POJ 2429 GCD & LCM Inverse 解题报告(大数因式分解)
来源:互联网 发布:网络出版与电子出版 编辑:程序博客网 时间:2024/06/15 03:21
GCD & LCM Inverse
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8910 Accepted: 1675
Description
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b. But what about the inverse? That is: given GCD and LCM, finding a and b.
Input
The input contains multiple test cases, each of which contains two positive integers, the GCD and the LCM. You can assume that these two numbers are both less than 2^63.
Output
For each test case, output a and b in ascending order. If there are multiple solutions, output the pair with smallest a + b.
Sample Input
3 60
Sample Output
12 15
解题报告:分解(b/a),将pi^ei分配到两个数里且两个数和最小。另外当lcm==gcd时,直接输出lcm,gcd。
大数分解用Pollard rho算法。代码如下:
#include<cstdio>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<map>using namespace std;typedef long long LL;const int S=40;LL mul_mod(LL a, LL b, LL mod){ a%=mod; b%=mod; LL res=0; while(b) { if(b&1) res+=a, res%=mod; a<<=1, a%=mod; b>>=1; } return res;}LL pow_mod(LL a, LL b, LL mod){ a%=mod; LL res=1; while(b) { if(b&1) res=mul_mod(res, a, mod); a=mul_mod(a, a, mod); b>>=1; } return res;}bool check(LL a, LL n, LL x, LL t){ LL res = pow_mod(a, x, n); if(res==1) return false; LL last=res; for(int i=1;i<=t;i++) { res=mul_mod(res, res, n); if(res==1 && last!=1 && last!=n-1) return true; last=res; } if(res!=1) return true; return false;}bool Miller_Rabin(LL n){ if(n<2) return false; if(n==2) return true; if((n&1)==0) return false; LL x=n-1; LL t=0; while((x&1)==0) x>>=1,t++; for(int i=0;i<S;i++) if(check(rand()%(n-1)+1, n, x, t)) return false; return true;}long long factor[100];//质因数分解结果(刚返回时是无序的)int tol;//质因数的个数。数组小标从0开始LL gcd(LL a, LL b){ if(a==0) return 1; if(a<0) return gcd(-a, b); while(b) { LL t=a%b; a=b; b=t; } return a;}LL Pollard_rho(LL x, LL c){ LL i=1, k=2; LL x0=rand()%x; LL y=x0; while(1) { i++; x0=(mul_mod(x0, x0, x)+c)%x; LL d=gcd(y-x0, x); if(d!=1 && d!=x) return d; if(y==x0) return x; if(i==k) y=x0, k<<=1; }}void findfac(LL n){ if(Miller_Rabin(n)) { factor[tol++]=n; return; } LL p=n; while(p>=n) p=Pollard_rho(p, rand()%(n-1)+1); findfac(p); findfac(n/p);}LL num[500];LL powLL(LL a, int b){ LL res=1; while(b) { if(b&1) res*=a; a*=a; b>>=1; } return res;}void work(LL a, LL b){ if(a==b) { printf("%lld %lld\n", a, b); return; } if(a>b) swap(a, b); tol=0; findfac(b/a); map<LL, LL> mm; for(int i=0;i<tol;i++) mm[factor[i]]++; sort(factor, factor+tol); tol=unique(factor, factor+tol)-factor; int tol2=0; for(int i=0;i<tol;i++) num[tol2++]=powLL(factor[i], mm[factor[i]]); sort(num, num+tol2); LL pro=1; for(int i=0;i<tol2;i++) pro*=num[i]; LL sum=1+b/a; LL mi=1; for(int i=1;i<(1<<(tol2-1));i++) { LL tmp=1; for(int j=0;j<tol2;j++) if(i&(1<<j)) tmp*=num[j]; if(tmp+pro/tmp<sum) { sum=tmp+pro/tmp; mi=min(tmp, pro/tmp); } } printf("%lld %lld\n", mi*a, pro/mi*a);}int main(){ LL a, b; while(~scanf("%lld%lld", &a, &b)) work(a, b);}
0 0
- POJ 2429 GCD & LCM Inverse 解题报告(大数因式分解)
- POJ 2429 GCD & LCM Inverse 因式分解
- POJ 2429 GCD & LCM Inverse (大数分解)
- POJ 2429GCD & LCM Inverse(大数分解)
- poj 2429 GCD & LCM Inverse(大数质因数分解+DFS)
- POJ 2429 GCD & LCM Inverse (大整数素性测试与因式分解)
- 解题报告 之 SOJ2106 GCD & LCM Inverse
- POJ 2429 GCD & LCM Inverse Pollard_rho大数因子分解
- poj 2429 GCD & LCM Inverse(Miller_rabin 测试+pollard_rho大数分解)
- POJ 2429 GCD & LCM Inverse Pollard_Rho大数分解+Miller_Rabin
- poj 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- POJ 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- POJ-2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse
- poj 2429 GCD & LCM Inverse(拉宾米勒测试+大数分解+dfs)
- OpenCV编程->HOG特征(1)
- 智能温度传感器DS18B20的原理与应用
- linux sed详解
- 人生的价值
- How To Move Or Rebuild A Lob Partition
- POJ 2429 GCD & LCM Inverse 解题报告(大数因式分解)
- 如何从unix/linux文件中删除ctrl-m(^M)字符
- v4l2抓取一帧图像的程序
- SDM Templates
- 第7周作业4——JMF(Java多媒体框架)的应用之视频播放器
- java提高篇(十)-----强制类型转换
- 图解IIS配置过程
- 设计模式(13)-行为型模式-Chain of Responsibility
- 用jsoup获取网页内容,比如图片