QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#450325#249. Miller Rabin 算法KevinLikesCodingTL 0ms0kbC++141.5kb2024-06-22 09:56:262024-06-22 09:56:27

Judging History

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

  • [2024-06-22 09:56:27]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-06-22 09:56:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#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 PII pair<int, int>
#define FI(v) v.first
#define SE(v) v.second
#define endl '\n'
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); }
const int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 331};
ll mul(ll x, ll y, ll p) {
	ll res = 0;
	for(; y; y >>= 1) {
		if(y & 1) res = (res + x) % p;
		x = (x + x) % p;
	}
	return res;
}
ll fp(ll x, ll y, ll p) {
	ll res = 1;
	for(; y; y >>= 1) {
		if(y & 1) res = mul(res, x, p);
		x = mul(x, x, p);
	}
	return res;
}
bool MillerRabin(ll p, ll a) {
	ll d = p - 1, r = 0;
	while(!(d & 1)) d >>= 1, r++;
	ll res = fp(a, d, p);
	if(res == 1) return 1;
	REP(i, r) {
		if(res == p - 1) return 1;
		res = mul(res, res, p);
	}
	return 0;
}
bool ip(ll n) {
	if(n <= 1) return 0;
	REP(i, 10) {
		if(n == prime[i]) return 1;
		if(n % prime[i] == 0) return 0;
		if(!MillerRabin(n, prime[i])) return 0;
	}
	return 1;
}
void solve() {
	ll x;
	while(cin >> x) {
		cout << (ip(x) ? "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
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: