题目描述:

C. Neko does Maths
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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?

Input

The only line contains two integers aa and bb (1≤a,b≤1091≤a,b≤109).

Output

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.

Examples
input

Copy
6 10
output

Copy
2
input

Copy
21 31
output

Copy
9
input

Copy
5 10
output

Copy
0
Note

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的更多相关文章

  1. codeforces#1152C. Neko does Maths(最小公倍数)

    题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...

  2. Codeforces Round #554 (Div. 2) C. Neko does Maths (简单推导)

    题目:http://codeforces.com/contest/1152/problem/C 题意:给你a,b, 你可以找任意一个k     算出a+k,b+k的最小公倍数,让最小公倍数尽量小,求出 ...

  3. Neko does Maths CodeForces - 1152C 数论欧几里得

    Neko does MathsCodeForces - 1152C 题目大意:给两个正整数a,b,找到一个非负整数k使得,a+k和b+k的最小公倍数最小,如果有多个k使得最小公倍数最小的话,输出最小的 ...

  4. 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 ...

  5. Codeforces Round #554 (Div. 2) C. Neko does Maths(数学+GCD)

    传送门 题意: 给出两个整数a,b: 求解使得LCM(a+k,b+k)最小的k,如果有多个k使得LCM()最小,输出最小的k: 思路: 刚开始推了好半天公式,一顿xjb乱操作: 后来,看了一下题解,看 ...

  6. 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: •思路 时隔很久,又重新做这个题 温故果然可以知新❤ ...

  7. 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 ...

  8. codeforces#1152D. Neko and Aki's Prank(dp)

    题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...

  9. C. Neko does Maths(数论 二进制枚举因数)

     题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给你a和b,然后让你找到一个k,使得a+k和b+k的lcm. 学习网址:https:/ ...

随机推荐

  1. PowerShell学习笔记

    1,ps7官方文档 2,使用脚本生成帮助文档 function Add-Node { param ( $selectedNode, $name, $tag ) $newNode = new-objec ...

  2. 【记录】【MySQL】填充字符串函数 LPAD(str,len,padstr)

    LPAD(str,len,padstr) 1.如果str的长度等于len,那么就返回str 2.如果str的长度大于len,那么就返回str的前len个长度 3.如果str的长度小于len,那么就返回 ...

  3. ChecklistForTest

    相关字段内容较长时,页面显示是否正确(包括各主页面.明细页面.打印预览页面) 数据量较多时,页面显示是否正确(包括各主页面.明细页面.打印预览页面) 各字段为空校验(都为空,部分为空,都不为空)是否正 ...

  4. Spring Boot 项目的 API 接口防刷

    首先是写一个注解类 拦截器中实现 注册到springboot中 在Controller中加入注解 说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springbo ...

  5. SQL Server 数据库启动过程(用户数据库加载过程的疑难杂症)

    前言 本篇主要是上一篇文章的补充篇,上一篇我们介绍了SQL Server服务启动过程所遇到的一些问题和解决方法,可点击查看,我们此篇主要介绍的是SQL Server启动过程中关于用户数据库加载的流程, ...

  6. 【rt-thread】2、尝试用ENV添加18b20传感器

    尝试用ENV添加18b20传感器 rt-thread能通过env工具添加或者裁剪工程,这里调试的是通过ENV添加18b20传感器. 具体程序实现,可以参考以下资料 https://www.rt-thr ...

  7. golang的time包

    Time对象转换为string和时间戳调用Time对象的方法 转换为string:Time.Format(输出的格式) 转换为时间戳 :Time.Unix() 两者转换为Time对象的时候调用的是ti ...

  8. python_并发与通信

    独立的进程内存空间与共享的服务器进程空间 知识点一: 进程间通信的限制 进程是独立的,互不干扰的独立内存空间我们想不能修改变量但是,深层次问题是,这个进程与那个进程完全失去了联系 import mul ...

  9. TCP协议学习笔记

    TCP协议数据格式 TCP协议在互联网ISO协议的传输层. 在互联网传输过程中,互联网包在数据链路层,是传输数据的最基础的包.一个互联网的包包含IP包,即互联网包 = 互联网信息包头(至少20字节)+ ...

  10. repodata创建本地YUM仓库

    参考一createrepo是linux下的创建仓库的软件包.create是创建的意思,repo是repository的缩写,是仓库的意思. yum(Yellow dog Updater,Modifie ...