Miller-Rabin

\(code:\)

ll pri[12]={2,3,5,7,11,13,17,19};
ll qp(ll x,ll y,ll mod)
{
    ll ans=1;
    while(y)
    {
        if(y&1) ans=(ans*x)%mod;
        x=(x*x)%mod;
        y>>=1;
    }
    return ans%mod;
}
bool check(ll x,ll p,ll mod)
{
    ll t=qp(x,p,mod);
    if(t==mod-1) return true;
    if(t==1) return p&1?true:check(x,p/2,mod);
    return false;
}
bool Miller_Rabin(ll n)
{
    if(n==1) return false;
    if(n<=3) return true;
    if(!(n&1)) false;
    for(int i=0;i<8;++i)
    {
        if(n==pri[i]) return true;
        if(!check(pri[i],n-1,n)) return false;
    }
    return true;
}

猜你喜欢

转载自www.cnblogs.com/lhm-/p/12229660.html