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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;
int fie[501][501];
int dp[51][1 << 20];
int dp2[501][1 << 11];

int res = 0;
int used[501];

void dfs(int v, int c, int bit) {
  if (v == n) {
    for (int i = 0; i < m; i++) {
      int cnt = 0;
      for (int j = 0; j < n; j++) {
        if (used[j] == 1) {
          cnt += fie[j][i];
        }
      }
      if (cnt % 2 == 1)
        return;
    }
    res = max(res, c);
    return;
  }
  used[v] = 0;
  dfs(v + 1, c, 0);
  used[v] = 1;
  dfs(v + 1, c + 1, 0);
}

int main(void) {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(fie, 0, sizeof(fie));
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      for (int j = 0; j < m; j++) {
        if (str[j] == '1')
          fie[i][j] = 1;
        else
          fie[i][j] = 0;
      }
    }
    if (m <= 10) {
      memset(dp2, -1, sizeof(dp2));
      dp2[0][0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < (1 << m); j++) {
          if (dp2[i][j] < 0)
            continue;
          dp2[i + 1][j] = max(dp2[i + 1][j], dp2[i][j]);
          int bit = j;
          for (int k = 0; k < m; k++) {
            if (fie[i][k] == 1) {
              if (j >> k & 1) {
                bit -= 1 << k;
              } else {
                bit += 1 << k;
              }
            }
          }
          dp2[i + 1][bit] = max(dp2[i + 1][bit], dp2[i][j] + 1);
        }
      }
      printf("%d\\n", dp2[n][0]);
    } else if (m <= 20) {
      memset(dp, -1, sizeof(dp));
      dp[0][0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < (1 << m); j++) {
          if (dp[i][j] < 0)
            continue;
          dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
          int bit = j;
          for (int k = 0; k < m; k++) {
            if (fie[i][k] == 1) {
              if (j >> k & 1) {
                bit -= 1 << k;
              } else {
                bit += 1 << k;
              }
            }
          }
          dp[i + 1][bit] = max(dp[i + 1][bit], dp[i][j] + 1);
        }
      }
      printf("%d\\n", dp[n][0]);
    } else {
      res = 0;
      dfs(0, 0, 0);
      printf("%d\\n", res);
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;
int fie[501][501];
int dp[60][1 << 21];
int dp2[501][1 << 11];

int res = 0;
int used[501];

void dfs(int v, int c, int bit) {
  if (v == n) {
    for (int i = 0; i < m; i++) {
      int cnt = 0;
      for (int j = 0; j < n; j++) {
        if (used[j] == 1) {
          cnt += fie[j][i];
        }
      }
      if (cnt % 2 == 1)
        return;
    }
    res = max(res, c);
    return;
  }
  used[v] = 0;
  dfs(v + 1, c, 0);
  used[v] = 1;
  dfs(v + 1, c + 1, 0);
}

int main(void) {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(fie, 0, sizeof(fie));
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      for (int j = 0; j < m; j++) {
        if (str[j] == '1')
          fie[i][j] = 1;
        else
          fie[i][j] = 0;
      }
    }
    if (m <= 10) {
      memset(dp2, -1, sizeof(dp2));
      dp2[0][0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < (1 << m); j++) {
          if (dp2[i][j] < 0)
            continue;
          dp2[i + 1][j] = max(dp2[i + 1][j], dp2[i][j]);
          int bit = j;
          for (int k = 0; k < m; k++) {
            if (fie[i][k] == 1) {
              if (j >> k & 1) {
                bit -= 1 << k;
              } else {
                bit += 1 << k;
              }
            }
          }
          dp2[i + 1][bit] = max(dp2[i + 1][bit], dp2[i][j] + 1);
        }
      }
      printf("%d\\n", dp2[n][0]);
    } else if (m <= 20) {
      memset(dp, -1, sizeof(dp));
      dp[0][0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < (1 << m); j++) {
          if (dp[i][j] < 0)
            continue;
          dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
          int bit = j;
          for (int k = 0; k < m; k++) {
            if (fie[i][k] == 1) {
              if (j >> k & 1) {
                bit -= 1 << k;
              } else {
                bit += 1 << k;
              }
            }
          }
          dp[i + 1][bit] = max(dp[i + 1][bit], dp[i][j] + 1);
        }
      }
      printf("%d\\n", dp[n][0]);
    } else {
      res = 0;
      dfs(0, 0, 0);
      printf("%d\\n", res);
    }
  }
  return 0;
}