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

#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
  long long int tmp = 1;

  if (n > 0) {
    tmp = power(x, n / 2, M);
    if (n % 2 == 0)
      tmp = (tmp * tmp) % M;
    else
      tmp = (((tmp * tmp) % M) * x) % M;
  }
  return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  while (N) {
    vector<string> s(N);
    for (int i = 0; i < N; i++)
      cin >> s[i];
    if (N <= 20) {
      int loop = pow(2, N);
      ans = 0;
      for (int i = 0; i < loop; i++) {
        int num = 0;
        int box = i;
        int fig = 0;
        vector<int> used(M, 0);
        while (box) {
          if (box % 2) {
            num++;
            for (int j = 0; j < M; j++) {
              if (s[fig][j] == '1') {
                used[j]++;
              }
            }
          }
          fig++;
          box /= 2;
        }
        bool flag = true;
        for (int j = 0; j < M; j++) {
          if (used[j] % 2)
            flag = false;
        }
        if (flag) {
          ans = max((int)ans, num);
        }
      }
      cout << ans << endl;
    } else if (N <= 40) {
      int loop_f = pow(2, N / 2);
      int loop_s = pow(2, N - N / 2);
      vector<int> used_f((int)pow(2, M), 0);
      vector<int> used_s((int)pow(2, M), 0);
      vector<int> bi(N);
      for (int i = 0; i < N; i++) {
        for (auto j : s[i]) {
          bi[i] *= 2;
          if (j == '1')
            bi[i]++;
        }
      }
      ans = 0;
      for (int i = 0; i < loop_f; i++) {
        int num = 0;
        int box = i;
        int fig = 0;
        int bag = 0;
        while (box) {
          if (box % 2) {
            num++;
            bag ^= bi[fig];
          }
          fig++;
          box /= 2;
        }
        used_f[bag] = max(used_f[bag], num);
      }
      for (int i = 0; i < loop_s; i++) {
        int num = 0;
        int box = i;
        int fig = N / 2;
        int bag = 0;
        while (box) {
          if (box % 2) {
            num++;
            bag ^= bi[fig];
          }
          fig++;
          box /= 2;
        }
        used_s[bag] = max(used_s[bag], num);
      }
      ans = used_f[0] + used_s[0];
      for (int i = 1; i < (int)pow(2, M); i++) {
        if (used_f[i] && used_s[i])
          ans = max((int)ans, used_f[i] + used_s[i]);
      }
      cout << ans << endl;
    } else {
      int loop = pow(2, M);
      vector<vector<int>> dp(N, vector<int>(loop, 0));
      vector<int> num(N, 0);
      for (int i = 0; i < N; i++) {
        for (auto j : s[i]) {
          num[i] *= 2;
          if (j == '1')
            num[i]++;
        }
      }
      dp[0][num[0]]++;
      for (int i = 1; i < N; i++) {
        dp[i][num[i]] = max(dp[i - 1][0] + 1, dp[i - 1][num[i]]);
        for (int j = 1; j < loop; j++) {
          if (dp[i - 1][j]) {
            dp[i][j ^ num[i]] = max(dp[i - 1][j] + 1, dp[i - 1][j ^ num[i]]);
          } else {
            dp[i][j ^ num[i]] = dp[i - 1][j ^ num[i]];
          }
        }
      }
      cout << dp[N - 1][0] << endl;
    }
    cin >> N >> M;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
  long long int tmp = 1;

  if (n > 0) {
    tmp = power(x, n / 2, M);
    if (n % 2 == 0)
      tmp = (tmp * tmp) % M;
    else
      tmp = (((tmp * tmp) % M) * x) % M;
  }
  return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  while (N) {
    vector<string> s(N);
    for (int i = 0; i < N; i++)
      cin >> s[i];
    if (N <= 21) {
      int loop = pow(2, N);
      for (int i = 0; i < loop; i++) {
        int num = 0;
        int box = i;
        int fig = 0;
        vector<int> used(M, 0);
        while (box) {
          if (box % 2) {
            num++;
            for (int j = 0; j < M; j++) {
              if (s[fig][j] == '1') {
                used[j]++;
              }
            }
          }
          fig++;
          box /= 2;
        }
        bool flag = true;
        for (int j = 0; j < M; j++) {
          if (used[j] % 2)
            flag = false;
        }
        if (flag) {
          ans = max((int)ans, num);
        }
      }
      cout << ans << endl;
    } else {
      int loop = pow(2, M);
      vector<vector<int>> dp(N, vector<int>(loop, 0));
      vector<int> num(N, 0);
      for (int i = 0; i < N; i++) {
        for (auto j : s[i]) {
          num[i] *= 2;
          if (j == '1')
            num[i]++;
        }
      }
      dp[0][num[0]]++;
      for (int i = 1; i < N; i++) {
        dp[i][num[i]] = dp[i - 1][0] + 1;
        for (int j = 1; j < loop; j++) {
          if (dp[i - 1][j]) {
            dp[i][j ^ num[i]] = dp[i - 1][j] + 1;
          }
        }
      }
      cout << dp[N - 1][0] << endl;
    }
    cin >> N >> M;
  }
  return 0;
}