QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#764080#8238. Dreamy PutatamaspyAC ✓1045ms273368kbC++2329.6kb2024-11-20 00:00:392024-11-20 00:00:45

Judging History

你现在查看的是最新测评结果

  • [2024-11-20 00:00:45]
  • 评测
  • 测评结果:AC
  • 用时:1045ms
  • 内存:273368kb
  • [2024-11-20 00:00:39]
  • 提交

answer

#line 1 "/home/maspy/compro/library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else

// https://codeforces.com/blog/entry/96344
#pragma GCC optimize("Ofast,unroll-loops")
// いまの CF だとこれ入れると動かない?
// #pragma GCC target("avx2,popcnt")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
  vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T kth_bit(int k) {
  return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
  return x >> k & 1;
}

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
  T sm = 0;
  for (auto &&a: A) sm += a;
  return sm;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
  FOR(iter) {
    double x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
  vector<int> ids(len(A));
  iota(all(ids), 0);
  sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  vc<T> B(len(I));
  FOR(i, len(I)) B[i] = A[I[i]];
  return B;
}

template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &... others) {
  vc<T> &res = first;
  (res.insert(res.end(), others.begin(), others.end()), ...);
}
#endif
#line 1 "/home/maspy/compro/library/other/io.hpp"
#define FASTIO
#include <unistd.h>

// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
  char num[10000][4];
  constexpr Pre() : num() {
    for (int i = 0; i < 10000; i++) {
      int n = i;
      for (int j = 3; j >= 0; j--) {
        num[i][j] = n % 10 | '0';
        n /= 10;
      }
    }
  }
} constexpr pre;

inline void load() {
  memcpy(ibuf, ibuf + pil, pir - pil);
  pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
  pil = 0;
  if (pir < SZ) ibuf[pir++] = '\n';
}

inline void flush() {
  fwrite(obuf, 1, por, stdout);
  por = 0;
}

void rd(char &c) {
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
}

void rd(string &x) {
  x.clear();
  char c;
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
  do {
    x += c;
    if (pil == pir) load();
    c = ibuf[pil++];
  } while (!isspace(c));
}

template <typename T>
void rd_real(T &x) {
  string s;
  rd(s);
  x = stod(s);
}

template <typename T>
void rd_integer(T &x) {
  if (pil + 100 > pir) load();
  char c;
  do
    c = ibuf[pil++];
  while (c < '-');
  bool minus = 0;
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (c == '-') { minus = 1, c = ibuf[pil++]; }
  }
  x = 0;
  while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (minus) x = -x;
  }
}

void rd(int &x) { rd_integer(x); }
void rd(ll &x) { rd_integer(x); }
void rd(i128 &x) { rd_integer(x); }
void rd(u32 &x) { rd_integer(x); }
void rd(u64 &x) { rd_integer(x); }
void rd(u128 &x) { rd_integer(x); }
void rd(double &x) { rd_real(x); }
void rd(long double &x) { rd_real(x); }
void rd(f128 &x) { rd_real(x); }

template <class T, class U>
void rd(pair<T, U> &p) {
  return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
  if constexpr (N < std::tuple_size<T>::value) {
    auto &x = std::get<N>(t);
    rd(x);
    rd_tuple<N + 1>(t);
  }
}
template <class... T>
void rd(tuple<T...> &tpl) {
  rd_tuple(tpl);
}

template <size_t N = 0, typename T>
void rd(array<T, N> &x) {
  for (auto &d: x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
  for (auto &d: x) rd(d);
}

void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
  rd(h), read(t...);
}

void wt(const char c) {
  if (por == SZ) flush();
  obuf[por++] = c;
}
void wt(const string s) {
  for (char c: s) wt(c);
}
void wt(const char *s) {
  size_t len = strlen(s);
  for (size_t i = 0; i < len; i++) wt(s[i]);
}

template <typename T>
void wt_integer(T x) {
  if (por > SZ - 100) flush();
  if (x < 0) { obuf[por++] = '-', x = -x; }
  int outi;
  for (outi = 96; x >= 10000; outi -= 4) {
    memcpy(out + outi, pre.num[x % 10000], 4);
    x /= 10000;
  }
  if (x >= 1000) {
    memcpy(obuf + por, pre.num[x], 4);
    por += 4;
  } else if (x >= 100) {
    memcpy(obuf + por, pre.num[x] + 1, 3);
    por += 3;
  } else if (x >= 10) {
    int q = (x * 103) >> 10;
    obuf[por] = q | '0';
    obuf[por + 1] = (x - q * 10) | '0';
    por += 2;
  } else
    obuf[por++] = x | '0';
  memcpy(obuf + por, out + outi + 4, 96 - outi);
  por += 96 - outi;
}

template <typename T>
void wt_real(T x) {
  ostringstream oss;
  oss << fixed << setprecision(15) << double(x);
  string s = oss.str();
  wt(s);
}

void wt(int x) { wt_integer(x); }
void wt(ll x) { wt_integer(x); }
void wt(i128 x) { wt_integer(x); }
void wt(u32 x) { wt_integer(x); }
void wt(u64 x) { wt_integer(x); }
void wt(u128 x) { wt_integer(x); }
void wt(double x) { wt_real(x); }
void wt(long double x) { wt_real(x); }
void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first);
  wt(' ');
  wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::read;
using fastio::print;
using fastio::flush;

#if defined(LOCAL)
#define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define SHOW1(x) print(#x, "=", (x)), flush()
#define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush()
#define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush()
#else
#define SHOW(...)
#endif

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __VA_ARGS__; \
  read(__VA_ARGS__)
#define U32(...)   \
  u32 __VA_ARGS__; \
  read(__VA_ARGS__)
#define U64(...)   \
  u64 __VA_ARGS__; \
  read(__VA_ARGS__)
#define STR(...)      \
  string __VA_ARGS__; \
  read(__VA_ARGS__)
#define CHAR(...)   \
  char __VA_ARGS__; \
  read(__VA_ARGS__)
#define DBL(...)      \
  double __VA_ARGS__; \
  read(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"

#line 2 "/home/maspy/compro/library/mod/modint_common.hpp"

struct has_mod_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.get_mod(), std::true_type{});
  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_mod : public decltype(has_mod_impl::check<T>(std::declval<T>())) {};

