結果

問題 No.774 tatyamと素数大富豪
ユーザー lam6er
提出日時 2025-03-26 15:43:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 668 ms / 2,000 ms
コード長 1,369 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 184,152 KB
最終ジャッジ日時 2025-03-26 15:44:04
合計ジャッジ時間 4,800 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import permutations

def is_prime(n):
    if n <= 1:
        return False
    elif n <= 3:
        return True
    elif n % 2 == 0:
        return False
    d = n - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1
    bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    for a in bases:
        if a >= n:
            continue
        x = pow(a, d, n)
        if x == 1 or x == n - 1:
            continue
        for _ in range(s - 1):
            x = pow(x, 2, n)
            if x == n - 1:
                break
        else:
            return False
    return True

def main():
    n = int(sys.stdin.readline())
    a = list(map(int, sys.stdin.readline().split()))
    
    # Generate all unique permutations of the cards
    perm_set = set()
    for p in permutations(a):
        perm_set.add(p)
    
    # Convert permutations to their string representations
    candidates = set()
    for p in perm_set:
        s = ''.join(map(str, p))
        candidates.add(s)
    
    # Sort the candidates in descending order to check the largest first
    candidates = sorted(candidates, reverse=True)
    
    # Check each candidate for primality
    for num_str in candidates:
        num = int(num_str)
        if is_prime(num):
            print(num)
            return
    print(-1)

if __name__ == '__main__':
    main()
0