Problem p02800 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL << (n))
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v)                                                              \\
  {                                                                            \\
    cout << #v << "=" << endl;                                                 \\
    REP(i_debug, v.size()) { cout << v[i_debug] << ","; }                      \\
    cout << endl;                                                              \\
  }
#define mdebug(m)                                                              \\
  {                                                                            \\
    cout << #m << "=" << endl;                                                 \\
    REP(i_debug, m.size()) {                                                   \\
      REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; }  \\
      cout << endl;                                                            \\
    }                                                                          \\
  }
#define Return(ans)                                                            \\
  {                                                                            \\
    cout << (ans) << endl;                                                     \\
    return 0;                                                                  \\
  }
#define pb push_back
#define fi first
#define se second
#define int long long
#define INF 1000000000000000000
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i != v.size() - 1)
      cout << endl;
  };
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  cout << '(' << p.first << ',' << p.second << ')';
  return os;
}
template <typename T> void Out(T x) { cout << x << endl; }
template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) {
  if (f)
    Out(y);
  else
    Out(n);
}

using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using v_bool = vector<bool>;
using v_Pii = vector<Pii>;

// int dx[4] = {1,0,-1,0};
// int dy[4] = {0,1,0,-1};
// char d[4] = {'D','R','U','L'};

// const int mod = 1000000007;
const int mod = 998244353;

