結果

問題 No.774 tatyamと素数大富豪
ユーザー RiburaRibura
提出日時 2020-05-20 01:08:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 957 ms / 2,000 ms
コード長 1,387 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 30,556 KB
最終ジャッジ日時 2024-04-09 22:48:55
合計ジャッジ時間 11,699 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 957 ms
30,556 KB
testcase_01 AC 29 ms
10,624 KB
testcase_02 AC 803 ms
10,752 KB
testcase_03 AC 39 ms
11,008 KB
testcase_04 AC 29 ms
10,624 KB
testcase_05 AC 29 ms
10,752 KB
testcase_06 AC 28 ms
10,624 KB
testcase_07 AC 28 ms
10,752 KB
testcase_08 AC 28 ms
10,752 KB
testcase_09 AC 883 ms
11,520 KB
testcase_10 AC 768 ms
10,624 KB
testcase_11 AC 809 ms
10,880 KB
testcase_12 AC 910 ms
27,888 KB
testcase_13 AC 881 ms
27,880 KB
testcase_14 AC 127 ms
11,520 KB
testcase_15 AC 831 ms
11,008 KB
testcase_16 AC 787 ms
11,008 KB
testcase_17 AC 790 ms
10,752 KB
testcase_18 AC 785 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import permutations


def miller_rabin(n):
    # 確率的素数判定(ミラーラビン素数判定法)
    # 素数なら確実に True を返す、合成数なら確率的に False を返す
    # True が返ったなら恐らく素数で、False が返ったなら確実に合成数である
    # 参考: http://tjkendev.github.io/procon-library/python/prime/probabilistic.html
    # 検証: https://yukicoder.me/submissions/381948
    primes = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]  # 32bit: [2, 7, 61]
    if n == 2:
        return True
    if n <= 1 or n & 1 == 0:
        return False
    d = m1 = n - 1
    d //= d & -d
    for a in primes:
        if a >= n:
            return True
        t, y = d, pow(a, d, n)
        while t != m1 and y != 1 and y != m1:
            y = y * y % n
            t <<= 1
        if y != m1 and t & 1 == 0:
            return False
    return True


n = int(readline())
a = list(readline().rstrip().decode().split())
check = set()
for aa in permutations(a):
    v = ''
    for ai in aa:
        v += ai
    check.add(int(v))
check = list(check)
check.sort(reverse=True)
for v in check:
    if miller_rabin(v):
        print(v)
        break
else:
    print(-1)
0