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. 11、 Hadoop 2.x各个服务组件如何配置在那台服务器运行并测试

    HDFS模块 NameNode:是由哪个文件中的哪个配置属性指定的呢? core-site.xml文件中: <property> <name>fs.defaultFS</ ...

  2. siblings() 方法

    siblings([selected])       简介: 给定一个表示一组DOM元素的jQuery对象,该.siblings()方法允许我们在DOM树中搜索这些元素的兄弟节点,并从匹配的元素构造一 ...

  3. 【叔小生】JavaScript进阶篇

    如何插入JS JS基础语法 语法.函数.方法 提取字符串substring() substring() 方法用于提取字符串中介于两个指定下标之间的字符. <!DOCTYPE HTML> & ...

  4. Kafka 消费者到底是什么 以及消费者位移主题到底是什么(Python 客户端 1.01 broker)

    Kafka 中有这样一个概念消费者组,所有我们去订阅 topic 和 topic 交互的一些操作我们都是通过消费者组去交互的. 在 consumer 端设置了消费者的名字之后,该客户端可以对多个 to ...

  5. win10 自带计算器删除了怎么办

    win+S后输入Powershell,以管理员身份运行后,使用下面的命令:重要的说三遍:以管理员身份运行!以管理员身份运行!以管理员身份运行!Get-AppxPackage *calculator* ...

  6. 集成了SSM框架的系统怎么做测试?

    1.首先在测试文件夹下新建一个测试基类BaseTest BaseTest中的代码如下: package wbl_ssm_blog.mapper; import org.junit.Test; impo ...

  7. ACR Code Pacs

    ACR Index for Radiological Diagnosis 简称ACR Index,ACR Key或ACR Code,是一种应用于影像学分类的病理编码,由美国放射学院(American ...

  8. 2019_软工实践_Beta(1/5)

    队名:955 组长博客:点这里! 作业博客:点这里! 组员情况 组员1(组长):庄锡荣 过去两天完成了哪些任务 文字/口头描述  检测网站不合理的地方,给组员定下相应时间进度的安排 展示GitHub当 ...

  9. uniapp - picker

    [普通json数组] 针对官方的普通json数组示例,做些填充 <template> <view> <view class="uni-title uni-com ...

  10. 滚动事件优化 passive

    1.addEventListener参数 target.addEventListener(type, listener[, options]); target.addEventListener(typ ...