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.

题意:给出两个整数a,b,求出使(a+k)*(b+k)/gcd(a+k,b+k)的值最小的k,如果有多组答案求出最小的k

题解:gcd(a+k,b+k)=gcd(a-b,b+k),所以gcd(a+k,b+k)一定是(a-b)的一个因子,也就是说a+k一定是(a-b)的因子的倍数,即a+k=q*t,所以直接枚举(a-b)的因子q,求出相应的q*t,然后就可以根据k=q*t-a求出k,然后就可以求出最小的(a+k)*(b+k)/gcd(a+k,b+k)了

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define debug(x) cout<< #x <<" is "<<x<<endl;
int gcd(ll x,ll y){
if(y==)return x;
return gcd(y,x%y);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
if(n<m)swap(n,m);
ll xx=n-m;
ll ans=-;
ll ans0=;
for(ll i=;i*i<=xx;i++){
if(xx%i==){
ll y1=n/i;
if(n%i)y1++;
y1*=i;
y1-=n;
if((y1+n)*(y1+m)/gcd(y1+n,y1+m)<ans||ans==-){
ans=(y1+n)*(y1+m)/gcd(y1+n,y1+m);
ans0=y1;
}
else if((y1+n)*(y1+m)/gcd(y1+n,y1+m)==ans&&y1<ans0){
ans0=y1;
}
ll y2=n/(xx/i);
if(n%(xx/i))y2++;
y2*=(xx/i);
y2-=n;
if((y2+n)*(y2+m)/gcd(y2+n,y2+m)<ans||ans==-){
ans=(y2+n)*(y2+m)/gcd(y2+n,y2+m);
ans0=y2;
}
else if((y2+n)*(y2+m)/gcd(y2+n,y2+m)==ans&&y2<ans0){
ans0=y2;
}
}
}
printf("%lld\n",ans0);
return ;
}

[ Codeforces Round #554 (Div. 2) C]的更多相关文章

  1. Codeforces Round #554 (Div. 2) C. Neko does Maths (简单推导)

    题目:http://codeforces.com/contest/1152/problem/C 题意:给你a,b, 你可以找任意一个k     算出a+k,b+k的最小公倍数,让最小公倍数尽量小,求出 ...

  2. Codeforces Round #554 (Div. 2) 1152B. Neko Performs Cat Furrier Transform

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152B. Neko Performs Cat Furrier Transform 题目链接:"ht ...

  3. Codeforces Round #554 (Div. 2) 1152A - Neko Finds Grapes

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152A - Neko Finds Grapes 题目链接:"https://codeforces. ...

  4. Codeforces Round #554 (Div. 2)-C(gcd应用)

    题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k ...

  5. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

  6. Codeforces Round #554 (Div. 2) C 数论

    https://codeforces.com/contest/1152/problem/C 题意 a和b,找到k,使得lcm(a+k,b+k)最小(a,b:1e9) 题解 设gcd=gcd(a+k,b ...

  7. 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&l ...

  8. CodeForces Round #554 Div.2

    A. Neko Finds Grapes 代码: #include <bits/stdc++.h> using namespace std; ; int N, M; int a[maxn] ...

  9. Codeforces Round #554 (Div. 2)自闭记

    A 签到 #include<bits/stdc++.h> using namespace std; ],t[],ans; int main() { scanf("%d%d&quo ...

  10. Codeforces Round #554 (Div. 2) C. Neko does Maths(数学+GCD)

    传送门 题意: 给出两个整数a,b: 求解使得LCM(a+k,b+k)最小的k,如果有多个k使得LCM()最小,输出最小的k: 思路: 刚开始推了好半天公式,一顿xjb乱操作: 后来,看了一下题解,看 ...

随机推荐

  1. unity5.6 导出gradle工程,Android Studio 导入问题以及解决

    导入后gradle building 一直到跑,卡住了,一般是gradle没有下载,又下不下来的原因. 去  http://services.gradle.org/distributions/  下载 ...

  2. 响应式编程系列(一):什么是响应式编程?reactor入门

    响应式编程 系列文章目录 (一)什么是响应式编程?reactor入门 (二)Flux入门学习:流的概念,特性和基本操作 (三)Flux深入学习:流的高级特性和进阶用法 (四)reactor-core响 ...

  3. 复制程序到notepad++编辑器出现乱码的解决方法

    1.不从别的编辑器打开的文件中直接复制代码到notepad++打开的文件中,也不从notepad++打开的文件中复制到别的编辑器打开的文件中.但是确实想移植一些代码怎么办呢?具体方法:在别的编译器编辑 ...

  4. 阻塞队列 - java基于链表的简单实现

    1.阻塞队列的原理 阻塞队列与普通队列的区别在于:阻塞队列为空时,从队列中获取元素的操作将会被阻塞,当队列为满时,往队列里添加元素的操作会被阻塞. 试图从空的阻塞队列中获取元素的线程将会被阻塞,直到其 ...

  5. linux 普通用户授权root相关权限

    先查看当前用户(test)是否有特权 [test@web01 ~]$ sudo -l We trust you have received the usual lecture from the loc ...

  6. 如何用jmeter进行数据库性能测试

    由于业务需要,需要进行数据库性能测试,记录过程进行学习 测试前期准备: 1.测试点准备及需求点 a.性能测试目的 b.jmeter测试数据库基本脚本  get c.数据库性能监控工具选择 d.服务器性 ...

  7. 最全面的Redis命令行查阅手册(收藏查看)

    Redis是用C语言实现的,一般来说C语言实现的程序“距离”操作系统更近,执行速度相对会更快. Redis使用了单线程架构,预防了多线程可能产生的竞争问题. 作者对于Redis源代码可以说是精打细磨, ...

  8. 《Mem2Seq: Effectively Incorporating Knowledge Bases into End-to-EndTask-Oriented Dialog Systems》

    Multihop Attention Networks (MANs) https://zhuanlan.zhihu.com/p/52067672 https://blog.csdn.net/qq_38 ...

  9. 使用 <embed> 标签显示 flash文件(swf)格式 ,如何设置 width 和 height 宽度,高度.

    1. embed 标签 支持  .swf 格式.     .flv 的不支持. 2. 通常情况下, 网站中上传 多个 flash文件. 它的默认大小是不一样的. 而且 可以 宽度 大于 高度(横向的) ...

  10. 石头的Xcode学习列表

    http://www.cnblogs.com/JangoJing/tag/xcode/