QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290091#6190. Graph Problemucup-team087#AC ✓1546ms8356kbC++2021.4kb2023-12-24 13:07:162023-12-24 13:07:17

Judging History

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

  • [2023-12-24 13:07:17]
  • 评测
  • 测评结果:AC
  • 用时:1546ms
  • 内存:8356kb
  • [2023-12-24 13:07:16]
  • 提交

answer

#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
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); }
// (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, typename U>
T ceil(T x, U y) {
  return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
  return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

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

#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) {
  assert(!que.empty());
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  assert(!que.empty());
  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;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, 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;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, 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;
}
#endif
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>

namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.write(), std::true_type{});

  template <class T>
  static auto check(...) -> std::false_type;
};

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

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

  template <class T>
  static auto check(...) -> std::false_type;
};

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

struct Scanner {
  FILE *fp;
  char line[(1 << 15) + 1];
  size_t st = 0, ed = 0;
  void reread() {
    memmove(line, line + st, ed - st);
    ed -= st;
    st = 0;
    ed += fread(line + ed, 1, (1 << 15) - ed, fp);
    line[ed] = '\0';
  }
  bool succ() {
    while (true) {
      if (st == ed) {
        reread();
        if (st == ed) return false;
      }
      while (st != ed && isspace(line[st])) st++;
      if (st != ed) break;
    }
    if (ed - st <= 50) {
      bool sep = false;
      for (size_t i = st; i < ed; i++) {
        if (isspace(line[i])) {
          sep = true;
          break;
        }
      }
      if (!sep) reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    while (true) {
      size_t sz = 0;
      while (st + sz < ed && !isspace(line[st + sz])) sz++;
      ref.append(line + st, sz);
      st += sz;
      if (!sz || st != ed) break;
      reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    bool neg = false;
    if (line[st] == '-') {
      neg = true;
      st++;
    }
    ref = T(0);
    while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
    if (neg) ref = -ref;
    return true;
  }
  template <typename T,
            typename enable_if<has_read<T>::value>::type * = nullptr>
  inline bool read_single(T &x) {
    x.read();
    return true;
  }
  bool read_single(double &ref) {
    string s;
    if (!read_single(s)) return false;
    ref = std::stod(s);
    return true;
  }
  bool read_single(char &ref) {
    string s;
    if (!read_single(s) || s.size() != 1) return false;
    ref = s[0];
    return true;
  }
  template <class T>
  bool read_single(vector<T> &ref) {
    for (auto &d: ref) {
      if (!read_single(d)) return false;
    }
    return true;
  }
  template <class T, class U>
  bool read_single(pair<T, U> &p) {
    return (read_single(p.first) && read_single(p.second));
  }
  template <size_t N = 0, typename T>
  void read_single_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
      auto &x = std::get<N>(t);
      read_single(x);
      read_single_tuple<N + 1>(t);
    }
  }
  template <class... T>
  bool read_single(tuple<T...> &tpl) {
    read_single_tuple(tpl);
    return true;
  }
  void read() {}
  template <class H, class... T>
  void read(H &h, T &... t) {
    bool f = read_single(h);
    assert(f);
    read(t...);
  }
  Scanner(FILE *fp) : fp(fp) {}
};

struct Printer {
  Printer(FILE *_fp) : fp(_fp) {}
  ~Printer() { flush(); }

  static constexpr size_t SIZE = 1 << 15;
  FILE *fp;
  char line[SIZE], small[50];
  size_t pos = 0;
  void flush() {
    fwrite(line, 1, pos, fp);
    pos = 0;
  }
  void write(const char val) {
    if (pos == SIZE) flush();
    line[pos++] = val;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  void write(T val) {
    if (pos > (1 << 15) - 50) flush();
    if (val == 0) {
      write('0');
      return;
    }
    if (val < 0) {
      write('-');
      val = -val; // todo min
    }
    size_t len = 0;
    while (val) {
      small[len++] = char(0x30 | (val % 10));
      val /= 10;
    }
    for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
    pos += len;
  }
  void write(const string s) {
    for (char c: s) write(c);
  }
  void write(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++) write(s[i]);
  }
  void write(const double x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  void write(const long double x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  template <typename T,
            typename enable_if<has_write<T>::value>::type * = nullptr>
  inline void write(T x) {
    x.write();
  }
  template <class T>
  void write(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  template <class T, class U>
  void write(const pair<T, U> val) {
    write(val.first);
    write(' ');
    write(val.second);
  }
  template <size_t N = 0, typename T>
  void write_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
      if constexpr (N > 0) { write(' '); }
      const auto x = std::get<N>(t);
      write(x);
      write_tuple<N + 1>(t);
    }
  }
  template <class... T>
  bool write(tuple<T...> tpl) {
    write_tuple(tpl);
    return true;
  }
  template <class T, size_t S>
  void write(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  void write(i128 val) {
    string s;
    bool negative = 0;
    if (val < 0) {
      negative = 1;
      val = -val;
    }
    while (val) {
      s += '0' + int(val % 10);
      val /= 10;
    }
    if (negative) s += "-";
    reverse(all(s));
    if (len(s) == 0) s = "0";
    write(s);
  }
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  printer.write(head);
  if (sizeof...(Tail)) printer.write(' ');
  print(forward<Tail>(tail)...);
}

void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
  scanner.read(head);
  read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __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 "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(q));
  }
  return dat[n];
}

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

template <typename mint>
mint fact_inv(int n) {
  static const int mod = mint::get_mod();
  assert(-1 <= n && n < mod);
  static vector<mint> dat = {1, 1};
  if (n == -1) 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) {
  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 (dense) return C_dense<mint>(n, k);
  if (!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 "library/mod/modint.hpp"

template <int mod>
struct modint {
  static_assert(mod < (1 << 30));
  int val;
  constexpr modint(const ll val = 0) noexcept
      : val(val >= 0 ? val % mod : (mod - (-val) % mod) % mod) {}
  bool operator<(const modint &other) const {
    return val < other.val;
  } // To use std::map
  modint &operator+=(const modint &p) {
    if ((val += p.val) >= mod) val -= mod;
    return *this;
  }
  modint &operator-=(const modint &p) {
    if ((val += mod - p.val) >= mod) val -= mod;
    return *this;
  }
  modint &operator*=(const modint &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  modint &operator/=(const modint &p) {
    *this *= p.inverse();
    return *this;
  }
  modint operator-() const { return modint(-val); }
  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;
  }
#ifdef FASTIO
  void write() { fastio::printer.write(val); }
  void read() { fastio::scanner.read(val); }
#endif
  static constexpr int get_mod() { return mod; }
  // (n, r), r は 1 の 2^n 乗根
  static constexpr pair<int, int> ntt_info() {
    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 == 998244353) return {23, 31};
    if (mod == 1045430273) return {20, 363};
    if (mod == 1051721729) return {20, 330};
    if (mod == 1053818881) return {20, 2789};
    return {-1, -1};
  }
  static constexpr bool can_ntt() { return ntt_info().fi != -1; }
};

using modint107 = modint<1000000007>;
using modint998 = modint<998244353>;
#line 1 "library/linalg/mat_inv.hpp"
// (det, invA) をかえす
template <typename T>
pair<T, vc<vc<T>>> mat_inv(vc<vc<T>> A) {
  T det = 1;
  int N = len(A);
  vv(T, B, N, N);
  FOR(n, N) B[n][n] = 1;
  FOR(i, N) {
    FOR(k, i, N) if (A[k][i] != 0) {
      if (k != i) {
        swap(A[i], A[k]), swap(B[i], B[k]);
        det = -det;
      }
      break;
    }
    if (A[i][i] == 0) return {T(0), {}};
    T c = T(1) / A[i][i];
    det *= A[i][i];
    FOR(j, i, N) A[i][j] *= c;
    FOR(j, N) B[i][j] *= c;
    FOR(k, N) if (i != k) {
      T c = A[k][i];
      FOR(j, i, N) A[k][j] -= A[i][j] * c;
      FOR(j, N) B[k][j] -= B[i][j] * c;
    }
  }
  return {det, B};
}
#line 2 "library/random/base.hpp"

u64 RNG_64() {
  static uint64_t x_
      = uint64_t(chrono::duration_cast<chrono::nanoseconds>(
                     chrono::high_resolution_clock::now().time_since_epoch())
                     .count())
        * 10150724397891781847ULL;
  x_ ^= x_ << 7;
  return x_ ^= x_ >> 9;
}

u64 RNG(u64 lim) { return RNG_64() % lim; }

ll RNG(ll l, ll r) { return l + RNG_64() % (r - l); }
#line 7 "main.cpp"

using mint = modint998;

void solve() {
  LL(N, M);
  vv(mint, A, N, N);
  FOR(M) {
    LL(a, b);
    --a, --b;
    A[a][b] = RNG(mint::get_mod());
  }

  /*
  A : adj mat
  B = inv(I - A)
  */
  vv(mint, B, N, N);
  FOR(i, N) B[i][i] = mint(1);
  FOR(i, N) FOR(j, N) B[i][j] -= A[i][j];
  B = mat_inv<mint>(B).se;

  LL(Q);
  ll cnt = 0;
  FOR(Q) {
    INT(K);
    vc<int> P;
    FOR(K) {
      INT(x);
      x = (x + cnt - 1) % N;
      P.eb(x);
    }
    /*
    U:(N,K), i=P[k]
    V:(K,N), A[P[k]][j] → A の行ベクトルを並べたものの = tU A
    BU → B の列ベクトルを並べたもの

    VBU = tU AB U
    (I-A)B = I
    B - AB = I
    AB = B - I
    tU AB U = tU B U - I
    I + VBU = tU B U → B の P 部分
    */

    vv(mint, C, K, K);
    FOR(i, K) FOR(j, K) C[i][j] = B[P[i]][P[j]];
    C = mat_inv(C).se;

    auto query = [&](int s, int t) -> int {
      // BU C VB の (s,t)
      // BU C tU AB
      // BU C tU B - BU C tU
      mint x = 0;
      FOR(i, K) FOR(j, K) {
        // (BU)[s,i] = B[s][P[i]]
        // tU B[j,t] = B[P[j]][t]
        x += B[s][P[i]] * C[i][j] * B[P[j]][t];
        // I[P[j]][t] は 0 なので、BU C tU 側はなし
      }
      return (x == B[s][t] ? 0 : 1);
    };

    string ANS;
    INT(K2);
    FOR(K2) {
      INT(s, t);
      --s, --t;
      s = (s + cnt) % N;
      t = (t + cnt) % N;
      int ans = query(s, t);
      ANS += '0' + ans;
      cnt += ans;
    }
    print(ANS);
  }
}

signed main() {
  solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3720kb

input:

5 4
1 2
2 3
3 4
4 5
2
1 4
2 1 5 1 3
3 5 3 4
1 1 2

output:

01
1

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 3ms
memory: 3848kb

input:

100 4870
1 4
1 9
2 1
2 6
2 8
4 5
4 10
5 2
5 3
5 7
6 2
6 4
6 8
7 1
7 2
7 8
7 10
8 1
8 2
8 3
8 4
8 5
8 6
8 7
8 9
8 13
8 16
8 17
9 1
9 2
9 3
9 4
9 5
9 6
9 7
9 8
9 18
10 1
10 2
10 3
10 4
10 5
10 6
10 7
10 11
10 15
11 1
11 2
11 3
11 4
11 5
11 6
11 7
11 8
11 9
11 10
11 12
11 18
11 20
12 1
12 2
12 3
12 4
1...

output:

1011000010
1010110101
0001010000
0000101001
1000010000
0111001101
0011001101
1100010010
0001100010
0010110101
0011001111
0001000101
1101010010
0100001100
1000100001
0100000000
1110100000
0101111010
0111001001
1110000000
1011000011
0110000000
0000000100
0001011000
1000111000
1111000010
1000110000
011...

result:

ok 100 lines

Test #3:

score: 0
Accepted
time: 3ms
memory: 4200kb

input:

100 4839
1 2
1 7
1 12
1 13
1 15
1 17
1 21
1 22
1 24
2 1
2 4
2 5
2 7
2 12
2 16
2 19
2 22
2 24
3 7
3 8
3 13
3 20
3 22
4 8
4 12
4 14
4 19
5 2
5 3
5 4
5 6
5 7
5 10
5 13
5 14
5 19
5 24
6 3
6 7
6 8
6 10
6 12
6 13
6 15
6 17
6 19
6 21
6 23
6 24
7 2
7 6
7 8
7 9
7 10
7 12
7 13
7 16
7 23
8 1
8 9
8 11
8 12
8 13...

output:

1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111100111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
111...

result:

ok 100 lines

Test #4:

score: 0
Accepted
time: 3ms
memory: 3896kb

input:

100 4650
1 7
1 12
1 20
1 22
2 7
2 9
2 10
2 11
2 14
2 18
2 20
3 1
3 4
3 8
3 12
3 14
3 16
3 17
3 20
3 21
3 22
4 1
4 6
4 8
4 11
4 12
4 13
4 16
4 21
5 2
5 4
5 9
5 16
5 19
5 21
6 7
6 9
6 10
6 21
7 2
7 8
7 9
7 19
7 20
7 22
8 5
8 7
8 11
8 12
8 14
8 21
9 1
9 5
9 6
9 7
9 8
9 12
9 17
9 21
9 22
10 4
10 9
10 12...

output:

1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
111...

result:

ok 100 lines

Test #5:

score: 0
Accepted
time: 3ms
memory: 3960kb

input:

100 4945
1 2
1 3
1 5
1 7
1 8
1 9
1 10
1 13
1 15
2 1
2 3
2 4
2 9
2 11
2 12
2 15
3 1
3 10
3 12
3 16
4 1
4 5
4 10
4 12
4 13
4 15
4 16
5 6
5 11
5 16
5 17
6 2
6 10
6 11
6 14
7 1
7 2
7 3
7 4
7 5
7 6
7 8
7 9
7 10
7 11
7 16
7 21
7 22
7 23
7 24
7 25
7 27
7 28
7 30
7 31
7 32
7 35
7 38
7 39
8 1
8 2
8 3
8 4
8 5...

output:

1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1110101101
1111111111
1111111111
1111111111
1111111111
1100011011
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
111...

result:

ok 100 lines

Test #6:

score: 0
Accepted
time: 458ms
memory: 8268kb

input:

500 124582
1 12
1 14
2 4
2 7
2 9
2 13
2 14
2 18
3 1
3 4
3 7
3 14
3 17
4 1
4 3
4 6
4 12
4 15
4 18
5 3
6 1
6 3
6 8
6 10
6 11
6 13
6 15
6 17
6 18
7 3
7 4
7 5
7 8
7 9
7 17
8 2
8 6
9 10
9 13
9 17
10 2
10 5
10 17
11 1
11 2
11 3
11 4
11 5
11 6
11 7
11 8
11 9
11 10
11 17
11 18
11 19
11 26
12 1
12 2
12 3
12 ...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 100000 lines

Test #7:

score: 0
Accepted
time: 455ms
memory: 8236kb

input:

500 125402
1 3
1 7
1 10
1 13
1 16
1 21
1 23
1 25
1 28
1 36
1 37
2 3
2 4
2 6
2 7
2 9
2 11
2 16
2 17
2 19
2 20
2 24
2 26
2 28
2 30
2 37
3 2
3 18
3 19
3 21
3 25
3 26
3 32
4 7
4 9
4 10
4 11
4 12
4 14
4 15
4 18
4 19
4 24
4 33
4 35
4 36
5 1
5 4
5 10
5 11
5 17
5 19
5 20
5 21
5 23
5 24
5 31
5 32
5 36
5 38
5...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
0
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 100000 lines

Test #8:

score: 0
Accepted
time: 457ms
memory: 8228kb

input:

500 123930
1 6
1 11
1 13
1 16
1 22
1 25
1 26
1 34
1 38
1 41
1 42
1 43
1 44
1 46
1 48
1 49
1 51
1 53
1 56
2 6
2 9
2 13
2 15
2 24
2 31
2 33
2 36
2 37
2 43
2 44
2 50
2 53
2 56
3 2
3 4
3 5
3 16
3 18
3 19
3 25
3 31
3 32
3 40
3 43
3 44
3 47
3 48
3 50
3 51
3 53
4 1
4 2
4 3
4 5
4 6
4 8
4 14
4 18
4 20
4 26
4...

output:

0
1
1
0
1
1
1
1
1
1
1
1
0
1
1
0
1
1
0
1
1
0
0
1
0
1
0
1
0
1
1
1
0
1
0
0
1
0
0
1
1
1
0
1
1
0
0
1
0
0
0
0
1
0
1
0
0
1
0
0
1
0
1
0
0
0
0
0
1
0
0
1
0
0
0
1
0
0
1
0
0
1
1
0
1
1
0
0
1
0
1
0
1
1
0
1
0
0
0
1
0
1
1
1
1
0
0
0
1
0
1
1
0
0
1
1
0
1
1
0
1
1
0
0
0
1
1
0
0
1
1
0
0
0
0
1
1
1
1
1
1
0
0
0
0
1
0
1
1
1
...

result:

ok 100000 lines

Test #9:

score: 0
Accepted
time: 453ms
memory: 8188kb

input:

500 123484
1 5
1 8
1 10
1 11
1 12
1 20
1 23
1 26
1 27
1 28
1 29
1 31
1 33
1 41
2 4
2 7
2 10
2 18
2 20
2 34
2 35
2 39
2 41
2 42
2 44
2 46
2 52
3 4
3 5
3 6
3 7
3 9
3 14
3 17
3 19
3 23
3 25
3 26
3 32
3 36
3 38
3 39
3 40
3 44
3 46
3 48
3 51
4 1
4 7
4 11
4 12
4 14
4 16
4 17
4 19
4 21
4 23
4 24
4 25
4 29
...

output:

0
0
1
1
1
0
1
1
1
1
0
0
0
1
0
1
1
1
1
0
0
1
1
0
0
0
1
1
1
0
0
1
1
0
0
0
1
1
0
1
0
0
0
1
1
1
1
1
0
0
0
0
0
1
1
0
0
1
0
0
1
1
1
0
1
0
1
0
0
1
1
0
0
1
1
0
0
1
0
1
0
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
0
0
0
0
0
1
0
0
0
1
1
0
0
1
1
0
0
1
1
1
0
1
1
1
1
1
0
1
0
1
0
1
0
1
1
1
1
1
0
1
1
1
1
1
0
0
1
0
0
0
1
0
1
0
...

result:

ok 100000 lines

Test #10:

score: 0
Accepted
time: 1330ms
memory: 8252kb

input:

500 3278
2 5
2 7
3 5
4 6
4 9
5 6
5 7
5 8
6 8
6 9
6 10
7 8
7 9
8 9
9 13
9 20
9 22
9 23
9 24
9 27
9 29
9 31
10 12
10 14
10 17
10 21
10 23
10 24
10 25
10 26
10 27
11 15
11 17
11 20
11 21
11 25
12 16
12 20
12 21
12 22
12 24
12 28
12 32
12 33
12 39
12 42
12 45
12 46
12 47
12 48
13 14
13 20
13 21
13 23
13...

output:

1011111110
1010010110
0011111011
0101110101
0101011111
1111100001
1111111101
1110000110
1111111011
1111101011
1100101011
1110110111
1100111011
1110100100
1010011111
1111110111
0110111111
1111011100
1110011111
1001101000
1110100111
0111110111
1110111110
1101110001
1111010111
0100100011
1111010011
111...

result:

ok 400000 lines

Test #11:

score: 0
Accepted
time: 1320ms
memory: 8284kb

input:

500 3720
1 3
1 8
1 9
1 11
1 12
1 13
1 18
1 24
1 25
2 4
2 6
2 9
2 10
2 12
2 13
2 18
2 23
2 24
3 8
3 15
3 18
3 19
3 22
3 25
3 26
4 10
4 15
4 17
4 19
5 7
5 9
5 10
5 12
5 13
5 14
5 21
6 8
6 9
6 13
6 16
6 24
7 9
7 11
7 13
7 14
7 15
7 16
7 18
7 26
8 10
8 15
8 18
8 19
8 21
8 22
9 14
9 15
9 16
9 20
9 21
9 2...

output:

1111111101
1111111100
1110111110
1111111110
1111111111
1111111110
1101111011
1111111111
1111110111
1111111101
1111111111
0001010100
1111111111
1111111111
0011110111
1111111101
1111111111
1111111111
1110111111
1111111111
1011110011
1011111111
1111101101
1111111110
1111111111
1111111110
1111001111
110...

result:

ok 400000 lines

Test #12:

score: 0
Accepted
time: 1315ms
memory: 8100kb

input:

500 4232
1 2
1 6
1 14
1 16
2 4
2 6
2 10
2 16
2 17
2 18
2 25
2 26
2 28
3 4
3 6
3 12
3 13
3 17
3 18
3 25
4 8
4 12
4 18
4 23
4 28
5 8
5 9
5 10
5 12
5 13
5 17
5 18
5 21
5 26
5 28
6 8
6 9
6 12
6 23
6 24
6 25
6 26
6 27
6 29
7 15
7 23
7 24
7 25
7 28
8 12
8 14
8 16
8 19
8 29
9 12
9 13
9 15
9 16
9 17
9 20
9 ...

output:

1111011011
1111111111
1111111111
0111111011
1111111011
1110111111
0111111110
1110110111
1110001110
1011111111
0111111111
1111111111
1011111110
1111111111
1011111111
1111111111
1111011111
1111111011
1110111111
0111111111
0111110111
1111101111
0111110111
0111111111
1111111111
1111110011
1111111111
111...

result:

ok 400000 lines

Test #13:

score: 0
Accepted
time: 1336ms
memory: 8212kb

input:

500 6443
1 5
1 9
1 12
1 15
1 18
1 20
1 21
1 22
1 24
1 26
1 27
1 30
1 34
1 35
1 37
1 40
1 42
1 45
1 47
1 48
1 49
1 55
1 58
1 64
1 67
1 68
1 71
1 72
2 3
2 6
2 8
2 9
2 16
2 17
2 19
2 23
2 26
2 29
2 31
2 35
2 36
2 37
2 38
2 41
2 45
2 47
2 52
2 53
2 57
2 69
2 70
2 73
2 76
2 77
3 6
3 12
3 14
3 15
3 20
3 2...

output:

1001111111
0101111010
1111011111
1111111010
1111101111
0111111111
0110111111
1110111111
1111101101
1011111111
1111111111
1111111101
1110111110
1101111111
1011111111
1011111111
1111111111
1111110110
1111110101
1011111011
1111101110
1111111010
0011101101
1011111101
1111101111
1111100110
1101101000
111...

result:

ok 400000 lines

Test #14:

score: 0
Accepted
time: 1521ms
memory: 8356kb

input:

500 123824
2 4
2 6
3 1
3 10
4 2
4 6
4 7
4 8
5 2
5 3
5 4
5 7
5 8
5 11
6 7
7 3
7 5
7 6
7 8
7 11
8 2
8 4
9 1
9 2
9 3
9 4
9 5
9 6
9 7
9 8
9 12
9 14
9 17
9 21
9 23
9 24
9 25
9 26
9 27
10 1
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 13
10 15
10 18
10 19
10 23
11 1
11 2
11 3
11 4
11 5
11 6
11 7
11 8
11 10
11 15...

output:

0111110011
0110111111
1011001110
0101001011
0111110111
0011011000
1011110010
1000001101
1111000111
0101101111
1000001010
1010101010
0100000100
1011101110
1100011000
0000111110
0010100011
0110010101
1000111111
1101000001
0111110010
0011110100
0011101010
1001001111
0001010011
1010110110
0110111110
000...

result:

ok 400000 lines

Test #15:

score: 0
Accepted
time: 1521ms
memory: 8220kb

input:

500 124816
1 3
1 8
1 9
1 11
1 12
1 13
1 18
1 24
1 25
2 3
2 5
2 8
2 9
2 11
2 12
2 17
2 22
2 23
3 5
3 12
3 15
3 16
3 19
3 22
3 23
4 3
4 9
4 11
4 13
4 22
4 24
4 25
5 1
5 2
5 3
5 11
5 18
5 19
5 23
5 26
6 9
6 13
6 15
6 17
6 18
6 19
6 20
6 22
7 4
7 6
7 12
7 15
7 16
7 18
7 19
8 2
8 3
8 4
8 9
8 10
8 11
8 16...

output:

1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
111...

result:

ok 400000 lines

Test #16:

score: 0
Accepted
time: 1546ms
memory: 8212kb

input:

500 124872
1 2
1 6
1 14
1 16
2 1
2 4
2 6
2 10
2 16
2 17
2 18
2 25
2 26
2 28
3 1
3 2
3 5
3 11
3 12
3 16
3 17
3 24
4 1
4 5
4 9
4 15
4 20
4 25
4 30
5 1
5 2
5 3
5 6
5 7
5 11
5 12
5 15
5 20
5 22
5 26
5 27
5 30
6 1
6 13
6 14
6 15
6 16
6 17
6 19
6 28
7 1
7 8
7 9
7 10
7 13
7 19
7 21
7 23
7 26
8 1
8 7
8 12
8...

output:

0001111111
1101111010
1110011011
1011001101
0110111111
0000111000
1101010111
0101001111
1001101011
1111111101
1111110011
0010011010
0001000001
0101101111
1111000101
1100100001
0011010101
1010010110
0011111011
0000011011
1100010000
1011011011
1111010001
0110111011
0111000111
0111111000
1111000111
011...

result:

ok 400000 lines

Test #17:

score: 0
Accepted
time: 1524ms
memory: 8088kb

input:

500 124293
1 5
1 9
1 12
1 15
1 18
1 20
1 21
1 22
1 24
1 26
1 27
1 30
1 34
1 35
1 37
1 40
1 42
1 45
1 47
1 48
1 49
1 55
1 58
1 64
1 67
1 68
1 71
1 72
2 1
2 5
2 7
2 8
2 15
2 16
2 18
2 22
2 25
2 28
2 30
2 34
2 35
2 36
2 37
2 40
2 44
2 46
2 51
2 52
2 56
2 68
2 69
2 72
2 75
2 76
3 2
3 9
3 11
3 12
3 17
3 ...

output:

1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111101
1111111111
1111111111
1111111111
1111111111
1100100100
1111111111
1111111111
1111111111
111...

result:

ok 400000 lines