QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#560520 | #249. Miller Rabin 算法 | yae_miko | TL | 0ms | 0kb | C++14 | 989b | 2024-09-12 16:08:33 | 2024-09-12 16:08:34 |
answer
#include<stdio.h>
#include<stdlib.h>
typedef long long ll;
inline ll qpow(ll a,ll b,ll m){
ll res=1;
while(b){
if(b&1)res=res*a%m;
a=a*a%m,b>>=1;
}
return res;
}
inline ll qmul(ll a,ll b,ll m){
ll res=0;
while(b){
if(b&1)res=(res+a)%m;
a=(a+a)%m,b>>=1;
}
return res;
}
inline ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
inline ll check(ll a,ll n,ll r,ll s){
ll ans=qpow(a,r,n),p=ans;
for(ll i=1;i<=s;++i){
ans=qmul(ans,ans,n);
if(ans==1&&p!=1&&p!=n-1)return 1;
p=ans;
}
return ans!=1;
}
inline ll miller_rabin(ll n){
if(n<2)return 0;if(n==2)return 1;
if(!(n&1))return 0;
ll r=n-1,s=0;
while(!(r&1))r>>=1,++s;
for(ll i=0;i<1;++i){
ll a=rand()%(n-1)+1;
if(check(a,n,r,s))return 0;
}
return 1;
}
ll n;
signed main(){
while(scanf("%lld",&n)!=EOF)
puts(miller_rabin(n)?"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...