给出两个数a,b

求k     使得 a+k b+k有最小公倍数

a,b同时加上一个非负整数k,使得,a+k,b+k的最小公倍数最小

因为最小公公倍数=x*y / gcd(x,y),所以肯定离不开最大公约数了;

首先有个结论 gcd(x,y)=gcd(x,y-x) (y>x)

令c=gcd(x,y),那么x%c=0,y%c=0,(y-x)%c=0,所以gcd(x,y)=gcd(x,y-x)

因为题目中d=x-y的值不会变,所以我们就可以通过枚举d的因子,来凑a+k (d的因子也是(a+k)的因子)

拓展欧几里德 算法 待学。。。

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
LL gcd(LL a, LL b){
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b){
return a * b / gcd(a, b);
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
//gcd(a + k, b + k) == gcd(a - b, a + k);
LL a, b;
cin >> a >> b;
if(a < b) swap(a, b);
LL x = abs(a - b), ans = lcm(a, b), ansk = ;
for(LL i = , k; i * i <= x; i += ) if(x % i == ){
k = i - a % i;
if(lcm(a + k, b + k) < ans){
ans = lcm(a + k, b + k);
ansk = k;
}
k = x / i - a % (x / i);
if(lcm(a + k, b + k) < ans){
ans = lcm(a + k, b + k);
ansk = k;
}
}
if(a > b){ }
cout << ansk;
return ;
}

CF 552 Neko does Maths的更多相关文章

  1. Codeforces C.Neko does Maths

    题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input stan ...

  2. C. Neko does Maths

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. CF 552(div 3) E Two Teams 线段树,模拟链表

    题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...

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

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

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

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

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

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

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

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

  8. Codeforce Round #554 Div.2 C - Neko does Maths

    数论 gcd 看到这个题其实知道应该是和(a+k)(b+k)/gcd(a+k,b+k)有关,但是之后推了半天,思路全无. 然而..有一个引理: gcd(a, b) = gcd(a, b - a) = ...

  9. CF #552(div3)G 最小lcm

    题目链接:http://codeforces.com/contest/1154/problem/G 题意:lcm是最小公倍数,本题就是给你一个数组(可能会重复),要求你判断出那两个数的最小公倍数最小, ...

随机推荐

  1. python __call__或者说func()()的理解

    __call__ 对象后面加括号,触发执行. 注:构造方法的执行是由创建对象触发的,即:对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()() ...

  2. ArcGis dbf读写——挂接Excel到属性表 C#

    ArcMap提供了挂接Excel表格信息到属性表的功能,但是当数据量较大到以万计甚至十万计的时候这个功能就歇菜了,当然,你可以考虑分段挂接.这个挂接功能只是做了一个表关联,属性记录每个字段的信息需要通 ...

  3. Java使用POI解析Excel表格

    概述 Excel表格是常用的数据存储工具,项目中经常会遇到导入Excel和导出Excel的功能. 常见的Excel格式有xls和xlsx.07版本以后主要以基于XML的压缩格式作为默认文件格式xlsx ...

  4. EffectiveC++笔记 目录

    Charpter 1. 让自己习惯C++   条款01: 视C++为一个语言联邦 条款02: 尽量以const,enum,inline替换#define 条款03: 尽可能使用const 条款04: ...

  5. 【codeforces 983E】NN country

    Description In the NN country, there are n cities, numbered from 1 to n, and n−1 roads, connecting t ...

  6. windows 系统后台运行 jar 包

    windows平台下 后台运行 jar 包 1.cmd 下执行方式:后台运行  start /min java -server -Xms1024m -Xmx20480m -jar $JAR_NAME. ...

  7. slim.arg_scope中python技巧

    slim.arg_scope函数说明如下: Stores the default arguments for the given set of list_ops. For usage, please ...

  8. Tortoisegit图文使用教程

    本文只针对使用Tortoisegit的用户,使用命令行的后面可以不用看了 1.安装Git及Tortoisegit 先上图,首先需要把123按顺序安装了 Git下载地址:https://git-for- ...

  9. will not be managed by Spring/ [managed: 15; max: 15]

    检查事务配置 检查 mybatis 配置文件扫描路径是否正确 <!-- 自动扫描 --> <context:component-scan base-package="com ...

  10. springboot 共享session

    1.pom添加jar依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...