結果

問題 No.6 使いものにならないハッシュ
ユーザー むらためむらため
提出日時 2017-08-12 13:47:29
言語 Nim
(2.0.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,902 bytes
コンパイル時間 824 ms
コンパイル使用メモリ 59,760 KB
最終ジャッジ日時 2023-09-12 14:04:29
合計ジャッジ時間 1,763 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 50) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated]
/home/judge/data/code/Main.nim(1, 62) Error: cannot open file: queues

ソースコード

diff #

import sequtils,strutils,strscans,algorithm,math,future,sets,queues,tables,macros
macro unpack*(rhs: seq,cnt: static[int]): auto =
  let t = genSym(); result = quote do:(let `t` = `rhs`;())
  for i in 0..<cnt: result[0][1].add(quote do:`t`[`i`])
template get*():string = stdin.readLine()
template times*(n:int,body:untyped): untyped = (for _ in 0..<n: body)
template `max=`*(x,y:typed):void = x = max(x,y)
template `min=`*(x,y:typed):void = x = min(x,y)

proc `+=`[T](x:var set[T],y:T):void = x.incl(y)
proc `-=`[T](x:var set[T],y:T):void = x.excl(y)
proc `+=`[T](x:var set[T],y:set[T]):void = x = x.union(y)
proc `*=`[T](x:var set[T],y:set[T]):void = x = x.intersection(y)
proc `-=`[T](x:var set[T],y:set[T]):void = x = x.difference(y)
converter toInt8(x:int) : int8 = x.toU8()

proc parseDecimal(n:int) : auto = ($n).mapIt(it.ord - '0'.ord)
proc getIsPrimes(n:int) :seq[bool] = # [0...n]
  result = newSeqWith(n+1,true)
  result[0] = false
  result[1] = false
  for i in 2..n.float.sqrt.int :
    if not result[i]: continue
    for j in countup(i*2,n,i):
      result[j] = false

proc getPrimes(n:int,start:int = 0):seq[int] = # [2,3,5,...n]
  let isPrime = getIsPrimes(n)
  result = @[]
  for i,p in isPrime:
    if i < start: continue
    if p : result &= i

proc hash(n:int) : int =
  result = ($n).mapIt(it.ord - '0'.ord).sum()
  if result > 9:
    result = hash(result)

# hash は 0,3,6,9にならない (3は一度だけ) => 124578の6が最大だが、大きい方を優先するので途中でbreakはできない
let
  K = get().parseInt
  N = get().parseInt
  primes = getPrimes(N,start=K)

var
  L = 0
  hashes : set[int8] = {}
  ans = (prime:0, card:0)
for i,p in primes:
  let h = hash(p)
  if h in hashes:
    while h in hashes:
      hashes -= hash(primes[L])
      L += 1
  hashes += h
  if hashes.card >= ans.card:
    ans.prime = primes[L]
    ans.card = hashes.card
echo ans.prime
0