QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#605221#249. Miller Rabin 算法songziyanWA 505ms3616kbC++14855b2024-10-02 16:13:372024-10-02 16:13:38

Judging History

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

  • [2024-10-02 16:13:38]
  • 评测
  • 测评结果:WA
  • 用时:505ms
  • 内存:3616kb
  • [2024-10-02 16:13:37]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int prm[]={2,3,5,7,11,13,37};
int mul(int x,int y,int mod){
	if(x<y)swap(x,y);
	int res=0;
	while(y){
		if(y&1)res=(res+x)%mod;
		x=(x+x)%mod;y>>=1;
	}
	return res;
}
int qpow(int x,int y,int mod){
	int res=1;
	while(y){
		if(y&1)res=mul(res,x,mod);
		x=mul(x,x,mod);y>>=1;
	}
	return res;
}
bool check(int n,int p){
	int d=n-1,r=0;
	while((d&1)^1)d>>=1,r++;
	int a=qpow(p,d,n);
	if(a==1)return 1;
	for(int i=0;i<r;i++){
		if(a==n-1)return 1;
		a=mul(a,a,n);
	}
	return 0;
}
bool miller_rabin(int x){
	if(x<=1)return 0;
	for(int i=0;i<7;i++){
		if(x==prm[i])return 1;
		if(x%prm[i]==0)return 0;
		if(check(x,prm[i]))return 1;
	}
	return 1;
}
signed main(){
	int x;
	while(~scanf("%lld",&x))
		puts(miller_rabin(x)?"Y":"N");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 505ms
memory: 3616kb

input:

996581938833575363
971646509461160317
773155992127361153
161603952726515268
540879144500456953
476831214764178553
784255927154201144
671096087619405061
805545190025269709
339546334309245137
337726347922962343
222956293307015293
809183111090275843
799050063298926344
691718101820598109
646220213113313...

output:

Y
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:

wrong answer 1st lines differ - expected: 'N', found: 'Y'