QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#341463#249. Miller Rabin 算法WRuperD#TL 0ms0kbC++141.2kb2024-02-29 19:12:152024-02-29 19:12:15

Judging History

This is the latest submission verdict.

  • [2024-02-29 19:12:15]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 0kb
  • [2024-02-29 19:12:15]
  • Submitted

answer

// Problem: 40. Miller Rabin 算法
// URL: https://qoj.ac/contest/1536/problem/249
// Writer: WRuperD
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 7;
#define int __int128
#define pb emplace_back
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")

int quickPower(int a, int b, int p){
	int ans = 1, base = a;
	while(b){
		if(b & 1)	ans *= base, ans %= p;
		base *= base;
		base %= p;
		b >>= 1;
	}
	return ans;
}

bool check(int x){ 	
	if(x == 1)	return false;
	for(int i = 2; i <= min((long long)(x - 1), 1000ll); i++){
		// write(i), put(), write(quickPower(i, x - 1, x)), endl;
		if(quickPower(i, x - 1, x) != 1){
			// puts("1");
			return false;
		}
	}
	return true;
}

void solve(){
	unsigned long long x;
	while(cin>>x){
		if(check(x))	puts("Y");
		else puts("N");
	}	
}

signed main(){
	int t = 1;
	while(t--)	solve();
	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: