#include <bits/stdc++.h> n using namespace std; n bool psort(std::vector<long long int> p1, std::vector<long long int> p2) { n   return p1[0] < p2[0]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m; n   cin >> n; n   std::vector<vector<long long int> > v(n, vector<long long int>(3)); n   for (int i = 0; i < n; ++i) { n     cin >> v[i][0]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> v[i][1]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> v[i][2]; n   } n   sort(v.begin(), v.end(), psort); n   cin >> m; n   long long int start[4] = {0}; n   for (int i = 0; i < m; ++i) { n     long long int k, t, flag = 0; n     cin >> t; n     while (start[t] < n) { n       if (v[start[t]][0] != -1) { n         if (v[start[t]][1] == t || v[start[t]][2] == t) { n           cout << v[start[t]][0] <<    ; n           v[start[t]][0] = -1; n           start[t]++; n           flag = 1; n           break; n         } n       } n       start[t]++; n     } n     if (flag == 0) cout << -1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string fld; n   cin >> fld; n   int l = 1, r = 2 * n; n   int ans = -1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     int posbl = 1; n     int back = -1, flag = 0; n     for (int i = 0; i < n; i++) { n       if (fld[i] ==  * ) n         if (i > back && flag == 0) back = i, flag = 1; n       if (fld[i] ==  P ) { n         if (back == -1) { n           back = i + mid; n         } else { n           if (i <= back) { n             back = max(back, i + mid); n           } else { n             if (flag == 1) { n               if (i - back > mid) { n                 posbl = 0; n                 break; n               } else if (i - back <= mid && mid <= (2 * (i - back))) n                 back = max(i, (mid - (i - back)) / 2 + i); n               else { n                 back = n                     max(mid - (2 * (i - back)) + i, (mid - (i - back)) / 2 + i); n               } n             } else { n               back = i + mid; n             } n           } n         } n         flag = 0; n       } n     } n     if (flag == 1) posbl = 0; n     if (posbl == 1) { n       r = mid - 1; n       ans = mid; n     } else n       l = mid + 1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n char s[N]; n char t1[7] =  twone ; n char t2[4] =  one ; n char t3[4] =  two ; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %s , s); n     int ans = 0; n     int len = strlen(s); n     for (int i = 0; i < len; i++) { n       if (s[i] ==  t ) { n         int f = 0; n         for (int j = i; j <= i + 4; j++) { n           if (s[j] != t1[j - i]) { n             f = 1; n             break; n           } n         } n         if (f == 0) { n           s[i + 2] =  - ; n           ans++; n         } n       } n     } n     for (int i = 0; i < len; i++) { n       if (s[i] ==  t ) { n         int f = 0; n         for (int j = i; j <= i + 2; j++) { n           if (s[j] != t3[j - i]) { n             f = 1; n             break; n           } n         } n         if (f == 0) { n           s[i + 1] =  - ; n           ans++; n         } n       } n     } n     for (int i = 0; i < len; i++) { n       if (s[i] ==  o ) { n         int f = 0; n         for (int j = i; j <= i + 2; j++) { n           if (s[j] != t2[j - i]) { n             f = 1; n             break; n           } n         } n         if (f == 0) { n           s[i + 1] =  - ; n           ans++; n         } n       } n     } n     printf( %d n , ans); n     for (int i = 0; i < len; i++) n       if (s[i] ==  - ) printf( %d  , i + 1); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   long long int n, m = 0, p = 0, cnt = 0, f = 0; n   cin >> n; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] < 0) { n       m++; n     } n     if (a[i] > 0) p++; n     if (a[i] < 0) { n       cnt = cnt - a[i] - 1; n     } n     if (a[i] > 0) { n       cnt = cnt + a[i] - 1; n     } n     if (a[i] == 0) { n       cnt = cnt + 1; n       f++; n     } n   } n   if (f and m % 2 == 0) cout << cnt << endl; n   if (f and m % 2 != 0) cout << cnt << endl; n   if (!f and m % 2 == 0) cout << cnt << endl; n   if (!f and m % 2 != 0) cout << cnt + 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int A[600006]; n vector<int> G[600006]; n int dfn[600006], bc[600006], clo; n int g[600006][20], dep[600006]; n void dfs(int u) { n   dfn[u] = ++clo, bc[clo] = u; n   if (u == 1) dep[u] = 1; n   for (int v : G[u]) { n     g[v][0] = u; n     dep[v] = dep[u] + 1; n     for (int k = (1), kend = (18); k <= kend; ++k) n       if (g[g[v][k - 1]][k - 1]) n         g[v][k] = g[g[v][k - 1]][k - 1]; n       else n         break; n     dfs(v); n   } n } n int lca(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   if (dep[u] != dep[v]) n     for (int k = (18), kend = (0); k >= kend; --k) n       if (dep[g[u][k]] >= dep[v]) u = g[u][k]; n   if (u == v) return u; n   for (int k = (18), kend = (0); k >= kend; --k) n     if (g[u][k] != g[v][k]) u = g[u][k], v = g[v][k]; n   return g[u][0]; n } n int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } n set<int> S; n void solve() { n   cin >> n; n   ++n; n   for (int i = (2), iend = (n); i <= iend; ++i) { n     int p; n     scanf( %d , &p); n     G[p].push_back(i); n   } n   dfs(1); n   int s = 1, t = 1, lt = 0; n   auto wkr = [&](int x) { return max(dis(x, s), dis(x, t)); }; n   S.insert(1); n   for (int i = (2), iend = (n); i <= iend; ++i) { n     int is = dis(i, s), it = dis(i, t), flg = 0; n     if (is >= lt && is >= it) n       t = i, lt = is, flg = 1; n     else if (it >= lt && it >= is) n       s = i, lt = it, flg = 1; n     if (!flg) { n       printf( %d n , (int)S.size()); n       continue; n     } n     auto tr = S.lower_bound(dfn[i]), st = tr; n     while (tr != S.end()) { n       if (wkr(bc[*tr]) < lt) n         ++tr; n       else n         break; n     } n     if (tr == S.end()) { n       auto sr = S.begin(); n       while (sr != st) { n         if (wkr(bc[*sr]) < lt) n           ++sr; n         else n           break; n       } n       S.erase(S.begin(), sr); n     } n     S.erase(st, tr); n     tr = S.lower_bound(dfn[i]), st = tr; n     while (tr != S.begin()) { n       --tr; n       if (wkr(bc[*tr]) >= lt) { n         ++tr; n         break; n       } n     } n     if (tr == S.begin()) { n       auto sr = S.end(); n       while (sr != st) { n         --sr; n         if (wkr(bc[*sr]) >= lt) { n           ++sr; n           break; n         } n       } n       S.erase(sr, S.end()); n     } n     S.erase(tr, st); n     S.insert(dfn[i]); n     printf( %d n , (int)S.size()); n   } n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n struct fenwick { n   long long n, fen[100005]; n   void resize(int _n) { n     n = _n + 1; n     memset(fen, 0, sizeof fen); n   } n   int query(int ind) { n     ind++; n     int ans = 0; n     while (ind >= 1) { n       ans += fen[ind]; n       ind -= ind & -ind; n     } n     return ans; n   } n   void update(int ind, int val) { n     ind++; n     while (ind <= n) { n       fen[ind] += val; n       ind += ind & -ind; n     } n   } n   int query(int l, int r) { return query(r) - query(l - 1); } n }; n const int maxn = 100001; n int n, l, w, x, y, sz[maxn]; n long long res; n bool blocked[maxn]; n vector<pair<int, int> > c, v[maxn]; n fenwick tree; n void dfs(int node, int pnode, int curw, int curl) { n   c.push_back(make_pair(curw, curl)); n   for (auto &i : v[node]) n     if (i.first != pnode && !blocked[i.first]) n       dfs(i.first, node, curw + i.second, curl + 1); n } n long long solve(int node, int curw, int curl) { n   long long ret = 0; n   c.clear(); n   dfs(node, 0, curw, curl); n   sort(c.begin(), c.end()); n   for (auto &i : c) tree.update(i.second, 1); n   for (int i = 0, j = c.size() - 1; i < c.size(); i++) { n     while (i <= j && c[i].first + c[j].first > w) n       tree.update(c[j--].second, -1); n     if (j < i) break; n     tree.update(c[i].second, -1); n     ret += tree.query(l - c[i].second); n   } n   return ret; n } n int dfsz(int node, int pnode) { n   sz[node] = 1; n   for (auto &i : v[node]) n     if (i.first != pnode && !blocked[i.first]) sz[node] += dfsz(i.first, node); n   return sz[node]; n } n int find(int node, int pnode, int n) { n   for (auto &i : v[node]) n     if (i.first != pnode && !blocked[i.first] && 2 * sz[i.first] > n) n       return find(i.first, node, n); n   return node; n } n void decompose(int node) { n   int centroid = find(node, 0, dfsz(node, 0)); n   blocked[centroid] = 1; n   res += solve(centroid, 0, 0); n   for (auto &i : v[centroid]) n     if (!blocked[i.first]) res -= solve(i.first, i.second, 1); n   for (auto &i : v[centroid]) n     if (!blocked[i.first]) decompose(i.first); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> l >> w; n   tree.resize(n + 1); n   for (int i = 2; i <= n && cin >> x >> y; i++) { n     v[i].push_back(make_pair(x, y)); n     v[x].push_back(make_pair(i, y)); n   } n   decompose(1); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 10; n const int N = 5e2 + 10; n using namespace std; n long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } n long long qpow(long long p, long long q) { n   long long f = 1; n   while (q) { n     if (q & 1) f = f * p % 1000000007; n     p = p * p % 1000000007; n     q >>= 1; n   } n   return f; n } n int n, m, k, t; n vector<int> d[4][11][10]; n char a[maxn], b[maxn]; n int gao(char p) { n   if (p ==  A ) n     return 0; n   else if (p ==  T ) n     return 1; n   else if (p ==  G ) n     return 2; n   else n     return 3; n } n void add(vector<int>& d, int x, int y) { n   while (x < d.size()) d[x] += y, x += x & (-x); n } n int get(vector<int>& d, int x) { n   int ret = 0; n   while (x) ret += d[x], x -= x & (-x); n   return ret; n } n int main() { n   int i, j; n   scanf( %s , a + 1); n   n = strlen(a + 1); n   for (i = 0; i <= (int)3; i++) n     for (j = 1; j <= (int)10; j++) n       for (k = 0; k <= (int)j - 1; k++) d[i][j][k].resize((n + j - 1) / j + 1); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= 10; j++) { n       add(d[gao(a[i])][j][(i - 1) % j], (i - 1) / j + 1, 1); n     } n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int op, x, y; n     scanf( %d%d , &op, &x); n     if (op == 1) { n       scanf( %s , b); n       for (i = 1; i <= (int)10; i++) n         add(d[gao(a[x])][i][(x - 1) % i], (x - 1) / i + 1, -1); n       for (i = 1; i <= (int)10; i++) n         add(d[gao(b[0])][i][(x - 1) % i], (x - 1) / i + 1, 1); n       a[x] = b[0]; n     } else { n       scanf( %d%s , &y, b); n       int len = strlen(b), ret = 0; n       for (i = 0; i < len && i < y - x + 1; i++) { n         int mo = (i + x - 1) % len; n         ret += get(d[gao(b[i])][len][mo], (y - 1 - mo) / len + 1) - n                get(d[gao(b[i])][len][mo], (x - 1 + len - 1 - mo) / len); n       } n       printf( %d n , ret); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, t; n   cin >> t; n   for (long long int aa = 0; aa < t; aa++) { n     long long int n, k, d; n     cin >> n >> k >> d; n     long long int mini = k; n     vector<long long int> v(n); n     map<long long int, long long int> m; n     for (i = 0; i < n; i++) { n       cin >> v[i]; n       if (i < d) { n         long long int ss = m.size(); n         m[v[i]]++; n         if (ss < m.size()) m[v[i]] = 1; n       } n     } n     m.size() < mini ? mini = m.size() : mini = mini; n     for (i = 1; i < n - d + 1; i++) { n       m[v[i - 1]]--; n       if (m[v[i - 1]] == 0) m.erase(v[i - 1]); n       long long int ss = m.size(); n       m[v[i + d - 1]]++; n       if (ss < m.size()) m[v[i + d - 1]] = 1; n       m.size() < mini ? mini = m.size() : mini = mini; n     } n     m.size() < mini ? mini = m.size() : mini = mini; n     cout << mini << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int *p = new int[n + 1], c = 0; n   int *m = new int[n + 1]; n   for (int i = 0; i <= n; i++) { n     p[i] = m[i] = 0; n     c ^= i; n     m[i] = c; n   } n   int f = 0; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n     f ^= p[i]; n   } n   int r = 0; n   for (int i = 2; i <= n; i++) { n     int grps = (n / i); n     int rem = n % i; n     if (grps & 1) r ^= m[i - 1]; n     r ^= m[rem]; n   } n   cout << (r ^ f); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n long long a[maxn], b[maxn], dp[maxn]; n int q[maxn]; n int head = 0, tail = 0; n double A(int i, int j) { return dp[i] - dp[j]; } n double B(int i, int j) { return b[i] - b[j]; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   dp[1] = 0; n   int head = 1, tail = 1; n   q[tail] = 1; n   for (int i = 2; i <= n; i++) { n     while (head < tail && n            A(q[head + 1], q[head]) / B(q[head + 1], q[head]) >= -a[i]) n       head++; n     dp[i] = dp[q[head]] + b[q[head]] * a[i]; n     while (head < tail && A(i, q[tail]) / B(i, q[tail]) >= n                               A(q[tail], q[tail - 1]) / B(q[tail], q[tail - 1])) n       tail--; n     q[++tail] = i; n   } n   cout << dp[n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_INT = numeric_limits<int>::max(); n const int MAX_UINT = numeric_limits<unsigned int>::max(); n void get_acum(const vector<int>& v, vector<int>& acum) { n   int sum = 0; n   for (unsigned int i = 0; i < v.size(); ++i) { n     acum.push_back(sum); n     sum += v[i]; n   } n   acum.push_back(sum); n } n int solve(vector<int>& A, vector<int>& B, vector<int>& X, unsigned int k) { n   if (A.size() + X.size() < k || B.size() + X.size() < k) { n     return -1; n   } else { n     sort(A.begin(), A.end()); n     sort(B.begin(), B.end()); n     sort(X.begin(), X.end()); n     vector<int> A_acum, B_acum, X_acum; n     get_acum(A, A_acum); n     get_acum(B, B_acum); n     get_acum(X, X_acum); n     int res = MAX_INT; n     unsigned int max_i = min((unsigned int)X.size(), k); n     for (unsigned int i = 0; i < max_i + 1; ++i) { n       if (k - i < A_acum.size() && k - i < B_acum.size()) { n         int total = A_acum[k - i] + B_acum[k - i] + X_acum[i]; n         if (total < res) res = total; n       } n     } n     return res; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   unsigned int n, k; n   cin >> n >> k; n   vector<int> A; n   vector<int> B; n   vector<int> X; n   for (unsigned int i = 0; i < n; ++i) { n     unsigned int ti; n     bool ai, bi; n     cin >> ti >> ai >> bi; n     if (ai && bi) n       X.push_back(ti); n     else if (ai) n       A.push_back(ti); n     else if (bi) n       B.push_back(ti); n   } n   cout << solve(A, B, X, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long int> adj[3005]; n map<long long int, long long int> mq; n long long int done[3005][3005]; n int main() { n   ios_base::sync_with_stdio(false); n   long long int n, m, i, ans = 0; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     long long int x, y; n     cin >> x >> y; n     adj[x].insert(y); n   } n   for (i = 1; i <= n; i++) { n     for (auto it : adj[i]) { n       for (auto it2 : adj[it]) { n         if (mq.find(it2) != mq.end() && it2 != i) { n           ans += mq[it2]; n         } n         mq[it2]++; n       } n     } n     mq.clear(); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   int a, b, h; n   cin >> a >> b >> h; n   int c = 0; n   while (h != 0) { n     if (c % 2 == 0) { n       h = h - gcd(a, h); n     } else { n       h = h - gcd(b, h); n     } n     c++; n   } n   if (c % 2 == 0) { n     cout << 1 << endl; n   } else n     cout << 0 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 5; n const double EPS = 1e-3; n int n; n int t[MAXN]; n int g(double q) { n   bool ok = true; n   for (int i = 1; i < n; i++) n     if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false; n   if (ok) return 0; n   ok = true; n   for (int i = 2; i < n; i++) n     if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false; n   if (ok) return 1; n   ok = true; n   int prev = t[0]; n   for (int i = 1; i < n; i++) n     if (fabs(double(t[i] - prev * q)) > EPS) { n       if (!ok) return 2; n       ok = false; n     } else n       prev = t[i]; n   return 1; n } n int f() { n   if (n == 1) return 0; n   double q1 = 0, q2 = 0; n   if (t[0] != 0) q1 = double(t[1]) / t[0]; n   if (t[n - 2] != 0) q2 = double(t[n - 1]) / t[n - 2]; n   return min(g(q1), g(q2)); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &t[i]); n   printf( %d n , f()); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 5; n int a[MAX], run[MAX], ans[MAX]; n char s[MAX]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   scanf( %d , &n); n   scanf( %s , s); n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  0 ) n       a[i] = 0; n     else if (s[i] ==  1 ) n       a[i] = 1; n     else n       a[i] = -1; n   } n   vector<pair<int, int>> blocks; n   int it1 = 0, it2 = 0; n   while (it1 != n) { n     while (it2 < n - 1 && a[it1] == a[it2 + 1]) ++it2; n     blocks.emplace_back(it2 - it1 + 1, a[it1]); n     ++it2; n     it1 = it2; n   } n   for (int i = 0; i < (int)blocks.size(); ++i) { n     if (blocks[i].second != -1) continue; n     if (i == 0) n       blocks[i].second = blocks[i + 1].second; n     else if (i == (int)blocks.size() - 1) n       blocks[i].second = blocks[i - 1].second; n     else if (blocks[i - 1].second == blocks[i + 1].second) n       blocks[i].second = blocks[i - 1].second; n   } n   vector<pair<int, int>> compress; n   it1 = 0; n   it2 = 0; n   while (it1 != (int)blocks.size()) { n     int len = blocks[it1].first; n     while (it2 < (int)blocks.size() - 1 && n            blocks[it1].second == blocks[it2 + 1].second) n       it2++, len += blocks[it2].first; n     compress.emplace_back(len, blocks[it1].second); n     ++it2; n     it1 = it2; n   } n   for (auto block : compress) { n     if (block.second == -1) continue; n     for (int len = 1; len <= block.first; len++) ans[len] += block.first / len; n   } n   int max_size = -1, bef = -2; n   int size = compress.size(); n   for (int i = 0; i < size; i++) { n     int len = compress[i].first; n     int type = compress[i].second; n     if (bef != -1 && type != -1) { n       max_size = len; n       if (i != size - 1) max_size += compress[i + 1].first; n       for (int l = 1; l <= max_size; l++) run[l] = len % l; n     } else if (type == -1) { n       for (int l = 1; l <= max_size; l++) { n         if (run[l] + len >= l) n           run[l] += len, ans[l] += run[l] / l, run[l] %= l; n         else n           run[l] = len; n       } n       int add = 0; n       if (i != (int)size - 1) add += compress[i + 1].first; n       if (i != (int)size - 2) add += compress[i + 2].first; n       for (int l = max_size + 1; l <= len + add; ++l) run[l] = (len % l); n       max_size = len + add; n     } else { n       for (int l = 1; l <= max_size; ++l) { n         if (run[l] + (len % l) >= l) n           run[l] += ((len % l) - l), ans[l]++; n         else n           run[l] += (len % l); n       } n     } n     bef = type; n   } n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int f = 1, x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) { n       f = -1; n     } n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int N = 55; n struct W { n   int x, y, tx, ty, id; n } up[N], down[N]; n bool cmp_down(W a, W b) { return a.tx == b.tx ? a.ty < b.ty : a.tx > b.tx; } n bool cmp_up(W a, W b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } n vector<pair<int, int> > ans_fr, ans_to; n bool has[N][N]; n void move_up(int& x, int y) { n   int dx = x; n   has[x][y] = 0; n   has[x - 1][y] = 1; n   ans_fr.push_back(make_pair(dx, y)); n   ans_to.push_back(make_pair(dx - 1, y)); n   --x; n } n void move_down(int& x, int y) { n   int dx = x; n   has[x][y] = 0; n   has[x + 1][y] = 1; n   ans_fr.push_back(make_pair(dx, y)); n   ans_to.push_back(make_pair(dx + 1, y)); n   ++x; n } n void move_left(int x, int& y) { n   int dy = y; n   has[x][y] = 0; n   has[x][y - 1] = 1; n   ans_fr.push_back(make_pair(x, dy)); n   ans_to.push_back(make_pair(x, dy - 1)); n   --y; n } n void move_right(int x, int& y) { n   int dy = y; n   has[x][y] = 0; n   has[x][y + 1] = 1; n   ans_fr.push_back(make_pair(x, dy)); n   ans_to.push_back(make_pair(x, dy + 1)); n   ++y; n } n void move_row(int& x, int& y, int tx, int ty) { n   while (y < ty && has[x][y + 1] == 0) { n     move_right(x, y); n   } n   while (y > ty && has[x][y - 1] == 0) { n     move_left(x, y); n   } n } n void move(int& x, int& y, int tx, int ty) { n   while (x < tx) { n     move_down(x, y); n   } n   while (x > tx) { n     move_up(x, y); n   } n   while (y < ty) { n     move_right(x, y); n   } n   while (y > ty) { n     move_left(x, y); n   } n } n int check(W a) { n   if (a.x == a.tx) { n     return a.y == a.ty ? 0 : 1; n   } else { n     return a.y == a.ty ? 2 : 3; n   } n } n int nx[N], ny[N]; n char mp[N][N]; n int vis[N]; n void write(int n) { n   for (int i = 1; i <= n; i++) { n     cout << mp[i] + 1 << endl; n   } n   puts( ---------------------- ); n } n void deal(int n, int m) { n   if (n == 1) { n     puts( 0 ); n   } else { n     if (m == 1) { n       move(up[1].x, up[1].y, up[1].tx, up[1].ty); n     } else { n       if (cmp_up(up[1], up[2]) == 0) { n         swap(up[1], up[2]); n       } n       if (up[1].x == 2) { n         move_up(up[1].x, up[1].y); n         move_up(up[2].x, up[2].y); n       } else if (up[2].x == 2) { n         move_up(up[2].x, up[2].y); n       } n       switch (check(up[1])) { n         case 0: n           break; n         case 1: n           move(up[2].x, up[2].y, 2, 1); n           move_right(up[1].x, up[1].y); n           break; n         case 2: n           move(up[1].x, up[1].y, up[1].tx, up[1].ty); n           break; n         case 3: n           move(up[1].x, up[1].y, up[1].tx, up[1].ty); n           move_left(up[2].x, up[2].y); n           break; n       } n       move(up[2].x, up[2].y, up[2].tx, up[2].ty); n     } n     cout << ans_fr.size() << endl; n     for (int i = 0; i < ans_fr.size(); i++) { n       int xx1 = ans_fr[i].first, xx2 = ans_to[i].first; n       int yy1 = ans_fr[i].second, yy2 = ans_to[i].second; n       printf( %d %d %d %d n , xx1, yy1, xx2, yy2); n     } n   } n } n int main() { n   int n = read(), m = read(); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       mp[i][j] =  . ; n     } n   } n   for (int i = 1; i <= m; i++) { n     up[i].id = i; n     nx[i] = up[i].x = read(); n     ny[i] = up[i].y = read(); n     has[nx[i]][ny[i]] = 1; n     mp[nx[i]][ny[i]] = i +  0 ; n   } n   for (int i = 1; i <= m; i++) { n     up[i].tx = read(); n     up[i].ty = read(); n   } n   if (n <= 2) { n     deal(n, m); n     return 0; n   } n   copy(up + 1, up + 1 + m, down + 1); n   sort(up + 1, up + 1 + m, cmp_up); n   int cnt = m; n   while (cnt) { n     for (int i = 1; i <= m; i++) { n       if (vis[i] == 0) { n         int id = up[i].id; n         move_row(nx[id], ny[id], 1, i); n         if (ny[id] == i) { n           vis[i] = 1; n           cnt--; n         } n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     int id = up[i].id; n     while (nx[id] > 1) { n       move_up(nx[id], ny[id]); n     } n   } n   for (int i = 1; i <= m; i++) { n     move_down(nx[i], ny[i]); n     move_down(nx[i], ny[i]); n   } n   sort(down + 1, down + 1 + m, cmp_down); n   for (int i = 1; i <= m; i++) { n     int id = down[i].id; n     move_up(nx[id], ny[id]); n     move_row(nx[id], ny[id], 1, i); n     move_up(nx[id], ny[id]); n   } n   for (int i = 1; i <= m; i++) { n     int id = down[i].id; n     while (nx[id] < down[i].tx) { n       move_down(nx[id], ny[id]); n     } n   } n   cnt = m; n   while (cnt) { n     for (int i = 1; i <= m; i++) { n       if (vis[i] == 1) { n         int id = down[i].id; n         move_row(nx[id], ny[id], nx[id], down[i].ty); n         if (ny[id] == down[i].ty) { n           vis[i] = 2; n           cnt--; n         } n       } n     } n   } n   assert(ans_fr.size() == ans_to.size()); n   assert(ans_fr.size() <= 10800); n   cout << ans_fr.size() << endl; n   for (int i = 0; i < ans_fr.size(); i++) { n     int xx1 = ans_fr[i].first, xx2 = ans_to[i].first; n     int yy1 = ans_fr[i].second, yy2 = ans_to[i].second; n     printf( %d %d %d %d n , xx1, yy1, xx2, yy2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a[m], b[m]; n   int ndup = n; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(a, a + m); n   sort(b, b + m, greater<int>()); n   int mn = 0, mx = 0; n   for (int i = 0; i < m; i++) { n     while (a[i] > 0 && n > 0) { n       mn += a[i]; n       a[i]--; n       n--; n     } n   } n   while (ndup > 0) { n     mx += b[0]; n     b[0]--; n     ndup--; n     sort(b, b + m, greater<int>()); n   } n   cout << mx <<     << mn; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, t; n   cin >> t; n   while (t--) { n     cin >> n; n     cout << (n + 1) / 2 << endl; n   } n } n


