import sequtils,strutils,strscans,algorithm,math,sugar,sets,tables,macros macro unpack*(rhs: seq,cnt: static[int]): auto = let t = genSym(); result = quote do:(let `t` = `rhs`;()) for i in 0..= cap): var n = newSeq[T](cap*2) for i, x in pairs(q): # don't use copyMem because the GC and because it's slower. shallowCopy(n[i], x) shallowCopy(q.data, n) q.mask = cap*2 - 1 q.wr = q.count q.rd = 0 inc q.count q.data[q.wr] = item q.wr = (q.wr + 1) and q.mask template default[T](t: typedesc[T]): T = var v: T v proc pop*[T](q: var Queue[T]): T {.inline, discardable.} = dec q.count result = q.data[q.rd] q.data[q.rd] = default(type(result)) q.rd = (q.rd + 1) and q.mask proc enqueue*[T](q: var Queue[T], item: T) = q.add(item) proc dequeue*[T](q: var Queue[T]): T = q.pop() proc `$`*[T](q: Queue[T]): string = result = "[" for x in items(q): # Don't remove the items here for reasons that don't fit in this margin. if result.len > 1: result.add(", ") result.add($x) result.add("]") 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