Codeforces C.Neko does Maths
题目描述:
1 second
256 megabytes
standard input
standard output
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.
Neko has two integers aa and bb. His goal is to find a non-negative integer kk such that the least common multiple of a+ka+k and b+kb+k is the smallest possible. If there are multiple optimal integers kk, 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 aa and bb (1≤a,b≤1091≤a,b≤109).
Print the smallest non-negative integer kk (k≥0k≥0) such that the lowest common multiple of a+ka+k and b+kb+k is the smallest possible.
If there are many possible integers kk giving the same value of the least common multiple, print the smallest one.
6 10
2
21 31
9
5 10
0
In the first test, one should choose k=2k=2, as the least common multiple of 6+26+2 and 10+210+2 is 2424, which is the smallest least common multiple possible.
思路:
刚开始拿到题:首先看了看1秒限时,时间快完了,我就抱着试一试的心态,用欧几里得求最大公约数的方法,把k从0一直循环到1000000(大概一秒钟)来求最大值,他时间要是够长我就一直算下去 ,看能够算到那一个测试点。
结束后:试着推一推。
设a=x1*g,b=x2*g;(g为a和b的最大公因数,x1,x2为系数)。a'=x1*g+k,b'=x2*g+k,则a'-b'=a-b=g(x1-x2);
因为a-b是个定值,现在题目要求的是将a和b一起加上某个数值后的公倍数最小。又因为gcd(a',b')=gcd(a'-b',a')=gcd(a-b,b')(证明提示见上方的式子)。
也就是说,现在我们要求的是(a'*b')/gcd(a',b')=(a'*b')/gcd(b-a,b');要这个式子最小,怎么办?
已知的是b-a的值,目标式的分母是b-a和b'的最大公因数,那么也是b-a的因数,因为b-a的因数有限,可以枚举出,那么对于每一个因数i,就设它是gcd(b-a,b'),就可以找到相应的b',即让i成为b-a和b'的最大公因数。可以求出k值,也就是b加上k能够使i成为其因数,有了k值就有了目标式的所有值,求出答案。小心数据范围和求因数时选择根号将时间减半以避免超时,还有就是一种特殊情况需要单独讨论,a-b=0时,上面的过程中会出现被0除的错误。
知识点:gcd
代码:
#include <iostream>
#include <cmath>
using namespace std;
long long a;
long long b;
long long ab;
long long k;
long long gcd(long long a,long long b)
{
long long r = a%b;
if(r==) return b;
return gcd(b,r);
}
int main()
{
cin >> a >> b;
if(a<b) swap(a,b);
ab = a-b;
//cout << "ab " << ab << endl;
long long m = a*b/gcd(a,b);
long long p = ;
if(ab!=)
{
for(int i = ; i<=sqrt(ab)+; i++)
{
if(ab%i==)
{
//cout << i << endl;
k = i-a%i;
//cout << "k " << k << endl;
long long nm = (a+k)*(b+k)/gcd(a+k,b+k);
if(nm<m)
{
m = nm;
p = k; }
k = ab/i-a%(ab/i);
//cout << "k" << endl;
nm = (a+k)*(b+k)/gcd(a+k,b+k);
if(nm<m)
{
m = nm;
p = k; }
}
}
}
cout << p << endl;
return ;
}
Codeforces C.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 Round #554 (Div. 2) C. Neko does Maths (简单推导)
题目:http://codeforces.com/contest/1152/problem/C 题意:给你a,b, 你可以找任意一个k 算出a+k,b+k的最小公倍数,让最小公倍数尽量小,求出 ...
- Neko does Maths CodeForces - 1152C 数论欧几里得
Neko does MathsCodeForces - 1152C 题目大意:给两个正整数a,b,找到一个非负整数k使得,a+k和b+k的最小公倍数最小,如果有多个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: •思路 时隔很久,又重新做这个题 温故果然可以知新❤ ...
- 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#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:/ ...
随机推荐
- spring boot 指定启动端口
spring boot 默认端口为8080 1.修改为指定端口 (1)修改配置文件 src/main/resources/application.properties server.port= (2) ...
- csu 1978: LXX的图论题
1978: LXX的图论题 Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 71 ...
- 解决angular+element原有组件样式不能覆盖element自带样式问题
在对应的组件中写入 ::ng-deep + 想要改变的element组件样式名即可
- 2019广东外语外贸大学CTF新手赛-密码学-RSA题解
题面 n=100000463700003241 e=17 密文: 分析: 题面已明示是RSA加密,已公开n与公钥e,n为1e18内的数字(64位).要爆破RSA,显然是先分析n的值. n的值是由两个素 ...
- day54——jquery补充、bootstrap
day54 jquery 页面载入 window.onload: 原生js的window.onload事件:// onload 等待页面所有内容加载完成之后自动触发的事件 window.onload ...
- Linux基础(03)gdb调试
1. 安装GDB增强工具 (gef) * GDB的版本大于7.7 * wget -q -O- https://github.com/hugsy/gef/raw/master/scripts/gef.s ...
- CLRS10.2-4练习 - 修改链表查询方法
要求: As written, each loop iteration in the LIST-SEARCH' procedure requires two tests:one for x ≠ L.n ...
- stompjs, websocket和nginx的配置
server { listen 8080; location /socket/ { proxy_pass http://socket_server/; proxy_s ...
- linux安装好的mysql rpm -qa |grep mysql不见
输入: rpm -qa|grep -i mysql
- java之spring mvc之页面跳转
1. 如果返回值为ModelAndView,在处理方法中,返回null时,默认跳转的视图名称为请求名.跳转结果会根据视图解析器来跳转. @RequestMapping("/hello.do& ...