QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#342396 | #249. Miller Rabin 算法 | KevinLikesCoding# | TL | 0ms | 0kb | C++14 | 1.2kb | 2024-03-01 11:06:03 | 2024-03-01 11:06:04 |
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;
}
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 N N N N