CF1152C
http://codeforces.com/contest/1152/problem/C
大意:给两个数a,b。让你找一个k,使得a+k与b+k的最小公倍数尽可能小,输出k
思路: gcd(a,b) = gcd(b,a-b),最终得到的A,B,他们的gcd一定是a-b的因子
#include <bits/stdc++.h>
using namespace std;
// gcd(a,b) = gcd(b,a-b); -> (a-b)%gcd(a,b)==0
long long a,b,diff;
vector<long long> v;
int main(){
cin>>a>>b;
if(a>b)swap(a,b);
diff = b-a;
if(diff==0){
cout<<0<<endl;
goto _end;
}
long long ac,temp;
ac=0,temp=diff+7;
for(long long i=1;i*i<=diff;++i){ //枚举因子
if(diff%i==0){
v.push_back(i);
v.push_back(diff/i);
}
}
sort(v.begin(),v.end());
int pos;
pos = lower_bound(v.begin(),v.end(),a)-v.begin();
if(pos<v.size()){
ac = v[pos];
}else
while(ac<a){
ac += diff; //如果上面没找见>=a的因子,那么找diff的倍数直到>=a
}
cout<<ac-a<<endl;
_end:
return 0;
}