import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) # source: 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 = 1001000027.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.. 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: src/cplib/graph/graph.nim ImportExpand "cplib/graph/graph" <=== "when not declared CPLIB_GRAPH_GRAPH:\n const CPLIB_GRAPH_GRAPH* = 1\n\n import sequtils\n import math\n type DynamicGraph*[T] = ref object of RootObj\n edges*: seq[seq[(int32, T)]]\n len*: int\n type StaticGraph*[T] = ref object of RootObj\n src*, dst*: seq[int32]\n cost*: seq[T]\n elist*: seq[(int32, T)]\n start*: seq[int32]\n len*: int\n\n type WeightedDirectedGraph*[T] = ref object of DynamicGraph[T]\n type WeightedUnDirectedGraph*[T] = ref object of DynamicGraph[T]\n type UnWeightedDirectedGraph* = ref object of DynamicGraph[int]\n type UnWeightedUnDirectedGraph* = ref object of DynamicGraph[int]\n type WeightedDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n type WeightedUnDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n type UnWeightedDirectedStaticGraph* = ref object of StaticGraph[int]\n type UnWeightedUnDirectedStaticGraph* = ref object of StaticGraph[int]\n\n type GraphTypes*[T] = DynamicGraph[T] or StaticGraph[T]\n type DirectedGraph* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph\n type UnDirectedGraph* = WeightedUnDirectedGraph or UnWeightedUnDirectedGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n type WeightedGraph*[T] = WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T] or WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T]\n type UnWeightedGraph* = UnWeightedDirectedGraph or UnWeightedUnDirectedGraph or UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n type DynamicGraphTypes* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedUnDirectedGraph or UnWeightedUnDirectedGraph\n type StaticGraphTypes* = WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n\n proc add_edge_dynamic_impl*[T](g: DynamicGraph[T], u, v: int, cost: T, directed: bool) =\n g.edges[u].add((v.int32, cost))\n if not directed: g.edges[v].add((u.int32, cost))\n\n proc initWeightedDirectedGraph*(N: int, edgetype: typedesc = int): WeightedDirectedGraph[edgetype] =\n result = WeightedDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n proc add_edge*[T](g: var WeightedDirectedGraph[T], u, v: int, cost: T) =\n g.add_edge_dynamic_impl(u, v, cost, true)\n\n proc initWeightedUnDirectedGraph*(N: int, edgetype: typedesc = int): WeightedUnDirectedGraph[edgetype] =\n result = WeightedUnDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n proc add_edge*[T](g: var WeightedUnDirectedGraph[T], u, v: int, cost: T) =\n g.add_edge_dynamic_impl(u, v, cost, false)\n\n proc initUnWeightedDirectedGraph*(N: int): UnWeightedDirectedGraph =\n result = UnWeightedDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n proc add_edge*(g: var UnWeightedDirectedGraph, u, v: int) =\n g.add_edge_dynamic_impl(u, v, 1, true)\n\n proc initUnWeightedUnDirectedGraph*(N: int): UnWeightedUnDirectedGraph =\n result = UnWeightedUnDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n proc add_edge*(g: var UnWeightedUnDirectedGraph, u, v: int) =\n g.add_edge_dynamic_impl(u, v, 1, false)\n\n proc len*[T](G: WeightedGraph[T]): int = G.len\n proc len*(G: UnWeightedGraph): int = G.len\n\n iterator `[]`*[T](g: WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T], x: int): (int, T) =\n for e in g.edges[x]: yield (e[0].int, e[1])\n iterator `[]`*(g: UnWeightedDirectedGraph or UnWeightedUnDirectedGraph, x: int): int =\n for e in g.edges[x]: yield e[0].int\n\n proc add_edge_static_impl*[T](g: StaticGraph[T], u, v: int, cost: T, directed: bool) =\n g.src.add(u.int32)\n g.dst.add(v.int32)\n g.cost.add(cost)\n if not directed:\n g.src.add(v.int32)\n g.dst.add(u.int32)\n g.cost.add(cost)\n\n proc build_impl*[T](g: StaticGraph[T]) =\n g.start = newSeqWith(g.len + 1, 0.int32)\n for i in 0.. 0, \"Static Graph must be initialized before use.\"\n\n iterator `[]`*[T](g: WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T], x: int): (int, T) =\n g.static_graph_initialized_check()\n for i in g.start[x]..\n inline unsigned long long calc_mul(const unsigned long long &a, const unsigned long long &b) {\n return (unsigned long long)(((__uint128_t)(a) * b) >> 64);\n }\n \"\"\".}\n proc calc_mul*(a, b: culonglong): culonglong {.importcpp: \"calc_mul(#, #)\", nodecl, inline.}\n proc rem*(T: typedesc[BarrettModint], a: uint): uint32 =\n when T is StaticBarrettModint:\n const im = get_im(T.M)\n const M = get_M(T)\n var x = (calc_mul(cast[culonglong](a), cast[culonglong](im))).uint\n var r = a - x * M\n if M <= r: r += M\n return cast[uint32](r)\n else:\n var p = get_param(T)\n var x = (calc_mul(cast[culonglong](a), cast[culonglong](p.im))).uint\n var r = a - x * p.M\n if p.M <= r: r += p.M\n return cast[uint32](r)\n proc init*(T: typedesc[BarrettModint], a: T or SomeInteger): auto =\n when a is T: return a\n else:\n if a in 0..= T.umod: a.a -= T.umod\n proc `-=`*[T: BarrettModint](a: var T, b: T or SomeInteger) =\n a.a -= init(T, b).a\n if a.a >= T.umod: a.a += T.umod\n proc `*=`*[T: BarrettModint] (a: var T, b: T or SomeInteger) =\n a.a = rem(T, (a.a).uint * (init(T, b).a).uint)\n proc inv*[T: BarrettModint](x: T): T =\n assert x.val != 0\n var x: int32 = int32(x.val)\n var y: int32 = T.mod\n var u = 1i32\n var v, t = 0i32\n while y > 0:\n t = x div y\n x -= t * y\n u -= t * v\n swap(x, y)\n swap(u, v)\n return init(T, u)\n proc `/=`*[T: BarrettModint](a: var T, b: T or SomeInteger) = a *= init(T, b).inv\n proc val*(a: BarrettModint): int = a.a.int\n macro declarStaticBarrettModint*(name, M) =\n let converter_name = ident(\"to\" & $`name`)\n quote do:\n type `name`* = StaticBarrettModint[`M`]\n converter `converter_name`*(a: int): StaticBarrettModint[`M`] = init(StaticBarrettModint[`M`], a)\n macro declarDynamicBarrettModint*(name, id) =\n let converter_name = ident(\"to\" & $`name`)\n quote do:\n type `name`* = DynamicBarrettModint[`id`]\n converter `converter_name`*(a: int): DynamicBarrettModint[`id`] = init(DynamicBarrettModint[`id`], a)\n \n when not declared CPLIB_MODINT_MODINT_MONTGOMERY:\n const CPLIB_MODINT_MODINT_MONTGOMERY* = 1\n import std/macros\n import std/tables\n type StaticMontgomeryModint*[M: static[uint32]] = object\n a: uint32\n type DynamicMontgomeryModint*[M: static[uint32]] = object\n a: uint32\n type MontgomeryModint* = StaticMontgomeryModint or DynamicMontgomeryModint\n \n proc get_r*(M: uint32): uint32 =\n result = M\n for _ in 0..<4: result *= 2u32 - M * result\n proc get_n2*(M: uint32): uint32 = uint32((not uint(M - 1u32)) mod uint(M))\n proc check_params(M, r: uint32) =\n assert M < (1u32 shl 30), \"invalid mod >= 2^30\"\n assert (M and 1u32) == 1u32, \"invalid mod % 2 == 0\"\n assert r * M == 1, \"r * mod != 1\"\n var montgomeryParamCache {.compileTime.}: Table[uint32, NimNode]\n var montgomeryCachedParam: tuple[M, r, n2: uint32]\n macro get_param*[M: static[uint32]](self: typedesc[StaticMontgomeryModint[M]]): untyped =\n if M notin montgomeryParamCache:\n let value = (M.uint32, get_r(M), get_n2(M))\n montgomeryParamCache[M] = newLit(value)\n return montgomeryParamCache[M]\n template get_param*(self: typedesc[DynamicMontgomeryModint]): tuple[M, r, n2: uint32] =\n # FIXME: cast(noSideEffect)を付けないと、set_of_mint.join(\" \")とかで死ぬ。\n # もうちょっと筋の良い解決方法があればそうしたい\n {.cast(noSideEffect).}:\n montgomeryCachedParam\n template get_M*(T: typedesc[MontgomeryModint]): uint32 =\n when T is StaticMontgomeryModint: T.M\n else: get_param(T).M\n proc setMod*[T: static[uint32]](self: typedesc[DynamicMontgomeryModint[T]], M: SomeInteger or SomeUnsignedInt) =\n var r = get_r(M.uint32)\n var n2 = get_n2(M.uint32)\n montgomeryCachedParam = (M: M.uint32, r: get_r(M.uint32), n2: n2)\n check_params(M.uint32, r)\n template umod*[T: MontgomeryModint](self: typedesc[T] or T): uint32 =\n when self is typedesc:\n when self is StaticMontgomeryModint: self.M\n else: get_param(self).M\n else: T.umod\n template `mod`*[T: MontgomeryModint](self: typedesc[T] or T): int32 = (T.umod).int32\n \n proc reduce(T: typedesc[StaticMontgomeryModint], b: uint): uint32 =\n let (_, r, _) = get_param(T)\n return cast[uint32]((b + uint(cast[uint32](b) * (not (r - 1u32))) * T.M) shr 32)\n proc reduce(T: typedesc[DynamicMontgomeryModint], b: uint): uint32 =\n var p = get_param(T)\n return cast[uint32]((b + uint(cast[uint32](b) * (not (p.r - 1u32))) * p.M) shr 32)\n proc init*(T: typedesc[MontgomeryModint], a: T or SomeInteger): auto =\n when a is T: return a\n elif T is StaticMontgomeryModint:\n let (_, r, n2) = get_param(T)\n check_params(T.M, r)\n var ai = reduce(T, uint(a.int32 mod T.M.int32 + T.M.int32) * n2)\n result = StaticMontgomeryModint[T.M](a: ai)\n elif T is DynamicMontgomeryModint:\n var p = get_param(T)\n var ai = reduce(T, uint(a.int32 mod p.M.int32 + p.M.int32) * p.n2)\n result = DynamicMontgomeryModint[T.M](a: ai)\n \n proc `+=`*[T: MontgomeryModint](a: var T, b: T or SomeInteger) =\n a.a += init(T, b).a - T.get_M * 2u32\n if cast[int32](a.a) < 0i32: a.a += T.get_M * 2u32\n proc `-=`*[T: MontgomeryModint](a: var T, b: T or SomeInteger) =\n a.a -= init(T, b).a\n if cast[int32](a.a) < 0i32: a.a += T.get_M * 2u32\n proc val*[T: MontgomeryModint](a: T): int =\n result = reduce(T, a.a).int\n if result.uint32 >= T.get_M: result -= T.get_M.int\n \n proc `-`*[T: MontgomeryModint](a: T): T = (result = init(T, 0); result -= a)\n proc `*=`*[T: MontgomeryModint] (a: var T, b: T or SomeInteger) = a.a = reduce(T, uint(a.a) * init(T, b).a)\n proc inv*[T: MontgomeryModint](x: T): T =\n assert x.val != 0\n var x: int32 = int32(x.val)\n var y: int32 = T.mod\n var u = 1i32\n var v, t = 0i32\n while y > 0:\n t = x div y\n x -= t * y\n u -= t * v\n swap(x, y)\n swap(u, v)\n return init(T, u)\n proc `/=`*[T: MontgomeryModint](a: var T, b: T or SomeInteger) = a *= init(T, b).inv\n \n macro declarStaticMontgomeryModint*(name, M) =\n let converter_name = ident(\"to\" & $`name`)\n quote do:\n type `name`* = StaticMontgomeryModint[`M`]\n converter `converter_name`*(a: int): StaticMontgomeryModint[`M`] = init(StaticMontgomeryModint[`M`], a)\n macro declarDynamicMontgomeryModint*(name, id) =\n let converter_name = ident(\"to\" & $`name`)\n quote do:\n type `name`* = DynamicMontgomeryModint[`id`]\n converter `converter_name`*(a: int): DynamicMontgomeryModint[`id`] = init(DynamicMontgomeryModint[`id`], a)\n \n import std/math\n import std/algorithm\n declarStaticMontgomeryModint(modint998244353_montgomery, 998244353u32)\n declarStaticMontgomeryModint(modint1000000007_montgomery, 1000000007u32)\n declarDynamicMontgomeryModint(modint_montgomery, 1u32)\n declarStaticBarrettModint(modint998244353_barrett, 998244353u32)\n declarStaticBarrettModint(modint1000000007_barrett, 1000000007u32)\n declarDynamicBarrettModint(modint_barrett, 1u32)\n proc `+`*(a, b: MontgomeryModint or BarrettModint): auto = (result = a; result += b)\n proc `-`*(a, b: MontgomeryModint or BarrettModint): auto = (result = a; result -= b)\n proc `*`*(a, b: MontgomeryModint or BarrettModint): auto = (result = a; result *= b)\n proc `/`*(a, b: MontgomeryModint or BarrettModint): auto = (result = a; result /= b)\n proc `+`*(a: MontgomeryModint or BarrettModint, b: SomeInteger): auto = (result = a; result += b)\n proc `-`*(a: MontgomeryModint or BarrettModint, b: SomeInteger): auto = (result = a; result -= b)\n proc `*`*(a: MontgomeryModint or BarrettModint, b: SomeInteger): auto = (result = a; result *= b)\n proc `/`*(a: MontgomeryModint or BarrettModint, b: SomeInteger): auto = (result = a; result /= b)\n proc `+`*[ModInt: MontgomeryModint or BarrettModint](a: SomeInteger, b: Modint): auto = init(Modint, a) + b\n proc `-`*[ModInt: MontgomeryModint or BarrettModint](a: SomeInteger, b: Modint): auto = init(Modint, a) - b\n proc `*`*[ModInt: MontgomeryModint or BarrettModint](a: SomeInteger, b: Modint): auto = init(Modint, a) * b\n proc `/`*[ModInt: MontgomeryModint or BarrettModint](a: SomeInteger, b: Modint): auto = init(Modint, a) / b\n proc `/`*[ModInt: MontgomeryModint or BarrettModint](a: ModInt, b: static int): auto =\n when ModInt is StaticMontgomeryModint or ModInt is StaticBarrettModint:\n const tmp = init(Modint, b).inv\n return a * tmp\n else:\n return a * init(Modint, b).inv\n proc pow*(a: MontgomeryModint or BarrettModint, n: int): auto =\n result = init(typeof(a), 1)\n var a = a\n var n = n\n while n > 0:\n if (n and 1) == 1: result *= a\n a *= a\n n = (n shr 1)\n proc `$`*(a: MontgomeryModint or BarrettModint): string = $(a.val)\n proc estimate_rational*(a: MontgomeryModint or BarrettModint, ub: int = isqrt(typeof(a).mod)): string =\n var v: seq[tuple[s, n, d: int]]\n for d in 1..ub:\n var n = (a * d).val\n if n * 2 > a.mod:\n n = - (a.mod - n)\n if gcd(n, d) > 1: continue\n v.add((n.abs + d, n, d))\n v.sort\n return $v[0].n & \"/\" & $v[0].d\n \n when not declared CPLIB_MATH_INVGCD:\n const CPLIB_MATH_INVGCD* = 1\n # @param b `1 <= b`\n # @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\n import std/math\n proc inv_gcd*(a, b: int): (int, int) =\n var a = floorMod(a, b)\n if a == 0: return (b, 0)\n var\n s = b\n t = a\n m0 = 0\n m1 = 1\n \n while t != 0:\n var u = s div t\n s -= t * u\n m0 -= m1 * u\n \n var tmp = s\n s = t\n t = tmp\n tmp = m0\n m0 = m1\n m1 = tmp\n if m0 < 0: m0 += b div s\n return (s, m0)\n \n \n\n {.emit: \"\"\"\n#ifndef CPLIB_CONVOLUTION_AVX2_NTT_HPP\n#define CPLIB_CONVOLUTION_AVX2_NTT_HPP\n#include \n#include \n#include \n#include \n#include \nusing cZ=std::size_t;\n#pragma GCC target(\"avx2,bmi2\")\n#pragma GCC optimize(\"O3\")\nnamespace cplib_avx2_ntt {\nusing u32 = std::uint32_t;\nusing u64 = std::uint64_t;\nusing V=__m256i;\nu32 modulus = 998244353U;\nu32 primitive_root = 3U;\nstruct Montgomery {\nu32 negative_inverse;\nu32 radix;\nu32 radix_squared;\nMontgomery() {\nnegative_inverse = 1;\nfor (int i = 0; i < 5; ++i) {\nnegative_inverse *= 2U + negative_inverse * modulus;\n}\nradix = (u32)((u64(1) << 32) % modulus);\nradix_squared = (u32)(u64(radix) * radix % modulus);\n}\ninline u32 multiply(u32 a, u32 b) const {\nconst u64 product = u64(a) * b;\nconst u32 correction = (u32)(product) * negative_inverse;\nu32 value = (u32)(\n(product + u64(correction) * modulus) >> 32);\nif (value >= modulus) value -= modulus;\nreturn value;\n}\ninline u32 to_montgomery(u32 value) const {\nreturn multiply(value, radix_squared);\n}\n};\ninline u32 add_mod(u32 a, u32 b) {\nconst u32 sum = a + b;\nreturn sum >= modulus ? sum - modulus : sum;\n}\ninline u32 subtract_mod(u32 a, u32 b) {\nreturn a >= b ? a - b : a + modulus - b;\n}\ninline u32 power_mod(u32 base, u32 exponent) {\nu32 result = 1;\nwhile (exponent != 0) {\nif (exponent & 1U) result = (u32)(u64(result) * base % modulus);\nbase = (u32)(u64(base) * base % modulus);\nexponent >>= 1;\n}\nreturn result;\n}\ninline u32 find_primitive_root(u32 prime) {\nswitch (prime) {\ncase 998244353U: return 3U;\ncase 754974721U: return 11U;\ncase 167772161U: return 3U;\ncase 469762049U: return 3U;\ndefault: break;\n}\nu32 factors[16];\nint factor_count = 0;\nu32 remaining = prime - 1;\nfor (u32 divisor = 2; u64(divisor) * divisor <= remaining; ++divisor) {\nif (remaining % divisor != 0) continue;\nfactors[factor_count++] = divisor;\ndo {\nremaining /= divisor;\n} while (remaining % divisor == 0);\n}\nif (remaining != 1) factors[factor_count++] = remaining;\nfor (u32 candidate = 2;; ++candidate) {\nbool valid = true;\nfor (int i = 0; i < factor_count; ++i) {\nif (power_mod(candidate, (prime - 1) / factors[i]) == 1) {\nvalid = false;\nbreak;\n}\n}\nif (valid) return candidate;\n}\n}\ninline V shrink(V value) {\nconst V mod = _mm256_set1_epi32((int)(modulus));\nreturn _mm256_min_epu32(value, _mm256_sub_epi32(value, mod));\n}\ninline V shrink_twice_modulus(V value) {\nconst V twice_modulus = _mm256_set1_epi32(\n(int)(2U * modulus));\nreturn _mm256_min_epu32(\nvalue, _mm256_sub_epi32(value, twice_modulus));\n}\ninline V add_lazy(V a, V b) {\nreturn _mm256_add_epi32(a, b);\n}\ninline V subtract_lazy(V a, V b) {\nconst V twice_modulus = _mm256_set1_epi32(\n(int)(2U * modulus));\nreturn _mm256_sub_epi32(_mm256_add_epi32(a, twice_modulus), b);\n}\ninline V add_mod(V a, V b) {\nreturn shrink(_mm256_add_epi32(a, b));\n}\ninline V subtract_mod(V a, V b) {\nconst V mod = _mm256_set1_epi32((int)(modulus));\nreturn shrink(_mm256_sub_epi32(_mm256_add_epi32(a, mod), b));\n}\ninline V montgomery_multiply_lazy(\nV a, V b, const Montgomery& montgomery) {\nconst V inverse = _mm256_set1_epi32(\n(int)(montgomery.negative_inverse));\nconst V mod = _mm256_set1_epi32((int)(modulus));\nconst V even_product = _mm256_mul_epu32(a, b);\nconst V odd_product = _mm256_mul_epu32(\n_mm256_srli_epi64(a, 32), _mm256_srli_epi64(b, 32));\nconst V even_correction = _mm256_mul_epu32(even_product, inverse);\nconst V odd_correction = _mm256_mul_epu32(odd_product, inverse);\nconst V even_sum = _mm256_add_epi64(\neven_product, _mm256_mul_epu32(even_correction, mod));\nconst V odd_sum = _mm256_add_epi64(\nodd_product, _mm256_mul_epu32(odd_correction, mod));\nconst V even_result = _mm256_srli_epi64(even_sum, 32);\nconst V odd_result = _mm256_slli_epi64(\n_mm256_srli_epi64(odd_sum, 32), 32);\nreturn _mm256_or_si256(even_result, odd_result);\n}\ninline V montgomery_multiply(\nV a, V b, const Montgomery& montgomery) {\nreturn shrink(montgomery_multiply_lazy(a, b, montgomery));\n}\nclass TransformPlan {\ncZ size_;\nMontgomery montgomery_;\nu32* twiddles_;\nvoid fill_stage(u32* destination, cZ count, u32 ratio) {\nconst u32 ratio_montgomery = montgomery_.to_montgomery(ratio);\nu32 first_powers[8];\nfirst_powers[0] = montgomery_.radix;\nfor (int i = 1; i < 8; ++i) {\nfirst_powers[i] = montgomery_.multiply(\nfirst_powers[i - 1], ratio_montgomery);\n}\nif (count < 8) {\nstd::memcpy(destination, first_powers, count * sizeof(u32));\nreturn;\n}\nV powers = _mm256_loadu_si256(\n(const V*)(first_powers));\nu32 ratio_eighth = first_powers[7];\nratio_eighth = montgomery_.multiply(ratio_eighth, ratio_montgomery);\nconst V step = _mm256_set1_epi32((int)(ratio_eighth));\nfor (cZ i = 0; i < count; i += 8) {\n_mm256_storeu_si256(\n(V*)(destination + i), powers);\npowers = montgomery_multiply(powers, step, montgomery_);\n}\n}\nvoid build_twiddles() {\nfor (cZ length = size_;; length >>= 1) {\nconst cZ half = length >> 1;\nconst cZ offset = size_ - length;\nconst u32 root = power_mod(\nprimitive_root,\n(u32)((modulus - 1) / length));\nfill_stage(twiddles_ + offset, half, root);\nif (length == 2) break;\n}\n}\nvoid forward_single(\nu32* data, cZ length, u32* second = nullptr) const {\nconst cZ half = length >> 1;\nconst u32* twiddle = twiddles_ + size_ - length;\nfor (cZ block = 0; block < size_; block += length) {\nfor (cZ j = 0; j < half; j += 8) {\nconst V weight = _mm256_loadu_si256(\n(const V*)(twiddle + j));\nconst auto process = [&](u32* target) {\nconst V left = shrink_twice_modulus(\n_mm256_loadu_si256((const V*)(\ntarget + block + j)));\nconst V right = shrink_twice_modulus(\n_mm256_loadu_si256((const V*)(\ntarget + block + half + j)));\n_mm256_storeu_si256(\n(V*)(target + block + j),\nadd_lazy(left, right));\n_mm256_storeu_si256(\n(V*)(target + block + half + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(left, right), weight, montgomery_));\n};\nprocess(data);\nif (second != nullptr) process(second);\n}\n}\n}\nvoid forward_pair(\nu32* data, cZ length, u32* second = nullptr) const {\nconst cZ quarter = length >> 2;\nconst u32* outer = twiddles_ + size_ - length;\nconst u32* inner = twiddles_ + size_ - (length >> 1);\nfor (cZ block = 0; block < size_; block += length) {\nfor (cZ j = 0; j < quarter; j += 8) {\nconst V outer0 = _mm256_loadu_si256(\n(const V*)(outer + j));\nconst V outer1 = _mm256_loadu_si256(\n(const V*)(outer + quarter + j));\nconst V inner_weight = _mm256_loadu_si256(\n(const V*)(inner + j));\nconst auto process = [&](u32* target) {\nconst V a = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(target + block + j)));\nconst V b = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(target + block + quarter + j)));\nconst V c = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(target + block + 2 * quarter + j)));\nconst V d = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(target + block + 3 * quarter + j)));\nconst V ac_sum = shrink_twice_modulus(add_lazy(a, c));\nconst V ac_difference = montgomery_multiply_lazy(\nsubtract_lazy(a, c), outer0, montgomery_);\nconst V bd_sum = shrink_twice_modulus(add_lazy(b, d));\nconst V bd_difference = montgomery_multiply_lazy(\nsubtract_lazy(b, d), outer1, montgomery_);\n_mm256_storeu_si256(\n(V*)(target + block + j),\nadd_lazy(ac_sum, bd_sum));\n_mm256_storeu_si256(\n(V*)(target + block + quarter + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(ac_sum, bd_sum), inner_weight, montgomery_));\n_mm256_storeu_si256(\n(V*)(target + block + 2 * quarter + j),\nadd_lazy(ac_difference, bd_difference));\n_mm256_storeu_si256(\n(V*)(target + block + 3 * quarter + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(ac_difference, bd_difference),\ninner_weight, montgomery_));\n};\nprocess(data);\nif (second != nullptr) process(second);\n}\n}\n}\nvoid forward_pair_half_zero(u32* data, u32* second = nullptr) const {\nconst cZ quarter = size_ >> 2;\nconst u32* outer = twiddles_;\nconst u32* inner = twiddles_ + (size_ >> 1);\nfor (cZ j = 0; j < quarter; j += 8) {\nconst V outer0 = _mm256_loadu_si256(\n(const V*)(outer + j));\nconst V outer1 = _mm256_loadu_si256(\n(const V*)(outer + quarter + j));\nconst V inner_weight = _mm256_loadu_si256(\n(const V*)(inner + j));\nconst auto process = [&](u32* target) {\nconst V a = _mm256_loadu_si256(\n(const V*)(target + j));\nconst V b = _mm256_loadu_si256(\n(const V*)(target + quarter + j));\nconst V first = montgomery_multiply_lazy(\na, outer0, montgomery_);\nconst V second_value = montgomery_multiply_lazy(\nb, outer1, montgomery_);\n_mm256_storeu_si256(\n(V*)(target + j), add_lazy(a, b));\n_mm256_storeu_si256(\n(V*)(target + quarter + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(a, b), inner_weight, montgomery_));\n_mm256_storeu_si256(\n(V*)(target + 2 * quarter + j),\nadd_lazy(first, second_value));\n_mm256_storeu_si256(\n(V*)(target + 3 * quarter + j),\nmontgomery_multiply_lazy(\nsubtract_lazy(first, second_value),\ninner_weight, montgomery_));\n};\nprocess(data);\nif (second != nullptr) process(second);\n}\n}\nvoid forward_single_half_zero(u32* data, u32* second = nullptr) const {\nconst cZ half = size_ >> 1;\nfor (cZ j = 0; j < half; j += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(data + j));\nconst V weight = _mm256_loadu_si256(\n(const V*)(twiddles_ + j));\n_mm256_storeu_si256(\n(V*)(data + half + j),\nmontgomery_multiply_lazy(value, weight, montgomery_));\nif (second != nullptr) {\nconst V second_value = _mm256_loadu_si256(\n(const V*)(second + j));\n_mm256_storeu_si256(\n(V*)(second + half + j),\nmontgomery_multiply_lazy(\nsecond_value, weight, montgomery_));\n}\n}\n}\nvoid forward_bottom8(u32* data, u32* product = nullptr) const {\nconst u32* twiddle8 = twiddles_ + size_ - 8;\nconst u32* twiddle4 = twiddles_ + size_ - 4;\nconst __m128i w8_low = _mm_loadu_si128(\nreinterpret_cast(twiddle8));\nconst V w8 = _mm256_broadcastsi128_si256(w8_low);\nconst V w4 = _mm256_setr_epi32(\ntwiddle4[0], twiddle4[1], twiddle4[0], twiddle4[1],\ntwiddle4[0], twiddle4[1], twiddle4[0], twiddle4[1]);\nfor (cZ block = 0; block < size_; block += 8) {\nV value = shrink(shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block))));\nV other = _mm256_permute2x128_si256(value, value, 1);\nV sum = add_mod(value, other);\nV difference = montgomery_multiply(\nsubtract_mod(value, other), w8, montgomery_);\nvalue = _mm256_blend_epi32(\nsum, _mm256_permute2x128_si256(difference, difference, 1), 0xF0);\nother = _mm256_shuffle_epi32(value, 0x4E);\nsum = add_mod(value, other);\ndifference = montgomery_multiply(\nsubtract_mod(value, other), w4, montgomery_);\nvalue = _mm256_blend_epi32(\nsum, _mm256_shuffle_epi32(difference, 0x4E), 0xCC);\nother = _mm256_shuffle_epi32(value, 0xB1);\nsum = add_mod(value, other);\ndifference = subtract_mod(value, other);\nvalue = _mm256_blend_epi32(\nsum, _mm256_shuffle_epi32(difference, 0xB1), 0xAA);\nif (product == nullptr) {\n_mm256_storeu_si256(\n(V*)(data + block), value);\n} else {\nconst V other_transform = _mm256_loadu_si256(\n(const V*)(product + block));\n_mm256_storeu_si256(\n(V*)(product + block),\nmontgomery_multiply(\nother_transform, value, montgomery_));\n}\n}\n}\nvoid inverse_single(\nu32* data, cZ length, bool canonicalize = false) const {\nconst cZ half = length >> 1;\nconst u32* twiddle = twiddles_ + size_ - length;\nfor (cZ block = 0; block < size_; block += length) {\nfor (cZ j = 0; j < half; j += 8) {\nconst V left = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + j)));\nconst V right = montgomery_multiply_lazy(\nshrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + half + j))),\n_mm256_loadu_si256((const V*)(\ntwiddle + j)),\nmontgomery_);\nV sum = add_lazy(left, right);\nV difference = subtract_lazy(left, right);\nif (canonicalize) {\nsum = shrink(shrink_twice_modulus(sum));\ndifference = shrink(shrink_twice_modulus(difference));\n}\n_mm256_storeu_si256(\n(V*)(data + block + j), sum);\n_mm256_storeu_si256(\n(V*)(data + block + half + j),\ndifference);\n}\n}\n}\nvoid inverse_pair(u32* data, cZ length) const {\nconst cZ quarter = length >> 2;\nconst u32* outer = twiddles_ + size_ - length;\nconst u32* inner = twiddles_ + size_ - (length >> 1);\nfor (cZ block = 0; block < size_; block += length) {\nfor (cZ j = 0; j < quarter; j += 8) {\nconst V a = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + j)));\nconst V b = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + quarter + j)));\nconst V c = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + 2 * quarter + j)));\nconst V d = shrink_twice_modulus(_mm256_loadu_si256(\n(const V*)(data + block + 3 * quarter + j)));\nconst V inner_weight = _mm256_loadu_si256(\n(const V*)(inner + j));\nconst V outer0 = _mm256_loadu_si256(\n(const V*)(outer + j));\nconst V outer1 = _mm256_loadu_si256(\n(const V*)(outer + quarter + j));\nconst V bw = montgomery_multiply_lazy(b, inner_weight, montgomery_);\nconst V dw = montgomery_multiply_lazy(d, inner_weight, montgomery_);\nconst V ab_sum = shrink_twice_modulus(add_lazy(a, bw));\nconst V ab_difference = shrink_twice_modulus(subtract_lazy(a, bw));\nconst V cd_sum = add_lazy(c, dw);\nconst V cd_difference = subtract_lazy(c, dw);\nconst V cd_sum_weighted = montgomery_multiply_lazy(\ncd_sum, outer0, montgomery_);\nconst V cd_difference_weighted = montgomery_multiply_lazy(\ncd_difference, outer1, montgomery_);\nV output0 = add_lazy(ab_sum, cd_sum_weighted);\nV output1 = add_lazy(\nab_difference, cd_difference_weighted);\nV output2 = subtract_lazy(ab_sum, cd_sum_weighted);\nV output3 = subtract_lazy(\nab_difference, cd_difference_weighted);\nif (length == size_) {\noutput0 = shrink(shrink_twice_modulus(output0));\noutput1 = shrink(shrink_twice_modulus(output1));\noutput2 = shrink(shrink_twice_modulus(output2));\noutput3 = shrink(shrink_twice_modulus(output3));\n}\n_mm256_storeu_si256(\n(V*)(data + block + j), output0);\n_mm256_storeu_si256(\n(V*)(data + block + quarter + j),\noutput1);\n_mm256_storeu_si256(\n(V*)(data + block + 2 * quarter + j),\noutput2);\n_mm256_storeu_si256(\n(V*)(data + block + 3 * quarter + j),\noutput3);\n}\n}\n}\nvoid inverse_bottom8(u32* data) const {\nconst u32* twiddle4 = twiddles_ + size_ - 4;\nconst u32* twiddle8 = twiddles_ + size_ - 8;\nconst V w4 = _mm256_setr_epi32(\ntwiddle4[0], twiddle4[1], twiddle4[0], twiddle4[1],\ntwiddle4[0], twiddle4[1], twiddle4[0], twiddle4[1]);\nconst __m128i w8_low = _mm_loadu_si128(\nreinterpret_cast(twiddle8));\nconst V w8 = _mm256_broadcastsi128_si256(w8_low);\nfor (cZ block = 0; block < size_; block += 8) {\nV value = _mm256_loadu_si256(\n(const V*)(data + block));\nV other = _mm256_shuffle_epi32(value, 0xB1);\nV sum = add_mod(value, other);\nV difference = subtract_mod(value, other);\nvalue = _mm256_blend_epi32(\nsum, _mm256_shuffle_epi32(difference, 0xB1), 0xAA);\nother = _mm256_shuffle_epi32(value, 0x4E);\nother = montgomery_multiply(other, w4, montgomery_);\nsum = add_mod(value, other);\ndifference = subtract_mod(value, other);\nvalue = _mm256_blend_epi32(\nsum, _mm256_shuffle_epi32(difference, 0x4E), 0xCC);\nother = _mm256_permute2x128_si256(value, value, 1);\nother = montgomery_multiply(other, w8, montgomery_);\nsum = add_mod(value, other);\ndifference = subtract_mod(value, other);\nvalue = _mm256_blend_epi32(\nsum, _mm256_permute2x128_si256(difference, difference, 1), 0xF0);\n_mm256_storeu_si256(\n(V*)(data + block), value);\n}\n}\npublic:\nexplicit TransformPlan(cZ size)\n: size_(size),\ntwiddles_(static_cast(\n_mm_malloc(sizeof(u32) * size, 32))) {\nbuild_twiddles();\n}\n~TransformPlan() {\n_mm_free(twiddles_);\n}\nconst Montgomery& montgomery() const { return montgomery_; }\nvoid prepare_inverse() {\nfor (cZ length = size_;; length >>= 1) {\nconst cZ half = length >> 1;\nu32* stage = twiddles_ + size_ - length;\ncZ left = 1;\ncZ right = half - 1;\nwhile (left < right) {\nconst u32 a = stage[left];\nconst u32 b = stage[right];\nstage[left++] = modulus - b;\nstage[right--] = modulus - a;\n}\nif (left == right && left != 0) {\nstage[left] = modulus - stage[left];\n}\nif (length == 2) break;\n}\n}\nvoid forward(u32* data, u32* product = nullptr) const {\ncZ length = size_;\nif ((__builtin_ctzll(size_) & 1) != 0) {\nforward_single(data, length);\nlength >>= 1;\n}\nwhile (length > 16) {\nforward_pair(data, length);\nlength >>= 2;\n}\nforward_single(data, 16);\nforward_bottom8(data, product);\n}\nvoid forward_half_zero(u32* data, u32* product = nullptr) const {\ncZ length;\nif ((__builtin_ctzll(size_) & 1) == 0) {\nforward_pair_half_zero(data);\nlength = size_ >> 2;\n} else {\nforward_single_half_zero(data);\nlength = size_ >> 1;\n}\nwhile (length > 16) {\nforward_pair(data, length);\nlength >>= 2;\n}\nforward_single(data, 16);\nforward_bottom8(data, product);\n}\nvoid inverse(u32* data) const {\ninverse_bottom8(data);\ninverse_single(data, 16);\nconst bool has_unpaired_top = (__builtin_ctzll(size_) & 1) != 0;\nconst cZ paired_limit = has_unpaired_top ? size_ >> 1 : size_;\nfor (cZ length = 64; length <= paired_limit; length <<= 2) {\ninverse_pair(data, length);\n}\nif (has_unpaired_top) inverse_single(data, size_, true);\n}\n};\ninline void convolution_ntt_friendly(\nu32* output,\nconst u32* left,\ncZ left_size,\nconst u32* right,\ncZ right_size,\ncZ transform_size,\nu32 modulus_value,\nu32 primitive_root_value,\nbool montgomery_representation) {\nmodulus = modulus_value;\nprimitive_root = primitive_root_value != 0\n? primitive_root_value : find_primitive_root(modulus_value);\nu32* a = output;\nu32* b = static_cast(\n_mm_malloc(sizeof(u32) * transform_size, 32));\nstd::memcpy(a, left, sizeof(u32) * left_size);\nstd::memcpy(b, right, sizeof(u32) * right_size);\nTransformPlan plan(transform_size);\nconst Montgomery& montgomery = plan.montgomery();\nif (montgomery_representation) {\nconst V one = _mm256_set1_epi32(1);\ncZ i = 0;\nfor (; i + 8 <= left_size; i += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(a + i));\n_mm256_storeu_si256(\n(V*)(a + i),\nmontgomery_multiply(value, one, montgomery));\n}\nfor (; i < left_size; ++i) a[i] = montgomery.multiply(a[i], 1);\ni = 0;\nfor (; i + 8 <= right_size; i += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(b + i));\n_mm256_storeu_si256(\n(V*)(b + i),\nmontgomery_multiply(value, one, montgomery));\n}\nfor (; i < right_size; ++i) b[i] = montgomery.multiply(b[i], 1);\n}\nconst cZ half = transform_size >> 1;\nconst bool left_half_zero = left_size <= half;\nconst bool right_half_zero = right_size <= half;\nstd::memset(a + left_size, 0, sizeof(u32) *\n((left_half_zero ? half : transform_size) - left_size));\nstd::memset(b + right_size, 0, sizeof(u32) *\n((right_half_zero ? half : transform_size) - right_size));\nconst u32 inverse_size = power_mod(\n(u32)(transform_size % modulus), modulus - 2);\nconst u32 scaled_radix_squared = (u32)(\nu64(montgomery.radix_squared) * inverse_size % modulus);\nconst V conversion = _mm256_set1_epi32(\n(int)(scaled_radix_squared));\nconst cZ right_initialized = right_half_zero ? half : transform_size;\nfor (cZ i = 0; i < right_initialized; i += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(b + i));\n_mm256_storeu_si256(\n(V*)(b + i),\nmontgomery_multiply(value, conversion, montgomery));\n}\nif (left_half_zero) plan.forward_half_zero(a); else plan.forward(a);\nif (right_half_zero) {\nplan.forward_half_zero(b, a);\n} else {\nplan.forward(b, a);\n}\nplan.prepare_inverse();\nplan.inverse(a);\nif (montgomery_representation) {\nconst cZ output_size = left_size + right_size - 1;\nconst V radix_squared = _mm256_set1_epi32(\n(int)(montgomery.radix_squared));\ncZ i = 0;\nfor (; i + 8 <= output_size; i += 8) {\nconst V value = _mm256_loadu_si256(\n(const V*)(a + i));\n_mm256_storeu_si256(\n(V*)(a + i),\nmontgomery_multiply(value, radix_squared, montgomery));\n}\nfor (; i < output_size; ++i) {\na[i] = montgomery.to_montgomery(a[i]);\n}\n}\n_mm_free(b);\n}\n} \n#endif\nextern \"C\" void cplib_convolution_ntt_friendly(\nstd::uint32_t* output,\nstd::uint32_t* left,\ncZ left_size,\nstd::uint32_t* right,\ncZ right_size,\ncZ transform_size,\nstd::uint32_t modulus,\nstd::uint32_t primitive_root,\nbool montgomery_representation) {\ncplib_avx2_ntt::convolution_ntt_friendly(\noutput, left, left_size, right, right_size, transform_size,\nmodulus, primitive_root, montgomery_representation);\n}\n \"\"\".}\n\n proc convolutionNttFriendlyAvx2(\n output: ptr uint32,\n f: ptr uint32,\n fLen: csize_t,\n g: ptr uint32,\n gLen: csize_t,\n nttLen: csize_t,\n modulus: uint32,\n primitiveRoot: uint32,\n montgomeryRepresentation: bool\n ) {.importc: \"cplib_convolution_ntt_friendly\".}\n\n proc convolutionNttFriendlyU32(\n f, g: seq[uint32], modulus, primitiveRoot: uint32\n ): seq[uint32]\n\n proc convolutionArbitraryMod[T: BarrettModint or MontgomeryModint](\n f, g: seq[T]\n ): seq[T]\n\n proc convolution_naive*[T: BarrettModint or MontgomeryModint or int](f, g: seq[T]): seq[T] =\n if f.len == 0 or g.len == 0: return @[]\n var ans = newSeq[T](f.len + g.len - 1)\n if f.len > g.len:\n for i in 0.. 0 and m < (1 shl 31),\n \"convolution modulus must be in [1, 2^31)\"\n if f.len == 0 or g.len == 0: return @[]\n type Mint = StaticBarrettModint[m.uint32]\n var fm = newSeq[Mint](f.len)\n var gm = newSeq[Mint](g.len)\n for i in 0.. g.len:\n for i in 0.. 0 and targetMod < (1u64 shl 31),\n \"arbitrary-mod convolution requires a modulus in [1, 2^31)\"\n let transformSize = 1 shl (fastLog2(f.len + g.len - 2) + 1)\n assert transformSize <= (1 shl 24),\n \"arbitrary-mod convolution requires an NTT length at most 2^24\"\n\n var fm = newSeq[uint32](f.len)\n var gm = newSeq[uint32](g.len)\n for i in 0..= m and chmin(result[1], sz[u]): result[0] = u; result[1] = sz[u] var (c, _) = find_centroid(x, -1) var f = newSeq[seq[int]]() var getas = newSeq[int]() for v in g[c]: if removed[v]: continue proc calc_geta(u, par, x: int): int = result = x for v in g[u]: if v == par or removed[v]: continue result.min = calc_geta(v, u, x + s[v]) var geta = max(0, -calc_geta(v, c, s[v])) f.add(newSeqWith(geta+sz[v]+1, 0)) getas.add(geta) proc calc_f(u, par, x: int) = f[^1][x+geta] += 1 for v in g[u]: if v == par or removed[v]: continue calc_f(v, u, x + s[v]) calc_f(v, c, s[v]) var gmax = (if getas.len > 0: getas.max else: 0) var total = newSeqWith(gmax+sz[x], 0) for i in 0..