POJ 2429

使用Pollard_rho算法分解lcm/gcd的质因数,原因不说也明白了。然后排序,把相同的质因子合并,因为如果相同的质因子分落在两个因数,会使ab的GCD值改变。

然后,枚举各种组合,呃。。。。这个实在想不到好方法,只好枚举了,真想不明白,那么两位数时间的是怎么样做到的。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#define LL __int64
#define C 201
using namespace std;
const int Max=100000;
int atop;
LL ans[100];
bool se[Max];

bool cmp(LL a,LL b){
	if(a<b) return true;
	return false;
}

LL gcd(LL a,LL b){
	if(b==0) return a;
	return gcd(b,a%b);
}

LL random(LL n){
	return (LL)((double)rand()/RAND_MAX*n+0.5);
}

LL multi(LL a,LL b,LL m){
	LL ret=0;
	while(b>0){
		if(b&1) ret=(ret+a)%m;
		b>>=1;
		a=(a<<1)%m;
	}
	return ret;
}

LL quick(LL a,LL b,LL m){
	LL ans=1;
	a%=m;
	while(b){
		if(b&1){
			ans=multi(ans,a,m);
		}
		b>>=1;
		a=multi(a,a,m);
	}
	return ans;
}

LL Pollard_rho(LL n,int c){
	LL x,y,d,i=1,k=2;
	x=random(n-1)+1;
	y=x;
	while(true){
		i++;
		x=(multi(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=k<<1;
		}
	}
}

bool Witness(LL a,LL n){
	LL m=n-1;
	int j=0;
	while(!(m&1)){
		j++;
		m=m>>1;
	}
	LL x=quick(a,m,n);
	if(x==1||x==n-1)
	return false;
	while(j--){
		x=multi(x,x,n);
		if(x==n-1) return false;
	}
	return true;
}

bool Miller_Rabin(LL n){
	if(n<2) return false;
	if(n==2) return true;
	if(!(n&1)) return false;
	for(int i=1;i<=12;i++){
		LL a=random(n-2)+1;
		if(Witness(a,n)) return false;
	}
	return true;
}

void find(LL n, int k){
	if(n==1) return ;
	if(Miller_Rabin(n)){
		ans[atop++]=n;
		return ;
	}
	LL p=n;
	while(p>=n)
	p=Pollard_rho(p,k--);
	find(p,k);
	find(n/p,k);
}

LL getrt(int g){
	LL ret=1;
	for(int i=0;i<atop;i++){
		if(g&(1<<i)){
			ret*=ans[i];
		}
	}
	return ret;
}

int main(){
	LL gcd,lcm;
	srand(time(0));
	while(scanf("%I64d%I64d",&gcd,&lcm)!=EOF){
		lcm/=gcd; atop=0;
		memset(se,false,sizeof(se));
		find(lcm,C);
		sort(ans,ans+atop,cmp);
		int m=atop;
		atop=1;
		ans[0]=ans[0];
		for(int i=1;i<m;i++){
			if(ans[i]==ans[i-1]){
				ans[atop-1]*=ans[i];
			}
			else if(ans[i]!=ans[i-1]){
				ans[atop++]=ans[i];
			}
		}
	//	cout<<atop<<endl;
		int all=(1<<atop)-1;
		LL at=-1;
		LL sum=(1LL<<63)-1;
	//	cout<<sum<<endl;
		LL ret;
		for(int i=0;i<=all;i++){
			if(se[i]) continue;
			se[i]=se[all^i]=true;
			ret=getrt(i);
		//	cout<<ret<<endl;
			if(sum>ret+lcm/ret){
			//	cout<<ret<<endl;
				at=ret;
				sum=ret+lcm/ret;
			}
		}
		LL a=gcd*at,b=gcd*(lcm/at);
		printf("%I64d %I64d\n",a<b?a:b,a>b?a:b);
	}
	return 0;
}

  

时间: 2024-08-27 12:03:54

POJ 2429的相关文章

poj 1811, poj 2429 (pollard_rho算法)

poj 1811 题意: 给出一个整数n,判断n是不是素数,如果不是素数,输出最小的质因子. 限制: 2 <= N < 2^54 思路: miller_rabin算法判素数 pollard_rho算法求质因子 复杂度O(log(n)) poj 2429 题意: 给出两个数的lcm和gcd,求这两个数. 限制: 0 < lcm,gcd < 2^63 思路: pollard_rho O(log(n))分解质因数. 可以考虑到2^63不同的质因数只有20左右个,而相同的质数不可能分在不同

POJ 2429 GCD &amp; LCM Inverse (大数分解)

GCD & LCM Inverse 题目:http://poj.org/problem?id=2429 题意: 给你两个数的gcd和lcm,[1, 2^63).求a,b.使得a+b最小. 思路: lcm = a * b / gcd 将lcm/gcd之后进行大数分解,形成a^x1 * b^x2 * c^x3-- 的形式,其中a,b,c为互不相同的质数.然后暴力枚举即可. 代码: #include<map> #include<set> #include<queue>

poj 2429 Pollard_rho大数分解

先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里可以直接搜索,注意一个问题,由于相同因子不能分配给两边(会改变gcd)所以可以将相同因子合并,这样的话,搜索的层数也变的很少了. #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<time.h&g

poj 2429 GCD &amp; LCM Inverse miller_rabin素数判定和pollard_rho因数分解

题意: 给gcd(a,b)和lcm(a,b),求a+b最小的a和b. 分析: miller_rabin素数判定要用费马小定理和二次探测定理.pollard_rho因数分解算法导论上讲的又全又好,网上的资料大多讲不清楚. 代码: //poj 2429 //sep9 #include <iostream> #include <map> #include <vector> #define gcc 10007 #define max_prime 200000 using nam

POJ 2429 GCD &amp; 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越小 所以我们通过搜索求出最逼近sqr

POJ 2429 GCD &amp;amp; LCM Inverse (大数分解)

GCD & LCM Inverse 题目:http://poj.org/problem? id=2429 题意: 给你两个数的gcd和lcm,[1, 2^63). 求a,b.使得a+b最小. 思路: lcm = a * b / gcd 将lcm/gcd之后进行大数分解.形成a^x1 * b^x2 * c^x3-- 的形式.当中a,b,c为互不同样的质数.然后暴力枚举就可以. 代码: #include<map> #include<set> #include<queue&

POJ 2429 long long 质因数分解

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16206   Accepted: 3008 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

POJ 2429 GCD &amp; LCM Inverse

设答案为ans1,ans2 ans1=a1*gcd,ans2=a2*gcd,a1,a2互质 gcd*a1*b1=lcm,gcd*a2*b2=lcm a1*b1=lcm=(ans1*ans2)/gcd=a1*a2 综上所诉,a1=b2,a2=b1. 也就是说,ans1=gcd*k1,ans2=gcd*k2 要求k1,k2尽量接近,并且k1,k2互质,并且,k2*k2=lcm/gcd 需要用到Pollard_rho分解质因数,然后暴力搜索寻找k1,k2.用了kuangbin大神的Pollard_rh

poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9928   Accepted: 1843 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 a