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.. n :break 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はできない if false: # 愚直な実装 let K = get().parseInt N = get().parseInt primes = getPrimes(N,start=K) var L = 0 hashes : set[int8] = {} ans = (prime:0, card:0) for 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 else: # WARN: 最適化 let K = get().parseInt N = get().parseInt if K <= 3: if N <= 3:# 12 13 23 if K == 1: echo N else: echo 3 else: if N < 11: echo 2 else: echo 3 quit() let # どうせ最後の方にしかないのである程度大きければ探索範囲を縮めて良い start = if N > 10000: max(K, (N.float * 0.95).int) else: K primes = getPrimes(N,start) var R = primes.len() - 1 hashes : set[int8] = {} ans = (prime:0, card:0) for p in primes.reversed(): # countdown ? let h = hash(p) if h in hashes: while h in hashes: hashes -= hash(primes[R]) R -= 1 hashes += h if hashes.card >= ans.card: ans.prime = p ans.card = hashes.card if ans.card == 6: break echo ans.prime