結果

問題 No.2893 Minahoshi (Hard)
ユーザー 👑 seekworserseekworser
提出日時 2024-07-02 03:17:54
言語 Nim
(2.0.2)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 15,129 bytes
コンパイル時間 6,216 ms
コンパイル使用メモリ 94,848 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-13 20:50:39
合計ジャッジ時間 18,238 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 674 ms
6,940 KB
testcase_07 AC 807 ms
6,940 KB
testcase_08 AC 1,119 ms
6,940 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 -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2])
# source: https://github.com/kemuniku/cplib/tree/main/src/cplib/tmpl/citrus.nim
ImportExpand "cplib/tmpl/citrus" <=== "when not declared CPLIB_TMPL_CITRUS:\n    const CPLIB_TMPL_CITRUS* = 1\n    {.warning[UnusedImport]: off.}\n    {.hint[XDeclaredButNotUsed]: off.}\n    import os\n    import algorithm\n    import sequtils\n    import tables\n    import macros\n    import std/math\n    import sets\n    import strutils\n    import strformat\n    import sugar\n    import streams\n    import deques\n    import bitops\n    import heapqueue\n    import options\n    import hashes\n    const MODINT998244353* = 998244353\n    const MODINT1000000007* = 1000000007\n    when not declared CPLIB_UTILS_CONSTANTS:\n        const CPLIB_UTILS_CONSTANTS* = 1\n        const INF32*: int32 = 100100111.int32\n        const INF64*: int = int(3300300300300300491)\n    \n    const INFL = INF64\n    type double* = float64\n    let readNext = iterator(getsChar: bool = false): string {.closure.} =\n        while true:\n            var si: string\n            try: si = stdin.readLine\n            except EOFError: yield \"\"\n            for s in si.split:\n                if getsChar:\n                    for i in 0..<s.len():\n                        yield s[i..i]\n                else:\n                    if s.isEmptyOrWhitespace: continue\n                    yield s\n    proc input*(t: typedesc[string]): string = readNext()\n    proc input*(t: typedesc[char]): char = readNext(true)[0]\n    proc input*(t: typedesc[int]): int = readNext().parseInt\n    proc input*(t: typedesc[float]): float = readNext().parseFloat\n    macro input*(t: typedesc, n: varargs[int]): untyped =\n        var repStr = \"\"\n        for arg in n:\n            repStr &= &\"({arg.repr}).newSeqWith \"\n        parseExpr(&\"{repStr}input({t})\")\n    macro input*(ts: varargs[auto]): untyped =\n        var tupStr = \"\"\n        for t in ts:\n            tupStr &= &\"input({t.repr}),\"\n        parseExpr(&\"({tupStr})\")\n    macro input*(n: int, ts: varargs[auto]): untyped =\n        for typ in ts:\n            if typ.typeKind != ntyAnything:\n                error(\"Expected typedesc, got \" & typ.repr, typ)\n        parseExpr(&\"({n.repr}).newSeqWith input({ts.repr})\")\n    proc `fmtprint`*(x: int or string or char or bool): string = return $x\n    proc `fmtprint`*(x: float or float32 or float64): string = return &\"{x:.16f}\"\n    proc `fmtprint`*[T](x: seq[T] or Deque[T] or HashSet[T] or set[T]): string = return x.toSeq.join(\" \")\n    proc `fmtprint`*[T, N](x: array[T, N]): string = return x.toSeq.join(\" \")\n    proc `fmtprint`*[T](x: HeapQueue[T]): string =\n        var q = x\n        while q.len != 0:\n            result &= &\"{q.pop()}\"\n            if q.len != 0: result &= \" \"\n    proc `fmtprint`*[T](x: CountTable[T]): string =\n        result = x.pairs.toSeq.mapIt(&\"{it[0]}: {it[1]}\").join(\" \")\n    proc `fmtprint`*[K, V](x: Table[K, V]): string =\n        result = x.pairs.toSeq.mapIt(&\"{it[0]}: {it[1]}\").join(\" \")\n    proc print*(prop: tuple[f: File, sepc: string, endc: string, flush: bool], args: varargs[string, `fmtprint`]) =\n        for i in 0..<len(args):\n            prop.f.write(&\"{args[i]}\")\n            if i != len(args) - 1: prop.f.write(prop.sepc) else: prop.f.write(prop.endc)\n        if prop.flush: prop.f.flushFile()\n    proc print*(args: varargs[string, `fmtprint`]) = print((f: stdout, sepc: \" \", endc: \"\\n\", flush: false), args)\n    const LOCAL_DEBUG{.booldefine.} = false\n    macro getSymbolName(x: typed): string = x.toStrLit\n    macro debug*(args: varargs[untyped]): untyped =\n        when LOCAL_DEBUG:\n            result = newNimNode(nnkStmtList, args)\n            template prop(e: string = \"\"): untyped = (f: stderr, sepc: \"\", endc: e, flush: true)\n            for i, arg in args:\n                if arg.kind == nnkStrLit:\n                    result.add(quote do: print(prop(), \"\\\"\", `arg`, \"\\\"\"))\n                else:\n                    result.add(quote do: print(prop(\": \"), getSymbolName(`arg`)))\n                    result.add(quote do: print(prop(), `arg`))\n                if i != args.len - 1: result.add(quote do: print(prop(), \", \"))\n                else: result.add(quote do: print(prop(), \"\\n\"))\n        else:\n            return (quote do: discard)\n    proc `%`*(x: SomeInteger, y: SomeInteger): int =\n        result = x mod y\n        if y > 0 and result < 0: result += y\n        if y < 0 and result > 0: result += y\n    proc `//`*(x: SomeInteger, y: SomeInteger): int =\n        result = x div y\n        if y > 0 and result * y > x: result -= 1\n        if y < 0 and result * y < x: result -= 1\n    proc `^`*(x: SomeInteger, y: SomeInteger): int = x xor y\n    proc `&`*(x: SomeInteger, y: SomeInteger): int = x and y\n    proc `|`*(x: SomeInteger, y: SomeInteger): int = x or y\n    proc `>>`*(x: SomeInteger, y: SomeInteger): int = x shr y\n    proc `<<`*(x: SomeInteger, y: SomeInteger): int = x shl y\n    proc `%=`*(x: var SomeInteger, y: SomeInteger): void = x = x % y\n    proc `//=`*(x: var SomeInteger, y: SomeInteger): void = x = x // y\n    proc `^=`*(x: var SomeInteger, y: SomeInteger): void = x = x ^ y\n    proc `&=`*(x: var SomeInteger, y: SomeInteger): void = x = x & y\n    proc `|=`*(x: var SomeInteger, y: SomeInteger): void = x = x | y\n    proc `>>=`*(x: var SomeInteger, y: SomeInteger): void = x = x >> y\n    proc `<<=`*(x: var SomeInteger, y: SomeInteger): void = x = x << y\n    proc `[]`*(x, n: int): bool = (x and (1 shl n)) != 0\n    proc `[]=`*(x: var int, n: int, i: bool) =\n        if i: x = x or (1 << n)\n        else: (if x[n]: x = x xor (1 << n))\n    proc pow*(a, n: int, m = INF64): int =\n        var\n            rev = 1\n            a = a\n            n = n\n        while n > 0:\n            if n % 2 != 0: rev = (rev * a) mod m\n            if n > 1: a = (a * a) mod m\n            n >>= 1\n        return rev\n    when not declared CPLIB_MATH_ISQRT:\n        const CPLIB_MATH_ISQRT* = 1\n        proc isqrt*(n: int): int =\n            var x = n\n            var y = (x + 1) shr 1\n            while y < x:\n                x = y\n                y = (x + n div x) shr 1\n            return x\n    \n    proc chmax*[T](x: var T, y: T): bool {.discardable.} = (if x < y: (x = y; return true; ) return false)\n    proc chmin*[T](x: var T, y: T): bool {.discardable.} = (if x > y: (x = y; return true; ) return false)\n    proc `max=`*[T](x: var T, y: T) = x = max(x, y)\n    proc `min=`*[T](x: var T, y: T) = x = min(x, y)\n    proc at*(x: char, a = '0'): int = int(x) - int(a)\n    proc Yes*(b: bool = true): void = print(if b: \"Yes\" else: \"No\")\n    proc No*(b: bool = true): void = Yes(not b)\n    proc YES_upper*(b: bool = true): void = print(if b: \"YES\" else: \"NO\")\n    proc NO_upper*(b: bool = true): void = Yes_upper(not b)\n    const DXY* = [(0, -1), (0, 1), (-1, 0), (1, 0)]\n    const DDXY* = [(1, -1), (1, 0), (1, 1), (0, -1), (0, 1), (-1, -1), (-1, 0), (-1, 1)]\n    macro exit*(statement: untyped): untyped = (quote do: (`statement`; quit()))\n    proc initHashSet[T](): Hashset[T] = initHashSet[T](0)\n"
# source: https://github.com/zer0-star/Nim-ACL/tree/master/src/atcoder/string.nim
ImportExpand "atcoder/string" <=== "when not declared ATCODER_STRING_HPP:\n  const ATCODER_STRING_HPP* = 1\n\n  import std/algorithm\n  import std/sequtils\n\n  proc sa_naive*(s:seq[int]):seq[int] =\n    let n = s.len\n    var sa = newSeq[int](n)\n    for i in 0..<n:sa[i] = i\n    sa.sort() do (l, r:int) -> int:\n      if l == r: return 0\n      var (l, r) = (l, r)\n      while l < n and r < n:\n        if s[l] != s[r]: return cmp[int](s[l], s[r])\n        l.inc;r.inc\n      return cmp[int](n, l)\n    return sa\n  \n  proc sa_doubling*(s:seq[int]):seq[int] =\n    let n = s.len\n    var\n      sa, tmp = newSeq[int](n)\n      rnk = s\n    for i in 0..<n:sa[i] = i\n    var k = 1\n    while k < n:\n      proc cmp0(x, y:int):int =\n        if rnk[x] != rnk[y]: return cmp[int](rnk[x], rnk[y])\n        let\n          rx = if x + k < n: rnk[x + k] else: -1\n          ry = if y + k < n: rnk[y + k] else: -1\n        return cmp[int](rx, ry)\n      sa.sort(cmp0)\n      tmp[sa[0]] = 0\n      for i in 1..<n:\n        tmp[sa[i]] = tmp[sa[i - 1]] + (if cmp0(sa[i - 1], sa[i]) < 0: 1 else: 0)\n      swap(tmp, rnk)\n      k = k shl 1\n    return sa\n\n  # SA-IS, linear-time suffix array construction\n  # Reference:\n  # G. Nong, S. Zhang, and W. H. Chan,\n  # Two Efficient Algorithms for Linear Time Suffix Array Construction\n#  template <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40>\n  proc sa_is*(s:seq[int], upper:int, THRESHOLD_NAIVE:static[int] = 10, THRESHOLD_DOUBLING:static[int] = 40):seq[int] =\n    let n = s.len\n    if n == 0: return @[]\n    if n == 1: return @[0]\n    if n == 2:\n      if s[0] < s[1]:\n        return @[0, 1]\n      else:\n        return @[1, 0]\n    if n < THRESHOLD_NAIVE:\n      return sa_naive(s)\n    if n < THRESHOLD_DOUBLING:\n      return sa_doubling(s)\n    \n    var sa, ls = newSeq[int](n)\n    for i in countdown(n - 2, 0):\n      ls[i] = if s[i] == s[i + 1]: ls[i + 1] else: (s[i] < s[i + 1]).int\n    var sum_l, sum_s = newSeq[int](upper + 1)\n    for i in 0..<n:\n      if ls[i] == 0:\n        sum_s[s[i]].inc\n      else:\n        sum_l[s[i] + 1].inc\n    for i in 0..upper:\n      sum_s[i] += sum_l[i]\n      if i < upper: sum_l[i + 1] += sum_s[i]\n\n    proc induce(lms:seq[int]):auto =\n      sa.fill(-1)\n      var buf = sum_s\n      for d in lms:\n        if d == n: continue\n        sa[buf[s[d]]] = d\n        buf[s[d]].inc\n      buf = sum_l\n      sa[buf[s[n - 1]]] = n - 1\n      buf[s[n - 1]].inc\n      for i in 0..<n:\n        let v = sa[i]\n        if v >= 1 and ls[v - 1] == 0:\n          sa[buf[s[v - 1]]] = v - 1\n          buf[s[v - 1]].inc\n      buf = sum_l\n      for i in countdown(n - 1, 0):\n        let v = sa[i]\n        if v >= 1 and ls[v - 1] != 0:\n          buf[s[v - 1] + 1].dec\n          sa[buf[s[v - 1] + 1]] = v - 1\n  \n    var lms_map = newSeqWith(n + 1, -1)\n    var m = 0\n    for i in 1..<n:\n      if ls[i - 1] == 0 and ls[i] != 0:\n        lms_map[i] = m\n        m.inc\n    var lms = newSeqOfCap[int](m)\n    for i in 1..<n:\n      if ls[i - 1] == 0 and ls[i] != 0:\n        lms.add(i)\n  \n    induce(lms)\n  \n    if m != 0:\n      var sorted_lms = newSeqOfCap[int](m)\n      for v in sa:\n        if lms_map[v] != -1: sorted_lms.add(v)\n      var\n        rec_s = newSeq[int](m)\n        rec_upper = 0;\n      rec_s[lms_map[sorted_lms[0]]] = 0\n      for i in 1..<m:\n        var (l, r) = (sorted_lms[i - 1], sorted_lms[i])\n        let\n          end_l = if lms_map[l] + 1 < m: lms[lms_map[l] + 1] else: n\n          end_r = if lms_map[r] + 1 < m: lms[lms_map[r] + 1] else: n\n        var same = true\n        if end_l - l != end_r - r:\n          same = false\n        else:\n          while l < end_l:\n            if s[l] != s[r]:\n              break\n            l.inc\n            r.inc\n          if l == n or s[l] != s[r]: same = false\n        if not same: rec_upper.inc\n        rec_s[lms_map[sorted_lms[i]]] = rec_upper\n\n      let rec_sa =\n        sa_is[THRESHOLD_NAIVE, THRESHOLD_DOUBLING](rec_s, rec_upper)\n\n      for i in 0..<m:\n        sorted_lms[i] = lms[rec_sa[i]]\n      induce(sorted_lms)\n    return sa\n\n# namespace internal\n  \n  proc suffix_array*(s:seq[int], upper:int):seq[int] =\n    assert 0 <= upper\n    for d in s:\n      assert 0 <= d and d <= upper\n    return sa_is(s, upper)\n  \n  proc suffix_array*[T](s:seq[T]):seq[int] =\n    let n = s.len\n    var idx = newSeq[int](n)\n    for i in 0..<n: idx[i] = i\n    idx.sort(proc(l,r:int):int = cmp[int](s[l], s[r]))\n    var s2 = newSeq[int](n)\n    var now = 0\n    for i in 0..<n:\n      if i != 0 and s[idx[i - 1]] != s[idx[i]]: now.inc\n      s2[idx[i]] = now\n    return sa_is(s2, now)\n  \n  proc suffix_array*(s:string):seq[int] =\n    return sa_is(s.mapIt(it.int), 255);\n  \n  # Reference:\n  # T. Kasai, G. Lee, H. Arimura, S. Arikawa, and K. Park,\n  # Linear-Time Longest-Common-Prefix Computation in Suffix Arrays and Its\n  # Applications\n  proc lcp_array*[T](s:seq[T], sa:seq[int]):seq[int] =\n    let n = s.len\n    assert n >= 1\n    var rnk = newSeq[int](n)\n    for i in 0..<n:\n      rnk[sa[i]] = i\n    var lcp = newSeq[int](n - 1)\n    var h = 0;\n    for i in 0..<n:\n      if h > 0: h.dec\n      if rnk[i] == 0: continue\n      let j = sa[rnk[i] - 1]\n      while j + h < n and i + h < n:\n        if s[j + h] != s[i + h]: break\n        h.inc\n      lcp[rnk[i] - 1] = h\n    return lcp\n  \n  proc lcp_array*(s:string, sa:seq[int]):seq[int] = lcp_array(s.mapIt(it.int), sa)\n  \n  # Reference:\n  # D. Gusfield,\n  # Algorithms on Strings, Trees, and Sequences: Computer Science and\n  # Computational Biology\n  proc z_algorithm*[T](s:seq[T]):seq[T] =\n    let n = s.len\n    if n == 0: return @[]\n    var z = newSeq[int](n)\n    z[0] = 0\n    var j = 0\n    for i in 1..<n:\n      var k = z[i].addr\n      k[] = if j + z[j] <= i: 0 else: min(j + z[j] - i, z[i - j])\n      while i + k[] < n and s[k[]] == s[i + k[]]: k[].inc\n      if j + z[j] < i + z[i]: j = i\n    z[0] = n\n    return z\n\n  proc z_algorithm*(s:string):auto = z_algorithm(s.mapIt(it.int))\n\n# namespace atcoder\n\n"

