QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#479392 | #249. Miller Rabin 算法 | A_programmer# | WA | 9ms | 3676kb | C++17 | 997b | 2024-07-15 17:05:21 | 2024-07-15 17:05:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int p[8] = {2, 3, 5, 7, 11, 13, 17, 19};
int fpow(ll a, ll b, ll mod)
{
ll ans = 1;
while (b)
{
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
bool judge(ll x)
{
ll t = x - 1; int k = 0;
while (!(t & 1)) k++, t >>= 1;
for (int i = 0; i < 8; i++)
{
if (x == p[i]) return true;
ll tmp = fpow(p[i], t, x);
for (int j = 1; j <= k; j++)
{
int nxt = tmp * tmp % x;
if (nxt == 1 && tmp != 1 && tmp != x - 1) return false;
tmp = nxt;
}
if (tmp != 1) return false;
}
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll x;
while (~scanf("%lld", &x))
{
if (x == 1) { cout << "N\n"; continue; }
cout << (judge(x) ? "Y\n" : "N\n");
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 9ms
memory: 3676kb
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'