POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)
【题目链接】 http://poj.org/problem?id=2429
【题目大意】
给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小
【题解】
我们发现,(x/gcd)*(y/gcd)=lcm/gcd,并且x/gcd和y/gcd互质
那么我们先利用把所有的质数求出来Pollard_Rho,将相同的质数合并
现在的问题转变成把合并后的质数分为两堆,使得x+y最小
我们考虑不等式a+b>=2sqrt(ab),在a趋向于sqrt(ab)的时候a+b越小
所以我们通过搜索求出最逼近sqrt(ab)的值即可。
【代码】
#include <cstdio>
#include <algorithm>
#include <cmath>
#define C 2730
#define S 3
using namespace std;
typedef long long ll;
ll n,m,s[1000],cnt,f[1000],cnf,ans;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mul(ll a,ll b,ll n){return(a*b-(ll)(a/(long double)n*b+1e-3)*n+n)%n;}
ll pow(ll a, ll b, ll n){
ll d=1; a%=n;
while(b){
if(b&1)d=mul(d,a,n);
a=mul(a,a,n);
b>>=1;
}return d;
}
bool check(ll a,ll n){
ll m=n-1,x,y;int i,j=0;
while(!(m&1))m>>=1,j++;
x=pow(a,m,n);
for(i=1;i<=j;x=y,i++){
y=pow(x,2,n);
if((y==1)&&(x!=1)&&(x!=n-1))return 1;
}return y!=1;
}
bool miller_rabin(int times,ll n){
ll a;
if(n==1)return 0;
if(n==2)return 1;
if(!(n&1))return 0;
while(times--)if(check(rand()%(n-1)+1,n))return 0;
return 1;
}
ll pollard_rho(ll n,int c){
ll i=1,k=2,x=rand()%n,y=x,d;
while(1){
i++,x=(mul(x,x,n)+c)%n,d=gcd(y-x,n);
if(d>1&&d<n)return d;
if(y==x)return n;
if(i==k)y=x,k<<=1;
}
}
void findfac(ll n,int c){
if(n==1)return;
if(miller_rabin(S,n)){
s[cnt++]=n;
return;
}ll m=n;
while(m==n)m=pollard_rho(n,c--);
findfac(m,c),findfac(n/m,c);
}
void dfs(int pos,long long x,long long k){
if(pos>cnf)return;
if(x>ans&&x<=k)ans=x;
dfs(pos+1,x,k);
x*=f[pos];
if(x>ans&&x<=k)ans=x;
dfs(pos+1,x,k);
}
int main(){
while(~scanf("%lld%lld",&m,&n)){
if(n==m){printf("%lld %lld\n",n,n);continue;}
cnt=0; long long k=n/m;
findfac(k,C);
sort(s,s+cnt);
f[0]=s[0]; cnf=0;
for(int i=1;i<cnt;i++){
if(s[i]==s[i-1])f[cnf]*=s[i];
else f[++cnf]=s[i];
}long long tmp=(long long)sqrt(1.0*k);
ans=1; dfs(0,1,tmp);
printf("%lld %lld\n",m*ans,k/ans*m);
}return 0;
}
POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)的更多相关文章
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...
- POJ 2429 GCD & LCM Inverse(Miller-Rabbin素性测试,Pollard rho质因子分解)
x = lcm/gcd,假设答案为a,b,那么a*b = x且gcd(a,b) = 1,因为均值不等式所以当a越接近sqrt(x),a+b越小. x的范围是int64的,所以要用Pollard_rho ...
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)
原题链接:http://poj.org/problem?id=2429 GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K To ...
- POJ 2004 Mix and Build (预处理+dfs)
题意: 给N个字符串,要求出一个序列,在该序列中,后一个串,是由前一个串加一个字母后得来的(顺序可以改动). 问最多能组成多长的序列.思路:将给的字符串排序,再对所有的字符串按长度从小到大排序,若长度 ...
- POJ 2679:Adventurous Driving(SPFA+DFS)
http://poj.org/problem?id=2679 Adventurous Driving Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2429 GCD & LCM Inverse 【java】+【数学】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9928 Accepted: ...
- Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)
Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...
- POJ 1426 Find The Multiple(寻找倍数)
POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given ...
随机推荐
- SQL Server 与内存相关的术语
术语 1. virtual address space 虚拟地址空间 它是一个应用程序可以访问的最大地址空间.32位机上是4G(2^32).虚拟地址空间中的数据不一定在物理内存里.可能在缓存文件中. ...
- python编辑器对比和推荐
python编辑器对比和推荐 我先给一个初步的表格吧,大家如果有什么意见,或有补充,欢迎提出.有些我没有用过,先不写了.以下是我使用过的python IDE: 除了PythonWin, Visua ...
- Python网络资源 + Python Manual
如何学习Python + 如何有效利用Python有关的网络资源 + 如何利用Python自带手册(Python Manual) 都差点忘了说了,在看下面所有的内容之前,对于python版本不了解的, ...
- SignalR with ASP.NET MVC5 可用于倒计时同步
原文地址:http://www.codeproject.com/Articles/806919/SignalR-with-ASP-NET-MVC
- WIN7 Wireshark: There are no interfaces on which a capture can be done
有的时候我们在Windows7的环境下使用Wireshark的时候,比如点击[Interface List]的时候,出现错误. 错误内容如下: There are no interfaces on w ...
- myeclipse 2013 git
1. 2.添加site http://download.eclipse.org/egit/updates-2.3 3.安装 完成后,查看windows->preference的team下面有gi ...
- CSSREM插件
CSSREM 一个CSS的px值转rem值的Sublime Text 3自动完成插件. 插件效果如下: 安装 下载本项目,比如:git clone https://github.com/flashli ...
- SSCTF-PWN
前几天比赛的PWN题,简单写了下. PWN400 漏洞是一个数组越界访问造成的任意地址读写.在对数据排序后,对数据进行查询和更新时,可以访问到数组以外一个元素(4个字节). 程序中存在3种数据结构,第 ...
- codevs1041
codevs 1041 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路,第I个城市中高 ...
- WPF那些事儿
概述 感觉学习的东西必须做个记录,不然很快就忘掉了.现在把WPF学习过程中一些零碎的东西记录在下面,没有具体的主题,想到啥.看到啥都写在这里,算是复习一下并做个备忘吧. 1. 等待对话框 看到同事做的 ...