結果
| 問題 | No.3677 Global Checksum |
| コンテスト | |
| ユーザー |
kemuniku
|
| 提出日時 | 2026-09-04 23:14:04 |
| 言語 | Nim (2.2.10) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 21,979 bytes |
| 記録 | |
| コンパイル時間 | 4,208 ms |
| コンパイル使用メモリ | 77,952 KB |
| 実行使用メモリ | 97,152 KB |
| 最終ジャッジ日時 | 2026-09-04 23:14:14 |
| 合計ジャッジ時間 | 5,808 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 2 |
| other | AC * 4 WA * 8 TLE * 1 -- * 7 |
ソースコード
import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2])
# source: src/cplib/tmpl/sheep.nim
ImportExpand "cplib/tmpl/sheep" <=== "when not declared CPLIB_TMPL_SHEEP:\n const CPLIB_TMPL_SHEEP* = 1\n {.warning[UnusedImport]: off.}\n {.hint[XDeclaredButNotUsed]: off.}\n import algorithm\n import sequtils\n import tables\n import macros\n import math\n import sets\n import strutils\n import strformat\n import sugar\n import heapqueue\n import streams\n import deques\n import bitops\n import std/lenientops\n import options\n #入力系\n {.emit: \"\"\"\n #include <cstdio>\n #include <cstdint>\n #include <cstring>\n #include <sys/mman.h>\n #include <sys/stat.h>\n\n namespace cplib_sheep_input {\n constexpr std::size_t buffer_size = 1U << 20;\n char buffer[buffer_size];\n std::size_t cursor = 0;\n std::size_t length = 0;\n const char* mapped = nullptr;\n bool initialized = false;\n\n inline void initialize() {\n if (initialized) return;\n initialized = true;\n\n struct stat st;\n const int fd = fileno(stdin);\n if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && st.st_size > 0) {\n void* p = mmap(nullptr, static_cast<std::size_t>(st.st_size),\n PROT_READ, MAP_PRIVATE, fd, 0);\n if (p != MAP_FAILED) {\n mapped = static_cast<const char*>(p);\n length = static_cast<std::size_t>(st.st_size);\n madvise(const_cast<char*>(mapped), length, MADV_SEQUENTIAL);\n }\n }\n }\n\n inline int get_char() {\n initialize();\n if (mapped != nullptr) {\n if (cursor == length) return -1;\n return static_cast<unsigned char>(mapped[cursor++]);\n }\n\n if (cursor == length) {\n length = fread_unlocked(buffer, 1, buffer_size, stdin);\n cursor = 0;\n if (length == 0) return -1;\n }\n return static_cast<unsigned char>(buffer[cursor++]);\n }\n\n inline bool refill() {\n length = fread_unlocked(buffer, 1, buffer_size, stdin);\n cursor = 0;\n return length != 0;\n }\n\n inline bool has_eight_digits(const char* source) {\n std::uint64_t bytes;\n std::memcpy(&bytes, source, sizeof(bytes));\n constexpr std::uint64_t high_nibbles = 0xf0f0f0f0f0f0f0f0ULL;\n return (bytes & high_nibbles) == 0x3030303030303030ULL &&\n ((bytes + 0x0606060606060606ULL) & high_nibbles) ==\n 0x3030303030303030ULL;\n }\n\n inline unsigned parse_eight_digits(const char* source) {\n#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__\n std::uint64_t digits;\n std::memcpy(&digits, source, sizeof(digits));\n digits -= 0x3030303030303030ULL;\n digits = (digits * 10 + (digits >> 8)) & 0x00ff00ff00ff00ffULL;\n digits = (digits * 100 + (digits >> 16)) & 0x0000ffff0000ffffULL;\n return static_cast<unsigned>(\n (digits * 10000 + (digits >> 32)) & 0xffffffffULL);\n#else\n unsigned result = 0;\n for (int i = 0; i < 8; ++i) {\n result = result * 10U + static_cast<unsigned>(source[i] - '0');\n }\n return result;\n#endif\n }\n\n inline long long read_int() {\n initialize();\n\n if (mapped != nullptr) {\n while (cursor < length && mapped[cursor] <= ' ') ++cursor;\n if (cursor == length) return 0;\n\n const bool negative = mapped[cursor] == '-';\n if (negative) {\n ++cursor;\n if (cursor == length) return 0;\n }\n\n if (!negative && length - cursor >= 9 &&\n has_eight_digits(mapped + cursor)) {\n const unsigned value = parse_eight_digits(mapped + cursor);\n const unsigned ninth = static_cast<unsigned>(mapped[cursor + 8] - '0');\n if (ninth >= 10U) {\n cursor += 8;\n return static_cast<long long>(value);\n }\n if (length - cursor >= 10 &&\n static_cast<unsigned>(mapped[cursor + 9] - '0') >= 10U) {\n cursor += 9;\n return static_cast<long long>(value * 10U + ninth);\n }\n }\n\n long long value = 0;\n if (negative) {\n while (length - cursor >= 2) {\n const unsigned first = static_cast<unsigned>(mapped[cursor] - '0');\n const unsigned second = static_cast<unsigned>(mapped[cursor + 1] - '0');\n if (first >= 10U || second >= 10U) break;\n value = value * 100 - static_cast<long long>(first * 10U + second);\n cursor += 2;\n }\n if (cursor < length) {\n const unsigned digit = static_cast<unsigned>(mapped[cursor] - '0');\n if (digit < 10U) {\n value = value * 10 - static_cast<long long>(digit);\n ++cursor;\n }\n }\n } else {\n while (length - cursor >= 2) {\n const unsigned first = static_cast<unsigned>(mapped[cursor] - '0');\n const unsigned second = static_cast<unsigned>(mapped[cursor + 1] - '0');\n if (first >= 10U || second >= 10U) break;\n value = value * 100 + static_cast<long long>(first * 10U + second);\n cursor += 2;\n }\n if (cursor < length) {\n const unsigned digit = static_cast<unsigned>(mapped[cursor] - '0');\n if (digit < 10U) {\n value = value * 10 + static_cast<long long>(digit);\n ++cursor;\n }\n }\n }\n return value;\n }\n\n for (;;) {\n if (cursor == length && !refill()) return 0;\n while (cursor < length && buffer[cursor] <= ' ') ++cursor;\n if (cursor < length) break;\n }\n\n const bool negative = buffer[cursor] == '-';\n if (negative) ++cursor;\n long long value = 0;\n\n for (;;) {\n if (!negative && length - cursor >= 9 &&\n has_eight_digits(buffer + cursor)) {\n const unsigned first_eight = parse_eight_digits(buffer + cursor);\n const unsigned ninth = static_cast<unsigned>(buffer[cursor + 8] - '0');\n if (ninth >= 10U) {\n cursor += 8;\n return static_cast<long long>(first_eight);\n }\n if (length - cursor >= 10 &&\n static_cast<unsigned>(buffer[cursor + 9] - '0') >= 10U) {\n cursor += 9;\n return static_cast<long long>(first_eight * 10U + ninth);\n }\n }\n\n while (length - cursor >= 2) {\n const unsigned first = static_cast<unsigned>(buffer[cursor] - '0');\n const unsigned second = static_cast<unsigned>(buffer[cursor + 1] - '0');\n if (first >= 10U) return value;\n if (second >= 10U) {\n value = negative\n ? value * 10 - static_cast<long long>(first)\n : value * 10 + static_cast<long long>(first);\n ++cursor;\n return value;\n }\n value = negative\n ? value * 100 - static_cast<long long>(first * 10U + second)\n : value * 100 + static_cast<long long>(first * 10U + second);\n cursor += 2;\n }\n\n if (cursor < length) {\n const unsigned digit = static_cast<unsigned>(buffer[cursor] - '0');\n if (digit >= 10U) return value;\n value = negative\n ? value * 10 - static_cast<long long>(digit)\n : value * 10 + static_cast<long long>(digit);\n ++cursor;\n }\n if (!refill()) return value;\n }\n }\n\n template <class T>\n inline void read_int_array(T* output, std::size_t count) {\n for (std::size_t i = 0; i < count; ++i) {\n output[i] = static_cast<T>(read_int());\n }\n }\n } // namespace cplib_sheep_input\n \"\"\".}\n\n proc sheepGetChar(): cint {.importcpp: \"cplib_sheep_input::get_char()\", nodecl, inline.}\n proc sheepReadInt(): clonglong {.importcpp: \"cplib_sheep_input::read_int()\", nodecl, inline.}\n proc sheepReadIntArray(values: ptr int, count: csize_t) {.importcpp: \"cplib_sheep_input::read_int_array(@)\", nodecl, inline.}\n\n proc ii(): int {.inline.} = sheepReadInt().int\n proc lii(N: int): seq[int] {.inline.} =\n result = newSeq[int](N)\n if N > 0:\n sheepReadIntArray(addr result[0], N.csize_t)\n\n proc si(): string {.inline.} =\n var c = sheepGetChar()\n while c >= 0 and c <= ord(' '):\n c = sheepGetChar()\n while c > ord(' '):\n result.add(char(c))\n c = sheepGetChar()\n \n # 出力系\n {.emit: \"\"\"\n #include <cstddef>\n #include <cstdint>\n #include <cstring>\n #include <type_traits>\n\n namespace cplib_sheep_output {\n struct FourDigits {\n char data[10000][4];\n FourDigits() {\n for (unsigned i = 0; i < 10000; ++i) {\n data[i][0] = static_cast<char>('0' + i / 1000);\n data[i][1] = static_cast<char>('0' + i / 100 % 10);\n data[i][2] = static_cast<char>('0' + i / 10 % 10);\n data[i][3] = static_cast<char>('0' + i % 10);\n }\n }\n };\n\n inline const FourDigits& four_digits() {\n static const FourDigits table;\n return table;\n }\n\n inline char* write_small(char* output, unsigned value,\n const FourDigits& table) {\n if (value >= 1000) {\n std::memcpy(output, table.data[value], 4);\n return output + 4;\n }\n if (value >= 100) {\n std::memcpy(output, table.data[value] + 1, 3);\n return output + 3;\n }\n if (value >= 10) {\n std::memcpy(output, table.data[value] + 2, 2);\n return output + 2;\n }\n *output++ = static_cast<char>('0' + value);\n return output;\n }\n\n template <class Unsigned>\n inline char* write_unsigned(char* output, Unsigned value,\n const FourDigits& table) {\n unsigned chunks[5];\n unsigned count = 0;\n while (value >= 10000) {\n const Unsigned quotient = value / 10000;\n chunks[count++] = static_cast<unsigned>(value - quotient * 10000);\n value = quotient;\n }\n output = write_small(output, static_cast<unsigned>(value), table);\n while (count != 0) {\n std::memcpy(output, table.data[chunks[--count]], 4);\n output += 4;\n }\n return output;\n }\n\n template <class Integer>\n inline std::size_t join_signed(\n const Integer* values, std::size_t count, char* output,\n const char* separator, std::size_t separator_length) {\n const FourDigits& table = four_digits();\n char* cursor = output;\n using Unsigned = typename std::make_unsigned<Integer>::type;\n for (std::size_t i = 0; i < count; ++i) {\n const Integer value = values[i];\n Unsigned magnitude = static_cast<Unsigned>(value);\n if (value < 0) {\n *cursor++ = '-';\n magnitude = Unsigned(0) - magnitude;\n }\n cursor = write_unsigned(cursor, magnitude, table);\n if (i + 1 != count) {\n std::memcpy(cursor, separator, separator_length);\n cursor += separator_length;\n }\n }\n return static_cast<std::size_t>(cursor - output);\n }\n\n template <class Integer>\n inline std::size_t join_unsigned(\n const Integer* values, std::size_t count, char* output,\n const char* separator, std::size_t separator_length) {\n const FourDigits& table = four_digits();\n char* cursor = output;\n for (std::size_t i = 0; i < count; ++i) {\n cursor = write_unsigned(cursor, values[i], table);\n if (i + 1 != count) {\n std::memcpy(cursor, separator, separator_length);\n cursor += separator_length;\n }\n }\n return static_cast<std::size_t>(cursor - output);\n }\n } // namespace cplib_sheep_output\n \"\"\".}\n\n proc sheepJoinI32(values: ptr int32, count: csize_t, output: ptr char,\n separator: cstring, separatorLen: csize_t): csize_t\n {.importcpp: \"cplib_sheep_output::join_signed(@)\", nodecl.}\n proc sheepJoinI64(values: ptr int64, count: csize_t, output: ptr char,\n separator: cstring, separatorLen: csize_t): csize_t\n {.importcpp: \"cplib_sheep_output::join_signed(@)\", nodecl.}\n proc sheepJoinU32(values: ptr uint32, count: csize_t, output: ptr char,\n separator: cstring, separatorLen: csize_t): csize_t\n {.importcpp: \"cplib_sheep_output::join_unsigned(@)\", nodecl.}\n proc sheepJoinU64(values: ptr uint64, count: csize_t, output: ptr char,\n separator: cstring, separatorLen: csize_t): csize_t\n {.importcpp: \"cplib_sheep_output::join_unsigned(@)\", nodecl.}\n\n # 1. 実際の処理を行う proc (openArray を受け取る)\n proc print_internal(prop: tuple[f: File, sepc: string, endc: string, flush: bool], args: openArray[string]) =\n for i in 0 ..< args.len:\n prop.f.write(args[i])\n if i != args.len - 1:\n prop.f.write(prop.sepc)\n else:\n prop.f.write(prop.endc)\n if prop.flush:\n prop.f.flushFile()\n\n # 2. ユーザーが呼び出すためのインターフェース (varargs を受け取る)\n proc print*(prop: tuple[f: File, sepc: string, endc: string, flush: bool], args: varargs[string, `$`]) =\n # varargs は内部では openArray として扱えるので、そのまま渡せる\n print_internal(prop, args)\n\n proc print*(args: varargs[string, `$`]) =\n # こちらも内部用の proc を呼ぶ\n print_internal((f: stdout, sepc: \" \", endc: \"\\n\", flush: false), args)\n\n proc sheepPrintWithSeparator(sep: string,\n args: varargs[string, `$`]) =\n print_internal((f: stdout, sepc: sep, endc: \"\\n\", flush: false), args)\n\n macro getSymbolName(x: typed): string = x.toStrLit\n macro debug*(args: varargs[untyped]): untyped =\n when defined(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 #chmin,chmax\n template `max=`(x, y) =\n let yVal = y # yが計算式の場合に評価を1回にするため\n if x < yVal:\n x = yVal\n\n template `min=`(x, y) =\n let yVal = y\n if x > yVal:\n x = yVal\n proc chmin[T](x: var T, y: T):bool=\n if x > y:\n x = y\n return true\n return false\n proc chmax[T](x: var T, y: T):bool=\n if x < y:\n x = y\n return true\n return false\n #bit演算\n proc `%`*(x: int, y: int): 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: int, y: int): int{.inline.} =\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: var int, y: int): void = x = x%y\n proc `//=`(x: var int, y: int): void = x = x//y\n proc `**`(x: int, y: int): int = x^y\n proc `**=`(x: var int, y: int): void = x = x^y\n proc `^`(x: int, y: int): int = x xor y\n proc `|`(x: int, y: int): int = x or y\n proc `&`(x: int, y: int): int = x and y\n proc `>>`(x: int, y: int): int = x shr y\n proc `<<`(x: int, y: int): int = x shl y\n proc `~`(x: int): int = not x\n proc `^=`(x: var int, y: int): void = x = x ^ y\n proc `&=`(x: var int, y: int): void = x = x & y\n proc `|=`(x: var int, y: int): void = x = x | y\n proc `>>=`(x: var int, y: int): void = x = x >> y\n proc `<<=`(x: var int, y: int): void = x = x << y\n proc `[]`(x: int, n: int): bool = (x and (1 shl n)) != 0\n #便利な変換\n proc `!`(x: char, a = '0'): int = int(x)-int(a)\n #定数\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 INF = INF64\n #converter\n\n #range\n iterator range(start: int, ends: int, step: int): int =\n var i = start\n if step < 0:\n while i > ends:\n yield i\n i += step\n elif step > 0:\n while i < ends:\n yield i\n i += step\n iterator range(ends: int): int = (for i in 0..<ends: yield i)\n iterator range(start: int, ends: int): int = (for i in\n start..<ends: yield i)\n\n # 整数は4桁テーブルを使うC++フォーマッタへまとめて渡す。\n proc sheepJoinImpl[T](a: openArray[T], sep: string): string =\n if a.len == 0:\n return \"\"\n when T is SomeSignedInt and sizeof(T) == 8:\n result = newString(a.len * 20 + (a.len - 1) * sep.len)\n let written = sheepJoinI64(cast[ptr int64](unsafeAddr a[0]),\n a.len.csize_t, addr result[0], sep.cstring, sep.len.csize_t)\n result.setLen(written.int)\n elif T is SomeSignedInt and sizeof(T) == 4:\n result = newString(a.len * 11 + (a.len - 1) * sep.len)\n let written = sheepJoinI32(cast[ptr int32](unsafeAddr a[0]),\n a.len.csize_t, addr result[0], sep.cstring, sep.len.csize_t)\n result.setLen(written.int)\n elif T is SomeUnsignedInt and sizeof(T) == 8:\n result = newString(a.len * 20 + (a.len - 1) * sep.len)\n let written = sheepJoinU64(cast[ptr uint64](unsafeAddr a[0]),\n a.len.csize_t, addr result[0], sep.cstring, sep.len.csize_t)\n result.setLen(written.int)\n elif T is SomeUnsignedInt and sizeof(T) == 4:\n result = newString(a.len * 10 + (a.len - 1) * sep.len)\n let written = sheepJoinU32(cast[ptr uint32](unsafeAddr a[0]),\n a.len.csize_t, addr result[0], sep.cstring, sep.len.csize_t)\n result.setLen(written.int)\n elif compiles(a[0].umod) and compiles(a[0].val):\n # Montgomery表現を含め、公開値へ正規化してから一括変換する。\n var canonical = newSeq[uint32](a.len)\n for i, value in a:\n canonical[i] = value.val.uint32\n result = sheepJoinImpl(canonical, sep)\n else:\n result = newStringOfCap(a.len * 4)\n for i, value in a:\n if i != 0:\n result.add(sep)\n result.add($value)\n\n proc join*[T: not string](a: openArray[T], sep: string = \"\"): string {.inline.} =\n sheepJoinImpl(a, sep)\n\n # Python風に print(*X) と書くと、Xを空白区切りで1行に出力する。\n template `*`*[T](values: openArray[T]): string =\n sheepJoinImpl(values, \" \")\n\n # 最後の文字列引数をsepと誤認しないよう、名前付きsepはマクロで処理する。\n macro print*(args: varargs[untyped]): untyped =\n var sep = newLit(\" \")\n var hasSep = false\n var values: seq[NimNode]\n for arg in args:\n if arg.kind == nnkExprEqExpr and arg[0].eqIdent(\"sep\"):\n if hasSep:\n error(\"sep can only be specified once\", arg)\n sep = arg[1]\n hasSep = true\n else:\n values.add(arg)\n var splatValues: NimNode\n if values.len == 1:\n if values[0].kind == nnkPrefix and values[0][0].eqIdent(\"*\"):\n splatValues = values[0][1]\n elif values[0].kind in nnkCallKinds and values[0].len == 3 and\n values[0][0].eqIdent(\"sheepJoinImpl\"):\n # オーバーロード解決時に *values が先に展開された場合。\n splatValues = values[0][1]\n if not splatValues.isNil:\n let joined = newCall(bindSym\"sheepJoinImpl\", splatValues, sep)\n result = newCall(bindSym\"sheepPrintWithSeparator\", newLit(\" \"), joined)\n else:\n result = newCall(bindSym\"sheepPrintWithSeparator\", sep)\n for value in values:\n result.add(value)\n\n proc dump[T](arr:seq[seq[T]])=\n for i in 0..<len(arr):\n echo arr[i]\n\n proc sum(slice:HSlice[int,int]):int=\n return (slice.a+slice.b)*len(slice)//2\n \n proc `<`[T](l,r:seq[T]):bool=\n for i in 0..<min(len(l),len(r)):\n if l[i] > r[i]:\n return false\n elif l[i] < r[i]:\n return true\n return len(l) < len(r)\n \n # Yes/No\n proc yes*(b: bool = true): void = print(if b: \"Yes\" else: \"No\")\n\n template dblock(body: untyped) =\n when defined(debug):\n block:\n body\n"
var H,W = ii()
var A = newseqwith(H,lii(W))
var S = newseqwith(H,uint(0))
for i in range(H):
for j in range(W):
S[i] += A[i][j].uint()
var T = S.sum()
S.applyit(it + T)
print(S.join("\n"))
kemuniku