prithivMLmods/Coder-Stat huggingface dataset:
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits>

#define MAX_ELEMENTS 400

using namespace std;

// I have to create a binary tree where each inner node has the value of its two
// sons, I want to minimize the value of the root.

long long dp[MAX_ELEMENTS]
            [MAX_ELEMENTS]; // The weight of having to combine all elements
                            // between interval i and j. It has to be continuous
                            // since the slimes don't change positions
long long slimes[MAX_ELEMENTS]; // The slimes themselves
long long inf = 1e18L + 5;

long long arraySum(int L, int R) {
  long long ans = 0;
  for (int i = L; i <= R; i++)
    ans += slimes[i];
  return ans;
}

int main() {
  int nSlimes;
  long long slimeValue;
  cin >> nSlimes;
  for (int i = 0; i < nSlimes; i++) {
    cin >> slimeValue;
    slimes[i] = slimeValue;
  }
  for (int i = nSlimes - 1; i >= 0; i--) {
    for (int j = i; j < nSlimes; j++) {
      if (i == j)
        dp[i][j] = 0; // The cost to combine a slime to itself is none
      else {
        dp[i][j] = inf;
        for (int k = i; k <= j; k++)
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + arraySum(i, j));
      }
    }
  }
  cout << dp[0][nSlimes - 1];
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits>

#define MAX_ELEMENTS 400

using namespace std;

// I have to create a binary tree where each inner node has the value of its two
// sons, I want to minimize the value of the root.

long long dp[MAX_ELEMENTS]
            [MAX_ELEMENTS]; // The weight of having to combine all elements
                            // between interval i and j. It has to be continuous
                            // since the slimes don't change positions
long long slimes[MAX_ELEMENTS]; // The slimes themselves
long long inf = 1e18L + 5;

long long arraySum(int L, int R) {
  long long ans = 0;
  for (int i = L; i <= R; i++)
    ans += slimes[i];
  return ans;
}

int main() {
  int nSlimes;
  long long slimeValue;
  cin >> nSlimes;
  for (int i = 0; i < nSlimes; i++) {
    cin >> slimeValue;
    slimes[i] = slimeValue;
  }
  for (int i = nSlimes - 1; i >= 0; i--) {
    for (int j = i; j < nSlimes; j++) {
      if (i == j)
        dp[i][j] = 0; // The cost to combine a slime to itself is none
      else {
        dp[i][j] = inf;
        for (int k = i; k < j; k++)
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + arraySum(i, j));
      }
    }
  }
  cout << dp[0][nSlimes - 1];
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

long long dp[409][409];
long long N, A[409], V[409][409];

long long dfs(long long L, long long R) {
  if (L > R)
    return 0;
  if (L == R)
    return A[L];
  if (dp[L][R] != 0)
    return dp[L][R];

  long long minx = (1LL << 60);
  for (int i = L + 1; i <= R; i++) {
    minx = min(minx, V[L][R] + dfs(L, i - 1) + dfs(i, R));
  }
  return minx;
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  for (int i = 1; i <= N; i++) {
    long long sum = 0;
    for (int j = i; j <= N; j++) {
      sum += A[j];
      V[i][j] = sum;
    }
  }
  cout << dfs(1, N) - V[1][N] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

long long dp[409][409];
long long N, A[409], V[409][409];

long long dfs(long long L, long long R) {
  if (L > R)
    return 0;
  if (L == R)
    return A[L];
  if (dp[L][R] != 0)
    return dp[L][R];

  long long minx = (1LL << 60);
  for (int i = L + 1; i <= R; i++) {
    minx = min(minx, V[L][R] + dfs(L, i - 1) + dfs(i, R));
  }
  dp[L][R] = minx;
  return minx;
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  for (int i = 1; i <= N; i++) {
    long long sum = 0;
    for (int j = i; j <= N; j++) {
      sum += A[j];
      V[i][j] = sum;
    }
  }
  cout << dfs(1, N) - V[1][N] << endl;
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define gc(x)                                                                  \
  do {                                                                         \
    while ((x = getchar()) < '0')                                              \
      ;                                                                        \
    for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0')  \
      ;                                                                        \
  } while (0)
#define ms(a, b) memset(a, b, sizeof(a));
char _;
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f;

ll dp[400][400];
ll pfsum[401];
int arr[400];

ll mincost(int l, int r) {
  if (dp[l][r] >= INF) {
    if (l > r) {
      dp[l][r] = INF;
    } else if (l == r) {
      dp[l][r] = 0;
    } else {
      for (int i = l; i < r; ++i) {
        dp[l][r] = min(dp[l][r], mincost(l, i) + mincost(i + 1, r) +
                                     pfsum[r + 1] - pfsum[l]);
      }
    }
  }
  return dp[l][r];
}

int main() {
  ms(dp, INF);
  pfsum[0] = 0;
  int n;
  gc(n);
  for (int i = 0; i < n; ++i) {
    int a;
    gc(a);
    pfsum[i + 1] = pfsum[i] + a;
    arr[i] = a;
  }
  printf("%llu\n", mincost(0, n - 1));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define gc(x)                                                                  \
  do {                                                                         \
    while ((x = getchar()) < '0')                                              \
      ;                                                                        \
    for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0')  \
      ;                                                                        \
  } while (0)
#define ms(a, b) memset(a, b, sizeof(a));
char _;
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f;

ll dp[400][400];
ll pfsum[401];
int arr[400];

ll mincost(int l, int r) {
  if (dp[l][r] >= INF) {
    if (l > r) {
      dp[l][r] = INF;
    } else if (l == r) {
      dp[l][r] = 0;
    } else {
      for (int i = l; i < r; ++i) {
        dp[l][r] = min(dp[l][r], mincost(l, i) + mincost(i + 1, r) +
                                     pfsum[r + 1] - pfsum[l]);
      }
    }
  }
  return dp[l][r];
}

int main() {
  ms(dp, INF);
  pfsum[0] = 0;
  int n;
  gc(n);
  for (int i = 0; i < n; ++i) {
    int a;
    gc(a);
    pfsum[i + 1] = pfsum[i] + a;
    arr[i] = a;
  }
  for (int l = n - 1; l >= 0; --l) {
    for (int r = l; r < n; ++r) {
      if (l == r) {
        dp[l][r] = 0;
      } else {
        for (int i = l; i < r; ++i) {
          dp[l][r] =
              min(dp[l][r], dp[l][i] + dp[i + 1][r] + pfsum[r + 1] - pfsum[l]);
        }
      }
    }
  }
  printf("%llu\n", dp[0][n - 1]);
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
/*--ILRS-- sr4saurabh  */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define popb pop_back
#define mp make_pair
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define fi first
#define sz size
#define len length
#define se second
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 10000000 + 6;
#define M_PI 3.14159265358979323846
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  ll arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  ll dp[n + 1][n + 1];
  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      dp[l][r] = 1e18l + 5;
      ll sum = 0;
      for (int j = l; j <= r; j++)
        sum = sum + arr[j];

      if (l == r)
        dp[l][r] = 0;
      else {
        for (int i = l; i <= r - 1; i++) {
          dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r] + sum);
        }
      }
    }
  }
  cout << dp[0][n - 1];

  return 0;
}

Fixed submission:
/*--ILRS-- sr4saurabh  */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define popb pop_back
#define mp make_pair
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define fi first
#define sz size
#define len length
#define se second
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 10000000 + 6;
#define M_PI 3.14159265358979323846
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  /** #ifndef ONLINE_JUDGE
      freopen ("INPUT.txt" , "r" , stdin);
      freopen ("OUTPUT.txt", "w" , stdout);
   #endif**/

  int n;
  cin >> n;
  ll arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  ll dp[n + 1][n + 1];
  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      dp[l][r] = 1e18l + 5;
      ll sum = 0;
      for (int j = l; j <= r; j++)
        sum = sum + arr[j];

      if (l == r)
        dp[l][r] = 0;
      else {
        for (int i = l; i <= r - 1; i++) {
          dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r] + sum);
        }
      }
    }
  }
  cout << dp[0][n - 1];

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout << fixed << setprecision(20);
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll n;
vector<ll> A(n);
bool flag[405][405];
ll dp[405][405];

ll f(ll a, ll b) {
  if (flag[a][b])
    return dp[a][b];
  else {
    if (a == b) {
      flag[a][b] = 0;
      dp[a][b] = 0;
      return 0;
    } else if (a == b - 1) {
      flag[a][b] = 1;
      dp[a][b] = A[a] + A[b];
      return A[a] + A[b];
    } else {
      ll ans = INF;
      rep2(i, a, b - 1) { chmin(ans, f(a, i) + f(i + 1, b)); }
      rep2(i, a, b) ans += A[i];
      flag[a][b] = 1;
      dp[a][b] = ans;
      return ans;
    }
  }
}

int main() {
  cin >> n;
  rep(i, n) A[i] = 0;
  rep(i, n) { cin >> A[i]; }
  rep(i, 405) {
    rep(j, 405) {
      flag[i][j] = 0;
      dp[i][j] = 0;
    }
  }

  cout << f(0, n - 1) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout << fixed << setprecision(20);
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll n;
vector<ll> A(n);
bool flag[405][405];
ll dp[405][405];

ll f(ll a, ll b) {
  if (flag[a][b])
    return dp[a][b];
  else {
    if (a == b) {
      flag[a][b] = 0;
      dp[a][b] = 0;
      return 0;
    } else if (a == b - 1) {
      flag[a][b] = 1;
      dp[a][b] = A[a] + A[b];
      return A[a] + A[b];
    } else {
      ll ans = INF;
      rep2(i, a, b - 1) { chmin(ans, f(a, i) + f(i + 1, b)); }
      rep2(i, a, b) ans += A[i];
      flag[a][b] = 1;
      dp[a][b] = ans;
      return ans;
    }
  }
}

int main() {
  cin >> n;
  A.assign(n, 0);
  rep(i, n) { cin >> A[i]; }
  rep(i, 405) {
    rep(j, 405) {
      flag[i][j] = 0;
      dp[i][j] = 0;
    }
  }

  cout << f(0, n - 1) << endl;

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
/*
https://atcoder.jp/contests/dp/tasks/dp_n
*/
#include <bits/stdc++.h>
using namespace std;
long long dp[401][401];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  long long a[401] = {0};
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  memset(dp, 0x7f, sizeof dp[0][0] * 401 * 401);
  // for(int i=0;i<n;i++) {
  //   for(int j=0;j<n;j++) {
  //     cerr << dp[i][j] << ' ';
  //   }
  //   cerr << '\n';
  // }
  for (int l = 0; l < n; l++) {
    for (int i = 0; i < n; i++) {
      if (l == 0)
        dp[i][i] = 0;
      for (int j = i; j < min(i + l, n - 1); j++) {
        dp[i][i + l] = min(dp[i][i + l],
                           dp[i][j] + dp[j + 1][i + l] + a[i + l + 1] - a[i]);
      }
    }
  }
  cout << dp[0][n - 1] << '\n';
  return 0;
}

Fixed submission:
/*
https://atcoder.jp/contests/dp/tasks/dp_n
*/
#include <bits/stdc++.h>
using namespace std;
long long dp[401][401];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  long long a[401] = {0};
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  memset(dp, 0x7f, sizeof dp[0][0] * 401 * 401);
  // for(int i=0;i<n;i++) {
  //   for(int j=0;j<n;j++) {
  //     cerr << dp[i][j] << ' ';
  //   }
  //   cerr << '\n';
  // }
  for (int l = 0; l < n; l++) {
    for (int i = 0; i < n; i++) {
      if (l == 0)
        dp[i][i] = 0;
      int limit = min(n - 1, i + l);
      for (int j = i; j < limit; j++) {
        dp[i][limit] = min(dp[i][limit],
                           dp[i][j] + dp[j + 1][limit] + a[limit + 1] - a[i]);
      }
    }
  }
  cout << dp[0][n - 1] << '\n';
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int a[401];
long long dp[401][401];
long long pref[401];
int main() {
  long long n, l;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  pref[0] = a[0];
  for (int i = 1; i < n; i++) {
    pref[i] = pref[i - 1] + a[i];
    for (int len = 1; len <= n; len++) {
      for (l = 0; l < n; l++) {
        int r = l + len - 1;
        if (r == n)
          break;
        else if (len == 1)
          dp[l][l] = 0;
        else {
          dp[l][r] = 2e18;
          for (int mid = l; mid < r; mid++) {
            if (l > 0) {
              long long calc =
                  dp[l][mid] + dp[mid + 1][r] + pref[r] - pref[l - 1];
              dp[l][r] = min(dp[l][r], calc);
            } else {
              long long calc = dp[l][mid] + dp[mid + 1][r] + pref[r];
              dp[l][r] = min(dp[l][r], calc);
            }
          }
        }
      }
    }
  }
  cout << dp[0][n - 1];
}

Fixed submission:
#include <iostream>
using namespace std;
int a[401];
long long dp[401][401];
long long pref[401];
int main() {
  long long n, l;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  pref[0] = a[0];
  for (int i = 1; i < n; i++) {
    pref[i] = pref[i - 1] + a[i];
  }
  for (int len = 1; len <= n; len++) {
    for (l = 0; l < n; l++) {
      int r = l + len - 1;
      if (r == n)
        break;
      else if (len == 1)
        dp[l][l] = 0;
      else {
        dp[l][r] = 2e18;
        for (int mid = l; mid < r; mid++) {
          if (l > 0) {
            long long calc =
                dp[l][mid] + dp[mid + 1][r] + pref[r] - pref[l - 1];
            dp[l][r] = min(dp[l][r], calc);
          } else {
            long long calc = dp[l][mid] + dp[mid + 1][r] + pref[r];
            dp[l][r] = min(dp[l][r], calc);
          }
        }
      }
    }
  }
  cout << dp[0][n - 1];
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
int mod = 1e9 + 7;
#define input_from_file freopen("input.txt", "r", stdin);

ll dp[1 << 22][25];
ll allmask;
ll recursion(ll &n, ll mask, vector<vector<int>> &vec, ll level) {
  if (level >= n) {
    if (mask == allmask)
      return 1;
    return 0;
  }
  if (dp[mask][level] != -1)
    return dp[mask][level];
  ll val = 0;
  for (int i = 0; i < n; i++) {
    if (vec[i][level] == 1 && (mask & (1 << i)) == 0) {
      val = (val % mod + recursion(n, mask | (1 << i), vec, level + 1) % mod) %
            mod;
    }
  }
  dp[mask][level] = val;
  return dp[mask][level];
}
signed main() {
  //------------ SEE BELOW ARTICLE---------------
  // https://codeforces.com/blog/entry/64250?#comment-484569
  input_from_file;
  ll n;
  cin >> n;
  vector<vector<int>> vec(n, vector<int>(n, 0));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> vec[i][j];
  }
  allmask = (1 << n) - 1;
  memset(dp, -1, sizeof dp);
  cout << recursion(n, 0, vec, 0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
int mod = 1e9 + 7;
#define input_from_file freopen("input.txt", "r", stdin);

ll dp[1 << 22][25];
ll allmask;
ll recursion(ll &n, ll mask, vector<vector<int>> &vec, ll level) {
  if (level >= n) {
    if (mask == allmask)
      return 1;
    return 0;
  }
  if (dp[mask][level] != -1)
    return dp[mask][level];
  ll val = 0;
  for (int i = 0; i < n; i++) {
    if (vec[i][level] == 1 && (mask & (1 << i)) == 0) {
      val = (val % mod + recursion(n, mask | (1 << i), vec, level + 1) % mod) %
            mod;
    }
  }
  dp[mask][level] = val;
  return dp[mask][level];
}
signed main() {
  //------------ SEE BELOW ARTICLE---------------
  // https://codeforces.com/blog/entry/64250?#comment-484569
  // input_from_file;
  ll n;
  cin >> n;
  vector<vector<int>> vec(n, vector<int>(n, 0));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> vec[i][j];
  }
  allmask = (1 << n) - 1;
  memset(dp, -1, sizeof dp);
  cout << recursion(n, 0, vec, 0);
  return 0;
}
Problem p03174 (C++)

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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvll;
typedef vector<vd> vvd;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pair<int, int>> vpii;

const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vvi a(n, vi(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  vvi state(1 << n, vi(n, 0));

  // memset(state, 0, sizeof(state));

  for (int i = 0; i < n; i++)
    if (a[i][0])
      state[1 << i][0] = 1;

  for (int i = 0; i < (1 << n); i++)
    for (int j = 1; j < n; j++)
      for (int k = 0; k < n; k++)
        if (((1 << k) & i) && a[k][j])
          state[i][j] = (state[i][j] + state[i ^ (1 << k)][j - 1]) % MOD;

  cout << state[(1 << n) - 1][n - 1] << endl;
}

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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvll;
typedef vector<vd> vvd;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pair<int, int>> vpii;

const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vvi a(n, vi(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  vvi state(1 << n, vi(n, 0));

  // memset(state, 0, sizeof(state));

  for (int i = 0; i < n; i++)
    if (a[i][0])
      state[1 << i][0] = 1;

  for (int i = 0; i < (1 << n); i++)
    for (int j = 1; j < n; j++)
      if (j == __builtin_popcount(i) - 1)
        for (int k = 0; k < n; k++)
          if (((1 << k) & i) && a[k][j])
            state[i][j] = (state[i][j] + state[i ^ (1 << k)][j - 1]) % MOD;

  cout << state[(1 << n) - 1][n - 1] << endl;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using vll = std::vector<long long>;
constexpr long long mod{1000000007};

int n;
bool edge[21][21]{};
std::vector<vll> mf;

long long rc(int, int);

int main() {
  scanf("%d", &n);
  for (int i{}; i < n; i++)
    for (int j{}; j < n; j++) {
      int a;
      scanf("%d", &a);
      edge[i][j] = a == 1;
    }

  mf.resize(n + 1, vll(1 << n, -1));
  mf[n][(1 << n) - 1] = 1;
  for (int i{}; i + 1 < 1 << n; i++)
    mf[n][i - 1] = 0;
  printf("%lld\n", rc(0, 0));

  return 0;
}

long long rc(int male, int set) {
  long long &combi{mf[male][set]};
  if (combi >= 0)
    return combi;
  combi = 0;
  for (int female{}; female < n; female++) {
    if (!edge[male][female] || ((1 << female) & set))
      continue;
    combi += rc(male + 1, set | (1 << female));
    combi %= mod;
  }
  return combi;
}

Fixed submission:
#include <bits/stdc++.h>

using vll = std::vector<long long>;
constexpr long long mod{1000000007};

int n;
bool edge[21][21]{};
std::vector<vll> mf;

long long rc(int, int);

int main() {
  scanf("%d", &n);
  for (int i{}; i < n; i++)
    for (int j{}; j < n; j++) {
      int a;
      scanf("%d", &a);
      edge[i][j] = a == 1;
    }

  mf.resize(n + 1, vll(1 << n, -1));
  mf[n][(1 << n) - 1] = 1;
  for (int i{}; i + 1 < 1 << n; i++)
    mf[n][i] = 0;
  printf("%lld\n", rc(0, 0));

  return 0;
}

long long rc(int male, int set) {
  long long &combi{mf[male][set]};
  if (combi >= 0)
    return combi;
  combi = 0;
  for (int female{}; female < n; female++) {
    if (!edge[male][female] || ((1 << female) & set))
      continue;
    combi += rc(male + 1, set | (1 << female));
    combi %= mod;
  }
  return combi;
}
Problem p03174 (C++)

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

#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef array<int, 3> tri;
typedef long double ld;

template <class T> istream &operator>>(istream &I, vector<T> &v) {
  for (T &e : v)
    I >> e;
  return I;
}
template <class T> ostream &operator<<(ostream &O, const vector<T> &v) {
  for (const T &e : v)
    O << e << ' ';
  return O;
}

const int mod = 1e9 + 7;
void _main() {
  int n;
  cin >> n;
  vector<vector<int>> match(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> match[i][j];
    }
  }

  vector<int> dp(1 << n);
  dp[0] = 1;
  for (int man = 0; man < n; man++) {
    vector<int> new_dp(1 << n);
    for (int mask = 0; mask < (1 << n); mask++) {
      for (int woman = 0; woman < n; woman++) {
        if (1 << woman & mask && match[man][woman]) {
          (new_dp[mask] += dp[mask ^ (1 << woman)]) %= mod;
        }
      }
    }
    dp = new_dp;
  }

  cout << dp.back();
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //        freopen("input.txt", "r", stdin);
  int _t = 1;
  //        cin >> _t;
  while (_t--)
    _main();
  return 0;
}

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

#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef array<int, 3> tri;
typedef long double ld;

template <class T> istream &operator>>(istream &I, vector<T> &v) {
  for (T &e : v)
    I >> e;
  return I;
}
template <class T> ostream &operator<<(ostream &O, const vector<T> &v) {
  for (const T &e : v)
    O << e << ' ';
  return O;
}

const int mod = 1e9 + 7;
void _main() {
  int n;
  cin >> n;
  vector<vector<int>> match(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> match[i][j];
    }
  }

  vector<int> dp(1 << n);
  dp[0] = 1;
  for (int man = 0; man < n; man++) {
    vector<int> new_dp(1 << n);
    for (int mask = 0; mask < (1 << n); mask++) {
      if (__builtin_popcount(mask) != man + 1)
        continue;
      for (int woman = 0; woman < n; woman++) {
        if (1 << woman & mask && match[man][woman]) {
          (new_dp[mask] += dp[mask ^ (1 << woman)]) %= mod;
        }
      }
    }
    dp = new_dp;
  }

  cout << dp.back();
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //        freopen("input.txt", "r", stdin);
  int _t = 1;
  //        cin >> _t;
  while (_t--)
    _main();
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 100100
const ll inf = (ll)1e10;
const ll MOD = (ll)1e9 + 7;
ll dp[21][1 << 21];
bool chk(int pos, int mask) { return mask & (1 << pos); }
int on(int pos, int mask) { return mask | (1 << pos); }
int n, done;
int adj[21][21];
ll solve(ll pos, ll mask) {
  if (dp[pos][mask] != -1)
    return dp[pos][mask];
  if (pos == n) {
    if (mask == done - 1) {
      return 1;
    } else
      return 0;
  }
  dp[pos][mask] = 0;
  for (int j = 0; j < n; j++) {
    if (adj[pos][j] && !(mask & (1 << j)))
      dp[pos][mask] = (dp[pos][mask] + solve(pos + 1, mask | (1 << j))) % MOD;
  }
  return dp[pos][mask];
}
int main() {
#ifdef __APPLE__
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> adj[i][j];
    }
  }
  done = (1 << n);
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 100100
const ll inf = (ll)1e10;
const ll MOD = (ll)1e9 + 7;
ll dp[22][1 << 22];
bool chk(int pos, int mask) { return mask & (1 << pos); }
int on(int pos, int mask) { return mask | (1 << pos); }
int n, done;
int adj[21][21];
ll solve(ll pos, ll mask) {
  if (dp[pos][mask] != -1)
    return dp[pos][mask];
  if (pos == n) {
    if (mask == done - 1) {
      return 1;
    } else
      return 0;
  }
  dp[pos][mask] = 0;
  for (int j = 0; j < n; j++) {
    if (adj[pos][j] && !(mask & (1 << j)))
      dp[pos][mask] = (dp[pos][mask] + solve(pos + 1, mask | (1 << j))) % MOD;
  }
  return dp[pos][mask];
}
int main() {
#ifdef __APPLE__
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> adj[i][j];
    }
  }
  done = (1 << n);
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define MAX 2999
ll mod = pow(10, 9) + 7;
ll dp[22][1 << 21];

using namespace std;

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

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

  dp[0][0] = 1;

  for (int i = 1; i <= n; i++) {
    for (int mask = 0; mask < int(1 << n); mask++) {
      for (int j = 0; j < n; j++) {
        if (A[i - 1][j] == 1 && int(mask & (1 << j)) != 0) {
          dp[i][mask] =
              (dp[i][mask] % mod + dp[i - 1][int(mask ^ (1 << j))] % mod) % mod;
        }
      }
    }
  }

  cout << dp[n][(1 << n) - 1];
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MAX 2999
ll mod = pow(10, 9) + 7;
ll dp[22][1 << 21];

using namespace std;

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

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

  dp[0][0] = 1;

  for (int i = 1; i <= n; i++) {
    for (int mask = 0; mask < int(1 << n); mask++) {
      int k = __builtin_popcount(mask);
      if (k != i)
        continue;
      for (int j = 0; j < n; j++) {
        if (A[i - 1][j] == 1 && int(mask & (1 << j)) != 0) {
          dp[i][mask] =
              (dp[i][mask] % mod + dp[i - 1][int(mask ^ (1 << j))] % mod) % mod;
        }
      }
    }
  }

  cout << dp[n][(1 << n) - 1];
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;

#define MOD 1000000007

int main() {

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n, 0));
  for (int i = 0; i <= n - 1; i++)
    for (int j = 0; j <= n - 1; j++)
      cin >> a[i][j];

  vector<vector<long long>> dp(n, vector<long long>((1 << n), 0));
  long long ans = 0;

  for (int i = 0; i <= n - 1; i++) {

    for (int m = 0; m <= (1 << n) - 1; m++) {

      for (int j = 0; j <= n - 1; j++) {

        if (a[i][j] == 1 and (m & (1 << j)) != 0) {

          if (i == 0)
            dp[i][m] = 1;

          else {
            dp[i][m] += dp[i - 1][m - (1 << j)];
            if (dp[i][m] >= MOD)
              dp[i][m] %= MOD;
          }
        }
      }

      if (i == n - 1) {
        ans += dp[i][m];
        if (ans >= MOD)
          ans %= MOD;
      }
    }
  }

  cout << ans;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;

#define MOD 1000000007

int main() {

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n, 0));
  for (int i = 0; i <= n - 1; i++)
    for (int j = 0; j <= n - 1; j++)
      cin >> a[i][j];

  vector<vector<long long>> dp(n, vector<long long>((1 << n), 0));
  long long ans = 0;

  for (int i = 0; i <= n - 1; i++) {

    for (int m = 0; m <= (1 << n) - 1; m++) {
      if (__builtin_popcount(m) != i + 1)
        continue;

      for (int j = 0; j <= n - 1; j++) {

        if (a[i][j] == 1 and (m & (1 << j)) != 0) {

          if (i == 0)
            dp[i][m] = 1;

          else {
            dp[i][m] += dp[i - 1][m - (1 << j)];
            if (dp[i][m] >= MOD)
              dp[i][m] %= MOD;
          }
        }
      }

      if (i == n - 1) {
        ans += dp[i][m];
        if (ans >= MOD)
          ans %= MOD;
      }
    }
  }

  cout << ans;

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
// In The Name Of God
#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define L(x) (2 * (x))
#define R(x) ((2 * (x)) + 1)
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int N = 4e2 + 5, Mod = 1e9 + 7, P = 727, Lg = 21, TOF = 1900;