template <typename mint>
mint inv(int n) {
  static const int mod = mint::get_mod();
  static vector<mint> dat = {0, 1};
  assert(0 <= n);
  if (n >= mod) n %= mod;
  while (len(dat) <= n) {
    int k = len(dat);
    int q = (mod + k - 1) / k;
    dat.eb(dat[k * q - mod] * mint::raw(q));
  }
  return dat[n];
}

template <typename mint>
mint fact(int n) {
  static const int mod = mint::get_mod();
  assert(0 <= n && n < mod);
  static vector<mint> dat = {1, 1};
  while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * mint::raw(len(dat)));
  return dat[n];
}

template <typename mint>
mint fact_inv(int n) {
  static vector<mint> dat = {1, 1};
  if (n < 0) return mint(0);
  while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * inv<mint>(len(dat)));
  return dat[n];
}

template <class mint, class... Ts>
mint fact_invs(Ts... xs) {
  return (mint(1) * ... * fact_inv<mint>(xs));
}

template <typename mint, class Head, class... Tail>
mint multinomial(Head &&head, Tail &&... tail) {
  return fact<mint>(head) * fact_invs<mint>(std::forward<Tail>(tail)...);
}

template <typename mint>
mint C_dense(int n, int k) {
  assert(n >= 0);
  if (k < 0 || n < k) return 0;
  static vvc<mint> C;
  static int H = 0, W = 0;
  auto calc = [&](int i, int j) -> mint {
    if (i == 0) return (j == 0 ? mint(1) : mint(0));
    return C[i - 1][j] + (j ? C[i - 1][j - 1] : 0);
  };
  if (W <= k) {
    FOR(i, H) {
      C[i].resize(k + 1);
      FOR(j, W, k + 1) { C[i][j] = calc(i, j); }
    }
    W = k + 1;
  }
  if (H <= n) {
    C.resize(n + 1);
    FOR(i, H, n + 1) {
      C[i].resize(W);
      FOR(j, W) { C[i][j] = calc(i, j); }
    }
    H = n + 1;
  }
  return C[n][k];
}

template <typename mint, bool large = false, bool dense = false>
mint C(ll n, ll k) {
  assert(n >= 0);
  if (k < 0 || n < k) return 0;
  if constexpr (dense) return C_dense<mint>(n, k);
  if constexpr (!large) return multinomial<mint>(n, k, n - k);
  k = min(k, n - k);
  mint x(1);
  FOR(i, k) x *= mint(n - i);
  return x * fact_inv<mint>(k);
}

template <typename mint, bool large = false>
mint C_inv(ll n, ll k) {
  assert(n >= 0);
  assert(0 <= k && k <= n);
  if (!large) return fact_inv<mint>(n) * fact<mint>(k) * fact<mint>(n - k);
  return mint(1) / C<mint, 1>(n, k);
}

// [x^d](1-x)^{-n}
template <typename mint, bool large = false, bool dense = false>
mint C_negative(ll n, ll d) {
  assert(n >= 0);
  if (d < 0) return mint(0);
  if (n == 0) { return (d == 0 ? mint(1) : mint(0)); }
  return C<mint, large, dense>(n + d - 1, d);
}
#line 3 "/home/maspy/compro/library/mod/modint.hpp"

template <int mod>
struct modint {
  static constexpr u32 umod = u32(mod);
  static_assert(umod < u32(1) << 31);
  u32 val;

  static modint raw(u32 v) {
    modint x;
    x.val = v;
    return x;
  }
  constexpr modint() : val(0) {}
  constexpr modint(u32 x) : val(x % umod) {}
  constexpr modint(u64 x) : val(x % umod) {}
  constexpr modint(u128 x) : val(x % umod) {}
  constexpr modint(int x) : val((x %= mod) < 0 ? x + mod : x){};
  constexpr modint(ll x) : val((x %= mod) < 0 ? x + mod : x){};
  constexpr modint(i128 x) : val((x %= mod) < 0 ? x + mod : x){};
  bool operator<(const modint &other) const { return val < other.val; }
  modint &operator+=(const modint &p) {
    if ((val += p.val) >= umod) val -= umod;
    return *this;
  }
  modint &operator-=(const modint &p) {
    if ((val += umod - p.val) >= umod) val -= umod;
    return *this;
  }
  modint &operator*=(const modint &p) {
    val = u64(val) * p.val % umod;
    return *this;
  }
  modint &operator/=(const modint &p) {
    *this *= p.inverse();
    return *this;
  }
  modint operator-() const { return modint::raw(val ? mod - val : u32(0)); }
  modint operator+(const modint &p) const { return modint(*this) += p; }
  modint operator-(const modint &p) const { return modint(*this) -= p; }
  modint operator*(const modint &p) const { return modint(*this) *= p; }
  modint operator/(const modint &p) const { return modint(*this) /= p; }
  bool operator==(const modint &p) const { return val == p.val; }
  bool operator!=(const modint &p) const { return val != p.val; }
  modint inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b), swap(u -= t * v, v);
    }
    return modint(u);
  }
  modint pow(ll n) const {
    assert(n >= 0);
    modint ret(1), mul(val);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  static constexpr int get_mod() { return mod; }
  // (n, r), r は 1 の 2^n 乗根
  static constexpr pair<int, int> ntt_info() {
    if (mod == 120586241) return {20, 74066978};
    if (mod == 167772161) return {25, 17};
    if (mod == 469762049) return {26, 30};
    if (mod == 754974721) return {24, 362};
    if (mod == 880803841) return {23, 211};
    if (mod == 943718401) return {22, 663003469};
    if (mod == 998244353) return {23, 31};
    if (mod == 1004535809) return {21, 582313106};
    if (mod == 1012924417) return {21, 368093570};
    return {-1, -1};
  }
  static constexpr bool can_ntt() { return ntt_info().fi != -1; }
};

#ifdef FASTIO
template <int mod>
void rd(modint<mod> &x) {
  fastio::rd(x.val);
  x.val %= mod;
  // assert(0 <= x.val && x.val < mod);
}
template <int mod>
void wt(modint<mod> x) {
  fastio::wt(x.val);
}
#endif

