結果

問題 No.774 tatyamと素数大富豪
ユーザー かりあげクンかりあげクン
提出日時 2020-08-26 12:28:21
言語 Nim
(2.0.2)
結果
RE  
実行時間 -
コード長 1,182 bytes
コンパイル時間 3,879 ms
コンパイル使用メモリ 66,176 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 19:14:27
合計ジャッジ時間 4,719 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 WA -
testcase_02 RE -
testcase_03 WA -
testcase_04 WA -
testcase_05 RE -
testcase_06 AC 1 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 1 ms
5,376 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils
import sequtils
import algorithm
import bitops

const Mod : int = 1_000_000_007

proc powMod*(n : int, x : int, m = Mod) : int =
  if x == 0: return 1
  if n == 1: return 1
  if x == 1: return (n mod m)
  if x mod 2 == 0:
    return powMod((n * n) mod m, x div 2, m) mod m
  else:
    return n * powMod((n * n) mod m, x div 2, m) mod m

proc millerRabin*(n : int) : bool =
  if n <= 1: return false
  if n == 2 and n == 3 and n == 5: return true
  if n mod 2 == 0: return false
  let
    s = popcount(n-1)
    d = (n - 1) div (1 shl s)
  var xs = @[2, 7, 61, 103]
  if n >= 4_759_123_141 and n < 341_550_071_728_321:
    xs = @[2,3,5,7,11,13,17]
  if n in xs : return true
  for a in xs :
    if powMod(a,d,n) == 1: continue
    let notPrime = toSeq(0..<s).allIt(powMod(a,d*(1 shl it),n) != n-1)
    if notPrime: return false
  return true

proc conc(A : seq[int]) : int =
  for a in A:
    if a < 10: result = 10 * result + a
    else:result = 100 * result + a

var
  _ = stdin.readLine
  A = stdin.readLine.split.map(parseInt)
  ans = -1
A.sort()

while true:
  let p = A.conc()
  if ans < p and p.millerRabin(): ans = p
  if not nextPermutation(A): break
echo ans
0