結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー kacho65535kacho65535
提出日時 2020-11-15 13:43:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,161 bytes
コンパイル時間 1,631 ms
コンパイル使用メモリ 169,808 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 14:32:59
合計ジャッジ時間 4,977 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define mod 1000000007ll
#define loop(i, n) for (int i = 0; i < n; i++)
#define flagcount(bit) __builtin_popcount(bit)
#define flag(x) (1ll << x)
#define flagadd(bit, x) bit |= flag(x)
#define flagpop(bit, x) bit &= ~flag(x)
#define flagon(bit, i) bit &flag(i)
#define flagoff(bit, i) !(bit & (1ll << i))
#define all(v) v.begin(), v.end()
#define putout(a) cout << a << '\n'
template <typename T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b; // aをbで更新
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b; // aをbで更新
        return true;
    }
    return false;
}
void AS(ll X, ll L, ll R) { assert(L <= X && X <= R); }
template <typename T>
bool primejudge(T n)
{
    if (n < 2)
        return false;
    else if (n == 2)
        return true;
    else if (n % 2 == 0)
        return false;
    double sqrtn = sqrt(n);
    for (T i = 3; i < sqrtn + 1; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
        i++;
    }
    return true;
}
//modpow
long long modp(ll a, ll n, ll p)
{
    long long res = 1;
    while (n > 0)
    {
        if (n & 1)
            res = res * a % p;
        a = a * a % p;
        n >>= 1;
    }
    return res;
}
//素数ならtrue,合成数ならfalseを返す
bool MR(ll X)
{
    if (X < 10000)
        return primejudge(X);
    if (X % 2 == 0)
        return false;
    ll Z = X - 1;
    ll s = 0, t = 0;
    while (Z % 2 == 0)
    {
        s++;
        Z /= 2;
    }
    t = Z;
    vector<ll> A = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
    for (auto a : A)
    {
        if (modp(a, t, X) == 1)
            return true;
        loop(i, s)
        {
            if (modp(a, modp(2, i, X) * t, X) == X - 1)
                return true;
        }
    }
    return false;
}
int main()
{
    ll Q;
    cin >> Q;
    loop(i, Q)
    {
        ll X;
        cin >> X;
        if (MR(X))
            cout << X << " " << 1 << endl;
        else
            cout << X << " " << 0 << endl;
    }
    return 0;
}
0