using modint107 = modint<1000000007>;
using modint998 = modint<998244353>;
#line 3 "/home/maspy/compro/library/linalg/matrix_mul.hpp"

template <class T, typename enable_if<has_mod<T>::value>::type* = nullptr>
vc<vc<T>> matrix_mul(const vc<vc<T>>& A, const vc<vc<T>>& B, int N1 = -1,
                     int N2 = -1, int N3 = -1) {
  if (N1 == -1) { N1 = len(A), N2 = len(B), N3 = len(B[0]); }
  vv(u32, b, N3, N2);
  FOR(i, N2) FOR(j, N3) b[j][i] = B[i][j].val;
  vv(T, C, N1, N3);

  if ((T::get_mod() < (1 << 30)) && N2 <= 16) {
    FOR(i, N1) FOR(j, N3) {
      u64 sm = 0;
      FOR(m, N2) sm += u64(A[i][m].val) * b[j][m];
      C[i][j] = sm;
    }
  } else {
    FOR(i, N1) FOR(j, N3) {
      u128 sm = 0;
      FOR(m, N2) sm += u64(A[i][m].val) * b[j][m];
      C[i][j] = T::raw(sm % (T::get_mod()));
    }
  }
  return C;
}

template <class T, typename enable_if<!has_mod<T>::value>::type* = nullptr>
vc<vc<T>> matrix_mul(const vc<vc<T>>& A, const vc<vc<T>>& B, int N1 = -1,
                     int N2 = -1, int N3 = -1) {
  if (N1 == -1) { N1 = len(A), N2 = len(B), N3 = len(B[0]); }
  vv(T, b, N2, N3);
  FOR(i, N2) FOR(j, N3) b[j][i] = B[i][j];
  vv(T, C, N1, N3);
  FOR(n, N1) FOR(m, N2) FOR(k, N3) C[n][k] += A[n][m] * b[k][m];
  return C;
}

// square-matrix defined as array
template <class T, int N,
          typename enable_if<has_mod<T>::value>::type* = nullptr>
array<array<T, N>, N> matrix_mul(const array<array<T, N>, N>& A,
                                 const array<array<T, N>, N>& B) {
  array<array<T, N>, N> C{};

  if ((T::get_mod() < (1 << 30)) && N <= 16) {
    FOR(i, N) FOR(k, N) {
      u64 sm = 0;
      FOR(j, N) sm += u64(A[i][j].val) * (B[j][k].val);
      C[i][k] = sm;
    }
  } else {
    FOR(i, N) FOR(k, N) {
      u128 sm = 0;
      FOR(j, N) sm += u64(A[i][j].val) * (B[j][k].val);
      C[i][k] = sm;
    }
  }
  return C;
}

// square-matrix defined as array
template <class T, int N,
          typename enable_if<!has_mod<T>::value>::type* = nullptr>
array<array<T, N>, N> matrix_mul(const array<array<T, N>, N>& A,
                                 const array<array<T, N>, N>& B) {
  array<array<T, N>, N> C{};
  FOR(i, N) FOR(j, N) FOR(k, N) C[i][k] += A[i][j] * B[j][k];
  return C;
}
#line 2 "/home/maspy/compro/library/ds/segtree/segtree.hpp"

template <class Monoid>
struct SegTree {
  using MX = Monoid;
  using X = typename MX::value_type;
  using value_type = X;
  vc<X> dat;
  int n, log, size;

  SegTree() {}
  SegTree(int n) { build(n); }
  template <typename F>
  SegTree(int n, F f) {
    build(n, f);
  }
  SegTree(const vc<X>& v) { build(v); }

  void build(int m) {
    build(m, [](int i) -> X { return MX::unit(); });
  }
  void build(const vc<X>& v) {
    build(len(v), [&](int i) -> X { return v[i]; });
  }
  template <typename F>
  void build(int m, F f) {
    n = m, log = 1;
    while ((1 << log) < n) ++log;
    size = 1 << log;
    dat.assign(size << 1, MX::unit());
    FOR(i, n) dat[size + i] = f(i);
    FOR_R(i, 1, size) update(i);
  }

  X get(int i) { return dat[size + i]; }
  vc<X> get_all() { return {dat.begin() + size, dat.begin() + size + n}; }

  void update(int i) { dat[i] = Monoid::op(dat[2 * i], dat[2 * i + 1]); }
  void set(int i, const X& x) {
    assert(i < n);
    dat[i += size] = x;
    while (i >>= 1) update(i);
  }

  void multiply(int i, const X& x) {
    assert(i < n);
    i += size;
    dat[i] = Monoid::op(dat[i], x);
    while (i >>= 1) update(i);
  }

  X prod(int L, int R) {
    assert(0 <= L && L <= R && R <= n);
    X vl = Monoid::unit(), vr = Monoid::unit();
    L += size, R += size;
    while (L < R) {
      if (L & 1) vl = Monoid::op(vl, dat[L++]);
      if (R & 1) vr = Monoid::op(dat[--R], vr);
      L >>= 1, R >>= 1;
    }
    return Monoid::op(vl, vr);
  }

  X prod_all() { return dat[1]; }

  template <class F>
  int max_right(F check, int L) {
    assert(0 <= L && L <= n && check(Monoid::unit()));
    if (L == n) return n;
    L += size;
    X sm = Monoid::unit();
    do {
      while (L % 2 == 0) L >>= 1;
      if (!check(Monoid::op(sm, dat[L]))) {
        while (L < size) {
          L = 2 * L;
          if (check(Monoid::op(sm, dat[L]))) { sm = Monoid::op(sm, dat[L++]); }
        }
        return L - size;
      }
      sm = Monoid::op(sm, dat[L++]);
    } while ((L & -L) != L);
    return n;
  }

  template <class F>
  int min_left(F check, int R) {
    assert(0 <= R && R <= n && check(Monoid::unit()));
    if (R == 0) return 0;
    R += size;
    X sm = Monoid::unit();
    do {
      --R;
      while (R > 1 && (R % 2)) R >>= 1;
      if (!check(Monoid::op(dat[R], sm))) {
        while (R < size) {
          R = 2 * R + 1;
          if (check(Monoid::op(dat[R], sm))) { sm = Monoid::op(dat[R--], sm); }
        }
        return R + 1 - size;
      }
      sm = Monoid::op(dat[R], sm);
    } while ((R & -R) != R);
    return 0;
  }

