QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#590032 | #249. Miller Rabin 算法 | lanhuo | WA | 7ms | 3516kb | C++17 | 1.3kb | 2024-09-25 21:08:38 | 2024-09-25 21:08:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")
typedef long long ll;
#define pll pair<ll,ll>
ll N;
ll base[7]={2,325,9375,28178,450775,9780504,1795265022};
ll power(__int128 a,ll b,ll m){
ll ans=1;
a%=m;
while(b){
if(b&1)ans=ans*a%m;
a=a*a%m;
b>>=1;
}
return ans;
}
bool Miller_Rabin(ll x){ //判断素数
if(x==2)return true; //2是素数
if(x<2||!(x&1))return false; //如果x是偶数或者是0,1,那它不是素数
ll u=x-1,k=0;
while(!(u&1)){
u++;
k>>=1;
}
for(auto it:base){
if(it%x==0)continue;
ll v=power(it,u,x);
if(v==1||v==x-1)continue;
for(int j=1;j<=k;j++){
ll last=v;
v=(__int128)v*v%x;
if(v==1){
if(last!=x-1)return false;
break;
}
}
if(v!=1)return false; //用费马小定理判断
}
return true; //如果进行多次测试都是对的,那么x就很有可能是素数
}
void solve(){
ll N;
while(cin>>N){
if(Miller_Rabin(N))cout<<"Y\n";
else cout<<"N\n";
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 3516kb
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'