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:/ ...
随机推荐
- 利用vue-meta管理头部标签
在 Vue SPA 应用中,如果想要修改HTML的头部标签,或许,你会在代码里,直接这么做 // 改下title document.title = 'what?' // 引入一段script let ...
- Postgresql 数据库迁移步骤
1.操作位置:迁移数据库源(旧数据库主机) 找到PostgreSql 的data目录 关闭数据库进程 打包 tar -zcvf pgdatabak.tar.gz data/ ----------- ...
- 缺陷的优先程度(Priority)
测试人员希望程序员什么时间哪个版本修改该bug (1)Urgent 立即修改否则影响开发进度 (2)Veryhigh 本版本修改 (3)High 下个版本修改 (4)Medium 发布前修改 (5)L ...
- QML学习(四)——<Text显示>
文本显示是界面开发必不可少的内容,在Qt Quick模块中提供了 Text 项目来进行文本的显示,其中可以使用 font 属性组对文本字体进行设置.这一篇我们来看看它们的具体使用. 使用字体 就像前面 ...
- Luogu3379 【模板】最近公共祖先(LCA)
题面 题解 这里讲一种硬核做法. 首先\(\mathrm{dfs}\)整棵树,求出这棵树的欧拉序,然后\(\mathrm{LCA}\)问题就变成了\(\pm 1\mathrm{RMQ}\)问题. 考虑 ...
- 配置Always On AG
1.准备测试环境的服务器 在 Always On AG 中如果需要自动 Failover 至少需要集群中有 3 台服务器,但是我只是测试功能,因此只使用了两台服务器.并且本文不涉及任何 Pacemak ...
- TypeScript之Https通信
NetWorkRequest.ts(源代码如下) import * as https from "https"; import * as vscode from 'vscode'; ...
- SpringCloud Feign通过FallbackFactory显示异常信息
SpringCloud Feign可以进行服务消费,而且内置了Hystrix,能够进行熔断. Feign可以通过fallback指定熔断回调的类.代码示例及讲解可见: https://www.cnbl ...
- qtcreator 添加 cppreference 离线文档
https://en.cppreference.com/w/File:qch_book_20190607.zip 下载后放到 D:\Qt\Qt5.10.0\Docs\Qt-5.10.0目录下, 并在q ...
- MacOS安装rJava
rJava出了名的难装,一大堆问题. 核心的问题: 1. java版本问题,最好用1.8版本的java 2. 编译器的问题 3. 相关头文件header的问题 之前几次装过,但都放弃了,这次花了一下午 ...