  // prod_{l<=i<r} A[i xor x]
  X xor_prod(int l, int r, int xor_val) {
    static_assert(Monoid::commute);
    X x = Monoid::unit();
    for (int k = 0; k < log + 1; ++k) {
      if (l >= r) break;
      if (l & 1) { x = Monoid::op(x, dat[(size >> k) + ((l++) ^ xor_val)]); }
      if (r & 1) { x = Monoid::op(x, dat[(size >> k) + ((--r) ^ xor_val)]); }
      l /= 2, r /= 2, xor_val /= 2;
    }
    return x;
  }
};
#line 1 "/home/maspy/compro/library/linalg/solve_linear.hpp"
/*
0 行目に解のひとつ。
1~行目に解空間の基底が行ベクトルとして入る。
解なし = empty
*/
template <typename T>
vc<vc<T>> solve_linear(vc<vc<T>> a, vc<T> b, int n = -1, int m = -1) {
  if (n == -1) {
    n = len(a);
    assert(n > 0);
    m = len(a[0]);
  }
  assert(n == len(a) && n == len(b));
  int rk = 0;
  FOR(j, m) {
    if (rk == n) break;
    FOR(i, rk, n) if (a[i][j] != 0) {
      swap(a[rk], a[i]);
      swap(b[rk], b[i]);
      break;
    }
    if (a[rk][j] == 0) continue;
    T c = T(1) / a[rk][j];
    for (auto&& x: a[rk]) x *= c;
    b[rk] *= c;
    FOR(i, n) if (i != rk) {
      T c = a[i][j];
      if (c == T(0)) continue;
      b[i] -= b[rk] * c;
      FOR(k, j, m) { a[i][k] -= a[rk][k] * c; }
    }
    ++rk;
  }
  FOR(i, rk, n) if (b[i] != 0) return {};
  vc<vc<T>> res(1, vc<T>(m));
  vc<int> pivot(m, -1);
  int p = 0;
  FOR(i, rk) {
    while (a[i][p] == 0) ++p;
    res[0][p] = b[i];
    pivot[p] = i;
  }
  FOR(j, m) if (pivot[j] == -1) {
    vc<T> x(m);
    x[j] = -1;
    FOR(k, j) if (pivot[k] != -1) x[k] = a[pivot[k]][j];
    res.eb(x);
  }
  return res;
}
#line 8 "main.cpp"

using mint = modint107;

/*
tx,tx+1行目の解を表す変数 2M個 X[0] to X[2M-1]
tx+1 to tx-1 で釣り合うようにする
[tx-1,tx] が  X[0] to X[2M-1] の線形結合で書ける
tx 行目が戻ってきます
tx 行目で釣り合ってます
これで 2M 個の式
det=0 があるかは知らない!

セグ木
dat[i]: i-1,i行目 ->  i+1行目
*/

using MAT = array<array<mint, 11>, 11>;

struct Mono {
  using value_type = MAT;
  using X = value_type;
  static X op(X L, X R) { return matrix_mul<mint, 11>(R, L); }
  static X unit() {
    MAT x;
    FOR(i, 11) FOR(j, 11) x[i][j] = (i == j ? 1 : 0);
    return x;
  }
  static constexpr bool commute = 0;
};

void solve() {
  LL(N, M);
  VV(mint, L, N, M);
  VV(mint, R, N, M);
  VV(mint, U, N, M);
  VV(mint, D, N, M);
  FOR(i, N) FOR(j, M) L[i][j] *= inv<mint>(100);
  FOR(i, N) FOR(j, M) R[i][j] *= inv<mint>(100);
  FOR(i, N) FOR(j, M) U[i][j] *= inv<mint>(100);
  FOR(i, N) FOR(j, M) D[i][j] *= inv<mint>(100);

  vc<int> pre(M), nxt(M);
  FOR(i, M) pre[i] = (i + M - 1) % M;
  FOR(i, M) nxt[i] = (i + 1) % M;

  auto segf = [&](int i) -> MAT {
    i %= N;
    MAT mat{};
    mat[2 * M][2 * M] = 1;
    FOR(j, M) mat[j][M + j] = 1;
    FOR(j, M) {
      mint p = (D[i][j].inverse());
      mat[M + j][M + j] = p;
      mat[M + j][2 * M] = -p;
      mat[M + j][j] = -U[i][j] * p;
      mat[M + j][M + nxt[j]] = -R[i][j] * p;
      mat[M + j][M + pre[j]] = -L[i][j] * p;
    }
    return mat;
  };
  SegTree<Mono> seg(2 * N, segf);

  auto Q1 = [&]() -> void {
    LL(i, j, l, r, u, d);
    L[i][j] = mint(l) * inv<mint>(100);
    R[i][j] = mint(r) * inv<mint>(100);
    U[i][j] = mint(u) * inv<mint>(100);
    D[i][j] = mint(d) * inv<mint>(100);
    MAT x = segf(i);
    seg.set(i, x), seg.set(N + i, x);
  };

  auto Q2 = [&]() -> void {
    LL(sx, sy, tx, ty);
    MAT A = seg.prod(tx + 1, tx + N);
    vv(mint, mat, 2 * M + 1, 2 * M + 1);
    vc<mint> rhs(2 * M + 1);
    mat[2 * M][2 * M] = 1;
    rhs[2 * M] = 1;
    // 0,1,2,3 は戻ってきています
    FOR(j, M) {
      mat[j][j] -= 1;
      FOR(k, 2 * M + 1) { mat[j][k] += A[M + j][k]; }
    }
    // 0,1,...でのつり合い
    FOR(j, M) {
      if (j == ty) {
        mat[M + j][j] = 1;
        continue;
      }
      mat[M + j][j] = -1;
      mat[M + j][nxt[j]] += R[tx][j];
      mat[M + j][pre[j]] += L[tx][j];
      mat[M + j][M + j] += D[tx][j];
      mat[M + j][2 * M] += 1;
      FOR(k, 2 * M + 1) { mat[M + j][k] += A[j][k] * U[tx][j]; }
    }
    auto Xs = solve_linear<mint>(mat, rhs);
    assert(len(Xs) == 1);
    vc<mint> X = Xs[0];
    if (sx == tx) {
      print(X[sy]);
      return;
    }
    auto get = [&](int sx) -> vc<mint> {
      // if (sx == tx) return X;
      if (sx <= tx) sx += N;
      MAT B = seg.prod(tx + 1, sx);
      vc<mint> Y(2 * M + 1);
      FOR(i, 2 * M + 1) FOR(j, 2 * M + 1) Y[i] += B[i][j] * X[j];
      return {Y.begin() + M, Y.begin() + 2 * M};
    };

    auto Y = get(sx);
    print(Y[sy]);

    // SHOW(X);
    // SHOW(tx, ty);
    // vv(mint, dp, 2 * N, M);
    // FOR(i, N) dp[i] = get(i);
    // FOR(i, N) dp[N + i] = dp[i];
    // FOR(i, 1, 2 * N - 1) FOR(j, M) {
    //   if ((i - tx) % N == 0 && (j - ty) % M == 0) {
    //     assert(dp[i][j] == 0);
    //     continue;
    //   }
    //   mint lhs = dp[i][j];
    //   mint rhs = 1;
    //   rhs += D[i % N][j] * dp[i + 1][j];
    //   rhs += L[i % N][j] * dp[i][pre[j]];
    //   rhs += R[i % N][j] * dp[i][nxt[j]];
    //   rhs += U[i % N][j] * dp[i - 1][j];
    //   if (lhs != rhs) SHOW(i, j, lhs, rhs);
    // }
    // SHOW(dp[sx][sy]);
    // print();
  };

  LL(Q);
  FOR(Q) {
    INT(t);
    if (t == 1) Q1();
    if (t == 2) Q2();
  }
}

signed main() { solve(); }

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3776kb

input:

4 3
1 2 3
4 5 6
7 8 9
10 11 12
23 24 25
26 27 28
29 30 31
32 33 34
10 11 12
13 14 15
16 17 18
19 20 21
66 63 60
57 54 51
48 45 42
39 36 33
4
2 0 1 1 1
2 0 0 3 2
1 1 1 25 25 25 25
2 0 0 3 2

output:

76426175
344136684
555192113

result:

ok 3 number(s): "76426175 344136684 555192113"

Test #2:

score: 0
Accepted
time: 118ms
memory: 3964kb

input:

3 3
95 62 24
70 74 23
53 63 36
2 25 20
13 2 4
16 22 40
2 2 14
11 1 67
17 6 20
1 11 42
6 23 6
14 9 4
30000
2 2 2 0 2
2 2 0 0 0
2 1 0 0 0
1 0 1 28 36 4 32
1 1 1 55 32 12 1
2 2 1 1 0
2 1 2 0 0
1 0 2 89 2 3 6
1 2 0 53 29 8 10
2 2 2 0 1
2 2 0 1 0
1 2 1 83 15 1 1
2 2 1 1 2
1 2 2 54 41 2 3
2 2 0 1 2
1 0 1 ...

output:

794352047
445720561
950211149
433211214
322045805
617604648
966924565
819436272
601016121
500019039
427833008
54797408
789868594
569035765
757433456
254373638
982293964
982293964
853196341
504864820
764730651
545590959
586948249
843898620
592509932
508256498
954689273
713397189
518777787
988654370
9...

result:

ok 15144 numbers

Test #3:

score: 0
Accepted
time: 139ms
memory: 4032kb

input:

3 4
71 12 51 12
85 83 41 85
20 19 3 75
25 25 1 60
12 7 11 10
72 65 13 1
1 44 16 5
1 1 32 3
3 14 69 5
3 19 32 23
2 9 16 2
5 2 15 19
30000
2 2 3 1 3
2 2 0 0 2
2 1 1 0 1
1 2 1 64 29 1 6
1 2 3 20 70 2 8
2 1 3 0 3
1 1 1 87 7 5 1
2 2 1 0 0
2 2 1 0 3
2 2 1 0 1
1 2 3 64 25 4 7
2 2 2 1 3
2 2 0 1 0
2 2 2 1 1
...

output:

354672429
912592651
205898503
454595712
326527558
244765319
546555335
503022787
150107622
215140594
135585822
236524624
320026574
56673681
280529820
593236671
485177445
743045702
401830954
263027327
262401428
875715418
150860374
179088742
530926216
923964115
667195812
555355389
571319510
737826815
2...

result:

ok 15095 numbers

Test #4:

score: 0
Accepted
time: 166ms
memory: 4268kb

input:

3 5
30 59 44 6 16
12 30 11 83 86
58 49 65 50 44
68 24 2 31 34
24 11 74 12 3
32 23 4 2 17
1 15 29 59 5
19 31 6 4 6
6 15 8 5 34
1 2 25 4 45
45 28 9 1 5
4 13 23 43 5
30000
2 2 0 0 3
2 1 2 0 2
1 1 4 75 7 5 13
2 1 3 0 3
2 1 2 0 4
1 0 1 29 4 53 14
1 0 4 88 10 1 1
2 2 4 1 3
2 2 4 1 3
1 1 2 79 14 3 4
2 2 4 ...

output:

518130880
192901969
549392638
587807011
692872396
692872396
17980668
639677814
546570041
285563686
899784603
294224899
300472120
850053405
384430261
300472120
427268842
269195383
688402844
326045142
856426869
371304714
239555499
858574611
249782581
367550595
813603991
235110041
400091873
781877964
3...

result:

ok 15101 numbers

Test #5:

score: 0
Accepted
time: 126ms
memory: 3860kb

input:

4 3
88 61 8
25 67 82
16 72 5
71 24 3
4 31 51
66 17 4
31 26 63
1 8 11
5 7 21
4 4 6
11 1 31
15 11 50
3 1 20
5 12 8
42 1 1
13 57 36
30000
2 2 1 1 0
1 0 1 15 57 26 2
2 2 1 1 2
2 2 2 0 2
2 2 0 0 0
1 0 1 54 22 8 16
1 2 2 75 12 5 8
1 2 2 67 4 11 18
2 3 2 1 2
2 1 1 0 0
1 0 0 10 70 2 18
1 2 1 35 45 1 19
2 1 ...

