QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#342477#249. Miller Rabin 算法shuopihua#WA 5ms1452kbC++14820b2024-03-01 11:36:252024-03-01 11:36:26

Judging History

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

  • [2024-03-01 11:36:26]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:1452kb
  • [2024-03-01 11:36:25]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;

namespace Miller{
	int pcnt=12;
	int p[13]={2,3,5,7,11,13,17,19,61,2333,4567,24251};
	inline int calc(int x,int k,int p){
		int tmp=1;
		while(k){
			if(k&1) tmp=tmp*x%p;
			x=x*x%p;
			k>>=1;
		}
		return tmp;
	}
	inline bool check(int x,int p){
		if(x%p==0||calc(p%x,x-1,x)^1) return 1;
		int t,k=x-1;
		while((k^1)&1){
			t=calc(p%x,k>>=1,x);
			if((t^1)&&(t^(x-1))) return 1;
			if(!(t^(x-1))) return 0;
		}
		return 0;
	}
	inline bool MR(int x){
		if(x<2) return 0;
		for(int i=0;i<12;i++){
			if(!(x^p[i])) return 1;
			if(check(x,p[i])) return 0;
		}
		return 1;
	}
}

signed main(){
	int n;
	while(~scanf("%lld",&n)){
		puts(Miller::MR(n)?"Y":"N");
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 5ms
memory: 1452kb

input:

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

output:

N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
...

result:

wrong answer 2nd lines differ - expected: 'Y', found: 'N'