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> using namespace std; using ll = long long; const int mod = 1e9 + 7; int n; vector<vector<ll>> a, dp; int main() { cin >> n; a.resize(n + 1, vector<ll>(n + 1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } dp.resize(n + 1, vector<ll>(1 << n)); dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int mask = 0; mask <= (1 << n) - 1; mask++) { for (int j = 0; j < n; j++) { if ((mask & (1 << j)) && a[i][j + 1]) { dp[i][mask] = (dp[i][mask] + dp[i - 1][mask ^ (1 << j)]) % mod; } } } } ll res = dp[n][(1 << n) - 1]; cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 1e9 + 7; int n; vector<vector<ll>> a, dp; int main() { cin >> n; a.resize(n + 1, vector<ll>(n + 1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } dp.resize(n + 1, vector<ll>(1 << n)); dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int mask = 0; mask <= (1 << n) - 1; mask++) { if (__builtin_popcount(mask) != i) continue; // number of boys == number of girls for (int j = 0; j < n; j++) { if ((mask & (1 << j)) && a[i][j + 1]) { dp[i][mask] = (dp[i][mask] + dp[i - 1][mask ^ (1 << j)]) % mod; } } } } ll res = dp[n][(1 << n) - 1]; cout << res << endl; return 0; }
insert
22
22
22
24
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1000000007; ll modpow(ll x, ll n, ll mod = MOD) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll dp[21][1 << 21]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<vector<int>> a(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < (1 << n); j++) { if (__builtin_popcount(j) != i) continue; for (int k = 0; k < n; k++) { if (j & (1 << k)) continue; if (a[i][k] == 0) continue; (dp[i + 1][j | (1 << k)] += dp[i][j]) %= MOD; } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1000000007; ll modpow(ll x, ll n, ll mod = MOD) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll dp[22][1 << 21]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<vector<int>> a(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < (1 << n); j++) { if (__builtin_popcount(j) != i) continue; for (int k = 0; k < n; k++) { if (j & (1 << k)) continue; if (a[i][k] == 0) continue; (dp[i + 1][j | (1 << k)] += dp[i][j]) %= MOD; } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
replace
19
20
19
20
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define FOR(v, a, b) for (int v = (a); v < (b); ++v) #define FORE(v, a, b) for (int v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for (int v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define ITR(it, c) for (auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for (auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c, x) ((c).find(x) != (c).end()) #define LLI long long int #define fst first #define snd second #ifdef DEBUG #include <boost/core/demangle.hpp> #define dump(x) \ cerr << "L" << __LINE__ << ": in " << __PRETTY_FUNCTION__ << " \e[32;1m" \ << boost::core::demangle(typeid(x).name()) << "\e[37m" \ << " " << (#x) << " = " << (x) << "\e[m" << endl; #else #define dump(x) #endif using namespace std; template <typename T> using V = vector<T>; template <typename T, typename U> using P = pair<T, U>; template <typename I> void join(ostream &ost, I s, I t, string d = " ") { for (auto i = s; i != t; ++i) { if (i != s) ost << d; ost << *i; } ost << endl; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &a : v) is >> a; return is; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << "{"; ITR(i, v) { if (i != v.begin()) os << ","; os << *i; } os << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } const LLI mod = 1e9 + 7; LLI dp[30][1 << 21]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); cin >> a; dp[0][0] = 1; REP(i, N) { REP(j, 1 << N) { dp[i + 1][j] = dp[i][j]; REP(k, N) { if (a[i][k] == 1 && (1 << k)) { (dp[i + 1][j] += dp[i][j ^ (1 << k)]) %= mod; } } } } cout << dp[N][(1 << N) - 1] << endl; return 0; }
#include <bits/stdc++.h> #define FOR(v, a, b) for (int v = (a); v < (b); ++v) #define FORE(v, a, b) for (int v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for (int v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define ITR(it, c) for (auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for (auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c, x) ((c).find(x) != (c).end()) #define LLI long long int #define fst first #define snd second #ifdef DEBUG #include <boost/core/demangle.hpp> #define dump(x) \ cerr << "L" << __LINE__ << ": in " << __PRETTY_FUNCTION__ << " \e[32;1m" \ << boost::core::demangle(typeid(x).name()) << "\e[37m" \ << " " << (#x) << " = " << (x) << "\e[m" << endl; #else #define dump(x) #endif using namespace std; template <typename T> using V = vector<T>; template <typename T, typename U> using P = pair<T, U>; template <typename I> void join(ostream &ost, I s, I t, string d = " ") { for (auto i = s; i != t; ++i) { if (i != s) ost << d; ost << *i; } ost << endl; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &a : v) is >> a; return is; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << "{"; ITR(i, v) { if (i != v.begin()) os << ","; os << *i; } os << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } const LLI mod = 1e9 + 7; LLI dp[30][1 << 21]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); cin >> a; dp[0][0] = 1; REP(i, N) { REP(j, 1 << N) { if (__builtin_popcount(j) != i + 1) continue; dp[i + 1][j] = dp[i][j]; REP(k, N) { if (a[i][k] == 1 && (1 << k)) { (dp[i + 1][j] += dp[i][j ^ (1 << k)]) %= mod; } } } } cout << dp[N][(1 << N) - 1] << endl; return 0; }
insert
80
80
80
82
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back using namespace std; typedef pair<int, int> pii; typedef long long ll; const int inf = 1e9 + 1; const double eps = 1e-9; const int MOD = 1e9 + 7; const int MAXN = 21; int n; int a[MAXN][MAXN]; int dp[MAXN][(int)1 << MAXN]; vector<int> v[MAXN]; int main() { cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; for (int mask = 0; mask < (int)1 << n; mask++) { int br = 0; for (int i = 0; i < n; i++) if (mask & 1 << i) br++; v[br].pb(mask); } dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < v[i - 1].size(); j++) { for (int k = 0; k < n; k++) { if (a[i - 1][k] && (v[i - 1][j] & 1 << (n - 1 - k)) == 0) { dp[i][v[i - 1][j] ^ 1 << (n - 1 - k)] = (dp[i][v[i - 1][j] ^ 1 << (n - 1 - k)] % MOD + dp[i - 1][v[i - 1][j]] % MOD) % MOD; // cout << i << " " << k << " " << dp[i][v[i-1][j] ^ 1<<(n-1-k)] << // '\n'; } } } } cout << dp[n][(int)(1 << n) - 1]; return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back using namespace std; typedef pair<int, int> pii; typedef long long ll; const int inf = 1e9 + 1; const double eps = 1e-9; const int MOD = 1e9 + 7; const int MAXN = 22; int n; int a[MAXN][MAXN]; int dp[MAXN][(int)1 << MAXN]; vector<int> v[MAXN]; int main() { cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; for (int mask = 0; mask < (int)1 << n; mask++) { int br = 0; for (int i = 0; i < n; i++) if (mask & 1 << i) br++; v[br].pb(mask); } dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < v[i - 1].size(); j++) { for (int k = 0; k < n; k++) { if (a[i - 1][k] && (v[i - 1][j] & 1 << (n - 1 - k)) == 0) { dp[i][v[i - 1][j] ^ 1 << (n - 1 - k)] = (dp[i][v[i - 1][j] ^ 1 << (n - 1 - k)] % MOD + dp[i - 1][v[i - 1][j]] % MOD) % MOD; // cout << i << " " << k << " " << dp[i][v[i-1][j] ^ 1<<(n-1-k)] << // '\n'; } } } } cout << dp[n][(int)(1 << n) - 1]; return 0; }
replace
13
14
13
14
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int mp[25][25]; int mod = 1e9 + 7; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> mp[i][j]; } } vector<int> dp(1 << n); dp[0] = 1; for (int man = 0; man < n; ++man) { vector<int> currDp(1 << n); for (int mask = 0; mask < (1 << n); ++mask) { for (int women = 0; women < n; ++women) { if (mp[man][women] == 1 && ((mask & (1 << women)) == 0)) { int next = mask ^ (1 << women); currDp[next] += dp[mask]; currDp[next] %= mod; } } } dp = currDp; } /* for(int i=0;i<(1<<n);++i){ cout<<dp[i]<<" "; } */ cout << dp[(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int mp[25][25]; int mod = 1e9 + 7; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> mp[i][j]; } } vector<int> dp(1 << n); dp[0] = 1; for (int man = 0; man < n; ++man) { vector<int> currDp(1 << n); for (int mask = 0; mask < (1 << n); ++mask) { if (__builtin_popcount(mask) != man) { continue; } for (int women = 0; women < n; ++women) { if (mp[man][women] == 1 && ((mask & (1 << women)) == 0)) { int next = mask ^ (1 << women); currDp[next] += dp[mask]; currDp[next] %= mod; } } } dp = currDp; } /* for(int i=0;i<(1<<n);++i){ cout<<dp[i]<<" "; } */ cout << dp[(1 << n) - 1] << endl; return 0; }
insert
17
17
17
20
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define endl "\n" typedef long long int ll; inline void fastio() { ios::sync_with_stdio(false); cin.tie(0); } inline void setPrecision(int n) { cout.precision(n); } // DEBUG #define dbg(x) cerr << (#x) << ": " << x << endl #define dbgV(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << it << " "; \ cerr << endl; #define dbgS(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << it << " "; \ cerr << endl; #define dbgM(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << "[" << it.f << ", " << it.s << "] "; \ cerr << endl; #define dbg2D(x) \ cerr << (#x) << ": \n"; \ for (auto y : x) { \ for (auto it : y) \ cerr << it << " "; \ cerr << endl; \ } \ cerr << endl; #define dbgA(x, n) \ cerr << (#x) << ": "; \ for (int i = 0; i < n; ++i) \ cerr << x[i] << " "; \ cerr << endl; #define dbgVP(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << "[" << it.f << ", " << it.s << "] "; \ cerr << endl; ll INF = 1e10; ll MOD = 1e9 + 7; int n; int a[21][21]; ll done[1 << 21]; ll pairs(int state) { int idx = __builtin_popcount(state); if (idx == n) return 1; if (done[state] > 0) return done[state]; ll ans = 0; for (int i = 0; i < n; ++i) if (a[idx][i] && !(state & (1 << i))) { ans = (ans + pairs(state | (1 << i))) % MOD; } done[state] = ans; return ans; } int main() { fastio(); cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> a[i][j]; memset(done, -1, sizeof(done)); cout << pairs(0); return 0; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" typedef long long int ll; inline void fastio() { ios::sync_with_stdio(false); cin.tie(0); } inline void setPrecision(int n) { cout.precision(n); } // DEBUG #define dbg(x) cerr << (#x) << ": " << x << endl #define dbgV(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << it << " "; \ cerr << endl; #define dbgS(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << it << " "; \ cerr << endl; #define dbgM(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << "[" << it.f << ", " << it.s << "] "; \ cerr << endl; #define dbg2D(x) \ cerr << (#x) << ": \n"; \ for (auto y : x) { \ for (auto it : y) \ cerr << it << " "; \ cerr << endl; \ } \ cerr << endl; #define dbgA(x, n) \ cerr << (#x) << ": "; \ for (int i = 0; i < n; ++i) \ cerr << x[i] << " "; \ cerr << endl; #define dbgVP(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << "[" << it.f << ", " << it.s << "] "; \ cerr << endl; ll INF = 1e10; ll MOD = 1e9 + 7; int n; int a[21][21]; ll done[1 << 21]; ll pairs(int state) { int idx = __builtin_popcount(state); if (idx == n) return 1; if (done[state] != -1) return done[state]; ll ans = 0; for (int i = 0; i < n; ++i) if (a[idx][i] && !(state & (1 << i))) { ans = (ans + pairs(state | (1 << i))) % MOD; } done[state] = ans; return ans; } int main() { fastio(); cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> a[i][j]; memset(done, -1, sizeof(done)); cout << pairs(0); return 0; }
replace
61
62
61
62
TLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <cstring> #include <iostream> #include <limits.h> #include <map> #include <queue> #include <set> #include <stdio.h> #include <utility> #include <vector> #define MAX_DOUBLE 40000000000.0 #include <math.h> #include <unistd.h> using namespace std; long long mod = 1e9 + 7; bool graph[21][21]; long long res[22][2097152]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a; scanf("%d", &a); if (a == 1) graph[i][j] = true; } } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 2097152; j++) { for (int z = 0; z < n; z++) { if (j < (1 << z)) break; if (graph[i][z] && j >= (1 << z) && (j & (1 << z))) { if (i == n - 1) { res[i][j] = 1; } else { res[i][j] = (res[i][j] + res[i + 1][j - (1 << z)]) % mod; } } } } } printf("%d", res[0][(1 << n) - 1]); }
#include <algorithm> #include <cstring> #include <iostream> #include <limits.h> #include <map> #include <queue> #include <set> #include <stdio.h> #include <utility> #include <vector> #define MAX_DOUBLE 40000000000.0 #include <math.h> #include <unistd.h> using namespace std; long long mod = 1e9 + 7; bool graph[21][21]; long long res[22][2097152]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a; scanf("%d", &a); if (a == 1) graph[i][j] = true; } } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 2097152; j++) { int count = 0; for (int z = 0; z < n; z++) { count += ((j & (1 << z)) > 0); } if (count != (n - i)) continue; for (int z = 0; z < n; z++) { if (j < (1 << z)) break; if (graph[i][z] && j >= (1 << z) && (j & (1 << z))) { if (i == n - 1) { res[i][j] = 1; } else { res[i][j] = (res[i][j] + res[i + 1][j - (1 << z)]) % mod; } } } } } printf("%d", res[0][(1 << n) - 1]); }
insert
37
37
37
45
TLE
p03174
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; int MOD = 1000 * 1000 * 1000 + 7; const int MXN = 21; int n; int match[MXN][MXN]; int dp[MXN][(1 << MXN)]; int ways(int x, int bts) { if (x == n) { return (bts == ((1 << n) - 1)); } if (dp[x][bts] != -1) return dp[x][bts]; int res = 0; for (int i = 0; i < n; i++) { if (match[x][i]) { res += ways(x + 1, (bts | (1 << i))); res = res % MOD; } } return dp[x][bts] = res; } int main() { for (int i = 0; i < MXN; i++) for (int j = 0; j < (1 << MXN); j++) dp[i][j] = -1; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> match[i][j]; } } cout << ways(0, 0); }
#include "bits/stdc++.h" using namespace std; int MOD = 1000 * 1000 * 1000 + 7; const int MXN = 21; int n; int match[MXN][MXN]; int dp[MXN][(1 << MXN)]; int ways(int x, int bts) { if (x == n) { return (bts == ((1 << n) - 1)); } if (dp[x][bts] != -1) return dp[x][bts]; int res = 0; for (int i = 0; i < n; i++) { if ((1 << i) & bts) continue; if (match[x][i]) { res += ways(x + 1, (bts | (1 << i))); res = res % MOD; } } return dp[x][bts] = res; } int main() { for (int i = 0; i < MXN; i++) for (int j = 0; j < (1 << MXN); j++) dp[i][j] = -1; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> match[i][j]; } } cout << ways(0, 0); }
insert
20
20
20
22
TLE
p03174
C++
Time Limit Exceeded
#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; #define ll long long #define ull unsigned long long #define ld long double #define vi vector<int> #define vl vector<ll> #define vul vector<ull> #define pii pair<int, int> #define pll pair<ll, ll> #define vi2d vector<vi> #define vl2d vector<vl> #define vpll vector<pll> #define fi first #define se second #define mp make_pair #define pb push_back #define rz resize #define fr(i, n) for (i = 0; i < (n); i++) #define frr(i, n) for (i = (n - 1); i >= 0; i--) #define fre(i, n) for (i = 0; i <= (n); i++) #define frre(i, n) for (i = (n); i >= 0; i--) #define frab(i, a, b) for (i = (a); i < (b); i++) #define freab(i, a, b) for (i = (a); i <= (b); i++) #define frrab(i, a, b) for (i = (b - 1); i >= a; i--) #define frreab(i, b, a) for (i = (b); i >= a; i--) #define sz(a) int((a).size()) #define mset(a, b) (memset(a, b, sizeof(a))) #define max3(a, b, c) (max((a), max((b), (c)))) #define min3(a, b, c) (min((c), min((a), (b)))) #define sumv(v) accumulate(all(v), ll(0)) #define productv(v) accumulate(all(v), ll(1), multiplies<ll>()) #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); vector<ll> sprime; vector<char> prime(1000000, true); void sieve(ll n) { // O(nloglogn) time sprime.pb(0); prime[0] = prime[1] = false; ll cnt = 0; for (ll i = 2; i <= n; ++i) { if (prime[i]) { cnt++; sprime.pb(i); for (ll j = 2 * i; j <= n; j += i) prime[j] = false; } } } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll fastpowMOD(ll a, ll p, ll MOD) { if (p == 0) return 1; ll z = fastpowMOD(a, p / 2, MOD); z = (z * z) % MOD; if (p % 2) z = (z * a) % MOD; return z; } ll fastpow(ll a, ll p) { if (p == 0) return 1; ll z = fastpow(a, p / 2); z = (z * z); if (p % 2) z = (z * a); return z; } #define mod 1000000007 #define inf LLONG_MAX #define minf LLONG_MIN #define pi 3.14 #define ex 2.71 //*********************************************/// const int N = 1e5 + 5; // int i,j; ll dp[22][2097200]; ll a[22][22]; ll n; ll fun(ll p, ll mask) { int i; if (dp[p][mask] != -1) return dp[p][mask]; if (p == 0) { if (mask == (pow(2, n) - 1)) return dp[p][mask] = 1; else return dp[p][mask] = 0; } dp[p][mask] = 0; freab(i, 1, n) { if (a[p][i]) { ll k = mask | (1 << (i - 1)); dp[p][mask] = (dp[p][mask] + fun(p - 1, mask | (1 << (i - 1)))) % mod; } } return dp[p][mask]; } void solve() { cin >> n; int i, j; freab(i, 1, n) { // cout<<i<<endl; freab(j, 1, n) cin >> a[i][j]; } mset(dp, -1); int ans = fun(n, 0); cout << ans << endl; } int main() { fast; int z; // cin>>z; z = 1; while (z--) { solve(); } return 0; }
#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; #define ll long long #define ull unsigned long long #define ld long double #define vi vector<int> #define vl vector<ll> #define vul vector<ull> #define pii pair<int, int> #define pll pair<ll, ll> #define vi2d vector<vi> #define vl2d vector<vl> #define vpll vector<pll> #define fi first #define se second #define mp make_pair #define pb push_back #define rz resize #define fr(i, n) for (i = 0; i < (n); i++) #define frr(i, n) for (i = (n - 1); i >= 0; i--) #define fre(i, n) for (i = 0; i <= (n); i++) #define frre(i, n) for (i = (n); i >= 0; i--) #define frab(i, a, b) for (i = (a); i < (b); i++) #define freab(i, a, b) for (i = (a); i <= (b); i++) #define frrab(i, a, b) for (i = (b - 1); i >= a; i--) #define frreab(i, b, a) for (i = (b); i >= a; i--) #define sz(a) int((a).size()) #define mset(a, b) (memset(a, b, sizeof(a))) #define max3(a, b, c) (max((a), max((b), (c)))) #define min3(a, b, c) (min((c), min((a), (b)))) #define sumv(v) accumulate(all(v), ll(0)) #define productv(v) accumulate(all(v), ll(1), multiplies<ll>()) #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); vector<ll> sprime; vector<char> prime(1000000, true); void sieve(ll n) { // O(nloglogn) time sprime.pb(0); prime[0] = prime[1] = false; ll cnt = 0; for (ll i = 2; i <= n; ++i) { if (prime[i]) { cnt++; sprime.pb(i); for (ll j = 2 * i; j <= n; j += i) prime[j] = false; } } } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll fastpowMOD(ll a, ll p, ll MOD) { if (p == 0) return 1; ll z = fastpowMOD(a, p / 2, MOD); z = (z * z) % MOD; if (p % 2) z = (z * a) % MOD; return z; } ll fastpow(ll a, ll p) { if (p == 0) return 1; ll z = fastpow(a, p / 2); z = (z * z); if (p % 2) z = (z * a); return z; } #define mod 1000000007 #define inf LLONG_MAX #define minf LLONG_MIN #define pi 3.14 #define ex 2.71 //*********************************************/// const int N = 1e5 + 5; // int i,j; ll dp[22][2097200]; ll a[22][22]; ll n; ll fun(ll p, ll mask) { int i; if (dp[p][mask] != -1) return dp[p][mask]; if (p == 0) { if (mask == (pow(2, n) - 1)) return dp[p][mask] = 1; else return dp[p][mask] = 0; } dp[p][mask] = 0; freab(i, 1, n) { if (a[p][i] && !(mask & (1 << (i - 1)))) { ll k = mask | (1 << (i - 1)); dp[p][mask] = (dp[p][mask] + fun(p - 1, mask | (1 << (i - 1)))) % mod; } } return dp[p][mask]; } void solve() { cin >> n; int i, j; freab(i, 1, n) { // cout<<i<<endl; freab(j, 1, n) cin >> a[i][j]; } mset(dp, -1); int ans = fun(n, 0); cout << ans << endl; } int main() { fast; int z; // cin>>z; z = 1; while (z--) { solve(); } return 0; }
replace
108
109
108
109
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) 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; } #define all(x) (x).begin(), (x).end() #pragma GCC optimize("Ofast") using namespace std; typedef long long int ll; typedef long double ld; const ll INF = (1LL << 62); const ld pi = acosl((ld)-1); const ll mod = 1000000007; // const ll mod = 1234567; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1}; const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0}; #define endn "\n" #define TO_STRING(VariableName) #VariableName template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { rep(i, (int)v.size() - 1) cout << v[i] << " "; cout << v[(int)v.size() - 1]; return out; } template <typename T1, typename T2> ostream &operator<<(ostream &out, const map<T1, T2> &p) { out << "(" << p.first << ", " << p.second << ")"; return out; } template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "(" << p.first << ", " << p.second << ")"; return out; } template <class T> void debag(const T &obj) { cout << obj << endl; } template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt<mod>; bool solve() { ll n; cin >> n; int a[n][n]; rep(i, n) rep(j, n) cin >> a[i][j]; vector<vector<modint>> dp(n + 1, vector<modint>((1LL << n), modint(0))); dp[0][0] = 1; rep(i, n) rep(s, 1LL << n) { rep(j, n) { if ((s >> j) & 1) continue; if (!a[i][j]) continue; dp[i + 1][s | (1LL << j)] += dp[i][s]; } } cout << dp[n][(1LL << n) - 1] << endl; return false; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(30); solve(); }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) 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; } #define all(x) (x).begin(), (x).end() #pragma GCC optimize("Ofast") using namespace std; typedef long long int ll; typedef long double ld; const ll INF = (1LL << 62); const ld pi = acosl((ld)-1); const ll mod = 1000000007; // const ll mod = 1234567; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1}; const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0}; #define endn "\n" #define TO_STRING(VariableName) #VariableName template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { rep(i, (int)v.size() - 1) cout << v[i] << " "; cout << v[(int)v.size() - 1]; return out; } template <typename T1, typename T2> ostream &operator<<(ostream &out, const map<T1, T2> &p) { out << "(" << p.first << ", " << p.second << ")"; return out; } template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "(" << p.first << ", " << p.second << ")"; return out; } template <class T> void debag(const T &obj) { cout << obj << endl; } template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt<mod>; bool solve() { ll n; cin >> n; int a[n][n]; rep(i, n) rep(j, n) cin >> a[i][j]; vector<vector<modint>> dp(n + 1, vector<modint>((1LL << n), modint(0))); dp[0][0] = 1; rep(s, 1LL << n) { int i = 0; rep(j, n) if ((s >> j) & 1) i++; rep(j, n) { if ((s >> j) & 1) continue; if (!a[i][j]) continue; dp[i + 1][s | (1LL << j)] += dp[i][s]; } } cout << dp[n][(1LL << n) - 1] << endl; return false; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(30); solve(); }
replace
135
136
135
138
TLE
p03174
C++
Time Limit Exceeded
#include <iostream> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; const int mod = 1000000000 + 7; int count_bit(int n) { int ret = 0; rep(i, 30) { if (n & (1 << i)) ret++; } return ret; } int main() { int n; cin >> n; vector<vector<int>> c(n, vector<int>(n)); rep(i, n) rep(j, n) cin >> c[i][j]; vector<vector<int>> dp(n + 1, vector<int>(1 << n)); dp[0][0] = 1; rep(i, n) for (int j = 0; j < (1 << n); j++) if (i == count_bit(j)) { rep(w, n) if (i + 1 == count_bit(j | (1 << w))) if (c[i][w]) { dp[i + 1][j | (1 << w)] += dp[i][j]; dp[i + 1][j | (1 << w)] %= mod; } } cout << dp[n][(1 << n) - 1] << endl; }
#include <iostream> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; const int mod = 1000000000 + 7; int count_bit(int n) { int ret = 0; rep(i, 30) { if (n & (1 << i)) ret++; } return ret; } int main() { int n; cin >> n; vector<vector<int>> c(n, vector<int>(n)); rep(i, n) rep(j, n) cin >> c[i][j]; vector<vector<int>> dp(n + 1, vector<int>(1 << n)); dp[0][0] = 1; rep(i, n) for (int j = 0; j < (1 << n); j++) if (i == count_bit(j)) { rep(w, n) if (j != j | (1 << w)) if (c[i][w]) { dp[i + 1][j | (1 << w)] += dp[i][j]; dp[i + 1][j | (1 << w)] %= mod; } } cout << dp[n][(1 << n) - 1] << endl; }
replace
26
27
26
27
TLE
p03174
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrep(i, n) for (int(i) = ((n)-1); (i) >= 0; (i)--) #define itn int #define all(x) (x).begin(), (x).end() #define F first #define S second const long long INF = 1LL << 60; const int MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } // https://www.creativ.xyz/dump-cpp-652/ #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) // vector template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // pair template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // map template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif vector<vector<int>> data(22, vector<int>(21)); vector<vector<int>> dp(25, vector<int>((1LL << 22))); vector<vector<int>> vis(25, vector<int>((1LL << 22))); int n; int solve(int x, int bit) { if (vis[x][bit]) return dp[x][bit]; else vis[x][bit] = true; if (x == 0 && bit == 0) return dp[x][bit] = 1; if (dp[x][bit]) return dp[x][bit]; rep(y, n) { if (data[x - 1][y] && (bit & (1LL << y))) { dp[x][bit] += solve(x - 1, bit & (~(1LL << y))); dp[x][bit] %= MOD; } } return dp[x][bit]; } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> n; rep(i, n) { rep(j, n) { cin >> data[i][j]; } } dp[0][0] = 1; cout << solve(n, (1LL << n) - 1) << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrep(i, n) for (int(i) = ((n)-1); (i) >= 0; (i)--) #define itn int #define all(x) (x).begin(), (x).end() #define F first #define S second const long long INF = 1LL << 60; const int MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } // https://www.creativ.xyz/dump-cpp-652/ #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) // vector template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // pair template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // map template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif vector<vector<int>> data(22, vector<int>(21)); vector<vector<int>> dp(23, vector<int>((1LL << 21))); vector<vector<int>> vis(23, vector<int>((1LL << 21))); int n; int solve(int x, int bit) { if (vis[x][bit]) return dp[x][bit]; else vis[x][bit] = true; if (x == 0 && bit == 0) return dp[x][bit] = 1; if (dp[x][bit]) return dp[x][bit]; rep(y, n) { if (data[x - 1][y] && (bit & (1LL << y))) { dp[x][bit] += solve(x - 1, bit & (~(1LL << y))); dp[x][bit] %= MOD; } } return dp[x][bit]; } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> n; rep(i, n) { rep(j, n) { cin >> data[i][j]; } } dp[0][0] = 1; cout << solve(n, (1LL << n) - 1) << endl; }
replace
101
103
101
103
MLE
p03174
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define M_PI 3.14159265358979323846 using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } inline int readInt() { int x; scanf("%d", &x); return x; } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<int, PII> TIII; typedef long long LL; typedef unsigned long long ULL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define SQ(a) ((a) * (a)) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, s, n) for (int i = s; i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define MOD 1000000007 const int N = 1 << 21; LL dp[21][N]; int a[21][21]; int main() { int n; scanf("%d", &n); REP(i, n) REP(j, n) scanf("%d", &a[i][j]); dp[0][0] = 1; REP(i, n) { REP(k, 1 << n) { if (!dp[i][k]) continue; REP(j, n) { if (!a[i][j]) continue; if (k & (1 << j)) continue; dp[i + 1][k | (1 << j)] = (dp[i + 1][k | (1 << j)] + dp[i][k]) % MOD; } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define M_PI 3.14159265358979323846 using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } inline int readInt() { int x; scanf("%d", &x); return x; } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<int, PII> TIII; typedef long long LL; typedef unsigned long long ULL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define SQ(a) ((a) * (a)) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, s, n) for (int i = s; i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define MOD 1000000007 const int N = 1 << 21; LL dp[22][N]; int a[21][21]; int main() { int n; scanf("%d", &n); REP(i, n) REP(j, n) scanf("%d", &a[i][j]); dp[0][0] = 1; REP(i, n) { REP(k, 1 << n) { if (!dp[i][k]) continue; REP(j, n) { if (!a[i][j]) continue; if (k & (1 << j)) continue; dp[i + 1][k | (1 << j)] = (dp[i + 1][k | (1 << j)] + dp[i][k]) % MOD; } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
replace
85
86
85
86
-11
p03174
C++
Runtime Error
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define PI 3.14159265359 using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) const long long INF = 1e+18 + 1; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> T; const ll MOD = 1000000007LL; string abc = "abcdefghijklmnopqrstuvwxyz"; string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; vl dx = {-1, -1, -1, 0, 0, 1, 1, 1}; vl dy = {1, -1, 0, 1, -1, 1, 0, -1}; vl dp(1 << 21); int main() { dp[0] = 1; ll n; cin >> n; vvl a(n, vl(n)); rep(i, n) { rep(j, n) cin >> a[i][j]; } for (ll S = 1; S < (1 << 21); S++) { ll i = __builtin_popcount(S); rep(j, n) { if ((S & (1 << j)) && a[i - 1][j]) dp[S] = (dp[S] + dp[S ^ (1 << j)]) % MOD; } } ll ans = dp[(1 << n) - 1]; cout << ans << endl; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define PI 3.14159265359 using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) const long long INF = 1e+18 + 1; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> T; const ll MOD = 1000000007LL; string abc = "abcdefghijklmnopqrstuvwxyz"; string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; vl dx = {-1, -1, -1, 0, 0, 1, 1, 1}; vl dy = {1, -1, 0, 1, -1, 1, 0, -1}; vl dp(1 << 21); int main() { dp[0] = 1; ll n; cin >> n; vvl a(n, vl(n)); rep(i, n) { rep(j, n) cin >> a[i][j]; } for (ll S = 1; S < (1 << n); S++) { ll i = __builtin_popcount(S); rep(j, n) { if ((S & (1 << j)) && a[i - 1][j]) dp[S] = (dp[S] + dp[S ^ (1 << j)]) % MOD; } } ll ans = dp[(1 << n) - 1]; cout << ans << endl; }
replace
23
24
23
24
-11
p03174
C++
Memory Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; const int MN = 24, MOD = 1e9 + 7; int N, A[MN][MN], dp[MN][1 << MN]; inline int cb(int msk, int b) { return msk & ~(1 << b); } inline int sb(int msk, int b) { return msk | (1 << b); } inline int tb(int msk, int b) { return msk ^ (1 << b); } inline bool ib(int msk, int b) { return msk & (1 << b); } int solve(int n, int msk) { if (n < 0) return 1; if (~dp[n][msk]) return dp[n][msk]; dp[n][msk] = 0; for (int i = 0; i < N; ++i) if (A[n][i] && !ib(msk, i)) dp[n][msk] = (dp[n][msk] + solve(n - 1, sb(msk, i))) % MOD; return dp[n][msk]; } int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) scanf("%d", &A[i][j]); memset(dp, -1, sizeof dp); printf("%d\n", solve(N - 1, 0)); }
#include <bits/stdc++.h> typedef long long ll; const int MN = 21, MOD = 1e9 + 7; int N, A[MN][MN], dp[MN][1 << MN]; inline int cb(int msk, int b) { return msk & ~(1 << b); } inline int sb(int msk, int b) { return msk | (1 << b); } inline int tb(int msk, int b) { return msk ^ (1 << b); } inline bool ib(int msk, int b) { return msk & (1 << b); } int solve(int n, int msk) { if (n < 0) return 1; if (~dp[n][msk]) return dp[n][msk]; dp[n][msk] = 0; for (int i = 0; i < N; ++i) if (A[n][i] && !ib(msk, i)) dp[n][msk] = (dp[n][msk] + solve(n - 1, sb(msk, i))) % MOD; return dp[n][msk]; } int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) scanf("%d", &A[i][j]); memset(dp, -1, sizeof dp); printf("%d\n", solve(N - 1, 0)); }
replace
2
3
2
3
MLE
p03174
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; // ref: https://atcoder.jp/contests/dp/submissions/7981121 int main(int argc, char const *argv[]) { int MOD = 1000000007; int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; } } vector<vector<int>> dp(N + 1, vector<int>(1 << N, 0)); dp[0][0] = 1; for (int i = 1; i <= N; i++) { for (int mask = 0; mask < (1 << N); mask++) { for (int j = 0; j < N; j++) { if (a[i - 1][j] && (mask & (1 << j))) { dp[i][mask] += dp[i - 1][mask ^ (1 << j)]; if (dp[i][mask] >= MOD) { dp[i][mask] -= MOD; } } } } } // for (int i = 0; i <= N; i++) { // for (int mask = 0; mask < (1 << N); mask++) { // cout << dp[i][mask] << ", "; // } // cout << endl; // } cout << dp[N][(1 << N) - 1] << endl; return 0; }
#include <iostream> #include <vector> using namespace std; // ref: https://atcoder.jp/contests/dp/submissions/7981121 int main(int argc, char const *argv[]) { int MOD = 1000000007; int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; } } vector<vector<int>> dp(N + 1, vector<int>(1 << N, 0)); dp[0][0] = 1; for (int i = 1; i <= N; i++) { for (int mask = 0; mask < (1 << N); mask++) { if (__builtin_popcount(mask) != i) { continue; } for (int j = 0; j < N; j++) { if (a[i - 1][j] && (mask & (1 << j))) { dp[i][mask] += dp[i - 1][mask ^ (1 << j)]; if (dp[i][mask] >= MOD) { dp[i][mask] -= MOD; } } } } } // for (int i = 0; i <= N; i++) { // for (int mask = 0; mask < (1 << N); mask++) { // cout << dp[i][mask] << ", "; // } // cout << endl; // } cout << dp[N][(1 << N) - 1] << endl; return 0; }
insert
22
22
22
25
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < n; ++i) #define Graph vector<vector<ll>> #define INF (1ll << 60) #define mod 1000000007 template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } int main() { ll n; cin >> n; vector<vector<ll>> a(n, vector<ll>(n)); rep(i, n) rep(j, n) cin >> a[i][j]; vector<vector<ll>> dp(25, vector<ll>(1 << n, 0)); dp[0][0] = 1; rep(i, n) { // i番目の男性まで組を作っている rep(j, (1 << n)) { // すでに組んでいる女性をbitで表現 rep(k, n) { if (!((j >> k) & 1) && a[i][k]) { dp[i + 1][j | (1 << k)] += dp[i][j]; dp[i + 1][j | (1 << k)] %= mod; } } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < n; ++i) #define Graph vector<vector<ll>> #define INF (1ll << 60) #define mod 1000000007 template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } int main() { ll n; cin >> n; vector<vector<ll>> a(n, vector<ll>(n)); rep(i, n) rep(j, n) cin >> a[i][j]; vector<vector<ll>> dp(25, vector<ll>(1 << n, 0)); dp[0][0] = 1; rep(i, n) { // i番目の男性まで組を作っている rep(j, (1 << n)) { // すでに組んでいる女性をbitで表現 if (dp[i][j] == 0) continue; rep(k, n) { if (!((j >> k) & 1) && a[i][k]) { dp[i + 1][j | (1 << k)] += dp[i][j]; dp[i + 1][j | (1 << k)] %= mod; } } } } cout << dp[n][(1 << n) - 1] << endl; return 0; }
insert
33
33
33
35
TLE
p03174
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <ciso646> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define inf 0x3f3f3f3f3f3f3f3f #define ALL(a) (a).begin(), (a).end() #define DEBUG(x) // cerr<<#x<<": "<<x<<endl #define ll long long #define ull unsigned long long using pii = pair<ll, ll>; #define eps 1e-14 #define SETUP \ cin.tie(0), ios::sync_with_stdio(false), \ cout << setprecision(15) << std::fixed; #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) template <class T> using vec2 = std::vector<vector<T>>; namespace { struct input_returnner { ll N; input_returnner(ll N_ = 0) : N(N_) {} template <typename T> operator vector<T>() const { vector<T> res(N); for (auto &a : res) cin >> a; return std::move(res); } template <typename T> operator T() const { T res; cin >> res; return res; } template <typename T> T operator-(T right) { return T(input_returnner()) - right; } template <typename T> T operator+(T right) { return T(input_returnner()) + right; } template <typename T> T operator*(T right) { return T(input_returnner()) * right; } template <typename T> T operator/(T right) { return T(input_returnner()) / right; } template <typename T> T operator<<(T right) { return T(input_returnner()) << right; } template <typename T> T operator>>(T right) { return T(input_returnner()) >> right; } }; template <typename T> input_returnner in() { return in<T>(); } input_returnner in() { return input_returnner(); } input_returnner in(ll N) { return std::move(input_returnner(N)); } } // namespace template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<std::vector<T>> : std::true_type {}; template <typename T> constexpr bool is_vector_v = is_vector<T>::value; template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { if (!v.empty()) { for (int i = 0; i < v.size(); ++i) { out << v[i] << (i == v.size() - 1 ? "\n" : (is_vector_v<T> ? "" : ", ")); } } return out; } namespace std { // ref: // https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set template <class T> inline void hash_combine(std::size_t &seed, T const &v) { seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; template <class T, class U> class hash<std::pair<T, U>> { public: size_t operator()(const std::pair<T, U> &x) const { return hash<std::tuple<T, U>>()(std::tie(x.first, x.second)); } }; } // namespace std // ref: https://stackoverflow.com/questions/6245735/pretty-print-stdtuple namespace aux { template <std::size_t...> struct seq {}; template <std::size_t N, std::size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {}; template <std::size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {}; template <class Ch, class Tr, class Tuple, std::size_t... Is> void print_tuple(std::basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) { using swallow = int[]; (void)swallow{0, (void(os << (Is == 0 ? "" : ", ") << std::get<Is>(t)), 0)...}; } } // namespace aux template <class Ch, class Tr, class... Args> auto operator<<(std::basic_ostream<Ch, Tr> &os, std::tuple<Args...> const &t) -> std::basic_ostream<Ch, Tr> & { os << "("; aux::print_tuple(os, t, aux::gen_seq<sizeof...(Args)>()); return os << ")"; } template <class S, class T> std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) { return os << "(" << p.first << ", " << p.second << ")"; } // ref: // https://stackoverflow.com/questions/8542591/c11-reverse-range-based-for-loo�Fp template <typename T> struct reversion_wrapper { T &iterable; }; template <typename T> auto begin(reversion_wrapper<T> w) { return std::rbegin(w.iterable); } template <typename T> auto end(reversion_wrapper<T> w) { return std::rend(w.iterable); } template <typename T> reversion_wrapper<T> REV(T &&iterable) { return {iterable}; } template <class T> bool inside(T left, T val, T right) { return left <= val and val < right; } template <class T> T bitCount(T num) { T res = 0; while (num > 0) { if (num & 1) ++res; num >>= 1; } return res; } ll MOD = 1e9 + 7; void solve(); signed main() { SETUP; #ifdef _DEBUG while (true) { #endif solve(); #ifdef _DEBUG cout << "-------" << endl; } #endif #ifdef _DEBUG system("pause"); #endif return 0; } #define int ll // template struct mint { int64_t value; mint() : value(0) {} mint(int64_t value_) : value(value_ % MOD) {} inline mint &operator+=(const mint &rhs) { value += rhs.value; if (value >= MOD) { value -= MOD; } return *this; } inline mint &operator-=(const mint &rhs) { value -= rhs.value; if (value < 0) { value += MOD; } return *this; } inline mint &operator*=(const mint &rhs) { value *= rhs.value; value %= MOD; return *this; } inline mint &operator%=(const mint &rhs) { return *this; } }; inline mint operator+(const mint &lhs, const mint &rhs) { mint res = lhs; res += rhs; return res; } inline mint operator-(const mint &lhs, const mint &rhs) { mint res = lhs; res -= rhs; return res; } inline mint operator*(const mint &lhs, const mint &rhs) { mint res = lhs; res *= rhs; return res; } inline mint operator%(const mint &lhs, const uint64_t &rhs) { return lhs; } ostream &operator<<(ostream &out, mint n) { return out << n.value; } // x^n % mod // O(log n) mint mod_pow(mint x, uint64_t n) { mint res = 1; while (n > 0) { if (n & 1) res = res * x; x = x * x; n >>= 1; } return res; } void solve() { int N; cin >> N; vector<vector<int>> A(N, vector<int>(N)); REP(i, N) REP(j, N) { cin >> A[i][j]; } vector<vector<int>> pos(N); REP(i, N) REP(j, N) if (A[i][j]) { pos[i].push_back(j); } vector<vector<mint>> dp(N + 1, vector<mint>(1 << N)); dp[0][0] = 1; FOR(i, 1, N + 1) { for (int j : pos[i - 1]) { //(i-1)からjを使用する REP(k, (1 << N)) if ((bitCount(k) == i) and (k & (1 << j))) { int src = (k & ~(1 << j)); dp[i][k] += dp[i - 1][src]; } } } cout << dp.back().back() << endl; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <ciso646> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define inf 0x3f3f3f3f3f3f3f3f #define ALL(a) (a).begin(), (a).end() #define DEBUG(x) // cerr<<#x<<": "<<x<<endl #define ll long long #define ull unsigned long long using pii = pair<ll, ll>; #define eps 1e-14 #define SETUP \ cin.tie(0), ios::sync_with_stdio(false), \ cout << setprecision(15) << std::fixed; #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) template <class T> using vec2 = std::vector<vector<T>>; namespace { struct input_returnner { ll N; input_returnner(ll N_ = 0) : N(N_) {} template <typename T> operator vector<T>() const { vector<T> res(N); for (auto &a : res) cin >> a; return std::move(res); } template <typename T> operator T() const { T res; cin >> res; return res; } template <typename T> T operator-(T right) { return T(input_returnner()) - right; } template <typename T> T operator+(T right) { return T(input_returnner()) + right; } template <typename T> T operator*(T right) { return T(input_returnner()) * right; } template <typename T> T operator/(T right) { return T(input_returnner()) / right; } template <typename T> T operator<<(T right) { return T(input_returnner()) << right; } template <typename T> T operator>>(T right) { return T(input_returnner()) >> right; } }; template <typename T> input_returnner in() { return in<T>(); } input_returnner in() { return input_returnner(); } input_returnner in(ll N) { return std::move(input_returnner(N)); } } // namespace template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<std::vector<T>> : std::true_type {}; template <typename T> constexpr bool is_vector_v = is_vector<T>::value; template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { if (!v.empty()) { for (int i = 0; i < v.size(); ++i) { out << v[i] << (i == v.size() - 1 ? "\n" : (is_vector_v<T> ? "" : ", ")); } } return out; } namespace std { // ref: // https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set template <class T> inline void hash_combine(std::size_t &seed, T const &v) { seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; template <class T, class U> class hash<std::pair<T, U>> { public: size_t operator()(const std::pair<T, U> &x) const { return hash<std::tuple<T, U>>()(std::tie(x.first, x.second)); } }; } // namespace std // ref: https://stackoverflow.com/questions/6245735/pretty-print-stdtuple namespace aux { template <std::size_t...> struct seq {}; template <std::size_t N, std::size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {}; template <std::size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {}; template <class Ch, class Tr, class Tuple, std::size_t... Is> void print_tuple(std::basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) { using swallow = int[]; (void)swallow{0, (void(os << (Is == 0 ? "" : ", ") << std::get<Is>(t)), 0)...}; } } // namespace aux template <class Ch, class Tr, class... Args> auto operator<<(std::basic_ostream<Ch, Tr> &os, std::tuple<Args...> const &t) -> std::basic_ostream<Ch, Tr> & { os << "("; aux::print_tuple(os, t, aux::gen_seq<sizeof...(Args)>()); return os << ")"; } template <class S, class T> std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) { return os << "(" << p.first << ", " << p.second << ")"; } // ref: // https://stackoverflow.com/questions/8542591/c11-reverse-range-based-for-loo�Fp template <typename T> struct reversion_wrapper { T &iterable; }; template <typename T> auto begin(reversion_wrapper<T> w) { return std::rbegin(w.iterable); } template <typename T> auto end(reversion_wrapper<T> w) { return std::rend(w.iterable); } template <typename T> reversion_wrapper<T> REV(T &&iterable) { return {iterable}; } template <class T> bool inside(T left, T val, T right) { return left <= val and val < right; } template <class T> T bitCount(T num) { T res = 0; while (num > 0) { if (num & 1) ++res; num >>= 1; } return res; } ll MOD = 1e9 + 7; void solve(); signed main() { SETUP; #ifdef _DEBUG while (true) { #endif solve(); #ifdef _DEBUG cout << "-------" << endl; } #endif #ifdef _DEBUG system("pause"); #endif return 0; } #define int ll // template struct mint { int64_t value; mint() : value(0) {} mint(int64_t value_) : value(value_ % MOD) {} inline mint &operator+=(const mint &rhs) { value += rhs.value; if (value >= MOD) { value -= MOD; } return *this; } inline mint &operator-=(const mint &rhs) { value -= rhs.value; if (value < 0) { value += MOD; } return *this; } inline mint &operator*=(const mint &rhs) { value *= rhs.value; value %= MOD; return *this; } inline mint &operator%=(const mint &rhs) { return *this; } }; inline mint operator+(const mint &lhs, const mint &rhs) { mint res = lhs; res += rhs; return res; } inline mint operator-(const mint &lhs, const mint &rhs) { mint res = lhs; res -= rhs; return res; } inline mint operator*(const mint &lhs, const mint &rhs) { mint res = lhs; res *= rhs; return res; } inline mint operator%(const mint &lhs, const uint64_t &rhs) { return lhs; } ostream &operator<<(ostream &out, mint n) { return out << n.value; } // x^n % mod // O(log n) mint mod_pow(mint x, uint64_t n) { mint res = 1; while (n > 0) { if (n & 1) res = res * x; x = x * x; n >>= 1; } return res; } void solve() { int N; cin >> N; vector<vector<int>> A(N, vector<int>(N)); REP(i, N) REP(j, N) { cin >> A[i][j]; } vector<vector<int>> pos(N); REP(i, N) REP(j, N) if (A[i][j]) { pos[i].push_back(j); } vector<vector<mint>> dp(N + 1, vector<mint>(1 << N)); dp[0][0] = 1; FOR(i, 1, N + 1) { for (int j : pos[i - 1]) { //(i-1)からjを使用する REP(k, (1 << N)) if ((k & (1 << j))) { int src = (k & ~(1 << j)); dp[i][k] += dp[i - 1][src]; } } } cout << dp.back().back() << endl; }
replace
305
306
305
306
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define debug(x) cerr << #x << ": " << x << endl #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<vector<bool>> canmatch(n, vector<bool>(n)); rep(i, 0, n) rep(j, 0, n) { int x; cin >> x; canmatch[i][j] = x == 1; } vector<ll> dp(1 << n, 0); dp[0] = 1; const ll MOD = 1e9 + 7; rep(i, 0, n) { vector<ll> next(1 << n, 0); rep(mask, 0, 1 << n) { rep(j, 0, n) if (canmatch[i][j] && (mask & (1 << j)) == 0) { next[mask | (1 << j)] = (next[mask | (1 << j)] + dp[mask]) % MOD; } } swap(next, dp); } ll ans = dp[(1 << n) - 1]; cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define debug(x) cerr << #x << ": " << x << endl #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<vector<bool>> canmatch(n, vector<bool>(n)); rep(i, 0, n) rep(j, 0, n) { int x; cin >> x; canmatch[i][j] = x == 1; } vector<ll> dp(1 << n, 0); dp[0] = 1; const ll MOD = 1e9 + 7; rep(i, 0, n) { vector<ll> next(1 << n, 0); rep(mask, 0, 1 << n) if (dp[mask] != 0) { rep(j, 0, n) if (canmatch[i][j] && (mask & (1 << j)) == 0) { next[mask | (1 << j)] = (next[mask | (1 << j)] + dp[mask]) % MOD; } } swap(next, dp); } ll ans = dp[(1 << n) - 1]; cout << ans << "\n"; }
replace
27
28
27
28
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define FASTIO \ cin.tie(0); \ cout.tie(0); \ ios_base::sync_with_stdio(false); #define loop(i, a, b) for (int i = a; i < b; i++) #define INF 10000000000099ll #define mod 1000000007 #define prDouble(x) cout << fixed << setprecision(10) << x ll dp[22][(1 << 22)]; ll solve(vector<vector<int>> &compat, int i, int womenSubset, int n) { if (i == n + 1) { if (womenSubset == 0) return 1; return 0; } if (dp[i][womenSubset] != -1) { return dp[i][womenSubset]; } ll ans = 0; for (int women = 0; women < n; women++) { bool available = ((1 << women) & (womenSubset) == 0) ? 0 : 1; if (available && compat[i][women + 1]) { ans = (ans + solve(compat, i + 1, womenSubset ^ (1 << women), n)) % mod; } } return dp[i][womenSubset] = ans; } int main() { FASTIO; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<vector<int>> compat(n + 1, vector<int>(n + 1)); memset(dp, -1, sizeof dp); loop(i, 1, n + 1) loop(j, 1, n + 1) cin >> compat[i][j]; cout << solve(compat, 1, ((1 << (n)) - 1), n); // 2^n-1 return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define FASTIO \ cin.tie(0); \ cout.tie(0); \ ios_base::sync_with_stdio(false); #define loop(i, a, b) for (int i = a; i < b; i++) #define INF 10000000000099ll #define mod 1000000007 #define prDouble(x) cout << fixed << setprecision(10) << x ll dp[22][(1 << 22)]; ll solve(vector<vector<int>> &compat, int i, int womenSubset, int n) { if (i == n + 1) { if (womenSubset == 0) return 1; return 0; } if (dp[i][womenSubset] != -1) { return dp[i][womenSubset]; } ll ans = 0; for (int women = 0; women < n; women++) { bool available = (((1 << women) & (womenSubset)) == 0) ? 0 : 1; if (available && compat[i][women + 1]) { ans = (ans + solve(compat, i + 1, womenSubset ^ (1 << women), n)) % mod; } } return dp[i][womenSubset] = ans; } int main() { FASTIO; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<vector<int>> compat(n + 1, vector<int>(n + 1)); memset(dp, -1, sizeof dp); loop(i, 1, n + 1) loop(j, 1, n + 1) cin >> compat[i][j]; cout << solve(compat, 1, ((1 << (n)) - 1), n); // 2^n-1 return 0; }
replace
26
27
26
27
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mo 1000000007 #define ll long long int int n; int a[22][22]; ll dp[1500000][22]; ll func(int bm, int i) { if (i >= n) return (1); if (dp[bm][i] != -1) return (dp[bm][i]); ll ans = 0; for (int j = 0; j < n; j++) { int p = 1 << j; p = bm & p; if (a[i][j] && (p)) { int s = bm ^ p; ans = (ans + func(s, i + 1)) % mo; } } return (dp[bm][i] = ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> a[i][j]; } int bm = 0; for (int j = 0; j < n; j++) { bm = bm << 1; bm = bm | 1; } memset(dp, -1, sizeof(dp)); cout << func(bm, 0); return (0); }
#include <bits/stdc++.h> using namespace std; #define mo 1000000007 #define ll long long int int n; int a[22][22]; ll dp[2200000][22]; ll func(int bm, int i) { if (i >= n) return (1); if (dp[bm][i] != -1) return (dp[bm][i]); ll ans = 0; for (int j = 0; j < n; j++) { int p = 1 << j; p = bm & p; if (a[i][j] && (p)) { int s = bm ^ p; ans = (ans + func(s, i + 1)) % mo; } } return (dp[bm][i] = ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> a[i][j]; } int bm = 0; for (int j = 0; j < n; j++) { bm = bm << 1; bm = bm | 1; } memset(dp, -1, sizeof(dp)); cout << func(bm, 0); return (0); }
replace
6
7
6
7
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define DIM 23 #define INF 1000000000000000007LL #define modulo 1000000007 using namespace std; long long n, a[DIM][DIM], d[DIM][1 << 21]; bool isbit(int mask, int num) { return (mask >> (num - 1)) & 1; } void sbit(int &mask, int num) { mask ^= 1 << (num - 1); } int main() { cin >> n; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) cin >> a[i][j]; d[0][0] = 1; for (int i = 1; i <= n; ++i) for (int mask = 0; mask < (1 << n); ++mask) for (int j = 1; j <= n; ++j) if (isbit(mask, j) && a[i][j]) { int nmask = mask; sbit(nmask, j); d[i][mask] += d[i - 1][nmask]; d[i][mask] %= modulo; } cout << d[n][(1 << n) - 1]; return 0; }
#include <bits/stdc++.h> #define DIM 23 #define INF 1000000000000000007LL #define modulo 1000000007 using namespace std; long long n, a[DIM][DIM], d[DIM][1 << 21]; bool isbit(int mask, int num) { return (mask >> (num - 1)) & 1; } void sbit(int &mask, int num) { mask ^= 1 << (num - 1); } int main() { cin >> n; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) cin >> a[i][j]; d[0][0] = 1; for (int mask = 0; mask < (1 << n); ++mask) { int i = __builtin_popcount(mask); for (int j = 1; j <= n; ++j) if (isbit(mask, j) && a[i][j]) { int nmask = mask; sbit(nmask, j); d[i][mask] += d[i - 1][nmask]; d[i][mask] %= modulo; } } cout << d[n][(1 << n) - 1]; return 0; }
replace
17
26
17
28
TLE
p03174
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; #define endl "\n" typedef long long int ll; inline void fastio() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } inline void setPrecision(int n) { cout.precision(n); } // DEBUG #define dbg(x) cerr << (#x) << ": " << x << endl #define dbgV(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << it << " "; \ cerr << endl; #define dbgS(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << it << " "; \ cerr << endl; #define dbgM(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << "[" << it.f << ", " << it.s << "] "; \ cerr << endl; #define dbg2D(x) \ cerr << (#x) << ": \n"; \ for (auto y : x) { \ for (auto it : y) \ cerr << it << " "; \ cerr << endl; \ } \ cerr << endl; #define dbgA(x, n) \ cerr << (#x) << ": "; \ for (int i = 0; i < n; ++i) \ cerr << x[i] << " "; \ cerr << endl; #define dbgVP(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << "[" << it.f << ", " << it.s << "] "; \ cerr << endl; ll INF = 1e10; ll MOD = 1e9 + 7; int n; bool a[22][22]; bool women[22]; int done[(1 << 22)]; int base = 0; ll pairs(int now) { if (now == n) return 1; if (done[base] > 0) return done[base]; ll ans = 0; for (int j = 0; j < n; ++j) { if (a[now][j] && !women[j]) { women[j] = 1; base += 1 << j; ans += pairs(now + 1); women[j] = 0; base -= 1 << j; if (ans >= MOD) ans -= MOD; } } done[base] = ans; // dbg(now); dbg(ans); dbgA(women, n); cerr<<endl; return ans; } int main() { #ifndef ONLINE_JUDGE freopen("debug.txt", "w", stderr); #endif fastio(); cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { int x; cin >> x; a[i][j] = x == 1; } memset(done, -1, sizeof(done)); cout << pairs(0); return 0; }
#include "bits/stdc++.h" using namespace std; #define endl "\n" typedef long long int ll; inline void fastio() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } inline void setPrecision(int n) { cout.precision(n); } // DEBUG #define dbg(x) cerr << (#x) << ": " << x << endl #define dbgV(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << it << " "; \ cerr << endl; #define dbgS(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << it << " "; \ cerr << endl; #define dbgM(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << "[" << it.f << ", " << it.s << "] "; \ cerr << endl; #define dbg2D(x) \ cerr << (#x) << ": \n"; \ for (auto y : x) { \ for (auto it : y) \ cerr << it << " "; \ cerr << endl; \ } \ cerr << endl; #define dbgA(x, n) \ cerr << (#x) << ": "; \ for (int i = 0; i < n; ++i) \ cerr << x[i] << " "; \ cerr << endl; #define dbgVP(x) \ cerr << (#x) << ": "; \ for (auto it : x) \ cerr << "[" << it.f << ", " << it.s << "] "; \ cerr << endl; ll INF = 1e10; ll MOD = 1e9 + 7; int n; bool a[22][22]; bool women[22]; int done[(1 << 22)]; int base = 0; ll pairs(int now) { if (now == n) return 1; if (done[base] >= 0) return done[base]; ll ans = 0; for (int j = 0; j < n; ++j) { if (a[now][j] && !women[j]) { women[j] = 1; base += 1 << j; ans += pairs(now + 1); women[j] = 0; base -= 1 << j; if (ans >= MOD) ans -= MOD; } } done[base] = ans; // dbg(now); dbg(ans); dbgA(women, n); cerr<<endl; return ans; } int main() { #ifndef ONLINE_JUDGE freopen("debug.txt", "w", stderr); #endif fastio(); cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { int x; cin >> x; a[i][j] = x == 1; } memset(done, -1, sizeof(done)); cout << pairs(0); return 0; }
replace
63
64
63
64
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; bool grid[22][22]; int N = 22, n, mod = 1e9 + 7, dp[22][22]; int solve(int m, int w) { if (m == n) { if (w == 0) return 1; return 0; } if (dp[m][w] != -1) return dp[m][w]; int ans = 0; for (int i = 0; i < n; i++) { bool okay = ((((1 << i) & w) == 0) ? 0 : 1); if (okay && grid[m][i]) ans = (ans + solve(m + 1, (1 << i) ^ w)) % mod; } return dp[m][w] = ans; } int main() { cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> grid[i][j]; memset(dp, -1, sizeof(dp)); cout << solve(0, (1 << n) - 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 24; bool grid[24][24]; long long n, mod = 1e9 + 7, dp[24][1 << 22]; int solve(int m, int w) { if (m == n) { if (w == 0) return 1; return 0; } if (dp[m][w] != -1) return dp[m][w]; int ans = 0; for (int i = 0; i < n; i++) { bool okay = ((((1 << i) & w) == 0) ? 0 : 1); if (okay && grid[m][i]) ans = (ans + solve(m + 1, (1 << i) ^ w)) % mod; } return dp[m][w] = ans; } int main() { cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> grid[i][j]; memset(dp, -1, sizeof(dp)); cout << solve(0, (1 << n) - 1); return 0; }
replace
2
4
2
5
0
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long using namespace std; int n; int maska[25]; int mod = 1e9 + 7; int dp[25][(1 << 22)]; int main() { cin >> n; for (int i = 1; n >= i; i++) { int val = 1; for (int j = 0; n > j; j++) { int a; cin >> a; if (a == 1) maska[i] ^= val; val *= 2; } } dp[0][(1LL << n) - 1] = 1; for (int i = 1; n >= i; i++) { for (int mas = 0; (1 << n) > mas; mas++) { for (int j = 0; n > j; j++) { if (((1 << j) & mas) == 0 and (maska[i] & (1 << j)) > 0) dp[i][mas] = (dp[i][mas] + dp[i - 1][mas ^ (1 << j)]) % mod; } } } cout << dp[n][0]; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; int n; int maska[25]; int mod = 1e9 + 7; int dp[25][(1 << 22)]; int main() { cin >> n; for (int i = 1; n >= i; i++) { int val = 1; for (int j = 0; n > j; j++) { int a; cin >> a; if (a == 1) maska[i] ^= val; val *= 2; } } dp[0][(1LL << n) - 1] = 1; for (int i = 1; n >= i; i++) { for (int mas = 0; (1 << n) > mas; mas++) { if (__builtin_popcount(mas) != n - i) continue; for (int j = 0; n > j; j++) { if (((1 << j) & mas) == 0 and (maska[i] & (1 << j)) > 0) dp[i][mas] = (dp[i][mas] + dp[i - 1][mas ^ (1 << j)]) % mod; } } } cout << dp[n][0]; return 0; }
insert
22
22
22
24
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using lint = long long int; using ulint = unsigned long long int; template <class T = int> using V = vector<T>; template <class T = int> using VV = V<V<T>>; template <class T, class U> void assign(V<T> &v, int n, const U &a) { v.assign(n, a); } template <class T, class... Args> void assign(V<T> &v, int n, const Args &...args) { v.resize(n); for (auto &&e : v) assign(e, args...); } template <class Int> constexpr bool is_prime(Int n) { if (n < 2 or n > 2 and ~n & 1) return false; for (Int i = 3; i * i <= n; i += 2) if (n % i == 0) return false; return true; } template <uint P> struct ModInt { static_assert(is_prime(P), "template argument must be a prime number"); using M = ModInt; uint v; ModInt() : v(0) {} template <class Int> ModInt(Int x) : v(x >= 0 ? x % P : -x % P ? P - -x % P : 0) {} constexpr ModInt(uint v, int _) : v(v) {} // 直接値を初期化 static constexpr uint p() { return P; } M operator++(int) { M t = *this; if (++v == P) v = 0; return t; } M operator--(int) { M t = *this; v = (v ? v : P) - 1; return t; } M &operator++() { if (++v == P) v = 0; return *this; } M &operator--() { v = (v ? v : P) - 1; return *this; } M operator+() const { return *this; } M operator-() const { return {v ? P - v : 0, 0}; } explicit operator bool() const noexcept { return v; } bool operator!() const noexcept { return !static_cast<bool>(*this); } M operator*(M rhs) const { return M(*this) *= rhs; } M operator/(M rhs) const { return M(*this) /= rhs; } M operator+(M rhs) const { return M(*this) += rhs; } M operator-(M rhs) const { return M(*this) -= rhs; } bool operator==(M rhs) { return v == rhs.v; } bool operator!=(M rhs) { return !(*this == rhs); } M &operator*=(M rhs) { v = (ulint)v * rhs.v % P; return *this; } M &operator/=(M rhs) { return *this *= rhs.inv(); } M &operator+=(M rhs) { v = rhs.v < P - v ? v + rhs.v : v - (P - rhs.v); return *this; } M &operator-=(M rhs) { v = rhs.v <= v ? v - rhs.v : v + (P - rhs.v); return *this; } M inv() const { assert(v); #ifdef __linux__ // ACとCFで動作を変えるための仮の対処 return pow(P - 2); #else int a = v, b = P, x = 1, u = 0; while (b) { int q = a / b; swap(a -= q * b, b); swap(x -= q * u, u); } return x; #endif } template <class Int> M pow(Int n) const { n = n >= 0 ? n % (P - 1) : P - 1 - -n % (P - 1); M res = 1; for (M a = *this; n > 0; a *= a, n >>= 1) if (n & 1) res *= a; return res; } template <class Int> friend M operator*(Int lhs, M rhs) { return M(lhs) *= rhs; } template <class Int> friend M operator/(Int lhs, M rhs) { return M(lhs) /= rhs; } template <class Int> friend M operator+(Int lhs, M rhs) { return M(lhs) += rhs; } template <class Int> friend M operator-(Int lhs, M rhs) { return M(lhs) -= rhs; } friend ostream &operator<<(ostream &os, M rhs) { return os << rhs.v; } friend istream &operator>>(istream &is, M &rhs) { lint x; is >> x; rhs = x; return is; } template <class Int> friend bool operator==(Int lhs, M rhs) { return M(lhs) == rhs; } template <class Int> friend bool operator!=(Int lhs, M rhs) { return !(lhs == rhs); } }; using Mint = ModInt<static_cast<uint>(1e9 + 7)>; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; cin >> n; VV<> a; assign(a, n, n, 0); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> a[i][j]; VV<Mint> dp; assign(dp, n + 1, 1 << n, 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] and !(bit & 1 << j)) { dp[i + 1][bit | 1 << j] += dp[i][bit]; } } } cout << dp[n].back() << '\n'; }
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using lint = long long int; using ulint = unsigned long long int; template <class T = int> using V = vector<T>; template <class T = int> using VV = V<V<T>>; template <class T, class U> void assign(V<T> &v, int n, const U &a) { v.assign(n, a); } template <class T, class... Args> void assign(V<T> &v, int n, const Args &...args) { v.resize(n); for (auto &&e : v) assign(e, args...); } template <class Int> constexpr bool is_prime(Int n) { if (n < 2 or n > 2 and ~n & 1) return false; for (Int i = 3; i * i <= n; i += 2) if (n % i == 0) return false; return true; } template <uint P> struct ModInt { static_assert(is_prime(P), "template argument must be a prime number"); using M = ModInt; uint v; ModInt() : v(0) {} template <class Int> ModInt(Int x) : v(x >= 0 ? x % P : -x % P ? P - -x % P : 0) {} constexpr ModInt(uint v, int _) : v(v) {} // 直接値を初期化 static constexpr uint p() { return P; } M operator++(int) { M t = *this; if (++v == P) v = 0; return t; } M operator--(int) { M t = *this; v = (v ? v : P) - 1; return t; } M &operator++() { if (++v == P) v = 0; return *this; } M &operator--() { v = (v ? v : P) - 1; return *this; } M operator+() const { return *this; } M operator-() const { return {v ? P - v : 0, 0}; } explicit operator bool() const noexcept { return v; } bool operator!() const noexcept { return !static_cast<bool>(*this); } M operator*(M rhs) const { return M(*this) *= rhs; } M operator/(M rhs) const { return M(*this) /= rhs; } M operator+(M rhs) const { return M(*this) += rhs; } M operator-(M rhs) const { return M(*this) -= rhs; } bool operator==(M rhs) { return v == rhs.v; } bool operator!=(M rhs) { return !(*this == rhs); } M &operator*=(M rhs) { v = (ulint)v * rhs.v % P; return *this; } M &operator/=(M rhs) { return *this *= rhs.inv(); } M &operator+=(M rhs) { v = rhs.v < P - v ? v + rhs.v : v - (P - rhs.v); return *this; } M &operator-=(M rhs) { v = rhs.v <= v ? v - rhs.v : v + (P - rhs.v); return *this; } M inv() const { assert(v); #ifdef __linux__ // ACとCFで動作を変えるための仮の対処 return pow(P - 2); #else int a = v, b = P, x = 1, u = 0; while (b) { int q = a / b; swap(a -= q * b, b); swap(x -= q * u, u); } return x; #endif } template <class Int> M pow(Int n) const { n = n >= 0 ? n % (P - 1) : P - 1 - -n % (P - 1); M res = 1; for (M a = *this; n > 0; a *= a, n >>= 1) if (n & 1) res *= a; return res; } template <class Int> friend M operator*(Int lhs, M rhs) { return M(lhs) *= rhs; } template <class Int> friend M operator/(Int lhs, M rhs) { return M(lhs) /= rhs; } template <class Int> friend M operator+(Int lhs, M rhs) { return M(lhs) += rhs; } template <class Int> friend M operator-(Int lhs, M rhs) { return M(lhs) -= rhs; } friend ostream &operator<<(ostream &os, M rhs) { return os << rhs.v; } friend istream &operator>>(istream &is, M &rhs) { lint x; is >> x; rhs = x; return is; } template <class Int> friend bool operator==(Int lhs, M rhs) { return M(lhs) == rhs; } template <class Int> friend bool operator!=(Int lhs, M rhs) { return !(lhs == rhs); } }; using Mint = ModInt<static_cast<uint>(1e9 + 7)>; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; cin >> n; VV<> a; assign(a, n, n, 0); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> a[i][j]; VV<Mint> dp; assign(dp, n + 1, 1 << n, 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) { for (int j = 0; j < n; ++j) if (a[i][j] and !(bit & 1 << j)) { dp[i + 1][bit | 1 << j] += dp[i][bit]; } } } cout << dp[n].back() << '\n'; }
replace
144
150
144
151
TLE
p03174
C++
Memory Limit Exceeded
/* HELLO WORLD ----------- SHUBHAM ----------- :) CODING */ #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; #define ff first #define ss second #define pb push_back #define int long long #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define umii unordered_map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n] #define test(t) \ int t; \ cin >> t; \ while (t--) #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define SIZE 100005 #define all(x) (x).begin(), (x).end() #define sclk() auto start_time = Clock::now() #define eclk() \ auto end_time = Clock::now(); \ cout << "\nTime Taken : " \ << chrono::duration_cast<chrono::milliseconds>(end_time - start_time) \ .count() \ << " ms" typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef std::chrono::high_resolution_clock Clock; int powmod(int a, int b) { int res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void f_io() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE } int dp[22][1 << 23]; int arr[21][21]; int n; int solve(int r, int s) { if (r == n) { if (s == 0) return 1; return 0; } if (dp[r][s] == -1) { int ans = 0; for (int i = 0; i < n; i++) { int ck = (1 << i) & s; if (arr[r][i] and ck) { ans = (ans + solve(r + 1, s ^ (1 << i))) % mod; } } dp[r][s] = ans; } return dp[r][s]; } int32_t 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]; } } int s = (1 << n) - 1; cout << solve(0, s); return 0; }
/* HELLO WORLD ----------- SHUBHAM ----------- :) CODING */ #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; #define ff first #define ss second #define pb push_back // #define int long long #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define umii unordered_map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n] #define test(t) \ int t; \ cin >> t; \ while (t--) #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define SIZE 100005 #define all(x) (x).begin(), (x).end() #define sclk() auto start_time = Clock::now() #define eclk() \ auto end_time = Clock::now(); \ cout << "\nTime Taken : " \ << chrono::duration_cast<chrono::milliseconds>(end_time - start_time) \ .count() \ << " ms" typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef std::chrono::high_resolution_clock Clock; int powmod(int a, int b) { int res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } void f_io() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE } int dp[22][1 << 23]; int arr[21][21]; int n; int solve(int r, int s) { if (r == n) { if (s == 0) return 1; return 0; } if (dp[r][s] == -1) { int ans = 0; for (int i = 0; i < n; i++) { int ck = (1 << i) & s; if (arr[r][i] and ck) { ans = (ans + solve(r + 1, s ^ (1 << i))) % mod; } } dp[r][s] = ans; } return dp[r][s]; } int32_t 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]; } } int s = (1 << n) - 1; cout << solve(0, s); return 0; }
replace
17
18
17
18
MLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int N; char ch; cin >> N; vector<int> adj(N); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) { cin >> ch; if (ch == '1') adj[i] |= (1 << j); } vector<int> dp(1 << N); dp[0] = 1; for (int mask = 1; mask < (1 << N); ++mask) { int a = __builtin_popcount(mask), ans = 0; for (int b = mask & adj[a - 1]; b; b &= b - 1) if (!(mask & (1 << b))) { ans += dp[mask ^ (1 << b)]; if (ans >= mod) ans -= mod; } dp[mask] = ans; } cout << dp[(1 << N) - 1]; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int N; char ch; cin >> N; vector<int> adj(N); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) { cin >> ch; if (ch == '1') adj[i] |= (1 << j); } vector<int> dp(1 << N); dp[0] = 1; for (int mask = 1; mask < (1 << N); ++mask) { int a = __builtin_popcount(mask), ans = 0; for (int b = mask & adj[a - 1]; b; b &= b - 1) { ans += dp[mask & ~(b & -b)]; if (ans >= mod) ans -= mod; } dp[mask] = ans; } cout << dp[(1 << N) - 1]; }
replace
24
30
24
29
0
p03174
C++
Time Limit Exceeded
#include <algorithm> //swap function is here #include <bits/stdc++.h> #include <cassert> #include <climits> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> // istringstream buffer(myString); #include <stack> #include <vector> using namespace std; //__builtin_popcount(n); #define bit(x, i) (x & (1 << i)) // select the bit of position i of x #define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x #define hBit(msb, n) \ asm("bsrl %1,%0" \ : "=r"(msb) \ : "r"(n)) // get the highest bit of x, maybe the fastest #define max(a, b) (a < b ? b : a) #define IN(i, l, r) (l < i && i < r) // the next for are for checking bound #define LINR(i, l, r) (l <= i && i <= r) #define LIN(i, l, r) (l <= i && i < r) #define INR(i, l, r) (l < i && i <= r) #define F(i, L, R) for (int i = L; i < R; i++) // next four are for "for loops" #define FE(i, L, R) for (int i = L; i <= R; i++) #define FF(i, L, R) for (int i = L; i > R; i--) #define FFE(i, L, R) for (int i = L; i >= R; i--) #define getI(a) \ scanf("%d", &a) // next three are handy ways to get ints, it's also force you // to use '&' sign #define getII(a, b) scanf("%d%d", &a, &b) #define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c) #define wez(n) \ int(n); \ scanf( \ "%d", \ &(n)) // handy if the input is right after the definition of a variable #define wez2(n, m) \ int(n), (m); \ scanf("%d %d", &(n), &(m)) #define wez3(n, m, k) \ int(n), (m), (k); \ scanf("%d %d %d", &(n), &(m), &(k)) #define TESTS wez(testow) while (testow--) // for multilple cases problems #define whileZ \ int T; \ getI(T); \ while (T--) // the same as above #define getS(x) scanf("%s", x) // get a char* string #define clr(a, x) memset(a, x, sizeof(a)) // set array to some value #define char2Int(c) (c - '0') #define lastEle(vec) vec[vec.size() - 1] #define SZ(x) ((int)((x).size())) #define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b #define REMIN(a, b) (a) = min((a), (b)); #define FOREACH(i, t) \ for (typeof(t.begin()) i = t.begin(); i != t.end(); \ i++) // traverse an STL data structure #define ALL(c) (c).begin(), (c).end() // handy for function like "sort()" #define PRESENT(c, x) ((c).find(x) != (c).end()) #define CPRESENT(c, x) (find(ALL(c), x) != (c).end()) #define ll \ long long // data types used often, but you don't want to type them time by // time #define ull unsigned long long #define ui unsigned int #define us unsigned short #define IOS \ ios_base::sync_with_stdio(0); // to synchronize the input of cin and scanf #define INF 1001001001 #define PI 3.1415926535897932384626 // for map, pair #define mp make_pair #define fi first #define se second // for debug inline void pisz(int n) { printf("%d\n", n); } #define DBG(vari) cerr << #vari << " = " << (vari) << endl; #define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\n' : ' ') #define printV(a) printA(a, 0, a.size() - 1) #define MAXN 10000 // for vectors #define pb push_back typedef int elem_t; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; // directions const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; template <typename T, typename TT> ostream &operator<<(ostream &s, pair<T, TT> t) { return s << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream &operator<<(ostream &s, vector<T> t) { F(i, 0, SZ(t)) s << t[i] << " "; return s; } ll mod = 1000000007; ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } ll power(ll a, ll b, ll m) { ll r = 1; a = a % m; while (b) { if (b & 1) r = r * a % m; a = a * a % m; b = b >> 1; } return r; } ll findlcm(int arr[], int n) { // Initialize result ll ans = arr[0]; // ans contains LCM of arr[0], ..arr[i] // after i'th iteration, for (int i = 1; i < n; i++) ans = (((arr[i] * ans)) / (gcd(arr[i], ans))); return ans; } ll count = 0; ll dp[(1 << 21)][22]; ll recur(vector<std::vector<int>> matrix, int mask, int index) { // cout << matrix << "::\n"; if (mask == (1 << (int)(matrix.size())) - 1 && index == (int)matrix.size()) { // cout << "aya\n"; return 1; } if (dp[mask][index] != -1) return dp[mask][index]; if (index == (int)matrix.size()) return 0; dp[mask][index] = 0; for (int i = 0; i < matrix.size(); ++i) { // bool test = mask & (1 << i); // cout << test << "::\n"; if (((mask & (1 << i)) == 0) && matrix[index][i] == 1) { // cout << "tata: " << mask << " toto: " << matrix[index][i] << "\n"; int temp = mask | (1 << i); dp[mask][index] = (dp[mask][index] + recur(matrix, temp, index + 1)) % mod; } /* code */ } return dp[mask][index]; } int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; std::vector<std::vector<int>> matrix; for (int i = 0; i < n; ++i) { std::vector<int> temp; for (int j = 0; j < n; ++j) { int val; cin >> val; temp.push_back(val); /* code */ } matrix.push_back(temp); /* code */ } // cout << matrix << "\n"; int mask = 0; memset(dp, -1, sizeof(dp)); recur(matrix, mask, 0); cout << dp[0][0] << "\n"; }
#include <algorithm> //swap function is here #include <bits/stdc++.h> #include <cassert> #include <climits> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> // istringstream buffer(myString); #include <stack> #include <vector> using namespace std; //__builtin_popcount(n); #define bit(x, i) (x & (1 << i)) // select the bit of position i of x #define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x #define hBit(msb, n) \ asm("bsrl %1,%0" \ : "=r"(msb) \ : "r"(n)) // get the highest bit of x, maybe the fastest #define max(a, b) (a < b ? b : a) #define IN(i, l, r) (l < i && i < r) // the next for are for checking bound #define LINR(i, l, r) (l <= i && i <= r) #define LIN(i, l, r) (l <= i && i < r) #define INR(i, l, r) (l < i && i <= r) #define F(i, L, R) for (int i = L; i < R; i++) // next four are for "for loops" #define FE(i, L, R) for (int i = L; i <= R; i++) #define FF(i, L, R) for (int i = L; i > R; i--) #define FFE(i, L, R) for (int i = L; i >= R; i--) #define getI(a) \ scanf("%d", &a) // next three are handy ways to get ints, it's also force you // to use '&' sign #define getII(a, b) scanf("%d%d", &a, &b) #define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c) #define wez(n) \ int(n); \ scanf( \ "%d", \ &(n)) // handy if the input is right after the definition of a variable #define wez2(n, m) \ int(n), (m); \ scanf("%d %d", &(n), &(m)) #define wez3(n, m, k) \ int(n), (m), (k); \ scanf("%d %d %d", &(n), &(m), &(k)) #define TESTS wez(testow) while (testow--) // for multilple cases problems #define whileZ \ int T; \ getI(T); \ while (T--) // the same as above #define getS(x) scanf("%s", x) // get a char* string #define clr(a, x) memset(a, x, sizeof(a)) // set array to some value #define char2Int(c) (c - '0') #define lastEle(vec) vec[vec.size() - 1] #define SZ(x) ((int)((x).size())) #define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b #define REMIN(a, b) (a) = min((a), (b)); #define FOREACH(i, t) \ for (typeof(t.begin()) i = t.begin(); i != t.end(); \ i++) // traverse an STL data structure #define ALL(c) (c).begin(), (c).end() // handy for function like "sort()" #define PRESENT(c, x) ((c).find(x) != (c).end()) #define CPRESENT(c, x) (find(ALL(c), x) != (c).end()) #define ll \ long long // data types used often, but you don't want to type them time by // time #define ull unsigned long long #define ui unsigned int #define us unsigned short #define IOS \ ios_base::sync_with_stdio(0); // to synchronize the input of cin and scanf #define INF 1001001001 #define PI 3.1415926535897932384626 // for map, pair #define mp make_pair #define fi first #define se second // for debug inline void pisz(int n) { printf("%d\n", n); } #define DBG(vari) cerr << #vari << " = " << (vari) << endl; #define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\n' : ' ') #define printV(a) printA(a, 0, a.size() - 1) #define MAXN 10000 // for vectors #define pb push_back typedef int elem_t; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; // directions const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; template <typename T, typename TT> ostream &operator<<(ostream &s, pair<T, TT> t) { return s << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream &operator<<(ostream &s, vector<T> t) { F(i, 0, SZ(t)) s << t[i] << " "; return s; } ll mod = 1000000007; ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } ll power(ll a, ll b, ll m) { ll r = 1; a = a % m; while (b) { if (b & 1) r = r * a % m; a = a * a % m; b = b >> 1; } return r; } ll findlcm(int arr[], int n) { // Initialize result ll ans = arr[0]; // ans contains LCM of arr[0], ..arr[i] // after i'th iteration, for (int i = 1; i < n; i++) ans = (((arr[i] * ans)) / (gcd(arr[i], ans))); return ans; } ll count = 0; ll dp[(1 << 21)][22]; ll recur(vector<std::vector<int>> &matrix, int mask, int index) { // cout << matrix << "::\n"; if (mask == (1 << (int)(matrix.size())) - 1 && index == (int)matrix.size()) { // cout << "aya\n"; return 1; } if (dp[mask][index] != -1) return dp[mask][index]; if (index == (int)matrix.size()) return 0; dp[mask][index] = 0; for (int i = 0; i < matrix.size(); ++i) { // bool test = mask & (1 << i); // cout << test << "::\n"; if (((mask & (1 << i)) == 0) && matrix[index][i] == 1) { // cout << "tata: " << mask << " toto: " << matrix[index][i] << "\n"; int temp = mask | (1 << i); dp[mask][index] = (dp[mask][index] + recur(matrix, temp, index + 1)) % mod; } /* code */ } return dp[mask][index]; } int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; std::vector<std::vector<int>> matrix; for (int i = 0; i < n; ++i) { std::vector<int> temp; for (int j = 0; j < n; ++j) { int val; cin >> val; temp.push_back(val); /* code */ } matrix.push_back(temp); /* code */ } // cout << matrix << "\n"; int mask = 0; memset(dp, -1, sizeof(dp)); recur(matrix, mask, 0); cout << dp[0][0] << "\n"; }
replace
141
142
141
142
TLE
p03174
C++
Runtime Error
#pragma GCC optimize("Ofast", "unroll-loops") #include <bits/stdc++.h> #define pb push_back #define eb emplace_back #define MP make_pair #define F first #define S second #define MEM(a, b) memset(a, b, sizeof a) #define Tie ios::sync_with_stdio(0), cin.tie(0); using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; int n, sn, cnt, tmp, a[21][21], dp[1048577]; const int MOD = 1e9 + 7; int main() { Tie cin >> n, sn = 1 << n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; dp[0] = 1; for (int i = 1; i < sn; i++) { tmp = i, cnt = -1; while (tmp) cnt++, tmp -= tmp & -tmp; for (int j = 1, rj = 0; j < sn; j <<= 1, rj++) if ((i & j) && a[cnt][rj]) dp[i] = (dp[i] + dp[i ^ j]) % MOD; } cout << dp[sn - 1] << '\n'; }
#pragma GCC optimize("Ofast", "unroll-loops") #include <bits/stdc++.h> #define pb push_back #define eb emplace_back #define MP make_pair #define F first #define S second #define MEM(a, b) memset(a, b, sizeof a) #define Tie ios::sync_with_stdio(0), cin.tie(0); using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; int n, sn, cnt, tmp, a[21][21], dp[2097153]; const int MOD = 1e9 + 7; int main() { Tie cin >> n, sn = 1 << n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; dp[0] = 1; for (int i = 1; i < sn; i++) { tmp = i, cnt = -1; while (tmp) cnt++, tmp -= tmp & -tmp; for (int j = 1, rj = 0; j < sn; j <<= 1, rj++) if ((i & j) && a[cnt][rj]) dp[i] = (dp[i] + dp[i ^ j]) % MOD; } cout << dp[sn - 1] << '\n'; }
replace
14
15
14
15
0
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif void Main() { int N; cin >> N; vector<vector<int>> info(N, vector<int>(N)); rep(i, N) { rep(j, N) { cin >> info[i][j]; } } ll S = 1 << N; vector<vector<ll>> dp(N + 1, vector<ll>(S)); dp[0][0] = 1; rep(i, N) { rep(j, S) { rep(w, N) { if (info[i][w] == 1 && (j & (1 << w)) == 0) { dp[i + 1][j | (1 << w)] += dp[i][j]; dp[i + 1][j | (1 << w)] %= MOD; } } } } cout << dp[N][S - 1] << '\n'; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif void Main() { int N; cin >> N; vector<vector<int>> info(N, vector<int>(N)); rep(i, N) { rep(j, N) { cin >> info[i][j]; } } ll S = 1 << N; vector<vector<ll>> dp(N + 1, vector<ll>(S)); dp[0][0] = 1; rep(i, N) { rep(j, S) { if (__builtin_popcount(j) != i) continue; rep(w, N) { if (info[i][w] == 1 && (j & (1 << w)) == 0) { dp[i + 1][j | (1 << w)] += dp[i][j]; dp[i + 1][j | (1 << w)] %= MOD; } } } } cout << dp[N][S - 1] << '\n'; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
insert
38
38
38
40
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // Define using ll = long long; using ull = unsigned long long; using ld = long double; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, 1, 0, -1}; const ll MOD = 1e9 + 7; const ll mod = 998244353; const ll inf = 1 << 30; const ll LINF = LONG_MAX; const ll INF = 1LL << 60; const ull MAX = ULONG_MAX; #define mp make_pair #define pb push_back #define elif else if #define endl '\n' #define space ' ' #define def inline auto #define func inline constexpr ll #define run(a) __attribute__((constructor)) def _##a() #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) #define input(a) scanf("%lld", &(a)) #define print(a) printf("%lld\n", (a)) #define fi first #define se second #define ok(a, b) (0 <= (a) && (a) < (b)) template <class T> using vvector = vector<vector<T>>; template <class T> using pvector = vector<pair<T, T>>; template <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>; 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; } // Debug #define debug(...) \ { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ for (auto &&X : {__VA_ARGS__}) \ cerr << "[" << X << "] "; \ cerr << endl; \ } #define dump(a, h, w) \ { \ cerr << __LINE__ << ": " << #a << " = [" << endl; \ rep(__i, h) { \ rep(__j, w) { cerr << a[__i][__j] << space; } \ cerr << endl; \ } \ cerr << "]" << endl; \ } #define vdump(a, n) \ { \ cerr << __LINE__ << ": " << #a << " = ["; \ rep(__i, n) if (__i) cerr << space << a[__i]; \ else cerr << a[__i]; \ cerr << "]" << endl; \ } // Loop #define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i) #define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i) #define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i) #define each(i, a) for (auto &&i : a) #define loop() for (;;) // Stream #define fout(n) cout << fixed << setprecision(n) #define fasten cin.tie(0), ios::sync_with_stdio(0) run(0) { fasten, fout(10); } // Speed #pragma GCC optimize("O3") #pragma GCC target("avx") // Math func gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } func lcm(ll a, ll b) { return a / gcd(a, b) * b; } func sign(ll a) { return a ? abs(a) / a : 0; } inline constexpr ll modulo(const ll n, const ll m = MOD) { ll k = n % m; return k + m * (k < 0); } inline constexpr ll chmod(ll &n, const ll m = MOD) { n %= m; return n += m * (n < 0); } inline constexpr ll mpow(ll a, ll n, const ll m = MOD) { ll r = 1; while (n) { if (n & 1) r *= a; chmod(r, m); a *= a; chmod(a, m); n >>= 1; } return r; } inline ll inv(const ll n, const ll m = MOD) { ll a = n, b = m, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return modulo(x, m); } struct Factor { inline vector<ll> factors(ll N) { vector<ll> A; ll i = 2; while (i * i <= N) { if (N % i == 0) { A.push_back(i); N /= i; } else { i++; } } if (N != 1) A.push_back(N); sort(all(A)); return A; } inline vector<ll> divisor(ll N) { vector<ll> A; inc(i, 1, sqrt(N)) { if (N % i == 0) { A.push_back(i); if (i * i != N) A.push_back(N / i); } } sort(all(A)); return A; } }; struct csum { ll N; vector<ll> A, S; csum(vector<ll> v) : A(v), S(1) { each(k, v) S.push_back(k + S.back()); } ll sum(ll l, ll r) { return S[r] - S[l]; } ll lsum(ll r) { return S[r]; } ll rsum(ll l) { return S.back() - S[l]; } }; template <ull mod = MOD> struct mi { inline constexpr ll modulo(const ll n, const ll m) const noexcept { ll k = n % m; return k + m * (k < 0); } ll num; inline constexpr mi() noexcept : num() { num = 0; } inline constexpr mi(const int n) noexcept : num() { num = modulo(n, mod); } inline constexpr mi(const ll n) noexcept : num() { num = modulo(n, mod); } inline constexpr mi<mod> inv() const noexcept { ll a = num, b = mod, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return mi<mod>(x); } inline constexpr mi<mod> inv(ll n) const noexcept { ll a = n, b = mod, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return mi<mod>(x); } inline constexpr mi<mod> inv(const mi<mod> m) const noexcept { return inv(m.num); } inline constexpr mi<mod> operator+() const noexcept { return mi(num); } inline constexpr mi<mod> operator+(const int n) const noexcept { return mi<mod>(num + n); } inline constexpr mi<mod> operator+(const ll n) const noexcept { return mi<mod>(num + n); } inline constexpr mi<mod> operator+(const mi<mod> m) const noexcept { return mi<mod>(num + m.num); } inline constexpr mi<mod> operator-() const noexcept { return -num; } inline constexpr mi<mod> operator-(const int n) const noexcept { return mi<mod>(num - n); } inline constexpr mi<mod> operator-(const ll n) const noexcept { return mi<mod>(num - n); } inline constexpr mi<mod> operator-(const mi<mod> m) const noexcept { return mi<mod>(num - m.num); } inline constexpr mi<mod> operator*(const int n) const noexcept { return mi<mod>(num * n); } inline constexpr mi<mod> operator*(const ll n) const noexcept { return mi<mod>(num * n); } inline constexpr mi<mod> operator*(const mi<mod> m) const noexcept { return mi<mod>(num * m); } inline constexpr mi<mod> operator/(const int n) const noexcept { return mi<mod>(num * (ll)inv(n)); } inline constexpr mi<mod> operator/(const ll n) const noexcept { return mi<mod>(num * (ll)inv(n)); } inline constexpr mi<mod> operator/(const mi<mod> m) const noexcept { return mi<mod>(num * (ll)inv(m)); } inline constexpr mi<mod> &operator=(const int n) noexcept { num = modulo(n, mod); return *this; } inline constexpr mi<mod> &operator=(const ll n) noexcept { num = modulo(n, mod); return *this; } inline constexpr mi<mod> &operator=(const mi<mod> m) noexcept { num = m.num; return *this; } inline constexpr mi<mod> &operator+=(const int n) noexcept { num = modulo(num + n, mod); return *this; } inline constexpr mi<mod> &operator+=(const ll n) noexcept { num = modulo(num + n, mod); return *this; } inline constexpr mi<mod> &operator+=(const mi<mod> m) noexcept { num = modulo(num + m.num, mod); return *this; } inline constexpr mi<mod> &operator++() noexcept { num = modulo(num + 1, mod); return *this; } inline constexpr mi<mod> operator++(int) noexcept { mi &pre = *this; num = modulo(num + 1, mod); return pre; } inline constexpr mi<mod> &operator-=(const int n) noexcept { num = modulo(num - n, mod); return *this; } inline constexpr mi<mod> &operator-=(const ll n) noexcept { num = modulo(num - n, mod); return *this; } inline constexpr mi<mod> &operator-=(const mi<mod> m) noexcept { num = modulo(num - m.num, mod); return *this; } inline constexpr mi<mod> &operator--() noexcept { num = modulo(num - 1, mod); return *this; } inline constexpr mi<mod> operator--(int) noexcept { mi &pre = *this; num = modulo(num - 1, mod); return pre; } inline constexpr mi<mod> &operator*=(const int n) noexcept { num = modulo(num * n, mod); return *this; } inline constexpr mi<mod> &operator*=(const ll n) noexcept { num = modulo(num * n, mod); return *this; } inline constexpr mi<mod> &operator*=(const mi<mod> m) noexcept { num = modulo(num * m.num, mod); return *this; } inline constexpr mi<mod> &operator/=(const int n) noexcept { num = modulo(num * (ll)inv(n), mod); return *this; } inline constexpr mi<mod> &operator/=(const ll n) noexcept { num = modulo(num * (ll)inv(n), mod); return *this; } inline constexpr mi<mod> &operator/=(const mi<mod> m) noexcept { num = modulo(num * (ll)inv(m), mod); return *this; } inline constexpr mi<mod> operator==(const int n) const noexcept { return num == modulo(n, mod); } inline constexpr mi<mod> operator==(const ll n) const noexcept { return num == modulo(n, mod); } inline constexpr mi<mod> operator==(const mi<mod> m) const noexcept { return num == m.num; } inline constexpr mi<mod> operator!=(const int n) const noexcept { return num != modulo(n, mod); } inline constexpr mi<mod> operator!=(const ll n) const noexcept { return num != modulo(n, mod); } inline constexpr mi<mod> operator!=(const mi<mod> m) const noexcept { return num != m.num; } constexpr operator int() const noexcept { return num; } constexpr operator ll() const noexcept { return num; } friend std::istream &operator>>(std::istream &, const mi<> &); friend std::ostream &operator<<(std::ostream &, const mi<> &); }; template <ull mod = MOD> inline constexpr mi<mod> operator+(const int n, const mi<mod> m) noexcept { return mi<mod>(n + m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator+(const ll n, const mi<mod> m) noexcept { return mi<mod>(n + m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator-(const int n, const mi<mod> m) noexcept { return mi<mod>(n - m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator-(const ll n, const mi<mod> m) noexcept { return mi<mod>(n - m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator*(const int n, const mi<mod> m) noexcept { return mi<mod>(n * m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator*(const ll n, const mi<mod> m) noexcept { return mi<mod>(n * m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator/(const int n, const mi<mod> m) noexcept { return mi<mod>(n * (ll)m.inv()); } template <ull mod = MOD> inline constexpr mi<mod> operator/(const ll n, const mi<mod> m) noexcept { return mi<mod>(n * (ll)m.inv()); } inline constexpr mi<MOD> operator""_m(ull n) { return mi<MOD>((ll)n); } template <ull mod = MOD> inline constexpr mi<mod> pow(mi<mod> m, ll n) noexcept { mi<mod> r = mi<mod>(1); while (n) { if (n & 1) r *= m; m *= m; n >>= 1; } return r; } template <ull mod> istream &operator>>(istream &is, mi<mod> &m) { is >> m.num; return is; } template <ull mod> ostream &operator<<(ostream &is, mi<mod> &m) { is << (ll)m; return is; } template <ull mod = MOD> struct modmath { ll max; vector<mi<mod>> fac, inv; modmath() : max(1 << 20), fac(max + 1), inv(max + 1) { fac[0] = mi<mod>(1); rep(i, max) fac[i + 1] = fac[i] * (i + 1); inv[max] = fac[max].inv(); dec(i, max - 1, 0) inv[i] = inv[i + 1] * (i + 1); } modmath(ll n) : max(n), fac(n + 1), inv(n + 1) { fac[0] = 1; rep(i, n) fac[i + 1] = fac[i] * (i + 1); inv[n] = 1 / fac[n]; dec(i, n - 1, 0) inv[i] = inv[i + 1] * (i + 1); } inline mi<mod> fact(ll n) { if (n < 0) return mi<mod>(0); return fac[n]; } inline mi<mod> perm(ll n, ll r) { if (r < 0 || n < r) return mi<mod>(0); return fac[n] * inv[n - r]; } inline mi<mod> comb(ll n, ll r) { if (r < 0 || n < r) return mi<mod>(0); return fac[n] * inv[r] * inv[n - r]; } inline mi<mod> nHr(ll n, ll r) { return comb(n + r - 1, n - 1); } }; struct UFR { vector<ll> data; UFR(ll N) : data(N) { rep(i, N) data[i] = -1; } def root(ll x) { if (data[x] < 0) return x; else return data[x] = root(data[x]); } def unite(ll x, ll y) { ll root_x = root(x), root_y = root(y); if (root_x != root_y) { if (data[root_x] > data[root_y]) swap(root_x, root_y); data[root_x] -= data[root_x] == data[root_y]; data[root_y] = root_x; return true; } return false; } def same(ll x, ll y) { return root(x) == root(y); } }; struct position { ll x, y; position() {} position(ll a, ll b) : x(a), y(b) {} position next(ll i) { return {x + dx[i], y + dy[i]}; } ll mdist() { return abs(x) + abs(y); } double dist() { return sqrt(x * x + y * y); } double norm(ll d) { if (d == inf) return max(x, y); if (d == 1) return mdist(); if (d == 2) return dist(); return 0; } ll num(ll width) { return abs(x) * width + abs(y); } bool operator==(position a) { return x == a.x && y == a.y; } bool operator!=(position a) { return x != a.x || y != a.y; } bool operator<(position a) { return x < a.x && y < a.y; } bool operator>(position a) { return x > a.x && y > a.y; } bool operator<=(position a) { return x <= a.x && y <= a.y; } bool operator>=(position a) { return x >= a.x && y >= a.y; } position operator+(position a) { return position(x + a.x, y + a.y); } position operator-(position a) { return position(x - a.x, y - a.y); } position operator*(position a) { return position(x * a.x, y * a.y); } position operator/(position a) { return position(x / a.x, y / a.y); } position operator%(position a) { return position(x % a.x, y % a.y); } position complex(position a) { return position(x * a.x - y * a.y, x * a.y + y * a.x); } /* // for sort: bool operator<(position a) { return x ^ a.x ? x < a.x : y < a.y; } bool operator>(position a) { return x ^ a.x ? x > a.x : y > a.y; } bool operator<=(position a) { return x ^ a.x ? x < a.x : y <= a.y; } bool operator>=(position a) { return x ^ a.x ? x > a.x : y >= a.y; } */ }; position Origin = position(0, 0); using pos = position; using vec = position; template <class T> struct Array { struct node { ll childl, childr; T data; node(ll l, ll r, T t) : childl(l), childr(r), data(t) {} }; ll n, depth; vector<ll> versions; vector<ll> prev_versions; vector<node> nodes; Array(ll n = 1 << 20, T val = T()) : n(n), depth(0) { while (n /= 2) depth++; init(val); } void init(T val) { versions.push_back(0); prev_versions.push_back(0); rep(i, 2 * n - 1) { if (i < n - 1) { nodes.push_back(node(2 * i + 1, 2 * i + 2, T())); } else { nodes.push_back(node(0, 0, val)); } } } void set(ll index, ll val, ll version = -1) { ll id, par = nodes.size(), left = 0, right = n; if (version == -1) { id = versions.back(); version = versions.size() - 1; } else { id = versions[version]; } versions.push_back(par); prev_versions.push_back(version); if (right == -1) right = n; rep(i, depth) { ll mid = (left + right) / 2; if (index < mid) { nodes.push_back(node(par + i + 1, nodes[id].childr, T())); id = nodes[id].childl; right = mid; } else { nodes.push_back(node(nodes[id].childl, par + i + 1, T())); id = nodes[id].childr; left = mid; } } nodes.push_back(node(0, 0, val)); } T get(ll index, ll version = -1) { ll id, left = 0, right = n; if (version == -1) { id = versions.back(); } else { id = versions[version]; } rep(i, depth) { ll mid = (left + right) / 2; if (index < mid) { id = nodes[id].childl; right = mid; } else { id = nodes[id].childr; left = mid; } } return nodes[id].data; } ll latest() { return versions.size() - 1; } }; struct BipartiteGraph { ll V; vector<vector<ll>> G; vector<ll> match; vector<bool> used; BipartiteGraph(ll N) : V(N), G(N), match(N), used(N) {} void addEdge(ll i, ll j) { G[i].push_back(j); G[j].push_back(i); } bool dfs(ll now) { used[now] = true; rep(i, G[now].size()) { ll next = G[now][i], w = match[next]; if (w == -1 || (!used[w] && dfs(w))) { match[now] = next, match[next] = now; return true; } } return false; } ll matching() { ll res = 0; fill(all(match), -1); rep(i, V) { if (match[i] == -1) { fill(all(used), false); if (dfs(i)) res++; } } return res; } }; template <typename T = ll> struct Dijkstra { ll V; using P = pair<ll, ll>; vector<vector<P>> G; vector<T> dist; vector<bool> used; Dijkstra(ll v) : V(v), G(v), dist(v), used(v) {} void setDist(ll a, ll b, ll d) { G[a].push_back(P(d, b)); } void culc(ll s = 0) { priority_queue<P, vector<P>, greater<P>> Q; Q.push(P(0, s)); fill_n(dist.begin(), V, INF); fill_n(used.begin(), V, false); while (!Q.empty()) { T d; ll t; tie(d, t) = Q.top(), Q.pop(); if (used[t]) continue; used[t] = true, dist[t] = d; for (P e : G[t]) { if (dist[e.second] <= d + e.first) continue; Q.push(P(d + e.first, e.second)); } } } }; ll N, A[21][21], DP[22][1 << 21]; signed main() { cin >> N; rep(i, N) rep(j, N) cin >> A[i][j]; rep(i, N + 1) rep(j, 1 << N) DP[i][j] = 0; DP[0][0] = 1; rep(i, N) { rep(j, 1 << N) { rep(k, N) { if (!(j & (1 << k)) && A[i][k]) { DP[i + 1][j | (1 << k)] += DP[i][j]; DP[i + 1][j | (1 << k)] %= MOD; } } } } cout << DP[N][(1 << N) - 1] << endl; }
#include <bits/stdc++.h> using namespace std; // Define using ll = long long; using ull = unsigned long long; using ld = long double; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, 1, 0, -1}; const ll MOD = 1e9 + 7; const ll mod = 998244353; const ll inf = 1 << 30; const ll LINF = LONG_MAX; const ll INF = 1LL << 60; const ull MAX = ULONG_MAX; #define mp make_pair #define pb push_back #define elif else if #define endl '\n' #define space ' ' #define def inline auto #define func inline constexpr ll #define run(a) __attribute__((constructor)) def _##a() #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) #define input(a) scanf("%lld", &(a)) #define print(a) printf("%lld\n", (a)) #define fi first #define se second #define ok(a, b) (0 <= (a) && (a) < (b)) template <class T> using vvector = vector<vector<T>>; template <class T> using pvector = vector<pair<T, T>>; template <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>; 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; } // Debug #define debug(...) \ { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ for (auto &&X : {__VA_ARGS__}) \ cerr << "[" << X << "] "; \ cerr << endl; \ } #define dump(a, h, w) \ { \ cerr << __LINE__ << ": " << #a << " = [" << endl; \ rep(__i, h) { \ rep(__j, w) { cerr << a[__i][__j] << space; } \ cerr << endl; \ } \ cerr << "]" << endl; \ } #define vdump(a, n) \ { \ cerr << __LINE__ << ": " << #a << " = ["; \ rep(__i, n) if (__i) cerr << space << a[__i]; \ else cerr << a[__i]; \ cerr << "]" << endl; \ } // Loop #define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i) #define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i) #define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i) #define each(i, a) for (auto &&i : a) #define loop() for (;;) // Stream #define fout(n) cout << fixed << setprecision(n) #define fasten cin.tie(0), ios::sync_with_stdio(0) run(0) { fasten, fout(10); } // Speed #pragma GCC optimize("O3") #pragma GCC target("avx") // Math func gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } func lcm(ll a, ll b) { return a / gcd(a, b) * b; } func sign(ll a) { return a ? abs(a) / a : 0; } inline constexpr ll modulo(const ll n, const ll m = MOD) { ll k = n % m; return k + m * (k < 0); } inline constexpr ll chmod(ll &n, const ll m = MOD) { n %= m; return n += m * (n < 0); } inline constexpr ll mpow(ll a, ll n, const ll m = MOD) { ll r = 1; while (n) { if (n & 1) r *= a; chmod(r, m); a *= a; chmod(a, m); n >>= 1; } return r; } inline ll inv(const ll n, const ll m = MOD) { ll a = n, b = m, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return modulo(x, m); } struct Factor { inline vector<ll> factors(ll N) { vector<ll> A; ll i = 2; while (i * i <= N) { if (N % i == 0) { A.push_back(i); N /= i; } else { i++; } } if (N != 1) A.push_back(N); sort(all(A)); return A; } inline vector<ll> divisor(ll N) { vector<ll> A; inc(i, 1, sqrt(N)) { if (N % i == 0) { A.push_back(i); if (i * i != N) A.push_back(N / i); } } sort(all(A)); return A; } }; struct csum { ll N; vector<ll> A, S; csum(vector<ll> v) : A(v), S(1) { each(k, v) S.push_back(k + S.back()); } ll sum(ll l, ll r) { return S[r] - S[l]; } ll lsum(ll r) { return S[r]; } ll rsum(ll l) { return S.back() - S[l]; } }; template <ull mod = MOD> struct mi { inline constexpr ll modulo(const ll n, const ll m) const noexcept { ll k = n % m; return k + m * (k < 0); } ll num; inline constexpr mi() noexcept : num() { num = 0; } inline constexpr mi(const int n) noexcept : num() { num = modulo(n, mod); } inline constexpr mi(const ll n) noexcept : num() { num = modulo(n, mod); } inline constexpr mi<mod> inv() const noexcept { ll a = num, b = mod, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return mi<mod>(x); } inline constexpr mi<mod> inv(ll n) const noexcept { ll a = n, b = mod, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return mi<mod>(x); } inline constexpr mi<mod> inv(const mi<mod> m) const noexcept { return inv(m.num); } inline constexpr mi<mod> operator+() const noexcept { return mi(num); } inline constexpr mi<mod> operator+(const int n) const noexcept { return mi<mod>(num + n); } inline constexpr mi<mod> operator+(const ll n) const noexcept { return mi<mod>(num + n); } inline constexpr mi<mod> operator+(const mi<mod> m) const noexcept { return mi<mod>(num + m.num); } inline constexpr mi<mod> operator-() const noexcept { return -num; } inline constexpr mi<mod> operator-(const int n) const noexcept { return mi<mod>(num - n); } inline constexpr mi<mod> operator-(const ll n) const noexcept { return mi<mod>(num - n); } inline constexpr mi<mod> operator-(const mi<mod> m) const noexcept { return mi<mod>(num - m.num); } inline constexpr mi<mod> operator*(const int n) const noexcept { return mi<mod>(num * n); } inline constexpr mi<mod> operator*(const ll n) const noexcept { return mi<mod>(num * n); } inline constexpr mi<mod> operator*(const mi<mod> m) const noexcept { return mi<mod>(num * m); } inline constexpr mi<mod> operator/(const int n) const noexcept { return mi<mod>(num * (ll)inv(n)); } inline constexpr mi<mod> operator/(const ll n) const noexcept { return mi<mod>(num * (ll)inv(n)); } inline constexpr mi<mod> operator/(const mi<mod> m) const noexcept { return mi<mod>(num * (ll)inv(m)); } inline constexpr mi<mod> &operator=(const int n) noexcept { num = modulo(n, mod); return *this; } inline constexpr mi<mod> &operator=(const ll n) noexcept { num = modulo(n, mod); return *this; } inline constexpr mi<mod> &operator=(const mi<mod> m) noexcept { num = m.num; return *this; } inline constexpr mi<mod> &operator+=(const int n) noexcept { num = modulo(num + n, mod); return *this; } inline constexpr mi<mod> &operator+=(const ll n) noexcept { num = modulo(num + n, mod); return *this; } inline constexpr mi<mod> &operator+=(const mi<mod> m) noexcept { num = modulo(num + m.num, mod); return *this; } inline constexpr mi<mod> &operator++() noexcept { num = modulo(num + 1, mod); return *this; } inline constexpr mi<mod> operator++(int) noexcept { mi &pre = *this; num = modulo(num + 1, mod); return pre; } inline constexpr mi<mod> &operator-=(const int n) noexcept { num = modulo(num - n, mod); return *this; } inline constexpr mi<mod> &operator-=(const ll n) noexcept { num = modulo(num - n, mod); return *this; } inline constexpr mi<mod> &operator-=(const mi<mod> m) noexcept { num = modulo(num - m.num, mod); return *this; } inline constexpr mi<mod> &operator--() noexcept { num = modulo(num - 1, mod); return *this; } inline constexpr mi<mod> operator--(int) noexcept { mi &pre = *this; num = modulo(num - 1, mod); return pre; } inline constexpr mi<mod> &operator*=(const int n) noexcept { num = modulo(num * n, mod); return *this; } inline constexpr mi<mod> &operator*=(const ll n) noexcept { num = modulo(num * n, mod); return *this; } inline constexpr mi<mod> &operator*=(const mi<mod> m) noexcept { num = modulo(num * m.num, mod); return *this; } inline constexpr mi<mod> &operator/=(const int n) noexcept { num = modulo(num * (ll)inv(n), mod); return *this; } inline constexpr mi<mod> &operator/=(const ll n) noexcept { num = modulo(num * (ll)inv(n), mod); return *this; } inline constexpr mi<mod> &operator/=(const mi<mod> m) noexcept { num = modulo(num * (ll)inv(m), mod); return *this; } inline constexpr mi<mod> operator==(const int n) const noexcept { return num == modulo(n, mod); } inline constexpr mi<mod> operator==(const ll n) const noexcept { return num == modulo(n, mod); } inline constexpr mi<mod> operator==(const mi<mod> m) const noexcept { return num == m.num; } inline constexpr mi<mod> operator!=(const int n) const noexcept { return num != modulo(n, mod); } inline constexpr mi<mod> operator!=(const ll n) const noexcept { return num != modulo(n, mod); } inline constexpr mi<mod> operator!=(const mi<mod> m) const noexcept { return num != m.num; } constexpr operator int() const noexcept { return num; } constexpr operator ll() const noexcept { return num; } friend std::istream &operator>>(std::istream &, const mi<> &); friend std::ostream &operator<<(std::ostream &, const mi<> &); }; template <ull mod = MOD> inline constexpr mi<mod> operator+(const int n, const mi<mod> m) noexcept { return mi<mod>(n + m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator+(const ll n, const mi<mod> m) noexcept { return mi<mod>(n + m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator-(const int n, const mi<mod> m) noexcept { return mi<mod>(n - m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator-(const ll n, const mi<mod> m) noexcept { return mi<mod>(n - m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator*(const int n, const mi<mod> m) noexcept { return mi<mod>(n * m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator*(const ll n, const mi<mod> m) noexcept { return mi<mod>(n * m.num); } template <ull mod = MOD> inline constexpr mi<mod> operator/(const int n, const mi<mod> m) noexcept { return mi<mod>(n * (ll)m.inv()); } template <ull mod = MOD> inline constexpr mi<mod> operator/(const ll n, const mi<mod> m) noexcept { return mi<mod>(n * (ll)m.inv()); } inline constexpr mi<MOD> operator""_m(ull n) { return mi<MOD>((ll)n); } template <ull mod = MOD> inline constexpr mi<mod> pow(mi<mod> m, ll n) noexcept { mi<mod> r = mi<mod>(1); while (n) { if (n & 1) r *= m; m *= m; n >>= 1; } return r; } template <ull mod> istream &operator>>(istream &is, mi<mod> &m) { is >> m.num; return is; } template <ull mod> ostream &operator<<(ostream &is, mi<mod> &m) { is << (ll)m; return is; } template <ull mod = MOD> struct modmath { ll max; vector<mi<mod>> fac, inv; modmath() : max(1 << 20), fac(max + 1), inv(max + 1) { fac[0] = mi<mod>(1); rep(i, max) fac[i + 1] = fac[i] * (i + 1); inv[max] = fac[max].inv(); dec(i, max - 1, 0) inv[i] = inv[i + 1] * (i + 1); } modmath(ll n) : max(n), fac(n + 1), inv(n + 1) { fac[0] = 1; rep(i, n) fac[i + 1] = fac[i] * (i + 1); inv[n] = 1 / fac[n]; dec(i, n - 1, 0) inv[i] = inv[i + 1] * (i + 1); } inline mi<mod> fact(ll n) { if (n < 0) return mi<mod>(0); return fac[n]; } inline mi<mod> perm(ll n, ll r) { if (r < 0 || n < r) return mi<mod>(0); return fac[n] * inv[n - r]; } inline mi<mod> comb(ll n, ll r) { if (r < 0 || n < r) return mi<mod>(0); return fac[n] * inv[r] * inv[n - r]; } inline mi<mod> nHr(ll n, ll r) { return comb(n + r - 1, n - 1); } }; struct UFR { vector<ll> data; UFR(ll N) : data(N) { rep(i, N) data[i] = -1; } def root(ll x) { if (data[x] < 0) return x; else return data[x] = root(data[x]); } def unite(ll x, ll y) { ll root_x = root(x), root_y = root(y); if (root_x != root_y) { if (data[root_x] > data[root_y]) swap(root_x, root_y); data[root_x] -= data[root_x] == data[root_y]; data[root_y] = root_x; return true; } return false; } def same(ll x, ll y) { return root(x) == root(y); } }; struct position { ll x, y; position() {} position(ll a, ll b) : x(a), y(b) {} position next(ll i) { return {x + dx[i], y + dy[i]}; } ll mdist() { return abs(x) + abs(y); } double dist() { return sqrt(x * x + y * y); } double norm(ll d) { if (d == inf) return max(x, y); if (d == 1) return mdist(); if (d == 2) return dist(); return 0; } ll num(ll width) { return abs(x) * width + abs(y); } bool operator==(position a) { return x == a.x && y == a.y; } bool operator!=(position a) { return x != a.x || y != a.y; } bool operator<(position a) { return x < a.x && y < a.y; } bool operator>(position a) { return x > a.x && y > a.y; } bool operator<=(position a) { return x <= a.x && y <= a.y; } bool operator>=(position a) { return x >= a.x && y >= a.y; } position operator+(position a) { return position(x + a.x, y + a.y); } position operator-(position a) { return position(x - a.x, y - a.y); } position operator*(position a) { return position(x * a.x, y * a.y); } position operator/(position a) { return position(x / a.x, y / a.y); } position operator%(position a) { return position(x % a.x, y % a.y); } position complex(position a) { return position(x * a.x - y * a.y, x * a.y + y * a.x); } /* // for sort: bool operator<(position a) { return x ^ a.x ? x < a.x : y < a.y; } bool operator>(position a) { return x ^ a.x ? x > a.x : y > a.y; } bool operator<=(position a) { return x ^ a.x ? x < a.x : y <= a.y; } bool operator>=(position a) { return x ^ a.x ? x > a.x : y >= a.y; } */ }; position Origin = position(0, 0); using pos = position; using vec = position; template <class T> struct Array { struct node { ll childl, childr; T data; node(ll l, ll r, T t) : childl(l), childr(r), data(t) {} }; ll n, depth; vector<ll> versions; vector<ll> prev_versions; vector<node> nodes; Array(ll n = 1 << 20, T val = T()) : n(n), depth(0) { while (n /= 2) depth++; init(val); } void init(T val) { versions.push_back(0); prev_versions.push_back(0); rep(i, 2 * n - 1) { if (i < n - 1) { nodes.push_back(node(2 * i + 1, 2 * i + 2, T())); } else { nodes.push_back(node(0, 0, val)); } } } void set(ll index, ll val, ll version = -1) { ll id, par = nodes.size(), left = 0, right = n; if (version == -1) { id = versions.back(); version = versions.size() - 1; } else { id = versions[version]; } versions.push_back(par); prev_versions.push_back(version); if (right == -1) right = n; rep(i, depth) { ll mid = (left + right) / 2; if (index < mid) { nodes.push_back(node(par + i + 1, nodes[id].childr, T())); id = nodes[id].childl; right = mid; } else { nodes.push_back(node(nodes[id].childl, par + i + 1, T())); id = nodes[id].childr; left = mid; } } nodes.push_back(node(0, 0, val)); } T get(ll index, ll version = -1) { ll id, left = 0, right = n; if (version == -1) { id = versions.back(); } else { id = versions[version]; } rep(i, depth) { ll mid = (left + right) / 2; if (index < mid) { id = nodes[id].childl; right = mid; } else { id = nodes[id].childr; left = mid; } } return nodes[id].data; } ll latest() { return versions.size() - 1; } }; struct BipartiteGraph { ll V; vector<vector<ll>> G; vector<ll> match; vector<bool> used; BipartiteGraph(ll N) : V(N), G(N), match(N), used(N) {} void addEdge(ll i, ll j) { G[i].push_back(j); G[j].push_back(i); } bool dfs(ll now) { used[now] = true; rep(i, G[now].size()) { ll next = G[now][i], w = match[next]; if (w == -1 || (!used[w] && dfs(w))) { match[now] = next, match[next] = now; return true; } } return false; } ll matching() { ll res = 0; fill(all(match), -1); rep(i, V) { if (match[i] == -1) { fill(all(used), false); if (dfs(i)) res++; } } return res; } }; template <typename T = ll> struct Dijkstra { ll V; using P = pair<ll, ll>; vector<vector<P>> G; vector<T> dist; vector<bool> used; Dijkstra(ll v) : V(v), G(v), dist(v), used(v) {} void setDist(ll a, ll b, ll d) { G[a].push_back(P(d, b)); } void culc(ll s = 0) { priority_queue<P, vector<P>, greater<P>> Q; Q.push(P(0, s)); fill_n(dist.begin(), V, INF); fill_n(used.begin(), V, false); while (!Q.empty()) { T d; ll t; tie(d, t) = Q.top(), Q.pop(); if (used[t]) continue; used[t] = true, dist[t] = d; for (P e : G[t]) { if (dist[e.second] <= d + e.first) continue; Q.push(P(d + e.first, e.second)); } } } }; ll N, A[21][21], DP[22][1 << 21]; signed main() { cin >> N; rep(i, N) rep(j, N) cin >> A[i][j]; rep(i, N + 1) rep(j, 1 << N) DP[i][j] = 0; DP[0][0] = 1; rep(i, N) { rep(j, 1 << N) { if (__builtin_popcount(unsigned(j)) != i) continue; rep(k, N) { if (!(j & (1 << k)) && A[i][k]) { DP[i + 1][j | (1 << k)] += DP[i][j]; DP[i + 1][j | (1 << k)] %= MOD; } } } } cout << DP[N][(1 << N) - 1] << endl; }
insert
651
651
651
653
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define printa(x, m, n) \ for (ll i = (m); i <= n; i++) { \ cout << (x[i]) << " "; \ } \ cout << endl; ll dp[22][(1 << 21)]; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; bool a[n][n]; rep(i, 0, n) { rep(j, 0, n) { cin >> a[i][j]; } } memset(dp, 0, sizeof(dp)); dp[0][(1 << n) - 1] = 1; rep(i, 0, n) { rep(bit, 0, (1 << n)) { rep(j, 0, n) { if (bit & (1 << j) && a[i][j]) { dp[i + 1][bit - (1 << j)] += dp[i][bit]; dp[i + 1][bit - (1 << j)] %= MOD; } } } } print(dp[n][0]) return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define printa(x, m, n) \ for (ll i = (m); i <= n; i++) { \ cout << (x[i]) << " "; \ } \ cout << endl; ll dp[22][(1 << 21)]; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; bool a[n][n]; rep(i, 0, n) { rep(j, 0, n) { cin >> a[i][j]; } } memset(dp, 0, sizeof(dp)); dp[0][(1 << n) - 1] = 1; rep(i, 0, n) { rep(bit, 0, (1 << n)) { if (__builtin_popcount(bit) == n - i) { rep(j, 0, n) { if (bit & (1 << j) && a[i][j]) { dp[i + 1][bit - (1 << j)] += dp[i][bit]; dp[i + 1][bit - (1 << j)] %= MOD; } } } } } print(dp[n][0]) return 0; }
replace
33
37
33
39
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ff first #define ss second #define pb push_back using namespace std; const int N = 1e6 + 69, INF = 1e9, mod = 1e9 + 7; const ll INFLL = 1e18; int n, K; ll dp[405][405], A[30][30], sum[405][405]; 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 < n; i++) { if (A[0][i] == 1) { dp[0][(1 << i)] = 1; } } for (int i = 0; i < n; i++) { for (int mask = 0; mask < (1 << n); mask++) { if (!dp[i][mask]) continue; for (int j = 0; j < n; j++) { if (mask & (1 << j) || !A[i + 1][j]) continue; dp[i + 1][mask ^ (1 << j)] += dp[i][mask]; dp[i + 1][mask ^ (1 << j)] %= mod; } } } cout << dp[n - 1][(1 << n) - 1] << endl; }
#include <bits/stdc++.h> #define ll long long #define ff first #define ss second #define pb push_back using namespace std; const int N = 1e6 + 69, INF = 1e9, mod = 1e9 + 7; const ll INFLL = 1e18; int n, K; ll dp[22][(1 << 22)], A[30][30], sum[405][405]; 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 < n; i++) { if (A[0][i] == 1) { dp[0][(1 << i)] = 1; } } for (int i = 0; i < n; i++) { for (int mask = 0; mask < (1 << n); mask++) { if (!dp[i][mask]) continue; for (int j = 0; j < n; j++) { if (mask & (1 << j) || !A[i + 1][j]) continue; dp[i + 1][mask ^ (1 << j)] += dp[i][mask]; dp[i + 1][mask ^ (1 << j)] %= mod; } } } cout << dp[n - 1][(1 << n) - 1] << endl; }
replace
14
15
14
15
0
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <typeinfo> #include <unordered_map> #include <utility> #include <vector> using namespace std; using ll = long long; using vll = vector<ll>; using pll = pair<ll, ll>; using qll = queue<ll>; using vb = vector<bool>; using mll = map<ll, ll>; using sll = stack<ll>; #define REP(i, n) for (ll i(0); (i) < (n); (i)++) #define rep(i, n) for (ll i(0); (i) < (n); (i)++) #define ALL(a) a.begin(), a.end() #define enld endl //* missspell check const ll INF = 1LL << 60; const long long MOD = 1000000007; ll cntOne(ll msk) { ll res = 0; REP(i, 64) { res += (msk >> i) & 1; } return res; } void solve(long long N, std::vector<std::vector<long long>> a) { vector<vll> dp(N + 1, vll(1LL << N, 0)); dp[0][0] = 1; REP(i, N) { REP(msk, 1LL << N) { if (cntOne(msk) == i + 1) { REP(j, N) { if (a[i][j] && ((msk >> j) & 1)) { dp[i + 1][msk] += dp[i][msk & (~(1LL << j))]; } dp[i + 1][msk] %= MOD; } } } } cout << dp[N][(1LL << N) - 1] << endl; return; } int main() { long long N; scanf("%lld", &N); std::vector<std::vector<long long>> a(N, std::vector<long long>(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%lld", &a[i][j]); } } solve(N, std::move(a)); return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <typeinfo> #include <unordered_map> #include <utility> #include <vector> using namespace std; using ll = long long; using vll = vector<ll>; using pll = pair<ll, ll>; using qll = queue<ll>; using vb = vector<bool>; using mll = map<ll, ll>; using sll = stack<ll>; #define REP(i, n) for (ll i(0); (i) < (n); (i)++) #define rep(i, n) for (ll i(0); (i) < (n); (i)++) #define ALL(a) a.begin(), a.end() #define enld endl //* missspell check const ll INF = 1LL << 60; const long long MOD = 1000000007; ll cntOne(ll msk) { ll res = 0; REP(i, 64) { res += (msk >> i) & 1; } return res; } void solve(long long N, std::vector<std::vector<long long>> a) { vector<vll> dp(N + 1, vll(1LL << N, 0)); dp[0][0] = 1; REP(i, N) { REP(msk, 1LL << N) { if (__builtin_popcount(msk) == i + 1) { REP(j, N) { if (a[i][j] && ((msk >> j) & 1)) { dp[i + 1][msk] += dp[i][msk & (~(1LL << j))]; } dp[i + 1][msk] %= MOD; } } } } cout << dp[N][(1LL << N) - 1] << endl; return; } int main() { long long N; scanf("%lld", &N); std::vector<std::vector<long long>> a(N, std::vector<long long>(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%lld", &a[i][j]); } } solve(N, std::move(a)); return 0; }
replace
54
55
54
55
TLE
p03174
C++
Time Limit Exceeded
// ############################################################################# #include "bits/stdc++.h" // ############################################################################# // Macros #define MOD 1000000007 #define INF 1000000000000009 #define eb emplace_back #define mp make_pair #define fr first #define sc second #define vec vector #define len(x) x.size() #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define all(v) v.begin(), v.end() #define alla(a, n) a, a + n #define sum(v) accumulate(all(v), 0LL) // Type Defs typedef long long ll; typedef unsigned long long ull; // ############################################################################# using namespace std; // ############################################################################# // Helpers void io_handler() { #ifdef LOCAL_TESTING freopen("inputf.in", "r", stdin); freopen("outputf.in", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } template <typename T> void print_matrix(vector<vector<T>> mat) { for (auto row : mat) { for (auto e : row) { // auto fst = e[0]==INF?"INF":to_string(e[0]); // auto scd = e[1]==INF?"INF":to_string(e[1]); // cout <<"["<<fst<<","<<scd<< "]"<<" "; cout << e << " "; } cout << endl; } } template <typename T> void print_vector(vector<T> v) { for (auto e : v) { cout << e << " "; } cout << endl; } template <typename T> map<T, T> get_count(vector<T> v) { map<T, T> count; for (auto e : v) { if (count.find(e) != count.end()) { count[e]++; } else { count[e] = 1; } } return count; } void add_self(ll &a, ll b) { a = a + b; if (a >= MOD) { a -= MOD; } } ll sub_mod(ll a, ll b) { a = (a - b + MOD) % MOD; return a; } // ############################################################################# // Solution void solve() { ll n; cin >> n; vec<vec<ll>> match(n, vec<ll>(n, 0LL)); for (ll i = 0LL; i < n; i++) { for (ll j = 0LL; j < n; j++) { cin >> match[i][j]; } } ll mxN = (1LL << n); vec<vec<ll>> dp(n + 1, vec<ll>(mxN, 0LL)); dp[0][0] = 1; for (ll i = 1LL; i <= n; i++) { for (ll mask = 0LL; mask < mxN; mask++) { for (ll j = 0LL; j < n; j++) { if (match[i - 1][j]) { ll sub_mask = 1 << j; if (!(mask & sub_mask)) { add_self(dp[i][mask | sub_mask], dp[i - 1][mask]); } } } } } // print_matrix(dp); cout << dp[n][mxN - 1] << endl; } // ############################################################################# // Driver int main() { io_handler(); int t = 1; // cin>>t; for (int i = 0; i < t; i++) { solve(); } #ifdef LOCAL_TESTING cout << "⏰ Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; } // #############################################################################
// ############################################################################# #include "bits/stdc++.h" // ############################################################################# // Macros #define MOD 1000000007 #define INF 1000000000000009 #define eb emplace_back #define mp make_pair #define fr first #define sc second #define vec vector #define len(x) x.size() #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define all(v) v.begin(), v.end() #define alla(a, n) a, a + n #define sum(v) accumulate(all(v), 0LL) // Type Defs typedef long long ll; typedef unsigned long long ull; // ############################################################################# using namespace std; // ############################################################################# // Helpers void io_handler() { #ifdef LOCAL_TESTING freopen("inputf.in", "r", stdin); freopen("outputf.in", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } template <typename T> void print_matrix(vector<vector<T>> mat) { for (auto row : mat) { for (auto e : row) { // auto fst = e[0]==INF?"INF":to_string(e[0]); // auto scd = e[1]==INF?"INF":to_string(e[1]); // cout <<"["<<fst<<","<<scd<< "]"<<" "; cout << e << " "; } cout << endl; } } template <typename T> void print_vector(vector<T> v) { for (auto e : v) { cout << e << " "; } cout << endl; } template <typename T> map<T, T> get_count(vector<T> v) { map<T, T> count; for (auto e : v) { if (count.find(e) != count.end()) { count[e]++; } else { count[e] = 1; } } return count; } void add_self(ll &a, ll b) { a = a + b; if (a >= MOD) { a -= MOD; } } ll sub_mod(ll a, ll b) { a = (a - b + MOD) % MOD; return a; } // ############################################################################# // Solution void solve() { ll n; cin >> n; vec<vec<ll>> match(n, vec<ll>(n, 0LL)); for (ll i = 0LL; i < n; i++) { for (ll j = 0LL; j < n; j++) { cin >> match[i][j]; } } ll mxN = (1LL << n); vec<vec<ll>> dp(n + 1, vec<ll>(mxN, 0LL)); dp[0][0] = 1; for (ll i = 1LL; i <= n; i++) { for (ll mask = 0LL; mask < mxN; mask++) { if (!dp[i - 1][mask]) { continue; } for (ll j = 0LL; j < n; j++) { if (match[i - 1][j]) { ll sub_mask = 1 << j; if (!(mask & sub_mask)) { add_self(dp[i][mask | sub_mask], dp[i - 1][mask]); } } } } } // print_matrix(dp); cout << dp[n][mxN - 1] << endl; } // ############################################################################# // Driver int main() { io_handler(); int t = 1; // cin>>t; for (int i = 0; i < t; i++) { solve(); } #ifdef LOCAL_TESTING cout << "⏰ Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; } // #############################################################################
insert
87
87
87
90
TLE
p03174
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; constexpr ld EPS = 1e-12; constexpr int INF = numeric_limits<int>::max() / 2; constexpr int MOD = 1e9 + 7; template <typename T> void printv(const vector<T> &v) { int sz = v.size(); for (int i = 0; i < sz; i++) { cout << v[i] << " \n"[i == sz - 1]; } } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); vector<vector<ll>> dp(N, vector<ll>(1 << N, 0)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; if (i == 0 && a[i][j]) { dp[i][1 << j] = 1; } } } for (int i = 1; i < N; i++) { for (int j = 0; j < (1 << N); j++) { for (int k = 0; k < N; k++) { if (!a[i][k]) continue; if (j & (1 << k)) { dp[i][j] += dp[i - 1][j ^ (1 << k)]; dp[i][j] %= MOD; } } } } cout << dp[N - 1][(1 << N) - 1] << endl; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; constexpr ld EPS = 1e-12; constexpr int INF = numeric_limits<int>::max() / 2; constexpr int MOD = 1e9 + 7; template <typename T> void printv(const vector<T> &v) { int sz = v.size(); for (int i = 0; i < sz; i++) { cout << v[i] << " \n"[i == sz - 1]; } } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<vector<int>> a(N, vector<int>(N)); vector<vector<ll>> dp(N, vector<ll>(1 << N, 0)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; if (i == 0 && a[i][j]) { dp[i][1 << j] = 1; } } } for (int i = 1; i < N; i++) { for (int j = 0; j < N; j++) { if (!a[i][j]) continue; for (int k = 0; k < (1 << N); k++) { if (k & (1 << j)) { dp[i][k] += dp[i - 1][k ^ (1 << j)]; dp[i][k] %= MOD; } } } } cout << dp[N - 1][(1 << N) - 1] << endl; }
replace
35
42
35
42
TLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) const int mod = 1000000007; int dp[3000000][22]; bool field[22][22]; vector<int> in[3000000]; int n; int main() { // cout.precision(10); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int in; cin >> in; if (in == 1) field[i][j] = true; else field[i][j] = false; } } for (ll bits = 0; bits < ((ll)1 << n); ++bits) { for (int woman = 1; woman <= n; woman++) { if (!(bits & ((ll)1 << (woman - 1)))) in[bits].push_back(woman); // cout << bits << " " << woman << endl; } } dp[0][0]++; for (int man = 1; man <= n; man++) { for (ll bits = 0; bits < ((ll)1 << n); ++bits) { dp[bits][man] = (dp[bits][man] + dp[bits][man - 1]) % mod; // dp[bits][man] %= mod; // cout << man << " " << bits << " " << dp[bits][man - 1] << endl; for (int i = 0; i < in[bits].size(); i++) { // cout << i << endl; // for(ll woman = 1; woman <= n; woman++){ int woman = in[bits][i]; // cout << man << " " << woman << " " << bits << endl; // if(bits & ((ll)1<<(woman - 1))) continue; if (!field[man][woman]) continue; // cout << man << " " << woman << " " << bits << endl; // dp[bits | ((ll)1<<(woman - 1))][man] += dp[bits][man - 1]; dp[bits | ((ll)1 << (woman - 1))][man] = (dp[bits][man - 1] + dp[bits | ((ll)1 << (woman - 1))][man]) % mod; } } } cout << dp[((ll)1 << n) - 1][n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) const int mod = 1000000007; int dp[3000000][22]; bool field[22][22]; vector<int> in[3000000]; int n; int main() { // cout.precision(10); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int in; cin >> in; if (in == 1) field[i][j] = true; else field[i][j] = false; } } for (ll bits = 0; bits < ((ll)1 << n); ++bits) { for (int woman = 1; woman <= n; woman++) { if (!(bits & ((ll)1 << (woman - 1)))) in[bits].push_back(woman); // cout << bits << " " << woman << endl; } } dp[0][0]++; for (int man = 1; man <= n; man++) { for (ll bits = 0; bits < ((ll)1 << n); ++bits) { if (__builtin_popcount(bits) != (man - 1)) continue; dp[bits][man] = (dp[bits][man] + dp[bits][man - 1]) % mod; // dp[bits][man] %= mod; // cout << man << " " << bits << " " << dp[bits][man - 1] << endl; for (int i = 0; i < in[bits].size(); i++) { // cout << i << endl; // for(ll woman = 1; woman <= n; woman++){ int woman = in[bits][i]; // cout << man << " " << woman << " " << bits << endl; // if(bits & ((ll)1<<(woman - 1))) continue; if (!field[man][woman]) continue; // cout << man << " " << woman << " " << bits << endl; // dp[bits | ((ll)1<<(woman - 1))][man] += dp[bits][man - 1]; dp[bits | ((ll)1 << (woman - 1))][man] = (dp[bits][man - 1] + dp[bits | ((ll)1 << (woman - 1))][man]) % mod; } } } cout << dp[((ll)1 << n) - 1][n] << endl; return 0; }
insert
35
35
35
37
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> typedef int64_t LL; using namespace std; const int N = 25; const int mod = 1e9 + 7; bool A[N][N]; int n; int dp[N][1 << 21]; LL solve(int k, int used) { if (k == n) return 1LL; if (dp[k][used] >= 0) return dp[k][used]; LL res = 0; for (int i = 0; i < n; i++) { if (A[k][i] && !((used >> i) & 1)) { used ^= (1 << i); res += solve(k + 1, used); res %= mod; used ^= (1 << i); } } return dp[k][used] = res; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { scanf("%d", &A[i][j]); } } memset(dp, sizeof(dp), -1); int ans = solve(0, 0); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> typedef int64_t LL; using namespace std; const int N = 25; const int mod = 1e9 + 7; bool A[N][N]; int n; int dp[N][1 << 21]; LL solve(int k, int used) { if (k == n) return 1LL; if (dp[k][used] >= 0) return dp[k][used]; LL res = 0; for (int i = 0; i < n; i++) { if (A[k][i] && !((used >> i) & 1)) { used ^= (1 << i); res += solve(k + 1, used); res %= mod; used ^= (1 << i); } } return dp[k][used] = res; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { scanf("%d", &A[i][j]); } } memset(dp, -1, sizeof(dp)); int ans = solve(0, 0); printf("%d\n", ans); return 0; }
replace
33
34
33
34
-11
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, s, n) for (int i = (int)s; i < (int)n; i++) #define ll long long #define ld long double #define pb push_back #define eb emplace_back #define All(x) x.begin(), x.end() #define Range(x, i, j) x.begin() + i, x.begin() + j #define lbidx(x, y) lower_bound(x.begin(), x.end(), y) - x.begin() #define ubidx(x, y) upper_bound(x.begin(), x.end(), y) - x.begin() #define llbidx(x, y, z) \ lower_bound(x.begin(), x.end(), z) - \ lower_bound(x.begin(), x.end(), y) // 二要素間の距離 #define deg2rad(deg) ((((double)deg) / ((double)360) * 2 * M_PI)) #define rad2deg(rad) ((((double)rad) / (double)2 / M_PI) * (double)360) #define Find(set, element) set.find(element) != set.end() #define bit(n, k) ((n >> k) & 1) // nのk bit目 #define Decimal(x) printf("%.10f\n", x) // 小数点を10桁まで表示 #define endl "\n" // debug用 #define PrintVec(x) \ for (auto elementPrintVec : x) { \ cout << elementPrintVec << " "; \ } \ cout << "\n"; #define debug(x) cerr << #x << ": " << (x) << "\n"; // gcj print用 #define Case(x) printf("Case #%d: ", x); typedef pair<int, int> PI; typedef pair<ll, ll> PLL; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<int>>> vvvl; typedef vector<PI> vpi; typedef vector<vector<PI>> vvpi; typedef vector<vector<vector<PI>>> vvvpi; typedef vector<PLL> vpl; typedef vector<vector<PLL>> vvpl; typedef vector<vector<vector<PLL>>> vvvpl; int POWINT(int x, int n) { int ret = 1; while (n > 0) { if (n & 1) ret *= x; x *= x; n >>= 1; } return ret; }; ll POWLL(ll x, int n) { ll ret = 1; while (n > 0) { if (n & 1) ret *= x; x *= x; n >>= 1; } return ret; }; 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 <std::int_fast64_t Modulus> class modint { using i64 = int_fast64_t; public: i64 a; constexpr modint(const i64 x = 0) noexcept { this->a = x % Modulus; if (a < 0) { a += Modulus; } } // constexpr i64 &value() const noexcept {return a;} constexpr const i64 &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; } 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; } constexpr modint &operator/=(modint rhs) noexcept { i64 a_ = rhs.a, b = Modulus, u = 1, v = 0; while (b) { i64 t = a_ / b; a_ -= t * b; swap(a_, b); u -= t * v; swap(u, v); } a = a * u % Modulus; if (a < 0) a += Modulus; return *this; } // 自前実装 constexpr bool operator==(const modint rhs) noexcept { return a == rhs.a; } constexpr bool operator!=(const modint rhs) noexcept { return a != rhs.a; } constexpr bool operator>(const modint rhs) noexcept { return a > rhs.a; } constexpr bool operator>=(const modint rhs) noexcept { return a >= rhs.a; } constexpr bool operator<(const modint rhs) noexcept { return a < rhs.a; } constexpr bool operator<=(const modint rhs) noexcept { return a <= rhs.a; } // constexpr modint& operator++() noexcept { // return (*this) += modint(1); // } // constexpr modint operator++(int) { // modint tmp(*this); // operator++(); // return tmp; // } // constexpr modint& operator--() noexcept { // return (*this) -= modint(1); // } // constexpr modint operator--(int) { // modint tmp(*this); // operator--(); // return tmp; // } template <typename T> friend constexpr modint modpow(const modint &mt, T n) noexcept { if (n < 0) { modint t = (modint(1) / mt); return modpow(t, -n); } modint res = 1, tmp = mt; while (n) { if (n & 1) res *= tmp; tmp *= tmp; n /= 2; } return res; } }; const ll MOD = 1e9 + 7; using mint = modint<MOD>; // 標準入出力対応 std::ostream &operator<<(std::ostream &out, const modint<MOD> &m) { out << m.a; return out; } std::istream &operator>>(std::istream &in, modint<MOD> &m) { ll a; in >> a; m = mint(a); return in; } int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vvi adj(N, vi(N)); rep(i, 0, N) { rep(j, 0, N) { cin >> adj[i][j]; } } vector<mint> dp(1 << N); dp[0] = mint(1); rep(S, 1, 1 << N) { int i = __builtin_popcount(S); rep(j, 0, N) { // i番目の男性がj番目の女性とマッチしたときの場合の数を足す if (bit(S, j) == 1 && adj[i - 1][j] == 1) { dp[S] += dp[S ^ (1 << j)]; } } } // 配るdp vector<vector<mint>> dp2(N, vector<mint>(1 << N)); dp2[0][0] = mint(1); // 一見するとO(2^N N^2)だけど…… rep(i, 0, N) { rep(S, 0, 1 << N) { if (dp2[i][S] > mint(0)) { //<-ここのif文の枝刈りが効いてO(2^N N)に落ちる rep(j, 0, N) { if (bit(S, j) == 0 && adj[i][j] == 1) { dp2[i + 1][S ^ (1 << j)] += dp2[i][S]; } } } } } // cout << dp[(1 << N) - 1] << endl; cout << dp2[N][(1 << N) - 1] << endl; return 0; };
#include <bits/stdc++.h> using namespace std; #define rep(i, s, n) for (int i = (int)s; i < (int)n; i++) #define ll long long #define ld long double #define pb push_back #define eb emplace_back #define All(x) x.begin(), x.end() #define Range(x, i, j) x.begin() + i, x.begin() + j #define lbidx(x, y) lower_bound(x.begin(), x.end(), y) - x.begin() #define ubidx(x, y) upper_bound(x.begin(), x.end(), y) - x.begin() #define llbidx(x, y, z) \ lower_bound(x.begin(), x.end(), z) - \ lower_bound(x.begin(), x.end(), y) // 二要素間の距離 #define deg2rad(deg) ((((double)deg) / ((double)360) * 2 * M_PI)) #define rad2deg(rad) ((((double)rad) / (double)2 / M_PI) * (double)360) #define Find(set, element) set.find(element) != set.end() #define bit(n, k) ((n >> k) & 1) // nのk bit目 #define Decimal(x) printf("%.10f\n", x) // 小数点を10桁まで表示 #define endl "\n" // debug用 #define PrintVec(x) \ for (auto elementPrintVec : x) { \ cout << elementPrintVec << " "; \ } \ cout << "\n"; #define debug(x) cerr << #x << ": " << (x) << "\n"; // gcj print用 #define Case(x) printf("Case #%d: ", x); typedef pair<int, int> PI; typedef pair<ll, ll> PLL; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<vector<vector<int>>> vvvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<int>>> vvvl; typedef vector<PI> vpi; typedef vector<vector<PI>> vvpi; typedef vector<vector<vector<PI>>> vvvpi; typedef vector<PLL> vpl; typedef vector<vector<PLL>> vvpl; typedef vector<vector<vector<PLL>>> vvvpl; int POWINT(int x, int n) { int ret = 1; while (n > 0) { if (n & 1) ret *= x; x *= x; n >>= 1; } return ret; }; ll POWLL(ll x, int n) { ll ret = 1; while (n > 0) { if (n & 1) ret *= x; x *= x; n >>= 1; } return ret; }; 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 <std::int_fast64_t Modulus> class modint { using i64 = int_fast64_t; public: i64 a; constexpr modint(const i64 x = 0) noexcept { this->a = x % Modulus; if (a < 0) { a += Modulus; } } // constexpr i64 &value() const noexcept {return a;} constexpr const i64 &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; } 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; } constexpr modint &operator/=(modint rhs) noexcept { i64 a_ = rhs.a, b = Modulus, u = 1, v = 0; while (b) { i64 t = a_ / b; a_ -= t * b; swap(a_, b); u -= t * v; swap(u, v); } a = a * u % Modulus; if (a < 0) a += Modulus; return *this; } // 自前実装 constexpr bool operator==(const modint rhs) noexcept { return a == rhs.a; } constexpr bool operator!=(const modint rhs) noexcept { return a != rhs.a; } constexpr bool operator>(const modint rhs) noexcept { return a > rhs.a; } constexpr bool operator>=(const modint rhs) noexcept { return a >= rhs.a; } constexpr bool operator<(const modint rhs) noexcept { return a < rhs.a; } constexpr bool operator<=(const modint rhs) noexcept { return a <= rhs.a; } // constexpr modint& operator++() noexcept { // return (*this) += modint(1); // } // constexpr modint operator++(int) { // modint tmp(*this); // operator++(); // return tmp; // } // constexpr modint& operator--() noexcept { // return (*this) -= modint(1); // } // constexpr modint operator--(int) { // modint tmp(*this); // operator--(); // return tmp; // } template <typename T> friend constexpr modint modpow(const modint &mt, T n) noexcept { if (n < 0) { modint t = (modint(1) / mt); return modpow(t, -n); } modint res = 1, tmp = mt; while (n) { if (n & 1) res *= tmp; tmp *= tmp; n /= 2; } return res; } }; const ll MOD = 1e9 + 7; using mint = modint<MOD>; // 標準入出力対応 std::ostream &operator<<(std::ostream &out, const modint<MOD> &m) { out << m.a; return out; } std::istream &operator>>(std::istream &in, modint<MOD> &m) { ll a; in >> a; m = mint(a); return in; } int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vvi adj(N, vi(N)); rep(i, 0, N) { rep(j, 0, N) { cin >> adj[i][j]; } } vector<mint> dp(1 << N); dp[0] = mint(1); rep(S, 1, 1 << N) { int i = __builtin_popcount(S); rep(j, 0, N) { // i番目の男性がj番目の女性とマッチしたときの場合の数を足す if (bit(S, j) == 1 && adj[i - 1][j] == 1) { dp[S] += dp[S ^ (1 << j)]; } } } // 配るdp vector<vector<mint>> dp2(N + 1, vector<mint>(1 << N)); dp2[0][0] = mint(1); // 一見するとO(2^N N^2)だけど…… rep(i, 0, N) { rep(S, 0, 1 << N) { if (dp2[i][S] > mint(0)) { //<-ここのif文の枝刈りが効いてO(2^N N)に落ちる rep(j, 0, N) { if (bit(S, j) == 0 && adj[i][j] == 1) { dp2[i + 1][S ^ (1 << j)] += dp2[i][S]; } } } } } // cout << dp[(1 << N) - 1] << endl; cout << dp2[N][(1 << N) - 1] << endl; return 0; };
replace
221
222
221
222
-11
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define int long long typedef pair<int, int> ii; const int mod = 1e9 + 7; const int N = 21; int n, a[N][N], dp[N][(1 << N)]; signed main() { cin.tie(0), ios::sync_with_stdio(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 i = 0; i < n; i++) { for (int mask = 0; mask < (1 << n); mask++) { if (!dp[i][mask]) continue; for (int k = 0; k < n; k++) { if (a[i][k] && (mask & (1 << k)) == 0) dp[i + 1][(mask ^ (1 << k))] = (dp[i + 1][(mask ^ (1 << k))] + dp[i][mask]) % mod; } } } cout << dp[n][(1 << n) - 1]; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair #define int long long typedef pair<int, int> ii; const int mod = 1e9 + 7; const int N = 22; int n, a[N][N], dp[N][(1 << N)]; signed main() { cin.tie(0), ios::sync_with_stdio(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 i = 0; i < n; i++) { for (int mask = 0; mask < (1 << n); mask++) { if (!dp[i][mask]) continue; for (int k = 0; k < n; k++) { if (a[i][k] && (mask & (1 << k)) == 0) dp[i + 1][(mask ^ (1 << k))] = (dp[i + 1][(mask ^ (1 << k))] + dp[i][mask]) % mod; } } } cout << dp[n][(1 << n) - 1]; }
replace
15
16
15
16
-11
p03174
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define int long long #define CLK clock_t clk = clock(); // Start of main #define OCLK \ cerr << "Time (in ms): " \ << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC \ << '\n'; // End of main #define MOD 1000000007 /* DEFINE cout */ #define pln cout << "===============================\n" #define on cout << "\n" #define os cout << " " #define o2(a, b) cout << a << " " << b #define o(a) cout << a #define bitcount __builtin_popcount /* DEFINE cout */ #define endl "\n" #define fastio \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); // fastio using namespace std; struct hash_pair { // use pair as key in // unordered_map<pair<int,int>,int,hash_pair>; template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; struct ArrayHasher { std::size_t operator()(const std::array<int, 3> &a) { std::size_t h = 0; for (auto e : a) { h ^= std::hash<int>{}(e) + 0x9e3779b9 + (h << 6) + (h >> 2); } return h; } }; bool prime[10000001]; int P = MOD - 2; int factorial[10000001] = {0}; int powerFunction(int x, int y) { int res = 1; int p = MOD; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } void sieveFunction(int maxLimit) { memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; for (int i = 2; maxLimit >= i; i++) { if (prime[i]) { for (int j = 2 * i; maxLimit >= j; j += i) prime[j] = false; } } } void factorialFunction(int maxLimit) { factorial[0] = 1; for (int i = 1; i <= maxLimit; i++) factorial[i] = (factorial[i - 1] * i) % MOD; return; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int n; int arr[22][22]; int has[22]; int dp[22][2 << 22]; int fun(int idx, int bh) { if (idx == n) { return 1; } if (dp[idx][bh] != -1) { return dp[idx][bh]; } int temp = 0; for (int j = 0; n > j; j++) { if (arr[idx][j] == 1 && has[j] == 1) { has[j] = 0; bh += 2 << (j); temp += fun(idx + 1, bh); temp %= MOD; bh -= 2 << (j); has[j] = 1; } } dp[idx][bh] = temp; return temp; } signed main() { // CLK; memset(dp, -1, sizeof(dp)); cin >> n; for (int i = 0; n > i; i++) { has[i] = 1; for (int j = 0; n > j; j++) { cin >> arr[i][j]; } } cout << fun(0, 0) << endl; // OCLK; }
#include <bits/stdc++.h> #define ll long long #define CLK clock_t clk = clock(); // Start of main #define OCLK \ cerr << "Time (in ms): " \ << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC \ << '\n'; // End of main #define MOD 1000000007 /* DEFINE cout */ #define pln cout << "===============================\n" #define on cout << "\n" #define os cout << " " #define o2(a, b) cout << a << " " << b #define o(a) cout << a #define bitcount __builtin_popcount /* DEFINE cout */ #define endl "\n" #define fastio \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); // fastio using namespace std; struct hash_pair { // use pair as key in // unordered_map<pair<int,int>,int,hash_pair>; template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; struct ArrayHasher { std::size_t operator()(const std::array<int, 3> &a) { std::size_t h = 0; for (auto e : a) { h ^= std::hash<int>{}(e) + 0x9e3779b9 + (h << 6) + (h >> 2); } return h; } }; bool prime[10000001]; int P = MOD - 2; int factorial[10000001] = {0}; int powerFunction(int x, int y) { int res = 1; int p = MOD; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } void sieveFunction(int maxLimit) { memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; for (int i = 2; maxLimit >= i; i++) { if (prime[i]) { for (int j = 2 * i; maxLimit >= j; j += i) prime[j] = false; } } } void factorialFunction(int maxLimit) { factorial[0] = 1; for (int i = 1; i <= maxLimit; i++) factorial[i] = (factorial[i - 1] * i) % MOD; return; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int n; int arr[22][22]; int has[22]; int dp[22][2 << 22]; int fun(int idx, int bh) { if (idx == n) { return 1; } if (dp[idx][bh] != -1) { return dp[idx][bh]; } int temp = 0; for (int j = 0; n > j; j++) { if (arr[idx][j] == 1 && has[j] == 1) { has[j] = 0; bh += 2 << (j); temp += fun(idx + 1, bh); temp %= MOD; bh -= 2 << (j); has[j] = 1; } } dp[idx][bh] = temp; return temp; } signed main() { // CLK; memset(dp, -1, sizeof(dp)); cin >> n; for (int i = 0; n > i; i++) { has[i] = 1; for (int j = 0; n > j; j++) { cin >> arr[i][j]; } } cout << fun(0, 0) << endl; // OCLK; }
replace
1
2
1
2
MLE
p03174
Python
Time Limit Exceeded
import numpy as np N = int(input()) A = [np.flatnonzero(np.array(list(map(int, input().split())))) for _ in range(N)] MOD = 10**9 + 7 dp = np.zeros(1 << N, dtype=np.int64) dp[0] = 1 for a in A: new_dp = np.zeros_like(dp) for i in a: x = 1 << i new_dp[x:] += dp[:-x] dp = new_dp dp %= MOD print(dp[-1])
import numpy as np N = int(input()) A = [np.array(list(map(int, input().split()))).nonzero()[0] for _ in range(N)] MOD = 10**9 + 7 dp = np.zeros(1 << N, dtype=np.int64) dp[0] = 1 for a in A: new_dp = np.zeros_like(dp) for i in a: x = 1 << i new_dp[x:] += dp[:-x] dp = new_dp dp %= MOD print(dp[-1])
replace
3
4
3
4
TLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define MOD2 998244353 #define int long long #define double long double #define EPS 1e-9 // #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) { int N = A.size(); int M; if (N > 0) M = A[0].size(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) os << A[i][j] << " "; os << endl; } return os; } typedef pair<int, int> pii; typedef long long ll; struct edge { int from, to, d, c; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector<edge> edges; typedef vector<edges> graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector<vector<flow>> flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1LL << n; } template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(20); } }; initializer _____; int N, M, K, T, Q, H, W; signed main() { cin >> N; vector<vector<int>> A(N, vector<int>(N)); rep(i, N) rep(j, N) cin >> A[i][j]; vector<int> B(N, 0); rep(i, N) rep(j, N) B[i] += A[i][j]; vector<vector<int>> dp(N + 1, vector<int>(1 << N, 0)); dp[0][0] = 1; rep(i, N) { rep(b, 1 << N) { bitset<21> bit(b); if (bit.count() < i) continue; rep(k, N) { if (!bit[k] && A[k][i]) { int nb = b | (1 << k); (dp[i + 1][nb] += dp[i][b]) %= MOD; } } } } cout << dp[N][(1 << N) - 1] << endl; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define MOD2 998244353 #define int long long #define double long double #define EPS 1e-9 // #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) { int N = A.size(); int M; if (N > 0) M = A[0].size(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) os << A[i][j] << " "; os << endl; } return os; } typedef pair<int, int> pii; typedef long long ll; struct edge { int from, to, d, c; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector<edge> edges; typedef vector<edges> graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector<vector<flow>> flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1LL << n; } template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(20); } }; initializer _____; int N, M, K, T, Q, H, W; signed main() { cin >> N; vector<vector<int>> A(N, vector<int>(N)); rep(i, N) rep(j, N) cin >> A[i][j]; vector<int> B(N, 0); rep(i, N) rep(j, N) B[i] += A[i][j]; vector<vector<int>> dp(N + 1, vector<int>(1 << N, 0)); dp[0][0] = 1; rep(i, N) { rep(b, 1 << N) { bitset<21> bit(b); if (bit.count() != i) continue; rep(k, N) { if (!bit[k] && A[k][i]) { int nb = b | (1 << k); (dp[i + 1][nb] += dp[i][b]) %= MOD; } } } } cout << dp[N][(1 << N) - 1] << endl; return 0; }
replace
117
118
117
118
TLE
p03174
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; // using lint = long long; struct P { int x, y; bool operator<(const P &a) const { if (x != a.x) return x > a.x; return y > a.y; } }; vector<int> v[1], v1; // bitset<4001000> b; int i, n, d, m, k, a, b, c, h; int o[1111][33]; long long l[33][4000511]; int j[1]; int e; // int dx[10]={2,2,-2,-2,1,1,-1,-1},dy[10]={1,-1,1,-1,2,-2,2,-2}; int dx[10] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[10] = {1, 0, -1, 0, 1, -1, 1, -1}, dz[10] = {0, 0, 0, 0, 1, -1}; long long mod = 1000000007, mod2 = 1000000009, mod3 = 2017; long long x, z, y; double pi = 3.14159265; P u[1]; stack<int> s; // set<int> s[222][22]; priority_queue<long long, vector<long long>, greater<long long>> q; // queue<int> q; // 2147483647 //'1'==49; //'A'==65; //'a'==97; // unordered_ // map<int,int> p; /* struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; unordered_map<long long, int, custom_hash> safe_map; */ // list<int> l; // string r,r1; // char r[2]; // deque<int> de; // srand((int)time(NULL)); /*bool as(P a,P b) { if(a.x!=b.x) return a.x<b.x; return a.y<b.y; }*/ bool as(P a, P b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; } long long f(int n, int m) { if (n >= a) return 1; if (l[n][m] >= 0) return l[n][m]; long long x = 0; for (int h = 0, i = 1; h < a; i *= 2, h++) if (o[n][h] && (m & i) == 0) x += f(n + 1, m + i), x %= mod; return l[n][m] = x; } int main() { memset(l, -1, sizeof(l)); scanf("%d", &a); for (int t = 0; t < a; t++) for (int w = 0; w < a; w++) scanf("%d", &o[t][w]); printf("%lld", f(0, 0)); }
#include <bits/stdc++.h> using namespace std; // using lint = long long; struct P { int x, y; bool operator<(const P &a) const { if (x != a.x) return x > a.x; return y > a.y; } }; vector<int> v[1], v1; // bitset<4001000> b; int i, n, d, m, k, a, b, c, h; int o[1111][33]; long long l[23][2500511]; int j[1]; int e; // int dx[10]={2,2,-2,-2,1,1,-1,-1},dy[10]={1,-1,1,-1,2,-2,2,-2}; int dx[10] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[10] = {1, 0, -1, 0, 1, -1, 1, -1}, dz[10] = {0, 0, 0, 0, 1, -1}; long long mod = 1000000007, mod2 = 1000000009, mod3 = 2017; long long x, z, y; double pi = 3.14159265; P u[1]; stack<int> s; // set<int> s[222][22]; priority_queue<long long, vector<long long>, greater<long long>> q; // queue<int> q; // 2147483647 //'1'==49; //'A'==65; //'a'==97; // unordered_ // map<int,int> p; /* struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; unordered_map<long long, int, custom_hash> safe_map; */ // list<int> l; // string r,r1; // char r[2]; // deque<int> de; // srand((int)time(NULL)); /*bool as(P a,P b) { if(a.x!=b.x) return a.x<b.x; return a.y<b.y; }*/ bool as(P a, P b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; } long long f(int n, int m) { if (n >= a) return 1; if (l[n][m] >= 0) return l[n][m]; long long x = 0; for (int h = 0, i = 1; h < a; i *= 2, h++) if (o[n][h] && (m & i) == 0) x += f(n + 1, m + i), x %= mod; return l[n][m] = x; } int main() { memset(l, -1, sizeof(l)); scanf("%d", &a); for (int t = 0; t < a; t++) for (int w = 0; w < a; w++) scanf("%d", &o[t][w]); printf("%lld", f(0, 0)); }
replace
20
21
20
21
MLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif class mint { public: long long x; constexpr mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} constexpr mint operator-() const { return mint(-x); } constexpr mint &operator+=(const mint &rhs) { if ((x += rhs.x) >= MOD) x -= MOD; return *this; } constexpr mint &operator-=(const mint &rhs) { if ((x += MOD - rhs.x) >= MOD) x -= MOD; return *this; } constexpr mint &operator*=(const mint &rhs) { (x *= rhs.x) %= MOD; return *this; } constexpr mint operator+(const mint &rhs) const { mint res(*this); return res += rhs; } constexpr mint operator-(const mint &rhs) const { mint res(*this); return res -= rhs; } constexpr mint operator*(const mint &rhs) const { mint res(*this); return res *= rhs; } [[nodiscard]] constexpr mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } [[nodiscard]] constexpr mint inv() const { return pow(MOD - 2); } mint &operator/=(mint rhs) { return *this *= rhs.inv(); } mint operator/(const mint rhs) const { return mint(*this) /= rhs; } constexpr bool operator==(const mint &rhs) const noexcept { return this->x == rhs.x; } constexpr bool operator!=(const mint &rhs) const noexcept { return this->x != rhs.x; } bool operator<(const mint &rhs) const noexcept { return this->x < rhs.x; } bool operator>(const mint &rhs) const noexcept { return this->x > rhs.x; } bool operator<=(const mint &rhs) const noexcept { return this->x <= rhs.x; } bool operator>=(const mint &rhs) const noexcept { return this->x >= rhs.x; } friend istream &operator>>(istream &is, mint &a) { long long t; is >> t; a = mint(t); return is; } friend ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } }; void solve() { ll N; cin >> N; vector<vector<ll>> grid(N, vector<ll>(N)); rep(i, N) rep(j, N) cin >> grid[i][j]; vector<vector<mint>> dp(N + 1, vector<mint>(1 << N, 0)); dp[0][0] = 1; rep(i, N) { rep(v, 1 << N) { ll p_cnt = __builtin_popcount(v); if (p_cnt > i) continue; rep(j, N) { if (grid[i][j] == 0) continue; dp[i + 1][(v | (1 << j))] += dp[i][v]; } } } cout << dp[N][(1 << N) - 1] << '\n'; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif class mint { public: long long x; constexpr mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} constexpr mint operator-() const { return mint(-x); } constexpr mint &operator+=(const mint &rhs) { if ((x += rhs.x) >= MOD) x -= MOD; return *this; } constexpr mint &operator-=(const mint &rhs) { if ((x += MOD - rhs.x) >= MOD) x -= MOD; return *this; } constexpr mint &operator*=(const mint &rhs) { (x *= rhs.x) %= MOD; return *this; } constexpr mint operator+(const mint &rhs) const { mint res(*this); return res += rhs; } constexpr mint operator-(const mint &rhs) const { mint res(*this); return res -= rhs; } constexpr mint operator*(const mint &rhs) const { mint res(*this); return res *= rhs; } [[nodiscard]] constexpr mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } [[nodiscard]] constexpr mint inv() const { return pow(MOD - 2); } mint &operator/=(mint rhs) { return *this *= rhs.inv(); } mint operator/(const mint rhs) const { return mint(*this) /= rhs; } constexpr bool operator==(const mint &rhs) const noexcept { return this->x == rhs.x; } constexpr bool operator!=(const mint &rhs) const noexcept { return this->x != rhs.x; } bool operator<(const mint &rhs) const noexcept { return this->x < rhs.x; } bool operator>(const mint &rhs) const noexcept { return this->x > rhs.x; } bool operator<=(const mint &rhs) const noexcept { return this->x <= rhs.x; } bool operator>=(const mint &rhs) const noexcept { return this->x >= rhs.x; } friend istream &operator>>(istream &is, mint &a) { long long t; is >> t; a = mint(t); return is; } friend ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } }; void solve() { ll N; cin >> N; vector<vector<ll>> grid(N, vector<ll>(N)); rep(i, N) rep(j, N) cin >> grid[i][j]; vector<vector<mint>> dp(N + 1, vector<mint>(1 << N, 0)); dp[0][0] = 1; rep(i, N) { rep(v, 1 << N) { ll p_cnt = __builtin_popcount(v); if (p_cnt != i) continue; rep(j, N) { if (grid[i][j] == 0) continue; dp[i + 1][(v | (1 << j))] += dp[i][v]; } } } cout << dp[N][(1 << N) - 1] << '\n'; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); return 0; }
replace
118
119
118
119
TLE
p03174
C++
Memory Limit Exceeded
// Author : Siriuslight #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #define FIO \ ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0), \ cout << setprecision(10) << fixed; #define int long long #define F first #define S second using namespace std; using namespace __gnu_pbds; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif typedef long long ll; typedef long double ld; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const ll inf = 1e9 + 100, N = 23; const ll INF = 1e18 + 100, MOD = 1e9 + 7; ll powmod(ll a, ll b, ll m = MOD) { ll r = 1; while (b > 0) { if (b & 1) r = r * a % m; a = a * a % m; b >>= 1; } return r; } ll power(ll a, ll b) { ll r = 1; while (b > 0) { if (b & 1) r = r * a; a = a * a; b >>= 1; } return r; } ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a % b); } ll inv(ll a, ll m = MOD) { return powmod(a, m - 2, m); } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } // ordered_set X; // X.insert(3); // X.order_of_key(3) no of elements less than 3 //*X.find_by_order(n) nth greatest element(0-indexed) int n, a[N][N], dp[1LL << N][N]; int solve(int mask, int id) { if (id == 0) { return mask == ((1LL << (n + 1)) - 1); } int &res = dp[mask][id]; if (res != -1) return res; res = 0; for (int j = 1; j <= n; ++j) { if (a[id][j] == 1 && ((mask >> j) & 1) == 0) { res += solve(mask | (1LL << j), id - 1); res %= MOD; } } return res; } int32_t main() { FIO; cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> a[i][j]; } } memset(dp, -1, sizeof(dp)); cout << solve(1, n); }
// Author : Siriuslight #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #define FIO \ ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0), \ cout << setprecision(10) << fixed; // #define int long long #define F first #define S second using namespace std; using namespace __gnu_pbds; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif typedef long long ll; typedef long double ld; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const ll inf = 1e9 + 100, N = 23; const ll INF = 1e18 + 100, MOD = 1e9 + 7; ll powmod(ll a, ll b, ll m = MOD) { ll r = 1; while (b > 0) { if (b & 1) r = r * a % m; a = a * a % m; b >>= 1; } return r; } ll power(ll a, ll b) { ll r = 1; while (b > 0) { if (b & 1) r = r * a; a = a * a; b >>= 1; } return r; } ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a % b); } ll inv(ll a, ll m = MOD) { return powmod(a, m - 2, m); } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } // ordered_set X; // X.insert(3); // X.order_of_key(3) no of elements less than 3 //*X.find_by_order(n) nth greatest element(0-indexed) int n, a[N][N], dp[1LL << N][N]; int solve(int mask, int id) { if (id == 0) { return mask == ((1LL << (n + 1)) - 1); } int &res = dp[mask][id]; if (res != -1) return res; res = 0; for (int j = 1; j <= n; ++j) { if (a[id][j] == 1 && ((mask >> j) & 1) == 0) { res += solve(mask | (1LL << j), id - 1); res %= MOD; } } return res; } int32_t main() { FIO; cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> a[i][j]; } } memset(dp, -1, sizeof(dp)); cout << solve(1, n); }
replace
7
8
7
8
MLE
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define ll long long template <typename T, typename U> static inline void amin(T &x, U y) { if (x > y) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } #define rep(i, n) for (int i = 0; i < n; ++i) #define irep(i, k, n) for (int i = k; i <= n; ++i) #define irepr(i, k, n) for (int i = k; i >= n; --i) int inf = 2e9; ll INF = 2e18; const int mod = 1e9 + 7; const int N = 22; int n; vvi dp(N, vi(1 << N, -1)); vvi a(N, vi(N)); int solve(int r, int mask) { if (r == n) { return mask == 0; } if (dp[r][mask] == -1) { dp[r][mask] = 0; for (int i = 0; i < n; ++i) { if (a[r][n - 1 - i] == 1) { dp[r][mask] += solve(r + 1, mask ^ (1 << i)); dp[r][mask] %= mod; } } } return dp[r][mask]; } int main() { cin >> n; rep(i, n) { rep(j, n) { cin >> a[i][j]; } } cout << solve(0, (1 << n) - 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define ll long long template <typename T, typename U> static inline void amin(T &x, U y) { if (x > y) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } #define rep(i, n) for (int i = 0; i < n; ++i) #define irep(i, k, n) for (int i = k; i <= n; ++i) #define irepr(i, k, n) for (int i = k; i >= n; --i) int inf = 2e9; ll INF = 2e18; const int mod = 1e9 + 7; const int N = 22; int n; vvi dp(N, vi(1 << N, -1)); vvi a(N, vi(N)); int solve(int r, int mask) { if (r == n) { return mask == 0; } if (dp[r][mask] == -1) { dp[r][mask] = 0; for (int i = 0; i < n; ++i) { if (a[r][n - 1 - i] == 1 && mask & (1 << i)) { dp[r][mask] += solve(r + 1, mask ^ (1 << i)); dp[r][mask] %= mod; } } } return dp[r][mask]; } int main() { cin >> n; rep(i, n) { rep(j, n) { cin >> a[i][j]; } } cout << solve(0, (1 << n) - 1) << endl; return 0; }
replace
34
35
34
35
TLE
p03175
C++
Runtime Error
/* Written by Nitrogens Desire for getting accepted!! */ #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<db, db> pdd; const int maxn = 1e5 + 5; const int Mod = 1000000007; const int INF = 0x3f3f3f3f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const double e = exp(1); const db PI = acos(-1); const db ERR = 1e-10; #define Se second #define Fi first #define pb push_back #define dbg(x) cout << #x << " = " << (x) << endl #define dbg2(x1, x2) \ cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl #define dbg3(x1, x2, x3) \ cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3 \ << " = " << x3 << endl int head[maxn], cnt, n; ll dp[maxn][2]; struct edge { int v, nxt; } Edge[maxn]; void init() { for (int i = 0; i <= n; i++) head[i] = -1; cnt = 0; } void addedge(int u, int v) { Edge[cnt].v = v; Edge[cnt].nxt = head[u]; head[u] = cnt++; } void dfs(int id, int fa) { dp[id][0] = dp[id][1] = 1; int cnt = 0; for (int i = head[id]; i != -1; i = Edge[i].nxt) { int v = Edge[i].v; if (v == fa) continue; ++cnt; dfs(v, id); // black dp[id][0] = (dp[id][0] * dp[v][1]) % Mod; // white dp[id][1] = (dp[id][1] * (dp[v][0] + dp[v][1])) % Mod; } } int main() { // ios::sync_with_stdio(false); // freopen("a.txt","r",stdin); // freopen("b.txt","w",stdout); int u, v; scanf("%d", &n); init(); for (int i = 0; i < n - 1; i++) { scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, -1); printf("%lld\n", (dp[1][0] + dp[1][1]) % Mod); // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << // endl; return 0; }
/* Written by Nitrogens Desire for getting accepted!! */ #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<db, db> pdd; const int maxn = 1e5 + 5; const int Mod = 1000000007; const int INF = 0x3f3f3f3f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const double e = exp(1); const db PI = acos(-1); const db ERR = 1e-10; #define Se second #define Fi first #define pb push_back #define dbg(x) cout << #x << " = " << (x) << endl #define dbg2(x1, x2) \ cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl #define dbg3(x1, x2, x3) \ cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3 \ << " = " << x3 << endl int head[maxn], cnt, n; ll dp[maxn][2]; struct edge { int v, nxt; } Edge[2 * maxn]; void init() { for (int i = 0; i <= n; i++) head[i] = -1; cnt = 0; } void addedge(int u, int v) { Edge[cnt].v = v; Edge[cnt].nxt = head[u]; head[u] = cnt++; } void dfs(int id, int fa) { dp[id][0] = dp[id][1] = 1; int cnt = 0; for (int i = head[id]; i != -1; i = Edge[i].nxt) { int v = Edge[i].v; if (v == fa) continue; ++cnt; dfs(v, id); // black dp[id][0] = (dp[id][0] * dp[v][1]) % Mod; // white dp[id][1] = (dp[id][1] * (dp[v][0] + dp[v][1])) % Mod; } } int main() { // ios::sync_with_stdio(false); // freopen("a.txt","r",stdin); // freopen("b.txt","w",stdout); int u, v; scanf("%d", &n); init(); for (int i = 0; i < n - 1; i++) { scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, -1); printf("%lld\n", (dp[1][0] + dp[1][1]) % Mod); // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << // endl; return 0; }
replace
50
51
50
51
0
p03175
C++
Runtime Error
// You either die a hero, or you live long enough to see yourself become the // villain. /** * author: Blind_is_love * created: 12.10.2019, 21:07 **/ #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; typedef long long ll; template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; } template <class T, class S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <class Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <class Arg1, class... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *sep = strchr(names + 1, ','); cerr.write(names, sep - names) << " : " << arg1 << " "; __f(sep + 1, args...); } #else #define trace(...) 0 #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target( \ "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #endif // ifndef ONLINE_JUDGE #define endl "\n" #define vll vector<int> #define max_heap priority_queue<int> #define min_heap priority_queue<int, vector<int>, greater<int>> #define pb push_back #define all(container) container.begin(), container.end() #define pll pair<ll, ll> #define pii pair<int, int> #define printArr(name, from, to) \ for (int x = from; x < to; x++) { \ cout << name[x] << " "; \ } \ cout << endl; #define fi first #define se second #define mp make_pair #define prime1 304933 #define prime2 15486277 #define mod2 179424691 #define int ll typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const ll MOD = 1000000007; const ll MAX = 4005; const double PI = 3.1415926; const ll INF = 1e18; const double EPS = 1e-5; int n; vll adj[MAX]; int dp[MAX][2]; bool seen[MAX][2]; int dfs(int s, int p, bool b) { int ans[2]{}; ans[0] = 1; if (!b) ans[1] = 1; for (int x : adj[s]) { if (x == p) continue; if (seen[x][false]) { ans[0] = (ans[0] * dp[x][false]) % MOD; continue; } if (seen[x][true]) { ans[1] = (ans[1] * dp[x][true]) % MOD; continue; } ans[0] = (ans[0] * dfs(x, s, false)) % MOD; ans[1] = (ans[1] * dfs(x, s, true)) % MOD; } seen[s][b] = true; return dp[s][b] = (ans[0] + ans[1]) % MOD; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); clock_t t1, t2; t1 = clock(); cin >> n; for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; x--; y--; adj[x].pb(y); adj[y].pb(x); } cout << dfs(0, 0, 0) << endl; t2 = clock(); cerr << "time taken: " << (t2 - t1) / (long double)CLOCKS_PER_SEC << endl; return 0; }
// You either die a hero, or you live long enough to see yourself become the // villain. /** * author: Blind_is_love * created: 12.10.2019, 21:07 **/ #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; typedef long long ll; template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; } template <class T, class S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <class Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <class Arg1, class... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *sep = strchr(names + 1, ','); cerr.write(names, sep - names) << " : " << arg1 << " "; __f(sep + 1, args...); } #else #define trace(...) 0 #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target( \ "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #endif // ifndef ONLINE_JUDGE #define endl "\n" #define vll vector<int> #define max_heap priority_queue<int> #define min_heap priority_queue<int, vector<int>, greater<int>> #define pb push_back #define all(container) container.begin(), container.end() #define pll pair<ll, ll> #define pii pair<int, int> #define printArr(name, from, to) \ for (int x = from; x < to; x++) { \ cout << name[x] << " "; \ } \ cout << endl; #define fi first #define se second #define mp make_pair #define prime1 304933 #define prime2 15486277 #define mod2 179424691 #define int ll typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const ll MOD = 1000000007; const ll MAX = 1e5 + 10; const double PI = 3.1415926; const ll INF = 1e18; const double EPS = 1e-5; int n; vll adj[MAX]; int dp[MAX][2]; bool seen[MAX][2]; int dfs(int s, int p, bool b) { int ans[2]{}; ans[0] = 1; if (!b) ans[1] = 1; for (int x : adj[s]) { if (x == p) continue; if (seen[x][false]) { ans[0] = (ans[0] * dp[x][false]) % MOD; continue; } if (seen[x][true]) { ans[1] = (ans[1] * dp[x][true]) % MOD; continue; } ans[0] = (ans[0] * dfs(x, s, false)) % MOD; ans[1] = (ans[1] * dfs(x, s, true)) % MOD; } seen[s][b] = true; return dp[s][b] = (ans[0] + ans[1]) % MOD; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); clock_t t1, t2; t1 = clock(); cin >> n; for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; x--; y--; adj[x].pb(y); adj[y].pb(x); } cout << dfs(0, 0, 0) << endl; t2 = clock(); cerr << "time taken: " << (t2 - t1) / (long double)CLOCKS_PER_SEC << endl; return 0; }
replace
91
92
91
92
0
time taken: 9.7e-05
p03175
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; struct ModInt { using M = ModInt; long long v; ModInt(long long _v = 0) : v(_v % MOD + MOD) { norm(); } M &norm() { v = (v < MOD) ? v : v - MOD; return *this; } M operator+(const M &x) const { return M(v + x.v); } M operator-(const M &x) const { return M(v + MOD - x.v); } M operator*(const M &x) const { return M(v * x.v % MOD); } M operator/(const M &x) const { return M(v * x.inv().v); } M &operator+=(const M &x) { return *this = *this + x; } M &operator-=(const M &x) { return *this = *this - x; } M &operator*=(const M &x) { return *this = *this * x; } M &operator/=(const M &x) { return *this = *this / x; } friend istream &operator>>(istream &input, M &x) { return input >> x.v, x.norm(), input; } friend ostream &operator<<(ostream &output, const M &x) { return output << x.v; } M pow(long long n) const { M x(v), res(1); while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } M inv() const { return this->pow(MOD - 2); } static long long MOD; static vector<M> fact, finv; static void build(int n) { fact.assign(n + 1, 1); finv.assign(n + 1, 1); for (int i = 1; i < n + 1; i++) fact[i] = fact[i - 1] * M(i); for (int i = 0; i < n + 1; i++) finv[i] = fact[i].inv(); } static M comb(int n, int k) { if (n < k || k < 0) return M(0); return fact[n] * finv[n - k] * finv[k]; } static M extgcd(int a, int b, int *x, int *y) { M d(a); if (b) { d = extgcd(b, a % b, y, x); *y -= (a / b) * *x; } else { *x = 1, *y = 0; } return d; } }; vector<ModInt> ModInt::fact = vector<ModInt>(); vector<ModInt> ModInt::finv = vector<ModInt>(); long long ModInt::MOD = 1e9 + 7; const int maxn = 11; ModInt dp[maxn][2]; int n; vector<vector<int>> graph; vector<bool> visited; /* 木DP。 */ void dfs(int i) { visited[i] = true; dp[i][0] = 1; dp[i][1] = 1; for (int j : graph[i]) { if (visited[j]) continue; dfs(j); dp[i][0] *= dp[j][0] + dp[j][1]; dp[i][1] *= dp[j][0]; } return; } int main() { cin >> n; graph.resize(n); visited.resize(n); rep(i, n - 1) { int x, y; cin >> x >> y; x--; y--; graph[x].push_back(y); graph[y].push_back(x); } dfs(0); cout << dp[0][0] + dp[0][1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; struct ModInt { using M = ModInt; long long v; ModInt(long long _v = 0) : v(_v % MOD + MOD) { norm(); } M &norm() { v = (v < MOD) ? v : v - MOD; return *this; } M operator+(const M &x) const { return M(v + x.v); } M operator-(const M &x) const { return M(v + MOD - x.v); } M operator*(const M &x) const { return M(v * x.v % MOD); } M operator/(const M &x) const { return M(v * x.inv().v); } M &operator+=(const M &x) { return *this = *this + x; } M &operator-=(const M &x) { return *this = *this - x; } M &operator*=(const M &x) { return *this = *this * x; } M &operator/=(const M &x) { return *this = *this / x; } friend istream &operator>>(istream &input, M &x) { return input >> x.v, x.norm(), input; } friend ostream &operator<<(ostream &output, const M &x) { return output << x.v; } M pow(long long n) const { M x(v), res(1); while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } M inv() const { return this->pow(MOD - 2); } static long long MOD; static vector<M> fact, finv; static void build(int n) { fact.assign(n + 1, 1); finv.assign(n + 1, 1); for (int i = 1; i < n + 1; i++) fact[i] = fact[i - 1] * M(i); for (int i = 0; i < n + 1; i++) finv[i] = fact[i].inv(); } static M comb(int n, int k) { if (n < k || k < 0) return M(0); return fact[n] * finv[n - k] * finv[k]; } static M extgcd(int a, int b, int *x, int *y) { M d(a); if (b) { d = extgcd(b, a % b, y, x); *y -= (a / b) * *x; } else { *x = 1, *y = 0; } return d; } }; vector<ModInt> ModInt::fact = vector<ModInt>(); vector<ModInt> ModInt::finv = vector<ModInt>(); long long ModInt::MOD = 1e9 + 7; const int maxn = 100001; ModInt dp[maxn][2]; int n; vector<vector<int>> graph; vector<bool> visited; /* 木DP。 */ void dfs(int i) { visited[i] = true; dp[i][0] = 1; dp[i][1] = 1; for (int j : graph[i]) { if (visited[j]) continue; dfs(j); dp[i][0] *= dp[j][0] + dp[j][1]; dp[i][1] *= dp[j][0]; } return; } int main() { cin >> n; graph.resize(n); visited.resize(n); rep(i, n - 1) { int x, y; cin >> x >> y; x--; y--; graph[x].push_back(y); graph[y].push_back(x); } dfs(0); cout << dp[0][0] + dp[0][1] << endl; return 0; }
replace
69
70
69
70
0
p03175
C++
Runtime Error
#include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define ppb pop_back #define ppf pop_front #define umax(x, y) x = max(x, y) #define umin(x, y) x = min(x, y) #define ll long long #define ii pair<int, int> #define iii pair<ii, int> #define iiii pair<ii, ii> #define sz(x) ((int)x.size()) #define orta ((bas + son) >> 1) #define all(x) x.begin(), x.end() #define dbgs(x) cerr << (#x) << " --> " << (x) << " " #define dbg(x) \ cerr << (#x) << " --> " << (x) << endl; \ getchar() #define pw(x) (1 << (x)) #define inf 1000000000000005 #define MOD 1000000007 #define N 10005 #define M 27000 #define LOG 1000000 #define KOK 650 #define EPS 0.000000001 using namespace std; int n, x, y; int dp[N][2]; vector<int> v[N]; void dfs(int node, int ata) { for (int i : v[node]) { if (i == ata) continue; dfs(i, node); } int z = 1, o = 1; for (int i : v[node]) { if (i == ata) continue; z = 1ll * z * dp[i][0] % MOD; o = 1ll * o * dp[i][1] % MOD; } dp[node][0] = (z + o) % MOD; dp[node][1] = z; } int main() { // freopen("input.txt","r",stdin); scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d %d", &x, &y); v[x].pb(y); v[y].pb(x); } dfs(1, 0); printf("%d", dp[1][0]); }
#include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define ppb pop_back #define ppf pop_front #define umax(x, y) x = max(x, y) #define umin(x, y) x = min(x, y) #define ll long long #define ii pair<int, int> #define iii pair<ii, int> #define iiii pair<ii, ii> #define sz(x) ((int)x.size()) #define orta ((bas + son) >> 1) #define all(x) x.begin(), x.end() #define dbgs(x) cerr << (#x) << " --> " << (x) << " " #define dbg(x) \ cerr << (#x) << " --> " << (x) << endl; \ getchar() #define pw(x) (1 << (x)) #define inf 1000000000000005 #define MOD 1000000007 #define N 100005 #define M 27000 #define LOG 1000000 #define KOK 650 #define EPS 0.000000001 using namespace std; int n, x, y; int dp[N][2]; vector<int> v[N]; void dfs(int node, int ata) { for (int i : v[node]) { if (i == ata) continue; dfs(i, node); } int z = 1, o = 1; for (int i : v[node]) { if (i == ata) continue; z = 1ll * z * dp[i][0] % MOD; o = 1ll * o * dp[i][1] % MOD; } dp[node][0] = (z + o) % MOD; dp[node][1] = z; } int main() { // freopen("input.txt","r",stdin); scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d %d", &x, &y); v[x].pb(y); v[y].pb(x); } dfs(1, 0); printf("%d", dp[1][0]); }
replace
22
23
22
23
0
p03175
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; pair<ll, ll> func(vector<vector<int>> &v, bool b[], int ind, ll &mod) { b[ind] = 1; ll t = 1, t1 = 1; for (int i = 0; i < v[ind].size(); i++) { if (b[v[ind][i]] == 0) { pair<ll, ll> p; p = func(v, b, v[ind][i], mod); t = (t * ((p.first + p.second) % mod)) % mod; t1 = (t1 * p.second) % mod; } } return {t1, t}; } int main() { int n; cin >> n; if (n == 1) return 2; vector<vector<int>> v(n + 1); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } bool b[n + 1]; ll mod = 1e9 + 7; for (int i = 0; i <= n; i++) b[i] = 0; pair<ll, ll> ans[n + 1]; for (int i = 0; i <= n; i++) ans[i] = {0, 0}; ans[1] = func(v, b, 1, mod); ll k = (ans[1].first + ans[1].second) % mod; cout << k; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; pair<ll, ll> func(vector<vector<int>> &v, bool b[], int ind, ll &mod) { b[ind] = 1; ll t = 1, t1 = 1; for (int i = 0; i < v[ind].size(); i++) { if (b[v[ind][i]] == 0) { pair<ll, ll> p; p = func(v, b, v[ind][i], mod); t = (t * ((p.first + p.second) % mod)) % mod; t1 = (t1 * p.second) % mod; } } return {t1, t}; } int main() { int n; cin >> n; if (n == 1) { cout << "2"; return 0; } vector<vector<int>> v(n + 1); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } bool b[n + 1]; ll mod = 1e9 + 7; for (int i = 0; i <= n; i++) b[i] = 0; pair<ll, ll> ans[n + 1]; for (int i = 0; i <= n; i++) ans[i] = {0, 0}; ans[1] = func(v, b, 1, mod); ll k = (ans[1].first + ans[1].second) % mod; cout << k; return 0; }
replace
20
22
20
24
0
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define pb push_back #define ppb pop_back #define endl '\n' #define mii map<ll, ll> #define pii pair<ll, ll> #define vi vector<ll> #define vs vector<string> #define all(a) (a).begin(), (a).end() #define F first #define S second #define sz(x) (ll) x.size() #define hell 1000000007 #define inf (ll)1e18 + 5 #define PI (double)3.14159265358979323844 #define DECIMAL(n) cout << fixed << setprecision(n); #define rep(i, a, b) for (ll i = a; i < b; i++) #define repr(i, a, b) for (ll i = a - 1; i >= b; i--) #define bitcount(a) (ll) __builtin_popcount(a) #define lbnd lower_bound #define ubnd upper_bound #define mp make_pair #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { std::cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } using namespace std; using namespace __gnu_pbds; /*----------------------Graph Moves----------------*/ // const int fx[]={+1,-1,+0,+0}; // const int fy[]={+0,+0,+1,-1}; // const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move // const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move // const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move // const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move /*------------------------------------------------*/ // primes for hashing 937,991,1013,1409,1741 ll add(ll a, ll b) { return ((a % hell) + (b % hell)) % hell; } ll sub(ll a, ll b) { return ((a % hell) - (b % hell) + hell) % hell; } ll mul(ll a, ll b) { return ((a % hell) * (b % hell)) % hell; } const ll MAXN = 100005; ll n; vi a[MAXN]; ll dp[MAXN][2]; ll dfs(ll node, ll color, ll par) { // if (dp[node][color]!=-1)return dp[node][color]; dp[node][color] = 1; if (color == 1) { for (auto i : a[node]) { if (i != par) { dp[node][color] = mul(dp[node][color], dfs(i, 0, node)); } } } else { for (auto i : a[node]) { if (i != par) { dp[node][color] = mul(dp[node][color], add(dfs(i, 0, node), dfs(i, 1, node))); } } } return dp[node][color]; } void solve() { cin >> n; rep(i, 0, n - 1) { ll x, y; cin >> x >> y; a[x].pb(y); a[y].pb(x); } memset(dp, -1, sizeof dp); cout << add(dfs(1, 0, -1), dfs(1, 1, -1)) << endl; } signed main() { ios ll test = 1; // cin>>test; while (test--) solve(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define pb push_back #define ppb pop_back #define endl '\n' #define mii map<ll, ll> #define pii pair<ll, ll> #define vi vector<ll> #define vs vector<string> #define all(a) (a).begin(), (a).end() #define F first #define S second #define sz(x) (ll) x.size() #define hell 1000000007 #define inf (ll)1e18 + 5 #define PI (double)3.14159265358979323844 #define DECIMAL(n) cout << fixed << setprecision(n); #define rep(i, a, b) for (ll i = a; i < b; i++) #define repr(i, a, b) for (ll i = a - 1; i >= b; i--) #define bitcount(a) (ll) __builtin_popcount(a) #define lbnd lower_bound #define ubnd upper_bound #define mp make_pair #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { std::cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } using namespace std; using namespace __gnu_pbds; /*----------------------Graph Moves----------------*/ // const int fx[]={+1,-1,+0,+0}; // const int fy[]={+0,+0,+1,-1}; // const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move // const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move // const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move // const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move /*------------------------------------------------*/ // primes for hashing 937,991,1013,1409,1741 ll add(ll a, ll b) { return ((a % hell) + (b % hell)) % hell; } ll sub(ll a, ll b) { return ((a % hell) - (b % hell) + hell) % hell; } ll mul(ll a, ll b) { return ((a % hell) * (b % hell)) % hell; } const ll MAXN = 100005; ll n; vi a[MAXN]; ll dp[MAXN][2]; ll dfs(ll node, ll color, ll par) { trace(node, color); if (dp[node][color] != -1) return dp[node][color]; dp[node][color] = 1; if (color == 1) { for (auto i : a[node]) { if (i != par) { dp[node][color] = mul(dp[node][color], dfs(i, 0, node)); } } } else { for (auto i : a[node]) { if (i != par) { dp[node][color] = mul(dp[node][color], add(dfs(i, 0, node), dfs(i, 1, node))); } } } return dp[node][color]; } void solve() { cin >> n; rep(i, 0, n - 1) { ll x, y; cin >> x >> y; a[x].pb(y); a[y].pb(x); } memset(dp, -1, sizeof dp); cout << add(dfs(1, 0, -1), dfs(1, 1, -1)) << endl; } signed main() { ios ll test = 1; // cin>>test; while (test--) solve(); return 0; }
replace
68
69
68
71
TLE
p03175
C++
Memory Limit Exceeded
#include <iostream> #include <vector> using namespace std; using ll = long long; ll mod = 1e9 + 7; pair<ll, ll> dfs(int s, vector<int> graph[], vector<bool> visited) { visited[s] = 1; pair<ll, ll> pr = {1, 1}; for (int v : graph[s]) { if (!visited[v]) { pair<ll, ll> pr1 = dfs(v, graph, visited); pr.first *= pr1.second; pr.first %= mod; pr.second *= (pr1.second + pr1.first); pr.second %= mod; } } return pr; } int main() { int n; cin >> n; vector<int> graph[n + 1]; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } vector<bool> visited(n + 1, false); pair<ll, ll> pr = dfs(1, graph, visited); cout << (pr.first + pr.second) % mod; }
#include <iostream> #include <vector> using namespace std; using ll = long long; ll mod = 1e9 + 7; pair<ll, ll> dfs(int s, vector<int> graph[], vector<bool> &visited) { visited[s] = 1; pair<ll, ll> pr = {1, 1}; for (int v : graph[s]) { if (!visited[v]) { pair<ll, ll> pr1 = dfs(v, graph, visited); pr.first *= pr1.second; pr.first %= mod; pr.second *= (pr1.second + pr1.first); pr.second %= mod; } } return pr; } int main() { int n; cin >> n; vector<int> graph[n + 1]; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } vector<bool> visited(n + 1, false); pair<ll, ll> pr = dfs(1, graph, visited); cout << (pr.first + pr.second) % mod; }
replace
5
6
5
6
MLE
p03175
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define pb emplace_back using namespace std; const int MAXN = 1e5 + 10, MD = 1e9 + 7; int n; ll dp[MAXN][2]; vector<int> g[MAXN]; void Dfs(int v = 1, int p = -1) { dp[v][0] = 1; dp[v][1] = 1; for (auto i : g[v]) { if (p == i) continue; Dfs(i, v); dp[v][0] = dp[v][0] * (dp[i][0] + dp[i][1]) % MD; dp[v][1] = dp[v][1] * dp[i][0] % MD; } } int main() { ios::sync_with_stdio(0), cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } Dfs(); ll ans = dp[1][0] + dp[1][1]; cout << (ans + MD) % MD << '\n'; return 0; }
#include <bits/stdc++.h> #define ll long long #define pb emplace_back using namespace std; const int MAXN = 1e5 + 10, MD = 1e9 + 7; int n; ll dp[MAXN][2]; vector<int> g[MAXN]; void Dfs(int v = 1, int p = -1) { dp[v][0] = 1; dp[v][1] = 1; for (auto i : g[v]) { if (p == i) continue; Dfs(i, v); dp[v][0] = dp[v][0] * (dp[i][0] + dp[i][1]) % MD; dp[v][1] = dp[v][1] * dp[i][0] % MD; } } int main() { ios::sync_with_stdio(0), cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } Dfs(); ll ans = dp[1][0] + dp[1][1]; cout << (ans + MD) % MD << '\n'; return 0; }
replace
28
29
28
29
0
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define fori(i, a, b) for (int i = a; i <= b; i++) #define ford(i, a, b) for (int i = a; i >= b; i--) #define all(a) a.begin(), a.end() #define in(c, x) ((c).find(x) != (c).end()) #define isp(c, x) (find(all(c), x) != (c).end()) #define pb push_back #define ppb pop_back #define sz(x) (int)x.size() #define fill(a, v) memset(a, v, sizeof a) #define fs first #define sc second #define ub upper_bound #define lb lower_bound #define en '\n' typedef long long int lli; typedef long double ld; typedef vector<int> vi; typedef vector<long long int> vlli; typedef pair<int, int> pii; typedef pair<lli, lli> plli; typedef list<int> li; typedef map<int, int> mi; typedef map<lli, lli> mlli; lli gcd(lli a, lli b) { return (!b) ? a : gcd(b, a % b); } int t = 1; int ti; lli md = 1e9 + 7; const int N = 1e5 + 3; lli n, mem[N][2]; li adj[N]; lli dfs(int s, int c, int p) { // if(mem[s][c] != -1) // return mem[s][c]; int ff = 0; lli ans = 1; for (auto x : adj[s]) { if (x != p) { ff = 1; if (c == 0) { ans *= (dfs(x, 0, s) + dfs(x, 1, s)); ans %= md; } else { ans *= dfs(x, 0, s); ans %= md; } } } if (!ff) { return 1; } return mem[s][c] = ans; } void solve() { cin >> n; fill(mem, -1); fori(i, 1, n - 1) { int x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } lli ans = 0; ans += dfs(1, 0, -1); ans %= md; ans += dfs(1, 1, -1); ans %= md; cout << ans; } // THINK OF SPECIAL CASES, ARRAY LIMITS, LLI vs INT, 1LL int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // cin>>t; // for(ti= 1; ti <= t; ti++) solve(); // cout<<"\nTHINK OF SPECIAL CASES, ARRAY LIMITS, LLI vs INT, 1LL"; }
#include <bits/stdc++.h> using namespace std; #define fori(i, a, b) for (int i = a; i <= b; i++) #define ford(i, a, b) for (int i = a; i >= b; i--) #define all(a) a.begin(), a.end() #define in(c, x) ((c).find(x) != (c).end()) #define isp(c, x) (find(all(c), x) != (c).end()) #define pb push_back #define ppb pop_back #define sz(x) (int)x.size() #define fill(a, v) memset(a, v, sizeof a) #define fs first #define sc second #define ub upper_bound #define lb lower_bound #define en '\n' typedef long long int lli; typedef long double ld; typedef vector<int> vi; typedef vector<long long int> vlli; typedef pair<int, int> pii; typedef pair<lli, lli> plli; typedef list<int> li; typedef map<int, int> mi; typedef map<lli, lli> mlli; lli gcd(lli a, lli b) { return (!b) ? a : gcd(b, a % b); } int t = 1; int ti; lli md = 1e9 + 7; const int N = 1e5 + 3; lli n, mem[N][2]; li adj[N]; lli dfs(int s, int c, int p) { if (mem[s][c] != -1) return mem[s][c]; int ff = 0; lli ans = 1; for (auto x : adj[s]) { if (x != p) { ff = 1; if (c == 0) { ans *= (dfs(x, 0, s) + dfs(x, 1, s)); ans %= md; } else { ans *= dfs(x, 0, s); ans %= md; } } } if (!ff) { return 1; } return mem[s][c] = ans; } void solve() { cin >> n; fill(mem, -1); fori(i, 1, n - 1) { int x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } lli ans = 0; ans += dfs(1, 0, -1); ans %= md; ans += dfs(1, 1, -1); ans %= md; cout << ans; } // THINK OF SPECIAL CASES, ARRAY LIMITS, LLI vs INT, 1LL int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // cin>>t; // for(ti= 1; ti <= t; ti++) solve(); // cout<<"\nTHINK OF SPECIAL CASES, ARRAY LIMITS, LLI vs INT, 1LL"; }
replace
37
39
37
39
TLE
p03175
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; int f[100001][2]; int head[100001], to[100001], nxt[100001]; int tot; int n; void addedge(int u, int v) { tot++; nxt[tot] = head[u]; to[tot] = v; head[u] = tot; } void dfs(int index, int p) { f[index][0] = f[index][1] = 1; for (int i = head[index]; i; i = nxt[i]) { int v = to[i]; if (p == v) continue; dfs(v, index); f[index][0] = f[index][0] * ((long long)f[v][0] + f[v][1]) % 1000000007; f[index][1] = f[index][1] * (long long)f[v][0] % 1000000007; } return; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, 0); printf("%lld", ((long long)f[1][0] + f[1][1]) % 1000000007); }
#include "bits/stdc++.h" using namespace std; int f[100001][2]; int head[100001], to[200001], nxt[200001]; int tot; int n; void addedge(int u, int v) { tot++; nxt[tot] = head[u]; to[tot] = v; head[u] = tot; } void dfs(int index, int p) { f[index][0] = f[index][1] = 1; for (int i = head[index]; i; i = nxt[i]) { int v = to[i]; if (p == v) continue; dfs(v, index); f[index][0] = f[index][0] * ((long long)f[v][0] + f[v][1]) % 1000000007; f[index][1] = f[index][1] * (long long)f[v][0] % 1000000007; } return; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, 0); printf("%lld", ((long long)f[1][0] + f[1][1]) % 1000000007); }
replace
3
4
3
4
0
p03175
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; // dp[i][j]=(頂点iを(j = 1黒く: j = // 0白く)塗ったとき、iを親とする部分木の塗り方の場合の数) int dp[100100][2]; int dp[100][2]; vector<vector<int>> g; const int MOD = 1e9 + 7; void dfs(int v, int p) { dp[v][0] = 1; dp[v][1] = 1; for (auto next_n : g[v]) { if (next_n == p) continue; dfs(next_n, v); // 自分が白なら白黒どっちでもok dp[v][0] = dp[v][0] * (dp[next_n][0] + dp[next_n][1]); dp[v][0] %= MOD; // 黒ならその前は絶対に白 dp[v][1] = dp[v][1] * dp[next_n][0]; dp[v][1] %= MOD; } } int main() { int N; cin >> N; g.resize(N); for (int i = 0; i < N - 1; ++i) { int x, y; cin >> x >> y; --x; --y; g[x].push_back(y); g[y].push_back(x); } dfs(0, -1); cout << (dp[0][0] + dp[0][1]) % MOD << endl; }
#include <iostream> #include <vector> using namespace std; // dp[i][j]=(頂点iを(j = 1黒く: j = // 0白く)塗ったとき、iを親とする部分木の塗り方の場合の数) long long dp[100100][2]; // long long dp[100][2]; vector<vector<int>> g; const int MOD = 1e9 + 7; void dfs(int v, int p) { dp[v][0] = 1; dp[v][1] = 1; for (auto next_n : g[v]) { if (next_n == p) continue; dfs(next_n, v); // 自分が白なら白黒どっちでもok dp[v][0] = dp[v][0] * (dp[next_n][0] + dp[next_n][1]); dp[v][0] %= MOD; // 黒ならその前は絶対に白 dp[v][1] = dp[v][1] * dp[next_n][0]; dp[v][1] %= MOD; } } int main() { int N; cin >> N; g.resize(N); for (int i = 0; i < N - 1; ++i) { int x, y; cin >> x >> y; --x; --y; g[x].push_back(y); g[y].push_back(x); } dfs(0, -1); cout << (dp[0][0] + dp[0][1]) % MOD << endl; }
replace
6
8
6
9
0
p03175
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define ff first #define ss second #define mp make_pair #define pb push_back #define endl "\n" #define si set<int> #define vi vector<int> #define pii pair<int, int> #define vpii vector<pii> #define mii map<int, int> #define que_max priority_queue<int> #define que_min priority_queue<int, vi, greater<int>> #define loop(i, s, e) for (int i = s; i < e; i++) #define rloop(i, e, s) for (int i = e; i >= s; i--) #define mset(a, f) memset(a, f, sizeof(a)) #define M 1000000007 #define sz(x) ((int)x.size()) #define all(p) p.begin(), p.end() #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__) #define print(a) \ for (auto x : a) \ cout << x << " "; \ cout << endl #define Print(a, x, y) \ for (int i = x; i < y; i++) \ cout << a[i] << " "; \ cout << endl const int inf = INT_MAX; const long long INF = LLONG_MAX; inline int power(int a, int b) { int x = 1; while (b) { if (b & 1) x *= a; a *= a; b >>= 1; } return x; } template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << 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...); } const int N = 3e5 + 7; vector<vi> adj(N); bool vis[N]; // 0-->white int dp[100005][2]; int n; /* In tree vale questions no need to keep the visited array as each node can be visited only once as each node has exactly one parent except the root node which has no parent. while doing dfs of a tree we only need to keep track of the parent node.*/ int dfs(int id, int p, int pc) { int &ans = dp[id][pc]; if (ans != -1) return ans; int a1 = 1; for (auto i : adj[id]) { if (i != p) { a1 *= dfs(i, id, 0); a1 %= M; } } int a2 = 0; if (pc != 1) { a2 = 1; for (auto i : adj[id]) { if (i != p) { a2 *= dfs(i, id, 1); a2 %= M; } } } return ans = (a1 + a2) % M; } void solve() { cin >> n; adj.resize(n + 1); mset(dp, -1); loop(i, 0, n - 1) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } cout << dfs(1, 0, 3) << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cout << setprecision(9) << fixed; int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define ff first #define ss second #define mp make_pair #define pb push_back #define endl "\n" #define si set<int> #define vi vector<int> #define pii pair<int, int> #define vpii vector<pii> #define mii map<int, int> #define que_max priority_queue<int> #define que_min priority_queue<int, vi, greater<int>> #define loop(i, s, e) for (int i = s; i < e; i++) #define rloop(i, e, s) for (int i = e; i >= s; i--) #define mset(a, f) memset(a, f, sizeof(a)) #define M 1000000007 #define sz(x) ((int)x.size()) #define all(p) p.begin(), p.end() #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__) #define print(a) \ for (auto x : a) \ cout << x << " "; \ cout << endl #define Print(a, x, y) \ for (int i = x; i < y; i++) \ cout << a[i] << " "; \ cout << endl const int inf = INT_MAX; const long long INF = LLONG_MAX; inline int power(int a, int b) { int x = 1; while (b) { if (b & 1) x *= a; a *= a; b >>= 1; } return x; } template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << 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...); } const int N = 3e5 + 7; vector<vi> adj(N); bool vis[N]; // 0-->white int dp[100005][2]; int n; /* In tree vale questions no need to keep the visited array as each node can be visited only once as each node has exactly one parent except the root node which has no parent. while doing dfs of a tree we only need to keep track of the parent node.*/ int dfs(int id, int p, int pc) { int &ans = dp[id][pc]; if (ans != -1) return ans; int a1 = 1; for (auto i : adj[id]) { if (i != p) { a1 *= dfs(i, id, 0); a1 %= M; } } int a2 = 0; if (pc != 1) { a2 = 1; for (auto i : adj[id]) { if (i != p) { a2 *= dfs(i, id, 1); a2 %= M; } } } return ans = (a1 + a2) % M; } void solve() { cin >> n; adj.resize(n + 1); mset(dp, -1); loop(i, 0, n - 1) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } cout << dfs(1, 0, 3) << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(9) << fixed; int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
delete
118
122
118
118
0
p03175
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int, int> #define F first #define S second #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 + 1 #define endl '\n' #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define forI(i, a, n) for (int i = a; i <= n; i++) #define forD(i, a, n) for (int i = n; i >= a; i--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; void init() { ios_base::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 dp[100005][2]; vi Tree[100005]; void compute(int n, int p) { dp[n][0] = dp[n][1] = 1; for (auto child : Tree[n]) { if (child == p) continue; compute(child, n); dp[n][0] = dp[n][0] * ((dp[child][0] + dp[child][1]) % mod) % mod; dp[n][1] = dp[n][1] * dp[child][0] % mod; } } void solve() { int n, x, y; cin >> n; forI(i, 1, n) { cin >> x >> y; Tree[x].pb(y); Tree[y].pb(x); } compute(1, -1); cout << (dp[1][0] + dp[1][1]) % mod << endl; } int32_t main() { init(); // w(t) solve(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int, int> #define F first #define S second #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 + 1 #define endl '\n' #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define forI(i, a, n) for (int i = a; i <= n; i++) #define forD(i, a, n) for (int i = n; i >= a; i--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; void init() { ios_base::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 dp[100005][2]; vi Tree[100005]; void compute(int n, int p) { dp[n][0] = dp[n][1] = 1; for (auto child : Tree[n]) { if (child == p) continue; compute(child, n); dp[n][0] = dp[n][0] * ((dp[child][0] + dp[child][1]) % mod) % mod; dp[n][1] = dp[n][1] * dp[child][0] % mod; } } void solve() { int n, x, y; cin >> n; forI(i, 1, n - 1) { cin >> x >> y; Tree[x].pb(y); Tree[y].pb(x); } compute(1, -1); cout << (dp[1][0] + dp[1][1]) % mod << endl; } int32_t main() { init(); // w(t) solve(); return 0; }
replace
64
65
64
65
-11
p03175
C++
Runtime Error
// KiSmAt #include "bits/stdc++.h" using namespace std; using ll = long long; const ll inf = 1e18; const int mod = 1e9 + 7; const int N = 2 * 1e5 + 10; ll res; ll a[N], dp[N][2]; vector<ll> v[N]; inline ll add(ll x, ll y) { return (x % mod + y % mod) % mod; } inline ll sub(ll x, ll y) { return (x % mod - y % mod + mod) % mod; } inline ll mul(ll x, ll y) { x %= mod; y %= mod; return 1LL * x * y % mod; } void dfs(ll u, ll par) { dp[u][0] = dp[u][1] = 1; for (auto i : v[u]) { if (i != par) { dfs(i, par); dp[u][0] = mul(dp[u][0], dp[i][1]); dp[u][1] = mul(dp[u][1], add(dp[i][0], dp[i][1])); } } } void solve() { ll n, x, y; cin >> n; for (int i = 0; i < n - 1; ++i) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } dfs(1, 1); cout << add(dp[1][0], dp[1][1]); } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ll t = 1; // cin >> t; while (t--) { solve(); } } // nEro
// KiSmAt #include "bits/stdc++.h" using namespace std; using ll = long long; const ll inf = 1e18; const int mod = 1e9 + 7; const int N = 2 * 1e5 + 10; ll res; ll a[N], dp[N][2]; vector<ll> v[N]; inline ll add(ll x, ll y) { return (x % mod + y % mod) % mod; } inline ll sub(ll x, ll y) { return (x % mod - y % mod + mod) % mod; } inline ll mul(ll x, ll y) { x %= mod; y %= mod; return 1LL * x * y % mod; } void dfs(ll u, ll par) { dp[u][0] = dp[u][1] = 1; for (auto i : v[u]) { if (i != par) { dfs(i, u); dp[u][0] = mul(dp[u][0], dp[i][1]); dp[u][1] = mul(dp[u][1], add(dp[i][0], dp[i][1])); } } } void solve() { ll n, x, y; cin >> n; for (int i = 0; i < n - 1; ++i) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } dfs(1, 1); cout << add(dp[1][0], dp[1][1]); } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ll t = 1; // cin >> t; while (t--) { solve(); } } // nEro
replace
27
28
27
28
-11
p03175
C++
Runtime Error
#include <bits/stdc++.h> #define ALL(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef vector<int> vi; ll mod = 1e9 + 7; vector<vi> memo(10123, vi(2)); vector<vi> adj(10123); void dfs(int u, int p) { if (memo[u][0]) { return; } ll black = 1, white = 1; for (int v : adj[u]) { if (v != p) { dfs(v, u); black *= memo[v][1]; black %= mod; white *= memo[v][1] + memo[v][0]; white %= mod; } } memo[u][0] = black; memo[u][1] = white; } void solve(void) { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, 0); ll ans = (memo[1][1] + memo[1][0]) % mod; cout << ans << '\n'; } int main(void) { solve(); return 0; }
#include <bits/stdc++.h> #define ALL(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef vector<int> vi; ll mod = 1e9 + 7; vector<vi> memo(100123, vi(2)); vector<vi> adj(100123); void dfs(int u, int p) { if (memo[u][0]) { return; } ll black = 1, white = 1; for (int v : adj[u]) { if (v != p) { dfs(v, u); black *= memo[v][1]; black %= mod; white *= memo[v][1] + memo[v][0]; white %= mod; } } memo[u][0] = black; memo[u][1] = white; } void solve(void) { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, 0); ll ans = (memo[1][1] + memo[1][0]) % mod; cout << ans << '\n'; } int main(void) { solve(); return 0; }
replace
11
13
11
13
0
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define popcnt(a) __builtin_popcount(a) #define FastIO() ios::sync_with_stdio(false), cin.tie(0); #define IO() freopen("palindrome.in", "rw", stdin) #define error(args...) \ { \ cerr << "LINE " << __LINE__; \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ cerr << endl; \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << ' ' << *it << " = " << a; err(++it, args...); } typedef long long LL; const int N = 1e5 + 9, M = 3e6 + 9, OO = 0x3f3f3f3f, MOD = 1e9 + 7; int n; vector<int> adj[N]; LL memo[N][2]; LL solve(int v, int c, int p) { LL &ret = memo[v][c]; error(ret, v, c, p); if (~ret) return ret; ret = 1; for (auto ch : adj[v]) { if (ch != p) { if (c == 0) ret = (ret * (solve(ch, 0, v) + solve(ch, 1, v))) % MOD; else ret = (ret * (solve(ch, 0, v))) % MOD; } } return ret % MOD; } int main() { memset(memo, -1, sizeof memo); scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } printf("%lld\n", (solve(1, 0, -1) + solve(1, 1, -1)) % MOD); #ifdef _LOCAL_DEFINE cerr << (double)clock() * 1.0 / CLOCKS_PER_SEC << endl; #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define popcnt(a) __builtin_popcount(a) #define FastIO() ios::sync_with_stdio(false), cin.tie(0); #define IO() freopen("palindrome.in", "rw", stdin) #define error(args...) \ { \ cerr << "LINE " << __LINE__; \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ cerr << endl; \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << ' ' << *it << " = " << a; err(++it, args...); } typedef long long LL; const int N = 1e5 + 9, M = 3e6 + 9, OO = 0x3f3f3f3f, MOD = 1e9 + 7; int n; vector<int> adj[N]; LL memo[N][2]; LL solve(int v, int c, int p) { LL &ret = memo[v][c]; if (~ret) return ret; ret = 1; for (auto ch : adj[v]) { if (ch != p) { if (c == 0) ret = (ret * (solve(ch, 0, v) + solve(ch, 1, v))) % MOD; else ret = (ret * (solve(ch, 0, v))) % MOD; } } return ret % MOD; } int main() { memset(memo, -1, sizeof memo); scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } printf("%lld\n", (solve(1, 0, -1) + solve(1, 1, -1)) % MOD); #ifdef _LOCAL_DEFINE cerr << (double)clock() * 1.0 / CLOCKS_PER_SEC << endl; #endif return 0; }
delete
29
30
29
29
TLE
p03175
C++
Runtime Error
#include <bits/stdc++.h> // #include <ext/numeric> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #define oo 0x3f3f3f3f #define OO 0x3f3f3f3f3f3f3f3f #define ones(n) __builtin_popcount(n) #define ONES(n) __builtin_popcountll(n) using namespace std; // using namespace __gnu_cxx; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef pair<int, int> ii; typedef pair<long long, pair<int, int>> iii; typedef pair<ii, ii> iiii; const double PI = acos(-1.0), EPS = 1e-6; const int N = 402, M = 100005, mod = 1000000007, mxLog = 20; vector<int> adj[N]; int memo[N][2]; int solve(int u, int par, int par_color) { if (adj[u].size() == 0 || adj[u].size() == 1 && par != -1) { return 1 + par_color; } int &ret = memo[u][par_color]; if (~ret) return ret; ret = 0; if (par_color) { long long x = 1; for (int v : adj[u]) { if (v == par) continue; x *= 1LL * solve(v, u, 1); x %= mod; } ret += x; x = 1; for (int v : adj[u]) { if (v == par) continue; x *= 1LL * solve(v, u, 0); x %= mod; } ret += x; ret %= mod; } else { long long x = 1; for (int v : adj[u]) { if (v == par) continue; x *= 1LL * solve(v, u, 1); x %= mod; } ret += x; } return ret; } int main() { #ifndef ONLINE_JUDGE // freopen("input.txt", "rt", stdin); // freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed; int n; cin >> n; int u, v; for (int i = 1; i < n; ++i) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } memset(memo, -1, sizeof memo); cout << solve(1, -1, 1) << '\n'; return 0; }
#include <bits/stdc++.h> // #include <ext/numeric> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #define oo 0x3f3f3f3f #define OO 0x3f3f3f3f3f3f3f3f #define ones(n) __builtin_popcount(n) #define ONES(n) __builtin_popcountll(n) using namespace std; // using namespace __gnu_cxx; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef pair<int, int> ii; typedef pair<long long, pair<int, int>> iii; typedef pair<ii, ii> iiii; const double PI = acos(-1.0), EPS = 1e-6; const int N = 100005, M = 100005, mod = 1000000007, mxLog = 20; vector<int> adj[N]; int memo[N][2]; int solve(int u, int par, int par_color) { if (adj[u].size() == 0 || adj[u].size() == 1 && par != -1) { return 1 + par_color; } int &ret = memo[u][par_color]; if (~ret) return ret; ret = 0; if (par_color) { long long x = 1; for (int v : adj[u]) { if (v == par) continue; x *= 1LL * solve(v, u, 1); x %= mod; } ret += x; x = 1; for (int v : adj[u]) { if (v == par) continue; x *= 1LL * solve(v, u, 0); x %= mod; } ret += x; ret %= mod; } else { long long x = 1; for (int v : adj[u]) { if (v == par) continue; x *= 1LL * solve(v, u, 1); x %= mod; } ret += x; } return ret; } int main() { #ifndef ONLINE_JUDGE // freopen("input.txt", "rt", stdin); // freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed; int n; cin >> n; int u, v; for (int i = 1; i < n; ++i) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } memset(memo, -1, sizeof memo); cout << solve(1, -1, 1) << '\n'; return 0; }
replace
21
22
21
22
0
p03175
C++
Runtime Error
/* Written by Nitrogens Desire for getting accepted!! */ #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<db, db> pdd; const int maxn = 1e5 + 5; const int Mod = 1000000007; const int INF = 0x3f3f3f3f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const double e = exp(1); const db PI = acos(-1); const db ERR = 1e-10; #define Se second #define Fi first #define pb push_back #define dbg(x) cout << #x << " = " << (x) << endl #define dbg2(x1, x2) \ cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl #define dbg3(x1, x2, x3) \ cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3 \ << " = " << x3 << endl int head[maxn], cnt, n; struct edge { int v, nxt; } Edge[maxn]; void init() { for (int i = 0; i <= n; i++) head[i] = -1; cnt = 0; } void addedge(int u, int v) { Edge[cnt].v = v; Edge[cnt].nxt = head[u]; head[u] = cnt++; } ll dp[maxn][2]; void dfs(int id, int fa) { dp[id][0] = dp[id][1] = 1; for (int i = head[id]; i != -1; i = Edge[i].nxt) { int v = Edge[i].v; if (v == fa) continue; dfs(v, id); dp[id][0] = (dp[id][0] * dp[v][1]) % Mod; dp[id][1] = (dp[id][1] * ((dp[v][0] + dp[v][1]) % Mod)) % Mod; } /* if(!dp[id][0]) dp[id][0] = 1; if(!dp[id][1]) dp[id][1] = 1; */ } int main() { // ios::sync_with_stdio(false); // freopen("a.txt","r",stdin); // freopen("b.txt","w",stdout); int u, v; scanf("%d", &n); init(); for (int i = 1; i <= n - 1; i++) { scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, -1); printf("%lld\n", (dp[1][0] + dp[1][1]) % Mod); // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << // endl; return 0; }
/* Written by Nitrogens Desire for getting accepted!! */ #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<db, db> pdd; const int maxn = 1e5 + 5; const int Mod = 1000000007; const int INF = 0x3f3f3f3f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const double e = exp(1); const db PI = acos(-1); const db ERR = 1e-10; #define Se second #define Fi first #define pb push_back #define dbg(x) cout << #x << " = " << (x) << endl #define dbg2(x1, x2) \ cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl #define dbg3(x1, x2, x3) \ cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3 \ << " = " << x3 << endl int head[maxn], cnt, n; struct edge { int v, nxt; } Edge[2 * maxn]; void init() { for (int i = 0; i <= n; i++) head[i] = -1; cnt = 0; } void addedge(int u, int v) { Edge[cnt].v = v; Edge[cnt].nxt = head[u]; head[u] = cnt++; } ll dp[maxn][2]; void dfs(int id, int fa) { dp[id][0] = dp[id][1] = 1; for (int i = head[id]; i != -1; i = Edge[i].nxt) { int v = Edge[i].v; if (v == fa) continue; dfs(v, id); dp[id][0] = (dp[id][0] * dp[v][1]) % Mod; dp[id][1] = (dp[id][1] * ((dp[v][0] + dp[v][1]) % Mod)) % Mod; } /* if(!dp[id][0]) dp[id][0] = 1; if(!dp[id][1]) dp[id][1] = 1; */ } int main() { // ios::sync_with_stdio(false); // freopen("a.txt","r",stdin); // freopen("b.txt","w",stdout); int u, v; scanf("%d", &n); init(); for (int i = 1; i <= n - 1; i++) { scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, -1); printf("%lld\n", (dp[1][0] + dp[1][1]) % Mod); // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << // endl; return 0; }
replace
49
50
49
50
0
p03175
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define FI freopen("in.txt", "r", stdin) #define FO freopen("out.txt", "w", stdout) #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define FOR(i, n) for (int i = 1; i <= n; i++) #define For(i, n) for (int i = 0; i < n; i++) #define ROF(i, n) for (int i = n; i >= 1; i--) #define Rof(i, n) for (int i = n - 1; i >= 0; i--) #define FORI(i, n) for (auto i : n) #define ll long long #define ull unsigned long long #define vi vector<int> #define vl vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define mk make_pair #define ff first #define ss second #define eb emplace_back #define em emplace #define pb push_back #define ppb pop_back #define All(a) a.begin(), a.end() #define memo(a, b) memset(a, b, sizeof a) #define Sort(a) sort(All(a)) #define ED(a) Sort(a), a.erase(unique(All(a)), a.end()) #define rev(a) reverse(All(a)) #define sz(a) (int)a.size() #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define maxAll(a) *max_element(All(a)) #define minAll(a) *min_element(All(a)) #define allUpper(a) transform(All(a), a.begin(), ::toupper) #define allLower(a) transform(All(a), a.begin(), ::tolower) #define endl '\n' #define nl puts("") #define ub upper_bound #define lb lower_bound #define Exp exp(1.0) #define PIE 2 * acos(0.0) #define Sin(a) sin(((a)*PIE) / 180.0) #define EPS 1e-9 #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" using namespace __gnu_pbds; template <typename T> using orderset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #include "ext/rope" using namespace __gnu_cxx; // rope <int> Rope; // int dr[] = {1, -1, 0, 0}; // 4 Direction // int dc[] = {0, 0, 1, -1}; // int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction // int dc[] = {1, -1, 0, 0, 1, -1, 1, -1}; // int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves // int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1}; #define trace1(x) cerr << #x << ": " << x << endl; #define trace2(x, y) \ cerr << #x << ": " << x << " | " << #y << ": " << y << endl; #define trace3(x, y, z) \ cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl; #define trace4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl; #define trace5(a, b, c, d, e) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define trace6(a, b, c, d, e, f) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \ << #f << ": " << f << endl; inline int setbit(int mask, int pos) { return mask |= (1 << pos); } inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); } inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); } inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); } #define popcount(mask) __builtin_popcount(mask) // count set bit #define popcountLL(mask) __builtin_popcountll(mask) // for long long inline int read() { int a; scanf("%d", &a); return a; } inline ll readLL() { ll a; scanf("%lld", &a); return a; } inline double readDD() { double a; scanf("%lf", &a); return a; } template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); } int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; } ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; } #define inf 1e17 #define mod 1000000007 static const int maxn = 1e2 + 5; static const int logn = 18; vector<int> graph[maxn]; int tnode; int tlebel; int lastNode; inline void dfs(int u = 1, int p = -1, int lebel = 1) { if (lebel > tlebel) tlebel = lebel; lastNode = u; for (int v : graph[u]) { if (v == p) continue; dfs(v, u, lebel + 1); } } ll dp[maxn][2]; bool memoize[maxn][2]; inline ll ways(int u, int p, int color) { if (sz(graph[u]) == 1 && p) return 1; ll &ret = dp[u][color]; bool &mem = memoize[u][color]; if (mem) return ret; mem = 1; ll mul = 1; for (int v : graph[u]) { if (v == p) continue; ll sum = 0; if (color == 0) { sum = (sum + ways(v, u, color)) % mod; sum = (sum + ways(v, u, color ^ 1)) % mod; } else { sum = (sum + ways(v, u, color ^ 1)) % mod; } mul = (mul * sum) % mod; } return ret = mul; } int main() { // FI; tnode = read(); FOR(i, tnode - 1) { int u = read(); int v = read(); graph[u].eb(v); graph[v].eb(u); } dfs(); trace1(tlebel); ll tways = (ways(1, 0, 0) + ways(1, 0, 1)) % mod; printf("%lld", tways); }
#include "bits/stdc++.h" using namespace std; #define FI freopen("in.txt", "r", stdin) #define FO freopen("out.txt", "w", stdout) #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define FOR(i, n) for (int i = 1; i <= n; i++) #define For(i, n) for (int i = 0; i < n; i++) #define ROF(i, n) for (int i = n; i >= 1; i--) #define Rof(i, n) for (int i = n - 1; i >= 0; i--) #define FORI(i, n) for (auto i : n) #define ll long long #define ull unsigned long long #define vi vector<int> #define vl vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define mk make_pair #define ff first #define ss second #define eb emplace_back #define em emplace #define pb push_back #define ppb pop_back #define All(a) a.begin(), a.end() #define memo(a, b) memset(a, b, sizeof a) #define Sort(a) sort(All(a)) #define ED(a) Sort(a), a.erase(unique(All(a)), a.end()) #define rev(a) reverse(All(a)) #define sz(a) (int)a.size() #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define maxAll(a) *max_element(All(a)) #define minAll(a) *min_element(All(a)) #define allUpper(a) transform(All(a), a.begin(), ::toupper) #define allLower(a) transform(All(a), a.begin(), ::tolower) #define endl '\n' #define nl puts("") #define ub upper_bound #define lb lower_bound #define Exp exp(1.0) #define PIE 2 * acos(0.0) #define Sin(a) sin(((a)*PIE) / 180.0) #define EPS 1e-9 #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" using namespace __gnu_pbds; template <typename T> using orderset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #include "ext/rope" using namespace __gnu_cxx; // rope <int> Rope; // int dr[] = {1, -1, 0, 0}; // 4 Direction // int dc[] = {0, 0, 1, -1}; // int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction // int dc[] = {1, -1, 0, 0, 1, -1, 1, -1}; // int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves // int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1}; #define trace1(x) cerr << #x << ": " << x << endl; #define trace2(x, y) \ cerr << #x << ": " << x << " | " << #y << ": " << y << endl; #define trace3(x, y, z) \ cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl; #define trace4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl; #define trace5(a, b, c, d, e) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define trace6(a, b, c, d, e, f) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \ << #f << ": " << f << endl; inline int setbit(int mask, int pos) { return mask |= (1 << pos); } inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); } inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); } inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); } #define popcount(mask) __builtin_popcount(mask) // count set bit #define popcountLL(mask) __builtin_popcountll(mask) // for long long inline int read() { int a; scanf("%d", &a); return a; } inline ll readLL() { ll a; scanf("%lld", &a); return a; } inline double readDD() { double a; scanf("%lf", &a); return a; } template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); } int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; } ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; } #define inf 1e17 #define mod 1000000007 static const int maxn = 1e5 + 5; static const int logn = 18; vector<int> graph[maxn]; int tnode; int tlebel; int lastNode; inline void dfs(int u = 1, int p = -1, int lebel = 1) { if (lebel > tlebel) tlebel = lebel; lastNode = u; for (int v : graph[u]) { if (v == p) continue; dfs(v, u, lebel + 1); } } ll dp[maxn][2]; bool memoize[maxn][2]; inline ll ways(int u, int p, int color) { if (sz(graph[u]) == 1 && p) return 1; ll &ret = dp[u][color]; bool &mem = memoize[u][color]; if (mem) return ret; mem = 1; ll mul = 1; for (int v : graph[u]) { if (v == p) continue; ll sum = 0; if (color == 0) { sum = (sum + ways(v, u, color)) % mod; sum = (sum + ways(v, u, color ^ 1)) % mod; } else { sum = (sum + ways(v, u, color ^ 1)) % mod; } mul = (mul * sum) % mod; } return ret = mul; } int main() { // FI; tnode = read(); FOR(i, tnode - 1) { int u = read(); int v = read(); graph[u].eb(v); graph[v].eb(u); } dfs(); trace1(tlebel); ll tways = (ways(1, 0, 0) + ways(1, 0, 1)) % mod; printf("%lld", tways); }
replace
130
131
130
131
0
tlebel: 3
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; pair<long long, long long> dfs(vector<vector<int>> c, int v) { long long ans0 = 1, ans1 = 1; for (int w : c[v]) { pair<long long, long long> P = dfs(c, w); ans0 *= (P.first + P.second); ans0 %= MOD; ans1 *= P.first; ans1 %= MOD; } return make_pair(ans0, ans1); } int main() { int N; cin >> N; vector<vector<int>> E(N); for (int i = 0; i < N - 1; i++) { int x, y; cin >> x >> y; x--; y--; E[x].push_back(y); E[y].push_back(x); } vector<int> p(N, -1); queue<int> Q; Q.push(0); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int w : E[v]) { if (p[w] == -1) { p[w] = v; Q.push(w); } } } vector<vector<int>> c(N); for (int i = 1; i < N; i++) { c[p[i]].push_back(i); } pair<long long, long long> P = dfs(c, 0); cout << (P.first + P.second) % MOD << endl; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; pair<long long, long long> dfs(vector<vector<int>> &c, int v) { long long ans0 = 1, ans1 = 1; for (int w : c[v]) { pair<long long, long long> P = dfs(c, w); ans0 *= (P.first + P.second); ans0 %= MOD; ans1 *= P.first; ans1 %= MOD; } return make_pair(ans0, ans1); } int main() { int N; cin >> N; vector<vector<int>> E(N); for (int i = 0; i < N - 1; i++) { int x, y; cin >> x >> y; x--; y--; E[x].push_back(y); E[y].push_back(x); } vector<int> p(N, -1); queue<int> Q; Q.push(0); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int w : E[v]) { if (p[w] == -1) { p[w] = v; Q.push(w); } } } vector<vector<int>> c(N); for (int i = 1; i < N; i++) { c[p[i]].push_back(i); } pair<long long, long long> P = dfs(c, 0); cout << (P.first + P.second) % MOD << endl; }
replace
3
4
3
4
TLE
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define MOD 1000000007 #define sz 100100 ll dp[sz][2]; inline ll mul(ll a, ll b) { a = ((a % MOD) + MOD) % MOD; b = ((b % MOD) + MOD) % MOD; return (a * b) % MOD; } inline ll add(ll a, ll b) { a = ((a % MOD) + MOD) % MOD; b = ((b % MOD) + MOD) % MOD; return (a + b) % MOD; } vector<int> adj[sz]; ll dfs(int rt, bool st, int par) { if (adj[rt].size() == 1 && adj[rt][0] == par) { if (st) return 2; return 1; } ll ans = dp[rt][st]; if (ans != -1) return ans; ans = 1; for (auto it : adj[rt]) { if (it == par) continue; ans = mul(ans, dfs(it, 1, rt)); } if (st) { ll ans2 = 1; for (auto it : adj[rt]) { if (it == par) continue; ans2 = mul(ans2, dfs(it, 0, rt)); } ans = add(ans, ans2); } return ans; } int main() { memset(dp, -1, sizeof(dp)); int n, a, b; scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { scanf("%d", &a); scanf("%d", &b); adj[a].push_back(b); adj[b].push_back(a); } cout << dfs(1, 1, -1); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define MOD 1000000007 #define sz 100100 ll dp[sz][2]; inline ll mul(ll a, ll b) { a = ((a % MOD) + MOD) % MOD; b = ((b % MOD) + MOD) % MOD; return (a * b) % MOD; } inline ll add(ll a, ll b) { a = ((a % MOD) + MOD) % MOD; b = ((b % MOD) + MOD) % MOD; return (a + b) % MOD; } vector<int> adj[sz]; ll dfs(int rt, bool st, int par) { if (adj[rt].size() == 1 && adj[rt][0] == par) { if (st) return 2; return 1; } ll &ans = dp[rt][st]; if (ans != -1) return ans; ans = 1; for (auto it : adj[rt]) { if (it == par) continue; ans = mul(ans, dfs(it, 1, rt)); } if (st) { ll ans2 = 1; for (auto it : adj[rt]) { if (it == par) continue; ans2 = mul(ans2, dfs(it, 0, rt)); } ans = add(ans, ans2); } return ans; } int main() { memset(dp, -1, sizeof(dp)); int n, a, b; scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { scanf("%d", &a); scanf("%d", &b); adj[a].push_back(b); adj[b].push_back(a); } cout << dfs(1, 1, -1); return 0; }
replace
30
31
30
31
TLE
p03175
C++
Time Limit Exceeded
#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; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define mp make_pair #define fi first #define se second #define SZ(x) ((int)(x).size()) #define lli long long int #define pii pair<int, int> #define vii vector<pii> #define pb push_back #define eb emplace_back #define em emplace #define all(v) v.begin(), v.end() #define mod 1000000007 #define vi vector<int> #define vl vector<lli> #define vll vector<pair<lli, lli>> #define pll pair<lli, lli> #define vvi vector<vector<int>> #define vvl vector<vector<lli>> #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } #define tk(args...) take(args); #define posLSB(X) __builtin_ctz(X) #define num1bit(X) __builtin_popcount(X) #define numlead0(X) __builtin_clz(X) #define umreserve(X) \ X.reserve(32768); \ X.max_load_factor(0.25); void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << "\n"; err(++it, args...); } void take() {} template <typename T, typename... Args> void take(T &a, Args &...args) { cin >> a; take(args...); } template <class T> void printvec(vector<T> &a) { for (T &x : a) { cout << x << " "; } cout << endl; } template <class T> void printarr(T a[], lli n) { for (lli i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } struct HASH { size_t operator()(const pair<int, int> &x) const { return (size_t)x.first * 37U + (size_t)x.second; } }; #define endl "\n" lli add(lli a, lli b) { a += b; if (a >= mod) a -= mod; return a; } lli mul(lli a, lli b) { return a * 1ll * b % mod; } inline bool setmin(lli &x, lli y) { return (y < x) ? x = y, 1 : 0; } inline bool setmax(lli &x, lli y) { return (y > x) ? x = y, 1 : 0; } lli power_mod(lli a, lli x) { if (x == 0) return 1; lli y = power_mod(a, x / 2); lli ans = (y * y) % mod; if (x % 2) ans = (ans * a) % mod; return ans; } lli inv(lli a) { return power_mod(a, mod - 2); } lli power(lli a, lli x) { if (x == 0) return 1; lli y = power(a, x / 2); lli ans = (y * y); if (x % 2) ans *= a; return ans; } vector<vector<int>> dp; vector<vector<int>> a; void dfs(int u, int p) { for (int color : {0, 1}) { dp[u][color] = 1; for (int v : a[u]) { if (v == p) continue; dfs(v, u); if (color) dp[u][color] = mul(dp[u][color], dp[v][color ^ 1]); else { dp[u][color] = mul(dp[u][color], add(dp[v][color], dp[v][color ^ 1])); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; a.assign(n, vector<int>()); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; a[u].pb(v); a[v].pb(u); } dp.assign(n, vector<int>(2, 0)); dfs(0, -1); cout << add(dp[0][0], dp[0][1]) << endl; }
#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; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define mp make_pair #define fi first #define se second #define SZ(x) ((int)(x).size()) #define lli long long int #define pii pair<int, int> #define vii vector<pii> #define pb push_back #define eb emplace_back #define em emplace #define all(v) v.begin(), v.end() #define mod 1000000007 #define vi vector<int> #define vl vector<lli> #define vll vector<pair<lli, lli>> #define pll pair<lli, lli> #define vvi vector<vector<int>> #define vvl vector<vector<lli>> #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } #define tk(args...) take(args); #define posLSB(X) __builtin_ctz(X) #define num1bit(X) __builtin_popcount(X) #define numlead0(X) __builtin_clz(X) #define umreserve(X) \ X.reserve(32768); \ X.max_load_factor(0.25); void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << "\n"; err(++it, args...); } void take() {} template <typename T, typename... Args> void take(T &a, Args &...args) { cin >> a; take(args...); } template <class T> void printvec(vector<T> &a) { for (T &x : a) { cout << x << " "; } cout << endl; } template <class T> void printarr(T a[], lli n) { for (lli i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } struct HASH { size_t operator()(const pair<int, int> &x) const { return (size_t)x.first * 37U + (size_t)x.second; } }; #define endl "\n" lli add(lli a, lli b) { a += b; if (a >= mod) a -= mod; return a; } lli mul(lli a, lli b) { return a * 1ll * b % mod; } inline bool setmin(lli &x, lli y) { return (y < x) ? x = y, 1 : 0; } inline bool setmax(lli &x, lli y) { return (y > x) ? x = y, 1 : 0; } lli power_mod(lli a, lli x) { if (x == 0) return 1; lli y = power_mod(a, x / 2); lli ans = (y * y) % mod; if (x % 2) ans = (ans * a) % mod; return ans; } lli inv(lli a) { return power_mod(a, mod - 2); } lli power(lli a, lli x) { if (x == 0) return 1; lli y = power(a, x / 2); lli ans = (y * y); if (x % 2) ans *= a; return ans; } vector<vector<int>> dp; vector<vector<int>> a; void dfs(int u, int p) { for (int color : {0, 1}) { dp[u][color] = 1; for (int v : a[u]) { if (v == p) continue; if (dp[v][0] == 0 || dp[v][1] == 0) dfs(v, u); if (color) dp[u][color] = mul(dp[u][color], dp[v][color ^ 1]); else { dp[u][color] = mul(dp[u][color], add(dp[v][color], dp[v][color ^ 1])); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; a.assign(n, vector<int>()); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; a[u].pb(v); a[v].pb(u); } dp.assign(n, vector<int>(2, 0)); dfs(0, -1); cout << add(dp[0][0], dp[0][1]) << endl; }
replace
110
111
110
112
TLE
p03175
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <queue> #include <stack> #include <string> #include <vector> const long long INF = 1000000007; using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<pii, pii> piiii; int n; vector<int> adj[10011]; bool visit[10011]; ll db[10011]; ll dw[10011]; ll dp(int x) { if (visit[x]) return (db[x] + dw[x]) % INF; ll ansb = 1, answ = 1; visit[x] = true; for (int v : adj[x]) { if (visit[v]) continue; answ = (answ * dp(v)) % INF; ansb = (ansb * dw[v]) % INF; } dw[x] = answ; db[x] = ansb; return (answ + ansb) % INF; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d %d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } fill(visit + 1, visit + n + 1, false); printf("%d\n", dp(1)); }
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <queue> #include <stack> #include <string> #include <vector> const long long INF = 1000000007; using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<pii, pii> piiii; int n; vector<int> adj[100011]; bool visit[100011]; ll db[100011]; ll dw[100011]; ll dp(int x) { if (visit[x]) return (db[x] + dw[x]) % INF; ll ansb = 1, answ = 1; visit[x] = true; for (int v : adj[x]) { if (visit[v]) continue; answ = (answ * dp(v)) % INF; ansb = (ansb * dw[v]) % INF; } dw[x] = answ; db[x] = ansb; return (answ + ansb) % INF; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d %d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } fill(visit + 1, visit + n + 1, false); printf("%d\n", dp(1)); }
replace
19
23
19
23
0
p03175
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 3e5 + 5; const ll M = 1e9 + 7; vector<int> v[N]; ll dp[N][2]; void dfs(int s, int p) { dp[s][0] = dp[s][1] = 1; for (auto i : v[s]) { dfs(i, s); dp[s][0] = (dp[s][0] * dp[i][1]) % M; dp[s][1] = (dp[s][1] * ((dp[i][0] + dp[i][1]) % M)) % M; } } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } dfs(1, 1); cout << (dp[1][0] + dp[1][1]) % M; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 3e5 + 5; const ll M = 1e9 + 7; vector<int> v[N]; ll dp[N][2]; void dfs(int s, int p) { dp[s][0] = dp[s][1] = 1; for (auto i : v[s]) { if (i != p) { dfs(i, s); dp[s][0] = (dp[s][0] * dp[i][1]) % M; dp[s][1] = (dp[s][1] * ((dp[i][0] + dp[i][1]) % M)) % M; } } } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } dfs(1, 1); cout << (dp[1][0] + dp[1][1]) % M; return 0; }
replace
10
14
10
15
-11
p03175
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define F first #define S second #define sz(x) (int)x.size() #define endl '\n' #define hell 1000000007 #define mem(x) memset(x, 0, sizeof(x)); #define what_is(x) cerr << #x << " is " << x << endl; #define rep(i, a, b) for (int i = a; i < b; i++) using namespace std; const int N = 1e5; vi ad[N]; bool visw[N], visb[N]; ll dpw[N], dpb[N], par[N]; ll dfsw(int x); ll dfsb(int x) { if (visb[x]) return dpb[x]; visb[x] = true; dpb[x] = 1; for (auto y : ad[x]) { if (y == par[x]) continue; par[y] = x; dpb[x] = (dpb[x] * dfsw(y)) % hell; } return dpb[x]; } ll dfsw(int x) { if (visw[x] == true) return dpw[x]; visw[x] = true; dpw[x] = 1; ll val1 = 1, val = 0; for (auto y : ad[x]) { if (y == par[x]) continue; par[y] = x; val = 0; val = (val + dfsb(y)) % hell; val = (val + dfsw(y)) % hell; val1 = (val1 * val) % hell; } dpw[x] = val1; return dpw[x]; } void solve() { int n; cin >> n; int u, v; rep(i, 0, n - 1) { cin >> u >> v; ad[u].pb(v); ad[v].pb(u); } par[1] = -1; dfsw(1); dfsb(1); cout << (dpb[1] + dpw[1]) % hell; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1, i = 1; // cin>>t; while (t--) { // cout<<"Case #"<<i++<<": "; solve(); } return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define F first #define S second #define sz(x) (int)x.size() #define endl '\n' #define hell 1000000007 #define mem(x) memset(x, 0, sizeof(x)); #define what_is(x) cerr << #x << " is " << x << endl; #define rep(i, a, b) for (int i = a; i < b; i++) using namespace std; const int N = 1e5 + 5; vi ad[N]; bool visw[N], visb[N]; ll dpw[N], dpb[N], par[N]; ll dfsw(int x); ll dfsb(int x) { if (visb[x]) return dpb[x]; visb[x] = true; dpb[x] = 1; for (auto y : ad[x]) { if (y == par[x]) continue; par[y] = x; dpb[x] = (dpb[x] * dfsw(y)) % hell; } return dpb[x]; } ll dfsw(int x) { if (visw[x] == true) return dpw[x]; visw[x] = true; dpw[x] = 1; ll val1 = 1, val = 0; for (auto y : ad[x]) { if (y == par[x]) continue; par[y] = x; val = 0; val = (val + dfsb(y)) % hell; val = (val + dfsw(y)) % hell; val1 = (val1 * val) % hell; } dpw[x] = val1; return dpw[x]; } void solve() { int n; cin >> n; int u, v; rep(i, 0, n - 1) { cin >> u >> v; ad[u].pb(v); ad[v].pb(u); } par[1] = -1; dfsw(1); dfsb(1); cout << (dpb[1] + dpw[1]) % hell; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1, i = 1; // cin>>t; while (t--) { // cout<<"Case #"<<i++<<": "; solve(); } return 0; }
replace
21
22
21
22
0
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> using namespace std; #define REP(i, m, n) for (int i = (m); i < (n); i++) #define rep(i, n) REP(i, 0, n) #define pb push_back #define all(a) a.begin(), a.end() #define rall(c) (c).rbegin(), (c).rend() #define mp make_pair #define double long double typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll inf = 1e9 + 7; const ll mod = 1e9 + 7; ll n; vector<vector<ll>> G(100010); vector<vector<ll>> dp(100010, vector<ll>(2, -1)); ll dfs(ll i, ll p, ll c) { if (dp[i][c] != -1) return dp[i][c]; ll res = 1; for (auto e : G[i]) { if (e == p) continue; ll a = dfs(e, i, 0); ll b = dfs(e, i, 1); if (c) res = res % inf * a % inf; else res = res % inf * (a + b) % inf; } return res; } int main() { cin >> n; rep(i, n - 1) { ll x, y; cin >> x >> y; x--; y--; G[x].pb(y); G[y].pb(x); } cout << (dfs(0, -1, 0) + dfs(0, -1, 1)) % inf << endl; }
#include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> using namespace std; #define REP(i, m, n) for (int i = (m); i < (n); i++) #define rep(i, n) REP(i, 0, n) #define pb push_back #define all(a) a.begin(), a.end() #define rall(c) (c).rbegin(), (c).rend() #define mp make_pair #define double long double typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll inf = 1e9 + 7; const ll mod = 1e9 + 7; ll n; vector<vector<ll>> G(100010); vector<vector<ll>> dp(100010, vector<ll>(2, -1)); ll dfs(ll i, ll p, ll c) { if (dp[i][c] != -1) return dp[i][c]; ll res = 1; for (auto e : G[i]) { if (e == p) continue; ll a = dfs(e, i, 0); ll b = dfs(e, i, 1); if (c) res = res % inf * a % inf; else res = res % inf * (a + b) % inf; } dp[i][c] = res; return res; } int main() { cin >> n; rep(i, n - 1) { ll x, y; cin >> x >> y; x--; y--; G[x].pb(y); G[y].pb(x); } cout << (dfs(0, -1, 0) + dfs(0, -1, 1)) % inf << endl; }
insert
33
33
33
34
TLE
p03175
C++
Runtime Error
#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; #define rep(a, n) for (ll a = 0; a < n; a++) #define repi(a, b, n) for (ll a = b; a < n; a++) #include <bits/stdc++.h> using namespace std; template <typename T> void cmpmax(T &reference, T value) { reference = max(reference, value); } template <typename T> void cmpmin(T &reference, T value) { reference = min(reference, value); } const ll mod = 1000000007; static const ll INF = 1e15; int n; vector<vector<int>> graph; vector<vector<ll>> dp; const int white = 0; const int black = 1; void dfs(int c, int from) { ll w = 1; ll b = 1; for (auto p : graph[c]) { if (p == from) continue; dfs(p, c); w *= (dp[p][white] + dp[p][black]); b *= dp[p][white]; w %= mod; b %= mod; } dp[c][white] = w; dp[c][black] = b; } int main() { cin >> n; graph.resize(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; graph[a].push_back(b); graph[b].push_back(a); } int leaf = -1; rep(i, n) { if (graph[i].size() == 1) { leaf = i; break; } } dp.resize(n, vector<ll>(2, 0)); dfs(leaf, -1); auto sum = dp[leaf][white] + dp[leaf][black]; sum %= mod; cout << sum << 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; #define rep(a, n) for (ll a = 0; a < n; a++) #define repi(a, b, n) for (ll a = b; a < n; a++) #include <bits/stdc++.h> using namespace std; template <typename T> void cmpmax(T &reference, T value) { reference = max(reference, value); } template <typename T> void cmpmin(T &reference, T value) { reference = min(reference, value); } const ll mod = 1000000007; static const ll INF = 1e15; int n; vector<vector<int>> graph; vector<vector<ll>> dp; const int white = 0; const int black = 1; void dfs(int c, int from) { ll w = 1; ll b = 1; for (auto p : graph[c]) { if (p == from) continue; dfs(p, c); w *= (dp[p][white] + dp[p][black]); b *= dp[p][white]; w %= mod; b %= mod; } dp[c][white] = w; dp[c][black] = b; } int main() { cin >> n; graph.resize(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; graph[a].push_back(b); graph[b].push_back(a); } int leaf = 0; rep(i, n) { if (graph[i].size() == 1) { leaf = i; break; } } dp.resize(n, vector<ll>(2, 0)); dfs(leaf, -1); auto sum = dp[leaf][white] + dp[leaf][black]; sum %= mod; cout << sum << endl; return 0; }
replace
63
64
63
64
0
p03175
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 ll int n; map<ll int, vector<ll int>> g; ll int dp[100001][2]; void dfs(ll int u = 1, ll int p = 0) { for (auto v : g[u]) { if (v == p) continue; dfs(v, u); dp[u][1] *= (dp[v][0] + dp[v][1]); dp[u][1] %= md; dp[u][0] *= (dp[v][1]); dp[u][0] %= md; } } int main() { fastio #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> n; for (ll int i = 1; i < n; i++) { ll int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } for (ll int i = 1; i <= n; i++) for (ll int j = 0; j <= 1; j++) dp[i][j] = 1; dfs(); cout << (dp[1][0] + dp[1][1]) % md << 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 ll int n; map<ll int, vector<ll int>> g; ll int dp[100001][2]; void dfs(ll int u = 1, ll int p = 0) { for (auto v : g[u]) { if (v == p) continue; dfs(v, u); dp[u][1] *= (dp[v][0] + dp[v][1]); dp[u][1] %= md; dp[u][0] *= (dp[v][1]); dp[u][0] %= md; } } int main() { // fastio // #ifndef ONLINE_JUDGE // freopen("input.txt" , "r" , stdin); // freopen("output.txt" , "w" , stdout); // #endif cin >> n; for (ll int i = 1; i < n; i++) { ll int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } for (ll int i = 1; i <= n; i++) for (ll int j = 0; j <= 1; j++) dp[i][j] = 1; dfs(); cout << (dp[1][0] + dp[1][1]) % md << endl; return 0; }
replace
33
38
33
38
0
p03175
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define _overload3(_1, _2, _3, _4, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n, s) for (int i = a; s > 0 ? i < n : i > n; i += s) #define rep(...) _overload3(__VA_ARGS__, per, repi, _rep, )(__VA_ARGS__) #define all(x) (x).begin(), (x).end() 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; } const int inf = 1e9 + 10, mod = 1e9 + 7; const long long llinf = 1e18; // head int black[10001]; int white[10001]; int visited[10001]; vector<int> G[10001]; void dfs(int node) { visited[node] = 1; long long b = 1, w = 1; for (auto v : G[node]) { if (!visited[v]) { dfs(v); b = (b * white[v]) % mod; w = (w * (white[v] + black[v]) % mod) % mod; } } black[node] = b; white[node] = w; } int main() { int n; cin >> n; rep(i, n - 1) { int x, y; cin >> x >> y; x--; y--; G[x].push_back(y); G[y].push_back(x); } dfs(0); cout << (black[0] + white[0]) % mod << endl; }
#include <bits/stdc++.h> using namespace std; #define _overload3(_1, _2, _3, _4, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n, s) for (int i = a; s > 0 ? i < n : i > n; i += s) #define rep(...) _overload3(__VA_ARGS__, per, repi, _rep, )(__VA_ARGS__) #define all(x) (x).begin(), (x).end() 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; } const int inf = 1e9 + 10, mod = 1e9 + 7; const long long llinf = 1e18; // head int black[100001]; int white[100001]; int visited[100001]; vector<int> G[100001]; void dfs(int node) { visited[node] = 1; long long b = 1, w = 1; for (auto v : G[node]) { if (!visited[v]) { dfs(v); b = (b * white[v]) % mod; w = (w * (white[v] + black[v]) % mod) % mod; } } black[node] = b; white[node] = w; } int main() { int n; cin >> n; rep(i, n - 1) { int x, y; cin >> x >> y; x--; y--; G[x].push_back(y); G[y].push_back(x); } dfs(0); cout << (black[0] + white[0]) % mod << endl; }
replace
25
29
25
29
0
p03175
C++
Runtime Error
// #pragma GCC optimize "trapv" #include <bits/stdc++.h> #define ll long long int #define fab(a, b, i) for (int i = a; i < b; i++) #define pb push_back #define db double #define mp make_pair #define endl "\n" #define f first #define se second #define all(x) x.begin(), x.end() #define MOD 1000000007 #define quick \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) using namespace std; vector<int> v[100005]; ll dp[100005][2]; void dfs(int src, int par) { // cout<<"src:"<<src<<" "<<pr<<endl; dp[src][0] = dp[src][1] = 1; for (auto i : v[src]) { if (i == par) continue; dfs(i, src); dp[src][0] = ((dp[i][1] + dp[i][0]) * dp[src][0]) % MOD; dp[src][1] = (dp[src][1] * dp[i][0]) % MOD; } // cout<<"dp:"<<dp[src][pr]<<endl; } int main() { quick; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; fab(0, n + 1, i) dp[i][0] = dp[i][1] = -1; fab(1, n, i) { int x, y; cin >> x >> y; x--; y--; v[x].pb(y); v[y].pb(x); } dfs(0, -1); cout << (dp[0][1] + dp[0][0]) % MOD << endl; return 0; }
// #pragma GCC optimize "trapv" #include <bits/stdc++.h> #define ll long long int #define fab(a, b, i) for (int i = a; i < b; i++) #define pb push_back #define db double #define mp make_pair #define endl "\n" #define f first #define se second #define all(x) x.begin(), x.end() #define MOD 1000000007 #define quick \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) using namespace std; vector<int> v[100005]; ll dp[100005][2]; void dfs(int src, int par) { // cout<<"src:"<<src<<" "<<pr<<endl; dp[src][0] = dp[src][1] = 1; for (auto i : v[src]) { if (i == par) continue; dfs(i, src); dp[src][0] = ((dp[i][1] + dp[i][0]) * dp[src][0]) % MOD; dp[src][1] = (dp[src][1] * dp[i][0]) % MOD; } // cout<<"dp:"<<dp[src][pr]<<endl; } int main() { quick; int n; cin >> n; fab(0, n + 1, i) dp[i][0] = dp[i][1] = -1; fab(1, n, i) { int x, y; cin >> x >> y; x--; y--; v[x].pb(y); v[y].pb(x); } dfs(0, -1); cout << (dp[0][1] + dp[0][0]) % MOD << endl; return 0; }
replace
39
43
39
40
-11
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double lf; typedef pair<ll, ll> ii; #define REP(i, n) for (int i = 0; i < n; i++) #define REP1(i, n) for (ll i = 1; i <= n; i++) #define RST(i, n) memset(i, n, sizeof i) #define SZ(a) (int)a.size() #define ALL(a) a.begin(), a.end() #define X first #define Y second #define mkp make_pair #define pb push_back #define eb emplace_back #define pob pop_back #ifdef cold66 #define debug(...) \ do { \ fprintf(stderr, "%s - %d (%s) = ", __PRETTY_FUNCTION__, __LINE__, \ #__VA_ARGS__); \ _do(__VA_ARGS__); \ } while (0) template <typename T> void _do(T &&_x) { cerr << _x << endl; } template <typename T, typename... S> void _do(T &&_x, S &&..._t) { cerr << _x << ", "; _do(_t...); } template <typename _a, typename _b> ostream &operator<<(ostream &_s, const pair<_a, _b> &_p) { return _s << "(" << _p.X << "," << _p.Y << ")"; } template <typename It> ostream &_OUTC(ostream &_s, It _ita, It _itb) { _s << "{"; for (It _it = _ita; _it != _itb; _it++) { _s << (_it == _ita ? "" : ",") << *_it; } _s << "}"; return _s; } template <typename _a> ostream &operator<<(ostream &_s, vector<_a> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _a> ostream &operator<<(ostream &_s, set<_a> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _a, typename _b> ostream &operator<<(ostream &_s, map<_a, _b> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _t> void pary(_t _a, _t _b) { _OUTC(cerr, _a, _b); cerr << endl; } #define IOS() #else #define debug(...) #define pary(...) #define endl '\n' #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0); #endif // cold66 //} template <class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const ll MAXn = 1e5 + 5, MAXlg = __lg(MAXn) + 2; const ll MOD = 1000000007; const ll INF = 0x3f3f3f3f; vector<ll> e[MAXn]; ll dp[MAXn][2]; ll mul(ll x, ll y) { ll ret = x * y; while (ret >= MOD) ret -= MOD; return ret; } void dfs(ll x, ll p) { dp[x][0] = dp[x][1] = 1; for (auto i : e[x]) { if (i == p) continue; dfs(i, x); dp[x][0] = mul(dp[x][0], dp[i][0] + dp[i][1]); dp[x][1] = mul(dp[x][1], dp[i][0]); } } int main() { IOS(); ll n; cin >> n; REP(i, n - 1) { ll u, v; cin >> u >> v; u--, v--; e[u].eb(v); e[v].eb(u); } dfs(0, 0); cout << (dp[0][0] + dp[0][1]) % MOD << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double lf; typedef pair<ll, ll> ii; #define REP(i, n) for (int i = 0; i < n; i++) #define REP1(i, n) for (ll i = 1; i <= n; i++) #define RST(i, n) memset(i, n, sizeof i) #define SZ(a) (int)a.size() #define ALL(a) a.begin(), a.end() #define X first #define Y second #define mkp make_pair #define pb push_back #define eb emplace_back #define pob pop_back #ifdef cold66 #define debug(...) \ do { \ fprintf(stderr, "%s - %d (%s) = ", __PRETTY_FUNCTION__, __LINE__, \ #__VA_ARGS__); \ _do(__VA_ARGS__); \ } while (0) template <typename T> void _do(T &&_x) { cerr << _x << endl; } template <typename T, typename... S> void _do(T &&_x, S &&..._t) { cerr << _x << ", "; _do(_t...); } template <typename _a, typename _b> ostream &operator<<(ostream &_s, const pair<_a, _b> &_p) { return _s << "(" << _p.X << "," << _p.Y << ")"; } template <typename It> ostream &_OUTC(ostream &_s, It _ita, It _itb) { _s << "{"; for (It _it = _ita; _it != _itb; _it++) { _s << (_it == _ita ? "" : ",") << *_it; } _s << "}"; return _s; } template <typename _a> ostream &operator<<(ostream &_s, vector<_a> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _a> ostream &operator<<(ostream &_s, set<_a> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _a, typename _b> ostream &operator<<(ostream &_s, map<_a, _b> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _t> void pary(_t _a, _t _b) { _OUTC(cerr, _a, _b); cerr << endl; } #define IOS() #else #define debug(...) #define pary(...) #define endl '\n' #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0); #endif // cold66 //} template <class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const ll MAXn = 1e5 + 5, MAXlg = __lg(MAXn) + 2; const ll MOD = 1000000007; const ll INF = 0x3f3f3f3f; vector<ll> e[MAXn]; ll dp[MAXn][2]; ll mul(ll x, ll y) { ll ret = x * y; return ret % MOD; } void dfs(ll x, ll p) { dp[x][0] = dp[x][1] = 1; for (auto i : e[x]) { if (i == p) continue; dfs(i, x); dp[x][0] = mul(dp[x][0], dp[i][0] + dp[i][1]); dp[x][1] = mul(dp[x][1], dp[i][0]); } } int main() { IOS(); ll n; cin >> n; REP(i, n - 1) { ll u, v; cin >> u >> v; u--, v--; e[u].eb(v); e[v].eb(u); } dfs(0, 0); cout << (dp[0][0] + dp[0][1]) % MOD << endl; }
replace
81
84
81
82
TLE
p03175
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #include <unordered_map> #define inputarr(a, n) \ for (ll i = 0; i < n; i++) \ cin >> a[i]; #define prllarr(a, n) \ for (ll i = 0; i < n; i++) \ cout << a[i] << " "; #define pb push_back #define ll long long #define mod 1000000007 #define foi \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(0); #define in(n) scanf("%lld", &n); #define in2(x, y) scanf("%lld %lld", &(x), &(y)); #define in3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z)); #define out(n) printf("%lld\n", n); #define out2(x, y) printf("%lld %lld\n", x, y); #define test(t) \ ll t; \ in(t); \ while (t--) #define set(arr, n, s) \ for (ll i = 0; i < n; i++) { \ arr[i] = s; \ } 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 a, ll p) { return power(a, p - 2, p); } // used with feemat little ll gcd(ll x, ll y) { if (x == 0 || y == 0) { return max(y, x); } return gcd(y % x, x); } ll gcdExtended(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } // o(log(b)) ; void prac() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } vector<ll> adj[100004]; pair<ll, ll> dfs(ll cur, ll par) { if ((adj[cur].size() == 1LL && adj[cur][0] == par) || adj[cur].size() == 0) { return {1LL, 1LL}; } ll first = 1LL; ll second = 1LL; for (ll son : adj[cur]) { if (son != par) { auto f = dfs(son, cur); first *= (f.first + f.second); first %= mod; second *= f.first; second %= mod; } } return {first, second}; } int main() { prac(); ll n; in(n) for (ll i = 0; i < n - 1; i++) { ll x, y; in2(x, y) adj[x].pb(y); adj[y].pb(x); } auto f = dfs(1, 0); out((f.first + f.second) % mod); } /*error----- convert every int to long long eg-1LL create array with proper analysis of problem constrain check mod also */
#include <bits/stdc++.h> using namespace std; #include <unordered_map> #define inputarr(a, n) \ for (ll i = 0; i < n; i++) \ cin >> a[i]; #define prllarr(a, n) \ for (ll i = 0; i < n; i++) \ cout << a[i] << " "; #define pb push_back #define ll long long #define mod 1000000007 #define foi \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(0); #define in(n) scanf("%lld", &n); #define in2(x, y) scanf("%lld %lld", &(x), &(y)); #define in3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z)); #define out(n) printf("%lld\n", n); #define out2(x, y) printf("%lld %lld\n", x, y); #define test(t) \ ll t; \ in(t); \ while (t--) #define set(arr, n, s) \ for (ll i = 0; i < n; i++) { \ arr[i] = s; \ } 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 a, ll p) { return power(a, p - 2, p); } // used with feemat little ll gcd(ll x, ll y) { if (x == 0 || y == 0) { return max(y, x); } return gcd(y % x, x); } ll gcdExtended(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } // o(log(b)) ; void prac() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } vector<ll> adj[100004]; pair<ll, ll> dfs(ll cur, ll par) { if ((adj[cur].size() == 1LL && adj[cur][0] == par) || adj[cur].size() == 0) { return {1LL, 1LL}; } ll first = 1LL; ll second = 1LL; for (ll son : adj[cur]) { if (son != par) { auto f = dfs(son, cur); first *= (f.first + f.second); first %= mod; second *= f.first; second %= mod; } } return {first, second}; } int main() { // prac(); ll n; in(n) for (ll i = 0; i < n - 1; i++) { ll x, y; in2(x, y) adj[x].pb(y); adj[y].pb(x); } auto f = dfs(1, 0); out((f.first + f.second) % mod); } /*error----- convert every int to long long eg-1LL create array with proper analysis of problem constrain check mod also */
replace
93
94
93
94
-11
p03175
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; typedef long long int lli; #define ff first #define ss second #define pb push_back #define mp make_pair #define pii pair<lli, lli> #define vi vector<lli> #define mii map<lli, lli> #define pqb priority_queue<lli> #define pqs priority_queue<lli, vi, greater<lli>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ lli x; \ cin >> x; \ while (x--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update> pbds; void SS() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } const int N = 1e5 + 5; vector<int> graph[N]; int u, v; int n; int dp[N][2]; int add(lli a, lli b) { return ((a % mod) + (b % mod)) % mod; } int mul(lli a, lli b) { return ((a % mod) * (b % mod)) % mod; } void dfs(int node, int par) { dp[node][1] = 1; dp[node][0] = 1; for (lli i : graph[node]) { if (i == par) continue; dfs(i, node); dp[node][0] = mul(dp[node][0], add(dp[i][1], dp[i][0])); dp[node][1] = mul(dp[node][1], dp[i][0]); } } void solve() { cin >> n; for (int i = 0, u, v; i < n - 1; i++) { cin >> u >> v; graph[u].pb(v); graph[v].pb(u); } dfs(1, 0); cout << add(dp[1][0], dp[1][1]) << endl; } int main() { SS(); solve(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; typedef long long int lli; #define ff first #define ss second #define pb push_back #define mp make_pair #define pii pair<lli, lli> #define vi vector<lli> #define mii map<lli, lli> #define pqb priority_queue<lli> #define pqs priority_queue<lli, vi, greater<lli>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ lli x; \ cin >> x; \ while (x--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update> pbds; void SS() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int N = 1e5 + 5; vector<int> graph[N]; int u, v; int n; int dp[N][2]; int add(lli a, lli b) { return ((a % mod) + (b % mod)) % mod; } int mul(lli a, lli b) { return ((a % mod) * (b % mod)) % mod; } void dfs(int node, int par) { dp[node][1] = 1; dp[node][0] = 1; for (lli i : graph[node]) { if (i == par) continue; dfs(i, node); dp[node][0] = mul(dp[node][0], add(dp[i][1], dp[i][0])); dp[node][1] = mul(dp[node][1], dp[i][0]); } } void solve() { cin >> n; for (int i = 0, u, v; i < n - 1; i++) { cin >> u >> v; graph[u].pb(v); graph[v].pb(u); } dfs(1, 0); cout << add(dp[1][0], dp[1][1]) << endl; } int main() { SS(); solve(); return 0; }
delete
34
38
34
34
0
p03175
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; vector<int> adj[11000]; ll dp[11000][2]; void dfs(int nd, int prv = -1) { dp[nd][0] = dp[nd][1] = 1; for (int nxt : adj[nd]) { if (nxt == prv) continue; dfs(nxt, nd); dp[nd][1] *= dp[nxt][0]; dp[nd][0] *= dp[nxt][0] + dp[nxt][1]; dp[nd][1] %= MOD; dp[nd][0] %= MOD; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } dfs(0); cout << (dp[0][0] + dp[0][1]) % MOD << '\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; vector<int> adj[110000]; ll dp[110000][2]; void dfs(int nd, int prv = -1) { dp[nd][0] = dp[nd][1] = 1; for (int nxt : adj[nd]) { if (nxt == prv) continue; dfs(nxt, nd); dp[nd][1] *= dp[nxt][0]; dp[nd][0] *= dp[nxt][0] + dp[nxt][1]; dp[nd][1] %= MOD; dp[nd][0] %= MOD; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } dfs(0); cout << (dp[0][0] + dp[0][1]) % MOD << '\n'; return 0; }
replace
7
9
7
9
0
p03175
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; const long long mod = 1000000000 + 7; vector<int> g[100000]; long long f[100000][2]; int dd[100000], d2[100000][2]; int n; long long qhd(int u, int tt) { dd[u] = 1; long long sum = 1; if (d2[u][tt] == 0) { for (auto i = g[u].begin(); i != g[u].end(); ++i) { sum = 1; if (dd[*i] == 0) { sum = qhd(*i, 0); if (tt == 0) sum += qhd(*i, 1); } f[u][tt] = f[u][tt] * sum; f[u][tt] = f[u][tt] % mod; } d2[u][tt] = 1; } dd[u] = 0; return f[u][tt]; } int main() { cin >> n; int u, v; for (int i = 0; i < n - 1; ++i) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; ++i) { f[i][0] = 1; f[i][1] = 1; } cout << (qhd(1, 0) + qhd(1, 1)) % mod; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; const long long mod = 1000000000 + 7; vector<int> g[100001]; long long f[100001][2]; int dd[100001], d2[100001][2]; int n; long long qhd(int u, int tt) { dd[u] = 1; long long sum = 1; if (d2[u][tt] == 0) { for (auto i = g[u].begin(); i != g[u].end(); ++i) { sum = 1; if (dd[*i] == 0) { sum = qhd(*i, 0); if (tt == 0) sum += qhd(*i, 1); } f[u][tt] = f[u][tt] * sum; f[u][tt] = f[u][tt] % mod; } d2[u][tt] = 1; } dd[u] = 0; return f[u][tt]; } int main() { cin >> n; int u, v; for (int i = 0; i < n - 1; ++i) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; ++i) { f[i][0] = 1; f[i][1] = 1; } cout << (qhd(1, 0) + qhd(1, 1)) % mod; }
replace
5
8
5
8
0
p03175
C++
Runtime Error
#pragma GCC optimize "trapv" #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define rep(i, a, n) for (int i = a; i < n; i++) #define rep3(n) for (int i = 0; i < n; i++) #define inarr(arr, n) rep(i, a, n) cin >> arr[i] #define ll long long int #define pb push_back #define all(v) v.begin(), v.end() #define endl "\n" #define eb emplace_back #define x first #define y second #define gcd(a, b) __gcd(a, b) #define mem1(a) memset(a, -1, sizeof(a)) #define pres(a, x) a.find(x) != a.end() #define sz(a) (int)a.size() #define pii pair<int, int> #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define ret return 0 #define pi 3.1415926536 #define hell 1000000007 #define narak 998244353 #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC const int inf1 = 1e9; const ll inf2 = 1e18; const int N = 100000; using namespace std; using namespace __gnu_pbds; #define ordered_set \ tree<pii, null_type, less<pii>, rb_tree_tag, \ tree_order_statistics_node_update> int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int dp[N + 1][2]; vector<int> g[N + 1]; void dfs(int u, int p) { dp[u][0]++; dp[u][1]++; for (auto i : g[u]) { if (i != p) { dfs(i, u); dp[u][1] = dp[i][0] * dp[u][1] % hell; dp[u][0] = (dp[u][0] * (dp[i][0] + dp[i][1])) % hell; } } } int solve() { int n; cin >> n; rep3(n - 1) { int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } dfs(1, 0); cout << (dp[1][0] + dp[1][1]) % hell << endl; ret; } int main() { IOS; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #ifdef cat cat(); #endif int t = 1; // cin>>t; while (t--) { solve(); } ret; }
#pragma GCC optimize "trapv" #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define rep(i, a, n) for (int i = a; i < n; i++) #define rep3(n) for (int i = 0; i < n; i++) #define inarr(arr, n) rep(i, a, n) cin >> arr[i] #define ll long long int #define pb push_back #define all(v) v.begin(), v.end() #define endl "\n" #define eb emplace_back #define x first #define y second #define gcd(a, b) __gcd(a, b) #define mem1(a) memset(a, -1, sizeof(a)) #define pres(a, x) a.find(x) != a.end() #define sz(a) (int)a.size() #define pii pair<int, int> #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define ret return 0 #define pi 3.1415926536 #define hell 1000000007 #define narak 998244353 #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC const int inf1 = 1e9; const ll inf2 = 1e18; const int N = 100000; using namespace std; using namespace __gnu_pbds; #define ordered_set \ tree<pii, null_type, less<pii>, rb_tree_tag, \ tree_order_statistics_node_update> int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; ll dp[N + 1][2]; vector<int> g[N + 1]; void dfs(int u, int p) { dp[u][0]++; dp[u][1]++; for (auto i : g[u]) { if (i != p) { dfs(i, u); dp[u][1] = dp[i][0] * dp[u][1] % hell; dp[u][0] = (dp[u][0] * (dp[i][0] + dp[i][1])) % hell; } } } int solve() { int n; cin >> n; rep3(n - 1) { int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } dfs(1, 0); cout << (dp[1][0] + dp[1][1]) % hell << endl; ret; } int main() { IOS; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #ifdef cat cat(); #endif int t = 1; // cin>>t; while (t--) { solve(); } ret; }
replace
37
38
37
38
0
p03175
C++
Time Limit Exceeded
#include <fstream> #include <iostream> #include <vector> #define NMAX 100005 #define MOD 1000000007 #define f cin using namespace std; // ifstream f("date.in"); vector<long long int> a[NMAX]; vector<long long int> b[NMAX]; long long int n; long long int dp[NMAX][2]; long long int putere_doi[NMAX]; long long int vis[NMAX]; void solve(long long int x) { vis[x] = 1; for (int i = 0; i < a[x].size(); ++i) { int vecin = a[x][i]; if (vis[vecin] == 0) { b[x].push_back(vecin); solve(vecin); } } } long long int adauga_suma(vector<long long int> vect, long long int &s, long long int current_index) { if (current_index == vect.size()) return -1; if (current_index == vect.size() - 1) { long long int vecin = vect[current_index]; // s = (s + ((dp[vecin][0] + dp[vecin][1]) % MOD)) % MOD; return ((dp[vecin][0] + dp[vecin][1]) % MOD); } long long int curr_ans = adauga_suma(vect, s, current_index + 1); long long int vecin = vect[current_index]; // s = (s + ((dp[vecin][0] + dp[vecin][1]) * curr_ans) % MOD ) % MOD; return (((dp[vecin][0] + dp[vecin][1]) * curr_ans) % MOD); } void calc_sum(long long int x) { /// 0 -> alb /// 1 -> negru for (long long int i = 0; i < b[x].size(); ++i) { long long int vecin = b[x][i]; calc_sum(vecin); } if (b[x].size() == 0) { dp[x][0] = 1; dp[x][1] = 1; } else { long long int sum = 1; for (long long int i = 0; i < b[x].size(); ++i) { int vecin = b[x][i]; sum = (sum * dp[vecin][0]) % MOD; } dp[x][1] = sum; long long int sum_negru = 0; long long int nr = b[x].size(); sum_negru = adauga_suma(b[x], sum_negru, 0) % MOD; dp[x][0] = sum_negru; } } int main() { f >> n; for (long long int i = 0; i < n - 1; ++i) { long long int x, y; f >> x >> y; x--; y--; a[x].push_back(y); a[y].push_back(x); } solve(0); calc_sum(0); long long int ans = (dp[0][0] + dp[0][1]) % MOD; /*for (int i = 0; i < n; ++i) { cout << dp[i][0] << " " << dp[i][1] << "\n"; }*/ cout << ans; return 0; }
#include <fstream> #include <iostream> #include <vector> #define NMAX 100005 #define MOD 1000000007 #define f cin using namespace std; // ifstream f("date.in"); vector<long long int> a[NMAX]; vector<long long int> b[NMAX]; long long int n; long long int dp[NMAX][2]; long long int putere_doi[NMAX]; long long int vis[NMAX]; void solve(long long int x) { vis[x] = 1; for (int i = 0; i < a[x].size(); ++i) { int vecin = a[x][i]; if (vis[vecin] == 0) { b[x].push_back(vecin); solve(vecin); } } } long long int adauga_suma(vector<long long int> vect, long long int &s, long long int current_index) { if (current_index == vect.size()) return -1; if (current_index == vect.size() - 1) { long long int vecin = vect[current_index]; // s = (s + ((dp[vecin][0] + dp[vecin][1]) % MOD)) % MOD; return ((dp[vecin][0] + dp[vecin][1]) % MOD); } long long int curr_ans = adauga_suma(vect, s, current_index + 1); long long int vecin = vect[current_index]; // s = (s + ((dp[vecin][0] + dp[vecin][1]) * curr_ans) % MOD ) % MOD; return (((dp[vecin][0] + dp[vecin][1]) * curr_ans) % MOD); } void calc_sum(long long int x) { /// 0 -> alb /// 1 -> negru for (long long int i = 0; i < b[x].size(); ++i) { long long int vecin = b[x][i]; calc_sum(vecin); } if (b[x].size() == 0) { dp[x][0] = 1; dp[x][1] = 1; } else { long long int sum = 1; for (long long int i = 0; i < b[x].size(); ++i) { int vecin = b[x][i]; sum = (sum * dp[vecin][0]) % MOD; } dp[x][1] = sum; long long int sum_negru = 0; long long int nr = b[x].size(); long long int curr_ans = 1; for (int i = b[x].size() - 1; i >= 0; --i) { int vecin = b[x][i]; curr_ans = (curr_ans * (dp[vecin][0] + dp[vecin][1]) % MOD) % MOD; } sum_negru = curr_ans; dp[x][0] = sum_negru; } } int main() { f >> n; for (long long int i = 0; i < n - 1; ++i) { long long int x, y; f >> x >> y; x--; y--; a[x].push_back(y); a[y].push_back(x); } solve(0); calc_sum(0); long long int ans = (dp[0][0] + dp[0][1]) % MOD; /*for (int i = 0; i < n; ++i) { cout << dp[i][0] << " " << dp[i][1] << "\n"; }*/ cout << ans; return 0; }
replace
63
64
63
69
TLE
p03175
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int maxN = 1e5, MOD = 1e9 + 7; vector<vector<int>> eg(maxN); vector<int> q; vector<ll> dpw(maxN), dpb(maxN); bool visited[maxN]; int main() { int n, x, y; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; eg[x].push_back(y); eg[y].push_back(x); } q.push_back(1); int ci = 0, cn; while (q.size() != n) { cn = q[ci]; // cout << "vertices "; for (int i = 0; i < eg[cn].size(); i++) { if (!visited[eg[cn][i]]) { q.push_back(eg[cn][i]); // cout << eg[cn][i] << ' '; } } visited[q[ci]] = true; ci++; // cout << endl << "size " << q.size() << endl; } // cout << "yyay" << endl; fill(visited, visited + n + 1, false); for (auto it = q.rbegin(); it != q.rend(); it++) { ll mw = 1, mb = 1; cn = *it; // cout << "visited is "; for(int i = 1; i <= n; i++) cout << visited[i] << // ' '; cout << endl; for (int i = 0; i < eg[cn].size(); i++) { // cout << eg[cn][i] << endl; if (visited[eg[cn][i]]) { mw = (mw * (dpw[eg[cn][i]] + dpb[eg[cn][i]]) % MOD) % MOD; mb = (mb * dpw[eg[cn][i]]) % MOD; // cout << dpw[eg[cn][i]] << ' ' << dpb[eg[cn][i]] << endl; } } dpw[cn] = mw; dpb[cn] = mb; visited[cn] = true; // cout << "current node " << cn << ' ' << dpw[cn] << ' ' << dpb[cn] << // endl; cout << "yay" << endl; } // for(int i = 1; i <= n; i++) cout << dpw[i] << ' ' << dpb[i] << endl; cout << (dpw[1] + dpb[1]) % MOD; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int maxN = 1e5 + 5, MOD = 1e9 + 7; vector<vector<int>> eg(maxN); vector<int> q; vector<ll> dpw(maxN), dpb(maxN); bool visited[maxN]; int main() { int n, x, y; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; eg[x].push_back(y); eg[y].push_back(x); } q.push_back(1); int ci = 0, cn; while (q.size() != n) { cn = q[ci]; // cout << "vertices "; for (int i = 0; i < eg[cn].size(); i++) { if (!visited[eg[cn][i]]) { q.push_back(eg[cn][i]); // cout << eg[cn][i] << ' '; } } visited[q[ci]] = true; ci++; // cout << endl << "size " << q.size() << endl; } // cout << "yyay" << endl; fill(visited, visited + n + 1, false); for (auto it = q.rbegin(); it != q.rend(); it++) { ll mw = 1, mb = 1; cn = *it; // cout << "visited is "; for(int i = 1; i <= n; i++) cout << visited[i] << // ' '; cout << endl; for (int i = 0; i < eg[cn].size(); i++) { // cout << eg[cn][i] << endl; if (visited[eg[cn][i]]) { mw = (mw * (dpw[eg[cn][i]] + dpb[eg[cn][i]]) % MOD) % MOD; mb = (mb * dpw[eg[cn][i]]) % MOD; // cout << dpw[eg[cn][i]] << ' ' << dpb[eg[cn][i]] << endl; } } dpw[cn] = mw; dpb[cn] = mb; visited[cn] = true; // cout << "current node " << cn << ' ' << dpw[cn] << ' ' << dpb[cn] << // endl; cout << "yay" << endl; } // for(int i = 1; i <= n; i++) cout << dpw[i] << ' ' << dpb[i] << endl; cout << (dpw[1] + dpb[1]) % MOD; return 0; }
replace
6
7
6
7
0
p03175
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; const long long mod = 1e9 + 7ll; vector<vector<int>> adjlist; long long dp[MAX][2]; int n; long long solve(int parent, int node, int color) { if (adjlist.size() == 1 && parent != -1) return 1ll; long long &ret = dp[node][color]; if (ret != -1) return ret; ret = 1ll; for (auto &child : adjlist[node]) { if (child == parent) continue; if (color == 1) ret = (ret * solve(node, child, 0)) % mod; else { long long ways = (solve(node, child, 0) + solve(node, child, 1)) % mod; ret = (ret * ways) % mod; } } return ret; } int main() { memset(dp, -1, sizeof(dp)); ios::sync_with_stdio(0); cin.tie(0); scanf("%d", &n); adjlist = vector<vector<int>>(n + 2); for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; --x; --y; adjlist[x].push_back(y); adjlist[y].push_back(x); } return cout << ((solve(-1, 0, 0) + solve(-1, 0, 1)) % mod) % mod << "\n", 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; const long long mod = 1e9 + 7ll; vector<vector<int>> adjlist; long long dp[MAX][2]; int n; long long solve(int parent, int node, int color) { if (adjlist.size() == 1 && parent != -1) return 1ll; long long &ret = dp[node][color]; if (ret != -1) return ret; ret = 1ll; for (auto &child : adjlist[node]) { if (child == parent) continue; if (color == 1) ret = (ret * solve(node, child, 0)) % mod; else { long long ways = (solve(node, child, 0) + solve(node, child, 1)) % mod; ret = (ret * ways) % mod; } } return ret; } int main() { memset(dp, -1, sizeof(dp)); ios::sync_with_stdio(0); cin.tie(0); cin >> n; adjlist = vector<vector<int>>(n + 2); for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; --x; --y; adjlist[x].push_back(y); adjlist[y].push_back(x); } return cout << ((solve(-1, 0, 0) + solve(-1, 0, 1)) % mod) % mod << "\n", 0; }
replace
34
35
34
35
-11
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define MOD 1000000007 #define fi(i, a, b) for (ll i = a; i < b; i++) #define fd(i, a, b) for (ll i = a; i >= b; i--) #define cfi(container, it) \ for (auto it = container.begin(); it != container.end(); it++) #define cfd(container, it) \ for (auto it = container.rbegin(); it != container.rend(); it++) #define vi vector<int> #define vll vector<ll> #define pb push_back #define pbx(num) \ ll temp; \ cin >> temp; \ num.pb(temp) using namespace std; typedef long long ll; struct modll { ll num; operator ll() { return num; } }; modll operator*(modll x, modll y) { return {(x.num * y.num) % MOD}; } modll operator+(modll x, modll y) { return {(x.num + y.num + MOD) % MOD}; } int n; const int nax = 1e5 + 5; // first for white and second for black vector<pair<ll, ll>> dp(nax, {1, 1}); vi adj[nax]; vector<bool> vis(nax); void add_self(ll &a, ll b) { a += b; if (a > MOD) a -= MOD; } void mul_self(ll &a, ll b) { a *= b; while (a > MOD) a -= MOD; } void dfs(int i) { vis[i] = true; cfi(adj[i], it) { if (!vis[*it]) { dfs(*it); ll temp = 0; add_self(temp, dp[*it].first); add_self(temp, dp[*it].second); mul_self(dp[i].first, temp); mul_self(dp[i].second, dp[*it].first); // flag = true; } } // if(!flag) { // dp[i].first = dp[i].second = 1; // } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; fi(i, 0, n - 1) { int x, y; cin >> x >> y; x--; y--; adj[x].pb(y); adj[y].pb(x); } ll ans = 0; // because its a tree and everything is connected dfs(0); add_self(ans, dp[0].first); add_self(ans, dp[0].second); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define MOD 1000000007 #define fi(i, a, b) for (ll i = a; i < b; i++) #define fd(i, a, b) for (ll i = a; i >= b; i--) #define cfi(container, it) \ for (auto it = container.begin(); it != container.end(); it++) #define cfd(container, it) \ for (auto it = container.rbegin(); it != container.rend(); it++) #define vi vector<int> #define vll vector<ll> #define pb push_back #define pbx(num) \ ll temp; \ cin >> temp; \ num.pb(temp) using namespace std; typedef long long ll; struct modll { ll num; operator ll() { return num; } }; modll operator*(modll x, modll y) { return {(x.num * y.num) % MOD}; } modll operator+(modll x, modll y) { return {(x.num + y.num + MOD) % MOD}; } int n; const int nax = 1e5 + 5; // first for white and second for black vector<pair<ll, ll>> dp(nax, {1, 1}); vi adj[nax]; vector<bool> vis(nax); void add_self(ll &a, ll b) { a += b; if (a > MOD) a -= MOD; } void mul_self(ll &a, ll b) { a *= b; a %= MOD; if (a < 0) a += MOD; } void dfs(int i) { vis[i] = true; cfi(adj[i], it) { if (!vis[*it]) { dfs(*it); ll temp = 0; add_self(temp, dp[*it].first); add_self(temp, dp[*it].second); mul_self(dp[i].first, temp); mul_self(dp[i].second, dp[*it].first); // flag = true; } } // if(!flag) { // dp[i].first = dp[i].second = 1; // } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; fi(i, 0, n - 1) { int x, y; cin >> x >> y; x--; y--; adj[x].pb(y); adj[y].pb(x); } ll ans = 0; // because its a tree and everything is connected dfs(0); add_self(ans, dp[0].first); add_self(ans, dp[0].second); cout << ans << '\n'; return 0; }
replace
41
43
41
44
TLE
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define mk make_pair #define fi first #define se second #define vll vector<ll> #define pii pair<ll, ll> #define vvll vector<vector<ll>> #define pb push_back #define sz(v) (v).size() #define inf 1e18 #define md 1000000007 #define all(v) (v).begin(), (v).end() #define rep(i, a, b) for (ll i = a; i < b; ++i) #define tel(a) \ { cout << a << "\n"; } #define tell(a, b) \ { cout << a << " | " << b << "\n"; } #define telll(a, b, c) \ { cout << a << " | " << b << " | " << c << "\n"; } #define teln(v, n) \ { \ cout << "v- "; \ rep(i, 0, n) cout << v[i] << " "; \ cout << "\n"; \ } #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define tihs \ if (fopen("inp.txt", "r")) \ freopen("inp.txt", "r", stdin), freopen("out.txt", "w", stdout); using namespace std; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif #define M 100010 ll dp[M][2]; ll n; vll g[M]; ll dfs(ll u, ll par, ll col) { ll ans = 1; for (auto v : g[u]) { if (v == par) continue; if (col) ans = (ans * (dfs(v, u, 0) + dfs(v, u, 1))) % md; else ans = (ans * dfs(v, u, 1)) % md; } return dp[u][col] = ans; } int main() { IOS; tihs; memset(dp, -1, sizeof(dp)); cin >> n; rep(i, 0, n - 1) { ll u, v; cin >> u >> v; u--, v--; g[u].pb(v); g[v].pb(u); } ll ans = dfs(0, -1, 0) + dfs(0, -1, 1); ans %= md; cout << ans; return 0; }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define mk make_pair #define fi first #define se second #define vll vector<ll> #define pii pair<ll, ll> #define vvll vector<vector<ll>> #define pb push_back #define sz(v) (v).size() #define inf 1e18 #define md 1000000007 #define all(v) (v).begin(), (v).end() #define rep(i, a, b) for (ll i = a; i < b; ++i) #define tel(a) \ { cout << a << "\n"; } #define tell(a, b) \ { cout << a << " | " << b << "\n"; } #define telll(a, b, c) \ { cout << a << " | " << b << " | " << c << "\n"; } #define teln(v, n) \ { \ cout << "v- "; \ rep(i, 0, n) cout << v[i] << " "; \ cout << "\n"; \ } #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define tihs \ if (fopen("inp.txt", "r")) \ freopen("inp.txt", "r", stdin), freopen("out.txt", "w", stdout); using namespace std; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif #define M 100010 ll dp[M][2]; ll n; vll g[M]; ll dfs(ll u, ll par, ll col) { if (dp[u][col] != -1) return dp[u][col]; ll ans = 1; for (auto v : g[u]) { if (v == par) continue; if (col) ans = (ans * (dfs(v, u, 0) + dfs(v, u, 1))) % md; else ans = (ans * dfs(v, u, 1)) % md; } return dp[u][col] = ans; } int main() { IOS; tihs; memset(dp, -1, sizeof(dp)); cin >> n; rep(i, 0, n - 1) { ll u, v; cin >> u >> v; u--, v--; g[u].pb(v); g[v].pb(u); } ll ans = dfs(0, -1, 0) + dfs(0, -1, 1); ans %= md; cout << ans; return 0; }
insert
60
60
60
62
TLE
p03175
C++
Runtime Error
#pragma gcc optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; vector<vector<int>> G(n + 1); for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; G[x].emplace_back(y); G[y].emplace_back(x); } vector<vector<ll>> dp(n + 1, vector<ll>(n + 1)); function<void(int, int)> dfs = [&](int u, int p) { dp[u][0] = dp[u][1] = 1; for (int &v : G[u]) { if (v == p) continue; dfs(v, u); (dp[u][0] *= (dp[v][0] + dp[v][1]) % MOD) %= MOD; (dp[u][1] *= dp[v][0]) %= MOD; } }; dfs(1, 0); cout << (dp[1][0] + dp[1][1]) % MOD << '\n'; }
#pragma gcc optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; vector<vector<int>> G(n + 1); for (int i = 0; i < n - 1; ++i) { int x, y; cin >> x >> y; G[x].emplace_back(y); G[y].emplace_back(x); } vector<vector<ll>> dp(n + 1, vector<ll>(2)); function<void(int, int)> dfs = [&](int u, int p) { dp[u][0] = dp[u][1] = 1; for (int &v : G[u]) { if (v == p) continue; dfs(v, u); (dp[u][0] *= (dp[v][0] + dp[v][1]) % MOD) %= MOD; (dp[u][1] *= dp[v][0]) %= MOD; } }; dfs(1, 0); cout << (dp[1][0] + dp[1][1]) % MOD << '\n'; }
replace
17
18
17
18
0
p03175
C++
Runtime Error
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long llt; const int MaxN = 100000 + 5, MaxM = 100000 + 5; const llt Mod = 1e9 + 7; int N; int cnte; int Head[MaxN], To[MaxM], Next[MaxM]; int fa[MaxN]; llt dp[MaxN][2]; inline void add_edge(int from, int to) { cnte++; To[cnte] = to; Next[cnte] = Head[from]; Head[from] = cnte; } void init() { scanf("%d", &N); for (int i = 1; i < N; ++i) { int u, v; scanf("%d %d", &u, &v); add_edge(u, v); add_edge(v, u); } } void dfs(int u) { dp[u][0] = dp[u][1] = 1; for (int i = Head[u]; i; i = Next[i]) { int v = To[i]; if (v == fa[u]) continue; fa[v] = u; dfs(v); dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % Mod; dp[u][1] = dp[u][1] * dp[v][0] % Mod; } } void solve() { dfs(1); llt ans = (dp[1][0] + dp[1][1]) % Mod; cout << ans << endl; } int main() { init(); solve(); return 0; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long llt; const int MaxN = 100000 + 5, MaxM = 200000 + 5; const llt Mod = 1e9 + 7; int N; int cnte; int Head[MaxN], To[MaxM], Next[MaxM]; int fa[MaxN]; llt dp[MaxN][2]; inline void add_edge(int from, int to) { cnte++; To[cnte] = to; Next[cnte] = Head[from]; Head[from] = cnte; } void init() { scanf("%d", &N); for (int i = 1; i < N; ++i) { int u, v; scanf("%d %d", &u, &v); add_edge(u, v); add_edge(v, u); } } void dfs(int u) { dp[u][0] = dp[u][1] = 1; for (int i = Head[u]; i; i = Next[i]) { int v = To[i]; if (v == fa[u]) continue; fa[v] = u; dfs(v); dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % Mod; dp[u][1] = dp[u][1] * dp[v][0] % Mod; } } void solve() { dfs(1); llt ans = (dp[1][0] + dp[1][1]) % Mod; cout << ans << endl; } int main() { init(); solve(); return 0; }
replace
7
8
7
8
0
p03175
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif class mint { public: long long x; constexpr mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} constexpr mint operator-() const { return mint(-x); } constexpr mint &operator+=(const mint &rhs) { if ((x += rhs.x) >= MOD) x -= MOD; return *this; } constexpr mint &operator-=(const mint &rhs) { if ((x += MOD - rhs.x) >= MOD) x -= MOD; return *this; } constexpr mint &operator*=(const mint &rhs) { (x *= rhs.x) %= MOD; return *this; } constexpr mint operator+(const mint &rhs) const { mint res(*this); return res += rhs; } constexpr mint operator-(const mint &rhs) const { mint res(*this); return res -= rhs; } constexpr mint operator*(const mint &rhs) const { mint res(*this); return res *= rhs; } [[nodiscard]] constexpr mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } [[nodiscard]] constexpr mint inv() const { return pow(MOD - 2); } mint &operator/=(mint rhs) { return *this *= rhs.inv(); } mint operator/(const mint rhs) const { return mint(*this) /= rhs; } constexpr bool operator==(const mint &rhs) const noexcept { return this->x == rhs.x; } constexpr bool operator!=(const mint &rhs) const noexcept { return this->x != rhs.x; } bool operator<(const mint &rhs) const noexcept { return this->x < rhs.x; } bool operator>(const mint &rhs) const noexcept { return this->x > rhs.x; } bool operator<=(const mint &rhs) const noexcept { return this->x <= rhs.x; } bool operator>=(const mint &rhs) const noexcept { return this->x >= rhs.x; } friend istream &operator>>(istream &is, mint &a) { long long t; is >> t; a = mint(t); return is; } friend ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } }; ll N; Graph graph(N); vector<vector<mint>> dp; vector<vector<bool>> dp2; mint dfs(ll u, ll prev, ll c) { if (dp2[u][c]) { return dp[u][c]; } mint res = 1; for (ll v : graph[u]) { if (v != prev) { mint cnt = 0; if (c == 0) { cnt += dfs(v, u, 1); cnt += dfs(v, u, 0); } else { cnt += dfs(v, u, 0); } res *= cnt; } } return dp[u][c] = res; } void solve() { cin >> N; graph.resize(N); dp.resize(N, vector<mint>(2, 0)); dp2.resize(N, vector<bool>(2, false)); rep(i, N - 1) { ll x, y; cin >> x >> y; x--; y--; graph[x].push_back(y); graph[y].push_back(x); } mint ans = dfs(0, -1, 0); ans += dfs(0, -1, 1); cout << ans << '\n'; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif class mint { public: long long x; constexpr mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} constexpr mint operator-() const { return mint(-x); } constexpr mint &operator+=(const mint &rhs) { if ((x += rhs.x) >= MOD) x -= MOD; return *this; } constexpr mint &operator-=(const mint &rhs) { if ((x += MOD - rhs.x) >= MOD) x -= MOD; return *this; } constexpr mint &operator*=(const mint &rhs) { (x *= rhs.x) %= MOD; return *this; } constexpr mint operator+(const mint &rhs) const { mint res(*this); return res += rhs; } constexpr mint operator-(const mint &rhs) const { mint res(*this); return res -= rhs; } constexpr mint operator*(const mint &rhs) const { mint res(*this); return res *= rhs; } [[nodiscard]] constexpr mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } [[nodiscard]] constexpr mint inv() const { return pow(MOD - 2); } mint &operator/=(mint rhs) { return *this *= rhs.inv(); } mint operator/(const mint rhs) const { return mint(*this) /= rhs; } constexpr bool operator==(const mint &rhs) const noexcept { return this->x == rhs.x; } constexpr bool operator!=(const mint &rhs) const noexcept { return this->x != rhs.x; } bool operator<(const mint &rhs) const noexcept { return this->x < rhs.x; } bool operator>(const mint &rhs) const noexcept { return this->x > rhs.x; } bool operator<=(const mint &rhs) const noexcept { return this->x <= rhs.x; } bool operator>=(const mint &rhs) const noexcept { return this->x >= rhs.x; } friend istream &operator>>(istream &is, mint &a) { long long t; is >> t; a = mint(t); return is; } friend ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } }; ll N; Graph graph(N); vector<vector<mint>> dp; vector<vector<bool>> dp2; mint dfs(ll u, ll prev, ll c) { if (dp2[u][c]) { return dp[u][c]; } mint res = 1; for (ll v : graph[u]) { if (v != prev) { mint cnt = 0; if (c == 0) { cnt += dfs(v, u, 1); cnt += dfs(v, u, 0); } else { cnt += dfs(v, u, 0); } res *= cnt; } } dp2[u][c] = true; return dp[u][c] = res; } void solve() { cin >> N; graph.resize(N); dp.resize(N, vector<mint>(2, 0)); dp2.resize(N, vector<bool>(2, false)); rep(i, N - 1) { ll x, y; cin >> x >> y; x--; y--; graph[x].push_back(y); graph[y].push_back(x); } mint ans = dfs(0, -1, 0); ans += dfs(0, -1, 1); cout << ans << '\n'; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); return 0; }
insert
130
130
130
131
TLE
p03175
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n; int dp[100100][2]; int head[100100], nxt[100100], to[100100]; int edgeCount; int md = 1e9 + 7; void addEdge(int x, int y) { to[edgeCount] = y; nxt[edgeCount] = head[x]; head[x] = edgeCount; ++edgeCount; } int f(int u, int p, bool blockBlack) { int &ret = dp[u][blockBlack]; if (~ret) return ret; ll ret1 = 1; ll ret2 = !blockBlack; for (int v = head[u]; ~v; v = nxt[v]) if (to[v] != p) { ret1 *= f(to[v], u, 0); if (ret1 >= md) ret1 %= md; if (!blockBlack) { ret2 *= f(to[v], u, 1); if (ret2 >= md) ret2 %= md; } } ret1 += ret2; if (ret1 >= md) ret1 -= md; return ret = ret1; } int main() { memset(head, -1, sizeof head); memset(dp, -1, sizeof dp); scanf("%d", &n); for (int i = 1, x, y; i < n; ++i) { scanf("%d%d", &x, &y); --x; --y; addEdge(x, y); addEdge(y, x); } printf("%d\n", f(0, 0, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n; int dp[100100][2]; int head[200100], nxt[200100], to[200100]; int edgeCount; int md = 1e9 + 7; void addEdge(int x, int y) { to[edgeCount] = y; nxt[edgeCount] = head[x]; head[x] = edgeCount; ++edgeCount; } int f(int u, int p, bool blockBlack) { int &ret = dp[u][blockBlack]; if (~ret) return ret; ll ret1 = 1; ll ret2 = !blockBlack; for (int v = head[u]; ~v; v = nxt[v]) if (to[v] != p) { ret1 *= f(to[v], u, 0); if (ret1 >= md) ret1 %= md; if (!blockBlack) { ret2 *= f(to[v], u, 1); if (ret2 >= md) ret2 %= md; } } ret1 += ret2; if (ret1 >= md) ret1 -= md; return ret = ret1; } int main() { memset(head, -1, sizeof head); memset(dp, -1, sizeof dp); scanf("%d", &n); for (int i = 1, x, y; i < n; ++i) { scanf("%d%d", &x, &y); --x; --y; addEdge(x, y); addEdge(y, x); } printf("%d\n", f(0, 0, 0)); return 0; }
replace
7
8
7
8
0
p03175
C++
Runtime Error
#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[100100][2]; vll G[100100]; ll dfs(ll v, ll p) { for (auto nv : G[v]) { if (nv == p) continue; dfs(nv, v); dp[v][0] = dp[v][0] * (dp[nv][0] + dp[nv][1]) % MOD; dp[v][1] = dp[v][1] * dp[nv][0] % MOD; } } signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; rep(i, n - 1) { ll a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } rep(i, n) rep(j, 2) dp[i][j] = 1; dfs(0, -1); ll ans = (dp[0][1] + dp[0][0]) % MOD; 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[100100][2]; vll G[100100]; void dfs(ll v, ll p) { for (auto nv : G[v]) { if (nv == p) continue; dfs(nv, v); dp[v][0] = dp[v][0] * (dp[nv][0] + dp[nv][1]) % MOD; dp[v][1] = dp[v][1] * dp[nv][0] % MOD; } } signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; rep(i, n - 1) { ll a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } rep(i, n) rep(j, 2) dp[i][j] = 1; dfs(0, -1); ll ans = (dp[0][1] + dp[0][0]) % MOD; cout << ans << endl; }
replace
59
61
59
60
0
p03175
C++
Time Limit Exceeded
#pragma region template 1.1 #pragma region def #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vi; typedef pair<ll, ll> ii; #define REP(i, n) for (ll i = 0; i < (n); ++i) #define REP1(i, n) for (ll i = 1; i <= (n); ++i) #define OUT(x) cout << (x) << endl; #define OUTA(a) \ REP(i, (a).size()) { cout << (a[i]) << (i == (a).size() - 1 ? "\n" : " "); } #define ALL(a) (a).begin(), (a).end() #define SORT(a) sort(ALL(a)) #define RSORT(a) \ SORT(a); \ reverse(ALL(a)) const ll INF = 1e18; const ll MOD = 1e9 + 7; #pragma endregion enddef #pragma region mint 1.0 struct mint { ll a; mint(ll x = 0) : a((x % MOD + MOD) % MOD) {} mint pow(ll rhs) { ll exp = rhs; mint res = mint(1); mint p = mint(a); while (exp) { if (exp & 1) { res *= p; } exp >>= 1; p *= p; } return res; } mint pow(mint rhs) { return pow(rhs.a); } mint &operator+=(ll rhs) { a += rhs; if (a >= MOD) { a -= MOD; } return *this; } mint &operator+=(mint rhs) { *this += rhs.a; return *this; } mint &operator-=(ll rhs) { if (a < rhs) { a += MOD; } a -= rhs; return *this; } mint &operator-=(mint rhs) { *this -= rhs.a; return *this; } mint &operator*=(ll rhs) { if (rhs < 0) { rhs += MOD; } a = a * rhs % MOD; return *this; } mint &operator*=(mint rhs) { *this *= rhs.a; return *this; } mint &operator/=(ll rhs) { *this *= mint(rhs).pow(MOD - 2); return *this; } mint &operator/=(const mint rhs) { *this /= rhs.a; return *this; } mint &operator++() { *this += 1; return *this; } mint &operator++(int) { *this += 1; return *this; } mint &operator--() { *this -= 1; return *this; } mint &operator--(int) { *this -= 1; return *this; } }; mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs.a) += rhs; } mint operator+(const mint &lhs, const ll &rhs) { return mint(lhs.a) += rhs; } mint operator+(const ll &lhs, mint &rhs) { return mint(lhs) += rhs; } mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs.a) -= rhs; } mint operator-(const mint &lhs, const ll &rhs) { return mint(lhs.a) -= rhs; } mint operator-(const ll &lhs, const mint &rhs) { return mint(lhs) -= rhs; } mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs.a) *= rhs; } mint operator*(const mint &lhs, const ll &rhs) { return mint(lhs.a) *= rhs; } mint operator*(const ll &lhs, const mint &rhs) { return mint(lhs) *= rhs; } mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs.a) /= rhs; } mint operator/(const mint &lhs, const ll &rhs) { return mint(lhs.a) /= rhs; } mint operator/(const ll &lhs, const mint &rhs) { return mint(lhs) /= rhs; } istream &operator>>(istream &is, mint &i) { is >> i.a; return is; } ostream &operator<<(ostream &os, const mint &i) { os << i.a; return os; } #pragma endregion mint vector<vi> G; vector<mint> dp; vector<bool> memo; mint f(ll i, ll p) { if (memo[i]) { return dp[i]; } mint w = 1; mint b = 1; REP(k, G[i].size()) { ll j = G[i][k]; if (j != p) { w *= f(j, i); REP(k2, G[j].size()) { ll j2 = G[j][k2]; if (j2 != i) { b *= f(j2, j); } } } } dp[i] = w + b; return dp[i]; } int main() { ll N; cin >> N; vi x(N - 1), y(N - 1); REP(i, N - 1) { cin >> x[i] >> y[i]; } G = vector<vi>(N + 1); REP(i, N - 1) { G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } dp = vector<mint>(N + 1); memo = vector<bool>(N + 1); mint ans = f(1, 0); OUT(ans); } #pragma endregion template
#pragma region template 1.1 #pragma region def #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vi; typedef pair<ll, ll> ii; #define REP(i, n) for (ll i = 0; i < (n); ++i) #define REP1(i, n) for (ll i = 1; i <= (n); ++i) #define OUT(x) cout << (x) << endl; #define OUTA(a) \ REP(i, (a).size()) { cout << (a[i]) << (i == (a).size() - 1 ? "\n" : " "); } #define ALL(a) (a).begin(), (a).end() #define SORT(a) sort(ALL(a)) #define RSORT(a) \ SORT(a); \ reverse(ALL(a)) const ll INF = 1e18; const ll MOD = 1e9 + 7; #pragma endregion enddef #pragma region mint 1.0 struct mint { ll a; mint(ll x = 0) : a((x % MOD + MOD) % MOD) {} mint pow(ll rhs) { ll exp = rhs; mint res = mint(1); mint p = mint(a); while (exp) { if (exp & 1) { res *= p; } exp >>= 1; p *= p; } return res; } mint pow(mint rhs) { return pow(rhs.a); } mint &operator+=(ll rhs) { a += rhs; if (a >= MOD) { a -= MOD; } return *this; } mint &operator+=(mint rhs) { *this += rhs.a; return *this; } mint &operator-=(ll rhs) { if (a < rhs) { a += MOD; } a -= rhs; return *this; } mint &operator-=(mint rhs) { *this -= rhs.a; return *this; } mint &operator*=(ll rhs) { if (rhs < 0) { rhs += MOD; } a = a * rhs % MOD; return *this; } mint &operator*=(mint rhs) { *this *= rhs.a; return *this; } mint &operator/=(ll rhs) { *this *= mint(rhs).pow(MOD - 2); return *this; } mint &operator/=(const mint rhs) { *this /= rhs.a; return *this; } mint &operator++() { *this += 1; return *this; } mint &operator++(int) { *this += 1; return *this; } mint &operator--() { *this -= 1; return *this; } mint &operator--(int) { *this -= 1; return *this; } }; mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs.a) += rhs; } mint operator+(const mint &lhs, const ll &rhs) { return mint(lhs.a) += rhs; } mint operator+(const ll &lhs, mint &rhs) { return mint(lhs) += rhs; } mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs.a) -= rhs; } mint operator-(const mint &lhs, const ll &rhs) { return mint(lhs.a) -= rhs; } mint operator-(const ll &lhs, const mint &rhs) { return mint(lhs) -= rhs; } mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs.a) *= rhs; } mint operator*(const mint &lhs, const ll &rhs) { return mint(lhs.a) *= rhs; } mint operator*(const ll &lhs, const mint &rhs) { return mint(lhs) *= rhs; } mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs.a) /= rhs; } mint operator/(const mint &lhs, const ll &rhs) { return mint(lhs.a) /= rhs; } mint operator/(const ll &lhs, const mint &rhs) { return mint(lhs) /= rhs; } istream &operator>>(istream &is, mint &i) { is >> i.a; return is; } ostream &operator<<(ostream &os, const mint &i) { os << i.a; return os; } #pragma endregion mint vector<vi> G; vector<mint> dp; vector<bool> memo; mint f(ll i, ll p) { if (memo[i]) { return dp[i]; } mint w = 1; mint b = 1; REP(k, G[i].size()) { ll j = G[i][k]; if (j != p) { w *= f(j, i); REP(k2, G[j].size()) { ll j2 = G[j][k2]; if (j2 != i) { b *= f(j2, j); } } } } dp[i] = w + b; memo[i] = true; return dp[i]; } int main() { ll N; cin >> N; vi x(N - 1), y(N - 1); REP(i, N - 1) { cin >> x[i] >> y[i]; } G = vector<vi>(N + 1); REP(i, N - 1) { G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } dp = vector<mint>(N + 1); memo = vector<bool>(N + 1); mint ans = f(1, 0); OUT(ans); } #pragma endregion template
insert
145
145
145
146
TLE
p03175
C++
Runtime Error
#include <cmath> #include <iomanip> #include <iostream> #include <vector> using namespace std; typedef long long ll; #define INF 1000000000000009LL #define MOD 1000000007LL ll dp[100005][2]; vector<vector<int>> v(10000); ll dfs(int node, int prev_color, int p) { if (dp[node][prev_color] != -1) return dp[node][prev_color]; ll white = 1LL, black = 1LL - ll(prev_color); for (int i = 0; i < v[node].size(); i++) { if (v[node][i] != p) { if (prev_color == 0) { white = (white * dfs(v[node][i], 0, node)) % MOD; black = (black * dfs(v[node][i], 1, node)) % MOD; } else { white = (white * dfs(v[node][i], 0, node)) % MOD; black = 0LL; } } } return dp[node][prev_color] = (white + black) % MOD; } int main() { int N; cin >> N; for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; v[a - 1].push_back(b - 1); v[b - 1].push_back(a - 1); } for (int i = 0; i < N; i++) { dp[i][0] = dp[i][1] = -1; } cout << dfs(0, 0, -1) << endl; return 0; }
#include <cmath> #include <iomanip> #include <iostream> #include <vector> using namespace std; typedef long long ll; #define INF 1000000000000009LL #define MOD 1000000007LL ll dp[100005][2]; vector<vector<int>> v(100000); ll dfs(int node, int prev_color, int p) { if (dp[node][prev_color] != -1) return dp[node][prev_color]; ll white = 1LL, black = 1LL - ll(prev_color); for (int i = 0; i < v[node].size(); i++) { if (v[node][i] != p) { if (prev_color == 0) { white = (white * dfs(v[node][i], 0, node)) % MOD; black = (black * dfs(v[node][i], 1, node)) % MOD; } else { white = (white * dfs(v[node][i], 0, node)) % MOD; black = 0LL; } } } return dp[node][prev_color] = (white + black) % MOD; } int main() { int N; cin >> N; for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; v[a - 1].push_back(b - 1); v[b - 1].push_back(a - 1); } for (int i = 0; i < N; i++) { dp[i][0] = dp[i][1] = -1; } cout << dfs(0, 0, -1) << endl; return 0; }
replace
13
14
13
14
0
p03175
C++
Time Limit Exceeded
#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; vector<vector<int>> G; mint dp[100010][2]; bool vis[100010][2]; mint Solve(int curr, int prev = -1, int prevColor = -1) { if (prevColor >= 0 && vis[curr][prevColor]) return dp[curr][prevColor]; mint res = 0; rep(color, 0, 2) { if (prevColor == 1 && color == 1) continue; mint tmp = 1; for (const int next : G[curr]) { if (next == prev) continue; tmp *= Solve(next, curr, color); } res += tmp; } if (prevColor >= 0) dp[curr][prevColor] = res; return res; } void _main() { cin >> N; G.resize(N); rep(i, 0, N - 1) { int x, y; cin >> x >> y; --x; --y; G[x].push_back(y); G[y].push_back(x); } cout << Solve(0) << 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; vector<vector<int>> G; mint dp[100010][2]; bool vis[100010][2]; mint Solve(int curr, int prev = -1, int prevColor = -1) { if (prevColor >= 0 && vis[curr][prevColor]) return dp[curr][prevColor]; if (prevColor >= 0) vis[curr][prevColor] = true; mint res = 0; rep(color, 0, 2) { if (prevColor == 1 && color == 1) continue; mint tmp = 1; for (const int next : G[curr]) { if (next == prev) continue; tmp *= Solve(next, curr, color); } res += tmp; } if (prevColor >= 0) dp[curr][prevColor] = res; return res; } void _main() { cin >> N; G.resize(N); rep(i, 0, N - 1) { int x, y; cin >> x >> y; --x; --y; G[x].push_back(y); G[y].push_back(x); } cout << Solve(0) << endl; }
insert
102
102
102
105
TLE
p03175
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #define int long long using namespace std; vector<int> tree[100001]; int dp[100001][2]; int solve(int u, int constraint, int pr) { if (dp[u][constraint] != -1) { return dp[u][constraint]; } int ans = 0; int w1 = 1; for (int child : tree[u]) { if (child != pr) { w1 = (w1 * solve(child, 0, u)) % 1000000007; } } ans = ans + w1; if (!constraint) { int w2 = 1; for (int child : tree[u]) { if (child != pr) { w2 = (w2 * solve(child, 1, u)) % 1000000007; } } ans = (ans + w2) % 1000000007; } return dp[u][constraint] = ans; } int32_t main() { int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; tree[u].push_back(v); tree[v].push_back(u); } memset(dp, -1, sizeof dp); cout << solve(1, 0, -1); }
#include <bits/stdc++.h> #include <iostream> #define int long long using namespace std; vector<int> tree[100001]; int dp[100001][2]; int solve(int u, int constraint, int pr) { if (dp[u][constraint] != -1) { return dp[u][constraint]; } int ans = 0; int w1 = 1; for (int child : tree[u]) { if (child != pr) { w1 = (w1 * solve(child, 0, u)) % 1000000007; } } ans = ans + w1; if (!constraint) { int w2 = 1; for (int child : tree[u]) { if (child != pr) { w2 = (w2 * solve(child, 1, u)) % 1000000007; } } ans = (ans + w2) % 1000000007; } return dp[u][constraint] = ans; } int32_t main() { int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; tree[u].push_back(v); tree[v].push_back(u); } memset(dp, -1, sizeof dp); cout << solve(1, 0, -1); }
insert
36
36
36
37
-11
p03176
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define taskname "TEST" #define pb push_back typedef long double ld; typedef long long ll; const int maxn = 1e5 + 5; const int mod = 1e9 + 7; int n; ll dp[maxn], a[maxn], h[maxn]; void update(int x, ll val) { for (; x <= n; x += x & -x) dp[x] = max(dp[x], val); } ll query(int x) { ll res = 0; for (; x > 0; x &= x - 1) res = max(res, dp[x]); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (fopen(taskname ".INP", "r")) freopen(taskname ".INP", "r", stdin), freopen(taskname ".OUT", "w", stdout); cin >> n; for (int i = 1; i <= n; ++i) cin >> h[i]; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { update(h[i], query(h[i] - 1) + a[i]); } cout << query(n); }
#include <bits/stdc++.h> using namespace std; #define taskname "TEST" #define pb push_back typedef long double ld; typedef long long ll; const int maxn = 2e5 + 5; const int mod = 1e9 + 7; int n; ll dp[maxn], a[maxn], h[maxn]; void update(int x, ll val) { for (; x <= n; x += x & -x) dp[x] = max(dp[x], val); } ll query(int x) { ll res = 0; for (; x > 0; x &= x - 1) res = max(res, dp[x]); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (fopen(taskname ".INP", "r")) freopen(taskname ".INP", "r", stdin), freopen(taskname ".OUT", "w", stdout); cin >> n; for (int i = 1; i <= n; ++i) cin >> h[i]; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { update(h[i], query(h[i] - 1) + a[i]); } cout << query(n); }
replace
7
8
7
8
0
p03176
C++
Runtime Error
/*input 5 1 2 3 4 5 1000000000 1000000000 1000000000 1000000000 1000000000 */ #include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<int, int> #define pb push_back #define f first #define s second #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); int dp[100010]; int tree[400010]; int const N = 100001; int V, ind; void update(int TN, int S, int E) { if (S == E) { tree[TN] = V; return; } int mid = (S + E) / 2; if (mid >= ind) { update(2 * TN, S, mid); } else { update(2 * TN + 1, mid + 1, E); } tree[TN] = max(tree[2 * TN], tree[2 * TN + 1]); } int L, R; int query(int TN, int S, int E) { if (S > R || E < L) { return 0; } if (S >= L && E <= R) { return tree[TN]; } int mid = (S + E) / 2; return max(query(2 * TN, S, mid), query(2 * TN + 1, mid + 1, E)); } signed main() { IOS; int n; cin >> n; int a[n + 1]; int h[n + 1]; for (int i = 1; i <= n; i++) { cin >> h[i]; } for (int i = 1; i <= n; i++) { cin >> a[i]; } h[0] = 0; a[0] = 0; for (int i = 1; i <= n; i++) { dp[h[i]] = a[i]; V = a[i]; ind = h[i]; update(1, 1, N); L = 0; R = h[i] - 1; dp[h[i]] = max(dp[h[i]], a[i] + query(1, 1, N)); V = dp[h[i]]; ind = h[i]; update(1, 1, N); } int ans = 0; for (int i = 1; i <= n; i++) { ans = max(ans, dp[h[i]]); } cout << ans << endl; return 0; }
/*input 5 1 2 3 4 5 1000000000 1000000000 1000000000 1000000000 1000000000 */ #include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<int, int> #define pb push_back #define f first #define s second #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); int const N = 200001; int dp[N]; int tree[4 * N]; int V, ind; void update(int TN, int S, int E) { if (S == E) { tree[TN] = V; return; } int mid = (S + E) / 2; if (mid >= ind) { update(2 * TN, S, mid); } else { update(2 * TN + 1, mid + 1, E); } tree[TN] = max(tree[2 * TN], tree[2 * TN + 1]); } int L, R; int query(int TN, int S, int E) { if (S > R || E < L) { return 0; } if (S >= L && E <= R) { return tree[TN]; } int mid = (S + E) / 2; return max(query(2 * TN, S, mid), query(2 * TN + 1, mid + 1, E)); } signed main() { IOS; int n; cin >> n; int a[n + 1]; int h[n + 1]; for (int i = 1; i <= n; i++) { cin >> h[i]; } for (int i = 1; i <= n; i++) { cin >> a[i]; } h[0] = 0; a[0] = 0; for (int i = 1; i <= n; i++) { dp[h[i]] = a[i]; V = a[i]; ind = h[i]; update(1, 1, N); L = 0; R = h[i] - 1; dp[h[i]] = max(dp[h[i]], a[i] + query(1, 1, N)); V = dp[h[i]]; ind = h[i]; update(1, 1, N); } int ans = 0; for (int i = 1; i <= n; i++) { ans = max(ans, dp[h[i]]); } cout << ans << endl; return 0; }
replace
17
20
17
20
0