output:

932295932
233938741
962914267
815912722
593921746
511030278
628154532
228176878
914256121
677597027
882198995
674345936
857722782
760168451
592843949
808131481
511414300
772346610
433759393
630381295
280392804
171039969
661778948
70945073
35883397
783291216
850970394
64550544
976645462
954726136
157...

result:

ok 15053 numbers

Test #6:

score: 0
Accepted
time: 145ms
memory: 3980kb

input:

4 4
64 11 70 2
77 89 4 59
6 25 27 5
63 4 18 2
2 50 16 37
21 5 64 27
80 35 52 91
32 84 58 39
9 31 4 21
1 5 19 6
12 6 17 3
4 7 7 48
25 8 10 40
1 1 13 8
2 34 4 1
1 5 17 11
30000
2 3 3 0 2
2 3 3 2 0
2 1 0 0 2
2 2 2 0 3
2 3 2 0 0
1 1 0 23 67 3 7
1 1 2 35 55 4 6
2 3 3 2 0
1 3 0 43 45 2 10
2 2 2 1 2
1 2 1 ...

output:

334801426
881562250
651785364
269029145
797504056
802317525
440410805
53552375
677589332
658093753
982712164
788895880
961111614
469915277
451427917
456274210
40639936
749247016
771008350
950381441
457182636
209481283
480115371
761237802
49182981
367217021
640094262
160525935
294564634
429528898
122...

result:

ok 15241 numbers

Test #7:

score: 0
Accepted
time: 179ms
memory: 4036kb

input:

4 5
23 58 1 58 70
53 28 81 87 92
30 77 67 56 70
88 69 21 93 36
70 2 67 16 18
3 61 17 2 6
55 5 10 39 14
9 1 16 3 35
3 13 23 7 7
11 5 1 5 1
8 15 1 4 13
2 20 60 3 26
4 27 9 19 5
33 6 1 6 1
7 3 22 1 3
1 10 3 1 3
30000
2 2 3 0 4
2 3 4 2 1
1 1 4 67 20 7 6
2 1 4 0 0
2 3 2 0 4
2 2 0 1 0
1 1 0 70 17 2 11
2 1...

output:

773395519
202993618
363831331
26600683
628490409
965293227
988464346
254121301
37657552
240517728
959914287
429450563
627759885
77881676
628640396
532755563
789153435
734943549
768356171
155599128
586898868
974688108
332913835
782091995
938757871
25868446
879373433
781809249
970823879
749935361
7024...

result:

ok 15156 numbers

Test #8:

score: 0
Accepted
time: 148ms
memory: 3912kb

input:

5 3
45 41 80
78 24 25
14 64 88
77 13 48
78 19 44
38 13 9
12 57 47
20 8 5
19 15 8
9 26 1
7 5 4
7 4 26
59 8 2
2 3 43
2 36 44
10 41 7
3 15 2
7 20 5
2 69 1
11 19 11
30000
2 4 2 2 1
1 3 0 79 7 4 10
1 3 1 25 28 42 5
1 4 1 66 19 12 3
2 4 2 0 1
2 4 2 3 0
1 4 2 81 2 2 15
2 2 2 0 0
2 4 0 0 1
1 0 1 92 5 1 2
2 ...

output:

424703237
74093968
190343982
962559774
102755459
671175664
701973110
602834246
516593273
331066997
859528771
49139165
970030107
394559031
237142618
869135259
952444644
324920125
954118145
742091927
407449341
550194093
802575663
95826696
690734073
36175815
772030766
568903915
533320284
698003803
8396...

result:

ok 15052 numbers

Test #9:

score: 0
Accepted
time: 171ms
memory: 3984kb

input:

5 4
57 88 72 54
52 68 2 33
72 47 16 85
48 71 26 31
13 24 1 36
21 8 11 31
5 1 87 27
7 47 61 7
20 9 59 19
24 36 92 49
8 1 10 11
34 20 5 9
8 2 2 1
21 13 2 27
56 11 1 9
14 3 7 4
9 11 6 31
13 4 21 7
11 7 13 23
7 29 6 6
30000
2 4 0 0 1
2 4 1 3 0
2 4 3 3 0
1 0 1 10 50 15 25
2 3 0 2 3
2 4 1 3 0
2 4 2 2 3
2 ...

output:

50910996
216377646
573900831
94488786
398129185
72553576
227714101
439263073
864769360
737633649
315953214
971053832
297538736
612444954
155336683
149698059
307519489
277288455
434987247
690011402
580947251
261583247
24280548
473637822
957506150
659358847
304584321
805231430
355763909
371671194
3563...

result:

ok 15229 numbers

Test #10:

score: 0
Accepted
time: 205ms
memory: 4248kb

input:

5 5
16 38 3 13 63
94 53 55 11 36
89 60 70 27 78
36 15 75 89 65
69 44 67 66 92
4 3 48 80 31
1 40 31 72 13
7 15 14 17 19
19 50 9 6 4
27 21 24 26 3
9 13 36 2 2
4 4 11 7 44
3 13 13 51 2
30 9 2 1 1
2 8 5 5 4
71 46 13 5 4
1 3 3 10 7
1 12 3 5 1
15 26 14 4 30
2 27 4 3 1
30000
2 3 3 0 4
2 3 3 2 2
1 0 0 34 34...

output:

157640412
687387050
837923934
844421869
274018362
895430369
443299523
382623443
850399991
931489969
759234940
192352559
611861383
387013782
842447071
496743050
623398591
561396284
479647208
336852340
777717261
191482727
633322383
287868764
74768141
632861696
784428747
218665333
627277702
333573501
8...

result:

ok 15144 numbers

Test #11:

score: 0
Accepted
time: 58ms
memory: 3940kb

input:

10 3
81 66 37
60 28 14
78 86 86
48 85 80
35 38 80
6 24 92
42 39 22
51 19 17
24 90 75
84 97 58
8 6 58
15 52 16
4 10 8
30 7 3
51 30 15
62 4 5
50 18 11
38 39 50
72 8 17
10 1 40
2 10 1
16 6 13
14 1 1
21 1 7
2 25 3
26 20 2
4 13 7
7 41 1
1 1 7
4 1 1
9 18 4
9 14 57
4 3 5
1 7 10
12 7 2
6 52 1
4 30 60
4 1 32...

output:

228427104
803896245
264833563
753064087
301119297
725047338
241342719
686145017
602098062
293499112
249785324
771050358
774679486
964267877
470137845
52315848
785143210
282403387
517877956
785226997
445689512
232849844
615681766
129948283
123199230
605814309
15398995
490612360
46486523
376156864
929...

result:

ok 6539 numbers

Test #12:

score: 0
Accepted
time: 70ms
memory: 3812kb

input:

15 4
19 30 58 23
95 35 56 12
73 23 82 3
24 36 64 33
35 83 67 28
79 1 36 24
45 83 39 70
49 21 10 16
11 8 66 75
86 89 44 82
33 84 74 46
91 63 8 4
68 41 30 94
37 29 26 41
73 5 55 14
66 42 25 21
1 7 19 17
16 43 2 69
26 39 10 48
31 14 10 65
5 75 17 43
45 13 25 22
40 66 55 34
44 73 11 17
8 7 26 13
61 5 5 ...

output:

817241723
982734749
482199175
119378443
893638291
979342465
264556338
92052934
958824466
851266810
683720751
872806324
787928882
851735609
807816096
141792886
967921253
692672934
559586634
163827114
976156746
592647529
549317880
658406488
218852213
808431177
539401099
850609482
1883073
656868643
114...

result:

ok 6484 numbers

Test #13:

score: 0
Accepted
time: 90ms
memory: 4016kb

input:

20 5
74 46 89 8 37
52 88 26 44 20
60 50 15 11 39
60 42 90 42 17
54 79 34 49 25
47 34 97 67 66
33 32 57 63 57
54 32 95 41 77
20 30 10 61 75
43 68 59 81 66
50 40 42 26 28
7 72 31 77 76
81 92 31 27 7
10 67 68 71 52
58 42 12 69 35
73 88 66 38 47
71 23 56 66 2
27 49 57 59 90
41 58 71 75 91
8 67 52 65 26
...

output:

639778417
364226267
647053368
883426672
42223536
418385936
943273737
122622223
109902690
422993489
189755364
885103191
233768416
16323535
133413893
368728303
429746539
146564917
232762353
447281254
52315190
375639139
419899498
635167879
757822657
952255724
152964536
238218545
147952347
886296437
428...

result:

ok 6491 numbers

Test #14:

score: 0
Accepted
time: 1005ms
memory: 273328kb

input:

100000 3
1 73 64
52 63 95
30 77 54
84 82 88
42 49 85
50 78 67
58 19 58
35 18 15
5 7 1
1 36 93
68 26 2
86 92 53
81 60 61
16 64 23
85 48 19
36 93 13
69 28 55
64 12 55
5 17 48
96 39 96
36 70 90
38 96 5
2 16 89
20 60 23
77 95 60
26 31 9
44 11 49
96 9 26
58 53 55
2 6 15
6 36 28
69 87 57
37 24 9
75 40 75
...

output:

284120907
924649627
863561154
124580633
327461480
880541069
354216083
344425981
869290322
726162864
852401987
901164811
737248442
713336732
116936699
93725609
571036745
555110281
98123687
360768289
548752652
135342301
113187378
854007521
456474003
405207785
568222544
273119944
23105323
959342696
470...

result:

ok 5549 numbers

Test #15:

score: 0
Accepted
time: 976ms
memory: 272900kb

input:

100000 4
43 84 94 9
51 53 43 92
80 85 84 74
28 60 14 37
92 35 80 41
81 74 93 81
18 64 73 44
1 63 6 43
8 19 36 10
47 46 56 63
81 34 51 34
15 5 31 66
40 67 37 78
69 95 77 92
49 87 88 44
59 56 36 96
2 25 93 6
54 16 4 89
97 76 21 72
13 24 79 87
88 41 66 41
31 53 91 66
83 39 28 54
9 39 8 79
39 28 83 22
8...

output:

970083585
468439101
702708882
361058188
286356617
216643969
789090532
745804812
760332031
510418319
89227262
36071722
66406269
348122580
495905506
780300006
103263448
817779478
706395495
778878502
870194500
741118561
870781768
148436812
830347059
417656361
985758040
964132171
889163823
78517671
5364...

result:

ok 8118 numbers

Test #16:

score: 0
Accepted
time: 958ms
memory: 273328kb

input:

100000 3
32 44 5
68 85 73
54 73 84
94 40 65
11 68 78
85 11 49
75 70 68
31 30 36
44 70 10
77 48 92
31 37 68
23 70 76
45 76 40
16 40 33
36 76 3
58 48 89
89 45 14
68 44 36
79 86 81
67 58 23
34 43 67
5 92 17
3 21 81
23 59 73
76 77 89
54 17 48
10 77 49
86 54 30
14 37 45
45 68 44
66 1 59
21 72 54
46 25 61...

output:

657821239
557832902
526733891
405455461
512327394
774740622
451722300
971503418
89575344
409160388
26513920
406848935
585908802
956036971
92428741
797757545
406723554
654954849
254898574
73821667
438908231
632644781
497029378
10049486
855950702
600584181
546853474
460107062
256959150
153078927
61518...

result:

ok 13353 numbers

Test #17:

score: 0
Accepted
time: 997ms
memory: 273336kb

input:

100000 4
1 34 43 58
33 75 88 53
85 73 77 32
96 79 20 15
71 61 73 32
72 42 68 8
93 48 84 45
64 74 21 85
90 29 95 34
74 27 80 37
55 9 20 70
77 77 78 4
53 26 84 66
30 13 56 88
94 97 79 10
9 94 46 23
76 19 96 57
59 77 27 41
68 80 80 3
43 12 88 57
5 72 63 54
74 94 97 95
87 10 88 59
90 79 34 81
30 22 87 6...

