Codeforces Round #554 (Div. 2)C. Neko does Maths

C. Neko does Maths
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.

Neko has two integers a and b. His goal is to find a non-negative integer k such that the least common multiple of a+k and b+k is the smallest possible. If there are multiple optimal integers k, he needs to choose the smallest one.

Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it?

Input
The only line contains two integers a and b (1≤a,b≤109).

Output
Print the smallest non-negative integer k (k≥0) such that the lowest common multiple of a+k and b+k is the smallest possible.

If there are many possible integers k giving the same value of the least common multiple, print the smallest one.

Examples
inputCopy
6 10
outputCopy
2
inputCopy
21 31
outputCopy
9
inputCopy
5 10
outputCopy
0
Note
In the first test, one should choose k=2, as the least common multiple of 6+2 and 10+2 is 24, which is the smallest least common multiple possible.

求x和y加上一个k之后,使(x+k)和(y+k)的最小公倍数最小
解法lcm(x,y)=x*y/gcd(x,y),那么求gcd(x+k,y+k)的最大就好了,而知道gcd(x,y)=gcd(y-x,x),因为如果gcd(x,y)=c,那么,x%c=0,y%c=0,(y-x)%c=0,则,求gcd(y-x,x+k),求出所有的y-x的因子,然后全部都拿来算一下,现在要知道因子,求对应的k值,可知k+x是因子的倍数,则k=因子-(因子%x)就可以了,然后每一次算一下lcm,得出最大的保留就好了

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#define sf scanf
#define scf(x) scanf("%lld",&x)
#define scff(x,y) scanf("%lld%lld",&x,&y)
#define scfff(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define vi vector<int>
#define mp make_pair
#define pf printf
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const double eps=1e-6;
const double pi=acos(-1.0);
const int inf=0x7fffffff;
const int N=1e7+7;
ll gcd(ll x,ll y)
{
    return y==0?x:gcd(y,x%y);
}
ll lcm(ll x,ll y)
{
    return x*y/(gcd(x,y));
}
vector<int> v;
int main()
{ 
    ll x,y;scff(x,y);
    if(x>y) swap(x,y); 
    ll ans=0,maxn=lcm(x,y),ss=y-x;
    for(ll i=1;i*i<=ss;i++)
    {
        if(ss%i==0)
        {
            v.push_back(i);
            if(i*i!=ss) 
            v.push_back(ss/i);
        }
    }
    rep(i,0,v.size() )
    {
        ll t=0;
        if(x%v[i]!=0)
            t=v[i]-x%v[i];
        ll now=lcm(x+t,y+t);
        if(now<maxn)
        {
            maxn=now;
            ans=t;
        }
    }
    cout<<ans;
     return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值