POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)
题目大意
给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b
题解
GCD(a,b)=G
GCD(a/G,b/G)=1
LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G
这样的话我们只要对L/G进行质因数分解,找出最接近√(L/G)的因子p,最终结果就是a=p*G,b=L/p,对(L/G)就是套用Miller–Rabin和Pollard's rho了,刚开始Pollard's rho用的函数也是
f(x)=x^2+1,然后死循环了。。。。改成f(x)=x^2+c(c<=181)就OK了
代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#define MAXN 100000
using namespace std;
typedef unsigned long long LL;
LL fac[MAXN],cnt,G,L,m,p;
LL min(LL a,LL b)
{
return a<b?a:b;
}
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
LL mult_mod(LL a,LL b,LL mod)
{
LL ans=0;
while(b)
{
if(b&1)
ans=(ans+a)%mod;
a=(a<<1)%mod;
b>>=1;
}
return ans;
}
LL pow_mod(LL a,LL b,LL mod)
{
LL d=1;
a%=mod;
while(b)
{
if(b&1)
d=mult_mod(d,a,mod);
a=mult_mod(a,a,mod);
b>>=1;
}
return d%mod;
}
bool witness(LL a,LL n)
{
LL u=n-1,t=0;
while((u&1)==0)
{
u>>=1;
t++;
}
LL x,x0=pow_mod(a,u,n);
for(LL i=1; i<=t; i++)
{
x=mult_mod(x0,x0,n);
if(x==1&&x0!=1&&x0!=(n-1))
return true;
x0=x;
}
if(x!=1)
return true;
return false;
}
bool miller_rabin(LL n)
{
if(n==2) return true;
if(n<2||!(n&1)) return false;
for(int j=1; j<=8; j++)
{
LL a=rand()%(n-1)+1;
if(witness(a,n))
return false;
}
return true;
}
LL pollard_rho(LL n,LL c)
{
LL i=1,k=2,d,x=2,y=2;
while(true)
{
i++;
x=(mult_mod(x,x,n)+c)%n;
d=gcd(y-x,n);
if(d!=1&&d!=n)
return d;
if(x==y) return n;
if(i==k)
{
y=x;
k<<=1;
}
}
}
void find_fac(LL n,LL c)
{
if(miller_rabin(n)||n<=1)
{
fac[cnt++]=n;
return;
}
LL p=pollard_rho(n,c);
while(p>=n) p=pollard_rho(p,c--);
find_fac(p,c);
find_fac(n/p,c);
}
void dfs( LL step,LL num)
{
if(step==cnt||num>p)
{
if(num<=p&&num>m)
m=num;
return;
}
dfs(step+1,num*fac[step]);
dfs(step+1,num);
}
int main()
{
srand(time(NULL));
while(scanf("%I64u%I64u",&G,&L)!=EOF)
{
cnt=0;
find_fac(L/G,181);
sort(fac,fac+cnt);
LL i=0,t=0;
while(i<cnt)
{
LL ans=1,j=i;
while(j<cnt&&fac[i]==fac[j])
{
ans*=fac[i];
j++;
}
fac[t++]=ans;
i=j;
}
cnt=t,m=1,p=sqrt(0.0+(L/G));
dfs(0,1);
printf("%I64u %I64u\n",m*G,L/m);
}
return 0;
}
POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)的更多相关文章
- POJ2429 GCD & LCM Inverse pollard_rho大整数分解
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...
- POJ1811- Prime Test(Miller–Rabin+Pollard's rho)
题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...
- 数学基础IV 欧拉函数 Miller Rabin Pollard's rho 欧拉定理 行列式
找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(h ...
- 【Pollard-rho算法】【DFS】poj2429 GCD & LCM Inverse
题意:给你一两个数m和n,它们分别是某对数A,B的gcd和lcm,让你求出一对使得A+B最小的A,B. n/m的所有质因子中,一定有一部分是只在A中的,另一部分是只在B中的. 于是对n/m质因子分解后 ...
- poj2429 GCD & LCM Inverse
用miller_rabin 和 pollard_rho对大数因式分解,再用dfs寻找答案即可. http://poj.org/problem?id=2429 #include <cstdio&g ...
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- 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 ...
- Mathematics:GCD & LCM Inverse(POJ 2429)
根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...
- POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)
[题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...
随机推荐
- 1.0 基础、标示符、常量、数据类型(enum 枚举,struct 结构体)、操作符、循环、数组
一.程序 现实生活中,程序是指完成某些事务的一种既定方法和过程,可以把程序看成是一系列动作执行过程的描述. 在计算机世界,程序是指令,即为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集 ...
- 用shell查找某个目录下最大文件
网上资料学习: 1.查找当前目录下最大文件(包括子目录里文件): find . -type f -exec stat -c "%s %n" {} \; | sort -nr | h ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...
- Codeforces Round #360 div2
Problem_A(CodeForces 688A): 题意: 有d天, n个人.如果这n个人同时出现, 那么你就赢不了他们所有的人, 除此之外, 你可以赢他们所有到场的人. 到场人数为0也算赢. 现 ...
- Javascript事件绑定的几种方式
Javascript的事件绑定主要有四种方法(一下在IE中运行正常,但不保证其他浏览器): [注:onXXX为某一事件,fun为某一function,domId为某一DOM对象id,event类型见后 ...
- Codeforces Round #237 (Div. 2)
链接 A. Valera and X time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inp ...
- 性能测试_响应时间、并发、RPS的关系
写这篇文章是为了帮自己理清一下性能测试中最最基本,却总是被人忽略的一些概念. 并发: 什么叫并发?并发不是我们理解的在loadrunner场景中设置并发数,而是正在系统中执行操作或者在系统的队列中排队 ...
- Android 使控件各占屏幕的一半
在xml中将两个要占屏幕一半的控件都加上android:layout_weight="1": 注意:weight只能用在LinearLayout布局中. 在LinearLayout ...
- Subline Text默认设置文件Preferences.sublime-settings—Default详解
Subline Text中,点击Preferences,选择Settings - Default 全部属性解析 // While you can edit this file, it's best t ...
- poj 1167 简单搜索
这题主要是注意好限定的条件 条件1:每个公交车都至少要到达两次 条件2:公交车相同时间和相同间隔是属于两种车辆 条件3:不同的车可能到达时间相同 上述都是深搜的重要条件: #include<al ...