int Dp[Lg][(1 << Lg)], A[Lg][Lg];
vector<int> V[Lg];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> A[i][j];
  for (int j = 1; j < (1 << n); j++)
    V[__builtin_popcount(j)].pb(j);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (int)V[i + 1].size(); j++) {
      int mask = V[i + 1][j], cur = V[i + 1][j];
      while (cur) {
        int ct = __builtin_ctz(cur);
        cur ^= (1 << ct);
        if (A[i][ct]) {
          if (i)
            Dp[i][mask] = (Dp[i][mask] + Dp[i - 1][mask ^ (1 << ct)]) % Mod;
          else
            Dp[i][mask] = 1;
        }
      }
    }
  }
  cout << Dp[n - 1][(1 << n) - 1];
  return 0;
}


Fixed submission:
// In The Name Of God
#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define L(x) (2 * (x))
#define R(x) ((2 * (x)) + 1)
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int N = 4e2 + 5, Mod = 1e9 + 7, P = 727, Lg = 22, TOF = 1900;

int Dp[Lg][(1 << Lg)], A[Lg][Lg];
vector<int> V[Lg];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> A[i][j];
  for (int j = 1; j < (1 << n); j++)
    V[__builtin_popcount(j)].pb(j);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (int)V[i + 1].size(); j++) {
      int mask = V[i + 1][j], cur = V[i + 1][j];
      while (cur) {
        int ct = __builtin_ctz(cur);
        cur ^= (1 << ct);
        if (A[i][ct]) {
          if (i)
            Dp[i][mask] = (Dp[i][mask] + Dp[i - 1][mask ^ (1 << ct)]) % Mod;
          else
            Dp[i][mask] = 1;
        }
      }
    }
  }
  cout << Dp[n - 1][(1 << n) - 1];
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

typedef long long ll;
ll dp[22][(1 << 21)];
int a[21][21];
const ll MOD = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  dp[0][0] = 1;

  for (int i = 0; i < n; i++) {
    for (int s = 0; s < (1 << n); s++) {
      for (int j = 0; j < n; j++) {
        if (a[i][j] == 1 && !((s >> j) & 1)) {
          //	  cout << "i = "<<i<<"  j = "<<j<<endl;
          dp[i + 1][s | (1 << j)] = (dp[i + 1][s | (1 << j)] + dp[i][s]) % MOD;
        }
      }
    }
  }
  /*  for(int i = 0; i < n; i++) {
    for(int j = 0; j < (1 << n); j++) {
      cout << dp[i][j] << endl;
    }
    }*/

  cout << dp[n][(1 << n) - 1] << endl;
}


Fixed submission:
#include <iostream>

using namespace std;

typedef long long ll;
ll dp[22][(1 << 21)];
int a[21][21];
const ll MOD = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  dp[0][0] = 1;

  for (int i = 0; i < n; i++) {
    for (int s = 0; s < (1 << n); s++) {
      if (__builtin_popcount(s) != i)
        continue;
      for (int j = 0; j < n; j++) {
        if (a[i][j] == 1 && !((s >> j) & 1)) {
          //	  cout << "i = "<<i<<"  j = "<<j<<endl;
          dp[i + 1][s | (1 << j)] = (dp[i + 1][s | (1 << j)] + dp[i][s]) % MOD;
        }
      }
    }
  }
  /*  for(int i = 0; i < n; i++) {
    for(int j = 0; j < (1 << n); j++) {
      cout << dp[i][j] << endl;
    }
    }*/

  cout << dp[n][(1 << n) - 1] << endl;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

ll MOD = 1000000007;

int main() {
  int N;
  cin >> N;

  vector<bitset<21>> a(N, 0);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int t;
      cin >> t;
      if (t == 1)
        a[i].set(j);
    }
  }

  vector<vector<ll>> r(N + 1, vector<ll>((1 << N)));

  for (int i = 0; i <= N; i++) {
    r[i][0] = 1;
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < (1 << N); j++) {
      for (int k = 0; k < N; k++) {
        if (a[i - 1].test(k) && ((j >> k) & 1)) {
          r[i][j] += r[i - 1][j ^ (1 << k)];
          r[i][j] %= MOD;
        }
      }
      // cout << r[i][j] << " ";
    }
    // cout << endl;
  }

  cout << (r[N][(1 << N) - 1] + MOD) % MOD << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

ll MOD = 1000000007;

int main() {
  int N;
  cin >> N;

  vector<bitset<21>> a(N, 0);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int t;
      cin >> t;
      if (t == 1)
        a[i].set(j);
    }
  }

  vector<vector<ll>> r(N + 1, vector<ll>((1 << N)));

  for (int i = 0; i <= N; i++) {
    r[i][0] = 1;
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < (1 << N); j++) {
      if (__builtin_popcount(j) != i)
        continue;
      for (int k = 0; k < N; k++) {
        if (a[i - 1].test(k) && ((j >> k) & 1)) {
          r[i][j] += r[i - 1][j ^ (1 << k)];
          r[i][j] %= MOD;
        }
      }
      // cout << r[i][j] << " ";
    }
    // cout << endl;
  }

  cout << (r[N][(1 << N) - 1] + MOD) % MOD << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

