nefu 120 梅森素数 【Lucas-Lehmer】【miller_rabin】


点击打开链接


Lucas-Lehmer     judge it.



#include <iostream>
#include <stdio.h>
#include <string.h>
#define LL long long
using namespace std;
LL p,sum,data[70],t;
LL multi(LL a,LL b,LL m){
    LL ret=0;
    while(b>0){
        if(b&1) ret=(ret+a)%m;
        b=b>>1;
        a=(a<<1)%m;
    }
    return ret;
}
int main()
{
    int T;
    data[1]=4;
    scanf("%d", &T);
    while(T--){
         sum=1;
         scanf("%lld" ,&p);
         sum=(sum<<p)-1;
         for(int i=2;i<=p-1;++i){
            t=multi(data[i-1] , data[i-1] , sum);
            data[i]=(t-2)%sum;
         }
         if(p==2) printf("yes\n");
         else {
            if(data[p-1]==0) printf("yes\n");
            else printf("no\n");
         }
    }
    return 0;
}


miller_rabin


#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <time.h>
#define LL long long
using namespace std;
const int S=80;
int t,p;
LL mod_mul(LL a,LL b,LL m){
    LL ret=0;
    while(b>0){
        if(b&1) ret=(ret+a)%m;
        b>>=1;
        a=(a<<1)%m;
    }
    return ret;
}
LL mod_exp(LL a,LL b,LL m){
    LL ans=1;
    a%=m;
    while(b){
        if(b&1){
            ans=mod_mul(ans,a,m);
            b--;
        }
        b/=2;
        a=mod_mul(a,a,m);
    }
    return ans;
}
bool miller_rabin(LL n){
    if(n==2||n==3||n==5||n==7||n==11) return true;
    if(n==1||!(n%2)||!(n%3)||!(n%5)||!(n%7)||!(n%11)) return false;
    LL x,pre,u;
    int i,j,k=0;
    u=n-1;
    while(!(u&1)){
        k++; u>>=1;
    }
    srand((LL)time(0));
    for(i=0;i<S;++i){
        x=rand()%(n-2)+2;
        if((x%n)==0) continue;
        x=mod_exp(x,u,n);
        pre=x;
        for(j=0;j<k;++j){
            x=mod_mul(x,x,n);
            if(x==1&&pre!=1&&pre!=n-1) return false;
            pre=x;
        }
        if(x!=1) return false;
    }
    return true;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&p);
        LL n=((LL)1<<p)-1;
        if(miller_rabin(n)) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值