結果
問題 |
No.774 tatyamと素数大富豪
|
ユーザー |
|
提出日時 | 2019-01-02 19:52:57 |
言語 | Python2 (2.7.18) |
結果 |
AC
|
実行時間 | 993 ms / 2,000 ms |
コード長 | 1,223 bytes |
コンパイル時間 | 240 ms |
コンパイル使用メモリ | 6,912 KB |
実行使用メモリ | 8,448 KB |
最終ジャッジ日時 | 2024-11-21 13:41:33 |
合計ジャッジ時間 | 11,951 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 14 |
ソースコード
#!/usr/bin/python2 # -*- coding: utf-8 -*- # † from itertools import permutations from random import randrange def memoize(func): cache = {} def _memoizer(*args): res = cache.get(args) if res == None: res = cache[args] = func(*args) return res _memoizer.cache = cache return _memoizer @memoize def is_probable_prime(n, k=20): if n == 1: return False for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]: if n == p: return True if n % p == 0: return False s, d = 0, n-1 while d & 1 == 0: s, d = s+1, d>>1 for _ in xrange(k): a = randrange(2, n-1) x = pow(a, d, n) if x == 1 or x == n-1: continue for _ in xrange(s-1): x = x * x % n if x == n-1: break # could be strong liar, try another a else: return False # composite if we reached end of this loop return True # probably prime if reached end of outer loop n = int(raw_input()) v = map(int, raw_input().split()) maxi = -1 for perm in permutations(v): x = int(''.join(map(str, perm))) if maxi < x and is_probable_prime(x): maxi = x print maxi