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

#include <bits/stdc++.h>
using namespace std;

const int MOD = 998244353;
int upd(int x) { return x + (x >> 31 & MOD); }
void inc(int &a, int b) { a = upd(a + b - MOD); }
int N, M, vr[13], vc[13];
char arr[13][13];

int f[1 << 10][1 << 10][3], g[1 << 10][1 << 10][4][3][4];
bool vf[1 << 10][1 << 10][3], vg[1 << 10][1 << 10][4][3][4];

int sf(int, int, int);
int sg(int, int, int, int, int);

bool check(int r, int c) {
  bool flg = 1;
  for (int i = 0; i < N; ++i)
    if (r >> i & 1)
      flg &= (vr[i] & c) != 0 && (vr[i] & c) != c;
  for (int j = 0; j < M; ++j)
    if (c >> j & 1)
      flg &= (vc[j] & r) != 0 && (vc[j] & r) != r;
  return flg;
}

int sf(int a, int b, int c) {
  if (!a && !b)
    return 1;
  if (!a || !b)
    return 0;
  if (vf[a][b][c])
    return f[a][b][c];
  vf[a][b][c] = 1;
  return f[a][b][c] = sg(a, b, 0, c, 0) + check(a, b);
}

int sg(int a, int b, int c, int d, int e) {
  if (c == 4)
    return e ? sf(a, b, e - 1) : 0;
  if (vg[a][b][c][d][e])
    return g[a][b][c][d][e];
  int &t = g[a][b][c][d][e];
  vg[a][b][c][d][e] = 1;
  if (c <= 1)
    if (!d)
      t = sg(a, b, c + 1, d, e);
    else
      for (int x = (!c ? a & 31 : a >> 5 << 5), s = x;; s = (s - 1) & x) {
        inc(t, sg(a ^ x ^ s, b, c + 1, d, e | (s != x)));
        if (!s)
          break;
      }
  else if (d == 1)
    t = sg(a, b, c + 1, d, e);
  else
    for (int x = (c == 2 ? b & 31 : b >> 5 << 5), s = x;; s = (s - 1) & x) {
      inc(t, sg(a, b ^ x ^ s, c + 1, d, e | (s != x) << 1));
      if (!s)
        break;
    }
  return t;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j)
      cin >> arr[i][j];
  for (int i = 0; i < N; ++i)
    for (int j = M - 1; ~j; --j)
      vr[i] = vr[i] << 1 | (arr[i][j] == '#');
  for (int i = 0; i < M; ++i)
    for (int j = N - 1; ~j; --j)
      vc[i] = vc[i] << 1 | (arr[j][i] == '#');
  int sum = 0;
  for (int i = 0; i < 1 << N; ++i)
    for (int j = 0; j < 1 << M; ++j)
      inc(sum, sf(i, j, 2));
  cout << sum;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MOD = 998244353;
int upd(int x) { return x + (x >> 31 & MOD); }
void inc(int &a, int b) { a = upd(a + b - MOD); }
int N, M, vr[13], vc[13];
char arr[13][13];

int f[1 << 10][1 << 10][3], g[1 << 10][1 << 10][2][3][4];
bool vf[1 << 10][1 << 10][3], vg[1 << 10][1 << 10][2][3][4];

int sf(int, int, int);
int sg(int, int, int, int, int);

bool check(int r, int c) {
  bool flg = 1;
  for (int i = 0; i < N; ++i)
    if (r >> i & 1)
      flg &= (vr[i] & c) != 0 && (vr[i] & c) != c;
  for (int j = 0; j < M; ++j)
    if (c >> j & 1)
      flg &= (vc[j] & r) != 0 && (vc[j] & r) != r;
  return flg;
}

int sf(int a, int b, int c) {
  if (!a && !b)
    return 1;
  if (!a || !b)
    return 0;
  if (vf[a][b][c])
    return f[a][b][c];
  vf[a][b][c] = 1;
  return f[a][b][c] = sg(a, b, 0, c, 0) + check(a, b);
}

int sg(int a, int b, int c, int d, int e) {
  if (c == 2)
    return e ? sf(a, b, e - 1) : 0;
  if (vg[a][b][c][d][e])
    return g[a][b][c][d][e];
  int &t = g[a][b][c][d][e];
  vg[a][b][c][d][e] = 1;
  if (c == 0)
    if (!d)
      t = sg(a, b, c + 1, d, e);
    else
      for (int s = a;; s = (s - 1) & a) {
        inc(t, sg(s, b, c + 1, d, e | (s != a)));
        if (!s)
          break;
      }
  else if (d == 1)
    t = sg(a, b, c + 1, d, e);
  else
    for (int s = b;; s = (s - 1) & b) {
      inc(t, sg(a, s, c + 1, d, e | (s != b) << 1));
      if (!s)
        break;
    }
  return t;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j)
      cin >> arr[i][j];
  for (int i = 0; i < N; ++i)
    for (int j = M - 1; ~j; --j)
      vr[i] = vr[i] << 1 | (arr[i][j] == '#');
  for (int i = 0; i < M; ++i)
    for (int j = N - 1; ~j; --j)
      vc[i] = vc[i] << 1 | (arr[j][i] == '#');
  int sum = 0;
  for (int i = 0; i < 1 << N; ++i)
    for (int j = 0; j < 1 << M; ++j)
      inc(sum, sf(i, j, 2));
  cout << sum;
  return 0;
}