ll dp[22][1ll << 21];

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  rep(i, n) rep(j, n) cin >> a[i][j];
  dp[0][0] = 1;
  rep(i, n) {
    rep(s, 1ll << n) {
      // if(__builtin_popcount(s)!=i) continue;
      rep(j, n) {
        if (a[i][j] && (((1ll << j) & s) == 0)) {
          dp[i + 1][s | (1ll << j)] += dp[i][s];
          dp[i + 1][s | (1ll << j)] %= mod;
        }
      }
    }
  }
  ll ans = 0;
  rep(s, 1ll << n) {
    ans += dp[n][s];
    ans %= mod;
  }
  cout << ans << endl;
  /*
  rep(i,n+1){
    rep(j,1ll<<n){
      cout << dp[i][j];
    }cout << endl;
  }*/
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

ll dp[22][1ll << 21];

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  rep(i, n) rep(j, n) cin >> a[i][j];
  dp[0][0] = 1;
  rep(i, n) {
    rep(s, 1ll << n) if (__builtin_popcount(s) == i) {
      rep(j, n) {
        if (a[i][j] && (((1ll << j) & s) == 0)) {
          dp[i + 1][s | (1ll << j)] += dp[i][s];
          dp[i + 1][s | (1ll << j)] %= mod;
        }
      }
    }
  }
  ll ans = 0;
  rep(s, 1ll << n) {
    ans += dp[n][s];
    ans %= mod;
  }
  cout << ans << endl;
  /*
  rep(i,n+1){
    rep(j,1ll<<n){
      cout << dp[i][j];
    }cout << endl;
  }*/
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forn(i, n) for (int i = 0; i < n; ++i)
ll MOD = 1e9 + 7;
ll INF = 1e17 + 9;

