QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#605226 | #249. Miller Rabin 算法 | songziyan | TL | 0ms | 0kb | C++14 | 859b | 2024-10-02 16:16:07 | 2024-10-02 16:16:09 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int prm[]={2,3,5,7,11,13,17,37};
int mul(int x,int y,int mod){
if(x<y)swap(x,y);
int res=0;
while(y){
if(y&1)res=(res+x)%mod;
x=(x+x)%mod;y>>=1;
}
return res;
}
int qpow(int x,int y,int mod){
int res=1;
while(y){
if(y&1)res=mul(res,x,mod);
x=mul(x,x,mod);y>>=1;
}
return res;
}
bool check(int n,int p){
int d=n-1,r=0;
while((d&1)^1)d>>=1,r++;
int a=qpow(p,d,n);
if(a==1)return 1;
for(int i=0;i<r;i++){
if(a==n-1)return 1;
a=mul(a,a,n);
}
return 0;
}
bool miller_rabin(int x){
if(x<=1)return 0;
for(int i=0;i<8;i++){
if(x==prm[i])return 1;
if(x%prm[i]==0)return 0;
if(!check(x,prm[i]))return 0;
}
return 1;
}
signed main(){
int x;
while(~scanf("%lld",&x))
puts(miller_rabin(x)?"Y":"N");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
996581938833575363 971646509461160317 773155992127361153 161603952726515268 540879144500456953 476831214764178553 784255927154201144 671096087619405061 805545190025269709 339546334309245137 337726347922962343 222956293307015293 809183111090275843 799050063298926344 691718101820598109 646220213113313...
output:
N Y Y N Y Y N Y Y Y Y Y Y N Y N Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y N Y Y Y Y Y Y Y Y Y Y Y Y Y N Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y N Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y N Y Y Y Y Y Y Y Y N Y Y N Y Y Y Y Y Y Y N Y Y Y Y Y Y N Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y N Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y ...