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

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

char b[510][510];
bitset<510> bits[510];
int dp[2][1 << 22], nums[510];
signed main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        scanf(" %c", &b[i][j]);
      }
    }

    int ans = 0;
    if (N <= 22) {
      for (int i = 0; i < N; i++) {
        bits[i].set().flip();
        for (int j = 0; j < M; j++) {
          bits[i][j] = (b[i][j] - '0');
        }
      }

      for (int bit = 0; bit < (1 << N); bit++) {
        bitset<510> bb;
        for (int i = 0; i < N; i++) {
          if (!(bit >> i & 1))
            continue;
          bb ^= bits[i];
        }

        bool ok = bb.flip().all();
        if (ok)
          ans = max(ans, (int)__builtin_popcountll(bit));
      }
      cout << ans << endl;
    } else {
      for (int i = 0; i < N; i++) {
        nums[i] = 0;
        for (int j = 0; j < M; j++) {
          nums[i] |= (b[i][j] - '0') << j;
        }
      }

      fill(dp[0], dp[2], -1);
      dp[0][0] = 0;
      for (int i = 0; i < N; i++) {
        int c = i % 2, n = 1 - c;
        for (int bit = 0; bit < (1 << M); bit++) {
          if (dp[c][bit] < 0)
            continue;

          // 使わない
          chmax(dp[n][bit], dp[c][bit]);

          // 使う
          int nbit = bit ^ nums[i];
          chmax(dp[n][nbit], dp[c][bit] + 1);

          dp[c][bit] = -1;
        }
      }
      cout << dp[N % 2][0] << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

char b[510][510];
bitset<510> bits[510];
int dpc[1 << 23], dpn[1 << 23], nums[510];
signed main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        cin >> b[i][j];
      }
    }

    int ans = 0;
    if (N <= 23) {
      for (int i = 0; i < N; i++) {
        bits[i].set().flip();
        for (int j = 0; j < M; j++) {
          bits[i][j] = (b[i][j] - '0');
        }
      }

      for (int bit = 0; bit < (1 << N); bit++) {
        bitset<510> bb;
        for (int i = 0; i < N; i++) {
          if (!(bit >> i & 1))
            continue;
          bb ^= bits[i];
        }

        bool ok = bb.flip().all();
        if (ok)
          ans = max(ans, (int)__builtin_popcountll(bit));
      }
      cout << ans << endl;
    } else {
      for (int i = 0; i < N; i++) {
        nums[i] = 0;
        for (int j = 0; j < M; j++) {
          nums[i] |= (b[i][j] - '0') << j;
        }
      }

      fill(dpc, dpc + (1 << M), -1);
      fill(dpn, dpn + (1 << M), -1);
      dpc[0] = 0;

      for (int i = 0; i < N; i++) {
        for (int bit = 0; bit < (1 << M); bit++) {
          if (dpc[bit] < 0)
            continue;

          // 使わない
          chmax(dpn[bit], dpc[bit]);

          // 使う
          int nbit = bit ^ nums[i];
          chmax(dpn[nbit], dpc[bit] + 1);

          dpc[bit] = -1;
        }
        swap(dpn, dpc);
      }
      cout << dpc[0] << endl;
    }
  }
  return 0;
}