#include <bits/stdc++.h> n using namespace std; n struct ppp { n   int x1, x2, y1, y2; n   void make_pair(int x1_, int x2_, int y1_, int y2_) { n     x1 = x1_; n     x2 = x2_; n     y1 = y1_; n     y2 = y2_; n   } n } a[120]; n long long ans; n int n, m, i, j, k, x, y, l, r, P[220], tot, Pn, u, d; n char c[1010][1010]; n int Find(int x) { return lower_bound(P + 1, P + Pn + 1, x) - P; } n int main() { n   scanf( %d%d%*d , &n, &m); n   for (i = 1; i <= n; ++i) scanf( %s , c[i] + 1); n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= m; ++j) n       if (c[i][j] ==  * ) { n         k = j; n         while (c[i][k] ==  * ) k++; n         k--; n         l = i; n         while (c[l][j] ==  * ) l++; n         l--; n         for (x = i; x <= l; ++x) n           for (y = j; y <= k; ++y) c[x][y] =  . ; n         a[++tot].make_pair(i, l, j, k); n         P[++Pn] = i; n         P[++Pn] = l; n       } n   P[++Pn] = 0; n   P[++Pn] = n + 1; n   sort(P + 1, P + Pn + 1); n   Pn = unique(P + 1, P + Pn + 1) - P - 1; n   for (i = 1; i <= tot; ++i) a[i].x1 = Find(a[i].x1), a[i].x2 = Find(a[i].x2); n   for (u = 2; u < Pn; ++u) { n     for (d = u; d < Pn; ++d) { n       long long tmp = 0; n       vector<pair<int, int> > Q; n       for (i = 1; i <= tot; ++i) { n         if (a[i].x2 < u || a[i].x1 > d) continue; n         if (a[i].x1 >= u && a[i].x2 <= d) n           Q.push_back(make_pair(a[i].y1, -1)), n               Q.push_back(make_pair(a[i].y2, 1)); n         else n           Q.push_back(make_pair(a[i].y1, -100)), n               Q.push_back(make_pair(a[i].y2, 100)); n       } n       sort(Q.begin(), Q.end()); n       int num = Q.size(), C, suml, sumr, cc; n       Q.push_back(make_pair(m + 1, 0)); n       for (int C = 1; C <= 3; ++C) { n         suml = sumr = 0; n         cc = 0; n         for (l = 0, r = 0; r < num; ++r) { n           sumr += Q[r].second; n           cc += max(0, Q[r].second); n           if (!sumr) { n             while (suml || cc > C) { n               suml -= Q[l].second; n               cc -= max(0, Q[l].second); n               l++; n             } n             if (!suml && cc == C) n               tmp += (Q[r + 1].first - Q[r].first) * n                      (Q[l].first - (!l ? 0 : Q[l - 1].first)); n           } n         } n       } n       ans += tmp * (P[u] - P[u - 1]) * (P[d + 1] - P[d]); n     } n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n int n, k, s, e, m, ans, temp, tp, tv; n bool m11, m22, first, b[100005], move; n char a[100005]; n int dist() { n   if (m - s < e - m) n     return e - m; n   else n     return m - s; n } n int main() { n   scanf( %d %d , &n, &k); n   scanf( %s , a); n   for (e = 0; e < n; e++) { n     if (a[e] ==  0 ) temp++; n     if (a[e] ==  0  && !first) s = e, first = true; n     if (temp >= k + 1) break; n   } n   m = s; n   ans = dist(); n   while (e < n) { n     tv = dist(); n     tp = m; n     move = false; n     if (ans > dist()) { n       ans = dist(); n     } n     while (m <= e) { n       m++; n       if (a[m] ==  0 ) { n         if (tv > dist()) { n           tv = dist(); n           tp = m; n           if (ans > dist()) { n             ans = dist(); n           } n         } else { n           m = tp; n           break; n         } n       } n     } n     while (e < n) { n       e++; n       if (e < n && a[e] ==  0 ) { n         while (true) { n           s++; n           if (a[s] ==  0 ) { n             if (m < s) { n               m = s; n             } n             move = true; n             break; n           } n         } n       } n       if (move) break; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> temp[102][102]; n int ans[102][102]; n int main() { n   int n, m, q; n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       temp[i][j].first = i; n       temp[i][j].second = j; n     } n   } n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int r; n       cin >> r; n       pair<int, int> p; n       p = temp[r][1]; n       for (int j = 1; j < m; j++) { n         temp[r][j] = temp[r][j + 1]; n       } n       temp[r][m] = p; n     } else if (t == 2) { n       int c; n       cin >> c; n       pair<int, int> p; n       p = temp[1][c]; n       for (int j = 1; j < n; j++) { n         temp[j][c] = temp[j + 1][c]; n       } n       temp[n][c] = p; n     } else { n       int r, c, x; n       cin >> r >> c >> x; n       int indx1 = temp[r][c].first; n       int indx2 = temp[r][c].second; n       ans[indx1][indx2] = x; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cout << ans[i][j] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) n using namespace std; n long long powmod(long long a, long long b, long long mod) { n   if (b == 0 || a == 1) { n     if (mod == 1) n       return 0; n     else n       return 1; n   } n   if (b % 2 == 0) { n     long long k = powmod(a, b / 2, mod); n     return (k * k) % mod; n   } else { n     long long k = powmod(a, b / 2, mod); n     return ((k * k) % mod * a) % mod; n   } n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a > b) n     return gcd(a % b, b); n   else n     return gcd(b % a, a); n } n long long prime(long long p) { n   for (long long i = 2; i * i <= p; i++) { n     if (p % i == 0 && i < p) return i; n   } n   return 1; n } n long long inv(long long a, long long mod) { return powmod(a, mod - 2, mod); } n void solve(long long ppppppppp = 1) { n   long long a, b; n   cin >> a >> b; n   if (a + b == 2) { n     cout << 0; n     return; n   } n   if (a == 1) { n     for (long long i = 0; i < b; i++) cout << i + 2 <<    ; n     return; n   } n   if (b == 1) { n     for (long long i = 0; i < a; i++) cout << i + 2 <<    ; n     return; n   } n   for (long long i = 0; i < a; i++) { n     for (long long j = 0; j < b; j++) cout << (i + 1) * (a + j + 1) <<    ; n     cout <<   n ; n   } n   return; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tututu; n   tututu = 1; n   for (long long qwerty = 0; qwerty < tututu; qwerty++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n pair<pair<int, int>, int> queries[N]; n long long arr[N], acc[N], in[N]; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %d , &x); n     in[i] = x; n   } n   for (int i = 0; i < m; ++i) { n     scanf( %d %d %d , &queries[i].first.first, &queries[i].first.second, n           &queries[i].second); n     --queries[i].first.first; n     --queries[i].first.second; n   } n   for (int i = 0; i < k; ++i) { n     int a, b; n     scanf( %d %d , &a, &b); n     --a; n     --b; n     acc[a] += 1; n     acc[b + 1] -= 1; n   } n   for (int i = 1; i < m; ++i) { n     acc[i] += acc[i - 1]; n   } n   for (int i = 0; i < m; ++i) { n     int a = queries[i].first.first; n     int b = queries[i].first.second; n     long long x = queries[i].second * acc[i]; n     arr[a] += x; n     arr[b + 1] -= x; n   } n   for (int i = 1; i < n; ++i) { n     arr[i] += arr[i - 1]; n   } n   for (int i = 0; i < n - 1; ++i) { n     cout << arr[i] + in[i] <<    ; n   } n   cout << arr[n - 1] + in[n - 1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n long long R = 1, n, q, arr[N]; n vector<long long> a[N]; n long long h[N], p[N][20]; n long long cnt = 0, st[N], en[N], pos[2 * N]; n long long it[8 * N], lz[8 * N]; n void dfs(long long u) { n   st[u] = ++cnt; n   pos[cnt] = arr[u]; n   for (long long i = 1; i < 17; ++i) p[u][i] = p[p[u][i - 1]][i - 1]; n   for (long long i = 0; i < a[u].size(); ++i) { n     long long v = a[u][i]; n     if (v == p[u][0]) continue; n     h[v] = h[u] + 1; n     p[v][0] = u; n     dfs(v); n   } n   en[u] = ++cnt; n   pos[cnt] = arr[u]; n } n long long par(long long u, long long d) { n   for (long long i = 0; i < 17; ++i) n     if ((d >> i & 1)) u = p[u][i]; n   return u; n } n long long lca(long long u, long long v) { n   if (h[u] != h[v]) { n     if (h[u] < h[v]) swap(u, v); n     u = par(u, h[u] - h[v]); n   } n   if (u == v) return u; n   for (long long i = 16; i >= 0; --i) n     if (p[u][i] != p[v][i]) { n       u = p[u][i]; n       v = p[v][i]; n     } n   return p[u][0]; n } n void build_IT(long long i, long long l, long long r) { n   if (l == r) { n     it[i] = pos[l]; n     return; n   } n   long long m = (l + r) / 2; n   build_IT(i * 2, l, m); n   build_IT(i * 2 + 1, m + 1, r); n   it[i] = it[i * 2] + it[i * 2 + 1]; n } n void true_val(long long i, long long l, long long r) { n   if (lz[i] == 0) return; n   it[i] += (r - l + 1) * lz[i]; n   if (l != r) { n     lz[i * 2] += lz[i]; n     lz[i * 2 + 1] += lz[i]; n   } n   lz[i] = 0; n } n void update(long long i, long long l, long long r, long long a, long long b, n             long long val) { n   true_val(i, l, r); n   if (l > b || r < a || l > r) return; n   if (a <= l && r <= b) { n     it[i] += (r - l + 1) * val; n     if (l != r) { n       lz[i * 2] += val; n       lz[i * 2 + 1] += val; n     } n     return; n   } n   long long m = (l + r) / 2; n   update(i * 2, l, m, a, b, val); n   update(i * 2 + 1, m + 1, r, a, b, val); n   it[i] = it[i * 2] + it[i * 2 + 1]; n } n long long get(long long i, long long l, long long r, long long a, long long b) { n   true_val(i, l, r); n   if (l > b || r < a || l > r) return 0; n   if (a <= l && r <= b) return it[i]; n   long long m = (l + r) / 2; n   return get(i * 2, l, m, a, b) + get(i * 2 + 1, m + 1, r, a, b); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   scanf( %lld%lld , &n, &q); n   for (long long i = 1; i <= n; ++i) scanf( %lld , &arr[i]); n   for (long long i = 1; i < n; ++i) { n     long long x, y; n     scanf( %lld%lld , &x, &y); n     a[x].push_back(y); n     a[y].push_back(x); n   } n   dfs(1); n   n *= 2; n   build_IT(1, 1, n); n   while (q--) { n     long long t; n     scanf( %lld , &t); n     if (t == 1) { n       scanf( %lld , &R); n       continue; n     } n     if (t == 3) { n       long long u, ans = 0; n       scanf( %lld , &u); n       if (lca(R, u) != u) n         ans = get(1, 1, n, st[u], en[u]); n       else if (R == u) n         ans = it[1]; n       else { n         long long z = par(R, h[R] - h[u] - 1); n         ans = it[1] - get(1, 1, n, st[z], en[z]); n       } n       printf( %lld n , ans / 2); n       continue; n     } n     long long u, v, val; n     scanf( %lld%lld%lld , &u, &v, &val); n     long long x = lca(u, v); n     if (lca(x, R) != x) n       update(1, 1, n, st[x], en[x], val); n     else { n       long long a = lca(u, R), b = lca(v, R); n       long long w = (a == x ? b : a); n       update(1, 1, n, 1, n, val); n       if (w != R) { n         long long z = par(R, h[R] - h[w] - 1); n         update(1, 1, n, st[z], en[z], -val); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dig[15]; n int qpow(int digit) { n   int a = 2, b = digit, ans = 1; n   while (b) { n     if (b & 1) ans *= a; n     a *= a; n     b >>= 1; n   } n   return ans; n } n int getDigit(int x) { n   int ans = 0; n   while (x) { n     x /= 10; n     ans++; n   } n   return ans; n } n void getHead(int x, int len) { n   int index = len; n   while (x) { n     dig[index--] = x % 10; n     x /= 10; n   } n } n int main() { n   int n; n   while (~scanf( %d , &n)) { n     int len = getDigit(n); n     int ans = 0; n     for (int i = 1; i <= len - 1; i++) ans += qpow(i); n     if (ans == 1) ans -= 1; n     getHead(n, len); n     for (int i = 1; i <= len; i++) { n       if (dig[i] > 7) { n         ans += qpow(len - i + 1); n         break; n       } else if (dig[i] == 7) { n         ans += qpow(len - i); n         if (i == len) ans += 1; n       } else if (dig[i] > 4) { n         ans += qpow(len - i); n         break; n       } else if (dig[i] == 4) { n         if (i == len) ans += 1; n         continue; n       } else n         break; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native ) n using namespace std; n int n, m, k, ct[200010], cnt; n vector<pair<int, int> > e[200010]; n vector<int> mp[200010]; n char bf[100000], *p1 = bf, *p2 = bf; n inline int read() { n   int x = 0; n   char ch = n       (p1 == p2 && (p2 = (p1 = bf) + fread(bf, 1, 100000, stdin), p2 == p1) n            ? -1 n            : *p1++); n   for (; ch <  0  || ch >  9 ; n        ch = n            (p1 == p2 && (p2 = (p1 = bf) + fread(bf, 1, 100000, stdin), p2 == p1) n                 ? -1 n                 : *p1++)) n     ; n   for (; ch <=  9  && ch >=  0 ; n        x = x * 10 + ch - 48, n        ch = n            (p1 == p2 && (p2 = (p1 = bf) + fread(bf, 1, 100000, stdin), p2 == p1) n                 ? -1 n                 : *p1++)) n     ; n   return x; n } n int ans, O = 1; n int hah, val[10][200010]; n void dfs(int x, int f) { n   if (x == k + 1) { n     if (f == hah) ++ans; n     return; n   } n   if (mp[x].size() == 0) { n     dfs(x + 1, f); n     return; n   } n   for (auto i = (1); i <= (x); ++i) { n     int flag = 1; n     dfs(x + 1, f + val[x][i]); n   } n } n int hsh[200010]; n inline void solve() { n   n = read(), m = read(), k = read(); n   for (auto i = (1); i <= (n); ++i) hsh[i] = rand(), hah += hsh[i]; n   for (auto i = (1); i <= (m); ++i) { n     int u = read(), v = read(), w = read(); n     e[u].push_back({w, v}); n   } n   for (auto i = (1); i <= (n); ++i) sort(e[i].begin(), e[i].end()); n   for (auto i = (1); i <= (n); ++i) mp[e[i].size()].push_back(i); n   for (auto i = (1); i <= (k); ++i) n     for (auto j = (1); j <= (i); ++j) { n       val[i][j] = 0; n       for (auto v : mp[i]) val[i][j] += hsh[e[v][j - 1].second]; n     } n   for (auto i = (1); i <= (k); ++i) n     if (mp[i].size() == 0) O *= i; n   dfs(1, 0); n   printf( %d n , ans * O); n } n int main() { n   int T = 1; n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 205; n template <typename T> n inline void read(T &AKNOI) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   AKNOI = flag * x; n } n namespace ModCalculator { n long long MOD; n inline void Inc(long long &x, long long y) { n   x += y; n   if (x >= MOD) x -= MOD; n } n inline void Dec(long long &x, long long y) { n   x -= y; n   if (x < 0) x += MOD; n } n inline long long Add(long long x, long long y) { n   Inc(x, y); n   return x; n } n inline long long Sub(long long x, long long y) { n   Dec(x, y); n   return x; n } n inline long long Mul(long long x, long long y) { return x * y % MOD; } n }  // namespace ModCalculator n using namespace ModCalculator; n int n, d; n long long C[N][N], dp[N][N], sum[N], ans; n void init() { n   read(n); n   read(d); n   read(MOD); n   for (int i = 0; i <= n; ++i) { n     C[i][0] = 1; n     for (int j = 1; j <= i; ++j) { n       C[i][j] = Add(C[i - 1][j], C[i - 1][j - 1]); n     } n   } n } n void solve() { n   dp[1][0] = sum[1] = 1; n   for (int i = 2; i <= n; ++i) { n     for (int j = 1; j <= d; ++j) { n       for (int k = 1; k < i; ++k) { n         Inc(dp[i][j], Mul(C[i - 2][k - 1], Mul(sum[k], dp[i - k][j - 1]))); n       } n       if (j < d) Inc(sum[i], dp[i][j]); n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= d; ++j) { n       for (int k = 0; k + j <= d; ++k) { n         if (k == 1) continue; n         Inc(ans, Mul(dp[i][j], dp[n - i + 1][k])); n       } n     } n   } n   printf( %lld n , Mul(ans, 2LL * n * (n - 1))); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx ) n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long int A, B, C, D, E, H, W, M, K, L, R, N, X, Y, Z, num = 0, sum = 0, n                                                              flag = 0; n   string S, T; n   cin >> Z; n   while (Z--) { n     cin >> N >> M; n     vector<long long int> P(N, 0); n     vector<long long int> Q(M, 0); n     for (long long int i = 0; i < N; i++) cin >> P[i]; n     for (long long int i = 0; i < M; i++) cin >> Q[i]; n     sort(P.begin(), P.end()); n     sort(Q.begin(), Q.end()); n     num = -1; n     for (long long int i = 0; i < N; i++) n       for (long long int j = 0; j < M; j++) n         if (P[i] == Q[j]) num = P[i]; n     if (num == -1) n       cout <<  NO  << endl; n     else { n       cout <<  YES  << endl; n       cout << 1 <<     << num << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n; n   while (cin >> n) { n     long flag = 0, count = 0, a[100005] = {0}, i; n     for (i = 0; i < n; i++) cin >> a[i]; n     for (i = n - 1; i > 0; i--) { n       if (a[i - 1] > a[i]) n         break; n       else n         count++; n     } n     cout << n - count - 1 << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t, x, n; n   cin >> t; n   while (t--) { n     cin >> n; n     vector<int> v(109, 0); n     while (n--) { n       cin >> x; n       v[x] += 1; n     } n     int sum = 0; n     for (int i = 0; i < v.size(); i++) { n       if (v[i] == 0) { n         sum += i; n         break; n       } n     } n     for (int i = 0; i < v.size(); i++) { n       if (v[i] <= 1) { n         sum += i; n         break; n       } n     } n     cout << sum <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 6010; n const int mod = 1000000007; n int n; n long long k[MAXN], k2[MAXN]; n int main() { n   string asd; n   string asd2; n   cin >> asd; n   cin >> asd2; n   n = ((int)(asd2.size())); n   long long res = 0; n   for (int i = 0; i < ((int)(asd.size())); i++) { n     for (int j = 0; j < ((int)(asd2.size())); j++) { n       if (asd2[j] == asd[i]) { n         k2[j] = (k2[j] + k[j - 1] + 1) % mod; n       } n       k2[j + 1] = (k2[j + 1] + k2[j]) % mod; n     } n     res = (res + k2[n]) % mod; n     for (int j = 0; j < ((int)(asd2.size())); j++) k[j] = k2[j]; n     memset(k2, 0, sizeof k2); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010; n int Case, n, a[N], la[N], fn[N]; n int f[N][N]; n int main() { n   scanf( %d , &Case); n   while (Case--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) fn[i] = 0; n     for (int i = 1; i <= n; ++i) { n       scanf( %d , a + i); n       if (a[i] == a[i - 1]) { n         --i; n         --n; n       } else { n         la[i] = fn[a[i]]; n         fn[a[i]] = i; n       } n     } n     for (int i = n; i; --i) { n       for (int j = i + 1; j <= n; ++j) { n         f[i][j] = f[i][j - 1]; n         for (int k = la[j]; k >= i; k = la[k]) { n           f[i][j] = max(f[i][j], f[i][k] + f[k][j - 1] + 1); n         } n       } n     } n     printf( %d n , n - 1 - f[1][n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int tc; n   cin >> tc; n   while (tc--) { n     int n; n     cin >> n >> ws; n     string str; n     cin >> str; n     int flag = 0, i; n     for (i = 0; i < str.size() - 1; i++) { n       if (str[i] > str[i + 1]) { n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       cout << str << endl; n       continue; n     } n     string clean_str; n     for (i = 0; i < str.size(); i++) { n       if (str[i] ==  1 ) break; n       clean_str.push_back( 0 ); n     } n     clean_str.push_back( 0 ); n     for (i = str.size() - 1; i >= 0; i--) { n       if (str[i] ==  0 ) break; n       clean_str.push_back( 1 ); n     } n     cout << clean_str << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 4e5 + 5; n int ans[nax]; n int b[nax]; n int n; n bool solve(int bb) { n   vector<int> a; n   int val = (1 << (bb)); n   int val2 = val * 2; n   int val3 = val * 3; n   int val4 = val * 4; n   for (int i = 1; i <= n; i++) { n     a.push_back(b[i] & ((1 << (bb + 1)) - 1)); n   } n   sort(a.begin(), a.end()); n   int hi2 = n - 1; n   int hi1 = n - 1; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     hi1 = min(hi1, n - 1); n     while (hi1 >= i && a[i] + a[hi1] >= val) hi1--; n     if (a[i] + a[hi1] < val) hi1++; n     hi1 = max(hi1, i + 1); n     while (hi2 >= 0 && a[i] + a[hi2] >= val2) hi2--; n     ans += max(0, hi2 - hi1 + 1); n   } n   hi2 = n - 1; n   hi1 = n - 1; n   for (int i = 0; i < n; i++) { n     hi1 = min(hi1, n - 1); n     while (hi1 >= i && a[i] + a[hi1] >= val3) hi1--; n     if (a[i] + a[hi1] < val3) hi1++; n     hi1 = max(hi1, i + 1); n     while (hi2 >= 0 && a[i] + a[hi2] >= val4) hi2--; n     ans += max(0, hi2 - hi1 + 1); n   } n   return ans % 2; n } n void brut() { n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) ans ^= (b[i] + b[j]); n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   int ans = 0; n   for (int i = 0; i <= 27; i++) { n     if (solve(i)) ans += (1 << i); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   long long a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long ind = 0, xtr = 0; n   sort(a, a + n); n   while (ind < n) { n     for (long long i = ind; i < n; i++) { n       if (a[i] > 2 * k) { n         ind = i; n         break; n       } else if (i == n - 1 and a[i] <= 2 * k) { n         ind = n; n         break; n       } n       k = max(k, a[i]); n     } n     if (ind != n) { n       if (ind > 0) k = max(k, a[ind - 1]); n       k = 2 * k; n       xtr++; n     } n   } n   cout << xtr << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long long MAXN = 1123456; n const long long N = 2e6; n const long long inf = 3e18; n const long long mod = 998244353; n const long long MOD = mod * (mod - 2); n mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count()); n template <typename T> n void vout(T s) { n   cout << s << endl; n   exit(0); n } n void main_solve() { n   long long n; n   cin >> n; n   vector<long long> a(n + 1); n   for (int i = 1; i <= n; i++) cin >> a[i]; n   vector<bool> f(n + 1); n   vector<long long> c, ans; n   long long s = 1; n   while (1) { n     c.push_back(s); n     if (f[s]) break; n     f[s] = 1; n     s = s - a[s]; n   } n   reverse(c.begin(), c.end()); n   ans.push_back(c[0]); n   for (int i = 1; i < (int)c.size(); i++) n     if (c[i] != c[0]) { n       ans.push_back(c[i]); n     } else n       break; n   cout << (int)ans.size() <<   n ; n   for (auto i : ans) cout << i <<    ; n   cout <<   n ; n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     main_solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char field[8][8]; n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       cin >> field[i][j]; n     } n   } n   int minDist = 10; n   for (int j = 0; j < 8; j++) { n     for (int i = 0; i < 8; i++) { n       if (field[i][j] ==  B ) { n         break; n       } n       if (field[i][j] ==  W ) { n         if (minDist > i) minDist = i; n       } n     } n   } n   int blackMinDist = -1; n   for (int j = 0; j < 8; j++) { n     for (int i = 7; i >= 0; i--) { n       if (field[i][j] ==  W ) { n         break; n       } n       if (field[i][j] ==  B ) { n         if (blackMinDist < i) blackMinDist = i; n       } n     } n   } n   blackMinDist = 7 - blackMinDist; n   if (blackMinDist >= minDist) { n     cout <<  A ; n     return 0; n   } n   cout <<  B ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 1e15; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   int n, k; n   cin >> n >> k; n   vector<int> first(n); n   for (int i = 0; i < n; i++) cin >> first[i]; n   vector<long long> pr(n + 1); n   for (int i = 0; i < n; i++) pr[i + 1] = pr[i] + first[i]; n   map<long long, int> how; n   for (int i = 0; i < n; i++) { n     how[pr[i + 1]]++; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (k == 1) { n       ans += how[k + pr[i]]; n     } else { n       if (k == -1) { n         ans += how[k + pr[i]]; n         ans += how[-k + pr[i]]; n       } else { n         for (long long p = 1; p <= MAX; p *= k) { n           ans += how[p + pr[i]]; n         } n       } n     } n     how[pr[i + 1]]--; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n vector<pair<int, bool> > adj[maxn]; n vector<pair<pair<int, int>, bool> > edge; n int dp[maxn], dis[maxn], par[maxn]; n bool mark[maxn], in_path[maxn]; n int n, m, ans = 0; n void input() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b, w; n     cin >> a >> b >> w; n     adj[a - 1].push_back(make_pair(b - 1, 1 - w)); n     adj[b - 1].push_back(make_pair(a - 1, 1 - w)); n     edge.push_back(make_pair(make_pair(a - 1, b - 1), 1 - w)); n     if (w) ans++; n   } n } n void bfs() { n   queue<int> q; n   q.push(0); n   mark[0] = true; n   dis[0] = 0; n   dp[0] = 0; n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (auto u : adj[v]) { n       if (!mark[u.first]) { n         q.push(u.first); n         mark[u.first] = true; n         dis[u.first] = dis[v] + 1; n         par[u.first] = v; n         dp[u.first] = dp[v] + u.second; n       } else if ((dp[u.first] > dp[v] + u.second) && n                  (dis[u.first] == dis[v] + 1)) { n         par[u.first] = v; n         dp[u.first] = dp[v] + u.second; n       } n     } n   } n } n void check() { n   for (int i = 0; i < n; i++) cout <<  par   << par[i] << endl; n } n void pp(int v) { n   if (par[v] == v) return; n   in_path[v] = true; n   pp(par[v]); n } n void output() { n   cout << ans - dis[n - 1] + 2 * dp[n - 1] << endl; n   in_path[0] = true; n   in_path[n - 1] = true; n   pp(n - 1); n   for (int i = 0; i < m; i++) { n     int a = edge[i].first.first, b = edge[i].first.second, w = edge[i].second; n     if (!in_path[a] || !in_path[b]) n       if (!w) cout << a + 1 <<     << b + 1 <<     << w << endl; n     if (in_path[a] && in_path[b]) n       if (w) cout << a + 1 <<     << b + 1 <<     << w << endl; n   } n   return; n } n int main() { n   input(); n   bfs(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   long long f = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) f *= -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   x *= f; n } n template <typename T, typename... Args> n void read(T &first, Args &...args) { n   read(first); n   read(args...); n } n template <typename T> n void write(T arg) { n   T x = arg; n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) { n     write(x / 10); n   } n   putchar(x % 10 +  0 ); n } n template <typename T, typename... Ts> n void write(T arg, Ts... args) { n   write(arg); n   if (sizeof...(args) != 0) { n     putchar(   ); n     write(args...); n   } n } n const long long MOD = 998244353; n const int MAXN = 5e5 + 50; n long long n, k, fac[MAXN], inv[MAXN]; n long long qpow(long long a, long long p) { n   long long sum = 1; n   while (p) { n     if (p & 1) { n       sum = sum * a % MOD; n     } n     a = a * a % MOD; n     p >>= 1; n   } n   return sum; n } n long long get_inv(long long k) { return qpow(k, MOD - 2); } n void pre() { n   fac[0] = 1; n   for (register int i = 1; i <= n; ++i) { n     fac[i] = fac[i - 1] * i % MOD; n     inv[i] = get_inv(fac[i]); n   } n } n long long C(long long n, long long m) { n   if (m > n) { n     return 0; n   } else { n     return fac[n] * get_inv(fac[n - m] * fac[m] % MOD) % MOD; n   } n } n int main() { n   read(n, k); n   pre(); n   long long ans = 0; n   for (register int i = 1; i <= n; ++i) { n     long long p = n / i - 1; n     ans = (ans + C(p, k - 1)) % MOD; n   } n   write(ans), putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int cnt; n int getdeal(long long c) { n   int ret = 0; n   while (c) { n     ret += c % 10; n     c /= 10; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   long long temp = 0; n   while (temp * 10 + 9 <= n) { n     temp = temp * 10 + 9; n   } n   cout << getdeal(temp) + getdeal(n - temp); n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 998244353; n const int M = 1e6 + 10; n const int N = 1e5 + 10; n inline long long read() { n   long long b = 1, sum = 0; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) b = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return b * sum; n } n int a[N], in[N], d[N]; n int n, m, k, T; n int vis[N]; n vector<int> e[N]; n vector<pair<int, int> > ans; n void dfs(int x, int _fa, int need) { n   int i, j; n   if (x == 1) { n     for (i = 0; i < e[x].size(); i++) { n       int y = e[x][i]; n       ans.push_back({y, i + 1}); n       dfs(y, x, i); n     } n     return; n   } n   int cha = need - in[x] + 1, sum = 0; n   if (in[x] == 1) { n     ans.push_back({x, need}); n   } else if (cha >= 0) { n     ans.push_back({x, cha}); n     for (i = 0; i < e[x].size(); i++) { n       int y = e[x][i]; n       if (y == _fa) continue; n       ans.push_back({y, cha + sum + 1}); n       dfs(y, x, cha + sum); n       sum++; n     } n   } else { n     int now = need + 1, sum = 0; n     for (i = 0; sum < -cha && i < e[x].size(); i++) { n       int y = e[x][i]; n       if (y == _fa) continue; n       ans.push_back({y, now + 1}); n       dfs(y, x, now); n       now++; n       sum++; n     } n     ans.push_back({x, 0}); n     sum = 0; n     for (; i < e[x].size(); i++) { n       int y = e[x][i]; n       if (y == _fa) continue; n       ans.push_back({y, sum + 1}); n       dfs(y, x, sum); n       sum++; n     } n   } n   ans.push_back({_fa, need + 1}); n } n int main() { n   int i, j; n   n = read(); n   for (i = 1; i <= n - 1; i++) { n     int x = read(), y = read(); n     e[x].push_back(y); n     e[y].push_back(x); n     in[x]++; n     in[y]++; n   } n   ans.push_back({1, 0}); n   dfs(1, 0, 0); n   printf( %d n , ans.size()); n   for (i = 0; i < ans.size(); i++) { n     printf( %d %d n , ans[i].first, ans[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 3e5 + 100; n int t, T, n, k; n int A[maxn]; n int B[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin >> T; n   while (T--) { n     cin >> n; n     int sum = 0; n     for (int i = 1; i <= n; i++) { n       cin >> A[i]; n       sum += A[i]; n       B[i] = A[i]; n     } n     if (sum % n) { n       cout << -1 << endl; n       continue; n     } n     sum /= n; n     int cnt = 0; n     for (int i = 2; i <= n; i++) { n       int d = (A[i] + i - 1) / i * i - A[i]; n       if (d <= A[1]) { n         A[i] += d; n         A[1] -= d; n         cnt++; n       } n       int x = A[i] / i * i; n       A[1] += x; n       A[i] -= x; n     } n     int ok = 1; n     for (int i = n; i > 1; i--) { n       if (A[i] > sum) ok = 0; n       A[1] -= (sum - A[i]); n       if (A[1] < 0) ok = 0; n       if (!ok) break; n     } n     if (!ok) { n       cout << -1 << endl; n       continue; n     } n     if (ok) { n       cout << 2 * (n - 1) + cnt << endl; n       for (int i = 2; i <= n; i++) { n         int d = (B[i] + i - 1) / i * i - B[i]; n         if (d <= B[1]) { n           B[i] += d; n           B[1] -= d; n           cout << 1 <<     << i <<     << d << endl; n         } n         int x = B[i] / i * i; n         B[1] += x; n         B[i] -= x; n         cout << i <<     << 1 <<     << x / i << endl; n       } n       for (int i = n; i > 1; i--) { n         int x = sum - B[i]; n         B[1] -= x; n         B[i] += x; n         cout << 1 <<     << i <<     << x << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, v, k1 = 0, k2 = 0; n   int how_many1 = 0; n   vector<int> molt[99 + 1]; n   scanf( %d , &n); n   short b[n << 1]; n   for (int i = 0; i < (n << 1); i++) { n     scanf( %d , &v); n     molt[v].push_back(i); n   } n   for (int v = 10; v <= 99; v++) n     if (molt[v].size() >= 2) { n       k1++; n       k2++; n       how_many1++; n       b[molt[v][0]] = 1; n       b[molt[v][1]] = 2; n     } n   for (int v = 10; v <= 99; v++) n     if (molt[v].size() == 1) { n       if (k1 <= k2) { n         k1++; n         how_many1++; n         b[molt[v][0]] = 1; n       } else { n         k2++; n         b[molt[v][0]] = 2; n       } n     } n   for (int v = 10; v <= 99; v++) { n     for (int i = 2; i < molt[v].size(); i++) { n       if (how_many1 < n) { n         b[molt[v][i]] = 1; n         how_many1++; n       } else { n         b[molt[v][i]] = 2; n       } n     } n   } n   printf( %d n , k1 * k2); n   for (int i = 0; i < 2 * n; i++) printf( %d  , b[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[105][3]; n vector<int> vec[105]; n bool vis[105]; n void dfs(int i) { n   vis[i] = true; n   for (int j = 0; j < vec[i].size(); j++) { n     if (!vis[vec[i][j]]) dfs(vec[i][j]); n   } n   return; n } n int main() { n   int n, i, cnt = 0, j; n   cin >> n; n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &arr[i][0], &arr[i][1]); n   } n   for (i = 1; i <= n; i++) { n     for (j = i + 1; j <= n; j++) { n       if (i != j) { n         if (arr[i][0] == arr[j][0] || arr[i][1] == arr[j][1]) { n           vec[i].push_back(j); n           vec[j].push_back(i); n         } n       } n     } n   } n   for (i = 1; i <= n; i++) { n     if (!vis[i]) { n       cnt++; n       dfs(i); n     } n   } n   cout << cnt - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int kMaxN = 1 + 50; n char A[kMaxN][kMaxN], B[kMaxN][kMaxN]; n int N, M; n int type; n void ReadInput() { n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= N; i++) { n     scanf( %s , A[i] + 1); n   } n   for (int i = 1; i <= N; i++) { n     scanf( %s , B[i] + 1); n   } n   if (N % 2 == 0) { n     type = 0; n   } else { n     type = 1; n   } n } n bool Perfect(char mat[kMaxN][kMaxN]) { n   if (type == 0) { n     for (int i = 1; i <= N; i++) { n       for (int j = 1; j <= M; j++) { n         if (i % 2 == 1 && mat[i][j] !=  U ) { n           return false; n         } n         if (i % 2 == 0 && mat[i][j] !=  D ) { n           return false; n         } n       } n     } n     return true; n   } else { n     for (int i = 1; i <= N; i++) { n       for (int j = 1; j <= M; j++) { n         if (j % 2 == 1 && mat[i][j] !=  L ) { n           return false; n         } n         if (j % 2 == 0 && mat[i][j] !=  R ) { n           return false; n         } n       } n     } n     return true; n   } n } n vector<pair<int, int> > Normalize(char mat[kMaxN][kMaxN]) { n   vector<pair<int, int> > moves; n   bool now = 0; n   while (!Perfect(mat)) { n     if (now == 0) { n       for (int i = 1; i < N; i++) { n         for (int j = 1; j < M; j++) { n           if (mat[i][j] ==  U  && mat[i][j + 1] ==  U  && n               mat[i + 1][j] ==  D  && mat[i + 1][j + 1] ==  D ) { n             mat[i][j] = mat[i + 1][j] =  L ; n             mat[i][j + 1] = mat[i + 1][j + 1] =  R ; n             moves.push_back(make_pair(i, j)); n           } n         } n       } n     } else { n       for (int i = 1; i < N; i++) { n         for (int j = 1; j < M; j++) { n           if (mat[i][j] ==  L  && mat[i + 1][j] ==  L  && n               mat[i][j + 1] ==  R  && mat[i + 1][j + 1] ==  R ) { n             mat[i][j] = mat[i][j + 1] =  U ; n             mat[i + 1][j] = mat[i + 1][j + 1] =  D ; n             moves.push_back(make_pair(i, j)); n           } n         } n       } n     } n     now ^= 1; n   } n   return moves; n } n int main() { n   ReadInput(); n   vector<pair<int, int> > first = Normalize(A); n   vector<pair<int, int> > second = Normalize(B); n   reverse(second.begin(), second.end()); n   printf( %d n , (int)first.size() + (int)second.size()); n   for (auto p : first) { n     printf( %d %d n , p.first, p.second); n   } n   for (auto p : second) { n     printf( %d %d n , p.first, p.second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int MX = 1e5 + 6; n long long l, r; n int main() { n   cin >> l >> r; n   puts( YES ); n   for (long long i = l; i <= r; i += 2) cout << i <<     << i + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m, a, b; n     cin >> n >> m >> a >> b; n     if (n * a != m * b) { n       cout <<  NO  << endl; n       continue; n     } n     int index = 0; n     cout <<  YES  << endl; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         cout << ((index + j) % m < a ? 1 : 0); n       } n       index -= a; n       if (index < 0) index += m; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct dot { n   string f, s; n }; n bool cmp(dot x, dot y) {} n int main() { n   int i, j, k, n; n   int p[100010]; n   dot A[100010]; n   cin >> n; n   for (int(i) = int(1); i <= int(n); i++) { n     cin >> A[i].f >> A[i].s; n   } n   string las =   ; n   for (int(i) = int(1); i <= int(n); i++) { n     cin >> p[i]; n   } n   bool OK = true; n   las = min(A[p[1]].f, A[p[1]].s); n   for (int(i) = int(2); i <= int(n); i++) { n     if (min(A[p[i]].f, A[p[i]].s) > las) n       las = min(A[p[i]].f, A[p[i]].s); n     else if (max(A[p[i]].f, A[p[i]].s) > las) { n       las = max(A[p[i]].f, A[p[i]].s); n     } else { n       OK = false; n       break; n     } n   } n   if (OK) n     cout <<  YES n ; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long num[30]; n int main() { n   int n, m; n   long long k; n   string temp; n   scanf( %d%I64d , &n, &k); n   long long cnt = 0; n   cin >> temp; n   for (int i = 0; i < n; i++) { n     num[temp[i] -  A  + 1]++; n   } n   for (int i = 0; i < 26 && k != 0; i++) { n     m = 1; n     for (int j = 2; j <= 26; j++) { n       if (num[m] < num[j]) m = j; n     } n     if (num[m] <= k) { n       cnt += num[m] * num[m]; n       k -= num[m]; n       num[m] = 0; n     } else { n       cnt += k * k; n       break; n     } n   } n   printf( %I64d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int c[1000010]; n int mp[1000]; n int in[1000010]; n int fir[1000010]; n int ans[1000010]; n vector<int> v[1000010]; n int calc(int nod) { n   if (c[nod] == 5) return fir[nod] = in[nod]; n   if (c[nod] == 4) return fir[nod] = 1 - calc(v[nod][0]); n   int q1 = calc(v[nod][0]); n   int q2 = calc(v[nod][1]); n   if (c[nod] == 3) return fir[nod] = q1 ^ q2; n   if (c[nod] == 2) return fir[nod] = q1 | q2; n   if (c[nod] == 1) return fir[nod] = q1 & q2; n } n void dfs(int nod, int ch) { n   if (c[nod] == 5) { n     ans[nod] = ch; n     return; n   } n   if (c[nod] == 4) { n     dfs(v[nod][0], ch); n     return; n   } n   if (ch == 0) { n     dfs(v[nod][0], 0); n     dfs(v[nod][1], 0); n     return; n   } n   int q1 = fir[v[nod][0]]; n   int q2 = fir[v[nod][1]]; n   if (c[nod] == 3) { n     if ((q1 ^ (!q2)) == fir[nod]) { n       dfs(v[nod][1], 0); n     } else { n       dfs(v[nod][1], 1); n     } n     if ((q2 ^ (!q1)) == fir[nod]) { n       dfs(v[nod][0], 0); n     } else { n       dfs(v[nod][0], 1); n     } n   } n   if (c[nod] == 2) { n     if ((q1 | (!q2)) == fir[nod]) { n       dfs(v[nod][1], 0); n     } else { n       dfs(v[nod][1], 1); n     } n     if ((q2 | (!q1)) == fir[nod]) { n       dfs(v[nod][0], 0); n     } else { n       dfs(v[nod][0], 1); n     } n   } n   if (c[nod] == 1) { n     if ((q1 & (!q2)) == fir[nod]) { n       dfs(v[nod][1], 0); n     } else { n       dfs(v[nod][1], 1); n     } n     if ((q2 & (!q1)) == fir[nod]) { n       dfs(v[nod][0], 0); n     } else { n       dfs(v[nod][0], 1); n     } n   } n } n int main() { n   memset(ans, -1, sizeof ans); n   mp[ A ] = 1; n   mp[ O ] = 2; n   mp[ X ] = 3; n   mp[ N ] = 4; n   mp[ I ] = 5; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     string s; n     cin >> s; n     if (s ==  IN ) { n       int a; n       cin >> a; n       c[i] = 5; n       in[i] = a; n     } else if (s ==  NOT ) { n       int a; n       cin >> a; n       c[i] = mp[ N ]; n       v[i].push_back(a); n     } else { n       int a, b; n       cin >> a >> b; n       v[i].push_back(a); n       v[i].push_back(b); n       c[i] = mp[s[0]]; n     } n   } n   calc(1); n   dfs(1, 1); n   string s1 =   ; n   for (int i = 1; i <= n; i++) { n     if (ans[i] != -1) { n       if (ans[i]) n         ans[i] = 1 - fir[1]; n       else n         ans[i] = fir[1]; n       s1 += char( 0  + ans[i]); n     } n   } n   cout << s1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1010]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] <= k * 2) { n       k = max(k, a[i]); n       continue; n     } else { n       k = k * 2; n       ans++; n       i--; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[1000005], b[1000005], s[1000005]; n int k, T, tmp[27], vis[27], p, n, tt[27], vv[27]; n bool check(int x, int cas) { n   memcpy(tt, tmp, sizeof(tt)); n   memcpy(vv, vis, sizeof(vv)); n   if (cas == 1) { n     for (int i = x; i <= n; i++) { n       if (tt[s[i] -  a ] != -1) { n         if (tt[s[i] -  a ] > a[i]) return 1; n         if (tt[s[i] -  a ] < a[i]) return 0; n         continue; n       } n       for (int j = a[i] + 1; j <= k +  a  - 1; j++) n         if (!vv[j -  a ]) return 1; n       if (!vv[a[i] -  a ]) n         vv[a[i] -  a ] = 1, tt[s[i] -  a ] = a[i]; n       else n         return 0; n     } n     return 1; n   } else { n     for (int i = x; i <= n; i++) { n       if (tt[s[i] -  a ] != -1) { n         if (tt[s[i] -  a ] > b[i]) return 0; n         if (tt[s[i] -  a ] < b[i]) return 1; n         continue; n       } n       for (int j =  a ; j < b[i]; j++) n         if (!vv[j -  a ]) return 1; n       if (!vv[b[i] -  a ]) n         vv[b[i] -  a ] = 1, tt[s[i] -  a ] = b[i]; n       else n         return 0; n     } n     return 1; n   } n } n void solve() { n   scanf( %d%s%s%s , &k, s + 1, a + 1, b + 1); n   n = strlen(s + 1); n   memset(tmp, -1, sizeof(tmp)); n   p = n + 1; n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= n; i++) n     if (a[i] == b[i]) { n       if (tmp[s[i] -  a ] == a[i]) continue; n       if (tmp[s[i] -  a ] == -1 && !vis[a[i] -  a ]) n         tmp[s[i] -  a ] = a[i], vis[a[i] -  a ] = 1; n       else { n         puts( NO ); n         return; n       } n     } else { n       p = i; n       break; n     } n   int flag = 0; n   for (int i = p; i <= n; i++) { n     if (!flag && a[i] > b[i]) { n       puts( NO ); n       return; n     } n     if (tmp[s[i] -  a ] != -1) { n       if ((((flag & 2) == 0) && tmp[s[i] -  a ] < a[i]) || n           (((flag & 4) == 0) && tmp[s[i] -  a ] > b[i])) { n         puts( NO ); n         return; n       } n       if ((flag & 2) == 0 && tmp[s[i] -  a ] > a[i]) flag |= 2; n       if ((flag & 4) == 0 && tmp[s[i] -  a ] < b[i]) flag |= 4; n     } else if ((flag & 6) == 0) { n       bool used = 0; n       for (int j = a[i] + 1; j < b[i]; j++) n         if (!vis[j -  a ]) { n           tmp[s[i] -  a ] = j; n           vis[j -  a ] = 1; n           flag |= 6; n           used = 1; n           break; n         } n       if (!used) { n         if (!used && !vis[b[i] -  a ]) { n           vis[b[i] -  a ] = 1, tmp[s[i] -  a ] = b[i]; n           if (!check(i + 1, 2)) n             vis[b[i] -  a ] = 0, tmp[s[i] -  a ] = -1; n           else n             flag |= 2, used = 1; n         } n         if (!used && !vis[a[i] -  a ]) { n           vis[a[i] -  a ] = 1, tmp[s[i] -  a ] = a[i]; n           if (check(i + 1, 1)) flag |= 4, used = 1; n         } n         if (!used) { n           puts( NO ); n           return; n         } n       } n     } else if ((flag & 2) == 0) { n       bool used = 0; n       for (int j = a[i] + 1; j <= k +  a  - 1; j++) n         if (!vis[j -  a ]) { n           tmp[s[i] -  a ] = j; n           vis[j -  a ] = 1; n           flag |= 2; n           used = 1; n           break; n         } n       if (!used) { n         if (!vis[a[i] -  a ]) n           vis[a[i] -  a ] = 1, tmp[s[i] -  a ] = a[i]; n         else { n           puts( NO ); n           return; n         } n       } n     } else if ((flag & 4) == 0) { n       bool used = 0; n       for (int j =  a ; j < b[i]; j++) n         if (!vis[j -  a ]) { n           tmp[s[i] -  a ] = j; n           vis[j -  a ] = 1; n           flag |= 4; n           used = 1; n           break; n         } n       if (!used) { n         if (!vis[b[i] -  a ]) n           vis[b[i] -  a ] = 1, tmp[s[i] -  a ] = b[i]; n         else { n           puts( NO ); n           return; n         } n       } n     } n     flag |= 1; n     if (flag == 7) break; n   } n   puts( YES ); n   int now = 0; n   while (vis[now]) now++; n   for (int i = 0; i < k; i++) n     if (tmp[i] == -1) { n       tmp[i] =  a  + now; n       vis[now] = 1; n       while (vis[now]) now++; n     } n   for (int i = 0; i < k; i++) printf( %c , tmp[i]); n   puts(  ); n } n int main() { n   scanf( %d , &T); n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n using std::string; n int main() { n   int n, k; n   cin >> n >> k; n   if (n * (n - 1) / 2 > k) { n     int i = 0, j = 0; n     while (n--) { n       cout << i <<     << j++ << endl; n     } n   } else n     cout <<  no solution ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans = 1; n long long C(long long m, long long n) { n   long long p = 1; n   for (long long i = 1; i <= n; i++) p *= i; n   for (long long i = 1; i <= m; i++) p /= i; n   for (long long i = 1; i <= n - m; i++) p /= i; n   return p; n } n signed main() { n   cin >> n; n   for (long long i = 1; i <= n / 2 - 1; i++) ans *= i; n   ans *= ans; n   ans *= C(n / 2, n); n   ans /= 2; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T[100010], first[100010], canKill[100010], ans = 0; n char flag; n int X, M, N, fle = 0, A[100010]; n int main() { n   scanf( %d %d , &N, &M); n   memset(T, 0, sizeof(T)); n   memset(first, 0, sizeof(first)); n   memset(canKill, 0, sizeof(canKill)); n   ans = 0; n   for (int i = 1; i <= N; i++) { n     scanf( %d , &A[i]); n     if (A[i] > 0) n       T[A[i]]++; n     else { n       first[-1 * A[i]]++; n       fle++; n     } n   } n   for (int i = 1; i <= N; i++) { n     if (fle - first[i] + T[i] == M) { n       canKill[i] = 1; n       ans++; n     } n   } n   for (int i = 1; i <= N; i++) { n     if (A[i] > 0) { n       if (canKill[A[i]] == 1) { n         if (ans == 1) n           printf( Truth n ); n         else n           printf( Not defined n ); n       } else { n         printf( Lie n ); n       } n     } else { n       A[i] = A[i] * -1; n       if (canKill[A[i]] == 0) { n         printf( Truth n ); n       } else { n         if (ans == 1) n           printf( Lie n ); n         else n           printf( Not defined n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   vector<long long> v(2000000); n   for (long long i = 0; i < 2000000; i++) v[i] = 0; n   for (long long i = 0; i < n; i++) { n     long long t1; n     cin >> t1; n     v[t1]++; n   } n   long long t = 0; n   long long l = 1; n   long long r = 1000000; n   while (n > 0) { n     t++; n     l++; n     r--; n     if (v[l]) n--; n     if (v[r]) n--; n   } n   cout << t; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n = 0; n   cin >> n; n   string s; n   vector<vector<int> > v(5, vector<int>(0)); n   for (int i = 0; i < n; i++) { n     cin >> s; n     vector<int> cnt(5, 0); n     int sum = 0; n     for (int j = 0; j < s.size(); j++) { n       cnt[s[j] -  a ]++; n       sum++; n     } n     for (int j = 0; j < 5; j++) { n       v[j].push_back(cnt[j] - (sum - cnt[j])); n     } n   } n   long long ans = -1; n   for (int i = 0; i < 5; i++) { n     sort(v[i].rbegin(), v[i].rend()); n     long long sum = 0; n     long long cnt = 0; n     for (int j = 0; j < v[i].size(); j++) { n       sum += v[i][j]; n       if (sum > 0) { n         cnt++; n       } n     } n     ans = max(ans, cnt); n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 0; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   char x = getchar(); n   long long ans = 0, flag = 1; n   while (!isdigit(x)) n     if (x ==  - ) n       flag = -1, x = getchar(); n     else n       x = getchar(); n   while (isdigit(x)) ans = ans * 10 + x -  0 , x = getchar(); n   return ans * flag; n } n long long n, a[200005], ans, las; n map<long long, long long> mp; n signed main() { n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 1; i <= n; i++) a[i] += a[i - 1]; n   mp[0] = 0; n   las = 0; n   for (long long i = 1; i <= n; i++) { n     if (mp.find(a[i]) != mp.end()) { n       if (mp[a[i]] >= las) { n         ans -= (mp[a[i]] - las + 1) * ((n - i + 1)); n         las = mp[a[i]] + 1; n       } n     } n     mp[a[i]] = i; n   } n   cout << ans + n * (n + 1) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n ; n int n; n long long mod = 1000 * 1000 * 1000 + 7; n const int maxn = 1000005; n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long d[maxn]; n long long cnt[maxn]; n long long pow2[maxn]; n long long coef[maxn]; n long long bad[maxn]; n int main() { n   pow2[0] = 1; n   for (int i = 1; i < maxn; ++i) { n     pow2[i] = pow2[i - 1] * 2; n     pow2[i] %= mod; n   } n   cin >> n; n   vector<int> v; n   v.resize(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &v[i]); n     cnt[v[i]]++; n   } n   for (int i = 2; i < maxn; ++i) { n     for (int j = i; j < maxn; j += i) { n       d[i] += cnt[j]; n       if (i != j) bad[j] += i; n       bad[j] %= mod; n     } n   } n   long long ans = 0; n   for (int i = maxn - 1; i >= 2; --i) { n     if (d[i] == 0) continue; n     coef[i] = (1LL * ((d[i] * pow2[d[i] - 1]) % mod)) % mod; n     for (int j = i * 2; j < maxn; j += i) coef[i] -= coef[j]; n     ans += i * coef[i]; n     ans %= mod; n     ans += mod; n     ans %= mod; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n vector<int> ed[200001]; n int an[200001]; n int b[200001]; n set<int> o[2000001]; n void dfs(int x, int g, int p) { n   int i; n   if (x == 1) { n     an[x] = b[1]; n     o[x].insert(0); n   } else { n     o[x].insert(g); n     an[x] = g; n     if (o[p].size()) { n       set<int>::iterator f = o[p].begin(); n       for (i = 0; i < o[p].size(); i++) { n         int y = gcd(b[x], *f); n         an[x] = max(an[x], y); n         if (y != 1) o[x].insert(y); n         f++; n       } n     } n   } n   g = gcd(g, b[x]); n   for (i = 0; i < ed[x].size(); i++) { n     if (ed[x][i] != p) { n       dfs(ed[x][i], g, x); n     } n   } n } n int main() { n   int n; n   cin >> n; n   int i; n   for (i = 1; i <= n; i++) cin >> b[i]; n   for (i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     ed[x].push_back(y); n     ed[y].push_back(x); n   } n   dfs(1, 0, 0); n   for (i = 1; i <= n; i++) cout << an[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char arr[200]; n   int i, j, jp, g, t, cl, jm, a, m, n; n   scanf( %d%d , &cl, &jm); n   scanf( %c , &arr[0]); n   for (i = 1; i <= cl; i++) scanf( %c , &arr[i]); n   for (i = 1; i <= cl; i++) { n     if (arr[i] ==  G ) { n       g = i; n     } n     if (arr[i] ==  T ) { n       t = i; n     } n   } n   if (g > t) { n     m = t; n     n = g; n     a = g - t; n   } else { n     m = g; n     n = t; n     a = t - g; n   } n   for (i = m; i < n; i = i + jm) { n     if (arr[i] ==  # ) { n       printf( NO n ); n       return 0; n     } n   } n   if ((a % jm) == 0) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double eps = 1e-9; n const int INF = 1e9 + 7; n const int MAXN = int(2e5 + 7); n int n, k, b, ans; n long long cur = 1, t; n int main() { n   cin >> k >> b >> n >> t; n   while (cur <= t) { n     cur = cur * k + b; n     n--; n   } n   cout << max(0, ++n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void boost() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n const int N = 2e5 + 5; n int arr[N], ans[N]; n vector<int> adj[N]; n vector<int> factor[2][N]; n bool avail[N]; n void dfs(int u, int p, int d) { n   if (u != 1) { n     for (auto x : factor[0][p]) n       if (arr[u] % x == 0) n         factor[0][u].push_back(x); n       else n         factor[1][u].push_back(x); n     for (auto x : factor[1][p]) n       if (arr[u] % x == 0) factor[1][u].push_back(x); n   } n   if (d == 2) { n     for (int i = 1; i * i <= arr[u]; i++) { n       if (arr[u] % i == 0) { n         if (!avail[i]) factor[1][u].push_back(i); n       } n       if (i * i != arr[u] && (arr[u] % (arr[u] / i)) == 0) { n         if (!avail[arr[u] / i]) { n           factor[1][u].push_back(arr[u] / i); n         } n       } n     } n   } n   for (auto x : factor[0][u]) ans[u] = max(ans[u], x); n   for (auto x : factor[1][u]) ans[u] = max(ans[u], x); n   for (auto v : adj[u]) { n     if (v != p) dfs(v, u, d + 1); n   } n } n void solve() { n   int n; n   cin >> n; n   for (int i = 1; i < n + 1; ++i) { n     cin >> arr[i]; n   } n   int u, v; n   for (int i = 0; i < n - 1; ++i) { n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 1; i * i <= arr[1]; i++) { n     if (arr[1] % i == 0) { n       factor[0][1].push_back(i); n       avail[i] = true; n     } n     if (i * i != arr[1] && (arr[1] % (arr[1] / i)) == 0) { n       factor[0][1].push_back(arr[1] / i); n       avail[arr[1] / i] = true; n     } n   } n   dfs(1, 0, 1); n   for (int i = 1; i < n + 1; ++i) { n     cout << ans[i] <<    ; n   } n   return; n } n int main() { n   boost(); n   int tc = 1; n   while (tc--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, g, t; n   cin >> n >> k; n   char a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     if (a[i] ==  G ) g = i; n     if (a[i] ==  T ) t = i; n   } n   for (int i = 0; i < n; i++) { n     if (g < t) { n       while (g < n && a[g] !=  #  && a[g] !=  T ) g = g + k; n       if (a[g] ==  T ) { n         cout <<  YES  << endl; n         return 0; n       } else if (a[g] ==  #  || g >= n) { n         cout <<  NO  << endl; n         return 0; n       } n     } else if (g > t) { n       while (g >= 0 && a[g] !=  #  && a[g] !=  T ) g = g - k; n       if (a[g] ==  T ) { n         cout <<  YES  << endl; n         return 0; n       } else if (a[g] ==  #  || g < 0) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } 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   cout.tie(NULL); n   string s; n   cin >> s; n   int n = s.size(); n   bool dp[n + 1][8]; n   memset(dp, false, sizeof(dp)); n   string ans[8]; n   for (int i = 1; i < n + 1; i++) { n     string temp_ans[8]; n     dp[i][(s[i - 1] -  0 ) % 8] = true; n     temp_ans[(s[i - 1] -  0 ) % 8] = s[i - 1]; n     for (int j = 0; j < 8; j++) { n       if (dp[i - 1][j]) { n         int temp = (10 * j + (s[i - 1] -  0 )) % 8; n         dp[i][temp] = true; n         temp_ans[temp] = ans[j] + s[i - 1]; n         dp[i][j] = true; n         temp_ans[j] = ans[j]; n       } n     } n     for (int j = 0; j < 8; j++) ans[j] = temp_ans[j]; n   } n   if (dp[n][0]) { n     cout <<  YES n ; n     cout << ans[0] << endl; n   } else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int b, l, d; n   cin >> b >> l >> d; n   long long int m = b; n   if (l > m) { n     m = l; n   } n   if (d > m) { n     m = d; n   } n   long long int miss = 0; n   if (m - 1 - b > 0) { n     miss += m - 1 - b; n   } n   if (m - 1 - l > 0) { n     miss += m - 1 - l; n   } n   if (m - 1 - d > 0) { n     miss += m - 1 - d; n   } n   cout << miss; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > a[100500]; n int d[100500]; n int main() { n   int n, m, cap; n   scanf( %d%d%d , &n, &m, &cap); n   cap--; n   for (int i = 0; i < (m); i++) { n     int x, y, w; n     scanf( %d%d%d , &x, &y, &w); n     x--, y--; n     a[x].push_back(pair<int, int>(y, w)); n     a[y].push_back(pair<int, int>(x, w)); n   } n   int l; n   scanf( %d , &l); n   const int INF = 1100000000; n   for (int i = 0; i < (n); i++) d[i] = INF; n   d[cap] = 0; n   set<pair<int, int> > s; n   s.insert(pair<int, int>(d[cap], cap)); n   while (!s.empty()) { n     int v = s.begin()->second; n     s.erase(s.begin()); n     for (typeof((a[v]).begin()) it = (a[v]).begin(); it != (a[v]).end(); ++it) { n       int u = it->first; n       int w = it->second; n       if (d[u] >= INF || d[u] >= d[v] + w) { n         if (d[u] < INF) s.erase(s.find(pair<int, int>(d[u], u))); n         d[u] = d[v] + w; n         s.insert(pair<int, int>(d[u], u)); n       } n     } n   } n   int ans = 0; n   for (int i = 0; i < (n); i++) n     for (typeof((a[i]).begin()) it = (a[i]).begin(); it != (a[i]).end(); ++it) { n       int u = i; n       int v = it->first; n       int w = it->second; n       int x = d[v] - d[u] + w; n       assert(0 <= x && x <= w + w); n       if (0 < x && x < w + w) { n         if (d[u] < l && l + l <= 2 * d[u] + x) ans++; n         if (d[v] < l && l + l <= 2 * d[v] + w + w - x) ans++; n         if (l + l == 2 * d[u] + x && l + l == 2 * d[v] + w + w - x) ans--; n       } else { n         if (min(d[u], d[v]) < l && l < max(d[u], d[v])) ans++; n       } n     } n   ans >>= 1; n   for (int i = 0; i < (n); i++) ans += d[i] == l; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using D = double; n using uint = unsigned int; n template <typename T> n using pair2 = pair<T, T>; n const int maxn = 300005; n bool used[maxn]; n int n, k; n vector<int> all; n int main() { n   cin >> n >> k; n   if (k == 6) { n     if (n < 6) n       printf( No n ); n     else { n       printf( Yes n ); n       printf( 5 n ); n       printf( 1 2 4 5 6 n ); n     } n     return 0; n   } n   int cur = 0; n   int mx = 0; n   for (int i = 1; i <= n && cur < k; i++) { n     mx = i; n     used[i] = true; n     for (int j = 1; j < i && j * j <= i; j++) n       if (i % j == 0) { n         cur++; n         if (i / j > j && i / j < i) cur++; n       } n   } n   if (cur < k) { n     printf( No n ); n     return 0; n   } n   for (int IT = 0; IT < 3; IT++) { n     for (int i = mx; i >= 1 && cur > k; i--) n       if (used[i]) { n         int cnt = 0; n         for (int j = 1; j < i && j * j <= i; j++) n           if (i % j == 0) { n             if (used[j]) cnt++; n             if (i / j > j && i / j < i && used[i / j]) cnt++; n           } n         for (int j = 2 * i; j <= mx; j++) n           if (used[j]) cnt++; n         if (cnt <= cur - k) { n           used[i] = false; n           cur -= cnt; n         } n       } n   } n   assert(cur == k); n   for (int i = 0; i <= mx; i++) n     if (used[i]) all.push_back(i); n   printf( Yes n ); n   printf( %d n , (int)all.size()); n   for (auto t : all) printf( %d  , t); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 55; n int n, r1, c1, r2, c2, ans = 1000000000; n string s[sz]; n bool visited[sz][sz]; n vector<pair<int, int> > v; n void dfs(int x, int y, bool flag) { n   if (x < 1 || x > n || y < 1 || y > n || s[x][y] ==  1  || n       visited[x][y] == true) { n     return; n   } n   visited[x][y] = true; n   if (flag == true) { n     v.push_back(make_pair(x, y)); n   } else { n     for (int i = 0; i < v.size(); i++) { n       int cost = (v[i].first - x) * (v[i].first - x) + n                  (v[i].second - y) * (v[i].second - y); n       ans = min(ans, cost); n     } n   } n   dfs(x - 1, y, flag); n   dfs(x + 1, y, flag); n   dfs(x, y - 1, flag); n   dfs(x, y + 1, flag); n } n int main() { n   cin >> n; n   cin >> r1 >> c1 >> r2 >> c2; n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n     s[i] =     + s[i]; n   } n   memset(visited, false, sizeof(visited)); n   dfs(r2, c2, true); n   memset(visited, false, sizeof(visited)); n   dfs(r1, c1, false); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2, t =   ; n   cin >> s1 >> s2; n   int n = s1.size(), c = 0, c1 = 0, c2 = 0; n   for (int i = 0; i < n; i++) { n     if (s1[i] != s2[i]) c++; n   } n   if (c % 2 != 0) n     cout <<  impossible  << endl; n   else { n     c1 = c2 = c / 2; n     for (int i = 0; i < n; i++) { n       if (s1[i] == s2[i]) n         t += s1[i]; n       else if (s1[i] != s2[i]) { n         if (c1 > 0) { n           c1--; n           t += s1[i]; n         } else if (c2 > 0) { n           c2--; n           t += s2[i]; n         } n       } n     } n     cout << t << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void checkMin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkMax(T &a, T b) { n   if (a < b) a = b; n } n template <class T> n inline T Min(T a, T b) { n   return a < b ? a : b; n } n template <class T> n inline T Max(T a, T b) { n   return a > b ? a : b; n } n template <class T> n inline T Myabs(T a) { n   return a > 0 ? a : -a; n } n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n const int inf = 0x3f3f3f3f; n const long long linf = 1LL << 60; n const double pi = acos(-1.0); n const double eps = 1e-9; n const int mod = 100000007; n const int maxn = 10010; n int n, r, c; n char ch[maxn], s[maxn]; n void print(int v) { n   if (v) { n     print((v - 1) / 26); n     printf( %c ,  A  + (v - 1) % 26); n   } n } n int main() { n   scanf( %d , &n); n   for (int cas = 1; cas <= n; cas++) { n     scanf( %s , ch); n     if (sscanf(ch,  R%dC%d , &r, &c) == 2) { n       print(c); n       printf( %d n , r); n     } else { n       sscanf(ch,  %[^0-9]%d , s, &r); n       int ans = 0; n       for (int i = 0; s[i]; i++) ans *= 26, ans += s[i] -  A  + 1; n       printf( R%dC%d n , r, ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2][4500005], xl[3005], ans, gd[3005]; n void merge(int l, int r) { n   if (l == r) return; n   int mid = (l + r) >> 1; n   merge(l, mid), merge(mid + 1, r); n   int i = l, j = mid + 1, dq = l; n   while (i <= mid && j <= r) { n     if (xl[i] <= xl[j]) n       gd[dq++] = xl[i++]; n     else n       gd[dq++] = xl[j++], ans += mid - i + 1; n   } n   for (; i <= mid; ++i) gd[dq++] = xl[i]; n   for (; j <= r; ++j) gd[dq++] = xl[j]; n   for (int i = l; i <= r; ++i) xl[i] = gd[i]; n   return; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &xl[i]); n   merge(1, n); n   for (int i = 1; i <= ans; ++i) { n     dp[1][i] = dp[0][i - 1] + 1; n     dp[0][i] = dp[1][i - 1] + 3; n   } n   printf( %.6lf , (double)dp[1][ans]); n } n


