QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#750013 | #249. Miller Rabin 算法 | pudding164253 | WA | 15ms | 3760kb | C++14 | 982b | 2024-11-15 12:05:49 | 2024-11-15 12:05:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define uLL unsigned long long
template<class T,class POW>
void fastpow(T x,POW n,POW p,T &ans){
for(;n;n>>=1){
if(n&1){ans*=x;ans%=p;}
x*=x;x%=p;
}
}
/*輸入x,n,p,ans 會將ans修改為x^n%p
對整數/矩陣/不要求精度的浮點 皆有效
模板第一個型別是x,ans 第二個是n,p(應該放LL或__int128)*/
uLL pri[7]={2,325,9375,28178,450775,9780504,1795265022};/*2^64*/
// int p[3]={2,7,61};/*2^32*/
bool check(const uLL x,const uLL p){
uLL d=x-1,ans=1;fastpow(p,d,x,ans);
if(ans!=1)return 1;
for(;!(d&1);){
d>>=1;ans=1;fastpow(p,d,x,ans);
if(ans==x-1)return 0;
else if(ans!=1)return 1;
}
return 0;
}
bool miller_rabin(const uLL x){
if(x==1)return 0;
for(auto e:pri){
if(e==x)return 1;
if(check(x,e))return 0;
}
return 1;
}
int main(){for(uLL n;cin>>n;)printf("%c\n","NY"[miller_rabin(n)]);}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 15ms
memory: 3760kb
input:
996581938833575363 971646509461160317 773155992127361153 161603952726515268 540879144500456953 476831214764178553 784255927154201144 671096087619405061 805545190025269709 339546334309245137 337726347922962343 222956293307015293 809183111090275843 799050063298926344 691718101820598109 646220213113313...
output:
N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N ...
result:
wrong answer 2nd lines differ - expected: 'Y', found: 'N'