• 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
    ",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;
    }
    
    
  • 相关阅读:
    vps云服务器建站后绑定域名的方法?
    怎么做局域网远程联机?
    解决服务器Active&nbsp;Directory环境里Windows登录性能问题办法
    搭建dns域名服务器过程
    服务器电源常见故障判断及处理方法
    有些网站为什么要使用CDN,CDN又是什么呢
    独立服务器使用技巧介绍
    云服务器和虚拟主机的区别
    CDN在中国的发展的九个年头的点点滴滴
    网络安全的攻防战争
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/10771980.html
Copyright © 2020-2023  润新知