Miller Rabin 求条
  • 板块灌水区
  • 楼主Alea
  • 当前回复10
  • 已保存回复10
  • 发布时间2023/8/8 22:29
  • 上次更新2023/11/3 05:05:15
查看原帖
Miller Rabin 求条
322792
Alea楼主2023/8/8 22:29
#include <iostream>
#define int long long
using namespace std;
int pow(int a,int b,int p){
    int ans=1;
    while(b!=0){
        if(b%2==1) ans=ans*a%p;
        a=a*a%p,b/=2;
    }
    return ans;
}
bool pr(int x){
    if(x==0||x==1) return false;
    if(x==2) return true;
    const int chk[8]={0,2,325,9375,28178,450775,9780504,1795265022};
    for(int i=1;i<=8;i++){
        if(pow(chk[i],x,x)!=chk[i]%x){
            cout<<chk[i]%x<<"!="<<pow(chk[i],x,x)<<":";
            return false;
        }
    }
    return true;
}
signed main(){
    int x;
    while(cin>>x){
        if(pr(x)) cout<<"Y"<<endl;
        else cout<<"N"<<endl;
    }
    return 0;
}

对于样例

1
2
6
9
666623333

每次样例跑一遍都不同。。。

2023/8/8 22:29
加载中...