poj 2429 GCD & LCM Inverse

来源:互联网 发布:mysql null的逻辑 编辑:程序博客网 时间:2024/06/05 18:00

素因子分解,然后搜就行,不过因为素数很大,要用大素数分解的板子,数字越靠近sqrt(n),结果的和就越小。
不知道哪里出了毛病,代码怎么弄都re,很蛋疼,参考了这位大佬的代码:http://www.cnblogs.com/zufezzt/p/5369784.html

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#include<iostream>#include<cmath>#include<algorithm>using namespace std;const int S=10;long long mult_mod(long long a,long long b,long long c){    a%=c;    b%=c;    long long ret=0;    while(b)    {        if(b&1)        {            ret+=a;            ret%=c;        }        a<<=1;        if(a>=c)a%=c;        b>>=1;    }    return ret;}long long pow_mod(long long x,long long n,long long mod){    if(n==1)return x%mod;    x%=mod;    long long tmp=x;    long long ret=1;    while(n)    {        if(n&1) ret=mult_mod(ret,tmp,mod);        tmp=mult_mod(tmp,tmp,mod);        n>>=1;    }    return ret;}bool check(long long a,long long n,long long x,long long t){    long long ret=pow_mod(a,x,n);    long long last=ret;    for(int i=1; i<=t; i++)    {        ret=mult_mod(ret,ret,n);        if(ret==1&&last!=1&&last!=n-1) return true;        last=ret;    }    if(ret!=1) return true;    return false;}bool Miller_Rabin(long long n){    if(n<2)return false;    if(n==2)return true;    if((n&1)==0) return false;    long long x=n-1;    long long t=0;    while((x&1)==0)    {        x>>=1;        t++;    }    for(int i=0; i<S; i++)    {        long long a=rand()%(n-1)+1;        if(check(a,n,x,t))            return false;    }    return true;}long long factor[100];int tol;long long gcd(long long a,long long b){    if(a==0)return 1;    if(a<0) return gcd(-a,b);    while(b)    {        long long t=a%b;        a=b;        b=t;    }    return a;}long long Pollard_rho(long long x,long long c){    long long i=1,k=2;    long long x0=rand()%x;    long long y=x0;    while(1)    {        i++;        x0=(mult_mod(x0,x0,x)+c)%x;        long long d=gcd(y-x0,x);        if(d!=1&&d!=x) return d;        if(y==x0) return x;        if(i==k)        {            y=x0;            k+=k;        }    }}void findfac(long long n){    if(Miller_Rabin(n))    {        factor[tol++]=n;        return;    }    long long p=n;    while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);    findfac(p);    findfac(n/p);}long long r[100];int num;long long k,bd;void dfs(long long now,int x){    if(now>bd) return;    if(x >= num)    {        if(now > k)            k = now;        return;    }    dfs(now*r[x],x+1);    dfs(now,x+1);}int main(){    long long gcd,lcm,n;    while(scanf("%lld%lld",&gcd,&lcm)!=EOF)    {        if(gcd==lcm)        {            printf("%lld %lld\n",gcd,lcm);            continue;        }        tol=0;        n=lcm/gcd;        bd = sqrt(n);        findfac(n);        sort(factor,factor+tol);        num=0;        for(int i=0; i<tol; i++)        {            if(i == 0 || factor[i] != factor[i-1])                r[num++] = factor[i];            else                r[num-1] *= factor[i];        }        k=1;        dfs(1,0);        printf("%lld %lld\n",gcd*k,gcd*(n/k));    }    return 0;}
原创粉丝点击