poj 2191 大数素数判定 && 大数素数分解

来源:互联网 发布:淘宝里的转化率是什么 编辑:程序博客网 时间:2024/05/21 09:15



再次用到Miller_rabin 和Pollard - rho,

题意: 给出一个梅森数,2^x - 1,;

           然后要对x为素数的时候,梅森数不为素数时的数进行素数分解;

思路:打表;


#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<algorithm>#include<map>#include<string>using namespace std;const int maxn = 64 ;#define INF 0x3f3f3f3f#define clr(x,y) memset(x,y,sizeof x )typedef long long ll;#define eps 10e-10const ll Mod = 1000000007;typedef pair<ll, ll> P;ll Mult_mod (ll a,ll b,ll c)  //减法实现比取模速度快{    //返回(a*b) mod c,a,b,c<2^63    a%=c;    b%=c;    ll ret=0;    while (b)    {        if (b&1)        {            ret+=a;            if (ret>=c) ret-=c;        }        a<<=1;        if (a>=c) a-=c;        b>>=1;    }    return ret;}//计算  x^n %cll Pow_mod (ll x,ll n,ll mod) //x^n%c{    if (n==1) return x%mod;    x%=mod;    ll tmp=x;    ll ret=1;    while (n)    {        if (n&1) ret=Mult_mod(ret,tmp,mod);        tmp=Mult_mod(tmp,tmp,mod);        n>>=1;    }    return ret;}//以a为基,n-1=x*2^t      a^(n-1)=1(mod n)  验证n是不是合数//一定是合数返回true,不一定返回falsebool Check (ll a,ll n,ll x,ll t){    ll ret=Pow_mod(a,x,n);    ll last=ret;    for (ll 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;}// Miller_Rabin()算法素数判定//是素数返回true.(可能是伪素数,但概率极小)//合数返回false;ll S = 20;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 (ll i=0;i<S;i++)    {        ll a=rand()%(n-1)+1; //rand()需要stdlib.h头文件        if (Check(a,n,x,t))            return false;//合数    }    return true;}//************************************************//pollard_rho 算法进行质因数分解//************************************************ll factor[maxn];//质因数分解结果(刚返回时是无序的)ll 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 (true)    {        i++;        x0=(Mult_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+=k;}    }}//对n进行素因子分解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);}int len = 0;ll a[maxn];ll prime[maxn];ll ans[maxn][maxn];bool flag[maxn];int cnt[maxn];void Init(){    a[0] = 1;    for(int i = 1; i < maxn; i ++)        a[i] = a[i - 1] * 2;    bool is_[maxn];    clr(is_,true);    for(int i = 2; i < maxn; i ++)    {        if(is_[i])        {            for(int j = i * 2; j < maxn ; j += i)                is_[j] = false;            prime[len ++] = i;        }    }    clr(flag,true);    for(int i = 0; i < len; i ++)    {//        cout << i << " " << prime[i] << endl;        if(!Miller_Rabin(a[prime[i]] - 1))        {            tol = 0;//            cout << a[prime[i]] - 1 << " ";            flag[i] = false;            Findfac(a[prime[i]] - 1);//            cout << tol << endl;            sort(factor,factor + tol);            cnt[i] = tol;            for(int j = 0; j < tol; j ++)            {                ans[i][j] = factor[j];            }        }    }}int main(){    Init();    int n;    while( ~ scanf("%d",&n))    {        for(int i = 0; i < len; i ++)        {            if(!flag[i] && prime[i] < n)            {                printf("%lld",ans[i][0]);                for(int j = 1; j < cnt[i]; j ++)                {                    printf(" * %lld",ans[i][j]);                }                printf(" = %lld = ( 2 ^ %d ) - 1\n",a[prime[i]],prime[i]);            }        }    }    return 0;}


阅读全文
0 0
原创粉丝点击