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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (1LL << 62)
#define NINF ((-1) * (INF))
#define MOD (1000000007)

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int arr[n + 5][n + 5];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> arr[i][j];
    }
  }
  vector<int> dp(1LL << n, 0);
  dp[0] = 1;
  for (int i = 0; i < (1LL << n) - 1; i++) {
    int set_bits = 0;
    for (int j = 0; j < n; j++) {
      if (((1LL << j) & i))
        set_bits++;
    }
    for (int j = 0; j < n; j++) {
      if (((1LL << j) & i) == 0) {
        dp[i | (1LL << j)] =
            (dp[i | (1LL << j)] + arr[set_bits][j] * dp[i]) % MOD;
      }
    }
  }
  cout << dp[(1LL << n) - 1];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (1LL << 62)
#define NINF ((-1) * (INF))
#define MOD (1000000007)

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int arr[n + 5][n + 5];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> arr[i][j];
    }
  }
  vector<int> dp(1LL << n, 0);
  dp[0] = 1;
  for (int i = 0; i < (1LL << n) - 1; i++) {
    int set_bits = 0;
    for (int j = 0; j < n; j++) {
      if (((1LL << j) & i))
        set_bits++;
    }
    for (int j = 0; j < n; j++) {
      if (((1LL << j) & i) == 0) {
        dp[i | (1LL << j)] =
            (dp[i | (1LL << j)] + arr[set_bits][j] * dp[i]) % MOD;
      }
    }
  }
  cout << dp[(1LL << n) - 1];
}
Problem p03174 (C++)

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

#define pb push_back
#define mp make_pair
#define inside sl <= l &&r <= sr
#define outside r < sl || sr < l
#define INF 1000000009
#define mod 1000000007
using namespace std;
typedef long long ll;

int n;
int mat[22][22];
ll dp[22][(1 << 21)];

int main() {
  // freopen("stl.gir", "r", stdin);

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

  // dp[i][mask] = i'den öncekiler mask ile eşlendi

  for (int i = n - 1; i >= 0; i--) {
    for (int mask = 0; mask < (1 << n); mask++) {
      dp[i][mask] = 0;
      for (int j = 0; j < n; j++) {
        if (mat[i][j] && ((mask & (1 << j)) == 0)) {
          if (i < n - 1)
            dp[i][mask] += dp[i + 1][mask | (1 << j)];
          else
            dp[i][mask] += 1;
          dp[i][mask] %= mod;
        }
      }
    }
  }

  cout << dp[0][0] << endl;
}

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

#define pb push_back
#define mp make_pair
#define inside sl <= l &&r <= sr
#define outside r < sl || sr < l
#define INF 1000000009
#define mod 1000000007
using namespace std;
typedef long long ll;

int n;
int mat[22][22];
ll dp[22][(1 << 21)];

int main() {
  // freopen("stl.gir", "r", stdin);

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

  // dp[i][mask] = i'den öncekiler mask ile eşlendi

  for (int i = n - 1; i >= 0; i--) {
    for (int mask = 0; mask < (1 << n); mask++) {
      if (__builtin_popcount(mask) != i)
        continue;
      dp[i][mask] = 0;
      for (int j = 0; j < n; j++) {
        if (mat[i][j] && ((mask & (1 << j)) == 0)) {
          if (i < n - 1)
            dp[i][mask] += dp[i + 1][mask | (1 << j)];
          else
            dp[i][mask] += 1;
          dp[i][mask] %= mod;
        }
      }
    }
  }

  cout << dp[0][0] << endl;
}
Problem p03174 (C++)

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

#define forn(x, n, s) for (int i = x; i < n; i += s)
#define forr(x, n, s) for (int i = x; i >= n; i -= s)

using namespace std;

void go() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
long long int mod = 1e9 + 7;
int bit;
vector<vector<int>> memo;

int helper(vector<vector<int>> &arr, int index, int mask) {
  if (mask == (1 << bit) - 1)
    return 1;
  if (index == arr.size())
    return 0;
  if (memo[index][mask] != -1)
    return memo[index][mask];
  int include = 0;
  include = helper(arr, index + 1, mask);
  for (int i = 0; i < bit; i++) {
    if ((mask & (1 << i)) == 0 and arr[index][i] == 1) {
      include += helper(arr, index + 1, mask | (1 << i));
      include %= mod;
    }
  }
  memo[index][mask] = include;
  return include;
}

int main() {
  go();

  // code here

  int n;
  cin >> n;
  bit = n;
  vector<vector<int>> arr(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> arr[i][j];
    }
  }
  memo.resize(n, vector<int>(1 << n, -1));
  int sol = helper(arr, 0, 0);
  sol %= mod;
  cout << sol;
}


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

#define forn(x, n, s) for (int i = x; i < n; i += s)
#define forr(x, n, s) for (int i = x; i >= n; i -= s)

using namespace std;

