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) ...
随机推荐
- httpcontext in asp.net unit test
[TestMethod] [HostType("ASP.NET")] [UrlToTest("http://localhost:25153/qq/a.aspx" ...
- 视频边下边播--缓存播放数据流-b
google搜索“iOS视频变下边播”,有好几篇博客写到了实现方法,其实只有一篇,其他都是copy的,不过他们都是使用的本地代理服务器的方式. 原理很简单,但是缺点也很明显,需要自己写一个本地代理服务 ...
- centos 下 yum 安装 nginx 平滑切换安装到 Tengine
---恢复内容开始--- 据说淘宝的Tengine很牛X,所以我们今天也来玩玩,我们这里是某开放云的vps,现在已经安装好了nginx,现在我们要平滑切换到安装Tengine. 下载Tengine,解 ...
- js library
jquery.js prototype.js requirejs.js backbone.js modernizr.js knockout.js http://share.renren.com/sha ...
- js高手
http://kb.cnblogs.com/page/173798/ http://kb.cnblogs.com/page/121539/ http://blog.jobbole.com/9648/ ...
- HDU 1098 Ignatius's puzzle
http://acm.hdu.edu.cn/showproblem.php?pid=1098 题意 :输入一个K,让你找一个a,使得f(x)=5*x^13+13*x^5+k*a*x这个f(x)%65等 ...
- android 服务service开启和关闭
startService()方法开启一个服务. 服务只会开启一次,如果服务已经创建,并且没有销毁,多次调用startService方法只会执行onStartCommand方法和onStart方法. 服 ...
- MongoDB实战指南(三):MongoDB的锁机制
与关系数据库一样,MongoDB也是通过锁机制来保证数据的完整性和一致性,MongoDB利用读写锁来支持并发操作,读锁可以共享写锁具有排他性.当一个读锁存在时,其他读操作也可以用这个读锁:但当一个写锁 ...
- ASP.NET在主题中添加CSS文件
ASP.NET在主题中添加CSS文件 在ASP.NET中,可以使用CSS来控制页面上HTML元素和ASP.NET控件的皮肤.如果在主题文件夹中添加了CSS文件,则在页面应用主题时也会自动应用CSS. ...
- easyui源码翻译1.32--Messager(消息窗口)
前言 使用$.messager.defaults重写默认值对象.下载该插件翻译源码 消息窗口提供了不同的消息框风格,包含alert(警告框), confirm(确认框), prompt(提示框), p ...