template <long long MOD> struct ModInt {

  using ll = long long;
  ll val;

  void setval(ll v) { val = v % MOD; };
  ModInt() : val(0) {}
  ModInt(ll v) { setval(v); };

  ModInt operator+(const ModInt &x) const { return ModInt(val + x.val); }
  ModInt operator-(const ModInt &x) const { return ModInt(val - x.val + MOD); }
  ModInt operator*(const ModInt &x) const { return ModInt(val * x.val); }
  ModInt operator/(const ModInt &x) const { return *this * x.inv(); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt operator+=(const ModInt &x) { return *this = *this + x; }
  ModInt operator-=(const ModInt &x) { return *this = *this - x; }
  ModInt operator*=(const ModInt &x) { return *this = *this * x; }
  ModInt operator/=(const ModInt &x) { return *this = *this / x; }

  friend ostream &operator<<(ostream &os, const ModInt &x) {
    os << x.val;
    return os;
  }
  friend istream &operator>>(istream &is, ModInt &x) {
    is >> x.val;
    x.val = (x.val % MOD + MOD) % MOD;
    return is;
  }

  ModInt pow(ll n) const {
    ModInt a = 1;
    if (n == 0)
      return a;
    int i0 = 64 - __builtin_clzll(n);
    for (int i = i0 - 1; i >= 0; i--) {
      a = a * a;
      if ((n >> i) & 1)
        a *= (*this);
    }
    return a;
  }
  ModInt inv() const { return this->pow(MOD - 2); }
};

using mint = ModInt<mod>;
mint pow(mint x, long long n) { return x.pow(n); }
// using mint = double; //for debug
using mvec = vector<mint>;
using mmat = vector<mvec>;

struct Combination {

  vector<mint> fact, invfact;

  Combination(int N) {
    fact = vector<mint>({mint(1)});
    invfact = vector<mint>({mint(1)});
    fact_initialize(N);
  }

  void fact_initialize(int N) {
    int i0 = fact.size();
    if (i0 >= N + 1)
      return;
    fact.resize(N + 1);
    invfact.resize(N + 1);
    for (int i = i0; i <= N; i++)
      fact[i] = fact[i - 1] * i;
    invfact[N] = (mint)1 / fact[N];
    for (int i = N - 1; i >= i0; i--)
      invfact[i] = invfact[i + 1] * (i + 1);
  }

  mint nCr(int n, int r) {
    if (n < 0 || r < 0 || r > n)
      return mint(0);
    if (fact.size() < n + 1)
      fact_initialize(n);
    return fact[n] * invfact[r] * invfact[n - r];
  }

  mint nPr(int n, int r) {
    if (n < 0 || r < 0 || r > n)
      return mint(0);
    if (fact.size() < n + 1)
      fact_initialize(n);
    return fact[n] * invfact[n - r];
  }

  mint Catalan(int n) {
    if (n < 0)
      return 0;
    else if (n == 0)
      return 1;
    if (fact.size() < 2 * n + 1)
      fact_initialize(2 * n);
    return fact[2 * n] * invfact[n + 1] * invfact[n];
  }
};

signed main() {

  int H, W;
  cin >> H >> W;
  vector<string> A(H);
  cin >> A;

  int n = H + W;
  Combination C(n);
  mmat sti(n + 1);
  sti[0] = mvec(1, 1);
  FOR(i, 1, n + 1) {
    sti[i].resize(i + 1);
    sti[i][0] = 0;
    FOR(j, 1, i) sti[i][j] = sti[i - 1][j - 1] + sti[i - 1][j] * j;
    sti[i][i] = 1;
  }

  mmat dp(H + 1, mvec(W, 0));
  dp[0][0] = 1;
  REP(i, H) {
    REP(j, W) {
      dp[i + 1][j] += dp[i][j] * (j + 2);
      if (j < W - 1)
        dp[i + 1][j + 1] += dp[i][j];
    }
  }

  mmat dq(H + 1, mvec(W + 1, 0));
  REP(h, H + 1) REP(w, W + 1) {
    REP(k, w) dq[h][w] += dp[h][k] * sti[w][k + 1] * C.fact[k + 1] * C.fact[k];
    if (w == 0)
      dq[h][w] = 1;
  }

  mmat dq2(H + 1, mvec(W + 1, 0));
  REP(h, H + 1) REP(w, W + 1) {
    REP(i, h + 1) REP(j, w + 1) {
      dq2[h][w] += dq[i][w - j] * dq[h - i][j] * C.nCr(h, i) * C.nCr(w, j);
    }
  }

  mint ans = dq[H][W];

  mat flgx(bit(H), vec(W, 0)), flgy(bit(W), vec(H, 0));
  REP(x, bit(H)) {
    REP(j1, W) REP(j2, j1) {
      bool f = false;

      REP(i1, H) REP(i2, i1) if ((x >> i1) & (x >> i2) & 1) {
        if (A[i1][j1] == A[i2][j2] && A[i1][j2] == A[i2][j1] &&
            A[i1][j1] != A[i2][j1]) {
          f = true;
        }
      }
      if (f) {
        flgx[x][j1] ^= bit(j2);
        flgx[x][j2] ^= bit(j1);
      }
    }
  }

  REP(y, bit(W)) {
    REP(i1, H) REP(i2, i1) {
      bool f = false;

      REP(j1, W) REP(j2, j1) if ((y >> j1) & (y >> j2) & 1) {
        if (A[i1][j1] == A[i2][j2] && A[i1][j2] == A[i2][j1] &&
            A[i1][j1] != A[i2][j1]) {
          f = true;
        }
      }
      if (f) {
        flgy[y][i1] ^= bit(i2);
        flgy[y][i2] ^= bit(i1);
      }
    }
  }

  vector<v_bool> difx(bit(H), v_bool(W, false)), dify(bit(W), v_bool(H, false));
  REP(x, bit(H)) REP(j, W) {
    REP(i1, H) REP(i2, i1) if ((x >> i1) & (x >> i2) & 1) {
      if (A[i1][j] != A[i2][j])
        difx[x][j] = true;
    }
  }
  REP(y, bit(W)) REP(i, H) {
    REP(j1, W) REP(j2, j1) if ((y >> j1) & (y >> j2) & 1) {
      if (A[i][j1] != A[i][j2])
        dify[y][i] = true;
    }
  }

  REP(x, bit(H)) REP(y, bit(W)) {
    int tx = 0;
    REP(i, H) if ((x >> i) & 1) tx |= flgy[y][i];
    int ty = 0;
    REP(j, W) if ((y >> j) & 1) ty |= flgx[x][j];

    bool f = true;
    int ux = x ^ (x & tx), uy = y ^ (y & ty);
    REP(i, H) if (((ux >> i) & 1) && !dify[y & ty][i]) f = false;
    REP(j, W) if (((uy >> j) & 1) && !difx[x & tx][j]) f = false;

    if (f) {
      int h = H - __builtin_popcountll(x), w = W - __builtin_popcountll(y);
      if (h < H && w < W)
        ans += dq2[h][w];
    }
  }

  Out(ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>; template<typename T, typename L> using
gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>,
rb_tree_tag, tree_order_statistics_node_update>;*/
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL << (n))
#define bit_exist(x, n) ((x >> n) & 1)
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v)                                                              \\
  {                                                                            \\
    cout << #v << "=" << endl;                                                 \\
    REP(i_debug, v.size()) { cout << v[i_debug] << ","; }                      \\
    cout << endl;                                                              \\
  }
#define mdebug(m)                                                              \\
  {                                                                            \\
    cout << #m << "=" << endl;                                                 \\
    REP(i_debug, m.size()) {                                                   \\
      REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; }  \\
      cout << endl;                                                            \\
    }                                                                          \\
  }
#define Return(ans)                                                            \\
  {                                                                            \\
    cout << (ans) << endl;                                                     \\
    return 0;                                                                  \\
  }
#define pb push_back
#define f first
#define s second
#define int long long
#define INF 1000000000000000000
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i != v.size() - 1)
      cout << endl;
  };
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  cout << '(' << p.first << ',' << p.second << ')';
  return os;
}
template <typename T> void Out(T x) { cout << x << endl; }
template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) {
  if (f)
    Out(y);
  else
    Out(n);
}

using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using PiP = pair<int, Pii>;
using PPi = pair<Pii, int>;
using Pdi = pair<double, int>;
using bools = vector<bool>;
using pairs = vector<Pii>;

// int dx[4] = {1,0,-1,0};
// int dy[4] = {0,1,0,-1};
// char d[4] = {'D','R','U','L'};

// const int mod = 1000000007;
const int mod = 998244353;
// #define Add(x, y) x = (x + (y)) % mod
// #define Mult(x, y) x = (x * (y)) % mod

template <long long MOD> struct ModInt {

  using ll = long long;
  ll val;

  void setval(ll v) { val = v % MOD; };
  ModInt() : val(0) {}
  ModInt(ll v) { setval(v); };

  ModInt operator+(const ModInt &x) const { return ModInt(val + x.val); }
  ModInt operator-(const ModInt &x) const { return ModInt(val - x.val + MOD); }
  ModInt operator*(const ModInt &x) const { return ModInt(val * x.val); }
  ModInt operator/(const ModInt &x) const { return *this * x.inv(); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt operator+=(const ModInt &x) { return *this = *this + x; }
  ModInt operator-=(const ModInt &x) { return *this = *this - x; }
  ModInt operator*=(const ModInt &x) { return *this = *this * x; }
  ModInt operator/=(const ModInt &x) { return *this = *this / x; }

  friend ostream &operator<<(ostream &os, const ModInt &x) {
    os << x.val;
    return os;
  }
  friend istream &operator>>(istream &is, ModInt &x) {
    is >> x.val;
    x.val = (x.val % MOD + MOD) % MOD;
    return is;
  }

  ModInt pow(ll n) const {
    ModInt a = 1;
    if (n == 0)
      return a;
    int i0 = 64 - __builtin_clzll(n);
    for (int i = i0 - 1; i >= 0; i--) {
      a = a * a;
      if ((n >> i) & 1)
        a *= (*this);
    }
    return a;
  }
  ModInt inv() const { return this->pow(MOD - 2); }
};

using mint = ModInt<mod>;
mint pow(mint x, long long n) { return x.pow(n); }
// using mint = double; //for debug
using mvec = vector<mint>;
using mmat = vector<mvec>;

struct Combination {

  vector<mint> fact, invfact;

  Combination(int N) {
    fact = vector<mint>({mint(1)});
    invfact = vector<mint>({mint(1)});
    fact_initialize(N);
  }

  void fact_initialize(int N) {
    int i0 = fact.size();
    if (i0 >= N + 1)
      return;
    fact.resize(N + 1);
    invfact.resize(N + 1);
    for (int i = i0; i <= N; i++)
      fact[i] = fact[i - 1] * i;
    invfact[N] = (mint)1 / fact[N];
    for (int i = N - 1; i >= i0; i--)
      invfact[i] = invfact[i + 1] * (i + 1);
  }

  mint nCr(int n, int r) {
    if (n < 0 || r < 0 || r > n)
      return mint(0);
    if (fact.size() < n + 1)
      fact_initialize(n);
    return fact[n] * invfact[r] * invfact[n - r];
  }

  mint nPr(int n, int r) {
    if (n < 0 || r < 0 || r > n)
      return mint(0);
    if (fact.size() < n + 1)
      fact_initialize(n);
    return fact[n] * invfact[n - r];
  }

  mint Catalan(int n) {
    if (n < 0)
      return 0;
    else if (n == 0)
      return 1;
    if (fact.size() < 2 * n + 1)
      fact_initialize(2 * n);
    return fact[2 * n] * invfact[n + 1] * invfact[n];
  }
};

signed main() {

  int H, W;
  cin >> H >> W;
  vector<string> A(H);
  cin >> A;

  int n = H + W;
  Combination C(n);
  mmat sti(n + 1);
  sti[0] = mvec(1, 1);
  FOR(i, 1, n + 1) {
    sti[i].resize(i + 1);
    sti[i][0] = 0;
    FOR(j, 1, i) sti[i][j] = sti[i - 1][j - 1] + sti[i - 1][j] * j;
    sti[i][i] = 1;
  }

  mmat dp(H + 1, mvec(W, 0));
  dp[0][0] = 1;
  REP(i, H) {
    REP(j, W) {
      dp[i + 1][j] += dp[i][j] * (j + 2);
      if (j < W - 1)
        dp[i + 1][j + 1] += dp[i][j];
    }
  }

  mmat dq(H + 1, mvec(W + 1, 0));
  REP(h, H + 1) REP(w, W + 1) {
    REP(k, w) dq[h][w] += dp[h][k] * sti[w][k + 1] * C.fact[k + 1] * C.fact[k];
    if (w == 0)
      dq[h][w] = 1;
  }

  mmat dq2(H + 1, mvec(W + 1, 0));
  REP(h, H + 1) REP(w, W + 1) {
    REP(i, h + 1) REP(j, w + 1) {
      dq2[h][w] += dq[i][w - j] * dq[h - i][j] * C.nCr(h, i) * C.nCr(w, j);
    }
  }

  mint ans = dq[H][W];
  REP(x, bit(H)) REP(y, bit(W)) {
    bools okx(H, false), oky(W, false);
    REP(i1, H) REP(i2, i1) REP(j1, W) REP(j2, j1) {
      if ((x >> i1) & (x >> i2) & (y >> j1) & (y >> j2) & 1) {
        if (A[i1][j1] == A[i2][j2] && A[i1][j2] == A[i2][j1] &&
            A[i1][j1] != A[i2][j1]) {
          okx[i1] = true;
          okx[i2] = true;
          oky[j1] = true;
          oky[j2] = true;
        }
      }
    }
    bool f = true;
    REP(i, H) if (((x >> i) & 1) && !okx[i]) f = false;
    REP(j, W) if (((y >> j) & 1) && !oky[j]) f = false;

    if (f) {
      int h = H - __builtin_popcount(x), w = W - __builtin_popcount(y);
      // cout << h << "," << w << endl;
      if (h < H && w < W)
        ans += dq2[h][w];
    }
  }
  Out(ans);

  return 0;
}