Miller_Rabbin&&Pollard_Rho

Foreword

Miller Rabin algorithm is an efficient method for determining a prime number. Although a number of uncertain quality determination method, but in the case of selecting the plurality of base number, accuracy is acceptable.

Pollard Rho is a very metaphysical way for \ (O (n ^ \ frac {1} {4}) \) number calculation within the desired engagement time complexity \ (n-\) is a non-trivial factor. Introduction to Algorithms fact is given \ (O (\ sqrt {P}) \) , \ (P \) is \ (n-\) of a certain minimum factor satisfies \ (P \) and \ (n / p \) are relatively prime. But these expectations are not always realistic. But in fact Pollard Rho algorithm running in a real environment is quite good.

Miller_Rabbin algorithm

Topic: LOJ # 143 prime number judgment.

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int p[10]={2,3,5,7,11,13,17,19,61,24251};
LL quick(LL a,LL b,LL mod)
{
    LL res=1;
    while(b)
    {
        if(b&1)res=((__int128)res*a)%mod;
        a=((__int128)a*a)%mod;
        b>>=1;
    }
    return res;
}
bool mr(LL x,LL b)
{
    LL k=x-1;
    while(k)
    {
        LL cur=quick(b,k,x);
        if(cur!=1&&cur!=x-1)return false;
        if((k&1)||cur==x-1)return true;
        k>>=1;
    }
    return true;
}
bool prime(LL x)
{
    if(x==1)return false;
    for(int i=0;i<10;i++)
    {
        if(x==p[i])return true;
        if(!mr(x,p[i]))return false;
    }
    return true;
}
int main()
{
    LL x;
    while(~scanf("%lld",&x))
    {
        if(prime(x))printf("Y\n");
        else printf("N\n");
    }
    return 0;
}

Pollard_Rho algorithm

Topic: P4718 [template] Pollard-Rho algorithm

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 LLL;
LL maxfac;
int p[10]={2,3,5,7,11,13,17,19,61,24251};
LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}
LL quick(LL a,LL b,LL mod)
{
    LL res=1;
    while(b)
    {
        if(b&1)res=((LLL)res*a)%mod;
        a=((LLL)a*a)%mod;
        b>>=1;
    }
    return res;
}
bool mr(LL x,LL b)
{
    LL k=x-1;
    while(k)
    {
        LL cur=quick(b,k,x);
        if(cur!=1&&cur!=x-1)return false;
        if((k&1)||cur==x-1)return true;
        k>>=1;
    }
    return true;
}
bool prime(LL x)
{
    if(x==1)return false;
    for(int i=0;i<10;i++)
    {
        if(x==p[i])return true;
        if(!mr(x,p[i]))return false;
    }
    return true;
}
LL PR(LL x)
{
    LL s=0,t=0,c=1LL*rand()%(x-1)+1;
    for(int goal=1;;goal<<=1)
    {
        s=t;
        LL val=1;
        for(int stp=1;stp<=goal;stp++)
        {
            t=((LLL)t*t+c)%x;
            val=(LLL)val*abs(t-s)%x;
            if(stp%127==0)
            {
                LL d=gcd(val,x);
                if(d>1)return d;
            }
        }
        LL d=gcd(val,x);
        if(d>1)return d;
    }
}
void fac(LL x)
{
    if(x<=maxfac||x<2)return;
    if(prime(x))
    {
        maxfac=x;
        return;
    }
    LL p=x;
    while(p>=x)p=PR(x);
    while(x%p==0)x/=p;
    fac(x);fac(p);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        srand((unsigned)time(NULL));
        LL x;
        scanf("%lld",&x);
        maxfac=0;
        fac(x);
        if(maxfac==x)printf("Prime\n");
        else printf("%lld\n",maxfac);
    }
}

Guess you like

Origin www.cnblogs.com/HooYing/p/12466609.html