QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#479389#249. Miller Rabin 算法A_programmer#WA 6ms3716kbC++17987b2024-07-15 17:04:192024-07-15 17:04:20

Judging History

This is the latest submission verdict.

  • [2024-07-15 17:04:20]
  • Judged
  • Verdict: WA
  • Time: 6ms
  • Memory: 3716kb
  • [2024-07-15 17:04:19]
  • Submitted

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 (cin >> x)
    {
        if (x == 1) { cout << "N\n"; continue; }
        cout << (judge(x) ? "Y\n" : "N\n");
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 3716kb

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'