题目链接:https://codeforces.com/contest/1152/problem/C

题目大意:给你a和b,然后让你找到一个k,使得a+k和b+k的lcm.

学习网址:https://blog.csdn.net/yopilipala/article/details/89517933

具体思路:

 

AC代码:

 #include<bits/stdc++.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 2e5+;
vector<ll>sto;
void init(ll t)
{
ll tmp=t;
for(ll i=; i*i<=tmp; i++)
{
while(t%i==)
{
t/=i;
sto.push_back(i);
}
}
if(t!=)
sto.push_back(t);
}
ll cal(int t)
{
ll ans=1ll;
int pos=;
while(t)
{
if(t&)
{
ans=ans*sto[pos];
}
pos++;
t>>=;
}
return ans;
}
int main()
{
ll a,b;
scanf("%lld %lld",&a,&b);
if(a==b)
{
printf("0\n");
return ;
}
if(a>b)
swap(a,b);
init(b-a);
int maxstate=(<<(sto.size()))-;
ll minn=(1ll<<);
ll ans=(1ll<<);
for(int i=; i<=maxstate; i++)
{
ll tmp=cal(i);
ll t1=(a/tmp+)*tmp;//注意是先除
ll t2=(b/tmp+)*tmp;
ll w=t1*t2/__gcd(t1,t2);
if(w<=minn)
{
if(w<minn)
{
minn=w;
ans=t1;
}
else if(w==minn)
{
minn=w;
ans=min(ans,t1);
}
}
}
ll tmp=a*b/__gcd(a,b);
if(tmp<=minn)
{
minn=tmp;
ans=a;
}
printf("%lld\n",ans-a);
return ;
}

 

C. Neko does Maths(数论 二进制枚举因数)的更多相关文章

  1. 牛客网 牛客练习赛43 F.Tachibana Kanade Loves Game-容斥(二进制枚举)+读入挂

    链接:https://ac.nowcoder.com/acm/contest/548/F来源:牛客网 Tachibana Kanade Loves Game 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举

    1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...

  3. Codeforces C.Neko does Maths

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

  4. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  5. Good Bye 2015B(模拟或者二进制枚举)

    B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. Poj(2784),二进制枚举最小生成树

    题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  7. POJ 2436 二进制枚举+位运算

    题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...

  8. hdu 3118(二进制枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...

  9. HDU 5025Saving Tang Monk BFS + 二进制枚举状态

    3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...

随机推荐

  1. jenkins自动化部署vue

    一.nodejs配置 首先加入nodejs插件 在配置里面配置这个插件 这样我们就能在自动构建发布的配置里看到nodejs的编译选项了 二.发布配置 首先新建一个自由风格的项目 然后配置构建保留天数和 ...

  2. margin塌陷与margin合并(margin),清除浮动

    **1.margin塌陷**问题:垂直方向的父子关系的盒子使用不当会产生margin塌陷.给子级设置margin-top时,他不会相对父级一起动,只有他的margin超过父级的margin时,才会生效 ...

  3. [官网]Windows modules

    Windows modules https://docs.ansible.com/ansible/latest/modules/list_of_windows_modules.html win_acl ...

  4. 如何在django视图中使用asyncio(协程)和ThreadPoolExecutor(多线程)

    Django视图函数执行,不在主线程中,直接 loop = asyncio.new_event_loop() # 更不能loop = asyncio.get_event_loop() 会触发 Runt ...

  5. MyBatis-Plus

    一.通用SQL 1.简介:(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 2.定义Javabean中成员变量所使用的的类型: ...

  6. ftm国际化解决方案

    记录一下踩过的坑,在使用ftm:message的时候我发现这个的国际化是依赖于本地浏览器的语言环境的!关于自主设置这个语言的方法有如下3种:(个人建议使用第二种,可以更加灵活且有效!第一种我这边没有生 ...

  7. Python——SQL——将查询的数据列表化

    sql = 'select paihao from yang1.cailiaopai' data = datebase.shujuku.sj_select(sql) #将获得的数据进行列表化 data ...

  8. cesium-navigation 使用(非require,es6引用)

    cesium-navigation-es6(非require,es6引用) 为什么你建立cesium-navigation插件? 插件形式不影响之前项目使用,es6 使用 首先,所有的Cesiumjs ...

  9. linux搭建所遇到的坑elasticsearch-6.3.0

    注意: 不能使用主账号(root账号运行,必须使用子账号登录) 第一步安装:: wget https://artifacts.elastic.co/downloads/elasticsearch/el ...

  10. 树莓派3B+(二)

    一.安装SSH工具 这里用的是putty,下载下来是一个exe文件,点开就能用. 下载地址:https://www.chiark.greenend.org.uk/~sgtatham/putty/lat ...