[ Codeforces Round #554 (Div. 2) C]
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.
题意:给出两个整数a,b,求出使(a+k)*(b+k)/gcd(a+k,b+k)的值最小的k,如果有多组答案求出最小的k
题解:gcd(a+k,b+k)=gcd(a-b,b+k),所以gcd(a+k,b+k)一定是(a-b)的一个因子,也就是说a+k一定是(a-b)的因子的倍数,即a+k=q*t,所以直接枚举(a-b)的因子q,求出相应的q*t,然后就可以根据k=q*t-a求出k,然后就可以求出最小的(a+k)*(b+k)/gcd(a+k,b+k)了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define debug(x) cout<< #x <<" is "<<x<<endl;
int gcd(ll x,ll y){
if(y==)return x;
return gcd(y,x%y);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
if(n<m)swap(n,m);
ll xx=n-m;
ll ans=-;
ll ans0=;
for(ll i=;i*i<=xx;i++){
if(xx%i==){
ll y1=n/i;
if(n%i)y1++;
y1*=i;
y1-=n;
if((y1+n)*(y1+m)/gcd(y1+n,y1+m)<ans||ans==-){
ans=(y1+n)*(y1+m)/gcd(y1+n,y1+m);
ans0=y1;
}
else if((y1+n)*(y1+m)/gcd(y1+n,y1+m)==ans&&y1<ans0){
ans0=y1;
}
ll y2=n/(xx/i);
if(n%(xx/i))y2++;
y2*=(xx/i);
y2-=n;
if((y2+n)*(y2+m)/gcd(y2+n,y2+m)<ans||ans==-){
ans=(y2+n)*(y2+m)/gcd(y2+n,y2+m);
ans0=y2;
}
else if((y2+n)*(y2+m)/gcd(y2+n,y2+m)==ans&&y2<ans0){
ans0=y2;
}
}
}
printf("%lld\n",ans0);
return ;
}
[ Codeforces Round #554 (Div. 2) C]的更多相关文章
- 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) 1152B. Neko Performs Cat Furrier Transform
学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152B. Neko Performs Cat Furrier Transform 题目链接:"ht ...
- Codeforces Round #554 (Div. 2) 1152A - Neko Finds Grapes
学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152A - Neko Finds Grapes 题目链接:"https://codeforces. ...
- Codeforces Round #554 (Div. 2)-C(gcd应用)
题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Round #554 (Div. 2) C 数论
https://codeforces.com/contest/1152/problem/C 题意 a和b,找到k,使得lcm(a+k,b+k)最小(a,b:1e9) 题解 设gcd=gcd(a+k,b ...
- 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
A. Neko Finds Grapes 代码: #include <bits/stdc++.h> using namespace std; ; int N, M; int a[maxn] ...
- Codeforces Round #554 (Div. 2)自闭记
A 签到 #include<bits/stdc++.h> using namespace std; ],t[],ans; int main() { scanf("%d%d&quo ...
- Codeforces Round #554 (Div. 2) C. Neko does Maths(数学+GCD)
传送门 题意: 给出两个整数a,b: 求解使得LCM(a+k,b+k)最小的k,如果有多个k使得LCM()最小,输出最小的k: 思路: 刚开始推了好半天公式,一顿xjb乱操作: 后来,看了一下题解,看 ...
随机推荐
- min_25筛题目总结
看了网上众多博客后,我才发现,实现min_25只有脑子,没有代码. 当然可能是我太ruo了. min_25是一种想法,不是算法. 不要尝试套模板,因为很多题目并没有什么用. 最重要的一点,g不要看成是 ...
- Java作业:第四次过程性考核 ——长春职业技术学院 16级网络工程
Java作业:第四次过程性考核 码云链接:https://gitee.com/SoridoD/java_kaohe4 (时间匆忙没打注释,真有急事) (客户端和服务器会自动创建表,所以没有sql ...
- 搭建SSH框架心得
<Struts2+Spring4+hibernate3> 工程结构 导入相关jar包:自行网上百度即可,根据环境需要 写dao类 以及实现 package com.icommon.dao; ...
- C#第一课
C#是一种现代的面向对象的程序开发语言,它使得程序员能够在新的微软.NET平台上快速开发种类丰富的应用程序..NET平台提供了大量的工具和服务,能够最大限度地发掘和使用计算机通信能力. 相对于C或者C ...
- Mantis:Mantis rest api url 404 Not Found.解决过程纪录
测试Mantis rest api时碰到的问题:404 Not Found. 根据文件,Mantis rest api的预设url是{{url}}/api/rest/{{controller}}. 其 ...
- 软件测试人员必备网络知识(一):什么是cookie?
初入职场的新人,是不是经常会被一些基础的网络知识难住,又不敢问老大,只好默默的百度?纳,我花一个星期的加班时间,把这些经常要用到的网络知识点给整理出来了!这是一个系列的,如果对你们有用,后续还会继续 ...
- 解决nim db_mysql could not load: libmysql.dll的问题
title: 解决nim db_mysql could not load: libmysql.dll的问题 nim中使用db_mysql 操作数据库的代码看起来很简单: import db_mysql ...
- jmeter保持登录
1.添加配置元祖---http cookie 管理器(注意上面的http cookie管理器和其他的配置是对其的) 2.这里是当你想要配置多用户并发是要配置不同的账号,配置后记得上面的第一张图的清除c ...
- nginx——配置 Nginx 防盗链
什么是防盗链:简单地说,就是某些不法网站未经许可,通过在其自身网站程序里非法调用其他网站的资源,然后在自己的网站上显示这些调用的资源,使得被盗链的那一端消耗带宽资源 (1) 根据 HTTP refer ...
- 精进之路之AQS及相关组件
AQS ( AbstractQueuedSynchronizer)是一个用来构建锁和同步器的框架,使用AQS能简单且高效地构造出应用广泛的大量的同步器,比如我们提到的ReentrantLock,Sem ...