现在的位置: 首页 > 综合 > 正文

POJ 2429 数论

2018年01月20日 ⁄ 综合 ⁄ 共 2492字 ⁄ 字号 评论关闭

 GCD & LCM Inverse

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5559   Accepted: 993

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

Source

 
大数质因数分解
然后DFS搜索出每一个因子
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<time.h>

using namespace std;

typedef unsigned __int64 ll;
const ll MAX=100;
const ll inf=(ll)1<<62;
ll f0[100],ff,n,tmp,ret,ret1;

ll myrandom()
{
    ll a;
    a=rand();
    a*=rand();
    a*=rand();
    a*=rand();
    return a;
}

ll mulmod(ll a,ll b,ll c)
{
    ll ret=0;
    while(b)
	{
        if(b&1)
		{
            ret+=a;
            if(ret>c)
				ret=ret-c;
        }
		a=a<<1;
        if(a>c)
            a=a-c;
       b=b>>1;
    }
    return ret;
}

ll powmod(ll a,ll b,ll c)
{
    ll ret=1;
    while(b)
	{
        if(b&1)
            ret=mulmod(ret,a,c);
        a=mulmod(a,a,c);
        b=b>>1;
    }
    return ret;
}

int miller(ll base,ll n)
{
    ll m=n-1,k=0;
	int i;
    while(m%2==0)
	{
        m=m>>1;
        k++;
    }
    if(powmod(base,m,n)==1)
        return 1;
    for(i=0;i<k;i++)
	{
        if(powmod(base,m<<i,n)==n-1)
            return 1;
    }
    return 0;
}

int miller_rabin(ll n)
{
	int i;
    for( i=2;i<100;++i)
        if(n%i==0)
            if(n==i)
                return 1;
            else return 0;
    for(i=0;i<MAX;++i)
	{
        ll base=myrandom()%(n-1)+1;
        if(!miller(base,n))
            return 0;
    }
    return 1;
}

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

ll f(ll a,ll b)
{
    return (mulmod(a,a,b)+1)%b;
}

ll pollard_rho(ll n)
{
	int i;
    if(n<=2) 
		return 0;
    for( i=2;i<100;++i)
        if(n%i==0)
            return i;
    ll p,x,xx;
    for( i=1;i<MAX;i ++)
	{
        x=myrandom()%n;
        xx=f(x,n);
        p=gcd((xx+n-x)%n,n);
        while(p==1)
		{
            x=f(x,n);
            xx=f(f(xx,n),n);
            p=gcd((xx+n-x)%n,n)%n;
        }
        if(p) 
			return p;
    }
    return 0;
}

ll prime(ll a)
{
    if(miller_rabin(a)) 
		return 0;
    ll t=pollard_rho(a);
    ll p=prime(t);
    if(p) 
		return p;
    return t;
}

ll ans,ansa,ansb;

ll min(ll a,ll b)
{
	if(a>b)
		return b;
	else
		return a;
}

void dfs(ll n,ll now,ll id)
{
	if(now*now>n)//剪哈枝
		return;
	if(id==ff)
	{
		ll a,b;
		a=now;
		b=n/now;
		if(a+b<ans)
		{
			ans=a+b;
			ansa=a;
			ansb=b;
		}
		return;
	}
	dfs(n,now*f0[id],id+1);
	dfs(n,now,id+1);
}

void swap(ll &a,ll &b)
{
	ll temp;
	temp=a;
	a=b;
	b=temp;
}

int main()
{
	ll a,b,key,tmp;
	srand(time(NULL));
	while(scanf("%I64u%I64u",&a,&b)!=EOF)
	{
		if(a>b)
			swap(a,b);
		if(a==b||miller_rabin(b))
		{
			printf("%I64u %I64u\n",a,b);
			continue;
		}
		key=b/a;
		ff=0,n=key;
		while(key>1)
		{
			if(miller_rabin(key))
				break;
			tmp=prime(key);
			f0[ff]=tmp;
			key=key/tmp;
			while(key%tmp==0)
			{
				key=key/tmp;
				f0[ff]=f0[ff]*tmp;
			}
			ff++;
		}
		if(key>1)
		{
			f0[ff++]=key;
		}
		ans=inf;
		dfs(n,1,0);
		printf("%I64u %I64u\n",ansa*a,ansb*a);
	}
	return 0;
}

抱歉!评论已关闭.