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
p03171
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define sz(x) (int)(x.size()) using namespace std; const int N = 1e3 + 5, inf = 1e18, mod = 1e9 + 7; int n, a[N], dp[N][N]; int go(int L, int R) { if (L > R) return 0; int &ans = dp[L][R]; if (~ans) return ans; int c1 = a[L] - go(L + 1, R); int c2 = a[R] - go(L, R - 1); return ans = max(c1, c2); } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(dp, -1, sizeof(dp)); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cout << go(1, n); }
#include <bits/stdc++.h> #define int long long #define sz(x) (int)(x.size()) using namespace std; const int N = 3e3 + 5, inf = 1e18, mod = 1e9 + 7; int n, a[N], dp[N][N]; int go(int L, int R) { if (L > R) return 0; int &ans = dp[L][R]; if (~ans) return ans; int c1 = a[L] - go(L + 1, R); int c2 = a[R] - go(L, R - 1); return ans = max(c1, c2); } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(dp, -1, sizeof(dp)); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cout << go(1, n); }
replace
5
6
5
6
0
p03171
C++
Runtime Error
/* Author: Racer5x *************************************** UNAUTHORISED COPYING OF CODE PROHIBITED ********************************** */ /*#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/ #include <bits/stdc++.h> #define int long long #define double long double #define pb emplace_back #define pf emplace_front #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define x first #define y second #define sz(x) (int)x.size() #define endl '\n' #define hell 998244353 #define PI 3.141592653589 #define tezz \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define MAX 2000000000000000000 #define M 1000000007 using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int power(int x, int n) { int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result % M; } int n, arr[3005] = {0}; int dpx[3005][3005], dpy[3005][3005]; int fx(int left, int right); int fy(int left, int right); int fx(int left, int right) { // cout<<" fx -> "<<left<<' '<<right<<" -------> "<<endl; if (left > n - 1 || right < 0) return 0; if (left == right) { /*cout<<arr[left]<<endl;*/ return arr[left]; } if (dpx[left][right] >= 0) { /*cout<<dpx[left][right]<<endl;*/ return dpx[left][right]; } dpx[left][right] = max(arr[left] - fy(left + 1, right), arr[right] - fy(left, right - 1)); // cout<<dpx[left][right]<<endl; return dpx[left][right]; } int fy(int left, int right) { // cout<<" fy -> "<<left<<' '<<right<<" -------> "<<endl; if (left > n - 1 || right < 0) return 0; if (left == right) { /* cout<<arr[left]<<endl;*/ return arr[left]; } if (dpy[left][right] >= 0) { /* cout<<dpy[left][right]<<endl; */ return dpy[left][right]; } dpy[left][right] = max((1) * arr[left] - fx(left + 1, right), (1) * arr[right] - fx(left, right - 1)); // cout<<dpy[left][right]<<endl; return dpy[left][right]; } signed main() { tezz #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif memset(dpx, -1, sizeof(dpx)); memset(dpy, -1, sizeof(dpy)); cin >> n; int sum = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; } cout << fx(0, n - 1); }
/* Author: Racer5x *************************************** UNAUTHORISED COPYING OF CODE PROHIBITED ********************************** */ /*#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/ #include <bits/stdc++.h> #define int long long #define double long double #define pb emplace_back #define pf emplace_front #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define x first #define y second #define sz(x) (int)x.size() #define endl '\n' #define hell 998244353 #define PI 3.141592653589 #define tezz \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define MAX 2000000000000000000 #define M 1000000007 using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int power(int x, int n) { int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result % M; } int n, arr[3005] = {0}; int dpx[3005][3005], dpy[3005][3005]; int fx(int left, int right); int fy(int left, int right); int fx(int left, int right) { // cout<<" fx -> "<<left<<' '<<right<<" -------> "<<endl; if (left > n - 1 || right < 0) return 0; if (left == right) { /*cout<<arr[left]<<endl;*/ return arr[left]; } if (dpx[left][right] >= 0) { /*cout<<dpx[left][right]<<endl;*/ return dpx[left][right]; } dpx[left][right] = max(arr[left] - fy(left + 1, right), arr[right] - fy(left, right - 1)); // cout<<dpx[left][right]<<endl; return dpx[left][right]; } int fy(int left, int right) { // cout<<" fy -> "<<left<<' '<<right<<" -------> "<<endl; if (left > n - 1 || right < 0) return 0; if (left == right) { /* cout<<arr[left]<<endl;*/ return arr[left]; } if (dpy[left][right] >= 0) { /* cout<<dpy[left][right]<<endl; */ return dpy[left][right]; } dpy[left][right] = max((1) * arr[left] - fx(left + 1, right), (1) * arr[right] - fx(left, right - 1)); // cout<<dpy[left][right]<<endl; return dpy[left][right]; } signed main() { tezz memset(dpx, -1, sizeof(dpx)); memset(dpy, -1, sizeof(dpy)); cin >> n; int sum = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; } cout << fx(0, n - 1); }
replace
90
96
90
91
-11
p03171
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long int dp[4000][4000]; int a[8000]; int solve(int l, int r) { if (l > r || l <= 0 || r <= 0) return 0; if (l == r) return a[l]; if (dp[l][r] != -1) return dp[l][r]; if (r - l + 1 == 2) return max(a[l], a[r]); return max(a[l] + min(solve(l + 2, r), solve(l + 1, r - 1)), a[r] + min(solve(l + 1, r - 1), solve(l, r - 2))); } signed main() { int n; cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } for (int i = 0; i < n + 4; i++) for (int j = 0; j < n + 4; j++) dp[i][j] = -1; for (int i = 1; i <= n; i++) dp[i][i] = a[i]; cout << 2 * solve(1, n) - (sum) << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long int dp[4000][4000]; int a[8000]; int solve(int l, int r) { if (l > r || l <= 0 || r <= 0) return 0; if (l == r) return a[l]; if (dp[l][r] != -1) return dp[l][r]; if (r - l + 1 == 2) return max(a[l], a[r]); return dp[l][r] = max(a[l] + min(solve(l + 2, r), solve(l + 1, r - 1)), a[r] + min(solve(l + 1, r - 1), solve(l, r - 2))); } signed main() { int n; cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } for (int i = 0; i < n + 4; i++) for (int j = 0; j < n + 4; j++) dp[i][j] = -1; for (int i = 1; i <= n; i++) dp[i][i] = a[i]; cout << 2 * solve(1, n) - (sum) << endl; }
replace
14
16
14
16
TLE
p03171
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; typedef pair<ll, ll> P; const int MAXN = 3005; vector<vector<vector<P>>> dp(MAXN, vector<vector<P>>(2, vector<P>(MAXN))); vector<int> a(MAXN); bool visited[MAXN][MAXN][2]; P q(int i, int j, int k) { // k=1太郎, a_i...a_jでkが操作 if (visited[i][j][k]) return dp[i][j][k]; else if (i > j) return P(0, 0); P front, back; if (k == 0) { // 太郎 front = q(i + 1, j, k + 1); front.first += a[i]; back = q(i, j - 1, k + 1); back.first += a[j]; if (front.first - front.second >= back.first - back.second) dp[i][j][k] = front; else dp[i][j][k] = back; } else if (k == 1) { front = q(i + 1, j, k - 1); front.second += a[i]; back = q(i, j - 1, k - 1); back.second += a[j]; if (front.first - front.second <= back.first - back.second) dp[i][j][k] = front; else dp[i][j][k] = back; } visited[i][j][k] = true; return dp[i][j][k]; } int main() { int N; cin >> N; rep(i, N) cin >> a[i]; P result = q(0, N - 1, 0); cout << (result.first - result.second) << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; typedef pair<ll, ll> P; const int MAXN = 3005; vector<vector<vector<P>>> dp(MAXN, vector<vector<P>>(MAXN, vector<P>(2))); vector<int> a(MAXN); bool visited[MAXN][MAXN][2]; P q(int i, int j, int k) { // k=1太郎, a_i...a_jでkが操作 if (visited[i][j][k]) return dp[i][j][k]; else if (i > j) return P(0, 0); P front, back; if (k == 0) { // 太郎 front = q(i + 1, j, k + 1); front.first += a[i]; back = q(i, j - 1, k + 1); back.first += a[j]; if (front.first - front.second >= back.first - back.second) dp[i][j][k] = front; else dp[i][j][k] = back; } else if (k == 1) { front = q(i + 1, j, k - 1); front.second += a[i]; back = q(i, j - 1, k - 1); back.second += a[j]; if (front.first - front.second <= back.first - back.second) dp[i][j][k] = front; else dp[i][j][k] = back; } visited[i][j][k] = true; return dp[i][j][k]; } int main() { int N; cin >> N; rep(i, N) cin >> a[i]; P result = q(0, N - 1, 0); cout << (result.first - result.second) << endl; return 0; }
replace
7
8
7
8
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03171
C++
Time Limit Exceeded
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> // #include<bits/extc++.h> using namespace std; // using namespace __gnu_pbds; // typedef // tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> // set_t; #define mp(a, b) make_pair((a), (b)) #define pii pair<int, int> #define pll pair<LL, LL> #define pdd pair<double, double> #define pb push_back #define x first #define y second #define sqr(x) ((x) * (x)) #define EPS 1e-6 #define MEM(x) memset(x, 0, sizeof(x)) #define MEMS(x) memset(x, -1, sizeof(x)) #define pi acos(-1) #define index Index #define Line pll typedef long long LL; LL dp[3005][3005]; int vis[3005][3005]; int a[3005]; LL DP(int l, int r) { if (l > r) return 0; if (vis[l][r]) return dp[l][r]; dp[l][r] = max(a[r] - DP(l, r - 1), a[l] - DP(l + 1, r)); return dp[l][r]; } int main() { int n; scanf("%d", &n); MEM(vis); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); printf("%lld\n", DP(1, n)); }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> // #include<bits/extc++.h> using namespace std; // using namespace __gnu_pbds; // typedef // tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> // set_t; #define mp(a, b) make_pair((a), (b)) #define pii pair<int, int> #define pll pair<LL, LL> #define pdd pair<double, double> #define pb push_back #define x first #define y second #define sqr(x) ((x) * (x)) #define EPS 1e-6 #define MEM(x) memset(x, 0, sizeof(x)) #define MEMS(x) memset(x, -1, sizeof(x)) #define pi acos(-1) #define index Index #define Line pll typedef long long LL; LL dp[3005][3005]; int vis[3005][3005]; int a[3005]; LL DP(int l, int r) { if (l > r) return 0; if (vis[l][r]) return dp[l][r]; dp[l][r] = max(a[r] - DP(l, r - 1), a[l] - DP(l + 1, r)); vis[l][r] = 1; return dp[l][r]; } int main() { int n; scanf("%d", &n); MEM(vis); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); printf("%lld\n", DP(1, n)); }
insert
33
33
33
34
TLE
p03171
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[2001][2001]; long long a[2001]; long long game(int i, int j, int c) { // cout<<i<<" "<<j<<" "<<x<<" "<<y<<endl; if (dp[i][j] != -1) { return dp[i][j]; } long long ans; if (i > j) { // cout<<x<<" "<<y<<"%%%%5"<<endl; return 0; } else { if (c % 2 == 0) { // if(i == j)return (x + a[i] - y); ans = max(game(i + 1, j, c + 1) + a[i], game(i, j - 1, c + 1) + a[j]); // cout<<game(i + 1, j, x + a[i], y, c + 1)<<"****"<<game(i, j-1, x + // a[j], y, c + 1)<<endl; } else { // if(i == j)return (x - y - a[i]); ans = min(game(i + 1, j, c + 1) - a[i], game(i, j - 1, c + 1) - a[j]); // cout<<game(i + 1, j, x , y + a[i], c + 1)<<"****"<<game(i, j-1, x , // y + a[j], c + 1)<<endl; } } // cout<<i<<"#"<<j<<"#"<<c<<"#"<<ans<<endl; dp[i][j] = ans; return ans; } int main() { long long mod = 1000000007; ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; // vector<int> a(n, 0); for (int i = 0; i < n; i++) { cin >> a[i]; } memset(dp, -1, sizeof(dp)); cout << game(0, n - 1, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long dp[3001][3001]; long long a[3001]; long long game(int i, int j, int c) { // cout<<i<<" "<<j<<" "<<x<<" "<<y<<endl; if (dp[i][j] != -1) { return dp[i][j]; } long long ans; if (i > j) { // cout<<x<<" "<<y<<"%%%%5"<<endl; return 0; } else { if (c % 2 == 0) { // if(i == j)return (x + a[i] - y); ans = max(game(i + 1, j, c + 1) + a[i], game(i, j - 1, c + 1) + a[j]); // cout<<game(i + 1, j, x + a[i], y, c + 1)<<"****"<<game(i, j-1, x + // a[j], y, c + 1)<<endl; } else { // if(i == j)return (x - y - a[i]); ans = min(game(i + 1, j, c + 1) - a[i], game(i, j - 1, c + 1) - a[j]); // cout<<game(i + 1, j, x , y + a[i], c + 1)<<"****"<<game(i, j-1, x , // y + a[j], c + 1)<<endl; } } // cout<<i<<"#"<<j<<"#"<<c<<"#"<<ans<<endl; dp[i][j] = ans; return ans; } int main() { long long mod = 1000000007; ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; // vector<int> a(n, 0); for (int i = 0; i < n; i++) { cin >> a[i]; } memset(dp, -1, sizeof(dp)); cout << game(0, n - 1, 0) << endl; return 0; }
replace
2
4
2
4
0
p03171
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[3001][3001]; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; // dp[i][j]=i-dp[i+1][j-1]||j-dp[] for (int gap = 1; gap <= n; gap++) { for (int i = 0; i < n; i++) { int j = i + gap - 1; if (i == j) dp[i][j] = a[i]; else { dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]); } } } cout << dp[0][n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; long long dp[3001][3001]; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; // dp[i][j]=i-dp[i+1][j-1]||j-dp[] for (int gap = 1; gap <= n; gap++) { for (int i = 0; i < n; i++) { int j = i + gap - 1; if (i == j) dp[i][j] = a[i]; else if (j < n) { dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]); } } } cout << dp[0][n - 1] << endl; }
replace
15
16
15
16
0
p03171
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; #define watch(x) cout << (#x) << " is " << x << endl; #define ll long long int #define pb push_back #define in insert #define mp make_pair #define F first #define S second #define N 1000000 #define MOD 1000000007 #define PI acos(-1) ll dp[3005][3005]; ll func(ll l, ll r, ll a[]) { if (l > r) return 0; if (l == r) return dp[l][r] = a[l]; return dp[l][r] = max(a[l] - func(l + 1, r, a), a[r] - func(l, r - 1, a)); } int main() { IOS ll n; cin >> n; ll a[n], i; for (i = 0; i < n; i++) cin >> a[i]; memset(dp, -1, sizeof(dp)); ll ans = func(0, n - 1, a); cout << ans; }
#include <bits/stdc++.h> #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; #define watch(x) cout << (#x) << " is " << x << endl; #define ll long long int #define pb push_back #define in insert #define mp make_pair #define F first #define S second #define N 1000000 #define MOD 1000000007 #define PI acos(-1) ll dp[3005][3005]; ll func(ll l, ll r, ll a[]) { if (l > r) return 0; if (dp[l][r] != -1) return dp[l][r]; if (l == r) return dp[l][r] = a[l]; return dp[l][r] = max(a[l] - func(l + 1, r, a), a[r] - func(l, r - 1, a)); } int main() { IOS ll n; cin >> n; ll a[n], i; for (i = 0; i < n; i++) cin >> a[i]; memset(dp, -1, sizeof(dp)); ll ans = func(0, n - 1, a); cout << ans; }
insert
20
20
20
22
TLE
p03171
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iostream> using namespace std; long long int dp[3001][3001]; long long int min(long long int a, long long int b) { if (a > b) return b; return a; } long long int max(long long int a, long long int b) { if (a > b) return a; return b; } long long int util(vector<long long int> &v, int i, int j) { if (i == j) return v[i]; if (i + 1 == j) return max(v[i], v[j]); long long int ma = max(v[i] + min(util(v, i + 1, j - 1), util(v, i + 2, j)), v[j] + min(util(v, i, j - 2), util(v, i + 1, j - 1))); dp[i][j] = ma; return ma; } int main() { int n; cin >> n; vector<long long int> v(n); memset(dp, -1, sizeof(dp)); long long int tot = 0; for (int i = 0; i < n; i++) { cin >> v[i]; tot += v[i]; } long long int ret = util(v, 0, n - 1); cout << (2 * 1LL * ret - tot) << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; long long int dp[3001][3001]; long long int min(long long int a, long long int b) { if (a > b) return b; return a; } long long int max(long long int a, long long int b) { if (a > b) return a; return b; } long long int util(vector<long long int> &v, int i, int j) { if (i == j) return v[i]; if (i + 1 == j) return max(v[i], v[j]); if (dp[i][j] != -1) return dp[i][j]; long long int ma = max(v[i] + min(util(v, i + 1, j - 1), util(v, i + 2, j)), v[j] + min(util(v, i, j - 2), util(v, i + 1, j - 1))); dp[i][j] = ma; return ma; } int main() { int n; cin >> n; vector<long long int> v(n); memset(dp, -1, sizeof(dp)); long long int tot = 0; for (int i = 0; i < n; i++) { cin >> v[i]; tot += v[i]; } long long int ret = util(v, 0, n - 1); cout << (2 * 1LL * ret - tot) << endl; return 0; }
insert
19
19
19
21
TLE
p03171
C++
Time Limit Exceeded
// YA TOH WOH AAGE SE LEGA YA PHIR PICHE SE // TOH BAS USKA MAXIMUM NIKALNA HAI // DP[I][J] MAXIMUM OF X-Y IN THE SUBARRAY FROM I TO J #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define ff first #define ss second #define eb emplace_back #define MP make_pair #define pb push_back #define pf push_front #define ins insert #define endl '\n' #define all(v) (v).begin(), (v).end() #define mset(m, v) memset(m, v, sizeof(m)) #define tr(c, it) for (auto it = c.begin(); it != c.end(); it++) #define fr(i, n) for (lli i = 0; i < (n); i++) #define inc(i, a, b) for (lli i = a; i <= b; i++) #define dec(i, a, b) for (lli i = a; i >= b; i--) #define fr2(i, n) for (int i = 0; i < (n); i++) #define inc2(i, a, b) for (int i = a; i <= b; i++) #define dec2(i, a, b) for (int i = a; i >= b; i--) #define ymin(a, b) (a = min((a), (b))) #define ymax(a, b) (a = max((a), (b))) #define sz(x) (lli)(x).size() #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...); } typedef long long int lli; typedef long double ld; typedef pair<int, int> pii; typedef pair<lli, lli> plli; typedef vector<lli> vlli; typedef vector<int> vi; // typedef vector<vector<lli> >ncr(1001,vector<lli>(1001,0)) typedef vector<plli> vplli; long long MOD = 1000000009; #define addm(x, y) (x + y >= MOD ? (x + y - MOD) : (x + y)) lli n; lli a[3001]; lli dp[3001][3001]; lli vis[3001][3001]; lli solve(lli i, lli j) { if (vis[i][j]) return dp[i][j]; if (i == j) return dp[i][j] = a[i]; return dp[i][j] = max(a[i] - solve(i + 1, j), a[j] - solve(i, j - 1)); } int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif IOS // clock_t start,end; // start=clock(); // int t; // cin>>t; // for(int i=1;i<=t;i++) // { // cout<<"Case #"<<i<<": "; // solve(); // } cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; // mset(dp,-MOD*MOD); mset(dp, false); cout << solve(0, n - 1) << endl; // cout<<dp[0][n-1]<<endl; // end=clock(); // double time_taken=double(end-start)/double(CLOCKS_PER_SEC); // cout<<fixed<<setprecision(10)<<time_taken<<" sec"<<endl; }
// YA TOH WOH AAGE SE LEGA YA PHIR PICHE SE // TOH BAS USKA MAXIMUM NIKALNA HAI // DP[I][J] MAXIMUM OF X-Y IN THE SUBARRAY FROM I TO J #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define ff first #define ss second #define eb emplace_back #define MP make_pair #define pb push_back #define pf push_front #define ins insert #define endl '\n' #define all(v) (v).begin(), (v).end() #define mset(m, v) memset(m, v, sizeof(m)) #define tr(c, it) for (auto it = c.begin(); it != c.end(); it++) #define fr(i, n) for (lli i = 0; i < (n); i++) #define inc(i, a, b) for (lli i = a; i <= b; i++) #define dec(i, a, b) for (lli i = a; i >= b; i--) #define fr2(i, n) for (int i = 0; i < (n); i++) #define inc2(i, a, b) for (int i = a; i <= b; i++) #define dec2(i, a, b) for (int i = a; i >= b; i--) #define ymin(a, b) (a = min((a), (b))) #define ymax(a, b) (a = max((a), (b))) #define sz(x) (lli)(x).size() #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...); } typedef long long int lli; typedef long double ld; typedef pair<int, int> pii; typedef pair<lli, lli> plli; typedef vector<lli> vlli; typedef vector<int> vi; // typedef vector<vector<lli> >ncr(1001,vector<lli>(1001,0)) typedef vector<plli> vplli; long long MOD = 1000000009; #define addm(x, y) (x + y >= MOD ? (x + y - MOD) : (x + y)) lli n; lli a[3001]; lli dp[3001][3001]; lli vis[3001][3001]; lli solve(lli i, lli j) { if (vis[i][j]) return dp[i][j]; vis[i][j] = true; if (i == j) return dp[i][j] = a[i]; return dp[i][j] = max(a[i] - solve(i + 1, j), a[j] - solve(i, j - 1)); } int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif IOS // clock_t start,end; // start=clock(); // int t; // cin>>t; // for(int i=1;i<=t;i++) // { // cout<<"Case #"<<i<<": "; // solve(); // } cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; // mset(dp,-MOD*MOD); mset(dp, false); cout << solve(0, n - 1) << endl; // cout<<dp[0][n-1]<<endl; // end=clock(); // double time_taken=double(end-start)/double(CLOCKS_PER_SEC); // cout<<fixed<<setprecision(10)<<time_taken<<" sec"<<endl; }
insert
63
63
63
64
TLE
p03171
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; const int N = 305; int dp[N][N]; signed main() { fastio int n; cin >> n; int a[n]; for (auto &x : a) cin >> x; for (int i = n - 1; i >= 0; i--) { for (int j = i; j < n; j++) { if (i == j) dp[i][j] = a[i]; else dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]); } } cout << dp[0][n - 1] << "\n"; }
#include <bits/stdc++.h> #define int long long #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; const int N = 3005; int dp[N][N]; signed main() { fastio int n; cin >> n; int a[n]; for (auto &x : a) cin >> x; for (int i = n - 1; i >= 0; i--) { for (int j = i; j < n; j++) { if (i == j) dp[i][j] = a[i]; else dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]); } } cout << dp[0][n - 1] << "\n"; }
replace
10
11
10
11
0
p03171
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less_equal<int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; #define ll long long #define pb push_back #define F first #define S second #define all(c) c.begin(), c.end() #define llv vector<ll> #define mp make_pair #define endl "\n" const int N = (int)3e3 + 10; const int M = (int)1000000007; ll dp[N][N], a[N]; bool mark[N][N] = {}; ll check(int be, int en) { if (mark[be][en]) return dp[be][en]; if (en == be + 1) { mark[be][en] = 1; ll d = abs(a[en] - a[be]); return dp[be][en] = d; } mark[be][en] = 1; return dp[be][en] = max(a[be] - check(be + 1, en), a[en] - check(be, en - 1)); } int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, i; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } cout << check(0, n - 1); 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 ordered_set \ tree<int, null_type, less_equal<int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; #define ll long long #define pb push_back #define F first #define S second #define all(c) c.begin(), c.end() #define llv vector<ll> #define mp make_pair #define endl "\n" const int N = (int)3e3 + 10; const int M = (int)1000000007; ll dp[N][N], a[N]; bool mark[N][N] = {}; ll check(int be, int en) { if (mark[be][en]) return dp[be][en]; if (en == be + 1) { mark[be][en] = 1; ll d = abs(a[en] - a[be]); return dp[be][en] = d; } mark[be][en] = 1; return dp[be][en] = max(a[be] - check(be + 1, en), a[en] - check(be, en - 1)); } int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, i; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } if (n == 1) cout << a[0]; else cout << check(0, n - 1); return 0; }
replace
47
48
47
51
0
p03171
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define f first #define s second #define int long long #define pb push_back #define pencho \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define N 1005 #define mod 1000000007 int dp[N][N]; int a[N]; int solveDp(int index_1, int index_2) { if (index_1 > index_2) return 0; if (dp[index_1][index_2] != -1) return dp[index_1][index_2]; dp[index_1][index_2] = max(a[index_1] - solveDp(index_1 + 1, index_2), a[index_2] - solveDp(index_1, index_2 - 1)); return dp[index_1][index_2]; } void solve() { pencho int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; memset(dp, -1, sizeof(dp)); for (int i = 0; i < n; i++) dp[i][i] = a[i]; cout << solveDp(0, n - 1); } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("o.txt", "w", stdout); #endif solve(); }
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define f first #define s second #define int long long #define pb push_back #define pencho \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define N 3005 #define mod 1000000007 int dp[N][N]; int a[N]; int solveDp(int index_1, int index_2) { if (index_1 > index_2) return 0; if (dp[index_1][index_2] != -1) return dp[index_1][index_2]; dp[index_1][index_2] = max(a[index_1] - solveDp(index_1 + 1, index_2), a[index_2] - solveDp(index_1, index_2 - 1)); return dp[index_1][index_2]; } void solve() { pencho int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; memset(dp, -1, sizeof(dp)); for (int i = 0; i < n; i++) dp[i][i] = a[i]; cout << solveDp(0, n - 1); } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("o.txt", "w", stdout); #endif solve(); }
replace
10
11
10
11
TLE
p03171
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define F first #define S second // cout<<fixed<<setprecision(11); const string nl = "\n"; const ll MOD = 1e9 + 7; const ll ARR_MAX = 3e3 + 1; const ll INF = 1e14; ll dp[ARR_MAX] [ARR_MAX]; // dp[i][j] = maximum value of X-Y when a[i ... j] is left. void solve() { int n; cin >> n; ll a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { dp[i][i] = a[i]; } int i = 0; for (int d = 1; d < n; d++) { for (i = 0; i + d < n; i++) { dp[i][i + d] = max(a[i] - dp[i + 1][i + d], a[i + d] - dp[i][i + d - 1]); } } cout << dp[0][n - 1] << nl; } int main() { fastio(); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define F first #define S second // cout<<fixed<<setprecision(11); const string nl = "\n"; const ll MOD = 1e9 + 7; const ll ARR_MAX = 3e3 + 1; const ll INF = 1e14; ll dp[ARR_MAX] [ARR_MAX]; // dp[i][j] = maximum value of X-Y when a[i ... j] is left. void solve() { int n; cin >> n; ll a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { dp[i][i] = a[i]; } int i = 0; for (int d = 1; d < n; d++) { for (i = 0; i + d < n; i++) { dp[i][i + d] = max(a[i] - dp[i + 1][i + d], a[i + d] - dp[i][i + d - 1]); } } cout << dp[0][n - 1] << nl; } int main() { fastio(); int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
delete
43
47
43
43
0
p03171
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <numeric> #include <queue> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long LL; const int MOD = 1e9 + 7; LL dfs(int x, int y, vector<vector<LL>> &dp, vector<LL> &sif) { if (x > y) return 0ll; return max(sif[y + 1] - sif[x] - dfs(x + 1, y, dp, sif), sif[y + 1] - sif[x] - dfs(x, y - 1, dp, sif)); } void solve() { int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; vector<LL> sif(n + 1, 0); for (int i = 0; i < n; i++) { sif[i + 1] = sif[i] + nums[i]; } vector<vector<LL>> dp(n, vector<LL>(n, 0ll)); cout << 2ll * dfs(0, n - 1, dp, sif) - sif[n] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T = 1; // cin>>T; while (T--) { solve(); } return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <numeric> #include <queue> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long LL; const int MOD = 1e9 + 7; LL dfs(int x, int y, vector<vector<LL>> &dp, vector<LL> &sif) { if (x > y) return 0ll; return max(sif[y + 1] - sif[x] - dfs(x + 1, y, dp, sif), sif[y + 1] - sif[x] - dfs(x, y - 1, dp, sif)); } void solve() { int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; vector<LL> sif(n + 1, 0); for (int i = 0; i < n; i++) { sif[i + 1] = sif[i] + nums[i]; } vector<vector<LL>> dp(n, vector<LL>(n, 0ll)); for (int i = 0; i < n; i++) { dp[i][i] = nums[i]; } for (int len = 2; len <= n; len++) { for (int i = 0; i < (n - len + 1); i++) { int j = i + len - 1; dp[i][j] = max(sif[j + 1] - sif[i] - dp[i + 1][j], sif[j + 1] - sif[i] - dp[i][j - 1]); } } cout << 2ll * dp[0][n - 1] - sif[n] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T = 1; // cin>>T; while (T--) { solve(); } return 0; }
replace
32
33
32
43
TLE
p03171
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define dd double #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, a, b) for (int i = a; i < b; i++) #define rep1(i, b) for (int i = 1; i <= b; i++) #define pb push_back #define mp make_pair #define clr(x) x.clear() #define sz(x) ((int)(x).size()) #define F first #define S second #define vec vector<int, int> #define int long long #define pii pair<int, int> int visited[100001]; // int ax,ay,bx,by,t_x0,t_y0; int dp[1001][1001]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // ios_base& scientific (ios_base& str); int t = 1; // cin>>t; while (t--) { int n; cin >> n; int arr[n]; int sum = 0; rep(i, n) { cin >> arr[i]; sum += arr[i]; } // sort(arr,arr+n); for (int i = 0; i < n; i++) { dp[i][i] = arr[i]; } for (int i = 0; i < n - 1; i++) { dp[i][i + 1] = max(arr[i], arr[i + 1]); } int a = 2; while (1) { if (a >= n) break; for (int i = 0; i < n - a; i++) { int j = i + a; dp[i][i + a] = max(arr[i] + min(dp[i + 2][j], dp[i + 1][j - 1]), arr[j] + min(dp[i + 1][j - 1], dp[i][j - 2])); } a++; } cout << (2 * dp[0][n - 1] - sum) << endl; } // exit(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define dd double #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, a, b) for (int i = a; i < b; i++) #define rep1(i, b) for (int i = 1; i <= b; i++) #define pb push_back #define mp make_pair #define clr(x) x.clear() #define sz(x) ((int)(x).size()) #define F first #define S second #define vec vector<int, int> #define int long long #define pii pair<int, int> int visited[100001]; // int ax,ay,bx,by,t_x0,t_y0; int dp[3001][3001]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // ios_base& scientific (ios_base& str); int t = 1; // cin>>t; while (t--) { int n; cin >> n; int arr[n]; int sum = 0; rep(i, n) { cin >> arr[i]; sum += arr[i]; } // sort(arr,arr+n); for (int i = 0; i < n; i++) { dp[i][i] = arr[i]; } for (int i = 0; i < n - 1; i++) { dp[i][i + 1] = max(arr[i], arr[i + 1]); } int a = 2; while (1) { if (a >= n) break; for (int i = 0; i < n - a; i++) { int j = i + a; dp[i][i + a] = max(arr[i] + min(dp[i + 2][j], dp[i + 1][j - 1]), arr[j] + min(dp[i + 1][j - 1], dp[i][j - 2])); } a++; } cout << (2 * dp[0][n - 1] - sum) << endl; } // exit(); return 0; }
replace
20
21
20
21
0
p03171
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define PB push_back #define ALL(x) (x).begin(), (x).end() #define ALLR(x) (x).rbegin(), (x).rend() #define MP make_pair #define debug(_x) cerr << #_x << '=' << (_x) << endl; #define debug2(_x, _y) \ cerr << #_x << '=' << (_x) << ' ' << #_y << '=' << (_y) << endl; #define debugn(_a) \ cerr << #_a << ": "; \ for (const auto _x : _a) \ cerr << (_x) << ' '; \ cerr << endl; #define HERE debug(__LINE__); #define FAST_IO \ ios::sync_with_stdio(false); \ cin.tie(0) #define int long long int typedef pair<int, int> ii; typedef pair<pair<int, int>, int> iii; typedef vector<int> vi; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<ii> vii; typedef vector<string> vs; typedef vector<bool> vb; typedef unsigned long long ull; const int INF = LONG_LONG_MAX >> 1; const int NINF = LONG_LONG_MIN / 2; vi a; const int N = 3001; ii dp[N][N][2]; pair<int, int> solve(int l, int r, bool turn) { if (dp[l][r][turn] != ii{INF, INF}) return dp[l][r][turn]; if (l > r) return dp[l][r][turn] = ii{0, 0}; if (turn) { ii p = solve(l + 1, r, !turn); ii q = solve(l, r - 1, !turn); p.first += a[l]; q.first += a[r]; if (p.first - p.second > q.first - q.second) { return dp[l][r][turn] = p; } else return dp[l][r][turn] = q; } else { ii p = solve(l + 1, r, !turn); ii q = solve(l, r - 1, !turn); p.second += a[l]; q.second += a[r]; if (p.first - p.second < q.first - q.second) { return dp[l][r][turn] = p; } else return dp[l][r][turn] = q; } } int32_t main() { FAST_IO; int n; cin >> n; a.resize(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) for (int k = 0; k < 2; k++) dp[i][j][k] = {INF, INF}; ii ans = solve(1, n, true); cout << ans.first - ans.second << endl; } // g++ -Wall -Wextra -Wshadow -fsanitize=undefined
#include <bits/stdc++.h> using namespace std; #define PB push_back #define ALL(x) (x).begin(), (x).end() #define ALLR(x) (x).rbegin(), (x).rend() #define MP make_pair #define debug(_x) cerr << #_x << '=' << (_x) << endl; #define debug2(_x, _y) \ cerr << #_x << '=' << (_x) << ' ' << #_y << '=' << (_y) << endl; #define debugn(_a) \ cerr << #_a << ": "; \ for (const auto _x : _a) \ cerr << (_x) << ' '; \ cerr << endl; #define HERE debug(__LINE__); #define FAST_IO \ ios::sync_with_stdio(false); \ cin.tie(0) #define int long long int typedef pair<int, int> ii; typedef pair<pair<int, int>, int> iii; typedef vector<int> vi; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<ii> vii; typedef vector<string> vs; typedef vector<bool> vb; typedef unsigned long long ull; const int INF = LONG_LONG_MAX >> 1; const int NINF = LONG_LONG_MIN / 2; vi a; const int N = 3009; ii dp[N][N][2]; pair<int, int> solve(int l, int r, bool turn) { if (dp[l][r][turn] != ii{INF, INF}) return dp[l][r][turn]; if (l > r) return dp[l][r][turn] = ii{0, 0}; if (turn) { ii p = solve(l + 1, r, !turn); ii q = solve(l, r - 1, !turn); p.first += a[l]; q.first += a[r]; if (p.first - p.second > q.first - q.second) { return dp[l][r][turn] = p; } else return dp[l][r][turn] = q; } else { ii p = solve(l + 1, r, !turn); ii q = solve(l, r - 1, !turn); p.second += a[l]; q.second += a[r]; if (p.first - p.second < q.first - q.second) { return dp[l][r][turn] = p; } else return dp[l][r][turn] = q; } } int32_t main() { FAST_IO; int n; cin >> n; a.resize(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) for (int k = 0; k < 2; k++) dp[i][j][k] = {INF, INF}; ii ans = solve(1, n, true); cout << ans.first - ans.second << endl; } // g++ -Wall -Wextra -Wshadow -fsanitize=undefined
replace
41
42
41
42
-11
p03171
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int a[5555]; int cache[5555][5555]; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int solve(int start, int end) { if (start > end) { return 0; } if (start == end) { return a[start]; } if (cache[start][end] != -1) { return cache[start][end]; } return cache[start][end] = max( a[start] + min(solve(start + 2, end), solve(start + 1, end - 1)), a[end] + min(solve(start, end - 2), solve(start + 1, end - 1))); } int32_t main() { c_p_c(); memset(cache, -1, sizeof(cache)); int n, ts = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; ts += a[i]; } int x = solve(0, n - 1); int y = ts - x; cout << x - y << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int a[5555]; int cache[5555][5555]; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int solve(int start, int end) { if (start > end) { return 0; } if (start == end) { return a[start]; } if (cache[start][end] != -1) { return cache[start][end]; } return cache[start][end] = max( a[start] + min(solve(start + 2, end), solve(start + 1, end - 1)), a[end] + min(solve(start, end - 2), solve(start + 1, end - 1))); } int32_t main() { // c_p_c(); memset(cache, -1, sizeof(cache)); int n, ts = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; ts += a[i]; } int x = solve(0, n - 1); int y = ts - x; cout << x - y << endl; return 0; }
replace
47
48
47
48
-11
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define vll vector<long long> #define MEM(var, val) memset(var, (val), sizeof(var)) const int M = 1e9 + 7; vll a; ll n, k; vector<vll> dp; vll pref; void dist(ll i) { for (int j = 0; j <= k; j++) if (j > a[i]) dp[i][j] = (pref[j] - pref[j - a[i] - 1] + M) % M; else dp[i][j] = pref[j]; pref[0] = dp[i][0]; for (int j = 1; j <= k; j++) pref[j] = (pref[j - 1] + dp[i][j]) % M; if (i > 0) dist(i - 1); return; } int main() { cin >> n >> k; dp.resize(n + 1, vll(k + 1, 0)); a.resize(n + 1); for (int i = 0; i < n; i++) cin >> a[i]; for (int j = 0; j <= a[n - 1]; j++) dp[n - 1][j] = 1; pref.resize(k + 1, 0); pref[0] = 1; for (int j = 1; j <= k; j++) pref[j] = pref[j - 1] + dp[n - 1][j]; // MEM(dp,-1); dist(n - 2); cout << dp[0][k] % M; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define vll vector<long long> #define MEM(var, val) memset(var, (val), sizeof(var)) const int M = 1e9 + 7; vll a; ll n, k; vector<vll> dp; vll pref; void dist(ll i) { for (int j = 0; j <= k; j++) if (j > a[i]) dp[i][j] = (pref[j] - pref[j - a[i] - 1] + M) % M; else dp[i][j] = pref[j]; pref[0] = dp[i][0]; for (int j = 1; j <= k; j++) pref[j] = (pref[j - 1] + dp[i][j]) % M; if (i > 0) dist(i - 1); return; } int main() { cin >> n >> k; dp.resize(n + 1, vll(k + 1, 0)); a.resize(n + 1); for (int i = 0; i < n; i++) cin >> a[i]; for (int j = 0; j <= a[n - 1]; j++) dp[n - 1][j] = 1; pref.resize(k + 1, 0); pref[0] = 1; for (int j = 1; j <= k; j++) pref[j] = pref[j - 1] + dp[n - 1][j]; // MEM(dp,-1); if (n >= 2) dist(n - 2); cout << dp[0][k] % M; }
replace
38
39
38
40
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long int #define MOD 1000000007 #define INF 100000000000000 // INF - 1e14 #define vi vector<ll> #define pb push_back #define mpl map<ll, ll> #define ff first #define ss second #define pll pair<ll, ll> #define vpll vector<pair<ll, ll>> #define fer(i, a, n) for (ll i = a; i <= n; i++) #define fdr(i, a, n) for (ll i = a; i >= n; i--) #define invec(n, v) \ vi v(n); \ fer(i, 0, n - 1) cin >> v[i] #define insvec(n, v) \ rin(n); \ vi v(n); \ fer(i, 0, n - 1) cin >> v[i] #define MK2DV(v, row, col, val) vector<vector<ll>> v(row, vector<ll>(col, val)) #define matrix vector<vector<ll>> #define printpartition \ cout << "-------------------------------------------------------------" \ << endl; #define printmat(mat, row, col) \ cout << #mat << "::" << endl; \ fer(i, 0, row - 1) fer(j, 0, col - 1) cout << mat[i][j] \ << " \n"[j == col - 1]; \ printpartition; using namespace std; using namespace __gnu_pbds; #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> void __print(int x) { cout << x; } void __print(long x) { cout << x; } void __print(long long x) { cout << x; } void __print(unsigned x) { cout << x; } void __print(unsigned long x) { cout << x; } void __print(unsigned long long x) { cout << x; } void __print(float x) { cout << x; } void __print(double x) { cout << x; } void __print(long double x) { cout << x; } void __print(char x) { cout << '\'' << x << '\''; } void __print(const char *x) { cout << '\"' << x << '\"'; } void __print(const string &x) { cout << '\"' << x << '\"'; } void __print(bool x) { cout << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; } template <typename T> void __print(const T &x) { int f = 0; cout << '{'; for (auto &i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; } void _print() { cout << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); } template <typename T> inline void inp(T &any) { cin >> any; } template <typename T, typename... U> inline void inp(T &a, U &...b) { cin >> a; inp(b...); } #define rin(args...) \ ll args; \ inp(args); #ifndef ONLINE_JUDGE #define debug(x...) \ cout << "[" << #x << "] = ["; \ _print(x) #define matdeb(mat, row, col) printmat(mat, row, col); #else #define debug(x...) #define matdeb(mat, row, col) #endif ll fast_pow(ll base, ll exp, ll M = MOD) { base = base % M; ll result = 1; while (exp > 0) { if (exp % 2 == 1) result = (result * base) % M; base = (base * base) % M; exp = exp / 2; } return result; } ll modInverse(ll A, ll M = MOD) { return fast_pow(A, M - 2, M); } // only when M is prime string yes = "Yes", no = "No"; const int N = 100; const int K = 100000; const int opti = 2; ll dp[2][K + 1]; ll pref[2][K + 1]; ll prefv[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("intt.txt", "r", stdin); freopen("outt.txt", "w", stdout); #endif ll test_cases = 1; // cin >> test_cases; for (ll test_case = 1; test_case <= test_cases; test_case++) { // cout << "Case #" << test_case << ": "; rin(n, k); invec(n, v); fer(i, 0, n - 1) { if (i == 0) prefv[i] = v[i]; else prefv[i] = prefv[i - 1] + v[i]; } fer(i, 0, n - 1) { fer(j, 0, k) { if (!i) { if (!j) { dp[0][0] = pref[0][0] = 1; } else { dp[0][j] = (j <= v[0]); pref[0][j] = pref[0][j - 1] + dp[0][j]; } continue; } dp[i & 1][j] = (pref[(i - 1) & 1][j] - pref[(i - 1) & 1][max(0LL, j - v[i])] + dp[(i - 1) & 1][max(0LL, j - v[i])] + MOD) % MOD; pref[i & 1][j] = (pref[i & 1][max(0LL, j - 1)] + dp[i & 1][j]) % MOD; } } cout << dp[(n - 1) & 1][k]; } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long int #define MOD 1000000007 #define INF 100000000000000 // INF - 1e14 #define vi vector<ll> #define pb push_back #define mpl map<ll, ll> #define ff first #define ss second #define pll pair<ll, ll> #define vpll vector<pair<ll, ll>> #define fer(i, a, n) for (ll i = a; i <= n; i++) #define fdr(i, a, n) for (ll i = a; i >= n; i--) #define invec(n, v) \ vi v(n); \ fer(i, 0, n - 1) cin >> v[i] #define insvec(n, v) \ rin(n); \ vi v(n); \ fer(i, 0, n - 1) cin >> v[i] #define MK2DV(v, row, col, val) vector<vector<ll>> v(row, vector<ll>(col, val)) #define matrix vector<vector<ll>> #define printpartition \ cout << "-------------------------------------------------------------" \ << endl; #define printmat(mat, row, col) \ cout << #mat << "::" << endl; \ fer(i, 0, row - 1) fer(j, 0, col - 1) cout << mat[i][j] \ << " \n"[j == col - 1]; \ printpartition; using namespace std; using namespace __gnu_pbds; #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> void __print(int x) { cout << x; } void __print(long x) { cout << x; } void __print(long long x) { cout << x; } void __print(unsigned x) { cout << x; } void __print(unsigned long x) { cout << x; } void __print(unsigned long long x) { cout << x; } void __print(float x) { cout << x; } void __print(double x) { cout << x; } void __print(long double x) { cout << x; } void __print(char x) { cout << '\'' << x << '\''; } void __print(const char *x) { cout << '\"' << x << '\"'; } void __print(const string &x) { cout << '\"' << x << '\"'; } void __print(bool x) { cout << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; } template <typename T> void __print(const T &x) { int f = 0; cout << '{'; for (auto &i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; } void _print() { cout << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); } template <typename T> inline void inp(T &any) { cin >> any; } template <typename T, typename... U> inline void inp(T &a, U &...b) { cin >> a; inp(b...); } #define rin(args...) \ ll args; \ inp(args); #ifndef ONLINE_JUDGE #define debug(x...) \ cout << "[" << #x << "] = ["; \ _print(x) #define matdeb(mat, row, col) printmat(mat, row, col); #else #define debug(x...) #define matdeb(mat, row, col) #endif ll fast_pow(ll base, ll exp, ll M = MOD) { base = base % M; ll result = 1; while (exp > 0) { if (exp % 2 == 1) result = (result * base) % M; base = (base * base) % M; exp = exp / 2; } return result; } ll modInverse(ll A, ll M = MOD) { return fast_pow(A, M - 2, M); } // only when M is prime string yes = "Yes", no = "No"; const int N = 100; const int K = 100000; const int opti = 2; ll dp[2][K + 1]; ll pref[2][K + 1]; ll prefv[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll test_cases = 1; // cin >> test_cases; for (ll test_case = 1; test_case <= test_cases; test_case++) { // cout << "Case #" << test_case << ": "; rin(n, k); invec(n, v); fer(i, 0, n - 1) { if (i == 0) prefv[i] = v[i]; else prefv[i] = prefv[i - 1] + v[i]; } fer(i, 0, n - 1) { fer(j, 0, k) { if (!i) { if (!j) { dp[0][0] = pref[0][0] = 1; } else { dp[0][j] = (j <= v[0]); pref[0][j] = pref[0][j - 1] + dp[0][j]; } continue; } dp[i & 1][j] = (pref[(i - 1) & 1][j] - pref[(i - 1) & 1][max(0LL, j - v[i])] + dp[(i - 1) & 1][max(0LL, j - v[i])] + MOD) % MOD; pref[i & 1][j] = (pref[i & 1][max(0LL, j - 1)] + dp[i & 1][j]) % MOD; } } cout << dp[(n - 1) & 1][k]; } return 0; }
delete
114
119
114
114
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define rep(i, n) for (int i = 0; i < (int)n; i++) const ll INF = (1LL << 60); const int MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int N, K; vector<int> a(110); vector<vector<ll>> dp(110, vector<ll>(100010, 0)); int main() { cin >> N >> K; rep(i, N) { cin >> a[i]; } vector<ll> sum(K + 1, 0); dp[0][0] = 1; for (int i = 0; i < N; i++) { sum[0] = 0; for (int j = 0; j <= K; j++) sum[j + 1] = (sum[j] + dp[i][j]) % MOD; for (int j = 0; j <= K; j++) dp[i + 1][j] = (sum[j + 1] - sum[max(0, j - a[i])] + MOD) % MOD; } cout << dp[N][K] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define rep(i, n) for (int i = 0; i < (int)n; i++) const ll INF = (1LL << 60); const int MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int N, K; vector<int> a(110); vector<vector<ll>> dp(110, vector<ll>(100010, 0)); int main() { cin >> N >> K; rep(i, N) { cin >> a[i]; } vector<ll> sum(100010, 0); dp[0][0] = 1; for (int i = 0; i < N; i++) { sum[0] = 0; for (int j = 0; j <= K; j++) sum[j + 1] = (sum[j] + dp[i][j]) % MOD; for (int j = 0; j <= K; j++) dp[i + 1][j] = (sum[j + 1] - sum[max(0, j - a[i])] + MOD) % MOD; } cout << dp[N][K] << endl; }
replace
30
31
30
31
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03172
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; long long dp[110][1000010], sdp[1000010], a[1100], sa[1100], n, m; int main() { sa[0] = 0; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i], sa[i] = max(sa[i - 1] + a[i], m); memset(dp, 0, sizeof dp); dp[0][0] = 1; for (int i = 1; i <= n; i++) { sdp[0] = 1; for (long long k = 1; k <= (sa[i] + 100); k++) sdp[k] = sdp[k - 1] + dp[i - 1][k]; for (long long j = 0; j <= sa[i]; j++) { if (j <= sa[i]) dp[i][j] = sdp[j] - ((j - a[i] - 1) >= 0 ? sdp[j - a[i] - 1] : 0); dp[i][j] %= 1000000007; } } cout << dp[n][m]; }
#include <bits/stdc++.h> using namespace std; long long dp[110][1000010], sdp[1000010], a[1100], sa[1100], n, m; int main() { sa[0] = 0; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i], sa[i] = min(sa[i - 1] + a[i], m); memset(dp, 0, sizeof dp); dp[0][0] = 1; for (int i = 1; i <= n; i++) { sdp[0] = 1; for (long long k = 1; k <= (sa[i] + 100); k++) sdp[k] = sdp[k - 1] + dp[i - 1][k]; for (long long j = 0; j <= sa[i]; j++) { if (j <= sa[i]) dp[i][j] = sdp[j] - ((j - a[i] - 1) >= 0 ? sdp[j - a[i] - 1] : 0); dp[i][j] %= 1000000007; } } cout << dp[n][m]; }
replace
9
10
9
10
TLE
p03172
C++
Runtime Error
/*Knapsack problem Input: N W w1 v1 w2 v2 . . . wn vn Output: print ma possible sum of the values if time that knapsack of capacity W can contain. */ #include <bits/stdc++.h> using namespace std; #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__) << "] " const long long MOD = 1e9 + 7; using ll = long long; void add_self(int &a, int b) { a += b; if (a >= MOD) { a -= MOD; } } void sub_self(int &a, int b) { a -= b; if (a <= 0) { a += MOD; } } int main() { int N, K; cin >> N >> K; vector<int> dp(K + 1); // 0-K candies dp[i] = count when i candies used dp[0] = 1; for (int child = 0; child < N; ++child) { int up_to; cin >> up_to; vector<int> fake(K + 1); for (int used = K; used >= 0; --used) { int l = used + 1; int r = used + min(up_to, K - used); if (l <= r) { add_self(fake[l], dp[used]); sub_self(fake[r + 1], dp[used]); } } int prefix_sum = 0; for (int i = 0; i <= K; ++i) { add_self(prefix_sum, fake[i]); add_self(dp[i], prefix_sum); } } printf("%d\n", dp[K]); }
/*Knapsack problem Input: N W w1 v1 w2 v2 . . . wn vn Output: print ma possible sum of the values if time that knapsack of capacity W can contain. */ #include <bits/stdc++.h> using namespace std; #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__) << "] " const long long MOD = 1e9 + 7; using ll = long long; void add_self(int &a, int b) { a += b; if (a >= MOD) { a -= MOD; } } void sub_self(int &a, int b) { a -= b; if (a <= 0) { a += MOD; } } int main() { int N, K; cin >> N >> K; vector<int> dp(K + 1); // 0-K candies dp[i] = count when i candies used dp[0] = 1; for (int child = 0; child < N; ++child) { int up_to; cin >> up_to; vector<int> fake(K + 1); for (int used = K; used >= 0; --used) { int l = used + 1; int r = used + min(up_to, K - used); if (l <= r) { add_self(fake[l], dp[used]); if (r + 1 <= K) { sub_self(fake[r + 1], dp[used]); } } } int prefix_sum = 0; for (int i = 0; i <= K; ++i) { add_self(prefix_sum, fake[i]); add_self(dp[i], prefix_sum); } } printf("%d\n", dp[K]); }
replace
83
84
83
86
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { int n, k; cin >> n >> k; vector<int> pref(k), dp(k); dp[0] = 1; for (int kid = 1; kid <= n; kid++) { int c; cin >> c; pref[0] = dp[0]; dp[0] = 0; for (int i = 1; i <= k; i++) { pref[i] = (pref[i - 1] + dp[i]) % MOD; } for (int i = 0; i <= k; i++) { dp[i] = (pref[i] - (i - c >= 0 ? pref[i - c - 1] : 0)) % MOD; } } int ans = dp[k]; if (ans < 0) ans += MOD; printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { int n, k; cin >> n >> k; vector<int> pref(k + 1), dp(k + 1); dp[0] = 1; for (int kid = 1; kid <= n; kid++) { int c; cin >> c; pref[0] = dp[0]; dp[0] = 0; for (int i = 1; i <= k; i++) { pref[i] = (pref[i - 1] + dp[i]) % MOD; } for (int i = 0; i <= k; i++) { dp[i] = (pref[i] - (i - c >= 0 ? pref[i - c - 1] : 0)) % MOD; } } int ans = dp[k]; if (ans < 0) ans += MOD; printf("%d\n", ans); return 0; }
replace
9
10
9
10
0
p03172
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unistd.h> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<bool> vb; typedef vector<vector<bool>> vvb; typedef vector<string> vs; typedef vector<vector<string>> vvs; typedef vector<char> vc; typedef vector<vector<char>> vvc; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<vvd> vvvd; typedef pair<int, int> P; typedef pair<ll, ll> PL; typedef vector<P> vp; typedef vector<PL> vpl; typedef vector<vector<P>> vvp; typedef vector<vector<PL>> vvpl; const int INF = 1001001001; const ll LINF = 1e12; const double pi = 3.1415926535897932; const string endstr = "\n"; #define FOR(i, a, b) for (ll i = (a); i < b; i++) #define REP(i, n) for (ll i = 0; i < n; i++) #define FORMAP(it, m) for (auto it = m.begin(); it != m.end(); it++) #define ff first #define ss second #define pb push_back template <typename T> T gcd(T a, T b) { return (a == 0) ? b : gcd(b % a, a); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } bool p_comp_fs(const PL p1, const PL p2) { return p1.first < p2.first; }; bool p_comp_fg(const PL p1, const PL p2) { return p1.first > p2.first; }; bool p_comp_ss(const PL p1, const PL p2) { return p1.second < p2.second; }; bool p_comp_sg(const PL p1, const PL p2) { return p1.second > p2.second; }; template <typename T> vector<T> uniquen(vector<T> vec) { vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } const ll mod = 1e9 + 7; int main() { ll N, K; cin >> N >> K; vl a(N); REP(i, N) cin >> a[i]; vvl dp(N + 1, vl(K + 1, 0)); REP(i, K + 1) if (i <= a[0]) dp[1][i] = 1; FOR(i, 1, N) { vl sum(K + 1, 0); REP(j, K + 1) sum[j + 1] = (sum[j] + dp[i][j]) % mod; REP(l, K + 1) { dp[i + 1][l] = (sum[l + 1] - sum[max(l - a[i], 0ll)] + mod) % mod; } } cout << dp[N][K] % mod << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unistd.h> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<bool> vb; typedef vector<vector<bool>> vvb; typedef vector<string> vs; typedef vector<vector<string>> vvs; typedef vector<char> vc; typedef vector<vector<char>> vvc; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<vvd> vvvd; typedef pair<int, int> P; typedef pair<ll, ll> PL; typedef vector<P> vp; typedef vector<PL> vpl; typedef vector<vector<P>> vvp; typedef vector<vector<PL>> vvpl; const int INF = 1001001001; const ll LINF = 1e12; const double pi = 3.1415926535897932; const string endstr = "\n"; #define FOR(i, a, b) for (ll i = (a); i < b; i++) #define REP(i, n) for (ll i = 0; i < n; i++) #define FORMAP(it, m) for (auto it = m.begin(); it != m.end(); it++) #define ff first #define ss second #define pb push_back template <typename T> T gcd(T a, T b) { return (a == 0) ? b : gcd(b % a, a); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } bool p_comp_fs(const PL p1, const PL p2) { return p1.first < p2.first; }; bool p_comp_fg(const PL p1, const PL p2) { return p1.first > p2.first; }; bool p_comp_ss(const PL p1, const PL p2) { return p1.second < p2.second; }; bool p_comp_sg(const PL p1, const PL p2) { return p1.second > p2.second; }; template <typename T> vector<T> uniquen(vector<T> vec) { vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } const ll mod = 1e9 + 7; int main() { ll N, K; cin >> N >> K; vl a(N); REP(i, N) cin >> a[i]; vvl dp(N + 1, vl(K + 1, 0)); REP(i, K + 1) if (i <= a[0]) dp[1][i] = 1; FOR(i, 1, N) { vl sum(K + 2, 0); REP(j, K + 1) sum[j + 1] = (sum[j] + dp[i][j]) % mod; REP(l, K + 1) { dp[i + 1][l] = (sum[l + 1] - sum[max(l - a[i], 0ll)] + mod) % mod; } } cout << dp[N][K] % mod << endl; return 0; }
replace
74
75
74
75
-6
munmap_chunk(): invalid pointer
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < n; i++) #define repl(i, l, r) for (ll i = (l); i < (r); i++) #define per(i, n) for (ll i = n - 1; i >= 0; i--) #define perl(i, r, l) for (ll i = r - 1; i >= l; i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x, vector<x>, greater<x>> #define all(x) (x).begin(), (x).end() #define CST(x) cout << fixed << setprecision(x) #define rev(x) reverse(x); using ll = long long; using vl = vector<ll>; using vvl = vector<vector<ll>>; using pl = pair<ll, ll>; using vpl = vector<pl>; using vvpl = vector<vpl>; const ll MOD = 1000000007; const ll MOD9 = 998244353; const int inf = 1e9 + 10; const ll INF = 4e18; const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0}; 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 mod = 1000000007; const int max_n = 200005; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } int main() { ll n, k; cin >> n >> k; vl a(n); rep(i, n) cin >> a[i]; vector<vector<mint>> dp(n + 1, vector<mint>(k + 1)), cum(n + 1, vector<mint>(k + 2)); dp[0][0] = 1; rep(i, k + 2) cum[0][i] = 1; cum[0][0] = 0; rep(i, n) { rep(j, k + 1) { dp[i + 1][j] += cum[i][j + 1]; if (j - a[i] >= 0) dp[i + 1][j] -= cum[i][j - a[i]]; } rep(j, k + 2) cum[i + 1][j + 1] += cum[i + 1][j] + dp[i + 1][j]; } /*rep(i,n+1){ rep(j,k+1){ cout << dp[i][j] <<" "; } cout << endl; } cout << endl; rep(i,n+1){ rep(j,k+2){ cout << cum[i][j] <<" "; } cout << endl; }*/ cout << dp[n][k] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < n; i++) #define repl(i, l, r) for (ll i = (l); i < (r); i++) #define per(i, n) for (ll i = n - 1; i >= 0; i--) #define perl(i, r, l) for (ll i = r - 1; i >= l; i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x, vector<x>, greater<x>> #define all(x) (x).begin(), (x).end() #define CST(x) cout << fixed << setprecision(x) #define rev(x) reverse(x); using ll = long long; using vl = vector<ll>; using vvl = vector<vector<ll>>; using pl = pair<ll, ll>; using vpl = vector<pl>; using vvpl = vector<vpl>; const ll MOD = 1000000007; const ll MOD9 = 998244353; const int inf = 1e9 + 10; const ll INF = 4e18; const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0}; 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 mod = 1000000007; const int max_n = 200005; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } int main() { ll n, k; cin >> n >> k; vl a(n); rep(i, n) cin >> a[i]; vector<vector<mint>> dp(n + 1, vector<mint>(k + 1)), cum(n + 1, vector<mint>(k + 2)); dp[0][0] = 1; rep(i, k + 2) cum[0][i] = 1; cum[0][0] = 0; rep(i, n) { rep(j, k + 1) { dp[i + 1][j] += cum[i][j + 1]; if (j - a[i] >= 0) dp[i + 1][j] -= cum[i][j - a[i]]; } rep(j, k + 1) cum[i + 1][j + 1] += cum[i + 1][j] + dp[i + 1][j]; } /*rep(i,n+1){ rep(j,k+1){ cout << dp[i][j] <<" "; } cout << endl; } cout << endl; rep(i,n+1){ rep(j,k+2){ cout << cum[i][j] <<" "; } cout << endl; }*/ cout << dp[n][k] << endl; }
replace
98
99
98
99
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define fi first #define se second #define all(v) v.begin(), v.end() #define allarr(a) a, a + n #define ll long long #define ull unsigned long long #define pb push_back #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef pair<int, pi> trp; typedef vector<pi> vpi; typedef vector<pll> vpll; // int ab (int x ) { return (x>0?x:-x); } const int F = 3e5 + 5; const ll mod = 1e9 + 7; ll dp[105][F]; int a[105]; int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { dp[i + 1][j] += dp[i][j]; // dp[i+1][j]%=mod; // if(j+a[i]<=k){ dp[i + 1][j + a[i] + 1] -= dp[i][j]; // dp[i+1][j+a[i]+1]%=mod; } dp[i + 1][0] %= mod; for (int j = 1; j <= k; j++) dp[i + 1][j] += dp[i + 1][j - 1], assert(dp[i + 1][j] >= 0), dp[i + 1][j] %= mod; } // for(int i = 1 ; i <= n ; i++ ){for(int j = 0 ; j <= k ; j++ )cout << // dp[i][j]<<" " ; cout<<endl; } cout << dp[n][k] << endl; }
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define fi first #define se second #define all(v) v.begin(), v.end() #define allarr(a) a, a + n #define ll long long #define ull unsigned long long #define pb push_back #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef pair<int, pi> trp; typedef vector<pi> vpi; typedef vector<pll> vpll; // int ab (int x ) { return (x>0?x:-x); } const int F = 3e5 + 5; const ll mod = 1e9 + 7; ll dp[105][F]; int a[105]; int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { dp[i + 1][j] += dp[i][j]; // dp[i+1][j]%=mod; // if(j+a[i]<=k){ dp[i + 1][j + a[i] + 1] -= dp[i][j]; // dp[i+1][j+a[i]+1]%=mod; } dp[i + 1][0] %= mod; for (int j = 1; j <= k; j++) dp[i + 1][j] += dp[i + 1][j - 1] + mod, assert(dp[i + 1][j] >= 0), dp[i + 1][j] %= mod; } // for(int i = 1 ; i <= n ; i++ ){for(int j = 0 ; j <= k ; j++ )cout << // dp[i][j]<<" " ; cout<<endl; } cout << dp[n][k] << endl; }
replace
42
43
42
43
-11
p03172
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <vector> using namespace std; #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) typedef long long ll; typedef pair<int, int> P; typedef pair<int, double> Pid; typedef pair<double, int> Pdi; const double PI = 3.1415926535897932; // acos(-1) const double EPS = 1e-15; const int INF = 1001001001; const int mod = 1e+9 + 7; ll dp[105][100005]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } dp[0][k] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j <= k; ++j) { int pos = max(0, j - a[i]); dp[i + 1][pos] = (dp[i + 1][pos] + dp[i][j]) % mod; dp[i + 1][j + 1] = (dp[i + 1][j + 1] - dp[i][j]) % mod; while (dp[i + 1][j + 1] < 0) dp[i + 1][j + 1] += mod; } for (int j = 0; j <= k; ++j) { dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i + 1][j]) % mod; } } cout << dp[n][0] << endl; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= k + 1; ++j) { cerr << dp[i][j] << " "; } cerr << "\n"; } }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <vector> using namespace std; #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) typedef long long ll; typedef pair<int, int> P; typedef pair<int, double> Pid; typedef pair<double, int> Pdi; const double PI = 3.1415926535897932; // acos(-1) const double EPS = 1e-15; const int INF = 1001001001; const int mod = 1e+9 + 7; ll dp[105][100005]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } dp[0][k] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j <= k; ++j) { int pos = max(0, j - a[i]); dp[i + 1][pos] = (dp[i + 1][pos] + dp[i][j]) % mod; dp[i + 1][j + 1] = (dp[i + 1][j + 1] - dp[i][j]) % mod; while (dp[i + 1][j + 1] < 0) dp[i + 1][j + 1] += mod; } for (int j = 0; j <= k; ++j) { dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i + 1][j]) % mod; } } cout << dp[n][0] << endl; }
delete
61
67
61
61
TLE
p03172
C++
Runtime Error
#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #define int long long using namespace std; int dp[105][200005]; int mod = 1e9 + 7; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("INPUT.txt", "r", stdin); freopen("OUTPUT.txt", "w", stdout); #endif clock_t clk; clk = clock(); // --------------Code starts // here--------------------------------------------------------------------- int n, k; cin >> n >> k; int a[n + 1]; for (int i = 1; i <= n; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { dp[i][j] = (dp[i][j] + dp[i - 1][j] + mod) % mod; dp[i][j + a[i] + 1] = (dp[i][j + a[i] + 1] + mod - dp[i - 1][j]); } for (int j = 1; j <= k; j++) { dp[i][j] += dp[i][j - 1]; dp[i][j] %= mod; } } cout << dp[n][k]; // -------------------------------------Code ends // here------------------------------------------------------ clk = clock() - clk; cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC << "\n"; return 0; }
#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #define int long long using namespace std; int dp[105][200005]; int mod = 1e9 + 7; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // #ifndef ONLINE_JUDGE // freopen ("INPUT.txt" , "r" , stdin); // freopen ("OUTPUT.txt" , "w" , stdout); // #endif clock_t clk; clk = clock(); // --------------Code starts // here--------------------------------------------------------------------- int n, k; cin >> n >> k; int a[n + 1]; for (int i = 1; i <= n; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { dp[i][j] = (dp[i][j] + dp[i - 1][j] + mod) % mod; dp[i][j + a[i] + 1] = (dp[i][j + a[i] + 1] + mod - dp[i - 1][j]); } for (int j = 1; j <= k; j++) { dp[i][j] += dp[i][j - 1]; dp[i][j] %= mod; } } cout << dp[n][k]; // -------------------------------------Code ends // here------------------------------------------------------ clk = clock() - clk; cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC << "\n"; return 0; }
replace
12
16
12
16
-11
p03172
C++
Runtime Error
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; ll mod = 1e9 + 7; int main() { ll n, k; cin >> n >> k; vector<ll> arr(n); for (int z = 1; z <= n; z++) { cin >> arr[z]; } vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, 0)); dp[0][0] = 1; ll count = 0; for (int z = 1; z <= n; z++) { ll sum = 0; count += arr[z]; for (int x = 0; x <= count && x <= k; x++) { if (x > arr[z]) sum -= dp[z - 1][x - arr[z] - 1]; sum += dp[z - 1][x]; if (sum < 0) sum += mod; dp[z][x] += sum; dp[z][x] %= mod; sum %= mod; } } // for (int z=0;z<=n;z++){ // for (int x=0;x<=k;x++){ // cout << dp[z][x] << " "; // } cout << endl; // } cout << dp[n][k] << endl; cin >> n; }
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; ll mod = 1e9 + 7; int main() { ll n, k; cin >> n >> k; vector<ll> arr(n + 1); for (int z = 1; z <= n; z++) { cin >> arr[z]; } vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, 0)); dp[0][0] = 1; ll count = 0; for (int z = 1; z <= n; z++) { ll sum = 0; count += arr[z]; for (int x = 0; x <= count && x <= k; x++) { if (x > arr[z]) sum -= dp[z - 1][x - arr[z] - 1]; sum += dp[z - 1][x]; if (sum < 0) sum += mod; dp[z][x] += sum; dp[z][x] %= mod; sum %= mod; } } // for (int z=0;z<=n;z++){ // for (int x=0;x<=k;x++){ // cout << dp[z][x] << " "; // } cout << endl; // } cout << dp[n][k] << endl; cin >> n; }
replace
12
13
12
13
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair<int, int> P; constexpr int mod = 1e9 + 7; int main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; vector<int> dp(k + 2, 0); dp[0] = 1; rep(i, n) { vector<int> cum(k + 1, 0); rep(j, k + 1)(cum[j + 1] = cum[j] + dp[j]) %= mod; rep(j, k + 1)(dp[j] = (cum[j + 1] - cum[max(0, j - a[i])]) + mod) %= mod; } cout << dp[k] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair<int, int> P; constexpr int mod = 1e9 + 7; int main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; vector<int> dp(k + 2, 0); dp[0] = 1; rep(i, n) { vector<int> cum(k + 2, 0); rep(j, k + 1)(cum[j + 1] = cum[j] + dp[j]) %= mod; rep(j, k + 1)(dp[j] = (cum[j + 1] - cum[max(0, j - a[i])]) + mod) %= mod; } cout << dp[k] << endl; return 0; }
replace
16
17
16
17
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) // 自動でmodを取ってくれる整数型 // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; // const int mod = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } mint f(int n) { // 2のn乗 if (n == 0) return 1; mint x = f(n / 2); x *= x; if (n % 2 == 1) x *= 2; return x; } // a^n mod を計算する long long modpow(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // indexは1から始まる! // Binary Indexed Tree (Fenwick Tree) // https://youtu.be/lyHk98daDJo?t=7960 template <typename T> struct BIT { int n; vector<T> d; BIT(int n = 0) { init(n); } void init(int nn) { n = nn; d.resize(nn + 1); } void add(int i, T x = 1) { for (i++; i <= n; i += i & -i) { d[i] += x; } } T sum(int i) { // [1,x] T x = 0; for (i++; i; i -= i & -i) { x += d[i]; } return x; } T sum(int l, int r) { // [l,r) return sum(r - 1) - sum(l - 1); } }; // BIT配列の宣言 N:配列数, n:各要素数+1; // BIT<int> bit[N]; // rep(i,N) bit[i].init(n); ////////////////////////// int main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; BIT<mint> dp[n + 1]; rep(i, n + 1) dp[i].init(k + 2); dp[0].add(k + 1, 1); rep(i, n + 1) { for (int j = 1; j <= k + 1; j++) { dp[i + 1].add(j, dp[i].sum(j, min(k + 1, j + a[i]) + 1)); } } cout << dp[n].sum(1) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) // 自動でmodを取ってくれる整数型 // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; // const int mod = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } mint f(int n) { // 2のn乗 if (n == 0) return 1; mint x = f(n / 2); x *= x; if (n % 2 == 1) x *= 2; return x; } // a^n mod を計算する long long modpow(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // indexは1から始まる! // Binary Indexed Tree (Fenwick Tree) // https://youtu.be/lyHk98daDJo?t=7960 template <typename T> struct BIT { int n; vector<T> d; BIT(int n = 0) { init(n); } void init(int nn) { n = nn; d.resize(nn + 1); } void add(int i, T x = 1) { for (i++; i <= n; i += i & -i) { d[i] += x; } } T sum(int i) { // [1,x] T x = 0; for (i++; i; i -= i & -i) { x += d[i]; } return x; } T sum(int l, int r) { // [l,r) return sum(r - 1) - sum(l - 1); } }; // BIT配列の宣言 N:配列数, n:各要素数+1; // BIT<int> bit[N]; // rep(i,N) bit[i].init(n); ////////////////////////// int main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; BIT<mint> dp[n + 1]; rep(i, n + 1) dp[i].init(k + 2); dp[0].add(k + 1, 1); rep(i, n) { for (int j = 1; j <= k + 1; j++) { dp[i + 1].add(j, dp[i].sum(j, min(k + 1, j + a[i]) + 1)); } } cout << dp[n].sum(1) << endl; }
replace
115
116
115
116
-11
p03172
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long int #define pll pair<int, int> #define dbl long double #define ff first #define ss second #define endl "\n" #define mod 1000000007 #define eps 0.00000001 #define inf 10000000000001 #define all(x) (x).begin(), (x).end() #define size(x) (int)(x).size() #define pb(x) push_back(x) #define pf(x) push_front(x) #define popb() pop_back() #define popf() pop_front() #define mp(x, y) make_pair((x), (y)) #define vv(dt) vector<vector<dt>> #define fastio(x) \ ios_base::sync_with_stdio(x); \ cin.tie(NULL) #define init(v, s) memset(v, s, sizeof(v)) #define bug(x) \ cerr << "LINE: " << __LINE__ << " || " << #x << " = " << x << endl #define loop(i, s, n) for (auto i = s; i < n; i++) #define rloop(i, n, b) for (auto i = n - 1; i >= b; i--) using namespace std; signed main() { fastio(0); int n, k; cin >> n >> k; int a[n]; loop(i, 0, n) cin >> a[i]; int dp[k + 1][n + 1]; init(dp, 0); // no of ways for n=1, fill upto a[0], is 1. loop(i, 0, k + 1) { if (i <= a[0]) dp[i][1] = 1; else dp[i][1] = 0; // use dp[k][0] as prefix array if (i == 0) dp[i][0] = dp[i][1]; else dp[i][0] = (dp[i - 1][0] + dp[i][1]) % mod; } // dp[k][n] = sum(dp[k-ai][n-1]) ; ai E [0,min(k,an)]. for (int N = 2; N <= n; N++) { for (int K = 0; K <= k; K++) { if (a[N - 1] < K) { bug(K); dp[K][N] = (dp[K][0] - dp[K - a[N - 1] - 1][0] + mod) % mod; } else dp[K][N] = dp[K][0] % mod; } // update prefix array for (int K = 0; K <= k; K++) { if (K == 0) dp[K][0] = dp[K][N] % mod; else dp[K][0] = (dp[K - 1][0] + dp[K][N]) % mod; } // bug("DP"); // loop(i,0,k+1) // { // loop(j,0,n+1) cout<<dp[i][j]<<" "; // cout<<endl; // } } // ans = dp[k][n] cout << dp[k][n] % mod << endl; return 0; }
#include <bits/stdc++.h> #define int long long int #define pll pair<int, int> #define dbl long double #define ff first #define ss second #define endl "\n" #define mod 1000000007 #define eps 0.00000001 #define inf 10000000000001 #define all(x) (x).begin(), (x).end() #define size(x) (int)(x).size() #define pb(x) push_back(x) #define pf(x) push_front(x) #define popb() pop_back() #define popf() pop_front() #define mp(x, y) make_pair((x), (y)) #define vv(dt) vector<vector<dt>> #define fastio(x) \ ios_base::sync_with_stdio(x); \ cin.tie(NULL) #define init(v, s) memset(v, s, sizeof(v)) #define bug(x) \ cerr << "LINE: " << __LINE__ << " || " << #x << " = " << x << endl #define loop(i, s, n) for (auto i = s; i < n; i++) #define rloop(i, n, b) for (auto i = n - 1; i >= b; i--) using namespace std; signed main() { fastio(0); int n, k; cin >> n >> k; int a[n]; loop(i, 0, n) cin >> a[i]; int dp[k + 1][n + 1]; init(dp, 0); // no of ways for n=1, fill upto a[0], is 1. loop(i, 0, k + 1) { if (i <= a[0]) dp[i][1] = 1; else dp[i][1] = 0; // use dp[k][0] as prefix array if (i == 0) dp[i][0] = dp[i][1]; else dp[i][0] = (dp[i - 1][0] + dp[i][1]) % mod; } // dp[k][n] = sum(dp[k-ai][n-1]) ; ai E [0,min(k,an)]. for (int N = 2; N <= n; N++) { for (int K = 0; K <= k; K++) { if (a[N - 1] < K) { // bug(K); dp[K][N] = (dp[K][0] - dp[K - a[N - 1] - 1][0] + mod) % mod; } else dp[K][N] = dp[K][0] % mod; } // update prefix array for (int K = 0; K <= k; K++) { if (K == 0) dp[K][0] = dp[K][N] % mod; else dp[K][0] = (dp[K - 1][0] + dp[K][N]) % mod; } // bug("DP"); // loop(i,0,k+1) // { // loop(j,0,n+1) cout<<dp[i][j]<<" "; // cout<<endl; // } } // ans = dp[k][n] cout << dp[k][n] % mod << endl; return 0; }
replace
53
54
53
54
TLE
p03172
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define fir first #define sec second #define sz(s) (s).size() #define pb push_back #define get(n) scanf("%d", &n); #define gets(s) \ string s; \ cin >> (s); #define prfi(n) printf("%d", &n); #define prfd(n) printf("%lf", &n); #define All(s) (s).begin(), (s).end() #define rep(i, j) for (int(i) = 0; (i) < (j); (i)++) #define For(i, j, k) for (int(i) = (j); (i) < (k); (i)++) #define drep(i, j) for (int(i) = (j); (i) >= 0; (i)--) #define Ford(i, j, k) for (int(i) = (j); i >= (k); i--) #define vfor(c, v) for (auto(c) : (v)) #define lpi(n) for (int i = 0; i < (n); i++) #define lpj(n) for (int j = 0; j < (n); j++) #define lpz(n) for (int z = 0; z < (n); z++) #define mem(a, b) memset(a, b, sizeof(a)); #define dump(x) std::cout << #x << " = " << (x) << std::endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using ull = unsigned long long int; using ll = long long; using ld = long double; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double>; using vvd = std::vector<vd>; using qi = std::queue<int>; using vpii = std::vector<std::pair<int, int>>; using vpll = std::vector<pll>; using namespace std; const int Mod = (1e9) + 7; const int INF = 1e9 + 19; const ll INFL = 1e18 + 19; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; const int dx2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; const int dy2[] = {1, 0, -1, 1, 0, -1, 1, 0, -1}; //_____________________________________Templates_________________________________________// template <class T1, class T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <class T1, class T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <class T> inline void pri(T a) { cout << a << endl; } template <class Z> using vec = vector<Z>; template <class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; // mainly use for dynamic prog template <class T1, class T2> void update(T1 &a, T2 b) { a += b; if (a > Mod) a %= Mod; } inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } bool pairsort(pll pl, pll pr) { if (pl.first == pr.first) return pl.second > pr.second; return pl.first < pr.first; } int cntbit(ll a, int n, int j) { int res = 0; For(i, j, n) { if (a >> i & 1) { res++; } } return res; } vector<int> make_bit(int a) { vector<int> res; lpi(32) if (a & (1 << i)) res.pb(i); return res; } bool stdbit(int a, int b) { return a & (1 << b); } int GCD(int a, int b) { if (b > a) return GCD(b, a); if (a % b == 0) return b; else return GCD(b, a % b); } int LCM(int a, int b) { return a * b / GCD(a, b); } int roundup(int a, int b) { if (a % b == 0) return a / b; else return (a + b) / b; } int rounddown(int a, int b) { if (a % b == 0) return a / b; else { return (a - b) / b; } } ll pow(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res *= a; a *= a; n = n >> 1; } return res; } ll GetDiviserCount(ll N) // 約数の個数 { ll res = 1; For(i, 2, sqrt(N) + 1) { ll cnt = 0; while (N % i == 0) { cnt++; N /= i; } res *= (cnt + 1); if (N == 1) break; } if (N != 1) res *= 2; return res; } vll GetDivisor(ll N) // 約数列挙 { vll res; for (ll i = 1; i * i <= N; i++) { if (N % i == 0) { res.pb(i); if (i * i != N) res.pb(N / i); } } sort(All(res)); return res; } struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % Mod + Mod) % Mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= Mod) x -= Mod; return *this; } mint &operator-=(const mint a) { if ((x += Mod - a.x) >= Mod) x -= Mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= Mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(Mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &a); }; ostream &operator<<(ostream &os, const mint &a) { os << a.x; return os; } struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < Mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; //_____________________ following sorce code_________________________// const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; int n, m, k; ll N; int h, w; vvi tree; string S; int a, b, c; vi v; int ans; signed main() { cin.tie(0); ios::sync_with_stdio(false); IN(n, k); vi a(n); rep(i, n) IN(a[i]); vec<vec<mint>> dp(n + 1, vec<mint>(k + 1)); vec<vec<mint>> sum(n + 1, vec<mint>(k + 1)); For(i, max(0, k - a[0]), k + 1) { dp[0][i] = 1; sum[0][i + 1] = sum[0][i] + dp[0][i]; } For(i, 1, n) { rep(j, k + 1) { dp[i][j] = sum[i - 1][min(k, j + a[i]) + 1] - sum[i - 1][j]; sum[i][j + 1] = sum[i][j] + dp[i][j]; } } mint ans = dp[n - 1][0]; cout << ans << endl; // for(auto c : ans){cout << c << endl;} // cout << fixed << setprecision(15) << ans << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define fir first #define sec second #define sz(s) (s).size() #define pb push_back #define get(n) scanf("%d", &n); #define gets(s) \ string s; \ cin >> (s); #define prfi(n) printf("%d", &n); #define prfd(n) printf("%lf", &n); #define All(s) (s).begin(), (s).end() #define rep(i, j) for (int(i) = 0; (i) < (j); (i)++) #define For(i, j, k) for (int(i) = (j); (i) < (k); (i)++) #define drep(i, j) for (int(i) = (j); (i) >= 0; (i)--) #define Ford(i, j, k) for (int(i) = (j); i >= (k); i--) #define vfor(c, v) for (auto(c) : (v)) #define lpi(n) for (int i = 0; i < (n); i++) #define lpj(n) for (int j = 0; j < (n); j++) #define lpz(n) for (int z = 0; z < (n); z++) #define mem(a, b) memset(a, b, sizeof(a)); #define dump(x) std::cout << #x << " = " << (x) << std::endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using ull = unsigned long long int; using ll = long long; using ld = long double; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double>; using vvd = std::vector<vd>; using qi = std::queue<int>; using vpii = std::vector<std::pair<int, int>>; using vpll = std::vector<pll>; using namespace std; const int Mod = (1e9) + 7; const int INF = 1e9 + 19; const ll INFL = 1e18 + 19; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; const int dx2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; const int dy2[] = {1, 0, -1, 1, 0, -1, 1, 0, -1}; //_____________________________________Templates_________________________________________// template <class T1, class T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <class T1, class T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <class T> inline void pri(T a) { cout << a << endl; } template <class Z> using vec = vector<Z>; template <class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; // mainly use for dynamic prog template <class T1, class T2> void update(T1 &a, T2 b) { a += b; if (a > Mod) a %= Mod; } inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } bool pairsort(pll pl, pll pr) { if (pl.first == pr.first) return pl.second > pr.second; return pl.first < pr.first; } int cntbit(ll a, int n, int j) { int res = 0; For(i, j, n) { if (a >> i & 1) { res++; } } return res; } vector<int> make_bit(int a) { vector<int> res; lpi(32) if (a & (1 << i)) res.pb(i); return res; } bool stdbit(int a, int b) { return a & (1 << b); } int GCD(int a, int b) { if (b > a) return GCD(b, a); if (a % b == 0) return b; else return GCD(b, a % b); } int LCM(int a, int b) { return a * b / GCD(a, b); } int roundup(int a, int b) { if (a % b == 0) return a / b; else return (a + b) / b; } int rounddown(int a, int b) { if (a % b == 0) return a / b; else { return (a - b) / b; } } ll pow(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res *= a; a *= a; n = n >> 1; } return res; } ll GetDiviserCount(ll N) // 約数の個数 { ll res = 1; For(i, 2, sqrt(N) + 1) { ll cnt = 0; while (N % i == 0) { cnt++; N /= i; } res *= (cnt + 1); if (N == 1) break; } if (N != 1) res *= 2; return res; } vll GetDivisor(ll N) // 約数列挙 { vll res; for (ll i = 1; i * i <= N; i++) { if (N % i == 0) { res.pb(i); if (i * i != N) res.pb(N / i); } } sort(All(res)); return res; } struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % Mod + Mod) % Mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= Mod) x -= Mod; return *this; } mint &operator-=(const mint a) { if ((x += Mod - a.x) >= Mod) x -= Mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= Mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(Mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &a); }; ostream &operator<<(ostream &os, const mint &a) { os << a.x; return os; } struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < Mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; //_____________________ following sorce code_________________________// const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; int n, m, k; ll N; int h, w; vvi tree; string S; int a, b, c; vi v; int ans; signed main() { cin.tie(0); ios::sync_with_stdio(false); IN(n, k); vi a(n); rep(i, n) IN(a[i]); vec<vec<mint>> dp(n + 1, vec<mint>(k + 2)); vec<vec<mint>> sum(n + 1, vec<mint>(k + 2)); For(i, max(0, k - a[0]), k + 1) { dp[0][i] = 1; sum[0][i + 1] = sum[0][i] + dp[0][i]; } For(i, 1, n) { rep(j, k + 1) { dp[i][j] = sum[i - 1][min(k, j + a[i]) + 1] - sum[i - 1][j]; sum[i][j + 1] = sum[i][j] + dp[i][j]; } } mint ans = dp[n - 1][0]; cout << ans << endl; // for(auto c : ans){cout << c << endl;} // cout << fixed << setprecision(15) << ans << endl; return 0; }
replace
274
276
274
276
-6
munmap_chunk(): invalid pointer
p03172
C++
Runtime Error
#include <bits/stdc++.h> #define l_ength size using ll = long long; const int mod = 1000000007; const ll infll = (1ll << 60); using namespace std; ll dp[110][100010]; ll sum[100010]; int main() { int n, k; cin >> n >> k; vector<ll> a(n); for (auto &k : a) cin >> k; dp[0][0] = 1; for (int i = 0; i <= k; ++i) { sum[i + 1] = (sum[i] + dp[0][i]) % mod; } for (int i = 0; i < n; ++i) { for (int j = 0; j <= k; ++j) { dp[i + 1][j] = (sum[j + 1] - sum[j - a[i]] + mod) % mod; } for (int j = 0; j <= k; ++j) { sum[j + 1] = (sum[j] + dp[i + 1][j]) % mod; } } cout << dp[n][k] << endl; }
#include <bits/stdc++.h> #define l_ength size using ll = long long; const int mod = 1000000007; const ll infll = (1ll << 60); using namespace std; ll dp[110][100010]; ll sum[100010]; int main() { int n, k; cin >> n >> k; vector<ll> a(n); for (auto &k : a) cin >> k; dp[0][0] = 1; for (int i = 0; i <= k; ++i) { sum[i + 1] = (sum[i] + dp[0][i]) % mod; } for (int i = 0; i < n; ++i) { for (ll j = 0; j <= k; ++j) { dp[i + 1][j] = (sum[j + 1] - sum[max(0ll, j - a[i])] + mod) % mod; } for (int j = 0; j <= k; ++j) { sum[j + 1] = (sum[j] + dp[i + 1][j]) % mod; } } cout << dp[n][k] << endl; }
replace
21
23
21
23
0
p03172
C++
Runtime Error
// WA #include <bits/stdc++.h> using namespace std; typedef long long int ll; int dp[100001]; vector<int> ar(100); // ll solve(ll index, ll x) { // // cout << "0\n"; // if(dp[index][x] != -1) // return dp[index][x]; // ll ansl = 0; // // cout << "1\n"; // if(x == 0) // return dp[index][x] = 1; // // cout << "2\n"; // if(index == 0) { // if(ar[0] >= x) // return dp[index][x] = 1; // else return dp[index][x] = 0; // } // // cout << "3\n"; // for(int i = 0; i <= min(ar[index], x); i++) { // ansl = (ansl + solve(index-1, x-i)) % 1000000007; // } // // cout << "ansl = " << "\n"; // return dp[index][x] = ansl; // } void sub(int &a, int b) { a -= b; if (a < 0) a += 1000000007; } void add(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; } ll solve_bottom_up(int n, int k) { dp[0] = 1; for (int child = 0; child < n; child++) { vector<int> pf(k + 1, 0); for (int used = k; used >= 0; used--) { int l = used + 1; int r = used + min(ar[child], k - used); add(pf[l], dp[used]); if (r + 1 <= k) sub(pf[r + 1], dp[used]); } int prf_sum = 0; for (int i = 0; i <= k; i++) { add(prf_sum, pf[i]); add(dp[i], prf_sum); } } return dp[k]; } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> ar[i]; // for(int i = 0; i < n; i++) { // for(int j = 0; j <= k; j++) // dp[i][j] = -1; // } cout << solve_bottom_up(n, k) << "\n"; // for(int i = 0; i < n; i++) { // for(int j = 0; j <= k; j++) // cout << dp[i][j] << " "; // cout << "\n"; // } }
// WA #include <bits/stdc++.h> using namespace std; typedef long long int ll; int dp[100001]; vector<int> ar(100); // ll solve(ll index, ll x) { // // cout << "0\n"; // if(dp[index][x] != -1) // return dp[index][x]; // ll ansl = 0; // // cout << "1\n"; // if(x == 0) // return dp[index][x] = 1; // // cout << "2\n"; // if(index == 0) { // if(ar[0] >= x) // return dp[index][x] = 1; // else return dp[index][x] = 0; // } // // cout << "3\n"; // for(int i = 0; i <= min(ar[index], x); i++) { // ansl = (ansl + solve(index-1, x-i)) % 1000000007; // } // // cout << "ansl = " << "\n"; // return dp[index][x] = ansl; // } void sub(int &a, int b) { a -= b; if (a < 0) a += 1000000007; } void add(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; } ll solve_bottom_up(int n, int k) { dp[0] = 1; for (int child = 0; child < n; child++) { vector<int> pf(k + 1, 0); for (int used = k; used >= 0; used--) { int l = used + 1; int r = used + min(ar[child], k - used); if (l <= r) { add(pf[l], dp[used]); if (r + 1 <= k) sub(pf[r + 1], dp[used]); } } int prf_sum = 0; for (int i = 0; i <= k; i++) { add(prf_sum, pf[i]); add(dp[i], prf_sum); } } return dp[k]; } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> ar[i]; // for(int i = 0; i < n; i++) { // for(int j = 0; j <= k; j++) // dp[i][j] = -1; // } cout << solve_bottom_up(n, k) << "\n"; // for(int i = 0; i < n; i++) { // for(int j = 0; j <= k; j++) // cout << dp[i][j] << " "; // cout << "\n"; // } }
replace
53
56
53
58
0
p03172
C++
Runtime Error
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; void print() {} template <class H, class... T> void print(H &&h, T &&...t) { cout << h << " \n"[sizeof...(t) == 0]; print(forward<T>(t)...); } ll dp[2][100010] = {}; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, K; cin >> N >> K; ll a[110]; rep(i, 0, N) cin >> a[i]; dp[0][0] = 1; ll cur = 0, nxt = 1; rep(i, 0, N) { rep(j, 0, K + 1) dp[nxt][j] = 0; rrep(j, K, 0) { dp[nxt][j] += dp[cur][j]; dp[nxt][j] %= MOD; dp[nxt][j + a[i] + 1] = (dp[nxt][j + a[i] + 1] - dp[cur][j] + MOD) % MOD; } rep(j, 0, K + 1)(dp[nxt][j + 1] += dp[nxt][j]) %= MOD; nxt ^= 1; cur ^= 1; } print(dp[cur][K]); }
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; void print() {} template <class H, class... T> void print(H &&h, T &&...t) { cout << h << " \n"[sizeof...(t) == 0]; print(forward<T>(t)...); } ll dp[2][100010] = {}; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, K; cin >> N >> K; ll a[110]; rep(i, 0, N) cin >> a[i]; dp[0][0] = 1; ll cur = 0, nxt = 1; rep(i, 0, N) { rep(j, 0, K + 1) dp[nxt][j] = 0; rrep(j, K, 0) { dp[nxt][j] += dp[cur][j]; dp[nxt][j] %= MOD; if (j + a[i] + 1 <= K + 1) dp[nxt][j + a[i] + 1] = (dp[nxt][j + a[i] + 1] - dp[cur][j] + MOD) % MOD; } rep(j, 0, K + 1)(dp[nxt][j + 1] += dp[nxt][j]) %= MOD; nxt ^= 1; cur ^= 1; } print(dp[cur][K]); }
replace
33
34
33
36
0
p03172
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; template <class S, class T> using P = pair<S, T>; template <class... T> using TP = tuple<T...>; using ll = long long; using ull = unsigned long long; using dbl = double; using str = string; using vll = V<ll>; using vvll = V<vll>; using vvvll = V<vvll>; using pl = P<ll, ll>; using tl = TP<ll, ll, ll>; using vpl = V<pl>; using vvpl = V<vpl>; using vtl = V<tl>; using vvtl = V<vtl>; using vs = V<str>; using vvs = V<vs>; using vd = V<dbl>; using vvd = V<vd>; using vvvd = V<vvd>; using qll = queue<ll>; using qpl = queue<pl>; using stll = stack<ll>; using stpl = stack<pl>; using mapll = map<ll, ll>; using setll = set<ll>; using pqll = priority_queue<ll>; // #define int ll #define fi first #define se second #define mp make_pair #define mt make_tuple #define pb push_back #define pob pop_back() #define pf push_front #define pof pop_front() #define sz size() #define bgn begin() #define en end() #define asn assign #define emp empty() #define fr front() #define bk back() #define clr clear() #define ins insert #define ers erase #define res resize #define tp top() #define p_q priority_queue #define inv inverse() #define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++) #define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--) #define REP(i, a) FOR((i), 0, (ll)(a)-1) #define REP0(i, a) FOR((i), 0, (ll)(a)) #define REP1(i, a) FOR((i), 1, (ll)(a)) #define rREP(i, a) rFOR((i), 0, (ll)(a)-1) #define rREP0(i, a) rFOR((i), 0, (ll)(a)) #define rREP1(i, a) rFOR((i), 1, (ll)(a)) #define ROR(v, i) for (auto &(i) : (v)) #define IOTA(a, n) iota((a).bgn, (a).en, (n)) #define SORT(a) sort((a).bgn, (a).en) #define rSORT(a) sort((a).rbegin(), (a).rend()) #define UNIQUE(a) (a).erase(unique((a).bgn, (a).en), (a).en) #define PREVP(a) prev_permutation((a).bgn, (a).en) #define NEXTP(a) next_permutation((a).bgn, (a).en) #define BINS(a, b) binary_search((a).bgn, (a).en, (b)) #define LOWB(a, b) (lower_bound((a).bgn, (a).en, (b)) - (a).bgn) #define UPB(a, b) (upper_bound((a).bgn, (a).en, (b)) - (a).bgn) #define CNT(a, b) count((a).bgn, (a).en, b) #define SUM(a) accumulate((a).bgn, (a).en, 0) #define REV(a) reverse((a).bgn, (a).en) #define REGS(a, b) regex_search((a), regex(b)) #define REGM(a, b) regex_match((a), regex(b)) #define yn(a) cout << ((a) ? "yes" : "no") << "\n"; #define Yn(a) cout << ((a) ? "Yes" : "No") << "\n"; #define YN(a) cout << ((a) ? "YES" : "NO") << "\n"; #define imp(a) cout << ((a) ? "possible" : "impossible") << "\n"; #define Imp(a) cout << ((a) ? "Possible" : "Impossible") << "\n"; #define IMP(a) cout << ((a) ? "POSSIBLE" : "IMPOSSIBLE") << "\n"; #define fs(a) cout << ((a) ? "second" : "first") << "\n"; #define Fs(a) cout << ((a) ? "Second" : "First") << "\n"; #define FS(a) cout << ((a) ? "SECOND" : "FIRST") << "\n"; // #define say(a) cout <<(a); // #define sal(a) cout <<(a)<<"\n"; #define sak cout << "\n"; #define sas cout << " "; #define sat cout << "\t"; #define dbg(a) cerr << (#a) << ": " << (a) << "\n"; #define dbgs(...) \ dal(#__VA_ARGS__); \ dal(__VA_ARGS__); #define c2l(a) ((ll)(a - 48)) #define a2l(a) ((ll)(a - 97)) #define A2l(a) ((ll)(a - 65)) #define l2c(a) ((char)(a + 48)) #define l2a(a) ((char)(a + 97)) #define l2A(a) ((char)(a + 65)) #define DigN2(a) ((llabs(a) == 0) ? (1) : ((ll)(log2(double(llabs(a)))) + 1)) #define DigN10(a) ((llabs(a) == 0) ? (1) : ((ll)(log10(double(llabs(a)))) + 1)) #define Dig2(a, b) (((a) >> (b)) & 1) #define Dig10(a, b) (ll)(((a) / ((ll)(pow(10.0, (double)(b))))) % 10) #define Pow2(a) ((ll)(1) << (a)) #define Pow10(a) ((ll)(pow(10.0, double(a)))) #define LSB(a) ((a) & (-(a))) /*#define llin(a) ll (a);cin >>(a); #define llin2(a,b) ll (a),(b);cin >>(a)>>(b); #define llin3(a,b,c) ll (a),(b),(c);cin >>(a)>>(b)>>(c); #define stin(a) string (a);cin >>(a);*/ #define vin(v) \ ROR((v), (i)) { cin >> (i); }; #define vllin(N, v) \ vll(v)((N)); \ vin(v); #define vllin2(N, a, b) \ vll(a)(N), (b)(N); \ REP(i, N) { cin >> (a)[i] >> (b)[i]; }; #define vsin(N, v) \ vs(v)((N)); \ vin(v); #define rdn(a, b) ((a) / (b)) #define rou(a, b) \ ((((double(a) / double(b)) - ((a) / (b))) < 0.5) ? ((a) / (b)) \ : (((a) / (b)) + 1)) #define rup(a, b) ((((a) % (b)) == 0) ? ((a) / (b)) : (((a) / (b)) + 1)) #define powll(a, b) (ll)(pow((double)(a), (double)(b))) #define Triangle(x1, y1, x2, y2, x3, y3) \ (((x1) - (x2)) * ((y1) - (y3)) - ((x1) - (x3)) * ((y1) - (y2))) #define tg(t, i) get<i>(t) #define Id(x) get<0>(x) #define Act(x) get<1>(x) #define InvAct(x) get<2>(x) #define mg(id, act) mt(id, act, lam(l)) // #define MonoidSet(T) TP<T, function<T(T, T)>> #define GroupSet(T) TP<T, function<T(T, T)>, function<T(T, T)>> #define CompareSet(T) TP<T, function<bool(T, T)>> #define lam(lr) ([](auto l, auto r) { return (lr); }) #define elam(lr) ([=](auto l, auto r) { return (lr); }) #define clam(lr) ([&](auto l, auto r) { return (lr); }) #define lamr(lr) ([](auto l, auto r) { lr }) #define elamr(lr) ([=](auto l, auto r) { lr }) #define clamr(lr) ([&](auto l, auto r) { lr }) #define min(...) Operation(MIN, __VA_ARGS__) #define max(...) Operation(MAX, __VA_ARGS__) #define gcd(...) Operation(GCD, __VA_ARGS__) #define lcm(...) Operation(LCM, __VA_ARGS__) #define vmin(...) VOperation(MIN, __VA_ARGS__) #define vmax(...) VOperation(MAX, __VA_ARGS__) #define vgcd(...) VOperation(GCD, __VA_ARGS__) #define vlcm(...) VOperation(LCM, __VA_ARGS__) #define vsum(...) VOperation(ADD, __VA_ARGS__) #define vpro(...) VOperation(MUL, __VA_ARGS__) #define emin(a, ...) ((a) = min((a), __VA_ARGS__)) #define emax(a, ...) ((a) = max((a), __VA_ARGS__)) #define egcd(a, ...) ((a) = gcd((a), __VA_ARGS__)) #define elcm(a, ...) ((a) = lcm((a), __VA_ARGS__)) #define ope Operation #define vope VOperation #define svll SumV<ll> #define svvll SumV2<ll> #define li(...) \ ll __VA_ARGS__; \ Input(__VA_ARGS__); #define si(...) \ str __VA_ARGS__; \ Input(__VA_ARGS__); // #define vli(size, ...) vll __VA_ARGS__;vInitInput(size,__VA_ARGS__); #define vlr(size, ...) \ vll __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vlc(size, ...) \ vll __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vli(size, ...) \ vll __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vsr(size, ...) \ vs __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vsc(size, ...) \ vs __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vsi(size, ...) \ vs __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vli2(rowSize, columnSize, ...) \ vvll __VA_ARGS__; \ vInitInput2(rowSize, columnSize, __VA_ARGS__); #define vplr(size, ...) \ vpl __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vplc(size, ...) \ vpl __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vpli(size, ...) \ vpl __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; // const ll MOD = 924844033; // const ll MOD = 9007199254740881; const ll INF = 1LL << 60; // 1.15e18 const double PI = acos(-1.0); const vll DX = {0, -1, 0, 1, 0, -1, 1, 1, -1}; const vll DY = {0, 0, -1, 0, 1, -1, -1, 1, 1}; const str alp = "abcdefghijklmnopqrstuvwxyz"; const str ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; template <class T> auto GetVectorValueType(T v) { return v; } template <class T> auto GetVectorValueType(V<T> v) { return GetVectorValueType(T()); } template <class S, class T> istream &operator>>(istream &in, P<S, T> &p) { return in >> p.fi >> p.se; } template <class T> istream &operator>>(istream &in, V<T> &v) { REP(i, v.sz) in >> v[i]; return in; } void Input() {} template <class Var, class... Args> void Input(Var &var, Args &...args) { cin >> var; Input(args...); } void vInit(ll size) {} template <class T, class... Args> void vInit(ll size, V<T> &v, Args &...args) { v.res(size); vInit(size, args...); } void vInput(ll size) {} template <class T, class... Args> void vInput(ll size, V<T> &v, Args &...args) { REP(i, size) cin >> v[i]; vInput(size, args...); } void vInputR(ll size) {} template <class T, class... Args> void vInputR(ll size, V<T> &v, Args &...args) { REP(i, size) cin >> v[i]; vInputR(size, args...); } void vInputNumC(ll num) {} template <class T, class... Args> void vInputNumC(ll num, V<T> &v, Args &...args) { cin >> v[num]; vInputNumC(num, args...); } void vInputC(ll size) {} template <class... Args> void vInputC(ll size, Args &...args) { REP(i, size) vInputNumC(i, args...); } void vInitInputR(ll size) {} template <class... Args> void vInitInputR(ll size, Args &...args) { vInit(size, args...); vInputR(size, args...); } void vInitInputC(ll size) {} template <class... Args> void vInitInputC(ll size, Args &...args) { vInit(size, args...); vInputC(size, args...); } void vInit2(ll rowSize, ll columnSize) {} template <class T, class... Args> void vInit2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) { v.asn(rowSize, V<T>(columnSize)); vInit2(rowSize, columnSize, args...); } void vInput2(ll rowSize, ll columnSize) {} template <class T, class... Args> void vInput2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) { REP(r, rowSize) { REP(c, columnSize) { cin >> v[r][c]; } } vInput2(rowSize, columnSize, args...); } void vInitInput2(ll rowSize, ll columnSize) {} template <class... Args> void vInitInput2(ll rowSize, ll columnSize, Args &...args) { vInit2(rowSize, columnSize, args...); vInput2(rowSize, columnSize, args...); } template <class S, class T> ostream &operator<<(ostream &out, const P<S, T> &p) { return out << "[" << p.fi << ", " << p.se << "]"; } template <class T> ostream &operator<<(ostream &out, V<T> &v) { if (v.emp) return out << "{}"; else { auto itr = v.bgn; out << "{" << *itr; itr++; while (itr != v.en) { out << ", " << *itr; itr++; } out << "}"; return out; } } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> &m) { if (m.emp) return out << "<[]>"; else { auto itr = m.bgn; out << "< [" << (itr->fi) << ": " << (itr->se); itr++; while (itr != m.en) { out << "], [" << (itr->fi) << ": " << (itr->se); itr++; } out << "] >"; return out; } } template <class T> ostream &operator<<(ostream &out, const set<T> &s) { if (s.emp) return out << "<>"; else { auto itr = s.bgn; out << "<" << *itr; itr++; while (itr != s.en) { out << ", " << *itr; itr++; } out << ">"; return out; } } void say() {} template <class T> void say(T t) { cout << t; } template <class Head, class... Body> void say(Head head, Body... body) { cout << head << " "; say(body...); } void sal() { cout << "\n"; } template <class... Args> void sal(Args... args) { say(args...); cout << "\n"; } void day() {} template <class T> void day(T t) { cerr << t; } template <class Head, class... Body> void day(Head head, Body... body) { cerr << head << " "; day(body...); } void dal() { cerr << "\n"; } template <class... Args> void dal(Args... args) { day(args...); cerr << "\n"; } void salv() {} template <class T> void salv(V<T> v) { if (v.emp) sal(); else { auto itr = v.bgn; say(*itr); itr++; while (itr != v.en) { sas; say(*itr); itr++; } sak; } } template <class T> void salv(VV<T> v) { if (v.emp) sal(); else { ROR(v, i) salv(i); } } template <class T, class... Args> void salv(T v, Args... args) { salv(v); salv(args...); } template <class L, class R> auto Gcd(L l, R r) -> decltype(l + r) { if (l < r) swap(l, r); return r ? Gcd(r, l % r) : l; } template <class L, class R> auto Lcm(L l, R r) { if (!l || !r) return 0; return l / Gcd(l, r) * r; } /* auto LES = mp(INF, lam(return l < r;)); auto GRT = mp(-INF, lam(return l > r;)); auto EQ = mp(0, lam(return l == r;)); auto ADD = mp(0, lam(return l + r;)); auto SUB = mp(0, lam(return l - r;)); auto MUL = mp(1, lam(return l * r;)); auto DIV = mp(1, lam(return l / r;)); auto MDL = mp(1, lam(return l % r;)); auto XOR = mp(0, lam(return l ^ r;)); auto OR = mp(0, lam(return l | r;)); auto AND = mp(((ll)(1) << 63) - 1, lam(return l & r;)); auto MIN = mp(INF, lam(return (l < r) ? l : r;)); auto MAX = mp(-INF, lam(return (l > r) ? l : r;)); auto GCD = mp(0, lam(return Gcd(l, r);)); auto LCM = mp(1, lam(return Lcm(l, r);)); */ auto LES = mp(INF, lam(l < r)); auto GRT = mp(-INF, lam(l > r)); auto EQ = mp(0, lam(l == r)); auto ADD = mt(0, lam(l + r), lam(l - r)); auto MUL = mt(1, lam(l *r), lam(l / r)); auto XOR = mt(0, lam(l ^ r), lam(l ^ r)); auto OR = mg(0, lam(l | r)); auto AND = mg(((ll)(1) << 63) - 1, lam(l &r)); auto MIN = mg(0, lam((l < r) ? l : r)); auto MAX = mg(0, lam((l > r) ? l : r)); auto GCD = mg(0, lam(Gcd(l, r))); auto LCM = mg(0, lam(Lcm(l, r))); template <class OperationType> auto Operation(OperationType A) { return Id(A); } template <class OperationType, class T> auto Operation(OperationType A, T x) { return x; } template <class OperationType, class T, class... Args> auto Operation(OperationType A, T x, Args... args) { auto tmp = Operation(A, args...); return Act(A)(x, tmp); } template <class OperationType> auto VOperation(OperationType A) { return Id(A); } template <class OperationType, class T> auto VOperation(OperationType A, T x) { return x; } template <class OperationType, class T> auto VOperation(OperationType A, V<T> v) { if (v.emp) { decltype(GetVectorValueType(T())) tmp = Id(A); return tmp; } auto tmp = VOperation(A, v[0]); FOR(i, 1, v.sz - 1) tmp = Act(A)(tmp, VOperation(A, v[i])); return tmp; } template <class OperationType, class T, class... Args> auto VOperation(OperationType A, T x, Args... args) { auto xResult = VOperation(A, x); auto tmp = VOperation(A, args...); return Act(A)(xResult, tmp); } ll Bset(ll a, ll b, ll c) { if (c) a |= b; else a &= ~b; return a; } struct UFT { public: ll tsize; ll mode; vll par; vll rank; UFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } } ll size(ll x) { return -par[root(x)]; } }; template <class T> struct pUFT { public: ll tsize; ll now; vll par; vll rank; vll mtime; vvll sizepi; VV<T> sizepv; V<T> elm; GroupSet(T) Add; pUFT(ll tsize, GroupSet(T) Add = ADD) : tsize(tsize), Add(Add) { init(); } void init() { now = 0; par.asn(tsize, -1); rank.asn(tsize, 0); mtime.asn(tsize, INF); sizepi.asn(tsize, {0}); sizepv.asn(tsize, {}); } void set(ll x, T s) { elm[x] = s; sizepv[x] = {s}; } ll root(ll x, ll t) { return (mtime[x] > t) ? x : root(par[x], t); } bool same(ll x, ll y, ll t) { return root(x, t) == root(y, t); } ll merge(ll x, ll y) { now++; x = root(x, now); y = root(y, now); if (x != y) { if (rank[x] < rank[y]) { elm[y] = Act(Add)(elm[x], elm[y]); sizepi[y].pb(now); sizepv[y].pb(elm[y]); par[x] = y; mtime[x] = now; } else { elm[x] = Act(Add)(elm[x], elm[y]); sizepi[x].pb(now); sizepv[x].pb(elm[x]); par[y] = x; mtime[y] = now; if (rank[x] == rank[y]) rank[x]++; } } return now; } T size(ll x, ll t) { x = root(x, t); return sizepv[x][UPB(sizepi[x], t) - 1]; } }; struct wUFT { public: ll tsize; ll mode; vll par; vll rank; vll dweight; wUFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); dweight.asn(tsize, 0); } ll root(ll x) { if (par[x] < 0) return x; else { ll r = root(par[x]); dweight[x] += dweight[par[x]]; return par[x] = r; } } ll weight(ll x) { root(x); return dweight[x]; } ll diff(ll x, ll y) { return weight(y) - weight(x); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y, ll w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; dweight[x] = -w; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; dweight[y] = w; } } } ll size(ll x) { return -par[root(x)]; } }; template <class T> struct sUFT { public: ll tsize; ll mode; vll par; vll rank; GroupSet(T) Add; V<T> elm; sUFT(ll tsize, GroupSet(T) Add = ADD, ll mode = 0) : tsize(tsize), Add(Add), mode(mode) { init(); } void init() { par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); elm.asn(tsize, Id(Add)); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { elm[x] = Act(Add)(elm[x], elm[y]); par[y] = x; } else { if (rank[x] < rank[y]) { elm[y] = Act(Add)(elm[x], elm[y]); par[x] = y; } else { elm[x] = Act(Add)(elm[x], elm[y]); par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } } T size(ll x) { return elm[root(x)]; } T &operator[](ll x) { return elm[x]; } }; template <typename valtype> class SegT { public: ll size; vector<valtype> v; valtype initv; function<valtype(valtype x, valtype y)> calc; SegT() {} SegT(const SegT &segt) {} SegT(ll sizeget, ll modeget = 0) { sizeset(sizeget); modeset(modeget); init(); } SegT(vector<valtype> cpyvec, ll modeget = 0) { sizeset(cpyvec.sz); modeset(modeget); init(); copy(cpyvec); } SegT(ll sizeget, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(sizeget); initv = initvget; calc = calcget; init(); } SegT(vector<valtype> cpyvec, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(cpyvec.sz); initv = initvget; calc = calcget; init(); copy(cpyvec); } void sizeset(ll rsize) { size = DigN2(rsize); if (rsize == Pow2(size - 1)) size--; return; } void modeset(ll mode) { switch (mode) { case 0: initv = 0; calc = [](valtype x, valtype y) { return x + y; }; break; case 1: initv = INF; calc = [](valtype x, valtype y) { return min(x, y); }; break; case 2: initv = -INF; calc = [](valtype x, valtype y) { return max(x, y); }; break; } return; } void init() { v.asn(Pow2(size + 1) - 1, initv); } void copy(vector<valtype> cpyvec) { REP(i, min(cpyvec.sz, Pow2(size))) set(i, cpyvec[i]); } ll i2v(ll i) const { if (i < 0 || i >= Pow2(size)) return -1; return Pow2(size) + i - 1; } ll top(ll i) const { if (i == 0) return -1; return (i - 1) / 2; } pl bot(ll i) const { if (i + 1 >= Pow2(size)) return mp(-1, -1); return mp(2 * i + 1, 2 * i + 2); } void set(ll i, valtype x) { i = i2v(i); v[i] = x; while (i > 0) { i = top(i); v[i] = calc(v[bot(i).fi], v[bot(i).se]); } return; } void add(ll i, valtype x) { set(i, v[i2v(i)] + x); return; } valtype operator[](const ll &i) const { return v[i2v(i)]; } // valtype que(ll a = 0, ll b = Pow2(size) - 1) { valtype que(ll a, ll b) { if (a == b) return v[i2v(a)]; if (a > b) return initv; // swap(a, b); valtype ans = initv; ll ai = i2v(a); ll bi = i2v(b); FOR(i, 1, size + 1) { if (a > b) break; if (a % Pow2(i)) { ans = calc(ans, v[ai]); a += Pow2(i - 1); ai = top(ai) + 1; } else { ai = top(ai); } if (a > b) break; if ((b + 1) % Pow2(i)) { ans = calc(ans, v[bi]); b -= Pow2(i - 1); bi = top(bi) - 1; } else { bi = top(bi); } if (a > b) break; } return ans; } valtype que(ll b) { return que(0, b); } valtype que() { return que(0, Pow2(size) - 1); } }; /*template <class Type> class DP { public: vector<Type> v; Type initv; vll size, block; DP() {} DP(const DP &dp) {} template<class... Args> DP(Args... args) { block.asn(1, 1); Initialize(args...); } void Initialize(Type initv_) { initv = initv_; v.asn(block.bk, initv); } template<class... Args> void Initialize(ll val, Args... args) { size.pb(val); block.pb(block.bk*val); Initialize(args...); } };*/ pl Bezout(ll a, ll b) { if (b != 0) { pl xy; xy = Bezout(b, a % b); return mp(xy.se, xy.fi - ((a / b) * xy.se)); } else { return mp(1, 0); } } pl Bez(ll a, ll b, ll c) { pl xy; ll x, y, z, gc; xy = Bezout(a, b); gc = gcd(a, b); if (c % gc != 0) return mp(-1, -1); x = xy.fi * (c / gc); y = xy.se * (c / gc); if (x < 0) z = rup(-x, (b / gc)); if (x >= 0) z = -x / (b / gc); x += z * (b / gc); y -= z * (a / gc); return mp(x, y); } ll DigS10(ll n) { ll ans = 0; while (1) { ans += n % 10; n /= 10; if (!n) break; } return ans; } ll isP(ll n) { if (n <= 1) return 0; FOR(i, 2, (ll)sqrt(n) + 1) { if (n % i == 0) return 0; } return 1; } ll Tot(ll n) { if (n <= 0) return 0; ll ans = n, x = 2; while (x * x <= n) { if (n % x == 0) { ans -= ans / x; while (n % x == 0) n /= x; } x++; } if (n > 1) ans -= ans / n; return ans; } template <class T> struct Graph { public: ll vSize; ll eMode; ll mapMode; GroupSet(T) Add; CompareSet(T) Less; CompareSet(T) Equal; VV<P<T, ll>> adj; map<pl, T> len; Graph(ll vSize, ll eMode = 0, ll mapMode = 0, GroupSet(T) Add = ADD, CompareSet(T) Less = LES, CompareSet(T) Equal = EQ) : vSize(vSize), eMode(eMode), mapMode(mapMode), Add(Add), Less(Less), Equal(Equal) {} void Init() { adj.asn(vSize, V<P<T, ll>>()); } void AddE(ll x, ll y, T cost) { iAddE(x, y, cost); if (!eMode) iAddE(y, x, cost); } void iAddE(ll x, ll y, T cost) { adj[x].pb(mp(cost, y)); if (mapMode) len[mp(x, y)] = cost; } P<bool, T> getE(ll x, ll y) { if (!len.count(mp(x, y))) return mp(false, Id(Less)); return mp(true, len[mp(x, y)]); } V<T> Dijk(ll x) { V<T> ans(vSize, Id(Less)); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans[now.se] = segt[now.se].fi; segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se)); continue; } if (Act(Less)(Act(Add)(ans[now.se], i.fi), segt[i.se].fi)) { segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se)); continue; } } } return ans; } V<P<T, vll>> rDijk(ll x) { V<P<T, vll>> ans(vSize, mp(Id(Less), vll())); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans[now.se].fi = segt[now.se].fi; segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se)); ans[i.se].se = {now.se}; continue; } if (Act(Less)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) { segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se)); ans[i.se].se = {now.se}; continue; } if (Act(Equal)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) { ans[i.se].se.pb(now.se); continue; } } } return ans; } T Prim(ll x = 0) { T ans = Id(Add); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans = Act(Add)(ans, segt[now.se].fi); segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, i); continue; } if (Act(Less)(i.fi, segt[i.se].fi)) { segt.set(i.se, i); continue; } } } return ans; } P<T, V<P<T, vll>>> rPrim(ll x = 0) { P<T, V<P<T, vll>>> ans = mp(Id(Add), V<P<T, vll>>(vSize, mp(Id(Less), vll()))); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans.se[now.se].fi = segt[now.se].fi; ans.fi = Act(Add)(ans.fi, ans.se[now.se].fi); segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, i); ans.se[i.se].se = {now.se}; continue; } if (Act(Less)(i.fi, segt[i.se].fi)) { segt.set(i.se, i); ans.se[i.se].se = {now.se}; continue; } if (Act(Equal)(i.fi, segt[i.se].fi)) { ans.se[i.se].se.pb(now.se); continue; } } } return ans; } }; template <class T> struct Sum { public: V<T> v, s; ll size; GroupSet(T) Add; Sum(V<T> v, GroupSet(T) Add = ADD) : v(v), size(v.sz), Add(Add) { Init(); Calc(); } void Init() { s.asn(size + 1, Id(Add)); } void Calc() { REP(i, size) s[i + 1] = Act(Add)(s[i], v[i]); } T operator()(ll x) { if (x < -1) x = -1; if (x > size - 1) x = size - 1; return s[x + 1]; } T operator()(ll l, ll r) { if (l < 0) l = 0; if (r >= size) r = size - 1; if (l > r) return Id(Add); return InvAct(Add)(s[r + 1], s[l]); } }; using sumll = Sum<ll>; template <class T> struct Sum2 { public: VV<T> v, s; ll RowSize, ColumnSize; GroupSet(T) Add; Sum2(VV<T> v, GroupSet(T) Add = ADD) : v(v), RowSize(v.sz), ColumnSize(v.sz ? v[0].sz : 0), Add(Add) { Init(); Calc(); } void Init() { s.asn(RowSize + 1, V<T>(ColumnSize + 1, Id(Add))); } void Calc() { REP1(r, RowSize) { REP1(c, ColumnSize) { // s[r][c] = InvAct(Add)(Act(Add)(Act(Add)(v[r - //1][c - 1], operator()(r - 1, c - 2)), operator()(r - 2, c - 1)), //operator()(r - 2, c - 2)); s[r][c] = Act(Add)(s[r][c - 1], v[r - 1][c - 1]); } } REP1(r, RowSize) { REP1(c, ColumnSize) { s[r][c] = Act(Add)(s[r - 1][c], s[r][c]); } } } T operator()(ll r, ll c) { if (r < -1) r = -1; if (c < -1) c = -1; if (r > RowSize - 1) r = RowSize - 1; if (c > ColumnSize - 1) c = ColumnSize - 1; return s[r + 1][c + 1]; } T operator()(ll r1, ll c1, ll r2, ll c2) { if (r1 < 0) r1 = 0; if (c1 < 0) c1 = 0; if (r2 >= RowSize) r2 = RowSize - 1; if (c2 >= ColumnSize) c2 = ColumnSize - 1; if (r1 > r2) return Id(Add); if (c1 > c2) return Id(Add); return InvAct(Add)(Act(Add)(s[r2 + 1][c2 + 1], s[r1][c1]), Act(Add)(s[r2 + 1][c1], s[r1][c2 + 1])); } }; using sumll2 = Sum2<ll>; template <class T> struct Point2 { public: VV<T> v; ll h, w; Point2() : h(0), w(0) {} Point2(ll h, ll w) : h(h), w(w) { asn(h, w); } Point2(ll h, ll w, T val) : h(h), w(w) { asn(h, w, val); } Point2(VV<T> cv) : h(cv.sz), w(cv.sz ? cv[0].sz : 0) { asn(h, w); copy(cv); } void assign(ll h, ll w) { v.asn(h, V<T>(w)); } void assign(ll h, ll w, ll val) { v.asn(h, V<T>(w, val)); } void copy(VV<T> cv) { REP(_h, h) REP(_w, w) v[_h][_w] = cv[_h][_w]; } T &operator()(ll h, ll w) { return v[h][w]; } T &operator()(pl p) { return v[p.fi][p.se]; } T &operator[](pl p) { return v[p.fi][p.se]; } }; template <class T> using P2 = Point2<T>; template <ll Mod> struct Modll { public: ll v; Modll() : v(0) {} Modll(ll _v) { set(_v % Mod + Mod); } Modll &set(ll _v) { v = (_v < Mod) ? _v : (_v - Mod); return *this; } Modll pow(ll n) const { Modll x = *this, ans = 1; while (n) { if (n & 1) ans *= x; x *= x; n >>= 1; } return ans; } Modll inverse() const { return (*this).pow(Mod - 2); } Modll operator+(const Modll &m) const { return Modll().set(v + m.v); } Modll operator-(const Modll &m) const { return Modll().set(Mod + v - m.v); } Modll operator*(const Modll &m) const { return Modll().set((ull(v) * m.v) % Mod); } Modll operator/(const Modll &m) const { return *this * m.inv; } Modll &operator+=(const Modll &m) { return *this = *this + m; } Modll &operator-=(const Modll &m) { return *this = *this - m; } Modll &operator*=(const Modll &m) { return *this = *this * m; } Modll &operator/=(const Modll &m) { return *this = *this / m; } Modll operator-() const { return Modll(0) - *this; } explicit operator bool() const { return v != 0; } friend istream &operator>>(istream &in, Modll &m) { return in >> m.v; } friend ostream &operator<<(ostream &out, const Modll &m) { return out << m.v; } }; using mll = Modll<MOD>; using vmll = V<mll>; using vvmll = V<vmll>; using vvvmll = V<vvmll>; vmll MFactMemo(2, 1); vmll MIFactMemo(2, 1); mll mFact(ll n) { if (MFactMemo.sz <= n) { ll oldsize = MFactMemo.sz; MFactMemo.res(n + 1, 1); FOR(i, oldsize, n) MFactMemo[i] = MFactMemo[i - 1] * i; } return MFactMemo[n]; } mll miFact(ll n) { if (MIFactMemo.sz <= n) { ll oldsize = MIFactMemo.sz; MIFactMemo.res(n + 1, 1); MIFactMemo.bk = mFact(n).inv; rFOR(i, oldsize + 1, n) MIFactMemo[i - 1] = MIFactMemo[i] * i; } return MIFactMemo[n]; } mll mComb(ll n, ll k) { if (n < 0 || k < 0 || n < k) return 0; return mFact(n) * miFact(k) * miFact(n - k); } ll LIS(vll v, ll m = 0) { if (v.sz > 0) { ll ans = 0; vll dp(v.sz, INF); FOR(i, 0, v.sz - 1) { dp[m ? UPB(dp, v[i]) : LOWB(dp, v[i])] = v[i]; } FOR(i, 0, v.sz - 1) { if (dp[i] == INF) break; ans++; } return ans; } else { return 0; } } ll Bsrch(function<bool(ll x)> f, ll mi, ll ma) { ll ng = mi - 1, ok = ma, mid; while (ok - ng > 1) { mid = (ng + ok) / 2; (f(mid) ? ok : ng) = mid; } return ok; } template <class T, class M = decltype(MUL), class S = decltype(ADD)> VV<T> MultiMatrix(VV<T> A, VV<T> B, M Mul = MUL, S Add = ADD) { VV<T> ans; ll ii = A.sz; if (!ii) return ans; ll jj = A[0].sz; if (!jj) return ans; ll jj2 = B.sz; if (!jj2) return ans; if (jj != jj2) return ans; ll kk = B[0].sz; if (!kk) return ans; ans.asn(ii, V<T>(kk, 0)); REP(i, ii) { REP(k, kk) { REP(j, jj) { ans[i][k] = Act(Add)(ans[i][k], Act(Mul)(A[i][j], B[j][k])); } } } return ans; } vvll CombMemo(1000, vll(1000, -1)); ll Comb(ll n, ll k) { if ((n < 0) || (k < 0)) return 0; if (CombMemo[n][k] == -1) { if (n < k) CombMemo[n][k] = 0; else { if (n == 0) CombMemo[n][k] = 1; else if (k == 0) CombMemo[n][k] = 1; else if (n == k) CombMemo[n][k] = 1; else CombMemo[n][k] = Comb(n - 1, k - 1) + Comb(n - 1, k); } } return CombMemo[n][k]; } template <class T> map<T, ll> Dict(V<T> v) { map<T, ll> m; if (!v.sz) return m; SORT(v); UNIQUE(v); REP(i, v.sz) { m[v[i]] = i; } return m; } template <class T> vll Cmprs(V<T> v) { auto m = Dict(v); vll ans(v.sz); REP(i, v.sz) { ans[i] = m[v[i]]; } return ans; } template <class T> vll PCmprs(V<T> v) { if (v.sz == 0) return V<T>(); vll tmp(v.sz); vll ans(v.sz); IOTA(tmp, 0); IOTA(ans, 0); sort(tmp.bgn, tmp.en, clam(v[l] < v[r])); sort(ans.bgn, ans.en, clam(tmp[l] < tmp[r])); return ans; } ll BblCnt(vll rv) { vll v = PCmprs(rv); SegT<ll> b(v.sz, 0); ll ans = 0; REP(i, v.sz) { ans += (i - b.que(0, v[i])); b.add(v[i], 1); } return ans; } pl NGrid(pl p, ll i, ll H, ll W) { p = mp(p.fi + DX[i], p.se + DY[i]); if (p.fi < 0 || p.fi >= H || p.se < 0 || p.se >= W) return mp(INF, INF); return p; } vvll llGrid(vs v) { vvll r(v.sz, vll(v.sz ? v[0].sz : 0, 0)); REP(h, v.sz) REP(w, v.sz ? v[0].sz : 0) r[h][w] = (v[h][w] == '#'); return r; } template <class T> auto ven(T val) { return val; } template <> auto ven<int>(int val) { return (ll)val; } template <class T, class... Args> auto ven(T val, Args... args) { auto tmp = ven(args...); return V<decltype(tmp)>(val, tmp); } template <class T> void zind(T &v) { v--; } template <class T> void zind(V<T> &v) { ROR(v, i) zind(i); } template <class T, class... Args> void zind(T &v, Args &...args) { zind(v); zind(args...); } void Solve(); signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20) << fixed; Solve(); } void Solve() { li(N, K); vli(N, a); SegT<mll> st(K + 1, mll(0), lam(l + r)); st.set(0, 1); REP(i, N) { rREP(j, K + 1) st.set(j, st.que(max(0, j - a[i]), j)); } sal(st[K]); }
#include <bits/stdc++.h> using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; template <class S, class T> using P = pair<S, T>; template <class... T> using TP = tuple<T...>; using ll = long long; using ull = unsigned long long; using dbl = double; using str = string; using vll = V<ll>; using vvll = V<vll>; using vvvll = V<vvll>; using pl = P<ll, ll>; using tl = TP<ll, ll, ll>; using vpl = V<pl>; using vvpl = V<vpl>; using vtl = V<tl>; using vvtl = V<vtl>; using vs = V<str>; using vvs = V<vs>; using vd = V<dbl>; using vvd = V<vd>; using vvvd = V<vvd>; using qll = queue<ll>; using qpl = queue<pl>; using stll = stack<ll>; using stpl = stack<pl>; using mapll = map<ll, ll>; using setll = set<ll>; using pqll = priority_queue<ll>; // #define int ll #define fi first #define se second #define mp make_pair #define mt make_tuple #define pb push_back #define pob pop_back() #define pf push_front #define pof pop_front() #define sz size() #define bgn begin() #define en end() #define asn assign #define emp empty() #define fr front() #define bk back() #define clr clear() #define ins insert #define ers erase #define res resize #define tp top() #define p_q priority_queue #define inv inverse() #define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++) #define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--) #define REP(i, a) FOR((i), 0, (ll)(a)-1) #define REP0(i, a) FOR((i), 0, (ll)(a)) #define REP1(i, a) FOR((i), 1, (ll)(a)) #define rREP(i, a) rFOR((i), 0, (ll)(a)-1) #define rREP0(i, a) rFOR((i), 0, (ll)(a)) #define rREP1(i, a) rFOR((i), 1, (ll)(a)) #define ROR(v, i) for (auto &(i) : (v)) #define IOTA(a, n) iota((a).bgn, (a).en, (n)) #define SORT(a) sort((a).bgn, (a).en) #define rSORT(a) sort((a).rbegin(), (a).rend()) #define UNIQUE(a) (a).erase(unique((a).bgn, (a).en), (a).en) #define PREVP(a) prev_permutation((a).bgn, (a).en) #define NEXTP(a) next_permutation((a).bgn, (a).en) #define BINS(a, b) binary_search((a).bgn, (a).en, (b)) #define LOWB(a, b) (lower_bound((a).bgn, (a).en, (b)) - (a).bgn) #define UPB(a, b) (upper_bound((a).bgn, (a).en, (b)) - (a).bgn) #define CNT(a, b) count((a).bgn, (a).en, b) #define SUM(a) accumulate((a).bgn, (a).en, 0) #define REV(a) reverse((a).bgn, (a).en) #define REGS(a, b) regex_search((a), regex(b)) #define REGM(a, b) regex_match((a), regex(b)) #define yn(a) cout << ((a) ? "yes" : "no") << "\n"; #define Yn(a) cout << ((a) ? "Yes" : "No") << "\n"; #define YN(a) cout << ((a) ? "YES" : "NO") << "\n"; #define imp(a) cout << ((a) ? "possible" : "impossible") << "\n"; #define Imp(a) cout << ((a) ? "Possible" : "Impossible") << "\n"; #define IMP(a) cout << ((a) ? "POSSIBLE" : "IMPOSSIBLE") << "\n"; #define fs(a) cout << ((a) ? "second" : "first") << "\n"; #define Fs(a) cout << ((a) ? "Second" : "First") << "\n"; #define FS(a) cout << ((a) ? "SECOND" : "FIRST") << "\n"; // #define say(a) cout <<(a); // #define sal(a) cout <<(a)<<"\n"; #define sak cout << "\n"; #define sas cout << " "; #define sat cout << "\t"; #define dbg(a) cerr << (#a) << ": " << (a) << "\n"; #define dbgs(...) \ dal(#__VA_ARGS__); \ dal(__VA_ARGS__); #define c2l(a) ((ll)(a - 48)) #define a2l(a) ((ll)(a - 97)) #define A2l(a) ((ll)(a - 65)) #define l2c(a) ((char)(a + 48)) #define l2a(a) ((char)(a + 97)) #define l2A(a) ((char)(a + 65)) #define DigN2(a) ((llabs(a) == 0) ? (1) : ((ll)(log2(double(llabs(a)))) + 1)) #define DigN10(a) ((llabs(a) == 0) ? (1) : ((ll)(log10(double(llabs(a)))) + 1)) #define Dig2(a, b) (((a) >> (b)) & 1) #define Dig10(a, b) (ll)(((a) / ((ll)(pow(10.0, (double)(b))))) % 10) #define Pow2(a) ((ll)(1) << (a)) #define Pow10(a) ((ll)(pow(10.0, double(a)))) #define LSB(a) ((a) & (-(a))) /*#define llin(a) ll (a);cin >>(a); #define llin2(a,b) ll (a),(b);cin >>(a)>>(b); #define llin3(a,b,c) ll (a),(b),(c);cin >>(a)>>(b)>>(c); #define stin(a) string (a);cin >>(a);*/ #define vin(v) \ ROR((v), (i)) { cin >> (i); }; #define vllin(N, v) \ vll(v)((N)); \ vin(v); #define vllin2(N, a, b) \ vll(a)(N), (b)(N); \ REP(i, N) { cin >> (a)[i] >> (b)[i]; }; #define vsin(N, v) \ vs(v)((N)); \ vin(v); #define rdn(a, b) ((a) / (b)) #define rou(a, b) \ ((((double(a) / double(b)) - ((a) / (b))) < 0.5) ? ((a) / (b)) \ : (((a) / (b)) + 1)) #define rup(a, b) ((((a) % (b)) == 0) ? ((a) / (b)) : (((a) / (b)) + 1)) #define powll(a, b) (ll)(pow((double)(a), (double)(b))) #define Triangle(x1, y1, x2, y2, x3, y3) \ (((x1) - (x2)) * ((y1) - (y3)) - ((x1) - (x3)) * ((y1) - (y2))) #define tg(t, i) get<i>(t) #define Id(x) get<0>(x) #define Act(x) get<1>(x) #define InvAct(x) get<2>(x) #define mg(id, act) mt(id, act, lam(l)) // #define MonoidSet(T) TP<T, function<T(T, T)>> #define GroupSet(T) TP<T, function<T(T, T)>, function<T(T, T)>> #define CompareSet(T) TP<T, function<bool(T, T)>> #define lam(lr) ([](auto l, auto r) { return (lr); }) #define elam(lr) ([=](auto l, auto r) { return (lr); }) #define clam(lr) ([&](auto l, auto r) { return (lr); }) #define lamr(lr) ([](auto l, auto r) { lr }) #define elamr(lr) ([=](auto l, auto r) { lr }) #define clamr(lr) ([&](auto l, auto r) { lr }) #define min(...) Operation(MIN, __VA_ARGS__) #define max(...) Operation(MAX, __VA_ARGS__) #define gcd(...) Operation(GCD, __VA_ARGS__) #define lcm(...) Operation(LCM, __VA_ARGS__) #define vmin(...) VOperation(MIN, __VA_ARGS__) #define vmax(...) VOperation(MAX, __VA_ARGS__) #define vgcd(...) VOperation(GCD, __VA_ARGS__) #define vlcm(...) VOperation(LCM, __VA_ARGS__) #define vsum(...) VOperation(ADD, __VA_ARGS__) #define vpro(...) VOperation(MUL, __VA_ARGS__) #define emin(a, ...) ((a) = min((a), __VA_ARGS__)) #define emax(a, ...) ((a) = max((a), __VA_ARGS__)) #define egcd(a, ...) ((a) = gcd((a), __VA_ARGS__)) #define elcm(a, ...) ((a) = lcm((a), __VA_ARGS__)) #define ope Operation #define vope VOperation #define svll SumV<ll> #define svvll SumV2<ll> #define li(...) \ ll __VA_ARGS__; \ Input(__VA_ARGS__); #define si(...) \ str __VA_ARGS__; \ Input(__VA_ARGS__); // #define vli(size, ...) vll __VA_ARGS__;vInitInput(size,__VA_ARGS__); #define vlr(size, ...) \ vll __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vlc(size, ...) \ vll __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vli(size, ...) \ vll __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vsr(size, ...) \ vs __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vsc(size, ...) \ vs __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vsi(size, ...) \ vs __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vli2(rowSize, columnSize, ...) \ vvll __VA_ARGS__; \ vInitInput2(rowSize, columnSize, __VA_ARGS__); #define vplr(size, ...) \ vpl __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vplc(size, ...) \ vpl __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vpli(size, ...) \ vpl __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; // const ll MOD = 924844033; // const ll MOD = 9007199254740881; const ll INF = 1LL << 60; // 1.15e18 const double PI = acos(-1.0); const vll DX = {0, -1, 0, 1, 0, -1, 1, 1, -1}; const vll DY = {0, 0, -1, 0, 1, -1, -1, 1, 1}; const str alp = "abcdefghijklmnopqrstuvwxyz"; const str ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; template <class T> auto GetVectorValueType(T v) { return v; } template <class T> auto GetVectorValueType(V<T> v) { return GetVectorValueType(T()); } template <class S, class T> istream &operator>>(istream &in, P<S, T> &p) { return in >> p.fi >> p.se; } template <class T> istream &operator>>(istream &in, V<T> &v) { REP(i, v.sz) in >> v[i]; return in; } void Input() {} template <class Var, class... Args> void Input(Var &var, Args &...args) { cin >> var; Input(args...); } void vInit(ll size) {} template <class T, class... Args> void vInit(ll size, V<T> &v, Args &...args) { v.res(size); vInit(size, args...); } void vInput(ll size) {} template <class T, class... Args> void vInput(ll size, V<T> &v, Args &...args) { REP(i, size) cin >> v[i]; vInput(size, args...); } void vInputR(ll size) {} template <class T, class... Args> void vInputR(ll size, V<T> &v, Args &...args) { REP(i, size) cin >> v[i]; vInputR(size, args...); } void vInputNumC(ll num) {} template <class T, class... Args> void vInputNumC(ll num, V<T> &v, Args &...args) { cin >> v[num]; vInputNumC(num, args...); } void vInputC(ll size) {} template <class... Args> void vInputC(ll size, Args &...args) { REP(i, size) vInputNumC(i, args...); } void vInitInputR(ll size) {} template <class... Args> void vInitInputR(ll size, Args &...args) { vInit(size, args...); vInputR(size, args...); } void vInitInputC(ll size) {} template <class... Args> void vInitInputC(ll size, Args &...args) { vInit(size, args...); vInputC(size, args...); } void vInit2(ll rowSize, ll columnSize) {} template <class T, class... Args> void vInit2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) { v.asn(rowSize, V<T>(columnSize)); vInit2(rowSize, columnSize, args...); } void vInput2(ll rowSize, ll columnSize) {} template <class T, class... Args> void vInput2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) { REP(r, rowSize) { REP(c, columnSize) { cin >> v[r][c]; } } vInput2(rowSize, columnSize, args...); } void vInitInput2(ll rowSize, ll columnSize) {} template <class... Args> void vInitInput2(ll rowSize, ll columnSize, Args &...args) { vInit2(rowSize, columnSize, args...); vInput2(rowSize, columnSize, args...); } template <class S, class T> ostream &operator<<(ostream &out, const P<S, T> &p) { return out << "[" << p.fi << ", " << p.se << "]"; } template <class T> ostream &operator<<(ostream &out, V<T> &v) { if (v.emp) return out << "{}"; else { auto itr = v.bgn; out << "{" << *itr; itr++; while (itr != v.en) { out << ", " << *itr; itr++; } out << "}"; return out; } } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> &m) { if (m.emp) return out << "<[]>"; else { auto itr = m.bgn; out << "< [" << (itr->fi) << ": " << (itr->se); itr++; while (itr != m.en) { out << "], [" << (itr->fi) << ": " << (itr->se); itr++; } out << "] >"; return out; } } template <class T> ostream &operator<<(ostream &out, const set<T> &s) { if (s.emp) return out << "<>"; else { auto itr = s.bgn; out << "<" << *itr; itr++; while (itr != s.en) { out << ", " << *itr; itr++; } out << ">"; return out; } } void say() {} template <class T> void say(T t) { cout << t; } template <class Head, class... Body> void say(Head head, Body... body) { cout << head << " "; say(body...); } void sal() { cout << "\n"; } template <class... Args> void sal(Args... args) { say(args...); cout << "\n"; } void day() {} template <class T> void day(T t) { cerr << t; } template <class Head, class... Body> void day(Head head, Body... body) { cerr << head << " "; day(body...); } void dal() { cerr << "\n"; } template <class... Args> void dal(Args... args) { day(args...); cerr << "\n"; } void salv() {} template <class T> void salv(V<T> v) { if (v.emp) sal(); else { auto itr = v.bgn; say(*itr); itr++; while (itr != v.en) { sas; say(*itr); itr++; } sak; } } template <class T> void salv(VV<T> v) { if (v.emp) sal(); else { ROR(v, i) salv(i); } } template <class T, class... Args> void salv(T v, Args... args) { salv(v); salv(args...); } template <class L, class R> auto Gcd(L l, R r) -> decltype(l + r) { if (l < r) swap(l, r); return r ? Gcd(r, l % r) : l; } template <class L, class R> auto Lcm(L l, R r) { if (!l || !r) return 0; return l / Gcd(l, r) * r; } /* auto LES = mp(INF, lam(return l < r;)); auto GRT = mp(-INF, lam(return l > r;)); auto EQ = mp(0, lam(return l == r;)); auto ADD = mp(0, lam(return l + r;)); auto SUB = mp(0, lam(return l - r;)); auto MUL = mp(1, lam(return l * r;)); auto DIV = mp(1, lam(return l / r;)); auto MDL = mp(1, lam(return l % r;)); auto XOR = mp(0, lam(return l ^ r;)); auto OR = mp(0, lam(return l | r;)); auto AND = mp(((ll)(1) << 63) - 1, lam(return l & r;)); auto MIN = mp(INF, lam(return (l < r) ? l : r;)); auto MAX = mp(-INF, lam(return (l > r) ? l : r;)); auto GCD = mp(0, lam(return Gcd(l, r);)); auto LCM = mp(1, lam(return Lcm(l, r);)); */ auto LES = mp(INF, lam(l < r)); auto GRT = mp(-INF, lam(l > r)); auto EQ = mp(0, lam(l == r)); auto ADD = mt(0, lam(l + r), lam(l - r)); auto MUL = mt(1, lam(l *r), lam(l / r)); auto XOR = mt(0, lam(l ^ r), lam(l ^ r)); auto OR = mg(0, lam(l | r)); auto AND = mg(((ll)(1) << 63) - 1, lam(l &r)); auto MIN = mg(0, lam((l < r) ? l : r)); auto MAX = mg(0, lam((l > r) ? l : r)); auto GCD = mg(0, lam(Gcd(l, r))); auto LCM = mg(0, lam(Lcm(l, r))); template <class OperationType> auto Operation(OperationType A) { return Id(A); } template <class OperationType, class T> auto Operation(OperationType A, T x) { return x; } template <class OperationType, class T, class... Args> auto Operation(OperationType A, T x, Args... args) { auto tmp = Operation(A, args...); return Act(A)(x, tmp); } template <class OperationType> auto VOperation(OperationType A) { return Id(A); } template <class OperationType, class T> auto VOperation(OperationType A, T x) { return x; } template <class OperationType, class T> auto VOperation(OperationType A, V<T> v) { if (v.emp) { decltype(GetVectorValueType(T())) tmp = Id(A); return tmp; } auto tmp = VOperation(A, v[0]); FOR(i, 1, v.sz - 1) tmp = Act(A)(tmp, VOperation(A, v[i])); return tmp; } template <class OperationType, class T, class... Args> auto VOperation(OperationType A, T x, Args... args) { auto xResult = VOperation(A, x); auto tmp = VOperation(A, args...); return Act(A)(xResult, tmp); } ll Bset(ll a, ll b, ll c) { if (c) a |= b; else a &= ~b; return a; } struct UFT { public: ll tsize; ll mode; vll par; vll rank; UFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } } ll size(ll x) { return -par[root(x)]; } }; template <class T> struct pUFT { public: ll tsize; ll now; vll par; vll rank; vll mtime; vvll sizepi; VV<T> sizepv; V<T> elm; GroupSet(T) Add; pUFT(ll tsize, GroupSet(T) Add = ADD) : tsize(tsize), Add(Add) { init(); } void init() { now = 0; par.asn(tsize, -1); rank.asn(tsize, 0); mtime.asn(tsize, INF); sizepi.asn(tsize, {0}); sizepv.asn(tsize, {}); } void set(ll x, T s) { elm[x] = s; sizepv[x] = {s}; } ll root(ll x, ll t) { return (mtime[x] > t) ? x : root(par[x], t); } bool same(ll x, ll y, ll t) { return root(x, t) == root(y, t); } ll merge(ll x, ll y) { now++; x = root(x, now); y = root(y, now); if (x != y) { if (rank[x] < rank[y]) { elm[y] = Act(Add)(elm[x], elm[y]); sizepi[y].pb(now); sizepv[y].pb(elm[y]); par[x] = y; mtime[x] = now; } else { elm[x] = Act(Add)(elm[x], elm[y]); sizepi[x].pb(now); sizepv[x].pb(elm[x]); par[y] = x; mtime[y] = now; if (rank[x] == rank[y]) rank[x]++; } } return now; } T size(ll x, ll t) { x = root(x, t); return sizepv[x][UPB(sizepi[x], t) - 1]; } }; struct wUFT { public: ll tsize; ll mode; vll par; vll rank; vll dweight; wUFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); dweight.asn(tsize, 0); } ll root(ll x) { if (par[x] < 0) return x; else { ll r = root(par[x]); dweight[x] += dweight[par[x]]; return par[x] = r; } } ll weight(ll x) { root(x); return dweight[x]; } ll diff(ll x, ll y) { return weight(y) - weight(x); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y, ll w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; dweight[x] = -w; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; dweight[y] = w; } } } ll size(ll x) { return -par[root(x)]; } }; template <class T> struct sUFT { public: ll tsize; ll mode; vll par; vll rank; GroupSet(T) Add; V<T> elm; sUFT(ll tsize, GroupSet(T) Add = ADD, ll mode = 0) : tsize(tsize), Add(Add), mode(mode) { init(); } void init() { par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); elm.asn(tsize, Id(Add)); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { elm[x] = Act(Add)(elm[x], elm[y]); par[y] = x; } else { if (rank[x] < rank[y]) { elm[y] = Act(Add)(elm[x], elm[y]); par[x] = y; } else { elm[x] = Act(Add)(elm[x], elm[y]); par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } } T size(ll x) { return elm[root(x)]; } T &operator[](ll x) { return elm[x]; } }; template <typename valtype> class SegT { public: ll size; vector<valtype> v; valtype initv; function<valtype(valtype x, valtype y)> calc; SegT() {} SegT(const SegT &segt) {} SegT(ll sizeget, ll modeget = 0) { sizeset(sizeget); modeset(modeget); init(); } SegT(vector<valtype> cpyvec, ll modeget = 0) { sizeset(cpyvec.sz); modeset(modeget); init(); copy(cpyvec); } SegT(ll sizeget, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(sizeget); initv = initvget; calc = calcget; init(); } SegT(vector<valtype> cpyvec, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(cpyvec.sz); initv = initvget; calc = calcget; init(); copy(cpyvec); } void sizeset(ll rsize) { size = DigN2(rsize); if (rsize == Pow2(size - 1)) size--; return; } void modeset(ll mode) { switch (mode) { case 0: initv = 0; calc = [](valtype x, valtype y) { return x + y; }; break; case 1: initv = INF; calc = [](valtype x, valtype y) { return min(x, y); }; break; case 2: initv = -INF; calc = [](valtype x, valtype y) { return max(x, y); }; break; } return; } void init() { v.asn(Pow2(size + 1) - 1, initv); } void copy(vector<valtype> cpyvec) { REP(i, min(cpyvec.sz, Pow2(size))) set(i, cpyvec[i]); } ll i2v(ll i) const { if (i < 0 || i >= Pow2(size)) return -1; return Pow2(size) + i - 1; } ll top(ll i) const { if (i == 0) return -1; return (i - 1) / 2; } pl bot(ll i) const { if (i + 1 >= Pow2(size)) return mp(-1, -1); return mp(2 * i + 1, 2 * i + 2); } void set(ll i, valtype x) { i = i2v(i); v[i] = x; while (i > 0) { i = top(i); v[i] = calc(v[bot(i).fi], v[bot(i).se]); } return; } void add(ll i, valtype x) { set(i, v[i2v(i)] + x); return; } valtype operator[](const ll &i) const { return v[i2v(i)]; } // valtype que(ll a = 0, ll b = Pow2(size) - 1) { valtype que(ll a, ll b) { if (a == b) return v[i2v(a)]; if (a > b) return initv; // swap(a, b); valtype ans = initv; ll ai = i2v(a); ll bi = i2v(b); FOR(i, 1, size + 1) { if (a > b) break; if (a % Pow2(i)) { ans = calc(ans, v[ai]); a += Pow2(i - 1); ai = top(ai) + 1; } else { ai = top(ai); } if (a > b) break; if ((b + 1) % Pow2(i)) { ans = calc(ans, v[bi]); b -= Pow2(i - 1); bi = top(bi) - 1; } else { bi = top(bi); } if (a > b) break; } return ans; } valtype que(ll b) { return que(0, b); } valtype que() { return que(0, Pow2(size) - 1); } }; /*template <class Type> class DP { public: vector<Type> v; Type initv; vll size, block; DP() {} DP(const DP &dp) {} template<class... Args> DP(Args... args) { block.asn(1, 1); Initialize(args...); } void Initialize(Type initv_) { initv = initv_; v.asn(block.bk, initv); } template<class... Args> void Initialize(ll val, Args... args) { size.pb(val); block.pb(block.bk*val); Initialize(args...); } };*/ pl Bezout(ll a, ll b) { if (b != 0) { pl xy; xy = Bezout(b, a % b); return mp(xy.se, xy.fi - ((a / b) * xy.se)); } else { return mp(1, 0); } } pl Bez(ll a, ll b, ll c) { pl xy; ll x, y, z, gc; xy = Bezout(a, b); gc = gcd(a, b); if (c % gc != 0) return mp(-1, -1); x = xy.fi * (c / gc); y = xy.se * (c / gc); if (x < 0) z = rup(-x, (b / gc)); if (x >= 0) z = -x / (b / gc); x += z * (b / gc); y -= z * (a / gc); return mp(x, y); } ll DigS10(ll n) { ll ans = 0; while (1) { ans += n % 10; n /= 10; if (!n) break; } return ans; } ll isP(ll n) { if (n <= 1) return 0; FOR(i, 2, (ll)sqrt(n) + 1) { if (n % i == 0) return 0; } return 1; } ll Tot(ll n) { if (n <= 0) return 0; ll ans = n, x = 2; while (x * x <= n) { if (n % x == 0) { ans -= ans / x; while (n % x == 0) n /= x; } x++; } if (n > 1) ans -= ans / n; return ans; } template <class T> struct Graph { public: ll vSize; ll eMode; ll mapMode; GroupSet(T) Add; CompareSet(T) Less; CompareSet(T) Equal; VV<P<T, ll>> adj; map<pl, T> len; Graph(ll vSize, ll eMode = 0, ll mapMode = 0, GroupSet(T) Add = ADD, CompareSet(T) Less = LES, CompareSet(T) Equal = EQ) : vSize(vSize), eMode(eMode), mapMode(mapMode), Add(Add), Less(Less), Equal(Equal) {} void Init() { adj.asn(vSize, V<P<T, ll>>()); } void AddE(ll x, ll y, T cost) { iAddE(x, y, cost); if (!eMode) iAddE(y, x, cost); } void iAddE(ll x, ll y, T cost) { adj[x].pb(mp(cost, y)); if (mapMode) len[mp(x, y)] = cost; } P<bool, T> getE(ll x, ll y) { if (!len.count(mp(x, y))) return mp(false, Id(Less)); return mp(true, len[mp(x, y)]); } V<T> Dijk(ll x) { V<T> ans(vSize, Id(Less)); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans[now.se] = segt[now.se].fi; segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se)); continue; } if (Act(Less)(Act(Add)(ans[now.se], i.fi), segt[i.se].fi)) { segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se)); continue; } } } return ans; } V<P<T, vll>> rDijk(ll x) { V<P<T, vll>> ans(vSize, mp(Id(Less), vll())); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans[now.se].fi = segt[now.se].fi; segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se)); ans[i.se].se = {now.se}; continue; } if (Act(Less)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) { segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se)); ans[i.se].se = {now.se}; continue; } if (Act(Equal)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) { ans[i.se].se.pb(now.se); continue; } } } return ans; } T Prim(ll x = 0) { T ans = Id(Add); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans = Act(Add)(ans, segt[now.se].fi); segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, i); continue; } if (Act(Less)(i.fi, segt[i.se].fi)) { segt.set(i.se, i); continue; } } } return ans; } P<T, V<P<T, vll>>> rPrim(ll x = 0) { P<T, V<P<T, vll>>> ans = mp(Id(Add), V<P<T, vll>>(vSize, mp(Id(Less), vll()))); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans.se[now.se].fi = segt[now.se].fi; ans.fi = Act(Add)(ans.fi, ans.se[now.se].fi); segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, i); ans.se[i.se].se = {now.se}; continue; } if (Act(Less)(i.fi, segt[i.se].fi)) { segt.set(i.se, i); ans.se[i.se].se = {now.se}; continue; } if (Act(Equal)(i.fi, segt[i.se].fi)) { ans.se[i.se].se.pb(now.se); continue; } } } return ans; } }; template <class T> struct Sum { public: V<T> v, s; ll size; GroupSet(T) Add; Sum(V<T> v, GroupSet(T) Add = ADD) : v(v), size(v.sz), Add(Add) { Init(); Calc(); } void Init() { s.asn(size + 1, Id(Add)); } void Calc() { REP(i, size) s[i + 1] = Act(Add)(s[i], v[i]); } T operator()(ll x) { if (x < -1) x = -1; if (x > size - 1) x = size - 1; return s[x + 1]; } T operator()(ll l, ll r) { if (l < 0) l = 0; if (r >= size) r = size - 1; if (l > r) return Id(Add); return InvAct(Add)(s[r + 1], s[l]); } }; using sumll = Sum<ll>; template <class T> struct Sum2 { public: VV<T> v, s; ll RowSize, ColumnSize; GroupSet(T) Add; Sum2(VV<T> v, GroupSet(T) Add = ADD) : v(v), RowSize(v.sz), ColumnSize(v.sz ? v[0].sz : 0), Add(Add) { Init(); Calc(); } void Init() { s.asn(RowSize + 1, V<T>(ColumnSize + 1, Id(Add))); } void Calc() { REP1(r, RowSize) { REP1(c, ColumnSize) { // s[r][c] = InvAct(Add)(Act(Add)(Act(Add)(v[r - //1][c - 1], operator()(r - 1, c - 2)), operator()(r - 2, c - 1)), //operator()(r - 2, c - 2)); s[r][c] = Act(Add)(s[r][c - 1], v[r - 1][c - 1]); } } REP1(r, RowSize) { REP1(c, ColumnSize) { s[r][c] = Act(Add)(s[r - 1][c], s[r][c]); } } } T operator()(ll r, ll c) { if (r < -1) r = -1; if (c < -1) c = -1; if (r > RowSize - 1) r = RowSize - 1; if (c > ColumnSize - 1) c = ColumnSize - 1; return s[r + 1][c + 1]; } T operator()(ll r1, ll c1, ll r2, ll c2) { if (r1 < 0) r1 = 0; if (c1 < 0) c1 = 0; if (r2 >= RowSize) r2 = RowSize - 1; if (c2 >= ColumnSize) c2 = ColumnSize - 1; if (r1 > r2) return Id(Add); if (c1 > c2) return Id(Add); return InvAct(Add)(Act(Add)(s[r2 + 1][c2 + 1], s[r1][c1]), Act(Add)(s[r2 + 1][c1], s[r1][c2 + 1])); } }; using sumll2 = Sum2<ll>; template <class T> struct Point2 { public: VV<T> v; ll h, w; Point2() : h(0), w(0) {} Point2(ll h, ll w) : h(h), w(w) { asn(h, w); } Point2(ll h, ll w, T val) : h(h), w(w) { asn(h, w, val); } Point2(VV<T> cv) : h(cv.sz), w(cv.sz ? cv[0].sz : 0) { asn(h, w); copy(cv); } void assign(ll h, ll w) { v.asn(h, V<T>(w)); } void assign(ll h, ll w, ll val) { v.asn(h, V<T>(w, val)); } void copy(VV<T> cv) { REP(_h, h) REP(_w, w) v[_h][_w] = cv[_h][_w]; } T &operator()(ll h, ll w) { return v[h][w]; } T &operator()(pl p) { return v[p.fi][p.se]; } T &operator[](pl p) { return v[p.fi][p.se]; } }; template <class T> using P2 = Point2<T>; template <ll Mod> struct Modll { public: ll v; Modll() : v(0) {} Modll(ll _v) { set(_v % Mod + Mod); } Modll &set(ll _v) { v = (_v < Mod) ? _v : (_v - Mod); return *this; } Modll pow(ll n) const { Modll x = *this, ans = 1; while (n) { if (n & 1) ans *= x; x *= x; n >>= 1; } return ans; } Modll inverse() const { return (*this).pow(Mod - 2); } Modll operator+(const Modll &m) const { return Modll().set(v + m.v); } Modll operator-(const Modll &m) const { return Modll().set(Mod + v - m.v); } Modll operator*(const Modll &m) const { return Modll().set((ull(v) * m.v) % Mod); } Modll operator/(const Modll &m) const { return *this * m.inv; } Modll &operator+=(const Modll &m) { return *this = *this + m; } Modll &operator-=(const Modll &m) { return *this = *this - m; } Modll &operator*=(const Modll &m) { return *this = *this * m; } Modll &operator/=(const Modll &m) { return *this = *this / m; } Modll operator-() const { return Modll(0) - *this; } explicit operator bool() const { return v != 0; } friend istream &operator>>(istream &in, Modll &m) { return in >> m.v; } friend ostream &operator<<(ostream &out, const Modll &m) { return out << m.v; } }; using mll = Modll<MOD>; using vmll = V<mll>; using vvmll = V<vmll>; using vvvmll = V<vvmll>; vmll MFactMemo(2, 1); vmll MIFactMemo(2, 1); mll mFact(ll n) { if (MFactMemo.sz <= n) { ll oldsize = MFactMemo.sz; MFactMemo.res(n + 1, 1); FOR(i, oldsize, n) MFactMemo[i] = MFactMemo[i - 1] * i; } return MFactMemo[n]; } mll miFact(ll n) { if (MIFactMemo.sz <= n) { ll oldsize = MIFactMemo.sz; MIFactMemo.res(n + 1, 1); MIFactMemo.bk = mFact(n).inv; rFOR(i, oldsize + 1, n) MIFactMemo[i - 1] = MIFactMemo[i] * i; } return MIFactMemo[n]; } mll mComb(ll n, ll k) { if (n < 0 || k < 0 || n < k) return 0; return mFact(n) * miFact(k) * miFact(n - k); } ll LIS(vll v, ll m = 0) { if (v.sz > 0) { ll ans = 0; vll dp(v.sz, INF); FOR(i, 0, v.sz - 1) { dp[m ? UPB(dp, v[i]) : LOWB(dp, v[i])] = v[i]; } FOR(i, 0, v.sz - 1) { if (dp[i] == INF) break; ans++; } return ans; } else { return 0; } } ll Bsrch(function<bool(ll x)> f, ll mi, ll ma) { ll ng = mi - 1, ok = ma, mid; while (ok - ng > 1) { mid = (ng + ok) / 2; (f(mid) ? ok : ng) = mid; } return ok; } template <class T, class M = decltype(MUL), class S = decltype(ADD)> VV<T> MultiMatrix(VV<T> A, VV<T> B, M Mul = MUL, S Add = ADD) { VV<T> ans; ll ii = A.sz; if (!ii) return ans; ll jj = A[0].sz; if (!jj) return ans; ll jj2 = B.sz; if (!jj2) return ans; if (jj != jj2) return ans; ll kk = B[0].sz; if (!kk) return ans; ans.asn(ii, V<T>(kk, 0)); REP(i, ii) { REP(k, kk) { REP(j, jj) { ans[i][k] = Act(Add)(ans[i][k], Act(Mul)(A[i][j], B[j][k])); } } } return ans; } vvll CombMemo(1000, vll(1000, -1)); ll Comb(ll n, ll k) { if ((n < 0) || (k < 0)) return 0; if (CombMemo[n][k] == -1) { if (n < k) CombMemo[n][k] = 0; else { if (n == 0) CombMemo[n][k] = 1; else if (k == 0) CombMemo[n][k] = 1; else if (n == k) CombMemo[n][k] = 1; else CombMemo[n][k] = Comb(n - 1, k - 1) + Comb(n - 1, k); } } return CombMemo[n][k]; } template <class T> map<T, ll> Dict(V<T> v) { map<T, ll> m; if (!v.sz) return m; SORT(v); UNIQUE(v); REP(i, v.sz) { m[v[i]] = i; } return m; } template <class T> vll Cmprs(V<T> v) { auto m = Dict(v); vll ans(v.sz); REP(i, v.sz) { ans[i] = m[v[i]]; } return ans; } template <class T> vll PCmprs(V<T> v) { if (v.sz == 0) return V<T>(); vll tmp(v.sz); vll ans(v.sz); IOTA(tmp, 0); IOTA(ans, 0); sort(tmp.bgn, tmp.en, clam(v[l] < v[r])); sort(ans.bgn, ans.en, clam(tmp[l] < tmp[r])); return ans; } ll BblCnt(vll rv) { vll v = PCmprs(rv); SegT<ll> b(v.sz, 0); ll ans = 0; REP(i, v.sz) { ans += (i - b.que(0, v[i])); b.add(v[i], 1); } return ans; } pl NGrid(pl p, ll i, ll H, ll W) { p = mp(p.fi + DX[i], p.se + DY[i]); if (p.fi < 0 || p.fi >= H || p.se < 0 || p.se >= W) return mp(INF, INF); return p; } vvll llGrid(vs v) { vvll r(v.sz, vll(v.sz ? v[0].sz : 0, 0)); REP(h, v.sz) REP(w, v.sz ? v[0].sz : 0) r[h][w] = (v[h][w] == '#'); return r; } template <class T> auto ven(T val) { return val; } template <> auto ven<int>(int val) { return (ll)val; } template <class T, class... Args> auto ven(T val, Args... args) { auto tmp = ven(args...); return V<decltype(tmp)>(val, tmp); } template <class T> void zind(T &v) { v--; } template <class T> void zind(V<T> &v) { ROR(v, i) zind(i); } template <class T, class... Args> void zind(T &v, Args &...args) { zind(v); zind(args...); } void Solve(); signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20) << fixed; Solve(); } void Solve() { li(N, K); vli(N, a); vmll dp(K + 1, 0); vmll dps(K + 2, 0); dp[0] = 1; REP(i, N) { REP1(j, K + 1) dps[j] = dps[j - 1] + dp[j - 1]; rREP1(j, K) dp[j] = dps[j + 1] - dps[max(0, j - a[i])]; } sal(dp[K]); }
replace
1,395
1,400
1,395
1,404
TLE
p03172
C++
Runtime Error
// Om Sree Sai Ram #include "bits/stdc++.h" using namespace std; const int mod = 1e9 + 7; int add_self(int &a, int b) { a += b; if (a >= mod) { a -= mod; } } int sub_self(int &a, int b) { a -= b; if (a <= 0) { a += mod; } } int main() { int n, k; cin >> n >> k; vector<int> dp(k + 1); // dp[i] represents number of ways to get into state in which i candies are // distributed. dp[0] = 1; for (int i = 0; i < n; i++) { int upto; cin >> upto; vector<int> diff(k + 1); for (int j = k; j >= 0; j--) { int tmp = dp[j]; // for order(n*k^2) algorithm int L = j + 1; int R = j + upto; if (L <= R) { add_self(diff[L], tmp); if (R + 1 <= k) { sub_self(diff[R + 1], tmp); } } // for(int dat=L;dat<=R;dat++) // { // if(dat<=k) // { // dp[dat] += tmp; // } // } } int prefix = 0; for (int i = 0; i <= k; i++) { // cout<<"diff "<<i<<endl; add_self(prefix, diff[i]); // cout<<"prefix "<<prefix<<endl; add_self(dp[i], prefix); } } cout << dp[k] << endl; }
// Om Sree Sai Ram #include "bits/stdc++.h" using namespace std; const int mod = 1e9 + 7; int add_self(int &a, int b) { a += b; if (a >= mod) { a -= mod; } } int sub_self(int &a, int b) { a -= b; if (a <= 0) { a += mod; } } int main() { int n, k; cin >> n >> k; vector<int> dp(k + 1); // dp[i] represents number of ways to get into state in which i candies are // distributed. dp[0] = 1; for (int i = 0; i < n; i++) { int upto; cin >> upto; vector<int> diff(k + 1); for (int j = k; j >= 0; j--) { int tmp = dp[j]; // for order(n*k^2) algorithm int L = j + 1; int R = j + min(upto, k - j); if (L <= R) { add_self(diff[L], tmp); if (R + 1 <= k) { sub_self(diff[R + 1], tmp); } } // for(int dat=L;dat<=R;dat++) // { // if(dat<=k) // { // dp[dat] += tmp; // } // } } int prefix = 0; for (int i = 0; i <= k; i++) { // cout<<"diff "<<i<<endl; add_self(prefix, diff[i]); // cout<<"prefix "<<prefix<<endl; add_self(dp[i], prefix); } } cout << dp[k] << endl; }
replace
37
38
37
38
0
p03172
C++
Runtime Error
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define FIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define ff first #define ss second #define vll vector<ll> #define pll pair<ll, ll> #define vpll vector<pll> #define vvll vector<vll> #define all(v) v.begin(), v.end() #define itr(r, v) for (auto r = v.begin(); r != v.end(); r++) #define FOR(i, n) for (ll i = 0; i < n; i++) #define ffo(i, a, b) for (ll i = a; i <= b; i++) #define rfr(i, a, b) for (ll i = a; i >= b; i--) #define space cout << "\n\n"; #define endl "\n" #define pqmx priority_queue<ll> #define pqmn priority_queue<ll, vll, greater<ll>> #define fps(x, y) fixed << setprecision(y) << x #define merg(a, b, c) \ set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin())) #define set_ar(arr, v) memset(arr, v, sizeof(arr)) #define ctime auto sttart = high_resolution_clock::now() #define etime auto sttop = high_resolution_clock::now() #define go_t \ ll testcases; \ cin >> testcases; \ for (ll caseno = 1; caseno <= testcases; caseno++) #define ptime \ auto z1z = duration_cast<microseconds>(sttop - sttart); \ cout << "\nTime taken : " << z1z.count() << " microseconds\n" #define dbg1(x) cout << #x << ": " << x << '\n'; #define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << '\n'; #define dbg3(x, y, z) \ cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << '\n'; #define dbg4(a, b, c, d) \ cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << '\n'; #define dbg5(a, b, c, d, e) \ cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << '\n'; using namespace std; using namespace std::chrono; const ll MOD = 1e9 + 7; const ll N = 1e5 + 5; const ll maxN = 3e3 + 5; const ll MAX_SIZE = 2e6 + 6; const ll INF = 0x3f3f3f3f3f3f3f3fll; const double PI = 3.14159265359; ll powerM(ll x, ll y, ll M = MOD) { // default argument ll v = 1; x = x % M; while (y > 0) { if (y & 1) v = (v * x) % M; y = y >> 1; x = (x * x) % M; } return v; } ll power(ll x, ll y) { ll v = 1; while (y > 0) { if (y & 1) v = v * x; y = y >> 1; x = x * x; } return v; } ll n, k; ll a[105]; ll dp[105][maxN]; // here dp[i][j] will store the prefix sum of the diff no. of ways to distribute // j candies among i children with the given upper bound constraints ll solve(ll child, ll candy) { if (candy < 0) return 0; if (candy == 0) return 1; if (child == n + 1) return 1; // ll &dd = dp[child][candy]; if (dd != -1) return dd; // max amt of candies that can be given to the child ll mini = min(candy, a[child]); dd = solve(child + 1, candy) - solve(child + 1, candy - mini - 1) + MOD; dd %= MOD; dd += solve(child, candy - 1); dd %= MOD; return dd; } int main() { #ifndef ONLINE_JUDGE freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout); #endif FIO // Ashishgup you're great bro cin >> n >> k; memset(dp, -1, sizeof(dp)); for (ll i = 1; i <= n; i++) cin >> a[i]; ll ans = solve(1, k) - solve(1, k - 1) + MOD; ans %= MOD; cout << ans; return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define FIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define ff first #define ss second #define vll vector<ll> #define pll pair<ll, ll> #define vpll vector<pll> #define vvll vector<vll> #define all(v) v.begin(), v.end() #define itr(r, v) for (auto r = v.begin(); r != v.end(); r++) #define FOR(i, n) for (ll i = 0; i < n; i++) #define ffo(i, a, b) for (ll i = a; i <= b; i++) #define rfr(i, a, b) for (ll i = a; i >= b; i--) #define space cout << "\n\n"; #define endl "\n" #define pqmx priority_queue<ll> #define pqmn priority_queue<ll, vll, greater<ll>> #define fps(x, y) fixed << setprecision(y) << x #define merg(a, b, c) \ set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin())) #define set_ar(arr, v) memset(arr, v, sizeof(arr)) #define ctime auto sttart = high_resolution_clock::now() #define etime auto sttop = high_resolution_clock::now() #define go_t \ ll testcases; \ cin >> testcases; \ for (ll caseno = 1; caseno <= testcases; caseno++) #define ptime \ auto z1z = duration_cast<microseconds>(sttop - sttart); \ cout << "\nTime taken : " << z1z.count() << " microseconds\n" #define dbg1(x) cout << #x << ": " << x << '\n'; #define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << '\n'; #define dbg3(x, y, z) \ cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << '\n'; #define dbg4(a, b, c, d) \ cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << '\n'; #define dbg5(a, b, c, d, e) \ cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << '\n'; using namespace std; using namespace std::chrono; const ll MOD = 1e9 + 7; const ll N = 1e5 + 5; const ll maxN = 1e5 + 5; const ll MAX_SIZE = 2e6 + 6; const ll INF = 0x3f3f3f3f3f3f3f3fll; const double PI = 3.14159265359; ll powerM(ll x, ll y, ll M = MOD) { // default argument ll v = 1; x = x % M; while (y > 0) { if (y & 1) v = (v * x) % M; y = y >> 1; x = (x * x) % M; } return v; } ll power(ll x, ll y) { ll v = 1; while (y > 0) { if (y & 1) v = v * x; y = y >> 1; x = x * x; } return v; } ll n, k; ll a[105]; ll dp[105][maxN]; // here dp[i][j] will store the prefix sum of the diff no. of ways to distribute // j candies among i children with the given upper bound constraints ll solve(ll child, ll candy) { if (candy < 0) return 0; if (candy == 0) return 1; if (child == n + 1) return 1; // ll &dd = dp[child][candy]; if (dd != -1) return dd; // max amt of candies that can be given to the child ll mini = min(candy, a[child]); dd = solve(child + 1, candy) - solve(child + 1, candy - mini - 1) + MOD; dd %= MOD; dd += solve(child, candy - 1); dd %= MOD; return dd; } int main() { #ifndef ONLINE_JUDGE freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout); #endif FIO // Ashishgup you're great bro cin >> n >> k; memset(dp, -1, sizeof(dp)); for (ll i = 1; i <= n; i++) cin >> a[i]; ll ans = solve(1, k) - solve(1, k - 1) + MOD; ans %= MOD; cout << ans; return 0; }
replace
56
57
56
57
0
p03172
C++
Runtime Error
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> int main() { using namespace std; constexpr unsigned int MOD = 1000000007; unsigned long N, K; cin >> N >> K; vector<unsigned int> dp(K + 1); dp[0] = 1; for (unsigned long i{0}, a; i < N; ++i) { cin >> a; for (unsigned long j{K}; j > a; --j) dp[j] += dp[j] > dp[j - a - 1] ? -dp[j - a - 1] : MOD - dp[j - a - 1]; } for (unsigned long i{0}; i < N; ++i) partial_sum(dp.begin(), dp.end(), dp.begin(), [](auto i, auto j) { return (i + j) % MOD; }); cout << dp.back() << endl; return 0; }
#pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> int main() { using namespace std; constexpr unsigned int MOD = 1000000007; unsigned long N, K; cin >> N >> K; vector<unsigned int> dp(K + 1); dp[0] = 1; for (unsigned long i{0}, a; i < N; ++i) { cin >> a; for (unsigned long j{K}; j > a; --j) dp[j] += dp[j] > dp[j - a - 1] ? -dp[j - a - 1] : MOD - dp[j - a - 1]; } for (unsigned long i{0}; i < N; ++i) partial_sum(dp.begin(), dp.end(), dp.begin(), [](auto i, auto j) { return (i + j) % MOD; }); cout << dp.back() << endl; return 0; }
replace
0
1
0
1
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const ll mod = 1e9L + 7; int n; vector<int> inp; vector<vector<ll>> dp; ll candp(int k, int i = 0) { // cout<<i<<" "<<j<<" "<<c<<" "<<sum<<'\n'; if (k == 0) return 1; if (i == n) return 0; if (dp[i][k] != -1) return dp[i][k]; ll ans = 0; for (int a = 0; a <= inp[i]; a++) { if (k - a >= 0) { ans += candp(k - a, i + 1); ans %= mod; } else break; } return dp[i][k] = ans; } ll cf_solver(int k) { vector<vector<ll>> cf_dp(k + 1, vector<ll>(n + 1, 1)); for (int a = 1; a <= k; a++) { for (int i = 1; i <= n; i++) { ll ans = 0; int sub = min(a, inp[i]); sub++; if (a - sub == -1) ans = cf_dp[a][i - 1]; else ans = cf_dp[a][i - 1] - cf_dp[a - sub][i - 1]; if (ans < 0) ans += mod; ans += cf_dp[a - 1][i]; ans %= mod; cf_dp[a][i] = ans; } } ll ret_val = cf_dp[k][n] - cf_dp[k - 1][n]; if (ret_val < 0) ret_val += mod; ret_val %= mod; return ret_val; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int k; cin >> n >> k; inp.resize(n + 1); dp.assign(n + 1, vector<ll>(k + 1, -1)); for (int i = 1; i <= n; i++) { cin >> inp[i]; } // cout<<candp(k)<<'\n'; cout << cf_solver(k) << '\n'; }
#include <bits/stdc++.h> #define ll long long using namespace std; const ll mod = 1e9L + 7; int n; vector<int> inp; vector<vector<ll>> dp; ll candp(int k, int i = 0) { // cout<<i<<" "<<j<<" "<<c<<" "<<sum<<'\n'; if (k == 0) return 1; if (i == n) return 0; if (dp[i][k] != -1) return dp[i][k]; ll ans = 0; for (int a = 0; a <= inp[i]; a++) { if (k - a >= 0) { ans += candp(k - a, i + 1); ans %= mod; } else break; } return dp[i][k] = ans; } ll cf_solver(int k) { vector<vector<ll>> cf_dp(k + 1, vector<ll>(n + 1, 1)); for (int a = 1; a <= k; a++) { for (int i = 1; i <= n; i++) { ll ans = 0; int sub = min(a, inp[i]); sub++; if (a - sub == -1) ans = cf_dp[a][i - 1]; else ans = cf_dp[a][i - 1] - cf_dp[a - sub][i - 1]; if (ans < 0) ans += mod; ans += cf_dp[a - 1][i]; ans %= mod; cf_dp[a][i] = ans; } } ll ret_val = cf_dp[k][n]; if (k > 0) ret_val -= cf_dp[k - 1][n]; if (ret_val < 0) ret_val += mod; ret_val %= mod; return ret_val; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int k; cin >> n >> k; inp.resize(n + 1); dp.assign(n + 1, vector<ll>(k + 1, -1)); for (int i = 1; i <= n; i++) { cin >> inp[i]; } // cout<<candp(k)<<'\n'; cout << cf_solver(k) << '\n'; }
replace
49
50
49
52
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define db long double #define ull unsigned long long int #define mp make_pair #define F first #define S second #define pb push_back #define rep(i, a, b) for (ll i = a; i <= b; i++) #define all(a) a.begin(), a.end() #define Nmax 1000005 #define INF 1000000000 #define MOD 1000000007 #define MAXN 1000005 using namespace std; ll dp[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tests = 1; // cin>>tests; while (tests--) { ll x, i, j, n, k; cin >> n >> k; // vector<ll>pre; dp[0] = 1; rep(i, 1, n) { cin >> x; // vector<ll>pre(k+1,0); // pre.resize(k+1,0LL); ll pre[k + 1LL]; memset(pre, 0, sizeof(pre)); for (j = 0; j <= k; j++) { ll left = j + 1; ll right = j + x; if (right >= left) { pre[left] += dp[j]; pre[left] %= MOD; if (right + 1 <= k) { pre[right + 1] -= dp[j]; pre[right + 1] = (pre[right + 1] + MOD) % MOD; } } } rep(j, 1, k) { pre[j] += pre[j - 1]; pre[j] = (pre[j] + MOD) % MOD; dp[j] += pre[j]; dp[j] = (dp[j] + MOD) % MOD; } } cout << dp[k]; } return 0; }
#include <bits/stdc++.h> #define ll long long int #define db long double #define ull unsigned long long int #define mp make_pair #define F first #define S second #define pb push_back #define rep(i, a, b) for (ll i = a; i <= b; i++) #define all(a) a.begin(), a.end() #define Nmax 1000005 #define INF 1000000000 #define MOD 1000000007 #define MAXN 1000005 using namespace std; ll dp[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tests = 1; // cin>>tests; while (tests--) { ll x, i, j, n, k; cin >> n >> k; // vector<ll>pre; dp[0] = 1; rep(i, 1, n) { cin >> x; // vector<ll>pre(k+1,0); // pre.resize(k+1,0LL); ll pre[k + 50LL]; memset(pre, 0, sizeof(pre)); for (j = 0; j <= k; j++) { ll left = j + 1; ll right = j + x; if (right >= left) { pre[left] += dp[j]; pre[left] %= MOD; if (right + 1 <= k) { pre[right + 1] -= dp[j]; pre[right + 1] = (pre[right + 1] + MOD) % MOD; } } } rep(j, 1, k) { pre[j] += pre[j - 1]; pre[j] = (pre[j] + MOD) % MOD; dp[j] += pre[j]; dp[j] = (dp[j] + MOD) % MOD; } } cout << dp[k]; } return 0; }
replace
33
34
33
34
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define PI 3.14159265358979 #define EPS 1e-8 #define mod 1000000007 #define fi first #define se second #define mp make_pair #define pb push_back const ll INF = 1e18; const int N = 1e5 + 50; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); ll n, k; cin >> n >> k; vector<ll> v(n); for (ll i = 0; i < n; i++) { cin >> v[i]; } vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, 0)); for (int i = 0; i <= k; i++) { dp[i][0] = 1; } for (int j = 1; j <= n; j++) { for (int i = 0; i <= k; i++) { dp[i][j] = dp[i][j - 1]; if ((i - v[j - 1]) > 0) { dp[i][j] -= dp[i - v[j - 1] - 1][j - 1]; } dp[i][j] %= mod; dp[i][j] += mod; dp[i][j] %= mod; if (i > 0) dp[i][j] += dp[i - 1][j]; dp[i][j] %= mod; } } cout << (dp[k][n] - dp[k - 1][n]); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define PI 3.14159265358979 #define EPS 1e-8 #define mod 1000000007 #define fi first #define se second #define mp make_pair #define pb push_back const ll INF = 1e18; const int N = 1e5 + 50; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); ll n, k; cin >> n >> k; vector<ll> v(n); for (ll i = 0; i < n; i++) { cin >> v[i]; } vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, 0)); for (int i = 0; i <= k; i++) { dp[i][0] = 1; } for (int j = 1; j <= n; j++) { for (int i = 0; i <= k; i++) { dp[i][j] = dp[i][j - 1]; if ((i - v[j - 1]) > 0) { dp[i][j] -= dp[i - v[j - 1] - 1][j - 1]; } dp[i][j] %= mod; dp[i][j] += mod; dp[i][j] %= mod; if (i > 0) dp[i][j] += dp[i - 1][j]; dp[i][j] %= mod; } } ll ans = dp[k][n]; if (k) ans -= dp[k - 1][n]; ans %= mod; ans += mod; ans %= mod; cout << ans; }
replace
46
47
46
53
0
p03172
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define int long long using namespace std; const int mod = 1e9 + 7; const int maxn = 1e6; int n; vector<vector<int>> dp(n); int query(int pos1, int l, int r) { l += maxn; r += maxn; int res = 0; while (l <= r) { if (l % 2 == 1) res += dp[pos1][l++]; if (r % 2 == 0) res += dp[pos1][r--]; l /= 2; r /= 2; } return res % mod; } void change(int pos1, int pos2, int val) { pos2 += maxn; dp[pos1][pos2] += val; for (pos2 /= 2; pos2 >= 1; pos2 /= 2) { dp[pos1][pos2] = dp[pos1][pos2 * 2] + dp[pos1][pos2 * 2 + 1]; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; dp = vector<vector<int>>(n, vector<int>(2 * maxn)); for (int i = 0; i <= a[0]; i++) { change(0, i, 1); change(0, i + 1, -1); } for (int i = 1; i < n; i++) { for (int j = 0; j <= k; j++) { int get = query(i - 1, 0, j); if (get > 0) { int left = j, right = min(k, j + a[i]); change(i, left, get); change(i, right + 1, -get); } } } int ans = query(n - 1, 0, k); cout << ans % mod; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; const int mod = 1e9 + 7; const int maxn = 1e5 + 1e4; int n; vector<vector<int>> dp(n); int query(int pos1, int l, int r) { l += maxn; r += maxn; int res = 0; while (l <= r) { if (l % 2 == 1) res += dp[pos1][l++]; if (r % 2 == 0) res += dp[pos1][r--]; l /= 2; r /= 2; } return res % mod; } void change(int pos1, int pos2, int val) { pos2 += maxn; dp[pos1][pos2] += val; for (pos2 /= 2; pos2 >= 1; pos2 /= 2) { dp[pos1][pos2] = dp[pos1][pos2 * 2] + dp[pos1][pos2 * 2 + 1]; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; dp = vector<vector<int>>(n, vector<int>(2 * maxn)); for (int i = 0; i <= a[0]; i++) { change(0, i, 1); change(0, i + 1, -1); } for (int i = 1; i < n; i++) { for (int j = 0; j <= k; j++) { int get = query(i - 1, 0, j); if (get > 0) { int left = j, right = min(k, j + a[i]); change(i, left, get); change(i, right + 1, -get); } } } int ans = query(n - 1, 0, k); cout << ans % mod; return 0; }
replace
4
5
4
5
MLE
p03172
C++
Runtime Error
#include <iostream> #include <stdio.h> #define Re register int #define mod int(1e9 + 7) int N, K, A[105]; long long s[105], f[105][100005]; template <typename T> inline void read(T &var) { T x = 0; int w = 0; char ch = 0; while (!isdigit(ch)) w |= ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); var = w ? -x : x; } int main(int argc, char const *argv[]) { read(N), read(K); f[0][0] = 1; for (Re i = 1; i <= N; ++i) read(A[i]); for (Re i = 1; i <= N; ++i) { s[0] = 0; for (Re j = 0; j <= K; ++j) s[j + 1] = s[j] + f[i - 1][j]; for (Re j = 0; j <= K; ++j) (f[i][j] = s[j + 1] - s[std::max(0, j - A[i])]) %= mod; } printf("%lld\n", f[N][K]); return 0; }
#include <iostream> #include <stdio.h> #define Re register int #define mod int(1e9 + 7) int N, K, A[105]; long long s[100005], f[105][100005]; template <typename T> inline void read(T &var) { T x = 0; int w = 0; char ch = 0; while (!isdigit(ch)) w |= ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); var = w ? -x : x; } int main(int argc, char const *argv[]) { read(N), read(K); f[0][0] = 1; for (Re i = 1; i <= N; ++i) read(A[i]); for (Re i = 1; i <= N; ++i) { s[0] = 0; for (Re j = 0; j <= K; ++j) s[j + 1] = s[j] + f[i - 1][j]; for (Re j = 0; j <= K; ++j) (f[i][j] = s[j + 1] - s[std::max(0, j - A[i])]) %= mod; } printf("%lld\n", f[N][K]); return 0; }
replace
6
7
6
7
0
p03172
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using lint = long long int; template <int64_t MOD> struct Mint { int64_t v; Mint() { v = 0; } Mint(int64_t x) { v = x % MOD; if (v < 0) v += MOD; } Mint operator-() { return Mint{-v}; }; Mint operator+(Mint x) { return Mint(v + x.v); }; Mint operator-(Mint x) { return Mint(v - x.v); }; Mint operator*(Mint x) { return Mint(v * x.v); }; Mint operator/(Mint x) { return Mint(v * pow(x.v, MOD - 2).v); }; Mint &operator+=(Mint x) { return *this = (*this + x); }; Mint &operator-=(Mint x) { return *this = (*this - x); }; Mint &operator*=(Mint x) { return *this = (*this * x); }; Mint &operator/=(Mint x) { return *this = (*this / x); }; static Mint pow(Mint base, int64_t exp) { if (exp == 0) return Mint(1); return pow(base * base, exp / 2) * (exp & 1 ? base : Mint(1)); } friend ostream &operator<<(ostream &os, Mint x) { return os << x.v; } }; using mint = Mint<1000000007>; template <class T> struct Polynomial { vector<T> p; Polynomial() {} Polynomial(int n) { p.assign(n, T{}); } Polynomial(int n, T val) { p.assign(n, val); } Polynomial(vector<T> v) : p(v) {} inline int size() { return p.size(); } T &operator[](int i) { return p[i]; } auto begin() { return p.begin(); } auto end() { return p.end(); } Polynomial operator+=(Polynomial x) { int n = max(size(), x.size()); p.resize(n); x.p.resize(n); for (int i = 0; i < n; i++) p[i] += x[i]; return *this; } Polynomial operator-=(Polynomial x) { int n = max(size(), x.size()); p.resize(n); x.p.resize(n); for (int i = 0; i < n; i++) p[i] -= x[i]; return *this; } // !!! O(n*m) !!! Polynomial operator*=(Polynomial x) { int n = size(); int m = x.size(); auto res = Polynomial(n + m - 1); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { res.p[i + j] += p[i] * x[j]; } } return res; } Polynomial operator>>(int c) { int n = size(); auto res = Polynomial(n + c); for (int i = 0; i < n; i++) res.p[i + c] = p[i]; return res; } Polynomial operator<<(int c) { int n = size(); assert(n - c > 0); auto res = Polynomial(n - c); for (int i = c; i < n; i++) res.p[i - c] = p[i]; return res; } Polynomial operator+(Polynomial x) { return Polynomial(p) += x; } Polynomial operator-(Polynomial x) { return Polynomial(p) -= x; } Polynomial operator*(Polynomial x) { return Polynomial(p) *= x; } friend istream &operator>>(istream &is, Polynomial &x) { for (auto &e : x.p) is >> e; return is; } friend ostream &operator<<(ostream &os, Polynomial x) { for (auto &e : x.p) os << e << " \n"[&e == &x.p.back()]; return os; } }; int main() { lint n, k; cin >> n >> k; Polynomial<mint> p(k + 1); p[0] = 1; while (n--) { int a; cin >> a; p = p - (p >> (a + 1)); partial_sum(p.begin(), p.end(), p.begin()); } cout << p[k] << endl; }
#include <bits/stdc++.h> using namespace std; using lint = long long int; template <int64_t MOD> struct Mint { int64_t v; Mint() { v = 0; } Mint(int64_t x) { v = x % MOD; if (v < 0) v += MOD; } Mint operator-() { return Mint{-v}; }; Mint operator+(Mint x) { return Mint(v + x.v); }; Mint operator-(Mint x) { return Mint(v - x.v); }; Mint operator*(Mint x) { return Mint(v * x.v); }; Mint operator/(Mint x) { return Mint(v * pow(x.v, MOD - 2).v); }; Mint &operator+=(Mint x) { return *this = (*this + x); }; Mint &operator-=(Mint x) { return *this = (*this - x); }; Mint &operator*=(Mint x) { return *this = (*this * x); }; Mint &operator/=(Mint x) { return *this = (*this / x); }; static Mint pow(Mint base, int64_t exp) { if (exp == 0) return Mint(1); return pow(base * base, exp / 2) * (exp & 1 ? base : Mint(1)); } friend ostream &operator<<(ostream &os, Mint x) { return os << x.v; } }; using mint = Mint<1000000007>; template <class T> struct Polynomial { vector<T> p; Polynomial() {} Polynomial(int n) { p.assign(n, T{}); } Polynomial(int n, T val) { p.assign(n, val); } Polynomial(vector<T> v) : p(v) {} inline int size() { return p.size(); } T &operator[](int i) { return p[i]; } auto begin() { return p.begin(); } auto end() { return p.end(); } Polynomial operator+=(Polynomial x) { int n = max(size(), x.size()); p.resize(n); x.p.resize(n); for (int i = 0; i < n; i++) p[i] += x[i]; return *this; } Polynomial operator-=(Polynomial x) { int n = max(size(), x.size()); p.resize(n); x.p.resize(n); for (int i = 0; i < n; i++) p[i] -= x[i]; return *this; } // !!! O(n*m) !!! Polynomial operator*=(Polynomial x) { int n = size(); int m = x.size(); auto res = Polynomial(n + m - 1); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { res.p[i + j] += p[i] * x[j]; } } return res; } Polynomial operator>>(int c) { int n = size(); auto res = Polynomial(n + c); for (int i = 0; i < n; i++) res.p[i + c] = p[i]; return res; } Polynomial operator<<(int c) { int n = size(); assert(n - c > 0); auto res = Polynomial(n - c); for (int i = c; i < n; i++) res.p[i - c] = p[i]; return res; } Polynomial operator+(Polynomial x) { return Polynomial(p) += x; } Polynomial operator-(Polynomial x) { return Polynomial(p) -= x; } Polynomial operator*(Polynomial x) { return Polynomial(p) *= x; } friend istream &operator>>(istream &is, Polynomial &x) { for (auto &e : x.p) is >> e; return is; } friend ostream &operator<<(ostream &os, Polynomial x) { for (auto &e : x.p) os << e << " \n"[&e == &x.p.back()]; return os; } }; int main() { lint n, k; cin >> n >> k; Polynomial<mint> p(k + 1); p[0] = 1; while (n--) { int a; cin >> a; p = p - (p >> (a + 1)); partial_sum(p.begin(), p.end(), p.begin()); p.p.resize(k + 1); } cout << p[k] << endl; }
insert
124
124
124
125
TLE
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n, i, j, k; cin >> n >> k; ll a[n + 3]; for (i = 1; i <= n; i++) cin >> a[i]; a[0] = 0; sort(a, a + n + 1); vector<vector<ll>> dp(k + 2, vector<ll>(n + 2, 1)); ll ans = 1; ll mod = 1000000007; for (i = 1; i <= k; i++) { for (j = 1; j <= n; j++) { ll x = min(i, a[j]); x++; if (x > i) ans = dp[i][j - 1] % mod; else { ans = dp[i][j - 1] - dp[i - x][j - 1] + mod; ans %= mod; } dp[i][j] = dp[i - 1][j] % mod + ans; dp[i][j] %= mod; } } cout << (dp[k][n] - dp[k - 1][n] + mod) % mod; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n, i, j, k; cin >> n >> k; ll a[n + 3]; for (i = 1; i <= n; i++) cin >> a[i]; a[0] = 0; sort(a, a + n + 1); vector<vector<ll>> dp(k + 2, vector<ll>(n + 2, 1)); ll ans = 1; ll mod = 1000000007; for (i = 1; i <= k; i++) { for (j = 1; j <= n; j++) { ll x = min(i, a[j]); x++; if (x > i) ans = dp[i][j - 1] % mod; else { ans = dp[i][j - 1] - dp[i - x][j - 1] + mod; ans %= mod; } dp[i][j] = dp[i - 1][j] % mod + ans; dp[i][j] %= mod; } } cout << ans; return 0; }
replace
29
30
29
30
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() typedef long long ll; typedef pair<ll, ll> P; typedef vector<ll> vec; typedef vector<vec> mat; ll dp[101][100001]; ll mod = 1e9 + 7; int main() { int n, k, A[200001]; cin >> n >> k; rep(i, n) cin >> A[i]; fill(dp[0], dp[0] + k + 1, 0); dp[0][0] = 1; rep(i, n) { rep(j, k)(dp[i][j + 1] += dp[i][j]) %= mod; rep(j, k + 1) dp[i + 1][j] = (dp[i][j] - dp[i][j - A[i] - 1] * (j - A[i] - 1 >= 0) + mod) % mod; } cout << dp[n][k] << "\n"; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() typedef long long ll; typedef pair<ll, ll> P; typedef vector<ll> vec; typedef vector<vec> mat; ll dp[101][100001]; ll mod = 1e9 + 7; int main() { int n, k, A[200001]; cin >> n >> k; rep(i, n) cin >> A[i]; fill(dp[0], dp[0] + k + 1, 0); dp[0][0] = 1; rep(i, n) { rep(j, k)(dp[i][j + 1] += dp[i][j]) %= mod; rep(j, k + 1) { dp[i + 1][j] = dp[i][j]; if (j - A[i] - 1 >= 0) (dp[i + 1][j] += mod - dp[i][j - A[i] - 1]) %= mod; } } cout << dp[n][k] << "\n"; }
replace
18
20
18
23
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ld long double #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define debug(x) cerr << #x << " " << x << endl #define cina(a, n) \ for (ll i = 0; i < n; i++) \ cin >> a[i]; #define couta(a, n) \ for (ll i = 0; i < n; i++) \ cout << a[i] << " "; #define fauto(ar) \ for (auto x : ar) \ cout << x << " "; #define mod 1000000007 #define inf 10010010010010010 #define siz 100005 #define f first #define s second #define pb push_back #define endl "\n" #define ci cin.ignore() using namespace std; int main() { ll n, k, i, j, p, flag; cin >> n >> k; ll a[n + 1]; for (i = 1; i <= n; i++) cin >> a[i]; ll dp[2][k + 1]; for (i = 0; i <= 1; i++) for (j = 0; j <= k; j++) dp[i][j] = 0; // for(i = 0;i<=k;i++) // dp[0][i] = 1; ll sum[k + 1]; flag = 1; for (i = 1; i <= n; i++) { sum[0] = 1; for (j = 1; j <= k; j++) sum[j] = sum[j - 1] + dp[flag ^ 1][j], sum[j] %= mod; dp[i][0] = 1; for (j = 1; j <= k; j++) { ll left = 0; if (j - a[i] - 1 >= 0) left = sum[j - a[i] - 1]; ll right = sum[j]; dp[flag][j] = right - left; dp[flag][j] %= mod; dp[flag][j] += mod; dp[flag][j] %= mod; } flag ^= 1; } cout << dp[flag ^ 1][k]; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define debug(x) cerr << #x << " " << x << endl #define cina(a, n) \ for (ll i = 0; i < n; i++) \ cin >> a[i]; #define couta(a, n) \ for (ll i = 0; i < n; i++) \ cout << a[i] << " "; #define fauto(ar) \ for (auto x : ar) \ cout << x << " "; #define mod 1000000007 #define inf 10010010010010010 #define siz 100005 #define f first #define s second #define pb push_back #define endl "\n" #define ci cin.ignore() using namespace std; int main() { ll n, k, i, j, p, flag; cin >> n >> k; ll a[n + 1]; for (i = 1; i <= n; i++) cin >> a[i]; ll dp[2][k + 1]; for (i = 0; i <= 1; i++) for (j = 0; j <= k; j++) dp[i][j] = 0; // for(i = 0;i<=k;i++) // dp[0][i] = 1; ll sum[k + 1]; flag = 1; for (i = 1; i <= n; i++) { sum[0] = 1; for (j = 1; j <= k; j++) sum[j] = sum[j - 1] + dp[flag ^ 1][j], sum[j] %= mod; dp[flag][0] = 1; for (j = 1; j <= k; j++) { ll left = 0; if (j - a[i] - 1 >= 0) left = sum[j - a[i] - 1]; ll right = sum[j]; dp[flag][j] = right - left; dp[flag][j] %= mod; dp[flag][j] += mod; dp[flag][j] %= mod; } flag ^= 1; } cout << dp[flag ^ 1][k]; }
replace
48
49
48
49
0
p03172
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define INF (1ll << 60) long long M = 1000000007; vector<vector<long long>> dp; vector<long long> a; long long solve(int i, int r) { if (r < 0) return 0; if (dp[i][r] != -1) return dp[i][r]; if (i == 0) { if (r == 0) return 1; else return 0; } dp[i][r] = 0; for (int k = 0; k <= a[i - 1]; ++k) { dp[i][r] += solve(i - 1, r - k); dp[i][r] %= M; } return dp[i][r]; } int main(int argc, char const *argv[]) { long long n, k; cin >> n >> k; a = vector<long long>(n); dp = vector<vector<long long>>(n + 1, vector<long long>(k + 1, -1)); for (int i = 0; i < n; ++i) cin >> a[i]; cout << solve(n, k) << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define INF (1ll << 60) long long M = 1000000007; vector<vector<long long>> dp; vector<long long> a; long long solve(int i, int r) { if (r < 0) return 0; if (dp[i][r] != -1) return dp[i][r]; if (i == 0) { if (r == 0) return 1; else return 0; } dp[i][r] = 0; // dp[i][r+1] - dp[i][r] = solve(i-1,r+1) - solve(i-1,r-a[i-1]); // for(int k=0;k<=a[i-1];++k){ // dp[i][r] += solve(i-1,r-k); // dp[i][r] %= M; // } dp[i][r] = solve(i, r - 1) + solve(i - 1, r) - solve(i - 1, r - a[i - 1] - 1); dp[i][r] %= M; dp[i][r] += M; dp[i][r] %= M; return dp[i][r]; } int main(int argc, char const *argv[]) { long long n, k; cin >> n >> k; a = vector<long long>(n); dp = vector<vector<long long>>(n + 1, vector<long long>(k + 1, -1)); for (int i = 0; i < n; ++i) cin >> a[i]; cout << solve(n, k) << endl; return 0; }
replace
32
36
32
41
TLE
p03172
C++
Runtime Error
#include <iostream> using namespace std; const int mod = 1000000007; int N, K; int a[110]; int dp[110][100010]; int main() { cin >> N >> K; for (int i = 0; i < N; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 0; i < N; i++) { // 累積和を取る int sum[100010]; sum[0] = 0; for (int j = 0; j <= K; j++) sum[j + 1] = (sum[j] + dp[i - 1][j]) % mod; for (int j = 0; j <= K; j++) // 負の mod に気を付ける dp[i + 1][j] = (sum[j + 1] - sum[max(0, j - a[i])] + mod) % mod; } cout << dp[N][K] << endl; return 0; }
#include <iostream> using namespace std; const int mod = 1000000007; int N, K; int a[110]; int dp[110][100010]; int main() { cin >> N >> K; for (int i = 0; i < N; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 0; i < N; i++) { // 累積和を取る int sum[100010]; sum[0] = 0; for (int j = 0; j <= K; j++) sum[j + 1] = (sum[j] + dp[i][j]) % mod; for (int j = 0; j <= K; j++) // 負の mod に気を付ける dp[i + 1][j] = (sum[j + 1] - sum[max(0, j - a[i])] + mod) % mod; } cout << dp[N][K] << endl; return 0; }
replace
21
22
21
22
-11
p03172
C++
Runtime Error
/** 🍪 the_hyp0cr1t3 🍪 31.08.2020 16:36:44 **/ #ifdef W #include "k_II.h" #else #include <bits/stdc++.h> using namespace std; #define aylmao cin.tie(nullptr)->sync_with_stdio(false); #endif #define endl '\n' #define pb emplace_back // #define int int64_t #define sz(x) (int)((x).size()) #define all(x) x.begin(), x.end() #define Luv(...) [&](auto &&u, auto &&v) { return __VA_ARGS__; } auto chmax = [](auto &A, auto &&B) { return B > A ? A = B, true : false; }; auto chmin = [](auto &A, auto &&B) { return B < A ? A = B, true : false; }; using ll = int64_t; using pii = pair<int, int>; const ll DESPACITO = 2e18; const int INF = 2e9, MOD = 1e9 + 7; struct num { typedef decay<decltype(MOD)>::type T; T v; static T inv(T a, T m) { a %= m; return a == 1 ? 1 : (T)(m - ll(inv(m, a)) * ll(m) / a); } num() : v(0) {} num(ll v_) { v = (-MOD < v_ && v_ < MOD) ? T(v_) : T(v_ % MOD); if (v < 0) v += MOD; } explicit operator T() const { return v; } friend std::ostream &operator<<(std::ostream &out, const num &n) { return out << T(n); } friend std::istream &operator>>(std::istream &in, num &n) { ll v_; in >> v_; n = num(v_); return in; } friend bool operator==(const num &a, const num &b) { return a.v == b.v; } friend bool operator!=(const num &a, const num &b) { return a.v != b.v; } num inv() const { num res; res.v = inv(v, MOD); return res; } num operator-() const { num res; res.v = v ? MOD - v : 0; return res; } num operator+() const { return num(*this); } num &operator++() { v++; if (v == MOD) v = 0; return *this; } num &operator--() { if (v == 0) v = MOD; v--; return *this; } num &operator+=(const num &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } num &operator-=(const num &o) { v -= o.v; if (v < 0) v += MOD; return *this; } num &operator*=(const num &o) { v = T(ll(v) * ll(o.v) % MOD); return *this; } num &operator/=(const num &o) { return *this *= o.inv(); } friend num operator++(num &a, int) { num r = a; ++a; return r; } friend num operator--(num &a, int) { num r = a; --a; return r; } friend num operator+(const num &a, const num &b) { return num(a) += b; } friend num operator-(const num &a, const num &b) { return num(a) -= b; } friend num operator*(const num &a, const num &b) { return num(a) *= b; } friend num operator/(const num &a, const num &b) { return num(a) /= b; } friend bool operator<(const num &a, const num &b) { return a.v < b.v; } friend bool operator<=(const num &a, const num &b) { return a.v <= b.v; } friend bool operator>(const num &a, const num &b) { return a.v > b.v; } friend bool operator>=(const num &a, const num &b) { return a.v >= b.v; } }; const int N = 1e5 + 5; num dp[100][N]; int32_t main() { aylmao int i, j, n, k; cin >> n >> k; dp[0][k] = 1; for (i = 1; i <= n; i++) { int a; cin >> a; for (j = 0; j <= k; j++) { dp[i][max(j - a, 0)] += dp[i - 1][j]; dp[i][j + 1] -= dp[i - 1][j]; } for (j = 0; j <= k; j++) dp[i][j] += dp[i][j - 1]; } cout << dp[n][0]; return 0; }
/** 🍪 the_hyp0cr1t3 🍪 31.08.2020 16:36:44 **/ #ifdef W #include "k_II.h" #else #include <bits/stdc++.h> using namespace std; #define aylmao cin.tie(nullptr)->sync_with_stdio(false); #endif #define endl '\n' #define pb emplace_back // #define int int64_t #define sz(x) (int)((x).size()) #define all(x) x.begin(), x.end() #define Luv(...) [&](auto &&u, auto &&v) { return __VA_ARGS__; } auto chmax = [](auto &A, auto &&B) { return B > A ? A = B, true : false; }; auto chmin = [](auto &A, auto &&B) { return B < A ? A = B, true : false; }; using ll = int64_t; using pii = pair<int, int>; const ll DESPACITO = 2e18; const int INF = 2e9, MOD = 1e9 + 7; struct num { typedef decay<decltype(MOD)>::type T; T v; static T inv(T a, T m) { a %= m; return a == 1 ? 1 : (T)(m - ll(inv(m, a)) * ll(m) / a); } num() : v(0) {} num(ll v_) { v = (-MOD < v_ && v_ < MOD) ? T(v_) : T(v_ % MOD); if (v < 0) v += MOD; } explicit operator T() const { return v; } friend std::ostream &operator<<(std::ostream &out, const num &n) { return out << T(n); } friend std::istream &operator>>(std::istream &in, num &n) { ll v_; in >> v_; n = num(v_); return in; } friend bool operator==(const num &a, const num &b) { return a.v == b.v; } friend bool operator!=(const num &a, const num &b) { return a.v != b.v; } num inv() const { num res; res.v = inv(v, MOD); return res; } num operator-() const { num res; res.v = v ? MOD - v : 0; return res; } num operator+() const { return num(*this); } num &operator++() { v++; if (v == MOD) v = 0; return *this; } num &operator--() { if (v == 0) v = MOD; v--; return *this; } num &operator+=(const num &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } num &operator-=(const num &o) { v -= o.v; if (v < 0) v += MOD; return *this; } num &operator*=(const num &o) { v = T(ll(v) * ll(o.v) % MOD); return *this; } num &operator/=(const num &o) { return *this *= o.inv(); } friend num operator++(num &a, int) { num r = a; ++a; return r; } friend num operator--(num &a, int) { num r = a; --a; return r; } friend num operator+(const num &a, const num &b) { return num(a) += b; } friend num operator-(const num &a, const num &b) { return num(a) -= b; } friend num operator*(const num &a, const num &b) { return num(a) *= b; } friend num operator/(const num &a, const num &b) { return num(a) /= b; } friend bool operator<(const num &a, const num &b) { return a.v < b.v; } friend bool operator<=(const num &a, const num &b) { return a.v <= b.v; } friend bool operator>(const num &a, const num &b) { return a.v > b.v; } friend bool operator>=(const num &a, const num &b) { return a.v >= b.v; } }; const int N = 1e5 + 5; num dp[101][N]; int32_t main() { aylmao int i, j, n, k; cin >> n >> k; dp[0][k] = 1; for (i = 1; i <= n; i++) { int a; cin >> a; for (j = 0; j <= k; j++) { dp[i][max(j - a, 0)] += dp[i - 1][j]; dp[i][j + 1] -= dp[i - 1][j]; } for (j = 0; j <= k; j++) dp[i][j] += dp[i][j - 1]; } cout << dp[n][0]; return 0; }
replace
111
112
111
112
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct Benri { Benri() { std::cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12); } } benri; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vll = vector<long long>; using vvll = vector<vll>; using pll = pair<ll, ll>; using ull = unsigned long long; template <typename T> using PQ = priority_queue<T>; template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define mp make_pair #define F first #define S second 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; } constexpr long long MOD = 1000000007; // constexpr long long MOD = 998244353; // constexpr int INF = 1001001001; constexpr ll INF = 1001001001001001001ll; constexpr double EPS = 1e-10; using number = long long; ll dp[100][100005]; int main() { int N, K; cin >> N >> K; vi a(N + 1, 0); rep(i, N) cin >> a[i + 1]; dp[0][0] = 1; for (int i = 1; i <= N; i++) { rep(j, K + 1) { dp[i][j] = dp[i - 1][j]; if (j - 1 >= 0) dp[i][j] += dp[i][j - 1]; if (j - 1 - a[i] >= 0) dp[i][j] -= dp[i - 1][j - 1 - a[i]]; dp[i][j] = (dp[i][j] + MOD) % MOD; } } /*rep(i, N + 1) { rep(j, K + 1) { cout << dp[i][j] << " "; } cout << endl; }*/ cout << dp[N][K] << endl; }
#include <bits/stdc++.h> using namespace std; struct Benri { Benri() { std::cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12); } } benri; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vll = vector<long long>; using vvll = vector<vll>; using pll = pair<ll, ll>; using ull = unsigned long long; template <typename T> using PQ = priority_queue<T>; template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define mp make_pair #define F first #define S second 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; } constexpr long long MOD = 1000000007; // constexpr long long MOD = 998244353; // constexpr int INF = 1001001001; constexpr ll INF = 1001001001001001001ll; constexpr double EPS = 1e-10; using number = long long; ll dp[105][100005]; int main() { int N, K; cin >> N >> K; vi a(N + 1, 0); rep(i, N) cin >> a[i + 1]; dp[0][0] = 1; for (int i = 1; i <= N; i++) { rep(j, K + 1) { dp[i][j] = dp[i - 1][j]; if (j - 1 >= 0) dp[i][j] += dp[i][j - 1]; if (j - 1 - a[i] >= 0) dp[i][j] -= dp[i - 1][j - 1 - a[i]]; dp[i][j] = (dp[i][j] + MOD) % MOD; } } /*rep(i, N + 1) { rep(j, K + 1) { cout << dp[i][j] << " "; } cout << endl; }*/ cout << dp[N][K] << endl; }
replace
52
53
52
53
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1e9 + 7 template <typename T> void printVector(const vector<T> &vec) { for (const T x : vec) { cout << x << " "; } cout << endl; } template <typename T> void printMatrix(const vector<vector<T>> &mat) { for (const vector<T> vec : mat) { printVector(vec); } cout << endl; } const ll INF = 1e18L + 5; template <typename T> void min_self(T &a, T b) { a = min(a, b); } template <typename T> void max_self(T &a, T b) { a = max(a, b); } template <typename T> void add_self(T &a, T b) { a += b; if (a >= MOD) { a -= MOD; } } template <typename T> void sub_self(T &a, T b) { a -= b; if (a < 0) { a += MOD; } } int main() { int n, k; scanf("%d %d", &n, &k); vector<int> a(n); for (int &x : a) scanf("%d", &x); vector<int> dp(k + 1); dp[0] = 1; for (int child = 0; child < n; child++) { // upper bound = a[child] // lower bound = 0 vector<int> vec(k + 1, 0); for (int used = k; used >= 0; used--) { // used -> #used (distributed) candies // toGive >= 0 and toGive <= a[child] and toGive + used <= k // => toGive is in [0, min (a[child], k - used)] int to = min(a[child], k - used); // x = toGive + used // toGive = 1 -> toGive + used = used + 1 // toGive = to -> toGive + used = used + to int y = used + 1, z = used + to; int temp = dp[used]; // for (int x = y; x <= z; x++) { // add_self (dp[x], temp); // } add_self(vec[y], temp); if (z < k) sub_self(vec[z + 1], temp); } int prefix_sum = 0; for (int i = 0; i <= k; i++) { add_self(prefix_sum, vec[i]); add_self(dp[i], prefix_sum); } } printf("%d\n", dp[k]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD 1e9 + 7 template <typename T> void printVector(const vector<T> &vec) { for (const T x : vec) { cout << x << " "; } cout << endl; } template <typename T> void printMatrix(const vector<vector<T>> &mat) { for (const vector<T> vec : mat) { printVector(vec); } cout << endl; } const ll INF = 1e18L + 5; template <typename T> void min_self(T &a, T b) { a = min(a, b); } template <typename T> void max_self(T &a, T b) { a = max(a, b); } template <typename T> void add_self(T &a, T b) { a += b; if (a >= MOD) { a -= MOD; } } template <typename T> void sub_self(T &a, T b) { a -= b; if (a < 0) { a += MOD; } } int main() { int n, k; scanf("%d %d", &n, &k); vector<int> a(n); for (int &x : a) scanf("%d", &x); vector<int> dp(k + 1); dp[0] = 1; for (int child = 0; child < n; child++) { // upper bound = a[child] // lower bound = 0 vector<int> vec(k + 1, 0); for (int used = k; used >= 0; used--) { // used -> #used (distributed) candies // toGive >= 0 and toGive <= a[child] and toGive + used <= k // => toGive is in [0, min (a[child], k - used)] int to = min(a[child], k - used); // x = toGive + used // toGive = 1 -> toGive + used = used + 1 // toGive = to -> toGive + used = used + to int y = used + 1, z = used + to; int temp = dp[used]; // for (int x = y; x <= z; x++) { // add_self (dp[x], temp); // } if (y <= z) { add_self(vec[y], temp); if (z < k) sub_self(vec[z + 1], temp); } } int prefix_sum = 0; for (int i = 0; i <= k; i++) { add_self(prefix_sum, vec[i]); add_self(dp[i], prefix_sum); } } printf("%d\n", dp[k]); return 0; }
replace
65
68
65
70
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define mp make_pair #define forn(i, n) for (ll i = 0; i < n; i++) #define fore(i, a, b) for (ll i = a; i <= b; i++) #define ford(i, n) for (ll i = n - 1; i >= 0; i--) #define fi first #define se second #define endl "\n" #define all(a) a.begin(), a.end() #define sync \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define PI 3.14159265 /*************************************************************************************/ void file() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif } /*************************************************************************************/ const ll maxn = 2e5 + 1, mod = 1e9 + 7; int main() { sync // file(); int n, k; cin >> n >> k; vector<ll> dp(k + 1); dp[0] = 1; forn(i, n) { int x; cin >> x; vector<int> fake(k + 1); for (int j = k; j >= 0; --j) { int l = j + 1; int r = j + min(x, k - j) + 1; if (l < r && l <= k) { fake[l] += dp[j]; fake[l] %= mod; fake[r] -= dp[j]; fake[r] += mod; fake[r] %= mod; } } ll pre = 0; fore(lel, 0, k) { pre += fake[lel]; pre %= mod; dp[lel] += pre; dp[lel] %= mod; } } cout << dp[k] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define mp make_pair #define forn(i, n) for (ll i = 0; i < n; i++) #define fore(i, a, b) for (ll i = a; i <= b; i++) #define ford(i, n) for (ll i = n - 1; i >= 0; i--) #define fi first #define se second #define endl "\n" #define all(a) a.begin(), a.end() #define sync \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define PI 3.14159265 /*************************************************************************************/ void file() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif } /*************************************************************************************/ const ll maxn = 2e5 + 1, mod = 1e9 + 7; int main() { sync // file(); int n, k; cin >> n >> k; vector<ll> dp(k + 1); dp[0] = 1; forn(i, n) { int x; cin >> x; vector<int> fake(k + 1); for (int j = k; j >= 0; --j) { int l = j + 1; int r = j + min(x, k - j) + 1; if (l < r && l <= k) { fake[l] += dp[j]; fake[l] %= mod; if (r <= k) { fake[r] -= dp[j]; fake[r] += mod; fake[r] %= mod; } } } ll pre = 0; fore(lel, 0, k) { pre += fake[lel]; pre %= mod; dp[lel] += pre; dp[lel] %= mod; } } cout << dp[k] << endl; return 0; }
replace
51
54
51
56
0
p03172
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; static const int kModulo = 1000 * 1000 * 1000 + 7; int main() { ios::sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> ways(K + 1, 0); ways[0] = 1; for (int i = 0; i < N; ++i) { int a; cin >> a; vector<int> partial(N + 1); for (int j = 0; j <= K; ++j) { partial[j] = ways[j]; if (j > 0) partial[j] = (partial[j] + partial[j - 1]) % kModulo; } for (int j = 0; j <= K; ++j) { ways[j] = partial[j]; if (j > a) ways[j] = (ways[j] + kModulo - partial[j - a - 1]) % kModulo; } } cout << ways[K] << "\n"; }
#include <iostream> #include <vector> using namespace std; static const int kModulo = 1000 * 1000 * 1000 + 7; int main() { ios::sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> ways(K + 1, 0); ways[0] = 1; for (int i = 0; i < N; ++i) { int a; cin >> a; vector<int> partial(K + 1); for (int j = 0; j <= K; ++j) { partial[j] = ways[j]; if (j > 0) partial[j] = (partial[j] + partial[j - 1]) % kModulo; } for (int j = 0; j <= K; ++j) { ways[j] = partial[j]; if (j > a) ways[j] = (ways[j] + kModulo - partial[j - a - 1]) % kModulo; } } cout << ways[K] << "\n"; }
replace
19
20
19
20
0
p03172
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #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) using namespace std; const int INF = 1001001001; const long long LINF = 1001002003004005006ll; 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; } typedef long long ll; typedef pair<int, int> P; const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } mint dp[105][100005]; mint cum[105][100005]; int main() { int n, K; cin >> n >> K; int a[n]; rep(i, n) cin >> a[i]; dp[0][0] = 1; // for(int i=1;i<=n;i++){ // rep(j,K+1){ // rep(k,a[i-1]+1){ // if(j-k>=0)dp[i][j]+=dp[i-1][j-k]; // } // } // } for (int i = 1; i <= n; i++) { rrep(j, K + 1) cum[i][j] = cum[i][j - 1] + dp[i - 1][j - 1]; rep(j, K + 1) dp[i][j] = cum[i][j + 1] - cum[i][max(0, j - a[i])]; } // rep(i,n+1){ // rep(j,K+1){ // cout << dp[i][j]; // } // cout << endl; // } cout << dp[n][K] << endl; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #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) using namespace std; const int INF = 1001001001; const long long LINF = 1001002003004005006ll; 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; } typedef long long ll; typedef pair<int, int> P; const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } mint dp[105][100005]; mint cum[105][100005]; int main() { int n, K; cin >> n >> K; int a[n]; rep(i, n) cin >> a[i]; dp[0][0] = 1; // for(int i=1;i<=n;i++){ // rep(j,K+1){ // rep(k,a[i-1]+1){ // if(j-k>=0)dp[i][j]+=dp[i-1][j-k]; // } // } // } for (int i = 1; i <= n; i++) { rrep(j, K + 1) cum[i][j] = cum[i][j - 1] + dp[i - 1][j - 1]; rep(j, K + 1) dp[i][j] = cum[i][j + 1] - cum[i][max(0, j - a[i - 1])]; } // rep(i,n+1){ // rep(j,K+1){ // cout << dp[i][j]; // } // cout << endl; // } cout << dp[n][K] << endl; }
replace
105
106
105
106
-11
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fto(i, s, e) for (int i = s; i <= e; ++i) #define fto1(i, s, e) for (int i = s; i < e; ++i) #define fdto(i, s, e) for (int i = s; i >= e; --i) #define fit(var, it) \ for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it) #define frit(var, it) \ for (__typeof(var.rbegin()) it = var.rbegin(); it != var.rend(); ++it) #define newl '\n' #define debugt cerr << 0.001 * clock() << newl #define debug1(x, i) cout << #x << '[' << i << "] = " << x[i] << newl #define debug(v, l, r) fto(_i, l, r) debug1(v, _i) #define debug2(x, i, j) \ cout << #x << '[' << i << "][" << j << "] = " << x[i][j] << newl #define debug3(x, i, j, k) \ cout << #x << '[' << i << "][" << j << "][" << k << "] = " << x[i][j][k] \ << newl #define ll long long #define ii pair<int, int> #define ff first #define ss second #define pb push_back #define glen(v) int((v).size()) template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> const &v) { return os << '(' << v.ff << ", " << v.ss << ')'; } #define eps 1e-15 #define oo 1000000007 #define OO 1000000000000000003LL #define maxn 103 int n, k; int a[maxn], sum[maxn]; int f[maxn][100003]; int main() { // ios_base::sync_with_stdio(0); cin.tie(0); scanf("%d%d", &n, &k); fto(i, 1, n) scanf("%d", &a[i]); fto(i, 0, k) sum[i] = 1; fto(i, 1, n) { fto(j, 0, k) { int mn = min(j, a[i]); f[i][j] = (sum[j] - (j - mn == 0 ? 0 : sum[j - mn - 1])) % oo; if (f[i][j] < 0) f[i][j] += oo; } sum[0] = f[i][0]; fto(j, 1, k) sum[j] = (sum[j - 1] + f[i][j]) % oo; } printf("%d\n", f[n][k]); return 0; }
#include <bits/stdc++.h> using namespace std; #define fto(i, s, e) for (int i = s; i <= e; ++i) #define fto1(i, s, e) for (int i = s; i < e; ++i) #define fdto(i, s, e) for (int i = s; i >= e; --i) #define fit(var, it) \ for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it) #define frit(var, it) \ for (__typeof(var.rbegin()) it = var.rbegin(); it != var.rend(); ++it) #define newl '\n' #define debugt cerr << 0.001 * clock() << newl #define debug1(x, i) cout << #x << '[' << i << "] = " << x[i] << newl #define debug(v, l, r) fto(_i, l, r) debug1(v, _i) #define debug2(x, i, j) \ cout << #x << '[' << i << "][" << j << "] = " << x[i][j] << newl #define debug3(x, i, j, k) \ cout << #x << '[' << i << "][" << j << "][" << k << "] = " << x[i][j][k] \ << newl #define ll long long #define ii pair<int, int> #define ff first #define ss second #define pb push_back #define glen(v) int((v).size()) template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> const &v) { return os << '(' << v.ff << ", " << v.ss << ')'; } #define eps 1e-15 #define oo 1000000007 #define OO 1000000000000000003LL #define maxn 103 int n, k; int a[maxn], sum[100003]; int f[maxn][100003]; int main() { // ios_base::sync_with_stdio(0); cin.tie(0); scanf("%d%d", &n, &k); fto(i, 1, n) scanf("%d", &a[i]); fto(i, 0, k) sum[i] = 1; fto(i, 1, n) { fto(j, 0, k) { int mn = min(j, a[i]); f[i][j] = (sum[j] - (j - mn == 0 ? 0 : sum[j - mn - 1])) % oo; if (f[i][j] < 0) f[i][j] += oo; } sum[0] = f[i][0]; fto(j, 1, k) sum[j] = (sum[j - 1] + f[i][j]) % oo; } printf("%d\n", f[n][k]); return 0; }
replace
40
41
40
41
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define DUMP(x) std::cerr << (#x) << " = " << (x) << "\n" #define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define EREP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) #define RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i) #define rep(i, n) REP(i, 0, n) #define erep(i, n) EREP(i, 0, n) #define rrep(i, n) RREP(i, n, 0) #define ALL(r) r.begin(), r.end() #define YES cout << "YES\n" #define Yes cout << "Yes\n" #define NO cout << "NO\n" #define No cout << "No\n" #define IMP cout << "IMPOSSIBLE\n" #define Imp cout << "Impossible\n" #define imp cout << "impossible\n" #define M1 cout << "-1\n" #define pb push_back #define eb emplace_back #define fi first #define se second template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << "{"; rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", "); os << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &p) { return (os << "(" << p.first << ", " << p.second << ")"); } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { bool first = true; os << "{"; for (const auto e : m) { if (!first) os << ", "; os << "{" << e.first << ": " << e.second << "}"; first = false; } os << "}"; return os; } template <typename T> T dup(T x, T y) { return (x + y - 1) / y; }; template <typename A, size_t N, typename T> inline void arrayFill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } 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; } struct in { const size_t n = 0; in() = default; in(size_t n) : n(n){}; template <typename T> operator T() { T ret; cin >> ret; return ret; } template <typename T> operator vector<T>() { assert(n != 0); vector<T> ret(n); for (T &x : ret) { T tmp = in(); x = tmp; } return ret; } template <typename T, typename U> operator pair<T, U>() { pair<T, U> ret; ret.first = in(); ret.second = in(); return ret; } }; using ll = int64_t; using vint = vector<int32_t>; using vvint = vector<vint>; using vll = vector<ll>; using vvll = vector<vll>; using vstr = vector<string>; using pint = pair<int32_t, int32_t>; using vpint = vector<pint>; using pll = pair<ll, ll>; using vpll = vector<pll>; using setint = set<int32_t>; using setstr = set<string>; using qint = queue<int32_t>; using qpint = queue<pint>; constexpr std::int32_t INF = 1001001001; constexpr std::int64_t LINF = 1001001001001001001; constexpr int mod = 1'000'000'007; // constexpr int mod = 998'244'353; class mint { private: std::int64_t m_x; public: mint(std::int64_t m_x = 0) : m_x((m_x % mod + mod) % mod) {} std::int64_t get() const { return m_x; } mint operator-() const { return mint(-m_x); } mint &operator+=(const mint a) { if ((m_x += a.m_x) >= mod) m_x -= mod; return *this; } mint &operator-=(const mint a) { if ((m_x += mod - a.m_x) >= mod) m_x -= mod; return *this; } mint &operator*=(const mint a) { (m_x *= a.m_x) %= mod; return *this; } mint &operator++() { return *this += 1; } mint operator++(int) { mint tmp = *this; ++*this; return tmp; } mint operator--() { return *this -= 1; } mint operator--(int) { mint tmp = *this; --*this; return tmp; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(const std::int64_t t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream &operator>>(istream &is, mint &a) { ll x; is >> x; a = x; return is; } ostream &operator<<(ostream &os, const mint &a) { os << a.get(); return os; } mint dp[100][100005]; void Main() { arrayFill(dp, 0); int n = in(), k = in(); vint a = in(n); dp[0][0] = 1; EREP(i, 1, n) { vector<mint> sum(k + 2, 0); erep(j, k) sum[j + 1] = sum[j] + dp[i - 1][j]; erep(j, k) { dp[i][j] = sum[j + 1] - sum[max(0, j - a[i - 1])]; } } cout << dp[n][k] << endl; } signed main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); Main(); return 0; }
#include <bits/stdc++.h> using namespace std; #define DUMP(x) std::cerr << (#x) << " = " << (x) << "\n" #define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define EREP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) #define RREP(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i) #define rep(i, n) REP(i, 0, n) #define erep(i, n) EREP(i, 0, n) #define rrep(i, n) RREP(i, n, 0) #define ALL(r) r.begin(), r.end() #define YES cout << "YES\n" #define Yes cout << "Yes\n" #define NO cout << "NO\n" #define No cout << "No\n" #define IMP cout << "IMPOSSIBLE\n" #define Imp cout << "Impossible\n" #define imp cout << "impossible\n" #define M1 cout << "-1\n" #define pb push_back #define eb emplace_back #define fi first #define se second template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << "{"; rep(i, v.size()) os << v[i] << (i == (int)v.size() - 1 ? "" : ", "); os << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &p) { return (os << "(" << p.first << ", " << p.second << ")"); } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { bool first = true; os << "{"; for (const auto e : m) { if (!first) os << ", "; os << "{" << e.first << ": " << e.second << "}"; first = false; } os << "}"; return os; } template <typename T> T dup(T x, T y) { return (x + y - 1) / y; }; template <typename A, size_t N, typename T> inline void arrayFill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } 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; } struct in { const size_t n = 0; in() = default; in(size_t n) : n(n){}; template <typename T> operator T() { T ret; cin >> ret; return ret; } template <typename T> operator vector<T>() { assert(n != 0); vector<T> ret(n); for (T &x : ret) { T tmp = in(); x = tmp; } return ret; } template <typename T, typename U> operator pair<T, U>() { pair<T, U> ret; ret.first = in(); ret.second = in(); return ret; } }; using ll = int64_t; using vint = vector<int32_t>; using vvint = vector<vint>; using vll = vector<ll>; using vvll = vector<vll>; using vstr = vector<string>; using pint = pair<int32_t, int32_t>; using vpint = vector<pint>; using pll = pair<ll, ll>; using vpll = vector<pll>; using setint = set<int32_t>; using setstr = set<string>; using qint = queue<int32_t>; using qpint = queue<pint>; constexpr std::int32_t INF = 1001001001; constexpr std::int64_t LINF = 1001001001001001001; constexpr int mod = 1'000'000'007; // constexpr int mod = 998'244'353; class mint { private: std::int64_t m_x; public: mint(std::int64_t m_x = 0) : m_x((m_x % mod + mod) % mod) {} std::int64_t get() const { return m_x; } mint operator-() const { return mint(-m_x); } mint &operator+=(const mint a) { if ((m_x += a.m_x) >= mod) m_x -= mod; return *this; } mint &operator-=(const mint a) { if ((m_x += mod - a.m_x) >= mod) m_x -= mod; return *this; } mint &operator*=(const mint a) { (m_x *= a.m_x) %= mod; return *this; } mint &operator++() { return *this += 1; } mint operator++(int) { mint tmp = *this; ++*this; return tmp; } mint operator--() { return *this -= 1; } mint operator--(int) { mint tmp = *this; --*this; return tmp; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(const std::int64_t t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream &operator>>(istream &is, mint &a) { ll x; is >> x; a = x; return is; } ostream &operator<<(ostream &os, const mint &a) { os << a.get(); return os; } mint dp[105][100005]; void Main() { arrayFill(dp, 0); int n = in(), k = in(); vint a = in(n); dp[0][0] = 1; EREP(i, 1, n) { vector<mint> sum(k + 2, 0); erep(j, k) sum[j + 1] = sum[j] + dp[i - 1][j]; erep(j, k) { dp[i][j] = sum[j + 1] - sum[max(0, j - a[i - 1])]; } } cout << dp[n][k] << endl; } signed main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); Main(); return 0; }
replace
188
189
188
189
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; ll n, m, t, k, a[105]; #define ari \ for (int i = 0; i < n; i++) \ cin >> a[i]; #define ginp \ for (int i = 0; i < m; i++) { \ int u, v; \ cin >> u >> v; \ adj[u].push_back(v); \ adj[v].push_back(u); \ } #define pb push_back #define ss second #define ff first #define fs first.second #define fff first.first #define sss second.second #define sf second.first #define mp make_pair const ll mod = 1e9 + 7; void add(ll &a, ll b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } void sub(ll &a, ll b) { a -= b; if (a >= mod) a -= mod; if (a < 0) a += mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; ari; vector<ll> dp(k + 1, 0); ll maxi = 0; dp[0] = 1; for (int i = 0; i < n; i++) { vector<ll> temp(k + 1, 0); for (int j = k; j >= 0; j--) { add(temp[j + 1], dp[j]); if (a[i] + 1 <= k - j) sub(temp[j + a[i] + 1], dp[j]); } ll prefix_sum = 0; for (int j = 0; j <= k; j++) { add(prefix_sum, temp[j]); add(dp[j], prefix_sum); } } cout << dp[k] << endl; return (0); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; ll n, m, t, k, a[105]; #define ari \ for (int i = 0; i < n; i++) \ cin >> a[i]; #define ginp \ for (int i = 0; i < m; i++) { \ int u, v; \ cin >> u >> v; \ adj[u].push_back(v); \ adj[v].push_back(u); \ } #define pb push_back #define ss second #define ff first #define fs first.second #define fff first.first #define sss second.second #define sf second.first #define mp make_pair const ll mod = 1e9 + 7; void add(ll &a, ll b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } void sub(ll &a, ll b) { a -= b; if (a >= mod) a -= mod; if (a < 0) a += mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; ari; vector<ll> dp(k + 1, 0); ll maxi = 0; dp[0] = 1; for (int i = 0; i < n; i++) { vector<ll> temp(k + 1, 0); for (int j = k; j >= 0; j--) { if (j + 1 <= j + min(a[i], k - j)) { add(temp[j + 1], dp[j]); if (a[i] + 1 <= k - j) sub(temp[j + min(a[i], k - j) + 1], dp[j]); } } ll prefix_sum = 0; for (int j = 0; j <= k; j++) { add(prefix_sum, temp[j]); add(dp[j], prefix_sum); } } cout << dp[k] << endl; return (0); }
replace
52
55
52
57
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define LL long long #define pii pair<int, int> #define pll pair<ll, ll> #define pil pair<int, ll> #define pli pair<ll, int> #define vi vector<int> #define vl vector<ll> #define vii vector<pii> #define vll vector<pll> #define vil vector<pil> #define vli vector<pli> #define pb push_back #define ppb pop_back #define mp make_pair #define ff first #define ss second #define sz(x) (int)x.size() #define TN typename #define all(v) v.begin(), v.end() #define fill(a, b) memset(a, b, sizeof(a)) #define endl '\n' template <TN T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); } template <TN T> T lcm(T a, T b) { return a * (b / gcd(a, b)); } template <TN T> T sqr(T a) { return a * a; } template <TN T> T cube(T a) { return a * a * a; } template <TN T> inline void smin(T &a, T b) { a = a < b ? a : b; } template <TN T> inline void smax(T &a, T b) { a = a > b ? a : b; } template <TN T> inline void Int(T &n) { n = 0; int f = 1; register int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0'; n = n * f; } #define error(args...) \ { \ vector<string> _v = split(#args, ','); \ err(_v.begin(), args); \ cout << endl; \ } vector<string> split(const string &s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", "; err(++it, args...); } inline int in() { int n; scanf("%d", &n); return n; } inline ll Lin() { ll n; scanf("%lld", &n); return n; } inline double Din() { double n; scanf("%lf", &n); return n; } inline int add(int a, int b, int mod) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b, int mod) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b, int mod) { return (ll)a * b % mod; } const int N = 2e5 + 5; const ll Mod = (ll)1e9 + 7; const int inf = (int)2e9; const ll Inf = (ll)1e18; const int mod = (int)1e9 + 7; int solve() { int n = in(), k = in(); vi dp(k + 1); dp[0] = 1; for (int child = 1; child <= n; child++) { int upto = in(); vi prefix(k + 1); for (int used = k; used >= 0; used--) { int tem = dp[used]; int l = used + 1, r = min(k, used + upto); prefix[l] = add(prefix[l], tem, mod); if (r + 1 <= k) prefix[r + 1] = sub(prefix[r + 1], tem, mod); } int sum = 0; for (int i = 1; i <= k; i++) { sum = add(sum, prefix[i], mod); dp[i] = add(dp[i], sum, mod); } } printf("%d\n", dp[k]); return 0; } int main() { int test = 1, tc = 0; // scanf("%d", &test); while (test--) { // printf("Case %d: ", ++tc); solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define LL long long #define pii pair<int, int> #define pll pair<ll, ll> #define pil pair<int, ll> #define pli pair<ll, int> #define vi vector<int> #define vl vector<ll> #define vii vector<pii> #define vll vector<pll> #define vil vector<pil> #define vli vector<pli> #define pb push_back #define ppb pop_back #define mp make_pair #define ff first #define ss second #define sz(x) (int)x.size() #define TN typename #define all(v) v.begin(), v.end() #define fill(a, b) memset(a, b, sizeof(a)) #define endl '\n' template <TN T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); } template <TN T> T lcm(T a, T b) { return a * (b / gcd(a, b)); } template <TN T> T sqr(T a) { return a * a; } template <TN T> T cube(T a) { return a * a * a; } template <TN T> inline void smin(T &a, T b) { a = a < b ? a : b; } template <TN T> inline void smax(T &a, T b) { a = a > b ? a : b; } template <TN T> inline void Int(T &n) { n = 0; int f = 1; register int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0'; n = n * f; } #define error(args...) \ { \ vector<string> _v = split(#args, ','); \ err(_v.begin(), args); \ cout << endl; \ } vector<string> split(const string &s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", "; err(++it, args...); } inline int in() { int n; scanf("%d", &n); return n; } inline ll Lin() { ll n; scanf("%lld", &n); return n; } inline double Din() { double n; scanf("%lf", &n); return n; } inline int add(int a, int b, int mod) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b, int mod) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b, int mod) { return (ll)a * b % mod; } const int N = 2e5 + 5; const ll Mod = (ll)1e9 + 7; const int inf = (int)2e9; const ll Inf = (ll)1e18; const int mod = (int)1e9 + 7; int solve() { int n = in(), k = in(); vi dp(k + 1); dp[0] = 1; for (int child = 1; child <= n; child++) { int upto = in(); vi prefix(k + 1); for (int used = k; used >= 0; used--) { int tem = dp[used]; int l = used + 1, r = min(k, used + upto); if (l <= r) { prefix[l] = add(prefix[l], tem, mod); if (r + 1 <= k) prefix[r + 1] = sub(prefix[r + 1], tem, mod); } } int sum = 0; for (int i = 1; i <= k; i++) { sum = add(sum, prefix[i], mod); dp[i] = add(dp[i], sum, mod); } } printf("%d\n", dp[k]); return 0; } int main() { int test = 1, tc = 0; // scanf("%d", &test); while (test--) { // printf("Case %d: ", ++tc); solve(); } return 0; }
replace
108
111
108
113
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int n, k, i, lim; int dp[100001]; int pre[100001]; int main() { scanf("%d%d", &n, &k); fill(pre, pre + k + 1, 1); while (n--) { scanf("%d", &lim); for (i = 0; i <= k; i++) { dp[i] = pre[i]; if (i - lim - 1 >= 0) { dp[i] -= pre[i - lim - i]; if (dp[i] < 0) dp[i] += mod; } } for (i = 1; i <= k; i++) { pre[i] = pre[i - 1] + dp[i]; if (pre[i] >= mod) pre[i] -= mod; } } printf("%d\n", dp[k]); return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int n, k, i, lim; int dp[100001]; int pre[100001]; int main() { scanf("%d%d", &n, &k); fill(pre, pre + k + 1, 1); while (n--) { scanf("%d", &lim); for (i = 0; i <= k; i++) { dp[i] = pre[i]; if (i - lim - 1 >= 0) { dp[i] -= pre[i - lim - 1]; if (dp[i] < 0) dp[i] += mod; } } for (i = 1; i <= k; i++) { pre[i] = pre[i - 1] + dp[i]; if (pre[i] >= mod) pre[i] -= mod; } } printf("%d\n", dp[k]); return 0; }
replace
16
17
16
17
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; typedef pair<ll, ll> pll; const ll mod = 1e9 + 7; // const ll mod=998244353; const ll inf = 1LL << 61; int main() { ll n, k; cin >> n >> k; vec a(n); for (ll i = 0; i < n; i++) cin >> a[i]; mat dp(n + 1, vec(k + 10)); dp[0][0] = 1; for (ll i = 0; i < n; i++) { vec sum(k + 1); for (ll j = 0; j <= k; j++) { (sum[j + 1] = sum[j] + dp[i][j]) %= mod; } for (ll j = 0; j <= k; j++) { dp[i + 1][j] = (sum[j + 1] - sum[max(0LL, j - a[i])] + mod) % mod; } } cout << dp[n][k] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; typedef pair<ll, ll> pll; const ll mod = 1e9 + 7; // const ll mod=998244353; const ll inf = 1LL << 61; int main() { ll n, k; cin >> n >> k; vec a(n); for (ll i = 0; i < n; i++) cin >> a[i]; mat dp(n + 1, vec(k + 10)); dp[0][0] = 1; for (ll i = 0; i < n; i++) { vec sum(k + 10); for (ll j = 0; j <= k; j++) { (sum[j + 1] = sum[j] + dp[i][j]) %= mod; } for (ll j = 0; j <= k; j++) { dp[i + 1][j] = (sum[j + 1] - sum[max(0LL, j - a[i])] + mod) % mod; } } cout << dp[n][k] << endl; }
replace
19
20
19
20
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define all(x) x.begin(), x.end() #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second #define mp make_pair #define pb push_back #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) template <class T> inline int chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline int chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = 1 << 30; const ll LINF = 1LL << 58; const int mod = 1000000007; ll dp[110][101010]; int main() { int n, k; cin >> n >> k; int A[110]; for (int i = 0; i < n; i++) cin >> A[i]; int a = 0; vector<ll> S(k + 1, 0LL); dp[0][0] = 1LL; for (int i = 0; i < n; i++) { a += A[i]; for (int j = 0; j <= min(k, a); j++) { S[j + 1] = (S[j] + dp[i][j]) % mod; } for (int j = 0; j <= min(k, a); j++) { dp[i + 1][j] = S[j + 1] - S[max(0, j - A[i])]; dp[i + 1][j] %= mod; if (dp[i + 1][j] < 0) dp[i + 1][j] += mod; } } cout << dp[n][k] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define all(x) x.begin(), x.end() #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second #define mp make_pair #define pb push_back #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) template <class T> inline int chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline int chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = 1 << 30; const ll LINF = 1LL << 58; const int mod = 1000000007; ll dp[110][101010]; int main() { int n, k; cin >> n >> k; int A[110]; for (int i = 0; i < n; i++) cin >> A[i]; int a = 0; vector<ll> S(k + 2, 0LL); dp[0][0] = 1LL; for (int i = 0; i < n; i++) { a += A[i]; for (int j = 0; j <= min(k, a); j++) { S[j + 1] = (S[j] + dp[i][j]) % mod; } for (int j = 0; j <= min(k, a); j++) { dp[i + 1][j] = S[j + 1] - S[max(0, j - A[i])]; dp[i + 1][j] %= mod; if (dp[i + 1][j] < 0) dp[i + 1][j] += mod; } } cout << dp[n][k] << endl; }
replace
39
40
39
40
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i, n) for (auto &i : n) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } void init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } #ifdef DEBUG template <class T, class N> void verr(const T &a, const N &n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } template <class H, class... T> void err(H &&h, T &&...t) { cerr << h << (sizeof...(t) ? " " : "\n") << flush; if (sizeof...(t) > 0) err(forward<T>(t)...); } #endif const ll INF = 5e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// ll A[100]; ll dp[101][100005]; int main() { init(); ll N, K; cin >> N >> K; rep(i, N) cin >> A[i]; ll sum = 0; dp[0][0] = 1; rep(i, N) { sum += A[i]; rep(j, sum)(dp[i][j + 1] += dp[i][j]) %= MOD; rep(j, sum + 1) { if (j - A[i] <= 0) dp[i + 1][j] = dp[i][j]; else (dp[i + 1][j] = dp[i][j] - dp[i][j - A[i] - 1] + MOD) %= MOD; } } cout << dp[N][K] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i, n) for (auto &i : n) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } void init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } #ifdef DEBUG template <class T, class N> void verr(const T &a, const N &n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } template <class H, class... T> void err(H &&h, T &&...t) { cerr << h << (sizeof...(t) ? " " : "\n") << flush; if (sizeof...(t) > 0) err(forward<T>(t)...); } #endif const ll INF = 5e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// ll A[100]; ll dp[101][100005]; int main() { init(); ll N, K; cin >> N >> K; rep(i, N) cin >> A[i]; ll sum = 0; dp[0][0] = 1; rep(i, N) { sum = min(sum + A[i], K); rep(j, sum)(dp[i][j + 1] += dp[i][j]) %= MOD; rep(j, sum + 1) { if (j - A[i] <= 0) dp[i + 1][j] = dp[i][j]; else (dp[i + 1][j] = dp[i][j] - dp[i][j - A[i] - 1] + MOD) %= MOD; } } cout << dp[N][K] << endl; }
replace
67
68
67
68
0
p03172
C++
Runtime Error
// Program.cpp // Author : Aaryan Srivastava #include <bits/stdc++.h> #include <chrono> #include <random> #define pb push_back #define popb pop_back #define ull unsigned long long #define beg begin #define mp make_pair #define pii pair<int, int> #define piii pair<pii, int> #define rep(i, n) for (int(i) = 0; i < (n); i++) #define repA(i, x, y) for (int i = (x); i <= (y); i++) #define repD(i, x, y) for (int i = (x); i >= (y); i--) #define all(c) (c).begin(), (c).end() #define ff first #define ss second #define fill(a, val) memset(a, val, sizeof(a)) #define Randomize \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) using ll = long long; #define int ll const int N = 2 * 1e5 + 5; const int mod = 1e9 + 7; const int inf = 1e18; const int SZ = 101; const double eps = 1e-9; using namespace std; #ifdef AaryanS #define mycout cout #define mycerr cerr #endif #ifndef AaryanS #define mycout \ if (false) \ cout #define mycerr \ if (false) \ cerr #define AaryanS 0 #endif int dp[100][2 * 100 * 1000 + 5] = {}; void solve() { int n, k; scanf("%lld%lld", &n, &k); vector<int> a(n); for (auto &it : a) scanf("%lld", &it); dp[0][0] = 1; for (int i = 0; i < n; ++i) { for (int cur = 0; cur <= k; ++cur) { dp[i + 1][cur] += dp[i][cur]; dp[i + 1][cur] %= mod; dp[i + 1][cur + a[i] + 1] -= dp[i][cur]; dp[i + 1][cur + a[i] + 1] %= mod; if (dp[i + 1][cur + a[i] + 1] < 0) dp[i + 1][cur + a[i] + 1] += mod; } for (int cur = 1; cur <= k; ++cur) dp[i + 1][cur] += dp[i + 1][cur - 1], dp[i + 1][cur] %= mod; } cout << dp[n][k]; } int32_t main(int32_t argc, char *argv[]) { double t1 = clock(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TC = 1, t = 0; // cin >> TC ; while (t++ < TC) { // cout << "Case " << t << ":\n" ; solve(); } mycerr << "Time : " << 1000 * (clock() - t1) / CLOCKS_PER_SEC << " ms\n"; return 0; }
// Program.cpp // Author : Aaryan Srivastava #include <bits/stdc++.h> #include <chrono> #include <random> #define pb push_back #define popb pop_back #define ull unsigned long long #define beg begin #define mp make_pair #define pii pair<int, int> #define piii pair<pii, int> #define rep(i, n) for (int(i) = 0; i < (n); i++) #define repA(i, x, y) for (int i = (x); i <= (y); i++) #define repD(i, x, y) for (int i = (x); i >= (y); i--) #define all(c) (c).begin(), (c).end() #define ff first #define ss second #define fill(a, val) memset(a, val, sizeof(a)) #define Randomize \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) using ll = long long; #define int ll const int N = 2 * 1e5 + 5; const int mod = 1e9 + 7; const int inf = 1e18; const int SZ = 101; const double eps = 1e-9; using namespace std; #ifdef AaryanS #define mycout cout #define mycerr cerr #endif #ifndef AaryanS #define mycout \ if (false) \ cout #define mycerr \ if (false) \ cerr #define AaryanS 0 #endif int dp[101][2 * 100 * 1000 + 5] = {}; void solve() { int n, k; scanf("%lld%lld", &n, &k); vector<int> a(n); for (auto &it : a) scanf("%lld", &it); dp[0][0] = 1; for (int i = 0; i < n; ++i) { for (int cur = 0; cur <= k; ++cur) { dp[i + 1][cur] += dp[i][cur]; dp[i + 1][cur] %= mod; dp[i + 1][cur + a[i] + 1] -= dp[i][cur]; dp[i + 1][cur + a[i] + 1] %= mod; if (dp[i + 1][cur + a[i] + 1] < 0) dp[i + 1][cur + a[i] + 1] += mod; } for (int cur = 1; cur <= k; ++cur) dp[i + 1][cur] += dp[i + 1][cur - 1], dp[i + 1][cur] %= mod; } cout << dp[n][k]; } int32_t main(int32_t argc, char *argv[]) { double t1 = clock(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TC = 1, t = 0; // cin >> TC ; while (t++ < TC) { // cout << "Case " << t << ":\n" ; solve(); } mycerr << "Time : " << 1000 * (clock() - t1) / CLOCKS_PER_SEC << " ms\n"; return 0; }
replace
44
45
44
45
-11
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<long long> dp(k + 1); dp[0] = 1; for (int i = 0; i < n; i++) { int x; cin >> x; vector<long long> dp2(k + 1); for (int j = k - 1; j >= 0; j--) { dp2[j + 1] = (dp2[j + 1] + dp[j]) % mod; if (j + x + 1 <= k) dp2[j + x + 1] += -dp[j]; if (dp2[j + x + 1] < 0) dp2[j + x + 1] += mod; dp2[j + x + 1] %= mod; } long long z{}; for (int j = 0; j <= k; j++) { z = (z + dp2[j]) % mod; dp[j] = (dp[j] + z) % mod; } } cout << dp[k]; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<long long> dp(k + 1); dp[0] = 1; for (int i = 0; i < n; i++) { int x; cin >> x; vector<long long> dp2(k + 1); for (int j = k - 1; j >= 0; j--) { dp2[j + 1] = (dp2[j + 1] + dp[j]) % mod; if (j + x + 1 <= k) dp2[j + x + 1] = ((dp2[j + x + 1] - dp[j]) % mod + mod) % mod; } long long z{}; for (int j = 0; j <= k; j++) { z = (z + dp2[j]) % mod; dp[j] = (dp[j] + z) % mod; } } cout << dp[k]; }
replace
21
25
21
22
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MODYYQ = 1e9 + 7; int n, k, a[105], dp[100][100005]; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 1; i <= n; i++) { int sum = 0; for (int j = 0; j <= k; j++) { sum += dp[i - 1][j]; if (j - a[i] - 1 >= 0) sum -= dp[i - 1][j - a[i] - 1]; sum = (sum % MODYYQ + MODYYQ) % MODYYQ; dp[i][j] = sum; } } cout << dp[n][k] << endl; }
#include <bits/stdc++.h> using namespace std; const int MODYYQ = 1e9 + 7; int n, k, a[105], dp[105][100005]; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 1; i <= n; i++) { int sum = 0; for (int j = 0; j <= k; j++) { sum += dp[i - 1][j]; if (j - a[i] - 1 >= 0) sum -= dp[i - 1][j - a[i] - 1]; sum = (sum % MODYYQ + MODYYQ) % MODYYQ; dp[i][j] = sum; } } cout << dp[n][k] << endl; }
replace
3
4
3
4
0
p03172
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #define FAST_IO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; typedef long long ll; typedef pair<int, int> pi; const int MOD = 1000000007; int dp[100001]; void add_self(int &a, int b) { a += b; if (a >= MOD) { a -= MOD; } } void sub_self(int &a, int b) { a -= b; if (a < 0) { a += MOD; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FAST_IO; int n, k, sum = 0; cin >> n >> k; vector<int> a(n + 1); dp[0] = 1; int up_to; for (int child = 1; child <= n; ++child) { cin >> up_to; vector<int> fake(k + 1); for (int used = k; used >= 0; --used) { int tmp = dp[used]; int L = used + 1; int R = used + min(up_to, k - used); if (L <= R) { add_self(fake[L], tmp); if (R + 1 <= k) sub_self(fake[R + 1], tmp); } } int prefix_sum = 0; for (int i = 0; i <= k; ++i) { add_self(prefix_sum, fake[i]); add_self(dp[i], prefix_sum); } } cout << dp[k]; return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #define FAST_IO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; typedef long long ll; typedef pair<int, int> pi; const int MOD = 1000000007; int dp[100001]; void add_self(int &a, int b) { a += b; if (a >= MOD) { a -= MOD; } } void sub_self(int &a, int b) { a -= b; if (a < 0) { a += MOD; } } int main() { FAST_IO; int n, k, sum = 0; cin >> n >> k; vector<int> a(n + 1); dp[0] = 1; int up_to; for (int child = 1; child <= n; ++child) { cin >> up_to; vector<int> fake(k + 1); for (int used = k; used >= 0; --used) { int tmp = dp[used]; int L = used + 1; int R = used + min(up_to, k - used); if (L <= R) { add_self(fake[L], tmp); if (R + 1 <= k) sub_self(fake[R + 1], tmp); } } int prefix_sum = 0; for (int i = 0; i <= k; ++i) { add_self(prefix_sum, fake[i]); add_self(dp[i], prefix_sum); } } cout << dp[k]; return 0; }
delete
32
36
32
32
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 1e2 + 10, MD = 1e9 + 7; int n, k, a[MAXN]; ll dp[MAXN][MAXN << 2]; int main() { ios::sync_with_stdio(0), cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; dp[0][0] = 1; for (int i = 1; i <= n; ++i) { ll ans = 0; for (int j = 0; j <= k; ++j) { ans += dp[i - 1][j]; if (j - a[i] - 1 >= 0) ans -= dp[i - 1][j - a[i] - 1]; ans = (ans + MD) % MD; dp[i][j] = ans; } } cout << dp[n][k] << '\n'; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 1e5 + 10, MD = 1e9 + 7; int n, k, a[110]; ll dp[110][MAXN]; int main() { ios::sync_with_stdio(0), cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; dp[0][0] = 1; for (int i = 1; i <= n; ++i) { ll ans = 0; for (int j = 0; j <= k; ++j) { ans += dp[i - 1][j]; if (j - a[i] - 1 >= 0) ans -= dp[i - 1][j - a[i] - 1]; ans = (ans + MD) % MD; dp[i][j] = ans; } } cout << dp[n][k] << '\n'; return 0; }
replace
5
8
5
8
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> #ifdef __LOCAL #define DBG(X) cout << #X << " = " << (X) << endl; #define SAY(X) cout << (X) << endl; #else #define DBG(X) #define SAY(X) #endif #ifdef __LOCAL #include <filesystem> namespace fs = std::filesystem; #endif using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T, typename S> inline ostream &operator<<(ostream &os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T, typename S> inline ostream &operator<<(ostream &os, const map<T, S> p) { for (auto el : p) os << "[" << el.first << " " << el.second << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { for (auto el : v) os << el << " "; return os; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto &elem : ret) cin >> elem; return ret; } template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; ll const mod = 1e9 + 7; using modint = ModInt<mod>; int N, K; vector<ll> A; void input() { fast_io(); #ifdef __LOCAL fs::path p = __FILE__; fs::path input, output; input = output = p.parent_path(); input += string("/input/") + string(p.stem()) + string(".txt"); output += string("/output/") + string(p.stem()) + string(".txt"); freopen(input.c_str(), "r", stdin); freopen(output.c_str(), "w", stdout); #endif cin >> N >> K; for (int i = 0; i < N; i++) { ll a; cin >> a; A.push_back(a); } } int solve() { // modint dp[105][100010]; vector<vector<modint>> dp(N + 1, vector<modint>(K, 0)); // memset(dp,0,sizeof(dp)); dp[0][0] = 1; for (int i = 1; i <= N; i++) { for (int j = 0; j <= K; j++) { dp[i][j] += dp[i - 1][j]; if (j) dp[i][j] += dp[i][j - 1]; if (j - A[i - 1] - 1 >= 0) dp[i][j] -= dp[i - 1][j - A[i - 1] - 1]; } } cout << dp[N][K] << endl; return 0; } int main() { input(); solve(); return 0; }
#include <bits/stdc++.h> #ifdef __LOCAL #define DBG(X) cout << #X << " = " << (X) << endl; #define SAY(X) cout << (X) << endl; #else #define DBG(X) #define SAY(X) #endif #ifdef __LOCAL #include <filesystem> namespace fs = std::filesystem; #endif using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T, typename S> inline ostream &operator<<(ostream &os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T, typename S> inline ostream &operator<<(ostream &os, const map<T, S> p) { for (auto el : p) os << "[" << el.first << " " << el.second << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { for (auto el : v) os << el << " "; return os; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto &elem : ret) cin >> elem; return ret; } template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; ll const mod = 1e9 + 7; using modint = ModInt<mod>; int N, K; vector<ll> A; void input() { fast_io(); #ifdef __LOCAL fs::path p = __FILE__; fs::path input, output; input = output = p.parent_path(); input += string("/input/") + string(p.stem()) + string(".txt"); output += string("/output/") + string(p.stem()) + string(".txt"); freopen(input.c_str(), "r", stdin); freopen(output.c_str(), "w", stdout); #endif cin >> N >> K; for (int i = 0; i < N; i++) { ll a; cin >> a; A.push_back(a); } } int solve() { // modint dp[105][100010]; vector<vector<modint>> dp(N + 10, vector<modint>(K + 10, 0)); // memset(dp,0,sizeof(dp)); dp[0][0] = 1; for (int i = 1; i <= N; i++) { for (int j = 0; j <= K; j++) { dp[i][j] += dp[i - 1][j]; if (j) dp[i][j] += dp[i][j - 1]; if (j - A[i - 1] - 1 >= 0) dp[i][j] -= dp[i - 1][j - A[i - 1] - 1]; } } cout << dp[N][K] << endl; return 0; } int main() { input(); solve(); return 0; }
replace
154
155
154
155
0
p03172
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <climits> #include <iostream> #include <map> #include <math.h> #include <stack> #include <string.h> #include <unordered_map> #include <vector> using namespace std; const int INF = 1e9; #define ll long long #define int ll #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define testcase \ int t; \ cin >> t; \ while (t--) #define pb push_back #define endl "\n" #define deb1(x) cout << #x << ": " << x << endl #define deb2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl #define deb3(x, y, z) \ cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl #define deb4(x, y, z, w) \ cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << " | " << #w << ": " << w << endl #define deb5(a, b, c, d, e) \ cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(a) int((a).size()) #define pii pair<int, int> #define fi first #define se second #define ld long double #define vii vector<int> #define all(v) v.begin(), v.end() #define prec(n) fixed << setprecision(n) const int MOD = (int)1e9 + 7; const int MOD2 = 1007681537; const ll LINF = (ll)1e18; const ld PI = acos((ld)-1); const ld EPS = 1e-12; inline ll gcd(ll a, ll b) { ll r; while (b) { r = a % b; a = b; b = r; } return a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } inline ll fpow(ll n, ll k, int p = MOD) { ll r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T &a, const T &val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T &a, const T &val) { return a < val ? a = val, 1 : 0; } inline void addmod(int &a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int &a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (ll)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } template <class T> inline void get_arr(T arr[], int n) { for (int i = 0; i < n; i++) cin >> arr[i]; } template <class T> inline void print_arr(T arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } int32_t main() { int n, k; cin >> n >> k; vector<int> dp(n + 1); dp[0] = 1; for (int child = 0; child < n; child++) { int upto; cin >> upto; vii fake(k + 1); for (int used = k; used >= 0; used--) { int tmp = dp[used]; int L = used + 1; int R = used + min(upto, k - used); if (L <= R) { addmod(fake[L], tmp); if (R + 1 <= k) { submod(fake[R + 1], tmp); } } } int presum = 0; for (int i = 0; i <= k; i++) { addmod(presum, fake[i]); addmod(dp[i], presum); } } cout << dp[k] << endl; }
#include <algorithm> #include <bits/stdc++.h> #include <climits> #include <iostream> #include <map> #include <math.h> #include <stack> #include <string.h> #include <unordered_map> #include <vector> using namespace std; const int INF = 1e9; #define ll long long #define int ll #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define testcase \ int t; \ cin >> t; \ while (t--) #define pb push_back #define endl "\n" #define deb1(x) cout << #x << ": " << x << endl #define deb2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl #define deb3(x, y, z) \ cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl #define deb4(x, y, z, w) \ cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << " | " << #w << ": " << w << endl #define deb5(a, b, c, d, e) \ cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(a) int((a).size()) #define pii pair<int, int> #define fi first #define se second #define ld long double #define vii vector<int> #define all(v) v.begin(), v.end() #define prec(n) fixed << setprecision(n) const int MOD = (int)1e9 + 7; const int MOD2 = 1007681537; const ll LINF = (ll)1e18; const ld PI = acos((ld)-1); const ld EPS = 1e-12; inline ll gcd(ll a, ll b) { ll r; while (b) { r = a % b; a = b; b = r; } return a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } inline ll fpow(ll n, ll k, int p = MOD) { ll r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T &a, const T &val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T &a, const T &val) { return a < val ? a = val, 1 : 0; } inline void addmod(int &a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int &a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (ll)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } template <class T> inline void get_arr(T arr[], int n) { for (int i = 0; i < n; i++) cin >> arr[i]; } template <class T> inline void print_arr(T arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } int32_t main() { int n, k; cin >> n >> k; vector<int> dp(k + 1); dp[0] = 1; for (int child = 0; child < n; child++) { int upto; cin >> upto; vii fake(k + 1); for (int used = k; used >= 0; used--) { int tmp = dp[used]; int L = used + 1; int R = used + min(upto, k - used); if (L <= R) { addmod(fake[L], tmp); if (R + 1 <= k) { submod(fake[R + 1], tmp); } } } int presum = 0; for (int i = 0; i <= k; i++) { addmod(presum, fake[i]); addmod(dp[i], presum); } } cout << dp[k] << endl; }
replace
98
99
98
99
0
p03172
C++
Runtime Error
#include <algorithm> #include <cassert> #include <deque> #include <iostream> #include <map> #include <vector> static const int IINF = 1 << 30; static const long long LINF = 1LL << 60; static const long long MOD = 1.0e+9 + 7; template <typename T> std::vector<T> vectors(std::size_t n, T val) { return std::vector<T>(n, val); } template <typename T, typename... Args> auto vectors(std::size_t n, Args... args) { return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...)); } template <class T> inline bool chmin(T &a, const T &b) { return (a > b) ? a = b, true : false; } template <class T> inline bool chmax(T &a, const T &b) { return (a < b) ? a = b, true : false; } template <class T> inline void chadd(T &a, const T &b) { a += b, a %= MOD; } int main() { // Input int numChild; int numCandy; std::cin >> numChild >> numCandy; assert(1 <= numChild and numChild <= 100); assert(0 <= numCandy and numCandy <= 1.0e+5); std::vector<long long> upper(1 + numChild, -1); for (int i = 1; i <= numChild; ++i) { std::cin >> upper[i]; assert(0 <= upper[i] and upper[i] <= numCandy); } // Initialization std::vector<std::vector<long long>> dp(1 + numChild); for (int i = 1; i <= numChild; ++i) dp[i].assign(1 + numCandy, 0LL); // Initial condition for (int k = 0; k <= upper[1]; ++k) dp[1][k] = 1; // DP main for (int i = 1; i < numChild; ++i) { long long currentSum = 0LL; std::deque<long long> dque; for (int k = 0; k <= numCandy; ++k) { if (k <= upper[i + 1]) { dque.push_back(dp[i][k]); chadd(currentSum, dque.back()); dp[i + 1][k] = currentSum; } else { assert(dque.size() == (size_t)upper[i + 1] + 1); dque.push_back(dp[i][k]); chadd(currentSum, dque.back()); chadd(currentSum, -dque.front()); assert(currentSum >= 0); dque.pop_front(); dp[i + 1][k] = currentSum; } } } // Output // long long res = 0LL; // for (int k = 0; k <= upper[numChild]; ++k) { // chadd(res, dp[numChild][k]); // } // std::cout << res << std::endl; std::cout << dp[numChild][numCandy] << std::endl; return 0; }
#include <algorithm> #include <cassert> #include <deque> #include <iostream> #include <map> #include <vector> static const int IINF = 1 << 30; static const long long LINF = 1LL << 60; static const long long MOD = 1.0e+9 + 7; template <typename T> std::vector<T> vectors(std::size_t n, T val) { return std::vector<T>(n, val); } template <typename T, typename... Args> auto vectors(std::size_t n, Args... args) { return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...)); } template <class T> inline bool chmin(T &a, const T &b) { return (a > b) ? a = b, true : false; } template <class T> inline bool chmax(T &a, const T &b) { return (a < b) ? a = b, true : false; } template <class T> inline void chadd(T &a, const T &b) { a += b, a %= MOD; if (a < 0) a += MOD; } int main() { // Input int numChild; int numCandy; std::cin >> numChild >> numCandy; assert(1 <= numChild and numChild <= 100); assert(0 <= numCandy and numCandy <= 1.0e+5); std::vector<long long> upper(1 + numChild, -1); for (int i = 1; i <= numChild; ++i) { std::cin >> upper[i]; assert(0 <= upper[i] and upper[i] <= numCandy); } // Initialization std::vector<std::vector<long long>> dp(1 + numChild); for (int i = 1; i <= numChild; ++i) dp[i].assign(1 + numCandy, 0LL); // Initial condition for (int k = 0; k <= upper[1]; ++k) dp[1][k] = 1; // DP main for (int i = 1; i < numChild; ++i) { long long currentSum = 0LL; std::deque<long long> dque; for (int k = 0; k <= numCandy; ++k) { if (k <= upper[i + 1]) { dque.push_back(dp[i][k]); chadd(currentSum, dque.back()); dp[i + 1][k] = currentSum; } else { assert(dque.size() == (size_t)upper[i + 1] + 1); dque.push_back(dp[i][k]); chadd(currentSum, dque.back()); chadd(currentSum, -dque.front()); assert(currentSum >= 0); dque.pop_front(); dp[i + 1][k] = currentSum; } } } // Output // long long res = 0LL; // for (int k = 0; k <= upper[numChild]; ++k) { // chadd(res, dp[numChild][k]); // } // std::cout << res << std::endl; std::cout << dp[numChild][numCandy] << std::endl; return 0; }
replace
28
29
28
33
0
p03172
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long int #define hell 1000000007 #define vi vector<ll> #define pb push_back #define pii pair<ll, ll> #define vii vector<pii> #define rep(i, a, b) for (ll i = a; i < b; i++) #define sep(i, a, b) for (ll i = a - 1; i >= b; i--) #define inf (ll)(1e16) using namespace std; ll helper(vi &a, ll st, ll k, vector<vi> &dp) { if (st == a.size() - 1 && k <= a[st]) return 1; if (st >= a.size()) return 0; if (dp[st][k] != -1) return dp[st][k]; ll ans = 0; rep(i, 0, a[st] + 1) { if (i <= k) ans = (ans + helper(a, st + 1, k - i, dp)) % hell; else break; } return dp[st][k] = ans; } void solve() { ll n, k; cin >> n >> k; vi a(n); rep(i, 0, n) cin >> a[i]; vector<vi> dp(n + 1, vi(k + 1, -1)); cout << helper(a, 0, k, dp); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t = 1; // cin>>t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> #define ll long long int #define hell 1000000007 #define vi vector<ll> #define pb push_back #define pii pair<ll, ll> #define vii vector<pii> #define rep(i, a, b) for (ll i = a; i < b; i++) #define sep(i, a, b) for (ll i = a - 1; i >= b; i--) #define inf (ll)(1e16) using namespace std; ll helper(vi &a, ll st, ll k, vector<vi> &dp) { if (st == a.size() - 1 && k <= a[st]) return 1; if (st >= a.size()) return 0; if (dp[st][k] != -1) return dp[st][k]; ll ans = 0; rep(i, 0, a[st] + 1) { if (i <= k) ans = (ans + helper(a, st + 1, k - i, dp)) % hell; else break; } return dp[st][k] = ans; } void solve() { ll n, k; cin >> n >> k; vi a(n); rep(i, 0, n) cin >> a[i]; vector<vi> dp(n + 1, vi(k + 1, 0)); rep(i, 0, n + 1) { ll p = 0; rep(j, 0, k + 1) { if (i >= 1) p = (p + dp[i - 1][j]) % hell; if (i == 0 && j == 0) dp[i][j] = 1; else if (i == 0) dp[i][j] = 0; else if (j == 0) dp[i][j] = 1; else dp[i][j] = p; if (i > 0 && j >= a[i - 1]) p = (p - dp[i - 1][j - a[i - 1]] + hell) % hell; // cout<<dp[i][j]<<" "; } // cout<<"\n"; } cout << dp[n][k]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t = 1; // cin>>t; while (t--) solve(); return 0; }
replace
36
38
36
57
TLE
p03172
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; 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)とキャストしよう ごちゃごちゃ場合分けを考える前に全探索は考えましたか? */ 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; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } 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; }; /* <--------------------------------------------> */ typedef tuple<ll, ll, ll> T; // get<K>(tuple型の変数) int main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<ll> a(n + 1); for (int i = 1; i <= n; ++i) cin >> a[i]; vector<vector<ll>> dp(110, vector<ll>(100005, 0)); vector<ll> cum(100005, 0); dp[0][0] = 1; for (int i = 1; i <= n; ++i) { cum[0] = 0; for (int j = 1; j <= k + 1; ++j) cum[j] = (cum[j - 1] + dp[i - 1][j - 1]) % mod; // cum[j] = dp[i-1][0]+...+dp[i-1][j-1] for (int j = 0; j <= k; ++k) dp[i][j] = (cum[j + 1] - cum[max(0LL, j - a[i])] + mod) % mod; } cout << dp[n][k] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; 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)とキャストしよう ごちゃごちゃ場合分けを考える前に全探索は考えましたか? */ 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; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } 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; }; /* <--------------------------------------------> */ typedef tuple<ll, ll, ll> T; // get<K>(tuple型の変数) int main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<ll> a(n + 1); for (int i = 1; i <= n; ++i) cin >> a[i]; vector<vector<ll>> dp(110, vector<ll>(100005, 0)); vector<ll> cum(100005, 0); dp[0][0] = 1; for (int i = 1; i <= n; ++i) { cum[0] = 0; for (int j = 1; j <= k + 1; ++j) cum[j] = (cum[j - 1] + dp[i - 1][j - 1]) % mod; // cum[j] = dp[i-1][0]+...+dp[i-1][j-1] for (int j = 0; j <= k; ++j) dp[i][j] = (cum[j + 1] - cum[max(0LL, j - a[i])] + mod) % mod; } cout << dp[n][k] << endl; return 0; }
replace
85
86
85
86
TLE
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define ff first #define ss second #define mp make_pair #define pb push_back #define endl "\n" #define si set<int> #define vi vector<int> #define pii pair<int, int> #define vpii vector<pii> #define mii map<int, int> #define que_max priority_queue<int> #define que_min priority_queue<int, vi, greater<int>> #define loop(i, s, e) for (int i = s; i < e; i++) #define rloop(i, e, s) for (int i = e; i >= s; i--) #define mset(a, f) memset(a, f, sizeof(a)) #define M 1000000007 #define sz(x) ((int)x.size()) #define all(p) p.begin(), p.end() #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__) #define print(a) \ for (auto x : a) \ cout << x << " "; \ cout << endl #define Print(a, x, y) \ for (int i = x; i < y; i++) \ cout << a[i] << " "; \ cout << endl inline int power(int a, int b) { int x = 1; while (b) { if (b & 1) x *= a; a *= a; b >>= 1; } return x; } template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int N = 3e5 + 7; vector<vi> adj; bool vis[N]; int arr[105]; int dp[105][100005]; void solve() { int n, k; cin >> n >> k; loop(i, 1, n + 1) cin >> arr[i]; dp[0][0] = 1; loop(i, 1, n + 1) { loop(j, 0, k + 1) { int ans = 0; if (i - 1 >= 0) ans += dp[(i - 1)][j]; if (j - 1 >= 0) ans += dp[i][j - 1], ans %= M; if (j - arr[i] >= 0) { ans -= dp[(i - 1)][j - 1 - arr[i]]; ans %= M; } if (ans < 0) ans += M; dp[i][j] = ans; } } cout << dp[n][k] << endl; return; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cout << setprecision(9) << fixed; int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define ff first #define ss second #define mp make_pair #define pb push_back #define endl "\n" #define si set<int> #define vi vector<int> #define pii pair<int, int> #define vpii vector<pii> #define mii map<int, int> #define que_max priority_queue<int> #define que_min priority_queue<int, vi, greater<int>> #define loop(i, s, e) for (int i = s; i < e; i++) #define rloop(i, e, s) for (int i = e; i >= s; i--) #define mset(a, f) memset(a, f, sizeof(a)) #define M 1000000007 #define sz(x) ((int)x.size()) #define all(p) p.begin(), p.end() #define bug(...) __f(#__VA_ARGS__, __VA_ARGS__) #define print(a) \ for (auto x : a) \ cout << x << " "; \ cout << endl #define Print(a, x, y) \ for (int i = x; i < y; i++) \ cout << a[i] << " "; \ cout << endl inline int power(int a, int b) { int x = 1; while (b) { if (b & 1) x *= a; a *= a; b >>= 1; } return x; } template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int N = 3e5 + 7; vector<vi> adj; bool vis[N]; int arr[105]; int dp[105][100005]; void solve() { int n, k; cin >> n >> k; loop(i, 1, n + 1) cin >> arr[i]; dp[0][0] = 1; loop(i, 1, n + 1) { loop(j, 0, k + 1) { int ans = 0; if (i - 1 >= 0) ans += dp[(i - 1)][j]; if (j - 1 >= 0) ans += dp[i][j - 1], ans %= M; if (j - arr[i] >= 0) { ans -= dp[(i - 1)][j - 1 - arr[i]]; ans %= M; } if (ans < 0) ans += M; dp[i][j] = ans; } } cout << dp[n][k] << endl; return; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(9) << fixed; int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
delete
96
100
96
96
TLE
p03172
C++
Time Limit Exceeded
# 1 "m.cpp" # 1 "<built-in>" # 1 "<command-line>" # 1 "m.cpp" # 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 1 # 12 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" # 1 "/home/tysm/dev/cpplib/bin/../stdlib/bits/stdc++.h" 1 #include <bits/stdc++.h> # 13 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2 # 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/assoc_container.hpp" 1 #include <ext/pb_ds/assoc_container.hpp> # 14 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2 # 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/tree_policy.hpp" 1 #include <ext/pb_ds/tree_policy.hpp> # 15 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2 using namespace std; using namespace __gnu_pbds; using ii = pair<long long, long long>; using vi = vector<long long>; using vd = vector<long double>; using vb = vector<bool>; using vii = vector<ii>; using vvi = vector<vi>; using vvd = vector<vd>; using vvb = vector<vb>; using vvii = vector<vii>; # 50 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" inline long long mod(const long long n, const long long m = ((long long)1e9 + 7)) { return (n % m + m) % m; } /** * Euclidean GCD. * * Time Complexity: O(log(min(a, b))). * Space Complexity: O(log(min(a, b))). */ long long gcd(const long long a, const long long b) { if (a < 0 or b < 0) return gcd(abs(a), abs(b)); if (a == 0) return b; return gcd(b % a, a); } /** * Euclidean derivated LCM. * * Time Complexity: O(log(min(a, b))). * Space Complexity: O(log(min(a, b))). */ long long lcm(const long long a, const long long b) { // same as a*b/gcd(a, b) but avoiding overflow. return a / gcd(a, b) * b; } # 2 "m.cpp" 2 # 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/data-structure/tree/segment-tree.hpp" 1 /** * Segment Tree Node. * * Helps to hold range data dynamically so * that only a small number of nodes are * visited per query. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <typename T> struct STNodeB { T value, lazy = 0; STNodeB() { // meant to create a node with default value. } STNodeB(const T value) : value(value) { // meant to initialize a node with a defined value. } STNodeB(const STNodeB &lhs, const STNodeB &rhs) { // meant to create a node by merging lhs and rhs. } /** * Checks if a value matches with * this->value according to the * node implementation. * * Time Complexity: O(1). * Space Complexity: O(1). */ virtual bool match(const T value) const = 0; /** * Updates the current node according to * the node implementation. * * Time Complexity: O(1). * Space Complexity: O(1). */ virtual void update(const size_t range) = 0; }; /** * Segment Tree. * * Arranges data in a binary tree so that * it is possible to perform range queries * and modifications over an array * effectively. * * Note: when the tree is constructed by * inserting each value one by one using * update the time complexity is actually * O(n*log(n)). * * Time Complexity: O(n). * Space Complexity: O(n). * Where n is the size of the array. */ template <typename Node, typename T> class SegTreeB { public: SegTreeB() = delete; SegTreeB(const size_t arr_size) : tree(4 * arr_size), arr_size(arr_size) {} SegTreeB(const vector<T> &arr) : SegTreeB(arr.size(), arr) {} SegTreeB(const size_t arr_size, const vector<T> &arr) : tree(4 * arr_size), arr_size(arr_size) { build(0, arr_size - 1, 0, arr); } SegTreeB(const SegTreeB &lhs, const SegTreeB &rhs) : SegTreeB(lhs.arr_size) { assert(lhs.arr_size == rhs.arr_size); for (size_t i = 0; i < 4 * arr_size; ++i) tree[i] = Node(lhs.tree[i], rhs.tree[i]); } /** * Finds the index of the first element * over the array that matches with value. * * It returns the size of the array when * the value doesn't match with any array * value. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ size_t find(const T value) { return find(0, arr_size - 1, 0, value); } /** * Computes the ith array value. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ T query(const size_t i) { assert(i < arr_size); return query(i, i); } /** * Computes the value corresponding to the * range [l, r] of the array. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ T query(const size_t l, const size_t r) { assert(l <= r and r < arr_size); return query(0, arr_size - 1, l, r, 0).value; } /** * Updates the ith array value according to * the SegmentTreeKind. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ void update(const size_t i, const T delta) { assert(i < arr_size); update(i, i, delta); } /** * Updates the array values in the range * [l, r] according to the SegmentTreeKind. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ void update(const size_t l, const size_t r, const T delta) { assert(l <= r and r < arr_size); update(0, arr_size - 1, l, r, 0, delta); } private: Node build(const size_t l, const size_t r, const size_t pos, const vector<T> &arr) { if (l == r) return tree[pos] = Node(arr[l]); size_t mid = (l + r) / 2; return tree[pos] = Node(build(l, mid, 2 * pos + 1, arr), build(mid + 1, r, 2 * pos + 2, arr)); } size_t find(const size_t l, const size_t r, const size_t pos, const T value) { propagate(l, r, pos); if (!tree[pos].match(value)) return arr_size; else if (l == r) return l; size_t mid = (l + r) / 2; size_t ans = find(l, mid, 2 * pos + 1, value); return ans != arr_size ? ans : find(mid + 1, r, 2 * pos + 2, value); } Node query(const size_t l, const size_t r, const size_t i, const size_t j, const size_t pos) { propagate(l, r, pos); if (l > j or r < i) return Node(); if (l >= i and r <= j) return tree[pos]; size_t mid = (l + r) / 2; return Node(query(l, mid, i, j, 2 * pos + 1), query(mid + 1, r, i, j, 2 * pos + 2)); } Node update(const size_t l, const size_t r, const size_t i, const size_t j, const size_t pos, const T delta) { propagate(l, r, pos); if (l > j or r < i) return tree[pos]; if (l >= i and r <= j) { tree[pos].lazy = delta; // it's important to propagate before returning and merge nodes. propagate(l, r, pos); return tree[pos]; } size_t mid = (l + r) / 2; return tree[pos] = Node(update(l, mid, i, j, 2 * pos + 1, delta), update(mid + 1, r, i, j, 2 * pos + 2, delta)); } virtual void propagate(const size_t l, const size_t r, const size_t pos) { T lazy = tree[pos].lazy; if (lazy == 0) return; tree[pos].update(r - l + 1); if (l != r) { tree[2 * pos + 1].lazy += lazy; tree[2 * pos + 2].lazy += lazy; } } protected: vector<Node> tree; size_t arr_size; }; /** * Segment Tree Node. * * Note: this implementation only adds * required variables to perform set * operations. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <typename T> struct STNodeSB : STNodeB<T> { using super_type = STNodeB<T>; pair<T, bool> set = {0, false}; STNodeSB() : super_type() {} STNodeSB(const T value) : super_type(value) {} STNodeSB(const STNodeSB &lhs, const STNodeSB &rhs) { // meant to create a node by merging lhs and rhs. } }; /** * Segment Tree. * * Note: this implementation only adds * (range and point) set operations * since it costs a bunch of extra time and * memory (mainly when many Segment Trees * are constructed, e.g.: Segment Tree 2D). * * Note: when the tree is constructed by * inserting each value one by one using * set and/or update, the time complexity * is actually O(n*log(n)). * * Time Complexity: O(n). * Space Complexity: O(n). * Where n is the size of the array. */ template <typename NodeS, typename T> class SegTreeSB : public SegTreeB<NodeS, T> { public: SegTreeSB() = delete; SegTreeSB(const size_t arr_size) : super_type(arr_size) {} SegTreeSB(const vector<T> &arr) : super_type(arr) {} SegTreeSB(const size_t arr_size, const vector<T> &arr) : super_type(arr_size, arr) {} SegTreeSB(const SegTreeSB &lhs, const SegTreeSB &rhs) : super_type(lhs, rhs) {} /** * Sets the ith array value to value. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ void set(const size_t i, const T value) { assert(i < this->arr_size); set(i, i, value); } /** * Sets the array values in the range * [l, r] to value. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ void set(const size_t l, const size_t r, const T value) { assert(l <= r and r < this->arr_size); set(0, this->arr_size - 1, l, r, 0, value); } private: using super_type = SegTreeB<NodeS, T>; NodeS set(const size_t l, const size_t r, const size_t i, const size_t j, const size_t pos, const T value) { propagate(l, r, pos); if (l > j or r < i) return this->tree[pos]; if (l >= i and r <= j) { this->tree[pos].set = {value, true}; // it's important to propagate before returning and merge nodes. propagate(l, r, pos); return this->tree[pos]; } size_t mid = (l + r) / 2; return this->tree[pos] = NodeS(set(l, mid, i, j, 2 * pos + 1, value), set(mid + 1, r, i, j, 2 * pos + 2, value)); } void propagate(const size_t l, const size_t r, const size_t pos) { T lazy = this->tree[pos].lazy; pair<T, bool> set = this->tree[pos].set; if (lazy == 0 and !set.second) return; this->tree[pos].update(r - l + 1); if (l != r) { if (set.second) { this->tree[2 * pos + 1].set = set; this->tree[2 * pos + 2].set = set; this->tree[2 * pos + 1].lazy = 0; this->tree[2 * pos + 2].lazy = 0; } this->tree[2 * pos + 1].lazy += lazy; this->tree[2 * pos + 2].lazy += lazy; } } }; enum class STKind { RMaxQ, RMinQ, RSumQ, RXorQ, }; const STKind RMaxQ = STKind::RMaxQ; const STKind RMinQ = STKind::RMinQ; const STKind RSumQ = STKind::RSumQ; const STKind RXorQ = STKind::RXorQ; /** * Returns the default value according to * the SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <typename T> T default_value(STKind k) { switch (k) { case RMaxQ: return -((long long)1e9 + 1); case RMinQ: return ((long long)1e9 + 1); case RSumQ: return 0; case RXorQ: return 0; default: assert(false); } } /** * Merges two values into a new one * according to the SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <typename T> T merge_values(STKind k, const T lhs, const T rhs) { switch (k) { case RMaxQ: return max(lhs, rhs); case RMinQ: return min(lhs, rhs); case RSumQ: return lhs + rhs; case RXorQ: return lhs ^ rhs; default: assert(false); } } /** * Segment Tree Node. * * Note: this is a node implementation * for common use cases such as: * - Range Max Query; * - Range Min Query; * - Range Sum Query; * - Range Xor Query. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <STKind K, typename T> struct STNode : STNodeB<T> { using super_type = STNodeB<T>; STNode() : super_type(default_value<T>(K)) {} STNode(const T value) : super_type(value) {} STNode(const STNode &lhs, const STNode &rhs) : super_type(merge_values<T>(K, lhs.value, rhs.value)) { assert(lhs.lazy == 0 and rhs.lazy == 0); } /** * Checks if a value matches with * this->value according to the * SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ bool match(const T value) const { switch (K) { case RMaxQ: return this->value >= value; // return this->value > value; case RMinQ: return this->value <= value; // return this->value < value; case RSumQ: case RXorQ: default: assert(false); } } /** * Updates the current node according to * the SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ void update(const size_t range) { switch (K) { case RMaxQ: case RMinQ: this->value += this->lazy; break; case RSumQ: this->value += range * this->lazy; break; case RXorQ: break; default: assert(false); } this->lazy = 0; } }; template <STKind K, typename T> using SegTree = SegTreeB<STNode<K, T>, T>; /** * Segment Tree Node. * * Note: this implementation only * adds set operations. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <STKind K, typename T> struct STNodeS : STNodeSB<T> { using super_type = STNodeSB<T>; STNodeS() : super_type(default_value<T>(K)) {} STNodeS(const T value) : super_type(value) {} STNodeS(const STNodeS &lhs, const STNodeS &rhs) : super_type(merge_values<T>(K, lhs.value, rhs.value)) { assert(lhs.lazy == 0 and rhs.lazy == 0); assert(!lhs.set.second and !rhs.set.second); } /** * Checks if a value matches with * this->value according to the * SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ bool match(const T value) const { switch (K) { case RMaxQ: return this->value >= value; // return this->value > value; case RMinQ: return this->value <= value; // return this->value < value; case RSumQ: case RXorQ: default: assert(false); } } /** * Updates the current node according to * the SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ void update(const size_t range) { if (this->set.second) { switch (K) { case RMaxQ: case RMinQ: this->value = this->set.first; break; case RSumQ: this->value = range * this->set.first; break; case RXorQ: this->value = (range % 2) * this->set.first; break; default: assert(false); } } switch (K) { case RMaxQ: case RMinQ: this->value += this->lazy; break; case RSumQ: this->value += range * this->lazy; break; case RXorQ: break; default: assert(false); } this->lazy = 0; this->set = {0, false}; } }; template <STKind K, typename T> using SegTreeS = SegTreeSB<STNodeS<K, T>, T>; # 3 "m.cpp" 2 struct Node : STNodeB<long long> { Node() : STNodeB<long long>(mod(default_value<long long>(RSumQ))) {} Node(const long long value) : STNodeB<long long>(mod(value)) {} Node(const Node &lhs, const Node &rhs) { this->value = mod(merge_values<long long>(RSumQ, lhs.value, rhs.value)); } bool match(const long long value) const { return true; }; void update(const size_t range) { this->value = mod(this->value + mod(range * this->lazy)); this->lazy = 0; } }; int32_t main() { (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)); long long n, k; cin >> n >> k; vi arr(n); for (long long &i : arr) cin >> i; reverse(arr.begin(), arr.end()); vvi dp(2, vi(k + 1)); dp[1][0] = 1; for (long long i : arr) { SegTreeB<Node, long long> st(dp[1]); fill(dp[0].begin(), dp[0].end(), 0); for (long long j = 0; j <= k; ++j) dp[0][j] = st.query(max((long long)0, j - i), j); swap(dp[0], dp[1]); } cout << dp[1][k] << "\n"; return 0; }
# 1 "m.cpp" # 1 "<built-in>" # 1 "<command-line>" # 1 "m.cpp" # 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 1 # 12 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" # 1 "/home/tysm/dev/cpplib/bin/../stdlib/bits/stdc++.h" 1 #include <bits/stdc++.h> # 13 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2 # 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/assoc_container.hpp" 1 #include <ext/pb_ds/assoc_container.hpp> # 14 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2 # 1 "/home/tysm/dev/cpplib/bin/../stdlib/ext/pb_ds/tree_policy.hpp" 1 #include <ext/pb_ds/tree_policy.hpp> # 15 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" 2 using namespace std; using namespace __gnu_pbds; using ii = pair<long long, long long>; using vi = vector<long long>; using vd = vector<long double>; using vb = vector<bool>; using vii = vector<ii>; using vvi = vector<vi>; using vvd = vector<vd>; using vvb = vector<vb>; using vvii = vector<vii>; # 50 "/home/tysm/dev/cpplib/bin/../include/cpplib/stdinc.hpp" inline long long mod(const long long n, const long long m = ((long long)1e9 + 7)) { return (n % m + m) % m; } /** * Euclidean GCD. * * Time Complexity: O(log(min(a, b))). * Space Complexity: O(log(min(a, b))). */ long long gcd(const long long a, const long long b) { if (a < 0 or b < 0) return gcd(abs(a), abs(b)); if (a == 0) return b; return gcd(b % a, a); } /** * Euclidean derivated LCM. * * Time Complexity: O(log(min(a, b))). * Space Complexity: O(log(min(a, b))). */ long long lcm(const long long a, const long long b) { // same as a*b/gcd(a, b) but avoiding overflow. return a / gcd(a, b) * b; } # 2 "m.cpp" 2 # 1 "/home/tysm/dev/cpplib/bin/../include/cpplib/data-structure/tree/segment-tree.hpp" 1 /** * Segment Tree Node. * * Helps to hold range data dynamically so * that only a small number of nodes are * visited per query. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <typename T> struct STNodeB { T value, lazy = 0; STNodeB() { // meant to create a node with default value. } STNodeB(const T value) : value(value) { // meant to initialize a node with a defined value. } STNodeB(const STNodeB &lhs, const STNodeB &rhs) { // meant to create a node by merging lhs and rhs. } /** * Checks if a value matches with * this->value according to the * node implementation. * * Time Complexity: O(1). * Space Complexity: O(1). */ virtual bool match(const T value) const = 0; /** * Updates the current node according to * the node implementation. * * Time Complexity: O(1). * Space Complexity: O(1). */ virtual void update(const size_t range) = 0; }; /** * Segment Tree. * * Arranges data in a binary tree so that * it is possible to perform range queries * and modifications over an array * effectively. * * Note: when the tree is constructed by * inserting each value one by one using * update the time complexity is actually * O(n*log(n)). * * Time Complexity: O(n). * Space Complexity: O(n). * Where n is the size of the array. */ template <typename Node, typename T> class SegTreeB { public: SegTreeB() = delete; SegTreeB(const size_t arr_size) : tree(4 * arr_size), arr_size(arr_size) {} SegTreeB(const vector<T> &arr) : SegTreeB(arr.size(), arr) {} SegTreeB(const size_t arr_size, const vector<T> &arr) : tree(4 * arr_size), arr_size(arr_size) { build(0, arr_size - 1, 0, arr); } SegTreeB(const SegTreeB &lhs, const SegTreeB &rhs) : SegTreeB(lhs.arr_size) { assert(lhs.arr_size == rhs.arr_size); for (size_t i = 0; i < 4 * arr_size; ++i) tree[i] = Node(lhs.tree[i], rhs.tree[i]); } /** * Finds the index of the first element * over the array that matches with value. * * It returns the size of the array when * the value doesn't match with any array * value. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ size_t find(const T value) { return find(0, arr_size - 1, 0, value); } /** * Computes the ith array value. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ T query(const size_t i) { assert(i < arr_size); return query(i, i); } /** * Computes the value corresponding to the * range [l, r] of the array. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ T query(const size_t l, const size_t r) { assert(l <= r and r < arr_size); return query(0, arr_size - 1, l, r, 0).value; } /** * Updates the ith array value according to * the SegmentTreeKind. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ void update(const size_t i, const T delta) { assert(i < arr_size); update(i, i, delta); } /** * Updates the array values in the range * [l, r] according to the SegmentTreeKind. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ void update(const size_t l, const size_t r, const T delta) { assert(l <= r and r < arr_size); update(0, arr_size - 1, l, r, 0, delta); } private: Node build(const size_t l, const size_t r, const size_t pos, const vector<T> &arr) { if (l == r) return tree[pos] = Node(arr[l]); size_t mid = (l + r) / 2; return tree[pos] = Node(build(l, mid, 2 * pos + 1, arr), build(mid + 1, r, 2 * pos + 2, arr)); } size_t find(const size_t l, const size_t r, const size_t pos, const T value) { propagate(l, r, pos); if (!tree[pos].match(value)) return arr_size; else if (l == r) return l; size_t mid = (l + r) / 2; size_t ans = find(l, mid, 2 * pos + 1, value); return ans != arr_size ? ans : find(mid + 1, r, 2 * pos + 2, value); } Node query(const size_t l, const size_t r, const size_t i, const size_t j, const size_t pos) { propagate(l, r, pos); if (l > j or r < i) return Node(); if (l >= i and r <= j) return tree[pos]; size_t mid = (l + r) / 2; return Node(query(l, mid, i, j, 2 * pos + 1), query(mid + 1, r, i, j, 2 * pos + 2)); } Node update(const size_t l, const size_t r, const size_t i, const size_t j, const size_t pos, const T delta) { propagate(l, r, pos); if (l > j or r < i) return tree[pos]; if (l >= i and r <= j) { tree[pos].lazy = delta; // it's important to propagate before returning and merge nodes. propagate(l, r, pos); return tree[pos]; } size_t mid = (l + r) / 2; return tree[pos] = Node(update(l, mid, i, j, 2 * pos + 1, delta), update(mid + 1, r, i, j, 2 * pos + 2, delta)); } virtual void propagate(const size_t l, const size_t r, const size_t pos) { T lazy = tree[pos].lazy; if (lazy == 0) return; tree[pos].update(r - l + 1); if (l != r) { tree[2 * pos + 1].lazy += lazy; tree[2 * pos + 2].lazy += lazy; } } protected: vector<Node> tree; size_t arr_size; }; /** * Segment Tree Node. * * Note: this implementation only adds * required variables to perform set * operations. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <typename T> struct STNodeSB : STNodeB<T> { using super_type = STNodeB<T>; pair<T, bool> set = {0, false}; STNodeSB() : super_type() {} STNodeSB(const T value) : super_type(value) {} STNodeSB(const STNodeSB &lhs, const STNodeSB &rhs) { // meant to create a node by merging lhs and rhs. } }; /** * Segment Tree. * * Note: this implementation only adds * (range and point) set operations * since it costs a bunch of extra time and * memory (mainly when many Segment Trees * are constructed, e.g.: Segment Tree 2D). * * Note: when the tree is constructed by * inserting each value one by one using * set and/or update, the time complexity * is actually O(n*log(n)). * * Time Complexity: O(n). * Space Complexity: O(n). * Where n is the size of the array. */ template <typename NodeS, typename T> class SegTreeSB : public SegTreeB<NodeS, T> { public: SegTreeSB() = delete; SegTreeSB(const size_t arr_size) : super_type(arr_size) {} SegTreeSB(const vector<T> &arr) : super_type(arr) {} SegTreeSB(const size_t arr_size, const vector<T> &arr) : super_type(arr_size, arr) {} SegTreeSB(const SegTreeSB &lhs, const SegTreeSB &rhs) : super_type(lhs, rhs) {} /** * Sets the ith array value to value. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ void set(const size_t i, const T value) { assert(i < this->arr_size); set(i, i, value); } /** * Sets the array values in the range * [l, r] to value. * * Time Complexity: O(log(n)). * Space Complexity: O(log(n)). * Where n is the size of the array. */ void set(const size_t l, const size_t r, const T value) { assert(l <= r and r < this->arr_size); set(0, this->arr_size - 1, l, r, 0, value); } private: using super_type = SegTreeB<NodeS, T>; NodeS set(const size_t l, const size_t r, const size_t i, const size_t j, const size_t pos, const T value) { propagate(l, r, pos); if (l > j or r < i) return this->tree[pos]; if (l >= i and r <= j) { this->tree[pos].set = {value, true}; // it's important to propagate before returning and merge nodes. propagate(l, r, pos); return this->tree[pos]; } size_t mid = (l + r) / 2; return this->tree[pos] = NodeS(set(l, mid, i, j, 2 * pos + 1, value), set(mid + 1, r, i, j, 2 * pos + 2, value)); } void propagate(const size_t l, const size_t r, const size_t pos) { T lazy = this->tree[pos].lazy; pair<T, bool> set = this->tree[pos].set; if (lazy == 0 and !set.second) return; this->tree[pos].update(r - l + 1); if (l != r) { if (set.second) { this->tree[2 * pos + 1].set = set; this->tree[2 * pos + 2].set = set; this->tree[2 * pos + 1].lazy = 0; this->tree[2 * pos + 2].lazy = 0; } this->tree[2 * pos + 1].lazy += lazy; this->tree[2 * pos + 2].lazy += lazy; } } }; enum class STKind { RMaxQ, RMinQ, RSumQ, RXorQ, }; const STKind RMaxQ = STKind::RMaxQ; const STKind RMinQ = STKind::RMinQ; const STKind RSumQ = STKind::RSumQ; const STKind RXorQ = STKind::RXorQ; /** * Returns the default value according to * the SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <typename T> T default_value(STKind k) { switch (k) { case RMaxQ: return -((long long)1e9 + 1); case RMinQ: return ((long long)1e9 + 1); case RSumQ: return 0; case RXorQ: return 0; default: assert(false); } } /** * Merges two values into a new one * according to the SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <typename T> T merge_values(STKind k, const T lhs, const T rhs) { switch (k) { case RMaxQ: return max(lhs, rhs); case RMinQ: return min(lhs, rhs); case RSumQ: return lhs + rhs; case RXorQ: return lhs ^ rhs; default: assert(false); } } /** * Segment Tree Node. * * Note: this is a node implementation * for common use cases such as: * - Range Max Query; * - Range Min Query; * - Range Sum Query; * - Range Xor Query. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <STKind K, typename T> struct STNode : STNodeB<T> { using super_type = STNodeB<T>; STNode() : super_type(default_value<T>(K)) {} STNode(const T value) : super_type(value) {} STNode(const STNode &lhs, const STNode &rhs) : super_type(merge_values<T>(K, lhs.value, rhs.value)) { assert(lhs.lazy == 0 and rhs.lazy == 0); } /** * Checks if a value matches with * this->value according to the * SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ bool match(const T value) const { switch (K) { case RMaxQ: return this->value >= value; // return this->value > value; case RMinQ: return this->value <= value; // return this->value < value; case RSumQ: case RXorQ: default: assert(false); } } /** * Updates the current node according to * the SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ void update(const size_t range) { switch (K) { case RMaxQ: case RMinQ: this->value += this->lazy; break; case RSumQ: this->value += range * this->lazy; break; case RXorQ: break; default: assert(false); } this->lazy = 0; } }; template <STKind K, typename T> using SegTree = SegTreeB<STNode<K, T>, T>; /** * Segment Tree Node. * * Note: this implementation only * adds set operations. * * Time Complexity: O(1). * Space Complexity: O(1). */ template <STKind K, typename T> struct STNodeS : STNodeSB<T> { using super_type = STNodeSB<T>; STNodeS() : super_type(default_value<T>(K)) {} STNodeS(const T value) : super_type(value) {} STNodeS(const STNodeS &lhs, const STNodeS &rhs) : super_type(merge_values<T>(K, lhs.value, rhs.value)) { assert(lhs.lazy == 0 and rhs.lazy == 0); assert(!lhs.set.second and !rhs.set.second); } /** * Checks if a value matches with * this->value according to the * SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ bool match(const T value) const { switch (K) { case RMaxQ: return this->value >= value; // return this->value > value; case RMinQ: return this->value <= value; // return this->value < value; case RSumQ: case RXorQ: default: assert(false); } } /** * Updates the current node according to * the SegmentTreeKind. * * Time Complexity: O(1). * Space Complexity: O(1). */ void update(const size_t range) { if (this->set.second) { switch (K) { case RMaxQ: case RMinQ: this->value = this->set.first; break; case RSumQ: this->value = range * this->set.first; break; case RXorQ: this->value = (range % 2) * this->set.first; break; default: assert(false); } } switch (K) { case RMaxQ: case RMinQ: this->value += this->lazy; break; case RSumQ: this->value += range * this->lazy; break; case RXorQ: break; default: assert(false); } this->lazy = 0; this->set = {0, false}; } }; template <STKind K, typename T> using SegTreeS = SegTreeSB<STNodeS<K, T>, T>; # 3 "m.cpp" 2 struct Node : STNodeB<long long> { Node() : STNodeB<long long>(mod(default_value<long long>(RSumQ))) {} Node(const long long value) : STNodeB<long long>(mod(value)) {} Node(const Node &lhs, const Node &rhs) { this->value = mod(merge_values<long long>(RSumQ, lhs.value, rhs.value)); } bool match(const long long value) const { return true; }; void update(const size_t range) { this->value = mod(this->value + mod(range * this->lazy)); this->lazy = 0; } }; int32_t main() { (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)); long long n, k; cin >> n >> k; vi arr(n); for (long long &i : arr) cin >> i; reverse(arr.begin(), arr.end()); vvi dp(2, vi(k + 1)); dp[1][0] = 1; for (long long i : arr) { for (long long j = 0; j <= k; ++j) { dp[0][j] = 0; if (j) dp[1][j] = mod(dp[1][j] + dp[1][j - 1]); } for (long long j = 0; j <= k; ++j) { long long l = max((long long)0, j - i); dp[0][j] = dp[1][j]; if (l) dp[0][j] = mod(dp[0][j] - dp[1][l - 1]); } swap(dp[0], dp[1]); } cout << dp[1][k] << "\n"; return 0; }
replace
662
666
662
673
TLE
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll mod = 1e9 + 7, arr[105], pre[105], sum[105], dp[105][100005]; ll solve(ll n, ll k) { dp[0][0] = 1; for (int i = 1; i <= k; ++i) dp[0][i] = 0; for (int i = 1; i <= n; ++i) { sum[0] = dp[i - 1][0]; for (int j = 1; j <= k; ++j) sum[j] = (sum[j - 1] + dp[i - 1][j]) % mod; ll bound = min(k, pre[i]); for (int j = 0; j <= bound; ++j) { dp[i][j] = sum[j]; if (j > arr[i]) dp[i][j] = (dp[i][j] - sum[j - arr[i] - 1] + mod) % mod; } } return dp[n][k]; } int main() { ios::sync_with_stdio(0); cin.tie(0); ll n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> arr[i]; pre[i] = pre[i - 1] + arr[i]; } cout << solve(n, k) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll mod = 1e9 + 7, arr[105], pre[105], sum[100005], dp[105][100005]; ll solve(ll n, ll k) { dp[0][0] = 1; for (int i = 1; i <= k; ++i) dp[0][i] = 0; for (int i = 1; i <= n; ++i) { sum[0] = dp[i - 1][0]; for (int j = 1; j <= k; ++j) sum[j] = (sum[j - 1] + dp[i - 1][j]) % mod; ll bound = min(k, pre[i]); for (int j = 0; j <= bound; ++j) { dp[i][j] = sum[j]; if (j > arr[i]) dp[i][j] = (dp[i][j] - sum[j - arr[i] - 1] + mod) % mod; } } return dp[n][k]; } int main() { ios::sync_with_stdio(0); cin.tie(0); ll n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> arr[i]; pre[i] = pre[i - 1] + arr[i]; } cout << solve(n, k) << '\n'; return 0; }
replace
3
4
3
4
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long ll MOD = 1e9 + 7; ll A[110]; ll n, k; ll B[110][101000]; int main() { cin >> n >> k; ll a; for (ll j = 0; j <= k; j++) { B[0][j] = 0; } B[0][0] = 1; A[0] = 0; for (ll i = 1; i <= n; i++) { cin >> a; for (ll j = 0; j <= k; j++) { A[j + 1] = (A[j] + B[i - 1][j]) % MOD; } for (ll j = 0; j <= k; j++) { B[i][j] = (A[j + 1] - A[max(0LL, j - a)] + MOD) % MOD; } } cout << B[n][k] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long ll MOD = 1e9 + 7; ll A[101000]; ll n, k; ll B[110][101000]; int main() { cin >> n >> k; ll a; for (ll j = 0; j <= k; j++) { B[0][j] = 0; } B[0][0] = 1; A[0] = 0; for (ll i = 1; i <= n; i++) { cin >> a; for (ll j = 0; j <= k; j++) { A[j + 1] = (A[j] + B[i - 1][j]) % MOD; } for (ll j = 0; j <= k; j++) { B[i][j] = (A[j + 1] - A[max(0LL, j - a)] + MOD) % MOD; } } cout << B[n][k] << endl; return 0; }
replace
4
5
4
5
0
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int mod = 1e9 + 7; int a[110]; long long dp[110][N]; long long ps[110][N]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { ps[i - 1][j] = dp[i - 1][j]; if (j) ps[i - 1][j] = (ps[i - 1][j] + ps[i - 1][j - 1]) % mod; dp[i][j] = ps[i - 1][j]; dp[i][j] += mod - ps[i - 1][j - a[i] - 1]; dp[i][j] %= mod; } cout << dp[n][k]; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int mod = 1e9 + 7; int a[110]; long long dp[110][N]; long long ps[110][N]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; dp[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { ps[i - 1][j] = dp[i - 1][j]; if (j) ps[i - 1][j] = (ps[i - 1][j] + ps[i - 1][j - 1]) % mod; dp[i][j] = ps[i - 1][j]; if (j - a[i] - 1 >= 0) dp[i][j] += mod - ps[i - 1][j - a[i] - 1]; dp[i][j] %= mod; } cout << dp[n][k]; return 0; }
replace
19
20
19
21
-11
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int mod = 1e9 + 7; signed main() { int N, K; cin >> N >> K; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; int dp[N + 1][K + 1]; for (int i = 0; i < N + 1; i++) { for (int j = 0; j < K + 1; j++) { dp[i][j] = 0; } } dp[0][0] = 1; for (int i = 1; i < N + 1; i++) { vector<int> cs(K + 2); for (int k = 0; k < K + 1; k++) { cs[k + 1] = (cs[k] + dp[i - 1][k]) % mod; } for (int j = 0; j < K + 1; j++) { dp[i][j] = (cs[j + 1] - cs[max(j - A[i - 1], 0LL) + mod]) % mod; } } cout << dp[N][K] << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long const int mod = 1e9 + 7; signed main() { int N, K; cin >> N >> K; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; int dp[N + 1][K + 1]; for (int i = 0; i < N + 1; i++) { for (int j = 0; j < K + 1; j++) { dp[i][j] = 0; } } dp[0][0] = 1; for (int i = 1; i < N + 1; i++) { vector<int> cs(K + 2); for (int k = 0; k < K + 1; k++) { cs[k + 1] = (cs[k] + dp[i - 1][k]) % mod; } for (int j = 0; j < K + 1; j++) { dp[i][j] = (cs[j + 1] - cs[max(j - A[i - 1], 0LL)] + mod) % mod; } } cout << dp[N][K] << endl; }
replace
27
28
27
28
-11
p03172
Python
Time Limit Exceeded
N, K = map(int, input().split()) A = list(map(int, input().split())) MOD = 10**9 + 7 dp = [0] * (K + 1) dp[0] = 1 for i, a in enumerate(A, start=1): accDp = [0] * (K + 2) for k in range(1, K + 2): accDp[k] = accDp[k - 1] + dp[k - 1] for k in range(K + 1): dp[k] += accDp[k] - accDp[max(0, k - a)] print(dp[K] % MOD)
N, K = map(int, input().split()) A = list(map(int, input().split())) MOD = 10**9 + 7 dp = [0] * (K + 1) dp[0] = 1 for i, a in enumerate(A, start=1): accDp = [0] * (K + 2) for k in range(1, K + 2): accDp[k] = accDp[k - 1] + dp[k - 1] for k in range(K + 1): dp[k] = (dp[k] + accDp[k] - accDp[max(0, k - a)]) % MOD print(dp[K] % MOD)
replace
13
14
13
14
TLE
p03172
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int mod = 1e9 + 7; int N, K; cin >> N >> K; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a[i]; vector<vector<long long>> dp(N, vector<long long>(K + 1, 0)); for (int i = 0; i < N; i++) { for (int j = 0; j < K + 1; j++) { if (j == 0) { dp[i][j] = 1; continue; } if (i == 0) { if (j <= a[i]) dp[i][j] = 1; else dp[i][j] = 0; continue; } dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; if (j > a[i]) dp[i][j] -= dp[i - 1][j - 1 - a[i]]; while (dp[i][j] > mod) dp[i][j] -= mod; } } cout << dp[N - 1][K] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int mod = 1e9 + 7; int N, K; cin >> N >> K; vector<int> a(N); for (int i = 0; i < N; i++) cin >> a[i]; vector<vector<long long>> dp(N, vector<long long>(K + 1, 0)); for (int i = 0; i < N; i++) { for (int j = 0; j < K + 1; j++) { if (j == 0) { dp[i][j] = 1; continue; } if (i == 0) { if (j <= a[i]) dp[i][j] = 1; else dp[i][j] = 0; continue; } dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; if (j > a[i]) dp[i][j] += mod - dp[i - 1][j - 1 - a[i]]; dp[i][j] %= mod; } } cout << dp[N - 1][K] << endl; return 0; }
replace
32
35
32
34
TLE
p03172
C++
Runtime Error
// Created by Kira #include <bits/stdc++.h> using namespace std; #define IOS \ cin.sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define pii pair<int, int> #define pb push_back #define vi vector<int> using ll = long long; const int mod = 1e9 + 7; ll pwr(ll a, ll b); void add_self(int &a, int &b) { a += b; if (a >= mod) a -= mod; } void sub_self(int &a, int &b) { a -= b; if (a < 0) a += mod; } int n, k; // dp[i] = Number of ways to share candies if i candies are distributed // so far. int main() { IOS cin >> n >> k; vi dp(k + 1); dp[0] = 1; for (int i = 0; i < n; i++) { int up_to; cin >> up_to; vi helper(k + 1); // O(k) for (int used = k; used >= 0; --used) { int L = used + 1; int R = used + min(up_to, k - used); add_self(helper[L], dp[used]); if (R + 1 <= k) sub_self(helper[R + 1], dp[used]); } // O(k) int prefix_sum = 0; for (int j = 0; j <= k; j++) { add_self(prefix_sum, helper[j]); add_self(dp[j], prefix_sum); } } cout << dp[k] << "\n"; // Time Complexity : O(nk) } ll pwr(ll a, ll b) { a %= mod; // Remove mod if not required ll res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; }
// Created by Kira #include <bits/stdc++.h> using namespace std; #define IOS \ cin.sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define pii pair<int, int> #define pb push_back #define vi vector<int> using ll = long long; const int mod = 1e9 + 7; ll pwr(ll a, ll b); void add_self(int &a, int &b) { a += b; if (a >= mod) a -= mod; } void sub_self(int &a, int &b) { a -= b; if (a < 0) a += mod; } int n, k; // dp[i] = Number of ways to share candies if i candies are distributed // so far. int main() { IOS cin >> n >> k; vi dp(k + 1); dp[0] = 1; for (int i = 0; i < n; i++) { int up_to; cin >> up_to; vi helper(k + 1); // O(k) for (int used = k; used >= 0; --used) { int L = used + 1; int R = used + min(up_to, k - used); if (L <= R) { add_self(helper[L], dp[used]); if (R + 1 <= k) sub_self(helper[R + 1], dp[used]); } } // O(k) int prefix_sum = 0; for (int j = 0; j <= k; j++) { add_self(prefix_sum, helper[j]); add_self(dp[j], prefix_sum); } } cout << dp[k] << "\n"; // Time Complexity : O(nk) } ll pwr(ll a, ll b) { a %= mod; // Remove mod if not required ll res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; }
replace
55
58
55
60
0
p03172
C++
Runtime Error
#pragma GCC optimize("-O3") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #include <boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; using namespace std; #define all(c) (c).begin(), (c).end() #define endl "\n" #define ff first #define ss second #define allr(c) (c).rbegin(), (c).rend() #define ifr(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define pof pop_front #define pob pop_back #define pb emplace_back #define pf emplace_front #define fstm(m, n, r) \ m.reserve(n); \ m.max_load_factor(r) #define mp make_pair #define mt make_tuple #define inf LLONG_MAX #define os \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). const double PI = acos(-1); typedef complex<double> cd; typedef long long ll; ll gcd() { return 0ll; } template <typename T, typename... Args> T gcd(T a, Args... args) { return __gcd(a, (__typeof(a))gcd(args...)); } typedef map<ll, ll> mll; typedef map<string, ll> msll; typedef unordered_map<ll, ll> umap; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef long double ld; #define mod 1000000007 #define N 100002 ll dp[101][N], pre[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif int n, k; cin >> n >> k; ll a[n]; ifr(i, 0, n) cin >> a[i]; ifr(i, 0, n) ifr(j, 0, N) dp[i][j] = 0; ifr(i, 0, a[0] + 1) dp[0][i] = 1; ifr(i, 1, n) { ifr(j, 0, k + 1) { pre[j] = dp[i - 1][j] + ((j - 1 >= 0) ? pre[j - 1] : 0); pre[j] %= mod; dp[i][j] = (pre[j] - ((j - a[i] - 1 >= 0) ? pre[j - a[i] - 1] : 0)) % mod; dp[i][j] = (dp[i][j] + mod) % mod; } } cout << dp[n - 1][k] << endl; return 0; }
#pragma GCC optimize("-O3") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #include <boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; using namespace std; #define all(c) (c).begin(), (c).end() #define endl "\n" #define ff first #define ss second #define allr(c) (c).rbegin(), (c).rend() #define ifr(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define pof pop_front #define pob pop_back #define pb emplace_back #define pf emplace_front #define fstm(m, n, r) \ m.reserve(n); \ m.max_load_factor(r) #define mp make_pair #define mt make_tuple #define inf LLONG_MAX #define os \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). const double PI = acos(-1); typedef complex<double> cd; typedef long long ll; ll gcd() { return 0ll; } template <typename T, typename... Args> T gcd(T a, Args... args) { return __gcd(a, (__typeof(a))gcd(args...)); } typedef map<ll, ll> mll; typedef map<string, ll> msll; typedef unordered_map<ll, ll> umap; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef long double ld; #define mod 1000000007 #define N 100002 ll dp[101][N], pre[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; ll a[n]; ifr(i, 0, n) cin >> a[i]; ifr(i, 0, n) ifr(j, 0, N) dp[i][j] = 0; ifr(i, 0, a[0] + 1) dp[0][i] = 1; ifr(i, 1, n) { ifr(j, 0, k + 1) { pre[j] = dp[i - 1][j] + ((j - 1 >= 0) ? pre[j - 1] : 0); pre[j] %= mod; dp[i][j] = (pre[j] - ((j - a[i] - 1 >= 0) ? pre[j - a[i] - 1] : 0)) % mod; dp[i][j] = (dp[i][j] + mod) % mod; } } cout << dp[n - 1][k] << endl; return 0; }
replace
54
57
54
55
-11
p03172
C++
Runtime Error
/* @author: Charan Sai */ #include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define int long long #define ppi pair<int, int> #define endl "\n" #define nl cout << "\n" #define deb(x) cout << #x << " " << x << endl; int n, k; const int MAX_N = 102, MAX_K = 1e5 + 2; const int mod = 1e9 + 7; int arr[MAX_N]; int dp[MAX_N][MAX_K]; int s[MAX_N][MAX_K]; // O(N*K*K) time = O(NK^2) time // O(NK) space int Top_Down(int i = n, int candies = k) { // Base Case if (i == 0) return (candies == 0); if (dp[i][candies] != -1) return dp[i][candies] % mod; // Choice int ways = 0; for (int x = 0; x <= arr[i]; ++x) { if (candies >= x) ways += (Top_Down(i - 1, candies - x) % mod); } return (dp[i][candies] = ways % mod); } int Bottom_Up() { // Base dp[0][0] = 1; for (int j = 1; j <= k; ++j) dp[0][j] = 0; for (int i = 1; i <= n; ++i) dp[i][0] = 1; // Choice for (int i = 1; i <= n; ++i) { for (int candies = 1; candies <= k; ++candies) { int ways = 0; for (int x = 0; x <= arr[i]; ++x) { if (candies >= x) ways += dp[i - 1][candies - x] % mod; } dp[i][candies] = ways % mod; } } return dp[n][k]; } /* dp[0][0] = 1; for(int j = 1; j <= k; ++j) dp[0][j] = 0; for(int i = 1; i <= n; ++i) dp[i][0] = 1; for(i = 1; i <= n; ++i) { s[i-1][0] = dp[i-1][0]; for(int j = 1; j <= k; ++j) { //compute s[i][j] s[i-1][j] = (dp[i-1][j] + s[i-1][j-1])%mod; } for(int j = 1; j <= k; ++j) { //compute dp[i][j] dp[i][j] = s[i-1][j] if(j-arr[i]-1 >= 0) dp[i][j] = (dp[i][j] - s[i-1][j-arr[i]-1] + mod)%mod; } } s(i,j) = T(i,0) + T(i,1) + ..... + T(i,j) T(i,j) = s(i-1,j) - s(i-1,j-a[i]-1) */ int Bottom_Up2() { dp[0][0] = 1; for (int j = 1; j <= k; ++j) dp[0][j] = 0; for (int i = 1; i <= n; ++i) dp[i][0] = 1; int i, j; for (i = 1; i <= n; ++i) { s[i - 1][0] = dp[i - 1][0]; for (j = 1; j <= k; ++j) { // compute s[i][j] s[i - 1][j] = (dp[i - 1][j] + s[i - 1][j - 1]) % mod; } for (j = 1; j <= k; ++j) { // compute dp[i][j] dp[i][j] = s[i - 1][j]; if (j - arr[i] - 1 >= 0) dp[i][j] = (dp[i][j] - s[i - 1][j - arr[i] - 1] + mod) % mod; } } return dp[n][k] % mod; } int32_t main() { IOS; int t; cin >> t; while (t--) { memset(dp, -1, sizeof(dp)); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> arr[i]; cout << Bottom_Up2() % mod << endl; } }
/* @author: Charan Sai */ #include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define int long long #define ppi pair<int, int> #define endl "\n" #define nl cout << "\n" #define deb(x) cout << #x << " " << x << endl; int n, k; const int MAX_N = 102, MAX_K = 1e5 + 2; const int mod = 1e9 + 7; int arr[MAX_N]; int dp[MAX_N][MAX_K]; int s[MAX_N][MAX_K]; // O(N*K*K) time = O(NK^2) time // O(NK) space int Top_Down(int i = n, int candies = k) { // Base Case if (i == 0) return (candies == 0); if (dp[i][candies] != -1) return dp[i][candies] % mod; // Choice int ways = 0; for (int x = 0; x <= arr[i]; ++x) { if (candies >= x) ways += (Top_Down(i - 1, candies - x) % mod); } return (dp[i][candies] = ways % mod); } int Bottom_Up() { // Base dp[0][0] = 1; for (int j = 1; j <= k; ++j) dp[0][j] = 0; for (int i = 1; i <= n; ++i) dp[i][0] = 1; // Choice for (int i = 1; i <= n; ++i) { for (int candies = 1; candies <= k; ++candies) { int ways = 0; for (int x = 0; x <= arr[i]; ++x) { if (candies >= x) ways += dp[i - 1][candies - x] % mod; } dp[i][candies] = ways % mod; } } return dp[n][k]; } /* dp[0][0] = 1; for(int j = 1; j <= k; ++j) dp[0][j] = 0; for(int i = 1; i <= n; ++i) dp[i][0] = 1; for(i = 1; i <= n; ++i) { s[i-1][0] = dp[i-1][0]; for(int j = 1; j <= k; ++j) { //compute s[i][j] s[i-1][j] = (dp[i-1][j] + s[i-1][j-1])%mod; } for(int j = 1; j <= k; ++j) { //compute dp[i][j] dp[i][j] = s[i-1][j] if(j-arr[i]-1 >= 0) dp[i][j] = (dp[i][j] - s[i-1][j-arr[i]-1] + mod)%mod; } } s(i,j) = T(i,0) + T(i,1) + ..... + T(i,j) T(i,j) = s(i-1,j) - s(i-1,j-a[i]-1) */ int Bottom_Up2() { dp[0][0] = 1; for (int j = 1; j <= k; ++j) dp[0][j] = 0; for (int i = 1; i <= n; ++i) dp[i][0] = 1; int i, j; for (i = 1; i <= n; ++i) { s[i - 1][0] = dp[i - 1][0]; for (j = 1; j <= k; ++j) { // compute s[i][j] s[i - 1][j] = (dp[i - 1][j] + s[i - 1][j - 1]) % mod; } for (j = 1; j <= k; ++j) { // compute dp[i][j] dp[i][j] = s[i - 1][j]; if (j - arr[i] - 1 >= 0) dp[i][j] = (dp[i][j] - s[i - 1][j - arr[i] - 1] + mod) % mod; } } return dp[n][k] % mod; } int32_t main() { IOS; int t = 1; // cin >> t; while (t--) { memset(dp, -1, sizeof(dp)); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> arr[i]; cout << Bottom_Up2() % mod << endl; } }
replace
112
114
112
114
-11
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define m_p make_pair #define rep(i, a, b) for (int i = a; i < b; i++) #define sz(x) (int)x.size() #define f first #define debug cout << "##########3" << endl #define mod 1000000007 #define ll long long int #define lMax LLONG_MAX // queue<int> , prefix_sum(a,a+n) , reverse(a,a+n) , priority_queue <int> max // heap , priority_queue <int, vector<int>, greater<int> > min heap; // == precedence gretaer then & ll n, m; ll arr[105]; // Prefix sum calculation wala dp !!!! ll dp[105][100006]; ll prefix[105][100006]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll i, j, k, c = 0, ans = 0, t, z, x = 0, y; cin >> n >> k; rep(i, 0, n) cin >> arr[i]; dp[0][0] = 1; for (j = 0; j <= k; j++) { prefix[0][j] = 1; } for (i = 1; i <= n; i++) { for (j = 0; j <= k; j++) { if (j == 0) { dp[i][j] = dp[i - 1][j]; dp[i][j] %= mod; prefix[i][j] = 1; prefix[i][j] %= mod; } else { ll l = max(0LL, arr[i - 1] + 1); dp[i][j] = prefix[i - 1][j] - prefix[i - 1][j - l] + mod; dp[i][j] %= mod; prefix[i][j] = prefix[i][j - 1] + dp[i][j]; prefix[i][j] %= mod; } // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; } } cout << dp[n][k] % mod; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define m_p make_pair #define rep(i, a, b) for (int i = a; i < b; i++) #define sz(x) (int)x.size() #define f first #define debug cout << "##########3" << endl #define mod 1000000007 #define ll long long int #define lMax LLONG_MAX // queue<int> , prefix_sum(a,a+n) , reverse(a,a+n) , priority_queue <int> max // heap , priority_queue <int, vector<int>, greater<int> > min heap; // == precedence gretaer then & ll n, m; ll arr[105]; // Prefix sum calculation wala dp !!!! ll dp[105][100006]; ll prefix[105][100006]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll i, j, k, c = 0, ans = 0, t, z, x = 0, y; cin >> n >> k; rep(i, 0, n) cin >> arr[i]; dp[0][0] = 1; for (j = 0; j <= k; j++) { prefix[0][j] = 1; } for (i = 1; i <= n; i++) { for (j = 0; j <= k; j++) { if (j - arr[i - 1] - 1 >= 0) dp[i][j] = (prefix[i - 1][j] - prefix[i - 1][j - arr[i - 1] - 1] + mod) % mod; else dp[i][j] = prefix[i - 1][j]; prefix[i][j] = (prefix[i][j - 1] % mod + dp[i][j] % mod) % mod; } } cout << dp[n][k] % mod; return 0; }
replace
34
47
34
40
-11
p03172
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 typedef long long ll; void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } ll modInverse(ll a) { return modPow(a, MOD - 2); } ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } int main() { #ifdef tajir freopen("input.txt", "r", stdin); #else // online submission #endif int n, k; cin >> n >> k; vector<int> dp(k + 1, 0); dp[0] = 1; for (int child = 0; child < n; ++child) { int up_to; cin >> up_to; vector<int> dp2(k + 1, 0); for (int used = k; used >= 0; --used) { int L = used + 1, R = used + min(up_to, k - used); if (L > R) continue; dp2[L] = modAdd(dp2[L], dp[used]); dp2[R + 1] = modSub(dp2[R + 1], dp[used]); } for (int i = 0; i <= k; ++i) { dp2[i] = modAdd(dp2[i], dp2[i - 1]); dp[i] = modAdd(dp[i], dp2[i]); } } cout << dp[k] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 typedef long long ll; void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } ll modInverse(ll a) { return modPow(a, MOD - 2); } ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } int main() { #ifdef tajir freopen("input.txt", "r", stdin); #else // online submission #endif int n, k; cin >> n >> k; vector<int> dp(k + 1, 0); dp[0] = 1; for (int child = 0; child < n; ++child) { int up_to; cin >> up_to; vector<int> dp2(k + 1, 0); for (int used = k; used >= 0; --used) { int L = used + 1, R = used + min(up_to, k - used); if (L > R) continue; dp2[L] = modAdd(dp2[L], dp[used]); if (R + 1 <= k) dp2[R + 1] = modSub(dp2[R + 1], dp[used]); } for (int i = 0; i <= k; ++i) { dp2[i] = modAdd(dp2[i], dp2[i - 1]); dp[i] = modAdd(dp[i], dp2[i]); } } cout << dp[k] << endl; return 0; }
replace
68
69
68
70
0
p03172
C++
Runtime Error
#include <algorithm> //swap function is here #include <bits/stdc++.h> #include <cassert> #include <climits> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> // istringstream buffer(myString); #include <stack> #include <vector> using namespace std; #define bit(x, i) (x & (1 << i)) // select the bit of position i of x #define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x #define hBit(msb, n) \ asm("bsrl %1,%0" \ : "=r"(msb) \ : "r"(n)) // get the highest bit of x, maybe the fastest #define max(a, b) (a < b ? b : a) //__builtin_popcount(n); #define IN(i, l, r) (l < i && i < r) // the next for are for checking bound #define LINR(i, l, r) (l <= i && i <= r) #define LIN(i, l, r) (l <= i && i < r) #define INR(i, l, r) (l < i && i <= r) #define F(i, L, R) for (int i = L; i < R; i++) // next four are for "for loops" #define FE(i, L, R) for (int i = L; i <= R; i++) #define FF(i, L, R) for (int i = L; i > R; i--) #define FFE(i, L, R) for (int i = L; i >= R; i--) #define getI(a) \ scanf("%d", &a) // next three are handy ways to get ints, it's also force you // to use '&' sign #define getII(a, b) scanf("%d%d", &a, &b) #define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c) #define wez(n) \ int(n); \ scanf( \ "%d", \ &(n)) // handy if the input is right after the definition of a variable #define wez2(n, m) \ int(n), (m); \ scanf("%d %d", &(n), &(m)) #define wez3(n, m, k) \ int(n), (m), (k); \ scanf("%d %d %d", &(n), &(m), &(k)) #define TESTS wez(testow) while (testow--) // for multilple cases problems #define whileZ \ int T; \ getI(T); \ while (T--) // the same as above #define getS(x) scanf("%s", x) // get a char* string #define clr(a, x) memset(a, x, sizeof(a)) // set elements of array to some value #define char2Int(c) (c - '0') #define lastEle(vec) vec[vec.size() - 1] #define SZ(x) ((int)((x).size())) #define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b #define REMIN(a, b) (a) = min((a), (b)); #define FOREACH(i, t) \ for (typeof(t.begin()) i = t.begin(); i != t.end(); \ i++) // traverse an STL data structure #define ALL(c) (c).begin(), (c).end() // handy for function like "sort()" #define PRESENT(c, x) ((c).find(x) != (c).end()) #define CPRESENT(c, x) (find(ALL(c), x) != (c).end()) #define ll \ long long // data types used often, but you don't want to type them time by // time #define ull unsigned long long #define ui unsigned int #define us unsigned short #define IOS \ ios_base::sync_with_stdio(0); // to synchronize the input of cin and scanf #define INF 1001001001 #define PI 3.1415926535897932384626 // for map, pair #define mp make_pair #define fi first #define se second // for debug inline void pisz(int n) { printf("%d\n", n); } #define DBG(vari) cerr << #vari << " = " << (vari) << endl; #define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\n' : ' ') #define printV(a) printA(a, 0, a.size() - 1) #define MAXN 10000 // for vectors #define pb push_back typedef int elem_t; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; // directions const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; template <typename T, typename TT> ostream &operator<<(ostream &s, pair<T, TT> t) { return s << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream &operator<<(ostream &s, vector<T> t) { F(i, 0, SZ(t)) s << t[i] << " "; return s; } ll mod = 1000000007; ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } ll power(ll a, ll b, ll m) { ll r = 1; a = a % m; while (b) { if (b & 1) r = r * a % m; a = a * a % m; b = b >> 1; } return r; } ll dp[101][100001]; int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; ll k; cin >> k; ll arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; /* code */ } // cout<<"YES\n"; // intital state // ways of having atleast j candies remaining after considering ith kid for (int i = 0; i <= k; ++i) { if (i != 0) dp[0][i] = dp[0][i - 1]; if (k - i <= arr[0] && i <= k) { dp[0][i]++; } dp[0][i] = dp[0][i] % mod; /* code */ } for (int i = 1; i <= n; ++i) { for (int j = 0; j <= k; ++j) { if (j == 0) { dp[i][j] = dp[i - 1][min(k, j + arr[i])] % mod; } else dp[i][j] = (dp[i][j - 1] + dp[i - 1][min(k, j + arr[i])] - dp[i - 1][j - 1]); dp[i][j] = dp[i][j] % mod; /* code */ } /* code */ } if (dp[n - 1][0] < 0) cout << dp[n - 1][0] + mod << "\n"; else cout << dp[n - 1][0] << "\n"; }
#include <algorithm> //swap function is here #include <bits/stdc++.h> #include <cassert> #include <climits> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> // istringstream buffer(myString); #include <stack> #include <vector> using namespace std; #define bit(x, i) (x & (1 << i)) // select the bit of position i of x #define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x #define hBit(msb, n) \ asm("bsrl %1,%0" \ : "=r"(msb) \ : "r"(n)) // get the highest bit of x, maybe the fastest #define max(a, b) (a < b ? b : a) //__builtin_popcount(n); #define IN(i, l, r) (l < i && i < r) // the next for are for checking bound #define LINR(i, l, r) (l <= i && i <= r) #define LIN(i, l, r) (l <= i && i < r) #define INR(i, l, r) (l < i && i <= r) #define F(i, L, R) for (int i = L; i < R; i++) // next four are for "for loops" #define FE(i, L, R) for (int i = L; i <= R; i++) #define FF(i, L, R) for (int i = L; i > R; i--) #define FFE(i, L, R) for (int i = L; i >= R; i--) #define getI(a) \ scanf("%d", &a) // next three are handy ways to get ints, it's also force you // to use '&' sign #define getII(a, b) scanf("%d%d", &a, &b) #define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c) #define wez(n) \ int(n); \ scanf( \ "%d", \ &(n)) // handy if the input is right after the definition of a variable #define wez2(n, m) \ int(n), (m); \ scanf("%d %d", &(n), &(m)) #define wez3(n, m, k) \ int(n), (m), (k); \ scanf("%d %d %d", &(n), &(m), &(k)) #define TESTS wez(testow) while (testow--) // for multilple cases problems #define whileZ \ int T; \ getI(T); \ while (T--) // the same as above #define getS(x) scanf("%s", x) // get a char* string #define clr(a, x) memset(a, x, sizeof(a)) // set elements of array to some value #define char2Int(c) (c - '0') #define lastEle(vec) vec[vec.size() - 1] #define SZ(x) ((int)((x).size())) #define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b #define REMIN(a, b) (a) = min((a), (b)); #define FOREACH(i, t) \ for (typeof(t.begin()) i = t.begin(); i != t.end(); \ i++) // traverse an STL data structure #define ALL(c) (c).begin(), (c).end() // handy for function like "sort()" #define PRESENT(c, x) ((c).find(x) != (c).end()) #define CPRESENT(c, x) (find(ALL(c), x) != (c).end()) #define ll \ long long // data types used often, but you don't want to type them time by // time #define ull unsigned long long #define ui unsigned int #define us unsigned short #define IOS \ ios_base::sync_with_stdio(0); // to synchronize the input of cin and scanf #define INF 1001001001 #define PI 3.1415926535897932384626 // for map, pair #define mp make_pair #define fi first #define se second // for debug inline void pisz(int n) { printf("%d\n", n); } #define DBG(vari) cerr << #vari << " = " << (vari) << endl; #define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\n' : ' ') #define printV(a) printA(a, 0, a.size() - 1) #define MAXN 10000 // for vectors #define pb push_back typedef int elem_t; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; // directions const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; template <typename T, typename TT> ostream &operator<<(ostream &s, pair<T, TT> t) { return s << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream &operator<<(ostream &s, vector<T> t) { F(i, 0, SZ(t)) s << t[i] << " "; return s; } ll mod = 1000000007; ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } ll power(ll a, ll b, ll m) { ll r = 1; a = a % m; while (b) { if (b & 1) r = r * a % m; a = a * a % m; b = b >> 1; } return r; } ll dp[101][100001]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; ll k; cin >> k; ll arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; /* code */ } // cout<<"YES\n"; // intital state // ways of having atleast j candies remaining after considering ith kid for (int i = 0; i <= k; ++i) { if (i != 0) dp[0][i] = dp[0][i - 1]; if (k - i <= arr[0] && i <= k) { dp[0][i]++; } dp[0][i] = dp[0][i] % mod; /* code */ } for (int i = 1; i <= n; ++i) { for (int j = 0; j <= k; ++j) { if (j == 0) { dp[i][j] = dp[i - 1][min(k, j + arr[i])] % mod; } else dp[i][j] = (dp[i][j - 1] + dp[i - 1][min(k, j + arr[i])] - dp[i - 1][j - 1]); dp[i][j] = dp[i][j] % mod; /* code */ } /* code */ } if (dp[n - 1][0] < 0) cout << dp[n - 1][0] + mod << "\n"; else cout << dp[n - 1][0] << "\n"; }
delete
128
134
128
128
-11
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #include <unordered_map> #define inputarr(a, n) \ for (ll i = 0; i < n; i++) \ cin >> a[i]; #define prllarr(a, n) \ for (ll i = 0; i < n; i++) \ cout << a[i] << " "; #define pb push_back #define ll long long #define mod 1000000007 #define foi \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(0); #define in(n) scanf("%lld", &n); #define in2(x, y) scanf("%lld %lld", &(x), &(y)); #define in3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z)); #define out(n) printf("%lld\n", n); #define out2(x, y) printf("%lld %lld\n", x, y); #define test(t) \ ll t; \ in(t); \ while (t--) #define set(arr, n, s) \ for (ll i = 0; i < n; i++) { \ arr[i] = s; \ } ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } ll modInverse(ll a, ll p) { return power(a, p - 2, p); } // used with feemat little ll gcd(ll x, ll y) { if (x == 0 || y == 0) { return max(y, x); } return gcd(y % x, x); } ll gcdExtended(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } // o(log(b)) ; ll arr[403]; ll dp[405][405]; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n; in(n) for (ll i = 1; i <= n; i++){in(arr[i])} arr[0] = 0; for (ll i = 1; i <= n; i++) { arr[i] += arr[i - 1]; } for (ll i = 0; i <= 400; i++) { for (ll j = 0; j <= 400; j++) { dp[i][j] = 1e18; } } for (ll i = 1; i <= n; i++) { dp[i][i] = 0; } for (ll len = 2; len <= n; len++) { for (ll i = 1; i <= n; i++) { ll end = i + len - 1; if (end > n) break; for (ll k = i; k < end; k++) { dp[i][end] = min(dp[i][k] + dp[k + 1][end] + arr[end] - arr[i - 1], dp[i][end]); } } } // for(ll i=0;i<=n;i++){ // for(ll j=0;j<=n;j++){ // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; // } // } cout << dp[1][n] << endl; } /*error----- convert every int to long long eg-1LL create array with proper analysis of problem constrain check mod also */
#include <bits/stdc++.h> using namespace std; #include <unordered_map> #define inputarr(a, n) \ for (ll i = 0; i < n; i++) \ cin >> a[i]; #define prllarr(a, n) \ for (ll i = 0; i < n; i++) \ cout << a[i] << " "; #define pb push_back #define ll long long #define mod 1000000007 #define foi \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(0); #define in(n) scanf("%lld", &n); #define in2(x, y) scanf("%lld %lld", &(x), &(y)); #define in3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z)); #define out(n) printf("%lld\n", n); #define out2(x, y) printf("%lld %lld\n", x, y); #define test(t) \ ll t; \ in(t); \ while (t--) #define set(arr, n, s) \ for (ll i = 0; i < n; i++) { \ arr[i] = s; \ } ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } ll modInverse(ll a, ll p) { return power(a, p - 2, p); } // used with feemat little ll gcd(ll x, ll y) { if (x == 0 || y == 0) { return max(y, x); } return gcd(y % x, x); } ll gcdExtended(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } // o(log(b)) ; ll arr[403]; ll dp[405][405]; int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // #endif ll n; in(n) for (ll i = 1; i <= n; i++){in(arr[i])} arr[0] = 0; for (ll i = 1; i <= n; i++) { arr[i] += arr[i - 1]; } for (ll i = 0; i <= 400; i++) { for (ll j = 0; j <= 400; j++) { dp[i][j] = 1e18; } } for (ll i = 1; i <= n; i++) { dp[i][i] = 0; } for (ll len = 2; len <= n; len++) { for (ll i = 1; i <= n; i++) { ll end = i + len - 1; if (end > n) break; for (ll k = i; k < end; k++) { dp[i][end] = min(dp[i][k] + dp[k + 1][end] + arr[end] - arr[i - 1], dp[i][end]); } } } // for(ll i=0;i<=n;i++){ // for(ll j=0;j<=n;j++){ // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; // } // } cout << dp[1][n] << endl; } /*error----- convert every int to long long eg-1LL create array with proper analysis of problem constrain check mod also */
replace
69
73
69
73
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const long long INF = 1LL << 60; using namespace std; 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; } // 入力 int N; long long a[410]; long long b[410]; long long dp[410][410]; bool ischecked[410][410] = {false}; void ruiseki() { b[0] = a[0]; for (int i = 1; i < N; ++i) b[i] = b[i - 1] + a[i]; } long long sum(int L, int R) { return b[R] - b[L - 1]; } long long rec(int L, int R) { if (L == R) return 0; if (ischecked[L][R]) return dp[L][R]; long long ret = INF; for (int i = L; i < R; ++i) chmin(ret, rec(L, i) + rec(i + 1, R) + sum(L, R)); return dp[L][R] = ret; } int main() { cin >> N; for (int i = 0; i < N; ++i) cin >> a[i]; ruiseki(); cout << rec(0, N - 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const long long INF = 1LL << 60; using namespace std; 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; } // 入力 int N; long long a[410]; long long b[410]; long long dp[410][410]; bool ischecked[410][410] = {false}; void ruiseki() { b[0] = a[0]; for (int i = 1; i < N; ++i) b[i] = b[i - 1] + a[i]; } long long sum(int L, int R) { return b[R] - b[L - 1]; } long long rec(int L, int R) { if (L == R) return 0; if (ischecked[L][R]) return dp[L][R]; ischecked[L][R] = true; long long ret = INF; for (int i = L; i < R; ++i) chmin(ret, rec(L, i) + rec(i + 1, R) + sum(L, R)); return dp[L][R] = ret; } int main() { cin >> N; for (int i = 0; i < N; ++i) cin >> a[i]; ruiseki(); cout << rec(0, N - 1) << endl; return 0; }
insert
42
42
42
43
TLE
p03173
C++
Runtime Error
/*------U Have To DO It------*/ /* BY-> RicoProg */ /* ___ __|__| | |___ */ #include <bits/stdc++.h> using namespace std; //---------------------------------------------------MACROS---------------------------------------------------------- #define ll long long #define ld long double #define beg(i, n) for (ll i = 0; i < n; i++) #define beg1(i, n) for (ll i = 1; i < n; i++) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL) #define pb push_back #define mp make_pair #define nl "\n" #define vec(v, n) vector<ll> v(n) #define all(x) x.begin(), x.end() #define pii pair<int, int> #define pll pair<ll, ll> #define mii map<int, int> #define mll map<ll, ll> #define msi map<string, int> #define mci map<char, int> #define f1 first #define f2 second #define inf 1e18 + 10 //---------------------------------------------------GLOBAL---------------------------------------------------------- const ll MOD = 1e9 + 7; const ll MAX = 1e5 + 7; //---------------------------------------------------TEMPLATES------------------------------------------------------- void add_self(ll &a, ll b) { a += b; if (a >= MOD) { a -= MOD; } } void sub_self(ll &a, ll b) { a -= b; if (a < 0) { a += MOD; } } void merge_self(vector<ll> &a, vector<ll> b) { ll s = a.size(); beg(i, s) a[i] += b[i]; } void set_self(vector<ll> &a, ll n, ll s) { beg(i, s) a[i] = n; } ll factors() {} //---------------------------------------------------FUNCTIONS------------------------------------------------------- ll dp[401][401], sum[401], ar[401]; void rec(ll s, ll e) { if (s == e) { dp[s][e] = 0; return; } if (dp[s][e] != -1) { return; } dp[s][e] = inf; for (int i = s; i <= e; i++) { rec(s, i); rec(i + 1, e); dp[s][e] = min(dp[s][e], dp[s][i] + dp[i + 1][e] + sum[e] - sum[s - 1]); // cout << s << " " << e << " " << sum[e] - sum[s-1] << nl ; } return; } void solve() { memset(dp, -1, sizeof(dp)); ll n; cin >> n; beg1(i, n + 1) cin >> ar[i]; sum[0] = 0; beg1(i, n + 1) sum[i] = sum[i - 1] + ar[i]; rec(1, n); cout << dp[1][n]; } //---------------------------------------------------DRIVER---------------------------------------------------------- int main() { fast; int t = 1; // cin >> t ; while (t--) { solve(); } return 0; }
/*------U Have To DO It------*/ /* BY-> RicoProg */ /* ___ __|__| | |___ */ #include <bits/stdc++.h> using namespace std; //---------------------------------------------------MACROS---------------------------------------------------------- #define ll long long #define ld long double #define beg(i, n) for (ll i = 0; i < n; i++) #define beg1(i, n) for (ll i = 1; i < n; i++) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL) #define pb push_back #define mp make_pair #define nl "\n" #define vec(v, n) vector<ll> v(n) #define all(x) x.begin(), x.end() #define pii pair<int, int> #define pll pair<ll, ll> #define mii map<int, int> #define mll map<ll, ll> #define msi map<string, int> #define mci map<char, int> #define f1 first #define f2 second #define inf 1e18 + 10 //---------------------------------------------------GLOBAL---------------------------------------------------------- const ll MOD = 1e9 + 7; const ll MAX = 1e5 + 7; //---------------------------------------------------TEMPLATES------------------------------------------------------- void add_self(ll &a, ll b) { a += b; if (a >= MOD) { a -= MOD; } } void sub_self(ll &a, ll b) { a -= b; if (a < 0) { a += MOD; } } void merge_self(vector<ll> &a, vector<ll> b) { ll s = a.size(); beg(i, s) a[i] += b[i]; } void set_self(vector<ll> &a, ll n, ll s) { beg(i, s) a[i] = n; } ll factors() {} //---------------------------------------------------FUNCTIONS------------------------------------------------------- ll dp[401][401], sum[401], ar[401]; void rec(ll s, ll e) { if (s == e) { dp[s][e] = 0; return; } if (dp[s][e] != -1) { return; } dp[s][e] = inf; for (int i = s; i < e; i++) { rec(s, i); rec(i + 1, e); dp[s][e] = min(dp[s][e], dp[s][i] + dp[i + 1][e] + sum[e] - sum[s - 1]); // cout << s << " " << e << " " << sum[e] - sum[s-1] << nl ; } return; } void solve() { memset(dp, -1, sizeof(dp)); ll n; cin >> n; beg1(i, n + 1) cin >> ar[i]; sum[0] = 0; beg1(i, n + 1) sum[i] = sum[i - 1] + ar[i]; rec(1, n); cout << dp[1][n]; } //---------------------------------------------------DRIVER---------------------------------------------------------- int main() { fast; int t = 1; // cin >> t ; while (t--) { solve(); } return 0; }
replace
74
75
74
75
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; typedef pair<double, double> pdd; #define MEM(a, b) memset(a, (b), sizeof(a)) #define SZ(i) ll(i.size()) #define FOR(i, j, k, in) for (ll i = j; i < k; i += in) #define RFOR(i, j, k, in) for (ll i = j; i >= k; i -= in) #define REP(i, j) FOR(i, 0, j, 1) #define REP1(i, j) FOR(i, 1, j + 1, 1) #define RREP(i, j) RFOR(i, j, 0, 1) #define ALL(_a) _a.begin(), _a.end() #define mp make_pair #define pb push_back #define eb emplace_back #define X first #define Y second #ifdef tmd #define debug(...) \ do { \ fprintf(stderr, "%s - %d (%s) = ", __PRETTY_FUNCTION__, __LINE__, \ #__VA_ARGS__); \ _do(__VA_ARGS__); \ } while (0) template <typename T> void _do(T &&_x) { cerr << _x << endl; } template <typename T, typename... S> void _do(T &&_x, S &&..._t) { cerr << _x << " ,"; _do(_t...); } template <typename _a, typename _b> ostream &operator<<(ostream &_s, const pair<_a, _b> &_p) { return _s << "(" << _p.X << "," << _p.Y << ")"; } template <typename It> ostream &_OUTC(ostream &_s, It _ita, It _itb) { _s << "{"; for (It _it = _ita; _it != _itb; _it++) { _s << (_it == _ita ? "" : ",") << *_it; } _s << "}"; return _s; } template <typename _a> ostream &operator<<(ostream &_s, vector<_a> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _a> ostream &operator<<(ostream &_s, set<_a> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _a, typename _b> ostream &operator<<(ostream &_s, map<_a, _b> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _t> void pary(_t _a, _t _b) { _OUTC(cerr, _a, _b); cerr << endl; } #define IOS() #else #define debug(...) #define pary(...) #define endl '\n' #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0) #endif template <class T> inline bool cmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool cmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const ll MOD = 1000000007; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll MAXN = 409; const ll MAXLG = __lg(MAXN) + 2; ll n, a[MAXN]; ll dp[MAXN][MAXN], sum[MAXN][MAXN]; /********** Good Luck :) **********/ int main() { IOS(); cin >> n; REP(i, n) { cin >> a[i]; } MEM(dp, INF); REP(l, n) { dp[l][l] = 0; sum[l][l] = a[l]; } REP1(len, n - 1) { REP(l, n) { for (ll t = l; t < l + len; t++) { sum[l][len + l] = sum[l][t] + sum[t + 1][l + len]; dp[l][len + l] = min(dp[l][len + l], dp[l][t] + dp[t + 1][l + len] + sum[l][len + l]); } } } cout << dp[0][n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; typedef pair<double, double> pdd; #define MEM(a, b) memset(a, (b), sizeof(a)) #define SZ(i) ll(i.size()) #define FOR(i, j, k, in) for (ll i = j; i < k; i += in) #define RFOR(i, j, k, in) for (ll i = j; i >= k; i -= in) #define REP(i, j) FOR(i, 0, j, 1) #define REP1(i, j) FOR(i, 1, j + 1, 1) #define RREP(i, j) RFOR(i, j, 0, 1) #define ALL(_a) _a.begin(), _a.end() #define mp make_pair #define pb push_back #define eb emplace_back #define X first #define Y second #ifdef tmd #define debug(...) \ do { \ fprintf(stderr, "%s - %d (%s) = ", __PRETTY_FUNCTION__, __LINE__, \ #__VA_ARGS__); \ _do(__VA_ARGS__); \ } while (0) template <typename T> void _do(T &&_x) { cerr << _x << endl; } template <typename T, typename... S> void _do(T &&_x, S &&..._t) { cerr << _x << " ,"; _do(_t...); } template <typename _a, typename _b> ostream &operator<<(ostream &_s, const pair<_a, _b> &_p) { return _s << "(" << _p.X << "," << _p.Y << ")"; } template <typename It> ostream &_OUTC(ostream &_s, It _ita, It _itb) { _s << "{"; for (It _it = _ita; _it != _itb; _it++) { _s << (_it == _ita ? "" : ",") << *_it; } _s << "}"; return _s; } template <typename _a> ostream &operator<<(ostream &_s, vector<_a> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _a> ostream &operator<<(ostream &_s, set<_a> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _a, typename _b> ostream &operator<<(ostream &_s, map<_a, _b> &_c) { return _OUTC(_s, ALL(_c)); } template <typename _t> void pary(_t _a, _t _b) { _OUTC(cerr, _a, _b); cerr << endl; } #define IOS() #else #define debug(...) #define pary(...) #define endl '\n' #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0) #endif template <class T> inline bool cmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool cmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const ll MOD = 1000000007; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll MAXN = 409; const ll MAXLG = __lg(MAXN) + 2; ll n, a[MAXN]; ll dp[MAXN][MAXN], sum[MAXN][MAXN]; /********** Good Luck :) **********/ int main() { IOS(); cin >> n; REP(i, n) { cin >> a[i]; } MEM(dp, INF); REP(l, n) { dp[l][l] = 0; sum[l][l] = a[l]; } REP1(len, n - 1) { for (ll l = 0; l + len < n; l++) { for (ll t = l; t < l + len; t++) { sum[l][len + l] = sum[l][t] + sum[t + 1][l + len]; dp[l][len + l] = min(dp[l][len + l], dp[l][t] + dp[t + 1][l + len] + sum[l][len + l]); } } } cout << dp[0][n - 1] << endl; return 0; }
replace
94
95
94
95
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e2 + 5; const int MAXNN = 1e5 + 5; const long long INF = 1e18 + 7; #define ll long long #define pb push_back #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define F first #define S second ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } ll MOD(ll a) { return ((a % INF) + INF) % INF; } ll inv(ll a) { return poww(a, INF - 2, INF); } ll a[MAXN], dp[MAXN][MAXN], sums[MAXN]; int main() { fast_io; // cout << fixed << setprecision(15); int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) sums[i] = sums[i - 1] + a[i]; for (int len = 1; len <= n; ++len) { for (int l = 1; l <= n - len + 1; ++l) { int r = l + len; dp[l][r] = INF; for (int f = l; f <= r - 1; ++f) { dp[l][r] = min(dp[l][r], sums[r] - sums[l - 1] + dp[l][f] + dp[f + 1][r]); } } } cout << dp[1][n]; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 4e2 + 5; const long long INF = 1e18 + 7; #define ll long long #define pb push_back #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define F first #define S second ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } ll MOD(ll a) { return ((a % INF) + INF) % INF; } ll inv(ll a) { return poww(a, INF - 2, INF); } ll a[MAXN], dp[MAXN][MAXN], sums[MAXN]; int main() { fast_io; // cout << fixed << setprecision(15); int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) sums[i] = sums[i - 1] + a[i]; for (int len = 1; len <= n; ++len) { for (int l = 1; l <= n - len + 1; ++l) { int r = l + len; dp[l][r] = INF; for (int f = l; f <= r - 1; ++f) { dp[l][r] = min(dp[l][r], sums[r] - sums[l - 1] + dp[l][f] + dp[f + 1][r]); } } } cout << dp[1][n]; }
replace
3
5
3
4
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(a) (a).begin(), (a).end() #define forn(i, a, b) for (int i = a; i <= b; i++) #define ff first #define ss second #define pb push_back #define mp make_pair #define rc(s) return cout << s, 0 #define rcc(s) cout << s, exit(0) #define er erase #define in insert #define pi pair<int, int> #define sz(x) (int)((x).size()) #define int long long const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const ll inf = 0x3f3f3f3f3f3f3f; const ll mod = 1e9 + 7; const int N = 500 + 11; int dp[N][N][2], a[N], n, x; // vector<int>v[N]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int j = 1; j <= n; j++) { for (int i = 1; i <= n; i++) { if (j == 1) { dp[i][i][0] = a[i]; continue; } if (j == 2) { dp[i][i + 1][0] = a[i] + a[i + 1]; dp[i][i + 1][1] = a[i] + a[i + 1]; continue; } int l = i, r = i + j - 1; dp[l][r][1] = 3e18; for (int k = i; k < r; k++) { int val = dp[i][k][0] + dp[k + 1][r][0]; int price = dp[i][k][1] + dp[k + 1][r][1] + val; if (dp[l][r][1] > price) { dp[l][r][1] = price; dp[l][r][0] = val; } } } } rc(dp[1][n][1]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(a) (a).begin(), (a).end() #define forn(i, a, b) for (int i = a; i <= b; i++) #define ff first #define ss second #define pb push_back #define mp make_pair #define rc(s) return cout << s, 0 #define rcc(s) cout << s, exit(0) #define er erase #define in insert #define pi pair<int, int> #define sz(x) (int)((x).size()) #define int long long const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const ll inf = 0x3f3f3f3f3f3f3f; const ll mod = 1e9 + 7; const int N = 500 + 11; int dp[N][N][2], a[N], n, x; // vector<int>v[N]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int j = 1; j <= n; j++) { for (int i = 1; i <= n; i++) { if (j == 1) { dp[i][i][0] = a[i]; continue; } if (j == 2) { dp[i][i + 1][0] = a[i] + a[i + 1]; dp[i][i + 1][1] = a[i] + a[i + 1]; continue; } int l = i, r = i + j - 1; dp[l][r][1] = 3e18; if (r > n) continue; for (int k = i; k < r; k++) { int val = dp[i][k][0] + dp[k + 1][r][0]; int price = dp[i][k][1] + dp[k + 1][r][1] + val; if (dp[l][r][1] > price) { dp[l][r][1] = price; dp[l][r][0] = val; } } } } rc(dp[1][n][1]); return 0; }
insert
52
52
52
54
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define oo 666666666 #define ll long long #define ld long double int n; ll DP[303][303]; ll pf[303]; ll A[303]; ll solve(int l, int r) { if (l >= r) return 0; if (l + 1 == r) return A[l] + A[r]; if (DP[l][r] != 0) return DP[l][r]; DP[l][r] = LLONG_MAX; for (int k = l; k <= r; k++) DP[l][r] = min(DP[l][r], pf[k] - pf[l - 1] + pf[r] - pf[k] + solve(l, k) + solve(k + 1, r)); return DP[l][r]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> A[i], pf[i] = pf[i - 1] + A[i]; cout << solve(1, n); }
#include <bits/stdc++.h> using namespace std; #define oo 666666666 #define ll long long #define ld long double int n; ll DP[404][404]; ll pf[404]; ll A[404]; ll solve(int l, int r) { if (l >= r) return 0; if (l + 1 == r) return A[l] + A[r]; if (DP[l][r] != 0) return DP[l][r]; DP[l][r] = LLONG_MAX; for (int k = l; k <= r; k++) DP[l][r] = min(DP[l][r], pf[k] - pf[l - 1] + pf[r] - pf[k] + solve(l, k) + solve(k + 1, r)); return DP[l][r]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> A[i], pf[i] = pf[i - 1] + A[i]; cout << solve(1, n); }
replace
7
10
7
10
0
p03173
C++
Time Limit Exceeded
#include <cstdio> #include <iostream> using namespace std; const long long oo = 1e16; long long a[500]; long long s[500]; long long f[500][500]; bool c[500][500]; int n; void inp() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; s[i] = s[i - 1] + a[i]; } } long long tryyy(int l, int r) { if (l == r) return 0; if (c[l][r]) return f[l][r]; long long tmp = oo; for (int m = l; m < r; m++) tmp = min(tmp, tryyy(l, m) + tryyy(m + 1, r)); tmp += s[r] - s[l - 1]; f[l][r] = tmp; return tmp; } int main() { inp(); cout << tryyy(1, n); return 0; }
#include <cstdio> #include <iostream> using namespace std; const long long oo = 1e16; long long a[500]; long long s[500]; long long f[500][500]; bool c[500][500]; int n; void inp() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; s[i] = s[i - 1] + a[i]; } } long long tryyy(int l, int r) { if (l == r) return 0; if (c[l][r]) return f[l][r]; long long tmp = oo; c[l][r] = true; for (int m = l; m < r; m++) tmp = min(tmp, tryyy(l, m) + tryyy(m + 1, r)); tmp += s[r] - s[l - 1]; f[l][r] = tmp; return tmp; } int main() { inp(); cout << tryyy(1, n); return 0; }
insert
25
25
25
26
TLE
p03173
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // const long double PIL = 3.141592653589793238462643383279502884L; // const double PI = 3.14159265358979323846; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<pair<int, int>> vii; #define sz(a) int((a).size()) #define all(c) (c).begin(), (c).end() const ll INF = 1e18; const int N = 401; int a[N]; ll ps[N], dp[N][N]; int main() { //~ ios::sync_with_stdio(0); //~ cin.tie(0); int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dp[i][j] = INF; for (int i = 1; i <= n; i++) dp[i][i] = 0; for (int x = 1; x < n; x++) { for (int i = 1; i <= n; i++) { for (int j = i; j < i + x; j++) { ll tmp = dp[i][j] + dp[j + 1][i + x] + ps[i + x] - ps[i - 1]; dp[i][i + x] = min(dp[i][i + x], tmp); } } } printf("%lld\n", dp[1][n]); }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // const long double PIL = 3.141592653589793238462643383279502884L; // const double PI = 3.14159265358979323846; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<pair<int, int>> vii; #define sz(a) int((a).size()) #define all(c) (c).begin(), (c).end() const ll INF = 1e18; const int N = 401; int a[N]; ll ps[N], dp[N][N]; int main() { //~ ios::sync_with_stdio(0); //~ cin.tie(0); int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dp[i][j] = INF; for (int i = 1; i <= n; i++) dp[i][i] = 0; for (int x = 1; x < n; x++) { for (int i = 1; i <= n - x; i++) { for (int j = i; j < i + x; j++) { ll tmp = dp[i][j] + dp[j + 1][i + x] + ps[i + x] - ps[i - 1]; dp[i][i + x] = min(dp[i][i + x], tmp); } } } printf("%lld\n", dp[1][n]); }
replace
51
52
51
52
0
p03173
C++
Runtime Error
#include <iostream> using namespace std; long long b[401][401]; long long suma[401]; int a[401]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; suma[0] = a[0]; for (int i = 1; i < n; i++) { suma[i] = suma[i - 1] + a[i]; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (i == 0) { b[j][j] = 0; continue; } long long min1 = 9223372036854775807LL; for (int k = 0; k < i; k++) { long long x = b[j][j + k] + b[j + k + 1][j + i] + suma[j + i] - (j == 0 ? 0 : suma[j - 1]); if (x < min1) min1 = x; } b[j][j + i] = min1; } cout << b[0][n - 1]; return 0; }
#include <iostream> using namespace std; long long b[401][401]; long long suma[401]; int a[401]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; suma[0] = a[0]; for (int i = 1; i < n; i++) { suma[i] = suma[i - 1] + a[i]; } for (int i = 0; i < n; i++) for (int j = 0; j + i < n; j++) { if (i == 0) { b[j][j] = 0; continue; } long long min1 = 9223372036854775807LL; for (int k = 0; k < i; k++) { long long x = b[j][j + k] + b[j + k + 1][j + i] + suma[j + i] - (j == 0 ? 0 : suma[j - 1]); if (x < min1) min1 = x; } b[j][j + i] = min1; } cout << b[0][n - 1]; return 0; }
replace
17
18
17
18
0