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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
//    #include <boost/multiprecision/cpp_int.hpp>
#include <functional>
//     #define int long long
#define inf 1000000007
#define pa pair<int, int>
#define ll long long
#define pal pair<double, pa>
#define ppa pair<int, int>
#define ppap pair<pa, pa>
#define ssa pair<string, int>
#define mp make_pair
#define pb push_back
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define VI vector<int>
using namespace std;

class pas {
public:
  int x, y;
  pas(int x = 0, int y = 0) : x(x), y(y) {}
  pas operator+(pas p) { return pas(x + p.x, y + p.y); }
  pas operator-(pas p) { return pas(x - p.x, y - p.y); }
  pas operator*(int a) { return pas(x * a, y * a); }
  pas operator/(int a) { return pas(x / a, y / a); }
  //	double absv() {return sqrt(norm());}
  int norm() { return x * x + y * y; }
  bool operator<(const pas &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const pas &p) const { return x == p.x && y == p.y; }
};

vector<int> ve;

// int dp[2][10001]={0};
int dp[2][1 << 25] = {0};
string s[600];

int n, m;
void solve1() {
  int ans = 0;
  //	cout<<"ss"<<endl;
  string t = "";
  //	string tt=t;
  for (int i = 0; i < m; i++)
    t += "0";
  string tt = t, ttt = t;
  for (int i = 0; i < (1 << n); i++) {

    //	cout<<i<<endl;
    int cnt = 0;
    t = ttt;
    int uu = __builtin_popcount(i);

    if (uu < ans)
      continue;
    bool g = true;
    for (int k = 0; k < m; k++) {
      for (int j = 0; j < n; j++)
        if (i & (1 << j)) {
          if (s[j][k] == '1')
            cnt++;
        }
      if (cnt % 2) {
        g = false;
        break;
      }
    }
    //	cout<<"   "<<t<<endl;
    //	cout<<" "<<s[j]<<endl;
    // cout<<j<<endl;

    //	cout<<t<<endl;
    if (g)
      ans = max(ans, uu);
  }
  cout << ans << endl;
  // cout<<"ss"<<endl;
  return;
}

int me[600] = {0};
void solve2() {
  for (int i = 0; i < n; i++) {
    me[i + 1] = 0;
    for (int j = 0; j < m; j++)
      if (s[i][j] == '1')
        me[i + 1] += (1 << j);
    //	cout<<me[i+1]<<endl;
  }

  for (int i = 0; i < 2; i++)
    for (int j = 0; j < (1 << 22); j++)
      dp[i][j] = -inf;
  dp[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << 22); j++)
      dp[i % 2][j] = -inf;
    for (int j = 0; j < (1 << m); j++)
      if (dp[(i - 1) % 2][j] >= 0) {
        dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][j]);
        dp[i % 2][j ^ me[i]] =
            max(dp[i % 2][j ^ me[i]], dp[(i - 1) % 2][j] + 1);
      }
  }
  cout << dp[n % 2][0] << endl;
  // cout<<endl;
}

signed main() {

  while (cin >> n >> m) {

    if (n == 0)
      return 0;

    for (int i = 1; i <= n; i++)
      cin >> s[i - 1];
    //       		for(int i=0;i<n;i++)cout<<s[i]<<endl;
    if (n <= 23)
      solve1();
    else
      solve2();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
//    #include <boost/multiprecision/cpp_int.hpp>
#include <functional>
//     #define int long long
#define inf 1000000007
#define pa pair<int, int>
#define ll long long
#define pal pair<double, pa>
#define ppa pair<int, int>
#define ppap pair<pa, pa>
#define ssa pair<string, int>
#define mp make_pair
#define pb push_back
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define VI vector<int>
using namespace std;

class pas {
public:
  int x, y;
  pas(int x = 0, int y = 0) : x(x), y(y) {}
  pas operator+(pas p) { return pas(x + p.x, y + p.y); }
  pas operator-(pas p) { return pas(x - p.x, y - p.y); }
  pas operator*(int a) { return pas(x * a, y * a); }
  pas operator/(int a) { return pas(x / a, y / a); }
  //	double absv() {return sqrt(norm());}
  int norm() { return x * x + y * y; }
  bool operator<(const pas &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const pas &p) const { return x == p.x && y == p.y; }
};

vector<int> ve;

// int dp[2][10001]={0};
int dp[2][1 << 22] = {0};
string s[600];

int n, m;
void solve1() {
  int ans = 0;
  //	cout<<"ss"<<endl;
  string t = "";
  //	string tt=t;
  for (int i = 0; i < m; i++)
    t += "0";
  string tt = t, ttt = t;
  for (int i = 0; i < (1 << n); i++) {
    //	cout<<i<<endl;
    int cnt = 0;
    t = ttt;
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) {
        for (int k = 0; k < m; k++) {

          if (t[k] == s[j][k])
            t[k] = '0';
          else
            t[k] = '1';
        }
        //	cout<<"   "<<t<<endl;
        //	cout<<" "<<s[j]<<endl;
        // cout<<j<<endl;
        cnt++;
      }
    //	cout<<t<<endl;
    if (tt == t)
      ans = max(ans, cnt);
  }
  cout << ans << endl;
  // cout<<"ss"<<endl;
  return;
}

int me[600] = {0};
void solve2() {
  for (int i = 0; i < n; i++) {
    me[i + 1] = 0;
    for (int j = 0; j < m; j++)
      if (s[i][j] == '1')
        me[i + 1] += (1 << j);
    //	cout<<me[i+1]<<endl;
  }

  for (int i = 0; i < 2; i++)
    for (int j = 0; j < (1 << 22); j++)
      dp[i][j] = -inf;
  dp[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << 22); j++)
      dp[i % 2][j] = -inf;
    for (int j = 0; j < (1 << m); j++)
      if (dp[(i - 1) % 2][j] >= 0) {
        dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][j]);
        dp[i % 2][j ^ me[i]] =
            max(dp[i % 2][j ^ me[i]], dp[(i - 1) % 2][j] + 1);
      }
  }
  cout << dp[n % 2][0] << endl;
  // cout<<endl;
}

signed main() {

  while (cin >> n >> m) {

    if (n == 0)
      return 0;

    for (int i = 1; i <= n; i++)
      cin >> s[i - 1];
    //       		for(int i=0;i<n;i++)cout<<s[i]<<endl;
    if (n <= 23)
      solve1();
    else
      solve2();
  }

  return 0;
}