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 (1≤a,b≤10^9).

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
 
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即可。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#define re register
#define il inline
#define ll long long
#define ld long double
const ll MAXN = 1e6+;
const ll INF = 1e8; ll f[MAXN]; //快读
il ll read()
{
char ch = getchar();
ll res = , f = ;
while(ch < '' || ch > '')
{
if(ch == '-') f = -;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
res = (res<<) + (res<<) + (ch-'');
ch = getchar();
}
return res*f;
} //辗转相除法
ll gcd(ll a, ll b)
{
ll mx = std::max(a,b);
ll mi = std::min(a,b);
return mi == ? mx : gcd(mi,mx%mi);
} int main()
{
ll a = read();
ll b = read();
ll mx = std::max(a,b);
ll mi = std::min(a,b);
ll tot = ;
ll c = mx-mi;
ll n = sqrt(c);
//枚举c的所有因子
for(re ll i = ; i <= n; ++i)
{
if(!(c%i))
{
f[++tot] = i;
f[++tot] = c/i;
}
}
//依次代回c的因子计算
ll k = ;
ll d = a*b/gcd(a,b);
for(re ll i = ; i <= tot; ++i)
{
ll tk = !(mi%f[i]) ? : f[i]-mi%f[i];
ll td = (a+tk)*(b+tk)/f[i];
if(td <= d)
{
if(td == d) k = std::min(k,tk); //二者相同取最小的k
else k = tk;
d = td;
}
}
printf("%lld\n", k);
return ;
}

CodeForces-1152C-Neko does Maths的更多相关文章

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

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

  2. Codeforces C.Neko does Maths

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

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

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

  4. L - Neko does Maths CodeForces - 1152C 数论(gcd)

    题目大意:输入两个数 a,b,输出一个k使得lcm(a+k,b+k)尽可能的小,如果有多个K,输出最小的. 题解: 假设gcd(a+k,b+k)=z; 那么(a+k)%z=(b+k)%z=0. a%z ...

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

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

  6. 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 ...

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

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

  8. 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: •思路 时隔很久,又重新做这个题 温故果然可以知新❤ ...

  9. codeforces#1152D. Neko and Aki's Prank(dp)

    题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...

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

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

随机推荐

  1. LeetCode 1046. Last Stone Weight

    原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...

  2. CLR内部异常(中)

    不捕捉某一个异常 常常有这种情况,代码不需要捕捉异常,但需要执行一些清理或者修正操作.虽然不总是,支持物(holders)经常用在这种场景里.在支持物(holders)不适用的情况里,CLR提供了两个 ...

  3. 使用rrweb 进行web 操作录制以及回放

    rrweb 是使用typescript 开发的web 操作录制以及回放框架,包含了比较完整的系统组件 rrweb-snapshot 进行dom 与操作实践的关联处理 rrweb 主要包含了record ...

  4. [RN] React Native 实现 FlatList上拉加载

     FlatList可以利用官方组件 RefreshControl实现下拉刷新功能,但官方没有提供相应的上拉加载的组件,因此在RN中实现上拉加载比下拉刷新要复杂一点. 不过我们仍可以通过FlatList ...

  5. 如何学习uni-app?

    uni-app 是一个使用 Vue.js 开发跨平台应用的前端框架. 开发者通过编写 Vue.js 代码,uni-app 将其编译到iOS.Android.微信小程序.H5等多个平台,保证其正确运行并 ...

  6. RESTFull开发风格

  7. [学习笔记] kd-tree

    本文参考这位dalao的题解 前置技能:二叉查找树 其实kd-tree很简单的啦 和BST都差不多的啦 就是在划分的时候把每一维都比较一下就行啦 (\(dalao\)的kd-tree教程) 然而本蒟蒻 ...

  8. 对snapshot isolation和write-snapshot isolation的一些思考

    数据库中存在读异常和写异常. 所谓snapshot,目的在于保证事务执行的各个阶段,读相同的数据项得到的结果没有变化,这样一来就避免了不可重复读.幻读等读数据异常. 但是仅仅是读数据不变还不够,因为这 ...

  9. 分布式CAP理论介绍:一致性(Consistency),可用性(Availability),容忍网络分区(Partition tolerance)

    在理论计算机科学中,CAP定理(CAP theorem),又被称作布鲁尔定理(Brewer's theorem),它指出对于一个分布式计算系统来说,不可能同时满足以下三点: 一致性(Consisten ...

  10. Bi-Directional ConvLSTM U-Net with Densley Connected Convolutions

    Bi-Directional ConvLSTM U-Net with Densley Connected Convolutions  ICCV workshop 2019  2019-09-15 11 ...