栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 2429 GCD & LCM Inverse

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 2429 GCD & LCM Inverse

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <ctime>#include <algorithm>using namespace std;typedef long long int64;int64 gcd(int64 a,int64 b){    return b==0?a:gcd(b,a%b);}int64 mult_mod(int64 a,int64 b,int64 c){    int64 ret=0;    int64 tmp=a;    a%=c,b%=c;    while(b)    {        if(b&1)        { ret+=tmp; if(ret>c)   ret-=c;        }        tmp<<=1;        if(tmp>c)tmp-=c;        b>>=1;    }    return ret;}int64 pow_mod(int64 a,int64 n,int64 mod){    int64 ret = 1;    int64 tmp = a%mod;    while(n)    {        if(n & 1)ret = mult_mod(ret,tmp,mod);        tmp = mult_mod(tmp,tmp,mod);        n >>= 1;    }    return ret;}bool check(int64 a,int64 n,int64 x,int64 t){    int64 ret = pow_mod(a,x,n);    int64 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;    else return false;}bool Miller_Rabin(long long n,int k){    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++;}    srand(time(NULL));    for(int i=0;i<k;i++)    {        long long a = rand()%(n-1) + 1;        if( check(a,n,x,t) )        return false;    }    return true;}int64 factor[10000];int tot;int64 pollard_rho(int64 x,int64 c){    int64 i=1, k=2;    srand(time(NULL));    int64 x0=rand()%(x-1)+1;    int64 y=x0;    while(1)    {        i++;        x0=(mult_mod(x0,x0,x)+c)%x;        int64 d=gcd(y-x0+x,x);        if(d!=1&&d!=x)return d;        if(y==x0)return x;        if(i==k){y=x0;k+=k;}    }}void findfac(int64 n,int k){    if(n==1)return;    if(Miller_Rabin(n,10))    {        factor[tot++]=n;        return;    }    int64 p=n;    int c=k;    while(p>=n)    p=pollard_rho(p,c--);    findfac(p,k);    findfac(n/p,k);}int64 mins,aa,bb;int top;void dfs(int64 a,int64 b,int p){    if(a+b>=mins) return;    if(p==top)    {        if(a+b<mins)        { mins=a+b; aa=a; bb=b;        }        return;    }    dfs(a*factor[p],b,p+1);    dfs(a,b*factor[p],p+1);}int main(){    int64 g,l,c;    while(~scanf("%I64d%I64d",&g,&l))    {        if(g==l){printf("%I64d %I64dn",g,l);continue;};        mins=~0ull>>1;tot=0;        c=l/g;        findfac(c,107);        top=0;        sort(factor,factor+tot);        for(int i=0;i<tot;i++)        { if(i==0) factor[top++]=factor[i]; else if(factor[i]==factor[i-1]) factor[top-1]*=factor[i]; else factor[top++]=factor[i];        }        dfs(g,g,0);        if(aa>bb) swap(aa,bb);        printf("%I64d %I64dn",aa,bb);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号