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
p02732
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define x first #define y second #define pii pair<int, int> #define mp make_pair #define pb push_back #define sz(a) (int)(a.size()) ll ans = 0; int n; int b[100005]; map<int, ll> a; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> b[i]; a[b[i]]++; } for (auto x : a) ans += x.y * (x.y - 1) / 2; for (int i = 0; i < n; i++) { ans -= a[b[i]] * (a[b[i]] - 1) / 2; a[b[i]]--; ans += a[b[i]] * (a[b[i]] - 1) / 2; cout << ans << endl; ans -= a[b[i]] * (a[b[i]] - 1) / 2; a[b[i]]++; ans += a[b[i]] * (a[b[i]] - 1) / 2; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define x first #define y second #define pii pair<int, int> #define mp make_pair #define pb push_back #define sz(a) (int)(a.size()) ll ans = 0; int n; int b[200005]; map<int, ll> a; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> b[i]; a[b[i]]++; } for (auto x : a) ans += x.y * (x.y - 1) / 2; for (int i = 0; i < n; i++) { ans -= a[b[i]] * (a[b[i]] - 1) / 2; a[b[i]]--; ans += a[b[i]] * (a[b[i]] - 1) / 2; cout << ans << endl; ans -= a[b[i]] * (a[b[i]] - 1) / 2; a[b[i]]++; ans += a[b[i]] * (a[b[i]] - 1) / 2; } }
replace
11
12
11
12
0
p02732
C++
Runtime Error
#include <cstdio> #include <map> #define F(i, x, y) for (int i = x; i <= y; ++i) using namespace std; int n, a[100005]; long long sum; map<int, int> m; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); ++m[a[i]]; } for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) { int t = it->second; sum += 1ll * (t - 1) * t / 2; } for (int i = 1; i <= n; ++i) { printf("%lld\n", sum - m[a[i]] + 1); } return 0; }
#include <cstdio> #include <map> #define F(i, x, y) for (int i = x; i <= y; ++i) using namespace std; int n, a[200005]; long long sum; map<int, int> m; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); ++m[a[i]]; } for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) { int t = it->second; sum += 1ll * (t - 1) * t / 2; } for (int i = 1; i <= n; ++i) { printf("%lld\n", sum - m[a[i]] + 1); } return 0; }
replace
4
5
4
5
0
p02732
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> using namespace std; int fa[100008], fb[100008], fc[100008]; int main() { int n, m; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", fa + i); fb[i] = fa[i]; } sort(fb, fb + n); long long sum = 0, s = 1; for (int i = 1; i < n; i++) { if (fb[i] == fb[i - 1]) s++; else { fc[fb[i - 1]] = s; sum += (s - 1) * s / 2; s = 1; } } fc[fb[n - 1]] = s; sum += (s - 1) * s / 2; for (int i = 0; i < n; i++) { int x = fc[fa[i]]; printf("%lld\n", sum - x + 1); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> using namespace std; int fa[200008], fb[200008], fc[200008]; int main() { int n, m; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", fa + i); fb[i] = fa[i]; } sort(fb, fb + n); long long sum = 0, s = 1; for (int i = 1; i < n; i++) { if (fb[i] == fb[i - 1]) s++; else { fc[fb[i - 1]] = s; sum += (s - 1) * s / 2; s = 1; } } fc[fb[n - 1]] = s; sum += (s - 1) * s / 2; for (int i = 0; i < n; i++) { int x = fc[fa[i]]; printf("%lld\n", sum - x + 1); } }
replace
5
6
5
6
0
p02732
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, c) for (int i = 0; i < (int)c; ++i) using llint = long long int; using namespace std; int main() { int N; map<int, llint> mp; vector<int> A(N, 0); cin >> N; rep(i, N) { cin >> A[i]; mp[A[i]]++; } llint num = 0; for (auto itr = mp.begin(); itr != mp.end(); itr++) { llint seco = itr->second; if (seco == 0) continue; else num += seco * (seco - 1) / 2; } for (int i = 0; i < N; i++) { if (mp[A[i]] < 2) cout << num << endl; else cout << num - (mp[A[i]] * (mp[A[i]] - 1) / 2) + ((mp[A[i]] - 1) * (mp[A[i]] - 2) / 2) << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, c) for (int i = 0; i < (int)c; ++i) using llint = long long int; using namespace std; int main() { int N; map<int, llint> mp; int A[300000]; cin >> N; rep(i, N) { cin >> A[i]; mp[A[i]]++; } llint num = 0; for (auto itr = mp.begin(); itr != mp.end(); itr++) { llint seco = itr->second; if (seco == 0) continue; else num += seco * (seco - 1) / 2; } for (int i = 0; i < N; i++) { if (mp[A[i]] < 2) cout << num << endl; else cout << num - (mp[A[i]] * (mp[A[i]] - 1) / 2) + ((mp[A[i]] - 1) * (mp[A[i]] - 2) / 2) << endl; } return 0; }
replace
9
10
9
10
0
p02732
C++
Runtime Error
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define eps 1e-6 #define ll long long using namespace std; const int N = 1e3 + 5; ll a[N], sum, b[N]; ll c(int x, int y) { ll a = 1, b = 1; for (int i = y; i > y - x; i--) a *= i; for (int i = 1; i <= x; i++) b *= i; return a / b; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, x; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; a[x]++; b[i] = x; } for (int i = 1; i <= n; i++) { if (a[i] > 1) sum += c(2, a[i]); } // cout<<sum<<endl; // for(int i=1;i<=n;i++)cout<<a[b[i]]<<" "; // cout<<endl; for (int i = 1; i <= n; i++) { if (a[b[i]] >= 2) cout << sum - a[b[i]] + 1 << endl; else cout << sum << endl; } }
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define eps 1e-6 #define ll long long using namespace std; const int N = 2e5 + 5; ll a[N], sum, b[N]; ll c(int x, int y) { ll a = 1, b = 1; for (int i = y; i > y - x; i--) a *= i; for (int i = 1; i <= x; i++) b *= i; return a / b; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, x; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; a[x]++; b[i] = x; } for (int i = 1; i <= n; i++) { if (a[i] > 1) sum += c(2, a[i]); } // cout<<sum<<endl; // for(int i=1;i<=n;i++)cout<<a[b[i]]<<" "; // cout<<endl; for (int i = 1; i <= n; i++) { if (a[b[i]] >= 2) cout << sum - a[b[i]] + 1 << endl; else cout << sum << endl; } }
replace
5
6
5
6
0
p02732
C++
Runtime Error
#include <bits/stdc++.h> #define F first #define S second using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 100010, M = 110; int n; int a[N]; int cnt[N]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); a[i] = x; cnt[x]++; } LL tot = 0; for (int i = 1; i <= n; i++) tot += (LL)cnt[i] * (cnt[i] - 1) / 2LL; for (int i = 0; i < n; i++) { LL t = tot; int x = a[i]; t -= (LL)cnt[x] * (cnt[x] - 1) / 2LL; t += (LL)(cnt[x] - 2) * (cnt[x] - 1) / 2LL; printf("%lld\n", t); } return 0; }
#include <bits/stdc++.h> #define F first #define S second using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 200010, M = 110; int n; int a[N]; int cnt[N]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); a[i] = x; cnt[x]++; } LL tot = 0; for (int i = 1; i <= n; i++) tot += (LL)cnt[i] * (cnt[i] - 1) / 2LL; for (int i = 0; i < n; i++) { LL t = tot; int x = a[i]; t -= (LL)cnt[x] * (cnt[x] - 1) / 2LL; t += (LL)(cnt[x] - 2) * (cnt[x] - 1) / 2LL; printf("%lld\n", t); } return 0; }
replace
10
11
10
11
0
p02732
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; ll a, b, c, i, j, k, l, m, n, ans, tmp, hoge, fuga, piyo; ll x[10010], y[200200]; string s, str; int main() { cin >> n; for (int i = 0; i < n; i++) { y[i] = 0; } for (int i = 0; i < n; i++) { cin >> x[i]; y[x[i] - 1]++; } for (int i = 0; i < n; i++) { ans += y[i] * (y[i] - 1) / 2; } for (int i = 0; i < n; i++) { cout << ans - (y[x[i] - 1] - 1) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll a, b, c, i, j, k, l, m, n, ans, tmp, hoge, fuga, piyo; ll x[200200], y[200200]; string s, str; int main() { cin >> n; for (int i = 0; i < n; i++) { y[i] = 0; } for (int i = 0; i < n; i++) { cin >> x[i]; y[x[i] - 1]++; } for (int i = 0; i < n; i++) { ans += y[i] * (y[i] - 1) / 2; } for (int i = 0; i < n; i++) { cout << ans - (y[x[i] - 1] - 1) << endl; } return 0; }
replace
4
5
4
5
0
p02732
C++
Time Limit Exceeded
#include <algorithm> #include <ciso646> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <string> #include <utility> #include <vector> // 解説ac using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int momm(int a) { int sum = 0; for (int i = 1; i < a; i++) sum += i; return sum; } int main() { // ゼロツーかわいい!!!! int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; rep(i, n) a[i]--; vector<int> cnt(n); rep(i, n) cnt[a[i]]++; long long tot = 0; rep(i, n) // 消去前のansを作り適宜消去していく tot += momm(cnt[i]); rep(i, n) { long long ans = tot; ans -= momm(cnt[a[i]]); // まるごと消去した cnt[a[i]]--; ans += momm(cnt[a[i]]); // 復元すべきところを復元した cout << ans << endl; cnt[a[i]]++; } }
#include <algorithm> #include <ciso646> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <string> #include <utility> #include <vector> // 解説ac using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) long long momm(long long a) { return a * (a - 1) / 2; } int main() { // ゼロツーかわいい!!!! int n; cin >> n; vector<int> a(n); rep(i, n) cin >> a[i]; rep(i, n) a[i]--; vector<int> cnt(n); rep(i, n) cnt[a[i]]++; long long tot = 0; rep(i, n) // 消去前のansを作り適宜消去していく tot += momm(cnt[i]); rep(i, n) { long long ans = tot; ans -= momm(cnt[a[i]]); // まるごと消去した cnt[a[i]]--; ans += momm(cnt[a[i]]); // 復元すべきところを復元した cout << ans << endl; cnt[a[i]]++; } }
replace
17
23
17
18
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define INF 1 << 30 #define endl '\n' #define maxn 1000005 #define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); typedef long long ll; const double PI = acos(-1.0); const ll mod = 1e9 + 7; inline void normal(ll &a) { a %= mod; (a < 0) && (a += mod); } inline ll modMul(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a * b) % mod; } inline ll modAdd(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a + b) % mod; } inline ll modSub(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); a -= b; normal(a); return a; } inline ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } inline ll modInverse(ll a) { return modPow(a, mod - 2); } inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } /* for Random Number generate mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); */ ///** template <typename F, typename S> ostream &operator<<(ostream &os, const pair<F, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "}"; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename F, typename S> ostream &operator<<(ostream &os, const map<F, S> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } #define dbg(args...) \ do { \ cerr << #args << " : "; \ faltu(args); \ } while (0) clock_t tStart = clock(); #define timeStamp \ dbg("Execution Time: ", (double)(clock() - tStart) / CLOCKS_PER_SEC) void faltu() { cerr << endl; } template <typename T> void faltu(T a[], int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl; } template <typename T, typename... hello> void faltu(T arg, const hello &...rest) { cerr << arg << ' '; faltu(rest...); } // Program showing a policy-based data structure. #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less #include <iostream> using namespace __gnu_pbds; using namespace std; // GNU link : https://goo.gl/WVDL6g typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set; // find_by_order(k) – ফাংশনটি kth ordered element এর একটা পয়েন্টার রিটার্ন করে। // অর্থাৎ তুমি চাইলেই kth ইন্ডেক্সে কি আছে, সেটা জেনে ফেলতে পারছো! order_of_key(x) – // ফাংশনটি x এলিমেন্টটা কোন পজিশনে আছে সেটা বলে দেয়। //*//**___________________________________________________**/ const int H = 10; const int W = 1010; string g[H]; vector<int> dp[W]; int h, w, k; int ans; int main() { FASTIO /* #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif //*/ cin >> h >> w >> k; for (int i = 1; i <= h; i++) { cin >> g[i]; g[i] = "#" + g[i]; } ans = mod; for (int mask = 0; mask < (1 << (h - 1)); mask++) { int cnt = __builtin_popcount(mask); vector<int> cur(cnt + 1, 0); int x = mask | (1 << (h - 1)); int flag = 1; for (int j = 1; j <= w; j++) { int ok = 1; vector<int> extra; int y = 0; for (int i = 1; i <= h; i++) { y += (g[i][j] == '1'); if ((x >> (i - 1)) & 1) { extra.emplace_back(y); y = 0; } } for (auto it : extra) { if (it > k) { flag = 0; break; } } if (flag == 0) break; for (int i = 0; i < (int)extra.size(); i++) { cur[i] += extra[i]; if (cur[i] > k) { ok = 0; break; } } if (!ok) { cnt++; cur = extra; } } if (flag) ans = min(ans, cnt); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define INF 1 << 30 #define endl '\n' #define maxn 1000005 #define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); typedef long long ll; const double PI = acos(-1.0); const ll mod = 1e9 + 7; inline void normal(ll &a) { a %= mod; (a < 0) && (a += mod); } inline ll modMul(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a * b) % mod; } inline ll modAdd(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a + b) % mod; } inline ll modSub(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); a -= b; normal(a); return a; } inline ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } inline ll modInverse(ll a) { return modPow(a, mod - 2); } inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } /* for Random Number generate mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); */ ///** template <typename F, typename S> ostream &operator<<(ostream &os, const pair<F, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "}"; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename F, typename S> ostream &operator<<(ostream &os, const map<F, S> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } #define dbg(args...) \ do { \ cerr << #args << " : "; \ faltu(args); \ } while (0) clock_t tStart = clock(); #define timeStamp \ dbg("Execution Time: ", (double)(clock() - tStart) / CLOCKS_PER_SEC) void faltu() { cerr << endl; } template <typename T> void faltu(T a[], int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl; } template <typename T, typename... hello> void faltu(T arg, const hello &...rest) { cerr << arg << ' '; faltu(rest...); } // Program showing a policy-based data structure. #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less #include <iostream> using namespace __gnu_pbds; using namespace std; // GNU link : https://goo.gl/WVDL6g typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set; // find_by_order(k) – ফাংশনটি kth ordered element এর একটা পয়েন্টার রিটার্ন করে। // অর্থাৎ তুমি চাইলেই kth ইন্ডেক্সে কি আছে, সেটা জেনে ফেলতে পারছো! order_of_key(x) – // ফাংশনটি x এলিমেন্টটা কোন পজিশনে আছে সেটা বলে দেয়। //*//**___________________________________________________**/ const int H = 15; const int W = 1010; string g[H]; vector<int> dp[W]; int h, w, k; int ans; int main() { FASTIO /* #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif //*/ cin >> h >> w >> k; for (int i = 1; i <= h; i++) { cin >> g[i]; g[i] = "#" + g[i]; } ans = mod; for (int mask = 0; mask < (1 << (h - 1)); mask++) { int cnt = __builtin_popcount(mask); vector<int> cur(cnt + 1, 0); int x = mask | (1 << (h - 1)); int flag = 1; for (int j = 1; j <= w; j++) { int ok = 1; vector<int> extra; int y = 0; for (int i = 1; i <= h; i++) { y += (g[i][j] == '1'); if ((x >> (i - 1)) & 1) { extra.emplace_back(y); y = 0; } } for (auto it : extra) { if (it > k) { flag = 0; break; } } if (flag == 0) break; for (int i = 0; i < (int)extra.size(); i++) { cur[i] += extra[i]; if (cur[i] > k) { ok = 0; break; } } if (!ok) { cnt++; cur = extra; } } if (flag) ans = min(ans, cnt); } cout << ans << endl; return 0; }
replace
125
126
125
126
0
p02733
C++
Time Limit Exceeded
/** * author: zjsdut * created: 2020/03/23 11:53:32 **/ #include <bits/stdc++.h> using namespace std; struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; template <typename A, typename B> bool chkmin(A &a, const B &b) { if (b < a) { a = b; return true; } return false; } template <typename A, typename B> bool chkmax(A &a, const B &b) { if (b > a) { a = b; return true; } return false; } template <typename T> istream &operator>>(istream &stream, vector<T> &vec) { for (auto &x : vec) stream >> x; return stream; } void scan() {} template <class T, class... Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } using ll = long long; using vl = vector<ll>; using vb = vector<bool>; using vi = vector<int>; using pii = pair<int, int>; template <typename T> using vv = vector<vector<T>>; #define rng(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define up(i, a, b) for (int i = int(a); i <= int(b); ++i) #define down(i, b, a) for (int i = int(b); i >= int(a); i--) #define TOKENPASTE(x, y) x##y #define TOKENPASTE2(x, y) TOKENPASTE(x, y) #define rep(n) \ for (int TOKENPASTE2(_iter_, __LINE__) = 0, \ TOKENPASTE2(_num_, __LINE__) = (int)(n); \ TOKENPASTE2(_iter_, __LINE__) < TOKENPASTE2(_num_, __LINE__); \ ++TOKENPASTE2(_iter_, __LINE__)) #define FOR(x, cont) for (const auto &x : cont) #define For(x, cont) for (auto &x : cont) #define all(x) begin(x), end(x) #define pb push_back #define mp make_pair #define eb emplace_back #define ep emplace #define SZ(x) (int)(x).size() #define UNIQ(vec) (vec).erase(unique(all(vec)), end(vec)) #define LB(cont, x) int(lower_bound(all(cont), x) - begin(cont)) #define UB(cont, x) int(upper_bound(all(cont), x) - begin(cont)) template <typename T> ostream &operator<<(ostream &stream, const vector<T> &vec) { if (!vec.empty()) { stream << vec[0]; for (size_t i = 1; i != vec.size(); ++i) stream << ' ' << vec[i]; } return stream; } template <typename T> void print(const vector<T> &t) { cout << t << '\n'; } template <typename T> void print(const vector<vector<T>> &t) { for (const auto &row : t) { print(row); } } template <typename T> void print(const T &t) { cout << t << ' '; } template <typename T, typename... Args> void print(const T &t, const Args &...rest) { print(t); print(rest...); } template <typename T> void println(const T &t) { cout << t << '\n'; } template <typename T, typename... Args> void println(const T &t, const Args &...rest) { print(t); println(rest...); } template <typename int_t> inline int_t lowbit(int_t x) { return x & -x; } inline unsigned h_bit(unsigned long long x) { return sizeof(unsigned long long) * 8 - __builtin_clzll(x); } unsigned long long pow2(unsigned long long x) { // round up to power of 2 return x == lowbit(x) ? x : 1ull << h_bit(x); } template <typename T> int get_bit(T a, int i) { return a >> i & 1; } // debug code by tourist string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #if defined LOCAL && !defined DUIPAI #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define popcnt(x) __builtin_popcountll((x)) int a[10][1000]; int main() { #if defined LOCAL && !defined DUIPAI ifstream in("in.txt"); cin.rdbuf(in.rdbuf()); #endif int H, W, K; scan(H, W, K); rng(i, 0, H) { string s; scan(s); rng(j, 0, W) { a[i][j] = s[j] - '0'; } } auto check = [&](int s) { // cout <<"s: " << mask << endl; s |= 1 << (H - 1); int pre = 0; vv<int> cnt; rng(i, 0, H) { if (get_bit(s, i)) { cnt.emplace_back(); rng(j, 0, W) { int sum = 0; up(row, pre, i) { sum += a[row][j]; } cnt.back().pb(sum); } pre = i + 1; } } // FOR (r, cnt) { // println(r); // } int n = popcnt(s); assert(SZ(cnt) == n); pre = 0; int ans = 0; while (true) { int mi = W; FOR(row, cnt) { debug(row); int sum = 0; int i = pre; while (i < W && sum + row[i] <= K) { debug(i, sum); sum += row[i]; ++i; } debug(i); chkmin(mi, i); } debug(mi); if (mi == W) break; pre = mi; ++ans; } debug(n, ans + n - 1); return ans + n - 1; }; int ans = INT_MAX; int tot = 1 << (H - 1); rng(s, 0, tot) { chkmin(ans, check(s)); } println(ans); return 0; }
/** * author: zjsdut * created: 2020/03/23 11:53:32 **/ #include <bits/stdc++.h> using namespace std; struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; template <typename A, typename B> bool chkmin(A &a, const B &b) { if (b < a) { a = b; return true; } return false; } template <typename A, typename B> bool chkmax(A &a, const B &b) { if (b > a) { a = b; return true; } return false; } template <typename T> istream &operator>>(istream &stream, vector<T> &vec) { for (auto &x : vec) stream >> x; return stream; } void scan() {} template <class T, class... Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } using ll = long long; using vl = vector<ll>; using vb = vector<bool>; using vi = vector<int>; using pii = pair<int, int>; template <typename T> using vv = vector<vector<T>>; #define rng(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define up(i, a, b) for (int i = int(a); i <= int(b); ++i) #define down(i, b, a) for (int i = int(b); i >= int(a); i--) #define TOKENPASTE(x, y) x##y #define TOKENPASTE2(x, y) TOKENPASTE(x, y) #define rep(n) \ for (int TOKENPASTE2(_iter_, __LINE__) = 0, \ TOKENPASTE2(_num_, __LINE__) = (int)(n); \ TOKENPASTE2(_iter_, __LINE__) < TOKENPASTE2(_num_, __LINE__); \ ++TOKENPASTE2(_iter_, __LINE__)) #define FOR(x, cont) for (const auto &x : cont) #define For(x, cont) for (auto &x : cont) #define all(x) begin(x), end(x) #define pb push_back #define mp make_pair #define eb emplace_back #define ep emplace #define SZ(x) (int)(x).size() #define UNIQ(vec) (vec).erase(unique(all(vec)), end(vec)) #define LB(cont, x) int(lower_bound(all(cont), x) - begin(cont)) #define UB(cont, x) int(upper_bound(all(cont), x) - begin(cont)) template <typename T> ostream &operator<<(ostream &stream, const vector<T> &vec) { if (!vec.empty()) { stream << vec[0]; for (size_t i = 1; i != vec.size(); ++i) stream << ' ' << vec[i]; } return stream; } template <typename T> void print(const vector<T> &t) { cout << t << '\n'; } template <typename T> void print(const vector<vector<T>> &t) { for (const auto &row : t) { print(row); } } template <typename T> void print(const T &t) { cout << t << ' '; } template <typename T, typename... Args> void print(const T &t, const Args &...rest) { print(t); print(rest...); } template <typename T> void println(const T &t) { cout << t << '\n'; } template <typename T, typename... Args> void println(const T &t, const Args &...rest) { print(t); println(rest...); } template <typename int_t> inline int_t lowbit(int_t x) { return x & -x; } inline unsigned h_bit(unsigned long long x) { return sizeof(unsigned long long) * 8 - __builtin_clzll(x); } unsigned long long pow2(unsigned long long x) { // round up to power of 2 return x == lowbit(x) ? x : 1ull << h_bit(x); } template <typename T> int get_bit(T a, int i) { return a >> i & 1; } // debug code by tourist string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #if defined LOCAL && !defined DUIPAI #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define popcnt(x) __builtin_popcountll((x)) int a[10][1000]; int main() { #if defined LOCAL && !defined DUIPAI ifstream in("in.txt"); cin.rdbuf(in.rdbuf()); #endif int H, W, K; scan(H, W, K); rng(i, 0, H) { string s; scan(s); rng(j, 0, W) { a[i][j] = s[j] - '0'; } } auto check = [&](int s) { // cout <<"s: " << mask << endl; s |= 1 << (H - 1); int pre = 0; vv<int> cnt; rng(i, 0, H) { if (get_bit(s, i)) { cnt.emplace_back(); rng(j, 0, W) { int sum = 0; up(row, pre, i) { sum += a[row][j]; } if (sum > K) return INT_MAX; cnt.back().pb(sum); } pre = i + 1; } } // FOR (r, cnt) { // println(r); // } int n = popcnt(s); assert(SZ(cnt) == n); pre = 0; int ans = 0; while (true) { int mi = W; FOR(row, cnt) { debug(row); int sum = 0; int i = pre; while (i < W && sum + row[i] <= K) { debug(i, sum); sum += row[i]; ++i; } debug(i); chkmin(mi, i); } debug(mi); if (mi == W) break; pre = mi; ++ans; } debug(n, ans + n - 1); return ans + n - 1; }; int ans = INT_MAX; int tot = 1 << (H - 1); rng(s, 0, tot) { chkmin(ans, check(s)); } println(ans); return 0; }
insert
181
181
181
183
TLE
p02733
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define fi first #define se second #define mp make_pair #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, n) for (int i = n; i >= 0; --i) const int inf = 1e9 + 7; const ll mod = 1e9 + 7; const ll big = 1e18; const double PI = 2 * asin(1); int main() { int H, W, K; cin >> H >> W >> K; vector<string> S(H); for (int i = 0; i < H; ++i) cin >> S[i]; int ans = inf; int tmp; int index; bool out; bool outout; map<int, int> amap; for (int bit = 0; bit < (1 << (H - 1)); ++bit) { for (int i = 0; i < H; ++i) amap[i] = 0; outout = true; tmp = 0; for (int i = 0; i < H - 1; ++i) { if ((bit & (1 << i)) != 0) tmp++; } for (int i = 0; i < W; ++i) { index = 0; out = true; for (int j = 0; j < H; ++j) { if (S[j][i] == '1') { amap[index]++; if (amap[index] > K) out = false; } if ((bit & (1 << j)) != 0) index++; } if (!out) { if (!outout) break; tmp++; for (int j = 0; j < H; ++j) amap[j] = 0; i--; outout = false; } outout = true; } if (!outout) continue; ans = min(ans, tmp); } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define fi first #define se second #define mp make_pair #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, n) for (int i = n; i >= 0; --i) const int inf = 1e9 + 7; const ll mod = 1e9 + 7; const ll big = 1e18; const double PI = 2 * asin(1); int main() { int H, W, K; cin >> H >> W >> K; vector<string> S(H); for (int i = 0; i < H; ++i) cin >> S[i]; int ans = inf; int tmp; int index; bool out; bool outout; map<int, int> amap; for (int bit = 0; bit < (1 << (H - 1)); ++bit) { for (int i = 0; i < H; ++i) amap[i] = 0; outout = true; tmp = 0; for (int i = 0; i < H - 1; ++i) { if ((bit & (1 << i)) != 0) tmp++; } for (int i = 0; i < W; ++i) { index = 0; out = true; for (int j = 0; j < H; ++j) { if (S[j][i] == '1') { amap[index]++; if (amap[index] > K) out = false; } if ((bit & (1 << j)) != 0) index++; } if (!out) { if (!outout) break; tmp++; for (int j = 0; j < H; ++j) amap[j] = 0; i--; outout = false; } else outout = true; } if (!outout) continue; ans = min(ans, tmp); } cout << ans << endl; }
replace
64
66
64
66
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; #define ll long long #define pp pair<ll, ll> typedef tree<pp, null_type, less<pp>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define ld long double #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for (ll i = a; i < b; i += 1) #define sz(v) (ll)(v.size()) #define hell (ll)1000000007 #define slld(x) scanf("%lld", &x) ll t = 1; const ll inf = hell, N = 305; inline ll mult(ll x, ll y) { return (x * y) % hell; } inline ll powMod(ll base, ll expo) { ll ans = 1; while (expo) { if (expo & 1) ans = mult(ans, base); base = mult(base, base); expo >>= 1; } return ans; } void solve() { ll h, w, k, f, res = inf, ans; cin >> h >> w >> k; char c; ll a[h + 1][w + 1]; rep(i, 1, h + 1) { rep(j, 1, w + 1) { cin >> c; a[i][j] = c - '0'; } } map<ll, vector<ll>> mapa; rep(mask, 0, (1 << (h - 1))) { ans = 0; mapa.clear(); rep(i, 1, h + 1) { mapa[ans].pb(i); if ((mask >> (i - 1)) & 1) ans++; } vector<ll> cnt(ans + 1, 0); rep(i, 1, w + 1) { f = 1; vector<ll> tmp(ans + 1, 0); for (auto x : mapa) { for (auto y : x.se) tmp[x.fi] += a[y][i]; if (cnt[x.fi] + tmp[x.fi] > k) f = 0; } for (auto x : mapa) { f == 1 ? cnt[x.fi] += tmp[x.fi] : cnt[x.fi] = tmp[x.fi]; if (cnt[x.fi] > k) ans = inf; } if (f == 0) ans++; } res = min(res, ans); } cout << res << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // cin >> t ; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; #define ll long long #define pp pair<ll, ll> typedef tree<pp, null_type, less<pp>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define ld long double #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for (ll i = a; i < b; i += 1) #define sz(v) (ll)(v.size()) #define hell (ll)1000000007 #define slld(x) scanf("%lld", &x) ll t = 1; const ll inf = hell, N = 305; inline ll mult(ll x, ll y) { return (x * y) % hell; } inline ll powMod(ll base, ll expo) { ll ans = 1; while (expo) { if (expo & 1) ans = mult(ans, base); base = mult(base, base); expo >>= 1; } return ans; } void solve() { ll h, w, k, f, res = inf, ans; cin >> h >> w >> k; char c; ll a[h + 1][w + 1]; rep(i, 1, h + 1) { rep(j, 1, w + 1) { cin >> c; a[i][j] = c - '0'; } } map<ll, vector<ll>> mapa; rep(mask, 0, (1 << (h - 1))) { ans = 0; mapa.clear(); rep(i, 1, h + 1) { mapa[ans].pb(i); if ((mask >> (i - 1)) & 1) ans++; } vector<ll> cnt(ans + 1, 0); rep(i, 1, w + 1) { f = 1; if (ans >= inf) continue; vector<ll> tmp(ans + 1, 0); for (auto x : mapa) { for (auto y : x.se) tmp[x.fi] += a[y][i]; if (cnt[x.fi] + tmp[x.fi] > k) f = 0; } for (auto x : mapa) { f == 1 ? cnt[x.fi] += tmp[x.fi] : cnt[x.fi] = tmp[x.fi]; if (cnt[x.fi] > k) ans = inf; } if (f == 0) ans++; } res = min(res, ans); } cout << res << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // cin >> t ; while (t--) { solve(); } return 0; }
insert
55
55
55
57
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <string> #define ft first #define sc second #define pt(sth) cout << sth << "\n" #define chmax(a, b) (a) = max(a, b) #define chmin(a, b) (a) = min(a, b) #define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD using namespace std; typedef long long ll; typedef pair<ll, ll> pll; static const ll INF = 1e18; static const ll MAX = 101010; static const ll MOD = 1e9 + 7; /* for(i=0; i<N; i++) cin >> a[i]; */ int main() { ll i, j, k; ll H, W, K; cin >> H >> W >> K; string s[11]; ll sum[11][1111] = {}; for (i = 1; i <= H; i++) { cin >> s[i]; s[i] = "0" + s[i]; } for (i = 1; i <= H; i++) for (j = 1; j <= W; j++) sum[i][j] = sum[i][j - 1] + s[i][j] - '0'; for (j = 1; j <= W; j++) for (i = 1; i <= H; i++) sum[i][j] += sum[i - 1][j]; ll ans = INF; for (ll S = 0; S < 1 << (H - 1); S++) { vector<ll> v; v.push_back(0); for (i = 0; i < H - 1; i++) if (S >> i & 1) v.push_back(i + 1); v.push_back(H); ll n = v.size() - 1; vector<ll> cnt[n]; for (k = 0, i = 1; i < v.size(); k++, i++) { cnt[k].push_back(0); for (j = 1; j <= W; j++) cnt[k].push_back(sum[v[i]][j] - sum[v[i - 1]][j]); cnt[k].push_back(INF); } ll c = -1; ll k = 0; while (k < W) { ll t = W; for (i = 0; i < n; i++) { ll p = k + 1; while (cnt[i][k] + K >= cnt[i][p]) p++; p--; chmin(t, p); } k = t; c++; } chmin(ans, (ll)v.size() - 2LL + c); } pt(ans); }
#include <bits/stdc++.h> #include <string> #define ft first #define sc second #define pt(sth) cout << sth << "\n" #define chmax(a, b) (a) = max(a, b) #define chmin(a, b) (a) = min(a, b) #define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD using namespace std; typedef long long ll; typedef pair<ll, ll> pll; static const ll INF = 1e18; static const ll MAX = 101010; static const ll MOD = 1e9 + 7; /* for(i=0; i<N; i++) cin >> a[i]; */ int main() { ll i, j, k; ll H, W, K; cin >> H >> W >> K; string s[11]; ll sum[11][1111] = {}; for (i = 1; i <= H; i++) { cin >> s[i]; s[i] = "0" + s[i]; } for (i = 1; i <= H; i++) for (j = 1; j <= W; j++) sum[i][j] = sum[i][j - 1] + s[i][j] - '0'; for (j = 1; j <= W; j++) for (i = 1; i <= H; i++) sum[i][j] += sum[i - 1][j]; ll ans = INF; for (ll S = 0; S < 1 << (H - 1); S++) { vector<ll> v; v.push_back(0); for (i = 0; i < H - 1; i++) if (S >> i & 1) v.push_back(i + 1); v.push_back(H); ll n = v.size() - 1; vector<ll> cnt[n]; for (k = 0, i = 1; i < v.size(); k++, i++) { cnt[k].push_back(0); for (j = 1; j <= W; j++) cnt[k].push_back(sum[v[i]][j] - sum[v[i - 1]][j]); cnt[k].push_back(INF); } ll c = -1; ll k = 0; while (k < W) { ll t = W; for (i = 0; i < n; i++) { ll p = k + 1; while (cnt[i][k] + K >= cnt[i][p]) p++; p--; chmin(t, p); } if (k == t) { c = INF; break; } k = t; c++; } chmin(ans, (ll)v.size() - 2LL + c); } pt(ans); }
insert
71
71
71
76
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define fw(p) for (int w = 0; w < (p); w++) #define fx(p) for (int x = 0; x < (p); x++) #define fy(p) for (int y = 0; y < (p); y++) #define fz(p) for (int z = 0; z < (p); z++) #define fyg(p, g) for (int y = (g); y < (p); y++) #define fzg(p, g) for (int z = (g); z < (p); z++) #define ce(d) cout << d << endl; #define vecp(p) \ int aa; \ cin >> aa; \ (p).push_back(aa); #define vecpl(p) \ long long aa; \ cin >> aa; \ (p).push_back(aa); #define vecps(p) \ string aa; \ cin >> aa; \ (p).push_back(aa); #define vecp2(p) \ cin >> aa; \ (p).push_back(aa); #define vecpl2(p) \ long long a b; \ cin >> ab; \ (p).push_back(ab); #define vecps2(p) \ string ab; \ cin >> ab; \ (p).push_back(ab); #define sorts(c) sort((c).begin(), (c).end()); #define reverses(c) reverse((c).begin(), (c).end()); #define vec(b) vector<int>(b); #define vecl(b) vector<long long>(b); #define vecs(b) vector<string>(b); #define pb(b, a) (b).push_back((a)); #define doublece(a, b) cout << (a) << ' ' << (b) << endl; #define pairs(s) vector<pair<int, int>>(s); #define pairsp(s) \ int aa, bb; \ cin >> aa >> bb; \ (s).push_back(make_pair(aa, bb)); #define MOD 1000000007 #define cey ce("Yes") #define cen ce("No") #define ceY ce("YES") #define ceN ce("NO") int choco[11][1001]; int main() { int H, W, K; cin >> H >> W >> K; vector<string> S; fx(H) { vecps(S); } fy(W) { choco[1][y + 1] = S[0][y] == '1' ? 1 : 0; fx(H - 1) { choco[x + 2][y + 1] = choco[x + 1][y + 1]; if (S[x + 1][y] == '1') { choco[x + 2][y + 1]++; } } } int ans = INT_MAX; fx(2 << H) { vec(B); pb(B, 0); int y = x; int i = 0; while (y > 0) { if ((y & 1) == 1) { pb(B, i + 1); } i++; y >>= 1; } pb(B, H) int sum = B.size() - 2; vector<int> SUMS(B.size() - 1); fw(W + 1) { int flag = 0; fz(B.size() - 1) { SUMS[z] += choco[B[z + 1]][w] - choco[B[z]][w]; if (w + 1 <= W && SUMS[z] + choco[B[z + 1]][w + 1] - choco[B[z]][w + 1] > K) { flag = 1; if (w == 0) { flag = 2; } break; } } if (flag > 0) { sum++; if (flag == 2) { sum = H * W; } SUMS = vector<int>(B.size() - 1, 0); } } ans = min(ans, sum); } ce(ans) return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define fw(p) for (int w = 0; w < (p); w++) #define fx(p) for (int x = 0; x < (p); x++) #define fy(p) for (int y = 0; y < (p); y++) #define fz(p) for (int z = 0; z < (p); z++) #define fyg(p, g) for (int y = (g); y < (p); y++) #define fzg(p, g) for (int z = (g); z < (p); z++) #define ce(d) cout << d << endl; #define vecp(p) \ int aa; \ cin >> aa; \ (p).push_back(aa); #define vecpl(p) \ long long aa; \ cin >> aa; \ (p).push_back(aa); #define vecps(p) \ string aa; \ cin >> aa; \ (p).push_back(aa); #define vecp2(p) \ cin >> aa; \ (p).push_back(aa); #define vecpl2(p) \ long long a b; \ cin >> ab; \ (p).push_back(ab); #define vecps2(p) \ string ab; \ cin >> ab; \ (p).push_back(ab); #define sorts(c) sort((c).begin(), (c).end()); #define reverses(c) reverse((c).begin(), (c).end()); #define vec(b) vector<int>(b); #define vecl(b) vector<long long>(b); #define vecs(b) vector<string>(b); #define pb(b, a) (b).push_back((a)); #define doublece(a, b) cout << (a) << ' ' << (b) << endl; #define pairs(s) vector<pair<int, int>>(s); #define pairsp(s) \ int aa, bb; \ cin >> aa >> bb; \ (s).push_back(make_pair(aa, bb)); #define MOD 1000000007 #define cey ce("Yes") #define cen ce("No") #define ceY ce("YES") #define ceN ce("NO") int choco[11][1001]; int main() { int H, W, K; cin >> H >> W >> K; vector<string> S; fx(H) { vecps(S); } fy(W) { choco[1][y + 1] = S[0][y] == '1' ? 1 : 0; fx(H - 1) { choco[x + 2][y + 1] = choco[x + 1][y + 1]; if (S[x + 1][y] == '1') { choco[x + 2][y + 1]++; } } } int ans = INT_MAX; fx(2 << (H - 1)) { vec(B); pb(B, 0); int y = x; int i = 0; while (y > 0) { if ((y & 1) == 1) { pb(B, i + 1); } i++; y >>= 1; } pb(B, H) int sum = B.size() - 2; vector<int> SUMS(B.size() - 1); fw(W + 1) { int flag = 0; fz(B.size() - 1) { SUMS[z] += choco[B[z + 1]][w] - choco[B[z]][w]; if (w + 1 <= W && SUMS[z] + choco[B[z + 1]][w + 1] - choco[B[z]][w + 1] > K) { flag = 1; if (w == 0) { flag = 2; } break; } } if (flag > 0) { sum++; if (flag == 2) { sum = H * W; } SUMS = vector<int>(B.size() - 1, 0); } } ans = min(ans, sum); } ce(ans) return 0; }
replace
69
70
69
70
0
p02733
Python
Time Limit Exceeded
import numpy as np from itertools import product H, W, K = map(int, input().split()) G = np.array([list(map(int, list(input()))) for _ in range(H)]) ans = float("inf") for pattern in product([0, 1], repeat=H - 1): # スライスで横方向への割り方を表現する div = [0] + [i for i, p in enumerate(pattern, start=1) if p == 1] + [10] # 横方向へ割った後、各ブロックについて縦方向にホワイトチョコの数を修正 rows = [] for i in range(len(div) - 1): rows.append(np.sum(G[div[i] : div[i + 1]], axis=0)) # 縦方向にすでにK個より多いホワイトチョコが含まれている場合はどうにもならない if [r for r in rows if np.any(r > K)]: continue tmp_ans = 0 counts = [0] * len(rows) w = 0 while w < W: for r in range(len(rows)): counts[r] += rows[r][w] if any([c > K for c in counts]): counts = [0] * len(rows) w -= 1 tmp_ans += 1 w += 1 # 横に割った分を加算 tmp_ans += len(div) - 2 ans = min(ans, tmp_ans) print(ans)
import numpy as np from itertools import product H, W, K = map(int, input().split()) G = np.array([list(map(int, list(input()))) for _ in range(H)]) ans = float("inf") for pattern in product([0, 1], repeat=H - 1): # スライスで横方向への割り方を表現する div = [0] + [i for i, p in enumerate(pattern, start=1) if p == 1] + [10] # 横方向へ割った後、各ブロックについて縦方向にホワイトチョコの数を修正 rows = [] for i in range(len(div) - 1): rows.append(np.sum(G[div[i] : div[i + 1]], axis=0)) # 縦方向にすでにK個より多いホワイトチョコが含まれている場合はどうにもならない if [r for r in rows if np.any(r > K)]: continue # 縦方向に貪欲に割っていく rows = [r.tolist() for r in rows] tmp_ans = 0 counts = [0] * len(rows) w = 0 while w < W: for r in range(len(rows)): counts[r] += rows[r][w] if any([c > K for c in counts]): counts = [0] * len(rows) w -= 1 tmp_ans += 1 w += 1 # 横に割った分を加算 tmp_ans += len(div) - 2 ans = min(ans, tmp_ans) print(ans)
insert
20
20
20
22
TLE
p02733
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long int ll; int h, w, kk; char fi[15][1010]; int cnt[15][1010]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> h >> w >> kk; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> fi[i][j]; if (fi[i][j] == '1') { cnt[i + 1][j + 1] = cnt[i + 1][j] + 1; } else { cnt[i + 1][j + 1] = cnt[i + 1][j]; } } } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cnt[i][j] += cnt[i - 1][j]; } } int ans = 1e9; for (int i = 0; i < (1 << (h - 1)); i++) { int res = __builtin_popcount(i); int p = 1; for (int k = 1; k <= w; k++) { int l = 1; for (int j = 0; j < h - 1; j++) { if ((1 << j) & i) { if (cnt[j + 1][k] + cnt[l - 1][k - 1] - cnt[j + 1][k - 1] - cnt[l - 1][k] > kk) { res = 1e8; break; } if (cnt[j + 1][k] + cnt[l - 1][p - 1] - cnt[j + 1][p - 1] - cnt[l - 1][k] > kk) { if (p != k) res++; p = k; } l = i + 1; } } if (cnt[h][k] + cnt[l - 1][k - 1] - cnt[h][k - 1] - cnt[l - 1][k] > kk) { res = 1e8; break; } if (cnt[h][k] + cnt[l - 1][p - 1] - cnt[h][p - 1] - cnt[l - 1][k] > kk) { if (p != k) res++; p = k; } } /*for(int j=0;j<h-1;j++){ if((1<<j)&i){ for(int k=1;k<=w;k++){ if(cnt[j+1][k]+cnt[l-1][p-1]-cnt[j+1][p-1]-cnt[l-1][k]>kk){ res++; if(p==k){ res=1e8; break; } p=k; if(cnt[j+1][k]+cnt[l-1][p-1]-cnt[j+1][p-1]-cnt[l-1][k]>kk){ res=1e8; break; } } } l=j+2; p=1; } } for(int k=1;k<=w;k++){ if(cnt[h][k]+cnt[l-1][p-1]-cnt[h][p-1]-cnt[l-1][k]>kk){ res++; if(p==k){ res=1e8; break; } p=k-1; } }*/ ans = min(ans, res); } cout << ans << endl; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long int ll; int h, w, kk; char fi[15][1010]; int cnt[15][1010]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> h >> w >> kk; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> fi[i][j]; if (fi[i][j] == '1') { cnt[i + 1][j + 1] = cnt[i + 1][j] + 1; } else { cnt[i + 1][j + 1] = cnt[i + 1][j]; } } } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cnt[i][j] += cnt[i - 1][j]; } } int ans = 1e9; for (int i = 0; i < (1 << (h - 1)); i++) { int res = __builtin_popcount(i); int p = 1; for (int k = 1; k <= w; k++) { int l = 1; for (int j = 0; j < h - 1; j++) { if ((1 << j) & i) { if (cnt[j + 1][k] + cnt[l - 1][k - 1] - cnt[j + 1][k - 1] - cnt[l - 1][k] > kk) { res = 1e8; break; } if (cnt[j + 1][k] + cnt[l - 1][p - 1] - cnt[j + 1][p - 1] - cnt[l - 1][k] > kk) { if (p != k) res++; p = k; } l = j + 2; } } if (cnt[h][k] + cnt[l - 1][k - 1] - cnt[h][k - 1] - cnt[l - 1][k] > kk) { res = 1e8; break; } if (cnt[h][k] + cnt[l - 1][p - 1] - cnt[h][p - 1] - cnt[l - 1][k] > kk) { if (p != k) res++; p = k; } } /*for(int j=0;j<h-1;j++){ if((1<<j)&i){ for(int k=1;k<=w;k++){ if(cnt[j+1][k]+cnt[l-1][p-1]-cnt[j+1][p-1]-cnt[l-1][k]>kk){ res++; if(p==k){ res=1e8; break; } p=k; if(cnt[j+1][k]+cnt[l-1][p-1]-cnt[j+1][p-1]-cnt[l-1][k]>kk){ res=1e8; break; } } } l=j+2; p=1; } } for(int k=1;k<=w;k++){ if(cnt[h][k]+cnt[l-1][p-1]-cnt[h][p-1]-cnt[l-1][k]>kk){ res++; if(p==k){ res=1e8; break; } p=k-1; } }*/ ans = min(ans, res); } cout << ans << endl; }
replace
57
58
57
58
0
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using llong = long long int; #define rep(i, n) for (int i = 0; i < n; ++i) #define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr) #define stl_repr(itr, x) for (auto itr = x.rbegin(); itr != x.rend(); ++itr) #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() const static int MOD = 1000000007; const static int IINF = INT_MAX; const static llong LINF = LLONG_MAX; const static int dx[4] = {1, 0, -1, 0}; const static int dy[4] = {0, 1, 0, -1}; /* --------------- template start --------------- */ template <class Abel> struct BinaryIndexTree { vector<Abel> dat[2]; Abel UNITY_SUM = 0; // to be set /* [1, n] */ BinaryIndexTree(int n) { init(n); } void init(int n) { for (int iter = 0; iter < 2; ++iter) dat[iter].assign(n + 1, UNITY_SUM); } /* a, b are 1-indexed, [a, b) */ inline void sub_add(int p, int a, Abel x) { for (int i = a; i < (int)dat[p].size(); i += i & -i) dat[p][i] = dat[p][i] + x; } inline void add(int a, int b, Abel x) { sub_add(0, a, x * -(a - 1)); sub_add(1, a, x); sub_add(0, b, x * (b - 1)); sub_add(1, b, x * (-1)); } /* a is 1-indexed, [a, b) */ inline Abel sub_sum(int p, int a) { Abel res = UNITY_SUM; for (int i = a; i > 0; i -= i & -i) res = res + dat[p][i]; return res; } inline Abel sum(int a, int b) { return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) - sub_sum(1, a - 1) * (a - 1); } /* debug */ void print() { for (int i = 1; i < (int)dat[0].size(); ++i) cout << sum(i, i + 1) << ","; cout << endl; } }; template <typename T> struct UnionFind { vector<int> par; vector<T> sz; UnionFind(int n) { par.resize(n); sz.resize(n); for (int i = 0; i < n; ++i) { par[i] = i; sz[i] = 1; } } int find_root(int x) { return par[x] == x ? x : par[x] = find_root(par[x]); } bool same(int x, int y) { return find_root(x) == find_root(y); } void unite(int x, int y) { x = find_root(x); y = find_root(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; par[y] = x; } T size_of_tree(int x) { return sz[find_root(x)]; } }; /* --------------- template end --------------- */ /* --------------- function start --------------- */ llong my_gcd(llong a, llong b) { return (a ? my_gcd(b % a, a) : b); } llong my_lcm(llong a, llong b) { return a * b / my_gcd(a, b); } llong facmod(llong n) { if (n == 0) return 1; llong res = n; for (int i = n - 1; i > 0; --i) { res = (res * i) % MOD; } return res; } llong powmod(llong a, llong n) { llong res = 1; while (n > 0) { if (n & 1) res = (res * a) % MOD; a = (a * a) % MOD; n >>= 1; } return res; } llong modinv(llong n) { return powmod(n, MOD - 2); } llong combmod(llong n, llong k) { llong res = 1; for (int i = 0; i < k; ++i) { res = (res * (n - i)) % MOD; } res = (res * modinv(facmod(k))) % MOD; return res; } /* --------------- function end --------------- */ int main(int argc, char *argv[]) { cin.tie(0); ios::sync_with_stdio(false); int h, w, k; cin >> h >> w >> k; vector<string> choco(h); rep(i, h) { cin >> choco[i]; } int ans = IINF; for (int bit = 0; bit < (1 << (h - 1)); ++bit) { bool ok1 = true; vector<int> group(h, 0); int wl = 0; for (int i = 0; i < h + 1; ++i) { if (bit & (1 << i)) group[i + 1] += group[i] + 1, ++wl; else group[i + 1] = group[i]; } int hl = 0; vector<int> nums(wl + 1, 0); rep(j, w) { vector<int> tmp_nums(wl + 1, 0); bool ok2 = true; rep(i, h) { tmp_nums[group[i]] += choco[i][j] - '0'; nums[group[i]] += choco[i][j] - '0'; if (tmp_nums[group[i]] > k) ok1 = false; if (nums[group[i]] > k) ok2 = false; } if (!ok2) ++hl, nums = tmp_nums; } if (ok1) ans = min(ans, wl + hl); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using llong = long long int; #define rep(i, n) for (int i = 0; i < n; ++i) #define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr) #define stl_repr(itr, x) for (auto itr = x.rbegin(); itr != x.rend(); ++itr) #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() const static int MOD = 1000000007; const static int IINF = INT_MAX; const static llong LINF = LLONG_MAX; const static int dx[4] = {1, 0, -1, 0}; const static int dy[4] = {0, 1, 0, -1}; /* --------------- template start --------------- */ template <class Abel> struct BinaryIndexTree { vector<Abel> dat[2]; Abel UNITY_SUM = 0; // to be set /* [1, n] */ BinaryIndexTree(int n) { init(n); } void init(int n) { for (int iter = 0; iter < 2; ++iter) dat[iter].assign(n + 1, UNITY_SUM); } /* a, b are 1-indexed, [a, b) */ inline void sub_add(int p, int a, Abel x) { for (int i = a; i < (int)dat[p].size(); i += i & -i) dat[p][i] = dat[p][i] + x; } inline void add(int a, int b, Abel x) { sub_add(0, a, x * -(a - 1)); sub_add(1, a, x); sub_add(0, b, x * (b - 1)); sub_add(1, b, x * (-1)); } /* a is 1-indexed, [a, b) */ inline Abel sub_sum(int p, int a) { Abel res = UNITY_SUM; for (int i = a; i > 0; i -= i & -i) res = res + dat[p][i]; return res; } inline Abel sum(int a, int b) { return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) - sub_sum(1, a - 1) * (a - 1); } /* debug */ void print() { for (int i = 1; i < (int)dat[0].size(); ++i) cout << sum(i, i + 1) << ","; cout << endl; } }; template <typename T> struct UnionFind { vector<int> par; vector<T> sz; UnionFind(int n) { par.resize(n); sz.resize(n); for (int i = 0; i < n; ++i) { par[i] = i; sz[i] = 1; } } int find_root(int x) { return par[x] == x ? x : par[x] = find_root(par[x]); } bool same(int x, int y) { return find_root(x) == find_root(y); } void unite(int x, int y) { x = find_root(x); y = find_root(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; par[y] = x; } T size_of_tree(int x) { return sz[find_root(x)]; } }; /* --------------- template end --------------- */ /* --------------- function start --------------- */ llong my_gcd(llong a, llong b) { return (a ? my_gcd(b % a, a) : b); } llong my_lcm(llong a, llong b) { return a * b / my_gcd(a, b); } llong facmod(llong n) { if (n == 0) return 1; llong res = n; for (int i = n - 1; i > 0; --i) { res = (res * i) % MOD; } return res; } llong powmod(llong a, llong n) { llong res = 1; while (n > 0) { if (n & 1) res = (res * a) % MOD; a = (a * a) % MOD; n >>= 1; } return res; } llong modinv(llong n) { return powmod(n, MOD - 2); } llong combmod(llong n, llong k) { llong res = 1; for (int i = 0; i < k; ++i) { res = (res * (n - i)) % MOD; } res = (res * modinv(facmod(k))) % MOD; return res; } /* --------------- function end --------------- */ int main(int argc, char *argv[]) { cin.tie(0); ios::sync_with_stdio(false); int h, w, k; cin >> h >> w >> k; vector<string> choco(h); rep(i, h) { cin >> choco[i]; } int ans = IINF; for (int bit = 0; bit < (1 << (h - 1)); ++bit) { bool ok1 = true; vector<int> group(h, 0); int wl = 0; for (int i = 0; i < h - 1; ++i) { if (bit & (1 << i)) group[i + 1] += group[i] + 1, ++wl; else group[i + 1] = group[i]; } int hl = 0; vector<int> nums(wl + 1, 0); rep(j, w) { vector<int> tmp_nums(wl + 1, 0); bool ok2 = true; rep(i, h) { tmp_nums[group[i]] += choco[i][j] - '0'; nums[group[i]] += choco[i][j] - '0'; if (tmp_nums[group[i]] > k) ok1 = false; if (nums[group[i]] > k) ok2 = false; } if (!ok2) ++hl, nums = tmp_nums; } if (ok1) ans = min(ans, wl + hl); } cout << ans << endl; return 0; }
replace
146
147
146
147
0
p02733
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <queue> #include <string> #include <vector> using ll = long long; using namespace std; int rui[20][1010] = {{0}}; int main() { int h, w, k; cin >> h >> w >> k; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { char c; cin >> c; rui[i][j] = c - '0'; } } for (int i = 0; i <= h; i++) { for (int j = 0; j < w; j++) { rui[i][j + 1] += rui[i][j]; } } for (int j = 0; j <= w; j++) { for (int i = 0; i < h; i++) { rui[i + 1][j] += rui[i][j]; } } int lim = (1 << (h - 1)); int ans = h + w; for (int b = 0; b < lim; b++) { vector<int> ends; for (int i = 0; i < h - 1; i++) if (b & (1 << i)) ends.push_back(i); ends.push_back(h - 1); int sj = 0, cnt = ends.size() - 1; for (int j = 0; j < w; j++) { int si = 0; for (auto &ei : ends) { int val = rui[ei + 1][j + 1] - rui[si][j + 1] - rui[ei + 1][sj] + rui[si][sj]; cerr << ei + 1 << ' ' << j + 1 << ' ' << si << ' ' << sj << '\n'; if (val > k) { if (sj == j) cnt = h + w; sj = j; cnt++; break; } si = ei + 1; } } ans = min(ans, cnt); // cerr << cnt << ' ' << b << '\n'; } cout << ans << '\n'; return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <string> #include <vector> using ll = long long; using namespace std; int rui[20][1010] = {{0}}; int main() { int h, w, k; cin >> h >> w >> k; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { char c; cin >> c; rui[i][j] = c - '0'; } } for (int i = 0; i <= h; i++) { for (int j = 0; j < w; j++) { rui[i][j + 1] += rui[i][j]; } } for (int j = 0; j <= w; j++) { for (int i = 0; i < h; i++) { rui[i + 1][j] += rui[i][j]; } } int lim = (1 << (h - 1)); int ans = h + w; for (int b = 0; b < lim; b++) { vector<int> ends; for (int i = 0; i < h - 1; i++) if (b & (1 << i)) ends.push_back(i); ends.push_back(h - 1); int sj = 0, cnt = ends.size() - 1; for (int j = 0; j < w; j++) { int si = 0; for (auto &ei : ends) { int val = rui[ei + 1][j + 1] - rui[si][j + 1] - rui[ei + 1][sj] + rui[si][sj]; if (val > k) { if (sj == j) cnt = h + w; sj = j; cnt++; break; } si = ei + 1; } } ans = min(ans, cnt); // cerr << cnt << ' ' << b << '\n'; } cout << ans << '\n'; return 0; }
delete
46
47
46
46
TLE
p02733
Python
Time Limit Exceeded
from itertools import combinations, chain H, W, K = map(int, input().split()) Sss = [] for _ in range(H): Sss.append(list(map(int, input()))) def h_divide(Sss, div_is): result = [] for s, e in zip(chain([0], div_is), chain(div_is, [len(Sss)])): result.append(Sss[s:e]) return result def _v_end(Sss, start, K): count = 0 for i in range(start, len(Sss[0])): s = sum(map(lambda Ss: Ss[i], Sss)) if count + s > K: return i - 1 count += s return len(Sss[0]) - 1 def v_end(Ssss, start, K): result = -1 for Sss in Ssss: end = _v_end(Sss, start, K) if end < start: return -1 if result < 0 or result > end: result = end return result def v_div_count(Ssss, K): start, result = 0, 0 while True: end = v_end(Ssss, start, K) if end < 0: return -1 if end == len(Ssss[0][0]) - 1: break result += 1 start = end + 1 return result h_cands = list(range(1, len(Sss))) ans = -1 for n in range(len(h_cands) + 1): for comb in combinations(h_cands, n): Ssss = h_divide(Sss, list(comb)) cnt = v_div_count(Ssss, K) if cnt < 0: continue if ans < 0 or ans > n + cnt: ans = n + cnt print(ans)
from itertools import combinations, chain H, W, K = map(int, input().split()) Sss = [] for _ in range(H): Sss.append(list(map(int, input()))) def h_divide(Sss, div_is): result = [] for s, e in zip(chain([0], div_is), chain(div_is, [len(Sss)])): result.append(Sss[s:e]) return result def _v_end(Sss, start, K): count = 0 for i in range(start, len(Sss[0])): s = sum(map(lambda Ss: Ss[i], Sss)) if count + s > K: return i - 1 count += s return len(Sss[0]) - 1 def v_end(Ssss, start, K): result = -1 for Sss in Ssss: end = _v_end(Sss, start, K) if end < start: return -1 if result < 0 or result > end: result = end return result def v_div_count(Ssss, K): start, result = 0, 0 while True: end = v_end(Ssss, start, K) if end < 0: return -1 if end == len(Ssss[0][0]) - 1: break result += 1 start = end + 1 return result h_cands = list(range(1, len(Sss))) ans = -1 for n in range(len(h_cands) + 1): for comb in combinations(h_cands, n): if ans > 0 and n >= ans: continue Ssss = h_divide(Sss, list(comb)) cnt = v_div_count(Ssss, K) if cnt < 0: continue if ans < 0 or ans > n + cnt: ans = n + cnt print(ans)
insert
53
53
53
55
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using lint = long long; const lint inf = 1LL << 60; const lint mod = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w, k; cin >> h >> w >> k; vector<string> s(h); for (int i = 0; i < h; ++i) { cin >> s[i]; } int ret = 1e8; for (int i = 0; i < 1 << (h - 1); ++i) { int temp = __builtin_popcount(i); // holizontal cut vector<int> white(h, 0); bool ng = false; bool cut = false; for (int j = 0; j < w; ++j) { int row = 0; for (int l = 0; l < h; ++l) { if (s[l][j] == '1') { white[row]++; if (white[row] > k) { cut = true; } } if (1 << l & i) { row++; } } if (ng) break; if (cut) { // vertical cut temp++; cut = false; white.assign(h, 0); for (int l = 0; l < h; ++l) { if (s[l][j] == '1') { white[row]++; if (white[row] > k) ng = true; } if (1 << l & i) { row++; } } } if (ng) break; } if (!ng) chmin(ret, temp); } cout << ret << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using lint = long long; const lint inf = 1LL << 60; const lint mod = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w, k; cin >> h >> w >> k; vector<string> s(h); for (int i = 0; i < h; ++i) { cin >> s[i]; } int ret = 1e8; for (int i = 0; i < 1 << (h - 1); ++i) { int temp = __builtin_popcount(i); // holizontal cut vector<int> white(h, 0); bool ng = false; bool cut = false; for (int j = 0; j < w; ++j) { int row = 0; for (int l = 0; l < h; ++l) { if (s[l][j] == '1') { white[row]++; if (white[row] > k) { cut = true; } } if (1 << l & i) { row++; } } if (ng) break; if (cut) { // vertical cut temp++; cut = false; white.assign(h, 0); row = 0; for (int l = 0; l < h; ++l) { if (s[l][j] == '1') { white[row]++; if (white[row] > k) ng = true; } if (1 << l & i) { row++; } } } if (ng) break; } if (!ng) chmin(ret, temp); } cout << ret << "\n"; return 0; }
insert
47
47
47
48
0
p02733
C++
Runtime Error
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define eb emplace_back using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll MOD = 1e9 + 7; template <class T> void chmin(T &a, T b) { a = min(a, b); } template <class T> void chmax(T &a, T b) { a = max(a, b); } void ok() { cerr << "ok" << endl; } int main() { cin.tie(0); ios::sync_with_stdio(0); int h, w, k; cin >> h >> w >> k; vector<string> c(h); for (int i = 0; i < h; i++) { cin >> c[i]; } int res = INF; for (int i = 0; i < (1 << (h - 1)); i++) { vector<int> co(h); int s = __builtin_popcount(i); bool ff = true; for (int j = 0; j < w; j++) { bool f = false; int id = 0; for (int l = 0; l < h; l++) { co[id] += (c[l][j] == '1'); if (co[id] > k) { s++; for (int m = 0; m < h; m++) { co[m] = 0; } f = true; break; } if ((i >> l) & 1) { id++; } } if (f) { for (int l = 0; l < h; l++) { co[id] += (c[l][j] == '1'); if (co[id] > k) { ff = false; break; } if ((i >> l) & 1) { id++; } } } } if (ff) { chmin(res, s); } } cout << res << '\n'; }
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define eb emplace_back using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll MOD = 1e9 + 7; template <class T> void chmin(T &a, T b) { a = min(a, b); } template <class T> void chmax(T &a, T b) { a = max(a, b); } void ok() { cerr << "ok" << endl; } int main() { cin.tie(0); ios::sync_with_stdio(0); int h, w, k; cin >> h >> w >> k; vector<string> c(h); for (int i = 0; i < h; i++) { cin >> c[i]; } int res = INF; for (int i = 0; i < (1 << (h - 1)); i++) { vector<int> co(h); int s = __builtin_popcount(i); bool ff = true; for (int j = 0; j < w; j++) { bool f = false; int id = 0; for (int l = 0; l < h; l++) { co[id] += (c[l][j] == '1'); if (co[id] > k) { s++; for (int m = 0; m < h; m++) { co[m] = 0; } f = true; break; } if ((i >> l) & 1) { id++; } } if (f) { id = 0; for (int l = 0; l < h; l++) { co[id] += (c[l][j] == '1'); if (co[id] > k) { ff = false; break; } if ((i >> l) & 1) { id++; } } } } if (ff) { chmin(res, s); } } cout << res << '\n'; }
insert
43
43
43
44
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; /*-------------------Define Start-------------------*/ typedef bool BL; // 布尔类型 typedef char SB; // 有符号1字节,8位 typedef unsigned char UB; // 无符号1字节,8位 typedef short SW; // 有符号短整型,16位 typedef unsigned short UW; // 无符号短整型,16位 typedef long SDW; // 有符号整型,32位 typedef unsigned long UDW; // 无符号整型,32位 typedef long long SLL; // 有符号长整型,64位 typedef unsigned long long ULL; // 无符号长整型,64位 typedef char CH; // 单个字符 typedef float R32; // 单精度浮点数 typedef double R64; // 双精度浮点数 #define Rep(i, n) for (register SDW i = 0; i < (n); ++i) #define For(i, s, t) for (register SDW i = (s); i <= (t); ++i) #define rFor(i, t, s) for (register SDW i = (t); i >= (s); --i) #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) #define ms0(a) memset(a, 0, sizeof(a)) #define msI(a) memset(a, 0x7f, sizeof(a)) #define LOWBIT(x) ((x) & (-x)) #define MP make_pair #define PB push_back #define ft first #define sd second #define pr(x) cout << #x << " = " << x << " " #define prln(x) cout << #x << " = " << x << endl const ULL mod = 1e9 + 7; // 常用模数(可根据题目需要修改) const ULL inf = 0x7fffffff; // 用来表示无限大 const ULL infLL = 0x7fffffffffffffffLL; // 用来表示无限大 /*-------------------Define End-------------------*/ const UDW maxN = 1e3 + 7; SDW H, W, K; SDW S[13][maxN]; SDW ans = inf; SDW getSum(SDW x1, SDW y1, SDW x2, SDW y2) { return S[x2][y2] - S[x2][y1 - 1] - S[x1 - 1][y2] + S[x1 - 1][y1 - 1]; } void input() { cin >> H >> W >> K; For(i, 1, H) { For(j, 1, W) { scanf("%1d", &S[i][j]); S[i][j] += S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1]; // 记录为前缀和,方便计算 } } } void solve() { // 枚举水平方向上的所有切法 Rep(i, 1 << (H - 1)) { // 在竖直方向上采用贪心切法 vector<SDW> pos; // 记录水平切的位置 SDW tmp = i; SDW ret = __builtin_popcount(i); pos.PB(0); while (tmp) { SDW x = LOWBIT(tmp); tmp &= tmp - 1; pos.PB(__builtin_ctz(x) + 1); } pos.PB(H); SDW a = 1, b = 1; while (b <= W) { For(j, 1, pos.size() - 1) { if (getSum(pos[j - 1] + 1, a, pos[j], b) > K) { ++ret; a = b; --b; break; } } ++b; } ans = min(ans, ret); } } void output() { cout << ans << endl; } int main() { input(); solve(); output(); return 0; }
#include <bits/stdc++.h> using namespace std; /*-------------------Define Start-------------------*/ typedef bool BL; // 布尔类型 typedef char SB; // 有符号1字节,8位 typedef unsigned char UB; // 无符号1字节,8位 typedef short SW; // 有符号短整型,16位 typedef unsigned short UW; // 无符号短整型,16位 typedef long SDW; // 有符号整型,32位 typedef unsigned long UDW; // 无符号整型,32位 typedef long long SLL; // 有符号长整型,64位 typedef unsigned long long ULL; // 无符号长整型,64位 typedef char CH; // 单个字符 typedef float R32; // 单精度浮点数 typedef double R64; // 双精度浮点数 #define Rep(i, n) for (register SDW i = 0; i < (n); ++i) #define For(i, s, t) for (register SDW i = (s); i <= (t); ++i) #define rFor(i, t, s) for (register SDW i = (t); i >= (s); --i) #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) #define ms0(a) memset(a, 0, sizeof(a)) #define msI(a) memset(a, 0x7f, sizeof(a)) #define LOWBIT(x) ((x) & (-x)) #define MP make_pair #define PB push_back #define ft first #define sd second #define pr(x) cout << #x << " = " << x << " " #define prln(x) cout << #x << " = " << x << endl const ULL mod = 1e9 + 7; // 常用模数(可根据题目需要修改) const ULL inf = 0x7fffffff; // 用来表示无限大 const ULL infLL = 0x7fffffffffffffffLL; // 用来表示无限大 /*-------------------Define End-------------------*/ const UDW maxN = 1e3 + 7; SDW H, W, K; SDW S[13][maxN]; SDW ans = inf; SDW getSum(SDW x1, SDW y1, SDW x2, SDW y2) { return S[x2][y2] - S[x2][y1 - 1] - S[x1 - 1][y2] + S[x1 - 1][y1 - 1]; } void input() { cin >> H >> W >> K; For(i, 1, H) { For(j, 1, W) { scanf("%1d", &S[i][j]); S[i][j] += S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1]; // 记录为前缀和,方便计算 } } } void solve() { // 枚举水平方向上的所有切法 Rep(i, 1 << (H - 1)) { // 在竖直方向上采用贪心切法 vector<SDW> pos; // 记录水平切的位置 SDW tmp = i; SDW ret = __builtin_popcount(i); pos.PB(0); while (tmp) { SDW x = LOWBIT(tmp); tmp &= tmp - 1; pos.PB(__builtin_ctz(x) + 1); } pos.PB(H); SDW a = 1, b = 1; while (b <= W) { For(j, 1, pos.size() - 1) { if (getSum(pos[j - 1] + 1, a, pos[j], b) > K) { if (a == b) { // 无法切开的情况,直接跳出 b = W + 1; ret = inf; break; } else { ++ret; a = b; --b; break; } } } ++b; } ans = min(ans, ret); } } void output() { cout << ans << endl; } int main() { input(); solve(); output(); return 0; }
replace
80
84
80
90
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int h, w, k; cin >> h >> w >> k; vector<string> s(h); for (int i = 0; i < h; i++) cin >> s[i]; int answer = h + w; for (int mask = 0; mask < (1 << (h - 1)); mask++) { int block = __builtin_popcount(mask) + 1; int cut = block - 1; vector<int> count(block, 0); for (int i = 0; i < w; i++) { int block_num = 0; vector<int> count_new(block, 0); for (int j = 0; j < h; j++) { count_new[block_num] += s[j][i] - '0'; block_num += mask & (1 << j); } bool possible = true; for (int j = 0; j < block; j++) { if (count_new[j] > k) { possible = false; break; } } if (!possible) { cut = h + w; break; } bool pass = true; for (int j = 0; j < block; j++) { if (count[j] + count_new[j] > k) { pass = false; break; } } if (pass) { for (int j = 0; j < block; j++) count[j] += count_new[j]; } else { if (i == 0) { cut = h + w; break; } count = count_new; cut += 1; } } answer = min(answer, cut); } cout << answer << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int h, w, k; cin >> h >> w >> k; vector<string> s(h); for (int i = 0; i < h; i++) cin >> s[i]; int answer = h + w; for (int mask = 0; mask < (1 << (h - 1)); mask++) { int block = __builtin_popcount(mask) + 1; int cut = block - 1; vector<int> count(block, 0); for (int i = 0; i < w; i++) { int block_num = 0; vector<int> count_new(block, 0); for (int j = 0; j < h; j++) { count_new[block_num] += s[j][i] - '0'; block_num += (mask & 1 << j) > 0 ? 1 : 0; } bool possible = true; for (int j = 0; j < block; j++) { if (count_new[j] > k) { possible = false; break; } } if (!possible) { cut = h + w; break; } bool pass = true; for (int j = 0; j < block; j++) { if (count[j] + count_new[j] > k) { pass = false; break; } } if (pass) { for (int j = 0; j < block; j++) count[j] += count_new[j]; } else { if (i == 0) { cut = h + w; break; } count = count_new; cut += 1; } } answer = min(answer, cut); } cout << answer << endl; return 0; }
replace
20
21
20
21
0
p02733
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define MOD 1000000007 #define MAX 100005 #define pb push_back #define mp make_pair #define pii pair<int, int> #define mii map<int, int> #define vi vector<int> #define ff first #define ss second #define T \ int t; \ cin >> t; \ while (t--) #define loop(i, a, b) for (int i = a; i < b; i++) #define umi unordered_map<int, int> #define maxheap priority_queue<int> #define minheap priority_queue<int, vector<int>, greater<int>> #define inp(n) for (int i = 0; i < n; i++) #define N 200005 #define mxN 2000005 #define all(v) v.begin(), v.end() #define lb(v, val) (int)(lower_bound(v.begin(), v.end(), val) - v.begin()) #define ub(v, val) (int)(upper_bound(v.begin(), v.end(), val) - v.begin()) using namespace std; using namespace std::chrono; void solve() { int h, w, K; cin >> h >> w >> K; char a[h + 1][w + 1]; inp(h) loop(j, 0, w) cin >> a[i][j]; int output = INT_MAX; for (int mask = (1 << (h - 1)); mask < (1 << h); mask++) { bool possible = true; // cout << mask << ' '; int k, cut = 0; vi ans(10, 0), cur(10, 0); for (int j = 0; j < w; j++) { inp(10) cur[i] = 0; k = 0; for (int i = 0; i < h; i++) { if (a[i][j] == '1') { cur[k]++; ans[k]++; } if (mask & (1 << i)) { k++; } } for (int i = 0; i < k; i++) { if (cur[i] > K) { possible = false; break; } } for (int i = 0; i < k; i++) { if (ans[i] > K) { cut++; for (int x = 0; x < k; x++) { ans[x] = cur[x]; } break; } } if (!possible) break; } if (possible) output = min(output, cut + __builtin_popcount(mask) - 1); // cout << cut << '\n'; } cout << output; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> #define int long long #define MOD 1000000007 #define MAX 100005 #define pb push_back #define mp make_pair #define pii pair<int, int> #define mii map<int, int> #define vi vector<int> #define ff first #define ss second #define T \ int t; \ cin >> t; \ while (t--) #define loop(i, a, b) for (int i = a; i < b; i++) #define umi unordered_map<int, int> #define maxheap priority_queue<int> #define minheap priority_queue<int, vector<int>, greater<int>> #define inp(n) for (int i = 0; i < n; i++) #define N 200005 #define mxN 2000005 #define all(v) v.begin(), v.end() #define lb(v, val) (int)(lower_bound(v.begin(), v.end(), val) - v.begin()) #define ub(v, val) (int)(upper_bound(v.begin(), v.end(), val) - v.begin()) using namespace std; using namespace std::chrono; void solve() { int h, w, K; cin >> h >> w >> K; char a[h + 1][w + 1]; inp(h) loop(j, 0, w) cin >> a[i][j]; int output = INT_MAX; for (int mask = (1 << (h - 1)); mask < (1 << h); mask++) { bool possible = true; // cout << mask << ' '; int k, cut = 0; vi ans(10, 0), cur(10, 0); for (int j = 0; j < w; j++) { inp(10) cur[i] = 0; k = 0; for (int i = 0; i < h; i++) { if (a[i][j] == '1') { cur[k]++; ans[k]++; } if (mask & (1 << i)) { k++; } } for (int i = 0; i < k; i++) { if (cur[i] > K) { possible = false; break; } } for (int i = 0; i < k; i++) { if (ans[i] > K) { cut++; for (int x = 0; x < k; x++) { ans[x] = cur[x]; } break; } } if (!possible) break; } if (possible) output = min(output, cut + __builtin_popcount(mask) - 1); // cout << cut << '\n'; } cout << output; } signed main() { /*#ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif */ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } }
replace
79
83
79
83
-11
p02733
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define llint long long #define inf 1e18 #define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++) using namespace std; llint h, w, k; char c[15][1005]; llint sum[15], nsum[15]; // llint sum[15][1005]; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> h >> w >> k; for (int y = 0; y < h; y++) { for (int x = 1; x <= w; x++) { cin >> c[x][y]; } } /*for(int y = 1; y <= h; y++){ for(int x = 1; x <= w; x++){ sum[x][y] = sum[x-1][y] + sum[x][y-1] - sum[x-1][y-1] + (c[x][y]-'0'); } }*/ llint H = 1 << (h - 1), ans = inf; for (int i = 0; i < H; i++) { llint cnt = 0; for (int y = 0; y < h - 1; y++) { if (i & (1 << y)) cnt++; } for (int y = 0; y < h; y++) sum[y] = 0; for (int x = 1; x <= w; x++) { llint p = 0; for (int y = 0; y < h; y++) nsum[y] = 0; for (int y = 0; y < h; y++) { nsum[p] += c[x][y] - '0'; if (i & (1 << y)) p = y + 1; } bool flag = true; for (int y = 0; y < h; y++) { if (nsum[y] > k) goto end; if (sum[y] + nsum[y] > k) flag = false; } if (flag) { for (int y = 0; y < h; y++) sum[y] += nsum[y]; } else { for (int y = 0; y < h; y++) sum[y] = nsum[y]; cnt++; } } ans = min(ans, cnt); end:; } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define llint long long #define inf 1e18 #define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++) using namespace std; llint h, w, k; char c[1005][15]; llint sum[15], nsum[15]; // llint sum[15][1005]; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> h >> w >> k; for (int y = 0; y < h; y++) { for (int x = 1; x <= w; x++) { cin >> c[x][y]; } } /*for(int y = 1; y <= h; y++){ for(int x = 1; x <= w; x++){ sum[x][y] = sum[x-1][y] + sum[x][y-1] - sum[x-1][y-1] + (c[x][y]-'0'); } }*/ llint H = 1 << (h - 1), ans = inf; for (int i = 0; i < H; i++) { llint cnt = 0; for (int y = 0; y < h - 1; y++) { if (i & (1 << y)) cnt++; } for (int y = 0; y < h; y++) sum[y] = 0; for (int x = 1; x <= w; x++) { llint p = 0; for (int y = 0; y < h; y++) nsum[y] = 0; for (int y = 0; y < h; y++) { nsum[p] += c[x][y] - '0'; if (i & (1 << y)) p = y + 1; } bool flag = true; for (int y = 0; y < h; y++) { if (nsum[y] > k) goto end; if (sum[y] + nsum[y] > k) flag = false; } if (flag) { for (int y = 0; y < h; y++) sum[y] += nsum[y]; } else { for (int y = 0; y < h; y++) sum[y] = nsum[y]; cnt++; } } ans = min(ans, cnt); end:; } cout << ans << endl; return 0; }
replace
24
25
24
25
0
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, m, k; char grid[11][110]; int dp[11][110]; int ans; bool check(int l, int r, int a, int b) { return dp[a][b] - dp[l - 1][b] - dp[a][r - 1] + dp[l - 1][r - 1] <= k; } vector<int> pos = {1}; bool testC(int last, int j) { bool fl = true; for (int i = 0; i < pos.size() - 1; i++) { int r1 = pos[i]; int r2 = pos[i + 1] - 1; if (!check(r1, last, r2, j)) return false; } return true; } void gen(int cur) { if (cur == n + 1) { pos.push_back(n + 1); int cnt = 0, last = 1; bool ok = true; for (int j = 1; j <= m and ok; j++) { if (testC(last, j)) continue; if (j - 1 >= last and testC(last, j - 1)) { cnt++; last = j; continue; } ok = false; } if (ok) ans = min(ans, (int)pos.size() - 2 + cnt); pos.pop_back(); return; } gen(cur + 1); pos.push_back(cur); gen(cur + 1); pos.pop_back(); } int32_t main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> grid[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + (grid[i][j] == '1'); ans = (n - 1) * (m - 1); gen(2); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, k; char grid[11][1010]; int dp[11][1010]; int ans; bool check(int l, int r, int a, int b) { return dp[a][b] - dp[l - 1][b] - dp[a][r - 1] + dp[l - 1][r - 1] <= k; } vector<int> pos = {1}; bool testC(int last, int j) { bool fl = true; for (int i = 0; i < pos.size() - 1; i++) { int r1 = pos[i]; int r2 = pos[i + 1] - 1; if (!check(r1, last, r2, j)) return false; } return true; } void gen(int cur) { if (cur == n + 1) { pos.push_back(n + 1); int cnt = 0, last = 1; bool ok = true; for (int j = 1; j <= m and ok; j++) { if (testC(last, j)) continue; if (j - 1 >= last and testC(last, j - 1)) { cnt++; last = j; continue; } ok = false; } if (ok) ans = min(ans, (int)pos.size() - 2 + cnt); pos.pop_back(); return; } gen(cur + 1); pos.push_back(cur); gen(cur + 1); pos.pop_back(); } int32_t main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> grid[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + (grid[i][j] == '1'); ans = (n - 1) * (m - 1); gen(2); cout << ans << endl; return 0; }
replace
4
6
4
6
0
p02733
C++
Runtime Error
// header #ifdef LOCAL #include "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 ld = long double; typedef pair<ll, ll> Pl; typedef pair<int, int> Pi; typedef vector<ll> vl; typedef vector<int> vi; template <typename T> using mat = vector<vector<T>>; 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 rev(i, n) for (int i = n - 1; i >= 0; i--) #define SZ(x) ((ll)(x).size()) #define pb(x) push_back(x) #define eb(x) emplace_back(x) #define ff first #define ss second #define mp make_pair #define print(x) cout << x << endl // functions ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } 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); } // 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>>; // constant #define INF 1000000000000000LL #define mod 1000000007LL typedef modint<mod> mint; #define eps 0.0000000000008 // library // main int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); int h, w, k; cin >> h >> w >> k; mat<int> s(h, vi(w)); rep(i, h) rep(j, w) { char x; cin >> x; s[i][j] = x - '0'; } mat<int> cumsum(h + 1, vi(w)); rep(i, h) rep(j, w) cumsum[i][j + 1] = cumsum[i][j] + s[i][j]; int ans = h + w; rep(s, 1 << (h - 1), 1 << h) { int now = 0; int cnt = 0; bool flag = true; rep(i, w) { int res = 0; rep(j, h) { res += cumsum[j][i + 1] - cumsum[j][now]; if ((1 << j) & s) { if (res > k) { if (now == i) flag = false; now = i; cnt++; } res = 0; } } } if (not flag) continue; rep(i, h - 1) { if ((1 << i) & s) cnt++; } chmin(ans, cnt); } print(ans); }
// header #ifdef LOCAL #include "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 ld = long double; typedef pair<ll, ll> Pl; typedef pair<int, int> Pi; typedef vector<ll> vl; typedef vector<int> vi; template <typename T> using mat = vector<vector<T>>; 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 rev(i, n) for (int i = n - 1; i >= 0; i--) #define SZ(x) ((ll)(x).size()) #define pb(x) push_back(x) #define eb(x) emplace_back(x) #define ff first #define ss second #define mp make_pair #define print(x) cout << x << endl // functions ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } 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); } // 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>>; // constant #define INF 1000000000000000LL #define mod 1000000007LL typedef modint<mod> mint; #define eps 0.0000000000008 // library // main int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); int h, w, k; cin >> h >> w >> k; mat<int> s(h, vi(w)); rep(i, h) rep(j, w) { char x; cin >> x; s[i][j] = x - '0'; } mat<int> cumsum(h, vi(w + 1)); rep(i, h) rep(j, w) cumsum[i][j + 1] = cumsum[i][j] + s[i][j]; int ans = h + w; rep(s, 1 << (h - 1), 1 << h) { int now = 0; int cnt = 0; bool flag = true; rep(i, w) { int res = 0; rep(j, h) { res += cumsum[j][i + 1] - cumsum[j][now]; if ((1 << j) & s) { if (res > k) { if (now == i) flag = false; now = i; cnt++; } res = 0; } } } if (not flag) continue; rep(i, h - 1) { if ((1 << i) & s) cnt++; } chmin(ans, cnt); } print(ans); }
replace
191
192
191
192
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, a) for (int i = 0; i < (int)(a); i++) typedef long long ll; const int inf = 1001001001; int main() { int h, w, k; cin >> h >> w >> k; vector<string> s(h); rep(i, h) cin >> s[i]; vector<vector<int>> d(h, vector<int>(w, 0)); rep(i, h) rep(j, w) { int t = 0; if (s[i][j] == '1') t = 1; if (j == 0) d[i][j] = t; else d[i][j] = d[i][j - 1] + t; } rep(i, h - 1) rep(j, w) { d[i + 1][j] += d[i][j]; } ll ans = 1001001001; for (int bit = 0; bit < 1 << (h - 1); bit++) { ll tmp = __builtin_popcount(bit); int r = 0; int l = 0; while (r < w) { int t = 0; int b = 0; bool flg = false; while (t < h) { while (((bit >> b) & 1) == 0 && b < h - 1) b++; int x = 0; if (t == 0) { if (l == 0) { x = d[b][r]; } else { x = d[b][r] - d[b][l - 1]; } } else { if (l == 0) { x = d[b][r] - d[t - 1][r]; } else { x = d[b][r] - d[t - 1][r] - d[b][l - 1] + d[t - 1][l - 1]; } } if (x > k) { flg = true; break; } t = b + 1; b = t; } if (flg) { tmp++; l = r; } else { r++; } } ans = min(ans, tmp); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a) for (int i = 0; i < (int)(a); i++) typedef long long ll; const int inf = 1001001001; int main() { int h, w, k; cin >> h >> w >> k; vector<string> s(h); rep(i, h) cin >> s[i]; vector<vector<int>> d(h, vector<int>(w, 0)); rep(i, h) rep(j, w) { int t = 0; if (s[i][j] == '1') t = 1; if (j == 0) d[i][j] = t; else d[i][j] = d[i][j - 1] + t; } rep(i, h - 1) rep(j, w) { d[i + 1][j] += d[i][j]; } ll ans = 1001001001; for (int bit = 0; bit < 1 << (h - 1); bit++) { ll tmp = __builtin_popcount(bit); int r = 0; int l = 0; while (r < w) { int t = 0; int b = 0; bool flg = false; while (t < h) { while (((bit >> b) & 1) == 0 && b < h - 1) b++; int x = 0; if (t == 0) { if (l == 0) { x = d[b][r]; } else { x = d[b][r] - d[b][l - 1]; } } else { if (l == 0) { x = d[b][r] - d[t - 1][r]; } else { x = d[b][r] - d[t - 1][r] - d[b][l - 1] + d[t - 1][l - 1]; } } if (x > k) { flg = true; break; } t = b + 1; b = t; } if (flg) { tmp++; if (r == l) { tmp = 1001001001; break; } l = r; } else { r++; } } ans = min(ans, tmp); } cout << ans << endl; return 0; }
insert
57
57
57
61
TLE
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int H, W, K; scanf("%d %d %d", &H, &W, &K); vector<vector<char>> a(H, vector<char>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) cin >> a[i][j]; } int ans = 1e9; for (int bit = 0; bit < (1 << (H - 1)); bit++) { int num = __builtin_popcount(bit); vector<int> data(num + 1, 0); int ans1 = 0; bool check = true; for (int i = 0; i < W; i++) { int cnt = 0; for (int j = 0; j < H; j++) { if (a[j][i] == '1') data[cnt]++; if (bit & (1 << j)) cnt++; } if (*max_element(data.begin(), data.end()) > K) check = false; for (int j = 0; j <= num; j++) data[j] = 0; } if (!check) ans1 = 1e9; for (int i = 0; i < W; i++) { int cnt = 0; for (int j = 0; j < H; j++) { if (a[j][i] == '1') data[cnt]++; if (bit & (1 << j)) cnt++; } if (*max_element(data.begin(), data.end()) > K) { ans1++; for (int j = 0; j <= num; j++) data[j] = 0; --i; } } ans = min(ans, ans1 + num); } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int H, W, K; scanf("%d %d %d", &H, &W, &K); vector<vector<char>> a(H, vector<char>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) cin >> a[i][j]; } int ans = 1e9; for (int bit = 0; bit < (1 << (H - 1)); bit++) { int num = __builtin_popcount(bit); vector<int> data(num + 1, 0); int ans1 = 0; bool check = true; for (int i = 0; i < W; i++) { int cnt = 0; for (int j = 0; j < H; j++) { if (a[j][i] == '1') data[cnt]++; if (bit & (1 << j)) cnt++; } if (*max_element(data.begin(), data.end()) > K) check = false; for (int j = 0; j <= num; j++) data[j] = 0; } if (!check) continue; for (int i = 0; i < W; i++) { int cnt = 0; for (int j = 0; j < H; j++) { if (a[j][i] == '1') data[cnt]++; if (bit & (1 << j)) cnt++; } if (*max_element(data.begin(), data.end()) > K) { ans1++; for (int j = 0; j <= num; j++) data[j] = 0; --i; } } ans = min(ans, ans1 + num); } printf("%d\n", ans); }
replace
33
34
33
34
TLE
p02733
C++
Time Limit Exceeded
// warm heart, wagging tail,and a smile just for you! // ███████████ // ███╬╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬████╬╬╬╬╬╬███ // ███████████ // ██╬╬╬╬╬████╬╬████╬╬╬╬╬██ // █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██ // ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██ // ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██ // ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████ // █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██ // ██████████████ // ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████ // ███████ █████ // ███████████████████ // #include "bits/stdc++.h" using namespace std; #define INF (1 << 30) #define LINF (1LL << 60) #define fs first #define sc second #define int long long #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i) #define REP(i, n) FOR(i, 0, (n)) #define RREP(i, n) RFOR(i, 0, (n)) #define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr) #define range(i, a, b) ((a) <= (i) && (i) < (b)) #define debug(x) cout << #x << " = " << (x) << endl #define SP << " " << template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } #define MSB(x) (63 - __builtin_clzll(x)) #define pcnt(x) (__builtin_popcountll(x)) #define parity(i, j) (i & (1LL << j)) typedef pair<int, int> P; typedef tuple<int, int, int> T; typedef vector<int> vec; typedef vector<vector<int>> mat; signed main() { ios::sync_with_stdio(false); cin.tie(0); int h, w, x; cin >> h >> w >> x; vector<string> s(h); REP(i, h) cin >> s[i]; mat sum(h + 1, vec(w + 1, 0)); REP(i, h) REP(j, w) sum[i + 1][j + 1] += s[i][j] - '0'; REP(i, h) REP(j, w + 1) sum[i + 1][j] += sum[i][j]; REP(i, h + 1) REP(j, w) sum[i][j + 1] += sum[i][j]; // REP(i,h+1){ // REP(j,w+1) cout << sum[i][j] << " "; // cout << endl; // } int ans = INF; REP(i, 1 << (h - 1)) { int cnt = pcnt(i); vec list = {0}; REP(j, h - 1) if (parity(i, j)) list.push_back(j + 1); list.push_back(h); int l = 0; REP(j, w) { bool flag = false; REP(k, list.size() - 1) { int u = list[k], v = list[k + 1]; if (sum[v][j + 1] + sum[u][l] - sum[v][l] - sum[u][j + 1] > x) flag = true; } if (flag) cnt++, l = j, j--; } // debug(cnt); chmin(ans, cnt); } cout << ans << endl; return 0; }
// warm heart, wagging tail,and a smile just for you! // ███████████ // ███╬╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬████╬╬╬╬╬╬███ // ███████████ // ██╬╬╬╬╬████╬╬████╬╬╬╬╬██ // █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██ // ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██ // ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██ // ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████ // █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██ // ██████████████ // ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████ // ███████ █████ // ███████████████████ // #include "bits/stdc++.h" using namespace std; #define INF (1 << 30) #define LINF (1LL << 60) #define fs first #define sc second #define int long long #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i) #define REP(i, n) FOR(i, 0, (n)) #define RREP(i, n) RFOR(i, 0, (n)) #define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr) #define range(i, a, b) ((a) <= (i) && (i) < (b)) #define debug(x) cout << #x << " = " << (x) << endl #define SP << " " << template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } #define MSB(x) (63 - __builtin_clzll(x)) #define pcnt(x) (__builtin_popcountll(x)) #define parity(i, j) (i & (1LL << j)) typedef pair<int, int> P; typedef tuple<int, int, int> T; typedef vector<int> vec; typedef vector<vector<int>> mat; signed main() { ios::sync_with_stdio(false); cin.tie(0); int h, w, x; cin >> h >> w >> x; vector<string> s(h); REP(i, h) cin >> s[i]; mat sum(h + 1, vec(w + 1, 0)); REP(i, h) REP(j, w) sum[i + 1][j + 1] += s[i][j] - '0'; REP(i, h) REP(j, w + 1) sum[i + 1][j] += sum[i][j]; REP(i, h + 1) REP(j, w) sum[i][j + 1] += sum[i][j]; // REP(i,h+1){ // REP(j,w+1) cout << sum[i][j] << " "; // cout << endl; // } int ans = INF; REP(i, 1 << (h - 1)) { int cnt = pcnt(i); vec list = {0}; REP(j, h - 1) if (parity(i, j)) list.push_back(j + 1); list.push_back(h); int l = 0; REP(j, w) { bool flag = false; REP(k, list.size() - 1) { int u = list[k], v = list[k + 1]; if (sum[v][j + 1] + sum[u][l] - sum[v][l] - sum[u][j + 1] > x) flag = true; } if (flag) cnt++, l = j, j--; if (cnt > h + w) break; } // debug(cnt); chmin(ans, cnt); } cout << ans << endl; return 0; }
insert
94
94
94
96
TLE
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vb = vector<bool>; using vvb = vector<vb>; using vc = vector<char>; using vvc = vector<vc>; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; using vpii = vector<pii>; using vpli = vector<pli>; using vpll = vector<pll>; #define rep(i, n) for (int i = 0; i < (int)n; i++) #define rep2(i, s, n) for (int i = (s); i < (int)n; i++) int INF = 1e9 + 7; vvi side; int H, W, K; void dfs(vi v, int i) { if (i == H) { v.push_back(H); side.push_back(v); return; } dfs(v, i + 1); v.push_back(i); dfs(v, i + 1); } int main() { cin >> H >> W >> K; vvc S(H, vc(W)); rep(h, H) { rep(w, W) cin >> S[h][w]; } vector<vvi> sum(H, vvi(H, vi(W + 1))); rep(h1, H) { rep2(h2, h1, H) { rep(w, W) { int count = 0; rep2(h, h1, h2 + 1) count += S[h][w] - '0'; sum[h1][h2][w + 1] = sum[h1][h2][w] + count; } } } dfs({0}, 1); int ans = INF; rep(i, side.size()) { int count = side[i].size() - 2, l = 0; while (true) { bool flag = true; int r = l; while (flag && r < W) { r++; rep(j, side[i].size() - 1) flag &= sum[side[i][j]][side[i][j + 1] - 1][r] - sum[side[i][j]][side[i][j + 1] - 1][l] <= K; } l = r - 1; if (!flag) count++; if (r == W) break; } ans = min(ans, count); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vb = vector<bool>; using vvb = vector<vb>; using vc = vector<char>; using vvc = vector<vc>; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; using vpii = vector<pii>; using vpli = vector<pli>; using vpll = vector<pll>; #define rep(i, n) for (int i = 0; i < (int)n; i++) #define rep2(i, s, n) for (int i = (s); i < (int)n; i++) int INF = 1e9 + 7; vvi side; int H, W, K; void dfs(vi v, int i) { if (i == H) { v.push_back(H); side.push_back(v); return; } dfs(v, i + 1); v.push_back(i); dfs(v, i + 1); } int main() { cin >> H >> W >> K; vvc S(H, vc(W)); rep(h, H) { rep(w, W) cin >> S[h][w]; } vector<vvi> sum(H, vvi(H, vi(W + 1))); rep(h1, H) { rep2(h2, h1, H) { rep(w, W) { int count = 0; rep2(h, h1, h2 + 1) count += S[h][w] - '0'; sum[h1][h2][w + 1] = sum[h1][h2][w] + count; } } } dfs({0}, 1); int ans = INF; rep(i, side.size()) { int count = side[i].size() - 2, l = 0; while (true) { bool flag = true; int r = l; while (flag && r < W) { r++; rep(j, side[i].size() - 1) flag &= sum[side[i][j]][side[i][j + 1] - 1][r] - sum[side[i][j]][side[i][j + 1] - 1][l] <= K; } if (r == l + 1) { count = INF; break; } l = r - 1; if (!flag) count++; if (r == W) break; } ans = min(ans, count); } cout << ans << endl; }
insert
66
66
66
70
TLE
p02733
C++
Runtime Error
#include <cassert> #include <iostream> #include <vector> using namespace std; int H, W, K; vector<string> s; template <typename T> void print_vec(vector<T> &v) { for (auto i : v) cout << i << " "; cout << "\n"; } vector<int> cnt_whi(vector<int> &a, int w, int I) { int idx = 0; vector<int> res(__builtin_popcount(I) + 1, 0); for (int i = 0; i < H; i++) { if (s[i][w] == '1') a[idx]++, res[idx]++; if ((I & (1 << i)) != 0) idx++; } return res; } int op_count(int I) { int n = __builtin_popcount(I); int res = n; vector<int> cnt(n + 1, 0); for (int i = 0; i < W; i++) { auto v = cnt_whi(cnt, i, I); // print_vec(cnt); for (auto it : v) if (it >= K) return 1 << 29; bool d = false; for (auto it : cnt) if (it > K) d = true; if (d) { res++; swap(v, cnt); } } return res; } int main() { cin >> H >> W >> K; s.resize(H); for (int i = 0; i < H; i++) cin >> s[i]; int ans = 1 << 29; for (int i = 0; i < (1 << (H - 1)); i++) { int r = op_count(i); ans = min(ans, r); } assert(ans != 1 << 29); cout << ans << endl; }
#include <cassert> #include <iostream> #include <vector> using namespace std; int H, W, K; vector<string> s; template <typename T> void print_vec(vector<T> &v) { for (auto i : v) cout << i << " "; cout << "\n"; } vector<int> cnt_whi(vector<int> &a, int w, int I) { int idx = 0; vector<int> res(__builtin_popcount(I) + 1, 0); for (int i = 0; i < H; i++) { if (s[i][w] == '1') a[idx]++, res[idx]++; if ((I & (1 << i)) != 0) idx++; } return res; } int op_count(int I) { int n = __builtin_popcount(I); int res = n; vector<int> cnt(n + 1, 0); for (int i = 0; i < W; i++) { auto v = cnt_whi(cnt, i, I); // print_vec(cnt); for (auto it : v) if (it > K) return 1 << 29; bool d = false; for (auto it : cnt) if (it > K) d = true; if (d) { res++; swap(v, cnt); } } return res; } int main() { cin >> H >> W >> K; s.resize(H); for (int i = 0; i < H; i++) cin >> s[i]; int ans = 1 << 29; for (int i = 0; i < (1 << (H - 1)); i++) { int r = op_count(i); ans = min(ans, r); } assert(ans != 1 << 29); cout << ans << endl; }
replace
33
34
33
34
0
p02733
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <cstdint> #include <functional> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define N_TIMES(i, n) for (uint64_t i = 0; i < n; ++i) #define N_TIMES_REV(i, n) for (int64_t i = n - 1; i >= 0; --i) using namespace std; uint64_t calc(uint64_t H, uint64_t W, uint64_t K, const vector<vector<uint64_t>> &C, const bitset<16> &bs) { uint64_t T = 0; // comporess vector vector<vector<uint64_t>> D; D.push_back(C[0]); N_TIMES(y, H - 1) { if (bs[y]) { D.push_back(C[y + 1]); } else { N_TIMES(x, W + 1) { D[D.size() - 1][x] += C[y + 1][x]; } } } auto binary_search = [&](uint64_t left) { auto judge = [&](uint64_t x) { N_TIMES(i, D.size()) { if ((D[i][x] - D[i][left]) > K) { return false; } } return true; }; uint64_t lo = left, hi = W + 1; while (hi - lo > 1) { uint64_t mid = (hi + lo) >> 1; if (judge(mid)) { lo = mid; } else { hi = mid; } } return lo; }; uint64_t L = 0; while (L < W) { L = binary_search(L); ++T; } return T - 1; } int main() { uint64_t H, W, K; cin >> H >> W >> K; vector<string> S(H); N_TIMES(y, H) { cin >> S[y]; } vector<vector<uint64_t>> C(H); N_TIMES(y, H) { C[y] = vector<uint64_t>(W + 1, 0); N_TIMES(x, W) { C[y][x + 1] = (S[y][x] - '0') + C[y][x]; } } uint64_t ans = 1e+9; N_TIMES(i, 1 << (H - 1)) { bitset<16> bs(i); uint64_t num_of_edge = bs.count() + calc(H, W, K, C, bs); ans = min(ans, num_of_edge); } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdint> #include <functional> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define N_TIMES(i, n) for (uint64_t i = 0; i < n; ++i) #define N_TIMES_REV(i, n) for (int64_t i = n - 1; i >= 0; --i) using namespace std; uint64_t calc(uint64_t H, uint64_t W, uint64_t K, const vector<vector<uint64_t>> &C, const bitset<16> &bs) { uint64_t T = 0; // comporess vector vector<vector<uint64_t>> D; D.push_back(C[0]); N_TIMES(y, H - 1) { if (bs[y]) { D.push_back(C[y + 1]); } else { N_TIMES(x, W + 1) { D[D.size() - 1][x] += C[y + 1][x]; } } } auto binary_search = [&](uint64_t left) { auto judge = [&](uint64_t x) { N_TIMES(i, D.size()) { if ((D[i][x] - D[i][left]) > K) { return false; } } return true; }; uint64_t lo = left, hi = W + 1; while (hi - lo > 1) { uint64_t mid = (hi + lo) >> 1; if (judge(mid)) { lo = mid; } else { hi = mid; } } return lo; }; uint64_t L = 0; while (L < W) { uint64_t new_L = binary_search(L); if (L == new_L) { return 1e+9; } L = new_L; ++T; } return T - 1; } int main() { uint64_t H, W, K; cin >> H >> W >> K; vector<string> S(H); N_TIMES(y, H) { cin >> S[y]; } vector<vector<uint64_t>> C(H); N_TIMES(y, H) { C[y] = vector<uint64_t>(W + 1, 0); N_TIMES(x, W) { C[y][x + 1] = (S[y][x] - '0') + C[y][x]; } } uint64_t ans = 1e+9; N_TIMES(i, 1 << (H - 1)) { bitset<16> bs(i); uint64_t num_of_edge = bs.count() + calc(H, W, K, C, bs); ans = min(ans, num_of_edge); } cout << ans << endl; return 0; }
replace
61
62
61
66
TLE
p02733
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #pragma warning(disable : 4996) typedef long long ll; #define MIN(a, b) ((a) > (b) ? (b) : (a)) #define MAX(a, b) ((a) < (b) ? (b) : (a)) #define LINF 9223300000000000000 #define LINF2 1223300000000000000 #define INF 2140000000 // const long long MOD = 1000000007; // const long long MOD = 998244353; long long MOD; using namespace std; char str[10][1005]; int s[12][12][1005]; int ne[12][12][1005]; void solve() { int h, w, K; scanf("%d%d%d", &h, &w, &K); int i, j, k; for (i = 0; i < h; i++) { scanf("%s", str[i]); for (j = 0; j < w; j++) { s[i][i + 1][j + 1] = s[i][i + 1][j] + (str[i][j] == '1' ? 1 : 0); } } for (k = 0; k <= w; k++) { for (i = 0; i <= h; i++) { for (j = i + 2; j <= h; j++) { s[i][j][k] = s[i][j - 1][k] + s[j - 1][j][k]; } } } for (i = 0; i <= h; i++) { for (j = i + 1; j <= h; j++) { for (k = 0; k < w; k++) { int off = lower_bound(&s[i][j][k], &s[i][j][w + 1], s[i][j][k] + K + 1) - &s[i][j][0]; off--; ne[i][j][k] = off; } } } int ans = INF; for (k = 0; k < (1 << (h - 1)); k++) { int cnt0 = 0; for (i = 1; i < h; i++) { if (k & (1 << (i - 1))) { cnt0++; } } int cnt = 0; int stt = 0; while (stt < w) { int min = INF; int prev = 0; for (i = 1; i <= h; i++) { if (i == h || (k & (1 << (i - 1)))) { int off = ne[prev][i][stt]; min = MIN(min, off); prev = i; } } cnt++; stt = min; } // printf("%d ", cnt+cnt0-1); ans = MIN(ans, cnt + cnt0 - 1); } printf("%d\n", ans); return; } int main(int argc, char *argv[]) { #if 1 solve(); #else int T; scanf("%d", &T); while (T--) { solve(); } #endif return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #pragma warning(disable : 4996) typedef long long ll; #define MIN(a, b) ((a) > (b) ? (b) : (a)) #define MAX(a, b) ((a) < (b) ? (b) : (a)) #define LINF 9223300000000000000 #define LINF2 1223300000000000000 #define INF 2140000000 // const long long MOD = 1000000007; // const long long MOD = 998244353; long long MOD; using namespace std; char str[10][1005]; int s[12][12][1005]; int ne[12][12][1005]; void solve() { int h, w, K; scanf("%d%d%d", &h, &w, &K); int i, j, k; for (i = 0; i < h; i++) { scanf("%s", str[i]); for (j = 0; j < w; j++) { s[i][i + 1][j + 1] = s[i][i + 1][j] + (str[i][j] == '1' ? 1 : 0); } } for (k = 0; k <= w; k++) { for (i = 0; i <= h; i++) { for (j = i + 2; j <= h; j++) { s[i][j][k] = s[i][j - 1][k] + s[j - 1][j][k]; } } } for (i = 0; i <= h; i++) { for (j = i + 1; j <= h; j++) { for (k = 0; k < w; k++) { int off = lower_bound(&s[i][j][k], &s[i][j][w + 1], s[i][j][k] + K + 1) - &s[i][j][0]; off--; ne[i][j][k] = off; } } } int ans = INF; for (k = 0; k < (1 << (h - 1)); k++) { int cnt0 = 0; for (i = 1; i < h; i++) { if (k & (1 << (i - 1))) { cnt0++; } } int cnt = 0; int stt = 0; while (stt < w) { int min = INF; int prev = 0; for (i = 1; i <= h; i++) { if (i == h || (k & (1 << (i - 1)))) { int off = ne[prev][i][stt]; min = MIN(min, off); prev = i; } } cnt++; if (min <= stt) { cnt = INF; break; } stt = min; } // printf("%d ", cnt+cnt0-1); ans = MIN(ans, cnt + cnt0 - 1); } printf("%d\n", ans); return; } int main(int argc, char *argv[]) { #if 1 solve(); #else int T; scanf("%d", &T); while (T--) { solve(); } #endif return 0; }
insert
85
85
85
89
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int grid[10][1000], n, m, k; vector<int> get(int x, int y) { vector<int> ans(m); for (int c = 0; c < m; c++) { for (int r = x; r < y; r++) { ans[c] += grid[r][c]; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { grid[i][j] = s[j] - '0'; } } int ans = n * m; for (int s = 0; s < (1 << (n - 1)); s++) { vector<vector<int>> rows; vector<int> hor = {0}; for (int i = 0; i < n - 1; i++) { if (s >> i & 1) { hor.push_back(i + 1); } } hor.push_back(m); for (int i = 1; i < (int)hor.size(); i++) { rows.push_back(get(hor[i - 1], hor[i])); } int cur = __builtin_popcount(s); bool bad = false; for (auto &v : rows) { for (int x : v) { if (x > k) { bad = true; } } } if (bad) continue; vector<int> sum(rows.size()); for (int i = 0; i < m; i++) { bool need = false; for (int j = 0; j < (int)rows.size(); j++) { if (sum[j] + rows[j][i] > k) { need = true; } } if (need) { cur++; for (int j = 0; j < (int)rows.size(); j++) { sum[j] = rows[j][i]; } } else { for (int j = 0; j < (int)rows.size(); j++) { sum[j] += rows[j][i]; } } } ans = min(ans, cur); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int grid[10][1000], n, m, k; vector<int> get(int x, int y) { vector<int> ans(m); for (int c = 0; c < m; c++) { for (int r = x; r < y; r++) { ans[c] += grid[r][c]; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { grid[i][j] = s[j] - '0'; } } int ans = n * m; for (int s = 0; s < (1 << (n - 1)); s++) { vector<vector<int>> rows; vector<int> hor = {0}; for (int i = 0; i < n - 1; i++) { if (s >> i & 1) { hor.push_back(i + 1); } } hor.push_back(n); for (int i = 1; i < (int)hor.size(); i++) { rows.push_back(get(hor[i - 1], hor[i])); } int cur = __builtin_popcount(s); bool bad = false; for (auto &v : rows) { for (int x : v) { if (x > k) { bad = true; } } } if (bad) continue; vector<int> sum(rows.size()); for (int i = 0; i < m; i++) { bool need = false; for (int j = 0; j < (int)rows.size(); j++) { if (sum[j] + rows[j][i] > k) { need = true; } } if (need) { cur++; for (int j = 0; j < (int)rows.size(); j++) { sum[j] = rows[j][i]; } } else { for (int j = 0; j < (int)rows.size(); j++) { sum[j] += rows[j][i]; } } } ans = min(ans, cur); } cout << ans << endl; }
replace
43
44
43
44
0
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; // int/long: -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31) // long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 // (-2^63 <= long < 2^63) // lower_bound(A.begin(), A.end(), N) // upper_bound(... // A.erase(unique(A.begin(), A.end()), A.end()) // bit: &/and, |/or, ^/xor, ~/not // getline(cin, String) // while (getline(cin, S)) {} // #include <cstdio> // int a; // scanf("%d", &a); // printf("%d\n", a); // // for (int i=0; i<n; ++i) // scanf("%d", &a[i]); // // char c; // scanf(" %c", &c); // // cout << "\n"; // cout <<fixed <<setprecision(10) // priority_queue<int> q; // priority_queue<int, vector<int>, less<int>> q; // Default: vector, less // priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q; // Graph // Warshall-Floyd: Distance between each node, N^3 // Dijkstra: Distance from the start node, N^2 // DFS(Depth-First Search) // // Notes // // When specified less than N, try full scan. // RE: empty vector // Check Qs // long/int // xor -> count each bit // Check values // 1,000,000 -> Loop // 100,000 -> vector(int:400KB, long:800KB) #define INF (1 << 30) // 1,073,741,824 //= 536,870,912 *2 #define MOD 1000000007 /* ll myPow(ll x, ll n) { if (0==n) return 1; if (0==n%2) return myPow(x*x%MOD, n/2); else return x*myPow(x, n-1)%MOD; } */ ll myPow(ll x, ll n) { ll r = 1; while (n > 0) if (0 == n % 2) { x *= x; x %= MOD; n /= 2; } else { r *= x; r %= MOD; n--; } return r; } #define Rep0(i, n) for (int i = 0; i < n; i++) #define Rep1(i, n) for (int i = 1; i <= n; i++) #define Sort(P) sort(P.begin(), P.end()) #define Rev(P) reverse(P.begin(), P.end()) int Count(const int x0, const int y0, const int x1, const int y1, const vector<vector<int>> &SUM) { return SUM.at(x1).at(y1) + SUM.at(x0).at(y0) - SUM.at(x1).at(y0) - SUM.at(x0).at(y1); } int main() { int H, W, K; cin >> H >> W >> K; vector<vector<int>> S(H + 1, vector<int>(W + 1, 0)); Rep1(i, H) { string SS; cin >> SS; Rep1(j, W) S.at(i).at(j) = SS.at(j - 1) - '0'; } // Input Done // cumulative sum table vector<vector<int>> CUSUM(H + 1, vector<int>(W + 2, 0)); Rep1(i, H) { int s = 0; Rep1(j, W) { s += S.at(i).at(j); CUSUM.at(i).at(j) = CUSUM.at(i - 1).at(j) + s; } CUSUM.at(i).at(W + 1) = INF; } int SS = H + W; int M = pow(2, H - 1); Rep0(i, M) { // For each combination of holizontal lines vector<int> h; h.push_back(0); int SSS = 0; // Num of lines int I = i; int J = 1; while (I > 0) { if (1 == I % 2) { h.push_back(J); SSS++; } I /= 2; J *= 2; } h.push_back(H); // Count vertical lines int LL = 0; int RR = W + 1; bool FAIL = false; while (LL < W) { // Count vertical lines int RRR = W; Rep1(j, h.size() - 1) { int h0 = h.at(j - 1); int h1 = h.at(j); int L = LL; int R = RR; while (R - L > 1) { int M = (L + R) / 2; if (Count(h0, LL, h1, M, CUSUM) > K) R = M; else L = M; } if (L == LL) { // Not possible FAIL = true; break; } RRR = min(RRR, L); } if (FAIL) break; if (RRR < W) // Add vertical line after L SSS++; LL = RRR; } if (FAIL) continue; SS = min(SS, SSS); } cout << SS << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; // int/long: -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31) // long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 // (-2^63 <= long < 2^63) // lower_bound(A.begin(), A.end(), N) // upper_bound(... // A.erase(unique(A.begin(), A.end()), A.end()) // bit: &/and, |/or, ^/xor, ~/not // getline(cin, String) // while (getline(cin, S)) {} // #include <cstdio> // int a; // scanf("%d", &a); // printf("%d\n", a); // // for (int i=0; i<n; ++i) // scanf("%d", &a[i]); // // char c; // scanf(" %c", &c); // // cout << "\n"; // cout <<fixed <<setprecision(10) // priority_queue<int> q; // priority_queue<int, vector<int>, less<int>> q; // Default: vector, less // priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q; // Graph // Warshall-Floyd: Distance between each node, N^3 // Dijkstra: Distance from the start node, N^2 // DFS(Depth-First Search) // // Notes // // When specified less than N, try full scan. // RE: empty vector // Check Qs // long/int // xor -> count each bit // Check values // 1,000,000 -> Loop // 100,000 -> vector(int:400KB, long:800KB) #define INF (1 << 30) // 1,073,741,824 //= 536,870,912 *2 #define MOD 1000000007 /* ll myPow(ll x, ll n) { if (0==n) return 1; if (0==n%2) return myPow(x*x%MOD, n/2); else return x*myPow(x, n-1)%MOD; } */ ll myPow(ll x, ll n) { ll r = 1; while (n > 0) if (0 == n % 2) { x *= x; x %= MOD; n /= 2; } else { r *= x; r %= MOD; n--; } return r; } #define Rep0(i, n) for (int i = 0; i < n; i++) #define Rep1(i, n) for (int i = 1; i <= n; i++) #define Sort(P) sort(P.begin(), P.end()) #define Rev(P) reverse(P.begin(), P.end()) int Count(const int x0, const int y0, const int x1, const int y1, const vector<vector<int>> &SUM) { return SUM.at(x1).at(y1) + SUM.at(x0).at(y0) - SUM.at(x1).at(y0) - SUM.at(x0).at(y1); } int main() { int H, W, K; cin >> H >> W >> K; vector<vector<int>> S(H + 1, vector<int>(W + 1, 0)); Rep1(i, H) { string SS; cin >> SS; Rep1(j, W) S.at(i).at(j) = SS.at(j - 1) - '0'; } // Input Done // cumulative sum table vector<vector<int>> CUSUM(H + 1, vector<int>(W + 2, 0)); Rep1(i, H) { int s = 0; Rep1(j, W) { s += S.at(i).at(j); CUSUM.at(i).at(j) = CUSUM.at(i - 1).at(j) + s; } CUSUM.at(i).at(W + 1) = INF; } int SS = H + W; int M = pow(2, H - 1); Rep0(i, M) { // For each combination of holizontal lines vector<int> h; h.push_back(0); int SSS = 0; // Num of lines int I = i; int J = 1; while (I > 0) { if (1 == I % 2) { h.push_back(J); SSS++; } I /= 2; J++; } h.push_back(H); // Count vertical lines int LL = 0; int RR = W + 1; bool FAIL = false; while (LL < W) { // Count vertical lines int RRR = W; Rep1(j, h.size() - 1) { int h0 = h.at(j - 1); int h1 = h.at(j); int L = LL; int R = RR; while (R - L > 1) { int M = (L + R) / 2; if (Count(h0, LL, h1, M, CUSUM) > K) R = M; else L = M; } if (L == LL) { // Not possible FAIL = true; break; } RRR = min(RRR, L); } if (FAIL) break; if (RRR < W) // Add vertical line after L SSS++; LL = RRR; } if (FAIL) continue; SS = min(SS, SSS); } cout << SS << endl; return 0; }
replace
130
131
130
131
0
p02733
C++
Runtime Error
// #define NDEBUG #pragma GCC optimize("O3") // 最適化レベルの変更 O0〜O3 などを指定 #pragma GCC optimize("-O3,inline,omit-frame-pointer,unroll-loops") #pragma warning(1 : 4456) // 多重forループ内での変数の2重定義を警告にする #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <iostream> #include <algorithm> #include <array> #include <bitset> #include <cmath> #include <fstream> #include <iomanip> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <memory> #include <chrono> #include <functional> using namespace std; namespace ValLib { typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; constexpr ull V_MOD = 1000000007ull; constexpr int V_INT_MAX = 2147483647; constexpr ll V_LL_MAX = 9223372036854775807ll; constexpr ull V_ULL_MAX = 18446744073709551615ull; template <typename Key, typename Value> using umap = std::unordered_map<Key, Value>; template <typename T> using uset = std::unordered_set<T>; template <typename T> void fill(vector<T> &vec, const T &value) { std::fill(vec.begin(), vec.end(), value); } template <typename T> void fill(vector<vector<T>> &vec, const T &value) { for (vector<T> &t : vec) std::fill(t.begin(), t.end(), value); } template <typename T> void resize(vector<T> &vec, int size1) { vec.resize(size1); } template <typename T> void resize(vector<T> &vec, int size1, const T &value) { vec.resize(size1, value); } template <typename T> void resize(vector<vector<T>> &vec, int size1, int size2) { vec.resize(size1); for (vector<T> &t : vec) t.resize(size2); } template <typename T> void resize(vector<vector<T>> &vec, int size1, int size2, const T &value) { vec.resize(size1); for (vector<T> &t : vec) t.resize(size2, value); } template <typename T> void assign(vector<T> &vec, int size1, const T &value) { vec.assign(size1, value); } template <typename T> void assign(vector<vector<T>> &vec, int size1, int size2, const T &value) { vec.resize(size1); for (vector<T> &t : vec) t.assign(size2, value); } template <typename T> const typename vector<T>::const_iterator max_element(const vector<T> &vec) { assert(!vec.empty()); return std::max_element(vec.begin(), vec.end()); } template <typename T, typename _Pr> const typename vector<T>::const_iterator max_element(const vector<T> &vec, _Pr lessThan) { assert(!vec.empty()); return std::max_element(vec.begin(), vec.end(), lessThan); } template <typename T> typename vector<T>::iterator min_element(vector<T> &vec) { assert(!vec.empty()); return std::min_element(vec.begin(), vec.end()); } template <typename T> const typename vector<T>::const_iterator min_element(const vector<T> &vec) { assert(!vec.empty()); return std::min_element(vec.begin(), vec.end()); } template <typename T, typename _Pr> const typename vector<T>::const_iterator min_element(const vector<T> &vec, _Pr lessThan) { assert(!vec.empty()); return std::min_element(vec.begin(), vec.end(), lessThan); } int accumulate(const vector<int> &vec) { return std::accumulate(vec.begin(), vec.end(), 0); } template <typename _Pr> int accumulate(const vector<int> &vec, _Pr func) { return std::accumulate(vec.begin(), vec.end(), 0, func); } double accumulate(const vector<double> &vec) { return std::accumulate(vec.begin(), vec.end(), 0.0); } template <typename _Pr> double accumulate(const vector<double> &vec, _Pr func) { return std::accumulate(vec.begin(), vec.end(), 0.0, func); } float accumulate(const vector<float> &vec) { return std::accumulate(vec.begin(), vec.end(), 0.f); } template <typename _Pr> float accumulate(const vector<float> &vec, _Pr func) { return std::accumulate(vec.begin(), vec.end(), 0.f, func); } template <typename T, typename _Pr> bool all_of(const vector<T> &vec, _Pr pred) { return std::all_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> bool any_of(const vector<T> &vec, _Pr pred) { return std::any_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> bool none_of(const vector<T> &vec, _Pr pred) { return std::none_of(vec.begin(), vec.end(), pred); } template <typename T> const typename vector<T>::const_iterator find(const vector<T> &vec, const T &val) { return std::find(vec.begin(), vec.end(), val); } template <typename T, typename _Pr> const typename vector<T>::const_iterator find_if(const vector<T> &vec, _Pr pred) { return std::find_if(vec.begin(), vec.end(), pred); } template <typename T> bool contains(const vector<T> &vec, const T &val) { return std::find(vec.begin(), vec.end(), val) != vec.end(); } template <typename T, typename _Pr> bool contains_if(const vector<T> &vec, _Pr pred) { return std::find_if(vec.begin(), vec.end(), pred) != vec.end(); } template <typename T, typename _Pr> typename iterator_traits< const typename vector<T>::const_iterator>::difference_type count_if(const vector<T> &vec, _Pr pred) { return std::count_if(vec.begin(), vec.end(), pred); } template <typename T> void sort(vector<T> &vec) { std::sort(vec.begin(), vec.end()); } template <typename T, typename _Pr> void sort(vector<T> &vec, _Pr pred) { std::sort(vec.begin(), vec.end(), pred); } template <typename T> void stable_sort(vector<T> &vec) { std::stable_sort(vec.begin(), vec.end()); } template <typename T, typename _Pr> void stable_sort(vector<T> &vec, _Pr pred) { std::stable_sort(vec.begin(), vec.end(), pred); } template <typename T, size_t N> void fill(array<T, N> &ary, const T &value) { std::fill(ary.begin(), ary.end(), value); } template <typename T, size_t N, size_t M> void fill(array<array<T, M>, N> &ary, const T &value) { for (array<T, M> &t : ary) std::fill(t.begin(), t.end(), value); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator max_element(const array<T, N> &ary) { assert(!ary.empty()); return std::max_element(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> const typename vector<T>::const_iterator max_element(const array<T, N> &ary) { assert(!ary.empty()); return std::max_element(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator max_element(const array<T, N> &ary, _Pr lessThan) { assert(!ary.empty()); return std::max_element(ary.begin(), ary.end(), lessThan); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator min_element(const array<T, N> &ary) { assert(!ary.empty()); return std::min_element(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator min_element(const array<T, N> &ary, _Pr lessThan) { assert(!ary.empty()); return std::min_element(ary.begin(), ary.end(), lessThan); } template <size_t N> int accumulate(const array<int, N> &ary) { return std::accumulate(ary.begin(), ary.end(), 0); } template <size_t N, typename _Pr> int accumulate(const array<int, N> &ary, _Pr func) { return std::accumulate(ary.begin(), ary.end(), 0, func); } template <size_t N> double accumulate(const array<double, N> &ary) { return std::accumulate(ary.begin(), ary.end(), 0.0); } template <size_t N, typename _Pr> double accumulate(const array<double, N> &ary, _Pr func) { return std::accumulate(ary.begin(), ary.end(), 0.0, func); } template <size_t N> float accumulate(const array<float, N> &ary) { return std::accumulate(ary.begin(), ary.end(), 0.f); } template <size_t N, typename _Pr> float accumulate(const array<float, N> &ary, _Pr func) { return std::accumulate(ary.begin(), ary.end(), 0.f, func); } template <typename T, size_t N, typename _Pr> bool all_of(const array<T, N> &ary, _Pr pred) { return std::all_of(ary.begin(), ary.end(), pred); } template <typename T, size_t N, typename _Pr> bool any_of(const array<T, N> &ary, _Pr pred) { return std::any_of(ary.begin(), ary.end(), pred); } template <typename T, size_t N, typename _Pr> bool none_of(const array<T, N> &ary, _Pr pred) { return std::none_of(ary.begin(), ary.end(), pred); } template <typename T, size_t N> const typename array<T, N>::const_iterator find(const array<T, N> &ary, const T &val) { return std::find(ary.begin(), ary.end(), val); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator find_if(const array<T, N> &ary, _Pr pred) { return std::find_if(ary.begin(), ary.end(), pred); } template <typename T, size_t N> bool contains(const array<T, N> &ary, const T &val) { return std::find(ary.begin(), ary.end(), val) != ary.end(); } template <typename T, size_t N, typename _Pr> bool contains_if(const array<T, N> &ary, _Pr pred) { return std::find_if(ary.begin(), ary.end(), pred) != ary.end(); } template <typename T, size_t N, typename _Pr> typename iterator_traits< const typename array<T, N>::const_iterator>::difference_type count_if(const array<T, N> &ary, _Pr pred) { return std::count_if(ary.begin(), ary.end(), pred); } template <typename T, size_t N> void sort(array<T, N> &ary) { std::sort(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> void sort(array<T, N> &ary, _Pr pred) { std::sort(ary.begin(), ary.end(), pred); } template <typename T, size_t N> void stable_sort(array<T, N> &ary) { std::stable_sort(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> void stable_sort(array<T, N> &ary, _Pr pred) { std::stable_sort(ary.begin(), ary.end(), pred); } template <typename Key, typename Value> bool containsKey(const umap<Key, Value> &m, const Key &key) { return m.find(key) != m.end(); } template <typename Key, typename Value> bool containsValue(const umap<Key, Value> &m, const Value &val) { for (auto it = m.begin(); it != m.end(); ++it) if (it->second == val) return true; return false; } template <typename T> const typename uset<T>::const_iterator find(const uset<T> &s, const T &key) { return s.find(key); } template <typename T> bool contains(const uset<T> &s, const T &key) { return s.find(key) != s.end(); } constexpr int DX4[] = {0, -1, 0, 1}; constexpr int DY4[] = {-1, 0, 1, 0}; constexpr int DX8[] = {0, -1, -1, -1, 0, 1, 1, 1}; constexpr int DY8[] = {-1, -1, 0, 1, 1, 1, 0, -1}; constexpr int DX5[] = {DX4[0], DX4[1], DX4[2], DX4[3], 0}; constexpr int DY5[] = {DY4[0], DY4[1], DY4[2], DY4[3], 0}; constexpr int DX9[] = {DX8[0], DX8[1], DX8[2], DX8[3], DX8[4], DX8[5], DX8[6], DX8[7], 0}; constexpr int DY9[] = {DY8[0], DY8[1], DY8[2], DY8[3], DY8[4], DY8[5], DY8[6], DY8[7], 0}; class Point { public: constexpr inline Point() : Point(-1, -1) {} constexpr inline Point(int x, int y) : x(x), y(y) {} static Point getManhattanDist(const Point &p1, const Point &p2) { return move(Point(abs(p1.x - p2.x), abs(p1.y - p2.y))); } void setPos(const Point &pos) { x = pos.x; y = pos.y; } void setPos(int x, int y) { this->x = x; this->y = y; } Point operator+(int val) const { return move(Point(x + val, y + val)); } Point operator-(int val) const { return move(Point(x - val, y - val)); } Point operator*(int val) const { return move(Point(x * val, y * val)); } Point operator/(int val) const { return move(Point(x / val, y / val)); } Point operator+(const Point &p) const { return move(Point(x + p.x, y + p.y)); } Point operator-(const Point &p) const { return move(Point(x - p.x, y - p.y)); } Point operator*(const Point &p) const { return move(Point(x * p.x, y * p.y)); } Point operator/(const Point &p) const { return move(Point(x / p.x, y / p.y)); } void operator+=(int val) { x += val; y += val; } void operator-=(int val) { x -= val; y -= val; } void operator*=(int val) { x *= val; y *= val; } void operator/=(int val) { x /= val; y /= val; } void operator+=(const Point &p) { x += p.x; y += p.y; } void operator-=(const Point &p) { x -= p.x; y -= p.y; } void operator*=(const Point &p) { x *= p.x; y *= p.y; } void operator/=(const Point &p) { x /= p.x; y /= p.y; } bool operator==(const Point &p) const { return x == p.x && y == p.y; } bool operator!=(const Point &p) const { return x != p.x || y != p.y; } // bool operator<(const Point &p) const {return x * x + y * y < p.x * p.x + // p.y * p.y;} const Point &getPos() const { return *this; } string to_string() const { return "(" + std::to_string(x) + ", " + std::to_string(y) + ")"; } int x, y; }; constexpr Point DP4[] = {Point(DX4[0], DY4[0]), Point(DX4[1], DY4[1]), Point(DX4[2], DY4[2]), Point(DX4[3], DY4[3])}; constexpr Point DP5[] = {Point(DX5[0], DY5[0]), Point(DX5[1], DY5[1]), Point(DX5[2], DY5[2]), Point(DX5[3], DY5[3]), Point(DX5[4], DY5[4])}; constexpr Point DP8[] = {Point(DX8[0], DY8[0]), Point(DX8[1], DY8[1]), Point(DX8[2], DY8[2]), Point(DX8[3], DY8[3]), Point(DX8[4], DY8[4]), Point(DX8[5], DY8[5]), Point(DX8[6], DY8[6]), Point(DX8[7], DY8[7])}; constexpr Point DP9[] = {Point(DX9[0], DY9[0]), Point(DX9[1], DY9[1]), Point(DX9[2], DY9[2]), Point(DX9[3], DY9[3]), Point(DX9[4], DY9[4]), Point(DX9[5], DY9[5]), Point(DX9[6], DY9[6]), Point(DX9[7], DY9[7])}; // 文字列の先頭の0を削除する string cutZeroLeft(const string &str) { for (int i = 0; i < (int)str.length(); ++i) { if (str[i] != '0') { return str.substr(i); } } return ""; } // 0以上の整数を0埋めで表記する string fillZero(ull number, int digit) { int zeroNum = max(0, (int)(digit - to_string(number).length())); return move(move(string(zeroNum, '0')) + move(to_string(number))); } // 0以上の10進数を2進数に変換する string toBinString(ull x) { return move(cutZeroLeft(bitset<20>(x).to_string())); } // 0以上の10進数を8進数に変換する string toOctString(ull x) { stringstream ss; ss << std::oct << x; return move(ss.str()); } // 0以上の整数を16進数に変換する(a~fは小文字) string toHexString(ull x) { stringstream ss; ss << std::hex << x; return move(ss.str()); } // 0以上の整数を任意の基数に変換する(最大36進数)(a~zは小文字) string toBaseString(ull x, int base) { const string table = "0123456789abcdefghijklmnopqrstuvwxyz"; assert(base >= 2 && base <= 36); string result = ""; ull y = x; do { ull div = y / (ull)base; int mod = y % (ull)base; result = table[mod] + result; y = div; } while (y != 0ull); return move(result); } // 任意の基数の0以上の整数を10進数に変換する // base: 変換元の基数 ull toDecimal(string numberString, int base) { assert(numberString.length() < 20); assert(!numberString.empty()); return stoull(numberString, nullptr, base); } // double型の値を任意の少数桁数で表示する // 指定の桁数まで0で埋める。指数表記にはならない // precision=0を指定したときは小数点は表示しない // 表示桁数+1桁目を四捨五入する string sprintDoublePlane(double value, int precision) { assert(precision >= 0); stringstream ss; ss << fixed << setprecision(precision) << value; return move(ss.str()); } template <typename A> void in(A &a) { cin >> a; cin.ignore(); } template <typename A, typename B> void in(A &a, B &b) { cin >> a >> b; cin.ignore(); } template <typename A, typename B, typename C> void in(A &a, B &b, C &c) { cin >> a >> b >> c; cin.ignore(); } template <typename A, typename B, typename C, typename D> void in(A &a, B &b, C &c, D &d) { cin >> a >> b >> c >> d; cin.ignore(); } template <typename A, typename B, typename C, typename D, typename E> void in(A &a, B &b, C &c, D &d, E &e) { cin >> a >> b >> c >> d >> e; cin.ignore(); } template <typename A, typename B, typename C, typename D, typename E, typename F> void in(A &a, B &b, C &c, D &d, E &e, F &f) { cin >> a >> b >> c >> d >> e >> f; cin.ignore(); } template <typename A, typename B, typename C, typename D, typename E, typename F, typename G> void in(A &a, B &b, C &c, D &d, E &e, F &f, G &g) { cin >> a >> b >> c >> d >> e >> f >> g; cin.ignore(); } template <typename A> void inr(vector<A> &a, int size) { resize(a, size); for (int i = 0; i < size; ++i) { cin >> a[i]; cin.ignore(); } } template <typename A, typename B> void inr(vector<A> &a, vector<B> &b, int size) { resize(a, size); resize(b, size); for (int i = 0; i < size; ++i) { cin >> a[i] >> b[i]; cin.ignore(); } } template <typename A, typename B, typename C> void inr(vector<A> &a, vector<B> &b, vector<C> &c, int size) { resize(a, size); resize(b, size); resize(c, size); for (int i = 0; i < size; ++i) { cin >> a[i] >> b[i] >> c[i]; cin.ignore(); } } template <typename A, typename B, typename C, typename D> void inr(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d, int size) { resize(a, size); resize(b, size); resize(c, size); resize(d, size); for (int i = 0; i < size; ++i) { cin >> a[i] >> b[i] >> c[i] >> d[i]; cin.ignore(); } } template <typename A, typename B, typename C, typename D, typename E> void inr(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d, vector<E> &e, int size) { resize(a, size); resize(b, size); resize(c, size); resize(d, size); resize(e, size); for (int i = 0; i < size; ++i) { cin >> a[i] >> b[i] >> c[i] >> d[i] >> e[i]; cin.ignore(); } } template <typename A> void inr(vector<vector<A>> &a, int h, int wa) { resize(a, h, wa); for (int i = 0; i < h; ++i) { for (int j = 0; j < wa; ++j) { cin >> a[i][j]; } cin.ignore(); } } template <typename A, typename B> void inr(vector<vector<A>> &a, vector<vector<B>> &b, int h, int wa, int wb) { resize(a, h, wa); resize(b, h, wb); for (int i = 0; i < h; ++i) { for (int j = 0; j < wa; ++j) { cin >> a[i][j]; } for (int j = 0; j < wb; ++j) { cin >> b[i][j]; } cin.ignore(); } } template <typename A, typename B, typename C> void inr(vector<vector<A>> &a, vector<vector<B>> &b, vector<vector<C>> &c, int h, int wa, int wb, int wc) { resize(a, h, wa); resize(b, h, wb); resize(c, h, wc); for (int i = 0; i < h; ++i) { for (int j = 0; j < wa; ++j) { cin >> a[i][j]; } for (int j = 0; j < wb; ++j) { cin >> b[i][j]; } for (int j = 0; j < wc; ++j) { cin >> c[i][j]; } cin.ignore(); } } template <typename T> void out(const T &val) { cout << val << endl; } static constexpr int COMBI_MAX = 500000; // nCrのnとrの最大値 static ull gFact[COMBI_MAX + 1]; // modの世界での階乗 static ull gInv[COMBI_MAX + 1]; // modの世界での逆元 static ull gFactInv[COMBI_MAX + 1]; // modの世界での逆元の階乗 static ull gCombiInitMod = 0; // 組み合わせの計算に使用したmod値 // 組み合わせテーブルを作る static void initCombination(ull mod) { gFact[0] = gFact[1] = 1; gFactInv[0] = gFactInv[1] = 1; gInv[1] = 1; for (int i = 2; i <= COMBI_MAX; i++) { gFact[i] = gFact[i - 1] * i % mod; gInv[i] = mod - gInv[mod % i] * (mod / i) % mod; gFactInv[i] = gFactInv[i - 1] * gInv[i] % mod; } gCombiInitMod = mod; } static void initCombination() { initCombination(V_MOD); } // 組み合わせの数を計算 static long long combination(ull n, ull k) { assert(gCombiInitMod > 0 && "先にinitCombinationを呼び出して"); assert(n <= (ull)COMBI_MAX); assert(k <= (ull)COMBI_MAX); if (n < k) return 0; return (gFact[n] * ((gFactInv[k] * gFactInv[n - k]) % V_MOD)) % V_MOD; } } // namespace ValLib using namespace ValLib; int H, W, K; vector<vector<char>> c; vector<vector<int>> a; int solve(int val, bool &success) { vector<int> indexes(H, 0); int count = 0; for (int i = 0; i < H; ++i) { indexes[i] = count; if ((val & (1 << i)) != 0) { ++count; } } ++count; int cut = 0; vector<int> sums(H, 0); for (int x = 0; x < W; ++x) { vector<int> lineSum(indexes.size(), 0); for (int y = 0; y < H; ++y) { lineSum[indexes[y]] += a[y][x]; } for (int y = 0; y < H; ++y) { if (lineSum[indexes[y]] > K) { success = false; return 0; } } for (int i = 0; i < count; ++i) { sums[i] += lineSum[i]; } bool cutFlg = false; for (int y = 0; y < H; ++y) { if (sums[indexes[y]] > K) { cutFlg = true; break; } } if (cutFlg) { ++cut; for (int y = 0; y < H; ++y) { sums[indexes[y]] = lineSum[indexes[y]]; } } } success = true; return cut; } int main() { in(H, W, K); c.resize(H); for (int y = 0; y < H; ++y) { c[y].resize(W); string line; getline(cin, line); for (int x = 0; x < W; ++x) { c[y][x] = line[x]; } } a.resize(H, vector<int>(W, 0)); for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { if (c[y][x] == '1') { a[y][x] = 1; } else { a[y][x] = 0; } } } int sum = 0; for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { sum += a[y][x]; } } if (sum <= K) { out(0); } else { int loopNum = 1; for (int i = 0; i < H; ++i) { loopNum *= 2; } int minNum = 99999999; for (int i = 0; i < loopNum; ++i) { bool success = false; int num = solve(i, success); for (int j = 0; j < H; ++j) { if (((1 << j) & i) != 0) { ++num; } } if (success && num < minNum) { minNum = num; } } out(minNum); } }
// #define NDEBUG #pragma GCC optimize("O3") // 最適化レベルの変更 O0〜O3 などを指定 #pragma GCC optimize("-O3,inline,omit-frame-pointer,unroll-loops") #pragma warning(1 : 4456) // 多重forループ内での変数の2重定義を警告にする #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <iostream> #include <algorithm> #include <array> #include <bitset> #include <cmath> #include <fstream> #include <iomanip> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <memory> #include <chrono> #include <functional> using namespace std; namespace ValLib { typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; constexpr ull V_MOD = 1000000007ull; constexpr int V_INT_MAX = 2147483647; constexpr ll V_LL_MAX = 9223372036854775807ll; constexpr ull V_ULL_MAX = 18446744073709551615ull; template <typename Key, typename Value> using umap = std::unordered_map<Key, Value>; template <typename T> using uset = std::unordered_set<T>; template <typename T> void fill(vector<T> &vec, const T &value) { std::fill(vec.begin(), vec.end(), value); } template <typename T> void fill(vector<vector<T>> &vec, const T &value) { for (vector<T> &t : vec) std::fill(t.begin(), t.end(), value); } template <typename T> void resize(vector<T> &vec, int size1) { vec.resize(size1); } template <typename T> void resize(vector<T> &vec, int size1, const T &value) { vec.resize(size1, value); } template <typename T> void resize(vector<vector<T>> &vec, int size1, int size2) { vec.resize(size1); for (vector<T> &t : vec) t.resize(size2); } template <typename T> void resize(vector<vector<T>> &vec, int size1, int size2, const T &value) { vec.resize(size1); for (vector<T> &t : vec) t.resize(size2, value); } template <typename T> void assign(vector<T> &vec, int size1, const T &value) { vec.assign(size1, value); } template <typename T> void assign(vector<vector<T>> &vec, int size1, int size2, const T &value) { vec.resize(size1); for (vector<T> &t : vec) t.assign(size2, value); } template <typename T> const typename vector<T>::const_iterator max_element(const vector<T> &vec) { assert(!vec.empty()); return std::max_element(vec.begin(), vec.end()); } template <typename T, typename _Pr> const typename vector<T>::const_iterator max_element(const vector<T> &vec, _Pr lessThan) { assert(!vec.empty()); return std::max_element(vec.begin(), vec.end(), lessThan); } template <typename T> typename vector<T>::iterator min_element(vector<T> &vec) { assert(!vec.empty()); return std::min_element(vec.begin(), vec.end()); } template <typename T> const typename vector<T>::const_iterator min_element(const vector<T> &vec) { assert(!vec.empty()); return std::min_element(vec.begin(), vec.end()); } template <typename T, typename _Pr> const typename vector<T>::const_iterator min_element(const vector<T> &vec, _Pr lessThan) { assert(!vec.empty()); return std::min_element(vec.begin(), vec.end(), lessThan); } int accumulate(const vector<int> &vec) { return std::accumulate(vec.begin(), vec.end(), 0); } template <typename _Pr> int accumulate(const vector<int> &vec, _Pr func) { return std::accumulate(vec.begin(), vec.end(), 0, func); } double accumulate(const vector<double> &vec) { return std::accumulate(vec.begin(), vec.end(), 0.0); } template <typename _Pr> double accumulate(const vector<double> &vec, _Pr func) { return std::accumulate(vec.begin(), vec.end(), 0.0, func); } float accumulate(const vector<float> &vec) { return std::accumulate(vec.begin(), vec.end(), 0.f); } template <typename _Pr> float accumulate(const vector<float> &vec, _Pr func) { return std::accumulate(vec.begin(), vec.end(), 0.f, func); } template <typename T, typename _Pr> bool all_of(const vector<T> &vec, _Pr pred) { return std::all_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> bool any_of(const vector<T> &vec, _Pr pred) { return std::any_of(vec.begin(), vec.end(), pred); } template <typename T, typename _Pr> bool none_of(const vector<T> &vec, _Pr pred) { return std::none_of(vec.begin(), vec.end(), pred); } template <typename T> const typename vector<T>::const_iterator find(const vector<T> &vec, const T &val) { return std::find(vec.begin(), vec.end(), val); } template <typename T, typename _Pr> const typename vector<T>::const_iterator find_if(const vector<T> &vec, _Pr pred) { return std::find_if(vec.begin(), vec.end(), pred); } template <typename T> bool contains(const vector<T> &vec, const T &val) { return std::find(vec.begin(), vec.end(), val) != vec.end(); } template <typename T, typename _Pr> bool contains_if(const vector<T> &vec, _Pr pred) { return std::find_if(vec.begin(), vec.end(), pred) != vec.end(); } template <typename T, typename _Pr> typename iterator_traits< const typename vector<T>::const_iterator>::difference_type count_if(const vector<T> &vec, _Pr pred) { return std::count_if(vec.begin(), vec.end(), pred); } template <typename T> void sort(vector<T> &vec) { std::sort(vec.begin(), vec.end()); } template <typename T, typename _Pr> void sort(vector<T> &vec, _Pr pred) { std::sort(vec.begin(), vec.end(), pred); } template <typename T> void stable_sort(vector<T> &vec) { std::stable_sort(vec.begin(), vec.end()); } template <typename T, typename _Pr> void stable_sort(vector<T> &vec, _Pr pred) { std::stable_sort(vec.begin(), vec.end(), pred); } template <typename T, size_t N> void fill(array<T, N> &ary, const T &value) { std::fill(ary.begin(), ary.end(), value); } template <typename T, size_t N, size_t M> void fill(array<array<T, M>, N> &ary, const T &value) { for (array<T, M> &t : ary) std::fill(t.begin(), t.end(), value); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator max_element(const array<T, N> &ary) { assert(!ary.empty()); return std::max_element(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> const typename vector<T>::const_iterator max_element(const array<T, N> &ary) { assert(!ary.empty()); return std::max_element(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator max_element(const array<T, N> &ary, _Pr lessThan) { assert(!ary.empty()); return std::max_element(ary.begin(), ary.end(), lessThan); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator min_element(const array<T, N> &ary) { assert(!ary.empty()); return std::min_element(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator min_element(const array<T, N> &ary, _Pr lessThan) { assert(!ary.empty()); return std::min_element(ary.begin(), ary.end(), lessThan); } template <size_t N> int accumulate(const array<int, N> &ary) { return std::accumulate(ary.begin(), ary.end(), 0); } template <size_t N, typename _Pr> int accumulate(const array<int, N> &ary, _Pr func) { return std::accumulate(ary.begin(), ary.end(), 0, func); } template <size_t N> double accumulate(const array<double, N> &ary) { return std::accumulate(ary.begin(), ary.end(), 0.0); } template <size_t N, typename _Pr> double accumulate(const array<double, N> &ary, _Pr func) { return std::accumulate(ary.begin(), ary.end(), 0.0, func); } template <size_t N> float accumulate(const array<float, N> &ary) { return std::accumulate(ary.begin(), ary.end(), 0.f); } template <size_t N, typename _Pr> float accumulate(const array<float, N> &ary, _Pr func) { return std::accumulate(ary.begin(), ary.end(), 0.f, func); } template <typename T, size_t N, typename _Pr> bool all_of(const array<T, N> &ary, _Pr pred) { return std::all_of(ary.begin(), ary.end(), pred); } template <typename T, size_t N, typename _Pr> bool any_of(const array<T, N> &ary, _Pr pred) { return std::any_of(ary.begin(), ary.end(), pred); } template <typename T, size_t N, typename _Pr> bool none_of(const array<T, N> &ary, _Pr pred) { return std::none_of(ary.begin(), ary.end(), pred); } template <typename T, size_t N> const typename array<T, N>::const_iterator find(const array<T, N> &ary, const T &val) { return std::find(ary.begin(), ary.end(), val); } template <typename T, size_t N, typename _Pr> const typename array<T, N>::const_iterator find_if(const array<T, N> &ary, _Pr pred) { return std::find_if(ary.begin(), ary.end(), pred); } template <typename T, size_t N> bool contains(const array<T, N> &ary, const T &val) { return std::find(ary.begin(), ary.end(), val) != ary.end(); } template <typename T, size_t N, typename _Pr> bool contains_if(const array<T, N> &ary, _Pr pred) { return std::find_if(ary.begin(), ary.end(), pred) != ary.end(); } template <typename T, size_t N, typename _Pr> typename iterator_traits< const typename array<T, N>::const_iterator>::difference_type count_if(const array<T, N> &ary, _Pr pred) { return std::count_if(ary.begin(), ary.end(), pred); } template <typename T, size_t N> void sort(array<T, N> &ary) { std::sort(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> void sort(array<T, N> &ary, _Pr pred) { std::sort(ary.begin(), ary.end(), pred); } template <typename T, size_t N> void stable_sort(array<T, N> &ary) { std::stable_sort(ary.begin(), ary.end()); } template <typename T, size_t N, typename _Pr> void stable_sort(array<T, N> &ary, _Pr pred) { std::stable_sort(ary.begin(), ary.end(), pred); } template <typename Key, typename Value> bool containsKey(const umap<Key, Value> &m, const Key &key) { return m.find(key) != m.end(); } template <typename Key, typename Value> bool containsValue(const umap<Key, Value> &m, const Value &val) { for (auto it = m.begin(); it != m.end(); ++it) if (it->second == val) return true; return false; } template <typename T> const typename uset<T>::const_iterator find(const uset<T> &s, const T &key) { return s.find(key); } template <typename T> bool contains(const uset<T> &s, const T &key) { return s.find(key) != s.end(); } constexpr int DX4[] = {0, -1, 0, 1}; constexpr int DY4[] = {-1, 0, 1, 0}; constexpr int DX8[] = {0, -1, -1, -1, 0, 1, 1, 1}; constexpr int DY8[] = {-1, -1, 0, 1, 1, 1, 0, -1}; constexpr int DX5[] = {DX4[0], DX4[1], DX4[2], DX4[3], 0}; constexpr int DY5[] = {DY4[0], DY4[1], DY4[2], DY4[3], 0}; constexpr int DX9[] = {DX8[0], DX8[1], DX8[2], DX8[3], DX8[4], DX8[5], DX8[6], DX8[7], 0}; constexpr int DY9[] = {DY8[0], DY8[1], DY8[2], DY8[3], DY8[4], DY8[5], DY8[6], DY8[7], 0}; class Point { public: constexpr inline Point() : Point(-1, -1) {} constexpr inline Point(int x, int y) : x(x), y(y) {} static Point getManhattanDist(const Point &p1, const Point &p2) { return move(Point(abs(p1.x - p2.x), abs(p1.y - p2.y))); } void setPos(const Point &pos) { x = pos.x; y = pos.y; } void setPos(int x, int y) { this->x = x; this->y = y; } Point operator+(int val) const { return move(Point(x + val, y + val)); } Point operator-(int val) const { return move(Point(x - val, y - val)); } Point operator*(int val) const { return move(Point(x * val, y * val)); } Point operator/(int val) const { return move(Point(x / val, y / val)); } Point operator+(const Point &p) const { return move(Point(x + p.x, y + p.y)); } Point operator-(const Point &p) const { return move(Point(x - p.x, y - p.y)); } Point operator*(const Point &p) const { return move(Point(x * p.x, y * p.y)); } Point operator/(const Point &p) const { return move(Point(x / p.x, y / p.y)); } void operator+=(int val) { x += val; y += val; } void operator-=(int val) { x -= val; y -= val; } void operator*=(int val) { x *= val; y *= val; } void operator/=(int val) { x /= val; y /= val; } void operator+=(const Point &p) { x += p.x; y += p.y; } void operator-=(const Point &p) { x -= p.x; y -= p.y; } void operator*=(const Point &p) { x *= p.x; y *= p.y; } void operator/=(const Point &p) { x /= p.x; y /= p.y; } bool operator==(const Point &p) const { return x == p.x && y == p.y; } bool operator!=(const Point &p) const { return x != p.x || y != p.y; } // bool operator<(const Point &p) const {return x * x + y * y < p.x * p.x + // p.y * p.y;} const Point &getPos() const { return *this; } string to_string() const { return "(" + std::to_string(x) + ", " + std::to_string(y) + ")"; } int x, y; }; constexpr Point DP4[] = {Point(DX4[0], DY4[0]), Point(DX4[1], DY4[1]), Point(DX4[2], DY4[2]), Point(DX4[3], DY4[3])}; constexpr Point DP5[] = {Point(DX5[0], DY5[0]), Point(DX5[1], DY5[1]), Point(DX5[2], DY5[2]), Point(DX5[3], DY5[3]), Point(DX5[4], DY5[4])}; constexpr Point DP8[] = {Point(DX8[0], DY8[0]), Point(DX8[1], DY8[1]), Point(DX8[2], DY8[2]), Point(DX8[3], DY8[3]), Point(DX8[4], DY8[4]), Point(DX8[5], DY8[5]), Point(DX8[6], DY8[6]), Point(DX8[7], DY8[7])}; constexpr Point DP9[] = {Point(DX9[0], DY9[0]), Point(DX9[1], DY9[1]), Point(DX9[2], DY9[2]), Point(DX9[3], DY9[3]), Point(DX9[4], DY9[4]), Point(DX9[5], DY9[5]), Point(DX9[6], DY9[6]), Point(DX9[7], DY9[7])}; // 文字列の先頭の0を削除する string cutZeroLeft(const string &str) { for (int i = 0; i < (int)str.length(); ++i) { if (str[i] != '0') { return str.substr(i); } } return ""; } // 0以上の整数を0埋めで表記する string fillZero(ull number, int digit) { int zeroNum = max(0, (int)(digit - to_string(number).length())); return move(move(string(zeroNum, '0')) + move(to_string(number))); } // 0以上の10進数を2進数に変換する string toBinString(ull x) { return move(cutZeroLeft(bitset<20>(x).to_string())); } // 0以上の10進数を8進数に変換する string toOctString(ull x) { stringstream ss; ss << std::oct << x; return move(ss.str()); } // 0以上の整数を16進数に変換する(a~fは小文字) string toHexString(ull x) { stringstream ss; ss << std::hex << x; return move(ss.str()); } // 0以上の整数を任意の基数に変換する(最大36進数)(a~zは小文字) string toBaseString(ull x, int base) { const string table = "0123456789abcdefghijklmnopqrstuvwxyz"; assert(base >= 2 && base <= 36); string result = ""; ull y = x; do { ull div = y / (ull)base; int mod = y % (ull)base; result = table[mod] + result; y = div; } while (y != 0ull); return move(result); } // 任意の基数の0以上の整数を10進数に変換する // base: 変換元の基数 ull toDecimal(string numberString, int base) { assert(numberString.length() < 20); assert(!numberString.empty()); return stoull(numberString, nullptr, base); } // double型の値を任意の少数桁数で表示する // 指定の桁数まで0で埋める。指数表記にはならない // precision=0を指定したときは小数点は表示しない // 表示桁数+1桁目を四捨五入する string sprintDoublePlane(double value, int precision) { assert(precision >= 0); stringstream ss; ss << fixed << setprecision(precision) << value; return move(ss.str()); } template <typename A> void in(A &a) { cin >> a; cin.ignore(); } template <typename A, typename B> void in(A &a, B &b) { cin >> a >> b; cin.ignore(); } template <typename A, typename B, typename C> void in(A &a, B &b, C &c) { cin >> a >> b >> c; cin.ignore(); } template <typename A, typename B, typename C, typename D> void in(A &a, B &b, C &c, D &d) { cin >> a >> b >> c >> d; cin.ignore(); } template <typename A, typename B, typename C, typename D, typename E> void in(A &a, B &b, C &c, D &d, E &e) { cin >> a >> b >> c >> d >> e; cin.ignore(); } template <typename A, typename B, typename C, typename D, typename E, typename F> void in(A &a, B &b, C &c, D &d, E &e, F &f) { cin >> a >> b >> c >> d >> e >> f; cin.ignore(); } template <typename A, typename B, typename C, typename D, typename E, typename F, typename G> void in(A &a, B &b, C &c, D &d, E &e, F &f, G &g) { cin >> a >> b >> c >> d >> e >> f >> g; cin.ignore(); } template <typename A> void inr(vector<A> &a, int size) { resize(a, size); for (int i = 0; i < size; ++i) { cin >> a[i]; cin.ignore(); } } template <typename A, typename B> void inr(vector<A> &a, vector<B> &b, int size) { resize(a, size); resize(b, size); for (int i = 0; i < size; ++i) { cin >> a[i] >> b[i]; cin.ignore(); } } template <typename A, typename B, typename C> void inr(vector<A> &a, vector<B> &b, vector<C> &c, int size) { resize(a, size); resize(b, size); resize(c, size); for (int i = 0; i < size; ++i) { cin >> a[i] >> b[i] >> c[i]; cin.ignore(); } } template <typename A, typename B, typename C, typename D> void inr(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d, int size) { resize(a, size); resize(b, size); resize(c, size); resize(d, size); for (int i = 0; i < size; ++i) { cin >> a[i] >> b[i] >> c[i] >> d[i]; cin.ignore(); } } template <typename A, typename B, typename C, typename D, typename E> void inr(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d, vector<E> &e, int size) { resize(a, size); resize(b, size); resize(c, size); resize(d, size); resize(e, size); for (int i = 0; i < size; ++i) { cin >> a[i] >> b[i] >> c[i] >> d[i] >> e[i]; cin.ignore(); } } template <typename A> void inr(vector<vector<A>> &a, int h, int wa) { resize(a, h, wa); for (int i = 0; i < h; ++i) { for (int j = 0; j < wa; ++j) { cin >> a[i][j]; } cin.ignore(); } } template <typename A, typename B> void inr(vector<vector<A>> &a, vector<vector<B>> &b, int h, int wa, int wb) { resize(a, h, wa); resize(b, h, wb); for (int i = 0; i < h; ++i) { for (int j = 0; j < wa; ++j) { cin >> a[i][j]; } for (int j = 0; j < wb; ++j) { cin >> b[i][j]; } cin.ignore(); } } template <typename A, typename B, typename C> void inr(vector<vector<A>> &a, vector<vector<B>> &b, vector<vector<C>> &c, int h, int wa, int wb, int wc) { resize(a, h, wa); resize(b, h, wb); resize(c, h, wc); for (int i = 0; i < h; ++i) { for (int j = 0; j < wa; ++j) { cin >> a[i][j]; } for (int j = 0; j < wb; ++j) { cin >> b[i][j]; } for (int j = 0; j < wc; ++j) { cin >> c[i][j]; } cin.ignore(); } } template <typename T> void out(const T &val) { cout << val << endl; } static constexpr int COMBI_MAX = 500000; // nCrのnとrの最大値 static ull gFact[COMBI_MAX + 1]; // modの世界での階乗 static ull gInv[COMBI_MAX + 1]; // modの世界での逆元 static ull gFactInv[COMBI_MAX + 1]; // modの世界での逆元の階乗 static ull gCombiInitMod = 0; // 組み合わせの計算に使用したmod値 // 組み合わせテーブルを作る static void initCombination(ull mod) { gFact[0] = gFact[1] = 1; gFactInv[0] = gFactInv[1] = 1; gInv[1] = 1; for (int i = 2; i <= COMBI_MAX; i++) { gFact[i] = gFact[i - 1] * i % mod; gInv[i] = mod - gInv[mod % i] * (mod / i) % mod; gFactInv[i] = gFactInv[i - 1] * gInv[i] % mod; } gCombiInitMod = mod; } static void initCombination() { initCombination(V_MOD); } // 組み合わせの数を計算 static long long combination(ull n, ull k) { assert(gCombiInitMod > 0 && "先にinitCombinationを呼び出して"); assert(n <= (ull)COMBI_MAX); assert(k <= (ull)COMBI_MAX); if (n < k) return 0; return (gFact[n] * ((gFactInv[k] * gFactInv[n - k]) % V_MOD)) % V_MOD; } } // namespace ValLib using namespace ValLib; int H, W, K; vector<vector<char>> c; vector<vector<int>> a; int solve(int val, bool &success) { vector<int> indexes(H, 0); int count = 0; for (int i = 0; i < H; ++i) { indexes[i] = count; if ((val & (1 << i)) != 0) { ++count; } } ++count; int cut = 0; vector<int> sums(H, 0); for (int x = 0; x < W; ++x) { vector<int> lineSum(indexes.size(), 0); for (int y = 0; y < H; ++y) { lineSum[indexes[y]] += a[y][x]; } for (int y = 0; y < H; ++y) { if (lineSum[indexes[y]] > K) { success = false; return 0; } } for (int i = 0; i < H; ++i) { sums[i] += lineSum[i]; } bool cutFlg = false; for (int y = 0; y < H; ++y) { if (sums[indexes[y]] > K) { cutFlg = true; break; } } if (cutFlg) { ++cut; for (int y = 0; y < H; ++y) { sums[indexes[y]] = lineSum[indexes[y]]; } } } success = true; return cut; } int main() { in(H, W, K); c.resize(H); for (int y = 0; y < H; ++y) { c[y].resize(W); string line; getline(cin, line); for (int x = 0; x < W; ++x) { c[y][x] = line[x]; } } a.resize(H, vector<int>(W, 0)); for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { if (c[y][x] == '1') { a[y][x] = 1; } else { a[y][x] = 0; } } } int sum = 0; for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { sum += a[y][x]; } } if (sum <= K) { out(0); } else { int loopNum = 1; for (int i = 0; i < H; ++i) { loopNum *= 2; } int minNum = 99999999; for (int i = 0; i < loopNum; ++i) { bool success = false; int num = solve(i, success); for (int j = 0; j < H; ++j) { if (((1 << j) & i) != 0) { ++num; } } if (success && num < minNum) { minNum = num; } } out(minNum); } }
replace
675
676
675
676
0
p02733
C++
Runtime Error
#include <algorithm> // minmax, sort, swap #include <bitset> #include <cassert> #include <cmath> #include <cstdio> // printf, scanf #include <deque> // deque #include <functional> // function<void(int)> #include <iomanip> // cout<<setprecision(n) #include <iostream> // cin, cout, cerr #include <map> // key-value pairs sorted by keys #include <numeric> // iota #include <queue> // queue, priority_queue #include <set> // set #include <string> // string, stoi, to_string #include <vector> // vector #ifdef DEBUG #include "debug.hpp" #else #define debug(...) #endif #define int long long // at least int64 > 9*10^18 #define EL '\n' #define rep(i, n) for (int i = 0; i < (n); i++) #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() /* libraries */ signed main() { int h, w, k; std::cin >> h >> w >> k; std::vector<std::string> g(h); rep(i, h) std::cin >> g[i]; const int INF = 1e9; int min = INF; rep(c, 1 << (h - 1)) { int count = __builtin_popcount(c); int bc = count; std::vector<int> ac(bc + 1, 0); rep(i, w) { int j = 0; std::vector<int> ad(bc + 1, 0); rep(xx, h) { ad[j] += (g[xx][i] == '1'); if (c >> xx & 1) j++; } int brk = false; rep(xx, bc + 1) { if (ad[xx] > k) { goto impossible; } if (!brk && ac[xx] + ad[xx] > k) { if (i != 0) count++; ac = ad; brk = true; } } if (!brk) { rep(xx, count + 1) ac[xx] += ad[xx]; } } min = std::min(min, count); impossible:; } print(min); return 0; }
#include <algorithm> // minmax, sort, swap #include <bitset> #include <cassert> #include <cmath> #include <cstdio> // printf, scanf #include <deque> // deque #include <functional> // function<void(int)> #include <iomanip> // cout<<setprecision(n) #include <iostream> // cin, cout, cerr #include <map> // key-value pairs sorted by keys #include <numeric> // iota #include <queue> // queue, priority_queue #include <set> // set #include <string> // string, stoi, to_string #include <vector> // vector #ifdef DEBUG #include "debug.hpp" #else #define debug(...) #endif #define int long long // at least int64 > 9*10^18 #define EL '\n' #define rep(i, n) for (int i = 0; i < (n); i++) #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() /* libraries */ signed main() { int h, w, k; std::cin >> h >> w >> k; std::vector<std::string> g(h); rep(i, h) std::cin >> g[i]; const int INF = 1e9; int min = INF; rep(c, 1 << (h - 1)) { int count = __builtin_popcount(c); int bc = count; std::vector<int> ac(bc + 1, 0); rep(i, w) { int j = 0; std::vector<int> ad(bc + 1, 0); rep(xx, h) { ad[j] += (g[xx][i] == '1'); if (c >> xx & 1) j++; } int brk = false; rep(xx, bc + 1) { if (ad[xx] > k) { goto impossible; } if (!brk && ac[xx] + ad[xx] > k) { if (i != 0) count++; ac = ad; brk = true; } } if (!brk) { rep(xx, bc + 1) ac[xx] += ad[xx]; } } min = std::min(min, count); impossible:; } print(min); return 0; }
replace
61
62
61
62
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int H, W, K; cin >> H >> W >> K; vector<vector<int>> V(H + 1, vector<int>(W + 1, 0)); for (int i = 1; i <= H; i++) { string S; cin >> S; for (int j = 1; j <= W; j++) { if (S[j - 1] == '1') V[i][j]++; V[i][j] += V[i][j - 1]; } } int ans = H + W; for (int tmp = 0; tmp < (1 << (H - 1)); tmp++) { bitset<9> B(tmp); int N = B.count(), lastcut = 0; for (int i = 1; i <= W; i++) { int white = 0; for (int j = 1; j <= H; j++) { white += V[j][i] - V[j][lastcut]; if (white > K) { if (lastcut == i - 1) N += 10000; N++; lastcut = i - 1; i--; break; } if (j != H) if (B.test(j - 1)) white = 0; } } ans = min(ans, N); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int H, W, K; cin >> H >> W >> K; vector<vector<int>> V(H + 1, vector<int>(W + 1, 0)); for (int i = 1; i <= H; i++) { string S; cin >> S; for (int j = 1; j <= W; j++) { if (S[j - 1] == '1') V[i][j]++; V[i][j] += V[i][j - 1]; } } int ans = H + W; for (int tmp = 0; tmp < (1 << (H - 1)); tmp++) { bitset<9> B(tmp); int N = B.count(), lastcut = 0; for (int i = 1; i <= W; i++) { int white = 0; for (int j = 1; j <= H; j++) { white += V[j][i] - V[j][lastcut]; if (white > K) { if (lastcut == i - 1) N += 10000; N++; lastcut = i - 1; i--; break; } if (j != H) if (B.test(j - 1)) white = 0; } if (N > ans) break; } ans = min(ans, N); } cout << ans << endl; return 0; }
insert
37
37
37
39
TLE
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define fi first #define se second #define all(vr) vr.begin(), vr.end() #define pi pair<int, int> typedef long long ll; namespace myrand { mt19937 mt(chrono::system_clock::now().time_since_epoch() / chrono::microseconds(1)); ll Int(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(mt); } } // namespace myrand using namespace myrand; auto ss = chrono::system_clock::now(); double remain_time() { auto en = chrono::system_clock::now(); return (chrono::duration_cast<chrono::nanoseconds>(en - ss).count() * 1e-9); } int a[15][1005], s[15][1005]; int bitmask[15]; int kqtemp, res, H, W, k; vector<int> bitt; int sum(int x, int y, int i, int j) { return (s[x][y] + s[i - 1][j - 1] - s[i - 1][y] - s[x][j - 1]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt","r",stdin); cin >> H >> W >> k; for (int i = 1; i <= H; i++) { string s; cin >> s; s = '@' + s; for (int j = 1; j <= W; j++) a[i][j] = s[j] - '0'; } res = (H - 1) + (W - 1); bitmask[0] = 1; for (int i = 1; i <= H + 1; i++) bitmask[i] = bitmask[i - 1] * 2; for (int i = 1; i <= H; i++) for (int j = 1; j <= W; j++) s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j]; for (int i = bitmask[H - 1] - 1; i >= 0; i--) { bitt.clear(); bitt.push_back(0); for (int j = 0; j <= H - 2; j++) { if ((i >> j) & 1 == 1) bitt.push_back(j + 1); } int j1 = W; kqtemp = __builtin_popcount(i); bitt.push_back(H); while (j1 >= 1) { int temp = j1; bool flag = true; while (temp >= 1) { for (int bit = bitt.size() - 2; bit >= 0; bit--) { if (bitt[bit] == -1) continue; int cucu = sum(bitt[bit + 1], j1, bitt[bit] + 1, temp); if (sum(bitt[bit + 1], j1, bitt[bit] + 1, temp) > k) { flag = false; break; } } if (!flag) break; else temp--; } if (flag == false) kqtemp++; if (flag == true && temp <= 1) { j1 = 0; break; } j1 = temp; } res = min(res, kqtemp); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define fi first #define se second #define all(vr) vr.begin(), vr.end() #define pi pair<int, int> typedef long long ll; namespace myrand { mt19937 mt(chrono::system_clock::now().time_since_epoch() / chrono::microseconds(1)); ll Int(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(mt); } } // namespace myrand using namespace myrand; auto ss = chrono::system_clock::now(); double remain_time() { auto en = chrono::system_clock::now(); return (chrono::duration_cast<chrono::nanoseconds>(en - ss).count() * 1e-9); } int a[15][1005], s[15][1005]; int bitmask[15]; int kqtemp, res, H, W, k; vector<int> bitt; int sum(int x, int y, int i, int j) { return (s[x][y] + s[i - 1][j - 1] - s[i - 1][y] - s[x][j - 1]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt","r",stdin); cin >> H >> W >> k; for (int i = 1; i <= H; i++) { string s; cin >> s; s = '@' + s; for (int j = 1; j <= W; j++) a[i][j] = s[j] - '0'; } res = (H - 1) + (W - 1); bitmask[0] = 1; for (int i = 1; i <= H + 1; i++) bitmask[i] = bitmask[i - 1] * 2; for (int i = 1; i <= H; i++) for (int j = 1; j <= W; j++) s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j]; for (int i = bitmask[H - 1] - 1; i >= 0; i--) { bitt.clear(); bitt.push_back(0); for (int j = 0; j <= H - 2; j++) { if ((i >> j) & 1 == 1) bitt.push_back(j + 1); } int j1 = W; kqtemp = __builtin_popcount(i); bitt.push_back(H); while (j1 >= 1) { int temp = j1; bool flag = true; while (temp >= 1) { for (int bit = bitt.size() - 2; bit >= 0; bit--) { if (bitt[bit] == -1) continue; int cucu = sum(bitt[bit + 1], j1, bitt[bit] + 1, temp); if (sum(bitt[bit + 1], j1, bitt[bit] + 1, temp) > k) { flag = false; break; } } if (!flag) break; else temp--; } if (flag == false) kqtemp++; if (flag == true && temp <= 1) { j1 = 0; break; } if (j1 == temp && flag == false) { kqtemp = 1000000; break; } j1 = temp; } res = min(res, kqtemp); } cout << res << endl; return 0; }
insert
85
85
85
89
TLE
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define MOD 1000000007 #define mod 1000007 #define INF63 1061109567 #define INF127 9187201950435737471 #define UINF 18446744073709551615 #define F first #define S second #define ll long long #define N 1010 using namespace std; ll rint() { ll ret = 0, uu = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') uu = -1, c = getchar(); while (isdigit(c)) ret = ret * 10 + (c - '0'), c = getchar(); return ret * uu; } int n, m, k, nm[11], num[11], ans = INF, tmp, tp; char grid[11][N]; int main() { int i, j, l, lst; n = rint(); m = rint(); k = rint(); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf(" %c", &grid[i][j]); } } for (i = 0; i < (1 << (n - 1)); i++) { memset(nm, 0, sizeof(nm)); for (j = 0; j + 1 < n; j++) { if (i & (1 << j)) { nm[j + 1] = nm[j] + 1; } else { nm[j + 1] = nm[j]; } } tmp = 0; tp = __builtin_popcount(i); memset(num, 0, sizeof(num)); for (j = m - 1; j >= 0; j--) { if (tmp + tp >= ans) { break; } for (l = 0; l < n; l++) { if (grid[l][j] == '1') { num[nm[l]]++; } if (num[nm[l]] > k) { l = -1; break; } } if (l == -1) { tmp++; j++; memset(num, 0, sizeof(num)); } } ans = min(ans, tmp + tp); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define MOD 1000000007 #define mod 1000007 #define INF63 1061109567 #define INF127 9187201950435737471 #define UINF 18446744073709551615 #define F first #define S second #define ll long long #define N 1010 using namespace std; ll rint() { ll ret = 0, uu = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') uu = -1, c = getchar(); while (isdigit(c)) ret = ret * 10 + (c - '0'), c = getchar(); return ret * uu; } int n, m, k, nm[11], num[11], ans = INF, tmp, tp; char grid[11][N]; int main() { int i, j, l, lst; n = rint(); m = rint(); k = rint(); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf(" %c", &grid[i][j]); } } for (i = (1 << n) - 1; i >= 0; i--) { memset(nm, 0, sizeof(nm)); for (j = 0; j + 1 < n; j++) { if (i & (1 << j)) { nm[j + 1] = nm[j] + 1; } else { nm[j + 1] = nm[j]; } } tmp = 0; tp = __builtin_popcount(i); memset(num, 0, sizeof(num)); for (j = m - 1; j >= 0; j--) { if (tmp + tp >= ans) { break; } for (l = 0; l < n; l++) { if (grid[l][j] == '1') { num[nm[l]]++; } if (num[nm[l]] > k) { l = -1; break; } } if (l == -1) { tmp++; j++; memset(num, 0, sizeof(num)); } } ans = min(ans, tmp + tp); } cout << ans << endl; return 0; }
replace
36
37
36
37
TLE
p02733
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <regex> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) typedef int long long ll; using namespace std; typedef pair<int, int> P; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll MOD = 1e9 + 7; static const int MAX = 100; static const int INF = (1 << 23); template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } int main() { int h, w, kk; cin >> h >> w >> kk; vector<vector<int>> v(h, vector<int>(w, 0)); rep(i, h) { string s; cin >> s; int ssum = 0; rep(j, s.size()) { v[i][j] = (s[j] == '1' ? 1 : 0); if (s[j] == '1') { ssum++; } } } ll zen = pow(2, h - 1) + 1; ll ans = MOD; for (int i = 0; i < zen; i++) { ll k = i; vector<int> q; q.push_back(0); for (int j = 0; j < h - 1; j++) { if ((k >> j) & 1) { q.push_back(j + 1); } } q.push_back(h); bool ff = false; vector<int> rui(q.size() - 1, 0); vector<bool> line(w, false); ll ssum = 0; for (int j = 0; j < w; j++) { vector<int> srui(q.size() - 1, 0); for (int jj = 0; jj < q.size() - 1; jj++) { int ksum = 0; for (int t = q[jj]; t < q[jj + 1]; t++) { if (v[t][j]) { srui[jj]++; } } if (srui[jj] > kk) { ff = true; break; } } if (ff) break; bool f = false; rep(jk, q.size()) { if (srui[jk] + rui[jk] > kk) { f = true; } else { rui[jk] += srui[jk]; } } if (f) { ssum++; rui = srui; } } if (!ff) { ssum += q.size() - 2; ans = min(ans, ssum); } } cout << ans << endl; return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <regex> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) typedef int long long ll; using namespace std; typedef pair<int, int> P; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const ll MOD = 1e9 + 7; static const int MAX = 100; static const int INF = (1 << 23); template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } int main() { int h, w, kk; cin >> h >> w >> kk; vector<vector<int>> v(h, vector<int>(w, 0)); rep(i, h) { string s; cin >> s; int ssum = 0; rep(j, s.size()) { v[i][j] = (s[j] == '1' ? 1 : 0); if (s[j] == '1') { ssum++; } } } ll zen = pow(2, h - 1) + 1; ll ans = MOD; for (int i = 0; i < zen; i++) { ll k = i; vector<int> q; q.push_back(0); for (int j = 0; j < h - 1; j++) { if ((k >> j) & 1) { q.push_back(j + 1); } } q.push_back(h); bool ff = false; vector<int> rui(q.size() - 1, 0); vector<bool> line(w, false); ll ssum = 0; for (int j = 0; j < w; j++) { vector<int> srui(q.size() - 1, 0); for (int jj = 0; jj < q.size() - 1; jj++) { int ksum = 0; for (int t = q[jj]; t < q[jj + 1]; t++) { if (v[t][j]) { srui[jj]++; } } if (srui[jj] > kk) { ff = true; break; } } if (ff) break; bool f = false; rep(jk, q.size() - 1) { if (srui[jk] + rui[jk] > kk) { f = true; } else { rui[jk] += srui[jk]; } } if (f) { ssum++; rui = srui; } } if (!ff) { ssum += q.size() - 2; ans = min(ans, ssum); } } cout << ans << endl; return 0; }
replace
83
84
83
84
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; using ll = long long; int main() { int mp[13][1005] = {}; int h, w, k; cin >> h >> w >> k; rep(i, h) rep(j, w) scanf("%1d", &mp[i + 1][j + 1]); rep(i, h - 1) rep(j, w) mp[i + 2][j + 1] += mp[i + 1][j + 1]; rep(j, w - 1) rep(i, h) mp[i + 1][j + 2] += mp[i + 1][j + 1]; int ans = 1e9; for (int i = 0; i < (1 << (h - 1)); i++) { int cut = 0; rep(x, h - 1) if (i >> x & 1) cut++; int afterH = 0; int afterW = 0; bool small = true; int now = 1; while (now <= w) { int c; afterH = 0; small = true; for (int b = 0; b < (h - 1); b++) { if (i >> b & 1) { c = mp[b + 1][now] - mp[b + 1][afterW] - mp[afterH][now] + mp[afterH][afterW]; if (c > k) small = false; afterH = b + 1; } } c = mp[h][now] - mp[h][afterW] - mp[afterH][now] + mp[afterH][afterW]; if (c > k) small = false; if (small == false) { cut++; afterW = now - 1; now--; } now++; } ans = min(ans, cut); } cout << ans << endl; return 0; } //......mp[ah][aw] ..............mp[ah][now] //......mp[b+1][aw]..............mp[b+1][now]
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; using ll = long long; int main() { int mp[13][1005] = {}; int h, w, k; cin >> h >> w >> k; rep(i, h) rep(j, w) scanf("%1d", &mp[i + 1][j + 1]); rep(i, h - 1) rep(j, w) mp[i + 2][j + 1] += mp[i + 1][j + 1]; rep(j, w - 1) rep(i, h) mp[i + 1][j + 2] += mp[i + 1][j + 1]; int ans = 1e9; for (int i = 0; i < (1 << (h - 1)); i++) { int cut = 0; rep(x, h - 1) if (i >> x & 1) cut++; int afterH = 0; int afterW = 0; bool small = true; int now = 1; while (now <= w) { int c; afterH = 0; small = true; for (int b = 0; b < (h - 1); b++) { if (i >> b & 1) { c = mp[b + 1][now] - mp[b + 1][afterW] - mp[afterH][now] + mp[afterH][afterW]; if (c > k) small = false; afterH = b + 1; } } c = mp[h][now] - mp[h][afterW] - mp[afterH][now] + mp[afterH][afterW]; if (c > k) small = false; if (small == false) { if (afterW == now - 1) { cut = 1e9; now++; } cut++; afterW = now - 1; now--; } now++; } ans = min(ans, cut); } cout << ans << endl; return 0; } //......mp[ah][aw] ..............mp[ah][now] //......mp[b+1][aw]..............mp[b+1][now]
insert
39
39
39
43
TLE
p02733
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define ll int64_t #define Rep(i, n) for (ll i = 0; i < n; i++) using namespace std; typedef vector<ll> vec; typedef vector<vec> mat; const ll inf = 1LL << 60; template <class T> inline void chmin(T &a, T b) { if (a > b) { a = b; } } template <class T> inline void chmax(T &a, T b) { if (a < b) { a = b; } } int main() { // cin.tie(0); // ios::sync_with_stdio(false); ll H, W, K; cin >> H >> W >> K; vector<string> S(H); Rep(i, H) { cin >> S[i]; } mat s(H, vec(W + 1, 0)); Rep(i, H) { Rep(j, W) { s[i][j + 1] += s[i][j] + (S[i][j] == '1'); } } ll ans = inf; Rep(i, 1 << (H - 1)) { ll tmp = 0; Rep(j, H - 1) { if (i & (1 << j)) { tmp++; } } mat cnt(tmp + 1, vec(W + 1, 0)); ll now = 0; Rep(j, H) { Rep(k, W + 1) { // cout << now << "\n"; cnt[now][k] += s[j][k]; } if (i & (1 << j)) { now++; } } // cout << i << "\n"; // Rep (j, tmp+1) { // Rep (k, W+1) { // cout << cnt[j][k] << " \n"[k==W]; // } // } ll a = tmp + 1; ll left = 0; while (left < W) { ll right = inf; Rep(j, a) { auto e = upper_bound(cnt[j].begin(), cnt[j].end(), cnt[j][left] + K); chmin(right, e - cnt[j].begin()); } // cout << i << " " << left << " " << right << "\n"; if (right < W + 1) { tmp++; left = right - 1; } else if (right == W + 1) { left = right - 1; } } chmin(ans, tmp); } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define ll int64_t #define Rep(i, n) for (ll i = 0; i < n; i++) using namespace std; typedef vector<ll> vec; typedef vector<vec> mat; const ll inf = 1LL << 60; template <class T> inline void chmin(T &a, T b) { if (a > b) { a = b; } } template <class T> inline void chmax(T &a, T b) { if (a < b) { a = b; } } int main() { // cin.tie(0); // ios::sync_with_stdio(false); ll H, W, K; cin >> H >> W >> K; vector<string> S(H); Rep(i, H) { cin >> S[i]; } mat s(H, vec(W + 1, 0)); Rep(i, H) { Rep(j, W) { s[i][j + 1] += s[i][j] + (S[i][j] == '1'); } } ll ans = inf; Rep(i, 1 << (H - 1)) { ll tmp = 0; Rep(j, H - 1) { if (i & (1 << j)) { tmp++; } } mat cnt(tmp + 1, vec(W + 1, 0)); ll now = 0; Rep(j, H) { Rep(k, W + 1) { // cout << now << "\n"; cnt[now][k] += s[j][k]; } if (i & (1 << j)) { now++; } } // cout << i << "\n"; // Rep (j, tmp+1) { // Rep (k, W+1) { // cout << cnt[j][k] << " \n"[k==W]; // } // } ll a = tmp + 1; ll left = 0; while (left < W) { ll right = inf; Rep(j, a) { auto e = upper_bound(cnt[j].begin(), cnt[j].end(), cnt[j][left] + K); chmin(right, e - cnt[j].begin()); } // cout << i << " " << left << " " << right << "\n"; if (left + 1 == right) { tmp = inf; break; } if (right < W + 1) { tmp++; left = right - 1; } else if (right == W + 1) { left = right - 1; } } chmin(ans, tmp); } cout << ans << endl; }
insert
88
88
88
92
TLE
p02733
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; char **Sdata; int H, W, K; int MaxVerticalBar(int offset, vector<int> RegionEdge, int BarNumber) { int MAX = offset; vector<int> Chocolates(BarNumber + 1, 0); int ChocoEnd = 0; for (MAX = offset; MAX < W; MAX++) { for (int i = 0; i <= BarNumber; i++) for (int i2 = RegionEdge[i]; i2 < RegionEdge[i + 1]; i2++) { Chocolates[i] += (int)(Sdata[i2][MAX] - '0'); if (Chocolates[i] > K) { if (MAX != 0) return MAX - 1; else return 0; } } } return MAX; }; int main(void) { cin >> H >> W >> K; Sdata = new char *[H]; for (int i = 0; i < H; i++) { Sdata[i] = new char[W]; for (int j = 0; j < W; j++) cin >> Sdata[i][j]; } int BarNumber = 0; vector<int> RegionEdge(H + 1, 0); int WorldRecord = H + W; for (int UnderBar = 0; UnderBar < (1 << (H - 1)); UnderBar++) { BarNumber = 0; for (int i = 0; i < H; i++) { if (((UnderBar >> i) & 1) != 0) { RegionEdge[BarNumber + 1] = i + 1; BarNumber++; } } RegionEdge[BarNumber + 1] = H; if (BarNumber < WorldRecord) { int answer = BarNumber; for (int offset = 0; offset < W; offset++) { int VerticalBar; VerticalBar = MaxVerticalBar(offset, RegionEdge, BarNumber); if (VerticalBar != W) answer++; offset = VerticalBar; } if (WorldRecord > answer) WorldRecord = answer; } } cout << WorldRecord << endl; for (int i = 0; i < H; i++) delete[] Sdata[i]; delete[] Sdata; }
#include <iostream> #include <vector> using namespace std; char **Sdata; int H, W, K; int MaxVerticalBar(int offset, vector<int> RegionEdge, int BarNumber) { int MAX = offset; vector<int> Chocolates(BarNumber + 1, 0); int ChocoEnd = 0; for (MAX = offset; MAX < W; MAX++) { for (int i = 0; i <= BarNumber; i++) for (int i2 = RegionEdge[i]; i2 < RegionEdge[i + 1]; i2++) { Chocolates[i] += (int)(Sdata[i2][MAX] - '0'); if (Chocolates[i] > K) { if (MAX != 0) return MAX - 1; else return 0; } } } return MAX; }; int main(void) { cin >> H >> W >> K; Sdata = new char *[H]; for (int i = 0; i < H; i++) { Sdata[i] = new char[W]; for (int j = 0; j < W; j++) cin >> Sdata[i][j]; } int BarNumber = 0; vector<int> RegionEdge(H + 1, 0); int WorldRecord = H + W; for (int UnderBar = 0; UnderBar < (1 << (H - 1)); UnderBar++) { BarNumber = 0; for (int i = 0; i < H; i++) { if (((UnderBar >> i) & 1) != 0) { RegionEdge[BarNumber + 1] = i + 1; BarNumber++; } } RegionEdge[BarNumber + 1] = H; if (BarNumber < WorldRecord) { int answer = BarNumber; for (int offset = 0; offset < W; offset++) { int VerticalBar; VerticalBar = MaxVerticalBar(offset, RegionEdge, BarNumber); if (VerticalBar != W) answer++; if (WorldRecord <= answer) break; offset = VerticalBar; } if (WorldRecord > answer) WorldRecord = answer; } } cout << WorldRecord << endl; for (int i = 0; i < H; i++) delete[] Sdata[i]; delete[] Sdata; }
insert
55
55
55
57
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; using P = pair<ll, ll>; const ll mod = 1e9 + 7; #define ALL(x) (x).begin(), (x).end() #define pow(x, y) modpow(x, y) #define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++) template <class T = int> T in() { T x; cin >> x; return (x); } /* modとる */ inline ll modu(ll a) { return (a % mod + mod) % mod; } /* modつき二分累乗法 */ ll modpow(ll a, ll b) { ll res = 1; for (a = modu(a); b; a = modu(a * a), b >>= 1) { if (b & 1) res = modu(res * a); } return res; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); int H = in(), W = in(), K = in(); vector<vector<int>> S(H + 1, vector<int>(W + 1)); REP(i, H + 1) { REP(l, W + 1) { if (i == 0) { S[i][l] = 0; } else if (l == 0) { S[i][l] = 0; } else { S[i][l] = S[i - 1][l] + S[i][l - 1] - S[i - 1][l - 1] + (in<char>() == '1' ? 1 : 0); } } } int answer = INT32_MAX - 50; vector<int> where; REP(i, modpow(2, H - 1)) { int tmpe = i; where.assign(1, false); int counn = 0; while (tmpe != 0) { if (tmpe % 2 == 1) where.push_back(counn + 1); counn++; tmpe = tmpe >> 1; } where.push_back(H); int point = 0; int beforecut = 0; REP(i, W) { bool out = 0; REP(l, where.size()) { if (S[where[l + 1]][i + 1] - S[where[l]][i + 1] - S[where[l + 1]][beforecut] + S[where[l]][beforecut] > K) { if (beforecut == i) { out = 1; break; } beforecut = i; point++; break; } } if (out) { point = INT32_MAX - 50000; break; } } int wheresize = where.size(); answer = min(answer, point + wheresize - 2); } cout << answer << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; using P = pair<ll, ll>; const ll mod = 1e9 + 7; #define ALL(x) (x).begin(), (x).end() #define pow(x, y) modpow(x, y) #define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++) template <class T = int> T in() { T x; cin >> x; return (x); } /* modとる */ inline ll modu(ll a) { return (a % mod + mod) % mod; } /* modつき二分累乗法 */ ll modpow(ll a, ll b) { ll res = 1; for (a = modu(a); b; a = modu(a * a), b >>= 1) { if (b & 1) res = modu(res * a); } return res; } signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); int H = in(), W = in(), K = in(); vector<vector<int>> S(H + 1, vector<int>(W + 1)); REP(i, H + 1) { REP(l, W + 1) { if (i == 0) { S[i][l] = 0; } else if (l == 0) { S[i][l] = 0; } else { S[i][l] = S[i - 1][l] + S[i][l - 1] - S[i - 1][l - 1] + (in<char>() == '1' ? 1 : 0); } } } int answer = INT32_MAX - 50; vector<int> where; REP(i, modpow(2, H - 1)) { int tmpe = i; where.assign(1, false); int counn = 0; while (tmpe != 0) { if (tmpe % 2 == 1) where.push_back(counn + 1); counn++; tmpe = tmpe >> 1; } where.push_back(H); int point = 0; int beforecut = 0; REP(i, W) { bool out = 0; REP(l, where.size() - 1) { if (S[where[l + 1]][i + 1] - S[where[l]][i + 1] - S[where[l + 1]][beforecut] + S[where[l]][beforecut] > K) { if (beforecut == i) { out = 1; break; } beforecut = i; point++; break; } } if (out) { point = INT32_MAX - 50000; break; } } int wheresize = where.size(); answer = min(answer, point + wheresize - 2); } cout << answer << "\n"; }
replace
63
64
63
64
0
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) #define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i) #define R0F(i, a) ROF(i, 0, a) using ll = long long; using ld = long double; ll INF = LLONG_MAX; using vi = vector<int>; using vll = vector<ll>; using pii = pair<int, int>; namespace output { void pr(int x) { cout << x; } void pr(long x) { cout << x; } void pr(ll x) { cout << x; } void pr(unsigned x) { cout << x; } void pr(unsigned long x) { cout << x; } void pr(unsigned long long x) { cout << x; } void pr(float x) { cout << x; } void pr(double x) { cout << x; } void pr(ld x) { cout << x; } void pr(char x) { cout << x; } void pr(const char *x) { cout << x; } void pr(const string &x) { cout << x; } void pr(bool x) { pr(x ? "true" : "false"); } template <class T> void pr(const complex<T> &x) { cout << x; } template <class T1, class T2> void pr(const pair<T1, T2> &x); template <class T> void pr(const T &x); template <class T, class... Ts> void pr(const T &t, const Ts &...ts) { pr(t); pr(ts...); } template <class T1, class T2> void pr(const pair<T1, T2> &x) { pr("{", x.first, ", ", x.second, "}"); } template <class T> void pr(const T &x) { pr("{"); // const iterator needed for vector<bool> bool fst = 1; for (const auto &a : x) pr(!fst ? ", " : "", a), fst = 0; pr("}"); } void print() { pr("\n"); } // print w/ spaces template <class T, class... Ts> void print(const T &t, const Ts &...ts) { pr(t); if (sizeof...(ts)) pr(" "); print(ts...); } } // namespace output using namespace output; vector<vi> cumsum(11, vi(10001)); vector<vector<pii>> parts = { {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 9}}, {{0, 0}, {1, 2}, {3, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 9}}, {{0, 0}, {1, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 6}, {7, 9}}, {{0, 0}, {1, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 9}}, {{0, 0}, {1, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 5}, {6, 9}}, {{0, 0}, {1, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 6}, {7, 9}}, {{0, 0}, {1, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 7}, {8, 9}}, {{0, 0}, {1, 8}, {9, 9}}, {{0, 0}, {1, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 9}}, {{0, 1}, {2, 2}, {3, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 9}}, {{0, 1}, {2, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 6}, {7, 9}}, {{0, 1}, {2, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 9}}, {{0, 1}, {2, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 6}, {7, 9}}, {{0, 1}, {2, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 7}, {8, 9}}, {{0, 1}, {2, 4}, {5, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 9}}, {{0, 1}, {2, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 5}, {6, 9}}, {{0, 1}, {2, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 6}, {7, 9}}, {{0, 1}, {2, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 7}, {8, 9}}, {{0, 1}, {2, 8}, {9, 9}}, {{0, 1}, {2, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 9}}, {{0, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 6}, {7, 9}}, {{0, 2}, {3, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 9}}, {{0, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 6}, {7, 9}}, {{0, 2}, {3, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 7}, {8, 9}}, {{0, 2}, {3, 4}, {5, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 9}}, {{0, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 5}, {6, 6}, {7, 9}}, {{0, 2}, {3, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 5}, {6, 7}, {8, 9}}, {{0, 2}, {3, 5}, {6, 8}, {9, 9}}, {{0, 2}, {3, 5}, {6, 9}}, {{0, 2}, {3, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 6}, {7, 9}}, {{0, 2}, {3, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 7}, {8, 9}}, {{0, 2}, {3, 8}, {9, 9}}, {{0, 2}, {3, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 9}}, {{0, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 3}, {4, 5}, {6, 9}}, {{0, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 3}, {4, 6}, {7, 9}}, {{0, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 7}, {8, 9}}, {{0, 3}, {4, 8}, {9, 9}}, {{0, 3}, {4, 9}}, {{0, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 4}, {5, 5}, {6, 9}}, {{0, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 4}, {5, 6}, {7, 9}}, {{0, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 4}, {5, 7}, {8, 9}}, {{0, 4}, {5, 8}, {9, 9}}, {{0, 4}, {5, 9}}, {{0, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 5}, {6, 6}, {7, 9}}, {{0, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 5}, {6, 7}, {8, 9}}, {{0, 5}, {6, 8}, {9, 9}}, {{0, 5}, {6, 9}}, {{0, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 6}, {7, 7}, {8, 9}}, {{0, 6}, {7, 8}, {9, 9}}, {{0, 6}, {7, 9}}, {{0, 7}, {8, 8}, {9, 9}}, {{0, 7}, {8, 9}}, {{0, 8}, {9, 9}}, {{0, 9}}}; int query(int x1, int x2, int y1, int y2) { // print(cumsum[y2+1][x2+1], cumsum[y2+1][x1], cumsum[y1][x2+1], // cumsum[y1][x1]); return cumsum[y2 + 1][x2 + 1] - cumsum[y2 + 1][x1] - cumsum[y1][x2 + 1] + cumsum[y1][x1]; // blah } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int H, W, K; cin >> H >> W >> K; vector<vi> choc(10, vi(W, 0)); F0R(i, H) { F0R(j, W) { char c; cin >> c; choc[i][j] = (c == '1'); } } F0R(i, 10) { F0R(j, 1000) { cumsum[i + 1][j + 1] = cumsum[i][j + 1] + cumsum[i + 1][j] - cumsum[i][j] + choc[i][j]; } } int best = H * W; for (vector<pii> part : parts) { int cuts = part.size() - 1; // print(part); int lastx = 0; F0R(x, W) { int most = 0; for (pii group : part) { // most = 0; most = max(most, query(lastx, x, group.first, group.second)); } // print(lastx, x, most, query(lastx, x, part[0].first, part[0].second)); if (most > K) { if (lastx == x) cuts += H * W; cuts++; lastx = x; } } // print(part, cuts); best = min(best, cuts); } print(best); }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) #define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i) #define R0F(i, a) ROF(i, 0, a) using ll = long long; using ld = long double; ll INF = LLONG_MAX; using vi = vector<int>; using vll = vector<ll>; using pii = pair<int, int>; namespace output { void pr(int x) { cout << x; } void pr(long x) { cout << x; } void pr(ll x) { cout << x; } void pr(unsigned x) { cout << x; } void pr(unsigned long x) { cout << x; } void pr(unsigned long long x) { cout << x; } void pr(float x) { cout << x; } void pr(double x) { cout << x; } void pr(ld x) { cout << x; } void pr(char x) { cout << x; } void pr(const char *x) { cout << x; } void pr(const string &x) { cout << x; } void pr(bool x) { pr(x ? "true" : "false"); } template <class T> void pr(const complex<T> &x) { cout << x; } template <class T1, class T2> void pr(const pair<T1, T2> &x); template <class T> void pr(const T &x); template <class T, class... Ts> void pr(const T &t, const Ts &...ts) { pr(t); pr(ts...); } template <class T1, class T2> void pr(const pair<T1, T2> &x) { pr("{", x.first, ", ", x.second, "}"); } template <class T> void pr(const T &x) { pr("{"); // const iterator needed for vector<bool> bool fst = 1; for (const auto &a : x) pr(!fst ? ", " : "", a), fst = 0; pr("}"); } void print() { pr("\n"); } // print w/ spaces template <class T, class... Ts> void print(const T &t, const Ts &...ts) { pr(t); if (sizeof...(ts)) pr(" "); print(ts...); } } // namespace output using namespace output; vector<vi> cumsum(11, vi(10001)); vector<vector<pii>> parts = { {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 4}, {5, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 2}, {3, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 4}, {5, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 3}, {4, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 4}, {5, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 5}, {6, 9}}, {{0, 0}, {1, 1}, {2, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 6}, {7, 9}}, {{0, 0}, {1, 1}, {2, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 7}, {8, 9}}, {{0, 0}, {1, 1}, {2, 8}, {9, 9}}, {{0, 0}, {1, 1}, {2, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 4}, {5, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 5}, {6, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 3}, {4, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 4}, {5, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 5}, {6, 9}}, {{0, 0}, {1, 2}, {3, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 6}, {7, 9}}, {{0, 0}, {1, 2}, {3, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 7}, {8, 9}}, {{0, 0}, {1, 2}, {3, 8}, {9, 9}}, {{0, 0}, {1, 2}, {3, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 4}, {5, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 5}, {6, 9}}, {{0, 0}, {1, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 6}, {7, 9}}, {{0, 0}, {1, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 7}, {8, 9}}, {{0, 0}, {1, 3}, {4, 8}, {9, 9}}, {{0, 0}, {1, 3}, {4, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 5}, {6, 9}}, {{0, 0}, {1, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 6}, {7, 9}}, {{0, 0}, {1, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 7}, {8, 9}}, {{0, 0}, {1, 4}, {5, 8}, {9, 9}}, {{0, 0}, {1, 4}, {5, 9}}, {{0, 0}, {1, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 5}, {6, 6}, {7, 9}}, {{0, 0}, {1, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 5}, {6, 7}, {8, 9}}, {{0, 0}, {1, 5}, {6, 8}, {9, 9}}, {{0, 0}, {1, 5}, {6, 9}}, {{0, 0}, {1, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 6}, {7, 7}, {8, 9}}, {{0, 0}, {1, 6}, {7, 8}, {9, 9}}, {{0, 0}, {1, 6}, {7, 9}}, {{0, 0}, {1, 7}, {8, 8}, {9, 9}}, {{0, 0}, {1, 7}, {8, 9}}, {{0, 0}, {1, 8}, {9, 9}}, {{0, 0}, {1, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 5}, {6, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 3}, {4, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 4}, {5, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 5}, {6, 9}}, {{0, 1}, {2, 2}, {3, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 6}, {7, 9}}, {{0, 1}, {2, 2}, {3, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 7}, {8, 9}}, {{0, 1}, {2, 2}, {3, 8}, {9, 9}}, {{0, 1}, {2, 2}, {3, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 4}, {5, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 5}, {6, 9}}, {{0, 1}, {2, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 6}, {7, 9}}, {{0, 1}, {2, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 7}, {8, 9}}, {{0, 1}, {2, 3}, {4, 8}, {9, 9}}, {{0, 1}, {2, 3}, {4, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 5}, {6, 9}}, {{0, 1}, {2, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 6}, {7, 9}}, {{0, 1}, {2, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 7}, {8, 9}}, {{0, 1}, {2, 4}, {5, 8}, {9, 9}}, {{0, 1}, {2, 4}, {5, 9}}, {{0, 1}, {2, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 5}, {6, 6}, {7, 9}}, {{0, 1}, {2, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 5}, {6, 7}, {8, 9}}, {{0, 1}, {2, 5}, {6, 8}, {9, 9}}, {{0, 1}, {2, 5}, {6, 9}}, {{0, 1}, {2, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 6}, {7, 7}, {8, 9}}, {{0, 1}, {2, 6}, {7, 8}, {9, 9}}, {{0, 1}, {2, 6}, {7, 9}}, {{0, 1}, {2, 7}, {8, 8}, {9, 9}}, {{0, 1}, {2, 7}, {8, 9}}, {{0, 1}, {2, 8}, {9, 9}}, {{0, 1}, {2, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 4}, {5, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 5}, {6, 9}}, {{0, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 6}, {7, 9}}, {{0, 2}, {3, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 7}, {8, 9}}, {{0, 2}, {3, 3}, {4, 8}, {9, 9}}, {{0, 2}, {3, 3}, {4, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 5}, {6, 9}}, {{0, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 6}, {7, 9}}, {{0, 2}, {3, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 7}, {8, 9}}, {{0, 2}, {3, 4}, {5, 8}, {9, 9}}, {{0, 2}, {3, 4}, {5, 9}}, {{0, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 5}, {6, 6}, {7, 9}}, {{0, 2}, {3, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 5}, {6, 7}, {8, 9}}, {{0, 2}, {3, 5}, {6, 8}, {9, 9}}, {{0, 2}, {3, 5}, {6, 9}}, {{0, 2}, {3, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 6}, {7, 7}, {8, 9}}, {{0, 2}, {3, 6}, {7, 8}, {9, 9}}, {{0, 2}, {3, 6}, {7, 9}}, {{0, 2}, {3, 7}, {8, 8}, {9, 9}}, {{0, 2}, {3, 7}, {8, 9}}, {{0, 2}, {3, 8}, {9, 9}}, {{0, 2}, {3, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 5}, {6, 9}}, {{0, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 3}, {4, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 6}, {7, 9}}, {{0, 3}, {4, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 7}, {8, 9}}, {{0, 3}, {4, 4}, {5, 8}, {9, 9}}, {{0, 3}, {4, 4}, {5, 9}}, {{0, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 3}, {4, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 3}, {4, 5}, {6, 6}, {7, 9}}, {{0, 3}, {4, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 5}, {6, 7}, {8, 9}}, {{0, 3}, {4, 5}, {6, 8}, {9, 9}}, {{0, 3}, {4, 5}, {6, 9}}, {{0, 3}, {4, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 6}, {7, 7}, {8, 9}}, {{0, 3}, {4, 6}, {7, 8}, {9, 9}}, {{0, 3}, {4, 6}, {7, 9}}, {{0, 3}, {4, 7}, {8, 8}, {9, 9}}, {{0, 3}, {4, 7}, {8, 9}}, {{0, 3}, {4, 8}, {9, 9}}, {{0, 3}, {4, 9}}, {{0, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 4}, {5, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 4}, {5, 5}, {6, 6}, {7, 9}}, {{0, 4}, {5, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 4}, {5, 5}, {6, 7}, {8, 9}}, {{0, 4}, {5, 5}, {6, 8}, {9, 9}}, {{0, 4}, {5, 5}, {6, 9}}, {{0, 4}, {5, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 4}, {5, 6}, {7, 7}, {8, 9}}, {{0, 4}, {5, 6}, {7, 8}, {9, 9}}, {{0, 4}, {5, 6}, {7, 9}}, {{0, 4}, {5, 7}, {8, 8}, {9, 9}}, {{0, 4}, {5, 7}, {8, 9}}, {{0, 4}, {5, 8}, {9, 9}}, {{0, 4}, {5, 9}}, {{0, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 5}, {6, 6}, {7, 7}, {8, 9}}, {{0, 5}, {6, 6}, {7, 8}, {9, 9}}, {{0, 5}, {6, 6}, {7, 9}}, {{0, 5}, {6, 7}, {8, 8}, {9, 9}}, {{0, 5}, {6, 7}, {8, 9}}, {{0, 5}, {6, 8}, {9, 9}}, {{0, 5}, {6, 9}}, {{0, 6}, {7, 7}, {8, 8}, {9, 9}}, {{0, 6}, {7, 7}, {8, 9}}, {{0, 6}, {7, 8}, {9, 9}}, {{0, 6}, {7, 9}}, {{0, 7}, {8, 8}, {9, 9}}, {{0, 7}, {8, 9}}, {{0, 8}, {9, 9}}, {{0, 9}}}; int query(int x1, int x2, int y1, int y2) { // print(cumsum[y2+1][x2+1], cumsum[y2+1][x1], cumsum[y1][x2+1], // cumsum[y1][x1]); return cumsum[y2 + 1][x2 + 1] - cumsum[y2 + 1][x1] - cumsum[y1][x2 + 1] + cumsum[y1][x1]; // blah } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int H, W, K; cin >> H >> W >> K; vector<vi> choc(15, vi(1005, 0)); F0R(i, H) { F0R(j, W) { char c; cin >> c; choc[i][j] = (c == '1'); } } F0R(i, 10) { F0R(j, 1000) { cumsum[i + 1][j + 1] = cumsum[i][j + 1] + cumsum[i + 1][j] - cumsum[i][j] + choc[i][j]; } } int best = H * W; for (vector<pii> part : parts) { int cuts = part.size() - 1; // print(part); int lastx = 0; F0R(x, W) { int most = 0; for (pii group : part) { // most = 0; most = max(most, query(lastx, x, group.first, group.second)); } // print(lastx, x, most, query(lastx, x, part[0].first, part[0].second)); if (most > K) { if (lastx == x) cuts += H * W; cuts++; lastx = x; } } // print(part, cuts); best = min(best, cuts); } print(best); }
replace
598
599
598
599
0
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, N) for (int i = 0; i < N; i++) #define rep1(i, N) for (int i = 1; i <= N; i++) const int intinf = numeric_limits<int>::max(); using ll = long long int; using pii = pair<int, int>; using pll = pair<ll, ll>; #define MOD 1000000007 void solve() { int H, W, K; cin >> H >> W >> K; vector<string> S(H); rep(i, H) cin >> S[i]; vector<vector<int>> s(H, vector<int>(W)); rep(i, H) rep(j, W) s[i][j] = S[i][j] - '0'; int ans = (H - 1) * (W - 1); rep(a, 1 << (H - 1)) { int ncut = __builtin_popcount(a); int ans_a = ncut; int h = ncut + 1; vector<vector<int>> bigs(h, vector<int>(W, 0)); int ii = 0; rep(i, H) { rep(j, W + 1) bigs[ii][j] += s[i][j]; if (a & (1 << i)) ii++; } bool is_impossible = false; vector<int> bigk(h, 0); rep(j, W) { bool is_ok = true; rep(ii, h) { if (bigs[ii][j] > K) { is_impossible = true; break; } if (bigk[ii] + bigs[ii][j] > K) { is_ok = false; break; } } if (is_impossible) continue; if (!is_ok) { ans_a++; bigk.clear(); bigk.resize(h, 0); } rep(ii, h) bigk[ii] += bigs[ii][j]; } if (is_impossible) continue; ans = min(ans, ans_a); } cout << ans << endl; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, N) for (int i = 0; i < N; i++) #define rep1(i, N) for (int i = 1; i <= N; i++) const int intinf = numeric_limits<int>::max(); using ll = long long int; using pii = pair<int, int>; using pll = pair<ll, ll>; #define MOD 1000000007 void solve() { int H, W, K; cin >> H >> W >> K; vector<string> S(H); rep(i, H) cin >> S[i]; vector<vector<int>> s(H, vector<int>(W)); rep(i, H) rep(j, W) s[i][j] = S[i][j] - '0'; int ans = (H - 1) * (W - 1); rep(a, 1 << (H - 1)) { int ncut = __builtin_popcount(a); int ans_a = ncut; int h = ncut + 1; vector<vector<int>> bigs(h, vector<int>(W, 0)); int ii = 0; rep(i, H) { rep(j, W) bigs[ii][j] += s[i][j]; if (a & (1 << i)) ii++; } bool is_impossible = false; vector<int> bigk(h, 0); rep(j, W) { bool is_ok = true; rep(ii, h) { if (bigs[ii][j] > K) { is_impossible = true; break; } if (bigk[ii] + bigs[ii][j] > K) { is_ok = false; break; } } if (is_impossible) continue; if (!is_ok) { ans_a++; bigk.clear(); bigk.resize(h, 0); } rep(ii, h) bigk[ii] += bigs[ii][j]; } if (is_impossible) continue; ans = min(ans, ans_a); } cout << ans << endl; } int main() { solve(); return 0; }
replace
31
32
31
32
0
p02733
C++
Time Limit Exceeded
// >>> TEMPLATES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using i32 = int32_t; using i64 = int64_t; #define int ll #define double ld #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repR(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rep1R(i, n) for (int i = (int)(n); i >= 1; i--) #define loop(i, a, B) for (int i = a; i B; i++) #define loopR(i, a, B) for (int i = a; i B; i--) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second auto constexpr INF32 = numeric_limits<int32_t>::max() / 2 - 1; auto constexpr INF64 = numeric_limits<int64_t>::max() / 2 - 1; auto constexpr INF = numeric_limits<int>::max() / 2 - 1; #ifdef LOCAL #include "debug.hpp" #define dump(...) \ cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", \ dump_impl(#__VA_ARGS__, __VA_ARGS__) #define say(x) \ cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl #define debug if (1) #else #define dump(...) (void)(0) #define say(x) (void)(0) #define debug if (0) #endif template <class T> using pque_max = priority_queue<T>; template <class T> using pque_min = priority_queue<T, vector<T>, greater<T>>; template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> ostream &operator<<(ostream &os, T const &v) { bool f = true; for (auto const &x : v) os << (f ? "" : " ") << x, f = false; return os; } template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> istream &operator>>(istream &is, T &v) { for (auto &x : v) is >> x; return is; } template <class T, class S> istream &operator>>(istream &is, pair<T, S> &p) { return is >> p.first >> p.second; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup; template <class F> struct FixPoint : private F { constexpr FixPoint(F &&f) : F(forward<F>(f)) {} template <class... T> constexpr auto operator()(T &&...x) const { return F::operator()(*this, forward<T>(x)...); } }; struct MakeFixPoint { template <class F> constexpr auto operator|(F &&f) const { return FixPoint<F>(forward<F>(f)); } }; #define MFP MakeFixPoint() | #define def(name, ...) auto name = MFP[&](auto &&name, __VA_ARGS__) template <class T, size_t d> struct vec_impl { using type = vector<typename vec_impl<T, d - 1>::type>; template <class... U> static type make_v(size_t n, U &&...x) { return type(n, vec_impl<T, d - 1>::make_v(forward<U>(x)...)); } }; template <class T> struct vec_impl<T, 0> { using type = T; static type make_v(T const &x = {}) { return x; } }; template <class T, size_t d = 1> using vec = typename vec_impl<T, d>::type; template <class T, size_t d = 1, class... Args> auto make_v(Args &&...args) { return vec_impl<T, d>::make_v(forward<Args>(args)...); } template <class T> void quit(T const &x) { cout << x << endl; exit(0); } template <class T, class U> constexpr bool chmin(T &x, U const &y) { if (x > y) { x = y; return true; } return false; } template <class T, class U> constexpr bool chmax(T &x, U const &y) { if (x < y) { x = y; return true; } return false; } template <class It> constexpr auto sumof(It b, It e) { return accumulate(b, e, typename iterator_traits<It>::value_type{}); } template <class T> int sz(T const &x) { return x.size(); } template <class C, class T> int lbd(C const &v, T const &x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class T> int ubd(C const &v, T const &x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class F> int ppt(C const &v, F f) { return partition_point(v.begin(), v.end(), f) - v.begin(); } // <<< int32_t main() { int h, w, k; cin >> h >> w >> k; vector<string> s(h); cin >> s; auto v = make_v<int, 2>(h + 1, w + 1); rep(i, h) rep(j, w) v[i + 1][j + 1] = s[i][j] - '0'; rep(i, h + 1) rep(j, w) v[i][j + 1] += v[i][j]; rep(i, h) rep(j, w + 1) v[i + 1][j] += v[i][j]; dump(as_mat(v)); auto count_1 = [&](int i0, int i1, int j0, int j1) { return v[i1][j1] - v[i1][j0] - v[i0][j1] + v[i0][j0]; }; int mi = INF; rep(bit, 1LL << (h - 1)) { int cnt = 0; vector<int> idx; idx.eb(0); rep(i, h - 1) if (bit >> i & 1) cnt++, idx.eb(i + 1); idx.eb(h); dump(bit, idx); auto ok = [&](int j0, int j1) { rep(i, idx.size() - 1) { int i0 = idx[i]; int i1 = idx[i + 1]; if (count_1(i0, i1, j0, j1) > k) return false; } return true; }; int j = 0, pj = 0; while (pj < w) { while (j < w && ok(pj, j + 1)) j++; dump(bit, pj, j); cnt++; pj = j; } cnt--; dump(bit, cnt); chmin(mi, cnt); } cout << mi << endl; }
// >>> TEMPLATES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using i32 = int32_t; using i64 = int64_t; #define int ll #define double ld #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repR(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rep1R(i, n) for (int i = (int)(n); i >= 1; i--) #define loop(i, a, B) for (int i = a; i B; i++) #define loopR(i, a, B) for (int i = a; i B; i--) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second auto constexpr INF32 = numeric_limits<int32_t>::max() / 2 - 1; auto constexpr INF64 = numeric_limits<int64_t>::max() / 2 - 1; auto constexpr INF = numeric_limits<int>::max() / 2 - 1; #ifdef LOCAL #include "debug.hpp" #define dump(...) \ cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", \ dump_impl(#__VA_ARGS__, __VA_ARGS__) #define say(x) \ cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl #define debug if (1) #else #define dump(...) (void)(0) #define say(x) (void)(0) #define debug if (0) #endif template <class T> using pque_max = priority_queue<T>; template <class T> using pque_min = priority_queue<T, vector<T>, greater<T>>; template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> ostream &operator<<(ostream &os, T const &v) { bool f = true; for (auto const &x : v) os << (f ? "" : " ") << x, f = false; return os; } template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> istream &operator>>(istream &is, T &v) { for (auto &x : v) is >> x; return is; } template <class T, class S> istream &operator>>(istream &is, pair<T, S> &p) { return is >> p.first >> p.second; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup; template <class F> struct FixPoint : private F { constexpr FixPoint(F &&f) : F(forward<F>(f)) {} template <class... T> constexpr auto operator()(T &&...x) const { return F::operator()(*this, forward<T>(x)...); } }; struct MakeFixPoint { template <class F> constexpr auto operator|(F &&f) const { return FixPoint<F>(forward<F>(f)); } }; #define MFP MakeFixPoint() | #define def(name, ...) auto name = MFP[&](auto &&name, __VA_ARGS__) template <class T, size_t d> struct vec_impl { using type = vector<typename vec_impl<T, d - 1>::type>; template <class... U> static type make_v(size_t n, U &&...x) { return type(n, vec_impl<T, d - 1>::make_v(forward<U>(x)...)); } }; template <class T> struct vec_impl<T, 0> { using type = T; static type make_v(T const &x = {}) { return x; } }; template <class T, size_t d = 1> using vec = typename vec_impl<T, d>::type; template <class T, size_t d = 1, class... Args> auto make_v(Args &&...args) { return vec_impl<T, d>::make_v(forward<Args>(args)...); } template <class T> void quit(T const &x) { cout << x << endl; exit(0); } template <class T, class U> constexpr bool chmin(T &x, U const &y) { if (x > y) { x = y; return true; } return false; } template <class T, class U> constexpr bool chmax(T &x, U const &y) { if (x < y) { x = y; return true; } return false; } template <class It> constexpr auto sumof(It b, It e) { return accumulate(b, e, typename iterator_traits<It>::value_type{}); } template <class T> int sz(T const &x) { return x.size(); } template <class C, class T> int lbd(C const &v, T const &x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class T> int ubd(C const &v, T const &x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class F> int ppt(C const &v, F f) { return partition_point(v.begin(), v.end(), f) - v.begin(); } // <<< int32_t main() { int h, w, k; cin >> h >> w >> k; vector<string> s(h); cin >> s; auto v = make_v<int, 2>(h + 1, w + 1); rep(i, h) rep(j, w) v[i + 1][j + 1] = s[i][j] - '0'; rep(i, h + 1) rep(j, w) v[i][j + 1] += v[i][j]; rep(i, h) rep(j, w + 1) v[i + 1][j] += v[i][j]; dump(as_mat(v)); auto count_1 = [&](int i0, int i1, int j0, int j1) { return v[i1][j1] - v[i1][j0] - v[i0][j1] + v[i0][j0]; }; int mi = INF; rep(bit, 1LL << (h - 1)) { int cnt = 0; vector<int> idx; idx.eb(0); rep(i, h - 1) if (bit >> i & 1) cnt++, idx.eb(i + 1); idx.eb(h); dump(bit, idx); auto ok = [&](int j0, int j1) { rep(i, idx.size() - 1) { int i0 = idx[i]; int i1 = idx[i + 1]; if (count_1(i0, i1, j0, j1) > k) return false; } return true; }; int j = 0, pj = 0; while (pj < w) { while (j < w && ok(pj, j + 1)) j++; dump(bit, pj, j); if (pj == j) { cnt = INF; break; } cnt++; pj = j; } cnt--; dump(bit, cnt); chmin(mi, cnt); } cout << mi << endl; }
insert
164
164
164
168
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define MAX 500005 #define INF 0x3f3f3f3f using namespace std; template <typename T> void read(T &n) { n = 0; T f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1, c = getchar(); while (isdigit(c)) n = n * 10 + c - '0', c = getchar(); n *= f; } template <typename T> void write(T n) { if (n < 0) putchar('-'), n = -n; if (n > 9) write(n / 10); putchar(n % 10 + '0'); } int n, m, k; int s[15][1005], a[15][1005], t[15]; vector<int> v; int solve() { int res = 0; for (int i = 1; i <= m; i++) { int flag = 0; for (int j = 1; j < v.size(); j++) { if (s[i][v[j]] - s[i][v[j - 1]] > k) return INF; t[j] += s[i][v[j]] - s[i][v[j - 1]]; if (t[j] > k) flag = 1; } if (flag) { res++; for (int j = 1; j < v.size(); j++) { t[j] = s[i][v[j]] - s[i][v[j - 1]]; } } } return res; } int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%1d", &a[i][j]); } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { s[i][j] = s[i][j - 1] + a[j][i]; } } int ans = INF; for (int i = 0; i < (1 << n - 1); i++) { v.clear(); v.push_back(0); int cnt = 0; for (int j = 0; j < n; j++) { if ((i >> j) & 1) v.push_back(j + 1), cnt++; } v.push_back(n); for (int i = 1; i < v.size(); i++) t[i] = 0; cnt += solve(); ans = min(ans, cnt); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define MAX 500005 #define INF 0x3f3f3f3f using namespace std; template <typename T> void read(T &n) { n = 0; T f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1, c = getchar(); while (isdigit(c)) n = n * 10 + c - '0', c = getchar(); n *= f; } template <typename T> void write(T n) { if (n < 0) putchar('-'), n = -n; if (n > 9) write(n / 10); putchar(n % 10 + '0'); } int n, m, k; int s[1005][15], a[15][1005], t[15]; vector<int> v; int solve() { int res = 0; for (int i = 1; i <= m; i++) { int flag = 0; for (int j = 1; j < v.size(); j++) { if (s[i][v[j]] - s[i][v[j - 1]] > k) return INF; t[j] += s[i][v[j]] - s[i][v[j - 1]]; if (t[j] > k) flag = 1; } if (flag) { res++; for (int j = 1; j < v.size(); j++) { t[j] = s[i][v[j]] - s[i][v[j - 1]]; } } } return res; } int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%1d", &a[i][j]); } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { s[i][j] = s[i][j - 1] + a[j][i]; } } int ans = INF; for (int i = 0; i < (1 << n - 1); i++) { v.clear(); v.push_back(0); int cnt = 0; for (int j = 0; j < n; j++) { if ((i >> j) & 1) v.push_back(j + 1), cnt++; } v.push_back(n); for (int i = 1; i < v.size(); i++) t[i] = 0; cnt += solve(); ans = min(ans, cnt); } cout << ans << endl; return 0; }
replace
27
28
27
28
0
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define int long long typedef int ll; typedef long double ld; const ll N = 200005; char en = '\n'; ll inf = 1e9; ll mod = 1e9 + 7; ll power(ll x, ll n, ll mod) { ll res = 1; x %= mod; while (n) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n >>= 1; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll h, w, k; cin >> h >> w >> k; char tmp[h + 5][w + 5]; ll arr[h + 5][w + 5]; memset(arr, 0, sizeof(arr)); for (ll i = 1; i <= h; i++) { cin >> (tmp[i] + 1); for (ll j = 1; j <= w; j++) arr[i][j] = tmp[i][j] - '0'; } ll pre[h + 5][w + 5]; memset(pre, 0, sizeof(pre)); for (ll i = 1; i <= h; i++) { for (ll j = 1; j <= w; j++) { pre[i][j] = arr[i][j] + pre[i - 1][j]; } } ll ans = inf; for (ll mask = 0; mask < (1 << (h - 1)); mask++) { vector<ll> cuts; for (ll j = 0; j < mask; j++) { if ((mask >> j) & 1) { cuts.push_back(j + 1); } } ll prev1 = 0; vector<vector<ll>> arr2; bool bad = false; for (ll &x : cuts) { vector<ll> tmp(w + 1, 0); for (ll j = 1; j <= w; j++) { tmp[j] = pre[x][j] - pre[prev1][j]; if (tmp[j] > k) bad = true; } prev1 = x; arr2.push_back(tmp); } vector<ll> tmp(w + 1, 0); for (ll j = 1; j <= w; j++) { tmp[j] = pre[h][j] - pre[prev1][j]; if (tmp[j] > k) bad = true; } if (bad) continue; arr2.push_back(tmp); ll res = cuts.size(); ll sum1[10]; memset(sum1, 0, sizeof(sum1)); for (ll j = 1; j <= w; j++) { bool bad = false; for (ll i = 0; i < arr2.size(); i++) { sum1[i] += arr2[i][j]; if (sum1[i] > k) { bad = true; } } if (bad) { res++; j--; memset(sum1, 0, sizeof(sum1)); } } ans = min(ans, res); } cout << ans << en; return 0; }
#include <bits/stdc++.h> using namespace std; // #define int long long typedef int ll; typedef long double ld; const ll N = 200005; char en = '\n'; ll inf = 1e9; ll mod = 1e9 + 7; ll power(ll x, ll n, ll mod) { ll res = 1; x %= mod; while (n) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n >>= 1; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll h, w, k; cin >> h >> w >> k; char tmp[h + 5][w + 5]; ll arr[h + 5][w + 5]; memset(arr, 0, sizeof(arr)); for (ll i = 1; i <= h; i++) { cin >> (tmp[i] + 1); for (ll j = 1; j <= w; j++) arr[i][j] = tmp[i][j] - '0'; } ll pre[h + 5][w + 5]; memset(pre, 0, sizeof(pre)); for (ll i = 1; i <= h; i++) { for (ll j = 1; j <= w; j++) { pre[i][j] = arr[i][j] + pre[i - 1][j]; } } ll ans = inf; for (ll mask = 0; mask < (1 << (h - 1)); mask++) { vector<ll> cuts; for (ll j = 0; j < (h - 1); j++) { if ((mask >> j) & 1) { cuts.push_back(j + 1); } } ll prev1 = 0; vector<vector<ll>> arr2; bool bad = false; for (ll &x : cuts) { vector<ll> tmp(w + 1, 0); for (ll j = 1; j <= w; j++) { tmp[j] = pre[x][j] - pre[prev1][j]; if (tmp[j] > k) bad = true; } prev1 = x; arr2.push_back(tmp); } vector<ll> tmp(w + 1, 0); for (ll j = 1; j <= w; j++) { tmp[j] = pre[h][j] - pre[prev1][j]; if (tmp[j] > k) bad = true; } if (bad) continue; arr2.push_back(tmp); ll res = cuts.size(); ll sum1[10]; memset(sum1, 0, sizeof(sum1)); for (ll j = 1; j <= w; j++) { bool bad = false; for (ll i = 0; i < arr2.size(); i++) { sum1[i] += arr2[i][j]; if (sum1[i] > k) { bad = true; } } if (bad) { res++; j--; memset(sum1, 0, sizeof(sum1)); } } ans = min(ans, res); } cout << ans << en; return 0; }
replace
51
52
51
52
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w, k; cin >> h >> w >> k; vector<string> s(h); for (auto &&e : s) { cin >> e; } int res = 1e9; for (int bt = 0; bt < 1 << (h - 1); ++bt) { vector<int> p{0}; for (int i = 1; i < h; ++i) { if (bt >> (i - 1) & 1) { p.push_back(i); } } p.push_back(h); int n = p.size() - 1; int cur = n - 1; int l = 0, r = 0; while (l < w) { vector<int> c(n); while (r < w) { for (int t = 0; t < n; ++t) { for (int i = p[t]; i < p[t + 1]; ++i) { c[t] += s[i][r] == '1'; } } if (*max_element(begin(c), end(c)) > k) { break; } ++r; } l = r; cur += l < w; } res = min(res, cur); } cout << res << '\n'; }
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w, k; cin >> h >> w >> k; vector<string> s(h); for (auto &&e : s) { cin >> e; } int res = 1e9; for (int bt = 0; bt < 1 << (h - 1); ++bt) { vector<int> p{0}; for (int i = 1; i < h; ++i) { if (bt >> (i - 1) & 1) { p.push_back(i); } } p.push_back(h); int n = p.size() - 1; int cur = n - 1; int l = 0, r = 0; while (l < w) { vector<int> c(n); while (r < w) { for (int t = 0; t < n; ++t) { for (int i = p[t]; i < p[t + 1]; ++i) { c[t] += s[i][r] == '1'; } } if (*max_element(begin(c), end(c)) > k) { break; } ++r; } if (l == r) { cur = 1e9; break; } l = r; cur += l < w; } res = min(res, cur); } cout << res << '\n'; }
insert
37
37
37
41
TLE
p02733
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> #define rep(i, n) for (i = 0; i < (n); i++) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define MOD 1000000007 #define PI 3.14159265358979323846 #define INF 1 << 30 using namespace std; typedef long long ll; typedef pair<int, int> pp; int Cut(int n, int k, vector<string> &s) { int i, j, c = 0, ans = 0; vector<vector<int>> d(s.size(), vector<int>(s[0].size(), 0)); vector<int> a(s.size()); rep(i, s.size()) { a[i] = ans; if (n & (1 << i)) ans++; } rep(j, s[0].size()) { rep(i, s.size()) { if (s[i][j] == '1') { if (d[a[i]][c] == k) { c++; j--; break; } d[a[i]][c]++; } } } return ans + c; } int main(void) { int num, i, h, w, k, ans = INF; cin >> h >> w >> k; vector<string> s(h); rep(i, h) cin >> s[i]; rep(i, 1 << (h - 1)) { chmin(ans, Cut(i, k, s)); } cout << ans << "\n"; return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> #define rep(i, n) for (i = 0; i < (n); i++) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define MOD 1000000007 #define PI 3.14159265358979323846 #define INF 1 << 30 using namespace std; typedef long long ll; typedef pair<int, int> pp; int Cut(int n, int k, vector<string> &s) { int i, j, c = 0, ans = 0; vector<vector<int>> d(s.size(), vector<int>(s[0].size(), 0)); vector<int> a(s.size()); rep(i, s.size()) { a[i] = ans; if (n & (1 << i)) ans++; } rep(j, s[0].size()) { rep(i, s.size()) { if (s[i][j] == '1') { if (c >= s[0].size()) return INF; if (d[a[i]][c] == k) { c++; j--; break; } d[a[i]][c]++; } } } return ans + c; } int main(void) { int num, i, h, w, k, ans = INF; cin >> h >> w >> k; vector<string> s(h); rep(i, h) cin >> s[i]; rep(i, 1 << (h - 1)) { chmin(ans, Cut(i, k, s)); } cout << ans << "\n"; return 0; }
insert
27
27
27
29
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define whole(x) (x).begin(), (x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using ll = long long; using P = pair<int, int>; const int mod = 1000000007; const int INF = 1001001001; int sum[11][1005]; int main() { int h, w, k; cin >> h >> w >> k; vector<string> s(h); rep(i, h) { cin >> s[i]; } rep(i, h) rep(j, w) { sum[i][j + 1] = sum[i][j] + (s[i][j] - '0'); } int ans = INF; rep(i, 1 << (h - 1)) { int res = __builtin_popcount(i); int st = 0; int temp = 0; rep(j, w) rep(l, h) { temp += (sum[l][j + 1] - sum[l][st]); if (i >> l & 1 || l == h - 1) { if (temp > k) { st = j; res++; l = 0; } temp = 0; } } ans = min(ans, res); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define whole(x) (x).begin(), (x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using ll = long long; using P = pair<int, int>; const int mod = 1000000007; const int INF = 1001001001; int sum[11][1005]; int main() { int h, w, k; cin >> h >> w >> k; vector<string> s(h); rep(i, h) { cin >> s[i]; } rep(i, h) rep(j, w) { sum[i][j + 1] = sum[i][j] + (s[i][j] - '0'); } int ans = INF; rep(i, 1 << (h - 1)) { int res = __builtin_popcount(i); int st = 0; int temp = 0; rep(j, w) rep(l, h) { temp += (sum[l][j + 1] - sum[l][st]); if (i >> l & 1 || l == h - 1) { if (temp > k) { if (st == j) { res += 10000; break; } st = j; res++; l = 0; } temp = 0; } } ans = min(ans, res); } cout << ans << endl; return 0; }
insert
28
28
28
32
TLE
p02733
C++
Runtime Error
/* Author- Aasav Badera Time- Tue Mar 24 21:33:15 2020 */ #include <bits/stdc++.h> #define ll long long #define pb push_back #define mii map<ll, ll> #define dbg(x) \ { cerr << #x << ": " << x << endl; } #define dbg2(x, y) \ { cerr << #x << ": " << x << " , " << #y << ": " << y << endl; } #define rep(i, a, b) for (ll i = a; i < b; i++) #define pii pair<ll, ll> #define ppb pop_back #define vi vector<ll> #define vii vector<pair<ll, ll>> #define si set<ll> #define all(a) (a).begin(), (a).end() #define sz(x) (ll) x.size() #define hell 1000000007 #define mp make_pair #define yes cout << "YES" << endl; #define no cout << "NO" << endl; #define inf 1e18L + 1 #define endl '\n' using namespace std; #define N 300005 ll powe(ll x, ll y) { ll res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ll tests = 1; // cin >> tests; while (tests--) { ll h, w, k; cin >> h >> w >> k; string s[h]; rep(i, 0, h) cin >> s[i]; ll dp[h + 1][w + 1]; rep(i, 0, h + 1) dp[i][0] = 0; rep(i, 0, w + 1) dp[0][i] = 0; rep(i, 1, h + 1) { rep(j, 1, w + 1) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + s[i - 1][j - 1] - '0'; } } // rep(i, 0, h) // { // rep(j, 0 ,w) // { // cout << dp[i][j] << " "; // } // cout << endl; // } ll ans = inf; rep(i, 0, powe(2, h - 1)) { ll ans1 = 0; vi v; ll j = 1, z = 1; v.pb(0); while (j <= 2 * i) { if (j & i) v.pb(z); j *= 2; z++; } v.pb(h); ll prevmove = 0; rep(width, 1, w + 1) { rep(division, 1, sz(v)) { if (dp[v[division]][width] - dp[v[division - 1]][width] - dp[v[division]][width - 1] + dp[v[division - 1]][width - 1] > k) { ans1 = inf; break; } if (dp[v[division]][width] - dp[v[division - 1]][width] - dp[v[division]][prevmove] + dp[v[division - 1]][prevmove] > k) { ans1++; prevmove = width - 1; break; } } } ans1 += sz(v) - 2; ans = min(ans1, ans); } cout << ans << endl; } return 0; }
/* Author- Aasav Badera Time- Tue Mar 24 21:33:15 2020 */ #include <bits/stdc++.h> #define ll long long #define pb push_back #define mii map<ll, ll> #define dbg(x) \ { cerr << #x << ": " << x << endl; } #define dbg2(x, y) \ { cerr << #x << ": " << x << " , " << #y << ": " << y << endl; } #define rep(i, a, b) for (ll i = a; i < b; i++) #define pii pair<ll, ll> #define ppb pop_back #define vi vector<ll> #define vii vector<pair<ll, ll>> #define si set<ll> #define all(a) (a).begin(), (a).end() #define sz(x) (ll) x.size() #define hell 1000000007 #define mp make_pair #define yes cout << "YES" << endl; #define no cout << "NO" << endl; #define inf 1e18L + 1 #define endl '\n' using namespace std; #define N 300005 ll powe(ll x, ll y) { ll res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("input.txt", "r", stdin); // // for writing output to output.txt // freopen("output.txt", "w", stdout); // #endif ll tests = 1; // cin >> tests; while (tests--) { ll h, w, k; cin >> h >> w >> k; string s[h]; rep(i, 0, h) cin >> s[i]; ll dp[h + 1][w + 1]; rep(i, 0, h + 1) dp[i][0] = 0; rep(i, 0, w + 1) dp[0][i] = 0; rep(i, 1, h + 1) { rep(j, 1, w + 1) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + s[i - 1][j - 1] - '0'; } } // rep(i, 0, h) // { // rep(j, 0 ,w) // { // cout << dp[i][j] << " "; // } // cout << endl; // } ll ans = inf; rep(i, 0, powe(2, h - 1)) { ll ans1 = 0; vi v; ll j = 1, z = 1; v.pb(0); while (j <= 2 * i) { if (j & i) v.pb(z); j *= 2; z++; } v.pb(h); ll prevmove = 0; rep(width, 1, w + 1) { rep(division, 1, sz(v)) { if (dp[v[division]][width] - dp[v[division - 1]][width] - dp[v[division]][width - 1] + dp[v[division - 1]][width - 1] > k) { ans1 = inf; break; } if (dp[v[division]][width] - dp[v[division - 1]][width] - dp[v[division]][prevmove] + dp[v[division - 1]][prevmove] > k) { ans1++; prevmove = width - 1; break; } } } ans1 += sz(v) - 2; ans = min(ans1, ans); } cout << ans << endl; } return 0; }
replace
46
52
46
52
-11
p02733
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #include <vector> #define mkp make_pair #define mkt make_tuple #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; template <class T> void chmin(T &a, const T &b) { if (a > b) a = b; } template <class T> void chmax(T &a, const T &b) { if (a < b) a = b; } int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W, K; cin >> H >> W >> K; vector<string> S(H); rep(i, H) cin >> S[i]; int ans = MOD; for (int bit = 0; bit < (1 << (H - 1)); bit++) { int Z = __builtin_popcount(bit); vector<int> line; line.push_back(0); for (int i = 0; i < H - 1; i++) if (bit & (1 << i)) line.push_back(i + 1); vector<int> trans(Z + 1, 0); int now = 0; for (int i = 0; i < H; i++) { if (now + 1 < line.size() && line[now + 1] == i) now++; trans[i] = now; } vector<int> num(Z + 1, 0); bool ng = false; int res = 0; for (int j = 0; j < W; j++) { vector<int> nxt(Z + 1, 0); for (int i = 0; i < H; i++) if (S[i][j] == '1') nxt[trans[i]]++; for (int i = 0; i < nxt.size(); i++) if (nxt[i] > K) ng = true; if (ng) break; bool over = false; for (int i = 0; i < num.size(); i++) if (num[i] + nxt[i] > K) over = true; if (over) { res++; num = nxt; } else { for (int i = 0; i < num.size(); i++) num[i] += nxt[i]; } } if (ng) continue; chmin(ans, res + Z); } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #include <vector> #define mkp make_pair #define mkt make_tuple #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; template <class T> void chmin(T &a, const T &b) { if (a > b) a = b; } template <class T> void chmax(T &a, const T &b) { if (a < b) a = b; } int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W, K; cin >> H >> W >> K; vector<string> S(H); rep(i, H) cin >> S[i]; int ans = MOD; for (int bit = 0; bit < (1 << (H - 1)); bit++) { int Z = __builtin_popcount(bit); vector<int> line; line.push_back(0); for (int i = 0; i < H - 1; i++) if (bit & (1 << i)) line.push_back(i + 1); vector<int> trans(H, 0); int now = 0; for (int i = 0; i < H; i++) { if (now + 1 < line.size() && line[now + 1] == i) now++; trans[i] = now; } vector<int> num(Z + 1, 0); bool ng = false; int res = 0; for (int j = 0; j < W; j++) { vector<int> nxt(Z + 1, 0); for (int i = 0; i < H; i++) if (S[i][j] == '1') nxt[trans[i]]++; for (int i = 0; i < nxt.size(); i++) if (nxt[i] > K) ng = true; if (ng) break; bool over = false; for (int i = 0; i < num.size(); i++) if (num[i] + nxt[i] > K) over = true; if (over) { res++; num = nxt; } else { for (int i = 0; i < num.size(); i++) num[i] += nxt[i]; } } if (ng) continue; chmin(ans, res + Z); } cout << ans << endl; return 0; }
replace
43
44
43
44
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const static ll INF = 1e9; int DP[11][1001]; int H, W, K; vector<string> SS; int Min = 100000; int dfs(string S) { if (S.size() == H - 1) { int hcnt = 0; vector<int> CH; for (int i = 0; i < S.size(); i++) if (S[i] == '1') { hcnt++; CH.push_back(i); } CH.push_back(H - 1); vector<int> WB(hcnt + 1), WB_(hcnt + 1); int wcnt = 0; // cout << S << " " << hcnt << endl; if (hcnt >= Min) return 0; for (int i = 0; i < W; i++) { int s = 0; for (int idx = 0; idx < hcnt + 1; idx++) { int x = CH[idx]; for (int j = s; j <= x; j++) { if (SS[j][i] == '1') WB[idx]++; } s = x + 1; } for (int idx = 0; idx < hcnt + 1; idx++) { if (WB[idx] > K) { i--; wcnt++; WB = WB_; break; } } } Min = min(Min, wcnt + hcnt); // cout << S << " " << hcnt << " " << wcnt << endl; return 0; } string S_ = S + '0'; dfs(S_); string S__ = S + '1'; dfs(S__); return 0; } int main() { cin >> H >> W >> K; SS.resize(H); for (int i = 0; i < H; i++) cin >> SS[i]; vector<int> CH(H), CW(W); dfs(""); cout << Min << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const static ll INF = 1e9; int DP[11][1001]; int H, W, K; vector<string> SS; int Min = 100000; int dfs(string S) { if (S.size() == H - 1) { int hcnt = 0; vector<int> CH; for (int i = 0; i < S.size(); i++) if (S[i] == '1') { hcnt++; CH.push_back(i); } CH.push_back(H - 1); vector<int> WB(hcnt + 1), WB_(hcnt + 1); int wcnt = 0; // cout << S << " " << hcnt << endl; if (hcnt >= Min) return 0; for (int i = 0; i < W; i++) { int s = 0; for (int idx = 0; idx < hcnt + 1; idx++) { int x = CH[idx]; for (int j = s; j <= x; j++) { if (SS[j][i] == '1') WB[idx]++; } s = x + 1; } for (int idx = 0; idx < hcnt + 1; idx++) { if (WB[idx] > K) { i--; wcnt++; if (hcnt + wcnt >= Min) return 0; WB = WB_; break; } } } Min = min(Min, wcnt + hcnt); // cout << S << " " << hcnt << " " << wcnt << endl; return 0; } string S_ = S + '0'; dfs(S_); string S__ = S + '1'; dfs(S__); return 0; } int main() { cin >> H >> W >> K; SS.resize(H); for (int i = 0; i < H; i++) cin >> SS[i]; vector<int> CH(H), CW(W); dfs(""); cout << Min << endl; }
insert
42
42
42
44
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll maxn = 2e5 + 50; const int mod = 1e9 + 7; int s[15][15]; int mark[15]; int sum(int x1, int y1, int x2, int y2) { return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1]; } int main() { memset(s, 0, sizeof s); int h, w, k, mi = 1000000000; scanf("%d%d%d", &h, &w, &k); for (int i = 1; i <= h; i++) { while (getchar() != '\n') ; for (int j = 1; j <= w; j++) { char x; scanf("%c", &x); s[i][j] = x - '0' + s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1]; } } for (int i = 0; i < 1 << (h - 1); i++) { int cnt = i, y = 1, ans = 0; for (int j = 1; j < h; j++) { mark[j] = cnt % 2; if (mark[j] == 1) ans++; cnt /= 2; } for (int j = 1; j <= w; j++) { int x = 1; for (int c = 1; c <= h; c++) { if (sum(x, y, c, j) > k) goto loop; if (mark[c] == 1 || c == h) { if (sum(x, y, c, j + 1) > k) { y = j + 1; ans++; } x = c + 1; } } } mi = min(mi, ans); loop:; } printf("%d", mi); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll maxn = 2e5 + 50; const int mod = 1e9 + 7; int s[15][1050]; int mark[15]; int sum(int x1, int y1, int x2, int y2) { return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1]; } int main() { memset(s, 0, sizeof s); int h, w, k, mi = 1000000000; scanf("%d%d%d", &h, &w, &k); for (int i = 1; i <= h; i++) { while (getchar() != '\n') ; for (int j = 1; j <= w; j++) { char x; scanf("%c", &x); s[i][j] = x - '0' + s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1]; } } for (int i = 0; i < 1 << (h - 1); i++) { int cnt = i, y = 1, ans = 0; for (int j = 1; j < h; j++) { mark[j] = cnt % 2; if (mark[j] == 1) ans++; cnt /= 2; } for (int j = 1; j <= w; j++) { int x = 1; for (int c = 1; c <= h; c++) { if (sum(x, y, c, j) > k) goto loop; if (mark[c] == 1 || c == h) { if (sum(x, y, c, j + 1) > k) { y = j + 1; ans++; } x = c + 1; } } } mi = min(mi, ans); loop:; } printf("%d", mi); return 0; }
replace
5
6
5
6
0
p02733
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using PL = pair<ll, ll>; // Welcome to my source code! const int INF = 1e9; int main() { int h, w, k; cin >> h >> w >> k; string s[h]; for (int i = 0; i < h; i++) cin >> s[i]; ll ans = INF; for (int bit = 0; bit < (1 << h - 1); bit++) { ll cnt = __builtin_popcount(bit); int nb = bit | (1 << h - 1); vector<int> prev(10, 0); bool div = false; bool over = false; for (int j = 0; j < w; j++) { vector<int> cur(10); if (div) { cnt++; for (int i = 0; i < 10; i++) cur[i] = 0; div = false; } else { for (int i = 0; i < 10; i++) cur[i] = prev[i]; } int idx = 0; for (int i = 0; i < h; i++) { if (s[i][j] == '1') cur[idx]++; if (cur[idx] > k) { if (div) { over = true; break; } div = true; break; } if (nb & (1 << i)) idx++; if (i == h - 1 && div) div = false; } if (over) break; if (div) { j--; continue; } else { for (int i = 0; i < 10; i++) prev[i] = cur[i]; } } if (over) continue; ans = min(ans, cnt); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using PL = pair<ll, ll>; // Welcome to my source code! const int INF = 1e9; int main() { int h, w, k; cin >> h >> w >> k; string s[h]; for (int i = 0; i < h; i++) cin >> s[i]; ll ans = INF; for (int bit = 0; bit < (1 << h - 1); bit++) { ll cnt = __builtin_popcount(bit); int nb = bit | (1 << h - 1); vector<int> prev(10, 0); bool div = false; bool over = false; for (int j = 0; j < w; j++) { vector<int> cur(10); if (div) { cnt++; for (int i = 0; i < 10; i++) cur[i] = 0; } else { for (int i = 0; i < 10; i++) cur[i] = prev[i]; } int idx = 0; for (int i = 0; i < h; i++) { if (s[i][j] == '1') cur[idx]++; if (cur[idx] > k) { if (div) { over = true; break; } div = true; break; } if (nb & (1 << i)) idx++; if (i == h - 1 && div) div = false; } if (over) break; if (div) { j--; continue; } else { for (int i = 0; i < 10; i++) prev[i] = cur[i]; } } if (over) continue; ans = min(ans, cnt); } cout << ans << endl; }
delete
28
29
28
28
TLE
p02733
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(v) v.begin(), v.end() #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define fs first #define sc second void reset(vector<int> &v) { rep(i, v.size()) { v[i] = 0; } } int main(void) { int h, w, k; cin >> h >> w >> k; vector<vector<bool>> s(h, vector<bool>(w, false)); rep(i, h) { string tmp; cin >> tmp; rep(j, w) { if (tmp[j] == '1') s[i][j] = true; } } int ans = h + w; rep(c, 1 << (h - 1)) { bitset<9> b(c); int cut = b.count(); vector<int> blocks(cut + 1, 0); int wc = 0; int hc = 0; int bc = 0; bool cutted = false; while (1) { blocks[bc] += s[hc][wc]; if (blocks[bc] > k) { if (cutted) { cut = h + w; break; } cut++; cutted = true; bc = 0; hc = 0; reset(blocks); continue; } if (b.test(hc)) { bc++; } hc++; if (hc == h) { bc = 0; hc = 0; wc++; cutted = false; if (wc == w) break; } } ans = min(ans, cut); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(v) v.begin(), v.end() #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define fs first #define sc second void reset(vector<int> &v) { rep(i, v.size()) { v[i] = 0; } } int main(void) { int h, w, k; cin >> h >> w >> k; vector<vector<bool>> s(h, vector<bool>(w, false)); rep(i, h) { string tmp; cin >> tmp; rep(j, w) { if (tmp[j] == '1') s[i][j] = true; } } int ans = h + w; rep(c, 1 << (h - 1)) { bitset<9> b(c); int cut = b.count(); vector<int> blocks(cut + 1, 0); int wc = 0; int hc = 0; int bc = 0; bool cutted = false; while (1) { blocks[bc] += s[hc][wc]; if (blocks[bc] > k) { if (cutted) { cut = h + w; break; } cut++; cutted = true; bc = 0; hc = 0; reset(blocks); continue; } if (hc < h - 1) { if (b.test(hc)) { bc++; } } hc++; if (hc == h) { bc = 0; hc = 0; wc++; cutted = false; if (wc == w) break; } } ans = min(ans, cut); } cout << ans << endl; }
replace
47
49
47
51
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep1(i, n) for (ll i = 1; i <= (n); ++i) #define rrep(i, n) for (ll i = (n - 1); i >= 0; --i) #define ALL(obj) (obj).begin(), (obj).end() #define pb push_back #define to_s to_string #define len(v) (ll) v.size() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define print(x) cout << (x) << '\n' #define debug(x) cout << #x << ": " << (x) << '\n' using namespace std; using ll = long long; typedef pair<ll, ll> P; ll MOD = 998244353; ll devc(ll x, ll y) { return 1 + (x - 1) / y; } int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); ll N, S; cin >> N >> S; vector<ll> A(N); rep(i, N) cin >> A[i]; vector<ll> coef(S + 5, 0); ll ans = 0; rep(i, N) { vector<ll> now(S + 5); ++now[0]; ++now[A[i]]; rep(j, S + 5) { now[j] += coef[j]; if (j + A[i] <= S) now[j + A[i]] += coef[j]; now[j] %= MOD; } coef = now; ans += coef[S], ans %= MOD; } print(ans); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep1(i, n) for (ll i = 1; i <= (n); ++i) #define rrep(i, n) for (ll i = (n - 1); i >= 0; --i) #define ALL(obj) (obj).begin(), (obj).end() #define pb push_back #define to_s to_string #define len(v) (ll) v.size() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define print(x) cout << (x) << '\n' #define debug(x) cout << #x << ": " << (x) << '\n' using namespace std; using ll = long long; typedef pair<ll, ll> P; ll MOD = 998244353; ll devc(ll x, ll y) { return 1 + (x - 1) / y; } int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); ll N, S; cin >> N >> S; vector<ll> A(N); rep(i, N) cin >> A[i]; vector<ll> coef(S + 5, 0); ll ans = 0; rep(i, N) { vector<ll> now(S + 5); ++now[0]; if (A[i] <= S) ++now[A[i]]; rep(j, S + 5) { now[j] += coef[j]; if (j + A[i] <= S) now[j + A[i]] += coef[j]; now[j] %= MOD; } coef = now; ans += coef[S], ans %= MOD; } print(ans); return 0; }
replace
34
35
34
36
0
p02734
C++
Time Limit Exceeded
// #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC optimize(3) // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("sse3","sse2","sse") // #pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3") // #pragma GCC target("f16c") // #pragma GCC // optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma // GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error // "-fcse-skip-blocks" #pragma GCC diagnostic error // "-funsafe-loop-optimizations" #pragma GCC diagnostic error "-std=c++14" #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x, "r", stdin) #define fw(x) freopen(x, "w", stdout) #define iout(x) printf("%d\n", x) #define lout(x) printf("%lld\n", x) #define REP(x, l, u) for (ll x = l; x < u; x++) #define RREP(x, l, u) for (ll x = l; x >= u; x--) #define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end()) #define mst(x, a) memset(x, a, sizeof(x)) #define all(a) begin(a), end(a) #define PII pair<int, int> #define PLL pair<ll, ll> #define MP make_pair #define lowbit(x) ((x) & (-(x))) #define lson (ind << 1) #define rson (ind << 1 | 1) #define se second #define fi first #define sz(x) ((int)x.size()) #define EX0 exit(0); typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace __gnu_pbds; // required using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef vector<ll> VLL; typedef vector<int> VI; const int block_size = 320; typedef complex<ll> point; const ll mod = 998244353; const ll inf = 1e9 + 7; const ld eps = 1e-9; const db PI = atan(1) * 4; template <typename T> inline int sign(const T &a) { if (a < 0) return -1; if (a > 0) return 1; return 0; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) \ {} #endif template <typename T, typename S> inline bool upmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> inline bool upmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline void in(T &x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } ull twop(ll x) { return 1ULL << x; } ll MOD(ll a, ll m) { a %= m; if (a < 0) a += m; return a; } ll inverse(ll a, ll m) { a = MOD(a, m); if (a <= 1) return a; return MOD((1 - inverse(m, a) * m) / a, m); } template <typename A, typename B> inline void in(A &x, B &y) { in(x); in(y); } template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) { in(x); in(y); in(z); } template <typename A, typename B, typename C, typename D> inline void in(A &x, B &y, C &z, D &d) { in(x); in(y); in(z); in(d); } template <typename T> T sqr(T x) { return x * x; } ll gcd(ll a, ll b) { while (b != 0) { a %= b; swap(a, b); } return abs(a); } ll fast(ll a, ll b, ll mod) { if (b < 0) a = inverse(a, mod), b = -b; ll ans = 1; while (b) { if (b & 1) { b--; ans = ans * a % mod; } else { a = a * a % mod; b /= 2; } } return ans % mod; } namespace SOLVE { ll n, s; ll a[3010]; ll dp[3010]; void main() { in(n, s); REP(i, 1, n + 1) in(a[i]); ll ans = 0; REP(i, 1, n + 1) { if (a[i] == s) { ans += i * (n - i + 1); } else if (a[i] < s) { ans += dp[s - a[i]] * (n - i + 1); RREP(v, s - a[i], 1) { dp[v + a[i]] += dp[v]; } dp[a[i]] += i; } ans %= mod; REP(i, 1, s + 1) dp[i] %= mod; } cout << ans << endl; } } // namespace SOLVE signed main() { #ifndef ONLINE_JUDGE fr("/Users/zhangqingchuan/Desktop/cp/cp/input.txt"); fw("/Users/zhangqingchuan/Desktop/cp/cp/output.txt"); #endif int t = 1; // in(t); for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<":"; SOLVE::main(); } // clock_t st = clock(); // while(clock() - st < 3.0 * CLOCKS_PER_SEC){ // // } return 0; }
// #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("Ofast") // #pragma GCC optimize(3) // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("sse3","sse2","sse") // #pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3") // #pragma GCC target("f16c") // #pragma GCC // optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma // GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error // "-fcse-skip-blocks" #pragma GCC diagnostic error // "-funsafe-loop-optimizations" #pragma GCC diagnostic error "-std=c++14" #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x, "r", stdin) #define fw(x) freopen(x, "w", stdout) #define iout(x) printf("%d\n", x) #define lout(x) printf("%lld\n", x) #define REP(x, l, u) for (ll x = l; x < u; x++) #define RREP(x, l, u) for (ll x = l; x >= u; x--) #define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end()) #define mst(x, a) memset(x, a, sizeof(x)) #define all(a) begin(a), end(a) #define PII pair<int, int> #define PLL pair<ll, ll> #define MP make_pair #define lowbit(x) ((x) & (-(x))) #define lson (ind << 1) #define rson (ind << 1 | 1) #define se second #define fi first #define sz(x) ((int)x.size()) #define EX0 exit(0); typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace __gnu_pbds; // required using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef vector<ll> VLL; typedef vector<int> VI; const int block_size = 320; typedef complex<ll> point; const ll mod = 998244353; const ll inf = 1e9 + 7; const ld eps = 1e-9; const db PI = atan(1) * 4; template <typename T> inline int sign(const T &a) { if (a < 0) return -1; if (a > 0) return 1; return 0; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) \ {} #endif template <typename T, typename S> inline bool upmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> inline bool upmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline void in(T &x) { x = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } ull twop(ll x) { return 1ULL << x; } ll MOD(ll a, ll m) { a %= m; if (a < 0) a += m; return a; } ll inverse(ll a, ll m) { a = MOD(a, m); if (a <= 1) return a; return MOD((1 - inverse(m, a) * m) / a, m); } template <typename A, typename B> inline void in(A &x, B &y) { in(x); in(y); } template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) { in(x); in(y); in(z); } template <typename A, typename B, typename C, typename D> inline void in(A &x, B &y, C &z, D &d) { in(x); in(y); in(z); in(d); } template <typename T> T sqr(T x) { return x * x; } ll gcd(ll a, ll b) { while (b != 0) { a %= b; swap(a, b); } return abs(a); } ll fast(ll a, ll b, ll mod) { if (b < 0) a = inverse(a, mod), b = -b; ll ans = 1; while (b) { if (b & 1) { b--; ans = ans * a % mod; } else { a = a * a % mod; b /= 2; } } return ans % mod; } namespace SOLVE { ll n, s; ll a[3010]; ll dp[3010]; void main() { in(n, s); REP(i, 1, n + 1) in(a[i]); ll ans = 0; REP(i, 1, n + 1) { if (a[i] == s) { ans += i * (n - i + 1); } else if (a[i] < s) { ans += dp[s - a[i]] * (n - i + 1); RREP(v, s - a[i], 1) { dp[v + a[i]] += dp[v]; } dp[a[i]] += i; } ans %= mod; REP(i, 1, s + 1) dp[i] %= mod; } cout << ans << endl; } } // namespace SOLVE signed main() { #ifndef ONLINE_JUDGE #endif int t = 1; // in(t); for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<":"; SOLVE::main(); } // clock_t st = clock(); // while(clock() - st < 3.0 * CLOCKS_PER_SEC){ // // } return 0; }
delete
215
217
215
215
TLE
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; #define rep(i, n) for (int i = 0; i < (n); ++i) #define repi(i, a, b) for (int i = int(a); i < (b); i++) #define repr(i, b, a) for (int i = int(b); i >= (a); i--) #define all(x) x.begin(), x.end() const ll mod = 998244353; const ll INF = 1e9; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool valid(int lx, int ux, int ly, int uy, int x, int y) { return lx <= x && x < ux && ly <= y && y < uy; } ll power(ll x, ll p) { if (p == 0) return 1; ll res = power(x * x % mod, p / 2); if (p % 2 == 1) res = res * x % mod; return res; } int main() { ll n, s; cin >> n >> s; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> dp(s, 0); ll ans = 0; rep(i, n) { repr(j, s, a[i]) { if (j == a[i]) { dp[j] += i + 1; dp[j] %= mod; } else { dp[j] += dp[j - a[i]]; dp[j] %= mod; } } ans += dp[s]; ans %= mod; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; #define rep(i, n) for (int i = 0; i < (n); ++i) #define repi(i, a, b) for (int i = int(a); i < (b); i++) #define repr(i, b, a) for (int i = int(b); i >= (a); i--) #define all(x) x.begin(), x.end() const ll mod = 998244353; const ll INF = 1e9; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool valid(int lx, int ux, int ly, int uy, int x, int y) { return lx <= x && x < ux && ly <= y && y < uy; } ll power(ll x, ll p) { if (p == 0) return 1; ll res = power(x * x % mod, p / 2); if (p % 2 == 1) res = res * x % mod; return res; } int main() { ll n, s; cin >> n >> s; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> dp(3030, 0); ll ans = 0; rep(i, n) { repr(j, s, a[i]) { if (j == a[i]) { dp[j] += i + 1; dp[j] %= mod; } else { dp[j] += dp[j - a[i]]; dp[j] %= mod; } } ans += dp[s]; ans %= mod; } cout << ans << endl; return 0; }
replace
36
37
36
37
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 2010, P = 998244353; int n, s, a[MAXN], dp[MAXN], np[MAXN]; ll ans; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); for (int j = s; j >= 1; j--) { dp[j + a[i]] = (dp[j + a[i]] + dp[j]) % P; } dp[a[i]] = (dp[a[i]] + i) % P; np[i] = dp[s]; ans = (ans + (1ll * ((np[i] - np[i - 1] + P) % P) * (n - i + 1))) % P; } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 6010, P = 998244353; int n, s, a[MAXN], dp[MAXN], np[MAXN]; ll ans; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); for (int j = s; j >= 1; j--) { dp[j + a[i]] = (dp[j + a[i]] + dp[j]) % P; } dp[a[i]] = (dp[a[i]] + i) % P; np[i] = dp[s]; ans = (ans + (1ll * ((np[i] - np[i - 1] + P) % P) * (n - i + 1))) % P; } printf("%lld\n", ans); return 0; }
replace
3
4
3
4
0
p02734
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define init \ ios::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL); #define setpr(x) cout << setprecision((x)) #define PI atan(1) * 4 #define fi first #define se second #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define in insert #define er erase #define mk(a, b) \ { min((a), (b)), max((a), (b)) } #define flush cout.flush() #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define endc "\n" #define MOD 998244353 #define mod(x) (x) %= MOD using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pld; // #define int ll ll gcd(ll a, ll b) { while (true) { if (a > b) swap(a, b); if (a == 0) return b; ll p = b % a, q = a; a = p; b = q; } } ll powerMod(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; } string binary(ll s) { string res = ""; while (s != 0) { res += (char)('0' + s % 2); s /= 2; } reverse(res.begin(), res.end()); return res; } // ========= /\ /| |====/| // | / \ | | / | // | /____\ | | / | // | / \ | | / | // ========= / \ ===== |/====| // code int32_t main() { init; ll n, s, i, j, k, l, p; cin >> n >> s; vector<ll> a(n); vector<vector<ll>> dp(n + 1, vector<ll>(s + 1, 0LL)); for (auto &i : a) cin >> i; for (i = p = 0LL; i < n; i++) { dp[i + 1] = dp[i]; mod(dp[i + 1][a[i]] += i + 1LL); for (j = 0LL; j <= s - a[i]; j++) { mod(dp[i + 1][j + a[i]] += dp[i][j]); } mod(p += dp[i + 1][s]); } cout << p << endc; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define init \ ios::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL); #define setpr(x) cout << setprecision((x)) #define PI atan(1) * 4 #define fi first #define se second #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define in insert #define er erase #define mk(a, b) \ { min((a), (b)), max((a), (b)) } #define flush cout.flush() #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define endc "\n" #define MOD 998244353 #define mod(x) (x) %= MOD using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pld; // #define int ll ll gcd(ll a, ll b) { while (true) { if (a > b) swap(a, b); if (a == 0) return b; ll p = b % a, q = a; a = p; b = q; } } ll powerMod(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; } string binary(ll s) { string res = ""; while (s != 0) { res += (char)('0' + s % 2); s /= 2; } reverse(res.begin(), res.end()); return res; } // ========= /\ /| |====/| // | / \ | | / | // | /____\ | | / | // | / \ | | / | // ========= / \ ===== |/====| // code int32_t main() { init; ll n, s, i, j, k, l, p; cin >> n >> s; vector<ll> a(n); vector<vector<ll>> dp(n + 1, vector<ll>(s + 1, 0LL)); for (auto &i : a) cin >> i; for (i = p = 0LL; i < n; i++) { dp[i + 1] = dp[i]; if (a[i] <= s) mod(dp[i + 1][a[i]] += i + 1LL); for (j = 0LL; j <= s - a[i]; j++) { mod(dp[i + 1][j + a[i]] += dp[i][j]); } mod(p += dp[i + 1][s]); } cout << p << endc; return 0; }
replace
113
114
113
115
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I) /* https://maspypy.com/atcoder-%E5%8F%82%E5%8A%A0%E6%84%9F%E6%83%B3-2019-03-22abc-159 ans = sum_(i<=j){ f(i,j) } = f(1,1) +f(1,2) +f(1,3) +... +f(2,2) +f(2,3) +... +f(3,3) +... 形式的べき級数で考えると f(i,j-1) {i<j} について *(x^A[j] + 1) をやることで f(i,j) の分を足す (x^A[j] + 1)を足すことで f(j,j) を足す */ int main() { ll ans = 0; ll mod = 998244353; ll N, S; cin >> N >> S; vector<ll> coefficient(S + 1, 0); FOR(i, 0, N) { ll a; cin >> a; vector<ll> t = coefficient; FOR(j, a, S + 1) { (t[j] += coefficient[j - a]) %= mod; } t[a]++; t[0]++; coefficient = t; (ans += coefficient[S]) %= mod; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I) /* https://maspypy.com/atcoder-%E5%8F%82%E5%8A%A0%E6%84%9F%E6%83%B3-2019-03-22abc-159 ans = sum_(i<=j){ f(i,j) } = f(1,1) +f(1,2) +f(1,3) +... +f(2,2) +f(2,3) +... +f(3,3) +... 形式的べき級数で考えると f(i,j-1) {i<j} について *(x^A[j] + 1) をやることで f(i,j) の分を足す (x^A[j] + 1)を足すことで f(j,j) を足す */ int main() { ll ans = 0; ll mod = 998244353; ll N, S; cin >> N >> S; vector<ll> coefficient(3003, 0); FOR(i, 0, N) { ll a; cin >> a; vector<ll> t = coefficient; FOR(j, a, S + 1) { (t[j] += coefficient[j - a]) %= mod; } t[a]++; t[0]++; coefficient = t; (ans += coefficient[S]) %= mod; } cout << ans << endl; }
replace
28
29
28
29
0
p02734
C++
Runtime Error
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math") using namespace std; #include <bits/stdc++.h> #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(X) push_back(X) #define x first #define y second #define all(x) (x).begin(), (x).end() #define INIT \ std::ios::sync_with_stdio(false); \ std::cin.tie(0); typedef long long ll; typedef vector<int> VI; typedef vector<long long> VL; typedef pair<int, int> pii; const ll MOD = 998244353; // ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); // 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; } const int INF = 1e9 + 7; int _; int n, k; ll ans; int main() { INIT cin >> n >> k; VI A(n); vector<vector<ll>> dp(n + 1, vector<ll>(k + 2)); rep(i, 0, n) { cin >> A[i]; } rep(i, 0, n) { dp[i + 1] = dp[i]; (dp[i + 1][A[i]] += i + 1) %= MOD; for (int j = 0; j + A[i] <= k; ++j) { (dp[i + 1][j + A[i]] += dp[i][j]) %= MOD; } (ans += dp[i + 1][k]) %= MOD; } cout << ans; return 0; }
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math") using namespace std; #include <bits/stdc++.h> #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(X) push_back(X) #define x first #define y second #define all(x) (x).begin(), (x).end() #define INIT \ std::ios::sync_with_stdio(false); \ std::cin.tie(0); typedef long long ll; typedef vector<int> VI; typedef vector<long long> VL; typedef pair<int, int> pii; const ll MOD = 998244353; // ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); // 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; } const int INF = 1e9 + 7; int _; int n, k; ll ans; int main() { INIT cin >> n >> k; VI A(n); vector<vector<ll>> dp(n + 1, vector<ll>(3030)); rep(i, 0, n) { cin >> A[i]; } rep(i, 0, n) { dp[i + 1] = dp[i]; (dp[i + 1][A[i]] += i + 1) %= MOD; for (int j = 0; j + A[i] <= k; ++j) { (dp[i + 1][j + A[i]] += dp[i][j]) %= MOD; } (ans += dp[i + 1][k]) %= MOD; } cout << ans; return 0; }
replace
29
30
29
30
0
p02734
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; template <typename T> inline bool chmin(T &x, T a) { if (x >= a) { x = a; return true; } return false; } template <typename T> inline bool chmax(T &x, T a) { if (x <= a) { x = a; return true; } return false; } ///////////// modの計算 #define MOD_IS_PRIME const ll mod = 998244353LL; inline ll mod_adjust(ll a) { if (a >= mod) { return a % mod; } if (a < 0) { a *= -1; a--; if (a >= mod) { a %= mod; } a++; return mod - a; } return a; } inline ll mod_add(ll a, ll b) { return mod_adjust(a % mod + b % mod); } inline ll mod_sub(ll a, ll b) { return mod_adjust(a % mod - b % mod); } inline ll mod_multi(ll a, ll b) { return mod_adjust(mod_adjust(a) * mod_adjust(b)); } int main() { int N, S; cin >> N >> S; vector<int> a(N, 0); for (int i = 0; i < N; i++) { cin >> a[i]; } vector<vector<ll>> v(N); for (int i = 0; i < N; i++) { v[i].resize(S + 1); for (int j = 0; j <= S; j++) { v[i][j] = 0; } } ll ans = 0; v[0][a[0]] = 1; for (int i = 1; i < N; i++) { if (a[i] == S) { ans = mod_add(ans, mod_multi(i + 1, N - i)); } else if (a[i] < S) { ans = mod_add(ans, mod_multi(v[i - 1][S - a[i]], N - i)); } for (int j = 1; j <= S; j++) { v[i][j] = v[i - 1][j]; } for (int j = a[i] + 1; j <= S; j++) { v[i][j] = mod_add(v[i][j], v[i - 1][j - a[i]]); } if (a[i] <= S) { v[i][a[i]] = mod_add(v[i][a[i]], i + 1); } } cout << ans << endl; return 0; }
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; template <typename T> inline bool chmin(T &x, T a) { if (x >= a) { x = a; return true; } return false; } template <typename T> inline bool chmax(T &x, T a) { if (x <= a) { x = a; return true; } return false; } ///////////// modの計算 #define MOD_IS_PRIME const ll mod = 998244353LL; inline ll mod_adjust(ll a) { if (a >= mod) { return a % mod; } if (a < 0) { a *= -1; a--; if (a >= mod) { a %= mod; } a++; return mod - a; } return a; } inline ll mod_add(ll a, ll b) { return mod_adjust(a % mod + b % mod); } inline ll mod_sub(ll a, ll b) { return mod_adjust(a % mod - b % mod); } inline ll mod_multi(ll a, ll b) { return mod_adjust(mod_adjust(a) * mod_adjust(b)); } int main() { int N, S; cin >> N >> S; vector<int> a(N, 0); for (int i = 0; i < N; i++) { cin >> a[i]; } vector<vector<ll>> v(N); for (int i = 0; i < N; i++) { v[i].resize(S + 1); for (int j = 0; j <= S; j++) { v[i][j] = 0; } } ll ans = 0; if (a[0] <= S) { v[0][a[0]] = 1; } for (int i = 1; i < N; i++) { if (a[i] == S) { ans = mod_add(ans, mod_multi(i + 1, N - i)); } else if (a[i] < S) { ans = mod_add(ans, mod_multi(v[i - 1][S - a[i]], N - i)); } for (int j = 1; j <= S; j++) { v[i][j] = v[i - 1][j]; } for (int j = a[i] + 1; j <= S; j++) { v[i][j] = mod_add(v[i][j], v[i - 1][j - a[i]]); } if (a[i] <= S) { v[i][a[i]] = mod_add(v[i][a[i]], i + 1); } } cout << ans << endl; return 0; }
replace
82
83
82
85
0
p02734
C++
Runtime Error
/** * author: zjsdut * created: 2020/03/23 11:14:30 **/ #include <bits/stdc++.h> using namespace std; struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; template <typename A, typename B> bool chkmin(A &a, const B &b) { if (b < a) { a = b; return true; } return false; } template <typename A, typename B> bool chkmax(A &a, const B &b) { if (b > a) { a = b; return true; } return false; } template <typename T> istream &operator>>(istream &stream, vector<T> &vec) { for (auto &x : vec) stream >> x; return stream; } void scan() {} template <class T, class... Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } using ll = long long; using vl = vector<ll>; using vb = vector<bool>; using vi = vector<int>; using pii = pair<int, int>; template <typename T> using vv = vector<vector<T>>; #define rng(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define up(i, a, b) for (int i = int(a); i <= int(b); ++i) #define down(i, b, a) for (int i = int(b); i >= int(a); i--) #define TOKENPASTE(x, y) x##y #define TOKENPASTE2(x, y) TOKENPASTE(x, y) #define rep(n) \ for (int TOKENPASTE2(_iter_, __LINE__) = 0, \ TOKENPASTE2(_num_, __LINE__) = (int)(n); \ TOKENPASTE2(_iter_, __LINE__) < TOKENPASTE2(_num_, __LINE__); \ ++TOKENPASTE2(_iter_, __LINE__)) #define FOR(x, cont) for (const auto &x : cont) #define For(x, cont) for (auto &x : cont) #define all(x) begin(x), end(x) #define pb push_back #define mp make_pair #define eb emplace_back #define ep emplace #define SZ(x) (int)(x).size() #define UNIQ(vec) (vec).erase(unique(all(vec)), end(vec)) #define LB(cont, x) int(lower_bound(all(cont), x) - begin(cont)) #define UB(cont, x) int(upper_bound(all(cont), x) - begin(cont)) template <typename T> ostream &operator<<(ostream &stream, const vector<T> &vec) { if (!vec.empty()) { stream << vec[0]; for (size_t i = 1; i != vec.size(); ++i) stream << ' ' << vec[i]; } return stream; } template <typename T> void print(const vector<T> &t) { cout << t << '\n'; } template <typename T> void print(const vector<vector<T>> &t) { for (const auto &row : t) { print(row); } } template <typename T> void print(const T &t) { cout << t << ' '; } template <typename T, typename... Args> void print(const T &t, const Args &...rest) { print(t); print(rest...); } template <typename T> void println(const T &t) { cout << t << '\n'; } template <typename T, typename... Args> void println(const T &t, const Args &...rest) { print(t); println(rest...); } // tourist's modular-arithmetic class template <typename T> T inverse(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(int) { Modular result(*this); *this += 1; return result; } Modular operator--(int) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> enable_if_t<is_same<typename Modular<U>::Type, int>::value, Modular> & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> enable_if_t<is_same<typename Modular<U>::Type, int64_t>::value, Modular> & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> enable_if_t<!is_integral<typename Modular<U>::Type>::value, Modular> & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(inverse(other.value, mod())); } template <typename U> friend const Modular<U> &abs(const Modular<U> &v) { return v; } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> power(const Modular<T> &a, const U &b) { assert(b >= 0); Modular<T> x = a, res = 1; U p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } template <typename T> bool IsZero(const Modular<T> &number) { return number() == 0; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { return stream << number(); } template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } constexpr int md = 998244353; using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; int main() { #if defined LOCAL && !defined DUIPAI ifstream in("in.txt"); cin.rdbuf(in.rdbuf()); #endif // f[i][j] --> f[i + 1][j], f[i+1][j + a[i + 1]] int n, s; scan(n, s); vi a(n); scan(a); vector<Mint> dp(s + 1); Mint ans = 0; rng(i, 0, n) { down(j, s, a[i] + 1) { dp[j] += dp[j - a[i]]; } dp[a[i]] += i + 1; ans += dp[s]; } println(ans); return 0; }
/** * author: zjsdut * created: 2020/03/23 11:14:30 **/ #include <bits/stdc++.h> using namespace std; struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; template <typename A, typename B> bool chkmin(A &a, const B &b) { if (b < a) { a = b; return true; } return false; } template <typename A, typename B> bool chkmax(A &a, const B &b) { if (b > a) { a = b; return true; } return false; } template <typename T> istream &operator>>(istream &stream, vector<T> &vec) { for (auto &x : vec) stream >> x; return stream; } void scan() {} template <class T, class... Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } using ll = long long; using vl = vector<ll>; using vb = vector<bool>; using vi = vector<int>; using pii = pair<int, int>; template <typename T> using vv = vector<vector<T>>; #define rng(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define up(i, a, b) for (int i = int(a); i <= int(b); ++i) #define down(i, b, a) for (int i = int(b); i >= int(a); i--) #define TOKENPASTE(x, y) x##y #define TOKENPASTE2(x, y) TOKENPASTE(x, y) #define rep(n) \ for (int TOKENPASTE2(_iter_, __LINE__) = 0, \ TOKENPASTE2(_num_, __LINE__) = (int)(n); \ TOKENPASTE2(_iter_, __LINE__) < TOKENPASTE2(_num_, __LINE__); \ ++TOKENPASTE2(_iter_, __LINE__)) #define FOR(x, cont) for (const auto &x : cont) #define For(x, cont) for (auto &x : cont) #define all(x) begin(x), end(x) #define pb push_back #define mp make_pair #define eb emplace_back #define ep emplace #define SZ(x) (int)(x).size() #define UNIQ(vec) (vec).erase(unique(all(vec)), end(vec)) #define LB(cont, x) int(lower_bound(all(cont), x) - begin(cont)) #define UB(cont, x) int(upper_bound(all(cont), x) - begin(cont)) template <typename T> ostream &operator<<(ostream &stream, const vector<T> &vec) { if (!vec.empty()) { stream << vec[0]; for (size_t i = 1; i != vec.size(); ++i) stream << ' ' << vec[i]; } return stream; } template <typename T> void print(const vector<T> &t) { cout << t << '\n'; } template <typename T> void print(const vector<vector<T>> &t) { for (const auto &row : t) { print(row); } } template <typename T> void print(const T &t) { cout << t << ' '; } template <typename T, typename... Args> void print(const T &t, const Args &...rest) { print(t); print(rest...); } template <typename T> void println(const T &t) { cout << t << '\n'; } template <typename T, typename... Args> void println(const T &t, const Args &...rest) { print(t); println(rest...); } // tourist's modular-arithmetic class template <typename T> T inverse(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(int) { Modular result(*this); *this += 1; return result; } Modular operator--(int) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> enable_if_t<is_same<typename Modular<U>::Type, int>::value, Modular> & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> enable_if_t<is_same<typename Modular<U>::Type, int64_t>::value, Modular> & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> enable_if_t<!is_integral<typename Modular<U>::Type>::value, Modular> & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(inverse(other.value, mod())); } template <typename U> friend const Modular<U> &abs(const Modular<U> &v) { return v; } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> power(const Modular<T> &a, const U &b) { assert(b >= 0); Modular<T> x = a, res = 1; U p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } template <typename T> bool IsZero(const Modular<T> &number) { return number() == 0; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { return stream << number(); } template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } constexpr int md = 998244353; using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; int main() { #if defined LOCAL && !defined DUIPAI ifstream in("in.txt"); cin.rdbuf(in.rdbuf()); #endif // f[i][j] --> f[i + 1][j], f[i+1][j + a[i + 1]] int n, s; scan(n, s); vi a(n); scan(a); vector<Mint> dp(s + 1); Mint ans = 0; rng(i, 0, n) { down(j, s, a[i] + 1) { dp[j] += dp[j - a[i]]; } if (a[i] <= s) dp[a[i]] += i + 1; ans += dp[s]; } println(ans); return 0; }
replace
384
385
384
386
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long int; const ll mod = 998244353; vector<int> a(4000); int n, s; ll dp[4000][4000][3]; int ways(int l, int r, int S) { if (S == 0) { return 1; } if (r == l) { return 0; } return ways(l, r - 1, S) + (S >= a[r - 1] ? ways(l, r - 1, S - a[r - 1]) : 0); } int main() { cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; } /* int ans = 0; for(int i = 0;i<n;++i){ for(int j = i;j < n;++j){ ans += ways(i,j+1,s); if(ans >= mod){ ans-=mod; } } } cout << ans <<"\n"; */ auto add = [&](ll &x, ll y) { x += y; x %= y; }; memset(dp, 0LL, sizeof dp); dp[0][0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j <= s; ++j) { add(dp[i + 1][j][0], dp[i][j][0]); add(dp[i + 1][j][1], dp[i][j][0] + dp[i][j][1]); add(dp[i + 1][j][2], dp[i][j][0] + dp[i][j][1] + dp[i][j][2]); if (j + a[i] <= s) { add(dp[i + 1][j + a[i]][1], dp[i][j][0] + dp[i][j][1]); add(dp[i + 1][j + a[i]][2], dp[i][j][0] + dp[i][j][1]); } } } cout << dp[n][s][2] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; const ll mod = 998244353; vector<int> a(4000); int n, s; ll dp[4000][4000][3]; int ways(int l, int r, int S) { if (S == 0) { return 1; } if (r == l) { return 0; } return ways(l, r - 1, S) + (S >= a[r - 1] ? ways(l, r - 1, S - a[r - 1]) : 0); } int main() { cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; } /* int ans = 0; for(int i = 0;i<n;++i){ for(int j = i;j < n;++j){ ans += ways(i,j+1,s); if(ans >= mod){ ans-=mod; } } } cout << ans <<"\n"; */ auto add = [&](ll &x, ll y) { x += y; x %= mod; }; memset(dp, 0LL, sizeof dp); dp[0][0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j <= s; ++j) { add(dp[i + 1][j][0], dp[i][j][0]); add(dp[i + 1][j][1], dp[i][j][0] + dp[i][j][1]); add(dp[i + 1][j][2], dp[i][j][0] + dp[i][j][1] + dp[i][j][2]); if (j + a[i] <= s) { add(dp[i + 1][j + a[i]][1], dp[i][j][0] + dp[i][j][1]); add(dp[i + 1][j + a[i]][2], dp[i][j][0] + dp[i][j][1]); } } } cout << dp[n][s][2] << "\n"; return 0; }
replace
38
39
38
39
-11
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define MOD 998244353 int n, s; int a[3010]; int memo[3010][3010][2]; int dp(int i, int num, int start) { if (num == 0) { return n - i + 1; } if (memo[i][num][start] != -1) return memo[i][num][start]; int ans = 0; if (start == 0) { ans = dp(i + 1, num, 0); ans = (ans + dp(i + 1, num, 1)) % MOD; if (num - a[i] >= 0) ans = (ans + dp(i + 1, num - a[i], 1)) % MOD; } else { ans = dp(i + 1, num, 1); if (num - a[i] >= 0) ans = (ans + dp(i + 1, num - a[i], 1)) % MOD; } return memo[i][num][start] = ans; } signed main() { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; } memset(memo, -1, sizeof(memo)); cout << dp(0, s, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define MOD 998244353 int n, s; int a[3010]; int memo[3010][3010][2]; int dp(int i, int num, int start) { if (num == 0) { return n - i + 1; } if (i == n) return 0; if (memo[i][num][start] != -1) return memo[i][num][start]; int ans = 0; if (start == 0) { ans = dp(i + 1, num, 0); ans = (ans + dp(i + 1, num, 1)) % MOD; if (num - a[i] >= 0) ans = (ans + dp(i + 1, num - a[i], 1)) % MOD; } else { ans = dp(i + 1, num, 1); if (num - a[i] >= 0) ans = (ans + dp(i + 1, num - a[i], 1)) % MOD; } return memo[i][num][start] = ans; } signed main() { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; } memset(memo, -1, sizeof(memo)); cout << dp(0, s, 0) << endl; return 0; }
insert
31
31
31
34
-11
p02734
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) typedef long long ll; const ll MOD = 998244353; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, S; cin >> N >> S; vector<ll> A(N); rep(i, N) { cin >> A[i]; } vector<map<ll, ll>> mp_v(N); ll ans = 0; rep(i, N) { if (i == 0) { mp_v[0][0] = 1; if (A[i] <= S) { ++mp_v[0][A[i]] = 1; } } else { for (auto v : mp_v[i - 1]) { if (v.first == 0) { mp_v[i][v.first] += v.second; if (v.first + A[i] < S) { mp_v[i][v.first + A[i]] += v.second; mp_v[i][v.first + A[i]] += i; } if (v.first + A[i] == S) { ans += (i + 1) * (N - i); ans = ans % MOD; } } else { mp_v[i][v.first] += v.second; if (v.first + A[i] < S) { mp_v[i][v.first + A[i]] += v.second; mp_v[i][v.first + A[i]] %= MOD; } if (v.first + A[i] == S) { ans += (v.second * (N - i)) % MOD; ans %= MOD; } } } } if (mp_v[i].find(S) != mp_v[i].end()) { ans += mp_v[i][S] * (N - i); } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) typedef long long ll; const ll MOD = 998244353; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, S; cin >> N >> S; vector<ll> A(N); rep(i, N) { cin >> A[i]; } vector<unordered_map<ll, ll>> mp_v(N); ll ans = 0; rep(i, N) { if (i == 0) { mp_v[0][0] = 1; if (A[i] <= S) { ++mp_v[0][A[i]] = 1; } } else { for (auto v : mp_v[i - 1]) { if (v.first == 0) { mp_v[i][v.first] += v.second; if (v.first + A[i] < S) { mp_v[i][v.first + A[i]] += v.second; mp_v[i][v.first + A[i]] += i; } if (v.first + A[i] == S) { ans += (i + 1) * (N - i); ans = ans % MOD; } } else { mp_v[i][v.first] += v.second; if (v.first + A[i] < S) { mp_v[i][v.first + A[i]] += v.second; mp_v[i][v.first + A[i]] %= MOD; } if (v.first + A[i] == S) { ans += (v.second * (N - i)) % MOD; ans %= MOD; } } } } if (mp_v[i].find(S) != mp_v[i].end()) { ans += mp_v[i][S] * (N - i); } } cout << ans << "\n"; return 0; }
replace
14
15
14
15
TLE
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define fst first #define snd second #define fore(i, a, b) for (int i = a, ggdem = b; i < ggdem; ++i) #define SZ(x) ((int)x.size()) #define ALL(x) x.begin(), x.end() #define mset(a, v) memset((a), (v), sizeof(a)) #define FIN \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) using namespace std; typedef long long ll; ll n, s; ll a[3030]; ll MOD = 998244353; ll dp[3030][3030]; ll f(ll x, ll y) { ll &r = dp[x][y]; if (r >= 0) return r; if (y == a[x]) { r = x + 1; if (x > 0) r += f(x - 1, y), r %= MOD; return r; } if (x == 0) { return r = 0; } /*if(y==s){ r=(f(x-1,y-a[x])*(n-x))%MOD; return r; }else{*/ r = (f(x - 1, y) + f(x - 1, y - a[x])) % MOD; // cout<<x<<" "<<y<<" "<<r<<"\n"; return r; //} } int main() { FIN; cin >> n >> s; fore(i, 0, n) cin >> a[i]; mset(dp, -1); ll res = 0; fore(i, 0, n) res += f(i, s), res %= MOD; cout << res; return 0; }
#include <bits/stdc++.h> #define pb push_back #define fst first #define snd second #define fore(i, a, b) for (int i = a, ggdem = b; i < ggdem; ++i) #define SZ(x) ((int)x.size()) #define ALL(x) x.begin(), x.end() #define mset(a, v) memset((a), (v), sizeof(a)) #define FIN \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) using namespace std; typedef long long ll; ll n, s; ll a[3030]; ll MOD = 998244353; ll dp[3030][3030]; ll f(ll x, ll y) { if (y < 0) return 0; ll &r = dp[x][y]; if (r >= 0) return r; if (y == a[x]) { r = x + 1; if (x > 0) r += f(x - 1, y), r %= MOD; return r; } if (x == 0) { return r = 0; } /*if(y==s){ r=(f(x-1,y-a[x])*(n-x))%MOD; return r; }else{*/ r = (f(x - 1, y) + f(x - 1, y - a[x])) % MOD; // cout<<x<<" "<<y<<" "<<r<<"\n"; return r; //} } int main() { FIN; cin >> n >> s; fore(i, 0, n) cin >> a[i]; mset(dp, -1); ll res = 0; fore(i, 0, n) res += f(i, s), res %= MOD; cout << res; return 0; }
insert
21
21
21
23
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define int long long int #define ld long double #define pb push_back #define pf push_front #define ft first #define sc second #define all(v) v.begin(), v.end() using namespace std; const int maxn = 3011, mod = 998244353; int dp[maxn][maxn]; signed main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, s; cin >> n >> s; vector<int> mas(n, 0); for (int d = 0; d < n; d++) { cin >> mas[d]; } dp[0][0] = 1; for (int d = 1; d <= n; d++) { dp[d][0] = d + 1; dp[d][mas[d - 1]] = dp[d - 1][mas[d - 1]] + d; // cout << dp[d][0] << " "; for (int s1 = 1; s1 <= s; s1++) { if (s1 != mas[d - 1]) { dp[d][s1] = dp[d - 1][s1]; if (s1 >= mas[d - 1]) { dp[d][s1] += dp[d - 1][s1 - mas[d - 1]]; } } dp[d][s1] %= mod; // cout << dp[d][s1] << " "; } // cout << "\n"; } int ans = 0; int tk = 1; for (int d = n - 1; d >= 0; d--) { ans += tk * dp[d][s - mas[d]]; ans %= mod; tk++; } cout << ans; }
#include <bits/stdc++.h> #define int long long int #define ld long double #define pb push_back #define pf push_front #define ft first #define sc second #define all(v) v.begin(), v.end() using namespace std; const int maxn = 3011, mod = 998244353; int dp[maxn][maxn]; signed main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, s; cin >> n >> s; vector<int> mas(n, 0); for (int d = 0; d < n; d++) { cin >> mas[d]; } dp[0][0] = 1; for (int d = 1; d <= n; d++) { dp[d][0] = d + 1; dp[d][mas[d - 1]] = dp[d - 1][mas[d - 1]] + d; // cout << dp[d][0] << " "; for (int s1 = 1; s1 <= s; s1++) { if (s1 != mas[d - 1]) { dp[d][s1] = dp[d - 1][s1]; if (s1 >= mas[d - 1]) { dp[d][s1] += dp[d - 1][s1 - mas[d - 1]]; } } dp[d][s1] %= mod; // cout << dp[d][s1] << " "; } // cout << "\n"; } int ans = 0; int tk = 1; for (int d = n - 1; d >= 0; d--) { if (s >= mas[d]) { ans += tk * dp[d][s - mas[d]]; ans %= mod; } tk++; } cout << ans; }
replace
39
41
39
43
0
p02734
C++
Time Limit Exceeded
// >>> TEMPLATES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using i32 = int32_t; using i64 = int64_t; #define int ll #define double ld #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repR(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rep1R(i, n) for (int i = (int)(n); i >= 1; i--) #define loop(i, a, B) for (int i = a; i B; i++) #define loopR(i, a, B) for (int i = a; i B; i--) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second auto constexpr INF32 = numeric_limits<int32_t>::max() / 2 - 1; auto constexpr INF64 = numeric_limits<int64_t>::max() / 2 - 1; auto constexpr INF = numeric_limits<int>::max() / 2 - 1; #ifdef LOCAL #include "debug.hpp" #define dump(...) \ cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", \ dump_impl(#__VA_ARGS__, __VA_ARGS__) #define say(x) \ cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl #define debug if (1) #else #define dump(...) (void)(0) #define say(x) (void)(0) #define debug if (0) #endif template <class T> using pque_max = priority_queue<T>; template <class T> using pque_min = priority_queue<T, vector<T>, greater<T>>; template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> ostream &operator<<(ostream &os, T const &v) { bool f = true; for (auto const &x : v) os << (f ? "" : " ") << x, f = false; return os; } template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> istream &operator>>(istream &is, T &v) { for (auto &x : v) is >> x; return is; } template <class T, class S> istream &operator>>(istream &is, pair<T, S> &p) { return is >> p.first >> p.second; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup; template <class F> struct FixPoint : private F { constexpr FixPoint(F &&f) : F(forward<F>(f)) {} template <class... T> constexpr auto operator()(T &&...x) const { return F::operator()(*this, forward<T>(x)...); } }; struct MakeFixPoint { template <class F> constexpr auto operator|(F &&f) const { return FixPoint<F>(forward<F>(f)); } }; #define MFP MakeFixPoint() | #define def(name, ...) auto name = MFP[&](auto &&name, __VA_ARGS__) template <class T, size_t d> struct vec_impl { using type = vector<typename vec_impl<T, d - 1>::type>; template <class... U> static type make_v(size_t n, U &&...x) { return type(n, vec_impl<T, d - 1>::make_v(forward<U>(x)...)); } }; template <class T> struct vec_impl<T, 0> { using type = T; static type make_v(T const &x = {}) { return x; } }; template <class T, size_t d = 1> using vec = typename vec_impl<T, d>::type; template <class T, size_t d = 1, class... Args> auto make_v(Args &&...args) { return vec_impl<T, d>::make_v(forward<Args>(args)...); } template <class T> void quit(T const &x) { cout << x << endl; exit(0); } template <class T, class U> constexpr bool chmin(T &x, U const &y) { if (x > y) { x = y; return true; } return false; } template <class T, class U> constexpr bool chmax(T &x, U const &y) { if (x < y) { x = y; return true; } return false; } template <class It> constexpr auto sumof(It b, It e) { return accumulate(b, e, typename iterator_traits<It>::value_type{}); } template <class T> int sz(T const &x) { return x.size(); } template <class C, class T> int lbd(C const &v, T const &x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class T> int ubd(C const &v, T const &x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class F> int ppt(C const &v, F f) { return partition_point(v.begin(), v.end(), f) - v.begin(); } // <<< // >>> modint template <int32_t md> class modint { static_assert(md > 0, ""); using M = modint; using ll = int64_t; int32_t x; public: static constexpr int32_t mod = md; constexpr modint(ll x = 0) : x((x %= mod) < 0 ? x + mod : x) {} constexpr ll val() const { return x; } constexpr explicit operator ll() const { return x; } constexpr bool operator==(M const &r) const { return x == r.x; } constexpr bool operator!=(M const &r) const { return x != r.x; } constexpr M operator+() const { return *this; } constexpr M operator-() const { return M() - *this; } constexpr M &operator+=(M const &r) { ll t = ll(x) + r.x; if (t >= mod) t -= mod; x = t; return *this; } constexpr M &operator-=(M const &r) { ll t = ll(x) + mod - r.x; if (t >= mod) t -= mod; x = t; return *this; } constexpr M &operator*=(M const &r) { return *this = *this * r; } constexpr M operator*(M const &r) const { M t; t.x = (ll(x) * r.x) % mod; return t; } constexpr M &operator/=(M const &r) { return *this *= r.inv(); } constexpr M operator+(M const &r) const { return M(*this) += r; } constexpr M operator-(M const &r) const { return M(*this) -= r; } constexpr M operator/(M const &r) const { return M(*this) /= r; } friend constexpr M operator+(ll x, M const &y) { return M(x) + y; } friend constexpr M operator-(ll x, M const &y) { return M(x) - y; } friend constexpr M operator*(ll x, M const &y) { return M(x) * y; } friend constexpr M operator/(ll x, M const &y) { return M(x) / y; } constexpr M pow(ll n) const { if (n < 0) return inv().pow(-n); M v = *this, r = 1; for (; n > 0; n >>= 1, v *= v) if (n & 1) r *= v; return r; } constexpr M inv() const { assert(x > 0); ll t = 1, v = x, q = 0, r = 0; while (v != 1) { q = mod / v; r = mod % v; if (r * 2 < v) { t *= -q; t %= mod; v = r; } else { t *= q + 1; t %= mod; v -= r; } } if (t < 0) t += mod; M y; y.x = t; return y; } #ifdef LOCAL friend string to_s(M r) { return to_s(r.val(), mod); } #endif friend ostream &operator<<(ostream &os, M r) { return os << r.val(); } friend istream &operator>>(istream &is, M &r) { int64_t x; is >> x; r = x; return is; } }; // <<< constexpr int64_t MOD = 998244353; // constexpr int64_t MOD = 1e9+7; using mint = modint<(int32_t)MOD>; int32_t main() { int n, s; cin >> n >> s; vector<int> a(n); cin >> a; mint ans = 0; const int MM = 3000 + 10; auto dp = make_v<mint, 2>(n, MM); rep(i, n) { if (a[i] == s) { ans += (i + 1) * (n - i); } if (a[i] < s) { rep(j, i) ans += dp[j][s - a[i]] * (j + 1) * (n - i); } dp[i][a[i]] = 1; rep(j, i) repR(k, MM) if (k + a[i] < MM) { dp[j][k + a[i]] += dp[j][k]; } } cout << ans << endl; }
// >>> TEMPLATES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using i32 = int32_t; using i64 = int64_t; #define int ll #define double ld #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repR(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rep1R(i, n) for (int i = (int)(n); i >= 1; i--) #define loop(i, a, B) for (int i = a; i B; i++) #define loopR(i, a, B) for (int i = a; i B; i--) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second auto constexpr INF32 = numeric_limits<int32_t>::max() / 2 - 1; auto constexpr INF64 = numeric_limits<int64_t>::max() / 2 - 1; auto constexpr INF = numeric_limits<int>::max() / 2 - 1; #ifdef LOCAL #include "debug.hpp" #define dump(...) \ cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", \ dump_impl(#__VA_ARGS__, __VA_ARGS__) #define say(x) \ cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl #define debug if (1) #else #define dump(...) (void)(0) #define say(x) (void)(0) #define debug if (0) #endif template <class T> using pque_max = priority_queue<T>; template <class T> using pque_min = priority_queue<T, vector<T>, greater<T>>; template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> ostream &operator<<(ostream &os, T const &v) { bool f = true; for (auto const &x : v) os << (f ? "" : " ") << x, f = false; return os; } template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> istream &operator>>(istream &is, T &v) { for (auto &x : v) is >> x; return is; } template <class T, class S> istream &operator>>(istream &is, pair<T, S> &p) { return is >> p.first >> p.second; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup; template <class F> struct FixPoint : private F { constexpr FixPoint(F &&f) : F(forward<F>(f)) {} template <class... T> constexpr auto operator()(T &&...x) const { return F::operator()(*this, forward<T>(x)...); } }; struct MakeFixPoint { template <class F> constexpr auto operator|(F &&f) const { return FixPoint<F>(forward<F>(f)); } }; #define MFP MakeFixPoint() | #define def(name, ...) auto name = MFP[&](auto &&name, __VA_ARGS__) template <class T, size_t d> struct vec_impl { using type = vector<typename vec_impl<T, d - 1>::type>; template <class... U> static type make_v(size_t n, U &&...x) { return type(n, vec_impl<T, d - 1>::make_v(forward<U>(x)...)); } }; template <class T> struct vec_impl<T, 0> { using type = T; static type make_v(T const &x = {}) { return x; } }; template <class T, size_t d = 1> using vec = typename vec_impl<T, d>::type; template <class T, size_t d = 1, class... Args> auto make_v(Args &&...args) { return vec_impl<T, d>::make_v(forward<Args>(args)...); } template <class T> void quit(T const &x) { cout << x << endl; exit(0); } template <class T, class U> constexpr bool chmin(T &x, U const &y) { if (x > y) { x = y; return true; } return false; } template <class T, class U> constexpr bool chmax(T &x, U const &y) { if (x < y) { x = y; return true; } return false; } template <class It> constexpr auto sumof(It b, It e) { return accumulate(b, e, typename iterator_traits<It>::value_type{}); } template <class T> int sz(T const &x) { return x.size(); } template <class C, class T> int lbd(C const &v, T const &x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class T> int ubd(C const &v, T const &x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class F> int ppt(C const &v, F f) { return partition_point(v.begin(), v.end(), f) - v.begin(); } // <<< // >>> modint template <int32_t md> class modint { static_assert(md > 0, ""); using M = modint; using ll = int64_t; int32_t x; public: static constexpr int32_t mod = md; constexpr modint(ll x = 0) : x((x %= mod) < 0 ? x + mod : x) {} constexpr ll val() const { return x; } constexpr explicit operator ll() const { return x; } constexpr bool operator==(M const &r) const { return x == r.x; } constexpr bool operator!=(M const &r) const { return x != r.x; } constexpr M operator+() const { return *this; } constexpr M operator-() const { return M() - *this; } constexpr M &operator+=(M const &r) { ll t = ll(x) + r.x; if (t >= mod) t -= mod; x = t; return *this; } constexpr M &operator-=(M const &r) { ll t = ll(x) + mod - r.x; if (t >= mod) t -= mod; x = t; return *this; } constexpr M &operator*=(M const &r) { return *this = *this * r; } constexpr M operator*(M const &r) const { M t; t.x = (ll(x) * r.x) % mod; return t; } constexpr M &operator/=(M const &r) { return *this *= r.inv(); } constexpr M operator+(M const &r) const { return M(*this) += r; } constexpr M operator-(M const &r) const { return M(*this) -= r; } constexpr M operator/(M const &r) const { return M(*this) /= r; } friend constexpr M operator+(ll x, M const &y) { return M(x) + y; } friend constexpr M operator-(ll x, M const &y) { return M(x) - y; } friend constexpr M operator*(ll x, M const &y) { return M(x) * y; } friend constexpr M operator/(ll x, M const &y) { return M(x) / y; } constexpr M pow(ll n) const { if (n < 0) return inv().pow(-n); M v = *this, r = 1; for (; n > 0; n >>= 1, v *= v) if (n & 1) r *= v; return r; } constexpr M inv() const { assert(x > 0); ll t = 1, v = x, q = 0, r = 0; while (v != 1) { q = mod / v; r = mod % v; if (r * 2 < v) { t *= -q; t %= mod; v = r; } else { t *= q + 1; t %= mod; v -= r; } } if (t < 0) t += mod; M y; y.x = t; return y; } #ifdef LOCAL friend string to_s(M r) { return to_s(r.val(), mod); } #endif friend ostream &operator<<(ostream &os, M r) { return os << r.val(); } friend istream &operator>>(istream &is, M &r) { int64_t x; is >> x; r = x; return is; } }; // <<< constexpr int64_t MOD = 998244353; // constexpr int64_t MOD = 1e9+7; using mint = modint<(int32_t)MOD>; int32_t main() { int n, s; cin >> n >> s; vector<int> a(n); cin >> a; def(dfs, int l, int r)->mint { assert(0 <= l); assert(l < r); assert(r <= n); if (l + 1 == r) { if (a[l] == s) { return (l + 1) * (n - l); } else { return 0; } } int m = (l + r) / 2; vector<mint> ldp(s + 1), rdp(s + 1); { vector<mint> dp(s + 1); dp[0] = 1; loopR(i, m - 1, >= l) { rep(j, s + 1) if (j >= a[i]) { ldp[j] += dp[j - a[i]] * (i + 1); } repR(j, s + 1) if (j + a[i] <= s) dp[j + a[i]] += dp[j]; } } { vector<mint> dp(s + 1); dp[0] = 1; loop(i, m, < r) { rep(j, s + 1) if (j >= a[i]) { rdp[j] += dp[j - a[i]] * (n - i); } repR(j, s + 1) if (j + a[i] <= s) dp[j + a[i]] += dp[j]; } } dump(l, r, ldp, rdp); mint sum = 0; rep(i, s + 1) sum += ldp[i] * rdp[s - i]; dump(l, r, sum); mint ret = dfs(l, m) + dfs(m, r) + sum; dump(l, r, ret); return ret; }; cout << dfs(0, n) << endl; }
replace
221
237
221
264
TLE
p02734
C++
Runtime Error
#include <bits/stdc++.h> #include <queue> #include <vector> using namespace std; typedef long long int64; const int P = 998244353; const int MAXN = 3011; int dp[MAXN]; int n, s, ans; inline int add(int x, int y) { x += y; x -= x >= P ? P : 0; return x; } inline int mul(int x, int y) { return 1ll * x * y % P; } inline void incr(int &x, int y) { x = add(x, y); } int main(void) { cin >> n >> s; for (int i = 1; i <= n; ++i) { int v; cin >> v; for (int j = s - v; ~j; --j) incr(dp[j + v], dp[j]); incr(dp[v], i); incr(ans, dp[s]); } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #include <queue> #include <vector> using namespace std; typedef long long int64; const int P = 998244353; const int MAXN = 3011; int dp[MAXN]; int n, s, ans; inline int add(int x, int y) { x += y; x -= x >= P ? P : 0; return x; } inline int mul(int x, int y) { return 1ll * x * y % P; } inline void incr(int &x, int y) { x = add(x, y); } int main(void) { cin >> n >> s; for (int i = 1; i <= n; ++i) { int v; cin >> v; for (int j = s - v; j >= 0; --j) incr(dp[j + v], dp[j]); incr(dp[v], i); incr(ans, dp[s]); } cout << ans << '\n'; return 0; }
replace
25
26
25
26
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #include "testlib.h" #define ff first #define ss second #define all(v) v.begin(), v.end() #define int long long #define ll long long #define M 1000000007 #define MM 998244353 #define inputarr(a, n) \ for (int i = 0; i < n; ++i) \ cin >> a[i] #define GCD(m, n) __gcd(m, n) #define LCM(m, n) m *(n / GCD(m, n)) #define mii map<ll, ll> #define sz(a) (int)a.size() #define msi map<string, ll> #define rep(a, b) for (ll i = a; i < b; i++) #define rep0(n) for (ll i = 0; i < n; i++) #define repi(i, a, b) for (ll i = a; i < b; i++) #define pb push_back #define vi vector<ll> #define vs vector<string> #define ppb pop_back #define endl '\n' #define asdf \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define r0 return 0; #define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); --i) #define inputoutput \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); #define Set(a, s) (a, s, sizeof(a)) #define FOR repi #define pii pair<int, int> #define REVERSE(v) reverse(all(v)) #define trav(a, x) for (auto &a : x) #define display(x) \ trav(a, x) cout << a << " "; \ cout << endl #define debug cerr << "bhau" << endl #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { std::cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } ll max(ll a, ll b) { return (a > b) ? a : b; } int min(int a, int b) { return (a < b) ? a : b; } const int N = 1e3 + 10; int dp[N][N]; int solve() { dp[0][0] = 1; int ans = 0; int n; cin >> n; int s; cin >> s; int a[n]; inputarr(a, n); for (int i = 0, left = 1, right = n; i < n; i++, left++, right--) { // trace(i); if (a[i] == s) { ans = (ans + left * right) % MM; } else if (a[i] < s) { dp[i + 1][a[i]] = left; } // trace(1); for (int sum = 1; sum < s; sum++) { // trace(sum,a[i]); if (sum + a[i] == s) { ans += (dp[i][sum] * right) % MM; ans %= MM; } else if (sum + a[i] < s) { dp[i + 1][sum + a[i]] += dp[i][sum]; } } for (int sum = 0; sum < s; sum++) { dp[i + 1][sum] = (dp[i + 1][sum] + dp[i][sum]) % MM; } } cout << ans << endl; // for(int i=1) return 0; } signed main() { asdf int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; // #include "testlib.h" #define ff first #define ss second #define all(v) v.begin(), v.end() #define int long long #define ll long long #define M 1000000007 #define MM 998244353 #define inputarr(a, n) \ for (int i = 0; i < n; ++i) \ cin >> a[i] #define GCD(m, n) __gcd(m, n) #define LCM(m, n) m *(n / GCD(m, n)) #define mii map<ll, ll> #define sz(a) (int)a.size() #define msi map<string, ll> #define rep(a, b) for (ll i = a; i < b; i++) #define rep0(n) for (ll i = 0; i < n; i++) #define repi(i, a, b) for (ll i = a; i < b; i++) #define pb push_back #define vi vector<ll> #define vs vector<string> #define ppb pop_back #define endl '\n' #define asdf \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define r0 return 0; #define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); --i) #define inputoutput \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); #define Set(a, s) (a, s, sizeof(a)) #define FOR repi #define pii pair<int, int> #define REVERSE(v) reverse(all(v)) #define trav(a, x) for (auto &a : x) #define display(x) \ trav(a, x) cout << a << " "; \ cout << endl #define debug cerr << "bhau" << endl #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { std::cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } ll max(ll a, ll b) { return (a > b) ? a : b; } int min(int a, int b) { return (a < b) ? a : b; } const int N = 3e3 + 10; int dp[N][N]; int solve() { dp[0][0] = 1; int ans = 0; int n; cin >> n; int s; cin >> s; int a[n]; inputarr(a, n); for (int i = 0, left = 1, right = n; i < n; i++, left++, right--) { // trace(i); if (a[i] == s) { ans = (ans + left * right) % MM; } else if (a[i] < s) { dp[i + 1][a[i]] = left; } // trace(1); for (int sum = 1; sum < s; sum++) { // trace(sum,a[i]); if (sum + a[i] == s) { ans += (dp[i][sum] * right) % MM; ans %= MM; } else if (sum + a[i] < s) { dp[i + 1][sum + a[i]] += dp[i][sum]; } } for (int sum = 0; sum < s; sum++) { dp[i + 1][sum] = (dp[i + 1][sum] + dp[i][sum]) % MM; } } cout << ans << endl; // for(int i=1) return 0; } signed main() { asdf int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
65
66
65
66
0
p02734
C++
Time Limit Exceeded
/*temp*/ // // // // // #undef _DEBUG // #pragma GCC optimize("Ofast") // 不動小数点の計算高速化 // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #include <boost/multiprecision/cpp_int.hpp> #ifdef _DEBUG #include "template.h" #else #if __cplusplus >= 201703L /*Atcoderでしか使えない(c++17 && このテンプレートが使えるならAtcoder)*/ #include <boost/sort/pdqsort/pdqsort.hpp> #define fast_sort boost::sort::pdqsort #endif #endif #ifndef _DEBUG #ifndef UNTITLED15_TEMPLATE_H #define UNTITLED15_TEMPLATE_H #ifdef _DEBUG #include "bits_stdc++.h" #else #include <bits/stdc++.h> #endif #ifndef fast_sort #define fast_sort sort #endif // #define use_pq #define use_for #define use_for_each #define use_sort #define use_fill #define use_rand #define use_mgr #define use_rui #define use_compress // // // // // // #define use_pbds #ifdef use_pbds #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <class T, class U, class W, class X> auto count(__gnu_pbds::gp_hash_table<T, U, W> &a, X k) { return a.find(k) != a.end(); } #endif using namespace std; using namespace std::chrono; /*@formatter:off*/ #define ll long long using sig_dou = double; // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define mt make_tuple #define pb pop_back #define pf push_front #define eb emplace_back #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() template <class T, class U> auto max(T a, U b) { return a > b ? a : b; } template <class T, class U> auto min(T a, U b) { return a < b ? a : b; } // optional<T>について下でオーバーロード(nullopt_tを左辺右辺について単位元として扱う) template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } // メタ系 meta template <typename T, typename U = typename T::value_type> std::true_type value_type_tester(signed); template <typename T> std::false_type value_type_tester(long); template <typename T> struct has_value_type : decltype(value_type_tester<T>(0)) {}; // A<T>でTを返す template <class T> using decl_t = typename T::value_type; // A<B<.....T>>でTを返す template <class T, bool end = !has_value_type<T>::value> struct decl2_ { typedef T type; }; template <class T> struct decl2_<T, false> { typedef typename decl2_<decl_t<T>>::type type; }; template <class T> using decl2_t = typename decl2_<T>::type; // #define decl_max(a, b) decltype(max(MAX<decltype(a)>(), MAX<decltype(b)>())) #define is_same2(T, U) is_same<T, U>::value template <class T> struct is_vector : std::false_type {}; template <class T> struct is_vector<std::vector<T>> : std::true_type {}; // 大きい型を返す max_type<int, char>::type // todo mintがlong long より小さいと判定されるためバグる template <class T1, class T2, bool t1_bigger = (sizeof(T1) > sizeof(T2))> struct max_type { typedef T1 type; }; template <class T1, class T2> struct max_type<T1, T2, false> { typedef T2 type; }; template <class T> struct vec_rank : integral_constant<int, 0> {}; template <class T> struct vec_rank<vector<T>> : integral_constant<int, vec_rank<T>{} + 1> {}; // N個のTを並べたtupleを返す // tuple_n<3, int>::type tuple<int, int, int> template <size_t N, class T, class... Arg> struct tuple_n { typedef typename tuple_n<N - 1, T, T, Arg...>::type type; }; template <class T, class... Arg> struct tuple_n<0, T, Arg...> { typedef tuple<Arg...> type; }; struct dummy_t1 {}; struct dummy_t2 {}; struct dummy_t3 {}; struct dummy_t4 {}; struct dummy_t5 {}; struct dummy_t6 {}; // template<class T, require(is_integral<T>::value)>など #define require_t(bo) enable_if_t<bo> * = nullptr // 複数でオーバーロードする場合、引数が同じだとうまくいかないため // require_arg(bool, dummy_t1) // require_arg(bool, dummy_t2)等とする #define require_arg1(bo) enable_if_t<bo> * = nullptr #define require_arg2(bo, dummy_type) enable_if_t<bo, dummy_type> * = nullptr #define require_arg(...) \ over2(__VA_ARGS__, require_arg2, require_arg1)(__VA_ARGS__) //->//enable_if_tのtを書き忘れそうだから #define require_ret(bo, ret_type) enable_if_t<bo, ret_type> #define int long long // todo 消したら動かない intの代わりにsignedを使う auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName // 最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name void assert2(bool b, const string &s = "") { if (!b) { cerr << s << endl; exit(1); /*assert(0);*/ } } // my_nulloptをあらゆる操作の単位元的な物として扱う // vectorの参照外時に返したり、右辺値として渡されたときに何もしないなど struct my_nullopt_t { } my_nullopt; #define nullopt_t my_nullopt_t #define nullopt my_nullopt /*@formatter:off*/ // 値が無いときは、setを使わない限り代入できない //=を使っても無視される template <class T> struct my_optional { private: bool is_null; T v; public: typedef T value_type; my_optional() : is_null(true) {} my_optional(const nullopt_t &) : is_null(true) {} my_optional(const T &v) : v(v), is_null(false) {} bool has_value() const { return !is_null; } T &value() { static string mes = "optional has no value"; assert2(!is_null, mes); return v; } const T &value() const { static string mes = "optional has no value"; assert2(!is_null, mes); return v; } void set(const T &nv) { is_null = false; v = nv; } template <class U> void operator=(const U &v) { set(v); // null状態でも代入出来るようにした // if (has_value())value() = v; else return; } template <class U> void operator=(const my_optional<U> &v) { if (/*has_value() && */ v.has_value()) (*this) = v; else return; } /*@formatter:off*/ void reset() { is_null = true; } void operator=(const nullopt_t &) { reset(); } template <require_t(!is_same2(T, bool))> explicit operator bool() { return !is_null; } // nullの時はエラー operator T &() { return value(); } operator const T &() const { return value(); } my_optional<T> operator++() { if (this->has_value()) { this->value()++; return *this; } else { return *this; } } my_optional<T> operator++(signed) { if (this->has_value()) { auto tem = *this; this->value()++; return tem; } else { return *this; } } my_optional<T> operator--() { if (this->has_value()) { this->value()--; return *this; } else { return *this; } } my_optional<T> operator--(signed) { if (this->has_value()) { auto tem = *this; this->value()--; return tem; } else { return *this; } } }; template <class T> istream &operator>>(istream &iss, my_optional<T> &v) { T val; iss >> val; v.set(val); return iss; } #define optional my_optional template <class T> using opt = my_optional<T>; // template<class T, class A = std::allocator<T>> struct debtor : std::vector<T, // A> { template <class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key, T>>> struct o_map : std::map<Key, optional<T>, Compare, Allocator> { optional<T> emp; o_map() : std::map<Key, optional<T>, Compare, Allocator>() {} auto operator()(const nullopt_t &) { return nullopt; } optional<T> &operator()(const optional<Key> &k) { if (k.has_value()) { return std::map<Key, optional<T>, Compare, Allocator>::operator[]( k.value()); } else { emp.reset(); return emp; } } optional<T> &operator()(const Key &k) { auto &v = std::map<Key, optional<T>, Compare, Allocator>::operator[](k); if (v.has_value()) return v; else { v.set(0); return v; } } template <class U> void operator[](U) { static string mes = "s_map cant []"; assert2(0, mes); } }; // 以下、空のoptionalをnulloptと書く // ov[-1(参照外)] でnulloptを返す // ov[nullopt] で nulloptをかえす template <class T> struct ov { optional<T> emp; vector<optional<T>> v; ov(int i = 0, T val = 0) : v(i, val) {} template <class U> ov(const U &rhs) { v.resize(sz(rhs)); for (int i = 0; i < sz(rhs); i++) v[i].set(rhs[i]); } optional<T> &operator()(int i) { if (i < 0 || sz(v) <= i) { emp.reset(); return emp; } else { return v[i]; } } optional<T> &operator()(const nullopt_t &) { return operator()(-1); } optional<T> &operator()(const optional<T> &i) { if (i.has_value()) return operator()(i.value()); else { return operator()(-1); } } /*@formatter:off*/ }; template <class T> string deb_tos(const ov<T> &v) { return deb_tos(v.v); } // vectorに対しての処理は.vを呼ぶ template <class T> class ovv { optional<T> emp; public: vector<vector<optional<T>>> v; ovv(int i = 0, int j = 0, T val = 0) : v(i, vector<optional<T>>(j, val)) {} optional<T> &operator()(int i, int j) { if (i < 0 || j < 0 || sz(v) <= i || sz(v[i]) <= j) { emp.reset(); return emp; } else { return v[i][j]; } } // 再帰ver 遅いと思う // optional<T>& gets(optional<T>& v){return v;} // template<class V, class H, class... U> optional<T>& gets(V& v, H i, U... // tail){ if constexpr(is_same2(H, nullopt_t))return // operator()(-1,-1); else if constexpr(is_same2(H, optional<int>)){ // if(i.has_value())return gets(v[(int)i], tail...); else return // operator()(-1,-1); }else if constexpr(is_integral<H>::value){ // return gets(v[(int)i], tail...); }else{ assert(0); // return emp; } } #if __cplusplus >= 201703L // if constexprバージョン 上が遅かったらこれで template <class U, class V> optional<T> &operator()(const U &i, const V &j) { /*駄目な場合を除外*/ if constexpr (is_same2(U, nullopt_t) || is_same2(U, nullopt_t)) { return operator()(-1, -1); /* o, o*/ } else if constexpr (is_same2(U, optional<int>) && is_same2(V, optional<int>)) { return operator()(i.has_value() ? (int)i : -1, j.has_value() ? (int)j : -1); /* o, x*/ } else if constexpr (is_same2(U, optional<int>)) { return operator()(i.has_value() ? (int)i : -1, (int)j); /* x, o*/ } else if constexpr (is_same2(V, optional<int>)) { return operator()((int)i, j.has_value() ? (int)j : -1); /* x, x*/ } else { return operator()((int)i, (int)j); } } #endif operator const vector<vector<optional<T>>> &() { return v; } }; template <class T> istream &operator>>(istream &iss, ovv<T> &a) { for (int h = 0; h < sz(a); h++) { for (int w = 0; w < sz(a[h]); w++) { iss >> a.v[h][w]; } } return iss; } template <class T> string deb_tos(const ovv<T> &v) { return deb_tos(v.v); } template <class T> struct ov3 { optional<T> emp; vector<vector<vector<optional<T>>>> v; ov3(int i, int j, int k, T val = 0) : v(i, vector<vector<optional<T>>>(j, vector<optional<T>>(k, val))) {} optional<T> &operator()(int i, int j, int k) { if (i < 0 || j < 0 || sz(v) <= i || sz(v[i]) <= j) { if (k < 0 || sz(v[i][j]) <= k) { emp.reset(); return emp; } } return v[i][j][k]; } private: #if __cplusplus >= 201703L // 再帰ver 遅いと思う template <class V, class H> optional<T> &gets(V &nowv, H i) { if constexpr (is_same2(H, nullopt_t)) { emp.reset(); return emp; } else if constexpr (is_same2(H, optional<int>)) { if (i.has_value()) { return nowv[(int)i]; } else { emp.reset(); return emp; } } else if constexpr (is_integral<H>::value) { return nowv[(int)i]; } else { static string mes = "ov3 error not index"; assert2(0, mes); emp.reset(); return emp; } } // todo const &消した template <class V, class H, class... U> optional<T> &gets(V &nowv, H i, U... tail) { if constexpr (is_same2(H, nullopt_t)) { emp.reset(); return emp; } else if constexpr (is_same2(H, optional<int>)) { if (i.has_value()) { return gets(nowv[(int)i], tail...); } else { emp.reset(); return emp; } } else if constexpr (is_integral<H>::value) { return gets(nowv[(int)i], tail...); } else { static string mes = "ov3 error not index"; assert2(0, mes); emp.reset(); return emp; } } #endif public: template <class U, class V, class W> optional<T> &operator()(U i, V j, W k) { return gets(v, i, j, k); } /*@formatter:off*/ }; template <class T> string deb_tos(const ov3<T> &v) { return deb_tos(v.v); } // nullopt_t // 優先順位 // null, [opt, tem] // + と += は違う意味を持つ // val+=null : val // val+null : null // //+は途中計算 //+=は最終的に格納したい値にだけ持たせる //+=がvoidを返すのは、途中計算で使うのを抑制するため // nulloptを考慮する際、計算途中では+を使ってnulloptを作り // 格納する際は+=で無効にする必要がある // 演算子== // optional<int>(10) == 10 // 全ての型に対応させ、value_typeが等しいかを見るようにするのもありかも // null同士を比較する状況はおかしいのではないか bool operator==(const nullopt_t &, const nullopt_t &) { assert2(0, "nul == null cant hikaku"); return false; } template <class T> bool operator==(const nullopt_t &, const T &) { return false; } template <class T> bool operator!=(const nullopt_t &, const T &) { return true; } template <class T> bool operator==(const T &, const nullopt_t &) { return false; } template <class T> bool operator!=(const T &, const nullopt_t &) { return true; } // nullを nullopt_t &operator+(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator-(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator*(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator/(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator+=(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator-=(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator*=(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator/=(const nullopt_t &, const nullopt_t &) { return nullopt; } template <class ANY> nullopt_t operator+(const nullopt_t &, const ANY &) { return nullopt; } template <class ANY> nullopt_t operator-(const nullopt_t &, const ANY &) { return nullopt; } template <class ANY> nullopt_t operator*(const nullopt_t &, const ANY &) { return nullopt; } template <class ANY> nullopt_t operator/(const nullopt_t &, const ANY &) { return nullopt; } template <class ANY> nullopt_t operator+(const ANY &, const nullopt_t &) { return nullopt; } template <class ANY> nullopt_t operator-(const ANY &, const nullopt_t &) { return nullopt; } template <class ANY> nullopt_t operator*(const ANY &, const nullopt_t &) { return nullopt; } template <class ANY> nullopt_t operator/(const ANY &, const nullopt_t &) { return nullopt; } template <class ANY> void operator+=(nullopt_t &, const ANY &) {} template <class ANY> void operator-=(nullopt_t &, const ANY &) {} template <class ANY> void operator*=(nullopt_t &, const ANY &) {} template <class ANY> void operator/=(nullopt_t &, const ANY &) {} template <class ANY> void operator+=(ANY &, const nullopt_t &) {} template <class ANY> void operator-=(ANY &, const nullopt_t &) {} template <class ANY> void operator*=(ANY &, const nullopt_t &) {} template <class ANY> void operator/=(ANY &, const nullopt_t &) {} template <class T> struct is_optional : false_type {}; template <class T> struct is_optional<optional<T>> : true_type {}; template <class T, class U> true_type both_optional(optional<T> t, optional<U> u); false_type both_optional(...); template <class T, class U> class opt_check : public decltype(both_optional(declval<T>(), declval<U>())) {}; // optionalは同じ型同士しか足せない //(o, t), (t, o), (o, o) #define opt_tem(op) \ template <class O, class O_ret = decltype(declval<O>() op declval<O>())> \ optional<O_ret> operator op(const optional<O> &opt1, \ const optional<O> &opt2) { \ if (!opt1.has_value() || !opt2.has_value()) { \ return optional<O_ret>(); \ } else { \ return optional<O_ret>(opt1.value() op opt2.value()); \ } \ } \ template <class O, class T, \ class O_ret = decltype(declval<O>() op declval<O>())> \ auto operator op(const optional<O> &opt, const T &tem) \ ->require_ret(!(opt_check<optional<O>, T>::value), optional<O_ret>) { \ if (!opt.has_value()) { \ return optional<O_ret>(); \ } else { \ return optional<O_ret>(opt.value() op tem); \ } \ } \ template <class O, class T, \ class O_ret = decltype(declval<O>() op declval<O>())> \ auto operator op(const T &tem, const optional<O> &opt) \ ->require_ret(!(opt_check<optional<O>, T>::value), optional<O_ret>) { \ if (!opt.has_value()) { \ return optional<O_ret>(); \ } else { \ return optional<O_ret>(opt.value() op tem); \ } \ } /*@formatter:off*/ opt_tem(+) opt_tem(-) opt_tem(*) opt_tem(/) // 比較はoptional<bool>を返す opt_tem(<) opt_tem(>) opt_tem(<=) opt_tem(>=) /*@formatter:on*/ /*@formatter:off*/ template <class O, class T> bool operator==(const optional<O> &opt, const T &tem) { if (opt.has_value()) { return opt.value() == tem; } else return nullopt == tem; } template <class O, class T> bool operator!=(const optional<O> &opt, const T &tem) { if (opt.has_value()) { return opt.value() != tem; } else return nullopt != tem; } template <class O, class T> bool operator==(const T &tem, const optional<O> &opt) { if (opt.has_value()) { return opt.value() == tem; } else return nullopt == tem; } template <class O, class T> bool operator!=(const T &tem, const optional<O> &opt) { if (opt.has_value()) { return opt.value() != tem; } else return nullopt != tem; } template <class O> bool operator==(const optional<O> &opt1, const optional<O> &opt2) { if (opt1.has_value() != opt2.has_value()) { return false; } else if (opt1.has_value()) { return opt1.value() == opt2.value(); } else { return nullopt == nullopt; } } template <class O> bool operator!=(const optional<O> &opt1, const optional<O> &opt2) { return !(opt1 == opt2); } //(a+=null) != (a=a+null) // a null template <class T, class O> void operator+=(T &tem, const optional<O> &opt) { if (opt.has_value()) { tem += opt.value(); } } template <class T, class O> void operator-=(T &tem, const optional<O> &opt) { if (opt.has_value()) { tem -= opt.value(); } } template <class T, class O> void operator*=(T &tem, const optional<O> &opt) { if (opt.has_value()) { tem *= opt.value(); } } template <class T, class O> void operator/=(T &tem, const optional<O> &opt) { if (opt.has_value()) { tem /= opt.value(); } } template <class T, class O> void operator+=(optional<O> &opt, const T &tem) { if (opt.has_value()) { opt.value() += tem; } } template <class T, class O> void operator-=(optional<O> &opt, const T &tem) { if (opt.has_value()) { opt.value() -= tem; } } template <class T, class O> void operator*=(optional<O> &opt, const T &tem) { if (opt.has_value()) { opt.value() *= tem; } } template <class T, class O> void operator/=(optional<O> &opt, const T &tem) { if (opt.has_value()) { opt.value() /= tem; } } // template <class Ol, class Or> void operator+=(optional<Ol> &opl, const optional<Or> &opr) { if (opr.has_value()) { return opl += opr.value(); } } template <class Ol, class Or> void operator-=(optional<Ol> &opl, const optional<Or> &opr) { if (opr.has_value()) { return opl -= opr.value(); } } template <class Ol, class Or> void operator*=(optional<Ol> &opl, const optional<Or> &opr) { if (opr.has_value()) { return opl *= opr.value(); } } template <class Ol, class Or> void operator/=(optional<Ol> &opl, const optional<Or> &opr) { if (opr.has_value()) { return opl /= opr.value(); } } /*@formatter:off*/ template <class U> auto max(const nullopt_t &, const U &val) { return val; } template <class U> auto max(const U &val, const nullopt_t &) { return val; } template <class U> auto min(const nullopt_t &, const U &val) { return val; } template <class U> auto min(const U &val, const nullopt_t &) { return val; } template <class T, class U> auto max(const optional<T> &opt, const U &val) { if (opt.has_value()) return max(opt.value(), val); else return val; } template <class T, class U> auto max(const U &val, const optional<T> &opt) { if (opt.has_value()) return max(opt.value(), val); else return val; } template <class T, class U> auto min(const optional<T> &opt, const U &val) { if (opt.has_value()) return min(opt.value(), val); else return val; } template <class T, class U> auto min(const U &val, const optional<T> &opt) { if (opt.has_value()) return min(opt.value(), val); else return val; } // null , optional, T bool chma(nullopt_t &, const nullopt_t &) { return false; } template <class T> bool chma(T &opt, const nullopt_t &) { return false; } template <class T> bool chma(nullopt_t &, const T &opt) { return false; } template <class T> bool chma(optional<T> &olv, const optional<T> &orv) { if (orv.has_value()) { return chma(olv, orv.value()); } else return false; } template <class T, class U> bool chma(optional<T> &opt, const U &rhs) { if (opt.has_value()) { return chma(opt.value(), rhs); } else return false; } template <class T, class U> bool chma(T &lhs, const optional<U> &opt) { if (opt.has_value()) { return chma(lhs, opt.value()); } else return false; } bool chmi(nullopt_t &, const nullopt_t &) { return false; } template <class T> bool chmi(T &opt, const nullopt_t &) { return false; } template <class T> bool chmi(nullopt_t &, const T &opt) { return false; } template <class T> bool chmi(optional<T> &olv, const optional<T> &orv) { if (orv.has_value()) { return chmi(olv, orv.value()); } else return false; } template <class T, class U> bool chmi(optional<T> &opt, const U &rhs) { if (opt.has_value()) { return chmi(opt.value(), rhs); } else return false; } template <class T, class U> bool chmi(T &lhs, const optional<U> &opt) { if (opt.has_value()) { return chmi(lhs, opt.value()); } else return false; } template <class T> ostream &operator<<(ostream &os, optional<T> p) { if (p.has_value()) os << p.value(); else os << "e"; return os; } template <class T> using opt = my_optional<T>; struct xorshift { /*@formatter:on*/ static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } /*@formatter:off*/ size_t operator()(const uint64_t &x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(const std::pair<ll, ll> &x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } template <class T, class U> size_t operator()(const std::pair<T, U> &x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); uint64_t hasx = splitmix64(x.first); uint64_t hasy = splitmix64(x.second + FIXED_RANDOM); return hasx ^ hasy; } template <class T> size_t operator()(const vector<T> &x) const { uint64_t has = 0; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); uint64_t rv = splitmix64(FIXED_RANDOM); for (int i = 0; i < sz(x); i++) { uint64_t v = splitmix64(x[i] + rv); has ^= v; rv = splitmix64(rv); } return has; } }; #ifdef _DEBUG string message; string res_mes; // #define use_debtor // template<class T, class U, class X> auto count(unordered_map<T, U> &a, X k) { // return a.find(k) != a.end(); } #ifdef use_debtor // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference my_at(typename std::vector<T>::size_type n, vector<int> &ind) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; ind.push_back(n); cerr << "out index at "; for (auto &&i : ind) { cerr << i << " "; } cerr << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef use_pbds template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> #endif // 区間削除は出来ない // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; template <class K, class V> using umap_f = my_unordered_map<K, V>; #else #define endl '\n' // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> template <class K, class V> using umap_f = __gnu_pbds::gp_hash_table<K, V, xorshift>; #endif #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umappp unordered_map<P, P> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> template <class T, class U, class X> auto count(unordered_map<T, U> &a, X k) { return a.find(k) != a.end(); } /*@formatter:off*/ #ifdef use_pbds template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } #endif // 衝突対策 #define ws ws_ // todo 要らないと思う template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 */ } bool operator==(const T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; // T mt(ll a, ll b, ll c) { return T(a, b, c); } // F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } // 関数内をまとめる // 初期値l=-1, r=-1 void set_lr12(int &l, int &r, int n) { /*r==-1*/ if (r == -1) { if (l == -1) { l = 0; r = n; } else { r = l; l = 0; } } } //@マクロ省略系 型,構造 // using で元のdoubleを同時に使えるはず #define double_big #ifdef double_big #define double long double // #define pow powl #endif using dou = double; /*@formatter:off*/ template <class T> T MAX() { return numeric_limits<T>::max(); } template <class T> T MIN() { return numeric_limits<T>::min(); } constexpr ll inf = (ll)1e9 + 100; constexpr ll linf = (ll)1e18 + 100; constexpr dou dinf = (dou)linf * linf; constexpr char infc = '{'; const string infs = "{"; template <class T> T INF() { return MAX<T>() / 2; } template <> signed INF() { return inf; } template <> ll INF() { return linf; } template <> double INF() { return dinf; } template <> char INF() { return infc; } template <> string INF() { return infs; } const double eps = 1e-9; // #define use_epsdou #ifdef use_epsdou // 基本コメントアウト struct epsdou { double v; epsdou(double v = 0) : v(v) {} template <class T> epsdou &operator+=(T b) { v += (double)b; return (*this); } template <class T> epsdou &operator-=(T b) { v -= (double)b; return (*this); } template <class T> epsdou &operator*=(T b) { v *= (double)b; return (*this); } template <class T> epsdou &operator/=(T b) { v /= (double)b; return (*this); } epsdou operator+(epsdou b) { return v + (double)b; } epsdou operator-(epsdou b) { return v - (double)b; } epsdou operator*(epsdou b) { return v * (double)b; } epsdou operator/(epsdou b) { return v / (double)b; } epsdou operator-() const { return epsdou(-v); } template <class T> bool operator<(T b) { return v < (double)b; } template <class T> bool operator>(T b) { auto r = (double)b; return v > (double)b; } template <class T> bool operator==(T b) { return fabs(v - (double)b) <= eps; } template <class T> bool operator<=(T b) { return v < (double)b || fabs(v - b) <= eps; } template <class T> bool operator>=(T b) { return v > (double)b || fabs(v - b) <= eps; } operator double() { return v; } }; template <> epsdou MAX() { return MAX<double>(); } template <> epsdou MIN() { return MIN<double>(); } // priqrity_queue等で使うのに必要 bool operator<(const epsdou &a, const epsdou &b) { return a.v < b.v; } bool operator>(const epsdou &a, const epsdou &b) { return a.v > b.v; } istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss; } ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os; } #define eps_conr_t(o) \ template <class T> epsdou operator o(T a, epsdou b) { return (dou)a o b.v; } #define eps_conl_t(o) \ template <class T> epsdou operator o(epsdou a, T b) { return a.v o(dou) b; } eps_conl_t(+) eps_conl_t(-) eps_conl_t(*) eps_conl_t(/) eps_conr_t(+) eps_conr_t(-) eps_conr_t(*) eps_conr_t(/) // template<class U> epsdou max(epsdou a, U b){return a.v>b ? a.v: b;} // template<class U> epsdou max(U a, epsdou b){return a>b.v ? a: b.v;} // template<class U> epsdou min(epsdou a, U b){return a.v<b ? a.v: b;} // template<class U> epsdou min(U a, epsdou b){return a<b.v ? a: b.v;} #undef double #define double epsdou #undef dou #define dou epsdou #endif template <class T = int, class A, class B = int> T my_pow(A a, B b = 2) { if (b < 0) return (T)1 / my_pow<T>(a, -b); #if __cplusplus >= 201703L if constexpr (is_floating_point<T>::value) { return pow((T)a, (T)b); } else if constexpr (is_floating_point<A>::value) { assert2( 0, "pow <not dou>(dou, )"); /*return 0;しない方がコンパイル前に(voidを受け取るので)エラーが出ていいかも*/ } else if constexpr (is_floating_point<B>::value) { assert2( 0, "pow <not dou>(, dou)"); /*return 0;しない方がコンパイル前に(voidを受け取るので)エラーが出ていいかも*/ } else { #endif T ret = 1; T bek = a; while (b) { if (b & 1) ret *= bek; bek *= bek; b >>= 1; } return ret; #if __cplusplus >= 201703L } #endif } #define pow my_pow #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam1(ret) [&](auto &&v) { return ret; } #define lam2(v, ret) [&](auto &&v) { return ret; } #define lam(...) over2(__VA_ARGS__, lam2, lam1)(__VA_ARGS__) #define lamr(right) [&](auto &&p) { return p right; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); // マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; // #define V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__) #define vvi(...) vv(ll, __VA_ARGS__) #define vvb(...) vv(bool, __VA_ARGS__) #define vvs(...) vv(string, __VA_ARGS__) #define vvd(...) vv(double, __VA_ARGS__) #define vvc(...) vv(char, __VA_ARGS__) #define vvp(...) vv(P, __VA_ARGS__) #define vvt(...) vv(T, __VA_ARGS__) // optional #define vvoi(...) vv(optional<ll>, __VA_ARGS__) template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setp = set<P>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap // 任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; /*@formatter:off*/ string to_string(char c) { string ret = ""; ret += c; return ret; } template <class T> class pq_min_max { vector<T> d; void make_heap() { for (int i = d.size(); i--;) { if (i & 1 && d[i - 1] < d[i]) swap(d[i - 1], d[i]); int k = down(i); up(k, i); } } inline int parent(int k) const { return ((k >> 1) - 1) & ~1; } int down(int k) { int n = d.size(); if (k & 1) { /* min heap*/ while (2 * k + 1 < n) { int c = 2 * k + 3; if (n <= c || d[c - 2] < d[c]) c -= 2; if (c < n && d[c] < d[k]) { swap(d[k], d[c]); k = c; } else break; } } else { /* max heap*/ while (2 * k + 2 < n) { int c = 2 * k + 4; if (n <= c || d[c] < d[c - 2]) c -= 2; if (c < n && d[k] < d[c]) { swap(d[k], d[c]); k = c; } else break; } } return k; } int up(int k, int root = 1) { if ((k | 1) < (int)d.size() && d[k & ~1] < d[k | 1]) { swap(d[k & ~1], d[k | 1]); k ^= 1; } int p; while (root < k && d[p = parent(k)] < d[k]) { /*max heap*/ swap(d[p], d[k]); k = p; } while (root < k && d[k] < d[p = parent(k) | 1]) { /* min heap*/ swap(d[p], d[k]); k = p; } return k; } public: pq_min_max() {} pq_min_max(const vector<T> &d_) : d(d_) { make_heap(); } template <class Iter> pq_min_max(Iter first, Iter last) : d(first, last) { make_heap(); } void operator+=(const T &x) { int k = d.size(); d.push_back(x); up(k); } void pop_min() { if (d.size() < 3u) { d.pop_back(); } else { swap(d[1], d.back()); d.pop_back(); int k = down(1); up(k); } } void pop_max() { if (d.size() < 2u) { d.pop_back(); } else { swap(d[0], d.back()); d.pop_back(); int k = down(0); up(k); } } const T &get_min() const { return d.size() < 2u ? d[0] : d[1]; } const T &get_max() const { return d[0]; } int size() const { return d.size(); } bool empty() const { return d.empty(); } }; // 小さいほうからM個取得するpq template <class T> struct helper_pq_size { pq_min_max<T> q; T su = 0; int max_size = 0; helper_pq_size() {} helper_pq_size(int max_size) : max_size(max_size) {} void clear() { q = pq_min_max<T>(); su = 0; } void operator+=(T v) { su += v; q += (v); if (sz(q) > max_size) { su -= q.get_max(); q.pop_max(); } } T sum() { return su; } T top() { return q.get_min(); } void pop() { su -= q.get_min(); q.pop_min(); } T poll() { T ret = q.get_min(); su -= ret; q.pop_min(); return ret; } ll size() { return q.size(); } }; // 大きいほうからM個取得するpq template <class T> struct helper_pqg_size { pq_min_max<T> q; T su = 0; int max_size = 0; helper_pqg_size() {} helper_pqg_size(int max_size) : max_size(max_size) {} void clear() { q = pq_min_max<T>(); su = 0; } void operator+=(T v) { su += v; q += (v); if (sz(q) > max_size) { su -= q.get_min(); q.pop_min(); } } T sum() { return su; } T top() { return q.get_max(); } void pop() { su -= q.get_max(); q.pop_max(); } T poll() { T ret = q.get_max(); su -= ret; q.pop_min(); return ret; } ll size() { return q.size(); } }; ; template <class T, class Container = vector<T>, class Compare = std::less<typename Container::value_type>> struct helper_pqg { priority_queue<T, Container, Compare> q; /*小さい順*/ T su = 0; helper_pqg() {} void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> using helper_pq = helper_pqg<T, vector<T>, greater<T>>; #if __cplusplus >= 201703L // 小さいほうからsize個残る // Tがoptionalなら空の時nullを返す template <class T> struct pq { helper_pq<T> a_q; /*大きい順*/ helper_pq_size<T> b_q; /*大きい順*/ bool aquery; T su = 0; pq(int size = inf) { aquery = size == inf; if (!aquery) { b_q = helper_pq_size<T>(size); } } void clear() { if (aquery) a_q.clear(); else b_q.clear(); } void operator+=(T v) { if (aquery) a_q += v; else b_q += v; } // optionalなら空の時nullを返す T top() { if constexpr (is_optional<T>::value) { if (aquery) { if (sz(a_q) == 0) return T(); return a_q.top(); } else { if (sz(b_q) == 0) return T(); return b_q.top(); } } else { if (aquery) return a_q.top(); else return b_q.top(); } } T sum() { if (aquery) return a_q.sum(); else return b_q.sum(); } // optionalなら空の時何もしない void pop() { if constexpr (is_optional<T>::value) { if (aquery) { if (sz(a_q)) a_q.pop(); } else { if (sz(b_q)) b_q.pop(); } } else { if (aquery) a_q.pop(); else b_q.pop(); } } /*T*/ T poll() { if constexpr (is_optional<T>::value) { if (aquery) { if (sz(a_q) == 0) return T(); return a_q.poll(); } else { if (sz(b_q) == 0) return T(); return b_q.poll(); } } else { if (aquery) return a_q.poll(); else return b_q.poll(); } } ll size() { if (aquery) return a_q.size(); else return b_q.size(); } /*@formatter:off*/ }; template <class T> struct pqg { helper_pqg<T> a_q; /*大きい順*/ helper_pqg_size<T> b_q; /*大きい順*/ bool aquery; T su = 0; pqg(int size = inf) { aquery = size == inf; if (!aquery) { b_q = helper_pqg_size<T>(size); } } void clear() { if (aquery) a_q.clear(); else b_q.clear(); } void operator+=(T v) { if (aquery) a_q += v; else b_q += v; } T sum() { if (aquery) return a_q.sum(); else return b_q.sum(); } T top() { if (aquery) return a_q.top(); else return b_q.top(); } void pop() { if (aquery) a_q.pop(); else b_q.pop(); } T poll() { if (aquery) return a_q.poll(); else return b_q.poll(); } ll size() { if (aquery) return a_q.size(); else return b_q.size(); } }; #else // 小さいほうからsize個残る template <class T> struct pq { helper_pq<T> a_q; /*大きい順*/ helper_pq_size<T> b_q; /*大きい順*/ bool aquery; T su = 0; pq(int size = inf) { aquery = size == inf; if (!aquery) { b_q = helper_pq_size<T>(size); } } void clear() { if (aquery) a_q.clear(); else b_q.clear(); } void operator+=(T v) { if (aquery) a_q += v; else b_q += v; } T sum() { if (aquery) return a_q.sum(); else return b_q.sum(); } T top() { if (aquery) return a_q.top(); else return b_q.top(); } void pop() { if (aquery) a_q.pop(); else b_q.pop(); } T poll() { if (aquery) return a_q.poll(); else return b_q.poll(); } ll size() { if (aquery) return a_q.size(); else return b_q.size(); } }; // 大きいほうからsize個残る template <class T> struct pqg { helper_pqg<T> a_q; /*大きい順*/ helper_pqg_size<T> b_q; /*大きい順*/ bool aquery; T su = 0; pqg(int size = inf) { aquery = size == inf; if (!aquery) { b_q = helper_pqg_size<T>(size); } } void clear() { if (aquery) a_q.clear(); else b_q.clear(); } void operator+=(T v) { if (aquery) a_q += v; else b_q += v; } T sum() { if (aquery) return a_q.sum(); else return b_q.sum(); } T top() { if (aquery) return a_q.top(); else return b_q.top(); } void pop() { if (aquery) a_q.pop(); else b_q.pop(); } T poll() { if (aquery) return a_q.poll(); else return b_q.poll(); } ll size() { if (aquery) return a_q.size(); else return b_q.size(); } }; #endif #define pqi pq<ll> #define pqgi pqg<ll> template <class T> string deb_tos(pq<T> &q) { vector<T> res; auto temq = q; while (sz(temq)) res.push_back(temq.top()), temq.pop(); stringstream ss; ss << res; return ss.str(); } template <class T> string deb_tos(pqg<T> &q) { vector<T> res; auto temq = q; while (sz(temq)) res.push_back(temq.top()), temq.pop(); stringstream ss; ss << res; return ss.str(); } /*@formatter:off*/ // マクロ 繰り返し // ↓@オーバーロード隔離 // todo 使わないもの非表示 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) // 逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #ifdef use_for // ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) // 正方形である必要はない // 直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int repvI = (int)a.size(), repvJ = (int)a[0].size(), i = 0, j = 0; \ i < repvI; nex_repv2(i, j, repvI, repvJ, a)) // 箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int repvI = (int)a.size(), repvJ = (int)a[0].size(), \ repvK = (int)a[0][0].size(), i = 0, j = 0, k = 0; \ i < repvI; nex_repv3(i, j, k, repvI, repvJ, repvK, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) #endif template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } // #define use_for #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define forrm1(st) \ for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it) #define forrm3(k, v, st) \ for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it) // 向こう側で // ++itか it = st.erase(it)とする #define fors1(st) for (auto &&it = st.begin(); it != st.end();) #define fors2(v, st) for (auto &&it = st.begin(); it != st.end();) #define fors3(v, st, r) \ for (auto &&it = st.begin(); it != st.end() && (*it) < r;) #define fors4(v, st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) #ifdef use_for #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define fora_f_init_2(a, A) ; #define fora_f_init_3(fora_f_i, a, A) auto &&a = A[fora_f_i]; #define fora_f_init_4(a, b, A, B) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; #define fora_f_init_5(fora_f_i, a, b, A, B) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; #define fora_f_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; \ auto &&c = C[fora_f_i]; #define fora_f_init_7(fora_f_i, a, b, c, A, B, C) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; \ auto &&c = C[fora_f_i]; #define fora_f_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; \ auto &&c = C[fora_f_i]; \ auto &&d = D[fora_f_i]; #define fora_f_init_9(fora_f_i, a, b, c, d, A, B, C, D) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; \ auto &&c = C[fora_f_i]; \ auto &&d = D[fora_f_i]; #define fora_f_init(...) \ over9(__VA_ARGS__, fora_f_init_9, fora_f_init_8, fora_f_init_7, \ fora_f_init_6, fora_f_init_5, fora_f_init_4, fora_f_init_3, \ fora_f_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_3(forr_i, a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_5(forr_i, a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_7(forr_i, a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init_9(forr_i, a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over9(__VA_ARGS__, forr_init_9, forr_init_8, forr_init_7, forr_init_6, \ forr_init_5, forr_init_4, forr_init_3, forr_init_2)(__VA_ARGS__) #define forp_init3(k, v, S) \ auto &&k = S[forp_i].first; \ auto &&v = S[forp_i].second; #define forp_init4(forp_i, k, v, S) \ auto &&k = S[forp_i].first; \ auto &&v = S[forp_i].second; #define forp_init(...) \ over4(__VA_ARGS__, forp_init4, forp_init3, forp_init2, \ forp_init1)(__VA_ARGS__) #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define forrm_init(k, v, ...) \ auto &&k = (*forrm_it).fi; \ auto &&v = (*forrm_it).se; #define fors_init(v, ...) auto &&v = (*it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); // こうしないとmapがおかしくなる #define fora_f_2(a, A) for (auto &&a : A) #define fora_f_3(fora_f_i, a, A) rep(fora_f_i, sz(A)) #define fora_f_4(a, b, A, B) rep(fora_f_i, sz(A)) #define fora_f_5(fora_f_i, a, b, A, B) rep(fora_f_i, sz(A)) #define fora_f_6(a, b, c, A, B, C) rep(fora_f_i, sz(A)) #define fora_f_7(fora_f_i, a, b, c, A, B, C) rep(fora_f_i, sz(A)) #define fora_f_8(a, b, c, d, A, B, C, D) rep(fora_f_i, sz(A)) #define fora_f_9(fora_f_i, a, b, c, d, A, B, C, D) rep(fora_f_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_3(forr_i, a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_5(forr_i, a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_7(forr_i, a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) #define forr_9(forr_i, a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) #endif // ↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // 自分込みで残りがREM以上の間ループを回す #define rem(i, N, REM) for (int i = 0; i < N - REM + 1; i++) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #ifdef use_for #define fora_f(...) \ over9(__VA_ARGS__, fora_f_9, fora_f_8, fora_f_7, fora_f_6, fora_f_5, \ fora_f_4, fora_f_3, fora_f_2)(__VA_ARGS__) #endif #define forr(...) \ over9(__VA_ARGS__, forr_9, forr_8, forr_7, forr_6, forr_5, forr_4, forr_3, \ forr_2)(__VA_ARGS__) // 0~N-2まで見る #define forar_init(v, rv, A) \ auto &&v = A[forar_i]; \ auto &&rv = A[forar_i + 1]; #define forar(v, rv, A) rep(forar_i, sz(A) - 1) #if __cplusplus >= 201703L template <size_t M_SZ, bool indexed, class Iterator, class T, class U = T, class V = T, class W = T> class ite_vec_merge : public Iterator { std::size_t i = 0; vector<T> &A; vector<U> &B; vector<V> &C; vector<W> &D; public: ite_vec_merge(Iterator ita, vector<T> &A) : Iterator(ita), A(A), B(A), C(A), D(A) {} ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B) : Iterator(ita), A(A), B(B), C(A), D(A) {} ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B, vector<V> &C) : Iterator(ita), A(A), B(B), C(C), D(A) {} ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B, vector<V> &C, vector<W> &D) : Iterator(ita), A(A), B(B), C(C), D(D) {} auto &operator++() { ++i; this->Iterator::operator++(); return *this; } auto operator*() const noexcept { if constexpr (!indexed && M_SZ == 1) { return tuple<T &>(A[i]); } else if constexpr (!indexed && M_SZ == 2) { return tuple<T &, U &>(A[i], B[i]); } else if constexpr (!indexed && M_SZ == 3) { return tuple<T &, U &, V &>(A[i], B[i], C[i]); } else if constexpr (!indexed && M_SZ == 4) { return tuple<T &, U &, V &, W &>(A[i], B[i], C[i], D[i]); } else if constexpr (indexed && M_SZ == 1) { return tuple<int, T &>(i, A[i]); } else if constexpr (indexed && M_SZ == 2) { return tuple<int, T &, U &>(i, A[i], B[i]); } else if constexpr (indexed && M_SZ == 3) { return tuple<int, T &, U &, V &>(i, A[i], B[i], C[i]); } else if constexpr (indexed && M_SZ == 4) { return tuple<int, T &, U &, V &, W &>(i, A[i], B[i], C[i], D[i]); } else { assert(0); return tuple<int>(i); } } }; template <size_t M_SZ, bool indexed, class T, class U = T, class V = T, class W = T> class vec_merge { vector<T> &a; vector<U> &b; vector<V> &c; vector<W> &d; public: vec_merge(vector<T> &a) : a(a), b(a), c(a), d(a) {} vec_merge(vector<T> &a, vector<U> &b) : a(a), b(b), c(a), d(a) {} vec_merge(vector<T> &a, vector<U> &b, vector<V> &c) : a(a), b(b), c(c), d(a) {} vec_merge(vector<T> &a, vector<U> &b, vector<V> &c, vector<W> &d) : a(a), b(b), c(c), d(d) {} auto begin() const { if constexpr (M_SZ == 1) { return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a}; } else if constexpr (M_SZ == 2) { return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a, b}; } else if constexpr (M_SZ == 3) { return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a, b, c}; } else if constexpr (M_SZ == 4) { return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a, b, c, d}; } else { assert(0); return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a}; } } auto end() const { if constexpr (M_SZ == 1) { return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a}; } else if constexpr (M_SZ == 2) { return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a, b}; } else if constexpr (M_SZ == 3) { return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a, b, c}; } else if constexpr (M_SZ == 4) { return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a, b, c, d}; } else { assert(0); return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a}; } } }; #endif #define fora_2(a, A) for (auto &&a : A) #if __cplusplus >= 201703L #define fora_3(i, a, A) \ for (auto [i, a] : vec_merge<1, true, decl_t<decltype(A)>>(A)) #define fora_4(a, b, A, B) \ for (auto [a, b] : \ vec_merge<2, false, decl_t<decltype(A)>, decl_t<decltype(B)>>(A, B)) #define fora_5(i, a, b, A, B) \ for (auto [i, a, b] : \ vec_merge<2, true, decl_t<decltype(A)>, decl_t<decltype(B)>>(A, B)) #define fora_6(a, b, c, A, B, C) \ for (auto [a, b, c] : \ vec_merge<3, false, decl_t<decltype(A)>, decl_t<decltype(B)>, \ decl_t<decltype(C)>>(A, B, C)) #define fora_7(i, a, b, c, A, B, C) \ for (auto [i, a, b, c] : \ vec_merge<3, true, decl_t<decltype(A)>, decl_t<decltype(B)>, \ decl_t<decltype(C)>>(A, B, C)) #define fora_8(a, b, c, d, A, B, C, D) \ for (auto [a, b, c, d] : \ vec_merge<4, false, decl_t<decltype(A)>, decl_t<decltype(B)>, \ decl_t<decltype(C)>, decl_t<decltype(D)>>(A, B, C, D)) #define fora_9(i, a, b, c, d, A, B, C, D) \ for (auto [i, a, b, c, d] : \ vec_merge<4, true, decl_t<decltype(A)>, decl_t<decltype(B)>, \ decl_t<decltype(C)>, decl_t<decltype(D)>>(A, B, C, D)) #endif // 構造化束縛ver // 1e5要素で40ms程度 // 遅いときはfora_fを使う #define fora(...) \ over9(__VA_ARGS__, fora_9, fora_8, fora_7, fora_6, fora_5, fora_4, fora_3, \ fora_2)(__VA_ARGS__) // #define forr(v, a) for(auto&& v : makeReversedRange(a)) // 参照を取らない /*@formatter:off*/ #ifdef use_for template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> auto to1d(vector<vector<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(vector<vector<vector<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<vector<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<vector<vector<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define forv(a, b) for (auto a : to1d(b)) // インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) // 値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #endif #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define forrm(...) \ over5(__VA_ARGS__, forrm5, forrm4, forrm3, forrm2, forrm1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp3(k, v, st) rep(forp_i, sz(st)) #define forp4(forp_i, k, v, st) rep(forp_i, sz(st)) #define forp(...) over4(__VA_ARGS__, forp4, forp3)(__VA_ARGS__) // to_vec(rep(i, N))のように使い // iが走査した値を持つvectorを返す #define to_vec2(type, my_for) \ [&]() { \ vector<type> ret; \ my_for { ret.push_back(i); } \ return ret; \ }() #define to_vec1(my_for) to_vec2(int, my_for) #define to_vec(...) over2(__VA_ARGS__, to_vec2, to_vec1)(__VA_ARGS__) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const double PI = 3.1415926535897932384626433832795029L; constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:off*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec_) { for (T &x : vec_) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec_) { for (ll i = 0; i < vec_.size(); ++i) os << vec_[i] << (i + 1 == vec_.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec_) { for (ll i = 0; i < vec_.size(); ++i) { for (ll j = 0; j < vec_[i].size(); ++j) { os << vec_[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { os << endl; for (auto &&v : m) os << v << endl; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, mset<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) { os << v << " "; } return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec_) { rep(h, sz(vec_)) { rep(w, sz(vec_[0])) { os << vec_[h][w]; } os << endl; } return os; } template <class T> struct range_now { int l; vector<T> A; range_now(vector<T> &&A, int l) : A(A), l(l) {} }; /*@formatter:off*/ // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora_f(v,a)os<<v<<endl;return os;} template <typename W, typename H> void resize(W &vec_, const H head) { vec_.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec_, const H &head, const T... tail) { vec_.resize(head); for (auto &v : vec_) resize(v, tail...); } // #define use_for_each //_each _all_of _any_of _none_of _find_if _rfind_if // _contains _count_if _erase_if _entry_if #ifdef use_for_each // todo Atcoderの過去問がc++17に対応したら #if __cplusplus >= 201703L // for_each以外はconst & (呼び出し側のラムダも) template <typename T, typename F> bool all_of2(const T &v, F f) { if constexpr (has_value_type<T>::value) { for (auto &&v_ : v) { if (!all_of2(v_, f)) return false; } return true; } else { return f(v); } } template <typename T, typename F> bool any_of2(const T &v, F f) { if constexpr (has_value_type<T>::value) { for (auto &&v_ : v) { if (!any_of2(v_, f)) return true; } return false; } else { return f(v); } } template <typename T, typename F> bool none_of2(const T &v, F f) { if constexpr (has_value_type<T>::value) { for (auto &&v_ : v) { if (none_of2(v_, f)) return false; } return true; } else { return f(v); } } // 存在しない場合 // 1次元 Nを返す // 多次元-1を返す template <typename T, typename F> ll find_if2(const vector<T> &v, F f) { rep(i, sz(v)) { if (f(v[i])) return i; } return sz(v); } template <typename T, typename F> tuple<int, int> find_if2(const vector<vector<T>> &v, F f) { rep(i, sz(v)) { rep(j, sz(v[i])) { if (f(v[i][j])) { return tuple<int, int>(i, j); } } } return tuple<int, int>(-1, -1); } template <typename T, typename F> auto find_if2(const vector<vector<vector<T>>> &v, F f) { rep(i, sz(v)) { if (auto ret = find_if2(v[i], f); get<0>(ret) != -1) { return tuple_cat(tuple<int>(i), ret); } } auto bad = tuple_cat(tuple<int>(-1), find_if2(v[0], f)); return bad; } template <class T, class F> auto find_if2(const range_now<T> &v, F f) { return find_if2(v.A, f) + v.l; } // 存在しない場合 // 1次元 -1を返す // 多次元-1を返す template <typename T, typename F> ll rfind_if2(const vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return i; } return -1; } template <typename T, typename F> tuple<int, int> rfind_if2(const vector<vector<T>> &v, F f) { rer(i, sz(v) - 1) { rer(j, sz(v[i]) - 1) { if (f(v[i][j])) { return tuple<int, int>(i, j); } } } return tuple<int, int>(-1, -1); } template <typename T, typename F> auto rfind_if2(const vector<vector<vector<T>>> &v, F f) { rer(i, sz(v) - 1) { if (auto ret = rfind_if2(v[i], f); get<0>(ret) != -1) { return tuple_cat(tuple<int>(i), ret); } } auto bad = tuple_cat(tuple<int>(-1), rfind_if2(v[0], f)); return bad; } // todo まとめられそう string,vector全般 template <class T> bool contains(const string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(const vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(const vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(const T &v, F f) { if constexpr (has_value_type<T>::value) { ll ret = 0; for (auto &&v_ : v) { ret += count_if2(v_, f); } return ret; } else { return f(v); } } template <typename T, typename F> void for_each2(T &a, F f) { if constexpr (has_value_type<T>::value) { for (auto &&v_ : a) for_each2(v_, f); } else { f(a); } } #else template <typename T, typename F> bool all_of2(const T &v, F f) { return f(v); } template <typename T, typename F> bool all_of2(const vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool any_of2(const T &v, F f) { return f(v); } template <typename T, typename F> bool any_of2(const vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f)) return true; } return false; } template <typename T, typename F> bool none_of2(const T &v, F f) { return f(v); } template <typename T, typename F> bool none_of2(const vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool find_if2(const T &v, F f) { return f(v); } template <typename T, typename F> ll find_if2(const vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f)) return i; } return sz(v); } template <typename T, typename F> bool rfind_if2(const T &v, F f) { return f(v); } template <typename T, typename F> ll rfind_if2(const vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f)) return i; } return -1; } template <class T> bool contains(const string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(const vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(const vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(const T &v, F f) { return f(v); } template <typename T, typename F> ll count_if2(const vector<T> &vec_, F f) { ll ret = 0; fora(v, vec_) { ret += count_if2(v, f); } return ret; } template <typename T, typename F> void for_each2(T &a, F f) { f(a); } template <typename T, typename F> void for_each2(vector<T> &a, F f) { for (auto &&v_ : a) for_each2(v_, f); } #endif template <typename W> ll count_od(const vector<W> &a) { return count_if2(a, [](ll v) { return v & 1; }); } template <typename W> ll count_ev(const vector<W> &a) { return count_if2(a, [](ll v) { return !(v & 1); }); } // 削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(const vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(const vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } template <typename T, typename F> vector<T> entry_if2(const vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> entry_if2(const vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = entry_if2(v[i], f); } return res; } template <typename T, typename F> ll l_rfind_if(const vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return i; } return -1; } template <typename T, typename F> bool l_contains_if(const vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return true; } return false; } template <class A, class B, class C> auto t_all_of(A a, B b, C c) { return std::all_of(a, b, c); } template <class A, class B, class C> auto t_any_of(A a, B b, C c) { return std::any_of(a, b, c); } template <class A, class B, class C> auto t_none_of(A a, B b, C c) { return std::none_of(a, b, c); } template <class A, class B, class C> auto t_find_if(A a, B b, C c) { return std::find_if(a, b, c); } template <class A, class B, class C> auto t_count_if(A a, B b, C c) { return std::count_if(a, b, c); } #define all_of_s__2(a, right) (t_all_of(ALL(a), lamr(right))) #define all_of_s__3(a, v, siki) (t_all_of(ALL(a), [&](auto v) { return siki; })) #define all_of_s(...) over3(__VA_ARGS__, all_of_s__3, all_of_s__2)(__VA_ARGS__) // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lamr(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of_s__2(a, right) (t_any_of(ALL(a), lamr(right))) #define any_of_s__3(a, v, siki) (t_any_of(ALL(a), [&](auto v) { return siki; })) #define any_of_s(...) over3(__VA_ARGS__, any_of_s__3, any_of_s__2)(__VA_ARGS__) #define any_of__2(a, right) any_of2(a, lamr(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of_s__2(a, right) (t_none_of(ALL(a), lamr(right))) #define none_of_s__3(a, v, siki) \ (t_none_of(ALL(a), [&](auto v) { return siki; })) #define none_of_s(...) \ over3(__VA_ARGS__, none_of_s__3, none_of_s__2)(__VA_ARGS__) #define none_of__2(a, right) none_of2(a, lamr(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if_s__2(a, right) (t_find_if(ALL(a), lamr(right)) - a.begin()) #define find_if_s__3(a, v, siki) \ (t_find_if(ALL(a), [&](auto v) { return siki; }) - a.begin()) #define find_if_s(...) \ over3(__VA_ARGS__, find_if_s__3, find_if_s__2)(__VA_ARGS__) #define find_if__2(a, right) find_if2(a, lamr(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if__4(a, l, v, siki) \ (find_if2(decltype(a)(a.begin() + l, a.end()), \ [&](auto v) { return siki; }) + \ l) #define find_if(...) \ over4(__VA_ARGS__, find_if__4, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if_s__2(a, right) l_rfind_if(a, lamr(right)) #define rfind_if_s__3(a, v, siki) l_rfind_if(a, [&](auto v) { return siki; }) #define rfind_if_s(...) \ over3(__VA_ARGS__, rfind_if_s__3, rfind_if_s__2)(__VA_ARGS__) #define rfind_if__2(a, right) rfind_if2(a, lamr(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if_s__2(a, right) l_contains_if(a, lamr(right)) #define contains_if_s__3(a, v, siki) \ l_contains_if(a, [&](auto v) { return siki; }) #define contains_if_s(...) \ over3(__VA_ARGS__, contains_if_s__3, contains_if_s__2)(__VA_ARGS__) #define contains_if__2(a, right) contains_if2(a, lamr(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if_s__2(a, right) (t_count_if(ALL(a), lamr(right))) #define count_if_s__3(a, v, siki) \ (t_count_if(ALL(a), [&](auto v) { return siki; })) #define count_if_s(...) \ over3(__VA_ARGS__, count_if_s__3, count_if_s__2)(__VA_ARGS__) #define count_if__2(a, right) count_if2(a, lamr(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) // vector<vi>で、viに対して操作 #define for_each_s__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each_s__3(a, v, shori) \ do { \ fora(v, a) { shori; } \ } while (0) #define for_each_s(...) \ over3(__VA_ARGS__, for_each_s__3, for_each_s__2)(__VA_ARGS__) // vector<vi>で、intに対して操作 #define for_each__2(a, right) for_each2(a, lamr(right)) #define for_each__3(a, v, shori) for_each2(a, [&](auto &v) { shori; }) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) for_each2(a, f); template <class T, class F> vector<T> help_for_eached(const vector<T> &A, F f) { vector<T> ret = A; for_each(ret, v, f(v)); return ret; } #define for_eached__2(a, right) help_for_eached(a, lamr(right)) #define for_eached__3(a, v, shori) help_for_eached(a, lam(v, shori)) #define for_eached(...) \ over3(__VA_ARGS__, for_eached__3, for_eached__2)(__VA_ARGS__) #define for_eached_f(a, f) for_eached2(a, f); #define each for_each #define eached for_eached // #define erase_if_s__2(a, right) l_erase_if2(a,lamr(right)) // #define erase_if_s__3(a, v, siki) l_erase_if2(a,[&](auto v){return siki;}) // #define erase_if_s(...) // over3(__VA_ARGS__,erase_if_s__3,erase_if_s__2)(__VA_ARGS__) #define erase_if__2(a, right) erase_if2(a, lamr(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) // #define entry_if_s__2(a, right) l_entry_if2(a,lamr(right)) // #define entry_if_s__3(a, v, siki) l_entry_if2(a,[&](auto v){return siki;}) // #define entry_if_s(...) // over3(__VA_ARGS__,entry_if_s__3,entry_if_s__2)(__VA_ARGS__) #define entry_if__2(a, right) entry_if2(a, lamr(right)) #define entry_if__3(a, v, siki) entry_if2(a, [&](auto v) { return siki; }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) entry_if2(a, f) #endif /*@formatter:off*/ template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(ALL(a), key), a.end()); } void replace(str &a, char key, char v) { replace(ALL(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } void replace(str &a) { int dec = 0; if ('a' <= a[0] && a[0] <= 'z') dec = 'a'; if ('A' <= a[0] && a[0] <= 'Z') dec = 'A'; fora(v, a) { v -= dec; } } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(ALL(a), ALL(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(ALL(a)); } vi iota(ll s, ll len) { vi ve(len); iota(ALL(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } /*@formatter:off*/ template <class T> void sort(vector<T> &a, int l = -1, int r = -1) { set_lr12(l, r, sz(a)); fast_sort(a.begin() + l, a.begin() + r); } template <class T> void rsort(vector<T> &a, int l = -1, int r = -1) { set_lr12(l, r, sz(a)); fast_sort(a.begin() + l, a.begin() + r, greater<T>()); }; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } template <class... T, class U> auto rsorted(U head, T... a) { rsort(head, a...); return head; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } // #define use_sort #ifdef use_sort enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(ALL(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(ALL(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(ALL(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(ALL(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(ALL(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(ALL(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(ALL(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(ALL(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } /*@formatter:off*/ void sort(string &a) { sort(ALL(a)); } void rsort(string &a) { sort(RALL(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(ALL(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(ALL(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> // 例えばreturn p.fi + p.se; template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } #endif template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(c); sort(d); return includes(ALL(c), ALL(d)); } template <class T> bool distinct(const vector<T> &A) { if ((int)(A).size() == 1) return true; if ((int)(A).size() == 2) return A[0] != A[1]; if ((int)(A).size() == 3) return (A[0] != A[1] && A[1] != A[2] && A[0] != A[2]); auto B = A; sort(B); int N = (B.size()); unique(B); return N == (int)(B.size()); } template <class H, class... T> bool distinct(const H &a, const T &...b) { return distinct(vector<H>{a, b...}); } /*@formatter:off*/ template <class T, class U> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U = int> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U = int> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool san(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return san(0, i, sz(a)) && san(0, j, sz(a)); } #define inside ins ll u0(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u0(vector<T> &a) { vector<T> ret = a; fora(v, ret) { v = u(v); } return ret; } // todo 名前 bool d_(int a, int b) { if (b == 0) return false; return (a % b) == 0; } // エラー void ole() { #ifdef _DEBUG cerr << "ole" << endl; exit(0); #endif string a = "a"; rep(i, 30) a += a; rep(i, 1 << 17) cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void re(string s = "") { cerr << s << endl; assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } template <class T> T in() { T ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } // value_typeを持つ場合呼べる // len回要素を追加する template <class Iterable, class T = typename Iterable::value_type> Iterable tin(int len) { Iterable ret; T tem; while (len--) { cin >> tem; ret += tem; } return ret; } template <class T> T tin() { T ret; cin >> ret; return ret; } template <class T> T tind(int len = 0) { auto ret = tin<T>(len); dec(ret, 1); return ret; } #define din_t2(type, a) \ type a; \ cin >> a #define din_t3(type, a, b) \ type a, b; \ cin >> a >> b #define din_t4(type, a, b, c) \ type a, b, c; \ cin >> a >> b >> c #define din_t5(type, a, b, c, d) \ type a, b, c, d; \ cin >> a >> b >> c >> d #define din_t6(type, a, b, c, d, e) \ type a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din_t7(type, a, b, c, d, e, f) \ type a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din_t(...) \ over7(__VA_ARGS__, din_t7, din_t6, din_t5, din_t4, din_t3, \ din_t2)(__VA_ARGS__) #define din(...) din_t(int, __VA_ARGS__) #define d_in #define dsig(...) din_t(signed, __VA_ARGS__) #define dst(...) din_t(string, __VA_ARGS__) #define dstr dst #define d_str dst #define dcha(...) din_t(char, __VA_ARGS__) #define dchar dcha #define ddou(...) din_t(double, __VA_ARGS__) #define din1d(a) \ din_t2(int, a); \ a-- #define din2d(a, b) \ din_t3(int, a, b); \ a--, b-- #define din3d(a, b, c) \ din_t4(int, a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din_t5(int, a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) /*@formatter:off*/ #ifdef _DEBUG template <class T> void err2(T &&head) { cerr << head; } template <class T, class... U> void err2(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); } template <class T, class... U> void err(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); cerr << "" << endl; } template <class T> void err(T &&head) { cerr << head << endl; } void err() { cerr << "" << endl; } // debで出力する最大長 constexpr int DEB_LEN = 20; constexpr int DEB_LEN_H = 12; string deb_tos(const int &v) { if (abs(v) == inf || abs(v) == linf) return "e"; else return to_string(v); } template <class T> string deb_tos(const T &a) { stringstream ss; ss << a; return ss.str(); } #ifdef use_epsdou string deb_tos(const epsdou &a) { return deb_tos(a.v); } #endif template <class T> string deb_tos(const optional<T> &a) { if (a.has_value()) { return deb_tos(a.value()); } else return "e"; } template <class T> string deb_tos(const vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), DEB_LEN); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << deb_tos(a[i]); if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:on*/ template <class T> string deb_tos(const vector<vector<T>> &a, vi H, vi W, int key = -1) { stringstream ss; ss << endl; vi lens(sz(W)); fora(h, H) { rep(wi, sz(W)) { if (sz(a[h]) <= W[wi]) break; lens[wi] = max(lens[wi], sz(deb_tos(a[h][W[wi]])) + 1); lens[wi] = max(lens[wi], sz(deb_tos(W[wi])) + 1); } } if (key == -1) ss << " *|"; else ss << " " << key << "|"; int wi = 0; fora(w, W) { ss << std::right << std::setw(lens[wi]) << w; wi++; } ss << "" << endl; rep(i, sz(W)) rep(lens[i]) ss << "_"; rep(i, 3) ss << "_"; ss << "" << endl; fora(h, H) { ss << std::right << std::setw(2) << h << "|"; int wi = 0; fora(w, W) { if (sz(a[h]) <= w) break; ss << std::right << std::setw(lens[wi]) << deb_tos(a[h][w]); wi++; } ss << "" << endl; } return ss.str(); } template <class T> string deb_tos(const vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = (H != inf) ? H : min({H, sz(a), DEB_LEN_H}); W = min({W, sz(a[0]), DEB_LEN_H}); vi hs, ws; rep(h, H) { hs.push_back(h); } rep(w, W) { ws.push_back(w); } return deb_tos(a, hs, ws, key); } /*@formatter:off*/ template <class T> string deb_tos(const vector<vector<vector<T>>> &a, ll H = inf) { stringstream ss; if (H == inf) H = DEB_LEN_H; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << deb_tos(a[i], inf, inf, i); } return ss.str(); } template <class T> string deb_tos(vector<set<T>> &a, ll H = inf, ll W = inf, int key = -1) { vector<vector<T>> b(sz(a)); rep(i, sz(a)) { fora(v, a[i]) { b[i].push_back(v); } } return deb_tos(b, H, W, key); } template <class T, size_t A> string deb_tos(T (&a)[A]) { return deb_tos(vector<T>(begin(a), end(a))); } template <class T, size_t A, size_t B> string deb_tos(T (&a)[A][B]) { return deb_tos(vector<vector<T>>(begin(a), end(a))); } template <class T, size_t A, size_t B, size_t C> string deb_tos(T (&a)[A][B][C]) { return deb_tos(vector<vector<vector<T>>>(begin(a), end(a))); } /*@formatter:off*/ template <class T> void out2(T head) { cout << head; res_mes += deb_tos(head); } template <class T, class... U> void out2(T head, U... tail) { cout << head << " "; res_mes += deb_tos(head) + " "; out2(tail...); } template <class T, class... U> void out(T head, U... tail) { cout << head << " "; res_mes += deb_tos(head) + " "; out2(tail...); cout << "" << endl; res_mes += "\n"; } template <class T> void out(T head) { cout << head << endl; res_mes += deb_tos(head) + "\n"; } void out() { cout << "" << endl; } #else #define err(...) ; template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #endif template <class T> void outl(const vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } // テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } /*@formatter:off*/ template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } template <class T> void na(set<T> &a, ll n) { rep(i, n) a.insert(in()); } #define dna(a, n) \ vi a; \ na(a, n); /*nを複数使うと n==in()の時バグる事に注意*/ #define dnad(a, n) \ vi a; \ nad(a, n); template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T> void nad(set<T> &a, ll n) { rep(i, n) a.insert(in() - 1); } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } template <class T, class U> void na2(set<T> &a, set<U> &b, ll n) { rep(i, n) { a.insert(in()); b.insert(in()); } } #define dna2(a, b, n) \ vi a, b; \ na2(a, b, n); template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } #define dna2d(a, b, n) \ vi a, b; \ na2d(a, b, n); template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a, b, c; \ na3(a, b, c, n); template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--; } #define dna3d(a, b, c, n) \ vi a, b, c; \ na3d(a, b, c, n); template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a, b, c, d; \ na4(a, b, c, d, n); #define dna4d(a, b, c, d, n) \ vi a, b, c, d; \ na4d(a, b, c, d, n); #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; #define dnt(S, h, w) \ vvi(S, h, w); \ nt(S, h, w); #define dntc(S, h, w) \ vvc(S, h, w); \ nt(S, h, w); #define dnts(S, h, w) \ vvs(S, h, w); \ nt(S, h, w); // デバッグ #define sp << " " << /*@formatter:off*/ #define deb1(x) debugName(x) << " = " << deb_tos(x) #define deb_2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__) #define deb_3(x, ...) deb1(x) << ", " << deb_2(__VA_ARGS__) #define deb_4(x, ...) deb1(x) << ", " << deb_3(__VA_ARGS__) #define deb5(x, ...) deb1(x) << ", " << deb_4(__VA_ARGS__) #define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__) // #define deb7(x, ...) deb1(x) <<", "<< deb6(__VA_ARGS__) // #define deb8(x, ...) deb1(x) <<", "<< deb7(__VA_ARGS__) // #define deb9(x, ...) deb1(x) <<", "<< deb8(__VA_ARGS__) // #define deb10(x, ...) deb1(x) <<", "<< deb9(__VA_ARGS__) /*@formatter:off*/ #ifdef _DEBUG bool was_deb = false; #define deb(...) \ do { \ was_deb = true; \ cerr << over10(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb_4, \ deb_3, deb_2, deb1)(__VA_ARGS__) \ << endl; \ } while (0) void print_n_base(int x, int base) { string S; while (x) { S += (char)('0' + x % base); x /= base; } cerr << S << endl; } template <class T> void print_n_base(vector<T> X, int base) { cerr << endl; for (auto &&x : X) { print_n_base(x, base); } cerr << endl; } // n進数 #define deb2(x) \ was_deb = true; \ cerr << debugName(x) << " = "; \ print_n_base(x, 2); #define deb3(x) \ was_deb = true; \ cerr << debugName(x) << " = "; \ print_n_base(x, 3); #define deb4(x) \ was_deb = true; \ cerr << debugName(x) << " = "; \ print_n_base(x, 4); #define deb_ex_deb(x, len) debugName(x) << " = " << deb_tos(x, len) #define call_deb_ex_deb(x, len) deb_ex_deb(x, len) // 要素が存在する行だけ出力(vvt) #define deb_ex(v) \ do { \ int N = sz(v); \ int s = N; \ int t = 0; \ rep(i, N) { \ if (sz(v[i])) { \ chmi(s, i); \ chma(t, i); \ } \ } \ auto ex_v = sub(v, s, N); \ str S = deb_tos(ex_v, sz(ex_v)); \ debugName(v); \ cerr << " = " << endl; \ cerr << S << endl; \ } while (0); #define debi(A) \ { \ int len = min(sz(A), 20); \ was_deb = true; \ cerr << debugName(A) << " = " << endl; \ rep(i, len) cerr << std::right \ << std::setw((int)(sz(tos(A[i])) + (i ? 1 : 0))) \ << (i % 10); \ cerr << endl; \ rep(i, len) cerr << std::right \ << std::setw((int)(sz(tos(A[i])) + (i ? 1 : 0))) << A[i]; \ cerr << endl; \ } template <class T, class F> string deb_tos_f(vector<vector<T>> &a, F f, int key = -1) { vi hs, ws_; int H = sz(a), W = sz(a[0]); vi exh(H), exw(W); rep(h, H) { rep(w, W) { if (f(a[h][w])) { exh[h] = true; exw[w] = true; } } } rep(h, H) if (exh[h]) hs.push_back(h); rep(w, W) if (exw[w]) ws_.push_back(w); return deb_tos(a, hs, ws_, key); } template <class T, class F> string deb_tos_f(vector<vector<vector<T>>> &a, F f) { stringstream ss; int H = sz(a); if (sz(a) == 0) return ss.str(); rep(i, H) { ss << deb_tos_f(a[i], f, i); } ss << "" << endl; return ss.str(); } #define debf_normal(tab, f) \ do { \ cerr << debugName(tab) << " = " << endl; \ cerr << deb_tos_f(tab, f) << endl; \ } while (0); #define debf2(tab, siki_r) debf_normal(tab, lamr(siki_r)) #define debf3(tab, v, siki) debf_normal(tab, lam(siki)) // S, sikir // S, v, siki #define debf(...) over3(__VA_ARGS__, debf3, debf2, debf1)(__VA_ARGS__) #else #define deb(...) ; #define deb2(...) ; #define deb3(...) ; #define deb4(...) ; #define deb_ex(...) ; #define debf(...) ; #define debi(...) ; #endif #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' /*@formatter:off*/ using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; using bint = __int128; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } __int128 to_bint(string &s) { __int128 ret = 0; for (ll i = 0; i < (ll)s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } void operator>>(istream &iss, bint &v) { string S; iss >> S; v = 0; rep(i, sz(S)) { v *= 10; v += S[i] - '0'; } } // 便利関数 /*@formatter:off*/ // テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } ll rand(ll max) { return rand(0, max); } template <class T> T rand(vector<T> &A) { return A[rand(sz(A) - 1)]; } // 重複することがある template <class T> vector<T> ranv(vector<T> &A, int N) { vector<T> ret(N); rep(i, N) { ret[i] = rand(A); } return ret; } template <class T> vector<T> ranv_unique(vector<T> &A, int N) { vector<T> ret(N); umapi was; rep(j, N) { int i; while (1) { i = rand(sz(A) - 1); if (was.find(i) == was.end()) break; } ret[j] = A[i]; was[i] = 1; } return ret; } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } /*@formatter:off*/ #ifdef _DEBUG bool timeup(int time) { static bool never = true; if (never) message += "may timeup, because slow"; never = false; auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } #else bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } #endif void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う // 全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:off*/ // #define use_rand #ifdef use_rand str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } // 単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } // 便利 汎用 #endif void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class Iterable, class T = typename Iterable::value_type> vector<pair<T, int>> run_length(const Iterable &a) { vector<pair<T, int>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } /*@formatter:off*/ // #define use_mgr //_goldd _goldt #ifdef use_mgr //->[i, f(i)] template <class T, class U, class F> auto mgr(T ok, U ng, const F &f, require_arg(is_integral<T>::value &&is_integral<U>::value)) { auto mid = (ok + ng); if (ok < ng) while (ng - ok > 1) { mid = (ok + ng) >> 1; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { mid = (ok + ng) >> 1; if (f(mid)) ok = mid; else ng = mid; } return ok; } //[l, r)の中で,f(i)がtrueとなる範囲を返す okはそこに含まれる template <class F> P mgr_range(int l, int r, F f, int ok) { if (f(ok) == 0) { out("f(ok) must true"); re(); } return mp(mgr(ok, l - 1, f), mgr(ok, r, f) + 1); } template <class F> auto mgrd(dou ok, dou ng, F f, int kai = 100) { if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } template <class F> dou mgrd_time(dou ok, dou ng, F f, int time = 1980) { bool han = true; if (ok < ng) while (1) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); if (timeup(time)) { break; } } else while (1) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); if (timeup(time)) { break; } } return ok; } // todo 減らす template <class F> auto goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX<ll>(); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return make_tuple(resIndex, calc(resIndex)); } template <class F> auto goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN<ll>(); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return make_tuple(resIndex, calc(resIndex)); } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> auto goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return make_tuple(left, calc(left)); } template <class F> auto goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return make_tuple(left, calc(left)); } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> auto goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX<ll>(); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return make_tuple(mini, calc(mini)); } template <class F> auto goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN<ll>(); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return make_tuple(maxi, calc(maxi)); } template <class F> auto goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX<dou>(); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return make_tuple(mini, calc(mini)); } template <class F> auto goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN<dou>(); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return make_tuple(maxi, calc(maxi)); } #endif // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } // エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } // getid(find())を返す 1次元にする template <typename W, typename T> int findi(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return i * sz(a[0]) + j; return -1; } template <typename W, typename U> tuple<int, int, int> find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return tuple<int, int, int>(i, j, k); return tuple<int, int, int>(-1, -1, -1); } // 無ければ-1 int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } // N箇所について右のkeyの場所を返す template <typename W, typename T> vi finds(const W &a, const T &key) { int n = sz(a); vi rpos(n, -1); rer(i, n - 1) { if (i < n - 1) { rpos[i] = rpos[i + 1]; } if (a[i] == key) rpos[i] = i; } return rpos; } template <typename W, typename T> vi rfinds(const W &a, const T &key) { int n = sz(a); vi lpos(n, -1); rep(i, n) { if (i > 0) { lpos[i] = lpos[i - 1]; } if (a[i] == key) lpos[i] = i; } return lpos; } // todoz #if __cplusplus >= 201703L template <typename W, typename T, class Iterable = typename W::value_type> ll count(const W &a, const T &k) { return count_if(a, == k); } template <typename W, class Iterable = typename W::value_type> vi count(const W &a) { vi res; for_each(a, v, if (sz(res) <= (int)v) res.resize((int)v + 1); res[v]++;); return res; } #endif ll count(const str &a, const str &k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } /*@formatter:off*/ //'a' = 'A' = 0 として集計 既に-'a'されていても動く vi count(str &a, int l, int r) { vi cou(26); char c = 'a'; if ('A' <= a[l] && a[l] <= 'Z') c = 'A'; if ('a' <= a[l] && a[l] <= 'z') c = 'a'; else c = 0; rep(i, l, r)++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> auto rev(const vector<T> &a) { auto b = a; reverse(ALL(b)); return b; } /* \反転 */ template <class U> auto rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; return b; } /* |反転 */ template <class U> auto revw(vector<vector<U>> &a) { vector<vector<U>> b(sz(a), vector<U>(sz(a[0]))); int W = sz(a[0]); rep(h, sz(a)) rep(w, sz(a[0])) { b[h][W - 1 - w] = a[h][w]; } return b; } /* ー反転 */ template <class U> auto revh(vector<vector<U>> &a) { vector<vector<U>> b(sz(a), vector<U>(sz(a[0]))); int H = sz(a); rep(h, sz(a)) rep(w, sz(a[0])) { b[H - 1 - h][w] = a[h][w]; } return b; } /* /反転 */ template <class U> auto revr(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); int H = sz(a); int W = sz(a[0]); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[H - 1 - h][W - 1 - w]; return b; } auto rev(const string &a) { string b = a; reverse(ALL(b)); return b; } template <class T> auto rev(const T &v, int i) { return v[sz(v) - 1 - i]; } int rev(int N, int i) { return N - 1 - i; } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; // 0は0桁 ll keta(ll v, int if_zero_res) { if (!v) return if_zero_res; if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } #if __cplusplus >= 201703L ll getr(ll a, ll keta) { return (a / pow<ll>(10, keta)) % 10; } #else ll getr(ll a, ll keta) { return (a / (int)pow(10, keta)) % 10; } #endif // 上から何桁目か ll getl(ll a, ll ket) { int sketa = keta(a, 1); return getr(a, sketa - 1 - ket); } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } // 変換系 template <class T, class U> auto to_v1(vector<reference_wrapper<U>> &ret, vector<T> &A) { rep(i, sz(A)) ret.push_back(A[i]); return ret; } template <class T, class U> auto to_v1(vector<reference_wrapper<U>> &ret, vector<vector<T>> &A) { rep(i, sz(A)) to_v1(ret, A[i]); return ret; } // 参照付きで1次元に起こす template <class T> auto to_v1(vector<vector<T>> &A) { vector<reference_wrapper<decl2_t<decltype(A)>>> ret; rep(i, sz(A)) to_v1(ret, A[i]); return ret; } //[v] := iとなるようなvectorを返す // 存在しない物は-1 // 空でも動く(なぜか) template <class T> auto keys(const T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(const T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } // todo 可変長で template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } // 1次元のコンテナ template <class T> int mini(const vector<T> &a) { return min_element(ALL(a)) - a.begin(); } template <class T> int maxi(const vector<T> &a) { return max_element(ALL(a)) - a.begin(); } template <class T> T sum(const T &A) { return A; } template <class T> T sum(const vector<T> &A, int l = -1, int r = -1) { T s = 0; set_lr12(l, r, sz(A)); rep(i, l, r) s += sum(A[i]); return s; } // 1次元のIterableに使える template <class T, require_t(!has_value_type<T>::value)> T min(T &a) { return a; } template <class V, require_t(has_value_type<V>::value)> auto min(V &a, ll s = -1, ll n = -1) { using Ret_t = decl2_t<V>; auto ret = MAX<Ret_t>(); set_lr12(s, n, sz(a)); if (s == n) { return INF<Ret_t>(); } else { rep(i, s, n) { ret = min(ret, min(a[i])); } return ret; } } template <class T, require_t(!has_value_type<T>::value)> T max(T &a) { return a; } template <class V, require_t(has_value_type<V>::value)> auto max(V &a, ll s = -1, ll n = -1) { using Ret_t = decl2_t<V>; auto ret = MIN<Ret_t>(); set_lr12(s, n, sz(a)); if (s == n) { return -INF<Ret_t>(); } else { rep(i, s, n) { ret = max(ret, max(a[i])); } return ret; } } template <class T> T mul(vector<T> &v, ll t = inf) { T ret = v[0]; rep(i, 1, min(t, sz(v))) ret *= v[i]; return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret // += sum(v[i], tail...); return ret;} indexを持つvectorを返す /*@formatter:off*/ template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<W> &xx, const T v, int s, ll t) { rep(i, s, t) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, T v, int sh, int th, int sw, int tw) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } // #define use_fill //_sum _array _max _min #ifdef use_fill template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N) { fill(a[i], v); } } template <class T, class U> void fill(vector<T> &a, const vi &ind, U val) { fora(v, ind) { a[v] = val; } } template <typename A, size_t N> auto sum(A (&a)[N], int l = -1, int r = -1) { set_lr12(l, r, N); auto res = sum(a[l]); if (l == r) { return res - res; } rep(i, l + 1, r) res += sum(a[i]); return res; } template <typename A, size_t N> auto max(A (&a)[N], int l = -1, int r = -1) { set_lr12(l, r, N); auto res = max(a[l]); if (l == r) { return -INF<decltype(res)>(); } rep(i, l + 1, r) res = max(res, max(a[i])); return res; } template <typename A, size_t N> auto min(A (&a)[N], int l = -1, int r = -1) { set_lr12(l, r, N); auto res = min(a[l]); if (l == r) { return INF<decltype(res)>(); } rep(i, l + 1, r) res = min(res, min(a[i])); return res; } #endif vi inds_(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { assert(ret[a[i]] == -1); ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } // template<class T> T *negarr(ll size) { T *body = (T *) malloc((size * 2 + // 1) * sizeof(T)); return body + size;} template<class T> T *negarr2(ll h, // ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new // double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = // 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double // **a = dummy1 + h; return a;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r = inf) { return operator()(0, min(r, sz(rui) - 1)); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> string deb_tos(const ruiC<T> &a) { return deb_tos(a.rui); } template <class T> ostream &operator<<(ostream &os, ruiC<T> a) { fora(v, a.rui) { os << v << " "; } return os; } template <class T> vector<T> ruiv(const vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i]; return ret; } template <class T> ruiC<T> ruic(const vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } template <class T> ruiC<T> ruic() { return ruiC<T>(); } // imoは0-indexed // ruiは1-indexed template <class T> vector<T> imo(const vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } // #define use_rui //_imo _ruic _ruiv #ifdef use_rui // kと同じものの数 template <class T, class U> vi imo(const vector<T> &a, U k) { vi equ(sz(a)); rep(i, sz(a)) { equ[i] = a[i] == k; } return imo(equ); } template <class T> vector<T> imox(const vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i]; return ret; } // 漸化的に最小を持つ template <class T> vector<T> imi(const vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> ima(const vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> rimi(const vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chmi(ret[i - 1], ret[i]); return ret; } template <class T> vector<T> rima(const vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chma(ret[i - 1], ret[i]); return ret; } template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN<T>(), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<int> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; T ma = MIN<T>(); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -INF<T>(); ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T getv(int r = inf) { return getv(0, min(r, n)); }; int geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } int geti(int r = inf) { return geti(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX<T>(), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<int> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; T mi = MAX<T>(); rv.resize(n + 1); ri.resize(n + 1); rv[0] = INF<T>(); ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T getv(int r = inf) { return getv(0, min(r, n)); }; int geti(int l, int r) { { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } int geti(int r = inf) { return geti(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; /*@formatter:off*/ vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return ret; } // kと同じものの数 template <class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k); return ret; } template <class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret); } template <class T> struct ruiC2 { int H; vector<ruiC<T>> rui; ruiC<T> dummy; // 変なのをよばれたときはこれを返す//todo ruiC2(const vector<vector<T>> &ru) : rui(sz(ru)), H(sz(ru)) { for (int h = 0; h < H; h++) { if (sz(ru[h]) == 0) continue; if (sz(dummy) == 1) dummy = ruic(vector<T>(sz(ru[h]))); rui[h] = ruic(ru[h]); } } // WについてHを返す vector<T> operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } vector<T> res(H); for (int h = 0; h < H; h++) res[h] = rui[h](l, r); return res; } // HについてWを返す ruiC<T> &operator[](ll h) { #ifdef _DEBUG if (h >= H) { message += "warning ruiC h >= H"; } #endif if (h >= H || sz(rui[h]) == 1) return dummy; else return rui[h]; } /*@formatter:off*/ // vector<T> operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } /*0から順に追加される必要がある*/ // T back() { return rui.back(); } // ll size() { return rui.size(); } // auto begin(){return rui.begin();} // auto end(){return rui.end();} }; template <class T, class U> ruiC<ll> ruicou(vector<T> &a, U b) { vi cou(sz(a)); rep(i, sz(a)) { cou[i] = a[i] == b; } return ruic(cou); } // メモリは形式によらず(26*N) // rui(l,r)でvector(26文字について, l~rのcの個数) // rui[h] ruic()を返す // 添え字は'a', 'A'のまま扱う // (予め-='a','A'されているものが渡されたらそれに従う) template <typename Iterable, class is_Iterable = typename Iterable::value_type> ruiC2<ll> ruicou(const Iterable &a) { int H = max(a) + 1; vvi(cou, H); rep(i, sz(a)) { if (sz(cou[a[i]]) == 0) cou[a[i]].resize(sz(a)); cou[a[i]][i] = 1; } return ruiC2<ll>(cou); } /*@formatter:off*/ // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { vector<T> rui; ruixC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruiXc "; deb(l, r); assert(0); } return rui[r] ^ rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i]; return ret; } template <class T> ruixC<ll> ruixc(vector<T> &a) { vi ret = ruix(a); return ruixC<ll>(ret); } // 差分を返す(累積を取ると元に戻る) // 101なら // 1111を返す // 元の配列で[l, r)へのxorは //[l]と[r]へのxorになる https://atcoder.jp/contests/abc155/tasks/abc155_f vi ruix_diff(vi &A) { int N = sz(A); assert(N); vi res(N + 1); res[0] = A[0]; rep(i, 1, N) { res[i] = A[i - 1] ^ A[i]; } res[N] = A[N - 1]; return res; } template <class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size()) res[i + 1] = res[i] * a[i]; return res; } // 漸化的に最小を1indexで持つ template <class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) { os << v << " "; } return os; } template <class T> string deb_tos(rruiC<T> &a) { return deb_tos(a.rui); } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } // 掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } template <class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1) res[i - 1] = res[i] * a[i]; return res; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #endif constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); // オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } #if __cplusplus >= 201703L ll bget(ll m, ll keta, ll sinsuu) { m /= pow<ll>(sinsuu, keta); return m % sinsuu; } #else ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } #endif constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); // オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } #if __cplusplus >= 201703L ll bit(ll n, ll sinsuu) { return pow<ll>(sinsuu, n); } #else ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } #endif ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll bit(const vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 // bitsetに置き換える感覚 i が立っていたら i bit目を立てる ll bit_bool(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll // 最下位ビット ll lbit(ll n) { assert(n); return n & -n; } ll lbiti(ll n) { assert(n); return log2(n & -n); } // 最上位ビット ll hbit(ll n) { assert(n); n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { assert(n); return log2(hbit(n)); } // ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll // b = 1ll << a; if (b <= n)k += 1ll << i; } return k;} // 初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } /*over*/ #define forbit1_2(i, mas) \ for (int forbitj = !mas ? 0 : lbit(mas), forbitm = mas, \ i = !mas ? 0 : log2(forbitj); \ forbitm; forbitm = forbitm ^ forbitj, \ forbitj = !forbitm ? 1 : lbit(forbitm), i = log2(forbitj)) /*over*/ #define forbit1_3(i, N, mas) \ for (int forbitj = !mas ? 0 : lbit(mas), forbitm = mas, \ i = !mas ? 0 : log2(forbitj); \ forbitm && i < N; forbitm = forbitm ^ forbitj, \ forbitj = !forbitm ? 1 : lbit(forbitm), i = log2(forbitj)) // masの立ってるindexを見る // i, [N], mas #define forbit1(...) over3(__VA_ARGS__, forbit1_3, forbit1_2)(__VA_ARGS__) // masが立っていないindexを見る // i, N, mas #define forbit0(i, N, mas) forbit1(i, mask(N) & (~(mas))) // forsubをスニペットして使う // Mの部分集合(0,M含む)を見る 3^sz(S)個ある #define forsub_all(m, M) for (int m = M; m != -1; m = m == 0 ? -1 : (m - 1) & M) // BASE進数 template <size_t BASE> class base_num { int v; public: base_num(int v = 0) : v(v){}; int operator[](int i) { return bget(v, i, BASE); } void operator++() { v++; } void operator++(signed) { v++; } operator int() { return v; } }; #define base3(mas, lim, BASE) for (base_num<BASE> mas; mas < lim; mas++) #define base2(mas, lim) base3(mas, lim, 2) #define base(...) over3(__VA_ARGS__, base3, base2, base1)(__VA_ARGS__) // aにある物をtrueとする vb bool_(vi a, int n) { vb ret(max(max(a) + 1, n)); rep(i, sz(a)) ret[a[i]] = true; return ret; } char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } res = rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } // 基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } // #define use_compress //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(ALL(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } #ifdef use_compress // ind[i] := i番目に小さい数 // map[v] := vは何番目に小さいか vi compress(vi &a, umapi &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(ALL(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) { b.push_back(v); } fora(v, r) { b.push_back(v); } sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(ALL(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(ALL(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) { b.push_back(v); } fora(v, r) { b.push_back(v); } fora(v, s) { b.push_back(v); } sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(ALL(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(ALL(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(ALL(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) { fora(v, vv) { b.push_back(v); } } sort(b); unique(b); fora(vv, a) { fora(v, vv) { v = lower_bound(ALL(b), v) - b.begin(); } } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) { fora(vv, vvv) { fora(v, vv) { b.push_back(v); } } } sort(b); unique(b); fora(vvv, a) { fora(vv, vvv) { fora(v, vv) { v = lower_bound(ALL(b), v) - b.begin(); } } } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(ALL(b), a[i]) - b.begin(); } } #endif // 要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(ALL(a), v)) #define lowerIndex(a, v) (lower_bound(ALL(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(ALL(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(ALL(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(ALL(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } // todo 消せないか template <class T, class U, class W> T lowerBound(set<T> &a, U v, W banpei) { auto it = a.lower_bound(v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(set<T> &a, U v, W banpei) { auto it = a.upper_bound(v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T rlowerBound(set<T> &a, U v, W banpei) { auto it = a.upper_bound(v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(set<T> &a, U v, W banpei) { auto it = a.lower_bound(v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T lowerBound(mset<T> &a, U v, W banpei) { auto it = a.lower_bound(v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(mset<T> &a, U v, W banpei) { auto it = a.upper_bound(v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T rlowerBound(mset<T> &a, U v, W banpei) { auto it = a.upper_bound(v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(mset<T> &a, U v, W banpei) { auto it = a.lower_bound(v); if (it == a.begin()) return banpei; else { return *(--it); } } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) // 狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX<T>()); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX<T>()); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX<T>()); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX<T>()); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } // 存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } // 便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } // ll pow(ll a) { return a * a; }; template <class T> T fact(int v) { static vector<T> fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.emplace_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } // 二項係数の偶奇を返す int com_mod2(int n, int r) { return n == (r | (n - r)); } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } #define gcd my_gcd ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } #define lcm my_lcm ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } #define hypot my_hypot double hypot(double dx, double dy) { return std::sqrt(dx * dx + dy * dy); } ll sig0(int t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } bint sig0(bint t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } // 第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // b * res <= aを満たす [l, r)を返す div P drange_ika(int a, int b) { P null_p = mp(linf, linf); if (b == 0) { if (a >= 0) { return mp(-linf, linf + 1) /*全て*/; } else { return null_p /*無い*/; } } else { if (a >= 0) { if (b > 0) { return mp(-linf, a / b + 1); } else { return mp(-(a / -b), linf + 1); } } else { if (b > 0) { return mp(-linf, -ceil(-a, b) + 1); } else { return mp(ceil(-a, -b), linf + 1); } } } } // v * v >= aとなる最小のvを返す ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll)std::sqrt(a); while (res * res < a) ++res; return res; } double log(double e, double x) { return log(x) / log(e); } /*@formatter:off*/ // 機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } // todo stringもセットで template <class T> vector<T> sub(const vector<T> &A, int l, int r) { assert(0 <= l && l <= r && r <= sz(A)); vector<T> ret(r - l); std::copy(A.begin() + l, A.begin() + r, ret.begin()); return ret; } template <class T> vector<T> sub(const vector<T> &A, int r) { return sub(A, 0, r); } template <class T> vector<T> subn(const vector<T> &A, int l, int len) { return sub(A, l, l + len); } string sub(string &A, int l, int r) { assert(0 <= l && l <= r && r <= sz(A)); return A.substr(l, r - l); } template <class T, class F> // sub2で呼ぶ vector<T> sub(const vector<vector<T>> &A, int h, int w, int ah, int aw, F f) { vector<T> res; while (0 <= h && h < sz(A) && 0 <= w && w < sz(A[h]) && f(A[h][w])) { res.emplace_back(A[h][w]); h += ah; w += aw; } return res; } template <class T> vector<T> sub(const vector<vector<T>> &A, int h, int w, int ah, int aw) { return sub(A, h, w, ah, aw, [&](T v) { return true; }); } // range_nowを返す(find_ifでしか使われない) #if __cplusplus >= 201703L template <class T> auto subr(const vector<T> &A, int l) { return range_now(vector<T>(A.begin() + l, A.end()), l); } #endif #define sub25(A, h, w, ah, aw) sub(A, h, w, ah, aw) #define sub26(A, h, w, ah, aw, siki_r) \ sub(A, h, w, ah, aw, [&](auto v) { return v siki_r; }) #define sub27(A, h, w, ah, aw, v, siki) \ sub(A, h, w, ah, aw, [&](auto v) { return siki; }) #define sub2(...) over7(__VA_ARGS__, sub27, sub26, sub25)(__VA_ARGS__) constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> int count(set<T> &S, T l, T r) { assert(l < r); auto it = S.lower_bound(l); return it != S.end() && (*it) < r; } // template<class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) // {assert(sz(S) == sz(v));assert(w < sz(S[0]));rep(h, sz(S)) { S[h][w] = v[h]; // }} template <class T> vector<T> geth(vector<vector<T>> &S, int w) { assert(w < sz(S[0])); vector<T> ret(sz(S)); rep(h, sz(S)) { ret[h] = S[h][w]; } return ret; } // vector<bool>[i]は参照を返さないため、こうしないとvb[i] |= // trueがコンパイルエラー vb::reference operator|=(vb::reference a, bool b) { return a = a | b; } vb::reference operator&=(vb::reference a, bool b) { return a = a & b; } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(const pair<T, U> &a, const pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <class T, class U> pair<T, U> operator-(const pair<T, U> &a, const pair<T, U> &b) { return pair<T, U>(a.fi - b.fi, a.se - b.se); } template <class T, class U> pair<T, U> operator-(const pair<T, U> &a) { return pair<T, U>(-a.first, -a.second); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { #ifdef _DEBUG static bool was = false; if (!was) message += "str += 65 is 'A' not \"65\" "; was = true; #endif return lhs + (char)rv; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs = lhs + rv; } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { const int rv2 = rv; return lhs + rv2; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { const int v = rv; lhs += v; } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) { a.push(d); } return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) { a.insert(d); } return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(const vector<T> &a, const vector<T> &b) { vector<T> ret; ret = a; fora(v, b) { ret += v; } return ret; } template <class T> vector<T> &operator+=(vector<T> &a, const vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { for (auto &&bv : b) { a[bv.first] += bv.second; } return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, const U &v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> auto operator+=(uset<T> &a, U v) { return a.insert(v); } template <class T> vector<T> operator%(vector<T> &a, int v) { vi ret(sz(a)); rep(i, sz(a)) { ret[i] = a[i] % v; } return ret; } template <class T> vector<T> operator%=(vector<T> &a, int v) { rep(i, sz(a)) { a[i] %= v; } return a; } vi operator&(vi &a, vi &b) { assert(sz(a) == sz(b)); vi ret(sz(a)); rep(i, sz(a)) { ret[i] = min(a[i], b[i]); } return ret; } template <class T> void operator+=(mset<T> &a, vector<T> &v) { for (auto &&u : v) a.insert(u); } template <class T> void operator+=(set<T> &a, vector<T> &v) { for (auto &&u : v) a.insert(u); } template <class T> void operator+=(vector<T> &a, set<T> &v) { for (auto &&u : v) a.emplace_back(u); } template <class T> void operator+=(vector<T> &a, mset<T> &v) { for (auto &&u : v) a.emplace_back(u); } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } // template<class T, class U> void operator*=(vector<T> &a, U b) { vector<T> // ta = a; rep(b-1){ a+=ta; }} template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <typename T> void pop_front(vector<T> &v) { erase(v, 0); } template <typename T> void entry(vector<T> &v, unsigned ll s, unsigned ll e) { erase(v, e, sz(v)); erase(v, 0, s); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } vector<string> split(const string &a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string &a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } ll stoi(string &s) { return stol(s); } #define assert_yn(yn_v, v) \ ; \ assert(yn_v == 0 || yn_v == v); \ yn_v = v; // 不完全な対策、現状はautohotkeyで対応 int yn_v = 0; void yn(bool a) { assert_yn(yn_v, 1); if (a) cout << "yes" << endl; else cout << "no" << endl; } void fyn(bool a) { assert_yn(yn_v, 1); yn(a); exit(0); } void Yn(bool a) { assert_yn(yn_v, 2); if (a) cout << "Yes" << endl; else cout << "No" << endl; } void fYn(bool a) { assert_yn(yn_v, 2); Yn(a); exit(0); } void YN(bool a) { assert_yn(yn_v, 3); if (a) cout << "YES" << endl; else cout << "NO" << endl; } void fYN(bool a) { assert_yn(yn_v, 3); YN(a); exit(0); } int ab_v = 0; void fAb(bool a) { assert_yn(ab_v, 1); if (a) cout << "Alice" << endl; else cout << "Bob"; } void fAB(bool a) { assert_yn(yn_v, 2); if (a) cout << "ALICE" << endl; else cout << "BOB"; } int pos_v = 0; void Possible(bool a) { assert_yn(pos_v, 1); if (a) cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { assert_yn(pos_v, 2); if (a) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } void fPossible(bool a) { assert_yn(pos_v, 1) Possible(a); exit(0); } void fPOSSIBLE(bool a) { assert_yn(pos_v, 2) POSSIBLE(a); exit(0); } template <typename T> class fixed_point : T { public: explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return T::operator()(*this, std::forward<Args>(args)...); } }; template <typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point<T>{std::forward<T>(t)}; } // 未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename Iterable> vector<Iterable> table(const Iterable &a, int tou = 2) { int N = sz(a); vector<Iterable> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } // 長さn毎に分割する template <typename Iterable> vector<Iterable> table_n(const Iterable &a, int len) { int N = sz(a); vector<Iterable> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } // 縦を返す vi &geth(vvi() & a, int w) { static vi ret; ret.resize(sz(a)); rep(i, sz(a)) { ret[i] = a[i][w]; } return ret; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; // #define pre prev // #define nex next // gra mll pr // 上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] vc atoz = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; vc AtoZ = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; // 右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) int mei_inc8(int h, int w, int H, int W, int i) { while (++i < 8) { if (inside(h + h8[i], w + w8[i], H, W)) return i; } return i; } #define mei8(nh, nw, h, w) \ for (int i = mei_inc8(h, w, H, W, -1), nh = i < 8 ? h + h8[i] : 0, \ nw = i < 8 ? w + w8[i] : 0; \ i < 8; i = mei_inc8(h, w, H, W, i), nh = h + h8[i], nw = w + w8[i]) int mei_incv(int h, int w, int H, int W, int i, vp &p) { while (++i < sz(p)) { if (inside(h + p[i].fi, w + p[i].se, H, W)) return i; } return i; } #define meiv(nh, nw, h, w, p) \ for (int i = mei_incv(h, w, H, W, -1, p), nh = i < sz(p) ? h + p[i].fi : 0, \ nw = i < sz(p) ? w + p[i].se : 0; \ i < sz(p); \ i = mei_incv(h, w, H, W, i, p), nh = h + p[i].fi, nw = w + p[i].se) // H*Wのグリッドを斜めに分割する // 右上 vector<vp> naname_list_ne(int H, int W) { vector<vp> res(H + W - 1); rep(sh, H) { int sw = 0; res[sh] += mp(sh, sw); int nh = sh; int nw = sw; while (1) { nh--; nw++; if (0 <= nh && nw < W) { res[sh] += mp(nh, nw); } else { break; } } } rep(sw, 1, W) { int sh = H - 1; res[H + sw - 1] += mp(sh, sw); int nh = sh; int nw = sw; while (1) { nh--; nw++; if (0 <= nh && nw < W) { res[H + sw - 1] += mp(nh, nw); } else { break; } } } return res; } // 右下 vector<vp> naname_list_se(int H, int W) { vector<vp> res(H + W - 1); rep(sh, H) { int sw = 0; res[sh] += mp(sh, sw); int nh = sh; int nw = sw; while (1) { nh++; nw++; if (0 <= nh && nh < H && nw < W) { res[sh] += mp(nh, nw); } else { break; } } } rep(sw, 1, W) { int sh = 0; res[H + sw - 1] += mp(sh, sw); int nh = sh; int nw = sw; while (1) { nh++; nw++; if (0 <= nh && nh < H && nw < W) { res[H + sw - 1] += mp(nh, nw); } else { break; } } } return res; } // グラフ内で #undef getid // #define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) ((h) * (W) + (w)) #define getid_1(p) ((p).first * W + (p).second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) #define set_shuffle() \ std::random_device seed_gen; \ std::mt19937 engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), engine); // 1980 開始からtime ms経っていたらtrue vb bit_bool(int v, int len) { assert(bit(len) > v); vb ret(len); rep(i, len) { ret[i] = bget(v, i); } return ret; } vi range(int l, int r) { vi ret; ret.resize(r - l); rep(v, l, r) { ret[v - l] = v; } return ret; } vi range(int r) { return range(0, r); } vi tov(vb &a) { vi ret; rep(i, sz(a)) { if (a[i]) ret.push_back(i); } return ret; } bool kaibun(const str &S) { return S == rev(S); } template <class T> vector<T> repeat(const vector<T> &A, int kaisu) { vector<T> ret; while (kaisu--) { ret += A; } return ret; } #define rge range #define upd update // S[{s, t, d}] #define strs slice_str struct slice_str { string S; slice_str() {} slice_str(const string &S) : S(S) {} slice_str(int len, char c) : S(len, c) {} auto size() { return S.size(); } char &operator[](int p) { return S[p]; } string operator[](initializer_list<int> p) { if (sz(p) == 1) { return S.substr(0, *(p.begin())); } else if (sz(p) == 2) { int l = *(p.begin()); int r = *(next(p.begin())); return S.substr(l, r - l); } else { auto it = p.begin(); int s = *(it++); int t = *(it++); int d = *(it); if (d == -1) { int s_ = sz(S) - s - 1; int t_ = sz(S) - t - 1; return rev(S).substr(s_, t_ - s_); } else if (d < 0) { t = max(-1ll, t); string ret; while (s > t) { ret += S[s]; s += d; } return ret; } else { t = min(sz(S), t); string ret; while (s < t) { ret += S[s]; s += d; } return ret; } } } operator string &() { return S; } template <class T> void operator+=(const T &a) { S += a; } bool operator==(const slice_str &rhs) { return S == rhs.S; } }; ostream &operator<<(ostream &os, const slice_str &a) { os << a.S; return os; } istream &operator>>(istream &iss, const slice_str &a) { iss >> a.S; return iss; } template <class T> bool can(const T &v, int i) { return 0 <= i && i < sz(v); } #if __cplusplus >= 201703L // template<class T> auto sum(int a, T v...) {return (v + ... + 0);} #endif #define VEC vector #endif /*UNTITLED15_TEMPLATE_H*/ #endif // † ←template終了 /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; #ifndef MINT //@formatter:off template <typename T> T minv(T a, T m); template <typename T> T minv(T a); template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(signed) { Modular result(*this); *this += 1; return result; } Modular operator--(signed) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); operator int() { return value; } private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } constexpr signed MOD = 998244353; // 1e9 + 7;//MOD using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>; constexpr int mint_len = 1400001; vi fac, finv, inv; vi p2; mint com(int n, int r) { if (r < 0 || r > n) return 0; /*nが大きくてrが小さい場合、nを上からr個掛ける*/ if (n >= mint_len) { int fa = finv[r]; rep(i, r) fa *= n - i, fa %= MOD; return mint(fa); } return mint(finv[r] * fac[n] % MOD * finv[n - r]); } mint pom(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } mint npr(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } int nprin(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return fac[n] * finv[n - r] % MOD; } int icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD; } #define ncr com #define ncri icom // n個の場所にr個の物を置く mint nhr(int n, int r) { if (n == 0 && r == 0) return 1; else return com(n + r - 1, r); } mint hom(int n, int r) { if (n == 0 && r == 0) return 1; else return com(n + r - 1, r); } int nhri(int n, int r) { if (n == 0 && r == 0) return 1; else return icom(n + r - 1, r); } // グリッドで0-indexedの最短経路 pascal mint pas(int h, int w) { return com(h + w, w); } template <typename T> T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> T minv(T a) { if (a < mint_len) return inv[a]; T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) { assert(b >= 0); int x = a(), res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } //-k乗出来る template <typename T, typename U> mint mpow(const T a, const U b) { /* assert(b >= 0);*/ if (b < 0) { return minv(mpow(a, -b)); } int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } #ifdef _DEBUG void yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint)i / j == a) { st << i << " / " << j; i = 2000; break; } } } string val = st.str(); if (val != "") { deb(val); return; } rep(i, 1000) { rep(j, 1000) { if ((mint)i / j == a) { st << i << " / " << j; i = 2000; break; } } } val = st.str(); deb(val); } #else #define yuri(...) ; #endif template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using PM = pair<mint, mint>; using vm = vector<mint>; using mapm = map<int, mint>; // using umapm = umap<int, mint>; #define vvm(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(mint, __VA_ARGS__) #define vnm(name, ...) auto name = make_v<mint>(__VA_ARGS__) string out_m2(mint a) { stringstream st; st << (int)a; rep(i, 300) { rep(j, 2, 300) { if ((i % j) && (mint)i / j == a) { st << "(" << i << "/" << j << ")"; i = 2000; break; } } } return st.str(); } struct setmod { setmod() { p2.resize(mint_len); p2[0] = 1; for (int i = 1; i < mint_len; ++i) p2[i] = p2[i - 1] * 2 % MOD; fac.resize(mint_len); finv.resize(mint_len); inv.resize(mint_len); inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i < mint_len; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i < mint_len; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } } setmodv; //@formatter:on // nhr n個の場所にr個の物を分ける mint m1 = (mint)1; mint half = (mint)1 / 2; #endif /*@formatter:off*/ namespace FastFourierTransform { using real = sig_dou; struct C { real x, y; C() : x(0), y(0) {} C(real x, real y) : x(x), y(y) {} inline C operator+(const C &c) const { return C(x + c.x, y + c.y); } inline C operator-(const C &c) const { return C(x - c.x, y - c.y); } inline C operator*(const C &c) const { return C(x * c.x - y * c.y, x * c.y + y * c.x); } inline C conj() const { return C(x, -y); } }; const real PI = acosl(-1); signed base = 1; vector<C> rts = {{0, 0}, {1, 0}}; vector<signed> rev = {0, 1}; void ensure_base(signed nbase) { if (nbase <= base) return; rev.resize(1 << nbase); rts.resize(1 << nbase); for (signed i = 0; i < (1 << nbase); i++) { rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1)); } while (base < nbase) { real angle = PI * 2.0 / (1 << (base + 1)); for (signed i = 1 << (base - 1); i < (1 << base); i++) { rts[i << 1] = rts[i]; real angle_i = angle * (2 * i + 1 - (1 << base)); rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i)); } ++base; } } void fft(vector<C> &a, signed n) { assert((n & (n - 1)) == 0); signed zeros = __builtin_ctz(n); ensure_base(zeros); signed shift = base - zeros; for (signed i = 0; i < n; i++) { if (i < (rev[i] >> shift)) { swap(a[i], a[rev[i] >> shift]); } } for (signed k = 1; k < n; k <<= 1) { for (signed i = 0; i < n; i += 2 * k) { for (signed j = 0; j < k; j++) { C z = a[i + j + k] * rts[j + k]; a[i + j + k] = a[i + j] - z; a[i + j] = a[i + j] + z; } } } } vector<int64_t> multiply(const vector<signed> &a, const vector<signed> &b) { signed need = (signed)a.size() + (signed)b.size() - 1; signed nbase = 1; while ((1 << nbase) < need) nbase++; ensure_base(nbase); signed sz = 1 << nbase; vector<C> fa(sz); for (signed i = 0; i < sz; i++) { signed x = (i < (signed)a.size() ? a[i] : 0); signed y = (i < (signed)b.size() ? b[i] : 0); fa[i] = C(x, y); } fft(fa, sz); C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0); for (signed i = 0; i <= (sz >> 1); i++) { signed j = (sz - i) & (sz - 1); C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r; fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r; fa[i] = z; } for (signed i = 0; i < (sz >> 1); i++) { C A0 = (fa[i] + fa[i + (sz >> 1)]) * t; C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i]; fa[i] = A0 + A1 * s; } fft(fa, sz >> 1); vector<int64_t> ret(need); for (signed i = 0; i < need; i++) { ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x); } return ret; } }; // namespace FastFourierTransform template <typename T> struct ArbitraryModConvolution { using real = FastFourierTransform::real; using C = FastFourierTransform::C; ArbitraryModConvolution() = default; vector<T> multiply(const vector<T> &a, const vector<T> &b, signed need = -1) { if (need == -1) need = a.size() + b.size() - 1; signed nbase = 0; while ((1 << nbase) < need) nbase++; FastFourierTransform::ensure_base(nbase); signed sz = 1 << nbase; vector<C> fa(sz); for (signed i = 0; i < a.size(); i++) { fa[i] = C((signed)a[i] & ((1 << 15) - 1), (signed)a[i] >> 15); } fft(fa, sz); vector<C> fb(sz); if (a == b) { fb = fa; } else { for (signed i = 0; i < b.size(); i++) { fb[i] = C((signed)b[i] & ((1 << 15) - 1), (signed)b[i] >> 15); } fft(fb, sz); } real ratio = 0.25 / sz; C r2(0, -1), r3(ratio, 0), r4(0, -ratio), r5(0, 1); for (signed i = 0; i <= (sz >> 1); i++) { signed j = (sz - i) & (sz - 1); C a1 = (fa[i] + fa[j].conj()); C a2 = (fa[i] - fa[j].conj()) * r2; C b1 = (fb[i] + fb[j].conj()) * r3; C b2 = (fb[i] - fb[j].conj()) * r4; if (i != j) { C c1 = (fa[j] + fa[i].conj()); C c2 = (fa[j] - fa[i].conj()) * r2; C d1 = (fb[j] + fb[i].conj()) * r3; C d2 = (fb[j] - fb[i].conj()) * r4; fa[i] = c1 * d1 + c2 * d2 * r5; fb[i] = c1 * d2 + c2 * d1; } fa[j] = a1 * b1 + a2 * b2 * r5; fb[j] = a1 * b2 + a2 * b1; } fft(fa, sz); fft(fb, sz); vector<T> ret(need); for (signed i = 0; i < need; i++) { int64_t aa = llround(fa[i].x); int64_t bb = llround(fb[i].x); int64_t cc = llround(fa[i].y); aa = T(aa), bb = T(bb), cc = T(cc); ret[i] = aa + (bb << 15) + (cc << 30); } return ret; } }; template <typename T> struct FormalPowerSeries : vector<T> { using vector<T>::vector; using P = FormalPowerSeries; using MULT = function<P(P, P)>; static MULT &get_mult() { static MULT mult = nullptr; return mult; } static void set_fft(MULT f) { get_mult() = f; } template <typename E> FormalPowerSeries(const vector<E> &x) : vector<T>(begin(x), end(x)) {} void shrink() { while (this->size() && this->back() == T(0)) this->pop_back(); } P operator+(const P &r) const { return P(*this) += r; } P operator+(const T &v) const { return P(*this) += v; } P operator-(const P &r) const { return P(*this) -= r; } P operator-(const T &v) const { return P(*this) -= v; } P operator*(const P &r) const { return P(*this) *= r; } P operator*(const T &v) const { return P(*this) *= v; } P operator/(const P &r) const { return P(*this) /= r; } P operator%(const P &r) const { return P(*this) %= r; } P &operator+=(const P &r) { if (r.size() > this->size()) this->resize(r.size()); for (int i = 0; i < r.size(); i++) (*this)[i] += r[i]; return *this; } P &operator+=(const T &r) { if (this->empty()) this->resize(1); (*this)[0] += r; return *this; } P &operator-=(const P &r) { if (r.size() > this->size()) this->resize(r.size()); for (int i = 0; i < r.size(); i++) (*this)[i] -= r[i]; shrink(); return *this; } P &operator-=(const T &r) { if (this->empty()) this->resize(1); (*this)[0] -= r; shrink(); return *this; } P &operator*=(const T &v) { const int n = (int)this->size(); for (int k = 0; k < n; k++) (*this)[k] *= v; return *this; } P &operator*=(const P &r) { if (this->empty() || r.empty()) { this->clear(); return *this; } assert(get_mult() != nullptr); return *this = get_mult()(*this, r); } P &operator%=(const P &r) { return *this -= *this / r * r; } P operator-() const { P ret(this->size()); for (int i = 0; i < this->size(); i++) ret[i] = -(*this)[i]; return ret; } // 謎が多い あまり使いたくない P &operator/=(P r) { while ((*this).back() == T(0)) (*this).pop_back(); while (r.back() == T(0)) r.pop_back(); if (this->size() < r.size()) { this->clear(); return *this; } int n = this->size() - r.size() + 1; return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev(n); } P pre(int sz) const { return P(begin(*this), begin(*this) + min((int)this->size(), sz)); } P operator>>(int sz) const { if (this->size() <= sz) return {}; P ret(*this); ret.erase(ret.begin(), ret.begin() + sz); return ret; } P operator<<(int sz) const { P ret(*this); ret.insert(ret.begin(), sz, T(0)); return ret; } P rev(int deg = -1) const { P ret(*this); if (deg != -1) ret.resize(deg, T(0)); reverse(begin(ret), end(ret)); return ret; } P diff() const { const int n = (int)this->size(); P ret(max(0ll, n - 1)); for (int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * T(i); return ret; } P integral() const { const int n = (int)this->size(); P ret(n + 1); ret[0] = T(0); for (int i = 0; i < n; i++) ret[i + 1] = (*this)[i] / T(i + 1); return ret; } // F(0) must not be 0 P inv(int deg = -1) const { assert(((*this)[0]) != T(0)); const int n = (int)this->size(); if (deg == -1) deg = n; P ret({T(1) / (*this)[0]}); for (int i = 1; i < deg; i <<= 1) { ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1); } return ret.pre(deg); } // F(0) must be 1 P log(int deg = -1) const { assert((*this)[0] == T(1)); const int n = (int)this->size(); if (deg == -1) deg = n; return (this->diff() * this->inv(deg)).pre(deg - 1).integral(); } P sqrt(int deg = -1) const { const int n = (int)this->size(); if (deg == -1) deg = n; if ((*this)[0] == T(0)) { for (int i = 1; i < n; i++) { if ((*this)[i] != T(0)) { if (i & 1) return {}; if (deg - i / 2 <= 0) break; auto ret = (*this >> i).sqrt(deg - i / 2) << (i / 2); if (ret.size() < deg) ret.resize(deg, T(0)); return ret; } } return P(deg, 0); } P ret({T(1)}); T inv2 = T(1) / T(2); for (int i = 1; i < deg; i <<= 1) { ret = (ret + pre(i << 1) * ret.inv(i << 1)) * inv2; } return ret.pre(deg); } // F(0) must be 0 P exp(int deg = -1) const { assert((*this)[0] == T(0)); const int n = (int)this->size(); if (deg == -1) deg = n; P ret({T(1)}); for (int i = 1; i < deg; i <<= 1) { ret = (ret * (pre(i << 1) + T(1) - ret.log(i << 1))).pre(i << 1); } return ret.pre(deg); } P pow(int64_t k, int deg = -1) const { const int n = (int)this->size(); if (deg == -1) deg = n; for (int i = 0; i < n; i++) { if ((*this)[i] != T(0)) { T rev = T(1) / (*this)[i]; P C(*this * rev); P D(n - i); for (int j = i; j < n; j++) D[j - i] = C[j]; D = (D.log() * k).exp() * mpow((*this)[i], (k)); P E(deg); if (i * k > deg) return E; auto S = i * k; for (int j = 0; j + S < deg && j < D.size(); j++) E[j + S] = D[j]; return E; } } return *this; } T eval(T x) const { T r = 0, w = 1; for (auto &v : *this) { r += w * v; w *= x; } return r; } // *x^x P &rshift(int k) { int an = this->size(); this->resize(an + k); rer(i, an + k - 1, k) { (*this)[i] = (*this)[i - k]; } rep(i, k) { (*this)[i] = 0; } return (*this); } // /x^k P &lshift(int k) { int an = this->size(); rep(i, an - k) { (*this)[i] = (*this)[i + k]; } this->resize(an - k); return (*this); } // x = rx^dを代入する P &assign(int r, int d, int msize) { P ret(msize + 1); T rv = 1; rep(i, min((int)this->size(), msize / d + 1)) { ret[i * d] = (*this)[i] * rv; rv *= r; } return *this = ret; } }; ArbitraryModConvolution<mint> fft; using fps = FormalPowerSeries<mint>; struct FPS_init { FPS_init() { fps::set_fft( [](const fps::P &a, const fps::P &b) { return fft.multiply(a, b); }); }; } initooonv; struct helper_fps { int i; mint k; helper_fps(int i, mint k) : i(i), k(k) {} operator fps() { fps ret(i + 1); ret[i] = k; return ret; } fps operator+(mint b) { return fps(*this) + b; } fps operator+(helper_fps b) { return fps(*this) + fps(b); } fps operator-(mint b) { return fps(*this) - b; } fps operator-(helper_fps b) { return fps(*this) - fps(b); } helper_fps operator*(mint b) { return helper_fps(i, k * b); } helper_fps operator*(helper_fps b) { return helper_fps(i + b.i, k * b.k); } helper_fps operator^(int b) { return helper_fps(i * b, mpow(k, b)); } } X(1, 1); fps operator+(mint a, helper_fps b) { return b + a; } fps operator+(const fps &a, helper_fps b) { return a + (fps)b; } fps operator-(mint a, helper_fps b) { return b + a; } fps operator-(const fps &a, helper_fps b) { return a - (fps)b; } helper_fps operator*(mint a, helper_fps b) { return b * a; } // vectorとして使える fps operator+(fps &l, signed v) { fps ret = l; ret[0] += v; return ret; } fps operator+(fps &l, ll v) { fps ret = l; ret[0] += v; return ret; } fps operator+=(fps &l, signed v) { l[0] += v; return l; } fps operator+=(fps &l, ll v) { l[0] += v; return l; } fps operator-(fps &l, signed v) { return operator+(l, -v); } fps operator-(fps &l, ll v) { return operator+(l, -v); } fps operator-=(fps &l, signed v) { return operator+=(l, -v); } fps operator-=(fps &l, ll v) { return operator+=(l, -v); } fps operator*(fps &l, signed v) { fps ret = l; for (int i = 0; i < l.size(); i++) ret[i] *= v; return ret; } fps operator*(fps &l, ll v) { fps ret = l; for (int i = 0; i < l.size(); i++) ret[i] *= v; return ret; } fps operator*=(fps &l, signed v) { for (int i = 0; i < l.size(); i++) l[i] *= v; return l; } fps operator*=(fps &l, ll v) { for (int i = 0; i < l.size(); i++) l[i] *= v; return l; } fps operator/(fps &l, signed v) { return operator*(l, minv(v)); } fps operator/(fps &l, ll v) { return operator*(l, minv(v)); } fps operator/=(fps &l, signed v) { return operator*=(l, minv(v)); } fps operator/=(fps &l, ll v) { return operator*=(l, minv(v)); } fps operator+(signed l, fps &v) { return operator+(v, l); } fps operator+(ll l, fps &v) { return operator+(v, l); } fps operator-(signed l, fps v) { v *= -1; return operator+(v, l); } fps operator-(ll l, fps v) { v *= -1; return operator+(v, l); } fps operator*(signed l, fps &v) { return operator*(v, l); } fps operator*(ll l, fps &v) { return operator*(v, l); } fps operator/(signed l, fps v) { v = v.inv(); return operator*(v, l); } fps operator/(ll l, fps v) { v = v.inv(); return operator*(v, l); } fps cut(fps l, fps r) { fps ret = l; ret *= r; ret.resize(max(l.size(), r.size())); return ret; } template <class... T> fps cut(fps l, T... r) { return cut(l, cut(r...)); } // initializerlist operatorに fps cutf(fps &l, fps &r) { fps ret = l; ret *= r; ret.resize(max(l.size(), r.size())); return ret; } // 合計がSになる組合せを返す template <class T> T fft_get(vector<T> &a, vector<T> &b, int S) { int bn = sz(b); T res = 0; rep(l, sz(a) + 1) { int r = S - l; if (r >= bn || r < 0) con; res += a[l] * b[r]; } return res; } // 右から疎なfpsを掛ける、O(N) * 右の項数 fps mul_sparse(const fps &l, const fps &r, int msize = -1) { vi inds; rep(i, r.size()) { if (r[i]) { inds.push_back(i); } } assert(sz(inds)); int n = l.size() + inds.back(); if (msize == -1) { msize = n - 1; } fps ret(msize + 1); rep(i, l.size()) { for (auto d : inds) { if (i + d > msize) break; ret[i + d] += l[i] * r[d]; } } return ret; } // 右から疎なfpsで割る、O(N) * 右の項数 fps div_sparse(const fps &a, const fps &r, int msize = -1) { fps l = a; vi inds; rep(i, r.size()) { if (r[i]) { inds.push_back(i); } } assert(sz(inds)); int n = l.size(); if (msize == -1) { msize = n - 1; } msize = min(msize, n - 1); fps ret(msize + 1); rep(i, l.size()) { if (l[i]) { assert(i >= inds[0]); mint dec_k = l[i] / r[inds[0]]; if (i - inds[0] > msize) break; ret[i - inds[0]] += dec_k; for (auto d : inds) { if (i + d - inds[0] >= n) break; l[i + d - inds[0]] -= dec_k * r[d]; } } } return ret; } // Π(1-x^n) (1<=n<=inf) = 1 + Σ -1^i * (x^(i*(3i-1)/2) + x^(i*(3i+1)/2) // (1<=i<=inf)http://math.josai.ac.jp/~oshima/s2004.pdf P2 fps pow_sigma(int msize) { vi reti(msize + 1); reti[0] = 1; for (int i = 2; i < inf; i += 2) { int a = (i * (3 * i - 1)) >> 1; if (a > msize) break; reti[a]++; a = (i * (3 * i + 1)) >> 1; if (a > msize) continue; reti[a]++; } for (int i = 1; i < inf; i += 2) { int a = (i * (3 * i - 1)) >> 1; if (a > msize) break; reti[a]--; a = (i * (3 * i + 1)) >> 1; if (a > msize) continue; reti[a]--; } return reti; } // 閉区間 sigma x^(0~r) = (1 - x^(r+1)) / (1 - x) // N乗はcombinationで分かる fps operator*(const fps &a, helper_fps b) { return mul_sparse(a, (fps)b); } // Σx^(0~r) = (1-x^(r+1)) / (1-x) // // (1-x^r)^nを返す sigma x^(0~r) = (1 - x^(r+1)) / (1 - x)に注意! (0~rは // r+1乗になる) fps pow_term2(int r, int n, int msize) { fps ret(msize + 1); rep(i, n + 1) { if (i * r <= msize) { ret[i * r] = com(n, i); if (i & 1) ret[i * r] = -ret[i * r]; } } return ret; } // (x^l - x^r)^n fps pow_term2(int l, int r, int n, int msize) { fps ret = pow_term2(r - l, n, msize); /*(x^l)^n * (1-x^(r-l))^n*/ ret.rshift(l * n); if (sz(ret) > msize + 1) ret.resize(msize + 1); return ret; } // 1/(1-rx)^nを返す [x^i]f = nhi * r^i fps pow_term2_inv(int r, int n, int msize) { /*http://math.josai.ac.jp/~oshima/s2004.pdf P2 */ /*wolfram Series (1/(1-r*x)^m) 級数表現*/ fps ret(msize + 1); if (r == 1) { for (int i = 0; i < msize + 1; i++) ret[i] = nhr(n, i); return ret; } mint rb = 1; for (int i = 0; i < msize + 1; i++) { ret[i] = nhr(n, i) * rb; rb *= r; } return ret; } // 1/(1-x)^n fps pow_term2_inv(int n, int msize) { return pow_term2_inv(1, n, msize); } // 1/(1-rx^d)^nを返す 1/(1-rx)^n にx=x^dを代入 fps pow_term2_inv(int r, int d, int n, int msize) { fps ret = pow_term2_inv(r, n, msize); ret.assign(1, d, msize); return ret; } /*@formatter:on*/ // 割り算の使い方が怪しい // 逆元は使えるため、逆元を掛けたほうがいいかも // invは長さが元のだけ必要 void solve() { din(N); din(S); na(A, N); int len = 4000; fps ret(len); fps su(len); rer(i, N - 1) { fps add(len); add[0] = 1; fps r(len); r[0] = 1; r[A[i]] = 1; // ret += add; ret = mul_sparse(ret + add, r); su += ret; // cout<<ret<<endl;; // deb(ret); } out(su[S]); } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // solveを書き直す // solveを呼び出す } if (was_deb && sz(res_mes)) { cerr << "result = " << endl << res_mes << endl; } if (sz(message)) { cerr << "****************************" << endl; cerr << "Note." << endl; cerr << message << endl; cerr << "****************************" << endl; } #endif return 0; };
/*temp*/ // // // // // #undef _DEBUG // #pragma GCC optimize("Ofast") // 不動小数点の計算高速化 // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #include <boost/multiprecision/cpp_int.hpp> #ifdef _DEBUG #include "template.h" #else #if __cplusplus >= 201703L /*Atcoderでしか使えない(c++17 && このテンプレートが使えるならAtcoder)*/ #include <boost/sort/pdqsort/pdqsort.hpp> #define fast_sort boost::sort::pdqsort #endif #endif #ifndef _DEBUG #ifndef UNTITLED15_TEMPLATE_H #define UNTITLED15_TEMPLATE_H #ifdef _DEBUG #include "bits_stdc++.h" #else #include <bits/stdc++.h> #endif #ifndef fast_sort #define fast_sort sort #endif // #define use_pq #define use_for #define use_for_each #define use_sort #define use_fill #define use_rand #define use_mgr #define use_rui #define use_compress // // // // // // #define use_pbds #ifdef use_pbds #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <class T, class U, class W, class X> auto count(__gnu_pbds::gp_hash_table<T, U, W> &a, X k) { return a.find(k) != a.end(); } #endif using namespace std; using namespace std::chrono; /*@formatter:off*/ #define ll long long using sig_dou = double; // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define mt make_tuple #define pb pop_back #define pf push_front #define eb emplace_back #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() template <class T, class U> auto max(T a, U b) { return a > b ? a : b; } template <class T, class U> auto min(T a, U b) { return a < b ? a : b; } // optional<T>について下でオーバーロード(nullopt_tを左辺右辺について単位元として扱う) template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } // メタ系 meta template <typename T, typename U = typename T::value_type> std::true_type value_type_tester(signed); template <typename T> std::false_type value_type_tester(long); template <typename T> struct has_value_type : decltype(value_type_tester<T>(0)) {}; // A<T>でTを返す template <class T> using decl_t = typename T::value_type; // A<B<.....T>>でTを返す template <class T, bool end = !has_value_type<T>::value> struct decl2_ { typedef T type; }; template <class T> struct decl2_<T, false> { typedef typename decl2_<decl_t<T>>::type type; }; template <class T> using decl2_t = typename decl2_<T>::type; // #define decl_max(a, b) decltype(max(MAX<decltype(a)>(), MAX<decltype(b)>())) #define is_same2(T, U) is_same<T, U>::value template <class T> struct is_vector : std::false_type {}; template <class T> struct is_vector<std::vector<T>> : std::true_type {}; // 大きい型を返す max_type<int, char>::type // todo mintがlong long より小さいと判定されるためバグる template <class T1, class T2, bool t1_bigger = (sizeof(T1) > sizeof(T2))> struct max_type { typedef T1 type; }; template <class T1, class T2> struct max_type<T1, T2, false> { typedef T2 type; }; template <class T> struct vec_rank : integral_constant<int, 0> {}; template <class T> struct vec_rank<vector<T>> : integral_constant<int, vec_rank<T>{} + 1> {}; // N個のTを並べたtupleを返す // tuple_n<3, int>::type tuple<int, int, int> template <size_t N, class T, class... Arg> struct tuple_n { typedef typename tuple_n<N - 1, T, T, Arg...>::type type; }; template <class T, class... Arg> struct tuple_n<0, T, Arg...> { typedef tuple<Arg...> type; }; struct dummy_t1 {}; struct dummy_t2 {}; struct dummy_t3 {}; struct dummy_t4 {}; struct dummy_t5 {}; struct dummy_t6 {}; // template<class T, require(is_integral<T>::value)>など #define require_t(bo) enable_if_t<bo> * = nullptr // 複数でオーバーロードする場合、引数が同じだとうまくいかないため // require_arg(bool, dummy_t1) // require_arg(bool, dummy_t2)等とする #define require_arg1(bo) enable_if_t<bo> * = nullptr #define require_arg2(bo, dummy_type) enable_if_t<bo, dummy_type> * = nullptr #define require_arg(...) \ over2(__VA_ARGS__, require_arg2, require_arg1)(__VA_ARGS__) //->//enable_if_tのtを書き忘れそうだから #define require_ret(bo, ret_type) enable_if_t<bo, ret_type> #define int long long // todo 消したら動かない intの代わりにsignedを使う auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName // 最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name void assert2(bool b, const string &s = "") { if (!b) { cerr << s << endl; exit(1); /*assert(0);*/ } } // my_nulloptをあらゆる操作の単位元的な物として扱う // vectorの参照外時に返したり、右辺値として渡されたときに何もしないなど struct my_nullopt_t { } my_nullopt; #define nullopt_t my_nullopt_t #define nullopt my_nullopt /*@formatter:off*/ // 値が無いときは、setを使わない限り代入できない //=を使っても無視される template <class T> struct my_optional { private: bool is_null; T v; public: typedef T value_type; my_optional() : is_null(true) {} my_optional(const nullopt_t &) : is_null(true) {} my_optional(const T &v) : v(v), is_null(false) {} bool has_value() const { return !is_null; } T &value() { static string mes = "optional has no value"; assert2(!is_null, mes); return v; } const T &value() const { static string mes = "optional has no value"; assert2(!is_null, mes); return v; } void set(const T &nv) { is_null = false; v = nv; } template <class U> void operator=(const U &v) { set(v); // null状態でも代入出来るようにした // if (has_value())value() = v; else return; } template <class U> void operator=(const my_optional<U> &v) { if (/*has_value() && */ v.has_value()) (*this) = v; else return; } /*@formatter:off*/ void reset() { is_null = true; } void operator=(const nullopt_t &) { reset(); } template <require_t(!is_same2(T, bool))> explicit operator bool() { return !is_null; } // nullの時はエラー operator T &() { return value(); } operator const T &() const { return value(); } my_optional<T> operator++() { if (this->has_value()) { this->value()++; return *this; } else { return *this; } } my_optional<T> operator++(signed) { if (this->has_value()) { auto tem = *this; this->value()++; return tem; } else { return *this; } } my_optional<T> operator--() { if (this->has_value()) { this->value()--; return *this; } else { return *this; } } my_optional<T> operator--(signed) { if (this->has_value()) { auto tem = *this; this->value()--; return tem; } else { return *this; } } }; template <class T> istream &operator>>(istream &iss, my_optional<T> &v) { T val; iss >> val; v.set(val); return iss; } #define optional my_optional template <class T> using opt = my_optional<T>; // template<class T, class A = std::allocator<T>> struct debtor : std::vector<T, // A> { template <class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key, T>>> struct o_map : std::map<Key, optional<T>, Compare, Allocator> { optional<T> emp; o_map() : std::map<Key, optional<T>, Compare, Allocator>() {} auto operator()(const nullopt_t &) { return nullopt; } optional<T> &operator()(const optional<Key> &k) { if (k.has_value()) { return std::map<Key, optional<T>, Compare, Allocator>::operator[]( k.value()); } else { emp.reset(); return emp; } } optional<T> &operator()(const Key &k) { auto &v = std::map<Key, optional<T>, Compare, Allocator>::operator[](k); if (v.has_value()) return v; else { v.set(0); return v; } } template <class U> void operator[](U) { static string mes = "s_map cant []"; assert2(0, mes); } }; // 以下、空のoptionalをnulloptと書く // ov[-1(参照外)] でnulloptを返す // ov[nullopt] で nulloptをかえす template <class T> struct ov { optional<T> emp; vector<optional<T>> v; ov(int i = 0, T val = 0) : v(i, val) {} template <class U> ov(const U &rhs) { v.resize(sz(rhs)); for (int i = 0; i < sz(rhs); i++) v[i].set(rhs[i]); } optional<T> &operator()(int i) { if (i < 0 || sz(v) <= i) { emp.reset(); return emp; } else { return v[i]; } } optional<T> &operator()(const nullopt_t &) { return operator()(-1); } optional<T> &operator()(const optional<T> &i) { if (i.has_value()) return operator()(i.value()); else { return operator()(-1); } } /*@formatter:off*/ }; template <class T> string deb_tos(const ov<T> &v) { return deb_tos(v.v); } // vectorに対しての処理は.vを呼ぶ template <class T> class ovv { optional<T> emp; public: vector<vector<optional<T>>> v; ovv(int i = 0, int j = 0, T val = 0) : v(i, vector<optional<T>>(j, val)) {} optional<T> &operator()(int i, int j) { if (i < 0 || j < 0 || sz(v) <= i || sz(v[i]) <= j) { emp.reset(); return emp; } else { return v[i][j]; } } // 再帰ver 遅いと思う // optional<T>& gets(optional<T>& v){return v;} // template<class V, class H, class... U> optional<T>& gets(V& v, H i, U... // tail){ if constexpr(is_same2(H, nullopt_t))return // operator()(-1,-1); else if constexpr(is_same2(H, optional<int>)){ // if(i.has_value())return gets(v[(int)i], tail...); else return // operator()(-1,-1); }else if constexpr(is_integral<H>::value){ // return gets(v[(int)i], tail...); }else{ assert(0); // return emp; } } #if __cplusplus >= 201703L // if constexprバージョン 上が遅かったらこれで template <class U, class V> optional<T> &operator()(const U &i, const V &j) { /*駄目な場合を除外*/ if constexpr (is_same2(U, nullopt_t) || is_same2(U, nullopt_t)) { return operator()(-1, -1); /* o, o*/ } else if constexpr (is_same2(U, optional<int>) && is_same2(V, optional<int>)) { return operator()(i.has_value() ? (int)i : -1, j.has_value() ? (int)j : -1); /* o, x*/ } else if constexpr (is_same2(U, optional<int>)) { return operator()(i.has_value() ? (int)i : -1, (int)j); /* x, o*/ } else if constexpr (is_same2(V, optional<int>)) { return operator()((int)i, j.has_value() ? (int)j : -1); /* x, x*/ } else { return operator()((int)i, (int)j); } } #endif operator const vector<vector<optional<T>>> &() { return v; } }; template <class T> istream &operator>>(istream &iss, ovv<T> &a) { for (int h = 0; h < sz(a); h++) { for (int w = 0; w < sz(a[h]); w++) { iss >> a.v[h][w]; } } return iss; } template <class T> string deb_tos(const ovv<T> &v) { return deb_tos(v.v); } template <class T> struct ov3 { optional<T> emp; vector<vector<vector<optional<T>>>> v; ov3(int i, int j, int k, T val = 0) : v(i, vector<vector<optional<T>>>(j, vector<optional<T>>(k, val))) {} optional<T> &operator()(int i, int j, int k) { if (i < 0 || j < 0 || sz(v) <= i || sz(v[i]) <= j) { if (k < 0 || sz(v[i][j]) <= k) { emp.reset(); return emp; } } return v[i][j][k]; } private: #if __cplusplus >= 201703L // 再帰ver 遅いと思う template <class V, class H> optional<T> &gets(V &nowv, H i) { if constexpr (is_same2(H, nullopt_t)) { emp.reset(); return emp; } else if constexpr (is_same2(H, optional<int>)) { if (i.has_value()) { return nowv[(int)i]; } else { emp.reset(); return emp; } } else if constexpr (is_integral<H>::value) { return nowv[(int)i]; } else { static string mes = "ov3 error not index"; assert2(0, mes); emp.reset(); return emp; } } // todo const &消した template <class V, class H, class... U> optional<T> &gets(V &nowv, H i, U... tail) { if constexpr (is_same2(H, nullopt_t)) { emp.reset(); return emp; } else if constexpr (is_same2(H, optional<int>)) { if (i.has_value()) { return gets(nowv[(int)i], tail...); } else { emp.reset(); return emp; } } else if constexpr (is_integral<H>::value) { return gets(nowv[(int)i], tail...); } else { static string mes = "ov3 error not index"; assert2(0, mes); emp.reset(); return emp; } } #endif public: template <class U, class V, class W> optional<T> &operator()(U i, V j, W k) { return gets(v, i, j, k); } /*@formatter:off*/ }; template <class T> string deb_tos(const ov3<T> &v) { return deb_tos(v.v); } // nullopt_t // 優先順位 // null, [opt, tem] // + と += は違う意味を持つ // val+=null : val // val+null : null // //+は途中計算 //+=は最終的に格納したい値にだけ持たせる //+=がvoidを返すのは、途中計算で使うのを抑制するため // nulloptを考慮する際、計算途中では+を使ってnulloptを作り // 格納する際は+=で無効にする必要がある // 演算子== // optional<int>(10) == 10 // 全ての型に対応させ、value_typeが等しいかを見るようにするのもありかも // null同士を比較する状況はおかしいのではないか bool operator==(const nullopt_t &, const nullopt_t &) { assert2(0, "nul == null cant hikaku"); return false; } template <class T> bool operator==(const nullopt_t &, const T &) { return false; } template <class T> bool operator!=(const nullopt_t &, const T &) { return true; } template <class T> bool operator==(const T &, const nullopt_t &) { return false; } template <class T> bool operator!=(const T &, const nullopt_t &) { return true; } // nullを nullopt_t &operator+(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator-(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator*(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator/(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator+=(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator-=(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator*=(const nullopt_t &, const nullopt_t &) { return nullopt; } nullopt_t &operator/=(const nullopt_t &, const nullopt_t &) { return nullopt; } template <class ANY> nullopt_t operator+(const nullopt_t &, const ANY &) { return nullopt; } template <class ANY> nullopt_t operator-(const nullopt_t &, const ANY &) { return nullopt; } template <class ANY> nullopt_t operator*(const nullopt_t &, const ANY &) { return nullopt; } template <class ANY> nullopt_t operator/(const nullopt_t &, const ANY &) { return nullopt; } template <class ANY> nullopt_t operator+(const ANY &, const nullopt_t &) { return nullopt; } template <class ANY> nullopt_t operator-(const ANY &, const nullopt_t &) { return nullopt; } template <class ANY> nullopt_t operator*(const ANY &, const nullopt_t &) { return nullopt; } template <class ANY> nullopt_t operator/(const ANY &, const nullopt_t &) { return nullopt; } template <class ANY> void operator+=(nullopt_t &, const ANY &) {} template <class ANY> void operator-=(nullopt_t &, const ANY &) {} template <class ANY> void operator*=(nullopt_t &, const ANY &) {} template <class ANY> void operator/=(nullopt_t &, const ANY &) {} template <class ANY> void operator+=(ANY &, const nullopt_t &) {} template <class ANY> void operator-=(ANY &, const nullopt_t &) {} template <class ANY> void operator*=(ANY &, const nullopt_t &) {} template <class ANY> void operator/=(ANY &, const nullopt_t &) {} template <class T> struct is_optional : false_type {}; template <class T> struct is_optional<optional<T>> : true_type {}; template <class T, class U> true_type both_optional(optional<T> t, optional<U> u); false_type both_optional(...); template <class T, class U> class opt_check : public decltype(both_optional(declval<T>(), declval<U>())) {}; // optionalは同じ型同士しか足せない //(o, t), (t, o), (o, o) #define opt_tem(op) \ template <class O, class O_ret = decltype(declval<O>() op declval<O>())> \ optional<O_ret> operator op(const optional<O> &opt1, \ const optional<O> &opt2) { \ if (!opt1.has_value() || !opt2.has_value()) { \ return optional<O_ret>(); \ } else { \ return optional<O_ret>(opt1.value() op opt2.value()); \ } \ } \ template <class O, class T, \ class O_ret = decltype(declval<O>() op declval<O>())> \ auto operator op(const optional<O> &opt, const T &tem) \ ->require_ret(!(opt_check<optional<O>, T>::value), optional<O_ret>) { \ if (!opt.has_value()) { \ return optional<O_ret>(); \ } else { \ return optional<O_ret>(opt.value() op tem); \ } \ } \ template <class O, class T, \ class O_ret = decltype(declval<O>() op declval<O>())> \ auto operator op(const T &tem, const optional<O> &opt) \ ->require_ret(!(opt_check<optional<O>, T>::value), optional<O_ret>) { \ if (!opt.has_value()) { \ return optional<O_ret>(); \ } else { \ return optional<O_ret>(opt.value() op tem); \ } \ } /*@formatter:off*/ opt_tem(+) opt_tem(-) opt_tem(*) opt_tem(/) // 比較はoptional<bool>を返す opt_tem(<) opt_tem(>) opt_tem(<=) opt_tem(>=) /*@formatter:on*/ /*@formatter:off*/ template <class O, class T> bool operator==(const optional<O> &opt, const T &tem) { if (opt.has_value()) { return opt.value() == tem; } else return nullopt == tem; } template <class O, class T> bool operator!=(const optional<O> &opt, const T &tem) { if (opt.has_value()) { return opt.value() != tem; } else return nullopt != tem; } template <class O, class T> bool operator==(const T &tem, const optional<O> &opt) { if (opt.has_value()) { return opt.value() == tem; } else return nullopt == tem; } template <class O, class T> bool operator!=(const T &tem, const optional<O> &opt) { if (opt.has_value()) { return opt.value() != tem; } else return nullopt != tem; } template <class O> bool operator==(const optional<O> &opt1, const optional<O> &opt2) { if (opt1.has_value() != opt2.has_value()) { return false; } else if (opt1.has_value()) { return opt1.value() == opt2.value(); } else { return nullopt == nullopt; } } template <class O> bool operator!=(const optional<O> &opt1, const optional<O> &opt2) { return !(opt1 == opt2); } //(a+=null) != (a=a+null) // a null template <class T, class O> void operator+=(T &tem, const optional<O> &opt) { if (opt.has_value()) { tem += opt.value(); } } template <class T, class O> void operator-=(T &tem, const optional<O> &opt) { if (opt.has_value()) { tem -= opt.value(); } } template <class T, class O> void operator*=(T &tem, const optional<O> &opt) { if (opt.has_value()) { tem *= opt.value(); } } template <class T, class O> void operator/=(T &tem, const optional<O> &opt) { if (opt.has_value()) { tem /= opt.value(); } } template <class T, class O> void operator+=(optional<O> &opt, const T &tem) { if (opt.has_value()) { opt.value() += tem; } } template <class T, class O> void operator-=(optional<O> &opt, const T &tem) { if (opt.has_value()) { opt.value() -= tem; } } template <class T, class O> void operator*=(optional<O> &opt, const T &tem) { if (opt.has_value()) { opt.value() *= tem; } } template <class T, class O> void operator/=(optional<O> &opt, const T &tem) { if (opt.has_value()) { opt.value() /= tem; } } // template <class Ol, class Or> void operator+=(optional<Ol> &opl, const optional<Or> &opr) { if (opr.has_value()) { return opl += opr.value(); } } template <class Ol, class Or> void operator-=(optional<Ol> &opl, const optional<Or> &opr) { if (opr.has_value()) { return opl -= opr.value(); } } template <class Ol, class Or> void operator*=(optional<Ol> &opl, const optional<Or> &opr) { if (opr.has_value()) { return opl *= opr.value(); } } template <class Ol, class Or> void operator/=(optional<Ol> &opl, const optional<Or> &opr) { if (opr.has_value()) { return opl /= opr.value(); } } /*@formatter:off*/ template <class U> auto max(const nullopt_t &, const U &val) { return val; } template <class U> auto max(const U &val, const nullopt_t &) { return val; } template <class U> auto min(const nullopt_t &, const U &val) { return val; } template <class U> auto min(const U &val, const nullopt_t &) { return val; } template <class T, class U> auto max(const optional<T> &opt, const U &val) { if (opt.has_value()) return max(opt.value(), val); else return val; } template <class T, class U> auto max(const U &val, const optional<T> &opt) { if (opt.has_value()) return max(opt.value(), val); else return val; } template <class T, class U> auto min(const optional<T> &opt, const U &val) { if (opt.has_value()) return min(opt.value(), val); else return val; } template <class T, class U> auto min(const U &val, const optional<T> &opt) { if (opt.has_value()) return min(opt.value(), val); else return val; } // null , optional, T bool chma(nullopt_t &, const nullopt_t &) { return false; } template <class T> bool chma(T &opt, const nullopt_t &) { return false; } template <class T> bool chma(nullopt_t &, const T &opt) { return false; } template <class T> bool chma(optional<T> &olv, const optional<T> &orv) { if (orv.has_value()) { return chma(olv, orv.value()); } else return false; } template <class T, class U> bool chma(optional<T> &opt, const U &rhs) { if (opt.has_value()) { return chma(opt.value(), rhs); } else return false; } template <class T, class U> bool chma(T &lhs, const optional<U> &opt) { if (opt.has_value()) { return chma(lhs, opt.value()); } else return false; } bool chmi(nullopt_t &, const nullopt_t &) { return false; } template <class T> bool chmi(T &opt, const nullopt_t &) { return false; } template <class T> bool chmi(nullopt_t &, const T &opt) { return false; } template <class T> bool chmi(optional<T> &olv, const optional<T> &orv) { if (orv.has_value()) { return chmi(olv, orv.value()); } else return false; } template <class T, class U> bool chmi(optional<T> &opt, const U &rhs) { if (opt.has_value()) { return chmi(opt.value(), rhs); } else return false; } template <class T, class U> bool chmi(T &lhs, const optional<U> &opt) { if (opt.has_value()) { return chmi(lhs, opt.value()); } else return false; } template <class T> ostream &operator<<(ostream &os, optional<T> p) { if (p.has_value()) os << p.value(); else os << "e"; return os; } template <class T> using opt = my_optional<T>; struct xorshift { /*@formatter:on*/ static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } /*@formatter:off*/ size_t operator()(const uint64_t &x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(const std::pair<ll, ll> &x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } template <class T, class U> size_t operator()(const std::pair<T, U> &x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); uint64_t hasx = splitmix64(x.first); uint64_t hasy = splitmix64(x.second + FIXED_RANDOM); return hasx ^ hasy; } template <class T> size_t operator()(const vector<T> &x) const { uint64_t has = 0; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); uint64_t rv = splitmix64(FIXED_RANDOM); for (int i = 0; i < sz(x); i++) { uint64_t v = splitmix64(x[i] + rv); has ^= v; rv = splitmix64(rv); } return has; } }; #ifdef _DEBUG string message; string res_mes; // #define use_debtor // template<class T, class U, class X> auto count(unordered_map<T, U> &a, X k) { // return a.find(k) != a.end(); } #ifdef use_debtor // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference my_at(typename std::vector<T>::size_type n, vector<int> &ind) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; ind.push_back(n); cerr << "out index at "; for (auto &&i : ind) { cerr << i << " "; } cerr << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef use_pbds template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> #endif // 区間削除は出来ない // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; template <class K, class V> using umap_f = my_unordered_map<K, V>; #else #define endl '\n' // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> template <class K, class V> using umap_f = __gnu_pbds::gp_hash_table<K, V, xorshift>; #endif #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umappp unordered_map<P, P> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> template <class T, class U, class X> auto count(unordered_map<T, U> &a, X k) { return a.find(k) != a.end(); } /*@formatter:off*/ #ifdef use_pbds template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } #endif // 衝突対策 #define ws ws_ // todo 要らないと思う template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 */ } bool operator==(const T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; // T mt(ll a, ll b, ll c) { return T(a, b, c); } // F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } // 関数内をまとめる // 初期値l=-1, r=-1 void set_lr12(int &l, int &r, int n) { /*r==-1*/ if (r == -1) { if (l == -1) { l = 0; r = n; } else { r = l; l = 0; } } } //@マクロ省略系 型,構造 // using で元のdoubleを同時に使えるはず #define double_big #ifdef double_big #define double long double // #define pow powl #endif using dou = double; /*@formatter:off*/ template <class T> T MAX() { return numeric_limits<T>::max(); } template <class T> T MIN() { return numeric_limits<T>::min(); } constexpr ll inf = (ll)1e9 + 100; constexpr ll linf = (ll)1e18 + 100; constexpr dou dinf = (dou)linf * linf; constexpr char infc = '{'; const string infs = "{"; template <class T> T INF() { return MAX<T>() / 2; } template <> signed INF() { return inf; } template <> ll INF() { return linf; } template <> double INF() { return dinf; } template <> char INF() { return infc; } template <> string INF() { return infs; } const double eps = 1e-9; // #define use_epsdou #ifdef use_epsdou // 基本コメントアウト struct epsdou { double v; epsdou(double v = 0) : v(v) {} template <class T> epsdou &operator+=(T b) { v += (double)b; return (*this); } template <class T> epsdou &operator-=(T b) { v -= (double)b; return (*this); } template <class T> epsdou &operator*=(T b) { v *= (double)b; return (*this); } template <class T> epsdou &operator/=(T b) { v /= (double)b; return (*this); } epsdou operator+(epsdou b) { return v + (double)b; } epsdou operator-(epsdou b) { return v - (double)b; } epsdou operator*(epsdou b) { return v * (double)b; } epsdou operator/(epsdou b) { return v / (double)b; } epsdou operator-() const { return epsdou(-v); } template <class T> bool operator<(T b) { return v < (double)b; } template <class T> bool operator>(T b) { auto r = (double)b; return v > (double)b; } template <class T> bool operator==(T b) { return fabs(v - (double)b) <= eps; } template <class T> bool operator<=(T b) { return v < (double)b || fabs(v - b) <= eps; } template <class T> bool operator>=(T b) { return v > (double)b || fabs(v - b) <= eps; } operator double() { return v; } }; template <> epsdou MAX() { return MAX<double>(); } template <> epsdou MIN() { return MIN<double>(); } // priqrity_queue等で使うのに必要 bool operator<(const epsdou &a, const epsdou &b) { return a.v < b.v; } bool operator>(const epsdou &a, const epsdou &b) { return a.v > b.v; } istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss; } ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os; } #define eps_conr_t(o) \ template <class T> epsdou operator o(T a, epsdou b) { return (dou)a o b.v; } #define eps_conl_t(o) \ template <class T> epsdou operator o(epsdou a, T b) { return a.v o(dou) b; } eps_conl_t(+) eps_conl_t(-) eps_conl_t(*) eps_conl_t(/) eps_conr_t(+) eps_conr_t(-) eps_conr_t(*) eps_conr_t(/) // template<class U> epsdou max(epsdou a, U b){return a.v>b ? a.v: b;} // template<class U> epsdou max(U a, epsdou b){return a>b.v ? a: b.v;} // template<class U> epsdou min(epsdou a, U b){return a.v<b ? a.v: b;} // template<class U> epsdou min(U a, epsdou b){return a<b.v ? a: b.v;} #undef double #define double epsdou #undef dou #define dou epsdou #endif template <class T = int, class A, class B = int> T my_pow(A a, B b = 2) { if (b < 0) return (T)1 / my_pow<T>(a, -b); #if __cplusplus >= 201703L if constexpr (is_floating_point<T>::value) { return pow((T)a, (T)b); } else if constexpr (is_floating_point<A>::value) { assert2( 0, "pow <not dou>(dou, )"); /*return 0;しない方がコンパイル前に(voidを受け取るので)エラーが出ていいかも*/ } else if constexpr (is_floating_point<B>::value) { assert2( 0, "pow <not dou>(, dou)"); /*return 0;しない方がコンパイル前に(voidを受け取るので)エラーが出ていいかも*/ } else { #endif T ret = 1; T bek = a; while (b) { if (b & 1) ret *= bek; bek *= bek; b >>= 1; } return ret; #if __cplusplus >= 201703L } #endif } #define pow my_pow #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam1(ret) [&](auto &&v) { return ret; } #define lam2(v, ret) [&](auto &&v) { return ret; } #define lam(...) over2(__VA_ARGS__, lam2, lam1)(__VA_ARGS__) #define lamr(right) [&](auto &&p) { return p right; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); // マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; // #define V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__) #define vvi(...) vv(ll, __VA_ARGS__) #define vvb(...) vv(bool, __VA_ARGS__) #define vvs(...) vv(string, __VA_ARGS__) #define vvd(...) vv(double, __VA_ARGS__) #define vvc(...) vv(char, __VA_ARGS__) #define vvp(...) vv(P, __VA_ARGS__) #define vvt(...) vv(T, __VA_ARGS__) // optional #define vvoi(...) vv(optional<ll>, __VA_ARGS__) template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setp = set<P>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap // 任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; /*@formatter:off*/ string to_string(char c) { string ret = ""; ret += c; return ret; } template <class T> class pq_min_max { vector<T> d; void make_heap() { for (int i = d.size(); i--;) { if (i & 1 && d[i - 1] < d[i]) swap(d[i - 1], d[i]); int k = down(i); up(k, i); } } inline int parent(int k) const { return ((k >> 1) - 1) & ~1; } int down(int k) { int n = d.size(); if (k & 1) { /* min heap*/ while (2 * k + 1 < n) { int c = 2 * k + 3; if (n <= c || d[c - 2] < d[c]) c -= 2; if (c < n && d[c] < d[k]) { swap(d[k], d[c]); k = c; } else break; } } else { /* max heap*/ while (2 * k + 2 < n) { int c = 2 * k + 4; if (n <= c || d[c] < d[c - 2]) c -= 2; if (c < n && d[k] < d[c]) { swap(d[k], d[c]); k = c; } else break; } } return k; } int up(int k, int root = 1) { if ((k | 1) < (int)d.size() && d[k & ~1] < d[k | 1]) { swap(d[k & ~1], d[k | 1]); k ^= 1; } int p; while (root < k && d[p = parent(k)] < d[k]) { /*max heap*/ swap(d[p], d[k]); k = p; } while (root < k && d[k] < d[p = parent(k) | 1]) { /* min heap*/ swap(d[p], d[k]); k = p; } return k; } public: pq_min_max() {} pq_min_max(const vector<T> &d_) : d(d_) { make_heap(); } template <class Iter> pq_min_max(Iter first, Iter last) : d(first, last) { make_heap(); } void operator+=(const T &x) { int k = d.size(); d.push_back(x); up(k); } void pop_min() { if (d.size() < 3u) { d.pop_back(); } else { swap(d[1], d.back()); d.pop_back(); int k = down(1); up(k); } } void pop_max() { if (d.size() < 2u) { d.pop_back(); } else { swap(d[0], d.back()); d.pop_back(); int k = down(0); up(k); } } const T &get_min() const { return d.size() < 2u ? d[0] : d[1]; } const T &get_max() const { return d[0]; } int size() const { return d.size(); } bool empty() const { return d.empty(); } }; // 小さいほうからM個取得するpq template <class T> struct helper_pq_size { pq_min_max<T> q; T su = 0; int max_size = 0; helper_pq_size() {} helper_pq_size(int max_size) : max_size(max_size) {} void clear() { q = pq_min_max<T>(); su = 0; } void operator+=(T v) { su += v; q += (v); if (sz(q) > max_size) { su -= q.get_max(); q.pop_max(); } } T sum() { return su; } T top() { return q.get_min(); } void pop() { su -= q.get_min(); q.pop_min(); } T poll() { T ret = q.get_min(); su -= ret; q.pop_min(); return ret; } ll size() { return q.size(); } }; // 大きいほうからM個取得するpq template <class T> struct helper_pqg_size { pq_min_max<T> q; T su = 0; int max_size = 0; helper_pqg_size() {} helper_pqg_size(int max_size) : max_size(max_size) {} void clear() { q = pq_min_max<T>(); su = 0; } void operator+=(T v) { su += v; q += (v); if (sz(q) > max_size) { su -= q.get_min(); q.pop_min(); } } T sum() { return su; } T top() { return q.get_max(); } void pop() { su -= q.get_max(); q.pop_max(); } T poll() { T ret = q.get_max(); su -= ret; q.pop_min(); return ret; } ll size() { return q.size(); } }; ; template <class T, class Container = vector<T>, class Compare = std::less<typename Container::value_type>> struct helper_pqg { priority_queue<T, Container, Compare> q; /*小さい順*/ T su = 0; helper_pqg() {} void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> using helper_pq = helper_pqg<T, vector<T>, greater<T>>; #if __cplusplus >= 201703L // 小さいほうからsize個残る // Tがoptionalなら空の時nullを返す template <class T> struct pq { helper_pq<T> a_q; /*大きい順*/ helper_pq_size<T> b_q; /*大きい順*/ bool aquery; T su = 0; pq(int size = inf) { aquery = size == inf; if (!aquery) { b_q = helper_pq_size<T>(size); } } void clear() { if (aquery) a_q.clear(); else b_q.clear(); } void operator+=(T v) { if (aquery) a_q += v; else b_q += v; } // optionalなら空の時nullを返す T top() { if constexpr (is_optional<T>::value) { if (aquery) { if (sz(a_q) == 0) return T(); return a_q.top(); } else { if (sz(b_q) == 0) return T(); return b_q.top(); } } else { if (aquery) return a_q.top(); else return b_q.top(); } } T sum() { if (aquery) return a_q.sum(); else return b_q.sum(); } // optionalなら空の時何もしない void pop() { if constexpr (is_optional<T>::value) { if (aquery) { if (sz(a_q)) a_q.pop(); } else { if (sz(b_q)) b_q.pop(); } } else { if (aquery) a_q.pop(); else b_q.pop(); } } /*T*/ T poll() { if constexpr (is_optional<T>::value) { if (aquery) { if (sz(a_q) == 0) return T(); return a_q.poll(); } else { if (sz(b_q) == 0) return T(); return b_q.poll(); } } else { if (aquery) return a_q.poll(); else return b_q.poll(); } } ll size() { if (aquery) return a_q.size(); else return b_q.size(); } /*@formatter:off*/ }; template <class T> struct pqg { helper_pqg<T> a_q; /*大きい順*/ helper_pqg_size<T> b_q; /*大きい順*/ bool aquery; T su = 0; pqg(int size = inf) { aquery = size == inf; if (!aquery) { b_q = helper_pqg_size<T>(size); } } void clear() { if (aquery) a_q.clear(); else b_q.clear(); } void operator+=(T v) { if (aquery) a_q += v; else b_q += v; } T sum() { if (aquery) return a_q.sum(); else return b_q.sum(); } T top() { if (aquery) return a_q.top(); else return b_q.top(); } void pop() { if (aquery) a_q.pop(); else b_q.pop(); } T poll() { if (aquery) return a_q.poll(); else return b_q.poll(); } ll size() { if (aquery) return a_q.size(); else return b_q.size(); } }; #else // 小さいほうからsize個残る template <class T> struct pq { helper_pq<T> a_q; /*大きい順*/ helper_pq_size<T> b_q; /*大きい順*/ bool aquery; T su = 0; pq(int size = inf) { aquery = size == inf; if (!aquery) { b_q = helper_pq_size<T>(size); } } void clear() { if (aquery) a_q.clear(); else b_q.clear(); } void operator+=(T v) { if (aquery) a_q += v; else b_q += v; } T sum() { if (aquery) return a_q.sum(); else return b_q.sum(); } T top() { if (aquery) return a_q.top(); else return b_q.top(); } void pop() { if (aquery) a_q.pop(); else b_q.pop(); } T poll() { if (aquery) return a_q.poll(); else return b_q.poll(); } ll size() { if (aquery) return a_q.size(); else return b_q.size(); } }; // 大きいほうからsize個残る template <class T> struct pqg { helper_pqg<T> a_q; /*大きい順*/ helper_pqg_size<T> b_q; /*大きい順*/ bool aquery; T su = 0; pqg(int size = inf) { aquery = size == inf; if (!aquery) { b_q = helper_pqg_size<T>(size); } } void clear() { if (aquery) a_q.clear(); else b_q.clear(); } void operator+=(T v) { if (aquery) a_q += v; else b_q += v; } T sum() { if (aquery) return a_q.sum(); else return b_q.sum(); } T top() { if (aquery) return a_q.top(); else return b_q.top(); } void pop() { if (aquery) a_q.pop(); else b_q.pop(); } T poll() { if (aquery) return a_q.poll(); else return b_q.poll(); } ll size() { if (aquery) return a_q.size(); else return b_q.size(); } }; #endif #define pqi pq<ll> #define pqgi pqg<ll> template <class T> string deb_tos(pq<T> &q) { vector<T> res; auto temq = q; while (sz(temq)) res.push_back(temq.top()), temq.pop(); stringstream ss; ss << res; return ss.str(); } template <class T> string deb_tos(pqg<T> &q) { vector<T> res; auto temq = q; while (sz(temq)) res.push_back(temq.top()), temq.pop(); stringstream ss; ss << res; return ss.str(); } /*@formatter:off*/ // マクロ 繰り返し // ↓@オーバーロード隔離 // todo 使わないもの非表示 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) // 逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #ifdef use_for // ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) // 正方形である必要はない // 直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int repvI = (int)a.size(), repvJ = (int)a[0].size(), i = 0, j = 0; \ i < repvI; nex_repv2(i, j, repvI, repvJ, a)) // 箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int repvI = (int)a.size(), repvJ = (int)a[0].size(), \ repvK = (int)a[0][0].size(), i = 0, j = 0, k = 0; \ i < repvI; nex_repv3(i, j, k, repvI, repvJ, repvK, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) #endif template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } // #define use_for #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define forrm1(st) \ for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it) #define forrm3(k, v, st) \ for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it) // 向こう側で // ++itか it = st.erase(it)とする #define fors1(st) for (auto &&it = st.begin(); it != st.end();) #define fors2(v, st) for (auto &&it = st.begin(); it != st.end();) #define fors3(v, st, r) \ for (auto &&it = st.begin(); it != st.end() && (*it) < r;) #define fors4(v, st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) #ifdef use_for #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define fora_f_init_2(a, A) ; #define fora_f_init_3(fora_f_i, a, A) auto &&a = A[fora_f_i]; #define fora_f_init_4(a, b, A, B) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; #define fora_f_init_5(fora_f_i, a, b, A, B) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; #define fora_f_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; \ auto &&c = C[fora_f_i]; #define fora_f_init_7(fora_f_i, a, b, c, A, B, C) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; \ auto &&c = C[fora_f_i]; #define fora_f_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; \ auto &&c = C[fora_f_i]; \ auto &&d = D[fora_f_i]; #define fora_f_init_9(fora_f_i, a, b, c, d, A, B, C, D) \ auto &&a = A[fora_f_i]; \ auto &&b = B[fora_f_i]; \ auto &&c = C[fora_f_i]; \ auto &&d = D[fora_f_i]; #define fora_f_init(...) \ over9(__VA_ARGS__, fora_f_init_9, fora_f_init_8, fora_f_init_7, \ fora_f_init_6, fora_f_init_5, fora_f_init_4, fora_f_init_3, \ fora_f_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_3(forr_i, a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_5(forr_i, a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_7(forr_i, a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init_9(forr_i, a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over9(__VA_ARGS__, forr_init_9, forr_init_8, forr_init_7, forr_init_6, \ forr_init_5, forr_init_4, forr_init_3, forr_init_2)(__VA_ARGS__) #define forp_init3(k, v, S) \ auto &&k = S[forp_i].first; \ auto &&v = S[forp_i].second; #define forp_init4(forp_i, k, v, S) \ auto &&k = S[forp_i].first; \ auto &&v = S[forp_i].second; #define forp_init(...) \ over4(__VA_ARGS__, forp_init4, forp_init3, forp_init2, \ forp_init1)(__VA_ARGS__) #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define forrm_init(k, v, ...) \ auto &&k = (*forrm_it).fi; \ auto &&v = (*forrm_it).se; #define fors_init(v, ...) auto &&v = (*it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); // こうしないとmapがおかしくなる #define fora_f_2(a, A) for (auto &&a : A) #define fora_f_3(fora_f_i, a, A) rep(fora_f_i, sz(A)) #define fora_f_4(a, b, A, B) rep(fora_f_i, sz(A)) #define fora_f_5(fora_f_i, a, b, A, B) rep(fora_f_i, sz(A)) #define fora_f_6(a, b, c, A, B, C) rep(fora_f_i, sz(A)) #define fora_f_7(fora_f_i, a, b, c, A, B, C) rep(fora_f_i, sz(A)) #define fora_f_8(a, b, c, d, A, B, C, D) rep(fora_f_i, sz(A)) #define fora_f_9(fora_f_i, a, b, c, d, A, B, C, D) rep(fora_f_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_3(forr_i, a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_5(forr_i, a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_7(forr_i, a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) #define forr_9(forr_i, a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) #endif // ↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // 自分込みで残りがREM以上の間ループを回す #define rem(i, N, REM) for (int i = 0; i < N - REM + 1; i++) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #ifdef use_for #define fora_f(...) \ over9(__VA_ARGS__, fora_f_9, fora_f_8, fora_f_7, fora_f_6, fora_f_5, \ fora_f_4, fora_f_3, fora_f_2)(__VA_ARGS__) #endif #define forr(...) \ over9(__VA_ARGS__, forr_9, forr_8, forr_7, forr_6, forr_5, forr_4, forr_3, \ forr_2)(__VA_ARGS__) // 0~N-2まで見る #define forar_init(v, rv, A) \ auto &&v = A[forar_i]; \ auto &&rv = A[forar_i + 1]; #define forar(v, rv, A) rep(forar_i, sz(A) - 1) #if __cplusplus >= 201703L template <size_t M_SZ, bool indexed, class Iterator, class T, class U = T, class V = T, class W = T> class ite_vec_merge : public Iterator { std::size_t i = 0; vector<T> &A; vector<U> &B; vector<V> &C; vector<W> &D; public: ite_vec_merge(Iterator ita, vector<T> &A) : Iterator(ita), A(A), B(A), C(A), D(A) {} ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B) : Iterator(ita), A(A), B(B), C(A), D(A) {} ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B, vector<V> &C) : Iterator(ita), A(A), B(B), C(C), D(A) {} ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B, vector<V> &C, vector<W> &D) : Iterator(ita), A(A), B(B), C(C), D(D) {} auto &operator++() { ++i; this->Iterator::operator++(); return *this; } auto operator*() const noexcept { if constexpr (!indexed && M_SZ == 1) { return tuple<T &>(A[i]); } else if constexpr (!indexed && M_SZ == 2) { return tuple<T &, U &>(A[i], B[i]); } else if constexpr (!indexed && M_SZ == 3) { return tuple<T &, U &, V &>(A[i], B[i], C[i]); } else if constexpr (!indexed && M_SZ == 4) { return tuple<T &, U &, V &, W &>(A[i], B[i], C[i], D[i]); } else if constexpr (indexed && M_SZ == 1) { return tuple<int, T &>(i, A[i]); } else if constexpr (indexed && M_SZ == 2) { return tuple<int, T &, U &>(i, A[i], B[i]); } else if constexpr (indexed && M_SZ == 3) { return tuple<int, T &, U &, V &>(i, A[i], B[i], C[i]); } else if constexpr (indexed && M_SZ == 4) { return tuple<int, T &, U &, V &, W &>(i, A[i], B[i], C[i], D[i]); } else { assert(0); return tuple<int>(i); } } }; template <size_t M_SZ, bool indexed, class T, class U = T, class V = T, class W = T> class vec_merge { vector<T> &a; vector<U> &b; vector<V> &c; vector<W> &d; public: vec_merge(vector<T> &a) : a(a), b(a), c(a), d(a) {} vec_merge(vector<T> &a, vector<U> &b) : a(a), b(b), c(a), d(a) {} vec_merge(vector<T> &a, vector<U> &b, vector<V> &c) : a(a), b(b), c(c), d(a) {} vec_merge(vector<T> &a, vector<U> &b, vector<V> &c, vector<W> &d) : a(a), b(b), c(c), d(d) {} auto begin() const { if constexpr (M_SZ == 1) { return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a}; } else if constexpr (M_SZ == 2) { return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a, b}; } else if constexpr (M_SZ == 3) { return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a, b, c}; } else if constexpr (M_SZ == 4) { return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a, b, c, d}; } else { assert(0); return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{ std::begin(a), a}; } } auto end() const { if constexpr (M_SZ == 1) { return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a}; } else if constexpr (M_SZ == 2) { return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a, b}; } else if constexpr (M_SZ == 3) { return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a, b, c}; } else if constexpr (M_SZ == 4) { return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a, b, c, d}; } else { assert(0); return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{ std::end(a), a}; } } }; #endif #define fora_2(a, A) for (auto &&a : A) #if __cplusplus >= 201703L #define fora_3(i, a, A) \ for (auto [i, a] : vec_merge<1, true, decl_t<decltype(A)>>(A)) #define fora_4(a, b, A, B) \ for (auto [a, b] : \ vec_merge<2, false, decl_t<decltype(A)>, decl_t<decltype(B)>>(A, B)) #define fora_5(i, a, b, A, B) \ for (auto [i, a, b] : \ vec_merge<2, true, decl_t<decltype(A)>, decl_t<decltype(B)>>(A, B)) #define fora_6(a, b, c, A, B, C) \ for (auto [a, b, c] : \ vec_merge<3, false, decl_t<decltype(A)>, decl_t<decltype(B)>, \ decl_t<decltype(C)>>(A, B, C)) #define fora_7(i, a, b, c, A, B, C) \ for (auto [i, a, b, c] : \ vec_merge<3, true, decl_t<decltype(A)>, decl_t<decltype(B)>, \ decl_t<decltype(C)>>(A, B, C)) #define fora_8(a, b, c, d, A, B, C, D) \ for (auto [a, b, c, d] : \ vec_merge<4, false, decl_t<decltype(A)>, decl_t<decltype(B)>, \ decl_t<decltype(C)>, decl_t<decltype(D)>>(A, B, C, D)) #define fora_9(i, a, b, c, d, A, B, C, D) \ for (auto [i, a, b, c, d] : \ vec_merge<4, true, decl_t<decltype(A)>, decl_t<decltype(B)>, \ decl_t<decltype(C)>, decl_t<decltype(D)>>(A, B, C, D)) #endif // 構造化束縛ver // 1e5要素で40ms程度 // 遅いときはfora_fを使う #define fora(...) \ over9(__VA_ARGS__, fora_9, fora_8, fora_7, fora_6, fora_5, fora_4, fora_3, \ fora_2)(__VA_ARGS__) // #define forr(v, a) for(auto&& v : makeReversedRange(a)) // 参照を取らない /*@formatter:off*/ #ifdef use_for template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> auto to1d(vector<vector<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(vector<vector<vector<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<vector<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<vector<vector<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define forv(a, b) for (auto a : to1d(b)) // インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) // 値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #endif #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define forrm(...) \ over5(__VA_ARGS__, forrm5, forrm4, forrm3, forrm2, forrm1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp3(k, v, st) rep(forp_i, sz(st)) #define forp4(forp_i, k, v, st) rep(forp_i, sz(st)) #define forp(...) over4(__VA_ARGS__, forp4, forp3)(__VA_ARGS__) // to_vec(rep(i, N))のように使い // iが走査した値を持つvectorを返す #define to_vec2(type, my_for) \ [&]() { \ vector<type> ret; \ my_for { ret.push_back(i); } \ return ret; \ }() #define to_vec1(my_for) to_vec2(int, my_for) #define to_vec(...) over2(__VA_ARGS__, to_vec2, to_vec1)(__VA_ARGS__) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const double PI = 3.1415926535897932384626433832795029L; constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:off*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec_) { for (T &x : vec_) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec_) { for (ll i = 0; i < vec_.size(); ++i) os << vec_[i] << (i + 1 == vec_.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec_) { for (ll i = 0; i < vec_.size(); ++i) { for (ll j = 0; j < vec_[i].size(); ++j) { os << vec_[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { os << endl; for (auto &&v : m) os << v << endl; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, mset<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) { os << v << " "; } return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec_) { rep(h, sz(vec_)) { rep(w, sz(vec_[0])) { os << vec_[h][w]; } os << endl; } return os; } template <class T> struct range_now { int l; vector<T> A; range_now(vector<T> &&A, int l) : A(A), l(l) {} }; /*@formatter:off*/ // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora_f(v,a)os<<v<<endl;return os;} template <typename W, typename H> void resize(W &vec_, const H head) { vec_.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec_, const H &head, const T... tail) { vec_.resize(head); for (auto &v : vec_) resize(v, tail...); } // #define use_for_each //_each _all_of _any_of _none_of _find_if _rfind_if // _contains _count_if _erase_if _entry_if #ifdef use_for_each // todo Atcoderの過去問がc++17に対応したら #if __cplusplus >= 201703L // for_each以外はconst & (呼び出し側のラムダも) template <typename T, typename F> bool all_of2(const T &v, F f) { if constexpr (has_value_type<T>::value) { for (auto &&v_ : v) { if (!all_of2(v_, f)) return false; } return true; } else { return f(v); } } template <typename T, typename F> bool any_of2(const T &v, F f) { if constexpr (has_value_type<T>::value) { for (auto &&v_ : v) { if (!any_of2(v_, f)) return true; } return false; } else { return f(v); } } template <typename T, typename F> bool none_of2(const T &v, F f) { if constexpr (has_value_type<T>::value) { for (auto &&v_ : v) { if (none_of2(v_, f)) return false; } return true; } else { return f(v); } } // 存在しない場合 // 1次元 Nを返す // 多次元-1を返す template <typename T, typename F> ll find_if2(const vector<T> &v, F f) { rep(i, sz(v)) { if (f(v[i])) return i; } return sz(v); } template <typename T, typename F> tuple<int, int> find_if2(const vector<vector<T>> &v, F f) { rep(i, sz(v)) { rep(j, sz(v[i])) { if (f(v[i][j])) { return tuple<int, int>(i, j); } } } return tuple<int, int>(-1, -1); } template <typename T, typename F> auto find_if2(const vector<vector<vector<T>>> &v, F f) { rep(i, sz(v)) { if (auto ret = find_if2(v[i], f); get<0>(ret) != -1) { return tuple_cat(tuple<int>(i), ret); } } auto bad = tuple_cat(tuple<int>(-1), find_if2(v[0], f)); return bad; } template <class T, class F> auto find_if2(const range_now<T> &v, F f) { return find_if2(v.A, f) + v.l; } // 存在しない場合 // 1次元 -1を返す // 多次元-1を返す template <typename T, typename F> ll rfind_if2(const vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return i; } return -1; } template <typename T, typename F> tuple<int, int> rfind_if2(const vector<vector<T>> &v, F f) { rer(i, sz(v) - 1) { rer(j, sz(v[i]) - 1) { if (f(v[i][j])) { return tuple<int, int>(i, j); } } } return tuple<int, int>(-1, -1); } template <typename T, typename F> auto rfind_if2(const vector<vector<vector<T>>> &v, F f) { rer(i, sz(v) - 1) { if (auto ret = rfind_if2(v[i], f); get<0>(ret) != -1) { return tuple_cat(tuple<int>(i), ret); } } auto bad = tuple_cat(tuple<int>(-1), rfind_if2(v[0], f)); return bad; } // todo まとめられそう string,vector全般 template <class T> bool contains(const string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(const vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(const vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(const T &v, F f) { if constexpr (has_value_type<T>::value) { ll ret = 0; for (auto &&v_ : v) { ret += count_if2(v_, f); } return ret; } else { return f(v); } } template <typename T, typename F> void for_each2(T &a, F f) { if constexpr (has_value_type<T>::value) { for (auto &&v_ : a) for_each2(v_, f); } else { f(a); } } #else template <typename T, typename F> bool all_of2(const T &v, F f) { return f(v); } template <typename T, typename F> bool all_of2(const vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool any_of2(const T &v, F f) { return f(v); } template <typename T, typename F> bool any_of2(const vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f)) return true; } return false; } template <typename T, typename F> bool none_of2(const T &v, F f) { return f(v); } template <typename T, typename F> bool none_of2(const vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool find_if2(const T &v, F f) { return f(v); } template <typename T, typename F> ll find_if2(const vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f)) return i; } return sz(v); } template <typename T, typename F> bool rfind_if2(const T &v, F f) { return f(v); } template <typename T, typename F> ll rfind_if2(const vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f)) return i; } return -1; } template <class T> bool contains(const string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(const vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(const vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(const T &v, F f) { return f(v); } template <typename T, typename F> ll count_if2(const vector<T> &vec_, F f) { ll ret = 0; fora(v, vec_) { ret += count_if2(v, f); } return ret; } template <typename T, typename F> void for_each2(T &a, F f) { f(a); } template <typename T, typename F> void for_each2(vector<T> &a, F f) { for (auto &&v_ : a) for_each2(v_, f); } #endif template <typename W> ll count_od(const vector<W> &a) { return count_if2(a, [](ll v) { return v & 1; }); } template <typename W> ll count_ev(const vector<W> &a) { return count_if2(a, [](ll v) { return !(v & 1); }); } // 削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(const vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(const vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } template <typename T, typename F> vector<T> entry_if2(const vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> entry_if2(const vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = entry_if2(v[i], f); } return res; } template <typename T, typename F> ll l_rfind_if(const vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return i; } return -1; } template <typename T, typename F> bool l_contains_if(const vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return true; } return false; } template <class A, class B, class C> auto t_all_of(A a, B b, C c) { return std::all_of(a, b, c); } template <class A, class B, class C> auto t_any_of(A a, B b, C c) { return std::any_of(a, b, c); } template <class A, class B, class C> auto t_none_of(A a, B b, C c) { return std::none_of(a, b, c); } template <class A, class B, class C> auto t_find_if(A a, B b, C c) { return std::find_if(a, b, c); } template <class A, class B, class C> auto t_count_if(A a, B b, C c) { return std::count_if(a, b, c); } #define all_of_s__2(a, right) (t_all_of(ALL(a), lamr(right))) #define all_of_s__3(a, v, siki) (t_all_of(ALL(a), [&](auto v) { return siki; })) #define all_of_s(...) over3(__VA_ARGS__, all_of_s__3, all_of_s__2)(__VA_ARGS__) // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lamr(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of_s__2(a, right) (t_any_of(ALL(a), lamr(right))) #define any_of_s__3(a, v, siki) (t_any_of(ALL(a), [&](auto v) { return siki; })) #define any_of_s(...) over3(__VA_ARGS__, any_of_s__3, any_of_s__2)(__VA_ARGS__) #define any_of__2(a, right) any_of2(a, lamr(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of_s__2(a, right) (t_none_of(ALL(a), lamr(right))) #define none_of_s__3(a, v, siki) \ (t_none_of(ALL(a), [&](auto v) { return siki; })) #define none_of_s(...) \ over3(__VA_ARGS__, none_of_s__3, none_of_s__2)(__VA_ARGS__) #define none_of__2(a, right) none_of2(a, lamr(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if_s__2(a, right) (t_find_if(ALL(a), lamr(right)) - a.begin()) #define find_if_s__3(a, v, siki) \ (t_find_if(ALL(a), [&](auto v) { return siki; }) - a.begin()) #define find_if_s(...) \ over3(__VA_ARGS__, find_if_s__3, find_if_s__2)(__VA_ARGS__) #define find_if__2(a, right) find_if2(a, lamr(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if__4(a, l, v, siki) \ (find_if2(decltype(a)(a.begin() + l, a.end()), \ [&](auto v) { return siki; }) + \ l) #define find_if(...) \ over4(__VA_ARGS__, find_if__4, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if_s__2(a, right) l_rfind_if(a, lamr(right)) #define rfind_if_s__3(a, v, siki) l_rfind_if(a, [&](auto v) { return siki; }) #define rfind_if_s(...) \ over3(__VA_ARGS__, rfind_if_s__3, rfind_if_s__2)(__VA_ARGS__) #define rfind_if__2(a, right) rfind_if2(a, lamr(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if_s__2(a, right) l_contains_if(a, lamr(right)) #define contains_if_s__3(a, v, siki) \ l_contains_if(a, [&](auto v) { return siki; }) #define contains_if_s(...) \ over3(__VA_ARGS__, contains_if_s__3, contains_if_s__2)(__VA_ARGS__) #define contains_if__2(a, right) contains_if2(a, lamr(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if_s__2(a, right) (t_count_if(ALL(a), lamr(right))) #define count_if_s__3(a, v, siki) \ (t_count_if(ALL(a), [&](auto v) { return siki; })) #define count_if_s(...) \ over3(__VA_ARGS__, count_if_s__3, count_if_s__2)(__VA_ARGS__) #define count_if__2(a, right) count_if2(a, lamr(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) // vector<vi>で、viに対して操作 #define for_each_s__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each_s__3(a, v, shori) \ do { \ fora(v, a) { shori; } \ } while (0) #define for_each_s(...) \ over3(__VA_ARGS__, for_each_s__3, for_each_s__2)(__VA_ARGS__) // vector<vi>で、intに対して操作 #define for_each__2(a, right) for_each2(a, lamr(right)) #define for_each__3(a, v, shori) for_each2(a, [&](auto &v) { shori; }) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) for_each2(a, f); template <class T, class F> vector<T> help_for_eached(const vector<T> &A, F f) { vector<T> ret = A; for_each(ret, v, f(v)); return ret; } #define for_eached__2(a, right) help_for_eached(a, lamr(right)) #define for_eached__3(a, v, shori) help_for_eached(a, lam(v, shori)) #define for_eached(...) \ over3(__VA_ARGS__, for_eached__3, for_eached__2)(__VA_ARGS__) #define for_eached_f(a, f) for_eached2(a, f); #define each for_each #define eached for_eached // #define erase_if_s__2(a, right) l_erase_if2(a,lamr(right)) // #define erase_if_s__3(a, v, siki) l_erase_if2(a,[&](auto v){return siki;}) // #define erase_if_s(...) // over3(__VA_ARGS__,erase_if_s__3,erase_if_s__2)(__VA_ARGS__) #define erase_if__2(a, right) erase_if2(a, lamr(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) // #define entry_if_s__2(a, right) l_entry_if2(a,lamr(right)) // #define entry_if_s__3(a, v, siki) l_entry_if2(a,[&](auto v){return siki;}) // #define entry_if_s(...) // over3(__VA_ARGS__,entry_if_s__3,entry_if_s__2)(__VA_ARGS__) #define entry_if__2(a, right) entry_if2(a, lamr(right)) #define entry_if__3(a, v, siki) entry_if2(a, [&](auto v) { return siki; }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) entry_if2(a, f) #endif /*@formatter:off*/ template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(ALL(a), key), a.end()); } void replace(str &a, char key, char v) { replace(ALL(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } void replace(str &a) { int dec = 0; if ('a' <= a[0] && a[0] <= 'z') dec = 'a'; if ('A' <= a[0] && a[0] <= 'Z') dec = 'A'; fora(v, a) { v -= dec; } } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(ALL(a), ALL(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(ALL(a)); } vi iota(ll s, ll len) { vi ve(len); iota(ALL(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } /*@formatter:off*/ template <class T> void sort(vector<T> &a, int l = -1, int r = -1) { set_lr12(l, r, sz(a)); fast_sort(a.begin() + l, a.begin() + r); } template <class T> void rsort(vector<T> &a, int l = -1, int r = -1) { set_lr12(l, r, sz(a)); fast_sort(a.begin() + l, a.begin() + r, greater<T>()); }; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } template <class... T, class U> auto rsorted(U head, T... a) { rsort(head, a...); return head; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } // #define use_sort #ifdef use_sort enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(ALL(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(ALL(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(ALL(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(ALL(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(ALL(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(ALL(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(ALL(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(ALL(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(ALL(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(ALL(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(ALL(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(ALL(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(ALL(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(ALL(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } /*@formatter:off*/ void sort(string &a) { sort(ALL(a)); } void rsort(string &a) { sort(RALL(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(ALL(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(ALL(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> // 例えばreturn p.fi + p.se; template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(ALL(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } #endif template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(c); sort(d); return includes(ALL(c), ALL(d)); } template <class T> bool distinct(const vector<T> &A) { if ((int)(A).size() == 1) return true; if ((int)(A).size() == 2) return A[0] != A[1]; if ((int)(A).size() == 3) return (A[0] != A[1] && A[1] != A[2] && A[0] != A[2]); auto B = A; sort(B); int N = (B.size()); unique(B); return N == (int)(B.size()); } template <class H, class... T> bool distinct(const H &a, const T &...b) { return distinct(vector<H>{a, b...}); } /*@formatter:off*/ template <class T, class U> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U = int> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U = int> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool san(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return san(0, i, sz(a)) && san(0, j, sz(a)); } #define inside ins ll u0(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u0(vector<T> &a) { vector<T> ret = a; fora(v, ret) { v = u(v); } return ret; } // todo 名前 bool d_(int a, int b) { if (b == 0) return false; return (a % b) == 0; } // エラー void ole() { #ifdef _DEBUG cerr << "ole" << endl; exit(0); #endif string a = "a"; rep(i, 30) a += a; rep(i, 1 << 17) cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void re(string s = "") { cerr << s << endl; assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } template <class T> T in() { T ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } // value_typeを持つ場合呼べる // len回要素を追加する template <class Iterable, class T = typename Iterable::value_type> Iterable tin(int len) { Iterable ret; T tem; while (len--) { cin >> tem; ret += tem; } return ret; } template <class T> T tin() { T ret; cin >> ret; return ret; } template <class T> T tind(int len = 0) { auto ret = tin<T>(len); dec(ret, 1); return ret; } #define din_t2(type, a) \ type a; \ cin >> a #define din_t3(type, a, b) \ type a, b; \ cin >> a >> b #define din_t4(type, a, b, c) \ type a, b, c; \ cin >> a >> b >> c #define din_t5(type, a, b, c, d) \ type a, b, c, d; \ cin >> a >> b >> c >> d #define din_t6(type, a, b, c, d, e) \ type a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din_t7(type, a, b, c, d, e, f) \ type a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din_t(...) \ over7(__VA_ARGS__, din_t7, din_t6, din_t5, din_t4, din_t3, \ din_t2)(__VA_ARGS__) #define din(...) din_t(int, __VA_ARGS__) #define d_in #define dsig(...) din_t(signed, __VA_ARGS__) #define dst(...) din_t(string, __VA_ARGS__) #define dstr dst #define d_str dst #define dcha(...) din_t(char, __VA_ARGS__) #define dchar dcha #define ddou(...) din_t(double, __VA_ARGS__) #define din1d(a) \ din_t2(int, a); \ a-- #define din2d(a, b) \ din_t3(int, a, b); \ a--, b-- #define din3d(a, b, c) \ din_t4(int, a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din_t5(int, a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) /*@formatter:off*/ #ifdef _DEBUG template <class T> void err2(T &&head) { cerr << head; } template <class T, class... U> void err2(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); } template <class T, class... U> void err(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); cerr << "" << endl; } template <class T> void err(T &&head) { cerr << head << endl; } void err() { cerr << "" << endl; } // debで出力する最大長 constexpr int DEB_LEN = 20; constexpr int DEB_LEN_H = 12; string deb_tos(const int &v) { if (abs(v) == inf || abs(v) == linf) return "e"; else return to_string(v); } template <class T> string deb_tos(const T &a) { stringstream ss; ss << a; return ss.str(); } #ifdef use_epsdou string deb_tos(const epsdou &a) { return deb_tos(a.v); } #endif template <class T> string deb_tos(const optional<T> &a) { if (a.has_value()) { return deb_tos(a.value()); } else return "e"; } template <class T> string deb_tos(const vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), DEB_LEN); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << deb_tos(a[i]); if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:on*/ template <class T> string deb_tos(const vector<vector<T>> &a, vi H, vi W, int key = -1) { stringstream ss; ss << endl; vi lens(sz(W)); fora(h, H) { rep(wi, sz(W)) { if (sz(a[h]) <= W[wi]) break; lens[wi] = max(lens[wi], sz(deb_tos(a[h][W[wi]])) + 1); lens[wi] = max(lens[wi], sz(deb_tos(W[wi])) + 1); } } if (key == -1) ss << " *|"; else ss << " " << key << "|"; int wi = 0; fora(w, W) { ss << std::right << std::setw(lens[wi]) << w; wi++; } ss << "" << endl; rep(i, sz(W)) rep(lens[i]) ss << "_"; rep(i, 3) ss << "_"; ss << "" << endl; fora(h, H) { ss << std::right << std::setw(2) << h << "|"; int wi = 0; fora(w, W) { if (sz(a[h]) <= w) break; ss << std::right << std::setw(lens[wi]) << deb_tos(a[h][w]); wi++; } ss << "" << endl; } return ss.str(); } template <class T> string deb_tos(const vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = (H != inf) ? H : min({H, sz(a), DEB_LEN_H}); W = min({W, sz(a[0]), DEB_LEN_H}); vi hs, ws; rep(h, H) { hs.push_back(h); } rep(w, W) { ws.push_back(w); } return deb_tos(a, hs, ws, key); } /*@formatter:off*/ template <class T> string deb_tos(const vector<vector<vector<T>>> &a, ll H = inf) { stringstream ss; if (H == inf) H = DEB_LEN_H; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << deb_tos(a[i], inf, inf, i); } return ss.str(); } template <class T> string deb_tos(vector<set<T>> &a, ll H = inf, ll W = inf, int key = -1) { vector<vector<T>> b(sz(a)); rep(i, sz(a)) { fora(v, a[i]) { b[i].push_back(v); } } return deb_tos(b, H, W, key); } template <class T, size_t A> string deb_tos(T (&a)[A]) { return deb_tos(vector<T>(begin(a), end(a))); } template <class T, size_t A, size_t B> string deb_tos(T (&a)[A][B]) { return deb_tos(vector<vector<T>>(begin(a), end(a))); } template <class T, size_t A, size_t B, size_t C> string deb_tos(T (&a)[A][B][C]) { return deb_tos(vector<vector<vector<T>>>(begin(a), end(a))); } /*@formatter:off*/ template <class T> void out2(T head) { cout << head; res_mes += deb_tos(head); } template <class T, class... U> void out2(T head, U... tail) { cout << head << " "; res_mes += deb_tos(head) + " "; out2(tail...); } template <class T, class... U> void out(T head, U... tail) { cout << head << " "; res_mes += deb_tos(head) + " "; out2(tail...); cout << "" << endl; res_mes += "\n"; } template <class T> void out(T head) { cout << head << endl; res_mes += deb_tos(head) + "\n"; } void out() { cout << "" << endl; } #else #define err(...) ; template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #endif template <class T> void outl(const vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } // テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } /*@formatter:off*/ template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } template <class T> void na(set<T> &a, ll n) { rep(i, n) a.insert(in()); } #define dna(a, n) \ vi a; \ na(a, n); /*nを複数使うと n==in()の時バグる事に注意*/ #define dnad(a, n) \ vi a; \ nad(a, n); template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T> void nad(set<T> &a, ll n) { rep(i, n) a.insert(in() - 1); } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } template <class T, class U> void na2(set<T> &a, set<U> &b, ll n) { rep(i, n) { a.insert(in()); b.insert(in()); } } #define dna2(a, b, n) \ vi a, b; \ na2(a, b, n); template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } #define dna2d(a, b, n) \ vi a, b; \ na2d(a, b, n); template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a, b, c; \ na3(a, b, c, n); template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--; } #define dna3d(a, b, c, n) \ vi a, b, c; \ na3d(a, b, c, n); template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a, b, c, d; \ na4(a, b, c, d, n); #define dna4d(a, b, c, d, n) \ vi a, b, c, d; \ na4d(a, b, c, d, n); #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; #define dnt(S, h, w) \ vvi(S, h, w); \ nt(S, h, w); #define dntc(S, h, w) \ vvc(S, h, w); \ nt(S, h, w); #define dnts(S, h, w) \ vvs(S, h, w); \ nt(S, h, w); // デバッグ #define sp << " " << /*@formatter:off*/ #define deb1(x) debugName(x) << " = " << deb_tos(x) #define deb_2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__) #define deb_3(x, ...) deb1(x) << ", " << deb_2(__VA_ARGS__) #define deb_4(x, ...) deb1(x) << ", " << deb_3(__VA_ARGS__) #define deb5(x, ...) deb1(x) << ", " << deb_4(__VA_ARGS__) #define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__) // #define deb7(x, ...) deb1(x) <<", "<< deb6(__VA_ARGS__) // #define deb8(x, ...) deb1(x) <<", "<< deb7(__VA_ARGS__) // #define deb9(x, ...) deb1(x) <<", "<< deb8(__VA_ARGS__) // #define deb10(x, ...) deb1(x) <<", "<< deb9(__VA_ARGS__) /*@formatter:off*/ #ifdef _DEBUG bool was_deb = false; #define deb(...) \ do { \ was_deb = true; \ cerr << over10(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb_4, \ deb_3, deb_2, deb1)(__VA_ARGS__) \ << endl; \ } while (0) void print_n_base(int x, int base) { string S; while (x) { S += (char)('0' + x % base); x /= base; } cerr << S << endl; } template <class T> void print_n_base(vector<T> X, int base) { cerr << endl; for (auto &&x : X) { print_n_base(x, base); } cerr << endl; } // n進数 #define deb2(x) \ was_deb = true; \ cerr << debugName(x) << " = "; \ print_n_base(x, 2); #define deb3(x) \ was_deb = true; \ cerr << debugName(x) << " = "; \ print_n_base(x, 3); #define deb4(x) \ was_deb = true; \ cerr << debugName(x) << " = "; \ print_n_base(x, 4); #define deb_ex_deb(x, len) debugName(x) << " = " << deb_tos(x, len) #define call_deb_ex_deb(x, len) deb_ex_deb(x, len) // 要素が存在する行だけ出力(vvt) #define deb_ex(v) \ do { \ int N = sz(v); \ int s = N; \ int t = 0; \ rep(i, N) { \ if (sz(v[i])) { \ chmi(s, i); \ chma(t, i); \ } \ } \ auto ex_v = sub(v, s, N); \ str S = deb_tos(ex_v, sz(ex_v)); \ debugName(v); \ cerr << " = " << endl; \ cerr << S << endl; \ } while (0); #define debi(A) \ { \ int len = min(sz(A), 20); \ was_deb = true; \ cerr << debugName(A) << " = " << endl; \ rep(i, len) cerr << std::right \ << std::setw((int)(sz(tos(A[i])) + (i ? 1 : 0))) \ << (i % 10); \ cerr << endl; \ rep(i, len) cerr << std::right \ << std::setw((int)(sz(tos(A[i])) + (i ? 1 : 0))) << A[i]; \ cerr << endl; \ } template <class T, class F> string deb_tos_f(vector<vector<T>> &a, F f, int key = -1) { vi hs, ws_; int H = sz(a), W = sz(a[0]); vi exh(H), exw(W); rep(h, H) { rep(w, W) { if (f(a[h][w])) { exh[h] = true; exw[w] = true; } } } rep(h, H) if (exh[h]) hs.push_back(h); rep(w, W) if (exw[w]) ws_.push_back(w); return deb_tos(a, hs, ws_, key); } template <class T, class F> string deb_tos_f(vector<vector<vector<T>>> &a, F f) { stringstream ss; int H = sz(a); if (sz(a) == 0) return ss.str(); rep(i, H) { ss << deb_tos_f(a[i], f, i); } ss << "" << endl; return ss.str(); } #define debf_normal(tab, f) \ do { \ cerr << debugName(tab) << " = " << endl; \ cerr << deb_tos_f(tab, f) << endl; \ } while (0); #define debf2(tab, siki_r) debf_normal(tab, lamr(siki_r)) #define debf3(tab, v, siki) debf_normal(tab, lam(siki)) // S, sikir // S, v, siki #define debf(...) over3(__VA_ARGS__, debf3, debf2, debf1)(__VA_ARGS__) #else #define deb(...) ; #define deb2(...) ; #define deb3(...) ; #define deb4(...) ; #define deb_ex(...) ; #define debf(...) ; #define debi(...) ; #endif #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' /*@formatter:off*/ using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; using bint = __int128; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } __int128 to_bint(string &s) { __int128 ret = 0; for (ll i = 0; i < (ll)s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } void operator>>(istream &iss, bint &v) { string S; iss >> S; v = 0; rep(i, sz(S)) { v *= 10; v += S[i] - '0'; } } // 便利関数 /*@formatter:off*/ // テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } ll rand(ll max) { return rand(0, max); } template <class T> T rand(vector<T> &A) { return A[rand(sz(A) - 1)]; } // 重複することがある template <class T> vector<T> ranv(vector<T> &A, int N) { vector<T> ret(N); rep(i, N) { ret[i] = rand(A); } return ret; } template <class T> vector<T> ranv_unique(vector<T> &A, int N) { vector<T> ret(N); umapi was; rep(j, N) { int i; while (1) { i = rand(sz(A) - 1); if (was.find(i) == was.end()) break; } ret[j] = A[i]; was[i] = 1; } return ret; } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } /*@formatter:off*/ #ifdef _DEBUG bool timeup(int time) { static bool never = true; if (never) message += "may timeup, because slow"; never = false; auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } #else bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } #endif void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う // 全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:off*/ // #define use_rand #ifdef use_rand str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } // 単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } // 便利 汎用 #endif void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class Iterable, class T = typename Iterable::value_type> vector<pair<T, int>> run_length(const Iterable &a) { vector<pair<T, int>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } /*@formatter:off*/ // #define use_mgr //_goldd _goldt #ifdef use_mgr //->[i, f(i)] template <class T, class U, class F> auto mgr(T ok, U ng, const F &f, require_arg(is_integral<T>::value &&is_integral<U>::value)) { auto mid = (ok + ng); if (ok < ng) while (ng - ok > 1) { mid = (ok + ng) >> 1; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { mid = (ok + ng) >> 1; if (f(mid)) ok = mid; else ng = mid; } return ok; } //[l, r)の中で,f(i)がtrueとなる範囲を返す okはそこに含まれる template <class F> P mgr_range(int l, int r, F f, int ok) { if (f(ok) == 0) { out("f(ok) must true"); re(); } return mp(mgr(ok, l - 1, f), mgr(ok, r, f) + 1); } template <class F> auto mgrd(dou ok, dou ng, F f, int kai = 100) { if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } template <class F> dou mgrd_time(dou ok, dou ng, F f, int time = 1980) { bool han = true; if (ok < ng) while (1) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); if (timeup(time)) { break; } } else while (1) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); if (timeup(time)) { break; } } return ok; } // todo 減らす template <class F> auto goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX<ll>(); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return make_tuple(resIndex, calc(resIndex)); } template <class F> auto goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN<ll>(); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return make_tuple(resIndex, calc(resIndex)); } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> auto goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return make_tuple(left, calc(left)); } template <class F> auto goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return make_tuple(left, calc(left)); } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> auto goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX<ll>(); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return make_tuple(mini, calc(mini)); } template <class F> auto goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN<ll>(); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return make_tuple(maxi, calc(maxi)); } template <class F> auto goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX<dou>(); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return make_tuple(mini, calc(mini)); } template <class F> auto goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN<dou>(); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return make_tuple(maxi, calc(maxi)); } #endif // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } // エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } // getid(find())を返す 1次元にする template <typename W, typename T> int findi(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return i * sz(a[0]) + j; return -1; } template <typename W, typename U> tuple<int, int, int> find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return tuple<int, int, int>(i, j, k); return tuple<int, int, int>(-1, -1, -1); } // 無ければ-1 int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } // N箇所について右のkeyの場所を返す template <typename W, typename T> vi finds(const W &a, const T &key) { int n = sz(a); vi rpos(n, -1); rer(i, n - 1) { if (i < n - 1) { rpos[i] = rpos[i + 1]; } if (a[i] == key) rpos[i] = i; } return rpos; } template <typename W, typename T> vi rfinds(const W &a, const T &key) { int n = sz(a); vi lpos(n, -1); rep(i, n) { if (i > 0) { lpos[i] = lpos[i - 1]; } if (a[i] == key) lpos[i] = i; } return lpos; } // todoz #if __cplusplus >= 201703L template <typename W, typename T, class Iterable = typename W::value_type> ll count(const W &a, const T &k) { return count_if(a, == k); } template <typename W, class Iterable = typename W::value_type> vi count(const W &a) { vi res; for_each(a, v, if (sz(res) <= (int)v) res.resize((int)v + 1); res[v]++;); return res; } #endif ll count(const str &a, const str &k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } /*@formatter:off*/ //'a' = 'A' = 0 として集計 既に-'a'されていても動く vi count(str &a, int l, int r) { vi cou(26); char c = 'a'; if ('A' <= a[l] && a[l] <= 'Z') c = 'A'; if ('a' <= a[l] && a[l] <= 'z') c = 'a'; else c = 0; rep(i, l, r)++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> auto rev(const vector<T> &a) { auto b = a; reverse(ALL(b)); return b; } /* \反転 */ template <class U> auto rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; return b; } /* |反転 */ template <class U> auto revw(vector<vector<U>> &a) { vector<vector<U>> b(sz(a), vector<U>(sz(a[0]))); int W = sz(a[0]); rep(h, sz(a)) rep(w, sz(a[0])) { b[h][W - 1 - w] = a[h][w]; } return b; } /* ー反転 */ template <class U> auto revh(vector<vector<U>> &a) { vector<vector<U>> b(sz(a), vector<U>(sz(a[0]))); int H = sz(a); rep(h, sz(a)) rep(w, sz(a[0])) { b[H - 1 - h][w] = a[h][w]; } return b; } /* /反転 */ template <class U> auto revr(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); int H = sz(a); int W = sz(a[0]); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[H - 1 - h][W - 1 - w]; return b; } auto rev(const string &a) { string b = a; reverse(ALL(b)); return b; } template <class T> auto rev(const T &v, int i) { return v[sz(v) - 1 - i]; } int rev(int N, int i) { return N - 1 - i; } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; // 0は0桁 ll keta(ll v, int if_zero_res) { if (!v) return if_zero_res; if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } #if __cplusplus >= 201703L ll getr(ll a, ll keta) { return (a / pow<ll>(10, keta)) % 10; } #else ll getr(ll a, ll keta) { return (a / (int)pow(10, keta)) % 10; } #endif // 上から何桁目か ll getl(ll a, ll ket) { int sketa = keta(a, 1); return getr(a, sketa - 1 - ket); } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } // 変換系 template <class T, class U> auto to_v1(vector<reference_wrapper<U>> &ret, vector<T> &A) { rep(i, sz(A)) ret.push_back(A[i]); return ret; } template <class T, class U> auto to_v1(vector<reference_wrapper<U>> &ret, vector<vector<T>> &A) { rep(i, sz(A)) to_v1(ret, A[i]); return ret; } // 参照付きで1次元に起こす template <class T> auto to_v1(vector<vector<T>> &A) { vector<reference_wrapper<decl2_t<decltype(A)>>> ret; rep(i, sz(A)) to_v1(ret, A[i]); return ret; } //[v] := iとなるようなvectorを返す // 存在しない物は-1 // 空でも動く(なぜか) template <class T> auto keys(const T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(const T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } // todo 可変長で template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } // 1次元のコンテナ template <class T> int mini(const vector<T> &a) { return min_element(ALL(a)) - a.begin(); } template <class T> int maxi(const vector<T> &a) { return max_element(ALL(a)) - a.begin(); } template <class T> T sum(const T &A) { return A; } template <class T> T sum(const vector<T> &A, int l = -1, int r = -1) { T s = 0; set_lr12(l, r, sz(A)); rep(i, l, r) s += sum(A[i]); return s; } // 1次元のIterableに使える template <class T, require_t(!has_value_type<T>::value)> T min(T &a) { return a; } template <class V, require_t(has_value_type<V>::value)> auto min(V &a, ll s = -1, ll n = -1) { using Ret_t = decl2_t<V>; auto ret = MAX<Ret_t>(); set_lr12(s, n, sz(a)); if (s == n) { return INF<Ret_t>(); } else { rep(i, s, n) { ret = min(ret, min(a[i])); } return ret; } } template <class T, require_t(!has_value_type<T>::value)> T max(T &a) { return a; } template <class V, require_t(has_value_type<V>::value)> auto max(V &a, ll s = -1, ll n = -1) { using Ret_t = decl2_t<V>; auto ret = MIN<Ret_t>(); set_lr12(s, n, sz(a)); if (s == n) { return -INF<Ret_t>(); } else { rep(i, s, n) { ret = max(ret, max(a[i])); } return ret; } } template <class T> T mul(vector<T> &v, ll t = inf) { T ret = v[0]; rep(i, 1, min(t, sz(v))) ret *= v[i]; return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret // += sum(v[i], tail...); return ret;} indexを持つvectorを返す /*@formatter:off*/ template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<W> &xx, const T v, int s, ll t) { rep(i, s, t) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, T v, int sh, int th, int sw, int tw) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } // #define use_fill //_sum _array _max _min #ifdef use_fill template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N) { fill(a[i], v); } } template <class T, class U> void fill(vector<T> &a, const vi &ind, U val) { fora(v, ind) { a[v] = val; } } template <typename A, size_t N> auto sum(A (&a)[N], int l = -1, int r = -1) { set_lr12(l, r, N); auto res = sum(a[l]); if (l == r) { return res - res; } rep(i, l + 1, r) res += sum(a[i]); return res; } template <typename A, size_t N> auto max(A (&a)[N], int l = -1, int r = -1) { set_lr12(l, r, N); auto res = max(a[l]); if (l == r) { return -INF<decltype(res)>(); } rep(i, l + 1, r) res = max(res, max(a[i])); return res; } template <typename A, size_t N> auto min(A (&a)[N], int l = -1, int r = -1) { set_lr12(l, r, N); auto res = min(a[l]); if (l == r) { return INF<decltype(res)>(); } rep(i, l + 1, r) res = min(res, min(a[i])); return res; } #endif vi inds_(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { assert(ret[a[i]] == -1); ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } // template<class T> T *negarr(ll size) { T *body = (T *) malloc((size * 2 + // 1) * sizeof(T)); return body + size;} template<class T> T *negarr2(ll h, // ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new // double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = // 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double // **a = dummy1 + h; return a;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r = inf) { return operator()(0, min(r, sz(rui) - 1)); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> string deb_tos(const ruiC<T> &a) { return deb_tos(a.rui); } template <class T> ostream &operator<<(ostream &os, ruiC<T> a) { fora(v, a.rui) { os << v << " "; } return os; } template <class T> vector<T> ruiv(const vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i]; return ret; } template <class T> ruiC<T> ruic(const vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } template <class T> ruiC<T> ruic() { return ruiC<T>(); } // imoは0-indexed // ruiは1-indexed template <class T> vector<T> imo(const vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } // #define use_rui //_imo _ruic _ruiv #ifdef use_rui // kと同じものの数 template <class T, class U> vi imo(const vector<T> &a, U k) { vi equ(sz(a)); rep(i, sz(a)) { equ[i] = a[i] == k; } return imo(equ); } template <class T> vector<T> imox(const vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i]; return ret; } // 漸化的に最小を持つ template <class T> vector<T> imi(const vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> ima(const vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> rimi(const vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chmi(ret[i - 1], ret[i]); return ret; } template <class T> vector<T> rima(const vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chma(ret[i - 1], ret[i]); return ret; } template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN<T>(), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<int> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; T ma = MIN<T>(); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -INF<T>(); ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T getv(int r = inf) { return getv(0, min(r, n)); }; int geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } int geti(int r = inf) { return geti(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX<T>(), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<int> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; T mi = MAX<T>(); rv.resize(n + 1); ri.resize(n + 1); rv[0] = INF<T>(); ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T getv(int r = inf) { return getv(0, min(r, n)); }; int geti(int l, int r) { { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } int geti(int r = inf) { return geti(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; /*@formatter:off*/ vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return ret; } // kと同じものの数 template <class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k); return ret; } template <class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret); } template <class T> struct ruiC2 { int H; vector<ruiC<T>> rui; ruiC<T> dummy; // 変なのをよばれたときはこれを返す//todo ruiC2(const vector<vector<T>> &ru) : rui(sz(ru)), H(sz(ru)) { for (int h = 0; h < H; h++) { if (sz(ru[h]) == 0) continue; if (sz(dummy) == 1) dummy = ruic(vector<T>(sz(ru[h]))); rui[h] = ruic(ru[h]); } } // WについてHを返す vector<T> operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } vector<T> res(H); for (int h = 0; h < H; h++) res[h] = rui[h](l, r); return res; } // HについてWを返す ruiC<T> &operator[](ll h) { #ifdef _DEBUG if (h >= H) { message += "warning ruiC h >= H"; } #endif if (h >= H || sz(rui[h]) == 1) return dummy; else return rui[h]; } /*@formatter:off*/ // vector<T> operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } /*0から順に追加される必要がある*/ // T back() { return rui.back(); } // ll size() { return rui.size(); } // auto begin(){return rui.begin();} // auto end(){return rui.end();} }; template <class T, class U> ruiC<ll> ruicou(vector<T> &a, U b) { vi cou(sz(a)); rep(i, sz(a)) { cou[i] = a[i] == b; } return ruic(cou); } // メモリは形式によらず(26*N) // rui(l,r)でvector(26文字について, l~rのcの個数) // rui[h] ruic()を返す // 添え字は'a', 'A'のまま扱う // (予め-='a','A'されているものが渡されたらそれに従う) template <typename Iterable, class is_Iterable = typename Iterable::value_type> ruiC2<ll> ruicou(const Iterable &a) { int H = max(a) + 1; vvi(cou, H); rep(i, sz(a)) { if (sz(cou[a[i]]) == 0) cou[a[i]].resize(sz(a)); cou[a[i]][i] = 1; } return ruiC2<ll>(cou); } /*@formatter:off*/ // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { vector<T> rui; ruixC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruiXc "; deb(l, r); assert(0); } return rui[r] ^ rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i]; return ret; } template <class T> ruixC<ll> ruixc(vector<T> &a) { vi ret = ruix(a); return ruixC<ll>(ret); } // 差分を返す(累積を取ると元に戻る) // 101なら // 1111を返す // 元の配列で[l, r)へのxorは //[l]と[r]へのxorになる https://atcoder.jp/contests/abc155/tasks/abc155_f vi ruix_diff(vi &A) { int N = sz(A); assert(N); vi res(N + 1); res[0] = A[0]; rep(i, 1, N) { res[i] = A[i - 1] ^ A[i]; } res[N] = A[N - 1]; return res; } template <class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size()) res[i + 1] = res[i] * a[i]; return res; } // 漸化的に最小を1indexで持つ template <class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) { os << v << " "; } return os; } template <class T> string deb_tos(rruiC<T> &a) { return deb_tos(a.rui); } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } // 掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } template <class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1) res[i - 1] = res[i] * a[i]; return res; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #endif constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); // オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } #if __cplusplus >= 201703L ll bget(ll m, ll keta, ll sinsuu) { m /= pow<ll>(sinsuu, keta); return m % sinsuu; } #else ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } #endif constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); // オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } #if __cplusplus >= 201703L ll bit(ll n, ll sinsuu) { return pow<ll>(sinsuu, n); } #else ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } #endif ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll bit(const vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 // bitsetに置き換える感覚 i が立っていたら i bit目を立てる ll bit_bool(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll // 最下位ビット ll lbit(ll n) { assert(n); return n & -n; } ll lbiti(ll n) { assert(n); return log2(n & -n); } // 最上位ビット ll hbit(ll n) { assert(n); n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { assert(n); return log2(hbit(n)); } // ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll // b = 1ll << a; if (b <= n)k += 1ll << i; } return k;} // 初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } /*over*/ #define forbit1_2(i, mas) \ for (int forbitj = !mas ? 0 : lbit(mas), forbitm = mas, \ i = !mas ? 0 : log2(forbitj); \ forbitm; forbitm = forbitm ^ forbitj, \ forbitj = !forbitm ? 1 : lbit(forbitm), i = log2(forbitj)) /*over*/ #define forbit1_3(i, N, mas) \ for (int forbitj = !mas ? 0 : lbit(mas), forbitm = mas, \ i = !mas ? 0 : log2(forbitj); \ forbitm && i < N; forbitm = forbitm ^ forbitj, \ forbitj = !forbitm ? 1 : lbit(forbitm), i = log2(forbitj)) // masの立ってるindexを見る // i, [N], mas #define forbit1(...) over3(__VA_ARGS__, forbit1_3, forbit1_2)(__VA_ARGS__) // masが立っていないindexを見る // i, N, mas #define forbit0(i, N, mas) forbit1(i, mask(N) & (~(mas))) // forsubをスニペットして使う // Mの部分集合(0,M含む)を見る 3^sz(S)個ある #define forsub_all(m, M) for (int m = M; m != -1; m = m == 0 ? -1 : (m - 1) & M) // BASE進数 template <size_t BASE> class base_num { int v; public: base_num(int v = 0) : v(v){}; int operator[](int i) { return bget(v, i, BASE); } void operator++() { v++; } void operator++(signed) { v++; } operator int() { return v; } }; #define base3(mas, lim, BASE) for (base_num<BASE> mas; mas < lim; mas++) #define base2(mas, lim) base3(mas, lim, 2) #define base(...) over3(__VA_ARGS__, base3, base2, base1)(__VA_ARGS__) // aにある物をtrueとする vb bool_(vi a, int n) { vb ret(max(max(a) + 1, n)); rep(i, sz(a)) ret[a[i]] = true; return ret; } char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } res = rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } // 基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } // #define use_compress //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(ALL(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } #ifdef use_compress // ind[i] := i番目に小さい数 // map[v] := vは何番目に小さいか vi compress(vi &a, umapi &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(ALL(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) { b.push_back(v); } fora(v, r) { b.push_back(v); } sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(ALL(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(ALL(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) { b.push_back(v); } fora(v, r) { b.push_back(v); } fora(v, s) { b.push_back(v); } sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(ALL(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(ALL(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(ALL(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) { fora(v, vv) { b.push_back(v); } } sort(b); unique(b); fora(vv, a) { fora(v, vv) { v = lower_bound(ALL(b), v) - b.begin(); } } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) { fora(vv, vvv) { fora(v, vv) { b.push_back(v); } } } sort(b); unique(b); fora(vvv, a) { fora(vv, vvv) { fora(v, vv) { v = lower_bound(ALL(b), v) - b.begin(); } } } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(ALL(b), a[i]) - b.begin(); } } #endif // 要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(ALL(a), v)) #define lowerIndex(a, v) (lower_bound(ALL(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(ALL(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(ALL(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(ALL(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } // todo 消せないか template <class T, class U, class W> T lowerBound(set<T> &a, U v, W banpei) { auto it = a.lower_bound(v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(set<T> &a, U v, W banpei) { auto it = a.upper_bound(v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T rlowerBound(set<T> &a, U v, W banpei) { auto it = a.upper_bound(v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(set<T> &a, U v, W banpei) { auto it = a.lower_bound(v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T lowerBound(mset<T> &a, U v, W banpei) { auto it = a.lower_bound(v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(mset<T> &a, U v, W banpei) { auto it = a.upper_bound(v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T rlowerBound(mset<T> &a, U v, W banpei) { auto it = a.upper_bound(v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(mset<T> &a, U v, W banpei) { auto it = a.lower_bound(v); if (it == a.begin()) return banpei; else { return *(--it); } } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) // 狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX<T>()); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX<T>()); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX<T>()); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX<T>()); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } // 存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } // 便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } // ll pow(ll a) { return a * a; }; template <class T> T fact(int v) { static vector<T> fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.emplace_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } // 二項係数の偶奇を返す int com_mod2(int n, int r) { return n == (r | (n - r)); } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } #define gcd my_gcd ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } #define lcm my_lcm ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } #define hypot my_hypot double hypot(double dx, double dy) { return std::sqrt(dx * dx + dy * dy); } ll sig0(int t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } bint sig0(bint t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } // 第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // b * res <= aを満たす [l, r)を返す div P drange_ika(int a, int b) { P null_p = mp(linf, linf); if (b == 0) { if (a >= 0) { return mp(-linf, linf + 1) /*全て*/; } else { return null_p /*無い*/; } } else { if (a >= 0) { if (b > 0) { return mp(-linf, a / b + 1); } else { return mp(-(a / -b), linf + 1); } } else { if (b > 0) { return mp(-linf, -ceil(-a, b) + 1); } else { return mp(ceil(-a, -b), linf + 1); } } } } // v * v >= aとなる最小のvを返す ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll)std::sqrt(a); while (res * res < a) ++res; return res; } double log(double e, double x) { return log(x) / log(e); } /*@formatter:off*/ // 機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } // todo stringもセットで template <class T> vector<T> sub(const vector<T> &A, int l, int r) { assert(0 <= l && l <= r && r <= sz(A)); vector<T> ret(r - l); std::copy(A.begin() + l, A.begin() + r, ret.begin()); return ret; } template <class T> vector<T> sub(const vector<T> &A, int r) { return sub(A, 0, r); } template <class T> vector<T> subn(const vector<T> &A, int l, int len) { return sub(A, l, l + len); } string sub(string &A, int l, int r) { assert(0 <= l && l <= r && r <= sz(A)); return A.substr(l, r - l); } template <class T, class F> // sub2で呼ぶ vector<T> sub(const vector<vector<T>> &A, int h, int w, int ah, int aw, F f) { vector<T> res; while (0 <= h && h < sz(A) && 0 <= w && w < sz(A[h]) && f(A[h][w])) { res.emplace_back(A[h][w]); h += ah; w += aw; } return res; } template <class T> vector<T> sub(const vector<vector<T>> &A, int h, int w, int ah, int aw) { return sub(A, h, w, ah, aw, [&](T v) { return true; }); } // range_nowを返す(find_ifでしか使われない) #if __cplusplus >= 201703L template <class T> auto subr(const vector<T> &A, int l) { return range_now(vector<T>(A.begin() + l, A.end()), l); } #endif #define sub25(A, h, w, ah, aw) sub(A, h, w, ah, aw) #define sub26(A, h, w, ah, aw, siki_r) \ sub(A, h, w, ah, aw, [&](auto v) { return v siki_r; }) #define sub27(A, h, w, ah, aw, v, siki) \ sub(A, h, w, ah, aw, [&](auto v) { return siki; }) #define sub2(...) over7(__VA_ARGS__, sub27, sub26, sub25)(__VA_ARGS__) constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> int count(set<T> &S, T l, T r) { assert(l < r); auto it = S.lower_bound(l); return it != S.end() && (*it) < r; } // template<class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) // {assert(sz(S) == sz(v));assert(w < sz(S[0]));rep(h, sz(S)) { S[h][w] = v[h]; // }} template <class T> vector<T> geth(vector<vector<T>> &S, int w) { assert(w < sz(S[0])); vector<T> ret(sz(S)); rep(h, sz(S)) { ret[h] = S[h][w]; } return ret; } // vector<bool>[i]は参照を返さないため、こうしないとvb[i] |= // trueがコンパイルエラー vb::reference operator|=(vb::reference a, bool b) { return a = a | b; } vb::reference operator&=(vb::reference a, bool b) { return a = a & b; } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(const pair<T, U> &a, const pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <class T, class U> pair<T, U> operator-(const pair<T, U> &a, const pair<T, U> &b) { return pair<T, U>(a.fi - b.fi, a.se - b.se); } template <class T, class U> pair<T, U> operator-(const pair<T, U> &a) { return pair<T, U>(-a.first, -a.second); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { #ifdef _DEBUG static bool was = false; if (!was) message += "str += 65 is 'A' not \"65\" "; was = true; #endif return lhs + (char)rv; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs = lhs + rv; } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { const int rv2 = rv; return lhs + rv2; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { const int v = rv; lhs += v; } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) { a.push(d); } return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) { a.insert(d); } return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(const vector<T> &a, const vector<T> &b) { vector<T> ret; ret = a; fora(v, b) { ret += v; } return ret; } template <class T> vector<T> &operator+=(vector<T> &a, const vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { for (auto &&bv : b) { a[bv.first] += bv.second; } return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, const U &v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> auto operator+=(uset<T> &a, U v) { return a.insert(v); } template <class T> vector<T> operator%(vector<T> &a, int v) { vi ret(sz(a)); rep(i, sz(a)) { ret[i] = a[i] % v; } return ret; } template <class T> vector<T> operator%=(vector<T> &a, int v) { rep(i, sz(a)) { a[i] %= v; } return a; } vi operator&(vi &a, vi &b) { assert(sz(a) == sz(b)); vi ret(sz(a)); rep(i, sz(a)) { ret[i] = min(a[i], b[i]); } return ret; } template <class T> void operator+=(mset<T> &a, vector<T> &v) { for (auto &&u : v) a.insert(u); } template <class T> void operator+=(set<T> &a, vector<T> &v) { for (auto &&u : v) a.insert(u); } template <class T> void operator+=(vector<T> &a, set<T> &v) { for (auto &&u : v) a.emplace_back(u); } template <class T> void operator+=(vector<T> &a, mset<T> &v) { for (auto &&u : v) a.emplace_back(u); } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } // template<class T, class U> void operator*=(vector<T> &a, U b) { vector<T> // ta = a; rep(b-1){ a+=ta; }} template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <typename T> void pop_front(vector<T> &v) { erase(v, 0); } template <typename T> void entry(vector<T> &v, unsigned ll s, unsigned ll e) { erase(v, e, sz(v)); erase(v, 0, s); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } vector<string> split(const string &a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string &a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } ll stoi(string &s) { return stol(s); } #define assert_yn(yn_v, v) \ ; \ assert(yn_v == 0 || yn_v == v); \ yn_v = v; // 不完全な対策、現状はautohotkeyで対応 int yn_v = 0; void yn(bool a) { assert_yn(yn_v, 1); if (a) cout << "yes" << endl; else cout << "no" << endl; } void fyn(bool a) { assert_yn(yn_v, 1); yn(a); exit(0); } void Yn(bool a) { assert_yn(yn_v, 2); if (a) cout << "Yes" << endl; else cout << "No" << endl; } void fYn(bool a) { assert_yn(yn_v, 2); Yn(a); exit(0); } void YN(bool a) { assert_yn(yn_v, 3); if (a) cout << "YES" << endl; else cout << "NO" << endl; } void fYN(bool a) { assert_yn(yn_v, 3); YN(a); exit(0); } int ab_v = 0; void fAb(bool a) { assert_yn(ab_v, 1); if (a) cout << "Alice" << endl; else cout << "Bob"; } void fAB(bool a) { assert_yn(yn_v, 2); if (a) cout << "ALICE" << endl; else cout << "BOB"; } int pos_v = 0; void Possible(bool a) { assert_yn(pos_v, 1); if (a) cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { assert_yn(pos_v, 2); if (a) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } void fPossible(bool a) { assert_yn(pos_v, 1) Possible(a); exit(0); } void fPOSSIBLE(bool a) { assert_yn(pos_v, 2) POSSIBLE(a); exit(0); } template <typename T> class fixed_point : T { public: explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return T::operator()(*this, std::forward<Args>(args)...); } }; template <typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point<T>{std::forward<T>(t)}; } // 未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename Iterable> vector<Iterable> table(const Iterable &a, int tou = 2) { int N = sz(a); vector<Iterable> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } // 長さn毎に分割する template <typename Iterable> vector<Iterable> table_n(const Iterable &a, int len) { int N = sz(a); vector<Iterable> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } // 縦を返す vi &geth(vvi() & a, int w) { static vi ret; ret.resize(sz(a)); rep(i, sz(a)) { ret[i] = a[i][w]; } return ret; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; // #define pre prev // #define nex next // gra mll pr // 上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] vc atoz = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; vc AtoZ = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; // 右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) int mei_inc8(int h, int w, int H, int W, int i) { while (++i < 8) { if (inside(h + h8[i], w + w8[i], H, W)) return i; } return i; } #define mei8(nh, nw, h, w) \ for (int i = mei_inc8(h, w, H, W, -1), nh = i < 8 ? h + h8[i] : 0, \ nw = i < 8 ? w + w8[i] : 0; \ i < 8; i = mei_inc8(h, w, H, W, i), nh = h + h8[i], nw = w + w8[i]) int mei_incv(int h, int w, int H, int W, int i, vp &p) { while (++i < sz(p)) { if (inside(h + p[i].fi, w + p[i].se, H, W)) return i; } return i; } #define meiv(nh, nw, h, w, p) \ for (int i = mei_incv(h, w, H, W, -1, p), nh = i < sz(p) ? h + p[i].fi : 0, \ nw = i < sz(p) ? w + p[i].se : 0; \ i < sz(p); \ i = mei_incv(h, w, H, W, i, p), nh = h + p[i].fi, nw = w + p[i].se) // H*Wのグリッドを斜めに分割する // 右上 vector<vp> naname_list_ne(int H, int W) { vector<vp> res(H + W - 1); rep(sh, H) { int sw = 0; res[sh] += mp(sh, sw); int nh = sh; int nw = sw; while (1) { nh--; nw++; if (0 <= nh && nw < W) { res[sh] += mp(nh, nw); } else { break; } } } rep(sw, 1, W) { int sh = H - 1; res[H + sw - 1] += mp(sh, sw); int nh = sh; int nw = sw; while (1) { nh--; nw++; if (0 <= nh && nw < W) { res[H + sw - 1] += mp(nh, nw); } else { break; } } } return res; } // 右下 vector<vp> naname_list_se(int H, int W) { vector<vp> res(H + W - 1); rep(sh, H) { int sw = 0; res[sh] += mp(sh, sw); int nh = sh; int nw = sw; while (1) { nh++; nw++; if (0 <= nh && nh < H && nw < W) { res[sh] += mp(nh, nw); } else { break; } } } rep(sw, 1, W) { int sh = 0; res[H + sw - 1] += mp(sh, sw); int nh = sh; int nw = sw; while (1) { nh++; nw++; if (0 <= nh && nh < H && nw < W) { res[H + sw - 1] += mp(nh, nw); } else { break; } } } return res; } // グラフ内で #undef getid // #define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) ((h) * (W) + (w)) #define getid_1(p) ((p).first * W + (p).second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) #define set_shuffle() \ std::random_device seed_gen; \ std::mt19937 engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), engine); // 1980 開始からtime ms経っていたらtrue vb bit_bool(int v, int len) { assert(bit(len) > v); vb ret(len); rep(i, len) { ret[i] = bget(v, i); } return ret; } vi range(int l, int r) { vi ret; ret.resize(r - l); rep(v, l, r) { ret[v - l] = v; } return ret; } vi range(int r) { return range(0, r); } vi tov(vb &a) { vi ret; rep(i, sz(a)) { if (a[i]) ret.push_back(i); } return ret; } bool kaibun(const str &S) { return S == rev(S); } template <class T> vector<T> repeat(const vector<T> &A, int kaisu) { vector<T> ret; while (kaisu--) { ret += A; } return ret; } #define rge range #define upd update // S[{s, t, d}] #define strs slice_str struct slice_str { string S; slice_str() {} slice_str(const string &S) : S(S) {} slice_str(int len, char c) : S(len, c) {} auto size() { return S.size(); } char &operator[](int p) { return S[p]; } string operator[](initializer_list<int> p) { if (sz(p) == 1) { return S.substr(0, *(p.begin())); } else if (sz(p) == 2) { int l = *(p.begin()); int r = *(next(p.begin())); return S.substr(l, r - l); } else { auto it = p.begin(); int s = *(it++); int t = *(it++); int d = *(it); if (d == -1) { int s_ = sz(S) - s - 1; int t_ = sz(S) - t - 1; return rev(S).substr(s_, t_ - s_); } else if (d < 0) { t = max(-1ll, t); string ret; while (s > t) { ret += S[s]; s += d; } return ret; } else { t = min(sz(S), t); string ret; while (s < t) { ret += S[s]; s += d; } return ret; } } } operator string &() { return S; } template <class T> void operator+=(const T &a) { S += a; } bool operator==(const slice_str &rhs) { return S == rhs.S; } }; ostream &operator<<(ostream &os, const slice_str &a) { os << a.S; return os; } istream &operator>>(istream &iss, const slice_str &a) { iss >> a.S; return iss; } template <class T> bool can(const T &v, int i) { return 0 <= i && i < sz(v); } #if __cplusplus >= 201703L // template<class T> auto sum(int a, T v...) {return (v + ... + 0);} #endif #define VEC vector #endif /*UNTITLED15_TEMPLATE_H*/ #endif // † ←template終了 /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; #ifndef MINT //@formatter:off template <typename T> T minv(T a, T m); template <typename T> T minv(T a); template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(signed) { Modular result(*this); *this += 1; return result; } Modular operator--(signed) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); operator int() { return value; } private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } constexpr signed MOD = 998244353; // 1e9 + 7;//MOD using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>; constexpr int mint_len = 1400001; vi fac, finv, inv; vi p2; mint com(int n, int r) { if (r < 0 || r > n) return 0; /*nが大きくてrが小さい場合、nを上からr個掛ける*/ if (n >= mint_len) { int fa = finv[r]; rep(i, r) fa *= n - i, fa %= MOD; return mint(fa); } return mint(finv[r] * fac[n] % MOD * finv[n - r]); } mint pom(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } mint npr(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } int nprin(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return fac[n] * finv[n - r] % MOD; } int icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD; } #define ncr com #define ncri icom // n個の場所にr個の物を置く mint nhr(int n, int r) { if (n == 0 && r == 0) return 1; else return com(n + r - 1, r); } mint hom(int n, int r) { if (n == 0 && r == 0) return 1; else return com(n + r - 1, r); } int nhri(int n, int r) { if (n == 0 && r == 0) return 1; else return icom(n + r - 1, r); } // グリッドで0-indexedの最短経路 pascal mint pas(int h, int w) { return com(h + w, w); } template <typename T> T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> T minv(T a) { if (a < mint_len) return inv[a]; T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) { assert(b >= 0); int x = a(), res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } //-k乗出来る template <typename T, typename U> mint mpow(const T a, const U b) { /* assert(b >= 0);*/ if (b < 0) { return minv(mpow(a, -b)); } int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } #ifdef _DEBUG void yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint)i / j == a) { st << i << " / " << j; i = 2000; break; } } } string val = st.str(); if (val != "") { deb(val); return; } rep(i, 1000) { rep(j, 1000) { if ((mint)i / j == a) { st << i << " / " << j; i = 2000; break; } } } val = st.str(); deb(val); } #else #define yuri(...) ; #endif template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using PM = pair<mint, mint>; using vm = vector<mint>; using mapm = map<int, mint>; // using umapm = umap<int, mint>; #define vvm(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(mint, __VA_ARGS__) #define vnm(name, ...) auto name = make_v<mint>(__VA_ARGS__) string out_m2(mint a) { stringstream st; st << (int)a; rep(i, 300) { rep(j, 2, 300) { if ((i % j) && (mint)i / j == a) { st << "(" << i << "/" << j << ")"; i = 2000; break; } } } return st.str(); } struct setmod { setmod() { p2.resize(mint_len); p2[0] = 1; for (int i = 1; i < mint_len; ++i) p2[i] = p2[i - 1] * 2 % MOD; fac.resize(mint_len); finv.resize(mint_len); inv.resize(mint_len); inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i < mint_len; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i < mint_len; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } } setmodv; //@formatter:on // nhr n個の場所にr個の物を分ける mint m1 = (mint)1; mint half = (mint)1 / 2; #endif /*@formatter:off*/ namespace FastFourierTransform { using real = sig_dou; struct C { real x, y; C() : x(0), y(0) {} C(real x, real y) : x(x), y(y) {} inline C operator+(const C &c) const { return C(x + c.x, y + c.y); } inline C operator-(const C &c) const { return C(x - c.x, y - c.y); } inline C operator*(const C &c) const { return C(x * c.x - y * c.y, x * c.y + y * c.x); } inline C conj() const { return C(x, -y); } }; const real PI = acosl(-1); signed base = 1; vector<C> rts = {{0, 0}, {1, 0}}; vector<signed> rev = {0, 1}; void ensure_base(signed nbase) { if (nbase <= base) return; rev.resize(1 << nbase); rts.resize(1 << nbase); for (signed i = 0; i < (1 << nbase); i++) { rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1)); } while (base < nbase) { real angle = PI * 2.0 / (1 << (base + 1)); for (signed i = 1 << (base - 1); i < (1 << base); i++) { rts[i << 1] = rts[i]; real angle_i = angle * (2 * i + 1 - (1 << base)); rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i)); } ++base; } } void fft(vector<C> &a, signed n) { assert((n & (n - 1)) == 0); signed zeros = __builtin_ctz(n); ensure_base(zeros); signed shift = base - zeros; for (signed i = 0; i < n; i++) { if (i < (rev[i] >> shift)) { swap(a[i], a[rev[i] >> shift]); } } for (signed k = 1; k < n; k <<= 1) { for (signed i = 0; i < n; i += 2 * k) { for (signed j = 0; j < k; j++) { C z = a[i + j + k] * rts[j + k]; a[i + j + k] = a[i + j] - z; a[i + j] = a[i + j] + z; } } } } vector<int64_t> multiply(const vector<signed> &a, const vector<signed> &b) { signed need = (signed)a.size() + (signed)b.size() - 1; signed nbase = 1; while ((1 << nbase) < need) nbase++; ensure_base(nbase); signed sz = 1 << nbase; vector<C> fa(sz); for (signed i = 0; i < sz; i++) { signed x = (i < (signed)a.size() ? a[i] : 0); signed y = (i < (signed)b.size() ? b[i] : 0); fa[i] = C(x, y); } fft(fa, sz); C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0); for (signed i = 0; i <= (sz >> 1); i++) { signed j = (sz - i) & (sz - 1); C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r; fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r; fa[i] = z; } for (signed i = 0; i < (sz >> 1); i++) { C A0 = (fa[i] + fa[i + (sz >> 1)]) * t; C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i]; fa[i] = A0 + A1 * s; } fft(fa, sz >> 1); vector<int64_t> ret(need); for (signed i = 0; i < need; i++) { ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x); } return ret; } }; // namespace FastFourierTransform template <typename T> struct ArbitraryModConvolution { using real = FastFourierTransform::real; using C = FastFourierTransform::C; ArbitraryModConvolution() = default; vector<T> multiply(const vector<T> &a, const vector<T> &b, signed need = -1) { if (need == -1) need = a.size() + b.size() - 1; signed nbase = 0; while ((1 << nbase) < need) nbase++; FastFourierTransform::ensure_base(nbase); signed sz = 1 << nbase; vector<C> fa(sz); for (signed i = 0; i < a.size(); i++) { fa[i] = C((signed)a[i] & ((1 << 15) - 1), (signed)a[i] >> 15); } fft(fa, sz); vector<C> fb(sz); if (a == b) { fb = fa; } else { for (signed i = 0; i < b.size(); i++) { fb[i] = C((signed)b[i] & ((1 << 15) - 1), (signed)b[i] >> 15); } fft(fb, sz); } real ratio = 0.25 / sz; C r2(0, -1), r3(ratio, 0), r4(0, -ratio), r5(0, 1); for (signed i = 0; i <= (sz >> 1); i++) { signed j = (sz - i) & (sz - 1); C a1 = (fa[i] + fa[j].conj()); C a2 = (fa[i] - fa[j].conj()) * r2; C b1 = (fb[i] + fb[j].conj()) * r3; C b2 = (fb[i] - fb[j].conj()) * r4; if (i != j) { C c1 = (fa[j] + fa[i].conj()); C c2 = (fa[j] - fa[i].conj()) * r2; C d1 = (fb[j] + fb[i].conj()) * r3; C d2 = (fb[j] - fb[i].conj()) * r4; fa[i] = c1 * d1 + c2 * d2 * r5; fb[i] = c1 * d2 + c2 * d1; } fa[j] = a1 * b1 + a2 * b2 * r5; fb[j] = a1 * b2 + a2 * b1; } fft(fa, sz); fft(fb, sz); vector<T> ret(need); for (signed i = 0; i < need; i++) { int64_t aa = llround(fa[i].x); int64_t bb = llround(fb[i].x); int64_t cc = llround(fa[i].y); aa = T(aa), bb = T(bb), cc = T(cc); ret[i] = aa + (bb << 15) + (cc << 30); } return ret; } }; template <typename T> struct FormalPowerSeries : vector<T> { using vector<T>::vector; using P = FormalPowerSeries; using MULT = function<P(P, P)>; static MULT &get_mult() { static MULT mult = nullptr; return mult; } static void set_fft(MULT f) { get_mult() = f; } template <typename E> FormalPowerSeries(const vector<E> &x) : vector<T>(begin(x), end(x)) {} void shrink() { while (this->size() && this->back() == T(0)) this->pop_back(); } P operator+(const P &r) const { return P(*this) += r; } P operator+(const T &v) const { return P(*this) += v; } P operator-(const P &r) const { return P(*this) -= r; } P operator-(const T &v) const { return P(*this) -= v; } P operator*(const P &r) const { return P(*this) *= r; } P operator*(const T &v) const { return P(*this) *= v; } P operator/(const P &r) const { return P(*this) /= r; } P operator%(const P &r) const { return P(*this) %= r; } P &operator+=(const P &r) { if (r.size() > this->size()) this->resize(r.size()); for (int i = 0; i < r.size(); i++) (*this)[i] += r[i]; return *this; } P &operator+=(const T &r) { if (this->empty()) this->resize(1); (*this)[0] += r; return *this; } P &operator-=(const P &r) { if (r.size() > this->size()) this->resize(r.size()); for (int i = 0; i < r.size(); i++) (*this)[i] -= r[i]; shrink(); return *this; } P &operator-=(const T &r) { if (this->empty()) this->resize(1); (*this)[0] -= r; shrink(); return *this; } P &operator*=(const T &v) { const int n = (int)this->size(); for (int k = 0; k < n; k++) (*this)[k] *= v; return *this; } P &operator*=(const P &r) { if (this->empty() || r.empty()) { this->clear(); return *this; } assert(get_mult() != nullptr); return *this = get_mult()(*this, r); } P &operator%=(const P &r) { return *this -= *this / r * r; } P operator-() const { P ret(this->size()); for (int i = 0; i < this->size(); i++) ret[i] = -(*this)[i]; return ret; } // 謎が多い あまり使いたくない P &operator/=(P r) { while ((*this).back() == T(0)) (*this).pop_back(); while (r.back() == T(0)) r.pop_back(); if (this->size() < r.size()) { this->clear(); return *this; } int n = this->size() - r.size() + 1; return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev(n); } P pre(int sz) const { return P(begin(*this), begin(*this) + min((int)this->size(), sz)); } P operator>>(int sz) const { if (this->size() <= sz) return {}; P ret(*this); ret.erase(ret.begin(), ret.begin() + sz); return ret; } P operator<<(int sz) const { P ret(*this); ret.insert(ret.begin(), sz, T(0)); return ret; } P rev(int deg = -1) const { P ret(*this); if (deg != -1) ret.resize(deg, T(0)); reverse(begin(ret), end(ret)); return ret; } P diff() const { const int n = (int)this->size(); P ret(max(0ll, n - 1)); for (int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * T(i); return ret; } P integral() const { const int n = (int)this->size(); P ret(n + 1); ret[0] = T(0); for (int i = 0; i < n; i++) ret[i + 1] = (*this)[i] / T(i + 1); return ret; } // F(0) must not be 0 P inv(int deg = -1) const { assert(((*this)[0]) != T(0)); const int n = (int)this->size(); if (deg == -1) deg = n; P ret({T(1) / (*this)[0]}); for (int i = 1; i < deg; i <<= 1) { ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1); } return ret.pre(deg); } // F(0) must be 1 P log(int deg = -1) const { assert((*this)[0] == T(1)); const int n = (int)this->size(); if (deg == -1) deg = n; return (this->diff() * this->inv(deg)).pre(deg - 1).integral(); } P sqrt(int deg = -1) const { const int n = (int)this->size(); if (deg == -1) deg = n; if ((*this)[0] == T(0)) { for (int i = 1; i < n; i++) { if ((*this)[i] != T(0)) { if (i & 1) return {}; if (deg - i / 2 <= 0) break; auto ret = (*this >> i).sqrt(deg - i / 2) << (i / 2); if (ret.size() < deg) ret.resize(deg, T(0)); return ret; } } return P(deg, 0); } P ret({T(1)}); T inv2 = T(1) / T(2); for (int i = 1; i < deg; i <<= 1) { ret = (ret + pre(i << 1) * ret.inv(i << 1)) * inv2; } return ret.pre(deg); } // F(0) must be 0 P exp(int deg = -1) const { assert((*this)[0] == T(0)); const int n = (int)this->size(); if (deg == -1) deg = n; P ret({T(1)}); for (int i = 1; i < deg; i <<= 1) { ret = (ret * (pre(i << 1) + T(1) - ret.log(i << 1))).pre(i << 1); } return ret.pre(deg); } P pow(int64_t k, int deg = -1) const { const int n = (int)this->size(); if (deg == -1) deg = n; for (int i = 0; i < n; i++) { if ((*this)[i] != T(0)) { T rev = T(1) / (*this)[i]; P C(*this * rev); P D(n - i); for (int j = i; j < n; j++) D[j - i] = C[j]; D = (D.log() * k).exp() * mpow((*this)[i], (k)); P E(deg); if (i * k > deg) return E; auto S = i * k; for (int j = 0; j + S < deg && j < D.size(); j++) E[j + S] = D[j]; return E; } } return *this; } T eval(T x) const { T r = 0, w = 1; for (auto &v : *this) { r += w * v; w *= x; } return r; } // *x^x P &rshift(int k) { int an = this->size(); this->resize(an + k); rer(i, an + k - 1, k) { (*this)[i] = (*this)[i - k]; } rep(i, k) { (*this)[i] = 0; } return (*this); } // /x^k P &lshift(int k) { int an = this->size(); rep(i, an - k) { (*this)[i] = (*this)[i + k]; } this->resize(an - k); return (*this); } // x = rx^dを代入する P &assign(int r, int d, int msize) { P ret(msize + 1); T rv = 1; rep(i, min((int)this->size(), msize / d + 1)) { ret[i * d] = (*this)[i] * rv; rv *= r; } return *this = ret; } }; ArbitraryModConvolution<mint> fft; using fps = FormalPowerSeries<mint>; struct FPS_init { FPS_init() { fps::set_fft( [](const fps::P &a, const fps::P &b) { return fft.multiply(a, b); }); }; } initooonv; struct helper_fps { int i; mint k; helper_fps(int i, mint k) : i(i), k(k) {} operator fps() { fps ret(i + 1); ret[i] = k; return ret; } fps operator+(mint b) { return fps(*this) + b; } fps operator+(helper_fps b) { return fps(*this) + fps(b); } fps operator-(mint b) { return fps(*this) - b; } fps operator-(helper_fps b) { return fps(*this) - fps(b); } helper_fps operator*(mint b) { return helper_fps(i, k * b); } helper_fps operator*(helper_fps b) { return helper_fps(i + b.i, k * b.k); } helper_fps operator^(int b) { return helper_fps(i * b, mpow(k, b)); } } X(1, 1); fps operator+(mint a, helper_fps b) { return b + a; } fps operator+(const fps &a, helper_fps b) { return a + (fps)b; } fps operator-(mint a, helper_fps b) { return b + a; } fps operator-(const fps &a, helper_fps b) { return a - (fps)b; } helper_fps operator*(mint a, helper_fps b) { return b * a; } // vectorとして使える fps operator+(fps &l, signed v) { fps ret = l; ret[0] += v; return ret; } fps operator+(fps &l, ll v) { fps ret = l; ret[0] += v; return ret; } fps operator+=(fps &l, signed v) { l[0] += v; return l; } fps operator+=(fps &l, ll v) { l[0] += v; return l; } fps operator-(fps &l, signed v) { return operator+(l, -v); } fps operator-(fps &l, ll v) { return operator+(l, -v); } fps operator-=(fps &l, signed v) { return operator+=(l, -v); } fps operator-=(fps &l, ll v) { return operator+=(l, -v); } fps operator*(fps &l, signed v) { fps ret = l; for (int i = 0; i < l.size(); i++) ret[i] *= v; return ret; } fps operator*(fps &l, ll v) { fps ret = l; for (int i = 0; i < l.size(); i++) ret[i] *= v; return ret; } fps operator*=(fps &l, signed v) { for (int i = 0; i < l.size(); i++) l[i] *= v; return l; } fps operator*=(fps &l, ll v) { for (int i = 0; i < l.size(); i++) l[i] *= v; return l; } fps operator/(fps &l, signed v) { return operator*(l, minv(v)); } fps operator/(fps &l, ll v) { return operator*(l, minv(v)); } fps operator/=(fps &l, signed v) { return operator*=(l, minv(v)); } fps operator/=(fps &l, ll v) { return operator*=(l, minv(v)); } fps operator+(signed l, fps &v) { return operator+(v, l); } fps operator+(ll l, fps &v) { return operator+(v, l); } fps operator-(signed l, fps v) { v *= -1; return operator+(v, l); } fps operator-(ll l, fps v) { v *= -1; return operator+(v, l); } fps operator*(signed l, fps &v) { return operator*(v, l); } fps operator*(ll l, fps &v) { return operator*(v, l); } fps operator/(signed l, fps v) { v = v.inv(); return operator*(v, l); } fps operator/(ll l, fps v) { v = v.inv(); return operator*(v, l); } fps cut(fps l, fps r) { fps ret = l; ret *= r; ret.resize(max(l.size(), r.size())); return ret; } template <class... T> fps cut(fps l, T... r) { return cut(l, cut(r...)); } // initializerlist operatorに fps cutf(fps &l, fps &r) { fps ret = l; ret *= r; ret.resize(max(l.size(), r.size())); return ret; } // 合計がSになる組合せを返す template <class T> T fft_get(vector<T> &a, vector<T> &b, int S) { int bn = sz(b); T res = 0; rep(l, sz(a) + 1) { int r = S - l; if (r >= bn || r < 0) con; res += a[l] * b[r]; } return res; } // 右から疎なfpsを掛ける、O(N) * 右の項数 fps mul_sparse(const fps &l, const fps &r, int msize = -1) { vi inds; rep(i, r.size()) { if (r[i]) { inds.push_back(i); } } assert(sz(inds)); int n = l.size() + inds.back(); if (msize == -1) { msize = n - 1; } fps ret(msize + 1); rep(i, l.size()) { for (auto d : inds) { if (i + d > msize) break; ret[i + d] += l[i] * r[d]; } } return ret; } // 右から疎なfpsで割る、O(N) * 右の項数 fps div_sparse(const fps &a, const fps &r, int msize = -1) { fps l = a; vi inds; rep(i, r.size()) { if (r[i]) { inds.push_back(i); } } assert(sz(inds)); int n = l.size(); if (msize == -1) { msize = n - 1; } msize = min(msize, n - 1); fps ret(msize + 1); rep(i, l.size()) { if (l[i]) { assert(i >= inds[0]); mint dec_k = l[i] / r[inds[0]]; if (i - inds[0] > msize) break; ret[i - inds[0]] += dec_k; for (auto d : inds) { if (i + d - inds[0] >= n) break; l[i + d - inds[0]] -= dec_k * r[d]; } } } return ret; } // Π(1-x^n) (1<=n<=inf) = 1 + Σ -1^i * (x^(i*(3i-1)/2) + x^(i*(3i+1)/2) // (1<=i<=inf)http://math.josai.ac.jp/~oshima/s2004.pdf P2 fps pow_sigma(int msize) { vi reti(msize + 1); reti[0] = 1; for (int i = 2; i < inf; i += 2) { int a = (i * (3 * i - 1)) >> 1; if (a > msize) break; reti[a]++; a = (i * (3 * i + 1)) >> 1; if (a > msize) continue; reti[a]++; } for (int i = 1; i < inf; i += 2) { int a = (i * (3 * i - 1)) >> 1; if (a > msize) break; reti[a]--; a = (i * (3 * i + 1)) >> 1; if (a > msize) continue; reti[a]--; } return reti; } // 閉区間 sigma x^(0~r) = (1 - x^(r+1)) / (1 - x) // N乗はcombinationで分かる fps operator*(const fps &a, helper_fps b) { return mul_sparse(a, (fps)b); } // Σx^(0~r) = (1-x^(r+1)) / (1-x) // // (1-x^r)^nを返す sigma x^(0~r) = (1 - x^(r+1)) / (1 - x)に注意! (0~rは // r+1乗になる) fps pow_term2(int r, int n, int msize) { fps ret(msize + 1); rep(i, n + 1) { if (i * r <= msize) { ret[i * r] = com(n, i); if (i & 1) ret[i * r] = -ret[i * r]; } } return ret; } // (x^l - x^r)^n fps pow_term2(int l, int r, int n, int msize) { fps ret = pow_term2(r - l, n, msize); /*(x^l)^n * (1-x^(r-l))^n*/ ret.rshift(l * n); if (sz(ret) > msize + 1) ret.resize(msize + 1); return ret; } // 1/(1-rx)^nを返す [x^i]f = nhi * r^i fps pow_term2_inv(int r, int n, int msize) { /*http://math.josai.ac.jp/~oshima/s2004.pdf P2 */ /*wolfram Series (1/(1-r*x)^m) 級数表現*/ fps ret(msize + 1); if (r == 1) { for (int i = 0; i < msize + 1; i++) ret[i] = nhr(n, i); return ret; } mint rb = 1; for (int i = 0; i < msize + 1; i++) { ret[i] = nhr(n, i) * rb; rb *= r; } return ret; } // 1/(1-x)^n fps pow_term2_inv(int n, int msize) { return pow_term2_inv(1, n, msize); } // 1/(1-rx^d)^nを返す 1/(1-rx)^n にx=x^dを代入 fps pow_term2_inv(int r, int d, int n, int msize) { fps ret = pow_term2_inv(r, n, msize); ret.assign(1, d, msize); return ret; } /*@formatter:on*/ // 割り算の使い方が怪しい // 逆元は使えるため、逆元を掛けたほうがいいかも // invは長さが元のだけ必要 void solve() { din(N); din(S); na(A, N); int len = 4000; fps ret(len); fps su(len); rer(i, N - 1) { fps add(len); add[0] = 1; fps r(len); r[0] = 1; r[A[i]] = 1; // ret += add; ret = mul_sparse(ret + add, r, len); su += ret; // cout<<ret<<endl;; // deb(ret); } out(su[S]); } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // solveを書き直す // solveを呼び出す } if (was_deb && sz(res_mes)) { cerr << "result = " << endl << res_mes << endl; } if (sz(message)) { cerr << "****************************" << endl; cerr << "Note." << endl; cerr << message << endl; cerr << "****************************" << endl; } #endif return 0; };
replace
8,443
8,444
8,443
8,444
TLE
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define V vector<long long> #define VV vector<vector<long long>> #define VVV vector<vector<vector<long long>>> #define P pair<ll, ll> #define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i)) using namespace std; long long mod = 998244353; struct mint { long long x; mint(long long 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(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } int main() { ll n, s; cin >> n >> s; V a(n); rep(i, n) cin >> a[i]; vector<vector<mint>> dp(n + 1, vector<mint>(s + 1, 0)); mint ans = 0; rep(i, n) { dp[i][0] += 1; rep(j, s + 1) { dp[i + 1][j] += dp[i][j]; if (j + a[i] <= s + 1) dp[i + 1][j + a[i]] += dp[i][j]; } ans += dp[i + 1][s]; } cout << ans << endl; }
#include <bits/stdc++.h> #define ll long long #define V vector<long long> #define VV vector<vector<long long>> #define VVV vector<vector<vector<long long>>> #define P pair<ll, ll> #define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i)) using namespace std; long long mod = 998244353; struct mint { long long x; mint(long long 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(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } int main() { ll n, s; cin >> n >> s; V a(n); rep(i, n) cin >> a[i]; vector<vector<mint>> dp(n + 1, vector<mint>(s + 1, 0)); mint ans = 0; rep(i, n) { dp[i][0] += 1; rep(j, s + 1) { dp[i + 1][j] += dp[i][j]; if (j + a[i] <= s) dp[i + 1][j + a[i]] += dp[i][j]; } ans += dp[i + 1][s]; } cout << ans << endl; }
replace
58
59
58
59
0
p02734
Python
Runtime Error
# https://atcoder.jp/contests/abc159/submissions/11139734 def main(): MOD = 998244353 N, S = map(int, input().split()) (*a,) = map(int, input().split()) dp = [0] * (S + 1) ret = 0 for i, x in enumerate(a): dp[0] += 1 ret = (ret + dp[S - x] * (N - i)) % MOD for k in range(S - 1, x - 1, -1): dp[k] = (dp[k] + dp[k - x]) % MOD print(ret) if __name__ == "__main__": main()
# https://atcoder.jp/contests/abc159/submissions/11139734 def main(): MOD = 998244353 N, S = map(int, input().split()) (*a,) = map(int, input().split()) dp = [0] * (S + 1) ret = 0 for i, x in enumerate(a): dp[0] += 1 if x > S: continue ret = (ret + dp[S - x] * (N - i)) % MOD for k in range(S - 1, x - 1, -1): dp[k] = (dp[k] + dp[k - x]) % MOD print(ret) if __name__ == "__main__": main()
insert
14
14
14
16
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using Int = int_fast64_t; constexpr Int mod = 998244353; int main() { cin.tie(0); ios::sync_with_stdio(false); Int n, s; cin >> n >> s; vector<Int> a(n); for (auto &i : a) cin >> i; vector<vector<Int>> dp(n, vector<Int>(s + 1, 0)); Int ans = 0; for (Int i = 0; i < n; ++i) { if (i > 0) { for (Int j = 0; j <= s; ++j) { dp[i][j] += dp[i - 1][j]; dp[i][j] %= mod; if (j + a[i] <= s) { dp[i][j + a[i]] += dp[i - 1][j]; dp[i][j + a[i]] %= mod; } } } dp[i][a[i]] += i + 1; dp[i][a[i]] %= mod; ans += dp[i][s]; ans %= mod; } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; using Int = int_fast64_t; constexpr Int mod = 998244353; int main() { cin.tie(0); ios::sync_with_stdio(false); Int n, s; cin >> n >> s; vector<Int> a(n); for (auto &i : a) cin >> i; vector<vector<Int>> dp(n, vector<Int>(s + 1, 0)); Int ans = 0; for (Int i = 0; i < n; ++i) { if (i > 0) { for (Int j = 0; j <= s; ++j) { dp[i][j] += dp[i - 1][j]; dp[i][j] %= mod; if (j + a[i] <= s) { dp[i][j + a[i]] += dp[i - 1][j]; dp[i][j + a[i]] %= mod; } } } if (a[i] <= s) { dp[i][a[i]] += i + 1; dp[i][a[i]] %= mod; } ans += dp[i][s]; ans %= mod; } cout << ans << "\n"; }
replace
27
29
27
31
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll maxs = 1e6 + 5; const ll lmaxs = 20; ll mod = 1e9 + 7; ll oo = 1e15; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define mp make_pair #define ff first #define ss second #define I insert const int MAX = 3005; ll dp[MAX][MAX]; void solve() { mod = 998244353; ll n, s; cin >> n >> s; ll ans = 0; ll a[n + 1]; for (ll i = 1; i <= n; i++) { cin >> a[i]; memcpy(dp[i], dp[i - 1], sizeof dp[0]); (dp[i][a[i]] += i) %= mod; for (ll j = 0; j + a[i] <= s; j++) { (dp[i][j + a[i]] += dp[i - 1][j]) %= mod; } ans += dp[i][s]; ans %= mod; } cout << ans << endl; } int main() { #ifndef ONLINE_JUDGE freopen("input00.txt", "r", stdin); #endif IOS; ll T = 1; // cin>>T; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll maxs = 1e6 + 5; const ll lmaxs = 20; ll mod = 1e9 + 7; ll oo = 1e15; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define mp make_pair #define ff first #define ss second #define I insert const int MAX = 3005; ll dp[MAX][MAX]; void solve() { mod = 998244353; ll n, s; cin >> n >> s; ll ans = 0; ll a[n + 1]; for (ll i = 1; i <= n; i++) { cin >> a[i]; memcpy(dp[i], dp[i - 1], sizeof dp[0]); (dp[i][a[i]] += i) %= mod; for (ll j = 0; j + a[i] <= s; j++) { (dp[i][j + a[i]] += dp[i - 1][j]) %= mod; } ans += dp[i][s]; ans %= mod; } cout << ans << endl; } int main() { IOS; ll T = 1; // cin>>T; while (T--) { solve(); } return 0; }
replace
42
45
42
43
-11
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define SZ(v) int((v).size()) #define ALL(vec) begin(vec), end(vec) #define pb push_back #define mk make_pair #define fi first #define se second typedef long long ll; template <typename T> inline bool uax(T &x, T y) { return (y > x) ? x = y, true : false; } template <typename T> inline bool uin(T &x, T y) { return (y < x) ? x = y, true : false; } #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } string to_string(char c) { return "'" + string(1, c) + "'"; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ": " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = false; string r = "{"; for (auto x : v) { if (f) r += ", "; r += to_string(x); f = true; } return r += "}"; } template <typename A> string to_string(vector<vector<A>> v) { string r; for (auto x : v) r += "\n" + to_string(x); return r; } int Nerr; template <typename A> string to_string(A *p) { return to_string(vector<A>(p, p + Nerr)); } void err(istream_iterator<string>) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " =: " << to_string(a) << "; "; err(++it, args...); } template <typename T> void kek(T ans) { cout << ans << endl; exit(0); } int const MOD = 998244353; long long const INF = 1e18 + 42; int mod = 998244353; int add(int a, int b) { if (a + b >= mod) return a + b - mod; return a + b; } int mul(ll a, ll b) { return 1LL * a * b % mod; } int sub(int a, int b) { if (a >= b) return a - b; return a - b + mod; } int power(ll a, ll b) { int re = 1; while (b) { if (b & 1) { re = mul(re, a); } a = mul(a, a); b >>= 1; } return re; } int const N = 500000 + 5; // int fact[N], inverse[N]; // void FAC(){ // fact[0] = 1; // for(int x = 1; x< N; x++){ // fact[x] = mul(fact[x-1], x); // } // inverse[N-1] = power(fact[N-1], mod -2); // for(int x = N-2; x>= 0; x--){ // inverse[x] = mul(x + 1, inverse[x+1]); // } // } // int ncr(int a, int b){ // if(a < b)return 0; // return mul(fact[a], mul(inverse[a-b], inverse[b])); // } /***********************************************************************/ // int mod = MOD; // int add(int a, int b){ // if(a + b >= mod)return a + b - mod; // return a + b; // } // int mul(int a, int b){ // return 1LL * a * b % mod; // } // vector<vector<int>> mull(vector<vector<int>> a, vector<vector<int>> b){ // vector<vector<int>> re = {{0, 0}, {0, 0}}; // for(int x =0; x<2; x++){ // for(int y = 0;y<2; y++){ // for(int z = 0; z<2; z++){ // re[x][y] = add(re[x][y], mul(a[x][z], b[z][y])); // } // } // } // return re; void solve() { int n, s; cin >> n >> s; vector<int> vec(s + s + 10, 0), sum(s + s + 10, 0); // vec[0] = 1; int ans = 0; for (int x = 1; x <= n; x++) { int i; cin >> i; vector<int> dp = vec; dp[i] = add(dp[i], x); for (int u = 0; u <= s + s - i; u++) { dp[u + i] = add(dp[u + i], vec[u]); } // error(i ,dp, vec); // cout << "\n"; swap(vec, dp); ans = add(vec[s], ans); } cout << ans; } /**************/ int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; #define SZ(v) int((v).size()) #define ALL(vec) begin(vec), end(vec) #define pb push_back #define mk make_pair #define fi first #define se second typedef long long ll; template <typename T> inline bool uax(T &x, T y) { return (y > x) ? x = y, true : false; } template <typename T> inline bool uin(T &x, T y) { return (y < x) ? x = y, true : false; } #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } string to_string(char c) { return "'" + string(1, c) + "'"; } string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ": " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = false; string r = "{"; for (auto x : v) { if (f) r += ", "; r += to_string(x); f = true; } return r += "}"; } template <typename A> string to_string(vector<vector<A>> v) { string r; for (auto x : v) r += "\n" + to_string(x); return r; } int Nerr; template <typename A> string to_string(A *p) { return to_string(vector<A>(p, p + Nerr)); } void err(istream_iterator<string>) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " =: " << to_string(a) << "; "; err(++it, args...); } template <typename T> void kek(T ans) { cout << ans << endl; exit(0); } int const MOD = 998244353; long long const INF = 1e18 + 42; int mod = 998244353; int add(int a, int b) { if (a + b >= mod) return a + b - mod; return a + b; } int mul(ll a, ll b) { return 1LL * a * b % mod; } int sub(int a, int b) { if (a >= b) return a - b; return a - b + mod; } int power(ll a, ll b) { int re = 1; while (b) { if (b & 1) { re = mul(re, a); } a = mul(a, a); b >>= 1; } return re; } int const N = 500000 + 5; // int fact[N], inverse[N]; // void FAC(){ // fact[0] = 1; // for(int x = 1; x< N; x++){ // fact[x] = mul(fact[x-1], x); // } // inverse[N-1] = power(fact[N-1], mod -2); // for(int x = N-2; x>= 0; x--){ // inverse[x] = mul(x + 1, inverse[x+1]); // } // } // int ncr(int a, int b){ // if(a < b)return 0; // return mul(fact[a], mul(inverse[a-b], inverse[b])); // } /***********************************************************************/ // int mod = MOD; // int add(int a, int b){ // if(a + b >= mod)return a + b - mod; // return a + b; // } // int mul(int a, int b){ // return 1LL * a * b % mod; // } // vector<vector<int>> mull(vector<vector<int>> a, vector<vector<int>> b){ // vector<vector<int>> re = {{0, 0}, {0, 0}}; // for(int x =0; x<2; x++){ // for(int y = 0;y<2; y++){ // for(int z = 0; z<2; z++){ // re[x][y] = add(re[x][y], mul(a[x][z], b[z][y])); // } // } // } // return re; void solve() { int n, s; cin >> n >> s; vector<int> vec(s + s + 10, 0), sum(s + s + 10, 0); // vec[0] = 1; int ans = 0; for (int x = 1; x <= n; x++) { int i; cin >> i; if (i > s) { ans = add(ans, vec[s]); continue; } vector<int> dp = vec; dp[i] = add(dp[i], x); for (int u = 0; u <= s + s - i; u++) { dp[u + i] = add(dp[u + i], vec[u]); } // error(i ,dp, vec); // cout << "\n"; swap(vec, dp); ans = add(vec[s], ans); } cout << ans; } /**************/ int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); }
insert
138
138
138
142
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define fr(i, n) for (int i = 0; i < (n); ++i) #define foor(i, a, b) for (int i = (a); i <= (b); ++i) #define rf(i, n) for (int i = (n); i-- > 0;) #define roof(i, b, a) for (int i = (b); i >= (a); --i) #define elsif else if #define all(x) x.begin(), x.end() #define Sort(x) sort(all(x)) #define Reverse(x) reverse(all(x)) #define PQ priority_queue #define NP(x) next_permutation(all(x)) #define M_PI 3.14159265358979323846 #define popcount __builtin_popcountll using namespace std; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef unsigned long long ull; typedef vector<ull> vu; typedef vector<vu> vvu; typedef double dbl; typedef vector<dbl> vd; typedef vector<vd> vvd; typedef string str; typedef vector<str> vs; typedef vector<vs> vvs; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef map<int, int> mii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef map<ll, ll> mll; typedef pair<dbl, dbl> pdd; typedef vector<pdd> vpdd; typedef map<dbl, dbl> mdd; typedef pair<str, str> pss; typedef vector<pss> vpss; typedef map<str, str> mss; typedef pair<int, ll> pil; typedef vector<pil> vpil; typedef map<int, ll> mil; typedef pair<ll, int> pli; typedef vector<pli> vpli; typedef map<ll, int> mli; typedef pair<dbl, int> pdi; typedef vector<pdi> vpdi; typedef map<dbl, int> mdi; template <typename T> vector<T> &operator<<(vector<T> &v, const T t) { v.push_back(t); return v; } template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) { m.insert(t); return m; } template <typename T> set<T> &operator<<(set<T> &s, const T t) { s.insert(t); return s; } template <typename T> stack<T> &operator<<(stack<T> &s, const T t) { s.push(t); return s; } template <typename T> stack<T> &operator>>(stack<T> &s, T &t) { t = s.top(); s.pop(); return s; } template <typename T> queue<T> &operator<<(queue<T> &q, const T t) { q.push(t); return q; } template <typename T> queue<T> &operator>>(queue<T> &q, T &t) { t = q.front(); q.pop(); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) { q.push(t); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) { t = q.top(); q.pop(); return q; } template <typename T, typename U> istream &operator>>(istream &s, pair<T, U> &p) { return s >> p.first >> p.second; } istream &operator>>(istream &s, _Bit_reference b) { int a; s >> a; assert(a == 0 || a == 1); b = a; return s; } template <typename T> istream &operator>>(istream &s, vector<T> &v) { fr(i, v.size()) { s >> v[i]; } return s; } template <typename T, typename U> ostream &operator<<(ostream &s, const pair<T, U> p) { return s << p.first << " " << p.second; } template <typename T> ostream &operator<<(ostream &s, const vector<T> v) { fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; } return s; } template <typename T> ostream &operator<<(ostream &s, const deque<T> d) { fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; } return s; } template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) { return b = b & t; } template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) { return b = b ^ t; } template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) { return b = b | t; } template <typename T, typename U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return {a.first + b.first, a.second + b.second}; } template <typename T, typename U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return {a.first - b.first, a.second - b.second}; } template <typename T, typename U> pair<T, U> &operator+=(pair<T, U> &a, pair<T, U> b) { return a = a + b; } template <typename T, typename U> pair<T, U> &operator-=(pair<T, U> &a, pair<T, U> b) { return a = a - b; } void print(void) { cout << "\n"; } void Print(void) { cout << endl; } template <typename T> void print(T t) { cout << t << "\n"; } template <typename T> void Print(T t) { cout << t << endl; } template <typename T, typename... U> void print(T &&t, U &&...u) { cout << t << " "; print(forward<U>(u)...); } template <typename T, typename... U> void Print(T &&t, U &&...u) { cout << t << " "; Print(forward<U>(u)...); } bool YN(bool b) { print(b ? "YES" : "NO"); return b; } bool PI(bool b) { print(b ? "POSSIBLE" : "IMPOSSIBLE"); return b; } bool Yn(bool b) { print(b ? "Yes" : "No"); return b; } bool Pi(bool b) { print(b ? "Possible" : "Impossible"); return b; } bool yn(bool b) { print(b ? "yes" : "no"); return b; } bool pi(bool b) { print(b ? "possible" : "impossible"); return b; } const int e5 = 1e5; const int e9 = 1e9; const int MD = 1e9 + 7; const int md = 998244353; const ll e18 = 1e18; template <typename T> str to_string(const T &n) { ostringstream s; s << n; return s.str(); } template <typename T> T &chmax(T &a, T b) { return a = max(a, b); } template <typename T> T &chmin(T &a, T b) { return a = min(a, b); } template <typename T, typename U> vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s, const T inf) { using P = pair<T, U>; vector<P> d; fr(i, E.size()) { d << P{inf, i}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } template <typename T, typename U> map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s, const T inf) { using P = pair<T, U>; map<U, P> d; for (pair<U, vector<P>> e : E) { d[e.first] = P{inf, e.first}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } ll maxflow(vector<mil> &E, int s, int t) { ll z = 0; vi b(E.size(), -1); for (int i = 0;; ++i) { static auto dfs = [&](int v, ll f, auto &dfs) -> ll { if (v == t) return f; b[v] = i; for (auto &p : E[v]) { if (b[p.first] < i && p.second) { if (ll r = dfs(p.first, min(f, p.second), dfs)) { p.second -= r; E[p.first][v] += r; return r; } } } return 0; }; ll x = dfs(s, ll(1e18), dfs); z += x; if (x == 0) return z; } } template <typename T> T distsq(pair<T, T> a, pair<T, T> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } template <typename T> T max(const vector<T> a) { assert(a.size()); T m = a[0]; for (T e : a) { m = max(m, e); } return m; } template <typename T> T min(const vector<T> a) { assert(a.size()); T m = a[0]; for (T e : a) { m = min(m, e); } return m; } template <typename T> T gcd(const T a, const T b) { return a ? gcd(b % a, a) : b; } template <typename T> T gcd(const vector<T> a) { T g = a[0]; for (T e : a) { g = gcd(g, e); } return g; } template <typename T> vector<T> LCS(vector<T> A, vector<T> B) { int N = A.size(), M = B.size(); vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1)); fr(i, N) { fr(j, M) { if (A[i] == B[j]) { d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}}; } else { d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]); } } } vector<T> r; for (pii p = {N, M}; d[p.first][p.second].first; p = d[p.first][p.second].second) { r << A[d[p.first][p.second].second.first]; } Reverse(r); return r; } str LCS(str S, str T) { vector<char> s = LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end())); return str(s.begin(), s.end()); } template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) { if (V.size() <= 3) { return V; } Sort(V); rf(i, V.size() - 1) V << V[i]; vector<pair<T, T>> r; for (pair<T, T> p : V) { int s = r.size(); while (s >= 2 && (p.second - r[s - 1].second) * (p.first - r[s - 2].first) < (p.second - r[s - 2].second) * (p.first - r[s - 1].first)) { r.pop_back(); --s; } r << p; } r.pop_back(); return r; } class UnionFind { vi p, s; void extend(int N) { foor(i, p.size(), N) { p << i; s << 1; } } public: UnionFind(void) {} UnionFind(int N) { extend(N - 1); } int find(int i) { extend(i); return p[i] = p[i] == i ? i : find(p[i]); } void unite(int a, int b) { extend(a); extend(b); if ((a = find(a)) != (b = find(b))) { if (s[a] > s[b]) { swap(a, b); } s[b] += s[a]; p[a] = b; } } void unite(pii p) { return unite(p.first, p.second); } bool same(int a, int b) { extend(a); extend(b); return find(a) == find(b); } bool same(pii p) { return same(p.first, p.second); } int size(int x) { extend(x); return s[find(x)]; } }; ll MST(vector<pair<ll, pii>> &E) { Sort(E); UnionFind uf; ll z = 0; for (auto &e : E) { if (!uf.same(e.second)) { z += e.first; uf.unite(e.second); } } return z; } ll strmod(const str &s, const int m) { ll x = 0; fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; } return x; } vvl mul(const vvl &A, const vvl &B, const int m) { vvl C; fr(y, A.size()) { C << vl(B[y].size()); } fr(y, C.size()) { fr(x, C[y].size()) { fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; } } } return C; } vvl pow(const vvl &A, const ll n, const int m) { vvl B; fr(y, A.size()) { B << vl(A.size()); } if (n == 0) { fr(i, B.size()) { B[i][i] = 1; } } elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); } else { vvl C = pow(A, n / 2, m); B = mul(C, C, m); } return B; } ll pow(const ll a, const ll n, const int m) { ll t; return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) * (t = pow(a, n >> 1, m), t * t % m) % m : !!a; } ll inv(const ll x, const int p) { assert(x != 0); return pow(x, p - 2, p); } ll inv(const ll x) { return inv(x, MD); } vpll fact(const int n, const int p) { assert(n < p); vpll v(n + 1); v[0].first = 1; foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; } v[n].second = inv(v[n].first, p); roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; } return v; } class Combination { const vpll f; const int M; public: Combination(int n, int m) : f(fact(n, m)), M(m) {} Combination(int n) : Combination(n, MD) {} ll P(int n, int k) { return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M; } ll C(int n, int k) { return k < 0 ? 0 : P(n, k) * f[k].second % M; } ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); } ll F(int n) { return n < 0 ? 0 : f[n].first; } }; ll C2(const int n) { return (ll)n * ~-n / 2; } ll sum(const vi a) { ll s = 0; for (int e : a) { s += e; } return s; } ll sum(const vl a) { ll s = 0; for (ll e : a) { s += e; } return s; } template <typename T> int MSB(T N) { int r = -1; for (; N > 0; N /= 2) { ++r; } return r; } template <typename T> class SegmentTree { vector<T> S; T (*const op)(T a, T b); const T zero; const int B; public: SegmentTree(int N, T (*f)(T a, T b), const T zero) : S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero), B(1 << MSB(N - 1) + 1) {} SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero) : SegmentTree(v.size(), f, zero) { fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; } roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); } } T calc(int l, int r) { l += B; r += B; if (l > r) { return zero; } if (l == r) { return S[l]; } T L = S[l], R = S[r]; for (; l / 2 < r / 2; l /= 2, r /= 2) { if (l % 2 == 0) { L = op(L, S[l + 1]); } if (r % 2 == 1) { R = op(S[r - 1], R); } } return op(L, R); } void replace(int i, T x) { for (S[i += B] = x; i != 1; i /= 2) { if (i % 2) { S[i / 2] = op(S[i - 1], S[i]); } else { S[i / 2] = op(S[i], S[i + 1]); } } } void add(int i, T x) { replace(i, op(S[B + i], x)); } T top() { return S[1]; } T get(int i) { return S[i + B]; } }; ll BITsum(vl &B, int i) { ll z = 0; while (i > 0) { z += B[i]; i -= i & -i; } return z; } void BITadd(vl &B, int i, ll x) { while (i < B.size()) { B[i] += x; i += i & -i; } } ll fib(const ll n, const int m) { ll a, b, c, d, A, B, C, D; a = 1; b = 0; c = 0; d = 1; rf(i, 63) { A = a * a + b * c; B = a * b + b * d; C = c * a + d * c; D = c * b + d * d; if (n >> i & 1) { a = A; b = B; c = C; d = D; A = a + b; B = a; C = c + d; D = c; } a = A % m; b = B % m; c = C % m; d = D % m; } return b; } vi primes(int n) { vb b(n + 1); vi p; foor(i, 2, n) { if (!b[i]) { p << i; for (int j = 2 * i; j <= n; j += i) { b[j] = true; } } } return p; } vb isprime(const int n) { vb v(n + 1, true); v[0] = v[1] = false; foor(i, 2, n) { if (v[i]) { for (int j = 2 * i; j <= n; j += i) { v[j] = false; } } } return v; } class LCA { vvi par; vi dep; public: LCA(vvi &E, int root) : par(MSB(E.size()) + 1, vi(E.size())), dep(E.size()) { function<void(int, int)> dfs = [&](int i, int p) { for (int j : E[i]) if (j != p) { par[0][j] = i; dep[j] = dep[i] + 1; dfs(j, i); } }; par[0][root] = root; dfs(root, root); fr(i, par.size() - 1) { fr(j, par[0].size()) { par[i + 1][j] = par[i][par[i][j]]; } } } int operator()(int a, int b) { if (dep[a] > dep[b]) swap(a, b); for (int t = dep[b] - dep[a], i = 0; t; t >>= 1, ++i) { if (t & 1) { b = par[i][b]; } } if (a == b) return a; rf(i, par.size()) { if (par[i][a] != par[i][b]) { a = par[i][a]; b = par[i][b]; } } return par[0][a]; } int dist(int a, int b) { return dep[a] + dep[b] - 2 * dep[operator()(a, b)]; } }; vpli factor(ll N) { vpli r; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) { r << pli{i, 0}; while (N % i == 0) { N /= i; ++r.back().second; } } } if (N > 1) { r << pli{N, 1}; } return r; } vl divisors(ll n) { vl r; ll m = sqrt(n); foor(i, 1, m) if (n % i == 0) r << ll(i); rf(i, r.size() - (m * m == n)) r << n / r[i]; return r; } vi SuffixArray(str S) { int N = S.size(); vi rank(N + 1), tmp(N + 1), sa(N + 1); fr(i, N) { sa[i] = i; rank[i] = S[i]; } sa[N] = N; rank[N] = -1; int k; auto cmp = [&](int &a, int &b) -> bool { if (rank[a] != rank[b]) return rank[a] < rank[b]; return (a + k <= N ? rank[a + k] : -1) < (b + k <= N ? rank[b + k] : -1); }; for (k = 1; k <= N; k *= 2) { sort(all(sa), cmp); tmp[sa[0]] = 0; foor(i, 1, N) { tmp[sa[i]] = tmp[sa[i - 1]] + cmp(sa[i - 1], sa[i]); } rank = tmp; } return sa; }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, S; cin >> N >> S; vl d(S + 1); ll z = 0; fr(i, N) { int A; cin >> A; d[S] = 0; roof(j, S - A, 1) { d[j + A] += d[j]; } d[A] += i + 1; (z += d[S] * (N - i)) %= md; } print(z); return 0; }
#include <bits/stdc++.h> #define fr(i, n) for (int i = 0; i < (n); ++i) #define foor(i, a, b) for (int i = (a); i <= (b); ++i) #define rf(i, n) for (int i = (n); i-- > 0;) #define roof(i, b, a) for (int i = (b); i >= (a); --i) #define elsif else if #define all(x) x.begin(), x.end() #define Sort(x) sort(all(x)) #define Reverse(x) reverse(all(x)) #define PQ priority_queue #define NP(x) next_permutation(all(x)) #define M_PI 3.14159265358979323846 #define popcount __builtin_popcountll using namespace std; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef unsigned long long ull; typedef vector<ull> vu; typedef vector<vu> vvu; typedef double dbl; typedef vector<dbl> vd; typedef vector<vd> vvd; typedef string str; typedef vector<str> vs; typedef vector<vs> vvs; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef map<int, int> mii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef map<ll, ll> mll; typedef pair<dbl, dbl> pdd; typedef vector<pdd> vpdd; typedef map<dbl, dbl> mdd; typedef pair<str, str> pss; typedef vector<pss> vpss; typedef map<str, str> mss; typedef pair<int, ll> pil; typedef vector<pil> vpil; typedef map<int, ll> mil; typedef pair<ll, int> pli; typedef vector<pli> vpli; typedef map<ll, int> mli; typedef pair<dbl, int> pdi; typedef vector<pdi> vpdi; typedef map<dbl, int> mdi; template <typename T> vector<T> &operator<<(vector<T> &v, const T t) { v.push_back(t); return v; } template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) { m.insert(t); return m; } template <typename T> set<T> &operator<<(set<T> &s, const T t) { s.insert(t); return s; } template <typename T> stack<T> &operator<<(stack<T> &s, const T t) { s.push(t); return s; } template <typename T> stack<T> &operator>>(stack<T> &s, T &t) { t = s.top(); s.pop(); return s; } template <typename T> queue<T> &operator<<(queue<T> &q, const T t) { q.push(t); return q; } template <typename T> queue<T> &operator>>(queue<T> &q, T &t) { t = q.front(); q.pop(); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) { q.push(t); return q; } template <typename T, typename U> PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) { t = q.top(); q.pop(); return q; } template <typename T, typename U> istream &operator>>(istream &s, pair<T, U> &p) { return s >> p.first >> p.second; } istream &operator>>(istream &s, _Bit_reference b) { int a; s >> a; assert(a == 0 || a == 1); b = a; return s; } template <typename T> istream &operator>>(istream &s, vector<T> &v) { fr(i, v.size()) { s >> v[i]; } return s; } template <typename T, typename U> ostream &operator<<(ostream &s, const pair<T, U> p) { return s << p.first << " " << p.second; } template <typename T> ostream &operator<<(ostream &s, const vector<T> v) { fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; } return s; } template <typename T> ostream &operator<<(ostream &s, const deque<T> d) { fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; } return s; } template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) { return b = b & t; } template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) { return b = b ^ t; } template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) { return b = b | t; } template <typename T, typename U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return {a.first + b.first, a.second + b.second}; } template <typename T, typename U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return {a.first - b.first, a.second - b.second}; } template <typename T, typename U> pair<T, U> &operator+=(pair<T, U> &a, pair<T, U> b) { return a = a + b; } template <typename T, typename U> pair<T, U> &operator-=(pair<T, U> &a, pair<T, U> b) { return a = a - b; } void print(void) { cout << "\n"; } void Print(void) { cout << endl; } template <typename T> void print(T t) { cout << t << "\n"; } template <typename T> void Print(T t) { cout << t << endl; } template <typename T, typename... U> void print(T &&t, U &&...u) { cout << t << " "; print(forward<U>(u)...); } template <typename T, typename... U> void Print(T &&t, U &&...u) { cout << t << " "; Print(forward<U>(u)...); } bool YN(bool b) { print(b ? "YES" : "NO"); return b; } bool PI(bool b) { print(b ? "POSSIBLE" : "IMPOSSIBLE"); return b; } bool Yn(bool b) { print(b ? "Yes" : "No"); return b; } bool Pi(bool b) { print(b ? "Possible" : "Impossible"); return b; } bool yn(bool b) { print(b ? "yes" : "no"); return b; } bool pi(bool b) { print(b ? "possible" : "impossible"); return b; } const int e5 = 1e5; const int e9 = 1e9; const int MD = 1e9 + 7; const int md = 998244353; const ll e18 = 1e18; template <typename T> str to_string(const T &n) { ostringstream s; s << n; return s.str(); } template <typename T> T &chmax(T &a, T b) { return a = max(a, b); } template <typename T> T &chmin(T &a, T b) { return a = min(a, b); } template <typename T, typename U> vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s, const T inf) { using P = pair<T, U>; vector<P> d; fr(i, E.size()) { d << P{inf, i}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } template <typename T, typename U> map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s, const T inf) { using P = pair<T, U>; map<U, P> d; for (pair<U, vector<P>> e : E) { d[e.first] = P{inf, e.first}; } PQ<P, vector<P>, greater<P>> pq; pq << (d[s] = P{0, s}); while (pq.size()) { P a = pq.top(); pq.pop(); U v = a.second; if (d[v].first >= a.first) { for (P e : E[v]) { if (d[v].first + e.first < d[e.second].first) { d[e.second] = P{d[v].first + e.first, v}; pq << P{d[v].first + e.first, e.second}; } } } } return d; } ll maxflow(vector<mil> &E, int s, int t) { ll z = 0; vi b(E.size(), -1); for (int i = 0;; ++i) { static auto dfs = [&](int v, ll f, auto &dfs) -> ll { if (v == t) return f; b[v] = i; for (auto &p : E[v]) { if (b[p.first] < i && p.second) { if (ll r = dfs(p.first, min(f, p.second), dfs)) { p.second -= r; E[p.first][v] += r; return r; } } } return 0; }; ll x = dfs(s, ll(1e18), dfs); z += x; if (x == 0) return z; } } template <typename T> T distsq(pair<T, T> a, pair<T, T> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } template <typename T> T max(const vector<T> a) { assert(a.size()); T m = a[0]; for (T e : a) { m = max(m, e); } return m; } template <typename T> T min(const vector<T> a) { assert(a.size()); T m = a[0]; for (T e : a) { m = min(m, e); } return m; } template <typename T> T gcd(const T a, const T b) { return a ? gcd(b % a, a) : b; } template <typename T> T gcd(const vector<T> a) { T g = a[0]; for (T e : a) { g = gcd(g, e); } return g; } template <typename T> vector<T> LCS(vector<T> A, vector<T> B) { int N = A.size(), M = B.size(); vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1)); fr(i, N) { fr(j, M) { if (A[i] == B[j]) { d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}}; } else { d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]); } } } vector<T> r; for (pii p = {N, M}; d[p.first][p.second].first; p = d[p.first][p.second].second) { r << A[d[p.first][p.second].second.first]; } Reverse(r); return r; } str LCS(str S, str T) { vector<char> s = LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end())); return str(s.begin(), s.end()); } template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) { if (V.size() <= 3) { return V; } Sort(V); rf(i, V.size() - 1) V << V[i]; vector<pair<T, T>> r; for (pair<T, T> p : V) { int s = r.size(); while (s >= 2 && (p.second - r[s - 1].second) * (p.first - r[s - 2].first) < (p.second - r[s - 2].second) * (p.first - r[s - 1].first)) { r.pop_back(); --s; } r << p; } r.pop_back(); return r; } class UnionFind { vi p, s; void extend(int N) { foor(i, p.size(), N) { p << i; s << 1; } } public: UnionFind(void) {} UnionFind(int N) { extend(N - 1); } int find(int i) { extend(i); return p[i] = p[i] == i ? i : find(p[i]); } void unite(int a, int b) { extend(a); extend(b); if ((a = find(a)) != (b = find(b))) { if (s[a] > s[b]) { swap(a, b); } s[b] += s[a]; p[a] = b; } } void unite(pii p) { return unite(p.first, p.second); } bool same(int a, int b) { extend(a); extend(b); return find(a) == find(b); } bool same(pii p) { return same(p.first, p.second); } int size(int x) { extend(x); return s[find(x)]; } }; ll MST(vector<pair<ll, pii>> &E) { Sort(E); UnionFind uf; ll z = 0; for (auto &e : E) { if (!uf.same(e.second)) { z += e.first; uf.unite(e.second); } } return z; } ll strmod(const str &s, const int m) { ll x = 0; fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; } return x; } vvl mul(const vvl &A, const vvl &B, const int m) { vvl C; fr(y, A.size()) { C << vl(B[y].size()); } fr(y, C.size()) { fr(x, C[y].size()) { fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; } } } return C; } vvl pow(const vvl &A, const ll n, const int m) { vvl B; fr(y, A.size()) { B << vl(A.size()); } if (n == 0) { fr(i, B.size()) { B[i][i] = 1; } } elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); } else { vvl C = pow(A, n / 2, m); B = mul(C, C, m); } return B; } ll pow(const ll a, const ll n, const int m) { ll t; return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) * (t = pow(a, n >> 1, m), t * t % m) % m : !!a; } ll inv(const ll x, const int p) { assert(x != 0); return pow(x, p - 2, p); } ll inv(const ll x) { return inv(x, MD); } vpll fact(const int n, const int p) { assert(n < p); vpll v(n + 1); v[0].first = 1; foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; } v[n].second = inv(v[n].first, p); roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; } return v; } class Combination { const vpll f; const int M; public: Combination(int n, int m) : f(fact(n, m)), M(m) {} Combination(int n) : Combination(n, MD) {} ll P(int n, int k) { return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M; } ll C(int n, int k) { return k < 0 ? 0 : P(n, k) * f[k].second % M; } ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); } ll F(int n) { return n < 0 ? 0 : f[n].first; } }; ll C2(const int n) { return (ll)n * ~-n / 2; } ll sum(const vi a) { ll s = 0; for (int e : a) { s += e; } return s; } ll sum(const vl a) { ll s = 0; for (ll e : a) { s += e; } return s; } template <typename T> int MSB(T N) { int r = -1; for (; N > 0; N /= 2) { ++r; } return r; } template <typename T> class SegmentTree { vector<T> S; T (*const op)(T a, T b); const T zero; const int B; public: SegmentTree(int N, T (*f)(T a, T b), const T zero) : S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero), B(1 << MSB(N - 1) + 1) {} SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero) : SegmentTree(v.size(), f, zero) { fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; } roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); } } T calc(int l, int r) { l += B; r += B; if (l > r) { return zero; } if (l == r) { return S[l]; } T L = S[l], R = S[r]; for (; l / 2 < r / 2; l /= 2, r /= 2) { if (l % 2 == 0) { L = op(L, S[l + 1]); } if (r % 2 == 1) { R = op(S[r - 1], R); } } return op(L, R); } void replace(int i, T x) { for (S[i += B] = x; i != 1; i /= 2) { if (i % 2) { S[i / 2] = op(S[i - 1], S[i]); } else { S[i / 2] = op(S[i], S[i + 1]); } } } void add(int i, T x) { replace(i, op(S[B + i], x)); } T top() { return S[1]; } T get(int i) { return S[i + B]; } }; ll BITsum(vl &B, int i) { ll z = 0; while (i > 0) { z += B[i]; i -= i & -i; } return z; } void BITadd(vl &B, int i, ll x) { while (i < B.size()) { B[i] += x; i += i & -i; } } ll fib(const ll n, const int m) { ll a, b, c, d, A, B, C, D; a = 1; b = 0; c = 0; d = 1; rf(i, 63) { A = a * a + b * c; B = a * b + b * d; C = c * a + d * c; D = c * b + d * d; if (n >> i & 1) { a = A; b = B; c = C; d = D; A = a + b; B = a; C = c + d; D = c; } a = A % m; b = B % m; c = C % m; d = D % m; } return b; } vi primes(int n) { vb b(n + 1); vi p; foor(i, 2, n) { if (!b[i]) { p << i; for (int j = 2 * i; j <= n; j += i) { b[j] = true; } } } return p; } vb isprime(const int n) { vb v(n + 1, true); v[0] = v[1] = false; foor(i, 2, n) { if (v[i]) { for (int j = 2 * i; j <= n; j += i) { v[j] = false; } } } return v; } class LCA { vvi par; vi dep; public: LCA(vvi &E, int root) : par(MSB(E.size()) + 1, vi(E.size())), dep(E.size()) { function<void(int, int)> dfs = [&](int i, int p) { for (int j : E[i]) if (j != p) { par[0][j] = i; dep[j] = dep[i] + 1; dfs(j, i); } }; par[0][root] = root; dfs(root, root); fr(i, par.size() - 1) { fr(j, par[0].size()) { par[i + 1][j] = par[i][par[i][j]]; } } } int operator()(int a, int b) { if (dep[a] > dep[b]) swap(a, b); for (int t = dep[b] - dep[a], i = 0; t; t >>= 1, ++i) { if (t & 1) { b = par[i][b]; } } if (a == b) return a; rf(i, par.size()) { if (par[i][a] != par[i][b]) { a = par[i][a]; b = par[i][b]; } } return par[0][a]; } int dist(int a, int b) { return dep[a] + dep[b] - 2 * dep[operator()(a, b)]; } }; vpli factor(ll N) { vpli r; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) { r << pli{i, 0}; while (N % i == 0) { N /= i; ++r.back().second; } } } if (N > 1) { r << pli{N, 1}; } return r; } vl divisors(ll n) { vl r; ll m = sqrt(n); foor(i, 1, m) if (n % i == 0) r << ll(i); rf(i, r.size() - (m * m == n)) r << n / r[i]; return r; } vi SuffixArray(str S) { int N = S.size(); vi rank(N + 1), tmp(N + 1), sa(N + 1); fr(i, N) { sa[i] = i; rank[i] = S[i]; } sa[N] = N; rank[N] = -1; int k; auto cmp = [&](int &a, int &b) -> bool { if (rank[a] != rank[b]) return rank[a] < rank[b]; return (a + k <= N ? rank[a + k] : -1) < (b + k <= N ? rank[b + k] : -1); }; for (k = 1; k <= N; k *= 2) { sort(all(sa), cmp); tmp[sa[0]] = 0; foor(i, 1, N) { tmp[sa[i]] = tmp[sa[i - 1]] + cmp(sa[i - 1], sa[i]); } rank = tmp; } return sa; }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, S; cin >> N >> S; vl d(S + 1); ll z = 0; fr(i, N) { int A; cin >> A; d[S] = 0; roof(j, S - A, 1) { (d[j + A] += d[j]) %= md; } if (A <= S) (d[A] += i + 1) %= md; (z += d[S] * (N - i)) %= md; } print(z); return 0; }
replace
688
690
688
691
0
p02734
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; using ll = long long; // Modint struct Mint { static const ll mod = 998244353; ll val; Mint() { val = 0; } Mint(ll a) { val = a; verify_value(); } void verify_value() { if (val >= mod) val %= mod; if (val < 0) val %= mod, val += mod; } Mint pow(ll p) const { Mint cur = Mint(val), ret = 1; while (p > 0) { if (p & 1) ret *= cur; cur *= cur; p >>= 1LL; } return ret; } Mint inv() const { if (val == 0) cerr << "WARNING: inv() is called with 0." << endl; return pow(mod - 2); } Mint operator+() const { return *this; } Mint operator-() const { return Mint(mod - val); } Mint operator+=(const Mint &a) { val += a.val; if (val >= mod) val -= mod; return Mint(val); } Mint operator*=(const Mint &a) { val *= a.val; if (val >= mod) val %= mod; return Mint(val); } Mint operator-=(const Mint &a) { return *this += -a; } Mint operator/=(const Mint &a) { return *this *= a.inv(); } Mint operator++() { return *this += Mint(1); } Mint operator--() { return *this -= Mint(1); } Mint operator++(int) { Mint ret = *this; ++(*this); return ret; } Mint operator--(int) { Mint ret = *this; --(*this); return ret; } operator ll() const { return val; } }; Mint operator+(const Mint &a, const Mint &b) { ll ret = a.val + b.val; if (ret >= Mint::mod) ret -= Mint::mod; return Mint(ret); } Mint operator*(const Mint &a, const Mint &b) { ll ret = a.val * b.val; if (ret >= Mint::mod) ret %= Mint::mod; return Mint(ret); } Mint operator-(const Mint &a, const Mint &b) { return a + (-b); } Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); } ostream &operator<<(ostream &out, const Mint &a) { return out << a.val; } istream &operator>>(istream &in, Mint &a) { in >> a.val; a.verify_value(); return in; } Mint pow(Mint a, ll b) { return a.pow(b); } // Combinatorics constexpr int MAX_N = 2000003; Mint fact[MAX_N], inv[MAX_N]; void init_fact() { fact[0] = inv[0] = 1; for (ll i = 1; i < MAX_N; i++) { fact[i] = fact[i - 1] * Mint(i); inv[i] = fact[i].inv(); } } // aCb Mint C(int a, int b) { if (a < b) return 0; Mint res = fact[a]; res *= inv[b]; res *= inv[a - b]; return res; } // aPb Mint P(int a, int b) { if (a < b) return 0; return fact[a] * inv[a - b]; } // aHb Mint H(int a, int b) { if (b == 0) return 1; return C(a + b - 1, b); } int main() { int n, s; cin >> n >> s; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<Mint> cnt(3 * s); cnt[0] = 1; for (int i = 0; i < n; i++) { for (int j = s; j >= 0; j--) { Mint cf = 1; if (j == 0) cf *= i + 1; if (j + a[i] == s) cf *= n - i; cnt[j + a[i]] += cf * cnt[j]; } } cout << cnt[s] << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; using ll = long long; // Modint struct Mint { static const ll mod = 998244353; ll val; Mint() { val = 0; } Mint(ll a) { val = a; verify_value(); } void verify_value() { if (val >= mod) val %= mod; if (val < 0) val %= mod, val += mod; } Mint pow(ll p) const { Mint cur = Mint(val), ret = 1; while (p > 0) { if (p & 1) ret *= cur; cur *= cur; p >>= 1LL; } return ret; } Mint inv() const { if (val == 0) cerr << "WARNING: inv() is called with 0." << endl; return pow(mod - 2); } Mint operator+() const { return *this; } Mint operator-() const { return Mint(mod - val); } Mint operator+=(const Mint &a) { val += a.val; if (val >= mod) val -= mod; return Mint(val); } Mint operator*=(const Mint &a) { val *= a.val; if (val >= mod) val %= mod; return Mint(val); } Mint operator-=(const Mint &a) { return *this += -a; } Mint operator/=(const Mint &a) { return *this *= a.inv(); } Mint operator++() { return *this += Mint(1); } Mint operator--() { return *this -= Mint(1); } Mint operator++(int) { Mint ret = *this; ++(*this); return ret; } Mint operator--(int) { Mint ret = *this; --(*this); return ret; } operator ll() const { return val; } }; Mint operator+(const Mint &a, const Mint &b) { ll ret = a.val + b.val; if (ret >= Mint::mod) ret -= Mint::mod; return Mint(ret); } Mint operator*(const Mint &a, const Mint &b) { ll ret = a.val * b.val; if (ret >= Mint::mod) ret %= Mint::mod; return Mint(ret); } Mint operator-(const Mint &a, const Mint &b) { return a + (-b); } Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); } ostream &operator<<(ostream &out, const Mint &a) { return out << a.val; } istream &operator>>(istream &in, Mint &a) { in >> a.val; a.verify_value(); return in; } Mint pow(Mint a, ll b) { return a.pow(b); } // Combinatorics constexpr int MAX_N = 2000003; Mint fact[MAX_N], inv[MAX_N]; void init_fact() { fact[0] = inv[0] = 1; for (ll i = 1; i < MAX_N; i++) { fact[i] = fact[i - 1] * Mint(i); inv[i] = fact[i].inv(); } } // aCb Mint C(int a, int b) { if (a < b) return 0; Mint res = fact[a]; res *= inv[b]; res *= inv[a - b]; return res; } // aPb Mint P(int a, int b) { if (a < b) return 0; return fact[a] * inv[a - b]; } // aHb Mint H(int a, int b) { if (b == 0) return 1; return C(a + b - 1, b); } int main() { int n, s; cin >> n >> s; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<Mint> cnt(n * s + 2); cnt[0] = 1; for (int i = 0; i < n; i++) { for (int j = s; j >= 0; j--) { Mint cf = 1; if (j == 0) cf *= i + 1; if (j + a[i] == s) cf *= n - i; cnt[j + a[i]] += cf * cnt[j]; } } cout << cnt[s] << endl; return 0; }
replace
152
153
152
153
0
p02734
C++
Runtime Error
// Etavioxy #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #define il inline #define ll long long #define rep(i, s, t) for (register int i = (s); i <= (t); i++) #define rev_rep(i, s, t) for (register int i = (s); i >= (t); i--) #define each(i, u) for (int i = head[u]; i; i = bow[i].nxt) #define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout) #define pt(x) putchar(x) using namespace std; il int ci() { register char ch; int f = 1; while (!isdigit(ch = getchar())) f = ch == '-' ? -1 : 1; register int x = ch ^ '0'; while (isdigit(ch = getchar())) x = (x * 10) + (ch ^ '0'); return f * x; } const int mod = 998244353; enum { N = 3023 }; ll f[N]; int main() { int n = ci(), s = ci(); ll ans = 0; rep(i, 1, n) { int x = ci(); ans = (ans + (n - i + 1) * f[s - x]) % mod; rev_rep(j, s, x) f[j] = (f[j] + f[j - x]); f[x] += i; } printf("%lld\n", (ans + mod) % mod); return 0; }
// Etavioxy #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #define il inline #define ll long long #define rep(i, s, t) for (register int i = (s); i <= (t); i++) #define rev_rep(i, s, t) for (register int i = (s); i >= (t); i--) #define each(i, u) for (int i = head[u]; i; i = bow[i].nxt) #define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout) #define pt(x) putchar(x) using namespace std; il int ci() { register char ch; int f = 1; while (!isdigit(ch = getchar())) f = ch == '-' ? -1 : 1; register int x = ch ^ '0'; while (isdigit(ch = getchar())) x = (x * 10) + (ch ^ '0'); return f * x; } const int mod = 998244353; enum { N = 3023 }; ll f[N]; int main() { int n = ci(), s = ci(); ll ans = 0; rep(i, 1, n) { int x = ci(); if (x > s) continue; ans = (ans + (n - i + 1) * (f[s - x] + (x == s ? i : 0))) % mod; rev_rep(j, s, x) f[j] = (f[j] + f[j - x]) % mod; f[x] = (f[x] + i) % mod; } printf("%lld\n", (ans + mod) % mod); return 0; }
replace
34
37
34
39
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef vector<ll> vll; #define repi(i, a, b) for (ll i = a; i < b; i++) #define rep(i, a) repi(i, 0, a) #define rrep(i, a) for (ll i = a - 1; i >= 0; i--) #define MOD 998244353 // debug #define debug(arr) \ cerr << #arr << "(l" << __LINE__ << ") : "; \ for (auto x : arr) \ cerr << x << " "; \ cerr << endl; int main() { ll N, S; cin >> N >> S; vll A(N); rep(i, N) cin >> A[i]; vector<vll> dp(2, vll(S + 1, 0)); ll ans = 0; rep(i, N) { rep(s, S + 1) dp[(i + 1) % 2][s] = dp[i % 2][s]; rep(s, S + 1 - A[i]) dp[(i + 1) % 2][s + A[i]] = (dp[(i + 1) % 2][s + A[i]] + dp[i % 2][s]) % MOD; dp[(i + 1) % 2][A[i]] = (dp[(i + 1) % 2][A[i]] + i + 1) % MOD; ans = (ans + dp[(i + 1) % 2][S]) % MOD; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef vector<ll> vll; #define repi(i, a, b) for (ll i = a; i < b; i++) #define rep(i, a) repi(i, 0, a) #define rrep(i, a) for (ll i = a - 1; i >= 0; i--) #define MOD 998244353 // debug #define debug(arr) \ cerr << #arr << "(l" << __LINE__ << ") : "; \ for (auto x : arr) \ cerr << x << " "; \ cerr << endl; int main() { ll N, S; cin >> N >> S; vll A(N); rep(i, N) cin >> A[i]; vector<vll> dp(2, vll(S + 1, 0)); ll ans = 0; rep(i, N) { rep(s, S + 1) dp[(i + 1) % 2][s] = dp[i % 2][s]; rep(s, S + 1 - A[i]) dp[(i + 1) % 2][s + A[i]] = (dp[(i + 1) % 2][s + A[i]] + dp[i % 2][s]) % MOD; if (A[i] <= S) dp[(i + 1) % 2][A[i]] = (dp[(i + 1) % 2][A[i]] + i + 1) % MOD; ans = (ans + dp[(i + 1) % 2][S]) % MOD; } cout << ans << endl; return 0; }
replace
30
31
30
32
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define ll long long #define ii pair<int, int> #define vi vector<int> #define vll vector<ll> #define vii vector<ii> const int maxn = 3e3 + 10; const int mod = 998244353; int n, s; int memo[maxn][maxn], vs[maxn]; using namespace std; int sum(int x, int v) { return (x % mod + v % mod) % mod; } int dp(int pos, int soma) { if (pos > n) return 0; if (soma == s) return n - pos + 1; int &val = memo[pos][soma]; if (val != -1) return val; return val = sum(dp(pos + 1, soma), dp(pos + 1, soma + vs[pos])); } void solve() { memset(memo, -1, sizeof memo); cin >> n >> s; int ans = 0; for (int i = 0; i < n; ++i) cin >> vs[i]; for (int i = 0; i < n; ++i) { // cout<<dp(i,0)<<endl; ans = sum(ans, dp(i, 0)); } cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define ll long long #define ii pair<int, int> #define vi vector<int> #define vll vector<ll> #define vii vector<ii> const int maxn = 3e3 + 10; const int mod = 998244353; int n, s; int memo[maxn][maxn], vs[maxn]; using namespace std; int sum(int x, int v) { return (x % mod + v % mod) % mod; } int dp(int pos, int soma) { if (pos > n) return 0; if (soma == s) return n - pos + 1; if (soma > s) return 0; int &val = memo[pos][soma]; if (val != -1) return val; return val = sum(dp(pos + 1, soma), dp(pos + 1, soma + vs[pos])); } void solve() { memset(memo, -1, sizeof memo); cin >> n >> s; int ans = 0; for (int i = 0; i < n; ++i) cin >> vs[i]; for (int i = 0; i < n; ++i) { // cout<<dp(i,0)<<endl; ans = sum(ans, dp(i, 0)); } cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
insert
29
29
29
31
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define syosu(x) fixed << setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<int, P> pip; typedef vector<pip> vip; const int inf = 1 << 30; const ll INF = 1ll << 60; const double pi = acos(-1); const double eps = 1e-8; const ll mod = 998244353; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; int n, m; int main() { cin >> n >> m; vl dp(m + 1); ll res = 0; for (int j = 0; j < n; j++) { int x; cin >> x; vl DP(m + 1); (DP[x] += j + 1) %= mod; for (int j = 0; j <= m - x; j++) (DP[j + x] += dp[j]) %= mod; (res += DP[m] * (n - j)) %= mod; for (int j = 0; j <= m; j++) (dp[j] += DP[j]) %= mod; } cout << res << endl; }
#include <bits/stdc++.h> #define syosu(x) fixed << setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<int, P> pip; typedef vector<pip> vip; const int inf = 1 << 30; const ll INF = 1ll << 60; const double pi = acos(-1); const double eps = 1e-8; const ll mod = 998244353; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; int n, m; int main() { cin >> n >> m; vl dp(m + 1); ll res = 0; for (int j = 0; j < n; j++) { int x; cin >> x; if (x > m) continue; vl DP(m + 1); (DP[x] += j + 1) %= mod; for (int j = 0; j <= m - x; j++) (DP[j + x] += dp[j]) %= mod; (res += DP[m] * (n - j)) %= mod; for (int j = 0; j <= m; j++) (dp[j] += DP[j]) %= mod; } cout << res << endl; }
insert
37
37
37
39
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #include <algorithm> #include <array> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder #include <utility> namespace atcoder { namespace internal { // @param m `1 <= m` // @return x mod m constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } // Fast moduler by barrett reduction // Reference: https://en.wikipedia.org/wiki/Barrett_reduction // NOTE: reconsider after Ice Lake struct barrett { unsigned int _m; unsigned long long im; // @param m `1 <= m` barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} // @return m unsigned int umod() const { return _m; } // @param a `0 <= a < m` // @param b `0 <= b < m` // @return `a * b % m` unsigned int mul(unsigned int a, unsigned int b) const { // [1] m = 1 // a = b = im = 0, so okay // [2] m >= 2 // im = ceil(2^64 / m) // -> im * m = 2^64 + r (0 <= r < m) // let z = a*b = c*m + d (0 <= c, d < m) // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < // 2^64 * 2 // ((ab * im) >> 64) == c or c + 1 unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; // @param n `0 <= n` // @param m `1 <= m` // @return `(x ** n) % m` constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } // Reference: // M. Forisek and J. Jancina, // Fast Primality Testing for Integers That Fit into a Machine Word // @param n `0 <= n` constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; for (long long a : {2, 7, 61}) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template <int n> constexpr bool is_prime = is_prime_constexpr(n); // @param b `1 <= b` // @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; // Contracts: // [1] s - m0 * a = 0 (mod b) // [2] t - m1 * a = 0 (mod b) // [3] s * |m1| + t * |m0| <= b long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b // [3]: // (s - t * u) * |m1| + t * |m0 - m1 * u| // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u) // = s * |m1| + t * |m0| <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } // by [3]: |m0| <= b/g // by g != b: |m0| < b/g if (m0 < 0) m0 += b / s; return {s, m0}; } // Compile time primitive root // @param m must be prime // @return primitive root (and minimum in now) constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template <int m> constexpr int primitive_root = primitive_root_constexpr(m); } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template <class T> using is_modint = std::is_base_of<modint_base, T>; template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>; } // namespace internal template <int m, std::enable_if_t<(1 <= m)> * = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T> * = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T> * = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } static_modint(bool v) { _v = ((unsigned int)(v) % umod()); } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator-=(const mint &rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint &operator*=(const mint &rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime<m>; }; template <int id> struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T> * = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T> * = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator-=(const mint &rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator*=(const mint &rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template <int id> internal::barrett dynamic_modint<id>::bt = 998244353; using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template <class T> using is_static_modint = std::is_base_of<internal::static_modint_base, T>; template <class T> using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>; template <class> struct is_dynamic_modint : public std::false_type {}; template <int id> struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {}; template <class T> using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>; } // namespace internal } // namespace atcoder #include <cassert> #include <type_traits> #include <vector> namespace atcoder { namespace internal { template <class mint, internal::is_static_modint_t<mint> * = nullptr> void butterfly(std::vector<mint> &a) { static constexpr int g = internal::primitive_root<mint::mod()>; int n = int(a.size()); int h = internal::ceil_pow2(n); static bool first = true; static mint sum_e[30]; // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i] if (first) { first = false; mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1 int cnt2 = bsf(mint::mod() - 1); mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv(); for (int i = cnt2; i >= 2; i--) { // e^(2^i) == 1 es[i - 2] = e; ies[i - 2] = ie; e *= e; ie *= ie; } mint now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_e[i] = es[i] * now; now *= ies[i]; } } for (int ph = 1; ph <= h; ph++) { int w = 1 << (ph - 1), p = 1 << (h - ph); mint now = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p] * now; a[i + offset] = l + r; a[i + offset + p] = l - r; } now *= sum_e[bsf(~(unsigned int)(s))]; } } } template <class mint, internal::is_static_modint_t<mint> * = nullptr> void butterfly_inv(std::vector<mint> &a) { static constexpr int g = internal::primitive_root<mint::mod()>; int n = int(a.size()); int h = internal::ceil_pow2(n); static bool first = true; static mint sum_ie[30]; // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i] if (first) { first = false; mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1 int cnt2 = bsf(mint::mod() - 1); mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv(); for (int i = cnt2; i >= 2; i--) { // e^(2^i) == 1 es[i - 2] = e; ies[i - 2] = ie; e *= e; ie *= ie; } mint now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_ie[i] = ies[i] * now; now *= es[i]; } } for (int ph = h; ph >= 1; ph--) { int w = 1 << (ph - 1), p = 1 << (h - ph); mint inow = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * inow.val(); } inow *= sum_ie[bsf(~(unsigned int)(s))]; } } } } // namespace internal template <class mint, internal::is_static_modint_t<mint> * = nullptr> std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) { if (n < m) { std::swap(n, m); std::swap(a, b); } std::vector<mint> ans(n + m - 1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; } } return ans; } int z = 1 << internal::ceil_pow2(n + m - 1); a.resize(z); internal::butterfly(a); b.resize(z); internal::butterfly(b); for (int i = 0; i < z; i++) { a[i] *= b[i]; } internal::butterfly_inv(a); a.resize(n + m - 1); mint iz = mint(z).inv(); for (int i = 0; i < n + m - 1; i++) a[i] *= iz; return a; } template <unsigned int mod = 998244353, class T, std::enable_if_t<internal::is_integral<T>::value> * = nullptr> std::vector<T> convolution(const std::vector<T> &a, const std::vector<T> &b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; using mint = static_modint<mod>; std::vector<mint> a2(n), b2(m); for (int i = 0; i < n; i++) { a2[i] = mint(a[i]); } for (int i = 0; i < m; i++) { b2[i] = mint(b[i]); } auto c2 = convolution(move(a2), move(b2)); std::vector<T> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { c[i] = c2[i].val(); } return c; } std::vector<long long> convolution_ll(const std::vector<long long> &a, const std::vector<long long> &b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; static constexpr unsigned long long MOD1 = 754974721; // 2^24 static constexpr unsigned long long MOD2 = 167772161; // 2^25 static constexpr unsigned long long MOD3 = 469762049; // 2^26 static constexpr unsigned long long M2M3 = MOD2 * MOD3; static constexpr unsigned long long M1M3 = MOD1 * MOD3; static constexpr unsigned long long M1M2 = MOD1 * MOD2; static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3; static constexpr unsigned long long i1 = internal::inv_gcd(MOD2 * MOD3, MOD1).second; static constexpr unsigned long long i2 = internal::inv_gcd(MOD1 * MOD3, MOD2).second; static constexpr unsigned long long i3 = internal::inv_gcd(MOD1 * MOD2, MOD3).second; auto c1 = convolution<MOD1>(a, b); auto c2 = convolution<MOD2>(a, b); auto c3 = convolution<MOD3>(a, b); std::vector<long long> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { unsigned long long x = 0; x += (c1[i] * i1) % MOD1 * M2M3; x += (c2[i] * i2) % MOD2 * M1M3; x += (c3[i] * i3) % MOD3 * M1M2; // B = 2^63, -B <= x, r(real value) < B // (x, x - M, x - 2M, or x - 3M) = r (mod 2B) // r = c1[i] (mod MOD1) // focus on MOD1 // r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B) // r = x, // x - M' + (0 or 2B), // x - 2M' + (0, 2B or 4B), // x - 3M' + (0, 2B, 4B or 6B) (without mod!) // (r - x) = 0, (0) // - M' + (0 or 2B), (1) // -2M' + (0 or 2B or 4B), (2) // -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1) // we checked that // ((1) mod MOD1) mod 5 = 2 // ((2) mod MOD1) mod 5 = 3 // ((3) mod MOD1) mod 5 = 4 long long diff = c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1)); if (diff < 0) diff += MOD1; static constexpr unsigned long long offset[5] = {0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3}; x -= offset[diff % 5]; c[i] = x; } return c; } } // namespace atcoder #include <algorithm> #include <cassert> #include <vector> namespace atcoder { // Implement (union by size) + (path compression) // Reference: // Zvi Galil and Giuseppe F. Italiano, // Data structures and algorithms for disjoint set union problems struct dsu { public: dsu() : _n(0) {} dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int> &v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; }; } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { // Reference: https://en.wikipedia.org/wiki/Fenwick_tree template <class T> struct fenwick_tree { using U = internal::to_unsigned_t<T>; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector<U> data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; } // namespace atcoder #include <algorithm> #include <cassert> #include <iostream> #include <vector> namespace atcoder { template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S> &v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder #include <algorithm> #include <cassert> #include <tuple> #include <vector> namespace atcoder { long long pow_mod(long long x, long long n, int m) { assert(0 <= n && 1 <= m); if (m == 1) return 0; internal::barrett bt((unsigned int)(m)); unsigned int r = 1, y = (unsigned int)(internal::safe_mod(x, m)); while (n) { if (n & 1) r = bt.mul(r, y); y = bt.mul(y, y); n >>= 1; } return r; } long long inv_mod(long long x, long long m) { assert(1 <= m); auto z = internal::inv_gcd(x, m); assert(z.first == 1); return z.second; } // (rem, mod) std::pair<long long, long long> crt(const std::vector<long long> &r, const std::vector<long long> &m) { assert(r.size() == m.size()); int n = int(r.size()); // Contracts: 0 <= r0 < m0 long long r0 = 0, m0 = 1; for (int i = 0; i < n; i++) { assert(1 <= m[i]); long long r1 = internal::safe_mod(r[i], m[i]), m1 = m[i]; if (m0 < m1) { std::swap(r0, r1); std::swap(m0, m1); } if (m0 % m1 == 0) { if (r0 % m1 != r1) return {0, 0}; continue; } // assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1) // (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1)); // r2 % m0 = r0 // r2 % m1 = r1 // -> (r0 + x*m0) % m1 = r1 // -> x*u0*g % (u1*g) = (r1 - r0) (u0*g = m0, u1*g = m1) // -> x = (r1 - r0) / g * inv(u0) (mod u1) // im = inv(u0) (mod u1) (0 <= im < u1) long long g, im; std::tie(g, im) = internal::inv_gcd(m0, m1); long long u1 = (m1 / g); // |r1 - r0| < (m0 + m1) <= lcm(m0, m1) if ((r1 - r0) % g) return {0, 0}; // u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1) long long x = (r1 - r0) / g % u1 * im % u1; // |r0| + |m0 * x| // < m0 + m0 * (u1 - 1) // = m0 + m0 * m1 / g - m0 // = lcm(m0, m1) r0 += x * m0; m0 *= u1; // -> lcm(m0, m1) if (r0 < 0) r0 += m0; } return {r0, m0}; } long long floor_sum(long long n, long long m, long long a, long long b) { long long ans = 0; if (a >= m) { ans += (n - 1) * n * (a / m) / 2; a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } long long y_max = (a * n + b) / m, x_max = (y_max * m - b); if (y_max == 0) return ans; ans += (n - (x_max + a - 1) / a) * y_max; ans += floor_sum(y_max, a, m, (a - x_max % a) % a); return ans; } } // namespace atcoder #include <algorithm> #include <vector> namespace atcoder { namespace internal { template <class T> struct simple_queue { std::vector<T> payload; int pos = 0; void reserve(int n) { payload.reserve(n); } int size() const { return int(payload.size()) - pos; } bool empty() const { return pos == int(payload.size()); } void push(const T &t) { payload.push_back(t); } T &front() { return payload[pos]; } void clear() { payload.clear(); pos = 0; } void pop() { pos++; } }; } // namespace internal } // namespace atcoder #include <cassert> #include <limits> #include <queue> #include <vector> namespace atcoder { template <class Cap> struct mf_graph { public: mf_graph() : _n(0) {} mf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); g[from].push_back(_edge{to, int(g[to].size()), cap}); g[to].push_back(_edge{from, int(g[from].size()) - 1, 0}); return m; } struct edge { int from, to; Cap cap, flow; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap}; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result; for (int i = 0; i < m; i++) { result.push_back(get_edge(i)); } return result; } void change_edge(int i, Cap new_cap, Cap new_flow) { int m = int(pos.size()); assert(0 <= i && i < m); assert(0 <= new_flow && new_flow <= new_cap); auto &_e = g[pos[i].first][pos[i].second]; auto &_re = g[_e.to][_e.rev]; _e.cap = new_cap - new_flow; _re.cap = new_flow; } Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } Cap flow(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); std::vector<int> level(_n), iter(_n); internal::simple_queue<int> que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.push(e.to); } } }; auto dfs = [&](auto self, int v, Cap up) { if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int &i = iter[v]; i < int(g[v].size()); i++) { _edge &e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) break; } return res; }; Cap flow = 0; while (flow < flow_limit) { bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); while (flow < flow_limit) { Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } } return flow; } std::vector<bool> min_cut(int s) { std::vector<bool> visited(_n); internal::simple_queue<int> que; que.push(s); while (!que.empty()) { int p = que.front(); que.pop(); visited[p] = true; for (auto e : g[p]) { if (e.cap && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } } return visited; } private: int _n; struct _edge { int to, rev; Cap cap; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; } // namespace atcoder #include <algorithm> #include <cassert> #include <limits> #include <queue> #include <vector> namespace atcoder { template <class Cap, class Cost> struct mcf_graph { public: mcf_graph() {} mcf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); g[from].push_back(_edge{to, int(g[to].size()), cap, cost}); g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{ pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, }; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result(m); for (int i = 0; i < m; i++) { result[i] = get_edge(i); } return result; } std::pair<Cap, Cost> flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) { return slope(s, t, flow_limit).back(); } std::vector<std::pair<Cap, Cost>> slope(int s, int t) { return slope(s, t, std::numeric_limits<Cap>::max()); } std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); // variants (C = maxcost): // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0 // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge std::vector<Cost> dual(_n, 0), dist(_n); std::vector<int> pv(_n), pe(_n); std::vector<bool> vis(_n); auto dual_ref = [&]() { std::fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max()); std::fill(pv.begin(), pv.end(), -1); std::fill(pe.begin(), pe.end(), -1); std::fill(vis.begin(), vis.end(), false); struct Q { Cost key; int to; bool operator<(Q r) const { return key > r.key; } }; std::priority_queue<Q> que; dist[s] = 0; que.push(Q{0, s}); while (!que.empty()) { int v = que.top().to; que.pop(); if (vis[v]) continue; vis[v] = true; if (v == t) break; // dist[v] = shortest(s, v) + dual[s] - dual[v] // dist[v] >= 0 (all reduced cost are positive) // dist[v] <= (n-1)C for (int i = 0; i < int(g[v].size()); i++) { auto e = g[v][i]; if (vis[e.to] || !e.cap) continue; // |-dual[e.to] + dual[v]| <= (n-1)C // cost <= C - -(n-1)C + 0 = nC Cost cost = e.cost - dual[e.to] + dual[v]; if (dist[e.to] - dist[v] > cost) { dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push(Q{dist[e.to], e.to}); } } } if (!vis[t]) { return false; } for (int v = 0; v < _n; v++) { if (!vis[v]) continue; // dual[v] = dual[v] - dist[t] + dist[v] // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + // (shortest(s, v) + dual[s] - dual[v]) = - shortest(s, t) + // dual[t] + shortest(s, v) = shortest(s, v) - shortest(s, t) >= // 0 - (n-1)C dual[v] -= dist[t] - dist[v]; } return true; }; Cap flow = 0; Cost cost = 0, prev_cost = -1; std::vector<std::pair<Cap, Cost>> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (!dual_ref()) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = pv[v]) { c = std::min(c, g[pv[v]][pe[v]].cap); } for (int v = t; v != s; v = pv[v]) { auto &e = g[pv[v]][pe[v]]; e.cap -= c; g[v][e.rev].cap += c; } Cost d = -dual[s]; flow += c; cost += c * d; if (prev_cost == d) { result.pop_back(); } result.push_back({flow, cost}); prev_cost = cost; } return result; } private: int _n; struct _edge { int to, rev; Cap cap; Cost cost; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; } // namespace atcoder #include <algorithm> #include <algorithm> #include <utility> #include <vector> namespace atcoder { namespace internal { template <class E> struct csr { std::vector<int> start; std::vector<E> elist; csr(int n, const std::vector<std::pair<int, E>> &edges) : start(n + 1), elist(edges.size()) { for (auto e : edges) { start[e.first + 1]++; } for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } auto counter = start; for (auto e : edges) { elist[counter[e.first]++] = e.second; } } }; // Reference: // R. Tarjan, // Depth-First Search and Linear Graph Algorithms struct scc_graph { public: scc_graph(int n) : _n(n) {} int num_vertices() { return _n; } void add_edge(int from, int to) { edges.push_back({from, {to}}); } // @return pair of (# of scc, scc id) std::pair<int, std::vector<int>> scc_ids() { auto g = csr<edge>(_n, edges); int now_ord = 0, group_num = 0; std::vector<int> visited, low(_n), ord(_n, -1), ids(_n); visited.reserve(_n); auto dfs = [&](auto self, int v) -> void { low[v] = ord[v] = now_ord++; visited.push_back(v); for (int i = g.start[v]; i < g.start[v + 1]; i++) { auto to = g.elist[i].to; if (ord[to] == -1) { self(self, to); low[v] = std::min(low[v], low[to]); } else { low[v] = std::min(low[v], ord[to]); } } if (low[v] == ord[v]) { while (true) { int u = visited.back(); visited.pop_back(); ord[u] = _n; ids[u] = group_num; if (u == v) break; } group_num++; } }; for (int i = 0; i < _n; i++) { if (ord[i] == -1) dfs(dfs, i); } for (auto &x : ids) { x = group_num - 1 - x; } return {group_num, ids}; } std::vector<std::vector<int>> scc() { auto ids = scc_ids(); int group_num = ids.first; std::vector<int> counts(group_num); for (auto x : ids.second) counts[x]++; std::vector<std::vector<int>> groups(ids.first); for (int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for (int i = 0; i < _n; i++) { groups[ids.second[i]].push_back(i); } return groups; } private: int _n; struct edge { int to; }; std::vector<std::pair<int, edge>> edges; }; } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { struct scc_graph { public: scc_graph() : internal(0) {} scc_graph(int n) : internal(n) {} void add_edge(int from, int to) { int n = internal.num_vertices(); assert(0 <= from && from < n); assert(0 <= to && to < n); internal.add_edge(from, to); } std::vector<std::vector<int>> scc() { return internal.scc(); } private: internal::scc_graph internal; }; } // namespace atcoder #include <algorithm> #include <cassert> #include <vector> namespace atcoder { template <class S, S (*op)(S, S), S (*e)()> struct segtree { public: segtree() : segtree(0) {} segtree(int n) : segtree(std::vector<S>(n, e())) {} segtree(const std::vector<S> &v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } template <bool (*f)(S)> int max_right(int l) { return max_right(l, [](S x) { return f(x); }); } template <class F> int max_right(int l, F f) { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*f)(S)> int min_left(int r) { return min_left(r, [](S x) { return f(x); }); } template <class F> int min_left(int r, F f) { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder #include <algorithm> #include <cassert> #include <numeric> #include <string> #include <vector> namespace atcoder { namespace internal { std::vector<int> sa_naive(const std::vector<int> &s) { int n = int(s.size()); std::vector<int> sa(n); std::iota(sa.begin(), sa.end(), 0); std::sort(sa.begin(), sa.end(), [&](int l, int r) { if (l == r) return false; while (l < n && r < n) { if (s[l] != s[r]) return s[l] < s[r]; l++; r++; } return l == n; }); return sa; } std::vector<int> sa_doubling(const std::vector<int> &s) { int n = int(s.size()); std::vector<int> sa(n), rnk = s, tmp(n); std::iota(sa.begin(), sa.end(), 0); for (int k = 1; k < n; k *= 2) { auto cmp = [&](int x, int y) { if (rnk[x] != rnk[y]) return rnk[x] < rnk[y]; int rx = x + k < n ? rnk[x + k] : -1; int ry = y + k < n ? rnk[y + k] : -1; return rx < ry; }; std::sort(sa.begin(), sa.end(), cmp); tmp[sa[0]] = 0; for (int i = 1; i < n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0); } std::swap(tmp, rnk); } return sa; } // SA-IS, linear-time suffix array construction // Reference: // G. Nong, S. Zhang, and W. H. Chan, // Two Efficient Algorithms for Linear Time Suffix Array Construction template <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40> std::vector<int> sa_is(const std::vector<int> &s, int upper) { int n = int(s.size()); if (n == 0) return {}; if (n == 1) return {0}; if (n == 2) { if (s[0] < s[1]) { return {0, 1}; } else { return {1, 0}; } } if (n < THRESHOLD_NAIVE) { return sa_naive(s); } if (n < THRESHOLD_DOUBLING) { return sa_doubling(s); } std::vector<int> sa(n); std::vector<bool> ls(n); for (int i = n - 2; i >= 0; i--) { ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]); } std::vector<int> sum_l(upper + 1), sum_s(upper + 1); for (int i = 0; i < n; i++) { if (!ls[i]) { sum_s[s[i]]++; } else { sum_l[s[i] + 1]++; } } for (int i = 0; i <= upper; i++) { sum_s[i] += sum_l[i]; if (i < upper) sum_l[i + 1] += sum_s[i]; } auto induce = [&](const std::vector<int> &lms) { std::fill(sa.begin(), sa.end(), -1); std::vector<int> buf(upper + 1); std::copy(sum_s.begin(), sum_s.end(), buf.begin()); for (auto d : lms) { if (d == n) continue; sa[buf[s[d]]++] = d; } std::copy(sum_l.begin(), sum_l.end(), buf.begin()); sa[buf[s[n - 1]]++] = n - 1; for (int i = 0; i < n; i++) { int v = sa[i]; if (v >= 1 && !ls[v - 1]) { sa[buf[s[v - 1]]++] = v - 1; } } std::copy(sum_l.begin(), sum_l.end(), buf.begin()); for (int i = n - 1; i >= 0; i--) { int v = sa[i]; if (v >= 1 && ls[v - 1]) { sa[--buf[s[v - 1] + 1]] = v - 1; } } }; std::vector<int> lms_map(n + 1, -1); int m = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms_map[i] = m++; } } std::vector<int> lms; lms.reserve(m); for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms.push_back(i); } } induce(lms); if (m) { std::vector<int> sorted_lms; sorted_lms.reserve(m); for (int v : sa) { if (lms_map[v] != -1) sorted_lms.push_back(v); } std::vector<int> rec_s(m); int rec_upper = 0; rec_s[lms_map[sorted_lms[0]]] = 0; for (int i = 1; i < m; i++) { int l = sorted_lms[i - 1], r = sorted_lms[i]; int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n; int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n; bool same = true; if (end_l - l != end_r - r) { same = false; } else { while (l < end_l) { if (s[l] != s[r]) { break; } l++; r++; } if (l == n || s[l] != s[r]) same = false; } if (!same) rec_upper++; rec_s[lms_map[sorted_lms[i]]] = rec_upper; } auto rec_sa = sa_is<THRESHOLD_NAIVE, THRESHOLD_DOUBLING>(rec_s, rec_upper); for (int i = 0; i < m; i++) { sorted_lms[i] = lms[rec_sa[i]]; } induce(sorted_lms); } return sa; } } // namespace internal std::vector<int> suffix_array(const std::vector<int> &s, int upper) { assert(0 <= upper); for (int d : s) { assert(0 <= d && d <= upper); } auto sa = internal::sa_is(s, upper); return sa; } template <class T> std::vector<int> suffix_array(const std::vector<T> &s) { int n = int(s.size()); std::vector<int> idx(n); iota(idx.begin(), idx.end(), 0); sort(idx.begin(), idx.end(), [&](int l, int r) { return s[l] < s[r]; }); std::vector<int> s2(n); int now = 0; for (int i = 0; i < n; i++) { if (i && s[idx[i - 1]] != s[idx[i]]) now++; s2[idx[i]] = now; } return internal::sa_is(s2, now); } std::vector<int> suffix_array(const std::string &s) { int n = int(s.size()); std::vector<int> s2(n); for (int i = 0; i < n; i++) { s2[i] = s[i]; } return internal::sa_is(s2, 255); } // Reference: // T. Kasai, G. Lee, H. Arimura, S. Arikawa, and K. Park, // Linear-Time Longest-Common-Prefix Computation in Suffix Arrays and Its // Applications template <class T> std::vector<int> lcp_array(const std::vector<T> &s, const std::vector<int> &sa) { int n = int(s.size()); assert(n >= 1); std::vector<int> rnk(n); for (int i = 0; i < n; i++) { rnk[sa[i]] = i; } std::vector<int> lcp(n - 1); int h = 0; for (int i = 0; i < n; i++) { if (h > 0) h--; if (rnk[i] == 0) continue; int j = sa[rnk[i] - 1]; for (; j + h < n && i + h < n; h++) { if (s[j + h] != s[i + h]) break; } lcp[rnk[i] - 1] = h; } return lcp; } std::vector<int> lcp_array(const std::string &s, const std::vector<int> &sa) { int n = int(s.size()); std::vector<int> s2(n); for (int i = 0; i < n; i++) { s2[i] = s[i]; } return lcp_array(s2, sa); } // Reference: // D. Gusfield, // Algorithms on Strings, Trees, and Sequences: Computer Science and // Computational Biology template <class T> std::vector<int> z_algorithm(const std::vector<T> &s) { int n = int(s.size()); if (n == 0) return {}; std::vector<int> z(n); z[0] = 0; for (int i = 1, j = 0; i < n; i++) { int &k = z[i]; k = (j + z[j] <= i) ? 0 : std::min(j + z[j] - i, z[i - j]); while (i + k < n && s[k] == s[i + k]) k++; if (j + z[j] < i + z[i]) j = i; } z[0] = n; return z; } std::vector<int> z_algorithm(const std::string &s) { int n = int(s.size()); std::vector<int> s2(n); for (int i = 0; i < n; i++) { s2[i] = s[i]; } return z_algorithm(s2); } } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { // Reference: // B. Aspvall, M. Plass, and R. Tarjan, // A Linear-Time Algorithm for Testing the Truth of Certain Quantified Boolean // Formulas struct two_sat { public: two_sat() : _n(0), scc(0) {} two_sat(int n) : _n(n), _answer(n), scc(2 * n) {} void add_clause(int i, bool f, int j, bool g) { assert(0 <= i && i < _n); assert(0 <= j && j < _n); scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0)); scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0)); } bool satisfiable() { auto id = scc.scc_ids().second; for (int i = 0; i < _n; i++) { if (id[2 * i] == id[2 * i + 1]) return false; _answer[i] = id[2 * i] < id[2 * i + 1]; } return true; } std::vector<bool> answer() { return _answer; } private: int _n; std::vector<bool> _answer; internal::scc_graph scc; }; } // namespace atcoder using namespace std; using namespace atcoder; typedef long long ll; typedef long double ld; typedef double db; typedef string str; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<db, db> pd; const int MOD = 998244353; using mint = static_modint<MOD>; const int MX = 2e5 + 5; const ll INF = 1e18; const ld PI = acos((ld)-1); const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<db> vd; typedef vector<str> vs; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<pd> vpd; typedef vector<mint> vmi; #define mp make_pair #define f first #define s second #define len(x) (int)(x).size() #define all(x) begin(x), end(x) #define rall(x) (x).rbegin(), (x).rend() #define rsz resize #define ins insert #define ft front() #define bk back() #define pf push_front #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) #define rep(i, a) FOR(i, 0, a) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define RF0R(i, a) RFOR(i, 0, a) #define rrep(i, a) RFOR(i, 0, a) #define trav(a, x) for (auto &a : x) mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); 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; } constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31 - __builtin_clz(x); } // floor(log2(x)) ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down ll half(ll x) { return fdiv(x, 2); } template <class T, class U> T fstTrue(T lo, T hi, U f) { // note: if (lo+hi)/2 is used instead of half(lo+hi) then this will loop // infinitely when lo=hi hi++; assert(lo <= hi); // assuming f is increasing while (lo < hi) { // find first index such that f is true T mid = half(lo + hi); f(mid) ? hi = mid : lo = mid + 1; } return lo; } template <class T, class U> T lstTrue(T lo, T hi, U f) { lo--; assert(lo <= hi); // assuming f is decreasing while (lo < hi) { // find first index such that f is true T mid = half(lo + hi + 1); f(mid) ? lo = mid : hi = mid - 1; } return lo; } template <class T> void remDup(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), end(v)); } // INPUT template <class A> void re(complex<A> &c); template <class A, class B> void re(pair<A, B> &p); template <class A> void re(vector<A> &v); template <class A, size_t SZ> void re(array<A, SZ> &a); template <class T> void re(T &x) { cin >> x; } void re(db &d) { str t; re(t); d = stod(t); } void re(ld &d) { str t; re(t); d = stold(t); } template <class H, class... T> void re(H &h, T &...t) { re(h); re(t...); } template <class A> void re(complex<A> &c) { A a, b; re(a, b); c = {a, b}; } template <class A, class B> void re(pair<A, B> &p) { re(p.f, p.s); } template <class A> void re(vector<A> &x) { trav(a, x) re(a); } template <class A, size_t SZ> void re(array<A, SZ> &x) { trav(a, x) re(a); } // TO_STRING #define ts to_string str ts(char c) { return str(1, c); } str ts(const char *s) { return (str)s; } str ts(str s) { return s; } str ts(bool b) { #ifdef LOCAL return b ? "true" : "false"; #else return ts((int)b); #endif } template <class A> str ts(complex<A> c) { stringstream ss; ss << c; return ss.str(); } str ts(vector<bool> v) { str res = ""; F0R(i, len(v)) res += char('0' + v[i]); res += ""; return res; } template <size_t SZ> str ts(bitset<SZ> b) { str res = ""; F0R(i, SZ) res += char('0' + b[i]); return res; } template <class A, class B> str ts(pair<A, B> p); template <class T> str ts(T v) { // containers with begin(), end() bool fst = 1; str res = ""; for (const auto &x : v) { if (!fst) res += " "; fst = 0; res += ts(x); } return res; } template <class A, class B> str ts(pair<A, B> p) { #ifdef LOCAL return "(" + ts(p.f) + ", " + ts(p.s) + ")"; #else return ts(p.f) + " " + ts(p.s); #endif } // OUTPUT template <class A> void printl(A x) { cout << ts(x); } template <class H, class... T> void printl(const H &h, const T &...t) { printl(h); printl(t...); } void print() { printl("\n"); } // print w/ spaces template <class H, class... T> void print(const H &h, const T &...t) { printl(h); if (sizeof...(t)) printl(" "); print(t...); } // DEBUG void DBG() { cerr << " ##" << endl; } template <class H, class... T> void DBG(H h, T... t) { cerr << ts(h); if (sizeof...(t)) cerr << ", "; DBG(t...); } #ifdef LOCAL // compile with -DLOCAL, chk -> fake assert #define printe(...) \ cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: ", \ DBG(__VA_ARGS__) #define chk(...) \ if (!(__VA_ARGS__)) \ cerr << "Line(" << __LINE__ << ") -> function(" << __FUNCTION__ \ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" \ << "\n", \ exit(0); #else #define printe(...) 0 #define chk(...) 0 #endif // FILE I/O void setIn(str s) { freopen(s.c_str(), "r", stdin); } void setOut(str s) { freopen(s.c_str(), "w", stdout); } void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); } void setIO(str s = "") { unsyncIO(); // cin.exceptions(cin.failbit); // throws exception when do smth illegal // ex. try to read letter into int if (len(s)) { setIn(s + ".in"), setOut(s + ".out"); } // for USACO } 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 <typename T> vector<size_t> argsort(vector<T> &v) { vector<size_t> idx(v.size()); iota(idx.begin(), idx.end(), 0); sort(idx.begin(), idx.end(), [&v](size_t i1, size_t i2) { return v[i1] < v[i2]; }); return idx; } template <typename T> int bisect_left(vector<T> &v, T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <typename T> int bisect_right(vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <typename T> bool erase1(multiset<T> &ms, T x) { // erase only 1 element for multiset auto ind = ms.find(x); if (ind == ms.end()) return false; ms.erase(ind); return true; } 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>>; signed main() { setIO(); ll N, S; re(N, S); vl A(N); re(A); mint ans = 0; vmi dp(S + 1); rep(l, N) { int a = A[l]; // if (A[l]>S) continue; for (int j = S; j >= a + 1; j--) { dp[j] += dp[j - a]; } dp[a] += (l + 1); // dp[a]+=l+1; ans += dp[S]; } print(ans.val()); }
#include <bits/stdc++.h> #include <algorithm> #include <array> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder #include <utility> namespace atcoder { namespace internal { // @param m `1 <= m` // @return x mod m constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } // Fast moduler by barrett reduction // Reference: https://en.wikipedia.org/wiki/Barrett_reduction // NOTE: reconsider after Ice Lake struct barrett { unsigned int _m; unsigned long long im; // @param m `1 <= m` barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} // @return m unsigned int umod() const { return _m; } // @param a `0 <= a < m` // @param b `0 <= b < m` // @return `a * b % m` unsigned int mul(unsigned int a, unsigned int b) const { // [1] m = 1 // a = b = im = 0, so okay // [2] m >= 2 // im = ceil(2^64 / m) // -> im * m = 2^64 + r (0 <= r < m) // let z = a*b = c*m + d (0 <= c, d < m) // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < // 2^64 * 2 // ((ab * im) >> 64) == c or c + 1 unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; // @param n `0 <= n` // @param m `1 <= m` // @return `(x ** n) % m` constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } // Reference: // M. Forisek and J. Jancina, // Fast Primality Testing for Integers That Fit into a Machine Word // @param n `0 <= n` constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; for (long long a : {2, 7, 61}) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template <int n> constexpr bool is_prime = is_prime_constexpr(n); // @param b `1 <= b` // @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; // Contracts: // [1] s - m0 * a = 0 (mod b) // [2] t - m1 * a = 0 (mod b) // [3] s * |m1| + t * |m0| <= b long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b // [3]: // (s - t * u) * |m1| + t * |m0 - m1 * u| // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u) // = s * |m1| + t * |m0| <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } // by [3]: |m0| <= b/g // by g != b: |m0| < b/g if (m0 < 0) m0 += b / s; return {s, m0}; } // Compile time primitive root // @param m must be prime // @return primitive root (and minimum in now) constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template <int m> constexpr int primitive_root = primitive_root_constexpr(m); } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template <class T> using is_modint = std::is_base_of<modint_base, T>; template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>; } // namespace internal template <int m, std::enable_if_t<(1 <= m)> * = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T> * = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T> * = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } static_modint(bool v) { _v = ((unsigned int)(v) % umod()); } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator-=(const mint &rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint &operator*=(const mint &rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime<m>; }; template <int id> struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T> * = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T> * = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator-=(const mint &rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator*=(const mint &rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template <int id> internal::barrett dynamic_modint<id>::bt = 998244353; using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template <class T> using is_static_modint = std::is_base_of<internal::static_modint_base, T>; template <class T> using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>; template <class> struct is_dynamic_modint : public std::false_type {}; template <int id> struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {}; template <class T> using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>; } // namespace internal } // namespace atcoder #include <cassert> #include <type_traits> #include <vector> namespace atcoder { namespace internal { template <class mint, internal::is_static_modint_t<mint> * = nullptr> void butterfly(std::vector<mint> &a) { static constexpr int g = internal::primitive_root<mint::mod()>; int n = int(a.size()); int h = internal::ceil_pow2(n); static bool first = true; static mint sum_e[30]; // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i] if (first) { first = false; mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1 int cnt2 = bsf(mint::mod() - 1); mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv(); for (int i = cnt2; i >= 2; i--) { // e^(2^i) == 1 es[i - 2] = e; ies[i - 2] = ie; e *= e; ie *= ie; } mint now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_e[i] = es[i] * now; now *= ies[i]; } } for (int ph = 1; ph <= h; ph++) { int w = 1 << (ph - 1), p = 1 << (h - ph); mint now = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p] * now; a[i + offset] = l + r; a[i + offset + p] = l - r; } now *= sum_e[bsf(~(unsigned int)(s))]; } } } template <class mint, internal::is_static_modint_t<mint> * = nullptr> void butterfly_inv(std::vector<mint> &a) { static constexpr int g = internal::primitive_root<mint::mod()>; int n = int(a.size()); int h = internal::ceil_pow2(n); static bool first = true; static mint sum_ie[30]; // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i] if (first) { first = false; mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1 int cnt2 = bsf(mint::mod() - 1); mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv(); for (int i = cnt2; i >= 2; i--) { // e^(2^i) == 1 es[i - 2] = e; ies[i - 2] = ie; e *= e; ie *= ie; } mint now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_ie[i] = ies[i] * now; now *= es[i]; } } for (int ph = h; ph >= 1; ph--) { int w = 1 << (ph - 1), p = 1 << (h - ph); mint inow = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * inow.val(); } inow *= sum_ie[bsf(~(unsigned int)(s))]; } } } } // namespace internal template <class mint, internal::is_static_modint_t<mint> * = nullptr> std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) { if (n < m) { std::swap(n, m); std::swap(a, b); } std::vector<mint> ans(n + m - 1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; } } return ans; } int z = 1 << internal::ceil_pow2(n + m - 1); a.resize(z); internal::butterfly(a); b.resize(z); internal::butterfly(b); for (int i = 0; i < z; i++) { a[i] *= b[i]; } internal::butterfly_inv(a); a.resize(n + m - 1); mint iz = mint(z).inv(); for (int i = 0; i < n + m - 1; i++) a[i] *= iz; return a; } template <unsigned int mod = 998244353, class T, std::enable_if_t<internal::is_integral<T>::value> * = nullptr> std::vector<T> convolution(const std::vector<T> &a, const std::vector<T> &b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; using mint = static_modint<mod>; std::vector<mint> a2(n), b2(m); for (int i = 0; i < n; i++) { a2[i] = mint(a[i]); } for (int i = 0; i < m; i++) { b2[i] = mint(b[i]); } auto c2 = convolution(move(a2), move(b2)); std::vector<T> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { c[i] = c2[i].val(); } return c; } std::vector<long long> convolution_ll(const std::vector<long long> &a, const std::vector<long long> &b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; static constexpr unsigned long long MOD1 = 754974721; // 2^24 static constexpr unsigned long long MOD2 = 167772161; // 2^25 static constexpr unsigned long long MOD3 = 469762049; // 2^26 static constexpr unsigned long long M2M3 = MOD2 * MOD3; static constexpr unsigned long long M1M3 = MOD1 * MOD3; static constexpr unsigned long long M1M2 = MOD1 * MOD2; static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3; static constexpr unsigned long long i1 = internal::inv_gcd(MOD2 * MOD3, MOD1).second; static constexpr unsigned long long i2 = internal::inv_gcd(MOD1 * MOD3, MOD2).second; static constexpr unsigned long long i3 = internal::inv_gcd(MOD1 * MOD2, MOD3).second; auto c1 = convolution<MOD1>(a, b); auto c2 = convolution<MOD2>(a, b); auto c3 = convolution<MOD3>(a, b); std::vector<long long> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { unsigned long long x = 0; x += (c1[i] * i1) % MOD1 * M2M3; x += (c2[i] * i2) % MOD2 * M1M3; x += (c3[i] * i3) % MOD3 * M1M2; // B = 2^63, -B <= x, r(real value) < B // (x, x - M, x - 2M, or x - 3M) = r (mod 2B) // r = c1[i] (mod MOD1) // focus on MOD1 // r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B) // r = x, // x - M' + (0 or 2B), // x - 2M' + (0, 2B or 4B), // x - 3M' + (0, 2B, 4B or 6B) (without mod!) // (r - x) = 0, (0) // - M' + (0 or 2B), (1) // -2M' + (0 or 2B or 4B), (2) // -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1) // we checked that // ((1) mod MOD1) mod 5 = 2 // ((2) mod MOD1) mod 5 = 3 // ((3) mod MOD1) mod 5 = 4 long long diff = c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1)); if (diff < 0) diff += MOD1; static constexpr unsigned long long offset[5] = {0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3}; x -= offset[diff % 5]; c[i] = x; } return c; } } // namespace atcoder #include <algorithm> #include <cassert> #include <vector> namespace atcoder { // Implement (union by size) + (path compression) // Reference: // Zvi Galil and Giuseppe F. Italiano, // Data structures and algorithms for disjoint set union problems struct dsu { public: dsu() : _n(0) {} dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int> &v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; }; } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { // Reference: https://en.wikipedia.org/wiki/Fenwick_tree template <class T> struct fenwick_tree { using U = internal::to_unsigned_t<T>; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector<U> data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; } // namespace atcoder #include <algorithm> #include <cassert> #include <iostream> #include <vector> namespace atcoder { template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S> &v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder #include <algorithm> #include <cassert> #include <tuple> #include <vector> namespace atcoder { long long pow_mod(long long x, long long n, int m) { assert(0 <= n && 1 <= m); if (m == 1) return 0; internal::barrett bt((unsigned int)(m)); unsigned int r = 1, y = (unsigned int)(internal::safe_mod(x, m)); while (n) { if (n & 1) r = bt.mul(r, y); y = bt.mul(y, y); n >>= 1; } return r; } long long inv_mod(long long x, long long m) { assert(1 <= m); auto z = internal::inv_gcd(x, m); assert(z.first == 1); return z.second; } // (rem, mod) std::pair<long long, long long> crt(const std::vector<long long> &r, const std::vector<long long> &m) { assert(r.size() == m.size()); int n = int(r.size()); // Contracts: 0 <= r0 < m0 long long r0 = 0, m0 = 1; for (int i = 0; i < n; i++) { assert(1 <= m[i]); long long r1 = internal::safe_mod(r[i], m[i]), m1 = m[i]; if (m0 < m1) { std::swap(r0, r1); std::swap(m0, m1); } if (m0 % m1 == 0) { if (r0 % m1 != r1) return {0, 0}; continue; } // assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1) // (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1)); // r2 % m0 = r0 // r2 % m1 = r1 // -> (r0 + x*m0) % m1 = r1 // -> x*u0*g % (u1*g) = (r1 - r0) (u0*g = m0, u1*g = m1) // -> x = (r1 - r0) / g * inv(u0) (mod u1) // im = inv(u0) (mod u1) (0 <= im < u1) long long g, im; std::tie(g, im) = internal::inv_gcd(m0, m1); long long u1 = (m1 / g); // |r1 - r0| < (m0 + m1) <= lcm(m0, m1) if ((r1 - r0) % g) return {0, 0}; // u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1) long long x = (r1 - r0) / g % u1 * im % u1; // |r0| + |m0 * x| // < m0 + m0 * (u1 - 1) // = m0 + m0 * m1 / g - m0 // = lcm(m0, m1) r0 += x * m0; m0 *= u1; // -> lcm(m0, m1) if (r0 < 0) r0 += m0; } return {r0, m0}; } long long floor_sum(long long n, long long m, long long a, long long b) { long long ans = 0; if (a >= m) { ans += (n - 1) * n * (a / m) / 2; a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } long long y_max = (a * n + b) / m, x_max = (y_max * m - b); if (y_max == 0) return ans; ans += (n - (x_max + a - 1) / a) * y_max; ans += floor_sum(y_max, a, m, (a - x_max % a) % a); return ans; } } // namespace atcoder #include <algorithm> #include <vector> namespace atcoder { namespace internal { template <class T> struct simple_queue { std::vector<T> payload; int pos = 0; void reserve(int n) { payload.reserve(n); } int size() const { return int(payload.size()) - pos; } bool empty() const { return pos == int(payload.size()); } void push(const T &t) { payload.push_back(t); } T &front() { return payload[pos]; } void clear() { payload.clear(); pos = 0; } void pop() { pos++; } }; } // namespace internal } // namespace atcoder #include <cassert> #include <limits> #include <queue> #include <vector> namespace atcoder { template <class Cap> struct mf_graph { public: mf_graph() : _n(0) {} mf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); g[from].push_back(_edge{to, int(g[to].size()), cap}); g[to].push_back(_edge{from, int(g[from].size()) - 1, 0}); return m; } struct edge { int from, to; Cap cap, flow; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap}; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result; for (int i = 0; i < m; i++) { result.push_back(get_edge(i)); } return result; } void change_edge(int i, Cap new_cap, Cap new_flow) { int m = int(pos.size()); assert(0 <= i && i < m); assert(0 <= new_flow && new_flow <= new_cap); auto &_e = g[pos[i].first][pos[i].second]; auto &_re = g[_e.to][_e.rev]; _e.cap = new_cap - new_flow; _re.cap = new_flow; } Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } Cap flow(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); std::vector<int> level(_n), iter(_n); internal::simple_queue<int> que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.push(e.to); } } }; auto dfs = [&](auto self, int v, Cap up) { if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int &i = iter[v]; i < int(g[v].size()); i++) { _edge &e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) break; } return res; }; Cap flow = 0; while (flow < flow_limit) { bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); while (flow < flow_limit) { Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } } return flow; } std::vector<bool> min_cut(int s) { std::vector<bool> visited(_n); internal::simple_queue<int> que; que.push(s); while (!que.empty()) { int p = que.front(); que.pop(); visited[p] = true; for (auto e : g[p]) { if (e.cap && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } } return visited; } private: int _n; struct _edge { int to, rev; Cap cap; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; } // namespace atcoder #include <algorithm> #include <cassert> #include <limits> #include <queue> #include <vector> namespace atcoder { template <class Cap, class Cost> struct mcf_graph { public: mcf_graph() {} mcf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); g[from].push_back(_edge{to, int(g[to].size()), cap, cost}); g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{ pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, }; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result(m); for (int i = 0; i < m; i++) { result[i] = get_edge(i); } return result; } std::pair<Cap, Cost> flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) { return slope(s, t, flow_limit).back(); } std::vector<std::pair<Cap, Cost>> slope(int s, int t) { return slope(s, t, std::numeric_limits<Cap>::max()); } std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); // variants (C = maxcost): // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0 // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge std::vector<Cost> dual(_n, 0), dist(_n); std::vector<int> pv(_n), pe(_n); std::vector<bool> vis(_n); auto dual_ref = [&]() { std::fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max()); std::fill(pv.begin(), pv.end(), -1); std::fill(pe.begin(), pe.end(), -1); std::fill(vis.begin(), vis.end(), false); struct Q { Cost key; int to; bool operator<(Q r) const { return key > r.key; } }; std::priority_queue<Q> que; dist[s] = 0; que.push(Q{0, s}); while (!que.empty()) { int v = que.top().to; que.pop(); if (vis[v]) continue; vis[v] = true; if (v == t) break; // dist[v] = shortest(s, v) + dual[s] - dual[v] // dist[v] >= 0 (all reduced cost are positive) // dist[v] <= (n-1)C for (int i = 0; i < int(g[v].size()); i++) { auto e = g[v][i]; if (vis[e.to] || !e.cap) continue; // |-dual[e.to] + dual[v]| <= (n-1)C // cost <= C - -(n-1)C + 0 = nC Cost cost = e.cost - dual[e.to] + dual[v]; if (dist[e.to] - dist[v] > cost) { dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push(Q{dist[e.to], e.to}); } } } if (!vis[t]) { return false; } for (int v = 0; v < _n; v++) { if (!vis[v]) continue; // dual[v] = dual[v] - dist[t] + dist[v] // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + // (shortest(s, v) + dual[s] - dual[v]) = - shortest(s, t) + // dual[t] + shortest(s, v) = shortest(s, v) - shortest(s, t) >= // 0 - (n-1)C dual[v] -= dist[t] - dist[v]; } return true; }; Cap flow = 0; Cost cost = 0, prev_cost = -1; std::vector<std::pair<Cap, Cost>> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (!dual_ref()) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = pv[v]) { c = std::min(c, g[pv[v]][pe[v]].cap); } for (int v = t; v != s; v = pv[v]) { auto &e = g[pv[v]][pe[v]]; e.cap -= c; g[v][e.rev].cap += c; } Cost d = -dual[s]; flow += c; cost += c * d; if (prev_cost == d) { result.pop_back(); } result.push_back({flow, cost}); prev_cost = cost; } return result; } private: int _n; struct _edge { int to, rev; Cap cap; Cost cost; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; } // namespace atcoder #include <algorithm> #include <algorithm> #include <utility> #include <vector> namespace atcoder { namespace internal { template <class E> struct csr { std::vector<int> start; std::vector<E> elist; csr(int n, const std::vector<std::pair<int, E>> &edges) : start(n + 1), elist(edges.size()) { for (auto e : edges) { start[e.first + 1]++; } for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } auto counter = start; for (auto e : edges) { elist[counter[e.first]++] = e.second; } } }; // Reference: // R. Tarjan, // Depth-First Search and Linear Graph Algorithms struct scc_graph { public: scc_graph(int n) : _n(n) {} int num_vertices() { return _n; } void add_edge(int from, int to) { edges.push_back({from, {to}}); } // @return pair of (# of scc, scc id) std::pair<int, std::vector<int>> scc_ids() { auto g = csr<edge>(_n, edges); int now_ord = 0, group_num = 0; std::vector<int> visited, low(_n), ord(_n, -1), ids(_n); visited.reserve(_n); auto dfs = [&](auto self, int v) -> void { low[v] = ord[v] = now_ord++; visited.push_back(v); for (int i = g.start[v]; i < g.start[v + 1]; i++) { auto to = g.elist[i].to; if (ord[to] == -1) { self(self, to); low[v] = std::min(low[v], low[to]); } else { low[v] = std::min(low[v], ord[to]); } } if (low[v] == ord[v]) { while (true) { int u = visited.back(); visited.pop_back(); ord[u] = _n; ids[u] = group_num; if (u == v) break; } group_num++; } }; for (int i = 0; i < _n; i++) { if (ord[i] == -1) dfs(dfs, i); } for (auto &x : ids) { x = group_num - 1 - x; } return {group_num, ids}; } std::vector<std::vector<int>> scc() { auto ids = scc_ids(); int group_num = ids.first; std::vector<int> counts(group_num); for (auto x : ids.second) counts[x]++; std::vector<std::vector<int>> groups(ids.first); for (int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for (int i = 0; i < _n; i++) { groups[ids.second[i]].push_back(i); } return groups; } private: int _n; struct edge { int to; }; std::vector<std::pair<int, edge>> edges; }; } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { struct scc_graph { public: scc_graph() : internal(0) {} scc_graph(int n) : internal(n) {} void add_edge(int from, int to) { int n = internal.num_vertices(); assert(0 <= from && from < n); assert(0 <= to && to < n); internal.add_edge(from, to); } std::vector<std::vector<int>> scc() { return internal.scc(); } private: internal::scc_graph internal; }; } // namespace atcoder #include <algorithm> #include <cassert> #include <vector> namespace atcoder { template <class S, S (*op)(S, S), S (*e)()> struct segtree { public: segtree() : segtree(0) {} segtree(int n) : segtree(std::vector<S>(n, e())) {} segtree(const std::vector<S> &v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } template <bool (*f)(S)> int max_right(int l) { return max_right(l, [](S x) { return f(x); }); } template <class F> int max_right(int l, F f) { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*f)(S)> int min_left(int r) { return min_left(r, [](S x) { return f(x); }); } template <class F> int min_left(int r, F f) { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder #include <algorithm> #include <cassert> #include <numeric> #include <string> #include <vector> namespace atcoder { namespace internal { std::vector<int> sa_naive(const std::vector<int> &s) { int n = int(s.size()); std::vector<int> sa(n); std::iota(sa.begin(), sa.end(), 0); std::sort(sa.begin(), sa.end(), [&](int l, int r) { if (l == r) return false; while (l < n && r < n) { if (s[l] != s[r]) return s[l] < s[r]; l++; r++; } return l == n; }); return sa; } std::vector<int> sa_doubling(const std::vector<int> &s) { int n = int(s.size()); std::vector<int> sa(n), rnk = s, tmp(n); std::iota(sa.begin(), sa.end(), 0); for (int k = 1; k < n; k *= 2) { auto cmp = [&](int x, int y) { if (rnk[x] != rnk[y]) return rnk[x] < rnk[y]; int rx = x + k < n ? rnk[x + k] : -1; int ry = y + k < n ? rnk[y + k] : -1; return rx < ry; }; std::sort(sa.begin(), sa.end(), cmp); tmp[sa[0]] = 0; for (int i = 1; i < n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0); } std::swap(tmp, rnk); } return sa; } // SA-IS, linear-time suffix array construction // Reference: // G. Nong, S. Zhang, and W. H. Chan, // Two Efficient Algorithms for Linear Time Suffix Array Construction template <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40> std::vector<int> sa_is(const std::vector<int> &s, int upper) { int n = int(s.size()); if (n == 0) return {}; if (n == 1) return {0}; if (n == 2) { if (s[0] < s[1]) { return {0, 1}; } else { return {1, 0}; } } if (n < THRESHOLD_NAIVE) { return sa_naive(s); } if (n < THRESHOLD_DOUBLING) { return sa_doubling(s); } std::vector<int> sa(n); std::vector<bool> ls(n); for (int i = n - 2; i >= 0; i--) { ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]); } std::vector<int> sum_l(upper + 1), sum_s(upper + 1); for (int i = 0; i < n; i++) { if (!ls[i]) { sum_s[s[i]]++; } else { sum_l[s[i] + 1]++; } } for (int i = 0; i <= upper; i++) { sum_s[i] += sum_l[i]; if (i < upper) sum_l[i + 1] += sum_s[i]; } auto induce = [&](const std::vector<int> &lms) { std::fill(sa.begin(), sa.end(), -1); std::vector<int> buf(upper + 1); std::copy(sum_s.begin(), sum_s.end(), buf.begin()); for (auto d : lms) { if (d == n) continue; sa[buf[s[d]]++] = d; } std::copy(sum_l.begin(), sum_l.end(), buf.begin()); sa[buf[s[n - 1]]++] = n - 1; for (int i = 0; i < n; i++) { int v = sa[i]; if (v >= 1 && !ls[v - 1]) { sa[buf[s[v - 1]]++] = v - 1; } } std::copy(sum_l.begin(), sum_l.end(), buf.begin()); for (int i = n - 1; i >= 0; i--) { int v = sa[i]; if (v >= 1 && ls[v - 1]) { sa[--buf[s[v - 1] + 1]] = v - 1; } } }; std::vector<int> lms_map(n + 1, -1); int m = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms_map[i] = m++; } } std::vector<int> lms; lms.reserve(m); for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms.push_back(i); } } induce(lms); if (m) { std::vector<int> sorted_lms; sorted_lms.reserve(m); for (int v : sa) { if (lms_map[v] != -1) sorted_lms.push_back(v); } std::vector<int> rec_s(m); int rec_upper = 0; rec_s[lms_map[sorted_lms[0]]] = 0; for (int i = 1; i < m; i++) { int l = sorted_lms[i - 1], r = sorted_lms[i]; int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n; int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n; bool same = true; if (end_l - l != end_r - r) { same = false; } else { while (l < end_l) { if (s[l] != s[r]) { break; } l++; r++; } if (l == n || s[l] != s[r]) same = false; } if (!same) rec_upper++; rec_s[lms_map[sorted_lms[i]]] = rec_upper; } auto rec_sa = sa_is<THRESHOLD_NAIVE, THRESHOLD_DOUBLING>(rec_s, rec_upper); for (int i = 0; i < m; i++) { sorted_lms[i] = lms[rec_sa[i]]; } induce(sorted_lms); } return sa; } } // namespace internal std::vector<int> suffix_array(const std::vector<int> &s, int upper) { assert(0 <= upper); for (int d : s) { assert(0 <= d && d <= upper); } auto sa = internal::sa_is(s, upper); return sa; } template <class T> std::vector<int> suffix_array(const std::vector<T> &s) { int n = int(s.size()); std::vector<int> idx(n); iota(idx.begin(), idx.end(), 0); sort(idx.begin(), idx.end(), [&](int l, int r) { return s[l] < s[r]; }); std::vector<int> s2(n); int now = 0; for (int i = 0; i < n; i++) { if (i && s[idx[i - 1]] != s[idx[i]]) now++; s2[idx[i]] = now; } return internal::sa_is(s2, now); } std::vector<int> suffix_array(const std::string &s) { int n = int(s.size()); std::vector<int> s2(n); for (int i = 0; i < n; i++) { s2[i] = s[i]; } return internal::sa_is(s2, 255); } // Reference: // T. Kasai, G. Lee, H. Arimura, S. Arikawa, and K. Park, // Linear-Time Longest-Common-Prefix Computation in Suffix Arrays and Its // Applications template <class T> std::vector<int> lcp_array(const std::vector<T> &s, const std::vector<int> &sa) { int n = int(s.size()); assert(n >= 1); std::vector<int> rnk(n); for (int i = 0; i < n; i++) { rnk[sa[i]] = i; } std::vector<int> lcp(n - 1); int h = 0; for (int i = 0; i < n; i++) { if (h > 0) h--; if (rnk[i] == 0) continue; int j = sa[rnk[i] - 1]; for (; j + h < n && i + h < n; h++) { if (s[j + h] != s[i + h]) break; } lcp[rnk[i] - 1] = h; } return lcp; } std::vector<int> lcp_array(const std::string &s, const std::vector<int> &sa) { int n = int(s.size()); std::vector<int> s2(n); for (int i = 0; i < n; i++) { s2[i] = s[i]; } return lcp_array(s2, sa); } // Reference: // D. Gusfield, // Algorithms on Strings, Trees, and Sequences: Computer Science and // Computational Biology template <class T> std::vector<int> z_algorithm(const std::vector<T> &s) { int n = int(s.size()); if (n == 0) return {}; std::vector<int> z(n); z[0] = 0; for (int i = 1, j = 0; i < n; i++) { int &k = z[i]; k = (j + z[j] <= i) ? 0 : std::min(j + z[j] - i, z[i - j]); while (i + k < n && s[k] == s[i + k]) k++; if (j + z[j] < i + z[i]) j = i; } z[0] = n; return z; } std::vector<int> z_algorithm(const std::string &s) { int n = int(s.size()); std::vector<int> s2(n); for (int i = 0; i < n; i++) { s2[i] = s[i]; } return z_algorithm(s2); } } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { // Reference: // B. Aspvall, M. Plass, and R. Tarjan, // A Linear-Time Algorithm for Testing the Truth of Certain Quantified Boolean // Formulas struct two_sat { public: two_sat() : _n(0), scc(0) {} two_sat(int n) : _n(n), _answer(n), scc(2 * n) {} void add_clause(int i, bool f, int j, bool g) { assert(0 <= i && i < _n); assert(0 <= j && j < _n); scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0)); scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0)); } bool satisfiable() { auto id = scc.scc_ids().second; for (int i = 0; i < _n; i++) { if (id[2 * i] == id[2 * i + 1]) return false; _answer[i] = id[2 * i] < id[2 * i + 1]; } return true; } std::vector<bool> answer() { return _answer; } private: int _n; std::vector<bool> _answer; internal::scc_graph scc; }; } // namespace atcoder using namespace std; using namespace atcoder; typedef long long ll; typedef long double ld; typedef double db; typedef string str; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<db, db> pd; const int MOD = 998244353; using mint = static_modint<MOD>; const int MX = 2e5 + 5; const ll INF = 1e18; const ld PI = acos((ld)-1); const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<db> vd; typedef vector<str> vs; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<pd> vpd; typedef vector<mint> vmi; #define mp make_pair #define f first #define s second #define len(x) (int)(x).size() #define all(x) begin(x), end(x) #define rall(x) (x).rbegin(), (x).rend() #define rsz resize #define ins insert #define ft front() #define bk back() #define pf push_front #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) #define rep(i, a) FOR(i, 0, a) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define RF0R(i, a) RFOR(i, 0, a) #define rrep(i, a) RFOR(i, 0, a) #define trav(a, x) for (auto &a : x) mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); 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; } constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31 - __builtin_clz(x); } // floor(log2(x)) ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down ll half(ll x) { return fdiv(x, 2); } template <class T, class U> T fstTrue(T lo, T hi, U f) { // note: if (lo+hi)/2 is used instead of half(lo+hi) then this will loop // infinitely when lo=hi hi++; assert(lo <= hi); // assuming f is increasing while (lo < hi) { // find first index such that f is true T mid = half(lo + hi); f(mid) ? hi = mid : lo = mid + 1; } return lo; } template <class T, class U> T lstTrue(T lo, T hi, U f) { lo--; assert(lo <= hi); // assuming f is decreasing while (lo < hi) { // find first index such that f is true T mid = half(lo + hi + 1); f(mid) ? lo = mid : hi = mid - 1; } return lo; } template <class T> void remDup(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), end(v)); } // INPUT template <class A> void re(complex<A> &c); template <class A, class B> void re(pair<A, B> &p); template <class A> void re(vector<A> &v); template <class A, size_t SZ> void re(array<A, SZ> &a); template <class T> void re(T &x) { cin >> x; } void re(db &d) { str t; re(t); d = stod(t); } void re(ld &d) { str t; re(t); d = stold(t); } template <class H, class... T> void re(H &h, T &...t) { re(h); re(t...); } template <class A> void re(complex<A> &c) { A a, b; re(a, b); c = {a, b}; } template <class A, class B> void re(pair<A, B> &p) { re(p.f, p.s); } template <class A> void re(vector<A> &x) { trav(a, x) re(a); } template <class A, size_t SZ> void re(array<A, SZ> &x) { trav(a, x) re(a); } // TO_STRING #define ts to_string str ts(char c) { return str(1, c); } str ts(const char *s) { return (str)s; } str ts(str s) { return s; } str ts(bool b) { #ifdef LOCAL return b ? "true" : "false"; #else return ts((int)b); #endif } template <class A> str ts(complex<A> c) { stringstream ss; ss << c; return ss.str(); } str ts(vector<bool> v) { str res = ""; F0R(i, len(v)) res += char('0' + v[i]); res += ""; return res; } template <size_t SZ> str ts(bitset<SZ> b) { str res = ""; F0R(i, SZ) res += char('0' + b[i]); return res; } template <class A, class B> str ts(pair<A, B> p); template <class T> str ts(T v) { // containers with begin(), end() bool fst = 1; str res = ""; for (const auto &x : v) { if (!fst) res += " "; fst = 0; res += ts(x); } return res; } template <class A, class B> str ts(pair<A, B> p) { #ifdef LOCAL return "(" + ts(p.f) + ", " + ts(p.s) + ")"; #else return ts(p.f) + " " + ts(p.s); #endif } // OUTPUT template <class A> void printl(A x) { cout << ts(x); } template <class H, class... T> void printl(const H &h, const T &...t) { printl(h); printl(t...); } void print() { printl("\n"); } // print w/ spaces template <class H, class... T> void print(const H &h, const T &...t) { printl(h); if (sizeof...(t)) printl(" "); print(t...); } // DEBUG void DBG() { cerr << " ##" << endl; } template <class H, class... T> void DBG(H h, T... t) { cerr << ts(h); if (sizeof...(t)) cerr << ", "; DBG(t...); } #ifdef LOCAL // compile with -DLOCAL, chk -> fake assert #define printe(...) \ cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: ", \ DBG(__VA_ARGS__) #define chk(...) \ if (!(__VA_ARGS__)) \ cerr << "Line(" << __LINE__ << ") -> function(" << __FUNCTION__ \ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" \ << "\n", \ exit(0); #else #define printe(...) 0 #define chk(...) 0 #endif // FILE I/O void setIn(str s) { freopen(s.c_str(), "r", stdin); } void setOut(str s) { freopen(s.c_str(), "w", stdout); } void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); } void setIO(str s = "") { unsyncIO(); // cin.exceptions(cin.failbit); // throws exception when do smth illegal // ex. try to read letter into int if (len(s)) { setIn(s + ".in"), setOut(s + ".out"); } // for USACO } 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 <typename T> vector<size_t> argsort(vector<T> &v) { vector<size_t> idx(v.size()); iota(idx.begin(), idx.end(), 0); sort(idx.begin(), idx.end(), [&v](size_t i1, size_t i2) { return v[i1] < v[i2]; }); return idx; } template <typename T> int bisect_left(vector<T> &v, T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <typename T> int bisect_right(vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <typename T> bool erase1(multiset<T> &ms, T x) { // erase only 1 element for multiset auto ind = ms.find(x); if (ind == ms.end()) return false; ms.erase(ind); return true; } 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>>; signed main() { setIO(); ll N, S; re(N, S); vl A(N); re(A); mint ans = 0; vmi dp(S + 1); rep(l, N) { int a = A[l]; if (A[l] > S) { ans += dp[S]; continue; } for (int j = S; j >= a + 1; j--) { dp[j] += dp[j - a]; } dp[a] += (l + 1); // dp[a]+=l+1; ans += dp[S]; } print(ans.val()); }
replace
2,429
2,430
2,429
2,433
0
p02734
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; typedef long long ll; const ll MOD = 998244353; int main() { int n, s; cin >> n >> s; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; ll ret = 0; vector<ll> dp(s + 1); for (int i = 0; i < n; i++) { dp[a[i]] += i + 1; for (int j = s; j >= a[i]; j--) { dp[j] += dp[j - a[i]]; if (j - a[i] == a[i]) dp[j] -= i + 1; dp[j] += MOD, dp[j] %= MOD; } ret += dp[s] % MOD, ret %= MOD; } cout << ret << endl; return 0; }
#include <iostream> #include <vector> using namespace std; typedef long long ll; const ll MOD = 998244353; int main() { int n, s; cin >> n >> s; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; ll ret = 0; vector<ll> dp(s + 1); for (int i = 0; i < n; i++) { if (a[i] <= s) { dp[a[i]] += i + 1; for (int j = s; j >= a[i]; j--) { dp[j] += dp[j - a[i]]; if (j - a[i] == a[i]) dp[j] -= i + 1; dp[j] += MOD, dp[j] %= MOD; } } ret += dp[s] % MOD, ret %= MOD; } cout << ret << endl; return 0; }
replace
18
24
18
26
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define all(n) n.begin(), n.end() #define eb emplace_back #define endl "\n" const ll maxn = 2e3 + 50; const ll maxx = 1e13; const ll mod1 = 1000000007; const ll mod = 998244353; ll dp[maxn][maxn][4]; ll a[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("test.txt","r",stdin); // freopen("FRAC.INP", "r", stdin); // freopen("WALKING.OUT", "w", stdout); ll n, s; cin >> n >> s; for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) { for (int k = 0; k <= 2; k++) { dp[i][j][k] = 0; } } } dp[0][0][0] = 1; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { for (int j = 0; j <= s; j++) { dp[i][j][0] = (dp[i][j][0] + dp[i - 1][j][0]) % mod; dp[i][j][1] = (dp[i][j][1] + dp[i - 1][j][0] + dp[i - 1][j][1]) % mod; dp[i][j][2] = (dp[i][j][2] + dp[i - 1][j][0] + dp[i - 1][j][1] + dp[i - 1][j][2]) % mod; if (j >= a[i]) { dp[i][j][1] = (dp[i][j][1] + dp[i - 1][j - a[i]][1] + dp[i - 1][j - a[i]][0]) % mod; dp[i][j][2] = (dp[i][j][2] + dp[i - 1][j - a[i]][1] + dp[i - 1][j - a[i]][0]) % mod; } } } cout << dp[n][s][2]; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define all(n) n.begin(), n.end() #define eb emplace_back #define endl "\n" const ll maxn = 3e3 + 50; const ll maxx = 1e13; const ll mod1 = 1000000007; const ll mod = 998244353; ll dp[maxn][maxn][4]; ll a[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("test.txt","r",stdin); // freopen("FRAC.INP", "r", stdin); // freopen("WALKING.OUT", "w", stdout); ll n, s; cin >> n >> s; for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) { for (int k = 0; k <= 2; k++) { dp[i][j][k] = 0; } } } dp[0][0][0] = 1; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { for (int j = 0; j <= s; j++) { dp[i][j][0] = (dp[i][j][0] + dp[i - 1][j][0]) % mod; dp[i][j][1] = (dp[i][j][1] + dp[i - 1][j][0] + dp[i - 1][j][1]) % mod; dp[i][j][2] = (dp[i][j][2] + dp[i - 1][j][0] + dp[i - 1][j][1] + dp[i - 1][j][2]) % mod; if (j >= a[i]) { dp[i][j][1] = (dp[i][j][1] + dp[i - 1][j - a[i]][1] + dp[i - 1][j - a[i]][0]) % mod; dp[i][j][2] = (dp[i][j][2] + dp[i - 1][j - a[i]][1] + dp[i - 1][j - a[i]][0]) % mod; } } } cout << dp[n][s][2]; }
replace
10
11
10
11
-11
p02734
C++
Runtime Error
#include <bits/stdc++.h> /*#pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") */ // #include <ext/pb_ds/assoc_container.hpp> using namespace std; // using namespace __gnu_pbds; #define ss second #define ff first #define int long long // #define double long double #define pb push_back const int N = 2e6 + 51, MOD = 1e9 + 7, mod = 998244353, INF = 2e18; mt19937 Flash_new_51(51515); using vi = vector<int>; using vp = vector<pair<int, int>>; int a[N]; signed main() { #ifdef Flash freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int n, s; cin >> n >> s; int ans = 0; for (int i = 0; i < n; i++) cin >> a[i]; vi dp(s + 1, 0); for (int i = 0; i < n; i++) { int x = a[i]; vi tek(s + 1, 0); for (int j = 0; j < s; j++) { if (j == 0) { tek[x] = i + 1; } else { if (i) { tek[j] += dp[j]; if (j + x <= s) { tek[j + x] += dp[j]; tek[j + x] %= mod; } tek[j] %= mod; } } } dp = tek; ans += dp[s] * (n - i); // cout << ans << ' '; ans %= mod; } cout << ans; }
#include <bits/stdc++.h> /*#pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") */ // #include <ext/pb_ds/assoc_container.hpp> using namespace std; // using namespace __gnu_pbds; #define ss second #define ff first #define int long long // #define double long double #define pb push_back const int N = 2e6 + 51, MOD = 1e9 + 7, mod = 998244353, INF = 2e18; mt19937 Flash_new_51(51515); using vi = vector<int>; using vp = vector<pair<int, int>>; int a[N]; signed main() { #ifdef Flash freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int n, s; cin >> n >> s; int ans = 0; for (int i = 0; i < n; i++) cin >> a[i]; vi dp(s + 1, 0); for (int i = 0; i < n; i++) { int x = a[i]; vi tek(s + 1, 0); for (int j = 0; j < s; j++) { if (j == 0 && x <= s) { tek[x] = i + 1; } else { if (i) { tek[j] += dp[j]; if (j + x <= s) { tek[j + x] += dp[j]; tek[j + x] %= mod; } tek[j] %= mod; } } } dp = tek; ans += dp[s] * (n - i); // cout << ans << ' '; ans %= mod; } cout << ans; }
replace
44
45
44
45
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #define mxn 1000010 #define db double #define LL long long #define ldb long double #define ull unsigned long long #define pb push_back #define ppb pop_back #define pf push_front #define pul pair<ull, LL> #define pii pair<int, int> #define mp make_pair #define fr first #define sc second #define x1 faqx1 #define y1 faqy1 #define x2 faqx2 #define y2 faqy2 using namespace std; const int mod = 998244353; template <typename T> void chkmin(T &x, T y) { if (y < x) x = y; } template <typename T> void chkmax(T &x, T y) { if (y > x) x = y; } template <typename T> void upd(T &x, T y) { if ((x += y) >= mod) x -= mod; } int n, K, sl, fh, ans, a[3010], f[3010]; int rd() { sl = 0; fh = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') fh = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar(); return sl * fh; } int main() { n = rd(); K = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n; ++i) { ans = (ans + i * (n - i + 1) * (a[i] == K) + 1ll * f[K - a[i]] * (n - i + 1)) % mod; for (int j = K; j >= a[i]; --j) upd(f[j], f[j - a[i]]); upd(f[a[i]], i); } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #define mxn 1000010 #define db double #define LL long long #define ldb long double #define ull unsigned long long #define pb push_back #define ppb pop_back #define pf push_front #define pul pair<ull, LL> #define pii pair<int, int> #define mp make_pair #define fr first #define sc second #define x1 faqx1 #define y1 faqy1 #define x2 faqx2 #define y2 faqy2 using namespace std; const int mod = 998244353; template <typename T> void chkmin(T &x, T y) { if (y < x) x = y; } template <typename T> void chkmax(T &x, T y) { if (y > x) x = y; } template <typename T> void upd(T &x, T y) { if ((x += y) >= mod) x -= mod; } int n, K, sl, fh, ans, a[3010], f[3010]; int rd() { sl = 0; fh = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') fh = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar(); return sl * fh; } int main() { n = rd(); K = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n; ++i) if (a[i] <= K) { ans = (ans + i * (n - i + 1) * (a[i] == K) + 1ll * f[K - a[i]] * (n - i + 1)) % mod; for (int j = K; j >= a[i]; --j) upd(f[j], f[j - a[i]]); upd(f[a[i]], i); } printf("%d\n", ans); return 0; }
replace
51
59
51
60
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int main() { int n, s; cin >> n >> s; int a[n + 1], dp[n + 1][s + 1]; for (int i = 1; i <= n; i++) cin >> a[i]; memset(dp, 0, sizeof(dp)); // dp[0][0]=1; int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= s; j++) { // if(a[i]==j)continue; dp[i][j] += dp[i - 1][j]; dp[i][j] %= mod; } dp[i][a[i]] += i; dp[i][a[i]] %= mod; for (int j = a[i]; j <= s; j++) { dp[i][j] += dp[i - 1][j - a[i]]; dp[i][j] %= mod; } ans += dp[i][s]; ans %= mod; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int main() { int n, s; cin >> n >> s; int a[n + 1], dp[n + 1][3005]; for (int i = 1; i <= n; i++) cin >> a[i]; memset(dp, 0, sizeof(dp)); // dp[0][0]=1; int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= s; j++) { // if(a[i]==j)continue; dp[i][j] += dp[i - 1][j]; dp[i][j] %= mod; } dp[i][a[i]] += i; dp[i][a[i]] %= mod; for (int j = a[i]; j <= s; j++) { dp[i][j] += dp[i - 1][j - a[i]]; dp[i][j] %= mod; } ans += dp[i][s]; ans %= mod; } cout << ans << endl; return 0; }
replace
7
8
7
8
0
p02734
C++
Runtime Error
// author:luckYrat(twitter:@luckYrat_) //<ここに一言> // せんげん! #include <iostream> #include <algorithm> #include <cmath> #include <iomanip> #include <bitset> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <cctype> #include <climits> #include <utility> // なまえくーかん! using namespace std; using ll = long long; using P = pair<ll, ll>; // てーすう! const int mod = 998244353; const int inf = (1 << 30) - 1; const ll linf = (1LL << 62LL) - 1; const double EPS = (1e-10); // でふぁいん! #define anyfill(n, s) setw(n) << setfill(s) #define loop(s) for (int i = 0; s > i; i++) #define rep(i, q) for (int i = 0; (q) > i; i++) #define repp(i, n, q) for (int i = n; (q) > i; i++) #define dep(i, q) for (int i = (q); 0 < i; i--) // みじかく! #define pb push_back #define fir first #define scn second #define ednl endl // いぇすのー! #define YesNo(a) (a ? "Yes" : "No") #define YESNO(a) (a ? "YES" : "NO") #define yesno(a) (a ? "yes" : "no") // きんぼーnほーこー!! P ar4[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; P ar8[8] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; /* 確認ポイント cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる 計算量は変わらないが楽できるシリーズ min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる */ // B template <int mod> struct ModInt { int n; ModInt() : n(0) {} ModInt(int n_) : n(n_ >= 0 ? n_ % mod : mod - ((-n_) % mod)) {} ModInt &operator+=(const ModInt &p) { if ((n += p.n) >= mod) n -= mod; return *this; } ModInt &operator-=(const ModInt &p) { n += mod - p.n; if (n >= mod) n -= mod; return *this; } ModInt &operator*=(const ModInt &p) { n = (int)((1LL * n * p.n) % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-n); } 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 n == p.n; } bool operator<(const ModInt &p) const { return n < p.n; } bool operator>(const ModInt &p) const { return n > p.n; } bool operator>=(const ModInt &p) const { return n >= p.n; } bool operator<=(const ModInt &p) const { return n <= p.n; } bool operator!=(const ModInt &p) const { return n != p.n; } ModInt inverse() const { int a = n, b = mod, u = 1, v = 0; while (b) { int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } ModInt pow(int64_t z) const { ModInt ret(1), mul(n); while (z > 0) { if (z & 1) ret *= mul; mul *= mul; z >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.n; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } }; using mint = ModInt<mod>; // E int n, s; vector<int> A; mint dp[3001][9000]; // nw,p __attribute__((constructor)) void initial() { cin.tie(0); ios::sync_with_stdio(false); } int main() { cin >> n >> s; for (int i = 0; n > i; i++) { int t; cin >> t; A.push_back(t); } for (int i = 0; n >= i; i++) { dp[i][0] = i + 1; } for (int i = 0; n >= i; i++) { for (int j = 0; s >= j; j++) { if (j) dp[i + 1][j] += dp[i][j]; dp[i + 1][j + A[i]] += dp[i][j]; } } mint ans = 0; for (int i = 0; n >= i; i++) { ans += dp[i][s]; } cout << ans << endl; }
// author:luckYrat(twitter:@luckYrat_) //<ここに一言> // せんげん! #include <iostream> #include <algorithm> #include <cmath> #include <iomanip> #include <bitset> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <cctype> #include <climits> #include <utility> // なまえくーかん! using namespace std; using ll = long long; using P = pair<ll, ll>; // てーすう! const int mod = 998244353; const int inf = (1 << 30) - 1; const ll linf = (1LL << 62LL) - 1; const double EPS = (1e-10); // でふぁいん! #define anyfill(n, s) setw(n) << setfill(s) #define loop(s) for (int i = 0; s > i; i++) #define rep(i, q) for (int i = 0; (q) > i; i++) #define repp(i, n, q) for (int i = n; (q) > i; i++) #define dep(i, q) for (int i = (q); 0 < i; i--) // みじかく! #define pb push_back #define fir first #define scn second #define ednl endl // いぇすのー! #define YesNo(a) (a ? "Yes" : "No") #define YESNO(a) (a ? "YES" : "NO") #define yesno(a) (a ? "yes" : "no") // きんぼーnほーこー!! P ar4[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; P ar8[8] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; /* 確認ポイント cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる 計算量は変わらないが楽できるシリーズ min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる */ // B template <int mod> struct ModInt { int n; ModInt() : n(0) {} ModInt(int n_) : n(n_ >= 0 ? n_ % mod : mod - ((-n_) % mod)) {} ModInt &operator+=(const ModInt &p) { if ((n += p.n) >= mod) n -= mod; return *this; } ModInt &operator-=(const ModInt &p) { n += mod - p.n; if (n >= mod) n -= mod; return *this; } ModInt &operator*=(const ModInt &p) { n = (int)((1LL * n * p.n) % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-n); } 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 n == p.n; } bool operator<(const ModInt &p) const { return n < p.n; } bool operator>(const ModInt &p) const { return n > p.n; } bool operator>=(const ModInt &p) const { return n >= p.n; } bool operator<=(const ModInt &p) const { return n <= p.n; } bool operator!=(const ModInt &p) const { return n != p.n; } ModInt inverse() const { int a = n, b = mod, u = 1, v = 0; while (b) { int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } ModInt pow(int64_t z) const { ModInt ret(1), mul(n); while (z > 0) { if (z & 1) ret *= mul; mul *= mul; z >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.n; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } }; using mint = ModInt<mod>; // E int n, s; vector<int> A; mint dp[3002][9000]; // nw,p __attribute__((constructor)) void initial() { cin.tie(0); ios::sync_with_stdio(false); } int main() { cin >> n >> s; for (int i = 0; n > i; i++) { int t; cin >> t; A.push_back(t); } for (int i = 0; n >= i; i++) { dp[i][0] = i + 1; } for (int i = 0; n >= i; i++) { for (int j = 0; s >= j; j++) { if (j) dp[i + 1][j] += dp[i][j]; dp[i + 1][j + A[i]] += dp[i][j]; } } mint ans = 0; for (int i = 0; n >= i; i++) { ans += dp[i][s]; } cout << ans << endl; }
replace
136
137
136
137
-11
p02734
C++
Runtime Error
#include "algorithm" #include "bitset" #include "cassert" #include "climits" #include "cmath" #include "cstdio" #include "ctime" #include "functional" #include "iomanip" #include "iostream" #include "list" #include "map" #include "numeric" #include "queue" #include "random" #include "set" #include "stack" #include "string" #include "unordered_map" #include "unordered_set" using namespace std; // constexpr long long int MOD = 1000000007; // constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr long long int MOD = 998244353; constexpr long double EPS = 1e-8; long long int N, M, K, L, R, H, W; // int N, M, K, L, R, H, W; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; vector<int> v(N); for (auto &i : v) cin >> i; vector<vector<long long int>> dp(N + 1, vector<long long int>(M + 1)); for (int i = 0; i < N; i++) { dp[i + 1][v[i]] = i + 1; for (int j = 0; j <= M; j++) { if (j != M) dp[i + 1][j] += dp[i][j]; if (j >= v[i]) dp[i + 1][j] += dp[i][j - v[i]]; dp[i + 1][j] %= MOD; } } long long int ans = 0; for (int i = 1; i <= N; i++) { ans += dp[i][M] * (N - i + 1) % MOD; } cout << ans % MOD << endl; }
#include "algorithm" #include "bitset" #include "cassert" #include "climits" #include "cmath" #include "cstdio" #include "ctime" #include "functional" #include "iomanip" #include "iostream" #include "list" #include "map" #include "numeric" #include "queue" #include "random" #include "set" #include "stack" #include "string" #include "unordered_map" #include "unordered_set" using namespace std; // constexpr long long int MOD = 1000000007; // constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr long long int MOD = 998244353; constexpr long double EPS = 1e-8; long long int N, M, K, L, R, H, W; // int N, M, K, L, R, H, W; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; vector<int> v(N); for (auto &i : v) cin >> i; vector<vector<long long int>> dp(N + 1, vector<long long int>(M + 1)); for (int i = 0; i < N; i++) { if (v[i] <= M) dp[i + 1][v[i]] = i + 1; for (int j = 0; j <= M; j++) { if (j != M) dp[i + 1][j] += dp[i][j]; if (j >= v[i]) dp[i + 1][j] += dp[i][j - v[i]]; dp[i + 1][j] %= MOD; } } long long int ans = 0; for (int i = 1; i <= N; i++) { ans += dp[i][M] * (N - i + 1) % MOD; } cout << ans % MOD << endl; }
replace
42
43
42
44
0
p02734
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; #define int ll #define pb push_back #define pii pair<int, int> #define ff first #define ss second typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int MAX = 2003, MOD = 998244353; int dp[MAX][MAX], arr[MAX], n, s; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> s; for (int i = 1; i <= n; i++) cin >> arr[i]; int ans = 0; for (int i = 1; i <= n; i++) { dp[i][arr[i]] = i; for (int j = 0; j + arr[i] <= s; j++) { dp[i][j + arr[i]] += dp[i - 1][j]; dp[i][j + arr[i]] %= MOD; } ans = (ans + (n - i + 1) * dp[i][s]) % MOD; for (int j = 0; j <= s; j++) dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD; } cout << ans; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; #define int ll #define pb push_back #define pii pair<int, int> #define ff first #define ss second typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int MAX = 3003, MOD = 998244353; int dp[MAX][MAX], arr[MAX], n, s; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> s; for (int i = 1; i <= n; i++) cin >> arr[i]; int ans = 0; for (int i = 1; i <= n; i++) { dp[i][arr[i]] = i; for (int j = 0; j + arr[i] <= s; j++) { dp[i][j + arr[i]] += dp[i - 1][j]; dp[i][j + arr[i]] %= MOD; } ans = (ans + (n - i + 1) * dp[i][s]) % MOD; for (int j = 0; j <= s; j++) dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD; } cout << ans; return 0; }
replace
19
20
19
20
0
p02734
C++
Memory Limit Exceeded
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define MOD2 998244353 #define int long long #define double long double #define EPS 1e-9 // #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) { int N = A.size(); int M; if (N > 0) M = A[0].size(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) os << A[i][j] << " "; os << endl; } return os; } typedef pair<int, int> pii; typedef long long ll; struct edge { int from, to, d, c; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector<edge> edges; typedef vector<edges> graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector<vector<flow>> flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1LL << n; } template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(20); } }; initializer _____; int N, M, K, T, Q, H, W; signed main() { cin >> N; int S; cin >> S; vector<int> A(N); rep(i, N) cin >> A[i]; vector<vector<vector<int>>> dp(N + 1, vector<vector<int>>(6666, vector<int>(3, 0))); dp[0][0][0] = 1; rep(i, N) rep(j, S + 1) { (dp[i + 1][j][0] += dp[i][j][0]) %= MOD2; (dp[i + 1][j + A[i]][1] += dp[i][j][0] * (i + 1) % MOD2) %= MOD2; (dp[i + 1][j + A[i]][2] += dp[i][j][0] * (i + 1) % MOD2 * (N - i) % MOD2) %= MOD2; (dp[i + 1][j][1] += dp[i][j][1]) %= MOD2; (dp[i + 1][j + A[i]][1] += dp[i][j][1]) %= MOD2; (dp[i + 1][j + A[i]][2] += dp[i][j][1] * (N - i) % MOD2) %= MOD2; (dp[i + 1][j][2] += dp[i][j][2]) %= MOD2; } cout << dp[N][S][2] << endl; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define MOD2 998244353 #define int long long #define double long double #define EPS 1e-9 // #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) { int N = A.size(); int M; if (N > 0) M = A[0].size(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) os << A[i][j] << " "; os << endl; } return os; } typedef pair<int, int> pii; typedef long long ll; struct edge { int from, to, d, c; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector<edge> edges; typedef vector<edges> graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector<vector<flow>> flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1LL << n; } template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(20); } }; initializer _____; int N, M, K, T, Q, H, W; signed main() { cin >> N; int S; cin >> S; vector<int> A(N); rep(i, N) cin >> A[i]; vector<vector<vector<int>>> dp(N + 1, vector<vector<int>>(6005, vector<int>(3, 0))); dp[0][0][0] = 1; rep(i, N) rep(j, S + 1) { (dp[i + 1][j][0] += dp[i][j][0]) %= MOD2; (dp[i + 1][j + A[i]][1] += dp[i][j][0] * (i + 1) % MOD2) %= MOD2; (dp[i + 1][j + A[i]][2] += dp[i][j][0] * (i + 1) % MOD2 * (N - i) % MOD2) %= MOD2; (dp[i + 1][j][1] += dp[i][j][1]) %= MOD2; (dp[i + 1][j + A[i]][1] += dp[i][j][1]) %= MOD2; (dp[i + 1][j + A[i]][2] += dp[i][j][1] * (N - i) % MOD2) %= MOD2; (dp[i + 1][j][2] += dp[i][j][2]) %= MOD2; } cout << dp[N][S][2] << endl; return 0; }
replace
113
114
113
114
MLE
p02734
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define InfL 1000000000 #define InfLL 1000000000000000000LL #define mod 998244353 #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--) using namespace std; typedef long long ll; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; typedef vector<db> vd; int main() { int N, S; cin >> N >> S; vl A(N); rep(i, N) cin >> A[i]; vector<vl> DP(N + 1); rep(i, N + 1) DP[i].resize( S + 3, 0); // 0~S : 処理中で和がi, S+1 : 左端未確定, S+2 : 右端確定 DP[0][S + 1] = 1; rep(i, N) { ll Atmp = A[i]; rep(j, S + 1) { DP[i + 1][j] += DP[i][j]; // 選ばない DP[i + 1][j] %= mod; ll jnxt = j + Atmp; if (jnxt > S) continue; DP[i + 1][jnxt] += DP[i][j]; // 選ぶ DP[i + 1][jnxt] %= mod; } DP[i + 1][0] += DP[i][S + 1]; // 左端 : i, 選ばない DP[i + 1][0] %= mod; DP[i + 1][Atmp] += DP[i][S + 1]; // 左端 : i, 選ぶ DP[i + 1][Atmp] %= mod; DP[i + 1][S + 1] += DP[i][S + 1]; // 左端 : >i DP[i + 1][S + 1] %= mod; DP[i + 1][S + 2] += DP[i][S]; // 右端 : i, 選ばない DP[i + 1][S + 2] %= mod; if (S - Atmp >= 0) { DP[i + 1][S + 2] += DP[i][S - Atmp]; // 右端 : i, 選ぶ DP[i + 1][S + 2] %= mod; } if (S - Atmp == 0) { DP[i + 1][S + 2] += DP[i][S + 1]; // 左端,右端 : i, 選ぶ DP[i + 1][S + 2] %= mod; } DP[i + 1][S + 2] += DP[i][S + 2]; // 右端 : <i DP[i + 1][S + 2] %= mod; } ll ans = DP[N][S + 2]; cout << ans << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define InfL 1000000000 #define InfLL 1000000000000000000LL #define mod 998244353 #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--) using namespace std; typedef long long ll; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; typedef vector<db> vd; int main() { int N, S; cin >> N >> S; vl A(N); rep(i, N) cin >> A[i]; vector<vl> DP(N + 1); rep(i, N + 1) DP[i].resize( S + 3, 0); // 0~S : 処理中で和がi, S+1 : 左端未確定, S+2 : 右端確定 DP[0][S + 1] = 1; rep(i, N) { ll Atmp = A[i]; rep(j, S + 1) { DP[i + 1][j] += DP[i][j]; // 選ばない DP[i + 1][j] %= mod; ll jnxt = j + Atmp; if (jnxt > S) continue; DP[i + 1][jnxt] += DP[i][j]; // 選ぶ DP[i + 1][jnxt] %= mod; } DP[i + 1][0] += DP[i][S + 1]; // 左端 : i, 選ばない DP[i + 1][0] %= mod; if (Atmp <= S) { DP[i + 1][Atmp] += DP[i][S + 1]; // 左端 : i, 選ぶ DP[i + 1][Atmp] %= mod; } DP[i + 1][S + 1] += DP[i][S + 1]; // 左端 : >i DP[i + 1][S + 1] %= mod; DP[i + 1][S + 2] += DP[i][S]; // 右端 : i, 選ばない DP[i + 1][S + 2] %= mod; if (S - Atmp >= 0) { DP[i + 1][S + 2] += DP[i][S - Atmp]; // 右端 : i, 選ぶ DP[i + 1][S + 2] %= mod; } if (S - Atmp == 0) { DP[i + 1][S + 2] += DP[i][S + 1]; // 左端,右端 : i, 選ぶ DP[i + 1][S + 2] %= mod; } DP[i + 1][S + 2] += DP[i][S + 2]; // 右端 : <i DP[i + 1][S + 2] %= mod; } ll ans = DP[N][S + 2]; cout << ans << endl; return 0; }
replace
49
51
49
53
0