void go() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
long long int mod = 1e9 + 7;
int bit;
vector<vector<int>> memo;

int helper(vector<vector<int>> &arr, int index, int mask) {
  if (mask == (1 << bit) - 1)
    return 1;
  if (index == arr.size())
    return 0;
  if (memo[index][mask] != -1)
    return memo[index][mask];
  int include = 0;
  for (int i = 0; i < bit; i++) {
    if ((mask & (1 << i)) == 0 and arr[index][i] == 1) {
      include += helper(arr, index + 1, mask | (1 << i));
      include %= mod;
    }
  }
  memo[index][mask] = include;
  return include;
}

int main() {
  go();

  // code here

  int n;
  cin >> n;
  bit = n;
  vector<vector<int>> arr(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> arr[i][j];
    }
  }
  memo.resize(n, vector<int>(1 << n, -1));
  int sol = helper(arr, 0, 0);
  sol %= mod;
  cout << sol;
}
Problem p03174 (C++)

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

const int MAX_NB = 10, MOD = 1e9 + 7;

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

  bool comp[nbPersonne][nbPersonne];

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

  int dp[(1 << MAX_NB)] = {0};
  dp[0] = 1;

  for (int mask = 1; mask < (1 << nbPersonne); mask++) {
    for (int bitEnleve = 0; bitEnleve < MAX_NB; bitEnleve++) {
      int rang = __builtin_popcount(mask) - 1;
      if (mask & (1 << bitEnleve) && comp[rang][bitEnleve]) {
        dp[mask] += (dp[mask - (1 << bitEnleve)]);
        dp[mask] %= MOD;
      }
    }
  }

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

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

const int MAX_NB = 22, MOD = 1e9 + 7;

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

  bool comp[nbPersonne][nbPersonne];

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

  int dp[(1 << MAX_NB)] = {0};
  dp[0] = 1;

  for (int mask = 1; mask < (1 << nbPersonne); mask++) {
    for (int bitEnleve = 0; bitEnleve < MAX_NB; bitEnleve++) {
      int rang = __builtin_popcount(mask) - 1;
      if (mask & (1 << bitEnleve) && comp[rang][bitEnleve]) {
        dp[mask] += (dp[mask - (1 << bitEnleve)]);
        dp[mask] %= MOD;
      }
    }
  }

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

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

using namespace std;

int arr[22][22];
const int N = 22;
int dp[1ll << N][22];

int func(int mask, int col, int n) {
  if (col == n) {
    if (mask == (1 << n) - 1) {
      return 1;
    }
    return 0;
  }

  long long int ans = 0;
  // i have to assign col woman to a man who has not been assigned any queen
  for (int i = 0; i < n; i++) // got man i
  {
    if (arr[i][col] == 1 && (mask & (1 << i)) == 0) {
      ans = (ans % MOD + func(mask | (1 << i), col + 1, n) % MOD) % MOD;
    }
  }

  dp[mask][col] = ans;
  return dp[mask][col];
}

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

  int mask = 0;
  int col = 0;
  memset(dp, -1, sizeof(dp));
  cout << func(mask, col, n);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
typedef long long int lli;

using namespace std;

int arr[22][22];
const int N = 22;
int dp[1ll << N][22];

int func(int mask, int col, int n) {
  if (col == n) {
    if (mask == (1 << n) - 1) {
      return 1;
    }
    return 0;
  }

  if (dp[mask][col] != -1) {
    return dp[mask][col];
  }

  long long int ans = 0;
  // i have to assign col woman to a man who has not been assigned any queen
  for (int i = 0; i < n; i++) // got man i
  {
    if (arr[i][col] == 1 && (mask & (1 << i)) == 0) {
      ans = (ans % MOD + func(mask | (1 << i), col + 1, n) % MOD) % MOD;
    }
  }

  dp[mask][col] = ans;
  return dp[mask][col];
}

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

  int mask = 0;
  int col = 0;
  memset(dp, -1, sizeof(dp));
  cout << func(mask, col, n);

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF 100000000
#define fi first
#define se second
const int LEN = 2e5 + 5;
const int LEN5 = 1e5 + 5;
const int MOD = 1e9 + 7;
typedef long long ll;
int N, ar[22][22];
ll dp[22][1 << 22];
int lim;

ll sol(int n, int m) {
  // cout<<n<<" "<<m<<endl;

  if (n == N)
    return m == lim;
  if (dp[n][m] != -1)
    return dp[n][m];

  dp[n][m] = 0;
  for (int i = 0; i < N; i++) {
    // cout<<ar[n][i]<<endl;
    if (ar[n][i] && !((1 << i) & m)) {
      // cout<<n<<" "<<i<<endl;
      dp[n][m] += sol(n + 1, m | (1 << i));
    }
  }
  dp[n][m] %= MOD;
  return dp[n][m];
}

