CodeForces-1152C-Neko does Maths
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?
The only line contains two integers a and b (1≤a,b≤10^9).
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.
| input |
| 6 10 |
| output |
| 2 |
| input |
| 21 31 |
| output |
| 9 |
| input |
| 5 10 |
| output |
| 0 |
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,根据LCM和GCD的关系知: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%di (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的更多相关文章
- codeforces#1152C. Neko does Maths(最小公倍数)
题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...
- Codeforces C.Neko does Maths
题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Neko does Maths CodeForces - 1152C 数论欧几里得
Neko does MathsCodeForces - 1152C 题目大意:给两个正整数a,b,找到一个非负整数k使得,a+k和b+k的最小公倍数最小,如果有多个k使得最小公倍数最小的话,输出最小的 ...
- 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 ...
- Codeforces Round #554 (Div. 2) C. Neko does Maths (简单推导)
题目:http://codeforces.com/contest/1152/problem/C 题意:给你a,b, 你可以找任意一个k 算出a+k,b+k的最小公倍数,让最小公倍数尽量小,求出 ...
- 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 ...
- Codeforces Round #554 (Div. 2) C. Neko does Maths(数学+GCD)
传送门 题意: 给出两个整数a,b: 求解使得LCM(a+k,b+k)最小的k,如果有多个k使得LCM()最小,输出最小的k: 思路: 刚开始推了好半天公式,一顿xjb乱操作: 后来,看了一下题解,看 ...
- 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: •思路 时隔很久,又重新做这个题 温故果然可以知新❤ ...
- codeforces#1152D. Neko and Aki's Prank(dp)
题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...
- C. Neko does Maths(数论 二进制枚举因数)
题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给你a和b,然后让你找到一个k,使得a+k和b+k的lcm. 学习网址:https:/ ...
随机推荐
- etcd增删改查
 { int a=1; get(a);//值传递 System.out.println(a); ...
- Visual C++ 里的 Classes, Methods and RTTI
类的基本布局 为了说明以下内容,让我们考虑这个简单的例子: class A { int a1; public: virtual int A_virt1(); virtual int A_virt2() ...
- pgloader 学习(二)特性矩阵&&命令行
pgloader 对于各种数据库支持的还是很完整的,同时有一套自己的dsl 特性矩阵 操作命令 命令格式 pgloader [<options>] [<command-file> ...
- Python 09 安装torch、torchvision
这个也是弄了我很久,百度了好多文章,其实像下面那样挺简单的,没那么复杂 1.进入torch的官网的下载页面,选择一下参数信息 地址:https://pytorch.org/get-started/lo ...
- C语言博客作业00--我的第一篇博客
1.你对网络专业或者计算机专业了解是怎样? 起初 起初对于我来说,计算机专业毕业后就相当于程序员,或者去开发一些游戏,软件等等,而学得特别优秀的可能会成为黑客,就像电影电视剧里演得那样,这是我一开始的 ...
- durpal安装时The translation server is offline解决
从https://localize.drupal.org/download下载语言文件上传到 目录/var/www/html/sites/default/files/translations 或者wg ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统-代码生成器用法
新的代码生成器比老的更加容易使用,要生成什么形式就选择什么形式,新的代码生成器采用的是WCF界面开发,同样采用开源的模式,根据自己使用习惯容易扩展 1.单列表模式 2.树形列表模式 3.左右列表模式 ...