QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#479364 | #249. Miller Rabin 算法 | Max_s_xaM# | TL | 0ms | 0kb | C++14 | 1.8kb | 2024-07-15 16:46:31 | 2024-07-15 16:46:31 |
answer
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
typedef long long ll;
typedef double lf;
typedef __int128 LL;
namespace FastIO
{
const int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
#define blank(x) ((x) == ' ' || (x) == '\n' || (x) == '\r' || (x) == '\t')
template <typename T> void Read(T &x)
{
x = 0; char ch = gc; bool f = 0;
while (ch < '0' || ch > '9') {if (ch == '-') f = 1; ch = gc;}
while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc;
if (f) x = -x;
}
void Read(char *s)
{
char ch = gc;
while (blank(ch)) ch = gc;
while (!blank(ch)) *s++ = ch, ch = gc;
*s = 0;
}
}
using namespace std;
using FastIO::Read;
inline ll Qpow(ll x, int y, int mod) { ll res = 1; while (y) (y & 1) && (res = (LL)res * x % mod), x = (LL)x * x % mod, y >>= 1; return res; }
const int P[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
bool Miller_Rabin(ll n)
{
if (n < 3 || !(n & 1)) return n == 2;
ll r = n - 1, d = 0;
while (!(r & 1)) r >>= 1, d++;
for (int _ = 0; _ < 7; _++)
{
if (n == P[_]) return 1;
ll p = P[_], v = Qpow(p, r, n);
if (v == 1) continue;
for (int j = 0; j <= d; j++)
{
if (j == d) return 0;
if (v == n - 1) break;
v = (LL)v * v % n;
}
}
return 1;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
ll x;
while (cin >> x)
{
cout << (Miller_Rabin(x) ? "Y" : "N") << '\n';
}
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...