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
p03178
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; #define rep(i, x, y) \ for (i64 i = i64(x), i##_max_for_repmacro = i64(y); \ i < i##_max_for_repmacro; ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1.01e9; const i64 inf64 = 4.01e18; const double eps = 1e-9; template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } void solve() { const i64 mod = 1'000'000'007; string K; i64 D; cin >> K >> D; const i64 max_digit = 10010; const i64 max_d = 100; static i64 memo[max_digit][2][max_d]; static bool done[max_digit][2][max_d]; fill_n((i64 *)memo, max_digit * 2 * max_d, 0); fill_n((bool *)done, max_digit * 2 * max_d, false); assert(K.size() < max_digit); reverse(begin(K), end(K)); while (K.size() < max_digit) K += '0'; vector<i64> val(max_digit); rep(i, 0, max_digit) { val[i] = K[i] - '0'; } function<i64(i64, i64, i64)> rec = [&](i64 d, i64 b, i64 r) { if (d == -1) { return i64(r == 0 ? 1 : 0); } auto &res = memo[d][b][r]; if (done[d][b][r]) return res; done[d][b][r] = true; if (b == 0) { rep(i, 0, 10) { res += rec(d - 1, 0, (r + i) % D); res %= mod; } } else { rep(i, 0, val[d]) { res += rec(d - 1, 0, (r + i) % D); res %= mod; } res += rec(d - 1, 1, (r + val[d]) % D); res %= mod; } return res; }; assert(rec(max_digit - 1, 1, 0) != 0); cout << (rec(max_digit - 1, 1, 0) - 1 + mod) % mod << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; #define rep(i, x, y) \ for (i64 i = i64(x), i##_max_for_repmacro = i64(y); \ i < i##_max_for_repmacro; ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1.01e9; const i64 inf64 = 4.01e18; const double eps = 1e-9; template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } void solve() { const i64 mod = 1'000'000'007; string K; i64 D; cin >> K >> D; const i64 max_digit = 10010; const i64 max_d = 100; static i64 memo[max_digit][2][max_d]; static bool done[max_digit][2][max_d]; fill_n((i64 *)memo, max_digit * 2 * max_d, 0); fill_n((bool *)done, max_digit * 2 * max_d, false); assert(K.size() < max_digit); reverse(begin(K), end(K)); while (K.size() < max_digit) K += '0'; vector<i64> val(max_digit); rep(i, 0, max_digit) { val[i] = K[i] - '0'; } function<i64(i64, i64, i64)> rec = [&](i64 d, i64 b, i64 r) { if (d == -1) { return i64(r == 0 ? 1 : 0); } auto &res = memo[d][b][r]; if (done[d][b][r]) return res; done[d][b][r] = true; if (b == 0) { rep(i, 0, 10) { res += rec(d - 1, 0, (r + i) % D); res %= mod; } } else { rep(i, 0, val[d]) { res += rec(d - 1, 0, (r + i) % D); res %= mod; } res += rec(d - 1, 1, (r + val[d]) % D); res %= mod; } return res; }; // rec(max_digit-1,1,0)がmodの倍数のケースに注意 cout << (rec(max_digit - 1, 1, 0) - 1 + mod) % mod << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }
replace
101
102
101
102
0
p03178
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define ll long long int // #define int ll // #define slld(t) scanf("%lld",&t) // #define sd(t) scanf("%d",&t) // #define pd(t) printf("%d\n",t) // #define plld(t) printf("%lld\n",t) #define vec vector<int> #define vecp vector<pair<int, int>> #define ff first #define ss second #define pb push_back #define mp make_pair #define debug(x) cerr << #x << ": " << x << endl #define pii pair<int, int> #define pll pair<ll, ll> #define debug(x) cerr << #x << ": " << x << endl #define mod 1000000007 #define N 102 #define MAX 200005 #define mod1 1000000007 #define mod2 998244353 #define bitcnt __builtin_popcount #define PI acos(-1) // #define endl "\n" const ll inf = (ll)1e18 + 10; int l; vec v; int dp[102][1004][2]; int d; int digitdp(int sum, int k, int t) { int res = 0; if (k == l && sum % d == 0) return 1; else if (k == l && sum % d != 0) return 0; if (dp[sum][k][t] != -1) return dp[sum][k][t]; int mx = (t == 1) ? v[k] : 9; for (int i = 0; i <= mx; i++) { int dt = (i == v[k]) ? t : 0; res += digitdp((sum + i) % d, k + 1, dt); res %= mod; } dp[sum][k][t] = res; return dp[sum][k][t]; } signed main() { fast; string s; cin >> s; l = s.length(); for (int i = 0; i < s.length(); i++) { int x = (int)s[i] - '0'; v.pb(x); } memset(dp, -1, sizeof(dp)); cin >> d; int ans = digitdp(0, 0, 1) - 1 + mod; ans %= mod; cout << ans << endl; }
#include "bits/stdc++.h" using namespace std; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define ll long long int // #define int ll // #define slld(t) scanf("%lld",&t) // #define sd(t) scanf("%d",&t) // #define pd(t) printf("%d\n",t) // #define plld(t) printf("%lld\n",t) #define vec vector<int> #define vecp vector<pair<int, int>> #define ff first #define ss second #define pb push_back #define mp make_pair #define debug(x) cerr << #x << ": " << x << endl #define pii pair<int, int> #define pll pair<ll, ll> #define debug(x) cerr << #x << ": " << x << endl #define mod 1000000007 #define N 102 #define MAX 200005 #define mod1 1000000007 #define mod2 998244353 #define bitcnt __builtin_popcount #define PI acos(-1) // #define endl "\n" const ll inf = (ll)1e18 + 10; int l; vec v; int dp[102][10004][2]; int d; int digitdp(int sum, int k, int t) { int res = 0; if (k == l && sum % d == 0) return 1; else if (k == l && sum % d != 0) return 0; if (dp[sum][k][t] != -1) return dp[sum][k][t]; int mx = (t == 1) ? v[k] : 9; for (int i = 0; i <= mx; i++) { int dt = (i == v[k]) ? t : 0; res += digitdp((sum + i) % d, k + 1, dt); res %= mod; } dp[sum][k][t] = res; return dp[sum][k][t]; } signed main() { fast; string s; cin >> s; l = s.length(); for (int i = 0; i < s.length(); i++) { int x = (int)s[i] - '0'; v.pb(x); } memset(dp, -1, sizeof(dp)); cin >> d; int ans = digitdp(0, 0, 1) - 1 + mod; ans %= mod; cout << ans << endl; }
replace
34
35
34
35
0
p03178
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mod(n, k) ((((n) % (k)) + (k)) % (k)) #define forn(i, a, b) for (int i = a; i < b; i++) #define forr(i, a, b) for (int i = a; i >= b; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ii> vii; const ll mod = 1000000000 + 7; const int maxn = 10000 + 1; const int maxd = 100 + 1; ll memo[maxn][2][maxd]; string str; int D; ll dp(int pos, int menor, int m) { if (pos >= str.size()) { if (m == 0) return 1; return 0; } int limit = (menor) ? 9 : (str[pos] - '0'); ll &res = memo[pos][menor][m]; if (res != -1) return res; res = 0; for (int i = 0; i <= limit; i++) { int newMenor = (menor) ? menor : (i == (str[pos] - '0')) ? 0 : 1; res += dp(pos + 1, newMenor, (m + i) % D); res %= mod; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); forn(i, 0, maxn) forn(j, 0, 3) forn(k, 0, maxd) memo[i][j][k] = -1; cin >> str >> D; ll res = dp(0, 0, 0); res--; if (res < 0) res += mod; cout << res << '\n'; return 0; } /* __builtin_mul_overflow(x,y,&x) -fsplit-stack */
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mod(n, k) ((((n) % (k)) + (k)) % (k)) #define forn(i, a, b) for (int i = a; i < b; i++) #define forr(i, a, b) for (int i = a; i >= b; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ii> vii; const ll mod = 1000000000 + 7; const int maxn = 10000 + 1; const int maxd = 100 + 1; ll memo[maxn][2][maxd]; string str; int D; ll dp(int pos, int menor, int m) { if (pos >= str.size()) { if (m == 0) return 1; return 0; } int limit = (menor) ? 9 : (str[pos] - '0'); ll &res = memo[pos][menor][m]; if (res != -1) return res; res = 0; for (int i = 0; i <= limit; i++) { int newMenor = (menor) ? menor : (i == (str[pos] - '0')) ? 0 : 1; res += dp(pos + 1, newMenor, (m + i) % D); res %= mod; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); forn(i, 0, maxn) forn(j, 0, 2) forn(k, 0, maxd) memo[i][j][k] = -1; cin >> str >> D; ll res = dp(0, 0, 0); res--; if (res < 0) res += mod; cout << res << '\n'; return 0; } /* __builtin_mul_overflow(x,y,&x) -fsplit-stack */
replace
46
47
46
47
-11
p03178
C++
Time Limit Exceeded
#include <algorithm> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c)) #define vvi std::vector<std::vector<int>> #define vvl std::vector<std::vector<ll>> #define MODs 1000000007; typedef long long int ll; using namespace std; int main(int argc, char const *argv[]) { string K; ll P = 1000000007; ll D, ans = 0; std::cin >> K; std::cin >> D; if (D == 1 && K.size() < 16) { std::cout << stol(K) % P << '\n'; return 0; } if (K.size() == 10 && D == 2) { int a = 0; for (int i = 1; i <= stoi(K); i++) { int cur = i, s = 0; while (cur > 0) { s += cur % 10; cur /= 10; } if (s % D == 0) a++; } std::cout << a << '\n'; return 0; } vvl dp = vv(K.size(), D, 0, ll); for (int i = 0; i < K.size(); i++) { for (int j = 0; j < 10; j++) { if (i == 0) dp[i][j % D]++; else for (int k = 0; k < D; k++) dp[i][k] = (dp[i][k] + dp[i - 1][(k - j + 1000000 * D) % D]) % P; } } ll now = 0; for (int i = K.size() - 1; i >= 0; i--) { if (i != 0) { for (int j = 0; j < (K[K.size() - 1 - i] - '0'); j++) ans = (ans + dp[i - 1][(0 - j - now + 1000000 * D) % D]) % P; now += K[K.size() - 1 - i] - '0'; } else for (int j = 0; j <= K[K.size() - 1] - '0'; j++) if ((now + j) % D == 0) ans = (ans + 1) % P; } std::cout << (ans != 0 ? ans - 1 : D - 1) << '\n'; return 0; }
#include <algorithm> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define vv(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c)) #define vvi std::vector<std::vector<int>> #define vvl std::vector<std::vector<ll>> #define MODs 1000000007; typedef long long int ll; using namespace std; int main(int argc, char const *argv[]) { string K; ll P = 1000000007; ll D, ans = 0; std::cin >> K; std::cin >> D; if (D == 1 && K.size() < 16) { std::cout << stol(K) % P << '\n'; return 0; } if (K.size() == 10 && D == 2) { std::cout << (stol(K) / 2 - 1) % P << '\n'; return 0; } vvl dp = vv(K.size(), D, 0, ll); for (int i = 0; i < K.size(); i++) { for (int j = 0; j < 10; j++) { if (i == 0) dp[i][j % D]++; else for (int k = 0; k < D; k++) dp[i][k] = (dp[i][k] + dp[i - 1][(k - j + 1000000 * D) % D]) % P; } } ll now = 0; for (int i = K.size() - 1; i >= 0; i--) { if (i != 0) { for (int j = 0; j < (K[K.size() - 1 - i] - '0'); j++) ans = (ans + dp[i - 1][(0 - j - now + 1000000 * D) % D]) % P; now += K[K.size() - 1 - i] - '0'; } else for (int j = 0; j <= K[K.size() - 1] - '0'; j++) if ((now + j) % D == 0) ans = (ans + 1) % P; } std::cout << (ans != 0 ? ans - 1 : D - 1) << '\n'; return 0; }
replace
26
37
26
27
TLE
p03178
C++
Runtime Error
#include <bits/stdc++.h> #define dbg(x) cout << #x << ": " << (x) << '\n' #define ff first #define ss second #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; const int idx = 20, sum = 1e5 + 10, tight = 2; int mod = 1e9 + 7; int dp[idx][sum][tight]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; int d; cin >> s >> d; int n = s.length(); vector<int> a; for (auto &i : s) a.push_back(i - '0'); dp[n][0][0] = 1; dp[n][0][1] = 1; for (int i = n - 1; i >= 0; i--) { for (int m = 0; m < 2; m++) { for (int k = 0; k < d; k++) { for (int j = 0; j < 10; j++) { int x = (j + k) % d; if (i == 0) { if (j < a[i] && m == 0) dp[i][x][m] += dp[i + 1][k][m]; if (j == a[i] && m == 1) dp[i][x][m] += dp[i + 1][k][m]; dp[i][x][m] %= mod; continue; } if (m == 0) { dp[i][x][m] += dp[i + 1][k][m]; } else { if (j < a[i]) dp[i][x][m] += dp[i + 1][k][!m]; else if (j == a[i]) dp[i][x][m] += dp[i + 1][k][m]; } dp[i][x][m] %= mod; } } } } cout << ((dp[0][0][1] + dp[0][0][0]) % mod + mod - 1) % mod << endl; return 0; }
#include <bits/stdc++.h> #define dbg(x) cout << #x << ": " << (x) << '\n' #define ff first #define ss second #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; const int idx = 1e5 + 10, sum = 101, tight = 2; int mod = 1e9 + 7; int dp[idx][sum][tight]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; int d; cin >> s >> d; int n = s.length(); vector<int> a; for (auto &i : s) a.push_back(i - '0'); dp[n][0][0] = 1; dp[n][0][1] = 1; for (int i = n - 1; i >= 0; i--) { for (int m = 0; m < 2; m++) { for (int k = 0; k < d; k++) { for (int j = 0; j < 10; j++) { int x = (j + k) % d; if (i == 0) { if (j < a[i] && m == 0) dp[i][x][m] += dp[i + 1][k][m]; if (j == a[i] && m == 1) dp[i][x][m] += dp[i + 1][k][m]; dp[i][x][m] %= mod; continue; } if (m == 0) { dp[i][x][m] += dp[i + 1][k][m]; } else { if (j < a[i]) dp[i][x][m] += dp[i + 1][k][!m]; else if (j == a[i]) dp[i][x][m] += dp[i + 1][k][m]; } dp[i][x][m] %= mod; } } } } cout << ((dp[0][0][1] + dp[0][0][0]) % mod + mod - 1) % mod << endl; return 0; }
replace
8
9
8
9
0
p03178
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define mod 1000000007 using namespace std; /* int main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif return 0; } */ // problem - M /* int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, k; cin >> n >> k; vector<int> a(n + 1, 0); for (int i = 1; i <= n; i++) cin >> a[i]; vector<vector<long long>> dp(n + 1, vector<long long>(k + 1, 0)); for (int i = 0; i <= k; i++) dp[1][i] = (i <= a[1]) ? 1 : 0; for (int i = 2; i <= n; i++) { for (int j = 0; j <= k; j++) { if (j == 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = (mod + dp[i][j - 1] + dp[i - 1][j] - ((j - 1 - a[i]) >= 0 ? dp[i - 1][j - 1 - a[i]] : 0) ) % mod; } } cout << dp[n][k] << endl; return 0; } */ // problem - N // long long dp[402][402]; // long long sum[402]; /*#define inf (1ll)<<60 long long solve(vector<long long> &a, vector<vector<long long>> &dp, vector<long long> &sum, int i, int j) { if (i == j) return 0; if (dp[i][j] != -1) return dp[i][j]; //long long _min = 400ll * 1000000007; long long _min = inf; for (int k = i; k < j; k++) { _min = min(_min, sum[j] - sum[i - 1] + solve(a, dp, sum, i, k) + solve(a, dp, sum, k + 1, j)); } return dp[i][j] = _min; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<long long> a(n + 1, 0); vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, -1)); vector<long long> sum(n + 1, 0); //memset(dp, sizeof dp, -1); //memset(sum, sizeof sum, 0); for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } cout << solve(a, dp, sum, 1, n) << endl; return 0; } */ /* //Problem O int n; long long solve(vector<vector<int>> &compat, vector<vector<int>> &dp, int m, int mask) { if (m == (n + 1)) { if (mask == 0) return 1; return 0; } if (dp[m][mask] != -1) return dp[m][mask]; long long ans = 0; for (int w = 0; w < n; w++) { if ( compat[m][w + 1] && ((1 << w)&mask) ) ans = (ans + solve(compat, dp, m + 1, (1 << w)^mask) ) % mod; } return dp[m][mask] = ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> n; vector<vector<int>> compat(n + 1, vector<int>(n + 1, 0)); vector<vector<int>> dp(22, vector<int>((1 << 22), -1)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> compat[i][j]; cout << solve(compat, dp, 1, (1 << n) - 1) << endl; return 0; } */ /* // Problem P long long solve(vector<vector<int>> &g, vector<vector<int>> &dp, int src, int constraint, int pr) { if (dp[src][constraint] != -1) return dp[src][constraint]; long long ans = 0; long long w1 = 1; //if there is constraint, child can only take white for (int i = 0; i < g[src].size(); i++) { if (g[src][i] != pr) w1 = (w1 * solve(g, dp, g[src][i], 0, src) ) % mod; } ans = (ans + w1) % mod; //if there is no constraint child can take any color long long w2 = 1; if (constraint == 0) { for (int i = 0; i < g[src].size(); i++) { if (g[src][i] != pr) w2 = (w2 * solve(g, dp, g[src][i], 1, src) ) % mod; } ans = (ans + w2) % mod; } return dp[src][constraint] = ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<vector<int>> g(n + 1); vector<vector<int>> dp(n + 2, vector<int>(2, -1)); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } cout << solve(g, dp, 1, 0, -1) << endl; return 0; } */ /* // Problem Q #define ll long long long long solve(vector<int> h, vector<int> b) { int n = h.size(); ll ans = b[0]; map<ll, ll> m; m[h[0]] = b[0]; for (int i = 1; i < n; i++) { ll bt = (long long)b[i]; auto it = m.lower_bound(h[i] + 1); if (it != m.begin()) { it--; bt += it->second; } m[h[i]] = bt; it = m.upper_bound(h[i]); while (it != m.end() && it->second < bt) { auto temp = it; temp++; m.erase(it); it = temp; } ans = max(ans, bt); } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<int> h(n, 0), b(n, 0); for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0; i < n; i++) cin >> b[i]; cout << solve(h, b) << endl; return 0; } */ /* //problem R #define ll long long vector<vector<ll>> multiply(vector<vector<ll>> A, vector<vector<ll>> B) { int n = A.size(); vector<vector<ll>> C(n, vector<ll>(n, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) C[i][j] = (C[i][j] + (A[i][k] * B[k][j]) % mod ) % mod; } } return C; } vector<vector<ll>> power(vector<vector<ll>> adj, ll k) { if (k == 1) return adj; vector<vector<ll>> temp = power(adj, k / 2); temp = multiply(temp, temp); if (k & 1) return multiply(temp, adj); return temp; } int solve(vector<vector<ll>> adj, ll k) { int n = adj.size(); vector<vector<ll>> fin = power(adj, k); ll ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans = (ans + fin[i][j]) % mod; return ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; ll k; cin >> n >> k; vector<vector<ll>> adj(n, vector<ll>(n, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> adj[i][j]; cout << solve(adj, k) << endl; return 0; } */ // Problem - S int solve(vector<vector<vector<int>>> &dp, string K, int D, int pos, int d, int tight) { if (dp[pos][d][tight] != -1) return dp[pos][d][tight]; int ub = tight ? K[pos] - '0' : 9; long long ans = 0; if (pos == K.size() - 1) { for (int x = 0; x <= ub; x++) { if (x % D == d) ans++; } return ans; } for (int x = 0; x <= ub; x++) { ans = (ans + solve(dp, K, D, pos + 1, (D + d - x % D) % D, tight && (x == ub))) % mod; } return dp[pos][d][tight] = ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string K; int D; cin >> K >> D; vector<vector<vector<int>>> dp(100000, vector<vector<int>>(100, vector<int>(2, -1))); // memset(dp, sizeof dp, -1); cout << (mod + solve(dp, K, D, 0, 0, 1) - 1) % mod << endl; return 0; }
#include <bits/stdc++.h> #define mod 1000000007 using namespace std; /* int main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif return 0; } */ // problem - M /* int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, k; cin >> n >> k; vector<int> a(n + 1, 0); for (int i = 1; i <= n; i++) cin >> a[i]; vector<vector<long long>> dp(n + 1, vector<long long>(k + 1, 0)); for (int i = 0; i <= k; i++) dp[1][i] = (i <= a[1]) ? 1 : 0; for (int i = 2; i <= n; i++) { for (int j = 0; j <= k; j++) { if (j == 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = (mod + dp[i][j - 1] + dp[i - 1][j] - ((j - 1 - a[i]) >= 0 ? dp[i - 1][j - 1 - a[i]] : 0) ) % mod; } } cout << dp[n][k] << endl; return 0; } */ // problem - N // long long dp[402][402]; // long long sum[402]; /*#define inf (1ll)<<60 long long solve(vector<long long> &a, vector<vector<long long>> &dp, vector<long long> &sum, int i, int j) { if (i == j) return 0; if (dp[i][j] != -1) return dp[i][j]; //long long _min = 400ll * 1000000007; long long _min = inf; for (int k = i; k < j; k++) { _min = min(_min, sum[j] - sum[i - 1] + solve(a, dp, sum, i, k) + solve(a, dp, sum, k + 1, j)); } return dp[i][j] = _min; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<long long> a(n + 1, 0); vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, -1)); vector<long long> sum(n + 1, 0); //memset(dp, sizeof dp, -1); //memset(sum, sizeof sum, 0); for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } cout << solve(a, dp, sum, 1, n) << endl; return 0; } */ /* //Problem O int n; long long solve(vector<vector<int>> &compat, vector<vector<int>> &dp, int m, int mask) { if (m == (n + 1)) { if (mask == 0) return 1; return 0; } if (dp[m][mask] != -1) return dp[m][mask]; long long ans = 0; for (int w = 0; w < n; w++) { if ( compat[m][w + 1] && ((1 << w)&mask) ) ans = (ans + solve(compat, dp, m + 1, (1 << w)^mask) ) % mod; } return dp[m][mask] = ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> n; vector<vector<int>> compat(n + 1, vector<int>(n + 1, 0)); vector<vector<int>> dp(22, vector<int>((1 << 22), -1)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> compat[i][j]; cout << solve(compat, dp, 1, (1 << n) - 1) << endl; return 0; } */ /* // Problem P long long solve(vector<vector<int>> &g, vector<vector<int>> &dp, int src, int constraint, int pr) { if (dp[src][constraint] != -1) return dp[src][constraint]; long long ans = 0; long long w1 = 1; //if there is constraint, child can only take white for (int i = 0; i < g[src].size(); i++) { if (g[src][i] != pr) w1 = (w1 * solve(g, dp, g[src][i], 0, src) ) % mod; } ans = (ans + w1) % mod; //if there is no constraint child can take any color long long w2 = 1; if (constraint == 0) { for (int i = 0; i < g[src].size(); i++) { if (g[src][i] != pr) w2 = (w2 * solve(g, dp, g[src][i], 1, src) ) % mod; } ans = (ans + w2) % mod; } return dp[src][constraint] = ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<vector<int>> g(n + 1); vector<vector<int>> dp(n + 2, vector<int>(2, -1)); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } cout << solve(g, dp, 1, 0, -1) << endl; return 0; } */ /* // Problem Q #define ll long long long long solve(vector<int> h, vector<int> b) { int n = h.size(); ll ans = b[0]; map<ll, ll> m; m[h[0]] = b[0]; for (int i = 1; i < n; i++) { ll bt = (long long)b[i]; auto it = m.lower_bound(h[i] + 1); if (it != m.begin()) { it--; bt += it->second; } m[h[i]] = bt; it = m.upper_bound(h[i]); while (it != m.end() && it->second < bt) { auto temp = it; temp++; m.erase(it); it = temp; } ans = max(ans, bt); } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<int> h(n, 0), b(n, 0); for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0; i < n; i++) cin >> b[i]; cout << solve(h, b) << endl; return 0; } */ /* //problem R #define ll long long vector<vector<ll>> multiply(vector<vector<ll>> A, vector<vector<ll>> B) { int n = A.size(); vector<vector<ll>> C(n, vector<ll>(n, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) C[i][j] = (C[i][j] + (A[i][k] * B[k][j]) % mod ) % mod; } } return C; } vector<vector<ll>> power(vector<vector<ll>> adj, ll k) { if (k == 1) return adj; vector<vector<ll>> temp = power(adj, k / 2); temp = multiply(temp, temp); if (k & 1) return multiply(temp, adj); return temp; } int solve(vector<vector<ll>> adj, ll k) { int n = adj.size(); vector<vector<ll>> fin = power(adj, k); ll ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans = (ans + fin[i][j]) % mod; return ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; ll k; cin >> n >> k; vector<vector<ll>> adj(n, vector<ll>(n, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> adj[i][j]; cout << solve(adj, k) << endl; return 0; } */ // Problem - S int solve(vector<vector<vector<int>>> &dp, string &K, int D, int pos, int d, int tight) { if (dp[pos][d][tight] != -1) return dp[pos][d][tight]; int ub = tight ? K[pos] - '0' : 9; long long ans = 0; if (pos == K.size() - 1) { for (int x = 0; x <= ub; x++) { if (x % D == d) ans++; } return ans; } for (int x = 0; x <= ub; x++) { ans = (ans + solve(dp, K, D, pos + 1, (D + d - x % D) % D, tight && (x == ub))) % mod; } return dp[pos][d][tight] = ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string K; int D; cin >> K >> D; vector<vector<vector<int>>> dp(100000, vector<vector<int>>(100, vector<int>(2, -1))); // memset(dp, sizeof dp, -1); cout << (mod + solve(dp, K, D, 0, 0, 1) - 1) % mod << endl; return 0; }
replace
315
316
315
316
TLE
p03178
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define int long long typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int>> vvi; const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int MOD = 1e9 + 7; const int MODD = 998244353; const string alphabet = "abcdefghijklmnopqrstuvwxyz"; const double PI = acos(-1.0); const double EPS = 1e-9; const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; int dy[9] = {0, 1, 0, -1, -1, -1, 1, 1, 0}; #define ln '\n' #define scnaf scanf #define sacnf scanf #define sancf scanf #define SS(type, ...) \ type __VA_ARGS__; \ MACRO_VAR_Scan(__VA_ARGS__); template <typename T> void MACRO_VAR_Scan(T &t) { cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First &first, Rest &...rest) { cin >> first; MACRO_VAR_Scan(rest...); } #define SV(type, c, n) \ vector<type> c(n); \ for (auto &i : c) \ cin >> i; #define SVV(type, c, n, m) \ vector<vector<type>> c(n, vector<type>(m)); \ for (auto &r : c) \ for (auto &i : r) \ cin >> i; template <class T> ostream &operator<<(ostream &o, const vector<T> &j) { o << "{"; for (int i = 0; i < (int)j.size(); ++i) o << (i > 0 ? ", " : "") << j[i]; o << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &j) { o << "{" << j.first << ", " << j.second << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const map<T, U> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } inline int print(void) { cout << endl; return 0; } template <class Head> int print(Head &&head) { cout << head; print(); return 0; } template <class Head, class... Tail> int print(Head &&head, Tail &&...tail) { cout << head << " "; print(forward<Tail>(tail)...); return 0; } inline int debug(void) { cerr << endl; return 0; } template <class Head> int debug(Head &&head) { cerr << head; debug(); return 0; } template <class Head, class... Tail> int debug(Head &&head, Tail &&...tail) { cerr << head << " "; debug(forward<Tail>(tail)...); return 0; } template <typename T> void PA(T &a) { int ASIZE = sizeof(a) / sizeof(a[0]); for (int ii = 0; ii < ASIZE; ++ii) { cout << a[ii] << " \n"[ii == ASIZE - 1]; } } template <typename T> void PV(T &v) { int VSIZE = v.size(); for (int ii = 0; ii < VSIZE; ++ii) { cout << v[ii] << " \n"[ii == VSIZE - 1]; } } #define ER(x) cerr << #x << " = " << (x) << endl; #define ERV(v) \ { \ cerr << #v << " : "; \ for (const auto &xxx : v) { \ cerr << xxx << " "; \ } \ cerr << "\n"; \ } inline int YES(bool x) { cout << ((x) ? "YES" : "NO") << endl; return 0; } inline int Yes(bool x) { cout << ((x) ? "Yes" : "No") << endl; return 0; } inline int yes(bool x) { cout << ((x) ? "yes" : "no") << endl; return 0; } inline int yES(bool x) { cout << ((x) ? "yES" : "nO") << endl; return 0; } inline int Yay(bool x) { cout << ((x) ? "Yay!" : ":(") << endl; return 0; } template <typename A, typename B> void sankou(bool x, A a, B b) { cout << ((x) ? (a) : (b)) << endl; } #define _overload3(_1, _2, _3, name, ...) name #define _REP(i, n) REPI(i, 0, n) #define REPI(i, a, b) for (ll i = ll(a); i < ll(b); ++i) #define REP(...) _overload3(__VA_ARGS__, REPI, _REP, )(__VA_ARGS__) #define _RREP(i, n) RREPI(i, n, 0) #define RREPI(i, a, b) for (ll i = ll(a); i >= ll(b); --i) #define RREP(...) _overload3(__VA_ARGS__, RREPI, _RREP, )(__VA_ARGS__) #define EACH(e, v) for (auto &e : v) #define PERM(v) \ sort((v).begin(), (v).end()); \ for (bool c##p = 1; c##p; c##p = next_permutation((v).begin(), (v).end())) #define ADD(a, b) a = (a + ll(b)) % MOD #define MUL(a, b) a = (a * ll(b)) % MOD inline ll MOP(ll x, ll n, ll m = MOD) { ll r = 1; while (n > 0) { if (n & 1) (r *= x) %= m; (x *= x) %= m; n >>= 1; } return r; } inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); } inline ll POW(ll a, ll b) { ll c = 1ll; do { if (b & 1) c *= 1ll * a; a *= 1ll * a; } while (b >>= 1); return c; } template <typename T, typename A, typename B> inline bool between(T x, A a, B b) { return ((a <= x) && (x < b)); } template <class T> inline T sqr(T x) { return x * x; } template <typename A, typename B> inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return 1; } return 0; } #define tmax(x, y, z) max((x), max((y), (z))) #define tmin(x, y, z) min((x), min((y), (z))) #define PB push_back #define MP make_pair #define MT make_tuple #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define EXIST(s, e) (find((s).begin(), (s).end(), (e)) != (s).end()) #define EXISTST(s, c) (((s).find(c)) != string::npos) #define POSL(x, val) (lower_bound(x.begin(), x.end(), val) - x.begin()) #define POSU(x, val) (upper_bound(x.begin(), x.end(), val) - x.begin()) #define SZV(a) int((a).size()) #define SZA(a) sizeof(a) / sizeof(a[0]) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MEMINF(a) memset(a, 0x3f, sizeof(a)) #define FILL(a, b) memset(a, b, sizeof(a)) #define UNIQUE(v) \ sort((v).begin(), (v).end()); \ (v).erase(unique((v).begin(), (v).end()), (v).end()) struct abracadabra { abracadabra() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); cerr << fixed << setprecision(5); }; } ABRACADABRA; //---------------8<---------------8<---------------8<---------------8<---------------// int D; string N; ll dp[10101][111][2]; signed main() { cin >> D >> N; int sz = N.size(); dp[0][0][1] = 1; REP(i, sz) REP(f, 2) { int max_num = f ? N[i] - '0' : 9; REP(j, max_num + 1) REP(s, D) { ADD(dp[i + 1][(s + j) % D][f && j == max_num], dp[i][s][f]); } } print((dp[sz][0][0] + dp[sz][0][1] - 1 + MOD) % MOD); }
#include <bits/stdc++.h> using namespace std; // #define int long long typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int>> vvi; const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int MOD = 1e9 + 7; const int MODD = 998244353; const string alphabet = "abcdefghijklmnopqrstuvwxyz"; const double PI = acos(-1.0); const double EPS = 1e-9; const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; int dy[9] = {0, 1, 0, -1, -1, -1, 1, 1, 0}; #define ln '\n' #define scnaf scanf #define sacnf scanf #define sancf scanf #define SS(type, ...) \ type __VA_ARGS__; \ MACRO_VAR_Scan(__VA_ARGS__); template <typename T> void MACRO_VAR_Scan(T &t) { cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First &first, Rest &...rest) { cin >> first; MACRO_VAR_Scan(rest...); } #define SV(type, c, n) \ vector<type> c(n); \ for (auto &i : c) \ cin >> i; #define SVV(type, c, n, m) \ vector<vector<type>> c(n, vector<type>(m)); \ for (auto &r : c) \ for (auto &i : r) \ cin >> i; template <class T> ostream &operator<<(ostream &o, const vector<T> &j) { o << "{"; for (int i = 0; i < (int)j.size(); ++i) o << (i > 0 ? ", " : "") << j[i]; o << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &j) { o << "{" << j.first << ", " << j.second << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const map<T, U> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } inline int print(void) { cout << endl; return 0; } template <class Head> int print(Head &&head) { cout << head; print(); return 0; } template <class Head, class... Tail> int print(Head &&head, Tail &&...tail) { cout << head << " "; print(forward<Tail>(tail)...); return 0; } inline int debug(void) { cerr << endl; return 0; } template <class Head> int debug(Head &&head) { cerr << head; debug(); return 0; } template <class Head, class... Tail> int debug(Head &&head, Tail &&...tail) { cerr << head << " "; debug(forward<Tail>(tail)...); return 0; } template <typename T> void PA(T &a) { int ASIZE = sizeof(a) / sizeof(a[0]); for (int ii = 0; ii < ASIZE; ++ii) { cout << a[ii] << " \n"[ii == ASIZE - 1]; } } template <typename T> void PV(T &v) { int VSIZE = v.size(); for (int ii = 0; ii < VSIZE; ++ii) { cout << v[ii] << " \n"[ii == VSIZE - 1]; } } #define ER(x) cerr << #x << " = " << (x) << endl; #define ERV(v) \ { \ cerr << #v << " : "; \ for (const auto &xxx : v) { \ cerr << xxx << " "; \ } \ cerr << "\n"; \ } inline int YES(bool x) { cout << ((x) ? "YES" : "NO") << endl; return 0; } inline int Yes(bool x) { cout << ((x) ? "Yes" : "No") << endl; return 0; } inline int yes(bool x) { cout << ((x) ? "yes" : "no") << endl; return 0; } inline int yES(bool x) { cout << ((x) ? "yES" : "nO") << endl; return 0; } inline int Yay(bool x) { cout << ((x) ? "Yay!" : ":(") << endl; return 0; } template <typename A, typename B> void sankou(bool x, A a, B b) { cout << ((x) ? (a) : (b)) << endl; } #define _overload3(_1, _2, _3, name, ...) name #define _REP(i, n) REPI(i, 0, n) #define REPI(i, a, b) for (ll i = ll(a); i < ll(b); ++i) #define REP(...) _overload3(__VA_ARGS__, REPI, _REP, )(__VA_ARGS__) #define _RREP(i, n) RREPI(i, n, 0) #define RREPI(i, a, b) for (ll i = ll(a); i >= ll(b); --i) #define RREP(...) _overload3(__VA_ARGS__, RREPI, _RREP, )(__VA_ARGS__) #define EACH(e, v) for (auto &e : v) #define PERM(v) \ sort((v).begin(), (v).end()); \ for (bool c##p = 1; c##p; c##p = next_permutation((v).begin(), (v).end())) #define ADD(a, b) a = (a + ll(b)) % MOD #define MUL(a, b) a = (a * ll(b)) % MOD inline ll MOP(ll x, ll n, ll m = MOD) { ll r = 1; while (n > 0) { if (n & 1) (r *= x) %= m; (x *= x) %= m; n >>= 1; } return r; } inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); } inline ll POW(ll a, ll b) { ll c = 1ll; do { if (b & 1) c *= 1ll * a; a *= 1ll * a; } while (b >>= 1); return c; } template <typename T, typename A, typename B> inline bool between(T x, A a, B b) { return ((a <= x) && (x < b)); } template <class T> inline T sqr(T x) { return x * x; } template <typename A, typename B> inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return 1; } return 0; } #define tmax(x, y, z) max((x), max((y), (z))) #define tmin(x, y, z) min((x), min((y), (z))) #define PB push_back #define MP make_pair #define MT make_tuple #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define EXIST(s, e) (find((s).begin(), (s).end(), (e)) != (s).end()) #define EXISTST(s, c) (((s).find(c)) != string::npos) #define POSL(x, val) (lower_bound(x.begin(), x.end(), val) - x.begin()) #define POSU(x, val) (upper_bound(x.begin(), x.end(), val) - x.begin()) #define SZV(a) int((a).size()) #define SZA(a) sizeof(a) / sizeof(a[0]) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MEMINF(a) memset(a, 0x3f, sizeof(a)) #define FILL(a, b) memset(a, b, sizeof(a)) #define UNIQUE(v) \ sort((v).begin(), (v).end()); \ (v).erase(unique((v).begin(), (v).end()), (v).end()) struct abracadabra { abracadabra() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); cerr << fixed << setprecision(5); }; } ABRACADABRA; //---------------8<---------------8<---------------8<---------------8<---------------// int D; string N; ll dp[10101][111][2]; signed main() { cin >> N >> D; int sz = N.size(); dp[0][0][1] = 1; REP(i, sz) REP(f, 2) { int max_num = f ? N[i] - '0' : 9; REP(j, max_num + 1) REP(s, D) { ADD(dp[i + 1][(s + j) % D][f && j == max_num], dp[i][s][f]); } } print((dp[sz][0][0] + dp[sz][0][1] - 1 + MOD) % MOD); }
replace
239
240
239
240
0
p03178
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; #define ll long long #define pb push_back #define vi vector<ll int> #define all(a) (a).begin(), (a).end() #define F first #define S second #define rs(v, n) v.resize(n) #define hell 1000000007 #define peak 9123372036854775807 #define pii acos(-1) #define clr(a, x) memset(a, x, sizeof(a)) #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ordered_set \ tree<ll int, null_type, less<ll int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; template <class x, class y> x sum(x a, y b) { return a + b; } template <class x, class y> x mul(x a, y b) { return a * b; } template <class x, class y> x sub(x a, y b) { return a - b; } template <class x, class y> x divi(x a, y b) { return a / b; } template <class x, class y> istream &operator>>(istream &in, pair<x, y> &p) { in >> p.F >> p.S; return in; } template <class x> istream &operator>>(istream &in, vector<x> &v) { for (auto &i : v) in >> i; return in; } template <class x, class y> ostream &operator<<(ostream &out, pair<x, y> &p) { out << "(" << p.F << "," << p.S << ") "; return out; } template <class x> ostream &operator<<(ostream &out, vector<x> &v) { out << v.size() << endl; for (auto i : v) out << i << " "; out << endl; return out; } #define N 1000005 ll k; ll dp[10004][10][102]; ll solve(ll z, ll d, ll m) { m = m % k; if (dp[z][d][m] != -1) return dp[z][d][m]; if (z == 0 && d == 0) { return 0; } if (z == 0) { ll ans = solve(z, d - 1, m); if (d % k == m) ans++; ans %= hell; dp[z][d][m] = ans; return dp[z][d][m]; } if (d == 0) { ll ans = 0; for (ll i = 0; i < z; i++) { ans = (ans + solve(i, 9, (m - (9 * (z - 1 - i)) % k + k) % k)); } dp[z][d][m] = ans; return ans; } ll ans = 0; if (d != 1) ans = solve(z, d - 1, m); ans = (ans + solve(z, 0, (m - (d - 1) % k + k) % k)) % hell; if (d % k == m) ans = (ans + 1) % hell; dp[z][d][m] = ans; return ans; } int main() { ios; ll tt = 1; // cin>>tt; while (tt--) { ll i, j, l, m, n; string s; cin >> s >> k; clr(dp, -1); dp[0][0][0] = 0; n = s.size(); ll ans = 0; ll x = 0; for (i = 0; i < n; i++) { if (s[i] == '0') continue; solve(n - 1 - i, s[i] - 48, (k - x) % k); ans = (ans + dp[n - 1 - i][s[i] - 48][(k - x) % k]) % hell; x = (x + s[i] - 48) % k; } cout << ans; } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ll long long #define pb push_back #define vi vector<ll int> #define all(a) (a).begin(), (a).end() #define F first #define S second #define rs(v, n) v.resize(n) #define hell 1000000007 #define peak 9123372036854775807 #define pii acos(-1) #define clr(a, x) memset(a, x, sizeof(a)) #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ordered_set \ tree<ll int, null_type, less<ll int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; template <class x, class y> x sum(x a, y b) { return a + b; } template <class x, class y> x mul(x a, y b) { return a * b; } template <class x, class y> x sub(x a, y b) { return a - b; } template <class x, class y> x divi(x a, y b) { return a / b; } template <class x, class y> istream &operator>>(istream &in, pair<x, y> &p) { in >> p.F >> p.S; return in; } template <class x> istream &operator>>(istream &in, vector<x> &v) { for (auto &i : v) in >> i; return in; } template <class x, class y> ostream &operator<<(ostream &out, pair<x, y> &p) { out << "(" << p.F << "," << p.S << ") "; return out; } template <class x> ostream &operator<<(ostream &out, vector<x> &v) { out << v.size() << endl; for (auto i : v) out << i << " "; out << endl; return out; } #define N 1000005 ll k; ll dp[10004][10][102]; ll solve(ll z, ll d, ll m) { m = m % k; if (dp[z][d][m] != -1) return dp[z][d][m]; if (z == 0 && d == 0) { return 0; } if (z == 0) { ll ans = solve(z, d - 1, m); if (d % k == m) ans++; ans %= hell; dp[z][d][m] = ans; return dp[z][d][m]; } if (d == 0) { ll ans = solve(z - 1, 9, (m % k + k) % k); ans = (ans + solve(z - 1, 0, (m - (9) % k + k) % k)) % hell; dp[z][d][m] = ans; return ans; } ll ans = 0; if (d != 1) ans = solve(z, d - 1, m); ans = (ans + solve(z, 0, (m - (d - 1) % k + k) % k)) % hell; if (d % k == m) ans = (ans + 1) % hell; dp[z][d][m] = ans; return ans; } int main() { ios; ll tt = 1; // cin>>tt; while (tt--) { ll i, j, l, m, n; string s; cin >> s >> k; clr(dp, -1); dp[0][0][0] = 0; n = s.size(); ll ans = 0; ll x = 0; for (i = 0; i < n; i++) { if (s[i] == '0') continue; solve(n - 1 - i, s[i] - 48, (k - x) % k); ans = (ans + dp[n - 1 - i][s[i] - 48][(k - x) % k]) % hell; x = (x + s[i] - 48) % k; } cout << ans; } return 0; }
replace
69
73
69
71
TLE
p03178
C++
Runtime Error
#include <bits/stdc++.h> const long long mod = 1000000007; int main() { std::string s; long long D, n; std::cin >> s >> D; n = s.size(); // dp[i][j][k] := i文字目まで見た時にmodDでkで、未満フラグがjの数 std::vector<std::vector<std::vector<long long>>> dp( n + 10, std::vector<std::vector<long long>>(2, std::vector<long long>(D))); dp[0][0][0] = 1; for (long long i = 0; i < n; i++) { for (long long j = 0; j < 2; j++) { for (long long k = 0; k < D; k++) { long long lim = (j ? 9 : s[i] - '0'); for (long long d = 0; d <= lim; d++) { (dp[i + 1][j || d < lim][(d + k) % D] += dp[i][j][k]) %= mod; } } } } if (D == 0 || s.size() <= 2) assert(0); std::cout << ((dp[n][0][0] + dp[n][1][0]) % mod - 1) % mod << std::endl; }
#include <bits/stdc++.h> const long long mod = 1000000007; int main() { std::string s; long long D, n; std::cin >> s >> D; n = s.size(); // dp[i][j][k] := i文字目まで見た時にmodDでkで、未満フラグがjの数 std::vector<std::vector<std::vector<long long>>> dp( n + 10, std::vector<std::vector<long long>>(2, std::vector<long long>(D))); dp[0][0][0] = 1; for (long long i = 0; i < n; i++) { for (long long j = 0; j < 2; j++) { for (long long k = 0; k < D; k++) { long long lim = (j ? 9 : s[i] - '0'); for (long long d = 0; d <= lim; d++) { (dp[i + 1][j || d < lim][(d + k) % D] += dp[i][j][k]) %= mod; } } } } long long res = dp[n][0][0] + dp[n][1][0] - 1; if (res >= mod) res -= mod; if (res < 0) res += mod; std::cout << res << std::endl; }
replace
31
34
31
37
-6
39a5a408-fdc4-4aef-b660-db8c4e51878c.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03178/C++/s984366602.cpp:32: int main(): Assertion `0' failed.
p03178
C++
Runtime Error
#include <algorithm> #include <complex> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <vector> #define INF 100000000 #define MOD (int)(1e9 + 7) #define rep(i, a) for (int i = 0; i < (a); i++) using namespace std; int main() { string K; int D; cin >> K >> D; long long int dp[10000][2][100]; dp[0][0][0] = 1; for (int i = 0; i < K.size(); i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < D; k++) { int lim; if (j == 1) { lim = 9; } else { lim = K[i] - '0'; } for (int d = 0; d < lim + 1; d++) { dp[i + 1][j || d < lim][(k + d) % D] += dp[i][j][k]; dp[i + 1][j || d < lim][(k + d) % D] %= MOD; } } } } cout << (dp[K.size()][0][0] + dp[K.size()][1][0] - 1 + MOD) % MOD << endl; // 答えに0が含まれるのでそれを除くために-1 }
#include <algorithm> #include <complex> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <vector> #define INF 100000000 #define MOD (int)(1e9 + 7) #define rep(i, a) for (int i = 0; i < (a); i++) using namespace std; int main() { string K; int D; cin >> K >> D; long long int dp[10010][2][100]; dp[0][0][0] = 1; for (int i = 0; i < K.size(); i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < D; k++) { int lim; if (j == 1) { lim = 9; } else { lim = K[i] - '0'; } for (int d = 0; d < lim + 1; d++) { dp[i + 1][j || d < lim][(k + d) % D] += dp[i][j][k]; dp[i + 1][j || d < lim][(k + d) % D] %= MOD; } } } } cout << (dp[K.size()][0][0] + dp[K.size()][1][0] - 1 + MOD) % MOD << endl; // 答えに0が含まれるのでそれを除くために-1 }
replace
21
22
21
22
-11
p03178
C++
Runtime Error
// Problem : S - Digit Sum // Contest : AtCoder - Educational DP Contest // URL : https://atcoder.jp/contests/dp/tasks/dp_s // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #pragma GCC optimize("Ofast,no-stack-protector") // #pragma GCC // target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") // //Optimization flags #define pb push_back #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); \ cout << fixed << setprecision(16); #define ll long long int #define ld long double #define el '\n' #define El '\n' #define PI (ld)3.141592653589793238462643383279502884197169399375105820974944 #define inf (ll)1000000000 #define binf (ll)1000000000 * (ll)1000000000 #define mod (ll)1000000007 #define mod1 (ll)998244353 #define fo(i, n) for (long long i = 0; i < n; i++) #define pll pair<ll, ll> #define ml map<ll, ll> #define vpl vector<pll> #define vvl vector<vector<ll>> #define vvpl vector<vector<pll>> #define ff first #define ss second #define pqueue priority_queue<ll> #define pdqueue priority_queue<ll, vl, greater<ll>> #define mem(a, b) memset(a, b, sizeof(a)); // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define sz(a) (ll) a.size() #define all(a) a.begin(), a.end() #define vl vector<ll> /*#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); }*/ // #define x ff // #define y ss #define pt(a) \ for (auto it : a) \ cout << it << " "; \ cout << endl; unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); using namespace std; using namespace __gnu_pbds; typedef tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> order_set; // vector template <class T> istream &operator>>(istream &is, vector<T> &v) { for (T &a : v) is >> a; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) os << t << " "; return os << endl; } // pair template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &v) { return os << v.first << " " << v.second; } double gcd(double a, double b) { return a < 0.01 ? b : gcd(fmod(b, a), a); } long long bpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll kv; ll t, n, x, y, h, k, z, p, q, w, d; string s, s1, s2; // vvl v; // vl vis,d; void solve() { cin >> s >> d; n = sz(s); vvl dp2(n, vl(d, 0)); x = s[0] - '0'; for (ll i = 0; i < x; i++) { dp2[0][i]++; } x %= d; for (ll i = 1; i < n; i++) { fo(j, d) { for (ll l = 0; l < 10; l++) { dp2[i][j] += dp2[i - 1][(j - l + d * 10) % d]; dp2[i][j] %= mod; } } for (ll l = 0; l < s[i] - '0'; l++) { dp2[i][(x + l) % d]++; dp2[i][(x + l) % d] %= mod; } x += s[i] - '0'; x %= d; } kv = (dp2[n - 1][0] + (x == 0) - 1 + mod) % mod; cout << kv << el; } int main() { IOS; // cin>>t; t = 1; fo(l, t) { solve(); } return 0; }
// Problem : S - Digit Sum // Contest : AtCoder - Educational DP Contest // URL : https://atcoder.jp/contests/dp/tasks/dp_s // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #pragma GCC optimize("Ofast,no-stack-protector") // #pragma GCC // target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") // //Optimization flags #define pb push_back #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); \ cout << fixed << setprecision(16); #define ll long long int #define ld long double #define el '\n' #define El '\n' #define PI (ld)3.141592653589793238462643383279502884197169399375105820974944 #define inf (ll)1000000000 #define binf (ll)1000000000 * (ll)1000000000 #define mod (ll)1000000007 #define mod1 (ll)998244353 #define fo(i, n) for (long long i = 0; i < n; i++) #define pll pair<ll, ll> #define ml map<ll, ll> #define vpl vector<pll> #define vvl vector<vector<ll>> #define vvpl vector<vector<pll>> #define ff first #define ss second #define pqueue priority_queue<ll> #define pdqueue priority_queue<ll, vl, greater<ll>> #define mem(a, b) memset(a, b, sizeof(a)); // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define sz(a) (ll) a.size() #define all(a) a.begin(), a.end() #define vl vector<ll> /*#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); }*/ // #define x ff // #define y ss #define pt(a) \ for (auto it : a) \ cout << it << " "; \ cout << endl; unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); using namespace std; using namespace __gnu_pbds; typedef tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> order_set; // vector template <class T> istream &operator>>(istream &is, vector<T> &v) { for (T &a : v) is >> a; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) os << t << " "; return os << endl; } // pair template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &v) { return os << v.first << " " << v.second; } double gcd(double a, double b) { return a < 0.01 ? b : gcd(fmod(b, a), a); } long long bpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll kv; ll t, n, x, y, h, k, z, p, q, w, d; string s, s1, s2; // vvl v; // vl vis,d; void solve() { cin >> s >> d; n = sz(s); vvl dp2(n, vl(d, 0)); x = s[0] - '0'; for (ll i = 0; i < x; i++) { dp2[0][i % d]++; } x %= d; for (ll i = 1; i < n; i++) { fo(j, d) { for (ll l = 0; l < 10; l++) { dp2[i][j] += dp2[i - 1][(j - l + d * 10) % d]; dp2[i][j] %= mod; } } for (ll l = 0; l < s[i] - '0'; l++) { dp2[i][(x + l) % d]++; dp2[i][(x + l) % d] %= mod; } x += s[i] - '0'; x %= d; } kv = (dp2[n - 1][0] + (x == 0) - 1 + mod) % mod; cout << kv << el; } int main() { IOS; // cin>>t; t = 1; fo(l, t) { solve(); } return 0; }
replace
116
117
116
117
0
p03178
C++
Runtime Error
#include <bits/stdc++.h> #ifdef ONLINE_JUDGE #define endl "\n" #endif using namespace std; typedef unsigned long long int lu; typedef long long int ll; typedef long double ld; typedef vector<ll> v; typedef vector<lu> vu; typedef vector<v> vv; typedef vector<vu> vvu; typedef vector<ld> vld; typedef vector<bool> vb; typedef vector<string> vs; typedef pair<ll, ll> pll; typedef vector<set<ll>> vsll; typedef set<pair<ll, ll>> spll; typedef vector<spll> vspll; typedef vector<pll> vpll; typedef pair<lu, lu> puu; typedef vector<puu> vpuu; const ll MOD = 1e9 + 7; const ld PI = 2 * acos(0.0); const v dx = {1, -1, 0, 0}; const v dy = {0, 0, 1, -1}; #define round(x, y) ((x + y - 1) / y) #define ce(x, y) ((x + y - 1) / y) #define amax(x, y) \ if (y > x) \ x = y; #define amin(x, y) \ if (y < x) \ x = y; #define lcm(x, y) ((x) * (y) / __gcd(x, y)) #define sz(x) (ll) x.size() #define sq(x) ((x) * (x)) #define cb(x) ((x) * (x) * (x)) #define yes cout << "YES\n"; #define no cout << "NO\n"; #define yesno(f) \ if (f) \ yes else no; #define noo \ { no return; } #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() template <typename T = ll> vector<T> ga(ll n, bool oneIndexed = false) { vector<T> a = vector<T>(n + oneIndexed); for (ll i = 0; i < n; i++) { T p; cin >> p; a[i + oneIndexed] = p; } return move(a); } template <typename T, typename A> void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) { if (end == -1) end = sz(a) - 1; for (ll i = begin; i <= end; i++) { cout << a[i] << " "; } cout << endl; } void solve() { string k; ll d; cin >> k >> d; ll n = sz(k); vv dp(n, v(d, 0)); // dp[i][j] = number of ways to form number till i-th position from MSB such // that i-th posn par remainder is j for (ll i = 0; i < k[0] - '0'; i++) { dp[0][i] = 1; } ll sumsofar = k[0] - '0'; for (ll pos = 1; pos < n; pos++) { for (ll prevrem = 0; prevrem < d; prevrem++) { for (ll dig = 0; dig < 10; dig++) { ll rem = prevrem + dig; rem %= d; dp[pos][rem] += dp[pos - 1][prevrem]; dp[pos][rem] %= MOD; } } for (ll i = 0; i < k[pos] - '0'; i++) { ll rem = (sumsofar + i) % d; dp[pos][rem] += 1; dp[pos][rem] %= MOD; } sumsofar += k[pos] - '0'; } if (sumsofar % d != 0) dp[n - 1][0] -= 1; dp[n - 1][0] += MOD; dp[n - 1][0] %= MOD; cout << dp[n - 1][0]; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("file", "r", stdin); // freopen("file", "w", stdout); // ll t;cin >> t;while(t--) solve(); return 0; }
#include <bits/stdc++.h> #ifdef ONLINE_JUDGE #define endl "\n" #endif using namespace std; typedef unsigned long long int lu; typedef long long int ll; typedef long double ld; typedef vector<ll> v; typedef vector<lu> vu; typedef vector<v> vv; typedef vector<vu> vvu; typedef vector<ld> vld; typedef vector<bool> vb; typedef vector<string> vs; typedef pair<ll, ll> pll; typedef vector<set<ll>> vsll; typedef set<pair<ll, ll>> spll; typedef vector<spll> vspll; typedef vector<pll> vpll; typedef pair<lu, lu> puu; typedef vector<puu> vpuu; const ll MOD = 1e9 + 7; const ld PI = 2 * acos(0.0); const v dx = {1, -1, 0, 0}; const v dy = {0, 0, 1, -1}; #define round(x, y) ((x + y - 1) / y) #define ce(x, y) ((x + y - 1) / y) #define amax(x, y) \ if (y > x) \ x = y; #define amin(x, y) \ if (y < x) \ x = y; #define lcm(x, y) ((x) * (y) / __gcd(x, y)) #define sz(x) (ll) x.size() #define sq(x) ((x) * (x)) #define cb(x) ((x) * (x) * (x)) #define yes cout << "YES\n"; #define no cout << "NO\n"; #define yesno(f) \ if (f) \ yes else no; #define noo \ { no return; } #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() template <typename T = ll> vector<T> ga(ll n, bool oneIndexed = false) { vector<T> a = vector<T>(n + oneIndexed); for (ll i = 0; i < n; i++) { T p; cin >> p; a[i + oneIndexed] = p; } return move(a); } template <typename T, typename A> void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) { if (end == -1) end = sz(a) - 1; for (ll i = begin; i <= end; i++) { cout << a[i] << " "; } cout << endl; } void solve() { string k; ll d; cin >> k >> d; ll n = sz(k); vv dp(n, v(d, 0)); // dp[i][j] = number of ways to form number till i-th position from MSB such // that i-th posn par remainder is j for (ll i = 0; i < k[0] - '0'; i++) { dp[0][i % d] += 1; } ll sumsofar = k[0] - '0'; for (ll pos = 1; pos < n; pos++) { for (ll prevrem = 0; prevrem < d; prevrem++) { for (ll dig = 0; dig < 10; dig++) { ll rem = prevrem + dig; rem %= d; dp[pos][rem] += dp[pos - 1][prevrem]; dp[pos][rem] %= MOD; } } for (ll i = 0; i < k[pos] - '0'; i++) { ll rem = (sumsofar + i) % d; dp[pos][rem] += 1; dp[pos][rem] %= MOD; } sumsofar += k[pos] - '0'; } if (sumsofar % d != 0) dp[n - 1][0] -= 1; dp[n - 1][0] += MOD; dp[n - 1][0] %= MOD; cout << dp[n - 1][0]; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("file", "r", stdin); // freopen("file", "w", stdout); // ll t;cin >> t;while(t--) solve(); return 0; }
replace
81
82
81
82
0
p03178
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iostream> #include <numeric> #define ll long long #define mod 1000000007 #define mod1 998244353 using namespace std; // vector<int>adj[200000+1]; int cnt = 0; vector<int> cand; bool prime[100006]; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } int prob = 0; void dfs(vector<int> adj[], int v, bool visited[], stack<int> &s, bool visited2[]) { visited[v] = true; int i; int c = 1; bool f = true; for (i = 0; i < adj[v].size(); i++) { if (!visited[adj[v][i]]) { f = false; dfs(adj, adj[v][i], visited, s, visited2); } } if (adj[v].size() == 1) { s.push(v); visited2[v] = true; } } long long power(long long x, long long y) { long long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long long add(long long x, long long y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; return x; } long long mul(long long x, long long y) { return (x * 1ll * y) % mod; } long long binpow(long long x, long long y) { long long z = 1; while (y) { if (y & 1) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } long long inv(long long x) { return binpow(x, mod - 2); } long long divide(long long x, long long y) { return mul(x, inv(y)); } long long fact[100005]; void precalc() { fact[0] = 1; for (long long i = 1; i < 100005; i++) fact[i] = mul(fact[i - 1], i); } long long C(long long n, long long k) { if (n == 0) return 1; return divide(fact[n], mul(fact[k], fact[n - k])); } int dfs(bool visited[], vector<int> adj[], int v) { int c = 1; visited[v] = true; for (int i = 0; i < adj[v].size(); i++) { if (!visited[adj[v][i]]) { c += dfs(visited, adj, adj[v][i]); } } return c; } struct comp { bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) { if (p1.first == p2.first) { return p1.second < p2.second; } else return p1.first > p2.first; } }; long long sub(long long A, long long B) { return (A - B + mod) % mod; } void toposort(vector<int> &order, int v, int visited[], vector<int> adj[]) { visited[v] = 1; int i; for (i = 0; i < adj[v].size(); i++) { if (!visited[adj[v][i]]) { toposort(order, adj[v][i], visited, adj); } } order.push_back(v); } ll segtree[4 * 200000 + 1]; void build(ll A[], ll tl, ll tr, ll v) { if (tl == tr) segtree[v] = A[tl]; else { ll tm = (tl + tr) / 2; build(A, tl, tm, v * 2); build(A, tm + 1, tr, 2 * v + 1); segtree[v] = max(segtree[2 * v], segtree[2 * v + 1]); } } ll maxival(ll v, ll tl, ll tr, ll l, ll r) { if (l > r) return 0; if (l == tl && r == tr) return segtree[v]; ll tm = (tl + tr) / 2; return max(maxival(2 * v, tl, tm, l, min(r, tm)), maxival(2 * v + 1, tm + 1, tr, max(l, tm + 1), r)); } void updateval(ll v, ll tl, ll tr, ll pos, ll newval) { if (tl == tr) segtree[v] = newval; else { ll tm = (tl + tr) / 2; if (pos <= tm) updateval(2 * v, tl, tm, pos, newval); else updateval(2 * v + 1, tm + 1, tr, pos, newval); segtree[v] = max(segtree[2 * v], segtree[2 * v + 1]); } } ll dp[10000 + 2][100 + 1][2]; long long calcdp(string k, ll d, ll pos, ll rem, ll index) { ll i; if (index == k.size()) { if (rem == 0) return 1ll; return 0ll; } if (dp[index][rem][pos] != -1) return dp[index][rem][pos]; dp[index][rem][pos] = 0ll; if (pos == 0) { for (i = 0; i < k[index] - '0'; i++) { dp[index][rem][pos] = add(dp[index][rem][pos], calcdp(k, d, 1, (rem + i) % d, index + 1)); } dp[index][rem][pos] = add(dp[index][rem][pos], calcdp(k, d, 0, (rem + i) % d, index + 1)); } else { for (i = 0; i <= 9; i++) dp[index][rem][pos] = add(dp[index][rem][pos], calcdp(k, d, 1, (rem + i) % d, index + 1)); } return dp[index][rem][pos]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(10); cerr << fixed << setprecision(5); int t = 1; // SieveOfEratosthenes(100005); // precalc(); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // cin>>t; while (t--) { string k; long long d; cin >> k >> d; memset(dp, -1ll, sizeof(dp)); cout << (calcdp(k, d, 0, 0, 0) + mod - 1) % mod; } return 0; }
#include <bits/stdc++.h> #include <iostream> #include <numeric> #define ll long long #define mod 1000000007 #define mod1 998244353 using namespace std; // vector<int>adj[200000+1]; int cnt = 0; vector<int> cand; bool prime[100006]; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } int prob = 0; void dfs(vector<int> adj[], int v, bool visited[], stack<int> &s, bool visited2[]) { visited[v] = true; int i; int c = 1; bool f = true; for (i = 0; i < adj[v].size(); i++) { if (!visited[adj[v][i]]) { f = false; dfs(adj, adj[v][i], visited, s, visited2); } } if (adj[v].size() == 1) { s.push(v); visited2[v] = true; } } long long power(long long x, long long y) { long long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long long add(long long x, long long y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; return x; } long long mul(long long x, long long y) { return (x * 1ll * y) % mod; } long long binpow(long long x, long long y) { long long z = 1; while (y) { if (y & 1) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } long long inv(long long x) { return binpow(x, mod - 2); } long long divide(long long x, long long y) { return mul(x, inv(y)); } long long fact[100005]; void precalc() { fact[0] = 1; for (long long i = 1; i < 100005; i++) fact[i] = mul(fact[i - 1], i); } long long C(long long n, long long k) { if (n == 0) return 1; return divide(fact[n], mul(fact[k], fact[n - k])); } int dfs(bool visited[], vector<int> adj[], int v) { int c = 1; visited[v] = true; for (int i = 0; i < adj[v].size(); i++) { if (!visited[adj[v][i]]) { c += dfs(visited, adj, adj[v][i]); } } return c; } struct comp { bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) { if (p1.first == p2.first) { return p1.second < p2.second; } else return p1.first > p2.first; } }; long long sub(long long A, long long B) { return (A - B + mod) % mod; } void toposort(vector<int> &order, int v, int visited[], vector<int> adj[]) { visited[v] = 1; int i; for (i = 0; i < adj[v].size(); i++) { if (!visited[adj[v][i]]) { toposort(order, adj[v][i], visited, adj); } } order.push_back(v); } ll segtree[4 * 200000 + 1]; void build(ll A[], ll tl, ll tr, ll v) { if (tl == tr) segtree[v] = A[tl]; else { ll tm = (tl + tr) / 2; build(A, tl, tm, v * 2); build(A, tm + 1, tr, 2 * v + 1); segtree[v] = max(segtree[2 * v], segtree[2 * v + 1]); } } ll maxival(ll v, ll tl, ll tr, ll l, ll r) { if (l > r) return 0; if (l == tl && r == tr) return segtree[v]; ll tm = (tl + tr) / 2; return max(maxival(2 * v, tl, tm, l, min(r, tm)), maxival(2 * v + 1, tm + 1, tr, max(l, tm + 1), r)); } void updateval(ll v, ll tl, ll tr, ll pos, ll newval) { if (tl == tr) segtree[v] = newval; else { ll tm = (tl + tr) / 2; if (pos <= tm) updateval(2 * v, tl, tm, pos, newval); else updateval(2 * v + 1, tm + 1, tr, pos, newval); segtree[v] = max(segtree[2 * v], segtree[2 * v + 1]); } } ll dp[10000 + 2][100 + 1][2]; long long calcdp(string &k, ll d, ll pos, ll rem, ll index) { ll i; if (index == k.size()) { if (rem == 0) return 1ll; return 0ll; } if (dp[index][rem][pos] != -1) return dp[index][rem][pos]; dp[index][rem][pos] = 0ll; if (pos == 0) { for (i = 0; i < k[index] - '0'; i++) { dp[index][rem][pos] = add(dp[index][rem][pos], calcdp(k, d, 1, (rem + i) % d, index + 1)); } dp[index][rem][pos] = add(dp[index][rem][pos], calcdp(k, d, 0, (rem + i) % d, index + 1)); } else { for (i = 0; i <= 9; i++) dp[index][rem][pos] = add(dp[index][rem][pos], calcdp(k, d, 1, (rem + i) % d, index + 1)); } return dp[index][rem][pos]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(10); cerr << fixed << setprecision(5); int t = 1; // SieveOfEratosthenes(100005); // precalc(); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // cin>>t; while (t--) { string k; long long d; cin >> k >> d; memset(dp, -1ll, sizeof(dp)); cout << (calcdp(k, d, 0, 0, 0) + mod - 1) % mod; } return 0; }
replace
155
156
155
156
TLE
p03178
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; // #define int ll typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<short> vs; typedef vector<vs> vvs; typedef vector<vvs> vvvs; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<ld> vld; typedef vector<vld> vvld; typedef vector<vvld> vvvld; typedef vector<string> vst; typedef vector<vst> vvst; typedef pair<ld, ld> pld; typedef complex<double> base; #define inmin(a, b) a = min(a, (b)) #define inmax(a, b) a = max(a, (b)) #define mp(a, b) make_pair(a, b) #define modsum(a, b) ((a) + (b) >= M ? (a) + (b)-M : (a) + (b)) #define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b)) #define modmultiply(a, b) (((a) * (b)) % M) #define ALL(a) a.begin(), a.end() #define RALL(a) a.rbegin(), a.rend() #define sqr(x) ((x) * (x)) #define fori(i, n) for (int i = 0; i < int(n); ++i) #define cint const int & #define SZ(a) ((int)((a).size())) #define watch(x) cout << (#x) << " = " << (x) << endl; const double PI = 2 * acos(0.0); const string DIGITS = "0123456789"; const string ALPH = "abcdefghijklmnopqrstuvwxyz"; template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << "{"; fori(i, SZ(a)) out << a[i] << vector<string>{", ", "}"}[i + 1 == SZ(a)]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef MAX_HOME freopen("input.txt", "r", stdin); clock_t start = clock(); #define cerr cout #else #define cerr \ if (0) \ cerr #endif cout << setprecision(12) << fixed; smain(); #ifdef MAX_HOME cout << "\n\n\n\n"; cout << "TOTAL EXECUTION TIME: " << float(clock() - start) / CLOCKS_PER_SEC << endl; #endif } const int N = 1009; const int D = 109; const int T = 10; const ll M = 1e9 + 7; int dp[N][D][T + 1]; vi a; int d; ll dfs(int ind, int rem) { if (ind == -1) return 0; ll ret = 0; for (int dig = 0; dig < a[ind]; ++dig) { ret += dp[ind][rem][dig]; } rem = (rem - a[ind] % d + d) % d; return ret + dfs(ind - 1, rem); } void smain() { string s; cin >> s; a.resize(s.size()); fori(i, a.size()) a[i] = s[i] - '0'; reverse(ALL(a)); int r = 1; for (int i = 0; r; ++i) { if (i == a.size()) a.push_back(0); a[i] += r; r = a[i] / 10; a[i] %= 10; } cin >> d; int n = a.size(); fori(dig, T) { dp[0][dig % d][dig] = 1; dp[0][dig % d][T] = (dp[0][dig % d][T] + 1) % M; } for (int i = 1; i < n; ++i) { for (int j = 0; j < d; ++j) { for (int dig = 0; dig < T; ++dig) { dp[i][j][dig] = dp[i - 1][(d + j - dig % d) % d][T]; dp[i][j][T] = (dp[i][j][T] + dp[i][j][dig]) % M; } } } cout << (dfs(n - 1, 0) + M - 1) % M; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; // #define int ll typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<short> vs; typedef vector<vs> vvs; typedef vector<vvs> vvvs; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<ld> vld; typedef vector<vld> vvld; typedef vector<vvld> vvvld; typedef vector<string> vst; typedef vector<vst> vvst; typedef pair<ld, ld> pld; typedef complex<double> base; #define inmin(a, b) a = min(a, (b)) #define inmax(a, b) a = max(a, (b)) #define mp(a, b) make_pair(a, b) #define modsum(a, b) ((a) + (b) >= M ? (a) + (b)-M : (a) + (b)) #define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b)) #define modmultiply(a, b) (((a) * (b)) % M) #define ALL(a) a.begin(), a.end() #define RALL(a) a.rbegin(), a.rend() #define sqr(x) ((x) * (x)) #define fori(i, n) for (int i = 0; i < int(n); ++i) #define cint const int & #define SZ(a) ((int)((a).size())) #define watch(x) cout << (#x) << " = " << (x) << endl; const double PI = 2 * acos(0.0); const string DIGITS = "0123456789"; const string ALPH = "abcdefghijklmnopqrstuvwxyz"; template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << "{"; fori(i, SZ(a)) out << a[i] << vector<string>{", ", "}"}[i + 1 == SZ(a)]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef MAX_HOME freopen("input.txt", "r", stdin); clock_t start = clock(); #define cerr cout #else #define cerr \ if (0) \ cerr #endif cout << setprecision(12) << fixed; smain(); #ifdef MAX_HOME cout << "\n\n\n\n"; cout << "TOTAL EXECUTION TIME: " << float(clock() - start) / CLOCKS_PER_SEC << endl; #endif } const int N = 10009; const int D = 109; const int T = 10; const ll M = 1e9 + 7; int dp[N][D][T + 1]; vi a; int d; ll dfs(int ind, int rem) { if (ind == -1) return 0; ll ret = 0; for (int dig = 0; dig < a[ind]; ++dig) { ret += dp[ind][rem][dig]; } rem = (rem - a[ind] % d + d) % d; return ret + dfs(ind - 1, rem); } void smain() { string s; cin >> s; a.resize(s.size()); fori(i, a.size()) a[i] = s[i] - '0'; reverse(ALL(a)); int r = 1; for (int i = 0; r; ++i) { if (i == a.size()) a.push_back(0); a[i] += r; r = a[i] / 10; a[i] %= 10; } cin >> d; int n = a.size(); fori(dig, T) { dp[0][dig % d][dig] = 1; dp[0][dig % d][T] = (dp[0][dig % d][T] + 1) % M; } for (int i = 1; i < n; ++i) { for (int j = 0; j < d; ++j) { for (int dig = 0; dig < T; ++dig) { dp[i][j][dig] = dp[i - 1][(d + j - dig % d) % d][T]; dp[i][j][T] = (dp[i][j][T] + dp[i][j][dig]) % M; } } } cout << (dfs(n - 1, 0) + M - 1) % M; }
replace
77
78
77
78
0
p03178
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // MY TOOLS #define let long long int #define async \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); // int dp[90001][101][2]; int mod = 1e9 + 7; void self_add(int &a, int b) { a += b; if (a >= mod) a -= mod; } int SIZE; int generate_top_down(string num, int idx, int sum, int D, int bound) { if (idx == SIZE) return (sum == 0) ? 1 : 0; int check = dp[idx][sum][bound], N = (num[idx] - '0'); if (check != -1) return check; int up_to = 9; if (bound == 1) up_to = N; int payload = 0, new_bound; for (int digit = 0; digit <= up_to; ++digit) { new_bound = (N == digit) ? bound : 0; self_add(payload, generate_top_down(num, idx + 1, (sum + digit) % D, D, new_bound)); } return dp[idx][sum][bound] = payload; } // main // write your code here int main() { async; memset(dp, -1, sizeof(dp)); string K; int D; cin >> K >> D; SIZE = K.size(); int ans = generate_top_down(K, 0, 0, D, 1) - 1; if (ans == -1) { cout << mod - 1; } else { cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; // MY TOOLS #define let long long int #define async \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); // int dp[10001][101][2]; int mod = 1e9 + 7; void self_add(int &a, int b) { a += b; if (a >= mod) a -= mod; } int SIZE; int generate_top_down(string num, int idx, int sum, int D, int bound) { if (idx == SIZE) return (sum == 0) ? 1 : 0; int check = dp[idx][sum][bound], N = (num[idx] - '0'); if (check != -1) return check; int up_to = 9; if (bound == 1) up_to = N; int payload = 0, new_bound; for (int digit = 0; digit <= up_to; ++digit) { new_bound = (N == digit) ? bound : 0; self_add(payload, generate_top_down(num, idx + 1, (sum + digit) % D, D, new_bound)); } return dp[idx][sum][bound] = payload; } // main // write your code here int main() { async; memset(dp, -1, sizeof(dp)); string K; int D; cin >> K >> D; SIZE = K.size(); int ans = generate_top_down(K, 0, 0, D, 1) - 1; if (ans == -1) { cout << mod - 1; } else { cout << ans; } return 0; }
replace
11
12
11
12
TLE
p03178
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> using namespace std; typedef unsigned int uint; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<string> vs; typedef vector<ii> vii; typedef vector<vi> vvi; typedef map<int, int> mii; typedef map<string, int> msi; typedef map<int, string> mis; typedef pair<int, ii> edge; #define sc(x) scanf("%d", &x) #define scl(x) scanf("%lld", &x) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define mem(arr, val) memset(arr, val, sizeof arr) #define sz(v) (int)v.size() #define rsz(v) v.resize #define clr(v) v.clear() #define rev(v) reverse(all(v)) #define lop(i, end) for (int i = 0; i < end; i++) #define rlop(i, start) for (int i = start - 1; i >= 0; i--) #define loop(i, start, end) for (int i = start; i < end; i++) #define rloop(i, start, end) for (int i = start - 1; i >= end; i--) #define PB push_back #define pb pop_back #define mP make_pair #define f first #define s second const ll OO = (4e18) + 9; const int MOD = (1e9) + 7; const int oo = 2147483647; const double EPS = 1e-8; const double PI = acos(-1.0); // enum dir { r, d, l, u }; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; // enum diR { d, r, u, l , dr, ur, dl, ul }; int dX[] = {1, 0, -1, 0, 1, -1, 1, -1}; int dY[] = {0, 1, 0, -1, 1, 1, -1, -1}; string abc = "abcdefghijklmnopqrstuvwxyz"; string vowels = "aeiou"; // and sometimes "aeiouy" int dcmp(ld d1, ld d2) { return fabs(d1 - d2) <= EPS ? 0 : d1 > d2 ? 1 : -1; } // Compare Double Numbers ll gcd(ll x, ll y) { return !y ? x : gcd(y, x % y); } ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); } // convert index from 2D matrix to 1D array. (Zero-indexed) int ord(int r, int c, int nCol) { return (nCol * r) + c + 1; } // convert index from 1D array to 2D matrix . (One-indexed) ii rord(int n, int nCol) { int r = n % nCol, c = n / nCol; if (n % nCol == 0) c--, r = nCol; return ii(r, c); } ll poww(ll num, ll p, ll md = MOD) { num %= md; ll ret = 1; while (p > 0) { if (p & 1) ret = (ret * num) % md; num = (num * num) % md; p >>= 1; } return ret; } ll nCr(ll n, ll r) { if (n < r) return 0; if (r == 0) return 1; return n * nCr(n - 1, r - 1) / r; } const int N = 1100; int dp[N][110][2][2]; int D; string l, r; ll solve(int idx, int rem, int ls, int rs) { if (idx >= sz(r)) return rem == 0; int &ret = dp[idx][rem][ls][rs]; if (~ret) return ret; ret = 0; lop(i, 10) { if (!ls && i < l[idx] - '0') continue; if (!rs && i > r[idx] - '0') continue; ret = (ret + solve(idx + 1, (rem + i) % D, ls || i > l[idx] - '0', rs || i < r[idx] - '0') % MOD) % MOD; } return ret; } int main() { #ifndef ONLINE_JUDGE #else // freopen("pyramid.in", "r", stdin); #endif // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> r >> D; lop(i, sz(r) - 1) l += '0'; l += '1'; mem(dp, -1); cout << solve(0, 0, 0, 0) << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> using namespace std; typedef unsigned int uint; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<string> vs; typedef vector<ii> vii; typedef vector<vi> vvi; typedef map<int, int> mii; typedef map<string, int> msi; typedef map<int, string> mis; typedef pair<int, ii> edge; #define sc(x) scanf("%d", &x) #define scl(x) scanf("%lld", &x) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define mem(arr, val) memset(arr, val, sizeof arr) #define sz(v) (int)v.size() #define rsz(v) v.resize #define clr(v) v.clear() #define rev(v) reverse(all(v)) #define lop(i, end) for (int i = 0; i < end; i++) #define rlop(i, start) for (int i = start - 1; i >= 0; i--) #define loop(i, start, end) for (int i = start; i < end; i++) #define rloop(i, start, end) for (int i = start - 1; i >= end; i--) #define PB push_back #define pb pop_back #define mP make_pair #define f first #define s second const ll OO = (4e18) + 9; const int MOD = (1e9) + 7; const int oo = 2147483647; const double EPS = 1e-8; const double PI = acos(-1.0); // enum dir { r, d, l, u }; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; // enum diR { d, r, u, l , dr, ur, dl, ul }; int dX[] = {1, 0, -1, 0, 1, -1, 1, -1}; int dY[] = {0, 1, 0, -1, 1, 1, -1, -1}; string abc = "abcdefghijklmnopqrstuvwxyz"; string vowels = "aeiou"; // and sometimes "aeiouy" int dcmp(ld d1, ld d2) { return fabs(d1 - d2) <= EPS ? 0 : d1 > d2 ? 1 : -1; } // Compare Double Numbers ll gcd(ll x, ll y) { return !y ? x : gcd(y, x % y); } ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); } // convert index from 2D matrix to 1D array. (Zero-indexed) int ord(int r, int c, int nCol) { return (nCol * r) + c + 1; } // convert index from 1D array to 2D matrix . (One-indexed) ii rord(int n, int nCol) { int r = n % nCol, c = n / nCol; if (n % nCol == 0) c--, r = nCol; return ii(r, c); } ll poww(ll num, ll p, ll md = MOD) { num %= md; ll ret = 1; while (p > 0) { if (p & 1) ret = (ret * num) % md; num = (num * num) % md; p >>= 1; } return ret; } ll nCr(ll n, ll r) { if (n < r) return 0; if (r == 0) return 1; return n * nCr(n - 1, r - 1) / r; } const int N = 11000; int dp[N][110][2][2]; int D; string l, r; ll solve(int idx, int rem, int ls, int rs) { if (idx >= sz(r)) return rem == 0; int &ret = dp[idx][rem][ls][rs]; if (~ret) return ret; ret = 0; lop(i, 10) { if (!ls && i < l[idx] - '0') continue; if (!rs && i > r[idx] - '0') continue; ret = (ret + solve(idx + 1, (rem + i) % D, ls || i > l[idx] - '0', rs || i < r[idx] - '0') % MOD) % MOD; } return ret; } int main() { #ifndef ONLINE_JUDGE #else // freopen("pyramid.in", "r", stdin); #endif // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> r >> D; lop(i, sz(r) - 1) l += '0'; l += '1'; mem(dp, -1); cout << solve(0, 0, 0, 0) << endl; return 0; }
replace
91
92
91
92
0
p03178
C++
Runtime Error
#include <bits/stdc++.h> #define int long long int #define L_T_S_K \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) using namespace std; const int N = 1e6 + 700; const int mod = 1e9 + 7; int a, b, k, n; string lim; int dp[24][101][2]; int cntDigit(int n) { if (!n) return 0; return 1 + cntDigit(n / 10); } int add(int x, int y) { return ((x % mod) + (y % mod)) % mod; } int sub(int x, int y) { return ((x % mod) - (y % mod) + mod) % mod; } int solve(int i, int mod, int flag) { if (i == n) { if (!mod) return 1; return 0; } if (dp[i][mod][flag] != -1) return dp[i][mod][flag]; int ans = 0; int limit = 9; if (!flag) { limit = lim[i] - '0'; } for (int j = 0; j <= limit; j++) { int f1 = flag; if (!f1 and j < limit) f1 = 1; ans = add(ans, solve(i + 1, (mod + j) % k, f1)); } dp[i][mod][flag] = ans; return ans; } int32_t main() { L_T_S_K; int t = 1; // cin >> t; while (t--) { cin >> lim >> k; memset(dp, -1, sizeof dp); n = lim.size(); int p = solve(0, 0, 0); cout << sub(p, 1) << "\n"; } return 0; }
#include <bits/stdc++.h> #define int long long int #define L_T_S_K \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) using namespace std; const int N = 1e6 + 700; const int mod = 1e9 + 7; int a, b, k, n; string lim; int dp[10005][101][2]; int cntDigit(int n) { if (!n) return 0; return 1 + cntDigit(n / 10); } int add(int x, int y) { return ((x % mod) + (y % mod)) % mod; } int sub(int x, int y) { return ((x % mod) - (y % mod) + mod) % mod; } int solve(int i, int mod, int flag) { if (i == n) { if (!mod) return 1; return 0; } if (dp[i][mod][flag] != -1) return dp[i][mod][flag]; int ans = 0; int limit = 9; if (!flag) { limit = lim[i] - '0'; } for (int j = 0; j <= limit; j++) { int f1 = flag; if (!f1 and j < limit) f1 = 1; ans = add(ans, solve(i + 1, (mod + j) % k, f1)); } dp[i][mod][flag] = ans; return ans; } int32_t main() { L_T_S_K; int t = 1; // cin >> t; while (t--) { cin >> lim >> k; memset(dp, -1, sizeof dp); n = lim.size(); int p = solve(0, 0, 0); cout << sub(p, 1) << "\n"; } return 0; }
replace
11
12
11
12
0
p03178
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double ll mod = 1e9 + 7; int main() { /*#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output1.txt", "w", stdout); #endif*/ // cout << std::fixed; // cout << std::setprecision(10); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; ll m; cin >> s >> m; int n = s.size(); ll dp[n - 1][m]; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m; j++) { dp[i][j] = 0; vector<pair<int, int>> v(n + 1); } } for (int r = 1; r <= 9; r++) { dp[0][r % m]++; } for (int i = 1; i < n - 1; i++) { for (int j = 0; j < m; j++) { for (int r = 0; r <= 9; r++) { ll k = (j + r % m) % m; dp[i][k] = (dp[i][k] + dp[i - 1][j]) % mod; } } } ll ans = 0; for (int i = 0; i <= n - 2; i++) { ans = (ans + dp[i][0]) % mod; } ll dp1[n][m][2]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < 2; k++) { dp1[i][j][k] = 0; } } } for (int r = 1; r < (int)s[0] - 48; r++) { dp1[0][r % m][0]++; } dp1[0][((int)(s[0] - 48)) % m][1]++; for (int i = 1; i < n; i++) { for (int j = 0; j < m; j++) { int r = 0; for (; r < (s[i] - 48); r++) { ll k = (j + r % m) % m; dp1[i][k][0] = (dp1[i][k][0] + (dp1[i - 1][j][0] + dp1[i - 1][j][1]) % mod) % mod; } for (; r <= 9; r++) { ll k = (j + r % m) % m; dp1[i][k][0] = (dp1[i][k][0] + dp1[i - 1][j][0]) % mod; if (r == (s[i] - 48)) dp1[i][k][1] = (dp1[i][k][1] + dp1[i - 1][j][1]) % mod; } } } ans = (ans + (dp1[n - 1][0][0] + dp1[n - 1][0][1]) % mod) % mod; cout << ans; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double ll mod = 1e9 + 7; int main() { /*#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output1.txt", "w", stdout); #endif*/ // cout << std::fixed; // cout << std::setprecision(10); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; ll m; cin >> s >> m; int n = s.size(); ll dp[n - 1][m]; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m; j++) { dp[i][j] = 0; } } for (int r = 1; r <= 9; r++) { dp[0][r % m]++; } for (int i = 1; i < n - 1; i++) { for (int j = 0; j < m; j++) { for (int r = 0; r <= 9; r++) { ll k = (j + r % m) % m; dp[i][k] = (dp[i][k] + dp[i - 1][j]) % mod; } } } ll ans = 0; for (int i = 0; i <= n - 2; i++) { ans = (ans + dp[i][0]) % mod; } ll dp1[n][m][2]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < 2; k++) { dp1[i][j][k] = 0; } } } for (int r = 1; r < (int)s[0] - 48; r++) { dp1[0][r % m][0]++; } dp1[0][((int)(s[0] - 48)) % m][1]++; for (int i = 1; i < n; i++) { for (int j = 0; j < m; j++) { int r = 0; for (; r < (s[i] - 48); r++) { ll k = (j + r % m) % m; dp1[i][k][0] = (dp1[i][k][0] + (dp1[i - 1][j][0] + dp1[i - 1][j][1]) % mod) % mod; } for (; r <= 9; r++) { ll k = (j + r % m) % m; dp1[i][k][0] = (dp1[i][k][0] + dp1[i - 1][j][0]) % mod; if (r == (s[i] - 48)) dp1[i][k][1] = (dp1[i][k][1] + dp1[i - 1][j][1]) % mod; } } } ans = (ans + (dp1[n - 1][0][0] + dp1[n - 1][0][1]) % mod) % mod; cout << ans; }
delete
23
24
23
23
0
p03179
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define fi first #define se second #define mp make_pair #define itrfor(itr, A) for (auto itr = A.begin(); itr != A.end(); itr++) template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>; typedef long long llong; char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; #define Sort(a) sort(a.begin(), a.end()); #define Reverse(a) reverse(a.begin(), a.end()); #define print(a) cout << a << endl; #define MOD llong(1e9 + 7) #define MAX int(2 * 1e5 + 5) #define debug(x) cout << #x << " = " << (x) << endl; #define pi acos(-1.0) #define int llong #define INF llong(1e17) template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void myprint(int *A, int A_num) { REP(i, A_num) cout << A[i] << " "; cout << endl; } const int n_max = 305; signed main() { int n; string s; cin >> n >> s; int dp[n_max][n_max]; REP(i, n + 1) REP(j, n + 1) dp[i][j] = 0; REP(i, n) dp[0][i] = 1; FOR(i, 1, n) { if (s[i - 1] == '<') { int summ = 0; REPR(j, n - 1) { if (i + 1 + j > n) continue; summ += dp[i - 1][j + 1]; summ %= MOD; dp[i][j] = summ; } } else { int summ = 0; REP(j, n) { if (i + 1 + j > n) continue; summ += dp[i - 1][j]; summ %= MOD; dp[i][j] = summ; } } } cout << dp[n - 1][0] << endl; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define fi first #define se second #define mp make_pair #define itrfor(itr, A) for (auto itr = A.begin(); itr != A.end(); itr++) template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>; typedef long long llong; char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; #define Sort(a) sort(a.begin(), a.end()); #define Reverse(a) reverse(a.begin(), a.end()); #define print(a) cout << a << endl; #define MOD llong(1e9 + 7) #define MAX int(2 * 1e5 + 5) #define debug(x) cout << #x << " = " << (x) << endl; #define pi acos(-1.0) #define int llong #define INF llong(1e17) template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void myprint(int *A, int A_num) { REP(i, A_num) cout << A[i] << " "; cout << endl; } const int n_max = 3005; signed main() { int n; string s; cin >> n >> s; int dp[n_max][n_max]; REP(i, n + 1) REP(j, n + 1) dp[i][j] = 0; REP(i, n) dp[0][i] = 1; FOR(i, 1, n) { if (s[i - 1] == '<') { int summ = 0; REPR(j, n - 1) { if (i + 1 + j > n) continue; summ += dp[i - 1][j + 1]; summ %= MOD; dp[i][j] = summ; } } else { int summ = 0; REP(j, n) { if (i + 1 + j > n) continue; summ += dp[i - 1][j]; summ %= MOD; dp[i][j] = summ; } } } cout << dp[n - 1][0] << endl; }
replace
47
48
47
48
0
p03179
C++
Runtime Error
#include <algorithm> #include <cassert> #include <iostream> #include <vector> using namespace std; #define rep(i, a, b) for (auto i = (a); i < (b); ++i) #define per(i, a, b) for (auto i = (b)-1; i >= (a); --i) #define trav(x, v) for (auto &x : v) #define sz(x) int((x).size()) #define eb(x...) emplace_back(x) #define ff first #define ss second using ll = int64_t; const int M = 1e9 + 7; template <class T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } using vi = vector<int>; using vvi = vector<vi>; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi f(n), s(n + 1); string x; cin >> x; f[0] = 1; rep(i, 0, n - 1) { vi g(n); rep(i, 0, n) s[i + 1] = (s[i] + f[i]) % M; if (x[i] == '<') rep(j, 0, i + 2) g[j] = s[j]; else rep(j, 0, i + 2) g[j] = (s[n] - s[j]) % M; swap(f, g); } int ans = 0; trav(z, f) ans = (ans + z + M) % M; assert(ans >= 0); cout << ans; }
#include <algorithm> #include <cassert> #include <iostream> #include <vector> using namespace std; #define rep(i, a, b) for (auto i = (a); i < (b); ++i) #define per(i, a, b) for (auto i = (b)-1; i >= (a); --i) #define trav(x, v) for (auto &x : v) #define sz(x) int((x).size()) #define eb(x...) emplace_back(x) #define ff first #define ss second using ll = int64_t; const int M = 1e9 + 7; template <class T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } using vi = vector<int>; using vvi = vector<vi>; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi f(n), s(n + 1); string x; cin >> x; f[0] = 1; rep(i, 0, n - 1) { vi g(n); rep(i, 0, n) s[i + 1] = (s[i] + f[i]) % M; if (x[i] == '<') rep(j, 0, i + 2) g[j] = s[j]; else rep(j, 0, i + 2) g[j] = (s[n] - s[j]) % M; swap(f, g); } int ans = 0; trav(z, f)(ans += z) %= M; if (ans < 0) ans += M; cout << ans; }
replace
46
48
46
49
0
p03179
C++
Runtime Error
#include <stdio.h> const int MAXN = 3005; const int MOD = (int)1e9 + 7; char s[MAXN]; int dp[MAXN][MAXN]; int get_ans(int i, int l, int r) { int ans = dp[i][r]; if (l > 0) ans -= dp[i][l - 1]; return (ans % MOD + MOD) % MOD; } int main(void) { int n; scanf(" %d", &n); scanf(" %s", s); dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (s[i - 1] == '<') { dp[i][j] += get_ans(i - 1, 0, j - 1); } else { dp[i][j] += get_ans(i - 1, j, n - 1); } dp[i][j] %= MOD; } for (int j = 1; j < n; j++) { dp[i][j] += dp[i][j - 1]; dp[i][j] %= MOD; } } printf("%d\n", dp[n - 1][n - 1]); return 0; }
#include <stdio.h> const int MAXN = 3005; const int MOD = (int)1e9 + 7; char s[MAXN]; int dp[MAXN][MAXN]; int get_ans(int i, int l, int r) { int ans = dp[i][r]; if (l > 0) ans -= dp[i][l - 1]; return (ans % MOD + MOD) % MOD; } int main(void) { int n; scanf(" %d", &n); scanf(" %s", s); dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (i == 0) break; if (s[i - 1] == '<') { dp[i][j] += get_ans(i - 1, 0, j - 1); } else { dp[i][j] += get_ans(i - 1, j, n - 1); } dp[i][j] %= MOD; } for (int j = 1; j < n; j++) { dp[i][j] += dp[i][j - 1]; dp[i][j] %= MOD; } } printf("%d\n", dp[n - 1][n - 1]); return 0; }
insert
25
25
25
28
0
p03179
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define f first #define s second #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() #define sz(v) (int)v.size() #define MOO(i, a, b) for (int i = a; i < b; i++) #define M00(i, a) for (int i = 0; i < a; i++) #define MOOd(i, a, b) for (int i = (b)-1; i >= a; i--) #define M00d(i, a) for (int i = (a)-1; i >= 0; i--) #define FAST \ ios::sync_with_stdio(0); \ cin.tie(0); #define finish(x) return cout << x << '\n', 0; #define dbg(x) cerr << ">>> " << #x << " = " << x << "\n"; #define _ << " _ " << typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> pi; typedef pair<ld, ld> pd; typedef vector<ll, ll> vll; typedef pair<ll, ll> pll; // DP[i][j] = placed i pieces, of n-i left, (0 to j) are greater than last // placed ll DP[3010][3010]; ll MOD = 1e9 + 7; int main() { FAST mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; string s; cin >> n >> s; DP[n][0] = 1; for (int i = n - 1; i > 0; i--) { for (int j = 0; j <= n - i; j++) { if (j == 0) DP[i][j] = 0; else DP[i][j] = DP[i][j - 1]; if (s[i - 1] == '<') { dbg("yes"); if (j != 0) DP[i][j] += DP[i + 1][j - 1]; } else { DP[i][j] += DP[i + 1][n - i - 1]; if (j != 0) DP[i][j] -= DP[i + 1][j - 1]; } DP[i][j] += MOD; DP[i][j] %= MOD; } } finish(DP[1][n - 1] % MOD); }
#include <bits/stdc++.h> using namespace std; #define f first #define s second #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() #define sz(v) (int)v.size() #define MOO(i, a, b) for (int i = a; i < b; i++) #define M00(i, a) for (int i = 0; i < a; i++) #define MOOd(i, a, b) for (int i = (b)-1; i >= a; i--) #define M00d(i, a) for (int i = (a)-1; i >= 0; i--) #define FAST \ ios::sync_with_stdio(0); \ cin.tie(0); #define finish(x) return cout << x << '\n', 0; #define dbg(x) cerr << ">>> " << #x << " = " << x << "\n"; #define _ << " _ " << typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> pi; typedef pair<ld, ld> pd; typedef vector<ll, ll> vll; typedef pair<ll, ll> pll; // DP[i][j] = placed i pieces, of n-i left, (0 to j) are greater than last // placed ll DP[3010][3010]; ll MOD = 1e9 + 7; int main() { FAST mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; string s; cin >> n >> s; DP[n][0] = 1; for (int i = n - 1; i > 0; i--) { for (int j = 0; j <= n - i; j++) { if (j == 0) DP[i][j] = 0; else DP[i][j] = DP[i][j - 1]; if (s[i - 1] == '<') { if (j != 0) DP[i][j] += DP[i + 1][j - 1]; } else { DP[i][j] += DP[i + 1][n - i - 1]; if (j != 0) DP[i][j] -= DP[i + 1][j - 1]; } DP[i][j] += MOD; DP[i][j] %= MOD; } } finish(DP[1][n - 1] % MOD); }
delete
52
53
52
52
TLE
p03179
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ull mod = 1e9 + 7; #define REP(i, n) for (int i = 0; i < (int)n; ++i) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <typename T> void vprint(T &v) { REP(i, v.size()) { cout << v[i] << " "; } cout << endl; } const ll N_MAX = 3030; struct RAQSB { ll N; ll sN; ll data[N_MAX]; ll bucket[N_MAX]; RAQSB(ll n) { sN = (ll)sqrt(n) + 1; N = sN * sN; REP(i, N_MAX) { data[i] = 0; bucket[i] = 0; } } void add(ll s, ll t, ll x) { // add x to range [s, t) ll ss = s / sN; ll tt = t / sN; if (ss == tt) { for (int i = s; i < t; i++) { data[i] += x; data[i] %= mod; } } else { for (int i = s; i < (ss + 1) * sN; i++) { data[i] += x; data[i] %= mod; } for (int i = tt * sN; i < t; i++) { data[i] += x; data[i] %= mod; } for (int i = ss + 1; i < tt; i++) { bucket[i] += x; bucket[i] += mod; } } } ll get_val(ll i) { return (data[i] + bucket[i / sN]) % mod; } }; int main() { ll N; string S; cin >> N >> S; ll ma = 303; ll DP[ma][ma]; REP(i, ma) REP(j, ma) DP[i][j] = 0; REP(j, N) DP[0][j] = 1; REP(i, N - 1) { RAQSB raqsb(N + 10); if (S[i] == '<') { REP(j, N) { raqsb.add(0, j, DP[i][j]); /* REP(k, j){ DP[i+1][k] += DP[i][j]; DP[i+1][k] %= mod; } */ } } else { REP(j, N) { raqsb.add(j, N - i - 1, DP[i][j]); /* for(int k=j;k<=N-i-2;k++){ DP[i+1][k] += DP[i][j]; DP[i+1][k] %= mod; } */ } } REP(j, N) DP[i + 1][j] = raqsb.get_val(j); } cout << DP[N - 1][0] << endl; /* REP(i, N){ REP(j, N){ cout << DP[i][j] << " "; } cout << endl; } */ return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ull mod = 1e9 + 7; #define REP(i, n) for (int i = 0; i < (int)n; ++i) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <typename T> void vprint(T &v) { REP(i, v.size()) { cout << v[i] << " "; } cout << endl; } const ll N_MAX = 3030; struct RAQSB { ll N; ll sN; ll data[N_MAX]; ll bucket[N_MAX]; RAQSB(ll n) { sN = (ll)sqrt(n) + 1; N = sN * sN; REP(i, N_MAX) { data[i] = 0; bucket[i] = 0; } } void add(ll s, ll t, ll x) { // add x to range [s, t) ll ss = s / sN; ll tt = t / sN; if (ss == tt) { for (int i = s; i < t; i++) { data[i] += x; data[i] %= mod; } } else { for (int i = s; i < (ss + 1) * sN; i++) { data[i] += x; data[i] %= mod; } for (int i = tt * sN; i < t; i++) { data[i] += x; data[i] %= mod; } for (int i = ss + 1; i < tt; i++) { bucket[i] += x; bucket[i] += mod; } } } ll get_val(ll i) { return (data[i] + bucket[i / sN]) % mod; } }; int main() { ll N; string S; cin >> N >> S; ll ma = 3030; ll DP[ma][ma]; REP(i, ma) REP(j, ma) DP[i][j] = 0; REP(j, N) DP[0][j] = 1; REP(i, N - 1) { RAQSB raqsb(N + 10); if (S[i] == '<') { REP(j, N) { raqsb.add(0, j, DP[i][j]); /* REP(k, j){ DP[i+1][k] += DP[i][j]; DP[i+1][k] %= mod; } */ } } else { REP(j, N) { raqsb.add(j, N - i - 1, DP[i][j]); /* for(int k=j;k<=N-i-2;k++){ DP[i+1][k] += DP[i][j]; DP[i+1][k] %= mod; } */ } } REP(j, N) DP[i + 1][j] = raqsb.get_val(j); } cout << DP[N - 1][0] << endl; /* REP(i, N){ REP(j, N){ cout << DP[i][j] << " "; } cout << endl; } */ return 0; }
replace
71
72
71
72
0
p03179
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const int maxn = 3010; ll dp[maxn][maxn]; int main() { ios_base::sync_with_stdio(false); int n; string s; cin >> n >> s; dp[1][1] = 1; for (int i = 2; i <= n; i++) { ll pref[i]; pref[0] = 0; for (int j = 1; j < i; j++) { pref[j] = (pref[j - 1] + dp[i - 1][j]) % mod; } for (int j = 1; j <= n; j++) { int l, r; if (s[i - 2] == '<') { l = 1, r = j - 1; } else { l = j; r = i - 1; } if (l <= r) { dp[i][j] = (pref[r] - pref[l - 1] + mod) % mod; } } } ll ans = 0; for (int i = 1; i <= n; i++) { ans = (ans + dp[n][i]) % mod; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const int maxn = 3010; ll dp[maxn][maxn]; int main() { ios_base::sync_with_stdio(false); int n; string s; cin >> n >> s; dp[1][1] = 1; for (int i = 2; i <= n; i++) { ll pref[i]; pref[0] = 0; for (int j = 1; j < i; j++) { pref[j] = (pref[j - 1] + dp[i - 1][j]) % mod; } for (int j = 1; j <= i; j++) { int l, r; if (s[i - 2] == '<') { l = 1, r = j - 1; } else { l = j; r = i - 1; } if (l <= r) { dp[i][j] = (pref[r] - pref[l - 1] + mod) % mod; } } } ll ans = 0; for (int i = 1; i <= n; i++) { ans = (ans + dp[n][i]) % mod; } cout << ans; return 0; }
replace
21
22
21
22
0
p03179
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int64 P = 1000000007; // Verified: ABC133E, ABC132D, ABC130E struct FiniteField { private: int64 x; public: FiniteField(int64 input_x) : x(input_x) {} FiniteField() : x(0) {} int64 Value() { return x; } inline FiniteField operator+(FiniteField o) { FiniteField r(*this); r += o; return r; } inline FiniteField operator-(FiniteField o) { FiniteField r(*this); r -= o; return r; } inline FiniteField operator*(FiniteField o) { FiniteField r(*this); r *= o; return r; } inline FiniteField operator/(FiniteField o) { FiniteField r(*this); r /= o; return r; } inline void operator+=(FiniteField o) { x = (x + o.x) % P; } inline void operator-=(FiniteField o) { x = (x + P - o.x) % P; } inline void operator*=(FiniteField o) { x = (x * o.x) % P; } void operator/=(FiniteField o) { int64 p = P - 2; while (p) { if (p % 2) { *this *= o; } o *= o; p /= 2; } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; string s; cin >> n >> s; vector<vector<FiniteField>> dp1(n); vector<vector<FiniteField>> dp2(n); dp1[0] = vector<FiniteField>(n); for (int j = 0; j < n; j++) { dp1[0][j] = 1; } dp2[0] = vector<FiniteField>(n); dp2[0][0] = dp1[0][0]; for (int j = 1; j < n; j++) { dp2[0][j] = dp2[0][j - 1] + dp1[0][j]; } for (int i = 0; i < n - 1; i++) { dp1[i + 1] = vector<FiniteField>(n - (i + 1)); dp2[i + 1] = vector<FiniteField>(n - (i + 1)); for (int j = 0; j < n - i; j++) { if (s[i] == '<') { dp1[i + 1][j] = dp2[i][j]; } else { // s[i] == '>' dp1[i + 1][j] = dp2[i][n - i - 1] - dp2[i][j]; } } dp2[i + 1][0] = dp1[i + 1][0]; for (int j = 1; j < n - (i + 1); j++) { dp2[i + 1][j] = dp2[i + 1][j - 1] + dp1[i + 1][j]; } // cout << "i: " << i + 1 << endl; // for (int j = 0; j < n - (i + 1); j++) { // cout << dp1[i + 1][j].Value() << " "; // } // cout << endl; // for (int j = 0; j < n - (i + 1); j++) { // cout << dp2[i + 1][j].Value() << " "; // } // cout << endl; } cout << dp1[n - 1][0].Value() << endl; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int64 P = 1000000007; // Verified: ABC133E, ABC132D, ABC130E struct FiniteField { private: int64 x; public: FiniteField(int64 input_x) : x(input_x) {} FiniteField() : x(0) {} int64 Value() { return x; } inline FiniteField operator+(FiniteField o) { FiniteField r(*this); r += o; return r; } inline FiniteField operator-(FiniteField o) { FiniteField r(*this); r -= o; return r; } inline FiniteField operator*(FiniteField o) { FiniteField r(*this); r *= o; return r; } inline FiniteField operator/(FiniteField o) { FiniteField r(*this); r /= o; return r; } inline void operator+=(FiniteField o) { x = (x + o.x) % P; } inline void operator-=(FiniteField o) { x = (x + P - o.x) % P; } inline void operator*=(FiniteField o) { x = (x * o.x) % P; } void operator/=(FiniteField o) { int64 p = P - 2; while (p) { if (p % 2) { *this *= o; } o *= o; p /= 2; } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; string s; cin >> n >> s; vector<vector<FiniteField>> dp1(n); vector<vector<FiniteField>> dp2(n); dp1[0] = vector<FiniteField>(n); for (int j = 0; j < n; j++) { dp1[0][j] = 1; } dp2[0] = vector<FiniteField>(n); dp2[0][0] = dp1[0][0]; for (int j = 1; j < n; j++) { dp2[0][j] = dp2[0][j - 1] + dp1[0][j]; } for (int i = 0; i < n - 1; i++) { dp1[i + 1] = vector<FiniteField>(n); dp2[i + 1] = vector<FiniteField>(n); for (int j = 0; j < n - i; j++) { if (s[i] == '<') { dp1[i + 1][j] = dp2[i][j]; } else { // s[i] == '>' dp1[i + 1][j] = dp2[i][n - i - 1] - dp2[i][j]; } } dp2[i + 1][0] = dp1[i + 1][0]; for (int j = 1; j < n - (i + 1); j++) { dp2[i + 1][j] = dp2[i + 1][j - 1] + dp1[i + 1][j]; } // cout << "i: " << i + 1 << endl; // for (int j = 0; j < n - (i + 1); j++) { // cout << dp1[i + 1][j].Value() << " "; // } // cout << endl; // for (int j = 0; j < n - (i + 1); j++) { // cout << dp2[i + 1][j].Value() << " "; // } // cout << endl; } cout << dp1[n - 1][0].Value() << endl; }
replace
73
75
73
75
-11
p03179
C++
Runtime Error
/****************************************** * AUTHOR: Atharva Sarage * * INSTITUITION: IIT HYDERABAD * ******************************************/ #include <bits/stdc++.h> #warning Check Max_Limit,Overflows using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ff first #define ss second #define pb push_back #define mod 1000000007 #define ll long long #define db double #define inf 1e9 #define mx2 100005 #define mx 305 ll dp[mx][mx], pre[mx][mx]; int main() { IOS; ll n; cin >> n; string s; cin >> s; dp[0][1] = 1; pre[0][1] = 1; for (ll i = 1; i < n; i++) { if (s[i - 1] == '<') { dp[i][1] = 0; for (ll j = 2; j <= i + 1; j++) { dp[i][j] = pre[i - 1][j - 1]; } } else { for (ll j = 1; j < i + 1; j++) { dp[i][j] = (pre[i - 1][i] - pre[i - 1][j - 1] + mod) % mod; // j+1 to i-1 } dp[i][i + 1] = 0; } pre[i][0] = 0; for (ll j = 1; j <= i + 1; j++) { pre[i][j] = (pre[i][j - 1] + dp[i][j]) % mod; } } cout << pre[n - 1][n] << endl; }
/****************************************** * AUTHOR: Atharva Sarage * * INSTITUITION: IIT HYDERABAD * ******************************************/ #include <bits/stdc++.h> #warning Check Max_Limit,Overflows using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ff first #define ss second #define pb push_back #define mod 1000000007 #define ll long long #define db double #define inf 1e9 #define mx2 100005 #define mx 3005 ll dp[mx][mx], pre[mx][mx]; int main() { IOS; ll n; cin >> n; string s; cin >> s; dp[0][1] = 1; pre[0][1] = 1; for (ll i = 1; i < n; i++) { if (s[i - 1] == '<') { dp[i][1] = 0; for (ll j = 2; j <= i + 1; j++) { dp[i][j] = pre[i - 1][j - 1]; } } else { for (ll j = 1; j < i + 1; j++) { dp[i][j] = (pre[i - 1][i] - pre[i - 1][j - 1] + mod) % mod; // j+1 to i-1 } dp[i][i + 1] = 0; } pre[i][0] = 0; for (ll j = 1; j <= i + 1; j++) { pre[i][j] = (pre[i][j - 1] + dp[i][j]) % mod; } } cout << pre[n - 1][n] << endl; }
replace
19
20
19
20
0
p03179
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using vl = vector<ll>; using Map = map<ll, ll>; using T = tuple<ll, ll, ll>; using vvl = vector<vector<ll>>; #define all(v) v.begin(), v.end() #define print(v) cout << v << endl; #define fi(v) get<0>(v) #define se(v) get<1>(v) #define th(v) get<2>(v) template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1LL << 60; const ll MOD = 1000000007; ll N; string S; vector<vector<ll>> dp(3010, vector<ll>(3010, -1)); // 後ろx個がまだ決めてない、かつ末尾がy番目に入る ll F(ll x, ll y) { if (dp[x][y] != -1) return dp[x][y]; if (x == 0) return dp[x][y] = 1; if (x == N) { ll ans = 0; for (ll i = 1; i <= N; i++) ans += F(N - 1, i); return dp[x][y] = ans % MOD; } ll ret = 0; if (S[N - x - 1] == '<') for (ll i = y; i <= x; i++) ret += F(x - 1, i); else for (ll i = 1; i < y; i++) ret += F(x - 1, i); return dp[x][y] = ret % MOD; } int main() { cin >> N; cin >> S; print(F(N, 0)) return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using vl = vector<ll>; using Map = map<ll, ll>; using T = tuple<ll, ll, ll>; using vvl = vector<vector<ll>>; #define all(v) v.begin(), v.end() #define print(v) cout << v << endl; #define fi(v) get<0>(v) #define se(v) get<1>(v) #define th(v) get<2>(v) template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1LL << 60; const ll MOD = 1000000007; ll N; string S; vector<vector<ll>> dp(3010, vector<ll>(3010, -1)); // 後ろx個がまだ決めてない、かつ末尾がy番目に入る ll F(ll x, ll y) { if (dp[x][y] != -1) return dp[x][y]; if (x == 0) return dp[x][y] = 1; if (x == N) { ll ans = 0; for (ll i = 1; i <= N; i++) ans += F(N - 1, i); return dp[x][y] = ans % MOD; } ll ret = 0; if (S[N - x - 1] == '<') { if (y < x) ret += F(x - 1, y) + F(x, y + 1); else if (y == x) ret += F(x - 1, x); } else { if (y >= 3) ret += F(x, y - 1) + F(x - 1, y - 1); else if (y == 2) ret += F(x - 1, 1); } return dp[x][y] = ret % MOD; } int main() { cin >> N; cin >> S; print(F(N, 0)) return 0; }
replace
47
53
47
58
TLE
p03179
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; const int N = 2e3 + 1, MOD = 1e9 + 7; char c[N]; int val[N][N]; signed main() { /* freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); */ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); val[0][0] = 1; int n; cin >> n; for (int i = 0; i < n; i++) cin >> c[i]; for (int i = 1; i < n; i++) { int i1 = i - 1; if (c[i1] == '<') { for (int j = 0; j < i; j++) val[i][j + 1] = (val[i][j] + val[i1][j]) % MOD; } else { for (int j = i - 1; j > -1; j--) val[i][j] = (val[i1][j] + val[i][j + 1]) % MOD; } } int t = 0; for (int i = 0; i < n; i++) t = (t + val[n - 1][i]) % MOD; cout << t; }
#include <bits/stdc++.h> #define int long long using namespace std; const int N = 1e4, MOD = 1e9 + 7; char c[N]; int val[N][N]; signed main() { /* freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); */ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); val[0][0] = 1; int n; cin >> n; for (int i = 0; i < n; i++) cin >> c[i]; for (int i = 1; i < n; i++) { int i1 = i - 1; if (c[i1] == '<') { for (int j = 0; j < i; j++) val[i][j + 1] = (val[i][j] + val[i1][j]) % MOD; } else { for (int j = i - 1; j > -1; j--) val[i][j] = (val[i1][j] + val[i][j + 1]) % MOD; } } int t = 0; for (int i = 0; i < n; i++) t = (t + val[n - 1][i]) % MOD; cout << t; }
replace
4
5
4
5
0
p03179
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long int; const int MOD = 1000 * 1000 * 1000 + 7; const int N = 1000; ll dp[N + 1][N + 1]; int main() { int n; cin >> n; string s; cin >> s; for (int i = 1; i <= n; ++i) { dp[i][0] = 0; } dp[1][1] = 1; for (int i = 0; i < s.length(); ++i) { for (int j = 1; j <= i + 2; ++j) { if (s[i] == '<') dp[i + 2][j] = dp[i + 1][j - 1]; else dp[i + 2][j] = (dp[i + 1][i + 1] - dp[i + 1][j - 1] + MOD) % MOD; } for (int j = 2; j <= i + 2; ++j) { dp[i + 2][j] = (dp[i + 2][j] + dp[i + 2][j - 1]) % MOD; } } cout << dp[n][n]; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; const int MOD = 1000 * 1000 * 1000 + 7; const int N = 3000; ll dp[N + 1][N + 1]; int main() { int n; cin >> n; string s; cin >> s; for (int i = 1; i <= n; ++i) { dp[i][0] = 0; } dp[1][1] = 1; for (int i = 0; i < s.length(); ++i) { for (int j = 1; j <= i + 2; ++j) { if (s[i] == '<') dp[i + 2][j] = dp[i + 1][j - 1]; else dp[i + 2][j] = (dp[i + 1][i + 1] - dp[i + 1][j - 1] + MOD) % MOD; } for (int j = 2; j <= i + 2; ++j) { dp[i + 2][j] = (dp[i + 2][j] + dp[i + 2][j - 1]) % MOD; } } cout << dp[n][n]; return 0; }
replace
6
7
6
7
0
p03179
C++
Runtime Error
// {{{ by unolight #include <bits/stdc++.h> #include <unistd.h> #pragma GCC diagnostic ignored "-Wunused-result" #define SZ(x) ((int)(x).size()) #define ALL(x) begin(x), end(x) #define RALL(x) rbegin(x), rend(x) #define REP(i, n) for (int i = 0; i < int(n); i++) #define REP1(i, a, b) for (int i = (a); i <= int(b); i++) #define MP make_pair #define PB push_back using namespace std; typedef int64_t LL; typedef pair<int, int> PII; typedef vector<int> VI; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%" PRId64, &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const int64_t &x) { printf("%" PRId64, x); } void _W(const double &x) { printf("%.16f\n", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } template <class T> inline bool chmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool chmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) { sort(begin(v), end(v), f); v.resize(unique(begin(v), end(v)) - begin(v)); } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; #ifdef UNO template <class T> void _dump(const char *s, T &&head) { cerr << s << "=" << head << endl; } template <class T, class... Args> void _dump(const char *s, T &&head, Args &&...tail) { for (int c = 0; *s != ',' || c != 0; cerr << *s++) { if (*s == '(' || *s == '[' || *s == '{') c++; if (*s == ')' || *s == ']' || *s == '}') c--; } cerr << "=" << head << ", "; _dump(s + 1, tail...); } #define dump(...) \ do { \ fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \ _dump(#__VA_ARGS__, __VA_ARGS__); \ } while (0) template <class Iter> ostream &_out(ostream &s, Iter b, Iter e) { s << "["; for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it; return s << "]"; } template <class A, class B> ostream &operator<<(ostream &s, const pair<A, B> &p) { return s << "(" << p.first << "," << p.second << ")"; } template <class T> ostream &operator<<(ostream &s, const vector<T> &c) { return _out(s, ALL(c)); } template <class T, size_t N> ostream &operator<<(ostream &s, const array<T, N> &c) { return _out(s, ALL(c)); } template <class T> ostream &operator<<(ostream &s, const set<T> &c) { return _out(s, ALL(c)); } template <class A, class B> ostream &operator<<(ostream &s, const map<A, B> &c) { return _out(s, ALL(c)); } #else #define dump(...) #endif // }}} // {{{ ModInt template <int _MOD> struct ModInt { static const auto MOD = _MOD; template <class T> using integral_only = typename enable_if<is_integral<T>::value>::type; int x; constexpr ModInt() : x() {} template <class T, integral_only<T> * = nullptr> ModInt(T _x = 0) { x = _x % MOD; if (x < 0) x += MOD; } ModInt operator-() const { return {x == 0 ? 0 : MOD - x}; } ModInt &operator+=(const ModInt a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(const ModInt a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(const ModInt a) { x = (long long)x * a.x % MOD; return *this; } ModInt &operator/=(const ModInt a) { return (*this) *= a.inv(); } ModInt operator+(const ModInt a) const { return ModInt(*this) += a; } ModInt operator-(const ModInt a) const { return ModInt(*this) -= a; } ModInt operator*(const ModInt a) const { return ModInt(*this) *= a; } ModInt operator/(const ModInt a) const { return ModInt(*this) /= a; } ModInt inv() const { // for prime MOD return pow(MOD - 2); } ModInt inv2() const { // work for non-prime MOD if gcd(x,MOD) = 1 int a = x, b = MOD, u = 1, v = 0; while (b != 0) { int t = a / b; a -= t * b; u -= t * v; swap(a, b); swap(u, v); } return u; } template <class T, integral_only<T> * = nullptr> ModInt pow(T t) const { ModInt r = 1, p = *this; while (t) { if (t & 1) r *= p; p *= p; t >>= 1; } return r; } bool operator==(ModInt rhs) const { return x == rhs.x; } bool operator!=(ModInt rhs) const { return x != rhs.x; } bool operator<(ModInt rhs) const { return x < rhs.x; } bool operator<=(ModInt rhs) const { return x <= rhs.x; } bool operator>(ModInt rhs) const { return x > rhs.x; } bool operator>=(ModInt rhs) const { return x >= rhs.x; } friend string to_string(ModInt i) { return to_string(i.x); } friend istream &operator>>(istream &i, ModInt &a) { return i >> a.x; } friend ostream &operator<<(ostream &o, const ModInt &a) { return o << a.x; } }; const int MOD = 1000000007; using mint = ModInt<MOD>; // }}} int main() { int n; R(n); string s; R(s); vector<mint> dp(1); dp[0] = 1; for (int i = 1; i < n; i++) { vector<mint> ndp(i + 1); if (s[i - 1] == '<') { mint sum = 0; for (int j = 0; j < i; j++) { sum += dp[j]; ndp[j + 1] = sum; } } else { mint sum = 0; for (int j = i - 1; i >= 0; j--) { sum += dp[j]; ndp[j] = sum; } } swap(dp, ndp); } cout << accumulate(ALL(dp), mint(0)) << '\n'; return 0; }
// {{{ by unolight #include <bits/stdc++.h> #include <unistd.h> #pragma GCC diagnostic ignored "-Wunused-result" #define SZ(x) ((int)(x).size()) #define ALL(x) begin(x), end(x) #define RALL(x) rbegin(x), rend(x) #define REP(i, n) for (int i = 0; i < int(n); i++) #define REP1(i, a, b) for (int i = (a); i <= int(b); i++) #define MP make_pair #define PB push_back using namespace std; typedef int64_t LL; typedef pair<int, int> PII; typedef vector<int> VI; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%" PRId64, &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const int64_t &x) { printf("%" PRId64, x); } void _W(const double &x) { printf("%.16f\n", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } template <class T> inline bool chmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool chmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) { sort(begin(v), end(v), f); v.resize(unique(begin(v), end(v)) - begin(v)); } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; #ifdef UNO template <class T> void _dump(const char *s, T &&head) { cerr << s << "=" << head << endl; } template <class T, class... Args> void _dump(const char *s, T &&head, Args &&...tail) { for (int c = 0; *s != ',' || c != 0; cerr << *s++) { if (*s == '(' || *s == '[' || *s == '{') c++; if (*s == ')' || *s == ']' || *s == '}') c--; } cerr << "=" << head << ", "; _dump(s + 1, tail...); } #define dump(...) \ do { \ fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \ _dump(#__VA_ARGS__, __VA_ARGS__); \ } while (0) template <class Iter> ostream &_out(ostream &s, Iter b, Iter e) { s << "["; for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it; return s << "]"; } template <class A, class B> ostream &operator<<(ostream &s, const pair<A, B> &p) { return s << "(" << p.first << "," << p.second << ")"; } template <class T> ostream &operator<<(ostream &s, const vector<T> &c) { return _out(s, ALL(c)); } template <class T, size_t N> ostream &operator<<(ostream &s, const array<T, N> &c) { return _out(s, ALL(c)); } template <class T> ostream &operator<<(ostream &s, const set<T> &c) { return _out(s, ALL(c)); } template <class A, class B> ostream &operator<<(ostream &s, const map<A, B> &c) { return _out(s, ALL(c)); } #else #define dump(...) #endif // }}} // {{{ ModInt template <int _MOD> struct ModInt { static const auto MOD = _MOD; template <class T> using integral_only = typename enable_if<is_integral<T>::value>::type; int x; constexpr ModInt() : x() {} template <class T, integral_only<T> * = nullptr> ModInt(T _x = 0) { x = _x % MOD; if (x < 0) x += MOD; } ModInt operator-() const { return {x == 0 ? 0 : MOD - x}; } ModInt &operator+=(const ModInt a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(const ModInt a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(const ModInt a) { x = (long long)x * a.x % MOD; return *this; } ModInt &operator/=(const ModInt a) { return (*this) *= a.inv(); } ModInt operator+(const ModInt a) const { return ModInt(*this) += a; } ModInt operator-(const ModInt a) const { return ModInt(*this) -= a; } ModInt operator*(const ModInt a) const { return ModInt(*this) *= a; } ModInt operator/(const ModInt a) const { return ModInt(*this) /= a; } ModInt inv() const { // for prime MOD return pow(MOD - 2); } ModInt inv2() const { // work for non-prime MOD if gcd(x,MOD) = 1 int a = x, b = MOD, u = 1, v = 0; while (b != 0) { int t = a / b; a -= t * b; u -= t * v; swap(a, b); swap(u, v); } return u; } template <class T, integral_only<T> * = nullptr> ModInt pow(T t) const { ModInt r = 1, p = *this; while (t) { if (t & 1) r *= p; p *= p; t >>= 1; } return r; } bool operator==(ModInt rhs) const { return x == rhs.x; } bool operator!=(ModInt rhs) const { return x != rhs.x; } bool operator<(ModInt rhs) const { return x < rhs.x; } bool operator<=(ModInt rhs) const { return x <= rhs.x; } bool operator>(ModInt rhs) const { return x > rhs.x; } bool operator>=(ModInt rhs) const { return x >= rhs.x; } friend string to_string(ModInt i) { return to_string(i.x); } friend istream &operator>>(istream &i, ModInt &a) { return i >> a.x; } friend ostream &operator<<(ostream &o, const ModInt &a) { return o << a.x; } }; const int MOD = 1000000007; using mint = ModInt<MOD>; // }}} int main() { int n; R(n); string s; R(s); vector<mint> dp(1); dp[0] = 1; for (int i = 1; i < n; i++) { vector<mint> ndp(i + 1); if (s[i - 1] == '<') { mint sum = 0; for (int j = 0; j < i; j++) { sum += dp[j]; ndp[j + 1] = sum; } } else { mint sum = 0; for (int j = i - 1; j >= 0; j--) { sum += dp[j]; ndp[j] = sum; } } swap(dp, ndp); } cout << accumulate(ALL(dp), mint(0)) << '\n'; return 0; }
replace
193
194
193
194
-11
p03179
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory.h> #include <queue> #include <set> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef unsigned long long llu; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef pair<string, int> psi; typedef pair<char, int> pci; typedef pair<int, char> pic; const ll MOD = (ll)1e9 + 7; const long double PI = 3.141592653589793238462643383279502884197; priority_queue<int, vector<int>, greater<int>> pq; vector<int> v; char s[3005]; int dp[3005][3005]; int n; int go(int o, int dd) { if (o == n) return 1; if (dp[o][dd] != -1) return dp[o][dd]; ll ret = 0; int nn = n - o; if (s[o] == '<') { if (dd < nn - 1) ret += go(o, dd + 1); if (dd < nn) ret += go(o + 1, dd); } else { if (dd > 1) ret += go(o, dd - 1); if (dd > 0) ret += go(o + 1, dd - 1); } return dp[o][dd] = ret % MOD; } int main() { memset(dp, -1, sizeof(dp)); scanf("%d", &n); scanf("%s", s + 1); ll ret = 0; for (int i = 0; i < n; i++) ret += go(1, i); printf("%lld", ret % MOD); }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory.h> #include <queue> #include <set> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef unsigned long long llu; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; typedef pair<string, int> psi; typedef pair<char, int> pci; typedef pair<int, char> pic; const ll MOD = (ll)1e9 + 7; const long double PI = 3.141592653589793238462643383279502884197; priority_queue<int, vector<int>, greater<int>> pq; vector<int> v; char s[3005]; int dp[3005][3005]; int n; int go(int o, int dd) { if (o == n) return 1; if (dp[o][dd] != -1) return dp[o][dd]; ll ret = 0; int nn = n - o; if (s[o] == '<') { if (dd < nn - 1) ret += go(o, dd + 1); if (dd < nn) ret += go(o + 1, dd); } else { if (dd > 1) ret += go(o, dd - 1); if (dd > 0) ret += go(o + 1, dd - 1); } return dp[o][dd] = ret % MOD; } int main() { memset(dp, -1, sizeof(dp)); scanf("%d", &n); scanf("%s", s + 1); ll ret = 0; for (int i = n - 1; i >= 1; i--) for (int j = 0; j < n - i + 1; j++) go(i, j); for (int i = 0; i < n; i++) ret += go(1, i); printf("%lld", ret % MOD); }
insert
74
74
74
78
0
p03179
C++
Runtime Error
// #define LOCAL // #define BUG #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define sz(x) (int)((x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define fi first #define se second #define pb push_back #define ins insert #define rsz resize #define lb lower_bound #define ub upper_bound #define fore(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define fort(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define ford(i, a, b) for (int i = (a), _b = (b); i >= _b; --i) #ifdef BUG #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << ": " << arg1 << '\n'; } 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...); } #endif template <class T> bool mini(T &a, T b) { return a > b ? (a = b, 1) : 0; } template <class T> bool maxi(T &a, T b) { return a < b ? (a = b, 1) : 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<vii> vvii; const ll INF = (ll)1e9 + 7; const int N = 1e3 + 3; int n; char s[N]; ll f[N][N]; int main() { #ifdef LOCAL freopen("CP.inp", "r", stdin); // freopen("CP.out", "w", stdout); #endif // LOCAL ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> (s + 1); fort(i, 1, n) f[1][i] = i; fort(i, 2, n) { if (s[i - 1] == '<') { fort(j, 1, i) f[i][j] = (((f[i][j - 1] + f[i - 1][j - 1] - f[i - 1][0]) % INF) + INF) % INF; } else { fort(j, 1, i) f[i][j] = (((f[i][j - 1] + f[i - 1][i - 1] - f[i - 1][j - 1]) % INF) + INF) % INF; } } cout << f[n][n] << '\n'; return 0; }
// #define LOCAL // #define BUG #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define sz(x) (int)((x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define fi first #define se second #define pb push_back #define ins insert #define rsz resize #define lb lower_bound #define ub upper_bound #define fore(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define fort(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define ford(i, a, b) for (int i = (a), _b = (b); i >= _b; --i) #ifdef BUG #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << ": " << arg1 << '\n'; } 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...); } #endif template <class T> bool mini(T &a, T b) { return a > b ? (a = b, 1) : 0; } template <class T> bool maxi(T &a, T b) { return a < b ? (a = b, 1) : 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<vii> vvii; const ll INF = (ll)1e9 + 7; const int N = 3e3 + 3; int n; char s[N]; ll f[N][N]; int main() { #ifdef LOCAL freopen("CP.inp", "r", stdin); // freopen("CP.out", "w", stdout); #endif // LOCAL ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> (s + 1); fort(i, 1, n) f[1][i] = i; fort(i, 2, n) { if (s[i - 1] == '<') { fort(j, 1, i) f[i][j] = (((f[i][j - 1] + f[i - 1][j - 1] - f[i - 1][0]) % INF) + INF) % INF; } else { fort(j, 1, i) f[i][j] = (((f[i][j - 1] + f[i - 1][i - 1] - f[i - 1][j - 1]) % INF) + INF) % INF; } } cout << f[n][n] << '\n'; return 0; }
replace
65
66
65
66
0
p03179
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> #define st first #define nd second #define turbo \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define vi vector<int> #define vvi vector<vi> #define qi queue<int> #define ld long double using namespace std; /*---------------------------------------------------------///CODE///---------------------------------------------------------*/ const int N = 1e3; const int MOD = 1e9 + 7; int dp[N + 10][N + 10]; int pref[N + 10][N + 10]; int main() { turbo int n; cin >> n; string s; cin >> s; dp[1][1] = 1; pref[1][1] = 1; for (int i = 2; i <= s.size() + 1; i++) { for (int j = 1; j <= i; j++) (dp[i][j] += (s[i - 2] == '<' ? pref[i - 1][j - 1] : pref[i - 1][i - 1] - pref[i - 1][j - 1] + MOD)) %= MOD; for (int j = 1; j <= i; j++) pref[i][j] = (pref[i][j - 1] + dp[i][j]) % MOD; } int ans = 0; for (int i = 1; i <= s.size() + 1; i++) (ans += dp[s.size() + 1][i]) %= MOD; return cout << ans, 0; }
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> #define st first #define nd second #define turbo \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define vi vector<int> #define vvi vector<vi> #define qi queue<int> #define ld long double using namespace std; /*---------------------------------------------------------///CODE///---------------------------------------------------------*/ const int N = 3e3; const int MOD = 1e9 + 7; int dp[N + 10][N + 10]; int pref[N + 10][N + 10]; int main() { turbo int n; cin >> n; string s; cin >> s; dp[1][1] = 1; pref[1][1] = 1; for (int i = 2; i <= s.size() + 1; i++) { for (int j = 1; j <= i; j++) (dp[i][j] += (s[i - 2] == '<' ? pref[i - 1][j - 1] : pref[i - 1][i - 1] - pref[i - 1][j - 1] + MOD)) %= MOD; for (int j = 1; j <= i; j++) pref[i][j] = (pref[i][j - 1] + dp[i][j]) % MOD; } int ans = 0; for (int i = 1; i <= s.size() + 1; i++) (ans += dp[s.size() + 1][i]) %= MOD; return cout << ans, 0; }
replace
19
20
19
20
0
p03179
C++
Time Limit Exceeded
// HTTF.cpp : このファイルには 'main' // 関数が含まれています。プログラム実行の開始と終了がそこで行われます。 // #include "bits/stdc++.h" #define YES "YES" #define NO "NO" #define YESNO OUT(three(solve(), YES, NO)) #define ECHO OUT(solve()) #define three(A, B, C) ((A) ? (B) : (C)) #define FOR(i, a, b) for (LL i = (a); i < (LL)(b); i++) #define EFOR(i, a, b) for (LL i = (a); i <= (LL)(b); i++) #define RFOR(i, a, b) for (LL i = (a); i >= (LL)(b); i--) #define REP(i, b) FOR(i, zero, b) #define EREP(i, b) EFOR(i, zero, b) #define RREP(i, b) RFOR(i, b, zero) #define ALL(c) c.begin(), c.end() #define UNIQUE(c) \ sort(ALL(c)); \ c.erase(unique(ALL(c)), c.end()) #define MAX(c) (*max_element(ALL(c))) #define MIN(c) (*min_element(ALL(c))) #define MP make_pair #define FI first #define SE second #define SI(x) (LL(x.size())) #define PB push_back #define DEBUG(a) OUT(a) #define DEBUG2(a, b) OUT2(a, b) #define cat cout << __LINE__ << endl #define OUT(a) cout << (a) << endl #define OUT2(a, b) cout << (a) << " " << (b) << endl #define zero 0LL #define int LL #define pb emplace_back #define eb pb using namespace std; template <typename T> inline void maximize(T &a, T b) { a = max(a, b); } template <typename T> inline void minimize(T &a, T b) { a = min(a, b); } template <typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; } template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } typedef long long LL; typedef double ld; typedef int ut; typedef vector<ut> VI; typedef vector<VI> VII; typedef pair<ut, ut> pr; typedef pair<ut, pr> ppr; typedef vector<pr> Vpr; typedef vector<ppr> Vppr; typedef priority_queue<pr, Vpr, greater<pr>> PQ; inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; } OUT(""); } const int SIZE1 = 5e5 + 1000; const int SIZE2 = 3010; const int SIZE3 = 330; const int SIZE = SIZE1; const int MAPSIZE = 40; const LL p = 7 + 1e9; const LL INF = 1LL << 60; const long double EPS = 1e-7; typedef pair<ld, ut> pld; #define endl "\n" // インタラクティブでは消す ut N, M, K, L, Q, D, H, W; // ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U; VI edges[SIZE]; LL vals[SIZE], maps2[SIZE2][SIZE2], answer = zero; LL maps[SIZE2][SIZE2]; LL DP[SIZE2][SIZE2]; bool finished[SIZE2][SIZE2]; LL A[SIZE2][SIZE2]; string s; LL sums[SIZE2][SIZE2]; bool finished2[SIZE2][SIZE2]; LL solve2(LL, LL); LL sum(LL n, LL x) { if (x == 0) return 0; if (finished2[n][x]) return sums[n][x]; finished2[n][x]; return sums[n][x] = (sum(n, x - 1) + solve2(n, x)) % p; } LL sum(LL n, LL a, LL b) { if (a > b) return 0; return (p + sum(n, b) - sum(n, a - 1)) % p; } LL solve2(LL n, LL k) { if (n == 0) return k == 1; if (finished[n][k]) return DP[n][k]; finished[n][k] = true; if (s[n - 1] == '<') { DP[n][k] = sum(n - 1, 1, k - 1); } else { DP[n][k] = sum(n - 1, k, n); } // cout << n << " " << k << " " << DP[n][k] << endl; return DP[n][k]; } LL solve() { cin >> N; cin >> s; s += '<'; cout << solve2(N, N + 1) << endl; return 0; } //!!!!!!!!!!!!!!!!!!!実装を詰める!!!!!!!!!!!!!!!!!!!!!!!!! signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); solve(); cin >> N; return 0; }
// HTTF.cpp : このファイルには 'main' // 関数が含まれています。プログラム実行の開始と終了がそこで行われます。 // #include "bits/stdc++.h" #define YES "YES" #define NO "NO" #define YESNO OUT(three(solve(), YES, NO)) #define ECHO OUT(solve()) #define three(A, B, C) ((A) ? (B) : (C)) #define FOR(i, a, b) for (LL i = (a); i < (LL)(b); i++) #define EFOR(i, a, b) for (LL i = (a); i <= (LL)(b); i++) #define RFOR(i, a, b) for (LL i = (a); i >= (LL)(b); i--) #define REP(i, b) FOR(i, zero, b) #define EREP(i, b) EFOR(i, zero, b) #define RREP(i, b) RFOR(i, b, zero) #define ALL(c) c.begin(), c.end() #define UNIQUE(c) \ sort(ALL(c)); \ c.erase(unique(ALL(c)), c.end()) #define MAX(c) (*max_element(ALL(c))) #define MIN(c) (*min_element(ALL(c))) #define MP make_pair #define FI first #define SE second #define SI(x) (LL(x.size())) #define PB push_back #define DEBUG(a) OUT(a) #define DEBUG2(a, b) OUT2(a, b) #define cat cout << __LINE__ << endl #define OUT(a) cout << (a) << endl #define OUT2(a, b) cout << (a) << " " << (b) << endl #define zero 0LL #define int LL #define pb emplace_back #define eb pb using namespace std; template <typename T> inline void maximize(T &a, T b) { a = max(a, b); } template <typename T> inline void minimize(T &a, T b) { a = min(a, b); } template <typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; } template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } typedef long long LL; typedef double ld; typedef int ut; typedef vector<ut> VI; typedef vector<VI> VII; typedef pair<ut, ut> pr; typedef pair<ut, pr> ppr; typedef vector<pr> Vpr; typedef vector<ppr> Vppr; typedef priority_queue<pr, Vpr, greater<pr>> PQ; inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; } OUT(""); } const int SIZE1 = 5e5 + 1000; const int SIZE2 = 3010; const int SIZE3 = 330; const int SIZE = SIZE1; const int MAPSIZE = 40; const LL p = 7 + 1e9; const LL INF = 1LL << 60; const long double EPS = 1e-7; typedef pair<ld, ut> pld; #define endl "\n" // インタラクティブでは消す ut N, M, K, L, Q, D, H, W; // ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U; VI edges[SIZE]; LL vals[SIZE], maps2[SIZE2][SIZE2], answer = zero; LL maps[SIZE2][SIZE2]; LL DP[SIZE2][SIZE2]; bool finished[SIZE2][SIZE2]; LL A[SIZE2][SIZE2]; string s; LL sums[SIZE2][SIZE2]; bool finished2[SIZE2][SIZE2]; LL solve2(LL, LL); LL sum(LL n, LL x) { if (x == 0) return 0; if (finished2[n][x]) return sums[n][x]; finished2[n][x] = true; return sums[n][x] = (sum(n, x - 1) + solve2(n, x)) % p; } LL sum(LL n, LL a, LL b) { if (a > b) return 0; return (p + sum(n, b) - sum(n, a - 1)) % p; } LL solve2(LL n, LL k) { if (n == 0) return k == 1; if (finished[n][k]) return DP[n][k]; finished[n][k] = true; if (s[n - 1] == '<') { DP[n][k] = sum(n - 1, 1, k - 1); } else { DP[n][k] = sum(n - 1, k, n); } // cout << n << " " << k << " " << DP[n][k] << endl; return DP[n][k]; } LL solve() { cin >> N; cin >> s; s += '<'; cout << solve2(N, N + 1) << endl; return 0; } //!!!!!!!!!!!!!!!!!!!実装を詰める!!!!!!!!!!!!!!!!!!!!!!!!! signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); solve(); cin >> N; return 0; }
replace
91
92
91
92
TLE
p03179
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define For(i, n) for (int i = 0; i < n; i++) #define mem(a, b) memset(a, b, sizeof(a)) #define int long long int #define ld long double #define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define pb push_back #define mp make_pair #define mod 1000000007 #define ld long double #define pb push_back #define ff first #define ss second #define vpi vector<pair<int, int>> #define vv vector<int> #define MAXN 3005 int dp[MAXN][MAXN]; int n; // void fun(int I , int id) // { // if(id==0)return 1; // if(dp[I][id]!=) // int t; // if (str[id] == '>') // { // t = sum(n, id - 1) - sum(I-1,id-1); // } // else{ // t=sum(I-1,id-1); // } // return t; // } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fio; cin >> n; string str; cin >> str; int nn = 0; // for(int i=1;i<=n;i++) // nn+=fun(i,n-1) // nn=fun(i,n-1); // cout<<nn; if (str[0] == '>') { dp[0][1] = 1; dp[0][2] = 1; } else { dp[0][2] = 1; } for (int i = 1; i < n - 1; i++) { for (int j = 1; j <= i + 2; j++) { if (str[i] == '<') { dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod; } else { dp[i][j] = (dp[i][j - 1] + (dp[i - 1][i + 1] - dp[i - 1][j - 1]) + mod) % mod; } } } cout << dp[n - 2][n]; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define For(i, n) for (int i = 0; i < n; i++) #define mem(a, b) memset(a, b, sizeof(a)) #define int long long int #define ld long double #define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define pb push_back #define mp make_pair #define mod 1000000007 #define ld long double #define pb push_back #define ff first #define ss second #define vpi vector<pair<int, int>> #define vv vector<int> #define MAXN 3005 int dp[MAXN][MAXN]; int n; // void fun(int I , int id) // { // if(id==0)return 1; // if(dp[I][id]!=) // int t; // if (str[id] == '>') // { // t = sum(n, id - 1) - sum(I-1,id-1); // } // else{ // t=sum(I-1,id-1); // } // return t; // } int32_t main() { fio; cin >> n; string str; cin >> str; int nn = 0; // for(int i=1;i<=n;i++) // nn+=fun(i,n-1) // nn=fun(i,n-1); // cout<<nn; if (str[0] == '>') { dp[0][1] = 1; dp[0][2] = 1; } else { dp[0][2] = 1; } for (int i = 1; i < n - 1; i++) { for (int j = 1; j <= i + 2; j++) { if (str[i] == '<') { dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod; } else { dp[i][j] = (dp[i][j - 1] + (dp[i - 1][i + 1] - dp[i - 1][j - 1]) + mod) % mod; } } } cout << dp[n - 2][n]; }
replace
37
41
37
38
-11
p03179
C++
Time Limit Exceeded
#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; int comp[3011]; int dp[3011][3011]; int main() { scanf("%d", &n); string s; cin >> s; for (int i = 1; i < n; i++) { if (s[i - 1] == '<') comp[i] = 0; else comp[i] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) dp[i][j] = 0; } dp[1][1] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j <= i; j++) { if (comp[i - 1] == 0) { int sum = 0; for (int k = 1; k < j; k++) sum = (sum + dp[i - 1][k]) % INF; dp[i][j] = sum; } else { int sum = 0; for (int k = i - 1; k >= j; k--) sum = (sum + dp[i - 1][k]) % INF; dp[i][j] = sum; } } } int sum = 0; for (int i = 1; i <= n; i++) sum = (sum + dp[n][i]) % INF; printf("%d\n", sum); }
#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; int comp[3011]; int dp[3011][3011]; int main() { scanf("%d", &n); string s; cin >> s; for (int i = 1; i < n; i++) { if (s[i - 1] == '<') comp[i] = 0; else comp[i] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) dp[i][j] = 0; } dp[1][1] = 1; for (int i = 2; i <= n; i++) { if (comp[i - 1] == 0) { int sum = 0; for (int k = 1; k < i; k++) { sum = (sum + dp[i - 1][k]) % INF; dp[i][k + 1] = sum; } } else { int sum = 0; for (int k = i - 1; k >= 1; k--) { sum = (sum + dp[i - 1][k]) % INF; dp[i][k] = sum; } } } int sum = 0; for (int i = 1; i <= n; i++) sum = (sum + dp[n][i]) % INF; printf("%d\n", sum); }
replace
39
50
39
50
TLE
p03179
C++
Runtime Error
// :'((((((((((((( #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 M 1000000007LL #define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) #define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--) #define mll map<ll, ll> #define pb push_back #define lb lower_bound #define ub upper_bound #define sz(v) ((ll)v.size()) #define mp make_pair #define all(a) a.begin(), a.end() #define F first #define S second #define pii pair<ll, ll> #define mod 1000000007LL #define MAXN 300002 typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; vector<string> v; ll dp[2002][2003]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; string s; cin >> s; ll i; dp[0][1] = (s[0] == '>'); dp[0][2] = 1; for (i = 1; i < sz(s); i++) { for (ll j = 1; j <= i + 2; j++) { dp[i][j] = dp[i][j - 1]; if (s[i] == '<') dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod; else dp[i][j] = (((dp[i][j] + dp[i - 1][i + 1]) % mod - dp[i - 1][j - 1]) % mod + mod) % mod; } } cout << dp[sz(s) - 1][sz(s) + 1]; 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 M 1000000007LL #define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) #define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--) #define mll map<ll, ll> #define pb push_back #define lb lower_bound #define ub upper_bound #define sz(v) ((ll)v.size()) #define mp make_pair #define all(a) a.begin(), a.end() #define F first #define S second #define pii pair<ll, ll> #define mod 1000000007LL #define MAXN 300002 typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; vector<string> v; ll dp[3002][3003]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; string s; cin >> s; ll i; dp[0][1] = (s[0] == '>'); dp[0][2] = 1; for (i = 1; i < sz(s); i++) { for (ll j = 1; j <= i + 2; j++) { dp[i][j] = dp[i][j - 1]; if (s[i] == '<') dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod; else dp[i][j] = (((dp[i][j] + dp[i - 1][i + 1]) % mod - dp[i - 1][j - 1]) % mod + mod) % mod; } } cout << dp[sz(s) - 1][sz(s) + 1]; return 0; }
replace
26
27
26
27
0
p03179
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef long long LL; typedef pair<int, int> PI; typedef pair<LL, LL> PLL; typedef unsigned long long ULL; typedef pair<double, double> PD; #define FOR(x, b, e) for (int x = b; x <= (e); x++) #define FORD(x, b, e) for (int x = b; x >= (e); x--) #define REP(x, n) for (int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define IN insert #define ST first #define ND second #define INF 2000000011 #define MOD 1000000007 #define MAXS 3010 int n; LL dp[MAXS][MAXS]; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; dp[1][1] = 1; FOR(i, 2, n) { FOR(j, 1, i) { if (s[i - 2] == '>') { FOR(k, j, n) { dp[i][j] += dp[i - 1][k]; dp[i][j] %= MOD; } } else { FOR(k, 1, j - 1) { dp[i][j] += dp[i - 1][k]; dp[i][j] %= MOD; } } } } LL wyn = 0; FOR(i, 1, n) { wyn += dp[n][i]; wyn %= MOD; } cout << wyn; return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef long long LL; typedef pair<int, int> PI; typedef pair<LL, LL> PLL; typedef unsigned long long ULL; typedef pair<double, double> PD; #define FOR(x, b, e) for (int x = b; x <= (e); x++) #define FORD(x, b, e) for (int x = b; x >= (e); x--) #define REP(x, n) for (int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define IN insert #define ST first #define ND second #define INF 2000000011 #define MOD 1000000007 #define MAXS 3010 int n; LL dp[MAXS][MAXS]; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; dp[1][1] = 1; FOR(i, 2, n) { if (s[i - 2] == '>') { LL suf = 0; FORD(j, i, 1) { suf += dp[i - 1][j]; suf %= MOD; dp[i][j] += suf; dp[i][j] %= MOD; } } else { LL pref = 0; FOR(j, 1, i) { pref += dp[i - 1][j - 1]; pref %= MOD; dp[i][j] += pref; dp[i][j] %= MOD; } } } LL wyn = 0; FOR(i, 1, n) { wyn += dp[n][i]; wyn %= MOD; } cout << wyn; return 0; }
replace
39
50
39
54
TLE
p03179
C++
Runtime Error
#pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long LL; int n; string s; const LL MOD = 1e9 + 7; LL memo[4005][4005][5]; LL dp(int l, int r, int indi) { // 0 = menaik, 1 menurun if (l < 0 || r < 0) return 0; if ((l + r) == 0) return 1; LL &ret = memo[l][r][indi]; if (ret != -1) return ret; int tot = l + r; if (indi == 0) ret = dp(l + 1, r - 1, indi); else ret = dp(l - 1, r + 1, indi); int id = n - tot; // s[id] = '>'; if (s[id] == '>') ret += dp(l - 1, r, 1); else ret += dp(l, r - 1, 0); ret %= MOD; // cout<<l<<" "<<r<<" "<<indi<<endl; return ret; } int main() { cin >> n; cin >> s; n--; memset(memo, -1, sizeof(memo)); LL ans = dp(n, 0, 1); printf("%lld\n", ans); return 0; }
#pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long LL; int n; string s; const LL MOD = 1e9 + 7; LL memo[4005][4005][5]; LL dp(int l, int r, int indi) { // 0 = menaik, 1 menurun if (l < 0 || r < 0) return 0; if ((l + r) == 0) return 1; LL &ret = memo[l][r][indi]; if (ret != -1) return ret; int tot = l + r; if (indi == 0) ret = dp(l + 1, r - 1, indi); else ret = dp(l - 1, r + 1, indi); int id = n - tot; // s[id] = '>'; if (s[id] == '>') ret += dp(l - 1, r, 1); else ret += dp(l, r - 1, 0); ret %= MOD; // cout<<l<<" "<<r<<" "<<indi<<endl; return ret; } int main() { cin >> n; cin >> s; n--; memset(memo, -1, sizeof(memo)); for (int i = 0; i <= 1; i++) { for (int j = 1; j <= n; j++) { for (int k = 0; k <= j; k++) dp(k, j - k, i); } } LL ans = dp(n, 0, 1); printf("%lld\n", ans); return 0; }
insert
44
44
44
50
-11
p03180
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; 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; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう cin.tie(0); ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; // 線分 ab の偏角 返り値は[-π, π] double argument(const pair<double, double> &a, const pair<double, double> &b) { double ax = a.first, ay = a.second, bx = b.first, by = b.second; return atan2(by - ay, bx - ax); } /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ int N; vector<vector<ll>> a; vector<ll> memo(1 << 18, 0); vector<bool> flg(1 << 18, false); ll rec(int S) { if (flg[S]) return memo[S]; ll tmp = 0; rep(i, N) repr(j, i + 1, N) if (S & 1 << i && S & 1 << j) tmp += a[i][j]; for (int T = S - 1; T > 0; T = (T - 1) & S) { chmax(tmp, rec(T) + rec(S ^ T)); } flg[S] = true; return memo[S] = tmp; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin >> N; a.resize(N, vector<ll>(N, 0)); rep(i, N) rep(j, N) cin >> a[i][j]; cout << rec((1 << N) - 1) << endl; }
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; 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; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう cin.tie(0); ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; // 線分 ab の偏角 返り値は[-π, π] double argument(const pair<double, double> &a, const pair<double, double> &b) { double ax = a.first, ay = a.second, bx = b.first, by = b.second; return atan2(by - ay, bx - ax); } /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ /* <-----------------------------------------------------------------------------------> */ int N; vector<vector<ll>> a; vector<ll> memo(1 << 18, 0); vector<bool> flg(1 << 18, false); ll rec(int S) { if (flg[S]) return memo[S]; ll tmp = 0; rep(i, N) repr(j, i + 1, N) if (S & 1 << i && S & 1 << j) tmp += a[i][j]; for (int T = (S - 1) & S; T > 0; T = (T - 1) & S) { chmax(tmp, rec(T) + rec(S ^ T)); } flg[S] = true; return memo[S] = tmp; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin >> N; a.resize(N, vector<ll>(N, 0)); rep(i, N) rep(j, N) cin >> a[i][j]; cout << rec((1 << N) - 1) << endl; }
replace
84
85
84
85
-11
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef pair<int, int> PI; typedef pair<LL, LL> PLL; typedef unsigned long long ULL; typedef pair<double, double> PD; #define FOR(x, b, e) for (int x = b; x <= (e); x++) #define FORD(x, b, e) for (int x = b; x >= (e); x--) #define REP(x, n) for (int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define IN insert #define ST first #define ND second #define INF 2000000011 #define MOD 1000000007 #define MAXT 20 #define MAXS 65600 int n; LL tab[MAXT][MAXT]; LL dp[MAXS]; LL przejdz(int mask) { if (dp[mask] == -1) { LL sum = 0; FOR(i, 1, n) { FOR(j, 1, n) { if (((1 << (i - 1) & mask) != 0) && ((1 << (j - 1) & mask) != 0)) sum += tab[i][j]; } } dp[mask] = max(0LL, sum); } else return dp[mask]; REP(i, 1 << n) { int sub1 = i & mask; int sub2 = sub1 ^ mask; dp[mask] = max(dp[mask], przejdz(sub1) + przejdz(sub2)); } return dp[mask]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; FOR(i, 1, n) { FOR(j, 1, n) cin >> tab[i][j]; } FOR(i, 0, 1 << n) dp[i] = -1; cout << przejdz((1 << n) - 1) / 2LL; return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef pair<int, int> PI; typedef pair<LL, LL> PLL; typedef unsigned long long ULL; typedef pair<double, double> PD; #define FOR(x, b, e) for (int x = b; x <= (e); x++) #define FORD(x, b, e) for (int x = b; x >= (e); x--) #define REP(x, n) for (int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define IN insert #define ST first #define ND second #define INF 2000000011 #define MOD 1000000007 #define MAXT 20 #define MAXS 65600 int n; LL tab[MAXT][MAXT]; LL dp[MAXS]; LL przejdz(int mask) { if (dp[mask] == -1) { LL sum = 0; FOR(i, 1, n) { FOR(j, 1, n) { if (((1 << (i - 1) & mask) != 0) && ((1 << (j - 1) & mask) != 0)) sum += tab[i][j]; } } dp[mask] = max(0LL, sum); } else return dp[mask]; for (int sub1 = (mask - 1) & mask; sub1; sub1 = (sub1 - 1) & mask) { int sub2 = sub1 ^ mask; dp[mask] = max(dp[mask], przejdz(sub1) + przejdz(sub2)); } return dp[mask]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; FOR(i, 1, n) { FOR(j, 1, n) cin >> tab[i][j]; } FOR(i, 0, 1 << n) dp[i] = -1; cout << przejdz((1 << n) - 1) / 2LL; return 0; }
replace
45
47
45
47
TLE
p03180
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> #define MOD 1000000007 typedef long long ll; using namespace std; int N; ll a[20][20]; ll score[(1 << 16)]; ll dp[(1 << 16)]; const ll INF = 1e18; ll solve(int state) { if (dp[state] != -INF) return dp[state]; ll res = score[state]; for (int i = state - 1; i > 0; i--) { i &= state; ll tmp = solve(i); tmp += solve(state ^ i); res = max(res, tmp); } return dp[state] = res; } int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; } } for (int i = 0; i < (1 << N); i++) { vector<int> v; for (int j = 0; j < N; j++) { if ((1 << j) & i) v.push_back(j); } ll res = 0; for (int j = 0; j < v.size(); j++) { for (int k = j + 1; k < v.size(); k++) { res += a[v[j]][v[k]]; } } score[i] = max(res, 0ll); dp[i] = -INF; } dp[0] = 0; cout << solve((1 << N) - 1) << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> #define MOD 1000000007 typedef long long ll; using namespace std; int N; ll a[20][20]; ll score[(1 << 16)]; ll dp[(1 << 16)]; const ll INF = 1e18; ll solve(int state) { if (dp[state] != -INF) return dp[state]; ll res = score[state]; for (int i = state - 1; i > 0; i--) { i &= state; if (i == 0) break; ll tmp = solve(i); tmp += solve(state ^ i); res = max(res, tmp); } return dp[state] = res; } int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; } } for (int i = 0; i < (1 << N); i++) { vector<int> v; for (int j = 0; j < N; j++) { if ((1 << j) & i) v.push_back(j); } ll res = 0; for (int j = 0; j < v.size(); j++) { for (int k = j + 1; k < v.size(); k++) { res += a[v[j]][v[k]]; } } score[i] = max(res, 0ll); dp[i] = -INF; } dp[0] = 0; cout << solve((1 << N) - 1) << endl; return 0; }
insert
27
27
27
29
-11
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back #define INF 2000000000000000000LL #define EPS 1e-9 #define debug(a) cerr << #a << "=" << (a) << "\n" #define FastSlowInput \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); typedef long long ll; typedef unsigned long long ull; typedef complex<double> cd; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 998244353; const double PI = acos(-1); int n, i, j, k, t; ll dp[1 << 17]; ll cost[1 << 17]; int a[17][17]; bool visdp[1 << 17]; bool viscost[1 << 17]; ll totcost(int mask) { ll &ans = cost[mask]; if (viscost[mask]) { viscost[mask] = false; ans = 0; int i = __builtin_ffs(mask) - 1; for (int j = i + 1; j < n; ++j) { if (mask & (1 << j)) ans += a[i][j]; } ans += totcost(mask ^ (1 << i)); } return ans; } ll solve(int mask) { ll &ans = dp[mask]; if (visdp[mask]) { visdp[mask] = false; ans = -1e18; for (int i = 1; i < (1 << n); ++i) { if (mask & i) { int sub = mask & i; ans = max(ans, totcost(sub) + solve(mask ^ sub)); } } } return ans; } int main() { memset(dp, 0, sizeof dp); memset(visdp, true, sizeof visdp); memset(viscost, true, sizeof viscost); memset(cost, 0, sizeof cost); scanf("%d", &n); for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { scanf("%d", a[i] + j); } } visdp[0] = viscost[0] = 0; for (i = 0; i < n; ++i) visdp[1 << i] = viscost[1 << i] = 0; ll ans = solve((1 << n) - 1); printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back #define INF 2000000000000000000LL #define EPS 1e-9 #define debug(a) cerr << #a << "=" << (a) << "\n" #define FastSlowInput \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); typedef long long ll; typedef unsigned long long ull; typedef complex<double> cd; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 998244353; const double PI = acos(-1); int n, i, j, k, t; ll dp[1 << 17]; ll cost[1 << 17]; int a[17][17]; bool visdp[1 << 17]; bool viscost[1 << 17]; ll totcost(int mask) { ll &ans = cost[mask]; if (viscost[mask]) { viscost[mask] = false; ans = 0; int i = __builtin_ffs(mask) - 1; for (int j = i + 1; j < n; ++j) { if (mask & (1 << j)) ans += a[i][j]; } ans += totcost(mask ^ (1 << i)); } return ans; } ll solve(int mask) { ll &ans = dp[mask]; if (visdp[mask]) { visdp[mask] = false; ans = -1e18; for (int sub = mask; sub; sub = (sub - 1) & mask) { ans = max(ans, totcost(sub) + solve(mask ^ sub)); } } return ans; } int main() { memset(dp, 0, sizeof dp); memset(visdp, true, sizeof visdp); memset(viscost, true, sizeof viscost); memset(cost, 0, sizeof cost); scanf("%d", &n); for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { scanf("%d", a[i] + j); } } visdp[0] = viscost[0] = 0; for (i = 0; i < n; ++i) visdp[1 << i] = viscost[1 << i] = 0; ll ans = solve((1 << n) - 1); printf("%lld\n", ans); return 0; }
replace
51
56
51
53
TLE
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int N = 16; const int N2 = (1 << N); int n, a[N][N]; long long cost[N2], dp[N2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } int p2n = (1 << n); for (int bm = 0; bm < p2n; bm++) { for (int i = 1; i < n; i++) { if ((bm & (1 << i)) == 0) continue; for (int j = 0; j < i; j++) { if ((bm & (1 << j)) == 0) continue; cost[bm] += (long long)a[i][j]; } } } for (int bm = 1; bm < p2n; bm++) { long long ans = 0; for (int sbm = 0; sbm < p2n; sbm++) { if ((sbm & bm) != sbm) continue; int cbm = bm ^ sbm; ans = max(ans, cost[sbm] + dp[cbm]); } dp[bm] = ans; } cout << dp[p2n - 1] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 16; const int N2 = (1 << N); int n, a[N][N]; long long cost[N2], dp[N2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } int p2n = (1 << n); for (int bm = 0; bm < p2n; bm++) { for (int i = 1; i < n; i++) { if ((bm & (1 << i)) == 0) continue; for (int j = 0; j < i; j++) { if ((bm & (1 << j)) == 0) continue; cost[bm] += (long long)a[i][j]; } } } for (int bm = 1; bm < p2n; bm++) { long long ans = 0; for (int sbm = bm; sbm > 0; sbm = (sbm - 1) & bm) { if ((sbm & bm) != sbm) continue; int cbm = bm ^ sbm; ans = max(ans, cost[sbm] + dp[cbm]); } dp[bm] = ans; } cout << dp[p2n - 1] << '\n'; return 0; }
replace
35
36
35
36
TLE
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<double> vd; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<string> vs; typedef vector<pi> vp; typedef vector<pl> vpl; int n; vl pre; vl dp; void f(int i, vi p, ll s, int mask, int g) { int len = p.size(); if (i == len) { dp[mask] = max(dp[mask], s + pre[g]); return; } f(i + 1, p, s, mask, g); f(i + 1, p, s, (mask ^ (1 << p[i])), g ^ (1 << p[i])); } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n; int z = (1 << n); vector<vi> a(n, vi(n, 0)); for (auto &i : a) for (auto &j : i) cin >> j; pre.resize(z, 0); ll nn = INT64_MIN + 1000000; dp.resize(z, nn); for (int i = 0; i < (1 << n); ++i) { for (int j = 0; j < n; ++j) { if (!(i & (1 << j))) continue; for (int k = j + 1; k < n; ++k) { if (!(i & (1 << k))) continue; pre[i] += a[k][j]; } } } dp[0] = 0; for (int i = 0; i < z; ++i) { vi p; for (int j = 0; j < n; ++j) { if (!(i & (1 << j))) p.push_back(j); } f(0, p, dp[i], i, 0); } cout << dp[z - 1] << '\n'; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<double> vd; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<string> vs; typedef vector<pi> vp; typedef vector<pl> vpl; int n; vl pre; vl dp; void f(int i, const vi &p, ll s, int mask, int g) { if (i == (int)p.size()) { dp[mask] = max(dp[mask], s + pre[g]); return; } f(i + 1, p, s, mask, g); f(i + 1, p, s, (mask ^ (1 << p[i])), g ^ (1 << p[i])); } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n; int z = (1 << n); vector<vi> a(n, vi(n, 0)); for (auto &i : a) for (auto &j : i) cin >> j; pre.resize(z, 0); ll nn = INT64_MIN + 1000000; dp.resize(z, nn); for (int i = 0; i < (1 << n); ++i) { for (int j = 0; j < n; ++j) { if (!(i & (1 << j))) continue; for (int k = j + 1; k < n; ++k) { if (!(i & (1 << k))) continue; pre[i] += a[k][j]; } } } dp[0] = 0; for (int i = 0; i < z; ++i) { vi p; for (int j = 0; j < n; ++j) { if (!(i & (1 << j))) p.push_back(j); } f(0, p, dp[i], i, 0); } cout << dp[z - 1] << '\n'; }
replace
21
24
21
23
TLE
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair<int, int> P; #define yn \ { puts("Yes"); } \ else { \ puts("No"); \ } #define MAX_N 17 #define MAX_M 1000005 ll dp[MAX_M]; ll a[MAX_N][MAX_N]; ll sum[MAX_M]; int n, m; void dfs(int x) { if (dp[x] != -1) return; dp[x] = sum[x]; if (__builtin_popcount(x) <= 1) return; vector<int> f; int xx = x; rep(i, n) { if (xx % 2 == 1) f.push_back(i); xx /= 2; } int w = f.size(); rep(i, 1 << w) { int ii = i; int y = 0, z = 0; rep(j, w) { if (ii % 2 == 0) y += 1 << f[j]; else z += 1 << f[j]; ii /= 2; } if (y == 0 || z == 0) continue; dfs(y); dfs(z); dp[x] = max(dp[x], dp[y] + dp[z]); } return; } int main() { cin >> n; rep(i, n) rep(j, n) cin >> a[i][j]; m = 1 << n; rep(i, m) { vector<int> f; int ii = i; rep(j, n) { if (ii % 2 == 1) f.push_back(j); ii /= 2; } rep(j, f.size()) { srep(k, j + 1, f.size()) { sum[i] += a[f[j]][f[k]]; } } } rep(i, m) dp[i] = -1; dfs(m - 1); ll ans = dp[m - 1]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair<int, int> P; #define yn \ { puts("Yes"); } \ else { \ puts("No"); \ } #define MAX_N 17 #define MAX_M 1000005 ll dp[MAX_M]; ll a[MAX_N][MAX_N]; ll sum[MAX_M]; int n, m; void dfs(int x) { if (dp[x] != -1) return; dp[x] = sum[x]; if (__builtin_popcount(x) <= 1) return; for (int i = x; i > 0; --i &= x) { int y = i; int z = i ^ x; if (y == 0 || z == 0) continue; dfs(y); dfs(z); dp[x] = max(dp[x], dp[y] + dp[z]); } return; } int main() { cin >> n; rep(i, n) rep(j, n) cin >> a[i][j]; m = 1 << n; rep(i, m) { vector<int> f; int ii = i; rep(j, n) { if (ii % 2 == 1) f.push_back(j); ii /= 2; } rep(j, f.size()) { srep(k, j + 1, f.size()) { sum[i] += a[f[j]][f[k]]; } } } rep(i, m) dp[i] = -1; dfs(m - 1); ll ans = dp[m - 1]; cout << ans << endl; return 0; }
replace
27
47
27
30
TLE
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define inf 1 << 60 int dp[(1 << 16)]; int sum[(1 << 16)]; int calculate(vector<vector<int>> &score, int mask) { int ans = 0; for (int i = 0; i < 17; i++) { for (int j = i + 1; j < 17; j++) { if (((mask & (1 << i)) != 0) && ((mask & (1 << j)) != 0)) ans += score[i][j]; } } return ans; } void prepareSum(vector<vector<int>> &score, int n) { for (int i = 1; i < (1 << n); i++) dp[i] = inf; // summing up the values for each and every subset for (int submask = 1; submask < (1 << n); submask++) sum[submask] = calculate(score, submask); } int solve(vector<vector<int>> &score, int mask) { if (mask == 0) return 0; if (dp[mask] != inf) return dp[mask]; int ans = 0; for (int submask = mask; submask != 0; submask = (submask - 1) & mask) // generating all the subsets of the mask ans = max(ans, sum[submask] + solve(score, mask ^ submask)); // xoring the submask and mask return dp[mask] = ans; } signed main() { int n; cin >> n; vector<vector<int>> score(n, vector<int>(n)); // memset(dp, inf, sizeof dp); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> score[i][j]; prepareSum(score, n); cout << solve(score, (1 << n) - 1); }
#include <bits/stdc++.h> using namespace std; #define int long long #define inf (int)1 << 60 int dp[(1 << 16)]; int sum[(1 << 16)]; int calculate(vector<vector<int>> &score, int mask) { int ans = 0; for (int i = 0; i < 17; i++) { for (int j = i + 1; j < 17; j++) { if (((mask & (1 << i)) != 0) && ((mask & (1 << j)) != 0)) ans += score[i][j]; } } return ans; } void prepareSum(vector<vector<int>> &score, int n) { for (int i = 1; i < (1 << n); i++) dp[i] = inf; // summing up the values for each and every subset for (int submask = 1; submask < (1 << n); submask++) sum[submask] = calculate(score, submask); } int solve(vector<vector<int>> &score, int mask) { if (mask == 0) return 0; if (dp[mask] != inf) return dp[mask]; int ans = 0; for (int submask = mask; submask != 0; submask = (submask - 1) & mask) // generating all the subsets of the mask ans = max(ans, sum[submask] + solve(score, mask ^ submask)); // xoring the submask and mask return dp[mask] = ans; } signed main() { int n; cin >> n; vector<vector<int>> score(n, vector<int>(n)); // memset(dp, inf, sizeof dp); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> score[i][j]; prepareSum(score, n); cout << solve(score, (1 << n) - 1); }
replace
3
4
3
4
TLE
p03180
C++
Time Limit Exceeded
#include <iostream> #include <vector> #define endl '\n' using namespace std; const int MAXN = 16; long long points[MAXN][MAXN], dp[(1 << MAXN) + 3], total[(1 << MAXN) + 3]; void calc_dp(int mask, int curr, int ind, vector<int> missing) { if (ind == (int)missing.size()) { int new_mask = (mask | curr); dp[new_mask] = max(dp[new_mask], dp[mask] + total[curr]); return; } calc_dp(mask, curr, ind + 1, missing); calc_dp(mask, (curr | (1 << missing[ind])), ind + 1, missing); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; ++i) { for (int i2 = 0; i2 < n; ++i2) { cin >> points[i][i2]; } } for (int mask = 0; mask < (1 << n); ++mask) { for (int i = 0; i < n; ++i) { for (int i2 = i + 1; i2 < n; ++i2) { if ((mask & (1 << i)) and (mask & (1 << i2))) { total[mask] += points[i][i2]; } } } } for (int mask = 0; mask < (1 << n); ++mask) { vector<int> missing; for (int i = 0; i < n; ++i) { if (!(mask & (1 << i))) { missing.push_back(i); } } calc_dp(mask, 0, 0, missing); } cout << dp[(1 << n) - 1] << endl; return 0; }
#include <iostream> #include <vector> #define endl '\n' using namespace std; const int MAXN = 16; long long points[MAXN][MAXN], dp[(1 << MAXN) + 3], total[(1 << MAXN) + 3]; void calc_dp(int mask, int curr, int ind, const vector<int> &missing) { if (ind == (int)missing.size()) { int new_mask = (mask | curr); dp[new_mask] = max(dp[new_mask], dp[mask] + total[curr]); return; } calc_dp(mask, curr, ind + 1, missing); calc_dp(mask, (curr | (1 << missing[ind])), ind + 1, missing); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; ++i) { for (int i2 = 0; i2 < n; ++i2) { cin >> points[i][i2]; } } for (int mask = 0; mask < (1 << n); ++mask) { for (int i = 0; i < n; ++i) { for (int i2 = i + 1; i2 < n; ++i2) { if ((mask & (1 << i)) and (mask & (1 << i2))) { total[mask] += points[i][i2]; } } } } for (int mask = 0; mask < (1 << n); ++mask) { vector<int> missing; for (int i = 0; i < n; ++i) { if (!(mask & (1 << i))) { missing.push_back(i); } } calc_dp(mask, 0, 0, missing); } cout << dp[(1 << n) - 1] << endl; return 0; }
replace
10
11
10
11
TLE
p03180
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> #define all(x) (x).begin(), (x).end() using namespace std; typedef long long LL; typedef unsigned int uii; typedef pair<int, int> pii; typedef unsigned long long uLL; typedef vector<int> vii; const int maxn = 1e5; LL a[20][20], d[maxn], vis[maxn][20]; inline int num(int c, int t[]) { int res = 0; for (int i = 0; i < c; ++i) { res += 1 << t[i]; } return res; } int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } for (int i = 0; i < (1 << n); ++i) { int b[20], c = 0; for (int j = 0; j < n; ++j) { if (i & (1 << j)) { b[c++] = j; } } for (int j = 0; j < c; ++j) { for (int k = j + 1; k < c; ++k) { d[i] += a[b[j]][b[k]]; } } } int num = (1 << n) - 1; int c = 0; for (int i = 1; i <= num; ++i) { // cout << i << endl; for (int j = i; j > 0; j = (j - 1) & i) { // cout << j << ' '; d[i] = max(d[i], d[j] + d[j ^ i]); ++c; } // cout << endl; } cout << d[num] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> #define all(x) (x).begin(), (x).end() using namespace std; typedef long long LL; typedef unsigned int uii; typedef pair<int, int> pii; typedef unsigned long long uLL; typedef vector<int> vii; const int maxn = 1e5; LL a[20][20], d[maxn], vis[maxn][20]; inline int num(int c, int t[]) { int res = 0; for (int i = 0; i < c; ++i) { res += 1 << t[i]; } return res; } int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } for (int i = 0; i < (1 << n); ++i) { int b[20], c = 0; for (int j = 0; j < n; ++j) { if (i & (1 << j)) { b[c++] = j; } } for (int j = 0; j < c; ++j) { for (int k = j + 1; k < c; ++k) { d[i] += a[b[j]][b[k]]; } } } int num = (1 << n) - 1; int c = 0; for (int i = 1; i <= num; ++i) { // cout << i << endl; for (int j = i; j > 0; j = (j - 1) & i) { // cout << j << ' '; d[i] = max(d[i], d[j] + d[j ^ i]); ++c; } // cout << endl; } cout << d[num] << endl; return 0; }
replace
27
28
27
28
TLE
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvl = vector<vll>; using vb = vector<bool>; const int INF = 2e9 + 5; const ll LLINF = (1LL << 60) + 5; /* filippos Contest : AtCoder DP contest Task : U - Grouping */ int main() { cin.tie(0), cin.sync_with_stdio(0), cout.tie(0), cout.sync_with_stdio(0); int n; cin >> n; vvi v(n, vi(n)); for (auto &x : v) for (auto &i : x) cin >> i; vll mask_value(1 << n, 0); for (int mask = 0; mask < (1 << n); mask++) { ll &value = mask_value[mask]; vi indexes; for (int i = 0; i < n; i++) if (mask & (1 << i)) indexes.push_back(i); for (int i = 0; i < indexes.size(); i++) for (int j = i + 1; j < indexes.size(); j++) value += v[indexes[i]][indexes[j]]; } vll memo(1 << n, -1); function<ll(int)> dp = [&](int state) { ll &ans = memo[state]; if (ans != -1) return ans; ans = 0; // iterate over all masks is too slow (2^2N = 2^32) // so we iterate only over valid masks (should be less lol) vi indexes; for (int i = 0; i < n; i++) if (!(state & (1 << i))) indexes.push_back(i); // iterate over submasks for (int sub = 1; sub < (1 << indexes.size()); sub++) { int mask = 0; for (int j = 0; j < indexes.size(); j++) { if (sub & (1 << j)) { mask |= (1 << indexes[j]); } } // dp for each mask ans = max(ans, mask_value[mask] + dp(state | mask)); } return ans; }; cout << dp(0); return cout << endl, 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvl = vector<vll>; using vb = vector<bool>; const int INF = 2e9 + 5; const ll LLINF = (1LL << 60) + 5; /* filippos Contest : AtCoder DP contest Task : U - Grouping */ int main() { cin.tie(0), cin.sync_with_stdio(0), cout.tie(0), cout.sync_with_stdio(0); int n; cin >> n; vvi v(n, vi(n)); for (auto &x : v) for (auto &i : x) cin >> i; vll mask_value(1 << n, 0); for (int mask = 0; mask < (1 << n); mask++) { ll &value = mask_value[mask]; vi indexes; for (int i = 0; i < n; i++) if (mask & (1 << i)) indexes.push_back(i); for (int i = 0; i < indexes.size(); i++) for (int j = i + 1; j < indexes.size(); j++) value += v[indexes[i]][indexes[j]]; } vll memo(1 << n, -1); function<ll(int)> dp = [&](int state) { ll &ans = memo[state]; if (ans != -1) return ans; ans = 0; // iterate over all masks is too slow (2^2N = 2^32) // so we iterate only over valid masks (should be less lol) vi indexes; for (int i = 0; i < n; i++) if (!(state & (1 << i))) indexes.push_back(i); // iterate over submasks for (int sub = 1; sub < (1 << indexes.size()); sub++) { int mask = 0; for (int j = 0; j < indexes.size(); j++) mask |= ((((sub >> j) & 1)) << indexes[j]); // dp for each mask ans = max(ans, mask_value[mask] + dp(state | mask)); } return ans; }; cout << dp(0); return cout << endl, 0; }
replace
60
65
60
62
TLE
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define lint long long const int N = 16; lint cost[N][N]; int n; lint subsetCost[1 << N]; lint dp[1 << N]; lint solve(int mask) { if (__builtin_popcount(mask) == n) return 0; if (dp[mask] != -1) return dp[mask]; lint sum = 0; vector<int> indices; indices.clear(); for (int i = 0; i < n; i++) if (!(mask & (1 << i))) indices.push_back(i); int sz = indices.size(); for (int bitmask = 1; bitmask < (1 << sz); bitmask++) { int num = 0; for (int i = 0; i < sz; i++) if ((1 << i) & bitmask) num += (1 << indices[i]); if (!(num & mask)) sum = max(sum, subsetCost[num] + solve(mask | num)); } return dp[mask] = sum; } int main() { cin >> n; vector<int> subset; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> cost[i][j]; for (int mask = 0; mask < (1 << n); mask++) { subset.clear(); for (int i = 0; i < n; i++) if (mask & (1 << i)) subset.push_back(i); lint c = 0; for (int x : subset) for (int y : subset) c += cost[x][y]; subsetCost[mask] = c / 2; } // cout << "DONE!" << endl ; memset(dp, -1, sizeof(dp)); cout << solve(0); }
#include <bits/stdc++.h> using namespace std; #define lint long long const int N = 16; lint cost[N][N]; int n; lint subsetCost[1 << N]; lint dp[1 << N]; lint solve(int mask) { if (__builtin_popcount(mask) == n) return 0; if (dp[mask] != -1) return dp[mask]; lint sum = 0; vector<int> indices; indices.clear(); for (int i = 0; i < n; i++) if (!(mask & (1 << i))) indices.push_back(i); int sz = indices.size(); for (int bitmask = 1; bitmask < (1 << sz); bitmask++) { int num = 0; for (int i = 0; i < sz; i++) if ((1 << i) & bitmask) num += (1 << indices[i]); if (!(num & mask)) sum = max(sum, subsetCost[num] + solve(mask | num)); } return dp[mask] = sum; } int main() { cin >> n; vector<int> subset; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> cost[i][j]; for (int mask = 0; mask < (1 << n); mask++) { subset.clear(); for (int i = 0; i < n; i++) if (mask & (1 << i)) subset.push_back(i); lint c = 0; for (int x : subset) for (int y : subset) c += cost[x][y]; subsetCost[mask] = c / 2; } // cout << "DONE!" << endl ; dp[0] = 0; for (int mask = 1; mask < (1 << n); mask++) { lint sum = 0; vector<int> indices; indices.clear(); for (int i = 0; i < n; i++) if ((mask & (1 << i))) indices.push_back(i); int sz = indices.size(); for (int bitmask = 0; bitmask < (1 << sz); bitmask++) { int num = 0; for (int i = 0; i < sz; i++) if ((1 << i) & bitmask) num += (1 << indices[i]); sum = max(sum, subsetCost[num] + dp[num ^ mask]); } dp[mask] = sum; } // memset(dp, -1, sizeof(dp)); cout << dp[(1 << n) - 1]; // cout << solve(0); }
replace
56
58
56
78
TLE
p03180
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 16; int matrix[MAXN + 7][MAXN + 7]; long long cost[MAXN + 7]; long long dp[(1 << MAXN) + 7]; vector<int> masks_by_size[MAXN + 7]; int N; bool checkbit(int mask, int i) { return (mask >> i) & 1; } void precalc() { for (int mask = 1; mask < (1 << N); mask++) { // cerr << mask << " " << __builtin_popcount(mask) << "\n"; masks_by_size[__builtin_popcount(mask)].push_back(mask); for (int i = 1; i <= N; i++) { if (checkbit(mask, i - 1) == true) { for (int j = i + 1; j <= N; j++) { if (checkbit(mask, j - 1) == true) { cost[mask] += matrix[i][j]; } } } } } } long long dpf(int mask) { // cerr << mask << "\n"; long long &d = dp[mask]; if (d != -1) { return d; } if (mask == (1 << N) - 1) { return d = 0; } d = 0; for (int bit_cnt = 1; bit_cnt <= N / 2; bit_cnt++) { for (int mask2 : masks_by_size[bit_cnt]) { if ((mask & mask2) == 0) { // cerr << mask << " + " << mask2 << " -> " << cost[mask2] << " " << // (mask | mask2) << "\n"; d = max(d, cost[mask2] + dpf(mask | mask2)); } } } d = max(d, cost[((1 << N) - 1) ^ mask]); return d; } int main() { memset(dp, -1, sizeof dp); cin >> N; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { cin >> matrix[i][j]; } } precalc(); cout << dpf(0) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 16; int matrix[MAXN + 7][MAXN + 7]; long long cost[(1 << MAXN) + 7]; long long dp[(1 << MAXN) + 7]; vector<int> masks_by_size[MAXN + 7]; int N; bool checkbit(int mask, int i) { return (mask >> i) & 1; } void precalc() { for (int mask = 1; mask < (1 << N); mask++) { // cerr << mask << " " << __builtin_popcount(mask) << "\n"; masks_by_size[__builtin_popcount(mask)].push_back(mask); for (int i = 1; i <= N; i++) { if (checkbit(mask, i - 1) == true) { for (int j = i + 1; j <= N; j++) { if (checkbit(mask, j - 1) == true) { cost[mask] += matrix[i][j]; } } } } } } long long dpf(int mask) { // cerr << mask << "\n"; long long &d = dp[mask]; if (d != -1) { return d; } if (mask == (1 << N) - 1) { return d = 0; } d = 0; for (int bit_cnt = 1; bit_cnt <= N / 2; bit_cnt++) { for (int mask2 : masks_by_size[bit_cnt]) { if ((mask & mask2) == 0) { // cerr << mask << " + " << mask2 << " -> " << cost[mask2] << " " << // (mask | mask2) << "\n"; d = max(d, cost[mask2] + dpf(mask | mask2)); } } } d = max(d, cost[((1 << N) - 1) ^ mask]); return d; } int main() { memset(dp, -1, sizeof dp); cin >> N; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { cin >> matrix[i][j]; } } precalc(); cout << dpf(0) << "\n"; return 0; }
replace
7
8
7
8
0
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long pair[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> pair[i][j]; } } vector<long long> dp((1 << n), 0); for (int bit = 0; bit < (1 << n); bit++) { bitset<16> b = bit; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (b[i] && b[j]) { dp[bit] += pair[i][j]; } } } } for (int k = 2; k <= n; k++) { for (int bit = 0; bit < (1 << n); bit++) { bitset<16> b = bit; if (b.count() != k) continue; for (int bit2 = 0; bit2 < (1 << n); bit2++) { if ((bit | bit2) ^ bit) continue; dp[bit] = max(dp[bit], dp[bit2] + dp[bit ^ bit2]); } } } cout << dp[(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long pair[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> pair[i][j]; } } vector<long long> dp((1 << n), 0); for (int bit = 0; bit < (1 << n); bit++) { bitset<16> b = bit; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (b[i] && b[j]) { dp[bit] += pair[i][j]; } } } } for (int k = 2; k <= n; k++) { for (int bit = 0; bit < (1 << n); bit++) { bitset<16> b = bit; if (b.count() != k) continue; for (int bit2 = bit; bit2 > 0; bit2 = (bit2 - 1) & bit) { dp[bit] = max(dp[bit], dp[bit2] + dp[bit ^ bit2]); } } } cout << dp[(1 << n) - 1] << endl; return 0; }
replace
28
31
28
29
TLE
p03180
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const static ll MOD = 1e9 + 7; const static ll INF = 1e14; ll N; vector<vector<ll>> V(17, vector<ll>(17)); vector<ll> DP; int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> V[i][j]; } } DP.resize(1 << N); for (int i = 0; i < (1 << N); i++) { bitset<17> B = i; ll score = 0; for (int i = 0; i < N; i++) { for (int j = i; j < N; j++) { if (B[i] == 1 && B[j] == 1) score += V[i][j]; } } DP[i] = score; } for (int i1 = 0; i1 < (1 << N); i1++) { bitset<17> B = i1; ll cnt = B.count(); for (int t = i1; t >= 0; t = (t - 1) & i1) { // for(int t = (i1-1)&i1; t > 0; t = (t-1)&i1){ DP[i1] = max(DP[i1], DP[t] + DP[i1 - t]); } } cout << DP[(1 << N) - 1] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const static ll MOD = 1e9 + 7; const static ll INF = 1e14; ll N; vector<vector<ll>> V(17, vector<ll>(17)); vector<ll> DP; int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> V[i][j]; } } DP.resize(1 << N); for (int i = 0; i < (1 << N); i++) { bitset<17> B = i; ll score = 0; for (int i = 0; i < N; i++) { for (int j = i; j < N; j++) { if (B[i] == 1 && B[j] == 1) score += V[i][j]; } } DP[i] = score; } for (int i1 = 0; i1 < (1 << N); i1++) { bitset<17> B = i1; ll cnt = B.count(); for (int t = i1; t > 0; t = (t - 1) & i1) { // for(int t = (i1-1)&i1; t > 0; t = (t-1)&i1){ DP[i1] = max(DP[i1], DP[t] + DP[i1 - t]); } } cout << DP[(1 << N) - 1] << endl; }
replace
32
33
32
33
TLE
p03180
C++
Time Limit Exceeded
#define taskname "" #include <algorithm> #include <iostream> #include <vector> using namespace std; #define long long long const int N = 16 + 10; const int MOD = 1e9 + 7; int n, a[N][N], mark[1 << N]; long dp[1 << N]; inline int bit(int i, int mask) { return (mask >> i) & 1; } void cre_dp() { for (int mask = 1; mask < (1 << n); ++mask) { for (int i = 0; i < n; ++i) if (bit(i, mask)) for (int j = i + 1; j < n; ++j) if (bit(j, mask)) dp[mask] += a[i][j]; } } long get(int mask) { if (mark[mask]) return dp[mask]; vector<int> pos; for (int i = 0; i < n; ++i) if (bit(i, mask)) pos.push_back(i); int num = pos.size(); for (int i = 1; i < (1 << num) - 1; ++i) { int cnt = 0; for (int j = 0; j < num; ++j) if (bit(j, i)) cnt += 1 << pos[j]; dp[mask] = max(dp[mask], get(cnt) + get(mask ^ cnt)); } mark[mask] = 1; return dp[mask]; } int main() { // freopen(taskname".INP", "r", stdin); // freopen(taskname".OUT", "w", stdout); cin.tie(nullptr); ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> a[i][j]; cre_dp(); cout << get((1 << n) - 1); return 0; }
#define taskname "" #include <algorithm> #include <iostream> #include <vector> using namespace std; #define long long long const int N = 16 + 10; const int MOD = 1e9 + 7; int n, a[N][N], mark[1 << N]; long dp[1 << N]; inline int bit(int i, int mask) { return (mask >> i) & 1; } void cre_dp() { for (int mask = 1; mask < (1 << n); ++mask) { for (int i = 0; i < n; ++i) if (bit(i, mask)) for (int j = i + 1; j < n; ++j) if (bit(j, mask)) dp[mask] += a[i][j]; } } long get(int mask) { if (mark[mask]) return dp[mask]; for (int t = (mask - 1) & mask; t; t = (t - 1) & mask) dp[mask] = max(dp[mask], get(t) + get(mask ^ t)); mark[mask] = 1; return dp[mask]; } int main() { // freopen(taskname".INP", "r", stdin); // freopen(taskname".OUT", "w", stdout); cin.tie(nullptr); ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> a[i][j]; cre_dp(); cout << get((1 << n) - 1); return 0; }
replace
29
43
29
31
TLE
p03180
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> #define int long long #define rep(i, n) for (i = 0; i < n; i++) using namespace std; void chmax(int &a, int b) { a = max(a, b); } int INF = 1e+16; int n; int a[16][16]; int score[1 << 16]; int dp[1 << 16]; // dp[used] = max-value signed main() { int i, j, k; cin >> n; rep(i, n) rep(j, n) cin >> a[i][j]; rep(i, (1 << n)) { rep(j, n) { if ((i >> j) % 2 == 0) continue; for (k = j + 1; k < n; k++) { if ((i >> k) % 2 == 0) continue; score[i] += a[j][k]; } } } rep(i, (1 << n)) dp[i] = -INF; dp[0] = 0; rep(i, (1 << n) - 1) { int uku[16], cnt = 0; rep(j, n) { if ((i >> j) % 2 == 0) { uku[cnt++] = j; } } rep(j, (1 << cnt)) { if (j == 0) continue; int beet = 0; rep(k, cnt) { if ((j >> k) % 2 == 1) { beet += (1 << uku[k]); } } chmax(dp[i + beet], dp[i] + score[beet]); } } cout << dp[(1 << n) - 1] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> #define int long long #define rep(i, n) for (i = 0; i < n; i++) using namespace std; void chmax(int &a, int b) { a = max(a, b); } int INF = 1e+16; int n; int a[16][16]; int score[1 << 16]; int dp[1 << 16]; // dp[used] = max-value signed main() { int i, j, k; cin >> n; rep(i, n) rep(j, n) cin >> a[i][j]; rep(i, (1 << n)) { rep(j, n) { if ((i >> j) % 2 == 0) continue; for (k = j + 1; k < n; k++) { if ((i >> k) % 2 == 0) continue; score[i] += a[j][k]; } } } rep(i, (1 << n)) dp[i] = -INF; dp[0] = 0; rep(i, (1 << n) - 1) { int uku[16], cnt = 0; rep(j, n) { if ((i >> j) % 2 == 0) { uku[cnt++] = j; } } rep(j, (1 << cnt)) { if (j == 0) continue; if (j % 2 == 0) continue; int beet = 0; rep(k, cnt) { if ((j >> k) % 2 == 1) { beet += (1 << uku[k]); } } chmax(dp[i + beet], dp[i] + score[beet]); } } cout << dp[(1 << n) - 1] << endl; return 0; }
insert
56
56
56
58
TLE
p03180
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long solve() { int n; cin >> n; long long A[n][n]; long long dp[(1 << n)]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> A[i][j]; for (int i = 1; i < 1 << 16; i++) { dp[i] = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { for (int k = j + 1; k < n; k++) { if (i & (1 << k)) dp[i] += A[j][k]; } } } } for (int mask = 1; mask < 1 << n; mask++) { for (int ss = mask; ss > 0; ss = (ss - 1) & mask) { dp[mask] = max(dp[mask], dp[ss] + dp[mask ^ ss]); } } return dp[(1 << n) - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << solve() << "\n"; }
#include <bits/stdc++.h> using namespace std; long long solve() { int n; cin >> n; long long A[n][n]; long long dp[(1 << n)]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> A[i][j]; dp[0] = 0; for (int i = 1; i < 1 << n; i++) { dp[i] = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { for (int k = j + 1; k < n; k++) { if (i & (1 << k)) dp[i] += A[j][k]; } } } } for (int mask = 1; mask < 1 << n; mask++) { for (int ss = mask; ss > 0; ss = (ss - 1) & mask) { dp[mask] = max(dp[mask], dp[ss] + dp[mask ^ ss]); } } return dp[(1 << n) - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << solve() << "\n"; }
replace
10
11
10
12
TLE
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ALL(x) x.begin(), x.end() #define rep(i, n) for (int i = 0; i < n; i++) #define debug(v) \ cout << #v << ":"; \ for (auto x : v) { \ cout << x << ' '; \ } \ cout << endl; #define INF 1000000000 // #define mod 1000000007 using ll = long long; const ll LINF = 1001002003004005006ll; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } 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; } // 部分木の数え上げ ll mod, n; vector<vector<int>> g; vector<ll> dp, par, ans; ll dfs(int pre, int now) { ll ret = 1; for (auto to : g[now]) if (to != pre) { par[to] = now; ret = ret * (1 + dfs(now, to)) % mod; } return dp[now] = ret; } void fill_dp(int now) { // cout<<now<<" : "<<dp[now]<<endl; int m = (int)g[now].size(); vector<ll> l(m), r(m); l[0] = dp[g[now][0]] + 1; r[m - 1] = dp[g[now][m - 1]] + 1; for (int i = 1; i < m; i++) l[i] = l[i - 1] * (dp[g[now][i]] + 1) % mod; for (int i = m - 2; i >= 0; i--) r[i] = r[i + 1] * (dp[g[now][i]] + 1) % mod; ans[now] = 1; for (auto to : g[now]) ans[now] = ans[now] * (dp[to] + 1) % mod; rep(i, m) if (par[now] != g[now][i]) { dp[now] = 1; if (i > 0) dp[now] = dp[now] * l[i - 1] % mod; if (i < m - 1) dp[now] = dp[now] * r[i + 1] % mod; fill_dp(g[now][i]); } } signed main() { cin.tie(0); ios::sync_with_stdio(0); cin >> n >> mod; g.resize(n); dp.resize(n, 0); par.resize(n, -1); ans.resize(n); rep(i, n - 1) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); } dfs(-1, 0); ans[0] = dp[0]; fill_dp(0); // debug(par); // debug(dp);debug(ans); rep(i, n) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ALL(x) x.begin(), x.end() #define rep(i, n) for (int i = 0; i < n; i++) #define debug(v) \ cout << #v << ":"; \ for (auto x : v) { \ cout << x << ' '; \ } \ cout << endl; #define INF 1000000000 // #define mod 1000000007 using ll = long long; const ll LINF = 1001002003004005006ll; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } 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; } // 部分木の数え上げ ll mod, n; vector<vector<int>> g; vector<ll> dp, par, ans; ll dfs(int pre, int now) { ll ret = 1; for (auto to : g[now]) if (to != pre) { par[to] = now; ret = ret * (1 + dfs(now, to)) % mod; } return dp[now] = ret; } void fill_dp(int now) { // cout<<now<<" : "<<dp[now]<<endl; int m = (int)g[now].size(); vector<ll> l(m), r(m); l[0] = dp[g[now][0]] + 1; r[m - 1] = dp[g[now][m - 1]] + 1; for (int i = 1; i < m; i++) l[i] = l[i - 1] * (dp[g[now][i]] + 1) % mod; for (int i = m - 2; i >= 0; i--) r[i] = r[i + 1] * (dp[g[now][i]] + 1) % mod; ans[now] = 1; for (auto to : g[now]) ans[now] = ans[now] * (dp[to] + 1) % mod; rep(i, m) if (par[now] != g[now][i]) { dp[now] = 1; if (i > 0) dp[now] = dp[now] * l[i - 1] % mod; if (i < m - 1) dp[now] = dp[now] * r[i + 1] % mod; fill_dp(g[now][i]); } } signed main() { cin.tie(0); ios::sync_with_stdio(0); cin >> n >> mod; if (n == 1) { cout << 1 << endl; return 0; } g.resize(n); dp.resize(n, 0); par.resize(n, -1); ans.resize(n); rep(i, n - 1) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); } dfs(-1, 0); ans[0] = dp[0]; fill_dp(0); // debug(par); // debug(dp);debug(ans); rep(i, n) cout << ans[i] << endl; return 0; }
insert
76
76
76
80
0
p03181
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define INFLL 2000000000000000000 #define INF 2000000000 #define MOD 1000000007 #define BASE 77747 #define PI acos(-1.0) #define MAXLEN 29 using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef vector<ll> vll; int n; vector<int> t[100000]; ll m; ll dp[100000]; ll ans[100000]; void rec(int cur, int par) { dp[cur] = 1; for (int i = 0; i < t[cur].size(); i++) { if (t[cur][i] == par) { swap(t[cur][i], t[cur][t[cur].size() - 1]); break; } } if (t[cur][t[cur].size() - 1] == par) t[cur].pop_back(); for (int i = 0; i < t[cur].size(); i++) { rec(t[cur][i], cur); dp[cur] *= (dp[t[cur][i]] + 1) % m; dp[cur] %= m; } } void solve(int cur, int par, ll pval) { ans[cur] = dp[cur] * pval; ans[cur] %= m; vll le(t[cur].size()), ri(t[cur].size()); int sz = t[cur].size(); if (sz > 0) { le[0] = dp[t[cur][0]] + 1; ri[sz - 1] = dp[t[cur][sz - 1]] + 1; } for (int i = 1; i < t[cur].size(); i++) { le[i] = le[i - 1] * (dp[t[cur][i]] + 1); le[i] %= m; ri[sz - i - 1] = (dp[t[cur][sz - i - 1]] + 1) * ri[sz - i]; ri[sz - i - 1] %= m; } // for (int i = 0; i < t[cur].size(); i++) { // cout << le[i] << " " << ri[i] << endl; // } // cout << endl; for (int i = 0; i < t[cur].size(); i++) { ll left = 1; ll right = 1; if (i > 0) left = le[i - 1]; if (i < t[cur].size() - 1) right = ri[i + 1]; solve(t[cur][i], cur, ((((pval * left) % m) * right) + 1) % m); } } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n >> m; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; t[b].push_back(a); t[a].push_back(b); } rec(0, -1); solve(0, -1, 1); for (int i = 0; i < n; i++) { printf("%lld\n", ans[i]); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define INFLL 2000000000000000000 #define INF 2000000000 #define MOD 1000000007 #define BASE 77747 #define PI acos(-1.0) #define MAXLEN 29 using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef vector<ll> vll; int n; vector<int> t[100000]; ll m; ll dp[100000]; ll ans[100000]; void rec(int cur, int par) { dp[cur] = 1; for (int i = 0; i < t[cur].size(); i++) { if (t[cur][i] == par) { swap(t[cur][i], t[cur][t[cur].size() - 1]); break; } } if (t[cur].size() > 0 && t[cur][t[cur].size() - 1] == par) t[cur].pop_back(); for (int i = 0; i < t[cur].size(); i++) { rec(t[cur][i], cur); dp[cur] *= (dp[t[cur][i]] + 1) % m; dp[cur] %= m; } } void solve(int cur, int par, ll pval) { ans[cur] = dp[cur] * pval; ans[cur] %= m; vll le(t[cur].size()), ri(t[cur].size()); int sz = t[cur].size(); if (sz > 0) { le[0] = dp[t[cur][0]] + 1; ri[sz - 1] = dp[t[cur][sz - 1]] + 1; } for (int i = 1; i < t[cur].size(); i++) { le[i] = le[i - 1] * (dp[t[cur][i]] + 1); le[i] %= m; ri[sz - i - 1] = (dp[t[cur][sz - i - 1]] + 1) * ri[sz - i]; ri[sz - i - 1] %= m; } // for (int i = 0; i < t[cur].size(); i++) { // cout << le[i] << " " << ri[i] << endl; // } // cout << endl; for (int i = 0; i < t[cur].size(); i++) { ll left = 1; ll right = 1; if (i > 0) left = le[i - 1]; if (i < t[cur].size() - 1) right = ri[i + 1]; solve(t[cur][i], cur, ((((pval * left) % m) * right) + 1) % m); } } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n >> m; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; t[b].push_back(a); t[a].push_back(b); } rec(0, -1); solve(0, -1, 1); for (int i = 0; i < n; i++) { printf("%lld\n", ans[i]); } return 0; }
replace
44
45
44
45
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long long INF = 1e18; const int MaxN = 1e5; int n, mod; vector<int> v[MaxN]; int dp[MaxN]; vector<int> pref[MaxN], suf[MaxN]; int ans[MaxN]; int pDP[MaxN]; void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } void prod(int &x, int y) { x = (long long)x * y % mod; } void dfs(int x, int p = 0) { if (p) for (int i = 0; i < v[x].size(); ++i) if (v[x][i] == p) { v[x].erase(v[x].begin() + i); break; } int n = v[x].size(); pref[x].resize(n); suf[x].resize(n); for (int i = 0; i < n; ++i) { dfs(v[x][i], x); pref[x][i] = (1 + dp[v[x][i]]) % mod; if (i) { prod(pref[x][i], pref[x][i - 1]); } } for (int i = n - 1; i >= 0; --i) { suf[x][i] = (1 + dp[v[x][i]]) % mod; if (i + 1 < n) { prod(suf[x][i], suf[x][i + 1]); } } dp[x] = (suf[x].empty() ? 1 : suf[x][0]); } void dfs2(int x, int p = 0, int pos = -1) { if (p) { pDP[x] = (pDP[p] + 1) % mod; if (pos > 0) prod(pDP[x], pref[p][pos - 1]); if (pos + 1 < v[p].size()) prod(pDP[x], suf[p][pos + 1]); } int i = 0; for (int to : v[x]) dfs2(to, x, i++); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // freopen("input.txt", "r", stdin); cin >> n >> mod; 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); dfs2(1); for (int i = 1; i <= n; ++i) { int ans = (long long)dp[i] * (1 + pDP[i]) % mod; cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long long INF = 1e18; const int MaxN = 1e5 + 15; int n, mod; vector<int> v[MaxN]; int dp[MaxN]; vector<int> pref[MaxN], suf[MaxN]; int ans[MaxN]; int pDP[MaxN]; void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } void prod(int &x, int y) { x = (long long)x * y % mod; } void dfs(int x, int p = 0) { if (p) for (int i = 0; i < v[x].size(); ++i) if (v[x][i] == p) { v[x].erase(v[x].begin() + i); break; } int n = v[x].size(); pref[x].resize(n); suf[x].resize(n); for (int i = 0; i < n; ++i) { dfs(v[x][i], x); pref[x][i] = (1 + dp[v[x][i]]) % mod; if (i) { prod(pref[x][i], pref[x][i - 1]); } } for (int i = n - 1; i >= 0; --i) { suf[x][i] = (1 + dp[v[x][i]]) % mod; if (i + 1 < n) { prod(suf[x][i], suf[x][i + 1]); } } dp[x] = (suf[x].empty() ? 1 : suf[x][0]); } void dfs2(int x, int p = 0, int pos = -1) { if (p) { pDP[x] = (pDP[p] + 1) % mod; if (pos > 0) prod(pDP[x], pref[p][pos - 1]); if (pos + 1 < v[p].size()) prod(pDP[x], suf[p][pos + 1]); } int i = 0; for (int to : v[x]) dfs2(to, x, i++); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // freopen("input.txt", "r", stdin); cin >> n >> mod; 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); dfs2(1); for (int i = 1; i <= n; ++i) { int ans = (long long)dp[i] * (1 + pDP[i]) % mod; cout << ans << '\n'; } return 0; }
replace
6
7
6
7
0
p03181
C++
Runtime Error
// https://atcoder.jp/contests/dp/tasks/dp_v // V - Subtree #include <algorithm> #include <cassert> #include <iostream> #include <vector> #include <stdio.h> using namespace std; vector<vector<int>> adj; vector<vector<int>> adj_count; vector<long long> ans; vector<bool> visited; int M; long long dfs1(int u) { // cout << "v1" << u << endl; visited[u] = true; long long a = 1; int c = 0; for (auto v : adj[u]) { // cout << "ds " << u << " " << v << endl; if (!visited[v]) { auto r = dfs1(v) + 1; a = (a * r) % M; adj_count[u][c] = r % M; } c++; } return a; } void dfs2(int u, long long father) { // cout << "v2 " << u << " " << father << endl; visited[u] = true; int le = adj[u].size(); if (le == 0) { ans[u] = 1; return; } vector<long long> l(le, 1); vector<long long> r(le, 1); vector<long long> f(le, 1); for (auto i = 0; i < le; i++) { auto v = adj[u][i]; if (visited[v]) adj_count[u][i] = father + 1; // cout << "ac " << adj_count[u][i] << endl; if (i > 0) l[i] = (l[i - 1] * adj_count[u][i]) % M; else l[i] = adj_count[u][0] % M; } r[le - 1] = adj_count[u][le - 1]; if (le > 1) f[le - 1] = l[le - 2]; else f[le - 1] = 1; for (auto i = le - 2; i >= 0; i--) { auto v = adj[u][i]; r[i] = (r[i + 1] * adj_count[u][i]) % M; if (i > 0) f[i] = (l[i - 1] * r[i + 1]) % M; else f[i] = r[i + 1] % M; } assert(r[0] == l[le - 1]); ans[u] = r[0]; for (auto i = 0; i < le; i++) { // cout << "fi" << i << " " << f[i] << endl; auto v = adj[u][i]; if (!visited[v]) dfs2(v, f[i]); } } int main() { int N; cin >> N >> M; adj.assign(N, {}); adj_count.assign(N, {}); ans.assign(N, 0); visited.assign(N, false); for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj_count[u].push_back(0); adj[v].push_back(u); adj_count[v].push_back(0); } dfs1(0); visited.assign(N, false); dfs2(0, 0); for (auto i = 0; i < N; i++) cout << ans[i] << endl; return 0; }
// https://atcoder.jp/contests/dp/tasks/dp_v // V - Subtree #include <algorithm> #include <cassert> #include <iostream> #include <vector> #include <stdio.h> using namespace std; vector<vector<int>> adj; vector<vector<int>> adj_count; vector<long long> ans; vector<bool> visited; int M; long long dfs1(int u) { // cout << "v1" << u << endl; visited[u] = true; long long a = 1; int c = 0; for (auto v : adj[u]) { // cout << "ds " << u << " " << v << endl; if (!visited[v]) { auto r = dfs1(v) + 1; a = (a * r) % M; adj_count[u][c] = r % M; } c++; } return a; } void dfs2(int u, long long father) { // cout << "v2 " << u << " " << father << endl; visited[u] = true; int le = adj[u].size(); if (le == 0) { ans[u] = 1; return; } vector<long long> l(le, 1); vector<long long> r(le, 1); vector<long long> f(le, 1); for (auto i = 0; i < le; i++) { auto v = adj[u][i]; if (visited[v]) adj_count[u][i] = father + 1; // cout << "ac " << adj_count[u][i] << endl; if (i > 0) l[i] = (l[i - 1] * adj_count[u][i]) % M; else l[i] = adj_count[u][0] % M; } r[le - 1] = adj_count[u][le - 1] % M; if (le > 1) f[le - 1] = l[le - 2]; else f[le - 1] = 1; for (auto i = le - 2; i >= 0; i--) { auto v = adj[u][i]; r[i] = (r[i + 1] * adj_count[u][i]) % M; if (i > 0) f[i] = (l[i - 1] * r[i + 1]) % M; else f[i] = r[i + 1] % M; } assert(r[0] == l[le - 1]); ans[u] = r[0]; for (auto i = 0; i < le; i++) { // cout << "fi" << i << " " << f[i] << endl; auto v = adj[u][i]; if (!visited[v]) dfs2(v, f[i]); } } int main() { int N; cin >> N >> M; adj.assign(N, {}); adj_count.assign(N, {}); ans.assign(N, 0); visited.assign(N, false); for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj_count[u].push_back(0); adj[v].push_back(u); adj_count[v].push_back(0); } dfs1(0); visited.assign(N, false); dfs2(0, 0); for (auto i = 0; i < N; i++) cout << ans[i] << endl; return 0; }
replace
64
65
64
65
0
p03181
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; using ll = long long; const int NNODES = 1e5 + 6; vector<int> v[NNODES]; int fatherOf[NNODES]; ll down[NNODES]; ll up[NNODES]; ll prefix[NNODES], suffix[NNODES]; ll dfsDown(int root, int m) { vector<int> l; ll temp = 1; for (int node : v[root]) { if (fatherOf[root] == node) { continue; } fatherOf[node] = root; temp = ((temp % m) * (1 + (dfsDown(node, m) % m)) % m) % m; // calculate prefix, suffix l.push_back(node); } ll t = 1; for (int i = 0; i < l.size(); i++) { // pair<int, int> a(root, l[i]); prefix[l[i]] = t; t = ((t % m) * ((1 + (down[l[i]] % m)) % m)) % m; // printf("sss %d, %d, %lld\n", root, l[i], prefix[{root, l[i]}]); } t = 1; for (int i = l.size() - 1; i >= 0; i--) { // pair<int, int> a(root, l[i]); suffix[l[i]] = t; t = ((t % m) * ((1 + (down[l[i]] % m)) % m)) % m; } down[root] = temp % m; return down[root]; } ll dfsUp(int root, int m) { if (up[root] > 1) { return up[root]; } if (fatherOf[root] == -1) { up[root] = 1; return up[root]; } ll upV = dfsUp(fatherOf[root], m); ll kk = ((prefix[root] % m) * (suffix[root] % m)) % m; up[root] = (((kk * (upV % m)) % m) + 1) % m; return up[root]; } void solve(int n, int m) { int root = 2; fatherOf[root] = -1; dfsDown(root, m); for (int i = 1; i <= n; i++) { dfsUp(i, m); } queue<int> q; q.push(root); while (q.size() > 0) { int it = q.front(); q.pop(); dfsUp(it, m); for (int node : v[it]) { if (node == fatherOf[it]) { continue; } q.push(node); } } } int main() { memset(down, -1, sizeof(down)); memset(up, -1, sizeof(up)); memset(prefix, 1, sizeof(prefix)); memset(suffix, 1, sizeof(suffix)); int n, m; cin >> n >> m; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } solve(n, m); for (int i = 1; i <= n; i++) { // printf("father of %d %d\n", i, fatherOf[i]); // printf("down %d %d\n", i, down[i]); // printf("up %d %d\n", i, up[i]); printf("%lld\n", (down[i] * up[i]) % m); } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; using ll = long long; const int NNODES = 1e5 + 6; vector<int> v[NNODES]; int fatherOf[NNODES]; ll down[NNODES]; ll up[NNODES]; ll prefix[NNODES], suffix[NNODES]; ll dfsDown(int root, int m) { vector<int> l; ll temp = 1; for (int node : v[root]) { if (fatherOf[root] == node) { continue; } fatherOf[node] = root; temp = ((temp % m) * (1 + (dfsDown(node, m) % m)) % m) % m; // calculate prefix, suffix l.push_back(node); } ll t = 1; for (int i = 0; i < l.size(); i++) { // pair<int, int> a(root, l[i]); prefix[l[i]] = t; t = ((t % m) * ((1 + (down[l[i]] % m)) % m)) % m; // printf("sss %d, %d, %lld\n", root, l[i], prefix[{root, l[i]}]); } t = 1; for (int i = l.size() - 1; i >= 0; i--) { // pair<int, int> a(root, l[i]); suffix[l[i]] = t; t = ((t % m) * ((1 + (down[l[i]] % m)) % m)) % m; } down[root] = temp % m; return down[root]; } ll dfsUp(int root, int m) { if (up[root] > 1) { return up[root]; } if (fatherOf[root] == -1) { up[root] = 1; return up[root]; } ll upV = dfsUp(fatherOf[root], m); ll kk = ((prefix[root] % m) * (suffix[root] % m)) % m; up[root] = (((kk * (upV % m)) % m) + 1) % m; return up[root]; } void solve(int n, int m) { int root = 1; fatherOf[root] = -1; dfsDown(root, m); for (int i = 1; i <= n; i++) { dfsUp(i, m); } queue<int> q; q.push(root); while (q.size() > 0) { int it = q.front(); q.pop(); dfsUp(it, m); for (int node : v[it]) { if (node == fatherOf[it]) { continue; } q.push(node); } } } int main() { memset(down, -1, sizeof(down)); memset(up, -1, sizeof(up)); memset(prefix, 1, sizeof(prefix)); memset(suffix, 1, sizeof(suffix)); int n, m; cin >> n >> m; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } solve(n, m); for (int i = 1; i <= n; i++) { // printf("father of %d %d\n", i, fatherOf[i]); // printf("down %d %d\n", i, down[i]); // printf("up %d %d\n", i, up[i]); printf("%lld\n", (down[i] * up[i]) % m); } return 0; }
replace
66
67
66
67
0
p03181
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; typedef tuple<ll, ll, ll, ll, ll> t5; #define rep(a, n) for (ll a = 0; a < n; a++) #define repi(a, b, n) for (ll a = b; a < n; a++) using namespace std; static const ll INF = 1e15; template <typename T> static inline void chmax(T &ref, const T value) { if (ref < value) ref = value; } const ll mod = 1000000007; ll m = 1e9 + 7; class modint { using u64 = std::int_fast64_t; public: u64 a; modint(const u64 x = 0) noexcept : a(x % m) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= m) { a -= m; } return *this; } modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += m; } a -= rhs.a; return *this; } modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % m; return *this; } }; ostream &operator<<(ostream &stream, const modint &data) { stream << data.a; return stream; } ll n; vector<vector<int>> g; vector<map<int, modint>> children; modint dfs(int current, int prev) { // 頂点current が黒である組み合わせ modint i = 1; for (auto c : g[current]) { if (c == prev) continue; modint t = dfs(c, current) + 1; children[current][c] = t; i *= t; } return i; } vector<modint> dp1; void dfs2(int current, int parent, modint pValue) { // 親ノードの情報マージ auto &child = children[current]; if (parent != -1) { child[parent] = pValue + 1; } modint &ans = dp1[current]; ans = 1; for (auto p : child) { ans *= p.second; } // publish auto csize = child.size(); vector<int> next; for (auto p : child) { next.push_back(p.first); } auto b = child.begin(); auto rb = child.rbegin(); vector<modint> pos(csize); vector<modint> neg(csize); pos[0] = b->second; neg[csize - 1] = rb->second; for (int i = 1; i < csize; i++) { b++; rb++; pos[i] = pos[i - 1] * b->second; neg[csize - 1 - i] = neg[csize - i] * rb->second; } for (int i = 0; i < csize; i++) { if (next[i] == parent) continue; modint p = 1; if (i > 0) { p *= pos[i - 1]; } if (i < csize - 1) { p *= neg[i + 1]; } dfs2(next[i], current, p); } } int main() { cin >> n >> m; g.resize(n); dp1.resize(n); children.resize(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } dfs(0, -1); dfs2(0, -1, 0); rep(i, n) { cout << dp1[i] << endl; } return 0; }
#include <bits/stdc++.h> #define M_PI 3.14159265358979323846 // pi using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<ll> VI; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> t3; typedef tuple<ll, ll, ll, ll> t4; typedef tuple<ll, ll, ll, ll, ll> t5; #define rep(a, n) for (ll a = 0; a < n; a++) #define repi(a, b, n) for (ll a = b; a < n; a++) using namespace std; static const ll INF = 1e15; template <typename T> static inline void chmax(T &ref, const T value) { if (ref < value) ref = value; } const ll mod = 1000000007; ll m = 1e9 + 7; class modint { using u64 = std::int_fast64_t; public: u64 a; modint(const u64 x = 0) noexcept : a(x % m) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= m) { a -= m; } return *this; } modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += m; } a -= rhs.a; return *this; } modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % m; return *this; } }; ostream &operator<<(ostream &stream, const modint &data) { stream << data.a; return stream; } ll n; vector<vector<int>> g; vector<map<int, modint>> children; modint dfs(int current, int prev) { // 頂点current が黒である組み合わせ modint i = 1; for (auto c : g[current]) { if (c == prev) continue; modint t = dfs(c, current) + 1; children[current][c] = t; i *= t; } return i; } vector<modint> dp1; void dfs2(int current, int parent, modint pValue) { // 親ノードの情報マージ auto &child = children[current]; if (parent != -1) { child[parent] = pValue + 1; } modint &ans = dp1[current]; ans = 1; for (auto p : child) { ans *= p.second; } // publish auto csize = child.size(); if (csize <= 0) return; vector<int> next; for (auto p : child) { next.push_back(p.first); } auto b = child.begin(); auto rb = child.rbegin(); vector<modint> pos(csize); vector<modint> neg(csize); pos[0] = b->second; neg[csize - 1] = rb->second; for (int i = 1; i < csize; i++) { b++; rb++; pos[i] = pos[i - 1] * b->second; neg[csize - 1 - i] = neg[csize - i] * rb->second; } for (int i = 0; i < csize; i++) { if (next[i] == parent) continue; modint p = 1; if (i > 0) { p *= pos[i - 1]; } if (i < csize - 1) { p *= neg[i + 1]; } dfs2(next[i], current, p); } } int main() { cin >> n >> m; g.resize(n); dp1.resize(n); children.resize(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } dfs(0, -1); dfs2(0, -1, 0); rep(i, n) { cout << dp1[i] << endl; } return 0; }
insert
105
105
105
107
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double ll n, mod; const int nax = 1e5 + 5; ll down[nax]; vector<vector<int>> v(nax); ll up[nax]; int vis[nax] = {0}; ll ans[nax]; void dfs2(int ci) { vis[ci] = 1; ll g = up[ci]; for (auto x : v[ci]) { if (vis[x] == 1) continue; dfs2(x); g = (g * down[x]) % mod; } ans[ci] = g; } void dfs(int ci) { vis[ci] = 1; ll val = 1; for (auto x : v[ci]) { if (vis[x] == 1) continue; dfs(x); val = (val * down[x]) % mod; } val++; val %= mod; down[ci] = val; } void dfs1(int ci) { vis[ci] = 1; if (ci == 0) up[ci] = 1; vector<int> t; for (auto x : v[ci]) { if (vis[x] == 1) continue; t.push_back(x); } vector<ll> pre(t.size()), suff(t.size()); for (int i = 0; i < t.size(); i++) { pre[i] = down[t[i]]; if (i > 0) pre[i] = (pre[i] * pre[i - 1]) % mod; } for (int i = (int)t.size() - 1; i >= 0; i--) { suff[i] = down[t[i]]; if (i < (int)t.size() - 1) suff[i] = (suff[i] * suff[i + 1]) % mod; } for (int i = 0; i < t.size(); i++) { ll g = 1; if (i - 1 >= 0) g *= pre[i - 1]; if (i + 1 <= (int)t.size() - 1) g = (g * suff[i + 1]) % mod; up[t[i]] = (up[ci] * g + 1) % mod; dfs1(t[i]); } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> mod; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; v[a].push_back(b); v[b].push_back(a); } dfs(0); for (int i = 0; i < n; i++) vis[i] = 0; dfs1(0); for (int i = 0; i < n; i++) vis[i] = 0; dfs2(0); for (int i = 0; i < n; i++) cout << ans[i] << "\n"; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double ll n, mod; const int nax = 1e5 + 5; ll down[nax]; vector<vector<int>> v(nax); ll up[nax]; int vis[nax] = {0}; ll ans[nax]; void dfs2(int ci) { vis[ci] = 1; ll g = up[ci]; for (auto x : v[ci]) { if (vis[x] == 1) continue; dfs2(x); g = (g * down[x]) % mod; } ans[ci] = g; } void dfs(int ci) { vis[ci] = 1; ll val = 1; for (auto x : v[ci]) { if (vis[x] == 1) continue; dfs(x); val = (val * down[x]) % mod; } val++; val %= mod; down[ci] = val; } void dfs1(int ci) { vis[ci] = 1; if (ci == 0) up[ci] = 1; vector<int> t; for (auto x : v[ci]) { if (vis[x] == 1) continue; t.push_back(x); } vector<ll> pre(t.size()), suff(t.size()); for (int i = 0; i < t.size(); i++) { pre[i] = down[t[i]]; if (i > 0) pre[i] = (pre[i] * pre[i - 1]) % mod; } for (int i = (int)t.size() - 1; i >= 0; i--) { suff[i] = down[t[i]]; if (i < (int)t.size() - 1) suff[i] = (suff[i] * suff[i + 1]) % mod; } for (int i = 0; i < t.size(); i++) { ll g = 1; if (i - 1 >= 0) g *= pre[i - 1]; if (i + 1 <= (int)t.size() - 1) g = (g * suff[i + 1]) % mod; up[t[i]] = (up[ci] * g + 1) % mod; dfs1(t[i]); } } int main() { /* #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif */ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> mod; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; v[a].push_back(b); v[b].push_back(a); } dfs(0); for (int i = 0; i < n; i++) vis[i] = 0; dfs1(0); for (int i = 0; i < n; i++) vis[i] = 0; dfs2(0); for (int i = 0; i < n; i++) cout << ans[i] << "\n"; }
replace
67
71
67
73
-8
p03181
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; int n, m; int a, b; vector<int> v[100005]; int dp[100005]; int out[100005]; void dfs(int node, int prev) { dp[node] = 1; for (auto it : v[node]) { if (it != prev) { dfs(it, node); dp[node] = 1LL * dp[node] * (dp[it] + 1) % m; } } } void dfs2(int node, int prev) { vector<int> pref(n), suf(n); pref.resize(n); suf.resize(n); out[node] = dp[node]; int last = 1; for (int i = 0; v[node].size() > i; i++) { pref[i] = 1LL * last * (dp[v[node][i]] + 1) % m; last = pref[i]; } last = 1; for (int i = v[node].size() - 1; i >= 0; i--) { suf[i] = 1LL * last * (dp[v[node][i]] + 1) % m; last = suf[i]; } for (int i = 0; v[node].size() > i; i++) { if (v[node][i] == prev) continue; int ile = dp[v[node][i]]; int it = v[node][i]; int val = 1; if (i > 0) val = 1LL * val * pref[i - 1] % m; if (i < v[node].size() - 1) val = 1LL * val * suf[i + 1] % m; int co = dp[node]; dp[node] = val; dp[it] = (1LL * dp[it] * (val + 1)) % m; dfs2(it, node); dp[it] = ile; dp[node] = co; } } int main() { scanf("%d%d", &n, &m); for (int i = 0; n - 1 > i; i++) { scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } dfs(1, -1); dfs2(1, -1); for (int i = 1; n >= i; i++) printf("%d\n", out[i]); return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; int n, m; int a, b; vector<int> v[100005]; int dp[100005]; int out[100005]; void dfs(int node, int prev) { dp[node] = 1; for (auto it : v[node]) { if (it != prev) { dfs(it, node); dp[node] = 1LL * dp[node] * (dp[it] + 1) % m; } } } void dfs2(int node, int prev) { int f = v[node].size(); vector<int> pref(f), suf(f); pref.resize(f); suf.resize(f); out[node] = dp[node]; int last = 1; for (int i = 0; v[node].size() > i; i++) { pref[i] = 1LL * last * (dp[v[node][i]] + 1) % m; last = pref[i]; } last = 1; for (int i = v[node].size() - 1; i >= 0; i--) { suf[i] = 1LL * last * (dp[v[node][i]] + 1) % m; last = suf[i]; } for (int i = 0; v[node].size() > i; i++) { if (v[node][i] == prev) continue; int ile = dp[v[node][i]]; int it = v[node][i]; int val = 1; if (i > 0) val = 1LL * val * pref[i - 1] % m; if (i < v[node].size() - 1) val = 1LL * val * suf[i + 1] % m; int co = dp[node]; dp[node] = val; dp[it] = (1LL * dp[it] * (val + 1)) % m; dfs2(it, node); dp[it] = ile; dp[node] = co; } } int main() { scanf("%d%d", &n, &m); for (int i = 0; n - 1 > i; i++) { scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } dfs(1, -1); dfs2(1, -1); for (int i = 1; n >= i; i++) printf("%d\n", out[i]); return 0; }
replace
18
21
18
22
0
p03181
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define RFOR(i, c) \ for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a, b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define __builtin_popcount __builtin_popcountll template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "["; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const map<S, T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } const int INF = 1 << 28; const double EPS = 1e-8; int MOD; struct handler { typedef ll val_t; typedef ll opr_t; handler() {} val_t def_val() { return 1; } static val_t update(const val_t &l, const opr_t &r) { return l * r % MOD; } static val_t merge(const val_t &l, const val_t &r) { return l * r % MOD; } }; template <typename Handler> struct SegTree { typedef typename Handler::val_t val_t; typedef typename Handler::opr_t opr_t; vector<val_t> val; Handler hdl; int n; SegTree(int size) : hdl() { n = 1; while (n < size) n <<= 1; val = vector<val_t>(2 * n, hdl.def_val()); } SegTree(const vector<val_t> &in) : hdl() { n = 1; while (n < in.size()) n <<= 1; val = vector<val_t>(2 * n, hdl.def_val()); for (int i = n - 1 + in.size() - 1; i >= 0; i--) { if (n - 1 <= i) val[i] = in[i - (n - 1)]; else val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]); } } void update(int i, const opr_t &a) { i += n - 1; val[i] = hdl.update(val[i], a); while (i > 0) { i = (i - 1) / 2; val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]); } } val_t query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return hdl.def_val(); if (a <= l && r <= b) return val[k]; return hdl.merge(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } val_t query(int a, int b) { return query(a, b, 0, 0, n); } val_t operator[](size_t i) { return query(i, i + 1); } friend ostream &operator<<(ostream &os, SegTree<Handler> &t) { REP(i, t.n) os << (i ? ", " : "[") << t.query(i, i + 1); return os << "]"; } }; int T, n, m; vector<vi> g; vi ans; map<int, int> memo[100001]; ll dfs(int u, int p) { if (memo[u].count(p)) return memo[u][p]; ll ans = 1; vector<ll> r; for (int v : g[u]) { if (v != p) { ll res = dfs(v, u) + 1; ans = ans * res % MOD; r.push_back(res); } } if (p == -1) { SegTree<handler> seg(r); REP(i, g[u].size()) { memo[u][g[u][i]] = seg.query(0, i) * seg.query(i + 1, r.size()) % MOD; } } return memo[u][p] = ans; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); while (cin >> n >> MOD) { g = vector<vi>(n); ans = vi(n); REP(i, n - 1) { int u, v; cin >> u >> v; -u--; v--; g[u].push_back(v); g[v].push_back(u); } REP(i, n) { cout << dfs(i, -1) << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define RFOR(i, c) \ for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a, b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define __builtin_popcount __builtin_popcountll template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "["; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const map<S, T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } const int INF = 1 << 28; const double EPS = 1e-8; int MOD; struct handler { typedef ll val_t; typedef ll opr_t; handler() {} val_t def_val() { return 1; } static val_t update(const val_t &l, const opr_t &r) { return l * r % MOD; } static val_t merge(const val_t &l, const val_t &r) { return l * r % MOD; } }; template <typename Handler> struct SegTree { typedef typename Handler::val_t val_t; typedef typename Handler::opr_t opr_t; vector<val_t> val; Handler hdl; int n; SegTree(int size) : hdl() { n = 1; while (n < size) n <<= 1; val = vector<val_t>(2 * n, hdl.def_val()); } SegTree(const vector<val_t> &in) : hdl() { n = 1; while (n < in.size()) n <<= 1; val = vector<val_t>(2 * n, hdl.def_val()); for (int i = n - 1 + in.size() - 1; i >= 0; i--) { if (n - 1 <= i) val[i] = in[i - (n - 1)]; else val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]); } } void update(int i, const opr_t &a) { i += n - 1; val[i] = hdl.update(val[i], a); while (i > 0) { i = (i - 1) / 2; val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]); } } val_t query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return hdl.def_val(); if (a <= l && r <= b) return val[k]; return hdl.merge(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } val_t query(int a, int b) { return query(a, b, 0, 0, n); } val_t operator[](size_t i) { return query(i, i + 1); } friend ostream &operator<<(ostream &os, SegTree<Handler> &t) { REP(i, t.n) os << (i ? ", " : "[") << t.query(i, i + 1); return os << "]"; } }; int T, n, m; vector<vi> g; vi ans; map<int, int> memo[100001]; ll dfs(int u, int p) { if (memo[u].count(p)) return memo[u][p]; ll ans = 1; vector<ll> r; for (int v : g[u]) { if (v != p) { ll res = dfs(v, u) + 1; ans = ans * res % MOD; r.push_back(res); } } if (p == -1) { SegTree<handler> seg(r); REP(i, g[u].size()) { memo[u][g[u][i]] = seg.query(0, i) * seg.query(i + 1, r.size()) % MOD; } } return memo[u][p] = ans; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); while (cin >> n >> MOD) { g = vector<vi>(n); ans = vi(n); REP(i, n - 1) { int u, v; cin >> u >> v; -u--; v--; g[u].push_back(v); g[v].push_back(u); } vi d(n); REP(i, n) d[i] = i; sort(RALL(d), [&](int i, int j) { return g[i].size() < g[j].size(); }); REP(i, n) { dfs(d[i], -1); } REP(i, n) { cout << dfs(i, -1) << endl; } } return 0; }
insert
185
185
185
189
TLE
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; // #define P pair<ll,ll> #define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I) #define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I) #define TO(x, t, f) ((x) ? (t) : (f)) #define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x, v) \ (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v x is sorted #define POSU(x, v) \ (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v x is sorted #define NUM(x, v) (POSU(x, v) - POSL(x, v)) // x is sorted #define REV(x) (reverse(x.begin(), x.end())) // reverse ll gcd(ll a, ll b) { if (a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll c = gcd(a, b); return ((a / c) * (b / c) * c); } #define NEXTP(x) next_permutation(x.begin(), x.end()) const ll INF = ll(1e16) + ll(7); const ll MOD = 1000000007LL; #define out(a) cout << fixed << setprecision((a)) template <typename T> class func { public: // 0-index vector<T> datf, datb; T M, initial; int n; T f(T a, T b) { return (a * b) % M; } void final_updete(int j, T x) { for (int i = j; i < n; i++) (datf[i] *= x) %= M; for (int i = j; i >= 0; i--) (datb[i] *= x) %= M; } void init(int n_, T initial_, T M_) { M = M_; n = n_; initial = initial_; datf.resize(n, initial); datb.resize(n, initial); } void update(int i, T x) { datb[i] = x; datf[i] = x; } void calc() { for (int i = 1; i < n; i++) datf[i] = f(datf[i], datf[i - 1]); for (int i = n - 2; i >= 0; i--) datb[i] = f(datb[i], datb[i + 1]); } T get_b(int i) { if (i >= n || i < 0) return initial; return datb[i]; } T get_f(int i) { if (i >= n || i < 0) return initial; return datf[i]; } T get_except(int i) { return f(get_f(i - 1), get_b(i + 1)); } T get_all() { return datb[0]; } }; vector<int> leaf(const vector<int> &u, const vector<int> &v) { int N = u.size() + 1; vector<int> G(N, 0), res; for (int i = 0; i < N - 1; i++) { G[u[i]]++; G[v[i]]++; } for (int i = 0; i < N; i++) { if (G[i] == 1) { res.push_back(i); } } return res; } vector<pair<int, int>> leaf_to_root(const vector<int> &u, const vector<int> &v, const int root) { // return <parent,child> int yet = 1000000000; vector<int> d(u.size() + 1, yet); vector<pair<int, int>> res; queue<int> Q; Q.push(root); d[root] = 0; vector<vector<int>> G(u.size() + 1); for (int i = 0; i < u.size(); i++) { G[u[i]].push_back(v[i]); G[v[i]].push_back(u[i]); } while (Q.size()) { int now = Q.front(); Q.pop(); for (auto nex : G[now]) { if (d[nex] != yet) continue; d[nex] = d[now] + 1; Q.push(nex); res.push_back(make_pair(now, nex)); } } reverse(res.begin(), res.end()); return res; } int main() { int N, M; cin >> N >> M; vector<int> x(N - 1), y(N - 1); FOR(i, 0, N - 1) cin >> x[i] >> y[i]; FOR(i, 0, N - 1) x[i]--, y[i]--; vector<vector<int>> G(N); for (int i = 0; i < N - 1; i++) { G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } vector<map<int, int>> ma(N); for (int i = 0; i < N; i++) { for (int j = 0; j < G[i].size(); j++) { ma[i][G[i][j]] = j; } } vector<ll> val(N, 1); vector<func<ll>> dp(N); for (int i = 0; i < N; i++) { dp[i].init(G[i].size(), 1, M); } auto route = leaf_to_root(x, y, 0); for (auto a : route) { int p = a.first; int c = a.second; (val[p] *= val[c] + 1LL) %= M; dp[p].update(ma[p][c], (val[c] + 1LL) % M); } for (int i = 0; i < N; i++) dp[i].calc(); reverse(route.begin(), route.end()); for (auto a : route) { int c = a.second; int p = a.first; ll v = dp[p].get_except(ma[p][c]); dp[c].final_updete(ma[c][p], (v + 1LL) % M); } for (int i = 0; i < N; i++) { cout << dp[i].get_all() << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; // #define P pair<ll,ll> #define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I) #define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I) #define TO(x, t, f) ((x) ? (t) : (f)) #define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x, v) \ (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v x is sorted #define POSU(x, v) \ (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v x is sorted #define NUM(x, v) (POSU(x, v) - POSL(x, v)) // x is sorted #define REV(x) (reverse(x.begin(), x.end())) // reverse ll gcd(ll a, ll b) { if (a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll c = gcd(a, b); return ((a / c) * (b / c) * c); } #define NEXTP(x) next_permutation(x.begin(), x.end()) const ll INF = ll(1e16) + ll(7); const ll MOD = 1000000007LL; #define out(a) cout << fixed << setprecision((a)) template <typename T> class func { public: // 0-index vector<T> datf, datb; T M, initial; int n; T f(T a, T b) { return (a * b) % M; } void final_updete(int j, T x) { for (int i = j; i < n; i++) (datf[i] *= x) %= M; for (int i = j; i >= 0; i--) (datb[i] *= x) %= M; } void init(int n_, T initial_, T M_) { M = M_; n = n_; initial = initial_; datf.resize(n, initial); datb.resize(n, initial); } void update(int i, T x) { datb[i] = x; datf[i] = x; } void calc() { for (int i = 1; i < n; i++) datf[i] = f(datf[i], datf[i - 1]); for (int i = n - 2; i >= 0; i--) datb[i] = f(datb[i], datb[i + 1]); } T get_b(int i) { if (i >= n || i < 0) return initial; return datb[i]; } T get_f(int i) { if (i >= n || i < 0) return initial; return datf[i]; } T get_except(int i) { return f(get_f(i - 1), get_b(i + 1)); } T get_all() { return datb[0]; } }; vector<int> leaf(const vector<int> &u, const vector<int> &v) { int N = u.size() + 1; vector<int> G(N, 0), res; for (int i = 0; i < N - 1; i++) { G[u[i]]++; G[v[i]]++; } for (int i = 0; i < N; i++) { if (G[i] == 1) { res.push_back(i); } } return res; } vector<pair<int, int>> leaf_to_root(const vector<int> &u, const vector<int> &v, const int root) { // return <parent,child> int yet = 1000000000; vector<int> d(u.size() + 1, yet); vector<pair<int, int>> res; queue<int> Q; Q.push(root); d[root] = 0; vector<vector<int>> G(u.size() + 1); for (int i = 0; i < u.size(); i++) { G[u[i]].push_back(v[i]); G[v[i]].push_back(u[i]); } while (Q.size()) { int now = Q.front(); Q.pop(); for (auto nex : G[now]) { if (d[nex] != yet) continue; d[nex] = d[now] + 1; Q.push(nex); res.push_back(make_pair(now, nex)); } } reverse(res.begin(), res.end()); return res; } int main() { int N, M; cin >> N >> M; vector<int> x(N - 1), y(N - 1); FOR(i, 0, N - 1) cin >> x[i] >> y[i]; FOR(i, 0, N - 1) x[i]--, y[i]--; if (N == 1) { cout << 1 << endl; return 0; } vector<vector<int>> G(N); for (int i = 0; i < N - 1; i++) { G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } vector<map<int, int>> ma(N); for (int i = 0; i < N; i++) { for (int j = 0; j < G[i].size(); j++) { ma[i][G[i][j]] = j; } } vector<ll> val(N, 1); vector<func<ll>> dp(N); for (int i = 0; i < N; i++) { dp[i].init(G[i].size(), 1, M); } auto route = leaf_to_root(x, y, 0); for (auto a : route) { int p = a.first; int c = a.second; (val[p] *= val[c] + 1LL) %= M; dp[p].update(ma[p][c], (val[c] + 1LL) % M); } for (int i = 0; i < N; i++) dp[i].calc(); reverse(route.begin(), route.end()); for (auto a : route) { int c = a.second; int p = a.first; ll v = dp[p].get_except(ma[p][c]); dp[c].final_updete(ma[c][p], (v + 1LL) % M); } for (int i = 0; i < N; i++) { cout << dp[i].get_all() << endl; } }
insert
123
123
123
128
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 1; i <= (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define rng(a) a.begin(), a.end() #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define limit(x, l, r) max(l, min(x, r)) #define lims(x, l, r) (x = max(l, min(x, r))) #define isin(x, l, r) ((l) <= (x) && (x) < (r)) #define pb push_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)), x.end()) #define snuke srand((unsigned)clock() + (unsigned)time(NULL)); #define show(x) cout << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define bn(x) ((1 << x) - 1) #define dup(x, y) (((x) + (y)-1) / (y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; inline int in() { int x; scanf("%d", &x); return x; } template <typename T> inline istream &operator>>(istream &i, v(T) & v) { rep(j, sz(v)) i >> v[j]; return i; } template <typename T> string join(const v(T) & v) { stringstream s; rep(i, sz(v)) s << ' ' << v[i]; return s.str().substr(1); } template <typename T> inline ostream &operator<<(ostream &o, const v(T) & v) { if (sz(v)) o << join(v); return o; } template <typename T1, typename T2> inline istream &operator>>(istream &i, pair<T1, T2> &v) { return i >> v.fi >> v.se; } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> &v) { return o << v.fi << "," << v.se; } template <typename T> inline ll suma(const v(T) & a) { ll res(0); for (auto &&x : a) res += x; return res; } const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame \ { \ puts("-1"); \ return 0; \ } #define yn \ { puts("YES"); } \ else { \ puts("NO"); \ } const int MX = 200005; // Mod int int mod; struct mint { ll x; mint() : x(0) {} mint(ll x) : x((x % mod + mod) % mod) {} // mint(ll x):x(x){} mint &fix() { x = (x % mod + mod) % mod; return *this; } mint operator-() const { return mint(0) - *this; } mint operator~() const { return mint(1) / *this; } mint &operator+=(const mint &a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint &a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= mod; return *this; } mint &operator/=(const mint &a) { (x *= a.ex(mod - 2).x) %= mod; return *this; } mint operator+(const mint &a) const { return mint(*this) += a; } mint operator-(const mint &a) const { return mint(*this) -= a; } mint operator*(const mint &a) const { return mint(*this) *= a; } mint operator/(const mint &a) const { return mint(*this) /= a; } mint ex(ll t) const { if (!t) return 1; mint res = ex(t / 2); res *= res; return (t & 1) ? res * x : res; } bool operator<(const mint &a) const { return x < a.x; } bool operator==(const mint &a) const { return x == a.x; } }; mint ex(mint x, ll t) { return x.ex(t); } istream &operator>>(istream &i, mint &a) { i >> a.x; return i; } ostream &operator<<(ostream &o, const mint &a) { o << a.x; return o; } typedef vector<mint> vm; struct comb { vm f, g; comb() {} comb(int mx) : f(mx + 1), g(mx + 1) { f[0] = 1; rrep(i, mx) f[i] = f[i - 1] * i; g[mx] = f[mx].ex(mod - 2); for (int i = mx; i > 0; i--) g[i - 1] = g[i] * i; } mint c(int a, int b) { if (a < b) return 0; return f[a] * g[b] * g[a - b]; } }; // vvi to; vm dp[MX]; mint ans[MX]; mint dfs(int v, int p = -1) { int m = sz(to[v]); dp[v] = vm(m); mint res = 1; rep(i, m) { int u = to[v][i]; if (u == p) continue; dp[v][i] = dfs(u, v) + 1; res *= dp[v][i]; } // cout<<v<<": "<<dp[v]<<endl; return res; } void efs(int v, mint x, int p = -1) { int m = sz(to[v]); rep(i, m) { int u = to[v][i]; if (u == p) dp[v][i] = x + 1; } vm l = dp[v], r = dp[v]; rep(i, m - 1) l[i + 1] *= l[i]; drep(i, m - 1) r[i] *= r[i + 1]; ans[v] = l.back(); rep(i, m) { int u = to[v][i]; if (u == p) continue; x = 1; if (i) x *= l[i - 1]; if (i + 1 < m) x *= r[i + 1]; efs(u, x, v); } } int main() { int n; scanf("%d%d", &n, &mod); to = vvi(n); rep(i, n - 1) { int a, b; scanf("%d%d", &a, &b); --a; --b; to[a].pb(b); to[b].pb(a); } mint x = dfs(0); efs(0, x); rep(i, n) { printf("%lld\n", ans[i].x); } return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 1; i <= (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define rng(a) a.begin(), a.end() #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define limit(x, l, r) max(l, min(x, r)) #define lims(x, l, r) (x = max(l, min(x, r))) #define isin(x, l, r) ((l) <= (x) && (x) < (r)) #define pb push_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)), x.end()) #define snuke srand((unsigned)clock() + (unsigned)time(NULL)); #define show(x) cout << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define bn(x) ((1 << x) - 1) #define dup(x, y) (((x) + (y)-1) / (y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; inline int in() { int x; scanf("%d", &x); return x; } template <typename T> inline istream &operator>>(istream &i, v(T) & v) { rep(j, sz(v)) i >> v[j]; return i; } template <typename T> string join(const v(T) & v) { stringstream s; rep(i, sz(v)) s << ' ' << v[i]; return s.str().substr(1); } template <typename T> inline ostream &operator<<(ostream &o, const v(T) & v) { if (sz(v)) o << join(v); return o; } template <typename T1, typename T2> inline istream &operator>>(istream &i, pair<T1, T2> &v) { return i >> v.fi >> v.se; } template <typename T1, typename T2> inline ostream &operator<<(ostream &o, const pair<T1, T2> &v) { return o << v.fi << "," << v.se; } template <typename T> inline ll suma(const v(T) & a) { ll res(0); for (auto &&x : a) res += x; return res; } const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame \ { \ puts("-1"); \ return 0; \ } #define yn \ { puts("YES"); } \ else { \ puts("NO"); \ } const int MX = 200005; // Mod int int mod; struct mint { ll x; mint() : x(0) {} mint(ll x) : x((x % mod + mod) % mod) {} // mint(ll x):x(x){} mint &fix() { x = (x % mod + mod) % mod; return *this; } mint operator-() const { return mint(0) - *this; } mint operator~() const { return mint(1) / *this; } mint &operator+=(const mint &a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint &a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= mod; return *this; } mint &operator/=(const mint &a) { (x *= a.ex(mod - 2).x) %= mod; return *this; } mint operator+(const mint &a) const { return mint(*this) += a; } mint operator-(const mint &a) const { return mint(*this) -= a; } mint operator*(const mint &a) const { return mint(*this) *= a; } mint operator/(const mint &a) const { return mint(*this) /= a; } mint ex(ll t) const { if (!t) return 1; mint res = ex(t / 2); res *= res; return (t & 1) ? res * x : res; } bool operator<(const mint &a) const { return x < a.x; } bool operator==(const mint &a) const { return x == a.x; } }; mint ex(mint x, ll t) { return x.ex(t); } istream &operator>>(istream &i, mint &a) { i >> a.x; return i; } ostream &operator<<(ostream &o, const mint &a) { o << a.x; return o; } typedef vector<mint> vm; struct comb { vm f, g; comb() {} comb(int mx) : f(mx + 1), g(mx + 1) { f[0] = 1; rrep(i, mx) f[i] = f[i - 1] * i; g[mx] = f[mx].ex(mod - 2); for (int i = mx; i > 0; i--) g[i - 1] = g[i] * i; } mint c(int a, int b) { if (a < b) return 0; return f[a] * g[b] * g[a - b]; } }; // vvi to; vm dp[MX]; mint ans[MX]; mint dfs(int v, int p = -1) { int m = sz(to[v]); dp[v] = vm(m); mint res = 1; rep(i, m) { int u = to[v][i]; if (u == p) continue; dp[v][i] = dfs(u, v) + 1; res *= dp[v][i]; } // cout<<v<<": "<<dp[v]<<endl; return res; } void efs(int v, mint x, int p = -1) { int m = sz(to[v]); rep(i, m) { int u = to[v][i]; if (u == p) dp[v][i] = x + 1; } vm l = dp[v], r = dp[v]; rep(i, m - 1) l[i + 1] *= l[i]; drep(i, m - 1) r[i] *= r[i + 1]; ans[v] = l.back(); rep(i, m) { int u = to[v][i]; if (u == p) continue; x = 1; if (i) x *= l[i - 1]; if (i + 1 < m) x *= r[i + 1]; efs(u, x, v); } } int main() { int n; scanf("%d%d", &n, &mod); if (n == 1) { cout << 1 << endl; return 0; } to = vvi(n); rep(i, n - 1) { int a, b; scanf("%d%d", &a, &b); --a; --b; to[a].pb(b); to[b].pb(a); } mint x = dfs(0); efs(0, x); rep(i, n) { printf("%lld\n", ans[i].x); } return 0; }
insert
200
200
200
204
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int #define f first #define s second #define pb push_back ll mod; long long binpow(long long a, long long b, long long m = mod) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll modinv(ll n) { return binpow(n, mod - 2, mod); } vector<ll> ar[100001]; ll dp[100001] = {0}; ll dfs(ll u, ll p) { dp[u] = 1ll; for (auto it : ar[u]) { if (it != p) { dfs(it, u); dp[u] *= (dp[it] + 1ll); dp[u] %= mod; } } } ll ans[100001]; ll dfs2(ll u, ll p) { vector<ll> g; ll f = 1ll; vector<ll> k; for (auto it = ar[u].begin(); it != ar[u].end(); ++it) { if (*it != p) { k.push_back(*it); f *= (dp[(*it)] + 1ll); f %= mod; g.push_back(f); } } deque<ll> r; f = 1ll; for (auto it = ar[u].rbegin(); it != ar[u].rend(); ++it) { if (*it != p) { f *= (dp[(*it)] + 1ll); f %= mod; r.push_front(f); } } for (int x = 0; x < k.size(); x++) { ll t = 1; if (x > 0) t *= g[x - 1]; t %= mod; if (x < k.size() - 1ll) t *= r[x + 1]; t %= mod; ans[k[x]] *= (ans[u] * t + 1ll) % mod; ans[k[x]] %= mod; dfs2(k[x], u); } ans[u] *= dp[u]; ans[u] %= mod; } void solve() { ll n; cin >> n >> mod; ll u, v; for (int x = 1; x <= n; x++) ans[x] = 1; for (int x = 0; x < n - 1; x++) { cin >> u >> v; ar[u].pb(v); ar[v].pb(u); } dfs(1, -1); dfs2(1, -1); for (int x = 1; x <= n; x++) { cout << ans[x] << "\n"; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll i = 1; // cin>>i; ll k = 0; while (k < i) { solve(); ++k; } }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define f first #define s second #define pb push_back ll mod; long long binpow(long long a, long long b, long long m = mod) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll modinv(ll n) { return binpow(n, mod - 2, mod); } vector<ll> ar[100001]; ll dp[100001] = {0}; ll dfs(ll u, ll p) { dp[u] = 1ll; for (auto it : ar[u]) { if (it != p) { dfs(it, u); dp[u] *= (dp[it] + 1ll); dp[u] %= mod; } } } ll ans[100001]; ll dfs2(ll u, ll p) { vector<ll> g; ll f = 1ll; vector<ll> k; for (auto it = ar[u].begin(); it != ar[u].end(); ++it) { if (*it != p) { k.push_back(*it); f *= (dp[(*it)] + 1ll); f %= mod; g.push_back(f); } } deque<ll> r; f = 1ll; for (auto it = ar[u].rbegin(); it != ar[u].rend(); ++it) { if (*it != p) { f *= (dp[(*it)] + 1ll); f %= mod; r.push_front(f); } } for (int x = 0; x < k.size(); x++) { ll t = 1; if (x > 0) t *= g[x - 1]; t %= mod; if (x < k.size() - 1ll) t *= r[x + 1]; t %= mod; ans[k[x]] *= (ans[u] * t + 1ll) % mod; ans[k[x]] %= mod; dfs2(k[x], u); } ans[u] *= dp[u]; ans[u] %= mod; } void solve() { ll n; cin >> n >> mod; ll u, v; for (int x = 1; x <= n; x++) ans[x] = 1; for (int x = 0; x < n - 1; x++) { cin >> u >> v; ar[u].pb(v); ar[v].pb(u); } dfs(1, -1); dfs2(1, -1); for (int x = 1; x <= n; x++) { cout << ans[x] << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll i = 1; // cin>>i; ll k = 0; while (k < i) { solve(); ++k; } }
delete
86
90
86
86
-11
p03181
C++
Runtime Error
/********************* * Author: xuziyuan * *********************/ #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define repn(i, n) for (int i = 1; i <= n; i++) #define LL long long #define pii pair<int, int> #define fi first #define se second #define pb push_back #define mpr make_pair using namespace std; LL n, MOD, x, y, dp[100010], ans[100010], pro[200]; vector<LL> g[100010]; LL dfs(LL pos, LL no) { dp[pos] = 1; rep(i, g[pos].size()) if (g[pos][i] != no) dp[pos] = dp[pos] * (dfs(g[pos][i], pos) + 1) % MOD; // cout<<pos<<' '<<dp[pos]<<'p'<<endl; return dp[pos]; } void dfs2(LL pos, LL no, LL val) { // cout<<pos<<' '<<val<<endl; ans[pos] = dp[pos] * (val + 1) % MOD; LL t = floor(sqrt((LL)g[pos].size())); // cout<<t<<endl; LL pro[130]; rep(i, 125) pro[i] = 1; rep(i, g[pos].size()) if (g[pos][i] != no) pro[i / t] = pro[i / t] * (dp[g[pos][i]] + 1) % MOD; LL res; rep(i, g[pos].size()) { if (g[pos][i] == no) continue; res = val + 1; rep(j, (g[pos].size() - 1) / t + 1) if (j != i / t) res = res * pro[j] % MOD; for (int j = i / t * t; j < min((LL)g[pos].size(), (i / t + 1) * t); j++) if (g[pos][j] != no && j != i) res = res * (dp[g[pos][j]] + 1) % MOD; dfs2(g[pos][i], pos, res); } } int main() { cin >> n >> MOD; rep(i, n - 1) { scanf("%lld%lld", &x, &y); g[x].pb(y); g[y].pb(x); } dfs(1, 0); dfs2(1, 0, 0); repn(i, n) printf("%lld\n", ans[i]); return 0; }
/********************* * Author: xuziyuan * *********************/ #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define repn(i, n) for (int i = 1; i <= n; i++) #define LL long long #define pii pair<int, int> #define fi first #define se second #define pb push_back #define mpr make_pair using namespace std; LL n, MOD, x, y, dp[100010], ans[100010], pro[200]; vector<LL> g[100010]; LL dfs(LL pos, LL no) { dp[pos] = 1; rep(i, g[pos].size()) if (g[pos][i] != no) dp[pos] = dp[pos] * (dfs(g[pos][i], pos) + 1) % MOD; // cout<<pos<<' '<<dp[pos]<<'p'<<endl; return dp[pos]; } void dfs2(LL pos, LL no, LL val) { // cout<<pos<<' '<<val<<endl; ans[pos] = dp[pos] * (val + 1) % MOD; LL t = floor(sqrt((LL)g[pos].size())); // cout<<t<<endl; LL pro[400]; rep(i, 395) pro[i] = 1; rep(i, g[pos].size()) if (g[pos][i] != no) pro[i / t] = pro[i / t] * (dp[g[pos][i]] + 1) % MOD; LL res; rep(i, g[pos].size()) { if (g[pos][i] == no) continue; res = val + 1; rep(j, (g[pos].size() - 1) / t + 1) if (j != i / t) res = res * pro[j] % MOD; for (int j = i / t * t; j < min((LL)g[pos].size(), (i / t + 1) * t); j++) if (g[pos][j] != no && j != i) res = res * (dp[g[pos][j]] + 1) % MOD; dfs2(g[pos][i], pos, res); } } int main() { cin >> n >> MOD; rep(i, n - 1) { scanf("%lld%lld", &x, &y); g[x].pb(y); g[y].pb(x); } dfs(1, 0); dfs2(1, 0, 0); repn(i, n) printf("%lld\n", ans[i]); return 0; }
replace
33
35
33
35
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #pragma region Macros #define ll long long #define ld long double #define FOR(i, l, r) for (int i = (l); i < (r); ++i) #define REP(i, n) FOR(i, 0, n) #define REPS(i, n) FOR(i, 1, n + 1) #define RFOR(i, l, r) for (int i = (l); i >= (r); --i) #define RREP(i, n) RFOR(i, n - 1, 0) #define RREPS(i, n) RFOR(i, n, 1) #define pb push_back #define eb emplace_back #define SZ(x) ((ll)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template <class T = ll> using V = vector<T>; template <class T = ll> using VV = V<V<T>>; using P = pair<ll, ll>; #define VEC(type, name, size) \ V<type> name(size); \ IN(name) #define VVEC(type, name, h, w) \ VV<type> name(h, V<type>(w)); \ IN(name) #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DOUBLE(...) \ DOUBLE __VA_ARGS__; \ IN(__VA_ARGS__) #define LD(...) \ LD __VA_ARGS__; \ IN(__VA_ARGS__) template <class T> void scan(T a) { cin >> a; } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(long double &a) { cin >> a; } void scan(char a[]) { scanf("%s", a); } void scan(string &a) { cin >> a; } template <class T> void scan(V<T> &); template <class T, class L> void scan(pair<T, L> &); template <class T> void scan(V<T> &a) { for (auto &i : a) scan(i); } template <class T, class L> void scan(pair<T, L> &p) { scan(p.first); scan(p.second); } template <class T> void scan(T &a) { cin >> a; } void IN() {} template <class Head, class... Tail> void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template <class T> inline void print(T x) { cout << x << '\n'; } struct inputoutputfaster { inputoutputfaster() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } inputoutputfaster_; template <class T> V<T> press(V<T> &x) { V<T> res = x; sort(all(res)); res.erase(unique(all(res)), res.end()); REP(i, SZ(x)) { x[i] = lower_bound(all(res), x[i]) - res.begin(); } return res; } 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; } inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; } inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; } inline void err(bool b = true) { if (b) { cout << -1 << '\n'; exit(0); } } template <class T> inline void fin(bool b = true, T e = 0) { if (b) { cout << e << '\n'; exit(0); } } template <class T> T divup(T x, T y) { return (x + (y - 1)) / y; } template <typename T> T pow(T a, long long n, T e = 1) { T ret = e; while (n) { if (n & 1) ret *= a; a *= a; n >>= 1; } return ret; } const ll INF = 1e18; #pragma endregion // Graph Template struct Edge { ll to, cost; Edge(ll to, ll cost) : to(to), cost(cost) {} bool operator<(const Edge &a) const { return cost < a.cost; } }; using Graph = VV<>; using WGraph = VV<Edge>; void Read_Graph(Graph &g, int m, bool directed = false) { REP(i, m) { LL(u, v); u--; v--; g[u].pb(v); if (!directed) g[v].pb(u); } } void Read_Tree(Graph &g, bool directed = false) { Read_Graph(g, SZ(g) - 1, directed); } void Read_Graph(WGraph &g, int m, bool directed = false) { REP(i, m) { LL(u, v, c); u--; v--; g[u].pb({v, c}); if (!directed) g[v].pb({u, c}); } } void Read_Tree(WGraph &g, bool directed = false) { Read_Graph(g, SZ(g) - 1, directed); } template <typename T> struct ReRooting { private: using F = function<T(T, int)>; using F2 = function<T(T, T)>; int n; Graph G; VV<T> dp; F f, g; F2 merge; T ide; T dfs1(int p, int k) { T res = ide; int cnt = -1; REP(i, SZ(G[k])) { if (G[k][i] == p) continue; dp[k][i] = dfs1(k, G[k][i]); res = merge(res, f(dp[k][i], G[k][i])); } return g(res, k); } void dfs2(int p, int k, T add) { REP(i, SZ(G[k])) { if (G[k][i] == p) { dp[k][i] = add; break; } } V<T> pL(SZ(G[k]) + 1), pR(SZ(G[k]) + 1); pL[0] = ide; REP(i, SZ(G[k])) pL[i + 1] = merge(pL[i], f(dp[k][i], G[k][i])); pR[SZ(G[k])] = 1; RREP(i, SZ(G[k])) pR[i] = merge(pR[i + 1], f(dp[k][i], G[k][i])); REP(i, SZ(G[k])) { if (G[k][i] == p) continue; T val = merge(pL[i], pR[i + 1]); dfs2(k, G[k][i], g(val, k)); } } void build(int root) { REP(i, n) dp[i].resize(SZ(G[i])); dfs1(-1, root); dfs2(-1, root, ide); } public: ReRooting( Graph G_, F f_, F2 merge_, T ide_, F g_ = [](T a, int b) { return a; }) : G(G_), f(f_), merge(merge_), ide(ide_), g(g_) { n = SZ(G); dp.resize(n); } V<T> solve(int root = 0) { V<T> vec(n); REP(k, n) { T res = ide; REP(i, SZ(G[k])) res = merge(res, f(dp[k][i], G[k][i])); vec[k] = g(res, k); } return vec; } }; int main() { INT(n, m); Graph G(n); Read_Tree(G); auto f = [&](ll a, int v) { return (a + 1) % m; }; auto merge = [&](ll a, ll b) { return a * b % m; }; ReRooting<ll> Re(G, f, merge, 1); auto res = Re.solve(); for (auto v : res) print(v); }
#include <bits/stdc++.h> using namespace std; #pragma region Macros #define ll long long #define ld long double #define FOR(i, l, r) for (int i = (l); i < (r); ++i) #define REP(i, n) FOR(i, 0, n) #define REPS(i, n) FOR(i, 1, n + 1) #define RFOR(i, l, r) for (int i = (l); i >= (r); --i) #define RREP(i, n) RFOR(i, n - 1, 0) #define RREPS(i, n) RFOR(i, n, 1) #define pb push_back #define eb emplace_back #define SZ(x) ((ll)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template <class T = ll> using V = vector<T>; template <class T = ll> using VV = V<V<T>>; using P = pair<ll, ll>; #define VEC(type, name, size) \ V<type> name(size); \ IN(name) #define VVEC(type, name, h, w) \ VV<type> name(h, V<type>(w)); \ IN(name) #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DOUBLE(...) \ DOUBLE __VA_ARGS__; \ IN(__VA_ARGS__) #define LD(...) \ LD __VA_ARGS__; \ IN(__VA_ARGS__) template <class T> void scan(T a) { cin >> a; } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(long double &a) { cin >> a; } void scan(char a[]) { scanf("%s", a); } void scan(string &a) { cin >> a; } template <class T> void scan(V<T> &); template <class T, class L> void scan(pair<T, L> &); template <class T> void scan(V<T> &a) { for (auto &i : a) scan(i); } template <class T, class L> void scan(pair<T, L> &p) { scan(p.first); scan(p.second); } template <class T> void scan(T &a) { cin >> a; } void IN() {} template <class Head, class... Tail> void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template <class T> inline void print(T x) { cout << x << '\n'; } struct inputoutputfaster { inputoutputfaster() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } inputoutputfaster_; template <class T> V<T> press(V<T> &x) { V<T> res = x; sort(all(res)); res.erase(unique(all(res)), res.end()); REP(i, SZ(x)) { x[i] = lower_bound(all(res), x[i]) - res.begin(); } return res; } 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; } inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; } inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; } inline void err(bool b = true) { if (b) { cout << -1 << '\n'; exit(0); } } template <class T> inline void fin(bool b = true, T e = 0) { if (b) { cout << e << '\n'; exit(0); } } template <class T> T divup(T x, T y) { return (x + (y - 1)) / y; } template <typename T> T pow(T a, long long n, T e = 1) { T ret = e; while (n) { if (n & 1) ret *= a; a *= a; n >>= 1; } return ret; } const ll INF = 1e18; #pragma endregion // Graph Template struct Edge { ll to, cost; Edge(ll to, ll cost) : to(to), cost(cost) {} bool operator<(const Edge &a) const { return cost < a.cost; } }; using Graph = VV<>; using WGraph = VV<Edge>; void Read_Graph(Graph &g, int m, bool directed = false) { REP(i, m) { LL(u, v); u--; v--; g[u].pb(v); if (!directed) g[v].pb(u); } } void Read_Tree(Graph &g, bool directed = false) { Read_Graph(g, SZ(g) - 1, directed); } void Read_Graph(WGraph &g, int m, bool directed = false) { REP(i, m) { LL(u, v, c); u--; v--; g[u].pb({v, c}); if (!directed) g[v].pb({u, c}); } } void Read_Tree(WGraph &g, bool directed = false) { Read_Graph(g, SZ(g) - 1, directed); } template <typename T> struct ReRooting { private: using F = function<T(T, int)>; using F2 = function<T(T, T)>; int n; Graph G; VV<T> dp; F f, g; F2 merge; T ide; T dfs1(int p, int k) { T res = ide; int cnt = -1; REP(i, SZ(G[k])) { if (G[k][i] == p) continue; dp[k][i] = dfs1(k, G[k][i]); res = merge(res, f(dp[k][i], G[k][i])); } return g(res, k); } void dfs2(int p, int k, T add) { REP(i, SZ(G[k])) { if (G[k][i] == p) { dp[k][i] = add; break; } } V<T> pL(SZ(G[k]) + 1), pR(SZ(G[k]) + 1); pL[0] = ide; REP(i, SZ(G[k])) pL[i + 1] = merge(pL[i], f(dp[k][i], G[k][i])); pR[SZ(G[k])] = 1; RREP(i, SZ(G[k])) pR[i] = merge(pR[i + 1], f(dp[k][i], G[k][i])); REP(i, SZ(G[k])) { if (G[k][i] == p) continue; T val = merge(pL[i], pR[i + 1]); dfs2(k, G[k][i], g(val, k)); } } void build(int root) { REP(i, n) dp[i].resize(SZ(G[i])); dfs1(-1, root); dfs2(-1, root, ide); } public: ReRooting( Graph G_, F f_, F2 merge_, T ide_, F g_ = [](T a, int b) { return a; }) : G(G_), f(f_), merge(merge_), ide(ide_), g(g_) { n = SZ(G); dp.resize(n); } V<T> solve(int root = 0) { build(root); V<T> vec(n); REP(k, n) { T res = ide; REP(i, SZ(G[k])) res = merge(res, f(dp[k][i], G[k][i])); vec[k] = g(res, k); } return vec; } }; int main() { INT(n, m); Graph G(n); Read_Tree(G); auto f = [&](ll a, int v) { return (a + 1) % m; }; auto merge = [&](ll a, ll b) { return a * b % m; }; ReRooting<ll> Re(G, f, merge, 1); auto res = Re.solve(); for (auto v : res) print(v); }
insert
218
218
218
219
-11
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, m; int cnt = 0, head[100005], to[100005], nxt[100005]; ll f[100005], ans[100005]; ll p[100005], a[100005], b[100005], dp[100005]; void add(int x, int y) { to[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt; } void dfs(int x, int par = -1) { int cnter = 0; f[x]++; for (int i = head[x]; i; i = nxt[i]) { if (to[i] != par) { dfs(to[i], x); f[x] = f[x] * f[to[i]] % m; } } for (int i = head[x]; i; i = nxt[i]) { if (to[i] != par) { p[++cnter] = to[i]; } } f[x]++; f[x] %= m; a[0] = b[cnter + 1] = 1; for (int i = 1; i <= cnter; ++i) { a[i] = a[i - 1] * f[p[i]] % m; } for (int i = cnter; i > 0; --i) { b[i] = b[i + 1] * f[p[i]] % m; } for (int i = 1; i <= cnter; ++i) { dp[p[i]] = a[i - 1] * b[i + 1] % m; } } void DFS(int x, int par = -1) { if (x == 1) { ans[x] = 1; } else { ans[x] = (dp[x] * ans[par] + 1) % m; } for (int i = head[x]; i; i = nxt[i]) { if (to[i] != par) { DFS(to[i], x); } } } int main() { cin >> n >> m; for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; add(a, b), add(b, a); } dfs(1); DFS(1); for (int i = 1; i <= n; ++i) { cout << ans[i] * (m + f[i] - 1) % m << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, m; int cnt = 0, head[200005], to[200005], nxt[200005]; ll f[200005], ans[200005]; ll p[200005], a[200005], b[200005], dp[200005]; void add(int x, int y) { to[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt; } void dfs(int x, int par = -1) { int cnter = 0; f[x]++; for (int i = head[x]; i; i = nxt[i]) { if (to[i] != par) { dfs(to[i], x); f[x] = f[x] * f[to[i]] % m; } } for (int i = head[x]; i; i = nxt[i]) { if (to[i] != par) { p[++cnter] = to[i]; } } f[x]++; f[x] %= m; a[0] = b[cnter + 1] = 1; for (int i = 1; i <= cnter; ++i) { a[i] = a[i - 1] * f[p[i]] % m; } for (int i = cnter; i > 0; --i) { b[i] = b[i + 1] * f[p[i]] % m; } for (int i = 1; i <= cnter; ++i) { dp[p[i]] = a[i - 1] * b[i + 1] % m; } } void DFS(int x, int par = -1) { if (x == 1) { ans[x] = 1; } else { ans[x] = (dp[x] * ans[par] + 1) % m; } for (int i = head[x]; i; i = nxt[i]) { if (to[i] != par) { DFS(to[i], x); } } } int main() { cin >> n >> m; for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; add(a, b), add(b, a); } dfs(1); DFS(1); for (int i = 1; i <= n; ++i) { cout << ans[i] * (m + f[i] - 1) % m << '\n'; } return 0; }
replace
4
7
4
7
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep2(i, a, b) for (ll i = (a); i < (b); ++i) #define debug(x) cout << #x << '=' << x << endl #define all(v) (v).begin(), (v).end() const ll MOD = 1e9 + 7; // const ll MOD=998244353; const ll INF = 1e9; const ll IINF = 1e18; const double EPS = 1e-8; const double pi = acos(-1); 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; } const int MAX_N = 1e5 + 10; int N; ll M; vector<vector<int>> G(MAX_N); vector<ll> dat(MAX_N, 1), ans(MAX_N, 1); void dfs1(int v, int p) { for (int u : G[v]) { if (u == p) continue; dfs1(u, v); dat[v] = dat[v] * ((dat[u] + 1) % M) % M; } } void dfs2(int v, int p, ll d_par) { int n = G[v].size(), now = 1, np; map<int, int> m1, m2; vector<ll> cs1(n), cs2(n, 1); ans[v] = cs1[0] = (d_par + 1) % M; rep(i, n) { if (G[v][i] == p) { np = i; continue; } m1[i] = now - 1; cs1[now] = cs1[now - 1] * ((dat[G[v][i]] + 1) % M) % M, ++now; ans[v] = ans[v] * ((dat[G[v][i]] + 1) % M) % M; } now = 1; rep(i, n) { if (G[v][n - 1 - i] == p) continue; m2[n - 1 - i] = now - 1; cs2[now] = cs2[now - 1] * ((dat[G[v][n - 1 - i]] + 1) % M) % M, ++now; } rep(i, n) { if (G[v][i] == p) continue; dfs2(G[v][i], v, cs1[m1[i]] * cs2[m2[i]] % M); } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M; rep(i, N - 1) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); G[b].push_back(a); } dfs1(0, -1); if (N > 1) dfs2(0, -1, 0); rep(i, N) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep2(i, a, b) for (ll i = (a); i < (b); ++i) #define debug(x) cout << #x << '=' << x << endl #define all(v) (v).begin(), (v).end() const ll MOD = 1e9 + 7; // const ll MOD=998244353; const ll INF = 1e9; const ll IINF = 1e18; const double EPS = 1e-8; const double pi = acos(-1); 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; } const int MAX_N = 1e5 + 10; int N; ll M; vector<vector<int>> G(MAX_N); vector<ll> dat(MAX_N, 1), ans(MAX_N, 1); void dfs1(int v, int p) { for (int u : G[v]) { if (u == p) continue; dfs1(u, v); dat[v] = dat[v] * ((dat[u] + 1) % M) % M; } } void dfs2(int v, int p, ll d_par) { int n = G[v].size(), now = 1, np; map<int, int> m1, m2; vector<ll> cs1(n + 1), cs2(n + 1, 1); ans[v] = cs1[0] = (d_par + 1) % M; rep(i, n) { if (G[v][i] == p) { np = i; continue; } m1[i] = now - 1; cs1[now] = cs1[now - 1] * ((dat[G[v][i]] + 1) % M) % M, ++now; ans[v] = ans[v] * ((dat[G[v][i]] + 1) % M) % M; } now = 1; rep(i, n) { if (G[v][n - 1 - i] == p) continue; m2[n - 1 - i] = now - 1; cs2[now] = cs2[now - 1] * ((dat[G[v][n - 1 - i]] + 1) % M) % M, ++now; } rep(i, n) { if (G[v][i] == p) continue; dfs2(G[v][i], v, cs1[m1[i]] * cs2[m2[i]] % M); } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M; rep(i, N - 1) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); G[b].push_back(a); } dfs1(0, -1); if (N > 1) dfs2(0, -1, 0); rep(i, N) cout << ans[i] << endl; }
replace
50
51
50
51
0
p03181
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pb(x) emplace_back(x) #define forn(i, N) for (int i = 0; i < N; i++) #define rep(i, x, N) for (int i = x; i <= N; i++) #define revrep(i, x, N) for (int i = x; i >= N; i--) #define ar array using namespace std; using ll = long long int; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vd = vector<double>; using vvi = vector<vi>; int ctoi(char c) { return c - '0'; } int slen(string s) { return s.length(); } int vsize(vi a) { return (int)a.size(); } int in() { int x; cin >> x; return x; } void tf(bool ok) { cout << (ok ? "YES" : "NO") << endl; } // https://dmoj.ca/problem/dpv const int MAXN = 1e5 + 10; vector<int> g[MAXN]; ll dp[MAXN], c[MAXN], ans[MAXN]; int N, M; void dfs1(int e, int p) { dp[e] = 1; vi cdrn; for (int &u : g[e]) { if (u != p) { dfs1(u, e); dp[e] = (dp[e] * (dp[u] + 1)) % M; cdrn.pb(u); } } vll ps, ss; ll pref = 1, suf = 1; forn(i, vsize(cdrn)) { ps.pb(pref); pref = (pref * (dp[cdrn[i]] + 1)) % M; } revrep(i, vsize(cdrn) - 1, 0) { ss.pb(suf); suf = (suf * (dp[cdrn[i]] + 1)) % M; } reverse(all(ss)); forn(i, vsize(cdrn)) { c[cdrn[i]] = (ps[i] * ss[i]) % M; } } void dfs2(int e, int p) { ans[e] = (dp[e] * c[e]) % M; for (int &u : g[e]) { if (u != p) { c[u] = (c[u] * c[e] + 1) % M; dfs2(u, e); } } } int main() { // freopen("input.txt","rt",stdin); ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> M; forn(i, N - 1) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } dfs1(1, -1); c[1] = 1; dfs2(1, -1); rep(i, 1, N) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pb(x) emplace_back(x) #define forn(i, N) for (int i = 0; i < N; i++) #define rep(i, x, N) for (int i = x; i <= N; i++) #define revrep(i, x, N) for (int i = x; i >= N; i--) #define ar array using namespace std; using ll = long long int; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vd = vector<double>; using vvi = vector<vi>; int ctoi(char c) { return c - '0'; } int slen(string s) { return s.length(); } int vsize(vi a) { return (int)a.size(); } int in() { int x; cin >> x; return x; } void tf(bool ok) { cout << (ok ? "YES" : "NO") << endl; } // https://dmoj.ca/problem/dpv const int MAXN = 1e5 + 10; vector<int> g[MAXN]; ll dp[MAXN], c[MAXN], ans[MAXN]; int N, M; void dfs1(int e, int p) { dp[e] = 1; vi cdrn; for (int &u : g[e]) { if (u != p) { dfs1(u, e); dp[e] = (dp[e] * (dp[u] + 1)) % M; cdrn.pb(u); } } vll ps, ss; ll pref = 1, suf = 1; forn(i, vsize(cdrn)) { ps.pb(pref); pref = (pref * (dp[cdrn[i]] + 1)) % M; } revrep(i, vsize(cdrn) - 1, 0) { ss.pb(suf); suf = (suf * (dp[cdrn[i]] + 1)) % M; } reverse(all(ss)); forn(i, vsize(cdrn)) { c[cdrn[i]] = (ps[i] * ss[i]) % M; } } void dfs2(int e, int p) { ans[e] = (dp[e] * c[e]) % M; for (int &u : g[e]) { if (u != p) { c[u] = (c[u] * c[e] + 1) % M; dfs2(u, e); } } } int main() { cin >> N >> M; forn(i, N - 1) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } dfs1(1, -1); c[1] = 1; dfs2(1, -1); rep(i, 1, N) cout << ans[i] << endl; return 0; }
delete
71
74
71
71
TLE
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vb = vector<bool>; using vl = vector<long>; using vs = vector<string>; using vvi = vector<vector<int>>; using vvb = vector<vector<bool>>; using vvc = vector<vector<char>>; using vvl = vector<vector<long>>; using pii = pair<int, int>; using pil = pair<int, long>; using pll = pair<long, long>; #define fix20 cout << fixed << setprecision(20) #define YES cout << "Yes" << endl #define NO cout << "No" << endl #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, s, t) for (int i = s; i < t; i++) #define RNG(i, s, t, u) for (int i = s; i < t; i += u) #define MOD 1000000007 #define all(vec) vec.begin(), vec.end() struct Edge { int from; int to; long cost; }; vl dp(100010, -1); vector<vector<Edge>> Graph(100010); vl ans(100010, -1); long dfs(int v, int pv, long m) { if (dp[v] != -1) return dp[v]; dp[v] = 1; for (Edge ed : Graph[v]) { int nv = ed.to; if (nv == pv) continue; dp[v] = (dp[v] * (dfs(nv, v, m) + 1)) % m; } return dp[v]; } void dfs2(int v, int pv, long rec, long m) { int s = Graph.at(v).size(); vl acm(s + 1, 1); vl rev(s + 1, 1); rep(i, s) { if (Graph.at(v).at(i).to != pv) { acm.at(i + 1) = (acm.at(i) * (dp[Graph.at(v).at(i).to] + 1)) % m; } else { acm.at(i + 1) = (acm.at(i) * (rec + 1)) % m; } if (Graph.at(v).at(m - 1 - i).to != pv) { rev.at(s - 1 - i) = (rev.at(s - i) * (dp[Graph.at(v).at(s - 1 - i).to] + 1)) % m; } else { rev.at(s - 1 - i) = (rev.at(s - i) * (rec + 1)) % m; } } ans[v] = acm[s]; rep(i, s) { int nv = Graph.at(v).at(i).to; if (nv == pv) continue; long r = acm[i] * rev[i + 1] % m; dfs2(nv, v, r, m); } } int main() { long n, m; cin >> n >> m; rep(i, n - 1) { int x, y; cin >> x >> y; x--; y--; Graph.at(x).push_back({x, y, 1}); Graph.at(y).push_back({y, x, 1}); } ans[0] = dfs(0, -1, m); dfs2(0, -1, 1, m); rep(i, n) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vb = vector<bool>; using vl = vector<long>; using vs = vector<string>; using vvi = vector<vector<int>>; using vvb = vector<vector<bool>>; using vvc = vector<vector<char>>; using vvl = vector<vector<long>>; using pii = pair<int, int>; using pil = pair<int, long>; using pll = pair<long, long>; #define fix20 cout << fixed << setprecision(20) #define YES cout << "Yes" << endl #define NO cout << "No" << endl #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, s, t) for (int i = s; i < t; i++) #define RNG(i, s, t, u) for (int i = s; i < t; i += u) #define MOD 1000000007 #define all(vec) vec.begin(), vec.end() struct Edge { int from; int to; long cost; }; vl dp(100010, -1); vector<vector<Edge>> Graph(100010); vl ans(100010, -1); long dfs(int v, int pv, long m) { if (dp[v] != -1) return dp[v]; dp[v] = 1; for (Edge ed : Graph[v]) { int nv = ed.to; if (nv == pv) continue; dp[v] = (dp[v] * (dfs(nv, v, m) + 1)) % m; } return dp[v]; } void dfs2(int v, int pv, long rec, long m) { int s = Graph.at(v).size(); vl acm(s + 1, 1); vl rev(s + 1, 1); rep(i, s) { if (Graph.at(v).at(i).to != pv) { acm.at(i + 1) = (acm.at(i) * (dp[Graph.at(v).at(i).to] + 1)) % m; } else { acm.at(i + 1) = (acm.at(i) * (rec + 1)) % m; } if (Graph.at(v).at(s - 1 - i).to != pv) { rev.at(s - 1 - i) = (rev.at(s - i) * (dp[Graph.at(v).at(s - 1 - i).to] + 1)) % m; } else { rev.at(s - 1 - i) = (rev.at(s - i) * (rec + 1)) % m; } } ans[v] = acm[s]; rep(i, s) { int nv = Graph.at(v).at(i).to; if (nv == pv) continue; long r = acm[i] * rev[i + 1] % m; dfs2(nv, v, r, m); } } int main() { long n, m; cin >> n >> m; rep(i, n - 1) { int x, y; cin >> x >> y; x--; y--; Graph.at(x).push_back({x, y, 1}); Graph.at(y).push_back({y, x, 1}); } ans[0] = dfs(0, -1, m); dfs2(0, -1, 1, m); rep(i, n) { cout << ans[i] << endl; } }
replace
55
56
55
56
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 99) >= this->size() (which is 1)
p03181
C++
Runtime Error
#include <bits/stdc++.h> #define all(cont) cont.begin(), cont.end() #define pb push_back #define fi first #define se second #define DEBUG(x) cerr << (#x) << ": " << (x) << '\n' using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef long long ll; typedef unsigned long long ull; template <class T> bool uin(T &a, T b) { return (a < b ? false : (a = b, true)); } template <class T> bool uax(T &a, T b) { return (a > b ? false : (a = b, true)); } //~ ifstream f(".in"); //~ ofstream g(".out"); const int NMAX = 1e5 + 5; int n, MOD; vector<int> adj[NMAX]; int down[NMAX]; int up[NMAX]; void mul(int &a, int b) { a = (1LL * a * b) % MOD; } void add(int &a, int b) { a = (a + b) % MOD; } int prev(const vector<int> &v, int i) { return (i == 0) ? 1 : v[i - 1]; } int next(const vector<int> &v, int i) { return (i == (int)v.size() - 1) ? 1 : v[i + 1]; } void dfs(int node, int parent, int type) { if (type == 0) { down[node] = up[node] = 1; for (int &x : adj[node]) { if (x == parent) { continue; } dfs(x, node, type); mul(down[node], down[x] + 1); } return; } vector<int> pref_prod(adj[node].size() - 1, 1); vector<int> suff_prod(adj[node].size() - 1, 1); for (int i = 0, idx = 0; i < (int)adj[node].size(); ++i) { int x = adj[node][i]; if (x != parent) { pref_prod[idx] = 1LL * prev(pref_prod, idx) * (down[x] + 1) % MOD; ++idx; } } for (int i = adj[node].size() - 1, idx = i - 1; i >= 0; --i) { int x = adj[node][i]; if (x != parent) { suff_prod[i] = 1LL * next(suff_prod, i) * (down[x] + 1) % MOD; --idx; } } for (int i = 0, idx = 0; i < (int)adj[node].size(); ++i) { int x = adj[node][i]; if (x != parent) { add(up[x], (1LL * up[node] * prev(pref_prod, idx) % MOD) * next(suff_prod, idx) % MOD); ++idx; dfs(x, node, type); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); #ifdef LOCAL_DEFINE freopen(".in", "r", stdin); #endif cin >> n >> MOD; for (int i = 1; i <= n - 1; ++i) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } adj[1].push_back(0); dfs(1, 0, 0); dfs(1, 0, 1); for (int i = 1; i <= n; ++i) { cout << 1LL * down[i] * up[i] % MOD << '\n'; } #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
#include <bits/stdc++.h> #define all(cont) cont.begin(), cont.end() #define pb push_back #define fi first #define se second #define DEBUG(x) cerr << (#x) << ": " << (x) << '\n' using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef long long ll; typedef unsigned long long ull; template <class T> bool uin(T &a, T b) { return (a < b ? false : (a = b, true)); } template <class T> bool uax(T &a, T b) { return (a > b ? false : (a = b, true)); } //~ ifstream f(".in"); //~ ofstream g(".out"); const int NMAX = 1e5 + 5; int n, MOD; vector<int> adj[NMAX]; int down[NMAX]; int up[NMAX]; void mul(int &a, int b) { a = (1LL * a * b) % MOD; } void add(int &a, int b) { a = (a + b) % MOD; } int prev(const vector<int> &v, int i) { return (i == 0) ? 1 : v[i - 1]; } int next(const vector<int> &v, int i) { return (i == (int)v.size() - 1) ? 1 : v[i + 1]; } void dfs(int node, int parent, int type) { if (type == 0) { down[node] = up[node] = 1; for (int &x : adj[node]) { if (x == parent) { continue; } dfs(x, node, type); mul(down[node], down[x] + 1); } return; } vector<int> pref_prod(adj[node].size() - 1, 1); vector<int> suff_prod(adj[node].size() - 1, 1); for (int i = 0, idx = 0; i < (int)adj[node].size(); ++i) { int x = adj[node][i]; if (x != parent) { pref_prod[idx] = 1LL * prev(pref_prod, idx) * (down[x] + 1) % MOD; ++idx; } } for (int i = adj[node].size() - 1, idx = i - 1; i >= 0; --i) { int x = adj[node][i]; if (x != parent) { suff_prod[idx] = 1LL * next(suff_prod, idx) * (down[x] + 1) % MOD; --idx; } } for (int i = 0, idx = 0; i < (int)adj[node].size(); ++i) { int x = adj[node][i]; if (x != parent) { add(up[x], (1LL * up[node] * prev(pref_prod, idx) % MOD) * next(suff_prod, idx) % MOD); ++idx; dfs(x, node, type); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); #ifdef LOCAL_DEFINE freopen(".in", "r", stdin); #endif cin >> n >> MOD; for (int i = 1; i <= n - 1; ++i) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } adj[1].push_back(0); dfs(1, 0, 0); dfs(1, 0, 1); for (int i = 1; i <= n; ++i) { cout << 1LL * down[i] * up[i] % MOD << '\n'; } #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
replace
65
66
65
66
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define ll long long #define pq priority_queue #define mp make_pair #define pii pair<int, int> #define mod 998244353 int lowbit(int x) { return x & (-x); } int n, m; vector<int> edge[100010]; int ans[100010]; int ANS[100010]; vector<int> son[100010]; vector<int> pre[100010], suf[100010]; int dfs(int node, int lst) { ans[node] = 1; for (int i = 0; i < edge[node].size(); i++) { if (edge[node][i] == lst) continue; son[node].pb(edge[node][i]); ans[node] = (ll)(ans[node]) * (ll)(dfs(edge[node][i], node) + 1) % m; // cout<<node<<" "<<ans[node]<<endl; } for (int i = 0; i < son[node].size(); i++) { if (i == 0) pre[node].pb((ans[son[node][i]] + 1) % m); else pre[node].pb((ll)pre[node][i - 1] * (ll)(ans[son[node][i]] + 1) % m); } for (int i = son[node].size() - 1; i >= 0; i--) { if (i == son[node].size() - 1) suf[node].pb((ans[son[node][i]] + 1) % m); else suf[node].pb((ll)suf[node][i - 1] * (ll)(ans[son[node][i]] + 1) % m); } reverse(suf[node].begin(), suf[node].end()); return ans[node]; } void getANS(int node, int lst, int cnt) { ANS[node] = (ll)ans[node] * (ll)(cnt + 1) % m; for (int i = 0; i < son[node].size(); i++) { ll tmp = 1ll; if (i) tmp *= (ll)pre[node][i - 1]; if (i < son[node].size() - 1) tmp *= (ll)suf[node][i + 1]; tmp %= m; // cout<<son[node][i]<<" "<<tmp<<endl; tmp *= (ll)(cnt + 1), tmp %= m; getANS(son[node][i], node, tmp); } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); edge[x].pb(y); edge[y].pb(x); } cout << dfs(1, -1) << endl; getANS(1, -1, 0); for (int i = 2; i <= n; i++) cout << ANS[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define ll long long #define pq priority_queue #define mp make_pair #define pii pair<int, int> #define mod 998244353 int lowbit(int x) { return x & (-x); } int n, m; vector<int> edge[100010]; int ans[100010]; int ANS[100010]; vector<int> son[100010]; vector<int> pre[100010], suf[100010]; int dfs(int node, int lst) { ans[node] = 1; for (int i = 0; i < edge[node].size(); i++) { if (edge[node][i] == lst) continue; son[node].pb(edge[node][i]); ans[node] = (ll)(ans[node]) * (ll)(dfs(edge[node][i], node) + 1) % m; // cout<<node<<" "<<ans[node]<<endl; } for (int i = 0; i < son[node].size(); i++) { if (i == 0) pre[node].pb((ans[son[node][i]] + 1) % m); else pre[node].pb((ll)pre[node][i - 1] * (ll)(ans[son[node][i]] + 1) % m); } for (int i = son[node].size() - 1; i >= 0; i--) { if (i == son[node].size() - 1) suf[node].pb((ans[son[node][i]] + 1) % m); else suf[node].pb((ll)suf[node][son[node].size() - 1 - i - 1] * (ll)(ans[son[node][i]] + 1) % m); } reverse(suf[node].begin(), suf[node].end()); return ans[node]; } void getANS(int node, int lst, int cnt) { ANS[node] = (ll)ans[node] * (ll)(cnt + 1) % m; for (int i = 0; i < son[node].size(); i++) { ll tmp = 1ll; if (i) tmp *= (ll)pre[node][i - 1]; if (i < son[node].size() - 1) tmp *= (ll)suf[node][i + 1]; tmp %= m; // cout<<son[node][i]<<" "<<tmp<<endl; tmp *= (ll)(cnt + 1), tmp %= m; getANS(son[node][i], node, tmp); } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); edge[x].pb(y); edge[y].pb(x); } cout << dfs(1, -1) << endl; getANS(1, -1, 0); for (int i = 2; i <= n; i++) cout << ANS[i] << endl; return 0; }
replace
39
40
39
41
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> G(n), x(n); vector<int> dp(n, 1), par(n); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } function<void(int, int)> dfs1 = [&](int u, int p) { for (int v : G[u]) { if (v == p) { par[u] = x[u].size(); x[u].push_back(-1); } else { dfs1(v, u); x[u].push_back((dp[v] + 1) % m); dp[u] = 1LL * dp[u] * x[u].back() % m; } } }; dfs1(0, -1); function<void(int, int, int)> dfs2 = [&](int u, int p, int y) { if (p != -1) { x[u][par[u]] = y; dp[u] = 1LL * dp[u] * y % m; } vector<int> s = x[u]; for (int i = 0; i + 1 < s.size(); i++) s[i + 1] = 1LL * s[i + 1] * s[i] % m; for (int i = s.size() - 1; i; i--) x[u][i - 1] = 1LL * x[u][i - 1] * x[u][i] % m; for (int i = 0; i < s.size(); i++) { if (G[u][i] != p) { int a = (i ? s[i - 1] : 1); int b = (i + 1 < s.size() ? x[u][i + 1] : 1); dfs2(G[u][i], u, (1LL * a * b + 1) % m); } } }; dfs2(0, -1, 1); for (int i = 0; i < n; i++) cout << dp[i] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> G(n), x(n); vector<int> dp(n, 1), par(n); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } function<void(int, int)> dfs1 = [&](int u, int p) { for (int v : G[u]) { if (v == p) { par[u] = x[u].size(); x[u].push_back(-1); } else { dfs1(v, u); x[u].push_back((dp[v] + 1) % m); dp[u] = 1LL * dp[u] * x[u].back() % m; } } }; dfs1(0, -1); function<void(int, int, int)> dfs2 = [&](int u, int p, int y) { if (p != -1) { x[u][par[u]] = y; dp[u] = 1LL * dp[u] * y % m; } vector<int> s = x[u]; for (int i = 0; i + 1 < s.size(); i++) s[i + 1] = 1LL * s[i + 1] * s[i] % m; for (int i = s.size() - 1; i > 0; i--) x[u][i - 1] = 1LL * x[u][i - 1] * x[u][i] % m; for (int i = 0; i < s.size(); i++) { if (G[u][i] != p) { int a = (i ? s[i - 1] : 1); int b = (i + 1 < s.size() ? x[u][i + 1] : 1); dfs2(G[u][i], u, (1LL * a * b + 1) % m); } } }; dfs2(0, -1, 1); for (int i = 0; i < n; i++) cout << dp[i] << endl; }
replace
37
38
37
38
0
p03181
C++
Runtime Error
//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC target ("avx2") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC //target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma //GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define sim template <class c #define ris return *this #define dor > debug &operator<< #define eni(x) \ sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \ c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c *x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair<b, c> d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c &) { ris; } #endif } ; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define shandom_ruffle random_shuffle using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; const int nax = 3007; ll mod = 1000 * 1000 * 1000 + 7; const ll inf = 1e18; int n; vi graf[nax]; ll dol[nax]; ll gor[nax]; ll wyn[nax]; void dfs1(int v, int oj) { for (int &i : graf[v]) { if (i == oj) { swap(i, graf[v].back()); graf[v].pop_back(); break; } } dol[v] = 1; for (int i : graf[v]) { dfs1(i, v); dol[v] = (dol[v] * dol[i]) % mod; } dol[v] = (dol[v] + 1) % mod; } ll pre[nax]; ll suf[nax]; void dfs2(int v, int oj) { int r = graf[v].size(); pre[0] = suf[r + 1] = 1; for (int i = 1; i <= r; i++) pre[i] = (pre[i - 1] * dol[graf[v][i - 1]]) % mod; for (int i = r; i; i--) suf[i] = (suf[i + 1] * dol[graf[v][i - 1]]) % mod; ll mno = gor[v]; //~ debug() << v; //~ debug() << range(pre+1, pre+1+r); //~ debug() << range(suf+1, suf+1+r); for (int i = 1; i <= r; i++) { ll x = (pre[i - 1] * suf[i + 1]) % mod; x = (x * mno + 1) % mod; gor[graf[v][i - 1]] = x; } wyn[v] = gor[v] % mod; for (int i : graf[v]) wyn[v] = (wyn[v] * dol[i]) % mod; for (int i : graf[v]) { dfs2(i, v); } } int main() { scanf("%d%lld", &n, &mod); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); graf[a].push_back(b); graf[b].push_back(a); } dfs1(1, 0); gor[1] = 1; dfs2(1, 0); for (int i = 1; i <= n; i++) printf("%lld\n", wyn[i]); return 0; }
//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC target ("avx2") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC //target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma //GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define sim template <class c #define ris return *this #define dor > debug &operator<< #define eni(x) \ sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \ c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c *x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair<b, c> d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c &) { ris; } #endif } ; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define shandom_ruffle random_shuffle using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; const int nax = 1000 * 1007; ll mod = 1000 * 1000 * 1000 + 7; const ll inf = 1e18; int n; vi graf[nax]; ll dol[nax]; ll gor[nax]; ll wyn[nax]; void dfs1(int v, int oj) { for (int &i : graf[v]) { if (i == oj) { swap(i, graf[v].back()); graf[v].pop_back(); break; } } dol[v] = 1; for (int i : graf[v]) { dfs1(i, v); dol[v] = (dol[v] * dol[i]) % mod; } dol[v] = (dol[v] + 1) % mod; } ll pre[nax]; ll suf[nax]; void dfs2(int v, int oj) { int r = graf[v].size(); pre[0] = suf[r + 1] = 1; for (int i = 1; i <= r; i++) pre[i] = (pre[i - 1] * dol[graf[v][i - 1]]) % mod; for (int i = r; i; i--) suf[i] = (suf[i + 1] * dol[graf[v][i - 1]]) % mod; ll mno = gor[v]; //~ debug() << v; //~ debug() << range(pre+1, pre+1+r); //~ debug() << range(suf+1, suf+1+r); for (int i = 1; i <= r; i++) { ll x = (pre[i - 1] * suf[i + 1]) % mod; x = (x * mno + 1) % mod; gor[graf[v][i - 1]] = x; } wyn[v] = gor[v] % mod; for (int i : graf[v]) wyn[v] = (wyn[v] * dol[i]) % mod; for (int i : graf[v]) { dfs2(i, v); } } int main() { scanf("%d%lld", &n, &mod); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); graf[a].push_back(b); graf[b].push_back(a); } dfs1(1, 0); gor[1] = 1; dfs2(1, 0); for (int i = 1; i <= n; i++) printf("%lld\n", wyn[i]); return 0; }
replace
61
62
61
62
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair typedef long long ll; typedef pair<int, int> ii; const int len = 1e5 + 5; int n, m; int par[len]; vector<ii> adj[len]; vector<int> dp[len], pref[len], suf[len]; int add(int a, int b) { return (a + b) % m; } int mul(int a, int b) { return (a * 1LL * b) % m; } void dfs1(int u, int x) { dp[u][x] = 1; for (int j = 1; j < adj[u].size(); j++) { ii v = adj[u][j]; if (v.fi == par[u]) continue; par[v.fi] = u; dfs1(v.fi, v.se); dp[u][x] = mul(dp[u][x], dp[v.fi][v.se]); } dp[u][x] = add(dp[u][x], 1); } void dfs2(int u) { pref[u][0] = 1; for (int j = 1; j < adj[u].size(); j++) { ii v = adj[u][j]; pref[u][j] = mul(pref[u][j - 1], dp[v.fi][v.se]); } suf[u][adj[u].size()] = 1; for (int j = adj[u].size() - 1; j >= 1; j--) { ii v = adj[u][j]; suf[u][j] = mul(suf[u][j + 1], dp[v.fi][v.se]); } for (int j = 1; j < adj[u].size(); j++) { ii v = adj[u][j]; dp[u][j] = add(mul(pref[u][j - 1], suf[u][j + 1]), 1); if (v.fi != par[u]) dfs2(v.fi); } dp[u][0] = add(suf[u][1], 1); } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) adj[i].pb(mp(i, 0)); for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d %d", &a, &b); adj[a].pb(mp(b, adj[b].size())); adj[b].pb(mp(a, adj[a].size() - 1)); } for (int i = 1; i <= n; i++) { dp[i].resize(adj[i].size()); pref[i].resize(adj[i].size()); suf[i].resize(adj[i].size()); } dfs1(1, 0); dfs2(1); for (int i = 1; i <= n; i++) printf("%d\n", add(dp[i][0], m - 1)); return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair typedef long long ll; typedef pair<int, int> ii; const int len = 1e5 + 5; int n, m; int par[len]; vector<ii> adj[len]; vector<int> dp[len], pref[len], suf[len]; int add(int a, int b) { return (a + b) % m; } int mul(int a, int b) { return (a * 1LL * b) % m; } void dfs1(int u, int x) { dp[u][x] = 1; for (int j = 1; j < adj[u].size(); j++) { ii v = adj[u][j]; if (v.fi == par[u]) continue; par[v.fi] = u; dfs1(v.fi, v.se); dp[u][x] = mul(dp[u][x], dp[v.fi][v.se]); } dp[u][x] = add(dp[u][x], 1); } void dfs2(int u) { pref[u][0] = 1; for (int j = 1; j < adj[u].size(); j++) { ii v = adj[u][j]; pref[u][j] = mul(pref[u][j - 1], dp[v.fi][v.se]); } suf[u][adj[u].size()] = 1; for (int j = adj[u].size() - 1; j >= 1; j--) { ii v = adj[u][j]; suf[u][j] = mul(suf[u][j + 1], dp[v.fi][v.se]); } for (int j = 1; j < adj[u].size(); j++) { ii v = adj[u][j]; dp[u][j] = add(mul(pref[u][j - 1], suf[u][j + 1]), 1); if (v.fi != par[u]) dfs2(v.fi); } dp[u][0] = add(suf[u][1], 1); } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) adj[i].pb(mp(i, 0)); for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d %d", &a, &b); adj[a].pb(mp(b, adj[b].size())); adj[b].pb(mp(a, adj[a].size() - 1)); } for (int i = 1; i <= n; i++) { dp[i].resize(adj[i].size()); pref[i].resize(adj[i].size()); suf[i].resize(adj[i].size() + 1); } dfs1(1, 0); dfs2(1); for (int i = 1; i <= n; i++) printf("%d\n", add(dp[i][0], m - 1)); return 0; }
replace
71
72
71
72
0
p03181
C++
Runtime Error
#include <bits/stdc++.h> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/pb_ds/assoc_container.hpp> #include <chrono> using namespace std; using namespace std::chrono; // using namespace __gnu_pbds; #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define fi first #define se second #define int long long #define pb push_back #define emp emplace_back #define vv(x) vector<x> #define mp(x, y) map<x, y> #define dq(x) deque<x> #define pql(x) priority_queue<x> #define pqs(x) priority_queue<x, vv(x), greater<x>> #define M 1000000007 #define forf(i, a, b) for (int i = a; i < b; i++) #define it(x) x::iterator #define ll long long #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #define time__(d) \ for (long blockTime = 0; \ (blockTime == 0 ? (blockTime = clock()) != 0 : false); \ debug("%s time : %.4fs", d, \ (double)(clock() - blockTime) / CLOCKS_PER_SEC)) #define vii vector<int> #define big 3e18 #define sm -2e9 #define mkr make_pair #define vpi vector<pair<int, int>> #define pii pair<int, int> #define rng 500005 #define sz(x) (int)x.size() #define rv(x) reverse(x.begin(), x.end()) #define out(x) cout << x.fi << " " << x.se << endl; // #define ordered_set tree<pii, null_type,less<pii>, // rb_tree_tag,tree_order_statistics_node_update> void pr_init() { #ifndef ONLINE_JUDGE freopen("gin.txt", "r", stdin); freopen("gout.txt", "w", stdout); #endif } // dp[i] -- > number of ways to color the subtree rooted at i int sub[100005], dp[100005]; vector<vii> gp; int n, m; void dfs1(int u, int p) { sub[u] = 1; for (auto x : gp[u]) if (x != p) { dfs1(x, u); sub[u] = ((sub[x] + 1) % m * sub[u] % m) % m; } } void dfs2(int u, int p, int vl) { dp[u] = (sub[u] % m * (vl + 1) % m) % m; vii ar, suffix, prefix; for (int i = 0; i < sz(gp[u]); i++) { if (gp[u][i] != p) ar.pb(sub[gp[u][i]] + 1); else ar.pb(1); } int s = sz(ar); suffix.resize(s); prefix.resize(s); prefix[0] = 1; suffix[s - 1] = 1; for (int i = 1; i < s; i++) { prefix[i] = (prefix[i - 1] % m * ar[i - 1] % m) % m; } for (int i = s - 2; i >= 0; i--) { suffix[i] = (suffix[i + 1] % m * ar[i + 1] % m) % m; } for (int i = 0; i < sz(gp[u]); i++) if (gp[u][i] != p) { int cvl = ((vl + 1) % m * suffix[i] % m) % m; cvl = (cvl % m * prefix[i] % m) % m; dfs2(gp[u][i], u, cvl); } } void solve() { cin >> n >> m; gp.assign(n + 1, vii()); forf(i, 0, n - 1) { int u, v; cin >> u >> v; gp[u].pb(v); gp[v].pb(u); } dfs1(1, -1); dfs2(1, -1, 0); for (int i = 1; i <= n; i++) { cout << dp[i] % m << "\n"; } } int32_t main() { pr_init(); // fastio; auto start = high_resolution_clock::now(); solve(); auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop - start); // cout << "Time taken by function: " // << duration.count() << " microseconds" << endl; }
#include <bits/stdc++.h> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/pb_ds/assoc_container.hpp> #include <chrono> using namespace std; using namespace std::chrono; // using namespace __gnu_pbds; #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define fi first #define se second #define int long long #define pb push_back #define emp emplace_back #define vv(x) vector<x> #define mp(x, y) map<x, y> #define dq(x) deque<x> #define pql(x) priority_queue<x> #define pqs(x) priority_queue<x, vv(x), greater<x>> #define M 1000000007 #define forf(i, a, b) for (int i = a; i < b; i++) #define it(x) x::iterator #define ll long long #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #define time__(d) \ for (long blockTime = 0; \ (blockTime == 0 ? (blockTime = clock()) != 0 : false); \ debug("%s time : %.4fs", d, \ (double)(clock() - blockTime) / CLOCKS_PER_SEC)) #define vii vector<int> #define big 3e18 #define sm -2e9 #define mkr make_pair #define vpi vector<pair<int, int>> #define pii pair<int, int> #define rng 500005 #define sz(x) (int)x.size() #define rv(x) reverse(x.begin(), x.end()) #define out(x) cout << x.fi << " " << x.se << endl; // #define ordered_set tree<pii, null_type,less<pii>, // rb_tree_tag,tree_order_statistics_node_update> void pr_init() { #ifndef ONLINE_JUDGE freopen("gin.txt", "r", stdin); freopen("gout.txt", "w", stdout); #endif } // dp[i] -- > number of ways to color the subtree rooted at i int sub[100005], dp[100005]; vector<vii> gp; int n, m; void dfs1(int u, int p) { sub[u] = 1; for (auto x : gp[u]) if (x != p) { dfs1(x, u); sub[u] = ((sub[x] + 1) % m * sub[u] % m) % m; } } void dfs2(int u, int p, int vl) { dp[u] = (sub[u] % m * (vl + 1) % m) % m; vii ar, suffix, prefix; for (int i = 0; i < sz(gp[u]); i++) { if (gp[u][i] != p) ar.pb(sub[gp[u][i]] + 1); else ar.pb(1); } int s = sz(ar); suffix.resize(s); prefix.resize(s); prefix[0] = 1; suffix[s - 1] = 1; for (int i = 1; i < s; i++) { prefix[i] = (prefix[i - 1] % m * ar[i - 1] % m) % m; } for (int i = s - 2; i >= 0; i--) { suffix[i] = (suffix[i + 1] % m * ar[i + 1] % m) % m; } for (int i = 0; i < sz(gp[u]); i++) if (gp[u][i] != p) { int cvl = ((vl + 1) % m * suffix[i] % m) % m; cvl = (cvl % m * prefix[i] % m) % m; dfs2(gp[u][i], u, cvl); } } void solve() { cin >> n >> m; gp.assign(n + 1, vii()); if (n == 1) { cout << "1\n"; return; } forf(i, 0, n - 1) { int u, v; cin >> u >> v; gp[u].pb(v); gp[v].pb(u); } dfs1(1, -1); dfs2(1, -1, 0); for (int i = 1; i <= n; i++) { cout << dp[i] % m << "\n"; } } int32_t main() { pr_init(); // fastio; auto start = high_resolution_clock::now(); solve(); auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop - start); // cout << "Time taken by function: " // << duration.count() << " microseconds" << endl; }
insert
101
101
101
105
-11
p03181
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define int long long // int MOD = 1000000007; int N, M; vector<vector<int>> g; vector<vector<int>> dp; vector<vector<int>> sum1; vector<vector<int>> sum2; vector<int> X; map<pair<int, int>, int> mp; int solve(int a, int x) { // cerr << a << " " << x << endl; if (dp[a][x] == -1) { int ne = g[a][x]; if (X[ne] == -1) { X[ne] = a; dp[a][x] = 1; for (int i = 0; i < g[ne].size(); i++) { if (g[ne][i] != a) { dp[a][x] = (dp[a][x] * solve(ne, i)) % M; } } dp[a][x]++; } else { solve(ne, mp[make_pair(ne, X[ne])]); for (int i = 0; i < g[ne].size(); i++) { sum1[ne][i + 1] = (sum1[ne][i] * dp[ne][i]) % M; } for (int i = (int)g[ne].size() - 1; i >= 0; i--) { sum2[ne][i] = (sum2[ne][i + 1] * dp[ne][i]) % M; } X[ne] = -2; } if (X[ne] == -2) { int idx = mp[make_pair(ne, a)]; dp[a][x] = (sum1[ne][idx] * sum2[ne][idx + 1]) % M; dp[a][x]++; } } return dp[a][x]; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M; g.resize(N); dp.resize(N); sum1.resize(N); sum2.resize(N); X.resize(N, -1); int res = 1; int a, b; for (int i = 0; i < N - 1; i++) { cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); dp[a].push_back(-1); dp[b].push_back(-1); sum1[a].push_back(1); sum1[b].push_back(1); sum2[a].push_back(1); sum2[b].push_back(1); } for (int i = 0; i < N; i++) { sum1[i].push_back(1); sum2[i].push_back(1); } for (int i = 0; i < N; i++) { res = 1; for (int j = 0; j < g[i].size(); j++) { mp[make_pair(i, g[i][j])] = j; } } for (int i = 0; i < N; i++) { res = 1; for (int j = 0; j < g[i].size(); j++) { res = (res * solve(i, j)) % M; } cout << res << endl; } }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define int long long // int MOD = 1000000007; int N, M; vector<vector<int>> g; vector<vector<int>> dp; vector<vector<int>> sum1; vector<vector<int>> sum2; vector<int> X; map<pair<int, int>, int> mp; int solve(int a, int x) { // cerr << a << " " << x << endl; if (dp[a][x] == -1) { int ne = g[a][x]; if (X[ne] == -1) { X[ne] = a; dp[a][x] = 1; for (int i = 0; i < g[ne].size(); i++) { if (g[ne][i] != a) { dp[a][x] = (dp[a][x] * solve(ne, i)) % M; } } dp[a][x]++; } else if (X[ne] != -2) { solve(ne, mp[make_pair(ne, X[ne])]); for (int i = 0; i < g[ne].size(); i++) { sum1[ne][i + 1] = (sum1[ne][i] * dp[ne][i]) % M; } for (int i = (int)g[ne].size() - 1; i >= 0; i--) { sum2[ne][i] = (sum2[ne][i + 1] * dp[ne][i]) % M; } X[ne] = -2; } if (X[ne] == -2) { int idx = mp[make_pair(ne, a)]; dp[a][x] = (sum1[ne][idx] * sum2[ne][idx + 1]) % M; dp[a][x]++; } } return dp[a][x]; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> M; g.resize(N); dp.resize(N); sum1.resize(N); sum2.resize(N); X.resize(N, -1); int res = 1; int a, b; for (int i = 0; i < N - 1; i++) { cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); dp[a].push_back(-1); dp[b].push_back(-1); sum1[a].push_back(1); sum1[b].push_back(1); sum2[a].push_back(1); sum2[b].push_back(1); } for (int i = 0; i < N; i++) { sum1[i].push_back(1); sum2[i].push_back(1); } for (int i = 0; i < N; i++) { res = 1; for (int j = 0; j < g[i].size(); j++) { mp[make_pair(i, g[i][j])] = j; } } for (int i = 0; i < N; i++) { res = 1; for (int j = 0; j < g[i].size(); j++) { res = (res * solve(i, j)) % M; } cout << res << endl; } }
replace
36
37
36
37
TLE
p03182
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i)) #define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r, v) auto r = (v) #else #define aut(r, v) __typeof(v) r = (v) #endif #define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it) #define all(o) (o).begin(), (o).end() #define pb(x) push_back(x) #define mp(x, y) make_pair((x), (y)) #define mset(m, v) memset(m, v, sizeof(m)) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL - 1 double EPS = 1e-9; using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpii; typedef long long ll; typedef complex<double> P; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } long long int MOD = 1000000007; /* cin >> n; vi a(n); rep(i,n)cin>>a[i]; */ template <typename T> struct LazySegmentTree { int n; vector<T> data; vector<T> lazy; T INITIAL_DATA_VALUE; T INITIAL_LAZY_VALUE; // 以下が三つの演算 static T merge(T x, T y); void updateNode(int k, T x); void apply(int k, int seg_len); void init(int size, T initial_data_value, T initial_lazy_value) { n = 1; INITIAL_DATA_VALUE = initial_data_value; INITIAL_LAZY_VALUE = initial_lazy_value; while (n < size) n *= 2; data.resize(2 * n - 1, INITIAL_DATA_VALUE); lazy.resize(2 * n - 1, INITIAL_LAZY_VALUE); } LazySegmentTree(int size, T initial_data_value, T initial_lazy_value) { init(size, initial_data_value, initial_lazy_value); } LazySegmentTree(int size, T initial_value) { init(size, initial_value, initial_value); } T getLeaf(int k) { return data[k + n - 1]; } void eval(int k, int l, int r) { if (lazy[k] == INITIAL_LAZY_VALUE) return; apply(k, r - l); if (r - l > 1) { updateNode(2 * k + 1, lazy[k]); updateNode(2 * k + 2, lazy[k]); } lazy[k] = INITIAL_LAZY_VALUE; } // 区間[a, b)に対する更新 // k:節点番号, [l, r):節点に対応する区間 void update(int a, int b, T x, int k, int l, int r) { eval(k, l, r); //[a, b)と[l, r)が交差しない場合 if (r <= a || b <= l) return; //[a, b)が[l, r)を含む場合、節点の値 if (a <= l && r <= b) { updateNode(k, x); eval(k, l, r); } else { update(a, b, x, k * 2 + 1, l, (l + r) / 2); update(a, b, x, k * 2 + 2, (l + r) / 2, r); data[k] = merge(data[2 * k + 1], data[2 * k + 2]); } } void update(int a, int b, T x) { update(a, b, x, 0, 0, n); } //[a, b)に対するクエリに応答 // k:節点番号, [l, r):節点に対応する区間 T query(int a, int b, int k, int l, int r) { eval(k, l, r); //[a, b)と[l, r)が交差しない場合 if (r <= a || b <= l) return INITIAL_DATA_VALUE; //[a, b)が[l, r)を含む場合、節点の値 if (a <= l && r <= b) return data[k]; else { // 二つの子をマージ(モノイド演算) T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return merge(vl, vr); } } // 外から呼ぶ時 デフォルト値にしてもよい。 T query(int a, int b) { return query(a, b, 0, 0, n); } }; // この三つの演算をいじくる。作用素とモノイド? template <typename T> T LazySegmentTree<T>::merge(T x, T y) { return max(x, y); } template <typename T> void LazySegmentTree<T>::updateNode(int k, T x) { lazy[k] += x; } template <typename T> void LazySegmentTree<T>::apply(int k, int seg_len) { data[k] += lazy[k]; } int N, M; int main() { cin >> N >> M; vpii lfromr[114514]; vi val; rep(i, M) { int l, r; ll x; cin >> l >> r >> x; val.pb(x); lfromr[r].pb(mp(l, i)); } LazySegmentTree<ll> seg(N + 1, 0); for (int i = 1; i <= N; i++) { ll ma = seg.query(1, i + 1); seg.update(i, i + 1, ma); rep(j, lfromr[i].size()) { seg.update(lfromr[i][j].first, i + 1, val[lfromr[i][j].second]); } } // rep(i,N+1)cout<<seg.query(i,i+1)<<endl; cout << seg.query(0, N + 1) << endl; }
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i)) #define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r, v) auto r = (v) #else #define aut(r, v) __typeof(v) r = (v) #endif #define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it) #define all(o) (o).begin(), (o).end() #define pb(x) push_back(x) #define mp(x, y) make_pair((x), (y)) #define mset(m, v) memset(m, v, sizeof(m)) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL - 1 double EPS = 1e-9; using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpii; typedef long long ll; typedef complex<double> P; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } long long int MOD = 1000000007; /* cin >> n; vi a(n); rep(i,n)cin>>a[i]; */ template <typename T> struct LazySegmentTree { int n; vector<T> data; vector<T> lazy; T INITIAL_DATA_VALUE; T INITIAL_LAZY_VALUE; // 以下が三つの演算 static T merge(T x, T y); void updateNode(int k, T x); void apply(int k, int seg_len); void init(int size, T initial_data_value, T initial_lazy_value) { n = 1; INITIAL_DATA_VALUE = initial_data_value; INITIAL_LAZY_VALUE = initial_lazy_value; while (n < size) n *= 2; data.resize(2 * n - 1, INITIAL_DATA_VALUE); lazy.resize(2 * n - 1, INITIAL_LAZY_VALUE); } LazySegmentTree(int size, T initial_data_value, T initial_lazy_value) { init(size, initial_data_value, initial_lazy_value); } LazySegmentTree(int size, T initial_value) { init(size, initial_value, initial_value); } T getLeaf(int k) { return data[k + n - 1]; } void eval(int k, int l, int r) { if (lazy[k] == INITIAL_LAZY_VALUE) return; apply(k, r - l); if (r - l > 1) { updateNode(2 * k + 1, lazy[k]); updateNode(2 * k + 2, lazy[k]); } lazy[k] = INITIAL_LAZY_VALUE; } // 区間[a, b)に対する更新 // k:節点番号, [l, r):節点に対応する区間 void update(int a, int b, T x, int k, int l, int r) { eval(k, l, r); //[a, b)と[l, r)が交差しない場合 if (r <= a || b <= l) return; //[a, b)が[l, r)を含む場合、節点の値 if (a <= l && r <= b) { updateNode(k, x); eval(k, l, r); } else { update(a, b, x, k * 2 + 1, l, (l + r) / 2); update(a, b, x, k * 2 + 2, (l + r) / 2, r); data[k] = merge(data[2 * k + 1], data[2 * k + 2]); } } void update(int a, int b, T x) { update(a, b, x, 0, 0, n); } //[a, b)に対するクエリに応答 // k:節点番号, [l, r):節点に対応する区間 T query(int a, int b, int k, int l, int r) { eval(k, l, r); //[a, b)と[l, r)が交差しない場合 if (r <= a || b <= l) return INITIAL_DATA_VALUE; //[a, b)が[l, r)を含む場合、節点の値 if (a <= l && r <= b) return data[k]; else { // 二つの子をマージ(モノイド演算) T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return merge(vl, vr); } } // 外から呼ぶ時 デフォルト値にしてもよい。 T query(int a, int b) { return query(a, b, 0, 0, n); } }; // この三つの演算をいじくる。作用素とモノイド? template <typename T> T LazySegmentTree<T>::merge(T x, T y) { return max(x, y); } template <typename T> void LazySegmentTree<T>::updateNode(int k, T x) { lazy[k] += x; } template <typename T> void LazySegmentTree<T>::apply(int k, int seg_len) { data[k] += lazy[k]; } int N, M; int main() { cin >> N >> M; vpii lfromr[214514]; vi val; rep(i, M) { int l, r; ll x; cin >> l >> r >> x; val.pb(x); lfromr[r].pb(mp(l, i)); } LazySegmentTree<ll> seg(N + 1, 0); for (int i = 1; i <= N; i++) { ll ma = seg.query(1, i + 1); seg.update(i, i + 1, ma); rep(j, lfromr[i].size()) { seg.update(lfromr[i][j].first, i + 1, val[lfromr[i][j].second]); } } // rep(i,N+1)cout<<seg.query(i,i+1)<<endl; cout << seg.query(0, N + 1) << endl; }
replace
156
157
156
157
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; struct LazySeg { using T = ll; static T op(const T &a, const T &b) { return max(a, b); } static constexpr T e() { return -1e14; } using U = ll; static void ap(const U &f, T &a) { a += f; } static void cp(const U &g, U &f) { f += g; } static constexpr U id() { return 0LL; } const int n; vector<T> t; vector<U> u; LazySeg(int _n) : n(_n), t(2 * n, e()), u(n, id()) {} T &operator[](int i) { return t[i + n]; } void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); } void push() { for (int i = 1; i < n; ++i) push(i); } void apply(const U &f, int i) { ap(f, t[i]); if (i < n) cp(f, u[i]); } void push(int i) { if (u[i] == id()) return; apply(u[i], 2 * i); apply(u[i], 2 * i + 1); u[i] = id(); } void push(int l, int r) { for (int hl = __lg(l + n), hr = __lg(r - 1 + n); hr > 0; --hl, --hr) { int al = (l + n) >> hl, ar = (r - 1 + n) >> hr; if (al < n) push(al); if (ar != al) push(ar); } } T query(int l, int r) { r++; push(l, r); T resl = e(), resr = e(); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = op(resl, t[l++]); if (r & 1) resr = op(t[--r], resr); } return op(resl, resr); } T get(int i) { return query(i, i); } void update(int l, int r, const U &f) { r++; push(l, r); for (int i = l + n, j = r + n; i < j; i >>= 1, j >>= 1) { if (i & 1) apply(f, i++); if (j & 1) apply(f, --j); } l = (l + n) >> __builtin_ctz(l + n); while (l >>= 1) t[l] = op(t[2 * l], t[2 * l + 1]); r = (r + n) >> __builtin_ctz(r + n); while (r >>= 1) t[r] = op(t[2 * r], t[2 * r + 1]); } void set(int i, const T &a) { push(i, i + 1); t[i += n] = a; while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]); } }; constexpr int MAXN = 100005; int n, m; vector<pair<int, ll>> byStart[MAXN]; vector<pair<int, ll>> byEnd[MAXN]; LazySeg st(MAXN); void print() { for (int i = 0; i < n; i++) { cout << st.get(i) << " "; } cout << "\n"; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int l, r; ll a; cin >> l >> r >> a; byStart[l].emplace_back(r, a); byEnd[r].emplace_back(l, a); } for (int i = 0; i <= n; i++) { st.set(i, 0); } st.build(); // take nothing ll res = 0; for (int i = 1; i <= n; i++) { // activate every interval that starts at i for (auto &pr : byStart[i]) { ll a = pr.second; st.update(0, i - 1, a); } // represents maximum score of prefix of length i if s[i] == '1' ll cur = st.query(0, i - 1); res = max(res, cur); st.update(i, i, cur); // deactivate every interval that ends at i for (auto &pr : byEnd[i]) { int l = pr.first; ll a = pr.second; st.update(0, l - 1, -a); } } cout << res << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct LazySeg { using T = ll; static T op(const T &a, const T &b) { return max(a, b); } static constexpr T e() { return -1e14; } using U = ll; static void ap(const U &f, T &a) { a += f; } static void cp(const U &g, U &f) { f += g; } static constexpr U id() { return 0LL; } const int n; vector<T> t; vector<U> u; LazySeg(int _n) : n(_n), t(2 * n, e()), u(n, id()) {} T &operator[](int i) { return t[i + n]; } void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); } void push() { for (int i = 1; i < n; ++i) push(i); } void apply(const U &f, int i) { ap(f, t[i]); if (i < n) cp(f, u[i]); } void push(int i) { if (u[i] == id()) return; apply(u[i], 2 * i); apply(u[i], 2 * i + 1); u[i] = id(); } void push(int l, int r) { for (int hl = __lg(l + n), hr = __lg(r - 1 + n); hr > 0; --hl, --hr) { int al = (l + n) >> hl, ar = (r - 1 + n) >> hr; if (al < n) push(al); if (ar != al) push(ar); } } T query(int l, int r) { r++; push(l, r); T resl = e(), resr = e(); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = op(resl, t[l++]); if (r & 1) resr = op(t[--r], resr); } return op(resl, resr); } T get(int i) { return query(i, i); } void update(int l, int r, const U &f) { r++; push(l, r); for (int i = l + n, j = r + n; i < j; i >>= 1, j >>= 1) { if (i & 1) apply(f, i++); if (j & 1) apply(f, --j); } l = (l + n) >> __builtin_ctz(l + n); while (l >>= 1) t[l] = op(t[2 * l], t[2 * l + 1]); r = (r + n) >> __builtin_ctz(r + n); while (r >>= 1) t[r] = op(t[2 * r], t[2 * r + 1]); } void set(int i, const T &a) { push(i, i + 1); t[i += n] = a; while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]); } }; constexpr int MAXN = 200005; int n, m; vector<pair<int, ll>> byStart[MAXN]; vector<pair<int, ll>> byEnd[MAXN]; LazySeg st(MAXN); void print() { for (int i = 0; i < n; i++) { cout << st.get(i) << " "; } cout << "\n"; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int l, r; ll a; cin >> l >> r >> a; byStart[l].emplace_back(r, a); byEnd[r].emplace_back(l, a); } for (int i = 0; i <= n; i++) { st.set(i, 0); } st.build(); // take nothing ll res = 0; for (int i = 1; i <= n; i++) { // activate every interval that starts at i for (auto &pr : byStart[i]) { ll a = pr.second; st.update(0, i - 1, a); } // represents maximum score of prefix of length i if s[i] == '1' ll cur = st.query(0, i - 1); res = max(res, cur); st.update(i, i, cur); // deactivate every interval that ends at i for (auto &pr : byEnd[i]) { int l = pr.first; ll a = pr.second; st.update(0, l - 1, -a); } } cout << res << "\n"; return 0; }
replace
85
86
85
86
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> int N; int M; struct segtree { int n; long long v[500000]; long long lazy[500000]; segtree(int n_) { n = 1; while (n < n_) n *= 2; for (int i = 0; i < 2 * n - 1; ++i) { v[i] = 0; lazy[i] = 0; } } long long query(int a, int b, long long add) { return query(0, n, a, b, 0, add); } long long push(int k) { v[k] += lazy[k]; if (2 * k + 2 < 2 * n - 1) { lazy[2 * k + 1] += lazy[k]; lazy[2 * k + 2] += lazy[k]; } lazy[k] = 0; } long long query(int l, int r, int a, int b, int k, long long add) { push(k); if (r <= a || b <= l) { return -(1LL << 60); } else if (a <= l && r <= b) { lazy[k] += add; push(k); return v[k]; } else { long long vl = query(l, (l + r) / 2, a, b, 2 * k + 1, add); long long vr = query((l + r) / 2, r, a, b, 2 * k + 2, add); v[k] = std::max(v[2 * k + 1], v[2 * k + 2]); return std::max(vl, vr); } } }; int main() { std::cin >> N; std::cin >> M; std::vector<int> dst[N]; std::vector<long long> add[N]; for (int i = 0; i < M; ++i) { int l, r; long long a; std::cin >> l >> r >> a; --l; --r; dst[r].push_back(l); add[r].push_back(a); } segtree seg(N); for (int i = 0; i < N; ++i) { long long sum = 0; for (int j = 0; j < dst[i].size(); ++j) { sum += add[i][j]; } seg.query(i, i + 1, sum + std::max(0LL, seg.query(0, i, 0))); for (int j = 0; j < dst[i].size(); ++j) { seg.query(dst[i][j], i, add[i][j]); } } std::cout << std::max(0LL, seg.query(0, N, 0)) << std::endl; return 0; }
#include <bits/stdc++.h> int N; int M; struct segtree { int n; long long v[1000000]; long long lazy[1000000]; segtree(int n_) { n = 1; while (n < n_) n *= 2; for (int i = 0; i < 2 * n - 1; ++i) { v[i] = 0; lazy[i] = 0; } } long long query(int a, int b, long long add) { return query(0, n, a, b, 0, add); } long long push(int k) { v[k] += lazy[k]; if (2 * k + 2 < 2 * n - 1) { lazy[2 * k + 1] += lazy[k]; lazy[2 * k + 2] += lazy[k]; } lazy[k] = 0; } long long query(int l, int r, int a, int b, int k, long long add) { push(k); if (r <= a || b <= l) { return -(1LL << 60); } else if (a <= l && r <= b) { lazy[k] += add; push(k); return v[k]; } else { long long vl = query(l, (l + r) / 2, a, b, 2 * k + 1, add); long long vr = query((l + r) / 2, r, a, b, 2 * k + 2, add); v[k] = std::max(v[2 * k + 1], v[2 * k + 2]); return std::max(vl, vr); } } }; int main() { std::cin >> N; std::cin >> M; std::vector<int> dst[N]; std::vector<long long> add[N]; for (int i = 0; i < M; ++i) { int l, r; long long a; std::cin >> l >> r >> a; --l; --r; dst[r].push_back(l); add[r].push_back(a); } segtree seg(N); for (int i = 0; i < N; ++i) { long long sum = 0; for (int j = 0; j < dst[i].size(); ++j) { sum += add[i][j]; } seg.query(i, i + 1, sum + std::max(0LL, seg.query(0, i, 0))); for (int j = 0; j < dst[i].size(); ++j) { seg.query(dst[i][j], i, add[i][j]); } } std::cout << std::max(0LL, seg.query(0, N, 0)) << std::endl; return 0; }
replace
8
10
8
10
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mx = 2e5; vector<ll> lazy(2 * mx + 100); vector<ll> segtree(2 * mx + 100); void push(int k, int l, int r) { if (l != r) { lazy[2 * k] += lazy[k]; lazy[2 * k + 1] += lazy[k]; } segtree[k] += lazy[k]; lazy[k] = 0; } void pull(int k) { segtree[k] = max(segtree[2 * k], segtree[2 * k + 1]); } void update(int a, int b, int k, int x, int y, ll inc) { push(k, x, y); if (x > b || y < a) { return; } if (a <= x && y <= b) { lazy[k] = inc; push(k, x, y); return; } int m = (x + y) / 2; update(a, b, 2 * k, x, m, inc); update(a, b, 2 * k + 1, m + 1, y, inc); pull(k); } ll get(int a, int b, int k, int x, int y) { push(k, x, y); if (x > b || y < a) { return LLONG_MIN; } if (a <= x && y <= b) { return segtree[k]; } int m = (x + y) / 2; return max(get(a, b, 2 * k, x, m), get(a, b, 2 * k + 1, m + 1, y)); } int main() { int n, m; cin >> n >> m; vector<ll> inc[n + 1]; vector<pair<ll, ll>> rem[n + 1]; for (int q = 0; q < m; q++) { ll l, r, a; cin >> l >> r >> a; inc[l].push_back(a); rem[r].push_back({l, a}); } ll ans = 0; for (int q = 1; q <= n; q++) { for (ll w : inc[q]) { update(0, q - 1, 1, 0, n, w); } ll best = get(0, q - 1, 1, 0, n); update(q, q, 1, 0, n, best); for (pair<ll, ll> w : rem[q]) { update(0, w.first - 1, 1, 0, n, -1 * w.second); } ans = max(ans, best); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mx = 2e5; vector<ll> lazy(3 * mx + 5); vector<ll> segtree(3 * mx + 5); void push(int k, int l, int r) { if (l != r) { lazy[2 * k] += lazy[k]; lazy[2 * k + 1] += lazy[k]; } segtree[k] += lazy[k]; lazy[k] = 0; } void pull(int k) { segtree[k] = max(segtree[2 * k], segtree[2 * k + 1]); } void update(int a, int b, int k, int x, int y, ll inc) { push(k, x, y); if (x > b || y < a) { return; } if (a <= x && y <= b) { lazy[k] = inc; push(k, x, y); return; } int m = (x + y) / 2; update(a, b, 2 * k, x, m, inc); update(a, b, 2 * k + 1, m + 1, y, inc); pull(k); } ll get(int a, int b, int k, int x, int y) { push(k, x, y); if (x > b || y < a) { return LLONG_MIN; } if (a <= x && y <= b) { return segtree[k]; } int m = (x + y) / 2; return max(get(a, b, 2 * k, x, m), get(a, b, 2 * k + 1, m + 1, y)); } int main() { int n, m; cin >> n >> m; vector<ll> inc[n + 1]; vector<pair<ll, ll>> rem[n + 1]; for (int q = 0; q < m; q++) { ll l, r, a; cin >> l >> r >> a; inc[l].push_back(a); rem[r].push_back({l, a}); } ll ans = 0; for (int q = 1; q <= n; q++) { for (ll w : inc[q]) { update(0, q - 1, 1, 0, n, w); } ll best = get(0, q - 1, 1, 0, n); update(q, q, 1, 0, n, best); for (pair<ll, ll> w : rem[q]) { update(0, w.first - 1, 1, 0, n, -1 * w.second); } ans = max(ans, best); } cout << ans << "\n"; return 0; }
replace
4
6
4
6
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mx = 2e5; vector<ll> lazy(2 * mx + 5); vector<ll> segtree(2 * mx + 5); void push(int k, int l, int r) { if (l != r) { lazy[2 * k] += lazy[k]; lazy[2 * k + 1] += lazy[k]; } segtree[k] += lazy[k]; lazy[k] = 0; } void pull(int k) { segtree[k] = max(segtree[2 * k], segtree[2 * k + 1]); } void update(int a, int b, int k, int x, int y, ll inc) { push(k, x, y); if (x > b || y < a) { return; } if (a <= x && y <= b) { lazy[k] = inc; push(k, x, y); return; } int m = (x + y) / 2; update(a, b, 2 * k, x, m, inc); update(a, b, 2 * k + 1, m + 1, y, inc); pull(k); } ll get(int a, int b, int k, int x, int y) { push(k, x, y); if (x > b || y < a) { return LLONG_MIN; } if (a <= x && y <= b) { return segtree[k]; } int m = (x + y) / 2; return max(get(a, b, 2 * k, x, m), get(a, b, 2 * k + 1, m + 1, y)); } int main() { int n, m; cin >> n >> m; vector<ll> inc[n + 1]; vector<pair<ll, ll>> rem[n + 1]; for (int q = 0; q < m; q++) { ll l, r, a; cin >> l >> r >> a; inc[l].push_back(a); rem[r].push_back({l, a}); } ll ans = 0; for (int q = 1; q <= n; q++) { for (ll w : inc[q]) { update(0, q - 1, 1, 0, n, w); } ll best = get(0, q - 1, 1, 0, n); update(q, q, 1, 0, n, best); for (pair<ll, ll> w : rem[q]) { update(0, w.first - 1, 1, 0, n, -1 * w.second); } ans = max(ans, best); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mx = 2e5; vector<ll> lazy(4 * mx + 5); vector<ll> segtree(4 * mx + 5); void push(int k, int l, int r) { if (l != r) { lazy[2 * k] += lazy[k]; lazy[2 * k + 1] += lazy[k]; } segtree[k] += lazy[k]; lazy[k] = 0; } void pull(int k) { segtree[k] = max(segtree[2 * k], segtree[2 * k + 1]); } void update(int a, int b, int k, int x, int y, ll inc) { push(k, x, y); if (x > b || y < a) { return; } if (a <= x && y <= b) { lazy[k] = inc; push(k, x, y); return; } int m = (x + y) / 2; update(a, b, 2 * k, x, m, inc); update(a, b, 2 * k + 1, m + 1, y, inc); pull(k); } ll get(int a, int b, int k, int x, int y) { push(k, x, y); if (x > b || y < a) { return LLONG_MIN; } if (a <= x && y <= b) { return segtree[k]; } int m = (x + y) / 2; return max(get(a, b, 2 * k, x, m), get(a, b, 2 * k + 1, m + 1, y)); } int main() { int n, m; cin >> n >> m; vector<ll> inc[n + 1]; vector<pair<ll, ll>> rem[n + 1]; for (int q = 0; q < m; q++) { ll l, r, a; cin >> l >> r >> a; inc[l].push_back(a); rem[r].push_back({l, a}); } ll ans = 0; for (int q = 1; q <= n; q++) { for (ll w : inc[q]) { update(0, q - 1, 1, 0, n, w); } ll best = get(0, q - 1, 1, 0, n); update(q, q, 1, 0, n, best); for (pair<ll, ll> w : rem[q]) { update(0, w.first - 1, 1, 0, n, -1 * w.second); } ans = max(ans, best); } cout << ans << "\n"; return 0; }
replace
4
6
4
6
0
p03182
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fi first #define se second #define maxn 2000003 #define pb push_back using namespace std; typedef long long LL; typedef pair<int, int> pi; int n, m; LL total; LL tree[4 * maxn]; LL lazy[4 * maxn]; vector<pi> v[maxn]; void proc(int tx, int ty, int id) { if (lazy[id]) { tree[id] += lazy[id]; if (tx != ty) { lazy[2 * id] += lazy[id]; lazy[2 * id + 1] += lazy[id]; } lazy[id] = 0; } } LL query(int tx, int ty, int qx, int qy, int id) { proc(tx, ty, id); if (ty < qx || qy < tx) return LLONG_MAX; if (qx <= tx && ty <= qy) return tree[id]; int mid = (tx + ty) >> 1; LL left = query(tx, mid, qx, qy, id * 2); LL right = query(mid + 1, ty, qx, qy, id * 2 + 1); return min(left, right); } LL update(int tx, int ty, int qx, int qy, LL val, int id) { proc(tx, ty, id); if (ty < qx || qy < tx) return tree[id]; if (tx == ty) { lazy[id] = val; proc(tx, ty, id); return tree[id]; } int mid = (tx + ty) >> 1; LL left = update(tx, mid, qx, qy, val, id * 2); LL right = update(mid + 1, ty, qx, qy, val, id * 2 + 1); return tree[id] = min(left, right); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int l, r, val; scanf("%d%d%d", &l, &r, &val); total += val; v[r].pb(pi(l, val)); } for (int i = 1; i <= n; i++) { LL val = query(0, n, 0, i - 1, 1); update(0, n, i, i, val, 1); for (int j = 0; j < (int)v[i].size(); j++) update(0, n, 0, v[i][j].fi - 1, v[i][j].se, 1); } printf("%lld\n", total - query(0, n, 0, n, 1)); return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define maxn 2000003 #define pb push_back using namespace std; typedef long long LL; typedef pair<int, int> pi; int n, m; LL total; LL tree[4 * maxn]; LL lazy[4 * maxn]; vector<pi> v[maxn]; void proc(int tx, int ty, int id) { if (lazy[id]) { tree[id] += lazy[id]; if (tx != ty) { lazy[2 * id] += lazy[id]; lazy[2 * id + 1] += lazy[id]; } lazy[id] = 0; } } LL query(int tx, int ty, int qx, int qy, int id) { proc(tx, ty, id); if (ty < qx || qy < tx) return LLONG_MAX; if (qx <= tx && ty <= qy) return tree[id]; int mid = (tx + ty) >> 1; LL left = query(tx, mid, qx, qy, id * 2); LL right = query(mid + 1, ty, qx, qy, id * 2 + 1); return min(left, right); } LL update(int tx, int ty, int qx, int qy, LL val, int id) { proc(tx, ty, id); if (ty < qx || qy < tx) return tree[id]; if (qx <= tx && ty <= qy) { lazy[id] = val; proc(tx, ty, id); return tree[id]; } int mid = (tx + ty) >> 1; LL left = update(tx, mid, qx, qy, val, id * 2); LL right = update(mid + 1, ty, qx, qy, val, id * 2 + 1); return tree[id] = min(left, right); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int l, r, val; scanf("%d%d%d", &l, &r, &val); total += val; v[r].pb(pi(l, val)); } for (int i = 1; i <= n; i++) { LL val = query(0, n, 0, i - 1, 1); update(0, n, i, i, val, 1); for (int j = 0; j < (int)v[i].size(); j++) update(0, n, 0, v[i][j].fi - 1, v[i][j].se, 1); } printf("%lld\n", total - query(0, n, 0, n, 1)); return 0; }
replace
42
43
42
43
TLE
p03182
C++
Runtime Error
#include <bits/stdc++.h> #define fir first #define sec second #define MAX_N 100000 #define mid ((s + t) >> 1) using namespace std; typedef long long lnt; typedef pair<int, int> pii; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f * (c - '0'); } int n, m; vector<pii> p[MAX_N + 5]; lnt tr[(MAX_N << 2) + 5], tag[(MAX_N << 2) + 5]; void modify(int v, int s, int t, int l, int r, lnt x) { if (s >= l && t <= r) { tr[v] += x, tag[v] += x; return; } if (l <= mid) modify(v << 1, s, mid, l, r, x); if (r >= mid + 1) modify(v << 1 | 1, mid + 1, t, l, r, x); tr[v] = max(tr[v << 1], tr[v << 1 | 1]) + tag[v]; } int main() { read(n), read(m); for (int i = 1, l, r, c; i <= m; i++) read(l), read(r), read(c), p[r].push_back(pii(l, c)); for (int i = 1; i <= n; i++) { modify(1, 1, n, i, i, tr[1]); for (pii q : p[i]) modify(1, 1, n, q.fir, i, q.sec); } return printf("%lld\n", max(tr[1], 0LL)), 0; }
#include <bits/stdc++.h> #define fir first #define sec second #define MAX_N 200000 #define mid ((s + t) >> 1) using namespace std; typedef long long lnt; typedef pair<int, int> pii; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f * (c - '0'); } int n, m; vector<pii> p[MAX_N + 5]; lnt tr[(MAX_N << 2) + 5], tag[(MAX_N << 2) + 5]; void modify(int v, int s, int t, int l, int r, lnt x) { if (s >= l && t <= r) { tr[v] += x, tag[v] += x; return; } if (l <= mid) modify(v << 1, s, mid, l, r, x); if (r >= mid + 1) modify(v << 1 | 1, mid + 1, t, l, r, x); tr[v] = max(tr[v << 1], tr[v << 1 | 1]) + tag[v]; } int main() { read(n), read(m); for (int i = 1, l, r, c; i <= m; i++) read(l), read(r), read(c), p[r].push_back(pii(l, c)); for (int i = 1; i <= n; i++) { modify(1, 1, n, i, i, tr[1]); for (pii q : p[i]) modify(1, 1, n, q.fir, i, q.sec); } return printf("%lld\n", max(tr[1], 0LL)), 0; }
replace
3
4
3
4
0
p03182
C++
Time Limit Exceeded
#pragma region Macros #pragma GCC optimize("O3") #include <bits/stdc++.h> #define ll long long #define ld long double #define rep2(i, a, b) for (ll i = a; i <= b; ++i) #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, a, b) for (ll i = a; i >= b; i--) #define pii pair<int, int> #define pll pair<ll, ll> #define pq priority_queue #define pb push_back #define eb emplace_back #define vec vector<int> #define vecll vector<ll> #define vecpii vector<pii> #define vec2(a, b) vector<vec>(a, vec(b)) #define vec2ll(a, b) vector<vecll>(a, vecll(b)) #define vec3(a, b, c) vector<vector<vec>>(a, vec2(b, c)) #define vec3ll(a, b, c) vector<vector<vecll>>(a, vec2ll(b, c)) #define fi first #define se second #define endl "\n" #define all(c) begin(c), end(c) #define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define lb(c, x) distance(c.begin(), lower_bound(all(c), (x))) #define ub(c, x) distance(c.begin(), upper_bound(all(c), (x))) using namespace std; int in() { int x; cin >> x; return x; } ll lin() { ll x; cin >> x; return x; } string stin() { string s; cin >> s; return s; } 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; } vec iota(int n) { vec a(n); iota(all(a), 0); return a; } void print() { putchar(' '); } void print(bool a) { cout << a; } void print(int a) { cout << a; } void print(long long a) { cout << a; } void print(char a) { cout << a; } void print(string &a) { cout << a; } void print(double a) { cout << a; } template <class T> void print(const vector<T> &); template <class T, size_t size> void print(const array<T, size> &); template <class T, class L> void print(const pair<T, L> &p); template <class T, size_t size> void print(const T (&)[size]); template <class T> void print(const vector<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } } template <class T> void print(const deque<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } } template <class T, size_t size> void print(const array<T, size> &a) { print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } } template <class T, class L> void print(const pair<T, L> &p) { cout << '('; print(p.first); cout << ","; print(p.second); cout << ')'; } template <class T, size_t size> void print(const T (&a)[size]) { print(a[0]); for (auto i = a; ++i != end(a);) { cout << " "; print(*i); } } template <class T> void print(const T &a) { cout << a; } int out() { putchar('\n'); return 0; } template <class T> int out(const T &t) { print(t); putchar('\n'); return 0; } template <class Head, class... Tail> int out(const Head &head, const Tail &...tail) { print(head); putchar(' '); out(tail...); return 0; } ll gcd(ll a, ll b) { while (b) { ll c = b; b = a % b; a = c; } return a; } ll lcm(ll a, ll b) { if (!a || !b) return 0; return a * b / gcd(a, b); } #define _GLIBCXX_DEBU #ifdef _MY_DEBUG #undef endl #define debug(x) cout << #x << ": " << x << endl void err() {} template <class T> void err(const T &t) { print(t); cout << " "; } template <class Head, class... Tail> void err(const Head &head, const Tail &...tail) { print(head); putchar(' '); out(tail...); } #else #define debug(x) template <class... T> void err(const T &...) {} #endif #pragma endregion template <typename T, typename E> struct LazySegmentTree { typedef function<T(T, T)> F; typedef function<T(T, E)> G; typedef function<E(E, E)> H; typedef function<E(E, int)> P; int n; F f; G g; H h; P p; T d1; E d0; vector<T> dat; vector<E> laz; LazySegmentTree( int n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(), P p = [](E a, int b) { return a; }) : f(f), g(g), h(h), d1(d1), d0(d0), p(p) { init(n_); if (n_ == (int)v.size()) build(n_, v); } void init(int n_) { n = 1; while (n < n_) n *= 2; dat.clear(); dat.resize(2 * n - 1, d1); laz.clear(); laz.resize(2 * n - 1, d0); } void build(int n_, vector<T> v) { for (int i = 0; i < n_; i++) dat[i + n - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } inline void eval(int len, int k) { if (laz[k] == d0) return; if (k * 2 + 1 < n * 2 - 1) { laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]); laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]); } dat[k] = g(dat[k], p(laz[k], len)); laz[k] = d0; } T update(int a, int b, E x, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return dat[k]; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return g(dat[k], p(laz[k], r - l)); } return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2), update(a, b, x, k * 2 + 2, (l + r) / 2, r)); } T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); } T query(int a, int b, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return d1; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } T query(int a, int b) { return query(a, b, 0, 0, n); } }; constexpr int N = 301000; // 区間加算区間和 signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); int n = in(), m = in(); vector<vecpii> a(n + 2); vecll r(n + 2); LazySegmentTree<ll, ll> seg( N, [](ll x, ll y) { return max(x, y); }, plus<ll>(), plus<ll>(), -LONG_LONG_MAX, 0); seg.build(n + 2, vecll(n + 2)); rep(i, m) { int x = in(), y = in(); ll c = in(); seg.update(x, y + 1, -c); a[y + 1].eb(x, c); r[x] += c; r[y + 1] -= c; } rep(i, n + 1) r[i + 1] += r[i]; vecll dp(n + 1); rep2(i, 1, n) { for (auto e : a[i]) { seg.update(e.fi, i, e.se); } rep2(i, 1, n) err(seg.query(i, i + 1)); dp[i] = r[i] + seg.query(0, i); seg.update(i, i + 1, dp[i]); } cout << *max_element(all(dp)) << endl; }
#pragma region Macros #pragma GCC optimize("O3") #include <bits/stdc++.h> #define ll long long #define ld long double #define rep2(i, a, b) for (ll i = a; i <= b; ++i) #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, a, b) for (ll i = a; i >= b; i--) #define pii pair<int, int> #define pll pair<ll, ll> #define pq priority_queue #define pb push_back #define eb emplace_back #define vec vector<int> #define vecll vector<ll> #define vecpii vector<pii> #define vec2(a, b) vector<vec>(a, vec(b)) #define vec2ll(a, b) vector<vecll>(a, vecll(b)) #define vec3(a, b, c) vector<vector<vec>>(a, vec2(b, c)) #define vec3ll(a, b, c) vector<vector<vecll>>(a, vec2ll(b, c)) #define fi first #define se second #define endl "\n" #define all(c) begin(c), end(c) #define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define lb(c, x) distance(c.begin(), lower_bound(all(c), (x))) #define ub(c, x) distance(c.begin(), upper_bound(all(c), (x))) using namespace std; int in() { int x; cin >> x; return x; } ll lin() { ll x; cin >> x; return x; } string stin() { string s; cin >> s; return s; } 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; } vec iota(int n) { vec a(n); iota(all(a), 0); return a; } void print() { putchar(' '); } void print(bool a) { cout << a; } void print(int a) { cout << a; } void print(long long a) { cout << a; } void print(char a) { cout << a; } void print(string &a) { cout << a; } void print(double a) { cout << a; } template <class T> void print(const vector<T> &); template <class T, size_t size> void print(const array<T, size> &); template <class T, class L> void print(const pair<T, L> &p); template <class T, size_t size> void print(const T (&)[size]); template <class T> void print(const vector<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } } template <class T> void print(const deque<T> &a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } } template <class T, size_t size> void print(const array<T, size> &a) { print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } } template <class T, class L> void print(const pair<T, L> &p) { cout << '('; print(p.first); cout << ","; print(p.second); cout << ')'; } template <class T, size_t size> void print(const T (&a)[size]) { print(a[0]); for (auto i = a; ++i != end(a);) { cout << " "; print(*i); } } template <class T> void print(const T &a) { cout << a; } int out() { putchar('\n'); return 0; } template <class T> int out(const T &t) { print(t); putchar('\n'); return 0; } template <class Head, class... Tail> int out(const Head &head, const Tail &...tail) { print(head); putchar(' '); out(tail...); return 0; } ll gcd(ll a, ll b) { while (b) { ll c = b; b = a % b; a = c; } return a; } ll lcm(ll a, ll b) { if (!a || !b) return 0; return a * b / gcd(a, b); } #define _GLIBCXX_DEBU #ifdef _MY_DEBUG #undef endl #define debug(x) cout << #x << ": " << x << endl void err() {} template <class T> void err(const T &t) { print(t); cout << " "; } template <class Head, class... Tail> void err(const Head &head, const Tail &...tail) { print(head); putchar(' '); out(tail...); } #else #define debug(x) template <class... T> void err(const T &...) {} #endif #pragma endregion template <typename T, typename E> struct LazySegmentTree { typedef function<T(T, T)> F; typedef function<T(T, E)> G; typedef function<E(E, E)> H; typedef function<E(E, int)> P; int n; F f; G g; H h; P p; T d1; E d0; vector<T> dat; vector<E> laz; LazySegmentTree( int n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(), P p = [](E a, int b) { return a; }) : f(f), g(g), h(h), d1(d1), d0(d0), p(p) { init(n_); if (n_ == (int)v.size()) build(n_, v); } void init(int n_) { n = 1; while (n < n_) n *= 2; dat.clear(); dat.resize(2 * n - 1, d1); laz.clear(); laz.resize(2 * n - 1, d0); } void build(int n_, vector<T> v) { for (int i = 0; i < n_; i++) dat[i + n - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } inline void eval(int len, int k) { if (laz[k] == d0) return; if (k * 2 + 1 < n * 2 - 1) { laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]); laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]); } dat[k] = g(dat[k], p(laz[k], len)); laz[k] = d0; } T update(int a, int b, E x, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return dat[k]; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return g(dat[k], p(laz[k], r - l)); } return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2), update(a, b, x, k * 2 + 2, (l + r) / 2, r)); } T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); } T query(int a, int b, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return d1; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } T query(int a, int b) { return query(a, b, 0, 0, n); } }; constexpr int N = 301000; // 区間加算区間和 signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); int n = in(), m = in(); vector<vecpii> a(n + 2); vecll r(n + 2); LazySegmentTree<ll, ll> seg( N, [](ll x, ll y) { return max(x, y); }, plus<ll>(), plus<ll>(), -LONG_LONG_MAX, 0); seg.build(n + 2, vecll(n + 2)); rep(i, m) { int x = in(), y = in(); ll c = in(); seg.update(x, y + 1, -c); a[y + 1].eb(x, c); r[x] += c; r[y + 1] -= c; } rep(i, n + 1) r[i + 1] += r[i]; vecll dp(n + 1); rep2(i, 1, n) { for (auto e : a[i]) { seg.update(e.fi, i, e.se); } dp[i] = r[i] + seg.query(0, i); seg.update(i, i + 1, dp[i]); } cout << *max_element(all(dp)) << endl; }
delete
263
264
263
263
TLE
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define int long long #define ar array<int, 2> #define inf 1000000000000000000 int mod = 1e9 + 7; int min(int a, int b) { return (a < b) ? a : b; } int max(int a, int b) { return (a > b) ? a : b; } int fp(int a, int b) { if (b == 0) return 1; int x = fp(a, b / 2); x = (x * x) % mod; if (b & 1) x = (x * a) % mod; return x; } const int N = 1e5 + 5; struct node { int p, mx; node() { p = 0; mx = 0; } }; node merge(node a, node b) { node c; return c; } vector<node> bit(4 * N); void propagate(int id) { bit[2 * id].p += bit[id].p; bit[2 * id + 1].p += bit[id].p; bit[2 * id].mx += bit[id].p; bit[2 * id + 1].mx += bit[id].p; bit[id].p = 0; } int get(int id, int l, int r, int L, int R) { if (R < l || r < L) { return INT_MIN; } if (L <= l && r <= R) { return bit[id].mx; } propagate(id); int mid = (l + r) / 2; int ans = max(get(2 * id, l, mid, L, R), get(2 * id + 1, mid + 1, r, L, R)); bit[id].mx = max(bit[2 * id].mx, bit[2 * id + 1].mx); return ans; } void upd(int id, int l, int r, int L, int R, int v) { if (R < l || r < L) return; if (L <= l && r <= R) { bit[id].p += v; bit[id].mx += v; return; } propagate(id); int mid = (l + r) / 2; upd(2 * id, l, mid, L, R, v); upd(2 * id + 1, mid + 1, r, L, R, v); bit[id].mx = max(bit[2 * id].mx, bit[2 * id + 1].mx); // bit[id]=merge(bit[2*id],bit[2*id+1]); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; int a[m + 1][3]; vector<int> v[n + 1]; for (int i = 1; i <= m; i++) { cin >> a[i][0] >> a[i][1] >> a[i][2]; v[a[i][1]].push_back(i); } for (int i = 1; i <= n; i++) { int x = max(0, get(1, 1, n, 1, n - 1)); upd(1, 1, n, i, i, x); for (auto j : v[i]) { upd(1, 1, n, a[j][0], a[j][1], a[j][2]); } } // for(int i=1;i<=n;i++) // { // cout<<i<<" "<<get(1,1,n,1,i)<<endl; // } int ans = max(0, get(1, 1, n, 1, n)); cout << ans; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define int long long #define ar array<int, 2> #define inf 1000000000000000000 int mod = 1e9 + 7; int min(int a, int b) { return (a < b) ? a : b; } int max(int a, int b) { return (a > b) ? a : b; } int fp(int a, int b) { if (b == 0) return 1; int x = fp(a, b / 2); x = (x * x) % mod; if (b & 1) x = (x * a) % mod; return x; } const int N = 2e5 + 5; struct node { int p, mx; node() { p = 0; mx = 0; } }; node merge(node a, node b) { node c; return c; } vector<node> bit(4 * N); void propagate(int id) { bit[2 * id].p += bit[id].p; bit[2 * id + 1].p += bit[id].p; bit[2 * id].mx += bit[id].p; bit[2 * id + 1].mx += bit[id].p; bit[id].p = 0; } int get(int id, int l, int r, int L, int R) { if (R < l || r < L) { return INT_MIN; } if (L <= l && r <= R) { return bit[id].mx; } propagate(id); int mid = (l + r) / 2; int ans = max(get(2 * id, l, mid, L, R), get(2 * id + 1, mid + 1, r, L, R)); bit[id].mx = max(bit[2 * id].mx, bit[2 * id + 1].mx); return ans; } void upd(int id, int l, int r, int L, int R, int v) { if (R < l || r < L) return; if (L <= l && r <= R) { bit[id].p += v; bit[id].mx += v; return; } propagate(id); int mid = (l + r) / 2; upd(2 * id, l, mid, L, R, v); upd(2 * id + 1, mid + 1, r, L, R, v); bit[id].mx = max(bit[2 * id].mx, bit[2 * id + 1].mx); // bit[id]=merge(bit[2*id],bit[2*id+1]); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; int a[m + 1][3]; vector<int> v[n + 1]; for (int i = 1; i <= m; i++) { cin >> a[i][0] >> a[i][1] >> a[i][2]; v[a[i][1]].push_back(i); } for (int i = 1; i <= n; i++) { int x = max(0, get(1, 1, n, 1, n - 1)); upd(1, 1, n, i, i, x); for (auto j : v[i]) { upd(1, 1, n, a[j][0], a[j][1], a[j][2]); } } // for(int i=1;i<=n;i++) // { // cout<<i<<" "<<get(1,1,n,1,i)<<endl; // } int ans = max(0, get(1, 1, n, 1, n)); cout << ans; }
replace
19
20
19
20
0
p03182
C++
Time Limit Exceeded
// #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC optimize(3) // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("sse3","sse2","sse") // #pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3") // #pragma GCC target("f16c") // #pragma GCC // optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma // GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error // "-fcse-skip-blocks" #pragma GCC diagnostic error // "-funsafe-loop-optimizations" #pragma GCC diagnostic error "-std=c++14" #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x, "r", stdin) #define fw(x) freopen(x, "w", stdout) #define iout(x) printf("%d\n", x) #define lout(x) printf("%lld\n", x) #define REP(x, l, u) for (ll x = l; x < u; x++) #define RREP(x, l, u) for (ll x = l; x >= u; x--) #define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end()) #define mst(x, a) memset(x, a, sizeof(x)) #define all(a) begin(a), end(a) #define PII pair<int, int> #define PLL pair<ll, ll> #define MP make_pair #define lowbit(x) ((x) & (-(x))) #define lson (ind << 1) #define rson (ind << 1 | 1) #define se second #define fi first #define sz(x) ((int)x.size()) #define EX0 exit(0); typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace __gnu_pbds; // required using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef vector<ll> VLL; typedef vector<int> VI; const int block_size = 320; typedef complex<ll> point; const ll mod = 1e9 + 7; const ll inf = 1e9 + 7; const ld eps = 1e-9; const db PI = atan(1) * 4; template <typename T> inline int sign(const T &a) { if (a < 0) return -1; if (a > 0) return 1; return 0; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) \ {} #endif template <typename T, typename S> inline bool upmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> inline bool upmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline void in(T &x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } ull twop(ll x) { return 1ULL << x; } ll MOD(ll a, ll m) { a %= m; if (a < 0) a += m; return a; } ll inverse(ll a, ll m) { a = MOD(a, m); if (a <= 1) return a; return MOD((1 - inverse(m, a) * m) / a, m); } template <typename A, typename B> inline void in(A &x, B &y) { in(x); in(y); } template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) { in(x); in(y); in(z); } template <typename A, typename B, typename C, typename D> inline void in(A &x, B &y, C &z, D &d) { in(x); in(y); in(z); in(d); } template <typename T> T sqr(T x) { return x * x; } ll gcd(ll a, ll b) { while (b != 0) { a %= b; swap(a, b); } return abs(a); } ll fast(ll a, ll b, ll mod) { if (b < 0) a = inverse(a, mod), b = -b; ll ans = 1; while (b) { if (b & 1) { b--; ans = ans * a % mod; } else { a = a * a % mod; b /= 2; } } return ans % mod; } struct SegTree { static const int maxn = 200010; struct node { int l, r; ll max, lazy; ll get_max() { return max + lazy; } }; node no[maxn * 4]; void push_up(int ind) { no[ind].max = max(no[lson].get_max(), no[rson].get_max()); } void push_down(int ind) { no[lson].lazy += no[ind].lazy; no[rson].lazy += no[ind].lazy; no[ind].lazy = 0; } void build(int l, int r, int ind) { no[ind].l = l; no[ind].r = r; no[ind].lazy = no[ind].max = 0; if (l == r) { } else { int mid = (l + r) / 2; build(l, mid, lson); build(mid + 1, r, rson); push_up(ind); } } void update(int l, int r, int ind, ll val) { if (l > no[ind].r || r < no[ind].l) return; if (l <= no[ind].l && no[ind].r <= r) { no[ind].lazy += val; } else { push_down(ind); update(l, r, lson, val); update(l, r, rson, val); push_up(ind); } } void query(int l, int r, int ind, ll &ans) { if (l > no[ind].r || r < no[ind].l) return; if (l <= no[ind].l && no[ind].r <= r) { upmax(ans, no[ind].get_max()); } else { push_down(ind); query(l, r, lson, ans); query(l, r, rson, ans); push_up(ind); } } }; namespace SOLVE { const int maxn = 200010; int n, m; SegTree dp, sum; vector<pair<PII, int>> start[maxn], finish[maxn]; void main() { in(n, m); dp.build(0, n, 1); sum = dp; for (int i = 1; i <= m; i++) { /* sum 维护 区间sum dp 维护 区间max */ int l, r, a; cin >> l >> r >> a; sum.update(l, r, 1, a); /* 在 l 加入线段 在 r+1 移走线段 */ start[l].push_back({{l, r}, a}); finish[r + 1].push_back({{l, r}, a}); } long long ans = 0; for (int i = 1; i <= n; i++) { // 加入 for (auto interval : start[i]) { int l, r, a = interval.second; tie(l, r) = interval.first; dp.update(l, r, 1, -a); } // 移走 for (auto interval : finish[i]) { int l, r, a = interval.second; tie(l, r) = interval.first; dp.update(l, r, 1, a); } // 求 sum[i] long long s = LLONG_MIN; sum.query(i, i, 1, s); // 求dp[0..i-1]的最优值 long long max = LLONG_MIN; dp.query(0, i - 1, 1, max); // 更新 dp[i] dp.update(i, i, 1, max + s); if (max + s > ans) ans = max + s; } cout << ans << endl; } } // namespace SOLVE signed main() { #ifndef ONLINE_JUDGE fr("/Users/zhangqingchuan/Desktop/cp/cp/input.txt"); fw("/Users/zhangqingchuan/Desktop/cp/cp/output.txt"); #endif int t = 1; // in(t); for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<":"; SOLVE::main(); } // clock_t st = clock(); // while(clock() - st < 3.0 * CLOCKS_PER_SEC){ // // } return 0; }
// #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC optimize(3) // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("sse3","sse2","sse") // #pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3") // #pragma GCC target("f16c") // #pragma GCC // optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma // GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error // "-fcse-skip-blocks" #pragma GCC diagnostic error // "-funsafe-loop-optimizations" #pragma GCC diagnostic error "-std=c++14" #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x, "r", stdin) #define fw(x) freopen(x, "w", stdout) #define iout(x) printf("%d\n", x) #define lout(x) printf("%lld\n", x) #define REP(x, l, u) for (ll x = l; x < u; x++) #define RREP(x, l, u) for (ll x = l; x >= u; x--) #define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end()) #define mst(x, a) memset(x, a, sizeof(x)) #define all(a) begin(a), end(a) #define PII pair<int, int> #define PLL pair<ll, ll> #define MP make_pair #define lowbit(x) ((x) & (-(x))) #define lson (ind << 1) #define rson (ind << 1 | 1) #define se second #define fi first #define sz(x) ((int)x.size()) #define EX0 exit(0); typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace __gnu_pbds; // required using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef vector<ll> VLL; typedef vector<int> VI; const int block_size = 320; typedef complex<ll> point; const ll mod = 1e9 + 7; const ll inf = 1e9 + 7; const ld eps = 1e-9; const db PI = atan(1) * 4; template <typename T> inline int sign(const T &a) { if (a < 0) return -1; if (a > 0) return 1; return 0; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) \ {} #endif template <typename T, typename S> inline bool upmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> inline bool upmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline void in(T &x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } ull twop(ll x) { return 1ULL << x; } ll MOD(ll a, ll m) { a %= m; if (a < 0) a += m; return a; } ll inverse(ll a, ll m) { a = MOD(a, m); if (a <= 1) return a; return MOD((1 - inverse(m, a) * m) / a, m); } template <typename A, typename B> inline void in(A &x, B &y) { in(x); in(y); } template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) { in(x); in(y); in(z); } template <typename A, typename B, typename C, typename D> inline void in(A &x, B &y, C &z, D &d) { in(x); in(y); in(z); in(d); } template <typename T> T sqr(T x) { return x * x; } ll gcd(ll a, ll b) { while (b != 0) { a %= b; swap(a, b); } return abs(a); } ll fast(ll a, ll b, ll mod) { if (b < 0) a = inverse(a, mod), b = -b; ll ans = 1; while (b) { if (b & 1) { b--; ans = ans * a % mod; } else { a = a * a % mod; b /= 2; } } return ans % mod; } struct SegTree { static const int maxn = 200010; struct node { int l, r; ll max, lazy; ll get_max() { return max + lazy; } }; node no[maxn * 4]; void push_up(int ind) { no[ind].max = max(no[lson].get_max(), no[rson].get_max()); } void push_down(int ind) { no[lson].lazy += no[ind].lazy; no[rson].lazy += no[ind].lazy; no[ind].lazy = 0; } void build(int l, int r, int ind) { no[ind].l = l; no[ind].r = r; no[ind].lazy = no[ind].max = 0; if (l == r) { } else { int mid = (l + r) / 2; build(l, mid, lson); build(mid + 1, r, rson); push_up(ind); } } void update(int l, int r, int ind, ll val) { if (l > no[ind].r || r < no[ind].l) return; if (l <= no[ind].l && no[ind].r <= r) { no[ind].lazy += val; } else { push_down(ind); update(l, r, lson, val); update(l, r, rson, val); push_up(ind); } } void query(int l, int r, int ind, ll &ans) { if (l > no[ind].r || r < no[ind].l) return; if (l <= no[ind].l && no[ind].r <= r) { upmax(ans, no[ind].get_max()); } else { push_down(ind); query(l, r, lson, ans); query(l, r, rson, ans); push_up(ind); } } }; namespace SOLVE { const int maxn = 200010; int n, m; SegTree dp, sum; vector<pair<PII, int>> start[maxn], finish[maxn]; void main() { in(n, m); dp.build(0, n, 1); sum = dp; for (int i = 1; i <= m; i++) { /* sum 维护 区间sum dp 维护 区间max */ int l, r, a; cin >> l >> r >> a; sum.update(l, r, 1, a); /* 在 l 加入线段 在 r+1 移走线段 */ start[l].push_back({{l, r}, a}); finish[r + 1].push_back({{l, r}, a}); } long long ans = 0; for (int i = 1; i <= n; i++) { // 加入 for (auto interval : start[i]) { int l, r, a = interval.second; tie(l, r) = interval.first; dp.update(l, r, 1, -a); } // 移走 for (auto interval : finish[i]) { int l, r, a = interval.second; tie(l, r) = interval.first; dp.update(l, r, 1, a); } // 求 sum[i] long long s = LLONG_MIN; sum.query(i, i, 1, s); // 求dp[0..i-1]的最优值 long long max = LLONG_MIN; dp.query(0, i - 1, 1, max); // 更新 dp[i] dp.update(i, i, 1, max + s); if (max + s > ans) ans = max + s; } cout << ans << endl; } } // namespace SOLVE signed main() { #ifndef ONLINE_JUDGE #endif int t = 1; // in(t); for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<":"; SOLVE::main(); } // clock_t st = clock(); // while(clock() - st < 3.0 * CLOCKS_PER_SEC){ // // } return 0; }
delete
312
314
312
312
TLE
p03182
C++
Runtime Error
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> std::pair<int, int> DR[] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {1, 1}, {1, -1}}; #define ll long long #define clock (clock() * 1000.0 / CLOCKS_PER_SEC) #define rc(s) return cout << s, 0 #define rcg(s) \ cout << s; \ exit(0) #define _ \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cerr.tie(0); \ cout.tie(0); #define db(x) cerr << #x << " = " << x << '\n' #define pb push_back #define mp make_pair #define all(s) s.begin(), s.end() #define sz(x) (int)((x).size()) #define int ll using namespace std; int gcd(int a, int b) { if (b) return gcd(b, a % b); return a; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, m, a[1 << 18], t[1 << 21], lz[1 << 21]; vector<pair<int, int>> vec[1 << 18]; int ans; void push(int nod) { t[nod] += lz[nod]; lz[nod << 1] += lz[nod]; lz[nod << 1 | 1] += lz[nod]; lz[nod] = 0; } void upd(int nod, int tl, int tr, int l, int r, int val) { if (l > r) return; push(nod); if (tl == l && tr == r) { lz[nod] += val; push(nod); return; } int mid = tl + tr >> 1; upd(nod << 1, tl, mid, l, min(mid, r), val); upd(nod << 1 | 1, mid + 1, tr, max(mid + 1, l), r, val); t[nod] = max(t[nod << 1] + lz[nod << 1], t[nod << 1 | 1] + lz[nod << 1 | 1]); } void upd2(int nod, int l, int r, int pos, int val) { if (l == r) { t[nod] = val; return; } int mid = l + r >> 1; if (pos <= mid) { upd2(nod << 1, l, mid, pos, val); } else upd2(nod << 1 | 1, mid + 1, r, pos, val); t[nod] = max(t[nod << 1], t[nod << 1 | 1]); } int qry(int nod, int tl, int tr, int l, int r) { if (l > r) return -1e18; push(nod); if (l == tl && r == tr) return t[nod]; int mid = tl + tr >> 1; return max(qry(nod << 1, tl, mid, l, min(mid, r)), qry(nod << 1 | 1, mid + 1, tr, max(mid + 1, l), r)); } int32_t main() { _ // freopen("in","r",stdin); cin >> n >> m; for (int i = 1; i <= 4 * n; i++) t[i] = -1e18; while (m--) { int x, y, z; cin >> x >> y >> z; a[x] += z; a[y + 1] -= z; vec[y + 1].pb(mp(x, z)); } for (int i = 1; i <= n; i++) { a[i] += a[i - 1]; for (auto it : vec[i]) { upd(1, 1, n, it.first, i - 1, it.second); } upd2(1, 1, n, i, 0); int x = a[i] + qry(1, 1, n, 1, i); ans = max(ans, x); upd2(1, 1, n, i, x - a[i]); } rc(ans); }
#pragma GCC optimize("Ofast") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> std::pair<int, int> DR[] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {1, 1}, {1, -1}}; #define ll long long #define clock (clock() * 1000.0 / CLOCKS_PER_SEC) #define rc(s) return cout << s, 0 #define rcg(s) \ cout << s; \ exit(0) #define _ \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cerr.tie(0); \ cout.tie(0); #define db(x) cerr << #x << " = " << x << '\n' #define pb push_back #define mp make_pair #define all(s) s.begin(), s.end() #define sz(x) (int)((x).size()) #define int ll using namespace std; int gcd(int a, int b) { if (b) return gcd(b, a % b); return a; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, m, a[1 << 18], t[1 << 21], lz[1 << 21]; vector<pair<int, int>> vec[1 << 18]; int ans; void push(int nod) { t[nod] += lz[nod]; lz[nod << 1] += lz[nod]; lz[nod << 1 | 1] += lz[nod]; lz[nod] = 0; } void upd(int nod, int tl, int tr, int l, int r, int val) { if (l > r) return; push(nod); if (tl == l && tr == r) { lz[nod] += val; push(nod); return; } int mid = tl + tr >> 1; upd(nod << 1, tl, mid, l, min(mid, r), val); upd(nod << 1 | 1, mid + 1, tr, max(mid + 1, l), r, val); t[nod] = max(t[nod << 1] + lz[nod << 1], t[nod << 1 | 1] + lz[nod << 1 | 1]); } void upd2(int nod, int l, int r, int pos, int val) { if (l == r) { t[nod] = val; return; } int mid = l + r >> 1; if (pos <= mid) { upd2(nod << 1, l, mid, pos, val); } else upd2(nod << 1 | 1, mid + 1, r, pos, val); t[nod] = max(t[nod << 1], t[nod << 1 | 1]); } int qry(int nod, int tl, int tr, int l, int r) { if (l > r) return -1e18; push(nod); if (l == tl && r == tr) return t[nod]; int mid = tl + tr >> 1; return max(qry(nod << 1, tl, mid, l, min(mid, r)), qry(nod << 1 | 1, mid + 1, tr, max(mid + 1, l), r)); } int32_t main() { _ // freopen("in","r",stdin); cin >> n >> m; for (int i = 1; i <= 4 * n; i++) t[i] = -1e18; while (m--) { int x, y, z; cin >> x >> y >> z; a[x] += z; a[y + 1] -= z; vec[y + 1].pb(mp(x, z)); } for (int i = 1; i <= n; i++) { a[i] += a[i - 1]; for (auto it : vec[i]) { upd(1, 1, n, it.first, i - 1, it.second); } upd2(1, 1, n, i, 0); int x = a[i] + qry(1, 1, n, 1, i); ans = max(ans, x); upd2(1, 1, n, i, x - a[i]); } rc(ans); }
delete
1
2
1
1
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define ll long long #define N 100010 using namespace std; struct query { ll l, r, v; } a[N]; ll n, m, cnt, t[N << 2], lt[N << 2]; bool cmp(query a, query b) { return a.r < b.r; } void push(int x) { t[x << 1] += lt[x]; lt[x << 1] += lt[x]; t[x << 1 | 1] += lt[x]; lt[x << 1 | 1] += lt[x]; lt[x] = 0; } void update(int x, int l, int r, int a, int b, ll v) { if (b < l || a > r) return; if (l == a && r == b) { t[x] += v; lt[x] += v; return; } push(x); int m = (l + r) / 2; update(2 * x, l, m, a, min(b, m), v); update(2 * x + 1, m + 1, r, max(a, m + 1), b, v); t[x] = max(t[2 * x], t[2 * x + 1]); } ll query(int x, int l, int r, int a, int b) { if (b < l || a > r) return 0; if (l == a && r == b) return t[x]; push(x); int m = (l + r) / 2; return max(query(2 * x, l, m, a, min(b, m)), query(2 * x + 1, m + 1, r, max(a, m + 1), b)); } int main() { cin >> n >> m; FOR(i, 0, m) cin >> a[i].l >> a[i].r >> a[i].v; sort(a, a + m, cmp); FOR(i, 1, n + 1) { update(1, 1, n, i, i, query(1, 1, n, 1, i)); while (a[cnt].r == i) { update(1, 1, n, a[cnt].l, a[cnt].r, a[cnt].v); cnt++; } } cout << max(t[1], 0ll) << '\n'; return 0; }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define ll long long #define N 200010 using namespace std; struct query { ll l, r, v; } a[N]; ll n, m, cnt, t[N << 2], lt[N << 2]; bool cmp(query a, query b) { return a.r < b.r; } void push(int x) { t[x << 1] += lt[x]; lt[x << 1] += lt[x]; t[x << 1 | 1] += lt[x]; lt[x << 1 | 1] += lt[x]; lt[x] = 0; } void update(int x, int l, int r, int a, int b, ll v) { if (b < l || a > r) return; if (l == a && r == b) { t[x] += v; lt[x] += v; return; } push(x); int m = (l + r) / 2; update(2 * x, l, m, a, min(b, m), v); update(2 * x + 1, m + 1, r, max(a, m + 1), b, v); t[x] = max(t[2 * x], t[2 * x + 1]); } ll query(int x, int l, int r, int a, int b) { if (b < l || a > r) return 0; if (l == a && r == b) return t[x]; push(x); int m = (l + r) / 2; return max(query(2 * x, l, m, a, min(b, m)), query(2 * x + 1, m + 1, r, max(a, m + 1), b)); } int main() { cin >> n >> m; FOR(i, 0, m) cin >> a[i].l >> a[i].r >> a[i].v; sort(a, a + m, cmp); FOR(i, 1, n + 1) { update(1, 1, n, i, i, query(1, 1, n, 1, i)); while (a[cnt].r == i) { update(1, 1, n, a[cnt].l, a[cnt].r, a[cnt].v); cnt++; } } cout << max(t[1], 0ll) << '\n'; return 0; }
replace
3
4
3
4
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using P = pair<ll, ll>; const ll INF = 5e15; const ll MOD = 1e9 + 7; /* * This LazySegmentTree is tree for query "ADD". * If you want to change query, you have to change the function "eval". * Too show where you have to change, I wrote the comment "!! TO CHANGE !!". */ class LazySegmentTree { private: ll N, init; vector<ll> node, lazy; vector<bool> lazy_flag; public: LazySegmentTree(const vector<ll> &v, ll init) { this->init = init; ll tmp = 1; while (tmp < v.size()) tmp *= 2; N = tmp; node.resize(2 * N - 1); lazy.resize(2 * N - 1, init); lazy_flag.resize(2 * N - 1, init); for (ll i = 0; i < v.size(); i++) { node[i + N - 1] = v[i]; } for (ll i = N - 2; 0 <= i; i--) { /* * !! TO CHANGE !! * You have to change the operator '+' between node[i * 2 + 1] and node[i * * 2 + 2] */ node[i] = max(node[i * 2 + 1], node[i * 2 + 2]); } } /* * node[pos] -> [left, right) */ void lazy_eval(ll pos, ll left, ll right) { if (!lazy_flag[pos]) { return; } /* * !! TO CHANGE !! * How to update the value node[pos] by using lazy[pos] */ node[pos] += lazy[pos]; lazy_flag[pos] = false; /* * whether the node is the bottom of tree or not. */ if (right - left > 1) { /* * !! TO CHANGE !! * How to propagate the value to under node. * For example, when the query is "ADD", you have to add half of value to * under node. */ lazy[2 * pos + 1] += lazy[pos]; lazy[2 * pos + 2] += lazy[pos]; lazy_flag[2 * pos + 1] = true; lazy_flag[2 * pos + 2] = true; } /* * !! TO CHANGE !! * Init lazy[pos] because it alredy has been evaluated. */ lazy[pos] = init; } /* * If you want to call this func from out of class, in many cases you don't * have to change the args pos, node_left, node_right. Be careful that the * range is half-open interval. [left, right), [node_left, node_right) * @param left: lower limit of interval of query * @param right: upper limit of interval of query * @param val: the value gave from query * @param node_left: lower limit of interval of the node points. * @param node_right: upper limit of interval of the node points. */ void update_query(ll left, ll right, ll val, ll pos = 0, ll node_left = 0, ll node_right = -1) { if (node_right < 0) { node_right = N; } /* * Execute lazy evaluation. */ lazy_eval(pos, node_left, node_right); /* * If the node is out of inrerval, return. */ if (right <= node_left || node_right <= left) { return; } /* * If the node cover the interval complety, update this->lazy and execute * lazy_eval. Else recursion. */ if (left <= node_left && node_right <= right) { /* * !! TO CHANGE !! * How to propagate the val and how to summary the val.. */ lazy[pos] += val; lazy_flag[pos] = true; lazy_eval(pos, node_left, node_right); } else { /* * recursion */ update_query(left, right, val, 2 * pos + 1, node_left, (node_left + node_right) / 2); update_query(left, right, val, 2 * pos + 2, (node_left + node_right) / 2, node_right); /* * !! TO CHANGE !! * How to summary the value under the node. */ node[pos] = max(node[2 * pos + 1], node[2 * pos + 2]); } } ll get_query(ll left, ll right, ll pos = 0, ll node_left = 0, ll node_right = -1) { if (node_right < 0) { node_right = N; } /* * Evaluate the node[pos] */ lazy_eval(pos, node_left, node_right); if (node_right <= left || right <= node_left) { return -INF; } if (left <= node_left && node_right <= right) { return node[pos]; } /* * !! TO CHANGE !! * You have to change the mark '+' between get_query and get_query. */ return max(get_query(left, right, 2 * pos + 1, node_left, (node_left + node_right) / 2), get_query(left, right, 2 * pos + 2, (node_left + node_right) / 2, node_right)); } }; int main() { ll N, M; cin >> N >> M; ll ans = 0; LazySegmentTree LST(vector<ll>(N + 10), 0); vector<ll> left(2 + 1e5 + 10, 0); map<ll, vector<P>> right; for (ll i = 0; i < M; i++) { ll l, r, a; cin >> l >> r >> a; left[l] += a; right[r].push_back(P(l, a)); } for (ll i = 1; i <= N; i++) { LST.update_query(0, i, left[i]); ll val = LST.get_query(0, i); ans = max(ans, val); LST.update_query(i, i + 1, val); const auto &vec = right[i]; for (const auto &p : vec) { LST.update_query(0, p.first, -p.second); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using P = pair<ll, ll>; const ll INF = 5e15; const ll MOD = 1e9 + 7; /* * This LazySegmentTree is tree for query "ADD". * If you want to change query, you have to change the function "eval". * Too show where you have to change, I wrote the comment "!! TO CHANGE !!". */ class LazySegmentTree { private: ll N, init; vector<ll> node, lazy; vector<bool> lazy_flag; public: LazySegmentTree(const vector<ll> &v, ll init) { this->init = init; ll tmp = 1; while (tmp < v.size()) tmp *= 2; N = tmp; node.resize(2 * N - 1); lazy.resize(2 * N - 1, init); lazy_flag.resize(2 * N - 1, init); for (ll i = 0; i < v.size(); i++) { node[i + N - 1] = v[i]; } for (ll i = N - 2; 0 <= i; i--) { /* * !! TO CHANGE !! * You have to change the operator '+' between node[i * 2 + 1] and node[i * * 2 + 2] */ node[i] = max(node[i * 2 + 1], node[i * 2 + 2]); } } /* * node[pos] -> [left, right) */ void lazy_eval(ll pos, ll left, ll right) { if (!lazy_flag[pos]) { return; } /* * !! TO CHANGE !! * How to update the value node[pos] by using lazy[pos] */ node[pos] += lazy[pos]; lazy_flag[pos] = false; /* * whether the node is the bottom of tree or not. */ if (right - left > 1) { /* * !! TO CHANGE !! * How to propagate the value to under node. * For example, when the query is "ADD", you have to add half of value to * under node. */ lazy[2 * pos + 1] += lazy[pos]; lazy[2 * pos + 2] += lazy[pos]; lazy_flag[2 * pos + 1] = true; lazy_flag[2 * pos + 2] = true; } /* * !! TO CHANGE !! * Init lazy[pos] because it alredy has been evaluated. */ lazy[pos] = init; } /* * If you want to call this func from out of class, in many cases you don't * have to change the args pos, node_left, node_right. Be careful that the * range is half-open interval. [left, right), [node_left, node_right) * @param left: lower limit of interval of query * @param right: upper limit of interval of query * @param val: the value gave from query * @param node_left: lower limit of interval of the node points. * @param node_right: upper limit of interval of the node points. */ void update_query(ll left, ll right, ll val, ll pos = 0, ll node_left = 0, ll node_right = -1) { if (node_right < 0) { node_right = N; } /* * Execute lazy evaluation. */ lazy_eval(pos, node_left, node_right); /* * If the node is out of inrerval, return. */ if (right <= node_left || node_right <= left) { return; } /* * If the node cover the interval complety, update this->lazy and execute * lazy_eval. Else recursion. */ if (left <= node_left && node_right <= right) { /* * !! TO CHANGE !! * How to propagate the val and how to summary the val.. */ lazy[pos] += val; lazy_flag[pos] = true; lazy_eval(pos, node_left, node_right); } else { /* * recursion */ update_query(left, right, val, 2 * pos + 1, node_left, (node_left + node_right) / 2); update_query(left, right, val, 2 * pos + 2, (node_left + node_right) / 2, node_right); /* * !! TO CHANGE !! * How to summary the value under the node. */ node[pos] = max(node[2 * pos + 1], node[2 * pos + 2]); } } ll get_query(ll left, ll right, ll pos = 0, ll node_left = 0, ll node_right = -1) { if (node_right < 0) { node_right = N; } /* * Evaluate the node[pos] */ lazy_eval(pos, node_left, node_right); if (node_right <= left || right <= node_left) { return -INF; } if (left <= node_left && node_right <= right) { return node[pos]; } /* * !! TO CHANGE !! * You have to change the mark '+' between get_query and get_query. */ return max(get_query(left, right, 2 * pos + 1, node_left, (node_left + node_right) / 2), get_query(left, right, 2 * pos + 2, (node_left + node_right) / 2, node_right)); } }; int main() { ll N, M; cin >> N >> M; ll ans = 0; LazySegmentTree LST(vector<ll>(N + 10), 0); vector<ll> left(2 * 1e5 + 10, 0); map<ll, vector<P>> right; for (ll i = 0; i < M; i++) { ll l, r, a; cin >> l >> r >> a; left[l] += a; right[r].push_back(P(l, a)); } for (ll i = 1; i <= N; i++) { LST.update_query(0, i, left[i]); ll val = LST.get_query(0, i); ans = max(ans, val); LST.update_query(i, i + 1, val); const auto &vec = right[i]; for (const auto &p : vec) { LST.update_query(0, p.first, -p.second); } } cout << ans << endl; return 0; }
replace
175
176
175
176
0
p03182
C++
Runtime Error
#include "algorithm" #include "cstdio" #include "iostream" #define LC (x << 1) #define RC (x << 1 | 1) using namespace std; const int maxm = 2e5 + 10; const long long Inf = 1ll << 50; int N, M; struct Segment_Tree { int Left; int Right; long long Max; long long Lazy; #define Left(x) Tree[x].Left #define Right(x) Tree[x].Right #define Max(x) Tree[x].Max #define Lazy(x) Tree[x].Lazy } Tree[maxm << 1]; struct line { int Left; int Right; int A; #define L(x) Line[x].Left #define R(x) Line[x].Right #define A(x) Line[x].A } Line[maxm]; inline bool Comp(line x, line y) { return x.Right == y.Right ? x.Left < y.Left : x.Right < y.Right; } inline void Spread(int x) { if (Lazy(x)) { Max(LC) += Lazy(x); Max(RC) += Lazy(x); Lazy(LC) += Lazy(x); Lazy(RC) += Lazy(x); Lazy(x) = 0; } return; } inline void Update(int x) { Max(x) = max(Max(LC), Max(RC)); return; } void Build(int x, int l, int r) { Left(x) = l, Right(x) = r; if (l == r) { return; } int Mid = l + r >> 1; Build(LC, l, Mid); Build(RC, Mid + 1, r); return; } void Change(int x, int l, int r, long long Data) { if (l <= Left(x) && r >= Right(x)) { Lazy(x) += Data; Max(x) += Data; return; } Spread(x); int Mid = (Left(x) + Right(x)) >> 1; if (l <= Mid) Change(LC, l, r, Data); if (r > Mid) Change(RC, l, r, Data); Update(x); return; } long long Query(int x, int l, int r) { if (l <= Left(x) && r >= Right(x)) { return Max(x); } Spread(x); int Mid = (Left(x) + Right(x)) >> 1; long long Result = -Inf; if (l <= Mid) Result = max(Result, Query(LC, l, r)); if (r > Mid) Result = max(Result, Query(RC, l, r)); return Result; } int main() { register int i, k = 1; scanf("%d%d", &N, &M); Build(1, 0, N); for (i = 1; i <= M; i++) scanf("%d%d%d", &L(i), &R(i), &A(i)); sort(Line + 1, Line + 1 + M, Comp); for (i = 1; i <= N; i++) { Change(1, i, i, Query(1, 0, i)); while (R(k) == i && k <= M) { Change(1, L(k), R(k), A(k)); k++; } } printf("%lld", Query(1, 0, N)); return 0; }
#include "algorithm" #include "cstdio" #include "iostream" #define LC (x << 1) #define RC (x << 1 | 1) using namespace std; const int maxm = 2e5 + 10; const long long Inf = 1ll << 50; int N, M; struct Segment_Tree { int Left; int Right; long long Max; long long Lazy; #define Left(x) Tree[x].Left #define Right(x) Tree[x].Right #define Max(x) Tree[x].Max #define Lazy(x) Tree[x].Lazy } Tree[maxm << 2]; struct line { int Left; int Right; int A; #define L(x) Line[x].Left #define R(x) Line[x].Right #define A(x) Line[x].A } Line[maxm]; inline bool Comp(line x, line y) { return x.Right == y.Right ? x.Left < y.Left : x.Right < y.Right; } inline void Spread(int x) { if (Lazy(x)) { Max(LC) += Lazy(x); Max(RC) += Lazy(x); Lazy(LC) += Lazy(x); Lazy(RC) += Lazy(x); Lazy(x) = 0; } return; } inline void Update(int x) { Max(x) = max(Max(LC), Max(RC)); return; } void Build(int x, int l, int r) { Left(x) = l, Right(x) = r; if (l == r) { return; } int Mid = l + r >> 1; Build(LC, l, Mid); Build(RC, Mid + 1, r); return; } void Change(int x, int l, int r, long long Data) { if (l <= Left(x) && r >= Right(x)) { Lazy(x) += Data; Max(x) += Data; return; } Spread(x); int Mid = (Left(x) + Right(x)) >> 1; if (l <= Mid) Change(LC, l, r, Data); if (r > Mid) Change(RC, l, r, Data); Update(x); return; } long long Query(int x, int l, int r) { if (l <= Left(x) && r >= Right(x)) { return Max(x); } Spread(x); int Mid = (Left(x) + Right(x)) >> 1; long long Result = -Inf; if (l <= Mid) Result = max(Result, Query(LC, l, r)); if (r > Mid) Result = max(Result, Query(RC, l, r)); return Result; } int main() { register int i, k = 1; scanf("%d%d", &N, &M); Build(1, 0, N); for (i = 1; i <= M; i++) scanf("%d%d%d", &L(i), &R(i), &A(i)); sort(Line + 1, Line + 1 + M, Comp); for (i = 1; i <= N; i++) { Change(1, i, i, Query(1, 0, i)); while (R(k) == i && k <= M) { Change(1, L(k), R(k), A(k)); k++; } } printf("%lld", Query(1, 0, N)); return 0; }
replace
22
23
22
23
0
p03182
C++
Runtime Error
// in the name of Allah #include <bits/stdc++.h> using namespace std; #define endl '\n' #define pb push_back #define ll long long #define double long double #define sz(x) ((int)(x.size())) #define fr first #define se second #define inf 1e18 #define max_n 200002 #define mod 1000000007 ll add(ll a, ll b) { a += b; if (a >= mod) return a - mod; return a; } ll mul(ll a, ll b) { a *= b; if (a >= mod) return a % mod; return a; } ll power(ll a, ll b) { ll p = 1; while (b) { if (b & 1) p = mul(p, a); a = mul(a, a); b /= 2; } return p; } /* // a bit of NT const int _N = 1234567; ll F[_N]; ll IF[_N]; void init() { F[0] = 1; for(int i=1; i<_N; i++) F[i] = mul(F[i-1], i); IF[_N-1] = power(F[_N-1], mod-2); for(int i=_N-2; i>=0; i--) IF[i] = mul(IF[i+1], i+1); } ll ncr(ll x, ll y) { if(y > x || y < 0) return 0; return mul(F[x], mul(IF[x-y], IF[y])); } /**/ class mxLazySegTree { public: int sz; vector<ll> tree, lazy; mxLazySegTree(int _sz) { sz = _sz; tree.assign(sz * 2, 0); lazy.assign(sz * 2, 0); } // update void update(int p, int L, int R, int l, int r, ll val) { if (lazy[p] != 0) { tree[p] += lazy[p]; if (L != R) { lazy[2 * p] += lazy[p]; lazy[2 * p + 1] += lazy[p]; } lazy[p] = 0; } if (R < l || r < L) return; if (l <= L && R <= r) { tree[p] += val; if (L != R) { lazy[2 * p] += val; lazy[2 * p + 1] += val; } return; } int M = (L + R) / 2; update(2 * p, L, M, l, r, val); update(2 * p + 1, M + 1, R, l, r, val); tree[p] = max(tree[2 * p], tree[2 * p + 1]); } void update(int l, int r, ll val) { update(1, 0, sz - 1, l, r, val); } // query ll query(int p, int L, int R, int l, int r) { if (R < l || r < L) return -1e18; if (lazy[p] != 0) { tree[p] += lazy[p]; if (L != R) { lazy[2 * p] += lazy[p]; lazy[2 * p + 1] += lazy[p]; } lazy[p] = 0; } if (l <= L && R <= r) return tree[p]; int M = (L + R) / 2; return max(query(2 * p, L, M, l, r), query(2 * p + 1, M + 1, R, l, r)); } ll query(int l, int r) { return query(1, 0, sz - 1, l, r); } }; int n, m; ll a[max_n]; int l[max_n], r[max_n]; vector<int> s[max_n]; vector<int> e[max_n]; int main() { // ll start = clock(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> a[i]; s[l[i]].pb(i); e[r[i]].pb(i); } ll res = 0, ans; mxLazySegTree st(n + 1); for (int i = 1; i <= n; i++) { for (int id : s[i]) st.update(0, i - 1, a[id]); ans = st.query(0, i - 1); res = max(res, ans); st.update(i, i, ans); for (int id : e[i]) st.update(0, l[id] - 1, -a[id]); } cout << res << endl; // cerr << (clock() - start) / (CLOCKS_PER_SEC*1.0) << endl; return 0; }
// in the name of Allah #include <bits/stdc++.h> using namespace std; #define endl '\n' #define pb push_back #define ll long long #define double long double #define sz(x) ((int)(x.size())) #define fr first #define se second #define inf 1e18 #define max_n 200002 #define mod 1000000007 ll add(ll a, ll b) { a += b; if (a >= mod) return a - mod; return a; } ll mul(ll a, ll b) { a *= b; if (a >= mod) return a % mod; return a; } ll power(ll a, ll b) { ll p = 1; while (b) { if (b & 1) p = mul(p, a); a = mul(a, a); b /= 2; } return p; } /* // a bit of NT const int _N = 1234567; ll F[_N]; ll IF[_N]; void init() { F[0] = 1; for(int i=1; i<_N; i++) F[i] = mul(F[i-1], i); IF[_N-1] = power(F[_N-1], mod-2); for(int i=_N-2; i>=0; i--) IF[i] = mul(IF[i+1], i+1); } ll ncr(ll x, ll y) { if(y > x || y < 0) return 0; return mul(F[x], mul(IF[x-y], IF[y])); } /**/ class mxLazySegTree { public: int sz; vector<ll> tree, lazy; mxLazySegTree(int _sz) { sz = _sz; tree.assign(sz * 4, 0); lazy.assign(sz * 4, 0); } // update void update(int p, int L, int R, int l, int r, ll val) { if (lazy[p] != 0) { tree[p] += lazy[p]; if (L != R) { lazy[2 * p] += lazy[p]; lazy[2 * p + 1] += lazy[p]; } lazy[p] = 0; } if (R < l || r < L) return; if (l <= L && R <= r) { tree[p] += val; if (L != R) { lazy[2 * p] += val; lazy[2 * p + 1] += val; } return; } int M = (L + R) / 2; update(2 * p, L, M, l, r, val); update(2 * p + 1, M + 1, R, l, r, val); tree[p] = max(tree[2 * p], tree[2 * p + 1]); } void update(int l, int r, ll val) { update(1, 0, sz - 1, l, r, val); } // query ll query(int p, int L, int R, int l, int r) { if (R < l || r < L) return -1e18; if (lazy[p] != 0) { tree[p] += lazy[p]; if (L != R) { lazy[2 * p] += lazy[p]; lazy[2 * p + 1] += lazy[p]; } lazy[p] = 0; } if (l <= L && R <= r) return tree[p]; int M = (L + R) / 2; return max(query(2 * p, L, M, l, r), query(2 * p + 1, M + 1, R, l, r)); } ll query(int l, int r) { return query(1, 0, sz - 1, l, r); } }; int n, m; ll a[max_n]; int l[max_n], r[max_n]; vector<int> s[max_n]; vector<int> e[max_n]; int main() { // ll start = clock(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> l[i] >> r[i] >> a[i]; s[l[i]].pb(i); e[r[i]].pb(i); } ll res = 0, ans; mxLazySegTree st(n + 1); for (int i = 1; i <= n; i++) { for (int id : s[i]) st.update(0, i - 1, a[id]); ans = st.query(0, i - 1); res = max(res, ans); st.update(i, i, ans); for (int id : e[i]) st.update(0, l[id] - 1, -a[id]); } cout << res << endl; // cerr << (clock() - start) / (CLOCKS_PER_SEC*1.0) << endl; return 0; }
replace
72
74
72
74
-11
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define oo 666666666 vector<array<ll, 2>> A[200005]; ll seg[400005][2]; void lazy(int c, int l, int r) { seg[c][0] += seg[c][1]; if (l != r) { seg[2 * c][1] += seg[c][1]; seg[2 * c + 1][1] += seg[c][1]; } seg[c][1] = 0; } void update(int c, int l, int r, int L, int R, ll val) { lazy(c, l, r); if (l > r || r < L || l > R) return; if (l >= L && r <= R) { seg[c][1] += val; lazy(c, l, r); return; } int m = (l + r) / 2; update(2 * c, l, m, L, R, val); update(2 * c + 1, m + 1, r, L, R, val); seg[c][0] = max(seg[2 * c][0], seg[2 * c + 1][0]); } ll get(int c, int l, int r, int L, int R) { lazy(c, l, r); if (l > r || r < L || l > R) return -1e18; if (l >= L && r <= R) return seg[c][0]; int m = (l + r) / 2; ll ats = max(get(2 * c, l, m, L, R), get(2 * c + 1, m + 1, r, L, R)); seg[c][0] = max(seg[2 * c][0], seg[2 * c + 1][0]); return ats; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; while (m--) { ll L, R, val; cin >> L >> R >> val; A[L].push_back({val, L}); A[R + 1].push_back({-val, L}); } ll ats = 0; for (int i = 1; i <= n; i++) // lets choose which position in string should be '1' { for (auto &x : A[i]) update(1, 0, n, 0, x[1] - 1, x[0]); //^if some interval starts here we increase all previous positions [0,L-1] //by its value because i is in the interval if some interval ends when we // decrease all previous positions [0,L-1] by its value because i is not in // interval anymore ll cur = get(1, 0, n, 0, i - 1); // we get maximum value of prefix [0,i-1] - //^its the best we can get with prefix[1...i] bits done ats = max(ats, cur); update(1, 0, n, i, i, cur); // finally we add our current best for prefix[0,i] to the tree // now other queries can try extending this answer // its like brute-force where we would choose some j in range [0,i-1] //(0 meaning no bits on yet) to extend answer // yet segment tree saves us from N^2, and we can do this in N log N } cout << ats; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define oo 666666666 vector<array<ll, 2>> A[200005]; ll seg[800005][2]; void lazy(int c, int l, int r) { seg[c][0] += seg[c][1]; if (l != r) { seg[2 * c][1] += seg[c][1]; seg[2 * c + 1][1] += seg[c][1]; } seg[c][1] = 0; } void update(int c, int l, int r, int L, int R, ll val) { lazy(c, l, r); if (l > r || r < L || l > R) return; if (l >= L && r <= R) { seg[c][1] += val; lazy(c, l, r); return; } int m = (l + r) / 2; update(2 * c, l, m, L, R, val); update(2 * c + 1, m + 1, r, L, R, val); seg[c][0] = max(seg[2 * c][0], seg[2 * c + 1][0]); } ll get(int c, int l, int r, int L, int R) { lazy(c, l, r); if (l > r || r < L || l > R) return -1e18; if (l >= L && r <= R) return seg[c][0]; int m = (l + r) / 2; ll ats = max(get(2 * c, l, m, L, R), get(2 * c + 1, m + 1, r, L, R)); seg[c][0] = max(seg[2 * c][0], seg[2 * c + 1][0]); return ats; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; while (m--) { ll L, R, val; cin >> L >> R >> val; A[L].push_back({val, L}); A[R + 1].push_back({-val, L}); } ll ats = 0; for (int i = 1; i <= n; i++) // lets choose which position in string should be '1' { for (auto &x : A[i]) update(1, 0, n, 0, x[1] - 1, x[0]); //^if some interval starts here we increase all previous positions [0,L-1] //by its value because i is in the interval if some interval ends when we // decrease all previous positions [0,L-1] by its value because i is not in // interval anymore ll cur = get(1, 0, n, 0, i - 1); // we get maximum value of prefix [0,i-1] - //^its the best we can get with prefix[1...i] bits done ats = max(ats, cur); update(1, 0, n, i, i, cur); // finally we add our current best for prefix[0,i] to the tree // now other queries can try extending this answer // its like brute-force where we would choose some j in range [0,i-1] //(0 meaning no bits on yet) to extend answer // yet segment tree saves us from N^2, and we can do this in N log N } cout << ats; }
replace
7
8
7
8
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using LL = long long; using PII = pair<int, int>; using ll = long long; using DBL = double; using VI = vector<int>; using VD = vector<DBL>; using VVI = vector<VI>; using VVD = vector<VD>; #define FOR(i, a, b) for (int i = a; i < b; i++) #define REP(i, n) FOR(i, 0, n) #define SZ(a) ((int)((a).size())) #define ALL(x) (x).begin(), (x).end() #define SET(a, v) memset((a), (v), sizeof(a)) #define EB emplace_back #define PB push_back #define MP make_pair #define ST first #define ND second // #define trace(...) {__f(#__VA_ARGS__, __VA_ARGS__);} // template<typename Arg> void __f(const char* name, Arg&& arg) { // cerr << name << " = " << arg << 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...); } // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // OST: find_by_order, order_of_key // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> ordered_set; #pragma GCC optimize("Ofast") // optimize("unroll-loops") // target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ///*** RNGS ***/ // SEED=chrono::steady_clock::now().time_since_epoch().count(); //// or high_resolution_clock // mt19937 rng(SEED); /*or*/ random_device rd; mt19937 rng(rd()); // uniform_int_distribution<> dis(MIN, MAX);// usage: dis(rng) // struct chash {int operator()(int x) const {return x^SEED;}}; // gp_hash_table<int,int,chash>m;//use cc if very less updates ///*** FAST IO ***/ // inline int scan(){ bool y=0; int x=0; char c=getchar_unlocked(); // while(c<'0'||c>'9'){ if(c=='-')y=1; c=getchar_unlocked();} // while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar_unlocked();} // return y?-x:x; } const int MAXN = 200010; const long long inf = 1e18; int l[MAXN], r[MAXN], a[MAXN]; vector<int> u[MAXN]; int n, m; class node { public: long long maxVal; long long diff; long long val; node() { maxVal = 0; val = -inf; diff = 0; } } tree[MAXN]; void removelazy(int x, int l, int r) { if (tree[x].val == -inf and tree[x].diff == 0) return; tree[x].maxVal = max(tree[x].maxVal + tree[x].diff, tree[x].val); if (l != r) { tree[2 * x].val += tree[x].diff; tree[2 * x].diff += tree[x].diff; tree[2 * x].val = max(tree[2 * x].val, tree[x].val); tree[1 + (2 * x)].val += tree[x].diff; tree[1 + (2 * x)].diff += tree[x].diff; tree[1 + (2 * x)].val = max(tree[1 + (2 * x)].val, tree[x].val); } tree[x].val = -inf; tree[x].diff = 0; } void upd(int x, int l, int r, int p, int q, long long v1, long long v2) { removelazy(x, l, r); if (r < p or q < l) return; if (p <= l and r <= q) { tree[x].val = v1; tree[x].diff = v2; removelazy(x, l, r); return; } int m = ((l + r) / 2); upd(2 * x, l, m, p, q, v1, v2); upd((2 * x) + 1, 1 + m, r, p, q, v1, v2); tree[x].maxVal = max(tree[2 * x].maxVal, tree[2 * x + 1].maxVal); } long long query(int x, int l, int r, int p) { removelazy(x, l, r); if (l == r) { assert(l == p); return tree[x].maxVal; } int m = ((l + r) / 2); if (p <= m) return query((2 * x), l, m, p); else return query((2 * x) + 1, 1 + m, r, p); } int main() { #ifdef LOCAL_EXEC // freopen("sample.in", "r", stdin); // freopen("sample.out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #endif cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> l[i] >> r[i] >> a[i]; u[r[i]].PB(i); } for (int pos = n; pos >= 0; --pos) { if (pos != n) upd(1, 0, n, 0, pos, query(1, 0, n, pos + 1), 0); for (auto &i : u[pos]) { upd(1, 0, n, l[i], pos, -inf, a[i]); } } cout << query(1, 0, n, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using LL = long long; using PII = pair<int, int>; using ll = long long; using DBL = double; using VI = vector<int>; using VD = vector<DBL>; using VVI = vector<VI>; using VVD = vector<VD>; #define FOR(i, a, b) for (int i = a; i < b; i++) #define REP(i, n) FOR(i, 0, n) #define SZ(a) ((int)((a).size())) #define ALL(x) (x).begin(), (x).end() #define SET(a, v) memset((a), (v), sizeof(a)) #define EB emplace_back #define PB push_back #define MP make_pair #define ST first #define ND second // #define trace(...) {__f(#__VA_ARGS__, __VA_ARGS__);} // template<typename Arg> void __f(const char* name, Arg&& arg) { // cerr << name << " = " << arg << 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...); } // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // OST: find_by_order, order_of_key // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> ordered_set; #pragma GCC optimize("Ofast") // optimize("unroll-loops") // target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ///*** RNGS ***/ // SEED=chrono::steady_clock::now().time_since_epoch().count(); //// or high_resolution_clock // mt19937 rng(SEED); /*or*/ random_device rd; mt19937 rng(rd()); // uniform_int_distribution<> dis(MIN, MAX);// usage: dis(rng) // struct chash {int operator()(int x) const {return x^SEED;}}; // gp_hash_table<int,int,chash>m;//use cc if very less updates ///*** FAST IO ***/ // inline int scan(){ bool y=0; int x=0; char c=getchar_unlocked(); // while(c<'0'||c>'9'){ if(c=='-')y=1; c=getchar_unlocked();} // while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar_unlocked();} // return y?-x:x; } const int MAXN = 200010; const long long inf = 1e18; int l[MAXN], r[MAXN], a[MAXN]; vector<int> u[MAXN]; int n, m; class node { public: long long maxVal; long long diff; long long val; node() { maxVal = 0; val = -inf; diff = 0; } } tree[4 * MAXN]; void removelazy(int x, int l, int r) { if (tree[x].val == -inf and tree[x].diff == 0) return; tree[x].maxVal = max(tree[x].maxVal + tree[x].diff, tree[x].val); if (l != r) { tree[2 * x].val += tree[x].diff; tree[2 * x].diff += tree[x].diff; tree[2 * x].val = max(tree[2 * x].val, tree[x].val); tree[1 + (2 * x)].val += tree[x].diff; tree[1 + (2 * x)].diff += tree[x].diff; tree[1 + (2 * x)].val = max(tree[1 + (2 * x)].val, tree[x].val); } tree[x].val = -inf; tree[x].diff = 0; } void upd(int x, int l, int r, int p, int q, long long v1, long long v2) { removelazy(x, l, r); if (r < p or q < l) return; if (p <= l and r <= q) { tree[x].val = v1; tree[x].diff = v2; removelazy(x, l, r); return; } int m = ((l + r) / 2); upd(2 * x, l, m, p, q, v1, v2); upd((2 * x) + 1, 1 + m, r, p, q, v1, v2); tree[x].maxVal = max(tree[2 * x].maxVal, tree[2 * x + 1].maxVal); } long long query(int x, int l, int r, int p) { removelazy(x, l, r); if (l == r) { assert(l == p); return tree[x].maxVal; } int m = ((l + r) / 2); if (p <= m) return query((2 * x), l, m, p); else return query((2 * x) + 1, 1 + m, r, p); } int main() { #ifdef LOCAL_EXEC // freopen("sample.in", "r", stdin); // freopen("sample.out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #endif cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> l[i] >> r[i] >> a[i]; u[r[i]].PB(i); } for (int pos = n; pos >= 0; --pos) { if (pos != n) upd(1, 0, n, 0, pos, query(1, 0, n, pos + 1), 0); for (auto &i : u[pos]) { upd(1, 0, n, l[i], pos, -inf, a[i]); } } cout << query(1, 0, n, 0) << endl; return 0; }
replace
68
69
68
69
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; struct P { int l; ll a; }; ll L[200000], ans; vector<P> R[200000]; const ll LINF = 1e18; // BEGIN CUT HERE template <typename T, typename E> struct SegmentTree { using F = function<T(T, T)>; using G = function<T(T, E)>; using H = function<E(E, E)>; // この辺はとりあえずbeetのブログ読むのが良さそう? // 最初の具体例で言うとTとEが0以上の整数で、F(a,b):max(a,b) G(a,α):if(α)a=α // H(α,β):if(β)α=β のはず int n, height; F f; G g; H h; T ti; E ei; vector<T> dat; vector<E> laz; vector<int> leng; SegmentTree(F f, G g, H h, T ti, E ei) : f(f), g(g), h(h), ti(ti), ei(ei) {} /* イメージ的には initとbuildは初期化 buildだとvectorで初期化出来る reflectは作用済のdatを返すので適切なdatとlazが入っていればいい propagateはkの作用をeiにリセットする役割で、作用を全部済ませる(datの更新と子のlazへの渡し) thrustは(vectorでの)kを渡すとkの列を上から順にpropagateすることで上のlazを空にする recalcは(vectorでの)kを渡すとkの列を下から順にdatを子のdatのクエリ済のものに更新する(自分のlazは適用しない) updateは(vectorでの)a,b,xを渡すと[a,b)に作用xを適用      まず閉区間で区間の端二列に関してはlazを一回空にしておく      区間をカバーする最小個のブロックのlazを更新する そのあと閉区間で端っこに関してはdatを変更していく set_valは要素一つに値一つを代入する      その列のlazを空にしてから一番下に代入して、上にどんどんdatを更新 queryは普通にやるだけなんだけど、なんでこれで良いかを考える必要がありそう 一番引っかかったのはqueryやupdateで両端だけthrustやrecalcをするか 例えば[a,b]にupdateやったあとに[c,d]に対してqueryをする時に[c,d]で見るノードは 1.[a,b]でアプデしたノード 2.[a,b]に関係ないノード 3.a,b,c,dいずれかの上にあるノード だけってのが成り立つからっぽい気がする */ void init(int n_) { n = 1; height = 0; while (n < n_) n <<= 1, height++; dat.assign(2 * n, ti); laz.assign(2 * n, ei); leng.resize(2 * n); int tmp = n; for (int j = 1; j < 2 * n; j *= 2) { for (int i = j; i < 2 * j; i++) leng[i] = tmp; tmp /= 2; } } void build(const vector<T> &v) { int n_ = v.size(); init(n_); for (int i = 0; i < n_; i++) dat[n + i] = v[i]; for (int i = n - 1; i; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } // inlineは実行時間が短くなる魔法なので読む時には気にしなくていいはず inline T reflect(int k) { return laz[k] == ei ? dat[k] : g(dat[k], laz[k]); // 作用済のdat[k]の値を返す } // propagateは伝わるって意味らしい inline void propagate(int k) { // laz[k]をdat[k]と子のlazに適用して自身をeiに戻す if (laz[k] == ei) return; laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]); laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]); // 子の作用の更新 dat[k] = reflect(k); // dat[k]を作用済に変更 laz[k] = ei; // 作用したのでlaz[k]はリセット } // thrustは突き出すって意味らしい inline void thrust(int k) { for (int i = height; i; i--) propagate( k >> i); // kの列を一番上から更新させていく 子がいない一番下にはやらない } inline void recalc(int k) { while (k >>= 1) dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1)); // kの列を下から上に更新していく } void update(int a, int b, E x) { //[a,b)に作用Eを適用 thrust( a += n); // aを一番下のそれにして、その列の一番上から現在溜まっている作用を済ませておく thrust(b += n - 1); // bについても同様 ただしここでbは閉区間としてのそれになっている for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { //[a,b)の範囲を一番カバーできるlaz(だいたいlog個)を更新する if (l & 1) laz[l] = h(laz[l], x), l++; if (r & 1) --r, laz[r] = h(laz[r], x); } recalc(a); // 今回作用を施した端っこについて下から上にdatを更新していく recalc(b); } void set_val(int a, T x) { // aにxを代入 thrust( a += n); // aを一番下のそれにして、その列の一番上から現在溜まっている作用を済ませておく dat[a] = x; laz[a] = ei; // aの値をxに変更してそこのlazをリセット recalc(a); // aの上のdatを更新 } T query(int a, int b) { //[a,b)のfを求めるいつものクエリ thrust( a += n); // aを一番下のそれにして、その列の一番上から現在溜まっている作用を済ませておく thrust( b += n - 1); // bを一番下のそれにして、その列の一番上から現在溜まっている作用を済ませておく T vl = ti, vr = ti; for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, reflect(l++)); if (r & 1) vr = f(reflect(--r), vr); } return f(vl, vr); } template <typename C> int find(int st, C &check, T &acc, int k, int l, int r) { if (l + 1 == r) { acc = f(acc, reflect(k)); return check(acc) ? k - n : -1; } propagate(k); int m = (l + r) >> 1; if (m <= st) return find(st, check, acc, (k << 1) | 1, m, r); if (st <= l && !check(f(acc, dat[k]))) { acc = f(acc, dat[k]); return -1; } int vl = find(st, check, acc, (k << 1) | 0, l, m); if (~vl) return vl; return find(st, check, acc, (k << 1) | 1, m, r); } template <typename C> int find(int st, C &check) { T acc = ti; return find(st, check, acc, 1, 0, n); } }; // END CUT HERE int main() { int n, m; cin >> n >> m; while (m--) { int l, r, a; cin >> l >> r >> a; L[l] += a; R[r].push_back({l, a}); } auto f = [](ll a, ll b) { return max(a, b); }; auto g = [](ll a, ll b) { return a + b; }; SegmentTree<ll, ll> seg(f, g, g, -LINF, 0); seg.init(200000); seg.set_val(0, 0); for (int i = 1; i <= n; i++) { seg.update(0, i, L[i]); ll S = seg.query(0, i); if (ans < S) ans = S; seg.set_val(i, S); for (P p : R[i]) seg.update(0, p.l, -p.a); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct P { int l; ll a; }; ll L[200001], ans; vector<P> R[200001]; const ll LINF = 1e18; // BEGIN CUT HERE template <typename T, typename E> struct SegmentTree { using F = function<T(T, T)>; using G = function<T(T, E)>; using H = function<E(E, E)>; // この辺はとりあえずbeetのブログ読むのが良さそう? // 最初の具体例で言うとTとEが0以上の整数で、F(a,b):max(a,b) G(a,α):if(α)a=α // H(α,β):if(β)α=β のはず int n, height; F f; G g; H h; T ti; E ei; vector<T> dat; vector<E> laz; vector<int> leng; SegmentTree(F f, G g, H h, T ti, E ei) : f(f), g(g), h(h), ti(ti), ei(ei) {} /* イメージ的には initとbuildは初期化 buildだとvectorで初期化出来る reflectは作用済のdatを返すので適切なdatとlazが入っていればいい propagateはkの作用をeiにリセットする役割で、作用を全部済ませる(datの更新と子のlazへの渡し) thrustは(vectorでの)kを渡すとkの列を上から順にpropagateすることで上のlazを空にする recalcは(vectorでの)kを渡すとkの列を下から順にdatを子のdatのクエリ済のものに更新する(自分のlazは適用しない) updateは(vectorでの)a,b,xを渡すと[a,b)に作用xを適用      まず閉区間で区間の端二列に関してはlazを一回空にしておく      区間をカバーする最小個のブロックのlazを更新する そのあと閉区間で端っこに関してはdatを変更していく set_valは要素一つに値一つを代入する      その列のlazを空にしてから一番下に代入して、上にどんどんdatを更新 queryは普通にやるだけなんだけど、なんでこれで良いかを考える必要がありそう 一番引っかかったのはqueryやupdateで両端だけthrustやrecalcをするか 例えば[a,b]にupdateやったあとに[c,d]に対してqueryをする時に[c,d]で見るノードは 1.[a,b]でアプデしたノード 2.[a,b]に関係ないノード 3.a,b,c,dいずれかの上にあるノード だけってのが成り立つからっぽい気がする */ void init(int n_) { n = 1; height = 0; while (n < n_) n <<= 1, height++; dat.assign(2 * n, ti); laz.assign(2 * n, ei); leng.resize(2 * n); int tmp = n; for (int j = 1; j < 2 * n; j *= 2) { for (int i = j; i < 2 * j; i++) leng[i] = tmp; tmp /= 2; } } void build(const vector<T> &v) { int n_ = v.size(); init(n_); for (int i = 0; i < n_; i++) dat[n + i] = v[i]; for (int i = n - 1; i; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } // inlineは実行時間が短くなる魔法なので読む時には気にしなくていいはず inline T reflect(int k) { return laz[k] == ei ? dat[k] : g(dat[k], laz[k]); // 作用済のdat[k]の値を返す } // propagateは伝わるって意味らしい inline void propagate(int k) { // laz[k]をdat[k]と子のlazに適用して自身をeiに戻す if (laz[k] == ei) return; laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]); laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]); // 子の作用の更新 dat[k] = reflect(k); // dat[k]を作用済に変更 laz[k] = ei; // 作用したのでlaz[k]はリセット } // thrustは突き出すって意味らしい inline void thrust(int k) { for (int i = height; i; i--) propagate( k >> i); // kの列を一番上から更新させていく 子がいない一番下にはやらない } inline void recalc(int k) { while (k >>= 1) dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1)); // kの列を下から上に更新していく } void update(int a, int b, E x) { //[a,b)に作用Eを適用 thrust( a += n); // aを一番下のそれにして、その列の一番上から現在溜まっている作用を済ませておく thrust(b += n - 1); // bについても同様 ただしここでbは閉区間としてのそれになっている for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { //[a,b)の範囲を一番カバーできるlaz(だいたいlog個)を更新する if (l & 1) laz[l] = h(laz[l], x), l++; if (r & 1) --r, laz[r] = h(laz[r], x); } recalc(a); // 今回作用を施した端っこについて下から上にdatを更新していく recalc(b); } void set_val(int a, T x) { // aにxを代入 thrust( a += n); // aを一番下のそれにして、その列の一番上から現在溜まっている作用を済ませておく dat[a] = x; laz[a] = ei; // aの値をxに変更してそこのlazをリセット recalc(a); // aの上のdatを更新 } T query(int a, int b) { //[a,b)のfを求めるいつものクエリ thrust( a += n); // aを一番下のそれにして、その列の一番上から現在溜まっている作用を済ませておく thrust( b += n - 1); // bを一番下のそれにして、その列の一番上から現在溜まっている作用を済ませておく T vl = ti, vr = ti; for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, reflect(l++)); if (r & 1) vr = f(reflect(--r), vr); } return f(vl, vr); } template <typename C> int find(int st, C &check, T &acc, int k, int l, int r) { if (l + 1 == r) { acc = f(acc, reflect(k)); return check(acc) ? k - n : -1; } propagate(k); int m = (l + r) >> 1; if (m <= st) return find(st, check, acc, (k << 1) | 1, m, r); if (st <= l && !check(f(acc, dat[k]))) { acc = f(acc, dat[k]); return -1; } int vl = find(st, check, acc, (k << 1) | 0, l, m); if (~vl) return vl; return find(st, check, acc, (k << 1) | 1, m, r); } template <typename C> int find(int st, C &check) { T acc = ti; return find(st, check, acc, 1, 0, n); } }; // END CUT HERE int main() { int n, m; cin >> n >> m; while (m--) { int l, r, a; cin >> l >> r >> a; L[l] += a; R[r].push_back({l, a}); } auto f = [](ll a, ll b) { return max(a, b); }; auto g = [](ll a, ll b) { return a + b; }; SegmentTree<ll, ll> seg(f, g, g, -LINF, 0); seg.init(200000); seg.set_val(0, 0); for (int i = 1; i <= n; i++) { seg.update(0, i, L[i]); ll S = seg.query(0, i); if (ans < S) ans = S; seg.set_val(i, S); for (P p : R[i]) seg.update(0, p.l, -p.a); } cout << ans << endl; }
replace
8
10
8
10
0