結果

問題 No.774 tatyamと素数大富豪
ユーザー nebukuro09
提出日時 2018-12-22 02:25:12
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1,514 ms / 2,000 ms
コード長 1,348 bytes
コンパイル時間 2,567 ms
コンパイル使用メモリ 175,756 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-13 02:24:14
合計ジャッジ時間 9,243 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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