結果
| 問題 |
No.774 tatyamと素数大富豪
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-12-22 02:23:15 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,289 bytes |
| コンパイル時間 | 2,132 ms |
| コンパイル使用メモリ | 176,896 KB |
| 実行使用メモリ | 6,940 KB |
| 最終ジャッジ日時 | 2024-06-13 02:23:57 |
| 合計ジャッジ時間 | 8,788 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 12 WA * 2 |
ソースコード
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 % 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;
}