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

#include <bits/stdc++.h>
using namespace std;

int dp[2][1953130];
int fac[10];

int main() {

  fac[0] = 1;
  for (int i = 1; i < 10; i++) {
    fac[i] = fac[i - 1] * 5;
  }

  while (1) {
    int n;
    cin >> n;
    int win_number = (n - 1) / 2;
    if (!n)
      break;
    int m;
    cin >> m;
    vector<vector<int>> num(n, vector<int>(n));
    vector<vector<int>> winlose(n, vector<int>(n, 0));

    for (int i = 0; i < m; i++) {
      int win, lose;
      cin >> win >> lose;
      win--;
      lose--;
      winlose[win][lose] = 1;
      winlose[lose][win] = -1;
    }

    int cnt = 1;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        num[i][j] = cnt;
        cnt++;
      }
    }

    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    int now = 0;
    int next = 1;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {

        for (int mem = 0; mem < 1953130; mem++)
          dp[next][mem] = 0;
        for (int bit = 0; bit <= fac[n]; bit++) {
          // i vs j
          int next_num = next;
          int now_num = now;
          if (dp[now_num][bit] == 0)
            continue;

          // jが勝つ
          if (winlose[i][j] != 1) {
            int next_bit = bit;
            int number = bit % fac[j + 1];
            number = number / fac[j];
            if (number < win_number) {
              next_bit += fac[j];
              dp[next_num][next_bit] += dp[now_num][bit];
            }
          }

          // iがかつ
          if (winlose[j][i] != 1) {
            int next_bit = bit;
            int number = bit % fac[i + 1];
            number = number / fac[i];
            if (number < win_number) {
              next_bit += fac[i];
              dp[next_num][next_bit] += dp[now_num][bit];
            }
          }
        }

        swap(now, next);
      }
    }

    int ans_bit = 0;
    for (int i = 0; i < n; i++) {
      ans_bit += fac[i] * win_number;
    }

    cout << dp[now][ans_bit] << endl;
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int dp[40][1953130];
int fac[10];

int main() {

  fac[0] = 1;
  for (int i = 1; i < 10; i++) {
    fac[i] = fac[i - 1] * 5;
  }

  while (1) {
    int n;
    cin >> n;
    int win_number = (n - 1) / 2;
    if (!n)
      break;
    int m;
    cin >> m;
    vector<vector<int>> num(n, vector<int>(n));
    vector<vector<int>> winlose(n, vector<int>(n, 0));

    for (int i = 0; i < m; i++) {
      int win, lose;
      cin >> win >> lose;
      win--;
      lose--;
      winlose[win][lose] = 1;
      winlose[lose][win] = -1;
    }

    int cnt = 1;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        num[i][j] = cnt;
        cnt++;
      }
    }

    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        for (int bit = 0; bit <= fac[n]; bit++) {
          // i vs j
          int next_num = num[i][j];
          int now_num = next_num - 1;
          if (dp[now_num][bit] == 0)
            continue;

          // jが勝つ
          if (winlose[i][j] != 1) {
            int next_bit = bit;
            int number = bit % fac[j + 1];
            number = number / fac[j];
            if (number < win_number) {
              next_bit += fac[j];
              dp[next_num][next_bit] += dp[now_num][bit];
            }
          }

          // iがかつ
          if (winlose[j][i] != 1) {
            int next_bit = bit;
            int number = bit % fac[i + 1];
            number = number / fac[i];
            if (number < win_number) {
              next_bit += fac[i];
              dp[next_num][next_bit] += dp[now_num][bit];
            }
          }
        }
      }
    }

    int ans_bit = 0;
    for (int i = 0; i < n; i++) {
      ans_bit += fac[i] * win_number;
    }

    cout << dp[cnt - 1][ans_bit] << endl;
  }

  return 0;
}