結果

問題 No.774 tatyamと素数大富豪
ユーザー nebukuro09nebukuro09
提出日時 2018-12-22 02:22:37
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,289 bytes
コンパイル時間 1,981 ms
コンパイル使用メモリ 175,948 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 21:44:11
合計ジャッジ時間 9,338 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,288 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 20 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 421 ms
4,376 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 182 ms
4,376 KB
testcase_12 AC 1,195 ms
4,384 KB
testcase_13 AC 615 ms
4,380 KB
testcase_14 AC 84 ms
4,376 KB
testcase_15 AC 274 ms
4,376 KB
testcase_16 AC 102 ms
4,376 KB
testcase_17 AC 12 ms
4,380 KB
testcase_18 AC 135 ms
4,380 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 = 10) {
    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