problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define pb push_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define mod 1000000007 using namespace std; typedef long long ll; typedef vector<int> vi; // typedef vector<l>vl ; typedef vector<ll> vll; typedef vector<vector<ll>> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef vector<string> vstr; void READ(vll &v, ll n) { ll a; for (ll i = 0; i < n; i = i + 1) { cin >> a; v.pb(a); } } void PRINT(vll &v, ll n) { ll a; for (ll i = 0; i < n; i = i + 1) { cout << v[i] << " "; } cout << "\n"; } ll n, m, k; ll dp[23][2097160]; ll v[23][23]; ll solve(ll i, ll j) { if (i >= n && j == k) { return (1); } if (i == n) { return (0); } if (dp[i][j] != -1) { return (dp[i][j]); } ll ans = 0; for (ll m = 0; m < n; m = m + 1) { if (v[i][m] == 1 && (j ^ (1 << m))) { ans = ans + solve(i + 1, j ^ (1 << m)); ans %= mod; } } dp[i][j] = ans; return (ans); } int main() { ios_base::sync_with_stdio( false); // this and next two lines for cin cout TLE handling cin.tie(NULL); cout.tie(NULL); std::cout.unsetf( std::ios::floatfield); // this and next line for floating precision upto 6 std::cout.precision(6); ll a, b, c, d, e, x, i, j, t, y, w; double p, q, r, u, s; string str = "", str1 = ""; /*char ch,chr ; ll a1=0,a2=0,b1=0,b2=0 ;*/ cin >> n; k = (1 << n) - 1; for (i = 0; i < n + 2; i = i + 1) { for (j = 0; j < k + 2; j = j + 1) { dp[i][j] = -1; } } for (i = 0; i < n; i = i + 1) { for (j = 0; j < n; j = j + 1) { cin >> v[i][j]; } } a = solve(0, 0); cout << a; }
#include <bits/stdc++.h> #define pb push_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define mod 1000000007 using namespace std; typedef long long ll; typedef vector<int> vi; // typedef vector<l>vl ; typedef vector<ll> vll; typedef vector<vector<ll>> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef vector<string> vstr; void READ(vll &v, ll n) { ll a; for (ll i = 0; i < n; i = i + 1) { cin >> a; v.pb(a); } } void PRINT(vll &v, ll n) { ll a; for (ll i = 0; i < n; i = i + 1) { cout << v[i] << " "; } cout << "\n"; } ll n, m, k; ll dp[23][2097160]; ll v[23][23]; ll solve(ll i, ll j) { if (i >= n && j == k) { return (1); } if (i == n) { return (0); } if (dp[i][j] != -1) { return (dp[i][j]); } ll ans = 0; for (ll m = 0; m < n; m = m + 1) { if (v[i][m] == 1 && !(j & (1 << m))) { ans = ans + solve(i + 1, j ^ (1 << m)); ans %= mod; } } dp[i][j] = ans; return (ans); } int main() { ios_base::sync_with_stdio( false); // this and next two lines for cin cout TLE handling cin.tie(NULL); cout.tie(NULL); std::cout.unsetf( std::ios::floatfield); // this and next line for floating precision upto 6 std::cout.precision(6); ll a, b, c, d, e, x, i, j, t, y, w; double p, q, r, u, s; string str = "", str1 = ""; /*char ch,chr ; ll a1=0,a2=0,b1=0,b2=0 ;*/ cin >> n; k = (1 << n) - 1; for (i = 0; i < n + 2; i = i + 1) { for (j = 0; j < k + 2; j = j + 1) { dp[i][j] = -1; } } for (i = 0; i < n; i = i + 1) { for (j = 0; j < n; j = j + 1) { cin >> v[i][j]; } } a = solve(0, 0); cout << a; }
replace
44
45
44
45
TLE
p03174
C++
Time Limit Exceeded
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(vec) vec.begin(), vec.end() typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const ll mod = 1e9 + 7; const int inf = 1 << 30; ll dp[22][1ll << 21]; int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n)); rep(i, n) rep(j, n) cin >> a[i][j]; dp[0][0] = 1; rep(i, n) { rep(s, 1ll << n) { // if(__builtin_popcount(s)!=i) continue; rep(j, n) { if (a[i][j] && (((1ll << j) & s) == 0)) { dp[i + 1][s | (1ll << j)] += dp[i][s]; dp[i + 1][s | (1ll << j)] %= mod; } } } } ll ans = 0; rep(s, 1ll << n) { ans += dp[n][s]; ans %= mod; } cout << ans << endl; /* rep(i,n+1){ rep(j,1ll<<n){ cout << dp[i][j]; }cout << endl; }*/ }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(vec) vec.begin(), vec.end() typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const ll mod = 1e9 + 7; const int inf = 1 << 30; ll dp[22][1ll << 21]; int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n)); rep(i, n) rep(j, n) cin >> a[i][j]; dp[0][0] = 1; rep(i, n) { rep(s, 1ll << n) if (__builtin_popcount(s) == i) { rep(j, n) { if (a[i][j] && (((1ll << j) & s) == 0)) { dp[i + 1][s | (1ll << j)] += dp[i][s]; dp[i + 1][s | (1ll << j)] %= mod; } } } } ll ans = 0; rep(s, 1ll << n) { ans += dp[n][s]; ans %= mod; } cout << ans << endl; /* rep(i,n+1){ rep(j,1ll<<n){ cout << dp[i][j]; }cout << endl; }*/ }
replace
34
36
34
35
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define forn(i, n) for (int i = 0; i < n; ++i) ll MOD = 1e9 + 7; ll INF = 1e17 + 9; ll dp(int mask, int women, vector<vector<bool>> comp, int n, vector<vector<ll>> save) { if (mask == (1 << n) - 1) return 1; else if (women >= n) return 0; else if (save[mask][women] != -1) return save[mask][women]; ll res = 0; for (int bit = 0; bit < n; ++bit) { if (((mask >> bit) & 1) == 0 && comp[bit][women]) { res += dp(mask | (1 << bit), women + 1, comp, n, save); res %= MOD; } } save[mask][women] = res; return res; } int main() { int n; cin >> n; vector<vector<bool>> comp(n, vector<bool>(n, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { bool temp; cin >> temp; comp[i][j] = temp; } } vector<vector<ll>> save(1 << n, vector<ll>(n, -1)); ll ans = dp(0, 0, comp, n, save); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define forn(i, n) for (int i = 0; i < n; ++i) ll MOD = 1e9 + 7; ll INF = 1e17 + 9; ll dp(int mask, int women, vector<vector<bool>> &comp, int n, vector<vector<ll>> &save) { if (mask == (1 << n) - 1) return 1; else if (women >= n) return 0; else if (save[mask][women] != -1) return save[mask][women]; ll res = 0; for (int bit = 0; bit < n; ++bit) { if (((mask >> bit) & 1) == 0 && comp[bit][women]) { res += dp(mask | (1 << bit), women + 1, comp, n, save); res %= MOD; } } save[mask][women] = res; return res; } int main() { int n; cin >> n; vector<vector<bool>> comp(n, vector<bool>(n, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { bool temp; cin >> temp; comp[i][j] = temp; } } vector<vector<ll>> save(1 << n, vector<ll>(n, -1)); ll ans = dp(0, 0, comp, n, save); cout << ans << endl; }
replace
7
9
7
9
0
p03174
C++
Runtime Error
#include <bits/stdc++.h> #define L "\n" #define S " " #define ll long long #define pb push_back #define mp make_pair #define vi std::vector<int> #define si std::set<int> #define vl std::vector<long long> #define qi std::queue<int> #define s1(a) scanf("%d", &a) #define s2(a, b) scanf("%d %d", &a, &b) #define s3(a, b, c) scanf("%d %d %d", &a, &b, &c) #define de(a) cout << #a << " ------> " << a << "\n" #define SIZE 23 #define MOD 1000000007 #define INF INT_MAX #define rep(i, n) for (i = 1; i <= n; i++) #define FASTIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) using namespace std; int a[SIZE][SIZE]; ll dp[23][4100000]; bool flag[23][2100000]; vi w; int n; ll f(int men, int num) { if (men == n + 1) return 1; int now = 0, j; rep(j, n) { if (a[men][j] == 0 || w[j] == 0) continue; w[j] = 0; num |= 1 << (j - 1); if (flag[men + 1][num]) now = (now + dp[men + 1][num]) % MOD; else now = (now + f(men + 1, num)) % MOD; w[j] = 1; num &= ~(1 << (j - 1)); } flag[men][num] = 1; return dp[men][num] = now; } void solve() { int i, j; cin >> n; w.assign(n + 1, 1); rep(i, n) { rep(j, n) { cin >> a[i][j]; } } cout << f(1, 0) << L; } int main() { FASTIO; #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int t; // cin >> t; // while (t--) solve(); return 0; }
#include <bits/stdc++.h> #define L "\n" #define S " " #define ll long long #define pb push_back #define mp make_pair #define vi std::vector<int> #define si std::set<int> #define vl std::vector<long long> #define qi std::queue<int> #define s1(a) scanf("%d", &a) #define s2(a, b) scanf("%d %d", &a, &b) #define s3(a, b, c) scanf("%d %d %d", &a, &b, &c) #define de(a) cout << #a << " ------> " << a << "\n" #define SIZE 23 #define MOD 1000000007 #define INF INT_MAX #define rep(i, n) for (i = 1; i <= n; i++) #define FASTIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) using namespace std; int a[SIZE][SIZE]; ll dp[23][4100000]; bool flag[23][2100000]; vi w; int n; ll f(int men, int num) { if (men == n + 1) return 1; int now = 0, j; rep(j, n) { if (a[men][j] == 0 || w[j] == 0) continue; w[j] = 0; num |= 1 << (j - 1); if (flag[men + 1][num]) now = (now + dp[men + 1][num]) % MOD; else now = (now + f(men + 1, num)) % MOD; w[j] = 1; num &= ~(1 << (j - 1)); } flag[men][num] = 1; return dp[men][num] = now; } void solve() { int i, j; cin >> n; w.assign(n + 1, 1); rep(i, n) { rep(j, n) { cin >> a[i][j]; } } cout << f(1, 0) << L; } int main() { FASTIO; // #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // #endif int t; // cin >> t; // while (t--) solve(); return 0; }
replace
63
66
63
66
-11
p03174
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for (int i = x; i < (int)(n); ++i) const int N = 1e7, M = 7 + 1e9; int n, t; ll dp[21][N]; vector<int> v[21]; map<int, bool> sorryIhaveaboyfriend; ll cal(int i, int taken) { if (i == n) return 1; ll &ret = dp[i][taken]; if (~ret) return ret; ret = 0; f(j, 0, v[i].size()) { if (!sorryIhaveaboyfriend[v[i][j]]) { sorryIhaveaboyfriend[v[i][j]] = 1; ret += cal(i + 1, taken | (1 << v[i][j])); ret %= M; sorryIhaveaboyfriend[v[i][j]] = 0; } } return ret; } int main() { memset(dp, -1, sizeof dp); cin >> n; f(i, 0, n) { f(j, 0, n) { cin >> t; if (t) v[i].push_back(j); } } cout << cal(0, 0); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for (int i = x; i < (int)(n); ++i) const int N = 1e7, M = 7 + 1e9; int n, t; ll dp[21][2097152]; // (1 << 21)-1 the maximum number for the mask vector<int> v[21]; map<int, bool> sorryIhaveaboyfriend; ll cal(int i, int taken) { if (i == n) return 1; ll &ret = dp[i][taken]; if (~ret) return ret; ret = 0; f(j, 0, v[i].size()) { if (!sorryIhaveaboyfriend[v[i][j]]) { sorryIhaveaboyfriend[v[i][j]] = 1; ret += cal(i + 1, taken | (1 << v[i][j])); ret %= M; sorryIhaveaboyfriend[v[i][j]] = 0; } } return ret; } int main() { memset(dp, -1, sizeof dp); cin >> n; f(i, 0, n) { f(j, 0, n) { cin >> t; if (t) v[i].push_back(j); } } cout << cal(0, 0); }
replace
6
7
6
7
MLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; int a[21][21] = {0}; int state = pow(2, 21); vector<vector<int>> dp(21, vector<int>(state, 0)); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cin >> a[i][j]; } } for (int i = 0; i < N; ++i) { if (a[0][i]) dp.at(0).at(1 << i) = 1; } for (int i = 1; i < N; ++i) { for (int j = 0; j < N; ++j) { if (a[i][j]) { for (int k = 0; k < (1 << N); ++k) { if ((k >> j) % 2 == 0) { // dp.at(i).at(k+(1<<j)) = (dp.at(i).at(k+(1<<j)) + // dp.at(i-1).at(k))%1000000007; dp.at(i).at(k + (1 << j)) += dp.at(i - 1).at(k); dp.at(i).at(k + (1 << j)) %= 1000000007; } } } } } // cout << (1<<N) << endl; cout << dp.at(N - 1).at((1 << N) - 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; int a[21][21] = {0}; int state = pow(2, 21); vector<vector<int>> dp(21, vector<int>(state, 0)); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cin >> a[i][j]; } } for (int i = 0; i < N; ++i) { if (a[0][i]) dp.at(0).at(1 << i) = 1; } for (int i = 1; i < N; ++i) { for (int j = 0; j < N; ++j) { if (a[i][j]) { for (int k = 0; k < (1 << N); ++k) { if ((k >> j) % 2 == 0) { dp[i][k + (1 << j)] = (dp[i][k + (1 << j)] + dp[i - 1][k]) % 1000000007; // dp.at(i).at(k+(1<<j)) += dp.at(i-1).at(k); // dp.at(i).at(k+(1<<j)) %= 1000000007; } } } } } // cout << (1<<N) << endl; cout << dp.at(N - 1).at((1 << N) - 1) << endl; return 0; }
replace
28
32
28
32
TLE
p03174
C++
Time Limit Exceeded
// KALAM #include <bits/stdc++.h> using namespace std; const int N = 400 + 77, L = 22, Mod = 1000000007; int n, dp[L][1 << L]; bool M[N][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> M[i][j]; for (int i = 0; i < (1 << n); ++i) for (int j = 0; j < n; ++j) if ((i & (1 << j)) != 0 && M[0][j] == 1) ++dp[0][i]; for (int i = 1; i < n; ++i) { for (int msk = 1; msk < (1 << n); ++msk) { for (int j = 0; j < n; ++j) { if (((1 << j) & msk) != 0 && M[i][j] == 1) { dp[i][msk] = (dp[i][msk] + dp[i - 1][msk ^ (1 << j)]); if (dp[i][msk] >= Mod) dp[i][msk] -= Mod; } } } } cout << dp[n - 1][(1 << n) - 1]; return 0; }
// KALAM #include <bits/stdc++.h> using namespace std; const int N = 400 + 77, L = 22, Mod = 1000000007; int n, dp[L][1 << L]; bool M[N][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> M[i][j]; for (int i = 0; i < (1 << n); ++i) for (int j = 0; j < n; ++j) if ((i & (1 << j)) != 0 && M[0][j] == 1) ++dp[0][i]; for (int i = 1; i < n; ++i) { for (int msk = 1; msk < (1 << n); ++msk) { if (__builtin_popcount(msk) != i + 1) continue; for (int j = 0; j < n; ++j) { if (((1 << j) & msk) != 0 && M[i][j] == 1) { dp[i][msk] = (dp[i][msk] + dp[i - 1][msk ^ (1 << j)]); if (dp[i][msk] >= Mod) dp[i][msk] -= Mod; } } } } cout << dp[n - 1][(1 << n) - 1]; return 0; }
insert
21
21
21
23
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)), end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define FF first #define SS second template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) { return is >> p.FF >> p.SS; } template <class T> istream &operator>>(istream &is, vector<T> &xs) { for (auto &x : xs) is >> x; return is; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << p.FF << " " << p.SS; } template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) { for (unsigned int i = 0; i < xs.size(); ++i) os << (i ? " " : "") << xs[i]; return os; } template <class T> void maxi(T &x, T y) { if (x < y) x = y; } template <class T> void mini(T &x, T y) { if (x > y) x = y; } void debug(istringstream &) {} template <char sep = ',', class Head, class... Tail> void debug(istringstream &iss, Head &&head, Tail &&...tail) { string name; getline(iss, name, ','); cerr << sep << name << "=" << head; debug(iss, forward<Tail>(tail)...); } #define DEBUG(...) \ do { \ istringstream ss(#__VA_ARGS__); \ debug<' '>(ss, __VA_ARGS__); \ cerr << endl; \ } while (0) const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9 + 7; LL dp[21][1 << 21]; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; ; cin >> N; VVI xs(N, VI(N)); cin >> xs; const int B = 1 << N; dp[0][0] = 1; REP(i, N) { REP(b, B) { if (!dp[i][b]) continue; REP(k, N) { if (!xs[i][k] || b >> k & 1) continue; int nb = b | (1 << k); dp[i + 1][nb] += dp[i][b]; if (dp[i + 1][nb] >= MOD) dp[i + 1][nb] -= MOD; } } } cout << dp[N][B - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)), end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define FF first #define SS second template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) { return is >> p.FF >> p.SS; } template <class T> istream &operator>>(istream &is, vector<T> &xs) { for (auto &x : xs) is >> x; return is; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << p.FF << " " << p.SS; } template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) { for (unsigned int i = 0; i < xs.size(); ++i) os << (i ? " " : "") << xs[i]; return os; } template <class T> void maxi(T &x, T y) { if (x < y) x = y; } template <class T> void mini(T &x, T y) { if (x > y) x = y; } void debug(istringstream &) {} template <char sep = ',', class Head, class... Tail> void debug(istringstream &iss, Head &&head, Tail &&...tail) { string name; getline(iss, name, ','); cerr << sep << name << "=" << head; debug(iss, forward<Tail>(tail)...); } #define DEBUG(...) \ do { \ istringstream ss(#__VA_ARGS__); \ debug<' '>(ss, __VA_ARGS__); \ cerr << endl; \ } while (0) const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9 + 7; LL dp[22][1 << 21]; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; ; cin >> N; VVI xs(N, VI(N)); cin >> xs; const int B = 1 << N; dp[0][0] = 1; REP(i, N) { REP(b, B) { if (!dp[i][b]) continue; REP(k, N) { if (!xs[i][k] || b >> k & 1) continue; int nb = b | (1 << k); dp[i + 1][nb] += dp[i][b]; if (dp[i + 1][nb] >= MOD) dp[i + 1][nb] -= MOD; } } } cout << dp[N][B - 1] << endl; return 0; }
replace
73
74
73
74
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long const int mod = 1e9 + 7; int dp[1 << 21][21]; vector<int> a[21]; int DONE, n; int solve(int bitmask, int ind) { // base case if (bitmask == DONE) { return 1; } if (ind >= n) return 0; if (dp[bitmask][ind] != -1) return dp[bitmask][ind]; int ans = 0; // cout<<"printing "<<bitmask<<ind<<endl; for (int aa : a[ind]) { // cout<<"printing "<<bitmask<<" "<<ind<<endl; if ((bitmask & (1 << aa)) == 0) ans += solve(bitmask | (1 << aa), ind + 1); ans %= mod; } ans += solve(bitmask, ind + 1); ans %= mod; return dp[bitmask][ind] = ans; } int main() { cin >> n; int x; DONE = (1 << n) - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> x; // cout<<x<<endl; if (x == 1) a[i].push_back(j); } } /* for(int i=0;i<n;i++){ for(int aa:a[i]) cout<<aa; cout<<endl; }*/ memset(dp, -1, sizeof(dp)); int ans = solve(0, 0); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long const int mod = 1e9 + 7; int dp[1 << 21][21]; vector<int> a[21]; int DONE, n; int solve(int bitmask, int ind) { // base case if (bitmask == DONE) { return 1; } if (ind >= n) return 0; if (dp[bitmask][ind] != -1) return dp[bitmask][ind]; int ans = 0; // cout<<"printing "<<bitmask<<ind<<endl; for (int aa : a[ind]) { // cout<<"printing "<<bitmask<<" "<<ind<<endl; if ((bitmask & (1 << aa)) == 0) ans += solve(bitmask | (1 << aa), ind + 1); ans %= mod; } // ans+=solve(bitmask,ind+1); ans %= mod; return dp[bitmask][ind] = ans; } int main() { cin >> n; int x; DONE = (1 << n) - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> x; // cout<<x<<endl; if (x == 1) a[i].push_back(j); } } /* for(int i=0;i<n;i++){ for(int aa:a[i]) cout<<aa; cout<<endl; }*/ memset(dp, -1, sizeof(dp)); int ans = solve(0, 0); cout << ans << '\n'; return 0; }
replace
26
27
26
27
TLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> #include <utility> #include <vector> using namespace std; #define int long long #define reps(i, s, n) for (int(i) = (s); (i) < (n); ++(i)) #define rep(i, n) reps(i, 0, n) #define rept(i, n) rep(i, (n) + 1) #define repst(i, s, n) reps(i, s, (n) + 1) #define reprt(i, n, t) for (int(i) = (n); (i) >= (t); --(i)) #define repr(i, n) reprt(i, n, 0) #define each(i, v) for (auto &(i) : (v)) #define eachr(i, v) for (auto &(i) = (v).rbegin(); (i) != (v).rend(); (i)++) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define pb push_back #define mp make_pair #define fi first #define se second #define tmax(x, y, z) max(x, max(y, z)) #define tmin(x, y, z) min(x, min(y, z)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define ln '\n' #define bln(i, n) (((i) == (n)-1) ? '\n' : ' ') #define dbg(x) cout << #x " = " << (x) << ln << flush #define dbga(x, n) \ { \ cout << #x " : "; \ for (int(i) = 0; i < (n); ++i) { \ cout << ((x)[i]) << (i == ((n)-1) ? '\n' : ' ') << flush; \ } \ } #define zero(a) memset(a, 0, sizeof(a)) #define unq(a) sort(all(a)), a.erase(unique(all(a)), a.end()) // typedef complex<double> P; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<string> vst; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int>> mat; const ll inf = (ll)1e9 + 10; const ll linf = (ll)1e18 + 10; const ll mod = (ll)(1e9 + 7); const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1}; const double eps = 1e-10; ll mop(ll a, ll b, ll m = mod) { ll r = 1; a %= m; for (; b; b >>= 1) { if (b & 1) r = r * a % m; a = a * a % m; } return r; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } bool ool(int x, int y, int h, int w) { return ((x < 0) || (h <= x) || (y < 0) || (w <= y)); } bool deq(double a, double b) { return abs(a - b) < eps; } struct oreno_initializer { oreno_initializer() { cin.tie(0); ios::sync_with_stdio(0); } } oreno_initializer; int n, a[22][22], dp[22][1 << 21]; signed main() { cin >> n; rep(i, n) rep(j, n) cin >> a[i][j]; dp[0][0] = 1; rep(i, n) rep(b, 1 << n) rep(j, n) if (a[i][j] && ((b & (1 << j)) == 0))( dp[i + 1][b | (1 << j)] += dp[i][b]) %= mod; cout << dp[n][(1 << n) - 1] << endl; }
#include <algorithm> #include <bitset> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> #include <utility> #include <vector> using namespace std; #define int long long #define reps(i, s, n) for (int(i) = (s); (i) < (n); ++(i)) #define rep(i, n) reps(i, 0, n) #define rept(i, n) rep(i, (n) + 1) #define repst(i, s, n) reps(i, s, (n) + 1) #define reprt(i, n, t) for (int(i) = (n); (i) >= (t); --(i)) #define repr(i, n) reprt(i, n, 0) #define each(i, v) for (auto &(i) : (v)) #define eachr(i, v) for (auto &(i) = (v).rbegin(); (i) != (v).rend(); (i)++) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define pb push_back #define mp make_pair #define fi first #define se second #define tmax(x, y, z) max(x, max(y, z)) #define tmin(x, y, z) min(x, min(y, z)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define ln '\n' #define bln(i, n) (((i) == (n)-1) ? '\n' : ' ') #define dbg(x) cout << #x " = " << (x) << ln << flush #define dbga(x, n) \ { \ cout << #x " : "; \ for (int(i) = 0; i < (n); ++i) { \ cout << ((x)[i]) << (i == ((n)-1) ? '\n' : ' ') << flush; \ } \ } #define zero(a) memset(a, 0, sizeof(a)) #define unq(a) sort(all(a)), a.erase(unique(all(a)), a.end()) // typedef complex<double> P; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<string> vst; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int>> mat; const ll inf = (ll)1e9 + 10; const ll linf = (ll)1e18 + 10; const ll mod = (ll)(1e9 + 7); const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1}; const double eps = 1e-10; ll mop(ll a, ll b, ll m = mod) { ll r = 1; a %= m; for (; b; b >>= 1) { if (b & 1) r = r * a % m; a = a * a % m; } return r; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } bool ool(int x, int y, int h, int w) { return ((x < 0) || (h <= x) || (y < 0) || (w <= y)); } bool deq(double a, double b) { return abs(a - b) < eps; } struct oreno_initializer { oreno_initializer() { cin.tie(0); ios::sync_with_stdio(0); } } oreno_initializer; int n, a[22][22], dp[22][1 << 21]; signed main() { cin >> n; rep(i, n) rep(j, n) cin >> a[i][j]; dp[0][0] = 1; rep(i, n) rep(b, 1 << n) if (dp[i][b]) rep(j, n) if (a[i][j] && ((b & (1 << j)) == 0))(dp[i + 1][b | (1 << j)] += dp[i][b]) %= mod; cout << dp[n][(1 << n) - 1] << endl; }
replace
97
99
97
100
TLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> #include <vector> #define MOD 1000000007 using namespace std; int a[22][22]; long long dp[22][2107152]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; dp[0][0] = 1; int nnn = 1 << n; for (int i = 0; i < n; i++) { for (int mask = 0; mask < nnn; mask++) { for (int j = 0; j < n; j++) { if (a[i][j] && (mask & (1 << j)) == 0) { int newMask = mask | (1 << j); dp[i + 1][newMask] += dp[i][mask]; if (dp[i + 1][newMask] >= MOD) { dp[i + 1][newMask] -= MOD; } } } } } cout << dp[n][(1 << n) - 1]; return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> #define MOD 1000000007 using namespace std; int a[22][22]; long long dp[22][2107152]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; dp[0][0] = 1; int nnn = 1 << n; for (int i = 0; i < n; i++) { for (int mask = 0; mask < nnn; mask++) { if (__builtin_popcount(mask) != i) continue; for (int j = 0; j < n; j++) { if (a[i][j] && (mask & (1 << j)) == 0) { int newMask = mask | (1 << j); dp[i + 1][newMask] += dp[i][mask]; if (dp[i + 1][newMask] >= MOD) { dp[i + 1][newMask] -= MOD; } } } } } cout << dp[n][(1 << n) - 1]; return 0; }
insert
22
22
22
24
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll dp[1000000]; ll arr[22][22]; ll n; ll m = 1000000000 + 7; ll solve(ll mask) { ll j = __builtin_popcount(mask); if (j == n) return 1; if (dp[mask] != -1) return dp[mask]; ll ans = 0; for (int i = 0; i < n; i++) { if (arr[j][i] && !(mask & (1 << i))) { ans = (ans % m + solve(mask | (1 << i)) % m) % m; } } dp[mask] = ans; return dp[mask]; } int main() { memset(dp, -1, sizeof(dp)); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; } } cout << solve(0) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll dp[2097152]; ll arr[25][25]; ll n; ll m = 1000000000 + 7; ll solve(ll mask) { ll j = __builtin_popcount(mask); if (j == n) return 1; if (dp[mask] != -1) return dp[mask]; ll ans = 0; for (int i = 0; i < n; i++) { if (arr[j][i] && !(mask & (1 << i))) { ans = (ans % m + solve(mask | (1 << i)) % m) % m; } } dp[mask] = ans; return dp[mask]; } int main() { memset(dp, -1, sizeof(dp)); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; } } cout << solve(0) << endl; }
replace
6
8
6
8
0
p03174
C++
Time Limit Exceeded
#pragma GCC optimize("O3") #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; template <class T> inline T chmax(T &a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T chmin(T &a, const T b) { return a = (a > b) ? b : a; } constexpr int MOD = 1e9 + 7; constexpr int inf = 1e9; constexpr long long INF = 1e18; #define all(a) (a).begin(), (a).end() int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; 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]; } vector<vector<ll>> dp(n + 1, vector<ll>((1 << n), 0)); dp[0][0] = 1; for (int bit = 0; bit < (1 << n); bit++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (!(bit & (1 << j)) && a[i][j]) { dp[i + 1][bit | (1 << j)] += dp[i][bit]; dp[i + 1][bit | (1 << j)] %= MOD; } } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
#pragma GCC optimize("O3") #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; template <class T> inline T chmax(T &a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T chmin(T &a, const T b) { return a = (a > b) ? b : a; } constexpr int MOD = 1e9 + 7; constexpr int inf = 1e9; constexpr long long INF = 1e18; #define all(a) (a).begin(), (a).end() int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; 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]; } vector<vector<ll>> dp(n + 1, vector<ll>((1 << n), 0)); dp[0][0] = 1; for (int bit = 0; bit < (1 << n); bit++) { int i = __builtin_popcount(bit); for (int j = 0; j < n; j++) { if (!(bit & (1 << j)) && a[i][j]) { dp[i + 1][bit | (1 << j)] += dp[i][bit]; dp[i + 1][bit | (1 << j)] %= MOD; } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
replace
64
70
64
69
TLE
p03174
C++
Runtime Error
// #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #include <x86intrin.h> #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define F first #define S second #define lb lower_bound #define ub upper_bound #define pb push_back #define pf push_front #define ppb pop_back #define mp make_pair #define bpp __builtin_popcountll #define sqr(x) ((x) * (x)) #define al 0x3F3F3F3F #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define in insert #define ppf pop_front #define endl '\n' // #define int long long typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; const int mod = (int)1e9 + 7; const int N = (int)3e5 + 123; const ll inf = (ll)1e18 + 1; const double pi = acos(-1.0); const double eps = 1e-7; const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; int n, dp[21][(1 << 21)]; vector<int> can[(1 << 21)]; int a[22][22]; inline int add(int a, int b) { a += b; if (a >= mod) a -= mod; return a; } inline void boost() { ios_base ::sync_with_stdio(NULL); cin.tie(NULL), cout.tie(NULL); } inline void Solve() { cin >> n; for (int i = 1; i <= n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; dp[0][0] = 1; for (int mask = 0; mask < (1 << n); mask++) for (int i = 0; i < n; i++) if (!(mask & (1 << i))) can[mask].pb(i); for (int i = 1; i <= n; i++) { for (int mask = 0; mask < (1 << n); mask++) { if (bpp(mask) != i - 1) continue; for (auto nxt : can[mask]) { if (!a[i][nxt]) continue; dp[i][mask | (1 << nxt)] = add(dp[i][mask | (1 << nxt)], dp[i - 1][mask]); } } } cout << dp[n][(1 << n) - 1]; } int main() { // freopen (".in", "r", stdin); // freopen (".out", "w", stdout); boost(); int tt = 1; // cin >> tt; while (tt--) { Solve(); } return 0; }
// #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #include <x86intrin.h> #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define F first #define S second #define lb lower_bound #define ub upper_bound #define pb push_back #define pf push_front #define ppb pop_back #define mp make_pair #define bpp __builtin_popcountll #define sqr(x) ((x) * (x)) #define al 0x3F3F3F3F #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define in insert #define ppf pop_front #define endl '\n' // #define int long long typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; const int mod = (int)1e9 + 7; const int N = (int)3e5 + 123; const ll inf = (ll)1e18 + 1; const double pi = acos(-1.0); const double eps = 1e-7; const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; int n, dp[22][(1 << 21)]; vector<int> can[(1 << 21)]; int a[22][22]; inline int add(int a, int b) { a += b; if (a >= mod) a -= mod; return a; } inline void boost() { ios_base ::sync_with_stdio(NULL); cin.tie(NULL), cout.tie(NULL); } inline void Solve() { cin >> n; for (int i = 1; i <= n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; dp[0][0] = 1; for (int mask = 0; mask < (1 << n); mask++) for (int i = 0; i < n; i++) if (!(mask & (1 << i))) can[mask].pb(i); for (int i = 1; i <= n; i++) { for (int mask = 0; mask < (1 << n); mask++) { if (bpp(mask) != i - 1) continue; for (auto nxt : can[mask]) { if (!a[i][nxt]) continue; dp[i][mask | (1 << nxt)] = add(dp[i][mask | (1 << nxt)], dp[i - 1][mask]); } } } cout << dp[n][(1 << n) - 1]; } int main() { // freopen (".in", "r", stdin); // freopen (".out", "w", stdout); boost(); int tt = 1; // cin >> tt; while (tt--) { Solve(); } return 0; }
replace
47
48
47
48
-11
p03174
C++
Time Limit Exceeded
#pragma GCC target("avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include <iostream> #include <vector> using namespace std; int upbound; const int MOD = 1000000007; vector<int> g, dp; inline int Solve(int x) { if (x == 0) return 1; if (dp[x] != -1) return dp[x]; int p = __builtin_popcount(x), r = 0; for (int y = x & g[p - 1]; y; y &= y - 1) { r += Solve(x & ~(y & -y)); if (r >= MOD) r -= MOD; } return r; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, r; char p; cin >> n; upbound = (1 << n); g = vector<int>(n), dp = vector<int>(upbound, -1); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { cin >> p; if (p == '1') g[i] |= (1 << j); } cout << Solve(upbound - 1); }
#pragma GCC target("avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include <iostream> #include <vector> using namespace std; int upbound; const int MOD = 1000000007; vector<int> g, dp; inline int Solve(int x) { if (x == 0) return 1; if (dp[x] != -1) return dp[x]; int p = __builtin_popcount(x), r = 0; for (int y = x & g[p - 1]; y; y &= y - 1) { r += Solve(x & ~(y & -y)); if (r >= MOD) r -= MOD; } dp[x] = r; return r; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, r; char p; cin >> n; upbound = (1 << n); g = vector<int>(n), dp = vector<int>(upbound, -1); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { cin >> p; if (p == '1') g[i] |= (1 << j); } cout << Solve(upbound - 1); }
insert
23
23
23
24
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long int // comment for large arrays #define pll pair<int, int> #define dbl long double #define ff first #define ss second #define endl "\n" #define mod 1000000007 #define eps 0.00000001 #define INF 10000000000000001 #define all(x) (x).begin(), (x).end() #define LB(v, x) (lower_bound(all(v), x) - v.begin()) #define UB(v, x) (upper_bound(all(v), x) - v.begin()) #define size(x) (int)(x).size() #define pb(x) push_back(x) #define pf(x) push_front(x) #define popb() pop_back() #define popf() pop_front() #define mp(x, y) make_pair((x), (y)) #define vec(dt) vector<dt> #define vv(dt) vector<vector<dt>> #define fastio(x) \ ios_base::sync_with_stdio(x); \ cin.tie(NULL) #define init(v, s) memset(v, s, sizeof(v)) #define bug(x) \ cerr << "LINE: " << __LINE__ << " || click to see test details " << #x \ << " = " << x << endl #define loop(i, s, n) for (int i = s; i < n; i++) #define print(v) \ for (auto it : v) \ cout << it << " "; \ cout << endl using namespace std; signed main() { fastio(0); int n; cin >> n; vec(int) dp(1 << n, 0), a[n]; loop(i, 0, n) loop(j, 0, n) { int x; cin >> x; if (x == 1) a[i].pb(j); } for (auto j : a[0]) dp[1 << j] = 1; loop(i, 1, n) { for (int mask = (1 << n) - 1; mask >= 0; mask--) { for (auto j : a[i]) if (mask & (1 << j)) dp[mask] += dp[mask - (1 << j)]; dp[mask] %= mod; } } cout << dp[(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> #define int long long int // comment for large arrays #define pll pair<int, int> #define dbl long double #define ff first #define ss second #define endl "\n" #define mod 1000000007 #define eps 0.00000001 #define INF 10000000000000001 #define all(x) (x).begin(), (x).end() #define LB(v, x) (lower_bound(all(v), x) - v.begin()) #define UB(v, x) (upper_bound(all(v), x) - v.begin()) #define size(x) (int)(x).size() #define pb(x) push_back(x) #define pf(x) push_front(x) #define popb() pop_back() #define popf() pop_front() #define mp(x, y) make_pair((x), (y)) #define vec(dt) vector<dt> #define vv(dt) vector<vector<dt>> #define fastio(x) \ ios_base::sync_with_stdio(x); \ cin.tie(NULL) #define init(v, s) memset(v, s, sizeof(v)) #define bug(x) \ cerr << "LINE: " << __LINE__ << " || click to see test details " << #x \ << " = " << x << endl #define loop(i, s, n) for (int i = s; i < n; i++) #define print(v) \ for (auto it : v) \ cout << it << " "; \ cout << endl using namespace std; signed main() { fastio(0); int n; cin >> n; vec(int) dp(1 << n, 0), a[n]; loop(i, 0, n) loop(j, 0, n) { int x; cin >> x; if (x == 1) a[i].pb(j); } dp[0] = 1; loop(mask, 0, (1 << n) - 1) { int i = __builtin_popcount(mask); for (auto j : a[i]) if ((mask & (1 << j)) == 0) dp[mask + (1 << j)] = (dp[mask + (1 << j)] + dp[mask]) % mod; } cout << dp[(1 << n) - 1] << endl; return 0; }
replace
46
55
46
52
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int long long const int MAX_N = 22; const int MOD = 1e9 + 7; int to[MAX_N][MAX_N]; int dp[MAX_N][(1 << MAX_N)]; void modadd(int &a, int b) { a = (a + b) % MOD; } signed main(void) { int N; cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> to[i][j]; } } // 一番はじめの男についてだけ愚直に計算する for (int f = 0; f < N; f++) { if (to[0][f]) { dp[0][(1 << f)]++; } } for (int male = 0; male < N - 1; male++) { // 何番目の男までを見たか for (int female = 0; female < (1 << N); female++) { // どの女がまだ残っているか // 次に追加するのは male+1 番目の男である for (int check = 0; check < N; check++) { if (!(female >> check & 1) && to[male + 1][check]) { // まだ残っていて、かつmaleと相性がいい女がいたら合わせる modadd(dp[male + 1][female | (1 << check)], dp[male][female]); } } } } cout << dp[N - 1][(1 << N) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int long long const int MAX_N = 22; const int MOD = 1e9 + 7; int to[MAX_N][MAX_N]; int dp[MAX_N][(1 << MAX_N)]; void modadd(int &a, int b) { a = (a + b) % MOD; } signed main(void) { int N; cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> to[i][j]; } } // 一番はじめの男についてだけ愚直に計算する for (int f = 0; f < N; f++) { if (to[0][f]) { dp[0][(1 << f)]++; } } for (int male = 0; male < N - 1; male++) { // 何番目の男までを見たか for (int female = 0; female < (1 << N); female++) { // どの女がまだ残っているか // 次に追加するのは male+1 番目の男である // 枝刈り int cnt = 0; for (int i = 0; i < N; i++) { if (female >> i & 1) { cnt++; } } if (cnt != male + 1) { continue; } for (int check = 0; check < N; check++) { if (!(female >> check & 1) && to[male + 1][check]) { // まだ残っていて、かつmaleと相性がいい女がいたら合わせる modadd(dp[male + 1][female | (1 << check)], dp[male][female]); } } } } cout << dp[N - 1][(1 << N) - 1] << endl; return 0; }
insert
36
36
36
49
TLE
p03174
C++
Runtime Error
#pragma GCC optimize("-O3") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #include <boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; using namespace std; #define all(c) (c).begin(), (c).end() #define endl "\n" #define ff first #define ss second #define allr(c) (c).rbegin(), (c).rend() #define ifr(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define pof pop_front #define pob pop_back #define pb emplace_back #define pf emplace_front #define fstm(m, n, r) \ m.reserve(n); \ m.max_load_factor(r) #define mp make_pair #define mt make_tuple #define inf LLONG_MAX #define os \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). const double PI = acos(-1); typedef complex<double> cd; typedef long long ll; ll gcd() { return 0ll; } template <typename T, typename... Args> T gcd(T a, Args... args) { return __gcd(a, (__typeof(a))gcd(args...)); } typedef map<ll, ll> mll; typedef map<string, ll> msll; typedef unordered_map<ll, ll> umap; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef long double ld; #define mod 1000000007 #define N 22 ll dp[1 << N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif int n, i; cin >> n; bool a[n][n]; ifr(i, 0, n) ifr(j, 0, n) cin >> a[i][j]; dp[0] = 1; ifr(mask, 0, (1 << n) - 1) { i = __builtin_popcount(mask); ifr(j, 0, n) { if (!((mask >> j) & 1) && a[i][j]) dp[mask ^ (1 << j)] = (dp[mask ^ (1 << j)] + dp[mask]) % mod; } } cout << dp[(1 << n) - 1] << endl; return 0; }
#pragma GCC optimize("-O3") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #include <boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; using namespace std; #define all(c) (c).begin(), (c).end() #define endl "\n" #define ff first #define ss second #define allr(c) (c).rbegin(), (c).rend() #define ifr(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define pof pop_front #define pob pop_back #define pb emplace_back #define pf emplace_front #define fstm(m, n, r) \ m.reserve(n); \ m.max_load_factor(r) #define mp make_pair #define mt make_tuple #define inf LLONG_MAX #define os \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). const double PI = acos(-1); typedef complex<double> cd; typedef long long ll; ll gcd() { return 0ll; } template <typename T, typename... Args> T gcd(T a, Args... args) { return __gcd(a, (__typeof(a))gcd(args...)); } typedef map<ll, ll> mll; typedef map<string, ll> msll; typedef unordered_map<ll, ll> umap; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef long double ld; #define mod 1000000007 #define N 22 ll dp[1 << N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, i; cin >> n; bool a[n][n]; ifr(i, 0, n) ifr(j, 0, n) cin >> a[i][j]; dp[0] = 1; ifr(mask, 0, (1 << n) - 1) { i = __builtin_popcount(mask); ifr(j, 0, n) { if (!((mask >> j) & 1) && a[i][j]) dp[mask ^ (1 << j)] = (dp[mask ^ (1 << j)] + dp[mask]) % mod; } } cout << dp[(1 << n) - 1] << endl; return 0; }
replace
54
57
54
55
-11
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define md 1000000007 #define mx 1e18 #define pb push_back #define endl '\n' #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define pii pair<ll int, ll int> #define ff first #define ss second int main() { fastio #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll int n; cin >> n; ll int ar[n + 1][n + 1]; for (ll int i = 0; i < n; i++) for (ll int j = 0; j < n; j++) cin >> ar[i][j]; ll int dp[(1 << n) + 1]; memset(dp, 0, sizeof(dp)); dp[0] = 1LL; // for(ll int i=0;i<n;i++) // { for (ll int j = 0; j < (1 << n); j++) { ll int i = __builtin_popcount(j); for (ll int k = 0; k < n; k++) { if (ar[i][k] && !(j & (1LL << k))) { ll int nm = j ^ (1LL << k); dp[nm] += dp[j]; dp[nm] %= md; } } } // } cout << dp[(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define md 1000000007 #define mx 1e18 #define pb push_back #define endl '\n' #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define pii pair<ll int, ll int> #define ff first #define ss second int main() { // fastio // #ifndef ONLINE_JUDGE // freopen("input.txt" , "r" , stdin); // freopen("output.txt" , "w" , stdout); // #endif ll int n; cin >> n; ll int ar[n + 1][n + 1]; for (ll int i = 0; i < n; i++) for (ll int j = 0; j < n; j++) cin >> ar[i][j]; ll int dp[(1 << n) + 1]; memset(dp, 0, sizeof(dp)); dp[0] = 1LL; // for(ll int i=0;i<n;i++) // { for (ll int j = 0; j < (1 << n); j++) { ll int i = __builtin_popcount(j); for (ll int k = 0; k < n; k++) { if (ar[i][k] && !(j & (1LL << k))) { ll int nm = j ^ (1LL << k); dp[nm] += dp[j]; dp[nm] %= md; } } } // } cout << dp[(1 << n) - 1] << endl; return 0; }
replace
16
21
16
21
-11
p03174
C++
Runtime Error
/** Which of the favors of your Lord will you deny ? **/ #include <bits/stdc++.h> using namespace std; #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(), (x).end() #define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl #define READ freopen("alu.txt", "r", stdin) #define WRITE freopen("vorta.txt", "w", stdout) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class TIn> using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag, tree_order_statistics_node_update>; /** PBDS ------------------------------------------------- 1) insert(value) 2) erase(value) 3) order_of_key(value) // 0 based indexing 4) *find_by_order(position) // 0 based indexing **/ template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p); template <class T> ostream &operator<<(ostream &os, vector<T> &v); template <class T> ostream &operator<<(ostream &os, set<T> &v); inline void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const int nmax = 2e5 + 7; const LL LINF = 1e17; template <class T> string to_str(T x) { stringstream ss; ss << x; return ss.str(); } // bool cmp(const PII &A,const PII &B) //{ // // } int match[25][25]; const LL mod = 1e9 + 7; LL add_self(LL &a, LL b) { a += b; if (a >= mod) a -= mod; } int main() { optimizeIO(); int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> match[i][j]; vector<LL> dp(1 << n); dp[0] = 1; for (int mask = 0; mask < (1 << n) - 1; mask++) { int m = __builtin_popcount(mask); /// number of already matched men = number /// of already matched women for (int w = 0; w < n; w++) { if (match[m][w] && !(mask & (1 << w))) /// this woman is not matched yet and can be /// matched with m+1 th man add_self(dp[mask | (1 << w)], dp[mask]); } } cout << dp[(1 << n) - 1] << endl; return 0; } /** **/ template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p) { os << "{" << p.first << ", " << p.second << "} "; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { os << "[ "; for (int i = 0; i < v.size(); i++) { os << v[i] << " "; } os << " ]"; return os; } template <class T> ostream &operator<<(ostream &os, set<T> &v) { os << "[ "; for (T i : v) { os << i << " "; } os << " ]"; return os; }
/** Which of the favors of your Lord will you deny ? **/ #include <bits/stdc++.h> using namespace std; #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(), (x).end() #define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl #define READ freopen("alu.txt", "r", stdin) #define WRITE freopen("vorta.txt", "w", stdout) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class TIn> using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag, tree_order_statistics_node_update>; /** PBDS ------------------------------------------------- 1) insert(value) 2) erase(value) 3) order_of_key(value) // 0 based indexing 4) *find_by_order(position) // 0 based indexing **/ template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p); template <class T> ostream &operator<<(ostream &os, vector<T> &v); template <class T> ostream &operator<<(ostream &os, set<T> &v); inline void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const int nmax = 2e5 + 7; const LL LINF = 1e17; template <class T> string to_str(T x) { stringstream ss; ss << x; return ss.str(); } // bool cmp(const PII &A,const PII &B) //{ // // } int match[25][25]; const LL mod = 1e9 + 7; void add_self(LL &a, LL b) { a += b; if (a >= mod) a -= mod; } int main() { optimizeIO(); int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> match[i][j]; vector<LL> dp(1 << n); dp[0] = 1; for (int mask = 0; mask < (1 << n) - 1; mask++) { int m = __builtin_popcount(mask); /// number of already matched men = number /// of already matched women for (int w = 0; w < n; w++) { if (match[m][w] && !(mask & (1 << w))) /// this woman is not matched yet and can be /// matched with m+1 th man add_self(dp[mask | (1 << w)], dp[mask]); } } cout << dp[(1 << n) - 1] << endl; return 0; } /** **/ template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p) { os << "{" << p.first << ", " << p.second << "} "; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { os << "[ "; for (int i = 0; i < v.size(); i++) { os << v[i] << " "; } os << " ]"; return os; } template <class T> ostream &operator<<(ostream &os, set<T> &v) { os << "[ "; for (T i : v) { os << i << " "; } os << " ]"; return os; }
replace
64
65
64
65
0
p03174
C++
Runtime Error
/** Which of the favors of your Lord will you deny ? **/ #include <bits/stdc++.h> using namespace std; #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(), (x).end() #define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl #define READ freopen("alu.txt", "r", stdin) #define WRITE freopen("vorta.txt", "w", stdout) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class TIn> using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag, tree_order_statistics_node_update>; /** PBDS ------------------------------------------------- 1) insert(value) 2) erase(value) 3) order_of_key(value) // 0 based indexing 4) *find_by_order(position) // 0 based indexing **/ template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p); template <class T> ostream &operator<<(ostream &os, vector<T> &v); template <class T> ostream &operator<<(ostream &os, set<T> &v); inline void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const int nmax = 2e5 + 7; const LL LINF = 1e17; template <class T> string to_str(T x) { stringstream ss; ss << x; return ss.str(); } // bool cmp(const PII &A,const PII &B) //{ // // } int match[25][25]; const LL mod = 1e9 + 7; void add_self(LL &a, LL b) { a += b; if (a >= mod) a -= mod; } LL dp[21][1 << 21]; int n; LL solve(int m, int mask) { LL &ret = dp[m][mask]; if (ret != -1) return ret; if (m == n) return ret = (mask == (1 << n) - 1); /// n pair is matched ret = 0; for (int w = 0; w < n; w++) { if (match[m][w] && !(mask & (1 << w))) add_self(ret, solve(m + 1, mask | (1 << w))); } return ret; } int main() { optimizeIO(); cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> match[i][j]; memset(dp, -1, sizeof dp); cout << solve(0, 0) << endl; return 0; } /** **/ template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p) { os << "{" << p.first << ", " << p.second << "} "; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { os << "[ "; for (int i = 0; i < v.size(); i++) { os << v[i] << " "; } os << " ]"; return os; } template <class T> ostream &operator<<(ostream &os, set<T> &v) { os << "[ "; for (T i : v) { os << i << " "; } os << " ]"; return os; }
/** Which of the favors of your Lord will you deny ? **/ #include <bits/stdc++.h> using namespace std; #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(), (x).end() #define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl #define READ freopen("alu.txt", "r", stdin) #define WRITE freopen("vorta.txt", "w", stdout) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class TIn> using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag, tree_order_statistics_node_update>; /** PBDS ------------------------------------------------- 1) insert(value) 2) erase(value) 3) order_of_key(value) // 0 based indexing 4) *find_by_order(position) // 0 based indexing **/ template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p); template <class T> ostream &operator<<(ostream &os, vector<T> &v); template <class T> ostream &operator<<(ostream &os, set<T> &v); inline void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const int nmax = 2e5 + 7; const LL LINF = 1e17; template <class T> string to_str(T x) { stringstream ss; ss << x; return ss.str(); } // bool cmp(const PII &A,const PII &B) //{ // // } int match[25][25]; const LL mod = 1e9 + 7; void add_self(LL &a, LL b) { a += b; if (a >= mod) a -= mod; } LL dp[22][1 << 22]; int n; LL solve(int m, int mask) { LL &ret = dp[m][mask]; if (ret != -1) return ret; if (m == n) return ret = (mask == (1 << n) - 1); /// n pair is matched ret = 0; for (int w = 0; w < n; w++) { if (match[m][w] && !(mask & (1 << w))) add_self(ret, solve(m + 1, mask | (1 << w))); } return ret; } int main() { optimizeIO(); cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> match[i][j]; memset(dp, -1, sizeof dp); cout << solve(0, 0) << endl; return 0; } /** **/ template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p) { os << "{" << p.first << ", " << p.second << "} "; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { os << "[ "; for (int i = 0; i < v.size(); i++) { os << v[i] << " "; } os << " ]"; return os; } template <class T> ostream &operator<<(ostream &os, set<T> &v) { os << "[ "; for (T i : v) { os << i << " "; } os << " ]"; return os; }
replace
70
71
70
71
-11
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <functional> #include <iostream> #include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; static const int mod = 1000000007; static const int MAX_N = 21; int DP[1 << MAX_N][MAX_N]; // メモ化テーブル int N; int a[MAX_N][MAX_N]; int rec(int S, int V); int calc(); int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; } } for (int i = 0; i < 1 << N; i++) { for (int j = 0; j < N; j++) { DP[i][j] = -1; } } cout << calc() << endl; return 0; } int rec(int S, int V, int n) { if (DP[S][V] >= 0) { return DP[S][V]; } if (!a[n][V]) { DP[S][V] = 0; return 0; } else if (n == N - 1) { DP[S][V] = 1; return DP[S][V]; } int sum = 0; int nextS = S | (1 << V); for (int i = 0; i < N; i++) { if (i != V && !(S >> i & 1)) { // なるべく関数呼び出しを少なくすると少し速くなる... sum += DP[nextS][i] >= 0 ? DP[nextS][i] : rec(nextS, i, n + 1); sum %= mod; } } DP[S][V] = sum; return DP[S][V]; } int calc() { int sum = 0; for (int i = 0; i < N; i++) { sum += rec(0, i, 0); sum %= mod; } return sum; }
#include <algorithm> #include <climits> #include <functional> #include <iostream> #include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; static const int mod = 1000000007; static const int MAX_N = 21; int DP[1 << MAX_N][MAX_N]; // メモ化テーブル int N; int a[MAX_N][MAX_N]; int rec(int S, int V); int calc(); int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; } } std::fill(DP[0], DP[(1 << MAX_N) - 1], -1); cout << calc() << endl; return 0; } int rec(int S, int V, int n) { if (DP[S][V] >= 0) { return DP[S][V]; } if (!a[n][V]) { DP[S][V] = 0; return 0; } else if (n == N - 1) { DP[S][V] = 1; return DP[S][V]; } int sum = 0; int nextS = S | (1 << V); for (int i = 0; i < N; i++) { if (i != V && !(S >> i & 1)) { // なるべく関数呼び出しを少なくすると少し速くなる... sum += DP[nextS][i] >= 0 ? DP[nextS][i] : rec(nextS, i, n + 1); sum %= mod; } } DP[S][V] = sum; return DP[S][V]; } int calc() { int sum = 0; for (int i = 0; i < N; i++) { sum += rec(0, i, 0); sum %= mod; } return sum; }
replace
32
37
32
33
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define fastio \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); // STANDARD DATA TYPES #define ll long long #define llu unsigned long long #define ld long double // ADV DATA TYPES #define pii pair<int, int> #define pll pair<ll, ll> #define mp make_pair #define ff first #define ss second // C ++ DS #define vi vector<int> #define vll vector<ll> #define pb push_back #define pf push_front #define iter ::iterator #define all(x) x.begin(), x.end() // priority_queue < pii, vector < pii >, greater < pii > > pq; // lower_bound(v.begin(),v.end(),20);//for any sorted container // CONSTANTS #define MOD (int)(1e9 + 7) #define INF 0x3f3f3f3f #define MAXN (int)(1e5 + 1) // MOD OPERATIONS inline ll fpow(ll n, ll k, int p = MOD) { ll r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } inline ll inv(ll a, ll p = MOD) { return fpow(a, p - 2, p); } inline ll addmod(ll a, ll val, ll p = MOD) { { if ((a = (a + val)) >= p) a -= p; } return a; } inline ll submod(ll a, ll val, ll p = MOD) { { if ((a = (a - val)) < 0) a += p; } return a; } inline ll mult(ll a, ll b, ll p = MOD) { return (ll)a * b % p; } // DEBUG #define shout() \ { cout << "I'm Here...!!!" << endl; } #define dbg(x) \ { cout << #x << ": " << (x) << endl; } #define dbg2(x, y) \ { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; } int main() { fastio; int n; cin >> n; vector<vi> match(n, vi(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> match[i][j]; } // dp[i][mask] represent number of ways to match subset women(mask) with upto // 0 to i men for i+1 iterate over masks then jth women if jth women can pair // with ith men dp[i+1][mask^(1<<j)]+=dp[i][mask] else // dp[i+1][mask]+=dp[i][mask]//dont pair ith men(note if we iterate mask big // to small then this will be first update of dp[i+1][mask]) vector<int> dp(1 << n); dp[0] = 1; for (int i = 0; i < n; i++) // ith men { for (int mask = (1 << n) - 1; mask >= 0; mask--) { for (int j = 0; j < n; j++) { if (match[i][j] && !(mask & (1 << j))) { dp[mask ^ (1 << j)] = addmod(dp[mask ^ (1 << j)], dp[mask]); } } } } cout << dp[(1 << n) - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define fastio \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); // STANDARD DATA TYPES #define ll long long #define llu unsigned long long #define ld long double // ADV DATA TYPES #define pii pair<int, int> #define pll pair<ll, ll> #define mp make_pair #define ff first #define ss second // C ++ DS #define vi vector<int> #define vll vector<ll> #define pb push_back #define pf push_front #define iter ::iterator #define all(x) x.begin(), x.end() // priority_queue < pii, vector < pii >, greater < pii > > pq; // lower_bound(v.begin(),v.end(),20);//for any sorted container // CONSTANTS #define MOD (int)(1e9 + 7) #define INF 0x3f3f3f3f #define MAXN (int)(1e5 + 1) // MOD OPERATIONS inline ll fpow(ll n, ll k, int p = MOD) { ll r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } inline ll inv(ll a, ll p = MOD) { return fpow(a, p - 2, p); } inline ll addmod(ll a, ll val, ll p = MOD) { { if ((a = (a + val)) >= p) a -= p; } return a; } inline ll submod(ll a, ll val, ll p = MOD) { { if ((a = (a - val)) < 0) a += p; } return a; } inline ll mult(ll a, ll b, ll p = MOD) { return (ll)a * b % p; } // DEBUG #define shout() \ { cout << "I'm Here...!!!" << endl; } #define dbg(x) \ { cout << #x << ": " << (x) << endl; } #define dbg2(x, y) \ { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; } int main() { fastio; int n; cin >> n; vector<vi> match(n, vi(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> match[i][j]; } // dp[i][mask] represent number of ways to match subset women(mask) with upto // 0 to i men for i+1 iterate over masks then jth women if jth women can pair // with ith men dp[i+1][mask^(1<<j)]+=dp[i][mask] else // dp[i+1][mask]+=dp[i][mask]//dont pair ith men(note if we iterate mask big // to small then this will be first update of dp[i+1][mask]) vector<int> dp(1 << n); dp[0] = 1; for (int i = 0; i < n; i++) // ith men { for (int mask = (1 << n) - 1; mask >= 0; mask--) { if (dp[mask] == 0) continue; for (int j = 0; j < n; j++) { if (match[i][j] && !(mask & (1 << j))) { dp[mask ^ (1 << j)] = addmod(dp[mask ^ (1 << j)], dp[mask]); } } } } cout << dp[(1 << n) - 1] << endl; }
insert
83
83
83
85
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> pii; typedef pair<int, pair<int, int>> piii; #define pb push_back #define debug(x) cout << x << endl #define fastio ios_base::sync_with_stdio(0), cin.tie(0) #define PI acos(-1) #define all(c) c.begin(), c.end() #define SET(x, y) memset((x), y, sizeof(x)) const int MOD = 1e9 + 7; const int INF = 1e9 + 5; const LL INF64 = 1e18; const int N = 1e5 + 5; bool grid[25][25]; LL dp[25][(1LL << 22)]; int main() { fastio; int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) cin >> grid[i][j]; } SET(dp, 0); dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < (1 << n); j++) { for (int k = 0; k < n; k++) { int setted = n - k - 1; if (grid[i][k] == 1 && !(j & (1 << setted))) { dp[i][j ^ (1 << setted)] = (dp[i][j ^ (1 << setted)] + dp[i - 1][j]) % MOD; } } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> pii; typedef pair<int, pair<int, int>> piii; #define pb push_back #define debug(x) cout << x << endl #define fastio ios_base::sync_with_stdio(0), cin.tie(0) #define PI acos(-1) #define all(c) c.begin(), c.end() #define SET(x, y) memset((x), y, sizeof(x)) const int MOD = 1e9 + 7; const int INF = 1e9 + 5; const LL INF64 = 1e18; const int N = 1e5 + 5; bool grid[25][25]; LL dp[25][(1LL << 22)]; int main() { fastio; int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) cin >> grid[i][j]; } SET(dp, 0); dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < (1 << n); j++) { if (dp[i - 1][j] == 0 || __builtin_popcount(j) != i - 1) continue; for (int k = 0; k < n; k++) { int setted = n - k - 1; if (grid[i][k] == 1 && !(j & (1 << setted))) { dp[i][j ^ (1 << setted)] = (dp[i][j ^ (1 << setted)] + dp[i - 1][j]) % MOD; } } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
insert
32
32
32
34
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define LLINF 1000000000000000ll #define sz(x) ((int)(x).size()) #define fi first #define sec second #define all(x) (x).begin(), (x).end() #define sq(x) ((x) * (x)) #define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++) #define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++) #define EQ(a, b) (abs((a) - (b)) < eps) #define dmp(x) cerr << __LINE__ << " " << #x << " " << x << endl; template <class T> void chmin(T &a, const T &b) { if (a > b) a = b; } template <class T> void chmax(T &a, const T &b) { if (a < b) a = b; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) { os << p.fi << ',' << p.sec; return os; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.fi >> p.sec; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0; i < vec.size(); i++) { os << vec[i]; if (i + 1 < vec.size()) os << ' '; } return os; } template <class T> istream &operator>>(istream &is, vector<T> &vec) { for (int i = 0; i < vec.size(); i++) is >> vec[i]; return is; } void fastio() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); } ll MOD = 1000000007ll; // if inv is needed, this shold be prime. struct ModInt { ll val; ModInt() : val(0ll) {} ModInt(ll v) : val(((v % MOD) + MOD) % MOD) {} ModInt exp(ll y) const { if (!y) return ModInt(1ll); ModInt a = exp(y / 2ll); a *= a; if (y & 1) a *= (*this); return a; } bool operator==(const ModInt &x) const { return val == x.val; } inline bool operator!=(const ModInt &x) const { return !(*this == x); } bool operator<(const ModInt &x) const { return val < x.val; } bool operator>(const ModInt &x) const { return val > x.val; } inline bool operator>=(const ModInt &x) const { return !(*this < x); } inline bool operator<=(const ModInt &x) const { return !(*this > x); } ModInt &operator+=(const ModInt &x) { if ((val += x.val) >= MOD) val -= MOD; return *this; } ModInt &operator-=(const ModInt &x) { if ((val += MOD - x.val) >= MOD) val -= MOD; return *this; } ModInt &operator*=(const ModInt &x) { (val *= x.val) %= MOD; return *this; } ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; } ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; } ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; } }; istream &operator>>(istream &i, ModInt &x) { i >> x.val; return i; } ostream &operator<<(ostream &o, const ModInt &x) { o << x.val; return o; } ModInt pow(ModInt a, ll x) { ModInt res = ModInt(1ll); while (x) { if (x & 1) res *= a; x >>= 1; a = a * a; } return res; } const int SIZE = 100100; ModInt inv[SIZE + 10], fac[SIZE + 10], facinv[SIZE + 10]; // notice: 0C0 = 1 ModInt nCr(int n, int r) { assert(!(n < r)); assert(!(n < 0 || r < 0)); return fac[n] * facinv[r] * facinv[n - r]; } void init() { fac[0] = ModInt(1ll); for (int i = 1; i <= SIZE; i++) fac[i] = fac[i - 1] * ModInt(i); inv[1] = ModInt(1ll); for (int i = 2; i <= SIZE; i++) inv[i] = ModInt(0ll) - ModInt(MOD / i) * inv[MOD % i]; facinv[0] = ModInt(1ll); for (int i = 1; i <= SIZE; i++) facinv[i] = facinv[i - 1] * inv[i]; return; } int N; int a[30][30]; ModInt dp[24][1 << 22]; int main() { cin >> N; rep(i, N) { rep(j, N) { cin >> a[i][j]; } } dp[0][0] = ModInt(1ll); rep(i, N) { rep(j, (1 << N)) { rep(k, N) { if (a[i][k] == 0) continue; if ((j >> k) & 1) continue; dp[i + 1][j | (1 << k)] += dp[i][j]; } } } cout << dp[N][(1 << N) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define LLINF 1000000000000000ll #define sz(x) ((int)(x).size()) #define fi first #define sec second #define all(x) (x).begin(), (x).end() #define sq(x) ((x) * (x)) #define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++) #define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++) #define EQ(a, b) (abs((a) - (b)) < eps) #define dmp(x) cerr << __LINE__ << " " << #x << " " << x << endl; template <class T> void chmin(T &a, const T &b) { if (a > b) a = b; } template <class T> void chmax(T &a, const T &b) { if (a < b) a = b; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) { os << p.fi << ',' << p.sec; return os; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.fi >> p.sec; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0; i < vec.size(); i++) { os << vec[i]; if (i + 1 < vec.size()) os << ' '; } return os; } template <class T> istream &operator>>(istream &is, vector<T> &vec) { for (int i = 0; i < vec.size(); i++) is >> vec[i]; return is; } void fastio() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); } ll MOD = 1000000007ll; // if inv is needed, this shold be prime. struct ModInt { ll val; ModInt() : val(0ll) {} ModInt(ll v) : val(((v % MOD) + MOD) % MOD) {} ModInt exp(ll y) const { if (!y) return ModInt(1ll); ModInt a = exp(y / 2ll); a *= a; if (y & 1) a *= (*this); return a; } bool operator==(const ModInt &x) const { return val == x.val; } inline bool operator!=(const ModInt &x) const { return !(*this == x); } bool operator<(const ModInt &x) const { return val < x.val; } bool operator>(const ModInt &x) const { return val > x.val; } inline bool operator>=(const ModInt &x) const { return !(*this < x); } inline bool operator<=(const ModInt &x) const { return !(*this > x); } ModInt &operator+=(const ModInt &x) { if ((val += x.val) >= MOD) val -= MOD; return *this; } ModInt &operator-=(const ModInt &x) { if ((val += MOD - x.val) >= MOD) val -= MOD; return *this; } ModInt &operator*=(const ModInt &x) { (val *= x.val) %= MOD; return *this; } ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; } ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; } ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; } }; istream &operator>>(istream &i, ModInt &x) { i >> x.val; return i; } ostream &operator<<(ostream &o, const ModInt &x) { o << x.val; return o; } ModInt pow(ModInt a, ll x) { ModInt res = ModInt(1ll); while (x) { if (x & 1) res *= a; x >>= 1; a = a * a; } return res; } const int SIZE = 100100; ModInt inv[SIZE + 10], fac[SIZE + 10], facinv[SIZE + 10]; // notice: 0C0 = 1 ModInt nCr(int n, int r) { assert(!(n < r)); assert(!(n < 0 || r < 0)); return fac[n] * facinv[r] * facinv[n - r]; } void init() { fac[0] = ModInt(1ll); for (int i = 1; i <= SIZE; i++) fac[i] = fac[i - 1] * ModInt(i); inv[1] = ModInt(1ll); for (int i = 2; i <= SIZE; i++) inv[i] = ModInt(0ll) - ModInt(MOD / i) * inv[MOD % i]; facinv[0] = ModInt(1ll); for (int i = 1; i <= SIZE; i++) facinv[i] = facinv[i - 1] * inv[i]; return; } int N; int a[30][30]; ModInt dp[24][1 << 22]; int main() { cin >> N; rep(i, N) { rep(j, N) { cin >> a[i][j]; } } dp[0][0] = ModInt(1ll); rep(i, N) { rep(j, (1 << N)) { if (__builtin_popcount(j) != i) continue; rep(k, N) { if (a[i][k] == 0) continue; if ((j >> k) & 1) continue; dp[i + 1][j | (1 << k)] += dp[i][j]; } } } cout << dp[N][(1 << N) - 1] << endl; return 0; }
insert
141
141
141
143
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define fore(i, a) for (auto &i : a) #define all(x) (x).begin(), (x).end() // #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } //------------------------------------------------------------------------------ template <int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) {} ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) { st << a.get(); return st; }; template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } typedef ModInt<1000000007> mint; //------------------------------------------------------------------------------ int N; int a[21][21]; mint dp[21][1 << 21]; void _main() { int N; cin >> N; rep(i, 0, N) rep(j, 0, N) cin >> a[i][j]; dp[0][0] = 1; rep(i, 0, N) { rep(mask, 0, 1 << N) { if (i != __builtin_popcount(mask)) continue; rep(j, 0, N) { if (!(mask & (1 << j)) && a[i][j]) { dp[i + 1][mask | (1 << j)] += dp[i][mask]; } } } } cout << dp[N][(1 << N) - 1] << endl; }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define fore(i, a) for (auto &i : a) #define all(x) (x).begin(), (x).end() // #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } //------------------------------------------------------------------------------ template <int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) {} ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; template <int MOD> ostream &operator<<(ostream &st, const ModInt<MOD> a) { st << a.get(); return st; }; template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } typedef ModInt<1000000007> mint; //------------------------------------------------------------------------------ int N; int a[21][21]; mint dp[22][1 << 21]; void _main() { int N; cin >> N; rep(i, 0, N) rep(j, 0, N) cin >> a[i][j]; dp[0][0] = 1; rep(i, 0, N) { rep(mask, 0, 1 << N) { if (i != __builtin_popcount(mask)) continue; rep(j, 0, N) { if (!(mask & (1 << j)) && a[i][j]) { dp[i + 1][mask | (1 << j)] += dp[i][mask]; } } } } cout << dp[N][(1 << N) - 1] << endl; }
replace
95
96
95
96
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) \ for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) \ for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; 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; } const ll INF = 1e9; const ll MOD = 1e9 + 7; const ll LINF = 1e18; ll dp[25][2101001]; signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vvll a(n, vll(n)); rep(i, n) rep(j, n) cin >> a[i][j]; dp[0][0] = 1; rep(i, n) { rep(s, 1 << n) if (dp[s]) { rep(j, n) if ((s >> j & 1) == 0) { if (a[i][j]) (dp[i + 1][s | (1 << j)] += dp[i][s]) %= MOD; } } } ll ans = dp[n][(1 << n) - 1]; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) \ for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) \ for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; 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; } const ll INF = 1e9; const ll MOD = 1e9 + 7; const ll LINF = 1e18; ll dp[25][2101001]; signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vvll a(n, vll(n)); rep(i, n) rep(j, n) cin >> a[i][j]; dp[0][0] = 1; rep(i, n) { rep(s, 1 << n) if (dp[i][s]) { rep(j, n) if ((s >> j & 1) == 0) { if (a[i][j]) (dp[i + 1][s | (1 << j)] += dp[i][s]) %= MOD; } } } ll ans = dp[n][(1 << n) - 1]; cout << ans << endl; }
replace
70
71
70
71
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define all(c) (c).begin(), (c).end() #define pb push_back #define dbg(...) \ do { \ cerr << __LINE__ << ": "; \ dbgprint(#__VA_ARGS__, __VA_ARGS__); \ } while (0); using namespace std; namespace std { template <class S, class T> struct hash<pair<S, T>> { size_t operator()(const pair<S, T> &p) const { return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second); } }; template <class T> struct hash<vector<T>> { size_t operator()(const vector<T> &v) const { size_t h = 0; for (auto i : v) h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1; return h; } }; } // namespace std template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", "); return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { os << "{ "; for (const auto &i : v) os << i << ", "; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const map<T, U> &v) { os << "{"; for (const auto &i : v) os << " " << i.first << ": " << i.second << ","; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.first << ", " << p.second << ")"; } void dbgprint(const string &fmt) { cerr << endl; } template <class H, class... T> void dbgprint(const string &fmt, const H &h, const T &...r) { cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " "; dbgprint(fmt.substr(fmt.find(",") + 1), r...); } typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; int main() { cin.tie(0); cin.sync_with_stdio(0); const int mod = 1e9 + 7; int n; cin >> n; vector<vi> a(n, vi(n)); rep(i, n) rep(j, n) cin >> a[i][j]; vi dp(1 << n); dp[(1 << n) - 1] = 1; rep(k, n) rep(i, 1 << n) rep(j, n) if ((i >> j & 1) && a[k][j]) { dp[i ^ 1 << j] += dp[i]; if (dp[i ^ 1 << j] >= mod) dp[i ^ 1 << j] -= mod; } cout << dp[0] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define all(c) (c).begin(), (c).end() #define pb push_back #define dbg(...) \ do { \ cerr << __LINE__ << ": "; \ dbgprint(#__VA_ARGS__, __VA_ARGS__); \ } while (0); using namespace std; namespace std { template <class S, class T> struct hash<pair<S, T>> { size_t operator()(const pair<S, T> &p) const { return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second); } }; template <class T> struct hash<vector<T>> { size_t operator()(const vector<T> &v) const { size_t h = 0; for (auto i : v) h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1; return h; } }; } // namespace std template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", "); return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { os << "{ "; for (const auto &i : v) os << i << ", "; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const map<T, U> &v) { os << "{"; for (const auto &i : v) os << " " << i.first << ": " << i.second << ","; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.first << ", " << p.second << ")"; } void dbgprint(const string &fmt) { cerr << endl; } template <class H, class... T> void dbgprint(const string &fmt, const H &h, const T &...r) { cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " "; dbgprint(fmt.substr(fmt.find(",") + 1), r...); } typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; int main() { cin.tie(0); cin.sync_with_stdio(0); const int mod = 1e9 + 7; int n; cin >> n; vector<vi> a(n, vi(n)); rep(i, n) rep(j, n) cin >> a[i][j]; vi dp(1 << n); dp[(1 << n) - 1] = 1; rep(k, n) rep(i, 1 << n) if (dp[i]) rep(j, n) if ((i >> j & 1) && a[k][j]) { dp[i ^ 1 << j] += dp[i]; if (dp[i ^ 1 << j] >= mod) dp[i ^ 1 << j] -= mod; } cout << dp[0] << endl; return 0; }
replace
72
73
72
73
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define maxn 23 #define maxm (1 << 21) + 5 #define mod 1000000007 int dp[maxm] = {}; int main() { int n; int a[maxn][maxn]; scanf("%d", &n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &a[i][j]); int M = (1 << n) - 1; dp[0] = 1; for (int i = 0; i < n; i++) for (int j = M; j >= 0; j--) for (int k = 0; k < n; k++) if (j & (1 << k) && a[i][k]) dp[j] = (dp[j] + dp[j - (1 << k)]) % mod; printf("%d\n", dp[M]); return 0; }
#include <bits/stdc++.h> using namespace std; #define maxn 23 #define maxm (1 << 21) + 5 #define mod 1000000007 int dp[maxm] = {}; int main() { int n; int a[maxn][maxn]; scanf("%d", &n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &a[i][j]); int M = (1 << n) - 1; dp[0] = 1; /*for(int i=0; i<n; i++) for(int j=M; j>=0; j--) for(int k=0; k<n; k++) if(j&(1<<k) && a[i][k]) dp[j] = (dp[j] + dp[j-(1<<k)]) % mod;*/ for (int j = 0; j <= M; j++) { int i = __builtin_popcount(j) - 1; for (int k = 0; k < n; k++) if (j & (1 << k) && a[i][k]) dp[j] = (dp[j] + dp[j - (1 << k)]) % mod; } printf("%d\n", dp[M]); return 0; }
replace
15
20
15
26
TLE
p03174
C++
Time Limit Exceeded
// @idea: // dp(i, mask) : #ways of matching man i with women bitmask, mask // given that [0, i-1] men already matched // dp(i, mask) = sum {dp(i - 1, mask.clear(j)) | j <- mask.ones, a[i][j] == 1} #include <bits/stdc++.h> using namespace std; using vi = vector<int>; const int NMAX = 21 + 2; const int MOD = 1e9 + 7; int n, a[NMAX][NMAX]; int solve(void) { vector<vi> dp(1LL << n, vi(n + 1, 0)); dp[0][0] = 1; for (int mask = 1; mask < (1LL << n); ++mask) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (((mask >> (j - 1)) & 1LL) and a[i - 1][j - 1]) { dp[mask][i] = (dp[mask][i] + dp[mask ^ (1LL << (j - 1))][i - 1]) % MOD; } } } } return dp[(1 << n) - 1][n]; } int main(void) { 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]; cout << solve() << endl; return 0; }
// @idea: // dp(i, mask) : #ways of matching man i with women bitmask, mask // given that [0, i-1] men already matched // dp(i, mask) = sum {dp(i - 1, mask.clear(j)) | j <- mask.ones, a[i][j] == 1} #include <bits/stdc++.h> using namespace std; using vi = vector<int>; const int NMAX = 21 + 2; const int MOD = 1e9 + 7; int n, a[NMAX][NMAX]; int solve(void) { vector<vi> dp(1LL << n, vi(n + 1, 0)); dp[0][0] = 1; for (int mask = 1; mask < (1LL << n); ++mask) { for (int i = 1; i <= n; ++i) { if (__builtin_popcount(mask) != i) continue; for (int j = 1; j <= n; ++j) { if (((mask >> (j - 1)) & 1LL) and a[i - 1][j - 1]) { dp[mask][i] = (dp[mask][i] + dp[mask ^ (1LL << (j - 1))][i - 1]) % MOD; } } } } return dp[(1 << n) - 1][n]; } int main(void) { 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]; cout << solve() << endl; return 0; }
insert
20
20
20
22
TLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; const int maxn = 22; const int mo = 1e9 + 7; int n, a[maxn][maxn], f[maxn][1 << maxn]; int countOne(int cur) { int res = 0; while (cur > 0) { if (cur & 1) ++res; cur >>= 1; } return res; } 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 lim = (1 << n) - 1; f[0][0] = 1; for (int i = 1; i <= n; ++i) for (int j = (1 << i) - 1; j <= lim; ++j) // if (countOne(j) == i) { for (int k = 0; k < n; ++k) if (a[i - 1][k] && (j & (1 << k))) { f[i][j] += f[i - 1][j - (1 << k)]; if (f[i][j] > mo) f[i][j] -= mo; } // } cout << f[n][lim] << endl; return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; const int maxn = 22; const int mo = 1e9 + 7; int n, a[maxn][maxn], f[maxn][1 << maxn]; int countOne(int cur) { int res = 0; while (cur > 0) { if (cur & 1) ++res; cur >>= 1; } return res; } 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 lim = (1 << n) - 1; f[0][0] = 1; for (int i = 1; i <= n; ++i) for (int j = (1 << i) - 1; j <= lim; ++j) if (countOne(j) == i) { for (int k = 0; k < n; ++k) if ((j & (1 << k)) && a[i - 1][k]) { f[i][j] += f[i - 1][j - (1 << k)]; if (f[i][j] >= mo) f[i][j] -= mo; } } cout << f[n][lim] << endl; return 0; }
replace
46
54
46
54
TLE
p03174
C++
Memory Limit Exceeded
#include <cstring> #include <iostream> using namespace std; const int MAXN = 21; // maximum number of couples const int MAXL = 1 << 21 + 3; // maximum number of couples const int BIG_NUM = 1000000007; int N; // number of couples int dp[MAXN][MAXL]; // dp lookup table int comp[MAXN]; // compatibility int solve(int currI, int usedJ) { if (currI == N) return (__builtin_popcount(usedJ) == N); // if all are used if (dp[currI][usedJ] != -1) return dp[currI][usedJ]; int total = 0; for (int j = 0; j < N; j++) if (!(usedJ & (1 << j)) && comp[currI] & (1 << j)) total = (total + solve(currI + 1, usedJ | (1 << j))) % BIG_NUM; return dp[currI][usedJ] = total; } int main() { cin >> N; memset(dp, -1, sizeof(dp)); memset(comp, 0, sizeof(comp)); int temp; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { cin >> temp; comp[i] |= temp << j; } cout << solve(0, 0) << endl; }
#include <cstring> #include <iostream> using namespace std; const int MAXN = 21; // maximum number of couples const int MAXL = 1 << 21 + 2; // maximum number of couples const int BIG_NUM = 1000000007; int N; // number of couples int dp[MAXN][MAXL]; // dp lookup table int comp[MAXN]; // compatibility int solve(int currI, int usedJ) { if (currI == N) return (__builtin_popcount(usedJ) == N); // if all are used if (dp[currI][usedJ] != -1) return dp[currI][usedJ]; int total = 0; for (int j = 0; j < N; j++) if (!(usedJ & (1 << j)) && comp[currI] & (1 << j)) total = (total + solve(currI + 1, usedJ | (1 << j))) % BIG_NUM; return dp[currI][usedJ] = total; } int main() { cin >> N; memset(dp, -1, sizeof(dp)); memset(comp, 0, sizeof(comp)); int temp; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { cin >> temp; comp[i] |= temp << j; } cout << solve(0, 0) << endl; }
replace
5
6
5
6
MLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #define rep(i, m, n) for (int i = int(m); i < int(n); i++) #define all(c) begin(c), end(c) template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } // 改造 typedef long long int ll; using namespace std; #define INF (1 << 30) - 1 #define INFl (ll)5e15 #define DEBUG 0 // デバッグする時1にしてね #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 // ここから編集する class Solve { public: void input() {} void solve() { input(); int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); rep(i, 0, N) rep(j, 0, N) cin >> a[i][j]; vector<vector<ll>> dp(N, vector<ll>(1 << N)); // 男性0の組み合わせ for (int j = 0; j < N; ++j) { if (a[0][j]) { dp[0][1 << j] = 1; } } // 男性1~N-1 for (int i = 0; i + 1 < N; ++i) { for (int bit = 0; bit < 1 << N; ++bit) { for (int j = 0; j < N; ++j) { if (!(bit >> j & 1) && a[i + 1][j]) { (dp[i + 1][bit | (1 << j)] += dp[i][bit]) %= MOD; } } } } ll ans = dp[N - 1][(1 << N) - 1]; cout << ans << endl; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); Solve().solve(); return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #define rep(i, m, n) for (int i = int(m); i < int(n); i++) #define all(c) begin(c), end(c) template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } // 改造 typedef long long int ll; using namespace std; #define INF (1 << 30) - 1 #define INFl (ll)5e15 #define DEBUG 0 // デバッグする時1にしてね #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 // ここから編集する class Solve { public: void input() {} void solve() { input(); int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); rep(i, 0, N) rep(j, 0, N) cin >> a[i][j]; vector<vector<ll>> dp(N, vector<ll>(1 << N)); // 男性0の組み合わせ for (int j = 0; j < N; ++j) { if (a[0][j]) { dp[0][1 << j] = 1; } } // 男性1~N-1 for (int i = 0; i + 1 < N; ++i) { for (int bit = 0; bit < 1 << N; ++bit) { int cnt = 0; for (int j = 0; j < N; ++j) { if (bit >> j & 1) cnt++; } if (i + 1 != cnt) continue; for (int j = 0; j < N; ++j) { if (!(bit >> j & 1) && a[i + 1][j]) { (dp[i + 1][bit | (1 << j)] += dp[i][bit]) %= MOD; } } } } ll ans = dp[N - 1][(1 << N) - 1]; cout << ans << endl; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); Solve().solve(); return 0; }
insert
79
79
79
88
TLE
p03174
C++
Time Limit Exceeded
// S.G.N.// #include <bits/stdc++.h> using namespace std; typedef vector<long long> vi; typedef long long ll; #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define endl "\n" #define rep(i, a, b) for (ll i = a; i < b; i++) #define fr(n) for (ll i = 0; i < n; i++) #define tr(a) for (auto it = a.begin(); it != a.end(); it++) #define N 998244353445 #define PI 3.1415926535897932384 #define F first #define S second #define mp make_pair #define FAST \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define mod 1000000007 void solve() { ll n; cin >> n; ll a[n][n]; fr(n) { rep(j, 0, n) { cin >> a[i][j]; } } ll dp[2][1 << n]; fr(2) { rep(j, 0, 1 << n) { dp[i][j] = 0; } } dp[0][0] = 1; rep(i, 1, n + 1) { rep(j, 0, 1 << n) { dp[i % 2][j] = 0; rep(k, 0, n) { if (j & 1 << k && a[i - 1][k]) { dp[i % 2][j] += dp[(i - 1) % 2][j ^ 1 << k]; dp[i % 2][j] = dp[i % 2][j] % mod; } } } } cout << dp[n % 2][(1 << n) - 1]; } int main() { #ifndef ONLINE_JUDGE *freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FAST ll test; test = 1; // cin>>test; while (test--) { solve(); } return 0; }
// S.G.N.// #include <bits/stdc++.h> using namespace std; typedef vector<long long> vi; typedef long long ll; #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define endl "\n" #define rep(i, a, b) for (ll i = a; i < b; i++) #define fr(n) for (ll i = 0; i < n; i++) #define tr(a) for (auto it = a.begin(); it != a.end(); it++) #define N 998244353445 #define PI 3.1415926535897932384 #define F first #define S second #define mp make_pair #define FAST \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define mod 1000000007 void solve() { ll n; cin >> n; ll a[n][n]; fr(n) { rep(j, 0, n) { cin >> a[i][j]; } } ll dp[2][1 << n]; fr(2) { rep(j, 0, 1 << n) { dp[i][j] = 0; } } dp[0][0] = 1; rep(i, 1, n + 1) { rep(j, 0, 1 << n) { dp[i % 2][j] = 0; if (__builtin_popcount(j) != i) continue; rep(k, 0, n) { if (j & 1 << k && a[i - 1][k]) { dp[i % 2][j] += dp[(i - 1) % 2][j ^ 1 << k]; dp[i % 2][j] = dp[i % 2][j] % mod; } } } } cout << dp[n % 2][(1 << n) - 1]; } int main() { #ifndef ONLINE_JUDGE *freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FAST ll test; test = 1; // cin>>test; while (test--) { solve(); } return 0; }
insert
39
39
39
41
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); i++) #define REP2(i, m, n) for (int i = m; i < (n); i++) typedef long long ll; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; const ll MOD = 1000000007; int N; int A[22][22]; ll dp[21][1 << 21]; vector<int> masks[21]; void solve() { cin >> N; REP(i, N) REP(j, N) cin >> A[i][j]; dp[0][0] = 1; REP(mask, 1 << N) masks[__builtin_popcount(mask)].push_back(mask); REP(i, N) for (auto mask : masks[i]) { REP(j, N) { if (!A[i][j] || (mask & (1 << j))) continue; (dp[i + 1][mask | (1 << j)] += dp[i][mask]) %= MOD; } } cout << dp[N][(1 << N) - 1] << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); i++) #define REP2(i, m, n) for (int i = m; i < (n); i++) typedef long long ll; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; const ll MOD = 1000000007; int N; int A[25][25]; ll dp[23][1 << 21]; vector<int> masks[25]; void solve() { cin >> N; REP(i, N) REP(j, N) cin >> A[i][j]; dp[0][0] = 1; REP(mask, 1 << N) masks[__builtin_popcount(mask)].push_back(mask); REP(i, N) for (auto mask : masks[i]) { REP(j, N) { if (!A[i][j] || (mask & (1 << j))) continue; (dp[i + 1][mask | (1 << j)] += dp[i][mask]) %= MOD; } } cout << dp[N][(1 << N) - 1] << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
replace
12
15
12
15
-11
p03174
C++
Time Limit Exceeded
/* これを入れて実行 g++ code.cpp ./a.out */ #include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef long double ld; int dy4[4] = {-1, 0, +1, 0}; int dx4[4] = {0, +1, 0, -1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long INF = 1LL << 60; const ll MOD = 1e9 + 7; bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) { return f.second > s.second; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll nCr(ll n, ll r) { if (r == 0 || r == n) { return 1; } else if (r == 1) { return n; } return (nCr(n - 1, r) + nCr(n - 1, r - 1)); } ll nPr(ll n, ll r) { r = n - r; ll ret = 1; for (ll i = n; i >= r + 1; i--) ret *= i; return ret; } //-----------------------ここから----------- int n; vector<vector<int>> a; ll dp[23][(1LL << 23LL)]; int main(void) { cin >> n; a.resize(n); for (int i = 0; i < n; i++) { a[i].resize(n); 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++) { for (int j = 0; j < n; j++) { if (!((1 << j) & bit) && a[i][j]) { dp[i + 1][bit | (1 << j)] += dp[i][bit]; dp[i + 1][bit | (1 << j)] %= MOD; } } } } cout << dp[n][(1 << n) - 1] % MOD << endl; }
/* これを入れて実行 g++ code.cpp ./a.out */ #include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef long double ld; int dy4[4] = {-1, 0, +1, 0}; int dx4[4] = {0, +1, 0, -1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long INF = 1LL << 60; const ll MOD = 1e9 + 7; bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) { return f.second > s.second; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll nCr(ll n, ll r) { if (r == 0 || r == n) { return 1; } else if (r == 1) { return n; } return (nCr(n - 1, r) + nCr(n - 1, r - 1)); } ll nPr(ll n, ll r) { r = n - r; ll ret = 1; for (ll i = n; i >= r + 1; i--) ret *= i; return ret; } //-----------------------ここから----------- int n; vector<vector<int>> a; ll dp[23][(1LL << 23LL)]; int main(void) { cin >> n; a.resize(n); for (int i = 0; i < n; i++) { a[i].resize(n); 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 (dp[i][bit] == 0) continue; for (int j = 0; j < n; j++) { if (!((1 << j) & bit) && a[i][j]) { dp[i + 1][bit | (1 << j)] += dp[i][bit]; dp[i + 1][bit | (1 << j)] %= MOD; } } } } cout << dp[n][(1 << n) - 1] % MOD << endl; }
insert
83
83
83
85
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <unordered_map> using namespace std; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define show(x) cerr << (#x) << " : " << x << endl; #define ll long long #define ld long double #define fill(a, val) memset(a, val, sizeof(a)) #define mp make_pair #define ff first #define ss second #define pii pair<int, int> #define pll pair<ll, ll> #define sq(x) ((x) * (x)) #define all(v) v.begin(), v.end() #define endl "\n" #define fo(i, n) for (int i = 0; i < n; i++) #define foo(i, x, n) for (int i = x; i < n; i++) #define vi vector<int> #define vl vector<ll> #define vii vector<pii> #define vll vector<pll> #define w(x) \ int x; \ cin >> x; \ while (x--) #define display(a, n) \ { \ fo(i, n) { cout << a[i] << " "; } \ cout << endl; \ } #define fil(a, n, val) \ { fo(i, n) a[i] = val; } #define sp(x, y) cout << fixed << setprecision(y) << x << endl; const ll MOD = 1000 * 1000 * 1000 + 7; const ll MOD2 = 998244353; const ll N = 1000 * 1000 + 5; const double PI = 3.14159265; #define printclock \ cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n"; ll search(vl arr, ll l, ll r, ll x) { if (r >= l) { ll mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return search(arr, l, mid - 1, x); return search(arr, mid + 1, r, x); } return -1; } ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } ll modInverse(ll n, ll p) { return power(n, p - 2, p); } const int maxn = 1LL << 22; int n; int a[25][25]; ll dp[25][maxn]; ll solve(ll row, ll mask) { if (row == n) { if (mask == 0) return 1; return 0; } if (dp[row][mask] != -1) return dp[row][mask]; ll ans = 0; fo(i, n) { if (a[row][i] == 0) continue; ans = (ans + solve(row + 1, mask & (~(1LL << i)))) % MOD; } return dp[row][mask] = ans; } int main() { fastio(); cin >> n; fo(i, n) { fo(j, n) { cin >> a[i][j]; } } fill(dp, -1); ll mask = (1LL << n) - 1; cout << solve(0, mask) << endl; }
#include <bits/stdc++.h> #include <unordered_map> using namespace std; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define show(x) cerr << (#x) << " : " << x << endl; #define ll long long #define ld long double #define fill(a, val) memset(a, val, sizeof(a)) #define mp make_pair #define ff first #define ss second #define pii pair<int, int> #define pll pair<ll, ll> #define sq(x) ((x) * (x)) #define all(v) v.begin(), v.end() #define endl "\n" #define fo(i, n) for (int i = 0; i < n; i++) #define foo(i, x, n) for (int i = x; i < n; i++) #define vi vector<int> #define vl vector<ll> #define vii vector<pii> #define vll vector<pll> #define w(x) \ int x; \ cin >> x; \ while (x--) #define display(a, n) \ { \ fo(i, n) { cout << a[i] << " "; } \ cout << endl; \ } #define fil(a, n, val) \ { fo(i, n) a[i] = val; } #define sp(x, y) cout << fixed << setprecision(y) << x << endl; const ll MOD = 1000 * 1000 * 1000 + 7; const ll MOD2 = 998244353; const ll N = 1000 * 1000 + 5; const double PI = 3.14159265; #define printclock \ cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n"; ll search(vl arr, ll l, ll r, ll x) { if (r >= l) { ll mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return search(arr, l, mid - 1, x); return search(arr, mid + 1, r, x); } return -1; } ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } ll modInverse(ll n, ll p) { return power(n, p - 2, p); } const int maxn = 1LL << 22; int n; int a[25][25]; ll dp[25][maxn]; ll solve(ll row, ll mask) { if (row == n) { if (mask == 0) return 1; return 0; } if (dp[row][mask] != -1) return dp[row][mask]; ll ans = 0; fo(i, n) { if (a[row][i] == 0) continue; if ((mask & (1LL << i)) != 0) ans = (ans + solve(row + 1, mask & (~(1LL << i)))) % MOD; } return dp[row][mask] = ans; } int main() { fastio(); cin >> n; fo(i, n) { fo(j, n) { cin >> a[i][j]; } } fill(dp, -1); ll mask = (1LL << n) - 1; cout << solve(0, mask) << endl; }
replace
92
93
92
94
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #define debarr(a, x, y) \ cerr << #a << ":"; \ for (int i = x; i <= y; i++) \ cerr << a[i] << " "; \ cerr << endl; #define debmat(mat, row, col) \ cerr << #mat << ":\n"; \ for (int i = 0; i < row; i++) { \ for (int j = 0; j < col; j++) \ cerr << mat[i][j] << " "; \ cerr << endl; \ } #define deb(...) dbs(#__VA_ARGS__, __VA_ARGS__) template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << "(" << p.first << "," << p.second << ")"; } template <class T> ostream &operator<<(ostream &os, const vector<T> &p) { os << "["; for (auto &it : p) os << it << " "; return os << "]"; } template <class T> ostream &operator<<(ostream &os, const set<T> &p) { os << "["; for (auto &it : p) os << it << " "; return os << "]"; } template <class T> ostream &operator<<(ostream &os, const multiset<T> &p) { os << "["; for (auto &it : p) os << it << " "; return os << "]"; } template <class S, class T> ostream &operator<<(ostream &os, const map<S, T> &p) { os << "["; for (auto &it : p) os << it << " "; return os << "]"; } template <class T> void dbs(string str, T t) { cerr << str << ":" << t << "\n"; } template <class T, class... S> void dbs(string str, T t, S... s) { int idx = str.find(','); cerr << str.substr(0, idx) << ":" << t << ","; dbs(str.substr(idx + 1), s...); } #else #define deb(...) \ {} #define debarr(a, x, y) \ {} #define debmat(mat, row, col) \ {} #endif #define int long long #define fi first #define se second #define mp make_pair #define pb push_back #define rr return #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define ini(x, y) memset(x, y, sizeof(x)) #define rep(i, n) for (int i = 0; i < n; i++) #define fr(i, a, b) for (int i = a; i <= b; i++) #define fb(i, a, b) for (int i = a; i >= b; i--) typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vii; const int inf = 4e18; const int N = 2e5 + 10; const int mod = 1e9 + 7; int n; int a[25][25]; int dp[22][1 << 22]; int go(int idx, int mask) { if (idx == n) return (mask == 0); if (dp[idx][mask] != -1) return dp[idx][mask]; int ret = 0; for (int i = 0; i < n; i++) { if (a[idx][i] && (mask & (1 << i))) { ret = (ret + go(idx + 1, mask ^ (1 << i))) % mod; } } return dp[idx][mask] = ret; } void solve() { cin >> n; rep(i, n) rep(j, n) cin >> a[i][j]; ini(dp, -1); cout << go(0, (1 << n) - 1); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #define debarr(a, x, y) \ cerr << #a << ":"; \ for (int i = x; i <= y; i++) \ cerr << a[i] << " "; \ cerr << endl; #define debmat(mat, row, col) \ cerr << #mat << ":\n"; \ for (int i = 0; i < row; i++) { \ for (int j = 0; j < col; j++) \ cerr << mat[i][j] << " "; \ cerr << endl; \ } #define deb(...) dbs(#__VA_ARGS__, __VA_ARGS__) template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << "(" << p.first << "," << p.second << ")"; } template <class T> ostream &operator<<(ostream &os, const vector<T> &p) { os << "["; for (auto &it : p) os << it << " "; return os << "]"; } template <class T> ostream &operator<<(ostream &os, const set<T> &p) { os << "["; for (auto &it : p) os << it << " "; return os << "]"; } template <class T> ostream &operator<<(ostream &os, const multiset<T> &p) { os << "["; for (auto &it : p) os << it << " "; return os << "]"; } template <class S, class T> ostream &operator<<(ostream &os, const map<S, T> &p) { os << "["; for (auto &it : p) os << it << " "; return os << "]"; } template <class T> void dbs(string str, T t) { cerr << str << ":" << t << "\n"; } template <class T, class... S> void dbs(string str, T t, S... s) { int idx = str.find(','); cerr << str.substr(0, idx) << ":" << t << ","; dbs(str.substr(idx + 1), s...); } #else #define deb(...) \ {} #define debarr(a, x, y) \ {} #define debmat(mat, row, col) \ {} #endif #define int long long #define fi first #define se second #define mp make_pair #define pb push_back #define rr return #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define ini(x, y) memset(x, y, sizeof(x)) #define rep(i, n) for (int i = 0; i < n; i++) #define fr(i, a, b) for (int i = a; i <= b; i++) #define fb(i, a, b) for (int i = a; i >= b; i--) typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vii; const int inf = 4e18; const int N = 2e5 + 10; const int mod = 1e9 + 7; int n; int a[25][25]; int dp[22][1 << 22]; int go(int idx, int mask) { if (idx == n) return (mask == 0); if (dp[idx][mask] != -1) return dp[idx][mask]; int ret = 0; for (int i = 0; i < n; i++) { if (a[idx][i] && (mask & (1 << i))) { ret = (ret + go(idx + 1, mask ^ (1 << i))) % mod; } } return dp[idx][mask] = ret; } void solve() { cin >> n; rep(i, n) rep(j, n) cin >> a[i][j]; ini(dp, -1); cout << go(0, (1 << n) - 1); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
replace
112
116
112
113
-11
p03174
C++
Time Limit Exceeded
// https://atcoder.jp/contests/dp/tasks/dp_o #include <bits/stdc++.h> #define ALL(e) e.begin(), e.end() #define RALL(e) e.rbegin(), e.rend() #define mp(a, b) make_pair(a, b) #define pb push_back #define dbg(x) (cerr << #x << ":" << x) #define mid (l + r) / 2 #define fi first #define sc second #define N 1000000009 using namespace std; typedef long long int lli; void mod(lli &a) { if (a > 1e9 + 7) a -= (1e9 + 7); } int main() { ios::sync_with_stdio(0); int n; cin >> n; vector<vector<int>> ar(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> ar[i][j]; lli mask = 1 << n; vector<lli> dp(mask + 5, 0); dp[0] = 1; for (int i = 0; i < mask; i++) { int a = __builtin_popcount(i); // cerr << a << endl; for (int j = 0; j < n; j++) { if (ar[a][j] and !(i & (1 << j))) { cerr << a << " " << j << endl; mod(dp[i]); dp[i | (1 << j)] += dp[i]; mod(dp[i | (1 << j)]); } } } cout << dp[mask - 1] << endl; return 0; }
// https://atcoder.jp/contests/dp/tasks/dp_o #include <bits/stdc++.h> #define ALL(e) e.begin(), e.end() #define RALL(e) e.rbegin(), e.rend() #define mp(a, b) make_pair(a, b) #define pb push_back #define dbg(x) (cerr << #x << ":" << x) #define mid (l + r) / 2 #define fi first #define sc second #define N 1000000009 using namespace std; typedef long long int lli; void mod(lli &a) { if (a > 1e9 + 7) a -= (1e9 + 7); } int main() { ios::sync_with_stdio(0); int n; cin >> n; vector<vector<int>> ar(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> ar[i][j]; lli mask = 1 << n; vector<lli> dp(mask + 5, 0); dp[0] = 1; for (int i = 0; i < mask; i++) { int a = __builtin_popcount(i); // cerr << a << endl; for (int j = 0; j < n; j++) { if (ar[a][j] and !(i & (1 << j))) { mod(dp[i]); dp[i | (1 << j)] += dp[i]; mod(dp[i | (1 << j)]); } } } cout << dp[mask - 1] << endl; return 0; }
delete
42
43
42
42
TLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) const int MOD = 1e+9 + 7; int main() { int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); REP(i, N) REP(j, N) cin >> a[i][j]; // dp[i][j]: 男性は 1-index, 女性は 0 index で状態 // i 番目の男性までマッチングしたとき、 女性 j // もマッチングが終わっている場合の数 vector<vector<long long>> dp(N + 1, vector<long long>(1 << N)); dp[0][0] = 1; REP(i, N) { REP(j, (1 << N)) { REP(k, N) { if (!(j & (1 << k)) and a[i][k] == 1) { 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; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) const int MOD = 1e+9 + 7; int main() { int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); REP(i, N) REP(j, N) cin >> a[i][j]; // dp[i][j]: 男性は 1-index, 女性は 0 index で状態 // i 番目の男性までマッチングしたとき、 女性 j // もマッチングが終わっている場合の数 vector<vector<long long>> dp(N + 1, vector<long long>(1 << N)); dp[0][0] = 1; REP(i, N) { REP(j, (1 << N)) { // 立っている bit はマッチが終わっている if (i != __builtin_popcount(j)) continue; REP(k, N) { if (!(j & (1 << k)) and a[i][k] == 1) { 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; }
replace
22
23
22
25
TLE
p03174
C++
Memory Limit Exceeded
/*There's a possibility*/ #define dbg(...) ; #define db(...) ; #include "bits/stdc++.h" #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL) #define f(i, a, b) for (i = a; i < b; i++) #define fr(i, a, b) for (i = a; i >= b; i--) #define endl '\n' #define tst cout << '*'; #define ll long long int #define ff first #define ss second #define pb push_back #define pf push_front #define mod 1000000007 using namespace std; ll n, ar[23][23], dp[23][1 << 23]; ll fun(int i, ll mask) { // dbg(i,mask); if (dp[i][mask] != -1) return dp[i][mask]; if (i == n) { if (mask == ((1 << n) - 1)) return dp[i][mask] = 1; else return dp[i][mask] = 0; } ll ans = 0, j; f(j, 0, n) { if (ar[i][j + 1] and !(mask & (1 << j))) ans = (ans + fun(i + 1, mask | (1 << j))) % mod; } return dp[i][mask] = ans; } int main() { fast; int i, j; cin >> n; f(i, 0, n) f(j, 1, n + 1) cin >> ar[i][j]; memset(dp, -1, sizeof(dp)); cout << fun(0, 0); // db(dp,0,n,0,3); }
/*There's a possibility*/ #define dbg(...) ; #define db(...) ; #include "bits/stdc++.h" #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL) #define f(i, a, b) for (i = a; i < b; i++) #define fr(i, a, b) for (i = a; i >= b; i--) #define endl '\n' #define tst cout << '*'; #define ll long long int #define ff first #define ss second #define pb push_back #define pf push_front #define mod 1000000007 using namespace std; ll n, ar[22][22], dp[22][1 << 22]; ll fun(int i, ll mask) { // dbg(i,mask); if (dp[i][mask] != -1) return dp[i][mask]; if (i == n) { if (mask == ((1 << n) - 1)) return dp[i][mask] = 1; else return dp[i][mask] = 0; } ll ans = 0, j; f(j, 0, n) { if (ar[i][j + 1] and !(mask & (1 << j))) ans = (ans + fun(i + 1, mask | (1 << j))) % mod; } return dp[i][mask] = ans; } int main() { fast; int i, j; cin >> n; f(i, 0, n) f(j, 1, n + 1) cin >> ar[i][j]; memset(dp, -1, sizeof(dp)); cout << fun(0, 0); // db(dp,0,n,0,3); }
replace
20
21
20
21
MLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define mod 1000000007 #define all(v) v.begin(), v.end() #define pb push_back #define size(v) (int)v.size() #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) ll power_mod(ll a, ll x) { if (x == 0) return 1; ll y = power_mod(a, x / 2); ll ans = (y * y) % mod; if (x % 2) ans = (ans * a) % mod; return ans; } ll inv(ll a) { return power_mod(a, mod - 2); } ll power(ll a, ll x) { if (x == 0) return 1; ll y = power(a, x / 2); ll ans = (y * y); if (x % 2) ans *= a; return ans; } ll dp[(1 << 16) + 3]; int main() { fast; 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<int> dp(1 << n, 0); dp[0] = 1; for (int mask = 0; mask < (1 << n); mask++) { int count_one = __builtin_popcount(mask); for (int j = 0; j < n; j++) { if (a[count_one][j]) { if (!(mask & (1 << j))) { dp[mask + (1 << j)] = (dp[mask + (1 << j)] + dp[mask]) % mod; } } } } cout << dp[(1 << n) - 1] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define mod 1000000007 #define all(v) v.begin(), v.end() #define pb push_back #define size(v) (int)v.size() #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) ll power_mod(ll a, ll x) { if (x == 0) return 1; ll y = power_mod(a, x / 2); ll ans = (y * y) % mod; if (x % 2) ans = (ans * a) % mod; return ans; } ll inv(ll a) { return power_mod(a, mod - 2); } ll power(ll a, ll x) { if (x == 0) return 1; ll y = power(a, x / 2); ll ans = (y * y); if (x % 2) ans *= a; return ans; } ll dp[(1 << 16) + 3]; int main() { fast; 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<int> dp(1 << n, 0); dp[0] = 1; for (int mask = 0; mask < ((1 << n) - 1); mask++) { int count_one = __builtin_popcount(mask); for (int j = 0; j < n; j++) { if (a[count_one][j]) { if (!(mask & (1 << j))) { dp[mask + (1 << j)] = (dp[mask + (1 << j)] + dp[mask]) % mod; } } } } cout << dp[(1 << n) - 1] << endl; }
replace
49
50
49
50
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; #define rep(i, x, y) \ for (i64 i = i64(x), i##_max_for_repmacro = i64(y); \ i < i##_max_for_repmacro; ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1.01e9; const i64 inf64 = 4.01e18; const double eps = 1e-9; template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } void solve() { const i64 mod = 1'000'000'007; i64 N; cin >> N; vector<vector<i64>> a(N, vector<i64>(N)); rep(i, 0, N) { rep(j, 0, N) { cin >> a[i][j]; } } const i64 size = 21; static i64 dp[size][1 << size]; fill_n((i64 *)dp, size * (1 << size), 0); rep(j, 0, N) { if (a[0][j] == 0) continue; ++dp[0][1 << j]; } rep(i, 0, N - 1) { rep(s, 0, 1 << N) { rep(j, 0, N) { if (s & (1 << j)) continue; if (a[i + 1][j] == 0) continue; i64 t = s | (1 << j); dp[i + 1][t] += dp[i][s]; dp[i + 1][t] %= mod; } } } cout << dp[N - 1][(1 << N) - 1] << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; #define rep(i, x, y) \ for (i64 i = i64(x), i##_max_for_repmacro = i64(y); \ i < i##_max_for_repmacro; ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1.01e9; const i64 inf64 = 4.01e18; const double eps = 1e-9; template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } void solve() { const i64 mod = 1'000'000'007; i64 N; cin >> N; vector<vector<i64>> a(N, vector<i64>(N)); rep(i, 0, N) { rep(j, 0, N) { cin >> a[i][j]; } } const i64 size = 21; static i64 dp[size][1 << size]; fill_n((i64 *)dp, size * (1 << size), 0); rep(j, 0, N) { if (a[0][j] == 0) continue; ++dp[0][1 << j]; } rep(i, 0, N - 1) { rep(s, 0, 1 << N) { if (dp[i][s] == 0) continue; rep(j, 0, N) { if (s & (1 << j)) continue; if (a[i + 1][j] == 0) continue; i64 t = s | (1 << j); dp[i + 1][t] += dp[i][s]; dp[i + 1][t] %= mod; } } } cout << dp[N - 1][(1 << N) - 1] << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }
insert
74
74
74
76
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int add(int a, int b) { return a + b - mod * (a + b >= mod); } int mul(int a, int b) { return int(1ll * a * b % mod); } int pwr(int a, int x) { if (x == 0) { return 1; } return mul(pwr(mul(a, a), x >> 1), (x & 1 ? a : 1)); } int dvd(int a, int b) { return mul(a, pwr(b, mod - 2)); } int sub(int a, int b) { return a - b + mod * (a < b); } void inc(int &a, int b) { a = add(a, b); } void dec(int &a, int b) { a = sub(a, b); } const int N = 21; int a[N][N], dp[1 << N]; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; cin >> 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 = 0; i < n; ++i) { for (int mask = (1 << n) - 1; mask >= 0; --mask) { for (int j = 0; j < n; ++j) { if (((mask >> j) & 1) == 0 and a[i][j]) { inc(dp[mask | (1 << j)], dp[mask]); } } } } cout << dp[(1 << n) - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int add(int a, int b) { return a + b - mod * (a + b >= mod); } int mul(int a, int b) { return int(1ll * a * b % mod); } int pwr(int a, int x) { if (x == 0) { return 1; } return mul(pwr(mul(a, a), x >> 1), (x & 1 ? a : 1)); } int dvd(int a, int b) { return mul(a, pwr(b, mod - 2)); } int sub(int a, int b) { return a - b + mod * (a < b); } void inc(int &a, int b) { a = add(a, b); } void dec(int &a, int b) { a = sub(a, b); } const int N = 21; int a[N][N], dp[1 << N]; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } dp[0] = 1; for (int mask = 0; mask < (1 << n); ++mask) { int j = __builtin_popcount(mask); for (int i = 0; i < n; ++i) { if (((mask >> i) & 1) == 0 and a[j][i]) { inc(dp[mask | (1 << i)], dp[mask]); } } } cout << dp[(1 << n) - 1]; return 0; }
replace
40
46
40
45
TLE
p03174
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; int N; int a[23][23]; int A[23]; const ll MOD = 1e9 + 7; ll dp[23][1 << 23]; ll mmax; ll rec(int n, int m) { if (dp[n][m] != -1) return dp[n][m]; if (n == 0) { if (m == mmax) return 1; else return 0; } ll res = 0; for (int i = 0; i < N; i++) { if (A[n - 1] >> i & 1) if (!(m >> i & 1)) res += rec(n - 1, m + (1 << i)) % MOD; } return dp[n][m] = res % MOD; } int main() { cin >> N; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) cin >> a[i][j]; memset(dp, -1, sizeof(dp)); memset(A, 0, sizeof(A)); mmax = (1 << N) - 1; for (int i = N; 0 < i; i--) { for (int j = 0; j < N; j++) A[j] += (1 << N - i) * a[i - 1][j]; } cout << rec(N, 0) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int N; int a[23][23]; int A[23]; const ll MOD = 1e9 + 7; ll dp[23][1 << 22]; ll mmax; ll rec(int n, int m) { if (dp[n][m] != -1) return dp[n][m]; if (n == 0) { if (m == mmax) return 1; else return 0; } ll res = 0; for (int i = 0; i < N; i++) { if (A[n - 1] >> i & 1) if (!(m >> i & 1)) res += rec(n - 1, m + (1 << i)) % MOD; } return dp[n][m] = res % MOD; } int main() { cin >> N; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) cin >> a[i][j]; memset(dp, -1, sizeof(dp)); memset(A, 0, sizeof(A)); mmax = (1 << N) - 1; for (int i = N; 0 < i; i--) { for (int j = 0; j < N; j++) A[j] += (1 << N - i) * a[i - 1][j]; } cout << rec(N, 0) << endl; }
replace
8
9
8
9
MLE
p03174
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long #define LSB __builtin_ctzll #define sc(a) scanf("%d", &a) #define MSB 63 - __builtin_clzll #define scl(a) scanf("%lld", &a) #define BITS __builtin_popcountll #define mem(a, v) memset(a, v, sizeof(a)) #define fastIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); const int MOD = 1e9 + 7; const int MAX = 1e5 + 55; const int INF = 1e9 + 77; const ll INFINF = 1e18 + 1e17; const double PI = acos(-1.0); vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; ll power(ll x, ll y) { if (y == 0) return 1; ll s = power(x, y / 2); s *= s; if (y & 1) s *= x; return s; } int n; bool x[22][22]; ll dp[10000000][22]; ll solve(int mask, int i) { if (i == n) { return 1; } ll &res = dp[mask][i]; if (res != -1) { return res; } res = 0; for (int j = 0; j < n; j++) { int cur = n - 1 - j; if (x[i][j] && !(mask & (1 << cur))) { res += solve(mask + (1 << cur), i + 1) % MOD; res %= MOD; } } return res % MOD; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt" , "r" , stdin); // freopen("output.txt" , "w" , stdout); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> x[i][j]; } } mem(dp, -1); cout << solve(0, 0) % MOD; return 0; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long #define LSB __builtin_ctzll #define sc(a) scanf("%d", &a) #define MSB 63 - __builtin_clzll #define scl(a) scanf("%lld", &a) #define BITS __builtin_popcountll #define mem(a, v) memset(a, v, sizeof(a)) #define fastIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); const int MOD = 1e9 + 7; const int MAX = 1e5 + 55; const int INF = 1e9 + 77; const ll INFINF = 1e18 + 1e17; const double PI = acos(-1.0); vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; ll power(ll x, ll y) { if (y == 0) return 1; ll s = power(x, y / 2); s *= s; if (y & 1) s *= x; return s; } int n; bool x[22][22]; ll dp[3000000][22]; ll solve(int mask, int i) { if (i == n) { return 1; } ll &res = dp[mask][i]; if (res != -1) { return res; } res = 0; for (int j = 0; j < n; j++) { int cur = n - 1 - j; if (x[i][j] && !(mask & (1 << cur))) { res += solve(mask + (1 << cur), i + 1) % MOD; res %= MOD; } } return res % MOD; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt" , "r" , stdin); // freopen("output.txt" , "w" , stdout); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> x[i][j]; } } mem(dp, -1); cout << solve(0, 0) % MOD; return 0; }
replace
35
36
35
36
MLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const int MOD = 1e9 + 7; int N; int a[22][22]; ll dp[22][2200000]; int bitcnt(int n) { int ret = 0; for (int i = 0; i < 22; i++) if (n & (1 << i)) ret++; return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); 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 mask = 0; mask < (1 << N); mask++) { int i = bitcnt(mask); for (int k = 0; k < N; k++) dp[i + 1][mask | (1 << k)] = (dp[i + 1][mask | (1 << k)] + dp[i][mask] * a[i][k]) % MOD; } cout << dp[N][(1 << N) - 1] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const int MOD = 1e9 + 7; int N; int a[22][22]; ll dp[23][2200000]; int bitcnt(int n) { int ret = 0; for (int i = 0; i < 22; i++) if (n & (1 << i)) ret++; return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); 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 mask = 0; mask < (1 << N); mask++) { int i = bitcnt(mask); for (int k = 0; k < N; k++) dp[i + 1][mask | (1 << k)] = (dp[i + 1][mask | (1 << k)] + dp[i][mask] * a[i][k]) % MOD; } cout << dp[N][(1 << N) - 1] << '\n'; return 0; }
replace
8
9
8
9
-11
p03174
C++
Time Limit Exceeded
#include <iostream> #define llint long long #define mod 1000000007 using namespace std; llint n; llint a[25][25]; llint dp[25][1 << 21]; int main(void) { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } llint S = 1 << n; dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < S; j++) { for (int k = 0; k < n; k++) { if (j & (1 << k)) continue; if (a[i + 1][k] == 0) continue; dp[i + 1][j | (1 << k)] += dp[i][j]; dp[i + 1][j | (1 << k)] %= mod; } } } cout << dp[n][S - 1] << endl; return 0; }
#include <iostream> #define llint long long #define mod 1000000007 using namespace std; llint n; llint a[25][25]; llint dp[25][1 << 21]; int main(void) { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } llint S = 1 << n; dp[0][0] = 1; for (int j = 0; j < S; j++) { int i = 0; for (int k = 0; k < n; k++) if (j & (1 << k)) i++; for (int k = 0; k < n; k++) { if (j & (1 << k)) continue; if (a[i + 1][k] == 0) continue; dp[i + 1][j | (1 << k)] += dp[i][j]; dp[i + 1][j | (1 << k)] %= mod; } } cout << dp[n][S - 1] << endl; return 0; }
replace
20
30
20
32
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll #define MOD ll(1e9 + 7) vector<vector<ll>> memo(22, vector<ll>(2e5 + 1e5, -1)); int binpow(int a, int b) { int res = 1; while (b) { if (b % 2) res = res * a; a *= a; b >>= 1; } return res; } ll dp(vector<vector<int>> &v, int mmask, int i) { if (i == v.size()) return 1; if (memo[i][mmask] != -1) return memo[i][mmask]; memo[i][mmask] = 0; for (int e : v[i]) { if (mmask & binpow(2, e)) { memo[i][mmask] = (memo[i][mmask] + dp(v, mmask & (binpow(2, 21) - 1 - binpow(2, e)), i + 1)) % MOD; } } return memo[i][mmask]; } signed main() { cin.tie(0); cin.sync_with_stdio(); int n; cin >> n; vector<vector<int>> v(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a; cin >> a; if (a) v[i].push_back(j); } } cout << dp(v, binpow(2, 21) - 1, 0); }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll #define MOD ll(1e9 + 7) vector<vector<ll>> memo(22, vector<ll>(2e6 + 1e5, -1)); int binpow(int a, int b) { int res = 1; while (b) { if (b % 2) res = res * a; a *= a; b >>= 1; } return res; } ll dp(vector<vector<int>> &v, int mmask, int i) { if (i == v.size()) return 1; if (memo[i][mmask] != -1) return memo[i][mmask]; memo[i][mmask] = 0; for (int e : v[i]) { if (mmask & binpow(2, e)) { memo[i][mmask] = (memo[i][mmask] + dp(v, mmask & (binpow(2, 21) - 1 - binpow(2, e)), i + 1)) % MOD; } } return memo[i][mmask]; } signed main() { cin.tie(0); cin.sync_with_stdio(); int n; cin >> n; vector<vector<int>> v(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a; cin >> a; if (a) v[i].push_back(j); } } cout << dp(v, binpow(2, 21) - 1, 0); }
replace
5
6
5
6
-11
p03174
C++
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]; }
#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]; }
delete
10
14
10
10
-11
p03174
C++
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; }
#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; }
insert
28
28
28
30
TLE
p03174
C++
Time Limit Exceeded
#pragma GCC optimize("-O3") #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <time.h> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <vector> #ifdef _MSC_VER #include <intrin.h> #define popcnt __popcnt64 // # define __builtin_popcount __popcnt #else #define popcnt __builtin_popcountll #endif // #include "boost/variant.hpp" using namespace std; typedef long long ll; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 60; #define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i) #define rep_skip(i, N, M, ...) \ for (ll i = N, i##_len = (M); i < i##_len; i += (skip)) #define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i) #define pb push_back typedef pair<double, double> pd; typedef pair<ll, ll> pll; template <int n> struct tll_impl { using type = decltype(tuple_cat(tuple<ll>(), declval<typename tll_impl<n - 1>::type>())); }; template <> struct tll_impl<1> { using type = tuple<ll>; }; template <int n> using tll = typename tll_impl<n>::type; template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); }; template <int n, typename T> struct vec_t_impl { using type = vector<typename vec_t_impl<n - 1, T>::type>; }; template <typename T> struct vec_t_impl<1, T> { using type = vector<T>; }; template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type; // check static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, ""); // decompose vector into basetype and dimension. template <typename T> struct vec_dec { static constexpr int dim = 0; using type = T; }; template <typename T> struct vec_dec<vector<T>> { static constexpr int dim = vec_dec<T>::dim + 1; using type = typename vec_dec<T>::type; }; static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, ""); static_assert(vec_dec<vec_t<3, ll>>::dim == 3, ""); template <typename T = ll> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T = ll, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } // ex: auto dp = make_v<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5)); // check if T is vector template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<vector<T>> : std::true_type {}; static_assert(is_vector<vector<ll>>::value == true && is_vector<ll>::value == false, ""); template <typename T, typename V, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void fill_v(T &t, const V &v) { t = v; } template <typename T, typename V, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void fill_v(T &t, const V &v) { for (auto &&x : t) fill_v(x, v); } // ex: fill_v(dp, INF); template <typename T, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void read_v(T &x) { cin >> x; } template <typename T, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void read_v(T &x) { rep(i, 0, x.size()) read_v(x[i]); } template <typename T, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void write_v(T &x) { cout << x << " "; } template <typename T, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void write_v(T &x) { rep(i, 0, x.size()) write_v(x[i]); cout << endl; } typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; template <typename T> using pq_greater = priority_queue<T, vector<T>, greater<T>>; #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define perm(c) \ sort(all(c)); \ for (bool c##perm = 1; c##perm; c##perm = next_permutation(all(c))) template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } vll seq(ll i, ll j) { vll res(j - i); rep(k, i, j) res[k] = i + k; return res; } constexpr ll POW_0(ll x, ll y) { if (y == 0) return 1; if (y == 1) return x; if (y == 2) return x * x; if (y % 2 == 0) return POW_0(POW_0(x, y / 2), 2LL); return ((POW_0(POW_0(x, y / 2), 2LL)) * (x)); } constexpr ll POW(ll x, ll y, ll mod = MOD) { if (mod == 0) return POW_0(x, y); if (y == 0) return 1; if (y == 1) return x % mod; if (y == 2) return x * x % mod; if (y % 2 == 0) return POW(POW(x, y / 2, mod), 2LL, mod) % mod; return ((POW(POW(x, y / 2, mod), 2LL, mod)) * (x % mod)) % mod; } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs, Functor f) { std::sort(std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void stable_sort_by(Inputs &inputs, Functor f) { std::stable_sort( std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } ll div_ferm(ll a, ll b, ll mod) { return (a * POW(b, mod - 2, mod)) % mod; } // === Modint === template <std::uint_fast64_t Modulus = 1000000007> class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} // constexpr modint(const modint& rhs) noexcept { // this->a = rhs.value(); // } // constexpr modint &operator=(const modint &rhs) noexcept { // this->a = rhs.value(); // return *this; // } constexpr u64 value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } modint &operator++() noexcept { return *this += modint(1); } modint &operator++(int) noexcept { auto t = *this; *this += modint(1); return t; } modint &operator--() noexcept { return *this -= modint(1); } modint &operator--(int) noexcept { auto t = *this; *this -= modint(1); return t; } constexpr bool operator==(const modint rhs) const noexcept { return a == rhs.value(); } constexpr bool operator!=(const modint rhs) const noexcept { return a != rhs.value(); } constexpr bool operator<(const modint rhs) const noexcept { return a < rhs.value(); } // should be moved to Google Test // constexpr void test(){ // constexpr auto x = modint<5>(3); // constexpr auto y = modint<5>(4); // constexpr auto z = modint<5>(2); // static_assert(x + y == z, ""); // static_assert(x != y, ""); // static_assert(++x == y && x++ != y && x == --y && x != y--, ""); // static_assert(x + 6 == y, ""); // static_assert(x / 2 == y, ""); // //} }; template <uint_fast64_t Modulus> ostream &operator<<(ostream &o, const modint<Modulus> &t) { o << t.value(); return o; } template <uint_fast64_t Modulus> istream &operator>>(istream &in, modint<Modulus> &t) { uint_fast64_t x; in >> x; t = modint<Modulus>(x); return in; } template <uint_fast64_t Modulus> modint<Modulus> POW(modint<Modulus> x, ll n) { return modint<Modulus>(POW(x.value(), n, Modulus)); } // === Mll === template <typename T = ll, T MOD = 1000000007> struct Mint { T v; Mint() : v(0) {} Mint(signed v) : v(v) {} Mint(long long t) { v = t % MOD; if (v < 0) v += MOD; } Mint inv() { return pow(MOD - 2); } Mint &operator+=(Mint a) { v += a.v; if (v >= MOD) v -= MOD; return *this; } Mint &operator-=(Mint a) { v += MOD - a.v; if (v >= MOD) v -= MOD; return *this; } Mint &operator*=(Mint a) { v = 1LL * v * a.v % MOD; return *this; } Mint &operator/=(Mint a) { return (*this) *= a.inv(); } Mint operator+(Mint a) const { return Mint(v) += a; }; Mint operator-(Mint a) const { return Mint(v) -= a; }; Mint operator*(Mint a) const { return Mint(v) *= a; }; Mint operator/(Mint a) const { return Mint(v) /= a; }; Mint operator-() { return v ? MOD - v : v; } bool operator==(const Mint a) const { return v == a.v; } bool operator!=(const Mint a) const { return v != a.v; } bool operator<(const Mint a) const { return v < a.v; } static Mint pow(Mint v, long long k) { Mint res(1), tmp(v); while (k) { if (k & 1) res *= tmp; tmp *= tmp; k >>= 1; } return res; } // find x s.t. a^x = b static T log(Mint a, Mint b) { const T sq = 40000; unordered_map<T, T> dp; dp.reserve(sq); Mint res(1); for (ll r = 0; r < sq; r++) { if (!dp.count(res)) dp[res] = r; res *= a; } Mint p = pow(a.inv(), sq); res = b; for (ll q = 0; q <= MOD / sq + 1; q++) { if (dp.count(res)) { T idx = q * sq + dp[res]; if (idx > 0) return idx; } res *= p; } return T(-1); } static vector<Mint> fact, finv, invs; static void init(ll n) { if (n + 1 <= (signed)fact.size()) return; fact.assign(n + 1, 1); finv.assign(n + 1, 1); invs.assign(n + 1, 1); for (ll i = 1; i <= n; i++) fact[i] = fact[i - 1] * Mint(i); finv[n] = Mint(1) / fact[n]; for (ll i = n; i >= 1; i--) finv[i - 1] = finv[i] * Mint(i); for (ll i = 1; i <= n; i++) invs[i] = finv[i] * fact[i - 1]; } static Mint comb(long long n, ll k) { Mint res(1); for (ll i = 0; i < k; i++) { res *= Mint(n - i); res /= Mint(i + 1); } return res; } static Mint C(ll n, ll k) { if (n < k || k < 0) return Mint(0); init(n); return fact[n] * finv[n - k] * finv[k]; } static Mint P(ll n, ll k) { if (n < k || k < 0) return Mint(0); init(n); return fact[n] * finv[n - k]; } static Mint H(ll n, ll k) { if (n < 0 || k < 0) return Mint(0); if (!n && !k) return Mint(1); init(n + k - 1); return C(n + k - 1, k); } static Mint S(ll n, ll k) { Mint res; init(k); for (ll i = 1; i <= k; i++) { Mint tmp = C(k, i) * Mint(i).pow(n); if ((k - i) & 1) res -= tmp; else res += tmp; } return res *= finv[k]; } static vector<vector<Mint>> D(ll n, ll m) { vector<vector<Mint>> dp(n + 1, vector<Mint>(m + 1, 0)); dp[0][0] = Mint(1); for (ll i = 0; i <= n; i++) { for (ll j = 1; j <= m; j++) { if (i - j >= 0) dp[i][j] = dp[i][j - 1] + dp[i - j][j]; else dp[i][j] = dp[i][j - 1]; } } return dp; } static Mint B(ll n, ll k) { Mint res; for (ll j = 1; j <= k; j++) res += S(n, j); return res; } static Mint montmort(ll n) { Mint res; init(n); for (ll k = 2; k <= n; k++) { if (k & 1) res -= finv[k]; else res += finv[k]; } return res *= fact[n]; } static Mint LagrangePolynomial(vector<Mint> &y, Mint t) { ll n = y.size() - 1; if (t.v <= n) return y[t.v]; init(n + 1); Mint num(1); for (ll i = 0; i <= n; i++) num *= t - Mint(i); Mint res; for (ll i = 0; i <= n; i++) { Mint tmp = y[i] * num / (t - Mint(i)) * finv[i] * finv[n - i]; if ((n - i) & 1) res -= tmp; else res += tmp; } return res; } }; class Combination { // this calculates combination (nCk). // Constructor runs in O(MAX). // get(n,k) returns nCk in O(1). ll MAX, MOD; vll fac; vll finv; vll inv; public: Combination(ll MAX = 210000, ll MOD = 1000000007) : MOD(MOD), MAX(max(MAX, 2LL)), fac(vll(MAX + 1)), finv(vll(MAX + 1)), inv(vll(MAX + 1)) { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; pre_process(2LL, MAX + 1); } ll get(ll n, ll k) { if (MAX < n) pre_process(MAX + 1, n + 1); if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } private: void pre_process(ll m, ll n) { if (MAX < n) { fac.resize(n); inv.resize(n); finv.resize(n); } rep(i, m, n) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } }; ll choose(int n, int r) { // O(r) for small n ll acc = 1; rep(i, 0, r) acc = acc * (n - i) / (i + 1); return acc; } ll gcd(ll a, ll b) { if (a % b == 0) return b; else return gcd(b, a % b); } vll getDivisors(ll n) { vll res; ll i = 1; for (; i * i < n; i++) { if (n % i == 0) { res.push_back(i); res.push_back(n / i); } } if (i * i == n) res.push_back(i); sort(res.begin(), res.end()); return res; } vll getDivisors(ll n, ll m) { // O(sqrt(min(n,m))) if (n > m) swap(n, m); vll res; ll i = 1; for (; i * i < n; i++) { if (n % i == 0) { if (m % i == 0) res.push_back(i); if (m % (n / i) == 0) res.push_back(n / i); } } if (i * i == n) if (m % i == 0) res.push_back(i); sort(res.begin(), res.end()); return res; } vector<pll> prime_factorize(ll n) { vector<pll> res; for (ll p = 2; p * p <= n; ++p) { if (n % p != 0) continue; ll num = 0; while (n % p == 0) { ++num; n /= p; } res.push_back({p, num}); } if (n != 1) res.push_back(make_pair(n, 1)); return res; } // ============================ Header ================================= int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(12); auto x = modint<MOD>(1); auto y = modint<>(); auto z = y + x; ll n; cin >> n; vvll a(n, vll(n)); read_v(a); vector<bitset<21>> as(n); rep(i, 0, n) rep(j, 0, n) { as[i][j] = a[i][j]; } vector<vector<int>> dp(n, vector<int>(1LL << 22)); rep(i, 0, n) { rep(j, 0, 1 << n) { auto poss = as[i] & bitset<21>(j); if (i == 0) { dp[0][j] = poss.count(); continue; } if (poss == 0) continue; rep(k, 0, n) { if (poss[k]) dp[i][j] += dp[i - 1][j ^ (1LL << k)]; if (dp[i][j] >= MOD) dp[i][j] -= MOD; } } } cout << dp[n - 1][(1LL << n) - 1] << endl; return 0; }
#pragma GCC optimize("-O3") #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <time.h> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <vector> #ifdef _MSC_VER #include <intrin.h> #define popcnt __popcnt64 // # define __builtin_popcount __popcnt #else #define popcnt __builtin_popcountll #endif // #include "boost/variant.hpp" using namespace std; typedef long long ll; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 60; #define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i) #define rep_skip(i, N, M, ...) \ for (ll i = N, i##_len = (M); i < i##_len; i += (skip)) #define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i) #define pb push_back typedef pair<double, double> pd; typedef pair<ll, ll> pll; template <int n> struct tll_impl { using type = decltype(tuple_cat(tuple<ll>(), declval<typename tll_impl<n - 1>::type>())); }; template <> struct tll_impl<1> { using type = tuple<ll>; }; template <int n> using tll = typename tll_impl<n>::type; template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); }; template <int n, typename T> struct vec_t_impl { using type = vector<typename vec_t_impl<n - 1, T>::type>; }; template <typename T> struct vec_t_impl<1, T> { using type = vector<T>; }; template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type; // check static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, ""); // decompose vector into basetype and dimension. template <typename T> struct vec_dec { static constexpr int dim = 0; using type = T; }; template <typename T> struct vec_dec<vector<T>> { static constexpr int dim = vec_dec<T>::dim + 1; using type = typename vec_dec<T>::type; }; static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, ""); static_assert(vec_dec<vec_t<3, ll>>::dim == 3, ""); template <typename T = ll> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T = ll, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } // ex: auto dp = make_v<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5)); // check if T is vector template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<vector<T>> : std::true_type {}; static_assert(is_vector<vector<ll>>::value == true && is_vector<ll>::value == false, ""); template <typename T, typename V, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void fill_v(T &t, const V &v) { t = v; } template <typename T, typename V, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void fill_v(T &t, const V &v) { for (auto &&x : t) fill_v(x, v); } // ex: fill_v(dp, INF); template <typename T, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void read_v(T &x) { cin >> x; } template <typename T, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void read_v(T &x) { rep(i, 0, x.size()) read_v(x[i]); } template <typename T, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void write_v(T &x) { cout << x << " "; } template <typename T, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void write_v(T &x) { rep(i, 0, x.size()) write_v(x[i]); cout << endl; } typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; template <typename T> using pq_greater = priority_queue<T, vector<T>, greater<T>>; #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define perm(c) \ sort(all(c)); \ for (bool c##perm = 1; c##perm; c##perm = next_permutation(all(c))) template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } vll seq(ll i, ll j) { vll res(j - i); rep(k, i, j) res[k] = i + k; return res; } constexpr ll POW_0(ll x, ll y) { if (y == 0) return 1; if (y == 1) return x; if (y == 2) return x * x; if (y % 2 == 0) return POW_0(POW_0(x, y / 2), 2LL); return ((POW_0(POW_0(x, y / 2), 2LL)) * (x)); } constexpr ll POW(ll x, ll y, ll mod = MOD) { if (mod == 0) return POW_0(x, y); if (y == 0) return 1; if (y == 1) return x % mod; if (y == 2) return x * x % mod; if (y % 2 == 0) return POW(POW(x, y / 2, mod), 2LL, mod) % mod; return ((POW(POW(x, y / 2, mod), 2LL, mod)) * (x % mod)) % mod; } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs, Functor f) { std::sort(std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void stable_sort_by(Inputs &inputs, Functor f) { std::stable_sort( std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } ll div_ferm(ll a, ll b, ll mod) { return (a * POW(b, mod - 2, mod)) % mod; } // === Modint === template <std::uint_fast64_t Modulus = 1000000007> class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} // constexpr modint(const modint& rhs) noexcept { // this->a = rhs.value(); // } // constexpr modint &operator=(const modint &rhs) noexcept { // this->a = rhs.value(); // return *this; // } constexpr u64 value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } modint &operator++() noexcept { return *this += modint(1); } modint &operator++(int) noexcept { auto t = *this; *this += modint(1); return t; } modint &operator--() noexcept { return *this -= modint(1); } modint &operator--(int) noexcept { auto t = *this; *this -= modint(1); return t; } constexpr bool operator==(const modint rhs) const noexcept { return a == rhs.value(); } constexpr bool operator!=(const modint rhs) const noexcept { return a != rhs.value(); } constexpr bool operator<(const modint rhs) const noexcept { return a < rhs.value(); } // should be moved to Google Test // constexpr void test(){ // constexpr auto x = modint<5>(3); // constexpr auto y = modint<5>(4); // constexpr auto z = modint<5>(2); // static_assert(x + y == z, ""); // static_assert(x != y, ""); // static_assert(++x == y && x++ != y && x == --y && x != y--, ""); // static_assert(x + 6 == y, ""); // static_assert(x / 2 == y, ""); // //} }; template <uint_fast64_t Modulus> ostream &operator<<(ostream &o, const modint<Modulus> &t) { o << t.value(); return o; } template <uint_fast64_t Modulus> istream &operator>>(istream &in, modint<Modulus> &t) { uint_fast64_t x; in >> x; t = modint<Modulus>(x); return in; } template <uint_fast64_t Modulus> modint<Modulus> POW(modint<Modulus> x, ll n) { return modint<Modulus>(POW(x.value(), n, Modulus)); } // === Mll === template <typename T = ll, T MOD = 1000000007> struct Mint { T v; Mint() : v(0) {} Mint(signed v) : v(v) {} Mint(long long t) { v = t % MOD; if (v < 0) v += MOD; } Mint inv() { return pow(MOD - 2); } Mint &operator+=(Mint a) { v += a.v; if (v >= MOD) v -= MOD; return *this; } Mint &operator-=(Mint a) { v += MOD - a.v; if (v >= MOD) v -= MOD; return *this; } Mint &operator*=(Mint a) { v = 1LL * v * a.v % MOD; return *this; } Mint &operator/=(Mint a) { return (*this) *= a.inv(); } Mint operator+(Mint a) const { return Mint(v) += a; }; Mint operator-(Mint a) const { return Mint(v) -= a; }; Mint operator*(Mint a) const { return Mint(v) *= a; }; Mint operator/(Mint a) const { return Mint(v) /= a; }; Mint operator-() { return v ? MOD - v : v; } bool operator==(const Mint a) const { return v == a.v; } bool operator!=(const Mint a) const { return v != a.v; } bool operator<(const Mint a) const { return v < a.v; } static Mint pow(Mint v, long long k) { Mint res(1), tmp(v); while (k) { if (k & 1) res *= tmp; tmp *= tmp; k >>= 1; } return res; } // find x s.t. a^x = b static T log(Mint a, Mint b) { const T sq = 40000; unordered_map<T, T> dp; dp.reserve(sq); Mint res(1); for (ll r = 0; r < sq; r++) { if (!dp.count(res)) dp[res] = r; res *= a; } Mint p = pow(a.inv(), sq); res = b; for (ll q = 0; q <= MOD / sq + 1; q++) { if (dp.count(res)) { T idx = q * sq + dp[res]; if (idx > 0) return idx; } res *= p; } return T(-1); } static vector<Mint> fact, finv, invs; static void init(ll n) { if (n + 1 <= (signed)fact.size()) return; fact.assign(n + 1, 1); finv.assign(n + 1, 1); invs.assign(n + 1, 1); for (ll i = 1; i <= n; i++) fact[i] = fact[i - 1] * Mint(i); finv[n] = Mint(1) / fact[n]; for (ll i = n; i >= 1; i--) finv[i - 1] = finv[i] * Mint(i); for (ll i = 1; i <= n; i++) invs[i] = finv[i] * fact[i - 1]; } static Mint comb(long long n, ll k) { Mint res(1); for (ll i = 0; i < k; i++) { res *= Mint(n - i); res /= Mint(i + 1); } return res; } static Mint C(ll n, ll k) { if (n < k || k < 0) return Mint(0); init(n); return fact[n] * finv[n - k] * finv[k]; } static Mint P(ll n, ll k) { if (n < k || k < 0) return Mint(0); init(n); return fact[n] * finv[n - k]; } static Mint H(ll n, ll k) { if (n < 0 || k < 0) return Mint(0); if (!n && !k) return Mint(1); init(n + k - 1); return C(n + k - 1, k); } static Mint S(ll n, ll k) { Mint res; init(k); for (ll i = 1; i <= k; i++) { Mint tmp = C(k, i) * Mint(i).pow(n); if ((k - i) & 1) res -= tmp; else res += tmp; } return res *= finv[k]; } static vector<vector<Mint>> D(ll n, ll m) { vector<vector<Mint>> dp(n + 1, vector<Mint>(m + 1, 0)); dp[0][0] = Mint(1); for (ll i = 0; i <= n; i++) { for (ll j = 1; j <= m; j++) { if (i - j >= 0) dp[i][j] = dp[i][j - 1] + dp[i - j][j]; else dp[i][j] = dp[i][j - 1]; } } return dp; } static Mint B(ll n, ll k) { Mint res; for (ll j = 1; j <= k; j++) res += S(n, j); return res; } static Mint montmort(ll n) { Mint res; init(n); for (ll k = 2; k <= n; k++) { if (k & 1) res -= finv[k]; else res += finv[k]; } return res *= fact[n]; } static Mint LagrangePolynomial(vector<Mint> &y, Mint t) { ll n = y.size() - 1; if (t.v <= n) return y[t.v]; init(n + 1); Mint num(1); for (ll i = 0; i <= n; i++) num *= t - Mint(i); Mint res; for (ll i = 0; i <= n; i++) { Mint tmp = y[i] * num / (t - Mint(i)) * finv[i] * finv[n - i]; if ((n - i) & 1) res -= tmp; else res += tmp; } return res; } }; class Combination { // this calculates combination (nCk). // Constructor runs in O(MAX). // get(n,k) returns nCk in O(1). ll MAX, MOD; vll fac; vll finv; vll inv; public: Combination(ll MAX = 210000, ll MOD = 1000000007) : MOD(MOD), MAX(max(MAX, 2LL)), fac(vll(MAX + 1)), finv(vll(MAX + 1)), inv(vll(MAX + 1)) { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; pre_process(2LL, MAX + 1); } ll get(ll n, ll k) { if (MAX < n) pre_process(MAX + 1, n + 1); if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } private: void pre_process(ll m, ll n) { if (MAX < n) { fac.resize(n); inv.resize(n); finv.resize(n); } rep(i, m, n) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } }; ll choose(int n, int r) { // O(r) for small n ll acc = 1; rep(i, 0, r) acc = acc * (n - i) / (i + 1); return acc; } ll gcd(ll a, ll b) { if (a % b == 0) return b; else return gcd(b, a % b); } vll getDivisors(ll n) { vll res; ll i = 1; for (; i * i < n; i++) { if (n % i == 0) { res.push_back(i); res.push_back(n / i); } } if (i * i == n) res.push_back(i); sort(res.begin(), res.end()); return res; } vll getDivisors(ll n, ll m) { // O(sqrt(min(n,m))) if (n > m) swap(n, m); vll res; ll i = 1; for (; i * i < n; i++) { if (n % i == 0) { if (m % i == 0) res.push_back(i); if (m % (n / i) == 0) res.push_back(n / i); } } if (i * i == n) if (m % i == 0) res.push_back(i); sort(res.begin(), res.end()); return res; } vector<pll> prime_factorize(ll n) { vector<pll> res; for (ll p = 2; p * p <= n; ++p) { if (n % p != 0) continue; ll num = 0; while (n % p == 0) { ++num; n /= p; } res.push_back({p, num}); } if (n != 1) res.push_back(make_pair(n, 1)); return res; } // ============================ Header ================================= int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(12); auto x = modint<MOD>(1); auto y = modint<>(); auto z = y + x; ll n; cin >> n; vvll a(n, vll(n)); read_v(a); vector<bitset<21>> as(n); rep(i, 0, n) rep(j, 0, n) { as[i][j] = a[i][j]; } vector<vector<int>> dp(n, vector<int>(1LL << 22)); rep(i, 0, n) { rep(j, 0, 1 << n) { if (i + 1 != popcnt(j)) continue; auto poss = as[i] & bitset<21>(j); if (i == 0) { dp[0][j] = poss.count(); continue; } if (poss == 0) continue; rep(k, 0, n) { if (poss[k]) dp[i][j] += dp[i - 1][j ^ (1LL << k)]; if (dp[i][j] >= MOD) dp[i][j] -= MOD; } } } cout << dp[n - 1][(1LL << n) - 1] << endl; return 0; }
insert
647
647
647
649
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define M_PI 3.14159265358979323846 using ull = unsigned long long; using ll = long long; #define endl "\n" #define REP(i, n) for (ll i = 0; i < n; i++) #define REPR(i, n) for (ll i = n; i >= 0; i--) #define FOR(i, m, n) for (ll i = m; i < n; i++) #define fill(x, y) memset(x, y, sizeof(x)) #define even(x) (x) % 2 == 0 #define odd(x) (x) % 2 != 0 #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define IN1(type, x) \ type x; \ cin >> x; #define inll(x) \ ll x; \ cin >> x; #define INIT() \ cin.tie(0); \ ios::sync_with_stdio(0); \ cout << fixed << setprecision(20) // these functions return the position of result of Binary Search. #define LB(s, t, x) (int)(lower_bound(s, t, x) - s) #define UB(s, t, x) (int)(upper_bound(s, t, x) - s) const ll MOD_CONST = (ll)(1e9 + 7); const ll CFM = (ll)(998244353); ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp ? (a / temp * b) : 0; } int mDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int dx8[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy8[] = {0, 0, -1, 1, -1, 1, -1, 1}; template <typename F> class #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) [[nodiscard]] #elif defined(__GNUC__) && __GNUC_PREREQ(3, 4) __attribute__((warn_unused_result)) #endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) FixPoint : F { public: explicit constexpr FixPoint(F &&f) noexcept : F(std::forward<F>(f)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return F::operator()(*this, std::forward<Args>(args)...); } }; // class FixPoint template <typename F> static inline constexpr decltype(auto) makeFixPoint(F &&f) noexcept { return FixPoint<F>{std::forward<F>(f)}; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, size_t b, Ts... ts) { return vector<decltype(make_v<T>(b, ts...))>(a, make_v<T>(b, ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <typename T> vector<T> pows(int b, int n) { // vec{b^0, b^1, b^2, ...} vector<T> ret; T x = 1; while (ret.size() < n) { ret.push_back(x); x *= b; } return ret; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline tuple<ll, ll> rotate45(tuple<ll, ll> point) { ll x = get<0>(point), y = get<1>(point); return tuple<ll, ll>(x + y, x - y); } inline bool rangeCheck2D(int nx, int ny, int Width, int Height) { return nx >= 0 and nx < Width and ny >= 0 and ny < Height; } template <typename T> vector<T> INPA(ll n) { vector<T> x; REP(i, n) { T tmp; cin >> tmp; x.push_back(tmp); } return move(x); } // base^x % MOD - O(x) ll p_base(ll base, ll x) { ll ans = 1; REP(i, x) { ans *= base; ans %= MOD_CONST; } return ans; } template <typename T> void out(T o) { cout << o << endl; } template <typename T> void out(vector<T> &out) { REP(i, (int)out.size()) { cout << out[i]; if (i == (int)out.size() - 1) cout << endl; else cout << " "; } } template <typename T> void out(vector<vector<T>> o) { REP(i, o.size()) out(o[i]); } void YesNo(bool f) { cout << (f ? "Yes" : "No") << endl; } void YESNO(bool f) { cout << (f ? "YES" : "NO") << endl; } string i_query(ll x, ll y) { cout << "? " << x << " " << y << endl; fflush(stdout); string ret; cin >> ret; return ret; } void i_answer(ll ans) { cout << "! " << ans << endl; fflush(stdout); } // use " for (const auto& e : V) // lambda expression // auto f = [](int arg1, double arg2) { return ret; }; // lambda recursion // auto result = makeFixPoint([&](auto rec, int pos, int v) -> int { // rec(pos, v); // })(0, 1); // auto func = makeFixPoint([]() -> int {}); // int ret = func(); // tuple binding // auto t = make_tuple(0, 0); // int x, y; tie(x, y) = t; // auto [x, y] = t; // for pair // auto [a, b] = pair<int, int>({v1, v2}); // bitset<N> bs(ini_val); // N must be constant // bs.reset(); // reset all int main(void) { INIT(); // comment out for Interective Program inll(N); auto p = make_v<int>(N, N); REP(i, N) REP(j, N) cin >> p[i][j]; auto dp = make_v<ll>(N + 1, (ll)pow(2, N)); fill_v(dp, 0); dp[0][0] = 1; #define bit(n, k) ((n >> k) & 1) FOR(i, 1, N + 1) { REP(s, 1 << N) { REP(j, N) { if (bit(s, j) == 0) continue; if (p[i - 1][j] == 0) continue; dp[i][s] += dp[i - 1][s ^ (1 << j)]; dp[i][s] %= MOD_CONST; } } } ll ans = dp[N][(1 << N) - 1]; out(ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define M_PI 3.14159265358979323846 using ull = unsigned long long; using ll = long long; #define endl "\n" #define REP(i, n) for (ll i = 0; i < n; i++) #define REPR(i, n) for (ll i = n; i >= 0; i--) #define FOR(i, m, n) for (ll i = m; i < n; i++) #define fill(x, y) memset(x, y, sizeof(x)) #define even(x) (x) % 2 == 0 #define odd(x) (x) % 2 != 0 #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define IN1(type, x) \ type x; \ cin >> x; #define inll(x) \ ll x; \ cin >> x; #define INIT() \ cin.tie(0); \ ios::sync_with_stdio(0); \ cout << fixed << setprecision(20) // these functions return the position of result of Binary Search. #define LB(s, t, x) (int)(lower_bound(s, t, x) - s) #define UB(s, t, x) (int)(upper_bound(s, t, x) - s) const ll MOD_CONST = (ll)(1e9 + 7); const ll CFM = (ll)(998244353); ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp ? (a / temp * b) : 0; } int mDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int dx8[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy8[] = {0, 0, -1, 1, -1, 1, -1, 1}; template <typename F> class #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) [[nodiscard]] #elif defined(__GNUC__) && __GNUC_PREREQ(3, 4) __attribute__((warn_unused_result)) #endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) FixPoint : F { public: explicit constexpr FixPoint(F &&f) noexcept : F(std::forward<F>(f)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return F::operator()(*this, std::forward<Args>(args)...); } }; // class FixPoint template <typename F> static inline constexpr decltype(auto) makeFixPoint(F &&f) noexcept { return FixPoint<F>{std::forward<F>(f)}; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, size_t b, Ts... ts) { return vector<decltype(make_v<T>(b, ts...))>(a, make_v<T>(b, ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <typename T> vector<T> pows(int b, int n) { // vec{b^0, b^1, b^2, ...} vector<T> ret; T x = 1; while (ret.size() < n) { ret.push_back(x); x *= b; } return ret; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline tuple<ll, ll> rotate45(tuple<ll, ll> point) { ll x = get<0>(point), y = get<1>(point); return tuple<ll, ll>(x + y, x - y); } inline bool rangeCheck2D(int nx, int ny, int Width, int Height) { return nx >= 0 and nx < Width and ny >= 0 and ny < Height; } template <typename T> vector<T> INPA(ll n) { vector<T> x; REP(i, n) { T tmp; cin >> tmp; x.push_back(tmp); } return move(x); } // base^x % MOD - O(x) ll p_base(ll base, ll x) { ll ans = 1; REP(i, x) { ans *= base; ans %= MOD_CONST; } return ans; } template <typename T> void out(T o) { cout << o << endl; } template <typename T> void out(vector<T> &out) { REP(i, (int)out.size()) { cout << out[i]; if (i == (int)out.size() - 1) cout << endl; else cout << " "; } } template <typename T> void out(vector<vector<T>> o) { REP(i, o.size()) out(o[i]); } void YesNo(bool f) { cout << (f ? "Yes" : "No") << endl; } void YESNO(bool f) { cout << (f ? "YES" : "NO") << endl; } string i_query(ll x, ll y) { cout << "? " << x << " " << y << endl; fflush(stdout); string ret; cin >> ret; return ret; } void i_answer(ll ans) { cout << "! " << ans << endl; fflush(stdout); } // use " for (const auto& e : V) // lambda expression // auto f = [](int arg1, double arg2) { return ret; }; // lambda recursion // auto result = makeFixPoint([&](auto rec, int pos, int v) -> int { // rec(pos, v); // })(0, 1); // auto func = makeFixPoint([]() -> int {}); // int ret = func(); // tuple binding // auto t = make_tuple(0, 0); // int x, y; tie(x, y) = t; // auto [x, y] = t; // for pair // auto [a, b] = pair<int, int>({v1, v2}); // bitset<N> bs(ini_val); // N must be constant // bs.reset(); // reset all int main(void) { INIT(); // comment out for Interective Program inll(N); auto p = make_v<int>(N, N); REP(i, N) REP(j, N) cin >> p[i][j]; auto dp = make_v<ll>(N + 1, (ll)pow(2, N)); fill_v(dp, 0); dp[0][0] = 1; #define bit(n, k) ((n >> k) & 1) REP(s, 1 << N) { int i = buli(s); REP(j, N) { if (bit(s, j) == 0) continue; if (p[i - 1][j] == 0) continue; dp[i][s] += dp[i - 1][s ^ (1 << j)]; dp[i][s] %= MOD_CONST; } } ll ans = dp[N][(1 << N) - 1]; out(ans); return 0; }
replace
200
210
200
209
TLE
p03174
C++
Time Limit Exceeded
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } // #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) 1 #endif typedef long long ll; typedef double ld; #define pll pair<ll, ll> #define pii pair<int, int> #define pb push_back #define mp(x, y) make_pair((x), (y)) #define F first #define S second #define I insert #define vi vector<int> #define vll vector<ll> #define vpll vector<pll> #define all(x) (x).begin(), (x).end() #define sz(x) (ll)(x).size() const ll Mod = 1e9 + 7; int dp[21][1 << 21]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 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++) if (A[0][i] == 1) dp[0][1 << i] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < (1 << n); j++) { for (int k = 0; k < n; k++) { if (!(j & (1 << k)) && A[i][k] == 1) { dp[i][j ^ (1 << k)] += dp[i - 1][j]; if (dp[i][j ^ (1 << k)] >= Mod) dp[i][j ^ (1 << k)] -= Mod; } } } } cout << dp[n - 1][(1 << n) - 1] << endl; return 0; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } // #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) 1 #endif typedef long long ll; typedef double ld; #define pll pair<ll, ll> #define pii pair<int, int> #define pb push_back #define mp(x, y) make_pair((x), (y)) #define F first #define S second #define I insert #define vi vector<int> #define vll vector<ll> #define vpll vector<pll> #define all(x) (x).begin(), (x).end() #define sz(x) (ll)(x).size() const ll Mod = 1e9 + 7; int dp[21][1 << 21]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 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++) if (A[0][i] == 1) dp[0][1 << i] = 1; for (int i = 1; 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)) && A[i][k] == 1) { dp[i][j ^ (1 << k)] += dp[i - 1][j]; if (dp[i][j ^ (1 << k)] >= Mod) dp[i][j ^ (1 << k)] -= Mod; } } } } cout << dp[n - 1][(1 << n) - 1] << endl; return 0; }
insert
64
64
64
66
TLE
p03174
C++
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; }
#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; }
delete
30
31
30
30
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iostream> #define db(x) cout << #x << "=" << x << '\n' #define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\n' #define db3(x, y, z) \ cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \ << '\n' #define rep(i, n) for (int i = 0; i < (n); ++i) #define fr(i, a, n) for (int i = a; i <= (n); ++i) #define rf(i, a, n) for (int i = a; i >= (n); --i) #define mod 1000000007 #define eps 1e-9 #define PI 3.14159265358979323846 #define F first #define S second #define pb push_back #define pf push_front #define mp make_pair #define pll pair<ll, ll> #define _ << " " << using namespace std; typedef long long ll; typedef vector<ll> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef pair<int, pair<int, int>> iii; typedef vector<ii> vii; typedef vector<iii> viii; vector<bool> vis; vvi dp; ll matching(vvi a, int mask, int n) { if (n == 0) return 1; if (mask == 0) return 0; if (dp[n][mask] != -1) return dp[n][mask]; ll val = 0; // db(mask); for (int i = 0; i < a.size(); i++) { // db2(a[n-1][i],(mask&(1<<i))); if (a[n - 1][i] && (mask & (1 << i))) { // db2(n,mask); val += matching(a, mask ^ (1 << i), n - 1); val %= mod; } } dp[n][mask] = val % mod; // db(val); return dp[n][mask]; } int main() { int n; cin >> n; vvi a(n, vi(n, 0)); int mask = 0; for (int i = 0; i < n; i++) { mask |= (1 << i); for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } dp.clear(); dp.resize(n + 1, vi(1 << n, -1)); cout << matching(a, mask, n); // dp.resize(n,vi(n,1e18)); // for(int i=0;i<=n;i++){ // for(int j=0;j<=n;j++){ // if(i==j){ // dp[i][j]=i; // } // } // } // cout<<dp[0][n-1]<<endl; return 0; }
#include <bits/stdc++.h> #include <iostream> #define db(x) cout << #x << "=" << x << '\n' #define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\n' #define db3(x, y, z) \ cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \ << '\n' #define rep(i, n) for (int i = 0; i < (n); ++i) #define fr(i, a, n) for (int i = a; i <= (n); ++i) #define rf(i, a, n) for (int i = a; i >= (n); --i) #define mod 1000000007 #define eps 1e-9 #define PI 3.14159265358979323846 #define F first #define S second #define pb push_back #define pf push_front #define mp make_pair #define pll pair<ll, ll> #define _ << " " << using namespace std; typedef long long ll; typedef vector<ll> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef pair<int, pair<int, int>> iii; typedef vector<ii> vii; typedef vector<iii> viii; vector<bool> vis; vvi dp; ll matching(vvi &a, int mask, int n) { if (n == 0) return 1; if (mask == 0) return 0; if (dp[n][mask] != -1) return dp[n][mask]; ll val = 0; // db(mask); for (int i = 0; i < a.size(); i++) { // db2(a[n-1][i],(mask&(1<<i))); if (a[n - 1][i] && (mask & (1 << i))) { // db2(n,mask); val += matching(a, mask ^ (1 << i), n - 1); val %= mod; } } dp[n][mask] = val % mod; // db(val); return dp[n][mask]; } int main() { int n; cin >> n; vvi a(n, vi(n, 0)); int mask = 0; for (int i = 0; i < n; i++) { mask |= (1 << i); for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } dp.clear(); dp.resize(n + 1, vi(1 << n, -1)); cout << matching(a, mask, n); // dp.resize(n,vi(n,1e18)); // for(int i=0;i<=n;i++){ // for(int j=0;j<=n;j++){ // if(i==j){ // dp[i][j]=i; // } // } // } // cout<<dp[0][n-1]<<endl; return 0; }
replace
33
34
33
34
TLE
p03174
C++
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]; }
#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]; }
replace
3
4
3
4
0
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; #define F first #define S second #define migmig \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ld long double #define pii pair<int, int> #define bug(x) cout << "passed " << x << endl; #define ashar(x, y, z) \ cout << setprecision(x) << fixed << y; \ if (z == 1) \ cout << "\n"; long long const inf = 1e18, linf = 2e9, mod = 1e9 + 7, inf2 = 1e12; int const mxn = 5e6 + 10; ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } int l[30][30]; long long dp[30][(1 << 21) + 10]; int main() { migmig int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> l[i][j]; } for (int mask = 0; mask < (1 << n); mask++) { for (int i = 0; i < n; i++) if ((1 << i) & mask and l[0][i]) dp[0][mask]++; } for (int v = 1; v < n; v++) { for (int mask = 0; mask < (1 << n); mask++) { if (__builtin_popcount(mask) < v) continue; for (int u = 0; u < n; u++) { if (l[v][u] and mask & (1 << u)) { dp[v][mask] += dp[v - 1][mask - (1 << u)]; if (dp[v][mask] >= inf) dp[v][mask] %= mod; } } } } cout << dp[n - 1][(1 << n) - 1] % mod; }
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; #define F first #define S second #define migmig \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ld long double #define pii pair<int, int> #define bug(x) cout << "passed " << x << endl; #define ashar(x, y, z) \ cout << setprecision(x) << fixed << y; \ if (z == 1) \ cout << "\n"; long long const inf = 1e18, linf = 2e9, mod = 1e9 + 7, inf2 = 1e12; int const mxn = 5e6 + 10; ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } int l[30][30]; long long dp[30][(1 << 21) + 10]; int main() { migmig int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> l[i][j]; } for (int mask = 0; mask < (1 << n); mask++) { for (int i = 0; i < n; i++) if ((1 << i) & mask and l[0][i]) dp[0][mask]++; } for (int v = 1; v < n; v++) { for (int mask = (1 << (v - 1)) - 1; mask < (1 << n); mask++) { if (__builtin_popcount(mask) < v) continue; for (int u = 0; u < n; u++) { if (l[v][u] and mask & (1 << u)) { dp[v][mask] += dp[v - 1][mask - (1 << u)]; if (dp[v][mask] >= inf) dp[v][mask] %= mod; } } } } cout << dp[n - 1][(1 << n) - 1] % mod; }
replace
42
43
42
43
TLE
p03174
C++
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; }
#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; }
insert
16
16
16
20
TLE
p03174
C++
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; }
#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; }
replace
48
50
48
50
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<ll, null_type, less_equal<ll>, rb_tree_tag, \ tree_order_statistics_node_update> #define PI 3.14159265 #define ll long long #define ld long double #define vi vector<int> #define pb push_back #define vll vector<ll> #define vvi vector<vi> #define pii pair<int, int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 300000000000000001 #define infx 9223372036854775806 #define all(c) c.begin(), c.end() #define mk(x, yy) make_pair(x, y) #define mem(a, val) memset(a, val, sizeof(a)) #define eb emplace_back #define ff first #define ss second #define re return // #define endl "\n" #define max2(x, y) (x > y) ? x : y #define min2(x, y) (x < y) ? x : y #define mid(s, e) (s + (e - s) / 2) #define mini INT_MIN #define maxo INT_MAX #define rep(i, a, b) for (int i = a; i < (int)(b); ++i) #define read(a, n) \ for (int i = 0; i < n; i++) \ cin >> a[i] #define show(a, n) \ for (int i = 0; i < n; i++) \ cout << a[i] << ' '; \ cout << endl #define ene cout << "\n"; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll power(ll x, ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } bool compr(std::vector<ll> a, std::vector<ll> b) { if (a[0] == b[0]) return a[1] < b[1]; else return a[0] < b[0]; } bool comp(std::vector<ll> a, std::vector<ll> b) { if (a[1] == b[1] && a[2] == b[2]) return a[0] < b[0]; else if (a[1] == b[1]) return a[2] > b[2]; else return a[1] < b[1]; } ll dp[2097159] = {}; ll dp2[2097159] = {}; int main() { #ifdef SHIVANSH freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; ll a[n][n]; for (ll i = 0; i < n; i++) { for (ll j = 0; j < n; j++) cin >> a[i][j]; } ll MAXN = pow(2, n); // cout<<MAXN<<endl; for (ll i = 0; i < MAXN; i++) { dp[0] = 0; dp2[0] = 0; } dp[0] = 1; // cout<<"helloe"<<endl; for (ll man = 0; man < n; man++) { // cout<<man<<endl; for (ll i = 0; i < MAXN; i++) { ll b[21] = {}; ll tmp = i; ll k = 0; while (tmp > 0) { if (tmp % 2 == 1) b[k] = 1; k++; tmp /= 2; } for (ll j = 0; j < 21; j++) { if (b[j] == 0 && a[man][j] == 1) { ll kyr = pow(2, j); dp2[i + kyr] = (dp2[i + kyr] + dp[i]) % mod; } } } for (ll i = 0; i < MAXN; i++) { dp[i] = dp2[i]; dp2[i] = 0; } } cout << dp[MAXN - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<ll, null_type, less_equal<ll>, rb_tree_tag, \ tree_order_statistics_node_update> #define PI 3.14159265 #define ll long long #define ld long double #define vi vector<int> #define pb push_back #define vll vector<ll> #define vvi vector<vi> #define pii pair<int, int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 300000000000000001 #define infx 9223372036854775806 #define all(c) c.begin(), c.end() #define mk(x, yy) make_pair(x, y) #define mem(a, val) memset(a, val, sizeof(a)) #define eb emplace_back #define ff first #define ss second #define re return // #define endl "\n" #define max2(x, y) (x > y) ? x : y #define min2(x, y) (x < y) ? x : y #define mid(s, e) (s + (e - s) / 2) #define mini INT_MIN #define maxo INT_MAX #define rep(i, a, b) for (int i = a; i < (int)(b); ++i) #define read(a, n) \ for (int i = 0; i < n; i++) \ cin >> a[i] #define show(a, n) \ for (int i = 0; i < n; i++) \ cout << a[i] << ' '; \ cout << endl #define ene cout << "\n"; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll power(ll x, ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } bool compr(std::vector<ll> a, std::vector<ll> b) { if (a[0] == b[0]) return a[1] < b[1]; else return a[0] < b[0]; } bool comp(std::vector<ll> a, std::vector<ll> b) { if (a[1] == b[1] && a[2] == b[2]) return a[0] < b[0]; else if (a[1] == b[1]) return a[2] > b[2]; else return a[1] < b[1]; } ll dp[2097159] = {}; ll dp2[2097159] = {}; int main() { #ifdef SHIVANSH freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; ll a[n][n]; for (ll i = 0; i < n; i++) { for (ll j = 0; j < n; j++) cin >> a[i][j]; } ll MAXN = pow(2, n); // cout<<MAXN<<endl; for (ll i = 0; i < MAXN; i++) { dp[0] = 0; dp2[0] = 0; } dp[0] = 1; // cout<<"helloe"<<endl; for (ll man = 0; man < n; man++) { // cout<<man<<endl; for (ll i = 0; i < MAXN; i++) { if (__builtin_popcount(i) != man) continue; ll b[21] = {}; ll tmp = i; ll k = 0; while (tmp > 0) { if (tmp % 2 == 1) b[k] = 1; k++; tmp /= 2; } for (ll j = 0; j < 21; j++) { if (b[j] == 0 && a[man][j] == 1) { ll kyr = pow(2, j); dp2[i + kyr] = (dp2[i + kyr] + dp[i]) % mod; } } } for (ll i = 0; i < MAXN; i++) { dp[i] = dp2[i]; dp2[i] = 0; } } cout << dp[MAXN - 1] << endl; }
insert
108
108
108
110
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i, n) for (auto &i : n) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } void init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } #ifdef DEBUG template <class T, class N> void verr(const T &a, const N &n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } template <class H, class... T> void err(H &&h, T &&...t) { cerr << h << (sizeof...(t) ? " " : "\n") << flush; if (sizeof...(t) > 0) err(forward<T>(t)...); } #endif const ll INF = 5e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// ll dp[2][1 << 21]; bool A[21][21]; int main() { init(); ll N; cin >> N; rep(i, N) rep(j, N) cin >> A[i][j]; dp[0][0] = 1; rep(i, N) { ll now = i % 2, to = (i + 1) % 2; rep(bit, 1 << N) { rep(j, N) { if (!(bit & 1 << j) and A[i][j]) dp[to][bit | 1 << j] += dp[now][bit]; } } rep(bit, 1 << N) dp[to][bit] %= MOD, dp[now][bit] = 0; } // rep(i,2) verr(dp[i], 1 << N); cout << dp[N % 2][(1 << N) - 1] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i, n) for (auto &i : n) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } void init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } #ifdef DEBUG template <class T, class N> void verr(const T &a, const N &n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } template <class H, class... T> void err(H &&h, T &&...t) { cerr << h << (sizeof...(t) ? " " : "\n") << flush; if (sizeof...(t) > 0) err(forward<T>(t)...); } #endif const ll INF = 5e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// ll dp[2][1 << 21]; bool A[21][21]; int main() { init(); ll N; cin >> N; rep(i, N) rep(j, N) cin >> A[i][j]; dp[0][0] = 1; rep(i, N) { ll now = i % 2, to = (i + 1) % 2; rep(bit, 1 << N) { if (dp[now][bit]) rep(j, N) { if (!(bit & 1 << j) and A[i][j]) dp[to][bit | 1 << j] += dp[now][bit]; } } rep(bit, 1 << N) dp[to][bit] %= MOD, dp[now][bit] = 0; } // rep(i,2) verr(dp[i], 1 << N); cout << dp[N % 2][(1 << N) - 1] << endl; }
replace
67
71
67
72
TLE
p03174
C++
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; }
#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; }
replace
49
55
49
59
TLE
p03174
C++
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]); }
/*** 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]); }
insert
33
33
33
36
TLE
p03174
C++
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; }
#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; }
replace
27
29
27
30
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define INF LLONG_MAX #define dd double #define fi first #define se second #define rep(i, a, b) for (ll i = a; i <= b; i++) #define mp make_pair #define pb push_back #define fbo find_by_order #define ook order_of_key #define dll deque<long long> #define qll queue<long long> #define vll vector<long long> #define vc vector<char> #define vs vector<string> #define vvll vector<vector<ll>> #define vpll vector<pair<long long, long long>> #define vpcl vector<pair<char, long long>> #define vpsl vector<pair<string, long long>> #define stll stack<long long> #define stc stack<char> #define mll map<long long, long long> #define pll pair<long long, long long> #define psl pair<string, long long> #define mcl map<char, long long> #define msl map<string, long long> #define pcl pair<char, long long> #define mmll multimap<long long, long long> #define mmcl multimap<char, long long> #define mmsl multimap<string, long long> #define sll set<long long> #define sc set<char> #define ss set<string> #define msll multiset<long long> #define msc multiset<char> #define mss multiset<string> #define lb lower_bound #define up upper_bound #define lt length #define clr clear #define ap append #define sz size #define sub substr #define ull unsigned long long int using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #include <boost/multiprecision/cpp_int.hpp> // using boost::multiprecision::cpp_int; using namespace __gnu_pbds; #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ll modexp(ll x, ll n, ll p) { // modular exponential function if (n == 0) { return 1; } ll u = modexp(x, n / 2, p); u = u * u % p; if (n % 2 == 1) { u = (u * x) % p; } return u; } ll gcd(ll a, ll b) { // Eulers algorithm if (b == 0) return a; else return gcd(b, a % b); } ll arr[21][21]; ll dp[(1 << 21) + 1]; ll m = 1000000007; ll f(ll s, ll n) { ll j = __builtin_popcount(s); if (s == pow(2, n) - 1) { return 1; } if (j == n) { return 0; } if (dp[s] != -1) { return dp[s]; } // cout<<j<<"\n"; ll ans = 0; rep(i, 0, n - 1) { if ((s & 1 << i) == 0 && arr[i][j] == 1) { ans = (ans % m + f((s | 1 << i), n) % m) % m; } } dp[s] = ans; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; memset(dp, -1, sizeof(dp)); rep(i, 0, n - 1) { rep(j, 0, n - 1) { cin >> arr[i][j]; } } cout << f(0, n); }
#include <bits/stdc++.h> #define ll long long #define INF LLONG_MAX #define dd double #define fi first #define se second #define rep(i, a, b) for (ll i = a; i <= b; i++) #define mp make_pair #define pb push_back #define fbo find_by_order #define ook order_of_key #define dll deque<long long> #define qll queue<long long> #define vll vector<long long> #define vc vector<char> #define vs vector<string> #define vvll vector<vector<ll>> #define vpll vector<pair<long long, long long>> #define vpcl vector<pair<char, long long>> #define vpsl vector<pair<string, long long>> #define stll stack<long long> #define stc stack<char> #define mll map<long long, long long> #define pll pair<long long, long long> #define psl pair<string, long long> #define mcl map<char, long long> #define msl map<string, long long> #define pcl pair<char, long long> #define mmll multimap<long long, long long> #define mmcl multimap<char, long long> #define mmsl multimap<string, long long> #define sll set<long long> #define sc set<char> #define ss set<string> #define msll multiset<long long> #define msc multiset<char> #define mss multiset<string> #define lb lower_bound #define up upper_bound #define lt length #define clr clear #define ap append #define sz size #define sub substr #define ull unsigned long long int using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #include <boost/multiprecision/cpp_int.hpp> // using boost::multiprecision::cpp_int; using namespace __gnu_pbds; #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ll modexp(ll x, ll n, ll p) { // modular exponential function if (n == 0) { return 1; } ll u = modexp(x, n / 2, p); u = u * u % p; if (n % 2 == 1) { u = (u * x) % p; } return u; } ll gcd(ll a, ll b) { // Eulers algorithm if (b == 0) return a; else return gcd(b, a % b); } ll arr[21][21]; ll dp[(1 << 21) + 1]; ll m = 1000000007; ll f(ll s, ll n) { ll j = __builtin_popcount(s); if (s == (1 << n) - 1) { return 1; } if (j == n) { return 0; } if (dp[s] != -1) { return dp[s]; } // cout<<j<<"\n"; ll ans = 0; rep(i, 0, n - 1) { if ((s & 1 << i) == 0 && arr[i][j] == 1) { ans = (ans % m + f((s | 1 << i), n) % m) % m; } } dp[s] = ans; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; memset(dp, -1, sizeof(dp)); rep(i, 0, n - 1) { rep(j, 0, n - 1) { cin >> arr[i][j]; } } cout << f(0, n); }
replace
78
79
78
79
TLE
p03174
C++
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"; }
#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"; }
insert
37
37
37
39
-11
p03174
C++
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]); }
#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]); }
replace
16
20
16
22
TLE
p03174
C++
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; }
#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; }
replace
21
27
21
23
TLE
p03174
C++
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; }
#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; }
replace
8
10
8
10
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using VS = vector<string>; using LL = long long; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using PLL = pair<LL, LL>; using VL = vector<LL>; using VVL = vector<VL>; #define ALL(a) begin((a)), end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++) #define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--) // #pragma GCC optimize ("-O3") #ifdef YANG33 #include "mydebug.hpp" #else #define DD(x) #endif const int INF = 1e9; const LL LINF = 1e16; const LL MOD = 1000000007; const double PI = acos(-1.0); int DX[8] = {0, 0, 1, -1, 1, 1, -1, -1}; int DY[8] = {1, -1, 0, 0, 1, -1, 1, -1}; /* ----- 2019/01/06 Problem: EDU_DP O / Link: __CONTEST_URL__ ----- */ /* ------問題------ -----問題ここまで----- */ /* -----解説等----- permanentを160人も通せるわけが無いので ----解説ここまで---- */ template <::std::uint_fast32_t MODULO> class modint { public: using uint32 = ::std::uint_fast32_t; using uint64 = ::std::uint_fast64_t; using value_type = uint32; uint32 a; modint() noexcept : a(0) {} modint(const uint32 x) noexcept : a(x) {} modint operator+(const modint &o) const noexcept { return a + o.a < MODULO ? modint(a + o.a) : modint(a + o.a - MODULO); } modint operator-(const modint &o) const noexcept { return modint(a < o.a ? a + MODULO - o.a : a - o.a); } modint operator*(const modint &o) const noexcept { return modint(static_cast<uint64>(a) * o.a % MODULO); } modint operator/(const modint &o) const { return modint(static_cast<uint64>(a) * (~o).a % MODULO); } modint &operator+=(const modint &o) noexcept { return *this = *this + o; } modint &operator-=(const modint &o) noexcept { return *this = *this - o; } modint &operator*=(const modint &o) noexcept { return *this = *this * o; } modint &operator/=(const modint &o) { return *this = *this / o; } modint operator~() const noexcept { return pow(MODULO - 2); } modint operator-() const noexcept { return a ? modint(MODULO - a) : *this; } modint operator++() noexcept { return a == MODULO - 1 ? a = 0 : ++a, *this; } modint operator--() noexcept { return a ? --a : a = MODULO - 1, *this; } bool operator==(const modint &o) const noexcept { return a == o.a; } bool operator!=(const modint &o) const noexcept { return a != o.a; } bool operator<(const modint &o) const noexcept { return a < o.a; } bool operator<=(const modint &o) const noexcept { return a <= o.a; } bool operator>(const modint &o) const noexcept { return a > o.a; } bool operator>=(const modint &o) const noexcept { return a >= o.a; } explicit operator bool() const noexcept { return a; } explicit operator uint32() const noexcept { return a; } modint pow(uint32 x) const noexcept { uint64 t = a, u = 1; while (x) { if (x & 1) u = u * t % MODULO; t = (t * t) % MODULO; x >>= 1; } return modint(u); } uint32 get() const noexcept { return a; } }; using mint = modint<MOD>; void chmax(LL &a, LL b) { a = max(a, b); } void chmin(LL &a, LL b) { a = min(a, b); } mint dp[22][1 << 22]; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; VVI a(N, VI(N)); FOR(i, 0, N) { FOR(j, 0, N) { cin >> a[i][j]; } } dp[0][0] = 1; FOR(i, 0, N) { FOR(state, 0, 1 << N) { FOR(j, 0, N) { if (state & 1 << j) continue; if (a[i][j]) { dp[i + 1][state | (1 << j)] += dp[i][state]; } } } } cout << dp[N][(1 << N) - 1].get() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using VS = vector<string>; using LL = long long; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using PLL = pair<LL, LL>; using VL = vector<LL>; using VVL = vector<VL>; #define ALL(a) begin((a)), end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++) #define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--) // #pragma GCC optimize ("-O3") #ifdef YANG33 #include "mydebug.hpp" #else #define DD(x) #endif const int INF = 1e9; const LL LINF = 1e16; const LL MOD = 1000000007; const double PI = acos(-1.0); int DX[8] = {0, 0, 1, -1, 1, 1, -1, -1}; int DY[8] = {1, -1, 0, 0, 1, -1, 1, -1}; /* ----- 2019/01/06 Problem: EDU_DP O / Link: __CONTEST_URL__ ----- */ /* ------問題------ -----問題ここまで----- */ /* -----解説等----- permanentを160人も通せるわけが無いので ----解説ここまで---- */ template <::std::uint_fast32_t MODULO> class modint { public: using uint32 = ::std::uint_fast32_t; using uint64 = ::std::uint_fast64_t; using value_type = uint32; uint32 a; modint() noexcept : a(0) {} modint(const uint32 x) noexcept : a(x) {} modint operator+(const modint &o) const noexcept { return a + o.a < MODULO ? modint(a + o.a) : modint(a + o.a - MODULO); } modint operator-(const modint &o) const noexcept { return modint(a < o.a ? a + MODULO - o.a : a - o.a); } modint operator*(const modint &o) const noexcept { return modint(static_cast<uint64>(a) * o.a % MODULO); } modint operator/(const modint &o) const { return modint(static_cast<uint64>(a) * (~o).a % MODULO); } modint &operator+=(const modint &o) noexcept { return *this = *this + o; } modint &operator-=(const modint &o) noexcept { return *this = *this - o; } modint &operator*=(const modint &o) noexcept { return *this = *this * o; } modint &operator/=(const modint &o) { return *this = *this / o; } modint operator~() const noexcept { return pow(MODULO - 2); } modint operator-() const noexcept { return a ? modint(MODULO - a) : *this; } modint operator++() noexcept { return a == MODULO - 1 ? a = 0 : ++a, *this; } modint operator--() noexcept { return a ? --a : a = MODULO - 1, *this; } bool operator==(const modint &o) const noexcept { return a == o.a; } bool operator!=(const modint &o) const noexcept { return a != o.a; } bool operator<(const modint &o) const noexcept { return a < o.a; } bool operator<=(const modint &o) const noexcept { return a <= o.a; } bool operator>(const modint &o) const noexcept { return a > o.a; } bool operator>=(const modint &o) const noexcept { return a >= o.a; } explicit operator bool() const noexcept { return a; } explicit operator uint32() const noexcept { return a; } modint pow(uint32 x) const noexcept { uint64 t = a, u = 1; while (x) { if (x & 1) u = u * t % MODULO; t = (t * t) % MODULO; x >>= 1; } return modint(u); } uint32 get() const noexcept { return a; } }; using mint = modint<MOD>; void chmax(LL &a, LL b) { a = max(a, b); } void chmin(LL &a, LL b) { a = min(a, b); } mint dp[22][1 << 22]; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; VVI a(N, VI(N)); FOR(i, 0, N) { FOR(j, 0, N) { cin >> a[i][j]; } } dp[0][0] = 1; FOR(i, 0, N) { FOR(j, 0, N) { if (a[i][j]) { FOR(state, 0, 1 << N) { if (state & 1 << j) continue; dp[i + 1][state | (1 << j)] += dp[i][state]; } } } } cout << dp[N][(1 << N) - 1].get() << endl; return 0; }
replace
111
116
111
116
TLE
p03174
C++
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; }
// 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; }
insert
21
21
21
23
TLE
p03174
C++
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'; }
#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'; }
replace
10
11
10
11
0
p03174
C++
Time Limit Exceeded
// {{{ #include <algorithm> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define FOR(i, a, b) \ for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); i++) #define FORR(i, a, b) \ for (ll i = static_cast<ll>(a); i >= static_cast<ll>(b); i--) #define REP(i, n) for (ll i = 0ll; i < static_cast<ll>(n); i++) #define REPR(i, n) for (ll i = static_cast<ll>(n); i >= 0ll; i--) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<ll, ll> LP; typedef pair<int, P> IP; typedef pair<ll, LP> LLP; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; constexpr int INF = 100000000; constexpr ll LINF = 10000000000000000ll; constexpr int MOD = static_cast<int>(1e9 + 7); constexpr double EPS = 1e-9; static inline ll mod(ll x, ll m) { ll y = x % m; return (y >= 0 ? y : y + m); } // }}} int N; int a[21][21]; int dp[25][1 << 21]; void solve() { dp[0][0] = 1; FOR(i, 1, N + 1) { REP(j, (1 << N) - 1) { REP(k, N) { if ((j & (1 << k)) == 0 && a[i - 1][k]) { dp[i][j | (1 << k)] += dp[i - 1][j]; dp[i][j | (1 << k)] %= MOD; } } } } cout << dp[N][(1 << N) - 1] << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; REP(i, N) REP(j, N) cin >> a[i][j]; solve(); return 0; } // vim:set foldmethod=marker:
// {{{ #include <algorithm> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define FOR(i, a, b) \ for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); i++) #define FORR(i, a, b) \ for (ll i = static_cast<ll>(a); i >= static_cast<ll>(b); i--) #define REP(i, n) for (ll i = 0ll; i < static_cast<ll>(n); i++) #define REPR(i, n) for (ll i = static_cast<ll>(n); i >= 0ll; i--) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<ll, ll> LP; typedef pair<int, P> IP; typedef pair<ll, LP> LLP; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; constexpr int INF = 100000000; constexpr ll LINF = 10000000000000000ll; constexpr int MOD = static_cast<int>(1e9 + 7); constexpr double EPS = 1e-9; static inline ll mod(ll x, ll m) { ll y = x % m; return (y >= 0 ? y : y + m); } // }}} int N; int a[21][21]; int dp[25][1 << 21]; void solve() { dp[0][0] = 1; FOR(i, 1, N + 1) { REP(j, (1 << N) - 1) { if (dp[i - 1][j] == 0) continue; REP(k, N) { if ((j & (1 << k)) == 0 && a[i - 1][k]) { dp[i][j | (1 << k)] += dp[i - 1][j]; dp[i][j | (1 << k)] %= MOD; } } } } cout << dp[N][(1 << N) - 1] << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; REP(i, N) REP(j, N) cin >> a[i][j]; solve(); return 0; } // vim:set foldmethod=marker:
insert
62
62
62
64
TLE
p03174
C++
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; }
#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; }
replace
23
24
23
24
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define each(i, c) for (auto &i : c) #define unless(cond) if (!(cond)) using namespace std; typedef long long int lli; typedef unsigned long long ull; typedef complex<double> point; template <typename P, typename Q> ostream &operator<<(ostream &os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <typename P, typename Q> istream &operator>>(istream &is, pair<P, Q> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> v) { os << "("; each(i, v) os << i << ","; os << ")"; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { each(i, v) is >> i; return is; } template <typename T> inline T setmax(T &a, T b) { return a = std::max(a, b); } template <typename T> inline T setmin(T &a, T b) { return a = std::min(a, b); } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); int n; while (cin >> n) { int a[n][n]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } const int mod = 1e9 + 7; const int N = 22; const int B = 1 << 22; static int dp[N][B]; fill(&dp[0][0], &dp[N - 1][B - 1] + 1, 0); dp[0][0] = 1; for (int i = 0; i < n; ++i) { for (int bit = 0; bit < (1 << n); ++bit) { for (int j = 0; j < n; ++j) { if (a[i][j] && (bit & (1 << j)) == 0) { (dp[i + 1][bit | (1 << j)] += dp[i][bit]) %= mod; } } } } cout << dp[n][(1 << n) - 1] << endl; } return 0; }
#include <bits/stdc++.h> #define each(i, c) for (auto &i : c) #define unless(cond) if (!(cond)) using namespace std; typedef long long int lli; typedef unsigned long long ull; typedef complex<double> point; template <typename P, typename Q> ostream &operator<<(ostream &os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <typename P, typename Q> istream &operator>>(istream &is, pair<P, Q> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> v) { os << "("; each(i, v) os << i << ","; os << ")"; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { each(i, v) is >> i; return is; } template <typename T> inline T setmax(T &a, T b) { return a = std::max(a, b); } template <typename T> inline T setmin(T &a, T b) { return a = std::min(a, b); } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); int n; while (cin >> n) { int a[n][n]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } const int mod = 1e9 + 7; const int N = 22; const int B = 1 << 22; static int dp[N][B]; fill(&dp[0][0], &dp[N - 1][B - 1] + 1, 0); 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 (a[i][j] && (bit & (1 << j)) == 0) { (dp[i + 1][bit | (1 << j)] += dp[i][bit]) %= mod; } } } } cout << dp[n][(1 << n) - 1] << endl; } return 0; }
insert
56
56
56
58
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> // #include "bits/stdc++.h" using namespace std; typedef long long ll; // #include "boost/multiprecision/cpp_int.hpp" // typedef boost::multiprecision::cpp_int LL; typedef long double dd; #define i_7 (ll)(1E9 + 7) // #define i_7 998244353 #define i_5 i_7 - 2 ll mod(ll a) { ll c = a % i_7; if (c >= 0) return c; return c + i_7; } typedef pair<ll, ll> l_l; typedef pair<dd, dd> d_d; ll inf = (ll)1E16; #define rep(i, l, r) for (ll i = l; i <= r; i++) #define pb push_back ll max(ll a, ll b) { if (a < b) return b; else return a; } ll min(ll a, ll b) { if (a > b) return b; else return a; } void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]); void Min(ll &pos, ll val) { pos = min(pos, val); } void Add(ll &pos, ll val) { pos = mod(pos + val); } dd EPS = 1E-9; #define fastio \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define fi first #define se second #define endl "\n" #define SORT(v) sort(v.begin(), v.end()) #define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) // template<class T>void max(T a,T b){if(a<b)return b;else return a;} // template<class T>void min(T a,T b){if(a>b)return b;else return a;} // template<class T>bool Max(T&a, T b){if(a < b){a = b;return 1;}return 0;} // template<class T>bool Min(T&a, T b){if(a > b){a = b;return 1;}return 0;} ////////////////////////// int main() { fastio ll n; cin >> n; ll a[n][n]; rep(i, 0, n - 1) rep(j, 0, n - 1) cin >> a[i][j]; ll dp[2][1 << n]; memset(dp, 0, sizeof(dp)); rep(j, 0, n - 1) { if (a[0][j] == 1) dp[0][1 << j] = 1; } rep(i, 1, n - 1) { rep(j, 0, (1 << n) - 1) { rep(op, 0, n - 1) { if (a[i][op] == 0) continue; if ((j & (1 << op)) == 0) continue; Add(dp[i & 1][j], dp[(i - 1) & 1][j - (1 << op)]); } } } cout << dp[(n - 1) & 1][(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> // #include "bits/stdc++.h" using namespace std; typedef long long ll; // #include "boost/multiprecision/cpp_int.hpp" // typedef boost::multiprecision::cpp_int LL; typedef long double dd; #define i_7 (ll)(1E9 + 7) // #define i_7 998244353 #define i_5 i_7 - 2 ll mod(ll a) { ll c = a % i_7; if (c >= 0) return c; return c + i_7; } typedef pair<ll, ll> l_l; typedef pair<dd, dd> d_d; ll inf = (ll)1E16; #define rep(i, l, r) for (ll i = l; i <= r; i++) #define pb push_back ll max(ll a, ll b) { if (a < b) return b; else return a; } ll min(ll a, ll b) { if (a > b) return b; else return a; } void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]); void Min(ll &pos, ll val) { pos = min(pos, val); } void Add(ll &pos, ll val) { pos = mod(pos + val); } dd EPS = 1E-9; #define fastio \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define fi first #define se second #define endl "\n" #define SORT(v) sort(v.begin(), v.end()) #define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) // template<class T>void max(T a,T b){if(a<b)return b;else return a;} // template<class T>void min(T a,T b){if(a>b)return b;else return a;} // template<class T>bool Max(T&a, T b){if(a < b){a = b;return 1;}return 0;} // template<class T>bool Min(T&a, T b){if(a > b){a = b;return 1;}return 0;} ////////////////////////// int main() { fastio ll n; cin >> n; ll a[n][n]; rep(i, 0, n - 1) rep(j, 0, n - 1) cin >> a[i][j]; ll dp[2][1 << n]; memset(dp, 0, sizeof(dp)); rep(j, 0, n - 1) { if (a[0][j] == 1) dp[0][1 << j] = 1; } rep(i, 1, n - 1) { rep(j, 0, (1 << n) - 1) { ll cnt = 0; rep(k, 0, n - 1) if ((j & (1 << k))) cnt++; if (cnt != i + 1) continue; rep(op, 0, n - 1) { if (a[i][op] == 0) continue; if ((j & (1 << op)) == 0) continue; Add(dp[i & 1][j], dp[(i - 1) & 1][j - (1 << op)]); } } } cout << dp[(n - 1) & 1][(1 << n) - 1] << endl; return 0; }
insert
69
69
69
73
TLE
p03174
C++
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; }
#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; }
replace
32
33
32
33
-11
p03174
C++
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; }
#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; }
replace
0
1
0
1
0
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, a) for (int i = 0; i < (a); i++) #define ALL(a) (a).begin(), (a).end() typedef long long ll; typedef pair<int, int> P; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; using ll = long long; template <ll MOD = 1000000007> struct Mint { ll x; Mint() : x(0) {} Mint(ll t) { x = t % MOD; if (x < 0) x += MOD; } Mint pow(ll n) { Mint res(1), t(x); while (n > 0) { if (n & 1) res *= t; t *= t; n >>= 1; } return res; } Mint inv() const { ll a = x, b = MOD, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return Mint(u); } Mint &operator+=(Mint a) { x += a.x; if (x >= MOD) x -= MOD; return *this; } Mint &operator-=(Mint a) { x += MOD - a.x; if (x >= MOD) x -= MOD; return *this; } Mint &operator*=(Mint a) { x = 1LL * x * a.x % MOD; return *this; } Mint &operator/=(Mint a) { return (*this) *= a.inv(); } Mint operator+(Mint a) const { return Mint(x) += a; } Mint operator-(Mint a) const { return Mint(x) -= a; } Mint operator*(Mint a) const { return Mint(x) *= a; } Mint operator/(Mint a) const { return Mint(x) /= a; } Mint operator-() const { return Mint(-x); } bool operator==(const Mint a) { return x == a.x; } bool operator!=(const Mint a) { return x != a.x; } bool operator<(const Mint a) { return x < a.x; } friend ostream &operator<<(ostream &os, const Mint &a) { return os << a.x; } friend istream &operator>>(istream &is, Mint &a) { ll t; is >> t; a = Mint<MOD>(t); return (is); } }; signed main() { int n; cin >> n; vector<int> vec[n]; REP(i, n) { REP(j, n) { int a; cin >> a; if (a == 1) { vec[i].emplace_back(1 << j); } } } vector<vector<ll>> dp(vector<vector<ll>>(n + 1, vector<ll>(1 << n, 0))); dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < vec[i].size(); j++) { for (int k = 0; k < (1 << n); k++) { dp[i + 1][k | vec[i][j]] += dp[i][k]; dp[i + 1][k | vec[i][j]] %= MOD; } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, a) for (int i = 0; i < (a); i++) #define ALL(a) (a).begin(), (a).end() typedef long long ll; typedef pair<int, int> P; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; using ll = long long; template <ll MOD = 1000000007> struct Mint { ll x; Mint() : x(0) {} Mint(ll t) { x = t % MOD; if (x < 0) x += MOD; } Mint pow(ll n) { Mint res(1), t(x); while (n > 0) { if (n & 1) res *= t; t *= t; n >>= 1; } return res; } Mint inv() const { ll a = x, b = MOD, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return Mint(u); } Mint &operator+=(Mint a) { x += a.x; if (x >= MOD) x -= MOD; return *this; } Mint &operator-=(Mint a) { x += MOD - a.x; if (x >= MOD) x -= MOD; return *this; } Mint &operator*=(Mint a) { x = 1LL * x * a.x % MOD; return *this; } Mint &operator/=(Mint a) { return (*this) *= a.inv(); } Mint operator+(Mint a) const { return Mint(x) += a; } Mint operator-(Mint a) const { return Mint(x) -= a; } Mint operator*(Mint a) const { return Mint(x) *= a; } Mint operator/(Mint a) const { return Mint(x) /= a; } Mint operator-() const { return Mint(-x); } bool operator==(const Mint a) { return x == a.x; } bool operator!=(const Mint a) { return x != a.x; } bool operator<(const Mint a) { return x < a.x; } friend ostream &operator<<(ostream &os, const Mint &a) { return os << a.x; } friend istream &operator>>(istream &is, Mint &a) { ll t; is >> t; a = Mint<MOD>(t); return (is); } }; signed main() { int n; cin >> n; vector<int> vec[n]; REP(i, n) { REP(j, n) { int a; cin >> a; if (a == 1) { vec[i].emplace_back(1 << j); } } } vector<vector<ll>> dp(vector<vector<ll>>(n + 1, vector<ll>(1 << n, 0))); dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int bit = 0; bit < (1 << n); bit++) { if (i != __builtin_popcount(bit)) continue; for (int j = 0; j < vec[i].size(); j++) { dp[i + 1][bit | vec[i][j]] += dp[i][bit]; dp[i + 1][bit | vec[i][j]] %= MOD; } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
replace
99
103
99
105
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/hash_map> #include <ext/numeric> using namespace std; using namespace __gnu_cxx; #define REP(i, n) for ((i) = 0; (i) < (n); (i)++) #define rep(i, x, n) for ((i) = (x); (i) < (n); (i)++) #define REV(i, n) for ((i) = (n); (i) >= 0; (i)--) #define FORIT(it, x) for ((it) = (x).begin(); (it) != (x).end(); (it)++) #define foreach(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define rforeach(it, c) \ for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define foreach2d(i, j, v) \ foreach (i, v) \ foreach (j, *i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define SZ(x) ((int)(x).size()) #define MMS(x, n) memset(x, n, sizeof(x)) #define mms(x, n, s) memset(x, n, sizeof(x) * s) #define pb push_back #define mp make_pair #define NX next_permutation #define UN(x) sort(all(x)), x.erase(unique(all(x)), x.end()) #define CV(x, n) count(all(x), (n)) #define FIND(x, n) find(all(x), (n)) - (x).begin() #define ACC(x) accumulate(all(x), 0) #define PPC(x) __builtin_popcountll(x) #define LZ(x) __builtin_clz(x) #define TZ(x) __builtin_ctz(x) #define mxe(x) *max_element(all(x)) #define mne(x) *min_element(all(x)) #define low(x, i) lower_bound(all(x), i) #define upp(x, i) upper_bound(all(x), i) #define NXPOW2(x) (1ll << ((int)log2(x) + 1)) #define PR(x) cout << #x << " = " << (x) << endl; typedef unsigned long long ull; typedef long long ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<int, int> pii; const int OO = (int)2e9; const double eps = 1e-9; const int N = 22; const int mod = (int)1e9 + 7; int n; int a[N][N]; int dp[2][1 << (N - 1)]; int LOG[(1 << N) + 1]; // int calc(int idx, int msk) { // if (idx == n) // return msk == (1 << n) - 1; // int &ret = dp[idx][msk]; // if (ret != -1) // return ret; // ret = 0; // for (int i = 0; i < n; i++) { // if (!(msk & (1 << i)) && a[idx][i]) { // (ret += calc(idx + 1, msk | (1 << i))) %= mod; // } // } // return ret; // } int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE // freopen("in.txt", "rt", stdin); // freopen("out.txt", "wt", stdout); #endif cin >> 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++) LOG[1 << i] = i; // MMS(dp, -1); // cout << calc(0, 0) << endl; dp[n & 1][(1 << n) - 1] = 1; for (int idx = n - 1; idx >= 0; idx--) { for (int msk = 0; msk < (1 << n); msk++) { dp[idx & 1][msk] = 0; int curMsk = (~msk) & ((1 << n) - 1); while (curMsk) { int i = LOG[(curMsk & (-curMsk))]; if (a[idx][i]) { (dp[idx & 1][msk] += dp[(idx + 1) & 1][msk | (1 << i)]) %= mod; } curMsk -= curMsk & (-curMsk); } } } cout << dp[0][0] << endl; return 0; }
#include <bits/stdc++.h> #include <ext/hash_map> #include <ext/numeric> using namespace std; using namespace __gnu_cxx; #define REP(i, n) for ((i) = 0; (i) < (n); (i)++) #define rep(i, x, n) for ((i) = (x); (i) < (n); (i)++) #define REV(i, n) for ((i) = (n); (i) >= 0; (i)--) #define FORIT(it, x) for ((it) = (x).begin(); (it) != (x).end(); (it)++) #define foreach(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define rforeach(it, c) \ for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define foreach2d(i, j, v) \ foreach (i, v) \ foreach (j, *i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define SZ(x) ((int)(x).size()) #define MMS(x, n) memset(x, n, sizeof(x)) #define mms(x, n, s) memset(x, n, sizeof(x) * s) #define pb push_back #define mp make_pair #define NX next_permutation #define UN(x) sort(all(x)), x.erase(unique(all(x)), x.end()) #define CV(x, n) count(all(x), (n)) #define FIND(x, n) find(all(x), (n)) - (x).begin() #define ACC(x) accumulate(all(x), 0) #define PPC(x) __builtin_popcountll(x) #define LZ(x) __builtin_clz(x) #define TZ(x) __builtin_ctz(x) #define mxe(x) *max_element(all(x)) #define mne(x) *min_element(all(x)) #define low(x, i) lower_bound(all(x), i) #define upp(x, i) upper_bound(all(x), i) #define NXPOW2(x) (1ll << ((int)log2(x) + 1)) #define PR(x) cout << #x << " = " << (x) << endl; typedef unsigned long long ull; typedef long long ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<int, int> pii; const int OO = (int)2e9; const double eps = 1e-9; const int N = 22; const int mod = (int)1e9 + 7; int n; int a[N][N]; int dp[2][1 << (N - 1)]; int LOG[(1 << N) + 1]; // int calc(int idx, int msk) { // if (idx == n) // return msk == (1 << n) - 1; // int &ret = dp[idx][msk]; // if (ret != -1) // return ret; // ret = 0; // for (int i = 0; i < n; i++) { // if (!(msk & (1 << i)) && a[idx][i]) { // (ret += calc(idx + 1, msk | (1 << i))) %= mod; // } // } // return ret; // } int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE // freopen("in.txt", "rt", stdin); // freopen("out.txt", "wt", stdout); #endif cin >> 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++) LOG[1 << i] = i; // MMS(dp, -1); // cout << calc(0, 0) << endl; dp[n & 1][(1 << n) - 1] = 1; for (int idx = n - 1; idx >= 0; idx--) { for (int msk = 0; msk < (1 << n); msk++) { dp[idx & 1][msk] = 0; int curMsk = (~msk) & ((1 << n) - 1); while (curMsk) { int i = LOG[(curMsk & (-curMsk))]; if (a[idx][i]) { dp[idx & 1][msk] += dp[(idx + 1) & 1][msk | (1 << i)]; if (dp[idx & 1][msk] >= mod) dp[idx & 1][msk] -= mod; } curMsk -= curMsk & (-curMsk); } } } cout << dp[0][0] << endl; return 0; }
replace
96
97
96
99
TLE
p03174
C++
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; }
#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; }
replace
24
25
24
29
TLE
p03174
C++
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)); }
#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)); }
replace
48
49
48
49
TLE
p03174
C++
Time Limit Exceeded
// #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC optimize(3) // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("sse3","sse2","sse") // #pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3") // #pragma GCC target("f16c") // #pragma GCC // optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma // GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error // "-fcse-skip-blocks" #pragma GCC diagnostic error // "-funsafe-loop-optimizations" #pragma GCC diagnostic error "-std=c++14" #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x, "r", stdin) #define fw(x) freopen(x, "w", stdout) #define iout(x) printf("%d\n", x) #define lout(x) printf("%lld\n", x) #define REP(x, l, u) for (ll x = l; x < u; x++) #define RREP(x, l, u) for (ll x = l; x >= u; x--) #define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end()) #define mst(x, a) memset(x, a, sizeof(x)) #define all(a) begin(a), end(a) #define PII pair<int, int> #define PLL pair<ll, ll> #define MP make_pair #define lowbit(x) ((x) & (-(x))) #define lson (ind << 1) #define rson (ind << 1 | 1) #define se second #define fi first #define sz(x) ((int)x.size()) #define EX0 exit(0); typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace __gnu_pbds; // required using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef vector<ll> VLL; typedef vector<int> VI; const int block_size = 320; typedef complex<ll> point; const ll mod = 1e9 + 7; const ll inf = 1e9 + 7; const ld eps = 1e-9; const db PI = atan(1) * 4; template <typename T> inline int sign(const T &a) { if (a < 0) return -1; if (a > 0) return 1; return 0; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) \ {} #endif template <typename T, typename S> inline bool upmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> inline bool upmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline void in(T &x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } ull twop(ll x) { return 1ULL << x; } ll MOD(ll a, ll m) { a %= m; if (a < 0) a += m; return a; } ll inverse(ll a, ll m) { a = MOD(a, m); if (a <= 1) return a; return MOD((1 - inverse(m, a) * m) / a, m); } template <typename A, typename B> inline void in(A &x, B &y) { in(x); in(y); } template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) { in(x); in(y); in(z); } template <typename A, typename B, typename C, typename D> inline void in(A &x, B &y, C &z, D &d) { in(x); in(y); in(z); in(d); } template <typename T> T sqr(T x) { return x * x; } ll gcd(ll a, ll b) { while (b != 0) { a %= b; swap(a, b); } return abs(a); } ll fast(ll a, ll b, ll mod) { if (b < 0) a = inverse(a, mod), b = -b; ll ans = 1; while (b) { if (b & 1) { b--; ans = ans * a % mod; } else { a = a * a % mod; b /= 2; } } return ans % mod; } namespace SOLVE { bool connected[50][50]; int n; int dp[22][(1 << 21) + 5]; void main() { cin >> n; REP(i, 0, n) { REP(j, 0, n) cin >> connected[i][j]; } dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int mask = 0; mask < (1 << n); mask++) { for (int j = 0; j < n; j++) { if (connected[i][j] and (mask & (1 << j)) != 0) { dp[i + 1][mask] += dp[i][mask - (1 << j)]; if (dp[i + 1][mask] >= mod) { dp[i + 1][mask] -= mod; } } } } } cout << dp[n][(1 << n) - 1] << endl; } } // namespace SOLVE signed main() { #ifndef ONLINE_JUDGE #endif int t = 1; // in(t); for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<":"; SOLVE::main(); } // clock_t st = clock(); // while(clock() - st < 3.0 * CLOCKS_PER_SEC){ // // } return 0; }
// #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC optimize(3) // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("sse3","sse2","sse") // #pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3") // #pragma GCC target("f16c") // #pragma GCC // optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma // GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error // "-fcse-skip-blocks" #pragma GCC diagnostic error // "-funsafe-loop-optimizations" #pragma GCC diagnostic error "-std=c++14" #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x, "r", stdin) #define fw(x) freopen(x, "w", stdout) #define iout(x) printf("%d\n", x) #define lout(x) printf("%lld\n", x) #define REP(x, l, u) for (ll x = l; x < u; x++) #define RREP(x, l, u) for (ll x = l; x >= u; x--) #define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end()) #define mst(x, a) memset(x, a, sizeof(x)) #define all(a) begin(a), end(a) #define PII pair<int, int> #define PLL pair<ll, ll> #define MP make_pair #define lowbit(x) ((x) & (-(x))) #define lson (ind << 1) #define rson (ind << 1 | 1) #define se second #define fi first #define sz(x) ((int)x.size()) #define EX0 exit(0); typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace __gnu_pbds; // required using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef vector<ll> VLL; typedef vector<int> VI; const int block_size = 320; typedef complex<ll> point; const ll mod = 1e9 + 7; const ll inf = 1e9 + 7; const ld eps = 1e-9; const db PI = atan(1) * 4; template <typename T> inline int sign(const T &a) { if (a < 0) return -1; if (a > 0) return 1; return 0; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) \ {} #endif template <typename T, typename S> inline bool upmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> inline bool upmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline void in(T &x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } ull twop(ll x) { return 1ULL << x; } ll MOD(ll a, ll m) { a %= m; if (a < 0) a += m; return a; } ll inverse(ll a, ll m) { a = MOD(a, m); if (a <= 1) return a; return MOD((1 - inverse(m, a) * m) / a, m); } template <typename A, typename B> inline void in(A &x, B &y) { in(x); in(y); } template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) { in(x); in(y); in(z); } template <typename A, typename B, typename C, typename D> inline void in(A &x, B &y, C &z, D &d) { in(x); in(y); in(z); in(d); } template <typename T> T sqr(T x) { return x * x; } ll gcd(ll a, ll b) { while (b != 0) { a %= b; swap(a, b); } return abs(a); } ll fast(ll a, ll b, ll mod) { if (b < 0) a = inverse(a, mod), b = -b; ll ans = 1; while (b) { if (b & 1) { b--; ans = ans * a % mod; } else { a = a * a % mod; b /= 2; } } return ans % mod; } namespace SOLVE { bool connected[50][50]; int n; int dp[22][(1 << 21) + 5]; void main() { cin >> n; REP(i, 0, n) { REP(j, 0, n) cin >> connected[i][j]; } dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int mask = 0; mask < (1 << n); mask++) { if (__builtin_popcount(mask) == i + 1) { for (int j = 0; j < n; j++) { if (connected[i][j] and (mask & (1 << j)) != 0) { dp[i + 1][mask] += dp[i][mask - (1 << j)]; if (dp[i + 1][mask] >= mod) { dp[i + 1][mask] -= mod; } } } } } } cout << dp[n][(1 << n) - 1] << endl; } } // namespace SOLVE signed main() { #ifndef ONLINE_JUDGE #endif int t = 1; // in(t); for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<":"; SOLVE::main(); } // clock_t st = clock(); // while(clock() - st < 3.0 * CLOCKS_PER_SEC){ // // } return 0; }
replace
200
205
200
207
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long int #define vl vector<long long int> #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define all(x) x.begin(), x.end() #define vi vector<int> #define vb vector<bool> #define vvl vector<vector<ll>> #define vvi vector<vector<int>> #define pl pair<ll, ll> #define pb push_back #define PI 3.14159265 #define mod 1000000007 #define pb push_back #define mp make_pair #define fri(s, n) for (int i = s; i < n; i++) #define frj(s, n) for (int j = s; j < n; j++) #define T(i) \ int i = 1; \ cin >> i; \ while (i--) #define vsi vector<set<int>> #define pii pair<int, int> #define inf 1e9 #define vpii vector<pair<int, int>> ll power(ll a, ll b) { if (b == 0) return 1; if (b & 1) return a * power(a, b - 1); ll temp = power(a, b / 2); return temp * temp; } bool mycompare(ll a, ll b) { return a > b; } int dp[22][(1 << 22)]; int n; int solve(vvi relation, int wset, int men) { if (men == n + 1) { if (wset == 0) return 1; return 0; } if (dp[men][wset] != -1) return dp[men][wset]; int ans = 0; for (int w = 0; w < n; w++) { bool present = (1 << w) & wset; if (present && relation[men][w + 1] == 1) { ans += solve(relation, wset ^ (1 << w), men + 1); ans %= mod; } } return dp[men][wset] = ans; } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); cin >> n; vvi relation(n + 1, vi(n + 1)); memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> relation[i][j]; } } cout << solve(relation, (1 << n) - 1, 1); }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define vl vector<long long int> #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define all(x) x.begin(), x.end() #define vi vector<int> #define vb vector<bool> #define vvl vector<vector<ll>> #define vvi vector<vector<int>> #define pl pair<ll, ll> #define pb push_back #define PI 3.14159265 #define mod 1000000007 #define pb push_back #define mp make_pair #define fri(s, n) for (int i = s; i < n; i++) #define frj(s, n) for (int j = s; j < n; j++) #define T(i) \ int i = 1; \ cin >> i; \ while (i--) #define vsi vector<set<int>> #define pii pair<int, int> #define inf 1e9 #define vpii vector<pair<int, int>> ll power(ll a, ll b) { if (b == 0) return 1; if (b & 1) return a * power(a, b - 1); ll temp = power(a, b / 2); return temp * temp; } bool mycompare(ll a, ll b) { return a > b; } int dp[22][(1 << 22)]; int n; int solve(vvi &relation, int wset, int men) { if (men == n + 1) { if (wset == 0) return 1; return 0; } if (dp[men][wset] != -1) return dp[men][wset]; int ans = 0; for (int w = 0; w < n; w++) { bool present = (1 << w) & wset; if (present && relation[men][w + 1] == 1) { ans += solve(relation, wset ^ (1 << w), men + 1); ans %= mod; } } return dp[men][wset] = ans; } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); cin >> n; vvi relation(n + 1, vi(n + 1)); memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> relation[i][j]; } } cout << solve(relation, (1 << n) - 1, 1); }
replace
41
42
41
42
TLE
p03174
C++
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; }
#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; }
insert
29
29
29
32
TLE
p03174
C++
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; }
#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; }
replace
6
8
6
8
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define M_PI 3.14159265358979323846 // pi using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<ll> VI; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> t3; typedef tuple<ll, ll, ll, ll> t4; typedef tuple<ll, ll, ll, ll, ll> t5; #define rep(a, n) for (ll a = 0; a < n; a++) #define repi(a, b, n) for (ll a = b; a < n; a++) using namespace std; static const ll INF = 1e15; template <typename T> static inline void chmax(T &ref, const T value) { if (ref < value) ref = value; } const ll mod = 1000000007; class modint { static constexpr std::int_fast64_t Modulus = 1e9 + 7; using u64 = std::int_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } }; ostream &operator<<(ostream &stream, const modint &data) { stream << data.a; return stream; } int main() { ll n; cin >> n; vector<vector<bool>> match(n, vector<bool>(n, false)); rep(i, n) { rep(j, n) { int ok; cin >> ok; match[i][j] = ok; } } vector<vector<modint>> dp(n + 1, vector<modint>(1LL << n, 0)); dp[0][0] = 1; // dp[i][j]...i番目までみてj状態になる組み合わせの数 rep(i, n) { rep(j, 1LL << n) { rep(a, n) { if (!match[i][a]) continue; if (j & (1LL << a)) continue; dp[i + 1][j | (1LL << a)] += dp[i][j]; } } } cout << dp[n][(1LL << n) - 1LL] << endl; return 0; }
#include <bits/stdc++.h> #define M_PI 3.14159265358979323846 // pi using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<ll> VI; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> t3; typedef tuple<ll, ll, ll, ll> t4; typedef tuple<ll, ll, ll, ll, ll> t5; #define rep(a, n) for (ll a = 0; a < n; a++) #define repi(a, b, n) for (ll a = b; a < n; a++) using namespace std; static const ll INF = 1e15; template <typename T> static inline void chmax(T &ref, const T value) { if (ref < value) ref = value; } const ll mod = 1000000007; class modint { static constexpr std::int_fast64_t Modulus = 1e9 + 7; using u64 = std::int_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } }; ostream &operator<<(ostream &stream, const modint &data) { stream << data.a; return stream; } int main() { ll n; cin >> n; vector<vector<bool>> match(n, vector<bool>(n, false)); rep(i, n) { rep(j, n) { int ok; cin >> ok; match[i][j] = ok; } } vector<vector<modint>> dp(n + 1, vector<modint>(1LL << n, 0)); dp[0][0] = 1; // dp[i][j]...i番目までみてj状態になる組み合わせの数 rep(i, n) { rep(j, 1LL << n) { auto p = __builtin_popcountll(j); if (p != i) continue; rep(a, n) { if (!match[i][a]) continue; if (j & (1LL << a)) continue; dp[i + 1][j | (1LL << a)] += dp[i][j]; } } } cout << dp[n][(1LL << n) - 1LL] << endl; return 0; }
insert
86
86
86
89
TLE
p03174
C++
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; }
#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; }
delete
21
22
21
21
TLE
p03174
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; using ll = long long; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vvvll = std::vector<vvll>; using dd = double; using vdd = std::vector<dd>; using vvdd = std::vector<vdd>; using vvvdd = std::vector<vvdd>; constexpr ll INF = 1LL << 60; constexpr dd EPS = 1e-11; // cin,cout高速化のおまじない+桁数指定 struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; #define REPS(i, S, E) for (ll i = (S); i <= (E); i++) #define REP(i, N) REPS(i, 0, N - 1) #define DEPS(i, S, E) for (ll i = (E); i >= (S); i--) #define DEP(i, N) DEPS(i, 0, N - 1) #define EACH(e, v) for (auto &&e : v) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline T MaxE(vector<T> &v, ll S, ll E) { T m = v[S]; REPS(i, S, E) chmax(m, v[i]); return m; } // v[S]~v[E]の最大値 template <class T> inline T MinE(vector<T> &v, ll S, ll E) { T m = v[S]; REPS(i, S, E) chmin(m, v[i]); return m; } // v[S]~v[E]の最小値 template <class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } // 先頭N個中の最大値 template <class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); } template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); } template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); } template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) { ll m = S; REPS(i, S, E) { if (v[i] > v[m]) m = i; } return m; } template <class T> inline ll MinI(vector<T> &v, ll S, ll E) { ll m = S; REPS(i, S, E) { if (v[i] < v[m]) m = i; } return m; } template <class T> inline ll MaxI(vector<T> &v, ll N) { return MaxI(v, 0, N - 1); } template <class T> inline ll MinI(vector<T> &v, ll N) { return MinI(v, 0, N - 1); } template <class T> inline ll MaxI(vector<T> &v) { return MaxI(v, (ll)v.size()); } template <class T> inline ll MinI(vector<T> &v) { return MinI(v, (ll)v.size()); } template <class T> inline T Sum(vector<T> &v, ll S, ll E) { T s = v[S]; REPS(i, S + 1, E) s += v[i]; return s; } template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); } template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); } template <class T> inline T POW(T a, ll n) { T r = 1; for (; n > 0; n >>= 1, a *= a) { if (n & 1) r *= a; } return r; } inline ll POW(int a, ll n) { return POW((ll)a, n); } inline ll MSB(ll a) { for (ll o = 63, x = -1;;) { ll m = (o + x) / 2; if (a < (1LL << m)) o = m; else x = m; if (o - x == 1) return x; } } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } template <class T = ll> inline vector<T> cinv(ll N) { vector<T> v(N); REP(i, N) cin >> v[i]; return move(v); } template <class T = ll, class S = ll> inline vector<pair<T, S>> cinv2(ll N) { vector<pair<T, S>> v(N); REP(i, N) { cin >> v[i].first >> v[i].second; } return move(v); } template <class T = ll, class S = ll, class R = ll> inline vector<tuple<T, S, R>> cinv3(ll N) { vector<tuple<T, S, R>> v(N); REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); } return move(v); } template <class T = ll, class S = ll, class R = ll, class Q = ll> inline vector<tuple<T, S, R, Q>> cinv4(ll N) { vector<tuple<T, S, R, Q>> v(N); REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]); } return move(v); } template <class T> inline void coutv(vector<T> &v, char deli = ' ') { ll N = (ll)v.size(); REP(i, N) { cout << v[i] << ((i == N - 1) ? '\n' : deli); } } template <class T> void bye(T a) { cout << a << '\n'; exit(0); } struct mll { static ll MOD; ll val; mll(ll v = 0) : val(v % MOD) { if (val < 0) val += MOD; } mll operator-() const { return -val; } mll operator+(const mll &b) const { return val + b.val; } mll operator-(const mll &b) const { return val - b.val; } mll operator*(const mll &b) const { return val * b.val; } mll operator/(const mll &b) const { return mll(*this) /= b; } mll operator+(ll b) const { return *this + mll(b); } mll operator-(ll b) const { return *this - mll(b); } mll operator*(ll b) const { return *this * mll(b); } friend mll operator+(ll a, const mll &b) { return b + a; } friend mll operator-(ll a, const mll &b) { return -b + a; } friend mll operator*(ll a, const mll &b) { return b * a; } friend mll operator/(ll a, const mll &b) { return mll(a) / b; } mll &operator+=(const mll &b) { val = (val + b.val) % MOD; return *this; } mll &operator-=(const mll &b) { val = (val + MOD - b.val) % MOD; return *this; } mll &operator*=(const mll &b) { val = (val * b.val) % MOD; return *this; } mll &operator/=(const mll &b) { ll c = b.val, d = MOD, u = 1, v = 0; while (d) { ll t = c / d; c -= t * d; swap(c, d); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } mll &operator+=(ll b) { return *this += mll(b); } mll &operator-=(ll b) { return *this -= mll(b); } mll &operator*=(ll b) { return *this *= mll(b); } mll &operator/=(ll b) { return *this /= mll(b); } bool operator==(const mll &b) { return val == b.val; } bool operator!=(const mll &b) { return val != b.val; } bool operator==(ll b) { return *this == mll(b); } bool operator!=(ll b) { return *this != mll(b); } friend bool operator==(ll a, const mll &b) { return mll(a) == b.val; } friend bool operator!=(ll a, const mll &b) { return mll(a) != b.val; } friend ostream &operator<<(ostream &os, const mll &a) { return os << a.val; } friend istream &operator>>(istream &is, mll &a) { return is >> a.val; } static mll Combination(ll a, ll b) { chmin(b, a - b); if (b < 0) return mll(0); mll c = 1; REP(i, b) c *= a - i; REP(i, b) c /= i + 1; return c; } }; ll mll::MOD = 1000000007LL; // 998244353LL;//1000000007LL;(ll)(1e9 + 7); using vmll = std::vector<mll>; using vvmll = std::vector<vmll>; using vvvmll = std::vector<vvmll>; using vvvvmll = std::vector<vvvmll>; void solve() { ll N; cin >> N; vvll a(N); REP(i, N) { a[i] = cinv(N); } vector<map<ll, mll>> dp(N); REP(j, N) { if (a[0][j] == 0) continue; dp[0][1LL << j] = 1; } REPS(i, 1, N - 1) { EACH(e, dp[i - 1]) { ll bit = e.first; mll count = e.second; REP(j, N) { if (a[i][j] == 0) continue; ll mask = (1LL << j); if (bit & mask) continue; dp[i][bit | mask] += count; } } } mll ans = dp[N - 1][(1LL << N) - 1]; cout << ans << '\n'; } int main() { solve(); return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vvvll = std::vector<vvll>; using dd = double; using vdd = std::vector<dd>; using vvdd = std::vector<vdd>; using vvvdd = std::vector<vvdd>; constexpr ll INF = 1LL << 60; constexpr dd EPS = 1e-11; // cin,cout高速化のおまじない+桁数指定 struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; #define REPS(i, S, E) for (ll i = (S); i <= (E); i++) #define REP(i, N) REPS(i, 0, N - 1) #define DEPS(i, S, E) for (ll i = (E); i >= (S); i--) #define DEP(i, N) DEPS(i, 0, N - 1) #define EACH(e, v) for (auto &&e : v) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline T MaxE(vector<T> &v, ll S, ll E) { T m = v[S]; REPS(i, S, E) chmax(m, v[i]); return m; } // v[S]~v[E]の最大値 template <class T> inline T MinE(vector<T> &v, ll S, ll E) { T m = v[S]; REPS(i, S, E) chmin(m, v[i]); return m; } // v[S]~v[E]の最小値 template <class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } // 先頭N個中の最大値 template <class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); } template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); } template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); } template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) { ll m = S; REPS(i, S, E) { if (v[i] > v[m]) m = i; } return m; } template <class T> inline ll MinI(vector<T> &v, ll S, ll E) { ll m = S; REPS(i, S, E) { if (v[i] < v[m]) m = i; } return m; } template <class T> inline ll MaxI(vector<T> &v, ll N) { return MaxI(v, 0, N - 1); } template <class T> inline ll MinI(vector<T> &v, ll N) { return MinI(v, 0, N - 1); } template <class T> inline ll MaxI(vector<T> &v) { return MaxI(v, (ll)v.size()); } template <class T> inline ll MinI(vector<T> &v) { return MinI(v, (ll)v.size()); } template <class T> inline T Sum(vector<T> &v, ll S, ll E) { T s = v[S]; REPS(i, S + 1, E) s += v[i]; return s; } template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); } template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); } template <class T> inline T POW(T a, ll n) { T r = 1; for (; n > 0; n >>= 1, a *= a) { if (n & 1) r *= a; } return r; } inline ll POW(int a, ll n) { return POW((ll)a, n); } inline ll MSB(ll a) { for (ll o = 63, x = -1;;) { ll m = (o + x) / 2; if (a < (1LL << m)) o = m; else x = m; if (o - x == 1) return x; } } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } template <class T = ll> inline vector<T> cinv(ll N) { vector<T> v(N); REP(i, N) cin >> v[i]; return move(v); } template <class T = ll, class S = ll> inline vector<pair<T, S>> cinv2(ll N) { vector<pair<T, S>> v(N); REP(i, N) { cin >> v[i].first >> v[i].second; } return move(v); } template <class T = ll, class S = ll, class R = ll> inline vector<tuple<T, S, R>> cinv3(ll N) { vector<tuple<T, S, R>> v(N); REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); } return move(v); } template <class T = ll, class S = ll, class R = ll, class Q = ll> inline vector<tuple<T, S, R, Q>> cinv4(ll N) { vector<tuple<T, S, R, Q>> v(N); REP(i, N) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]); } return move(v); } template <class T> inline void coutv(vector<T> &v, char deli = ' ') { ll N = (ll)v.size(); REP(i, N) { cout << v[i] << ((i == N - 1) ? '\n' : deli); } } template <class T> void bye(T a) { cout << a << '\n'; exit(0); } struct mll { static ll MOD; ll val; mll(ll v = 0) : val(v % MOD) { if (val < 0) val += MOD; } mll operator-() const { return -val; } mll operator+(const mll &b) const { return val + b.val; } mll operator-(const mll &b) const { return val - b.val; } mll operator*(const mll &b) const { return val * b.val; } mll operator/(const mll &b) const { return mll(*this) /= b; } mll operator+(ll b) const { return *this + mll(b); } mll operator-(ll b) const { return *this - mll(b); } mll operator*(ll b) const { return *this * mll(b); } friend mll operator+(ll a, const mll &b) { return b + a; } friend mll operator-(ll a, const mll &b) { return -b + a; } friend mll operator*(ll a, const mll &b) { return b * a; } friend mll operator/(ll a, const mll &b) { return mll(a) / b; } mll &operator+=(const mll &b) { val = (val + b.val) % MOD; return *this; } mll &operator-=(const mll &b) { val = (val + MOD - b.val) % MOD; return *this; } mll &operator*=(const mll &b) { val = (val * b.val) % MOD; return *this; } mll &operator/=(const mll &b) { ll c = b.val, d = MOD, u = 1, v = 0; while (d) { ll t = c / d; c -= t * d; swap(c, d); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } mll &operator+=(ll b) { return *this += mll(b); } mll &operator-=(ll b) { return *this -= mll(b); } mll &operator*=(ll b) { return *this *= mll(b); } mll &operator/=(ll b) { return *this /= mll(b); } bool operator==(const mll &b) { return val == b.val; } bool operator!=(const mll &b) { return val != b.val; } bool operator==(ll b) { return *this == mll(b); } bool operator!=(ll b) { return *this != mll(b); } friend bool operator==(ll a, const mll &b) { return mll(a) == b.val; } friend bool operator!=(ll a, const mll &b) { return mll(a) != b.val; } friend ostream &operator<<(ostream &os, const mll &a) { return os << a.val; } friend istream &operator>>(istream &is, mll &a) { return is >> a.val; } static mll Combination(ll a, ll b) { chmin(b, a - b); if (b < 0) return mll(0); mll c = 1; REP(i, b) c *= a - i; REP(i, b) c /= i + 1; return c; } }; ll mll::MOD = 1000000007LL; // 998244353LL;//1000000007LL;(ll)(1e9 + 7); using vmll = std::vector<mll>; using vvmll = std::vector<vmll>; using vvvmll = std::vector<vvmll>; using vvvvmll = std::vector<vvvmll>; void solve() { ll N; cin >> N; vvll a(N); REP(i, N) { a[i] = cinv(N); } vector<unordered_map<ll, mll>> dp(N); REP(i, N) dp[i].reserve(353000); REP(j, N) { if (a[0][j] == 0) continue; dp[0][1LL << j] = 1; } REPS(i, 1, N - 1) { EACH(e, dp[i - 1]) { ll bit = e.first; mll count = e.second; REP(j, N) { if (a[i][j] == 0) continue; ll mask = (1LL << j); if (bit & mask) continue; dp[i][bit | mask] += count; } } } mll ans = dp[N - 1][(1LL << N) - 1]; cout << ans << '\n'; } int main() { solve(); return 0; }
replace
230
231
230
232
TLE
p03174
C++
Time Limit Exceeded
const long long MOD = 1000000007; #include <bits/stdc++.h> using namespace std; using ll = int_fast64_t; using ld = long double; using pll = pair<ll, ll>; using pld = pair<ld, ld>; const ll INF = 1LL << 60; void solve(); int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); } #define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT #define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__) #define _rep0(i, n) for (ll i = 0; i < n; ++i) #define _rep1(i, k, n) for (ll i = k; i < n; ++i) #define _rep2(i, k, n, d) \ for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d) template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <class... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void contout(const T &v) { for (auto it = v.begin(); it != v.end(); ++it, cout << (it != v.end() ? " " : "\n")) cout << *it; } void solve() { ll n; cin >> n; auto A = make_v(n, n, (ll)0); rep(i, n) rep(j, n) cin >> A[i][j]; ll N = 1 << n; vector<ll> dp(N, 0); dp[N - 1] = 1; rep(i, n) { vector<ll> ndp(N, 0); rep(mask, N) rep(j, n) { if (A[i][j] && ((mask >> j) & 1)) { ndp[mask ^ (1 << j)] += dp[mask]; ndp[mask ^ (1 << j)] %= MOD; } } dp = ndp; } cout << dp[0] << "\n"; }
const long long MOD = 1000000007; #include <bits/stdc++.h> using namespace std; using ll = int_fast64_t; using ld = long double; using pll = pair<ll, ll>; using pld = pair<ld, ld>; const ll INF = 1LL << 60; void solve(); int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); } #define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT #define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__) #define _rep0(i, n) for (ll i = 0; i < n; ++i) #define _rep1(i, k, n) for (ll i = k; i < n; ++i) #define _rep2(i, k, n, d) \ for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d) template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <class... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> void contout(const T &v) { for (auto it = v.begin(); it != v.end(); ++it, cout << (it != v.end() ? " " : "\n")) cout << *it; } void solve() { ll n; cin >> n; auto A = make_v(n, n, (ll)0); rep(i, n) rep(j, n) cin >> A[i][j]; ll N = 1 << n; vector<ll> dp(N, 0); dp[N - 1] = 1; rep(i, n) { vector<ll> ndp(N, 0); rep(mask, N) { if (__builtin_popcount(mask) != (n - i)) continue; rep(j, n) { if (A[i][j] && ((mask >> j) & 1)) { ndp[mask ^ (1 << j)] += dp[mask]; ndp[mask ^ (1 << j)] %= MOD; } } } dp = ndp; } cout << dp[0] << "\n"; }
replace
55
59
55
63
TLE
p03174
C++
Runtime Error
// People only find me interesting because they can't tell whether I'm serious // or not #include <boost/multiprecision/cpp_int.hpp> #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" typedef long long int ll; typedef double db; typedef __int128 s128; typedef __uint128_t u128; typedef unsigned long long u64; #define int long long int #define P 1000000007 #define Q 1000000006 #define sz(a) (ll) a.size() #define lt(a) (ll) a.length() #define pll pair<ll, ll> #define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) #define bac(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--) #define mll map<ll, ll> #define umll unordered_map<ll, ll> #define vl vector<ll> #define vvl vector<vector<ll>> #define pb push_back #define lb lower_bound #define ub upper_bound #define all(a) a.begin(), a.end() #define F first #define S second #define teevra_gati \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define N 100005 #define PI 3.14159265359 #define inf LLONG_MAX // using namespace boost::multiprecision; using namespace __gnu_pbds; using namespace std; ll dp[22][(1 << 22)]; vl adj[22]; void solve() { f(i, 0, (1 << 22)) { ll p = __builtin_popcount(i); adj[p].pb(i); } ll n; cin >> n; ll a[n + 1][n + 1]; f(i, 1, n + 1) { f(j, 1, n + 1) { cin >> a[i][j]; } } dp[0][0] = 1; f(i, 1, n + 1) { for (auto u : adj[i]) { f(j, 1, n + 1) { if (a[i][j] && (u & (1 << j))) { ll s = ~(1 << j); s &= u; (dp[i][u] += dp[i - 1][s]) %= P; } } } } cout << dp[n][(1 << (n + 1)) - 2]; } signed main() { teevra_gati #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); return 0; }
// People only find me interesting because they can't tell whether I'm serious // or not #include <boost/multiprecision/cpp_int.hpp> #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" typedef long long int ll; typedef double db; typedef __int128 s128; typedef __uint128_t u128; typedef unsigned long long u64; #define int long long int #define P 1000000007 #define Q 1000000006 #define sz(a) (ll) a.size() #define lt(a) (ll) a.length() #define pll pair<ll, ll> #define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) #define bac(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--) #define mll map<ll, ll> #define umll unordered_map<ll, ll> #define vl vector<ll> #define vvl vector<vector<ll>> #define pb push_back #define lb lower_bound #define ub upper_bound #define all(a) a.begin(), a.end() #define F first #define S second #define teevra_gati \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define N 100005 #define PI 3.14159265359 #define inf LLONG_MAX // using namespace boost::multiprecision; using namespace __gnu_pbds; using namespace std; ll dp[22][(1 << 22)]; vl adj[22]; void solve() { f(i, 0, (1 << 22)) { ll p = __builtin_popcount(i); adj[p].pb(i); } ll n; cin >> n; ll a[n + 1][n + 1]; f(i, 1, n + 1) { f(j, 1, n + 1) { cin >> a[i][j]; } } dp[0][0] = 1; f(i, 1, n + 1) { for (auto u : adj[i]) { f(j, 1, n + 1) { if (a[i][j] && (u & (1 << j))) { ll s = ~(1 << j); s &= u; (dp[i][u] += dp[i - 1][s]) %= P; } } } } cout << dp[n][(1 << (n + 1)) - 2]; } signed main() { teevra_gati solve(); return 0; }
delete
70
74
70
70
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define vout(x) rep(i, x.size()) cout << x[i] << " " template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } using namespace std; using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int, int>; const int inf = 1e9; const ll inf_l = 1e18; const int MAX = 1e5; const int mod = 1e9 + 7; struct mint { ll x; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } mint dp[25][1 << 21]; int main() { int n; cin >> n; vvint a(n, vint(n)); rep(i, n) rep(j, n) cin >> a[i][j]; dp[0][0] = 1; REP(i, n + 1) { rep(j, 1 << n) { rep(k, n) { if (a[i - 1][k] == 1) dp[i][j | (1 << k)] += dp[i - 1][j]; } } } cout << dp[n][(1 << n) - 1] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define vout(x) rep(i, x.size()) cout << x[i] << " " template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } using namespace std; using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int, int>; const int inf = 1e9; const ll inf_l = 1e18; const int MAX = 1e5; const int mod = 1e9 + 7; struct mint { ll x; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } mint dp[25][1 << 21]; int main() { int n; cin >> n; vvint a(n, vint(n)); rep(i, n) rep(j, n) cin >> a[i][j]; dp[0][0] = 1; REP(i, n + 1) { rep(j, 1 << n) { if (__builtin_popcount(j) != i - 1) continue; rep(k, n) { if (a[i - 1][k] == 1) dp[i][j | (1 << k)] += dp[i - 1][j]; } } } cout << dp[n][(1 << n) - 1] << endl; }
insert
79
79
79
81
TLE
p03174
C++
Time Limit Exceeded
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #define F first #define S second #define ll long long #define pb push_back #define db double #define ld long double #define ppb pop_back #define pii pair<int, int> #define pll pair<long long, long long> #define piii pair<int, pii> #define all(x) (x).begin(), (x).end() #define __ \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define watch(x) cout << (#x) << " is " << (x) << endl #define rep(i, a, n) for (int i = a; i <= n; i++) #define TEST \ int T; \ cin >> T; \ while (T--) #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } // #define int long long const int N = 1e5 + 1, inf = 2e9 + 7, mod = 1e9 + 7; const ll INF = 2e18 + 7, MOD = 1e15 + 7; int n, all; int dp[1 << 21]; bool v[21][21], was[1 << 21]; bool has(int mask, int i) { return (mask >> i) & 1; } int rec(int mask) { if (was[mask]) return dp[mask]; int ans = 0, bits = __builtin_popcount(mask); for (int i = 0; i < n; i++) { if (has(mask, i) && v[bits - 1][i]) { ans = (ans * 1LL + rec(mask ^ (1 << i))) % mod; } } return dp[mask] = ans; } signed main() { __ // freopen("lcm.in", "r", stdin); // freopen("lcm.out", "w", stdout); cin >> n; all = (1 << n) - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> v[i][j]; } } dp[0] = 1; was[0] = 1; cout << rec(all); return 0; }
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #define F first #define S second #define ll long long #define pb push_back #define db double #define ld long double #define ppb pop_back #define pii pair<int, int> #define pll pair<long long, long long> #define piii pair<int, pii> #define all(x) (x).begin(), (x).end() #define __ \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define watch(x) cout << (#x) << " is " << (x) << endl #define rep(i, a, n) for (int i = a; i <= n; i++) #define TEST \ int T; \ cin >> T; \ while (T--) #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } // #define int long long const int N = 1e5 + 1, inf = 2e9 + 7, mod = 1e9 + 7; const ll INF = 2e18 + 7, MOD = 1e15 + 7; int n, all; int dp[1 << 21]; bool v[21][21], was[1 << 21]; bool has(int mask, int i) { return (mask >> i) & 1; } int rec(int mask) { if (was[mask]) return dp[mask]; int ans = 0, bits = __builtin_popcount(mask); for (int i = 0; i < n; i++) { if (has(mask, i) && v[bits - 1][i]) { ans = (ans * 1LL + rec(mask ^ (1 << i))) % mod; } } was[mask] = 1; return dp[mask] = ans; } signed main() { __ // freopen("lcm.in", "r", stdin); // freopen("lcm.out", "w", stdout); cin >> n; all = (1 << n) - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> v[i][j]; } } dp[0] = 1; was[0] = 1; cout << rec(all); return 0; }
insert
66
66
66
67
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, x, n) for (int i = x; i < (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define all(X) (X).begin(), (X).end() #define X first #define Y second #define pb push_back #define eb emplace_back using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class A, size_t N, class T> void Fill(A (&a)[N], const T &v) { fill((T *)a, (T *)(a + N), v); } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "{"; rep(i, t.size()) { os << t[i] << ","; } os << "}"; return os; } template <class T, size_t n> ostream &operator<<(ostream &os, const array<T, n> &t) { os << "{"; rep(i, n) { os << t[i] << ","; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } const ll INF = 1e9 + 7; ll a[32][32]; ll dp[32][1 << 22]; int main() { ios_base::sync_with_stdio(false); ll N, ans = 0; cin >> N; rep(i, N) { rep(j, N) { cin >> a[i][j]; } } dp[0][0] = 1; rep(m, N) { rep(s, 1 << N) { rep(w, N) { if (!(s & (1 << w)) && a[m][w]) { dp[m + 1][s | (1 << w)] += dp[m][s]; dp[m + 1][s | (1 << w)] %= INF; } } } } ans += dp[N][(1 << N) - 1]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, x, n) for (int i = x; i < (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define all(X) (X).begin(), (X).end() #define X first #define Y second #define pb push_back #define eb emplace_back using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class A, size_t N, class T> void Fill(A (&a)[N], const T &v) { fill((T *)a, (T *)(a + N), v); } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "{"; rep(i, t.size()) { os << t[i] << ","; } os << "}"; return os; } template <class T, size_t n> ostream &operator<<(ostream &os, const array<T, n> &t) { os << "{"; rep(i, n) { os << t[i] << ","; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } const ll INF = 1e9 + 7; ll a[32][32]; ll dp[32][1 << 22]; int main() { ios_base::sync_with_stdio(false); ll N, ans = 0; cin >> N; rep(i, N) { rep(j, N) { cin >> a[i][j]; } } dp[0][0] = 1; rep(m, N) { rep(s, 1 << N) { if (dp[m][s]) { rep(w, N) { if (!(s & (1 << w)) && a[m][w]) { dp[m + 1][s | (1 << w)] += dp[m][s]; dp[m + 1][s | (1 << w)] %= INF; } } } } } ans += dp[N][(1 << N) - 1]; cout << ans << endl; return 0; }
replace
70
74
70
76
TLE
p03174
C++
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; }
#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; }
replace
23
28
23
30
TLE
p03174
C++
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; }
#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; }
insert
22
22
22
24
TLE
p03174
C++
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; } }
#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; } }
delete
41
45
41
41
0
p03174
C++
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; }
// #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; }
replace
33
40
33
43
TLE
p03174
C++
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; }
#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; }
insert
18
18
18
20
TLE