結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー wacchozwacchoz
提出日時 2020-10-17 11:16:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,194 bytes
コンパイル時間 4,219 ms
コンパイル使用メモリ 229,972 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 14:22:04
合計ジャッジ時間 4,607 ms
ジャッジサーバーID
(参考情報)
judge1 / 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>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

#ifdef DEBUG
    #include "debug.h"
    #define dump(...)                                                              \
        DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
                << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
                << endl                                                            \
                << "    ",                                                         \
            dump_func(__VA_ARGS__)
#else
    #define dump(...)
#endif

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}

//#define int long long
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define YES puts("YES")
#define Yes puts("Yes")
#define NO  puts("NO")
#define No  puts("No")
#define ALL(v) (v).begin(), (v).end()
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

//*
#define mod 1000000007 /*/
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL<<60)


bool isprime(int  n){
    if (n < 2) return false;
    if (n == 2) return true;
    for (int i = 2; i*i <= n; i++)
            if (n % i == 0) return false;

    return true;
}


using ll = long long;
// x^k mod m
long long modpow( ll x, ll k, ll m ){
    ll ret = 1;
    while(k > 0){
        if( ret % 2 == 1){
          ret = ((__uint128_t)ret * x) % m;
        }
        k /= 2;
        x = ((__uint128_t)x * x) % m;
    }
    return ret;
}

bool miller_rabin_primality_test(ll N){
    if(N<2) return false;
    if(N==2) return true;
    else if(N%2==0) return false;
    
    auto test = [&](int a){
        ll t = N-1, s=0;
        while(t % 2 == 0){
            t /= 2; s++;
        }
        ll r = modpow(a, t, N);
        if(r==1 || r==N-1) return true;
        for(int i=0; i<s; i++){
            r = ((__uint128_t)r * r) % N;
            if(r==N-1) return true;
        }
        return false;   // 合成数である
    };

    vector<int> witness;
    if(N < (1LL<<32))
        witness = {2, 7, 61};
    else
        witness = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    for(int a : witness){
        int g = gcd(a,N);
        if(1<g && g < N) return false;
        if(g==N) continue;
        if(!test(a)) return false;
    }
    return true;
}

void solve(){
    int N;
    cin >> N;
    while(N--){
        long long x;
        cin >> x;
        cout << x << " " << miller_rabin_primality_test(x) << endl;
    }
}


signed main(){
    cout << fixed << setprecision(18);
    cerr << fixed;
    solve();
}
0