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 when not declared CPLIB_TMPL_FASTIO:\n const CPLIB_TMPL_FASTIO* = 1\n import macros\n \n # 入力系\n {.emit: \"\"\"\n #include \n #include \n #include \n #include \n #include \n #include \n #include \n \n namespace cplib_fastio_input {\n constexpr std::size_t buffer_size = 1U << 20;\n constexpr std::size_t safe_integer_bytes = 32;\n \n struct InputState {\n alignas(64) char buffer[buffer_size];\n std::size_t cursor;\n std::size_t length;\n const char* mapped;\n bool initialized;\n };\n \n inline InputState& input_state() {\n static InputState state = {};\n return state;\n }\n \n inline std::string& token_storage() {\n static std::string storage;\n return storage;\n }\n \n #if defined(__GNUC__) || defined(__clang__)\n #define CPLIB_FASTIO_ALWAYS_INLINE inline __attribute__((always_inline))\n #define CPLIB_FASTIO_NIM_ALWAYS_INLINE \\\n static inline __attribute__((always_inline))\n #define CPLIB_FASTIO_UNLIKELY(condition) (__builtin_expect(!!(condition), 0))\n #elif defined(_MSC_VER)\n #define CPLIB_FASTIO_ALWAYS_INLINE __forceinline\n #define CPLIB_FASTIO_NIM_ALWAYS_INLINE static __forceinline\n #define CPLIB_FASTIO_UNLIKELY(condition) (condition)\n #else\n #define CPLIB_FASTIO_ALWAYS_INLINE inline\n #define CPLIB_FASTIO_NIM_ALWAYS_INLINE static inline\n #define CPLIB_FASTIO_UNLIKELY(condition) (condition)\n #endif\n \n inline void initialize(InputState& state) {\n if (state.initialized) return;\n state.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(st.st_size),\n PROT_READ, MAP_PRIVATE, fd, 0);\n if (p != MAP_FAILED) {\n state.mapped = static_cast(p);\n state.length = static_cast(st.st_size);\n madvise(const_cast(state.mapped), state.length,\n MADV_SEQUENTIAL);\n }\n }\n }\n \n inline bool refill(InputState& state) {\n state.length =\n fread_unlocked(state.buffer, 1, buffer_size, stdin);\n state.cursor = 0;\n return state.length != 0;\n }\n \n inline int get_char() {\n InputState& state = input_state();\n if (CPLIB_FASTIO_UNLIKELY(!state.initialized)) initialize(state);\n if (state.mapped != nullptr) {\n if (state.cursor == state.length) return -1;\n return static_cast(state.mapped[state.cursor++]);\n }\n if (state.cursor == state.length && !refill(state)) return -1;\n return static_cast(state.buffer[state.cursor++]);\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(\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(source[i] - '0');\n }\n return result;\n #endif\n }\n \n inline unsigned digit_at(const char* source) {\n return static_cast(static_cast(*source)) -\n static_cast('0');\n }\n \n template \n CPLIB_FASTIO_ALWAYS_INLINE bool try_parse_digits_unchecked(\n const char*& source, long long& output) {\n long long value = 0;\n if (has_eight_digits(source)) {\n const long long digits =\n static_cast(parse_eight_digits(source));\n value = negative ? value * 100000000LL - digits\n : value * 100000000LL + digits;\n source += 8;\n if (has_eight_digits(source)) {\n const long long next_digits =\n static_cast(parse_eight_digits(source));\n value = negative ? value * 100000000LL - next_digits\n : value * 100000000LL + next_digits;\n source += 8;\n for (int pair = 0; pair < 2; ++pair) {\n const unsigned first = digit_at(source);\n if (first >= 10U) {\n output = value;\n return true;\n }\n const unsigned second = digit_at(source + 1);\n if (second >= 10U) {\n ++source;\n output = negative\n ? value * 10 - static_cast(first)\n : value * 10 + static_cast(first);\n return true;\n }\n value = negative\n ? value * 100 - static_cast(first * 10U + second)\n : value * 100 + static_cast(first * 10U + second);\n source += 2;\n }\n if (digit_at(source) < 10U) return false;\n output = value;\n return true;\n }\n }\n for (;;) {\n const unsigned first = digit_at(source);\n if (first >= 10U) {\n output = value;\n return true;\n }\n const unsigned second = digit_at(source + 1);\n if (second >= 10U) {\n ++source;\n output = negative ? value * 10 - static_cast(first)\n : value * 10 + static_cast(first);\n return true;\n }\n value = negative\n ? value * 100 - static_cast(first * 10U + second)\n : value * 100 + static_cast(first * 10U + second);\n source += 2;\n }\n }\n \n template \n inline long long parse_digits_bounded(const char*& source,\n const char* end) {\n long long value = 0;\n while (end - source >= 8 && has_eight_digits(source)) {\n const long long digits =\n static_cast(parse_eight_digits(source));\n value = negative ? value * 100000000LL - digits\n : value * 100000000LL + digits;\n source += 8;\n }\n while (end - source >= 2) {\n const unsigned first = digit_at(source);\n const unsigned second = digit_at(source + 1);\n if (first >= 10U) return value;\n if (second >= 10U) {\n ++source;\n return negative ? value * 10 - static_cast(first)\n : value * 10 + static_cast(first);\n }\n value = negative\n ? value * 100 - static_cast(first * 10U + second)\n : value * 100 + static_cast(first * 10U + second);\n source += 2;\n }\n if (source != end) {\n const unsigned digit = digit_at(source);\n if (digit < 10U) {\n value = negative ? value * 10 - static_cast(digit)\n : value * 10 + static_cast(digit);\n ++source;\n }\n }\n return value;\n }\n \n CPLIB_FASTIO_ALWAYS_INLINE bool try_parse_unsigned_digits_unchecked(\n const char*& source, unsigned long long& output) {\n unsigned long long value = 0;\n if (has_eight_digits(source)) {\n value = static_cast(parse_eight_digits(source));\n source += 8;\n if (has_eight_digits(source)) {\n value = value * 100000000ULL +\n static_cast(parse_eight_digits(source));\n source += 8;\n for (int pair = 0; pair < 2; ++pair) {\n const unsigned first = digit_at(source);\n if (first >= 10U) {\n output = value;\n return true;\n }\n const unsigned second = digit_at(source + 1);\n if (second >= 10U) {\n ++source;\n output = value * 10ULL + first;\n return true;\n }\n value = value * 100ULL + first * 10U + second;\n source += 2;\n }\n if (digit_at(source) < 10U) return false;\n output = value;\n return true;\n }\n }\n for (;;) {\n const unsigned first = digit_at(source);\n if (first >= 10U) {\n output = value;\n return true;\n }\n const unsigned second = digit_at(source + 1);\n if (second >= 10U) {\n ++source;\n output = value * 10ULL + first;\n return true;\n }\n value = value * 100ULL + first * 10U + second;\n source += 2;\n }\n }\n \n inline unsigned long long parse_unsigned_digits_bounded(\n const char*& source, const char* end) {\n unsigned long long value = 0;\n while (end - source >= 8 && has_eight_digits(source)) {\n value = value * 100000000ULL +\n static_cast(parse_eight_digits(source));\n source += 8;\n }\n while (end - source >= 2) {\n const unsigned first = digit_at(source);\n const unsigned second = digit_at(source + 1);\n if (first >= 10U) return value;\n if (second >= 10U) {\n ++source;\n return value * 10ULL + first;\n }\n value = value * 100ULL + first * 10U + second;\n source += 2;\n }\n if (source != end) {\n const unsigned digit = digit_at(source);\n if (digit < 10U) {\n value = value * 10ULL + digit;\n ++source;\n }\n }\n return value;\n }\n \n CPLIB_FASTIO_ALWAYS_INLINE long long read_mapped_at(\n const char*& source, const char* end) {\n while (source != end &&\n static_cast(*source) <=\n static_cast(' ')) {\n ++source;\n }\n if (source == end) return 0;\n const bool negative = *source == '-';\n if (negative || *source == '+') ++source;\n if (end - source >= static_cast(safe_integer_bytes)) {\n const char* parsed = source;\n long long value;\n const bool complete = negative\n ? try_parse_digits_unchecked(parsed, value)\n : try_parse_digits_unchecked(parsed, value);\n if (complete) {\n source = parsed;\n return value;\n }\n }\n return negative ? parse_digits_bounded(source, end)\n : parse_digits_bounded(source, end);\n }\n \n CPLIB_FASTIO_ALWAYS_INLINE unsigned long long read_uint_mapped_at(\n const char*& source, const char* end) {\n while (source != end &&\n static_cast(*source) <=\n static_cast(' ')) {\n ++source;\n }\n if (source == end) return 0;\n const bool negative = *source == '-';\n if (negative || *source == '+') ++source;\n if (end - source >= static_cast(safe_integer_bytes)) {\n const char* parsed = source;\n unsigned long long value;\n if (try_parse_unsigned_digits_unchecked(parsed, value)) {\n source = parsed;\n return negative ? 0ULL - value : value;\n }\n }\n const unsigned long long value =\n parse_unsigned_digits_bounded(source, end);\n return negative ? 0ULL - value : value;\n }\n \n inline long long read_int_stream_slow(InputState& state) {\n bool negative = state.buffer[state.cursor] == '-';\n if (negative || state.buffer[state.cursor] == '+') {\n ++state.cursor;\n if (state.cursor == state.length && !refill(state)) return 0;\n }\n long long value = 0;\n for (;;) {\n while (state.cursor != state.length) {\n const unsigned digit = digit_at(state.buffer + state.cursor);\n if (digit >= 10U) return value;\n value = negative ? value * 10 - static_cast(digit)\n : value * 10 + static_cast(digit);\n ++state.cursor;\n }\n if (!refill(state)) return value;\n }\n }\n \n inline unsigned long long read_uint_stream_slow(InputState& state) {\n const bool negative = state.buffer[state.cursor] == '-';\n if (negative || state.buffer[state.cursor] == '+') {\n ++state.cursor;\n if (state.cursor == state.length && !refill(state)) return 0;\n }\n unsigned long long value = 0;\n for (;;) {\n while (state.cursor != state.length) {\n const unsigned digit = digit_at(state.buffer + state.cursor);\n if (digit >= 10U) return negative ? 0ULL - value : value;\n value = value * 10ULL + digit;\n ++state.cursor;\n }\n if (!refill(state)) return negative ? 0ULL - value : value;\n }\n }\n \n CPLIB_FASTIO_ALWAYS_INLINE long long read_int_stream(InputState& state) {\n for (;;) {\n if (state.cursor == state.length && !refill(state)) return 0;\n while (state.cursor != state.length &&\n static_cast(state.buffer[state.cursor]) <=\n static_cast(' ')) {\n ++state.cursor;\n }\n if (state.cursor != state.length) break;\n }\n if (state.length - state.cursor < safe_integer_bytes) {\n return read_int_stream_slow(state);\n }\n const char* source = state.buffer + state.cursor;\n const bool negative = *source == '-';\n if (negative || *source == '+') ++source;\n long long value;\n const bool complete = negative\n ? try_parse_digits_unchecked(source, value)\n : try_parse_digits_unchecked(source, value);\n if (!complete) return read_int_stream_slow(state);\n state.cursor = static_cast(source - state.buffer);\n return value;\n }\n \n CPLIB_FASTIO_ALWAYS_INLINE unsigned long long read_uint_stream(\n InputState& state) {\n for (;;) {\n if (state.cursor == state.length && !refill(state)) return 0;\n while (state.cursor != state.length &&\n static_cast(state.buffer[state.cursor]) <=\n static_cast(' ')) {\n ++state.cursor;\n }\n if (state.cursor != state.length) break;\n }\n if (state.length - state.cursor < safe_integer_bytes) {\n return read_uint_stream_slow(state);\n }\n const char* source = state.buffer + state.cursor;\n const bool negative = *source == '-';\n if (negative || *source == '+') ++source;\n unsigned long long value;\n if (!try_parse_unsigned_digits_unchecked(source, value)) {\n return read_uint_stream_slow(state);\n }\n state.cursor = static_cast(source - state.buffer);\n return negative ? 0ULL - value : value;\n }\n \n CPLIB_FASTIO_ALWAYS_INLINE long long read_int() {\n InputState& state = input_state();\n if (CPLIB_FASTIO_UNLIKELY(!state.initialized)) initialize(state);\n if (state.mapped == nullptr) return read_int_stream(state);\n const char* source = state.mapped + state.cursor;\n const long long value =\n read_mapped_at(source, state.mapped + state.length);\n state.cursor = static_cast(source - state.mapped);\n return value;\n }\n \n CPLIB_FASTIO_ALWAYS_INLINE unsigned long long read_uint() {\n InputState& state = input_state();\n if (CPLIB_FASTIO_UNLIKELY(!state.initialized)) initialize(state);\n if (state.mapped == nullptr) return read_uint_stream(state);\n const char* source = state.mapped + state.cursor;\n const unsigned long long value =\n read_uint_mapped_at(source, state.mapped + state.length);\n state.cursor = static_cast(source - state.mapped);\n return value;\n }\n \n template \n inline void read_int_array(T* output, std::size_t count) {\n InputState& state = input_state();\n if (CPLIB_FASTIO_UNLIKELY(!state.initialized)) initialize(state);\n if (state.mapped != nullptr) {\n const char* source = state.mapped + state.cursor;\n const char* const end = state.mapped + state.length;\n for (std::size_t i = 0; i < count; ++i) {\n output[i] = static_cast(read_mapped_at(source, end));\n }\n state.cursor = static_cast(source - state.mapped);\n } else {\n for (std::size_t i = 0; i < count; ++i) {\n output[i] = static_cast(read_int_stream(state));\n }\n }\n }\n \n template \n inline void read_uint_array(T* output, std::size_t count) {\n InputState& state = input_state();\n if (CPLIB_FASTIO_UNLIKELY(!state.initialized)) initialize(state);\n if (state.mapped != nullptr) {\n const char* source = state.mapped + state.cursor;\n const char* const end = state.mapped + state.length;\n for (std::size_t i = 0; i < count; ++i) {\n output[i] = static_cast(read_uint_mapped_at(source, end));\n }\n state.cursor = static_cast(source - state.mapped);\n } else {\n for (std::size_t i = 0; i < count; ++i) {\n output[i] = static_cast(read_uint_stream(state));\n }\n }\n }\n \n inline const char* read_token(std::size_t* output_length) {\n InputState& state = input_state();\n if (CPLIB_FASTIO_UNLIKELY(!state.initialized)) initialize(state);\n if (state.mapped != nullptr) {\n const char* source = state.mapped + state.cursor;\n const char* const end = state.mapped + state.length;\n while (source != end &&\n static_cast(*source) <=\n static_cast(' ')) {\n ++source;\n }\n const char* const token = source;\n while (source != end &&\n static_cast(*source) >\n static_cast(' ')) {\n ++source;\n }\n state.cursor = static_cast(source - state.mapped);\n *output_length = static_cast(source - token);\n return token;\n }\n \n for (;;) {\n if (state.cursor == state.length && !refill(state)) {\n *output_length = 0;\n return \"\";\n }\n while (state.cursor != state.length &&\n static_cast(state.buffer[state.cursor]) <=\n static_cast(' ')) {\n ++state.cursor;\n }\n if (state.cursor != state.length) break;\n }\n \n const std::size_t token_begin = state.cursor;\n while (state.cursor != state.length &&\n static_cast(state.buffer[state.cursor]) >\n static_cast(' ')) {\n ++state.cursor;\n }\n if (state.cursor != state.length) {\n *output_length = state.cursor - token_begin;\n return state.buffer + token_begin;\n }\n \n std::string& storage = token_storage();\n storage.assign(state.buffer + token_begin,\n state.length - token_begin);\n while (refill(state)) {\n while (state.cursor != state.length &&\n static_cast(state.buffer[state.cursor]) >\n static_cast(' ')) {\n ++state.cursor;\n }\n storage.append(state.buffer, state.cursor);\n if (state.cursor != state.length) break;\n }\n *output_length = storage.size();\n return storage.c_str();\n }\n \n #undef CPLIB_FASTIO_UNLIKELY\n #undef CPLIB_FASTIO_ALWAYS_INLINE\n } // namespace cplib_fastio_input\n \"\"\".}\n \n proc fastioGetChar(): cint {.importcpp: \"cplib_fastio_input::get_char()\", nodecl, inline.}\n proc fastioReadInt(): clonglong {.importcpp: \"cplib_fastio_input::read_int()\", nodecl, inline.}\n proc fastioReadUInt(): culonglong {.importcpp: \"cplib_fastio_input::read_uint()\", nodecl, inline.}\n proc fastioReadSignedArray[T: SomeSignedInt](values: ptr T, count: csize_t) {.importcpp: \"cplib_fastio_input::read_int_array(@)\", nodecl, inline.}\n proc fastioReadUnsignedArray[T: SomeUnsignedInt](values: ptr T, count: csize_t) {.importcpp: \"cplib_fastio_input::read_uint_array(@)\", nodecl, inline.}\n proc fastioReadToken(length: ptr csize_t): cstring\n {.importcpp: \"cplib_fastio_input::read_token(@)\", nodecl, inline.}\n \n when NimMajor >= 2:\n template fastioNewSeqUninit(T: typedesc, length: int): untyped =\n newSeqUninit[T](length)\n \n template fastioNewStringUninit(length: int): untyped =\n newStringUninit(length)\n else:\n template fastioNewSeqUninit(T: typedesc, length: int): untyped =\n newSeqUninitialized[T](length)\n \n template fastioNewStringUninit(length: int): untyped =\n newString(length)\n \n proc ii(): int {.inline,\n codegenDecl: \"CPLIB_FASTIO_NIM_ALWAYS_INLINE $# $#$#\".} =\n fastioReadInt().int\n proc lii(N: int): seq[int] {.inline.} =\n result = fastioNewSeqUninit(int, N)\n if N > 0:\n fastioReadSignedArray(addr result[0], N.csize_t)\n \n # 型だけなら1要素、長さと型ならseqとして読み込む。\n proc input*[T: SomeInteger](valueType: typedesc[T]): T {.inline,\n codegenDecl: \"CPLIB_FASTIO_NIM_ALWAYS_INLINE $# $#$#\".} =\n when T is SomeSignedInt:\n T(fastioReadInt())\n else:\n T(fastioReadUInt())\n \n proc input*[T: SomeInteger](N: int, valueType: typedesc[T]): seq[T] {.inline.} =\n result = fastioNewSeqUninit(T, N)\n if N > 0:\n when T is SomeSignedInt:\n fastioReadSignedArray(addr result[0], N.csize_t)\n else:\n fastioReadUnsignedArray(addr result[0], N.csize_t)\n \n proc si(): string {.inline.} =\n var length: csize_t\n let source = fastioReadToken(addr length)\n result = fastioNewStringUninit(length.int)\n if length != 0:\n copyMem(addr result[0], source, length.int)\n \n # 出力系\n {.emit: \"\"\"\n #include \n #include \n #include \n #include \n #include \n \n namespace cplib_fastio_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('0' + i / 1000);\n data[i][1] = static_cast('0' + i / 100 % 10);\n data[i][2] = static_cast('0' + i / 10 % 10);\n data[i][3] = static_cast('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('0' + value);\n return output;\n }\n \n template \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(value - quotient * 10000);\n value = quotient;\n }\n output = write_small(output, static_cast(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 \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::type;\n for (std::size_t i = 0; i < count; ++i) {\n const Integer value = values[i];\n Unsigned magnitude = static_cast(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(cursor - output);\n }\n \n template \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(cursor - output);\n }\n \n class BufferedWriter {\n public:\n static constexpr std::size_t capacity = 1U << 16;\n \n explicit BufferedWriter(std::FILE* output)\n : length_(0), output_(output) {}\n \n inline char* reserve_integer() {\n constexpr std::size_t max_integer_length = 21;\n if (capacity - length_ < max_integer_length) flush();\n return data_ + length_;\n }\n \n inline void commit(char* end) {\n length_ = static_cast(end - data_);\n }\n \n inline void append(const char* source, std::size_t size) {\n if (size <= capacity - length_) {\n std::memcpy(data_ + length_, source, size);\n length_ += size;\n return;\n }\n flush();\n if (size >= capacity) {\n fwrite_unlocked(source, 1, size, output_);\n } else {\n std::memcpy(data_, source, size);\n length_ = size;\n }\n }\n \n inline void flush() {\n if (length_ != 0) {\n fwrite_unlocked(data_, 1, length_, output_);\n length_ = 0;\n }\n }\n \n private:\n char data_[capacity];\n std::size_t length_;\n std::FILE* output_;\n };\n \n template \n inline void print_signed(std::FILE* output, const Integer* values,\n std::size_t count,\n const char* separator,\n std::size_t separator_length) {\n const FourDigits& table = four_digits();\n BufferedWriter writer(output);\n using Unsigned = typename std::make_unsigned::type;\n for (std::size_t i = 0; i < count; ++i) {\n char* cursor = writer.reserve_integer();\n const Integer value = values[i];\n Unsigned magnitude = static_cast(value);\n if (value < 0) {\n *cursor++ = '-';\n magnitude = Unsigned(0) - magnitude;\n }\n cursor = write_unsigned(cursor, magnitude, table);\n writer.commit(cursor);\n if (i + 1 != count) writer.append(separator, separator_length);\n }\n writer.append(\"\\n\", 1);\n writer.flush();\n }\n \n template \n inline void print_unsigned(std::FILE* output, const Integer* values,\n std::size_t count,\n const char* separator,\n std::size_t separator_length) {\n const FourDigits& table = four_digits();\n BufferedWriter writer(output);\n for (std::size_t i = 0; i < count; ++i) {\n char* cursor = writer.reserve_integer();\n cursor = write_unsigned(cursor, values[i], table);\n writer.commit(cursor);\n if (i + 1 != count) writer.append(separator, separator_length);\n }\n writer.append(\"\\n\", 1);\n writer.flush();\n }\n \n template \n inline void print_one_signed(std::FILE* output, Integer value) {\n const FourDigits& table = four_digits();\n char buffer[22];\n char* cursor = buffer;\n using Unsigned = typename std::make_unsigned::type;\n Unsigned magnitude = static_cast(value);\n if (value < 0) {\n *cursor++ = '-';\n magnitude = Unsigned(0) - magnitude;\n }\n cursor = write_unsigned(cursor, magnitude, table);\n *cursor++ = '\\n';\n fwrite_unlocked(buffer, 1,\n static_cast(cursor - buffer), output);\n }\n \n template \n inline void print_one_unsigned(std::FILE* output, Integer value) {\n const FourDigits& table = four_digits();\n char buffer[21];\n char* cursor = write_unsigned(buffer, value, table);\n *cursor++ = '\\n';\n fwrite_unlocked(buffer, 1,\n static_cast(cursor - buffer), output);\n }\n } // namespace cplib_fastio_output\n \"\"\".}\n \n proc fastioJoinI32(values: ptr int32, count: csize_t, output: ptr char,\n separator: cstring, separatorLen: csize_t): csize_t\n {.importcpp: \"cplib_fastio_output::join_signed(@)\", nodecl.}\n proc fastioJoinI64(values: ptr int64, count: csize_t, output: ptr char,\n separator: cstring, separatorLen: csize_t): csize_t\n {.importcpp: \"cplib_fastio_output::join_signed(@)\", nodecl.}\n proc fastioJoinU32(values: ptr uint32, count: csize_t, output: ptr char,\n separator: cstring, separatorLen: csize_t): csize_t\n {.importcpp: \"cplib_fastio_output::join_unsigned(@)\", nodecl.}\n proc fastioJoinU64(values: ptr uint64, count: csize_t, output: ptr char,\n separator: cstring, separatorLen: csize_t): csize_t\n {.importcpp: \"cplib_fastio_output::join_unsigned(@)\", nodecl.}\n proc fastioPrintI32(output: File, values: ptr int32, count: csize_t,\n separator: cstring, separatorLen: csize_t)\n {.importcpp: \"cplib_fastio_output::print_signed(@)\", nodecl.}\n proc fastioPrintI64(output: File, values: ptr int64, count: csize_t,\n separator: cstring, separatorLen: csize_t)\n {.importcpp: \"cplib_fastio_output::print_signed(@)\", nodecl.}\n proc fastioPrintU32(output: File, values: ptr uint32, count: csize_t,\n separator: cstring, separatorLen: csize_t)\n {.importcpp: \"cplib_fastio_output::print_unsigned(@)\", nodecl.}\n proc fastioPrintU64(output: File, values: ptr uint64, count: csize_t,\n separator: cstring, separatorLen: csize_t)\n {.importcpp: \"cplib_fastio_output::print_unsigned(@)\", nodecl.}\n proc fastioPrintOneI32(output: File, value: int32)\n {.importcpp: \"cplib_fastio_output::print_one_signed(@)\", nodecl.}\n proc fastioPrintOneI64(output: File, value: int64)\n {.importcpp: \"cplib_fastio_output::print_one_signed(@)\", nodecl.}\n proc fastioPrintOneU32(output: File, value: uint32)\n {.importcpp: \"cplib_fastio_output::print_one_unsigned(@)\", nodecl.}\n proc fastioPrintOneU64(output: File, value: uint64)\n {.importcpp: \"cplib_fastio_output::print_one_unsigned(@)\", nodecl.}\n \n proc print_internal(prop: tuple[f: File, sepc: string, endc: string,\n 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 proc print*(prop: tuple[f: File, sepc: string, endc: string, flush: bool],\n args: varargs[string, `$`]) =\n print_internal(prop, args)\n \n proc fastioPrintWithSeparator(sep: string, args: varargs[string, `$`]) =\n print_internal((f: stdout, sepc: sep, endc: \"\\n\", flush: false), args)\n \n proc fastioAppendInteger[T: SomeInteger](destination: var string, value: T) =\n var magnitude: uint64\n when T is SomeSignedInt:\n let signedValue = value.int64\n if signedValue < 0:\n destination.add('-')\n magnitude = uint64(-(signedValue + 1)) + 1'u64\n else:\n magnitude = signedValue.uint64\n else:\n magnitude = value.uint64\n \n if magnitude == 0:\n destination.add('0')\n return\n var digits: array[20, char]\n var count = 0\n while magnitude != 0:\n digits[count] = char(ord('0') + int(magnitude mod 10))\n magnitude = magnitude div 10\n inc count\n while count != 0:\n dec count\n destination.add(digits[count])\n \n # 整数は4桁テーブルを使うC++フォーマッタへまとめて渡す。\n proc fastioJoinImpl[T](a: openArray[T], sep: string): string =\n when nimvm:\n result = newStringOfCap(a.len * 4)\n for i, value in a:\n if i != 0:\n result.add(sep)\n when T is SomeInteger:\n fastioAppendInteger(result, value)\n else:\n result.add($value)\n else:\n if a.len == 0:\n return \"\"\n when T is SomeSignedInt and sizeof(T) == 8:\n result = fastioNewStringUninit(\n a.len * 20 + (a.len - 1) * sep.len)\n let written = fastioJoinI64(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 = fastioNewStringUninit(\n a.len * 11 + (a.len - 1) * sep.len)\n let written = fastioJoinI32(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 = fastioNewStringUninit(\n a.len * 20 + (a.len - 1) * sep.len)\n let written = fastioJoinU64(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 = fastioNewStringUninit(\n a.len * 10 + (a.len - 1) * sep.len)\n let written = fastioJoinU32(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(T.umod()) and compiles(a[0].val()):\n # Montgomery表現を含め、公開値へ正規化してから一括変換する。\n var canonical = fastioNewSeqUninit(uint32, a.len)\n for i, value in a:\n canonical[i] = value.val.uint32\n result = fastioJoinImpl(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 when T is SomeInteger:\n fastioAppendInteger(result, value)\n else:\n result.add($value)\n \n proc join*[T: not string](a: openArray[T], sep: string = \"\"): string {.inline.} =\n fastioJoinImpl(a, sep)\n \n proc fastioPrintArrayImpl[T](a: openArray[T], sep: string) =\n if a.len == 0:\n stdout.write('\\n')\n return\n when T is SomeSignedInt and sizeof(T) == 8:\n fastioPrintI64(stdout, cast[ptr int64](unsafeAddr a[0]), a.len.csize_t,\n sep.cstring, sep.len.csize_t)\n elif T is SomeSignedInt and sizeof(T) == 4:\n fastioPrintI32(stdout, cast[ptr int32](unsafeAddr a[0]), a.len.csize_t,\n sep.cstring, sep.len.csize_t)\n elif T is SomeUnsignedInt and sizeof(T) == 8:\n fastioPrintU64(stdout, cast[ptr uint64](unsafeAddr a[0]), a.len.csize_t,\n sep.cstring, sep.len.csize_t)\n elif T is SomeUnsignedInt and sizeof(T) == 4:\n fastioPrintU32(stdout, cast[ptr uint32](unsafeAddr a[0]), a.len.csize_t,\n sep.cstring, sep.len.csize_t)\n elif compiles(T.umod()) and compiles(a[0].val()):\n var canonical = fastioNewSeqUninit(uint32, a.len)\n for i, value in a:\n canonical[i] = value.val.uint32\n fastioPrintArrayImpl(canonical, sep)\n else:\n stdout.write(fastioJoinImpl(a, sep))\n stdout.write('\\n')\n \n proc fastioPrintOneImpl[T](value: T, sep: string) =\n when T is SomeSignedInt and sizeof(T) == 8:\n fastioPrintOneI64(stdout, value.int64)\n elif T is SomeSignedInt and sizeof(T) == 4:\n fastioPrintOneI32(stdout, value.int32)\n elif T is SomeUnsignedInt and sizeof(T) == 8:\n fastioPrintOneU64(stdout, value.uint64)\n elif T is SomeUnsignedInt and sizeof(T) == 4:\n fastioPrintOneU32(stdout, value.uint32)\n elif compiles(T.umod()) and compiles(value.val()):\n fastioPrintOneU32(stdout, value.val.uint32)\n else:\n fastioPrintWithSeparator(sep, value)\n \n proc fastioPrintIntegerMany[T: SomeInteger](sep: string,\n values: varargs[T]) =\n fastioPrintArrayImpl(values, sep)\n \n # Python風に print(*X) と書くと、Xを空白区切りで1行に出力する。\n template `*`*[T](values: openArray[T]): string =\n fastioJoinImpl(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(\"fastioJoinImpl\"):\n # オーバーロード解決時に *values が先に展開された場合。\n splatValues = values[0][1]\n if not splatValues.isNil:\n result = newCall(bindSym\"fastioPrintArrayImpl\", splatValues, sep)\n elif values.len == 1:\n result = newCall(bindSym\"fastioPrintOneImpl\", values[0], sep)\n else:\n let integerCall = newCall(bindSym\"fastioPrintIntegerMany\", sep)\n let fallbackCall = newCall(bindSym\"fastioPrintWithSeparator\", sep)\n for value in values:\n integerCall.add(value)\n fallbackCall.add(value)\n result = quote do:\n when compiles(`integerCall`):\n `integerCall`\n else:\n `fallbackCall`\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.. 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" # source: src/cplib/tmpl/optimize.nim ImportExpand "cplib/tmpl/optimize" <=== "when not declared CPLIB_TMPL_OPTIMIZE:\n const CPLIB_TMPL_OPTIMIZE* = 1\n import macros\n import strutils\n import std/compilesettings\n macro optimize*(arg: static string = \"\"\"nim c -d:danger -d:second_compile -d:useMalloc --gc:arc --panics:on --opt:speed --checks:off --passC:\"-flto -m64 -march=native -ffast-math\" --hints:off \"\"\") =\n let isSecond = defined(second_compile)\n let isDebug = defined(debug)\n\n if (not isSecond) and (not isDebug):\n if \"-d:second_compile\" notin arg:\n error(\"plz add -d:second_compile\")\n let sourcePath = querySetting(SingleValueSetting.projectFull)\n let projectDir = sourcePath[0.. 0: \"-o:\" & outFile & \" \" else: \"-o:a.out \"\n var cmd = \"cd \" & projectDir & \" && export PATH=$HOME/.nimble/bin:$PATH && \" & arg\n cmd.add(outFlag & sourcePath)\n\n echo \"--- Self-Recompiling with optimized settings ---\"\n echo \"Command: \", cmd\n echo \"\\n\\n\\n\"\n\n let output = staticExec(cmd)\n warning(output)\n echo \"\\n\\n\\n\"\n\n quit(0)" optimize("""nim cpp -d:danger -d:second_compile -d:useMalloc --gc:none --panics:on --opt:speed --checks:off --passC:"-O3 -flto -m64 -march=native -ffast-math" --hints:off """) var H,W = ii() var A = input(H*W,uint32) var S = newseqwith(H,uint32(0)) for i in 0..<(H): for j in 0..<(W): S[i] += A[i*W+j].uint32() var T = S.sum() S.applyit((it + T)) print(S.join("\n"))