結果

問題 No.774 tatyamと素数大富豪
ユーザー nebukuro09nebukuro09
提出日時 2018-12-22 02:25:12
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,696 ms / 2,000 ms
コード長 1,348 bytes
コンパイル時間 1,504 ms
コンパイル使用メモリ 173,980 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 21:44:56
合計ジャッジ時間 9,723 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,584 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 28 ms
4,380 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 431 ms
4,376 KB
testcase_10 AC 13 ms
4,376 KB
testcase_11 AC 298 ms
4,380 KB
testcase_12 AC 1,696 ms
4,380 KB
testcase_13 AC 754 ms
4,380 KB
testcase_14 AC 85 ms
4,380 KB
testcase_15 AC 441 ms
4,380 KB
testcase_16 AC 104 ms
4,380 KB
testcase_17 AC 13 ms
4,376 KB
testcase_18 AC 211 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

bool miller_rabin(BigInt n, int k = 20) {
    if (n == 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    BigInt d = n - 1;
    BigInt s = 0;
    while (d % 2) {
        s += 1;
        d /= 2;
    }
    while (k--) {
        BigInt a = uniform(0L, n.to!long).to!BigInt;
        if (powmod(a, d, n) == 1) continue;
        bool composite = true;
        BigInt r = 1;
        while (s--) {
            if (powmod(a, r * d, n) == n - 1) {
                composite = false;
                break;
            }
            r *= 2;
        }
        if (composite) return false;
    }
    return true;
}

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    A.sort();
    BigInt ans = -1;

    do {
        BigInt n = A.map!(to!string).join("").to!BigInt;
        if (n <= ans) continue;
        if (miller_rabin(n)) ans = max(ans, n);
    } while (nextPermutation(A));

    ans.writeln;
}

BigInt powmod(BigInt a, BigInt x, BigInt m) {
    BigInt ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0