#include <bits/stdc++.h> n #define Mid ((l + r) >> 1) n #define lson (rt << 1) n #define rson (rt << 1 | 1) n using namespace std; n int read(){ n  char c; int num, f = 1; n  while(c = getchar(),!isdigit(c)) if(c ==  - ) f = -1; num = c -  0 ; n  while(c = getchar(), isdigit(c)) num = num * 10 + c -  0 ; n  return f * num; n } n const int N = 2e5 + 1009; n int a, b, k; n int x[N], y[N]; n signed main() n { n  a = read(); b = read(); k = read(); n  if(k == 0) { n   printf( Yes n ); n   for(int i = 1; i <= b; i++) printf( 1 ); n   for(int i = 1; i <= a; i++) printf( 0 );printf(  n ); n   for(int i = 1; i <= b; i++) printf( 1 ); n   for(int i = 1; i <= a; i++) printf( 0 );printf(  n ); n   return 0; n  } n  if(a + b < k + 2 || (a > 0 && b == 1) || a == 0) { n   printf( No n ); n   return 0; n  } n  if(a >= k) { n   y[1] = 1; n   x[k + 1] = 1; n   for(int i = a + 2; i <= a + b; i++) n    x[i] = y[i] = 1; n  } else { n   y[1] = 1; n   x[k + 1] = 1; n   for(int i = a + 1; i <= k; i++) { n    x[i] = y[i] = 1; n   } n   for(int i = k + 2; i <= a + b; i++) n    x[i] = y[i] = 1; n  } n  reverse(x + 1, x + 1 + a + b); n  reverse(y + 1, y + 1 + a + b); n  printf( Yes n ); n  for(int i = 1; i <= a + b; i++) printf( %d , x[i]); printf(  n ); n  for(int i = 1; i <= a + b; i++) printf( %d , y[i]); printf(  n ); n  return 0; n } n /* n 两个位置都为1 n 表示这两个位置之间的数都变成1 n 也就是说只用两个1就可以构造出来了 n  n 可以把中间一对0换成1 n 可以减少0的数量 n 至少需要2+k n */


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, r; n   cin >> s; n   int k, l, x, n; n   cin >> k; n   l = s.length(); n   if (l % k != 0) { n     cout <<  NO n ; n     return 0; n   } n   x = l / k; n   for (int i = 0; i < l; i += x) { n     r = s.substr(i, x); n     n = r.length() - 1; n     for (int j = 0; j <= n / 2; j++) { n       if (r[j] != r[n - j]) { n         cout <<  NO n ; n         return 0; n       } n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <iostream> n #include <algorithm> n using namespace std; n inline int gcd(int a, int b) { n     int Max = max(a, b); n     int Min = min(a, b); n     if (Max % Min == 0) n         return Min; n     else n         return gcd(Min, Max % Min); n } n inline int lcm(int a, int b) { n     return a * b / gcd(a, b); n } n int main() n { n     int t; cin >> t; n     int n, k; n     while (t--) { n         cin >> n >> k; n         int temp1 = k - (n - k); n         for (int i = 1; i < temp1; ++i) n             cout << i <<    ; n         for (int i = k; i >= temp1; --i) n             cout << i <<    ; n         cout <<   n ; n     } n     return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300005; n int q, n; n int a[MAXN], lef[MAXN], rig[MAXN]; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> q; n   while (q--) { n     cin >> n; n     for (int i = 1; i <= n; i++) { n       lef[i] = rig[i] = -1; n     } n     v.clear(); n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       v.push_back(a[i]); n       if (lef[a[i]] == -1) lef[a[i]] = i; n       rig[a[i]] = i; n     } n     sort(v.begin(), v.end()); n     v.erase(unique(v.begin(), v.end()), v.end()); n     int cnt = 1, mx = 0; n     for (int i = 0; i < v.size(); i++) { n       if (i == v.size() - 1 || lef[v[i + 1]] < rig[v[i]]) { n         mx = max(mx, cnt); n         cnt = 0; n       } n       cnt++; n     } n     cout << v.size() - mx <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, qan, n, m, k, l, p; n bool mark[102]; n char s; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> s; n     if (s ==  * ) { n       mark[i] = true; n     } n   } n   qan = (n - 5) / 4; n   for (i = 0; i <= qan; i++) { n     for (j = 1; j <= n - 4 - 4 * i; j++) { n       if (mark[j] == true && mark[j + i + 1] == true && n           mark[j + 2 * i + 2] == true && mark[j + 3 * i + 3] == true && n           mark[4 * i + j + 4] == true) { n         cout <<  yes  << endl; n         return 0; n       } n     } n   } n   cout <<  no  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test; n   cin >> test; n   while (test--) { n     char str[10000], cpy[10000], st[10000]; n     cin >> str; n     int ln = strlen(str), ck = 0; n     for (int i = 0, j = ln - 1; i < ln; i++, j--) { n       cpy[j] = str[i]; n     } n     for (int i = 0; i < ln; i++) { n       if (str[i] == cpy[i]) { n         ck = 1; n       } else { n         ck = 0; n         cout << str << endl; n         break; n       } n     } n     if (ck) { n       strcpy(st, str); n       sort(str, str + ln); n       if (!strcmp(str, st)) n         printf( -1 n ); n       else n         cout << str << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n int a[MAXN]; n int cnt[101][101]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     cnt[i % k][a[i]]++; n   } n   int ans = 0; n   for (int i = 0; i < k; i++) { n     ans += min(cnt[i][1], cnt[i][2]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1001001001; n const long long INFLL = 1001001001001001001LL; n template <typename T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cout << *i <<    ; n   cout << endl; n } n template <typename T> n void chmin(T& a, T b) { n   if (a > b) a = b; n } n template <typename T> n void chmax(T& a, T b) { n   if (a < b) a = b; n } n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n double fin() { n   double x; n   scanf( %lf , &x); n   return x; n } n long long lin() { n   long long x; n   scanf( %lld , &x); n   return x; n } n int N, K, P, A[500005], S[500005], nxt[500005][101]; n vector<int> pos[101]; n int dp[101][10001]; n int main() { n   N = in(); n   K = in(); n   P = in(); n   int s = 0; n   for (int i = 0; i < N; ++i) { n     A[i] = in() % P; n     S[i] = ((s += A[i]) %= P); n     pos[s].push_back(i); n   } n   vector<int> nx(P, INF); n   for (int i = 0; i < P; ++i) { n     pos[i].push_back(INF); n     nx[i] = pos[i][0]; n   } n   s = 0; n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j < P; ++j) { n       nxt[i][(j - s + P) % P] = nx[j]; n     } n     (s += A[i]) %= P; n     nx[s] = *upper_bound(pos[s].begin(), pos[s].end(), i); n   } n   for (int i = 0; i <= 100; ++i) { n     fill(dp[i], dp[i] + 10001, INF); n   } n   dp[0][0] = 0; n   for (int k = 0; k + 1 < K; ++k) { n     for (int x = 0; x < K * P; ++x) { n       if (dp[k][x] >= N) continue; n       const int f = dp[k][x]; n       for (int p = 0; p < P; ++p) { n         if (nxt[f][p] == INF) continue; n         chmin(dp[k + 1][x + p], nxt[f][p] + 1); n       } n     } n   } n   int res = INF; n   for (int x = 0; x < K * P; ++x) { n     if (dp[K - 1][x] >= N) continue; n     int v = dp[K - 1][x]; n     int t = (S[N - 1] - S[v - 1] + P) % P; n     chmin(res, x + t); n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int sizePermute, sizebkp, x, minimum = 100, maximum = 0, maximumIndex, n                                minimumIndex, counter = 0; n   cin >> sizePermute; n   sizebkp = sizePermute; n   vector<int> myvec(sizePermute); n   myvec.clear(); n   while (sizePermute != 0) { n     cin >> x; n     if (x < minimum) { n       minimum = x; n       minimumIndex = counter; n     } n     if (x > maximum) { n       maximum = x; n       maximumIndex = counter; n     } n     myvec.push_back(x); n     sizePermute--; n     counter++; n   } n   if (maximumIndex == (sizebkp - 1) && minimumIndex == 0) { n     cout << sizebkp - 1 << endl; n     return 0; n   } else if (minimumIndex == (sizebkp - 1) && maximumIndex == 0) { n     cout << sizebkp - 1 << endl; n     return 0; n   } else if (maximumIndex < minimumIndex && n              (((sizebkp - 1) - minimumIndex) > (maximumIndex - 0))) { n     cout << sizebkp - 1 - maximumIndex << endl; n     return 0; n   } else if (maximumIndex < minimumIndex && n              (((sizebkp - 1) - minimumIndex) < (maximumIndex - 0))) { n     cout << minimumIndex << endl; n     return 0; n   } else if (maximumIndex > minimumIndex && n              (((sizebkp - 1) - maximumIndex) > (minimumIndex - 0))) { n     cout << sizebkp - 1 - minimumIndex << endl; n     return 0; n   } else if (maximumIndex > minimumIndex && n              (((sizebkp - 1) - maximumIndex) < (minimumIndex - 0))) { n     cout << maximumIndex << endl; n     return 0; n   } else if (maximumIndex > minimumIndex && n              (((sizebkp - 1) - maximumIndex) == (minimumIndex - 0))) { n     cout << sizebkp - 1 - minimumIndex << endl; n     return 0; n   } else if (maximumIndex < minimumIndex && n              (((sizebkp - 1) - minimumIndex) == (maximumIndex - 0))) { n     cout << sizebkp - 1 - maximumIndex << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long power = 0, l = 0, emp = 1, aa = 0; n   for (long long i = 0; i > -1; i++) { n     if (i != 0) { n       for (long long j = 0; j < i; j++) { n         emp = emp * c; n       } n     } n     if (emp >= a && emp <= b) { n       cout << emp <<    ; n       power++; n     } n     if (emp > b / c) { n       break; n     } n     emp = 1; n   } n   if (power == 0) { n     cout <<  -1  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = 1000 * 1000 * 1000 + 7; n const long long mod1 = 998244353; n const long long INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7; n using namespace std; n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = (long long)(res * x); n     y = y >> 1; n     x = (long long)(x * x); n   } n   return res; n } n bool vis[3000]; n vector<long long> a[3000]; n vector<pair<long long, long long> > fin[3000]; n map<long long, long long> suc; n long long val[3000]; n long long c[3000]; n long long cur = 1; n bool dfs(long long parent) { n   long long f = 0; n   long long s = 0; n   for (auto edge : a[parent]) { n     f += 1; n     if (!vis[edge]) { n       vis[edge] = true; n       bool x = dfs(edge); n       if (x == false) return false; n       s += int((fin[edge]).size()); n       for (int j = 0; j < int((fin[edge]).size()); j++) n         fin[parent].push_back(fin[edge][j]); n     } n   } n   if (f == 1 and parent != a[0][0]) { n     if (c[parent] > s) return false; n     while (suc[cur] != 0) cur += 1; n     fin[parent].push_back({cur, parent}); n     val[parent] = cur; n     suc[cur] = 1; n   } else { n     if (c[parent] > s) n       return false; n     else { n       sort(fin[parent].begin(), fin[parent].end()); n       if (c[parent] == 0) { n         val[parent] = fin[parent][0].first; n         suc[val[parent]] = 1; n         for (int j = 0; j < int((fin[parent]).size()); j++) { n           fin[parent][j].first += 1; n           val[fin[parent][j].second] += 1; n         } n         suc[fin[parent].back().first] = 1; n         fin[parent].push_back({val[parent], parent}); n       } else if (c[parent] == s) { n         val[parent] = fin[parent].back().first + 1; n         suc[val[parent]] = 1; n         fin[parent].push_back({val[parent], parent}); n       } else { n         val[parent] = fin[parent][c[parent]].first; n         suc[val[parent]] = 1; n         for (int j = c[parent]; j < int((fin[parent]).size()); j++) { n           fin[parent][j].first += 1; n           val[fin[parent][j].second] += 1; n         } n         suc[fin[parent].back().first] = 1; n         fin[parent].push_back({val[parent], parent}); n       } n     } n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   for (int(i) = (0); (i) < (n); ++(i)) { n     long long x, y; n     cin >> x >> y; n     if (x != 0) a[i + 1].push_back(x); n     a[x].push_back(i + 1); n     c[i + 1] = y; n   } n   if (n == 1) { n     if (c[1] == 0) { n       cout <<  YES  n            <<   n ; n       cout << 1; n       return 0; n     } n   } n   vis[a[0][0]] = true; n   bool ans = dfs(a[0][0]); n   if (ans == false) { n     cout <<  NO ; n   } else { n     cout <<  YES  n          <<   n ; n     for (int i = 1; i <= n; i++) cout << val[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10005; n int a[N], sorted[N], n, m, sum[20][N], tree[20][N]; n inline void build(int deep, int l, int r) { n   int i, mid = (l + r) >> 1, lm = mid - l + 1, lp = l, rp = mid + 1; n   for (i = l; i <= mid; i++) n     if (sorted[i] > sorted[mid]) lm--; n   for (i = l; i <= r; i++) { n     if (i == l) n       sum[deep][i] = 0; n     else n       sum[deep][i] = sum[deep][i - 1]; n     if (tree[deep][i] == sorted[mid]) { n       if (lm) { n         lm--; n         sum[deep][i]++; n         tree[deep + 1][lp++] = tree[deep][i]; n       } else n         tree[deep + 1][rp++] = tree[deep][i]; n     } else if (tree[deep][i] > sorted[mid]) { n       sum[deep][i]++; n       tree[deep + 1][lp++] = tree[deep][i]; n     } else n       tree[deep + 1][rp++] = tree[deep][i]; n   } n   if (l != r) { n     build(deep + 1, l, mid); n     build(deep + 1, mid + 1, r); n   } n } n int ask(int deep, int l, int r, int ql, int qr, int k) { n   int s, ss, mid = (l + r) >> 1; n   if (l == r) return tree[deep][l]; n   if (l == ql) { n     s = 0; n     ss = sum[deep][qr]; n   } else { n     s = sum[deep][ql - 1]; n     ss = sum[deep][qr] - s; n   } n   if (k <= ss) n     return ask(deep + 1, l, mid, l + s, l + s + ss - 1, k); n   else n     return ask(deep + 1, mid + 1, r, mid - l + 1 + ql - s, n                mid - l + 1 + qr - s - ss, k - ss); n } n inline bool cmp(int a, int b) { return a > b; } n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(int a) { n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) a[i] = tree[0][i] = sorted[i] = read(); n   sort(sorted + 1, sorted + 1 + n, cmp); n   build(0, 1, n); n   while (m--) { n     int s1 = read(), s2 = read(), s3 = read(); n     if (ask(0, 1, n, s1, s2, s2 - s3 + 1) == a[s3]) n       puts( Yes ); n     else n       puts( No ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 210, INF = 1 * 1000 * 1000 * 1000 + 10, maxm = 150, maxk = 10; n int n, m, k, sar, now, par[maxn], x[maxk], c[maxn], d[maxn], f[maxn][maxn], n     dp[maxm][maxn], dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}, cur, X, Y, n                     save; n vector<int> t[maxm][maxn], path[maxn][maxn]; n queue<int> q; n bool mark[maxn]; n char ans[maxn][maxn]; n inline bool valid(int &a, int &b) { return a >= 0 && a < n && b >= 0 && b < m; } n inline void SPFA() { n   while (!q.empty()) { n     sar = q.front(); n     X = sar / m; n     Y = sar % m; n     q.pop(); n     mark[sar] = 0; n     for (int i = 0; i < 4; i++) { n       X += dx[i]; n       Y += dy[i]; n       save = X * m + Y; n       if (valid(X, Y) && d[save] > d[sar] + c[save]) { n         d[save] = d[sar] + c[save]; n         par[save] = sar; n         if (!mark[save]) { n           q.push(save); n           mark[save] = 1; n         } n       } n       X -= dx[i]; n       Y -= dy[i]; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n * m; i++) cin >> c[i]; n   for (int i = 0; i < n * m; i++) { n     for (int j = 0; j < n * m; j++) d[j] = INF; n     d[i] = 0; n     par[i] = -1; n     mark[i] = 1; n     q.push(i); n     SPFA(); n     for (int j = 0; j < n * m; j++) { n       f[i][j] = d[j]; n       now = j; n       while (par[now] != -1) { n         path[i][j].push_back(now); n         now = par[now]; n       } n     } n   } n   memset(dp, 63, sizeof dp); n   for (int i = 0; i < k; i++) { n     cin >> X >> Y; n     X--; n     Y--; n     x[i] = X * m + Y; n     for (int j = 0; j < n * m; j++) { n       dp[1 << i][j] = f[x[i]][j] + c[x[i]]; n       t[1 << i][j] = path[x[i]][j]; n       t[1 << i][j].push_back(x[i]); n     } n   } n   for (int i = 3; i < (1 << k); i++) { n     if (__builtin_popcount(i) > 1) { n       for (int j = 0; j < n * m; j++) { n         for (int sub = i; sub; sub = (sub - 1) & i) { n           if (sub == i) continue; n           cur = i ^ sub; n           for (int p = 0; p < n * m; p++) { n             if (dp[sub][p] + dp[cur][p] + f[p][j] - c[p] < dp[i][j]) { n               dp[i][j] = dp[sub][p] + dp[cur][p] + f[p][j] - c[p]; n               save = sub; n               X = p; n             } n           } n         } n         cur = i ^ save; n         t[i][j] = path[X][j]; n         for (int p = 0; p < ((int(t[save][X].size()))); p++) n           t[i][j].push_back(t[save][X][p]); n         for (int p = 0; p < ((int(t[cur][X].size()))); p++) { n           if (t[cur][X][p] != X) t[i][j].push_back(t[cur][X][p]); n         } n       } n     } n   } n   cout << dp[(1 << k) - 1][x[0]]; n   for (int i = 0; i < ((int(t[(1 << k) - 1][x[0]].size()))); i++) n     ans[t[(1 << k) - 1][x[0]][i] / m][t[(1 << k) - 1][x[0]][i] % m] =  X ; n   for (int i = 0; i < n; i++) { n     cout << endl; n     for (int j = 0; j < m; j++) { n       if (ans[i][j] ==  X ) n         cout << ans[i][j]; n       else n         cout <<  . ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 21 | 1; n int n, p[N]; n long long a[N], b[N], c[N]; n char s[N], t[N]; n void OR(long long* a) { n   int i, j, u, v, l, t; n   for (i = 2; i <= n; i <<= 1) n     for (j = 0, t = i >> 1; j < n; j += i) n       for (u = j, v = l = j + t; u < l; ++u, ++v) a[v] += a[u]; n } n void IOR(long long* a) { n   int i, j, u, v, l, t; n   for (i = 2; i <= n; i <<= 1) n     for (j = 0, t = i >> 1; j < n; j += i) n       for (u = j, v = l = j + t; u < l; ++u, ++v) a[v] -= a[u]; n } n int main() { n   int m, i; n   scanf( %d%s%s , &m, s, t), n = 1 << m; n   for (i = 1; i < n; ++i) p[i] = p[i - (i & -i)] + 2; n   for (i = 0; i < n; ++i) n     a[i] = (s[i] - 48ll) << p[i], b[i] = (t[i] - 48ll) << p[i]; n   OR(a), OR(b); n   for (i = 0; i < n; ++i) c[i] = a[i] * b[i]; n   IOR(c); n   for (i = 0; i < n; ++i) putchar(c[i] >> p[i] & 3 | 48); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n const double PI = 3.14159; n vector<int> v; n long long n, m, k, t, ans, sum, r, l, b, c, d, a; n bool f; n string s; n set<int> st; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     sum += x; n     ans += sum / m; n     sum %= m; n     cout << ans <<    ; n     ans = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int INF = 1e9 + 5; n const long long INF64 = 1e18; n string to_string(const string &s) { return     + s +    ; } n string to_string(bool b) { return b ?  true  :  false ; } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename T> n string to_string(const vector<T> &v) { n   string ret =  < ; n   for (auto it : v) ret += to_string(it) +  ,  ; n   ret.pop_back(); n   ret.pop_back(); n   ret +=  > ; n   return ret; n } n void debug_print() { cerr << endl; } n template <typename T, typename... U> n void debug_print(T t, U... u) { n   cerr <<     << to_string(t); n   debug_print(u...); n } n const int N = 1e5 + 5; n long long mulmod(long long a, long long b) { n   a %= MOD; n   b %= MOD; n   return (a * b) % MOD; n } n long long f(long long n) { n   long long r = (n + 1) / 2; n   if (n & 1) return mulmod(r, r); n   return mulmod(r, r + 1); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     n += 2; n     n /= 2; n     cout << f(n) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n bool vis[N]; n int n, a[N], go[N]; n pair<int, int> v[N]; n bool isLucky(int x) { n   while (x > 0) { n     if (x % 10 != 4 && x % 10 != 7) return false; n     x /= 10; n   } n   return true; n } n void solve() { n   bool hasLN = false; n   for (int i = (0); i < (n); ++i) n     if (isLucky(a[i])) { n       hasLN = true; n       break; n     } n   if (!hasLN) { n     for (int i = (0); i < (n - 1); ++i) n       if (a[i] > a[i + 1]) { n         puts( -1 ); n         return; n       } n     puts( 0 ); n     return; n   } n   for (int i = (0); i < (n); ++i) v[i] = make_pair(a[i], i); n   sort(v, v + n); n   for (int i = (0); i < (n); ++i) go[v[i].second] = i; n   vector<pair<int, int> > ans; n   for (int i = (0); i < (n); ++i) n     if (!vis[i] && isLucky(a[i])) { n       vector<int> vec; n       int x = go[i]; n       while (!vis[x]) { n         vec.push_back(x), vis[x] = true; n         x = go[x]; n       } n       if (((int)(vec).size()) == 1) continue; n       for (int j = (((int)(vec).size()) - 1) - 1; j >= (0); --j) { n         ans.push_back(make_pair(vec[j], vec[j + 1])); n         swap(a[vec[j]], a[vec[j + 1]]); n       } n     } n   int p; n   for (int i = (0); i < (n); ++i) n     if (vis[i] && isLucky(a[i])) { n       p = i; n       break; n     } n   for (int i = (0); i < (n); ++i) n     if (!vis[i]) { n       vector<int> vec; n       int x = i; n       while (!vis[x]) { n         vec.push_back(x), vis[x] = true; n         x = go[x]; n       } n       if (((int)(vec).size()) == 1) continue; n       for (int j = (((int)(vec).size())) - 1; j >= (0); --j) { n         ans.push_back(make_pair(p, vec[j])); n         swap(p, vec[j]); n         swap(a[p], a[vec[j]]); n       } n       ans.push_back(make_pair(p, vec.back())); n       swap(p, vec.back()); n       swap(a[p], a[vec.back()]); n     } n   if (((int)(ans).size()) > 2 * n) { n     puts( -1 ); n     return; n   } n   printf( %d n , ((int)(ans).size())); n   for (int i = (0); i < (((int)(ans).size())); ++i) { n     int x, y; n     tie(x, y) = ans[i]; n     printf( %d %d n , x + 1, y + 1); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) scanf( %d , a + i); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n namespace IO { n bool REOF = 1; n inline char nc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && REOF && n                  (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? (REOF = 0, EOF) n              : *p1++; n } n template <class T> n inline bool read(T &x) { n   if (!REOF) return false; n   char c = nc(); n   bool f = 0; n   x = 0; n   while (c <  0  || c >  9 ) c ==  -  && (f = 1), c = nc(); n   while (c >=  0  && c <=  9 ) x = (x << 3) + (x << 1) + (c ^ 48), c = nc(); n   if (f) x = -x; n   return true; n } n template <typename T, typename... T2> n inline bool read(T &x, T2 &...rest) { n   if (!REOF) return false; n   read(x); n   return read(rest...); n } n inline bool need(char &c) { n   return ((c >=  a ) && (c <=  z )) || ((c >=  0 ) && (c <=  9 )) || n          ((c >=  A ) && (c <=  Z )); n } n inline bool read_str(char *a) { n   if (!REOF) return false; n   while ((*a = nc()) && need(*a) && REOF) ++a; n   *a =   0 ; n   return true; n } n inline bool read_dbl(double &x) { n   if (!REOF) return false; n   bool f = 0; n   char ch = nc(); n   x = 0; n   while (ch <  0  || ch >  9 ) { n     f |= (ch ==  - ); n     ch = nc(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10.0 + (ch ^ 48); n     ch = nc(); n   } n   if (ch ==  . ) { n     double tmp = 1; n     ch = nc(); n     while (ch >=  0  && ch <=  9 ) { n       tmp = tmp / 10.0; n       x = x + tmp * (ch ^ 48); n       ch = nc(); n     } n   } n   if (f) x = -x; n   return true; n } n template <class TH> n void _dbg(const char *sdbg, TH h) { n   cerr << sdbg <<  =  << h <<   n ; n } n template <class TH, class... TA> n void _dbg(const char *sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<  =  << h <<  ,  <<    ; n   _dbg(sdbg + 1, a...); n } n template <class T> n ostream &operator<<(ostream &os, vector<T> V) { n   os <<  [ ; n   for (auto vv : V) os << vv <<  , ; n   return os <<  ] ; n } n template <class L, class R> n ostream &operator<<(ostream &os, pair<L, R> P) { n   return os <<  (  << P.st <<  ,  << P.nd <<  ) ; n } n }  // namespace IO n using namespace IO; n const int maxn = 2e5 + 5; n const int mod = 998244353; n const int INF = 1e9 + 7; n const double eps = 1e-12; n int dx[4] = {0, 0, 1, -1}; n int dy[4] = {1, -1, 0, 0}; n int n, x, y; n int best() { n   int z = x + y + 1; n   return max(z - n, 1); n } n int worst() { n   int z = x + y; n   return min(z - 1, n); n } n void solve() { n   read(n, x, y); n   if (x > y) swap(x, y); n   if (x == 1 && y == 1) { n     printf( 1 1 n ); n     return; n   } n   if (x == n && y == n) { n     printf( %d %d n , n, n); n     return; n   } n   printf( %d %d n , best(), worst()); n } n int main() { n   int _T; n   read(_T); n   for (int _ = 1; _ <= _T; _++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int a, b, c, l; n int main() { n   ios::sync_with_stdio(false); n   cin >> a >> b >> c >> l; n   unsigned long long int ans = ((l + 3) * (l + 2) * (l + 1)) / (6); n   for (int i = 0; i <= l; i++) { n     if (a + i >= b + c) { n       unsigned long long int x = n           ((a + i - b - c) < (l - i) ? (a + i - b - c) : (l - i)); n       ans -= ((x + 2) * (x + 1)) / (2); n     } n   } n   for (int i = 0; i <= l; i++) { n     if (b + i >= a + c) { n       unsigned long long int x = n           ((b + i - a - c) < (l - i) ? (b + i - a - c) : (l - i)); n       ans -= ((x + 2) * (x + 1)) / (2); n     } n   } n   for (int i = 0; i <= l; i++) { n     if (c + i >= b + a) { n       unsigned long long int x = n           ((c + i - a - b) < (l - i) ? (c + i - a - b) : (l - i)); n       ans -= ((x + 2) * (x + 1)) / (2); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 5; n string s; n long long a, b; n long long v[N], pre[N], suf[N]; n int32_t main() { n   cin >> s >> a >> b; n   long long n = s.size(); n   for (long long i = 1; i <= n; i++) v[i] = s[i - 1] -  0 ; n   for (long long i = 1; i <= n; i++) { n     pre[i] = pre[i - 1] * 10 + v[i]; n     pre[i] %= a; n   } n   long long curpow = 1; n   for (long long i = n; i >= 1; i--) { n     suf[i] = v[i] * curpow + suf[i + 1]; n     curpow *= 10; n     curpow %= b; n     suf[i] %= b; n   } n   long long check = 0, ind; n   for (long long i = 1; i < n; i++) { n     if (pre[i] == 0 && suf[i + 1] == 0 && v[i + 1] != 0) { n       ind = i; n       check = 1; n     } n   } n   if (check) { n     cout <<  YES  n          <<   n ; n     for (long long i = 1; i <= ind; i++) { n       cout << v[i]; n     } n     cout <<   n ; n     for (long long i = ind + 1; i <= n; i++) { n       cout << v[i]; n     } n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int de(int x) { n   if (x == 0) return 0; n   if (x & 1) return de(x / 2) + 1; n   return de(x / 2); n } n long long val[101000]; n int main() { n   int n, i, j, a; n   while (scanf( %d , &n) != EOF) { n     memset(val, 0, sizeof(val)); n     for (i = 0; i < n; i++) { n       scanf( %d , &a); n       val[de(a)]++; n     } n     long long ans = 0; n     for (i = 0; i <= 50000; i++) ans += (val[i] * (val[i] - 1)) / 2; n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string n, r; n   cin >> n; n   r = n; n   reverse(r.begin(), r.end()); n   cout << n << r <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const int M1 = 1000000007; n const int M2 = 1000000087; n int N; n int H1[310000]; n int P1[310000]; n int H2[310000]; n int P2[310000]; n int HH1, HH2; n int n, m; n vector<int> g[100007]; n string s[100007]; n char t[300007]; n int ans; n void precalc() { n   H1[0] = H2[0] = 0; n   P1[0] = P2[0] = 1; n   for (int i = 1; i < 300007; i++) { n     P1[i] = (P1[i - 1] * 31LL) % M1; n     P2[i] = (P2[i - 1] * 37LL) % M2; n   } n   for (int i = 0, _n = (m); i < _n; ++i) { n     char c = t[i] -  a  + 1; n     HH1 = (HH1 * 31LL + c) % M1; n     HH2 = (HH2 * 37LL + c) % M2; n   } n } n void addChar(char c) { n   c = c -  a  + 1; n   H1[N + 1] = (H1[N] * 31LL + c) % M1; n   H2[N + 1] = (H2[N] * 37LL + c) % M2; n   N++; n } n bool isTail() { n   if (N < m) return false; n   int r = N - 1; n   int l = r - m + 1; n   int CH1 = H1[r + 1] - ((long long)H1[l] * P1[r - l + 1]) % M1; n   if (CH1 < 0) CH1 += M1; n   int CH2 = H2[r + 1] - ((long long)H2[l] * P2[r - l + 1]) % M2; n   if (CH2 < 0) CH2 += M2; n   return CH1 == HH1 && CH2 == HH2; n } n void dfs(int v) { n   for (int i = 0, _n = ((int)((s[v]).size())); i < _n; ++i) { n     addChar(s[v][i]); n     ans += isTail(); n   } n   for (int i = 0, _n = ((int)((g[v]).size())); i < _n; ++i) dfs(g[v][i]); n   N -= (int)((s[v]).size()); n } n void solution() { n   scanf( %d n , &n); n   for (int i = 0, _n = (n - 1); i < _n; ++i) { n     int p; n     scanf( %d %s n , &p, t); n     s[i + 1] = string(t); n     g[p - 1].push_back(i + 1); n   } n   m = strlen(gets(t)); n   precalc(); n   dfs(0); n   printf( %d n , ans); n } n int main() { n   solution(); 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     bool usa[n], usb[n]; n     for (int i = 0; i < n; ++i) { n       usa[i] = 1; n       usb[i] = 1; n     } n     for (int i = 0; i < n; ++i) { n       int a; n       cin >> a; n       bool f = 1; n       for (int j = 0; j < a; j++) { n         int b; n         cin >> b; n         if (usb[b - 1] && f) { n           usb[b - 1] = 0; n           usa[i] = 0; n           f = 0; n         } n       } n     } n     bool c = 0, d = 0; n     int x, y; n     for (int i = 0; i < n; ++i) { n       if (usa[i] && !c) { n         x = i + 1; n         c = 1; n       } n       if (usb[i] && !d) { n         y = i + 1; n         d = 1; n       } n     } n     if (c) n       cout <<  IMPROVE  << endl << x <<     << y << endl; n     else n       cout <<  OPTIMAL  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   bool f = false; n   char ch = getchar(); n   while (!isdigit(ch)) { n     f |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return f ? -x : x; n } n void write(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 + 48); n } n const int N = 2505, INF = 0x7fffffff; n int n; n int a[N][N]; n int fa[N], d[N]; n bool vis[N]; n inline void prim() { n   for (int i = 2; i <= n; i++) fa[i] = 1, d[i] = a[1][i]; n   memset(vis, false, sizeof(vis)); n   vis[1] = true; n   int k, minn; n   for (int tot = 2; tot <= n; tot++) { n     minn = INF; n     for (int i = 1; i <= n; i++) n       if (minn > d[i] && !vis[i]) minn = d[i], k = i; n     vis[k] = true; n     for (int i = 1; i <= n; i++) n       if (a[k][i] < d[i] && !vis[i]) d[i] = a[k][i], fa[i] = k; n   } n } n int dep[N]; n void getdep(int x) { n   if (!dep[fa[x]]) getdep(fa[x]); n   dep[x] = dep[fa[x]] + 1; n } n int main() { n   n = read(); n   for (register int i = 1; i <= n; i++) n     for (register int j = 1; j <= n; j++) a[i][j] = read(); n   for (register int i = 1; i <= n; i++) n     if (a[i][i]) return puts( NOT MAGIC ), 0; n   for (register int i = 1; i <= n; i++) n     for (register int j = i + 1; j <= n; j++) n       if (a[i][j] != a[j][i]) return puts( NOT MAGIC ), 0; n   prim(); n   dep[1] = 1; n   for (register int i = 2; i <= n; i++) n     if (!dep[i]) getdep(i); n   for (register int i = 1; i <= n; i++) n     for (register int j = i + 1; j <= n; j++) { n       int x = i, y = j; n       if (dep[x] < dep[y]) swap(x, y); n       if (a[x][y] > max(a[fa[x]][y], a[fa[x]][x])) return puts( NOT MAGIC ), 0; n     } n   return puts( MAGIC ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nn[100000], np[100000][19], ni[100000], nl[100000], nh[100000], nc[100000], n     nv[100000], nd[100000], dist[100000]; n vector<int> ne[100000]; n int es[100000], ee[100000]; n bool eb[100000]; n int curp[100000]; n int trav = 0; n bool dfs(int n, int p) { n   ni[n] = nl[n] = nh[n] = trav++; n   nc[n] = 1; n   for (int i = 0; i < ne[n].size(); i++) { n     int other = es[ne[n][i]]; n     if (other == n) { n       other = ee[ne[n][i]]; n     } n     if (other == p) continue; n     if (ni[other] == -1) { n       if (dfs(other, n)) { n         eb[ne[n][i]] = true; n       } n       nc[n] += nc[other]; n       nl[n] = min(nl[n], nl[other]); n       nh[n] = max(nh[n], nh[other]); n     } else { n       nl[n] = min(nl[n], ni[other]); n       nh[n] = max(nh[n], ni[other]); n     } n   } n   if (nl[n] == ni[n] && nh[n] < ni[n] + nc[n]) { n     return true; n   } n   return false; n } n class queue_order { n  public: n   int n; n   bool bridge; n   queue_order(int a, bool b) { n     n = a; n     bridge = b; n   } n }; n struct cmp { n   bool operator()(queue_order* a, queue_order* b) { n     if (a->bridge == b->bridge) return false; n     if (a->bridge) return true; n     return false; n   } n }; n void condense() { n   int curnn = 0; n   priority_queue<queue_order*, vector<queue_order*>, cmp> pq; n   pq.push(new queue_order(0, false)); n   while (pq.size() > 0) { n     queue_order* o = pq.top(); n     pq.pop(); n     if (nn[o->n] != -1) continue; n     if (o->bridge) curnn++; n     nn[o->n] = curnn; n     for (int i = 0; i < ne[o->n].size(); i++) { n       int other = es[ne[o->n][i]]; n       if (other == o->n) { n         other = ee[ne[o->n][i]]; n       } n       if (nn[other] != -1) continue; n       pq.push(new queue_order(other, eb[ne[o->n][i]])); n     } n   } n } n void dfs2(int n, int p, int d) { n   curp[d] = n; n   nd[n] = d; n   for (int i = 0; i < 18; i++) { n     if (d - (1 << i) >= 0) { n       np[n][i] = curp[d - (1 << i)]; n     } else { n       np[n][18] = i - 1; n       break; n     } n   } n   for (int i = 0; i < ne[n].size(); i++) { n     int other = es[ne[n][i]]; n     if (other == n) { n       other = ee[ne[n][i]]; n     } n     if (other == p) continue; n     dfs2(other, n, d + 1); n   } n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   memset(nv, 0, n * sizeof(int)); n   memset(nn, -1, n * sizeof(int)); n   memset(ni, -1, n * sizeof(int)); n   memset(nc, 0, n * sizeof(int)); n   memset(eb, false, m * sizeof(bool)); n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &es[i], &ee[i]); n     es[i]--; n     ee[i]--; n     ne[es[i]].push_back(i); n     ne[ee[i]].push_back(i); n   } n   dfs(0, 0); n   condense(); n   for (int i = 0; i < n; i++) { n     ne[i].clear(); n   } n   for (int i = 0; i < m; i++) { n     es[i] = nn[es[i]]; n     ee[i] = nn[ee[i]]; n     if (es[i] != ee[i]) { n       ne[es[i]].push_back(i); n       ne[ee[i]].push_back(i); n     } n   } n   int root = 0; n   queue<int> q; n   queue<int> d; n   q.push(root); n   nv[root] = 1; n   while (q.size() > 0) { n     int cur = q.front(); n     q.pop(); n     root = cur; n     for (int i = 0; i < ne[cur].size(); i++) { n       int other = es[ne[cur][i]]; n       if (other == cur) { n         other = ee[ne[cur][i]]; n       } n       if (nv[other] < 1) { n         nv[other] = 1; n         q.push(other); n       } n     } n   } n   q.push(root); n   d.push(0); n   nv[root] = 2; n   int maxd = 0; n   while (q.size() > 0) { n     int cur = q.front(); n     q.pop(); n     int curd = d.front(); n     d.pop(); n     maxd = curd; n     dist[curd] = cur; n     for (int i = 0; i < ne[cur].size(); i++) { n       int other = es[ne[cur][i]]; n       if (other == cur) { n         other = ee[ne[cur][i]]; n       } n       if (nv[other] < 2) { n         nv[other] = 2; n         q.push(other); n         d.push(curd + 1); n       } n     } n   } n   root = dist[maxd / 2]; n   dfs2(root, root, 0); n   int k, s, e, s2, e2; n   scanf( %d , &k); n   while (k-- > 0) { n     scanf( %d %d , &s, &e); n     s = s2 = nn[s - 1]; n     e = e2 = nn[e - 1]; n     while (s2 != e2) { n       if (nd[s2] < nd[e2]) { n         for (int i = np[e2][18]; i >= 0; i--) { n           if (nd[e2] - (1 << i) >= nd[s2]) { n             e2 = np[e2][i]; n           } n         } n       } else if (nd[s2] > nd[e2]) { n         for (int i = np[s2][18]; i >= 0; i--) { n           if (nd[s2] - (1 << i) >= nd[e2]) { n             s2 = np[s2][i]; n           } n         } n       } else { n         for (int i = np[s2][18]; i > 0; i--) { n           if (np[s2][i] != np[e2][i]) { n             s2 = np[s2][i]; n             e2 = np[e2][i]; n           } n         } n         s2 = np[s2][0]; n         e2 = np[e2][0]; n       } n     } n     printf( %d n , nd[s] + nd[e] - 2 * nd[s2]); n   } n   return 0; n } n


#include<bits/stdc++.h> n  n #define fill(arr,c) memset(arr,c,sizeof(arr)) n #define perm(r) next_permutation(all(r)) n //it gives bool value ans permute the string lexoggraphically n #define sortdesc greater<int>() n #define ll long long int  n //std::setfill( 0 ) << std::setw(5) << 25; it adds leaing zeroes; n #define f(i,a) for(ll i=0;i<a;i++) n #define fo(i,a) for(ll i=1;i<=a;i++) n #define foo(i,x,n,c) for(ll i=x;i<n;i+=c) n #define foi(i,x,n,c) for(ll i=x;i>=n;i+=c) n #define fauto(i,m) for(auto i:m) n #define forall(a) for( auto itr=a.begin();itr!=a.end();itr++) n #define ld long double n #define in push_back n #define mll map<ll,ll> n #define mcl map<char,ll> n #define msl map<string,ll> n #define vi vector<int > n #define vl vector<ll > n #define vp vector< pair<int,int> > n #define vs vector <string>  n #define vc vector <char>  n #define o(a) cout<<a n #define os(a) cout<<a<<    n #define en cout<<  n ; n #define testcase ll t ;cin>>t; while(t--) n #define ff first n #define ss second n #define pl pair<ll,ll> n #define pi pair<int,int> n #define all(a) (a).begin(), (a).end() n  n #define sz(a)  (a).size() n #define prec(n) fixed<<setprecision(n) n #define mp make_pair n #define bitcount  __builtin_popcountll n #define imin o( imin );en; n using namespace std; n bool comp(const pair<int,int> &a , const pair<int,int> &b){ n     if(a.first == b.first)  return a.second < b.second; n      return a.first < b.first; n } n bool comps(const pair<int,int> &a , const pair<int,int> &b){ n     if(a.second == b.second)  return a.first< b.first; n      return a.second < b.second; n } n  bool prime[1000005];  n void primeseries(ll n) n { n  n     memset(prime, true, sizeof(prime));  n    n     for (ll p=2; p*p<=n; p++)  n     {  n         // If prime[p] is not changed, then it is a prime  n         if (prime[p] == true)  n         {  n             // Update all multiples of p greater than or   n             // equal to the square of it  n             // numbers which are multiple of p and are  n             // less than p^2 are already been marked.   n             for (ll i=p*p; i<=n; i += p)  n                 prime[i] = false;  n         }  n     } n //return prime; n     } n struct custom_hash { n     size_t operator()(uint64_t x) const { n         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); n         x ^= FIXED_RANDOM; n         return x ^ (x >> 16); n     } n }; n bool compmx(int a, int b)  n {  n     return (a < b);  n }  n // bool isprime(int x){ n //     for(int i = 2; i*i <= x; i++){ n //         if(x%i == 0) return false; n //     } n //     return true; n // } n // n //ll pi=acos(0.0)*2; n void fastio() n { n      ios::sync_with_stdio(0); n cin.tie(0); n  n } n ll mod=1E9+7; n void inout() n { n  n     #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n  n      freopen( output.txt ,  w , stdout); n      #endif n } n unsigned long long power(unsigned long long x,  n                                   int y, int p) n { n     unsigned long long res = 1; // Initialize result n   n     x = x % p; // Update x if it is more than or n     // equal to p n   n     while (y > 0)  n     { n       n         // If y is odd, multiply x with result n         if (y & 1) n             res = (res * x) % p; n   n         // y must be even now n         y = y >> 1; // y = y/2 n         x = (x * x) % p; n     } n     return res; n } n   n // Returns n^(-1) mod p n unsigned long long modInverse(unsigned long long n,   n                                             int p) n { n     return power(n, p - 2, p); n } n   n // Returns nCr % p using Fermat s little n // theorem. n unsigned long long nCr(unsigned long long n, n                                  int r, int p) n { n     // If n<r, then nCr should return 0 n     if (n < r) n         return 0; n     // Base case n     if (r == 0) n         return 1; n   n     // Fill factorial array so that we n     // can find all factorial of r, n n     // and n-r n     unsigned long long fac[n + 1]; n     fac[0] = 1; n     for (int i = 1; i <= n; i++) n         fac[i] = (fac[i - 1] * i) % p; n   n     return (fac[n] * modInverse(fac[r], p) % p n             * modInverse(fac[n - r], p) % p) n            % p; n } n bool chk(ll i,ll a,ll b,ll n)  n { n  ll x=i*a+(n-i)*b; n  bool r=true; n  while(x) n   n  { n   if(x%10!=a && x%10!=b) n   return false; n   x/=10; n  } n  return true; n } n //ll a[1001][1001]; n  n int main() n { n //fastio(); n //os(1);; n testcase n { n  n  n ll n,m,u,v; n cin>>n>>m>>u>>v; n ll a[m]; n f(i,m) n cin>>a[i]; n sort(a,a+m); n f(i,m) n a[i]++; n ll ans=0; n if(u>v) n { n  u=n-u+1; n  v=n-v+1; n } n ll prev=m; n f(i,v) n { n  ll x=upper_bound(a,a+prev,v-i-1)-a; n  x--; n  prev=x; n  if(x>=0) n  ans++; n  else n  break; n } n o(min(ans,v-u-1)); n  n en; n } n return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { n   bool first = true; n   os <<  [ ; n   for (unsigned int i = 0; i < v.size(); i++) { n     if (!first) os <<  ,  ; n     os << v[i]; n     first = false; n   } n   return os <<  ] ; n } n template <typename T> n inline std::ostream& operator<<(std::ostream& os, const std::set<T>& v) { n   bool first = true; n   os <<  [ ; n   for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n template <typename T1, typename T2> n inline std::ostream& operator<<(std::ostream& os, const std::map<T1, T2>& v) { n   bool first = true; n   os <<  [ ; n   for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n struct debugger { n   bool first; n   debugger() { first = true; } n   template <typename T> n   debugger& operator,(const T& v) { n     if (!first) cout <<  ,  ; n     cout << v; n     first = false; n     return *this; n   } n }; n long long n; n long long arr[10000000]; n long long solve() { n   vector<long long> st; n   long long mx = 0; n   for (long long i = 0; i < n; i++) { n     while (!st.empty() && st.back() < arr[i]) st.pop_back(); n     st.push_back(arr[i]); n     if (st.size() >= 2) mx = max(mx, st[st.size() - 1] ^ st[st.size() - 2]); n   } n   return mx; n } n int32_t main(void) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   long long mx = solve(); n   reverse(arr, arr + n); n   cout << max(mx, solve()); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int find_first_num_index(long long &k, long long n) { n   if (n == 1) { n     return 0; n   } n   n--; n   int first_num_index; n   long long n_partial_fact = n; n   while (k >= n_partial_fact && n > 1) { n     n_partial_fact *= (n - 1); n     n--; n   } n   first_num_index = k / n_partial_fact; n   k %= n_partial_fact; n   return first_num_index; n } n vector<long long> find_kth_permutation(long long n, long long k) { n   vector<long long> Ans; n   set<long long> S; n   for (int i = 1; i <= n; i++) { n     S.insert(i); n   } n   auto itr = S.begin(); n   k -= 1; n   for (int i = 0; i < n; i++) { n     int index = find_first_num_index(k, n - i); n     advance(itr, index); n     Ans.push_back((*itr)); n     S.erase(itr); n     itr = S.begin(); n   } n   return Ans; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long n, q, t, x = 1; n   cin >> n >> q; n   long long suff_len = min(14LL, n); n   long long pref_len = n - suff_len; n   long long Sil[20]; n   Sil[0] = 1; n   for (int i = 1; i <= suff_len; i++) { n     Sil[i] = Sil[i - 1] * i; n   } n   vector<long long> Suff = find_kth_permutation(suff_len, 1); n   while (q--) { n     cin >> t; n     if (t == 1) { n       long long l, r, ans = 0; n       cin >> l >> r; n       int r_pr = min(r, pref_len); n       if (l <= r_pr) { n         ans += 1ll * (l + r_pr) * (r_pr - l + 1) / 2; n       } n       r -= pref_len + 1; n       l -= pref_len + 1; n       r_pr = max(r_pr - pref_len - 1, 0LL); n       for (int i = max(l, 0LL); i <= r; i++) { n         ans += pref_len + Suff[i]; n       } n       cout << ans << endl; n     } else { n       int dx; n       cin >> dx; n       x += dx; n       x = min(x, Sil[suff_len]); n       Suff = find_kth_permutation(suff_len, x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n inline int read() { n   char c; n   int x; n   for (c = getchar(); !isdigit(c); c = getchar()) n     ; n   for (x = 0; isdigit(c); c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   return x; n } n int n; n long long ans; n std::priority_queue<int> que; n int main() { n   n = read(); n   for (int i = 0; i < n; i++) { n     int x = read(); n     que.push(-x); n     if (que.top() + x > 0) { n       ans += que.top() + x; n       que.push(-x); n       que.pop(); n     } n   } n   printf( %lld n , ans); n   return 0; 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   int t; n   cin >> t; n   int n, m, k; n   while (t--) { n     cin >> n >> m >> k; n     if (n % 2 == 0 && k % 2 == 1) { n       cout <<  NO n ; n       continue; n     } n     if (n % 2 == 0 && k % 2 == 0) { n       if (m % 2 == 0) n         cout <<  YES n ; n       else { n         if (k > n * (m - 1) / 2) n           cout <<  NO n ; n         else n           cout <<  YES n ; n       } n       continue; n     } n     if (m / 2 % 2 == 0) { n       if (k % 2 == 0 && k >= m / 2) n         cout <<  YES n ; n       else n         cout <<  NO n ; n     } else { n       if (k % 2 == 1 && k >= m / 2) n         cout <<  YES n ; n       else n         cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e7; n const long long MOD = 1e9 + 7; n const long long mod = 998244353; n const int MAX_N = 1e5 + 5; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 << 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   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n vector<int> adj[1005]; n int tsz[1005]; n bool ga[1005]; n void dfs(int u, int v = -1) { n   tsz[u] = 1; n   if (adj[u].size() == 1) ga[u] = true; n   for (auto xd : adj[u]) { n     if (xd == v) continue; n     dfs(xd, u); n     tsz[u] += tsz[xd]; n   } n } n int main() { n   int tc; n   cin >> tc; n   while (tc--) { n     for (int i = 0; i < 1005; i++) { n       adj[i].clear(); n       tsz[i] = 0; n     } n     int n, x; n     cin >> n >> x; n     for (int i = 0; i < n - 1; i++) { n       int a, b; n       cin >> a >> b; n       adj[a].push_back(b); n       adj[b].push_back(a); n     } n     dfs(x); n     if (adj[x].size() <= 1) { n       cout <<  Ayush  << endl; n       continue; n     } n     long long p = tsz[x] - 1; n     if (p % 2 == 0) n       cout <<  Ashish  << endl; n     else n       cout <<  Ayush  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 152; n int previ[maxn]; n int curr[maxn]; n int temp[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   previ[0] = 1; n   curr[1] = 1; n   for (int i = 0; i < n - 1; i++) { n     temp[0] = 0; n     for (int j = 0; j <= i + 1; j++) { n       temp[j + 1] = curr[j]; n     } n     for (int j = 0; j <= i; j++) { n       temp[j] = (temp[j] + previ[j]) % 2; n     } n     for (int j = 0; j <= i + 2; j++) { n       previ[j] = curr[j]; n       curr[j] = temp[j]; n     } n   } n   cout << n << endl; n   for (int i = 0; i <= n; i++) { n     cout << curr[i] <<    ; n   } n   cout << endl; n   cout << n - 1 << endl; n   for (int i = 0; i <= n - 1; i++) { n     cout << previ[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &dig) { n   dig = 0; n   bool flag = false; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) flag = true, c = getchar(); n   while (isdigit(c)) dig = dig * 10 + c -  0 , c = getchar(); n   if (flag) dig = -dig; n } n const int MAXN = (int)5e3 + 5; n int n, maxn = INT_MIN; n int times[MAXN], prep[MAXN]; n inline void init() { n   read(n); n   for (register int i = (1); i <= (n); ++i) read(times[i]), prep[i] = times[i]; n   for (register int i = (2); i <= (n); ++i) prep[i] += prep[i - 1]; n } n inline void solve() { n   for (register int i = (1); i <= (n); ++i) { n     for (register int j = (i); j <= (n); ++j) { n       if (100 * (j - i + 1) < prep[j] - prep[i - 1]) n         maxn = max(maxn, j - i + 1); n       else n         continue; n     } n   } n   printf( %d , maxn == INT_MIN ? 0 : maxn); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, cnt; n   double ans, a, dp[100005] = {0}, pr[100005] = {0}; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a; n     pr[i] = pr[i - 1] * a + a; n     dp[i] = dp[i - 1] + a * (2 * pr[i - 1] + 1); n   } n   printf( %.15lf , dp[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fa[5010], ba[5010], b[5010]; n int main() { n   ios_base::sync_with_stdio(0); n   int i, j, k, l, n, m, t, tt, x = 0, y; n   string s, second; n   cin >> s; n   n = s.length(); n   fa[0] = 0; n   ba[n + 1] = 0; n   b[0] = 0; n   for (j = 1; j <= n; j++) { n     fa[j] = fa[j - 1]; n     b[j] = b[j - 1]; n     if (s[j - 1] ==  a ) n       fa[j] = fa[j] + 1; n     else n       b[j] = b[j] + 1; n   } n   if (fa[n] == n) { n     cout << n <<   n ; n     return 0; n   } n   for (j = n; j > 0; j--) { n     ba[j] = ba[j + 1]; n     if (s[j - 1] ==  a ) ba[j] = ba[j] + 1; n   } n   int mx = 0; n   for (i = 1; i <= n; i++) { n     for (j = i; j <= n; j++) { n       mx = max(fa[i - 1] + b[j] - b[i - 1] + ba[j + 1], mx); n     } n   } n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int maxn; n int vis[100010], a[100010], in[100010], temp[100010], res[100010]; n vector<int> q[100010]; n int bfs(int x) { n   int i, sum = 1, hash[100010] = {0}; n   hash[x] = 1; n   while (in[x] < 2) { n     temp[sum++] = x; n     if (!hash[q[x][0]]) { n       if (q[x][0] == 0) break; n       x = q[x][0]; n     } n   } n   return sum - 1; n } n int main() { n   int i, n; n   while (scanf( %d , &n) != EOF) { n     maxn = 0; n     memset(in, 0, sizeof(in)); n     for (i = 1; i <= n; i++) scanf( %d , &vis[i]); n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       in[a[i]]++; n       q[i].push_back(a[i]); n     } n     for (i = 1; i <= n; i++) { n       if (vis[i]) { n         int t = bfs(i); n         if (t > maxn) { n           maxn = t; n           for (int j = 1; j <= maxn; j++) res[j] = temp[j]; n         } n       } n     } n     printf( %d n , maxn); n     for (i = maxn; i > 0; i--) { n       if (i != maxn) printf(   ); n       printf( %d , res[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n inline void read(T &x) { n   bool b = 0; n   char c; n   while (!isdigit(c = getchar()) && c !=  - ) n     ; n   if (c ==  - ) c = getchar(), b = 1; n   x = c - 48; n   while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - 48; n   if (b) x = -x; n } n template <typename T> n inline void write(T x) { n   if (x > 9) write(x / 10); n   putchar(x % 10 + 48); n } n using namespace std; n const int N = 1e5 + 11; n int n, pos1, pos0, a[N], ans[N], ke[N], num[2]; n int Max0(int pos) { n   int res = 0; n   for (int i = 1; i <= pos; i++) { n     num[a[i]]++; n     if (a[i]) n       res++; n     else n       res = max(res, num[0]); n   } n   num[0] = num[1] = 0; n   return res; n } n int Max1(int pos) { n   int res = 0; n   for (int i = 1; i <= pos; i++) { n     num[ans[i]]++; n     if (ans[i]) n       res++; n     else n       res = max(res, num[0]); n   } n   num[0] = num[1] = 0; n   return res; n } n int main() { n   char c; n   while (isdigit(c = getchar())) a[++n] = c - 48; n   for (int i = 1; i <= n; i++) ans[i] = a[i]; n   ke[n + 1] = n; n   ke[n] = n; n   for (int i = n - 1; i > 0; i--) { n     if (a[i] == a[i + 1]) n       ke[i] = ke[i + 1]; n     else n       ke[i] = i; n   } n   for (int i = 1; i <= n; i++) n     if (a[i]) { n       pos1 = i; n       break; n     } n   while (pos1 <= n) { n     pos0 = ke[pos1] + 1; n     int num0 = ke[pos0] - pos0 + 1; n     int num1 = ke[pos1] - pos1 + 1; n     if (num0 < num1) { n       num1 -= num0; n       for (int i = 1; i <= num1; i++) { n         ans[pos1 + i - 1] = 0; n         if (Max0(pos1 + i - 1) != Max1(pos1 + i - 1) || Max0(n) != Max1(n)) n           ans[pos1 + i - 1] = 1; n       } n     } n     pos1 = ke[pos0] + 1; n   } n   for (int i = 1; i <= n; i++) cout << ans[i]; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, m, k; n int a[2000000]; n bool bo[2000000]; n int l, r, ans, tot; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + 1 + n); n   int now = 0; n   bo[0] = true; n   for (i = 1; i <= n; i++) { n     tot++; n     while (a[i] - a[now] >= m && now <= n) tot -= (!bo[now]), now++; n     if (tot >= k) ans++, tot--, bo[i] = true; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int A[200005]; n vector<int> L[105]; n int dp[400005]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &A[i]); n     L[A[i]].push_back(i); n   } n   int opt = 0; n   int D = 0; n   int N = min(n, 100); n   for (int i = 1; i <= N; i++) { n     if (L[i].size() > L[D].size()) D = i; n   } n   memset(dp, -1, sizeof(dp)); n   for (int V = 1; V <= N; V++) { n     if (V == D) continue; n     if (L[V].empty()) continue; n     int bal = 0; n     vector<int> T; n     T.push_back(bal); n     dp[bal + n] = 0; n     for (int i = 1; i <= n; i++) { n       if (A[i] == D) n         bal++; n       else if (A[i] == V) n         bal--; n       if (dp[bal + n] != -1) { n         if (i - dp[bal + n] > opt) opt = i - dp[bal + n]; n       } n       if (dp[bal + n] == -1) { n         dp[bal + n] = i; n         T.push_back(bal); n       } n     } n     for (int v : T) { n       dp[v + n] = -1; n     } n   } n   printf( %d n , opt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 26; n struct Vertex { n   int next[K]; n   long long int leaf = 0; n   int p = -1; n   char pch; n   int link = -1; n   int go[K]; n   long long int count = -1; n   int leaflink = 0; n   Vertex(int p = -1, char ch =  $ ) : p(p), pch(ch) { n     fill(begin(next), end(next), -1); n     fill(begin(go), end(go), -1); n   } n }; n vector<Vertex> t(1); n void add_string(string const& s) { n   int v = 0; n   for (char ch : s) { n     int c = ch -  a ; n     if (t[v].next[c] == -1) { n       t[v].next[c] = t.size(); n       t.emplace_back(v, ch); n     } n     v = t[v].next[c]; n   } n   t[v].leaf++; n } n int go(int v, char ch); n int get_link(int v); n long long int cnt(int v) { n   if (t[v].count == -1) { n     if (v == 0) n       t[v].count = 0; n     else n       t[v].count = cnt(get_link(v)) + t[v].leaf; n   } n   return t[v].count; n } n int get_link(int v) { n   if (t[v].link == -1) { n     if (v == 0 || t[v].p == 0) n       t[v].link = 0; n     else n       t[v].link = go(get_link(t[v].p), t[v].pch); n   } n   return t[v].link; n } n int go(int v, char ch) { n   int c = ch -  a ; n   if (t[v].go[c] == -1) { n     if (t[v].next[c] != -1) n       t[v].go[c] = t[v].next[c]; n     else n       t[v].go[c] = v == 0 ? 0 : go(get_link(v), ch); n   } n   return t[v].go[c]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   string ts; n   cin >> ts; n   int n; n   cin >> n; n   vector<string> v; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     v.push_back(s); n     reverse(s.begin(), s.end()); n     add_string(s); n   } n   int m = ts.size(); n   vector<long long int> a = vector<long long int>(m, 0); n   int cs = 0; n   for (int i = m - 1; i > -1; --i) { n     get_link(cs); n     cs = go(cs, ts[i]); n     get_link(cs); n     a[i] = cnt(cs); n   } n   t = vector<Vertex>(1); n   for (int i = 0; i < n; ++i) { n     add_string(v[i]); n   } n   cs = 0; n   long long int sol = 0; n   for (int i = 0; i < m - 1; ++i) { n     get_link(cs); n     cs = go(cs, ts[i]); n     get_link(cs); n     sol += cnt(cs) * a[i + 1]; n   } n   cout << sol << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void swap(int *a, int *b) { n   int temp = *a; n   *a = *b; n   *b = temp; n } n void swap(char *a, char *b) { n   char temp = *a; n   *a = *b; n   *b = temp; n } n int main() { n   int xa, xb, xc, ya, yb, yc; n   cin >> xa >> ya >> xb >> yb >> xc >> yc; n   char ca =  A , cb =  B , cc =  C ; n   int area = xa * ya + xb * yb + xc * yc; n   int n = sqrt(area); n   if (n * n != area) { n     cout << -1; n     return 0; n   } n   char arr[n][n]; n   if (xa < ya) swap(&xa, &ya); n   if (xb < yb) swap(&xb, &yb); n   if (xc < yc) swap(&xc, &yc); n   if (xb == n) { n     swap(&xa, &xb); n     swap(&ya, &yb); n     swap(&ca, &cb); n   } else if (xc == n) { n     swap(&xc, &xa); n     swap(&ya, &yc); n     swap(&ca, &cc); n   } n   if (xa != n) { n     cout << -1; n     return 0; n   } n   for (int i = 0; i < ya; i++) { n     for (int j = 0; j < n; j++) arr[i][j] = ca; n   } n   if (yb == yc && xb + xc == n && ya + yb == n) { n     for (int i = ya; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (j >= xb) n           arr[i][j] = cc; n         else n           arr[i][j] = cb; n       } n     } n   } else if (xb == xc && xb == n && ya + yb + yc == n) { n     for (int i = ya; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (i >= yb + ya) n           arr[i][j] = cc; n         else n           arr[i][j] = cb; n       } n     } n   } else if (xb == xc && yb + yc == n && ya + xb == n) { n     for (int i = ya; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (j >= yb) n           arr[i][j] = cc; n         else n           arr[i][j] = cb; n       } n     } n   } else if (xb == yc && yb + xc == n && ya + xb == n) { n     for (int i = ya; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (j >= yb) n           arr[i][j] = cc; n         else n           arr[i][j] = cb; n       } n     } n   } else if (yb == xc && xb + yc == n && ya + yb == n) { n     for (int i = ya; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (j >= xb) n           arr[i][j] = cc; n         else n           arr[i][j] = cb; n       } n     } n   } else { n     cout << -1; n     return 0; n   } n   cout << n << endl; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) cout << arr[i][j]; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int has = 0; n   ; n   cin >> n; n   int d[n - 1]; n   for (int i = 0; i < n - 1; i++) cin >> d[i]; n   int a, b; n   cin >> a >> b; n   int m; n   has = 0; n   for (int m = a - 1; m < b - 1; m++) { n     has = has + d[m]; n   } n   cout << has << endl; n } n


#include <bits/stdc++.h> n using namespace std; n class SuffixArrayBuilder { n   int n, gap; n   string s; n   vector<int> pos; n  n  public: n   SuffixArrayBuilder(string &s) : s(s) {} n   bool operator()(int i, int j) { n     if (pos[i] != pos[j]) return pos[i] < pos[j]; n     i += gap; n     j += gap; n     return ((i < n && j < n) ? pos[i] < pos[j] : i > j); n   } n   vector<int> buildSA() { n     n = (int)(s).size(); n     vector<int> sa(n), tmp(n); n     pos.resize(n); n     for (int i = (0); i <= ((int)(sa).size() - 1); ++i) n       sa[i] = i, pos[i] = s[i]; n     for (gap = 1;; gap *= 2) { n       sort(sa.begin(), sa.end(), ref(*this)); n       for (int i = (0); i <= (n - 2); ++i) n         tmp[i + 1] = tmp[i] + (*this)(sa[i], sa[i + 1]); n       for (int i = (0); i <= (n - 1); ++i) pos[sa[i]] = tmp[i]; n       if (tmp.back() == n - 1) return sa; n     } n   } n   vector<int> buildLCP(vector<int> &sa) { n     vector<int> lcp(n - 1); n     for (int i = 0, k = 0; i < n; ++i) n       if (pos[i] != n - 1) { n         for (int j = sa[pos[i] + 1]; s[i + k] == s[j + k];) k++; n         lcp[pos[i]] = k; n         if (k) k--; n       } n     return lcp; n   } n }; n vector<long long int> solve(string &p, string &q, string &r) { n   string s = p +  #  + q +  $  + r; n   SuffixArrayBuilder Builder(s); n   vector<int> SA = Builder.buildSA(); n   vector<int> LCP = Builder.buildLCP(SA); n   int n = (int)(s).size(); n   int cnt[3][n]; n   int len[] = {0, (int)(p).size() + 1, (int)(q).size() + 1, n                (int)(r).size() + 1}; n   for (int i = (1); i <= (3); ++i) len[i] += len[i - 1]; n   for (int i = (0); i <= (2); ++i) n     for (int j = (0); j <= (n - 1); ++j) { n       cnt[i][j] = ((j) ? cnt[i][j - 1] : 0); n       cnt[i][j] += (SA[j] >= len[i] && SA[j] < len[i + 1] - 1); n     } n   vector<pair<int, int> > ord; n   for (int i = (0); i <= ((int)(LCP).size() - 1); ++i) n     ord.push_back(make_pair(LCP[i], i)); n   sort(ord.begin(), ord.end()); n   set<int> S; n   S.insert(-1); n   S.insert(n - 1); n   int m = min({(int)(p).size(), (int)(q).size(), (int)(r).size()}); n   vector<long long int> agg(m, 0); n   for (int i = (0); i <= ((int)(ord).size() - 1); ++i) { n     int pos = ord[i].second, L, R; n     auto it = S.lower_bound(pos); n     R = *it; n     it--; n     L = *it; n     long long int left[3], right[3], whole[3]; n     for (int i = (0); i <= (2); ++i) { n       left[i] = cnt[i][pos] - ((L >= 0) ? cnt[i][L] : 0); n       right[i] = cnt[i][R] - cnt[i][pos]; n       whole[i] = cnt[i][R] - ((L >= 0) ? cnt[i][L] : 0); n     } n     long long int ans = whole[0] * whole[1] * whole[2] - n                         left[0] * left[1] * left[2] - n                         right[0] * right[1] * right[2]; n     ans %= 1000000007; n     ans = (ans + 1000000007) % 1000000007; n     int val = ord[i].first; n     if (val > 0) { n       agg[val - 1] += ans; n       agg[val - 1] %= 1000000007; n     } n     S.insert(pos); n   } n   for (int i = (m - 2); i >= (0); --i) n     agg[i] = (agg[i] + agg[i + 1]) % 1000000007; n   return agg; n } n int main() { n   ios_base::sync_with_stdio(0); n   string p, q, r; n   cin >> p >> q >> r; n   vector<long long int> ans = solve(p, q, r); n   for (int i = (0); i <= ((int)(ans).size() - 1); ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n int max(int a, int b) { return a > b ? a : b; } n int main() { n   static int aa[200000]; n   int n, d, q, i, h, j; n   scanf( %d%d , &n, &d); n   for (i = 0; i < n; i++) scanf( %d , &aa[i]); n   scanf( %d , &q); n   h = -1; n   while (q--) { n     int x, di, dj, d_; n     double alpha, beta; n     scanf( %d , &x); n     if (x == 0 || x == aa[n - 1] + 1) { n       printf( %.12f n , (atan(1) * 4) / 2); n       continue; n     } n     while (h + 1 < n && aa[h + 1] < x) h++; n     di = x - (aa[h] + 1); n     dj = aa[h + 1] - x; n     if (di == 0 && dj == 0) { n       printf( %.12f n , (atan(1) * 4)); n       continue; n     } else if (di == 0 || dj == 0) { n       printf( %.12f n , (atan(1) * 4) / 2); n       continue; n     } n     alpha = atan(1.0 / (di < dj ? di : dj)); n     d_ = (int)(1.0 / tan(alpha / 2.0) + 10); n     for (i = h, j = h + 1; i >= 0 && j < n && (di = x - (aa[i] + 1)) < d_ && n                            (dj = aa[j] - x) < d_;) n       if (di + 1 < dj) n         i--; n       else if (di > dj + 1) n         j++; n       else { n         if (alpha < (beta = atan(1.0 / max(di, dj)) * 2)) alpha = beta; n         break; n       } n     printf( %.12f n , alpha); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n using namespace std; n string s; n long long ispalindroma[2005][2005]; n long long ispalindrome(long long i, long long j) { n   if (i > j) return (1); n   if (i == j) return (1); n   if (ispalindroma[i][j] != -1) return (ispalindroma[i][j]); n   long long ans = 0; n   if (s[i] == s[j]) { n     ans = ispalindrome(i + 1, j - 1); n   } n   return (ispalindroma[i][j] = ans); n } n int solve() { n   cin >> s; n   long long sum[2005], dp[2005]; n   dp[0] = sum[0] = 0; n   for (long long i = 0; i < 2005; i++) { n     for (long long j = 0; j < 2005; j++) ispalindroma[i][j] = -1; n   } n   long long a1 = ispalindrome(0, s.size() - 1); n   for (long long i = 0; i < s.size(); i++) { n     long long c = 0; n     for (long long j = 0; j <= i; j++) { n       if (ispalindrome(j, i)) c++; n     } n     sum[i + 1] = sum[i] + c; n   } n   for (long long i = 0; i < s.size(); i++) { n     long long ans = 0; n     for (long long j = 0; j <= i; j++) { n       if (ispalindrome(j, i)) ans = ans + sum[j]; n     } n     dp[i + 1] = dp[i] + ans; n   } n   cout << dp[s.size()] <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, z; n   cin >> x >> y >> z; n   if (x >= y) { n     x = x - y; n     if (x > z) { n       cout <<  + ; n     } else if (x <= z) { n       if (z == 0) { n         cout <<  0 ; n       } else { n         cout <<  ? ; n       } n     } n   } else if (y > x) { n     y = y - x; n     if (y > z) { n       cout <<  - ; n     } else if (y <= z) { n       if (z == 0) { n         cout <<  0 ; n       } else { n         cout <<  ? ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long int n, x, y, i, j, k = 0, s; n   scanf( %lld %lld %lld , &n, &x, &y); n   long long int X[n], Y[n]; n   for (i = 0; i < n; i++) { n     scanf( %lld %lld , &X[i], &Y[i]); n     for (j = i - 1; j >= 0; j--) { n       s = x * Y[i] + X[i] * Y[j] + X[j] * y - X[i] * y - X[j] * Y[i] - x * Y[j]; n       if (s == 0) { n         break; n       } n     } n     if (j < 0) { n       k++; n     } n   } n   printf( %lld n , k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000007; n int d[210][210][510][2]; n short vis[210][210][510][2]; n int q, n, k, b, cas; n int nex[210][22], out[210]; n int fal[210]; n int s[210]; n int dp(int pos, int len, int w, int m) { n   w = min(w, k + 5); n   if (vis[pos][len][w][m] == cas) return d[pos][len][w][m]; n   vis[pos][len][w][m] = cas; n   if (w > k) return d[pos][len][w][m] = 0; n   if (len == n) return d[pos][len][w][m] = 1; n   int res = 0; n   for (int i = 0; i < b; ++i) { n     if (m == 0 && i > s[len]) break; n     int nst = nex[pos][i], mm = m | i < s[len]; n     int ww = w + out[nst]; n     res += dp(nst, len + 1, ww, mm); n     if (res >= inf) res -= inf; n   } n   return d[pos][len][w][m] = res; n } n int nn; n void add(int len, int w) { n   int pos = 0, i = 0; n   while (i < len) { n     int ii = s[i++]; n     if (!nex[pos][ii]) nex[pos][ii] = ++nn; n     pos = nex[pos][ii]; n   } n   out[pos] += w; n } n void bfs() { n   queue<int> q; n   q.push(0); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = 0; i < b; ++i) { n       if (!nex[x][i]) n         nex[x][i] = nex[fal[x]][i]; n       else { n         int y = nex[x][i]; n         if (x == 0) n           fal[y] = 0; n         else n           fal[y] = nex[fal[x]][i]; n         out[y] += out[fal[y]]; n         q.push(y); n       } n     } n   } n } n int aa[210], bb[210]; n int lena, lenb; n int down() { n   int res = 0; n   for (int pos = 0, i = 0; i < lena; ++i) { n     pos = nex[pos][aa[i]]; n     res += out[pos]; n   } n   return res <= k; n } n int ask() { n   ++cas; n   long long ans = 0; n   for (int i = 1; i < s[0]; ++i) ans += dp(nex[0][i], 1, out[nex[0][i]], 1); n   ans += dp(nex[0][s[0]], 1, out[nex[0][s[0]]], 0); n   for (int i = 1; i < n; ++i) n     for (int j = 1; j < b; ++j) ans += dp(nex[0][j], i + 1, out[nex[0][j]], 1); n   return ans % inf; n } n int main() { n   scanf( %d %d %d , &q, &b, &k); n   scanf( %d , &lena); n   for (int i = 0; i < lena; ++i) scanf( %d , &aa[i]); n   scanf( %d , &lenb); n   for (int i = 0; i < lenb; ++i) scanf( %d , &bb[i]); n   int len, w; n   while (q--) { n     scanf( %d , &len); n     for (int i = 0; i < len; ++i) scanf( %d , &s[i]); n     scanf( %d , &w); n     add(len, w); n   } n   bfs(); n   cas = 1; n   n = lena; n   for (int i = 0; i < lena; ++i) s[i] = aa[i]; n   int res = ask(); n   cas = 2; n   n = lenb; n   for (int i = 0; i < lenb; ++i) s[i] = bb[i]; n   int ans = ask(); n   cout << (ans + down() + inf - res) % inf << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = (long long int)1e9 + 7; n long long int powmod(long long int a, long long int b) { n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % MOD; n     a = (a * a) % MOD; n     b = b >> 1; n   } n   return res % MOD; n } n long long int num_swaps(vector<long long int> &arr, long long int k) { n   long long int n = arr.size(); n   vector<long long int> pos(n); n   for (long long int i = 0; i < n; i++) { n     long long int val = (n + arr[i] + k) % n; n     pos[val] = i; n   } n   vector<long long int> vis(n, 0); n   long long int cnt_cycles = 0; n   for (long long int i = 0; i < n; i++) { n     if (vis[i] == 0) { n       cnt_cycles++; n       long long int cur_node = i; n       while (1) { n         cur_node = pos[cur_node]; n         if (vis[cur_node] == 1) { n           break; n         } n         vis[cur_node] = 1; n       } n     } n   } n   return (n - cnt_cycles); n } n void solve(long long int testnum) { n   long long int n, m; n   cin >> n >> m; n   vector<long long int> a(n), b(n, 0), ans; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n   } n   for (long long int i = 0; i < n; i++) { n     long long int tmp = (n + i - a[i]) % n; n     b[tmp]++; n   } n   long long int cnt = 0; n   for (long long int i = 0; i < n; i++) { n     if (b[i] >= n - 2 * m && num_swaps(a, i) <= m) { n       ans.push_back(i); n     } n   } n   cout << ans.size() <<    ; n   for (long long int val : ans) { n     cout << val <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   cin >> t; n   for (long long int i = 1; i <= t; i++) { n     solve(i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct st { n   long long int num; n   long long int big; n } w[200001]; n bool cmp(st a, st b) { return a.big < b.big; } n int main() { n   std::ios::sync_with_stdio(false); n   long long int n, i; n   char p; n   while (cin >> n) { n     queue<long long int> q; n     stack<st> ss; n     for (i = 0; i < n; i++) { n       cin >> w[i].big; n       w[i].num = i + 1; n     } n     long long int t = 0; n     sort(w, w + n, cmp); n     for (i = 0; i < 2 * n; i++) { n       cin >> p; n       if (p ==  0 ) { n         q.push(w[t].num); n         ss.push(w[t]); n         t++; n       } n       if (p ==  1 ) { n         st a; n         a = ss.top(); n         q.push(a.num); n         ss.pop(); n       } n     } n     while (!q.empty()) { n       cout << q.front() <<    ; n       q.pop(); n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int INF = 1e9; n const long long mod = 1e9 + 7; n int n, a, b, v; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> a >> b >> v; n     cin >> s; n     int zero = 0, one = 0; n     for (auto isi : s) { n       if (isi ==  1 ) n         one++; n       else n         zero++; n     } n     cout << min(zero * a + one * b, min(n * a + one * v, n * b + zero * v)) n          <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10, logn = 20, inf = 2e5 + 10; n int L[maxn * logn], R[maxn * logn], sum[maxn * logn]; n int root[maxn]; n int sz = 1, rt = 1; n int copy(int v, int &u) { n   L[sz] = L[v]; n   R[sz] = R[v]; n   sum[sz] = sum[v]; n   return u = sz++; n } n void add(int x, int v = root[rt - 1], int l = 0, int r = +inf) { n   if (r - l == 1) { n     sum[v]++; n     return; n   } n   int m = (l + r) / 2; n   if (x < m) n     add(x, copy(L[v], L[v]), l, m); n   else n     add(x, copy(R[v], R[v]), m, r); n   sum[v] = sum[L[v]] + sum[R[v]]; n } n void push(int x) { n   copy(root[rt - 1], root[rt]); n   rt++; n   add(x); n } n int get(int a, int b, int v, int l = 0, int r = +inf) { n   if (a <= l && r <= b) return sum[v]; n   if (r <= a || b <= l) return 0; n   int m = (l + r) / 2; n   return get(a, b, L[v], l, m) + get(a, b, R[v], m, r); n } n int cnt(int l, int r, int x) { n   return get(0, x, root[r - 1]) - get(0, x, root[l - 1]); n } n int x[maxn], y[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &x[i]), y[i] = x[i]; n   sort(y, y + n); n   for (int i = 0; i < n; i++) { n     x[i] = lower_bound(y, y + n, x[i]) - y; n     push(x[i]); n   } n   int ans[n]; n   memset(ans, 0, sizeof(ans)); n   for (int k = 1; k < n; k++) n     for (int i = 1; k * (i - 1) + 2 <= n; i++) n       ans[k - 1] += cnt(k * (i - 1) + 2, min(n + 1, k * i + 2), x[i - 1]); n   for (int i = 0; i < n - 1; i++) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long num[100005], sorted[100005]; n long long prenum[100005], presorted[100005]; n int main() { n   int n, q, l, r, op; n   cin >> n; n   for (__typeof(n) i = 0; i < n; i++) { n     cin >> num[i]; n     sorted[i] = num[i]; n   } n   sort(sorted, sorted + n); n   prenum[0] = 0; n   presorted[0] = 0; n   for (__typeof(n + 1) i = 1; i < n + 1; i++) { n     prenum[i] = prenum[i - 1] + num[i - 1]; n     presorted[i] = presorted[i - 1] + sorted[i - 1]; n   } n   cin >> q; n   for (__typeof(q) i = 0; i < q; i++) { n     cin >> op >> l >> r; n     switch (op) { n       case 1: n         cout << prenum[r] - prenum[l - 1] << endl; n         break; n       case 2: n         cout << presorted[r] - presorted[l - 1] << endl; n         break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int root; n void addEdge(vector<int> adj[], int u, int v) { n   if (u == -1) { n     root = v; n     return; n   } n   adj[u].push_back(v); n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   vector<int> res; n   int n, ctr, c; n   cin >> n; n   vector<int> adj[n + 1]; n   vector<bool> vis(n + 1, false); n   for (int i = 0; i < n; i++) { n     cin >> ctr >> c; n     addEdge(adj, ctr, i + 1); n     if (c) vis[i + 1] = true; n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) continue; n     auto it = adj[i].begin(); n     for (; it != adj[i].end(); it++) { n       if (!vis[*it]) break; n     } n     if (it == adj[i].end()) res.push_back(i); n   } n   if (res.size() == 0) n     cout << -1; n   else n     for (int x : res) cout << x <<    ; n } n


#include <bits/stdc++.h> n constexpr double EPS = 1e-9; n using namespace std; n int sz = 0; n int n, m; n vector<vector<pair<int, int>>> ss; n bool vis[51][51]; n string grid[51]; n void dfs(int r, int c) { n   vis[r][c] = 1; n   ss[sz].push_back({r, c}); n   for (int i = 0; i < n; i++) { n     if (grid[i][c] ==  #  && !vis[i][c]) dfs(i, c); n   } n   for (int i = 0; i < m; i++) { n     if (grid[r][i] ==  #  && !vis[r][i]) dfs(r, i); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> grid[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (grid[i][j] ==  #  && !vis[i][j]) { n         ss.push_back(vector<pair<int, int>>()); n         dfs(i, j); n         sz++; n       } n     } n   } n   for (auto el : ss) { n     int rr[51] = {0}; n     int cc[51] = {0}; n     for (auto qq : el) { n       rr[qq.first] = 1; n       cc[qq.second] = 1; n     } n     int a, b; n     a = b = 0; n     for (int i = 0; i < 50; i++) { n       a += rr[i]; n       b += cc[i]; n     } n     if (a * b != el.size()) { n       cout <<  No ; n       return 0; n     } n   } n   cout <<  Yes ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n set<int> S[100001]; n int main() { n   scanf( %d , &n); n   set<int> s; n   int x, k; n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &x, &k); n     if (x && S[k].find(x - 1) == S[k].end()) { n       puts( NO ); n       return 0; n     } n     S[k].insert(x); n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5, inf = 1e9 + 7; n long long n, k, i, j; n long long C[N], f[3][N]; n pair<long long, long long> A[N]; n inline long long util(long long a, long long b, long long c) { n   return f[c][a] - f[c][b]; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long i, j, a, b, ans = 0, m, temp, c, d, e, ff, cur = 0; n   cin >> n >> k; n   for (i = 0; i < k; i++) { n     cin >> A[i].first >> A[i].second; n     C[A[i].first]++; n     C[A[i].second + 1]--; n   } n   sort(A, A + k); n   for (i = 1; i < n + 2; i++) C[i] += C[i - 1]; n   for (i = 1; i < n + 1; i++) n     if (C[i]) cur++; n   for (i = 1; i < n + 1; i++) n     for (j = 1; j < 3; j++) n       if (C[i] == j) f[j][i]++; n   for (i = 1; i < n + 1; i++) n     for (j = 1; j < 3; j++) f[j][i] += f[j][i - 1]; n   temp = 0; n   for (i = 0; i < k; i++) n     for (j = i + 1; j < k; j++) { n       if (A[i].second < A[j].first) { n         temp = cur - util(A[i].second, A[i].first - 1, 1) - n                util(A[j].second, A[j].first - 1, 1); n       } else if (A[i].second <= A[j].second) { n         a = A[i].first - 1; n         b = A[j].first - 1; n         c = A[j].first - 1; n         d = A[i].second; n         e = A[i].second; n         ff = A[j].second; n         temp = cur - util(b, a, 1) - util(d, c, 2) - util(ff, e, 1); n       } else { n         a = A[i].first - 1; n         b = A[j].first - 1; n         c = A[j].first - 1; n         d = A[j].second; n         e = A[j].second; n         ff = A[i].second; n         temp = cur - util(b, a, 1) - util(d, c, 2) - util(ff, e, 1); n       } n       ans = max(ans, temp); n     } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e5 + 1; n int n, rt, tot, sz[maxn], bel[maxn], in[maxn], out[maxn], seg[maxn << 1 | 1]; n vector<pair<int, int> > e[maxn]; n set<int> sp[maxn]; n long long ans; n void dfs(int u, long long dis, int ori) { n   sz[u] = 1; n   ans += dis; n   bel[u] = ori; n   in[u] = ++tot; n   for (auto it : e[u]) { n     int v = it.first; n     int w = it.second; n     if (!in[v]) { n       dfs(v, dis + w, ori); n       sz[u] += sz[v]; n     } n   } n   out[u] = tot; n } n inline int seg_idx(int L, int R) { return (L + R) | (L < R); } n void seg_upd(int L, int R, int x, int v) { n   int rt = seg_idx(L, R); n   if (L == R) { n     seg[rt] = v; n   } else { n     int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); n     if (x <= M) { n       seg_upd(L, M, x, v); n     } else { n       seg_upd(M + 1, R, x, v); n     } n     seg[rt] = min(seg[lch], seg[rch]); n   } n } n int seg_que(int L, int R, int l, int r) { n   if (l <= L && R <= r) return seg[seg_idx(L, R)]; n   int M = (L + R) >> 1, ret = n + 1; n   if (l <= M) ret = min(ret, seg_que(L, M, l, r)); n   if (M < r) ret = min(ret, seg_que(M + 1, R, l, r)); n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     e[u].push_back(make_pair(v, w)); n     e[v].push_back(make_pair(u, w)); n   } n   int *que = bel; n   tot = 0; n   memset(in + 1, 0, n * sizeof(int)); n   que[++tot] = 1; n   in[1] = tot; n   for (int i = 1; i <= n; ++i) { n     int u = que[i]; n     for (auto it : e[u]) { n       int v = it.first; n       if (!in[v]) { n         que[++tot] = v; n         in[v] = tot; n       } n     } n   } n   for (int i = n; i >= 1; --i) { n     int u = que[i], mx = 0; n     sz[u] = 1; n     for (auto it : e[u]) { n       int v = it.first; n       if (in[v] > in[u]) { n         sz[u] += sz[v]; n         mx = max(mx, sz[v]); n       } n     } n     if (max(mx, n - sz[u]) << 1 <= n) rt = u; n   } n   tot = ans = 0; n   memset(in + 1, 0, n * sizeof(int)); n   in[rt] = ++tot; n   bel[rt] = rt; n   for (auto it : e[rt]) { n     int v = it.first; n     int w = it.second; n     dfs(v, w, v); n     sp[sz[v] <<= 1].insert(v); n   } n   out[rt] = tot; n   printf( %lld n , ans <<= 1); n   for (int i = 1; i <= n; ++i) seg_upd(1, n, in[i], i); n   for (int i = 1; i <= n; ++i) { n     int p = 0; n     for (auto u : sp[n - i + 1]) n       if (u != bel[i]) p = u; n     if (!p && i == rt) p = rt; n     if (p) { n       p = seg_que(1, n, in[p], out[p]); n     } else { n       p = n + 1; n       if (in[bel[i]] > 1) p = min(p, seg_que(1, n, 1, in[bel[i]] - 1)); n       if (out[bel[i]] < n) p = min(p, seg_que(1, n, out[bel[i]] + 1, n)); n     } n     printf( %d%c , p,    n [i == n]); n     seg_upd(1, n, in[p], n + 1); n     if (i != rt) { n       sp[sz[bel[i]]].erase(bel[i]); n       sp[--sz[bel[i]]].insert(bel[i]); n     } n     if (p != rt) { n       sp[sz[bel[p]]].erase(bel[p]); n       sp[--sz[bel[p]]].insert(bel[p]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> cnt; n int lp[100005]; n long long p[100005][102]; n void sieve() { n   for (int i = 2; i <= 100000; i++) { n     if (!lp[i]) { n       lp[i] = i; n       for (int j = i + i; j <= 100000; j += i) n         if (!lp[j]) lp[j] = i; n     } n   } n } n int main() { n   int n, k, a; n   long long ans = 0; n   sieve(); n   for (int i = 1; i <= 100000; i++) { n     p[i][0] = 1; n     for (int j = 1; j <= 100; j++) { n       p[i][j] = p[i][j - 1] * i; n       if (p[i][j] > 1e9) p[i][j] = 0; n     } n   } n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     long long cur = 1; n     long long mate = 1; n     while (a > 1) { n       int cnt = 0; n       int prime = lp[a]; n       while (a % prime == 0) { n         a /= prime; n         cnt++; n       } n       cnt %= k; n       cur *= p[prime][cnt]; n       mate *= p[prime][(k - cnt) % k]; n       if (mate > 1e9) mate = 0; n     } n     if (mate != 0 && cnt.count(mate)) ans += cnt[mate]; n     cnt[cur]++; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n, k, num = 0; n   cin >> n >> k; n   vector<pair<int, int> > a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     a[i].first = -a[i].first; n   } n   (sort(a.begin(), a.end())); n   int fi = a[k - 1].first, sec = a[k - 1].second; n   for (int i = 0; i < n; i++) { n     if (fi == a[i].first && sec == a[i].second) { n       num++; n     } n   } n   cout << num; n } n


#include <bits/stdc++.h> n long long gcd(long long A, long long B) { n   return A > B ? gcd(B, A) : A ? gcd(B % A, A) : B; n } n long long lcm(long long A, long long B) { return A / gcd(A, B) * B; } n long long pow(long long A, long long B, long long P) { n   if (!B) return 1 % P; n   if (B == 1) return A % P; n   long long h = pow(A, B / 2, P); n   h *= h; n   h %= P; n   if (B % 2) h *= A; n   return h % P; n } n using namespace std; n int n, m, q, i, j, I, J; n int a[41][41]; n int sum[41][41]; n int res[41][41][41][41]; n int all[41][41][41][41]; n char ch; n int get(int i, int j, int I, int J) { n   return sum[I][J] - sum[i - 1][J] - sum[I][j - 1] + sum[i - 1][j - 1]; n } n bool f(int k) { n   if (k == 0) n     return true; n   else n     return false; n } n int main() { n   cin >> n >> m >> q; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) { n       cin >> ch; n       a[i][j] = ch -  0 ; n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; n     } n   for (i = n; i >= 1; i--) n     for (j = m; j >= 1; j--) n       if (!a[i][j]) { n         all[i][j][i][j] = 1; n         for (I = i - 1; I >= 1; I--) n           all[I][j][i][j] = all[I + 1][j][i][j] + f(get(I, j, i, j)); n         for (J = j - 1; J >= 1; J--) n           all[i][J][i][j] = all[i][J + 1][i][j] + f(get(i, J, i, j)); n         for (I = i - 1; I >= 1; I--) n           for (J = j - 1; J >= 1; J--) n             all[I][J][i][j] = all[I][J + 1][i][j] + all[I + 1][J][i][j] - n                               all[I + 1][J + 1][i][j] + f(get(I, J, i, j)); n       } n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) { n       if (!a[i][j]) res[i][j][i][j] = 1; n       for (I = i + 1; I <= n; I++) n         res[i][j][I][j] = res[i][j][I - 1][j] + all[i][j][I][j]; n       for (J = j + 1; J <= m; J++) n         res[i][j][i][J] = res[i][j][i][J - 1] + all[i][j][i][J]; n       for (I = i + 1; I <= n; I++) n         for (J = j + 1; J <= m; J++) n           res[i][j][I][J] = res[i][j][I][J - 1] + res[i][j][I - 1][J] - n                             res[i][j][I - 1][J - 1] + all[i][j][I][J]; n     } n   while (q--) { n     cin >> i >> j >> I >> J; n     cout << res[i][j][I][J] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000001]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int m; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     a[x + 1] += a[x] - y; n     a[x - 1] += y - 1; n     a[x] = 0; n   } n   for (int i = 1; i <= n; i++) { n     cout << a[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5010; n int v[MAXN], n, k; n int vec[1000100][12]; n bool vis[1000100]; n void crivo() { n   for (int i = 1; i <= 1000001; i++) { n     set<int> s; n     for (int j = 0; j <= 1000000; j += i) { n       for (int t = 1; t <= 11; t++) { n         int at = vec[j][t]; n         if (at == -1) break; n         s.insert(at); n         if (s.size() > 2 * k) { n           vis[i] = true; n           break; n         } n       } n       if (vis[i] == true) break; n     } n     if (vis[i] == true) continue; n     int q = 0; n     unordered_map<int, int> mp; n     for (auto j : s) mp[v[j] % i]++; n     for (auto j : mp) q += j.second - 1; n     if (q > k) vis[i] = true; n     if (vis[i] == false) { n       printf( %d n , i); n       return; n     } n   } n } n vector<int> vet[1000100]; n int cnt[1000100]; n int main() { n   memset(cnt, 0, sizeof cnt); n   memset(vec, -1, sizeof vec); n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &v[i]); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       int h = abs(v[i] - v[j]); n       if (cnt[h] > 10) continue; n       vec[h][cnt[h] + 1] = i, cnt[h]++; n     } n   } n   crivo(); n } n


#include <bits/stdc++.h> n using namespace std; n int a[1505]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   bool isEven = true; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (a[i] > a[j]) { n         isEven ^= 1; n       } n     } n   } n   int q; n   cin >> q; n   for (int qq = 0; qq < q; qq++) { n     int l, r; n     cin >> l >> r; n     int len = r - l + 1; n     isEven ^= ((len * (len - 1)) / 2) % 2; n     cout << (isEven ?  even  :  odd ) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long int p = 1; n   for (int i = 0; i < 5; i++) { n     p *= n - i; n   } n   cout << p * (p / 120); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n const long long INF = 1e18L + 7; n template <class n, class second> n ostream &operator<<(ostream &p, pair<n, second> x) { n   return p <<  <  << x.first <<  ,   << x.second <<  > ; n } n template <class n> n auto operator<<(ostream &p, n y) -> n     typename enable_if<!is_same<n, string>::value, n                        decltype(y.begin(), p)>::type { n   int o = 0; n   p <<  { ; n   for (auto c : y) { n     if (o++) p <<  ,  ; n     p << c; n   } n   return p <<  } ; n } n void dor() { cerr <<   n ; } n template <class n, class... second> n void dor(n p, second... y) { n   cerr << p <<    ; n   dor(y...); n } n template <class n, class second> n void mini(n &p, second y) { n   if (p > y) p = y; n } n template <class n, class second> n void maxi(n &p, second y) { n   if (p < y) p = y; n } n const int N = 57; n int n; n vector<int> G[N]; n long double kom[N][N][N][N]; n long double bez[N][N]; n long double kra[N][N]; n int sub[N]; n void merge(int v, int u) { n   long double tmp[N]; n   memset(tmp, 0, sizeof tmp); n   for (int i = 0; i <= sub[v]; ++i) { n     for (int j = 0; j <= sub[u]; ++j) { n       tmp[i + j] += n           bez[v][i] * kra[u][j] * kom[sub[v] + sub[u]][i + j][sub[v]][i]; n     } n   } n   memcpy(bez[v], tmp, sizeof bez[v]); n   sub[v] += sub[u]; n } n void dfs(int v, int p) { n   bez[v][0] = 1; n   for (auto u : G[v]) { n     if (u == p) continue; n     dfs(u, v); n     merge(v, u); n     ; n   } n   sub[v]++; n   if (v == 1) n     ; n   for (int second = 0; second <= sub[v]; ++second) { n     for (int wyb = 1; wyb <= sub[v]; ++wyb) { n       int oki = max(wyb - 1, second - 1); n       long double prob = 1.0 / sub[v]; n       if (wyb > second) prob /= 2; n       kra[v][second] += bez[v][oki] * prob; n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j <= i; ++j) { n       kom[i][j][0][0] = 1; n     } n   } n   for (int i = 1; i < N; ++i) { n     for (int j = 0; j < N; ++j) { n       for (int k = 1; k < N; ++k) { n         for (int l = 0; l < N; ++l) { n           long double ok = (long double)j / i; n           if (j > 0 && l > 0) n             kom[i][j][k][l] += ok * kom[i - 1][j - 1][k - 1][l - 1]; n           kom[i][j][k][l] += (1 - ok) * kom[i - 1][j][k - 1][l]; n         } n       } n     } n   }; n   cout << fixed << setprecision(15); n   cin >> n; n   if (n == 1) { n     cout << 1 <<   n ; n     return 0; n   } n   for (int i = 1; i < n; ++i) { n     int a, b; n     cin >> a >> b; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       memset(bez[j], 0, sizeof bez[j]); n       memset(kra[j], 0, sizeof kra[j]); n     } n     memset(sub, 0, sizeof sub); n     dfs(i, -1); n     cout << bez[i][0] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200200; n int n, k, cnt[MAXN]; n long long ans; n bool f[MAXN]; n vector<int> g[MAXN]; n void dfs(int v, int p = -1) { n   cnt[v] = f[v]; n   for (const int &u : g[v]) n     if (u != p) dfs(u, v), cnt[v] += cnt[u]; n } n void solve(int v, int p = -1) { n   for (const int &u : g[v]) n     if (u != p) { n       ans += min(2 * k - cnt[u], cnt[u]); n       solve(u, v); n     } n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1, x; i <= 2 * k; ++i) scanf( %d , &x), f[x] = true; n   for (int i = 1, u, v; i < n; ++i) n     scanf( %d %d , &u, &v), g[u].push_back(v), g[v].push_back(u); n   dfs(1); n   solve(1); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int r, c; n string str_col[1000006]; n string str_dir[1000006]; n vector<int> e[1000006], rev[1000006]; n int col[1000006], vis[1000006], vis2[1000006]; n int cyc_len, black_cnt; n inline int id(int x, int y) { return c * x + y; } n vector<int> st; n int got[1000006]; n void dfs2(int v, int mod, int d = 0) { n   if (0) cerr <<  vis2   << v <<     << col[v] <<   n ; n   vis2[v] = 1; n   got[d] |= col[v]; n   for (int u : rev[v]) { n     if (not vis2[u]) dfs2(u, mod, (d + 1) % mod); n   } n } n void dfs(int v) { n   if (vis[v] or vis2[v]) return; n   if (0) cerr <<  dfs   << v <<   n ; n   vis[v] = 1; n   st.push_back(v); n   for (int u : e[v]) { n     if (not vis[u]) n       dfs(u); n     else { n       int cyc = st.end() - find(st.begin(), st.end(), u); n       if (cyc == 0) continue; n       fill(got, got + cyc, 0); n       if (0) cerr <<  call cyc   << v <<     << u <<   n ; n       dfs2(u, cyc); n       black_cnt += accumulate(got, got + cyc, 0); n       cyc_len += cyc; n     } n   } n   st.pop_back(); n } n void solve() { n   cin >> r >> c; n   for (int i = 0; i < r; i++) cin >> str_col[i]; n   for (int i = 0; i < r; i++) cin >> str_dir[i]; n   for (int x = 0; x < r; x++) n     for (int y = 0; y < c; y++) { n       int nx = x, ny = y; n       if (str_dir[x][y] ==  U ) nx--; n       if (str_dir[x][y] ==  D ) nx++; n       if (str_dir[x][y] ==  L ) ny--; n       if (str_dir[x][y] ==  R ) ny++; n       e[id(x, y)].push_back(id(nx, ny)); n       rev[id(nx, ny)].push_back(id(x, y)); n       if (0) cerr <<  rev   << id(nx, ny) <<     << id(x, y) <<   n ; n       col[id(x, y)] = str_col[x][y] ==  0 ; n     } n   int n = r * c; n   for (int i = 0; i < n; i++) { n     if (not vis[i] and not vis2[i]) { n       if (0) cerr <<  call dfs   << i <<   n ; n       dfs(i); n     } n   } n   cout << cyc_len <<     << black_cnt <<   n ; n } n void reset() { n   for (int i = 0; i < r * c; i++) { n     e[i].clear(); n     rev[i].clear(); n     vis[i] = vis2[i] = cyc_len = black_cnt = 0; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   cin >> t; n   for (int no = 1; no <= t; no++) { n     solve(); n     reset(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   float a1, b1, c1, a2, b2, c2; n   cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2; n   if (!b1 && !b2) { n     if ((!a1 && c1) || (!a1 && c2)) { n       cout << 0 << endl; n       return 0; n     } n     if ((!c1 && !c2) || (!a1 && !a2) || c1 / a1 == c2 / a2) { n       cout << -1 << endl; n       return 0; n     } else { n       cout << 0 << endl; n       return 0; n     } n   } else if ((!b1 && !a1 && !c1) || (!b2 && !a2 && !c2)) { n     cout << -1 << endl; n     return 0; n   } else if ((!b1 && !a1) || (!b2 && !a2)) { n     cout << 0 << endl; n     return 0; n   } else if (!b1 || !b2) { n     cout << 1 << endl; n     return 0; n   } n   if (a1 / b1 == a2 / b2 && c1 / b1 == c2 / b2) n     cout << -1 << endl; n   else if (a1 / b1 == a2 / b2) n     cout << 0 << endl; n   else n     cout << 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n vector<vector<long long> > a, b, orig; n bool swapd = false; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   orig.resize(n); n   for (long long i = 0; i < n; i++) { n     orig[i].resize(m); n     for (long long j = 0; j < m; j++) { n       orig[i][j] = i * m + j + 1; n     } n   } n   if (n > m) { n     swap(n, m); n     swapd = true; n   } n   if (n == 1 && m == 1) { n     cout <<  YES n ; n     cout <<  1 n ; n     return 0; n   } n   if (n == 1 && m == 2) { n     cout <<  NO n ; n     return 0; n   } n   if (n == 1 && m == 3) { n     cout <<  NO n ; n     return 0; n   } n   if (n == 2 && m == 2) { n     cout <<  NO n ; n     return 0; n   } n   if (n == 2 && m == 3) { n     cout <<  NO n ; n     return 0; n   } n   if (n == 3 && m == 3) { n     cout <<  YES n6 1 8 n7 5 3 n2 9 4 n ; n     return 0; n   } n   if (n == 1 && m >= 4) { n     if (swapd == false) { n       cout <<  YES n ; n       for (long long i = 2; i <= m; i += 2) cout << i <<    ; n       for (long long i = 1; i <= m; i += 2) cout << i <<    ; n     } else { n       cout <<  YES n ; n       for (long long i = 2; i <= m; i += 2) cout << i <<   n ; n       for (long long i = 1; i <= m; i += 2) cout << i <<   n ; n     } n     return 0; n   } n   cout <<  YES n ; n   a.resize(n); n   if (swapd) { n     for (long long i = 0; i < n; i++) { n       a[i].resize(m); n       for (long long j = 0; j < m; j++) a[i][j] = orig[j][i]; n     } n   } else { n     for (long long i = 0; i < n; i++) { n       a[i].resize(m); n       for (long long j = 0; j < m; j++) { n         a[i][j] = i * m + j + 1; n       } n     } n   } n   b.resize(n); n   for (long long i = 0; i < n; i++) { n     b[i].resize(m); n     if (i % 2 == 1) { n       b[i] = a[i]; n       continue; n     } n     for (long long j = 0; j < m; j++) { n       b[i][j] = a[i][((j - 2) % m + m) % m]; n     } n   } n   a = b; n   for (long long j = 0; j < m; j++) { n     if (j % 2 == 0) { n       for (long long i = 0; i < n; i++) { n         b[i][j] = a[(i + 1) % n][j]; n       } n     } n   } n   if (swapd) { n     for (long long j = 0; j < m; j++) { n       for (long long i = 0; i < n; i++) cout << b[i][j] <<    ; n       cout <<   n ; n     } n   } else { n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         cout << b[i][j] <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n