首页 > 其他 > 详细

Miller-Rabin

时间:2020-01-22 22:14:24      阅读:72      评论:0      收藏:0      [点我收藏+]

\(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;
}

Miller-Rabin

原文:https://www.cnblogs.com/lhm-/p/12229660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!