题目传送门

CF1152C 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 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?

翻译:

给定两个正整数a,b,找到非负整数k使a+k与b+k的最小公倍数最小,如有多解输出最小的k

输入格式

The only line contains two integers a and b ( 1≤a,b≤1e9 ).

输出格式

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.

样例

输入样例1

6 10

输出样例1

2

输入样例2

21 31

输出样例2

9

输入样例3

5 10

输出样例3

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.


暴力解法就是枚举判断是否满足条件,这肯定是会超时的。

正解

因为公式

所以

因为

其中 y > x

所以

因此,我们可以枚举(b-a)的因子x,并将x假设为gcd(a+k , b-a)

因为要求最小化    

所以(a+k) (b+k)应当尽可能的小,即k尽可能小

所以我们现在可以枚举x,并找到满足(a+k)%x==0 这个式子的最小k值(因为x必须是a+k的因子)

这个k值也可以用算出来

即        

其中  表示向下取整

再用k算出lcm(a+k,b+k)

并求出最小的lcm值和k值即可

代码如下代码很丑

#include<bits/stdc++.h>
using namespace std;
long long a,b;
long long lcm=1e15-1,x,k=999999999999,y;
int main()
{scanf("%lld%lld",&a,&b);if(a>b) swap(a,b);for(int i=1;i<=sqrt(1.0*(b-a));i++){if((b-a)%i!=0) continue;x=((a/i)+1)*i-a;y=(a+x)*(b+x)/i;if(y<lcm){lcm=y;k=x;}else if(y==lcm&&x<k)k=x;x=((a/((b-a)/i)+1))*((b-a)/i)-a; //别忘了因子是成对出现的y=(a+x)*(b+x)/((b-a)/i);if(y<lcm){lcm=y;k=x;}else if(y==lcm&&x<k)k=x;}cout<<k;return 0;
}

CF1152C Neko does Maths相关推荐

  1. C. Neko does Maths

    contents: 题 题意 思路 AC代码 题 Neko loves divisors. During the latest number theory lesson, he got an inte ...

  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

    https://codeforces.com/contest/1152/problem/C 题解: 扩展欧几得里定理 /* *@Author: STZG *@Language: C++ */ #inc ...

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

  5. 【CodeForces - 1152C 】Neko does Maths(数学数论,lcm,gcd性质)

    题干: 给出a,b<=1e9,你要找到最小的k使得lcm(a+k,b+k)尽可能小,如果有多个k给出同样的最小公倍数,输出最小的一个k. 解题报告: 因为题目中k太多了,先化简一下公式,假设a& ...

  6. C - Neko does Maths 数论

    求使得lcm(a+k,b+k)最小的时候的最小k. 根据同余定理. a%c==0 b%c == 0 -->(a-b)%c ==0 所以gcd(a,b)=gcd(a,fabs(b-a)) 这个题目 ...

  7. [欧拉路]CF1152E Neko and Flashback

    1152E - Neko and Flashback 题意:对于长为n的序列c和长为n - 1的排列p,我们可以按照如下方法得到长为n - 1的序列a,b,a',b'. ai = min(ci, ci ...

  8. Codeforces Round #554 (Div. 2) 1152A - Neko Finds Grapes

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152A - Neko Finds Grapes 题目链接:"https://codeforces. ...

  9. NEKO's Maze Game(思维)

    3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or alive NEKO#ΦωΦ has just got a new m ...

最新文章

  1. 面向对象网络编程基础 property的使用
  2. 关掉ajax 的异步,asp.net ajax 取消异步回送
  3. jdbcType与javaType的对应关系
  4. Winddows 10 安装 COCO API(pycocotools)
  5. 【Hadoop学起来】分布式Hadoop的搭建(Ubuntu 17.04)
  6. elupload获取文件名与路径_Uipath获取文件名,路径,扩展名等操作
  7. 散列碰撞_散列中的碰撞和碰撞解决技术
  8. plsql查询乱码问题解决
  9. Madagascar中的宏定义函数--绝对值函数和符号函数
  10. Helm 3 完整教程(九):Helm 函数讲解(3)类型转换函数、正则表达式函数
  11. [Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配...
  12. 深度神经网络——中文语音识别
  13. 【无标题】单分子纳米孔测序技术及其应用研究进展
  14. Stata制作限制立方样条(RCS)(2)
  15. 甲方和大厂外包,哪个更好?这是最醍醐灌顶的回答
  16. WIN10安装配置JDK
  17. 宏定义和函数有何区别
  18. linux查看本地主机名,linux如何查看主机名
  19. Mars 模拟器编写 mips32 汇编 的入门教程
  20. 一文详解双目立体视觉系统的精度提升方法!

热门文章

  1. 转:Yahoo!网站性能最佳体验的34条黄金守则——图片、Coockie与移动应用
  2. 滚动轴承特征频率计算推导过程
  3. 建筑师杰克帮你理解框架与模式
  4. Java学习笔记 (码龄七年第一次写笔记 续2)
  5. LoadRunner性能测试实战教程
  6. Win11彻底卸载WSL2系统(去除导航窗格Linux图标)
  7. IPsec:IKEv1和IKEv2区别
  8. 西安工大计算机学院李颖,李颖 -西安交通大学人文社会科学学院
  9. 制作ubuntu自定义系统镜像
  10. 【2】数据可视化:基于 Echarts + Python 实现的动态实时大屏 - 物流大数据