# {.checks: off.}
type str = string

var f = @[1]
while f[^1] <= 2000: f.add(pow(2, f.len+1) + f.len - 1)

proc inf(n: int): int =
    var n = n-1
    var pos = 1
    while f[pos] <= n:
        result += pos * (f[pos] - f[pos-1])
        pos += 1
    result += pos * (n - f[pos-1])

proc solve() =
    var n = input(int)
    var ans = "ab".toDeque
    var rem = initHashSet[str]()
    while ans.len < n:
        var p = f.lowerBound(ans.len - 1)
        if f[p] == ans.len - 1:
            ans.addLast(ans[0])
            for bit in 0..<(1 << (p+2)):
                var s = ""
                for i in 0..<(p+2):
                    if bit[i]: s &= "a"
                    else: s &= "b"
                rem.incl(s)
            for i in 0..ans.len - (p+2):
                var sn = ""
                for j in i..<(i+p+2): sn &= ans[j]
                rem.excl(sn)
            continue
        var sn = ""
        for j in 0..<p: sn &= ans[^(p-j)]
        var flag = false
        var tmp: string
        for si in rem:
            if si.startsWith(sn):
                flag = true
                tmp = si
                ans.addLast(si[^1])
                break
        if flag:
            rem.excl(tmp)
            continue
        var si = rem.pop
        while true:
            ans.addLast(ans[p])
            discard ans.popFirst
            var sn = ""
            for j in 0..<p: sn &= ans[^(p-j)]
            if si.startsWith(sn): break
        rem.excl(si)
        ans.addLast(si[^1])
    var s = ans.toseq.join("")
    var sa = s.suffix_array
    var lcp = s.lcp_array(sa)
    assert lcp.sum == inf(n)
    print(s)

var t = input(int)
for _ in 0..<t: solve()
0