#include <bits/stdc++.h> n using namespace std; n int n; n char s[1001001]; n int p[1001001] = {1}; n int v[10][1001001], deg[1001001]; n int Rand() { return rand() * rand(); } n int main() { n   scanf( %s , s); n   n = strlen(s); n   for (int i = 1; i <= n; i++) { n     p[i] = p[i - 1] * 10 % 7; n   } n   int rlt = 0; n   reverse(s, s + n); n   for (int i = 0; s[i]; i++) { n     int c = s[i] -  0 ; n     rlt = (rlt + p[i] * c) % 7; n     v[c][deg[c]++] = i; n   } n   while (rlt) { n     int x = rand() % 9 + 1; n     int y = rand() % 9 + 1; n     if (x == y || !deg[x] || !deg[y]) { n       continue; n     } n     int tx = Rand() % deg[x]; n     int ty = Rand() % deg[y]; n     swap(v[x][tx], v[y][ty]); n     s[v[x][tx]] = x +  0 ; n     s[v[y][ty]] = y +  0 ; n     rlt = rlt + (p[v[x][tx]] - p[v[y][ty]]) * (x - y); n     rlt %= 7; n     if (rlt < 0) rlt += 7; n   } n   reverse(s, s + n); n   puts(s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, ans, tot = 1, n, temp; n bool flag, u[9]; n struct none { n   long long a; n   int sum; n } f[9]; n bool cmy(none f1, none f2) { return f1.a > f2.a; } n long long fuck(long long x) { n   int back = 0; n   for (long long i = 2; i * i <= x; i++) n     while (x % i == 0) { n       back++; n       x /= i; n     } n   if (x != 1) back++; n   if (back > 1) back++; n   return back; n } n void init() { n   long long x; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &f[i].a); n   sort(f + 1, f + 1 + n, cmy); n } n void work() { n   for (int i = 1; i <= n; i++) ans += f[i].sum; n   for (int i = 1; i < n; i++) { n     long long x = f[i].a; n     while (1) { n       temp = -1; n       flag = false; n       t = 0; n       for (int j = i + 1; j <= n; j++) n         if ((!u[j]) && (x % f[j].a == 0) && f[j].sum - 1 > temp) { n           if (temp == -1) temp++; n           t = j; n           flag = true; n           ans -= (f[j].sum - 1 - temp); n           temp = f[j].sum - 1; n         } n       if (!flag) break; n       x /= f[t].a; n       tot++; n       u[t] = true; n       if (f[t].sum == 1) ans--; n     } n   } n   if (tot < n) ans++; n   printf( %d n , ans); n } n int main() { n   init(); n   for (int i = 1; i <= n; i++) f[i].sum = fuck(f[i].a); n   work(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2500 * 4 + 10; n int n, k, d, sz; n bool isV[2000], M = 0; n string arr[N]; n set<string> st[N]; n string change(string z) { n   int c = 0; n   string s =   ; n   for (int i = z.size() - 1; i >= 0; i--) { n     s += z[i]; n     if (isV[z[i]]) M = 1, c++; n     if (c == k) break; n   } n   if (c != k) M = 0; n   reverse(s.begin(), s.end()); n   return s; n } n bool checkAABB() { n   vector<string> v(10); n   for (int i = 0; i < sz; i++) { n     for (int j = 0; j < 4; j++) { n       v[j] = arr[i * 4 + j]; n     } n     if (v[0] != v[1] || v[2] != v[3]) return 0; n   } n   return 1; n } n bool checkABAB() { n   vector<string> v(10); n   for (int i = 0; i < sz; i++) { n     for (int j = 0; j < 4; j++) { n       v[j] = arr[i * 4 + j]; n     } n     if (v[0] != v[2] || v[1] != v[3]) return 0; n   } n   return 1; n } n bool checkABBA() { n   vector<string> v(10); n   for (int i = 0; i < sz; i++) { n     for (int j = 0; j < 4; j++) { n       v[j] = arr[i * 4 + j]; n     } n     if (v[0] != v[3] || v[2] != v[1]) return 0; n   } n   return 1; n } n bool checkAAAA() { n   vector<string> v(10); n   for (int i = 0; i < sz; i++) { n     for (int j = 0; j < 4; j++) { n       v[j] = arr[i * 4 + j]; n     } n     if (v[0] != v[1] || v[2] != v[3] || v[1] != v[2]) return 0; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   isV[ a ] = isV[ o ] = isV[ i ] = isV[ u ] = isV[ e ] = 1; n   cin >> n >> k; n   sz = n; n   n *= 4; n   bool Valid = 1; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     M = 0; n     arr[i] = change(arr[i]); n     if (!M) Valid = 0; n   } n   if (!Valid) return cout <<  NO  << endl, 0; n   if (checkAAAA()) n     cout <<  aaaa  << endl; n   else if (checkABAB()) n     cout <<  abab  << endl; n   else if (checkABBA()) n     cout <<  abba  << endl; n   else if (checkAABB()) n     cout <<  aabb  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long mul(long long x, long long y) { n   if (x >= (1ll << 60) / y) n     return (1ll << 60); n   else n     return x * y; n } n int n, m, p; n int d[100005]; n long long cat[100005], sum[100005]; n long long dp[105][100005]; n int dq[100005], s, e; n int main() { n   scanf( %d%d%d , &n, &m, &p); n   for (int i = 2; i <= n; ++i) scanf( %d , d + i), d[i] += d[i - 1]; n   for (int i = 1; i <= m; ++i) { n     int h, t; n     scanf( %d%d , &h, &t); n     cat[i] = t - d[h] + 1000000000; n   } n   sort(cat + 1, cat + 1 + m); n   for (int i = 1; i <= m; ++i) sum[i] = sum[i - 1] + cat[i]; n   memset(dp, 63, sizeof(dp)); n   for (int i = 0; i < 105; ++i) dp[i][0] = 0; n   for (int k = 1; k <= p; ++k) { n     dq[0] = 0, s = 0, e = 1; n     for (int i = 1; i <= m; ++i) { n       long long c1 = cat[i], c2 = i * c1 - sum[i]; n       while (s + 1 < e) { n         long long x1 = dq[s], y1 = dp[k - 1][x1] + sum[x1]; n         long long x2 = dq[s + 1], y2 = dp[k - 1][x2] + sum[x2]; n         if ((y2 - y1) <= mul(c1, (x2 - x1))) n           s++; n         else n           break; n       } n       int t = dq[s]; n       dp[k][i] = min(dp[k][i], dp[k - 1][t] + sum[t] - c1 * t + c2); n       while (s + 1 < e) { n         long long x1 = i, y1 = dp[k - 1][i] + sum[i]; n         long long x2 = dq[e - 1], y2 = dp[k - 1][x2] + sum[x2]; n         long long x3 = dq[e - 2], y3 = dp[k - 1][x3] + sum[x3]; n         if (mul((y1 - y2), (x2 - x3)) <= mul((y2 - y3), (x1 - x2))) n           e--; n         else n           break; n       } n       dq[e++] = i; n     } n   } n   printf( %I64d n , dp[p][m]); n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n const int maxn = 3e5 + 5, N = 1e6 + 5, SQ = 55, base = 879169, mod = 1e9 + 7, n           INF = 1e9 + 5, lg = 20; n const long double eps = 1e-4; n struct node { n   int mn = INF, lz = 0; n   int c1 = 0, c2 = 0; n }; n node seg[4 * maxn]; n int n, p[maxn], pos[maxn], q[maxn]; n inline void shift(int v) { n   seg[2 * v].mn += seg[v].lz; n   seg[2 * v + 1].mn += seg[v].lz; n   seg[2 * v].lz += seg[v].lz; n   seg[2 * v + 1].lz += seg[v].lz; n   seg[v].lz = 0; n } n void add(int l, int r, int x, int v = 1, int s = 0, int e = n) { n   if (l >= e || s >= r) { n     return; n   } n   if (l <= s && e <= r) { n     seg[v].mn += x; n     seg[v].lz += x; n     return; n   } n   shift(v); n   add(l, r, x, 2 * v, s, (s + e) / 2); n   add(l, r, x, 2 * v + 1, (s + e) / 2, e); n   seg[v].mn = min(seg[2 * v].mn, seg[2 * v + 1].mn); n } n pair<int, int> get(int l, int r, int v = 1, int s = 0, int e = n) { n   if (l >= e || s >= r) { n     return {0, 0}; n   } n   if (l <= s && e <= r) { n     return {seg[v].c1, seg[v].c2}; n   } n   shift(v); n   pair<int, int> a = get(l, r, 2 * v, s, (s + e) / 2); n   pair<int, int> b = get(l, r, 2 * v + 1, (s + e) / 2, e); n   return {a.first + b.first, a.second + b.second}; n } n void modify_num(int p, int x, int v = 1, int s = 0, int e = n) { n   seg[v].c2++; n   seg[v].mn = min(seg[v].mn, x); n   if (e - s == 1) { n     return; n   } n   shift(v); n   if (p < (s + e) / 2) { n     modify_num(p, x, 2 * v, s, (s + e) / 2); n     return; n   } n   modify_num(p, x, 2 * v + 1, (s + e) / 2, e); n } n void modify_bomb(int p, int v = 1, int s = 0, int e = n) { n   seg[v].c1++; n   if (e - s == 1) { n     return; n   } n   shift(v); n   if (p < (s + e) / 2) { n     modify_bomb(p, 2 * v, s, (s + e) / 2); n     return; n   } n   modify_bomb(p, 2 * v + 1, (s + e) / 2, e); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> p[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> q[i]; n   } n   for (int i = 0; i < n; ++i) { n     p[i]--, q[i]--; n     pos[p[i]] = i; n   } n   int ans = n - 1; n   for (int i = 0; i < n; ++i) { n     while (ans) { n       add(0, pos[ans], -1); n       pair<int, int> res = get(pos[ans], n); n       if (res.first < res.second + 1 || seg[1].mn <= 0) { n         add(0, pos[ans], 1); n         break; n       } n       modify_num(pos[ans], res.first - res.second); n       ans--; n     } n     cout << ans + 1 <<    ; n     modify_bomb(q[i]); n     add(0, q[i] + 1, 1); n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e3 + 1; n struct node { n   int w, x, y, z; n }; n queue<int> q; n vector<int> g[N], rg[N]; n int d[N][N], rd[N][N], vis[N]; n vector<pair<int, int>> fg[N], frg[N]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     rg[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       vis[j] = 0; n       d[i][j] = n + 1; n     } n     vis[i] = 1; n     q.push(i); n     d[i][i] = 0; n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (auto u : g[v]) { n         if (vis[u]) continue; n         vis[u] = 1; n         q.push(u); n         d[i][u] = d[i][v] + 1; n       } n     } n     for (int j = 1; j <= n; j++) { n       if (j == i) continue; n       if (d[i][j] != n + 1) fg[i].push_back({d[i][j], j}); n     } n     sort(fg[i].begin(), fg[i].end()); n     for (int j = 1; j <= n; j++) { n       vis[j] = 0; n       rd[i][j] = n + 1; n     } n     vis[i] = 1; n     q.push(i); n     rd[i][i] = 0; n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (auto u : rg[v]) { n         if (vis[u]) continue; n         vis[u] = 1; n         q.push(u); n         rd[i][u] = rd[i][v] + 1; n       } n     } n     for (int j = 1; j <= n; j++) { n       if (j == i) continue; n       if (rd[i][j] != n + 1) frg[i].push_back({rd[i][j], j}); n     } n     sort(frg[i].begin(), frg[i].end()); n   } n   int ans = 0; n   node A; n   for (int b = 1; b <= n; b++) { n     for (int c = 1; c <= n; c++) { n       if (b == c || d[b][c] == n + 1) continue; n       int ans1 = 0, z = 0; n       node B; n       int sz1 = frg[b].size(), sz2 = fg[c].size(); n       for (int j = sz1 - 1; j >= max(z, sz1 - 3); j--) { n         int a = frg[b][j].second; n         for (int k = sz2 - 1; k >= max(z, sz2 - 3); k--) { n           int dd = fg[c][k].second; n           if (a == c || dd == b || a == dd) continue; n           int ans2 = d[b][c] + rd[b][a] + d[c][dd]; n           node C; n           C.w = a; n           C.x = b; n           C.y = c; n           C.z = dd; n           if (ans2 > ans1) { n             ans1 = ans2; n             B = C; n           } n         } n       } n       if (ans1 > ans) { n         ans = ans1; n         A = B; n       } n     } n   } n   cout << A.w <<     << A.x <<     << A.y <<     << A.z <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 5; n vector<int> adjList[N]; n unordered_set<int> remaining; n bitset<N> inQ, nextToQ; n vector<int> ans; n void solve(int u) { n   remaining.erase(u); n   for (auto v : adjList[u]) { n     remaining.erase(v); n   } n   if (!remaining.empty()) { n     solve(*remaining.begin()); n   } n   if (!nextToQ[u]) { n     inQ[u] = 1; n     nextToQ[u] = 1; n     for (auto v : adjList[u]) { n       nextToQ[v] = 1; n     } n     ans.push_back(u); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     adjList[u].push_back(v); n   } n   for (int i = 1; i <= n; i++) { n     remaining.insert(i); n   } n   solve(1); n   cout << ans.size() << endl; n   for (auto u : ans) { n     cout << u <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const double EPS = 1e-6; n const string alpha =  abcdefghijklmnopqrstuvwxyz ; n const long long INFINF = 1e18; n const int INF = 1e9; n const int maxN = 1e6; n int T; n void tc() { n   int N; n   cin >> N; n   string second =   ; n   int d = 0; n   for (int i = 0; i < N; i++) { n     if (d < N) { n       second +=  8 ; n       d += 4; n     } else { n       second +=  9 ; n     } n   } n   reverse(second.begin(), second.end()); n   cout << second << endl; n } n int main() { n   cin >> T; n   while (T--) { n     tc(); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2,Ofast,inline,unroll-all-loops,-ffast-math ) n #pragma GCC target( avx,sse2,sse3,sse4,popcnt ) n using namespace std; n int nxt[100010][26], f[255][255][255], pos[26], n, q, a = 0, b = 0, c = 0; n char s[100010], A[255], B[255], C[255]; n template <class T> n void read(T &x) { n   char ch = x = 0; n   bool fl = false; n   while (!isdigit(ch)) fl |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   x = fl ? -x : x; n } n int main() { n   read(n), read(q), scanf( %s , s + 1); n   for (int i = 0; i < 26; i++) { n     pos[i] = n + 1; n   } n   memcpy(nxt[n + 1], pos, sizeof(pos)); n   for (int i = n; ~i; i--) { n     memcpy(nxt[i], pos, sizeof(pos)), pos[s[i] -  a ] = i; n   } n   char op[10]; n   for (int i = 1, x; i <= q; i++) { n     scanf( %s , op); n     if (op[0] ==  + ) { n       read(x), scanf( %s , op); n       if (x == 1) { n         A[++a] = op[0]; n         for (int b = 0; b <= ::b; b++) { n           for (int c = 0; c <= ::c; c++) { n             f[a][b][c] = 1 << 30; n             if (a) n               f[a][b][c] = min(f[a][b][c], nxt[f[a - 1][b][c]][A[a] -  a ]); n             if (b) n               f[a][b][c] = min(f[a][b][c], nxt[f[a][b - 1][c]][B[b] -  a ]); n             if (c) n               f[a][b][c] = min(f[a][b][c], nxt[f[a][b][c - 1]][C[c] -  a ]); n           } n         } n       } n       if (x == 2) { n         B[++b] = op[0]; n         for (int a = 0; a <= ::a; a++) { n           for (int c = 0; c <= ::c; c++) { n             f[a][b][c] = 1 << 30; n             if (a) n               f[a][b][c] = min(f[a][b][c], nxt[f[a - 1][b][c]][A[a] -  a ]); n             if (b) n               f[a][b][c] = min(f[a][b][c], nxt[f[a][b - 1][c]][B[b] -  a ]); n             if (c) n               f[a][b][c] = min(f[a][b][c], nxt[f[a][b][c - 1]][C[c] -  a ]); n           } n         } n       } n       if (x == 3) { n         C[++c] = op[0]; n         for (int a = 0; a <= ::a; a++) { n           for (int b = 0; b <= ::b; b++) { n             f[a][b][c] = 1 << 30; n             if (a) n               f[a][b][c] = min(f[a][b][c], nxt[f[a - 1][b][c]][A[a] -  a ]); n             if (b) n               f[a][b][c] = min(f[a][b][c], nxt[f[a][b - 1][c]][B[b] -  a ]); n             if (c) n               f[a][b][c] = min(f[a][b][c], nxt[f[a][b][c - 1]][C[c] -  a ]); n           } n         } n       } n     } else { n       read(x); n       if (x == 1) a--; n       if (x == 2) b--; n       if (x == 3) c--; n     } n     puts(f[a][b][c] <= n ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, A, B; n long long arr[100000]; n long long solve(long long l, long long r, long long depth, long long add) { n   if (l == r) return A; n   if (depth == 0) return B * (r - l); n   long long mid = n       lower_bound(arr + l, arr + r, (1 << (depth - 1)) + add) - (arr); n   return min(B * (r - l) * (1 << depth), n              solve(l, mid, depth - 1, add) + n                  solve(mid, r, depth - 1, add + (1 << (depth - 1)))); n } n int32_t main() { n   cin >> n >> k >> A >> B; n   for (long long i = 0; i < k; ++i) cin >> arr[i]; n   sort(arr, arr + k); n   cout << solve(0, k, n, 1) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, q; n   cin >> n >> m >> q; n   vector<vector<int> > arr(n); n   for (int i = 0; i < n; i++) { n     arr[i].resize(m); n   } n   vector<pair<int, int> > did; n   for (int i = 0; i < q; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       int x, c; n       cin >> x; n       x--; n       c = arr[x][0]; n       for (int j = 0; j < m - 1; j++) { n         arr[x][j] = arr[x][j + 1]; n       } n       arr[x][m - 1] = c; n       did.push_back({1, x}); n     } n     if (t == 2) { n       int x, c; n       cin >> x; n       x--; n       c = arr[0][x]; n       for (int j = 0; j < n - 1; j++) { n         arr[j][x] = arr[j + 1][x]; n       } n       arr[n - 1][x] = c; n       did.push_back({2, x}); n     } n     if (t == 3) { n       int x, y, c; n       cin >> x >> y >> c; n       arr[x - 1][y - 1] = c; n     } n   } n   for (int i = did.size() - 1; i >= 0; i--) { n     int x = did[i].second; n     if (did[i].first == 1) { n       int c = arr[x][m - 1]; n       for (int j = m - 1; j > 0; j--) { n         arr[x][j] = arr[x][j - 1]; n       } n       arr[x][0] = c; n     } else { n       int c = arr[n - 1][x]; n       for (int j = n - 1; j > 0; j--) { n         arr[j][x] = arr[j - 1][x]; n       } n       arr[0][x] = c; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << arr[i][j] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n std::mt19937 rng( n     (int)std::chrono::steady_clock::now().time_since_epoch().count()); n struct PT { n   long long x, y; n   PT(long long _x = 0, long long _y = 0) : x(_x), y(_y) {} n   PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); } n   PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); } n   PT operator*(long long c) const { return PT(x * c, y * c); } n   long long operator*(const PT &p) const { return x * p.x + y * p.y; } n   long long operator%(const PT &p) const { return x * p.y - y * p.x; } n   bool operator<(const PT &p) const { return x != p.x ? x < p.x : y < p.y; } n   bool operator==(const PT &p) const { return x == p.x && y == p.y; } n   friend std::ostream &operator<<(std::ostream &os, const PT &p) { n     return os << p.x <<     << p.y; n   } n   friend std::istream &operator>>(std::istream &is, PT &p) { n     return is >> p.x >> p.y; n   } n }; n bool comp(PT a, PT b) { n   int hp1 = (a.x < 0 || (a.x == 0 && a.y < 0)); n   int hp2 = (b.x < 0 || (b.x == 0 && b.y < 0)); n   if (hp1 != hp2) return hp1 < hp2; n   long long R = a % b; n   if (R) return R > 0; n   return a * a < b * b; n } n const int ms = 6060; n int choice[3]; n long long f[4]; n long long go(int on, int lst) { n   if (on == 3) { n     int got = 0; n     for (int i = 0; i < 3; i++) n       for (int j = i + 1; j < 3; j++) { n         if (choice[i] ^ choice[j]) { n           got++; n         } n       } n     if (got % 2 == 0) { n       long long ans = 1; n       for (int l = 0, r = 0; l < 3; l = r) { n         while (r < 3 && choice[r] == choice[l]) r++; n         long long v = f[choice[l]]; n         if (r - l == 1) { n           ans *= v; n         } else if (r - l == 2) { n           ans *= v * (v - 1) / 2; n         } else { n           ans *= v * (v - 1) * (v - 2) / 6; n         } n       } n       return ans; n     } n     return 0; n   } n   long long ans = 0; n   choice[on] = lst; n   ans = go(on + 1, lst); n   if (lst < 3) { n     ans += go(on, lst + 1); n   } n   return ans; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   int n; n   std::cin >> n; n   std::vector<PT> a(n); n   for (int i = 0; i < n; i++) { n     std::cin >> a[i]; n     int v1 = (int)a[i].x % 4 / 2, v2 = (int)a[i].y % 4 / 2; n     f[v1 * 2 + v2]++; n   } n   std::cout << go(0, 0) <<   n ; n } n