#include <bits/stdc++.h> n int main() { n   int n, i; n   while (scanf( %d , &n) != EOF) { n     printf( %d , n); n     for (i = 1; i < n; i++) printf(  %d , i); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 203600; n pair<pair<int, int>, int> p[N]; n int n; n vector<int> ans; n int main() { n   cin >> n; n   for (int i = (0); i < (int)(n); i++) { n     cin >> p[i].first.first >> p[i].first.second; n     p[i].second = i + 1; n   } n   sort(p, p + n); n   for (int i = (0); i < (int)(n); i++) { n     int cur = -1e9; n     bool ok = 1; n     for (int j = (0); j < (int)(n); j++) n       if (j != i) { n         if (p[j].first.first < cur) n           ok = 0; n         else n           cur = p[j].first.second; n       } n     if (ok) ans.push_back(p[i].second); n   } n   sort((ans).begin(), (ans).end()); n   printf( %d n , (int)ans.size()); n   for (int i = (0); i < (int)(ans.size()); i++) n     printf( %d%s , ans[i], i + 1 == ans.size() ?   n  :    ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, r1 = -1, p1 = -1, p2 = INT_MAX, x, a, b; n   cin >> n; n   while (n--) cin >> x, r1 = max(r1, x); n   cin >> n; n   while (n--) cin >> x, p1 = max(p1, x); n   cin >> n; n   while (n--) cin >> x, p2 = min(p2, x); n   cin >> a >> b; n   long double q = ((long long)(b * p1) / (long double)(a * p2 + b * p1)); n   q = sqrt(q); n   q *= r1; n   printf( %.12Lf n , q); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], f[100005], st[100005]; n int main() { n   int n, top = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   for (int i = n - 1; i >= 0; i--) { n     int t = 0; n     while (top && a[st[top - 1]] < a[i]) f[i] = t = max(t + 1, f[st[--top]]); n     st[top++] = i; n   } n   printf( %d n , *max_element(f, f + n)); n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> m; n map<long long, int>::iterator it; n int main() { n   int n, k, q, a[150001]; n   cin >> n >> k >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   while (q--) { n     int x, y; n     cin >> x >> y; n     if (x == 1) n       m.insert(make_pair(-a[y], y)); n     else { n       int count = 0, p = 1; n       if (m.find(-a[y]) == m.end()) n         p = 0; n       else { n         for (it = m.begin(); it != m.end(); it++) { n           if (it->first == -a[y]) n             break; n           else n             count++; n           if (count == k) { n             p = 0; n             break; n           } n         } n       } n       if (p) 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 double PI = 3.141592653589793; n vector<complex<double> > duzywek; n vector<complex<double> > duzyret[50][2]; n void szykuj() { n   for (int j = 0; j < 22; j++) { n     for (int i = 1; i <= (1 << (21 - j)); i++) { n       duzyret[j][0].push_back(0); n       if (j) duzyret[j][1].push_back(0); n     } n   } n } n int potenga(int v) { n   for (int i = 1; 1; i <<= 1) { n     if (i >= v) { n       return i; n     } n   } n } n void dft(int nn, int kt, int coile, complex<double> omega, int gdzie, int poz) { n   if (nn == 1) { n     duzyret[poz][gdzie][0] = duzywek[kt]; n     return; n   } n   dft(nn >> 1, kt, coile << 1, omega * omega, 0, poz + 1); n   dft(nn >> 1, kt + coile, coile << 1, omega * omega, 1, poz + 1); n   complex<double> jeden(1.0, 0.0); n   int w = 0; n   int an = (nn >> 1) - 1; n   for (int i = 0; i < nn; i++) { n     duzyret[poz][gdzie][i] = n         duzyret[poz + 1][0][i & an] + jeden * duzyret[poz + 1][1][i & an]; n     jeden *= omega; n   } n } n vector<int> fft(vector<int> jed, vector<int> dwa) { n   int n1 = potenga(jed.size() + dwa.size()); n   while (jed.size() < n1) { n     jed.push_back(0); n   } n   while (dwa.size() < n1) { n     dwa.push_back(0); n   } n   complex<double> omega(cos(2.0 * PI / n1), sin(2.0 * PI / n1)); n   vector<complex<double> > pam; n   duzywek.clear(); n   for (int i = 0; i < n1; i++) { n     duzywek.push_back(complex<double>(jed[i], 0.0)); n   } n   dft(n1, 0, 1, omega, 0, 0); n   pam = duzyret[0][0]; n   for (int i = 0; i < n1; i++) { n     duzywek[i] = complex<double>(dwa[i], 0.0); n   } n   dft(n1, 0, 1, omega, 0, 0); n   for (int i = 0; i < n1; i++) { n     duzywek[i] = pam[i] * duzyret[0][0][i]; n   } n   omega = (complex<double>(1.0, 0.0)) / omega; n   dft(n1, 0, 1, omega, 0, 0); n   vector<int> retret; n   int dowrzucenia; n   for (int i = 0; i < n1; i++) { n     duzyret[0][0][i] /= n1; n     dowrzucenia = round(duzyret[0][0][i].real()); n     retret.push_back(dowrzucenia); n   } n   return retret; n } n int n, m; n vector<int> dopo; n vector<int> otrz; n vector<int> wyn; n int x; n int main() { n   szykuj(); n   scanf( %d%d , &n, &m); n   while (dopo.size() <= m) { n     dopo.push_back(0); n   } n   dopo[0] = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     dopo[x] = 1; n   } n   otrz = fft(dopo, dopo); n   while (otrz.size() > m + 1) otrz.pop_back(); n   for (int i = 1; i < otrz.size(); i++) { n     if (otrz[i] && !dopo[i]) { n       printf( NO ); n       return 0; n     } n     if (otrz[i] == 2) { n       wyn.push_back(i); n     } n   } n   printf( YES n ); n   printf( %d n , (int)wyn.size()); n   for (int i = 0; i < wyn.size(); i++) { n     printf( %d  , wyn[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> last; n int b[1000000]; n long long a[100000]; n long long d[100][2]; n pair<long long, long long> solve(long long x, long long y) { n   long long xx, yy; n   pair<long long, long long> ans; n   if (x == 1) { n     return make_pair(1, 0); n   } n   xx = x; n   yy = y % x; n   if (yy == 1) { n     ans = make_pair(1, xx - 1); n   } else { n     ans = solve(xx % yy, yy); n     ans.second += (xx / yy) * ans.first; n     ans.first %= yy; n     ans.second %= xx; n   } n   ans.first += (y / x) * ans.second; n   ans.first %= y; n   ans.second %= x; n   return ans; n } n long long inv(long long x, long long y) { n   x %= y; n   pair<long long, long long> p = solve(x, y); n   return p.first; n } n long long gcd(long long x, long long y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n int main() { n   vector<long long> primes; n   long long i, j, k, l; n   long long m, n, now, tmp, tmp1, cnt, b1, nn, ans, all; n   memset(b, 0, sizeof(b)); n   primes.clear(); n   for (i = 2; i < 1000000; i++) { n     if (b[i] == 0) { n       primes.push_back(i); n       for (j = i + i; j < 1000000; j += i) b[j] = 1; n     } n   } n   scanf( %I64d%I64d%I64d , &n, &m, &k); n   for (i = 0; i < k; i++) scanf( %I64d , &a[i]); n   last.clear(); n   now = 1; n   for (i = 0; i < k; i++) { n     tmp = gcd(now, a[i]); n     now /= tmp; n     if ((1LL << 60) / now < a[i]) { n       printf( NO n ); n       return 0; n     } n     now *= a[i]; n     if (now > n) { n       printf( NO n ); n       return 0; n     } n     tmp = a[i]; n     for (j = 0; j < primes.size() && primes[j] * primes[j] <= tmp; j++) { n       cnt = 0; n       while (tmp % primes[j] == 0) { n         tmp /= primes[j]; n         cnt++; n       } n       tmp1 = 1; n       for (l = 0; l < cnt; l++) { n         tmp1 *= primes[j]; n         if (i - tmp1 >= 0) { n           if ((last.find(tmp1) == last.end()) || (last[tmp1] != i - tmp1)) { n             printf( NO n ); n             return 0; n           } n           last[tmp1] = i; n         } else { n           if (last.find(tmp1) != last.end()) { n             printf( NO n ); n             return 0; n           } n           last[tmp1] = i; n         } n       } n     } n     if (tmp > 1) { n       tmp1 = tmp; n       if (i - tmp1 >= 0) { n         if ((last.find(tmp1) == last.end()) || (last[tmp1] != i - tmp1)) { n           printf( NO n ); n           return 0; n         } n         last[tmp1] = i; n       } else { n         if (last.find(tmp1) != last.end()) { n           printf( NO n ); n           return 0; n         } n         last[tmp1] = i; n       } n     } n   } n   for (map<long long, int>::iterator it = last.begin(); it != last.end(); n        it++) { n     if (it->second + it->first < k) { n       printf( NO n ); n       return 0; n     } n   } n   nn = 0; n   for (map<long long, int>::reverse_iterator it = last.rbegin(); n        it != last.rend(); it++) { n     b1 = 1; n     for (i = 0; i < nn; i++) n       if (d[i][0] % it->first == 0) { n         b1 = 0; n         break; n       } n     if (b1 == 0) continue; n     d[nn][0] = it->first; n     d[nn][1] = k - 1 - it->second; n     nn++; n   } n   ans = 0; n   all = 1; n   for (i = 0; i < nn; i++) all *= d[i][0]; n   for (i = 0; i < nn; i++) { n     tmp = 1; n     for (j = 0; j < nn; j++) n       if (j != i) tmp *= d[j][0]; n     tmp *= inv(tmp, d[i][0]); n     tmp *= d[i][1]; n     tmp %= all; n     ans += tmp; n   } n   ans %= all; n   if (ans - (k - 1) <= 0) ans += all; n   if (ans <= m) { n     printf( YES n ); n   } else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y; n   bool operator<(const node& a) const { return x < a.x || x == a.x && y < a.y; } n }; n const int dr[] = {-1, 1, 0, 0, -1, -1, 1, 1}; n const int dc[] = {0, 0, -1, 1, -1, 1, -1, 1}; n int HH[277]; n char s[401000]; n int main() { n   int n, k; n   do { n     char _c = getchar(), _v = 1; n     for (n = 0; _c < 48 || _c > 57; _c = getchar()) n       if (_c == 45) _v = -1; n     for (; _c >= 48 && _c <= 57; n          n = (n << 1) + (n << 3) + _c - 48, _c = getchar()) n       ; n     n *= _v; n   } while (0); n   do { n     char _c = getchar(), _v = 1; n     for (k = 0; _c < 48 || _c > 57; _c = getchar()) n       if (_c == 45) _v = -1; n     for (; _c >= 48 && _c <= 57; n          k = (k << 1) + (k << 3) + _c - 48, _c = getchar()) n       ; n     k *= _v; n   } while (0); n   scanf( %s , s); n   for (int i = 0; i < n; i++) ++HH[s[i]]; n   if (n == k) return 0; n   char c; n   for (c =  a ; c <=  z ; ++c) { n     k -= HH[c]; n     if (k <= 0) { n       k += HH[c]; n       break; n     } n     HH[c] = 0; n   } n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (HH[s[i]]) { n       if (s[i] != c) n         putchar(s[i]); n       else { n         cnt++; n         if (cnt > k) putchar(s[i]); n       } n     } n   } 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   while (t--) { n     int n, m; n     cin >> n >> m; n     bitset<32> b1(n); n     bitset<32> b2(m + 1); n     vector<int> a(32), b(32); n     for (int i = 0; i < 32; i++) a[i] = b1[31 - i]; n     for (int i = 0; i < 32; i++) b[i] = b2[31 - i]; n     long long ans = 0; n     for (int i = 0; i < 32; i++) { n       if (a[i] == 1 && b[i] == 0) n         break; n       else n         ans += (a[i] ^ b[i]) * pow(2, 31 - i); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int INFi = 2e9; n const int maxN = 1e5 + 5; n const int md = 998244353; n const int md2 = 1e9 + 7; n double getTime() { return clock() / (double)CLOCKS_PER_SEC; }; n void solve() { n   int m, n; n   cin >> m >> n; n   vector<pair<int, int>> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second; n   sort((a).begin(), (a).end()); n   vector<long long> suf(n + 1); n   suf[n] = 0; n   suf[n - 1] = a[n - 1].first; n   for (int i = n - 2; i >= 0; --i) suf[i] = suf[i + 1] + a[i].first; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     long long res = 0; n     pair<int, int> kek = {a[i].second, -1}; n     int l = lower_bound((a).begin(), (a).end(), kek) - a.begin(); n     l = max(l, n - m); n     int cnt = n - l; n     res += suf[l]; n     if (i < l && cnt < m) { n       cnt++; n       res += a[i].first; n     } n     res += 1ll * a[i].second * (m - cnt); n     ans = max(ans, res); n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int tests = 1; n   cin >> tests; n   for (int _ = 0; _ < tests; ++_) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n  n const int N = 5010; n const int M = 1 << 20; n const int K = 30; n  n char s[N]; n int vis[K]; n bool dp[M]; n int cnt[K], l[K], r[K]; n  n int main(){ n     int n, a, b; n     scanf( %d%d%d , &n, &a, &b); n     scanf( %s , s); n     memset(vis, -1, sizeof(vis)); n     int id = 0; n     for (int i = 0; i < n; ++ i){ n         if (vis[s[i] -  a ] != -1){ n             continue; n         } n         vis[s[i] -  a ] = id; n         l[id] = i; n         for (int j = i; j < n; ++ j){ n             if (s[j] == s[i]){ n                 r[id] = j; n                 ++ cnt[id]; n             } n         } n         if (1ll * a * (r[id] - l[id] + 1) <= 1ll * cnt[id] * b){ n             dp[1 << id] = true; n         } n         ++ id; n     } n     for (int i = 3; i < (1 << id) - 1; ++ i){ n         if (__builtin_popcount(i) <= 1){ n             continue; n         } n         for (int j = 1; j < id; ++ j){ n             int x = i & ((1 << j) - 1); n             int y = i ^ x; n             if (x < i && y < i){ n                 if (dp[x] && dp[y]){ n                     dp[i] = true; n                     break; n                 } n             } n         } n         if (dp[i]){ n             continue; n         } n         int left = INT_MAX, right = INT_MIN; n         int ccnt = 0; n         for (int j = 0; j < id; ++ j){ n             if (i >> j & 1){ n                 left = std::min(left, l[j]); n                 right = std::max(right, r[j]); n                 ccnt += cnt[j]; n             } n         } n         if (1ll * a * (right - left + 1) <= 1ll * ccnt * b){ n             for (int j = 0; j < id; ++ j){ n                 if (i >> j & 1){ n                     if (dp[i ^ 1 << j]){ n                         dp[i] = true; n                         break; n                     } n                 } n             } n         } n     } n     dp[0] = true; n     std::vector <char> ans; n     for (int i = 0; i < 26; ++ i){ n         if (vis[i] == -1){ n             continue; n         } n         if (dp[((1 << id) - 1) ^ (1 << vis[i])]){ n             ans.emplace_back( a  + i); n         } n     } n     printf( %d , int(ans.size())); n     for (auto u : ans){ n         printf(  %c , u); n     } n     puts(  ); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   char a[55][55]; n   memset(a, 0, sizeof(a)); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   cout << a[1] << endl; n   int r[55], c[55], rn = 0, cn = 0; n   bool rr[55]; n   memset(rr, 0, sizeof(rr)); n   for (int i = 1; i <= n; i++) { n     rr[i] = 1; n     memset(r, 0, sizeof(r)); n     memset(c, 0, sizeof(c)); n     rn = cn = 0; n     for (int j = 1; j <= m; j++) { n       if (a[i][j] ==  # ) { n         c[++cn] = j; n         for (int k = i + 1; k <= n; k++) { n           if (a[k][j] ==  #  && !rr[k]) { n             rr[k] = 1; n             r[++rn] = k; n           } n         } n       } n     } n     for (int j = 1; j <= rn; j++) { n       for (int k = 1; k <= cn; k++) { n         if (a[r[j]][c[k]] !=  # ) { n           cout <<  No n ; n           return 0; n         } n       } n       int num = 0; n       for (int k = 1; k <= m; k++) { n         if (a[r[j]][k] ==  # ) { n           num++; n         } n       } n       if (num != cn) { n         cout <<  No n ; n         return 0; n       } n     } n   } n   cout <<  Yes n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int h; n int a[N]; n int sum[N]; n int main() { n   scanf( %d , &h); n   for (int i = 0; i <= h; i++) { n     scanf( %d , &a[i]); n     sum[i] += sum[i - 1] + a[i]; n   } n   bool check = false; n   int pre = 1; n   for (int i = 0; i <= h; i++) { n     if (a[i] == 1) { n       pre = 1; n       continue; n     } else { n       if (pre == 1) { n         pre = a[i]; n         continue; n       } else { n         check = true; n       } n     } n   } n   if (check == false) { n     cout <<  perfect ; n     return 0; n   } n   cout <<  ambiguous  << endl; n   cout <<  0  ; n   for (int i = 1; i <= h; i++) { n     for (int j = 1; j <= a[i]; j++) { n       cout << sum[i - 2] + 1 <<    ; n     } n   } n   cout << endl; n   cout <<  0  ; n   for (int i = 1; i <= h; i++) { n     for (int j = 1; j <= a[i]; j++) { n       if (sum[i - 2] + j <= sum[i - 1]) n         cout << sum[i - 2] + j <<    ; n       else { n         cout << sum[i - 2] + 1 <<    ; n       } n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100010]; n int b[100010]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   b[0] = 1; n   for (int i = 1; i < n; i++) n     if (a[i] > a[i - 1]) n       b[i] = b[i - 1] + 1; n     else n       b[i] = 1; n   int mx = 1; n   for (int i = 0; i < n; i++) mx = max(mx, b[i]); n   if (mx != n) mx += 1; n   int nx = 0; n   if (n > 1 && a[0] >= a[1]) { n     for (int i = 2; i < n; i++) n       if (b[i] == 1) { n         mx = max(mx, b[i - 1] + 1); n         break; n       } else if (i == n - 1) n         mx = max(mx, b[n - 1] + 1); n   } n   for (int i = 1; i < n; i++) { n     if (b[i] == 1) { n       mx = max(mx, b[i - 1] + nx); n       mx = max(mx, b[i - 1] + 1); n       if (a[i + 1] - a[i - 1] >= 2) n         nx = b[i - 1]; n       else n         nx = 0; n     } else if (i == n - 1) n       mx = max(mx, b[i] + nx); n   } n   nx = 0; n   for (int i = 0; i < n; i++) { n     if (b[i + 1] == 1 && i + 1 < n) { n       mx = max(mx, b[i] + nx); n       mx = max(mx, b[i] + 1); n       if (a[i + 1] - a[i - 1] >= 2) n         nx = b[i]; n       else n         nx = 0; n     } else if (i + 1 == n) n       mx = max(mx, b[i] + nx); n   } n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110; n int T, mod = 1e9 + 7, n, k, deep[N], timer, sz; n long long ans, fact[N], mem[N][N], vis[N][N]; n vector<int> adj[N], v; n map<int, int> mp[N]; n void _clear() { n   ans = 0; n   for (int i = 1; i <= n; i++) adj[i].clear(); n } n long long po(long long x, long long os) { n   if (os == 0) return 1; n   long long z = po(x, os / 2); n   if (os & 1) return z * z % mod * x % mod; n   return z * z % mod; n } n long long modInv(long long x) { return po(x, mod - 2); } n long long c(long long n, long long r) { n   return fact[n] * modInv(fact[r] * fact[n - r] % mod) % mod; n } n void dfs(int u, int v) { n   deep[u] = deep[v] + 1; n   mp[u][deep[u]]++; n   for (auto &x : adj[u]) n     if (x != v) { n       dfs(x, u); n       for (auto &p : mp[x]) mp[u][p.first] += p.second; n     } n } n long long dp(int i, int k) { n   if (k == 0) return 1; n   if (i == sz) return 0; n   long long &ret = mem[i][k]; n   if (vis[i][k] == timer) return ret; n   vis[i][k] = timer; n   return ret = (dp(i + 1, k) + dp(i + 1, k - 1) * v[i] % mod) % mod; n } n long long calc(int p) { n   deep[p] = 0; n   for (int i = 1; i <= n; i++) mp[i].clear(); n   dfs(p, p); n   long long ret = 0; n   for (int i = 2; i <= n; i++) { n     int num = 0; n     v.clear(); n     for (auto &x : adj[p]) n       if (mp[x].find(i) != mp[x].end()) { n         num++; n         v.push_back(mp[x][i]); n       } n     if (num < k) continue; n     timer++; n     sz = (int)(v.size()); n     ret = (ret + dp(0, k)) % mod; n   } n   return ret; n } n int main() { n   fact[0] = 1; n   for (int i = 1; i < N; i++) fact[i] = fact[i - 1] * i % mod; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &k); n     _clear(); n     for (int i = 1; i <= n - 1; i++) { n       int a, b; n       scanf( %d%d , &a, &b); n       adj[a].push_back(b); n       adj[b].push_back(a); n     } n     if (k == 2) { n       printf( %lld n , c(n, 2)); n       continue; n     } n     for (int i = 1; i <= n; i++) ans = (ans + calc(i)) % mod; n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int x = 0; n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x; n } n const int maxn = (1 << 21) + 3; n const int INF = 0x3f3f3f3f; n struct segtree { n   int n; n   int tag[maxn], mmin[maxn]; n   int ul, ur, uv; n   void update(int o, int l, int r) { n     if (ul <= l && ur >= r) { n       tag[o] = max(tag[o], uv); n       mmin[o] = max(mmin[o], uv); n       return; n     } n     int mid = (l + r) / 2; n     if (ul <= mid) update(o * 2, l, mid); n     if (ur > mid) update(o * 2 + 1, mid + 1, r); n     mmin[o] = max(min(mmin[o * 2], mmin[o * 2 + 1]), tag[o]); n   } n   void modify(int l, int r, int v) { n     if (l > r) return; n     ul = l, ur = r, uv = v; n     update(1, 1, n); n   } n   int p, x; n   int query2(int o, int l, int r) { n     if (max(tag[o], mmin[o]) >= x) return n + 1; n     if (l == r) return l; n     int mid = (l + r) / 2; n     if (mmin[o * 2] < x) n       return query2(o * 2, l, mid); n     else n       return query2(o * 2 + 1, mid + 1, r); n   } n   int query(int o, int l, int r) { n     if (max(tag[o], mmin[o]) >= x) return n + 1; n     if (l == r) return l; n     int mid = (l + r) / 2; n     if (p > mid) n       return query(o * 2 + 1, mid + 1, r); n     else { n       int ans = query(o * 2, l, mid); n       if (ans != n + 1) n         return ans; n       else n         return query2(o * 2 + 1, mid + 1, r); n     } n   } n   int query(int p, int x) { n     if (p > n) return n + 1; n     this->p = p, this->x = x; n     return query(1, 1, n); n   } n } t; n struct edge { n   int l, r; n   bool operator<(const edge& rhs) const { return r < rhs.r; } n } e[maxn]; n struct query { n   int id, x, y; n   bool operator<(const query& rhs) const { return y < rhs.y; } n } q[maxn]; n int ans[maxn]; n void write(int x) { n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n void writeln(int x) { n   write(x); n   putchar(  n ); n } n int main() { n   int n = t.n = read(), k = read(); n   for (int i = 1; i <= k; i++) e[i].l = read(), e[i].r = read(); n   sort(e + 1, e + k + 1); n   int m = read(); n   for (int i = 1; i <= m; i++) q[i].id = i, q[i].x = read(), q[i].y = read(); n   sort(q + 1, q + m + 1); n   int now = 1; n   for (int i = 1; i <= m; i++) { n     while (now <= k && e[now].r <= q[i].y) { n       t.modify(e[now].l + 1, e[now].r, e[now].l); n       now++; n     } n     ans[q[i].id] = t.query(q[i].x + 1, q[i].x) - 1; n   } n   for (int i = 1; i <= m; i++) writeln(ans[i]); n } n


