結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー mamekinmamekin
提出日時 2018-04-09 21:16:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8,167 ms / 9,973 ms
コード長 2,951 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 102,112 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:17:06
合計ジャッジ時間 18,624 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 4,022 ms
5,376 KB
testcase_05 AC 3,626 ms
5,376 KB
testcase_06 AC 375 ms
5,376 KB
testcase_07 AC 365 ms
5,376 KB
testcase_08 AC 364 ms
5,376 KB
testcase_09 AC 8,167 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

class Xorshift
{
private:
    unsigned x, y, z, w;
public:
    Xorshift(unsigned seed=88675123, int loop=50){
        x = 123456789;
        y = 362436069;
        z = 521288629;
        w = seed;
        while(--loop >= 0){
            (*this)();
        }
    }
    unsigned operator()(){
        unsigned t=(x^(x<<11));
        x=y; y=z; z=w;
        return w=(w^(w>>19))^(t^(t>>8));
    }
    unsigned operator()(unsigned size){
        return (*this)() % size;
    }
    unsigned long long get64(){
        unsigned long long a = (*this)();
        unsigned long long b = (*this)();
        return (a << 32) | b;
    }
    unsigned long long get64(unsigned long long size){
        return get64() % size;
    }
    template <class T>
    void shufflte(vector<T>& v){
        unsigned n = v.size();
        for(unsigned i=0; i<n-1; ++i){
            unsigned j = (*this)(n-i) + i;
            swap(v[i], v[j]);
        }
    }
};
Xorshift xorshift;

unsigned long long modmul(unsigned long long a, unsigned long long b, unsigned long long mod)
{
    unsigned long long ret = 0;
    while(b > 0){
        if(b & 1){
            ret += a;
            if(ret > mod)
                ret -= mod;
        }
        a *= 2;
        if(a > mod)
            a -= mod;
        b >>= 1;
    }
    return ret;
}

unsigned long long modpow(unsigned long long a, unsigned long long b, unsigned long long mod)
{
    unsigned long long ret = 1;
    unsigned long long tmp = a;
    while(b > 0){
        if(b & 1)
            ret = modmul(ret, tmp, mod);
        tmp = modmul(tmp, tmp, mod);
        b >>= 1;
    }
    return ret;
}

bool millerRabinPrimalityTest(long long n, int loop=30)
{
    if(n < 2)
        return false;

    long long d = n - 1;
    int s = 0;
    while(d % 2 == 0){
        ++ s;
        d /= 2;
    }

    while(--loop >= 0){
        long long a = xorshift.get64(n-1) + 1;
        long long x = modpow(a, d, n);
        if(x != 1){
            bool isComposite = true;
            for(int r=0; r<s; ++r){
                if(x == n - 1){
                    isComposite = false;
                    break;
                }
                x = modmul(x, x, n);
            }
            if(isComposite)
                return false;
        }
    }

    return true;
}

int main()
{
    int n;
    cin >> n;

    while(--n >= 0){
        long long x;
        cin >> x;
        int ans = millerRabinPrimalityTest(x) ? 1 : 0;
        cout << x << ' ' << ans << endl;
    }

    return 0;
}
0