#include <bits/stdc++.h> n const int maxn = 100020, INF = 1 << 29; n int n; n struct rec { n   int n, x; n } a[maxn]; n int al[maxn], ar[maxn]; n struct SBT { n   int root, tot; n   int d[maxn], l[maxn], r[maxn], s[maxn]; n   SBT() { s[0] = root = tot = 0; } n   void prt(int x, int *a, int &w) { n     if (!x) return; n     prt(l[x], a, w); n     for (int i = 1; i <= w; ++i) a[++a[0]] = d[x]; n     prt(r[x], a, w); n   } n   void print(int *a, int w) { prt(root, a, w); } n   void tl(int &x) { n     int y = r[x]; n     r[x] = l[y]; n     l[y] = x; n     s[y] = s[x]; n     s[x] = s[l[x]] + s[r[x]] + 1; n     x = y; n   } n   void tr(int &x) { n     int y = l[x]; n     l[x] = r[y]; n     r[y] = x; n     s[y] = s[x]; n     s[x] = s[l[x]] + s[r[x]] + 1; n     x = y; n   } n   void mt(int &x, bool w) { n     if (!w) { n       if (s[l[l[x]]] > s[r[x]]) n         tr(x); n       else if (s[r[l[x]]] > s[r[x]]) { n         tl(l[x]); n         tr(x); n       } else n         return; n     } else { n       if (s[r[r[x]]] > s[l[x]]) n         tl(x); n       else if (s[l[r[x]]] > s[l[x]]) { n         tr(r[x]); n         tl(x); n       } else n         return; n     } n     mt(l[x], false); n     mt(r[x], true); n     mt(x, false); n     mt(x, true); n   } n   void ist(int &x, int w) { n     if (!x) { n       x = ++tot; n       s[x] = 1; n       d[x] = w; n       l[x] = r[x] = 0; n     } else { n       ++s[x]; n       if (w < d[x]) n         ist(l[x], w); n       else n         ist(r[x], w); n       mt(x, w >= d[x]); n     } n   } n   int dl(int &x, int w) { n     --s[x]; n     if ((d[x] == w) || ((w < d[x]) && (!l[x])) || ((w > d[x]) && (!r[x]))) { n       int tmp = d[x]; n       if ((!l[x]) || (!r[x])) n         x = l[x] + r[x]; n       else n         d[x] = dl(l[x], d[x] + 1); n       return tmp; n     } else if (w < d[x]) n       return dl(l[x], w); n     else n       return dl(r[x], w); n   } n   int getpre(int w) { n     int t = root, last = -INF; n     while (t) { n       if (d[t] < w && d[t] > last) last = d[t]; n       t = (w <= d[t]) ? l[t] : r[t]; n     } n     return last; n   } n   int getsuc(int w) { n     int t = root, last = INF; n     while (t) { n       if (d[t] > w && d[t] < last) last = d[t]; n       t = (w < d[t]) ? l[t] : r[t]; n     } n     return last; n   } n   int select(int k) { n     int t = root; n     while (t) { n       if (s[l[t]] + 1 == k) return d[t]; n       if (k > s[l[t]] + 1) { n         k = k - s[l[t]] - 1; n         t = r[t]; n       } else n         t = l[t]; n     } n   } n   void delet(int w) { dl(root, w); } n   void insert(int w) { ist(root, w); } n   int size() { return s[root]; } n }; n SBT left, right; n inline bool cmp(const rec &a, const rec &b) { return a.x > b.x; } n int main() { n   int i, l, r, tl, tr, ts, ans, w; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     a[i].n = i; n     scanf( %d , &a[i].x); n   } n   std::sort(a + 1, a + n + 1, cmp); n   l = 1; n   ans = al[0] = ar[0] = 0; n   while (l <= n) { n     for (r = l; r < n && a[r + 1].x == a[l].x;) ++r; n     for (i = l; i <= r; ++i) { n       w = a[i].n; n       tr = right.getpre(w); n       tl = left.getsuc(w); n       if (tr + 1 == w) { n         if (tl - 1 == w) { n           right.delet(tr); n           left.delet(tl); n         } else { n           right.delet(tr); n           right.insert(w); n         } n       } else { n         if (tl - 1 == w) { n           left.delet(tl); n           left.insert(w); n         } else { n           right.insert(w); n           left.insert(w); n         } n       } n     } n     ts = (r == n) ? a[r].x : (a[r].x - a[r + 1].x); n     left.print(al, ts); n     right.print(ar, ts); n     l = r + 1; n   } n   ans = al[0]; n   printf( %d n , ans); n   for (i = 1; i <= ans; ++i) printf( %d %d n , al[i], ar[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void setIO(const string &name =   ) { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   if (name.length()) { n     freopen((name +  .in ).c_str(),  r , stdin); n     freopen((name +  .out ).c_str(),  w , stdout); n   } n } n template <typename T> n void read(vector<T> &a) { n   for (auto &x : a) cin >> x; n } n template <typename T> n void read(vector<T> &a, int n) { n   a.resize(n); n   for (auto &x : a) cin >> x; n } n template <class T, class U> n ostream &operator<<(ostream &out, const pair<T, U> &v) { n   out <<  ( ; n   out << v.first <<  ,  << v.second; n   return out <<  ) ; n } n template <class T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   out <<  [ ; n   for (auto(i) = 0; (i) < ((int)(v).size()); (i)++) { n     if (i) out <<  ,  ; n     out << v[i]; n   } n   return out <<  ] ; n } n template <typename T> n void print(vector<T> &a) { n   for (const auto &x : a) cout << x <<    ; n   cout <<   n ; n } n void MOD(int &x, int m = 1000000007) { n   x %= m; n   if (x < 0) x += m; n } n template <typename T> n void dbg(const char *name, T &&arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename T, typename... U> n void dbg(const char *names, T &&arg1, U &&...args) { n   const char *comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   dbg(comma + 1, args...); n } n template <class T> n void read(T &x) { n   cin >> x; n } n template <class T, class... U> n void read(T &t, U &...u) { n   read(t); n   read(u...); n } n template <class T> n void tell(T &x) { n   cout << x; n } n template <class T, class... U> n void tell(T &t, U &...u) { n   tell(t); n   if (sizeof...(u)) tell(   ); n   tell(u...); n } n int gcd(int a, int b) { return !a ? b : gcd(b % a, a); } n int n, m; n vector<vector<int> > board; n int dp[101][int(5e4) + 10 + int(5e4) + 10]; n int32_t main() { n   setIO(); n   read(m, n); n   board.resize(n); n   for (auto &(c) : (board)) { n     read(c, m); n   } n   int ans = int(2e9) + 15; n   vector<int> ans_ar; n   for (int diff = -int(5e4) + 10 + 1; diff < int(5e4) + 10; diff++) { n     if (diff >= 0) { n       dp[n][diff + int(5e4) + 10] = 0; n     } else { n       dp[n][diff + int(5e4) + 10] = int(2e9) + 15; n     } n   } n   for (auto(i) = 0; (i) < (m - 1); (i)++) { n     for (int idx = n - 1; idx >= 0; idx--) { n       for (int diff = -int(5e4) + 10 + 1; diff < int(5e4) + 10; diff++) { n         int &res = dp[idx][diff + int(5e4) + 10]; n         res = int(2e9) + 15; n         res = min(res, dp[idx + 1][diff + int(5e4) + 10] + 1); n         if (diff + (board[idx][i] - board[idx][m - 1]) >= int(5e4) + 10) { n           res = 0; n         } else if (diff + (board[idx][i] - board[idx][m - 1]) <= n                    -int(5e4) + 10) { n           res = int(2e9) + 15; n         } else { n           res = n               min(res, dp[idx + 1][diff + (board[idx][i] - board[idx][m - 1]) + n                                    int(5e4) + 10]); n         } n       } n     } n     if (ans > dp[0][int(5e4) + 10]) { n       ans = dp[0][int(5e4) + 10]; n       int cur_diff = 0; n       ans_ar.clear(); n       for (int idx = 0; idx < n; idx++) { n         const int &res = dp[idx][cur_diff + int(5e4) + 10]; n         if (res == dp[idx + 1][cur_diff + int(5e4) + 10] + 1) { n           ans_ar.emplace_back(idx + 1); n           continue; n         } else { n           if (cur_diff + (board[idx][i] - board[idx][m - 1]) >= int(5e4) + 10) { n             break; n           } else if (cur_diff + (board[idx][i] - board[idx][m - 1]) <= n                      -int(5e4) + 10) { n             break; n           } else { n             cur_diff += (board[idx][i] - board[idx][m - 1]); n           } n         } n       } n     } n   } n   cout << ans <<   n ; n   print(ans_ar); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int printx(int x, int a, int b, int c) { n   if (x == 0) n     return a; n   else if (x == 1) n     return b; n   else n     return c; n } n int main() { n   long long n; n   int x; n   cin >> n >> x; n   n %= 6; n   switch (n) { n     case 0: n       x = printx(x, 0, 1, 2); n       break; n     case 1: n       x = printx(x, 1, 0, 2); n       break; n     case 2: n       x = printx(x, 1, 2, 0); n       break; n     case 3: n       x = printx(x, 2, 1, 0); n       break; n     case 4: n       x = printx(x, 2, 0, 1); n       break; n     case 5: n       x = printx(x, 0, 2, 1); n       break; n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a[100]; n int id[30]; n vector<int> cn[30]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     char l1 =  - ; n     char l2 =  - ; n     int j = -1; n     int s = a[i - 1].size(); n     if (a[i - 1].size() > a[i].size()) { n       s = a[i].size(); n       if (a[i] == string(a[i - 1].substr(0, s))) id[0]++; n     } n     while (++j < s) { n       if (a[i - 1][j] == a[i][j]) continue; n       l1 = a[i - 1][j]; n       l2 = a[i][j]; n       break; n     } n     if (l1 !=  - ) { n       cn[l1 -  a ].push_back(l2 -  a ); n       id[l2 -  a ]++; n     } n   } n   queue<int> ts; n   for (int i = 0; i < 26; i++) n     if (!id[i]) ts.push(i); n   string anw =   ; n   while (!ts.empty()) { n     int u = ts.front(); n     ts.pop(); n     anw += char(u +  a ); n     for (auto v : cn[u]) { n       id[v]--; n       if (!id[v]) ts.push(v); n     } n   } n   cout << (anw.size() == 26 ? anw :  Impossible ) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[26][26]; n int main(void) { n   int n, i, j, a, b, l, ans = 0; n   char s[20]; n   memset(dp, 0, sizeof dp); n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %s , s); n     l = strlen(s); n     a = s[0] -  a ; n     b = s[l - 1] -  a ; n     for (j = 0; j < 26; j++) { n       if (dp[j][a]) { n         dp[j][b] = max(dp[j][b], dp[j][a] + l); n       } n     } n     dp[a][b] = max(dp[a][b], l); n   } n   for (i = 0; i < 26; i++) { n     ans = max(ans, dp[i][i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n inline void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int& e : a) { n     cin >> e; n   } n   long long b = a[0]; n   for (int i = 0; i < n - 1; ++i) { n     int d = a[i + 1] - a[i]; n     if (d < 0) { n       b += d; n       if (b < 0) { n         cout <<  NO n ; n         return; n       } n     } n   } n   cout <<  YES n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   size_t t = 1; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     solve(); n   } n } n


