QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#497763#249. Miller Rabin 算法hhy06130 0ms0kbC++14958b2024-07-29 17:33:562024-07-29 17:33:56

Judging History

This is the latest submission verdict.

  • [2024-07-29 17:33:56]
  • Judged
  • Verdict: 0
  • Time: 0ms
  • Memory: 0kb
  • [2024-07-29 17:33:56]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
const __int128 o=1;
long long qpow(long long a,long long b,long long mod){
	long long ans=1;
	while(b>0){
		if(b&1) ans=o*ans*a%mod;
		b>>=1;
		a=o*a*a%mod;
	}
	return ans;
}
namespace MillerRabin{
	const int K=12,arr[K]={2,3,5,7,11,13,17,19,23,29,31,37};
	bool IsPrime(long long p){
		for(int a:arr){
			if(p%a==0) return (p==a);
			if(qpow(a,p-1,p)!=1) return false;
			long long r=p-1;
			int d=0;
			while(r%2==0){
				++d;
				r>>=1;
			}
			if(qpow(a,r,p)==1) continue;
			long long now=qpow(a,r,p);
			for(int i=0;i<=d;++i){
				if(now==p-1) break;
				if(i==d) return false;
				now=o*now*now%p;
			}
		}
		return true;
	}
}
using MillerRabin::IsPrime;
int main(){
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	int T;
	cin >> T;
	while(T--){
		long long x;
		cin >> x;
		if(IsPrime(x)) cout << "Y\n";
		else cout << "N\n";
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Output Limit Exceeded

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: