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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MOD 1000000007
#define LLI long long int
using namespace std;

int N;
LLI dp[22][1 << 21] = {};
int a[21][21] = {};

int main() {

  int i, j;
  int N;

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

  dp[0][0] = 1;
  for (i = 0; i < N; i++) {
    for (j = 0; j < (1 << N); j++) {
      for (int k = 0; k < N; k++) {
        if (a[i][k] && (j & (1 << k))) {
          dp[i + 1][j] += dp[i][j ^ (1 << k)];
          dp[i + 1][j] %= MOD;
        }
      }
    }
  }

  LLI ans = dp[N][(1 << N) - 1];
  printf("%lld\n", ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define LLI long long int
using namespace std;

int N;
LLI dp[22][1 << 21] = {};
int a[21][21] = {};

int main() {

  int i, j;
  int N;

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

  dp[0][0] = 1;
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      if (!a[i][j])
        continue;
      for (int k = 0; k < (1 << N); k++) {
        if (k & (1 << j)) {
          dp[i + 1][k] += dp[i][k ^ (1 << j)];
          dp[i + 1][k] %= MOD;
        }
      }
    }
  }

  LLI ans = dp[N][(1 << N) - 1];
  printf("%lld\n", ans);

  return 0;
}
Problem p03174 (C++)

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

const int N = 21;

int dp[N + 1][1 << N];
bool a[N][N];
int n;
const int MOD = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}

void solve() {
  memset(dp, 0, sizeof dp);
  dp[0][0] = 1; // only one way to distribute 0 man and 0 women
  for (int i = 0; i < n; ++i) {
    for (int mask = 0; mask < (1 << n); ++mask) {
      for (int j = 0; j < n; ++j) {
        if (!(mask & (1 << j)) && a[i][j]) {
          add(dp[i + 1][mask | (1 << j)], dp[i][mask]);
        }
      }
    }
  }
  printf("%d\n", dp[n][(1 << n) - 1]);
}

int main() {
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        scanf("%d", a[i] + j);
      }
    }
    solve();
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 21;

int dp[N + 1][1 << N];
bool a[N][N];
int n;
const int MOD = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}

void solve() {
  memset(dp, 0, sizeof dp);
  dp[0][0] = 1; // only one way to distribute 0 man and 0 women
  for (int i = 0; i < n; ++i) {
    for (int mask = 0; mask < (1 << n); ++mask) {
      if (__builtin_popcount(mask) != i)
        continue;
      for (int j = 0; j < n; ++j) {
        if (!(mask & (1 << j)) && a[i][j]) {
          add(dp[i + 1][mask | (1 << j)], dp[i][mask]);
        }
      }
    }
  }
  printf("%d\n", dp[n][(1 << n) - 1]);
}

int main() {
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        scanf("%d", a[i] + j);
      }
    }
    solve();
  }
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f first
#define s second
#define pb push_back
#define mod 1000000007
ll dp[1 << 22];
ll co(ll n) {
  ll g = n;
  ll c = 0;
  while (g > 0) {
    c += (g % 2);
    g /= 2;
  }
  return c;
}
void solve() {
  ll n;
  cin >> n;
  ll g = 1ll << n;
  ll a[30][30];
  for (int x = 0; x < n; x++)
    for (int y = 0; y < n; y++)
      cin >> a[x][y];
  dp[0] = 1;
  for (ll y = 0; y < n; y++) {
    for (ll x = 0; x < g; x++) {
      ll d = co(x);
      if (y + 1 == d)
        for (ll z = 0; z < n; z++) {
          if (a[y][z] == 1 && x & (1ll << z)) {
            dp[x] += dp[x ^ (1ll << z)];
            dp[x] %= mod;
          }
        }
    }
  }
  cout << dp[g - 1] << "\n";
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll i = 1;
  // cin>>i;
  ll k = 0;
  while (k < i) {
    solve();
    ++k;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f first
#define s second
#define pb push_back
#define mod 1000000007
ll dp[1 << 22];
ll co(ll n) {
  ll g = n;
  ll c = 0;
  while (g > 0) {
    c += (g % 2);
    g /= 2;
  }
  return c;
}
void solve() {
  ll n;
  cin >> n;
  ll g = 1ll << n;
  ll a[30][30];
  for (int x = 0; x < n; x++)
    for (int y = 0; y < n; y++)
      cin >> a[x][y];
  dp[0] = 1;
  for (ll y = 0; y < n; y++) {
    for (ll x = 0; x < g; x++) {
      ll d = co(x);
      if (y + 1 == d)
        for (ll z = 0; z < n; z++) {
          if (a[y][z] == 1 && x & (1ll << z)) {
            dp[x] += dp[x ^ (1ll << z)];
            dp[x] %= mod;
          }
        }
    }
  }
  cout << dp[g - 1] << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll i = 1;
  // cin>>i;
  ll k = 0;
  while (k < i) {
    solve();
    ++k;
  }
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
// namespace mp = boost::multiprecision;
// using llll = mp::cpp_int;
//  仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
// using Real = mp::number<mp::cpp_dec_float<1024>>;
using ll = long long;
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<vector<ll>> v(n, vector<ll>(n, 0));
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      cin >> v[i][j];
    }
  }
  vector<vector<ll>> dp(n + 1, vector<ll>((ll)1 << n, 0));
  dp[0][0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < (ll)1 << n; j++) {
      for (ll l = 0; l < n; l++) {
        if ((j & ((ll)1 << l)) == 0 && v[i - 1][l] > 0) {
          dp[i][(j | ((ll)1 << l))] =
              (dp[i][(j | ((ll)1 << l))] + dp[i - 1][j]) % MOD;
        }
      }
    }
  }
  ans = dp[n][((ll)1 << n) - 1];
  cout << ans << endl;
}


Fixed submission:
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
// namespace mp = boost::multiprecision;
// using llll = mp::cpp_int;
//  仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
// using Real = mp::number<mp::cpp_dec_float<1024>>;
using ll = long long;
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<vector<ll>> v(n, vector<ll>(n, 0));
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      cin >> v[i][j];
    }
  }
  vector<vector<ll>> dp(n + 1, vector<ll>((ll)1 << n, 0));
  dp[0][0] = 1;
  for (ll j = 0; j < (ll)1 << n; j++) {
    ll i = 1;
    for (ll a = 0; a < n; a++) {
      if (j & ((ll)1 << a))
        i++;
    }
    for (ll l = 0; l < n; l++) {
      if ((j & ((ll)1 << l)) == 0 && v[i - 1][l] > 0) {
        dp[i][(j | ((ll)1 << l))] =
            (dp[i][(j | ((ll)1 << l))] + dp[i - 1][j]) % MOD;
      }
    }
  }
  ans = dp[n][((ll)1 << n) - 1];
  cout << ans << endl;
}
Problem p03174 (C++)

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

int dp[22][(1 << 22)];
int main(void) {
  int N;
  cin >> N;
  int a[22][22];
  memset(a, 0, sizeof(a));
  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 used = 0; used < (1 << N); used++) {
      for (int nx = 0; nx < N; nx++) {
        if ((used >> nx) & 1)
          continue;
        if (!a[i][nx])
          continue;
        dp[i + 1][used | (1 << nx)] += dp[i][used];
        dp[i + 1][used | (1 << nx)] %= 1000000007;
      }
    }
  }

  cout << dp[N][(1 << N) - 1] << endl;

  return 0;
}

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

int dp[22][(1 << 22)];
int main(void) {
  int N;
  cin >> N;
  int a[22][22];
  memset(a, 0, sizeof(a));
  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 used = 0; used < (1 << N); used++) {
      if (dp[i][used] == 0)
        continue;
      for (int nx = 0; nx < N; nx++) {
        if ((used >> nx) & 1)
          continue;
        if (!a[i][nx])
          continue;
        dp[i + 1][used | (1 << nx)] += dp[i][used];
        dp[i + 1][used | (1 << nx)] %= 1000000007;
      }
    }
  }

  cout << dp[N][(1 << N) - 1] << endl;

  return 0;
}
Problem p03174 (C++)

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

using namespace std;
using ll = long long;

const int mod = 1e9 + 7;

int n;
vector<vector<ll>> a, dp;

int main() {
  cin >> n;
  a.resize(n + 1, vector<ll>(n + 1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  dp.resize(n + 1, vector<ll>(1 << n));
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int mask = 0; mask <= (1 << n) - 1; mask++) {
      for (int j = 0; j < n; j++) {
        if ((mask & (1 << j)) && a[i][j + 1]) {
          dp[i][mask] = (dp[i][mask] + dp[i - 1][mask ^ (1 << j)]) % mod;
        }
      }
    }
  }
  ll res = dp[n][(1 << n) - 1];
  cout << res << endl;
  return 0;
}

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

using namespace std;
using ll = long long;

const int mod = 1e9 + 7;

int n;
vector<vector<ll>> a, dp;

int main() {
  cin >> n;
  a.resize(n + 1, vector<ll>(n + 1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  dp.resize(n + 1, vector<ll>(1 << n));
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int mask = 0; mask <= (1 << n) - 1; mask++) {
      if (__builtin_popcount(mask) != i)
        continue; // number of boys == number of girls
      for (int j = 0; j < n; j++) {
        if ((mask & (1 << j)) && a[i][j + 1]) {
          dp[i][mask] = (dp[i][mask] + dp[i - 1][mask ^ (1 << j)]) % mod;
        }
      }
    }
  }
  ll res = dp[n][(1 << n) - 1];
  cout << res << endl;
  return 0;
}
Problem p03174 (C++)

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

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

ll modpow(ll x, ll n, ll mod = MOD) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(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 j = 0; j < (1 << n); j++) {
      if (__builtin_popcount(j) != i)
        continue;
      for (int k = 0; k < n; k++) {
        if (j & (1 << k))
          continue;
        if (a[i][k] == 0)
          continue;
        (dp[i + 1][j | (1 << k)] += dp[i][j]) %= MOD;
      }
    }
  }
  cout << dp[n][(1 << n) - 1] << endl;
  return 0;
}

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

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

ll modpow(ll x, ll n, ll mod = MOD) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(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 j = 0; j < (1 << n); j++) {
      if (__builtin_popcount(j) != i)
        continue;
      for (int k = 0; k < n; k++) {
        if (j & (1 << k))
          continue;
        if (a[i][k] == 0)
          continue;
        (dp[i + 1][j | (1 << k)] += dp[i][j]) %= MOD;
      }
    }
  }
  cout << dp[n][(1 << n) - 1] << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int inf = 1e9 + 1;
const double eps = 1e-9;
const int MOD = 1e9 + 7;
const int MAXN = 21;

int n;
int a[MAXN][MAXN];
int dp[MAXN][(int)1 << MAXN];
vector<int> v[MAXN];

int main() {

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

  for (int mask = 0; mask < (int)1 << n; mask++) {
    int br = 0;
    for (int i = 0; i < n; i++)
      if (mask & 1 << i)
        br++;
    v[br].pb(mask);
  }

  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < v[i - 1].size(); j++) {
      for (int k = 0; k < n; k++) {
        if (a[i - 1][k] && (v[i - 1][j] & 1 << (n - 1 - k)) == 0) {
          dp[i][v[i - 1][j] ^ 1 << (n - 1 - k)] =
              (dp[i][v[i - 1][j] ^ 1 << (n - 1 - k)] % MOD +
               dp[i - 1][v[i - 1][j]] % MOD) %
              MOD;
          // cout << i << " " << k << " " << dp[i][v[i-1][j] ^ 1<<(n-1-k)] <<
          // '\n';
        }
      }
    }
  }

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int inf = 1e9 + 1;
const double eps = 1e-9;
const int MOD = 1e9 + 7;
const int MAXN = 22;

int n;
int a[MAXN][MAXN];
int dp[MAXN][(int)1 << MAXN];
vector<int> v[MAXN];

int main() {

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

  for (int mask = 0; mask < (int)1 << n; mask++) {
    int br = 0;
    for (int i = 0; i < n; i++)
      if (mask & 1 << i)
        br++;
    v[br].pb(mask);
  }

  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < v[i - 1].size(); j++) {
      for (int k = 0; k < n; k++) {
        if (a[i - 1][k] && (v[i - 1][j] & 1 << (n - 1 - k)) == 0) {
          dp[i][v[i - 1][j] ^ 1 << (n - 1 - k)] =
              (dp[i][v[i - 1][j] ^ 1 << (n - 1 - k)] % MOD +
               dp[i - 1][v[i - 1][j]] % MOD) %
              MOD;
          // cout << i << " " << k << " " << dp[i][v[i-1][j] ^ 1<<(n-1-k)] <<
          // '\n';
        }
      }
    }
  }

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

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int mp[25][25];
int mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> mp[i][j];
    }
  }
  vector<int> dp(1 << n);
  dp[0] = 1;
  for (int man = 0; man < n; ++man) {
    vector<int> currDp(1 << n);
    for (int mask = 0; mask < (1 << n); ++mask) {
      for (int women = 0; women < n; ++women) {
        if (mp[man][women] == 1 && ((mask & (1 << women)) == 0)) {
          int next = mask ^ (1 << women);
          currDp[next] += dp[mask];
          currDp[next] %= mod;
        }
      }
    }
    dp = currDp;
  }
  /*
  for(int i=0;i<(1<<n);++i){
    cout<<dp[i]<<" ";
  }
  */
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int mp[25][25];
int mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> mp[i][j];
    }
  }
  vector<int> dp(1 << n);
  dp[0] = 1;
  for (int man = 0; man < n; ++man) {
    vector<int> currDp(1 << n);
    for (int mask = 0; mask < (1 << n); ++mask) {
      if (__builtin_popcount(mask) != man) {
        continue;
      }
      for (int women = 0; women < n; ++women) {
        if (mp[man][women] == 1 && ((mask & (1 << women)) == 0)) {
          int next = mask ^ (1 << women);
          currDp[next] += dp[mask];
          currDp[next] %= mod;
        }
      }
    }
    dp = currDp;
  }
  /*
  for(int i=0;i<(1<<n);++i){
    cout<<dp[i]<<" ";
  }
  */
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
#define MAX_DOUBLE 40000000000.0
#include <math.h>
#include <unistd.h>

using namespace std;

long long mod = 1e9 + 7;

bool graph[21][21];

long long res[22][2097152];

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      scanf("%d", &a);
      if (a == 1)
        graph[i][j] = true;
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 2097152; j++) {
      for (int z = 0; z < n; z++) {

        if (j < (1 << z))
          break;

        if (graph[i][z] && j >= (1 << z) && (j & (1 << z))) {
          if (i == n - 1) {
            res[i][j] = 1;
          } else {
            res[i][j] = (res[i][j] + res[i + 1][j - (1 << z)]) % mod;
          }
        }
      }
    }
  }

  printf("%d", res[0][(1 << n) - 1]);
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
#define MAX_DOUBLE 40000000000.0
#include <math.h>
#include <unistd.h>

using namespace std;

long long mod = 1e9 + 7;

bool graph[21][21];

long long res[22][2097152];

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      scanf("%d", &a);
      if (a == 1)
        graph[i][j] = true;
    }
  }

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

      int count = 0;
      for (int z = 0; z < n; z++) {
        count += ((j & (1 << z)) > 0);
      }
      if (count != (n - i))
        continue;

      for (int z = 0; z < n; z++) {

        if (j < (1 << z))
          break;

        if (graph[i][z] && j >= (1 << z) && (j & (1 << z))) {
          if (i == n - 1) {
            res[i][j] = 1;
          } else {
            res[i][j] = (res[i][j] + res[i + 1][j - (1 << z)]) % mod;
          }
        }
      }
    }
  }

  printf("%d", res[0][(1 << n) - 1]);
}
Problem p03174 (C++)

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

int MOD = 1000 * 1000 * 1000 + 7;

const int MXN = 21;

int n;
int match[MXN][MXN];

int dp[MXN][(1 << MXN)];

int ways(int x, int bts) {
  if (x == n) {
    return (bts == ((1 << n) - 1));
  }
  if (dp[x][bts] != -1)
    return dp[x][bts];
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (match[x][i]) {
      res += ways(x + 1, (bts | (1 << i)));
      res = res % MOD;
    }
  }
  return dp[x][bts] = res;
}

int main() {

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

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

  cout << ways(0, 0);
}


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

int MOD = 1000 * 1000 * 1000 + 7;

const int MXN = 21;

int n;
int match[MXN][MXN];

int dp[MXN][(1 << MXN)];

int ways(int x, int bts) {
  if (x == n) {
    return (bts == ((1 << n) - 1));
  }
  if (dp[x][bts] != -1)
    return dp[x][bts];
  int res = 0;
  for (int i = 0; i < n; i++) {
    if ((1 << i) & bts)
      continue;
    if (match[x][i]) {
      res += ways(x + 1, (bts | (1 << i)));
      res = res % MOD;
    }
  }
  return dp[x][bts] = res;
}

int main() {

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

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

  cout << ways(0, 0);
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const int mod = 1000000000 + 7;

int count_bit(int n) {
  int ret = 0;
  rep(i, 30) {
    if (n & (1 << i))
      ret++;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> c(n, vector<int>(n));
  rep(i, n) rep(j, n) cin >> c[i][j];
  vector<vector<int>> dp(n + 1, vector<int>(1 << n));
  dp[0][0] = 1;
  rep(i, n) for (int j = 0; j < (1 << n); j++) if (i == count_bit(j)) {
    rep(w, n) if (i + 1 == count_bit(j | (1 << w))) if (c[i][w]) {
      dp[i + 1][j | (1 << w)] += dp[i][j];
      dp[i + 1][j | (1 << w)] %= mod;
    }
  }
  cout << dp[n][(1 << n) - 1] << endl;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const int mod = 1000000000 + 7;

int count_bit(int n) {
  int ret = 0;
  rep(i, 30) {
    if (n & (1 << i))
      ret++;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> c(n, vector<int>(n));
  rep(i, n) rep(j, n) cin >> c[i][j];
  vector<vector<int>> dp(n + 1, vector<int>(1 << n));
  dp[0][0] = 1;
  rep(i, n) for (int j = 0; j < (1 << n); j++) if (i == count_bit(j)) {
    rep(w, n) if (j != j | (1 << w)) if (c[i][w]) {
      dp[i + 1][j | (1 << w)] += dp[i][j];
      dp[i + 1][j | (1 << w)] %= mod;
    }
  }
  cout << dp[n][(1 << n) - 1] << endl;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vl dy = {1, -1, 0, 1, -1, 1, 0, -1};
vl dp(1 << 21);
int main() {
  dp[0] = 1;
  ll n;
  cin >> n;
  vvl a(n, vl(n));
  rep(i, n) { rep(j, n) cin >> a[i][j]; }
  for (ll S = 1; S < (1 << 21); S++) {
    ll i = __builtin_popcount(S);
    rep(j, n) {
      if ((S & (1 << j)) && a[i - 1][j])
        dp[S] = (dp[S] + dp[S ^ (1 << j)]) % MOD;
    }
  }
  ll ans = dp[(1 << n) - 1];
  cout << ans << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vl dy = {1, -1, 0, 1, -1, 1, 0, -1};
vl dp(1 << 21);
int main() {
  dp[0] = 1;
  ll n;
  cin >> n;
  vvl a(n, vl(n));
  rep(i, n) { rep(j, n) cin >> a[i][j]; }
  for (ll S = 1; S < (1 << n); S++) {
    ll i = __builtin_popcount(S);
    rep(j, n) {
      if ((S & (1 << j)) && a[i - 1][j])
        dp[S] = (dp[S] + dp[S ^ (1 << j)]) % MOD;
    }
  }
  ll ans = dp[(1 << n) - 1];
  cout << ans << endl;
}
Problem p03174 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
const int MN = 24, MOD = 1e9 + 7;
int N, A[MN][MN], dp[MN][1 << MN];
inline int cb(int msk, int b) { return msk & ~(1 << b); }
inline int sb(int msk, int b) { return msk | (1 << b); }
inline int tb(int msk, int b) { return msk ^ (1 << b); }
inline bool ib(int msk, int b) { return msk & (1 << b); }
int solve(int n, int msk) {
  if (n < 0)
    return 1;
  if (~dp[n][msk])
    return dp[n][msk];
  dp[n][msk] = 0;
  for (int i = 0; i < N; ++i)
    if (A[n][i] && !ib(msk, i))
      dp[n][msk] = (dp[n][msk] + solve(n - 1, sb(msk, i))) % MOD;
  return dp[n][msk];
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j)
      scanf("%d", &A[i][j]);
  memset(dp, -1, sizeof dp);
  printf("%d\n", solve(N - 1, 0));
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
const int MN = 21, MOD = 1e9 + 7;
int N, A[MN][MN], dp[MN][1 << MN];
inline int cb(int msk, int b) { return msk & ~(1 << b); }
inline int sb(int msk, int b) { return msk | (1 << b); }
inline int tb(int msk, int b) { return msk ^ (1 << b); }
inline bool ib(int msk, int b) { return msk & (1 << b); }
int solve(int n, int msk) {
  if (n < 0)
    return 1;
  if (~dp[n][msk])
    return dp[n][msk];
  dp[n][msk] = 0;
  for (int i = 0; i < N; ++i)
    if (A[n][i] && !ib(msk, i))
      dp[n][msk] = (dp[n][msk] + solve(n - 1, sb(msk, i))) % MOD;
  return dp[n][msk];
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j)
      scanf("%d", &A[i][j]);
  memset(dp, -1, sizeof dp);
  printf("%d\n", solve(N - 1, 0));
}
Problem p03174 (C++)

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

using namespace std;

// ref: https://atcoder.jp/contests/dp/submissions/7981121

int main(int argc, char const *argv[]) {
  int MOD = 1000000007;
  int N;
  cin >> N;
  vector<vector<int>> a(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
    }
  }

  vector<vector<int>> dp(N + 1, vector<int>(1 << N, 0));
  dp[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int mask = 0; mask < (1 << N); mask++) {
      for (int j = 0; j < N; j++) {
        if (a[i - 1][j] && (mask & (1 << j))) {
          dp[i][mask] += dp[i - 1][mask ^ (1 << j)];
          if (dp[i][mask] >= MOD) {
            dp[i][mask] -= MOD;
          }
        }
      }
    }
  }
  // for (int i = 0; i <= N; i++) {
  //     for (int mask = 0; mask < (1 << N); mask++) {
  //         cout << dp[i][mask] << ", ";
  //     }
  //     cout << endl;
  // }
  cout << dp[N][(1 << N) - 1] << endl;
  return 0;
}


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

using namespace std;

// ref: https://atcoder.jp/contests/dp/submissions/7981121

int main(int argc, char const *argv[]) {
  int MOD = 1000000007;
  int N;
  cin >> N;
  vector<vector<int>> a(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
    }
  }

  vector<vector<int>> dp(N + 1, vector<int>(1 << N, 0));
  dp[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int mask = 0; mask < (1 << N); mask++) {
      if (__builtin_popcount(mask) != i) {
        continue;
      }
      for (int j = 0; j < N; j++) {
        if (a[i - 1][j] && (mask & (1 << j))) {
          dp[i][mask] += dp[i - 1][mask ^ (1 << j)];
          if (dp[i][mask] >= MOD) {
            dp[i][mask] -= MOD;
          }
        }
      }
    }
  }
  // for (int i = 0; i <= N; i++) {
  //     for (int mask = 0; mask < (1 << N); mask++) {
  //         cout << dp[i][mask] << ", ";
  //     }
  //     cout << endl;
  // }
  cout << dp[N][(1 << N) - 1] << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define Graph vector<vector<ll>>
#define INF (1ll << 60)
#define mod 1000000007
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;
}

int main() {
  ll n;
  cin >> n;
  vector<vector<ll>> a(n, vector<ll>(n));
  rep(i, n) rep(j, n) cin >> a[i][j];

  vector<vector<ll>> dp(25, vector<ll>(1 << n, 0));
  dp[0][0] = 1;

  rep(i, n) {          // i番目の男性まで組を作っている
    rep(j, (1 << n)) { // すでに組んでいる女性をbitで表現
      rep(k, n) {
        if (!((j >> k) & 1) && a[i][k]) {
          dp[i + 1][j | (1 << k)] += dp[i][j];
          dp[i + 1][j | (1 << k)] %= mod;
        }
      }
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define Graph vector<vector<ll>>
#define INF (1ll << 60)
#define mod 1000000007
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;
}

int main() {
  ll n;
  cin >> n;
  vector<vector<ll>> a(n, vector<ll>(n));
  rep(i, n) rep(j, n) cin >> a[i][j];

  vector<vector<ll>> dp(25, vector<ll>(1 << n, 0));
  dp[0][0] = 1;

  rep(i, n) {          // i番目の男性まで組を作っている
    rep(j, (1 << n)) { // すでに組んでいる女性をbitで表現
      if (dp[i][j] == 0)
        continue;
      rep(k, n) {
        if (!((j >> k) & 1) && a[i][k]) {
          dp[i + 1][j | (1 << k)] += dp[i][j];
          dp[i + 1][j | (1 << k)] %= mod;
        }
      }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define debug(x) cerr << #x << ": " << x << endl
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<vector<bool>> canmatch(n, vector<bool>(n));
  rep(i, 0, n) rep(j, 0, n) {
    int x;
    cin >> x;
    canmatch[i][j] = x == 1;
  }
  vector<ll> dp(1 << n, 0);
  dp[0] = 1;
  const ll MOD = 1e9 + 7;
  rep(i, 0, n) {
    vector<ll> next(1 << n, 0);
    rep(mask, 0, 1 << n) {
      rep(j, 0, n) if (canmatch[i][j] && (mask & (1 << j)) == 0) {
        next[mask | (1 << j)] = (next[mask | (1 << j)] + dp[mask]) % MOD;
      }
    }
    swap(next, dp);
  }
  ll ans = dp[(1 << n) - 1];
  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define debug(x) cerr << #x << ": " << x << endl
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<vector<bool>> canmatch(n, vector<bool>(n));
  rep(i, 0, n) rep(j, 0, n) {
    int x;
    cin >> x;
    canmatch[i][j] = x == 1;
  }
  vector<ll> dp(1 << n, 0);
  dp[0] = 1;
  const ll MOD = 1e9 + 7;
  rep(i, 0, n) {
    vector<ll> next(1 << n, 0);
    rep(mask, 0, 1 << n) if (dp[mask] != 0) {
      rep(j, 0, n) if (canmatch[i][j] && (mask & (1 << j)) == 0) {
        next[mask | (1 << j)] = (next[mask | (1 << j)] + dp[mask]) % MOD;
      }
    }
    swap(next, dp);
  }
  ll ans = dp[(1 << n) - 1];
  cout << ans << "\n";
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define FASTIO                                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define INF 10000000000099ll
#define mod 1000000007
#define prDouble(x) cout << fixed << setprecision(10) << x

ll dp[22][(1 << 22)];

ll solve(vector<vector<int>> &compat, int i, int womenSubset, int n) {
  if (i == n + 1) {
    if (womenSubset == 0)
      return 1;
    return 0;
  }
  if (dp[i][womenSubset] != -1) {
    return dp[i][womenSubset];
  }
  ll ans = 0;
  for (int women = 0; women < n; women++) {
    bool available = ((1 << women) & (womenSubset) == 0) ? 0 : 1;
    if (available && compat[i][women + 1]) {
      ans = (ans + solve(compat, i + 1, womenSubset ^ (1 << women), n)) % mod;
    }
  }
  return dp[i][womenSubset] = ans;
}

int main() {
  FASTIO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<vector<int>> compat(n + 1, vector<int>(n + 1));
  memset(dp, -1, sizeof dp);
  loop(i, 1, n + 1) loop(j, 1, n + 1) cin >> compat[i][j];
  cout << solve(compat, 1, ((1 << (n)) - 1), n); // 2^n-1
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define FASTIO                                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);
#define loop(i, a, b) for (int i = a; i < b; i++)
#define INF 10000000000099ll
#define mod 1000000007
#define prDouble(x) cout << fixed << setprecision(10) << x

ll dp[22][(1 << 22)];

ll solve(vector<vector<int>> &compat, int i, int womenSubset, int n) {
  if (i == n + 1) {
    if (womenSubset == 0)
      return 1;
    return 0;
  }
  if (dp[i][womenSubset] != -1) {
    return dp[i][womenSubset];
  }
  ll ans = 0;
  for (int women = 0; women < n; women++) {
    bool available = (((1 << women) & (womenSubset)) == 0) ? 0 : 1;
    if (available && compat[i][women + 1]) {
      ans = (ans + solve(compat, i + 1, womenSubset ^ (1 << women), n)) % mod;
    }
  }
  return dp[i][womenSubset] = ans;
}

int main() {
  FASTIO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<vector<int>> compat(n + 1, vector<int>(n + 1));
  memset(dp, -1, sizeof dp);
  loop(i, 1, n + 1) loop(j, 1, n + 1) cin >> compat[i][j];
  cout << solve(compat, 1, ((1 << (n)) - 1), n); // 2^n-1
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mo 1000000007
#define ll long long int
int n;
int a[22][22];
ll dp[1500000][22];

ll func(int bm, int i) {
  if (i >= n)
    return (1);

  if (dp[bm][i] != -1)
    return (dp[bm][i]);

  ll ans = 0;

  for (int j = 0; j < n; j++) {
    int p = 1 << j;
    p = bm & p;

    if (a[i][j] && (p)) {
      int s = bm ^ p;
      ans = (ans + func(s, i + 1)) % mo;
    }
  }
  return (dp[bm][i] = ans);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  }

  int bm = 0;
  for (int j = 0; j < n; j++) {
    bm = bm << 1;
    bm = bm | 1;
  }
  memset(dp, -1, sizeof(dp));
  cout << func(bm, 0);
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mo 1000000007
#define ll long long int
int n;
int a[22][22];
ll dp[2200000][22];

ll func(int bm, int i) {
  if (i >= n)
    return (1);

  if (dp[bm][i] != -1)
    return (dp[bm][i]);

  ll ans = 0;

  for (int j = 0; j < n; j++) {
    int p = 1 << j;
    p = bm & p;

    if (a[i][j] && (p)) {
      int s = bm ^ p;
      ans = (ans + func(s, i + 1)) % mo;
    }
  }
  return (dp[bm][i] = ans);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  }

  int bm = 0;
  for (int j = 0; j < n; j++) {
    bm = bm << 1;
    bm = bm | 1;
  }
  memset(dp, -1, sizeof(dp));
  cout << func(bm, 0);
  return (0);
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define DIM 23
#define INF 1000000000000000007LL
#define modulo 1000000007
using namespace std;

long long n, a[DIM][DIM], d[DIM][1 << 21];

bool isbit(int mask, int num) { return (mask >> (num - 1)) & 1; }
void sbit(int &mask, int num) { mask ^= 1 << (num - 1); }
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      cin >> a[i][j];

  d[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int mask = 0; mask < (1 << n); ++mask)
      for (int j = 1; j <= n; ++j)
        if (isbit(mask, j) && a[i][j]) {
          int nmask = mask;
          sbit(nmask, j);
          d[i][mask] += d[i - 1][nmask];
          d[i][mask] %= modulo;
        }

  cout << d[n][(1 << n) - 1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define DIM 23
#define INF 1000000000000000007LL
#define modulo 1000000007
using namespace std;

long long n, a[DIM][DIM], d[DIM][1 << 21];

bool isbit(int mask, int num) { return (mask >> (num - 1)) & 1; }
void sbit(int &mask, int num) { mask ^= 1 << (num - 1); }
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      cin >> a[i][j];

  d[0][0] = 1;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int i = __builtin_popcount(mask);
    for (int j = 1; j <= n; ++j)
      if (isbit(mask, j) && a[i][j]) {

        int nmask = mask;
        sbit(nmask, j);
        d[i][mask] += d[i - 1][nmask];
        d[i][mask] %= modulo;
      }
  }

  cout << d[n][(1 << n) - 1];
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
bool grid[22][22];
int N = 22, n, mod = 1e9 + 7, dp[22][22];
int solve(int m, int w) {
  if (m == n) {
    if (w == 0)
      return 1;
    return 0;
  }
  if (dp[m][w] != -1)
    return dp[m][w];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    bool okay = ((((1 << i) & w) == 0) ? 0 : 1);
    if (okay && grid[m][i])
      ans = (ans + solve(m + 1, (1 << i) ^ w)) % mod;
  }
  return dp[m][w] = ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> grid[i][j];

  memset(dp, -1, sizeof(dp));
  cout << solve(0, (1 << n) - 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 24;
bool grid[24][24];
long long n, mod = 1e9 + 7, dp[24][1 << 22];
int solve(int m, int w) {
  if (m == n) {
    if (w == 0)
      return 1;
    return 0;
  }
  if (dp[m][w] != -1)
    return dp[m][w];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    bool okay = ((((1 << i) & w) == 0) ? 0 : 1);
    if (okay && grid[m][i])
      ans = (ans + solve(m + 1, (1 << i) ^ w)) % mod;
  }
  return dp[m][w] = ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> grid[i][j];

  memset(dp, -1, sizeof(dp));
  cout << solve(0, (1 << n) - 1);
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int maska[25];
int mod = 1e9 + 7;
int dp[25][(1 << 22)];
int main() {
  cin >> n;
  for (int i = 1; n >= i; i++) {
    int val = 1;
    for (int j = 0; n > j; j++) {
      int a;
      cin >> a;
      if (a == 1)
        maska[i] ^= val;
      val *= 2;
    }
  }
  dp[0][(1LL << n) - 1] = 1;
  for (int i = 1; n >= i; i++) {
    for (int mas = 0; (1 << n) > mas; mas++) {
      for (int j = 0; n > j; j++) {
        if (((1 << j) & mas) == 0 and (maska[i] & (1 << j)) > 0)
          dp[i][mas] = (dp[i][mas] + dp[i - 1][mas ^ (1 << j)]) % mod;
      }
    }
  }
  cout << dp[n][0];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int maska[25];
int mod = 1e9 + 7;
int dp[25][(1 << 22)];
int main() {
  cin >> n;
  for (int i = 1; n >= i; i++) {
    int val = 1;
    for (int j = 0; n > j; j++) {
      int a;
      cin >> a;
      if (a == 1)
        maska[i] ^= val;
      val *= 2;
    }
  }
  dp[0][(1LL << n) - 1] = 1;
  for (int i = 1; n >= i; i++) {
    for (int mas = 0; (1 << n) > mas; mas++) {
      if (__builtin_popcount(mas) != n - i)
        continue;
      for (int j = 0; n > j; j++) {
        if (((1 << j) & mas) == 0 and (maska[i] & (1 << j)) > 0)
          dp[i][mas] = (dp[i][mas] + dp[i - 1][mas ^ (1 << j)]) % mod;
      }
    }
  }
  cout << dp[n][0];
  return 0;
}
Problem p03174 (C++)

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

using namespace std;

const int mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int N;
  char ch;
  cin >> N;
  vector<int> adj(N);
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j) {
      cin >> ch;
      if (ch == '1')
        adj[i] |= (1 << j);
    }
  vector<int> dp(1 << N);
  dp[0] = 1;
  for (int mask = 1; mask < (1 << N); ++mask) {
    int a = __builtin_popcount(mask), ans = 0;
    for (int b = mask & adj[a - 1]; b; b &= b - 1)
      if (!(mask & (1 << b))) {
        ans += dp[mask ^ (1 << b)];
        if (ans >= mod)
          ans -= mod;
      }
    dp[mask] = ans;
  }
  cout << dp[(1 << N) - 1];
}


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

using namespace std;

const int mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int N;
  char ch;
  cin >> N;
  vector<int> adj(N);
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j) {
      cin >> ch;
      if (ch == '1')
        adj[i] |= (1 << j);
    }
  vector<int> dp(1 << N);
  dp[0] = 1;
  for (int mask = 1; mask < (1 << N); ++mask) {
    int a = __builtin_popcount(mask), ans = 0;
    for (int b = mask & adj[a - 1]; b; b &= b - 1) {
      ans += dp[mask & ~(b & -b)];
      if (ans >= mod)
        ans -= mod;
    }
    dp[mask] = ans;
  }
  cout << dp[(1 << N) - 1];
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define F first
#define S second
#define MEM(a, b) memset(a, b, sizeof a)
#define Tie ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n, sn, cnt, tmp, a[21][21], dp[1048577];
const int MOD = 1e9 + 7;

int main() {
  Tie cin >> n, sn = 1 << n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  dp[0] = 1;
  for (int i = 1; i < sn; i++) {
    tmp = i, cnt = -1;
    while (tmp)
      cnt++, tmp -= tmp & -tmp;
    for (int j = 1, rj = 0; j < sn; j <<= 1, rj++)
      if ((i & j) && a[cnt][rj])
        dp[i] = (dp[i] + dp[i ^ j]) % MOD;
  }
  cout << dp[sn - 1] << '\n';
}

Fixed submission:
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define F first
#define S second
#define MEM(a, b) memset(a, b, sizeof a)
#define Tie ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n, sn, cnt, tmp, a[21][21], dp[2097153];
const int MOD = 1e9 + 7;

int main() {
  Tie cin >> n, sn = 1 << n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  dp[0] = 1;
  for (int i = 1; i < sn; i++) {
    tmp = i, cnt = -1;
    while (tmp)
      cnt++, tmp -= tmp & -tmp;
    for (int j = 1, rj = 0; j < sn; j <<= 1, rj++)
      if ((i & j) && a[cnt][rj])
        dp[i] = (dp[i] + dp[i ^ j]) % MOD;
  }
  cout << dp[sn - 1] << '\n';
}
Problem p03174 (C++)

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  int N;
  cin >> N;
  vector<vector<int>> info(N, vector<int>(N));
  rep(i, N) {
    rep(j, N) { cin >> info[i][j]; }
  }
  ll S = 1 << N;
  vector<vector<ll>> dp(N + 1, vector<ll>(S));
  dp[0][0] = 1;
  rep(i, N) {
    rep(j, S) {
      rep(w, N) {
        if (info[i][w] == 1 && (j & (1 << w)) == 0) {
          dp[i + 1][j | (1 << w)] += dp[i][j];
          dp[i + 1][j | (1 << w)] %= MOD;
        }
      }
    }
  }
  cout << dp[N][S - 1] << '\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  int N;
  cin >> N;
  vector<vector<int>> info(N, vector<int>(N));
  rep(i, N) {
    rep(j, N) { cin >> info[i][j]; }
  }
  ll S = 1 << N;
  vector<vector<ll>> dp(N + 1, vector<ll>(S));
  dp[0][0] = 1;
  rep(i, N) {
    rep(j, S) {
      if (__builtin_popcount(j) != i)
        continue;
      rep(w, N) {
        if (info[i][w] == 1 && (j & (1 << w)) == 0) {
          dp[i + 1][j | (1 << w)] += dp[i][j];
          dp[i + 1][j | (1 << w)] %= MOD;
        }
      }
    }
  }
  cout << dp[N][S - 1] << '\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \
  for (ll i = (m); i <= n; i++) {                                              \
    cout << (x[i]) << " ";                                                     \
  }                                                                            \
  cout << endl;

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  bool a[n][n];
  rep(i, 0, n) {
    rep(j, 0, n) { cin >> a[i][j]; }
  }

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

  rep(i, 0, n) {
    rep(bit, 0, (1 << n)) {
      rep(j, 0, n) {
        if (bit & (1 << j) && a[i][j]) {
          dp[i + 1][bit - (1 << j)] += dp[i][bit];
          dp[i + 1][bit - (1 << j)] %= MOD;
        }
      }
    }
  }

  print(dp[n][0]) return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \
  for (ll i = (m); i <= n; i++) {                                              \
    cout << (x[i]) << " ";                                                     \
  }                                                                            \
  cout << endl;

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  bool a[n][n];
  rep(i, 0, n) {
    rep(j, 0, n) { cin >> a[i][j]; }
  }

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

  rep(i, 0, n) {
    rep(bit, 0, (1 << n)) {
      if (__builtin_popcount(bit) == n - i) {
        rep(j, 0, n) {
          if (bit & (1 << j) && a[i][j]) {
            dp[i + 1][bit - (1 << j)] += dp[i][bit];
            dp[i + 1][bit - (1 << j)] %= MOD;
          }
        }
      }
    }
  }

  print(dp[n][0]) return 0;
}