結果

問題 No.3285 Chorus with Friends
ユーザー Kude
提出日時 2025-09-26 23:13:54
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,124 ms / 3,000 ms
コード長 3,656 bytes
コンパイル時間 3,963 ms
コンパイル使用メモリ 316,172 KB
実行使用メモリ 23,136 KB
最終ジャッジ日時 2025-09-26 23:14:08
合計ジャッジ時間 13,572 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

template <int sz>
vector<array<mint, sz>> ranked_zeta(const vector<mint>& f) {
  int N = f.size();
  vector<array<mint, sz>> fr(N);
  for (int i = 0; i < N; i++) fr[i][__builtin_popcount(i)] = f[i];
  for (int k = 1; k < N; k <<= 1) {
    for (int i = k; i < N; i = (i + 1) | k) {
      for (int p = 0; p < sz; p++) {
        fr[i][p] += fr[i ^ k][p];
      }
    }
  }
  return fr;
}

template <int sz>
vector<mint> ranked_mobius(vector<array<mint, sz>> fr) {
  int N = fr.size();
  for (int k = 1; k < N; k <<= 1) {
    for (int i = k; i < N; i = (i + 1) | k) {
      for (int p = 0; p < sz; p++) {
        fr[i][p] -= fr[i ^ k][p];
      }
    }
  }
  vector<mint> f(N);
  for (int i = 0; i < N; i++) f[i] = fr[i][__builtin_popcount(i)];
  return f;
}

template <int sz>
vector<mint> subset_convolution(const vector<mint>& f, const vector<mint>& g) {
  assert(size(f) == size(g));
  const int N = size(f), n = __builtin_ctz(size(f));
  assert(N == 1 << n);
  assert(sz >= n);
  auto fr = ranked_zeta<sz + 1>(f);
  auto gr = ranked_zeta<sz + 1>(g);
  for (int i = 0; i < N; i++) {
    for (int p = sz; p >= 0; p--) {
      for (int q = sz - p; q > 0; q--) {
        fr[i][p + q] += fr[i][p] * gr[i][q];
      }
      fr[i][p] *= gr[i][0];
    }
  }
  return ranked_mobius<sz + 1>(move(fr));
}


} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  if (n < m) {
    VVI a(m, VI(n));
    rep(i, n) rep(j, m) cin >> a[j][i];
    swap(n, m);
    set<int> cand;
    rep(j, m) cand.insert(a[0][j]);
    mint ans;
    for (int x : cand) {
      vector<mint> dp(1 << m), ndp;
      int s0 = 0;
      rep(j, m) s0 |= (a[0][j] == x) << j;
      dp[s0] = 1;
      rep(i, n) {
        ndp.assign(1 << m, 0);
        VI nbit(m);
        rep(j, m) nbit[j] = (i + 1 < n && a[i+1][j] == x) << j;
        rep(s, 1 << m) if (mint v = dp[s]; v.val()) {
          rep(j, m) if (s >> j & 1) {
            ndp[(s ^ (1 << j)) | nbit[j]] += v;
          }
        }
        swap(dp, ndp);
      }
      ans += accumulate(all(dp), mint());
    }
    cout << ans.val() << '\n';
    return 0;
  } else {
    VVI a(n, VI(m));
    rep(i, n) rep(j, m) cin >> a[i][j];
    set<int> cand;
    rep(i, n) cand.insert(a[i][0]);
    mint ans;
    for (int x : cand) {
      vector<mint> dp(1 << m), ndp;
      dp[0] = 1;
      rep(i, n) if (a[i][0] == x) {
        ndp.assign(1 << m, mint());
        ndp[0] = 1;
        rep(s, 1 << m) if (s) {
          int b = m - 1;
          while (~s >> b & 1) b--;
          b--;
          bool ok = true;
          for (; b >= 0; b--) if (s >> b & 1) {
            if (a[i][b+1] != x) {
              ok = false;
              break;
            }
          }
          if (ok) ndp[s] = 1;
        }
        dp = subset_convolution<17>(dp, ndp);
        dp.resize(1 << m);
      }
      ans += dp.back();
    }
    cout << ans.val() << '\n';
  }
}
0