結果

問題 No.774 tatyamと素数大富豪
ユーザー むらためむらため
提出日時 2019-02-05 06:53:16
言語 Nim
(2.0.2)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,360 bytes
コンパイル時間 2,948 ms
コンパイル使用メモリ 67,628 KB
実行使用メモリ 14,240 KB
最終ジャッジ日時 2023-09-14 03:30:31
合計ジャッジ時間 6,666 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1,795 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 116 ms
4,376 KB
testcase_15 WA -
testcase_16 AC 544 ms
4,376 KB
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 27) Warning: imported and not used: 'strutils' [UnusedImport]

ソースコード

diff #

import sequtils,algorithm,strutils,math
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
template `min=`*(x,y) = x = min(x,y)
const INF = int.high div 4
proc powerWhenTooBig(x,n:int,modulo:int = 0): int =
  proc mul(x,n,modulo:int):int =
    if n == 0: return 0
    if n == 1: return x
    result = mul(x,n div 2,modulo) mod modulo
    result = (result * 2) mod modulo
    result = (result + x * (n mod 2 == 1).int) mod modulo
    if n == 0: return 1
  if n == 1: return x
  let
    pow_2 = powerWhenTooBig(x,n div 2,modulo)
    odd = if n mod 2 == 1: x else: 1
  if modulo > 0:
    const maybig = int.high.float.sqrt.int div 2
    if pow_2 > maybig or odd > maybig:
      result = mul(pow_2,pow_2,modulo)
      result = mul(result,odd,modulo)
    else:
      result = (pow_2 * pow_2) mod modulo
      result = (result * odd) mod modulo
  else:
    return pow_2 * pow_2 * odd
proc millerRabinIsPrime(n:int):bool = # O(log n)
  proc ctz(n:int):int{.importC: "__builtin_ctzll", noDecl .} # 01<0000> -> 4
  proc power(x,n:int,modulo:int = 0): int =
    if n == 0: return 1
    if n == 1: return x
    let pow_2 = power(x,n div 2,modulo)
    result = pow_2 * pow_2 * (if n mod 2 == 1: x else: 1)
    if modulo > 0: result = result mod modulo
  if n <= 1 : return false
  if n == 2 or n == 3 or n == 5: return true
  if n mod 2 == 0: return false
  let
    s = ctz(n - 1)
    d = (n - 1) div (1 shl s)
  var a_list = @[2, 7, 61]
  if n >= 4_759_123_141 and n < 341_550_071_728_321:
    a_list = @[2, 3, 5, 7, 11, 13, 17]
  if n in a_list : return true
  for a in a_list:
    if powerWhenTooBig(a,d,n) == 1 : continue
    let notPrime = toSeq(0..<s).allIt(powerWhenTooBig(a,d*(1 shl it),n) != n-1)
    if notPrime : return false
  return true


proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

proc decode(A:var seq[int]): int =
  for a in A:
    if a < 10: result = 10 * result + a
    else:result = 100 * result + a
let n = scan()
var A = newSeqWith(n,scan()).sorted(cmp)
var cards = newSeq[int]()
while true:
  cards &= A.decode()
  if not A.nextPermutation() : break
cards.sort(cmp,Descending)
for c in cards:
  if c.millerRabinIsPrime() : quit $c,0
echo -1
0