int main() {

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

  // ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(0);
  cin >> N;
  lim = (1 << N) - 1;
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < N; k++)
      cin >> ar[i][k];
  }
  for (int i = 0; i < N; i++) {
    for (int k = 0; k <= lim; i++) {
      dp[i][k] = -1;
    }
  }
  cout << sol(0, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 100000000
#define fi first
#define se second
const int LEN = 2e5 + 5;
const int LEN5 = 1e5 + 5;
const int MOD = 1e9 + 7;
typedef long long ll;
int N, ar[22][22];
ll dp[22][1 << 22];
int lim;

ll sol(int n, int m) {
  // cout<<n<<" "<<m<<endl;

  if (n == N)
    return m == lim;
  if (dp[n][m] != -1)
    return dp[n][m];

  dp[n][m] = 0;
  for (int i = 0; i < N; i++) {
    // cout<<ar[n][i]<<endl;
    if (ar[n][i] && !((1 << i) & m)) {
      // cout<<n<<" "<<i<<endl;
      dp[n][m] += sol(n + 1, m | (1 << i));
    }
  }
  dp[n][m] %= MOD;
  return dp[n][m];
}

int main() {

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

  // ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(0);
  cin >> N;
  lim = (1 << N) - 1;
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < N; k++)
      cin >> ar[i][k];
  }
  for (int i = 0; i < 22; i++) {
    for (int k = 0; k < (1 << 21); k++) {
      dp[i][k] = -1;
    }
  }
  cout << sol(0, 0) << endl;
}
Problem p03174 (C++)

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

// repetition
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline ll toLL(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

int a[30][30];
ll dp[25][3000000];
const ll MOD = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  rep(i, n) rep(j, n) cin >> a[i][j];
  dp[0][0] = 1;
  rep(i, n) { // 男を
    rep(j, 1 << (n)) {
      rep(k, n) {
        if (a[i][k] && ((j & (1 << k)) == 0)) {
          (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;

// repetition
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline ll toLL(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

int a[30][30];
ll dp[25][3000000];
const ll MOD = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  rep(i, n) rep(j, n) cin >> a[i][j];
  dp[0][0] = 1;

  rep(j, 1 << (n)) {
    int i = 0;
    rep(x, n) { // bitが立っている数
      if (j & (1 << x))
        i++;
    }
    rep(k, n) {
      if (a[i][k] && ((j & (1 << k)) == 0)) {
        (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: Time Limit Exceeded):
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
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 printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll dp[23][1LL << 22] = {};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  ll a[25][25];
  rep(i, 0, N) {
    rep(j, 0, N) { cin >> a[i][j]; }
  }

  dp[0][0] = 1;
  rep(i, 0, N) {
    rep(bit, 0, (1LL << N)) {
      rep(j, 0, N) {
        if ((bit >> j) & 1)
          continue;
        if (a[i][j] == 1) {
          dp[i + 1][bit | (1LL << j)] += dp[i][bit];
          dp[i + 1][bit | (1LL << j)] %= MOD;
        }
      }
    }
  }
  print(dp[N][(1LL << N) - 1]);
}

Fixed submission:
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
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 printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll dp[23][1LL << 22] = {};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  ll a[25][25];
  rep(i, 0, N) {
    rep(j, 0, N) { cin >> a[i][j]; }
  }

  dp[0][0] = 1;
  rep(i, 0, N) {
    rep(bit, 0, (1LL << N)) {
      ll cnt = __builtin_popcount(bit);
      if (cnt != i)
        continue;
      rep(j, 0, N) {
        if ((bit >> j) & 1)
          continue;
        if (a[i][j] == 1) {
          dp[i + 1][bit | (1LL << j)] += dp[i][bit];
          dp[i + 1][bit | (1LL << j)] %= MOD;
        }
      }
    }
  }
  print(dp[N][(1LL << N) - 1]);
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
ll dp[22][1 << 21] = {};
vint bitcountset[21];
int main() {
  ll MOD = 1e+9 + 7;
  int N;
  scan(N);
  ll kumi = (1 << N);
  int a[22][22];
  dp[0][0] = 1;
  rep(bit, kumi) {
    int now = bit;
    int bitcount = 0;
    rep(i, N) {
      bitcount += (now & 1);
      now >>= 1;
    }
    bitcountset[bitcount].push_back(bit);
  }
  rep(i, N) {
    rep(j, N) {
      scan(a[i][j]);
      if (a[i][j] == 1) {
        rep(k, bitcountset[i].size()) {
          if (((bitcountset[i][k] >> j) & 1) == 0) {
            dp[i + 1][bitcountset[i][k] | (1 << j)] += dp[i][bitcountset[i][k]];
            dp[i + 1][bitcountset[i][k] | (1 << j)] %= MOD;
          }
        }
      }
    }
  }
  prin(dp[N][kumi - 1]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
ll dp[22][1 << 21] =
    {}; // dp[i][j]:男性i人目まで読んだときに婚約済の男性の集合がjであるときの場合の数
vint bitcountset[22]; // bitcountset[j]:2進数表記でN桁で1の数がj個である数の集合
int main() {
  ll MOD = 1e+9 + 7;
  int N;
  scan(N);
  ll kumi = (1 << N);
  int a[22][22];
  dp[0][0] = 1;
  rep(bit, kumi) {
    int now = bit;
    int bitcount = 0;
    rep(i, N) {
      bitcount += (now & 1);
      now >>= 1;
    }
    bitcountset[bitcount].push_back(bit);
  }
  rep(i, N) {
    rep(j, N) {
      scan(a[i][j]);
      if (a[i][j] == 1) {
        rep(k, bitcountset[i].size()) {
          if (((bitcountset[i][k] >> j) & 1) == 0) {
            dp[i + 1][bitcountset[i][k] | (1 << j)] += dp[i][bitcountset[i][k]];
            dp[i + 1][bitcountset[i][k] | (1 << j)] %= MOD;
          }
        }
      }
    }
  }
  prin(dp[N][kumi - 1]);
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;
ll dp[21][1 << 21];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int a[N][N];
  rep(i, 0, N) {
    rep(j, 0, N) { cin >> a[i][j]; }
  }
  int comb = (1 << (N - 1)) - 1;
  while (comb < 1 << N) {
    bool t = false;
    rep(j, 0, N) {
      if (!(comb & (1 << j)) && a[0][j]) {
        t = true;
        break;
      }
    }
    if (t)
      dp[0][comb] = 1;
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }
  rep(i, 1, N) {
    int comb = (1 << (N - i - 1)) - 1;
    while (comb < 1 << N) {
      rep(k, 0, N) {
        if (a[i][k] == 1 && !(comb & (1 << k))) {
          dp[i][comb] += dp[i - 1][comb | (1 << k)];
          dp[i][comb] %= MOD;
        }
      }
      if (comb == 0)
        break;
      int x = comb & -comb, y = comb + x;
      comb = ((comb & ~y) / x >> 1) | y;
    }
  }
  cout << dp[N - 1][0] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;
ll dp[21][1 << 21];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int a[N][N];
  rep(i, 0, N) {
    rep(j, 0, N) { cin >> a[i][j]; }
  }
  int comb = (1 << (N - 1)) - 1;
  while (comb < 1 << N) {
    bool t = false;
    rep(j, 0, N) {
      if (!(comb & (1 << j)) && a[0][j]) {
        t = true;
        break;
      }
    }
    if (t)
      dp[0][comb] = 1;
    if (comb == 0)
      break;
    int x = comb & -comb, y = comb + x;
    comb = ((comb & ~y) / x >> 1) | y;
  }
  rep(i, 1, N) {
    int comb = (1 << (N - i - 1)) - 1;
    while (comb < 1 << N) {
      rep(k, 0, N) {
        if (a[i][k] == 1 && !(comb & (1 << k))) {
          dp[i][comb] += dp[i - 1][comb | (1 << k)];
          dp[i][comb] %= MOD;
        }
      }
      if (comb == 0)
        break;
      int x = comb & -comb, y = comb + x;
      comb = ((comb & ~y) / x >> 1) | y;
    }
  }
  cout << dp[N - 1][0] << "\n";
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long int ll;
constexpr int kMod = int(1E9 + 7);
int a[30][30];
ll dp[2][1 << 21];
int main() {
  int n;
  bool f = false;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      scanf("%d", &a[i][j]);
  dp[!f][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int k = 0; k < (1 << n); k++)
      for (int j = 0; j < n; j++)
        if (((1 << j) & k) && a[i][j + 1])
          dp[f][k] += dp[!f][k ^ (1 << j)];
    for (int j = 0; j < (1 << n); j++)
      dp[f][j] %= kMod;
    f = !f;
    memset(dp[f], 0, sizeof(dp[f]));
  }
  printf("%lld\n", dp[!f][(1 << n) - 1]);
}


Fixed submission:
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long int ll;
constexpr int kMod = int(1E9 + 7);
int a[30][30];
ll dp[2][1 << 21];
int main() {
  int n;
  bool f = false;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      scanf("%d", &a[i][j]);
  dp[!f][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++)
      if (a[i][j + 1]) {
        for (int k = 0; k < (1 << n); k++)
          if ((1 << j) & k)
            dp[f][k] += dp[!f][k ^ (1 << j)];
      }
    for (int j = 0; j < (1 << n); j++)
      dp[f][j] %= kMod;
    f = !f;
    memset(dp[f], 0, sizeof(dp[f]));
  }
  printf("%lld\n", dp[!f][(1 << n) - 1]);
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

int n;
int arr[21][21];
int dp[21][1 << 21];

int func(int idx, int mask) {
  if (idx == n)
    return (mask == (int)pow(2, n) - 1);
  int &ans = dp[idx][mask];
  if (ans != -1)
    return ans;
  ans = 0;
  for (int j = 0; j < n; j++) {
    if (arr[idx][j]) {
      if ((mask & (1 << (n - j - 1))) == 1)
        continue;
      else
        ans += func(idx + 1, mask | (1 << (n - j - 1))) % mod;
    }
  }
  return ans;
}

int main() {
  fast;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> arr[i][j];
  memset(dp, -1, sizeof(dp));
  cout << func(0, 0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

int n;
int arr[21][21];
int dp[21][1 << 21];

int func(int idx, int mask) {
  if (idx == n)
    return (mask == (int)pow(2, n) - 1);
  int &ans = dp[idx][mask];
  if (ans != -1)
    return ans;
  ans = 0;
  for (int j = 0; j < n; j++) {
    if (arr[idx][j] && !(mask & (1 << j)))
      ans = (ans + func(idx + 1, mask | (1 << j))) % mod;
  }
  return ans;
}

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

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

using namespace std;

int n;
int a[21][21];
int dp[21][1 << 21];

int MOD = 1000000007;

int main(int argc, char **argv) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  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 bit = 0; bit < 1 << n; bit++) {
      if (__builtin_popcount(bit) != i)
        continue;
      for (int j = 0; j < n; j++) {
        if ((bit >> j & 1) == 0 && a[i][j] == 1) {
          dp[i + 1][bit | 1 << j] += dp[i][bit];
          dp[i + 1][bit | 1 << j] %= MOD;
        }
      }
    }
  }

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


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

using namespace std;

int n;
int a[22][22];
int dp[22][1 << 21];

int MOD = 1000000007;

int main(int argc, char **argv) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  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 bit = 0; bit < 1 << n; bit++) {
      if (__builtin_popcount(bit) != i)
        continue;
      for (int j = 0; j < n; j++) {
        if ((bit >> j & 1) == 0 && a[i][j] == 1) {
          dp[i + 1][bit | 1 << j] += dp[i][bit];
          dp[i + 1][bit | 1 << j] %= MOD;
        }
      }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
// failed to generate code

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

int a[22][22];

long long dp[22][1 << 22];
int MOD = 1000000007;
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
    }
  }
  long long ans = 0;
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (1 << N); j++) {
      dp[i][j] %= MOD;
      for (int k = 0; k < N; k++) {
        if ((j >> k & 1) == 0 && a[i][k]) {
          dp[i + 1][j | (1 << k)] += dp[i][j];
        }
      }
    }
  }
  cout << dp[N][(1 << N) - 1] % MOD << endl;
}


Fixed submission:
// failed to generate code

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

int a[22][22];

long long dp[22][1 << 22];
int MOD = 1000000007;
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
    }
  }
  long long ans = 0;
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (1 << N); j++) {
      if (dp[i][j] == 0)
        continue;
      dp[i][j] %= MOD;
      for (int k = 0; k < N; k++) {
        if ((j >> k & 1) == 0 && a[i][k]) {
          dp[i + 1][j | (1 << k)] += dp[i][j];
        }
      }
    }
  }
  cout << dp[N][(1 << N) - 1] % MOD << endl;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
using namespace std;
const int MAXN = 21;
const LL MOD = 1000000007;

int N;
bool adj[MAXN][MAXN];
LL dp[1 << MAXN];
vector<int> store[MAXN];

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

  cin >> N;
  for (int i = 0; i < (1 << N); i++) {
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      cnt += i >> j & 1;
    }
    store[cnt].push_back(i);
  }

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

  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    for (int m : store[i + 1]) {
      for (int j = 0; j < N; j++) {
        if ((m >> j & 1) && adj[i][j]) {
          dp[m] += dp[m ^ (1 << j)];
        }
      }
      dp[m] %= MOD;
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
using namespace std;
const int MAXN = 21;
const LL MOD = 1000000007;

int N;
bool adj[MAXN][MAXN];
LL dp[1 << MAXN];
vector<int> store[MAXN + 1];

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

  cin >> N;
  for (int i = 0; i < (1 << N); i++) {
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      cnt += i >> j & 1;
    }
    store[cnt].push_back(i);
  }

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

  dp[0] = 1;
  for (int i = 0; i < N; i++) {
    for (int m : store[i + 1]) {
      for (int j = 0; j < N; j++) {
        if ((m >> j & 1) && adj[i][j]) {
          dp[m] += dp[m ^ (1 << j)];
        }
      }
      dp[m] %= MOD;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
long long dp[21][1 << 21];

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;
  int mod = 1000000007;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (1 << N); j++) {
      if (dp[i][j] != 0) {
        // cout << i <<", "<< j <<": " << dp[i][j] << endl;
        for (int k = 0; k < N; k++) {
          if (a[i][k] == 0)
            continue;
          int j2 = j | (1 << k);
          if (j != j2) {
            dp[i + 1][j2] += dp[i][j];
            dp[i + 1][j2] %= mod;
          }
        }
      }
    }
  }
  cout << dp[N][(1 << N) - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
long long dp[22][1 << 21];

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;
  int mod = 1000000007;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (1 << N); j++) {
      if (dp[i][j] != 0) {
        // cout << i <<", "<< j <<": " << dp[i][j] << endl;
        for (int k = 0; k < N; k++) {
          if (a[i][k] == 0)
            continue;
          int j2 = j | (1 << k);
          if (j != j2) {
            dp[i + 1][j2] += dp[i][j];
            dp[i + 1][j2] %= mod;
          }
        }
      }
    }
  }
  cout << dp[N][(1 << N) - 1] << endl;
  return 0;
}
Problem p03174 (C++)

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

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cout << #a << " = " << a << endl;