output:

158184626
46438465
139960011
600876180
44697286
126543896
236850831
180061456
407651901
953701763
619394894
252320009
77702526
257557368
438333439
656982968
183063460
941107963
13589692
521891566
428345882
979513834
203561651
261654229
3204573
31758119
746462743
436951916
175846782
453983916
8032578...

result:

ok 15724 numbers

Test #18:

score: 0
Accepted
time: 999ms
memory: 273204kb

input:

100000 5
73 65 54 33 54
81 65 3 4 8
59 58 69 11 10
3 55 48 5 27
78 12 79 2 7
48 35 8 74 14
78 24 10 93 2
75 97 90 35 92
60 54 32 52 9
1 93 26 6 53
71 8 91 2 65
78 72 11 60 7
24 10 3 44 51
75 17 35 54 20
90 68 1 52 6
69 21 62 80 47
68 63 85 20 70
44 74 3 46 17
70 72 12 34 39
66 55 24 69 26
6 32 86 5 ...

output:

178851296
162115154
489843274
831874389
520384272
430534929
414601775
171889539
119979929
411670828
615705704
218029567
897060176
700748744
963244040
215172933
995792279
428831608
882734035
660462378
389767553
668275909
540133375
807319711
105670166
606575962
508412606
226324747
533640807
60093347
1...

result:

ok 6817 numbers

Test #19:

score: 0
Accepted
time: 1002ms
memory: 273292kb

input:

100000 5
32 74 16 46 75
80 68 8 15 33
39 97 41 71 59
89 47 18 30 1
66 45 30 89 5
2 85 32 36 50
42 23 76 32 69
95 35 4 46 77
78 96 27 40 76
70 8 70 54 66
4 31 84 92 14
53 2 15 51 72
43 29 53 52 25
85 79 20 35 78
41 76 33 65 58
61 13 61 54 37
58 29 60 72 87
52 79 7 91 25
19 21 42 80 69
43 52 46 18 25
...

output:

725955759
710046583
427576398
993651674
731931091
895785668
523961085
902666201
220828866
518078508
17424232
973201828
399984922
228839424
165140208
53143549
422567786
858181780
431701211
863933380
512985273
689088374
977027776
119748381
256956731
206376383
577241188
390637555
145428164
292073801
96...

result:

ok 9323 numbers

Test #20:

score: 0
Accepted
time: 1011ms
memory: 273368kb

input:

100000 5
30 94 3 3 45
68 30 61 45 22
52 16 57 66 96
78 96 12 96 80
42 77 90 26 83
94 90 53 58 25
31 39 92 86 27
20 27 54 94 48
17 11 63 53 17
29 42 79 81 92
47 94 52 17 43
12 20 21 68 8
72 83 65 67 62
25 54 52 6 64
86 91 42 56 21
35 51 15 55 18
65 59 82 78 95
68 80 85 50 86
5 77 14 30 30
69 80 71 11...

output:

605478808
5119144
445403182
21221335
142846095
588934996
46642807
204397690
5327551
831209476
654072485
334603179
534192008
545420302
825459264
205507704
597130365
521617449
646736700
754643199
103216781
929143067
791350967
891598837
971964043
448295138
262849669
882574577
695805194
380449124
205567...

result:

ok 14589 numbers

Test #21:

score: 0
Accepted
time: 1023ms
memory: 273332kb

input:

100000 5
52 41 62 61 57
23 60 66 56 83
67 55 47 29 13
33 88 79 23 26
74 30 85 51 81
83 44 76 2 61
92 3 26 25 15
40 45 3 70 51
35 9 75 76 85
45 73 9 32 8
16 54 27 89 71
84 46 25 59 91
28 5 19 92 37
34 3 36 40 8
3 1 39 87 91
71 43 49 64 8
20 60 57 15 15
58 85 36 60 94
60 88 61 93 42
81 77 57 4 29
6 44...

output:

185419912
768439949
374112631
745995763
826719432
822261079
588286700
576996967
490423215
614980501
7363742
813130672
42154188
47457075
917341787
115212408
940601676
795487859
590309669
589505982
982552899
207763921
367654465
954049447
209198384
273700729
197431340
365762925
267443836
361350246
8152...

result:

ok 17103 numbers

Test #22:

score: 0
Accepted
time: 1027ms
memory: 273100kb

input:

100000 5
85 26 49 17 71
46 13 85 86 9
80 54 10 86 67
57 75 2 89 8
49 97 48 50 61
78 83 54 24 36
64 81 24 96 51
63 19 70 21 22
89 3 14 10 79
5 54 18 94 52
76 20 14 93 48
7 64 49 59 28
22 59 75 29 74
53 57 6 10 28
12 16 48 96 19
64 81 83 64 85
62 28 17 56 76
11 24 17 54 57
47 65 95 26 38
53 87 82 16 8...

output:

615620508
127890517
476618930
64594793
682823828
252420790
552704013
614011083
331767096
251221155
278485650
142740174
340485180
960775912
761449259
540111993
201059179
558751054
680254505
902536986
787407723
521352466
23980633
652024083
613297227
949509131
389192631
156230012
171984172
953613464
41...

result:

ok 22188 numbers

Test #23:

score: 0
Accepted
time: 1045ms
memory: 273332kb

input:

100000 5
66 63 70 9 42
34 10 41 37 33
93 52 26 46 6
63 78 57 23 52
25 49 11 83 24
37 9 66 89 55
53 62 41 26 44
85 73 58 70 90
90 77 5 84 19
8 88 27 94 78
92 21 79 53 78
89 82 20 42 61
95 78 17 9 76
90 68 19 43 49
40 93 41 87 78
56 4 37 65 48
69 76 74 80 49
9 25 25 48 74
33 24 13 38 62
79 80 10 71 67...

output:

687281850
811218241
305501864
908513812
554542182
98535565
295873110
920639325
861717882
942340257
267684501
861694329
722030650
501829583
282662385
599250933
601193772
885782513
199996974
320025605
558708045
923843030
971980520
638459532
715776608
812453283
208159853
401865772
671523982
612371114
9...

result:

ok 27400 numbers

Extra Test:

score: 0
Extra Test Passed