//...START BY DOING WHAT IS NECESSARY, THEN WHAT IS POSSIBLE AND SUDDENLY YOU ARE DOING THE IMPOSSIBLE...  n #include <bits/stdc++.h> n using namespace std; n #define FAST_FURIER ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); n #define pb push_back n #define mk make_pair n #define rep(i,a,N) for(ll i=a;i<N;i++) n #define rrep(i,a,N) for(ll i=a;i>N;i--) n typedef long long ll; n #define M 1000000007 n bool comp(int x,int y) n { n     return x > y; n } n   n   n /*..............................code starts here........................*/ n // C is first won in M n   n   n int main() { n     FAST_FURIER; n     int tt=1; n     cin >> tt; n     ll m,d,k; n    //ll a,b,c; n     while(tt--) n     { n         cin >> d >> k; n         ll x = 0,y = 0; n         int f = 0; n         while(true){ n             ll d1 = pow(x+k,2)+pow(y,2), d2 = pow(x,2)+pow(y+k,2); n             f = abs(f-1); n             if(d1 < d2 and d1 <= d*d){ n                 x += k; n             } n             else if(d2 <= d1 and d2 <= d*d) y += k; n             else break; n         } n         if(f) cout <<  Utkarsh ; n         else cout <<  Ashish ; n         cout << endl; n  n     } n } n   n   n   n // Author : shivam_123 n   n // g++ -std=c++17


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, e[200000], mix, l, r, o, d[200000]; n vector<long long> v[100000], v1; n set<long long> st; n map<long long, long long> mp, mp1; n string s; n pair<int, int> p[100000], p1[100000]; n char ch[10]; n deque<char> dq; n int main() { n   cin >> a; n   for (int i = 0; i < a / 4; i++) n     if ((a - 2 * (i + 1)) % 2 == 0) c++; n   if (a % 4 == 0) { n     cout << c - 1; n   } else n     cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n char s[5000005]; n int len[1000005]; n int idx[1000005]; n int sum[1000005]; n int sz = 0; n int dp[1000005][20]; n int p[1000005]; n int main() { n   int n, r, c; n   scanf( %d%d%d , &n, &r, &c); n   gets(s); n   gets(s); n   int cnt = 0, slen = 0; n   for (int i = 0; s[i]; i++, slen++) { n     if (s[i] ==     && cnt != 0) { n       len[sz] = cnt; n       idx[sz++] = i - cnt; n       cnt = 0; n     } else n       cnt++; n   } n   if (cnt > 0) { n     len[sz] = cnt; n     idx[sz++] = slen - cnt; n     cnt = 0; n   } n   sum[0] = len[0]; n   for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + 1 + len[i]; n   for (int i = n - 1; i >= 0; i--) { n     int id = upper_bound(sum, sum + n, (i > 0 ? sum[i - 1] + 1 : 0) + c) - sum; n     if (id > n) id = n; n     dp[i][0] = id - i; n   } n   for (int j = 2, t = 1; j <= r; j *= 2, t++) { n     for (int i = n - 1; i >= 0; i--) n       dp[i][t] = dp[i][t - 1] + dp[i + dp[i][t - 1]][t - 1]; n   } n   int maid = -1, ma = 0; n   for (int i = 0; i < n; i++) { n     int idx = i, rr = r, cur = 0; n     for (int j = 20; j >= 0; j--) { n       if (rr >= (1 << j)) { n         rr -= (1 << j); n         cur += dp[idx][j]; n         idx += dp[idx][j]; n       } n     } n     if (cur > ma) maid = i, ma = cur; n   } n   int cn = 1, cs = dp[maid][0]; n   for (int i = maid; i < maid + ma; i++) { n     s[idx[i] + len[i]] = 0; n     if (cn < cs) { n       printf( %s  , s + idx[i]); n       cn++; n     } else { n       printf( %s n , s + idx[i]); n       cs = dp[i + 1][0]; n       cn = 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char change(char curranswer) { n   if (curranswer ==  z ) n     curranswer =  A ; n   else if (curranswer ==  Z ) n     curranswer =  0 ; n   else if (curranswer ==  9 ) n     ; n   else n     curranswer++; n   return curranswer; n } n int main() { n   long long i, j, t; n   cin >> t; n   while (t--) { n     long long r, c, k; n     long long rice = 0; n     char a[100][100]; n     cin >> r >> c >> k; n     for (i = 0; i < r; i++) { n       for (j = 0; j < c; j++) { n         cin >> a[i][j]; n         if (a[i][j] ==  R ) rice++; n       } n     } n     long long ans = rice / k; n     long long maxx = rice - k * ans; n     long long minn = k - maxx; n     char curranswer =  a ; n     long long currcount = 0; n     bool flag = true; n     for (i = 0; i < r; i++) { n       for (flag ? (j = 0) : (j = c - 1); flag ? (j < c) : j >= 0; n            flag ? j++ : j--) { n         bool local = false; n         if (a[i][j] ==  R ) local = true; n         a[i][j] = curranswer; n         if (local) { n           currcount++; n           if (maxx > 0) { n             if (currcount == ans + 1) { n               maxx--; n               currcount = 0; n               curranswer = change(curranswer); n             } n           } else if (minn > 0) { n             if (currcount == ans) { n               minn--; n               currcount = 0; n               if (minn) curranswer = change(curranswer); n             } n           } n         } n       } n       flag = !flag; n     } n     for (i = 0; i < r; i++) { n       for (j = 0; j < c; j++) cout << a[i][j]; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long x, long long y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n long long expo(long long n, long long m) { n   long long r = 1; n   while (m > 0) { n     if (m % 2) r = (r * n) % 1000000009; n     n = (n * n) % 1000000009; n     m = m / 2; n   } n   return r % 1000000009; n } n long long lx = 0, ly = 0; n bool check(long long x, long long y) {} n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long a, b; n   cin >> a >> b; n   string s; n   cin >> s; n   long long p = 0, q = 0; n   if (a == 0 && b == 0) { n     cout <<  Yes ; n     return 0; n   } n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  U ) { n       ly++; n     } else if (s[i] ==  D ) n       ly--; n     else if (s[i] ==  L ) n       lx--; n     else n       lx++; n     if (lx == a && ly == b) { n       cout <<  Yes ; n       return 0; n     } n   } n   if (lx == 0 && ly == 0) { n     cout <<  No ; n     return 0; n   } n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  U ) { n       q++; n     } else if (s[i] ==  D ) n       q--; n     else if (s[i] ==  L ) n       p--; n     else n       p++; n     long long x = a - p, y = b - q, X = lx, Y = ly; n     if (lx == 0) { n       if (ly > 0 && y > 0 && x == 0 && y % ly == 0) { n         cout <<  Yes ; n         return 0; n       } else if (ly < 0 && y < 0 && x == 0) { n         ly = -1 * ly, y = -1 * y; n         if (y % ly == 0) { n           cout <<  Yes ; n           return 0; n         } n       } n     } else if (ly == 0) { n       if (lx > 0 && x > 0 && y == 0 && x % lx == 0) { n         cout <<  Yes ; n         return 0; n       } else if (lx < 0 && x < 0 && y == 0) { n         lx = -1 * lx, x = -1 * x; n         if (x % lx == 0) { n           cout <<  Yes ; n           return 0; n         } n       } n     } else { n       if (lx > 0 && x > 0) { n         if (ly > 0 && y > 0) { n           if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { n             cout <<  Yes ; n             return 0; n           } n         } else if (ly < 0 && y < 0) { n           ly = -1 * ly, y = -1 * y; n           if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { n             cout <<  Yes ; n             return 0; n           } n         } n       } else if (lx < 0 && x < 0) { n         lx = -1 * lx, x = -1 * x; n         if (ly > 0 && y > 0) { n           if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { n             cout <<  Yes ; n             return 0; n           } n         } else if (ly < 0 && y < 0) { n           ly = -1 * ly, y = -1 * y; n           if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { n             cout <<  Yes ; n             return 0; n           } n         } n       } n     } n     lx = X, ly = Y; n   } n   cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(3e5), mod = int(1e9) + 7; n int n; n int a[N], c[N]; n long long t[N]; n long long x[N], pref[N]; n long double p[N]; n long long suf[N]; n void upd(int v, int val) { n   v += n - 1; n   t[v] += val; n   c[v]++; n   while (v > 1) { n     v >>= 1; n     t[v] = t[v + v] + t[v + v + 1]; n     c[v] = c[v + v] + c[v + v + 1]; n   } n } n long long get(int l, int r) { n   l += n - 1; n   r += n - 1; n   long long res = 0; n   while (l <= r) { n     if (l & 1) res += t[l]; n     if (!(r & 1)) res += t[r]; n     l = (l + 1) >> 1; n     r = (r - 1) >> 1; n   } n   return res; n } n int cnt(int l, int r) { n   l += n - 1; n   r += n - 1; n   int res = 0; n   while (l <= r) { n     if (l & 1) res += c[l]; n     if (!(r & 1)) res += c[r]; n     l = (l + 1) >> 1; n     r = (r - 1) >> 1; n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   long long all = 1ll * n * (n + 1) / 2; n   long double res = 0; n   for (int i = 1; i <= n; i++) { n     long long cur = get(1, a[i] - 1); n     long double p = (cur + 0.0) * (n - i + 1) / all; n     res += p * 0.5; n     cur = get(a[i] + 1, n); n     p = (cur + 0.0) * (n - i + 1) / all; n     res += cnt(a[i] + 1, n); n     res -= p * 0.5; n     upd(a[i], i); n   } n   printf( %.12lf , double(res)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   long long ans = 0; n   while (true) { n     long long x = max(a, b); n     long long y = min(a, b); n     if (x == 0 || y == 0) break; n     long long n = x / y; n     ans += n; n     x -= n * y; n     a = x; n     b = y; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int x, test; n   cin >> test; n   while (test--) { n     int n, c0, c1, h; n     string s; n     cin >> n >> c0 >> c1 >> h >> s; n     int cnt0 = 0, cnt1 = 0; n     for (auto &z : s) { n       cnt0 += z ==  0 ; n       cnt1 += z ==  1 ; n     } n     int cost = (cnt0 * c0) + (cnt1 * c1); n     int cost0 = (c0 * n) + (cnt1 * h); n     int cost1 = (c1 * n) + (cnt0 * h); n     cout << min({cost, cost0, cost1}) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = int(1e9) + 123; n int main() { n   int n, v; n   cin >> n >> v; n   vector<int> a; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   int sum = 0; n   for (int i = 0; i < a.size() - 1; i++) { n     sum += a[i]; n   } n   if (sum <= v) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string vow =  aeiou ; n long long month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n const long long dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2}; n const long long dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1}; n const long long dx4[] = {-1, 0, 1, 0}; n const long long dy4[] = {0, 1, 0, -1}; n const long long dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n const long long dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n const long double pie = 3.1415926535897932384626; n const long long special_prime = 982451653l; n const long long mod = 1e9 + 7; n const long long inf = 2e18; n template <class T> n istream &operator>>(istream &in, vector<T> &a) { n   for (auto &i : a) cin >> i; n   return in; n } n template <class T> n ostream &operator<<(ostream &out, const vector<T> &a) { n   for (auto &i : a) cout << i <<    ; n   return out; n } n template <class T, class U> n ostream &operator<<(ostream &out, const map<T, U> &a) { n   for (auto &i : a) cout <<  (  << i.first <<  ,   << i.second <<  ) n ; n   return out; n } n template <class T, class U> n ostream &operator<<(ostream &out, const unordered_map<T, U> &a) { n   for (auto &i : a) cout <<  (  << i.first <<  ,   << i.second <<  ) n ; n   return out; n } n long long GCD(long long a, long long b) { n   if (!b) return a; n   return GCD(b, a % b); n } n long long LCM(long long a, long long b) { return (a * b) / GCD(a, b); } n bool comp(long long a, long long b) { return a > b; } n void sieve(vector<long long> &prime) { n   prime[0] = false; n   prime[1] = false; n   for (long long p = 2; p * p <= 99999; p++) { n     if (prime[p] == true) { n       for (long long i = p * p; i <= 99999; i += p) prime[i] = false; n     } n   } n } n void solve() { n   long long n, g, b; n   cin >> n >> g >> b; n   long long d = n / 2 + n % 2; n   if (d % g) n     cout << max(n, (d / g) * (g + b) + d % g) <<   n ; n   else n     cout << max((d / g) * (g + b) - b, n) <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   cout << setprecision(15) << fixed; n   for (long long i = 1; i <= t; i++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = (int)1e9 + 7; n const int N = (int)1e6 + 6; n int f[30][N]; n int pref[30][N]; n int lp[N]; n void precalc() { n   pref[0][0] = f[0][0] = 1; n   for (int i = 1; i < 30; i++) { n     f[i][0] = 2; n     pref[i][0] = f[i][0] + pref[i - 1][0]; n     pref[i][0] -= pref[i][0] >= mod ? mod : 0; n   } n   for (int level = 1; level < N; level++) { n     pref[0][level] = f[0][level] = 1; n     for (int pw = 1; pw < 30; pw++) { n       f[pw][level] = pref[pw][level - 1]; n       pref[pw][level] = f[pw][level] + pref[pw - 1][level]; n       pref[pw][level] -= pref[pw][level] >= mod ? mod : 0; n     } n   } n   for (int i = 2; i < N; i++) { n     if (lp[i]) continue; n     for (int j = i; j < N; j += i) { n       lp[j] = lp[j] ? lp[j] : i; n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   precalc(); n   int T; n   scanf( %d , &T); n   while (T--) { n     int r, n; n     scanf( %d%d , &r, &n); n     if (n == 1) { n       printf( 1 n ); n       continue; n     } n     long long ans = 1; n     while (n > 1) { n       int p = lp[n]; n       int pw = 0; n       while (lp[n] == p) { n         n /= p; n         ++pw; n       } n       ans = ans * f[pw][r] % mod; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } n const int MAXN = 3e5 + 10; n const int INF = 0x3f3f3f3f; n const int MOD = 1e7 + 9; n int a[MAXN]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (1); i < (n + 1); ++i) { n     scanf( %d , &a[i]); n   } n   int ans1 = 0; n   int t1 = 1; n   for (int i = (n); i > (0); --i) { n     if (a[t1] != a[i]) { n       ans1 = i - t1; n       break; n     } else { n       t1++; n       i++; n     } n   } n   int ans2 = 0; n   int t2 = n; n   for (int i = (1); i < (n + 1); ++i) { n     if (a[t2] != a[i]) { n       ans2 = t2 - i; n       break; n     } else { n       t2--; n       i--; n     } n   } n   printf( %d n , max(ans1, ans2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, sum = 1; n   cin >> n; n   sum += 6 * ((n * (n + 1)) / 2); n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n const int M = 998244353; n int n, a[N], i, s; n long long f[N], c[N], x, y, z, ans[N], tp; n long long qpow(long long a, long long b) { n   long long s = a, ans = 1; n   while (b) { n     if (b & 1) ans = ans * s % M; n     s = s * s % M; n     b >>= 1; n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     s += a[i]; n   } n   long long is = qpow(s, M - 2), in = qpow(n - 1, M - 2); n   c[1] = 1 - n; n   f[0] = f[1] = 1; n   for (i = 1; i < s; ++i) { n     x = i * is % M, y = (s - i) * is % M * in % M; n     z = qpow(y, M - 2); n     f[i + 1] = (f[i] * (x + y) - f[i - 1] * x) % M * z % M; n     c[i + 1] = (c[i] * (x + y) - 1 - c[i - 1] * x) % M * z % M; n   } n   ans[0] = -c[s] * qpow(f[s], M - 2) % M; n   for (i = 1; i < s; ++i) ans[i] = (f[i] * ans[0] + c[i]) % M; n   for (i = 1; i <= n; ++i) tp += ans[a[i]]; n   tp -= ans[0] * (n - 1); n   cout << (tp % M * qpow(n, M - 2) % M + M) % M; n } n


#include <bits/stdc++.h> n using namespace std; n long long w[2], b[2], q[2], mod = 998244353, fac[100001]; n long long modpow(long long b, long long p) { n   long long ret = 1; n   while (p > 0) { n     if (p & 1) ret = ret * b % mod; n     b = b * b % mod; n     p >>= 1; n   } n   return ret; n } n long long choose(long long n, long long m) { n   long long denom = modpow(fac[m], mod - 2) * modpow(fac[n - m], mod - 2) % mod; n   return (fac[n] * denom % mod); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, qq = 0; n   bool bw = true, wb = true, both = true; n   string s; n   fac[0] = 1; n   for (int i = 1; i < 100001; i++) fac[i] = fac[i - 1] * i % mod; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s; n     for (int j = 0; j < 2; j++) { n       if (s[j] ==  W ) w[j]++; n       if (s[j] ==  B ) b[j]++; n       if (s[j] ==  ? ) q[j]++; n     } n     if (s ==  ?? ) qq++; n     if (s ==  WW  || s ==  BB ) bw = wb = both = false; n     if (s ==  W?  || s ==  ?B  || s ==  WB ) bw = false; n     if (s ==  B?  || s ==  ?W  || s ==  BW ) wb = false; n   } n   if (n == 1) { n     if (s ==  WW  | s ==  BB ) n       cout << 0; n     else if (s ==  ?? ) n       cout << 2; n     else n       cout << 1; n     return 0; n   } n   long long ans = 0; n   for (int i = 0; i < q[0] + 1; i++) { n     if (w[0] + i < b[1] || w[0] + i > b[1] + q[1]) continue; n     ans = (ans + choose(q[0], i) * choose(q[1], w[0] + i - b[1])) % mod; n   } n   if (both) { n     ans -= modpow(2, qq); n     if (bw) ans++; n     if (wb) ans++; n   } n   ans = ((ans % mod) + mod) % mod; n   cout << ans; n } n


#include <bits/stdc++.h> n using std::abs; n using std::bitset; n using std::cerr; n using std::cin; n using std::copy; n using std::cout; n using std::endl; n using std::fill; n using std::fixed; n using std::greater; n using std::lower_bound; n using std::map; n using std::max; n using std::min; n using std::next_permutation; n using std::pair; n using std::priority_queue; n using std::queue; n using std::reverse; n using std::rotate; n using std::set; n using std::sort; n using std::string; n using std::swap; n using std::unique; n using std::unordered_map; n using std::unordered_set; n using std::upper_bound; n using std::vector; n int const INF = (int)1e9; n long long const INFL = (long long)1e18; n long double const PI = 3.1415926535897932384626433832795028; n bool isPrime(int x) { n   for (int a = 2; a * a <= x; ++a) { n     if (x % a == 0) { n       return false; n     } n   } n   return true; n } n int const N = 100100; n int len[N]; n int g[N], next[N]; n int find(int v) { n   if (next[v] != v) { n     int root = find(next[v]); n     len[v] += len[next[v]]; n     next[v] = root; n   } n   return next[v]; n } n int v[N], res[N]; n bool did[N]; n int calc(int a, int b) { n   int ret = 0; n   find(a); n   b -= len[a]; n   a = next[a]; n   while (b > 0 && !did[a]) { n     ++ret; n     did[a] = true; n     next[a] = find(g[a]); n     len[a] = 1 + len[g[a]]; n     b -= len[a]; n     a = find(g[a]); n   } n   return ret; n } n void solve() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     next[i] = i; n     len[i] = 0; n     cin >> g[i]; n     --g[i]; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> v[i]; n   } n   for (int i = 1; i <= m; ++i) { n     int b; n     cin >> b; n     int a = (v[i] + res[i - 1] - 1) % n; n     res[i] = calc(a, b); n   } n   for (int i = 1; i <= m; ++i) { n     cout << res[i] <<   n ; n   } n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n bool vis[305][305]; n string s[305]; n int len[305][305]; n int d[305]; n int hs[305][305]; n int n; n const int mod = 9e8 + 7; n int qpow(int x, int y) { n   long long ret = 1; n   long long now = x; n   while (y) { n     if (y & 1) ret = ret * now % mod; n     now = now * now % mod; n     y >>= 1; n   } n   return ret; n } n void init() { n   memset(vis, false, sizeof(vis)); n   for (int i = 1; i <= n; i++) { n     d[i] = 0; n     len[i][0] = s[i].size(); n     for (int j = 0; j < s[i].size(); j++) { n       d[i] = (d[i] * 233LL + s[i][j] -  a  + 1) % mod; n     } n   } n   for (int i = 1; i <= n; i++) { n     hs[i][0] = d[i]; n     for (int j = 1; j + i <= n; j++) { n       len[i][j] = len[i + j][0] + len[i][j - 1]; n       hs[i][j] = (1LL * hs[i][j - 1] * 2333LL + d[i + j]) % mod; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) cin >> s[i]; n   init(); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     int ls = 0; n     for (int j = 0; i + j <= n; j++) { n       ls += len[i + j][0]; n       int nx = i + j + 1; n       int cnt = 1; n       while (nx + j <= n) { n         if (hs[i][j] == hs[nx][j] && len[i][j] == len[nx][j]) { n           cnt++; n           nx += j + 1; n         } else { n           nx++; n         } n       } n       if (cnt > 1) ans = max(ans, (ls - 1) * cnt); n     } n   } n   ans = n - 1 - ans; n   for (int i = 1; i <= n; i++) ans += len[i][0]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[100005]; n int p[100005]; n int h[100005]; n int happy[100005]; n int sad[100005]; n bool vis[100005]; n bool ok; n void init(int n) { n   for (int i = 1; i < n + 1; i++) { n     v[i].clear(); n     vis[i] = false; n   } n   ok = true; n } n void dfs(int node) { n   vis[node] = true; n   long long int happyChild = 0; n   long long int sadChild = p[node]; n   for (int child : v[node]) { n     if (vis[child]) continue; n     dfs(child); n     happyChild += happy[child]; n     sadChild += sad[child]; n   } n   long long int gg = (long long int)h[node] - (happyChild - sadChild); n   if (gg >= 0 && gg % 2 == 0 && gg / 2 <= sadChild) { n     long long int x = gg / 2; n     happy[node] = x + happyChild; n     sad[node] = sadChild - x; n   } else { n     ok = false; n     return; n   } n } n int main() { n   int t, n, m, a, b; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d , &n, &m); n     init(n); n     for (int i = 1; i < n + 1; i++) scanf( %d , &p[i]); n     for (int i = 1; i < n + 1; i++) scanf( %d , &h[i]); n     for (int i = 0; i < n - 1; i++) { n       scanf( %d %d , &a, &b); n       v[a].push_back(b); n       v[b].push_back(a); n     } n     dfs(1); n     if (ok) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long j, k, a, b, s, ss, l, m, n, t, d; n long long e, i, c[221]; n vector<long long> p[111], q[111], r, v; n char w; n map<long long, long long> y, z; n long long too(long long i, long long j) { n   long long l = 0, d = 0; n   for (k = 0; k < q[i].size(); k++) { n     l += q[i][k]; n     r.push_back(l); n   } n   l = 0; n   for (k = 0; k < q[j].size(); k++) { n     l += q[j][k]; n     r.push_back(l); n   } n   l = r.size(); n   sort(r.begin(), r.end()); n   for (k = 0; k < l; k++) c[k] = 0; n   for (k = 0; k < l; k++) { n     long long x, a = 0, b = 0, e = 0; n     x = r[k]; n     while (x > q[i][e] && e < q[i].size()) { n       a += p[i][e] * q[i][e]; n       x -= q[i][e]; n       e++; n     } n     if (e < q[i].size()) a += p[i][e] * x; n     x = r[k]; n     e = 0; n     while (x > q[j][e] && e < q[j].size()) { n       b += p[j][e] * q[j][e]; n       x -= q[j][e]; n       e++; n     } n     if (e < q[j].size()) b += p[j][e] * x; n     if (a > b) c[k] = 1; n     if (a < b) c[k] = 2; n   } n   e = 0; n   while (c[e] == 0) e++; n   for (k = e + 1; k < l; k++) { n     if (c[k] == 0) c[k] = c[k - 1]; n   } n   for (k = e + 1; k < l; k++) n     if (c[k] != c[k - 1]) d++; n   return d; n } n int main() { n   cin >> n >> s; n   for (i = 0; i < n; i++) { n     cin >> k; n     for (j = 0; j < k; j++) { n       cin >> a >> b; n       p[i].push_back(a); n       q[i].push_back(b); n     } n   } n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       ss += too(i, j); n       r = v; n     } n   } n   cout << ss; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(1e6) + 50; n char S[N], T[N]; n int slen, tlen, lef[N], rig[N], cnt[255][255]; n inline int getID(char c) { n   if (c ==  R ) return 0; n   if (c ==  G ) return 1; n   if (c ==  B ) return 2; n   throw; n } n int main() { n   scanf( %s , S + 1); n   slen = strlen(S + 1); n   scanf( %s , T + 1); n   tlen = strlen(T + 1); n   int l = 1, r = 1; n   while (l <= slen && r <= tlen) { n     lef[l] = r; n     r += S[l] == T[r]; n     ++l; n   } n   slen = min(slen, l - 1); n   l = r = 1; n   while (l <= slen && r <= tlen) { n     rig[l] = r; n     l += S[l] == T[r]; n     ++r; n   } n   tlen = min(tlen, r - 1); n   for (int i = l + bool(rig[l]); i <= slen; ++i) { n     rig[i] = tlen; n   } n   long long res = 0; n   for (int i = 1; i <= slen; ++i) { n     for (int j = max(1, lef[i - 1]); j < lef[i]; ++j) --cnt[T[j]][T[j - 1]]; n     for (int j = 1 + rig[i - 1]; j <= rig[i]; ++j) ++cnt[T[j]][T[j - 1]]; n     res += rig[i] - lef[i] + 1; n     if (S[i - 1] != S[i]) res -= cnt[S[i - 1]][S[i]]; n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n long long ans; n int main() { n   cin >> n >> m >> k; n   if (k > n + m - 2) { n     cout <<  -1  << endl; n     return 0; n   } n   if (n >= m) swap(n, m); n   if (k <= m - 1) { n     ans = (long long)(m / (k + 1)) * n; n   } else { n     ans = (long long)(n / (k - m + 2)); n   } n   if (k <= n - 1) { n     ans = max(ans, (long long)(n / (k + 1)) * m); n   } else { n     ans = max(ans, (long long)(m / (k - n + 2))); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 110; n int n, m, k; n int h[MAXN]; n void load() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < n; i++) scanf( %d , h + i); n } n bool solve() { n   long long sum = m; n   int ind = 0; n   while (ind < n - 1) { n     if (h[ind] >= h[ind + 1] - k) { n       int nxt = max(h[ind + 1] - k, 0); n       sum += h[ind] - nxt; n     } else { n       if (sum + h[ind] < h[ind + 1] - k) return false; n       sum -= h[ind + 1] - k - h[ind]; n     } n     ind++; n   } n   return true; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     load(); n     if (solve()) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 10; n int n, k; n int a[MAXN], f[MAXN]; n int bsL(int l, int r, int val) { n   int pos = -1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (a[mid] >= val) { n       pos = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   return pos; n } n bool check(int x) { n   f[1] = 1; n   for (int i = 1, l = 1; i <= n; i++) { n     while (a[i] - a[l] > x) l++; n     f[i + 1] = (i >= k && f[i - k + 1] - f[l - 1] > 0); n     f[i + 1] += f[i]; n   } n   return f[n + 1] - f[n] > 0; n } n int bs(int l, int r) { n   int pos = 0; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) { n       pos = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   return pos; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   cout << bs(0, 1e9); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 10; n long long h[N], e[2 * N], ne[2 * N], idx; n void add(long long a, long long b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; } n struct node { n   long long sum, add; n }; n node tr[N << 2]; n long long arr[N]; n long long f[N][20]; n long long dep[N], fa[N], son[N], sz[N]; n long long n, q; n void dfs1(long long u, long long fz) { n   fa[u] = fz; n   dep[u] = dep[fz] + 1; n   sz[u] = 1; n   f[u][0] = fz; n   for (long long i = 1; i < 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1]; n   for (long long i = h[u]; i != -1; i = ne[i]) { n     long long j = e[i]; n     if (j == fz) continue; n     dfs1(j, u); n     sz[u] += sz[j]; n     if (sz[j] > sz[son[u]]) { n       son[u] = j; n     } n   } n } n long long id[N], top[N], uid[N], tot; n void dfs2(long long u, long long topx) { n   top[u] = topx; n   id[u] = ++tot; n   uid[tot] = u; n   if (!son[u]) return; n   dfs2(son[u], topx); n   for (long long i = h[u]; i != -1; i = ne[i]) { n     long long j = e[i]; n     if (j == fa[u] || j == son[u]) continue; n     dfs2(j, j); n   } n } n long long get_lca(long long a, long long b) { n   while (top[a] != top[b]) { n     if (dep[top[a]] > dep[top[b]]) n       a = fa[top[a]]; n     else n       b = fa[top[b]]; n   } n   return dep[a] > dep[b] ? b : a; n } n long long topC(long long x, long long c) { n   for (long long i = 19; i >= 0; i--) { n     if ((1 << i) <= c) { n       x = f[x][i]; n       c -= (1 << i); n     } n   } n   return x; n } n long long root; n void pushup(long long u) { tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum; } n void pushdown(long long l, long long r, long long u) { n   if (tr[u].add) { n     long long mid = l + r >> 1; n     tr[u << 1].sum += (mid - l + 1) * tr[u].add; n     tr[u << 1 | 1].sum += (r - mid) * tr[u].add; n     tr[u << 1].add += tr[u].add; n     tr[u << 1 | 1].add += tr[u].add; n     tr[u].add = 0; n   } n } n long long query(long long l, long long r, long long a, long long b, n                 long long u) { n   if (l <= a && b <= r) { n     return tr[u].sum; n   } n   pushdown(a, b, u); n   long long mid = a + b >> 1; n   long long ans = 0; n   if (l <= mid) ans += query(l, r, a, mid, u << 1); n   if (r > mid) ans += query(l, r, mid + 1, b, u << 1 | 1); n   return ans; n } n void ud(long long l, long long r, long long a, long long b, long long d, n         long long u) { n   if (l <= a && b <= r) { n     tr[u].add += d; n     tr[u].sum += (b - a + 1) * d; n     return; n   } n   pushdown(a, b, u); n   long long mid = a + b >> 1; n   if (l <= mid) ud(l, r, a, mid, d, u << 1); n   if (r > mid) ud(l, r, mid + 1, b, d, u << 1 | 1); n   pushup(u); n } n void update(long long u, long long x) { n   ud(id[u], id[u] + sz[u] - 1, 1, n, x, 1); n } n long long qry(long long u) { return query(id[u], id[u] + sz[u] - 1, 1, n, 1); } n void build(long long l, long long r, long long u) { n   tr[u].add = 0; n   if (l == r) { n     tr[u].sum = arr[uid[l]]; n     return; n   } n   long long mid = l + r >> 1; n   build(l, mid, u << 1); n   build(mid + 1, r, u << 1 | 1); n   pushup(u); n } n int main() { n   ios::sync_with_stdio(false); n   std::cin.tie(0); n   memset(h, -1, sizeof h); n   cin >> n >> q; n   for (long long i = 1; i <= n; ++i) cin >> arr[i]; n   for (long long i = 1; i < n; ++i) { n     long long a, b; n     cin >> a >> b; n     add(a, b); n     add(b, a); n   } n   dfs1(1, 1); n   dfs2(1, 1); n   build(1, n, 1); n   root = 1; n   while (q--) { n     long long t; n     cin >> t; n     long long v, u, x; n     if (t == 1) { n       cin >> v; n       root = v; n     } else if (t == 2) { n       cin >> u >> v >> x; n       long long t1 = get_lca(u, root); n       long long t2 = get_lca(v, root); n       long long t3 = get_lca(u, v); n       long long ans = t1; n       if (dep[ans] < dep[t2]) ans = t2; n       if (dep[ans] < dep[t3]) ans = t3; n       long long lca = get_lca(root, ans); n       if (ans == root) { n         update(1, x); n       } else if (lca != ans) { n         update(ans, x); n       } else { n         update(1, x); n         long long s = topC(root, dep[root] - dep[ans] - 1); n         update(s, -x); n       } n     } else { n       cin >> v; n       long long ans = v; n       long long lca = get_lca(root, ans); n       if (ans == root) { n         cout << qry(1) << endl; n       } else if (lca != ans) { n         cout << qry(ans) << endl; n       } else { n         long long s = topC(root, dep[root] - dep[ans] - 1); n         cout << (qry(1) - qry(s)) << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const double EPS = 1e-9; n const int INF = 1 << 29; n int const NN = 5e3 + 3; n int const MX = 1e9 + 9; n int a[505][505]; n int mx[505][505]; n int main() { n   int n, m, K, q; n   cin >> n >> m >> K >> q; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) a[i][j] = MX; n   } n   while (q--) { n     int x, y, t; n     cin >> x >> y >> t; n     a[x][y] = t; n   } n   int ans = MX; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       mx[i][j] = a[i][j]; n       for (int k = 2; i + k - 1 <= n && k <= K; ++k) { n         mx[i][j] = max(mx[i][j], a[i + k - 1][j]); n       } n     } n   } n   for (int i = 1; i + K - 1 <= n; ++i) { n     for (int j = 1; j + K - 1 <= m; ++j) { n       int mxx = -1; n       for (int jj = j; jj <= j + K - 1; ++jj) { n         mxx = max(mxx, mx[i][jj]); n       } n       ans = min(ans, mxx); n     } n   } n   if (ans < MX) { n     printf( %d , ans); n     ; n     putchar(  n ); n   } else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n  n void solve() { n     int n, k; n     cin >> n >> k; n     if(2 * k + 1 > n) { n         cout <<  -1 n ; n         return; n     } n     vector<int> v(n); n     int p = n; n     for(int i = 1; i < 1 + 2 * k; i += 2) n         v[i] = p--; n     p = 1; n     for(int i = 0; i < n; i++) { n         if(v[i]) n             continue; n         v[i] = p++; n     } n     for(auto &x : v) n         cout << x <<    ; n     cout <<   n ; n } n  n int main() { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n  n     int t; n     cin >> t; n     while(t--) n         solve();     n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n char S[maxn][10]; n int ju(char a[], char b[]) { n   int n = strlen(a), m = strlen(b); n   int x = 0, y = 0; n   for (int i = 0; i < n; i++) x = x * 10 + a[i] -  0 ; n   for (int j = 0; j < m; j++) y = y * 10 + (b[j] ==  ?  ? 9 : b[j] -  0 ); n   return x < y; n } n int f(int ind) { n   int m = strlen(S[ind - 1]), n = strlen(S[ind]); n   if (m < n) { n     for (int i = 0; i < n; i++) n       if (S[ind][i] ==  ? ) { n         if (i == 0) n           S[ind][i] =  1 ; n         else n           S[ind][i] =  0 ; n       } n   } else if (m > n) { n     return 1; n   } else { n     for (int i = 0; i < n; i++) { n       if (S[ind][i] ==  ? ) n         for (int j = 0; j <= 9; j++) { n           S[ind][i] =  0  + j; n           if (ju(S[ind - 1], S[ind])) break; n         } n     } n     if (!ju(S[ind - 1], S[ind])) return 1; n   } n   return 0; n } n int main() { n   int n; n   while (scanf( %d , &n) > 0) { n     int si = 1; n     S[0][0] =  0 ; n     S[0][1] =   0 ; n     for (int i = 1; i <= n; i++) { n       scanf( %s , S[i]); n       if (si && f(i)) si = 0; n     } n     if (si) { n       printf( YES n ); n       for (int i = 1; i <= n; i++) printf( %s n , S[i]); n     } else { n       printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Friend { n   string name; n   int x, y, z; n } f[123]; n bool Isde(char *a) { n   if (a[0] <= a[1]) return false; n   if (a[1] <= a[3]) return false; n   if (a[3] <= a[4]) return false; n   if (a[4] <= a[6]) return false; n   if (a[6] <= a[7]) return false; n   return true; n } n bool Isiden(char *a) { n   if (a[0] != a[1]) return false; n   if (a[1] != a[3]) return false; n   if (a[3] != a[4]) return false; n   if (a[4] != a[6]) return false; n   if (a[6] != a[7]) return false; n   return true; n } n int main() { n   int n; n   while (cin >> n) { n     int mx = 0, my = 0, mz = 0; n     for (int i = 0; i < n; ++i) { n       int m; n       cin >> m >> f[i].name; n       f[i].x = f[i].y = f[i].z; n       char str[100]; n       for (int j = 0; j < m; ++j) { n         cin >> str; n         if (Isiden(str)) n           f[i].x++; n         else if (Isde(str)) n           f[i].y++; n         else n           f[i].z++; n       } n       mx = max(f[i].x, mx); n       my = max(f[i].y, my); n       mz = max(f[i].z, mz); n     } n     bool flag = false; n     cout <<  If you want to call a taxi, you should call:  ; n     for (int i = 0; i < n; ++i) { n       if (f[i].x == mx) { n         if (flag) cout <<  ,  ; n         cout << f[i].name; n         flag = true; n       } n     } n     cout <<  .  << endl; n     flag = false; n     cout <<  If you want to order a pizza, you should call:  ; n     for (int i = 0; i < n; ++i) { n       if (f[i].y == my) { n         if (flag) cout <<  ,  ; n         cout << f[i].name; n         flag = true; n       } n     } n     cout <<  .  << endl; n     flag = false; n     cout <<  If you want to go to a cafe with a wonderful girl, you should   n              call:  ; n     for (int i = 0; i < n; ++i) { n       if (f[i].z == mz) { n         if (flag) cout <<  ,  ; n         cout << f[i].name; n         flag = true; n       } n     } n     cout <<  .  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 0, R = 10000; n int n, vote, tot; n vector<int> p[110000]; n pair<int, int> x[110000]; n long long sum[110000]; n long long he[4 * 110000]; n int num[4 * 110000]; n long long ask(int root, int l, int r, int k) { n   if (k <= 0) n     return 0; n   else if (num[root] == k) n     return he[root]; n   else if (l == r) n     return (long long)k * l; n   else { n     int mid = (l + r) >> 1; n     if (num[2 * root] < k) { n       return ask(2 * root, l, mid, num[2 * root]) + n              ask(2 * root + 1, mid + 1, r, k - num[2 * root]); n     } else { n       return ask(2 * root, l, mid, k); n     } n   } n } n void add(int root, int l, int r, int k) { n   if (l > r || k < l || k > r) n     return; n   else if (l == r) { n     num[root]++; n     he[root] += k; n   } else { n     num[root]++; n     he[root] += k; n     int mid = (l + r) >> 1; n     add(2 * root, l, mid, k); n     add(2 * root + 1, mid + 1, r, k); n   } n } n int main() { n   cin >> n; n   int mx = 0; n   for (int i = 0, a, b; i < n; i++) { n     scanf( %d%d , &a, &b); n     if (a != 0) n       p[a].push_back(b); n     else n       vote++; n   } n   for (int i = 1; i < 110000; i++) { n     sort(p[i].begin(), p[i].end()); n     reverse(p[i].begin(), p[i].end()); n     mx = max(mx, (int)p[i].size()); n   } n   for (int i = 1; i < 110000; i++) { n     int m = (int)p[i].size(); n     for (int j = 0; j < m; j++) { n       x[tot++] = pair<int, int>(j + 1, p[i][j]); n     } n   } n   sort(x, x + tot); n   for (int i = 0; i < tot; i++) { n     if (i == 0) n       sum[i] = x[i].second; n     else n       sum[i] = sum[i - 1] + x[i].second; n   } n   long long ans = 1e10; n   int pre = 0; n   for (int i = 0; i <= mx + 1; i++) { n     int indx = upper_bound(x, x + tot, pair<int, int>(i, 1e9)) - x; n     long long cans = sum[tot - 1] - sum[indx] + x[indx].second; n     if (indx == tot) cans = 0; n     while (pre < indx) { n       add(1, L, R, x[pre].second); n       pre++; n     } n     indx = tot - indx + vote; n     if (i >= indx) { n       indx = i - indx + 1; n       cans += ask(1, L, R, indx); n     } n     ans = min(ans, cans); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n long long inv(long long a) { n   long long u = 0, v = 1, b = mod; n   while (a) { n     long long t = b / a; n     b -= t * a, u -= t * v; n     swap(a, b); n     swap(u, v); n   } n   u %= mod; n   if (u < 0) u += mod; n   return u; n } n struct mint { n   int x; n   mint() : x(0) {} n   mint(int x) : x(x) {} n   mint(long long x) : x() { n     if (x >= mod || x < -mod) x %= mod; n     if (x < 0) x += mod; n     this->x = x; n   } n   mint &operator-=(const mint &rhs) { n     x -= rhs.x; n     if (x < 0) x += mod; n     return *this; n   } n   mint &operator+=(const mint &rhs) { n     x += rhs.x; n     if (x >= mod) x -= mod; n     return *this; n   } n   mint &operator*=(const mint &rhs) { n     x = (long long)x * rhs.x % mod; n     return *this; n   } n   mint &operator/=(const mint &rhs) { n     x = (long long)x * inv(rhs.x) % mod; n     return *this; n   } n   bool operator<(const mint &rhs) { return x < rhs.x; } n   bool operator==(const mint &rhs) { return x == rhs.x; } n   bool operator!=(const mint &rhs) { return x != rhs.x; } n   friend mint operator+(const mint &lhs, const mint &rhs) { n     mint ret(lhs); n     return ret += rhs; n   } n   friend mint operator-(const mint &lhs, const mint &rhs) { n     mint ret(lhs); n     return ret -= rhs; n   } n   friend mint operator*(const mint &lhs, const mint &rhs) { n     mint ret(lhs); n     return ret *= rhs; n   } n   friend mint operator/(const mint &lhs, const mint &rhs) { n     mint ret(lhs); n     return ret /= rhs; n   } n   friend ostream &operator<<(ostream &os, const mint &rhs) { n     os << rhs.x; n     return os; n   } n   friend istream &operator>>(istream &is, mint &rhs) { n     long long a; n     is >> a; n     rhs = a; n     return is; n   } n }; n mint fpow(mint a, long long b) { n   mint r = 1; n   for (; b; b >>= 1) { n     if (b & 1) r = r * a; n     a = a * a; n   } n   return r; n } n const int N = 1e6 + 5; n mint pw[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   pw[0] = 1; n   for (int i = 1; i <= max(n, m); ++i) pw[i] = pw[i - 1] * 2; n   int f0 = 1, f1 = 1, cntx = n, cnty = m, fx = 0, fy = 0; n   vector<int> cnt(2); n   vector<vector<int>> cntr(n, vector<int>(2, 0)), cntc(m, vector<int>(2, 0)); n   map<pair<int, int>, int> mp; n   auto Erase = [&](int x, int y) { n     if (!mp.count({x, y})) return; n     int p = mp[{x, y}]; n     mp.erase({x, y}); n     if (--cntr[x][p ^ y & 1] == 0) { n       if (cntr[x][p ^ y & 1 ^ 1]) n         fx--; n       else n         cntx += 1; n     } n     if (--cntc[y][p ^ x & 1] == 0) { n       if (cntc[y][p ^ x & 1 ^ 1]) n         fy--; n       else n         cnty += 1; n     } n     --cnt[x & 1 ^ y & 1 ^ p]; n   }; n   auto Insert = [&](int x, int y, int p) { n     mp[{x, y}] = p; n     if (cntr[x][p ^ y & 1]++ == 0) { n       if (cntr[x][p ^ y & 1 ^ 1]) n         fx++; n       else n         cntx -= 1; n     } n     if (cntc[y][p ^ x & 1]++ == 0) { n       if (cntc[y][p ^ x & 1 ^ 1]) n         fy++; n       else n         cnty -= 1; n     } n     ++cnt[x & 1 ^ y & 1 ^ p]; n   }; n   while (k--) { n     int x, y, op; n     cin >> x >> y >> op; n     --x, --y; n     Erase(x, y); n     if (op != -1) Insert(x, y, op); n     mint ans = -(cnt[0] == 0) - (cnt[1] == 0); n     if (fx == 0) ans += pw[cntx]; n     if (fy == 0) ans += pw[cnty]; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, a, p, b, p1, p2; n   cin >> a; n   b = a; n   p = 0; n   p1 = p2 = 1; n   for (i = 2; i <= sqrt(b); i++) { n     if (b % i == 0) { n       b = b / i; n       p++; n       if (p == 1) p1 = i; n       if (p == 2) p2 = i; n       i--; n     } n   } n   if (p == 0) cout << 1 << endl << 0 << endl; n   if (p == 1) cout << 2 << endl; n   if (p >= 2) cout << 1 << endl << p1 * p2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class P, class Q> n ostream& operator<<(ostream& stream, pair<P, Q> v) { n   stream <<  (  << v.first <<  ,  << v.second <<  ) ; n   return stream; n } n template <class T> n ostream& operator<<(ostream& stream, const vector<T> v) { n   stream <<  [  ; n   for (int i = 0; i < (int)v.size(); i++) stream << v[i] <<    ; n   stream <<  ] ; n   return stream; n } n template <class T> n T smin(T& a, T b) { n   if (a > b) a = b; n   return a; n } n template <class T> n T smax(T& a, T b) { n   if (a < b) a = b; n   return a; n } n const long long M = 10004205361450474; n int ask(vector<long long> s) { n   int sz = s.size(); n   cout << sz <<    ; n   for (int i = 0; i < sz; ++i) cout << s[i] <<    n [i == sz - 1]; n   fflush(stdout); n   int v = 0; n   cin >> v; n   if (v == -1) { n     exit(0); n   } n   return v; n } n const int N = 1e4 + 5; n long long dp[N][6]; n long long f(int A, int B) { n   if (!B) return 0; n   if (dp[A][B] != -1) return dp[A][B]; n   long long& ans = dp[A][B]; n   ans = 0; n   const int W = A; n   for (int i = 0; i < W; ++i) { n     ans += f(A, B - 1) + 1; n     A = min(10000ll, A + f(A, B - 1) + 1); n   } n   ans += f(A, B - 1); n   return ans; n } n long long g(long long a, int b) { return f(min(10000ll, a), b); } n int main(void) { n   memset(dp, -1, sizeof(dp)); n   const long long c1 = M - f(10000, 4); n   const long long c2 = 2046; n   const long long c3 = 6; n   long long l, r; n   int used = 5; n   --used; n   if (ask(vector<long long>{c1}) > 0) { n     l = c1 + 1; n     r = M; n     goto shit; n   } n   --used; n   if (ask(vector<long long>{c2}) > 0) { n     l = c2 + 1; n     r = c1 - 1; n     goto shit; n   } n   --used; n   if (ask(vector<long long>{c3}) > 0) { n     l = c3 + 1; n     r = c2 - 1; n   } else { n     l = 1; n     r = c3 - 1; n   } n shit: n   for (int cs = used - 1; cs >= 0; --cs) { n     vector<long long> s; n     for (long long i = l; i + g(i, cs) <= r && s.size() < min(10000ll, l); n          i += g(i, cs) + 1) n       s.push_back(i + g(i, cs)); n     int v = ask(s); n     if (!v) { n       smin(r, s[0] - 1); n     } else if (v == s.size()) { n       smax(l, s.back() + 1); n     } else { n       smin(r, s[v] - 1); n       smax(l, s[v - 1] + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T inverse(T a, T m) { n   T u = 0, v = 1; n   while (a != 0) { n     T t = m / a; n     m -= t * a; n     swap(a, m); n     u -= t * v; n     swap(u, v); n   } n   assert(m == 1); n   return u; n } n template <typename T> n class Modular { n  public: n   using Type = typename decay<decltype(T::value)>::type; n   constexpr Modular() : value() {} n   template <typename U> n   Modular(const U& x) { n     value = normalize(x); n   } n   template <typename U> n   static Type normalize(const U& x) { n     Type v; n     if (-mod() <= x && x < mod()) n       v = static_cast<Type>(x); n     else n       v = static_cast<Type>(x % mod()); n     if (v < 0) v += mod(); n     return v; n   } n   const Type& operator()() const { return value; } n   template <typename U> n   explicit operator U() const { n     return static_cast<U>(value); n   } n   constexpr static Type mod() { return T::value; } n   Modular& operator+=(const Modular& other) { n     if ((value += other.value) >= mod()) value -= mod(); n     return *this; n   } n   Modular& operator-=(const Modular& other) { n     if ((value -= other.value) < 0) value += mod(); n     return *this; n   } n   template <typename U> n   Modular& operator+=(const U& other) { n     return *this += Modular(other); n   } n   template <typename U> n   Modular& operator-=(const U& other) { n     return *this -= Modular(other); n   } n   Modular& operator++() { return *this += 1; } n   Modular& operator--() { return *this -= 1; } n   Modular operator++(int) { n     Modular result(*this); n     *this += 1; n     return result; n   } n   Modular operator--(int) { n     Modular result(*this); n     *this -= 1; n     return result; n   } n   Modular operator-() const { return Modular(-value); } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(static_cast<int64_t>(value) * n                       static_cast<int64_t>(rhs.value)); n     return *this; n   } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, long long>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     long long q = static_cast<long long>(static_cast<long double>(value) * n                                          rhs.value / mod()); n     value = normalize(value * rhs.value - q * mod()); n     return *this; n   } n   template <typename U = T> n   typename enable_if<!is_integral<typename Modular<U>::Type>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(value * rhs.value); n     return *this; n   } n   Modular& operator/=(const Modular& other) { n     return *this *= Modular(inverse(other.value, mod())); n   } n   friend const Type& abs(const Modular& x) { return x.value; } n   template <typename U> n   friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename V, typename U> n   friend V& operator>>(V& stream, Modular<U>& number); n  n  private: n   Type value; n }; n template <typename T> n bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value == rhs.value; n } n template <typename T, typename U> n bool operator==(const Modular<T>& lhs, U rhs) { n   return lhs == Modular<T>(rhs); n } n template <typename T, typename U> n bool operator==(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) == rhs; n } n template <typename T> n bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(const Modular<T>& lhs, U rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(U lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T> n bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value < rhs.value; n } n template <typename T> n Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T> n Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T> n Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T> n Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> power(const Modular<T>& a, const U& b) { n   assert(b >= 0); n   Modular<T> x = a, res = 1; n   U p = b; n   while (p > 0) { n     if (p & 1) res *= x; n     x *= x; n     p >>= 1; n   } n   return res; n } n template <typename T> n bool IsZero(const Modular<T>& number) { n   return number() == 0; n } n template <typename T> n string to_string(const Modular<T>& number) { n   return to_string(number()); n } n template <typename U, typename T> n U& operator<<(U& stream, const Modular<T>& number) { n   return stream << number(); n } n template <typename U, typename T> n U& operator>>(U& stream, Modular<T>& number) { n   typename common_type<typename Modular<T>::Type, long long>::type x; n   stream >> x; n   number.value = Modular<T>::normalize(x); n   return stream; n } n constexpr int md = 998244353; n using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; n vector<Mint> fact(1, 1); n vector<Mint> inv_fact(1, 1); n Mint C(int n, int k) { n   if (k < 0 || k > n) { n     return 0; n   } n   while ((int)fact.size() < n + 1) { n     fact.push_back(fact.back() * (int)fact.size()); n     inv_fact.push_back(1 / fact.back()); n   } n   return fact[n] * inv_fact[k] * inv_fact[n - k]; n } n template <typename T> n class NTT { n  public: n   using Type = typename decay<decltype(T::value)>::type; n   static Type md; n   static Modular<T> root; n   static int base; n   static int max_base; n   static vector<Modular<T>> roots; n   static vector<int> rev; n   static void clear() { n     root = 0; n     base = 0; n     max_base = 0; n     roots.clear(); n     rev.clear(); n   } n   static void init() { n     md = T::value; n     assert(md >= 3 && md % 2 == 1); n     auto tmp = md - 1; n     max_base = 0; n     while (tmp % 2 == 0) { n       tmp /= 2; n       max_base++; n     } n     root = 2; n     while (power(root, (md - 1) >> 1) == 1) { n       root++; n     } n     assert(power(root, md - 1) == 1); n     root = power(root, (md - 1) >> max_base); n     base = 1; n     rev = {0, 1}; n     roots = {0, 1}; n   } n   static void ensure_base(int nbase) { n     if (md != T::value) { n       clear(); n     } n     if (roots.empty()) { n       init(); n     } n     if (nbase <= base) { n       return; n     } n     assert(nbase <= max_base); n     rev.resize(1 << nbase); n     for (int i = 0; i < (1 << nbase); i++) { n       rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1)); n     } n     roots.resize(1 << nbase); n     while (base < nbase) { n       Modular<T> z = power(root, 1 << (max_base - 1 - base)); n       for (int i = 1 << (base - 1); i < (1 << base); i++) { n         roots[i << 1] = roots[i]; n         roots[(i << 1) + 1] = roots[i] * z; n       } n       base++; n     } n   } n   static void fft(vector<Modular<T>>& a) { n     int n = (int)a.size(); n     assert((n & (n - 1)) == 0); n     int zeros = __builtin_ctz(n); n     ensure_base(zeros); n     int shift = base - zeros; n     for (int i = 0; i < n; i++) { n       if (i < (rev[i] >> shift)) { n         swap(a[i], a[rev[i] >> shift]); n       } n     } n     for (int k = 1; k < n; k <<= 1) { n       for (int i = 0; i < n; i += 2 * k) { n         for (int j = 0; j < k; j++) { n           Modular<T> x = a[i + j]; n           Modular<T> y = a[i + j + k] * roots[j + k]; n           a[i + j] = x + y; n           a[i + j + k] = x - y; n         } n       } n     } n   } n   static vector<Modular<T>> multiply(vector<Modular<T>> a, n                                      vector<Modular<T>> b) { n     if (a.empty() || b.empty()) { n       return {}; n     } n     int eq = (a == b); n     int need = (int)a.size() + (int)b.size() - 1; n     int nbase = 0; n     while ((1 << nbase) < need) nbase++; n     ensure_base(nbase); n     int sz = 1 << nbase; n     a.resize(sz); n     b.resize(sz); n     fft(a); n     if (eq) n       b = a; n     else n       fft(b); n     Modular<T> inv_sz = 1 / static_cast<Modular<T>>(sz); n     for (int i = 0; i < sz; i++) { n       a[i] *= b[i] * inv_sz; n     } n     reverse(a.begin() + 1, a.end()); n     fft(a); n     a.resize(need); n     return a; n   } n }; n template <typename T> n typename NTT<T>::Type NTT<T>::md; n template <typename T> n Modular<T> NTT<T>::root; n template <typename T> n int NTT<T>::base; n template <typename T> n int NTT<T>::max_base; n template <typename T> n vector<Modular<T>> NTT<T>::roots; n template <typename T> n vector<int> NTT<T>::rev; n template <typename T> n vector<Modular<T>> inverse(const vector<Modular<T>>& a) { n   assert(!a.empty()); n   int n = (int)a.size(); n   vector<Modular<T>> b = {1 / a[0]}; n   while ((int)b.size() < n) { n     vector<Modular<T>> x(a.begin(), a.begin() + min(a.size(), b.size() << 1)); n     x.resize(b.size() << 1); n     b.resize(b.size() << 1); n     vector<Modular<T>> c = b; n     NTT<T>::fft(c); n     NTT<T>::fft(x); n     Modular<T> inv = 1 / static_cast<Modular<T>>((int)x.size()); n     for (int i = 0; i < (int)x.size(); i++) { n       x[i] *= c[i] * inv; n     } n     reverse(x.begin() + 1, x.end()); n     NTT<T>::fft(x); n     rotate(x.begin(), x.begin() + (x.size() >> 1), x.end()); n     fill(x.begin() + (x.size() >> 1), x.end(), 0); n     NTT<T>::fft(x); n     for (int i = 0; i < (int)x.size(); i++) { n       x[i] *= c[i] * inv; n     } n     reverse(x.begin() + 1, x.end()); n     NTT<T>::fft(x); n     for (int i = 0; i < ((int)x.size() >> 1); i++) { n       b[i + ((int)x.size() >> 1)] = -x[i]; n     } n   } n   b.resize(n); n   return b; n } n template <typename T> n vector<Modular<T>> inverse_old(vector<Modular<T>> a) { n   assert(!a.empty()); n   int n = (int)a.size(); n   if (n == 1) { n     return {1 / a[0]}; n   } n   int m = (n + 1) >> 1; n   vector<Modular<T>> b = inverse(vector<Modular<T>>(a.begin(), a.begin() + m)); n   int need = n << 1; n   int nbase = 0; n   while ((1 << nbase) < need) { n     ++nbase; n   } n   NTT<T>::ensure_base(nbase); n   int size = 1 << nbase; n   a.resize(size); n   b.resize(size); n   NTT<T>::fft(a); n   NTT<T>::fft(b); n   Modular<T> inv = 1 / static_cast<Modular<T>>(size); n   for (int i = 0; i < size; ++i) { n     a[i] = (2 - a[i] * b[i]) * b[i] * inv; n   } n   reverse(a.begin() + 1, a.end()); n   NTT<T>::fft(a); n   a.resize(n); n   return a; n } n template <typename T> n vector<Modular<T>> operator*(const vector<Modular<T>>& a, n                              const vector<Modular<T>>& b) { n   if (a.empty() || b.empty()) { n     return {}; n   } n   if (min(a.size(), b.size()) < 150) { n     vector<Modular<T>> c(a.size() + b.size() - 1, 0); n     for (int i = 0; i < (int)a.size(); i++) { n       for (int j = 0; j < (int)b.size(); j++) { n         c[i + j] += a[i] * b[j]; n       } n     } n     return c; n   } n   return NTT<T>::multiply(a, b); n } n template <typename T> n vector<Modular<T>>& operator*=(vector<Modular<T>>& a, n                                const vector<Modular<T>>& b) { n   return a = a * b; n } n class graph { n  public: n   int n; n   vector<vector<int>> adj; n   vector<int> degree; n   vector<pair<int, int>> edge; n   vector<int> depth; n   vector<bool> covered; n   graph(int N) n       : n(N), n         adj(vector<vector<int>>(n)), n         degree(vector<int>(n)), n         depth(vector<int>(n)), n         covered(vector<bool>(n, false)) {} n   void add(int X, int Y) { n     degree[X]++, degree[Y]++; n     edge.emplace_back(X, Y); n     adj[X].push_back(Y); n     adj[Y].push_back(X); n   } n   void dfs(int N) { n     if (covered[N] == true) return; n     covered[N] = true; n     for (int i : adj[N]) { n       if (covered[i] == false) { n         depth[i] = depth[N] + 1; n         dfs(i); n       } n     } n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   string s, t; n   cin >> s >> t; n   int n = s.size(), m = t.size(); n   while (t.size() < s.size()) t +=  z ; n   assert(t.size() == s.size()); n   vector<graph> gr(n - m + 1, graph(6)); n   for (int i = 0; i < 6; i++) { n     for (int j = 0; j < 6; j++) { n       vector<Mint> a(n), b(n); n       for (int k = 0; k < n; k++) { n         if (int(s[k] -  a ) == i) a[k] = 1; n         if (int(t[k] -  a ) == j) b[k] = 1; n       } n       reverse(a.begin(), a.end()); n       for (int k = 0; k < n; k++) a.push_back(0), b.push_back(b[k]); n       a *= b; n       for (int k = 2 * n - 1, cnt = 0; cnt < n - m + 1; k--, cnt++) { n         if (a[k]()) { n           gr[cnt].add(i, j); n         } n       } n     } n   } n   for (int i = 0; i < n - m + 1; i++) { n     int ans = 0; n     for (int j = 0; j < 6; j++) { n       if (gr[i].covered[j] == false) { n         gr[i].dfs(j); n         ans += 1; n       } n     } n     cout << 6 - ans <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[60][110][2]; n long long bin[60], num[60], s, tt; n inline long long rd() { n   long long x = 0; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     ; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return x; n } n int main() { n   s = rd(); n   bin[0] = 1; n   for (int i = 1; i <= 51; i++) bin[i] = bin[i - 1] * 2; n   long long ans = 0; n   for (int h = 1; bin[h] - 1 <= s; h++) { n     long long hh = s % (bin[h] - 1); n     for (int j = h - 1; j; j--) n       if (bin[j] - 1 <= hh) hh -= bin[j] - 1; n     ans += (!hh); n   } n   for (int h0 = 1; h0 <= 50; h0++) n     for (int h1 = 1; h1 <= 50; h1++) n       if (bin[h0 + 1] + bin[h1 + 1] - 3 <= s - bin[h1] + 1) { n         long long hh = (s - bin[h1] + 1) % (bin[h0 + 1] + bin[h1 + 1] - 3); n         for (int n = 0; n <= h0 + h1 - 2 && n <= hh; n++) { n           tt = 0; n           long long now = hh + n; n           if (now & 1) continue; n           memset(num, 0, sizeof(num)); n           for (now >>= 1; now; now >>= 1) num[++tt] = now & 1; n           memset(f, 0, sizeof(f)); n           f[0][0][0] = 1; n           for (int i = 1; i <= tt; i++) n             for (int j = 0; j <= n; j++) n               for (int k = 0; k < 2; k++) n                 if (f[i - 1][j][k]) { n                   for (int l1 = 0; l1 < 2; l1++) n                     for (int l2 = 0; l2 < 2; l2++) { n                       if ((i >= h0 && l1) || (i >= h1 && l2)) continue; n                       int hh = l1 + l2 + k, now = j + l1 + l2; n                       if ((hh & 1) == num[i]) n                         f[i][now][hh >> 1] += f[i - 1][j][k]; n                     } n                 } n           ans += f[tt][n][0]; n         } n       } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int shortestPaths[100][100]; n int graph[100][100]; n void bfs(int start, int n) { n   queue<int> q; n   q.push(start); n   int visited[n]; n   for (int i = 0; i < n; i++) { n     visited[i] = 0; n   } n   visited[start] = 1; n   int dist = 1; n   while (true) { n     int sz = q.size(); n     if (sz == 0) { n       break; n     } n     for (int i = 0; i < sz; i++) { n       int cur = q.front(); n       q.pop(); n       for (int j = 0; j < 100; j++) { n         if (graph[cur][j] == 1 && visited[j] == 0) { n           visited[j] = 1; n           shortestPaths[start][j] = dist; n           q.push(j); n         } n       } n     } n     dist++; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < 100; i++) { n     for (int j = 0; j < 100; j++) { n       shortestPaths[i][j] = 0; n       graph[i][j] = 0; n     } n   } n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < n; j++) { n       graph[i][j] = (int)(s[j] -  0 ); n     } n   } n   int m; n   cin >> m; n   vector<int> vertices; n   for (int i = 0; i < m; i++) { n     int a; n     cin >> a; n     a--; n     vertices.push_back(a); n   } n   for (int i = 0; i < n; i++) { n     bfs(i, n); n   } n   vector<int> result; n   result.push_back(vertices[0]); n   int last = vertices[0]; n   int ind = 0; n   for (int i = 1; i < m; i++) { n     if (i != 1) { n       if (shortestPaths[last][vertices[i]] < i - ind) { n         result.push_back(vertices[i - 1]); n         ind = i - 1; n         last = vertices[i - 1]; n       } n     } n   } n   result.push_back(vertices[vertices.size() - 1]); n   cout << result.size() << endl; n   for (int i = 0; i < result.size(); i++) { n     cout << result[i] + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 111111; n long long tree[maxn * 4]; n void Insert(int o, int l, int r, int k, int v) { n   if (l == r) { n     tree[o] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (k <= mid) n     Insert(o * 2, l, mid, k, v); n   else n     Insert(o * 2 + 1, mid + 1, r, k, v); n   tree[o] = tree[o * 2] + tree[o * 2 + 1]; n } n long long Query(int o, int l, int r, int L, int R) { n   if (L <= l && r <= R) return tree[o]; n   int mid = (l + r) >> 1; n   long double ans = 0; n   if (L <= mid) ans += Query(o * 2, l, mid, L, R); n   if (R > mid) ans += Query(o * 2 + 1, mid + 1, r, L, R); n   return ans; n } n int n, x, a[maxn], f[maxn]; n int main() { n   cin >> n; n   long double ans = 0; n   for (int i = 1; i <= n; i++) cin >> a[i], f[a[i]] = i; n   for (int i = 1; i <= n; i++) { n     ans += Query(1, 1, n, a[i], n); n     Insert(1, 1, n, a[i], 1); n   } n   ans *= ((long long)n * (n + 1)); n   memset(tree, 0, sizeof(tree)); n   long long last = 0; n   for (int i = 1; i <= n; i++) { n     ans -= (last + Query(1, 1, n, a[i], n)); n     last = last + Query(1, 1, n, a[i], n); n     Insert(1, 1, n, a[i], f[a[i]] * 2); n   } n   for (int i = 1; i <= n; i++) n     ans += ((long long)(n - i + 1) * i * (i - 1) / 2); n   ans /= ((long long)n * (n + 1)); n   cout << setprecision(15) << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200001][22]; n int p[200001]; n int d[200001]; n int lca(int u, int v) { n   if (d[u] < d[v]) swap(u, v); n   int k = d[u] - d[v]; n   while (k > 0) { n     int t = log2(k); n     k -= (1 << t); n     u = a[u][t]; n   } n   if (u == v) return v; n   for (int i = 20; i >= 0; i--) { n     if (a[u][i] != a[v][i]) u = a[u][i], v = a[v][i]; n   } n   return p[v]; n } n int orobitiasvla(int x, int v) { n   while (x > 0) { n     int t = log2(x); n     v = a[v][t]; n     x -= (1 << t); n   } n   return v; n } n int main() { n   int n; n   cin >> n; n   int cnt = 2; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= 20; j++) a[i][j] = -1; n   } n   d[1] = 0; n   int d1 = 1, d2 = 1; n   int mx = 0; n   n--; n   while (n--) { n     int k; n     cin >> k; n     a[cnt][0] = k; n     p[cnt] = k; n     d[cnt] = d[k] + 1; n     for (int i = 1; i <= 20; i++) n       if (d[cnt] >= (1 << i)) a[cnt][i] = orobitiasvla((1 << i) - 1, k); n     int y = lca(cnt, d1); n     int cost = d[cnt] - d[y] + d[d1] - d[y]; n     y = lca(cnt, d2); n     int cost1 = d[cnt] - d[y] + d[d2] - d[y]; n     if (cost1 > mx || cost > mx) { n       if (cost > cost1) n         d2 = cnt; n       else n         d1 = cnt; n       mx = max(cost, cost1); n     } n     cout << mx <<    ; n     cnt++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 6e5 + 7; n int a, b, n, ans; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> a >> b, n = a + b; n   for (int l = 1, r, s; l <= n; l = r + 1) { n     s = n / l, r = n / s; n     int al = (a + s) / (s + 1), ar = a / s, bl = (b + s) / (s + 1), br = b / s; n     if (al <= ar && bl <= br) ans += min(ar + br, r) - max(al + bl, l) + 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a, b, c, d, s, len; n vector<long long> z, t, v[2]; n map<long long, int> ma; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> a >> b >> c >> d >> s >> len; n   t.assign(n + 1, -1); n   v[0].assign(n + 1, s); n   v[1].assign(n + 1, s); n   for (int i = 0; i < n; ++i) { n     int q; n     cin >> t[i + 1] >> q; n     v[0][i + 1] = v[0][i] + (q ? a : -b); n     v[1][i + 1] = v[1][i] + (q ? c : -d); n   } n   int lim = 0; n   ++ma[v[1][0]]; n   for (int i = 0; i < n + 1; ++i) { n     if (v[0][i] < 0) break; n     if (!(--ma[v[1][i]])) ma.erase(v[1][i]); n     int be = t[i] + 1; n     while (lim < n + 1 && t[lim] < be + len) { n       ++ma[v[1][lim]]; n       ++lim; n     } n     if (i + 1 == lim || v[0][i] - v[1][i] + ma.begin()->first >= 0) { n       cout << be << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int A[100], L[100], n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> A[i], A[i]--; n   for (int i = 0; i < m; ++i) cin >> L[i]; n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j <= n; ++j) { n       vector<int> cnt(m + 1, 0); n       for (int k = i; k < j; ++k) cnt[A[k]]++; n       bool fl = true; n       for (int k = 0; k < m; ++k) fl &= (L[k] == cnt[k]); n       if (fl) { n         cout <<  YES n ; n         return 0; n       } n     } n   } n   cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, b[1000010]; n int main() { n   scanf( %d%d , &n, &k); n   int i; n   if (k * 3 > n || k == 1) n     puts( -1 ); n   else { n     for (i = 1; i <= (int)(k); ++i) b[i] = b[i + 2 * k] = i, b[i + k] = i - 1; n     b[k + 1] = k; n     for (i = 3 * k + 1; i <= n; ++i) b[i] = 1; n     for (i = 1; i <= (int)(n); ++i) printf( %d  , b[i]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[9][5], b[8]; n long long dis(int i, int j) { n   long long x = a[i][0] - a[j][0], y = a[i][1] - a[j][1], z = a[i][2] - a[j][2]; n   return x * x + y * y + z * z; n } n bool check() { n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       if (i == j) continue; n       if (j < i) n         b[j] = dis(i, j); n       else n         b[j - 1] = dis(i, j); n     } n     sort(b, b + 7); n     long long t1 = b[0], t2 = 2 * b[0], t3 = 3 * b[0]; n     if (t1 == 0 || t1 != b[1] || t1 != b[2] || t2 != b[3] || t2 != b[4] || n         t2 != b[5] || t3 != b[6]) n       return false; n   } n   return true; n } n bool is_ok(int cur) { n   if (cur == 8) { n     return check(); n   } n   sort(a[cur], a[cur] + 3); n   do { n     if (is_ok(cur + 1)) return true; n   } while (next_permutation(a[cur], a[cur] + 3)); n   return false; n } n int main() { n   for (int i = 0; i < 8; i++) cin >> a[i][0] >> a[i][1] >> a[i][2]; n   if (!is_ok(0)) n     puts( NO ); n   else { n     puts( YES ); n     for (int i = 0; i < 8; i++) n       for (int j = 0; j < 3; j++) { n         cout << a[i][j] <<    ; n         if (j == 2) cout << endl; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int constexpr nax = 3e5 + 5; n using ll = int64_t; n const int INF = 1e9; n template <typename T> n struct fenwick { n   T bit[nax]; n   fenwick() { memset(bit, 0, sizeof(bit)); } n   void update(int x, T val) { n     for (; x < nax; x += x & -x) bit[x] += val; n   } n   T get(int x) { n     int res = 0; n     for (; x > 0; x -= x & -x) res += bit[x]; n     return res; n   } n   T get(int l, int r) { return get(r) - get(l - 1); } n }; n struct segtree { n   static constexpr int Mx = 4 * nax; n   ll sg[Mx] = {}, tag[Mx] = {}; n   inline void pull(int v) { sg[v] = min(sg[v * 2], sg[v * 2 + 1]); } n   segtree() { n     fill(sg, sg + Mx, INF); n     fill(tag, tag + Mx, 0); n   } n   inline void push(int v) { n     if (v * 2 + 1 < Mx && tag[v]) { n       sg[v * 2] += tag[v]; n       sg[v * 2 + 1] += tag[v]; n       tag[v * 2] += tag[v]; n       tag[v * 2 + 1] += tag[v]; n     } n     tag[v] = 0; n   } n   inline void update(int v, int x, int y, int l, int r, ll val) { n     push(v); n     if (l > r) return; n     if (l == x && r == y) { n       sg[v] += val; n       tag[v] += val; n       return push(v); n     } n     int mid = x + y >> 1; n     update(v * 2, x, mid, l, min(r, mid), val); n     update(v * 2 + 1, mid + 1, y, max(l, mid + 1), r, val); n     pull(v); n   } n   int find_first_neg(int v, int x, int y) { n     push(v); n     if (x == y) { n       assert(sg[v] <= 0); n       return x; n     } else { n       int mid = x + y >> 1; n       if (sg[v * 2] <= 0) n         return find_first_neg(v * 2, x, mid); n       else n         return find_first_neg(v * 2 + 1, mid + 1, y); n     } n   } n }; n segtree sg; n fenwick<int> sum; n int n, nq; n int ans[nax]; n int b[nax]; n vector<pair<int, int>> q[nax]; n int main() { n   cin.tie(0)->sync_with_stdio(false); n   cin >> n >> nq; n   for (int i = 1; i <= n; ++i) { n     cin >> b[i]; n     b[i] = i < b[i] ? INF : i - b[i]; n   } n   for (int i = 1; i <= nq; ++i) { n     int x, y; n     cin >> x >> y; n     q[x + 1].emplace_back(n - y, i); n   } n   for (int i = n; i >= 1; --i) { n     sg.update(1, 1, n, i, i, (ll)b[i] - INF); n     while (sg.sg[1] <= 0) { n       int v = sg.find_first_neg(1, 1, n); n       sum.update(v, 1); n       sg.update(1, 1, n, v, v, INF); n       sg.update(1, 1, n, v, n, -1); n     } n     for (const auto &[r, id] : q[i]) ans[id] = sum.get(i, r); n   } n   for (int i = 1; i <= nq; ++i) { n     cout << ans[i] <<   n ; n   } n } n