const double PI = acos(-1.0);
const LL INF = 1e9 + 7;
const LL LINF = INF * INF;

const int mod = 1e9 + 7;

inline void add(int &x, int &y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

const int MAX = 21;
int n;
int a[MAX][MAX];
int dp[MAX][1 << MAX];
VI bity[MAX];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("In.txt", "r", stdin);
  // freopen("In.txt", "w", stdout);

  cin >> n;
  FOR(i, 0, n)
  FOR(j, 0, n)
  cin >> a[i][j];

  FOR(i, 0, n)
  if (a[0][i])
    dp[0][1 << i] = 1;

  FOR(i, 0, 1 << n)
  bity[__builtin_popcount(i)].PB(i);

  FOR(i, 1, n) {
    for (auto j : bity[i]) {
      FOR(k, 0, n) {
        if (!a[i][k] || (j & (1 << k)))
          continue;

        add(dp[i][j | (1 << k)], dp[i - 1][j]);
      }
    }
  }

  cout << dp[n - 1][(1 << n) - 1] << endl;
  cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
  return 0;
}


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

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cout << #a << " = " << a << endl;

const double PI = acos(-1.0);
const LL INF = 1e9 + 7;
const LL LINF = INF * INF;

const int mod = 1e9 + 7;

inline void add(int &x, int &y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

const int MAX = 22;
int n;
int a[MAX][MAX];
int dp[MAX][1 << MAX];
VI bity[MAX];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("In.txt", "r", stdin);
  // freopen("In.txt", "w", stdout);

  cin >> n;
  FOR(i, 0, n)
  FOR(j, 0, n)
  cin >> a[i][j];

  FOR(i, 0, n)
  if (a[0][i])
    dp[0][1 << i] = 1;

  FOR(i, 0, 1 << n)
  bity[__builtin_popcount(i)].PB(i);

  FOR(i, 1, n) {
    for (auto j : bity[i]) {
      FOR(k, 0, n) {
        if (!a[i][k] || (j & (1 << k)))
          continue;

        add(dp[i][j | (1 << k)], dp[i - 1][j]);
      }
    }
  }

  cout << dp[n - 1][(1 << n) - 1] << endl;
  cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("popcnt,abm,bmi,avx2")
#include <cstdio>

int n;
int A[21];

constexpr int mod = 1000000007;

int dp[1 << 21];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int x;
      scanf("%d", &x);
      if (x)
        A[i] |= 1 << j;
    }
  }

  dp[0] = 1;
  for (int x = 1; x < (1 << n); x++) {
    int i = __builtin_popcount(x);
    long long int r = 0;
    for (int y = x & A[i - 1]; y; y &= y - 1) {
      int j = y & -y;
      r += dp[x & ~j];
    }
    dp[x] = r % mod;
  }
  printf("%d\n", dp[(1 << n) - 1]);
  return 0;
}


Fixed submission:
#pragma GCC target("arch=ivybridge")
#include <cstdio>

int n;
int A[21];

constexpr int mod = 1000000007;

int dp[1 << 21];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int x;
      scanf("%d", &x);
      if (x)
        A[i] |= 1 << j;
    }
  }

  dp[0] = 1;
  for (int x = 1; x < (1 << n); x++) {
    int i = __builtin_popcount(x);
    long long int r = 0;
    for (int y = x & A[i - 1]; y; y &= y - 1) {
      int j = y & -y;
      r += dp[x & ~j];
    }
    dp[x] = r % mod;
  }
  printf("%d\n", dp[(1 << n) - 1]);
  return 0;
}
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>;
#define ve vector
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)

const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  ve<ve<int>> a(n, ve<int>(n));
  rep(i, n) rep(j, n) cin >> a[i][j];
  ve<ll> dp(1 << n);
  rep(i, n) if (a[0][i] == 1) dp[1 << i] = 1;
  for (int i = 1; i < n; i++) {
    ve<ll> p(1 << n);
    swap(p, dp);
    rep(j, n) {
      if (a[i][j] == 0)
        continue;
      else {
        int mask = 1 << j;
        rep(k, 1 << n) { (dp[k | mask] += p[k]) %= mod; }
      }
    }
  }
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define ve vector
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)