ll dp(int mask, int women, vector<vector<bool>> comp, int n,
      vector<vector<ll>> save) {
  if (mask == (1 << n) - 1)
    return 1;
  else if (women >= n)
    return 0;
  else if (save[mask][women] != -1)
    return save[mask][women];

  ll res = 0;
  for (int bit = 0; bit < n; ++bit) {
    if (((mask >> bit) & 1) == 0 && comp[bit][women]) {
      res += dp(mask | (1 << bit), women + 1, comp, n, save);
      res %= MOD;
    }
  }
  save[mask][women] = res;
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vector<bool>> comp(n, vector<bool>(n, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      bool temp;
      cin >> temp;
      comp[i][j] = temp;
    }
  }
  vector<vector<ll>> save(1 << n, vector<ll>(n, -1));
  ll ans = dp(0, 0, comp, n, save);

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forn(i, n) for (int i = 0; i < n; ++i)
ll MOD = 1e9 + 7;
ll INF = 1e17 + 9;

ll dp(int mask, int women, vector<vector<bool>> &comp, int n,
      vector<vector<ll>> &save) {
  if (mask == (1 << n) - 1)
    return 1;
  else if (women >= n)
    return 0;
  else if (save[mask][women] != -1)
    return save[mask][women];

  ll res = 0;
  for (int bit = 0; bit < n; ++bit) {
    if (((mask >> bit) & 1) == 0 && comp[bit][women]) {
      res += dp(mask | (1 << bit), women + 1, comp, n, save);
      res %= MOD;
    }
  }
  save[mask][women] = res;
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vector<bool>> comp(n, vector<bool>(n, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      bool temp;
      cin >> temp;
      comp[i][j] = temp;
    }
  }
  vector<vector<ll>> save(1 << n, vector<ll>(n, -1));
  ll ans = dp(0, 0, comp, n, save);

  cout << ans << endl;
}
Problem p03174 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
const int N = 1e7, M = 7 + 1e9;
int n, t;
ll dp[21][N];
vector<int> v[21];
map<int, bool> sorryIhaveaboyfriend;
ll cal(int i, int taken) {
  if (i == n)
    return 1;
  ll &ret = dp[i][taken];
  if (~ret)
    return ret;
  ret = 0;
  f(j, 0, v[i].size()) {
    if (!sorryIhaveaboyfriend[v[i][j]]) {
      sorryIhaveaboyfriend[v[i][j]] = 1;
      ret += cal(i + 1, taken | (1 << v[i][j]));
      ret %= M;
      sorryIhaveaboyfriend[v[i][j]] = 0;
    }
  }
  return ret;
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  f(i, 0, n) {
    f(j, 0, n) {
      cin >> t;
      if (t)
        v[i].push_back(j);
    }
  }
  cout << cal(0, 0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
const int N = 1e7, M = 7 + 1e9;
int n, t;
ll dp[21][2097152]; // (1 << 21)-1 the maximum number for the mask
vector<int> v[21];
map<int, bool> sorryIhaveaboyfriend;
ll cal(int i, int taken) {
  if (i == n)
    return 1;
  ll &ret = dp[i][taken];
  if (~ret)
    return ret;
  ret = 0;
  f(j, 0, v[i].size()) {
    if (!sorryIhaveaboyfriend[v[i][j]]) {
      sorryIhaveaboyfriend[v[i][j]] = 1;
      ret += cal(i + 1, taken | (1 << v[i][j]));
      ret %= M;
      sorryIhaveaboyfriend[v[i][j]] = 0;
    }
  }
  return ret;
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  f(i, 0, n) {
    f(j, 0, n) {
      cin >> t;
      if (t)
        v[i].push_back(j);
    }
  }
  cout << cal(0, 0);
}
Problem p03174 (C++)

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

int main(void) {
  int N;
  cin >> N;

  int a[21][21] = {0};

  int state = pow(2, 21);
  vector<vector<int>> dp(21, vector<int>(state, 0));

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

  for (int i = 0; i < N; ++i) {
    if (a[0][i])
      dp.at(0).at(1 << i) = 1;
  }

  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (a[i][j]) {
        for (int k = 0; k < (1 << N); ++k) {
          if ((k >> j) % 2 == 0) {
            // dp.at(i).at(k+(1<<j)) = (dp.at(i).at(k+(1<<j)) +
            // dp.at(i-1).at(k))%1000000007;
            dp.at(i).at(k + (1 << j)) += dp.at(i - 1).at(k);
            dp.at(i).at(k + (1 << j)) %= 1000000007;
          }
        }
      }
    }
  }

  //    cout << (1<<N) << endl;
  cout << dp.at(N - 1).at((1 << N) - 1) << endl;

  return 0;
}


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

int main(void) {
  int N;
  cin >> N;

  int a[21][21] = {0};

  int state = pow(2, 21);
  vector<vector<int>> dp(21, vector<int>(state, 0));

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

  for (int i = 0; i < N; ++i) {
    if (a[0][i])
      dp.at(0).at(1 << i) = 1;
  }

  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (a[i][j]) {
        for (int k = 0; k < (1 << N); ++k) {
          if ((k >> j) % 2 == 0) {
            dp[i][k + (1 << j)] =
                (dp[i][k + (1 << j)] + dp[i - 1][k]) % 1000000007;
            // dp.at(i).at(k+(1<<j)) += dp.at(i-1).at(k);
            // dp.at(i).at(k+(1<<j)) %= 1000000007;
          }
        }
      }
    }
  }

  //    cout << (1<<N) << endl;
  cout << dp.at(N - 1).at((1 << N) - 1) << endl;

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
// KALAM
#include <bits/stdc++.h>