#include <bits/stdc++.h> n const int N = 4096; n int a, n, v[N], h; n long long w, c[N * 10]; n char s[N]; n int main() { n   std::cin >> a >> s; n   for (; s[n]; ++n) v[n + 1] = v[n] + s[n] -  0 ; n   h = v[n]; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j < i; ++j) ++c[v[i] - v[j]]; n   if (a) { n     for (int i = 1; i <= h && i * i <= a; ++i) n       if (0 == a % i) n         if (a / i <= h) w += c[i] * c[a / i] * (i * i == a ? 1 : 2); n   } else n     w = c[0] * ((n + 1) * n - c[0]); n   std::cout << w << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e3 + 10; n int N; n vector<int> g[maxn]; n int sub[maxn][maxn], fa[maxn][maxn]; n long long dp[maxn][maxn]; n void DFS1(int anc, int v, int p) { n   sub[anc][v] = 1; n   for (int u : g[v]) { n     if (u != p) { n       fa[anc][u] = v; n       DFS1(anc, u, v); n       sub[anc][v] += sub[anc][u]; n     } n   } n } n long long DFS2(int v, int u) { n   if (v == u) return 0; n   if (dp[v][u] != -1) return dp[v][u]; n   dp[v][u] = (long long)sub[v][u] * sub[u][v] + n              max(DFS2(v, fa[v][u]), DFS2(fa[u][v], u)); n   return dp[v][u]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> N; n   for (int i = 1; i <= N - 1; ++i) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= N; ++i) { n     fa[i][i] = i; n     DFS1(i, i, i); n   } n   long long ret = 0; n   for (int i = 1; i <= N; ++i) { n     for (int j = 1; j <= N; ++j) { n       dp[i][j] = -1; n     } n   } n   for (int i = 1; i <= N; ++i) { n     for (int j = 1; j <= N; ++j) { n       ret = max(ret, DFS2(i, j)); n     } n   } n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, k; n   cin >> n >> k; n   string fir; n   cin >> fir; n   vector<long long> v; n   for (long long i = 0; i < fir.size(); ++i) { n     if (fir[i] ==  0 ) { n       v.push_back(i); n     } n   } n   long long ans = pow(10, 7); n   long long m = 0; n   for (long long l = 0; l < v.size() - k; ++l) { n     long long r = l + k; n     ; n     while (v[m] - v[l] < v[r] - v[m]) { n       ++m; n     } n     ans = min(ans, max(v[m] - v[l], v[r] - v[m])); n     ans = min(ans, max(v[m - 1] - v[l], v[r] - v[m - 1])); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, l, r, m, ans = 1e9; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> l >> r; n     ans = min(ans, r - l + 1); n   } n   cout << ans <<   n ; n   for (int i = 0; i < n; i++) cout << i % ans <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   string buf; n   while (getline(cin, buf)) s += buf; n   vector<int> a; n   string hoge; n   vector<int> cnt; n   for (int i = 0; i < s.length(); ++i) { n     if (s[i] ==  < ) { n       hoge =   ; n     } else if (s[i] ==  > ) { n       if (hoge ==  table ) { n         cnt.push_back(0); n       } n       if (hoge ==  /table ) { n         a.push_back(cnt.back()); n         cnt.pop_back(); n       } n       if (hoge ==  td ) ++cnt.back(); n     } else { n       hoge += s[i]; n     } n   } n   sort(a.begin(), a.end()); n   for (int i = 0; i < a.size(); ++i) { n     if (i) cout <<    ; n     cout << a[i]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n long long powmod(long long a, long long b) { n   long long res = 1; n   if (a >= mod) a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a; n     if (res >= mod) res %= mod; n     a = a * a; n     if (a >= mod) a %= mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long a, b, c, d, num0, num1; n   cin >> a >> b >> c >> d; n   int flag = 0; n   long long i, j; n   for (i = 1; i <= 1000000; i++) { n     long long temp = (i * (i - 1)) / 2; n     if (temp > a) { n       flag = 1; n       break; n     } n     if (temp == a) { n       num0 = i; n       break; n     } n   } n   for (i = 1; i <= 1000000; i++) { n     long long temp = (i * (i - 1)) / 2; n     if (temp > d) { n       flag = 1; n       break; n     } n     if (temp == d) { n       num1 = i; n       break; n     } n   } n   if (flag == 1) { n     cout <<  Impossible ; n     return 0; n   } else if (a + b + c + d == 0) { n     cout <<  0 ; n   } else if (a == 0 && b == 0 && c == 0) { n     for (i = 0; i < num1; i++) cout <<  1 ; n   } else if (b == 0 && c == 0 && d == 0) { n     for (i = 0; i < num0; i++) cout <<  0 ; n   } else { n     long long num0110 = num0 * num1; n     if (b + c != num0110) { n       cout <<  Impossible ; n       return 0; n     } n     long long end = c / num0; n     long long rem = c % num0; n     long long last = num1 - end - 1; n     if (rem == 0) last++; n     for (i = 0; i < end; i++) cout <<  1 ; n     for (i = 0; i < num0 - rem; i++) cout <<  0 ; n     if (rem != 0) cout <<  1 ; n     for (i = num0 - rem; i < num0; i++) cout <<  0 ; n     for (i = 0; i < last; i++) cout <<  1 ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long INF = 1e6; n long long M = 1e9 + 7; n void solve() { n   long long n; n   cin >> n; n   vector<int> a(n), b(n - 1), c(n - 2); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n - 1; i++) cin >> b[i]; n   for (int i = 0; i < n - 2; i++) cin >> c[i]; n   sort((a).begin(), (a).end()); n   sort((b).begin(), (b).end()); n   sort((c).begin(), (c).end()); n   long long x, y; n   bool f1 = true, f2 = true; n   for (int i = 0; i < n - 1; i++) { n     if (i < n - 2 && f2) { n       if (b[i] != c[i]) { n         y = b[i]; n         f2 = false; n       } n     } n     if (a[i] != b[i] && f1) { n       x = a[i]; n       f1 = false; n     } n   } n   if (f1) x = a[n - 1]; n   if (f2) y = b[n - 2]; n   cout << x <<   n  << y <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int lz[N << 2][10]; n long long sum[N << 2][10], tmp[10]; n void push_up(int rt) { n   for (int i = 0; i < 10; ++i) sum[rt][i] = 0; n   for (int i = 0; i < 10; ++i) sum[rt][lz[(rt << 1)][i]] += sum[(rt << 1)][i]; n   for (int i = 0; i < 10; ++i) n     sum[rt][lz[(rt << 1 | 1)][i]] += sum[(rt << 1 | 1)][i]; n } n void build(int rt, int l, int r) { n   for (int i = 0; i < 10; ++i) lz[rt][i] = i, sum[rt][i] = 0; n   if (l == r) { n     int t, tmp = 1; n     scanf( %d , &t); n     while (t) sum[rt][t % 10] += tmp, tmp *= 10, t /= 10; n     return; n   } n   int m = (l + r) >> 1; n   build((rt << 1), l, m); n   build((rt << 1 | 1), m + 1, r); n   push_up(rt); n } n void push_down(int rt) { n   for (int i = 0; i < 10; ++i) lz[(rt << 1)][i] = lz[rt][lz[(rt << 1)][i]]; n   for (int i = 0; i < 10; ++i) n     lz[(rt << 1 | 1)][i] = lz[rt][lz[(rt << 1 | 1)][i]]; n   memset(tmp, 0, sizeof(tmp)); n   for (int i = 0; i < 10; ++i) tmp[lz[rt][i]] += sum[rt][i]; n   for (int i = 0; i < 10; ++i) sum[rt][i] = tmp[i]; n   for (int i = 0; i < 10; ++i) lz[rt][i] = i; n } n void update(int rt, int l, int r, int ql, int qr, int x, int y) { n   if (ql <= l && qr >= r) { n     for (int i = 0; i < 10; ++i) n       if (lz[rt][i] == x) lz[rt][i] = y; n     return; n   } n   push_down(rt); n   int m = (l + r) >> 1; n   if (ql <= m) update((rt << 1), l, m, ql, qr, x, y); n   if (qr > m) update((rt << 1 | 1), m + 1, r, ql, qr, x, y); n   push_up(rt); n } n long long query(int rt, int l, int r, int ql, int qr) { n   long long res = 0; n   if (ql <= l && qr >= r) { n     for (int i = 0; i < 10; ++i) res += sum[rt][i] * lz[rt][i]; n     return res; n   } n   push_down(rt); n   int m = (l + r) >> 1; n   if (ql <= m) res += query((rt << 1), l, m, ql, qr); n   if (qr > m) res += query((rt << 1 | 1), m + 1, r, ql, qr); n   return res; n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   build(1, 1, n); n   while (q--) { n     int op, l, r, x, y; n     scanf( %d%d%d , &op, &l, &r); n     if (op == 1) { n       scanf( %d%d , &x, &y); n       update(1, 1, n, l, r, x, y); n     } else n       printf( %I64d n , query(1, 1, n, l, r)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 5; n int cnt[MAXN * 2]; n struct state { n   int len, link, first_pos; n   bool isClone; n   map<char, int> next; n }; n state st[MAXN * 2]; n int sz, last; n void sa_init() { n   for (int i = 0; i < sz; i++) st[i].next.clear(); n   st[0].len = 0; n   st[0].link = -1; n   sz = last = 0; n   ++sz; n } n void sa_extend(char c) { n   int cur = sz++; n   st[cur].len = st[last].len + 1; n   st[cur].first_pos = st[cur].len - 1; n   st[cur].isClone = false; n   int p = last; n   while (p != -1 && !st[p].next.count(c)) { n     st[p].next[c] = cur; n     p = st[p].link; n   } n   if (p == -1) { n     st[cur].link = 0; n   } else { n     int q = st[p].next[c]; n     if (st[p].len + 1 == st[q].len) { n       st[cur].link = q; n     } else { n       int clone = sz++; n       st[clone].len = st[p].len + 1; n       st[clone].next = st[q].next; n       st[clone].link = st[q].link; n       st[clone].first_pos = st[q].first_pos; n       st[clone].isClone = true; n       while (p != -1 && st[p].next[c] == q) { n         st[p].next[c] = clone; n         p = st[p].link; n       } n       st[q].link = st[cur].link = clone; n     } n   } n   last = cur; n } n bool cmp(int i, int j) { return st[i].len > st[j].len; } n int b[MAXN]; n int pow_cad(string &P) { n   int i = 0, j = -1; n   b[0] = -1; n   while (i < (P).size()) { n     while (j >= 0 && P[i] != P[j]) j = b[j]; n     i++, j++, b[i] = j; n   } n   int n = (P).size(); n   int k = n - b[n]; n   if (n % k == 0) return k; n   return n; n } n void match(string &t, int k) { n   int v = 0, l = 0, ans = 0; n   for (int i = 0; i < (t).size(); i++) { n     while (v && !st[v].next.count(t[i])) { n       v = st[v].link; n       l = st[v].len; n     } n     if (st[v].next.count(t[i])) { n       v = st[v].next[t[i]]; n       l++; n       if (l >= k) { n         if (st[st[v].link].len >= k) { n           v = st[v].link; n           l = st[v].len; n         } n         ans += cnt[v]; n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   sa_init(); n   for (auto c : s) { n     sa_extend(c); n   } n   for (int i = 1; i < sz; i++) { n     if (!st[i].isClone) cnt[i] = 1; n   } n   vector<int> node(sz); n   iota((node).begin(), (node).end(), 0); n   sort((node).begin(), (node).end(), cmp); n   for (int u : node) { n     if (~st[u].link) cnt[st[u].link] += cnt[u]; n   } n   int n; n   string pat; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> pat; n     int k = (pat).size(); n     int rep = pow_cad(pat); n     pat += pat.substr(0, rep - 1); n     cerr <<  pat  n          <<  _=_  << pat <<   n ; n     match(pat, k); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const double infd = 2e+9; n const int infi = 1ll << 30; n const long long infl = 1ll << 60; n const int mod = 1e+9 + 7; n template <class T> n inline T sqr(T x) { n   return x * x; n } n int main() { n   string a, b; n   int k; n   cin >> a >> b >> k; n   int n = a.length(); n   vector<int> p(k + 1); n   p[0] = 1; n   for (int i = 1; i <= k - 1; i++) p[i] = p[i - 1] * 1ll * (n - 1) % mod; n   vector<int> d[2]; n   d[0].resize(k + 1); n   d[1].resize(k + 1); n   d[0][0] = 1; n   d[1][0] = 0; n   for (int i = 1; i <= k; i++) { n     for (int m = 0; m < 2; m++) d[m][i] = (p[i - 1] - d[m][i - 1] + mod) % mod; n   } n   int result = 0; n   for (int i = 0; i < n; i++) { n     bool compactible = true; n     for (int j = 0; j < n; j++) { n       if (b[(i + j) % n] != a[j]) { n         compactible = false; n         break; n       } n     } n     if (compactible) { n       if (i == 0) n         result += d[0][k]; n       else n         result += d[1][k]; n       result %= mod; n     } n   } n   cout << result; n   return 0; n } n


