题目描述:

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

代码:

 #include <iostream>
#include <cmath>
using namespace std;
long long a;
long long b;
long long ab;
long long k;
long long gcd(long long a,long long b)
{
long long r = a%b;
if(r==) return b;
return gcd(b,r);
}
int main()
{
cin >> a >> b;
if(a<b) swap(a,b);
ab = a-b;
//cout << "ab " << ab << endl;
long long m = a*b/gcd(a,b);
long long p = ;
if(ab!=)
{
for(int i = ; i<=sqrt(ab)+; i++)
{
if(ab%i==)
{
//cout << i << endl;
k = i-a%i;
//cout << "k " << k << endl;
long long nm = (a+k)*(b+k)/gcd(a+k,b+k);
if(nm<m)
{
m = nm;
p = k; }
k = ab/i-a%(ab/i);
//cout << "k" << endl;
nm = (a+k)*(b+k)/gcd(a+k,b+k);
if(nm<m)
{
m = nm;
p = k; }
}
}
}
cout << p << endl;
return ;
}

最新文章

  1. Ubuntu下开启php调试模式,显示报错信息
  2. Visual Studio 默认保存为UTF8编码
  3. java并发编程(3):ThreadLocal
  4. HDOJ多校联合第六场
  5. Docker入门
  6. Selenium Web 自动化 - 项目持续集成(进阶)
  7. [leetcode-515-Find Largest Value in Each Tree Row]
  8. [转载]MySQL5.6 PERFORMANCE_SCHEMA 说明
  9. Varnish的vcl子程序
  10. ABP官方文档翻译 3.3 仓储
  11. Android 学习资料入门到精通(PDF集合)共54本
  12. 典型分布式系统分析:MapReduce
  13. [转] KVM I/O slowness on RHEL 6
  14. Linux学习笔记7
  15. Oracle单机Rman笔记[1]---环境准备
  16. 项目方说性能达到百万TPS,如何测试它的可信度?
  17. #C++初学记录(算法考试1)
  18. surgemq 添加ssl
  19. HDU 5701 中位数计数 (思维题)
  20. MissingNumber缺失的数字,FirstMissingPositive第一个缺失的正数

热门文章

  1. 【论文阅读】FaceBoxes- CPU Real-time Face Detector with High Accuracy
  2. Spring的@Autowired和@Resource注入
  3. Pycharm2018中DataBase的使用
  4. Linux下Ngnix的安装与配置
  5. 文件和异常练习——python编程从入门到实践
  6. Java-手动搭建SSH(maven版)
  7. (八)pdf的构成之文件体(page属性)
  8. ubuntu中安装python3和pip
  9. # - net - cannot access a disposed object r nobject name filebufferingreadstream
  10. 3.使用 Code First 迁移更新数据库