const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  ve<ve<int>> a(n, ve<int>(n));
  rep(i, n) rep(j, n) cin >> a[i][j];
  ve<ll> dp(1 << n);
  rep(i, n) if (a[0][i] == 1) dp[1 << i] = 1;
  for (int i = 1; i < n; i++) {
    ve<ll> p(1 << n);
    swap(p, dp);
    rep(j, n) {
      if (a[i][j] == 0)
        continue;
      else {
        int mask = 1 << j;
        rep(k, 1 << n) {
          if (mask & k)
            continue;
          (dp[k | mask] += p[k]) %= mod;
        }
      }
    }
  }
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (decltype(n) i = 0; i < (n); ++i)
using namespace std;
using int64 = long long;

const long long MOD = 1000000007;

void solve(long long N, std::vector<std::vector<int>> a) {
  vector<unordered_map<int, int64>> dp(N);
  for (auto i : a[0]) {
    dp[0][1 << i] = 1;
  }
  for (int i = 1; i < N; ++i) {
    for (auto &&stcnt : dp[i - 1]) {
      int state = stcnt.first;
      int64 cnt = stcnt.second;
      for (auto j : a[i]) {
        int sj = 1 << j;
        if (state & sj)
          continue;
        dp[i][state | sj] += cnt;
        dp[i][state | sj] %= MOD;
        // cerr << "dp[" << i << "][" << (state | sj)
        //      << "] = " << dp[i][state | sj] << endl;
      }
    }
  }
  int64 ans = 0;
  for (auto &&stcnt : dp[N - 1]) {
    ans = (ans + stcnt.second) % MOD;
  }
  cout << ans << endl;
}

int main() {
  int64 N;
  cin >> N;
  std::vector<std::vector<int>> a(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int x;
      cin >> x;
      if (x) {
        a[i].push_back(j);
      }
    }
  }
  sort(a.begin(), a.end(),
       [](auto &v1, auto &v2) -> bool { return v1.size() > v2.size(); });
  solve(N, std::move(a));
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (decltype(n) i = 0; i < (n); ++i)
using namespace std;
using int64 = long long;

const long long MOD = 1000000007;

void solve(long long N, std::vector<std::vector<int>> a) {
  vector<unordered_map<int, int64>> dp(N);
  for (auto i : a[0]) {
    dp[0][1 << i] = 1;
  }
  for (int i = 1; i < N; ++i) {
    for (auto &&stcnt : dp[i - 1]) {
      int state = stcnt.first;
      int64 cnt = stcnt.second;
      for (auto j : a[i]) {
        int sj = 1 << j;
        if (state & sj)
          continue;
        dp[i][state | sj] += cnt;
        dp[i][state | sj] %= MOD;
        // cerr << "dp[" << i << "][" << (state | sj)
        //      << "] = " << dp[i][state | sj] << endl;
      }
    }
  }
  int64 ans = 0;
  for (auto &&stcnt : dp[N - 1]) {
    ans = (ans + stcnt.second) % MOD;
  }
  cout << ans << endl;
}

int main() {
  int64 N;
  cin >> N;
  std::vector<std::vector<int>> a(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int x;
      cin >> x;
      if (x) {
        a[i].push_back(j);
      }
    }
  }
  sort(a.begin(), a.end(),
       [](auto &v1, auto &v2) -> bool { return v1.size() < v2.size(); });
  solve(N, std::move(a));
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
typedef long long ll;
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define f first
#define s second
#define pb push_back
#define pop pop_back
#define int long long int
#define mod 1000000007
#define inf 1LL << 60

int dp[22][1 << 22];

int solve(vector<vector<int>> &compat, int i, int womansubset, int N) {
  if (i == N + 1) {
    if (womansubset == 0)
      return 1;
    return 0;
  }

  int ans = 0;
  for (int woman = 0; woman < N; woman++) {
    bool available = (((1 << woman) & (womansubset)) == 0) ? 0 : 1;

    if (available && compat[i][woman + 1]) {
      ans = (ans + solve(compat, i + 1, womansubset ^ (1 << woman), N)) % mod;
    }
  }

  return dp[i][womansubset] = ans;
}