#include <bits/stdc++.h> n #define ll long long n #define ull unsigned long long n #define vll vector<ll> n using namespace std; n inline void sol() { n   ll n, m; n   cin >> n >> m; n   vll a(n); n   for(ll i=0;i<n;i++){ n     cin >> a[i]; n   } n   vector<pair<int,double>> b(m); n   for(int i=0;i<m;i++){ n     int r; double p; n     cin >> r >> p; n     b[i] = {r, p}; n   } n   int rm; n   for(rm = n-1; rm >= 0 and a[rm] == rm + 1; rm--); n   if(rm == -1){ n     cout <<  1 n ; n     return; n   } n   double ans = 1; n   for(int i=0;i<m;i++){ n     if(b[i].first - 1 >= rm) n       ans *= (1 - b[i].second); n   } n   // cout << 1 - ans << endl; n   printf( %0.6lf n , 1 - ans); n } n int main() { n   // cin.tie(0); n   // ios_base::sync_with_stdio(false); n  n   int t; n   cin >> t; n   while(t--) n     sol(); n  n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long n, m, ans, u, v, start; n string opz; n int vis[2][200005]; n vector<int> grafo[2][200005]; n void dfs(int nodo, int mode) { n   vis[mode][nodo] = 1; n   for (auto i : grafo[mode][nodo]) { n     if (vis[mode][i] == 1) { n       cout <<  -1 n ; n       exit(0); n     } n     if (vis[mode][i] == 0) { n       dfs(i, mode); n     } n   } n   vis[mode][nodo] = 2; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   opz.resize(n); n   for (int i = 0; i < m; i++) { n     cin >> u >> v; n     grafo[0][u].push_back(v); n     grafo[1][v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     if (vis[0][i] == 0 && vis[1][i] == 0) { n       opz[i - 1] =  A ; n       ans++; n     } else { n       opz[i - 1] =  E ; n     } n     if (vis[0][i] == false) dfs(i, 0); n     if (vis[1][i] == false) dfs(i, 1); n   } n   cout << ans <<   n  << opz <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void handle_io() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n void solve() { n   int P; n   cin >> P; n   if (P == 5) n     cout << 2 <<     << 4 << endl; n   else n     cout << 2 <<     << P / 2 << endl; n } n int main() { n   handle_io(); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e6 + 7, Inf = 1e9 + 7; n int n, m, k = Inf, x, l, r, f, a[inf], b[inf], c[inf], d[inf]; n map<int, int> p[inf]; n vector<int> v; n string s; n int main() { n   cin >> n >> s; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 1; i < n; ++i) n     if (s[i] ==  L  && s[i - 1] ==  R ) k = min(k, (a[i] - a[i - 1]) / 2); n   if (k == Inf) n     cout << -1; n   else n     cout << k; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> all; n void backtrack(long long x, int cnt4, int cnt7) { n   if (x > 1e10) return; n   if (cnt4 == cnt7) all.push_back(x); n   long long nxt = x * 10 + 4; n   backtrack(nxt, cnt4 + 1, cnt7); n   nxt = x * 10 + 7; n   backtrack(nxt, cnt4, cnt7 + 1); n } n int main() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   backtrack(0, 0, 0); n   sort(all.begin(), all.end()); n   cout << *lower_bound(all.begin(), all.end(), n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 7; n const long long mod = 1e9 + 7; n int n; n int a; n int b; n string s; n int main() { n   cin >> n >> a >> b; n   cin >> s; n   if (s[b - 1] == s[a - 1]) { n     cout << 0; n     return 0; n   } n   int x = 1e7, y = 1e7; n   for (int i = b - 1; i < n; i++) { n     if (s[i] == s[b - 1]) { n       x = i; n       break; n     } n   } n   for (int i = b - 1; i >= 0; i--) { n     if (s[i] == s[b - 1]) { n       y = i; n       break; n     } n   } n   if (b - x < 0) { n     cout << b - x; n   } n   if (b - y < 0) { n     cout << b - y; n   } n   cout << min(b - x, b - y); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int X = 0, w = 1; n   char ch = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     X = (X << 3) + (X << 1) + ch -  0 , ch = getchar(); n   return X * w; n } n int n, tot, a[300007], val[300007 * 30], trie[300007 * 30][2], n     cnt[300007 * 30][2]; n void add(int x) { n   int cur = 0; n   for (int i = 29; i >= 0; i--) { n     int y = (x >> i) & 1; n     if (!trie[cur][y]) { n       trie[cur][y] = ++tot; n     } n     cnt[cur][y]++; n     cur = trie[cur][y]; n   } n   val[cur] = x; n } n int ask(int x) { n   int cur = 0; n   for (int i = 29; i >= 0; i--) { n     int y = (x >> i) & 1; n     if (cnt[cur][y]) { n       cnt[cur][y]--; n       cur = trie[cur][y]; n     } else { n       cnt[cur][y ^ 1]--; n       cur = trie[cur][y ^ 1]; n     } n   } n   return val[cur]; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n   } n   for (int i = 1; i <= n; i++) { n     int x = read(); n     add(x); n   } n   for (int i = 1; i <= n; i++) { n     printf( %d  , a[i] ^ ask(a[i])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e3 + 113; n long long n, k, cnt = 0; n int main() { n   cin >> k >> n; n   while (n > k) { n     if (n % k != 0) { n       cout <<  NO ; n       return 0; n     } n     n /= k; n     cnt++; n   } n   if (n == k) { n     cout <<  YES  << endl << cnt; n     return 0; n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n const int inf = 0x3f3f3f3f; n const int modu = 1e9 + 7; n int t, n, m, k, p, q, cnta, cntb; n double sum, res, ans; n struct pen { n   int money; n   int num; n   int pp; n } a[maxn]; n vector<int> ve[110]; n bool cmp(const pen a, const pen b) { n   return a.pp == b.pp ? a.money > b.money : a.pp < b.pp; n } n int main() { n   cnta = inf; n   sum = 0; n   res = 0; n   sum = 0; n   memset(a, 0, sizeof(a)); n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> p >> q; n     cnta = min(cnta, p); n     a[i].money = p; n     a[i].pp = q; n     a[i].num = i + 1; n   } n   sort(a, a + n, cmp); n   int j; n   for (j = 0; j < k - 1 && a[j].pp == 1; j++) { n     sum += a[j].money * 0.5; n   } n   if (a[k - 1].pp == 1) sum -= cnta * 0.5; n   for (j; j < n; j++) { n     sum += a[j].money; n   } n   printf( %.1f n , sum); n   for (int i = 0; i < k - 1; ++i) { n     printf( 1 %d n , a[i].num); n   } n   cout << n - k + 1; n   for (int i = k - 1; i < n; ++i) { n     printf(  %d , a[i].num); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int T; n   cin >> T; n   for (int t = 1; t <= T; ++t) { n     int64_t n; n     cin >> n; n     if (n == 1) { n       cout << 0 <<   n ; n       continue; n     } n     int count = 0; n     int first = 1; n     int64_t res = INT_MAX; n     for (int i = 1; i <= 63500; ++i) { n       first += 1; n       count += 1; n       int64_t sum = first; n       res = min(res, (((n - sum) + (first - 1)) / first) + count); n     } n     cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n, k; n     cin >> n >> k; n     int ans = 0; n     int count = 0; n     char x; n     int lwb = 0; n     bool f = false; n     for (int i = 0; i < n; i++) { n       cin >> x; n       if (x ==  1 ) { n         if (!f) { n           lwb = count; n         } else { n           ans += count / (k + 1); n           if (count / (k + 1) > 0 && count % (k + 1) != k) { n             ans--; n           } n         } n         count = 0; n         f = true; n       } else { n         count++; n       } n     } n     ans += lwb / (k + 1); n     ans += count / (k + 1); n     if (count == n && n % (k + 1) > 0) { n       ans++; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vec[200010]; n int a[200010], b[200010], color[200010]; n bool flag = false; n void dfs(int now, int pre, int col) { n   color[now] = col; n   for (int i = 0; i < vec[now].size(); i++) { n     int v = vec[now][i]; n     if (v == pre) continue; n     if (color[v] == -1) n       dfs(v, now, 1 - col); n     else if (color[v] == color[now]) n       flag = true; n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   memset(color, -1, sizeof(color)); n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     a[i] = u; n     b[i] = v; n     vec[u].push_back(v); n     vec[v].push_back(u); n   } n   dfs(1, -1, 0); n   if (flag) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < m; i++) { n     if (color[a[i]]) n       cout <<  0 ; n     else n       cout <<  1 ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int>> a(n, vector<int>(m)), n_s(1001, vector<int>(1001)), n       n_c(1001, vector<int>(1001)); n   vector<int> m_s(1001), m_c(1001); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) { n     vector<pair<int, int>> num; n     for (int j = 0; j < m; j++) num.push_back(make_pair(a[i][j], j)); n     sort(num.begin(), num.end()); n     int h = 0; n     for (int k = 0; k < num.size(); k++) { n       if (k && num[k].first != num[k - 1].first) h++; n       n_s[i][num[k].second] = h; n     } n     m_s[i] = h; n   } n   for (int i = 0; i < m; i++) { n     vector<pair<int, int>> num; n     for (int j = 0; j < n; j++) num.push_back(make_pair(a[j][i], j)); n     sort(num.begin(), num.end()); n     int h = 0; n     for (int k = 0; k < num.size(); k++) { n       if (k && num[k].first != num[k - 1].first) h++; n       n_c[i][num[k].second] = h; n     } n     m_c[i] = h; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int l_s = n_s[i][j]; n       int l_c = n_c[j][i]; n       int g_s = m_s[i] - l_s; n       int g_c = m_c[j] - l_c; n       cout << max(l_s, l_c) + 1 + max(g_s, g_c) <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mxn = 2e5; n const long long INF = 2e18; n const int32_t M = 1000000007; n const long double pie = acos(-1); n template <typename Arg1> n void __f(const char *name, Arg1 &&arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char *names, Arg1 &&arg1, Args &&...args) { n   const char *comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> gr[n + 1]; n   vector<long long> ingr[n + 1]; n   for (long long i = 1; i <= m; i++) { n     long long u, v; n     cin >> u >> v; n     gr[u].push_back(v); n     ingr[v].push_back(u); n   } n   long long k; n   cin >> k; n   vector<long long> p(k + 1); n   for (long long i = 1; i <= k; i++) { n     cin >> p[i]; n   } n   long long s = p[1], t = p[k]; n   vector<long long> d(n + 1, INF); n   d[t] = 0; n   queue<long long> q; n   vector<bool> vis(n + 1, false); n   q.push(t); n   vis[t] = true; n   while (!q.empty()) { n     long long v = q.front(); n     q.pop(); n     for (long long u : ingr[v]) { n       if (!vis[u]) { n         q.push(u); n         vis[u] = true; n       } n       d[u] = min(d[u], d[v] + 1); n     } n   } n   long long mn = 0, mx = 0; n   for (long long i = 1; i <= k - 1; i++) { n     long long curmn = INF; n     for (long long v : gr[p[i]]) { n       curmn = min(curmn, d[v]); n     } n     long long cntmn = 0; n     for (long long v : gr[p[i]]) { n       if (d[v] == curmn) cntmn++; n       if (cntmn == 2) break; n     } n     if (d[p[i + 1]] == curmn) { n       if (cntmn == 2) mx++; n     } else { n       mx++; n       mn++; n     } n   } n   cout << mn <<     << mx; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long _t = 1; n   for (long long i = 1; i <= _t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int max2(int a, int b) { return ((a > b) ? a : b); } n inline int min2(int a, int b) { return a < b ? a : b; } n inline int max3(int a, int b, int c) { return max2(a, max2(b, c)); } n long long int gcd(long long int a, long long int b) { n   while (b) { n     long long int temp = a; n     a = b; n     b = temp % b; n   } n   return a; n } n int main() { n   int n, w, k; n   scanf( %d , &k); n   scanf( %d , &n); n   scanf( %d , &w); n   int total = w * (w + 1); n   total = total / 2; n   total = total * k; n   int diff = total - n; n   if (diff > 0) n     printf( %d n , diff); n   else n     printf( %d n , 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-10; n template <typename T, typename S> n vector<T>& operator<<(vector<T>& a, S b) { n   a.push_back(b); n   return a; n } n template <typename T> n void operator>>(vector<T>& a, int b) { n   while (b--) n     if (!a.empty()) a.pop_back(); n } n bool isprime(int n) { n   if (n < 2) return false; n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n long long b_pow(long long x, long long n) { n   return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; n } n string itos(int n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> atk, def, ciel; n   for (int i = 0; i < n; i++) { n     string x; n     int y; n     cin >> x >> y; n     if (x ==  DEF ) n       def.push_back(y); n     else n       atk.push_back(y); n   } n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     ciel.push_back(x); n   } n   sort(atk.begin(), atk.end()); n   sort(def.begin(), def.end()); n   sort(ciel.rbegin(), ciel.rend()); n   int answer1 = 0; n   for (int i = 0; i < min(atk.size(), ciel.size()); i++) { n     if (atk[i] < ciel[i]) { n       answer1 += ciel[i] - atk[i]; n     } n   } n   int bad = 0; n   int answer2 = 0; n   sort(ciel.begin(), ciel.end()); n   int u[100] = {}; n   for (int j = 0; j < def.size(); j++) { n     int ok = 0; n     for (int i = 0; i < ciel.size(); i++) { n       if (!u[i] && def[j] < ciel[i]) { n         ok = 1; n         u[i] = true; n         break; n       } n     } n     if (!ok) bad = true; n   } n   for (int j = 0; j < atk.size(); j++) { n     int ok = 0; n     for (int i = 0; i < ciel.size(); i++) { n       if (!u[i] && atk[j] <= ciel[i]) { n         answer2 += ciel[i] - atk[j]; n         ok = 1; n         u[i] = true; n         break; n       } n     } n     if (!ok) bad = true; n   } n   for (int i = 0; i < m; i++) { n     if (!u[i]) { n       answer2 += ciel[i]; n     } n   } n   if (!bad) answer1 = max(answer1, answer2); n   cout << answer1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, step[1000000]; n long long a[2 * 1000000], pref[2 * 1000000 + 1]; n int calcLen(int strt) { n   int curr = strt, res = 0; n   while (curr < strt + n) curr += step[curr % n], res++; n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 0; i < (n); i++) cin >> a[i]; n   for (int i = 0; i < (n); i++) a[n + i] = a[i]; n   for (int i = 0; i < (2 * n); i++) pref[i + 1] = pref[i] + a[i]; n   for (int query = 0; query < (q); query++) { n     long long b; n     cin >> b; n     int lo = 0, hi = 0; n     while (lo < n) { n       while (lo < n && (hi == lo + n + 1 || pref[hi] > pref[lo] + b)) { n         step[lo] = hi - lo - 1; n         lo++; n       } n       hi++; n     } n     int res = INT_MAX; n     pair<int, int> mn = make_pair(INT_MAX, -1); n     for (int i = 0; i < (n); i++) n       if (step[(i + n - 1) % n] <= step[i]) mn = min(mn, make_pair(step[i], i)); n     for (int i = 0; i < (mn.first); i++) n       res = min(res, calcLen((mn.second + i) % n)); n     cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[60][60]; n bool check[60]; n int main() { n   int n, x; n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) { n     int t = 0; n     for (int j = 0; j < n; j++) check[a[i][j]] = true; n     for (int j = 1; j <= n; j++) n       if (check[j] == true) t++; n     if (t == n - 1) { n       x = i; n       break; n     } n   } n   for (int i = 0; i < n; i++) { n     if (a[x][i] == 0) n       cout << n <<    ; n     else n       cout << a[x][i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long long int mod = 1e9 + 7; n const long long int mod1 = 998244353; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long int rand(long long int l, long long int r) { n   uniform_int_distribution<long long int> uid(l, r); n   return uid(rng); n } n const long long int N = 2e5 + 5; n vector<pair<long long int, long long int> > v; n long long int dsu[N], sz[N], a[N]; n bool active[N]; n long long int odd = 0; n long long int root(long long int u) { n   return (dsu[u] == u) ? u : (dsu[u] = root(dsu[u])); n } n long long int n; n void uni(long long int u, long long int v) { n   if (u < 1 || u > n) return; n   u = root(u), v = root(v); n   if (u == v) return; n   odd -= (sz[u] & 1); n   odd -= (sz[v] & 1); n   sz[v] += sz[u]; n   odd += (sz[v] & 1); n   dsu[u] = v; n } n void solve() { n   cin >> n; n   for (long long int i = 1; i <= n; i++) dsu[i] = i, sz[i] = 1; n   for (long long int i = 1; i <= n; i++) cin >> a[i], v.push_back({a[i], i}); n   sort(v.begin(), v.end()); n   long long int pt = 0; n   while (1) { n     long long int val = v[pt].first; n     while (pt < n && v[pt].first == val) { n       active[v[pt].second] = 1; n       odd++; n       if (active[v[pt].second - 1]) { n         uni(v[pt].second - 1, v[pt].second); n       } n       if (active[v[pt].second + 1]) { n         uni(v[pt].second, v[pt].second + 1); n       } n       pt++; n     } n     if (odd && pt != n) { n       cout <<  NO  <<   n ; n       return; n     } n     if (pt == n) break; n   } n   cout <<  YES  <<   n ; n } n signed main() { n   ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   clock_t clk = clock(); n   long long int t = 1; n   while (t--) { n     solve(); n   } n   cerr <<   n  << (double)(clock() - clk) / CLOCKS_PER_SEC; n   return 0; n } n


