QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#775756 | #249. Miller Rabin 算法 | Physics212303 | TL | 0ms | 0kb | C++17 | 912b | 2024-11-23 16:37:01 | 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;
}
詳細信息
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 ...