int32_t main() {
  fast;
  int n;
  cin >> n;
  vector<vector<int>> compat(n + 1, vector<int>(n + 1));

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      cin >> compat[i][j];

  memset(dp, -1, sizeof dp);

  cout << solve(compat, 1, ((1 << (n)) - 1), n) << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
typedef long long ll;
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define f first
#define s second
#define pb push_back
#define pop pop_back
#define int long long int
#define mod 1000000007
#define inf 1LL << 60

int dp[22][1 << 22];

int solve(vector<vector<int>> &compat, int i, int womansubset, int N) {
  if (i == N + 1) {
    if (womansubset == 0)
      return 1;
    return 0;
  }

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

  int ans = 0;
  for (int woman = 0; woman < N; woman++) {
    bool available = (((1 << woman) & (womansubset)) == 0) ? 0 : 1;

    if (available && compat[i][woman + 1]) {
      ans = (ans + solve(compat, i + 1, womansubset ^ (1 << woman), N)) % mod;
    }
  }

  return dp[i][womansubset] = ans;
}

int32_t main() {
  fast;
  int n;
  cin >> n;
  vector<vector<int>> compat(n + 1, vector<int>(n + 1));

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      cin >> compat[i][j];

  memset(dp, -1, sizeof dp);

  cout << solve(compat, 1, ((1 << (n)) - 1), n) << "\n";

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
int n;
vector<vector<int>> a;
map<int, ll> dp;
map<int, bool> done;
ll dfs(int rest, int line) {
  if (done[rest]) {
    // cout << "memo" << endl;
    return dp[rest];
  }
  if (line == n - 1) {
    for (int j = 0; j < n; j++)
      if (a[line][j] == 1 && rest & 1 << j)
        return 1;
    return 0;
  }
  done[rest] = true;
  ll res = 0;
  for (int i = 0; i < n; i++) {
    if (rest & 1 << i && a[line][i] == 1) {
      rest ^= 1 << i;
      res += dfs(rest, line + 1);
      res %= MOD;
      rest ^= 1 << i;
    }
  }
  // cout << res << endl;
  return dp[rest] = res;
}

int main() {
  cin >> n;
  a.assign(n, vector<int>(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  int rest = (1 << n) - 1;
  ll ans = dfs(rest, 0);
  cout << ans << endl;
  // for (auto d: done) cout << d.second << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
int n;
vector<vector<int>> a;
ll dp[1 << 21];
bool done[1 << 21];
ll dfs(int rest, int line) {
  if (done[rest]) {
    // cout << "memo" << endl;
    return dp[rest];
  }
  if (line == n - 1) {
    for (int j = 0; j < n; j++)
      if (a[line][j] == 1 && rest & 1 << j)
        return 1;
    return 0;
  }
  done[rest] = true;
  ll res = 0;
  for (int i = 0; i < n; i++) {
    if (rest & 1 << i && a[line][i] == 1) {
      rest ^= 1 << i;
      res += dfs(rest, line + 1);
      res %= MOD;
      rest ^= 1 << i;
    }
  }
  // cout << res << endl;
  return dp[rest] = res;
}

int main() {
  cin >> n;
  a.assign(n, vector<int>(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  int rest = (1 << n) - 1;
  ll ans = dfs(rest, 0);
  cout << ans << endl;
  // for (auto d: done) cout << d.second << endl;
  return 0;
}
Problem p03174 (C++)

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

int mm, n;
vector<int> women[23];
int dp[2097154][22];

ll rec(int mask, int wom) {
  if (mask == mm) {
    return 1;
  }
  if (wom == 22) {
    return 0;
  }
  if (dp[mask][wom] != -1) {
    return dp[mask][wom];
  }

  ll ans = 0;
  ans += rec(mask, wom + 1);
  for (int man : women[wom]) {
    if ((mask & (1 << man)) == 0) {
      ans += rec((mask | (1 << man)), wom + 1);
    }
  }
  ans %= mod;
  dp[mask][wom] = ans;
  return ans;
}

int main() {
  for (int r = 0; r < 2097154; r++) {
    for (int c = 0; c < 22; c++) {
      dp[r][c] = -1;
    }
  }
  cin >> n;
  mm = (1 << n) - 1;
  for (int m = 0; m < n; m++) {
    for (int w = 1; w <= n; w++) {
      int x;
      cin >> x;
      if (x == 1) {
        women[w].push_back(m);
      }
    }
  }
  ll ans = rec(0, 1);
  cout << ans;
  return 0;
}

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

int mm, n;
vector<int> women[23];
int dp[2097154][22];

ll rec(int mask, int wom) {
  if (mask == mm) {
    return 1;
  }
  if (wom == 22) {
    return 0;
  }
  if (dp[mask][wom] != -1) {
    return dp[mask][wom];
  }

  ll ans = 0;
  for (int man : women[wom]) {
    if ((mask & (1 << man)) == 0) {
      ans += rec((mask | (1 << man)), wom + 1);
    }
  }
  ans %= mod;
  dp[mask][wom] = ans;
  return ans;
}

int main() {
  for (int r = 0; r < 2097154; r++) {
    for (int c = 0; c < 22; c++) {
      dp[r][c] = -1;
    }
  }
  cin >> n;
  mm = (1 << n) - 1;
  for (int m = 0; m < n; m++) {
    for (int w = 1; w <= n; w++) {
      int x;
      cin >> x;
      if (x == 1) {
        women[w].push_back(m);
      }
    }
  }
  ll ans = rec(0, 1);
  cout << ans;
  return 0;
}