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
p02634
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> #define DEBUG 1 using namespace std; constexpr int kMod = 998244353; typedef long long LL; int main() { int A, B, C, D; cin >> A >> B >> C >> D; vector<vector<LL>> dp(C + 1, vector<LL>(D + 1, 0)); dp[C][D] = 1; for (int c = D - 1; c >= B; --c) { dp[C][c] = C * dp[C][c + 1]; dp[C][c] %= kMod; } for (LL r = C - 1; r >= A; --r) { for (LL c = B; c <= D; ++c) { dp[r][c] = c * dp[r + 1][c]; dp[r][c] %= kMod; LL k = 1; for (int i = c + 1; i <= D; ++i) { k = (k * r) % kMod; dp[r][c] += k * dp[r + 1][i]; dp[r][c] %= kMod; } // cout << "dp[" << r << "][" << c << "] = " << dp[r][c] << endl; } } cout << dp[A][B] << endl; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> #define DEBUG 1 using namespace std; constexpr int kMod = 998244353; typedef long long LL; int main() { int A, B, C, D; cin >> A >> B >> C >> D; vector<vector<LL>> dp(C + 1, vector<LL>(D + 1, 0)); dp[C][D] = 1; for (int c = D - 1; c >= B; --c) { dp[C][c] = C * dp[C][c + 1]; dp[C][c] %= kMod; } for (LL r = C - 1; r >= A; --r) { LL S = 0; dp[r][D] = (D * dp[r + 1][D]) % kMod; for (LL c = D - 1; c >= B; --c) { S = (r * (S + dp[r + 1][c + 1])) % kMod; dp[r][c] = (c * dp[r + 1][c] + S) % kMod; } } cout << dp[A][B] << endl; }
replace
28
38
28
33
TLE
p02634
C++
Runtime Error
#include <bits/stdc++.h> /* Uncomment only if you are not using GNU compiler-> */ #include <algorithm> #include <chrono> #include <iomanip> #include <map> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <thread> #include <vector> /**/ // define #define rep(i, n) for (int i = 0; i < n; i++) #define repu(i, k, n) for (int i = k; i <= n; i++) #define repd(i, k, n) for (int i = k; i >= n; i--) #define se second #define fi first #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() // typedef typedef long long int ll; typedef long double ld; typedef std::pair<int, int> pii; typedef std::vector<int> vi; typedef std::vector<pii> vii; typedef std::vector<ll> vll; typedef std::vector<std::pair<pii, int>> vpp; const long long MOD = 998244353; const long long inf = 1e18; using namespace std; ll modpow(ll x, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } ll power(ll x, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * x; x = x * x; n >>= 1; } return res; } void solve() { int a, b, c, d; cin >> a >> b >> c >> d; vector<pair<pair<ll, ll>, pair<ll, ll>>> dp[c + 1]; repu(i, a, c) { rep(j, d + 1) { dp[i].pb(mp(mp(0, 0), mp(0, 0))); } } ll a1 = a, b1 = b; dp[a][b] = mp(mp(1, 0), mp(1, 0)); repu(i, a + 1, c) { dp[i][b].fi.fi = dp[i - 1][b].fi.fi * b1; dp[i][b].fi.fi %= MOD; dp[i][b].se.fi = dp[i][b].fi.fi; // dp[i][b]=b*dp[i-1][b]; // dp[i][b]%=MOD; } repu(i, b + 1, d) { dp[a][i].se.fi = dp[a][i - 1].se.fi * a1; dp[a][i].se.fi %= MOD; dp[a][i].fi.fi = dp[a][i].se.fi; // dp[a][i]=a*dp[a][i-1]; // dp[a][i]%=MOD; } // fi -> topmost row, se-> topmost column repu(i, a + 1, c) { repu(j, b + 1, d) { ll i1 = i, j1 = j; dp[i][j].fi.fi = dp[i - 1][j].se.fi * (j1) + dp[i - 1][j].se.se * (j1); dp[i][j].fi.se = dp[i][j - 1].fi.fi + dp[i][j - 1].fi.se * (i1); dp[i][j].se.fi = dp[i][j - 1].fi.se * (i1) + dp[i][j - 1].fi.fi * (i1); dp[i][j].se.se = dp[i - 1][j].se.fi + dp[i - 1][j].se.se * (j1); // dp[i][j]=(j1-1)*dp[i-1][j]+(i1-1)*dp[i][j-1]; // dp[i][j]/=2; // dp[i][j]+=dp[i][j-1]+dp[i-1][j]; // dp[i][j]%=MOD; dp[i][j].fi.fi %= MOD; dp[i][j].fi.se %= MOD; dp[i][j].se.fi %= MOD; dp[i][j].se.se %= MOD; } } // int xx=2, yy=2; // cout<<dp[xx][yy].fi.fi<<" "<<dp[xx][yy].fi.se<<" "<<dp[xx][yy].se.fi<<" // "<<dp[xx][yy].se.se<<endl; ll ans = dp[c][d].fi.fi + dp[c][d].fi.se + dp[c][d].se.fi + dp[c][d].se.se; if (ans % 2 == 1) { vi v(2); cout << v[3]; cout << 5 / 0; } ans /= 2LL; ans %= MOD; cout << ans << endl; } int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("oo.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(NULL); int t = 1, tt = 1; // cin>>t; while (tt <= t) { // cout<<"Case #"<<tt<<": "; solve(); tt++; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> /* Uncomment only if you are not using GNU compiler-> */ #include <algorithm> #include <chrono> #include <iomanip> #include <map> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <thread> #include <vector> /**/ // define #define rep(i, n) for (int i = 0; i < n; i++) #define repu(i, k, n) for (int i = k; i <= n; i++) #define repd(i, k, n) for (int i = k; i >= n; i--) #define se second #define fi first #define pb push_back #define mp make_pair #define all(v) v.begin(), v.end() // typedef typedef long long int ll; typedef long double ld; typedef std::pair<int, int> pii; typedef std::vector<int> vi; typedef std::vector<pii> vii; typedef std::vector<ll> vll; typedef std::vector<std::pair<pii, int>> vpp; const long long MOD = 998244353; const long long inf = 1e18; using namespace std; ll modpow(ll x, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } ll power(ll x, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * x; x = x * x; n >>= 1; } return res; } void solve() { int a, b, c, d; cin >> a >> b >> c >> d; vector<pair<pair<ll, ll>, pair<ll, ll>>> dp[c + 1]; repu(i, a, c) { rep(j, d + 1) { dp[i].pb(mp(mp(0, 0), mp(0, 0))); } } ll a1 = a, b1 = b; dp[a][b] = mp(mp(1, 0), mp(1, 0)); repu(i, a + 1, c) { dp[i][b].fi.fi = dp[i - 1][b].fi.fi * b1; dp[i][b].fi.fi %= MOD; dp[i][b].se.fi = dp[i][b].fi.fi; // dp[i][b]=b*dp[i-1][b]; // dp[i][b]%=MOD; } repu(i, b + 1, d) { dp[a][i].se.fi = dp[a][i - 1].se.fi * a1; dp[a][i].se.fi %= MOD; dp[a][i].fi.fi = dp[a][i].se.fi; // dp[a][i]=a*dp[a][i-1]; // dp[a][i]%=MOD; } // fi -> topmost row, se-> topmost column repu(i, a + 1, c) { repu(j, b + 1, d) { ll i1 = i, j1 = j; dp[i][j].fi.fi = dp[i - 1][j].se.fi * (j1) + dp[i - 1][j].se.se * (j1); dp[i][j].fi.se = dp[i][j - 1].fi.fi + dp[i][j - 1].fi.se * (i1); dp[i][j].se.fi = dp[i][j - 1].fi.se * (i1) + dp[i][j - 1].fi.fi * (i1); dp[i][j].se.se = dp[i - 1][j].se.fi + dp[i - 1][j].se.se * (j1); // dp[i][j]=(j1-1)*dp[i-1][j]+(i1-1)*dp[i][j-1]; // dp[i][j]/=2; // dp[i][j]+=dp[i][j-1]+dp[i-1][j]; // dp[i][j]%=MOD; dp[i][j].fi.fi %= MOD; dp[i][j].fi.se %= MOD; dp[i][j].se.fi %= MOD; dp[i][j].se.se %= MOD; } } // int xx=2, yy=2; // cout<<dp[xx][yy].fi.fi<<" "<<dp[xx][yy].fi.se<<" "<<dp[xx][yy].se.fi<<" // "<<dp[xx][yy].se.se<<endl; ll ans = dp[c][d].fi.fi + dp[c][d].fi.se + dp[c][d].se.fi + dp[c][d].se.se; if (ans % 2 == 1) { ans += MOD; } ans /= 2LL; ans %= MOD; cout << ans << endl; } int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("oo.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(NULL); int t = 1, tt = 1; // cin>>t; while (tt <= t) { // cout<<"Case #"<<tt<<": "; solve(); tt++; } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
replace
106
109
106
107
-11
p02634
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; const int inf = 1e9 + 7; const int N = 3 * 1e3 + 1; const int MOD = 998244353; int dp[N][N]; signed main() { ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); int a, b, c, d; cin >> a >> b >> c >> d; dp[a][b] = 1; for (int i = a; i <= c; i++) for (int j = b; j <= d; j++) { dp[i + 1][j] += j * dp[i][j]; dp[i][j + 1] += i * dp[i][j]; dp[i + 1][j] %= MOD; dp[i][j + 1] %= MOD; dp[i + 1][j + 1] -= (dp[i][j] * (i) * (j) % MOD); dp[i + 1][j + 1] += MOD; dp[i + 1][j + 1] %= MOD; } cout << dp[c][d] << '\n'; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; const int inf = 1e9 + 7; const int N = 3 * 1e3 + 111; const int MOD = 998244353; int dp[N][N]; signed main() { ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); int a, b, c, d; cin >> a >> b >> c >> d; dp[a][b] = 1; for (int i = a; i <= c; i++) for (int j = b; j <= d; j++) { dp[i + 1][j] += j * dp[i][j]; dp[i][j + 1] += i * dp[i][j]; dp[i + 1][j] %= MOD; dp[i][j + 1] %= MOD; dp[i + 1][j + 1] -= (dp[i][j] * (i) * (j) % MOD); dp[i + 1][j + 1] += MOD; dp[i + 1][j + 1] %= MOD; } cout << dp[c][d] << '\n'; return 0; }
replace
4
5
4
5
0
p02634
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; const long long mod = 998244353; int main(void) { long long A, B, C, D; cin >> A >> B >> C >> D; long long DP[3001][3001]; DP[A][B] = 1; long long ans = 0; long long N; long long i, j, t, k; for (i = A + 1; i < C + 1; i++) { DP[i][B] = DP[i - 1][B] * B % mod; } for (i = B + 1; i < D + 1; i++) { DP[A][i] = DP[A][i - 1] * A % mod; } N = (C - A) + (D - B) + 1; for (i = 2; i < N; i++) { long long s = max((long long)0, i - (D - B)); long long g = min(i, C - A + 1); for (j = s; j < g; j++) { int x = A + j; int y = B + i - j; DP[x][y] = (DP[x - 1][y] * y + DP[x][y - 1] * x) % mod; DP[x][y] -= DP[x - 1][y - 1] * (x - 1) * (y - 1); while (DP[x][y] < 0) { DP[x][y] += mod; } } } cout << DP[C][D] << endl; }
#include <algorithm> #include <iostream> using namespace std; const long long mod = 998244353; int main(void) { long long A, B, C, D; cin >> A >> B >> C >> D; long long DP[3001][3001]; DP[A][B] = 1; long long ans = 0; long long N; long long i, j, t, k; for (i = A + 1; i < C + 1; i++) { DP[i][B] = DP[i - 1][B] * B % mod; } for (i = B + 1; i < D + 1; i++) { DP[A][i] = DP[A][i - 1] * A % mod; } N = (C - A) + (D - B) + 1; for (i = 2; i < N; i++) { long long s = max((long long)0, i - (D - B)); long long g = min(i, C - A + 1); for (j = s; j < g; j++) { int x = A + j; int y = B + i - j; DP[x][y] = (DP[x - 1][y] * y + DP[x][y - 1] * x) % mod; DP[x][y] -= DP[x - 1][y - 1] * (x - 1) * (y - 1) % mod; while (DP[x][y] < 0) { DP[x][y] += mod; } } } cout << DP[C][D] << endl; }
replace
27
28
27
28
TLE
p02635
C++
Runtime Error
#include <bits/stdc++.h> #define long long long int using namespace std; // @author: pashka int mod = 998244353; struct mint { int value; constexpr mint() : value() {} mint(const long &x) { value = normalize(x); } static long normalize(const long &x) { long v = x % mod; if (v < 0) v += mod; return v; } bool operator==(const mint &x) { return value == x.value; }; mint operator+(const mint &x) { return value + x.value; }; mint operator*(const mint &x) { return (long)value * x.value; }; void operator+=(const mint &x) { value = normalize(value + x.value); }; }; mint d[301][301][301]; mint w[301][301][301]; int main() { ios::sync_with_stdio(false); string s; int k; cin >> s >> k; int n = s.size(); d[0][0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= n; j++) { for (int t = 0; t <= k; t++) { if (d[i][j][t] == 0 && w[i][j][t] == 0) continue; // cout << i << " " << j << " " << t << " " << // d[i][j][t].value << "\n"; if (s[i] == '1') { d[i + 1][j][t] += d[i][j][t]; if (j > 0) { w[i + 1][j - 1][t] += d[i][j][t] + w[i][j][t]; } } else { if (j < k && t < k) { d[i][j + 1][t + 1] += d[i][j][t]; } d[i + 1][j][t] += d[i][j][t] + w[i][j][t]; } } } } mint res = 0; for (int t = 0; t <= k; t++) { res += d[n][0][t]; res += w[n][0][t]; } cout << res.value << "\n"; return 0; }
#include <bits/stdc++.h> #define long long long int using namespace std; // @author: pashka int mod = 998244353; struct mint { int value; constexpr mint() : value() {} mint(const long &x) { value = normalize(x); } static long normalize(const long &x) { long v = x % mod; if (v < 0) v += mod; return v; } bool operator==(const mint &x) { return value == x.value; }; mint operator+(const mint &x) { return value + x.value; }; mint operator*(const mint &x) { return (long)value * x.value; }; void operator+=(const mint &x) { value = normalize(value + x.value); }; }; mint d[301][301][301]; mint w[301][301][301]; int main() { ios::sync_with_stdio(false); string s; int k; cin >> s >> k; int n = s.size(); k = min(k, n); d[0][0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= n; j++) { for (int t = 0; t <= k; t++) { if (d[i][j][t] == 0 && w[i][j][t] == 0) continue; // cout << i << " " << j << " " << t << " " << // d[i][j][t].value << "\n"; if (s[i] == '1') { d[i + 1][j][t] += d[i][j][t]; if (j > 0) { w[i + 1][j - 1][t] += d[i][j][t] + w[i][j][t]; } } else { if (j < k && t < k) { d[i][j + 1][t + 1] += d[i][j][t]; } d[i + 1][j][t] += d[i][j][t] + w[i][j][t]; } } } } mint res = 0; for (int t = 0; t <= k; t++) { res += d[n][0][t]; res += w[n][0][t]; } cout << res.value << "\n"; return 0; }
insert
34
34
34
35
-11
p02635
C++
Runtime Error
// #pragma GCC target("avx,avx2,fma") // #pragma GCC optimization ("Ofast") // #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // template <typename T> // using o_set = tree <T, null_type, less<T>, rb_tree_tag, // tree_order_statistics_node_update>; #define sfind(i) find_by_order(i) #define // ord(i) order_of_key(i) typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vii; typedef vector<pll> vll; #define mod 998244353 #define rep(i, a, b) for (int i = a; i < b; i++) #define repc(i, a, b, c) \ for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c) #define vi vector<int> #define vl vector<ll> #define _max(vec) (*max_element(all(vec))) #define _min(vec) (*min_element(all(vec))) #define fi first #define si second #define pb push_back #define all(name) name.begin(), name.end() #define ral(name) name.rbegin(), name.rend() #define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod) #define mod_in(a) power(a, mod - 2) #define ncr(n, r) \ (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL) #define deb(x) cerr << "[" << #x << " " << x << "]" << endl; #define endl '\n' //....???? // #define int ll vl ft(1, 1LL); /*======================================================================*/ ll power(ll x, ll y) { ll res = 1; x %= mod; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res % mod; } ll add(ll a, ll b) { a += b; return a >= mod ? a - mod : a; } ll sub(ll a, ll b) { a -= b; return (a < 0 ? a + mod : a); } ll mult(ll a, ll b) { return (a * b) % mod; } int dp[305][305][305]; void solve() { fact(1000); string s; int steps, n, len; cin >> s >> steps; n = s.length(); vi last; rep(i, 0, n) if (s[i] == '0') last.pb(i + 1); len = last.size(); steps = min(steps, n - len); dp[0][0][0] = 1; int prevj = 0; rep(i, 1, len + 1) { int l = last[i - 1]; for (int cur = n - len + i; cur >= l; cur--) { for (int j = prevj; j < cur; j++) { int lim = max(0, (cur - j) - (l - prevj)); for (int k = 0; k + lim <= steps; k++) { dp[i][cur][k + lim] = add(dp[i][cur][k + lim], dp[i - 1][j][k]); } } } prevj = l; } int ans = 0; for (int i = last[len - 1]; i <= n; i++) { rep(j, 0, steps + 1) { ans = add(ans, dp[len][i][j]); } } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } }
// #pragma GCC target("avx,avx2,fma") // #pragma GCC optimization ("Ofast") // #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // template <typename T> // using o_set = tree <T, null_type, less<T>, rb_tree_tag, // tree_order_statistics_node_update>; #define sfind(i) find_by_order(i) #define // ord(i) order_of_key(i) typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vii; typedef vector<pll> vll; #define mod 998244353 #define rep(i, a, b) for (int i = a; i < b; i++) #define repc(i, a, b, c) \ for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c) #define vi vector<int> #define vl vector<ll> #define _max(vec) (*max_element(all(vec))) #define _min(vec) (*min_element(all(vec))) #define fi first #define si second #define pb push_back #define all(name) name.begin(), name.end() #define ral(name) name.rbegin(), name.rend() #define fact(n) rep(i, 1, n + 1) ft.pb((ft[i - 1] * i) % mod) #define mod_in(a) power(a, mod - 2) #define ncr(n, r) \ (n >= r ? ((ft[n] * mod_in((ft[r] * ft[(n) - (r)]) % mod)) % mod) : 0LL) #define deb(x) cerr << "[" << #x << " " << x << "]" << endl; #define endl '\n' //....???? // #define int ll vl ft(1, 1LL); /*======================================================================*/ ll power(ll x, ll y) { ll res = 1; x %= mod; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res % mod; } ll add(ll a, ll b) { a += b; return a >= mod ? a - mod : a; } ll sub(ll a, ll b) { a -= b; return (a < 0 ? a + mod : a); } ll mult(ll a, ll b) { return (a * b) % mod; } int dp[305][305][305]; void solve() { fact(1000); string s; int steps, n, len; cin >> s >> steps; n = s.length(); vi last; rep(i, 0, n) if (s[i] == '0') last.pb(i + 1); len = last.size(); if (len == 0) { cout << 1; return; } steps = min(steps, n - len); dp[0][0][0] = 1; int prevj = 0; rep(i, 1, len + 1) { int l = last[i - 1]; for (int cur = n - len + i; cur >= l; cur--) { for (int j = prevj; j < cur; j++) { int lim = max(0, (cur - j) - (l - prevj)); for (int k = 0; k + lim <= steps; k++) { dp[i][cur][k + lim] = add(dp[i][cur][k + lim], dp[i - 1][j][k]); } } } prevj = l; } int ans = 0; for (int i = last[len - 1]; i <= n; i++) { rep(j, 0, steps + 1) { ans = add(ans, dp[len][i][j]); } } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } }
insert
71
71
71
75
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--) #define rrep(i, n) RREP(i, n - 1, 0) #define all(v) v.begin(), v.end() const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 998244353; int main() { cin.tie(0); ios::sync_with_stdio(false); string S; ll K; cin >> S >> K; int N = S.size(); int cnt = 0; vector<int> C; rep(i, N) { if (S[i] == '1') cnt++; else { C.push_back(cnt); cnt = 0; } } C.push_back(cnt); reverse(all(C)); int n = C.size(); int m = accumulate(all(C), 0); vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(m + 1, vector<ll>(m + 1))); dp[0][0][0] = 1; rep(i, n) { // i番目の区間 rep(j, m + 1) { // 取り出した回数 if (j > K) continue; rep(k, m + 1) { // 挿入した回数 int rem = j - k; if (rem < 0) continue; rep(l, rem + 1) { // ここで挿入する回数 (dp[i + 1][j][k + l] += dp[i][j][k]) %= mod; } rep(l, C[i] + 1) { // ここで取り出す回数 if (j + l > m) continue; if (l == 0) continue; (dp[i + 1][j + l][k] += dp[i][j][k]) %= mod; } } } } ll ans = 0; rep(i, K + 1)(ans += dp[n][i][i]) %= mod; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--) #define rrep(i, n) RREP(i, n - 1, 0) #define all(v) v.begin(), v.end() const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 998244353; int main() { cin.tie(0); ios::sync_with_stdio(false); string S; ll K; cin >> S >> K; int N = S.size(); int cnt = 0; vector<int> C; rep(i, N) { if (S[i] == '1') cnt++; else { C.push_back(cnt); cnt = 0; } } C.push_back(cnt); reverse(all(C)); int n = C.size(); int m = accumulate(all(C), 0); vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(m + 1, vector<ll>(m + 1))); dp[0][0][0] = 1; rep(i, n) { // i番目の区間 rep(j, m + 1) { // 取り出した回数 if (j > K) continue; rep(k, m + 1) { // 挿入した回数 int rem = j - k; if (rem < 0) continue; rep(l, rem + 1) { // ここで挿入する回数 (dp[i + 1][j][k + l] += dp[i][j][k]) %= mod; } rep(l, C[i] + 1) { // ここで取り出す回数 if (j + l > m) continue; if (l == 0) continue; (dp[i + 1][j + l][k] += dp[i][j][k]) %= mod; } } } } ll ans = 0; rep(i, min(K + 1, (ll)m + 1))(ans += dp[n][i][i]) %= mod; cout << ans << endl; return 0; }
replace
58
59
58
59
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long lol; const int mod = 998244353; inline int pls(int a, int b) { a += b - mod; return a + (a >> 31 & mod); } inline int mns(int a, int b) { a -= b; return a + (a >> 31 & mod); } inline void inc(int &a, int b) { a += b - mod; a += a >> 31 & mod; } inline void dec(int &a, int b) { a -= b; a += a >> 31 & mod; } inline int fpow(int base, int k) { int res = 1; while (k) { if (k & 1) res = (lol)res * base % mod; base = (lol)base * base % mod; k >>= 1; } return res; } int _w; const int N = 3e2 + 5; int f[N][N][N], n, k, sum[N], m, ss[N]; char str[N]; int main(void) { _w = scanf("%s%d", str + 1, &k); n = strlen(str + 1); str[n + 1] = '0'; for (int i = 0, r; i <= n; i = r) { r = i + 1; while (r <= n && str[r] != '0') ++r; sum[++m] = r - i - 1; } for (int i = m; i; --i) ss[i] = ss[i + 1] + sum[i]; f[0][0][0] = 1; for (int i = 1, s = 0; i <= m; s += sum[i], ++i) for (int j = s; j <= ss[1]; ++j) for (int l = 0; l <= k; ++l) if (f[i - 1][j][l]) for (int p = 0; p + j <= ss[1] && l + p - sum[i] <= k; ++p) { inc(f[i][j + p][l + max(p - sum[i], 0)], f[i - 1][j][l]); } int ans = 0; for (int i = 0; i <= k; ++i) inc(ans, f[m][ss[1]][i]); printf("%d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long lol; const int mod = 998244353; inline int pls(int a, int b) { a += b - mod; return a + (a >> 31 & mod); } inline int mns(int a, int b) { a -= b; return a + (a >> 31 & mod); } inline void inc(int &a, int b) { a += b - mod; a += a >> 31 & mod; } inline void dec(int &a, int b) { a -= b; a += a >> 31 & mod; } inline int fpow(int base, int k) { int res = 1; while (k) { if (k & 1) res = (lol)res * base % mod; base = (lol)base * base % mod; k >>= 1; } return res; } int _w; const int N = 3e2 + 5; int f[N][N][N], n, k, sum[N], m, ss[N]; char str[N]; int main(void) { _w = scanf("%s%d", str + 1, &k); n = strlen(str + 1); str[n + 1] = '0'; for (int i = 0, r; i <= n; i = r) { r = i + 1; while (r <= n && str[r] != '0') ++r; sum[++m] = r - i - 1; } for (int i = m; i; --i) ss[i] = ss[i + 1] + sum[i]; k = min(k, ss[1]); f[0][0][0] = 1; for (int i = 1, s = 0; i <= m; s += sum[i], ++i) for (int j = s; j <= ss[1]; ++j) for (int l = 0; l <= k; ++l) if (f[i - 1][j][l]) for (int p = 0; p + j <= ss[1] && l + p - sum[i] <= k; ++p) { inc(f[i][j + p][l + max(p - sum[i], 0)], f[i - 1][j][l]); } int ans = 0; for (int i = 0; i <= k; ++i) inc(ans, f[m][ss[1]][i]); printf("%d", ans); return 0; }
insert
51
51
51
52
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> #define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, lint> pll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } constexpr lint mod = 1e9 + 7; constexpr lint INF = mod * mod; constexpr int MAX = 100010; template <int_fast64_t MOD> struct modint { using i64 = int_fast64_t; i64 a; modint(const i64 a_ = 0) : a(a_) { if (a > MOD) a %= MOD; else if (a < 0) (a %= MOD) += MOD; } modint inv() { i64 t = 1, n = MOD - 2, x = a; while (n) { if (n & 1) (t *= x) %= MOD; (x *= x) %= MOD; n >>= 1; } modint ret(t); return ret; } bool operator==(const modint x) const { return a == x.a; } bool operator!=(const modint x) const { return a != x.a; } modint operator+(const modint x) const { return modint(*this) += x; } modint operator-(const modint x) const { return modint(*this) -= x; } modint operator*(const modint x) const { return modint(*this) *= x; } modint operator/(const modint x) const { return modint(*this) /= x; } modint operator^(const lint x) const { return modint(*this) ^= x; } modint &operator+=(const modint &x) { a += x.a; if (a >= MOD) a -= MOD; return *this; } modint &operator-=(const modint &x) { a -= x.a; if (a < 0) a += MOD; return *this; } modint &operator*=(const modint &x) { (a *= x.a) %= MOD; return *this; } modint &operator/=(modint x) { (a *= x.inv().a) %= MOD; return *this; } modint &operator^=(lint n) { i64 ret = 1; while (n) { if (n & 1) (ret *= a) %= MOD; (a *= a) %= MOD; n >>= 1; } a = ret; return *this; } modint operator-() const { return modint(0) - *this; } modint &operator++() { return *this += 1; } modint &operator--() { return *this -= 1; } bool operator<(const modint x) const { return a < x.a; } }; using mint = modint<998244353>; vector<mint> fact; vector<mint> revfact; void setfact(int n) { fact.resize(n + 1); revfact.resize(n + 1); fact[0] = 1; rep(i, n) fact[i + 1] = fact[i] * mint(i + 1); revfact[n] = fact[n].inv(); for (int i = n - 1; i >= 0; i--) revfact[i] = revfact[i + 1] * mint(i + 1); } mint getC(int n, int r) { if (n < r) return 0; return fact[n] * revfact[r] * revfact[n - r]; } mint dp[610][310][310]; int main() { string s; int K; cin >> s >> K; s = '0' + s + '0'; int n = s.size(); vector<int> a; int sum = 0; for (int l = 0; l < n - 1;) { int r = l + 1; while (r < n && s[r] == '1') ++r; a.push_back(r - l - 1); sum += r - l - 1; l = r; } dp[0][0][0] = 1; int t = 0; rep(i, a.size() - 1) { For(j, t, sum + 1) { rep(k, min(K + 1, sum + 1)) { if (dp[i][j][k] != 0) { for (int l = 0; l <= a[i] && j + l <= sum; ++l) { dp[i + 1][j + l][k] += dp[i][j][k]; } for (int l = a[i] + 1; j + l <= sum && k + l - a[i] <= K; ++l) { dp[i + 1][j + l][k + l - a[i]] += dp[i][j][k]; } } } } t += a[i]; } mint ans = 0; For(j, sum - *a.rbegin(), sum + 1) rep(k, K + 1) ans += dp[a.size() - 1][j][k]; printf("%lld\n", ans.a); }
#include <bits/stdc++.h> #define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, lint> pll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } constexpr lint mod = 1e9 + 7; constexpr lint INF = mod * mod; constexpr int MAX = 100010; template <int_fast64_t MOD> struct modint { using i64 = int_fast64_t; i64 a; modint(const i64 a_ = 0) : a(a_) { if (a > MOD) a %= MOD; else if (a < 0) (a %= MOD) += MOD; } modint inv() { i64 t = 1, n = MOD - 2, x = a; while (n) { if (n & 1) (t *= x) %= MOD; (x *= x) %= MOD; n >>= 1; } modint ret(t); return ret; } bool operator==(const modint x) const { return a == x.a; } bool operator!=(const modint x) const { return a != x.a; } modint operator+(const modint x) const { return modint(*this) += x; } modint operator-(const modint x) const { return modint(*this) -= x; } modint operator*(const modint x) const { return modint(*this) *= x; } modint operator/(const modint x) const { return modint(*this) /= x; } modint operator^(const lint x) const { return modint(*this) ^= x; } modint &operator+=(const modint &x) { a += x.a; if (a >= MOD) a -= MOD; return *this; } modint &operator-=(const modint &x) { a -= x.a; if (a < 0) a += MOD; return *this; } modint &operator*=(const modint &x) { (a *= x.a) %= MOD; return *this; } modint &operator/=(modint x) { (a *= x.inv().a) %= MOD; return *this; } modint &operator^=(lint n) { i64 ret = 1; while (n) { if (n & 1) (ret *= a) %= MOD; (a *= a) %= MOD; n >>= 1; } a = ret; return *this; } modint operator-() const { return modint(0) - *this; } modint &operator++() { return *this += 1; } modint &operator--() { return *this -= 1; } bool operator<(const modint x) const { return a < x.a; } }; using mint = modint<998244353>; vector<mint> fact; vector<mint> revfact; void setfact(int n) { fact.resize(n + 1); revfact.resize(n + 1); fact[0] = 1; rep(i, n) fact[i + 1] = fact[i] * mint(i + 1); revfact[n] = fact[n].inv(); for (int i = n - 1; i >= 0; i--) revfact[i] = revfact[i + 1] * mint(i + 1); } mint getC(int n, int r) { if (n < r) return 0; return fact[n] * revfact[r] * revfact[n - r]; } mint dp[610][310][310]; int main() { string s; int K; cin >> s >> K; s = '0' + s + '0'; int n = s.size(); vector<int> a; int sum = 0; for (int l = 0; l < n - 1;) { int r = l + 1; while (r < n && s[r] == '1') ++r; a.push_back(r - l - 1); sum += r - l - 1; l = r; } dp[0][0][0] = 1; int t = 0; rep(i, a.size() - 1) { For(j, t, sum + 1) { rep(k, min(K + 1, sum + 1)) { if (dp[i][j][k] != 0) { for (int l = 0; l <= a[i] && j + l <= sum; ++l) { dp[i + 1][j + l][k] += dp[i][j][k]; } for (int l = a[i] + 1; j + l <= sum && k + l - a[i] <= K; ++l) { dp[i + 1][j + l][k + l - a[i]] += dp[i][j][k]; } } } } t += a[i]; } mint ans = 0; For(j, sum - *a.rbegin(), sum + 1) rep(k, min(sum + 1, K + 1)) ans += dp[a.size() - 1][j][k]; printf("%lld\n", ans.a); }
replace
165
166
165
166
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; string S; ll N; ll K; vector<ll> A; ll M; vector<vector<vector<ll>>> Dp; const ll MOD = 998244353; ll Res; int main() { cin >> S >> K; N = S.size(); reverse(S.begin(), S.end()); ll now = 0; for (char c : S) { if (c == '0') { A.push_back(now); now = 0; } else { ++now; } } A.push_back(now); M = A.size(); Dp.resize(M + 1); for (auto &Dpi : Dp) { Dpi.resize(N + 1); for (auto &Dpij : Dpi) Dpij.resize(K + 1); } Dp[0][0][0] = 1; for (ll i = 0; i < M; ++i) { for (ll j = N; j >= 0; --j) { for (ll k = 0; k <= K; ++k) { (Dp[i + 1][j][k] += Dp[i][j][k]) %= MOD; if (j != N) { (Dp[i + 1][j][k] += Dp[i + 1][j + 1][k]) %= MOD; } } } for (ll j = 0; j <= N; ++j) { for (ll k = 0; k <= K; ++k) { for (ll a = 1; a <= A[i] && j + a <= N && k + a <= K; ++a) { (Dp[i + 1][j + a][k + a] += Dp[i][j][k]) %= MOD; } } } } for (ll k = 0; k <= K; ++k) { (Res += Dp[M][0][k]) %= MOD; } cout << Res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; string S; ll N; ll K; vector<ll> A; ll M; vector<vector<vector<ll>>> Dp; const ll MOD = 998244353; ll Res; int main() { cin >> S >> K; N = S.size(); K = min(N, K); reverse(S.begin(), S.end()); ll now = 0; for (char c : S) { if (c == '0') { A.push_back(now); now = 0; } else { ++now; } } A.push_back(now); M = A.size(); Dp.resize(M + 1); for (auto &Dpi : Dp) { Dpi.resize(N + 1); for (auto &Dpij : Dpi) Dpij.resize(K + 1); } Dp[0][0][0] = 1; for (ll i = 0; i < M; ++i) { for (ll j = N; j >= 0; --j) { for (ll k = 0; k <= K; ++k) { (Dp[i + 1][j][k] += Dp[i][j][k]) %= MOD; if (j != N) { (Dp[i + 1][j][k] += Dp[i + 1][j + 1][k]) %= MOD; } } } for (ll j = 0; j <= N; ++j) { for (ll k = 0; k <= K; ++k) { for (ll a = 1; a <= A[i] && j + a <= N && k + a <= K; ++a) { (Dp[i + 1][j + a][k + a] += Dp[i][j][k]) %= MOD; } } } } for (ll k = 0; k <= K; ++k) { (Res += Dp[M][0][k]) %= MOD; } cout << Res << endl; return 0; }
insert
17
17
17
18
0
p02635
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> #define rep(i, n) for (i = 0; i < n; i++) using namespace std; void reverse(string &s) { int l = 0, r = s.length() - 1; while (l < r) { swap(s[l], s[r]); l++; r--; } } int mod = 998244353; string s; int K; vector<int> a; vector<int> ra; int dp[310][310][310]; // dp[index][bsum][cost] = 何通り? int main() { int i; cin >> s >> K; reverse(s); int len = 0; rep(i, s.length()) { if (s[i] == '0') { a.push_back(len); len = 0; } else { len++; } } a.push_back(len); ra.resize(a.size() + 1); ra[0] = 0; rep(i, a.size()) { ra[i + 1] = ra[i] + a[i]; } dp[0][0][0] = 1; rep(i, a.size()) { for (int bsum = 0; bsum <= ra[i]; bsum++) { for (int cst = 0; cst <= ra[i]; cst++) { if (dp[i][bsum][cst] == 0) continue; for (int b = 0; bsum + b <= ra[i + 1]; b++) { int plus = a[i] - b; if (plus < 0) plus = 0; dp[i + 1][bsum + b][cst + plus] += dp[i][bsum][cst]; if (dp[i + 1][bsum + b][cst + plus] >= mod) { dp[i + 1][bsum + b][cst + plus] -= mod; } } } } } // dp[a.size()][*][≦K] int ans = 0; for (int bsum = ra[a.size()]; bsum <= ra[a.size()]; bsum++) { for (int cst = 0; cst <= K; cst++) { // cout << "dp[" << a.size() << ", " << bsum << ", " << cst << "] = " << // dp[a.size()][bsum][cst] << endl; ans += dp[a.size()][bsum][cst]; ans %= mod; } } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> #define rep(i, n) for (i = 0; i < n; i++) using namespace std; void reverse(string &s) { int l = 0, r = s.length() - 1; while (l < r) { swap(s[l], s[r]); l++; r--; } } int mod = 998244353; string s; int K; vector<int> a; vector<int> ra; int dp[310][310][310]; // dp[index][bsum][cost] = 何通り? int main() { int i; cin >> s >> K; reverse(s); K = min(K, 301); int len = 0; rep(i, s.length()) { if (s[i] == '0') { a.push_back(len); len = 0; } else { len++; } } a.push_back(len); ra.resize(a.size() + 1); ra[0] = 0; rep(i, a.size()) { ra[i + 1] = ra[i] + a[i]; } dp[0][0][0] = 1; rep(i, a.size()) { for (int bsum = 0; bsum <= ra[i]; bsum++) { for (int cst = 0; cst <= ra[i]; cst++) { if (dp[i][bsum][cst] == 0) continue; for (int b = 0; bsum + b <= ra[i + 1]; b++) { int plus = a[i] - b; if (plus < 0) plus = 0; dp[i + 1][bsum + b][cst + plus] += dp[i][bsum][cst]; if (dp[i + 1][bsum + b][cst + plus] >= mod) { dp[i + 1][bsum + b][cst + plus] -= mod; } } } } } // dp[a.size()][*][≦K] int ans = 0; for (int bsum = ra[a.size()]; bsum <= ra[a.size()]; bsum++) { for (int cst = 0; cst <= K; cst++) { // cout << "dp[" << a.size() << ", " << bsum << ", " << cst << "] = " << // dp[a.size()][bsum][cst] << endl; ans += dp[a.size()][bsum][cst]; ans %= mod; } } cout << ans << endl; return 0; }
insert
28
28
28
29
-11
p02635
C++
Runtime Error
#include <cstdio> #include <cstring> const long long mod = 998244353; char s[400]; long long f[400][400][400]; int count[400], sum[400]; long long preSum[400][400]; const int offset = 310; long long diaSum[800][400]; int max(int a, int b) { return a > b ? a : b; } void preWork(int p, int n, int m) { for (int i = 0; i <= n; i++) { preSum[i][0] = f[p][i][0]; for (int j = 1; j <= m; j++) { preSum[i][j] = preSum[i][j - 1] + f[p][i][j]; if (preSum[i][j] >= mod) preSum[i][j] -= mod; } } for (int i = -m; i <= n; i++) { int pos = i + offset; if (i >= 0) diaSum[pos][0] = f[p][i][0]; else diaSum[pos][0] = 0; for (int j = 1; j <= m; j++) { diaSum[pos][j] = diaSum[pos][j - 1]; int last = i + j; if (last <= n && last >= 0) diaSum[pos][j] += f[p][last][j]; if (diaSum[pos][j] >= mod) diaSum[pos][j] -= mod; } } } long long getPreSum(int i, int j, int n, int m) { if (i > n) return 0; else { if (j > m) j = m; return preSum[i][j]; } } int min(int a, int b) { return a > b ? b : a; } long long getDiaSum(int i, int j, int n, int m) { if (i > n || i < (-m)) return 0; else { if (j > m) j = m; return diaSum[i + offset][j]; } } int main() { int m; scanf("%s%d", s + 1, &m); int len = strlen(s + 1); s[++len] = '0'; int p = 1, size = 0; sum[0] = 0; while (p <= len) { int np = p; while (s[np] != '0') np++; size++; count[size] = np - p; sum[size] = sum[size - 1] + count[size]; p = np + 1; } // for(int i = 1; i <= size; i++) printf("%d ",count[i]); // printf("\n"); f[0][0][0] = 1; preWork(0, m, sum[size]); for (int i = 1; i <= size; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= sum[size]; k++) { f[i][j][k] = 0; if (k >= sum[i]) { if (k - count[i] >= 0) { long long add = getDiaSum(j + count[i] - k, k - count[i], m, sum[size]); f[i][j][k] = f[i][j][k] + add; if (f[i][j][k] >= mod) f[i][j][k] -= mod; } if (count[i] != 0) { long long add = getPreSum(j, k, m, sum[size]) - getPreSum(j, k - count[i], m, sum[size]); if (add < 0) add += mod; f[i][j][k] = f[i][j][k] + add; if (f[i][j][k] >= mod) f[i][j][k] -= mod; } } // if(f[i][j][k]<0) printf("f[%d][%d][%d] = %lld\n",i,j,k,f[i][j][k]); } } preWork(i, m, sum[size]); } long long ans = 0; for (int i = 0; i <= m; i++) ans = (ans + f[size][i][sum[size]]) % mod; printf("%lld\n", ans); return 0; } /*for(int l = 0; l <= k-count[i]; l++){ int u = k-l; int lastJ = j-max(u-count[i],0); if(lastJ>=0) f[i][j][k] = (f[i][j][k] + f[i-1][lastJ][l])%mod; }*/
#include <cstdio> #include <cstring> const long long mod = 998244353; char s[400]; long long f[400][400][400]; int count[400], sum[400]; long long preSum[400][400]; const int offset = 310; long long diaSum[800][400]; int max(int a, int b) { return a > b ? a : b; } void preWork(int p, int n, int m) { for (int i = 0; i <= n; i++) { preSum[i][0] = f[p][i][0]; for (int j = 1; j <= m; j++) { preSum[i][j] = preSum[i][j - 1] + f[p][i][j]; if (preSum[i][j] >= mod) preSum[i][j] -= mod; } } for (int i = -m; i <= n; i++) { int pos = i + offset; if (i >= 0) diaSum[pos][0] = f[p][i][0]; else diaSum[pos][0] = 0; for (int j = 1; j <= m; j++) { diaSum[pos][j] = diaSum[pos][j - 1]; int last = i + j; if (last <= n && last >= 0) diaSum[pos][j] += f[p][last][j]; if (diaSum[pos][j] >= mod) diaSum[pos][j] -= mod; } } } long long getPreSum(int i, int j, int n, int m) { if (i > n) return 0; else { if (j > m) j = m; return preSum[i][j]; } } int min(int a, int b) { return a > b ? b : a; } long long getDiaSum(int i, int j, int n, int m) { if (i > n || i < (-m)) return 0; else { if (j > m) j = m; return diaSum[i + offset][j]; } } int main() { int m; scanf("%s%d", s + 1, &m); int len = strlen(s + 1); s[++len] = '0'; int p = 1, size = 0; sum[0] = 0; while (p <= len) { int np = p; while (s[np] != '0') np++; size++; count[size] = np - p; sum[size] = sum[size - 1] + count[size]; p = np + 1; } // for(int i = 1; i <= size; i++) printf("%d ",count[i]); // printf("\n"); m = min(301, m); f[0][0][0] = 1; preWork(0, m, sum[size]); for (int i = 1; i <= size; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= sum[size]; k++) { f[i][j][k] = 0; if (k >= sum[i]) { if (k - count[i] >= 0) { long long add = getDiaSum(j + count[i] - k, k - count[i], m, sum[size]); f[i][j][k] = f[i][j][k] + add; if (f[i][j][k] >= mod) f[i][j][k] -= mod; } if (count[i] != 0) { long long add = getPreSum(j, k, m, sum[size]) - getPreSum(j, k - count[i], m, sum[size]); if (add < 0) add += mod; f[i][j][k] = f[i][j][k] + add; if (f[i][j][k] >= mod) f[i][j][k] -= mod; } } // if(f[i][j][k]<0) printf("f[%d][%d][%d] = %lld\n",i,j,k,f[i][j][k]); } } preWork(i, m, sum[size]); } long long ans = 0; for (int i = 0; i <= m; i++) ans = (ans + f[size][i][sum[size]]) % mod; printf("%lld\n", ans); return 0; } /*for(int l = 0; l <= k-count[i]; l++){ int u = k-l; int lastJ = j-max(u-count[i],0); if(lastJ>=0) f[i][j][k] = (f[i][j][k] + f[i-1][lastJ][l])%mod; }*/
insert
78
78
78
79
-11
p02635
C++
Runtime Error
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author majk */ #ifndef MAJK_LIB #define MAJK_LIB #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define x first #define y second typedef std::pair<int, int> pii; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<ui, ui> puu; template <typename T, typename U> std::istream &operator>>(std::istream &i, pair<T, U> &p) { i >> p.x >> p.y; return i; } template <typename T> std::istream &operator>>(std::istream &i, vector<T> &t) { for (auto &v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream &operator<<(std::ostream &o, const pair<T, U> &p) { o << p.x << ' ' << p.y; return o; } template <typename T> std::ostream &operator<<(std::ostream &o, const vector<T> &t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; ui logceil(ll x) { return x ? 8 * sizeof(ll) - __builtin_clzll(x) : 0; } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U> &p) const { return t(p.x) ^ (u(p.y) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F &f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F &f, double p = 1e-9) { ui r = 3 + (ui)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F &f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F &f, double p = 1e-9) { ui r = 3 + (ui)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; #endif #ifndef MOD_H #define MOD_H template <unsigned int N> class Field { typedef unsigned int ui; typedef unsigned long long ull; inline ui pow(ui a, ui p) { ui r = 1, e = a; while (p) { if (p & 1) { r = ((ull)r * e) % N; } e = ((ull)e * e) % N; p >>= 1; } return r; } /*extended GCD(slow):ll t=0,nt=1,r=N,nr=a;while(nr){ll * q=r/nr;t-=q*nt;swap(t,nt);r-=q*nr;swap(r,nr);}assert(r<=1);return(t<0)?t+N:t;*/ inline ui inv(ui a) { return pow(a, N - 2); } public: inline Field(int x = 0) : v(x < 0 ? N + x : x) {} inline Field<N> pow(int p) { return (*this) ^ p; } inline Field<N> operator^(int p) { return {(int)pow(v, (ui)p)}; } inline Field<N> &operator+=(const Field<N> &o) { if (v + o.v >= N) v += o.v - N; else v += o.v; return *this; } inline Field<N> &operator-=(const Field<N> &o) { if (v < o.v) v -= o.v - N; else v -= o.v; return *this; } inline Field<N> &operator*=(const Field<N> &o) { v = (ull)v * o.v % N; return *this; } inline Field<N> &operator/=(const Field<N> &o) { return *this *= inv(o.v); } inline Field<N> operator+(const Field<N> &o) const { Field<N> r{*this}; return r += o; } inline Field<N> operator-(const Field<N> &o) const { Field<N> r{*this}; return r -= o; } inline Field<N> operator*(const Field<N> &o) const { Field<N> r{*this}; return r *= o; } inline Field<N> operator/(const Field<N> &o) const { Field<N> r{*this}; return r /= o; } inline Field<N> operator-() { if (v) return {(int)(N - v)}; else return {0}; }; inline Field<N> &operator++() { ++v; if (v == N) v = 0; return *this; } inline Field<N> operator++(int) { Field<N> r{*this}; ++*this; return r; } inline Field<N> &operator--() { --v; if (v == -1) v = N - 1; return *this; } inline Field<N> operator--(int) { Field<N> r{*this}; --*this; return r; } inline bool operator==(const Field<N> &o) const { return o.v == v; } inline bool operator!=(const Field<N> &o) const { return o.v != v; } inline explicit operator ui() const { return v; } inline static vector<Field<N>> fact(int t) { vector<Field<N>> F(t + 1, 1); for (int i = 2; i <= t; ++i) { F[i] = F[i - 1] * i; } return F; } inline static vector<Field<N>> invfact(int t) { vector<Field<N>> F(t + 1, 1); Field<N> X{1}; for (int i = 2; i <= t; ++i) { X = X * i; } F[t] = 1 / X; for (int i = t - 1; i >= 2; --i) { F[i] = F[i + 1] * (i + 1); } return F; } private: ui v; }; template <unsigned int N> istream &operator>>(std::istream &is, Field<N> &f) { unsigned int v; is >> v; f = v; return is; } template <unsigned int N> ostream &operator<<(std::ostream &os, const Field<N> &f) { return os << (unsigned int)f; } template <unsigned int N> Field<N> operator+(int i, const Field<N> &f) { return Field<N>(i) + f; } template <unsigned int N> Field<N> operator-(int i, const Field<N> &f) { return Field<N>(i) - f; } template <unsigned int N> Field<N> operator*(int i, const Field<N> &f) { return Field<N>(i) * f; } template <unsigned int N> Field<N> operator/(int i, const Field<N> &f) { return Field<N>(i) / f; } typedef Field<1000000007> FieldMod; struct Ring { template <typename T> static T div(T p, T q, T N) { T t = 0, nt = 1, r = N, nr = q; while (nr != 0) { T q = r / nr; t -= q * nt; r -= q * nr; swap(t, nt); swap(r, nr); } t = (t < 0) ? t + N : t; r = (r < 0) ? r + N : r; if (gcd(p, N) % r != 0) { return 0; } return (t * p / r) % N; } }; #endif typedef Field<998244353> FF; class CShift { public: void solve(istream &cin, ostream &cout) { string S; cin >> S; int K; cin >> K; int N = S.size(); vector<int> A; int cnt = 0; for (int i = 0; i < N; ++i) { if (S[i] == '0') { A.push_back(cnt); cnt = 0; } else cnt++; } A.push_back(cnt); reverse(A.begin(), A.end()); int M = A.size(); vector3<FF> DP(M + 1, K + 1, K + 1, 0); DP[0][0][0] = 1; for (int i = 0; i < M; ++i) { int a = A[i]; for (int j = 0; j <= K; ++j) { vector<FF> Pref(K + 2, 0); for (int k = 0; k <= K; ++k) Pref[k + 1] = Pref[k] + DP[i][j][k]; for (int k = 0; k <= j; ++k) { for (int l = 1; l <= a; ++l) { if (l + j <= K) { DP[i + 1][j + l][k] += DP[i][j][k]; // store ones } } DP[i + 1][j][k] += Pref[k + 1]; } } } FF ans = 0; for (int i = 0; i <= K; ++i) { ans += DP[M][i][i]; } cout << ans << '\n'; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); CShift solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author majk */ #ifndef MAJK_LIB #define MAJK_LIB #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define x first #define y second typedef std::pair<int, int> pii; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<ui, ui> puu; template <typename T, typename U> std::istream &operator>>(std::istream &i, pair<T, U> &p) { i >> p.x >> p.y; return i; } template <typename T> std::istream &operator>>(std::istream &i, vector<T> &t) { for (auto &v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream &operator<<(std::ostream &o, const pair<T, U> &p) { o << p.x << ' ' << p.y; return o; } template <typename T> std::ostream &operator<<(std::ostream &o, const vector<T> &t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; ui logceil(ll x) { return x ? 8 * sizeof(ll) - __builtin_clzll(x) : 0; } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U> &p) const { return t(p.x) ^ (u(p.y) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F &f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F &f, double p = 1e-9) { ui r = 3 + (ui)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F &f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F &f, double p = 1e-9) { ui r = 3 + (ui)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; #endif #ifndef MOD_H #define MOD_H template <unsigned int N> class Field { typedef unsigned int ui; typedef unsigned long long ull; inline ui pow(ui a, ui p) { ui r = 1, e = a; while (p) { if (p & 1) { r = ((ull)r * e) % N; } e = ((ull)e * e) % N; p >>= 1; } return r; } /*extended GCD(slow):ll t=0,nt=1,r=N,nr=a;while(nr){ll * q=r/nr;t-=q*nt;swap(t,nt);r-=q*nr;swap(r,nr);}assert(r<=1);return(t<0)?t+N:t;*/ inline ui inv(ui a) { return pow(a, N - 2); } public: inline Field(int x = 0) : v(x < 0 ? N + x : x) {} inline Field<N> pow(int p) { return (*this) ^ p; } inline Field<N> operator^(int p) { return {(int)pow(v, (ui)p)}; } inline Field<N> &operator+=(const Field<N> &o) { if (v + o.v >= N) v += o.v - N; else v += o.v; return *this; } inline Field<N> &operator-=(const Field<N> &o) { if (v < o.v) v -= o.v - N; else v -= o.v; return *this; } inline Field<N> &operator*=(const Field<N> &o) { v = (ull)v * o.v % N; return *this; } inline Field<N> &operator/=(const Field<N> &o) { return *this *= inv(o.v); } inline Field<N> operator+(const Field<N> &o) const { Field<N> r{*this}; return r += o; } inline Field<N> operator-(const Field<N> &o) const { Field<N> r{*this}; return r -= o; } inline Field<N> operator*(const Field<N> &o) const { Field<N> r{*this}; return r *= o; } inline Field<N> operator/(const Field<N> &o) const { Field<N> r{*this}; return r /= o; } inline Field<N> operator-() { if (v) return {(int)(N - v)}; else return {0}; }; inline Field<N> &operator++() { ++v; if (v == N) v = 0; return *this; } inline Field<N> operator++(int) { Field<N> r{*this}; ++*this; return r; } inline Field<N> &operator--() { --v; if (v == -1) v = N - 1; return *this; } inline Field<N> operator--(int) { Field<N> r{*this}; --*this; return r; } inline bool operator==(const Field<N> &o) const { return o.v == v; } inline bool operator!=(const Field<N> &o) const { return o.v != v; } inline explicit operator ui() const { return v; } inline static vector<Field<N>> fact(int t) { vector<Field<N>> F(t + 1, 1); for (int i = 2; i <= t; ++i) { F[i] = F[i - 1] * i; } return F; } inline static vector<Field<N>> invfact(int t) { vector<Field<N>> F(t + 1, 1); Field<N> X{1}; for (int i = 2; i <= t; ++i) { X = X * i; } F[t] = 1 / X; for (int i = t - 1; i >= 2; --i) { F[i] = F[i + 1] * (i + 1); } return F; } private: ui v; }; template <unsigned int N> istream &operator>>(std::istream &is, Field<N> &f) { unsigned int v; is >> v; f = v; return is; } template <unsigned int N> ostream &operator<<(std::ostream &os, const Field<N> &f) { return os << (unsigned int)f; } template <unsigned int N> Field<N> operator+(int i, const Field<N> &f) { return Field<N>(i) + f; } template <unsigned int N> Field<N> operator-(int i, const Field<N> &f) { return Field<N>(i) - f; } template <unsigned int N> Field<N> operator*(int i, const Field<N> &f) { return Field<N>(i) * f; } template <unsigned int N> Field<N> operator/(int i, const Field<N> &f) { return Field<N>(i) / f; } typedef Field<1000000007> FieldMod; struct Ring { template <typename T> static T div(T p, T q, T N) { T t = 0, nt = 1, r = N, nr = q; while (nr != 0) { T q = r / nr; t -= q * nt; r -= q * nr; swap(t, nt); swap(r, nr); } t = (t < 0) ? t + N : t; r = (r < 0) ? r + N : r; if (gcd(p, N) % r != 0) { return 0; } return (t * p / r) % N; } }; #endif typedef Field<998244353> FF; class CShift { public: void solve(istream &cin, ostream &cout) { string S; cin >> S; int K; cin >> K; int N = S.size(); K = min(K, N); vector<int> A; int cnt = 0; for (int i = 0; i < N; ++i) { if (S[i] == '0') { A.push_back(cnt); cnt = 0; } else cnt++; } A.push_back(cnt); reverse(A.begin(), A.end()); int M = A.size(); vector3<FF> DP(M + 1, K + 1, K + 1, 0); DP[0][0][0] = 1; for (int i = 0; i < M; ++i) { int a = A[i]; for (int j = 0; j <= K; ++j) { vector<FF> Pref(K + 2, 0); for (int k = 0; k <= K; ++k) Pref[k + 1] = Pref[k] + DP[i][j][k]; for (int k = 0; k <= j; ++k) { for (int l = 1; l <= a; ++l) { if (l + j <= K) { DP[i + 1][j + l][k] += DP[i][j][k]; // store ones } } DP[i + 1][j][k] += Pref[k + 1]; } } } FF ans = 0; for (int i = 0; i <= K; ++i) { ans += DP[M][i][i]; } cout << ans << '\n'; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); CShift solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
insert
328
328
328
329
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: using ll = long long; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream &operator<<(std::ostream &out, const modnum &n) { return out << int(n); } friend std::istream &operator>>(std::istream &in, modnum &n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } friend modnum inv(const modnum &m) { return m.inv(); } modnum neg() const { modnum res; res.v = v ? MOD - v : 0; return res; } friend modnum neg(const modnum &m) { return m.neg(); } modnum operator-() const { return neg(); } modnum operator+() const { return modnum(*this); } modnum &operator++() { v++; if (v == MOD) v = 0; return *this; } modnum &operator--() { if (v == 0) v = MOD; v--; return *this; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= o.inv(); } friend modnum operator++(modnum &a, int) { modnum r = a; ++a; return r; } friend modnum operator--(modnum &a, int) { modnum r = a; --a; return r; } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } }; template <typename T> T pow(T a, long long b) { assert(b >= 0); T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; } using num = modnum<998244353>; vector<num> fact, ifact; void init() { int N = 1100000; fact = {1}; for (int i = 1; i < N; i++) fact.push_back(i * fact[i - 1]); ifact.resize(N); ifact.back() = 1 / fact.back(); for (int i = N - 1; i > 0; i--) ifact[i - 1] = i * ifact[i]; } num ncr(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); init(); string s; int k; cin >> s >> k; s += "0"; vector<int> cnt; int cur = 0; int ntot = 0; for (char c : s) { if (c == '1') { cur++; ntot++; } if (c == '0') { cnt.push_back(cur); cur = 0; } } vector<vector<num>> dp(ntot + 1, vector<num>(k + 1, 0)); dp[0][0] = 1; int ctot = 0; for (int c : cnt) { ctot += c; vector<vector<num>> z(ntot + 1, vector<num>(k + 1, 0)); for (int a = 0; a <= ntot; a++) { for (int b = 0; b <= k; b++) { for (int na = 0; a + na <= ntot; na++) { if (a + na < ctot) continue; int nb = b + max(na - c, 0); if (nb > k) continue; z[a + na][nb] += dp[a][b]; } } } dp = z; } num ans = 0; for (int i = 0; i <= k; i++) ans += dp[ntot][i]; cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: using ll = long long; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream &operator<<(std::ostream &out, const modnum &n) { return out << int(n); } friend std::istream &operator>>(std::istream &in, modnum &n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator==(const modnum &a, const modnum &b) { return a.v == b.v; } friend bool operator!=(const modnum &a, const modnum &b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } friend modnum inv(const modnum &m) { return m.inv(); } modnum neg() const { modnum res; res.v = v ? MOD - v : 0; return res; } friend modnum neg(const modnum &m) { return m.neg(); } modnum operator-() const { return neg(); } modnum operator+() const { return modnum(*this); } modnum &operator++() { v++; if (v == MOD) v = 0; return *this; } modnum &operator--() { if (v == 0) v = MOD; v--; return *this; } modnum &operator+=(const modnum &o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum &operator-=(const modnum &o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum &operator*=(const modnum &o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum &operator/=(const modnum &o) { return *this *= o.inv(); } friend modnum operator++(modnum &a, int) { modnum r = a; ++a; return r; } friend modnum operator--(modnum &a, int) { modnum r = a; --a; return r; } friend modnum operator+(const modnum &a, const modnum &b) { return modnum(a) += b; } friend modnum operator-(const modnum &a, const modnum &b) { return modnum(a) -= b; } friend modnum operator*(const modnum &a, const modnum &b) { return modnum(a) *= b; } friend modnum operator/(const modnum &a, const modnum &b) { return modnum(a) /= b; } }; template <typename T> T pow(T a, long long b) { assert(b >= 0); T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; } using num = modnum<998244353>; vector<num> fact, ifact; void init() { int N = 1100000; fact = {1}; for (int i = 1; i < N; i++) fact.push_back(i * fact[i - 1]); ifact.resize(N); ifact.back() = 1 / fact.back(); for (int i = N - 1; i > 0; i--) ifact[i - 1] = i * ifact[i]; } num ncr(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); init(); string s; int k; cin >> s >> k; s += "0"; vector<int> cnt; int cur = 0; int ntot = 0; for (char c : s) { if (c == '1') { cur++; ntot++; } if (c == '0') { cnt.push_back(cur); cur = 0; } } k = min(k, 400); vector<vector<num>> dp(ntot + 1, vector<num>(k + 1, 0)); dp[0][0] = 1; int ctot = 0; for (int c : cnt) { ctot += c; vector<vector<num>> z(ntot + 1, vector<num>(k + 1, 0)); for (int a = 0; a <= ntot; a++) { for (int b = 0; b <= k; b++) { for (int na = 0; a + na <= ntot; na++) { if (a + na < ctot) continue; int nb = b + max(na - c, 0); if (nb > k) continue; z[a + na][nb] += dp[a][b]; } } } dp = z; } num ans = 0; for (int i = 0; i <= k; i++) ans += dp[ntot][i]; cout << ans << '\n'; }
insert
166
166
166
167
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int MAX = 510000; const int MOD = 998244353; signed main() { string s; int K; cin >> s >> K; vector<int> c; c.push_back(0); int t = 0; for (auto &&i : s) { if (i == '0') c.push_back(0); else { c[c.size() - 1]++; t++; } } int dp[c.size() + 1][t + 1][301]; for (int i = 0; i <= t; i++) fill(dp[0][i], dp[0][i] + 301, 0); dp[0][0][0] = 1; int o = 0; for (int i = 1; i <= c.size(); i++) { o += c[i - 1]; for (int j = 0; j <= t; j++) fill(dp[i][j], dp[i][j] + 301, 0); for (int j = 0; j <= t; j++) for (int k = 0; k < 301; k++) for (int j2 = 0; j2 <= j; j2++) if (k - max(j - j2 - c[i - 1], 0ll) >= 0 && j >= o) dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j2][k - max(j - j2 - c[i - 1], 0ll)]) % MOD; } int ans = 0; for (int i = 0; i <= min(K, 301ll); i++) { ans = (ans + dp[c.size()][t][i]) % MOD; } // cerr<<dp[c.size()][t][i]<<endl;} cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long const int MAX = 510000; const int MOD = 998244353; signed main() { string s; int K; cin >> s >> K; vector<int> c; c.push_back(0); int t = 0; for (auto &&i : s) { if (i == '0') c.push_back(0); else { c[c.size() - 1]++; t++; } } int dp[c.size() + 1][t + 1][301]; for (int i = 0; i <= t; i++) fill(dp[0][i], dp[0][i] + 301, 0); dp[0][0][0] = 1; int o = 0; for (int i = 1; i <= c.size(); i++) { o += c[i - 1]; for (int j = 0; j <= t; j++) fill(dp[i][j], dp[i][j] + 301, 0); for (int j = 0; j <= t; j++) for (int k = 0; k < 301; k++) for (int j2 = 0; j2 <= j; j2++) if (k - max(j - j2 - c[i - 1], 0ll) >= 0 && j >= o) dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j2][k - max(j - j2 - c[i - 1], 0ll)]) % MOD; } int ans = 0; for (int i = 0; i <= min(K, 300ll); i++) { ans = (ans + dp[c.size()][t][i]) % MOD; } // cerr<<dp[c.size()][t][i]<<endl;} cout << ans << endl; }
replace
39
40
39
40
0
p02635
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int maxn = 300 + 10; typedef long long ll; const ll mod = 998244353; int tot, K; char s[maxn]; int a[maxn]; ll f[maxn][maxn][maxn]; ll ans; inline ll norm(const ll &x) { return x >= mod ? x - mod : x; } int main() { scanf("%s%d", s + 1, &K); int len = strlen(s + 1); reverse(s + 1, s + len + 1); int cur = 0; for (int i = 1; i <= len; ++i) if (s[i] == '0') { a[++tot] = cur; cur = 0; } else ++cur; a[++tot] = cur; f[0][0][0] = 1; for (int i = 1; i <= tot; ++i) for (int k = 0; k <= K; ++k) { for (int j = k; ~j; --j) f[i][j][k] = norm(f[i - 1][j][k] + f[i][j + 1][k]); for (int j = 0; j <= k; ++j) { /* for(int jd = j; jd <= k; ++jd) f[i][j][k] = (f[i][j][k] + f[i - 1][jd][k]) % mod; */ int lim = min(j, a[i]); for (int jd = 1; jd <= lim; ++jd) f[i][j][k] = norm(f[i][j][k] + f[i - 1][j - jd][k - jd]); } } for (int k = 0; k <= K; ++k) ans = norm(ans + f[tot][0][k]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 300 + 10; typedef long long ll; const ll mod = 998244353; int tot, K; char s[maxn]; int a[maxn]; ll f[maxn][maxn][maxn]; ll ans; inline ll norm(const ll &x) { return x >= mod ? x - mod : x; } int main() { scanf("%s%d", s + 1, &K); K = min(K, 300); int len = strlen(s + 1); reverse(s + 1, s + len + 1); int cur = 0; for (int i = 1; i <= len; ++i) if (s[i] == '0') { a[++tot] = cur; cur = 0; } else ++cur; a[++tot] = cur; f[0][0][0] = 1; for (int i = 1; i <= tot; ++i) for (int k = 0; k <= K; ++k) { for (int j = k; ~j; --j) f[i][j][k] = norm(f[i - 1][j][k] + f[i][j + 1][k]); for (int j = 0; j <= k; ++j) { /* for(int jd = j; jd <= k; ++jd) f[i][j][k] = (f[i][j][k] + f[i - 1][jd][k]) % mod; */ int lim = min(j, a[i]); for (int jd = 1; jd <= lim; ++jd) f[i][j][k] = norm(f[i][j][k] + f[i - 1][j - jd][k - jd]); } } for (int k = 0; k <= K; ++k) ans = norm(ans + f[tot][0][k]); cout << ans << endl; return 0; }
insert
13
13
13
14
TLE
p02635
C++
Runtime Error
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Ido Kessler */ #include <algorithm> #include <bitset> #include <cassert> #include <cstdint> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <unordered_map> #include <unordered_set> #include <vector> #include <cmath> #include <random> #include <tuple> using ll = long long int; using ull = unsigned long long int; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; using pi = std::pair<int, int>; using vpi = std::vector<pi>; using vvpi = std::vector<vpi>; using pll = std::pair<ll, ll>; using vpll = std::vector<pll>; using vvpll = std::vector<vpll>; #define all(x) (x).begin(), (x).end() #define fori(i, n) for (int i = 0; i < (int)(n); i++) #define rep(i, s, e) for (int i = (int)(s); i <= (int)(e); ++i) #define repr(i, s, e) for (int i = (int)(s); i >= (int)(e); --i) #define pb push_back template <typename T> void mk_unique(std::vector<T> &l) { if (!std::is_sorted(all(l))) std::sort(all(l)); auto it = std::unique(l.begin(), l.end()); auto d = std::distance(l.begin(), it); l.resize(d); } template <typename T> T &smax(T &l, const T &r) { if (l < r) l = r; return l; } template <typename T> T &smin(T &l, const T &r) { if (r < l) l = r; return l; } constexpr int MOD = 998244353; // 1000000007; namespace io { class input_reader { void throw_error() { throw std::runtime_error("No more inputs while reading number"); } template <typename T> inline void readIntegerValue(T &f) { bool positive = true; f = 0; char c; if (!in.get(c)) throw_error(); while (c < '0' || c > '9') { if (c == '-') positive = false; if (!in.get(c)) throw_error(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); if (!in.get(c)) break; } if (!positive) f *= -1; } public: std::istream &in; explicit input_reader(std::istream &in) : in(in) {} inline void read(int &f) { readIntegerValue(f); } inline void read(short &f) { readIntegerValue(f); } inline void read(long int &f) { readIntegerValue(f); } inline void read(long long int &f) { readIntegerValue(f); } inline void read(unsigned int &f) { readIntegerValue(f); } inline void read(unsigned short &f) { readIntegerValue(f); } inline void read(unsigned long int &f) { readIntegerValue(f); } inline void read(unsigned long long int &f) { readIntegerValue(f); } template <typename T> inline void read(std::vector<T> &f) { for (auto &i : f) read(i); } template <typename T, int N> inline void read(std::array<T, N> &res) { for (auto &i : res) read(i); } template <typename T1, typename T2> inline void read(std::pair<T1, T2> &p) { read(p.first, p.second); } template <typename T> inline void read(T &f) { in >> f; } template <typename T, typename... Args> inline void read(T &t, Args &...args) { read(t), read(args...); } template <typename T> inline void readArray(int n, T *res) { while (n--) read(*(res++)); } template <typename T> inline input_reader &operator>>(T &t) { return read(t), *this; } }; } // namespace io namespace io { class output_writer { std::ostream &out; template <typename T> inline void printIntegerHelper(T x) { if (x >= 10) printIntegerHelper(x / 10); out.put(x % 10 + 48); } template <typename T> inline void printInteger(T x) { if (x < 0) out.put('-'), x = -x; printIntegerHelper(x); } template <typename T> inline void printArray(T &ar) { bool first = true; for (auto &t : ar) print((first) ? first = false, "" : " ", t); } public: explicit output_writer(std::ostream &out) : out(out) {} inline void flush() { out.flush(); } inline void print() {} inline void print(char x) { out.put(x); } inline void print(short x) { printInteger(x); } inline void print(int x) { printInteger(x); } inline void print(long int x) { printInteger(x); } inline void print(long long int x) { printInteger(x); } inline void print(unsigned short x) { printInteger(x); } inline void print(unsigned int x) { printInteger(x); } inline void print(unsigned long int x) { printInteger(x); } inline void print(unsigned long long int x) { printInteger(x); } template <typename T1> inline void print(const T1 &t1) { out << t1; } template <typename T1, typename T2> inline void print(const std::pair<T1, T2> &t1) { println(t1.first, ' ', t1.second); } template <typename T1> inline void print(const std::vector<T1> &t1) { printArray(t1); } template <typename T1, int N> inline void print(const std::array<T1, N> &t1) { printArray(t1); } template <typename T1, int N, int M> inline void print(const std::array<std::array<T1, M>, N> &t1) { for (auto &t : t1) println(t); } template <typename T1> inline void print(const std::vector<std::vector<T1>> &t1) { bool first = true; for (auto &t : t1) print((first) ? first = false, "" : "\n", t); } template <typename T, typename... Args> inline void print(const T &t, Args... args) { print(t), print(args...); } template <typename... Args> inline void println(Args... args) { print(args...), print('\n'); } template <typename T> output_writer &operator<<(const T &t) { return print(t), *this; } }; } // namespace io inline ll pow_mod(ll a, ll b, const ll MOD) { ll res = 1; for (ll i = 1; b; b ^= b & i, i <<= 1, a = (a * a) % MOD) if (b & i) res = (res * a) % MOD; return res; } inline ll mod_inv(const ll a, const ll MOD) { return pow_mod(a, MOD - 2, MOD); } template <typename T> inline T gcd(T a, T b) { T t; while (a != 0) t = a, a = b % a, b = t; return b; } template <typename T> T lcm(const T &a, const T &b) { assert(a != 0 && b != 0); return a / gcd(a, b) * b; } namespace math { template <class T> int gMOD(T v) { int res = (int)(v % MOD); if (res >= 0) return res; return res + MOD; }; class mod_int { using mi = mod_int; int v; public: mod_int(int v) : v(gMOD(v)) {} mod_int(unsigned int v) : v(gMOD(v)) {} mod_int(long long int v) : v(gMOD(v)) {} mod_int(unsigned long long int v) : v(gMOD(v)) {} mod_int(long int v) : v(gMOD(v)) {} mod_int(unsigned long int v) : v(gMOD(v)) {} mod_int() = default; mod_int(const mi &o) = default; mod_int(mi &&o) = default; inline mi &operator=(const mi &o) = default; inline mi &operator=(mi &&o) = default; inline mi &operator++() { if (v == MOD - 1) v = 0; else v++; return *this; } inline mi &operator--() { if (v == 0) v = MOD - 1; else v--; return *this; } inline mi operator++(int) { mi t = *this; ++(*this); return t; } inline mi operator--(int) { mi t = *this; --(*this); return t; } inline bool friend operator==(const mi &lhs, const mi &rhs) { return lhs.v == rhs.v; } inline bool friend operator!=(const mi &lhs, const mi &rhs) { return lhs.v != rhs.v; } template <typename T> inline bool friend operator==(const mi &lhs, const T &rhs) { return lhs == mi(rhs); } template <typename T> inline bool friend operator!=(const mi &lhs, const T &rhs) { return lhs != mi(rhs); } inline mi friend operator-(const mi &lhs, const mi &rhs) { mi res; res.v = lhs.v - rhs.v; if (res.v < 0) res.v += MOD; return res; } inline mi friend operator+(const mi &lhs, const mi &rhs) { mi res; res.v = lhs.v + rhs.v - MOD; if (res.v < 0) res.v += MOD; return res; } inline mi friend operator*(const mi &lhs, const mi &rhs) { return {((((long long int)lhs.v) * rhs.v))}; } inline mi friend operator/(const mi &lhs, const mi &rhs) { return lhs * rhs.inv(); } template <typename T> inline mi friend operator-(const mi &lhs, const T &rhs) { return lhs - mi(rhs); } template <typename T> inline mi friend operator+(const mi &lhs, const T &rhs) { return lhs + mi(rhs); } template <typename T> inline mi friend operator*(const mi &lhs, const T &rhs) { return lhs * mi(rhs); } template <typename T> inline mi friend operator/(const mi &lhs, const T &rhs) { return lhs / mi(rhs); } template <typename T> inline mi &operator-=(const T &rhs) { return *this = *this - rhs; } template <typename T> inline mi &operator+=(const T &rhs) { return *this = *this + rhs; } template <typename T> inline mi &operator*=(const T &rhs) { return *this = *this * rhs; } template <typename T> inline mi &operator/=(const T &rhs) { return *this = *this / rhs; } inline mi inv() const { return pow(MOD - 2); } inline mi pow(long long int pw) const { mi res = 1, val = *this; while (pw) { if (pw & 1) res *= val; val *= val; pw >>= 1; } return res; } inline operator int() const { return v; } inline friend std::istream &operator>>(std::istream &in, mi &rhs) { long long int v; in >> v; rhs = {v}; return in; } inline friend std::ostream &operator<<(std::ostream &out, mi &rhs) { out << rhs.v; return out; } }; } // namespace math namespace math { mod_int factorial(int n) { static std::vector<mod_int> mem; static mod_int last; if (mem.empty()) last = {1}, mem.push_back(last); while (n >= (int)mem.size()) mem.push_back(mem.back() * last++); return mem[n]; } mod_int n_choose_k(int n, int k) { return factorial(n) / (factorial(k) * factorial(n - k)); } } // namespace math const double PI = 3.14159265358979323846264338327950288419716939937510; std::mt19937 get_random_engine() { return std::mt19937(std::random_device()()); } template <typename T = int> T myrand(T high = std::numeric_limits<T>::max()) { assert(high > 0); static std::mt19937 random_engine = get_random_engine(); return std::uniform_int_distribution<T>(0, high - 1)(random_engine); } // #include "prioity_queue_with_changes/pq_with_changes.h" // #include "points_query/point_query.hpp" // #include "numeric/numeric.hpp" // #include "hashes/hashes.hpp" // #include "graphs/graphs.hpp" // #include "geometry/geometry.hpp" // #include "extension_operations/extensions.hpp" // #include "ranges/ranges.hpp" // #include "matching/unweighted_max_matching.hpp" // #include "lists/lists.hpp" using mi = math::mod_int; using vmi = std::vector<mi>; using vvmi = std::vector<std::vector<mi>>; using namespace std; class CShift { class Solver { public: io::input_reader in; io::output_writer out; Solver(std::istream &in, std::ostream &out) : in(in), out(out) {} void solve() { // <code here> string s; int k; in >> s >> k; s.pb('0'); int number_of_1 = 0; for (char c : s) if (c == '1') number_of_1++; vi chunk_of_1; int n = s.size(); for (int i = 0; i < n; i++) { int j = i; int cur = 0; while (j < n && s[j] == '1') cur++, j++; chunk_of_1.pb(cur); i = j; } n = chunk_of_1.size(); vector<vvmi> dp(n + 1, vvmi(number_of_1 + 1, vmi(number_of_1 + 1))); dp[0][0][0] = 1; int ones_left = number_of_1; fori(i, n) { ones_left -= chunk_of_1[i]; fori(action_used, number_of_1 + 1) { fori(action_paid_for, action_used + 1) { int cur = dp[i][action_used][action_paid_for]; if (cur == 0) continue; fori(noo2u, chunk_of_1[i] + ones_left + 1) { if (noo2u < chunk_of_1[i]) { if (action_paid_for + chunk_of_1[i] - noo2u <= action_used) { dp[i + 1][action_used] [action_paid_for + chunk_of_1[i] - noo2u] += cur; } } else if (noo2u == chunk_of_1[i]) { dp[i + 1][action_used][action_paid_for] += cur; } else { int missing1 = noo2u - chunk_of_1[i]; if (missing1 > ones_left) break; int new_action = action_used + missing1; if (new_action > k) break; dp[i + 1][new_action][action_paid_for] += cur; } } } } } mi ans = 0; fori(i, number_of_1 + 1) ans += dp.back()[i][i]; // </code here> out.println(ans); } }; public: void solve(std::istream &in, std::ostream &out) { Solver(in, out).solve(); } }; int main() { std::ios_base::sync_with_stdio(false), cin.tie(0); CShift solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Ido Kessler */ #include <algorithm> #include <bitset> #include <cassert> #include <cstdint> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <unordered_map> #include <unordered_set> #include <vector> #include <cmath> #include <random> #include <tuple> using ll = long long int; using ull = unsigned long long int; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; using pi = std::pair<int, int>; using vpi = std::vector<pi>; using vvpi = std::vector<vpi>; using pll = std::pair<ll, ll>; using vpll = std::vector<pll>; using vvpll = std::vector<vpll>; #define all(x) (x).begin(), (x).end() #define fori(i, n) for (int i = 0; i < (int)(n); i++) #define rep(i, s, e) for (int i = (int)(s); i <= (int)(e); ++i) #define repr(i, s, e) for (int i = (int)(s); i >= (int)(e); --i) #define pb push_back template <typename T> void mk_unique(std::vector<T> &l) { if (!std::is_sorted(all(l))) std::sort(all(l)); auto it = std::unique(l.begin(), l.end()); auto d = std::distance(l.begin(), it); l.resize(d); } template <typename T> T &smax(T &l, const T &r) { if (l < r) l = r; return l; } template <typename T> T &smin(T &l, const T &r) { if (r < l) l = r; return l; } constexpr int MOD = 998244353; // 1000000007; namespace io { class input_reader { void throw_error() { throw std::runtime_error("No more inputs while reading number"); } template <typename T> inline void readIntegerValue(T &f) { bool positive = true; f = 0; char c; if (!in.get(c)) throw_error(); while (c < '0' || c > '9') { if (c == '-') positive = false; if (!in.get(c)) throw_error(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); if (!in.get(c)) break; } if (!positive) f *= -1; } public: std::istream &in; explicit input_reader(std::istream &in) : in(in) {} inline void read(int &f) { readIntegerValue(f); } inline void read(short &f) { readIntegerValue(f); } inline void read(long int &f) { readIntegerValue(f); } inline void read(long long int &f) { readIntegerValue(f); } inline void read(unsigned int &f) { readIntegerValue(f); } inline void read(unsigned short &f) { readIntegerValue(f); } inline void read(unsigned long int &f) { readIntegerValue(f); } inline void read(unsigned long long int &f) { readIntegerValue(f); } template <typename T> inline void read(std::vector<T> &f) { for (auto &i : f) read(i); } template <typename T, int N> inline void read(std::array<T, N> &res) { for (auto &i : res) read(i); } template <typename T1, typename T2> inline void read(std::pair<T1, T2> &p) { read(p.first, p.second); } template <typename T> inline void read(T &f) { in >> f; } template <typename T, typename... Args> inline void read(T &t, Args &...args) { read(t), read(args...); } template <typename T> inline void readArray(int n, T *res) { while (n--) read(*(res++)); } template <typename T> inline input_reader &operator>>(T &t) { return read(t), *this; } }; } // namespace io namespace io { class output_writer { std::ostream &out; template <typename T> inline void printIntegerHelper(T x) { if (x >= 10) printIntegerHelper(x / 10); out.put(x % 10 + 48); } template <typename T> inline void printInteger(T x) { if (x < 0) out.put('-'), x = -x; printIntegerHelper(x); } template <typename T> inline void printArray(T &ar) { bool first = true; for (auto &t : ar) print((first) ? first = false, "" : " ", t); } public: explicit output_writer(std::ostream &out) : out(out) {} inline void flush() { out.flush(); } inline void print() {} inline void print(char x) { out.put(x); } inline void print(short x) { printInteger(x); } inline void print(int x) { printInteger(x); } inline void print(long int x) { printInteger(x); } inline void print(long long int x) { printInteger(x); } inline void print(unsigned short x) { printInteger(x); } inline void print(unsigned int x) { printInteger(x); } inline void print(unsigned long int x) { printInteger(x); } inline void print(unsigned long long int x) { printInteger(x); } template <typename T1> inline void print(const T1 &t1) { out << t1; } template <typename T1, typename T2> inline void print(const std::pair<T1, T2> &t1) { println(t1.first, ' ', t1.second); } template <typename T1> inline void print(const std::vector<T1> &t1) { printArray(t1); } template <typename T1, int N> inline void print(const std::array<T1, N> &t1) { printArray(t1); } template <typename T1, int N, int M> inline void print(const std::array<std::array<T1, M>, N> &t1) { for (auto &t : t1) println(t); } template <typename T1> inline void print(const std::vector<std::vector<T1>> &t1) { bool first = true; for (auto &t : t1) print((first) ? first = false, "" : "\n", t); } template <typename T, typename... Args> inline void print(const T &t, Args... args) { print(t), print(args...); } template <typename... Args> inline void println(Args... args) { print(args...), print('\n'); } template <typename T> output_writer &operator<<(const T &t) { return print(t), *this; } }; } // namespace io inline ll pow_mod(ll a, ll b, const ll MOD) { ll res = 1; for (ll i = 1; b; b ^= b & i, i <<= 1, a = (a * a) % MOD) if (b & i) res = (res * a) % MOD; return res; } inline ll mod_inv(const ll a, const ll MOD) { return pow_mod(a, MOD - 2, MOD); } template <typename T> inline T gcd(T a, T b) { T t; while (a != 0) t = a, a = b % a, b = t; return b; } template <typename T> T lcm(const T &a, const T &b) { assert(a != 0 && b != 0); return a / gcd(a, b) * b; } namespace math { template <class T> int gMOD(T v) { int res = (int)(v % MOD); if (res >= 0) return res; return res + MOD; }; class mod_int { using mi = mod_int; int v; public: mod_int(int v) : v(gMOD(v)) {} mod_int(unsigned int v) : v(gMOD(v)) {} mod_int(long long int v) : v(gMOD(v)) {} mod_int(unsigned long long int v) : v(gMOD(v)) {} mod_int(long int v) : v(gMOD(v)) {} mod_int(unsigned long int v) : v(gMOD(v)) {} mod_int() = default; mod_int(const mi &o) = default; mod_int(mi &&o) = default; inline mi &operator=(const mi &o) = default; inline mi &operator=(mi &&o) = default; inline mi &operator++() { if (v == MOD - 1) v = 0; else v++; return *this; } inline mi &operator--() { if (v == 0) v = MOD - 1; else v--; return *this; } inline mi operator++(int) { mi t = *this; ++(*this); return t; } inline mi operator--(int) { mi t = *this; --(*this); return t; } inline bool friend operator==(const mi &lhs, const mi &rhs) { return lhs.v == rhs.v; } inline bool friend operator!=(const mi &lhs, const mi &rhs) { return lhs.v != rhs.v; } template <typename T> inline bool friend operator==(const mi &lhs, const T &rhs) { return lhs == mi(rhs); } template <typename T> inline bool friend operator!=(const mi &lhs, const T &rhs) { return lhs != mi(rhs); } inline mi friend operator-(const mi &lhs, const mi &rhs) { mi res; res.v = lhs.v - rhs.v; if (res.v < 0) res.v += MOD; return res; } inline mi friend operator+(const mi &lhs, const mi &rhs) { mi res; res.v = lhs.v + rhs.v - MOD; if (res.v < 0) res.v += MOD; return res; } inline mi friend operator*(const mi &lhs, const mi &rhs) { return {((((long long int)lhs.v) * rhs.v))}; } inline mi friend operator/(const mi &lhs, const mi &rhs) { return lhs * rhs.inv(); } template <typename T> inline mi friend operator-(const mi &lhs, const T &rhs) { return lhs - mi(rhs); } template <typename T> inline mi friend operator+(const mi &lhs, const T &rhs) { return lhs + mi(rhs); } template <typename T> inline mi friend operator*(const mi &lhs, const T &rhs) { return lhs * mi(rhs); } template <typename T> inline mi friend operator/(const mi &lhs, const T &rhs) { return lhs / mi(rhs); } template <typename T> inline mi &operator-=(const T &rhs) { return *this = *this - rhs; } template <typename T> inline mi &operator+=(const T &rhs) { return *this = *this + rhs; } template <typename T> inline mi &operator*=(const T &rhs) { return *this = *this * rhs; } template <typename T> inline mi &operator/=(const T &rhs) { return *this = *this / rhs; } inline mi inv() const { return pow(MOD - 2); } inline mi pow(long long int pw) const { mi res = 1, val = *this; while (pw) { if (pw & 1) res *= val; val *= val; pw >>= 1; } return res; } inline operator int() const { return v; } inline friend std::istream &operator>>(std::istream &in, mi &rhs) { long long int v; in >> v; rhs = {v}; return in; } inline friend std::ostream &operator<<(std::ostream &out, mi &rhs) { out << rhs.v; return out; } }; } // namespace math namespace math { mod_int factorial(int n) { static std::vector<mod_int> mem; static mod_int last; if (mem.empty()) last = {1}, mem.push_back(last); while (n >= (int)mem.size()) mem.push_back(mem.back() * last++); return mem[n]; } mod_int n_choose_k(int n, int k) { return factorial(n) / (factorial(k) * factorial(n - k)); } } // namespace math const double PI = 3.14159265358979323846264338327950288419716939937510; std::mt19937 get_random_engine() { return std::mt19937(std::random_device()()); } template <typename T = int> T myrand(T high = std::numeric_limits<T>::max()) { assert(high > 0); static std::mt19937 random_engine = get_random_engine(); return std::uniform_int_distribution<T>(0, high - 1)(random_engine); } // #include "prioity_queue_with_changes/pq_with_changes.h" // #include "points_query/point_query.hpp" // #include "numeric/numeric.hpp" // #include "hashes/hashes.hpp" // #include "graphs/graphs.hpp" // #include "geometry/geometry.hpp" // #include "extension_operations/extensions.hpp" // #include "ranges/ranges.hpp" // #include "matching/unweighted_max_matching.hpp" // #include "lists/lists.hpp" using mi = math::mod_int; using vmi = std::vector<mi>; using vvmi = std::vector<std::vector<mi>>; using namespace std; class CShift { class Solver { public: io::input_reader in; io::output_writer out; Solver(std::istream &in, std::ostream &out) : in(in), out(out) {} void solve() { // <code here> string s; int k; in >> s >> k; s.pb('0'); int number_of_1 = 0; for (char c : s) if (c == '1') number_of_1++; vi chunk_of_1; int n = s.size(); for (int i = 0; i < n; i++) { int j = i; int cur = 0; while (j < n && s[j] == '1') cur++, j++; chunk_of_1.pb(cur); i = j; } n = chunk_of_1.size(); vector<vvmi> dp(n + 1, vvmi(number_of_1 + 1, vmi(number_of_1 + 1))); dp[0][0][0] = 1; int ones_left = number_of_1; fori(i, n) { ones_left -= chunk_of_1[i]; fori(action_used, number_of_1 + 1) { fori(action_paid_for, action_used + 1) { int cur = dp[i][action_used][action_paid_for]; if (cur == 0) continue; fori(noo2u, chunk_of_1[i] + ones_left + 1) { if (noo2u < chunk_of_1[i]) { if (action_paid_for + chunk_of_1[i] - noo2u <= action_used) { dp[i + 1][action_used] [action_paid_for + chunk_of_1[i] - noo2u] += cur; } } else if (noo2u == chunk_of_1[i]) { dp[i + 1][action_used][action_paid_for] += cur; } else { int missing1 = noo2u - chunk_of_1[i]; if (missing1 > ones_left) break; int new_action = action_used + missing1; if (new_action > k || new_action > number_of_1) break; dp[i + 1][new_action][action_paid_for] += cur; } } } } } mi ans = 0; fori(i, number_of_1 + 1) ans += dp.back()[i][i]; // </code here> out.println(ans); } }; public: void solve(std::istream &in, std::ostream &out) { Solver(in, out).solve(); } }; int main() { std::ios_base::sync_with_stdio(false), cin.tie(0); CShift solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
replace
434
435
434
435
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif #define rep(i, n) for (int i = 0; i < (n); ++i) #define repA(i, a, n) for (int i = a; i <= (n); ++i) #define repD(i, a, n) for (int i = a; i >= (n); --i) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define fill(a) memset(a, 0, sizeof(a)) #define fst first #define snd second #define mp make_pair #define pb push_back typedef long double ld; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; void pre() {} void solve() {} const ll mod = 998244353; ll dp[2][309][309]; ll sum[2][309][609]; int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); pre(); string s; int k; cin >> s >> k; vi v; int cnt = 0; trav(i, s) { if (i == '1') cnt++; else v.pb(cnt), cnt = 0; } v.pb(cnt); if (sz(v) == 1) { cout << 1; return 0; } dp[0][0][0] = 1; int cur = 0; trav(i, v) { fill(dp[1 - cur]); repA(j, 0, k) repA(x, 0, k) { if (x && j) sum[0][j][x] = (sum[0][j - 1][x - 1] + dp[cur][j][x]) % mod; else sum[0][j][x] = dp[cur][j][x]; } repD(j, k, 0) repD(x, k, 0) { sum[1][j][x] = (sum[1][j][x + 1] + dp[cur][j][x]) % mod; } repA(j, 0, k) repA(x, 0, k) { dp[1 - cur][j][x] = (sum[0][j][x] + sum[1][j][x] - dp[cur][j][x] - sum[1][j][x + i + 1] + 2 * mod) % mod; } cur = 1 - cur; } ll fns = 0; repA(j, 0, k) { fns += dp[cur][j][0]; } cout << fns % mod; return 0; }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif #define rep(i, n) for (int i = 0; i < (n); ++i) #define repA(i, a, n) for (int i = a; i <= (n); ++i) #define repD(i, a, n) for (int i = a; i >= (n); --i) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define fill(a) memset(a, 0, sizeof(a)) #define fst first #define snd second #define mp make_pair #define pb push_back typedef long double ld; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; void pre() {} void solve() {} const ll mod = 998244353; ll dp[2][309][309]; ll sum[2][309][609]; int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); pre(); string s; int k; cin >> s >> k; k = min(k, 300); vi v; int cnt = 0; trav(i, s) { if (i == '1') cnt++; else v.pb(cnt), cnt = 0; } v.pb(cnt); if (sz(v) == 1) { cout << 1; return 0; } dp[0][0][0] = 1; int cur = 0; trav(i, v) { fill(dp[1 - cur]); repA(j, 0, k) repA(x, 0, k) { if (x && j) sum[0][j][x] = (sum[0][j - 1][x - 1] + dp[cur][j][x]) % mod; else sum[0][j][x] = dp[cur][j][x]; } repD(j, k, 0) repD(x, k, 0) { sum[1][j][x] = (sum[1][j][x + 1] + dp[cur][j][x]) % mod; } repA(j, 0, k) repA(x, 0, k) { dp[1 - cur][j][x] = (sum[0][j][x] + sum[1][j][x] - dp[cur][j][x] - sum[1][j][x + i + 1] + 2 * mod) % mod; } cur = 1 - cur; } ll fns = 0; repA(j, 0, k) { fns += dp[cur][j][0]; } cout << fns % mod; return 0; }
insert
76
76
76
77
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define rrep(i, a, b) for (int i = (a); i >= (b); --i) #define PB push_back #define ar2 array<int, 2> #define vs vector<string> typedef long long LL; const int P = 998244353; const int N = 305; mt19937 rng(time(0)); int T, n, K, n0 = 0, n1; char s[N]; int a[N], sum[N], dp[N][N][N], sdp[N][N][N]; // i-th, j-same, k-max void madd(int &x, int y) { x += y; x = (x >= P ? x - P : x); } int main() { cin >> (s + 1) >> K; n = strlen(s + 1); rep(i, 1, n) if (s[i] == '0') n0++; else a[n0]++; sum[0] = a[0]; rep(i, 1, n0) sum[i] = sum[i - 1] + a[i]; n1 = n - n0; rep(i, 0, n1) rep(j, 0, i) { dp[i][j][0] = sdp[i][j][0] = (min(i, a[0]) == j ? 1 : 0); // cout<<i<<" "<<j<<" 0 "<<dp[i][j][0]<<endl; rep(k, 1, n0) { if (sum[k] > i) { sdp[i][j][k] = sdp[i][j][k - 1]; continue; } rep(t, sum[k - 1], i - 1) { madd(dp[i][j][k], sdp[t][j - min(i - t, a[k])][k - 1]); } sdp[i][j][k] = sdp[i][j][k - 1]; madd(sdp[i][j][k], dp[i][j][k]); // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl; } } int ans = 0; rep(j, max(0, n1 - K), n1) madd(ans, sdp[n1][j][n0]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define rrep(i, a, b) for (int i = (a); i >= (b); --i) #define PB push_back #define ar2 array<int, 2> #define vs vector<string> typedef long long LL; const int P = 998244353; const int N = 305; mt19937 rng(time(0)); int T, n, K, n0 = 0, n1; char s[N]; int a[N], sum[N], dp[N][N][N], sdp[N][N][N]; // i-th, j-same, k-max void madd(int &x, int y) { x += y; x = (x >= P ? x - P : x); } int main() { cin >> (s + 1) >> K; n = strlen(s + 1); rep(i, 1, n) if (s[i] == '0') n0++; else a[n0]++; sum[0] = a[0]; rep(i, 1, n0) sum[i] = sum[i - 1] + a[i]; n1 = n - n0; rep(i, 0, n1) rep(j, 0, i) { dp[i][j][0] = sdp[i][j][0] = (min(i, a[0]) == j ? 1 : 0); // cout<<i<<" "<<j<<" 0 "<<dp[i][j][0]<<endl; rep(k, 1, n0) { if (sum[k] > i) { sdp[i][j][k] = sdp[i][j][k - 1]; continue; } rep(t, sum[k - 1], i - 1) { if (j - min(i - t, a[k]) < 0) continue; madd(dp[i][j][k], sdp[t][j - min(i - t, a[k])][k - 1]); } sdp[i][j][k] = sdp[i][j][k - 1]; madd(sdp[i][j][k], dp[i][j][k]); // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl; } } int ans = 0; rep(j, max(0, n1 - K), n1) madd(ans, sdp[n1][j][n0]); cout << ans << endl; return 0; }
insert
40
40
40
42
-11
p02635
C++
Runtime Error
/*input 01100110 2 */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #pragma GCC optimize("unroll-loops,no-stack-protector") // order_of_key #of elements less than x // find_by_order kth element #define ll long long #define ld long double #define pii pair<int, int> typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #define f first #define s second #define pb push_back #define REP(i, n) for (int i = 0; i < n; i++) #define REP1(i, n) for (int i = 1; i <= n; i++) #define FILL(n, x) memset(n, x, sizeof(n)) #define ALL(_a) _a.begin(), _a.end() #define sz(x) (int)x.size() #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define MP make_pair const ll INF64 = 4e12; const int INF = 0x3f3f3f3f; const ll MOD = 998244353; const ld PI = acos(-1); const ld eps = 1e-9; #define lowb(x) x & (-x) #define MNTO(x, y) x = min(x, (__typeof__(x))y) #define MXTO(x, y) x = max(x, (__typeof__(x))y) ll mult(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; } ll mypow(ll a, ll b) { if (b <= 0) return 1; ll res = 1LL; while (b) { if (b & 1) { res = (res * a) % MOD; } a = (a * a) % MOD; b >>= 1; } return res; } const ll maxn = 200 + 5; const ll maxlg = __lg(maxn) + 2; int dp[maxn][maxn][maxn]; int main() { ios::sync_with_stdio(false), cin.tie(0); string s; int z; cin >> s >> z; s += "0"; vector<int> v; int cnt = 0; REP(i, sz(s)) { if (s[i] == '1') ++cnt; else v.pb(cnt), cnt = 0; } int tot = sz(s) - sz(v); MNTO(z, tot); vector<int> pref; REP(i, sz(v)) { if (i) pref.pb(pref.back() + v[i]); else pref.pb(v[i]); } dp[0][0][0] = 1; REP(i, sz(v)) { REP(j, tot + 1) { REP(op, tot + 1) { if (dp[i][j][op]) { REP(cur, tot - j + 1) { if (j + cur < pref[i]) continue; dp[i + 1][j + cur][op + max(cur - v[i], 0)] += dp[i][j][op]; dp[i + 1][j + cur][op + max(cur - v[i], 0)] %= MOD; } } } } } ll ans = 0; REP(i, z + 1) ans += dp[sz(v)][tot][i], ans %= MOD; cout << ans; }
/*input 01100110 2 */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #pragma GCC optimize("unroll-loops,no-stack-protector") // order_of_key #of elements less than x // find_by_order kth element #define ll long long #define ld long double #define pii pair<int, int> typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #define f first #define s second #define pb push_back #define REP(i, n) for (int i = 0; i < n; i++) #define REP1(i, n) for (int i = 1; i <= n; i++) #define FILL(n, x) memset(n, x, sizeof(n)) #define ALL(_a) _a.begin(), _a.end() #define sz(x) (int)x.size() #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define MP make_pair const ll INF64 = 4e12; const int INF = 0x3f3f3f3f; const ll MOD = 998244353; const ld PI = acos(-1); const ld eps = 1e-9; #define lowb(x) x & (-x) #define MNTO(x, y) x = min(x, (__typeof__(x))y) #define MXTO(x, y) x = max(x, (__typeof__(x))y) ll mult(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; } ll mypow(ll a, ll b) { if (b <= 0) return 1; ll res = 1LL; while (b) { if (b & 1) { res = (res * a) % MOD; } a = (a * a) % MOD; b >>= 1; } return res; } const ll maxn = 300 + 5; const ll maxlg = __lg(maxn) + 2; int dp[maxn][maxn][maxn]; int main() { ios::sync_with_stdio(false), cin.tie(0); string s; int z; cin >> s >> z; s += "0"; vector<int> v; int cnt = 0; REP(i, sz(s)) { if (s[i] == '1') ++cnt; else v.pb(cnt), cnt = 0; } int tot = sz(s) - sz(v); MNTO(z, tot); vector<int> pref; REP(i, sz(v)) { if (i) pref.pb(pref.back() + v[i]); else pref.pb(v[i]); } dp[0][0][0] = 1; REP(i, sz(v)) { REP(j, tot + 1) { REP(op, tot + 1) { if (dp[i][j][op]) { REP(cur, tot - j + 1) { if (j + cur < pref[i]) continue; dp[i + 1][j + cur][op + max(cur - v[i], 0)] += dp[i][j][op]; dp[i + 1][j + cur][op + max(cur - v[i], 0)] %= MOD; } } } } } ll ans = 0; REP(i, z + 1) ans += dp[sz(v)][tot][i], ans %= MOD; cout << ans; }
replace
52
53
52
53
0
p02635
C++
Runtime Error
#include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; typedef long long ll; const int N = 310, mod = 998244353; char s[N]; int K, ct, n, m; int w[N]; ll sum, f[N][N][N]; void pls(ll &x, ll y) { x += y; x >= mod ? x -= mod : 0; } int main() { // freopen("a.in","r",stdin); scanf("%s%d", s + 1, &K); n = strlen(s + 1); for (int i = 1; i <= n; i++) if (s[i] == '0') m++, w[m] = ct, ct = 0; else ct++; m++; w[m] = ct; f[m + 1][0][0] = 1; for (int i = m; i; i--) { sum = min(sum, (ll)K); for (int j = 0; j <= sum; j++) for (int k = j; k <= sum; k++) for (int c = 0; c <= j + w[i]; c++) { if (c < w[i]) pls(f[i][j + w[i] - c][k + w[i] - c], f[i + 1][j][k]); else pls(f[i][j + w[i] - c][k], f[i + 1][j][k]); } sum += w[i]; } sum = 0; for (int i = 0; i <= K; i++) pls(sum, f[1][0][i]); cout << sum; }
#include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; typedef long long ll; const int N = 310, mod = 998244353; char s[N]; int K, ct, n, m; int w[N]; ll sum, f[N][N][N]; void pls(ll &x, ll y) { x += y; x >= mod ? x -= mod : 0; } int main() { // freopen("a.in","r",stdin); scanf("%s%d", s + 1, &K); n = strlen(s + 1); for (int i = 1; i <= n; i++) if (s[i] == '0') m++, w[m] = ct, ct = 0; else ct++; m++; w[m] = ct; f[m + 1][0][0] = 1; for (int i = m; i; i--) { sum = min(sum, (ll)K); for (int j = 0; j <= sum; j++) for (int k = j; k <= sum; k++) for (int c = 0; c <= j + w[i]; c++) { if (c < w[i]) pls(f[i][j + w[i] - c][k + w[i] - c], f[i + 1][j][k]); else pls(f[i][j + w[i] - c][k], f[i + 1][j][k]); } sum += w[i]; } K = min(K, n); sum = 0; for (int i = 0; i <= K; i++) pls(sum, f[1][0][i]); cout << sum; }
insert
43
43
43
44
-11
p02635
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define ll long long #define ull unsigned long long #define rrep(i, m, n) for (ll(i) = (ll)(m); (i) >= (ll)(n); (i)--) #define rep(i, m, n) for (ll(i) = (ll)(m); i < (ll)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; constexpr long double m_pi = 3.1415926535897932L; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 62; constexpr long double EPS = 1e-10; template <typename T> using vector2 = vector<vector<T>>; template <typename T> using vector3 = vector<vector2<T>>; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } 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; } struct Edge { // グラフ int to, rev; ll cap; Edge(int _to, ll _cap, int _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, int from, int to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size() + (from == to))); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = ((i != j) ? INF : 0); } } REP(i, G.size()) { REP(j, G[i].size()) { chmin(d[i][G[i][j].to], G[i][j].cap); } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, Array &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class Lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; Lca() {} Lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } ll size(int x) { // xの集合のサイズを返す return -data[root(x)]; } ll numSet() { // 集合の数を返す return num; } }; template <typename T, typename F> class SegmentTree { private: T identity; F merge; ll n; vector<T> dat; public: SegmentTree(F f, T id, vector<T> v) : merge(f), identity(id) { int _n = v.size(); n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); REP(i, _n) dat[n + i - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } SegmentTree(F f, T id, int _n) : merge(f), identity(id) { n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); } void set_val(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int l, int r) { T left = identity, right = identity; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0) left = merge(left, dat[l]); if ((r & 1) == 0) right = merge(dat[r - 1], right); l = l / 2; r = (r - 1) / 2; } return merge(left, right); } }; template <typename T> class FenwickTree { vector<T> data; int n; int p; public: FenwickTree(int n) : n(n) { data.resize(n + 1LL, 0); p = 1; while (p < data.size()) p *= 2; } T sum(int k) { T ret = 0; for (; k > 0; k -= k & -k) ret += data[k]; return (ret); } T sum(int a, int b) { return sum(b) - sum(a); } //[a,b) void add(int k, T x) { for (++k; k <= n; k += k & -k) data[k] += x; } int lower_bound(ll w) { if (w <= 0) return -1; int x = 0; for (int k = p / 2; k > 0; k /= 2) { if (x + k <= n && data[x + k] < w) w -= data[x + k], x += k; } return x; } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void prime_factorization(ll n, vector<P> &ret) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } inline ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } inline ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } class Combination { public: Array fact; Array fact_inv; ll mod; // if n >= mod use lucas ll nCr(ll n, ll r) { if (n < r) return 0; if (n < mod) return ((fact[n] * fact_inv[r] % mod) * fact_inv[n - r]) % mod; ll ret = 1; while (n || r) { ll _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } ll nPr(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll _n, ll _mod) { mod = _mod; ll n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; REP(i, n - 1) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } fact_inv.resize(n); fact_inv[n - 1] = mod_inv(fact[n - 1], mod); for (int i = n - 1; i > 0; i--) { fact_inv[i - 1] = fact_inv[i] * i % mod; } } }; ll popcount(ll x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } int dp[330][330][330]; int main() { ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); constexpr int mod = 998244353; string s; int k; cin >> s >> k; reverse(all(s)); vector<pair<int, int>> a; int cnt = 1; rep(i, 1, s.size()) { if (s[i] != s[i - 1]) a.push_back({s[i - 1] - '0', cnt}), cnt = 1; else cnt++; } a.push_back({s.back() - '0', cnt}); vector<pair<int, int>> b; for (auto [i, j] : a) { if (i == 1) b.push_back({i, j}); else { REP(c, j) b.push_back({0, 1}); } } swap(a, b); dp[0][0][0] = 1; REP(i, a.size()) { REP(j, 301) { REP(l, min(330, k + 1)) { if (dp[i][j][l] == 0) continue; if (a[i].first == 1) { REP(m, a[i].second + 1) (dp[i + 1][j + m][l + m] += dp[i][j][l]) %= mod; rep(m, 1, j + 1)(dp[i + 1][j - m][l] += dp[i][j][l]) %= mod; } else { (dp[i + 1][j][l] += dp[i][j][l]) %= mod; if (i != a.size() - 1 && a[i + 1].first == 1) continue; rep(m, 1, j + 1)(dp[i + 1][j - m][l] += dp[i][j][l]) %= mod; } } } } int ans = 0; REP(i, k + 1)(ans += dp[a.size()][0][i]) %= mod; cout << ans << "\n"; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define ll long long #define ull unsigned long long #define rrep(i, m, n) for (ll(i) = (ll)(m); (i) >= (ll)(n); (i)--) #define rep(i, m, n) for (ll(i) = (ll)(m); i < (ll)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; constexpr long double m_pi = 3.1415926535897932L; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 62; constexpr long double EPS = 1e-10; template <typename T> using vector2 = vector<vector<T>>; template <typename T> using vector3 = vector<vector2<T>>; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } 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; } struct Edge { // グラフ int to, rev; ll cap; Edge(int _to, ll _cap, int _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, int from, int to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size() + (from == to))); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = ((i != j) ? INF : 0); } } REP(i, G.size()) { REP(j, G[i].size()) { chmin(d[i][G[i][j].to], G[i][j].cap); } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, Array &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class Lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; Lca() {} Lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } ll size(int x) { // xの集合のサイズを返す return -data[root(x)]; } ll numSet() { // 集合の数を返す return num; } }; template <typename T, typename F> class SegmentTree { private: T identity; F merge; ll n; vector<T> dat; public: SegmentTree(F f, T id, vector<T> v) : merge(f), identity(id) { int _n = v.size(); n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); REP(i, _n) dat[n + i - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } SegmentTree(F f, T id, int _n) : merge(f), identity(id) { n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); } void set_val(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int l, int r) { T left = identity, right = identity; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0) left = merge(left, dat[l]); if ((r & 1) == 0) right = merge(dat[r - 1], right); l = l / 2; r = (r - 1) / 2; } return merge(left, right); } }; template <typename T> class FenwickTree { vector<T> data; int n; int p; public: FenwickTree(int n) : n(n) { data.resize(n + 1LL, 0); p = 1; while (p < data.size()) p *= 2; } T sum(int k) { T ret = 0; for (; k > 0; k -= k & -k) ret += data[k]; return (ret); } T sum(int a, int b) { return sum(b) - sum(a); } //[a,b) void add(int k, T x) { for (++k; k <= n; k += k & -k) data[k] += x; } int lower_bound(ll w) { if (w <= 0) return -1; int x = 0; for (int k = p / 2; k > 0; k /= 2) { if (x + k <= n && data[x + k] < w) w -= data[x + k], x += k; } return x; } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void prime_factorization(ll n, vector<P> &ret) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } inline ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } inline ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } class Combination { public: Array fact; Array fact_inv; ll mod; // if n >= mod use lucas ll nCr(ll n, ll r) { if (n < r) return 0; if (n < mod) return ((fact[n] * fact_inv[r] % mod) * fact_inv[n - r]) % mod; ll ret = 1; while (n || r) { ll _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } ll nPr(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll _n, ll _mod) { mod = _mod; ll n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; REP(i, n - 1) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } fact_inv.resize(n); fact_inv[n - 1] = mod_inv(fact[n - 1], mod); for (int i = n - 1; i > 0; i--) { fact_inv[i - 1] = fact_inv[i] * i % mod; } } }; ll popcount(ll x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } int dp[330][330][330]; int main() { ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); constexpr int mod = 998244353; string s; int k; cin >> s >> k; reverse(all(s)); vector<pair<int, int>> a; int cnt = 1; rep(i, 1, s.size()) { if (s[i] != s[i - 1]) a.push_back({s[i - 1] - '0', cnt}), cnt = 1; else cnt++; } a.push_back({s.back() - '0', cnt}); vector<pair<int, int>> b; for (auto [i, j] : a) { if (i == 1) b.push_back({i, j}); else { REP(c, j) b.push_back({0, 1}); } } swap(a, b); dp[0][0][0] = 1; REP(i, a.size()) { REP(j, 301) { REP(l, min(330, k + 1)) { if (dp[i][j][l] == 0) continue; if (a[i].first == 1) { REP(m, a[i].second + 1) (dp[i + 1][j + m][l + m] += dp[i][j][l]) %= mod; rep(m, 1, j + 1)(dp[i + 1][j - m][l] += dp[i][j][l]) %= mod; } else { (dp[i + 1][j][l] += dp[i][j][l]) %= mod; if (i != a.size() - 1 && a[i + 1].first == 1) continue; rep(m, 1, j + 1)(dp[i + 1][j - m][l] += dp[i][j][l]) %= mod; } } } } int ans = 0; REP(i, min(330, k + 1))(ans += dp[a.size()][0][i]) %= mod; cout << ans << "\n"; return 0; }
replace
507
508
507
508
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const int p = 998244353; int n, m, mx, i, j, k, l, val, ans, f[302][302][302], a[302], b[302], sum, sz; char s[10010]; int main() { scanf("%s", s + 1); scanf("%d", &mx); n = strlen(s + 1); for (i = 1; i <= n; i++) { if (s[i] == '1') sz++, sum++; else { a[++m] = sz; sz = 0; } } f[0][0][0] = 1; for (i = 1; i <= m; i++) { b[i] = a[i]; a[i] += a[i - 1]; for (j = a[i - 1]; j <= sum; j++) for (k = 0; k <= min(j, mx); k++) if (f[i - 1][j][k]) for (l = max(0, a[i] - j); l <= sum - j; l++) { val = max(0, l - b[i]); if (k + val <= mx) (f[i][j + l][k + val] += f[i - 1][j][k]) %= p; } } for (j = a[m]; j <= sum; j++) for (k = 0; k <= mx; k++) ans = (ans + f[m][j][k]) % p; printf("%d", ans); }
#include <bits/stdc++.h> #define ll long long using namespace std; const int p = 998244353; int n, m, mx, i, j, k, l, val, ans, f[302][302][302], a[302], b[302], sum, sz; char s[10010]; int main() { scanf("%s", s + 1); scanf("%d", &mx); n = strlen(s + 1); for (i = 1; i <= n; i++) { if (s[i] == '1') sz++, sum++; else { a[++m] = sz; sz = 0; } } f[0][0][0] = 1; for (i = 1; i <= m; i++) { b[i] = a[i]; a[i] += a[i - 1]; for (j = a[i - 1]; j <= sum; j++) for (k = 0; k <= min(j, mx); k++) if (f[i - 1][j][k]) for (l = max(0, a[i] - j); l <= sum - j; l++) { val = max(0, l - b[i]); if (k + val <= mx) (f[i][j + l][k + val] += f[i - 1][j][k]) %= p; } } for (j = a[m]; j <= sum; j++) for (k = 0; k <= min(mx, sum); k++) ans = (ans + f[m][j][k]) % p; printf("%d", ans); }
replace
32
33
32
33
-11
p02635
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(), (x).end() template <typename A, size_t N, typename T> void Fill(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; } template <unsigned int mod> class ModInt { private: unsigned int v; static unsigned int norm(const unsigned int &x) { return x < mod ? x : x - mod; } static ModInt make(const unsigned int &x) { ModInt m; return m.v = x, m; } static ModInt inv(const ModInt &x) { return make(inverse(x.v, mod)); } static unsigned int inverse(int a, int m) { int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t; while (*v) { t = *u / *v; swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]); } return (u[1] % m + m) % m; } public: ModInt() : v{0} {} ModInt(const long long val) : v{norm(val % mod + mod)} {} ModInt(const ModInt<mod> &n) : v{n()} {} explicit operator bool() const noexcept { return v != 0; } bool operator!() const noexcept { return !static_cast<bool>(*this); } ModInt &operator=(const ModInt &n) { return v = n(), (*this); } ModInt &operator=(const long long val) { return v = norm(val % mod + mod), (*this); } ModInt operator+() const { return *this; } ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); } ModInt operator+(const ModInt &val) const { return make(norm(v + val())); } ModInt operator-(const ModInt &val) const { return make(norm(v + mod - val())); } ModInt operator*(const ModInt &val) const { return make((long long)v * val() % mod); } ModInt operator/(const ModInt &val) const { return *this * inv(val); } ModInt &operator+=(const ModInt &val) { return *this = *this + val; } ModInt &operator-=(const ModInt &val) { return *this = *this - val; } ModInt &operator*=(const ModInt &val) { return *this = *this * val; } ModInt &operator/=(const ModInt &val) { return *this = *this / val; } ModInt operator+(const long long val) const { return ModInt{v + val}; } ModInt operator-(const long long val) const { return ModInt{v - val}; } ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; } ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; } ModInt &operator+=(const long long val) { return *this = *this + val; } ModInt &operator-=(const long long val) { return *this = *this - val; } ModInt &operator*=(const long long val) { return *this = *this * val; } ModInt &operator/=(const long long val) { return *this = *this / val; } bool operator==(const ModInt &val) const { return v == val.v; } bool operator!=(const ModInt &val) const { return !(*this == val); } bool operator==(const long long val) const { return v == norm(val % mod + mod); } bool operator!=(const long long val) const { return !(*this == val); } unsigned int operator()() const { return v; } friend ModInt operator+(const long long val, const ModInt &n) { return n + val; } friend ModInt operator-(const long long val, const ModInt &n) { return ModInt{val - n()}; } friend ModInt operator*(const long long val, const ModInt &n) { return n * val; } friend ModInt operator/(const long long val, const ModInt &n) { return ModInt{val} / n; } friend bool operator==(const long long val, const ModInt &n) { return n == val; } friend bool operator!=(const long long val, const ModInt &n) { return !(val == n); } friend istream &operator>>(istream &is, ModInt &n) { unsigned int v; return is >> v, n = v, is; } friend ostream &operator<<(ostream &os, const ModInt &n) { return (os << n()); } friend ModInt mod_pow(ModInt x, long long n) { ModInt ans = 1; while (n) { if (n & 1) ans *= x; x *= x, n >>= 1; } return ans; } }; #define MOD 998244353 using mod = ModInt<MOD>; mod dp[301][301]; mod dp2[301][301]; int main() { string s; cin >> s; int K; cin >> K; vector<int> a; int n = s.size(); int c = 0; rep(i, n) { if (s[i] == '0') { a.push_back(c); c = 0; } else { c++; } } a.push_back(c); vector<int> b; int m = a.size(); b.resize(m); for (int i = 0; i < m; i++) { b[i] += a[i]; if (i != 0) b[i] += b[i - 1]; } int sm = b[m - 1]; dp[0][0] = 1; rep(i, m) { int pa = 0; if (i != 0) pa = b[i - 1]; for (int j = pa; j <= sm; j++) { // imamade sum : j for (int k = j - pa; k <= j; k++) { // diff sum : k for (int x = max(0, b[i] - j); x <= sm - j; x++) { // x add int nxt1 = j + x; int diff = max(0, x - a[i]); if (k + diff > K || k + diff >= 301) break; dp2[nxt1][k + diff] += dp[j][k]; } } } for (int j = b[i]; j <= sm; j++) { for (int k = j - b[i]; k <= j; k++) { dp[j][k] = dp2[j][k]; dp2[j][k] = 0; } } } mod res = 0; for (int k = 0; k <= K; k++) { res += dp[sm][k]; } cout << res << "\n"; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(), (x).end() template <typename A, size_t N, typename T> void Fill(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; } template <unsigned int mod> class ModInt { private: unsigned int v; static unsigned int norm(const unsigned int &x) { return x < mod ? x : x - mod; } static ModInt make(const unsigned int &x) { ModInt m; return m.v = x, m; } static ModInt inv(const ModInt &x) { return make(inverse(x.v, mod)); } static unsigned int inverse(int a, int m) { int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t; while (*v) { t = *u / *v; swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]); } return (u[1] % m + m) % m; } public: ModInt() : v{0} {} ModInt(const long long val) : v{norm(val % mod + mod)} {} ModInt(const ModInt<mod> &n) : v{n()} {} explicit operator bool() const noexcept { return v != 0; } bool operator!() const noexcept { return !static_cast<bool>(*this); } ModInt &operator=(const ModInt &n) { return v = n(), (*this); } ModInt &operator=(const long long val) { return v = norm(val % mod + mod), (*this); } ModInt operator+() const { return *this; } ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); } ModInt operator+(const ModInt &val) const { return make(norm(v + val())); } ModInt operator-(const ModInt &val) const { return make(norm(v + mod - val())); } ModInt operator*(const ModInt &val) const { return make((long long)v * val() % mod); } ModInt operator/(const ModInt &val) const { return *this * inv(val); } ModInt &operator+=(const ModInt &val) { return *this = *this + val; } ModInt &operator-=(const ModInt &val) { return *this = *this - val; } ModInt &operator*=(const ModInt &val) { return *this = *this * val; } ModInt &operator/=(const ModInt &val) { return *this = *this / val; } ModInt operator+(const long long val) const { return ModInt{v + val}; } ModInt operator-(const long long val) const { return ModInt{v - val}; } ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; } ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; } ModInt &operator+=(const long long val) { return *this = *this + val; } ModInt &operator-=(const long long val) { return *this = *this - val; } ModInt &operator*=(const long long val) { return *this = *this * val; } ModInt &operator/=(const long long val) { return *this = *this / val; } bool operator==(const ModInt &val) const { return v == val.v; } bool operator!=(const ModInt &val) const { return !(*this == val); } bool operator==(const long long val) const { return v == norm(val % mod + mod); } bool operator!=(const long long val) const { return !(*this == val); } unsigned int operator()() const { return v; } friend ModInt operator+(const long long val, const ModInt &n) { return n + val; } friend ModInt operator-(const long long val, const ModInt &n) { return ModInt{val - n()}; } friend ModInt operator*(const long long val, const ModInt &n) { return n * val; } friend ModInt operator/(const long long val, const ModInt &n) { return ModInt{val} / n; } friend bool operator==(const long long val, const ModInt &n) { return n == val; } friend bool operator!=(const long long val, const ModInt &n) { return !(val == n); } friend istream &operator>>(istream &is, ModInt &n) { unsigned int v; return is >> v, n = v, is; } friend ostream &operator<<(ostream &os, const ModInt &n) { return (os << n()); } friend ModInt mod_pow(ModInt x, long long n) { ModInt ans = 1; while (n) { if (n & 1) ans *= x; x *= x, n >>= 1; } return ans; } }; #define MOD 998244353 using mod = ModInt<MOD>; mod dp[301][301]; mod dp2[301][301]; int main() { string s; cin >> s; int K; cin >> K; vector<int> a; int n = s.size(); int c = 0; rep(i, n) { if (s[i] == '0') { a.push_back(c); c = 0; } else { c++; } } a.push_back(c); vector<int> b; int m = a.size(); b.resize(m); for (int i = 0; i < m; i++) { b[i] += a[i]; if (i != 0) b[i] += b[i - 1]; } int sm = b[m - 1]; dp[0][0] = 1; rep(i, m) { int pa = 0; if (i != 0) pa = b[i - 1]; for (int j = pa; j <= sm; j++) { // imamade sum : j for (int k = j - pa; k <= j; k++) { // diff sum : k for (int x = max(0, b[i] - j); x <= sm - j; x++) { // x add int nxt1 = j + x; int diff = max(0, x - a[i]); if (k + diff > K || k + diff >= 301) break; dp2[nxt1][k + diff] += dp[j][k]; } } } for (int j = b[i]; j <= sm; j++) { for (int k = j - b[i]; k <= j; k++) { dp[j][k] = dp2[j][k]; dp2[j][k] = 0; } } } mod res = 0; for (int k = 0; k <= min(sm, K); k++) { res += dp[sm][k]; } cout << res << "\n"; return 0; }
replace
208
209
208
209
0
p02635
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 998244353 #define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD) typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; char S[330]; int N, K; i64 dp[2][330][330]; // 0, 1, required K vector<int> ones; vector<int> acc; void clr(int t) { for (int i = 0; i <= N; ++i) { for (int j = 0; j <= K; ++j) dp[t][i][j] = 0; } } int main() { scanf("%s%d", S, &K); N = strlen(S); S[N++] = '0'; S[N] = 0; int n0 = 0, n1 = 0, n1d = 0; for (int i = 0; i < N; ++i) { if (S[i] == '0') { ++n0; ones.push_back(n1d); acc.push_back(n1); n1d = 0; } else { ++n1; ++n1d; } } K = min(K, N); int t = 0; clr(t); dp[t][0][0] = 1; for (int i = 0; i < n0; ++i) { for (int j = 0; j <= n1; ++j) { for (int k = 0; k <= K; ++k) { dp[1 - t][j][k] = dp[t][j][k]; if (j > 0) ADD(dp[1 - t][j][k], dp[1 - t][j - 1][k]); } } for (int j = 0; j <= n1; ++j) { for (int k = 0; k <= K; ++k) { for (int l = 0; l < ones[i]; ++l) { ADD(dp[1 - t][j + l][k], MOD - dp[t][j][k]); if (k + ones[i] - l <= K) ADD(dp[1 - t][j + l][k + (ones[i] - l)], dp[t][j][k]); } } } for (int j = 0; j < acc[i]; ++j) { for (int k = 0; k <= K; ++k) { dp[1 - t][j][k] = 0; } } t = 1 - t; } i64 ret = 0; for (int i = 0; i <= n1; ++i) for (int j = 0; j <= K; ++j) ADD(ret, dp[t][i][j]); printf("%lld\n", ret); return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 998244353 #define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD) typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; char S[330]; int N, K; i64 dp[2][330][330]; // 0, 1, required K vector<int> ones; vector<int> acc; void clr(int t) { for (int i = 0; i <= N; ++i) { for (int j = 0; j <= K; ++j) dp[t][i][j] = 0; } } int main() { scanf("%s%d", S, &K); N = strlen(S); S[N++] = '0'; S[N] = 0; int n0 = 0, n1 = 0, n1d = 0; for (int i = 0; i < N; ++i) { if (S[i] == '0') { ++n0; ones.push_back(n1d); acc.push_back(n1); n1d = 0; } else { ++n1; ++n1d; } } K = min(K, N); int t = 0; clr(t); dp[t][0][0] = 1; for (int i = 0; i < n0; ++i) { for (int j = 0; j <= n1; ++j) { for (int k = 0; k <= K; ++k) { dp[1 - t][j][k] = dp[t][j][k]; if (j > 0) ADD(dp[1 - t][j][k], dp[1 - t][j - 1][k]); } } for (int j = 0; j <= n1; ++j) { for (int k = 0; k <= K; ++k) { for (int l = 0; l < ones[i]; ++l) { if (j + l > n1) break; ADD(dp[1 - t][j + l][k], MOD - dp[t][j][k]); if (k + ones[i] - l <= K) ADD(dp[1 - t][j + l][k + (ones[i] - l)], dp[t][j][k]); } } } for (int j = 0; j < acc[i]; ++j) { for (int k = 0; k <= K; ++k) { dp[1 - t][j][k] = 0; } } t = 1 - t; } i64 ret = 0; for (int i = 0; i <= n1; ++i) for (int j = 0; j <= K; ++j) ADD(ret, dp[t][i][j]); printf("%lld\n", ret); return 0; }
insert
66
66
66
68
0
p02635
C++
Runtime Error
#ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS #include <intrin.h> #define __builtin_popcount __popcnt #endif #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <cinttypes> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <unordered_map> #include <vector> using namespace std; template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto i = begin(v); i != end(v); i++) is >> *i; return is; } struct fast_io { fast_io &operator>>(int &target) { scanf("%" PRId32, &target); return *this; } fast_io &operator>>(unsigned int &target) { scanf("%" PRIu32, &target); return *this; } fast_io &operator>>(long long &target) { scanf("%" PRId64, &target); return *this; } fast_io &operator>>(unsigned long long &target) { scanf("%" PRIu64, &target); return *this; } template <typename T> fast_io &operator>>(vector<T> &target) { for (auto &value : target) { *this >> value; } return *this; } fast_io &operator<<(const int value) { printf("%" PRId32, value); return *this; } fast_io &operator<<(const unsigned int value) { printf("%" PRIu32, value); return *this; } fast_io &operator<<(const long long value) { printf("%" PRId64, value); return *this; } fast_io &operator<<(const unsigned long long value) { printf("%" PRIu64, value); return *this; } fast_io &operator<<(const string &value) { printf("%s", value.c_str()); return *this; } template <typename T> fast_io &operator<<(const vector<T> &target) { for (auto &value : target) { *this << value << " "; } return *this; } }; fast_io fio; template <class ForwardIt, class UnaryPredicate> ForwardIt first_true(ForwardIt first, ForwardIt last, UnaryPredicate predicate) { ForwardIt it; typename std::iterator_traits<ForwardIt>::difference_type count, step; count = std::distance(first, last); while (count > 0) { it = first; step = count / 2; std::advance(it, step); if (!predicate(*it)) { first = ++it; count -= step + 1; } else count = step; } return first; } #define MOD 1000000007 #define pii pair<int, int> #define pll pair<long long, long long> #define all(x) (x).begin(), (x).end() #define sort_all(x) sort(all(x)) #define make_unique(x) (x).erase(unique(all(x)), (x).end()) template <int mod> class ModNum { private: int num; inline int easy_mod(int value) const { while (value > mod) value -= mod; return value; } inline int int32_normalize(const int value) const { if (value < 0) return mod * (-value / mod + 1); else if (value >= mod) return value % mod; else return value; } public: ModNum(const int value) { num = int32_normalize(value); } ModNum(const long long value) { num = int32_normalize(value % mod); } operator int() const { return num; } ModNum &operator=(const ModNum<mod> &rhs) { this->num = rhs.num; return *this; } ModNum operator+(const ModNum &rhs) const { return ModNum(easy_mod(rhs.num + num)); } ModNum &operator+=(const ModNum &rhs) { this->num = easy_mod(num + rhs.num); return *this; } ModNum operator-(const ModNum &rhs) const { return ModNum(easy_mod(num + mod - rhs.num)); } ModNum &operator-=(const ModNum &rhs) { this->num = easy_mod(num + mod - rhs.num); return *this; } ModNum operator*(const int &rhs) const { long long x = (long long)num * rhs; return ModNum(x < mod ? x : (x % mod)); } ModNum &operator*=(const int &rhs) { long long x = (long long)num * rhs; this->num = x < mod ? (int)x : (x % mod); return *this; } ModNum operator/(const ModNum &rhs) const { return div(rhs); } ModNum operator/(const int rhs) const { return div(rhs); } ModNum div(const ModNum &other) const { return (*this) * (other.pow(mod - 2)); } ModNum div(const int &other) const { return div(ModNum(other)); } inline ModNum pow(const unsigned long long power) const { ModNum resp = 1; unsigned long long power2_value = 1; ModNum power2_mod = *this; while (power2_value <= power) { if (power & power2_value) resp *= power2_mod; power2_mod *= power2_mod; power2_value *= 2ULL; } return resp; } inline ModNum pow(const int power) const { return this->pow((const unsigned long long)power); } }; typedef ModNum<998244353> my_num; int main() { #if defined(_DEBUG) freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string s; int k; cin >> s >> k; if (k == 0) { cout << 1; return 0; } vector<int> vec; int k1 = 0, sum1 = 0; for (char c : s) { if (c == '0') { vec.push_back(k1); k1 = 0; } else { sum1++; k1++; } } if (k1 > 0) vec.push_back(k1); int max_total = min(k, sum1); vector<vector<my_num>> dp(sum1 + 1, vector<my_num>(max_total + 1, my_num(0))); for (int i = 0; i <= vec.back(); i++) { dp[i][i] = 1; } for (int i = vec.size() - 2; i >= 0; i--) { vector<vector<my_num>> new_dp = dp; for (int j = 0; j <= max_total; j++) { for (int k = sum1 - 1; k >= 0; k--) new_dp[k][j] += new_dp[k + 1][j]; } for (int z = 1; z <= vec[i]; z++) { for (int j = 0; j <= max_total; j++) { if (j - z < 0) continue; for (int k = 0; k <= sum1; k++) { if (k - z < 0) continue; new_dp[k][j] += dp[k - z][j - z]; } } } dp = new_dp; } my_num ans = 0; for (int i = 0; i <= max_total; i++) ans += dp[0][i]; cout << ans; return 0; }
#ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS #include <intrin.h> #define __builtin_popcount __popcnt #endif #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <cinttypes> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <unordered_map> #include <vector> using namespace std; template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto i = begin(v); i != end(v); i++) is >> *i; return is; } struct fast_io { fast_io &operator>>(int &target) { scanf("%" PRId32, &target); return *this; } fast_io &operator>>(unsigned int &target) { scanf("%" PRIu32, &target); return *this; } fast_io &operator>>(long long &target) { scanf("%" PRId64, &target); return *this; } fast_io &operator>>(unsigned long long &target) { scanf("%" PRIu64, &target); return *this; } template <typename T> fast_io &operator>>(vector<T> &target) { for (auto &value : target) { *this >> value; } return *this; } fast_io &operator<<(const int value) { printf("%" PRId32, value); return *this; } fast_io &operator<<(const unsigned int value) { printf("%" PRIu32, value); return *this; } fast_io &operator<<(const long long value) { printf("%" PRId64, value); return *this; } fast_io &operator<<(const unsigned long long value) { printf("%" PRIu64, value); return *this; } fast_io &operator<<(const string &value) { printf("%s", value.c_str()); return *this; } template <typename T> fast_io &operator<<(const vector<T> &target) { for (auto &value : target) { *this << value << " "; } return *this; } }; fast_io fio; template <class ForwardIt, class UnaryPredicate> ForwardIt first_true(ForwardIt first, ForwardIt last, UnaryPredicate predicate) { ForwardIt it; typename std::iterator_traits<ForwardIt>::difference_type count, step; count = std::distance(first, last); while (count > 0) { it = first; step = count / 2; std::advance(it, step); if (!predicate(*it)) { first = ++it; count -= step + 1; } else count = step; } return first; } #define MOD 1000000007 #define pii pair<int, int> #define pll pair<long long, long long> #define all(x) (x).begin(), (x).end() #define sort_all(x) sort(all(x)) #define make_unique(x) (x).erase(unique(all(x)), (x).end()) template <int mod> class ModNum { private: int num; inline int easy_mod(int value) const { while (value > mod) value -= mod; return value; } inline int int32_normalize(const int value) const { if (value < 0) return mod * (-value / mod + 1); else if (value >= mod) return value % mod; else return value; } public: ModNum(const int value) { num = int32_normalize(value); } ModNum(const long long value) { num = int32_normalize(value % mod); } operator int() const { return num; } ModNum &operator=(const ModNum<mod> &rhs) { this->num = rhs.num; return *this; } ModNum operator+(const ModNum &rhs) const { return ModNum(easy_mod(rhs.num + num)); } ModNum &operator+=(const ModNum &rhs) { this->num = easy_mod(num + rhs.num); return *this; } ModNum operator-(const ModNum &rhs) const { return ModNum(easy_mod(num + mod - rhs.num)); } ModNum &operator-=(const ModNum &rhs) { this->num = easy_mod(num + mod - rhs.num); return *this; } ModNum operator*(const int &rhs) const { long long x = (long long)num * rhs; return ModNum(x < mod ? x : (x % mod)); } ModNum &operator*=(const int &rhs) { long long x = (long long)num * rhs; this->num = x < mod ? (int)x : (x % mod); return *this; } ModNum operator/(const ModNum &rhs) const { return div(rhs); } ModNum operator/(const int rhs) const { return div(rhs); } ModNum div(const ModNum &other) const { return (*this) * (other.pow(mod - 2)); } ModNum div(const int &other) const { return div(ModNum(other)); } inline ModNum pow(const unsigned long long power) const { ModNum resp = 1; unsigned long long power2_value = 1; ModNum power2_mod = *this; while (power2_value <= power) { if (power & power2_value) resp *= power2_mod; power2_mod *= power2_mod; power2_value *= 2ULL; } return resp; } inline ModNum pow(const int power) const { return this->pow((const unsigned long long)power); } }; typedef ModNum<998244353> my_num; int main() { #if defined(_DEBUG) freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string s; int k; cin >> s >> k; if (k == 0) { cout << 1; return 0; } vector<int> vec; int k1 = 0, sum1 = 0; for (char c : s) { if (c == '0') { vec.push_back(k1); k1 = 0; } else { sum1++; k1++; } } if (k1 > 0) vec.push_back(k1); int max_total = min(k, sum1); vector<vector<my_num>> dp(sum1 + 1, vector<my_num>(max_total + 1, my_num(0))); for (int i = 0; i <= vec.back(); i++) { if (i <= sum1 && i <= max_total) dp[i][i] = 1; } for (int i = vec.size() - 2; i >= 0; i--) { vector<vector<my_num>> new_dp = dp; for (int j = 0; j <= max_total; j++) { for (int k = sum1 - 1; k >= 0; k--) new_dp[k][j] += new_dp[k + 1][j]; } for (int z = 1; z <= vec[i]; z++) { for (int j = 0; j <= max_total; j++) { if (j - z < 0) continue; for (int k = 0; k <= sum1; k++) { if (k - z < 0) continue; new_dp[k][j] += dp[k - z][j - z]; } } } dp = new_dp; } my_num ans = 0; for (int i = 0; i <= max_total; i++) ans += dp[0][i]; cout << ans; return 0; }
replace
245
246
245
247
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define per(i, a, b) for (int i = (b)-1; i >= (a); --i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<pii> vii; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll dp[302][302][302]; int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); memset(dp, 0ll, sizeof dp); int K; string s; cin >> s >> K; s.pb('0'); vi a; int cnt = 0; for (auto x : s) { if (x == '0') { a.pb(cnt); cnt = 0; } else { ++cnt; } } int n = sz(a); vi pref = a; rep(i, 1, n) pref[i] += pref[i - 1]; const ll mod = 998244353; // dp[i][j][k] => # sequences w [0...i) having prefix sum j and k needed // operations dp[0][0][0] = 1; rep(i, 0, n) { rep(j, 0, 301) { rep(k, 0, K + 1) { if (dp[i][j][k] == 0) continue; rep(nxt, max(0, pref[i] - j), pref[n - 1] - j + 1) { if (max(0, nxt - a[i]) + k > K) break; dp[i + 1][j + nxt][k + max(0, nxt - a[i])] += dp[i][j][k]; dp[i + 1][j + nxt][k + max(0, nxt - a[i])] %= mod; } } } } ll ans = 0; rep(j, 0, 301) { rep(k, 0, K + 1) { ans += dp[n][j][k]; ans %= mod; } } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define per(i, a, b) for (int i = (b)-1; i >= (a); --i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<pii> vii; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll dp[302][302][302]; int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); memset(dp, 0ll, sizeof dp); int K; string s; cin >> s >> K; ckmin(K, 300); s.pb('0'); vi a; int cnt = 0; for (auto x : s) { if (x == '0') { a.pb(cnt); cnt = 0; } else { ++cnt; } } int n = sz(a); vi pref = a; rep(i, 1, n) pref[i] += pref[i - 1]; const ll mod = 998244353; // dp[i][j][k] => # sequences w [0...i) having prefix sum j and k needed // operations dp[0][0][0] = 1; rep(i, 0, n) { rep(j, 0, 301) { rep(k, 0, K + 1) { if (dp[i][j][k] == 0) continue; rep(nxt, max(0, pref[i] - j), pref[n - 1] - j + 1) { if (max(0, nxt - a[i]) + k > K) break; dp[i + 1][j + nxt][k + max(0, nxt - a[i])] += dp[i][j][k]; dp[i + 1][j + nxt][k + max(0, nxt - a[i])] %= mod; } } } } ll ans = 0; rep(j, 0, 301) { rep(k, 0, K + 1) { ans += dp[n][j][k]; ans %= mod; } } cout << ans << '\n'; }
insert
39
39
39
40
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 998244353; ll po[302][302]; ll C[602][602]; void init() { for (int i = 0; i < 302; i++) { po[i][0] = 1; for (int j = 0; j + 1 < 302; j++) { po[i][j + 1] = po[i][j] * i; po[i][j + 1] %= MOD; } } for (int i = 0; i < 602; i++) { C[i][0] = C[i][i] = 1; for (int j = 1; j <= i - 1; j++) { C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; C[i][j] %= MOD; } } } int main() { init(); string s; ll k; cin >> s >> k; ll n = 0; ll c[302]; ll cnt = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { c[n++] = cnt; cnt = 0; } else cnt++; } c[n++] = cnt; /*cout<<n<<endl; for(int i=0;i<n;i++){ cout<<c[i]<<" "; }cout<<endl;*/ static ll dp[4][302][302] = {}; static ll t = 0, t_ = 1; dp[t][0][0] = 1; for (int x = 0; x < n; x++) { for (int i = 0; i < 302; i++) { for (int j = 0; j < 302; j++) { dp[t_][i][j] = dp[2][i][j] = dp[3][i][j] = 0; } } for (int i = 0; i < 302; i++) { if (i + c[x] + 1 >= 302) continue; for (int j = 0; j < 302; j++) { if (dp[t][i][j] == 0) continue; for (int y = 1; y <= j; y++) { dp[3][i + c[x] + 1][y] += MOD - dp[t][i][j] * C[j - y + c[x]][j - y] % MOD; } } } /*for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ cout<<dp[3][i][j]<<" "; }cout<<endl; } puts("==========");*/ for (int i = 1; i < 302; i++) { for (int j = 300; j >= 1; j--) { dp[2][i][j] = dp[t][i - 1][j] + dp[2][i - 1][j] + dp[2][i][j + 1]; dp[2][i][j] %= MOD; } for (int j = 1; j <= 300; j++) { dp[2][i][j] += dp[3][i][j]; dp[2][i][j] %= MOD; } } for (int i = 0; i < 302; i++) { for (int j = 0; j < 302; j++) { dp[t_][i][j] = dp[2][i][j]; if (j > 0) { dp[t_][i][j] += dp[t][i][j - 1]; if (dp[t_][i][j] >= MOD) dp[t_][i][j] -= MOD; } } } swap(t, t_); /*for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ cout<<dp[t][i][j]<<" "; }cout<<endl; } puts("==========");*/ } ll ret = 0; for (int i = 0; i <= k; i++) { for (int j = 1; j <= 301; j++) ret += dp[t][i][j]; } cout << ret % MOD << endl; /*ll dp[2][302][302]={}; ll t=0,t_=1; dp[t][0][0]=1; for(int x=0;x<n;x++){ for(int i=0;i<302;i++){ for(int j=0;j<302;j++){ dp[t_][i][j]=0; } } for(int i=0;i<302;i++){ for(int j=0;j<302;j++){ if(dp[t][i][j]==0)continue; dp[t_][i][j]+=dp[t][i][j]; dp[t_][i][j+1]+=MOD-dp[t][i][j]*(x+1-i)%MOD; dp[t_][i+1][j+1]+=dp[t][i][j]*(x-i)%MOD; dp[t_][i+1][j+c[x]+1]+=MOD-dp[t][i][j]*po[x-i][c[x]+1]%MOD; } } for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ cout<<dp[t_][i][j]<<" "; }cout<<endl; } puts("=========="); for(int i=0;i<302;i++){ dp[t_][i][0]%=MOD; for(int j=0;j+1<302;j++){ dp[t_][i][j+1]+=dp[t_][i][j]*(x+1-i); dp[t_][i][j+1]%=MOD; } } swap(t,t_); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ cout<<dp[t][i][j]<<" "; }cout<<endl; } puts("=========="); } ll ret=0; for(int i=0;i<302;i++){ for(int j=0;j<=k;j++){ ret+=dp[t][i][j]; } } cout<<ret%MOD<<endl;*/ }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 998244353; ll po[302][302]; ll C[602][602]; void init() { for (int i = 0; i < 302; i++) { po[i][0] = 1; for (int j = 0; j + 1 < 302; j++) { po[i][j + 1] = po[i][j] * i; po[i][j + 1] %= MOD; } } for (int i = 0; i < 602; i++) { C[i][0] = C[i][i] = 1; for (int j = 1; j <= i - 1; j++) { C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; C[i][j] %= MOD; } } } int main() { init(); string s; ll k; cin >> s >> k; ll n = 0; ll c[302]; ll cnt = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { c[n++] = cnt; cnt = 0; } else cnt++; } c[n++] = cnt; /*cout<<n<<endl; for(int i=0;i<n;i++){ cout<<c[i]<<" "; }cout<<endl;*/ static ll dp[4][302][302] = {}; static ll t = 0, t_ = 1; dp[t][0][0] = 1; for (int x = 0; x < n; x++) { for (int i = 0; i < 302; i++) { for (int j = 0; j < 302; j++) { dp[t_][i][j] = dp[2][i][j] = dp[3][i][j] = 0; } } for (int i = 0; i < 302; i++) { if (i + c[x] + 1 >= 302) continue; for (int j = 0; j < 302; j++) { if (dp[t][i][j] == 0) continue; for (int y = 1; y <= j; y++) { dp[3][i + c[x] + 1][y] += MOD - dp[t][i][j] * C[j - y + c[x]][j - y] % MOD; } } } /*for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ cout<<dp[3][i][j]<<" "; }cout<<endl; } puts("==========");*/ for (int i = 1; i < 302; i++) { for (int j = 300; j >= 1; j--) { dp[2][i][j] = dp[t][i - 1][j] + dp[2][i - 1][j] + dp[2][i][j + 1]; dp[2][i][j] %= MOD; } for (int j = 1; j <= 300; j++) { dp[2][i][j] += dp[3][i][j]; dp[2][i][j] %= MOD; } } for (int i = 0; i < 302; i++) { for (int j = 0; j < 302; j++) { dp[t_][i][j] = dp[2][i][j]; if (j > 0) { dp[t_][i][j] += dp[t][i][j - 1]; if (dp[t_][i][j] >= MOD) dp[t_][i][j] -= MOD; } } } swap(t, t_); /*for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ cout<<dp[t][i][j]<<" "; }cout<<endl; } puts("==========");*/ } ll ret = 0; for (int i = 0; i <= min(k, (ll)301); i++) { for (int j = 1; j <= 301; j++) ret += dp[t][i][j]; } cout << ret % MOD << endl; /*ll dp[2][302][302]={}; ll t=0,t_=1; dp[t][0][0]=1; for(int x=0;x<n;x++){ for(int i=0;i<302;i++){ for(int j=0;j<302;j++){ dp[t_][i][j]=0; } } for(int i=0;i<302;i++){ for(int j=0;j<302;j++){ if(dp[t][i][j]==0)continue; dp[t_][i][j]+=dp[t][i][j]; dp[t_][i][j+1]+=MOD-dp[t][i][j]*(x+1-i)%MOD; dp[t_][i+1][j+1]+=dp[t][i][j]*(x-i)%MOD; dp[t_][i+1][j+c[x]+1]+=MOD-dp[t][i][j]*po[x-i][c[x]+1]%MOD; } } for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ cout<<dp[t_][i][j]<<" "; }cout<<endl; } puts("=========="); for(int i=0;i<302;i++){ dp[t_][i][0]%=MOD; for(int j=0;j+1<302;j++){ dp[t_][i][j+1]+=dp[t_][i][j]*(x+1-i); dp[t_][i][j+1]%=MOD; } } swap(t,t_); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ cout<<dp[t][i][j]<<" "; }cout<<endl; } puts("=========="); } ll ret=0; for(int i=0;i<302;i++){ for(int j=0;j<=k;j++){ ret+=dp[t][i][j]; } } cout<<ret%MOD<<endl;*/ }
replace
105
106
105
106
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifdef NeverBeRed #include "debug.h" #else #define debug(...) 9715 #endif typedef long long ll; typedef long double ld; typedef complex<ld> point; #define F first #define S second const int mod = 998244353; int dp[305][305][305]; int dp2[305][305][305]; int dp3[305][305][305]; inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } int main() { #ifdef TurnRed // freopen("a.in", "r", stdin); // freopen("a.out", "w", stdout); #endif ios_base::sync_with_stdio(0), cin.tie(0); string s; int K; cin >> s >> K; vector<int> a = {0}; for (auto i : s) { if (i == '1') ++a.back(); else a.push_back(0); } int n = a.size(); int l = s.length(); dp[0][0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j <= l; ++j) for (int k = 0; k <= l; ++k) { // for (int x = 0; x <= min(a[i], j); ++x) // add(dp[i+1][j-x][k], //dp[i][j][k]); for (int x = 1; x + j <= l; ++x) add(dp[i+1][j+x][k+x], //dp[i][j][k]); add(dp2[i + 1][j][k], dp[i][j][k]); if (j - min(a[i], j) > 0) add(dp2[i + 1][j - min(a[i], j) - 1][k], mod - dp[i][j][k]); add(dp3[i + 1][j + 1][k + 1], dp[i][j][k]); } for (int j = 0; j <= l; ++j) for (int k = 0; k <= l; ++k) add(dp3[i + 1][j + 1][k + 1], dp3[i + 1][j][k]); for (int j = l; j >= 0; --j) for (int k = 0; k <= l; ++k) add(dp2[i + 1][j][k], dp2[i + 1][j + 1][k]); for (int j = 0; j <= l; ++j) for (int k = 0; k <= l; ++k) { dp[i + 1][j][k] = dp2[i + 1][j][k]; add(dp[i + 1][j][k], dp3[i + 1][j][k]); } } int ans = 0; for (int k = 0; k <= K; ++k) add(ans, dp[n][0][k]); cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef NeverBeRed #include "debug.h" #else #define debug(...) 9715 #endif typedef long long ll; typedef long double ld; typedef complex<ld> point; #define F first #define S second const int mod = 998244353; int dp[305][305][305]; int dp2[305][305][305]; int dp3[305][305][305]; inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } int main() { #ifdef TurnRed // freopen("a.in", "r", stdin); // freopen("a.out", "w", stdout); #endif ios_base::sync_with_stdio(0), cin.tie(0); string s; int K; cin >> s >> K; vector<int> a = {0}; for (auto i : s) { if (i == '1') ++a.back(); else a.push_back(0); } int n = a.size(); int l = s.length(); dp[0][0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j <= l; ++j) for (int k = 0; k <= l; ++k) { // for (int x = 0; x <= min(a[i], j); ++x) // add(dp[i+1][j-x][k], //dp[i][j][k]); for (int x = 1; x + j <= l; ++x) add(dp[i+1][j+x][k+x], //dp[i][j][k]); add(dp2[i + 1][j][k], dp[i][j][k]); if (j - min(a[i], j) > 0) add(dp2[i + 1][j - min(a[i], j) - 1][k], mod - dp[i][j][k]); add(dp3[i + 1][j + 1][k + 1], dp[i][j][k]); } for (int j = 0; j <= l; ++j) for (int k = 0; k <= l; ++k) add(dp3[i + 1][j + 1][k + 1], dp3[i + 1][j][k]); for (int j = l; j >= 0; --j) for (int k = 0; k <= l; ++k) add(dp2[i + 1][j][k], dp2[i + 1][j + 1][k]); for (int j = 0; j <= l; ++j) for (int k = 0; k <= l; ++k) { dp[i + 1][j][k] = dp2[i + 1][j][k]; add(dp[i + 1][j][k], dp3[i + 1][j][k]); } } int ans = 0; for (int k = 0; k <= min(K, l); ++k) add(ans, dp[n][0][k]); cout << ans << "\n"; return 0; }
replace
78
79
78
79
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define pb push_back #define eb emplace_back #define fi first #define se second using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; template <class T> inline void chmin(T &a, const T &b) { a = min(a, b); } template <class T> inline void chmax(T &a, const T &b) { a = max(a, b); } template <class T> inline bool kbit(const T &x, const int &k) { return ((x >> k) & 1LL); } inline int popcount(const int &n) { return __builtin_popcount(n); } inline ll popcountll(const ll &n) { return __builtin_popcountll(n); } inline ll mask(const ll &k) { return (1LL << k) - 1LL; } template <class T> void zip(V<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <class T> int lwb(V<T> &v, const T &x) { return lower_bound(all(v), x) - v.begin(); } void dump() { cerr << '\n'; } template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) { cerr << head << (sizeof...(Tail) == 0 ? " " : ", "); dump(std::move(tail)...); } template <class T> void print(const vector<T> &v) { for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' '); } template <class T> void read(vector<T> &v) { for (int i = 0; i < v.size(); i++) cin >> v[i]; } constexpr char sp = ' ', newl = '\n'; constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll MOD = 998244353LL; //////////////////////////////////////////////INSERT ABOVE HERE // from http://noshi91.hatenablog.com/entry/2019/03/31/174006 template <std::uint_fast64_t Modulus> class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint operator^(const u64 rhs) const noexcept { return modint(*this) ^= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } constexpr modint &operator^=(u64 exp) { modint rhs = modint(*this); a = 1; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } friend ostream &operator<<(ostream &os, const modint &x) { os << x.a; return os; } friend istream &operator>>(istream &is, modint &x) { is >> x.a; return is; } }; using mint = modint<MOD>; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int K; cin >> K; int n = s.length(); V<int> a(n + 1); int co = 0; for (int i = 0; i < n; i++) { if (s[i] == '0') { co++; } else { a[co]++; } } V<V<V<mint>>> dp(n + 1, V<V<mint>>(n + 1, V<mint>(n + 1))); dp[n][0][0] = 1; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= n; k++) { for (int l = 1; l <= a[i]; l++) { if (j + l > n || k + l > n) break; dp[i][j + l][k + l] += dp[i + 1][j][k]; } } // cout << i << " " << j << " "; // print(dp[i][j]); } for (int k = 0; k <= n; k++) { mint sum = 0; for (int j = n; j >= 0; j--) { sum += dp[i + 1][j][k]; dp[i][j][k] += sum; } } } mint res = 0; for (int i = 0; i <= K; i++) { res += dp[0][0][i]; } cout << res << newl; }
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define pb push_back #define eb emplace_back #define fi first #define se second using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; template <class T> inline void chmin(T &a, const T &b) { a = min(a, b); } template <class T> inline void chmax(T &a, const T &b) { a = max(a, b); } template <class T> inline bool kbit(const T &x, const int &k) { return ((x >> k) & 1LL); } inline int popcount(const int &n) { return __builtin_popcount(n); } inline ll popcountll(const ll &n) { return __builtin_popcountll(n); } inline ll mask(const ll &k) { return (1LL << k) - 1LL; } template <class T> void zip(V<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <class T> int lwb(V<T> &v, const T &x) { return lower_bound(all(v), x) - v.begin(); } void dump() { cerr << '\n'; } template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) { cerr << head << (sizeof...(Tail) == 0 ? " " : ", "); dump(std::move(tail)...); } template <class T> void print(const vector<T> &v) { for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' '); } template <class T> void read(vector<T> &v) { for (int i = 0; i < v.size(); i++) cin >> v[i]; } constexpr char sp = ' ', newl = '\n'; constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll MOD = 998244353LL; //////////////////////////////////////////////INSERT ABOVE HERE // from http://noshi91.hatenablog.com/entry/2019/03/31/174006 template <std::uint_fast64_t Modulus> class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint operator^(const u64 rhs) const noexcept { return modint(*this) ^= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } constexpr modint &operator^=(u64 exp) { modint rhs = modint(*this); a = 1; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } friend ostream &operator<<(ostream &os, const modint &x) { os << x.a; return os; } friend istream &operator>>(istream &is, modint &x) { is >> x.a; return is; } }; using mint = modint<MOD>; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int K; cin >> K; int n = s.length(); V<int> a(n + 1); int co = 0; for (int i = 0; i < n; i++) { if (s[i] == '0') { co++; } else { a[co]++; } } V<V<V<mint>>> dp(n + 1, V<V<mint>>(n + 1, V<mint>(n + 1))); dp[n][0][0] = 1; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= n; k++) { for (int l = 1; l <= a[i]; l++) { if (j + l > n || k + l > n) break; dp[i][j + l][k + l] += dp[i + 1][j][k]; } } // cout << i << " " << j << " "; // print(dp[i][j]); } for (int k = 0; k <= n; k++) { mint sum = 0; for (int j = n; j >= 0; j--) { sum += dp[i + 1][j][k]; dp[i][j][k] += sum; } } } mint res = 0; for (int i = 0; i <= min(n, K); i++) { res += dp[0][0][i]; } cout << res << newl; }
replace
158
159
158
159
0
p02635
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #define For(i, a, b) for (register int i = a; i <= b; ++i) using namespace std; const int maxn = 3e2 + 5; const int mod = 998244353; int f[maxn][maxn][maxn], K, N, n, num[maxn], ans; // x k owe char s[maxn]; void Add(int &x, const int &a) { x = (x + a) >= mod ? x + a - mod : x + a; } int main() { scanf("%s%d", s, &K); N = strlen(s), n = 1; f[0][0][0] = 1; For(i, 0, N - 1) { if (s[i] == '0') ++n; if (s[i] == '1') ++num[n]; } For(i, 0, n - 1) For(j, 0, K) For(k, 0, j) { Add(f[i + 1][j][k], f[i][j][k]); // in For(l, 1, K - j) Add(f[i + 1][j + l][k + l], f[i][j][k]); // out For(l, 1, min(k, num[i + 1])) Add(f[i + 1][j][k - l], f[i][j][k]); } For(i, 0, K) Add(ans, f[n][i][0]); printf("%d", ans); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #define For(i, a, b) for (register int i = a; i <= b; ++i) using namespace std; const int maxn = 3e2 + 5; const int mod = 998244353; int f[maxn][maxn][maxn], K, N, n, num[maxn], ans; // x k owe char s[maxn]; void Add(int &x, const int &a) { x = (x + a) >= mod ? x + a - mod : x + a; } int main() { scanf("%s%d", s, &K); N = strlen(s), n = 1; f[0][0][0] = 1; For(i, 0, N - 1) { if (s[i] == '0') ++n; if (s[i] == '1') ++num[n]; } K = min(K, N); For(i, 0, n - 1) For(j, 0, K) For(k, 0, j) { Add(f[i + 1][j][k], f[i][j][k]); // in For(l, 1, K - j) Add(f[i + 1][j + l][k + l], f[i][j][k]); // out For(l, 1, min(k, num[i + 1])) Add(f[i + 1][j][k - l], f[i][j][k]); } For(i, 0, K) Add(ans, f[n][i][0]); printf("%d", ans); return 0; }
insert
22
22
22
23
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using lint = long long; constexpr lint mod = 998244353; #define all(x) (x).begin(), (x).end() #define bitcount(n) __builtin_popcountl((lint)(n)) #define fcout cout << fixed << setprecision(15) #define highest(x) (63 - __builtin_clzl(x)) #define rep(i, n) for (int i = 0; i < int(n); i++) #define rep2(i, l, r) for (int i = int(l); i < int(r); i++) #define repr(i, n) for (int i = int(n) - 1; i >= 0; i--) #define repr2(i, l, r) for (int i = int(r) - 1; i >= int(l); i--) #define SZ(x) int(x.size()) constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18; inline void YES(bool condition) { if (condition) cout << "YES" << endl; else cout << "NO" << endl; } inline void Yes(bool condition) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; } inline void assertNO(bool condition) { if (!condition) { cout << "NO" << endl; exit(0); } } inline void assertNo(bool condition) { if (!condition) { cout << "No" << endl; exit(0); } } inline void assertm1(bool condition) { if (!condition) { cout << -1 << endl; exit(0); } } lint power(lint base, lint exponent, lint module) { if (exponent % 2) { return power(base, exponent - 1, module) * base % module; } else if (exponent) { lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; } else { return 1; } } struct position { int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; double euclidean(position first, position second) { return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); } template <class T, class U> string to_string(pair<T, U> x) { return to_string(x.first) + "," + to_string(x.second); } string to_string(string x) { return x; } template <class T> string to_string(complex<T> x) { return to_string(make_pair(x.real(), x.imag())); } template <class itr> void array_output(itr start, itr goal) { string ans; for (auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); if (!ans.empty()) ans.pop_back(); cout << ans << endl; } template <class itr> void cins(itr first, itr last) { for (auto i = first; i != last; i++) { cin >> (*i); } } template <class T> T gcd(T a, T b) { if (b) return gcd(b, a % b); else return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } struct combination { vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1) { fact[0] = 1; for (int i = 1; i <= sz; i++) { fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for (int i = sz - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } lint P(int n, int r) { if (r < 0 || n < r) return 0; return (fact[n] * inv[n - r] % mod); } lint C(int p, int q) { if (q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } }; template <class itr> bool next_sequence(itr first, itr last, int max_bound) { itr now = last; while (now != first) { now--; (*now)++; if ((*now) == max_bound) { (*now) = 0; } else { return true; } } return false; } template <class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2) { itr now = last; itr2 now2 = last2; while (now != first) { now--, now2--; (*now)++; if ((*now) == (*now2)) { (*now) = 0; } else { return true; } } return false; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline int at(lint i, int j) { return (i >> j) & 1; } random_device rnd; bool is_in_board(lint y, lint x, lint H, lint W) { return (0 <= y && y < H && 0 <= x && x < W); } lint dp[1000][1000]; // 今見ている0の場所、1を使った個数、1を移動した回数 int main() { string str; int K; cin >> str >> K; vector<int> one_cnt{0}; for (auto i : str) { if (i == '0') { one_cnt.push_back(0); } else { one_cnt.back()++; } } vector<int> sum; int now_sum = 0; for (int i : one_cnt) { now_sum += i; sum.push_back(now_sum); } int N = SZ(one_cnt), S = now_sum; rep(i, 1000) { rep(j, 1000) { dp[i][j] = 0; } } rep(i, K + 1) { dp[S][i] = 1; } repr(now, N) { vector<vector<lint>> dp2(S + 1, vector<lint>(K + 1)); rep(used, S + 1) { rep(changed, K + 1) { dp2[used][changed] = 0; int l = max(0, sum[now] - used), m = one_cnt[now], r = one_cnt[now] + K - changed; rep2(i, l, m) { dp2[used][changed] += dp[used + i][changed]; } rep2(i, max(l, m), r + 1) { dp2[used][changed] += dp[used + i][changed + i - m]; } dp2[used][changed] %= mod; } } rep(i, S + 1) { rep(j, K + 1) { dp[i][j] = dp2[i][j]; } } } cout << dp[0][0] << endl; }
#include <bits/stdc++.h> using namespace std; using lint = long long; constexpr lint mod = 998244353; #define all(x) (x).begin(), (x).end() #define bitcount(n) __builtin_popcountl((lint)(n)) #define fcout cout << fixed << setprecision(15) #define highest(x) (63 - __builtin_clzl(x)) #define rep(i, n) for (int i = 0; i < int(n); i++) #define rep2(i, l, r) for (int i = int(l); i < int(r); i++) #define repr(i, n) for (int i = int(n) - 1; i >= 0; i--) #define repr2(i, l, r) for (int i = int(r) - 1; i >= int(l); i--) #define SZ(x) int(x.size()) constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18; inline void YES(bool condition) { if (condition) cout << "YES" << endl; else cout << "NO" << endl; } inline void Yes(bool condition) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; } inline void assertNO(bool condition) { if (!condition) { cout << "NO" << endl; exit(0); } } inline void assertNo(bool condition) { if (!condition) { cout << "No" << endl; exit(0); } } inline void assertm1(bool condition) { if (!condition) { cout << -1 << endl; exit(0); } } lint power(lint base, lint exponent, lint module) { if (exponent % 2) { return power(base, exponent - 1, module) * base % module; } else if (exponent) { lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; } else { return 1; } } struct position { int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; double euclidean(position first, position second) { return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); } template <class T, class U> string to_string(pair<T, U> x) { return to_string(x.first) + "," + to_string(x.second); } string to_string(string x) { return x; } template <class T> string to_string(complex<T> x) { return to_string(make_pair(x.real(), x.imag())); } template <class itr> void array_output(itr start, itr goal) { string ans; for (auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); if (!ans.empty()) ans.pop_back(); cout << ans << endl; } template <class itr> void cins(itr first, itr last) { for (auto i = first; i != last; i++) { cin >> (*i); } } template <class T> T gcd(T a, T b) { if (b) return gcd(b, a % b); else return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } struct combination { vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1) { fact[0] = 1; for (int i = 1; i <= sz; i++) { fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for (int i = sz - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } lint P(int n, int r) { if (r < 0 || n < r) return 0; return (fact[n] * inv[n - r] % mod); } lint C(int p, int q) { if (q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } }; template <class itr> bool next_sequence(itr first, itr last, int max_bound) { itr now = last; while (now != first) { now--; (*now)++; if ((*now) == max_bound) { (*now) = 0; } else { return true; } } return false; } template <class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2) { itr now = last; itr2 now2 = last2; while (now != first) { now--, now2--; (*now)++; if ((*now) == (*now2)) { (*now) = 0; } else { return true; } } return false; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline int at(lint i, int j) { return (i >> j) & 1; } random_device rnd; bool is_in_board(lint y, lint x, lint H, lint W) { return (0 <= y && y < H && 0 <= x && x < W); } lint dp[1000][1000]; // 今見ている0の場所、1を使った個数、1を移動した回数 int main() { string str; int K; cin >> str >> K; K = min(K, 300); vector<int> one_cnt{0}; for (auto i : str) { if (i == '0') { one_cnt.push_back(0); } else { one_cnt.back()++; } } vector<int> sum; int now_sum = 0; for (int i : one_cnt) { now_sum += i; sum.push_back(now_sum); } int N = SZ(one_cnt), S = now_sum; rep(i, 1000) { rep(j, 1000) { dp[i][j] = 0; } } rep(i, K + 1) { dp[S][i] = 1; } repr(now, N) { vector<vector<lint>> dp2(S + 1, vector<lint>(K + 1)); rep(used, S + 1) { rep(changed, K + 1) { dp2[used][changed] = 0; int l = max(0, sum[now] - used), m = one_cnt[now], r = one_cnt[now] + K - changed; rep2(i, l, m) { dp2[used][changed] += dp[used + i][changed]; } rep2(i, max(l, m), r + 1) { dp2[used][changed] += dp[used + i][changed + i - m]; } dp2[used][changed] %= mod; } } rep(i, S + 1) { rep(j, K + 1) { dp[i][j] = dp2[i][j]; } } } cout << dp[0][0] << endl; }
insert
167
167
167
168
0
p02635
C++
Runtime Error
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template <class T> using PQ = priority_queue<T>; template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>; const int INF = 0xccccccc; const ll LINF = 922337203685477580LL; template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second; } // const int mod = 1000000007; const int mod = 998244353; struct mint { int64_t x; mint(int64_t 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(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) { 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; } #define N 310 // head string s; int k, n; int cnt0; int info[N]; mint dp[N][N], sum1[N][N], sum2[N][N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s >> k; n = s.size(); rep(i, n) cnt0 += (s[i] == '0'); chmin(k, n - cnt0); int now = 0; for (int i = 0; i < n;) { if (s[i] == '0') { i++; now++; continue; } int j = i + 1; while (j < n and s[j] == '1') j++; info[now] = j - i; i = j; } dp[0][0] = 1; for (int i = 1; i <= k + 1; i++) sum1[0][i] = 1; for (int i = 1; i <= k + 1; i++) sum2[i][i] = 1; for (int i = cnt0; i >= 0; i--) { rep(j, k + 1) { rep(u, k + 1) { int mi = u - max(0, u - info[i]); dp[j][u] = sum1[j][k + 1] - sum1[j][u] + sum2[j][u] - sum2[j - mi][u - mi]; } } rep(j, k + 1) for (int u = 1; u <= k + 1; u++) sum1[j][u] = sum1[j][u - 1] + dp[j][u - 1]; for (int j = 1; j <= k + 1; j++) rep(u, j) sum2[j][j - u] = sum2[j - 1][j - u - 1] + dp[j - 1][j - u - 1]; // rep(i, k+1) rep(j, k+1) cout << dp[i][j] << (j == k?'\n':' '); // cout << '\n'; } mint ans; rep(i, k + 1) ans += dp[i][0]; cout << ans << endl; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template <class T> using PQ = priority_queue<T>; template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>; const int INF = 0xccccccc; const ll LINF = 922337203685477580LL; template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second; } // const int mod = 1000000007; const int mod = 998244353; struct mint { int64_t x; mint(int64_t 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(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) { 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; } #define N 310 // head string s; int k, n; int cnt0; int info[N]; mint dp[N][N], sum1[N][N], sum2[N][N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s >> k; n = s.size(); rep(i, n) cnt0 += (s[i] == '0'); chmin(k, n - cnt0); int now = 0; for (int i = 0; i < n;) { if (s[i] == '0') { i++; now++; continue; } int j = i + 1; while (j < n and s[j] == '1') j++; info[now] = j - i; i = j; } dp[0][0] = 1; for (int i = 1; i <= k + 1; i++) sum1[0][i] = 1; for (int i = 1; i <= k + 1; i++) sum2[i][i] = 1; for (int i = cnt0; i >= 0; i--) { rep(j, k + 1) { rep(u, j + 1) { int mi = u - max(0, u - info[i]); dp[j][u] = sum1[j][k + 1] - sum1[j][u] + sum2[j][u] - sum2[j - mi][u - mi]; } } rep(j, k + 1) for (int u = 1; u <= k + 1; u++) sum1[j][u] = sum1[j][u - 1] + dp[j][u - 1]; for (int j = 1; j <= k + 1; j++) rep(u, j) sum2[j][j - u] = sum2[j - 1][j - u - 1] + dp[j - 1][j - u - 1]; // rep(i, k+1) rep(j, k+1) cout << dp[i][j] << (j == k?'\n':' '); // cout << '\n'; } mint ans; rep(i, k + 1) ans += dp[i][0]; cout << ans << endl; }
replace
111
112
111
112
0
p02635
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<int, int> P; const int MOD = 998244353; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3f; ll dp[400][400][400]; int a[400]; int d[400]; int main() { string s; cin >> s; int K; cin >> K; int n = 0; int cnt = 0; for (char c : s) { if (c == '0') cnt++; else a[n++] = cnt; } rep(i, n) { d[a[i]]++; } dp[cnt + 1][0][0] = 1; for (int i = cnt + 1; i >= 1; i--) rep(j, K + 1) rep(k, n + 1) { if (dp[i][j][k] == 0) continue; for (int t = 0; t <= d[i - 1] + k; t++) { int nj = j + max(0, d[i - 1] - t); //~ if(nj>K)break; (dp[i - 1][nj][d[i - 1] + k - t] += dp[i][j][k]) %= MOD; } } ll ans = 0; rep(i, K + 1) { (ans += dp[0][i][0]) %= MOD; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<int, int> P; const int MOD = 998244353; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3f; ll dp[400][400][400]; int a[400]; int d[400]; int main() { string s; cin >> s; int K; cin >> K; int n = 0; int cnt = 0; for (char c : s) { if (c == '0') cnt++; else a[n++] = cnt; } rep(i, n) { d[a[i]]++; } K = min(K, n); dp[cnt + 1][0][0] = 1; for (int i = cnt + 1; i >= 1; i--) rep(j, K + 1) rep(k, n + 1) { if (dp[i][j][k] == 0) continue; for (int t = 0; t <= d[i - 1] + k; t++) { int nj = j + max(0, d[i - 1] - t); //~ if(nj>K)break; (dp[i - 1][nj][d[i - 1] + k - t] += dp[i][j][k]) %= MOD; } } ll ans = 0; rep(i, K + 1) { (ans += dp[0][i][0]) %= MOD; } cout << ans << endl; }
insert
28
28
28
29
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define ld long double #define ff first #define ss second #define pb push_back #define mp make_pair #define all(a) a.begin(), a.end() #define sz(a) (ll)(a.size()) const int M = 302; const int mod = 998244353; int don[M][M], bor[M][M], dp[M][M]; void compute() { for (int j = 300; j >= 0; j--) for (int k = 300; k >= 0; k--) bor[j][k] = (dp[j][k] + bor[j + 1][k + 1]) % mod; for (int j = 0; j <= 300; j++) { for (int k = 0; k <= 300; k++) { don[j][k] = dp[j][k]; if (j) don[j][k] = (don[j][k] + don[j - 1][k]) % mod; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; int K; cin >> K; if (s[sz(s) - 1] != '0') s = s + "0"; std::vector<int> v; int c = 0; for (int i = 0; i < sz(s); i++) { if (s[i] == '0') { v.pb(c); c = 0; } else c++; } for (int i = 0; i <= K; i++) dp[0][i] = 1; compute(); for (int i = sz(v) - 1; i >= 0; i--) { for (int j = 0; j <= 300; j++) { for (int k = 0; k <= 300; k++) { // borrow int a = bor[j + 1][k + 1]; // donate int x = v[i]; int b = don[j][k]; if (j > x) b = (b - don[j - x - 1][k] + mod) % mod; dp[j][k] = (a + b) % mod; } } compute(); } cout << dp[0][0] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define ld long double #define ff first #define ss second #define pb push_back #define mp make_pair #define all(a) a.begin(), a.end() #define sz(a) (ll)(a.size()) const int M = 302; const int mod = 998244353; int don[M][M], bor[M][M], dp[M][M]; void compute() { for (int j = 300; j >= 0; j--) for (int k = 300; k >= 0; k--) bor[j][k] = (dp[j][k] + bor[j + 1][k + 1]) % mod; for (int j = 0; j <= 300; j++) { for (int k = 0; k <= 300; k++) { don[j][k] = dp[j][k]; if (j) don[j][k] = (don[j][k] + don[j - 1][k]) % mod; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; int K; cin >> K; if (s[sz(s) - 1] != '0') s = s + "0"; std::vector<int> v; int c = 0; for (int i = 0; i < sz(s); i++) { if (s[i] == '0') { v.pb(c); c = 0; } else c++; } for (int i = 0; i <= min(K, 300); i++) dp[0][i] = 1; compute(); for (int i = sz(v) - 1; i >= 0; i--) { for (int j = 0; j <= 300; j++) { for (int k = 0; k <= 300; k++) { // borrow int a = bor[j + 1][k + 1]; // donate int x = v[i]; int b = don[j][k]; if (j > x) b = (b - don[j - x - 1][k] + mod) % mod; dp[j][k] = (a + b) % mod; } } compute(); } cout << dp[0][0] << "\n"; return 0; }
replace
49
50
49
50
0
p02635
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits.h> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = b - 1; i >= a; i--) #define ALL(a) a.begin(), a.end() using pii = pair<int, int>; using piii = pair<pii, int>; using pll = pair<long long, long long>; using plll = pair<pll, long long>; // #pragma GCC optimize("Ofast") #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define pb push_back #define mp make_pair #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define isSquare(x) (sqrt(x) * sqrt(x) == x) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }; template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }; 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; } const double EPS = 1e-9; // const int mod = 1e9 + 7; const int mod = 998244353; const int INF = 1e9; const long long INFLL = 1e18; void iosetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } template <class S, class T> pair<S, T> operator*(const pair<S, T> &s, const S &t) { return pair<S, T>(s.first * t, s.second * t); } template <typename T> void Exit(T first) { cout << first << endl; exit(0); }; template <int mod> struct ModInt { unsigned x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt<mod>; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const pii dxy[4] = {pii(1, 0), pii(0, 1), pii(-1, 0), pii(0, -1)}; bool range(int a, int b, int x) { if (a <= x and x < b) return true; else return false; } bool range(int a, int b, int c, int d, pii p) { if (a <= p.first and p.first < b and c <= p.second and p.second < d) return true; else return false; } vector<int> rlc(string &S) { int n = S.size(); int cnt = 0; vector<int> res; rep(i, 0, n) { if (S[i] == '0') { res.emplace_back(cnt); cnt = 0; } else { cnt++; } } res.emplace_back(cnt); return res; } int main() { iosetup(); string S; cin >> S; int K; cin >> K; auto A = rlc(S); reverse(ALL(A)); // cerr << A << endl; int n = A.size(); auto dp = make_vec<modint>(n + 1, 310, 310); dp[0][0][0] = 1; rep(i, 0, n) { rep(j, 0, 310) { rep(k, 0, A[i] + 1) { if (j + k > K or j + k >= 305) break; rep(l, 0, 310) dp[i + 1][j + k][l] += dp[i][j][l]; } } rep(j, 0, 310) { rep(k, 0, j + 1) { rep(l, 1, 310) { if (j < k + l or k + l > 305) break; dp[i + 1][j][k + l] += dp[i][j][k]; } } } } modint ans = 0; // rep(i, 0, K + 1){ // cerr << i << " " << dp[n][i][i] << endl; // } rep(i, 0, K + 1) ans += dp[n][i][i]; cout << ans << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits.h> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = b - 1; i >= a; i--) #define ALL(a) a.begin(), a.end() using pii = pair<int, int>; using piii = pair<pii, int>; using pll = pair<long long, long long>; using plll = pair<pll, long long>; // #pragma GCC optimize("Ofast") #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define pb push_back #define mp make_pair #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define isSquare(x) (sqrt(x) * sqrt(x) == x) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }; template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }; 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; } const double EPS = 1e-9; // const int mod = 1e9 + 7; const int mod = 998244353; const int INF = 1e9; const long long INFLL = 1e18; void iosetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } template <class S, class T> pair<S, T> operator*(const pair<S, T> &s, const S &t) { return pair<S, T>(s.first * t, s.second * t); } template <typename T> void Exit(T first) { cout << first << endl; exit(0); }; template <int mod> struct ModInt { unsigned x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt<mod>; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const pii dxy[4] = {pii(1, 0), pii(0, 1), pii(-1, 0), pii(0, -1)}; bool range(int a, int b, int x) { if (a <= x and x < b) return true; else return false; } bool range(int a, int b, int c, int d, pii p) { if (a <= p.first and p.first < b and c <= p.second and p.second < d) return true; else return false; } vector<int> rlc(string &S) { int n = S.size(); int cnt = 0; vector<int> res; rep(i, 0, n) { if (S[i] == '0') { res.emplace_back(cnt); cnt = 0; } else { cnt++; } } res.emplace_back(cnt); return res; } int main() { iosetup(); string S; cin >> S; int K; cin >> K; auto A = rlc(S); reverse(ALL(A)); // cerr << A << endl; int n = A.size(); auto dp = make_vec<modint>(n + 1, 310, 310); dp[0][0][0] = 1; rep(i, 0, n) { rep(j, 0, 310) { rep(k, 0, A[i] + 1) { if (j + k > K or j + k >= 305) break; rep(l, 0, 310) dp[i + 1][j + k][l] += dp[i][j][l]; } } rep(j, 0, 310) { rep(k, 0, j + 1) { rep(l, 1, 310) { if (j < k + l or k + l > 305) break; dp[i + 1][j][k + l] += dp[i][j][k]; } } } } modint ans = 0; // rep(i, 0, K + 1){ // cerr << i << " " << dp[n][i][i] << endl; // } rep(i, 0, min(K + 1, 305)) ans += dp[n][i][i]; cout << ans << endl; return 0; }
replace
248
249
248
249
0
p02635
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : (x)) #define all(x) x.begin(), x.end() // sort(all(x)) #define sz(x) (int)(x).size() #define mp(x, y) make_pair((x), (y)) typedef long long ll; typedef double T; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tiii; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef vector<T> vt; typedef vector<vt> vvt; typedef vector<pii> vpi; typedef vector<pii> vpl; typedef vector<vpi> graph; typedef complex<T> Pt; typedef pair<Pt, Pt> Line; const ll MOD = 998244353; const T EPS = 1e-10; const int INF = 1e9 + 1; /* Mod Library */ struct Mod { ll x; Mod(ll x = 0) : x(x) {} Mod operator+(Mod b) { return Mod((x + b.x) % MOD); } const Mod &operator+=(Mod b) { x = (x + b.x) % MOD; return *this; } Mod operator-(Mod b) { return Mod((x - b.x + MOD) % MOD); } const Mod &operator-=(Mod b) { x = (x - b.x + MOD) % MOD; return *this; } Mod operator*(Mod b) { return Mod((x * b.x) % MOD); } Mod operator/(Mod b) { return *this * inverse(b); } Mod operator%(Mod b) { return Mod(x % b.x); } static ll euclid(ll a, ll b, ll &x, ll &y) { if (b) { ll d = euclid(b, a % b, y, x); y -= a / b * x; return d; } x = 1; y = 0; return a; } Mod inverse(Mod a) { ll x, y, g = euclid(a.x, MOD, x, y); assert(g == 1); return Mod((x + MOD) % MOD); } Mod operator^(ll e) { if (!e) return Mod(1); Mod r = *this ^ (e / 2); r = r * r; return e & 1 ? *this * r : r; } }; istream &operator>>(istream &in, Mod &b) { in >> b.x; return in; } ostream &operator<<(ostream &out, Mod &b) { out << b.x; return out; } /* Input Library */ template <typename A, typename B> istream &operator>>(istream &in, pair<A, B> &p) { in >> p.first >> p.second; return in; } template <typename A, typename B> ostream &operator<<(ostream &out, pair<A, B> &p) { out << p.first << " " << p.second; return out; } template <typename I> istream &operator>>(istream &in, vector<I> &a) { trav(i, a) in >> i; return in; } template <typename I> ostream &operator<<(ostream &out, vector<I> &a) { rep(i, 0, sz(a)) { if (i) out << " "; out << a[i]; } return out; } void fast_io() { cin.sync_with_stdio(0); cin.tie(0); // cout.tie(0); cin.exceptions(cin.failbit); } // index * num moves used * num ones available. Mod dp[301][301][301]; void solve_test() { string ss; int n, K; cin >> ss >> K; ss = ss + '0'; while (ss.size() && ss[0] == '1') { ss = ss.substr(1); } n = ss.size(); K = min(K, n); if (K == 0) { cout << 1 << endl; return; } vector<int> s; for (int i = 0; i < n; ++i) { int tl = i; while (tl < n && ss[tl] == '1') { ++tl; } s.emplace_back(tl - i); i = tl; } n = s.size(); dp[n][0][0] = 1; for (int i = n - 1; i >= 0; --i) { for (int j = 0; j <= K; ++j) { for (int k = 0; k <= K; ++k) { // giving up non-zero amount of ones. for (int l = max(s[i] - k, 0); l <= s[i]; ++l) { dp[i][j][k] += dp[i + 1][j][k - s[i] + l]; } // taking positive amount of ones. for (int l = 1; l <= min(j, K - k); ++l) { dp[i][j][k] += dp[i + 1][j - l][k + l]; } } } } Mod ans = 0; for (int i = 0; i <= K; ++i) { ans += dp[0][i][0]; } cout << ans << endl; } void setup() {} int main() { setup(); srand(time(0)); fast_io(); int t = 1; // cin >> t; while (t--) { solve_test(); } }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : (x)) #define all(x) x.begin(), x.end() // sort(all(x)) #define sz(x) (int)(x).size() #define mp(x, y) make_pair((x), (y)) typedef long long ll; typedef double T; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tiii; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef vector<T> vt; typedef vector<vt> vvt; typedef vector<pii> vpi; typedef vector<pii> vpl; typedef vector<vpi> graph; typedef complex<T> Pt; typedef pair<Pt, Pt> Line; const ll MOD = 998244353; const T EPS = 1e-10; const int INF = 1e9 + 1; /* Mod Library */ struct Mod { ll x; Mod(ll x = 0) : x(x) {} Mod operator+(Mod b) { return Mod((x + b.x) % MOD); } const Mod &operator+=(Mod b) { x = (x + b.x) % MOD; return *this; } Mod operator-(Mod b) { return Mod((x - b.x + MOD) % MOD); } const Mod &operator-=(Mod b) { x = (x - b.x + MOD) % MOD; return *this; } Mod operator*(Mod b) { return Mod((x * b.x) % MOD); } Mod operator/(Mod b) { return *this * inverse(b); } Mod operator%(Mod b) { return Mod(x % b.x); } static ll euclid(ll a, ll b, ll &x, ll &y) { if (b) { ll d = euclid(b, a % b, y, x); y -= a / b * x; return d; } x = 1; y = 0; return a; } Mod inverse(Mod a) { ll x, y, g = euclid(a.x, MOD, x, y); assert(g == 1); return Mod((x + MOD) % MOD); } Mod operator^(ll e) { if (!e) return Mod(1); Mod r = *this ^ (e / 2); r = r * r; return e & 1 ? *this * r : r; } }; istream &operator>>(istream &in, Mod &b) { in >> b.x; return in; } ostream &operator<<(ostream &out, Mod &b) { out << b.x; return out; } /* Input Library */ template <typename A, typename B> istream &operator>>(istream &in, pair<A, B> &p) { in >> p.first >> p.second; return in; } template <typename A, typename B> ostream &operator<<(ostream &out, pair<A, B> &p) { out << p.first << " " << p.second; return out; } template <typename I> istream &operator>>(istream &in, vector<I> &a) { trav(i, a) in >> i; return in; } template <typename I> ostream &operator<<(ostream &out, vector<I> &a) { rep(i, 0, sz(a)) { if (i) out << " "; out << a[i]; } return out; } void fast_io() { cin.sync_with_stdio(0); cin.tie(0); // cout.tie(0); cin.exceptions(cin.failbit); } // index * num moves used * num ones available. Mod dp[301][301][301]; void solve_test() { string ss; int n, K; cin >> ss >> K; ss = ss + '0'; while (ss.size() && ss[0] == '1') { ss = ss.substr(1); } n = ss.size(); K = min(K, n); if (K == 0) { cout << 1 << endl; return; } vector<int> s; for (int i = 0; i < n; ++i) { int tl = i; while (tl < n && ss[tl] == '1') { ++tl; } s.emplace_back(tl - i); i = tl; } n = s.size(); dp[n][0][0] = 1; for (int i = n - 1; i >= 0; --i) { for (int j = 0; j <= K; ++j) { for (int k = 0; k <= K - j; ++k) { // giving up non-zero amount of ones. for (int l = max(s[i] - k, 0); l <= s[i]; ++l) { dp[i][j][k] += dp[i + 1][j][k - s[i] + l]; } // taking positive amount of ones. for (int l = 1; l <= min(j, K - k); ++l) { dp[i][j][k] += dp[i + 1][j - l][k + l]; } } } } Mod ans = 0; for (int i = 0; i <= K; ++i) { ans += dp[0][i][0]; } cout << ans << endl; } void setup() {} int main() { setup(); srand(time(0)); fast_io(); int t = 1; // cin >> t; while (t--) { solve_test(); } }
replace
153
154
153
154
TLE
p02635
C++
Runtime Error
#include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; const int MOD = 998244353; // change if needed struct Modint { ll val; Modint(ll _val = 0) : val(_val % MOD) {} Modint operator+(Modint other) const { return Modint(val + other.val); } void operator+=(Modint other) { val += other.val; val %= MOD; } Modint operator-() const { return Modint(MOD - val); } Modint operator-(Modint other) const { return Modint(val + MOD - other.val); } void operator-=(Modint other) { val += MOD - other.val; val %= MOD; } Modint operator*(Modint other) const { return Modint(val * other.val); } void operator*=(Modint other) { val *= other.val; val %= MOD; } bool operator==(Modint other) const { return val == other.val; } bool operator!=(Modint other) const { return val != other.val; } }; Modint exp(Modint a, int k) { if (k == 0) { return Modint(1); } else if (k % 2 == 0) { Modint half = exp(a, k / 2); return half * half; } else { return a * exp(a, k - 1); } } Modint inv(Modint a) { return exp(a, MOD - 2); } ostream &operator<<(ostream &out, Modint p) { out << p.val; return out; } const int MAX_N = 305; Modint dp[MAX_N][MAX_N]; Modint prea[MAX_N][MAX_N]; Modint preb[MAX_N][MAX_N]; int main() { string s; int K; cin >> s >> K; vector<int> orig; int buf = 0; for (char c : s) { if (c == '0') { orig.push_back(buf); buf = 0; } else { buf++; } } orig.push_back(buf); buf = 0; int n = orig.size(); int sfx = 0; for (int i = 0; i < n; i++) { sfx += orig[i]; } dp[0][0] = Modint(1); for (int i = 0; i < n; i++) { sfx -= orig[i]; // reset prea and preb for (int j = 0; j < MAX_N; j++) { for (int k = 0; k < MAX_N; k++) { prea[j][k] = 0; preb[j][k] = 0; } } // calculate the new prea and preb for (int j = 0; j < MAX_N; j++) { for (int k = 0; k < MAX_N; k++) { prea[j][k] += dp[j][k]; if (k - orig[i] - 1 >= 0) { prea[j][k - orig[i] - 1] -= dp[j][k]; } int lft = sfx - k; if (lft > 0) { if (j + 1 < MAX_N && k + 1 < MAX_N) { preb[j + 1][k + 1] += dp[j][k]; } if (j + lft + 1 < MAX_N && k + lft + 1 < MAX_N) { preb[j + lft + 1][k + lft + 1] -= dp[j][k]; } } } } // prefixize prea by k in reverse for (int j = 0; j < MAX_N; j++) { for (int k = MAX_N - 2; k >= 0; k--) { prea[j][k] += prea[j][k + 1]; } } // prefixize preb by the diagonals for (int j = 1; j < MAX_N; j++) { for (int k = 1; k < MAX_N; k++) { preb[j][k] += preb[j - 1][k - 1]; } } // update the dp for (int j = 0; j < MAX_N; j++) { for (int k = 0; k < MAX_N; k++) { dp[j][k] = prea[j][k] + preb[j][k]; } } } Modint ans(0); for (int j = 0; j <= K; j++) { ans += dp[j][0]; } cout << ans << endl; }
#include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; const int MOD = 998244353; // change if needed struct Modint { ll val; Modint(ll _val = 0) : val(_val % MOD) {} Modint operator+(Modint other) const { return Modint(val + other.val); } void operator+=(Modint other) { val += other.val; val %= MOD; } Modint operator-() const { return Modint(MOD - val); } Modint operator-(Modint other) const { return Modint(val + MOD - other.val); } void operator-=(Modint other) { val += MOD - other.val; val %= MOD; } Modint operator*(Modint other) const { return Modint(val * other.val); } void operator*=(Modint other) { val *= other.val; val %= MOD; } bool operator==(Modint other) const { return val == other.val; } bool operator!=(Modint other) const { return val != other.val; } }; Modint exp(Modint a, int k) { if (k == 0) { return Modint(1); } else if (k % 2 == 0) { Modint half = exp(a, k / 2); return half * half; } else { return a * exp(a, k - 1); } } Modint inv(Modint a) { return exp(a, MOD - 2); } ostream &operator<<(ostream &out, Modint p) { out << p.val; return out; } const int MAX_N = 305; Modint dp[MAX_N][MAX_N]; Modint prea[MAX_N][MAX_N]; Modint preb[MAX_N][MAX_N]; int main() { string s; int K; cin >> s >> K; vector<int> orig; int buf = 0; for (char c : s) { if (c == '0') { orig.push_back(buf); buf = 0; } else { buf++; } } orig.push_back(buf); buf = 0; int n = orig.size(); int sfx = 0; for (int i = 0; i < n; i++) { sfx += orig[i]; } dp[0][0] = Modint(1); for (int i = 0; i < n; i++) { sfx -= orig[i]; // reset prea and preb for (int j = 0; j < MAX_N; j++) { for (int k = 0; k < MAX_N; k++) { prea[j][k] = 0; preb[j][k] = 0; } } // calculate the new prea and preb for (int j = 0; j < MAX_N; j++) { for (int k = 0; k < MAX_N; k++) { prea[j][k] += dp[j][k]; if (k - orig[i] - 1 >= 0) { prea[j][k - orig[i] - 1] -= dp[j][k]; } int lft = sfx - k; if (lft > 0) { if (j + 1 < MAX_N && k + 1 < MAX_N) { preb[j + 1][k + 1] += dp[j][k]; } if (j + lft + 1 < MAX_N && k + lft + 1 < MAX_N) { preb[j + lft + 1][k + lft + 1] -= dp[j][k]; } } } } // prefixize prea by k in reverse for (int j = 0; j < MAX_N; j++) { for (int k = MAX_N - 2; k >= 0; k--) { prea[j][k] += prea[j][k + 1]; } } // prefixize preb by the diagonals for (int j = 1; j < MAX_N; j++) { for (int k = 1; k < MAX_N; k++) { preb[j][k] += preb[j - 1][k - 1]; } } // update the dp for (int j = 0; j < MAX_N; j++) { for (int k = 0; k < MAX_N; k++) { dp[j][k] = prea[j][k] + preb[j][k]; } } } Modint ans(0); for (int j = 0; j <= K && j < MAX_N; j++) { ans += dp[j][0]; } cout << ans << endl; }
replace
148
149
148
149
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define whole(x) (x).begin(), (x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using P = pair<int, int>; #define debug(var) cerr << "[" << #var << "] " << var << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) const ll mod = 998244353; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; 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; } 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]; } }; mint dp[503][503][503]; int main() { string s; cin >> s; int K; cin >> K; int n = s.size(); K = min(K, n); vector<int> v; rep(i, n) { if (s[i] == '0') v.push_back(i); } int sv = v.size(); reverse(whole(v)); for (int i = 0; i < sv; i++) { for (int j = 0; j <= K; j++) { for (int k = 0; k <= j; k++) { int ini = v[i]; int now = ini + k; if (n - 1 - now < i) continue; if (now >= n) continue; // cout << i << " " << j << " " << k << endl; if (i == 0) { if (j == k) dp[i][j][k] = 1; // debug(dp[i][j][k]); continue; } int pk = 0; chmax(pk, now - v[i - 1] + 1); // debug(pk); for (int l = pk; l < k; l++) { if (j - k + l < 305) { dp[i][j][k] += dp[i - 1][j - k + l][l]; } } // debug(dp[i][j][k]); for (int l = k; l <= K; l++) { dp[i][j][k] += dp[i - 1][j][l]; } // debug(dp[i][j][k]); } } } mint ans = 0; rep(j, 301) rep(k, 301) { ans += dp[sv - 1][j][k]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define whole(x) (x).begin(), (x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using P = pair<int, int>; #define debug(var) cerr << "[" << #var << "] " << var << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) const ll mod = 998244353; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; 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; } 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]; } }; mint dp[503][503][503]; int main() { string s; cin >> s; int K; cin >> K; int n = s.size(); K = min(K, n); vector<int> v; rep(i, n) { if (s[i] == '0') v.push_back(i); } int sv = v.size(); reverse(whole(v)); if (sv == 0 || sv == n) { cout << 1 << endl; return 0; } for (int i = 0; i < sv; i++) { for (int j = 0; j <= K; j++) { for (int k = 0; k <= j; k++) { int ini = v[i]; int now = ini + k; if (n - 1 - now < i) continue; if (now >= n) continue; // cout << i << " " << j << " " << k << endl; if (i == 0) { if (j == k) dp[i][j][k] = 1; // debug(dp[i][j][k]); continue; } int pk = 0; chmax(pk, now - v[i - 1] + 1); // debug(pk); for (int l = pk; l < k; l++) { if (j - k + l < 305) { dp[i][j][k] += dp[i - 1][j - k + l][l]; } } // debug(dp[i][j][k]); for (int l = k; l <= K; l++) { dp[i][j][k] += dp[i - 1][j][l]; } // debug(dp[i][j][k]); } } } mint ans = 0; rep(j, 301) rep(k, 301) { ans += dp[sv - 1][j][k]; } cout << ans << endl; return 0; }
insert
101
101
101
105
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxN = 305, mod = 998244353; int n, m; int f[2][maxN + 1][maxN + 1]; int a[maxN + 1], cnt; char s[maxN + 1]; inline void update(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; } int main() { scanf("%s %d", s + 1, &m); n = strlen(s + 1); int l = 1; while (l <= n) { int r = l; while (r <= n && s[r] == '1') r++; a[++cnt] = r - l; l = r + 1; } int now = 0; f[now][0][0] = 1; for (int i = cnt; i >= 1; i--) { now ^= 1; memset(f[now], 0, sizeof(f[now])); for (int j = 0; j <= m; j++) for (int k = 0; k <= j; k++) { if (!f[now ^ 1][j][k]) continue; int res = f[now ^ 1][j][k]; for (int t = 0; t <= a[i]; t++) update(f[now][j + t][k + t], res); for (int t = 1; t <= k; t++) update(f[now][j][k - t], res); } } int ans = 0; for (int i = 0; i <= m; i++) update(ans, f[now][i][0]); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxN = 305, mod = 998244353; int n, m; int f[2][maxN + 1][maxN + 1]; int a[maxN + 1], cnt; char s[maxN + 1]; inline void update(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; } int main() { scanf("%s %d", s + 1, &m); n = strlen(s + 1); m = min(m, n); int l = 1; while (l <= n) { int r = l; while (r <= n && s[r] == '1') r++; a[++cnt] = r - l; l = r + 1; } int now = 0; f[now][0][0] = 1; for (int i = cnt; i >= 1; i--) { now ^= 1; memset(f[now], 0, sizeof(f[now])); for (int j = 0; j <= m; j++) for (int k = 0; k <= j; k++) { if (!f[now ^ 1][j][k]) continue; int res = f[now ^ 1][j][k]; for (int t = 0; t <= a[i]; t++) update(f[now][j + t][k + t], res); for (int t = 1; t <= k; t++) update(f[now][j][k - t], res); } } int ans = 0; for (int i = 0; i <= m; i++) update(ans, f[now][i][0]); printf("%d\n", ans); return 0; }
insert
15
15
15
16
0
p02635
C++
Runtime Error
// header #ifdef LOCAL #include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp" #define debug(x) cout << x << endl #else #define debug(...) 42 #endif #pragma GCC optimize("Ofast") #include <bits/stdc++.h> // types using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; typedef pair<ll, ll> Pl; typedef pair<int, int> Pi; typedef vector<ll> vl; typedef vector<int> vi; typedef vector<char> vc; template <typename T> using mat = vector<vector<T>>; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvl; typedef vector<vector<char>> vvc; 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; } }; // abreviations #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep_(i, a_, b_, a, b, ...) \ for (int i = (a), max_i = (b); i < max_i; i++) #define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define rrep_(i, a_, b_, a, b, ...) \ for (int i = (b - 1), min_i = (a); i >= min_i; i--) #define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define SZ(x) ((int)(x).size()) #define pb(x) push_back(x) #define eb(x) emplace_back(x) #define mp make_pair #define print(x) cout << x << endl #define vprint(x) \ rep(i, x.xize()) cout << x[i] << ' '; \ cout << endl #define vsum(x) accumulate(all(x), 0LL) #define vmax(a) *max_element(all(a)) #define vmin(a) *min_element(all(a)) #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) // functions // gcd(0, x) fails. ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T> T mypow(T x, ll n) { T ret = 1; while (n > 0) { if (n & 1) (ret *= x); (x *= x); n >>= 1; } return ret; } ll modpow(ll x, ll n, const ll mod) { ll ret = 1; while (n > 0) { if (n & 1) (ret *= x); (x *= x); n >>= 1; x %= mod; ret %= mod; } return ret; } uint64_t my_rand(void) { static uint64_t x = 88172645463325252ULL; x = x ^ (x << 13); x = x ^ (x >> 7); return x = x ^ (x << 17); } int popcnt(ull x) { return __builtin_popcountll(x); } // graph template template <typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; struct Timer { clock_t start_time; void start() { start_time = clock(); } int lap() { // return x ms. return (clock() - start_time) * 1000 / CLOCKS_PER_SEC; } }; // constant // #define inf 1000000005LL #define inf 4000000005LL #define INF 4000000004000000000LL #define mod 998244353LL #define endl '\n' typedef modint<mod> mint; const long double eps = 0.000001; const long double PI = 3.141592653589793; // library int main() { cin.tie(0); ios::sync_with_stdio(0); cout << setprecision(20); string x; int k; cin >> x >> k; int n = x.size(); vector<char> s(n); rep(i, n) s[i] = x[n - 1 - i]; // int m = min(k, n); // iばんめ, j個あまり, p回使った; vector<mat<mint>> dp(n + 5, mat<mint>(n + 5, vector<mint>(n + 5))); vi id; int cnt = 0; vi ones; rep(i, n) { if (s[i] == '0') { ones.pb(cnt); cnt = 0; } else { cnt++; } } ones.pb(cnt); int l = ones.size(); rep(i, ones[0] + 1) { dp[0][ones[0] - i][0] = 1; } rep(i, 1, l) { rep(p, n + 1) { //[s-ones[i], s] mint sm = 0; rep(s, n + 1) { sm += dp[i - 1][s][p]; dp[i][s][p] += sm; if (s >= ones[i]) { sm -= dp[i - 1][s - ones[i]][p]; //? } } } vector<mint> sm(n * 2 + 2); rep(xx, n * 2 + 2) { rep(yy, xx + 1) { if (xx > n || (xx - yy) > n) continue; sm[xx] += dp[i - 1][yy][xx - yy]; } } rrep(p, n + 1) { rrep(s, n + 1) { dp[i][s][p] += sm[s + p] - dp[i - 1][s][p]; sm[s + p] -= dp[i - 1][s][p]; } } } print( accumulate(dp[l - 1][0].begin(), dp[l - 1][0].begin() + k + 1, (mint)0)); // debug(dp[3]); }
// header #ifdef LOCAL #include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp" #define debug(x) cout << x << endl #else #define debug(...) 42 #endif #pragma GCC optimize("Ofast") #include <bits/stdc++.h> // types using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; typedef pair<ll, ll> Pl; typedef pair<int, int> Pi; typedef vector<ll> vl; typedef vector<int> vi; typedef vector<char> vc; template <typename T> using mat = vector<vector<T>>; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvl; typedef vector<vector<char>> vvc; 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; } }; // abreviations #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep_(i, a_, b_, a, b, ...) \ for (int i = (a), max_i = (b); i < max_i; i++) #define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define rrep_(i, a_, b_, a, b, ...) \ for (int i = (b - 1), min_i = (a); i >= min_i; i--) #define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define SZ(x) ((int)(x).size()) #define pb(x) push_back(x) #define eb(x) emplace_back(x) #define mp make_pair #define print(x) cout << x << endl #define vprint(x) \ rep(i, x.xize()) cout << x[i] << ' '; \ cout << endl #define vsum(x) accumulate(all(x), 0LL) #define vmax(a) *max_element(all(a)) #define vmin(a) *min_element(all(a)) #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) // functions // gcd(0, x) fails. ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T> T mypow(T x, ll n) { T ret = 1; while (n > 0) { if (n & 1) (ret *= x); (x *= x); n >>= 1; } return ret; } ll modpow(ll x, ll n, const ll mod) { ll ret = 1; while (n > 0) { if (n & 1) (ret *= x); (x *= x); n >>= 1; x %= mod; ret %= mod; } return ret; } uint64_t my_rand(void) { static uint64_t x = 88172645463325252ULL; x = x ^ (x << 13); x = x ^ (x >> 7); return x = x ^ (x << 17); } int popcnt(ull x) { return __builtin_popcountll(x); } // graph template template <typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; struct Timer { clock_t start_time; void start() { start_time = clock(); } int lap() { // return x ms. return (clock() - start_time) * 1000 / CLOCKS_PER_SEC; } }; // constant // #define inf 1000000005LL #define inf 4000000005LL #define INF 4000000004000000000LL #define mod 998244353LL #define endl '\n' typedef modint<mod> mint; const long double eps = 0.000001; const long double PI = 3.141592653589793; // library int main() { cin.tie(0); ios::sync_with_stdio(0); cout << setprecision(20); string x; int k; cin >> x >> k; int n = x.size(); vector<char> s(n); rep(i, n) s[i] = x[n - 1 - i]; // int m = min(k, n); // iばんめ, j個あまり, p回使った; vector<mat<mint>> dp(n + 5, mat<mint>(n + 5, vector<mint>(n + 5))); vi id; int cnt = 0; vi ones; rep(i, n) { if (s[i] == '0') { ones.pb(cnt); cnt = 0; } else { cnt++; } } ones.pb(cnt); int l = ones.size(); rep(i, ones[0] + 1) { dp[0][ones[0] - i][0] = 1; } rep(i, 1, l) { rep(p, n + 1) { //[s-ones[i], s] mint sm = 0; rep(s, n + 1) { sm += dp[i - 1][s][p]; dp[i][s][p] += sm; if (s >= ones[i]) { sm -= dp[i - 1][s - ones[i]][p]; //? } } } vector<mint> sm(n * 2 + 2); rep(xx, n * 2 + 2) { rep(yy, xx + 1) { if (xx > n || (xx - yy) > n) continue; sm[xx] += dp[i - 1][yy][xx - yy]; } } rrep(p, n + 1) { rrep(s, n + 1) { dp[i][s][p] += sm[s + p] - dp[i - 1][s][p]; sm[s + p] -= dp[i - 1][s][p]; } } } print(accumulate(dp[l - 1][0].begin(), dp[l - 1][0].begin() + min(n, k) + 1, (mint)0)); // debug(dp[3]); }
replace
258
260
258
260
0
p02635
C++
Time Limit Exceeded
#include <algorithm> #include <cstdint> #include <cstdlib> #include <iostream> #include <string> #include <vector> template <typename T> void fin(T const &t) { std::cout << t << std::endl; exit(0); } template <typename T> std::vector<std::vector<T>> mk2d(size_t s1, size_t s2, T const &val) { std::vector<T> tmp(s2, val); return std::vector<std::vector<T>>(s1, tmp); } template <typename T> std::vector<T> mk1d(size_t s, T const &val) { return std::vector<T>(s, val); } template <int64_t Prime> struct mod_int { int64_t v_; mod_int(int64_t x = 0) : v_(x) { normalize(); } void normalize() { ((v_ %= Prime) += Prime) %= Prime; } mod_int operator+=(mod_int const &r) { (v_ += r.v_) %= Prime; return *this; } mod_int operator-=(mod_int const &r) { (v_ += Prime - r.v_) %= Prime; return *this; } mod_int operator*=(mod_int const &r) { (v_ *= r.v_) %= Prime; return *this; } mod_int operator+(mod_int const &r) const { mod_int res(*this); return res += r; } mod_int operator-(mod_int const &r) const { mod_int res(*this); return res -= r; } mod_int operator*(mod_int const &r) const { mod_int res(*this); return res *= r; } mod_int pow(int x) const { int64_t res = 1, v = v_; while (x > 0) { if (x & 1) (res *= v) %= Prime; x /= 2; (v *= v) %= Prime; } return mod_int(res); } mod_int inv() const { return pow(Prime - 2); } }; int64_t const MOD = 998244353; typedef mod_int<MOD> mint; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); std::string S; int K; std::cin >> S >> K; std::vector<int> a; int sum_one = 0, k = 0; for (auto it = S.rbegin(); it != S.rend(); ++it) { if (*it == '1') ++sum_one; else if (sum_one > 0) { a.push_back(sum_one); k += sum_one; } } if (K > k) K = k; auto tmp = mk2d(K + 1, sum_one + 1, mint(0)); auto dp = mk1d(a.size() + 1, tmp); dp[0][0][0] = 1; for (int i = 0; i < a.size(); ++i) { std::vector<std::vector<mint>> &cur = dp[i + 1]; std::vector<std::vector<mint>> &pre = dp[i]; int ai1 = 0; if (i > 0) ai1 = a[i - 1]; for (int l = 0; l <= ai1; ++l) { int p = ai1 - l; for (int k = 0; k <= K; ++k) { int ma = std::min(K - k + p, a[i] - l); for (int j = 0; j <= std::min(p, ma); ++j) cur[k][a[i] - j] += pre[k][l]; for (int j = p + 1; j <= ma; ++j) cur[k + j - p][a[i] - j] += pre[k][l]; } } } mint ans; for (int k = 0; k <= K; ++k) for (int l = 0; l <= sum_one; ++l) ans += dp[a.size()][k][l]; fin(ans.v_); return 0; }
#include <algorithm> #include <cstdint> #include <cstdlib> #include <iostream> #include <string> #include <vector> template <typename T> void fin(T const &t) { std::cout << t << std::endl; exit(0); } template <typename T> std::vector<std::vector<T>> mk2d(size_t s1, size_t s2, T const &val) { std::vector<T> tmp(s2, val); return std::vector<std::vector<T>>(s1, tmp); } template <typename T> std::vector<T> mk1d(size_t s, T const &val) { return std::vector<T>(s, val); } template <int64_t Prime> struct mod_int { int64_t v_; mod_int(int64_t x = 0) : v_(x) { normalize(); } void normalize() { ((v_ %= Prime) += Prime) %= Prime; } mod_int operator+=(mod_int const &r) { (v_ += r.v_) %= Prime; return *this; } mod_int operator-=(mod_int const &r) { (v_ += Prime - r.v_) %= Prime; return *this; } mod_int operator*=(mod_int const &r) { (v_ *= r.v_) %= Prime; return *this; } mod_int operator+(mod_int const &r) const { mod_int res(*this); return res += r; } mod_int operator-(mod_int const &r) const { mod_int res(*this); return res -= r; } mod_int operator*(mod_int const &r) const { mod_int res(*this); return res *= r; } mod_int pow(int x) const { int64_t res = 1, v = v_; while (x > 0) { if (x & 1) (res *= v) %= Prime; x /= 2; (v *= v) %= Prime; } return mod_int(res); } mod_int inv() const { return pow(Prime - 2); } }; int64_t const MOD = 998244353; typedef mod_int<MOD> mint; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); std::string S; int K; std::cin >> S >> K; std::vector<int> a; int sum_one = 0, k = 0; for (auto it = S.rbegin(); it != S.rend(); ++it) { if (*it == '1') ++sum_one; else if (sum_one > 0) { a.push_back(sum_one); k += sum_one; } } if (K > sum_one * 2) K = sum_one * 2; auto tmp = mk2d(K + 1, sum_one + 1, mint(0)); auto dp = mk1d(a.size() + 1, tmp); dp[0][0][0] = 1; for (int i = 0; i < a.size(); ++i) { std::vector<std::vector<mint>> &cur = dp[i + 1]; std::vector<std::vector<mint>> &pre = dp[i]; int ai1 = 0; if (i > 0) ai1 = a[i - 1]; for (int l = 0; l <= ai1; ++l) { int p = ai1 - l; for (int k = 0; k <= K; ++k) { int ma = std::min(K - k + p, a[i] - l); for (int j = 0; j <= std::min(p, ma); ++j) cur[k][a[i] - j] += pre[k][l]; for (int j = p + 1; j <= ma; ++j) cur[k + j - p][a[i] - j] += pre[k][l]; } } } mint ans; for (int k = 0; k <= K; ++k) for (int l = 0; l <= sum_one; ++l) ans += dp[a.size()][k][l]; fin(ans.v_); return 0; }
replace
81
83
81
83
TLE
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define fi first #define se second typedef vector<int> VI; typedef long long ll; typedef pair<int, int> PII; const ll mod = 998244353; ll powmod(ll a, ll b) { ll res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // head const int N = 310; char s[N]; int m, ss[N], p[N]; int dp[N][N][N], ans; void upd(int &a, int b) { a += b; if (a >= mod) a -= mod; } int main() { scanf("%s%d", s, &m); int n = strlen(s); reverse(s, s + n); m = min(m, n); int p0 = 0; rep(i, 0, n) { if (s[i] == '0') ss[p0 + 1] = ss[p0], p0++; else p[p0]++, ss[p0]++; } dp[0][0][0] = 1; rep(i, 1, p0 + 2) rep(k, 0, m + 1) { int t = 0; rep(j, 0, ss[i - 1] + 1) { for (int z = 0; z < p[i - 1]; z++) upd(dp[i][j][k + p[i - 1] - z], dp[i - 1][j - z][k]); if (j - p[i - 1] >= 0) upd(t, dp[i - 1][j - p[i - 1]][k]); upd(dp[i][j][k], t); } } rep(i, 0, m + 1) upd(ans, dp[p0 + 1][ss[p0]][i]); printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define fi first #define se second typedef vector<int> VI; typedef long long ll; typedef pair<int, int> PII; const ll mod = 998244353; ll powmod(ll a, ll b) { ll res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // head const int N = 310; char s[N]; int m, ss[N], p[N]; int dp[N][N][N], ans; void upd(int &a, int b) { a += b; if (a >= mod) a -= mod; } int main() { scanf("%s%d", s, &m); int n = strlen(s); reverse(s, s + n); m = min(m, n); int p0 = 0; rep(i, 0, n) { if (s[i] == '0') ss[p0 + 1] = ss[p0], p0++; else p[p0]++, ss[p0]++; } dp[0][0][0] = 1; rep(i, 1, p0 + 2) rep(k, 0, m + 1) { int t = 0; rep(j, 0, ss[i - 1] + 1) { for (int z = 0; z < p[i - 1] && z <= j; z++) upd(dp[i][j][k + p[i - 1] - z], dp[i - 1][j - z][k]); if (j - p[i - 1] >= 0) upd(t, dp[i - 1][j - p[i - 1]][k]); upd(dp[i][j][k], t); } } rep(i, 0, m + 1) upd(ans, dp[p0 + 1][ss[p0]][i]); printf("%d\n", ans); }
replace
54
55
54
55
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define ms(s, n) memset(s, n, sizeof(s)) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORd(i, a, b) for (int i = (a)-1; i >= (b); --i) #define FORall(it, a) \ for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define sz(a) int((a).size()) #define present(t, x) (t.find(x) != t.end()) #define all(a) (a).begin(), (a).end() #define uni(a) (a).erase(unique(all(a)), (a).end()) #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define prec(n) fixed << setprecision(n) #define bit(n, i) (((n) >> (i)) & 1) #define bitcount(n) __builtin_popcountll(n) typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pi; typedef vector<int> vi; typedef vector<pi> vii; // const int MOD = (int) 1e9 + 7; const int MOD = 119 << 23 | 1; const int FFTMOD = 119 << 23 | 1; const int INF = (int)1e9 + 23111992; const ll LINF = (ll)1e18 + 23111992; const ld PI = acos((ld)-1); const ld EPS = 1e-9; 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 ull isqrt(ull k) { ull r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline ll icbrt(ll k) { ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } 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); } inline int sign(ld x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(ld x, ld y) { return sign(x - y); } mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int mrand() { return abs((int)mt()); } inline int mrand(int k) { return abs((int)mt()) % k; } #define db(x) cerr << "[" << #x << ": " << (x) << "] "; #define endln cerr << "\n"; void chemthan() { string s; cin >> s; int k; cin >> k; int n = sz(s); int cnt = 0; vi vals; FOR(i, 0, n) { if (s[i] == '0') { vals.pb(cnt); cnt = 0; } else { cnt++; } } vals.pb(cnt); auto a = vals; FOR(i, 1, sz(vals)) vals[i] += vals[i - 1]; int d = sz(vals); vector<vector<vi>> dp(d + 1, vector<vi>(n + 1, vi(k + k + 1))); dp[0][0][0] = 1; FOR(i, 0, d) { FOR(s, 0, n + 1) FOR(t, 0, k + k + 1) if (dp[i][s][t]) { FOR(l, max(0, vals[i] - s), n + 1) { if (n < s + l) break; if (k + k < t + abs(l - a[i])) break; addmod(dp[i + 1][s + l][t + abs(l - a[i])], dp[i][s][t]); } } } int res = 0; FOR(i, 0, k + k + 1) { addmod(res, dp[d][vals.back()][i]); } cout << res << "\n"; } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0), cin.tie(0); if (argc > 1) { assert(freopen(argv[1], "r", stdin)); } if (argc > 2) { assert(freopen(argv[2], "wb", stdout)); } chemthan(); cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> using namespace std; // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define ms(s, n) memset(s, n, sizeof(s)) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORd(i, a, b) for (int i = (a)-1; i >= (b); --i) #define FORall(it, a) \ for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define sz(a) int((a).size()) #define present(t, x) (t.find(x) != t.end()) #define all(a) (a).begin(), (a).end() #define uni(a) (a).erase(unique(all(a)), (a).end()) #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define prec(n) fixed << setprecision(n) #define bit(n, i) (((n) >> (i)) & 1) #define bitcount(n) __builtin_popcountll(n) typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pi; typedef vector<int> vi; typedef vector<pi> vii; // const int MOD = (int) 1e9 + 7; const int MOD = 119 << 23 | 1; const int FFTMOD = 119 << 23 | 1; const int INF = (int)1e9 + 23111992; const ll LINF = (ll)1e18 + 23111992; const ld PI = acos((ld)-1); const ld EPS = 1e-9; 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 ull isqrt(ull k) { ull r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline ll icbrt(ll k) { ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } 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); } inline int sign(ld x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(ld x, ld y) { return sign(x - y); } mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int mrand() { return abs((int)mt()); } inline int mrand(int k) { return abs((int)mt()) % k; } #define db(x) cerr << "[" << #x << ": " << (x) << "] "; #define endln cerr << "\n"; void chemthan() { string s; cin >> s; int k; cin >> k; int n = sz(s); chkmin(k, n + 5); int cnt = 0; vi vals; FOR(i, 0, n) { if (s[i] == '0') { vals.pb(cnt); cnt = 0; } else { cnt++; } } vals.pb(cnt); auto a = vals; FOR(i, 1, sz(vals)) vals[i] += vals[i - 1]; int d = sz(vals); vector<vector<vi>> dp(d + 1, vector<vi>(n + 1, vi(k + k + 1))); dp[0][0][0] = 1; FOR(i, 0, d) { FOR(s, 0, n + 1) FOR(t, 0, k + k + 1) if (dp[i][s][t]) { FOR(l, max(0, vals[i] - s), n + 1) { if (n < s + l) break; if (k + k < t + abs(l - a[i])) break; addmod(dp[i + 1][s + l][t + abs(l - a[i])], dp[i][s][t]); } } } int res = 0; FOR(i, 0, k + k + 1) { addmod(res, dp[d][vals.back()][i]); } cout << res << "\n"; } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0), cin.tie(0); if (argc > 1) { assert(freopen(argv[1], "r", stdin)); } if (argc > 2) { assert(freopen(argv[2], "wb", stdout)); } chemthan(); cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
insert
96
96
96
97
0
Time elapsed: 24ms
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define len(x) int((x).size()) ll dp_real[2][301][301]; ll dp_suff[2][301][301]; ll dp_diag[2][301][301]; const ll p = 998244353LL; int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef ARTHUR_LOCAL ifstream cin("input.txt"); #endif string s; int k; cin >> s >> k; int n = len(s); int zi = 0; int initial_ones = 0; int ind = n - 1; while (ind >= 0 && s[ind] == '1') { ind--; initial_ones++; } // initialise the dp for (int i = 0; i <= min(k, initial_ones); i++) { dp_real[0][i][i] = 1; // cout << 0 << " " << i << " " << i << " " << 1 << endl; } // diag dudes is pretty easy to do for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) { dp_diag[0][i][j] = dp_real[0][i][j]; if (i > 0 && j > 0) { dp_diag[0][i][j] += dp_diag[0][i - 1][j - 1]; dp_diag[0][i][j] %= p; } } } // suff dudes ... not so bad either ??? for (int i = 0; i <= k; i++) { for (int j = k; j >= 0; j--) { dp_suff[0][i][j] = dp_real[0][i][j]; if (j < k) { dp_suff[0][i][j] += dp_suff[0][i][j + 1]; dp_suff[0][i][j] %= p; } } } for (int ii = n - 1; ii >= 0; ii--) { if (s[ii] == '1') continue; // if(!skip_first) // { // skip_first=1; // continue; // } zi = 1 - zi; int a = 0; int copy_i = ii - 1; while (copy_i >= 0 && s[copy_i] == '1') { a++; copy_i--; } // cout << a << endl; for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) { // compute new real_dp[zi][i][j] value dp_real[zi][i][j] = dp_suff[1 - zi][i][j]; dp_real[zi][i][j] += dp_diag[1 - zi][i][j]; dp_real[zi][i][j] %= p; if (i - a > 0 && j - a > 0) // maybe add conditions on j < i or whatever { dp_real[zi][i][j] += p - dp_diag[1 - zi][i - a - 1][j - a - 1]; dp_real[zi][i][j] %= p; } dp_real[zi][i][j] += p - dp_real[1 - zi][i][j]; dp_real[zi][i][j] %= p; // cout << ii << " " << i << " " << j << " " << dp_real[zi][i][j] << // endl; dp_diag[zi][i][j] = dp_real[zi][i][j]; if (i > 0 && j > 0) { dp_diag[zi][i][j] += dp_diag[zi][i - 1][j - 1]; dp_diag[zi][i][j] %= p; } } dp_suff[zi][i][k] = dp_real[zi][i][k]; for (int j = k - 1; j >= 0; j--) { dp_suff[zi][i][j] = dp_real[zi][i][j]; dp_suff[zi][i][j] += dp_suff[zi][i][j + 1]; dp_suff[zi][i][j] %= p; } } } ll ans = 0LL; for (int i = 0; i <= k; i++) { ans += dp_real[zi][i][0]; ans %= p; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define len(x) int((x).size()) ll dp_real[2][301][301]; ll dp_suff[2][301][301]; ll dp_diag[2][301][301]; const ll p = 998244353LL; int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef ARTHUR_LOCAL ifstream cin("input.txt"); #endif string s; int k; cin >> s >> k; k = min(k, 300); int n = len(s); int zi = 0; int initial_ones = 0; int ind = n - 1; while (ind >= 0 && s[ind] == '1') { ind--; initial_ones++; } // initialise the dp for (int i = 0; i <= min(k, initial_ones); i++) { dp_real[0][i][i] = 1; // cout << 0 << " " << i << " " << i << " " << 1 << endl; } // diag dudes is pretty easy to do for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) { dp_diag[0][i][j] = dp_real[0][i][j]; if (i > 0 && j > 0) { dp_diag[0][i][j] += dp_diag[0][i - 1][j - 1]; dp_diag[0][i][j] %= p; } } } // suff dudes ... not so bad either ??? for (int i = 0; i <= k; i++) { for (int j = k; j >= 0; j--) { dp_suff[0][i][j] = dp_real[0][i][j]; if (j < k) { dp_suff[0][i][j] += dp_suff[0][i][j + 1]; dp_suff[0][i][j] %= p; } } } for (int ii = n - 1; ii >= 0; ii--) { if (s[ii] == '1') continue; // if(!skip_first) // { // skip_first=1; // continue; // } zi = 1 - zi; int a = 0; int copy_i = ii - 1; while (copy_i >= 0 && s[copy_i] == '1') { a++; copy_i--; } // cout << a << endl; for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) { // compute new real_dp[zi][i][j] value dp_real[zi][i][j] = dp_suff[1 - zi][i][j]; dp_real[zi][i][j] += dp_diag[1 - zi][i][j]; dp_real[zi][i][j] %= p; if (i - a > 0 && j - a > 0) // maybe add conditions on j < i or whatever { dp_real[zi][i][j] += p - dp_diag[1 - zi][i - a - 1][j - a - 1]; dp_real[zi][i][j] %= p; } dp_real[zi][i][j] += p - dp_real[1 - zi][i][j]; dp_real[zi][i][j] %= p; // cout << ii << " " << i << " " << j << " " << dp_real[zi][i][j] << // endl; dp_diag[zi][i][j] = dp_real[zi][i][j]; if (i > 0 && j > 0) { dp_diag[zi][i][j] += dp_diag[zi][i - 1][j - 1]; dp_diag[zi][i][j] %= p; } } dp_suff[zi][i][k] = dp_real[zi][i][k]; for (int j = k - 1; j >= 0; j--) { dp_suff[zi][i][j] = dp_real[zi][i][j]; dp_suff[zi][i][j] += dp_suff[zi][i][j + 1]; dp_suff[zi][i][j] %= p; } } } ll ans = 0LL; for (int i = 0; i <= k; i++) { ans += dp_real[zi][i][0]; ans %= p; } cout << ans << endl; }
insert
28
28
28
30
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define ull unsigned ll #define f first #define s second #define ALL(x) x.begin(), x.end() #define SZ(x) (int)x.size() #define SQ(x) (x) * (x) #define MN(a, b) a = min(a, (__typeof__(a))(b)) #define MX(a, b) a = max(a, (__typeof__(a))(b)) #define pb push_back #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #ifdef BALBIT #define IOS() #define bug(...) \ fprintf(stderr, "#%d (%s) = ", __LINE__, #__VA_ARGS__), _do(__VA_ARGS__); template <typename T> void _do(T &&x) { cerr << x << endl; } template <typename T, typename... S> void _do(T &&x, S &&...y) { cerr << x << ", "; _do(y...); } #else #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0); #define endl '\n' #define bug(...) #endif const int iinf = 1e9 + 10; const ll inf = 1ll << 60; const ll mod = 998244353; void GG() { cout << "0\n"; exit(0); } ll mpow(ll a, ll n, ll mo = mod) { // a^n % mod ll re = 1; while (n > 0) { if (n & 1) re = re * a % mo; a = a * a % mo; n >>= 1; } return re; } ll inv(ll b, ll mo = mod) { if (b == 1) return b; return (mo - mo / b) * inv(mo % b, mo) % mo; } const int maxn = 3e2 + 5; int a[maxn]; ll dp[2][305][305]; signed main() { IOS(); string s; cin >> s; int n0 = 0, n = SZ(s); int k; cin >> k; int n1 = 0; int now = 0; int N = 0; for (int i = 0; i < n; ++i) { (s[i] == '0' ? n0 : n1)++; if (s[i] == '0') { a[N++] = now; now = 0; } else now++; } if (now) a[N++] = now; bug(N); for (int i = 0; i < N; ++i) bug(i, a[i]); bool dpat = 0; dp[dpat][0][0] = 1; int befsum1 = 0; for (int i = N - 1; i >= 0; --i) { dpat ^= 1; memset(dp[dpat], 0, sizeof dp[dpat]); for (int chg = 0; chg <= min(n1, k); ++chg) { for (int def = 0; def <= chg; ++def) { for (int j = 0; j <= a[i]; ++j) { if (def - j >= 0) dp[dpat][def][chg] += dp[dpat ^ 1][def - j][chg - j]; else break; } for (int j = 1; j <= befsum1; ++j) { dp[dpat][def][chg] += dp[dpat ^ 1][def + j][chg]; } dp[dpat][def][chg] %= mod; } } befsum1 += a[i]; } ll sig = 0; for (int i = 0; i <= min(k, n1); ++i) { sig += dp[dpat][0][i]; bug(i, dp[dpat][0][i]); } cout << sig % mod << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define ull unsigned ll #define f first #define s second #define ALL(x) x.begin(), x.end() #define SZ(x) (int)x.size() #define SQ(x) (x) * (x) #define MN(a, b) a = min(a, (__typeof__(a))(b)) #define MX(a, b) a = max(a, (__typeof__(a))(b)) #define pb push_back #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #ifdef BALBIT #define IOS() #define bug(...) \ fprintf(stderr, "#%d (%s) = ", __LINE__, #__VA_ARGS__), _do(__VA_ARGS__); template <typename T> void _do(T &&x) { cerr << x << endl; } template <typename T, typename... S> void _do(T &&x, S &&...y) { cerr << x << ", "; _do(y...); } #else #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0); #define endl '\n' #define bug(...) #endif const int iinf = 1e9 + 10; const ll inf = 1ll << 60; const ll mod = 998244353; void GG() { cout << "0\n"; exit(0); } ll mpow(ll a, ll n, ll mo = mod) { // a^n % mod ll re = 1; while (n > 0) { if (n & 1) re = re * a % mo; a = a * a % mo; n >>= 1; } return re; } ll inv(ll b, ll mo = mod) { if (b == 1) return b; return (mo - mo / b) * inv(mo % b, mo) % mo; } const int maxn = 3e2 + 5; int a[maxn]; ll dp[2][305][305]; signed main() { IOS(); string s; cin >> s; int n0 = 0, n = SZ(s); int k; cin >> k; int n1 = 0; int now = 0; int N = 0; for (int i = 0; i < n; ++i) { (s[i] == '0' ? n0 : n1)++; if (s[i] == '0') { a[N++] = now; now = 0; } else now++; } if (now) a[N++] = now; bug(N); for (int i = 0; i < N; ++i) bug(i, a[i]); bool dpat = 0; dp[dpat][0][0] = 1; int befsum1 = 0; for (int i = N - 1; i >= 0; --i) { dpat ^= 1; memset(dp[dpat], 0, sizeof dp[dpat]); for (int chg = 0; chg <= min(n1, k); ++chg) { for (int def = 0; def <= chg; ++def) { for (int j = 0; j <= a[i]; ++j) { if (def - j >= 0) dp[dpat][def][chg] += dp[dpat ^ 1][def - j][chg - j]; else break; } for (int j = 1; j + def <= befsum1; ++j) { dp[dpat][def][chg] += dp[dpat ^ 1][def + j][chg]; } dp[dpat][def][chg] %= mod; } } befsum1 += a[i]; } ll sig = 0; for (int i = 0; i <= min(k, n1); ++i) { sig += dp[dpat][0][i]; bug(i, dp[dpat][0][i]); } cout << sig % mod << endl; }
replace
101
102
101
102
0
p02635
C++
Runtime Error
#pragma GCC target("avx2") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("O3") #include "bits/stdc++.h" #include <random> #include <unordered_map> #include <unordered_set> using namespace std; typedef long long ll; const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL; #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; string S; int K; vector<int> v, sum; ll dp[305][305][305], memo1[305][305][305], memo2[305][305][305]; ll solve(int, int, int); ll calc1(int n, int k, int d) { if (k < 0 || d < 0) return 0; if (memo1[n][k][d] != -1) return memo1[n][k][d]; return memo1[n][k][d] = (calc1(n, k - 1, d - 1) + solve(n, k, d)) % MOD; } ll calc2(int n, int k, int d) { if (k < 0 || d < 0) return 0; if (memo2[n][k][d] != -1) return memo2[n][k][d]; return memo2[n][k][d] = (calc2(n, k, d - 1) + solve(n, k, d)) % MOD; } ll solve(int n, int k, int d) { if (k < 0) return 0; if (n == 0) { return 1; } if (dp[n][k][d] != -1) return dp[n][k][d]; ll ret = 0; /*for(int i=0; i<=v[n]; i++){ ret += solve(n-1, k-v[n]+i, d+i); }*/ ret += calc1(n - 1, k, d + v[n]) - calc1(n - 1, k - v[n] - 1, d - 1) + MOD; /*for(int i=v[n]+1; i<=sum[n]-d; i++){ ret += solve(n-1, k, d+i); }*/ ret += calc2(n - 1, k, sum[n]) - calc2(n - 1, k, d + v[n]) + MOD; return dp[n][k][d] = ret % MOD; } signed main() { cin >> S >> K; K = min(K, 302); int cnt = 0; rep(i, S.size()) { if (S[i] == '0') { v.pb(cnt); cnt = 0; } else cnt++; } sum.resize(v.size()); sum.back() = v.back() + cnt; for (int i = v.size() - 2; i >= 0; i--) { sum[i] = sum[i + 1] + v[i]; } memset(dp, -1, sizeof(dp)); memset(memo1, -1, sizeof(memo1)); memset(memo2, -1, sizeof(memo2)); ll ans = 0; for (int i = 0; i <= cnt; i++) { ans += solve(v.size() - 1, K - (cnt - i), i); } cout << ans % MOD << endl; }
#pragma GCC target("avx2") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("O3") #include "bits/stdc++.h" #include <random> #include <unordered_map> #include <unordered_set> using namespace std; typedef long long ll; const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL; #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; string S; int K; vector<int> v, sum; ll dp[305][305][305], memo1[305][305][305], memo2[305][305][305]; ll solve(int, int, int); ll calc1(int n, int k, int d) { if (k < 0 || d < 0) return 0; if (memo1[n][k][d] != -1) return memo1[n][k][d]; return memo1[n][k][d] = (calc1(n, k - 1, d - 1) + solve(n, k, d)) % MOD; } ll calc2(int n, int k, int d) { if (k < 0 || d < 0) return 0; if (memo2[n][k][d] != -1) return memo2[n][k][d]; return memo2[n][k][d] = (calc2(n, k, d - 1) + solve(n, k, d)) % MOD; } ll solve(int n, int k, int d) { if (k < 0) return 0; if (n == 0) { return 1; } if (dp[n][k][d] != -1) return dp[n][k][d]; ll ret = 0; /*for(int i=0; i<=v[n]; i++){ ret += solve(n-1, k-v[n]+i, d+i); }*/ ret += calc1(n - 1, k, d + v[n]) - calc1(n - 1, k - v[n] - 1, d - 1) + MOD; /*for(int i=v[n]+1; i<=sum[n]-d; i++){ ret += solve(n-1, k, d+i); }*/ ret += calc2(n - 1, k, sum[n]) - calc2(n - 1, k, d + v[n]) + MOD; return dp[n][k][d] = ret % MOD; } signed main() { cin >> S >> K; K = min(K, 302); int cnt = 0; rep(i, S.size()) { if (S[i] == '0') { v.pb(cnt); cnt = 0; } else cnt++; } if (v.size() == 0) { cout << 1 << endl; return 0; } sum.resize(v.size()); sum.back() = v.back() + cnt; for (int i = v.size() - 2; i >= 0; i--) { sum[i] = sum[i + 1] + v[i]; } memset(dp, -1, sizeof(dp)); memset(memo1, -1, sizeof(memo1)); memset(memo2, -1, sizeof(memo2)); ll ans = 0; for (int i = 0; i <= cnt; i++) { ans += solve(v.size() - 1, K - (cnt - i), i); } cout << ans % MOD << endl; }
insert
72
72
72
76
-11
p02635
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define int long long using namespace std; const int N = 3e2 + 5; int n, a[N], dp[N][N][N], pref[N]; const int M = 998244353; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string S; int k; cin >> S >> k; int cur = 0; for (int i = 0; i < S.size(); i++) if (S[i] == '1') cur++; else n++, a[n] = cur, cur = 0; n++; a[n] = cur; for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i]; dp[0][0][0] = 1; for (int i = 1; i <= n; i++) for (int s = pref[i]; s <= pref[n]; s++) for (int j = 0; j <= k; j++) { for (int q = max(1ll, a[i] - s); q <= a[i]; q++) // if (s >= a[i] - q) dp[i][s][j] += dp[i - 1][s - (a[i] - q)][j], dp[i][s][j] %= M; for (int q = 0; q <= min(j, s - a[i]); q++) // if (s >= a[i] + q) dp[i][s][j] += dp[i - 1][s - (a[i] + q)][j - q], dp[i][s][j] %= M; // cout << i << ' ' << s << ' ' << j << ' ' << dp[i][s][j] << endl; } int ans = 0; for (int j = 0; j <= k; j++) ans += dp[n][pref[n]][j], ans %= M; cout << ans; return 0; }
#include <bits/stdc++.h> #define pb push_back #define int long long using namespace std; const int N = 3e2 + 5; int n, a[N], dp[N][N][N], pref[N]; const int M = 998244353; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string S; int k; cin >> S >> k; int sz = S.size(); k = min(k, sz); int cur = 0; for (int i = 0; i < S.size(); i++) if (S[i] == '1') cur++; else n++, a[n] = cur, cur = 0; n++; a[n] = cur; for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i]; dp[0][0][0] = 1; for (int i = 1; i <= n; i++) for (int s = pref[i]; s <= pref[n]; s++) for (int j = 0; j <= k; j++) { for (int q = max(1ll, a[i] - s); q <= a[i]; q++) // if (s >= a[i] - q) dp[i][s][j] += dp[i - 1][s - (a[i] - q)][j], dp[i][s][j] %= M; for (int q = 0; q <= min(j, s - a[i]); q++) // if (s >= a[i] + q) dp[i][s][j] += dp[i - 1][s - (a[i] + q)][j - q], dp[i][s][j] %= M; // cout << i << ' ' << s << ' ' << j << ' ' << dp[i][s][j] << endl; } int ans = 0; for (int j = 0; j <= k; j++) ans += dp[n][pref[n]][j], ans %= M; cout << ans; return 0; }
insert
17
17
17
19
-11
p02635
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; long d[333][666][666]; vector<int> u; int p = 998244353; int f(int i, int v, int w) { if (!i) return v ? 0 : 1; if (v > w) return 0; if (d[i][v][w] >= 0) return d[i][v][w]; d[i][v][w] = 0; for (int j = max(-u[i - 1], -w); j <= v; j++) (d[i][v][w] += f(i - 1, v - j, w - abs(j))) %= p; return d[i][v][w]; } int main() { string s; int k; cin >> s >> k; k = min(k, 300); k += k; for (s += '0';;) { auto i = s.find('0'); if (i == -1) break; s = s.substr(i + 1); u.push_back(i); } for (int i = 0; i < 333; i++) for (int j = 0; j <= k; j++) for (int w = 0; w <= k; w++) d[i][j][w] = -1; cout << f(u.size(), 0, k) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int d[333][666][666]; vector<int> u; int p = 998244353; int f(int i, int v, int w) { if (!i) return v ? 0 : 1; if (v > w) return 0; if (d[i][v][w] >= 0) return d[i][v][w]; d[i][v][w] = 0; for (int j = max(-u[i - 1], -w); j <= v; j++) (d[i][v][w] += f(i - 1, v - j, w - abs(j))) %= p; return d[i][v][w]; } int main() { string s; int k; cin >> s >> k; k = min(k, 300); k += k; for (s += '0';;) { auto i = s.find('0'); if (i == -1) break; s = s.substr(i + 1); u.push_back(i); } for (int i = 0; i < 333; i++) for (int j = 0; j <= k; j++) for (int w = 0; w <= k; w++) d[i][j][w] = -1; cout << f(u.size(), 0, k) << endl; return 0; }
replace
3
4
3
4
MLE
p02635
C++
Runtime Error
#include <bits/stdc++.h> #define ull unchaed long long #define ll long long #define il inline #define db double #define ls rt << 1 #define rs rt << 1 | 1 #define pb push_back #define mp make_pair #define pii pair<int, int> #define X first #define Y second #define pcc pair<char, char> #define vi vector<int> #define vl vector<ll> #define rep(i, x, y) for (int i = x; i <= y; i++) #define rrep(i, x, y) for (int i = x; i >= y; i--) #define rep0(i, n) for (int i = 0; i < (n); i++) #define per0(i, n) for (int i = (n)-1; i >= 0; i--) #define ept 1e-9 #define INF 0x3f3f3f3f #define sz(x) (x).size() #define Axx(x) (x).begin(), (x).cntd() using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline ll read1() { ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } using namespace std; const int mod = 998244353; const int N = 310; inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } char s[N]; int k, n, seq[N], tot, pre[N]; int dp[N][N], S; void do_dp(int minsum, int cur) { static int pre1[N][N], pre2[N][N]; for (int i = 0; i <= S; i++) for (int j = 0; j <= S; j++) pre1[i][j] = pre2[i][j] = 0; // puts("dp"); for (int i = 0; i <= S; i++) for (int j = 0; j <= S; j++) if (dp[i][j]) { // 0 <= a[i] < cur add(pre1[i][j], dp[i][j]); add(pre1[i + cur][j], mod - dp[i][j]); // a[i] >= cur add(pre2[i + cur][j], dp[i][j]); } memset(dp, 0, sizeof(dp)); for (int i = 0; i <= S; i++) for (int j = 0; j <= S; j++) { if (i) add(pre1[i][j], pre1[i - 1][j]); if (i && j) add(pre2[i][j], pre2[i - 1][j - 1]); if (i >= minsum) { dp[i][j] = pre1[i][j] + pre2[i][j]; if (dp[i][j] >= mod) dp[i][j] -= mod; } } } int main() { scanf("%s%d", s + 1, &k); n = strlen(s + 1); int last = 0; for (int i = 1; i <= n; i++) if (s[i] == '0') { seq[++tot] = i - last - 1; last = i; } seq[++tot] = n - last; for (int i = 1; i <= tot; i++) pre[i] = pre[i - 1] + seq[i]; S = pre[tot]; dp[0][0] = 1; for (int i = 1; i <= tot; i++) do_dp(pre[i], seq[i]); int ans = 0; for (int i = 0; i <= S && i <= k; i++) add(ans, dp[S][i]); cout << ans; return 0; }
#include <bits/stdc++.h> #define ull unchaed long long #define ll long long #define il inline #define db double #define ls rt << 1 #define rs rt << 1 | 1 #define pb push_back #define mp make_pair #define pii pair<int, int> #define X first #define Y second #define pcc pair<char, char> #define vi vector<int> #define vl vector<ll> #define rep(i, x, y) for (int i = x; i <= y; i++) #define rrep(i, x, y) for (int i = x; i >= y; i--) #define rep0(i, n) for (int i = 0; i < (n); i++) #define per0(i, n) for (int i = (n)-1; i >= 0; i--) #define ept 1e-9 #define INF 0x3f3f3f3f #define sz(x) (x).size() #define Axx(x) (x).begin(), (x).cntd() using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline ll read1() { ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } using namespace std; const int mod = 998244353; const int N = 1010; inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } char s[N]; int k, n, seq[N], tot, pre[N]; int dp[N][N], S; void do_dp(int minsum, int cur) { static int pre1[N][N], pre2[N][N]; for (int i = 0; i <= S; i++) for (int j = 0; j <= S; j++) pre1[i][j] = pre2[i][j] = 0; // puts("dp"); for (int i = 0; i <= S; i++) for (int j = 0; j <= S; j++) if (dp[i][j]) { // 0 <= a[i] < cur add(pre1[i][j], dp[i][j]); add(pre1[i + cur][j], mod - dp[i][j]); // a[i] >= cur add(pre2[i + cur][j], dp[i][j]); } memset(dp, 0, sizeof(dp)); for (int i = 0; i <= S; i++) for (int j = 0; j <= S; j++) { if (i) add(pre1[i][j], pre1[i - 1][j]); if (i && j) add(pre2[i][j], pre2[i - 1][j - 1]); if (i >= minsum) { dp[i][j] = pre1[i][j] + pre2[i][j]; if (dp[i][j] >= mod) dp[i][j] -= mod; } } } int main() { scanf("%s%d", s + 1, &k); n = strlen(s + 1); int last = 0; for (int i = 1; i <= n; i++) if (s[i] == '0') { seq[++tot] = i - last - 1; last = i; } seq[++tot] = n - last; for (int i = 1; i <= tot; i++) pre[i] = pre[i - 1] + seq[i]; S = pre[tot]; dp[0][0] = 1; for (int i = 1; i <= tot; i++) do_dp(pre[i], seq[i]); int ans = 0; for (int i = 0; i <= S && i <= k; i++) add(ans, dp[S][i]); cout << ans; return 0; }
replace
54
55
54
55
0
p02635
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifdef Adrian #include "debug.h" #else #define debug(...) 9999 #endif typedef long long ll; typedef long double ld; typedef complex<ld> point; #define F first #define S second #define ii pair<ll, ll> template <typename G> struct triple { G F, S, T; }; #define iii triple<int> int main() { #ifdef Adrian // freopen("test.txt", "r", stdin); #else ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif const ll mod = 998244353; string s; int h; cin >> s >> h; int n = 0; for (auto &i : s) n += (i == '1'); vector<int> v; for (int i = 0, j = 0; i < s.size(); i = j) { for (; j < s.size() && s[j] == '1'; j++) ; v.push_back(j - i); ++j; } int m = v.size(); vector<vector<ll>> cur(n + 1, vector<ll>(n + 1)); vector<vector<ll>> next(n + 1, vector<ll>(n + 1)); for (int i = 0; i <= n; i++) cur[i][0] = 1; vector<vector<ll>> ac1(2 * n + 1, vector<ll>(n + 2)); vector<vector<ll>> ac2(n + 1, vector<ll>(n + 2)); for (int i = 1; i < m; i++) { for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) { ac1[j + k][j + 1] = cur[j][k]; ac2[k][j + 1] = cur[j][k]; } for (auto &j : ac1) for (int k = 1; k < j.size(); k++) j[k] = (j[k] + j[k - 1]) % mod; for (auto &j : ac2) for (int k = 1; k < j.size(); k++) j[k] = (j[k] + j[k - 1]) % mod; for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) { next[j][k] = ac1[j + k][min(n, j + min(v[i], k)) + 1] - ac1[j + k][j]; next[j][k] += ac2[k][j]; next[j][k] %= mod; if (next[j][k] < 0) next[j][k] += mod; } swap(next, cur); } ll ans = 0; for (int i = 0; i <= h; i++) ans = (ans + cur[0][i]) % mod; cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef Adrian #include "debug.h" #else #define debug(...) 9999 #endif typedef long long ll; typedef long double ld; typedef complex<ld> point; #define F first #define S second #define ii pair<ll, ll> template <typename G> struct triple { G F, S, T; }; #define iii triple<int> int main() { #ifdef Adrian // freopen("test.txt", "r", stdin); #else ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif const ll mod = 998244353; string s; int h; cin >> s >> h; int n = 0; for (auto &i : s) n += (i == '1'); vector<int> v; for (int i = 0, j = 0; i < s.size(); i = j) { for (; j < s.size() && s[j] == '1'; j++) ; v.push_back(j - i); ++j; } int m = v.size(); vector<vector<ll>> cur(n + 1, vector<ll>(n + 1)); vector<vector<ll>> next(n + 1, vector<ll>(n + 1)); for (int i = 0; i <= n; i++) cur[i][0] = 1; vector<vector<ll>> ac1(2 * n + 1, vector<ll>(n + 2)); vector<vector<ll>> ac2(n + 1, vector<ll>(n + 2)); for (int i = 1; i < m; i++) { for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) { ac1[j + k][j + 1] = cur[j][k]; ac2[k][j + 1] = cur[j][k]; } for (auto &j : ac1) for (int k = 1; k < j.size(); k++) j[k] = (j[k] + j[k - 1]) % mod; for (auto &j : ac2) for (int k = 1; k < j.size(); k++) j[k] = (j[k] + j[k - 1]) % mod; for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) { next[j][k] = ac1[j + k][min(n, j + min(v[i], k)) + 1] - ac1[j + k][j]; next[j][k] += ac2[k][j]; next[j][k] %= mod; if (next[j][k] < 0) next[j][k] += mod; } swap(next, cur); } ll ans = 0; for (int i = 0; i <= min(h, n); i++) ans = (ans + cur[0][i]) % mod; cout << ans << '\n'; return 0; }
replace
86
87
86
87
0
p02635
C++
Runtime Error
#line 2 "lib/template.cpp" #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> using namespace std; using ll = long long; using ull = unsigned long long; using VI = vector<int>; using VVI = vector<vector<int>>; using VLL = vector<ll>; using VVLL = vector<vector<ll>>; using VB = vector<bool>; using PII = pair<int, int>; using PLL = pair<ll, ll>; constexpr int INF = 1000000007; constexpr ll INF_LL = 1'000'000'000'000'000'007; #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define newl '\n' // loops rep(until) / rep(var, until) / rep(var, from, until) / repr (reversed // order) #define OVERLOAD3(_1, _2, _3, name, ...) name #define rep(...) \ OVERLOAD3(__VA_ARGS__, REPEAT_FROM_UNTIL, REPEAT_UNTIL, REPEAT)(__VA_ARGS__) #define REPEAT(times) REPEAT_CNT(_repeat, __COUNTER__, times) #define REPEAT_CNT(_repeat, cnt, times) REPEAT_CNT_CAT(_repeat, cnt, times) #define REPEAT_CNT_CAT(_repeat, cnt, times) \ REPEAT_FROM_UNTIL(_repeat##cnt, 0, times) #define REPEAT_UNTIL(name, times) REPEAT_FROM_UNTIL(name, 0, times) #define REPEAT_FROM_UNTIL(name, from, until) \ for (int name = from, name##__until = (until); name < name##__until; name++) #define repr(...) \ OVERLOAD3(__VA_ARGS__, REPR_FROM_UNTIL, REPR_UNTIL, REPEAT)(__VA_ARGS__) #define REPR_UNTIL(name, times) REPR_FROM_UNTIL(name, 0, times) #define REPR_FROM_UNTIL(name, from, until) \ for (int name = (until)-1, name##__from = (from); name >= name##__from; \ name--) template <typename T, typename U> bool chmin(T &var, U x) { if (var > x) { var = x; return true; } else return false; } template <typename T, typename U> bool chmax(T &var, U x) { if (var < x) { var = x; return true; } else return false; } ll power(ll e, ll t, ll mod = INF_LL) { ll res = 1; for (; t; t >>= 1, (e *= e) %= mod) if (t & 1) (res *= e) %= mod; return res; } ll choose(ll n, int r) { chmin(r, n - r); if (r < 0) return 0; ll res = 1; rep(i, r) res *= n - i, res /= i + 1; return res; } template <typename T, typename U> T divceil(T m, U d) { return (m + d - 1) / d; } template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } // debugging stuff #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmisleading-indentation" #define repi(it, ds) for (auto it = ds.begin(); it != ds.end(); it++) class DebugPrint { public: template <typename T> DebugPrint &operator<<(const T &v) { #ifdef LOCAL cerr << v; #endif return *this; } } debugos; template <typename T> DebugPrint &operator<<(DebugPrint &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; } template <typename T, typename U> DebugPrint &operator<<(DebugPrint &os, const map<T, U> &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T> DebugPrint &operator<<(DebugPrint &os, const set<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T, typename U> DebugPrint &operator<<(DebugPrint &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } void dump_func() { debugos << newl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { debugos << head; if (sizeof...(Tail) > 0) { debugos << ", "; } dump_func(forward<Tail>(tail)...); } #ifdef LOCAL #define dump(...) \ debugos << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << newl \ << " ", \ dump_func(__VA_ARGS__) #else #define dump(...) ((void)0) #endif #pragma GCC diagnostic pop #line 2 "main.cpp" constexpr ll mod = 998244353; int main() { string s; int k; cin >> s >> k; VI a = {0}; for (auto c : s) if (c == '1') a.back()++; else a.push_back(0); reverse(all(a)); int n = a.size(); dump(a); static ll dp[305][305][305]; // i, kx, carry dp[0][0][0] = 1; rep(i, n) rep(kx, k + 1) rep(carry, k + 1) { if ((dp[i][kx][carry] %= mod) == 0) continue; rep(fw, 1, a[i] + 1) dp[i + 1][kx][carry + fw] += dp[i][kx][carry]; rep(take, carry + 1) dp[i + 1][kx + take][carry - take] += dp[i][kx][carry]; } ll res = 0; rep(kx, k + 1) res += dp[n][kx][0]; cout << res % mod << newl; }
#line 2 "lib/template.cpp" #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> using namespace std; using ll = long long; using ull = unsigned long long; using VI = vector<int>; using VVI = vector<vector<int>>; using VLL = vector<ll>; using VVLL = vector<vector<ll>>; using VB = vector<bool>; using PII = pair<int, int>; using PLL = pair<ll, ll>; constexpr int INF = 1000000007; constexpr ll INF_LL = 1'000'000'000'000'000'007; #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define newl '\n' // loops rep(until) / rep(var, until) / rep(var, from, until) / repr (reversed // order) #define OVERLOAD3(_1, _2, _3, name, ...) name #define rep(...) \ OVERLOAD3(__VA_ARGS__, REPEAT_FROM_UNTIL, REPEAT_UNTIL, REPEAT)(__VA_ARGS__) #define REPEAT(times) REPEAT_CNT(_repeat, __COUNTER__, times) #define REPEAT_CNT(_repeat, cnt, times) REPEAT_CNT_CAT(_repeat, cnt, times) #define REPEAT_CNT_CAT(_repeat, cnt, times) \ REPEAT_FROM_UNTIL(_repeat##cnt, 0, times) #define REPEAT_UNTIL(name, times) REPEAT_FROM_UNTIL(name, 0, times) #define REPEAT_FROM_UNTIL(name, from, until) \ for (int name = from, name##__until = (until); name < name##__until; name++) #define repr(...) \ OVERLOAD3(__VA_ARGS__, REPR_FROM_UNTIL, REPR_UNTIL, REPEAT)(__VA_ARGS__) #define REPR_UNTIL(name, times) REPR_FROM_UNTIL(name, 0, times) #define REPR_FROM_UNTIL(name, from, until) \ for (int name = (until)-1, name##__from = (from); name >= name##__from; \ name--) template <typename T, typename U> bool chmin(T &var, U x) { if (var > x) { var = x; return true; } else return false; } template <typename T, typename U> bool chmax(T &var, U x) { if (var < x) { var = x; return true; } else return false; } ll power(ll e, ll t, ll mod = INF_LL) { ll res = 1; for (; t; t >>= 1, (e *= e) %= mod) if (t & 1) (res *= e) %= mod; return res; } ll choose(ll n, int r) { chmin(r, n - r); if (r < 0) return 0; ll res = 1; rep(i, r) res *= n - i, res /= i + 1; return res; } template <typename T, typename U> T divceil(T m, U d) { return (m + d - 1) / d; } template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } // debugging stuff #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmisleading-indentation" #define repi(it, ds) for (auto it = ds.begin(); it != ds.end(); it++) class DebugPrint { public: template <typename T> DebugPrint &operator<<(const T &v) { #ifdef LOCAL cerr << v; #endif return *this; } } debugos; template <typename T> DebugPrint &operator<<(DebugPrint &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; } template <typename T, typename U> DebugPrint &operator<<(DebugPrint &os, const map<T, U> &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T> DebugPrint &operator<<(DebugPrint &os, const set<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T, typename U> DebugPrint &operator<<(DebugPrint &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } void dump_func() { debugos << newl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { debugos << head; if (sizeof...(Tail) > 0) { debugos << ", "; } dump_func(forward<Tail>(tail)...); } #ifdef LOCAL #define dump(...) \ debugos << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << newl \ << " ", \ dump_func(__VA_ARGS__) #else #define dump(...) ((void)0) #endif #pragma GCC diagnostic pop #line 2 "main.cpp" constexpr ll mod = 998244353; int main() { string s; int k; cin >> s >> k; chmin(k, count(all(s), '1')); VI a = {0}; for (auto c : s) if (c == '1') a.back()++; else a.push_back(0); reverse(all(a)); int n = a.size(); dump(a); static ll dp[305][305][305]; // i, kx, carry dp[0][0][0] = 1; rep(i, n) rep(kx, k + 1) rep(carry, k + 1) { if ((dp[i][kx][carry] %= mod) == 0) continue; rep(fw, 1, a[i] + 1) dp[i + 1][kx][carry + fw] += dp[i][kx][carry]; rep(take, carry + 1) dp[i + 1][kx + take][carry - take] += dp[i][kx][carry]; } ll res = 0; rep(kx, k + 1) res += dp[n][kx][0]; cout << res % mod << newl; }
insert
167
167
167
168
-11
p02635
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pi; typedef long long ll; const ll mod = 998244353; string s; ll K, dp[305][305][305]; // groups, moves, carry ll N, sz[305]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s >> K; int gn = 1, gz = 0; N = s.size(); K = min(K, N); reverse(s.begin(), s.end()); for (int i = 0; i < s.size(); ++i) { if (s[i] == '0') { sz[gn] = gz; gz = 0; gn++; } else gz++; } sz[gn] = gz; /* for (int i = 1; i <= gn; ++i){ cout << sz[i] << ' '; } cout << '\n'; */ dp[0][0][0] = 1; for (ll i = 1; i <= gn; ++i) { for (ll k = 0; k <= K; ++k) { for (ll c = 0; c <= k; ++c) { for (ll l = 0; (c + l) <= N; ++l) { dp[i][k][c] += dp[i - 1][k][c + l]; dp[i][k][c] %= mod; } for (ll l = 1; l <= min(min(sz[i], k), c); ++l) { dp[i][k][c] += dp[i - 1][k - l][c - l]; dp[i][k][c] %= mod; } // cout << i << ' ' << k << ' ' << c << ' ' << dp[i][k][c] << '\n'; } } } ll ans = 0; for (int i = 0; i <= K; ++i) { ans += dp[gn][i][0]; ans %= mod; } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pi; typedef long long ll; const ll mod = 998244353; string s; ll K, dp[305][305][305]; // groups, moves, carry ll N, sz[305]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s >> K; int gn = 1, gz = 0; N = s.size(); K = min(K, N); reverse(s.begin(), s.end()); for (int i = 0; i < s.size(); ++i) { if (s[i] == '0') { sz[gn] = gz; gz = 0; gn++; } else gz++; } sz[gn] = gz; /* for (int i = 1; i <= gn; ++i){ cout << sz[i] << ' '; } cout << '\n'; */ dp[0][0][0] = 1; for (ll i = 1; i <= gn; ++i) { for (ll k = 0; k <= K; ++k) { for (ll c = 0; c <= k; ++c) { for (ll l = 0; (c + l) <= k; ++l) { dp[i][k][c] += dp[i - 1][k][c + l]; dp[i][k][c] %= mod; } for (ll l = 1; l <= min(min(sz[i], k), c); ++l) { dp[i][k][c] += dp[i - 1][k - l][c - l]; dp[i][k][c] %= mod; } // cout << i << ' ' << k << ' ' << c << ' ' << dp[i][k][c] << '\n'; } } } ll ans = 0; for (int i = 0; i <= K; ++i) { ans += dp[gn][i][0]; ans %= mod; } cout << ans << '\n'; }
replace
42
43
42
43
TLE
p02636
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define forn(i, n) for (int i = 0; i < (int)(n); i++) #define si(c) ((int)(c).size()) #define forsn(i, s, n) for (int i = (int)(s); i < ((int)n); i++) #define dforsn(i, s, n) for (int i = (int)(n)-1; i >= ((int)s); i--) #define all(c) (c).begin(), (c).end() #define D(a) cerr << #a << "=" << a << endl; #define pb push_back #define eb emplace_back #define mp make_pair typedef long long int ll; typedef vector<int> vi; typedef pair<int, int> pii; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 998244353; const int N = 310; int n; string s; bool can[2][N][N]; int best[N][N], suf[N][2], dp[2][N][N]; void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } int memo[N][N][N]; int go(int from, int zeros, int ones) { if (zeros < 0 || ones < 0) return 0; if (zeros + ones == 0) return 1; int &res = memo[from][zeros][ones]; if (~res) return res; res = 0; if (s[from] == '0') add(res, go(from + 1, zeros, ones)); else add(res, go(from, zeros - 1, ones)); if (s[from] == '1') add(res, go(from + 1, zeros, ones)); else add(res, go(from, zeros, ones - 1)); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; n = si(s); dforsn(i, 0, n) { forn(j, 2) { suf[i][j] = s[i] == '0' + j; suf[i][j] += suf[i + 1][j]; } } memset(best, -1, sizeof best); auto cur = can[0], prv = can[1]; cur[0][0] = 1; for (int take = 1; take < n; take++) { swap(cur, prv); for (int a = 0; a <= n; a++) { for (int b = 0; b <= n - a; b++) { cur[a][b] = 0; } } for (int a = 0; a <= n; a++) { for (int b = 0; b <= n - a; b++) { if (prv[a][b]) { if (a >= 1 && a + b >= 2) cur[a - 1][b] = 1; if (b >= 1 && a + b >= 2) cur[a][b - 1] = 1; int rest = n - take + 1 - a - b; int pos = n - rest; if (a + b > 0) cur[a][b] = 1; if (s[pos] == '0' && b > 0) cur[a + 1][b - 1] = 1; if (s[pos] == '1' && a > 0) cur[a - 1][b + 1] = 1; if (pos + 1 < n) { if (s[pos] == '0' || s[pos + 1] == '0') cur[a + 1][b] = 1; if (s[pos] == '1' || s[pos + 1] == '1') cur[a][b + 1] = 1; } } } } for (int a = 0; a <= n; a++) { for (int b = 0; b <= n - a; b++) if (cur[a][b]) { int rest = n - take - a - b; int pos = n - rest; int aa = suf[pos][0] + a; int bb = suf[pos][1] + b; best[aa][bb] = max(best[aa][bb], pos); } } } s += '$'; memset(memo, -1, sizeof memo); ll ans = 0; forn(a, n + 1) forn(b, n + 1) if (best[a][b] != -1) { int pos = best[a][b]; int ca = a - suf[pos][0]; int cb = b - suf[pos][1]; ans += go(pos, ca, cb); } cout << (ans + 1) % MOD << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define forn(i, n) for (int i = 0; i < (int)(n); i++) #define si(c) ((int)(c).size()) #define forsn(i, s, n) for (int i = (int)(s); i < ((int)n); i++) #define dforsn(i, s, n) for (int i = (int)(n)-1; i >= ((int)s); i--) #define all(c) (c).begin(), (c).end() #define D(a) cerr << #a << "=" << a << endl; #define pb push_back #define eb emplace_back #define mp make_pair typedef long long int ll; typedef vector<int> vi; typedef pair<int, int> pii; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 998244353; const int N = 310; int n; string s; bool can[2][N][N]; int best[N][N], suf[N][2], dp[2][N][N]; void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } int memo[N][N][N]; int go(int from, int zeros, int ones) { if (zeros < 0 || ones < 0) return 0; if (zeros + ones == 0) return 1; int &res = memo[from][zeros][ones]; if (~res) return res; res = 0; if (s[from] == '0') add(res, go(from + 1, zeros, ones)); else add(res, go(from, zeros - 1, ones)); if (s[from] == '1') add(res, go(from + 1, zeros, ones)); else add(res, go(from, zeros, ones - 1)); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; n = si(s); dforsn(i, 0, n) { forn(j, 2) { suf[i][j] = s[i] == '0' + j; suf[i][j] += suf[i + 1][j]; } } memset(best, -1, sizeof best); auto cur = can[0], prv = can[1]; cur[0][0] = 1; for (int take = 1; take < n; take++) { swap(cur, prv); for (int a = 0; a <= n; a++) { for (int b = 0; b <= n - a; b++) { cur[a][b] = 0; } } for (int a = 0; a <= n; a++) { for (int b = 0; b <= n - a; b++) { if (prv[a][b]) { if (a >= 1 && a + b >= 2) cur[a - 1][b] = 1; if (b >= 1 && a + b >= 2) cur[a][b - 1] = 1; int rest = n - take + 1 - a - b; int pos = n - rest; if (pos < n) { if (a + b > 0) cur[a][b] = 1; if (s[pos] == '0' && b > 0) cur[a + 1][b - 1] = 1; if (s[pos] == '1' && a > 0) cur[a - 1][b + 1] = 1; if (pos + 1 < n) { if (s[pos] == '0' || s[pos + 1] == '0') cur[a + 1][b] = 1; if (s[pos] == '1' || s[pos + 1] == '1') cur[a][b + 1] = 1; } } } } } for (int a = 0; a <= n; a++) { for (int b = 0; b <= n - a; b++) if (cur[a][b]) { int rest = n - take - a - b; int pos = n - rest; int aa = suf[pos][0] + a; int bb = suf[pos][1] + b; best[aa][bb] = max(best[aa][bb], pos); } } } s += '$'; memset(memo, -1, sizeof memo); ll ans = 0; forn(a, n + 1) forn(b, n + 1) if (best[a][b] != -1) { int pos = best[a][b]; int ca = a - suf[pos][0]; int cb = b - suf[pos][1]; ans += go(pos, ca, cb); } cout << (ans + 1) % MOD << endl; return 0; }
replace
89
100
89
102
-11
p02636
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 210; typedef long long ll; const int mod = 998244353; inline int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b) { return 1ll * a * b % mod; } inline int qpow(int a, int b) { int ret = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) ret = mul(ret, a); return ret; } /* math */ int g[N][N][N], f[N][N][N], n; // j zeros to add, k ones to add; char s[N]; int main() { scanf("%s", s + 1); n = strlen(s + 1); reverse(s + 1, s + n + 1); s[n + 1] = '2'; f[0][0][0] = g[0][0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; j + k <= i; k++) { // f[i][j][k]=add(f[i][j][k], g[i][j][k]); if (g[i][j][k]) { int _d = i - j - k + 1; int dir = s[_d] - '0'; g[i + 1][j][k] = add(g[i][j][k], g[i + 1][j][k]); if (dir) g[i + 1][j + 1][k] = add(g[i + 1][j + 1][k], g[i][j][k]); else g[i + 1][j][k + 1] = add(g[i + 1][j][k + 1], g[i][j][k]); } /*-------*/ } } } f[n][0][0] = 1; for (int i = n - 1; i; i--) { for (int j = 0; j <= i; j++) { for (int k = 0; j + k <= i; k++) { int dir = s[i - j - k + 1] - '0'; int dir2 = s[i - j - k + 2] - '0'; // assert(dir>=0&&dir<=1&&dir2>=0&&dir2<=1); // cerr << dir << " " << dir2 << endl; int nt[3]; nt[0] = j, nt[1] = k, nt[2] = 0; nt[0]++; if (nt[dir]) { nt[dir]--; if (nt[dir2] && (dir2 == 0 || dir == 0)) nt[dir2]--; } f[i][j][k] |= f[i + 1][nt[0]][nt[1]]; // cerr << nt[0] << " " << nt[1] << ","; nt[0] = j, nt[1] = k; nt[1]++; if (nt[dir]) { nt[dir]--; if (nt[dir2] && (dir2 == 1 || dir == 1)) nt[dir2]--; } f[i][j][k] |= f[i + 1][nt[0]][nt[1]]; // cerr << nt[0] << " " << nt[1] << endl; // cerr << i << " " << j << " " << k << "|" << //f[i][j][k] << endl; } } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= i; j++) for (int k = 0; k + j <= i; k++) { if (f[i][j][k]) { // cerr << i << " " << j << " " << k << " " << //g[i][j][k] << endl; ans = add(ans, g[i][j][k]); } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 310; typedef long long ll; const int mod = 998244353; inline int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b) { return 1ll * a * b % mod; } inline int qpow(int a, int b) { int ret = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) ret = mul(ret, a); return ret; } /* math */ int g[N][N][N], f[N][N][N], n; // j zeros to add, k ones to add; char s[N]; int main() { scanf("%s", s + 1); n = strlen(s + 1); reverse(s + 1, s + n + 1); s[n + 1] = '2'; f[0][0][0] = g[0][0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; j + k <= i; k++) { // f[i][j][k]=add(f[i][j][k], g[i][j][k]); if (g[i][j][k]) { int _d = i - j - k + 1; int dir = s[_d] - '0'; g[i + 1][j][k] = add(g[i][j][k], g[i + 1][j][k]); if (dir) g[i + 1][j + 1][k] = add(g[i + 1][j + 1][k], g[i][j][k]); else g[i + 1][j][k + 1] = add(g[i + 1][j][k + 1], g[i][j][k]); } /*-------*/ } } } f[n][0][0] = 1; for (int i = n - 1; i; i--) { for (int j = 0; j <= i; j++) { for (int k = 0; j + k <= i; k++) { int dir = s[i - j - k + 1] - '0'; int dir2 = s[i - j - k + 2] - '0'; // assert(dir>=0&&dir<=1&&dir2>=0&&dir2<=1); // cerr << dir << " " << dir2 << endl; int nt[3]; nt[0] = j, nt[1] = k, nt[2] = 0; nt[0]++; if (nt[dir]) { nt[dir]--; if (nt[dir2] && (dir2 == 0 || dir == 0)) nt[dir2]--; } f[i][j][k] |= f[i + 1][nt[0]][nt[1]]; // cerr << nt[0] << " " << nt[1] << ","; nt[0] = j, nt[1] = k; nt[1]++; if (nt[dir]) { nt[dir]--; if (nt[dir2] && (dir2 == 1 || dir == 1)) nt[dir2]--; } f[i][j][k] |= f[i + 1][nt[0]][nt[1]]; // cerr << nt[0] << " " << nt[1] << endl; // cerr << i << " " << j << " " << k << "|" << //f[i][j][k] << endl; } } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= i; j++) for (int k = 0; k + j <= i; k++) { if (f[i][j][k]) { // cerr << i << " " << j << " " << k << " " << //g[i][j][k] << endl; ans = add(ans, g[i][j][k]); } } } cout << ans << endl; return 0; }
replace
2
3
2
3
0
p02637
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int k; cin >> k; vector<int> freq(k); for (int &x : freq) cin >> x; int minv = 1000000, maxv = -1; for (int x : freq) { minv = min(minv, x); maxv = max(maxv, x); } int sum = 0; for (int x : freq) sum += x; if (minv * 2 < maxv) { cout << -1 << '\n'; return 0; } vector<int> ans; int maxworks = 0; while ((int)ans.size() < sum) { bool found = false; for (int nxt = 0; nxt < k; nxt++) { vector<int> cans = ans; cans.push_back(nxt); int cmaxworks = maxworks; { if ((int)cans.size() >= k) { vector<int> d(k, 0); for (int i = 0; i < k; i++) { d[cans[(int)cans.size() - 1 - i]]++; } bool works = true; for (int i = 0; i < k; i++) if (!d[i]) works = false; if (works) cmaxworks = (int)cans.size(); } } vector<int> cfreq = freq; cfreq[nxt]--; if (cfreq[nxt] < 0) continue; vector<int> known; for (int f = cmaxworks; f < (int)cans.size(); f++) known.push_back(cans[f]); vector<int> zk = known; sort(zk.begin(), zk.end()); if (unique(zk.begin(), zk.end()) != zk.end()) continue; bool ok = false; { vector<int> dfreq = cfreq; for (int i = 0; i < k; i++) dfreq[i]--; multiset<int> g; for (int f : dfreq) g.insert(f); if (*g.rbegin() <= 2 * *g.begin()) ok = true; for (int a : known) { g.erase(g.find(dfreq[a])); dfreq[a]++; g.insert(dfreq[a]); if (*g.rbegin() <= 2 * *g.begin()) ok = true; } } { vector<int> dfreq = cfreq; for (int i = 0; i < k; i++) dfreq[i]--; for (int a : known) { dfreq[a]++; } multiset<int> g; for (int f : dfreq) g.insert(f); vector<int> cur(k, 0); for (int a : known) cur[a] = 1; for (int b = cmaxworks - 1; b >= 0; b--) { if (cur[cans[b]]) break; cur[cans[b]] = 1; int a = cans[b]; g.erase(g.find(dfreq[a])); dfreq[a]++; g.insert(dfreq[a]); if (*g.rbegin() <= 2 * *g.begin()) ok = true; } } { vector<int> dfreq = cfreq; for (int i = 0; i < k; i++) dfreq[i]--; vector<int> cur(k, 0); for (int a : known) cur[a] = 1; int minval = 1000000; for (int b : dfreq) minval = min(minval, b); for (int i = 0; i < k; i++) { if (cur[i]) continue; if (dfreq[i] > 2 * minval) { dfreq[i]--; cur[i]--; } } int ming = 100000, maxg = 0; for (int b : dfreq) ming = min(ming, b); for (int b : dfreq) maxg = max(maxg, b); if (ming * 2 >= maxg) { ok = true; } } if (ok) { ans.push_back(nxt); maxworks = cmaxworks; found = true; freq[nxt]--; break; } } // for(int b : ans) cerr << b << ' '; // cerr << '\n'; assert(found); } for (int b : ans) cout << b + 1 << ' '; cout << '\n'; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int k; cin >> k; vector<int> freq(k); for (int &x : freq) cin >> x; int minv = 1000000, maxv = -1; for (int x : freq) { minv = min(minv, x); maxv = max(maxv, x); } int sum = 0; for (int x : freq) sum += x; if (minv * 2 < maxv) { cout << -1 << '\n'; return 0; } vector<int> ans; int maxworks = 0; while ((int)ans.size() < sum) { bool found = false; for (int nxt = 0; nxt < k; nxt++) { vector<int> cans = ans; cans.push_back(nxt); int cmaxworks = maxworks; { if ((int)cans.size() >= k) { vector<int> d(k, 0); for (int i = 0; i < k; i++) { d[cans[(int)cans.size() - 1 - i]]++; } bool works = true; for (int i = 0; i < k; i++) if (!d[i]) works = false; if (works) cmaxworks = (int)cans.size(); } } vector<int> cfreq = freq; cfreq[nxt]--; if (cfreq[nxt] < 0) continue; vector<int> known; for (int f = cmaxworks; f < (int)cans.size(); f++) known.push_back(cans[f]); vector<int> zk = known; sort(zk.begin(), zk.end()); if (unique(zk.begin(), zk.end()) != zk.end()) continue; bool ok = false; { vector<int> dfreq = cfreq; for (int i = 0; i < k; i++) dfreq[i]--; multiset<int> g; for (int f : dfreq) g.insert(f); if (*g.rbegin() <= 2 * *g.begin()) ok = true; reverse(known.begin(), known.end()); for (int a : known) { g.erase(g.find(dfreq[a])); dfreq[a]++; g.insert(dfreq[a]); if (*g.rbegin() <= 2 * *g.begin()) ok = true; } } { vector<int> dfreq = cfreq; for (int i = 0; i < k; i++) dfreq[i]--; for (int a : known) { dfreq[a]++; } multiset<int> g; for (int f : dfreq) g.insert(f); vector<int> cur(k, 0); for (int a : known) cur[a] = 1; for (int b = cmaxworks - 1; b >= 0; b--) { if (cur[cans[b]]) break; cur[cans[b]] = 1; int a = cans[b]; g.erase(g.find(dfreq[a])); dfreq[a]++; g.insert(dfreq[a]); if (*g.rbegin() <= 2 * *g.begin()) ok = true; } } { vector<int> dfreq = cfreq; for (int i = 0; i < k; i++) dfreq[i]--; vector<int> cur(k, 0); for (int a : known) cur[a] = 1; int minval = 1000000; for (int b : dfreq) minval = min(minval, b); for (int i = 0; i < k; i++) { if (cur[i]) continue; if (dfreq[i] > 2 * minval) { dfreq[i]--; cur[i]--; } } int ming = 100000, maxg = 0; for (int b : dfreq) ming = min(ming, b); for (int b : dfreq) maxg = max(maxg, b); if (ming * 2 >= maxg) { ok = true; } } if (ok) { ans.push_back(nxt); maxworks = cmaxworks; found = true; freq[nxt]--; break; } } // for(int b : ans) cerr << b << ' '; // cerr << '\n'; assert(found); } for (int b : ans) cout << b + 1 << ' '; cout << '\n'; }
insert
65
65
65
66
0
p02637
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstring> #include <iostream> #include <set> #include <vector> #define TRACE(x) std::cout << #x << " = " << x << "\n" #define _ << " _ " << using namespace std; bool possiblePrefix(vector<pair<int, int>> A, vector<int> prefix) { int K = A.size(); vector<int> where(K); for (int i = 0; i < K; ++i) where[A[i].second] = i; for (int x : prefix) A[where[x]].first++; sort(A.begin(), A.end()); if (A[0].first * 2 < A.back().first) return false; vector<bool> isOne(K, false), isTwo(K, false); int min = A[0].first, nTwos = 0; for (int i = 0; i < K; ++i) { if (min * 2 == A[i].first) { isTwo[A[i].second] = true; nTwos++; } if (min == A[i].first) isOne[A[i].second] = true; } reverse(prefix.begin(), prefix.end()); while (prefix.size() && isTwo[prefix.back()]) { nTwos--; prefix.pop_back(); } if (!nTwos) return true; for (int x : prefix) if (isOne[x]) return false; return true; } bool possible(vector<pair<int, int>> A, const vector<int> &suffix) { int K = A.size(); vector<int> where(K); for (int i = 0; i < K; ++i) where[A[i].second] = i; for (int i = 0; i < K; ++i) { if (possiblePrefix(A, {})) return true; int j = where[suffix[i]]; if (A[j].first == 0) break; A[j].first--; } return false; } bool possibleFinish(vector<pair<int, int>> A, vector<int> &B, int coveredUpTo) { int K = A.size(); vector<bool> seen(K, false); vector<int> prefix; if (B.size()) { for (int32_t i = B.size() - 1; i >= 0; --i) { if (seen[B[i]]) { if (coveredUpTo <= i) return false; break; } prefix.push_back(B[i]); seen[B[i]] = true; } } reverse(prefix.begin(), prefix.end()); if ((int)prefix.size() == K) return possible(A, prefix); while (coveredUpTo + prefix.size() >= B.size()) { if (possiblePrefix(A, prefix)) return true; if (prefix.size()) { prefix.erase(prefix.begin()); } else { break; } } return false; } int main() { std::ios_base::sync_with_stdio(false); int K; cin >> K; vector<pair<int, int>> A; for (int i = 0; i < K; ++i) { int x; cin >> x; A.emplace_back(x, i); } sort(A.begin(), A.end()); vector<int> B; if (!possibleFinish(A, B, 0)) { cout << "-1\n"; return 0; } int coveredUpTo = 0; while (true) { int tot = 0; for (auto &p : A) tot += p.first; if (tot == 0) break; vector<int> where(K); for (int i = 0; i < K; ++i) where[A[i].second] = i; int x = -1; for (int i = 0; i < K; ++i) { if (A[where[i]].first == 0) continue; A[where[i]].first--; B.push_back(i); if (possibleFinish(A, B, coveredUpTo)) { x = i; break; } B.pop_back(); A[where[i]].first++; } assert(x != -1); if (B.size() >= K) { vector<int> p(B.end() - K, B.end()); sort(p.begin(), p.end()); bool isPerm = true; for (int i = 0; i < K; ++i) isPerm &= p[i] == i; if (isPerm) { assert(coveredUpTo + K >= B.size()); coveredUpTo = B.size(); } } } assert(coveredUpTo == B.size()); for (int x : B) cout << x + 1 << ' '; cout << '\n'; return 0; }
#include <algorithm> #include <cassert> #include <cstring> #include <iostream> #include <set> #include <vector> #define TRACE(x) std::cout << #x << " = " << x << "\n" #define _ << " _ " << using namespace std; bool possiblePrefix(vector<pair<int, int>> A, vector<int> prefix) { int K = A.size(); vector<int> where(K); for (int i = 0; i < K; ++i) where[A[i].second] = i; for (int x : prefix) A[where[x]].first++; sort(A.begin(), A.end()); if (A[0].first * 2 < A.back().first) return false; vector<bool> isOne(K, false), isTwo(K, false); int min = A[0].first, nTwos = 0; for (int i = 0; i < K; ++i) { if (min * 2 == A[i].first) { isTwo[A[i].second] = true; nTwos++; } if (min == A[i].first) isOne[A[i].second] = true; } reverse(prefix.begin(), prefix.end()); while (prefix.size() && !isOne[prefix.back()]) { if (isTwo[prefix.back()]) nTwos--; prefix.pop_back(); } if (!nTwos) return true; for (int x : prefix) if (isOne[x]) return false; return true; } bool possible(vector<pair<int, int>> A, const vector<int> &suffix) { int K = A.size(); vector<int> where(K); for (int i = 0; i < K; ++i) where[A[i].second] = i; for (int i = 0; i < K; ++i) { if (possiblePrefix(A, {})) return true; int j = where[suffix[i]]; if (A[j].first == 0) break; A[j].first--; } return false; } bool possibleFinish(vector<pair<int, int>> A, vector<int> &B, int coveredUpTo) { int K = A.size(); vector<bool> seen(K, false); vector<int> prefix; if (B.size()) { for (int32_t i = B.size() - 1; i >= 0; --i) { if (seen[B[i]]) { if (coveredUpTo <= i) return false; break; } prefix.push_back(B[i]); seen[B[i]] = true; } } reverse(prefix.begin(), prefix.end()); if ((int)prefix.size() == K) return possible(A, prefix); while (coveredUpTo + prefix.size() >= B.size()) { if (possiblePrefix(A, prefix)) return true; if (prefix.size()) { prefix.erase(prefix.begin()); } else { break; } } return false; } int main() { std::ios_base::sync_with_stdio(false); int K; cin >> K; vector<pair<int, int>> A; for (int i = 0; i < K; ++i) { int x; cin >> x; A.emplace_back(x, i); } sort(A.begin(), A.end()); vector<int> B; if (!possibleFinish(A, B, 0)) { cout << "-1\n"; return 0; } int coveredUpTo = 0; while (true) { int tot = 0; for (auto &p : A) tot += p.first; if (tot == 0) break; vector<int> where(K); for (int i = 0; i < K; ++i) where[A[i].second] = i; int x = -1; for (int i = 0; i < K; ++i) { if (A[where[i]].first == 0) continue; A[where[i]].first--; B.push_back(i); if (possibleFinish(A, B, coveredUpTo)) { x = i; break; } B.pop_back(); A[where[i]].first++; } assert(x != -1); if (B.size() >= K) { vector<int> p(B.end() - K, B.end()); sort(p.begin(), p.end()); bool isPerm = true; for (int i = 0; i < K; ++i) isPerm &= p[i] == i; if (isPerm) { assert(coveredUpTo + K >= B.size()); coveredUpTo = B.size(); } } } assert(coveredUpTo == B.size()); for (int x : B) cout << x + 1 << ' '; cout << '\n'; return 0; }
replace
35
37
35
38
0
p02637
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll #define rng(i, a, b) for (int i = int(a); i < int(b); i++) #define rep(i, b) rng(i, 0, b) #define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--) #define per(i, b) gnr(i, 0, b) #define pb push_back #define eb emplace_back #define a first #define b second #define bg begin() #define ed end() #define all(x) x.bg, x.ed #define si(x) int(x.size()) #ifdef LOCAL #define dmp(x) cerr << __LINE__ << " " << #x << " " << x << endl #else #define dmp(x) void(0) #endif template <class t, class u> void chmax(t &a, u b) { if (a < b) a = b; } template <class t, class u> void chmin(t &a, u b) { if (b < a) a = b; } template <class t> using vc = vector<t>; template <class t> using vvc = vc<vc<t>>; using pi = pair<int, int>; using vi = vc<int>; template <class t, class u> ostream &operator<<(ostream &os, const pair<t, u> &p) { return os << "{" << p.a << "," << p.b << "}"; } template <class t> ostream &operator<<(ostream &os, const vc<t> &v) { os << "{"; for (auto e : v) os << e << ","; return os << "}"; } #define mp make_pair #define mt make_tuple #define one(x) memset(x, -1, sizeof(x)) #define zero(x) memset(x, 0, sizeof(x)) #ifdef LOCAL void dmpr(ostream &os) { os << endl; } template <class T, class... Args> void dmpr(ostream &os, const T &t, const Args &...args) { os << t << " "; dmpr(os, args...); } #define dmp2(...) dmpr(cerr, __LINE__, ##__VA_ARGS__) #else #define dmp2(...) void(0) #endif using uint = unsigned; using ull = unsigned long long; template <class t, size_t n> ostream &operator<<(ostream &os, const array<t, n> &a) { return os << vc<t>(all(a)); } template <int i, class T> void print_tuple(ostream &, const T &) {} template <int i, class T, class H, class... Args> void print_tuple(ostream &os, const T &t) { if (i) os << ","; os << get<i>(t); print_tuple<i + 1, T, Args...>(os, t); } template <class... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { os << "{"; print_tuple<0, tuple<Args...>, Args...>(os, t); return os << "}"; } template <class t> void print(t x, int suc = 1) { cout << x; if (suc == 1) cout << "\n"; if (suc == 2) cout << " "; } ll read() { ll i; cin >> i; return i; } vi readvi(int n, int off = 0) { vi v(n); rep(i, n) v[i] = read() + off; return v; } template <class T> void print(const vector<T> &v, int suc = 1) { rep(i, v.size()) print(v[i], i == int(v.size()) - 1 ? suc : 2); } string readString() { string s; cin >> s; return s; } template <class T> T sq(const T &t) { return t * t; } // #define CAPITAL void yes(bool ex = true) { #ifdef CAPITAL cout << "YES" << "\n"; #else cout << "Yes" << "\n"; #endif if (ex) exit(0); } void no(bool ex = true) { #ifdef CAPITAL cout << "NO" << "\n"; #else cout << "No" << "\n"; #endif if (ex) exit(0); } void possible(bool ex = true) { #ifdef CAPITAL cout << "POSSIBLE" << "\n"; #else cout << "Possible" << "\n"; #endif if (ex) exit(0); } void impossible(bool ex = true) { #ifdef CAPITAL cout << "IMPOSSIBLE" << "\n"; #else cout << "Impossible" << "\n"; #endif if (ex) exit(0); } constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; } const ll infLL = LLONG_MAX / 3; #ifdef int const int inf = infLL; #else const int inf = INT_MAX / 2 - 100; #endif int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int botbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); } int popcount(signed t) { return __builtin_popcount(t); } int popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(int i) { return i && (i & -i) == i; } ll mask(int i) { return (ll(1) << i) - 1; } bool inc(int a, int b, int c) { return a <= b && b <= c; } template <class t> void mkuni(vc<t> &v) { sort(all(v)); v.erase(unique(all(v)), v.ed); } ll rand_int(ll l, ll r) { //[l, r] #ifdef LOCAL static mt19937_64 gen; #else static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); #endif return uniform_int_distribution<ll>(l, r)(gen); } template <class t> void myshuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rand_int(0, i)]); } template <class t> int lwb(const vc<t> &v, const t &a) { return lower_bound(all(v), a) - v.bg; } bool can(int k, vi a, vi pre) { vi u(k); for (auto v : pre) u[v] = 1; vc<pi> z; rep(i, k) if (!u[i]) z.eb(a[i], i); sort(all(z), greater<pi>()); for (auto w : z) { if (--a[w.b] < 0) return false; pre.pb(w.b); } vi mn(k + 1), mx(k + 1); mn[0] = *min_element(all(a)); rep(i, k) { mn[i + 1] = min(mn[i], --a[pre[i]]); } mx[k] = *max_element(all(a)); per(i, k) { mx[i] = max(mx[i + 1], ++a[pre[i]]); } rep(i, k + 1) if (mn[i] * 2 >= mx[i]) return true; return false; } signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); int k; cin >> k; vi a = readvi(k); int len = accumulate(all(a), 0); vi ans, cur; int ok = 0; rep(_, len) { dmp2(ans, cur, ok); bool found = false; rep(nx, k) if (a[nx]) { int p = find(all(cur), nx) - cur.bg; vi tmp; int waf; if (p == si(cur)) { tmp = cur; waf = ok; } else { if (ok <= p) continue; tmp = vi(cur.bg + p + 1, cur.ed); waf = ok - (p + 1); } tmp.pb(nx); if (si(tmp) == k) waf = k; a[nx]--; bool relka = false; per(j, waf + 1) { if (can(k, a, vi(tmp.bg + j, tmp.ed))) { ans.pb(nx); cur = tmp; ok = waf; found = true; relka = true; break; } } if (relka) break; a[nx]++; } if (!found) { assert(_ == 0); print(-1); return 0; } } assert(si(cur) == k); assert(si(ans) == len); for (auto &v : ans) v++; print(ans); }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll #define rng(i, a, b) for (int i = int(a); i < int(b); i++) #define rep(i, b) rng(i, 0, b) #define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--) #define per(i, b) gnr(i, 0, b) #define pb push_back #define eb emplace_back #define a first #define b second #define bg begin() #define ed end() #define all(x) x.bg, x.ed #define si(x) int(x.size()) #ifdef LOCAL #define dmp(x) cerr << __LINE__ << " " << #x << " " << x << endl #else #define dmp(x) void(0) #endif template <class t, class u> void chmax(t &a, u b) { if (a < b) a = b; } template <class t, class u> void chmin(t &a, u b) { if (b < a) a = b; } template <class t> using vc = vector<t>; template <class t> using vvc = vc<vc<t>>; using pi = pair<int, int>; using vi = vc<int>; template <class t, class u> ostream &operator<<(ostream &os, const pair<t, u> &p) { return os << "{" << p.a << "," << p.b << "}"; } template <class t> ostream &operator<<(ostream &os, const vc<t> &v) { os << "{"; for (auto e : v) os << e << ","; return os << "}"; } #define mp make_pair #define mt make_tuple #define one(x) memset(x, -1, sizeof(x)) #define zero(x) memset(x, 0, sizeof(x)) #ifdef LOCAL void dmpr(ostream &os) { os << endl; } template <class T, class... Args> void dmpr(ostream &os, const T &t, const Args &...args) { os << t << " "; dmpr(os, args...); } #define dmp2(...) dmpr(cerr, __LINE__, ##__VA_ARGS__) #else #define dmp2(...) void(0) #endif using uint = unsigned; using ull = unsigned long long; template <class t, size_t n> ostream &operator<<(ostream &os, const array<t, n> &a) { return os << vc<t>(all(a)); } template <int i, class T> void print_tuple(ostream &, const T &) {} template <int i, class T, class H, class... Args> void print_tuple(ostream &os, const T &t) { if (i) os << ","; os << get<i>(t); print_tuple<i + 1, T, Args...>(os, t); } template <class... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { os << "{"; print_tuple<0, tuple<Args...>, Args...>(os, t); return os << "}"; } template <class t> void print(t x, int suc = 1) { cout << x; if (suc == 1) cout << "\n"; if (suc == 2) cout << " "; } ll read() { ll i; cin >> i; return i; } vi readvi(int n, int off = 0) { vi v(n); rep(i, n) v[i] = read() + off; return v; } template <class T> void print(const vector<T> &v, int suc = 1) { rep(i, v.size()) print(v[i], i == int(v.size()) - 1 ? suc : 2); } string readString() { string s; cin >> s; return s; } template <class T> T sq(const T &t) { return t * t; } // #define CAPITAL void yes(bool ex = true) { #ifdef CAPITAL cout << "YES" << "\n"; #else cout << "Yes" << "\n"; #endif if (ex) exit(0); } void no(bool ex = true) { #ifdef CAPITAL cout << "NO" << "\n"; #else cout << "No" << "\n"; #endif if (ex) exit(0); } void possible(bool ex = true) { #ifdef CAPITAL cout << "POSSIBLE" << "\n"; #else cout << "Possible" << "\n"; #endif if (ex) exit(0); } void impossible(bool ex = true) { #ifdef CAPITAL cout << "IMPOSSIBLE" << "\n"; #else cout << "Impossible" << "\n"; #endif if (ex) exit(0); } constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; } const ll infLL = LLONG_MAX / 3; #ifdef int const int inf = infLL; #else const int inf = INT_MAX / 2 - 100; #endif int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int botbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); } int popcount(signed t) { return __builtin_popcount(t); } int popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(int i) { return i && (i & -i) == i; } ll mask(int i) { return (ll(1) << i) - 1; } bool inc(int a, int b, int c) { return a <= b && b <= c; } template <class t> void mkuni(vc<t> &v) { sort(all(v)); v.erase(unique(all(v)), v.ed); } ll rand_int(ll l, ll r) { //[l, r] #ifdef LOCAL static mt19937_64 gen; #else static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); #endif return uniform_int_distribution<ll>(l, r)(gen); } template <class t> void myshuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rand_int(0, i)]); } template <class t> int lwb(const vc<t> &v, const t &a) { return lower_bound(all(v), a) - v.bg; } bool can(int k, vi a, vi pre) { vi u(k); for (auto v : pre) u[v] = 1; vc<pi> z; rep(i, k) if (!u[i]) z.eb(a[i], i); sort(all(z), greater<pi>()); for (auto w : z) { if (--a[w.b] < 0) return false; pre.pb(w.b); } vi mn(k + 1), mx(k + 1); mn[0] = *min_element(all(a)); rep(i, k) { mn[i + 1] = min(mn[i], --a[pre[i]]); } mx[k] = *max_element(all(a)); per(i, k) { mx[i] = max(mx[i + 1], ++a[pre[i]]); } rep(i, k + 1) if (mn[i] * 2 >= mx[i]) return true; return false; } signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); int k; cin >> k; vi a = readvi(k); int len = accumulate(all(a), 0); vi ans, cur; int ok = 0; rep(_, len) { dmp2(ans, cur, ok); bool found = false; rep(nx, k) if (a[nx]) { int p = find(all(cur), nx) - cur.bg; vi tmp; int waf; if (p == si(cur)) { tmp = cur; waf = ok; } else { if (ok <= p) continue; tmp = vi(cur.bg + p + 1, cur.ed); waf = ok - (p + 1); } tmp.pb(nx); if (si(tmp) == k) waf = k; a[nx]--; bool relka = false; rep(j, waf + 1) { if (inc(1, j, waf - 1)) continue; if (can(k, a, vi(tmp.bg + j, tmp.ed))) { ans.pb(nx); cur = tmp; ok = waf; found = true; relka = true; break; } } if (relka) break; a[nx]++; } if (!found) { assert(_ == 0); print(-1); return 0; } } assert(si(cur) == k); assert(si(ans) == len); for (auto &v : ans) v++; print(ans); }
replace
267
268
267
270
TLE
p02637
C++
Runtime Error
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define MOD2 998244353 #define PRIM 3 #define INF (1 << 29) #define LINF (1LL << 60) #define EPS (1e-10) #define PI 3.1415926535897932384626433832795028 typedef long long Int; typedef pair<Int, Int> P; typedef long double Real; typedef complex<Real> CP; Int n; vector<Int> a; vector<Int> ans; Int mx, mn; set<int> maxs, mins; bool ok[1100]; Int min_initial(Int from, Int to) { auto aa = a; vector<int> used(n, 0); for (int i = from; i < to; i++) { if (used[ans[i]]) return n; used[ans[i]] = 1; aa[ans[i]]++; } Int tmpmx = *max_element(aa.begin(), aa.end()); Int tmpmn = *min_element(aa.begin(), aa.end()); if (tmpmn * 2 < tmpmx) return n; if (tmpmn * 2 > tmpmx) { for (int i = 0; i < n; i++) if (!used[i]) return i; return n; } Int max_cnt = 0; for (int i = 0; i < n; i++) { if (aa[i] == tmpmx) max_cnt++; } for (int i = from; i < to; i++) { if (aa[ans[i]] == tmpmx) max_cnt--; if (aa[ans[i]] == tmpmn) { if (max_cnt != 0) return n; } } for (int i = 0; i < n; i++) { if (aa[i] == 0) continue; if (used[i]) continue; if (max_cnt == 0 && !used[i]) return i; if (max_cnt > 0 && aa[i] != tmpmn && !used[i]) return i; } return ans[from]; } int main() { Int as = 0; cin >> n; a.resize(n); for (int i = 0; i < n; i++) { cin >> a[i]; as += a[i]; } Int mx = *max_element(a.begin(), a.end()); Int mn = *max_element(a.begin(), a.end()); if (mn * 2 < mx) { cout << -1 << endl; return 0; } for (int i = 0; i < as; i++) { Int nxt = n; for (int j = max(0ll, i - n + 1); j <= i; j++) { nxt = min(nxt, min_initial(j, i)); if (!ok[j]) break; } a[nxt]--; ans.push_back(nxt); if (ans.size() >= n) { set<int> hoge; for (int j = 0; j < n; j++) { hoge.insert(ans[ans.size() - 1 - j]); } if (hoge.size() == n) { for (int j = 0; j < n; j++) ok[ans.size() - 1 - j] = true; } } } for (auto elem : ans) cout << elem + 1 << " "; cout << endl; return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define MOD2 998244353 #define PRIM 3 #define INF (1 << 29) #define LINF (1LL << 60) #define EPS (1e-10) #define PI 3.1415926535897932384626433832795028 typedef long long Int; typedef pair<Int, Int> P; typedef long double Real; typedef complex<Real> CP; Int n; vector<Int> a; vector<Int> ans; Int mx, mn; set<int> maxs, mins; bool ok[1100]; Int min_initial(Int from, Int to) { auto aa = a; vector<int> used(n, 0); for (int i = from; i < to; i++) { if (used[ans[i]]) return n; used[ans[i]] = 1; aa[ans[i]]++; } Int tmpmx = *max_element(aa.begin(), aa.end()); Int tmpmn = *min_element(aa.begin(), aa.end()); if (tmpmn * 2 < tmpmx) return n; if (tmpmn * 2 > tmpmx) { for (int i = 0; i < n; i++) if (!used[i]) return i; return n; } Int max_cnt = 0; for (int i = 0; i < n; i++) { if (aa[i] == tmpmx) max_cnt++; } for (int i = from; i < to; i++) { if (aa[ans[i]] == tmpmx) max_cnt--; if (aa[ans[i]] == tmpmn) { if (max_cnt != 0) return n; } } for (int i = 0; i < n; i++) { if (aa[i] == 0) continue; if (used[i]) continue; if (max_cnt == 0 && !used[i]) return i; if (max_cnt > 0 && aa[i] != tmpmn && !used[i]) return i; } return ans[from]; } int main() { Int as = 0; cin >> n; a.resize(n); for (int i = 0; i < n; i++) { cin >> a[i]; as += a[i]; } Int mx = *max_element(a.begin(), a.end()); Int mn = *min_element(a.begin(), a.end()); if (mn * 2 < mx) { cout << -1 << endl; return 0; } for (int i = 0; i < as; i++) { Int nxt = n; for (int j = max(0ll, i - n + 1); j <= i; j++) { nxt = min(nxt, min_initial(j, i)); if (!ok[j]) break; } a[nxt]--; ans.push_back(nxt); if (ans.size() >= n) { set<int> hoge; for (int j = 0; j < n; j++) { hoge.insert(ans[ans.size() - 1 - j]); } if (hoge.size() == n) { for (int j = 0; j < n; j++) ok[ans.size() - 1 - j] = true; } } } for (auto elem : ans) cout << elem + 1 << " "; cout << endl; return 0; }
replace
90
92
90
91
0
p02637
C++
Runtime Error
#include <bits/stdc++.h> #define debug(x) cerr << #x << " " << (x) << endl using namespace std; const int N = 1005, K = 105; template <class T> void read(T &x) { int sgn = 1; char ch; x = 0; for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ; if (ch == '-') ch = getchar(), sgn = -1; for (; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; x *= sgn; } template <class T> void write(T x) { if (x < 0) putchar('-'), write(-x); else if (x < 10) putchar(x + '0'); else write(x / 10), putchar(x % 10 + '0'); } int n = 0, k, a[K], b[N]; bool tag[K], valid[N]; // validity check // last denotes the largest index i such that i <= pre and valid[i] = false. // len denotes the largest length of suffix such that b[pre], b[pre - 1], ..., // b[pre - len + 1] are distinct. bool pre_valid[N]; bool solve1(int pre, int last, int len, int num) { for (int i = 1; i <= k; i++) { if (!tag[i] && !(a[i] - 1 <= num && num <= (a[i] << 1) - 1)) return false; } pre_valid[pre - len] = true; for (int i = pre - len + 1; i <= pre; i++) { pre_valid[i] = pre_valid[i - 1]; if (!(a[b[i]] - 1 <= num && num <= (a[b[i]] << 1) - 1)) pre_valid[i] = false; } // if (pre == 6 && b[pre] == 1) debug(pre), debug(last), debug(len), // debug(num); for (int i = pre; i > pre - len; i--) { if (!(a[b[i]] <= num && num <= (a[b[i]] << 1))) break; if (i <= last && pre_valid[i - 1]) return true; } return false; } bool solve2(int pre, int last, int len, int num) { // if (pre == 6 && b[pre] == 1) debug(pre), debug(last), debug(len), // debug(num); for (int i = 1; i <= k; i++) { if (!tag[i] && !(a[i] <= num && num <= (a[i] << 1) - 1)) return false; } pre_valid[pre - len] = true; for (int i = pre - len + 1; i <= pre; i++) { pre_valid[i] = pre_valid[i - 1]; if (!(a[b[i]] <= num && num <= (a[b[i]] << 1) - 1)) pre_valid[i] = false; } int pos1 = 0, pos2 = n + 1; for (int i = pre; i > pre - len; i--) { if (a[b[i]] <= num && num <= (a[b[i]] << 1 | 1)) { if (num == a[b[i]]) { if (!pos1) pos1 = i; } if (num == (a[b[i]] << 1 | 1)) pos2 = i; } else break; if (i <= last && pos1 <= pos2 && pre_valid[i - 1]) return true; } return false; } bool solve(int pre, int last, int len, int num) { return solve1(pre, last, len, num) || solve2(pre, last, len, num); } bool check(int pre) { for (int i = 1; i <= k; i++) tag[i] = false; int len = min(pre, k); for (int i = pre; i && i > pre - k; i--) { if (tag[b[i]]) { len = pre - i; break; } tag[b[i]] = true; } int last = pre + 1; if (len < k) { for (int i = pre; i && i > pre - k; i--) { if (!valid[i]) last = i; } } int mx = 0; for (int i = 1; i <= k; i++) mx = max(mx, a[i] - 1); for (int i = 1; i <= k; i++) tag[i] = false; for (int i = pre; i > pre - len; i--) tag[b[i]] = true; // if (pre == 6) debug(len); return solve(pre, last, len, mx) || solve(pre, last, len, mx + 1); } int main() { read(k); for (int i = 1; i <= k; i++) read(a[i]), n += a[i]; bool flag = true; for (int i = 1; i <= n; i++) { bool chk = false; valid[i] = false; for (int j = 1; j <= k; j++) { b[i] = j, a[j]--, valid[i] = false; if (check(i)) { chk = true; break; } else a[j]++; } bool k_distinct = i >= k; for (int j = 1; j <= k; j++) tag[j] = false; for (int j = i; j > i - k; j--) { if (tag[b[j]]) k_distinct = false; tag[b[j]] = true; } if (k_distinct) { for (int j = i; j > i - k; j--) valid[j] = true; } if (!chk) { flag = false; break; } } // for (int i = 1; i <= n; i++) debug(i), debug(b[i]); if (flag) { for (int i = 1; i <= n; i++) write(b[i]), putchar(' '); putchar('\n'); } else puts("-1"); return 0; }
#include <bits/stdc++.h> #define debug(x) cerr << #x << " " << (x) << endl using namespace std; const int N = 10005, K = 10005; template <class T> void read(T &x) { int sgn = 1; char ch; x = 0; for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ; if (ch == '-') ch = getchar(), sgn = -1; for (; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; x *= sgn; } template <class T> void write(T x) { if (x < 0) putchar('-'), write(-x); else if (x < 10) putchar(x + '0'); else write(x / 10), putchar(x % 10 + '0'); } int n = 0, k, a[K], b[N]; bool tag[K], valid[N]; // validity check // last denotes the largest index i such that i <= pre and valid[i] = false. // len denotes the largest length of suffix such that b[pre], b[pre - 1], ..., // b[pre - len + 1] are distinct. bool pre_valid[N]; bool solve1(int pre, int last, int len, int num) { for (int i = 1; i <= k; i++) { if (!tag[i] && !(a[i] - 1 <= num && num <= (a[i] << 1) - 1)) return false; } pre_valid[pre - len] = true; for (int i = pre - len + 1; i <= pre; i++) { pre_valid[i] = pre_valid[i - 1]; if (!(a[b[i]] - 1 <= num && num <= (a[b[i]] << 1) - 1)) pre_valid[i] = false; } // if (pre == 6 && b[pre] == 1) debug(pre), debug(last), debug(len), // debug(num); for (int i = pre; i > pre - len; i--) { if (!(a[b[i]] <= num && num <= (a[b[i]] << 1))) break; if (i <= last && pre_valid[i - 1]) return true; } return false; } bool solve2(int pre, int last, int len, int num) { // if (pre == 6 && b[pre] == 1) debug(pre), debug(last), debug(len), // debug(num); for (int i = 1; i <= k; i++) { if (!tag[i] && !(a[i] <= num && num <= (a[i] << 1) - 1)) return false; } pre_valid[pre - len] = true; for (int i = pre - len + 1; i <= pre; i++) { pre_valid[i] = pre_valid[i - 1]; if (!(a[b[i]] <= num && num <= (a[b[i]] << 1) - 1)) pre_valid[i] = false; } int pos1 = 0, pos2 = n + 1; for (int i = pre; i > pre - len; i--) { if (a[b[i]] <= num && num <= (a[b[i]] << 1 | 1)) { if (num == a[b[i]]) { if (!pos1) pos1 = i; } if (num == (a[b[i]] << 1 | 1)) pos2 = i; } else break; if (i <= last && pos1 <= pos2 && pre_valid[i - 1]) return true; } return false; } bool solve(int pre, int last, int len, int num) { return solve1(pre, last, len, num) || solve2(pre, last, len, num); } bool check(int pre) { for (int i = 1; i <= k; i++) tag[i] = false; int len = min(pre, k); for (int i = pre; i && i > pre - k; i--) { if (tag[b[i]]) { len = pre - i; break; } tag[b[i]] = true; } int last = pre + 1; if (len < k) { for (int i = pre; i && i > pre - k; i--) { if (!valid[i]) last = i; } } int mx = 0; for (int i = 1; i <= k; i++) mx = max(mx, a[i] - 1); for (int i = 1; i <= k; i++) tag[i] = false; for (int i = pre; i > pre - len; i--) tag[b[i]] = true; // if (pre == 6) debug(len); return solve(pre, last, len, mx) || solve(pre, last, len, mx + 1); } int main() { read(k); for (int i = 1; i <= k; i++) read(a[i]), n += a[i]; bool flag = true; for (int i = 1; i <= n; i++) { bool chk = false; valid[i] = false; for (int j = 1; j <= k; j++) { b[i] = j, a[j]--, valid[i] = false; if (check(i)) { chk = true; break; } else a[j]++; } bool k_distinct = i >= k; for (int j = 1; j <= k; j++) tag[j] = false; for (int j = i; j > i - k; j--) { if (tag[b[j]]) k_distinct = false; tag[b[j]] = true; } if (k_distinct) { for (int j = i; j > i - k; j--) valid[j] = true; } if (!chk) { flag = false; break; } } // for (int i = 1; i <= n; i++) debug(i), debug(b[i]); if (flag) { for (int i = 1; i <= n; i++) write(b[i]), putchar(' '); putchar('\n'); } else puts("-1"); return 0; }
replace
4
5
4
5
0
p02637
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define rrep(i, a, b) for (int i = (b); i-- > (a);) #define all(v) (v).begin(), (v).end() #define trav(x, v) for (auto &x : v) #define sz(v) int(v.size()) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); int k; cin >> k; vi a(k); trav(x, a) cin >> x; int mn = 1000, mx = 1; trav(x, a) mn = min(mn, x); trav(x, a) mx = max(mx, x); if (mn * 2 < mx) { cout << -1 << endl; return 0; } int len = 0; trav(x, a) len += x; vi ans(len, -1); vi cnt(k, 0); vector<bool> happy(k, true); vi prev(k, -1); rep(i, 0, len) { mx = 0, mn = 1000; trav(x, a) mx = max(mx, x); trav(x, a) mn = min(mn, x); int imx = 1000; rep(d, 0, k) if (a[d] == mn) imx = min(imx, prev[d]); rep(d, 0, k) if (happy[d] && a[d] * 2 > mx && !(mx = 2 * mn + 1 && a[d] < mx && prev[d] > imx)) { ans[i] = d; break; } if (ans[i] < 0) { rep(j, 0, i) cout << 1 + ans[j] << " "; cout << endl; trav(x, a) cout << x << " "; cout << endl; cout << mx << " " << mn << endl; cout << a[ans[i]] << endl; cout << imx << " " << prev[ans[i]] << endl; assert(false); } prev[ans[i]] = i; happy[ans[i]] = false; --a[ans[i]]; ++cnt[ans[i]]; if (i >= k) --cnt[ans[i - k]]; if (count(all(cnt), 0) == 0) { rep(d, 0, k) happy[d] = true; } } trav(x, ans) cout << x + 1 << " "; cout << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define rrep(i, a, b) for (int i = (b); i-- > (a);) #define all(v) (v).begin(), (v).end() #define trav(x, v) for (auto &x : v) #define sz(v) int(v.size()) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); int k; cin >> k; vi a(k); trav(x, a) cin >> x; int mn = 1000, mx = 1; trav(x, a) mn = min(mn, x); trav(x, a) mx = max(mx, x); if (mn * 2 < mx) { cout << -1 << endl; return 0; } int len = 0; trav(x, a) len += x; vi ans(len, -1); vi cnt(k, 0); vector<bool> happy(k, true); vi prev(k, -1); rep(i, 0, len) { mx = 0, mn = 1000; trav(x, a) mx = max(mx, x); trav(x, a) mn = min(mn, x); int imx = 1000; rep(d, 0, k) if (a[d] == mn) imx = min(imx, prev[d]); rep(d, 0, k) if (happy[d] && a[d] * 2 > mx && !(mx >= 2 * mn + 1 && prev[d] > imx)) { ans[i] = d; break; } if (ans[i] < 0) { rep(j, 0, i) cout << 1 + ans[j] << " "; cout << endl; trav(x, a) cout << x << " "; cout << endl; cout << mx << " " << mn << endl; cout << a[ans[i]] << endl; cout << imx << " " << prev[ans[i]] << endl; assert(false); } prev[ans[i]] = i; happy[ans[i]] = false; --a[ans[i]]; ++cnt[ans[i]]; if (i >= k) --cnt[ans[i - k]]; if (count(all(cnt), 0) == 0) { rep(d, 0, k) happy[d] = true; } } trav(x, ans) cout << x + 1 << " "; cout << endl; }
replace
45
46
45
46
0
p02638
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int MOD; inline int add(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + MOD : a; } inline int mul(int a, int b) { return 1ll * a * b % MOD; } int ksm(int a, int b) { int ans = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) ans = mul(ans, a); return ans; } const int Q = 55; int f[Q][Q]; int fac[Q + 1], ifac[Q + 1]; int G(int n, int m, int k) { if (n > k || m > k) return 0; memset(f, 0, sizeof(f)); for (int j = 0; j <= min(m - 1, k - n); j++) f[1][j] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j <= m; j++) { if (!f[i][j]) continue; int r = min(i + k - n, m), l = max(j, m - k + i); if (l <= r) f[i + 1][l] = add(f[i + 1][l], f[i][j]), f[i + 1][r + 1] = sub(f[i + 1][r + 1], f[i][j]); } for (int j = 1; j <= m; j++) f[i + 1][j] = add(f[i + 1][j], f[i + 1][j - 1]); } int als = 0; for (int j = 0; j <= m; j++) als = add(als, f[n][j]); return mul(als, fac[n + m]); } int main() { int n, kk; scanf("%d%d%d", &n, &kk, &MOD); fac[0] = 1; for (int i = 1; i <= Q; i++) fac[i] = mul(fac[i - 1], i); ifac[Q] = ksm(fac[Q], MOD - 2); for (int i = Q; i; --i) ifac[i - 1] = mul(ifac[i], i); int als = mul(kk == n - 1, fac[n]); for (int d = 0; d < kk; d++) for (int i = 1; i <= n - d - 2; i++) als = add(als, mul(mul(fac[n], ifac[n - d]), G(i, n - d - i - 1, kk - d))); printf("%d\n", als); return 0; }
#include <bits/stdc++.h> using namespace std; int MOD; inline int add(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + MOD : a; } inline int mul(int a, int b) { return 1ll * a * b % MOD; } int ksm(int a, int b) { int ans = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) ans = mul(ans, a); return ans; } const int Q = 255; int f[Q][Q]; int fac[Q + 1], ifac[Q + 1]; int G(int n, int m, int k) { if (n > k || m > k) return 0; memset(f, 0, sizeof(f)); for (int j = 0; j <= min(m - 1, k - n); j++) f[1][j] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j <= m; j++) { if (!f[i][j]) continue; int r = min(i + k - n, m), l = max(j, m - k + i); if (l <= r) f[i + 1][l] = add(f[i + 1][l], f[i][j]), f[i + 1][r + 1] = sub(f[i + 1][r + 1], f[i][j]); } for (int j = 1; j <= m; j++) f[i + 1][j] = add(f[i + 1][j], f[i + 1][j - 1]); } int als = 0; for (int j = 0; j <= m; j++) als = add(als, f[n][j]); return mul(als, fac[n + m]); } int main() { int n, kk; scanf("%d%d%d", &n, &kk, &MOD); fac[0] = 1; for (int i = 1; i <= Q; i++) fac[i] = mul(fac[i - 1], i); ifac[Q] = ksm(fac[Q], MOD - 2); for (int i = Q; i; --i) ifac[i - 1] = mul(ifac[i], i); int als = mul(kk == n - 1, fac[n]); for (int d = 0; d < kk; d++) for (int i = 1; i <= n - d - 2; i++) als = add(als, mul(mul(fac[n], ifac[n - d]), G(i, n - d - i - 1, kk - d))); printf("%d\n", als); return 0; }
replace
19
20
19
20
0
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { int a[5]; for (int i = 0; i < 5; i++) cin >> a[i]; for (int i = 0; i < 5; i++) { if (a[i] == 0) return i + 1; } }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { int a[5]; for (int i = 0; i < 5; i++) cin >> a[i]; for (int i = 0; i < 5; i++) { if (a[i] == 0) { cout << i + 1; break; } } }
replace
10
12
10
14
1
p02639
C++
Runtime Error
#include <iostream> using namespace std; int main() { int x[5]; for (int i = 1; i <= 5; ++i) cin >> x[i]; for (int i = 1; i <= 5; ++i) { if (!x[i]) cout << i << '\n'; } return 0; }
#include <iostream> using namespace std; int main() { int x[6]; for (int i = 1; i <= 5; ++i) cin >> x[i]; for (int i = 1; i <= 5; ++i) { if (!x[i]) cout << i << '\n'; } return 0; }
replace
5
6
5
6
0
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int ara[5], i, c = 0; for (i = 1; i <= 5; i++) { cin >> ara[i]; } for (i = 1; i <= 5; i++) { if (ara[i] == 0) cout << i << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int x1, x2, x3, x4, x5; cin >> x1 >> x2 >> x3 >> x4 >> x5; if (x1 == 0) cout << 1 << endl; else if (x2 == 0) cout << 2 << endl; else if (x3 == 0) cout << 3 << endl; else if (x4 == 0) cout << 4 << endl; else if (x5 == 0) cout << 5 << endl; }
replace
3
12
3
16
0
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { vector<int> result; for (int i = 0; i < 5; i++) { cin >> result.at(i); if (result.at(i) == 0) { cout << i + 1 << endl; break; } } }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> result(5); for (int i = 0; i < 5; i++) { cin >> result.at(i); if (result.at(i) == 0) { cout << i + 1 << endl; break; } } }
replace
3
4
3
4
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int a[5]; for (int i = 1; i <= 5; i++) { cin >> a[i]; if (a[i] == 0) cout << i; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a[6]; for (int i = 1; i <= 5; i++) { cin >> a[i]; if (a[i] == 0) cout << i; } return 0; }
replace
4
5
4
5
0
p02639
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { while (true) { int num; for (int i = 1; i <= 5; i++) { cin >> num; if (num == 0) cout << i << endl; } } }
#include <iostream> using namespace std; int main() { int var[5]; while (cin >> var[0] >> var[1] >> var[2] >> var[3] >> var[4]) { for (int i = 0; i < 5; i++) { if (var[i] == 0) cout << i + 1 << endl; } } }
replace
4
10
4
9
TLE
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int v, ans = -1; for (int i = 0; i < 5; i++) { cin >> v; if (v == 0) { ans = i; } } return ans; }
#include <bits/stdc++.h> using namespace std; int main() { int v, ans = -1; for (int i = 0; i < 5; i++) { cin >> v; if (v == 0) { ans = i; } } cout << ans + 1; return 0; }
replace
12
13
12
14
0
p02639
C++
Runtime Error
// #include <bits/stdc++.h> #include <algorithm> #include <cmath> #include <iomanip> #include <ios> #include <iostream> #include <map> #include <numeric> #include <queue> #include <stack> // #include <windows.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 T1, class T2> using P = pair<T1, T2>; using I = int; using D = double; using B = bool; using C = char; using S = string; using LL = long long; using LD = long double; using ULL = unsigned long long; using PII = P<I, I>; using VPII = V<PII>; using PLL = P<LL, LL>; using VPLL = V<PLL>; using VI = V<I>; using VVI = VV<I>; using VLL = V<LL>; using VVLL = VV<LL>; using VC = V<C>; using VVC = VV<C>; using VS = V<S>; using VVS = VV<S>; using VB = V<B>; using VVB = VV<B>; #define REP(type, i, n) for (type i = 0; i < (type)(n); ++i) #define REP2(type, i, m, n) for (type i = (m); i < (type)(n); ++i) #define REPR(type, i, n) for (type i = (n)-1; i >= 0; --i) #define REPR2(type, i, m, n) for (type i = (n)-1; i >= (m); --i) #define REPx(x, a) for (auto x : a) #define ALL(a) a.begin(), a.end() #define SORT(a) sort(ALL(a)) #define SORTR(a, type) sort(ALL(a), G<type>()) #define REVERSE(a) reverse(ALL(a)) #define SIZE(a, type) ((type)(a).size()) #define bit_search(bit, n) REP(LL, bit, 1LL << (n)) #define bit_check(bit, i) ((bit >> (i)) & 1) #define setpre(n) fixed << setprecision((n)) #define UNIQUE(a) \ do { \ SORT(a); \ (a).erase(unique(ALL(a)), (a).end()); \ } while (0) #define MAX(a) *max_element(ALL(a)) #define MIN(a) *min_element(ALL(a)) #define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin() #define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin() #define INPUT(a) REPx(&x, a) cin >> x; #define INPUTP(a) REPx(&x, a) cin >> x.first >> x.second; #define OUTPUT_PERMUTATION(n) \ do { \ VI v(n); \ iota(ALL(v), 1); \ do { \ REPx(x, v) cout << x << " "; \ ENDL \ } while (next_permutation(ALL(v))); \ } while (0); #define MAKE_PERMUTATION(n, PER) \ do { \ VVI a(fact(n), VI(n)); \ int idx = 0; \ VI v(n); \ iota(ALL(v), 1); \ do { \ REP(roop, n) a[idx][roop] = v[roop]; \ idx++; \ } while (next_permutation(ALL(v))); \ PER = a; \ } while (0); // int fact(), VVI PERを宣言しておく、n=10が限界(500 ms) #define ENDL cout << endl; const int INF = 2e9; const LL MOD = 1e9 + 7; template <class T> using PRIORITY_QUEUE = priority_queue<T, V<T>, greater<T>>; 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; } template <class T> inline void debug1(V<T> A) { REP(int, i, SIZE(A, int)) { if (A[i] == INF) cout << "I "; else cout << A[i] << " "; } ENDL } template <class T> inline void debug2(VV<T> A) { REP(int, i, SIZE(A, int)) { REP(int, j, SIZE(A[i], int)) { if (A[i][j] == INF) cout << "I "; else cout << A[i][j] << " "; } ENDL } } int main() { cin.tie(0); ios::sync_with_stdio(false); VI x; INPUT(x); REP(int, i, 5) { if (x[i] == 0) { cout << i + 1 << endl; break; } } return 0; }
// #include <bits/stdc++.h> #include <algorithm> #include <cmath> #include <iomanip> #include <ios> #include <iostream> #include <map> #include <numeric> #include <queue> #include <stack> // #include <windows.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 T1, class T2> using P = pair<T1, T2>; using I = int; using D = double; using B = bool; using C = char; using S = string; using LL = long long; using LD = long double; using ULL = unsigned long long; using PII = P<I, I>; using VPII = V<PII>; using PLL = P<LL, LL>; using VPLL = V<PLL>; using VI = V<I>; using VVI = VV<I>; using VLL = V<LL>; using VVLL = VV<LL>; using VC = V<C>; using VVC = VV<C>; using VS = V<S>; using VVS = VV<S>; using VB = V<B>; using VVB = VV<B>; #define REP(type, i, n) for (type i = 0; i < (type)(n); ++i) #define REP2(type, i, m, n) for (type i = (m); i < (type)(n); ++i) #define REPR(type, i, n) for (type i = (n)-1; i >= 0; --i) #define REPR2(type, i, m, n) for (type i = (n)-1; i >= (m); --i) #define REPx(x, a) for (auto x : a) #define ALL(a) a.begin(), a.end() #define SORT(a) sort(ALL(a)) #define SORTR(a, type) sort(ALL(a), G<type>()) #define REVERSE(a) reverse(ALL(a)) #define SIZE(a, type) ((type)(a).size()) #define bit_search(bit, n) REP(LL, bit, 1LL << (n)) #define bit_check(bit, i) ((bit >> (i)) & 1) #define setpre(n) fixed << setprecision((n)) #define UNIQUE(a) \ do { \ SORT(a); \ (a).erase(unique(ALL(a)), (a).end()); \ } while (0) #define MAX(a) *max_element(ALL(a)) #define MIN(a) *min_element(ALL(a)) #define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin() #define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin() #define INPUT(a) REPx(&x, a) cin >> x; #define INPUTP(a) REPx(&x, a) cin >> x.first >> x.second; #define OUTPUT_PERMUTATION(n) \ do { \ VI v(n); \ iota(ALL(v), 1); \ do { \ REPx(x, v) cout << x << " "; \ ENDL \ } while (next_permutation(ALL(v))); \ } while (0); #define MAKE_PERMUTATION(n, PER) \ do { \ VVI a(fact(n), VI(n)); \ int idx = 0; \ VI v(n); \ iota(ALL(v), 1); \ do { \ REP(roop, n) a[idx][roop] = v[roop]; \ idx++; \ } while (next_permutation(ALL(v))); \ PER = a; \ } while (0); // int fact(), VVI PERを宣言しておく、n=10が限界(500 ms) #define ENDL cout << endl; const int INF = 2e9; const LL MOD = 1e9 + 7; template <class T> using PRIORITY_QUEUE = priority_queue<T, V<T>, greater<T>>; 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; } template <class T> inline void debug1(V<T> A) { REP(int, i, SIZE(A, int)) { if (A[i] == INF) cout << "I "; else cout << A[i] << " "; } ENDL } template <class T> inline void debug2(VV<T> A) { REP(int, i, SIZE(A, int)) { REP(int, j, SIZE(A[i], int)) { if (A[i][j] == INF) cout << "I "; else cout << A[i][j] << " "; } ENDL } } int main() { cin.tie(0); ios::sync_with_stdio(false); VI x(5); INPUT(x); REP(int, i, 5) { if (x[i] == 0) { cout << i + 1 << endl; break; } } return 0; }
replace
127
128
127
128
-11
p02639
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int a[5]; int pri; for (int i = 1; i <= 5; i++) { cin >> a[i]; if (a[i] == 0) pri = i; } cout << pri << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a[10]; int pri; for (int i = 1; i <= 5; i++) { cin >> a[i]; if (a[i] == 0) pri = i; } cout << pri << endl; return 0; }
replace
5
6
5
6
TLE
p02639
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int A[5]; int x = 0; for (int i = 1; i <= 5; i++) { cin >> A[i]; if (A[i] == 0) x = i; } cout << x; return 0; }
#include <iostream> using namespace std; int main() { int A[5]; int x = 0; for (int i = 1; i <= 5; i++) { cin >> A[i]; } for (int i = 1; i <= 5; i++) { if (A[i] == 0) x = i; } cout << x; return 0; }
insert
8
8
8
10
TLE
p02639
C++
Runtime Error
#pragma GCC optimize("Ofast") #include "bits/stdc++.h" using namespace std; using ll = long long; using ull = unsigned long long; #define PI 3.141592 #define all(a) (a).begin(), (a).end() int main() { vector<int> A; for (int i = 0; i < 5; i++) cin >> A[i]; for (int i = 0; i < 5; i++) { if (A[i] == 0) { cout << i + 1 << endl; return 0; } } }
#pragma GCC optimize("Ofast") #include "bits/stdc++.h" using namespace std; using ll = long long; using ull = unsigned long long; #define PI 3.141592 #define all(a) (a).begin(), (a).end() int main() { vector<int> A(5); for (int i = 0; i < 5; i++) cin >> A[i]; for (int i = 0; i < 5; i++) { if (A[i] == 0) { cout << i + 1 << endl; return 0; } } }
replace
9
10
9
10
-11
p02639
Python
Runtime Error
import sys line = sys.stdin.readlines()[0].rstrip("\n") print(line.split(" ").index("0", 1))
import sys line = sys.stdin.readlines()[0].rstrip("\n") nums = line.split(" ") print(nums.index("0") + 1)
replace
3
4
3
5
ValueError: '0' is not in list
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02639/Python/s359632708.py", line 3, in <module> print(line.split(" ").index("0", 1)) ValueError: '0' is not in list
p02639
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <math.h> #include <string> #define pi 3.1415926535897932384626 #include <cmath> #define test(t) while (t--) #define ll long long int #define in cin >> #define out cout << #define en "\n" #define sp " " #define pb push_back #define fast() \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define double long double using namespace std; ll form(ll n) { return (n * (n + 1)) / 2; } ll exponent(ll base, ll power) { ll res = 1; ll m = 1000000007; while (power) { if (power & 1) res = (res * base) % m; base = (base * base) % m; power = power >> 1; } return res; } bool count_one(ll n) { ll cnt = 0; while (n > 0) { cnt += 1; n = n & (n - 1); } if (cnt > 1) return true; return false; } void solve() { ll a[5]; ll ans; for (ll i = 1; i <= 5; i++) { in a[i]; if (a[i] == 0) ans = i; } out ans << en; } int main() { fast() // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ll t = 1; // cin>>t; test(t) { solve(); } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <math.h> #include <string> #define pi 3.1415926535897932384626 #include <cmath> #define test(t) while (t--) #define ll long long int #define in cin >> #define out cout << #define en "\n" #define sp " " #define pb push_back #define fast() \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define double long double using namespace std; ll form(ll n) { return (n * (n + 1)) / 2; } ll exponent(ll base, ll power) { ll res = 1; ll m = 1000000007; while (power) { if (power & 1) res = (res * base) % m; base = (base * base) % m; power = power >> 1; } return res; } bool count_one(ll n) { ll cnt = 0; while (n > 0) { cnt += 1; n = n & (n - 1); } if (cnt > 1) return true; return false; } void solve() { ll x1, x2, x3, x4, x5; in x1 >> x2 >> x3 >> x4 >> x5; if (x1 == 0) out 1 << en; else if (x2 == 0) out 2 << en; else if (x3 == 0) out 3 << en; else if (x4 == 0) out 4 << en; else out 5 << en; } int main() { fast() // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ll t = 1; // cin>>t; test(t) { solve(); } return 0; }
replace
45
53
45
57
TLE
p02639
C++
Runtime Error
#pragma warning(disable : 4996) #pragma comment(linker, "/STACK:336777216") #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> using namespace std; using namespace __gnu_pbds; typedef long long ll; #define int long long int typedef complex<long double> cd; const long double pi = acos(-1); typedef double db; typedef long double ldb; typedef pair<int, int> pii; typedef pair<db, db> pdd; typedef vector<int> vi; typedef vector<vector<int>> matrix; #define m1 make_pair #define pb push_back #define flush fflush(stdout) #define IOS \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define reset(x, v) memset(x, v, sizeof(x)) #define fi first #define se second #define endl "\n" #define debug(x) (cerr << #x << ": " << x << "\n") #define setbits(x) __builtin_popcount(x) #define setbitsll(x) __builtin_popcountll(x) #define all(x) x.begin(), x.end() #define pii pair<int, int> inline ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } inline ll power(ll a, ll n, ll m) { if (n == 0) return 1; ll p = power(a, n / 2, m); p = (p % m * p % m) % m; if (n % 2) return (p % m * a % m) % m; else return p; } const double EPS = 1e-9; const ll MOD = 998244353; const ll hell = 1000000007; const int INF = 1e18; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); std::mt19937 rng(seed); double startTime; double getCurrentTime() { return ((double)clock() - startTime) / CLOCKS_PER_SEC; } const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; const int N = 200005; void solve() { int a[5]; for (int i = 1; i <= 5; i++) { cin >> a[i]; if (!a[i]) { cout << i << endl; return; } } } int32_t main() { IOS; int t; t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#pragma warning(disable : 4996) #pragma comment(linker, "/STACK:336777216") #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> using namespace std; using namespace __gnu_pbds; typedef long long ll; #define int long long int typedef complex<long double> cd; const long double pi = acos(-1); typedef double db; typedef long double ldb; typedef pair<int, int> pii; typedef pair<db, db> pdd; typedef vector<int> vi; typedef vector<vector<int>> matrix; #define m1 make_pair #define pb push_back #define flush fflush(stdout) #define IOS \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define reset(x, v) memset(x, v, sizeof(x)) #define fi first #define se second #define endl "\n" #define debug(x) (cerr << #x << ": " << x << "\n") #define setbits(x) __builtin_popcount(x) #define setbitsll(x) __builtin_popcountll(x) #define all(x) x.begin(), x.end() #define pii pair<int, int> inline ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } inline ll power(ll a, ll n, ll m) { if (n == 0) return 1; ll p = power(a, n / 2, m); p = (p % m * p % m) % m; if (n % 2) return (p % m * a % m) % m; else return p; } const double EPS = 1e-9; const ll MOD = 998244353; const ll hell = 1000000007; const int INF = 1e18; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); std::mt19937 rng(seed); double startTime; double getCurrentTime() { return ((double)clock() - startTime) / CLOCKS_PER_SEC; } const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; const int N = 200005; void solve() { int a[6]; for (int i = 1; i <= 5; i++) { cin >> a[i]; if (!a[i]) { cout << i << endl; return; } } } int32_t main() { IOS; int t; t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
79
80
79
80
0
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ar array void solve() {} int main() { ios::sync_with_stdio(0); cin.tie(0); int x[5]; cin >> x[0] >> x[1] >> x[2] >> x[3] >> x[4]; for (int i = 0; i < 5; i++) { if (x[i] == 0) { cout << i + 1 << "\n"; return 1; } } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ar array void solve() {} int main() { ios::sync_with_stdio(0); cin.tie(0); int x[5]; cin >> x[0] >> x[1] >> x[2] >> x[3] >> x[4]; for (int i = 0; i < 5; i++) { if (x[i] == 0) { cout << i + 1 << "\n"; return 0; } } }
replace
16
17
16
17
1
p02639
C++
Runtime Error
// -------------- // Tejas Pandey | // 14 - 06 - 20 | // Atcoder BC 170| // -------------- #include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define vi vector<int> typedef long long int ll; int main(void) { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vector<pair<int, int>> v; for (int i = 0; i < 5; i++) cin >> v[i].first, v[i].second = i + 1; sort(v.begin(), v.end()); cout << v[0].second; return 0; }
// -------------- // Tejas Pandey | // 14 - 06 - 20 | // Atcoder BC 170| // -------------- #include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define vi vector<int> typedef long long int ll; int main(void) { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vector<pair<int, int>> v(5); for (int i = 0; i < 5; i++) cin >> v[i].first, v[i].second = i + 1; sort(v.begin(), v.end()); cout << v[0].second; return 0; }
replace
19
20
19
20
-11
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { vector<int> x(4); for (int i = 0; i < 5; i++) { cin >> x.at(i); } int ans = 0; for (int i = 0; i < 5; i++) { if (x.at(i) == 0) { ans = i + 1; break; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> x(5); for (int i = 0; i < 5; i++) { cin >> x.at(i); } int ans = 0; for (int i = 0; i < 5; i++) { if (x.at(i) == 0) { ans = i + 1; break; } } cout << ans << endl; }
replace
4
5
4
5
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)
p02639
C++
Runtime Error
#include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int i, var[5]; for (i = 1; i <= 5; ++i) { cin >> var[i]; if (var[i] == 0) cout << i << "\n"; } return 0; }
#include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long var; for (int i = 1; i <= 5; ++i) { cin >> var; if (var == 0) cout << i << "\n"; } return 0; }
replace
6
10
6
11
0
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; vector<int> vec(n); for (int i = 0; i < 6; i++) { cin >> vec.at(i); if (vec.at(i) == 0) { cout << i + 1 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e; cin >> a >> b >> c >> d >> e; if (a == 0) cout << 1 << endl; if (b == 0) cout << 2 << endl; if (c == 0) cout << 3 << endl; if (d == 0) cout << 4 << endl; if (e == 0) cout << 5 << endl; return 0; }
replace
4
12
4
16
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
p02639
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int num; while (1) { for (int i = 1; i <= 5; i++) { cin >> num; if (num == 0) { cout << i << endl; break; } } } }
#include <bits/stdc++.h> using namespace std; int main() { int num; while (1) { for (int i = 1; i <= 5; i++) { cin >> num; if (num == 0) { cout << i << endl; break; } } break; } }
insert
12
12
12
13
TLE
p02639
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; int main() { int in; vector<int> v; int s(0), save; int i = 0; while (i < 5) { cin >> in; s++; v.push_back(in); if (in == 0) { save = s; } } cout << save << "\n"; return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int in; vector<int> v; int s(0), save; int i = 0; while (i < 5) { cin >> in; s++; v.push_back(in); if (in == 0) { save = s; } i++; } cout << save << "\n"; return 0; }
insert
15
15
15
16
TLE
p02639
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define pp push_back #define pf push_front #define mp make_pair #define fs first #define sc second #define sf scanf using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> ii; const int N = (int)1e5; const int INF = (int)1e9 + 7; int main(int argc, char *argv[]) { // ios_base::sync_with_stdio(false); // cin.tie(NULL); int a[5]; int ans = 1; for (int i = 1; i <= 5; ++i) { sf("%d", &a[i]); if (a[i] == 0) ans = i; } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #define pp push_back #define pf push_front #define mp make_pair #define fs first #define sc second #define sf scanf using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> ii; const int N = (int)1e5; const int INF = (int)1e9 + 7; int main(int argc, char *argv[]) { // ios_base::sync_with_stdio(false); // cin.tie(NULL); int a[6]; int ans = 1; for (int i = 1; i <= 5; ++i) { sf("%d", &a[i]); if (a[i] == 0) ans = i; } printf("%d\n", ans); return 0; }
replace
22
23
22
23
TLE
p02639
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #define ll long long using namespace std; int main() { int arr[5]; arr[0] = -1333; int ans = -1; for (int i = 1; i < 6; i++) { cin >> arr[i]; if (arr[i] == 0) { ans = i; } } cout << ans; return 0; }
#include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #define ll long long using namespace std; int main() { int x1, x2, x3, x4, x5; cin >> x1 >> x2 >> x3 >> x4 >> x5; if (x1 == 0) cout << "1"; else if (x2 == 0) cout << "2"; else if (x3 == 0) cout << "3"; else if (x4 == 0) cout << "4"; else cout << "5"; return 0; }
replace
10
20
10
22
TLE
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int arr[5]; for (int i = 1; i <= 5; i++) { cin >> arr[i]; if (arr[i] == 0) { cout << i; } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll a, b, c, d, e; cin >> a >> b >> c >> d >> e; if (a == 0) { cout << 1; return 0; } else if (b == 0) { cout << 2; return 0; } else if (c == 0) { cout << 3; return 0; } else if (d == 0) { cout << 4; return 0; } else if (e == 0) { cout << 5; return 0; } }
replace
4
10
4
21
0
p02639
Python
Runtime Error
nums = [int(x) for x in input()] for i in range(5): if nums[i] == 0: print(i + 1) exit(0)
nums = [int(x) for x in input().split(" ")] for i in range(5): if nums[i] == 0: print(i + 1) exit(0)
replace
0
1
0
1
ValueError: invalid literal for int() with base 10: ' '
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02639/Python/s316600506.py", line 1, in <module> nums = [int(x) for x in input()] File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02639/Python/s316600506.py", line 1, in <listcomp> nums = [int(x) for x in input()] ValueError: invalid literal for int() with base 10: ' '
p02639
Python
Runtime Error
# -*- coding: utf-8 -*- a, b, c, d, e = map(int, input().split()) suuji_list = ["", a, b, c, d, e] print(list.index(0))
# -*- coding: utf-8 -*- a, b, c, d, e = map(int, input().split()) suuji_list = ["", a, b, c, d, e] print(suuji_list.index(0))
replace
5
6
5
6
TypeError: descriptor 'index' for 'list' objects doesn't apply to a 'int' object
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02639/Python/s072466363.py", line 6, in <module> print(list.index(0)) TypeError: descriptor 'index' for 'list' objects doesn't apply to a 'int' object
p02639
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define int long long #define mod 1000000007 #define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define f(i, n) for (int i = 0; i < n; i++) #define fp(i, k, n) for (int i = k; i <= n; i++) #define fr(i, k, n) for (int i = k; i >= n; i--) #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define dbg(x) cout << (#x) << " is " << (x) << '\n' #define F first #define S second #define sz(x) (int)(x).size() #define lb lower_bound #define ub upper_bound #define all(a) a.begin(), a.end() #define getc getchar_unlocked typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; /*---------------------------------------------------------------------------------------------------*/ void solve() { int x[5], ans = 0; fp(i, 1, 5) { cin >> x[i]; if (x[i] == 0) ans = i; } cout << ans << '\n'; return; } signed main() { FAST int T = 1; // cin >> T; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define int long long #define mod 1000000007 #define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define f(i, n) for (int i = 0; i < n; i++) #define fp(i, k, n) for (int i = k; i <= n; i++) #define fr(i, k, n) for (int i = k; i >= n; i--) #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define dbg(x) cout << (#x) << " is " << (x) << '\n' #define F first #define S second #define sz(x) (int)(x).size() #define lb lower_bound #define ub upper_bound #define all(a) a.begin(), a.end() #define getc getchar_unlocked typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; /*---------------------------------------------------------------------------------------------------*/ void solve() { int x[6], ans = 0; fp(i, 1, 5) { cin >> x[i]; if (x[i] == 0) ans = i; } cout << ans << '\n'; return; } signed main() { FAST int T = 1; // cin >> T; while (T--) { solve(); } return 0; }
replace
28
29
28
29
TLE
p02639
C++
Runtime Error
#include <bits/stdc++.h> #include <math.h> #include <stdio.h> using namespace std; int main() { vector<int> x(4); for (int i = 0; i < 5; i++) { cin >> x.at(i); } for (int i = 0; i < 5; i++) { if (x.at(i) == 0) { cout << i + 1; break; } } }
#include <bits/stdc++.h> #include <math.h> #include <stdio.h> using namespace std; int main() { vector<int> x(5); for (int i = 0; i < 5; i++) { cin >> x.at(i); } for (int i = 0; i < 5; i++) { if (x.at(i) == 0) { cout << i + 1; break; } } }
replace
7
8
7
8
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)
p02639
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int arr[1000000000]; for (int i = 1; i <= 5; i++) { cin >> arr[i]; } for (int i = 1; i <= 5; i++) { if (arr[i] == 0) { cout << i; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int arr[20]; for (int i = 1; i <= 5; i++) { cin >> arr[i]; } for (int i = 1; i <= 5; i++) { if (arr[i] == 0) { cout << i; } } return 0; }
replace
5
6
5
6
-11
p02639
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int x[5], ans; for (int i = 1; i <= 5; i++) { cin >> x[i]; if (x[i] == 0) ans = i; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int x[6]; int ans; for (int i = 1; i <= 5; i++) { cin >> x[i]; if (x[i] == 0) ans = i; } cout << ans << endl; return 0; }
replace
3
4
3
5
TLE
p02639
C++
Time Limit Exceeded
// Remeber Choose Compiler: GCC9 #include <bits/stdc++.h> using namespace std; // Debug Output Utility #ifdef LOCAL #define dbg(args...) \ do { \ cout << #args << " -> "; \ err(args); \ } while (0) void err() { std::cout << std::endl; } template <typename T, typename... Args> void err(T a, Args... args) { std::cout << a << ' '; err(args...); } template <template <typename...> class T, typename t, typename... A> void err(const T<t> &arg, const A &...args) { for (auto &v : arg) std::cout << v << ' '; err(args...); } #else #define dbg(...) #endif // Macro Definitions: #define mp make_pair #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() // Type Alias: using int64 = long long; using PII = pair<int, int>; // Global Const Variable const int64 INF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1000000007; const int MAXN = 200005; const int MAXL = 1000; int v[MAXN], w[MAXN], dp[MAXN][MAXL]; void run() { int a[5], ans = -1; for (int i = 1; i <= 5; i++) { cin >> a[i]; if (a[i] == 0) ans = i; } cout << ans << endl; } // Debug Input Redirection int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef LOCAL freopen("in.txt", "r", stdin); #endif run(); }
// Remeber Choose Compiler: GCC9 #include <bits/stdc++.h> using namespace std; // Debug Output Utility #ifdef LOCAL #define dbg(args...) \ do { \ cout << #args << " -> "; \ err(args); \ } while (0) void err() { std::cout << std::endl; } template <typename T, typename... Args> void err(T a, Args... args) { std::cout << a << ' '; err(args...); } template <template <typename...> class T, typename t, typename... A> void err(const T<t> &arg, const A &...args) { for (auto &v : arg) std::cout << v << ' '; err(args...); } #else #define dbg(...) #endif // Macro Definitions: #define mp make_pair #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() // Type Alias: using int64 = long long; using PII = pair<int, int>; // Global Const Variable const int64 INF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1000000007; const int MAXN = 200005; const int MAXL = 1000; int v[MAXN], w[MAXN], dp[MAXN][MAXL]; void run() { int a[6], ans = -1; for (int i = 1; i <= 5; i++) { cin >> a[i]; if (a[i] == 0) ans = i; } cout << ans << endl; } // Debug Input Redirection int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef LOCAL freopen("in.txt", "r", stdin); #endif run(); }
replace
47
48
47
48
TLE
p02639
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define fastio \ ios_base ::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; int main() { fastio ll a[5]; ll ans = 0; for (ll i = 1; i <= 5; ++i) { cin >> a[i]; if (a[i] == 0) ans = i; } cout << ans; }
#include <bits/stdc++.h> #define ll long long #define fastio \ ios_base ::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; int main() { fastio ll a, b, c, d, e; cin >> a >> b >> c >> d >> e; if (a == 0) cout << 1 << "\n"; if (b == 0) cout << 2 << "\n"; if (c == 0) cout << 3 << "\n"; if (d == 0) cout << 4 << "\n"; if (e == 0) cout << 5 << "\n"; }
replace
9
17
9
21
TLE
p02640
C++
Time Limit Exceeded
#include <iostream> using namespace std; int X, Y; bool dfs(int i, int sum) { if (i == X) return sum == Y; if (dfs(i + 1, sum + 2)) return true; if (dfs(i + 1, sum + 4)) return true; return false; } int main() { cin >> X >> Y; if (Y / 4 > X) { cout << "No"; return 0; } if (dfs(0, 0)) cout << "Yes"; else cout << "No"; }
#include <iostream> using namespace std; int X, Y; bool dfs(int i, int sum) { if (i == X) return sum == Y; if (dfs(i + 1, sum + 2)) return true; if (dfs(i + 1, sum + 4)) return true; return false; } int main() { cin >> X >> Y; if (Y >= X * 2 && Y <= X * 4 && Y % 2 == 0) { if (dfs(0, 0)) cout << "Yes"; } else cout << "No"; }
replace
17
24
17
21
TLE
p02640
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i <= (int)(n); i++) int main() { int x, y; cin >> x >> y; rep(i, x) { if (2 * i + 4 * (x - i) == y) { cout << "Yes "; exit(1); } } cout << "No" << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i <= (int)(n); i++) int main() { int x, y; cin >> x >> y; rep(i, x) { if (2 * i + 4 * (x - i) == y) { cout << "Yes "; return 0; } } cout << "No" << endl; return 0; }
replace
12
13
12
13
1
p02640
C++
Runtime Error
/* author : s@if */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define NIL -1 #define fi first #define sec second #define MAX INT_MAX #define INF 1e9 #define ll long long #define PI acos(-1.0) #define MOD 1000000007 #define PLL pair<ll, ll> #define PII pair<int, int> #define ull unsigned long long #define For(i, a, b) for (int i = a; i <= (int)b; i++) typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set; //*find_by_order(k) gives the kth element; // order_of_key(item) gives the index(number of element strictly less than // item) of item; inline int in() { int x; scanf("%d", &x); return x; } bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } int Set(int N, int pos) { return N = N | (1 << pos); } int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; // King's move int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; int hx[] = {-2, -2, -1, +1, +2, +2, -1, +1}; // Knight's move int hy[] = {+1, -1, +2, +2, -1, +1, -2, -2}; int dx[] = {+1, -1, +0, +0}; int dy[] = {+0, +0, +1, -1}; const int MAXN = (int)2e5 + 9; int main() { /* freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ int i, j, k, l, m, n, p, q, x, y, u, v, r, tc, t; cin >> x >> y; bool flag = false; for (i = 0; i <= x; i++) { p = i, q = x - i; if (p * 2 + q * 4 == y) { flag = true; } } if (flag) cout << "Yes\n"; else cout << "No\n"; main(); return 0; } // read the question correctly (is y a vowel? what are the exact constraints?) // look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)
/* author : s@if */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define NIL -1 #define fi first #define sec second #define MAX INT_MAX #define INF 1e9 #define ll long long #define PI acos(-1.0) #define MOD 1000000007 #define PLL pair<ll, ll> #define PII pair<int, int> #define ull unsigned long long #define For(i, a, b) for (int i = a; i <= (int)b; i++) typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set; //*find_by_order(k) gives the kth element; // order_of_key(item) gives the index(number of element strictly less than // item) of item; inline int in() { int x; scanf("%d", &x); return x; } bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } int Set(int N, int pos) { return N = N | (1 << pos); } int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; // King's move int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; int hx[] = {-2, -2, -1, +1, +2, +2, -1, +1}; // Knight's move int hy[] = {+1, -1, +2, +2, -1, +1, -2, -2}; int dx[] = {+1, -1, +0, +0}; int dy[] = {+0, +0, +1, -1}; const int MAXN = (int)2e5 + 9; int main() { /* freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ int i, j, k, l, m, n, p, q, x, y, u, v, r, tc, t; cin >> x >> y; bool flag = false; for (i = 0; i <= x; i++) { p = i, q = x - i; if (p * 2 + q * 4 == y) { flag = true; } } if (flag) cout << "Yes\n"; else cout << "No\n"; // main(); return 0; } // read the question correctly (is y a vowel? what are the exact constraints?) // look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)
replace
72
73
72
73
TLE
p02640
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define pb push_back #define mp make_pair #define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); #define ff first #define ss second #define INF (ll)(1e15) #define mod (ll)(1e9 + 7) #define endl "\n" #define tt \ ll testcase; \ cin >> testcase; \ while (testcase--) const int mxn = 1 * (1e5) + 5; ll n; int solve() { ll x, y; cin >> x >> y; if ((4 * x - y >= 0) && ((4 * x - y) % 2 == 0)) { if (y % 2 == 0) { if ((y - 2 * x >= 0) && ((y - 2 * x) % 2 == 0)) { cout << "Yes"; return 0; } } } cout << "No"; } int main() { fast; // tt solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define pb push_back #define mp make_pair #define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); #define ff first #define ss second #define INF (ll)(1e15) #define mod (ll)(1e9 + 7) #define endl "\n" #define tt \ ll testcase; \ cin >> testcase; \ while (testcase--) const int mxn = 1 * (1e5) + 5; ll n; int solve() { ll x, y; cin >> x >> y; if ((4 * x - y >= 0) && ((4 * x - y) % 2 == 0)) { if (y % 2 == 0) { if ((y - 2 * x >= 0) && ((y - 2 * x) % 2 == 0)) { cout << "Yes"; return 0; } } } cout << "No"; return 0; } int main() { fast; // tt solve(); }
insert
31
31
31
32
0
p02640
Python
Time Limit Exceeded
from numba import jit @jit def solve(X, Y, Z): while X >= 0: if 2 * X + 4 * Z == Y: return True elif 2 * X + 4 * Z < Y: X -= 1 Z += 1 return False def main(): X, Y = map(int, input().split()) Z = 0 if solve(X, Y, Z): print("Yes") else: print("No") if __name__ == "__main__": main()
from numba import jit @jit def solve(X, Y, Z): while X >= 0: if 2 * X + 4 * Z == Y: return True elif 2 * X + 4 * Z < Y: X -= 1 Z += 1 else: return False return False def main(): X, Y = map(int, input().split()) Z = 0 if solve(X, Y, Z): print("Yes") else: print("No") if __name__ == "__main__": main()
insert
11
11
11
13
TLE
p02640
C++
Runtime Error
#include <bits/stdc++.h> #define U(i, a, b) for (int i = (a); i <= (b); ++i) #define D(i, a, b) for (int i = (a); i >= (b); --i) using namespace std; int gi() { int f = 1, x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } typedef long long LL; typedef pair<int, int> pi; int main() { int x, y; cin >> x >> y; for (int many = 0; many <= x; ++many) { int left = (y - many * 2); if (left >= 0 && (x - many) * 4 == left) return puts("Yes"); } puts("No"); return 0; }
#include <bits/stdc++.h> #define U(i, a, b) for (int i = (a); i <= (b); ++i) #define D(i, a, b) for (int i = (a); i >= (b); --i) using namespace std; int gi() { int f = 1, x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } typedef long long LL; typedef pair<int, int> pi; int main() { int x, y; cin >> x >> y; for (int many = 0; many <= x; ++many) { int left = (y - many * 2); if (left >= 0 && (x - many) * 4 == left) return puts("Yes"), 0; } puts("No"); return 0; }
replace
27
28
27
28
4