#include<iostream> n using namespace std; n  n int main(){ n  int w,h,n,t; n  cin>>t; n  while(t--){ n   int s=1; n   cin>>w>>h>>n; n   int size=w*h; n   while(size>s){ n    if(w%2==0 && h%2!=0){ n     w=w/2; n     s=s*2; n    } n    else if(h%2==0 && w%2!=0){ n     h=h/2; n     s=s*2; n    } n    else if(w%2==0 && h%2==0){ n     w=w/2; n     s=s*2; n    } n    else { n     break; n    } n   } n   if(s>=n){ n    cout<< Yes <<  n ; n   } n   else{ n    cout<< No <<  n ; n   } n  } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int xa, ya; n   int xb, yb; n   int xc, yc; n   double k, b, y; n   scanf( %d %d , &xa, &ya); n   scanf( %d %d , &xb, &yb); n   scanf( %d %d , &xc, &yc); n   if (xa != xb) { n     k = (double)(yb - ya) / (xb - xa); n     b = ya - (double)(yb - ya) / (xb - xa) * xa; n     y = k * xc + b; n   } n   if (xa < xb) puts(y < yc ?  LEFT  : (yc < y ?  RIGHT  :  TOWARDS )); n   if (xa == xb) n     puts(xc == xa ?  TOWARDS  : ((xc < xa) != (yb < ya) ?  LEFT  :  RIGHT )); n   if (xa > xb) puts(y < yc ?  RIGHT  : (yc < y ?  LEFT  :  TOWARDS )); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10, inf = 1e9; n int n; n int a[maxn]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   long long lsum = 0, rsum = 0; n   for (int i = 1; i <= n / 2; ++i) lsum += a[i]; n   for (int i = n / 2 + 1; i <= n; ++i) rsum += a[i]; n   long long ans = lsum * lsum + rsum * rsum; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool prime(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n int main() { n   long long n, ans; n   cin >> n; n   if (prime(n)) n     ans = 1; n   else if (n % 2 == 0) n     ans = 2; n   else if (prime(n - 2)) n     ans = 2; n   else n     ans = 3; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, n, c[123456], a[1111234], b[1125678], d[123456], l, ll, s, t, k, n     m, xh, x, x1, y, z; n string p, r, w; n map<string, long long> q; n int main() { n   cin >> n >> k >> m; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   cout << a[m] - a[m - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, s, bigx[11], bigy[11], smlx[11], smly[11]; n int main() { n   scanf( %d%d%d%d , &n, &m, &k, &s); n   for (int i = 1; i <= k; i++) n     bigx[i] = bigy[i] = -2020, smlx[i] = smly[i] = 2 * 2020; n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) { n       int t; n       scanf( %d , &t); n       bigx[t] = max(bigx[t], i + j); n       bigy[t] = max(bigy[t], i - j); n       smlx[t] = min(smlx[t], i + j); n       smly[t] = min(smly[t], i - j); n     } n   int a, b, ans = -1; n   scanf( %d , &a); n   for (int i = 1; i < s; i++) { n     scanf( %d , &b); n     ans = max(ans, max(abs(bigx[a] - smlx[b]), abs(bigx[b] - smlx[a]))); n     ans = max(ans, max(abs(bigy[a] - smly[b]), abs(bigy[b] - smly[a]))); n     a = b; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:500000000 ) n using namespace std; n int A[105]; n int f[105]; n int main() { n   int n, i, p, x, y; n   cin >> n; n   for (i = 0; i < n; i++) cin >> A[i], f[A[i]] = i; n   int q; n   cin >> q; n   while (q--) { n     scanf( %d %d %d , &p, &x, &y); n     if (p == 1) { n       int res = 0; n       while (x <= y) { n         while (x < y && f[x + 1] > f[x]) x++; n         x++; n         res++; n       } n       printf( %d n , res); n     } else { n       x--; n       y--; n       int L = A[x], R = A[y]; n       swap(A[x], A[y]); n       f[L] = y; n       f[R] = x; n     } n   } n } n


