結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー ミドリムシミドリムシ
提出日時 2018-04-03 16:01:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,541 bytes
コンパイル時間 3,104 ms
コンパイル使用メモリ 83,640 KB
実行使用メモリ 814,336 KB
最終ジャッジ日時 2024-04-29 12:53:45
合計ジャッジ時間 8,113 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
using namespace std;

unsigned long multiplication(unsigned long base, long exponent, unsigned long mod){
    if(exponent % 2){
        return (multiplication(base, exponent - 1, mod) + base) % mod;
    }else if(exponent){
        return multiplication(base, exponent / 2, mod) * 2ul % mod;
    }else{
        return 0;
    }
}

long power(long base, long exponent, long mod){
    if(exponent % 2){
        return multiplication(power(base, exponent - 1, mod), base, mod);
    }else if(exponent){
        long root_ans = power(base, exponent / 2, mod);
        return multiplication(root_ans, root_ans, mod);
    }else{
        return 1;
    }
}

bool if_prime(long number){
    if(number <= 1){
        return false;
    }
    int exponent_of_2 = 0;
    long number_tmp = number - 1;
    while(number_tmp % 2 == 0){
        exponent_of_2++;
        number_tmp /= 2;
    }
    random_device rnd;
    for(int j = 0; j < 50; j++){
        long random_number = rnd() % (number - 1) + 1;
        bool if_composite = (power(random_number, number_tmp, number) != 1);
        for(int k = 0; k < exponent_of_2; k++){
            if_composite &= (power(random_number, (1 << k) * number_tmp, number) != number - 1);
        }
        if(if_composite){
            return false;
        }
    }
    return true;
}

int main(){
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        long x;
        cin >> x;
        cout << x << " " << if_prime(x) << endl;
    }
}
0