#include <bits/stdc++.h> n const int MOD = 1000000007; n const int SIZE_N = 2000 + 10; n int n; n int ary[SIZE_N]; n int vis[SIZE_N]; n int fac[SIZE_N]; n int madd(long long a, long long b) { return (a + b) % MOD; } n int mmul(long long a, long long b) { return (a * b) % MOD; } n int dp[SIZE_N][SIZE_N], v[SIZE_N][SIZE_N]; n int f(int a, int b) { n   if (v[a][b] == 1) return dp[a][b]; n   v[a][b] = 1; n   if (a == 0) return dp[a][b] = fac[b]; n   if (a == 1) return dp[a][b] = mmul(b, fac[b]); n   return dp[a][b] = madd(mmul(b, f(a - 1, b)), mmul(a - 1, f(a - 2, b + 1))); n } n int main(void) { n   fac[0] = 1; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , ary + i); n     vis[ary[i]] = 1; n     fac[i] = mmul(fac[i - 1], i); n   } n   int a = 0, b = 0; n   for (int i = 1; i <= n; ++i) { n     if (vis[i] == 1) continue; n     if (ary[i] == -1) n       ++a; n     else n       ++b; n   } n   int ans = 0; n   ans = f(a, b); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n const int M = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int s, x1, x2, t1, t2, p, d; n   cin >> s >> x1 >> x2; n   cin >> t1 >> t2; n   cin >> p >> d; n   int ans = t2 * abs(x1 - x2); n   if (x1 < x2) { n     if (d == -1) n       ans = min(ans, (p + x2) * t1); n     else if (p <= x1) n       ans = min(ans, (x2 - p) * t1); n     else n       ans = min(ans, (s + s - p + x2) * t1); n   } else { n     if (d == 1) n       ans = min(ans, (s + s - p - x2) * t1); n     else if (p >= x1) n       ans = min(ans, (p - x2) * t1); n     else n       ans = min(ans, (s + p + s - x2) * t1); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while ((t--)) { n     int n; n     cin >> n; n     int arr[n]; n     int diff[n - 1]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     int ctr = 0; n     int min = arr[n - 1]; n     for (int i = n - 2; i >= 0; i--) { n       if (min >= arr[i]) { n         min = arr[i]; n       } else { n         ctr++; n       } n     } n     cout << ctr <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 1e9; n vector<long long> lucky_nums; n void generate_lucky(long long n) { n   if (n > MAX) return; n   long long n1 = 10 * n + 4; n   lucky_nums.push_back(n1); n   generate_lucky(n1); n   long long n2 = 10 * n + 7; n   lucky_nums.push_back(n2); n   generate_lucky(n2); n } n void solve() { n   generate_lucky(0); n   sort(lucky_nums.begin(), lucky_nums.end()); n   long long l, r; n   cin >> l >> r; n   int i = n       lower_bound(lucky_nums.begin(), lucky_nums.end(), l) - lucky_nums.begin(); n   long long prev = l, ans = 0; n   while (lucky_nums[i] < r) { n     ans += (lucky_nums[i] - prev + 1) * lucky_nums[i]; n     prev = lucky_nums[i] + 1; n     i++; n   } n   ans += (r - prev + 1) * lucky_nums[i]; n   cout << ans << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n int n, k; n char a[N], b[N]; n int main() { n   scanf( %d%d%s%s , &n, &k, a, b); n   long long cur = 1, ans = 0; n   if (k == 1) { n     cout << n << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     cur *= 2; n     if (a[i] !=  a ) --cur; n     if (b[i] !=  b ) --cur; n     cur = min(cur, k * 1ll); n     ans += cur; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (1 << 30) - 1; n const long long oo = (1ll << 62) - 1; n const long double PI = 3.1415926535898; n const int N = 1000000 + 5; n int n, m; n vector<int> a[N], e[N]; n int num[N], low[N], vt[N], dd[N]; n int Time = 0, tpltm = 0; n stack<int> s; n vector<int> res1, res2; n void minimize(int &a, int b) { n   if (a > b) a = b; n } n void dfs(int u) { n   num[u] = low[u] = ++Time; n   s.push(u); n   for (int v : a[u]) { n     if (num[v] == -2) continue; n     if (num[v] > 0) { n       minimize(low[u], num[v]); n     } else { n       dfs(v); n       minimize(low[u], low[v]); n     } n   } n   if (low[u] >= num[u]) { n     tpltm += 1; n     int v; n     do { n       v = s.top(); n       s.pop(); n       vt[v] = tpltm; n       num[v] = -2; n     } while (v != u); n   } n } n void solve() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) a[i].clear(); n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     if (u == v) continue; n     a[u].push_back(v); n   } n   Time = 0; n   tpltm = 0; n   while (s.size()) s.pop(); n   for (int i = 1; i <= n; i++) num[i] = 0; n   for (int i = 1; i <= n; i++) { n     if (num[i]) continue; n     dfs(i); n   } n   if (tpltm <= 1) { n     cout <<  No  <<   n ; n     return; n   } n   for (int i = 1; i <= tpltm; i++) e[i].clear(); n   for (int u = 1; u <= n; u++) { n     for (int v : a[u]) { n       if (vt[u] == vt[v]) continue; n       e[vt[u]].push_back(vt[v]); n     } n   } n   int gt = INF, dinh = 0; n   for (int i = 1; i <= tpltm; i++) { n     if (e[i].size() < gt) { n       gt = e[i].size(); n       dinh = i; n     } n   } n   res1.clear(); n   res2.clear(); n   for (int i = 1; i <= n; i++) { n     if (vt[i] == dinh) { n       res1.push_back(i); n     } else n       res2.push_back(i); n   } n   if (res1.size() == 0 || res2.size() == 0) { n     cout <<  No  <<   n ; n     return; n   } n   cout <<  Yes  <<   n ; n   cout << res1.size() <<     << res2.size() <<   n ; n   for (int i = 0; i < res1.size(); i++) cout << res1[i] <<    ; n   cout <<   n ; n   for (int i = 0; i < res2.size(); i++) cout << res2[i] <<    ; n   cout <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   for (int i = 1; i <= t; i++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 505; n long long l1[MAX], l2[MAX], r1[MAX], r2[MAX], a1[MAX], a2[MAX]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     cin >> l1[i] >> r1[i] >> l2[i] >> r2[i]; n   } n   for (int i = 0; i < q; ++i) { n     a1[i] = l1[i], a2[i] = l2[i]; n     while (a1[i] == a2[i] && a2[i] <= r2[i]) { n       a2[i]++; n     } n     cout << a1[i] <<     << a2[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int M = 1000000000; n const int INF = 1e18; n int a[N], b[N], s1[N], s2[N]; n int n, m, k, d, top1, top2, ansr, ansl, sum[N], pos[N], t[N], mn[N]; n map<int, int> mp; n void reset(int x) { n   mn[x] = INF; n   for (int i = (x - 1) * m + 1; i <= min(n, x * m); i++) n     mn[x] = min(mn[x], t[i] + sum[x]); n } n inline void add(int l, int r, int c) { n   for (int i = l; i <= min(r, pos[l] * m); i++) t[i] += c; n   reset(pos[l]); n   if (pos[l] != pos[r]) { n     for (int i = (pos[r] - 1) * m + 1; i <= min(n, r); i++) t[i] += c; n     reset(pos[r]); n   } n   for (int i = pos[l] + 1; i <= pos[r] - 1; i++) sum[i] += c, mn[i] += c; n } n inline int query(int l, int r, int c) { n   for (int i = r; i >= max((pos[r] - 1) * m + 1, l); i--) n     if (t[i] + sum[pos[i]] <= c) { n       return i; n     } n   for (int i = pos[r] - 1; i >= pos[l] + 1; i--) n     if (mn[i] <= c) n       for (int j = i * m; j >= (i - 1) * m + 1; j--) n         if (t[j] + sum[pos[j]] <= c) { n           return j; n         } n   if (pos[l] != pos[r]) { n     for (int i = pos[l] * m; i >= l; i--) { n       if (t[i] + sum[pos[i]] <= c) return i; n     } n   } n } n inline void sol(int L, int R) { n   if (L == R) return; n   mp.clear(); n   int r = R + 1; n   top1 = top2 = 0; n   s1[top1] = s2[top2] = R + 1; n   for (int i = 0; i <= pos[n]; i++) sum[i] = 0; n   for (int i = R; i >= L; i--) { n     if (mp.count(a[i])) r = min(r, mp[a[i]]); n     while (top1 && a[i] > a[s1[top1]]) { n       add(s1[top1], s1[top1 - 1] - 1, -a[s1[top1]]); n       top1--; n     } n     s1[++top1] = i; n     add(s1[top1], s1[top1 - 1] - 1, a[s1[top1]]); n     while (top2 && a[i] < a[s2[top2]]) { n       add(s2[top2], s2[top2 - 1] - 1, a[s2[top2]]); n       top2--; n     } n     s2[++top2] = i; n     add(s2[top2], s2[top2 - 1] - 1, -a[s2[top2]]); n     int rr = query(i, r - 1, k - i); n     if (rr - i > ansr - ansl || (rr - i == ansr - ansl && i < ansl)) n       ansl = i, ansr = rr; n     mp[a[i]] = i; n   } n   return; n } n int main() { n   scanf( %d%d%d , &n, &k, &d); n   if (d == 0) { n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     int ansl = 1; n     int ansr = 1; n     int lst = 1; n     for (int i = 2; i <= n; i++) { n       if (a[i] != a[lst]) { n         if (i - 1 - lst > ansr - ansl) { n           ansl = lst; n           ansr = i - 1; n         } n         lst = i; n       } n     } n     printf( %d %d n , ansl, ansr); n     return 0; n   } n   ansl = n + 1; n   ansr = 0; n   m = sqrt(n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[i] = (a[i] += M) % d; n     a[i] = (a[i]) / d + 1; n     pos[i] = (i - 1) / m + 1; n     t[i] = -i; n   } n   for (int i = 1, j; i <= n; i = j) { n     for (j = i + 1; j <= n; j++) { n       if (b[i] != b[j]) break; n     } n     sol(i, j - 1); n   } n   printf( %d %d n , ansl, ansr); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, p[200005]; n int pn; n int main() { n   scanf( %I64d , &n); n   n *= 2; n   for (int k = 0; k < 60; ++k) { n     long long a = (1LL << (k + 1)) - 3; n     long long l = 0, r = 2000000000; n     if (a > 1) r = min(r, n / a + 1); n     while (r - l > 1) { n       long long x = (r + l) / 2; n       if (x * x + a * x <= n) n         l = x; n       else n         r = x; n     } n     if (l % 2 == 1 && l * l + a * l == n) p[pn++] = (l << k); n   } n   if (pn == 0) { n     puts( -1 ); n     exit(0); n   } n   sort(p, p + pn); n   for (int i = 0; i < pn; ++i) printf( %I64d n , p[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200010], t[200010]; n string ans; n void solve() { n   int n = strlen(s), x = 0; n   for (int i = 0; i < n; i++) { n     if (i) ans +=  + ; n     if (i < n - 1 && rand() % 2 == 0) { n       ans += (s[i]), ans += s[++i]; n       x += 10 * (s[i - 1] -  0 ) + s[i] -  0 ; n     } else { n       ans += (s[i]); n       x += s[i] -  0 ; n     } n   } n   ans +=   n ; n   sprintf(s,  %d , x); n } n int main() { n   srand(time(0)); n   scanf( %s%s , s, t); n   while (1) { n     strcpy(s, t); n     ans =   ; n     solve(); n     solve(); n     solve(); n     if (strlen(s) == 1) return cout << ans, 0; n   } n } n