#include <bits/stdc++.h> n #include<limits> n using namespace std; n #define ll long long n #define vi vector<int> n #define vll vector<long long> n #define pi pair<int,int> n #define pll pair<ll,ll> n #define FASTIO   ios_base::sync_with_stdio(false);cin.tie(NULL); n #define unmap unordered_map n #define pb push_back n ll max2(ll x, ll y) { n  if (x > y) n   return x; n  else n   return y; n } n ll min2(ll x, ll y) { n  if (x < y) n   return x; n  else n   return y; n } n ll gcd(ll a, ll b) { n  if (b == 0) n   return a; n  return gcd(b, a % b); n } n void solve(){ n  n     unsigned ll n; n     cin>>n; n     unsigned ll p=1,sum=0; n  n     while(n>1){ n         sum+=p; n         p*=2; n         n>>=1; n     } n     cout<<sum<<endl; n } n  n int main() { n  FASTIO n  ll t = 1; n  int q = 0; n  n  n  cin >> t; n  n  while (t--) { n             solve(); n  } n  n  return 0; n  n } n  n  n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10 * 1000 + 10, maxm = (1 << 17), mod = (int)1e9 + 7, n           hash = 701, inf = (1 << 29) + 3; n const double pi = 3.14159265359, ee = 2.71828; n int dis[30][30], d[(1 << 21)], dd[maxn], l[110], n, m, k, u; n vector<int> q, bf; n void bfs(int v) { n   dd[v] = 0, bf.push_back(v); n   for (int i = 0; i < bf.size(); i++) { n     v = bf[i]; n     for (int j = 0; j < m; j++) { n       if (v + l[j] <= n) n         if (dd[v + l[j]] > dd[v] + 1) n           dd[l[j] + v] = dd[v] + 1, bf.push_back(v + l[j]); n       if (v - l[j] >= 0) n         if (dd[v - l[j]] > dd[v] + 1) n           dd[v - l[j]] = dd[v] + 1, bf.push_back(v - l[j]); n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> k >> m; n   for (int i = 0; i < k; i++) cin >> u, q.push_back(u), q.push_back(u - 1); n   for (int i = 0; i < m; i++) cin >> l[i]; n   for (int i = 0; i < q.size(); i++) { n     for (int j = 0; j <= n; j++) dd[j] = inf; n     bfs(q[i]); n     for (int j = 0; j < q.size(); j++) dis[i][j] = dd[q[j]]; n   } n   for (int i = 1; i < (1 << q.size()); i++) d[i] = inf, u = 0; n   for (int i = 1; i < (1 << q.size()); i++, u = 0) { n     while (!(i & (1 << u))) u++; n     for (int j = 0; j < q.size(); j++) n       if (j != u && (i & (1 << j))) n         d[i] = min(d[i], d[i ^ (1 << u) ^ (1 << j)] + dis[u][j]); n   } n   cout << (d[(1 << q.size()) - 1] == inf ? -1 : d[(1 << q.size()) - 1]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<long long> pos, neg, a; n     for (int i = 0; i < n; i++) { n       long long x; n       cin >> x; n       a.push_back(x); n       if (x >= 0) n         pos.push_back(x); n       else n         neg.push_back(x); n     } n     sort((pos).begin(), (pos).end()); n     reverse((pos).begin(), (pos).end()); n     sort((neg).begin(), (neg).end()); n     long long ans = 1; n     for (int i = 0; i < 5; i++) { n       ans *= a[i]; n     } n     for (int m = 0; m <= 4 and m <= neg.size(); m += 2) { n       int p = 5 - m; n       if (p > pos.size()) continue; n       long long pro = 1; n       for (int i = 0; i < p; i++) pro *= pos[i]; n       for (int i = 0; i < m; i++) pro *= neg[i]; n       ans = max(ans, pro); n     } n     if (neg.size() >= 5) { n       reverse((neg).begin(), (neg).end()); n       long long temp = 1; n       for (int i = 0; i < 5; i++) { n         temp *= neg[i]; n       } n       ans = max(ans, temp); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int tree[4 * N]; n int n, m; n void update(int root, int lo, int hi, int u, int v) { n   if (v < lo || u > hi) return; n   if (u <= lo && hi <= v) { n     tree[root]++; n     return; n   } n   int mid = (lo + hi) / 2, left = root * 2, right = left + 1; n   update(left, lo, mid, u, v); n   update(right, mid + 1, hi, u, v); n   tree[root] = tree[left] + tree[right]; n } n int query(int root, int lo, int hi, int u, int v) { n   if (v < lo || u > hi) return 0; n   if (u <= lo && hi <= v) return tree[root]; n   int mid = (lo + hi) / 2, left = root * 2, right = left + 1; n   return query(left, lo, mid, u, v) + query(right, mid + 1, hi, u, v); n } n int main() { n   scanf( %d%d , &n, &m); n   int i, cur = 0, next, start, end, p1, p2; n   long long ans = 1; n   for (i = 1; i <= n; ++i) { n     next = (cur + m) % n; n     start = min(cur, next) + 1; n     end = max(cur, next) + 1; n     p1 = query(1, 1, n, start + 1, end - 1); n     p2 = 2 * (i - 1) - query(1, 1, n, start, start) - query(1, 1, n, end, end) - n          p1; n     ans += min(p1, p2) + 1; n     update(1, 1, n, start, start); n     update(1, 1, n, end, end); n     cur = next; n     printf( %lld  , ans); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n double ed(double x1, double y1, double x2, double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   double ax, ay, bx, by, tx, ty; n   int n; n   cin >> ax >> ay >> bx >> by >> tx >> ty >> n; n   double td = 0; n   vector<pair<double, int>> p1(n), p2(n); n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     td += 2 * ed(x, y, tx, ty); n     p1[i] = {ed(x, y, ax, ay) - ed(x, y, tx, ty), i}; n     p2[i] = {ed(x, y, bx, by) - ed(x, y, tx, ty), i}; n   } n   sort(p1.begin(), p1.end()); n   sort(p2.begin(), p2.end()); n   double ans = min(td + p1[0].first, td + p2[0].first); n   if (n == 1) { n     cout << setprecision(12) << ans; n     return 0; n   } n   if (p1[0].second != p2[0].second) { n     ans = min(ans, td + p1[0].first + p2[0].first); n   } else { n     ans = min(ans, min(td + p1[0].first + p2[1].first, n                        td + p1[1].first + p2[0].first)); n   } n   cout << setprecision(12) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> tree[4 * 100005 + 10]; n int a[100005]; n int dp[100005]; n int n, s, l; n int f[4 * 100005 + 10]; n void init(int node, int b, int e) { n   if (b == e) { n     tree[node].first = tree[node].second = a[b]; n     return; n   } n   int mid = (b + e) / 2; n   int lft = node * 2; n   int rght = lft + 1; n   init(lft, b, mid); n   init(rght, mid + 1, e); n   tree[node].first = min(tree[lft].first, tree[rght].first); n   tree[node].second = max(tree[lft].second, tree[rght].second); n   return; n } n pair<int, int> query(int node, int b, int e, int i, int j) { n   if (i <= b && j >= e) return tree[node]; n   int mid = (b + e) / 2; n   int lft = node * 2; n   int rght = lft + 1; n   if (j <= mid) n     return query(lft, b, mid, i, j); n   else if (mid < i) n     return query(rght, mid + 1, e, i, j); n   else { n     pair<int, int> c, d, ans; n     c = query(lft, b, mid, i, j); n     d = query(rght, mid + 1, e, i, j); n     ans.first = min(c.first, d.first); n     ans.second = max(c.second, d.second); n     return ans; n   } n } n void update_(int node, int b, int e, int indx) { n   if (b == e) { n     f[node] = dp[indx]; n     return; n   } n   int mid = (b + e) / 2; n   if (indx <= mid) n     update_(node * 2, b, mid, indx); n   else n     update_(node * 2 + 1, mid + 1, e, indx); n   f[node] = min(f[node * 2], f[node * 2 + 1]); n } n int get(int node, int b, int e, int i, int j) { n   if (b >= i && j >= e) return f[node]; n   int mid = (b + e) / 2; n   int c, d; n   if (j <= mid) n     return get(node * 2, b, mid, i, j); n   else if (mid < i) n     return get(node * 2 + 1, mid + 1, e, i, j); n   else { n     c = get(node * 2, b, mid, i, j); n     d = get(node * 2 + 1, mid + 1, e, i, j); n     return min(c, d); n   } n } n int solve() { n   pair<int, int> tmp; n   if (l > n) return 20000000; n   tmp = query(1, 1, n, 1, l); n   if (tmp.second - tmp.first <= s) n     dp[l] = 1; n   else n     return 20000000; n   int j = 1; n   update_(1, 1, n, l); n   for (int i = l + 1; i <= n; i++) { n     while (j <= i) { n       tmp = query(1, 1, n, j, i); n       if (tmp.second - tmp.first <= s) break; n       j++; n     } n     if (i - j + 1 >= l) { n       if (j == 1) n         dp[i] = 1; n       else n         dp[i] = get(1, 1, n, j - 1, i - l) + 1; n     } else n       dp[i] = 20000000; n     update_(1, 1, n, i); n   } n   return dp[n]; n } n int main() { n   cin >> n >> s >> l; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   init(1, 1, n); n   for (int i = 0; i <= 4 * n; i++) f[i] = 20000000; n   int res = solve(); n   if (res >= 20000000) n     cout << -1; n   else n     cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n const long long int mod = 1000000007; n long long int f(long long int x, long long int k) { n   long long int res = 1; n   while (k) { n     if (k & 1) res = res * x % mod; n     x = x * x % mod; n     k >>= 1; n   } n   return res; n } n int main() { n   cin >> n; n   n++; n   if (n == 1) n     cout <<  1 ; n   else { n     long long int a = f(4, n - 2); n     long long int b = f(2, n - 2); n     a = a * 2 % mod; n     cout << (a + b) % mod; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int long long> fact(1000001); n int long long sm, a, b, n; n bool check() { n   int long long m = sm; n   while (m) { n     if (m % 10 != b && m % 10 != a) return 0; n     m /= 10; n   } n   return 1; n } n int long long inc(int long long t, int long long p) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     int long long e = inc(t, p / 2); n     return (e * e) % 1000003; n   } else { n     return (inc(t, p - 1) * t) % 1000003; n   } n } n int long long comb(int long long i, int long long m) { n   return (fact[m] * n           (inc(((fact[m - i] * fact[i]) % 1000003), 1000003 - 2) % 1000003)) % n          1000003; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int long long a, b; n   cin >> a >> b; n   fact[0] = fact[1] = 1; n   for (long long i = 2; i <= 1000000; i++) n     fact[i] = i * fact[i - 1], fact[i] %= 1000003; n   cout << comb(b, a + b) - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double net, res; n   int d, h, vel, e; n   cin >> d >> h >> vel >> e; n   net = e - (vel / (3.14159265358979323846 * d * d / 4.0)); n   res = -h / net; n   if (res <= 0 || res > 10000) n     cout <<  NO n ; n   else n     cout <<  YES n  << fixed << setprecision(12) << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, comp_num; n vector<vector<int> > g; n vector<bool> used; n vector<vector<int> > comp; n vector<long long int> dv; n vector<long long int> dv_in_comp; n vector<int> components; n void dfs(int v) { n   used[v] = true; n   comp[comp.size() - 1].push_back(v); n   dv_in_comp[dv_in_comp.size() - 1] += dv[v]; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (!used[to]) dfs(to); n   } n } n void find_comps() { n   for (int i = 0; i < n; i++) { n     if (!used[i]) { n       comp.push_back(vector<int>()); n       dv_in_comp.push_back(0); n       dfs(i); n     } n   } n } n int main() { n   cin >> n >> m; n   g.resize(n); n   dv.resize(n, 0); n   used.resize(n, false); n   components.resize(n, -1); n   int ui, vi; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &ui, &vi); n     ui--; n     vi--; n     g[ui].push_back(vi); n     g[vi].push_back(ui); n     dv[ui]++; n     dv[vi]++; n   } n   find_comps(); n   int total = 0; n   for (int i = 0; i < comp.size(); i++) { n     if (dv_in_comp[i] > 2 * (comp[i].size() - 1)) { n       total += 0; n     } else n       total++; n   } n   cout << total; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long sl = s.length(); n   long long one = 0; n   long long two = 0; n   long long three = 0; n   long long i; n   for (i = 0; i < sl; i = i + 2) { n     if (s[i] -  0  == 1) { n       one++; n     } else if (s[i] -  0  == 2) { n       two++; n     } else { n       three++; n     } n   } n   vector<char> v; n   for (i = 0; i < one; i++) { n     v.push_back( 1 ); n     v.push_back( + ); n   } n   for (i = 0; i < two; i++) { n     v.push_back( 2 ); n     v.push_back( + ); n   } n   for (i = 0; i < three; i++) { n     v.push_back( 3 ); n     v.push_back( + ); n   } n   if (v[v.size() - 1] ==  + ) { n     for (i = 0; i < v.size() - 1; i++) { n       cout << v[i]; n     } n   } else { n     for (i = 0; i < v.size(); i++) { n       cout << v[i]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400010; n int h[N], ne[N], e[N], idx; n int n; n long long sz[N], dp[N], ans; n void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; } n void dfs(int u, int fa) { n   sz[u] = 1; n   for (int i = h[u]; ~i; i = ne[i]) { n     int j = e[i]; n     if (j == fa) continue; n     dfs(j, u); n     sz[u] += sz[j]; n   } n } n void dfs2(int u, int fa) { n   for (int i = h[u]; ~i; i = ne[i]) { n     int j = e[i]; n     if (j == fa) continue; n     dp[j] = dp[u] + n - 2 * sz[j]; n     dfs2(j, u); n   } n } n void init(int u, int fa) { n   ans += sz[u]; n   for (int i = h[u]; ~i; i = ne[i]) { n     int j = e[i]; n     if (j == fa) continue; n     init(j, u); n   } n } n int main() { n   cin >> n; n   memset(h, -1, sizeof h); n   for (int i = 1; i < n; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     add(a, b), add(b, a); n   } n   dfs(1, 1); n   init(1, 1); n   dp[1] = ans; n   dfs2(1, 1); n   long long maxx = 0; n   for (int i = 1; i <= n; ++i) maxx = max(maxx, dp[i]); n   cout << maxx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b, long long& x, long long& y) { n   cerr <<  a ==   << a <<   b ==   << b << endl; n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long d = gcd(b % a, a, x1, y1); n   cerr <<  x1 ==   << x1 <<   y1 ==   << y1 << endl; n   x = y1 - (b / a) * x1; n   y = x1; n   cerr <<  x ==   << x <<   y ==   << y << endl; n   return d; n } n long long fact(long long a) { n   long long ans = 1; n   for (int i = 2; i <= a; ++i) ans = (ans * i) % 1000000007; n   return ans; n } n int main() { n   int n; n   cin >> n; n   long long ans = fact(2 * n - 1) % 1000000007; n   long long tmp = (fact(n) * fact((2 * n - 1) - n)) % 1000000007; n   long long x, y; n   cerr << tmp <<     << ans << endl; n   gcd(tmp, 1000000007, x, y); n   cerr << x << endl; n   while (x < 0) x += 1000000007; n   ans = (ans * (x % 1000000007)) % 1000000007; n   if (n == 2) n     cout << 4 << endl; n   else n     cout << (ans * 2 - n) % 1000000007; n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n inline void checkmax(T &x, T y) { n   if (x < y) x = y; n } n template <class T> n inline void checkmin(T &x, T y) { n   if (x > y) x = y; n } n template <class T> n inline T Min(T x, T y) { n   return (x > y ? y : x); n } n template <class T> n inline T Max(T x, T y) { n   return (x < y ? y : x); n } n using namespace std; n vector<int> a[111111], ans; n int n, m, b[111111], c[111111]; n int main() { n   while (scanf( %d%d , &n, &m) == 2) { n     for (int i = 1; i <= n; ++i) a[i].clear(); n     ans.clear(); n     memset(c, 0, sizeof(c)); n     for (int i = 1; i <= m; ++i) { n       int u, v; n       scanf( %d%d , &u, &v); n       a[u].push_back(v); n       a[v].push_back(u); n     } n     for (int i = 1; i <= n; ++i) scanf( %d , b + i); n     queue<int> q; n     for (int i = 1; i <= n; ++i) n       if (!b[i]) q.push(i); n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       if (b[v] == c[v]) n         ans.push_back(v); n       else n         continue; n       c[v]++; n       for (int i = 0; i < a[v].size(); ++i) { n         c[a[v][i]]++; n         if (b[a[v][i]] == c[a[v][i]]) q.push(a[v][i]); n       } n     } n     printf( %d n , ans.size()); n     for (int i = 0; i < ans.size(); ++i) printf( %d  , ans[i]); n     if (ans.size()) puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double Fib(double x, double z); n string BinDec(unsigned long long x); n string StringInt(long long x); n unsigned long long StringInt(string x); n unsigned long long BinDec(string x); n unsigned long long POWMOD(unsigned long long x, unsigned long long y, n                           unsigned long long mod); n unsigned long long POWE(long long, long long); n long long fast_pow(long long a, long long b, long long mod) { n   if (b == 0) return 1ll; n   long long ans = 1; n   if (b % 2) return ans = (fast_pow(a, b - 1, mod) * a) % mod; n   ans = (fast_pow(a, b / 2, mod) % mod); n   return ((ans * ans) % mod); n } n int tree[100000 * 4] = {}, arr[100200] = {}, x; n void build(int in, int s, int e) { n   if (s == e) { n     tree[in] = s; n     return; n   } n   int m = (s + e) / 2; n   build(in * 2, s, m); n   build(in * 2 + 1, m + 1, e); n   if (arr[tree[in * 2]] > arr[tree[in * 2 + 1]]) n     tree[in] = tree[in * 2 + 1]; n   else n     tree[in] = tree[in * 2]; n } n void up(int in, int s, int e, int ind, int v) { n   if (s > ind || ind > e) return; n   if (s == e && s == ind) { n     arr[s] = v; n     tree[in] = s; n     return; n   } n   int m = (s + e) / 2; n   up(in * 2, s, m, ind, v); n   up(in * 2 + 1, m + 1, e, ind, v); n   if (arr[tree[in * 2]] > arr[tree[in * 2 + 1]]) n     tree[in] = tree[in * 2 + 1]; n   else n     tree[in] = tree[in * 2]; n } n long long qur(int in, int s, int e, int f, int l) { n   if (s > e || s > l || f > e) return 100001; n   if (f <= s && l >= e) return tree[in]; n   int m = (s + e) / 2; n   long long v, i = qur(in * 2, s, m, f, l), j = qur(in * 2 + 1, m + 1, e, f, l); n   if (arr[i] > arr[j]) n     v = j; n   else n     v = i; n   return v; n } n bool p[100210] = {}; n int S = 0, SS = 0; n vector<int> v; n vector<pair<int, int> > ans; n pair<int, int> vv[100010] = {}; n void sort() { n   int t; n   for (int i = 0; i < x; i++) { n     int j = qur(1, 0, x - 1, i, x - 1); n     if (i == j) continue; n     int k = j - i + 1; n     if (!p[k]) { n       ans.push_back({i, j}); n       t = arr[i]; n       up(1, 0, x - 1, i, arr[j]); n       up(1, 0, x - 1, j, t); n     } else if (k % 2 == 0) { n       if (p[j - i - vv[k].second + 1]) S++; n       ans.push_back({i + vv[k].second, j}); n       t = arr[i + vv[k].second]; n       up(1, 0, x - 1, i + vv[k].second, arr[j]); n       up(1, 0, x - 1, j, t); n       if (p[i + vv[k].second + -i - 1 + 1]) S++; n       ans.push_back({i + 1, i + vv[k].second}); n       t = arr[i + 1]; n       up(1, 0, x - 1, i + 1, arr[i + vv[k].second]); n       up(1, 0, x - 1, i + vv[k].second, t); n       if (p[i + 1 - i + 1]) S++; n       ans.push_back({i, i + 1}); n       t = arr[i + 1]; n       up(1, 0, x - 1, i + 1, arr[i]); n       up(1, 0, x - 1, i, t); n     } else { n       k++; n       i--; n       if (p[j - i - vv[k].second + 1]) S++; n       ans.push_back({i + vv[k].second, j}); n       t = arr[i + vv[k].second]; n       up(1, 0, x - 1, i + vv[k].second, arr[j]); n       up(1, 0, x - 1, j, t); n       if (p[i + vv[k].second + -i - 1 + 1]) SS++; n       ans.push_back({i + 1, i + vv[k].second}); n       t = arr[i + 1]; n       up(1, 0, x - 1, i + 1, arr[i + vv[k].second]); n       up(1, 0, x - 1, i + vv[k].second, t); n       i++; n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   for (long long i = 2; i <= 100000; i++) { n     if (!p[i]) { n       v.push_back(i); n       for (long long j = i * i; j <= 100000; j += i) p[j] = 1; n     } n   } n   for (int i = 4; i <= 100000; i += 2) { n     for (int j = 0; j < v.size(); j++) { n       if (!p[i - v[j]]) { n         vv[i] = {i - v[j], v[j]}; n         break; n       } n     } n   } n   arr[100001] = 1e10; n   cin >> x; n   for (int i = 0; i < x; i++) cin >> arr[i]; n   build(1, 0, x - 1); n   int j = 0; n   sort(); n   if (S != 0 || SS != 0) cout << S <<     << SS << endl; n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first + 1 <<     << ans[i].second + 1 <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <class T> n using vv = vector<vector<T>>; n template <class T> n inline bool MX(T &l, const T &r) { n   return l < r ? l = r, 1 : 0; n } n template <class T> n inline bool MN(T &l, const T &r) { n   return l > r ? l = r, 1 : 0; n } n const ll MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(0); n   int n; n   cin >> n; n   string opes(n,  . ); n   vector<pii> clds(n); n   for (int(i) = 0; (i) < (n); ++(i)) { n     string s; n     cin >> s >> clds[i].first; n     if (s ==  IN ) { n       opes[i] =  > ; n       continue; n     } n     --clds[i].first; n     if (s ==  NOT ) { n       opes[i] =  ~ ; n       continue; n     } n     cin >> clds[i].second; n     --clds[i].second; n     if (s ==  AND ) { n       opes[i] =  & ; n     } else if (s ==  OR ) { n       opes[i] =  | ; n     } else if (s ==  XOR ) { n       opes[i] =  ^ ; n     } else n       return 1; n   }; n   vector<int> vals(n); n   { n     function<int(int)> dfs = [&](int v) { n       if (opes[v] ==  & ) { n         vals[v] = dfs(clds[v].first) & dfs(clds[v].second); n       } else if (opes[v] ==  | ) { n         vals[v] = dfs(clds[v].first) | dfs(clds[v].second); n       } else if (opes[v] ==  ^ ) { n         vals[v] = dfs(clds[v].first) ^ dfs(clds[v].second); n       } else if (opes[v] ==  ~ ) { n         vals[v] = 1 - dfs(clds[v].first); n       } else n         vals[v] = clds[v].first; n       return vals[v]; n     }; n     dfs(0); n   }; n   string re(n, vals[0] +  0 ); n   ; n   { n     function<void(int)> dfs = [&](int v) { n       if (opes[v] ==  & ) { n         if (vals[clds[v].first]) dfs(clds[v].second); n         if (vals[clds[v].second]) dfs(clds[v].first); n       } else if (opes[v] ==  | ) { n         if (!vals[clds[v].first]) dfs(clds[v].second); n         if (!vals[clds[v].second]) dfs(clds[v].first); n       } else if (opes[v] ==  ^ ) { n         dfs(clds[v].first); n         dfs(clds[v].second); n       } else if (opes[v] ==  ~ ) { n         dfs(clds[v].first); n       } else n         re[v] ^= 1; n     }; n     dfs(0); n   } n   for (int(i) = 0; (i) < (n); ++(i)) n     if (opes[i] ==  > ) cout << re[i]; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1000000009; n const double eps = 1e-9; n const int mod = 1000000007; n int nos[100001], n, pos = 0; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) scanf( %d , &nos[i]); n   sort(nos, nos + n); n   for (int i = 0; i < n; ++i) n     if (pos + 1 <= nos[i]) ++pos; n   printf( %d n , pos + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, qu; n int a[100011]; n int st[100011], en[100011]; n int pos[100011]; n int rt[100011], nxt[100011]; n int step[100011]; n bool vis[100011]; n vector<int> v[100011], vv; n queue<int> q; n void bunhae_interval() { n   int r = n % m, mm = n / m; n   st[0] = 1; n   for (int i = 0; i < m; i++) { n     if (i) st[i] = en[i - 1] + 1; n     en[i] = st[i] + mm - 1; n     if (i < r) en[i]++; n     for (int j = st[i]; j <= en[i]; j++) pos[j] = i; n   } n } n void update(int x) { n   int p = pos[x]; n   for (int i = st[p]; i <= x; i++) vis[i] = 0; n   vis[x] = 1; n   q.push(x); n   while (!q.empty()) { n     int y = q.front(); n     q.pop(); n     for (int i = 0; i < v[y].size(); i++) { n       int z = v[y][i]; n       if (vis[z]) continue; n       vis[z] = 1; n       step[z] = step[y] + 1; n       rt[z] = rt[x]; n       q.push(z); n     } n   } n } n int main() { n   scanf( %d %d , &n, &qu); n   m = sqrt(n); n   bunhae_interval(); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = n; i >= 1; i--) { n     int j = i + a[i]; n     if (j > n || pos[j] != pos[i]) n       nxt[i] = j, rt[i] = i, step[i] = 0; n     else n       rt[i] = rt[j], step[i] = step[j] + 1, v[j].push_back(i); n   } n   while (qu--) { n     int tp, x, y; n     scanf( %d %d , &tp, &x); n     if (tp == 1) { n       int cnt = 1, en = x; n       while (1) { n         if (rt[en] == en) { n           if (nxt[en] > n) n             break; n           else n             cnt++, en = nxt[en]; n         } else { n           cnt += step[en], en = rt[en]; n         } n       } n       printf( %d %d n , en, cnt); n     } else { n       int xx = x + a[x]; n       if (xx > n) goto la; n       vv.clear(); n       for (int i = 0; i < v[xx].size(); i++) n         if (v[xx][i] != x) vv.push_back(v[xx][i]); n       v[xx].clear(); n       for (int i = 0; i < vv.size(); i++) v[xx].push_back(vv[i]); n     la:; n       scanf( %d , &y); n       int z = x + y; n       if (z > n || pos[z] != pos[x]) n         nxt[x] = z, rt[x] = x, step[x] = 0; n       else n         rt[x] = rt[z], step[x] = step[z] + 1, v[z].push_back(x); n       a[x] = y; n       update(x); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int powers[1000100]; n vector<pair<int, int> > edges; n unsigned long long hashes[1000100] = {0}; n unsigned long long ts[1000100] = {0}; n int main(void) { n   powers[0] = 1; n   for (int i = 1; i <= 1000000; i++) n     powers[i] = (powers[i - 1] * (unsigned long long)41) % 291598227841757; n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     hashes[u] = (hashes[u] + powers[v]) % 291598227841757; n     hashes[v] = (hashes[v] + powers[u]) % 291598227841757; n     ts[u - 1] = hashes[u]; n     ts[v - 1] = hashes[v]; n     edges.push_back(make_pair(u, v)); n   } n   sort(ts, ts + n); n   int cur = 0; n   long long int ans = 0; n   for (int i = 1; i < n; i++) { n     if (ts[i] != ts[cur]) { n       long long int n = i - cur; n       ans += n * (n - 1) / 2; n       cur = i; n     } n   } n   long long int a = n - cur; n   ans += a * (a - 1) / 2; n   for (int i = 0; i < m; i++) { n     int u = edges[i].first, v = edges[i].second; n     unsigned long long hu = (hashes[u] + powers[u]) % 291598227841757; n     unsigned long long hv = (hashes[v] + powers[v]) % 291598227841757; n     if (hu == hv) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 1e5 + 1; n int n, m; n vector<int> adj[N]; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int rand(int x) { return uniform_int_distribution<int>(0, x - 1)(rng); } n int d[N]; n bool inq[N]; n bool vis3[N]; n bool ok = true; n void dfs3(int id) { n   inq[id] = true; n   vis3[id] = true; n   for (auto x : adj[id]) { n     if (!vis3[x]) { n       dfs3(x); n     } else if (!inq[x]) n       ok = false; n   } n   inq[id] = false; n } n bool arin(int x) { n   for (int i = 1; i <= n; i++) vis3[i] = inq[i] = false; n   ok = true; n   dfs3(x); n   return ok; n } n bool vis[N]; n vector<int> ch[N]; n int par[N]; n int dx[N], dy[N]; n int cs[N]; n int up[N]; n void dfs(int id) { n   vis[id] = true; n   up[id] = id; n   for (auto x : adj[id]) { n     if (!vis[x]) { n       d[x] = d[id] + 1; n       par[x] = id; n       ch[id].push_back(x); n       cs[id] = x; n       dfs(x); n     } else { n       dx[x]--; n       dx[id]++; n       if (d[up[id]] > d[x]) up[id] = x; n     } n   } n   for (auto x : ch[id]) { n     if (d[up[x]] < d[up[id]]) up[id] = up[x]; n   } n } n int dfs4(int id) { n   int duh = dx[id]; n   for (auto x : ch[id]) duh += dfs4(x); n   dy[id] = duh; n   return duh; n } n bool good[N]; n int rt; n void dfs2(int id) { n   if (dy[id] <= 1) good[id] = good[up[id]]; n   if (id == rt) good[id] = true; n   for (auto x : ch[id]) { n     dfs2(x); n   } n } n void solve() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     adj[i].clear(); n     ch[i].clear(); n     dx[i] = dy[i] = 0; n     good[i] = false; n   } n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n   } n   int x = 0; n   for (int i = 1; i <= 100; i++) { n     x = rand(n) + 1; n     if (arin(x)) break; n     x = 0; n   } n   if (x == 0) { n     cout <<  -1 n ; n     return; n   } n   for (int i = 1; i <= n; i++) vis[i] = false; n   d[x] = 0; n   dfs(x); n   rt = x; n   dfs4(x); n   dfs2(x); n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     if (good[i]) cnt++; n   if (cnt * 5 < n) { n     cout <<  -1 n ; n     return; n   } n   for (int i = 1; i <= n; i++) n     if (good[i]) cout << i <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n const int maxN = 3e5 + 5; n int main() { n   if (fopen( iput.txt ,  r )) { n     freopen( iput.txt ,  r , stdin); n     freopen( oput.txt ,  w , stdout); n   } n   int n; n   scanf( %d , &n); n   printf( %d n , n / 2 * 2 + n - n / 2); n   printf( 2  ); n   for (int i = 4; i <= n; i += 2) printf( %d  , i); n   for (int i = 1; i <= n; i += 2) printf( %d  , i); n   for (int i = 2; i <= n; i += 2) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> st[200000 + 7]; n set<int>::iterator it; n vector<int> graph[200000 + 7], edge[200000 + 7]; n pair<int, int> edges[200000 + 7], path[200000 + 7]; n int sid[200000 + 7], prio[200000 + 7], pr_id, can[200000 + 7], neg[200000 + 7], n     rev[200000 + 7]; n long long weg[200000 + 7], uwg[200000 + 7]; n void dfs(int u, int edg) { n   int v, ed; n   long long wg = 0; n   neg[u] = edg; n   for (int i = 0; i < graph[u].size(); i++) { n     v = graph[u][i]; n     ed = edge[u][i]; n     dfs(v, ed); n     wg += weg[v]; n   } n   if (u == 1) return; n   prio[u] = ++pr_id; n   rev[pr_id] = edg; n   sid[u] = u; n   st[u].insert(pr_id); n   can[edg] = min(1LL * edges[edg].first - 1, edges[edg].second - wg); n   if (can[edg] < 0) { n     puts( -1 ); n     exit(0); n   } n   weg[u] = wg + edges[edg].first - can[edg]; n } n void solve(int u) { n   int v, id1, id2; n   for (int i = 0; i < graph[u].size(); i++) { n     v = graph[u][i]; n     solve(v); n     uwg[u] += uwg[v]; n     id1 = sid[u]; n     id2 = sid[v]; n     if (st[id2].size() > st[id1].size()) swap(id1, id2); n     for (it = st[id2].begin(); it != st[id2].end(); it++) n       st[id1].insert(int(*it)); n     sid[u] = id1; n   } n   if (u == 1) return; n   int temp; n   while (uwg[u] > edges[neg[u]].second) { n     temp = *st[sid[u]].begin(); n     temp = rev[temp]; n     if (uwg[u] - can[temp] >= edges[neg[u]].second) { n       edges[temp].first -= can[temp]; n       edges[temp].second -= can[temp]; n       uwg[u] -= can[temp]; n       can[temp] = 0; n       st[sid[u]].erase(st[sid[u]].begin()); n     } else { n       int tpv = uwg[u] - edges[neg[u]].second; n       edges[temp].first -= tpv; n       edges[temp].second -= tpv; n       uwg[u] -= tpv; n       can[temp] -= tpv; n     } n   } n   uwg[u] += edges[neg[u]].first; n } n int main() { n   int n, u, v, w, p; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &u, &v); n     scanf( %d %d , &edges[i].first, &edges[i].second); n     edge[u].push_back(i); n     graph[u].push_back(v); n     path[i] = make_pair(u, v); n   } n   dfs(1, 0); n   solve(1); n   printf( %d n , n); n   for (int i = 1; i < n; i++) n     printf( %d %d %d %d n , path[i].first, path[i].second, edges[i].first, n            edges[i].second); n   return 0; n } n