QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#560524#249. Miller Rabin 算法yae_mikoCompile Error//C++14978b2024-09-12 16:10:572024-09-12 16:10:57

Judging History

This is the latest submission verdict.

  • [2024-09-12 16:10:57]
  • Judged
  • [2024-09-12 16:10:57]
  • Submitted

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(cin>>n&&!n)
    puts(miller_rabin(n)?"Y":"N");
    return 0;
}

Details

answer.code: In function ‘int main()’:
answer.code:43:11: error: ‘cin’ was not declared in this scope
   43 |     while(cin>>n&&!n)
      |           ^~~