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:/ ...
随机推荐
- Linux CentOS7 下无图形界面安装Oracle11G R2版本
01,系统 Centos7 数据库版本 Oracle_11gR2 ,以及硬件要求 内存不能小于 1G,可用硬盘不小于8G Swap分区空间不小于2G grep MemTotal /proc/memin ...
- Apache显示目录列表及icons目录的问题
今天想部署下开源项目pig,发现它的mysql需要5.7.8 +,为了能支持多个版本并且可以方便切换,所以选择了phpstudy_pro 刚开始Apache不支持目录访问 修改配置 <Virtu ...
- AspNetCore 限流中间件IpRateLimitMiddleware 介绍
IpRateLimitMiddleware(Github: AspNetCoreRateLimit) 是ASPNETCore的一个限流的中间件,用于控制客户端调用API的频次, 如果客户端频繁访问服务 ...
- JavaEE 期末总结
struts2部分 一.struts2框架的集成: 1.web.xml配置struts2过滤器:前端控制器.核心控制器 如果有多个过滤器,需要将该过滤器放置到最后一个 2.struts.xml配置:主 ...
- @PropertySource绝对路径(java.io.FileNotFoundException问题解决)
经常使用@PropertySource 来指定配置文件,然后@value获取配置参数: @Component @PropertySource(value= {"classpath:rules ...
- oracle 在列名后的 (+)是什么意思,如何转换为mysql
外连接的意思select *from a,bwhere a.id=b.id(+)意思就是返回a,b中匹配的行 和 a中有但是b中没有的行. 参考https://www.cnblogs.com/Aaro ...
- Spring security oauth2 password flow
Spring security oauth2 包含以下两个endpoint来实现Authorization Server: AuthorizationEndpoint: 授权请求访问端点, 默认url ...
- [转帖]教你如何破解IC卡的校验值
教你如何破解IC卡的校验值 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin ...
- java 字符串转json,json转实体对象、json字符串转换成List、List转String、以及List排序等等...
@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...
- JVM中内存的设置和分配(最大内存,总内存,剩余内存的区别)
1.设置分配的内存大小 -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M -vmargs 说明后面是VM的参数,所以后面的 ...