using namespace std;

const int N = 400 + 77, L = 22, Mod = 1000000007;
int n, dp[L][1 << L];
bool M[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> M[i][j];
  for (int i = 0; i < (1 << n); ++i)
    for (int j = 0; j < n; ++j)
      if ((i & (1 << j)) != 0 && M[0][j] == 1)
        ++dp[0][i];
  for (int i = 1; i < n; ++i) {
    for (int msk = 1; msk < (1 << n); ++msk) {
      for (int j = 0; j < n; ++j) {
        if (((1 << j) & msk) != 0 && M[i][j] == 1) {
          dp[i][msk] = (dp[i][msk] + dp[i - 1][msk ^ (1 << j)]);
          if (dp[i][msk] >= Mod)
            dp[i][msk] -= Mod;
        }
      }
    }
  }
  cout << dp[n - 1][(1 << n) - 1];
  return 0;
}


Fixed submission:
// KALAM
#include <bits/stdc++.h>

using namespace std;

const int N = 400 + 77, L = 22, Mod = 1000000007;
int n, dp[L][1 << L];
bool M[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> M[i][j];
  for (int i = 0; i < (1 << n); ++i)
    for (int j = 0; j < n; ++j)
      if ((i & (1 << j)) != 0 && M[0][j] == 1)
        ++dp[0][i];
  for (int i = 1; i < n; ++i) {
    for (int msk = 1; msk < (1 << n); ++msk) {
      if (__builtin_popcount(msk) != i + 1)
        continue;
      for (int j = 0; j < n; ++j) {
        if (((1 << j) & msk) != 0 && M[i][j] == 1) {
          dp[i][msk] = (dp[i][msk] + dp[i - 1][msk ^ (1 << j)]);
          if (dp[i][msk] >= Mod)
            dp[i][msk] -= Mod;
        }
      }
    }
  }
  cout << dp[n - 1][(1 << n) - 1];
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
int dp[1 << 21][21];

vector<int> a[21];
int DONE, n;

int solve(int bitmask, int ind) {
  // base case
  if (bitmask == DONE) {
    return 1;
  }
  if (ind >= n)
    return 0;
  if (dp[bitmask][ind] != -1)
    return dp[bitmask][ind];
  int ans = 0;
  // cout<<"printing  "<<bitmask<<ind<<endl;
  for (int aa : a[ind]) {
    // cout<<"printing  "<<bitmask<<" "<<ind<<endl;
    if ((bitmask & (1 << aa)) == 0)
      ans += solve(bitmask | (1 << aa), ind + 1);
    ans %= mod;
  }
  ans += solve(bitmask, ind + 1);
  ans %= mod;
  return dp[bitmask][ind] = ans;
}

int main() {
  cin >> n;
  int x;
  DONE = (1 << n) - 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> x;
      // cout<<x<<endl;
      if (x == 1)
        a[i].push_back(j);
    }
  } /*
   for(int i=0;i<n;i++){
       for(int aa:a[i])
           cout<<aa;
       cout<<endl;
   }*/
  memset(dp, -1, sizeof(dp));
  int ans = solve(0, 0);
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
int dp[1 << 21][21];

vector<int> a[21];
int DONE, n;

int solve(int bitmask, int ind) {
  // base case
  if (bitmask == DONE) {
    return 1;
  }
  if (ind >= n)
    return 0;
  if (dp[bitmask][ind] != -1)
    return dp[bitmask][ind];
  int ans = 0;
  // cout<<"printing  "<<bitmask<<ind<<endl;
  for (int aa : a[ind]) {
    // cout<<"printing  "<<bitmask<<" "<<ind<<endl;
    if ((bitmask & (1 << aa)) == 0)
      ans += solve(bitmask | (1 << aa), ind + 1);
    ans %= mod;
  }
  // ans+=solve(bitmask,ind+1);
  ans %= mod;
  return dp[bitmask][ind] = ans;
}

int main() {
  cin >> n;
  int x;
  DONE = (1 << n) - 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> x;
      // cout<<x<<endl;
      if (x == 1)
        a[i].push_back(j);
    }
  } /*
   for(int i=0;i<n;i++){
       for(int aa:a[i])
           cout<<aa;
       cout<<endl;
   }*/
  memset(dp, -1, sizeof(dp));
  int ans = solve(0, 0);
  cout << ans << '\n';
  return 0;
}