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
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int table[80][80]; bool dp[80][80][15000] = {}; int main() { int H, W; cin >> H >> W; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> table[i][j]; } } int m = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int b; cin >> b; table[i][j] = abs(table[i][j] - b); m = max(m, table[i][j]); } } dp[0][0][table[0][0]] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k <= (H * W) * m; k++) { if (dp[i][j][k]) { if (j < W - 1) { dp[i][j + 1][k + table[i][j + 1]] = true; dp[i][j + 1][abs(k - table[i][j + 1])] = true; } if (i < H - 1) { dp[i + 1][j][k + table[i + 1][j]] = true; dp[i + 1][j][abs(k - table[i + 1][j])] = true; } } } } } for (int k = 0; k <= (H + W) * m; k++) { if (dp[H - 1][W - 1][k]) { cout << k << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; int table[80][80]; bool dp[80][80][15000] = {}; int main() { int H, W; cin >> H >> W; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> table[i][j]; } } int m = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { int b; cin >> b; table[i][j] = abs(table[i][j] - b); m = max(m, table[i][j]); } } dp[0][0][table[0][0]] = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k <= (H + W) * m; k++) { if (dp[i][j][k]) { if (j < W - 1) { dp[i][j + 1][k + table[i][j + 1]] = true; dp[i][j + 1][abs(k - table[i][j + 1])] = true; } if (i < H - 1) { dp[i + 1][j][k + table[i + 1][j]] = true; dp[i + 1][j][abs(k - table[i + 1][j])] = true; } } } } } for (int k = 0; k <= (H + W) * m; k++) { if (dp[H - 1][W - 1][k]) { cout << k << endl; break; } } return 0; }
replace
26
27
26
27
TLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> Pii; #define pb push_back #define mp make_pair #define MOD 1000000007 #define INF 1000000000 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define fs first #define ss second inline int Scan() { int x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } // call by reference is used in x template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } //////////////////////////////////////end of ///template////////////////////////////////////// bool dp[81][81][25600]; int a[81][81]; int main() { int h, w; cin >> h >> w; int x; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> a[i][j]; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) { cin >> x; a[i][j] = abs(a[i][j] - x); }; for (int i = 0; i <= 80; i++) for (int j = 0; j <= 80; j++) for (int k = 0; k <= 12800; k++) dp[i][j][k] = false; dp[1][1][a[1][1]] = true; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { for (int k = 0; k <= 12800; k++) { if (dp[i][j][k]) { dp[i][j + 1][k + a[i][j + 1]] = true; dp[i][j + 1][abs(k - a[i][j + 1])] = true; dp[i + 1][j][k + a[i + 1][j]] = true; dp[i + 1][j][abs(k - a[i + 1][j])] = true; } } } } for (int i = 0; i <= 12800; i++) { if (dp[h][w][i]) { cout << i << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> Pii; #define pb push_back #define mp make_pair #define MOD 1000000007 #define INF 1000000000 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define fs first #define ss second inline int Scan() { int x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } // call by reference is used in x template <typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } //////////////////////////////////////end of ///template////////////////////////////////////// bool dp[90][90][25600]; int a[90][90]; int main() { int h, w; cin >> h >> w; int x; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> a[i][j]; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) { cin >> x; a[i][j] = abs(a[i][j] - x); }; for (int i = 0; i <= 80; i++) for (int j = 0; j <= 80; j++) for (int k = 0; k <= 12800; k++) dp[i][j][k] = false; dp[1][1][a[1][1]] = true; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { for (int k = 0; k <= 12800; k++) { if (dp[i][j][k]) { dp[i][j + 1][k + a[i][j + 1]] = true; dp[i][j + 1][abs(k - a[i][j + 1])] = true; dp[i + 1][j][k + a[i + 1][j]] = true; dp[i + 1][j][abs(k - a[i + 1][j])] = true; } } } } for (int i = 0; i <= 12800; i++) { if (dp[h][w][i]) { cout << i << endl; break; } } return 0; }
replace
41
43
41
43
-11
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define REP(i, n) for (int i = 0; i < (n); ++i) #define RANGE(i, a, b) for (int i = (a); i < (b); ++i) #define RREP(i, n) for (int i = (n)-1; i >= 0; --i) #define endl '\n' #define pb push_back #define eb emplace_back #define pque priority_queue #define umap unordered_map #define uset unordered_set #define BIG 2000000000 #define VERYBIG 1000000000000000ll #define PI 3.141592653589793238 #define dcout cout << fixed << setprecision(50) #define _popcnt __builtin_popcount const long long dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; const long long MOD = 1e9 + 7; // typedef long long ll; #define int long long #define double long double template <typename T> inline T GCD(T a, T b) { T c; while (b != 0) { c = a % b; a = b; b = c; } return a; } template <typename T> inline T LCM(T a, T b) { T c = GCD(a, b); a /= c; return a * b; } template <typename T> inline T nCr(T a, T b) { T i, r = 1; for (i = 1; i <= b; i++) { r *= (a + 1 - i); r /= i; } return r; } template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } typedef pair<int, int> INTP; typedef vector<int> INTV; struct edge2 { int to, cost; }; struct edge3 { int from, to, cost; }; bool dp[81][81][30000]; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); int H, W, A[80][80], B[80][80]; cin >> H >> W; REP(i, H) REP(j, W) cin >> A[i][j]; REP(i, H) REP(j, W) cin >> B[i][j]; int dh[2] = {1, 0}, dw[2] = {0, 1}; dp[0][0][15000 + A[0][0] - B[0][0]] = 1; dp[0][0][15000 - A[0][0] + B[0][0]] = 1; REP(i, H) REP(j, W) REP(bias, 30000) { if (i == H - 1 and j == W - 1) continue; if (!dp[i][j][bias]) continue; REP(dir, 2) { int nxh = i + dh[dir], nxw = j + dw[dir]; int nxb_Ared = bias + A[nxh][nxw] - B[nxh][nxw]; int nxb_Ablu = bias - A[nxh][nxw] + B[nxh][nxw]; dp[nxh][nxw][nxb_Ared] |= dp[i][j][bias]; dp[nxh][nxw][nxb_Ablu] |= dp[i][j][bias]; } } int ans = BIG; REP(i, 30000) { if (dp[H - 1][W - 1][i]) chmin(ans, abs(i - 15000)); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define REP(i, n) for (int i = 0; i < (n); ++i) #define RANGE(i, a, b) for (int i = (a); i < (b); ++i) #define RREP(i, n) for (int i = (n)-1; i >= 0; --i) #define endl '\n' #define pb push_back #define eb emplace_back #define pque priority_queue #define umap unordered_map #define uset unordered_set #define BIG 2000000000 #define VERYBIG 1000000000000000ll #define PI 3.141592653589793238 #define dcout cout << fixed << setprecision(50) #define _popcnt __builtin_popcount const long long dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; const long long MOD = 1e9 + 7; // typedef long long ll; #define int long long #define double long double template <typename T> inline T GCD(T a, T b) { T c; while (b != 0) { c = a % b; a = b; b = c; } return a; } template <typename T> inline T LCM(T a, T b) { T c = GCD(a, b); a /= c; return a * b; } template <typename T> inline T nCr(T a, T b) { T i, r = 1; for (i = 1; i <= b; i++) { r *= (a + 1 - i); r /= i; } return r; } template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } typedef pair<int, int> INTP; typedef vector<int> INTV; struct edge2 { int to, cost; }; struct edge3 { int from, to, cost; }; bool dp[81][81][30000]; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); int H, W, A[80][80], B[80][80]; cin >> H >> W; REP(i, H) REP(j, W) cin >> A[i][j]; REP(i, H) REP(j, W) cin >> B[i][j]; int dh[2] = {1, 0}, dw[2] = {0, 1}; dp[0][0][15000 + A[0][0] - B[0][0]] = 1; dp[0][0][15000 - A[0][0] + B[0][0]] = 1; REP(i, H) REP(j, W) REP(bias, 30000) { if (i == H - 1 and j == W - 1) continue; if (!dp[i][j][bias]) continue; REP(dir, 2) { int nxh = i + dh[dir], nxw = j + dw[dir]; int nxb_Ared = bias + A[nxh][nxw] - B[nxh][nxw]; int nxb_Ablu = bias - A[nxh][nxw] + B[nxh][nxw]; if (0 > nxb_Ared or nxb_Ared >= 30000) continue; if (0 > nxb_Ablu or nxb_Ablu >= 30000) continue; dp[nxh][nxw][nxb_Ared] |= dp[i][j][bias]; dp[nxh][nxw][nxb_Ablu] |= dp[i][j][bias]; } } int ans = BIG; REP(i, 30000) { if (dp[H - 1][W - 1][i]) chmin(ans, abs(i - 15000)); } cout << ans << endl; }
insert
98
98
98
102
-11
p02839
C++
Runtime Error
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; #define ll long long const int mod = 1000000007; const ll INF = 1000000000000000000; typedef pair<int, int> P; bool dp[100][100][10000]; int A[100][100], B[100][100], C[100][100]; int num; int main() { int H, W; cin >> H >> W; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; C[i][j] = abs(A[i][j] - B[i][j]); } } dp[0][0][C[0][0]] = true; num = C[0][0]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i - 1 >= 0) { for (int k = 0; k <= num; k++) { if (dp[i - 1][j][k]) { dp[i][j][abs(k - C[i][j])] = true; dp[i][j][k + C[i][j]] = true; num = max(num, k + C[i][j]); } } } if (j - 1 >= 0) { for (int k = 0; k <= num; k++) { if (dp[i][j - 1][k]) { dp[i][j][abs(k - C[i][j])] = true; dp[i][j][k + C[i][j]] = true; num = max(num, k + C[i][j]); } } } } } /*for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << i << " " << j << " "; for (int k = 0; k < 160; k++) { if (dp[i][j][k]) { cout << k << " "; } } cout << endl; } }*/ for (int i = 0; i < 160; i++) { if (dp[H - 1][W - 1][i]) { cout << i << endl; return 0; } } }
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; #define ll long long const int mod = 1000000007; const ll INF = 1000000000000000000; typedef pair<int, int> P; bool dp[100][100][20000]; int A[100][100], B[100][100], C[100][100]; int num; int main() { int H, W; cin >> H >> W; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; C[i][j] = abs(A[i][j] - B[i][j]); } } dp[0][0][C[0][0]] = true; num = C[0][0]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i - 1 >= 0) { for (int k = 0; k <= num; k++) { if (dp[i - 1][j][k]) { dp[i][j][abs(k - C[i][j])] = true; dp[i][j][k + C[i][j]] = true; num = max(num, k + C[i][j]); } } } if (j - 1 >= 0) { for (int k = 0; k <= num; k++) { if (dp[i][j - 1][k]) { dp[i][j][abs(k - C[i][j])] = true; dp[i][j][k + C[i][j]] = true; num = max(num, k + C[i][j]); } } } } } /*for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << i << " " << j << " "; for (int k = 0; k < 160; k++) { if (dp[i][j][k]) { cout << k << " "; } } cout << endl; } }*/ for (int i = 0; i < 160; i++) { if (dp[H - 1][W - 1][i]) { cout << i << endl; return 0; } } }
replace
14
15
14
15
127
/tmp/f6cffec1-f856-4a76-a59f-3b7c23df2e3f.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
p02839
C++
Runtime Error
// By Mohit Gupta #include <bits/stdc++.h> using namespace std; #define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " ")) #define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " ")) #define forstl(i, v) for (auto &i : v) #define forn(i, e) for (int i = 0; i < e; i++) #define forsn(i, s, e) for (int i = s; i < e; i++) #define rforn(i, s) for (int i = s; i >= 0; i--) #define rforsn(i, s, e) for (int i = s; i >= e; i--) #define clz(a) __builtin_clz(a) // count leading zeroes #define ctz(a) __builtin_ctz(a) // count trailing zeroes #define popc(a) \ __builtin_popcount(a) // count set bits (for ints only diff for ll) // https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built #define ln "\n" #define dbg(x) cout << #x << " = " << x << ln #define mp make_pair #define pb push_back #define fi first #define se second #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef long long ll; typedef pair<int, int> p32; typedef pair<ll, ll> p64; typedef pair<double, double> pdd; typedef vector<ll> v64; typedef vector<int> v32; typedef vector<vector<int>> vv32; typedef vector<p64> vp64; typedef vector<p32> vp32; typedef map<int, int> m32; #pragma GCC optimize("-O2") const int LIM = 5e5 + 5, MOD = 1e9 + 7; int t, n, m, k, x, y; const int M = 160 * 80 + 1; int dp[80][80][M]; int a[81][81]; int b[81][81]; int main() { IOS; cin >> n >> m; forn(i, n) forn(j, m) cin >> a[i][j]; forn(i, n) forn(j, m) cin >> b[i][j]; x = abs(a[0][0] - b[0][0]); dp[0][0][x] = 1; forn(i, n) { forn(j, m) { forn(k, M) { if (dp[i][j][k]) { dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = 1; dp[i + 1][j][abs(k - abs((a[i + 1][j] - b[i + 1][j])))] = 1; dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = 1; dp[i][j + 1][abs(k - abs((a[i][j + 1] - b[i][j + 1])))] = 1; } } } } int ans = 0; forn(i, M) { if (dp[n - 1][m - 1][i]) { ans = i; break; } } cout << ans << ln; }
// By Mohit Gupta #include <bits/stdc++.h> using namespace std; #define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " ")) #define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " ")) #define forstl(i, v) for (auto &i : v) #define forn(i, e) for (int i = 0; i < e; i++) #define forsn(i, s, e) for (int i = s; i < e; i++) #define rforn(i, s) for (int i = s; i >= 0; i--) #define rforsn(i, s, e) for (int i = s; i >= e; i--) #define clz(a) __builtin_clz(a) // count leading zeroes #define ctz(a) __builtin_ctz(a) // count trailing zeroes #define popc(a) \ __builtin_popcount(a) // count set bits (for ints only diff for ll) // https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built #define ln "\n" #define dbg(x) cout << #x << " = " << x << ln #define mp make_pair #define pb push_back #define fi first #define se second #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef long long ll; typedef pair<int, int> p32; typedef pair<ll, ll> p64; typedef pair<double, double> pdd; typedef vector<ll> v64; typedef vector<int> v32; typedef vector<vector<int>> vv32; typedef vector<p64> vp64; typedef vector<p32> vp32; typedef map<int, int> m32; #pragma GCC optimize("-O2") const int LIM = 5e5 + 5, MOD = 1e9 + 7; int t, n, m, k, x, y; const int M = 160 * 80 + 1; int dp[81][81][M]; int a[81][81]; int b[81][81]; int main() { IOS; cin >> n >> m; forn(i, n) forn(j, m) cin >> a[i][j]; forn(i, n) forn(j, m) cin >> b[i][j]; x = abs(a[0][0] - b[0][0]); dp[0][0][x] = 1; forn(i, n) { forn(j, m) { forn(k, M) { if (dp[i][j][k]) { dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = 1; dp[i + 1][j][abs(k - abs((a[i + 1][j] - b[i + 1][j])))] = 1; dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = 1; dp[i][j + 1][abs(k - abs((a[i][j + 1] - b[i][j + 1])))] = 1; } } } } int ans = 0; forn(i, M) { if (dp[n - 1][m - 1][i]) { ans = i; break; } } cout << ans << ln; }
replace
39
40
39
40
-11
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = int_fast64_t; using ld = long double; using pll = pair<ll, ll>; using pld = pair<ld, ld>; const ll INF = 1LL << 60; void solve(); int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); } #define foreach(i, a) for (auto &i : a) #define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT #define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__) #define _rep0(i, n) for (ll i = 0; i < n; ++i) #define _rep1(i, k, n) for (ll i = k; i < n; ++i) #define _rep2(i, k, n, d) \ for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d) template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <class... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } void solve() { ll h, w; cin >> h >> w; auto A = make_v(h, w, 0); auto B = make_v(h, w, 0); rep(i, h) rep(j, w) cin >> A[i][j]; rep(i, h) rep(j, w) cin >> B[i][j]; ll M = 10000; vector<vector<vector<bool>>> dp = make_v(h, w, M, false); ll x = abs(A[h - 1][w - 1] - B[h - 1][w - 1]); dp[h - 1][w - 1][x] = true; rep(i, h - 1, -1, -1) rep(j, w - 1, -1, -1) { ll x = abs(A[i][j] - B[i][j]); if (i + 1 < h) { rep(k, M) { if (dp[i + 1][j][k]) { dp[i][j][k + x] = true; dp[i][j][abs(k - x)] = true; } } } if (j + 1 < w) { rep(k, M) { if (dp[i][j + 1][k]) { dp[i][j][k + x] = true; dp[i][j][abs(k - x)] = true; } } } } rep(k, M) { if (dp[0][0][k]) { cout << k << "\n"; return; } } }
#include <bits/stdc++.h> using namespace std; using ll = int_fast64_t; using ld = long double; using pll = pair<ll, ll>; using pld = pair<ld, ld>; const ll INF = 1LL << 60; void solve(); int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); } #define foreach(i, a) for (auto &i : a) #define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT #define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__) #define _rep0(i, n) for (ll i = 0; i < n; ++i) #define _rep1(i, k, n) for (ll i = k; i < n; ++i) #define _rep2(i, k, n, d) \ for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d) template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <class... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } void solve() { ll h, w; cin >> h >> w; auto A = make_v(h, w, 0); auto B = make_v(h, w, 0); rep(i, h) rep(j, w) cin >> A[i][j]; rep(i, h) rep(j, w) cin >> B[i][j]; ll M = 20000; vector<vector<vector<bool>>> dp = make_v(h, w, M, false); ll x = abs(A[h - 1][w - 1] - B[h - 1][w - 1]); dp[h - 1][w - 1][x] = true; rep(i, h - 1, -1, -1) rep(j, w - 1, -1, -1) { ll x = abs(A[i][j] - B[i][j]); if (i + 1 < h) { rep(k, M) { if (dp[i + 1][j][k]) { dp[i][j][k + x] = true; dp[i][j][abs(k - x)] = true; } } } if (j + 1 < w) { rep(k, M) { if (dp[i][j + 1][k]) { dp[i][j][k + x] = true; dp[i][j][abs(k - x)] = true; } } } } rep(k, M) { if (dp[0][0][k]) { cout << k << "\n"; return; } } }
replace
47
48
47
48
0
p02839
C++
Runtime Error
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; // マクロ&定数&関数 ================================================ typedef unsigned int uint; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<double> vdouble; typedef vector<bool> vbool; typedef vector<string> vstring; typedef vector<pair<int, int>> vpint; typedef vector<pair<ll, ll>> vpll; typedef vector<pair<double, double>> vpdouble; typedef vector<vector<int>> vvint; typedef vector<vector<ll>> vvll; typedef vector<vpint> vvpint; typedef vector<vpll> vvpll; typedef vector<vector<double>> vvdouble; typedef vector<vector<string>> vvstring; typedef vector<vector<bool>> vvbool; typedef vector<vector<vector<ll>>> vvvll; const int c_YET = -1; const int INF = 1e9 + 1; const ll LLINF = 1e17 + 1; const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;4近傍 const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:8近傍 9:(0,0)を含む const ll MOD = 1000000007; // 10^9 + 7 const ll MAX = 1e9; const double PI = 3.14159265358979323846264338327950288; //--------------------------------------------------------------- // オーバーフローチェック //--------------------------------------------------------------- bool is_overflow(ll a, ll b) { return ((a * b) / b != a); } //--------------------------------------------------------------- // 約数列挙 //--------------------------------------------------------------- vll divisor(ll n) { vll ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } //--------------------------------------------------------------- // N以下のすべての素数を列挙する(エラトステネスの篩) //--------------------------------------------------------------- vbool searchSosuu(ll N) { vbool sosuu; // ←これをグローバル変数にして1度だけ実行する for (ll i = 0; i < N; i++) { sosuu.emplace_back(true); } sosuu[0] = false; sosuu[1] = false; for (ll i = 2; i < N; i++) { if (sosuu[i]) { for (ll j = 2; i * j < N; j++) { sosuu[i * j] = false; } } } return sosuu; } //--------------------------------------------------------------- // 素因数分解 O(√N) //--------------------------------------------------------------- vpll div_prime(ll n) { vpll prime_factor; for (ll i = 2; i * i <= n; i++) { ll count = 0; while (n % i == 0) { count++; n /= i; } if (count) { pair<ll, ll> temp = {i, count}; prime_factor.emplace_back(temp); } } if (n != 1) { pair<ll, ll> temp = {n, 1}; prime_factor.emplace_back(temp); } return prime_factor; } //--------------------------------------------------------------- // 素数判定 //--------------------------------------------------------------- bool is_sosuu(ll N) { if (N < 2) { return false; } else if (N == 2) { return true; } else if (N % 2 == 0) { return false; } for (ll i = 3; i <= sqrt(N); i += 2) { if (N % i == 0) { return false; } } return true; } //--------------------------------------------------------------- // 最大公約数(ユークリッドの互除法) //--------------------------------------------------------------- ll gcd(ll a, ll b) { if (a < b) { ll tmp = a; a = b; b = tmp; } ll r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } //--------------------------------------------------------------- // 最小公倍数 //--------------------------------------------------------------- ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp * (a / temp) * (b / temp); } //--------------------------------------------------------------- // 階乗 //--------------------------------------------------------------- ll factorial(ll n) { if (n <= 1) { return 1; } return (n * (factorial(n - 1))) % MOD; } //--------------------------------------------------------------- // 高速コンビネーション計算(前処理:O(N) 計算:O(1)) //--------------------------------------------------------------- // テーブルを作る前処理 ll comb_const = 200005; vll fac(comb_const), finv(comb_const), inv(comb_const); bool COMineted = false; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < comb_const; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } COMineted = true; } // 二項係数計算 ll COM(ll n, ll k) { if (COMineted == false) COMinit(); if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } //--------------------------------------------------------------- // 繰り返し2乗法 base^sisuu //--------------------------------------------------------------- ll RepeatSquaring(ll base, ll sisuu) { if (sisuu < 0) { cout << "RepeatSquaring: 指数が負です!" << endl; return 0; } if (sisuu == 0) return 1; if (sisuu % 2 == 0) { ll t = RepeatSquaring(base, sisuu / 2) % MOD; return (t * t) % MOD; } return base * RepeatSquaring(base, sisuu - 1) % MOD; } //--------------------------------------------------------------- // 高速単発コンビネーション計算(O(logN)) //--------------------------------------------------------------- ll fast_com(ll a, ll b) { ll bunshi = 1; ll bunbo = 1; for (ll i = 1; i <= b; i++) { bunbo *= i; bunbo %= MOD; bunshi *= (a - i + 1); bunshi %= MOD; } ll ret = bunshi * RepeatSquaring(bunbo, MOD - 2); ret %= MOD; while (ret < 0) { ret += MOD; } return ret; } //--------------------------------------------------------------- // 2直線の交差判定(直線(x1, y1)->(x2, y2) と 直線(X1, Y1)->(X2, Y2)) //--------------------------------------------------------------- bool is_cross(ll x1, ll y1, ll x2, ll y2, ll X1, ll Y1, ll X2, ll Y2) { ll dx_ai = X1 - x1; ll dy_ai = Y1 - y1; ll dx_bi = X1 - x2; ll dy_bi = Y1 - y2; ll dx_ai2 = X2 - x1; ll dy_ai2 = Y2 - y1; ll dx_bi2 = X2 - x2; ll dy_bi2 = Y2 - y2; ll si = dx_ai * dy_bi - dy_ai * dx_bi; ll si2 = dx_ai2 * dy_bi2 - dy_ai2 * dx_bi2; ll si3 = dx_ai * dy_ai2 - dy_ai * dx_ai2; ll si4 = dx_bi * dy_bi2 - dy_bi * dx_bi2; return (si * si2 < 0 && si3 * si4 < 0); } //--------------------------------------------------------------- // 最長増加部分列の長さ(O(NlogN)) //--------------------------------------------------------------- ll LSI(vll vec) { ll size = vec.size(); vll lsi(size + 1); // 長さjを作った時の右端の最小値 for (ll i = 0; i <= size; i++) { lsi[i] = LLINF; } lsi[0] = 0; lsi[1] = vec[0]; for (ll i = 1; i < size; i++) { // 初めてvec[i]の方が小さくなるところを探す auto Iter = lower_bound(lsi.begin(), lsi.end(), vec[i]); ll idx = Iter - lsi.begin(); if (idx > 0 && lsi[idx - 1] < vec[i]) { lsi[idx] = vec[i]; } } for (ll i = size; i >= 0; i--) { if (lsi[i] < LLINF) { return i; } } } //--------------------------------------------------------------- // 木の根からの深さ //--------------------------------------------------------------- vll tree_depth(vvll edge, ll start_node, ll n_node) { vll dist(n_node, LLINF); dist[start_node] = 0; stack<pll> S; S.push({start_node, 0}); while (!S.empty()) { ll node = S.top().first; ll d = S.top().second; dist[node] = d; S.pop(); for (int i = 0; i < edge[node].size(); i++) { if (dist[edge[node][i]] == LLINF) { S.push({edge[node][i], d + 1}); } } } return dist; } //--------------------------------------------------------------- // ワーシャルフロイド法(O(N^3)) 任意の2点間の最短距離 //--------------------------------------------------------------- vvll warshall_floyd(ll n, vvll d) { for (int k = 0; k < n; k++) { // 経由する頂点 for (int i = 0; i < n; i++) { // 始点 for (int j = 0; j < n; j++) { // 終点 d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } return d; } //--------------------------------------------------------------- // Union Find //--------------------------------------------------------------- /* UnionFind uf(要素の個数); for(int i = 0;i < 関係の個数; i++) { uf.merge(A[i], B[i]); } nを含む集合の大きさ = uf.size(n) nを含む集合の代表者 = uf.root(n) 集合の個数 = uf.n_group */ class UnionFind { public: vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) ll n_group; // 集合の数 // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 n_group = sz_; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; n_group--; return true; } bool issame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; //======================================================================== int main() { ////================================== cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(30); ////================================== ll H, W; cin >> H >> W; vvll D(H, vll(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> D[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ll a; cin >> a; D[i][j] = abs(D[i][j] - a); } } ll M = 5000; vector<vvbool> bias(H, vvbool(W, vbool(M, false))); bias[0][0][D[0][0]] = true; for (int h = 0; h < H; h++) { for (int w = 0; w < W; w++) { if (h + 1 < H) { ll nH = h + 1; ll nW = w; for (int i = 0; i < M; i++) { if (bias[h][w][i]) { bias[nH][nW][abs(i - D[nH][nW])] = true; bias[nH][nW][abs(i + D[nH][nW])] = true; } } } if (w + 1 < W) { ll nH = h; ll nW = w + 1; for (int i = 0; i < M; i++) { if (bias[h][w][i]) { bias[nH][nW][abs(i - D[nH][nW])] = true; bias[nH][nW][abs(i + D[nH][nW])] = true; } } } } } for (int i = 0; i < M; i++) { if (bias[H - 1][W - 1][i]) { cout << i << endl; return 0; } } }
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; // マクロ&定数&関数 ================================================ typedef unsigned int uint; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<double> vdouble; typedef vector<bool> vbool; typedef vector<string> vstring; typedef vector<pair<int, int>> vpint; typedef vector<pair<ll, ll>> vpll; typedef vector<pair<double, double>> vpdouble; typedef vector<vector<int>> vvint; typedef vector<vector<ll>> vvll; typedef vector<vpint> vvpint; typedef vector<vpll> vvpll; typedef vector<vector<double>> vvdouble; typedef vector<vector<string>> vvstring; typedef vector<vector<bool>> vvbool; typedef vector<vector<vector<ll>>> vvvll; const int c_YET = -1; const int INF = 1e9 + 1; const ll LLINF = 1e17 + 1; const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;4近傍 const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:8近傍 9:(0,0)を含む const ll MOD = 1000000007; // 10^9 + 7 const ll MAX = 1e9; const double PI = 3.14159265358979323846264338327950288; //--------------------------------------------------------------- // オーバーフローチェック //--------------------------------------------------------------- bool is_overflow(ll a, ll b) { return ((a * b) / b != a); } //--------------------------------------------------------------- // 約数列挙 //--------------------------------------------------------------- vll divisor(ll n) { vll ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } //--------------------------------------------------------------- // N以下のすべての素数を列挙する(エラトステネスの篩) //--------------------------------------------------------------- vbool searchSosuu(ll N) { vbool sosuu; // ←これをグローバル変数にして1度だけ実行する for (ll i = 0; i < N; i++) { sosuu.emplace_back(true); } sosuu[0] = false; sosuu[1] = false; for (ll i = 2; i < N; i++) { if (sosuu[i]) { for (ll j = 2; i * j < N; j++) { sosuu[i * j] = false; } } } return sosuu; } //--------------------------------------------------------------- // 素因数分解 O(√N) //--------------------------------------------------------------- vpll div_prime(ll n) { vpll prime_factor; for (ll i = 2; i * i <= n; i++) { ll count = 0; while (n % i == 0) { count++; n /= i; } if (count) { pair<ll, ll> temp = {i, count}; prime_factor.emplace_back(temp); } } if (n != 1) { pair<ll, ll> temp = {n, 1}; prime_factor.emplace_back(temp); } return prime_factor; } //--------------------------------------------------------------- // 素数判定 //--------------------------------------------------------------- bool is_sosuu(ll N) { if (N < 2) { return false; } else if (N == 2) { return true; } else if (N % 2 == 0) { return false; } for (ll i = 3; i <= sqrt(N); i += 2) { if (N % i == 0) { return false; } } return true; } //--------------------------------------------------------------- // 最大公約数(ユークリッドの互除法) //--------------------------------------------------------------- ll gcd(ll a, ll b) { if (a < b) { ll tmp = a; a = b; b = tmp; } ll r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } //--------------------------------------------------------------- // 最小公倍数 //--------------------------------------------------------------- ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp * (a / temp) * (b / temp); } //--------------------------------------------------------------- // 階乗 //--------------------------------------------------------------- ll factorial(ll n) { if (n <= 1) { return 1; } return (n * (factorial(n - 1))) % MOD; } //--------------------------------------------------------------- // 高速コンビネーション計算(前処理:O(N) 計算:O(1)) //--------------------------------------------------------------- // テーブルを作る前処理 ll comb_const = 200005; vll fac(comb_const), finv(comb_const), inv(comb_const); bool COMineted = false; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < comb_const; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } COMineted = true; } // 二項係数計算 ll COM(ll n, ll k) { if (COMineted == false) COMinit(); if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } //--------------------------------------------------------------- // 繰り返し2乗法 base^sisuu //--------------------------------------------------------------- ll RepeatSquaring(ll base, ll sisuu) { if (sisuu < 0) { cout << "RepeatSquaring: 指数が負です!" << endl; return 0; } if (sisuu == 0) return 1; if (sisuu % 2 == 0) { ll t = RepeatSquaring(base, sisuu / 2) % MOD; return (t * t) % MOD; } return base * RepeatSquaring(base, sisuu - 1) % MOD; } //--------------------------------------------------------------- // 高速単発コンビネーション計算(O(logN)) //--------------------------------------------------------------- ll fast_com(ll a, ll b) { ll bunshi = 1; ll bunbo = 1; for (ll i = 1; i <= b; i++) { bunbo *= i; bunbo %= MOD; bunshi *= (a - i + 1); bunshi %= MOD; } ll ret = bunshi * RepeatSquaring(bunbo, MOD - 2); ret %= MOD; while (ret < 0) { ret += MOD; } return ret; } //--------------------------------------------------------------- // 2直線の交差判定(直線(x1, y1)->(x2, y2) と 直線(X1, Y1)->(X2, Y2)) //--------------------------------------------------------------- bool is_cross(ll x1, ll y1, ll x2, ll y2, ll X1, ll Y1, ll X2, ll Y2) { ll dx_ai = X1 - x1; ll dy_ai = Y1 - y1; ll dx_bi = X1 - x2; ll dy_bi = Y1 - y2; ll dx_ai2 = X2 - x1; ll dy_ai2 = Y2 - y1; ll dx_bi2 = X2 - x2; ll dy_bi2 = Y2 - y2; ll si = dx_ai * dy_bi - dy_ai * dx_bi; ll si2 = dx_ai2 * dy_bi2 - dy_ai2 * dx_bi2; ll si3 = dx_ai * dy_ai2 - dy_ai * dx_ai2; ll si4 = dx_bi * dy_bi2 - dy_bi * dx_bi2; return (si * si2 < 0 && si3 * si4 < 0); } //--------------------------------------------------------------- // 最長増加部分列の長さ(O(NlogN)) //--------------------------------------------------------------- ll LSI(vll vec) { ll size = vec.size(); vll lsi(size + 1); // 長さjを作った時の右端の最小値 for (ll i = 0; i <= size; i++) { lsi[i] = LLINF; } lsi[0] = 0; lsi[1] = vec[0]; for (ll i = 1; i < size; i++) { // 初めてvec[i]の方が小さくなるところを探す auto Iter = lower_bound(lsi.begin(), lsi.end(), vec[i]); ll idx = Iter - lsi.begin(); if (idx > 0 && lsi[idx - 1] < vec[i]) { lsi[idx] = vec[i]; } } for (ll i = size; i >= 0; i--) { if (lsi[i] < LLINF) { return i; } } } //--------------------------------------------------------------- // 木の根からの深さ //--------------------------------------------------------------- vll tree_depth(vvll edge, ll start_node, ll n_node) { vll dist(n_node, LLINF); dist[start_node] = 0; stack<pll> S; S.push({start_node, 0}); while (!S.empty()) { ll node = S.top().first; ll d = S.top().second; dist[node] = d; S.pop(); for (int i = 0; i < edge[node].size(); i++) { if (dist[edge[node][i]] == LLINF) { S.push({edge[node][i], d + 1}); } } } return dist; } //--------------------------------------------------------------- // ワーシャルフロイド法(O(N^3)) 任意の2点間の最短距離 //--------------------------------------------------------------- vvll warshall_floyd(ll n, vvll d) { for (int k = 0; k < n; k++) { // 経由する頂点 for (int i = 0; i < n; i++) { // 始点 for (int j = 0; j < n; j++) { // 終点 d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } return d; } //--------------------------------------------------------------- // Union Find //--------------------------------------------------------------- /* UnionFind uf(要素の個数); for(int i = 0;i < 関係の個数; i++) { uf.merge(A[i], B[i]); } nを含む集合の大きさ = uf.size(n) nを含む集合の代表者 = uf.root(n) 集合の個数 = uf.n_group */ class UnionFind { public: vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) ll n_group; // 集合の数 // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 n_group = sz_; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; n_group--; return true; } bool issame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; //======================================================================== int main() { ////================================== cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(30); ////================================== ll H, W; cin >> H >> W; vvll D(H, vll(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> D[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ll a; cin >> a; D[i][j] = abs(D[i][j] - a); } } ll M = 20000; vector<vvbool> bias(H, vvbool(W, vbool(M, false))); bias[0][0][D[0][0]] = true; for (int h = 0; h < H; h++) { for (int w = 0; w < W; w++) { if (h + 1 < H) { ll nH = h + 1; ll nW = w; for (int i = 0; i < M; i++) { if (bias[h][w][i]) { bias[nH][nW][abs(i - D[nH][nW])] = true; bias[nH][nW][abs(i + D[nH][nW])] = true; } } } if (w + 1 < W) { ll nH = h; ll nW = w + 1; for (int i = 0; i < M; i++) { if (bias[h][w][i]) { bias[nH][nW][abs(i - D[nH][nW])] = true; bias[nH][nW][abs(i + D[nH][nW])] = true; } } } } } for (int i = 0; i < M; i++) { if (bias[H - 1][W - 1][i]) { cout << i << endl; return 0; } } }
replace
441
442
441
442
0
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int(i) = 0; i < (n); ++(i)) typedef long long ll; const ll MOD = 1e9 + 7; int main() { int H, W; std::cin >> H >> W; static std::vector<std::vector<int>> A(H, std::vector<int>(W)); static std::vector<std::vector<int>> B(H, std::vector<int>(W)); rep(i, H) { rep(j, W) { std::cin >> A[i][j]; } } rep(i, H) { rep(j, W) { std::cin >> B[i][j]; } } int M = 80 * (H + W); std::vector<std::vector<std::map<int, bool>>> dp( H, std::vector<std::map<int, bool>>(W)); dp[0][0][std::abs(A[0][0] - B[0][0])] = true; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { for (int k = 0; k <= M; ++k) { if (!dp[i][j][k]) continue; if (i + 1 < H) { int d = std::abs(A[i + 1][j] - B[i + 1][j]); dp[i + 1][j][k + d] = dp[i + 1][j][std::abs(k - d)] = true; } if (j + 1 < W) { int d = std::abs(A[i][j + 1] - B[i][j + 1]); dp[i][j + 1][k + d] = dp[i][j + 1][std::abs(k - d)] = true; } } } } int ans; for (int k = 0; k <= M; ++k) { if (dp[H - 1][W - 1][k]) { ans = k; break; } } std::cout << ans << '\n'; }
#include <bits/stdc++.h> #define rep(i, n) for (int(i) = 0; i < (n); ++(i)) typedef long long ll; const ll MOD = 1e9 + 7; int main() { int H, W; std::cin >> H >> W; static std::vector<std::vector<int>> A(H, std::vector<int>(W)); static std::vector<std::vector<int>> B(H, std::vector<int>(W)); rep(i, H) { rep(j, W) { std::cin >> A[i][j]; } } rep(i, H) { rep(j, W) { std::cin >> B[i][j]; } } int M = 80 * (H + W); std::vector<std::vector<std::vector<bool>>> dp( H, std::vector<std::vector<bool>>(W)); rep(i, H) { rep(j, W) { dp[i][j] = std::vector<bool>(M + 1); } } dp[0][0][std::abs(A[0][0] - B[0][0])] = true; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { for (int k = 0; k <= M; ++k) { if (!dp[i][j][k]) continue; if (i + 1 < H) { int d = std::abs(A[i + 1][j] - B[i + 1][j]); dp[i + 1][j][k + d] = dp[i + 1][j][std::abs(k - d)] = true; } if (j + 1 < W) { int d = std::abs(A[i][j + 1] - B[i][j + 1]); dp[i][j + 1][k + d] = dp[i][j + 1][std::abs(k - d)] = true; } } } } int ans; for (int k = 0; k <= M; ++k) { if (dp[H - 1][W - 1][k]) { ans = k; break; } } std::cout << ans << '\n'; }
replace
21
23
21
26
TLE
p02839
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define fi first #define se second typedef pair<ll, ll> P; using VP = vector<P>; using VVP = vector<VP>; using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>; const int inf = 1e9 + 7; const ll INF = 1LL << 58; const ll mod = 1e9 + 7; int main() { int i, j, k; int h, w; cin >> h >> w; int a[h][w]; int b[h][w]; int c[h][w]; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> a[i][j]; } } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> b[i][j]; } } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { c[i][j] = b[i][j] - a[i][j]; // cout<<c[i][j]; } // cout<<endl; } int r = 80 * 162; VVVI v(h, VVI(w, VI(2 * r, 0))); v[0][0][r + c[0][0]] = 1; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (i >= 1) { for (k = 0; k < 2 * r; k++) { if (k - c[i][j] >= 0 && k - c[i][j] < 2 * r && v[i - 1][j][k - c[i][j]] == 1) { v[i][j][k] = 1; } } for (k = 0; k < 2 * r; k++) { if (k + c[i][j] >= 0 && k + c[i][j] < 2 * r && v[i - 1][j][k + c[i][j]] == 1) { v[i][j][k] = 1; } } } if (j >= 1) { for (k = 0; k < 2 * r; k++) { if (k - c[i][j] >= 0 && k - c[i][j] < 2 * r && v[i][j - 1][k - c[i][j]] == 1) { v[i][j][k] = 1; } } for (k = 0; k < 2 * r; k++) { if (k + c[i][j] >= 0 && k + c[i][j] < 2 * r && v[i][j - 1][k + c[i][j]] == 1) { v[i][j][k] = 1; } } } } } int ans = inf; for (i = 0; i < 2 * r; i++) { if (v[h - 1][w - 1][i] == 1) ans = min(ans, abs(i - r)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define fi first #define se second typedef pair<ll, ll> P; using VP = vector<P>; using VVP = vector<VP>; using VI = vector<bool>; using VVI = vector<VI>; using VVVI = vector<VVI>; const int inf = 1e9 + 7; const ll INF = 1LL << 58; const ll mod = 1e9 + 7; int main() { int i, j, k; int h, w; cin >> h >> w; int a[h][w]; int b[h][w]; int c[h][w]; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> a[i][j]; } } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> b[i][j]; } } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { c[i][j] = b[i][j] - a[i][j]; // cout<<c[i][j]; } // cout<<endl; } int r = 80 * 162; VVVI v(h, VVI(w, VI(2 * r, 0))); v[0][0][r + c[0][0]] = 1; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (i >= 1) { for (k = 0; k < 2 * r; k++) { if (k - c[i][j] >= 0 && k - c[i][j] < 2 * r && v[i - 1][j][k - c[i][j]] == 1) { v[i][j][k] = 1; } } for (k = 0; k < 2 * r; k++) { if (k + c[i][j] >= 0 && k + c[i][j] < 2 * r && v[i - 1][j][k + c[i][j]] == 1) { v[i][j][k] = 1; } } } if (j >= 1) { for (k = 0; k < 2 * r; k++) { if (k - c[i][j] >= 0 && k - c[i][j] < 2 * r && v[i][j - 1][k - c[i][j]] == 1) { v[i][j][k] = 1; } } for (k = 0; k < 2 * r; k++) { if (k + c[i][j] >= 0 && k + c[i][j] < 2 * r && v[i][j - 1][k + c[i][j]] == 1) { v[i][j][k] = 1; } } } } } int ans = inf; for (i = 0; i < 2 * r; i++) { if (v[h - 1][w - 1][i] == 1) ans = min(ans, abs(i - r)); } cout << ans << endl; return 0; }
replace
9
10
9
10
MLE
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { long long h, w; cin >> h >> w; vector<vector<long long>> a(h + 1, vector<long long>(w + 1, 0)); vector<vector<long long>> b(h + 1, vector<long long>(w + 1, 0)); vector<vector<long long>> sa(h + 1, vector<long long>(w + 1, 0)); for (int i = 1; i < h + 1; i++) { for (int j = 1; j < w + 1; j++) { cin >> a.at(i).at(j); } } for (int i = 1; i < h + 1; i++) { for (int j = 1; j < w + 1; j++) { cin >> b.at(i).at(j); sa.at(i).at(j) = a.at(i).at(j) - b.at(i).at(j); if (sa.at(i).at(j) < 0) sa.at(i).at(j) *= -1; } } long long k = 80 * 80 * 5; vector<vector<vector<int>>> dp(h + 1, vector<vector<int>>(w + 1, vector<int>(k, 0))); for (int i = 1; i < h + 1; i++) { for (int j = 1; j < w + 1; j++) { for (long long l = 0; l < k - 80; l++) { if (i == 1 & j == 1) { dp.at(i).at(j).at(sa.at(i).at(j)) = 1; continue; } long long kpi, kpj, kmi, kmj; kpi = l + sa.at(i).at(j); kpj = l + sa.at(i).at(j); kmi = l - sa.at(i).at(j); kmj = l - sa.at(i).at(j); if (kmi < 0) kmi *= -1; if (kmj < 0) kmj *= -1; if (dp.at(i - 1).at(j).at(kpi) == 1 | dp.at(i - 1).at(j).at(kmi) == 1 | dp.at(i).at(j - 1).at(kpi) == 1 | dp.at(i).at(j - 1).at(kmi) == 1) { dp.at(i).at(j).at(l) = 1; } } } } for (int i = 0; i < k; i++) { if (dp.at(h).at(w).at(i) == 1) { cout << i; return 0; } } }
#include <bits/stdc++.h> using namespace std; int main() { long long h, w; cin >> h >> w; vector<vector<long long>> a(h + 1, vector<long long>(w + 1, 0)); vector<vector<long long>> b(h + 1, vector<long long>(w + 1, 0)); vector<vector<long long>> sa(h + 1, vector<long long>(w + 1, 0)); for (int i = 1; i < h + 1; i++) { for (int j = 1; j < w + 1; j++) { cin >> a.at(i).at(j); } } for (int i = 1; i < h + 1; i++) { for (int j = 1; j < w + 1; j++) { cin >> b.at(i).at(j); sa.at(i).at(j) = a.at(i).at(j) - b.at(i).at(j); if (sa.at(i).at(j) < 0) sa.at(i).at(j) *= -1; } } long long k = 80 * 80 * 3; vector<vector<vector<int>>> dp(h + 1, vector<vector<int>>(w + 1, vector<int>(k, 0))); for (int i = 1; i < h + 1; i++) { for (int j = 1; j < w + 1; j++) { for (long long l = 0; l < k - 80; l++) { if (i == 1 & j == 1) { dp.at(i).at(j).at(sa.at(i).at(j)) = 1; continue; } long long kpi, kpj, kmi, kmj; kpi = l + sa.at(i).at(j); kpj = l + sa.at(i).at(j); kmi = l - sa.at(i).at(j); kmj = l - sa.at(i).at(j); if (kmi < 0) kmi *= -1; if (kmj < 0) kmj *= -1; if (dp.at(i - 1).at(j).at(kpi) == 1 | dp.at(i - 1).at(j).at(kmi) == 1 | dp.at(i).at(j - 1).at(kpi) == 1 | dp.at(i).at(j - 1).at(kmi) == 1) { dp.at(i).at(j).at(l) = 1; } } } } for (int i = 0; i < k; i++) { if (dp.at(h).at(w).at(i) == 1) { cout << i; return 0; } } }
replace
22
23
22
23
TLE
p02839
C++
Runtime Error
#include <algorithm> #include <cinttypes> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define FSCNd64 "%" SCNd64 #define FPRId64 "%" PRId64 using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vll = vector<ll>; using vvll = vector<vll>; using vpii = vector<pii>; #define PI 3.1415926535897932384626433832795 template <typename X> bool max_u(X &m, X v) { if (m < v) { m = v; return true; } return false; } template <typename X> bool min_u(X &m, X v) { if (m > v) { m = v; return true; } return false; } int dif_pii(pii &p) { return abs(p.first - p.second); } struct solve { int w, h; solve(int w, int h) : w(w), h(h) {} ll operator()(vector<vpii> &f) { int d_max = 0; int x, y; for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { max_u(d_max, dif_pii(f[y][x])); } } int s_max = d_max * (w + h); vector<vector<vector<bool>>> dp(h); for (auto &m : dp) { m.resize(w); for (auto &mm : m) { mm.assign(s_max, false); } } dp[0][0][dif_pii(f[0][0])] = true; for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { int dx = (x + 1 < w) ? dif_pii(f[y][x + 1]) : 0; int dy = (y + 1 < h) ? dif_pii(f[y + 1][x]) : 0; for (int v = 0; v < s_max; v++) { if (!dp[y][x][v]) continue; if (x + 1 < w) { if (abs(v + dx) < s_max) dp[y][x + 1][abs(v + dx)] = true; dp[y][x + 1][abs(v - dx)] = true; } if (y + 1 < h) { if (abs(v + dy) < s_max) dp[y + 1][x][abs(v + dy)] = true; dp[y + 1][x][abs(v - dy)] = true; } } } } for (int v = 0; v < s_max; v++) { if (dp[h - 1][w - 1][v]) return v; } return -1; } }; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); for (;;) { int h, w; cin >> h >> w; if (cin.fail()) break; vector<vpii> f(h); for (auto &m : f) m.resize(w); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> f[y][x].first; } } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> f[y][x].second; } } cout << solve(w, h)(f) << "\n"; } return 0; }
#include <algorithm> #include <cinttypes> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define FSCNd64 "%" SCNd64 #define FPRId64 "%" PRId64 using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vll = vector<ll>; using vvll = vector<vll>; using vpii = vector<pii>; #define PI 3.1415926535897932384626433832795 template <typename X> bool max_u(X &m, X v) { if (m < v) { m = v; return true; } return false; } template <typename X> bool min_u(X &m, X v) { if (m > v) { m = v; return true; } return false; } int dif_pii(pii &p) { return abs(p.first - p.second); } struct solve { int w, h; solve(int w, int h) : w(w), h(h) {} ll operator()(vector<vpii> &f) { int d_max = 0; int x, y; for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { max_u(d_max, dif_pii(f[y][x])); } } int s_max = d_max * (w + h) + 1; vector<vector<vector<bool>>> dp(h); for (auto &m : dp) { m.resize(w); for (auto &mm : m) { mm.assign(s_max, false); } } dp[0][0][dif_pii(f[0][0])] = true; for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { int dx = (x + 1 < w) ? dif_pii(f[y][x + 1]) : 0; int dy = (y + 1 < h) ? dif_pii(f[y + 1][x]) : 0; for (int v = 0; v < s_max; v++) { if (!dp[y][x][v]) continue; if (x + 1 < w) { if (abs(v + dx) < s_max) dp[y][x + 1][abs(v + dx)] = true; dp[y][x + 1][abs(v - dx)] = true; } if (y + 1 < h) { if (abs(v + dy) < s_max) dp[y + 1][x][abs(v + dy)] = true; dp[y + 1][x][abs(v - dy)] = true; } } } } for (int v = 0; v < s_max; v++) { if (dp[h - 1][w - 1][v]) return v; } return -1; } }; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); for (;;) { int h, w; cin >> h >> w; if (cin.fail()) break; vector<vpii> f(h); for (auto &m : f) m.resize(w); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> f[y][x].first; } } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> f[y][x].second; } } cout << solve(w, h)(f) << "\n"; } return 0; }
replace
59
60
59
60
0
p02839
C++
Runtime Error
#include <algorithm> #include <bitset> #include <iostream> #include <string> #include <vector> using namespace std; int H, W; vector<vector<int>> A, B, C; using mybit = bitset<12800>; int main() { int ans = 0; cin >> H; cin >> W; A = B = C = vector<vector<int>>(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; C[i][j] = abs(A[i][j] - B[i][j]); } } auto sum_list = vector<vector<mybit>>(H, vector<mybit>(W)); sum_list[0][0].set(C[0][0]); for (int i = 1; i < H + W; i++) { for (int j = max(i - H, 0); j <= min(i, H - 1); j++) { int x = i - j; int y = j; if (x > 0) { mybit left = sum_list[y][x - 1] << C[y][x]; mybit right = sum_list[y][x - 1] >> C[y][x]; string os = sum_list[y][x - 1].to_string(); reverse(os.begin(), os.end()); os.erase(C[y][x]); mybit over(os); over <<= 1; sum_list[y][x] |= left | right | over; } if (y > 0) { mybit left = sum_list[y - 1][x] << C[y][x]; mybit right = sum_list[y - 1][x] >> C[y][x]; string os = sum_list[y - 1][x].to_string(); reverse(os.begin(), os.end()); os.erase(C[y][x]); mybit over(os); over <<= 1; sum_list[y][x] |= left | right | over; } } } while (!sum_list[H - 1][W - 1].test(ans)) { ans++; } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <iostream> #include <string> #include <vector> using namespace std; int H, W; vector<vector<int>> A, B, C; using mybit = bitset<12800>; int main() { int ans = 0; cin >> H; cin >> W; A = B = C = vector<vector<int>>(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> B[i][j]; C[i][j] = abs(A[i][j] - B[i][j]); } } auto sum_list = vector<vector<mybit>>(H, vector<mybit>(W)); sum_list[0][0].set(C[0][0]); for (int i = 1; i < H + W; i++) { for (int x = min(i, W - 1); x >= 0; x--) { int y = i - x; if (y >= H) { break; } if (x > 0) { mybit left = sum_list[y][x - 1] << C[y][x]; mybit right = sum_list[y][x - 1] >> C[y][x]; string os = sum_list[y][x - 1].to_string(); reverse(os.begin(), os.end()); os.erase(C[y][x]); mybit over(os); over <<= 1; sum_list[y][x] |= left | right | over; } if (y > 0) { mybit left = sum_list[y - 1][x] << C[y][x]; mybit right = sum_list[y - 1][x] >> C[y][x]; string os = sum_list[y - 1][x].to_string(); reverse(os.begin(), os.end()); os.erase(C[y][x]); mybit over(os); over <<= 1; sum_list[y][x] |= left | right | over; } } } while (!sum_list[H - 1][W - 1].test(ans)) { ans++; } cout << ans << endl; return 0; }
replace
35
38
35
40
-6
double free or corruption (!prev)
p02839
C++
Runtime Error
// https://youtu.be/telPDj1VQjQ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef long long unsigned llu; typedef pair<long long, long long> pll; const long long inf = 2000000000000000000LL; // 2e18 #define pi acos(-1.0) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define shesh "\n" #define ff first #define ss second #define pb push_back #define bp pop_back #define pf push_front #define fp pop_front #define ub upper_bound #define lb lower_bound #define all(x) x.begin(), x.end() #define debug(x) cout << "debug " << x << "\n" #define mest(a, b) memset(a, b, sizeof(a)) int main() { // fast; ll t, n, m, i, j, k, temp, flag, ans, h, w; // cin>>h>>w; scanf("%lld%lld", &h, &w); ll a[h][w]; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { // cin>>a[i][j]; scanf("%lld", &a[i][j]); } } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { // cin>>b[i][j]; scanf("%lld", &temp); a[i][j] = abs(a[i][j] - temp); } } vector<ll> s[h][w]; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (i + j == 0) { s[i][j].pb(a[i][j]); // s[i][j].insert(-1*c[i][j]); continue; } // cout<<i<<' '<<j<<' '; bool b[10010] = {}; if (i - 1 >= 0) { for (auto it = s[i - 1][j].begin(); it != s[i - 1][j].end(); it++) { temp = *it; temp -= a[i][j]; temp = abs(temp); // cout<<temp<<' '; if (!b[temp]) { s[i][j].pb(temp); b[temp] = true; } // s[i][j].insert(temp); temp = *it; temp += a[i][j]; // cout<<temp<<' '; if (!b[temp]) { s[i][j].pb(temp); b[temp] = true; } // s[i][j].insert(temp); } } if (j - 1 >= 0) { for (auto it = s[i][j - 1].begin(); it != s[i][j - 1].end(); it++) { temp = *it; temp -= a[i][j]; temp = abs(temp); // cout<<temp<<' '; if (!b[temp]) { s[i][j].pb(temp); b[temp] = true; } // s[i][j].insert(temp); temp = *it; temp += a[i][j]; // cout<<temp<<' '; if (!b[temp]) { s[i][j].pb(temp); b[temp] = true; } // s[i][j].insert(temp); } } // cout<<endl; } } temp = (80 * 80) + 1; for (auto it = s[h - 1][w - 1].begin(); it != s[h - 1][w - 1].end(); it++) temp = min(temp, abs(*it)); printf("%lld", temp); return 0; }
// https://youtu.be/telPDj1VQjQ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef long long unsigned llu; typedef pair<long long, long long> pll; const long long inf = 2000000000000000000LL; // 2e18 #define pi acos(-1.0) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define shesh "\n" #define ff first #define ss second #define pb push_back #define bp pop_back #define pf push_front #define fp pop_front #define ub upper_bound #define lb lower_bound #define all(x) x.begin(), x.end() #define debug(x) cout << "debug " << x << "\n" #define mest(a, b) memset(a, b, sizeof(a)) int main() { // fast; ll t, n, m, i, j, k, temp, flag, ans, h, w; // cin>>h>>w; scanf("%lld%lld", &h, &w); ll a[h][w]; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { // cin>>a[i][j]; scanf("%lld", &a[i][j]); } } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { // cin>>b[i][j]; scanf("%lld", &temp); a[i][j] = abs(a[i][j] - temp); } } vector<ll> s[h][w]; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (i + j == 0) { s[i][j].pb(a[i][j]); // s[i][j].insert(-1*c[i][j]); continue; } // cout<<i<<' '<<j<<' '; bool b[20000] = {}; if (i - 1 >= 0) { for (auto it = s[i - 1][j].begin(); it != s[i - 1][j].end(); it++) { temp = *it; temp -= a[i][j]; temp = abs(temp); // cout<<temp<<' '; if (!b[temp]) { s[i][j].pb(temp); b[temp] = true; } // s[i][j].insert(temp); temp = *it; temp += a[i][j]; // cout<<temp<<' '; if (!b[temp]) { s[i][j].pb(temp); b[temp] = true; } // s[i][j].insert(temp); } } if (j - 1 >= 0) { for (auto it = s[i][j - 1].begin(); it != s[i][j - 1].end(); it++) { temp = *it; temp -= a[i][j]; temp = abs(temp); // cout<<temp<<' '; if (!b[temp]) { s[i][j].pb(temp); b[temp] = true; } // s[i][j].insert(temp); temp = *it; temp += a[i][j]; // cout<<temp<<' '; if (!b[temp]) { s[i][j].pb(temp); b[temp] = true; } // s[i][j].insert(temp); } } // cout<<endl; } } temp = (80 * 80) + 1; for (auto it = s[h - 1][w - 1].begin(); it != s[h - 1][w - 1].end(); it++) temp = min(temp, abs(*it)); printf("%lld", temp); return 0; }
replace
56
57
56
57
0
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef pair<int, int> P; typedef long long ll; bool dp[81][81][80 * 160 + 200]; int a[80][80]; int b[80][80]; int main() { int h, w; cin >> h >> w; rep(i, h) { rep(j, w) { cin >> a[i][j]; } } rep(i, h) { rep(j, w) { cin >> b[i][j]; } } dp[0][0][abs(a[0][0] - b[0][0])] = true; rep(i, h) { rep(j, w) { rep(k, 80 * 80 * 80) { if (dp[i][j][k] == false) { continue; } if (j == w - 1 && i == h - 1) { continue; } else if (j == w - 1) { dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = true; dp[i + 1][j][abs(k - abs(a[i + 1][j] - b[i + 1][j]))] = true; } else if (i == h - 1) { dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = true; dp[i][j + 1][abs(k - abs(a[i][j + 1] - b[i][j + 1]))] = true; } else { dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = true; dp[i + 1][j][abs(k - abs(a[i + 1][j] - b[i + 1][j]))] = true; dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = true; dp[i][j + 1][abs(k - abs(a[i][j + 1] - b[i][j + 1]))] = true; } } } } for (int i = 0; i < 80 * 160 + 200; i++) { if (dp[h - 1][w - 1][i]) { cout << i << endl; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef pair<int, int> P; typedef long long ll; bool dp[81][81][80 * 160 + 200]; int a[80][80]; int b[80][80]; int main() { int h, w; cin >> h >> w; rep(i, h) { rep(j, w) { cin >> a[i][j]; } } rep(i, h) { rep(j, w) { cin >> b[i][j]; } } dp[0][0][abs(a[0][0] - b[0][0])] = true; rep(i, h) { rep(j, w) { rep(k, 80 * 160 + 1) { if (dp[i][j][k] == false) { continue; } if (j == w - 1 && i == h - 1) { continue; } else if (j == w - 1) { dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = true; dp[i + 1][j][abs(k - abs(a[i + 1][j] - b[i + 1][j]))] = true; } else if (i == h - 1) { dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = true; dp[i][j + 1][abs(k - abs(a[i][j + 1] - b[i][j + 1]))] = true; } else { dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = true; dp[i + 1][j][abs(k - abs(a[i + 1][j] - b[i + 1][j]))] = true; dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = true; dp[i][j + 1][abs(k - abs(a[i][j + 1] - b[i][j + 1]))] = true; } } } } for (int i = 0; i < 80 * 160 + 200; i++) { if (dp[h - 1][w - 1][i]) { cout << i << endl; return 0; } } return 0; }
replace
20
21
20
21
TLE
p02839
C++
Runtime Error
#include <iostream> using namespace std; bool d[81][81][12900]; int main() { int h, w; cin >> h >> w; int a[81][81], b[81][81]; int c[81][81]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> a[i][j]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> b[i][j]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) c[i][j] = abs(a[i][j] - b[i][j]); d[0][0][c[0][0]] = true; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { for (int k = 0; k < 12800; k++) { if (d[i][j][k]) { d[i + 1][j][k + c[i + 1][j]] = true; d[i + 1][j][abs(k - c[i + 1][j])] = true; d[i][j + 1][k + c[i][j + 1]] = true; d[i][j + 1][abs(k - c[i][j + 1])] = true; } } } } for (int i = 0; i < 12800; i++) { if (d[h - 1][w - 1][i]) { cout << i << endl; return 0; } } }
#include <iostream> using namespace std; bool d[81][81][12900]; int main() { int h, w; cin >> h >> w; int a[81][81], b[81][81]; int c[81][81]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> a[i][j]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> b[i][j]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) c[i][j] = abs(a[i][j] - b[i][j]); d[0][0][c[0][0]] = true; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { for (int k = 0; k < 12800; k++) { if (d[i][j][k]) { if (i < h - 1) { d[i + 1][j][k + c[i + 1][j]] = true; d[i + 1][j][abs(k - c[i + 1][j])] = true; } if (j < w - 1) { d[i][j + 1][k + c[i][j + 1]] = true; d[i][j + 1][abs(k - c[i][j + 1])] = true; } } } } } for (int i = 0; i < 12800; i++) { if (d[h - 1][w - 1][i]) { cout << i << endl; return 0; } } }
replace
24
28
24
32
0
p02839
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <ios> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define rep(i, N) for (int(i) = 0; (i) < (N); (i)++) #define revrep(i, N) for (int(i) = (N)-1; (i) >= 0; (i)--) #define dump(vec) \ for (int i = 0; i < (vec).size(); i++) { \ cout << vec.at(i) << ","; \ } \ cout << endl typedef long long ll; int main(void) { int w, h; cin >> h >> w; const ll max = w * h * 80; vector<vector<vector<bool>>> dp( h, vector<vector<bool>>(w, vector<bool>(max, false))); vector<vector<int>> A(h, vector<int>(w, 0)); vector<vector<int>> B(h, vector<int>(w, 0)); rep(i, h) { rep(j, w) { int a; cin >> a; A.at(i).at(j) = a; } } rep(i, h) { rep(j, w) { int b; cin >> b; B.at(i).at(j) = b; } } rep(i, h) { rep(j, w) { int diff = abs(A.at(i).at(j) - B.at(i).at(j)); if (i == 0 && j == 0) { dp.at(0).at(0).at(diff) = true; } if (i > 0) { rep(k, max) { if (!dp.at(i - 1).at(j).at(k)) continue; dp.at(i).at(j).at(abs(k - diff)) = true; dp.at(i).at(j).at(abs(k + diff)) = true; } } if (j > 0) { rep(k, max) { if (!dp.at(i).at(j - 1).at(k)) continue; dp.at(i).at(j).at(abs(k - diff)) = true; dp.at(i).at(j).at(abs(k + diff)) = true; } } // cout << "(" << i << "," << j << ")"; // rep(k,10){ // cout << dp.at(i).at(j).at(k) << ","; // }cout << endl; } } rep(k, max) { if (!dp.at(h - 1).at(w - 1).at(k)) continue; cout << k << endl; break; } return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <ios> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; #define rep(i, N) for (int(i) = 0; (i) < (N); (i)++) #define revrep(i, N) for (int(i) = (N)-1; (i) >= 0; (i)--) #define dump(vec) \ for (int i = 0; i < (vec).size(); i++) { \ cout << vec.at(i) << ","; \ } \ cout << endl typedef long long ll; int main(void) { int w, h; cin >> h >> w; const ll max = (w + h) * 80; vector<vector<vector<bool>>> dp( h, vector<vector<bool>>(w, vector<bool>(max, false))); vector<vector<int>> A(h, vector<int>(w, 0)); vector<vector<int>> B(h, vector<int>(w, 0)); rep(i, h) { rep(j, w) { int a; cin >> a; A.at(i).at(j) = a; } } rep(i, h) { rep(j, w) { int b; cin >> b; B.at(i).at(j) = b; } } rep(i, h) { rep(j, w) { int diff = abs(A.at(i).at(j) - B.at(i).at(j)); if (i == 0 && j == 0) { dp.at(0).at(0).at(diff) = true; } if (i > 0) { rep(k, max) { if (!dp.at(i - 1).at(j).at(k)) continue; dp.at(i).at(j).at(abs(k - diff)) = true; dp.at(i).at(j).at(abs(k + diff)) = true; } } if (j > 0) { rep(k, max) { if (!dp.at(i).at(j - 1).at(k)) continue; dp.at(i).at(j).at(abs(k - diff)) = true; dp.at(i).at(j).at(abs(k + diff)) = true; } } // cout << "(" << i << "," << j << ")"; // rep(k,10){ // cout << dp.at(i).at(j).at(k) << ","; // }cout << endl; } } rep(k, max) { if (!dp.at(h - 1).at(w - 1).at(k)) continue; cout << k << endl; break; } return 0; }
replace
27
28
27
28
TLE
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int dp[81][81][6400 * 2 + 2]; int find_it(int dif_sum, int i, int j, int &n, int &m, vector<vector<pair<int, int>>> &matrix) { if (i == n - 1 && j == m - 1) { return min(abs(dif_sum + matrix[i][j].first - matrix[i][j].second), abs(dif_sum + matrix[i][j].second - matrix[i][j].first)); } if (dp[i][j][dif_sum] != -1) { return dp[i][j][dif_sum + 6401]; } int minvalue = INT_MAX; int ff = dif_sum + matrix[i][j].first - matrix[i][j].second; int ss = dif_sum + matrix[i][j].second - matrix[i][j].first; if (i + 1 < n) { minvalue = min(minvalue, find_it(ff, i + 1, j, n, m, matrix)); minvalue = min(minvalue, find_it(ss, i + 1, j, n, m, matrix)); } if (j + 1 < m) { minvalue = min(minvalue, find_it(ff, i, j + 1, n, m, matrix)); minvalue = min(minvalue, find_it(ss, i, j + 1, n, m, matrix)); } return dp[i][j][dif_sum + 6401] = minvalue; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<pair<int, int>>> matrix(n, vector<pair<int, int>>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j].first; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j].second; } } memset(dp, -1, sizeof(dp)); cout << find_it(0, 0, 0, n, m, matrix); return 0; }
#include <bits/stdc++.h> using namespace std; int dp[81][81][6400 * 2 + 2]; int find_it(int dif_sum, int i, int j, int &n, int &m, vector<vector<pair<int, int>>> &matrix) { if (i == n - 1 && j == m - 1) { return min(abs(dif_sum + matrix[i][j].first - matrix[i][j].second), abs(dif_sum + matrix[i][j].second - matrix[i][j].first)); } if (dp[i][j][dif_sum + 6401] != -1) { return dp[i][j][dif_sum + 6401]; } int minvalue = INT_MAX; int ff = dif_sum + matrix[i][j].first - matrix[i][j].second; int ss = dif_sum + matrix[i][j].second - matrix[i][j].first; if (i + 1 < n) { minvalue = min(minvalue, find_it(ff, i + 1, j, n, m, matrix)); minvalue = min(minvalue, find_it(ss, i + 1, j, n, m, matrix)); } if (j + 1 < m) { minvalue = min(minvalue, find_it(ff, i, j + 1, n, m, matrix)); minvalue = min(minvalue, find_it(ss, i, j + 1, n, m, matrix)); } return dp[i][j][dif_sum + 6401] = minvalue; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<pair<int, int>>> matrix(n, vector<pair<int, int>>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j].first; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j].second; } } memset(dp, -1, sizeof(dp)); cout << find_it(0, 0, 0, n, m, matrix); return 0; }
replace
9
10
9
10
TLE
p02839
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long LL; typedef pair<LL, LL> P; const LL mod = 1000000007; const LL LINF = 1LL << 60; const int INF = 1 << 30; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int h, w; cin >> h >> w; vector<vector<int>> a(h, vector<int>(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int b; cin >> b; a[i][j] = abs(a[i][j] - b); } } bitset<30000> bi[h][w]; bi[0][0][15000] = 1; for (int k = 0; k <= h + w - 2; k++) { for (int i = max(0, k - h + 1); i <= min(w - 1, k); i++) { int x = i, y = k - i; bi[y + 1][x] = bi[y + 1][x] | (bi[y][x] >> a[y][x]); bi[y + 1][x] = bi[y + 1][x] | (bi[y][x] << a[y][x]); bi[y][x + 1] = bi[y][x + 1] | (bi[y][x] >> a[y][x]); bi[y][x + 1] = bi[y][x + 1] | (bi[y][x] << a[y][x]); } } int ans = INF; for (int i = 0; i < 30000; i++) { if (bi[h][w - 1][i]) ans = min(ans, abs(15000 - i)); } for (int i = 0; i < 30000; i++) { if (bi[h - 1][w][i]) ans = min(ans, abs(15000 - i)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long LL; typedef pair<LL, LL> P; const LL mod = 1000000007; const LL LINF = 1LL << 60; const int INF = 1 << 30; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int h, w; cin >> h >> w; vector<vector<int>> a(h, vector<int>(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int b; cin >> b; a[i][j] = abs(a[i][j] - b); } } bitset<30000> bi[h + 1][w + 1]; bi[0][0][15000] = 1; for (int k = 0; k <= h + w - 2; k++) { for (int i = max(0, k - h + 1); i <= min(w - 1, k); i++) { int x = i, y = k - i; bi[y + 1][x] = bi[y + 1][x] | (bi[y][x] >> a[y][x]); bi[y + 1][x] = bi[y + 1][x] | (bi[y][x] << a[y][x]); bi[y][x + 1] = bi[y][x + 1] | (bi[y][x] >> a[y][x]); bi[y][x + 1] = bi[y][x + 1] | (bi[y][x] << a[y][x]); } } int ans = INF; for (int i = 0; i < 30000; i++) { if (bi[h][w - 1][i]) ans = min(ans, abs(15000 - i)); } for (int i = 0; i < 30000; i++) { if (bi[h - 1][w][i]) ans = min(ans, abs(15000 - i)); } cout << ans << endl; return 0; }
replace
33
34
33
34
0
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,sse4.1,sse4.2,popcnt,abm,mmx,avx") #pragma comment(linker, "/STACK:102400000,102400000") #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma GCC optimize("-fpeephole2") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-fsched-spec") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-falign-jumps") #pragma GCC optimize("-falign-loops") #pragma GCC optimize("-falign-labels") #pragma GCC optimize("-fdevirtualize") #pragma GCC optimize("-fcaller-saves") #pragma GCC optimize("-fcrossjumping") #pragma GCC optimize("-fthread-jumps") #pragma GCC optimize("-funroll-loops") #pragma GCC optimize("-fwhole-program") #pragma GCC optimize("-freorder-blocks") #pragma GCC optimize("-fschedule-insns") #pragma GCC optimize("inline-functions") #pragma GCC optimize("-ftree-tail-merge") #pragma GCC optimize("-fschedule-insns2") #pragma GCC optimize("-fstrict-aliasing") #pragma GCC optimize("-fstrict-overflow") #pragma GCC optimize("-falign-functions") #pragma GCC optimize("-fcse-skip-blocks") #pragma GCC optimize("-fcse-follow-jumps") #pragma GCC optimize("-fsched-interblock") #pragma GCC optimize("-fpartial-inlining") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("-freorder-functions") #pragma GCC optimize("-findirect-inlining") #pragma GCC optimize("-fhoist-adjacent-loads") #pragma GCC optimize("-frerun-cse-after-loop") #pragma GCC optimize("inline-small-functions") #pragma GCC optimize("-finline-small-functions") #pragma GCC optimize("-ftree-switch-conversion") #pragma GCC optimize("-foptimize-sibling-calls") #pragma GCC optimize("-fexpensive-optimizations") #pragma GCC optimize("-funsafe-loop-optimizations") #pragma GCC optimize("inline-functions-called-once") #pragma GCC optimize("-fdelete-null-pointer-checks") using namespace std; inline int read() { int ret = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar(); return w * ret; } int h, w; bitset<25610> s[85][85]; int a[85][85]; int b[85][85]; int main() { cin >> h >> w; for (register int i = 1; i <= h; i++) for (register int j = 1; j <= w; j++) a[i][j] = read(); for (register int i = 1; i <= h; i++) for (register int j = 1; j <= w; j++) b[i][j] = read(); s[0][1][12800] = 1; for (register int i = 1; i <= h; i++) for (register int j = 1; j <= w; j++) { int x = abs(a[i][j] - b[i][j]); for (register int k = -12800; k <= 12800; k++) { if (k - x >= -12800) s[i][j][k + 12800] = s[i][j][k + 12800] | s[i - 1][j][k - x + 12800], s[i][j][k + 12800] = s[i][j][k + 12800] | s[i][j - 1][k - x + 12800]; if (k + x <= 12800) s[i][j][k + 12800] = s[i][j][k + 12800] | s[i - 1][j][k + x + 12800], s[i][j][k + 12800] = s[i][j][k + 12800] | s[i][j - 1][k + x + 12800]; } } for (register int i = 0; i <= 12800; i++) if (s[h][w][12800 + i] || s[h][w][12800 - i]) { cout << i << endl; return 0; } return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int ret = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar(); return w * ret; } int h, w; bitset<25610> s[85][85]; int a[85][85]; int b[85][85]; int main() { cin >> h >> w; for (register int i = 1; i <= h; i++) for (register int j = 1; j <= w; j++) a[i][j] = read(); for (register int i = 1; i <= h; i++) for (register int j = 1; j <= w; j++) b[i][j] = read(); s[0][1][12800] = 1; for (register int i = 1; i <= h; i++) for (register int j = 1; j <= w; j++) { int x = abs(a[i][j] - b[i][j]); for (register int k = -12800; k <= 12800; k++) { if (k - x >= -12800) s[i][j][k + 12800] = s[i][j][k + 12800] | s[i - 1][j][k - x + 12800], s[i][j][k + 12800] = s[i][j][k + 12800] | s[i][j - 1][k - x + 12800]; if (k + x <= 12800) s[i][j][k + 12800] = s[i][j][k + 12800] | s[i - 1][j][k + x + 12800], s[i][j][k + 12800] = s[i][j][k + 12800] | s[i][j - 1][k + x + 12800]; } } for (register int i = 0; i <= 12800; i++) if (s[h][w][12800 + i] || s[h][w][12800 - i]) { cout << i << endl; return 0; } return 0; }
delete
1
48
1
1
TLE
p02839
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int dp[81][81][13000]; int find_it(int dif_sum, int i, int j, int &n, int &m, vector<vector<pair<int, int>>> &matrix) { if (i == n - 1 && j == m - 1) { return min(abs(dif_sum + matrix[i][j].first - matrix[i][j].second), abs(dif_sum + matrix[i][j].second - matrix[i][j].first)); } if (dp[i][j][dif_sum] != -1) { return dp[i][j][dif_sum]; } int minvalue = INT_MAX; if (i + 1 < n) { minvalue = min(minvalue, find_it(dif_sum + (matrix[i][j].first - matrix[i][j].second), i + 1, j, n, m, matrix)); minvalue = min(minvalue, find_it(dif_sum + (matrix[i][j].second - matrix[i][j].first), i + 1, j, n, m, matrix)); } if (j + 1 < m) { minvalue = min(minvalue, find_it(dif_sum + (matrix[i][j].second - matrix[i][j].first), i, j + 1, n, m, matrix)); minvalue = min(minvalue, find_it(dif_sum + (matrix[i][j].first - matrix[i][j].second), i, j + 1, n, m, matrix)); } return dp[i][j][dif_sum] = minvalue; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<pair<int, int>>> matrix(n, vector<pair<int, int>>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j].first; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j].second; } } memset(dp, -1, sizeof(dp)); cout << find_it(0, 0, 0, n, m, matrix); return 0; }
#include <bits/stdc++.h> using namespace std; int dp[81][81][6400 * 2 + 2]; int find_it(int dif_sum, int i, int j, int &n, int &m, vector<vector<pair<int, int>>> &matrix) { if (i == n - 1 && j == m - 1) { return min(abs(dif_sum + matrix[i][j].first - matrix[i][j].second), abs(dif_sum + matrix[i][j].second - matrix[i][j].first)); } if (dp[i][j][dif_sum] != -1) { return dp[i][j][dif_sum]; } int minvalue = INT_MAX; if (i + 1 < n) { minvalue = min(minvalue, find_it(dif_sum + (matrix[i][j].first - matrix[i][j].second), i + 1, j, n, m, matrix)); minvalue = min(minvalue, find_it(dif_sum + (matrix[i][j].second - matrix[i][j].first), i + 1, j, n, m, matrix)); } if (j + 1 < m) { minvalue = min(minvalue, find_it(dif_sum + (matrix[i][j].second - matrix[i][j].first), i, j + 1, n, m, matrix)); minvalue = min(minvalue, find_it(dif_sum + (matrix[i][j].first - matrix[i][j].second), i, j + 1, n, m, matrix)); } return dp[i][j][dif_sum] = minvalue; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<pair<int, int>>> matrix(n, vector<pair<int, int>>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j].first; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j].second; } } memset(dp, -1, sizeof(dp)); cout << find_it(0, 0, 0, n, m, matrix); return 0; }
replace
2
3
2
3
TLE
p02840
C++
Runtime Error
#include <algorithm> #include <cinttypes> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define FSCNd64 "%" SCNd64 #define FPRId64 "%" PRId64 using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vll = vector<ll>; using vvll = vector<vll>; using vpii = vector<pii>; using pll = pair<ll, ll>; using vpll = vector<pll>; template <typename X> bool max_u(X &m, X v) { if (m < v) { m = v; return true; } return false; } template <typename X> bool min_u(X &m, X v) { if (m > v) { m = v; return true; } return false; } #define PI 3.1415926535897932384626433832795 struct solve { solve() {} ll operator()(ll n, ll x, ll d) { if (d < 0) x = -x, d = -d; ll ans = 0; map<int, vpll> m; for (ll i = 0; i <= n; i++) { ll base = x * i; ll s = i * (i - 1) / 2; ll e = (n - 1) * i - i * (i - 1) / 2; int r = base % d; if (r < 0) r += d; m[r].push_back(make_pair((base - r) / d + s, (base - r) / d + e)); } for (auto &mm : m) { auto &v = mm.second; // cout << "r=" << r << " v.size()=" << v.size() << "\n"; sort(v.begin(), v.end()); int vs = v.size(); ll s, e; tie(s, e) = v[0]; for (int i = 1; i < vs; i++) { ll cs, ce; tie(cs, ce) = v[i]; if (e + 1 < cs) { ans += e - s + 1; tie(s, e) = v[i]; } else { max_u(e, ce); } } ans += e - s + 1; } return ans; } }; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); for (;;) { int n, x, d; cin >> n >> x >> d; if (cin.fail()) break; cout << solve()(n, x, d) << "\n"; } return 0; }
#include <algorithm> #include <cinttypes> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define FSCNd64 "%" SCNd64 #define FPRId64 "%" PRId64 using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vll = vector<ll>; using vvll = vector<vll>; using vpii = vector<pii>; using pll = pair<ll, ll>; using vpll = vector<pll>; template <typename X> bool max_u(X &m, X v) { if (m < v) { m = v; return true; } return false; } template <typename X> bool min_u(X &m, X v) { if (m > v) { m = v; return true; } return false; } #define PI 3.1415926535897932384626433832795 struct solve { solve() {} ll operator()(ll n, ll x, ll d) { if (d == 0) { if (x == 0) return 1; return n + 1; } if (d < 0) x = -x, d = -d; ll ans = 0; map<int, vpll> m; for (ll i = 0; i <= n; i++) { ll base = x * i; ll s = i * (i - 1) / 2; ll e = (n - 1) * i - i * (i - 1) / 2; int r = base % d; if (r < 0) r += d; m[r].push_back(make_pair((base - r) / d + s, (base - r) / d + e)); } for (auto &mm : m) { auto &v = mm.second; // cout << "r=" << r << " v.size()=" << v.size() << "\n"; sort(v.begin(), v.end()); int vs = v.size(); ll s, e; tie(s, e) = v[0]; for (int i = 1; i < vs; i++) { ll cs, ce; tie(cs, ce) = v[i]; if (e + 1 < cs) { ans += e - s + 1; tie(s, e) = v[i]; } else { max_u(e, ce); } } ans += e - s + 1; } return ans; } }; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); for (;;) { int n, x, d; cin >> n >> x >> d; if (cin.fail()) break; cout << solve()(n, x, d) << "\n"; } return 0; }
insert
50
50
50
55
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; map<int, vector<pair<long long, long long>>> l; long long sum(long long x) { return x * (x + 1) / 2; } int main() { long long n, x, d, ans = 0; cin >> n >> x >> d; if (!d) { if (!x) cout << 1; else cout << n + 1; } for (int i = 0; i <= n; i++) { long long a = (i * x) % abs(d), cur = (i * x - a) / d; l[a].push_back({sum(i - 1) + cur, sum(n - 1) - sum(n - i - 1) + cur}); } for (auto v : l) { auto cur = v.second; sort(cur.begin(), cur.end()); long long r = -1e18; for (auto p : cur) { ans += max(p.second - max(r, p.first) + 1, 0LL); r = max(r, p.second + 1); } } cout << ans; }
#include <bits/stdc++.h> using namespace std; map<int, vector<pair<long long, long long>>> l; long long sum(long long x) { return x * (x + 1) / 2; } int main() { long long n, x, d, ans = 0; cin >> n >> x >> d; if (!d) { if (!x) cout << 1; else cout << n + 1; return 0; } for (int i = 0; i <= n; i++) { long long a = (i * x) % abs(d), cur = (i * x - a) / d; l[a].push_back({sum(i - 1) + cur, sum(n - 1) - sum(n - i - 1) + cur}); } for (auto v : l) { auto cur = v.second; sort(cur.begin(), cur.end()); long long r = -1e18; for (auto p : cur) { ans += max(p.second - max(r, p.first) + 1, 0LL); r = max(r, p.second + 1); } } cout << ans; }
insert
12
12
12
13
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using PP = pair<long, long>; int n; long a, b; unordered_map<long, vector<PP>> mp; int main() { cin >> n >> b >> a; if (a < 0) { a = -a; b = -b; } long mn = 0; long mx = 0; for (int i = 0; i <= n; ++i) { long k = b * i % a; mp[k].push_back(PP(mn, mx)); mn += a * i + b; mx += a * (n - 1 - i) + b; } long sum = 0; for (auto &&q : mp) { vector<PP> ps = q.second; sort(ps.begin(), ps.end()); vector<PP> nps; const long inf = 1e18; long pl = -inf; long pr = -inf; for (PP p : ps) { if (p.second <= pr) continue; if (p.first > pr) { if (pr > -inf) nps.push_back(PP(pl, pr)); pl = p.first; } pr = p.second; } nps.push_back(PP(pl, pr)); for (PP p : nps) { long l = p.first; long r = p.second; sum += a == 0 ? 1 : (r - l) / a + 1; } } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; using PP = pair<long, long>; int n; long a, b; unordered_map<long, vector<PP>> mp; int main() { cin >> n >> b >> a; if (a < 0) { a = -a; b = -b; } long mn = 0; long mx = 0; for (int i = 0; i <= n; ++i) { long k = a == 0 ? 0 : b * i % a; mp[k].push_back(PP(mn, mx)); mn += a * i + b; mx += a * (n - 1 - i) + b; } long sum = 0; for (auto &&q : mp) { vector<PP> ps = q.second; sort(ps.begin(), ps.end()); vector<PP> nps; const long inf = 1e18; long pl = -inf; long pr = -inf; for (PP p : ps) { if (p.second <= pr) continue; if (p.first > pr) { if (pr > -inf) nps.push_back(PP(pl, pr)); pl = p.first; } pr = p.second; } nps.push_back(PP(pl, pr)); for (PP p : nps) { long l = p.first; long r = p.second; sum += a == 0 ? 1 : (r - l) / a + 1; } } cout << sum << endl; }
replace
18
19
18
19
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<string> VS; typedef pair<ll, ll> P; typedef tuple<int, int, int> tpl; #define ALL(a) (a).begin(), (a).end() #define SORT(c) sort((c).begin(), (c).end()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i) #define RREP(i, n) RFOR(i, n, 0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL << 60; constexpr long long MOD = 1000000007; // 998244353; #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> void remove_duplication(vector<T> &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } long long modp(long long a, long long p) { long long res = a % p; if (res < 0) res += p; return res; } void Main() { ll N, D, X; cin >> N >> X >> D; if (X == 0 && D == 0) { cout << 1 << en; return; } int flag = D > 0 ? 1 : -1; D = abs(D); VL mod{0}; REP(i, N) { ll m = modp(mod.back() + X, D); mod.push_back(m); } remove_duplication(mod); map<ll, int> mp; REP(i, mod.size()) mp[mod[i]] = i; vector<vector<P>> v(mod.size()); v[0].emplace_back(0, 0); ll m = 0; for (ll i = 1; i < N + 1; i++) { m = modp(m + X, D); int k = mp[m]; ll mn = i * X / D + flag * i * (i - 1) / 2; ll mx = i * X / D + flag * (N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2); if (flag < 0) swap(mn, mx); v[k].emplace_back(mn, mx); } ll ans = 0; REP(i, mp.size()) { SORT(v[i]); ll e = -LINF; REP(j, v[i].size()) { if (e < v[i][j].first) ans += v[i][j].second - v[i][j].first + 1; else if (e < v[i][j].second) ans += v[i][j].second - e; chmax(e, v[i][j].second); } } cout << ans << en; return; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); int t = 1; // cin>>t; REP(_, t) { Main(); } return 0; }
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<string> VS; typedef pair<ll, ll> P; typedef tuple<int, int, int> tpl; #define ALL(a) (a).begin(), (a).end() #define SORT(c) sort((c).begin(), (c).end()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i) #define RREP(i, n) RFOR(i, n, 0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL << 60; constexpr long long MOD = 1000000007; // 998244353; #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> void remove_duplication(vector<T> &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } long long modp(long long a, long long p) { long long res = a % p; if (res < 0) res += p; return res; } void Main() { ll N, D, X; cin >> N >> X >> D; if (X == 0 && D == 0) { cout << 1 << en; return; } else if (D == 0) { cout << N + 1 << en; return; } int flag = D > 0 ? 1 : -1; D = abs(D); VL mod{0}; REP(i, N) { ll m = modp(mod.back() + X, D); mod.push_back(m); } remove_duplication(mod); map<ll, int> mp; REP(i, mod.size()) mp[mod[i]] = i; vector<vector<P>> v(mod.size()); v[0].emplace_back(0, 0); ll m = 0; for (ll i = 1; i < N + 1; i++) { m = modp(m + X, D); int k = mp[m]; ll mn = i * X / D + flag * i * (i - 1) / 2; ll mx = i * X / D + flag * (N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2); if (flag < 0) swap(mn, mx); v[k].emplace_back(mn, mx); } ll ans = 0; REP(i, mp.size()) { SORT(v[i]); ll e = -LINF; REP(j, v[i].size()) { if (e < v[i][j].first) ans += v[i][j].second - v[i][j].first + 1; else if (e < v[i][j].second) ans += v[i][j].second - e; chmax(e, v[i][j].second); } } cout << ans << en; return; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); int t = 1; // cin>>t; REP(_, t) { Main(); } return 0; }
insert
82
82
82
85
0
p02840
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <vector> using namespace std; #define int long long #define rep(s, i, n) for (int i = s; i < n; i++) #define c(n) cout << n << endl; #define ic(n) \ int n; \ cin >> n; #define sc(s) \ string s; \ cin >> s; #define mod 998244353 #define inf 1000000000000000007 #define f first #define s second #define mini(c, a, b) *min_element(c + a, c + b) #define maxi(c, a, b) *max_element(c + a, c + b) #define pi 3.141592653589793238462643383279 #define e_ 2.718281828459045235360287471352 #define P pair<int, int> #define upp(a, n, x) upper_bound(a, a + n, x) - a; #define low(a, n, x) lower_bound(a, a + n, x) - a; #define UF UnionFind // printf("%.12Lf\n",); int keta(int x) { rep(0, i, 30) { if (x < 10) { return i + 1; } x = x / 10; } } int gcd(int x, int y) { if (x == 0 || y == 0) return x + y; int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return bb; } bb = bb % aa; if (bb == 0) { return aa; } } } int lcm(int x, int y) { int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return x / bb * y; } bb = bb % aa; if (bb == 0) { return x / aa * y; } } } bool p(int x) { if (x == 1) return false; rep(2, i, sqrt(x) + 1) { if (x % i == 0 && x != i) { return false; } } return true; } int max(int a, int b) { if (a >= b) return a; else return b; } string maxst(string s, string t) { int n = s.size(); int m = t.size(); if (n > m) return s; else if (n < m) return t; else { rep(0, i, n) { if (s[i] > t[i]) return s; if (s[i] < t[i]) return t; } return s; } } int min(int a, int b) { if (a >= b) return b; else return a; } int n2[61]; int nis[61]; int nia[61]; int mody[61]; int nn; int com(int n, int y) { int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } int bunsi = 1, bunbo = 1; rep(0, i, y) bunsi = (bunsi * (n - i)) % mod; rep(0, i, y) bunbo = (bunbo * (i + 1)) % mod; mody[0] = bunbo; rep(1, i, 41) { bunbo = (bunbo * bunbo) % mod; mody[i] = bunbo; } rep(0, i, 41) nis[i] = 0; nn = mod - 2; for (int i = 40; i >= 0; i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { bunsi = (bunsi * mody[i]) % mod; } } return bunsi; } int gyakugen(int n, int y) { int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } mody[0] = y; rep(1, i, 41) { y = (y * y) % mod; mody[i] = y; } rep(0, i, 41) nis[i] = 0; nn = mod - 2; for (int i = 40; i >= 0; i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { n = (n * mody[i]) % mod; } } return n; } int yakuwa(int n) { int sum = 0; rep(1, i, sqrt(n + 1)) { if (n % i == 0) sum += i + n / i; if (i * i == n) sum -= i; } return sum; } int poow(int y, int n) { if (n == 0) return 1; n -= 1; int ni = 1; for (int i = 0; i < 61; i++) { n2[i] = ni; ni *= 2; } int yy = y; mody[0] = yy; rep(1, i, 61) { yy = (yy * yy) % mod; mody[i] = yy; } rep(0, i, 61) nis[i] = 0; nn = n; for (int i = 60; i >= 0; i -= 1) { if (nn >= n2[i]) { nis[i]++; nn -= n2[i]; } } rep(0, i, 61) { if (nis[i] == 1) { y = (y * mody[i]) % mod; } } return y; } int minpow(int x, int y) { int sum = 1; rep(0, i, y) sum *= x; return sum; } int ketawa(int x, int sinsuu) { int sum = 0; rep(0, i, 100) sum += (x % poow(sinsuu, i + 1)) / (poow(sinsuu, i)); return sum; } int sankaku(int a) { return a * (a + 1) / 2; } int sames(int a[1111111], int n) { int ans = 0; rep(0, i, n) { if (a[i] == a[i + 1]) { int j = i; while (a[j + 1] == a[i] && j <= n - 2) j++; ans += sankaku(j - i); i = j; } } return ans; } using Graph = vector<vector<int>>; int oya[114514]; int depth[114514]; void dfs(const Graph &G, int v, int p, int d) { depth[v] = d; oya[v] = p; for (auto nv : G[v]) { if (nv == p) continue; // nv が親 p だったらダメ dfs(G, nv, v, d + 1); // d を 1 増やして子ノードへ } } /*int H=10,W=10; char field[10][10]; char memo[10][10]; void dfs(int h, int w) { memo[h][w] = 'x'; // 八方向を探索 for (int dh = -1; dh <= 1; ++dh) { for (int dw = -1; dw <= 1; ++dw) { if(abs(0-dh)+abs(0-dw)==2)continue; int nh = h + dh, nw = w + dw; // 場外アウトしたり、0 だったりはスルー if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (memo[nh][nw] == 'x') continue; // 再帰的に探索 dfs(nh, nw); } } }*/ int XOR(int a, int b) { if (a == 0 || b == 0) { return a + b; } int ni = 1; rep(0, i, 41) { n2[i] = ni; ni *= 2; } rep(0, i, 41) nis[i] = 0; for (int i = 40; i >= 0; i -= 1) { if (a >= n2[i]) { nis[i]++; a -= n2[i]; } if (b >= n2[i]) { nis[i]++; b -= n2[i]; } } int sum = 0; rep(0, i, 41) sum += (nis[i] % 2 * n2[i]); return sum; } // int ma[1024577][21]; // for(int bit=0;bit<(1<<n);bit++)rep(0,i,n)if(bit&(1<<i))ma[bit][i]=1; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); // xの根をrx int ry = root(y); // yの根をry if (rx == ry) return; // xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; vector<int> k[214514]; signed main() { ic(n) ic(x) ic(d) if (d == 0) { if (x != 0) c(n + 1) else c(1) return 0; } if (x == 0) { c(sankaku(n - 1) + 1) return 0; } int l = abs(d) / gcd(abs(x), abs(d)); rep(0, i, n + 1) k[i % l].push_back(i); int ans = 0; rep(0, i, max(n + 1, l)) { if (!k[i].empty()) { int s = k[i].size(); P r[s + 100]; rep(0, j, s) { r[j].f = sankaku(k[i][j] - 1); r[j].s = sankaku(n - 1) - sankaku(n - k[i][j] - 1); r[j].f += (k[i][j] - i) / l * (x / abs(x)) * lcm(abs(x), abs(d)) / d; r[j].s += (k[i][j] - i) / l * (x / abs(x)) * lcm(abs(x), abs(d)) / d; } sort(r, r + s); int m[s + 100]; int ma = -inf; m[0] = ma; rep(0, j, s) { ma = max(ma, r[j].s); m[j + 1] = ma; } rep(0, j, s) { if (m[j] < r[j].f) ans += r[j].s - r[j].f + 1; else ans += max(0, r[j].s - m[j]); } } } c(ans) }
#include <algorithm> #include <cmath> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <vector> using namespace std; #define int long long #define rep(s, i, n) for (int i = s; i < n; i++) #define c(n) cout << n << endl; #define ic(n) \ int n; \ cin >> n; #define sc(s) \ string s; \ cin >> s; #define mod 998244353 #define inf 1000000000000000007 #define f first #define s second #define mini(c, a, b) *min_element(c + a, c + b) #define maxi(c, a, b) *max_element(c + a, c + b) #define pi 3.141592653589793238462643383279 #define e_ 2.718281828459045235360287471352 #define P pair<int, int> #define upp(a, n, x) upper_bound(a, a + n, x) - a; #define low(a, n, x) lower_bound(a, a + n, x) - a; #define UF UnionFind // printf("%.12Lf\n",); int keta(int x) { rep(0, i, 30) { if (x < 10) { return i + 1; } x = x / 10; } } int gcd(int x, int y) { if (x == 0 || y == 0) return x + y; int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return bb; } bb = bb % aa; if (bb == 0) { return aa; } } } int lcm(int x, int y) { int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return x / bb * y; } bb = bb % aa; if (bb == 0) { return x / aa * y; } } } bool p(int x) { if (x == 1) return false; rep(2, i, sqrt(x) + 1) { if (x % i == 0 && x != i) { return false; } } return true; } int max(int a, int b) { if (a >= b) return a; else return b; } string maxst(string s, string t) { int n = s.size(); int m = t.size(); if (n > m) return s; else if (n < m) return t; else { rep(0, i, n) { if (s[i] > t[i]) return s; if (s[i] < t[i]) return t; } return s; } } int min(int a, int b) { if (a >= b) return b; else return a; } int n2[61]; int nis[61]; int nia[61]; int mody[61]; int nn; int com(int n, int y) { int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } int bunsi = 1, bunbo = 1; rep(0, i, y) bunsi = (bunsi * (n - i)) % mod; rep(0, i, y) bunbo = (bunbo * (i + 1)) % mod; mody[0] = bunbo; rep(1, i, 41) { bunbo = (bunbo * bunbo) % mod; mody[i] = bunbo; } rep(0, i, 41) nis[i] = 0; nn = mod - 2; for (int i = 40; i >= 0; i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { bunsi = (bunsi * mody[i]) % mod; } } return bunsi; } int gyakugen(int n, int y) { int ni = 1; for (int i = 0; i < 41; i++) { n2[i] = ni; ni *= 2; } mody[0] = y; rep(1, i, 41) { y = (y * y) % mod; mody[i] = y; } rep(0, i, 41) nis[i] = 0; nn = mod - 2; for (int i = 40; i >= 0; i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { n = (n * mody[i]) % mod; } } return n; } int yakuwa(int n) { int sum = 0; rep(1, i, sqrt(n + 1)) { if (n % i == 0) sum += i + n / i; if (i * i == n) sum -= i; } return sum; } int poow(int y, int n) { if (n == 0) return 1; n -= 1; int ni = 1; for (int i = 0; i < 61; i++) { n2[i] = ni; ni *= 2; } int yy = y; mody[0] = yy; rep(1, i, 61) { yy = (yy * yy) % mod; mody[i] = yy; } rep(0, i, 61) nis[i] = 0; nn = n; for (int i = 60; i >= 0; i -= 1) { if (nn >= n2[i]) { nis[i]++; nn -= n2[i]; } } rep(0, i, 61) { if (nis[i] == 1) { y = (y * mody[i]) % mod; } } return y; } int minpow(int x, int y) { int sum = 1; rep(0, i, y) sum *= x; return sum; } int ketawa(int x, int sinsuu) { int sum = 0; rep(0, i, 100) sum += (x % poow(sinsuu, i + 1)) / (poow(sinsuu, i)); return sum; } int sankaku(int a) { return a * (a + 1) / 2; } int sames(int a[1111111], int n) { int ans = 0; rep(0, i, n) { if (a[i] == a[i + 1]) { int j = i; while (a[j + 1] == a[i] && j <= n - 2) j++; ans += sankaku(j - i); i = j; } } return ans; } using Graph = vector<vector<int>>; int oya[114514]; int depth[114514]; void dfs(const Graph &G, int v, int p, int d) { depth[v] = d; oya[v] = p; for (auto nv : G[v]) { if (nv == p) continue; // nv が親 p だったらダメ dfs(G, nv, v, d + 1); // d を 1 増やして子ノードへ } } /*int H=10,W=10; char field[10][10]; char memo[10][10]; void dfs(int h, int w) { memo[h][w] = 'x'; // 八方向を探索 for (int dh = -1; dh <= 1; ++dh) { for (int dw = -1; dw <= 1; ++dw) { if(abs(0-dh)+abs(0-dw)==2)continue; int nh = h + dh, nw = w + dw; // 場外アウトしたり、0 だったりはスルー if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (memo[nh][nw] == 'x') continue; // 再帰的に探索 dfs(nh, nw); } } }*/ int XOR(int a, int b) { if (a == 0 || b == 0) { return a + b; } int ni = 1; rep(0, i, 41) { n2[i] = ni; ni *= 2; } rep(0, i, 41) nis[i] = 0; for (int i = 40; i >= 0; i -= 1) { if (a >= n2[i]) { nis[i]++; a -= n2[i]; } if (b >= n2[i]) { nis[i]++; b -= n2[i]; } } int sum = 0; rep(0, i, 41) sum += (nis[i] % 2 * n2[i]); return sum; } // int ma[1024577][21]; // for(int bit=0;bit<(1<<n);bit++)rep(0,i,n)if(bit&(1<<i))ma[bit][i]=1; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); // xの根をrx int ry = root(y); // yの根をry if (rx == ry) return; // xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; vector<int> k[214514]; signed main() { ic(n) ic(x) ic(d) if (d == 0) { if (x != 0) c(n + 1) else c(1) return 0; } if (x == 0) { c(sankaku(n - 1) + 1) return 0; } int l = abs(d) / gcd(abs(x), abs(d)); rep(0, i, n + 1) k[i % l].push_back(i); int ans = 0; rep(0, i, min(n + 1, l)) { if (!k[i].empty()) { int s = k[i].size(); P r[s + 100]; rep(0, j, s) { r[j].f = sankaku(k[i][j] - 1); r[j].s = sankaku(n - 1) - sankaku(n - k[i][j] - 1); r[j].f += (k[i][j] - i) / l * (x / abs(x)) * lcm(abs(x), abs(d)) / d; r[j].s += (k[i][j] - i) / l * (x / abs(x)) * lcm(abs(x), abs(d)) / d; } sort(r, r + s); int m[s + 100]; int ma = -inf; m[0] = ma; rep(0, j, s) { ma = max(ma, r[j].s); m[j + 1] = ma; } rep(0, j, s) { if (m[j] < r[j].f) ans += r[j].s - r[j].f + 1; else ans += max(0, r[j].s - m[j]); } } } c(ans) }
replace
333
334
333
334
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifdef DGC #include "debug.h" #else #define debug(...) 9715 #endif typedef long long ll; typedef long double ld; typedef complex<ld> point; #define F first #define S second int main() { #ifdef DGC // freopen("a.in", "r", stdin); // freopen("b.out", "w", stdout); #endif ios_base::sync_with_stdio(0), cin.tie(0); ll n, x, d; cin >> n >> x >> d; map<ll, vector<pair<ll, ll>>> mp; for (ll k = 0; k <= n; ++k) mp[k * x % d].push_back({k * x / d + k * (k - 1) / 2, k * x / d + (k * (n - 1) - k * (k - 1) / 2)}); ll ans = 0; for (auto &i : mp) { auto &a = i.S; sort(a.begin(), a.end()); auto cur = a[0]; a.push_back({(ll)1e18, 0}); for (auto i : a) { if (i.F <= cur.S) cur.S = max(cur.S, i.S); else ans += cur.S - cur.F + 1, cur = i; } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef DGC #include "debug.h" #else #define debug(...) 9715 #endif typedef long long ll; typedef long double ld; typedef complex<ld> point; #define F first #define S second int main() { #ifdef DGC // freopen("a.in", "r", stdin); // freopen("b.out", "w", stdout); #endif ios_base::sync_with_stdio(0), cin.tie(0); ll n, x, d; cin >> n >> x >> d; if (d == 0) { if (x == 0) cout << "1\n"; else cout << n + 1 << "\n"; return 0; } map<ll, vector<pair<ll, ll>>> mp; for (ll k = 0; k <= n; ++k) mp[k * x % d].push_back({k * x / d + k * (k - 1) / 2, k * x / d + (k * (n - 1) - k * (k - 1) / 2)}); ll ans = 0; for (auto &i : mp) { auto &a = i.S; sort(a.begin(), a.end()); auto cur = a[0]; a.push_back({(ll)1e18, 0}); for (auto i : a) { if (i.F <= cur.S) cur.S = max(cur.S, i.S); else ans += cur.S - cur.F + 1, cur = i; } } cout << ans << "\n"; return 0; }
insert
26
26
26
34
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using LL = long long; #define fs first #define sc second const LL MOD = 1e9 + 7; const double EPS = 1e-10; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } // std::vector Declaration 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...)); } // std::vector Declaration and Initialization template <typename T> vector<T> make_vector(size_t a, T x) { return vector<T>(a, x); } template <typename T, typename U, typename... Ts> auto make_vector(size_t a, U b, Ts... ts) { return vector<decltype(make_vector<T>(b, ts...))>(a, make_vector<T>(b, ts...)); } // std::vector Input template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) { is >> e; } return is; } // std::vector Debug template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; bool a = 1; for (auto e : v) { os << (a ? "" : " "); os << e; a = 0; } os << "]"; return os; } // std::array Debug template <typename T, size_t n> ostream &operator<<(ostream &os, const array<T, n> &v) { os << "["; bool a = 1; for (auto e : v) { os << (a ? "" : " "); os << e; a = 0; } os << "]"; return os; } // std::deque Debug template <typename T> ostream &operator<<(ostream &os, const deque<T> &d) { os << "["; bool a = 1; for (auto e : d) { os << (a ? "" : " "); os << e; a = 0; } os << "]"; return os; } // std::pair Debug template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << " " << p.second << ")"; return os; } // std::set Debug template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { os << "{"; bool a = 1; for (auto e : st) { os << (a ? "" : " "); os << e; a = 0; } os << "}"; return os; } // std::multiset Debug template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { os << "{"; bool a = 1; for (auto e : st) { os << (a ? "" : " "); os << e; a = 0; } os << "}"; return os; } // std::map Debug template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp) { os << "{"; bool a = 1; for (auto e : mp) { os << (a ? "" : " "); os << e.first << ":" << e.second; a = 0; } os << "}"; return os; } // std::tuple Debug template <int N, class Tuple> void out(ostream &os, const Tuple &t) {} template <int N, class Tuple, class H, class... Ts> void out(ostream &os, const Tuple &t) { if (N) os << " "; os << get<N>(t); out<N + 1, Tuple, Ts...>(os, t); } template <class... Ts> ostream &operator<<(ostream &os, const tuple<Ts...> &t) { os << "("; out<0, tuple<Ts...>, Ts...>(os, t); os << ")"; return os; } // Debug #define DUMP(x) cerr << #x << " = " << (x) << endl // Weighted edge 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; } friend ostream &operator<<(ostream &os, const edge &e) { return os << "(" << e.src << "->" << e.to << ":" << e.cost << ")"; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; template <typename T> using MatrixGraph = vector<vector<T>>; int main() { LL N, X, D; cin >> N >> X >> D; LL gcd = __gcd(D, X); D /= gcd; X /= gcd; if (D < 0) { D *= -1; X *= -1; } LL ans = 0; for (LL r = 0; r < D && r <= N; ++r) { multiset<pair<LL, int>> event; for (LL i = r; i <= N; i += D) { LL m = i / D; event.emplace(i * (i - 1) / 2 + X * m, 0); event.emplace(i * (N - 1) - i * (i - 1) / 2 + 1 + X * m, 1); } LL INF = numeric_limits<LL>::max(); LL prv = INF, cnt = 0, sum = 0; for (auto &tp : event) { assert(cnt >= 0); if (cnt && prv != INF) { sum += tp.fs - prv; } prv = tp.fs; cnt += tp.sc ? -1 : 1; } ans += sum; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using LL = long long; #define fs first #define sc second const LL MOD = 1e9 + 7; const double EPS = 1e-10; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } // std::vector Declaration 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...)); } // std::vector Declaration and Initialization template <typename T> vector<T> make_vector(size_t a, T x) { return vector<T>(a, x); } template <typename T, typename U, typename... Ts> auto make_vector(size_t a, U b, Ts... ts) { return vector<decltype(make_vector<T>(b, ts...))>(a, make_vector<T>(b, ts...)); } // std::vector Input template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) { is >> e; } return is; } // std::vector Debug template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; bool a = 1; for (auto e : v) { os << (a ? "" : " "); os << e; a = 0; } os << "]"; return os; } // std::array Debug template <typename T, size_t n> ostream &operator<<(ostream &os, const array<T, n> &v) { os << "["; bool a = 1; for (auto e : v) { os << (a ? "" : " "); os << e; a = 0; } os << "]"; return os; } // std::deque Debug template <typename T> ostream &operator<<(ostream &os, const deque<T> &d) { os << "["; bool a = 1; for (auto e : d) { os << (a ? "" : " "); os << e; a = 0; } os << "]"; return os; } // std::pair Debug template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << " " << p.second << ")"; return os; } // std::set Debug template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { os << "{"; bool a = 1; for (auto e : st) { os << (a ? "" : " "); os << e; a = 0; } os << "}"; return os; } // std::multiset Debug template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { os << "{"; bool a = 1; for (auto e : st) { os << (a ? "" : " "); os << e; a = 0; } os << "}"; return os; } // std::map Debug template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp) { os << "{"; bool a = 1; for (auto e : mp) { os << (a ? "" : " "); os << e.first << ":" << e.second; a = 0; } os << "}"; return os; } // std::tuple Debug template <int N, class Tuple> void out(ostream &os, const Tuple &t) {} template <int N, class Tuple, class H, class... Ts> void out(ostream &os, const Tuple &t) { if (N) os << " "; os << get<N>(t); out<N + 1, Tuple, Ts...>(os, t); } template <class... Ts> ostream &operator<<(ostream &os, const tuple<Ts...> &t) { os << "("; out<0, tuple<Ts...>, Ts...>(os, t); os << ")"; return os; } // Debug #define DUMP(x) cerr << #x << " = " << (x) << endl // Weighted edge 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; } friend ostream &operator<<(ostream &os, const edge &e) { return os << "(" << e.src << "->" << e.to << ":" << e.cost << ")"; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; template <typename T> using MatrixGraph = vector<vector<T>>; int main() { LL N, X, D; cin >> N >> X >> D; if (D == 0) { if (X == 0) { cout << 1 << endl; } else { cout << N + 1 << endl; } return 0; } LL gcd = __gcd(D, X); D /= gcd; X /= gcd; if (D < 0) { D *= -1; X *= -1; } LL ans = 0; for (LL r = 0; r < D && r <= N; ++r) { multiset<pair<LL, int>> event; for (LL i = r; i <= N; i += D) { LL m = i / D; event.emplace(i * (i - 1) / 2 + X * m, 0); event.emplace(i * (N - 1) - i * (i - 1) / 2 + 1 + X * m, 1); } LL INF = numeric_limits<LL>::max(); LL prv = INF, cnt = 0, sum = 0; for (auto &tp : event) { assert(cnt >= 0); if (cnt && prv != INF) { sum += tp.fs - prv; } prv = tp.fs; cnt += tp.sc ? -1 : 1; } ans += sum; } cout << ans << endl; return 0; }
insert
183
183
183
192
0
p02840
C++
Runtime Error
#include <algorithm> #include <cmath> //ABC 147-F #include <cstdio> #include <cstring> #include <iostream> #include <queue> #define LL long long #define MAXN 400001 #define MOD 1000000007ll using namespace std; LL N, X, D; int stk[MAXN], sum; bool vis[MAXN]; LL gcd(LL x, LL y) { return y ? gcd(y, x % y) : x; } struct DB { LL v; int kind, num; } p[MAXN]; bool comp(DB x, DB y) { return x.v == y.v ? x.kind < y.kind : x.v < y.v; } int cnt; LL sig(LL x) { return x < 0 ? -1 : 1; } int main() { cin >> N >> D >> X; // D*=2; LL G = gcd(X, D); LL B = abs(X / G); LL A = abs(D / G); LL ANS = 0; for (LL i = 0; i < B && i <= N; i++) { cnt = 0; for (LL j = 0; j + i <= N; j += B) { LL U = (j + i) * (j + i - 1) / 2 + j / B * A * sig(X * D), V = (i + j) * (N + N - j - i - 1) / 2 + j / B * A * sig(X * D); cnt++; p[cnt].kind = 0; p[cnt].v = min(U, V); p[cnt].num = j / B; cnt++; p[cnt].kind = 1; p[cnt].v = max(U, V); p[cnt].num = j / B; } sort(p + 1, p + cnt + 1, comp); p[0].v = p[1].v; sum = 0; for (int j = 1; j <= cnt; j++) { if (sum) { ANS += (p[j].v - p[j - 1].v); } else ANS++; if (p[j].kind == 0) { sum++; } else { sum--; } } // ANS++; } cout << ANS << endl; return 0; }
#include <algorithm> #include <cmath> //ABC 147-F #include <cstdio> #include <cstring> #include <iostream> #include <queue> #define LL long long #define MAXN 400001 #define MOD 1000000007ll using namespace std; LL N, X, D; int stk[MAXN], sum; bool vis[MAXN]; LL gcd(LL x, LL y) { return y ? gcd(y, x % y) : x; } struct DB { LL v; int kind, num; } p[MAXN]; bool comp(DB x, DB y) { return x.v == y.v ? x.kind < y.kind : x.v < y.v; } int cnt; LL sig(LL x) { return x < 0 ? -1 : 1; } int main() { cin >> N >> D >> X; // D*=2; if (X == 0 && D == 0) { cout << 1 << endl; return 0; } if (X == 0) { cout << N + 1 << endl; return 0; } LL G = gcd(X, D); LL B = abs(X / G); LL A = abs(D / G); LL ANS = 0; for (LL i = 0; i < B && i <= N; i++) { cnt = 0; for (LL j = 0; j + i <= N; j += B) { LL U = (j + i) * (j + i - 1) / 2 + j / B * A * sig(X * D), V = (i + j) * (N + N - j - i - 1) / 2 + j / B * A * sig(X * D); cnt++; p[cnt].kind = 0; p[cnt].v = min(U, V); p[cnt].num = j / B; cnt++; p[cnt].kind = 1; p[cnt].v = max(U, V); p[cnt].num = j / B; } sort(p + 1, p + cnt + 1, comp); p[0].v = p[1].v; sum = 0; for (int j = 1; j <= cnt; j++) { if (sum) { ANS += (p[j].v - p[j - 1].v); } else ANS++; if (p[j].kind == 0) { sum++; } else { sum--; } } // ANS++; } cout << ANS << endl; return 0; }
insert
24
24
24
32
0
p02840
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Vec = vector<ll>; using VecP = vector<P>; #define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i)) #define REPN(i, m, n) for (ll(i) = (m); (i) <= (n); ++(i)) #define REPR(i, m, n) for (ll(i) = (m); (i) >= (n); --(i)) #define rep(i, n) REP(i, 0, n) #define repn(i, n) REPN(i, 1, n) #define repr(i, n) REPR(i, n, 0) #define repnr(i, n) REPR(i, n, 1) #define co(n) cout << (n) << endl #define cosp(n) cout << (n) << ' ' #define setp(n) cout << fixed << setprecision(n); #define all(s) (s).begin(), (s).end() #define pb push_back #define mp make_pair #define mt make_tuple #define fs first #define sc second template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } const ll INF = 1e9 + 1; const ll LINF = 1e18 + 1; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const double PI = acos(-1); const double EPS = 1e-9; ll mod(ll n, ll m) { return ((n % m) + m) % m; } int main(void) { ll n, x, d; cin >> n >> x >> d; if (d < 0) { d = -d; x = -x; } if (!d) { if (!x) co(1); else co(n + 1); return 0; } map<ll, VecP> range; rep(i, n + 1) { ll p = i * x / d; ll r = mod(i * x, d); ll b = p + i * (0 + i - 1) / 2; ll e = p + i * (n - 1 + n - i) / 2; range[r].pb(mp(b, e)); } ll ans = 0; rep(i, d) { VecP v = range[i]; sort(all(v)); ll e = -LINF; for (P r : v) { if (r.fs > e) ans += r.sc - r.fs + 1; else ans += max(0LL, r.sc - e); chmax(e, r.sc); } } co(ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Vec = vector<ll>; using VecP = vector<P>; #define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i)) #define REPN(i, m, n) for (ll(i) = (m); (i) <= (n); ++(i)) #define REPR(i, m, n) for (ll(i) = (m); (i) >= (n); --(i)) #define rep(i, n) REP(i, 0, n) #define repn(i, n) REPN(i, 1, n) #define repr(i, n) REPR(i, n, 0) #define repnr(i, n) REPR(i, n, 1) #define co(n) cout << (n) << endl #define cosp(n) cout << (n) << ' ' #define setp(n) cout << fixed << setprecision(n); #define all(s) (s).begin(), (s).end() #define pb push_back #define mp make_pair #define mt make_tuple #define fs first #define sc second template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } const ll INF = 1e9 + 1; const ll LINF = 1e18 + 1; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const double PI = acos(-1); const double EPS = 1e-9; ll mod(ll n, ll m) { return ((n % m) + m) % m; } int main(void) { ll n, x, d; cin >> n >> x >> d; if (d < 0) { d = -d; x = -x; } if (!d) { if (!x) co(1); else co(n + 1); return 0; } map<ll, VecP> range; rep(i, n + 1) { ll p = i * x / d; ll r = mod(i * x, d); ll b = p + i * (0 + i - 1) / 2; ll e = p + i * (n - 1 + n - i) / 2; range[r].pb(mp(b, e)); } ll ans = 0; for (auto itr : range) { VecP &v = itr.sc; sort(all(v)); ll e = -LINF; for (P r : v) { if (r.fs > e) ans += r.sc - r.fs + 1; else ans += max(0LL, r.sc - e); chmax(e, r.sc); } } co(ans); return 0; }
replace
71
73
71
73
TLE
p02840
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int(i) = 0; i < (n); ++(i)) typedef long long ll; const ll MOD = 1e9 + 7; const ll INF = 1LL << 62; int main() { int N; ll X, D; std::cin >> N >> X >> D; std::map<ll, std::vector<std::pair<ll, ll>>> ranges; for (ll k = 0; k <= N; ++k) { ll L = k * (k - 1) / 2; ll R = k * (2 * N - k - 1) / 2; if (L > R) { std::swap(L, R); } ll r = X * k % D; ll n = (X * k) / D; if (r < 0) r -= D; ranges[r].push_back(std::make_pair(n + L, n + R + 1)); } ll ans = 0; for (auto &p : ranges) { auto &range_vec = p.second; ll last = -INF; std::sort(range_vec.begin(), range_vec.end()); for (auto range : range_vec) { ll L = range.first; ll R = range.second; if (R <= last) continue; L = std::max(L, last); ans += R - L; last = R; } } std::cout << ans << '\n'; }
#include <bits/stdc++.h> #define rep(i, n) for (int(i) = 0; i < (n); ++(i)) typedef long long ll; const ll MOD = 1e9 + 7; const ll INF = 1LL << 62; int main() { int N; ll X, D; std::cin >> N >> X >> D; if (D == 0) { if (X == 0) { std::cout << "1" << '\n'; return 0; } else { std::cout << N + 1 << '\n'; return 0; } } std::map<ll, std::vector<std::pair<ll, ll>>> ranges; for (ll k = 0; k <= N; ++k) { ll L = k * (k - 1) / 2; ll R = k * (2 * N - k - 1) / 2; if (L > R) { std::swap(L, R); } ll r = X * k % D; ll n = (X * k) / D; if (r < 0) r -= D; ranges[r].push_back(std::make_pair(n + L, n + R + 1)); } ll ans = 0; for (auto &p : ranges) { auto &range_vec = p.second; ll last = -INF; std::sort(range_vec.begin(), range_vec.end()); for (auto range : range_vec) { ll L = range.first; ll R = range.second; if (R <= last) continue; L = std::max(L, last); ans += R - L; last = R; } } std::cout << ans << '\n'; }
insert
12
12
12
22
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using LL = long long; using PLL = pair<LL, LL>; const LL INF = 1e18; LL sum(LL n) { return (n * (n - 1)) / 2; } int main() { #ifdef LOCAL_EXEC // freopen("sample.in", "r", stdin); // freopen("sample.out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(NULL); #endif LL n, x, d; cin >> n >> x >> d; LL ad = llabs(d); vector<pair<LL, PLL>> ranges; ranges.emplace_back(0, PLL(0, 0)); for (int i = 1; i <= n; ++i) { LL rem = (i * x) % ad; LL q = (i * x - rem) / d; LL lo = sum(i); LL hi = sum(n) - sum(n - i); ranges.emplace_back(rem, PLL{q + lo, q + hi}); } sort(ranges.begin(), ranges.end()); LL prv = -1; LL ans = 0, lo = -INF, hi = -INF - 10; for (auto &e : ranges) { if (e.first != prv) { ans += max(0ll, hi - lo + 1); lo = -INF, hi = -INF - 10; prv = e.first; } LL clo, chi; tie(clo, chi) = e.second; if (clo > hi) { ans += max(0ll, hi - lo + 1); lo = clo, hi = chi; } else { hi = max(hi, chi); } } ans += max(0ll, hi - lo + 1); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using LL = long long; using PLL = pair<LL, LL>; const LL INF = 1e18; LL sum(LL n) { return (n * (n - 1)) / 2; } int main() { #ifdef LOCAL_EXEC // freopen("sample.in", "r", stdin); // freopen("sample.out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(NULL); #endif LL n, x, d; cin >> n >> x >> d; if (d == 0) { if (x == 0) { cout << "1\n"; } else { cout << n + 1 << endl; } return 0; } LL ad = llabs(d); vector<pair<LL, PLL>> ranges; ranges.emplace_back(0, PLL(0, 0)); for (int i = 1; i <= n; ++i) { LL rem = (i * x) % ad; LL q = (i * x - rem) / d; LL lo = sum(i); LL hi = sum(n) - sum(n - i); ranges.emplace_back(rem, PLL{q + lo, q + hi}); } sort(ranges.begin(), ranges.end()); LL prv = -1; LL ans = 0, lo = -INF, hi = -INF - 10; for (auto &e : ranges) { if (e.first != prv) { ans += max(0ll, hi - lo + 1); lo = -INF, hi = -INF - 10; prv = e.first; } LL clo, chi; tie(clo, chi) = e.second; if (clo > hi) { ans += max(0ll, hi - lo + 1); lo = clo, hi = chi; } else { hi = max(hi, chi); } } ans += max(0ll, hi - lo + 1); cout << ans << endl; return 0; }
insert
19
19
19
27
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ull mod = 1e9 + 7; #define REP(i, n) for (int i = 0; i < (int)n; ++i) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> v) { os << "(" << v.first << ", " << v.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << " "; } os << v[i]; } return os; } template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << endl; } os << v[i]; } return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, X, D; cin >> N >> X >> D; if (X == 0) { if (D == 0) { cout << 1 << endl; } else { cout << (N - 1) * N / 2 + 1 << endl; } return 0; } if (D < 0) { X = X + (N - 1) * D; D = -D; } vector<pll> ps[202020]; map<ll, ll> mp; ll idx = 0; ll lb = 0, ub = 0; REP(k, N + 1) { ll L = k * X + lb * D; ll R = k * X + ub * D; // dump(L) // dump(R) if (mp.find((k * X) % D) == mp.end()) { mp[(k * X) % D] = idx; ps[idx].push_back({L, R}); idx++; } else { ps[mp[(k * X) % D]].push_back({L, R}); } lb += k; ub += (N - 1 - k); } // cout << ps[0] << endl; ll res = 0; REP(i, idx) { sort(ps[i].begin(), ps[i].end()); ll left = ps[i][0].first; ll right = ps[i][0].second; REP(j, ps[i].size()) { if (j == 0) continue; if (ps[i][j].first > right) { if (j > 0) res += ((right - left) / D + 1); left = ps[i][j].first; right = ps[i][j].second; } else { right = max(right, ps[i][j].second); } // dump(res) } res += ((right - left) / D + 1); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ull mod = 1e9 + 7; #define REP(i, n) for (int i = 0; i < (int)n; ++i) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> v) { os << "(" << v.first << ", " << v.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << " "; } os << v[i]; } return os; } template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << endl; } os << v[i]; } return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, X, D; cin >> N >> X >> D; if (X == 0) { if (D == 0) { cout << 1 << endl; } else { cout << (N - 1) * N / 2 + 1 << endl; } return 0; } if (D == 0) { cout << N + 1 << endl; return 0; } if (D < 0) { X = X + (N - 1) * D; D = -D; } vector<pll> ps[202020]; map<ll, ll> mp; ll idx = 0; ll lb = 0, ub = 0; REP(k, N + 1) { ll L = k * X + lb * D; ll R = k * X + ub * D; // dump(L) // dump(R) if (mp.find((k * X) % D) == mp.end()) { mp[(k * X) % D] = idx; ps[idx].push_back({L, R}); idx++; } else { ps[mp[(k * X) % D]].push_back({L, R}); } lb += k; ub += (N - 1 - k); } // cout << ps[0] << endl; ll res = 0; REP(i, idx) { sort(ps[i].begin(), ps[i].end()); ll left = ps[i][0].first; ll right = ps[i][0].second; REP(j, ps[i].size()) { if (j == 0) continue; if (ps[i][j].first > right) { if (j > 0) res += ((right - left) / D + 1); left = ps[i][j].first; right = ps[i][j].second; } else { right = max(right, ps[i][j].second); } // dump(res) } res += ((right - left) / D + 1); } cout << res << endl; return 0; }
insert
56
56
56
60
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr ll MOD = 1e9 + 7; template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chmax(T &a, T b) { a = max(a, b); } vector<P> vs[200010]; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, x, d; cin >> n >> x >> d; ll y = x - d; if (d == 0) { if (y == 0) { cout << 1 << endl; } else { cout << n + 1 << endl; } } if (d < 0) { y *= -1LL; d *= -1LL; } ll res = 0; vector<ll> v; for (ll k = 0; k <= n; k++) { v.push_back((k * y) % d); } sort(all(v)); v.erase(unique(all(v)), v.end()); for (ll k = 0; k <= n; k++) { int id = lower_bound(all(v), (k * y) % d) - v.begin(); ll l = k * y / d + k * (k + 1LL) / 2LL, r = k * y / d + k * (n - k + 1LL + n) / 2LL; vs[id].push_back(P(l, r)); // cout << id << " " << l << " " << r << endl; } for (int i = 0; i < v.size(); i++) { vector<ll> vv; for (auto &p : vs[i]) { vv.push_back(p.first); vv.push_back(p.second + 1); } sort(all(vv)); vv.erase(unique(all(vv)), vv.end()); vector<ll> sum(vv.size() + 1); for (auto &p : vs[i]) { int l = lower_bound(all(vv), p.first) - vv.begin(); int r = lower_bound(all(vv), p.second + 1) - vv.begin(); sum[l]++; sum[r]--; } for (int i = 1; i < vv.size(); i++) { sum[i] += sum[i - 1]; } for (int i = 0; i + 1 < vv.size(); i++) { if (sum[i] > 0) { res += vv[i + 1] - vv[i]; } } } cout << res << endl; }
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr ll MOD = 1e9 + 7; template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chmax(T &a, T b) { a = max(a, b); } vector<P> vs[200010]; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, x, d; cin >> n >> x >> d; ll y = x - d; if (d == 0) { if (y == 0) { cout << 1 << endl; } else { cout << n + 1 << endl; } return 0; } if (d < 0) { y *= -1LL; d *= -1LL; } ll res = 0; vector<ll> v; for (ll k = 0; k <= n; k++) { v.push_back((k * y) % d); } sort(all(v)); v.erase(unique(all(v)), v.end()); for (ll k = 0; k <= n; k++) { int id = lower_bound(all(v), (k * y) % d) - v.begin(); ll l = k * y / d + k * (k + 1LL) / 2LL, r = k * y / d + k * (n - k + 1LL + n) / 2LL; vs[id].push_back(P(l, r)); // cout << id << " " << l << " " << r << endl; } for (int i = 0; i < v.size(); i++) { vector<ll> vv; for (auto &p : vs[i]) { vv.push_back(p.first); vv.push_back(p.second + 1); } sort(all(vv)); vv.erase(unique(all(vv)), vv.end()); vector<ll> sum(vv.size() + 1); for (auto &p : vs[i]) { int l = lower_bound(all(vv), p.first) - vv.begin(); int r = lower_bound(all(vv), p.second + 1) - vv.begin(); sum[l]++; sum[r]--; } for (int i = 1; i < vv.size(); i++) { sum[i] += sum[i - 1]; } for (int i = 0; i + 1 < vv.size(); i++) { if (sum[i] > 0) { res += vv[i + 1] - vv[i]; } } } cout << res << endl; }
insert
23
23
23
24
0
p02840
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll 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; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } // greatest common divisor and least common multiple // gcd is calculated by Euclidean Algorithm // lcm = m * n / gcd(m,n) template <typename T = int> T gcd(T a, T b) { if (a < b) return gcd(b, a); T r; while ((r = a % b)) { a = b; b = r; } return b; } template <typename T = int> T lcm(T m, T n) { if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); } int main() { ll n, x, d; cin >> n >> x >> d; if (d == 0) { cout << (x == 0 ? 1 : n + 1) << "\n"; return 0; } ll g = gcd(abs(x), abs(d)); if (d < 0) g *= -1; x /= g; d /= g; vector<vector<pair<ll, int>>> imos(min(n + 1, d)); for (ll i = 0; i <= n; ++i) { ll l = (0 + i - 1) * (i) / 2; ll r = (n - 1 + n - i) * (i) / 2; l += x * i / d; r += x * i / d; ll idx = i % d; imos[idx].emplace_back(l, 1); imos[idx].emplace_back(r + 1, -1); } ll ret = 0; for (int i = 0; i < min(n + 1, d); ++i) { sort(imos[i].begin(), imos[i].end()); ll prev = -INF; ll depth = 0; for (auto &p : imos[i]) { if (depth > 0) { ret += p.first - prev; } prev = p.first; depth += p.second; } } cout << ret << "\n"; return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll 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; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } // greatest common divisor and least common multiple // gcd is calculated by Euclidean Algorithm // lcm = m * n / gcd(m,n) template <typename T = int> T gcd(T a, T b) { if (a < b) return gcd(b, a); T r; while ((r = a % b)) { a = b; b = r; } return b; } template <typename T = int> T lcm(T m, T n) { if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); } int main() { ll n, x, d; cin >> n >> x >> d; if (d == 0) { cout << (x == 0 ? 1 : n + 1) << "\n"; return 0; } if (x == 0) { d = 1; } else { ll g = gcd(abs(x), abs(d)); if (d < 0) g *= -1; x /= g; d /= g; } vector<vector<pair<ll, int>>> imos(min(n + 1, d)); for (ll i = 0; i <= n; ++i) { ll l = (0 + i - 1) * (i) / 2; ll r = (n - 1 + n - i) * (i) / 2; l += x * i / d; r += x * i / d; ll idx = i % d; imos[idx].emplace_back(l, 1); imos[idx].emplace_back(r + 1, -1); } ll ret = 0; for (int i = 0; i < min(n + 1, d); ++i) { sort(imos[i].begin(), imos[i].end()); ll prev = -INF; ll depth = 0; for (auto &p : imos[i]) { if (depth > 0) { ret += p.first - prev; } prev = p.first; depth += p.second; } } cout << ret << "\n"; return 0; }
replace
50
55
50
59
0
p02840
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll 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; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } // greatest common divisor and least common multiple // gcd is calculated by Euclidean Algorithm // lcm = m * n / gcd(m,n) template <typename T = int> T gcd(T a, T b) { if (a < b) return gcd(b, a); T r; while ((r = a % b)) { a = b; b = r; } return b; } template <typename T = int> T lcm(T m, T n) { if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); } int main() { ll n, x, d; cin >> n >> x >> d; if (d == 0) { cout << (x == 0 ? 1 : n + 1) << "\n"; return 0; } ll g = gcd(abs(x), abs(d)); if (d < 0) g *= -1; x /= g; d /= g; map<int, vector<pll>> imos; for (ll i = 0; i <= n; ++i) { ll l = (0 + i - 1) * (i) / 2; ll r = (n - 1 + n - i) * (i) / 2; l += x * i / d; r += x * i / d; imos[(i * x) % d].emplace_back(l, 1); imos[(i * x) % d].emplace_back(r + 1, -1); } ll ret = 0; for (auto &im : imos) { sort(im.second.begin(), im.second.end()); ll prev = -INF; ll depth = 0; for (auto &p : im.second) { if (depth > 0) { ret += p.first - prev; } prev = p.first; depth += p.second; } } cout << ret << "\n"; return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll 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; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } // greatest common divisor and least common multiple // gcd is calculated by Euclidean Algorithm // lcm = m * n / gcd(m,n) template <typename T = int> T gcd(T a, T b) { if (a < b) return gcd(b, a); if (b == 0) return a; T r; while ((r = a % b)) { a = b; b = r; } return b; } template <typename T = int> T lcm(T m, T n) { if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); } int main() { ll n, x, d; cin >> n >> x >> d; if (d == 0) { cout << (x == 0 ? 1 : n + 1) << "\n"; return 0; } ll g = gcd(abs(x), abs(d)); if (d < 0) g *= -1; x /= g; d /= g; map<int, vector<pll>> imos; for (ll i = 0; i <= n; ++i) { ll l = (0 + i - 1) * (i) / 2; ll r = (n - 1 + n - i) * (i) / 2; l += x * i / d; r += x * i / d; imos[(i * x) % d].emplace_back(l, 1); imos[(i * x) % d].emplace_back(r + 1, -1); } ll ret = 0; for (auto &im : imos) { sort(im.second.begin(), im.second.end()); ll prev = -INF; ll depth = 0; for (auto &p : im.second) { if (depth > 0) { ret += p.first - prev; } prev = p.first; depth += p.second; } } cout << ret << "\n"; return 0; }
insert
29
29
29
31
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll MOD = 1000000007; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-12; struct mint { ll x; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } using edge = struct { ll to; ll cost; }; struct fpoint { ld x; ld y; bool operator<(const fpoint &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const fpoint &p) const { if (p.x - EPS < x && x < p.x + EPS && p.y - EPS < y && y < p.y + EPS) return true; return false; } bool operator!=(const fpoint &p) const { if (p.x - EPS > x || x > p.x + EPS || p.y - EPS > y || y > p.y + EPS) return true; return false; } fpoint &operator+=(const ld a) { x += a, y += a; return *this; } fpoint &operator-=(const ld a) { x -= a, y -= a; return *this; } fpoint &operator*=(const ld a) { x *= a, y *= a; return *this; } fpoint &operator/=(const ld a) { x /= a, y /= a; return *this; } fpoint &operator+=(const fpoint p) { x += p.x, y += p.y; return *this; } fpoint &operator-=(const fpoint p) { x -= p.x, y -= p.y; return *this; } fpoint &operator*=(const fpoint p) { x *= p.x, y *= p.y; return *this; } fpoint &operator/=(const fpoint p) { x /= p.x, y /= p.y; return *this; } bool is_zero() { fpoint z = {0, 0}; return z == *this; } fpoint operator+(const fpoint p) const { return fpoint(*this) += p; } fpoint operator-(const fpoint p) const { return fpoint(*this) -= p; } fpoint operator*(const fpoint p) const { return fpoint(*this) *= p; } fpoint operator/(const fpoint p) const { return fpoint(*this) /= p; } fpoint operator+(const ld a) const { return fpoint(*this) += a; } fpoint operator-(const ld a) const { return fpoint(*this) -= a; } fpoint operator*(const ld a) const { return fpoint(*this) *= a; } fpoint operator/(const ld a) const { return fpoint(*this) /= a; } }; struct point { ll x; ll y; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const point &p) const { if (x == p.x && y == p.y) return true; return false; } bool operator!=(const point &p) const { if (x != p.x || y != p.y) return true; return false; } point &operator+=(const ll a) { x += a, y += a; return *this; } point &operator-=(const ll a) { x -= a, y -= a; return *this; } point &operator*=(const ll a) { x *= a, y *= a; return *this; } point &operator+=(const point p) { x += p.x, y += p.y; return *this; } point &operator-=(const point p) { x -= p.x, y -= p.y; return *this; } point &operator*=(const point p) { x *= p.x, y *= p.y; return *this; } bool is_zero() { point z = {0, 0}; return z == *this; } fpoint to_fpoint() { fpoint ret = {ld(x), ld(y)}; return ret; } void operator++(int) { x++, y++; } void operator++() { x++, y++; } void operator--(int) { x--, y--; } void operator--() { x--, y--; } point operator+(const point p) const { return point(*this) += p; } point operator-(const point p) const { return point(*this) -= p; } point operator*(const point p) const { return point(*this) *= p; } point operator+(const ll a) const { return point(*this) += a; } point operator-(const ll a) const { return point(*this) -= a; } point operator*(const ll a) const { return point(*this) *= a; } }; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<mint> vmi; typedef std::vector<vmi> vmi2; typedef std::vector<vmi2> vmi3; typedef std::vector<vmi3> vmi4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; typedef std::vector<fpoint> fpoints; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const long double PI = 3.14159265358979323846; points dirs = { {-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); 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"); } string to_string(mint a) { return to_string(a.x); } string to_string(point p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } string to_string(fpoint p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } 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; } template <typename T> string to_string(priority_queue<T> &q) { priority_queue<T> copy; bool first = true; string res = "{"; while (!q.empty()) { if (!first) { res += ", "; } first = false; res += to_string(q.top()); copy.push(q.top()); q.pop(); } swap(q, copy); res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) std::cout << (a) #define prl(a) std::cout << (a) << endl #define prl2(a, b) std::cout << (a) << " " << (b) << endl #define prl3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) std::cout << (a) << " " #define prs2(a, b) std::cout << (a) << " " << (b) << " " #define prs3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define ltos(n) to_string((n)) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ std::cout << fixed << setprecision(digits) << (num) << endl; #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #ifdef _LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow. if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; struct UnionFind { vl parents, set_size; set<ll> root_idx; ll n_groups; UnionFind(ll n) { parents = set_size = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); n_groups--; } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; class LowestCommonAncestor { public: ll N, logN; vl depth, len; Graph tree; vl2 parents; LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = len = vl(N); parents = vl2(logN, vl(N)); tree = Graph(N); input(N, offset, is_weighted); init(0, -1, 0, 0); build(); } void add_edge(ll from, ll to, ll dist) { tree[from].pb({to, dist}); tree[to].pb({from, dist}); } void input(ll n, ll offset = -1, bool is_weighted = true) { rep(i, n - 1) { ll a, b, d = 1; in2(a, b); a += offset, b += offset; if (is_weighted) in1(d); add_edge(a, b, d); } } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, d); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; // This is the exaple of the number of children static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } ll nPk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; return val % mod; } }; struct PowerMemo { ll sz, mod, base; vl powB; PowerMemo(ll sz, ll base, ll _mod) : sz(sz), base(base), mod(_mod) { powB.resize(sz + 5); init(); } void init() { powB[0] = 1; rep(i, sz + 3) powB[i + 1] = (powB[i] * base) % mod; } ll operator[](ll k) { return powB[k]; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point nxt = p + dirs[i]; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; struct Cumulative2D { vl2 cum; ll w, h; Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); } template <typename T> void build(vector<T> &vec) { // never forget building rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; } ll func(ll x, ll y, ll dx, ll dy) { // 1-indexed // the rectangle of (x, y), (x + dx, y), (x, y + dy) and (x + dx, y + dy) // think about the case of (1, 1, 1, 1). if (x + dx > w || y + dy > h) return -INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n + 1, vl(n + 1)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } mll prime_factorization(ll n) { ll i = 2; mll table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll next_combination(ll sub) { /* nCk ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; if (x != 0) return (((sub & ~y) / x) >> 1) | y; else return INF; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F template <typename T> vl z_algorithm(T &s, ll n) { /* Paramters --------- T: the string or list of interest n: the size of string or list Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } ll big_number_compare(str s1, str s2) { if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else if (s1 == s2) return 0; return 2 * (s1 > s2) - 1; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } template <typename T> bool isColinear(T p1, T p2, T p3) { T v1 = p2 - p1, v2 = p3 - p1; return v1.x * v2.y == v1.y * v2.x; } template <typename T> T PerpendicularBisector(T p1, T p2) { T vec = p2 - p1; assert(!vec.is_zero()); T ret = {vec.y, -vec.x}; return ret; } template <typename T> ld Distance2DPoints(T p1, T p2) { T vec = (p1 - p2) * (p1 - p2); return sqrt(vec.x + vec.y); } ll SquaredDistance2DPoints(point p1, point p2) { point vec = (p1 - p2) * (p1 - p2); return vec.x + vec.y; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> OuterCenter(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value point ret; if (isColinear(p1, p2, p3)) { ll d1 = SquaredDistance2DPoints(p1, p2); ll d2 = SquaredDistance2DPoints(p2, p3); ll d3 = SquaredDistance2DPoints(p3, p1); if (d1 >= d2 && d1 >= d3) { ret = p1 + p2; return mp(ret, 2); } else if (d2 >= d1 && d2 >= d3) { ret = p2 + p3; return mp(ret, 2); } else { ret = p3 + p1; return mp(ret, 2); } } point pv1 = PerpendicularBisector(p1, p2); point pv2 = PerpendicularBisector(p1, p3); point cv1_2x = p1 + p2, cv2_2x = p1 + p3; // cv1 + k pv1 == cv2 + m pv2 // (pv1x -pv2x) (k) = (cv2x - cv1x) // (pv1y -pv2y) (m) = (cv2y - cv1y) ll det_inv = -pv1.x * pv2.y + pv1.y * pv2.x; ll x1_2x = cv2_2x.x - cv1_2x.x, x2_2x = cv2_2x.y - cv1_2x.y; pl c_2x_det = {-pv2.y * x1_2x + pv2.x * x2_2x, -pv1.y * x1_2x + pv1.x * x2_2x}; // ret.x = ld(cv1_2x.x * det_inv + pv1.x * c_2x_det.e1) / ld(2 * det_inv); // ret.y = ld(cv1_2x.y * det_inv + pv1.y * c_2x_det.e1) / ld(2 * det_inv); ret.x = cv1_2x.x * det_inv + pv1.x * c_2x_det.e1; ret.y = cv1_2x.y * det_inv + pv1.y * c_2x_det.e1; ll jacobian = 2 * det_inv; return mp(ret, jacobian); } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value. val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } ld bin_search(ld left, ld right, bool lb, function<bool(ld)> judge) { ld mid; while (right - left > EPS) { mid = (right + left) / 2; if (lb) { if (judge(mid)) right = mid; else left = mid + EPS; } else { if (judge(mid)) left = mid; else right = mid - EPS; } } return right; } ll bin_search(ll left, ll right, bool lb, function<bool(ll)> judge) { ll mid; while (right > left) { if (lb) { // if true (satisfies the condition), range shifts smaller direction mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; } else { // if true (satisfies the condition), range shitfs larger direction mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } } return right; } ld trinary_search(ld left, ld right, function<ld(ld)> func) { // Care the value EPS!!! Compare to the condition while (abs(right - left) > EPS) { ld left2 = (2.0 * left + right) / 3.0; ld right2 = (left + 2.0 * right) / 3.0; ld f1 = func(left2); ld f2 = func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } return right; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { // O(ElogE) sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v) { ll n = v.size(); vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> edge_costs; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { ll idx, cost; tie(cost, idx) = edge_costs.top(); edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] + e.cost) { dist[e.to] = dist[idx] + e.cost; if (trace) vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void Euler_Tour(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record only when we first reach a node and leave the node. ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); } euler_tour.pb(-source); out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } void Euler_Tour2(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record everytime we reach a node ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); euler_tour.pb(source); } out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } std::mt19937 create_rand_engine() { std::random_device rnd; std::vector<std::uint_least32_t> v(10); std::generate(v.begin(), v.end(), std::ref(rnd)); std::seed_seq seed(v.begin(), v.end()); return std::mt19937(seed); } vl generate_unique_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); const size_t num = static_cast<size_t>(sz * 1.2); assert(vm <= vM); assert(sz <= range); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) { while (ret.size() < num) ret.pb(distribution(engine)); sort(all(ret)); auto unique_end = unique(all(ret)); if (sz < distance(ret.begin(), unique_end)) unique_end = next(ret.begin(), sz); ret.erase(unique_end, ret.end()); } return ret; } vl generate_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); assert(vm <= vM); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) ret.pb(distribution(engine)); return ret; } void read_graph(ll n_vertex, ll n_edges, Graph &graph, ll cost = -1, bool directed = false, ll offset = -1) { graph.resize(n_vertex); rep(i, n_edges) { ll from, to, c = cost; if (cost == -1) in3(from, to, c); else in2(from, to); from += offset, to += offset; graph[from].pb({to, cost}); if (!directed) graph[to].pb({from, cost}); } } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } void read_pair_vector(ll n, vpl &v, ll offset1 = 0, ll offset2 = 0, bool rev = false) { v.resize(n); rep(i, n) { ll a, b; in2(a, b); a += offset1, b += offset2; if (!rev) v[i] = {a, b}; else v[i] = {b, a}; } } void read_2DMap(ll w, ll h, vb2 &block, char b) { block = vb2(w, vb(h, false)); str s; rep(y, h) { in1(s); rep(x, w) block[x][y] = (s[x] == b); } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ if (l == r) r++; while(r < n && cond) r++; l++; } prl(answer); #11. bfs ABC146D, ABC007C 1. first create a tree. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = graph[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = graph[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } */ /* x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & ~ x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 ####### Attention ####### S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & ~(1 << i) -> S - {i} __builtin_popcountl(i) -> the number of 1 in binary #Conditional Operator condition ? true : false; # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; const size_t MAX_BIT = 12000; typedef bitset<MAX_BIT> bts; typedef vector<bts> vbt; typedef vector<vbt> vbt2; typedef vector<vbt2> vbt3; bool okay = false; ll answer = 0; str s, t, u; ll n, m, h, w, k, Q; ll a, b, c, d, idx, cnt; vl as, bs, cs, ds; vb2 block; vb visited; Graph graph; Graph tree; /* read_graph(n, m, graph, cost, directed, offset) cost: if link cost is fixed, put positive integer otherwise -1 directed: if directed true, olse false offset: if the given vertex is 1-indexed, -1. */ void solve() { // generate_array(size, min, max); ll a0; map<ll, vpl> bs; map<ll, vl> vs; in3(n, a0, d); if (d == 0) { if (a0 == 0) prl(1); else prl(n); } if (d < 0) d = -d, a0 = -a0; rep(i, n + 1) { ll c1, c2; c1 = i * (i + 1) - n * (n + 1) / 2; c2 = n * (n + 1) / 2 - (n - i) * (n - i + 1); c1 = (a0 - d) * (2 * i - n) + c1 * d; c2 = (a0 - d) * (2 * i - n) + c2 * d; ll key = (2 * d + (c1 % (2 * d))) % (2 * d); bs[key].pb({c1, c2 + 1}); vs[key].pb(c1); vs[key].pb(c2 + 1); } answer = 0; for (auto &kv : vs) { vl &v = kv.e2; sort(all(v)); v.erase(unique(all(v)), v.end()); map<ll, ll> vmp = dict(compress(v)); ll sz = v.size(); BinaryIndexedTree bit(sz + 5); for (auto &bound : bs[kv.e1]) { ll lb, ub; tie(lb, ub) = bound; bit.update(vmp[lb], 1); bit.update(vmp[ub], -1); } ll cur = bit.query(0), nxt; ll idx = 0; rep(i, sz - 1) { nxt = bit.query(i + 1); if (cur > 0 && nxt == 0) { answer += (v[i + 1] - v[idx]) / 2 / d + 1; cur = 0; idx = i + 1; } else if (cur == 0 && nxt > 0) cur = nxt, idx = i + 1; } } prl(answer); // ### DEBUG PART ### auto naive_solver = [&]() { vl as(n); as[0] = a0; rep(i, n - 1) as[i + 1] = as[i] + d; // map<ll, set<ll>> ss; set<ll> ss; rep(bit, 1LL << n) { ll num = 0; rep(i, n) { if (1LL << i & bit) num += as[i]; else num -= as[i]; } // ss[(2 * d + num % (2 * d)) % (2 * d)].insert(num); ss.insert(num); } prl("\nNaive Solution"); // 13 14 20 /* for (auto& p: ss){ prl3("#####", p.e1, p.e2.size()); debug(bs[p.e1]); debug(p.e2); } */ // debug(ss); prl(ss.size()); }; #ifdef _LOCAL // naive_solver(); #endif } void test(ll num = 0, bool verbose = false) { rep(i, max(1LL, num)) { ll t = clock(); if (verbose) prl4("\n#####", i + 1, "#####", "\n## Answer ##"); solve(); if (verbose) { prl(""); prl_time(t); } } } int main(void) { #ifdef _LOCAL test(3, true); #else test(0, false); #endif return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll MOD = 1000000007; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-12; struct mint { ll x; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } using edge = struct { ll to; ll cost; }; struct fpoint { ld x; ld y; bool operator<(const fpoint &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const fpoint &p) const { if (p.x - EPS < x && x < p.x + EPS && p.y - EPS < y && y < p.y + EPS) return true; return false; } bool operator!=(const fpoint &p) const { if (p.x - EPS > x || x > p.x + EPS || p.y - EPS > y || y > p.y + EPS) return true; return false; } fpoint &operator+=(const ld a) { x += a, y += a; return *this; } fpoint &operator-=(const ld a) { x -= a, y -= a; return *this; } fpoint &operator*=(const ld a) { x *= a, y *= a; return *this; } fpoint &operator/=(const ld a) { x /= a, y /= a; return *this; } fpoint &operator+=(const fpoint p) { x += p.x, y += p.y; return *this; } fpoint &operator-=(const fpoint p) { x -= p.x, y -= p.y; return *this; } fpoint &operator*=(const fpoint p) { x *= p.x, y *= p.y; return *this; } fpoint &operator/=(const fpoint p) { x /= p.x, y /= p.y; return *this; } bool is_zero() { fpoint z = {0, 0}; return z == *this; } fpoint operator+(const fpoint p) const { return fpoint(*this) += p; } fpoint operator-(const fpoint p) const { return fpoint(*this) -= p; } fpoint operator*(const fpoint p) const { return fpoint(*this) *= p; } fpoint operator/(const fpoint p) const { return fpoint(*this) /= p; } fpoint operator+(const ld a) const { return fpoint(*this) += a; } fpoint operator-(const ld a) const { return fpoint(*this) -= a; } fpoint operator*(const ld a) const { return fpoint(*this) *= a; } fpoint operator/(const ld a) const { return fpoint(*this) /= a; } }; struct point { ll x; ll y; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const point &p) const { if (x == p.x && y == p.y) return true; return false; } bool operator!=(const point &p) const { if (x != p.x || y != p.y) return true; return false; } point &operator+=(const ll a) { x += a, y += a; return *this; } point &operator-=(const ll a) { x -= a, y -= a; return *this; } point &operator*=(const ll a) { x *= a, y *= a; return *this; } point &operator+=(const point p) { x += p.x, y += p.y; return *this; } point &operator-=(const point p) { x -= p.x, y -= p.y; return *this; } point &operator*=(const point p) { x *= p.x, y *= p.y; return *this; } bool is_zero() { point z = {0, 0}; return z == *this; } fpoint to_fpoint() { fpoint ret = {ld(x), ld(y)}; return ret; } void operator++(int) { x++, y++; } void operator++() { x++, y++; } void operator--(int) { x--, y--; } void operator--() { x--, y--; } point operator+(const point p) const { return point(*this) += p; } point operator-(const point p) const { return point(*this) -= p; } point operator*(const point p) const { return point(*this) *= p; } point operator+(const ll a) const { return point(*this) += a; } point operator-(const ll a) const { return point(*this) -= a; } point operator*(const ll a) const { return point(*this) *= a; } }; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<mint> vmi; typedef std::vector<vmi> vmi2; typedef std::vector<vmi2> vmi3; typedef std::vector<vmi3> vmi4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; typedef std::vector<fpoint> fpoints; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const long double PI = 3.14159265358979323846; points dirs = { {-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); 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"); } string to_string(mint a) { return to_string(a.x); } string to_string(point p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } string to_string(fpoint p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } 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; } template <typename T> string to_string(priority_queue<T> &q) { priority_queue<T> copy; bool first = true; string res = "{"; while (!q.empty()) { if (!first) { res += ", "; } first = false; res += to_string(q.top()); copy.push(q.top()); q.pop(); } swap(q, copy); res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) std::cout << (a) #define prl(a) std::cout << (a) << endl #define prl2(a, b) std::cout << (a) << " " << (b) << endl #define prl3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) std::cout << (a) << " " #define prs2(a, b) std::cout << (a) << " " << (b) << " " #define prs3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define ltos(n) to_string((n)) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ std::cout << fixed << setprecision(digits) << (num) << endl; #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #ifdef _LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow. if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; struct UnionFind { vl parents, set_size; set<ll> root_idx; ll n_groups; UnionFind(ll n) { parents = set_size = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); n_groups--; } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; class LowestCommonAncestor { public: ll N, logN; vl depth, len; Graph tree; vl2 parents; LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = len = vl(N); parents = vl2(logN, vl(N)); tree = Graph(N); input(N, offset, is_weighted); init(0, -1, 0, 0); build(); } void add_edge(ll from, ll to, ll dist) { tree[from].pb({to, dist}); tree[to].pb({from, dist}); } void input(ll n, ll offset = -1, bool is_weighted = true) { rep(i, n - 1) { ll a, b, d = 1; in2(a, b); a += offset, b += offset; if (is_weighted) in1(d); add_edge(a, b, d); } } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, d); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; // This is the exaple of the number of children static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } ll nPk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; return val % mod; } }; struct PowerMemo { ll sz, mod, base; vl powB; PowerMemo(ll sz, ll base, ll _mod) : sz(sz), base(base), mod(_mod) { powB.resize(sz + 5); init(); } void init() { powB[0] = 1; rep(i, sz + 3) powB[i + 1] = (powB[i] * base) % mod; } ll operator[](ll k) { return powB[k]; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point nxt = p + dirs[i]; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; struct Cumulative2D { vl2 cum; ll w, h; Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); } template <typename T> void build(vector<T> &vec) { // never forget building rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; } ll func(ll x, ll y, ll dx, ll dy) { // 1-indexed // the rectangle of (x, y), (x + dx, y), (x, y + dy) and (x + dx, y + dy) // think about the case of (1, 1, 1, 1). if (x + dx > w || y + dy > h) return -INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n + 1, vl(n + 1)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } mll prime_factorization(ll n) { ll i = 2; mll table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll next_combination(ll sub) { /* nCk ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; if (x != 0) return (((sub & ~y) / x) >> 1) | y; else return INF; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F template <typename T> vl z_algorithm(T &s, ll n) { /* Paramters --------- T: the string or list of interest n: the size of string or list Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } ll big_number_compare(str s1, str s2) { if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else if (s1 == s2) return 0; return 2 * (s1 > s2) - 1; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } template <typename T> bool isColinear(T p1, T p2, T p3) { T v1 = p2 - p1, v2 = p3 - p1; return v1.x * v2.y == v1.y * v2.x; } template <typename T> T PerpendicularBisector(T p1, T p2) { T vec = p2 - p1; assert(!vec.is_zero()); T ret = {vec.y, -vec.x}; return ret; } template <typename T> ld Distance2DPoints(T p1, T p2) { T vec = (p1 - p2) * (p1 - p2); return sqrt(vec.x + vec.y); } ll SquaredDistance2DPoints(point p1, point p2) { point vec = (p1 - p2) * (p1 - p2); return vec.x + vec.y; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> OuterCenter(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value point ret; if (isColinear(p1, p2, p3)) { ll d1 = SquaredDistance2DPoints(p1, p2); ll d2 = SquaredDistance2DPoints(p2, p3); ll d3 = SquaredDistance2DPoints(p3, p1); if (d1 >= d2 && d1 >= d3) { ret = p1 + p2; return mp(ret, 2); } else if (d2 >= d1 && d2 >= d3) { ret = p2 + p3; return mp(ret, 2); } else { ret = p3 + p1; return mp(ret, 2); } } point pv1 = PerpendicularBisector(p1, p2); point pv2 = PerpendicularBisector(p1, p3); point cv1_2x = p1 + p2, cv2_2x = p1 + p3; // cv1 + k pv1 == cv2 + m pv2 // (pv1x -pv2x) (k) = (cv2x - cv1x) // (pv1y -pv2y) (m) = (cv2y - cv1y) ll det_inv = -pv1.x * pv2.y + pv1.y * pv2.x; ll x1_2x = cv2_2x.x - cv1_2x.x, x2_2x = cv2_2x.y - cv1_2x.y; pl c_2x_det = {-pv2.y * x1_2x + pv2.x * x2_2x, -pv1.y * x1_2x + pv1.x * x2_2x}; // ret.x = ld(cv1_2x.x * det_inv + pv1.x * c_2x_det.e1) / ld(2 * det_inv); // ret.y = ld(cv1_2x.y * det_inv + pv1.y * c_2x_det.e1) / ld(2 * det_inv); ret.x = cv1_2x.x * det_inv + pv1.x * c_2x_det.e1; ret.y = cv1_2x.y * det_inv + pv1.y * c_2x_det.e1; ll jacobian = 2 * det_inv; return mp(ret, jacobian); } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value. val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } ld bin_search(ld left, ld right, bool lb, function<bool(ld)> judge) { ld mid; while (right - left > EPS) { mid = (right + left) / 2; if (lb) { if (judge(mid)) right = mid; else left = mid + EPS; } else { if (judge(mid)) left = mid; else right = mid - EPS; } } return right; } ll bin_search(ll left, ll right, bool lb, function<bool(ll)> judge) { ll mid; while (right > left) { if (lb) { // if true (satisfies the condition), range shifts smaller direction mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; } else { // if true (satisfies the condition), range shitfs larger direction mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } } return right; } ld trinary_search(ld left, ld right, function<ld(ld)> func) { // Care the value EPS!!! Compare to the condition while (abs(right - left) > EPS) { ld left2 = (2.0 * left + right) / 3.0; ld right2 = (left + 2.0 * right) / 3.0; ld f1 = func(left2); ld f2 = func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } return right; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { // O(ElogE) sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v) { ll n = v.size(); vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> edge_costs; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { ll idx, cost; tie(cost, idx) = edge_costs.top(); edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] + e.cost) { dist[e.to] = dist[idx] + e.cost; if (trace) vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void Euler_Tour(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record only when we first reach a node and leave the node. ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); } euler_tour.pb(-source); out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } void Euler_Tour2(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record everytime we reach a node ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); euler_tour.pb(source); } out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } std::mt19937 create_rand_engine() { std::random_device rnd; std::vector<std::uint_least32_t> v(10); std::generate(v.begin(), v.end(), std::ref(rnd)); std::seed_seq seed(v.begin(), v.end()); return std::mt19937(seed); } vl generate_unique_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); const size_t num = static_cast<size_t>(sz * 1.2); assert(vm <= vM); assert(sz <= range); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) { while (ret.size() < num) ret.pb(distribution(engine)); sort(all(ret)); auto unique_end = unique(all(ret)); if (sz < distance(ret.begin(), unique_end)) unique_end = next(ret.begin(), sz); ret.erase(unique_end, ret.end()); } return ret; } vl generate_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); assert(vm <= vM); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) ret.pb(distribution(engine)); return ret; } void read_graph(ll n_vertex, ll n_edges, Graph &graph, ll cost = -1, bool directed = false, ll offset = -1) { graph.resize(n_vertex); rep(i, n_edges) { ll from, to, c = cost; if (cost == -1) in3(from, to, c); else in2(from, to); from += offset, to += offset; graph[from].pb({to, cost}); if (!directed) graph[to].pb({from, cost}); } } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } void read_pair_vector(ll n, vpl &v, ll offset1 = 0, ll offset2 = 0, bool rev = false) { v.resize(n); rep(i, n) { ll a, b; in2(a, b); a += offset1, b += offset2; if (!rev) v[i] = {a, b}; else v[i] = {b, a}; } } void read_2DMap(ll w, ll h, vb2 &block, char b) { block = vb2(w, vb(h, false)); str s; rep(y, h) { in1(s); rep(x, w) block[x][y] = (s[x] == b); } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ if (l == r) r++; while(r < n && cond) r++; l++; } prl(answer); #11. bfs ABC146D, ABC007C 1. first create a tree. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = graph[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = graph[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } */ /* x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & ~ x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 ####### Attention ####### S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & ~(1 << i) -> S - {i} __builtin_popcountl(i) -> the number of 1 in binary #Conditional Operator condition ? true : false; # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; const size_t MAX_BIT = 12000; typedef bitset<MAX_BIT> bts; typedef vector<bts> vbt; typedef vector<vbt> vbt2; typedef vector<vbt2> vbt3; bool okay = false; ll answer = 0; str s, t, u; ll n, m, h, w, k, Q; ll a, b, c, d, idx, cnt; vl as, bs, cs, ds; vb2 block; vb visited; Graph graph; Graph tree; /* read_graph(n, m, graph, cost, directed, offset) cost: if link cost is fixed, put positive integer otherwise -1 directed: if directed true, olse false offset: if the given vertex is 1-indexed, -1. */ void solve() { // generate_array(size, min, max); ll a0; map<ll, vpl> bs; map<ll, vl> vs; in3(n, a0, d); if (d == 0) { if (a0 == 0) prl(1); else prl(n + 1); return; } if (d < 0) d = -d, a0 = -a0; rep(i, n + 1) { ll c1, c2; c1 = i * (i + 1) - n * (n + 1) / 2; c2 = n * (n + 1) / 2 - (n - i) * (n - i + 1); c1 = (a0 - d) * (2 * i - n) + c1 * d; c2 = (a0 - d) * (2 * i - n) + c2 * d; ll key = (2 * d + (c1 % (2 * d))) % (2 * d); bs[key].pb({c1, c2 + 1}); vs[key].pb(c1); vs[key].pb(c2 + 1); } answer = 0; for (auto &kv : vs) { vl &v = kv.e2; sort(all(v)); v.erase(unique(all(v)), v.end()); map<ll, ll> vmp = dict(compress(v)); ll sz = v.size(); BinaryIndexedTree bit(sz + 5); for (auto &bound : bs[kv.e1]) { ll lb, ub; tie(lb, ub) = bound; bit.update(vmp[lb], 1); bit.update(vmp[ub], -1); } ll cur = bit.query(0), nxt; ll idx = 0; rep(i, sz - 1) { nxt = bit.query(i + 1); if (cur > 0 && nxt == 0) { answer += (v[i + 1] - v[idx]) / 2 / d + 1; cur = 0; idx = i + 1; } else if (cur == 0 && nxt > 0) cur = nxt, idx = i + 1; } } prl(answer); // ### DEBUG PART ### auto naive_solver = [&]() { vl as(n); as[0] = a0; rep(i, n - 1) as[i + 1] = as[i] + d; // map<ll, set<ll>> ss; set<ll> ss; rep(bit, 1LL << n) { ll num = 0; rep(i, n) { if (1LL << i & bit) num += as[i]; else num -= as[i]; } // ss[(2 * d + num % (2 * d)) % (2 * d)].insert(num); ss.insert(num); } prl("\nNaive Solution"); // 13 14 20 /* for (auto& p: ss){ prl3("#####", p.e1, p.e2.size()); debug(bs[p.e1]); debug(p.e2); } */ // debug(ss); prl(ss.size()); }; #ifdef _LOCAL // naive_solver(); #endif } void test(ll num = 0, bool verbose = false) { rep(i, max(1LL, num)) { ll t = clock(); if (verbose) prl4("\n#####", i + 1, "#####", "\n## Answer ##"); solve(); if (verbose) { prl(""); prl_time(t); } } } int main(void) { #ifdef _LOCAL test(3, true); #else test(0, false); #endif return 0; }
replace
2,026
2,027
2,026
2,028
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll mod = 1e9 + 7; int main() { ll N, X, D; cin >> N >> X >> D; ll ans = 0; ll p = D / gcd(X, D); for (ll M = 0; M <= N; M++) { // cout << ans << endl; if (M < p) { ans += ((N * (N - 1) / 2 - (N - M) * (N - M - 1)) + (N * (N - 1) / 2 - M * (M - 1))) / 2 + 1; continue; } ll l = -N * (N - 1) / 2 + M * (M - 1); ll r = N * (N - 1) / 2 - (N - M) * (N - M - 1); ll lp = -N * (N - 1) / 2 + (M - p) * (M - p - 1) - 2 * X * p / D; ll rp = N * (N - 1) / 2 - (N - M + p) * (N - M + p - 1) - 2 * X * p / D; if (r < lp || l > rp) ans += (r - l) / 2 + 1; else { if (l <= rp && rp <= r) ans += (r - rp) / 2; if (l <= lp && lp <= r) ans += (lp - l) / 2; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll mod = 1e9 + 7; int main() { ll N, X, D; cin >> N >> X >> D; if (D == 0) { if (X == 0) cout << 1 << endl; else cout << (N + 1) << endl; return 0; } ll ans = 0; ll p = D / gcd(X, D); for (ll M = 0; M <= N; M++) { // cout << ans << endl; if (M < p) { ans += ((N * (N - 1) / 2 - (N - M) * (N - M - 1)) + (N * (N - 1) / 2 - M * (M - 1))) / 2 + 1; continue; } ll l = -N * (N - 1) / 2 + M * (M - 1); ll r = N * (N - 1) / 2 - (N - M) * (N - M - 1); ll lp = -N * (N - 1) / 2 + (M - p) * (M - p - 1) - 2 * X * p / D; ll rp = N * (N - 1) / 2 - (N - M + p) * (N - M + p - 1) - 2 * X * p / D; if (r < lp || l > rp) ans += (r - l) / 2 + 1; else { if (l <= rp && rp <= r) ans += (r - rp) / 2; if (l <= lp && lp <= r) ans += (lp - l) / 2; } } cout << ans << endl; return 0; }
insert
15
15
15
22
0
p02840
C++
Runtime Error
//----------------------------templates #pragma GCC optimize("Ofast") #pragma GCC target("tune=native") #pragma GCC target("avx") //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- int n, x, d, ret; signed main() { cin >> n >> x >> d; map<int, vpi> rem; REP(i, n + 1) { int ama = x * i % d; if (ama < 0) ama += d; int sho = (x * i - ama) / d; // 0, 1, ..., i-1 int mini = i * (i - 1) / 2; // (n-i), ..., n-1 int maxi = i * (2 * n - 1 - i) / 2; rem[ama].eb(mini + sho, maxi + sho + 1); // cerr << ama << " : [" << mini+sho << ", " << maxi+sho+1 << ")\n"; } for (auto &p : rem) { auto event = p.second; sort(all(event)); int lev = sz(event); int mini = INT64_MIN + 2; int maxi = INT64_MIN + 2; for (auto ev : event) { if (ev.first > maxi) { ret += maxi - mini; mini = ev.first; maxi = ev.second; } else { maxi = max(maxi, ev.second); } } ret += maxi - mini; } cout << ret << endl; }
//----------------------------templates #pragma GCC optimize("Ofast") #pragma GCC target("tune=native") #pragma GCC target("avx") //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- int n, x, d, ret; signed main() { cin >> n >> x >> d; map<int, vpi> rem; if (!d) { cout << (x ? n + 1 : 1) << endl; return 0; } REP(i, n + 1) { int ama = x * i % d; if (ama < 0) ama += d; int sho = (x * i - ama) / d; // 0, 1, ..., i-1 int mini = i * (i - 1) / 2; // (n-i), ..., n-1 int maxi = i * (2 * n - 1 - i) / 2; rem[ama].eb(mini + sho, maxi + sho + 1); // cerr << ama << " : [" << mini+sho << ", " << maxi+sho+1 << ")\n"; } for (auto &p : rem) { auto event = p.second; sort(all(event)); int lev = sz(event); int mini = INT64_MIN + 2; int maxi = INT64_MIN + 2; for (auto ev : event) { if (ev.first > maxi) { ret += maxi - mini; mini = ev.first; maxi = ev.second; } else { maxi = max(maxi, ev.second); } } ret += maxi - mini; } cout << ret << endl; }
insert
47
47
47
51
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define repl(i, s, n) for (int i = s; i <= n; ++i) #define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i)) #define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r, v) auto r = (v) #else #define aut(r, v) __typeof(v) r = (v) #endif #define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it) #define ktya(x) sort(all(x)) #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define all(aa) (aa).begin(), (aa).end() #define pb(x) push_back(x) #define mp(x, y) make_pair((x), (y)) #define mset(m, v) memset(m, v, sizeof(m)) #define INF 1e18 #define INFLL 1000000000000000007LL #define SIZE 200105 #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define mind(a, b) (a > b ? b : a) #define maxd(a, b) (a > b ? a : b) #define PI (acos(-1)) typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpii; typedef long long ll; typedef pair<int, ll> pill; typedef pair<ll, int> plli; typedef pair<double, int> pdi; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } typedef complex<double> P; typedef pair<pill, int> PA; // ll MOD; ll MOD = 1000000007; // ll MOD=998244353; typedef ll Weight; struct Edge { int src, dst; ll weight; }; int N; ll X, D; ll gcdl(ll a, ll b) { if (a < b) swap(a, b); if (b == 0) return a; return gcdl(b, a % b); } vector<pair<ll, ll>> datas[214514]; int main() { cin >> N >> X >> D; ll A = X - D; ll G = gcdl(abs(A), abs(D)); ll x = D / G, y = A / G; // y*D=x*A if (x < 0) { x = -x; y = -y; } if (x < N) { ll ans = 0; ll lef = 0, rig = 0; datas[0].pb(mp((ll)0, (ll)0)); rep(i, N) { lef += (ll)(i + 1); rig += (ll)(N - i); int anum = i + 1; int sho = anum / x; datas[anum % x].pb(mp(lef + y * sho, rig + y * sho)); } rep(i, x) { sort(all(datas[i])); ll lef = datas[i][0].first, rig = datas[i][0].second; rep(j, datas[i].size()) { ll cl = datas[i][j].first, cr = datas[i][j].second; if (cr < lef || rig < cl) { ans += rig - lef + 1; lef = cl; rig = cr; } else { lef = min(lef, cl); rig = max(rig, cr); } } ans += rig - lef + 1; } cout << ans << endl; } else { ll ans = 1; ll lef = 0, rig = 0; rep(i, N) { lef += (ll)(i + 1); rig += (ll)(N - i); ans += (rig - lef + 1); } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define repl(i, s, n) for (int i = s; i <= n; ++i) #define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i)) #define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r, v) auto r = (v) #else #define aut(r, v) __typeof(v) r = (v) #endif #define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it) #define ktya(x) sort(all(x)) #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define all(aa) (aa).begin(), (aa).end() #define pb(x) push_back(x) #define mp(x, y) make_pair((x), (y)) #define mset(m, v) memset(m, v, sizeof(m)) #define INF 1e18 #define INFLL 1000000000000000007LL #define SIZE 200105 #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define mind(a, b) (a > b ? b : a) #define maxd(a, b) (a > b ? a : b) #define PI (acos(-1)) typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpii; typedef long long ll; typedef pair<int, ll> pill; typedef pair<ll, int> plli; typedef pair<double, int> pdi; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } typedef complex<double> P; typedef pair<pill, int> PA; // ll MOD; ll MOD = 1000000007; // ll MOD=998244353; typedef ll Weight; struct Edge { int src, dst; ll weight; }; int N; ll X, D; ll gcdl(ll a, ll b) { if (a < b) swap(a, b); if (b == 0) return a; return gcdl(b, a % b); } vector<pair<ll, ll>> datas[214514]; int main() { cin >> N >> X >> D; if (D == 0) { if (X != 0) cout << N + 1 << endl; else cout << 1 << endl; return 0; } ll A = X - D; ll G = gcdl(abs(A), abs(D)); ll x = D / G, y = A / G; // y*D=x*A if (x < 0) { x = -x; y = -y; } if (x < N) { ll ans = 0; ll lef = 0, rig = 0; datas[0].pb(mp((ll)0, (ll)0)); rep(i, N) { lef += (ll)(i + 1); rig += (ll)(N - i); int anum = i + 1; int sho = anum / x; datas[anum % x].pb(mp(lef + y * sho, rig + y * sho)); } rep(i, x) { sort(all(datas[i])); ll lef = datas[i][0].first, rig = datas[i][0].second; rep(j, datas[i].size()) { ll cl = datas[i][j].first, cr = datas[i][j].second; if (cr < lef || rig < cl) { ans += rig - lef + 1; lef = cl; rig = cr; } else { lef = min(lef, cl); rig = max(rig, cr); } } ans += rig - lef + 1; } cout << ans << endl; } else { ll ans = 1; ll lef = 0, rig = 0; rep(i, N) { lef += (ll)(i + 1); rig += (ll)(N - i); ans += (rig - lef + 1); } cout << ans << endl; } }
insert
67
67
67
74
0
p02840
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, a, b) for (ll i = (a); i < (b); i++) #define chmax(x, v) \ do { \ x = max(x, v); \ } while (0) typedef uint64_t ull; typedef int64_t ll; typedef std::pair<ll, ll> PLL; using namespace std; ll N, X, D; // k個の数字を選んだ時に、合計値として有り得る値の範囲 // 0 <= k <= N PLL range(ll k) { PLL res(k * X, k * X); for (ll i = 0; i <= k - 1; i++) res.first += i * D; for (ll i = N - k; i <= N - 1; i++) res.second += i * D; return res; } map<ll, vector<PLL>> mp; signed main() { cin >> N >> X >> D; if (D == 0) { if (X == 0) { cout << 1 << endl; // S=0のみ return 0; } else { cout << N + 1 << endl; // S=0,X,2X,..,NX return 0; } } if (D < 0) { X *= -1; D *= -1; } // D > 0 rep(k, N + 1) { ll m = (k * X) % D; // 余り m = (m + 3 * D) % D; auto r = range(k); // printf("k=%d min=%d max=%d m=%d mod_mid=%d mod_max=%d\n", // k, r.first, r.second, m, // (r.first - m)/D, (r.second - m)/D); mp[m].emplace_back((r.first - m) / D, (r.second - m) / D); } // swap(mp[0][0],mp[0][1]); // for (auto p: mp[0]) { // printf("mod_min=%d mod_max=%d\n", // p.first, p.second); // } ll ans = 0; for (auto e : mp) { ll m = e.first; auto ranges = mp[m]; sort(begin(ranges), end(ranges)); rep(i, ranges.size()) { // printf("mod_min=%d mod_max=%d\n", ranges[i].first, ranges[i].second); } vector<PLL> merged_ranges; merged_ranges.push_back(ranges[0]); for (ll i = 1; i < ranges.size(); i++) { auto r = ranges[i]; auto last_r = merged_ranges[merged_ranges.size() - 1]; if (r.first <= last_r.second + 1) { chmax(merged_ranges[merged_ranges.size() - 1].second, r.second); } else { merged_ranges.push_back(r); } } for (auto mr : merged_ranges) { // printf("mr_min=%d mr_max=%d\n", mr.first, mr.second); ans += mr.second - mr.first + 1; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, a, b) for (ll i = (a); i < (b); i++) #define chmax(x, v) \ do { \ x = max(x, v); \ } while (0) typedef uint64_t ull; typedef int64_t ll; typedef std::pair<ll, ll> PLL; using namespace std; ll N, X, D; // k個の数字を選んだ時に、合計値として有り得る値の範囲 // 0 <= k <= N PLL range(ll k) { PLL res(k * X, k * X); if (k == N) { res.first += ((N - 1) * N / 2) * D; res.second += ((N - 1) * N / 2) * D; } else { res.first += ((k - 1) * k / 2) * D; res.second += ((2 * N - k - 1) * k / 2) * D; } // for (ll i=0; i<=k-1; i++) // res.first += i * D; // for (ll i=N-k; i<=N-1; i++) // res.second += i * D; return res; } map<ll, vector<PLL>> mp; signed main() { cin >> N >> X >> D; if (D == 0) { if (X == 0) { cout << 1 << endl; // S=0のみ return 0; } else { cout << N + 1 << endl; // S=0,X,2X,..,NX return 0; } } if (D < 0) { X *= -1; D *= -1; } // D > 0 rep(k, N + 1) { ll m = (k * X) % D; // 余り m = (m + 3 * D) % D; auto r = range(k); // printf("k=%d min=%d max=%d m=%d mod_mid=%d mod_max=%d\n", // k, r.first, r.second, m, // (r.first - m)/D, (r.second - m)/D); mp[m].emplace_back((r.first - m) / D, (r.second - m) / D); } // swap(mp[0][0],mp[0][1]); // for (auto p: mp[0]) { // printf("mod_min=%d mod_max=%d\n", // p.first, p.second); // } ll ans = 0; for (auto e : mp) { ll m = e.first; auto ranges = mp[m]; sort(begin(ranges), end(ranges)); rep(i, ranges.size()) { // printf("mod_min=%d mod_max=%d\n", ranges[i].first, ranges[i].second); } vector<PLL> merged_ranges; merged_ranges.push_back(ranges[0]); for (ll i = 1; i < ranges.size(); i++) { auto r = ranges[i]; auto last_r = merged_ranges[merged_ranges.size() - 1]; if (r.first <= last_r.second + 1) { chmax(merged_ranges[merged_ranges.size() - 1].second, r.second); } else { merged_ranges.push_back(r); } } for (auto mr : merged_ranges) { // printf("mr_min=%d mr_max=%d\n", mr.first, mr.second); ans += mr.second - mr.first + 1; } } cout << ans << endl; return 0; }
replace
20
24
20
32
TLE
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using int32 = int_fast32_t; using int64 = int_fast64_t; const int32 INF = 1e9; const int32 MOD = 1e9 + 7; const int64 LLINF = 1e18; #define YES(n) cout << ((n) ? "YES\n" : "NO\n") #define Yes(n) cout << ((n) ? "Yes\n" : "No\n") #define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n") #define ANS(n) cout << (n) << "\n" #define REP(i, n) for (int64 i = 0; i < (n); ++i) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define FORR(i, a, b) for (int64 i = (a); i >= (b); i--) #define ALL(obj) (obj).begin(), (obj).end() #define pii pair<int32, int32> #define pll pair<int64, int64> #define pb(a) push_back(a) int main() { cin.tie(0); ios::sync_with_stdio(false); int64 n, x, d; cin >> n >> x >> d; map<int32, vector<pll>> mp; REP(k, n + 1) { int32 idx = ((k * x) % d + d) % d; mp[idx].emplace_back(((k * x - idx) / d + k * (k - 1) / 2), ((k * x - idx) / d + k * (2 * n - k - 1) / 2)); } int64 ans = 0; for (auto p : mp) { // cout << p.first << endl; // for(auto q : p.second){ // cout << q.first << " " << q.second << endl; // } // cout << endl; vector<pll> evt; for (auto q : p.second) { evt.emplace_back(q.first, 1); evt.emplace_back(q.second + 1, -1); } sort(ALL(evt)); int32 cur = 0; int32 pre = -INF; for (auto q : evt) { if (cur) ans += q.first - pre; pre = q.first; cur += q.second; } } ANS(ans); return 0; }
#include <bits/stdc++.h> using namespace std; using int32 = int_fast32_t; using int64 = int_fast64_t; const int32 INF = 1e9; const int32 MOD = 1e9 + 7; const int64 LLINF = 1e18; #define YES(n) cout << ((n) ? "YES\n" : "NO\n") #define Yes(n) cout << ((n) ? "Yes\n" : "No\n") #define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n") #define ANS(n) cout << (n) << "\n" #define REP(i, n) for (int64 i = 0; i < (n); ++i) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define FORR(i, a, b) for (int64 i = (a); i >= (b); i--) #define ALL(obj) (obj).begin(), (obj).end() #define pii pair<int32, int32> #define pll pair<int64, int64> #define pb(a) push_back(a) int main() { cin.tie(0); ios::sync_with_stdio(false); int64 n, x, d; cin >> n >> x >> d; if (d == 0) { if (x != 0) { ANS(n + 1); } else { ANS(1); } return 0; } map<int32, vector<pll>> mp; REP(k, n + 1) { int32 idx = ((k * x) % d + d) % d; mp[idx].emplace_back(((k * x - idx) / d + k * (k - 1) / 2), ((k * x - idx) / d + k * (2 * n - k - 1) / 2)); } int64 ans = 0; for (auto p : mp) { // cout << p.first << endl; // for(auto q : p.second){ // cout << q.first << " " << q.second << endl; // } // cout << endl; vector<pll> evt; for (auto q : p.second) { evt.emplace_back(q.first, 1); evt.emplace_back(q.second + 1, -1); } sort(ALL(evt)); int32 cur = 0; int32 pre = -INF; for (auto q : evt) { if (cur) ans += q.first - pre; pre = q.first; cur += q.second; } } ANS(ans); return 0; }
insert
27
27
27
37
0
p02840
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<ll, ll> P; int main() { int n; cin >> n; ll x, d; cin >> x >> d; ll l[200020], r[200020]; for (ll i = 0; i <= n; i++) { l[i] = (2 * i - n) * x - (ll)n * (n - 1) / 2 * d + i * (i - 1) * d; r[i] = (2 * i - n) * x + (ll)n * (n - 1) / 2 * d - (n - i) * (n - i - 1) * d; if (l[i] > r[i]) swap(l[i], r[i]); // cout<<l[i]<<" "<<r[i]<<endl; } map<ll, vector<P>> mp; d = abs(d); for (int i = 0; i <= n; i++) { ll s = (l[i] % (2 * d) + 2 * d) % (2 * d); mp[s].push_back({l[i], r[i]}); } for (auto &p : mp) sort(p.second.begin(), p.second.end()); ll ans = 0; for (auto p : mp) { ll y = p.second[0].first, z = p.second[0].second; for (int i = 1; i < p.second.size(); i++) { if (z < p.second[i].first) { ans += (z - y) / (2 * d) + 1; y = p.second[i].first, z = p.second[i].second; } else { z = max(z, p.second[i].second); } } ans += (z - y) / (2 * d) + 1; } cout << ans << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<ll, ll> P; int main() { int n; cin >> n; ll x, d; cin >> x >> d; if (d == 0) { if (x == 0) { cout << 1 << endl; } else { cout << n + 1 << endl; } return 0; } ll l[200020], r[200020]; for (ll i = 0; i <= n; i++) { l[i] = (2 * i - n) * x - (ll)n * (n - 1) / 2 * d + i * (i - 1) * d; r[i] = (2 * i - n) * x + (ll)n * (n - 1) / 2 * d - (n - i) * (n - i - 1) * d; if (l[i] > r[i]) swap(l[i], r[i]); // cout<<l[i]<<" "<<r[i]<<endl; } map<ll, vector<P>> mp; d = abs(d); for (int i = 0; i <= n; i++) { ll s = (l[i] % (2 * d) + 2 * d) % (2 * d); mp[s].push_back({l[i], r[i]}); } for (auto &p : mp) sort(p.second.begin(), p.second.end()); ll ans = 0; for (auto p : mp) { ll y = p.second[0].first, z = p.second[0].second; for (int i = 1; i < p.second.size(); i++) { if (z < p.second[i].first) { ans += (z - y) / (2 * d) + 1; y = p.second[i].first, z = p.second[i].second; } else { z = max(z, p.second[i].second); } } ans += (z - y) / (2 * d) + 1; } cout << ans << endl; return 0; }
insert
32
32
32
40
0
p02840
C++
Runtime Error
// #pragma GCC target("avx") // CPU 処理並列化 // #pragma GCC optimize("O3") // CPU 処理並列化 // #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす #include <algorithm> #include <bitset> #include <cassert> #include <complex> #include <deque> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unordered_set> #include <vector> using namespace std; const long long mod = 1000000007; const long long inf = mod * mod; const long long d2 = (mod + 1) / 2; const double EPS = 1e-10; const double INF = 1e+10; const double PI = acos(-1.0); const int C_SIZE = 3100000; const int UF_SIZE = 3100000; namespace { long long fact[C_SIZE]; long long finv[C_SIZE]; long long inv[C_SIZE]; long long Comb(int a, int b) { if (a < b || b < 0) return 0; return fact[a] * finv[b] % mod * finv[a - b] % mod; } void init_C(int n) { fact[0] = finv[0] = inv[1] = 1; for (int i = 2; i < n; i++) { inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod; } for (int i = 1; i < n; i++) { fact[i] = fact[i - 1] * i % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } long long pw(long long a, long long b) { if (a < 0LL) return 0; if (b < 0LL) return 0; long long ret = 1; while (b) { if (b % 2) ret = ret * a % mod; a = a * a % mod; b /= 2; } return ret; } long long pw_mod(long long a, long long b, long long M) { if (a < 0LL) return 0; if (b < 0LL) return 0; long long ret = 1; while (b) { if (b % 2) ret = ret * a % M; a = a * a % M; b /= 2; } return ret; } int pw_mod_int(int a, int b, int M) { if (a < 0) return 0; if (b < 0) return 0; int ret = 1; while (b) { if (b % 2) ret = (long long)ret * a % M; a = (long long)a * a % M; b /= 2; } return ret; } int ABS(int a) { return max(a, -a); } long long ABS(long long a) { return max(a, -a); } double ABS(double a) { return max(a, -a); } int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } int UF[UF_SIZE]; void init_UF(int n) { for (int i = 0; i < n; i++) UF[i] = -1; } int FIND(int a) { if (UF[a] < 0) return a; return UF[a] = FIND(UF[a]); } void UNION(int a, int b) { a = FIND(a); b = FIND(b); if (a == b) return; if (UF[a] > UF[b]) swap(a, b); UF[a] += UF[b]; UF[b] = a; } } // namespace // ここから編集しろ pair<long long, pair<long long, int>> ev[410000]; long long t[210000]; long long L[210000]; long long R[210000]; int main() { long long a, b, c; scanf("%lld%lld%lld", &a, &b, &c); for (int i = 0; i < a; i++) { t[i] = b + c * i; } for (int i = 0; i < a; i++) { L[i + 1] = L[i] + t[i]; R[i + 1] = R[i] + t[a - 1 - i]; } long long ret = 0; for (int i = 0; i <= a; i++) { long long left = (L[i] - R[a - i]); long long right = (R[i] - L[a - i]); if (left > right) swap(left, right); ev[i * 2] = make_pair((left % ABS(c * 2) + ABS(c * 2)) % ABS(c * 2), make_pair(left, 1)); ev[i * 2 + 1] = make_pair((right % ABS(c * 2) + ABS(c * 2)) % ABS(c * 2), make_pair(right + ABS(c * 2), -1)); // printf("%d: %lld %lld // %lld\n",(left%ABS(c*2)+ABS(c*2))%ABS(c*2),left,right+ABS(c*2)); } std::sort(ev, ev + a * 2 + 2); int cur = 0; for (int i = 0; i < a * 2 + 2; i++) { if (ev[i].second.second == 1) cur++; else cur--; if (cur > 0) { ret += (ev[i + 1].second.first - ev[i].second.first) / ABS(c * 2); } } printf("%lld\n", ret); }
// #pragma GCC target("avx") // CPU 処理並列化 // #pragma GCC optimize("O3") // CPU 処理並列化 // #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす #include <algorithm> #include <bitset> #include <cassert> #include <complex> #include <deque> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unordered_set> #include <vector> using namespace std; const long long mod = 1000000007; const long long inf = mod * mod; const long long d2 = (mod + 1) / 2; const double EPS = 1e-10; const double INF = 1e+10; const double PI = acos(-1.0); const int C_SIZE = 3100000; const int UF_SIZE = 3100000; namespace { long long fact[C_SIZE]; long long finv[C_SIZE]; long long inv[C_SIZE]; long long Comb(int a, int b) { if (a < b || b < 0) return 0; return fact[a] * finv[b] % mod * finv[a - b] % mod; } void init_C(int n) { fact[0] = finv[0] = inv[1] = 1; for (int i = 2; i < n; i++) { inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod; } for (int i = 1; i < n; i++) { fact[i] = fact[i - 1] * i % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } long long pw(long long a, long long b) { if (a < 0LL) return 0; if (b < 0LL) return 0; long long ret = 1; while (b) { if (b % 2) ret = ret * a % mod; a = a * a % mod; b /= 2; } return ret; } long long pw_mod(long long a, long long b, long long M) { if (a < 0LL) return 0; if (b < 0LL) return 0; long long ret = 1; while (b) { if (b % 2) ret = ret * a % M; a = a * a % M; b /= 2; } return ret; } int pw_mod_int(int a, int b, int M) { if (a < 0) return 0; if (b < 0) return 0; int ret = 1; while (b) { if (b % 2) ret = (long long)ret * a % M; a = (long long)a * a % M; b /= 2; } return ret; } int ABS(int a) { return max(a, -a); } long long ABS(long long a) { return max(a, -a); } double ABS(double a) { return max(a, -a); } int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } int UF[UF_SIZE]; void init_UF(int n) { for (int i = 0; i < n; i++) UF[i] = -1; } int FIND(int a) { if (UF[a] < 0) return a; return UF[a] = FIND(UF[a]); } void UNION(int a, int b) { a = FIND(a); b = FIND(b); if (a == b) return; if (UF[a] > UF[b]) swap(a, b); UF[a] += UF[b]; UF[b] = a; } } // namespace // ここから編集しろ pair<long long, pair<long long, int>> ev[410000]; long long t[210000]; long long L[210000]; long long R[210000]; int main() { long long a, b, c; scanf("%lld%lld%lld", &a, &b, &c); if (c == 0) { if (b == 0) { printf("1\n"); return 0; } printf("%lld\n", a + 1); return 0; } for (int i = 0; i < a; i++) { t[i] = b + c * i; } for (int i = 0; i < a; i++) { L[i + 1] = L[i] + t[i]; R[i + 1] = R[i] + t[a - 1 - i]; } long long ret = 0; for (int i = 0; i <= a; i++) { long long left = (L[i] - R[a - i]); long long right = (R[i] - L[a - i]); if (left > right) swap(left, right); ev[i * 2] = make_pair((left % ABS(c * 2) + ABS(c * 2)) % ABS(c * 2), make_pair(left, 1)); ev[i * 2 + 1] = make_pair((right % ABS(c * 2) + ABS(c * 2)) % ABS(c * 2), make_pair(right + ABS(c * 2), -1)); // printf("%d: %lld %lld // %lld\n",(left%ABS(c*2)+ABS(c*2))%ABS(c*2),left,right+ABS(c*2)); } std::sort(ev, ev + a * 2 + 2); int cur = 0; for (int i = 0; i < a * 2 + 2; i++) { if (ev[i].second.second == 1) cur++; else cur--; if (cur > 0) { ret += (ev[i + 1].second.first - ev[i].second.first) / ABS(c * 2); } } printf("%lld\n", ret); }
insert
124
124
124
132
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++) #define setp(n) fixed << setprecision(n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } #define ld long double #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll, ll> #define pi pair<int, int> #define all(a) (a.begin()), (a.end()) #define rall(a) (a.rbegin()), (a.rend()) #define fi first #define se second #define pb push_back #define mp make_pair #define ins insert using namespace std; /* Some Libraries */ //------------------------------------------------- int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, x, d; cin >> n >> x >> d; if (d == 0) { cout << n + 1 << endl; } else if (d < 0) { d = -d; x = -x; } unordered_map<ll, int> Map; vector<vector<pll>> rs; int idx = 0; FOR(i, 0, n) { ll X = x * i; ll m = X % d; ll os = X / d; if (!Map.count(m)) { rs.pb(vector<pll>()); Map[m] = idx++; } ll L = (ll)(i - 1) * i / 2 + os; ll R = (2 * n - i - 1) * i / 2 + os; rs[Map[m]].emplace_back(L, R); } ll ans = 0; for (auto r : rs) { sort(all(r)); ll now = r[0].fi; for (auto e : r) { if (now > e.se) continue; if (now >= e.fi) { ans += e.se - now + 1; } else { ans += e.se - e.fi + 1; } now = e.se + 1; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++) #define setp(n) fixed << setprecision(n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } #define ld long double #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll, ll> #define pi pair<int, int> #define all(a) (a.begin()), (a.end()) #define rall(a) (a.rbegin()), (a.rend()) #define fi first #define se second #define pb push_back #define mp make_pair #define ins insert using namespace std; /* Some Libraries */ //------------------------------------------------- int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, x, d; cin >> n >> x >> d; if (d == 0) { if (x != 0) cout << n + 1 << endl; if (x == 0) cout << 1 << endl; return 0; } else if (d < 0) { d = -d; x = -x; } unordered_map<ll, int> Map; vector<vector<pll>> rs; int idx = 0; FOR(i, 0, n) { ll X = x * i; ll m = X % d; ll os = X / d; if (!Map.count(m)) { rs.pb(vector<pll>()); Map[m] = idx++; } ll L = (ll)(i - 1) * i / 2 + os; ll R = (2 * n - i - 1) * i / 2 + os; rs[Map[m]].emplace_back(L, R); } ll ans = 0; for (auto r : rs) { sort(all(r)); ll now = r[0].fi; for (auto e : r) { if (now > e.se) continue; if (now >= e.fi) { ans += e.se - now + 1; } else { ans += e.se - e.fi + 1; } now = e.se + 1; } } cout << ans << endl; return 0; }
replace
51
52
51
56
0
p02840
C++
Runtime Error
#pragma region #include <bits/stdc++.h> using namespace std; /* #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; */ #define int long long #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define RREP(i, n) for (int i = (int)n - 1; i >= 0; i--) #define FOR(i, s, n) for (int i = s; i < (int)n; i++) #define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; i--) #define ALL(a) a.begin(), a.end() #define IN(a, x, b) (a <= x && x < b) template <class T> inline bool CHMAX(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool CHMIN(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <class T> inline void in(T &t) { cin >> t; } template <class T, class... Ts> inline void in(T &t, Ts &...ts) { cin >> t; in(ts...); } template <class T> inline void out(T t) { cout << t << endl; } template <class T, class... Ts> inline void out(T t, Ts... ts) { cout << t << " "; out(ts...); } template <typename T = int> vector<T> mv(size_t a) { return vector<T>(a); } template <typename T = int, typename... Ts> auto mv(size_t a, Ts... ts) { return vector<decltype(mv<T>(ts...))>(a, mv<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } constexpr long long INF = 1e18; template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { vector<T> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].getmod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr T com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr T fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; const int MOD = 1000000007; // const int MOD = 998244353; using mint = Fp<MOD>; BiCoef<mint> bc; // bc.init(500050); #pragma endregion signed main() { int N, X, D; in(N, X, D); int ans = 0; int right = 0; int left = 0; map<int, vector<pair<int, int>>> mp; // modDを区間で管理 mp[0].emplace_back(0, 0); REP(i, N) { right += N - i - 1; left += i; int mo = X * (i + 1) % D; int l = left + X * (i + 1) / D; int r = right + X * (i + 1) / D; mp[mo].emplace_back(l, r); } for (auto m : mp) { auto v = m.second; sort(ALL(v)); int l = v[0].first, r = v[0].second; FOR(i, 1, v.size()) { if (r < v[i].first) { ans += r - l + 1; l = v[i].first; r = v[i].second; } else { CHMAX(r, v[i].second); } } ans += r - l + 1; } out(ans); }
#pragma region #include <bits/stdc++.h> using namespace std; /* #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; */ #define int long long #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define RREP(i, n) for (int i = (int)n - 1; i >= 0; i--) #define FOR(i, s, n) for (int i = s; i < (int)n; i++) #define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; i--) #define ALL(a) a.begin(), a.end() #define IN(a, x, b) (a <= x && x < b) template <class T> inline bool CHMAX(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool CHMIN(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <class T> inline void in(T &t) { cin >> t; } template <class T, class... Ts> inline void in(T &t, Ts &...ts) { cin >> t; in(ts...); } template <class T> inline void out(T t) { cout << t << endl; } template <class T, class... Ts> inline void out(T t, Ts... ts) { cout << t << " "; out(ts...); } template <typename T = int> vector<T> mv(size_t a) { return vector<T>(a); } template <typename T = int, typename... Ts> auto mv(size_t a, Ts... ts) { return vector<decltype(mv<T>(ts...))>(a, mv<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } constexpr long long INF = 1e18; template <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // 二項係数ライブラリ template <class T> struct BiCoef { vector<T> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].getmod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr T com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr T fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr T inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr T finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; const int MOD = 1000000007; // const int MOD = 998244353; using mint = Fp<MOD>; BiCoef<mint> bc; // bc.init(500050); #pragma endregion signed main() { int N, X, D; in(N, X, D); int ans = 0; int right = 0; int left = 0; map<int, vector<pair<int, int>>> mp; // modDを区間で管理 mp[0].emplace_back(0, 0); REP(i, N) { right += N - i - 1; left += i; if (D) { int mo = X * (i + 1) % D; int l = left + X * (i + 1) / D; int r = right + X * (i + 1) / D; mp[mo].emplace_back(l, r); } else { int l = X * (i + 1); int r = X * (i + 1); mp[0].emplace_back(l, r); } } for (auto m : mp) { auto v = m.second; sort(ALL(v)); int l = v[0].first, r = v[0].second; FOR(i, 1, v.size()) { if (r < v[i].first) { ans += r - l + 1; l = v[i].first; r = v[i].second; } else { CHMAX(r, v[i].second); } } ans += r - l + 1; } out(ans); }
replace
177
181
177
187
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; // const ll MOD = 1000000007; const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-12; struct mint { ll x; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } struct edge { ll to, cost; tuple<ll, ll> get_value() { return make_tuple(to, cost); } }; struct BigInt { string num; ll mod(ll _mod) { ll l = num.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = num[l - 1 - idx] - '0'; val += (m * tenth) % _mod; val %= _mod; tenth *= 10; tenth %= _mod; idx++; } return val; } ll digits() { return num.length(); } bool operator<(BigInt &n2) const { if (num.length() < n2.digits()) return true; else if (num.length() > n2.digits()) return false; return num < n2.num; } bool operator>(BigInt &n2) const { if (num.length() > n2.digits()) return true; else if (num.length() < n2.digits()) return false; return num > n2.num; } }; struct fpoint { ld x = 0; ld y = 0; bool operator<(const fpoint &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const fpoint &p) const { if (p.x - EPS < x && x < p.x + EPS && p.y - EPS < y && y < p.y + EPS) return true; return false; } bool operator!=(const fpoint &p) const { if (p.x - EPS > x || x > p.x + EPS || p.y - EPS > y || y > p.y + EPS) return true; return false; } fpoint &operator+=(const ld a) { x += a, y += a; return *this; } fpoint &operator-=(const ld a) { x -= a, y -= a; return *this; } fpoint &operator*=(const ld a) { x *= a, y *= a; return *this; } fpoint &operator/=(const ld a) { x /= a, y /= a; return *this; } fpoint &operator+=(const fpoint p) { x += p.x, y += p.y; return *this; } fpoint &operator-=(const fpoint p) { x -= p.x, y -= p.y; return *this; } fpoint &operator*=(const fpoint p) { x *= p.x, y *= p.y; return *this; } fpoint &operator/=(const fpoint p) { x /= p.x, y /= p.y; return *this; } fpoint operator+(const fpoint p) const { return fpoint(*this) += p; } fpoint operator-(const fpoint p) const { return fpoint(*this) -= p; } fpoint operator*(const fpoint p) const { return fpoint(*this) *= p; } fpoint operator/(const fpoint p) const { return fpoint(*this) /= p; } fpoint operator+(const ld a) const { return fpoint(*this) += a; } fpoint operator-(const ld a) const { return fpoint(*this) -= a; } fpoint operator*(const ld a) const { return fpoint(*this) *= a; } fpoint operator/(const ld a) const { return fpoint(*this) /= a; } ld dot(const fpoint &p) const { return x * p.x + y * p.y; } ll cross(const fpoint &p) const { return x * p.y - y * p.x; } ld squared_norm() const { return x * x + y * y; } ld norm() const { return sqrt(x * x + y * y); } tuple<ld, ld> get_value() { return make_tuple(x, y); } ll which_quadrant() const { if (abs(x) < EPS && abs(y) < EPS) return 0; if (y > 0) return x > 0 ? 1 : 2; return x < 0 ? 3 : 4; } bool is_zero() { fpoint z = {0, 0}; return z == *this; } }; struct point { ll x = 0; ll y = 0; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const point &p) const { if (x == p.x && y == p.y) return true; return false; } bool operator!=(const point &p) const { if (x != p.x || y != p.y) return true; return false; } point &operator+=(const ll a) { x += a, y += a; return *this; } point &operator-=(const ll a) { x -= a, y -= a; return *this; } point &operator*=(const ll a) { x *= a, y *= a; return *this; } point &operator+=(const point p) { x += p.x, y += p.y; return *this; } point &operator-=(const point p) { x -= p.x, y -= p.y; return *this; } point &operator*=(const point p) { x *= p.x, y *= p.y; return *this; } void operator++(int) { x++, y++; } void operator++() { x++, y++; } void operator--(int) { x--, y--; } void operator--() { x--, y--; } point operator+(const point p) const { return point(*this) += p; } point operator-(const point p) const { return point(*this) -= p; } point operator*(const point p) const { return point(*this) *= p; } point operator+(const ll a) const { return point(*this) += a; } point operator-(const ll a) const { return point(*this) -= a; } point operator*(const ll a) const { return point(*this) *= a; } ll dot(const point &p) const { return x * p.x + y * p.y; } ll cross(const point &p) const { return x * p.y - y * p.x; } ll squared_norm() const { return x * x + y * y; } tuple<ll, ll> get_value() { return make_tuple(x, y); } ll which_quadrant() const { if (x == 0 && y == 0) return 0; if (x >= 0 && y >= 0) return 1; else if (x <= 0 && y >= 0) return 2; else if (x <= 0 && y <= 0) return 3; else return 4; } bool is_zero() { point z = {0, 0}; return z == *this; } fpoint to_fpoint() { fpoint ret = {ld(x), ld(y)}; return ret; } }; struct { template <typename T> bool operator()(const T p1, const T p2) const { ll q1 = p1.which_quadrant(); ll q2 = p2.which_quadrant(); if (q1 != q2) return q1 < q2; // judge for parallel lines // if p1 cross p2 > 0 -> sin arg(p1 -> o -> p2) > 0 return p1.cross(p2) > 0; } } angle_comparator; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } tuple<ll, ll, ll> get_value() { return make_tuple(from, to, cost); } }; struct event { ll loc, val, sgn; bool operator<(const event &e) const { if (loc == e.loc) return sgn == 1; return loc < e.loc; } bool operator>(const event &e) const { if (loc == e.loc) return sgn == -1; return loc > e.loc; } tuple<ll, ll, ll> get_value() { return make_tuple(loc, val, sgn); } }; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; typedef std::vector<ll> vl; typedef std::vector<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<mint> vmi; typedef std::vector<vmi> vmi2; typedef std::vector<vmi2> vmi3; typedef std::vector<vmi3> vmi4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; typedef std::vector<fpoint> fpoints; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const long double PI = 3.14159265358979323846; const points dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0}}; // self template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(mint a) { return to_string(a.x); } string to_string(point p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } string to_string(fpoint p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) res += ", "; first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; } 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; } template <typename T> string to_string(priority_queue<T> &q) { priority_queue<T> copy; bool first = true; string res = "{"; while (!q.empty()) { if (!first) { res += ", "; } first = false; res += to_string(q.top()); copy.push(q.top()); q.pop(); } swap(q, copy); 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...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) std::cout << (a) #define prl(a) std::cout << (a) << endl #define prl2(a, b) std::cout << (a) << " " << (b) << endl #define prl3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) std::cout << (a) << " " #define prs2(a, b) std::cout << (a) << " " << (b) << " " #define prs3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ std::cout << fixed << setprecision(digits) << (num) << endl; #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #ifdef _LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow. if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; struct UnionFind { vl parents, set_size; set<ll> root_idx; ll n_groups; UnionFind(ll n) { parents = set_size = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); n_groups--; } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; template <class T> class LowestCommonAncestor { public: ll N, logN; vl depth, len; T tree; vl2 parents; LowestCommonAncestor(ll n, T &_tree) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = len = vl(N); parents = vl2(logN, vl(N)); tree = _tree; init(0, -1, 0, 0); build(); } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, d); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; // This is the exaple of the number of children static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; // dag: edges from a cmp to another cmp vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } bool in_loop(ll v) { return cmp[cmp_idx[v]].size() > 1; } bool has_loop() { rep(i, cmp.size()) if (cmp[i].size() > 1) return true; return false; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } ll nPk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; return val % mod; } }; struct PowerMemo { ll sz, mod, base; vl powB; PowerMemo(ll sz, ll base, ll _mod) : sz(sz), base(base), mod(_mod) { powB.resize(sz + 5); init(); } void init() { powB[0] = 1; rep(i, sz + 3) powB[i + 1] = (powB[i] * base) % mod; } ll operator[](ll k) { return powB[k]; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point nxt = p + dirs[i]; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; struct Cumulative2D { vl2 cum; ll w, h; Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); } template <typename T> void build(vector<T> &vec) { // never forget building rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; } ll func(ll x, ll y, ll dx, ll dy) { // 1-indexed // the rectangle of (x, y), (x + dx, y), (x, y + dy) and (x + dx, y + dy) // think about the case of (1, 1, 1, 1). if (x + dx > w || y + dy > h) return -INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n + 1, vl(n + 1)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } string bin_expression(ll n, ll dig) { string s = ""; rep(i, dig) { s += to_string(n % 2); n /= 2; } reverse(all(s)); return s; } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } map<ll, ll> prime_factorization(ll n) { ll i = 2; map<ll, ll> table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll next_combination(ll sub) { /* nCk ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; if (x != 0) return (((sub & ~y) / x) >> 1) | y; else return INF; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F template <typename T> vl z_algorithm(T &s, ll n) { /* Paramters --------- T: the string or list of interest n: the size of string or list Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll string_to_ll(string s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth); tenth *= 10; idx++; } return val; } string reflected_string(string s) { string t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } template <typename T> bool isColinear(T p1, T p2, T p3) { T v1 = p2 - p1, v2 = p3 - p1; return v1.x * v2.y == v1.y * v2.x; } template <typename T> T PerpendicularBisector(T p1, T p2) { T vec = p2 - p1; assert(!vec.is_zero()); T ret = {vec.y, -vec.x}; return ret; } template <typename T> ld Distance2DPoints(T p1, T p2) { T vec = (p1 - p2) * (p1 - p2); return sqrt(vec.x + vec.y); } ll SquaredDistance2DPoints(point p1, point p2) { point vec = (p1 - p2) * (p1 - p2); return vec.x + vec.y; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> OuterCenter(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value point ret; if (isColinear(p1, p2, p3)) { ll d1 = SquaredDistance2DPoints(p1, p2); ll d2 = SquaredDistance2DPoints(p2, p3); ll d3 = SquaredDistance2DPoints(p3, p1); if (d1 >= d2 && d1 >= d3) { ret = p1 + p2; return mp(ret, 2); } else if (d2 >= d1 && d2 >= d3) { ret = p2 + p3; return mp(ret, 2); } else { ret = p3 + p1; return mp(ret, 2); } } point pv1 = PerpendicularBisector(p1, p2); point pv2 = PerpendicularBisector(p1, p3); point cv1_2x = p1 + p2, cv2_2x = p1 + p3; // cv1 + k pv1 == cv2 + m pv2 // (pv1x -pv2x) (k) = (cv2x - cv1x) // (pv1y -pv2y) (m) = (cv2y - cv1y) ll det_inv = -pv1.x * pv2.y + pv1.y * pv2.x; ll x1_2x = cv2_2x.x - cv1_2x.x, x2_2x = cv2_2x.y - cv1_2x.y; pl c_2x_det = {-pv2.y * x1_2x + pv2.x * x2_2x, -pv1.y * x1_2x + pv1.x * x2_2x}; // ret.x = ld(cv1_2x.x * det_inv + pv1.x * c_2x_det.e1) / ld(2 * det_inv); // ret.y = ld(cv1_2x.y * det_inv + pv1.y * c_2x_det.e1) / ld(2 * det_inv); ret.x = cv1_2x.x * det_inv + pv1.x * c_2x_det.e1; ret.y = cv1_2x.y * det_inv + pv1.y * c_2x_det.e1; ll jacobian = 2 * det_inv; return mp(ret, jacobian); } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value. val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } ld bin_search(ld left, ld right, bool lb, function<bool(ld)> judge) { ld mid; while (right - left > EPS) { mid = (right + left) / 2; if (lb) { if (judge(mid)) right = mid; else left = mid + EPS; } else { if (judge(mid)) left = mid; else right = mid - EPS; } } return right; } ll bin_search(ll left, ll right, bool lb, function<bool(ll)> judge) { ll mid; while (right > left) { if (lb) { // if true (satisfies the condition), range shifts smaller direction mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; } else { // if true (satisfies the condition), range shitfs larger direction mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } } return right; } ld trinary_search(ld left, ld right, function<ld(ld)> func) { // Care the value EPS!!! Compare to the condition while (abs(right - left) > EPS) { ld left2 = (2.0 * left + right) / 3.0; ld right2 = (left + 2.0 * right) / 3.0; ld f1 = func(left2); ld f2 = func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } return right; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { // O(ElogE) sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v) { ll n = v.size(); vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> edge_costs; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { ll idx, cost; tie(cost, idx) = edge_costs.top(); edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] + e.cost) { dist[e.to] = dist[idx] + e.cost; if (trace) vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void Euler_Tour(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record only when we first reach a node and leave the node. ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); } euler_tour.pb(-source); out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } void Euler_Tour2(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record everytime we reach a node ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); euler_tour.pb(source); } out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } std::mt19937 create_rand_engine() { std::random_device rnd; std::vector<std::uint_least32_t> v(10); std::generate(v.begin(), v.end(), std::ref(rnd)); std::seed_seq seed(v.begin(), v.end()); return std::mt19937(seed); } vl generate_unique_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); const size_t num = static_cast<size_t>(sz * 1.2); assert(vm <= vM); assert(sz <= range); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) { while (ret.size() < num) ret.pb(distribution(engine)); sort(all(ret)); auto unique_end = unique(all(ret)); if (sz < distance(ret.begin(), unique_end)) unique_end = next(ret.begin(), sz); ret.erase(unique_end, ret.end()); } return ret; } vl generate_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); assert(vm <= vM); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) ret.pb(distribution(engine)); return ret; } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } void read_points(ll n, points &ps, ll offset = 0) { ps.resize(n); rep(i, n) { ll x, y; in2(x, y); ps[i] = {x, y}; ps[i] += offset; } } void read_2DMap(ll w, ll h, vb2 &block, char b) { block = vb2(w, vb(h, false)); string s; rep(y, h) { in1(s); rep(x, w) block[x][y] = (s[x] == b); } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ if (l == r) r++; while(r < n && cond) r++; l++; } prl(answer); #11. bfs ABC146D, ABC007C 1. first create a graph. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = graph[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = graph[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } */ /* x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & ~ x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 ####### Attention ####### S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & ~(1 << i) -> S - {i} __builtin_popcountl(i) -> the number of 1 in binary #Conditional Operator condition ? true : false; # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; const size_t MAX_BIT = 160 * 160 + 10; typedef bitset<MAX_BIT> bts; typedef vector<bts> vbt; typedef vector<vbt> vbt2; typedef vector<vbt2> vbt3; void solve() { bool okay = false; string S, T, U; ll A, B, C, D, N, M, H, W, K, x, y, Q, idx, cnt, num, answer; vl as, bs, cs, ds; vb2 block; vb visited; Graph graph, tree; points ps; // generate_array(size, min, max); in3(N, B, D); vl cum(N + 1, 0); rep(i, N) cum[i + 1] = cum[i] + i + 1; if (D == 0) { } else if (D < 0) D = -D, B = -B; priority_queue<event, vector<event>, greater<event>> events; rep(i, N + 1) { ll m1, m2, c; m1 = cum[i], m2 = cum[N] - cum[N - i]; c = (B - D) * (2 * i - N) - D * N * (N + 1) / 2; events.push({c + 2 * D * m1, 0, +1}); events.push({c + 2 * D * m2, 0, -1}); } answer = 0; map<ll, stack<ll>> c; while (!events.empty()) { event e = events.top(); events.pop(); ll loc, _, sgn; tie(loc, _, sgn) = e.get_value(); ll m = (loc % (2 * D) + 2 * D) % (2 * D); if (sgn == 1) c[m].push(loc); else if (sgn == -1 && c[m].size() > 1) c[m].pop(); else answer += (loc - c[m].top()) / 2 / D + 1, c[m].pop(); } prl(answer); // ### DEBUG PART ### auto naive_solver = [&]() {}; #ifdef _LOCAL naive_solver(); #endif } void test(ll num = 0, bool verbose = false) { rep(i, max(1LL, num)) { ll t = clock(); if (verbose) prl4("\n#####", i + 1, "#####", "\n## Answer ##"); solve(); if (verbose) { prl(""); prl_time(t); } } } int main(void) { #ifdef _LOCAL test(4, true); #else test(0, false); #endif return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; // const ll MOD = 1000000007; const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-12; struct mint { ll x; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } struct edge { ll to, cost; tuple<ll, ll> get_value() { return make_tuple(to, cost); } }; struct BigInt { string num; ll mod(ll _mod) { ll l = num.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = num[l - 1 - idx] - '0'; val += (m * tenth) % _mod; val %= _mod; tenth *= 10; tenth %= _mod; idx++; } return val; } ll digits() { return num.length(); } bool operator<(BigInt &n2) const { if (num.length() < n2.digits()) return true; else if (num.length() > n2.digits()) return false; return num < n2.num; } bool operator>(BigInt &n2) const { if (num.length() > n2.digits()) return true; else if (num.length() < n2.digits()) return false; return num > n2.num; } }; struct fpoint { ld x = 0; ld y = 0; bool operator<(const fpoint &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const fpoint &p) const { if (p.x - EPS < x && x < p.x + EPS && p.y - EPS < y && y < p.y + EPS) return true; return false; } bool operator!=(const fpoint &p) const { if (p.x - EPS > x || x > p.x + EPS || p.y - EPS > y || y > p.y + EPS) return true; return false; } fpoint &operator+=(const ld a) { x += a, y += a; return *this; } fpoint &operator-=(const ld a) { x -= a, y -= a; return *this; } fpoint &operator*=(const ld a) { x *= a, y *= a; return *this; } fpoint &operator/=(const ld a) { x /= a, y /= a; return *this; } fpoint &operator+=(const fpoint p) { x += p.x, y += p.y; return *this; } fpoint &operator-=(const fpoint p) { x -= p.x, y -= p.y; return *this; } fpoint &operator*=(const fpoint p) { x *= p.x, y *= p.y; return *this; } fpoint &operator/=(const fpoint p) { x /= p.x, y /= p.y; return *this; } fpoint operator+(const fpoint p) const { return fpoint(*this) += p; } fpoint operator-(const fpoint p) const { return fpoint(*this) -= p; } fpoint operator*(const fpoint p) const { return fpoint(*this) *= p; } fpoint operator/(const fpoint p) const { return fpoint(*this) /= p; } fpoint operator+(const ld a) const { return fpoint(*this) += a; } fpoint operator-(const ld a) const { return fpoint(*this) -= a; } fpoint operator*(const ld a) const { return fpoint(*this) *= a; } fpoint operator/(const ld a) const { return fpoint(*this) /= a; } ld dot(const fpoint &p) const { return x * p.x + y * p.y; } ll cross(const fpoint &p) const { return x * p.y - y * p.x; } ld squared_norm() const { return x * x + y * y; } ld norm() const { return sqrt(x * x + y * y); } tuple<ld, ld> get_value() { return make_tuple(x, y); } ll which_quadrant() const { if (abs(x) < EPS && abs(y) < EPS) return 0; if (y > 0) return x > 0 ? 1 : 2; return x < 0 ? 3 : 4; } bool is_zero() { fpoint z = {0, 0}; return z == *this; } }; struct point { ll x = 0; ll y = 0; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } bool operator==(const point &p) const { if (x == p.x && y == p.y) return true; return false; } bool operator!=(const point &p) const { if (x != p.x || y != p.y) return true; return false; } point &operator+=(const ll a) { x += a, y += a; return *this; } point &operator-=(const ll a) { x -= a, y -= a; return *this; } point &operator*=(const ll a) { x *= a, y *= a; return *this; } point &operator+=(const point p) { x += p.x, y += p.y; return *this; } point &operator-=(const point p) { x -= p.x, y -= p.y; return *this; } point &operator*=(const point p) { x *= p.x, y *= p.y; return *this; } void operator++(int) { x++, y++; } void operator++() { x++, y++; } void operator--(int) { x--, y--; } void operator--() { x--, y--; } point operator+(const point p) const { return point(*this) += p; } point operator-(const point p) const { return point(*this) -= p; } point operator*(const point p) const { return point(*this) *= p; } point operator+(const ll a) const { return point(*this) += a; } point operator-(const ll a) const { return point(*this) -= a; } point operator*(const ll a) const { return point(*this) *= a; } ll dot(const point &p) const { return x * p.x + y * p.y; } ll cross(const point &p) const { return x * p.y - y * p.x; } ll squared_norm() const { return x * x + y * y; } tuple<ll, ll> get_value() { return make_tuple(x, y); } ll which_quadrant() const { if (x == 0 && y == 0) return 0; if (x >= 0 && y >= 0) return 1; else if (x <= 0 && y >= 0) return 2; else if (x <= 0 && y <= 0) return 3; else return 4; } bool is_zero() { point z = {0, 0}; return z == *this; } fpoint to_fpoint() { fpoint ret = {ld(x), ld(y)}; return ret; } }; struct { template <typename T> bool operator()(const T p1, const T p2) const { ll q1 = p1.which_quadrant(); ll q2 = p2.which_quadrant(); if (q1 != q2) return q1 < q2; // judge for parallel lines // if p1 cross p2 > 0 -> sin arg(p1 -> o -> p2) > 0 return p1.cross(p2) > 0; } } angle_comparator; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } tuple<ll, ll, ll> get_value() { return make_tuple(from, to, cost); } }; struct event { ll loc, val, sgn; bool operator<(const event &e) const { if (loc == e.loc) return sgn == 1; return loc < e.loc; } bool operator>(const event &e) const { if (loc == e.loc) return sgn == -1; return loc > e.loc; } tuple<ll, ll, ll> get_value() { return make_tuple(loc, val, sgn); } }; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; typedef std::vector<ll> vl; typedef std::vector<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<mint> vmi; typedef std::vector<vmi> vmi2; typedef std::vector<vmi2> vmi3; typedef std::vector<vmi3> vmi4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; typedef std::vector<fpoint> fpoints; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const long double PI = 3.14159265358979323846; const points dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0}}; // self template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(mint a) { return to_string(a.x); } string to_string(point p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } string to_string(fpoint p) { return "{" + to_string(p.x) + ", " + to_string(p.y) + "}"; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) res += ", "; first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; } 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; } template <typename T> string to_string(priority_queue<T> &q) { priority_queue<T> copy; bool first = true; string res = "{"; while (!q.empty()) { if (!first) { res += ", "; } first = false; res += to_string(q.top()); copy.push(q.top()); q.pop(); } swap(q, copy); 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...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) std::cout << (a) #define prl(a) std::cout << (a) << endl #define prl2(a, b) std::cout << (a) << " " << (b) << endl #define prl3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) std::cout << (a) << " " #define prs2(a, b) std::cout << (a) << " " << (b) << " " #define prs3(a, b, c) std::cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ std::cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ std::cout << fixed << setprecision(digits) << (num) << endl; #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #ifdef _LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow. if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; struct UnionFind { vl parents, set_size; set<ll> root_idx; ll n_groups; UnionFind(ll n) { parents = set_size = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); n_groups--; } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; template <class T> class LowestCommonAncestor { public: ll N, logN; vl depth, len; T tree; vl2 parents; LowestCommonAncestor(ll n, T &_tree) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = len = vl(N); parents = vl2(logN, vl(N)); tree = _tree; init(0, -1, 0, 0); build(); } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, d); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; // This is the exaple of the number of children static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; // dag: edges from a cmp to another cmp vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } bool in_loop(ll v) { return cmp[cmp_idx[v]].size() > 1; } bool has_loop() { rep(i, cmp.size()) if (cmp[i].size() > 1) return true; return false; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } ll nPk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; return val % mod; } }; struct PowerMemo { ll sz, mod, base; vl powB; PowerMemo(ll sz, ll base, ll _mod) : sz(sz), base(base), mod(_mod) { powB.resize(sz + 5); init(); } void init() { powB[0] = 1; rep(i, sz + 3) powB[i + 1] = (powB[i] * base) % mod; } ll operator[](ll k) { return powB[k]; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point nxt = p + dirs[i]; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; struct Cumulative2D { vl2 cum; ll w, h; Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); } template <typename T> void build(vector<T> &vec) { // never forget building rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; } ll func(ll x, ll y, ll dx, ll dy) { // 1-indexed // the rectangle of (x, y), (x + dx, y), (x, y + dy) and (x + dx, y + dy) // think about the case of (1, 1, 1, 1). if (x + dx > w || y + dy > h) return -INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n + 1, vl(n + 1)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } string bin_expression(ll n, ll dig) { string s = ""; rep(i, dig) { s += to_string(n % 2); n /= 2; } reverse(all(s)); return s; } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } map<ll, ll> prime_factorization(ll n) { ll i = 2; map<ll, ll> table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll next_combination(ll sub) { /* nCk ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; if (x != 0) return (((sub & ~y) / x) >> 1) | y; else return INF; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F template <typename T> vl z_algorithm(T &s, ll n) { /* Paramters --------- T: the string or list of interest n: the size of string or list Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll string_to_ll(string s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth); tenth *= 10; idx++; } return val; } string reflected_string(string s) { string t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } template <typename T> bool isColinear(T p1, T p2, T p3) { T v1 = p2 - p1, v2 = p3 - p1; return v1.x * v2.y == v1.y * v2.x; } template <typename T> T PerpendicularBisector(T p1, T p2) { T vec = p2 - p1; assert(!vec.is_zero()); T ret = {vec.y, -vec.x}; return ret; } template <typename T> ld Distance2DPoints(T p1, T p2) { T vec = (p1 - p2) * (p1 - p2); return sqrt(vec.x + vec.y); } ll SquaredDistance2DPoints(point p1, point p2) { point vec = (p1 - p2) * (p1 - p2); return vec.x + vec.y; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> OuterCenter(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value point ret; if (isColinear(p1, p2, p3)) { ll d1 = SquaredDistance2DPoints(p1, p2); ll d2 = SquaredDistance2DPoints(p2, p3); ll d3 = SquaredDistance2DPoints(p3, p1); if (d1 >= d2 && d1 >= d3) { ret = p1 + p2; return mp(ret, 2); } else if (d2 >= d1 && d2 >= d3) { ret = p2 + p3; return mp(ret, 2); } else { ret = p3 + p1; return mp(ret, 2); } } point pv1 = PerpendicularBisector(p1, p2); point pv2 = PerpendicularBisector(p1, p3); point cv1_2x = p1 + p2, cv2_2x = p1 + p3; // cv1 + k pv1 == cv2 + m pv2 // (pv1x -pv2x) (k) = (cv2x - cv1x) // (pv1y -pv2y) (m) = (cv2y - cv1y) ll det_inv = -pv1.x * pv2.y + pv1.y * pv2.x; ll x1_2x = cv2_2x.x - cv1_2x.x, x2_2x = cv2_2x.y - cv1_2x.y; pl c_2x_det = {-pv2.y * x1_2x + pv2.x * x2_2x, -pv1.y * x1_2x + pv1.x * x2_2x}; // ret.x = ld(cv1_2x.x * det_inv + pv1.x * c_2x_det.e1) / ld(2 * det_inv); // ret.y = ld(cv1_2x.y * det_inv + pv1.y * c_2x_det.e1) / ld(2 * det_inv); ret.x = cv1_2x.x * det_inv + pv1.x * c_2x_det.e1; ret.y = cv1_2x.y * det_inv + pv1.y * c_2x_det.e1; ll jacobian = 2 * det_inv; return mp(ret, jacobian); } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value. val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } ld bin_search(ld left, ld right, bool lb, function<bool(ld)> judge) { ld mid; while (right - left > EPS) { mid = (right + left) / 2; if (lb) { if (judge(mid)) right = mid; else left = mid + EPS; } else { if (judge(mid)) left = mid; else right = mid - EPS; } } return right; } ll bin_search(ll left, ll right, bool lb, function<bool(ll)> judge) { ll mid; while (right > left) { if (lb) { // if true (satisfies the condition), range shifts smaller direction mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; } else { // if true (satisfies the condition), range shitfs larger direction mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } } return right; } ld trinary_search(ld left, ld right, function<ld(ld)> func) { // Care the value EPS!!! Compare to the condition while (abs(right - left) > EPS) { ld left2 = (2.0 * left + right) / 3.0; ld right2 = (left + 2.0 * right) / 3.0; ld f1 = func(left2); ld f2 = func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } return right; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { // O(ElogE) sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v) { ll n = v.size(); vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> edge_costs; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { ll idx, cost; tie(cost, idx) = edge_costs.top(); edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] + e.cost) { dist[e.to] = dist[idx] + e.cost; if (trace) vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void Euler_Tour(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record only when we first reach a node and leave the node. ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); } euler_tour.pb(-source); out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } void Euler_Tour2(Graph &tree, vl &euler_tour, vl &in, vl &out, ll anc = 0) { // record everytime we reach a node ll n = tree.size(); vb visited(n, false); in = out = vl(n); auto dfs = [&](auto dfs, ll source) -> void { visited[source] = true; in[source] = euler_tour.size(); euler_tour.pb(source); bool is_leaf = true; for (auto &e : tree[source]) { ll target = e.to; if (visited[target]) continue; else is_leaf = false; dfs(dfs, target); euler_tour.pb(source); } out[source] = euler_tour.size() - 1; }; dfs(dfs, anc); } std::mt19937 create_rand_engine() { std::random_device rnd; std::vector<std::uint_least32_t> v(10); std::generate(v.begin(), v.end(), std::ref(rnd)); std::seed_seq seed(v.begin(), v.end()); return std::mt19937(seed); } vl generate_unique_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); const size_t num = static_cast<size_t>(sz * 1.2); assert(vm <= vM); assert(sz <= range); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) { while (ret.size() < num) ret.pb(distribution(engine)); sort(all(ret)); auto unique_end = unique(all(ret)); if (sz < distance(ret.begin(), unique_end)) unique_end = next(ret.begin(), sz); ret.erase(unique_end, ret.end()); } return ret; } vl generate_array(const size_t sz, ll vm, ll vM) { const size_t range = static_cast<size_t>(vM - vm + 1); assert(vm <= vM); vl ret; auto engine = create_rand_engine(); std::uniform_int_distribution<ll> distribution(vm, vM); while (ret.size() < sz) ret.pb(distribution(engine)); return ret; } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } void read_points(ll n, points &ps, ll offset = 0) { ps.resize(n); rep(i, n) { ll x, y; in2(x, y); ps[i] = {x, y}; ps[i] += offset; } } void read_2DMap(ll w, ll h, vb2 &block, char b) { block = vb2(w, vb(h, false)); string s; rep(y, h) { in1(s); rep(x, w) block[x][y] = (s[x] == b); } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ if (l == r) r++; while(r < n && cond) r++; l++; } prl(answer); #11. bfs ABC146D, ABC007C 1. first create a graph. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = graph[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = graph[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } */ /* x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & ~ x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 ####### Attention ####### S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & ~(1 << i) -> S - {i} __builtin_popcountl(i) -> the number of 1 in binary #Conditional Operator condition ? true : false; # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; const size_t MAX_BIT = 160 * 160 + 10; typedef bitset<MAX_BIT> bts; typedef vector<bts> vbt; typedef vector<vbt> vbt2; typedef vector<vbt2> vbt3; void solve() { bool okay = false; string S, T, U; ll A, B, C, D, N, M, H, W, K, x, y, Q, idx, cnt, num, answer; vl as, bs, cs, ds; vb2 block; vb visited; Graph graph, tree; points ps; // generate_array(size, min, max); in3(N, B, D); vl cum(N + 1, 0); rep(i, N) cum[i + 1] = cum[i] + i + 1; if (D == 0) { if (B == 0) prl(1); else prl(N + 1); return; } else if (D < 0) D = -D, B = -B; priority_queue<event, vector<event>, greater<event>> events; rep(i, N + 1) { ll m1, m2, c; m1 = cum[i], m2 = cum[N] - cum[N - i]; c = (B - D) * (2 * i - N) - D * N * (N + 1) / 2; events.push({c + 2 * D * m1, 0, +1}); events.push({c + 2 * D * m2, 0, -1}); } answer = 0; map<ll, stack<ll>> c; while (!events.empty()) { event e = events.top(); events.pop(); ll loc, _, sgn; tie(loc, _, sgn) = e.get_value(); ll m = (loc % (2 * D) + 2 * D) % (2 * D); if (sgn == 1) c[m].push(loc); else if (sgn == -1 && c[m].size() > 1) c[m].pop(); else answer += (loc - c[m].top()) / 2 / D + 1, c[m].pop(); } prl(answer); // ### DEBUG PART ### auto naive_solver = [&]() {}; #ifdef _LOCAL naive_solver(); #endif } void test(ll num = 0, bool verbose = false) { rep(i, max(1LL, num)) { ll t = clock(); if (verbose) prl4("\n#####", i + 1, "#####", "\n## Answer ##"); solve(); if (verbose) { prl(""); prl_time(t); } } } int main(void) { #ifdef _LOCAL test(4, true); #else test(0, false); #endif return 0; }
replace
2,008
2,009
2,008
2,013
0
p02840
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <set> #include <vector> using namespace std; long long n, x, d; long long smin[500000]; long long smax[500000]; map<int, vector<pair<long long, long long>>> st; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> x >> d; for (int i = 1; i <= n; i++) smin[i] = smin[i - 1] + i - 1; for (int i = 1; i <= n; i++) smax[i] = smax[i - 1] + n - i; for (int k = 0; k <= n; k++) { long long mn = k * x + smin[k] * d; long long mx = k * x + smax[k] * d; long long md = ((k * x) % d + d) % d; mn = (mn - md) / d; mx = (mx - md) / d; st[md].push_back({mn, mx}); } long long ans = 0; for (auto vv : st) { vector<pair<long long, long long>> v = vv.second; sort(v.begin(), v.end()); long long sm = -1e18; for (int i = 0; i < v.size(); i++) { v[i].first = max(v[i].first, sm); if (v[i].first > v[i].second) continue; else ans += v[i].second - v[i].first + 1, sm = v[i].second + 1; } } cout << ans; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <set> #include <vector> using namespace std; long long n, x, d; long long smin[500000]; long long smax[500000]; map<int, vector<pair<long long, long long>>> st; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> x >> d; if (d == 0) { if (x == 0) { cout << 1; return 0; } cout << n + 1; return 0; } for (int i = 1; i <= n; i++) smin[i] = smin[i - 1] + i - 1; for (int i = 1; i <= n; i++) smax[i] = smax[i - 1] + n - i; for (int k = 0; k <= n; k++) { long long mn = k * x + smin[k] * d; long long mx = k * x + smax[k] * d; long long md = ((k * x) % d + d) % d; mn = (mn - md) / d; mx = (mx - md) / d; st[md].push_back({mn, mx}); } long long ans = 0; for (auto vv : st) { vector<pair<long long, long long>> v = vv.second; sort(v.begin(), v.end()); long long sm = -1e18; for (int i = 0; i < v.size(); i++) { v[i].first = max(v[i].first, sm); if (v[i].first > v[i].second) continue; else ans += v[i].second - v[i].first + 1, sm = v[i].second + 1; } } cout << ans; }
insert
22
22
22
30
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; ll mod = 1e9 + 7; const ld error = 1e-5; const ld PI = acosl(-1); // const ld PI = acosl(-1) #define FASTIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define eq(x, y) (fabs((x) - (y)) < error) #define bt(i) (1LL << (i)) #define debug(x) cerr << #x << " = " << (x) << "\n" #define hoise cerr << "hoise - " << __LINE__ << "\n" #define tham getchar() mt19937 rng((unsigned)chrono::system_clock::now() .time_since_epoch() .count()); // mt199937_64 for ll inline ll MOD(ll x, ll m = mod) { ll y = x % m; return (y >= 0) ? y : y + m; } const int inf = 1e9; const ll infl = 1e18 + 1; const int nmax = 80 + 5; ///=========================================== template ///======================================================= ll sum(ll a, ll n) { ll b = a + n - 1; ll ans = (a + b) * (b - a + 1); return ans / 2; } int main() { FASTIO; ll n, x, d; cin >> n >> x >> d; if (d < 0) { x = x + (n - 1) * d; d = -d; } map<ll, int> rem; vector<vector<pair<ll, ll>>> ranges; /// 0 begin, 1 end for (int i = 0; i <= n; i++) { ll sum_x = x * i; ll r = MOD(sum_x, d); if (rem.find(r) == rem.end()) { rem[r] = ranges.size(); ranges.emplace_back(); } r = rem[r]; ll sum_d_first = sum(0, i); ll sum_d_second = sum(n - i, i); // debug(i); // debug(sum_x); // debug(sum_d_first); // debug(sum_d_second); ranges[r].emplace_back(sum_x + sum_d_first * d, 0); ranges[r].emplace_back(sum_x + sum_d_second * d, 1); //// // debug(sum_x + sum_d_first*d); // debug(sum_x + sum_d_second*d); } for (int i = 0; i < ranges.size(); i++) sort(ranges[i].begin(), ranges[i].end()); ll ans = 0; for (int r = 0; r < ranges.size(); r++) { ll last = 0; int cnt = 0; for (auto p : ranges[r]) { // cout<<p.first<<" "<<p.second<<"\n"; if (cnt == 0) last = p.first; cnt += (p.second == 0) ? 1 : -1; if (cnt == 0) ans += ((p.first - last) / d + 1); } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; ll mod = 1e9 + 7; const ld error = 1e-5; const ld PI = acosl(-1); // const ld PI = acosl(-1) #define FASTIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define eq(x, y) (fabs((x) - (y)) < error) #define bt(i) (1LL << (i)) #define debug(x) cerr << #x << " = " << (x) << "\n" #define hoise cerr << "hoise - " << __LINE__ << "\n" #define tham getchar() mt19937 rng((unsigned)chrono::system_clock::now() .time_since_epoch() .count()); // mt199937_64 for ll inline ll MOD(ll x, ll m = mod) { ll y = x % m; return (y >= 0) ? y : y + m; } const int inf = 1e9; const ll infl = 1e18 + 1; const int nmax = 80 + 5; ///=========================================== template ///======================================================= ll sum(ll a, ll n) { ll b = a + n - 1; ll ans = (a + b) * (b - a + 1); return ans / 2; } int main() { FASTIO; ll n, x, d; cin >> n >> x >> d; if (d == 0) { if (x == 0) cout << 1; else cout << n + 1; cout << "\n"; return 0; } if (d < 0) { x = x + (n - 1) * d; d = -d; } map<ll, int> rem; vector<vector<pair<ll, ll>>> ranges; /// 0 begin, 1 end for (int i = 0; i <= n; i++) { ll sum_x = x * i; ll r = MOD(sum_x, d); if (rem.find(r) == rem.end()) { rem[r] = ranges.size(); ranges.emplace_back(); } r = rem[r]; ll sum_d_first = sum(0, i); ll sum_d_second = sum(n - i, i); // debug(i); // debug(sum_x); // debug(sum_d_first); // debug(sum_d_second); ranges[r].emplace_back(sum_x + sum_d_first * d, 0); ranges[r].emplace_back(sum_x + sum_d_second * d, 1); //// // debug(sum_x + sum_d_first*d); // debug(sum_x + sum_d_second*d); } for (int i = 0; i < ranges.size(); i++) sort(ranges[i].begin(), ranges[i].end()); ll ans = 0; for (int r = 0; r < ranges.size(); r++) { ll last = 0; int cnt = 0; for (auto p : ranges[r]) { // cout<<p.first<<" "<<p.second<<"\n"; if (cnt == 0) last = p.first; cnt += (p.second == 0) ? 1 : -1; if (cnt == 0) ans += ((p.first - last) / d + 1); } } cout << ans << "\n"; return 0; }
insert
46
46
46
55
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define int long long // TEMPLATE // START---------------8<---------------8<---------------8<---------------8<---------------// typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int>> vvi; const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int MOD = 1e9 + 7; const int MODD = 998244353; const string alphabet = "abcdefghijklmnopqrstuvwxyz"; const double PI = acos(-1.0); const double EPS = 1e-9; const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; int dy[9] = {0, 1, 0, -1, -1, -1, 1, 1, 0}; #define ln '\n' #define scnaf scanf #define sacnf scanf #define sancf scanf #define SS(type, ...) \ type __VA_ARGS__; \ MACRO_VAR_Scan(__VA_ARGS__); template <typename T> void MACRO_VAR_Scan(T &t) { cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First &first, Rest &...rest) { cin >> first; MACRO_VAR_Scan(rest...); } #define SV(type, c, n) \ vector<type> c(n); \ for (auto &i : c) \ cin >> i; #define SVV(type, c, n, m) \ vector<vector<type>> c(n, vector<type>(m)); \ for (auto &r : c) \ for (auto &i : r) \ cin >> i; template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &j) { o << "{" << j.first << ", " << j.second << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const map<T, U> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const multiset<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &j) { o << "{"; for (int i = 0; i < (int)j.size(); ++i) o << (i > 0 ? ", " : "") << j[i]; o << "}"; return o; } inline int print(void) { cout << endl; return 0; } template <class Head> int print(Head &&head) { cout << head; print(); return 0; } template <class Head, class... Tail> int print(Head &&head, Tail &&...tail) { cout << head << " "; print(forward<Tail>(tail)...); return 0; } #ifdef LOCAL inline int dump(void) { cerr << endl; return 0; } template <class Head> int dump(Head &&head) { cerr << head; dump(); return 0; } template <class Head, class... Tail> int dump(Head &&head, Tail &&...tail) { cerr << head << " "; dump(forward<Tail>(tail)...); return 0; } #define debug(...) \ do { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ dump(__VA_ARGS__); \ } while (0) #define ER(x) cerr << #x << " = " << (x) << endl; #define ERV(v) \ { \ cerr << #v << " : "; \ for (const auto &xxx : v) { \ cerr << xxx << " "; \ } \ cerr << "\n"; \ } #else #define dump(...) #define debug(...) #define ER(x) #define ERV(v) #endif template <typename T> void PA(T &a) { int ASIZE = sizeof(a) / sizeof(a[0]); for (int ii = 0; ii < ASIZE; ++ii) { cout << a[ii] << " \n"[ii == ASIZE - 1]; } } template <typename T> void PV(T &v) { int VSIZE = v.size(); for (int ii = 0; ii < VSIZE; ++ii) { cout << v[ii] << " \n"[ii == VSIZE - 1]; } } inline int YES(bool x) { cout << ((x) ? "YES" : "NO") << endl; return 0; } inline int Yes(bool x) { cout << ((x) ? "Yes" : "No") << endl; return 0; } inline int yes(bool x) { cout << ((x) ? "yes" : "no") << endl; return 0; } inline int yES(bool x) { cout << ((x) ? "yES" : "nO") << endl; return 0; } inline int Yay(bool x) { cout << ((x) ? "Yay!" : ":(") << endl; return 0; } template <typename A, typename B> void sankou(bool x, A a, B b) { cout << ((x) ? (a) : (b)) << endl; } #define _overload3(_1, _2, _3, name, ...) name #define _REP(i, n) REPI(i, 0, n) #define REPI(i, a, b) for (ll i = ll(a); i < ll(b); ++i) #define REP(...) _overload3(__VA_ARGS__, REPI, _REP, )(__VA_ARGS__) #define _RREP(i, n) RREPI(i, n, 0) #define RREPI(i, a, b) for (ll i = ll(a); i >= ll(b); --i) #define RREP(...) _overload3(__VA_ARGS__, RREPI, _RREP, )(__VA_ARGS__) #define EACH(e, v) for (auto &e : v) #define PERM(v) \ sort((v).begin(), (v).end()); \ for (bool c##p = 1; c##p; c##p = next_permutation((v).begin(), (v).end())) #define ADD(a, b) a = (a + ll(b)) % MOD #define MUL(a, b) a = (a * ll(b)) % MOD inline ll MOP(ll x, ll n, ll m = MOD) { ll r = 1; while (n > 0) { if (n & 1) (r *= x) %= m; (x *= x) %= m; n >>= 1; } return r; } inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); } inline ll POW(ll a, ll b) { ll c = 1ll; do { if (b & 1) c *= 1ll * a; a *= 1ll * a; } while (b >>= 1); return c; } template <typename T, typename A, typename B> inline bool between(T x, A a, B b) { return ((a <= x) && (x < b)); } template <class T> inline T sqr(T x) { return x * x; } template <typename A, typename B> inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return 1; } return 0; } #define tmax(x, y, z) max((x), max((y), (z))) #define tmin(x, y, z) min((x), min((y), (z))) #define PB emplace_back #define MP make_pair #define MT make_tuple #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define EXIST(s, e) (find((s).begin(), (s).end(), (e)) != (s).end()) #define EXISTST(s, c) (((s).find(c)) != string::npos) #define POSL(x, val) (lower_bound(x.begin(), x.end(), val) - x.begin()) #define POSU(x, val) (upper_bound(x.begin(), x.end(), val) - x.begin()) #define GEQ(x, val) (int)(x).size() - POSL((x), (val)) #define GREATER(x, val) (int)(x).size() - POSU((x), (val)) #define LEQ(x, val) POSU((x), (val)) #define LESS(x, val) POSL((x), (val)) #define SZV(a) int((a).size()) #define SZA(a) sizeof(a) / sizeof(a[0]) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MEMINF(a) memset(a, 0x3f, sizeof(a)) #define FILL(a, b) memset(a, b, sizeof(a)) #define UNIQUE(v) \ sort((v).begin(), (v).end()); \ (v).erase(unique((v).begin(), (v).end()), (v).end()) struct abracadabra { abracadabra() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); cerr << fixed << setprecision(5); }; } ABRACADABRA; // TEMPLATE // END---------------8<---------------8<---------------8<---------------8<---------------// struct SegmentMap : public map<int_fast64_t, int_fast64_t> { bool flagToMergeAdjacentSegment; SegmentMap(bool flagToMergeAdjacentSegment = true) : flagToMergeAdjacentSegment(flagToMergeAdjacentSegment) {} auto get(int_fast64_t p) const { auto it = upper_bound(p); if (it == begin() or (--it)->second < p) return end(); return it; } void insert(int_fast64_t l, int_fast64_t r) { auto itl = upper_bound(l), itr = upper_bound(r + flagToMergeAdjacentSegment); if (itl != begin()) { if ((--itl)->second < l - flagToMergeAdjacentSegment) ++itl; } if (itl != itr) { l = min(l, itl->first); r = max(r, prev(itr)->second); erase(itl, itr); } (*this)[l] = r; } void remove(int_fast64_t l, int_fast64_t r) { auto itl = upper_bound(l), itr = upper_bound(r); if (itl != begin()) { if ((--itl)->second < l) ++itl; } if (itl == itr) return; int tl = min(l, itl->first), tr = max(r, prev(itr)->second); erase(itl, itr); if (tl < l) (*this)[tl] = l - 1; if (r < tr) (*this)[r + 1] = tr; } bool same(int_fast64_t p, int_fast64_t q) const { const auto &&it = get(p); return it != end() and it->first <= q and q <= it->second; } }; /* ・SegmentMap [備考] 区間をmapで管理するやつ [注意] 以下で扱う区間は全て閉区間 [使用例] SegmentMap mp(flag); // flagがtrueなら, [l, c]と[c + 1, r]をマージする mp.get(p); // pを含む区間を返す(存在しないならmp.end()) mp.insert(l, r); // 閉区間[l, r]を追加する mp.remove(l, r); // 閉区間[l, r]を取り除く mp.same(p, q); // 2点p, qが同じ区間にあればtrue */ /* 正負で対称 奇数のときは0の分(サンプル3) S-TじゃなくてSの通り数で良くない? */ signed main() { SS(int, N, X, D); if (D == 0) { // print(N + 1); assert(0); return 0; } map<int, SegmentMap> mps; REP(i, N + 1) { ll bas = i * X; ll frm = i * (i - 1) / 2; ll len = i * N - (i * i - 1); mps[bas % D].insert(bas / D + frm, bas / D + frm + len - 1); } ll res = 0; EACH(mp, mps) { EACH(e, mp.second) { ll l, r; tie(l, r) = e; // print(l, r); res += r - l + 1; } } print(res); // cerr << "D == 0のときの書いた?" << endl; }
#include <bits/stdc++.h> using namespace std; // #define int long long // TEMPLATE // START---------------8<---------------8<---------------8<---------------8<---------------// typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int>> vvi; const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int MOD = 1e9 + 7; const int MODD = 998244353; const string alphabet = "abcdefghijklmnopqrstuvwxyz"; const double PI = acos(-1.0); const double EPS = 1e-9; const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; int dy[9] = {0, 1, 0, -1, -1, -1, 1, 1, 0}; #define ln '\n' #define scnaf scanf #define sacnf scanf #define sancf scanf #define SS(type, ...) \ type __VA_ARGS__; \ MACRO_VAR_Scan(__VA_ARGS__); template <typename T> void MACRO_VAR_Scan(T &t) { cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First &first, Rest &...rest) { cin >> first; MACRO_VAR_Scan(rest...); } #define SV(type, c, n) \ vector<type> c(n); \ for (auto &i : c) \ cin >> i; #define SVV(type, c, n, m) \ vector<vector<type>> c(n, vector<type>(m)); \ for (auto &r : c) \ for (auto &i : r) \ cin >> i; template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &j) { o << "{" << j.first << ", " << j.second << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const map<T, U> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const multiset<T> &j) { o << "{"; for (auto t = j.begin(); t != j.end(); ++t) o << (t != j.begin() ? ", " : "") << *t; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &j) { o << "{"; for (int i = 0; i < (int)j.size(); ++i) o << (i > 0 ? ", " : "") << j[i]; o << "}"; return o; } inline int print(void) { cout << endl; return 0; } template <class Head> int print(Head &&head) { cout << head; print(); return 0; } template <class Head, class... Tail> int print(Head &&head, Tail &&...tail) { cout << head << " "; print(forward<Tail>(tail)...); return 0; } #ifdef LOCAL inline int dump(void) { cerr << endl; return 0; } template <class Head> int dump(Head &&head) { cerr << head; dump(); return 0; } template <class Head, class... Tail> int dump(Head &&head, Tail &&...tail) { cerr << head << " "; dump(forward<Tail>(tail)...); return 0; } #define debug(...) \ do { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ dump(__VA_ARGS__); \ } while (0) #define ER(x) cerr << #x << " = " << (x) << endl; #define ERV(v) \ { \ cerr << #v << " : "; \ for (const auto &xxx : v) { \ cerr << xxx << " "; \ } \ cerr << "\n"; \ } #else #define dump(...) #define debug(...) #define ER(x) #define ERV(v) #endif template <typename T> void PA(T &a) { int ASIZE = sizeof(a) / sizeof(a[0]); for (int ii = 0; ii < ASIZE; ++ii) { cout << a[ii] << " \n"[ii == ASIZE - 1]; } } template <typename T> void PV(T &v) { int VSIZE = v.size(); for (int ii = 0; ii < VSIZE; ++ii) { cout << v[ii] << " \n"[ii == VSIZE - 1]; } } inline int YES(bool x) { cout << ((x) ? "YES" : "NO") << endl; return 0; } inline int Yes(bool x) { cout << ((x) ? "Yes" : "No") << endl; return 0; } inline int yes(bool x) { cout << ((x) ? "yes" : "no") << endl; return 0; } inline int yES(bool x) { cout << ((x) ? "yES" : "nO") << endl; return 0; } inline int Yay(bool x) { cout << ((x) ? "Yay!" : ":(") << endl; return 0; } template <typename A, typename B> void sankou(bool x, A a, B b) { cout << ((x) ? (a) : (b)) << endl; } #define _overload3(_1, _2, _3, name, ...) name #define _REP(i, n) REPI(i, 0, n) #define REPI(i, a, b) for (ll i = ll(a); i < ll(b); ++i) #define REP(...) _overload3(__VA_ARGS__, REPI, _REP, )(__VA_ARGS__) #define _RREP(i, n) RREPI(i, n, 0) #define RREPI(i, a, b) for (ll i = ll(a); i >= ll(b); --i) #define RREP(...) _overload3(__VA_ARGS__, RREPI, _RREP, )(__VA_ARGS__) #define EACH(e, v) for (auto &e : v) #define PERM(v) \ sort((v).begin(), (v).end()); \ for (bool c##p = 1; c##p; c##p = next_permutation((v).begin(), (v).end())) #define ADD(a, b) a = (a + ll(b)) % MOD #define MUL(a, b) a = (a * ll(b)) % MOD inline ll MOP(ll x, ll n, ll m = MOD) { ll r = 1; while (n > 0) { if (n & 1) (r *= x) %= m; (x *= x) %= m; n >>= 1; } return r; } inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); } inline ll POW(ll a, ll b) { ll c = 1ll; do { if (b & 1) c *= 1ll * a; a *= 1ll * a; } while (b >>= 1); return c; } template <typename T, typename A, typename B> inline bool between(T x, A a, B b) { return ((a <= x) && (x < b)); } template <class T> inline T sqr(T x) { return x * x; } template <typename A, typename B> inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return 1; } return 0; } #define tmax(x, y, z) max((x), max((y), (z))) #define tmin(x, y, z) min((x), min((y), (z))) #define PB emplace_back #define MP make_pair #define MT make_tuple #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define EXIST(s, e) (find((s).begin(), (s).end(), (e)) != (s).end()) #define EXISTST(s, c) (((s).find(c)) != string::npos) #define POSL(x, val) (lower_bound(x.begin(), x.end(), val) - x.begin()) #define POSU(x, val) (upper_bound(x.begin(), x.end(), val) - x.begin()) #define GEQ(x, val) (int)(x).size() - POSL((x), (val)) #define GREATER(x, val) (int)(x).size() - POSU((x), (val)) #define LEQ(x, val) POSU((x), (val)) #define LESS(x, val) POSL((x), (val)) #define SZV(a) int((a).size()) #define SZA(a) sizeof(a) / sizeof(a[0]) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MEMINF(a) memset(a, 0x3f, sizeof(a)) #define FILL(a, b) memset(a, b, sizeof(a)) #define UNIQUE(v) \ sort((v).begin(), (v).end()); \ (v).erase(unique((v).begin(), (v).end()), (v).end()) struct abracadabra { abracadabra() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); cerr << fixed << setprecision(5); }; } ABRACADABRA; // TEMPLATE // END---------------8<---------------8<---------------8<---------------8<---------------// struct SegmentMap : public map<int_fast64_t, int_fast64_t> { bool flagToMergeAdjacentSegment; SegmentMap(bool flagToMergeAdjacentSegment = true) : flagToMergeAdjacentSegment(flagToMergeAdjacentSegment) {} auto get(int_fast64_t p) const { auto it = upper_bound(p); if (it == begin() or (--it)->second < p) return end(); return it; } void insert(int_fast64_t l, int_fast64_t r) { auto itl = upper_bound(l), itr = upper_bound(r + flagToMergeAdjacentSegment); if (itl != begin()) { if ((--itl)->second < l - flagToMergeAdjacentSegment) ++itl; } if (itl != itr) { l = min(l, itl->first); r = max(r, prev(itr)->second); erase(itl, itr); } (*this)[l] = r; } void remove(int_fast64_t l, int_fast64_t r) { auto itl = upper_bound(l), itr = upper_bound(r); if (itl != begin()) { if ((--itl)->second < l) ++itl; } if (itl == itr) return; int tl = min(l, itl->first), tr = max(r, prev(itr)->second); erase(itl, itr); if (tl < l) (*this)[tl] = l - 1; if (r < tr) (*this)[r + 1] = tr; } bool same(int_fast64_t p, int_fast64_t q) const { const auto &&it = get(p); return it != end() and it->first <= q and q <= it->second; } }; /* ・SegmentMap [備考] 区間をmapで管理するやつ [注意] 以下で扱う区間は全て閉区間 [使用例] SegmentMap mp(flag); // flagがtrueなら, [l, c]と[c + 1, r]をマージする mp.get(p); // pを含む区間を返す(存在しないならmp.end()) mp.insert(l, r); // 閉区間[l, r]を追加する mp.remove(l, r); // 閉区間[l, r]を取り除く mp.same(p, q); // 2点p, qが同じ区間にあればtrue */ /* 正負で対称 奇数のときは0の分(サンプル3) S-TじゃなくてSの通り数で良くない? */ signed main() { SS(int, N, X, D); if (D == 0) { if (X == 0) print(1); else print(N + 1); return 0; } map<int, SegmentMap> mps; REP(i, N + 1) { ll bas = i * X; ll frm = i * (i - 1) / 2; ll len = i * N - (i * i - 1); mps[bas % D].insert(bas / D + frm, bas / D + frm + len - 1); } ll res = 0; EACH(mp, mps) { EACH(e, mp.second) { ll l, r; tie(l, r) = e; // print(l, r); res += r - l + 1; } } print(res); // cerr << "D == 0のときの書いた?" << endl; }
replace
326
328
326
330
0
p02840
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll M = 1e9 + 7; const ll INF = 1e9 + 7; ll n, x, d, cnt, ans; priority_queue<P, vector<P>, greater<P>> pq[220000]; map<ll, ll> mp; int main() { cin >> n >> x >> d; if (d < 0) x = -x, d = -d; for (ll p = 0; p <= n; p++) { ll xp = x * p; ll st = xp / d + p * (p - 1) / 2; ll gl = xp / d + p * (2 * n - p - 1) / 2; if (mp[xp % d] == 0) mp[xp % d] = ++cnt; pq[mp[xp % d]].push(P(st, gl)); } for (ll i = 0; i < 210000; i++) { ll s = -1e18; while (pq[i].size()) { ll nt = pq[i].top().first; ll ns = pq[i].top().second; pq[i].pop(); if (s < ns) { ans += ns - max(s, nt - 1); s = ns; } } } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll M = 1e9 + 7; const ll INF = 1e9 + 7; ll n, x, d, cnt, ans; priority_queue<P, vector<P>, greater<P>> pq[220000]; map<ll, ll> mp; int main() { cin >> n >> x >> d; if (d == 0) { if (x == 0) { cout << 1 << endl; return 0; } else { cout << n + 1 << endl; return 0; } } if (d < 0) x = -x, d = -d; for (ll p = 0; p <= n; p++) { ll xp = x * p; ll st = xp / d + p * (p - 1) / 2; ll gl = xp / d + p * (2 * n - p - 1) / 2; if (mp[xp % d] == 0) mp[xp % d] = ++cnt; pq[mp[xp % d]].push(P(st, gl)); } for (ll i = 0; i < 210000; i++) { ll s = -1e18; while (pq[i].size()) { ll nt = pq[i].top().first; ll ns = pq[i].top().second; pq[i].pop(); if (s < ns) { ans += ns - max(s, nt - 1); s = ns; } } } cout << ans << endl; return 0; }
insert
27
27
27
37
0
p02840
C++
Runtime Error
#ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; #define rep(i, s, t) for (ll i = (ll)(s); i < (ll)(t); i++) #define rrep(i, s, t) for (ll i = (ll)(s - 1); (ll)(t) <= i; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<ll, ll> Pll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; constexpr ll INF = numeric_limits<ll>::max() / 4; constexpr ll n_max = 2e5 + 10; #define int ll template <typename A, typename B> string to_string(pair<A, B> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *c) { return to_string((string)c); } string to_string(bool b) { return (b ? "true" : "false"); } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; } 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; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll n, x, d; cin >> n >> x >> d; if (d == 0) { assert(false); cout << n + 1 << endl; return 0; } map<ll, set<Pll>> mp; ll min_, max_; rep(i, 0, n + 1) { min_ = x * i + d * (i * (i - 1) / 2); max_ = x * i + d * ((2 * n - i - 1) * i / 2); debug(min_, max_); max_ += d; if (min_ > max_) swap(min_, max_); debug(min_, max_); ll m = min_ % d; if (m < 0) m += d; if (mp[m].empty()) { mp[m].insert({min_, max_}); continue; } auto itr = mp[m].lower_bound({min_, -INF}); ll e = min_; if (itr != mp[m].begin()) { itr--; if (itr->second >= min_) { min_ = itr->first; e = itr->second; auto itr2 = itr; itr++; mp[m].erase(itr2); } else itr++; } bool is_end = false; while (e < max_) { if (itr == mp[m].end()) { is_end = true; break; } if (itr->first > max_) { e = max_; break; } e = itr->second; auto itr2 = itr; itr++; mp[m].erase(itr2); } if (is_end) { e = max_; } mp[m].insert({min_, e}); } ll ans = 0; for (auto &st : mp) { debug(st.first); for (auto &p : st.second) { debug(p); ans += (p.second - p.first) / abs(d); } } cout << ans << endl; }
#ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; #define rep(i, s, t) for (ll i = (ll)(s); i < (ll)(t); i++) #define rrep(i, s, t) for (ll i = (ll)(s - 1); (ll)(t) <= i; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<ll, ll> Pll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; constexpr ll INF = numeric_limits<ll>::max() / 4; constexpr ll n_max = 2e5 + 10; #define int ll template <typename A, typename B> string to_string(pair<A, B> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *c) { return to_string((string)c); } string to_string(bool b) { return (b ? "true" : "false"); } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; } 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; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll n, x, d; cin >> n >> x >> d; if (d == 0) { if (x) cout << n + 1 << endl; else cout << 1 << endl; return 0; } map<ll, set<Pll>> mp; ll min_, max_; rep(i, 0, n + 1) { min_ = x * i + d * (i * (i - 1) / 2); max_ = x * i + d * ((2 * n - i - 1) * i / 2); debug(min_, max_); max_ += d; if (min_ > max_) swap(min_, max_); debug(min_, max_); ll m = min_ % d; if (m < 0) m += d; if (mp[m].empty()) { mp[m].insert({min_, max_}); continue; } auto itr = mp[m].lower_bound({min_, -INF}); ll e = min_; if (itr != mp[m].begin()) { itr--; if (itr->second >= min_) { min_ = itr->first; e = itr->second; auto itr2 = itr; itr++; mp[m].erase(itr2); } else itr++; } bool is_end = false; while (e < max_) { if (itr == mp[m].end()) { is_end = true; break; } if (itr->first > max_) { e = max_; break; } e = itr->second; auto itr2 = itr; itr++; mp[m].erase(itr2); } if (is_end) { e = max_; } mp[m].insert({min_, e}); } ll ans = 0; for (auto &st : mp) { debug(st.first); for (auto &p : st.second) { debug(p); ans += (p.second - p.first) / abs(d); } } cout << ans << endl; }
replace
77
79
77
81
0
p02840
C++
Runtime Error
#include <algorithm> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; long long int gcd(long long int a, long long int b) { if ((a % b) == 0) return b; return gcd(b, a % b); } long long int kansu(long long int p, long long int q, long long int pp, long long int qq) { // cout << " " << p << " " << q << " " << pp << " " << qq << endl; if (qq < p) return q - p + 1; if (q < pp) return q - p + 1; if (qq <= q && pp <= p) return q - qq; if (q <= qq && pp <= p) return 0; if (qq <= q) return (q - qq) + (pp - p); return pp - p; } int main() { long long int n, x, d; cin >> n >> x >> d; long long int gx = x; if (gx < 0) gx = -x; long long int gd = d; if (gd < 0) gd = -d; long long int g = gcd(gx, gd); if (g > 0) { gx = gx / g; gd = gd / g; } long long int p[214514] = {}; long long int q[214514] = {}; long long int ans = 0; for (long long int xx = 0; xx <= n; xx++) { p[xx] = (0 + xx - 1) * xx / 2; q[xx] = ((n - 1) + (n - 1 - (xx - 1))) * xx / 2; // cout << p[xx] << " " << q[xx] << endl; if (x == 0) { if (d == 0) { cout << 1 << endl; return 0; } else { if (xx == n) { cout << q[xx] + 1 << endl; return 0; } } } else if (x < 0) { if (d == 0) { cout << n + 1 << endl; return 0; } else if (d < 0) { long long int y = xx - gd; if (y < 0) { ans += q[xx] - p[xx] + 1; } else { long long int pp = p[y] - gx; long long int qq = q[y] - gx; ans += kansu(p[xx], q[xx], pp, qq); } } else { long long int y = xx - gd; if (y < 0) { ans += q[xx] - p[xx] + 1; } else { long long int pp = p[y] + gx; long long int qq = q[y] + gx; ans += kansu(p[xx], q[xx], pp, qq); } } } else { if (d == 0) { cout << n + 1 << endl; return 0; } else if (d < 0) { long long int y = xx - gd; if (y < 0) { ans += q[xx] - p[xx] + 1; } else { long long int pp = p[y] + gx; long long int qq = q[y] + gx; ans += kansu(p[xx], q[xx], pp, qq); } } else { long long int y = xx - gd; if (y < 0) { ans += q[xx] - p[xx] + 1; } else { long long int pp = p[y] - gx; long long int qq = q[y] - gx; ans += kansu(p[xx], q[xx], pp, qq); } } } } cout << ans << endl; return 0; }
#include <algorithm> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; long long int gcd(long long int a, long long int b) { if ((a % b) == 0) return b; return gcd(b, a % b); } long long int kansu(long long int p, long long int q, long long int pp, long long int qq) { // cout << " " << p << " " << q << " " << pp << " " << qq << endl; if (qq < p) return q - p + 1; if (q < pp) return q - p + 1; if (qq <= q && pp <= p) return q - qq; if (q <= qq && pp <= p) return 0; if (qq <= q) return (q - qq) + (pp - p); return pp - p; } int main() { long long int n, x, d; cin >> n >> x >> d; long long int gx = x; if (gx < 0) gx = -x; long long int gd = d; if (gd < 0) gd = -d; long long int g; if (x != 0 && d != 0) { g = gcd(gx, gd); gx = gx / g; gd = gd / g; } long long int p[214514] = {}; long long int q[214514] = {}; long long int ans = 0; for (long long int xx = 0; xx <= n; xx++) { p[xx] = (0 + xx - 1) * xx / 2; q[xx] = ((n - 1) + (n - 1 - (xx - 1))) * xx / 2; // cout << p[xx] << " " << q[xx] << endl; if (x == 0) { if (d == 0) { cout << 1 << endl; return 0; } else { if (xx == n) { cout << q[xx] + 1 << endl; return 0; } } } else if (x < 0) { if (d == 0) { cout << n + 1 << endl; return 0; } else if (d < 0) { long long int y = xx - gd; if (y < 0) { ans += q[xx] - p[xx] + 1; } else { long long int pp = p[y] - gx; long long int qq = q[y] - gx; ans += kansu(p[xx], q[xx], pp, qq); } } else { long long int y = xx - gd; if (y < 0) { ans += q[xx] - p[xx] + 1; } else { long long int pp = p[y] + gx; long long int qq = q[y] + gx; ans += kansu(p[xx], q[xx], pp, qq); } } } else { if (d == 0) { cout << n + 1 << endl; return 0; } else if (d < 0) { long long int y = xx - gd; if (y < 0) { ans += q[xx] - p[xx] + 1; } else { long long int pp = p[y] + gx; long long int qq = q[y] + gx; ans += kansu(p[xx], q[xx], pp, qq); } } else { long long int y = xx - gd; if (y < 0) { ans += q[xx] - p[xx] + 1; } else { long long int pp = p[y] - gx; long long int qq = q[y] - gx; ans += kansu(p[xx], q[xx], pp, qq); } } } } cout << ans << endl; return 0; }
replace
41
43
41
44
0
p02840
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll, int> P; const int INF = (1 << 30); const ll INFLL = (1ll << 60); const ll MOD = (ll)(1e9 + 7); #define l_ength size void mul_mod(ll &a, ll b) { a *= b; a %= MOD; } void add_mod(ll &a, ll b) { a = (a < MOD) ? a : (a - MOD); b = (b < MOD) ? b : (b - MOD); a += b; a = (a < MOD) ? a : (a - MOD); } ll a[225816], s[225816], l[225816], r[225816]; map<ll, int> mp; vector<P> v[225816]; int main(void) { int n, i, j, p, m; bool flag; ll x, d, q, ans = 0ll; cin >> n >> a[0] >> d; for (i = 1; i < n; ++i) { a[i] = a[i - 1] + d; } if (d < 0) { reverse(a, a + n); d = -d; } for (i = 0; i < n; ++i) { a[i] *= 2; s[i + 1] = s[i] + a[i]; } x = a[0]; d *= 2; x %= d; x += d; x %= d; mp[0] = 0; v[0].push_back(P(-d, 1)); v[0].push_back(P(0ll, -1)); for (i = 1; i <= n; ++i) { q = x * i % d; if (mp.find(q) == mp.end()) { mp[q] = i; } p = mp[q]; l[i] = s[i] - d; r[i] = s[n] - s[n - i]; v[p].push_back(P(l[i], 1)); v[p].push_back(P(r[i], -1)); } for (i = 0; i <= n; ++i) { m = v[i].l_ength(); sort(v[i].begin(), v[i].end()); for (j = 1; j < m; ++j) { v[i][j].second += v[i][j - 1].second; } flag = false; q = 0ll; for (j = 0; j < m; ++j) { if ((!flag) && v[i][j].second) { q -= v[i][j].first; flag = true; } else if (flag && (!v[i][j].second)) { q += v[i][j].first; flag = false; } } ans += q / d; } cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll, int> P; const int INF = (1 << 30); const ll INFLL = (1ll << 60); const ll MOD = (ll)(1e9 + 7); #define l_ength size void mul_mod(ll &a, ll b) { a *= b; a %= MOD; } void add_mod(ll &a, ll b) { a = (a < MOD) ? a : (a - MOD); b = (b < MOD) ? b : (b - MOD); a += b; a = (a < MOD) ? a : (a - MOD); } ll a[225816], s[225816], l[225816], r[225816]; map<ll, int> mp; vector<P> v[225816]; int main(void) { int n, i, j, p, m; bool flag; ll x, d, q, ans = 0ll; cin >> n >> a[0] >> d; for (i = 1; i < n; ++i) { a[i] = a[i - 1] + d; } if (!d) { cout << (a[0] ? (n + 1) : 1) << endl; return 0; } if (d < 0) { reverse(a, a + n); d = -d; } for (i = 0; i < n; ++i) { a[i] *= 2; s[i + 1] = s[i] + a[i]; } x = a[0]; d *= 2; x %= d; x += d; x %= d; mp[0] = 0; v[0].push_back(P(-d, 1)); v[0].push_back(P(0ll, -1)); for (i = 1; i <= n; ++i) { q = x * i % d; if (mp.find(q) == mp.end()) { mp[q] = i; } p = mp[q]; l[i] = s[i] - d; r[i] = s[n] - s[n - i]; v[p].push_back(P(l[i], 1)); v[p].push_back(P(r[i], -1)); } for (i = 0; i <= n; ++i) { m = v[i].l_ength(); sort(v[i].begin(), v[i].end()); for (j = 1; j < m; ++j) { v[i][j].second += v[i][j - 1].second; } flag = false; q = 0ll; for (j = 0; j < m; ++j) { if ((!flag) && v[i][j].second) { q -= v[i][j].first; flag = true; } else if (flag && (!v[i][j].second)) { q += v[i][j].first; flag = false; } } ans += q / d; } cout << ans << endl; return 0; }
insert
33
33
33
37
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; struct line { ll l, r; bool operator<(const line &rhs) const { return r < rhs.l; } }; unordered_map<int, set<line>> eq; void add(int ind, ll l, ll r) { line x = {l, r}; while (1) { auto it = eq[ind].find(x); if (it == eq[ind].end()) break; x = {min(x.l, it->l), max(x.r, it->r)}; eq[ind].erase(it); } eq[ind].insert(x); } int main() { ll N, X, D; scanf("%lld%lld%lld", &N, &X, &D); if (D < 0) { X += (N - 1) * D; D = -D; } for (ll k = 0; k <= N; k++) { int ind = (k * X) % D; if (ind < 0) ind += D; ll det = (ind - k * X) / D; add(ind, k * (k - 1) / 2 - det, k * (2 * N - k - 1) / 2 - det); } ll ans = 0; for (auto item : eq) for (auto x : item.second) ans += x.r - x.l + 1; printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; struct line { ll l, r; bool operator<(const line &rhs) const { return r < rhs.l; } }; unordered_map<int, set<line>> eq; void add(int ind, ll l, ll r) { line x = {l, r}; while (1) { auto it = eq[ind].find(x); if (it == eq[ind].end()) break; x = {min(x.l, it->l), max(x.r, it->r)}; eq[ind].erase(it); } eq[ind].insert(x); } int main() { ll N, X, D; scanf("%lld%lld%lld", &N, &X, &D); if (!D) { printf("%lld\n", X ? N + 1 : 1ll); return 0; } if (D < 0) { X += (N - 1) * D; D = -D; } for (ll k = 0; k <= N; k++) { int ind = (k * X) % D; if (ind < 0) ind += D; ll det = (ind - k * X) / D; add(ind, k * (k - 1) / 2 - det, k * (2 * N - k - 1) / 2 - det); } ll ans = 0; for (auto item : eq) for (auto x : item.second) ans += x.r - x.l + 1; printf("%lld\n", ans); return 0; }
insert
22
22
22
26
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define Fast_IO ios::sync_with_stdio(false); #define int long long inline int read() { char ch = getchar(); int nega = 1; while (!isdigit(ch)) { if (ch == '-') nega = -1; ch = getchar(); } int ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; return ans; } #define N 200005 int f[N], sum[N]; signed main() { #ifdef __LOCAL__ freopen("in.txt", "r", stdin); #endif int n, x, d; cin >> n >> x >> d; if (x < 0 && d < 0) x = -x, d = -d; for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + i - 1; int k = __gcd(x, d); x /= k, d /= k; int ans = 0; if (x >= 0 && d >= 0) { for (int i = 0; i <= n; i++) { int l = sum[i], r = sum[n] - sum[n - i]; if (l > r) swap(l, r); ans += r - l + 1; if (i + d <= n) { l = max(l, sum[i + d] + x); ans -= max(0LL, r - l + 1); } } } else { if (x < 0) x *= -1; if (d < 0) d *= -1; for (int i = 0; i <= n; i++) { int l = sum[i], r = sum[n] - sum[n - i]; if (l > r) swap(l, r); ans += r - l + 1; if (i + d <= n) { r = min(r, sum[n] - sum[n - i - d] - x); l = max(l, sum[i + d] - x); ans -= max(0LL, r - l + 1); } } } cout << ans << endl; #ifdef __LOCAL__ printf("Time Used : %d\n", clock()); #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define Fast_IO ios::sync_with_stdio(false); #define int long long inline int read() { char ch = getchar(); int nega = 1; while (!isdigit(ch)) { if (ch == '-') nega = -1; ch = getchar(); } int ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; return ans; } #define N 200005 int f[N], sum[N]; signed main() { #ifdef __LOCAL__ freopen("in.txt", "r", stdin); #endif int n, x, d; cin >> n >> x >> d; if (x == 0 && d == 0) { cout << "1\n"; return 0; } if (x < 0 && d < 0) x = -x, d = -d; for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + i - 1; int k = __gcd(x, d); x /= k, d /= k; int ans = 0; if (x >= 0 && d >= 0) { for (int i = 0; i <= n; i++) { int l = sum[i], r = sum[n] - sum[n - i]; if (l > r) swap(l, r); ans += r - l + 1; if (i + d <= n) { l = max(l, sum[i + d] + x); ans -= max(0LL, r - l + 1); } } } else { if (x < 0) x *= -1; if (d < 0) d *= -1; for (int i = 0; i <= n; i++) { int l = sum[i], r = sum[n] - sum[n - i]; if (l > r) swap(l, r); ans += r - l + 1; if (i + d <= n) { r = min(r, sum[n] - sum[n - i - d] - x); l = max(l, sum[i + d] - x); ans -= max(0LL, r - l + 1); } } } cout << ans << endl; #ifdef __LOCAL__ printf("Time Used : %d\n", clock()); #endif return 0; }
insert
31
31
31
35
0
p02840
C++
Runtime Error
#include <iostream> #include <list> #include <map> #include <utility> #include <vector> using namespace std; void merge(list<pair<long long, long long>> &L, long long mn, long long mx) { for (auto p = L.begin(); p != L.end();) { if (mn <= p->first && p->second <= mx) { p = L.erase(p); } else if (p->first <= mn && mx <= p->second) { mn = p->first; mx = p->second; p = L.erase(p); } else if (mn <= p->first && p->first <= mx) { mx = p->second; p = L.erase(p); } else if (p->first <= mn && mn <= p->second) { mn = p->first; p = L.erase(p); } else p++; } L.push_back(make_pair(mn, mx)); } int main() { long long N, X, D; cin >> N >> X >> D; if (D < 0) { X *= -1LL; D *= -1LL; } map<long long, list<pair<long long, long long>>> M; long long mn = 0; long long mx = 0; M[0].push_back(make_pair(0LL, 0LL)); for (long long i = 0; i < N; i++) { mn += i; mx += N - i - 1; long long t = X * (i + 1); list<pair<long long, long long>> &L = M[(t % D + D) % D]; t -= (t % D + D) % D; t /= D; merge(L, t + mn, t + mx); } long long ans = 0; for (auto L : M) for (auto e : L.second) ans += e.second - e.first + 1; cout << ans << endl; }
#include <iostream> #include <list> #include <map> #include <utility> #include <vector> using namespace std; void merge(list<pair<long long, long long>> &L, long long mn, long long mx) { for (auto p = L.begin(); p != L.end();) { if (mn <= p->first && p->second <= mx) { p = L.erase(p); } else if (p->first <= mn && mx <= p->second) { mn = p->first; mx = p->second; p = L.erase(p); } else if (mn <= p->first && p->first <= mx) { mx = p->second; p = L.erase(p); } else if (p->first <= mn && mn <= p->second) { mn = p->first; p = L.erase(p); } else p++; } L.push_back(make_pair(mn, mx)); } int main() { long long N, X, D; cin >> N >> X >> D; if (D < 0) { X *= -1LL; D *= -1LL; } if (D == 0) { cout << (X == 0 ? 1 : N + 1) << endl; return 0; } map<long long, list<pair<long long, long long>>> M; long long mn = 0; long long mx = 0; M[0].push_back(make_pair(0LL, 0LL)); for (long long i = 0; i < N; i++) { mn += i; mx += N - i - 1; long long t = X * (i + 1); list<pair<long long, long long>> &L = M[(t % D + D) % D]; t -= (t % D + D) % D; t /= D; merge(L, t + mn, t + mx); } long long ans = 0; for (auto L : M) for (auto e : L.second) ans += e.second - e.first + 1; cout << ans << endl; }
insert
36
36
36
41
0
p02840
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define Inf 1000000000000000000 #define nmax_def 110000 #define mod 1000000007 #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; ll gcd(ll a, ll b) { if (a == 0) return b; if (b == 0) return 0; ll x; while (1) { x = a % b; if (x == 0) return b; else { a = b; b = x; } } } int main() { ll N, X, D; cin >> N >> X >> D; if (D < 0) { X *= -1; D *= -1; } ll ans = 0; if (D == 0) { if (X == 0) ans = 1; else ans = N + 1; } else { ll iprev_dist = D / gcd(X, D); vector<pll> sumLR(N + 1); // 取る個数 rep(i, N + 1) { sumLR[i].first = X * i + D * i * (i - 1) / 2; sumLR[i].second = sumLR[i].first + D * i * (N - i); if (sumLR[i].first > sumLR[i].second) swap(sumLR[i].first, sumLR[i].second); ans += (sumLR[i].second - sumLR[i].first) / D + 1; int iprev = i - iprev_dist; if (iprev >= 0) { pll sumLR_excluded = sumLR[iprev]; sumLR_excluded.first = max(sumLR_excluded.first, sumLR[i].first); sumLR_excluded.second = min(sumLR_excluded.second, sumLR[i].second); if (sumLR_excluded.first <= sumLR_excluded.second) ans -= (sumLR_excluded.second - sumLR_excluded.first) / D + 1; } } } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define Inf 1000000000000000000 #define nmax_def 110000 #define mod 1000000007 #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; ll gcd(ll a, ll b) { if (a == 0) return b; if (b == 0) return 0; ll x; while (1) { x = a % b; if (x == 0) return b; else { a = b; b = x; } } } int main() { ll N, X, D; cin >> N >> X >> D; if (D < 0) { X *= -1; D *= -1; } ll ans = 0; if (D == 0) { if (X == 0) ans = 1; else ans = N + 1; } else { ll iprev_dist = D / gcd(abs(X), D); vector<pll> sumLR(N + 1); // 取る個数 rep(i, N + 1) { sumLR[i].first = X * i + D * i * (i - 1) / 2; sumLR[i].second = sumLR[i].first + D * i * (N - i); if (sumLR[i].first > sumLR[i].second) swap(sumLR[i].first, sumLR[i].second); ans += (sumLR[i].second - sumLR[i].first) / D + 1; int iprev = i - iprev_dist; if (iprev >= 0) { pll sumLR_excluded = sumLR[iprev]; sumLR_excluded.first = max(sumLR_excluded.first, sumLR[i].first); sumLR_excluded.second = min(sumLR_excluded.second, sumLR[i].second); if (sumLR_excluded.first <= sumLR_excluded.second) ans -= (sumLR_excluded.second - sumLR_excluded.first) / D + 1; } } } cout << ans << endl; return 0; }
replace
53
54
53
54
0
p02840
C++
Runtime Error
// clang-format off #include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <string> #include <algorithm> #include <numeric> #include <limits> #include <chrono> #include <functional> #include <iterator> #include <random> #include <complex> #include <utility> #include <type_traits> #include <tuple> #include <memory> #include <vector> #include <array> #include <list> #include <stack> #include <queue> #include <deque> #include <unordered_map> #include <unordered_set> #include <map> #include <set> #include <valarray> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <csignal> #include <cstdint> #include <cstddef> #ifdef ONLINE_JUDGE #define NDEBUG #endif #include <cassert> #include <stdexcept> #include <exception> // << pair template <typename T1, typename T2>std::ostream&operator<<(std::ostream&os,const std::pair<T1,T2>&p){return os<<"("<<p.first<<", "<<p.second<<")";} // << container template<typename,template<typename...> class...>struct is_instantiation_of:public std::false_type{}; template<template<typename...>class U,template<typename...>class...Us,typename...T>struct is_instantiation_of<U<T...>,U,Us...>:public std::true_type{}; template<typename S,template<typename...>class U,template<typename...>class...Us>struct is_instantiation_of<S,U,Us...>:std::integral_constant<bool,is_instantiation_of<S,Us...>::value>{}; template<typename C>std::ostream&_debug_go(std::ostream&,const C&); template<typename C,typename=std::enable_if_t<is_instantiation_of<C,std::vector,std::deque,std::set,std::map,std::unordered_set,std::unordered_map,std::multiset,std::multimap,std::unordered_multiset,std::unordered_multimap,std::valarray>::value>>std::ostream&operator<<(std::ostream&os,const C&v){return _debug_go(os,v);} template<typename T,std::size_t N> std::ostream& operator<<(std::ostream&os,const std::array<T,N>&v){return _debug_go(os,v);} template<typename C>std::ostream& _debug_go(std::ostream&os,const C& v){bool first=true;os<<"[";for(auto i:v){if(!first)os<<", ";else first=false;os<<i;}return os<<"]";} struct Debugger{template<typename T>Debugger&operator,(const T&v){std::cerr<<v<<" ";return *this;}} dbg; #ifndef NDEBUG #define debug(args...) do {dbg,args; cerr << "\n";} while(0) #else #define debug(args...) #endif #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ITER(i, s, e) for (auto i((s)); i != (e); ++i) #define IT(i, c) ITER(i, (c).begin(), (c).end()) using namespace std; using ui = uint32_t; using ll = int64_t; using ull = uint64_t; template <typename T = int> using Pt = complex<T>; template <typename T> T cross(const Pt<T>& a, const Pt<T>& b) { return a.real() * b.imag() - a.imag() * b.real(); } template <typename T> bool cmpPt(const Pt<T>& a, const Pt<T>& b) { return a.real() < b.real() || (a.real() == b.real() && a.imag() < b.imag()); } // END TEMPLATE // clang-format on int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; ll X, D; cin >> N >> X >> D; if (D < 0) { X = -X; D = -D; } // X = (X % D + D) % D; map<ll, vector<pair<ll, ll>>> qRangesByRem; qRangesByRem[0].push_back({0, 1}); ll lo = 0, hi = 0; REP(i, N) { lo += i, hi += N - i - 1; ll loval = X * (i + 1) + D * lo, hival = X * (i + 1) + D * hi; ll rem = (loval % D + D) % D; ll loq = loval / D - (loval < 0 && rem), hiq = hival / D - (hival < 0 && rem); qRangesByRem[rem].push_back({loq, hiq + 1}); } ll tot = 0; for (auto &kv : qRangesByRem) { auto &qRanges = kv.second; sort(qRanges.begin(), qRanges.end()); ll end = qRanges[0].first; for (auto range : qRanges) { ll l, r; tie(l, r) = range; if (r <= end) continue; tot += r - max(l, end); end = r; } } cout << tot << '\n'; return 0; }
// clang-format off #include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <string> #include <algorithm> #include <numeric> #include <limits> #include <chrono> #include <functional> #include <iterator> #include <random> #include <complex> #include <utility> #include <type_traits> #include <tuple> #include <memory> #include <vector> #include <array> #include <list> #include <stack> #include <queue> #include <deque> #include <unordered_map> #include <unordered_set> #include <map> #include <set> #include <valarray> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <csignal> #include <cstdint> #include <cstddef> #ifdef ONLINE_JUDGE #define NDEBUG #endif #include <cassert> #include <stdexcept> #include <exception> // << pair template <typename T1, typename T2>std::ostream&operator<<(std::ostream&os,const std::pair<T1,T2>&p){return os<<"("<<p.first<<", "<<p.second<<")";} // << container template<typename,template<typename...> class...>struct is_instantiation_of:public std::false_type{}; template<template<typename...>class U,template<typename...>class...Us,typename...T>struct is_instantiation_of<U<T...>,U,Us...>:public std::true_type{}; template<typename S,template<typename...>class U,template<typename...>class...Us>struct is_instantiation_of<S,U,Us...>:std::integral_constant<bool,is_instantiation_of<S,Us...>::value>{}; template<typename C>std::ostream&_debug_go(std::ostream&,const C&); template<typename C,typename=std::enable_if_t<is_instantiation_of<C,std::vector,std::deque,std::set,std::map,std::unordered_set,std::unordered_map,std::multiset,std::multimap,std::unordered_multiset,std::unordered_multimap,std::valarray>::value>>std::ostream&operator<<(std::ostream&os,const C&v){return _debug_go(os,v);} template<typename T,std::size_t N> std::ostream& operator<<(std::ostream&os,const std::array<T,N>&v){return _debug_go(os,v);} template<typename C>std::ostream& _debug_go(std::ostream&os,const C& v){bool first=true;os<<"[";for(auto i:v){if(!first)os<<", ";else first=false;os<<i;}return os<<"]";} struct Debugger{template<typename T>Debugger&operator,(const T&v){std::cerr<<v<<" ";return *this;}} dbg; #ifndef NDEBUG #define debug(args...) do {dbg,args; cerr << "\n";} while(0) #else #define debug(args...) #endif #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ITER(i, s, e) for (auto i((s)); i != (e); ++i) #define IT(i, c) ITER(i, (c).begin(), (c).end()) using namespace std; using ui = uint32_t; using ll = int64_t; using ull = uint64_t; template <typename T = int> using Pt = complex<T>; template <typename T> T cross(const Pt<T>& a, const Pt<T>& b) { return a.real() * b.imag() - a.imag() * b.real(); } template <typename T> bool cmpPt(const Pt<T>& a, const Pt<T>& b) { return a.real() < b.real() || (a.real() == b.real() && a.imag() < b.imag()); } // END TEMPLATE // clang-format on int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; ll X, D; cin >> N >> X >> D; if (D < 0) { X = -X; D = -D; } if (D == 0) { cout << (X == 0 ? 1 : N + 1) << '\n'; return 0; } map<ll, vector<pair<ll, ll>>> qRangesByRem; qRangesByRem[0].push_back({0, 1}); ll lo = 0, hi = 0; REP(i, N) { lo += i, hi += N - i - 1; ll loval = X * (i + 1) + D * lo, hival = X * (i + 1) + D * hi; ll rem = (loval % D + D) % D; ll loq = loval / D - (loval < 0 && rem), hiq = hival / D - (hival < 0 && rem); qRangesByRem[rem].push_back({loq, hiq + 1}); } ll tot = 0; for (auto &kv : qRangesByRem) { auto &qRanges = kv.second; sort(qRanges.begin(), qRanges.end()); ll end = qRanges[0].first; for (auto range : qRanges) { ll l, r; tie(l, r) = range; if (r <= end) continue; tot += r - max(l, end); end = r; } } cout << tot << '\n'; return 0; }
replace
100
101
100
104
0
p02840
C++
Runtime Error
// warm heart, wagging tail,and a smile just for you! // ███████████ // ███╬╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬████╬╬╬╬╬╬███ // ███████████ // ██╬╬╬╬╬████╬╬████╬╬╬╬╬██ // █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██ // ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██ // ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██ // ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████ // █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██ // ██████████████ // ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████ // ███████ █████ // ███████████████████ // #include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 // #define MOD 998244353 const double EPS = 1e-9; #define INF (1LL << 60) #define D double #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 << " " << typedef pair<int, int> P; typedef vector<int> vec; typedef vector<vector<int>> mat; int ans = 0; vector<P> sect; void calc() { sort(sect.begin(), sect.end()); int l = -INF, r = -INF; for (P p : sect) { if (r < p.fs) { ans += r - l; l = p.fs, r = p.sc; } else r = max(r, p.sc); // cout << l SP r << endl; } ans += r - l; sect.clear(); } signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, x, d; cin >> n >> x >> d; vector<P> a(n + 1); REP(i, n + 1) { a[i] = P(x * i % d, i); } sort(a.begin(), a.end()); int now = -1; REP(i, n + 1) { if (now != a[i].fs) { calc(); now = a[i].fs; } int base = x * a[i].sc / d; int l = a[i].sc * (a[i].sc - 1) / 2; int r = a[i].sc * (n - 1 + (n - a[i].sc)) / 2; sect.emplace_back(l + base, r + base + 1); } calc(); cout << ans << endl; return 0; }
// warm heart, wagging tail,and a smile just for you! // ███████████ // ███╬╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬████╬╬╬╬╬╬███ // ███████████ // ██╬╬╬╬╬████╬╬████╬╬╬╬╬██ // █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██ // ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██ // ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██ // ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████ // █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████ // ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███ // ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████ // ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██ // ██████████████ // ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████ // ███████ █████ // ███████████████████ // #include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD 1000000007 // #define MOD 998244353 const double EPS = 1e-9; #define INF (1LL << 60) #define D double #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 << " " << typedef pair<int, int> P; typedef vector<int> vec; typedef vector<vector<int>> mat; int ans = 0; vector<P> sect; void calc() { sort(sect.begin(), sect.end()); int l = -INF, r = -INF; for (P p : sect) { if (r < p.fs) { ans += r - l; l = p.fs, r = p.sc; } else r = max(r, p.sc); // cout << l SP r << endl; } ans += r - l; sect.clear(); } signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, x, d; cin >> n >> x >> d; if (d == 0) { if (x == 0) cout << 1 << endl; else cout << n + 1 << endl; return 0; } vector<P> a(n + 1); REP(i, n + 1) { a[i] = P(x * i % d, i); } sort(a.begin(), a.end()); int now = -1; REP(i, n + 1) { if (now != a[i].fs) { calc(); now = a[i].fs; } int base = x * a[i].sc / d; int l = a[i].sc * (a[i].sc - 1) / 2; int r = a[i].sc * (n - 1 + (n - a[i].sc)) / 2; sect.emplace_back(l + base, r + base + 1); } calc(); cout << ans << endl; return 0; }
insert
88
88
88
96
0
p02840
C++
Runtime Error
#include <algorithm> #include <bitset> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define Per(i, sta, n) for (int i = n - 1; i >= sta; i--) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef long double ld; typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; ll n, x, d; unordered_map<ll, vector<LP>> D; void solve() { cin >> n >> x >> d; if (d < 0) { x = (n - 1) * d + x; d *= -1; } for (ll i = 0; i <= n; i++) { D[i * x % d].push_back(LP(i * (i - 1) * d / 2 + i * x, 0)); D[i * x % d].push_back(LP(i * (2 * n - i - 1) * d / 2 + i * x, 1)); } ll ans = 0; for (auto L : D) { sort(L.second.begin(), L.second.end()); int a = 0; ll fir; for (LP p : L.second) { // cout << p.first <<" " << p.second << endl; if (p.second == 0) { if (a == 0) fir = p.first; a += 1; } else { if (a == 1) ans += (p.first - fir) / d + 1; a -= 1; } // cout << ans << endl; } } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); solve(); }
#include <algorithm> #include <bitset> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define Per(i, sta, n) for (int i = n - 1; i >= sta; i--) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef long double ld; typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; ll n, x, d; unordered_map<ll, vector<LP>> D; void solve() { cin >> n >> x >> d; if (d == 0) { if (x == 0) { cout << 1 << endl; return; } cout << n + 1 << endl; return; } if (d < 0) { x = (n - 1) * d + x; d *= -1; } for (ll i = 0; i <= n; i++) { D[i * x % d].push_back(LP(i * (i - 1) * d / 2 + i * x, 0)); D[i * x % d].push_back(LP(i * (2 * n - i - 1) * d / 2 + i * x, 1)); } ll ans = 0; for (auto L : D) { sort(L.second.begin(), L.second.end()); int a = 0; ll fir; for (LP p : L.second) { // cout << p.first <<" " << p.second << endl; if (p.second == 0) { if (a == 0) fir = p.first; a += 1; } else { if (a == 1) ans += (p.first - fir) / d + 1; a -= 1; } // cout << ans << endl; } } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); solve(); }
insert
46
46
46
54
0
p02840
C++
Runtime Error
#include <algorithm> #include <map> #include <stdio.h> using namespace std; long long N, X, D; int main() { scanf("%lld %lld %lld", &N, &X, &D); map<long long, map<long long, int>> cnt; for (long long i = 0; i <= N; i++) { long long r = i * X % D; long long s = (i * X / D) + i * (i - 1) / 2; long long e = (i * X / D) + (N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2); cnt[r][s]++; cnt[r][e + 1]--; } long long ans = 0; for (auto &v : cnt) { int c = 0; for (auto &p : v.second) if (p.second) { if (c == 0) ans -= p.first; c += p.second; if (c == 0) ans += p.first; } } printf("%lld\n", ans); return 0; } // i -> i * (i - 1) / 2 ~ N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2
#include <algorithm> #include <map> #include <stdio.h> using namespace std; long long N, X, D; int main() { scanf("%lld %lld %lld", &N, &X, &D); if (D == 0) { if (X) printf("%lld\n", N + 1); else puts("1"); return 0; } map<long long, map<long long, int>> cnt; for (long long i = 0; i <= N; i++) { long long r = i * X % D; long long s = (i * X / D) + i * (i - 1) / 2; long long e = (i * X / D) + (N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2); cnt[r][s]++; cnt[r][e + 1]--; } long long ans = 0; for (auto &v : cnt) { int c = 0; for (auto &p : v.second) if (p.second) { if (c == 0) ans -= p.first; c += p.second; if (c == 0) ans += p.first; } } printf("%lld\n", ans); return 0; } // i -> i * (i - 1) / 2 ~ N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2
insert
9
9
9
17
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, int> ii; unordered_map<ll, vector<ii>> m; ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b % a, a); } int main() { ll n, x, d; scanf("%lld%lld%lld", &n, &x, &d); ll l = 0, r = 0; for (int i = 0; i <= n; i++) { ll k = (i * x) / d; ll st = min(i * x + l * d, i * x + r * d); ll en = max(i * x + l * d, i * x + r * d); // printf("group %lld ",k); // printf("with %d elements, can go from %lld to %lld with remainder // %lld\n",i,st,en,(i*x)%d); m[(i * x) % d].push_back(ii(st, -1)); m[(i * x) % d].push_back(ii(en, 1)); l += i; r += (n - i - 1); } int sum = 0; ll last; ll ans = 0; for (auto p : m) { vector<ii> v = p.second; sort(v.begin(), v.end()); for (auto x : v) { // printf("%lld %d\n",x.first,x.second); if (sum == 0 && x.second == -1) { last = x.first; } else if (sum == -1 && x.second == 1) { // printf("with remainder %lld, range (%lld -> // %lld)\n",p.first,last,x.first); ans += (x.first - last) / max(d, -d) + 1; } sum += x.second; } } printf("%lld", ans); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, int> ii; unordered_map<ll, vector<ii>> m; ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b % a, a); } int main() { ll n, x, d; scanf("%lld%lld%lld", &n, &x, &d); ll l = 0, r = 0; if (d == 0) { if (x == 0) { printf("1"); return 0; } printf("%lld", n + 1); return 0; } for (int i = 0; i <= n; i++) { ll k = (i * x) / d; ll st = min(i * x + l * d, i * x + r * d); ll en = max(i * x + l * d, i * x + r * d); // printf("group %lld ",k); // printf("with %d elements, can go from %lld to %lld with remainder // %lld\n",i,st,en,(i*x)%d); m[(i * x) % d].push_back(ii(st, -1)); m[(i * x) % d].push_back(ii(en, 1)); l += i; r += (n - i - 1); } int sum = 0; ll last; ll ans = 0; for (auto p : m) { vector<ii> v = p.second; sort(v.begin(), v.end()); for (auto x : v) { // printf("%lld %d\n",x.first,x.second); if (sum == 0 && x.second == -1) { last = x.first; } else if (sum == -1 && x.second == 1) { // printf("with remainder %lld, range (%lld -> // %lld)\n",p.first,last,x.first); ans += (x.first - last) / max(d, -d) + 1; } sum += x.second; } } printf("%lld", ans); }
insert
10
10
10
18
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> #define long long long int using namespace std; // @author: pashka long prog(long x) { return x * (x - 1) / 2; } int main() { ios::sync_with_stdio(false); int n; long x, d; cin >> n >> x >> d; if (d < 0) { x = -x; d = -d; } map<int, vector<pair<long, long>>> seg; for (int a = -n; a <= n; a++) { if ((a & 1) != (n & 1)) continue; // plus + minus = n // plus - minus = a int plus = (n + a) / 2; long bmin = prog(plus) - (prog(n) - prog(plus)); long bmax = -prog(n - plus) + (prog(n) - prog(n - plus)); long p = a * x + bmin * d; long k = (bmax - bmin) / 2 + 1; int q = p % (2 * d); if (q < 0) q += 2 * d; long l = (p - q) / (2 * d); long r = l + k; // cout << a << " " << bmin << " " << bmax << " " << // p << " " << k << " " << q << " " << l << " " << r << "\n"; seg[q].push_back({l, r}); } long res = 0; for (auto &p : seg) { auto &ss = p.second; sort(ss.begin(), ss.end()); long r = LONG_MIN; for (auto s : ss) { if (s.second <= r) continue; res += s.second - max(r, s.first); r = s.second; } } cout << res << "\n"; return 0; }
#include <bits/stdc++.h> #define long long long int using namespace std; // @author: pashka long prog(long x) { return x * (x - 1) / 2; } int main() { ios::sync_with_stdio(false); int n; long x, d; cin >> n >> x >> d; if (d < 0) { x = -x; d = -d; } if (d == 0) { if (x == 0) { cout << 1; } else { cout << n + 1; } return 0; } map<int, vector<pair<long, long>>> seg; for (int a = -n; a <= n; a++) { if ((a & 1) != (n & 1)) continue; // plus + minus = n // plus - minus = a int plus = (n + a) / 2; long bmin = prog(plus) - (prog(n) - prog(plus)); long bmax = -prog(n - plus) + (prog(n) - prog(n - plus)); long p = a * x + bmin * d; long k = (bmax - bmin) / 2 + 1; int q = p % (2 * d); if (q < 0) q += 2 * d; long l = (p - q) / (2 * d); long r = l + k; // cout << a << " " << bmin << " " << bmax << " " << // p << " " << k << " " << q << " " << l << " " << r << "\n"; seg[q].push_back({l, r}); } long res = 0; for (auto &p : seg) { auto &ss = p.second; sort(ss.begin(), ss.end()); long r = LONG_MIN; for (auto s : ss) { if (s.second <= r) continue; res += s.second - max(r, s.first); r = s.second; } } cout << res << "\n"; return 0; }
insert
17
17
17
26
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define x first #define y second #ifdef LOCAL #include "/Users/swad/Desktop/CP/debug.h" #endif const int N = int(2e5) + 10; ll n, x, d; ll pref[N], suff[N]; map<ll, ll> series_from; int main() { #ifdef LOCAL freopen("in", "r", stdin); freopen("out", "w", stdout); #endif cin >> n >> x >> d; if (d < 0) { x += d * (n - 1); d *= -1; } for (int i = 1; i <= n; i++) { pref[i] = pref[i - 1] + (i - 1); suff[i] = suff[i - 1] + (n - i); } vector<pair<ll, ll>> seriesv(n + 1); for (int i = 0; i <= n; i++) { ll add = x * (i - (n - i)); ll L = d * (pref[i] - suff[n - i]); ll R = d * (suff[i] - pref[n - i]); seriesv[i] = make_pair(add + L, add + R); } sort(seriesv.begin(), seriesv.end()); ll ans = 0LL; d *= 2; for (pair<ll, ll> p : seriesv) { ll l = p.x, r = p.y; ll rem = (l % d + d) % d; ll R = LLONG_MIN; if (series_from.find(rem) != series_from.end()) { R = series_from[rem]; } if (R < r) { l = max(l, R + d); ans += (r - l + d) / d; series_from[rem] = r; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define x first #define y second #ifdef LOCAL #include "/Users/swad/Desktop/CP/debug.h" #endif const int N = int(2e5) + 10; ll n, x, d; ll pref[N], suff[N]; map<ll, ll> series_from; int main() { #ifdef LOCAL freopen("in", "r", stdin); freopen("out", "w", stdout); #endif cin >> n >> x >> d; if (d == 0) { if (x == 0) cout << 1 << endl; else cout << n + 1 << endl; return 0; } if (d < 0) { x += d * (n - 1); d *= -1; } for (int i = 1; i <= n; i++) { pref[i] = pref[i - 1] + (i - 1); suff[i] = suff[i - 1] + (n - i); } vector<pair<ll, ll>> seriesv(n + 1); for (int i = 0; i <= n; i++) { ll add = x * (i - (n - i)); ll L = d * (pref[i] - suff[n - i]); ll R = d * (suff[i] - pref[n - i]); seriesv[i] = make_pair(add + L, add + R); } sort(seriesv.begin(), seriesv.end()); ll ans = 0LL; d *= 2; for (pair<ll, ll> p : seriesv) { ll l = p.x, r = p.y; ll rem = (l % d + d) % d; ll R = LLONG_MIN; if (series_from.find(rem) != series_from.end()) { R = series_from[rem]; } if (R < r) { l = max(l, R + d); ans += (r - l + d) / d; series_from[rem] = r; } } cout << ans << endl; return 0; }
insert
26
26
26
34
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> #define MAX_N 200000 using namespace std; typedef long long lint; int n; struct elem { lint r; lint st, dr; }; int m; elem v[MAX_N + 1]; lint x, d; struct cmp { bool operator()(const elem &a, const elem &b) const { if (a.r != b.r) return a.r < b.r; return a.st < b.st; } }; lint rez; int main() { cin >> n >> x >> d; /* if(d < 0) { x = -x; d = -d; }*/ if (d == 0) { rez = (x == 0 ? 1 : n + 1); cout << rez << "\n"; return 0; } v[++m] = {0, 0, 0}; for (int k = 1; k <= n; k++) { v[++m] = {1LL * k * x % d, 1LL * k * x / d + ((1LL * (k - 1) * k) >> 1), 1LL * k * x / d + 1LL * n * k - (((1LL * k) * (k + 1)) >> 1)}; } sort(v + 1, v + m + 1, cmp()); int ant = -1; lint st = 0, dr = -1; for (int i = 1; i <= m; i++) { // cout << v[i].r << " " << v[i].st << " " << v[i].dr << "\n"; if (ant != v[i].r) { rez += (dr - st) + 1; ant = v[i].r; st = v[i].st; dr = v[i].dr; } else { if (v[i].st <= dr) dr = max(dr, v[i].dr); else { rez += (dr - st) + 1; st = v[i].st; dr = v[i].dr; } } } rez += (dr - st) + 1; cout << rez << "\n"; return 0; }
#include <bits/stdc++.h> #define MAX_N 200000 using namespace std; typedef long long lint; int n; struct elem { lint r; lint st, dr; }; int m; elem v[MAX_N + 1 + 1]; lint x, d; struct cmp { bool operator()(const elem &a, const elem &b) const { if (a.r != b.r) return a.r < b.r; return a.st < b.st; } }; lint rez; int main() { cin >> n >> x >> d; /* if(d < 0) { x = -x; d = -d; }*/ if (d == 0) { rez = (x == 0 ? 1 : n + 1); cout << rez << "\n"; return 0; } v[++m] = {0, 0, 0}; for (int k = 1; k <= n; k++) { v[++m] = {1LL * k * x % d, 1LL * k * x / d + ((1LL * (k - 1) * k) >> 1), 1LL * k * x / d + 1LL * n * k - (((1LL * k) * (k + 1)) >> 1)}; } sort(v + 1, v + m + 1, cmp()); int ant = -1; lint st = 0, dr = -1; for (int i = 1; i <= m; i++) { // cout << v[i].r << " " << v[i].st << " " << v[i].dr << "\n"; if (ant != v[i].r) { rez += (dr - st) + 1; ant = v[i].r; st = v[i].st; dr = v[i].dr; } else { if (v[i].st <= dr) dr = max(dr, v[i].dr); else { rez += (dr - st) + 1; st = v[i].st; dr = v[i].dr; } } } rez += (dr - st) + 1; cout << rez << "\n"; return 0; }
replace
16
17
16
17
0
p02840
C++
Runtime Error
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #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 repeq(i, n) for (int i = 0; i <= (int)(n); ++i) #define rep1eq(i, n) for (int i = 1; i <= (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define rrep1(i, n) for (int i = (int)(n)-1; i > 0; --i) #define rrepeq(i, n) for (int i = (int)(n); i >= 0; --i) #define rrep1eq(i, n) for (int i = (int)(n); i > 0; --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; template <typename T> using Graph = vector<vector<T>>; template <typename T> using Spacial = vector<vector<vector<T>>>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; const int MOD2 = 998244353; const double EPS = 1e-9; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; template <typename T> struct is_pair : false_type {}; template <typename T1, typename T2> struct is_pair<pair<T1, T2>> : true_type {}; template <typename T> struct is_vector : false_type {}; template <typename T> struct is_vector<vector<T>> : true_type {}; template <typename T1, typename T2> istream &operator>>(istream &istr, pair<T1, T2> &p) { istr >> p.first >> p.second; return istr; } template <typename T1, typename T2> ostream &operator<<(ostream &ostr, const pair<T1, T2> &p) { ostr << p.first << " " << p.second; return ostr; } template <typename T> istream &operator>>(istream &istr, vector<T> &vec) { for (auto itr = vec.begin(); itr != vec.end(); ++itr) { istr >> *itr; } return istr; } template <typename T> ostream &operator<<(ostream &ostr, const vector<T> &vec) { if (vec.empty()) return ostr; bool vp = is_vector<T>() || is_pair<T>(); ostr << vec.front(); for (auto itr = ++vec.begin(); itr != vec.end(); ++itr) { ostr << (vp ? "\n" : " ") << *itr; } return ostr; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } int modpow(int a, long long n, int mod = MOD) { int ret = 1; do { if (n & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; } while (n >>= 1); return ret; } template <typename T> T GCD(T a, T b) { return b ? GCD(b, a % b) : a; } template <typename T> T LCM(T a, T b) { return a / GCD(a, b) * b; } template <typename T1, typename T2> bool CompareBySecond(pair<T1, T2> a, pair<T1, T2> b) { return a.second != b.second ? a.second < b.second : a.first < b.first; } template <typename T> bool CompareBySlope(pair<T, T> a, pair<T, T> b) { // counterclockwise from 12 o'clock direction if (a.first * b.first < 0) return a.first < b.first; if (a.first == 0) return a.second >= 0 || b.first > 0; if (b.first == 0) return b.second < 0 && a.first < 0; return a.second * b.first < a.first * b.second; } /* -------- <templates end> -------- */ void solve() { ll n, x, d; cin >> n >> x >> d; if (d == 0 && x == 0) { cout << 1 << endl; return; } if (d == 0) { cout << n + 1 << endl; return; } if (x == 0) { cout << n * (n - 1) / 2 + 1 << endl; return; } if (d < 0) x = -x, d = -d; ll cycle = d / GCD(abs(x), d); int diff = (x % d) * cycle / d; int sz = min(n, cycle); Graph<pll> dp(sz); repeq(i, n) { ll left = (ll)i * (i - 1) / 2; ll right = (ll)i * (n - i + n - 1) / 2 + 1; ll add = i / cycle * diff + x / d * i; dp[i % cycle].push_back(pll(left + add, right + add)); } ll ans = 0; rep(i, sz) { sort(ALL(dp[i])); ll cnt = 0; ll left, right; tie(left, right) = dp[i][0]; for (auto p = ++dp[i].begin(); p != dp[i].end(); ++p) { if (right < p->first) { cnt += right - left; tie(left, right) = *p; } else { chmax(right, p->second); } } cnt += right - left; ans += cnt; } cout << ans << endl; } /* -------- <programs end> -------- */ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(12); solve(); return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #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 repeq(i, n) for (int i = 0; i <= (int)(n); ++i) #define rep1eq(i, n) for (int i = 1; i <= (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define rrep1(i, n) for (int i = (int)(n)-1; i > 0; --i) #define rrepeq(i, n) for (int i = (int)(n); i >= 0; --i) #define rrep1eq(i, n) for (int i = (int)(n); i > 0; --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; template <typename T> using Graph = vector<vector<T>>; template <typename T> using Spacial = vector<vector<vector<T>>>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; const int MOD2 = 998244353; const double EPS = 1e-9; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; template <typename T> struct is_pair : false_type {}; template <typename T1, typename T2> struct is_pair<pair<T1, T2>> : true_type {}; template <typename T> struct is_vector : false_type {}; template <typename T> struct is_vector<vector<T>> : true_type {}; template <typename T1, typename T2> istream &operator>>(istream &istr, pair<T1, T2> &p) { istr >> p.first >> p.second; return istr; } template <typename T1, typename T2> ostream &operator<<(ostream &ostr, const pair<T1, T2> &p) { ostr << p.first << " " << p.second; return ostr; } template <typename T> istream &operator>>(istream &istr, vector<T> &vec) { for (auto itr = vec.begin(); itr != vec.end(); ++itr) { istr >> *itr; } return istr; } template <typename T> ostream &operator<<(ostream &ostr, const vector<T> &vec) { if (vec.empty()) return ostr; bool vp = is_vector<T>() || is_pair<T>(); ostr << vec.front(); for (auto itr = ++vec.begin(); itr != vec.end(); ++itr) { ostr << (vp ? "\n" : " ") << *itr; } return ostr; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } int modpow(int a, long long n, int mod = MOD) { int ret = 1; do { if (n & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; } while (n >>= 1); return ret; } template <typename T> T GCD(T a, T b) { return b ? GCD(b, a % b) : a; } template <typename T> T LCM(T a, T b) { return a / GCD(a, b) * b; } template <typename T1, typename T2> bool CompareBySecond(pair<T1, T2> a, pair<T1, T2> b) { return a.second != b.second ? a.second < b.second : a.first < b.first; } template <typename T> bool CompareBySlope(pair<T, T> a, pair<T, T> b) { // counterclockwise from 12 o'clock direction if (a.first * b.first < 0) return a.first < b.first; if (a.first == 0) return a.second >= 0 || b.first > 0; if (b.first == 0) return b.second < 0 && a.first < 0; return a.second * b.first < a.first * b.second; } /* -------- <templates end> -------- */ void solve() { ll n, x, d; cin >> n >> x >> d; if (d == 0 && x == 0) { cout << 1 << endl; return; } if (d == 0) { cout << n + 1 << endl; return; } if (x == 0) { cout << n * (n - 1) / 2 + 1 << endl; return; } if (d < 0) x = -x, d = -d; ll cycle = d / GCD(abs(x), d); int diff = (x % d) * cycle / d; int sz = min(n + 1, cycle); Graph<pll> dp(sz); repeq(i, n) { ll left = (ll)i * (i - 1) / 2; ll right = (ll)i * (n - i + n - 1) / 2 + 1; ll add = i / cycle * diff + x / d * i; dp[i % cycle].push_back(pll(left + add, right + add)); } ll ans = 0; rep(i, sz) { sort(ALL(dp[i])); ll cnt = 0; ll left, right; tie(left, right) = dp[i][0]; for (auto p = ++dp[i].begin(); p != dp[i].end(); ++p) { if (right < p->first) { cnt += right - left; tie(left, right) = *p; } else { chmax(right, p->second); } } cnt += right - left; ans += cnt; } cout << ans << endl; } /* -------- <programs end> -------- */ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(12); solve(); return 0; }
replace
120
121
120
121
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using lli = long long int; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); lli X, D; lli n, i, j, k; cin >> n >> X >> D; if (D < 0) { X = X + (n - 1) * D; D = -D; } /* cout << "n = " << n << " X = " << X << " D = " << D << endl; for(i = 0; i < n; ++i) { cout << X + D * i << " "; } cout << endl; */ vector<pair<lli, lli>> seg; lli max_s, min_s; lli S, l, r; S = ((n - 1) * n) / 2; lli step = 2 * D; vector<tuple<lli, int, lli>> points; for (k = 0; k <= n; ++k) { min_s = (k * (k - 1)) / 2; max_s = n * k - ((k + 1) * k) / 2; l = D * (2 * min_s - S) + X * (2 * k - n); r = D * (2 * max_s - S) + X * (2 * k - n); // cout << "L = " << l << " R = " << r << endl; seg.push_back({l, r}); points.emplace_back(l, 0, k); points.emplace_back(r, 1, k); } sort(points.begin(), points.end()); map<lli, int> rest; map<lli, lli> left; lli x, s, rst, ans; ans = 0; lli t; for (i = 0; i < points.size(); ++i) { tie(x, t, s) = points[i]; if (t == 1) // right { l = seg[s].first; r = x; rst = ((l % step) + step) % step; rest[rst] -= 1; if (rest[rst] == 0) { ans += (r - left[rst]) / step + 1; rest.erase(rst); } } else { l = x; r = seg[s].second; rst = ((l % step) + step) % step; if (rest.count(rst) == 0) { rest[rst] = 1; left[rst] = l; } else { rest[rst] += 1; } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; using lli = long long int; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); lli X, D; lli n, i, j, k; cin >> n >> X >> D; if (D < 0) { X = X + (n - 1) * D; D = -D; } if (D == 0) { if (X == 0) { cout << 1; } else { cout << n + 1; } return 0; } /* cout << "n = " << n << " X = " << X << " D = " << D << endl; for(i = 0; i < n; ++i) { cout << X + D * i << " "; } cout << endl; */ vector<pair<lli, lli>> seg; lli max_s, min_s; lli S, l, r; S = ((n - 1) * n) / 2; lli step = 2 * D; vector<tuple<lli, int, lli>> points; for (k = 0; k <= n; ++k) { min_s = (k * (k - 1)) / 2; max_s = n * k - ((k + 1) * k) / 2; l = D * (2 * min_s - S) + X * (2 * k - n); r = D * (2 * max_s - S) + X * (2 * k - n); // cout << "L = " << l << " R = " << r << endl; seg.push_back({l, r}); points.emplace_back(l, 0, k); points.emplace_back(r, 1, k); } sort(points.begin(), points.end()); map<lli, int> rest; map<lli, lli> left; lli x, s, rst, ans; ans = 0; lli t; for (i = 0; i < points.size(); ++i) { tie(x, t, s) = points[i]; if (t == 1) // right { l = seg[s].first; r = x; rst = ((l % step) + step) % step; rest[rst] -= 1; if (rest[rst] == 0) { ans += (r - left[rst]) / step + 1; rest.erase(rst); } } else { l = x; r = seg[s].second; rst = ((l % step) + step) % step; if (rest.count(rst) == 0) { rest[rst] = 1; left[rst] = l; } else { rest[rst] += 1; } } } cout << ans; return 0; }
insert
19
19
19
28
0
p02840
C++
Runtime Error
#line 1 "base.hpp" // 6 #ifndef __clang__ #pragma GCC optimize("O3") #endif void solve( #ifdef GCJ_CASE long long case_id #endif ); #if defined(EBUG) && !defined(ONLINE_JUDGE) #define debug true #define _GLIBCXX_DEBUG // #define _LIBCPP_DEBUG 0 #define _LIBCPP_DEBUG2 0 #else #define NDEBUG #define debug false #endif #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <type_traits> #include <vector> using namespace std; using LL = long long; using ULL = unsigned long long; #define int LL #define CS const #define CX constexpr #define IL inline #define OP operator #define RT return #define TL template #define TN typename #define lambda [&] #define rep(f, t, i, c, u) for (int Rb_ = (t), i = (f); i c Rb_; u(i)) #define upto(f, t, i) rep(f, t, i, <=, ++) #define uptil(f, t, i) rep(f, t, i, <, ++) #define downto(f, t, i) rep(f, t, i, >=, --) #define downtil(f, t, i) rep(f, t, i, >, --) #define times(n, i) uptil(0, n, i) #define rtimes(n, i) downto((n)-1, 0, i) #define iter(v) begin(v), end(v) #define citer(v) cbegin(v), cend(v) #define riter(v) rbegin(v), rend(v) #define criter(v) crbegin(v), crend(v) #define IF(a, b, c) ((a) ? (b) : (c)) #if debug #define ln << endl #else #define ln << '\n' #endif #define tb << '\t' #define sp << ' ' #line 1 "base_util.hpp" // 6b #define BINOPa(t, u, op) \ t OP op(CS u &o) CS { RT t(*this) op## = o; } #define CMPOP(t, op, f1, f2, x) \ bool OP op(CS t &x) CS { RT f1 op f2; } #define CMPOPS(t, f1, f2, x) \ CMPOP(t, ==, f1, f2, x) \ CMPOP(t, !=, f1, f2, x) CMPOP(t, <, f1, f2, x) CMPOP(t, <=, f1, f2, x) \ CMPOP(t, >, f1, f2, x) CMPOP(t, >=, f1, f2, x) #line 1 "mod.hpp" // 6b #ifndef MOD #define MOD 1000000007 #endif #if !defined(FORCE_MOD) && MOD != 1000000007 && MOD != 1000000009 && \ MOD != 998244353 #error mod #endif #line 1 "power.hpp" // 6bm TL<TN T> IL T power(T x, int n) { T r(1); for (; n; n /= 2) { if (n % 2) r *= x; x *= x; } RT r; } IL int pow_mod(int x, int n, int m) { int r = 1; for (; n; n /= 2) { if (n % 2) r = r * x % m; x = x * x % m; } RT r; } #line 2001 "mod.hpp" // 6b IL CX int modulo(int a, int b) { a %= b; RT a && (a > 0) != (b > 0) ? a + b : a; } IL CX int divide(int a, int b) { RT(a - modulo(a, b)) / b; } TL<int d = MOD> struct MInt { /*!https://ei1333.github.io/luzhiled/snippets/other/mod-int.html*/ int v; CX MInt() : v(0) {} explicit CX MInt(int i) : v(modulo(i, d)) {} MInt &OP += (CS MInt & m) { v += m.v; if (v >= d) v -= d; RT *this; } MInt &OP -= (CS MInt & m) { v -= m.v; if (v < 0) v += d; RT *this; } MInt &OP *= (CS MInt & m) { v = v * m.v % d; RT *this; } MInt &OP /= (CS MInt & m) { RT *this *= m.inv(); } BINOPa(MInt, MInt, +) BINOPa(MInt, MInt, -) BINOPa(MInt, MInt, *) BINOPa(MInt, MInt, /) MInt OP - () CS { RT MInt() -= *this; } CMPOP(MInt, ==, v, m.v, m) CMPOP(MInt, !=, v, m.v, m) MInt pow(int n) CS { RT power(*this, n); } MInt inv() CS { int a = v, b = d, x = 1, y = 0; while (b) { swap(y, x -= a / b * y); swap(b, a %= b); } RT(MInt) x; } friend ostream &OP << (ostream & o, CS MInt &m) { RT o << m.v; } friend istream &OP >> (istream & i, MInt &m) { i >> m.v; m.v %= d; RT i; } }; using mint = MInt<>; CX mint OP "" _m(ULL n) { RT mint(n); } CX MInt<998244353> OP "" _m998244353(ULL n) { RT MInt<998244353>(n); } CX MInt<1000000007> OP "" _m1e9_7(ULL n) { RT MInt<1000000007>(n); } CX MInt<1000000009> OP "" _m1e9_9(ULL n) { RT MInt<1000000009>(n); } #line 1 "typedefs.hpp" // 6b using unit = tuple<>; using LD = long double; TL<TN T> using vec = vector<T>; TL<TN T> using vvec = vec<vec<T>>; TL<TN T> using vvvec = vec<vvec<T>>; TL<TN T> using vvvvec = vec<vvvec<T>>; using PII = pair<int, int>; #line 1 "alias.hpp" // 6b #define EB emplace_back #define PB push_back #define foldl accumulate #define scanl partial_sum #line 1 "util.hpp" // 6b TL<TN T> IL bool amax(T &v, CS T &a) { RT v < a && (v = a, 1); } TL<TN T> IL bool amin(T &v, CS T &a) { RT v > a && (v = a, 1); } TL<TN T> IL int sizeRAB(T t) { RT t.size(); } #define size sizeRAB #define clamp clampRAB TL<TN T> IL CX CS T &clamp(CS T &a, CS T &l, CS T &r) { RT a < l ? l : r < a ? r : a; } TL<TN V> IL void uniq2(V &v) { v.erase(unique(iter(v)), v.end()); } TL<TN V> IL void uniq(V &v) { sort(iter(v)); uniq2(v); } #define leftmost_ge lower_bound #define leftmost_gt upper_bound TL<TN C, TN D> IL C rightmost_le(CS C &from, CS C &to, CS D &d) { auto l = leftmost_gt(from, to, d); RT l == from ? to : --l; } TL<TN C, TN D> IL C rightmost_lt(CS C &from, CS C &to, CS D &d) { auto l = leftmost_ge(from, to, d); RT l == from ? to : --l; } namespace rab { TL<TN I> IL bool is_in(I x, I l, I r) { RT l <= x &&x < r; } TL<TN T> IL T fetch(CS T &d, CS vec<T> &v, int i) { RT 0 <= i &&i < size(v) ? v[i] : d; } } // namespace rab #line 1 "debug.hpp" // 6b TL<TN T> IL istream &OP >> (istream & s, vec<T> &v) { for (auto &&p : v) s >> p; RT s; } TL<TN T, TN S> IL ostream &OP << (ostream & s, CS pair<T, S> &p) { RT s << "(" << p.first << "," << p.second << ")"; } #define Rdebug1(sep, ...) \ IL ostream &OP << (ostream & s, CS __VA_ARGS__ & v) { \ int i = 0; \ for (CS auto &e : v) { \ i++ &&s << sep; \ s << e; \ } \ RT s; \ } TL<TN T> Rdebug1(' ', vec<T>) TL<TN T, TN S> Rdebug1(' ', map<T, S>) TL<TN T> Rdebug1('\n', vvec<T>) TL<TN T, TN S> Rdebug1('\n', vec<map<T, S>>) #line 6001 "base.hpp" // 6 signed main() { if (debug) cerr << "MOD: " << MOD ln; else cin.tie(0), cerr.tie(0), ios::sync_with_stdio(0); auto p = setprecision(20); cout << fixed << p; cerr << fixed << p; #ifdef GCJ_CASE int T; cin >> T; times(T, t) { cout << "Case #" << t + 1 << ": "; solve(t); } #else solve(); #endif RT 0; } #line 1001 "6.cpp" // // #include "consts.hpp" struct Ranges { set<PII> ranges; // l,r; r,l void add(int a, int b) { /* [a, b) */ if (debug) { { if (debug) cerr << '#' << __LINE__ ln << " a: " << (a)ln << " b: " << (b)ln; } for (auto p : ranges) cerr << p; cerr ln; } { auto it = leftmost_ge(iter(ranges), PII(a, LLONG_MIN)); auto iu = it; while (iu != ranges.end() && iu->second <= b) ++iu; ranges.erase(it, iu); } { auto x = rightmost_lt(iter(ranges), PII(a, LLONG_MIN)); if (x != ranges.end()) { { if (debug) cerr << "*x: " << (*x)ln; } int l1 = x->first, r1 = x->second; if (r1 >= a) { ranges.erase(x); a = l1; if (r1 >= b) b = r1; } } } { auto y = rightmost_le(iter(ranges), PII(b, LLONG_MIN)); if (y != ranges.begin()) { { if (debug) cerr << "*y: " << (*y)ln; } int l2 = y->first, r2 = y->second; if (r2 > b) { ranges.erase(y); b = r2; if (l2 <= a) a = l2; } } } ranges.insert(PII(a, b)); } }; void solve() { int N, X, D; cin >> N >> X >> D; if (D < 0) { D = -D; X -= (N - 1) * D; } else if (D == 0) { cout << (X == 0 ? 1 : N + 1) ln; return; } map<int, Ranges> m; upto(0, N, k) { int xx = (2 * k - N) * X; m[modulo(xx, 2 * D)].add( divide(xx, D) + /* D * */ (2 * k * (k - 1) - N * (N - 1)) / 2, divide(xx, D) + /* D * */ (N * (N - 1) - 2 * (N - k) * (N - k - 1)) / 2 + (2 /* * D */)); } int ans = 0; for (auto &a : m) { for (auto &range : a.second.ranges) { { if (debug) cerr << '#' << __LINE__ ln << " a.first: " << (a.first)ln << " range: " << (range)ln; } int l = range.first, r = range.second; assert((r - l) % (2 /* * D */) == 0); ans += (r - l) / (2 /* * D */); { if (debug) cerr << '#' << __LINE__ ln << " a.first: " << (a.first)ln << " l: " << (l)ln << " r: " << (r)ln << " (r - l) / (2 /* * D */): " << ((r - l) / (2 /* * D */)) ln; } } } cout << ans ln; }
#line 1 "base.hpp" // 6 #ifndef __clang__ #pragma GCC optimize("O3") #endif void solve( #ifdef GCJ_CASE long long case_id #endif ); #if defined(EBUG) && !defined(ONLINE_JUDGE) #define debug true #define _GLIBCXX_DEBUG // #define _LIBCPP_DEBUG 0 #define _LIBCPP_DEBUG2 0 #else #define NDEBUG #define debug false #endif #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <type_traits> #include <vector> using namespace std; using LL = long long; using ULL = unsigned long long; #define int LL #define CS const #define CX constexpr #define IL inline #define OP operator #define RT return #define TL template #define TN typename #define lambda [&] #define rep(f, t, i, c, u) for (int Rb_ = (t), i = (f); i c Rb_; u(i)) #define upto(f, t, i) rep(f, t, i, <=, ++) #define uptil(f, t, i) rep(f, t, i, <, ++) #define downto(f, t, i) rep(f, t, i, >=, --) #define downtil(f, t, i) rep(f, t, i, >, --) #define times(n, i) uptil(0, n, i) #define rtimes(n, i) downto((n)-1, 0, i) #define iter(v) begin(v), end(v) #define citer(v) cbegin(v), cend(v) #define riter(v) rbegin(v), rend(v) #define criter(v) crbegin(v), crend(v) #define IF(a, b, c) ((a) ? (b) : (c)) #if debug #define ln << endl #else #define ln << '\n' #endif #define tb << '\t' #define sp << ' ' #line 1 "base_util.hpp" // 6b #define BINOPa(t, u, op) \ t OP op(CS u &o) CS { RT t(*this) op## = o; } #define CMPOP(t, op, f1, f2, x) \ bool OP op(CS t &x) CS { RT f1 op f2; } #define CMPOPS(t, f1, f2, x) \ CMPOP(t, ==, f1, f2, x) \ CMPOP(t, !=, f1, f2, x) CMPOP(t, <, f1, f2, x) CMPOP(t, <=, f1, f2, x) \ CMPOP(t, >, f1, f2, x) CMPOP(t, >=, f1, f2, x) #line 1 "mod.hpp" // 6b #ifndef MOD #define MOD 1000000007 #endif #if !defined(FORCE_MOD) && MOD != 1000000007 && MOD != 1000000009 && \ MOD != 998244353 #error mod #endif #line 1 "power.hpp" // 6bm TL<TN T> IL T power(T x, int n) { T r(1); for (; n; n /= 2) { if (n % 2) r *= x; x *= x; } RT r; } IL int pow_mod(int x, int n, int m) { int r = 1; for (; n; n /= 2) { if (n % 2) r = r * x % m; x = x * x % m; } RT r; } #line 2001 "mod.hpp" // 6b IL CX int modulo(int a, int b) { a %= b; RT a && (a > 0) != (b > 0) ? a + b : a; } IL CX int divide(int a, int b) { RT(a - modulo(a, b)) / b; } TL<int d = MOD> struct MInt { /*!https://ei1333.github.io/luzhiled/snippets/other/mod-int.html*/ int v; CX MInt() : v(0) {} explicit CX MInt(int i) : v(modulo(i, d)) {} MInt &OP += (CS MInt & m) { v += m.v; if (v >= d) v -= d; RT *this; } MInt &OP -= (CS MInt & m) { v -= m.v; if (v < 0) v += d; RT *this; } MInt &OP *= (CS MInt & m) { v = v * m.v % d; RT *this; } MInt &OP /= (CS MInt & m) { RT *this *= m.inv(); } BINOPa(MInt, MInt, +) BINOPa(MInt, MInt, -) BINOPa(MInt, MInt, *) BINOPa(MInt, MInt, /) MInt OP - () CS { RT MInt() -= *this; } CMPOP(MInt, ==, v, m.v, m) CMPOP(MInt, !=, v, m.v, m) MInt pow(int n) CS { RT power(*this, n); } MInt inv() CS { int a = v, b = d, x = 1, y = 0; while (b) { swap(y, x -= a / b * y); swap(b, a %= b); } RT(MInt) x; } friend ostream &OP << (ostream & o, CS MInt &m) { RT o << m.v; } friend istream &OP >> (istream & i, MInt &m) { i >> m.v; m.v %= d; RT i; } }; using mint = MInt<>; CX mint OP "" _m(ULL n) { RT mint(n); } CX MInt<998244353> OP "" _m998244353(ULL n) { RT MInt<998244353>(n); } CX MInt<1000000007> OP "" _m1e9_7(ULL n) { RT MInt<1000000007>(n); } CX MInt<1000000009> OP "" _m1e9_9(ULL n) { RT MInt<1000000009>(n); } #line 1 "typedefs.hpp" // 6b using unit = tuple<>; using LD = long double; TL<TN T> using vec = vector<T>; TL<TN T> using vvec = vec<vec<T>>; TL<TN T> using vvvec = vec<vvec<T>>; TL<TN T> using vvvvec = vec<vvvec<T>>; using PII = pair<int, int>; #line 1 "alias.hpp" // 6b #define EB emplace_back #define PB push_back #define foldl accumulate #define scanl partial_sum #line 1 "util.hpp" // 6b TL<TN T> IL bool amax(T &v, CS T &a) { RT v < a && (v = a, 1); } TL<TN T> IL bool amin(T &v, CS T &a) { RT v > a && (v = a, 1); } TL<TN T> IL int sizeRAB(T t) { RT t.size(); } #define size sizeRAB #define clamp clampRAB TL<TN T> IL CX CS T &clamp(CS T &a, CS T &l, CS T &r) { RT a < l ? l : r < a ? r : a; } TL<TN V> IL void uniq2(V &v) { v.erase(unique(iter(v)), v.end()); } TL<TN V> IL void uniq(V &v) { sort(iter(v)); uniq2(v); } #define leftmost_ge lower_bound #define leftmost_gt upper_bound TL<TN C, TN D> IL C rightmost_le(CS C &from, CS C &to, CS D &d) { auto l = leftmost_gt(from, to, d); RT l == from ? to : --l; } TL<TN C, TN D> IL C rightmost_lt(CS C &from, CS C &to, CS D &d) { auto l = leftmost_ge(from, to, d); RT l == from ? to : --l; } namespace rab { TL<TN I> IL bool is_in(I x, I l, I r) { RT l <= x &&x < r; } TL<TN T> IL T fetch(CS T &d, CS vec<T> &v, int i) { RT 0 <= i &&i < size(v) ? v[i] : d; } } // namespace rab #line 1 "debug.hpp" // 6b TL<TN T> IL istream &OP >> (istream & s, vec<T> &v) { for (auto &&p : v) s >> p; RT s; } TL<TN T, TN S> IL ostream &OP << (ostream & s, CS pair<T, S> &p) { RT s << "(" << p.first << "," << p.second << ")"; } #define Rdebug1(sep, ...) \ IL ostream &OP << (ostream & s, CS __VA_ARGS__ & v) { \ int i = 0; \ for (CS auto &e : v) { \ i++ &&s << sep; \ s << e; \ } \ RT s; \ } TL<TN T> Rdebug1(' ', vec<T>) TL<TN T, TN S> Rdebug1(' ', map<T, S>) TL<TN T> Rdebug1('\n', vvec<T>) TL<TN T, TN S> Rdebug1('\n', vec<map<T, S>>) #line 6001 "base.hpp" // 6 signed main() { if (debug) cerr << "MOD: " << MOD ln; else cin.tie(0), cerr.tie(0), ios::sync_with_stdio(0); auto p = setprecision(20); cout << fixed << p; cerr << fixed << p; #ifdef GCJ_CASE int T; cin >> T; times(T, t) { cout << "Case #" << t + 1 << ": "; solve(t); } #else solve(); #endif RT 0; } #line 1001 "6.cpp" // // #include "consts.hpp" struct Ranges { set<PII> ranges; // l,r; r,l void add(int a, int b) { /* [a, b) */ if (debug) { { if (debug) cerr << '#' << __LINE__ ln << " a: " << (a)ln << " b: " << (b)ln; } for (auto p : ranges) cerr << p; cerr ln; } { auto it = leftmost_ge(iter(ranges), PII(a, LLONG_MIN)); auto iu = it; while (iu != ranges.end() && iu->second <= b) ++iu; ranges.erase(it, iu); } { auto x = rightmost_lt(iter(ranges), PII(a, LLONG_MIN)); if (x != ranges.end()) { { if (debug) cerr << "*x: " << (*x)ln; } int l1 = x->first, r1 = x->second; if (r1 >= a) { ranges.erase(x); a = l1; if (r1 >= b) b = r1; } } } { auto y = rightmost_le(iter(ranges), PII(b, LLONG_MIN)); if (y != ranges.end()) { { if (debug) cerr << "*y: " << (*y)ln; } int l2 = y->first, r2 = y->second; if (r2 > b) { ranges.erase(y); b = r2; if (l2 <= a) a = l2; } } } ranges.insert(PII(a, b)); } }; void solve() { int N, X, D; cin >> N >> X >> D; if (D < 0) { D = -D; X -= (N - 1) * D; } else if (D == 0) { cout << (X == 0 ? 1 : N + 1) ln; return; } map<int, Ranges> m; upto(0, N, k) { int xx = (2 * k - N) * X; m[modulo(xx, 2 * D)].add( divide(xx, D) + /* D * */ (2 * k * (k - 1) - N * (N - 1)) / 2, divide(xx, D) + /* D * */ (N * (N - 1) - 2 * (N - k) * (N - k - 1)) / 2 + (2 /* * D */)); } int ans = 0; for (auto &a : m) { for (auto &range : a.second.ranges) { { if (debug) cerr << '#' << __LINE__ ln << " a.first: " << (a.first)ln << " range: " << (range)ln; } int l = range.first, r = range.second; assert((r - l) % (2 /* * D */) == 0); ans += (r - l) / (2 /* * D */); { if (debug) cerr << '#' << __LINE__ ln << " a.first: " << (a.first)ln << " l: " << (l)ln << " r: " << (r)ln << " (r - l) / (2 /* * D */): " << ((r - l) / (2 /* * D */)) ln; } } } cout << ans ln; }
replace
284
285
284
285
0
p02840
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <vector> using namespace std; int main() { const long long L = 100000000LL * 200000 + 199999LL * 100000LL * 100000000LL; int N; long long X, sD; while (cin >> N >> X >> sD) { map<int, vector<pair<long long, long long>>> mp; long long D = abs(sD); mp[L % D] = vector<pair<long long, long long>>(); mp[L % D].emplace_back(L / D, L / D); for (long long i = 1; i <= N; i++) { long long l = (i * X + (i - 1) * i / 2 * sD + L); long long r = (i * X + (2 * N - 1 - i) * i / 2 * sD + L); if (l > r) swap(l, r); int key = l % D; if (!mp.count(key)) { mp[key] = vector<pair<long long, long long>>(); } mp[key].emplace_back(l / D, r / D); } long long res = 0; for (auto &val : mp) { auto &v = val.second; sort(v.begin(), v.end()); auto end = v[0].first - 1; for (auto &p : v) { res += max(0LL, p.second - max(end, p.first) + 1); end = max(end, p.second + 1); } } cout << res << endl; } }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <vector> using namespace std; int main() { const long long L = 100000000LL * 200000 + 199999LL * 100000LL * 100000000LL; int N; long long X, sD; while (cin >> N >> X >> sD) { map<int, vector<pair<long long, long long>>> mp; long long D = abs(sD); if (D == 0) { cout << (X == 0 ? 1 : N + 1) << endl; continue; } mp[L % D] = vector<pair<long long, long long>>(); mp[L % D].emplace_back(L / D, L / D); for (long long i = 1; i <= N; i++) { long long l = (i * X + (i - 1) * i / 2 * sD + L); long long r = (i * X + (2 * N - 1 - i) * i / 2 * sD + L); if (l > r) swap(l, r); int key = l % D; if (!mp.count(key)) { mp[key] = vector<pair<long long, long long>>(); } mp[key].emplace_back(l / D, r / D); } long long res = 0; for (auto &val : mp) { auto &v = val.second; sort(v.begin(), v.end()); auto end = v[0].first - 1; for (auto &p : v) { res += max(0LL, p.second - max(end, p.first) + 1); end = max(end, p.second + 1); } } cout << res << endl; } }
insert
15
15
15
19
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for (int64 i = 0; i < (n); i++) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } 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...)); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } template <::std::uint_fast64_t mod> class ModInt { private: using value_type = ::std::uint_fast64_t; value_type n; public: ModInt() : n(0) {} ModInt(value_type n_) : n(n_ % mod) {} ModInt(const ModInt &m) : n(m.n) {} template <typename T> explicit operator T() const { return static_cast<T>(n); } value_type get() const { return n; } friend ::std::ostream &operator<<(::std::ostream &os, const ModInt<mod> &a) { return os << a.n; } friend ::std::istream &operator>>(::std::istream &is, ModInt<mod> &a) { value_type x; is >> x; a = ModInt<mod>(x); return is; } bool operator==(const ModInt &m) const { return n == m.n; } bool operator!=(const ModInt &m) const { return n != m.n; } ModInt &operator*=(const ModInt &m) { n = n * m.n % mod; return *this; } ModInt pow(value_type b) const { ModInt ans = 1, m = ModInt(*this); while (b) { if (b & 1) ans *= m; m *= m; b >>= 1; } return ans; } ModInt inv() const { return (*this).pow(mod - 2); } ModInt &operator+=(const ModInt &m) { n += m.n; n = (n < mod ? n : n - mod); return *this; } ModInt &operator-=(const ModInt &m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; } ModInt &operator/=(const ModInt &m) { *this *= m.inv(); return *this; } ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; } ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; } ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; } ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; } ModInt &operator++() { n += 1; return *this; } ModInt &operator--() { n -= 1; return *this; } ModInt operator++(int) { ModInt old(n); n += 1; return old; } ModInt operator--(int) { ModInt old(n); n -= 1; return old; } ModInt operator-() const { return ModInt(mod - n); } }; constexpr int64 mod = 1e9 + 7; using Mint = ModInt<mod>; int main(void) { int64 N, X, D, aD; cin >> N >> X >> D; // X= abs(X); D= abs(D); if (D == 0) { if (X == 0) return 1; else cout << N + 1 << endl; return 0; } aD = abs(D); map<int64, vector<PII>> mp; REP(i, N + 1) { // Sに選ぶ要素の数 int64 diff_X = i * X - (N - i) * X; int64 mod_dx = (diff_X % aD + aD) % aD; int64 mini_D = i > 0 ? (i - 1) * (i) / 2 : 0; int64 maxi_D = (N) * (N - 1) / 2 - (N - i - 1) * (N - i) / 2; int64 mini_ot = (N) * (N - 1) / 2 - mini_D; int64 maxi_ot = (N) * (N - 1) / 2 - maxi_D; int64 ll = diff_X + D * (mini_D - mini_ot), rr = diff_X + D * (maxi_D - maxi_ot); mp[(ll % (aD * 2) + (aD * 2)) % (aD * 2)].emplace_back(minmax( diff_X + D * (mini_D - mini_ot), diff_X + D * (maxi_D - maxi_ot))); } int64 res = 0; for (auto &v : mp) { int64 l = numeric_limits<int64_t>::min(); sort(all(v.sc)); REP(i, v.sc.size()) { PII p(v.sc[i]); // cout << p.fs << " " << p.sc << " " << l[0] << " " << l[1] << endl; if (l >= p.sc) continue; if (l < p.fs) res++; chmax(l, p.fs); res += (p.sc - l) / (aD * 2); chmax(l, p.sc); } } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for (int64 i = 0; i < (n); i++) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } 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...)); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } template <::std::uint_fast64_t mod> class ModInt { private: using value_type = ::std::uint_fast64_t; value_type n; public: ModInt() : n(0) {} ModInt(value_type n_) : n(n_ % mod) {} ModInt(const ModInt &m) : n(m.n) {} template <typename T> explicit operator T() const { return static_cast<T>(n); } value_type get() const { return n; } friend ::std::ostream &operator<<(::std::ostream &os, const ModInt<mod> &a) { return os << a.n; } friend ::std::istream &operator>>(::std::istream &is, ModInt<mod> &a) { value_type x; is >> x; a = ModInt<mod>(x); return is; } bool operator==(const ModInt &m) const { return n == m.n; } bool operator!=(const ModInt &m) const { return n != m.n; } ModInt &operator*=(const ModInt &m) { n = n * m.n % mod; return *this; } ModInt pow(value_type b) const { ModInt ans = 1, m = ModInt(*this); while (b) { if (b & 1) ans *= m; m *= m; b >>= 1; } return ans; } ModInt inv() const { return (*this).pow(mod - 2); } ModInt &operator+=(const ModInt &m) { n += m.n; n = (n < mod ? n : n - mod); return *this; } ModInt &operator-=(const ModInt &m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; } ModInt &operator/=(const ModInt &m) { *this *= m.inv(); return *this; } ModInt operator+(const ModInt &m) const { return ModInt(*this) += m; } ModInt operator-(const ModInt &m) const { return ModInt(*this) -= m; } ModInt operator*(const ModInt &m) const { return ModInt(*this) *= m; } ModInt operator/(const ModInt &m) const { return ModInt(*this) /= m; } ModInt &operator++() { n += 1; return *this; } ModInt &operator--() { n -= 1; return *this; } ModInt operator++(int) { ModInt old(n); n += 1; return old; } ModInt operator--(int) { ModInt old(n); n -= 1; return old; } ModInt operator-() const { return ModInt(mod - n); } }; constexpr int64 mod = 1e9 + 7; using Mint = ModInt<mod>; int main(void) { int64 N, X, D, aD; cin >> N >> X >> D; // X= abs(X); D= abs(D); if (D == 0) { if (X == 0) cout << 1 << endl; else cout << N + 1 << endl; return 0; } aD = abs(D); map<int64, vector<PII>> mp; REP(i, N + 1) { // Sに選ぶ要素の数 int64 diff_X = i * X - (N - i) * X; int64 mod_dx = (diff_X % aD + aD) % aD; int64 mini_D = i > 0 ? (i - 1) * (i) / 2 : 0; int64 maxi_D = (N) * (N - 1) / 2 - (N - i - 1) * (N - i) / 2; int64 mini_ot = (N) * (N - 1) / 2 - mini_D; int64 maxi_ot = (N) * (N - 1) / 2 - maxi_D; int64 ll = diff_X + D * (mini_D - mini_ot), rr = diff_X + D * (maxi_D - maxi_ot); mp[(ll % (aD * 2) + (aD * 2)) % (aD * 2)].emplace_back(minmax( diff_X + D * (mini_D - mini_ot), diff_X + D * (maxi_D - maxi_ot))); } int64 res = 0; for (auto &v : mp) { int64 l = numeric_limits<int64_t>::min(); sort(all(v.sc)); REP(i, v.sc.size()) { PII p(v.sc[i]); // cout << p.fs << " " << p.sc << " " << l[0] << " " << l[1] << endl; if (l >= p.sc) continue; if (l < p.fs) res++; chmax(l, p.fs); res += (p.sc - l) / (aD * 2); chmax(l, p.sc); } } cout << res << endl; }
replace
139
140
139
140
0
p02840
C++
Runtime Error
#ifndef DEBUG #define NDEBUG #endif #include <algorithm> #include <cassert> #include <iostream> #include <iterator> #include <limits> #include <numeric> #include <set> #include <string> #include <type_traits> #include <utility> #include <vector> using namespace std; // template {{{ // misc {{{ using Z = long long; [[maybe_unused]] constexpr Z INF = numeric_limits<Z>::max() / 3; // }}} // debug {{{ template <class T> using enable_if_sequence_t = enable_if_t< is_same<typename iterator_traits<typename remove_cv_t< remove_reference_t<T>>::iterator>::iterator_category, random_access_iterator_tag>{}>; #ifdef NDEBUG #define dump(x) #define debug if (0) template <class T> inline T &&ix(T &&value) { return value; } template <class T, class Index, class... Indices, class = enable_if_sequence_t<T>> inline auto &&ix(T &&container, Index index, Indices... indices) { return ix(container[index], indices...); } #else #define dump(x) \ do { \ cerr << "\e[33m[dump: " << __LINE__ << "] " << (#x) << " = " << x \ << "\e[0m" << endl; \ } while (0); #define debug template <class T> inline auto &&ix_impl([[maybe_unused]] int line, [[maybe_unused]] const char *code, T &&value) { return value; } template <class T, class Index, class... Indices, class = enable_if_sequence_t<T>> inline auto &&ix_impl(int line, const char *code, T &&container, Index index, Indices... indices) { if (index < 0) { cerr << "\e[31mix: negative index at line " << line << ", ix(" << code << ")\e[0m" << endl; exit(1); } int size = container.size(); if (index >= size) { cerr << "\e[31mix: index out of range at line " << line << ", ix(" << code << ")\n" << "index = " << index << " whereas size = " << size << "\e[0m" << endl; exit(1); } return ix_impl(line, code, container[index], indices...); } #define ix(...) ix_impl(__LINE__, #__VA_ARGS__, __VA_ARGS__) #endif // }}} // IO {{{ template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T> false_type is_container_impl(...); template <class T, class = typename T::iterator> true_type is_container_impl(T); template <class T> struct is_container : decltype(is_container_impl<T>(declval<T>())) {}; template <> struct is_container<string> : std::false_type {}; template <class Container, class = enable_if_t<is_container<Container>{}>> istream &operator>>(istream &is, Container &cont) { for (auto &v : cont) is >> v; return is; } template <class Container, class = enable_if_t<is_container<Container>{}>> ostream &operator<<(ostream &os, Container const &cont) { bool first = true; constexpr char sep = is_container<typename Container::value_type>{} ? '\n' : ' '; for (auto const &v : cont) { if (!first) { os << sep; } first = false; os << v; } return os; } // }}} // }}} // {{{ extgcd template <class T> struct extgcd_solution { T gcd, x, y; }; template <class T = Z> constexpr extgcd_solution<T> extgcd(T a, T b) { if (b == 0) return {a, 1, 0}; if (a < b) { auto res = extgcd(b, a); return {res.gcd, res.y, res.x}; } auto prev = extgcd(b, a % b); return {prev.gcd, prev.y, prev.x - prev.y * (a / b)}; } // }}} int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; Z N, X, D; cin >> N >> X >> D; Z g = extgcd(abs(X), abs(D)).gcd; X /= g; D /= g; if (D < 0) { X *= -1; D *= -1; } dump(X); dump(D); Z ans = 0; for (Z i = 0; i < D; i++) { if (i > N) break; dump(i); set<pair<Z, Z>> intervals; for (Z j = i; j <= N; j += D) { Z l = (j - 1) * j / 2, r = (N - j + N - 1) * j / 2; intervals.emplace(j * X + l * D, j * X + r * D + D); } dump(intervals); while (intervals.size() >= 2) { auto fst = *begin(intervals), snd = *next(begin(intervals)); intervals.erase(begin(intervals)); if (snd.first <= fst.second) { intervals.erase(begin(intervals)); intervals.emplace(fst.first, max(fst.second, snd.second)); } else { dump(fst); ans += (fst.second - fst.first) / D; } } dump(intervals); ans += (begin(intervals)->second - begin(intervals)->first) / D; } cout << ans << endl; }
#ifndef DEBUG #define NDEBUG #endif #include <algorithm> #include <cassert> #include <iostream> #include <iterator> #include <limits> #include <numeric> #include <set> #include <string> #include <type_traits> #include <utility> #include <vector> using namespace std; // template {{{ // misc {{{ using Z = long long; [[maybe_unused]] constexpr Z INF = numeric_limits<Z>::max() / 3; // }}} // debug {{{ template <class T> using enable_if_sequence_t = enable_if_t< is_same<typename iterator_traits<typename remove_cv_t< remove_reference_t<T>>::iterator>::iterator_category, random_access_iterator_tag>{}>; #ifdef NDEBUG #define dump(x) #define debug if (0) template <class T> inline T &&ix(T &&value) { return value; } template <class T, class Index, class... Indices, class = enable_if_sequence_t<T>> inline auto &&ix(T &&container, Index index, Indices... indices) { return ix(container[index], indices...); } #else #define dump(x) \ do { \ cerr << "\e[33m[dump: " << __LINE__ << "] " << (#x) << " = " << x \ << "\e[0m" << endl; \ } while (0); #define debug template <class T> inline auto &&ix_impl([[maybe_unused]] int line, [[maybe_unused]] const char *code, T &&value) { return value; } template <class T, class Index, class... Indices, class = enable_if_sequence_t<T>> inline auto &&ix_impl(int line, const char *code, T &&container, Index index, Indices... indices) { if (index < 0) { cerr << "\e[31mix: negative index at line " << line << ", ix(" << code << ")\e[0m" << endl; exit(1); } int size = container.size(); if (index >= size) { cerr << "\e[31mix: index out of range at line " << line << ", ix(" << code << ")\n" << "index = " << index << " whereas size = " << size << "\e[0m" << endl; exit(1); } return ix_impl(line, code, container[index], indices...); } #define ix(...) ix_impl(__LINE__, #__VA_ARGS__, __VA_ARGS__) #endif // }}} // IO {{{ template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T> false_type is_container_impl(...); template <class T, class = typename T::iterator> true_type is_container_impl(T); template <class T> struct is_container : decltype(is_container_impl<T>(declval<T>())) {}; template <> struct is_container<string> : std::false_type {}; template <class Container, class = enable_if_t<is_container<Container>{}>> istream &operator>>(istream &is, Container &cont) { for (auto &v : cont) is >> v; return is; } template <class Container, class = enable_if_t<is_container<Container>{}>> ostream &operator<<(ostream &os, Container const &cont) { bool first = true; constexpr char sep = is_container<typename Container::value_type>{} ? '\n' : ' '; for (auto const &v : cont) { if (!first) { os << sep; } first = false; os << v; } return os; } // }}} // }}} // {{{ extgcd template <class T> struct extgcd_solution { T gcd, x, y; }; template <class T = Z> constexpr extgcd_solution<T> extgcd(T a, T b) { if (b == 0) return {a, 1, 0}; if (a < b) { auto res = extgcd(b, a); return {res.gcd, res.y, res.x}; } auto prev = extgcd(b, a % b); return {prev.gcd, prev.y, prev.x - prev.y * (a / b)}; } // }}} int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; Z N, X, D; cin >> N >> X >> D; if (D == 0) { cout << (X == 0 ? 1 : N + 1) << endl; return 0; } Z g = extgcd(abs(X), abs(D)).gcd; X /= g; D /= g; if (D < 0) { X *= -1; D *= -1; } dump(X); dump(D); Z ans = 0; for (Z i = 0; i < D; i++) { if (i > N) break; dump(i); set<pair<Z, Z>> intervals; for (Z j = i; j <= N; j += D) { Z l = (j - 1) * j / 2, r = (N - j + N - 1) * j / 2; intervals.emplace(j * X + l * D, j * X + r * D + D); } dump(intervals); while (intervals.size() >= 2) { auto fst = *begin(intervals), snd = *next(begin(intervals)); intervals.erase(begin(intervals)); if (snd.first <= fst.second) { intervals.erase(begin(intervals)); intervals.emplace(fst.first, max(fst.second, snd.second)); } else { dump(fst); ans += (fst.second - fst.first) / D; } } dump(intervals); ans += (begin(intervals)->second - begin(intervals)->first) / D; } cout << ans << endl; }
insert
134
134
134
138
0
p02840
C++
Runtime Error
// includes #include <bits/stdc++.h> using namespace std; // macros #define pb emplace_back #define mk make_pair #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr) #define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define bit(n) (1LL << (n)) // functions template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0; i < vec.size(); i++) { os << vec[i]; if (i + 1 != vec.size()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << itr->first << ":" << itr->second; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << itr->first << ":" << itr->second; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } // types using ll = long long int; using P = pair<int, int>; // constants const int inf = 1e9; const ll linf = 1LL << 50; const double EPS = 1e-10; const int mod = 1000000007; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // io struct fast_io { fast_io() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); } } fast_io_; template <typename T> struct MergableRangeSet { using PT = pair<T, T>; set<PT> st; MergableRangeSet() {} // [l, r) void add(T l, T r) { if (r <= l) return; auto itr = st.upper_bound(make_pair(l, 0)); if (itr != st.begin()) { auto litr = itr; --litr; if (litr->second >= r) { return; } else if (litr->second >= l) { l = litr->first; st.erase(litr); } } if (itr != st.end()) { if (r < itr->first) st.insert(make_pair(l, r)); else { PT p = make_pair(l, max(r, itr->second)); st.erase(itr); st.insert(p); } } else { st.insert(make_pair(l, r)); } } typename set<PT>::iterator begin() noexcept { return st.begin(); } typename set<PT>::iterator end() noexcept { return st.end(); } size_t size() { return st.size(); } }; map<ll, MergableRangeSet<ll>> ms; int main(int argc, char const *argv[]) { int n; cin >> n; ll x, d; cin >> x >> d; rep(i, n + 1) { ll b = ((ll)i * x) % d; ll of = ((ll)i * x) / d; if (ms.find(b) == ms.end()) ms[b] = MergableRangeSet<ll>(); ll l = (ll)(i - 1) * (ll)i / 2; ll r = l + (ll)(i) * (ll)(n - i); ms[b].add(l + of, r + of + 1); } ll res = 0; irep(itr, ms) { auto &v = itr->second; irep(im, v) { res += im->second - im->first; } } cout << res << endl; return 0; }
// includes #include <bits/stdc++.h> using namespace std; // macros #define pb emplace_back #define mk make_pair #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr) #define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define bit(n) (1LL << (n)) // functions template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0; i < vec.size(); i++) { os << vec[i]; if (i + 1 != vec.size()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << itr->first << ":" << itr->second; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << itr->first << ":" << itr->second; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } // types using ll = long long int; using P = pair<int, int>; // constants const int inf = 1e9; const ll linf = 1LL << 50; const double EPS = 1e-10; const int mod = 1000000007; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // io struct fast_io { fast_io() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); } } fast_io_; template <typename T> struct MergableRangeSet { using PT = pair<T, T>; set<PT> st; MergableRangeSet() {} // [l, r) void add(T l, T r) { if (r <= l) return; auto itr = st.upper_bound(make_pair(l, 0)); if (itr != st.begin()) { auto litr = itr; --litr; if (litr->second >= r) { return; } else if (litr->second >= l) { l = litr->first; st.erase(litr); } } if (itr != st.end()) { if (r < itr->first) st.insert(make_pair(l, r)); else { PT p = make_pair(l, max(r, itr->second)); st.erase(itr); st.insert(p); } } else { st.insert(make_pair(l, r)); } } typename set<PT>::iterator begin() noexcept { return st.begin(); } typename set<PT>::iterator end() noexcept { return st.end(); } size_t size() { return st.size(); } }; map<ll, MergableRangeSet<ll>> ms; int main(int argc, char const *argv[]) { int n; cin >> n; ll x, d; cin >> x >> d; if (d == 0) { set<ll> st; rep(i, n + 1) st.insert((ll)i * x); cout << sz(st) << endl; return 0; } rep(i, n + 1) { ll b = ((ll)i * x) % d; ll of = ((ll)i * x) / d; if (ms.find(b) == ms.end()) ms[b] = MergableRangeSet<ll>(); ll l = (ll)(i - 1) * (ll)i / 2; ll r = l + (ll)(i) * (ll)(n - i); ms[b].add(l + of, r + of + 1); } ll res = 0; irep(itr, ms) { auto &v = itr->second; irep(im, v) { res += im->second - im->first; } } cout << res << endl; return 0; }
insert
169
169
169
175
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> #define pi 3.1415926535897932384626 using namespace std; struct P { long long x, y; bool operator<(const P &a) const { // if(y!=a.y) return y > a.y; }; }; int a, b, c, d, i, k, n, m, e, dx[10] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[10] = {0, 1, 0, -1, 1, -1, 1, -1}; int o[200101]; int l[631010]; int j[200102]; long long x, y, z, mod = 1000000007; // string r; char r[1]; P u[1]; queue<int> q; // stack<int> s; set<int> s; map<int, int> p; vector<P> v[200101]; bool as(P a, P b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } long long f(long long n, long long m) { if (m < n) return 0; long long x = (n + m) * (m - n + 1) / 2; return x * c; } int main() { scanf("%d %d %d", &a, &b, &c); d = abs(2 * c); for (long long t = 0; t <= a; t++) { y = t * b - (a - t) * b; int i; if (p[(y % d + d) % d]) i = p[(y % d + d) % d]; else e++, p[(y % d + d) % d] = i = e; v[i].push_back( {y - f(t, a - 1) + f(0, t - 1), y + f(a - t, a - 1) - f(0, a - t - 1)}); if (v[i].back().x > v[i].back().y) swap(v[i][v[i].size() - 1].x, v[i][v[i].size() - 1].y); // printf("%lld %lld\n",v[i].back().x,v[i].back().y); } for (int t = 1; t <= e; t++) { sort(v[t].begin(), v[t].end(), as); z = v[t][0].x - 1; // x++; for (int i = 0; i < v[t].size(); i++) if (z < v[t][i].y) { if (z >= v[t][i].x) x += abs((v[t][i].y - z) / (2 * c)); else x += abs((v[t][i].y - v[t][i].x) / (2 * c)) + 1; z = v[t][i].y; } } printf("%lld", x); }
#include <bits/stdc++.h> #define pi 3.1415926535897932384626 using namespace std; struct P { long long x, y; bool operator<(const P &a) const { // if(y!=a.y) return y > a.y; }; }; int a, b, c, d, i, k, n, m, e, dx[10] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[10] = {0, 1, 0, -1, 1, -1, 1, -1}; int o[200101]; int l[631010]; int j[200102]; long long x, y, z, mod = 1000000007; // string r; char r[1]; P u[1]; queue<int> q; // stack<int> s; set<int> s; map<int, int> p; vector<P> v[200101]; bool as(P a, P b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } long long f(long long n, long long m) { if (m < n) return 0; long long x = (n + m) * (m - n + 1) / 2; return x * c; } int main() { scanf("%d %d %d", &a, &b, &c); if (c == 0) { if (b) printf("%d", a + 1); else puts("1"); return 0; } d = abs(2 * c); for (long long t = 0; t <= a; t++) { y = t * b - (a - t) * b; int i; if (p[(y % d + d) % d]) i = p[(y % d + d) % d]; else e++, p[(y % d + d) % d] = i = e; v[i].push_back( {y - f(t, a - 1) + f(0, t - 1), y + f(a - t, a - 1) - f(0, a - t - 1)}); if (v[i].back().x > v[i].back().y) swap(v[i][v[i].size() - 1].x, v[i][v[i].size() - 1].y); // printf("%lld %lld\n",v[i].back().x,v[i].back().y); } for (int t = 1; t <= e; t++) { sort(v[t].begin(), v[t].end(), as); z = v[t][0].x - 1; // x++; for (int i = 0; i < v[t].size(); i++) if (z < v[t][i].y) { if (z >= v[t][i].x) x += abs((v[t][i].y - z) / (2 * c)); else x += abs((v[t][i].y - v[t][i].x) / (2 * c)) + 1; z = v[t][i].y; } } printf("%lld", x); }
insert
45
45
45
52
0
p02840
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-9; const ll mod = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; ll n, x, d; ll f(ll m, ll t) { if (m >= 0) return m / d; return -((-m - 1) / d) - 1; } int main() { cin >> n >> x >> d; if (d == 0) { if (x == 0) cout << 1 << endl; else cout << n + 1 << endl; } if (d < 0) { d *= -1; x *= -1; } ll mn = 0, mx = 0, id = 0; map<ll, ll> mp; vector<vpll> a; for (int i = 0; i <= n; i++) { int t; if (mn >= 0) t = mn % d; else t = (mn - mn / d * d + d) % d; assert(0 <= t && t < d); if (mp.find(t) == mp.end()) { mp[t] = id++; a.push_back(vpll()); } // cout<<t<<' '<<mn<<' '<<mx<<' '<<f(mn,t)<<' '<<f(mx,t)<<endl; a[mp[t]].push_back({f(mn, t), f(mx, t)}); mn += x + d * i; mx += x + d * (n - i - 1); } ll res = 0, m = a.size(); for (int i = 0; i < m; i++) { sort(a[i].begin(), a[i].end()); mx = -INF; ll tmp = 0; // set<int> st; for (auto p : a[i]) { // cout<<p.first<<' '<<p.second<<endl; if (mx < p.first) { tmp += p.second - p.first + 1; mx = p.second; } else if (mx < p.second) { tmp += p.second - mx; mx = p.second; } // for(int j=p.first;j<=p.second;j++) st.insert(j); // cout<<mx<<' '<<tmp<<endl; } // cout<<tmp<<endl; // cout<<st.size()<<endl; res += tmp; } // cout<<st.size()<<endl; 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-9; const ll mod = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; ll n, x, d; ll f(ll m, ll t) { if (m >= 0) return m / d; return -((-m - 1) / d) - 1; } int main() { cin >> n >> x >> d; if (d == 0) { if (x == 0) cout << 1 << endl; else cout << n + 1 << endl; return 0; } if (d < 0) { d *= -1; x *= -1; } ll mn = 0, mx = 0, id = 0; map<ll, ll> mp; vector<vpll> a; for (int i = 0; i <= n; i++) { int t; if (mn >= 0) t = mn % d; else t = (mn - mn / d * d + d) % d; assert(0 <= t && t < d); if (mp.find(t) == mp.end()) { mp[t] = id++; a.push_back(vpll()); } // cout<<t<<' '<<mn<<' '<<mx<<' '<<f(mn,t)<<' '<<f(mx,t)<<endl; a[mp[t]].push_back({f(mn, t), f(mx, t)}); mn += x + d * i; mx += x + d * (n - i - 1); } ll res = 0, m = a.size(); for (int i = 0; i < m; i++) { sort(a[i].begin(), a[i].end()); mx = -INF; ll tmp = 0; // set<int> st; for (auto p : a[i]) { // cout<<p.first<<' '<<p.second<<endl; if (mx < p.first) { tmp += p.second - p.first + 1; mx = p.second; } else if (mx < p.second) { tmp += p.second - mx; mx = p.second; } // for(int j=p.first;j<=p.second;j++) st.insert(j); // cout<<mx<<' '<<tmp<<endl; } // cout<<tmp<<endl; // cout<<st.size()<<endl; res += tmp; } // cout<<st.size()<<endl; cout << res << endl; }
insert
43
43
43
44
0
p02840
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, LL> pll; #define pb push_back #define mp make_pair const int N = 2e5 + 10; int n; LL X, D; stack<pll> V[N]; unordered_map<LL, int> MP; int getidx(LL v) { if (MP.count(v)) { return MP[v]; } int m = MP.size(); return MP[v] = m; } bool cross(LL L1, LL R1, LL L2, LL R2) { return min(R1, R2) >= max(L1, L2); } int main() { scanf("%d %lld %lld", &n, &X, &D); LL d = abs(D); MP[0] = 0; V[0].push(mp(0, 0)); LL low = 0, top = 0, lft = 0, rgt = n - 1; LL L, R, r; int idx; for (int i = 1; i <= n; i++) { low += lft; top += rgt; lft++; rgt--; r = (X * i % d + d) % d; L = (X * i - r) / D + low; R = (X * i - r) / D + top; idx = getidx(r); while (!V[idx].empty() && cross(L, R, V[idx].top().first, V[idx].top().second)) { L = min(L, V[idx].top().first); R = max(R, V[idx].top().second); V[idx].pop(); } V[idx].push(mp(L, R)); } LL ans = 0; int m = MP.size(); for (int i = 0; i < m; i++) { while (!V[i].empty()) { ans = ans + V[i].top().second - V[i].top().first + 1; V[i].pop(); } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, LL> pll; #define pb push_back #define mp make_pair const int N = 2e5 + 10; int n; LL X, D; stack<pll> V[N]; unordered_map<LL, int> MP; int getidx(LL v) { if (MP.count(v)) { return MP[v]; } int m = MP.size(); return MP[v] = m; } bool cross(LL L1, LL R1, LL L2, LL R2) { return min(R1, R2) >= max(L1, L2); } int main() { scanf("%d %lld %lld", &n, &X, &D); if (D == 0) { if (X == 0) { puts("1"); } else { printf("%d\n", n + 1); } return 0; } LL d = abs(D); MP[0] = 0; V[0].push(mp(0, 0)); LL low = 0, top = 0, lft = 0, rgt = n - 1; LL L, R, r; int idx; for (int i = 1; i <= n; i++) { low += lft; top += rgt; lft++; rgt--; r = (X * i % d + d) % d; L = (X * i - r) / D + low; R = (X * i - r) / D + top; idx = getidx(r); while (!V[idx].empty() && cross(L, R, V[idx].top().first, V[idx].top().second)) { L = min(L, V[idx].top().first); R = max(R, V[idx].top().second); V[idx].pop(); } V[idx].push(mp(L, R)); } LL ans = 0; int m = MP.size(); for (int i = 0; i < m; i++) { while (!V[i].empty()) { ans = ans + V[i].top().second - V[i].top().first + 1; V[i].pop(); } } printf("%lld\n", ans); return 0; }
insert
28
28
28
38
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02840
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll INF = 1ll << 60; typedef pair<ll, ll> P; int main() { ll n, x, d; cin >> n >> x >> d; map<int, vector<P>> mp; rep(i, n + 1) { ll s = x * i; ll l = (ll)i * (i - 1) / 2; ll r = l + (ll)i * (n - i); s += l * d; r -= l; mp[(s + (d << 30)) % d].emplace_back(s, r); } ll ans = 0; for (auto p : mp) { auto a = p.second; int m = a.size(); rep(i, m) a[i].first = (a[i].first - p.first) / d; vector<P> ev; rep(i, m) { ev.emplace_back(a[i].first, 1); ev.emplace_back(a[i].first + a[i].second + 1, -1); } sort(ev.begin(), ev.end()); ll cnt = 0, pre = -INF; for (auto e : ev) { ll len = e.first - pre; if (cnt) ans += len; cnt += e.second; pre = e.first; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll INF = 1ll << 60; typedef pair<ll, ll> P; int main() { ll n, x, d; cin >> n >> x >> d; if (d == 0) { if (x == 0) { cout << 1 << endl; } else { cout << n + 1 << endl; } return 0; } map<int, vector<P>> mp; rep(i, n + 1) { ll s = x * i; ll l = (ll)i * (i - 1) / 2; ll r = l + (ll)i * (n - i); s += l * d; r -= l; mp[(s + (d << 30)) % d].emplace_back(s, r); } ll ans = 0; for (auto p : mp) { auto a = p.second; int m = a.size(); rep(i, m) a[i].first = (a[i].first - p.first) / d; vector<P> ev; rep(i, m) { ev.emplace_back(a[i].first, 1); ev.emplace_back(a[i].first + a[i].second + 1, -1); } sort(ev.begin(), ev.end()); ll cnt = 0, pre = -INF; for (auto e : ev) { ll len = e.first - pre; if (cnt) ans += len; cnt += e.second; pre = e.first; } } cout << ans << endl; return 0; }
insert
12
12
12
22
0
p02840
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf = 1e9 + 7; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back #define debug(x) cout << #x << " = " << (x) << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } #define int long long void solve() { int n, x, d; cin >> n >> x >> d; static map<int, vector<pair<int, int>>> mp; for (int k = 0; k <= n; ++k) { int a = x * k % d, b = x * k / d; int p = b + k * (k - 1) / 2, q = b + k * (2 * n - k - 1) / 2; if (p > q) swap(p, q); mp[a].push_back(make_pair(p, q + 1)); } int ans = 0; for (auto &X : mp) { auto vec = X.sc; sort(all(vec)); int l = vec[0].fr, r = vec[0].sc; for (auto &ra : vec) { int p = ra.fr, q = ra.sc; if (p <= r) { r = max(r, q); } else { ans += r - l; l = p, r = q; } } ans += r - l; } cout << ans << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(10); // init(); solve(); // cout << "finish" << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf = 1e9 + 7; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back #define debug(x) cout << #x << " = " << (x) << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } #define int long long void solve() { int n, x, d; cin >> n >> x >> d; if (x == 0 && d == 0) { cout << 1 << endl; return; } if (d == 0) { cout << n + 1 << endl; return; } static map<int, vector<pair<int, int>>> mp; for (int k = 0; k <= n; ++k) { int a = x * k % d, b = x * k / d; int p = b + k * (k - 1) / 2, q = b + k * (2 * n - k - 1) / 2; if (p > q) swap(p, q); mp[a].push_back(make_pair(p, q + 1)); } int ans = 0; for (auto &X : mp) { auto vec = X.sc; sort(all(vec)); int l = vec[0].fr, r = vec[0].sc; for (auto &ra : vec) { int p = ra.fr, q = ra.sc; if (p <= r) { r = max(r, q); } else { ans += r - l; l = p, r = q; } } ans += r - l; } cout << ans << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(10); // init(); solve(); // cout << "finish" << endl; return 0; }
insert
66
66
66
74
0
p02840
C++
Runtime Error
// template version 1.14 using namespace std; #include <bits/stdc++.h> // varibable settings #define int long long const int INF = 1e18; const int MOD = 1e9 + 7; // define basic macro {{{ #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (int i = (int)((b)-1); i >= (int)(a); --i) #define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define mt(a, b, c) make_tuple(a, b, c) #define ceil(a, b) ((a) + (b)-1) / (b) #define is_in(x, a, b) ((a) <= (x) && (x) < (b)) #define uni(x) \ sort(all(x)); \ x.erase(unique(all(x)), x.end()) #define ub upper_bound #define lb lower_bound #define posl(A, x) (lower_bound(all(A), x) - A.begin()) #define posu(A, x) (upper_bound(all(A), x) - A.begin()) template <class T> inline void chmax(T &a, const T &b) { if ((a) < (b)) (a) = (b); } template <class T> inline void chmin(T &a, const T &b) { if ((a) > (b)) (a) = (b); } typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef long double ld; typedef pair<int, int> pii; typedef tuple<int, int, int> iii; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; #if defined(PCM) || defined(LOCAL) #include "lib/dump.hpp" #else #define dump(...) 42 #define dump_1d(...) 42 #define dump_2d(...) 42 #endif //}}} signed main() { int n, x, d; cin >> n >> x >> d; map<int, vector<pii>> z; int ans = 0; if (d < 0) { int last = x + (n - 1) * d; x = last; d = -d; } dump(x, d); int l = 0; int r = 0; rep(a, 0, n + 1) { int add = (a * x) / d; int rem = (a * x) % d; if (rem < 0) { rem += d; add -= 1; } dump(a, l, r); ans += r - l + 1; if (sz(z[rem]) > 0) { auto tmp = *(z[rem].rbegin()); int preladd = tmp.first; int preradd = tmp.second; int dec = max(0LL, min(add + r, preradd) - max(add + l, preladd) + 1); dump(preladd, preradd, add + l, add + r, dec); ans -= dec; } z[rem].pb(mp(add + l, add + r)); l += a; r += n - (a + 1); } dump(z); cout << ans << endl; return 0; }
// template version 1.14 using namespace std; #include <bits/stdc++.h> // varibable settings #define int long long const int INF = 1e18; const int MOD = 1e9 + 7; // define basic macro {{{ #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (int i = (int)((b)-1); i >= (int)(a); --i) #define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define mt(a, b, c) make_tuple(a, b, c) #define ceil(a, b) ((a) + (b)-1) / (b) #define is_in(x, a, b) ((a) <= (x) && (x) < (b)) #define uni(x) \ sort(all(x)); \ x.erase(unique(all(x)), x.end()) #define ub upper_bound #define lb lower_bound #define posl(A, x) (lower_bound(all(A), x) - A.begin()) #define posu(A, x) (upper_bound(all(A), x) - A.begin()) template <class T> inline void chmax(T &a, const T &b) { if ((a) < (b)) (a) = (b); } template <class T> inline void chmin(T &a, const T &b) { if ((a) > (b)) (a) = (b); } typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef long double ld; typedef pair<int, int> pii; typedef tuple<int, int, int> iii; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; #if defined(PCM) || defined(LOCAL) #include "lib/dump.hpp" #else #define dump(...) 42 #define dump_1d(...) 42 #define dump_2d(...) 42 #endif //}}} signed main() { int n, x, d; cin >> n >> x >> d; map<int, vector<pii>> z; int ans = 0; if (x == 0 && d == 0) { cout << 1 << endl; return 0; } if (d == 0) { cout << n + 1 << endl; return 0; } if (d < 0) { int last = x + (n - 1) * d; x = last; d = -d; } dump(x, d); int l = 0; int r = 0; rep(a, 0, n + 1) { int add = (a * x) / d; int rem = (a * x) % d; if (rem < 0) { rem += d; add -= 1; } dump(a, l, r); ans += r - l + 1; if (sz(z[rem]) > 0) { auto tmp = *(z[rem].rbegin()); int preladd = tmp.first; int preradd = tmp.second; int dec = max(0LL, min(add + r, preradd) - max(add + l, preladd) + 1); dump(preladd, preradd, add + l, add + r, dec); ans -= dec; } z[rem].pb(mp(add + l, add + r)); l += a; r += n - (a + 1); } dump(z); cout << ans << endl; return 0; }
insert
71
71
71
81
0
p02841
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <bitset> #include <cstdlib> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <type_traits> #include <utility> #include <vector> using namespace std; using ll = long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const double PI = acos(-1.0); #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define all(v) v.begin(), v.end() char alfa[] = {'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'}; inline void finish() { cout << -1 << endl; exit(0); } unsigned GetDigit(unsigned num) { return log10(num) + 1; } template <typename T> inline void debug(vector<T> v) { cout << "["; for (ll i = 0; i < v.size() - 1; ++i) { cout << v[i] << ", "; } cout << v[v.size() - 1] << "]" << endl; } void Main(); inline void DebugMain() { while (true) { Main(); } } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); bool debug = true; if (debug) { cout << "\x1b[41m" << "Debug Mode " << endl; } (debug) ? DebugMain() : Main(); return 0; } ll calcC(ld lastNum, ld calcNum, ll A, ll B, ll X) { if (calcNum < 1) { return 0; } ll ans; ans = A * calcNum + B * GetDigit(calcNum); if (calcNum == lastNum) { return calcNum; } if (ans > X) { return calcC(calcNum, calcNum - (fabs(calcNum - lastNum) / 2), A, B, X); } else { return calcC(calcNum, calcNum + (fabs(calcNum - lastNum) / 2), A, B, X); } } inline void Main() { ll X, Y, N; ll A, B; ll M1, D1, M2, D2; ld ans; string S; cin >> M1 >> D1 >> M2 >> D2; if (M1 != M2) { cout << 1; } else { cout << 0; } }
#include <algorithm> #include <array> #include <bitset> #include <cstdlib> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <type_traits> #include <utility> #include <vector> using namespace std; using ll = long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const double PI = acos(-1.0); #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define all(v) v.begin(), v.end() char alfa[] = {'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'}; inline void finish() { cout << -1 << endl; exit(0); } unsigned GetDigit(unsigned num) { return log10(num) + 1; } template <typename T> inline void debug(vector<T> v) { cout << "["; for (ll i = 0; i < v.size() - 1; ++i) { cout << v[i] << ", "; } cout << v[v.size() - 1] << "]" << endl; } void Main(); inline void DebugMain() { while (true) { Main(); } } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); bool debug = false; if (debug) { cout << "\x1b[41m" << "Debug Mode " << endl; } (debug) ? DebugMain() : Main(); return 0; } ll calcC(ld lastNum, ld calcNum, ll A, ll B, ll X) { if (calcNum < 1) { return 0; } ll ans; ans = A * calcNum + B * GetDigit(calcNum); if (calcNum == lastNum) { return calcNum; } if (ans > X) { return calcC(calcNum, calcNum - (fabs(calcNum - lastNum) / 2), A, B, X); } else { return calcC(calcNum, calcNum + (fabs(calcNum - lastNum) / 2), A, B, X); } } inline void Main() { ll X, Y, N; ll A, B; ll M1, D1, M2, D2; ld ans; string S; cin >> M1 >> D1 >> M2 >> D2; if (M1 != M2) { cout << 1; } else { cout << 0; } }
replace
61
62
61
62
TLE
p02841
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define lld long long int void deepu() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif } int main() { deepu(); int m1, d1, m2, d2; cin >> m1 >> d1 >> m2 >> d2; if (m1 == m2) cout << "0\n"; else cout << "1\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define lld long long int void deepu() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif } int main() { // deepu(); int m1, d1, m2, d2; cin >> m1 >> d1 >> m2 >> d2; if (m1 == m2) cout << "0\n"; else cout << "1\n"; return 0; }
replace
15
16
15
16
0
p02841
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define fst first #define snd second #define all(cont) cont.begin(), cont.end() #define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++) #define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i) #define SZ(x) ((int)x.size()) #define EPS 1e-9 #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 #define FIN \ std::ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) const int N = 0; typedef long long int ll; using namespace std; int main() { FIN; #ifndef ONLINE_JUDGE freopen("../in.txt", "r", stdin); freopen("../out.txt", "w", stdout); #endif int d1, m1, d2, m2; cin >> d1 >> m1 >> d2 >> m2; if (d1 != d2) { cout << 1; } else { cout << 0; } return 0; }
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define fst first #define snd second #define all(cont) cont.begin(), cont.end() #define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++) #define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i) #define SZ(x) ((int)x.size()) #define EPS 1e-9 #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 #define FIN \ std::ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) const int N = 0; typedef long long int ll; using namespace std; int main() { FIN; int d1, m1, d2, m2; cin >> d1 >> m1 >> d2 >> m2; if (d1 != d2) { cout << 1; } else { cout << 0; } return 0; }
replace
25
29
25
26
0
p02841
C++
Time Limit Exceeded
#include "bits/stdc++.h" #include <string> using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const double PI = acos(-1.0); const int MOD = pow(10, 9) + 7; #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define all(v) v.begin(), v.end() ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } inline void finish() { cout << -1 << endl; exit(0); } template <typename T> inline void debug(vector<T> v) { cout << "["; for (size_t i = 0; i < v.size() - 1; ++i) { cout << v[i] << ", "; } cout << v[v.size() - 1] << "]" << endl; } class Pos { public: double x, y; Pos(double x, double y) { this->x = x; this->y = y; } Pos() { x = 0.0; y = 0.0; } }; class Mod { static inline ll Add(ll a, ll b) { return (a + b) % MOD; } static inline ll Sub(ll a, ll b) { ll ans = (a - b) % MOD; return (ans < 0) ? (ans + MOD) : ans; } static inline ll Mul(ll a, ll b) { return (a * b) % MOD; } static inline ll Div(ll a, ll b) { return (a % MOD) * modinv(b, MOD) % MOD; } }; inline double distance(Pos p1, Pos p2) { return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); } inline int Kaijo(int n) { int ans = 1; for (int i = 2; i <= n; ++i) { ans *= i; } return ans; } template <typename T> inline int keta(T num) { return to_string(num).length(); } void Main(); inline void DebugMain() { while (true) { Main(); cout << endl; } } int main() { bool debug = true; cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); if (debug) { cout << "\x1b[41m" << "Debug Mode このまま提出すると死" << endl; } try { (debug) ? DebugMain() : Main(); } catch (exception &e) { cout << e.what() << endl; } return 0; } inline void Main() { int m1, m2, d1, d2; cin >> m1 >> d1 >> m2 >> d2; if (m1 == m2) { cout << 0; } else { cout << 1; } }
#include "bits/stdc++.h" #include <string> using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const double PI = acos(-1.0); const int MOD = pow(10, 9) + 7; #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define all(v) v.begin(), v.end() ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } inline void finish() { cout << -1 << endl; exit(0); } template <typename T> inline void debug(vector<T> v) { cout << "["; for (size_t i = 0; i < v.size() - 1; ++i) { cout << v[i] << ", "; } cout << v[v.size() - 1] << "]" << endl; } class Pos { public: double x, y; Pos(double x, double y) { this->x = x; this->y = y; } Pos() { x = 0.0; y = 0.0; } }; class Mod { static inline ll Add(ll a, ll b) { return (a + b) % MOD; } static inline ll Sub(ll a, ll b) { ll ans = (a - b) % MOD; return (ans < 0) ? (ans + MOD) : ans; } static inline ll Mul(ll a, ll b) { return (a * b) % MOD; } static inline ll Div(ll a, ll b) { return (a % MOD) * modinv(b, MOD) % MOD; } }; inline double distance(Pos p1, Pos p2) { return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); } inline int Kaijo(int n) { int ans = 1; for (int i = 2; i <= n; ++i) { ans *= i; } return ans; } template <typename T> inline int keta(T num) { return to_string(num).length(); } void Main(); inline void DebugMain() { while (true) { Main(); cout << endl; } } int main() { bool debug = false; cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); if (debug) { cout << "\x1b[41m" << "Debug Mode このまま提出すると死" << endl; } try { (debug) ? DebugMain() : Main(); } catch (exception &e) { cout << e.what() << endl; } return 0; } inline void Main() { int m1, m2, d1, d2; cin >> m1 >> d1 >> m2 >> d2; if (m1 == m2) { cout << 0; } else { cout << 1; } }
replace
94
95
94
95
TLE
p02841
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define mp make_pair #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, a, b) for (int i = a; i <= (b); ++i) #define repr(i, a, b) for (int i = a; i >= (b); --i) #define bit(n) (1LL << (n)) #define sz(x) ((ll)(x).size()) typedef long long ll; const int INF = 1001001001; const ll LINF = 1001001001001001001ll; const int MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int M1, D1; cin >> M1 >> D1; int M2, D2; cin >> M2 >> D2; int j = 0; rep(i, 1000000000000) { j++; } if (M1 == M2) { if (D1 + 1 == D2) cout << 0 << endl; else cout << 1 << endl; } else { cout << 1 << endl; } }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define mp make_pair #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, a, b) for (int i = a; i <= (b); ++i) #define repr(i, a, b) for (int i = a; i >= (b); --i) #define bit(n) (1LL << (n)) #define sz(x) ((ll)(x).size()) typedef long long ll; const int INF = 1001001001; const ll LINF = 1001001001001001001ll; const int MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int M1, D1; cin >> M1 >> D1; int M2, D2; cin >> M2 >> D2; cout << (M1 != M2) << endl; }
replace
39
50
39
40
TLE
p02841
Python
Runtime Error
m1, d1 = map(int, input().split()) m2, d2 = map(int, input().split()) if m1 != m2: print("1") exit(1) print("0")
m1, d1 = map(int, input().split()) m2, d2 = map(int, input().split()) if m1 != m2: print("1") else: print("0")
replace
5
8
5
7
0
p02841
Python
Runtime Error
M1 = int(input()) D1 = int(input()) M2 = int(input()) D2 = int(input()) if D2 == 1: print(1) else: print(0)
date_entry_1 = input() M1, D1 = map(int, date_entry_1.split(" ")) date_entry_2 = input() M2, D2 = map(int, date_entry_2.split(" ")) if D2 == 1: print(1) else: print(0)
replace
0
4
0
4
ValueError: invalid literal for int() with base 10: '11 16'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02841/Python/s700613660.py", line 1, in <module> M1 = int(input()) ValueError: invalid literal for int() with base 10: '11 16'
p02841
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define PI 3.14159265 using namespace std; using ll = long long; void self_max(int &a, int b) { a = max(a, b); } void self_min(ll &a, ll b) { a = min(a, b); } int main() { int m1, d1; cin >> m1 >> d1; int m2, d2; cin >> m2 >> d2; if (m1 + 1 == m2) return 1; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define PI 3.14159265 using namespace std; using ll = long long; void self_max(int &a, int b) { a = max(a, b); } void self_min(ll &a, ll b) { a = min(a, b); } int main() { int m1, d1; cin >> m1 >> d1; int m2, d2; cin >> m2 >> d2; if (m1 + 1 == m2) cout << 1 << endl; else cout << 0 << endl; return 0; }
replace
15
16
15
18
0
p02842
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int N; cin >> N; int k = N; while (k >= N / 2) { int kj = k * 108; kj /= 100; if (kj == N) { cout << k << endl; return 0; } } cout << ":(" << endl; }
#include <iostream> using namespace std; int main() { int N; cin >> N; int k = N; while (k >= N / 2) { int kj = k * 108; kj /= 100; if (kj == N) { cout << k << endl; return 0; } k--; } cout << ":(" << endl; }
insert
13
13
13
14
TLE
p02842
C++
Runtime Error
#include <iostream> int main(void) { int n; std::cin >> n; for (int i = n / 2; i < n; i++) { if (static_cast<int>(i * 1.08) == n) { std::cout << i << std::endl; return 1; } } std::cout << ":(" << std::endl; }
#include <iostream> int main(void) { int n; std::cin >> n; int a = n / 1.08; int b = a * 1.08; int c = (a + 1) * 1.08; if (b == n) std::cout << a << std::endl; else if (c == n) std::cout << a + 1 << std::endl; else std::cout << ":(" << std::endl; }
replace
4
11
4
13
1
p02842
C++
Time Limit Exceeded
#include <iostream> int main() { int N; std::cin >> N; int X = N * 25 / 27; for (;;) { int n = 27 * X / 25; if (n == N) break; if (n > N) { std::cout << ":(" << std::endl; return 0; } } std::cout << X << std::endl; return 0; }
#include <iostream> int main() { int N; std::cin >> N; int X = N * 25 / 27; for (;; ++X) { int n = 27 * X / 25; if (n == N) break; if (n > N) { std::cout << ":(" << std::endl; return 0; } } std::cout << X << std::endl; return 0; }
replace
6
7
6
7
TLE
p02842
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_popcount 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){for(auto // a:v){s<<a<<"\n";}return s;} 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]; } }; 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; cin >> N; vi m(N + 1); foor(X, 1, N) { m[X * 108 / 100] = X; } if (m[N]) { print(m[N]); } else { print(":("); } 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_popcount 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){for(auto // a:v){s<<a<<"\n";}return s;} 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]; } }; 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; cin >> N; vi m(N + 1); foor(X, 1, N) { if (X * 108 / 100 <= N) m[X * 108 / 100] = X; } if (m[N]) { print(m[N]); } else { print(":("); } return 0; }
replace
684
685
684
688
-6
double free or corruption (!prev)
p02842
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef unsigned long long int ll; #define fastio ios_base::sync_with_stdio(false); #define rep(i, start, end) for (int i = start; i < end; i++) using namespace std; // debugger template #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } void show(int *arr, int size) { rep(i, 0, size) { cout << arr[i] << " "; } cout << endl; } int main() { fastio int n; cin >> n; for (int i = 1; n <= 50000; i++) { if ((int)(i * 1.08) == n) { cout << i << endl; return 0; } } cout << ":(" << endl; return 0; }
#include <bits/stdc++.h> typedef unsigned long long int ll; #define fastio ios_base::sync_with_stdio(false); #define rep(i, start, end) for (int i = start; i < end; i++) using namespace std; // debugger template #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } void show(int *arr, int size) { rep(i, 0, size) { cout << arr[i] << " "; } cout << endl; } int main() { fastio int n; cin >> n; for (int i = 1; n <= 50000; i++) { if ((int)(i * 1.08) == n) { cout << i << endl; return 0; } if ((int)(i * 1.08) > n) { break; } } cout << ":(" << endl; return 0; }
insert
36
36
36
39
TLE
p02842
Python
Runtime Error
N = int(input()) for x in range(1, 50_000): if int(x * 1.08) == N: print(x) exit() print(":(")
N = int(input()) for x in range(1, 50000): if int(x * 1.08) == N: print(x) exit() print(":(")
replace
1
2
1
2
0
p02842
C++
Runtime Error
#include <iostream> #define ALL(a) (a).begin(), (a).end() #define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++) #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) //------ #define R2(i, in, j, jn) REP(i, in) REP(j, jn) using namespace std; int main(void) { int I; cin >> I; int z; z = (I / 1.08); // zeinuki // cout <<(I/1.08)<<" : "<<z; int Z = z * 1.08; // zeikomi if (Z == I) Z = I; else if (int((z + 1) * 1.08) == I) { z = z + 1; Z = int(z * 1.08); } else { cout << ":("; return 1; } cout << to_string(z); return 0; } // 422
#include <iostream> #define ALL(a) (a).begin(), (a).end() #define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++) #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) //------ #define R2(i, in, j, jn) REP(i, in) REP(j, jn) using namespace std; int main(void) { int I; cin >> I; int z; z = (I / 1.08); // zeinuki // cout <<(I/1.08)<<" : "<<z; int Z = z * 1.08; // zeikomi if (Z == I) Z = I; else if (int((z + 1) * 1.08) == I) { z = z + 1; Z = int(z * 1.08); } else { cout << ":("; return 0; } cout << to_string(z); return 0; } // 422
replace
23
24
23
24
0
p02843
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (n); ++i) #define ll long long #define P pair<ll, ll> #define all(v) (v).begin(), (v).end() const ll mod = 1e9 + 7; const ll INF = 1e18; const double pi = acos(-1.0); int main(void) { ll x; cin >> x; bool dp[100010]; dp[0] = true; rep(i, x) rep(j, 6) { if (dp[i] == true) dp[i + 100 + j] = true; } cout << dp[x] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (n); ++i) #define ll long long #define P pair<ll, ll> #define all(v) (v).begin(), (v).end() const ll mod = 1e9 + 7; const ll INF = 1e18; const double pi = acos(-1.0); int main(void) { ll x; cin >> x; bool dp[110000]; dp[0] = true; rep(i, x) rep(j, 6) { if (dp[i] == true) dp[i + 100 + j] = true; } cout << dp[x] << endl; return 0; }
replace
15
16
15
16
0
p02843
C++
Runtime Error
#include <bits/stdc++.h> using u32 = uint32_t; using u64 = uint64_t; using i32 = int32_t; using i64 = int64_t; int main() { std::ios_base::sync_with_stdio(0); u32 price101 = 101, price102 = 102, price103 = 103, price104 = 104, price105 = 105; i32 money; std::cin >> money; std::stringstream stream; stream << money; std::string digits = stream.str(); u32 last2digits = (u32)std::stoi(digits.substr(digits.size() - 2, 2)); while (money >= 0) { if (last2digits >= 5) { last2digits -= 5; money -= price105; } else if (last2digits >= 4) { last2digits -= 4; money -= price104; } else if (last2digits >= 3) { last2digits -= 3; money -= price103; } else if (last2digits >= 2) { last2digits -= 2; money -= price102; } else if (last2digits >= 1) { last2digits -= 1; money -= price101; } else if (last2digits == 0) { std::cout << "1"; return 0; } } std::cout << "0"; return 0; }
#include <bits/stdc++.h> using u32 = uint32_t; using u64 = uint64_t; using i32 = int32_t; using i64 = int64_t; int main() { std::ios_base::sync_with_stdio(0); u32 price101 = 101, price102 = 102, price103 = 103, price104 = 104, price105 = 105; i32 money; std::cin >> money; if (money < 100) { std::cout << "0"; return 0; } std::stringstream stream; stream << money; std::string digits = stream.str(); u32 last2digits = (u32)std::stoi(digits.substr(digits.size() - 2, 2)); while (money >= 0) { if (last2digits >= 5) { last2digits -= 5; money -= price105; } else if (last2digits >= 4) { last2digits -= 4; money -= price104; } else if (last2digits >= 3) { last2digits -= 3; money -= price103; } else if (last2digits >= 2) { last2digits -= 2; money -= price102; } else if (last2digits >= 1) { last2digits -= 1; money -= price101; } else if (last2digits == 0) { std::cout << "1"; return 0; } } std::cout << "0"; return 0; }
insert
15
15
15
20
0
p02843
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 = 1e9 + 7; int main() { int x; cin >> x; int n = x / 100; x %= 100; for (int a0 = 0; a0 <= n; ++a0) { for (int a1 = 0; a0 + a1 <= n; ++a1) { for (int a2 = 0; a0 + a1 + a2 <= n; ++a2) { for (int a3 = 0; a0 + a1 + a2 + a3 <= n; ++a3) { for (int a4 = 0; a0 + a1 + a2 + a3 + a4 <= n; ++a4) { if (a1 + 2 * a2 + 3 * a3 + 4 * a4 + 5 * (n - (a0 + a1 + a2 + a3 + a4)) == x) { cout << 1 << endl; return 0; } } } } } } cout << 0 << endl; }
#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 = 1e9 + 7; int main() { int x; cin >> x; int n = x / 100; x %= 100; for (int a0 = 0; a0 <= n; ++a0) { for (int a1 = 0; a0 + a1 <= n && a1 <= x; ++a1) { for (int a2 = 0; a0 + a1 + a2 <= n && a1 + 2 * a2 <= x; ++a2) { for (int a3 = 0; a0 + a1 + a2 + a3 <= n && a1 + 2 * a2 + 3 * a3 <= x; ++a3) { for (int a4 = 0; a0 + a1 + a2 + a3 + a4 <= n && a1 + 2 * a2 + 3 * a3 + 4 * a4 + 5 * (n - (a0 + a1 + a2 + a3 + a4)) <= x; ++a4) { if (a1 + 2 * a2 + 3 * a3 + 4 * a4 + 5 * (n - (a0 + a1 + a2 + a3 + a4)) == x) { cout << 1 << endl; return 0; } } } } } } cout << 0 << endl; }
replace
12
16
12
21
TLE
p02843
C++
Runtime Error
#include "bits/stdc++.h" #define rep(i, a) for (int i = 0; i < (a); ++i) #define REP(i, a, b) for (int i = (a); i < (b); ++i) #define FORV(a, A) for (auto &a : A) using namespace std; using ll = long long; using P = pair<int, int>; void Main() { int x; cin >> x; int n = x / 100; if ((x % 100) / n < 5) { cout << 1 << endl; return; } else if ((x % 100) / n == 5) { if ((x % 100) % n == 0) { cout << 1 << endl; return; } } cout << 0 << endl; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); Main(); return 0; }
#include "bits/stdc++.h" #define rep(i, a) for (int i = 0; i < (a); ++i) #define REP(i, a, b) for (int i = (a); i < (b); ++i) #define FORV(a, A) for (auto &a : A) using namespace std; using ll = long long; using P = pair<int, int>; void Main() { int x; cin >> x; int n = x / 100; if (n == 0) { cout << 0 << endl; return; } if ((x % 100) / n < 5) { cout << 1 << endl; return; } else if ((x % 100) / n == 5) { if ((x % 100) % n == 0) { cout << 1 << endl; return; } } cout << 0 << endl; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); Main(); return 0; }
insert
14
14
14
18
0
p02843
C++
Runtime Error
// #pragma GCC target("avx2") // #pragma GCC optimization("O3") // #pragma GCC optimization("unroll-loops") // assert(q==w) если q!=w --> runtime error #include <bits/stdc++.h> #define pb push_back #define F first #define S second #define all(x) x.begin(), x.end() #define ub upper_bound #define lb lower_bound #define bit __builtin_popcount #define memset(x) memset(x, 0, sizeof(x)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<vector<int>> matrix; inline void boost() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const ll maxn = 2e5 + 123; const ll inf = 1e9 + 123; const ll linf = 1e18 + 123; const ll mod = 1e9 + 7; const double eps = 1e-9; const double pi = acos(-1); int dx[8] = {0, 1, -1, 0, 1, -1, 1, -1}; int dy[8] = {1, 0, 0, -1, 1, 1, -1, -1}; int main() { boost(); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); int n; cin >> n; int kol = n / 100; int q = n % 100; if (ceil(q / kol) <= 6) { cout << "1"; return 0; } cout << "0"; exit(0); }
// #pragma GCC target("avx2") // #pragma GCC optimization("O3") // #pragma GCC optimization("unroll-loops") // assert(q==w) если q!=w --> runtime error #include <bits/stdc++.h> #define pb push_back #define F first #define S second #define all(x) x.begin(), x.end() #define ub upper_bound #define lb lower_bound #define bit __builtin_popcount #define memset(x) memset(x, 0, sizeof(x)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<vector<int>> matrix; inline void boost() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const ll maxn = 2e5 + 123; const ll inf = 1e9 + 123; const ll linf = 1e18 + 123; const ll mod = 1e9 + 7; const double eps = 1e-9; const double pi = acos(-1); int dx[8] = {0, 1, -1, 0, 1, -1, 1, -1}; int dy[8] = {1, 0, 0, -1, 1, 1, -1, -1}; int main() { boost(); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); int n; cin >> n; int kol = n / 100; int q = n % 100; if (n < 100) { cout << "0"; return 0; } if (q <= kol * 5) { cout << "1"; return 0; } cout << "0"; exit(0); }
replace
47
48
47
52
0
p02843
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool dfs(int k, int r, int cnt) { bool res = false; if (cnt == 0) { if (r == 0 && k >= 0) return 1; else return 0; } for (int i = 0; i <= k; i++) { res |= dfs(k - i, r - cnt * i, cnt - 1); } return res; } int main() { int x; cin >> x; int k = x / 100, r = x % 100; cout << (int)dfs(k, r, 5) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool dfs(int k, int r, int cnt) { bool res = false; if (cnt == 0) { if (r == 0 && k >= 0) return 1; else return 0; } for (int i = k; i >= 0; i--) { if (r - cnt * i >= 0) res |= dfs(k - i, r - cnt * i, cnt - 1); if (res == true) break; } return res; } int main() { int x; cin >> x; int k = x / 100, r = x % 100; cout << (int)dfs(k, r, 5) << endl; return 0; }
replace
12
14
12
17
TLE
p02843
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; bool b = 0; void dfs(int X) { if (X == 100) { b = 1; return; } else if (X == 101) { b = 1; return; } else if (X == 102) { b = 1; return; } else if (X == 103) { b = 1; return; } else if (X == 104) { b = 1; return; } else if (X == 105) { b = 1; return; } if (X < 100) return; dfs(X - 100); dfs(X - 101); dfs(X - 102); dfs(X - 103); dfs(X - 104); dfs(X - 105); } int main() { int X; cin >> X; dfs(X); cout << b; }
#include <bits/stdc++.h> using namespace std; bool b = 0; void dfs(int X) { if (X == 100) { b = 1; return; } else if (X == 101) { b = 1; return; } else if (X == 102) { b = 1; return; } else if (X == 103) { b = 1; return; } else if (X == 104) { b = 1; return; } else if (X == 105) { b = 1; return; } if (X < 100) return; dfs(X - 100); dfs(X - 101); dfs(X - 102); dfs(X - 103); dfs(X - 104); dfs(X - 105); } int main() { int X; cin >> X; int a[X + 1]; memset(a, 0, sizeof a); for (int i = 100; i <= 105; i++) a[i] = 1; for (int i = 200; i <= X; i++) { if (a[i - 100]) a[i] = 1; else if (a[i - 101]) a[i] = 1; else if (a[i - 102]) a[i] = 1; else if (a[i - 103]) a[i] = 1; else if (a[i - 104]) a[i] = 1; else if (a[i - 105]) a[i] = 1; } cout << a[X]; }
replace
36
38
36
55
TLE