Problem p03174 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ll long long
#define LSB __builtin_ctzll
#define sc(a) scanf("%d", &a)
#define MSB 63 - __builtin_clzll
#define scl(a) scanf("%lld", &a)
#define BITS __builtin_popcountll
#define mem(a, v) memset(a, v, sizeof(a))
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int MOD = 1e9 + 7;
const int MAX = 1e5 + 55;
const int INF = 1e9 + 77;
const ll INFINF = 1e18 + 1e17;
const double PI = acos(-1.0);
vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll s = power(x, y / 2);
  s *= s;
  if (y & 1)
    s *= x;
  return s;
}

int n;
bool x[22][22];
ll dp[10000000][22];

ll solve(int mask, int i) {
  if (i == n) {
    return 1;
  }

  ll &res = dp[mask][i];
  if (res != -1) {
    return res;
  }

  res = 0;
  for (int j = 0; j < n; j++) {
    int cur = n - 1 - j;
    if (x[i][j] && !(mask & (1 << cur))) {
      res += solve(mask + (1 << cur), i + 1) % MOD;
      res %= MOD;
    }
  }

  return res % MOD;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt" , "r" , stdin);
  // freopen("output.txt" , "w" , stdout);

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> x[i][j];
    }
  }

  mem(dp, -1);

  cout << solve(0, 0) % MOD;

  return 0;
}


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

#define endl '\n'
#define ll long long
#define LSB __builtin_ctzll
#define sc(a) scanf("%d", &a)
#define MSB 63 - __builtin_clzll
#define scl(a) scanf("%lld", &a)
#define BITS __builtin_popcountll
#define mem(a, v) memset(a, v, sizeof(a))
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int MOD = 1e9 + 7;
const int MAX = 1e5 + 55;
const int INF = 1e9 + 77;
const ll INFINF = 1e18 + 1e17;
const double PI = acos(-1.0);
vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll s = power(x, y / 2);
  s *= s;
  if (y & 1)
    s *= x;
  return s;
}

int n;
bool x[22][22];
ll dp[3000000][22];

ll solve(int mask, int i) {
  if (i == n) {
    return 1;
  }

  ll &res = dp[mask][i];
  if (res != -1) {
    return res;
  }

  res = 0;
  for (int j = 0; j < n; j++) {
    int cur = n - 1 - j;
    if (x[i][j] && !(mask & (1 << cur))) {
      res += solve(mask + (1 << cur), i + 1) % MOD;
      res %= MOD;
    }
  }

  return res % MOD;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt" , "r" , stdin);
  // freopen("output.txt" , "w" , stdout);

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> x[i][j];
    }
  }

  mem(dp, -1);

  cout << solve(0, 0) % MOD;

  return 0;
}