QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#524607#249. Miller Rabin 算法yae_mikoTL 0ms0kbC++141.8kb2024-08-19 20:52:542024-08-19 20:52:54

Judging History

你现在查看的是最新测评结果

  • [2024-08-19 20:52:54]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-08-19 20:52:54]
  • 提交

answer

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define dbg(x) cout<<#x<<": "<<x<<"\n"
// static char buf[100],*p1=buf,*p2=buf,obuf[100],*p3=obuf;
// #define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,100,stdin),p1==p2)?EOF:*p1++
// #define putchar(x) (p3-obuf<100)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x)
inline ll read(){ll x=0,f=1;char c=getchar();while(c<48||c>57){if(c==45)f=0;c=getchar();}while(c>47&&c<58)x=(x<<3)+(x<<1)+(c^48),c=getchar();return f?x:-x;}
inline void write(ll x){if(!x){putchar(48);putchar('\n');return;}short top=0,s[40];if(x<0)x=-x,putchar(45);while(x)s[top++]=x%10^48,x/=10;while(top--)putchar(s[top]);putchar('\n');}
mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());
const ll mod=1e9+7,maxn=1e5+5,maxt=505;
ll x;
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<10;++i){
        ll a=rand()%(n-1)+1;
        if(check(a,n,r,s))return 0;
    }
    return 1;
}
inline void solve(){
    while(scanf("%lld",&x)!=EOF){
        if(miller_rabin(x))puts("Y");
        else puts("N");
    }
}
signed main(){
    ll t=1;
    while(t--){
        solve();
    }
    // fwrite(obuf,p3-obuf,1,stdout);
    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:


result: