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

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int n, m;
bool b[504];

int c;
bool s[501];
int DP[2][1 << 25];

int bc(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int main() {

  while (cin >> n >> m, n | m) {
    char a;
    rep(i, n) {
      rep(j, m) {
        cin >> a;
        b[i * m + j] = (a == '1');
      }
    }

    if (n < 25) {
      int r = 0;
      rep(i, 1 << n) {
        rep(j, m) s[j] = true;

        c = bc(i);

        if (c <= r)
          continue;

        rep(j, n) {
          if (i & (1 << j)) {
            rep(k, m) {
              if (b[j * m + k])
                s[k] = !s[k];
            }
          }
        }

        bool f = true;
        rep(j, m) {
          if (!s[j]) {
            f = false;
            break;
          }
        }
        if (f)
          r = c;
      }

      cout << r << endl;

    } else {
      c = 0;
      rep(j, m) {
        if (b[j])
          c += 1 << j;
      }

      rep(i, 1 << m) {
        DP[0][i] = -1;
        DP[1][i] = -1;
      }

      DP[0][0] = 0;
      DP[0][c] = 1;

      for (int i = 1; i < n; i++) {
        c = 0;
        rep(j, m) {
          if (b[i * m + j])
            c += 1 << j;
        }
        rep(j, 1 << m) {
          DP[i & 1][j] = DP[(i - 1) & 1][j];
          if (DP[(i - 1) & 1][j ^ c] >= 0 &&
              DP[(i - 1) & 1][j ^ c] >= DP[i & 1][j])
            DP[i & 1][j] = DP[(i - 1) & 1][j ^ c] + 1;
        }
      }

      cout << (DP[(n - 1) % 2][0] < 0 ? 0 : DP[(n - 1) % 2][0]) << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define max(a, b) ((a) > (b) ? (a) : (b))

int n, m;
bool b[504];
int DP[2][1 << 22];

int round() {
  int r = 0;
  bool s[501];
  rep(i, 1 << n) {
    rep(j, m) s[j] = true;

    int c = 0;
    rep(j, n) {
      if (i & (1 << j)) {
        c++;
        rep(k, m) {
          if (b[j * m + k])
            s[k] = !s[k];
        }
      }
    }
    if (c <= r)
      continue;

    bool f = true;
    rep(j, m) {
      if (!s[j]) {
        f = false;
        break;
      }
    }
    if (f)
      r = c;
  }
  return r;
}

int dp() {

  int c = 0;
  rep(j, m) {
    if (b[j])
      c += 1 << j;
  }

  rep(i, 1 << m) {
    DP[0][i] = -1;
    DP[1][i] = -1;
  }
  DP[0][0] = 0;
  DP[0][c] = 1;

  for (int i = 1; i < n; i++) {
    c = 0;
    rep(j, m) {
      if (b[i * m + j])
        c += 1 << j;
    }
    rep(j, 1 << m) DP[i & 1][j] =
        max(DP[(i - 1) & 1][j],
            DP[(i - 1) & 1][j ^ c] < 0 ? -1 : (DP[(i - 1) & 1][j ^ c] + 1));
  }

  return DP[(n - 1) % 2][0] < 0 ? 0 : DP[(n - 1) % 2][0];
}

int ans() {
  if (n < 25)
    return round();
  else
    return dp();
}

int main() {

  while (cin >> n >> m, n | m) {
    char c;
    rep(i, n) {
      rep(j, m) {
        cin >> c;
        b[i * m + j] = (c == '1');
      }
    }

    cout << ans() << endl;
  }

  return 0;
}