結果

問題 No.774 tatyamと素数大富豪
ユーザー legosukelegosuke
提出日時 2019-10-26 14:49:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 1,232 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 10,888 KB
実行使用メモリ 10,016 KB
最終ジャッジ日時 2023-10-12 09:20:55
合計ジャッジ時間 4,390 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 255 ms
9,348 KB
testcase_01 AC 22 ms
9,216 KB
testcase_02 AC 271 ms
9,176 KB
testcase_03 AC 25 ms
9,324 KB
testcase_04 AC 22 ms
9,380 KB
testcase_05 AC 22 ms
9,212 KB
testcase_06 AC 22 ms
9,364 KB
testcase_07 AC 22 ms
9,160 KB
testcase_08 AC 22 ms
9,348 KB
testcase_09 AC 340 ms
10,016 KB
testcase_10 AC 256 ms
9,352 KB
testcase_11 AC 254 ms
9,264 KB
testcase_12 AC 247 ms
9,412 KB
testcase_13 AC 248 ms
9,176 KB
testcase_14 AC 66 ms
9,884 KB
testcase_15 AC 247 ms
9,168 KB
testcase_16 AC 277 ms
9,424 KB
testcase_17 AC 273 ms
9,320 KB
testcase_18 AC 251 ms
9,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import cmp_to_key
import itertools
import random

def is_prime(n: int, k: int = 25) -> bool:
    assert(n >= 0)
    if n == 2: return True
    if n < 2 or n & 1 == 0: return False

    d = n - 1
    while d & 1 == 0:
        d >>= 1

    for i in range(k):
        a = random.randint(1, n - 1)
        if n == a:
            return True
        t = d
        y = pow(a, t, n)
        while t != n - 1 and y != 1 and y != n - 1:
            y = pow(y, 2, n)
            t <<= 1
        if y != n - 1 and t & 1 == 0:
            return False
    return True

def comp(a: str, b: str) -> bool:
    if a == b:
        return 0
    sz = min(len(a), len(b))
    for i in range(sz):
        if a[i] > b[i]:
            return -1
        elif a[i] < b[i]:
            return 1
    return -1 if len(a) > len(b) else 1

if __name__ == '__main__':
    n = int(input())
    a = input().split()
    a = sorted(a, key=cmp_to_key(comp))
    ans = -1
    used = set()
    for p in itertools.permutations(a):
        num = int(''.join(p))
        if ans >= 0 and num < ans:
            continue
        if num in used:
            continue
        used.add(num)
        if is_prime(num):
            ans = max(ans, num)
    print(ans)
0