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

#include <iostream>

int source[500][500] = {};

void copy_array(int a[], int b[], int m) {
  for (int i = 0; i < m; i++) {
    b[i] = a[i];
  }
}

bool check(int n, int m, int sum[]) {
  for (int j = 0; j < m; j++) {
    if (sum[j] % 2 == 1)
      return false;
  }
  // std::cout << "      ";
  /*for(int j=0;j<m;j++){
      std::cout << sum[j] << ' ';
  }
  std::cout << '\\n';
   */
  return true;
}

bool sum_calc(int f_num, int f_pos, int m, int n, int pri_sum[]) {
  // std::cout << "再起\\n";
  int sum[500] = {};
  // std::cout << "f_num=" << f_num << '\\n';
  for (int i = f_pos; i < n - f_num + 1; i++) {
    // std::cout << "f_num=" << f_num << "  f_pos=" << i << '\\n';
    copy_array(pri_sum, sum, m);

    for (int j = 0; j < m; j++) {
      sum[j] -= source[i][j];
    }

    f_num--;
    if (f_num == 0) {
      // std::cout << "      f_num=0\\n";
      if (check(n, m, sum)) {
        return true;
      } else {
        // std::cout << "失敗した\\n";
        f_num++;
      }
    } else {
      if (sum_calc(f_num, i + 1, m, n, sum)) {
        return true;
      } else {
        f_num++;
      }
    }
  }
  // std::cout << "終了\\n";
  return false;
}

void to_int(char s[], int i) {
  int j = 0;
  while (s[j] != '\\0') {
    if (s[j] == '0') {
      source[i][j++] = 0;
    } else {
      source[i][j++] = 1;
    }
  }
}

int search(int n, int m, int sum[]) {
  for (int f_num = 1; f_num < n; f_num++) {
    // std::cout << "f_num=" << f_num << '\\n';
    if (sum_calc(f_num, 0, m, n, sum))
      return f_num;
  }
  return n;
}

int main() {
  int n, m;
  while (1) {
    std::cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    // std::cout << "n:" << n <<" m:" << m << '\\n';
    char s[500];
    for (int i = 0; i < n; i++) {
      std::cin >> s;
      to_int(s, i);
    }

    /*for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            std::cout << source[i][j] << ' ';
        }
        std::cout << '\\n';
    }
    */
    int sum[500] = {};
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        sum[j] += source[i][j];
      }
    }

    if (check(n, m, sum)) {
      std::cout << /*"ans=" <<*/ n << '\\n';
    } else {
      int ans = n - search(n, m, sum);
      std::cout << /*"ans=" <<*/ ans << '\\n';
    }
    // std::cout << '\\n';
  }
  /*for(int j=0;j<m;j++){
   std::cout << sum[j] << ' ' ;
   }
   std::cout << '\\n';
   */

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

int source[500][500] = {};

void copy_array(int a[], int b[], int m) {
  for (int i = 0; i < m; i++) {
    b[i] = a[i];
  }
}

bool check(int n, int m, int sum[]) {
  for (int j = 0; j < m; j++) {
    if (sum[j] % 2 == 1)
      return false;
  }
  return true;
}

bool sum_calc(int f_num, int f_pos, int m, int n, int pri_sum[]) {
  int sum[500] = {};
  for (int i = f_pos; i < n - f_num + 1; i++) {
    copy_array(pri_sum, sum, m);
    for (int j = 0; j < m; j++) {
      sum[j] -= source[i][j];
    }
    f_num--;
    if (f_num == 0) {
      if (check(n, m, sum)) {
        return true;
      } else {
        f_num++;
      }
    } else {
      sum_calc(f_num, i + 1, m, n, sum);
    }
  }
  return false;
}

void to_int(char s[], int i) {
  int j = 0;
  while (s[j] != '\\0') {
    if (s[j] == '0') {
      source[i][j++] = 0;
    } else {
      source[i][j++] = 1;
    }
  }
}

/*bool test(int f_num,int sum[]){
    for(int f_pos=0;f_pos<n;f_pos++){
        if(sum_calc(f_num,f_pos,m,sum)) return true;
    }
}
 */

int search(int n, int m, int sum[]) {
  for (int f_num = 1; f_num < n; f_num++) {
    for (int f_pos = 0; f_pos < n; f_pos++) {
      if (sum_calc(f_num, f_pos, m, n, sum))
        return f_num;
    }
  }
  return n;
}

int main() {
  int n, m;
  while (1) {
    std::cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    char s[500];
    for (int i = 0; i < n; i++) {
      std::cin >> s;
      to_int(s, i);
    }

    int sum[500] = {};
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        sum[j] += source[i][j];
      }
    }

    if (check(n, m, sum)) {
      std::cout << n << '\\n';
    } else {
      std::cout << n - search(n, m, sum) << '\\n';
    }
  }
  /*for(int j=0;j<m;j++){
      std::cout << sum[j] << ' ' ;
  }
  std::cout << '\\n';
   */
  /*for(int i=0;i<n;i++){
      for(int j=0;j<m;j++){
          std::cout << source[i][j] << ' ';
      }
      std::cout << '\\n';
  }*/

  return 0;
}