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

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

int n, m, b[510][510];
int pc[1 << 24], dp[2][1 << 24], bits[510];

void compute() {
  int maxi = 0;
  if (n <= m) {
    for (int i = 0; i < (1 << n); i++) {
      if (maxi >= pc[i])
        continue;
      bool ok = true;
      for (int x = 0; x < m; x++) {
        int cnt = 0;
        for (int y = 0; y < n; y++)
          if (((i >> y) & 1) && b[y][x])
            ++cnt;
        if (cnt & 1) {
          ok = false;
          break;
        }
      }
      if (ok)
        maxi = max(maxi, pc[i]);
    }
  } else { // n > m
    for (int y = 0; y < n; y++) {
      bits[y] = 0;
      for (int x = 0; x < m; x++)
        if (b[y][x])
          bits[y] = bits[y] | (1 << x);
    }
    memset(dp, -1, sizeof dp);
    dp[0][0] = 0;
    for (int y = 0; y < n; y++) {
      for (int i = 0; i < (1 << m); i++)
        dp[(y + 1) & 1][i] = -1;
      for (int state = 0; state < (1 << m); state++)
        if (dp[y & 1][state] != -1) {
          dp[(y + 1) & 1][state] =
              max(dp[(y + 1) & 1][state], dp[y & 1][state]);
          dp[(y + 1) & 1][state ^ bits[y]] =
              max(dp[(y + 1) & 1][state ^ bits[y]], dp[y & 1][state] + 1);
        }
    }
    if (dp[n & 1][0] != -1)
      maxi = dp[n & 1][0];
  }
  cout << maxi << endl;
}

int main() {
  for (int i = 0; i < (1 << 23); i++)
    pc[i] = __builtin_popcount(i);
  while (cin >> n >> m, n | m) {
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      for (int j = 0; j < m; j++)
        b[i][j] = (s[j] == '1');
    }
    compute();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define show(x) cerr << #x << "=" << x << "\\n"
typedef long long ll;
using namespace std;
// const ll MOD=(ll)1e9+7;
// const ll inf=(ll)1e14;
const int dy[] = {1, 0, -1};
const int dx[] = {1, 0, -1};
int dp[2][(1 << 22) + 2];
string s[511];
int n, m;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  // FILE *stream2;
  stream1 = freopen("in", "r", stdin);
  // stream2=freopen("out","w",stdout);
  if (stream1 == NULL)
    return 0;
    // if(stream2==NULL)return 0;
#endif
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    if (n <= m) {
      int ans = 0;
      for (int i = 0; i < (1 << n); i++) {
        int cont[501];
        fill(cont, cont + m, 0);
        int tmp = 0;
        for (int j = 0; j < n; j++) {
          if (i & (1 << j)) {
            tmp++;
            for (int k = 0; k < m; k++) {
              if (s[j][k] == '1') {
                cont[k]++;
              }
            }
          }
        }
        bool ok = 1;
        for (int j = 0; j < m; j++) {
          if (cont[j] & 1)
            ok = 0;
        }
        if (ok)
          ans = max(ans, tmp);
      }
      cout << ans << endl;
    } else {
      int list[501];
      for (int i = 0; i < n; i++)
        list[i] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (s[i][j] == '1') {
            list[i] |= (1 << (m - 1 - j));
          }
        }
      }
      for (int i = 0; i < 2; i++)
        for (int j = 0; j < (1 << m); j++)
          dp[i][j] = -1;
      dp[0][0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < (1 << m); j++) {
          if (dp[i & 1][j] == -1)
            continue;
          dp[(i + 1) & 1][j ^ list[i]] =
              max(dp[(i + 1) & 1][j ^ list[i]], dp[i & 1][j] + 1);
          dp[(i + 1) & 1][j] = max(dp[(i + 1) & 1][j], dp[i & 1][j]);
        }
      }
      cout << dp[n & 1][0] << endl;
    }
  }
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\\n";
  fclose(stream1);
  // fclose(stream2);
#endif
  return 0;
}