QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#775756#249. Miller Rabin 算法Physics212303TL 0ms0kbC++17912b2024-11-23 16:37:012024-11-23 16:37:01

Judging History

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

  • [2024-11-23 16:37:01]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-23 16:37:01]
  • 提交

answer

#pragma GCC optimzie("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int qmul(int a,int b,int p){
  int r=0;
  while(b){
    if(b&1)r=(r+a)%p;
    a=(a<<1ll)%p,b>>=1;
  }
  return r;
}
inline int qpow(int a,int b,int p){
  int r=1;
  while(b){
    if(b&1)r=qmul(r,a,p)%p;
    a=qmul(a,a,p)%p,b>>=1;
  }
  return r;
}
inline bool pd(int n,int m){
  int s=0,d=n-1,x;
  while(!(d&1))d>>=1,s++;
  x=qpow(m,d,n);
  if(x==1)return true;
  for(int i=0;i<s;i++){
    if(x==n-1)return true;
    x=qmul(x,x,n);
  }
  return false;
}
inline bool isprime(int n){
  if(n<2)return false;
  for(int i:{2,3,5,7,11,13,17,37}){
    if(n==i)return true;
    if(!(n%i)||!pd(n,i))return false;
  }
  return true;
}
main(){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  int n; while(cin>>n)
    cout<<(isprime(n)?"Y\n":"N\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...

output:

N
Y
Y
N
Y
Y
N
Y
Y
Y
Y
Y
Y
N
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
N
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
N
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
...

result: