欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CodeForces-1152C-Neko does Maths

程序员文章站 2022-03-25 22:27:02
C. Neko does Maths time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Neko loves divisors. D ......
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard 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 (1a,b10^9).

output

print the smallest non-negative integer k (k0) 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
 
input
6 10
output
2

 

input
21 31
output
9
 
 
input
5 10
output
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.

 

题解

 假设a <= b,根据lcmgcd的关系知:lcm(a,b) = a*b/gcd(a,b),要求最小的k满足最小的lcm(a+k,b+k) = (a+k)*(b+k)/gcd(a+k,b+k),由更相减损法知,gcd(a+k,b+k) = gcd(a+k,b-a)。这样就使得gcd中有一项(即b-a)是固定的,这样有什么好处呢?没错,就是gcd(a+k,b-a)的结果一定是b-a的某个因子,而b-a的所有因子是有限个,且可以在sqrt(b-a)的时间内求出,故枚举b-a所有的因子即可。对于b-a的每一个因子di,都可以求出最小的ki = di - a%d(a%di != 0) 或者ki = 0 (a%di == 0),再代回公式计算,保留使得lcm(a+k,b+k)最小的k即可。

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #define re register
 8 #define il inline
 9 #define ll long long
10 #define ld long double
11 const ll maxn = 1e6+5;
12 const ll inf = 1e8;
13 
14 ll f[maxn];
15 
16 //快读
17 il ll read()
18 {
19     char ch = getchar();
20     ll res = 0, f = 1;
21     while(ch < '0' || ch > '9')
22     {
23         if(ch == '-')   f = -1;
24         ch = getchar();
25     }
26     while(ch >= '0' && ch <= '9')
27     {
28         res = (res<<1) + (res<<3) + (ch-'0');
29         ch = getchar();
30     }
31     return res*f;
32 }
33 
34 //辗转相除法
35 ll gcd(ll a, ll b)
36 {
37     ll mx = std::max(a,b);
38     ll mi = std::min(a,b);
39     return mi == 0 ? mx : gcd(mi,mx%mi);
40 }
41 
42 int main()
43 {
44     ll a = read();
45     ll b = read();
46     ll mx = std::max(a,b);
47     ll mi = std::min(a,b);
48     ll tot = 0;
49     ll c = mx-mi;
50     ll n = sqrt(c);
51     //枚举c的所有因子
52     for(re ll i = 1; i <= n; ++i)
53     {
54         if(!(c%i))
55         {
56             f[++tot] = i;
57             f[++tot] = c/i;
58         }
59     }
60     //依次代回c的因子计算
61     ll k = 0;
62     ll d = a*b/gcd(a,b);
63     for(re ll i = 1; i <= tot; ++i)
64     {
65         ll tk = !(mi%f[i]) ? 0 : f[i]-mi%f[i];
66         ll td = (a+tk)*(b+tk)/f[i];
67         if(td <= d)
68         {
69             if(td == d) k = std::min(k,tk);     //二者相同取最小的k
70             else    k = tk;
71             d = td;
72         }
73     }
74     printf("%lld\n", k);
75     return 0;
76 }