QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#722521 | #249. Miller Rabin 算法 | AutomatiC__ | WA | 18ms | 3756kb | C++23 | 1.4kb | 2024-11-07 19:23:18 | 2024-11-07 19:23:19 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cstdlib>
#include <bitset>
#include <deque>
#include <ctime>
#include <random>
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3fll
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
mt19937_64 myrand(time(0));
ll binpow(ll a, ll b, ll mod) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
bool miller_rabin(ll n) {
if (n < 3 || n % 2 == 0) return n == 2;
if (n % 3 == 0) return n == 3;
ll u = n - 1, t = 0;
while (u % 2 == 0) u /= 2, ++t;
for (int i = 1; i <= 9; i++) {
ll a = myrand() % (n - 3) + 2;
ll v = binpow(a, u, n);
if (v == 1) continue;
int s = 0;
for (s = 0; s < t; s++) {
if (v == n - 1) break;
v = v * v % n;
}
if (s == t) return false;
}
return true;
}
int main() {
ll x;
while (cin >> x) {
if (miller_rabin(x)) {
cout << "Y" << endl;
} else {
cout << "N" << endl;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 18ms
memory: 3756kb
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:
wrong answer 2nd lines differ - expected: 'Y', found: 'N'