QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#342396#249. Miller Rabin 算法KevinLikesCoding#TL 0ms0kbC++141.2kb2024-03-01 11:06:032024-03-01 11:06:04

Judging History

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

  • [2024-03-01 11:06:04]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-03-01 11:06:03]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int128 __int128
#define FOR(i,n,m) for(int i=(n);i<=(m);i++)
#define ROF(i,n,m) for(int i=(n);i>=(m);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define SZ(v) v.size()
#define pb push_back
template < typename A , typename B>
inline bool chmax(A &x, B y) { return (x < y ? (x = y, true) : false); }
template < typename A , typename B>
inline bool chmin(A &x, B y) { return (x > y ? (x = y, true) : false); }
mt19937 rnd(time(0));
ll fp(ll x, ll y, ll p) {
	ll res = 1;
	for(; y; y >>= 1) {
		if(y & 1) res = (int128) res * x % p;
		x = (int128) x * x % p;
	}
	return res;
}
bool MillerRabin(ll n) {
	if(n < 2) return 0;
	if(n == 2) return 1;
	int u = n - 1, t = 0;
	while(u & 2 == 0) u >>= 1, t++;
	REP(i, 10) {
		ll a = rnd() % (n - 2) + 2;
		ll val = fp(a, u, n);
		if(val == 1) continue;
		int s = 0;
		for(; s < t; s++) {
			if(val == n - 1) break;
			val = (int128) val * val % n;
		}
		if(s == t) return 0;
	}
	return 1;
}
void solve() {
	ll n;
	while(cin >> n) {
		cout << (MillerRabin(n) ? 'Y' : 'N') << endl;
	}
}
int main() {
	ios :: sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	solve();
	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
N
N
N
N

result: