QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736530#7623. Coloring TapemaspyAC ✓131ms5624kbC++2321.8kb2024-11-12 11:39:302024-11-12 11:39:31

Judging History

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

  • [2024-11-12 11:39:31]
  • 评测
  • 测评结果:AC
  • 用时:131ms
  • 内存:5624kb
  • [2024-11-12 11:39:30]
  • 提交

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 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) {
  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 2 "/home/maspy/compro/library/setfunc/zeta.hpp"

template <typename T>
void superset_zeta(vc<T>& A) {
  int log = topbit(len(A));
  assert(1 << log == len(A));
  FOR(n, log) FOR(s, 1 << log) {
    int t = s ^ (1 << n);
    if (s < t) A[s] += A[t];
  }
}

template <typename T>
void superset_mobius(vc<T>& A) {
  int log = topbit(len(A));
  assert(1 << log == len(A));
  FOR(n, log) FOR(s, 1 << log) {
    int t = s ^ (1 << n);
    if (s < t) A[s] -= A[t];
  }
}

template <typename T>
void subset_zeta(vc<T>& A) {
  int log = topbit(len(A));
  assert(1 << log == len(A));
  FOR(n, log) FOR(s, 1 << log) {
    int t = s ^ (1 << n);
    if (s > t) A[s] += A[t];
  }
}

template <typename T>
void subset_mobius(vc<T>& A) {
  int log = topbit(len(A));
  assert(1 << log == len(A));
  FOR(n, log) FOR(s, 1 << log) {
    int t = s ^ (1 << n);
    if (s > t) A[s] -= A[t];
  }
}
#line 6 "main.cpp"

using mint = modint998;

/*
2^14 状態
それこそsame,diffの列で2^13遷移?

2^13遷移を列挙、左右の状態を列挙
上下も決める
右側は終点ごとに出るかどうかを決める

状態おおすぎ!
終点集合に遷移したあとゼータ変換というふうに分解する
*/

void solve() {
  LL(N, M, R);
  vvc<pair<int, int>> edge(1 << (N - 1));
  vvc<int> color(1 << (N - 1));
  FOR(s, 1 << (N - 1)) {
    vc<int> cut;
    cut.eb(0);
    FOR(i, N - 1) if (s >> i & 1) cut.eb(i + 1);
    cut.eb(N);
    vc<int> A(N);
    FOR(k, len(cut) - 1) FOR(i, cut[k], cut[k + 1]) A[i] = k;
    color[s] = A;
    auto dfs = [&](auto& dfs, int p, int L, int R) -> void {
      if (p == len(cut) - 1) {
        // FOR_subset(r, R) { edge[s].eb(L, r); }
        edge[s].eb(L, R);
        return;
      }
      int a = cut[p], b = cut[p + 1];
      // [a,b)
      if (b == a + 1) {
        dfs(dfs, p + 1, L | 1 << a, R | 1 << a);
      } else {
        dfs(dfs, p + 1, L | 1 << a, R | 1 << (b - 1));
        dfs(dfs, p + 1, L | 1 << (b - 1), R | 1 << a);
      }
    };
    dfs(dfs, 0, 0, 0);
  }

  // ll cnt = 0;
  // FOR(i, len(edge)) cnt += len(edge[i]);
  // SHOW(cnt);

  // col -> x,y,diff
  vvc<tuple<int, int, int>> cond(M);
  FOR(R) {
    INT(c, x, y, diff);
    --c, --x, --y;
    cond[c].eb(x, y, diff);
  }

  vc<mint> dp(1 << N);
  dp[(1 << N) - 1] = 1;
  // 最初の列は全部だよね
  FOR(c, M) {
    vc<mint> newdp(1 << N);
    FOR(s, 1 << (N - 1)) {
      bool valid = 1;
      for (auto& [x, y, diff]: cond[c]) {
        if (diff != (color[s][x] != color[s][y])) valid = 0;
      }
      if (!valid) continue;
      for (auto& [L, R]: edge[s]) newdp[R] += dp[L];
    }
    swap(dp, newdp);
    superset_zeta(dp);
  }
  print(dp[0]);
}

signed main() { solve(); }

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

19

result:

ok 1 number(s): "19"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3524kb

input:

5 10 10
9 3 4 1
2 4 5 0
7 2 3 0
9 2 3 0
6 3 5 0
6 2 4 1
2 4 5 0
1 1 3 1
7 2 4 0
10 2 3 0

output:

1514

result:

ok 1 number(s): "1514"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3644kb

input:

5 10 20
8 4 5 0
2 2 5 1
8 4 5 0
10 3 5 0
7 1 3 1
1 2 4 1
6 3 5 1
10 3 5 0
4 1 5 1
7 3 4 1
2 2 4 1
8 3 4 0
9 3 5 0
5 2 5 1
9 4 5 0
9 1 2 0
6 1 5 1
8 3 5 0
2 2 4 1
8 3 5 0

output:

28131

result:

ok 1 number(s): "28131"

Test #4:

score: 0
Accepted
time: 2ms
memory: 3744kb

input:

10 100 200
95 5 7 0
7 4 6 1
62 9 10 0
32 5 8 1
31 2 6 1
75 7 9 1
1 4 7 1
18 7 10 1
75 1 8 1
87 6 9 1
44 7 8 1
68 6 9 1
95 4 6 0
34 1 2 1
70 1 6 1
31 5 9 1
15 6 10 1
48 5 8 1
51 3 7 1
39 5 9 1
23 2 3 1
7 8 9 1
84 7 10 1
13 4 9 1
18 3 6 1
59 9 10 0
31 8 10 1
6 7 9 1
76 3 10 1
41 5 6 0
33 3 4 1
96 1 10...

output:

655333622

result:

ok 1 number(s): "655333622"

Test #5:

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

input:

10 200 200
106 9 10 0
93 4 10 1
199 3 7 0
73 2 9 1
105 8 9 0
38 9 10 1
73 8 10 1
153 3 9 1
123 2 5 1
159 7 9 0
154 5 7 1
162 3 7 0
113 1 5 1
131 7 9 1
67 4 6 1
178 6 10 0
157 7 9 0
147 9 10 0
154 7 10 0
123 3 4 1
39 8 10 1
139 2 9 1
191 9 10 0
36 4 5 1
17 2 8 1
124 3 7 1
9 9 10 1
71 9 10 1
181 7 8 0...

output:

552037151

result:

ok 1 number(s): "552037151"

Test #6:

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

input:

10 300 200
252 1 5 0
48 9 10 1
18 9 10 1
233 9 10 0
195 2 9 1
125 2 5 1
263 7 9 1
24 1 6 1
258 2 10 1
272 8 10 1
76 5 7 1
147 1 7 1
93 9 10 1
30 6 9 1
10 1 10 1
56 2 10 1
93 8 9 1
206 6 9 1
65 1 9 1
226 3 5 0
88 7 8 1
151 3 4 1
292 9 10 0
129 2 3 1
292 9 10 0
180 7 10 1
4 5 10 1
10 9 10 1
151 4 7 1
...

output:

4494096

result:

ok 1 number(s): "4494096"

Test #7:

score: 0
Accepted
time: 2ms
memory: 3684kb

input:

10 500 300
210 4 7 1
341 8 9 0
371 2 5 0
21 4 10 1
370 8 9 0
368 1 6 0
395 7 9 0
287 6 10 1
299 3 7 1
379 1 9 1
164 4 10 1
390 7 9 0
455 6 9 0
208 8 10 1
402 3 10 0
112 8 10 1
279 3 10 1
180 7 10 1
456 2 6 0
121 5 6 1
312 5 7 0
335 8 10 0
318 2 10 1
497 8 10 0
108 8 9 0
247 3 6 1
155 5 6 1
308 1 2 0...

output:

705403853

result:

ok 1 number(s): "705403853"

Test #8:

score: 0
Accepted
time: 24ms
memory: 4136kb

input:

12 500 300
115 3 10 1
152 10 12 1
89 8 12 1
276 4 7 0
467 6 7 0
405 5 9 0
189 4 9 1
197 1 3 1
341 7 8 0
67 7 8 1
266 2 6 1
78 8 12 1
317 11 12 0
417 8 10 0
380 2 8 0
255 2 5 1
80 7 9 1
317 5 11 1
470 5 9 0
373 3 4 0
413 4 10 0
393 9 12 0
362 8 10 1
42 7 12 1
486 3 5 0
229 1 5 0
224 6 7 0
55 3 10 1
4...

output:

378086467

result:

ok 1 number(s): "378086467"

Test #9:

score: 0
Accepted
time: 25ms
memory: 4420kb

input:

12 500 500
54 11 12 1
325 10 11 0
83 2 3 1
148 3 10 1
165 3 11 1
16 11 12 1
363 8 10 1
78 11 12 1
258 4 12 1
237 8 11 1
403 2 10 1
354 1 9 1
234 4 7 1
454 9 11 0
160 11 12 1
393 1 3 0
375 9 11 0
494 1 3 0
200 6 12 1
414 11 12 0
217 9 10 0
92 5 9 1
172 5 6 1
110 8 12 1
339 4 12 1
429 2 4 0
29 10 11 1...

output:

948753642

result:

ok 1 number(s): "948753642"

Test #10:

score: 0
Accepted
time: 119ms
memory: 5516kb

input:

14 500 500
362 4 12 1
225 5 9 1
428 5 9 1
101 8 10 1
488 5 9 0
249 11 14 1
232 2 6 1
220 4 10 1
20 7 13 1
154 4 13 1
480 8 14 0
9 2 4 1
201 7 10 1
174 10 11 0
169 13 14 0
256 10 12 1
403 11 13 0
492 10 14 0
167 6 12 1
123 11 13 1
471 9 10 0
77 5 9 1
51 6 10 1
411 11 14 1
422 11 13 0
7 1 7 1
284 5 11...

output:

103280588

result:

ok 1 number(s): "103280588"

Test #11:

score: 0
Accepted
time: 130ms
memory: 5380kb

input:

14 500 0

output:

750061283

result:

ok 1 number(s): "750061283"

Test #12:

score: 0
Accepted
time: 125ms
memory: 5456kb

input:

14 495 0

output:

662120858

result:

ok 1 number(s): "662120858"

Test #13:

score: 0
Accepted
time: 127ms
memory: 5484kb

input:

14 490 0

output:

456608006

result:

ok 1 number(s): "456608006"

Test #14:

score: 0
Accepted
time: 131ms
memory: 5384kb

input:

14 500 5
123 7 12 1
24 13 14 1
170 6 13 1
304 2 8 1
475 10 11 0

output:

715116697

result:

ok 1 number(s): "715116697"

Test #15:

score: 0
Accepted
time: 129ms
memory: 5448kb

input:

14 500 10
237 5 9 1
36 3 14 1
470 5 13 1
315 4 6 1
28 9 12 1
220 11 14 0
160 9 12 1
312 10 11 0
72 7 12 1
230 8 11 0

output:

434022866

result:

ok 1 number(s): "434022866"

Test #16:

score: 0
Accepted
time: 130ms
memory: 5624kb

input:

14 500 15
339 5 10 1
326 4 7 1
421 12 14 0
225 13 14 1
307 1 3 0
285 2 4 0
33 8 10 1
226 2 3 0
478 13 14 1
347 5 11 1
138 5 13 1
141 9 14 1
417 2 8 1
172 6 11 1
222 7 14 1

output:

268520991

result:

ok 1 number(s): "268520991"

Test #17:

score: 0
Accepted
time: 130ms
memory: 5476kb

input:

14 500 20
357 5 14 1
296 10 14 1
490 9 10 0
221 11 12 1
490 12 13 0
469 5 13 1
93 2 8 1
482 12 14 0
461 2 7 1
152 2 13 1
34 8 14 1
60 9 12 1
195 4 5 0
1 6 8 1
3 5 11 1
129 11 13 1
124 13 14 1
434 11 13 0
141 4 5 1
80 6 12 1

output:

691528902

result:

ok 1 number(s): "691528902"

Test #18:

score: 0
Accepted
time: 124ms
memory: 5340kb

input:

14 500 100
85 13 14 0
130 2 7 0
38 5 10 0
450 1 2 1
103 8 10 0
410 11 14 1
39 10 14 0
29 3 4 0
98 9 11 0
226 6 9 1
17 5 6 0
475 9 12 1
337 12 13 1
42 10 11 0
457 8 10 1
49 1 2 0
222 9 13 0
105 7 11 0
403 6 8 1
151 2 8 0
13 11 12 0
483 10 14 1
304 5 9 1
197 5 14 0
58 4 7 0
482 1 12 1
331 12 13 1
398 ...

output:

0

result:

ok 1 number(s): "0"

Test #19:

score: 0
Accepted
time: 119ms
memory: 5264kb

input:

14 498 200
457 10 13 0
163 6 10 0
23 2 5 0
109 5 8 0
113 12 14 0
294 10 12 0
1 10 14 0
451 1 2 0
275 1 13 0
345 10 14 0
171 2 9 0
392 8 11 0
184 13 14 0
328 10 11 0
84 10 13 0
238 6 12 0
306 6 13 0
56 8 14 0
404 10 14 0
90 3 10 0
446 12 14 0
303 9 11 0
71 11 12 0
362 10 13 0
405 13 14 1
258 4 13 0
1...

output:

0

result:

ok 1 number(s): "0"

Test #20:

score: 0
Accepted
time: 116ms
memory: 5464kb

input:

14 497 300
265 5 12 0
368 6 14 0
400 3 10 0
408 13 14 1
494 9 11 1
8 13 14 0
132 10 14 0
203 4 10 0
86 13 14 0
96 3 9 0
39 11 14 0
439 8 9 0
161 1 13 0
264 1 7 0
176 8 10 0
8 10 12 0
299 2 13 0
285 1 13 0
392 7 8 1
143 11 13 0
84 10 11 1
270 1 9 0
311 8 10 0
39 5 10 0
282 4 11 0
45 9 10 0
465 12 14 ...

output:

0

result:

ok 1 number(s): "0"

Test #21:

score: 0
Accepted
time: 111ms
memory: 5596kb

input:

14 499 500
349 7 10 0
440 11 13 0
391 5 11 0
461 8 10 1
172 12 14 0
139 5 10 0
79 3 4 0
456 10 11 0
276 11 14 0
484 5 6 1
178 11 13 0
295 8 11 0
384 3 8 0
112 9 11 0
170 3 7 0
490 12 14 1
243 7 9 0
360 4 7 0
302 10 12 0
266 5 8 0
350 8 12 0
282 7 12 0
480 7 11 1
312 10 13 0
356 13 14 0
277 4 5 0
245...

output:

0

result:

ok 1 number(s): "0"

Test #22:

score: 0
Accepted
time: 131ms
memory: 5572kb

input:

14 500 3
2 1 2 0
2 2 3 0
2 1 3 1

output:

0

result:

ok 1 number(s): "0"

Test #23:

score: 0
Accepted
time: 0ms
memory: 3916kb

input:

1 500 0

output:

1

result:

ok 1 number(s): "1"

Test #24:

score: 0
Accepted
time: 0ms
memory: 3708kb

input:

4 2 0

output:

17

result:

ok 1 number(s): "17"

Extra Test:

score: 0
Extra Test Passed