C. Neko does Maths(数论 二进制枚举因数)
题目链接: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(数论 二进制枚举因数)的更多相关文章
- 牛客网 牛客练习赛43 F.Tachibana Kanade Loves Game-容斥(二进制枚举)+读入挂
链接:https://ac.nowcoder.com/acm/contest/548/F来源:牛客网 Tachibana Kanade Loves Game 时间限制:C/C++ 1秒,其他语言2秒 ...
- 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举
1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...
- Codeforces C.Neko does Maths
题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input stan ...
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- Good Bye 2015B(模拟或者二进制枚举)
B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Poj(2784),二进制枚举最小生成树
题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2436 二进制枚举+位运算
题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...
- hdu 3118(二进制枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...
- HDU 5025Saving Tang Monk BFS + 二进制枚举状态
3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...
随机推荐
- A Diversity-Promoting Objective Function for Neural Conversation Models论文阅读
本文来自李纪为博士的论文 A Diversity-Promoting Objective Function for Neural Conversation Models 1,概述 对于seq2seq模 ...
- flask轻量级框架入门
# -*- encoding: utf-8 -*- #导入Flask类, 导入重定向,url_for是简易寻址跳转, from flask import Flask,redirect,url_for, ...
- CodeForces Round #554 Div.2
A. Neko Finds Grapes 代码: #include <bits/stdc++.h> using namespace std; ; int N, M; int a[maxn] ...
- OpenCV4.1.0实践(2) - Dlib+OpenCV人脸特征检测
待更! 参考: python dlib opencv 人脸68点特征检测
- 如何在springboot项目中进行XSS过滤
简单介绍 XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意 ...
- Linux(Ubuntu)使用日记------trash-cli防止误删文件
1.安装过程 cd /tmp git clone https://github.com/andreafrancia/trash-cli cd trash-cli sudo python setup.p ...
- bugku crypto easy-crypto
长度不一的01字符串 就要考虑是不是摩斯密码 用这个py脚本: from __future__ import print_function a = input("input the stri ...
- Kafka学习笔记-如何保证高可用
一.术语 1.1 Broker Kafka 集群包含一个或多个服务器,服务器节点称为broker. broker存储topic的数据. 如果某topic有N个partition,集群有N个broker ...
- Http input plugin
logstash作为一个数据输入端.提供http服务,接收客户端的http请求,获取发送的内容. 在config目录下新建文件: vim config/http-input.yml input { h ...
- (十五)qt-tcp
基本流程 QT += core gui network #include "tcp.h" #include "ui_tcp.h" #include <QD ...