time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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 (1≤a,b≤109).

Output

Print the smallest non-negative integer k (k≥0) 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

inputCopy

6 10

outputCopy

2

inputCopy

21 31

outputCopy

9

inputCopy

5 10

outputCopy

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.

求x和y加上一个k之后,使(x+k)和(y+k)的最小公倍数最小

解法lcm(x,y)=x*y/gcd(x,y),那么求gcd(x+k,y+k)的最大就好了,而知道gcd(x,y)=gcd(y-x,x),因为如果gcd(x,y)=c,那么,x%c=0,y%c=0,(y-x)%c=0,则,求gcd(y-x,x+k),求出所有的y-x的因子,然后全部都拿来算一下,现在要知道因子,求对应的k值,可知k+x是因子的倍数,则k=因子-(因子%x)就可以了,然后每一次算一下lcm,得出最大的保留就好了

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#define sf scanf
#define scf(x) scanf("%lld",&x)
#define scff(x,y) scanf("%lld%lld",&x,&y)
#define scfff(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define vi vector<int>
#define mp make_pair
#define pf printf
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const double eps=1e-6;
const double pi=acos(-1.0);
const int inf=0x7fffffff;
const int N=1e7+7;
ll gcd(ll x,ll y)
{
return y==0?x:gcd(y,x%y);
}
ll lcm(ll x,ll y)
{
return x*y/(gcd(x,y));
}
vector<int> v;
int main()
{
ll x,y;scff(x,y);
if(x>y) swap(x,y);
ll ans=0,maxn=lcm(x,y),ss=y-x;
for(ll i=1;i*i<=ss;i++)
{
if(ss%i==0)
{
v.push_back(i);
if(i*i!=ss)
v.push_back(ss/i);
}
}
rep(i,0,v.size() )
{
ll t=0;
if(x%v[i]!=0)
t=v[i]-x%v[i];
ll now=lcm(x+t,y+t);
if(now<maxn)
{
maxn=now;
ans=t;
}
}
cout<<ans;
return 0;
}

C. Neko does Maths的更多相关文章

  1. Codeforces C.Neko does Maths

    题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input stan ...

  2. Neko does Maths CodeForces - 1152C 数论欧几里得

    Neko does MathsCodeForces - 1152C 题目大意:给两个正整数a,b,找到一个非负整数k使得,a+k和b+k的最小公倍数最小,如果有多个k使得最小公倍数最小的话,输出最小的 ...

  3. codeforces#1152C. Neko does Maths(最小公倍数)

    题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...

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

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

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

     题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给你a和b,然后让你找到一个k,使得a+k和b+k的lcm. 学习网址:https:/ ...

  6. CF 552 Neko does Maths

    给出两个数a,b 求k     使得 a+k b+k有最小公倍数 a,b同时加上一个非负整数k,使得,a+k,b+k的最小公倍数最小 因为最小公公倍数=x*y / gcd(x,y),所以肯定离不开最大 ...

  7. Codeforce Round #554 Div.2 C - Neko does Maths

    数论 gcd 看到这个题其实知道应该是和(a+k)(b+k)/gcd(a+k,b+k)有关,但是之后推了半天,思路全无. 然而..有一个引理: gcd(a, b) = gcd(a, b - a) = ...

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

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

  9. CF1152C Neko does Maths

    思路: 假设a <= b,lcm(a + k, b + k) = (a + k) * (b + k) / gcd(a + k, b + k) = (a + k) * (b + k) / gcd( ...

随机推荐

  1. 用mongo和redis查询排行榜、统计活跃用户

    nosql数据库能解决关系型数据库遇到的性能和扩展性的问题,本博客将以mongodb和redis两种nosql数据库为基础,简单的介绍下面两个业务场景的解决方案: 1.查询排行榜(以当日总步数排名为例 ...

  2. LTPS、IGZO、OLED都是什么?【转】

    转自:https://www.sohu.com/a/226454503_99913194 薄化行业的小伙伴,经常有听到LTPS.IGZO.OLED等英文缩写名词.但究竟他们是什么意思?他们之间有什么区 ...

  3. LightOJ 1372 (枚举 + 树状数组)

    题目 Link 输出序列中有多少个组合 {a1,a2,a3,a4,a5,a6}可以构成一个六边形. 分析 序列每个数都不相等. 所以可以设 a1<a2<a3<a4<a5< ...

  4. 【python系列】--Python变量和数据类型

    python数据类型 一.整数 Python可以处理任意大小的整数,当然包括负整数,在Python程序中,整数的表示方法和数学上的写法一模一样,例如:1,100,-8080,0,等等. 计算机由于使用 ...

  5. javascript--实现几个简单的操作

    利用JS实现几个简单的功能 --还有省市联动,商品的左右选择,表单的校验.但在学了jQuery之后,感觉这个没那么重要了,就不写了 表格的全选和全不选 表格的动态隔行换色 <!DOCTYPE h ...

  6. Scyther-Semantics and verification of Security Protocol

    1 .本书前一节主要是介作者自己的生平经历(读完感觉作者是个神童),目标明确作者13岁代码已经写的很溜了.自己也开了网络公司,但是后面又专注于自己的计算机基础理论,修了哲学的博士学位(不得不说很多专业 ...

  7. md5加密 bas64加密

    /** * 获取bas64加密的算法 * @param str * @return */ public static String getBase64(String str) { byte[] b = ...

  8. jQuery配合html2canvas 使用时 报错 Uncaught (in promise) Provided element is not within a Document

    报错代码: 这个函数运行时 function download(){ var element = $("#demo"); //jquery 获取元素 //这里将会报错 html2c ...

  9. Vue过滤器使用

    格式(一个过滤器):{{ 'msg' | filterA }} (多个过滤器):{{ 'msg' | filterA | filterB }} window.onload =function(){ / ...

  10. AWS S3服务使用

    AWS S3是亚马逊的一种文件存储服务使用方便. 一.配置服务 public static class AWS_S3ClientInfo { private static readonly strin ...