Codeforces . C.Neko does Maths

题目描述:

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. During the latest number theory lesson, he got an interesting exercise from his math teacher.

Neko has two integers aa and bb. His goal is to find a non-negative integer kk such that the least common multiple of a+ka+k and b+kb+k is the smallest possible. If there are multiple optimal integers kk, 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 aa and bb (1≤a,b≤1091≤a,b≤109).

Output

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

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

Examples

input

Copy

6 10

output

Copy

2

input

Copy

21 31

output

Copy

9

input

Copy

5 10

output

Copy

0

Note

In the first test, one should choose k=2k=2, as the least common multiple of 6+26+2 and 10+210+2 is 2424, which is the smallest least common multiple possible.

思路:

刚开始拿到题:首先看了看1秒限时,时间快完了,我就抱着试一试的心态,用欧几里得求最大公约数的方法,把k从0一直循环到1000000(大概一秒钟)来求最大值,他时间要是够长我就一直算下去 ,看能够算到那一个测试点。

结束后:试着推一推。

设a=x1*g,b=x2*g;(g为a和b的最大公因数,x1,x2为系数)。a‘=x1*g+k,b‘=x2*g+k,则a‘-b‘=a-b=g(x1-x2);

因为a-b是个定值,现在题目要求的是将a和b一起加上某个数值后的公倍数最小。又因为gcd(a‘,b‘)=gcd(a‘-b‘,a‘)=gcd(a-b,b‘)(证明提示见上方的式子)。

也就是说,现在我们要求的是(a‘*b‘)/gcd(a‘,b‘)=(a‘*b‘)/gcd(b-a,b‘);要这个式子最小,怎么办?

已知的是b-a的值,目标式的分母是b-a和b‘的最大公因数,那么也是b-a的因数,因为b-a的因数有限,可以枚举出,那么对于每一个因数i,就设它是gcd(b-a,b‘),就可以找到相应的b‘,即让i成为b-a和b‘的最大公因数。可以求出k值,也就是b加上k能够使i成为其因数,有了k值就有了目标式的所有值,求出答案。小心数据范围和求因数时选择根号将时间减半以避免超时,还有就是一种特殊情况需要单独讨论,a-b=0时,上面的过程中会出现被0除的错误。

知识点:gcd

代码:

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 long long a;
 5 long long b;
 6 long long ab;
 7 long long k;
 8 long long gcd(long long a,long long b)
 9 {
10     long long r = a%b;
11     if(r==0) return b;
12     return gcd(b,r);
13 }
14 int main()
15 {
16     cin >> a >> b;
17     if(a<b) swap(a,b);
18     ab = a-b;
19     //cout << "ab " << ab << endl;
20     long long m = a*b/gcd(a,b);
21     long long p = 0;
22     if(ab!=0)
23     {
24         for(int i = 1; i<=sqrt(ab)+1; i++)
25         {
26             if(ab%i==0)
27             {
28                 //cout << i << endl;
29                 k = i-a%i;
30                 //cout << "k " << k << endl;
31                 long long nm = (a+k)*(b+k)/gcd(a+k,b+k);
32                 if(nm<m)
33                 {
34                     m = nm;
35                     p = k;
36
37                 }
38                 k = ab/i-a%(ab/i);
39                 //cout << "k" << endl;
40                 nm = (a+k)*(b+k)/gcd(a+k,b+k);
41                 if(nm<m)
42                 {
43                     m = nm;
44                     p = k;
45
46                 }
47             }
48         }
49     }
50     cout << p << endl;
51     return 0;
52 }

原文地址:https://www.cnblogs.com/zhanhonhao/p/11219460.html

时间: 2024-08-30 11:11:06

Codeforces . C.Neko does Maths的相关文章

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

学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152C. Neko does Maths 题目链接:"https://codeforces.com/contest/1152/problem/C" 题目大意:给你两个数a,b,现在要你找出一个数k使得(a+k)和(b+k)的最小公倍数最小. 题目思路:暴力(逃) 这题没得思路,想了想既然求LCM了那么和GCD说不定有点关系 然后就没有然后了 比赛的时候交了一发暴力上去,然并软 赛后补题,题解里面

CodeForces-1152C Neko does Maths(GCD)

题目传送门:CodeForces-1152C Neko does Maths 题目大意: 给你两个值a,b,让你求出使lcm(a+k,b+k),最小的k值 gcd性质: gcd性质:gcd(a,a) = gcd(a,a) gcd(a,b) = gcd(a,a-b)(a>b) gcd(a,b) = gcd(b,a-b)(a>b)证明:设gcd(a,b)=z; a=zx ,b=zy ,a-b=z(x-y) 要证明a-b和b的gcd等于a和b的gcd b=zy a-b=z(x-y) 则需要证明y和x

CodeForces 1292A NEKO&#39;s Maze Game(思维)

1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <

Codeforces Round #554 (Div. 2) C.Neko does Maths (gcd的运用)

题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给定两个正整数a,b,其中(1<=a,b<=1e9),求一个正整数k(0<=k),使得a+k与b+k的最小公倍数最小. 解题思路:首先我们需要知道gcd(a,b)=gcd(a,b-a)=gcd(b,b-a)(b>a)的 我们要求的是lcm(a+k,b+k)=(a+k)(b+k)/gcd(a+k,b+k)=(a+k)(b+k)/gcd(a+k,b-a) 因为b-a是定值,所

Codeforces Round #554 (Div. 2) C. Neko does Maths (数论 GCD(a,b) = GCD(a,b-a))

传送门 •题意 给出两个正整数 a,b: 求解 k ,使得 LCM(a+k,b+k) 最小,如果有多个 k 使得 LCM() 最小,输出最小的k: •思路 时隔很久,又重新做这个题 温故果然可以知新? 重要知识点 GCD(a,b)=GCD(a,b-a)=GCD(b,b-a) (b>a) 证明: 设GCD(a,b)=c 则a%c=0,b%c=0,(b-a)%c=0 所以GCD(a,b-a)=c 得GCD(a,b)=GCD(a,b-a) gcd(a+k,b-a)肯定是(b-a)的因子 所以gcd(a

C. Neko does Maths(数论 二进制枚举因数)

 题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给你a和b,然后让你找到一个k,使得a+k和b+k的lcm. 学习网址:https://blog.csdn.net/yopilipala/article/details/89517933 具体思路:   AC代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 # define ll long long 4 # define

CodeForces 1152F1 Neko Rules the Catniverse (Small Version)

题目链接:http://codeforces.com/problemset/problem/1152/F1 题目大意 有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq y \leq x + m$,且 y 不能被访问过. 求游玩其中 k 个星球有多少种不同的方案? 分析 一开始我的想法是二维 dp,dp[i][j] 表示前 i 个星球,访问其中 j 个,一共的方案种数,然后在遍历到第 i + 1 个星球的时候,很明显有访问和不访问两种操作,不访问好算,直接赋值即可,

CodeForces 1152F2 Neko Rules the Catniverse (Large Version)

题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem/1152/F1,此题 n 最大能到 109. 分析 在 F1 的基础上, 代码如下 时间复杂度:$O(\log n*(k*2^m)^3)$ 原文地址:https://www.cnblogs.com/zaq19970105/p/10886953.html

Codeforces 1152E Neko and Flashback 欧拉路径

Neko and Flashback 把a[ i ] - b[ i ] 看成边, 就是求一遍欧拉路径就好了. 注意图不连通的情况.. #include<bits/stdc++.h> #define fi first #define se second #define mk make_pair #define PII pair<int, int> using namespace std; const int N = (int)2e5 + 7; int n, a[N], b[N], d