結果

問題 No.774 tatyamと素数大富豪
ユーザー Shuz*Shuz*
提出日時 2018-06-23 17:44:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 330 ms / 2,000 ms
コード長 1,024 bytes
コンパイル時間 490 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 144,220 KB
最終ジャッジ日時 2024-10-01 13:16:47
合計ジャッジ時間 5,624 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#coding:utf8
import random
import itertools

def is_prime(q,k=30):
    q = abs(q)
    #計算するまでもなく判定できるものははじく
    if q == 2 or q == 3: return True
    if q == 1: return False
    if q%2==0 or q%3==0: return False

    #n-1=2^s*dとし(但しaは整数、dは奇数)、dを求める
    d = (q-1)>>1
    while d&1 == 0:
        d >>= 1
    
    #判定をk回繰り返す
    for i in range(k):
        a = random.randint(1,q-1)
        t = d
        y = pow(a,t,q)
        #[0,s-1]の範囲すべてをチェック
        while t != q-1 and y != 1 and y != q-1: 
            y = pow(y,2,q)
            t <<= 1
        if y != q-1 and t&1 == 0:
            return False
    return True

n=int(input())
a=list(map(int,input().split()))
s=[]
for i in list(itertools.permutations(a)):
    s.append(int(''.join(map(str,i))))
s.sort(reverse=True)
x=1
p=0
for i in s:
    if p-i:
        if is_prime(i):
            print(i)
            x=0
            break
    p=i
if x==1:
    print(-1)
0