#include <bits/stdc++.h> n using namespace std; n const int maxn = 500500; n bool x[maxn]; n int p[maxn], num[maxn], tmp[110]; n int main() { n   int n, q; n   long long ans = 0; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; i++) scanf( %d , num + i + 1); n   for (int u = 0; u < q; u++) { n     int t, k, cnt = 0; n     scanf( %d , &t); n     k = num[t]; n     if (x[t]) { n       for (int i = 2; i * i <= k; i++) n         if (k % i == 0) { n           tmp[cnt++] = i; n           while (k % i == 0) k /= i; n         } n       if (k != 1) tmp[cnt++] = k; n       for (int i = 0; i < (1 << cnt); i++) { n         long long cur = 0; n         int add = 0, mult = 1; n         for (int j = 0; j < cnt; j++) n           if (i & (1 << j)) mult *= tmp[j], add++; n         p[mult]--; n         cur = p[mult]; n         if (add & 1) n           ans += cur; n         else n           ans -= cur; n       } n       x[t] = 0; n     } else { n       for (int i = 2; i * i <= k; i++) n         if (k % i == 0) { n           tmp[cnt++] = i; n           while (k % i == 0) k /= i; n         } n       if (k != 1) tmp[cnt++] = k; n       for (int i = 0; i < (1 << cnt); i++) { n         long long cur = 0; n         int add = 0, mult = 1; n         for (int j = 0; j < cnt; j++) n           if (i & (1 << j)) mult *= tmp[j], add++; n         cur = p[mult]; n         p[mult]++; n         if (add & 1) n           ans -= cur; n         else n           ans += cur; n       } n       x[t] = 1; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n const int INF = 1e9; n int n, m, cl, ce, v, q; n int a[maxn + 5], b[maxn + 5]; n int main() { n   scanf( %d%d%d%d%d , &n, &m, &cl, &ce, &v); n   for (int i = 1; i <= cl; i++) scanf( %d , &b[i]); n   for (int i = 1; i <= ce; i++) scanf( %d , &a[i]); n   sort(a + 1, a + ce + 1); n   sort(b + 1, b + cl + 1); n   scanf( %d , &q); n   while (q--) { n     int Ax, Ay, Bx, By; n     int Min = INF, t; n     scanf( %d%d%d%d , &Ay, &Ax, &By, &Bx); n     if (Ax > Bx) swap(Ax, Bx); n     if (Ay > By) swap(Ay, By); n     if (Ay == By) Min = min(Min, Bx - Ax); n     int l = 1, r = ce, ans = 0; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (a[mid] <= Ax) n         l = mid + 1, ans = mid; n       else n         r = mid - 1; n     } n     if (ans != 0) n       t = (Ax - a[ans]) * 2 + (By - Ay + v - 1) / v + Bx - Ax; n     else n       t = INF; n     Min = min(Min, t); n     l = 1, r = cl, ans = 0; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (b[mid] <= Ax) n         l = mid + 1, ans = mid; n       else n         r = mid - 1; n     } n     if (ans != 0) n       t = (Ax - b[ans]) * 2 + By - Ay + Bx - Ax; n     else n       t = INF; n     Min = min(Min, t); n     l = 1, r = ce, ans = 0; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (a[mid] >= Ax) n         r = mid - 1, ans = mid; n       else n         l = mid + 1; n     } n     if (ans != 0 && a[ans] <= Bx) n       t = Bx - Ax + (By - Ay + v - 1) / v; n     else n       t = INF; n     Min = min(Min, t); n     l = 1, r = cl, ans = 0; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (b[mid] >= Ax) n         r = mid - 1, ans = mid; n       else n         l = mid + 1; n     } n     if (ans != 0 && b[ans] <= Bx) n       t = Bx - Ax + By - Ay; n     else n       t = INF; n     Min = min(Min, t); n     l = 1, r = ce, ans = 0; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (a[mid] >= Bx) n         r = mid - 1, ans = mid; n       else n         l = mid + 1; n     } n     if (ans != 0) n       t = (a[ans] - Bx) * 2 + Bx - Ax + (By - Ay + v - 1) / v; n     else n       t = INF; n     Min = min(Min, t); n     l = 1, r = cl, ans = 0; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (b[mid] >= Bx) n         r = mid - 1, ans = mid; n       else n         l = mid + 1; n     } n     if (ans != 0) n       t = (b[ans] - Bx) * 2 + Bx - Ax + By - Ay; n     else n       t = INF; n     Min = min(Min, t); n     printf( %d n , Min); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[300005]; n int l[5005], r[5005]; n long long dp[5005][5005]; n const long long inf = 2e16; n void Min(long long &a, long long b) { n   if (a > b) a = b; n } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   int cur = 0, tmp = n / k; n   int c1 = n - tmp * k, c2 = k - c1; n   for (int i = 0; i < 5005; i++) n     for (int j = 0; j < 5005; j++) dp[i][j] = inf; n   for (int i = 1; i <= k; i++) { n     if (c1 < i) n       r[i] = min(n, c1 * (tmp + 1) + (i - c1) * tmp); n     else n       r[i] = min(n, i * (tmp + 1)); n     if (c2 < i) n       l[i] = min(n, c2 * tmp + (i - c2) * (tmp + 1)); n     else n       l[i] = min(n, i * tmp); n   } n   dp[0][0] = 0; n   for (int i = 1; i <= k; i++) n     for (int j = l[i]; j <= r[i]; j++) { n       int pre = j - tmp; n       if (pre <= r[i - 1] && pre >= l[i - 1]) n         Min(dp[i][j - l[i]], dp[i - 1][pre - l[i - 1]] + a[j] - a[pre + 1]); n       if (pre - 1 <= r[i - 1] && pre - 1 >= l[i - 1]) n         Min(dp[i][j - l[i]], dp[i - 1][pre - 1 - l[i - 1]] + a[j] - a[pre]); n     } n   printf( %d n , dp[k][n - l[k]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200010]; n int a[200010]; n int ans[50]; n int n, k, x; n void init() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   for (int i = 1; i <= n; i++) a[i] = (int)s[i] -  a  + 1; n   a[++n] = 30; n   for (int i = 1; i <= 26; i++) { n     int l; n     bool flag = 0; n     for (int j = 1; j <= n; j++) { n       if (flag == 0 & a[j] == i) { n         l = j; n         flag = 1; n       } n       if (flag && a[j] != i) { n         ans[i] += (j - l) / k; n         flag = 0; n       } n     } n   } n } n int main() { n   init(); n   for (int i = 1; i <= 26; i++) x = max(ans[i], x); n   cout << x; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int f, a, b; n   scanf( %d , &f); n   scanf( %d:%d , &a, &b); n   if (b > 59) b %= 10; n   if (a > f or a == 24) a %= 10; n   if (a == 0 and f == 12) a = 10; n   printf( %02d:%02d , a, b); n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, s, t; n int main() { n   cin >> T; n   while (T--) { n     cin >> n >> s >> t; n     cout << max(1, max(n - s + 1, n - t + 1)) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n long long int gcd(long long int a, long long int b) { n   if (a < b) n     return gcd(b, a); n   else if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n long long int isPrime(long long int n) { n   long long int p = (long long int)sqrt(n); n   for (int i = (2); i <= (p); i += (1)) n     if (n % i == 0) return 0; n   return 1; n } n long long int pow(long long int b, long long int e) { n   if (e == 0) n     return 1; n   else if (e % 2 == 0) { n     long long int a = pow(b, e / 2); n     return a * a; n   } else { n     long long int a = pow(b, e / 2); n     return b * a * a; n   } n } n long long int powm(long long int x, long long int y, n                    long long int m = 1000000007) { n   x = x % m; n   long long int res = 1; n   while (y) { n     if (y & 1) res = res * x; n     res %= m; n     y = y >> 1; n     x = x * x; n     x %= m; n   } n   return res; n } n long long int modInverse(long long int a, long long int m) { n   return powm(a, m - 2, m); n } n int spf[10000005]; n void sieve() { n   spf[1] = 1; n   for (int i = 2; i < 10000005; i++) spf[i] = i; n   for (int i = 4; i < 10000005; i += 2) spf[i] = 2; n   for (int i = 3; i * i < 10000005; i++) { n     if (spf[i] == i) { n       for (int j = i * i; j < 10000005; j += i) n         if (spf[j] == j) spf[j] = i; n     } n   } n } n void solve() { n   sieve(); n   int n; n   cin >> n; n   vector<int> arr(n); n   vector<pair<long long int, long long int>> ans(n); n   for (int i = (0); i <= (n - 1); i += (1)) { n     cin >> arr[i]; n     int x = arr[i]; n     vector<int> fac; n     while (x != 1) { n       int temp = spf[x]; n       fac.push_back(spf[x]); n       while (x % temp == 0) x /= temp; n     } n     bool poss = 0; n     ans[i] = {-1, -1}; n     if ((int)fac.size() > 1) { n       int a = fac[0], b = 1; n       for (int i = (1); i <= ((int)fac.size() - 1); i += (1)) b *= fac[i]; n       ans[i] = {a, b}; n     } n   } n   for (int i = (0); i <= (n - 1); i += (1)) cout << ans[i].first <<    ; n   cout <<   n ; n   for (int i = (0); i <= (n - 1); i += (1)) cout << ans[i].second <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int LIM = 1e5; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, q, x, p; n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     cin >> x; n     p = (n - x) + x / 2; n     while (x % 2 == 0) { n       x += p; n       p >>= 1; n     } n     cout << (x + 1) / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void operator>>(istream& is, vector<T>& v) { n   for (auto& e : v) { n     is >> e; n   } n } n template <class T> n void operator<<(ostream& os, const vector<T>& v) { n   for (const auto& e : v) { n     os << e <<    ; n   } n   os <<   n ; n } n long long LenToAns(long long len) { return len * (len + 1) / 2; } n struct Node { n   int last; n   int first; n   int pref; n   int suff; n   int inf; n   int size; n   long long ans; n }; n vector<Node> tree; n int n; n int n_full; n Node MakeNode(int v) { return {v, v, 1, 1, 1, 1, 1}; } n Node MakeNeutral() { return {0, 0, 0, 0, 0, 0, 0}; } n Node Merge(const Node& l, const Node& r) { n   Node res; n   res.first = l.first; n   res.last = r.last; n   res.pref = l.pref; n   res.suff = r.suff; n   res.inf = max(l.inf, r.inf); n   res.ans = l.ans + r.ans; n   if (l.last <= r.first) { n     res.inf = max(res.inf, l.suff + r.pref); n     res.ans += LenToAns(l.suff + r.pref); n     res.ans -= LenToAns(l.suff); n     res.ans -= LenToAns(r.pref); n     if (l.pref == l.size) { n       res.pref = l.size + r.pref; n     } n     if (r.pref == r.size) { n       res.suff = l.suff + r.size; n     } n   } n   res.size = l.size + r.size; n   return res; n } n void Init(const vector<int>& a) { n   n_full = 1; n   while (n_full < n) { n     n_full *= 2; n   } n   tree.resize(n_full * 2 - 1); n   for (int i = 0; i < n; i++) { n     tree[n_full + i - 1] = MakeNode(a[i]); n   } n   for (int i = n_full - 2; i >= 0; i--) { n     tree[i] = Merge(tree[2 * i + 1], tree[2 * i + 2]); n   } n } n void Set(int i, int v, int xi, int xl, int xr) { n   if (xl > i || i >= xr) { n     return; n   } n   if (xr - xl == 1) { n     tree[xi] = MakeNode(v); n     return; n   } n   int xm = (xl + xr) / 2; n   Set(i, v, xi * 2 + 1, xl, xm); n   Set(i, v, xi * 2 + 2, xm, xr); n   tree[xi] = Merge(tree[xi * 2 + 1], tree[xi * 2 + 2]); n } n void Set(int i, int v) { Set(i, v, 0, 0, n_full); } n Node Sum(int l, int r, int xi, int xl, int xr) { n   if (xl >= r || l >= xr) { n     return MakeNeutral(); n   } n   if (l <= xl && xr <= r) { n     return tree[xi]; n   } n   int xm = (xl + xr) / 2; n   return Merge(Sum(l, r, xi * 2 + 1, xl, xm), Sum(l, r, xi * 2 + 2, xm, xr)); n } n long long Sum(int l, int r) { n   Node res = Sum(l, r, 0, 0, n_full); n   return res.ans; n } n int main() { n   int k; n   cin >> n >> k; n   vector<int> a(n); n   cin >> a; n   Init(a); n   while (k--) { n     int q; n     cin >> q; n     if (q == 1) { n       int i, v; n       cin >> i >> v; n       i--; n       Set(i, v); n     } else { n       int l, r; n       cin >> l >> r; n       l--; n       cout << Sum(l, r) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n; n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     long long ls, mi = 0; n     bool pd = false; n     cin >> ls; n     for (int i = 2; i <= n; i++) { n       long long x, lg; n       cin >> x; n       if (x < ls) { n         lg = int(floor(log2(ls - x))); n         mi = max(mi, lg); n         pd = true; n       } n       ls = max(ls, x); n     } n     if (!pd) n       cout << 0 << endl; n     else n       cout << mi + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int p[100000]; n int c[100000]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> p[i]; n     --p[i]; n   } n   vector<long long int> loops; n   for (int i = 0; i < n; ++i) { n     if (c[i]) continue; n     int j = i; n     int q = 0; n     while (c[j] == 0) { n       c[j] = 1; n       j = p[j]; n       ++q; n     } n     loops.push_back(q); n   } n   sort(loops.begin(), loops.end()); n   if (loops.size() > 1) { n     loops[loops.size() - 2] += loops[loops.size() - 1]; n     loops.pop_back(); n   } n   long long int ans = 0; n   for (int i = 0; i < loops.size(); ++i) { n     ans += loops[i] * loops[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ss[100010]; n string s; n string s1; n pair<int, int> b[2000000]; n int m[100100]; n unsigned long long h[100100]; n unsigned long long p[100100]; n int main() { n   scanf( %s , &ss); n   s = (string)ss; n   h[0] = 0; n   p[0] = 1; n   for (int i = (1); i < (100100); ++i) p[i] = p[i - 1] * 257; n   for (int i = (0); i < (s.size()); ++i) n     h[i + 1] = h[i] + (unsigned int)s[i] * p[i]; n   int n; n   cin >> n; n   int sz = 0; n   for (int it = (0); it < (n); ++it) { n     cin >> s1; n     unsigned long long h1 = 0; n     for (int i = (0); i < (s1.size()); ++i) h1 += (unsigned int)s1[i] * p[i]; n     int pp = (int)(s.size()) - (int)s1.size(); n     for (int i = (0); i < (pp + 1); ++i) { n       if (h1 * p[i] == h[i + s1.size()] - h[i]) { n         b[sz] = make_pair(i, i + s1.size() - 1); n         sz++; n       } n     } n   } n   for (int i = (0); i < (s.size()); ++i) m[i] = (int)s.size(); n   for (int i = (0); i < (sz); ++i) n     m[b[i].first] = n         ((m[b[i].first] < b[i].second) ? (m[b[i].first]) : (b[i].second)); n   for (int i = (int)s.size() - 2; i >= 0; --i) n     m[i] = ((m[i] < m[i + 1]) ? (m[i]) : (m[i + 1])); n   int res = 0; n   int res1 = 0; n   for (int i = (0); i < (s.size()); ++i) { n     int len = m[i] - i; n     if (len > res) { n       res = len; n       res1 = i; n     } n   } n   printf( %d %d n , res, res1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long dp[2005][2005], m, d, ans, bit[2005], len; n char l[2005], r[2005]; n long long dfs(int pos, long long sum, bool flag) { n   if (pos == -1) return sum == 0; n   if (!flag && dp[pos][sum] != -1) return dp[pos][sum]; n   long long res = 0; n   for (int i = 0, up = flag ? bit[pos] : 9; i <= up; ++i) { n     if ((len - pos) & 1) { n       if (i == d) continue; n     } else { n       if (i != d) continue; n     } n     (res += dfs(pos - 1, (sum * 10 + i) % m, flag && (i == up))) %= MOD; n   } n   return flag ? res : dp[pos][sum] = res; n } n long long calc(char *x) { n   len = strlen(x); n   for (int i = 0; i < len; ++i) bit[len - i - 1] = x[i] -  0 ; n   return dfs(len - 1, 0, 1); n } n int check() { n   long long sum = 0; n   len = strlen(l); n   for (int i = 0; i < len; ++i) { n     int x = l[i] -  0 ; n     if ((i + 1) & 1) { n       if (x == d) return 0; n     } else { n       if (x != d) return 0; n     } n     (sum = sum * 10 + x) %= m; n   } n   return sum == 0; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   scanf( %lld%lld , &m, &d); n   scanf( %s , l); n   scanf( %s , r); n   ans = calc(r) - calc(l); n   ans += check(); n   printf( %lld n , (ans + MOD) % MOD); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long int m, a[1000], b, sum = 0; n     cin >> m >> b; n     for (int j = 0; j < m; j++) { n       cin >> a[j]; n     } n     for (int j = 0; j < m; j++) { n       sum = sum + a[j]; n     } n     if (sum == b) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.141592653589793238463; n void printVec(vector<int> v1) { n   for (int i = 0; i < v1.size(); i++) cout << v1[i] <<    ; n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cout.tie(NULL); n   cin.tie(NULL); n   priority_queue<long long, vector<long long>, greater<long long>> pQ; n   long long n; n   cin >> n; n   string s; n   long long opr; n   vector<string> vec; n   vector<long long> operations; n   for (long long i = 0; i < n; i++) { n     cin >> s; n     vec.push_back(s); n     if (s ==  removeMin ) { n       operations.push_back(-1); n       continue; n     } n     cin >> opr; n     operations.push_back(opr); n   } n   vector<string> vec_Out; n   vector<long long> operations_Out; n   for (long long i = 0; i < n; i++) { n     if (vec[i] ==  removeMin ) { n       if (pQ.empty()) { n         pQ.push(0); n         vec_Out.push_back( insert  ); n         operations_Out.push_back(0); n       } n       pQ.pop(); n       vec_Out.push_back( removeMin ); n       operations_Out.push_back(-1); n     } else if (vec[i][0] ==  i ) { n       vec_Out.push_back( insert  ); n       operations_Out.push_back(operations[i]); n       pQ.push(operations[i]); n     } else { n       opr = operations[i]; n       long long MIN = -2; n       if (!pQ.empty()) { n         MIN = pQ.top(); n       } n       while (opr != MIN && !pQ.empty() && opr > MIN) { n         pQ.pop(); n         if (!pQ.empty()) { n           MIN = pQ.top(); n         } n         vec_Out.push_back( removeMin ); n         operations_Out.push_back(-1); n       } n       if (pQ.empty() || opr < MIN) { n         pQ.push(opr); n         vec_Out.push_back( insert  ); n         operations_Out.push_back(opr); n       } n       vec_Out.push_back( getMin  ); n       operations_Out.push_back(opr); n     } n   } n   cout << vec_Out.size() << endl; n   for (long long i = 0; i < vec_Out.size(); i++) { n     if (vec_Out[i] ==  removeMin ) { n       cout << vec_Out[i] << endl; n       continue; n     } n     cout << vec_Out[i] << operations_Out[i] << endl; n   } n   vec_Out.clear(); n   vec.clear(); n   operations.clear(); n   operations_Out.clear(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200001; n int n; n int d[maxn], anc[maxn][18], p[maxn]; n array<int, 2> tree[maxn * 4]; n int jua(int x, int y) { n   int dd = d[y] - d[x]; n   for (int i = 17; i >= 0; i--) { n     if (dd >= 1 << i) { n       y = anc[y][i]; n       dd -= (1 << i); n     } n   } n   return x == y; n } n int lca(int x, int y) { n   if (d[x] < d[y]) swap(x, y); n   int dd = d[x] - d[y]; n   for (int i = 17; i >= 0; i--) { n     if (dd >= 1 << i) { n       x = anc[x][i]; n       dd -= (1 << i); n     } n   } n   if (x == y) return x; n   for (int i = 17; i >= 0; i--) { n     if (anc[x][i] != anc[y][i]) { n       x = anc[x][i]; n       y = anc[y][i]; n     } n   } n   return anc[x][0]; n } n void cmb2(array<int, 2> &p0, int x) { n   if (p0[0] == -1 || x == -1) { n     p0[0] = -1; n     p0[1] = -1; n     return; n   } n   if (d[p0[0]] > d[p0[1]]) swap(p0[0], p0[1]); n   if (jua(p0[0], p0[1])) { n     if (d[x] >= d[p0[0]] && d[x] <= d[p0[1]] && jua(x, p0[1])) { n       return; n     } n     if (jua(p0[1], x)) { n       p0[1] = x; n       return; n     } n     int c = lca(p0[1], x); n     if (d[c] > d[p0[0]]) { n       p0[0] = p0[1] = -1; n       return; n     } n     p0[0] = x; n   } else { n     int c = lca(p0[0], p0[1]); n     if (d[x] >= d[c] && (jua(x, p0[0]) || jua(x, p0[1]))) { n       return; n     } n     if (jua(p0[0], x)) { n       p0[0] = x; n       return; n     } n     if (jua(p0[1], x)) { n       p0[1] = x; n       return; n     } n     p0[0] = p0[1] = -1; n   } n   return; n } n array<int, 2> cmb(array<int, 2> p1, array<int, 2> p2) { n   cmb2(p1, p2[0]); n   cmb2(p1, p2[1]); n   return p1; n } n void update(int v, array<int, 2> pp, int k, int l, int r) { n   if (l == r) { n     tree[k] = pp; n     return; n   } n   int mid = (l + r) / 2; n   if (v <= mid) n     update(v, pp, k * 2, l, mid); n   else n     update(v, pp, k * 2 + 1, mid + 1, r); n   tree[k] = cmb(tree[k * 2], tree[k * 2 + 1]); n } n int query(int k, int l, int r, array<int, 2> p) { n   array<int, 2> t = tree[k]; n   if (l) t = cmb(t, p); n   if (t[0] != -1) { n     return r + 1; n   } n   if (l == r) return l; n   int mid = (l + r) / 2; n   array<int, 2> tt = tree[k * 2]; n   if (l) tt = cmb(tt, p); n   if (tt[0] != -1) n     return query(k * 2 + 1, mid + 1, r, tt); n   else n     return query(k * 2, l, mid, p); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n   } n   memset(anc[0], -1, sizeof(anc[0])); n   anc[1][0] = -1; n   d[1] = 1; n   for (int i = 1; i < n; i++) { n     int x; n     cin >> x, x--; n     anc[i][0] = x; n     d[i] = d[x] + 1; n   } n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < 17; j++) { n       if (anc[i][j] != -1) n         anc[i][j + 1] = anc[anc[i][j]][j]; n       else n         anc[i][j + 1] = -1; n     } n   } n   for (int i = 0; i < n; i++) { n     update(p[i], {i, i}, 1, 0, n - 1); n   } n   int q; n   cin >> q; n   while (q--) { n     int ju; n     cin >> ju; n     if (ju == 1) { n       int x, y; n       cin >> x >> y; n       x--, y--; n       swap(p[x], p[y]); n       update(p[x], {x, x}, 1, 0, n - 1); n       update(p[y], {y, y}, 1, 0, n - 1); n     } else { n       cout << query(1, 0, n - 1, {}) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int n, m; n string A, B; n int f[405][405]; n int main() { n   int i, j, k; n   cin >> t; n   while (t--) { n     cin >> A >> B; n     n = A.length(), m = B.length(); n     A =  0  + A; n     B =  0  + B; n     i = 1, j = 1; n     for (i = 1; i <= n; i++) { n       if (A[i] == B[j]) { n         j++; n         if (j == m + 1) break; n       } n     } n     if (j == m + 1) { n       cout <<  YES  << endl; n       continue; n     } n     bool flag = false; n     for (k = 1; k < n; k++) { n       memset(f, 0, sizeof(f)); n       f[0][0] = k; n       for (i = 0; i < n; i++) { n         for (j = 0; j <= k; j++) { n           if (f[i][j] == 0) continue; n           f[i + 1][j] = max(f[i + 1][j], f[i][j]); n           if (j < k and A[i + 1] == B[j + 1]) { n             f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]); n           } n           if (f[i][j] < m and A[i + 1] == B[f[i][j] + 1]) { n             f[i + 1][j] = max(f[i + 1][j], f[i][j] + 1); n           } n         } n       } n       if (f[n][k] == m) { n         flag = true; n         break; n       } n     } n     if (flag) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 1005; n const int inf = 1 << 28; n int n, m, A[nmax][nmax], B[nmax][nmax], C[nmax][nmax]; n char a[nmax][nmax]; n int dx[] = {-1, 0, 0, 1}; n int dy[] = {0, -1, 1, 0}; n deque<pair<int, int> > q; n bitset<nmax> inq[nmax]; n void go(int x, int y, int d[nmax][nmax]) { n   for (int i = 1; i <= n; i++) inq[i] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) d[i][j] = inf; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] == a[x][y]) { n         d[i][j] = 0; n         q.push_back(make_pair(i, j)); n         inq[i][j] = 1; n       } n   while (!q.empty()) { n     x = q.front().first; n     y = q.front().second; n     q.pop_front(); n     inq[x][y] = 0; n     for (int k = 0; k < 4; k++) { n       int newx = x + dx[k]; n       int newy = y + dy[k]; n       int cost; n       if (a[newx][newy] >=  1  && a[newx][newy] <=  3 ) n         cost = 0; n       else if (a[newx][newy] ==  . ) n         cost = 1; n       else n         cost = inf; n       if (d[x][y] + cost < d[newx][newy]) { n         d[newx][newy] = d[x][y] + cost; n         if (!inq[newx][newy]) { n           q.push_back(make_pair(newx, newy)); n           inq[newx][newy] = 1; n         } n       } n     } n   } n } n int main() { n   cin.sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> (a[i] + 1); n   bool foundA = 0; n   bool foundB = 0; n   bool foundC = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) n       if (a[i][j] ==  1  && !foundA) { n         go(i, j, A); n         foundA = 1; n       } else if (a[i][j] ==  2  && !foundB) { n         go(i, j, B); n         foundB = 1; n       } else if (a[i][j] ==  3  && !foundC) { n         go(i, j, C); n         foundC = 1; n       } n   } n   int sol = inf; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int cost; n       if (a[i][j] ==  1  || a[i][j] ==  2  || a[i][j] ==  3 ) n         cost = 0; n       else if (a[i][j] ==  . ) n         cost = 2; n       sol = min(sol, A[i][j] + B[i][j] + C[i][j] - cost); n     } n   if (sol >= inf) n     cout << -1; n   else n     cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string solve1(string n) { n   string res(n.length(),  9 ); n   for (char c =  8 ; c >=  0 ; c--) { n     string t(n.length(), c); n     if (t >= n) res = t; n   } n   return res; n } n string solve2(string n) { n   string res = solve1(n); n   for (char a =  0 ; a <=  9 ; a++) n     for (char b = a + 1; b <=  9 ; b++) { n       bool n_ok = true; n       for (int i = 0; i < n.length(); i++) { n         if (n[i] < b) { n           string t = n; n           if (t[i] < a) n             t[i] = a; n           else n             t[i] = b; n           for (int j = i + 1; j < n.length(); j++) t[j] = a; n           if (res > t) res = t; n         } n         if (n[i] != a && n[i] != b) { n           n_ok = false; n           break; n         } n       } n       if (n_ok) return n; n     } n   return res; n } n string solve() { n   string n; n   int k; n   cin >> n >> k; n   if (k == 1) n     return solve1(n); n   else n     return solve2(n); n } n int main() { n   int t; n   cin >> t; n   while (t--) cout << solve() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n int main() { n   ll t; n   cin >> t; n   while (t--) { n     ll cnt3, cnt2; n     cnt2 = cnt3 = 0; n     ll n; n     cin >> n; n     while (n % 2 == 0) { n       n = n / 2; n       cnt2++; n     } n     while (n % 3 == 0) { n       n = n / 3; n       cnt3++; n     } n     if (n != 1 || cnt2 > cnt3) n       cout << -1 <<   n ; n     else { n       ll ans = cnt2 + (cnt3 - cnt2) * 2; n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, n, mx, mn, p1, p2, poz, poz1, poz2, k, mx1, mx2, m, mij, u, x, y, z, q; n struct para { n   int x, y, z, ind; n } a[100007]; n int cmp(para k1, para k2) { n   if (k1.x > k2.x) n     return 0; n   else if (k1.x == k2.x) { n     if (k1.y > k2.y) n       return 0; n     else if (k1.y == k2.y) { n       if (k1.z > k2.z) n         return 0; n       else if (k1.z == k2.z) { n         return 0; n       } n       return 1; n     } n     return 1; n   } n   return 1; n } n int main() { n   scanf( %d , &n); n   mx = -1; n   for (i = 1; i <= n; i++) { n     scanf( %d%d%d , &a[i].x, &a[i].y, &a[i].z); n     a[i].ind = i; n     mn = a[i].x; n     if (mn > a[i].y) mn = a[i].y; n     if (mn > a[i].z) mn = a[i].z; n     if (mx < mn) { n       mx = mn; n       k = 1; n       poz = a[i].ind; n     } n     m = max(max(a[i].x, a[i].y), a[i].z); n     mij = a[i].x + a[i].y + a[i].z - m - mn; n     a[i].x = mn; n     a[i].y = mij; n     a[i].z = m; n   } n   sort(a + 1, a + n + 1, cmp); n   for (i = 1; i <= n; i++) { n     mx1 = -1; n     mx2 = -1; n     u = 0; n     while (a[i].x == a[i + 1].x && a[i].y == a[i + 1].y) { n       u = 1; n       if (mx1 < a[i].z) { n         mx2 = mx1; n         poz2 = poz1; n         mx1 = a[i].z; n         poz1 = a[i].ind; n       } else if (mx2 < a[i].z) { n         mx2 = a[i].z; n         poz2 = a[i].ind; n       } n       i++; n     } n     if (u == 1) { n       if (mx1 < a[i].z) { n         mx2 = mx1; n         poz2 = poz1; n         mx1 = a[i].z; n         poz1 = a[i].ind; n       } else if (mx2 < a[i].z) { n         mx2 = a[i].z; n         poz2 = a[i].ind; n       } n     } n     q = min(min(a[i].x, a[i].y), mx1 + mx2); n     if (mx < q) { n       mx = q; n       k = 2; n       p1 = poz1; n       p2 = poz2; n     } n   } n   for (i = 1; i <= n; i++) { n     x = a[i].x; n     y = a[i].y; n     z = a[i].z; n     a[i].x = y; n     a[i].y = z; n     a[i].z = x; n   } n   sort(a + 1, a + n + 1, cmp); n   for (i = 1; i <= n; i++) { n     mx1 = -1; n     mx2 = -1; n     u = 0; n     while (a[i].x == a[i + 1].x && a[i].y == a[i + 1].y) { n       u = 1; n       if (mx1 < a[i].z) { n         mx2 = mx1; n         poz2 = poz1; n         mx1 = a[i].z; n         poz1 = a[i].ind; n       } else if (mx2 < a[i].z) { n         mx2 = a[i].z; n         poz2 = a[i].ind; n       } n       i++; n     } n     if (u == 1) { n       if (mx1 < a[i].z) { n         mx2 = mx1; n         poz2 = poz1; n         mx1 = a[i].z; n         poz1 = a[i].ind; n       } else if (mx2 < a[i].z) { n         mx2 = a[i].z; n         poz2 = a[i].ind; n       } n     } n     q = min(min(a[i].x, a[i].y), mx1 + mx2); n     if (mx < q) { n       mx = q; n       k = 2; n       p1 = poz1; n       p2 = poz2; n     } n   } n   for (i = 1; i <= n; i++) { n     x = a[i].x; n     y = a[i].y; n     z = a[i].z; n     a[i].x = y; n     a[i].y = z; n     a[i].z = x; n   } n   sort(a + 1, a + n + 1, cmp); n   for (i = 1; i <= n; i++) { n     mx1 = -1; n     mx2 = -1; n     u = 0; n     while (a[i].x == a[i + 1].x && a[i].y == a[i + 1].y) { n       u = 1; n       if (mx1 < a[i].z) { n         mx2 = mx1; n         poz2 = poz1; n         mx1 = a[i].z; n         poz1 = a[i].ind; n       } else if (mx2 < a[i].z) { n         mx2 = a[i].z; n         poz2 = a[i].ind; n       } n       i++; n     } n     if (u == 1) { n       if (mx1 < a[i].z) { n         mx2 = mx1; n         poz2 = poz1; n         mx1 = a[i].z; n         poz1 = a[i].ind; n       } else if (mx2 < a[i].z) { n         mx2 = a[i].z; n         poz2 = a[i].ind; n       } n     } n     q = min(min(a[i].x, a[i].y), mx1 + mx2); n     if (mx < q) { n       mx = q; n       k = 2; n       p1 = poz1; n       p2 = poz2; n     } n   } n   printf( %d n , k); n   if (k == 1) n     printf( %d n , poz); n   else { n     printf( %d %d n , p1, p2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   scanf( %d%d , &a, &b); n   if (a < b) { n     for (int i = b + 1; i <= (a + b + 1); i++) printf( %d  , i); n     for (int j = b; j >= 1; j--) printf( %d  , j); n   } else { n     for (int i = 1; i <= a; i++) printf( %d  , i); n     for (int j = (a + b + 1); j > a; j--) printf( %d  , j); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n void qread(int &x) { n   int neg = 1; n   x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = 10 * x + c -  0 , c = getchar(); n   x *= neg; n } n const int maxn = 100005; n int n; n char c[maxn]; n int dp[maxn][2][3]; n int main() { n   scanf( %d%s , &n, c); n   memset(dp, -1, sizeof(dp)); n   dp[0][0][0] = dp[0][1][0] = 0; n   for (int(i) = 0; (i) < n; i++) n     for (int(j) = 0; (j) < 2; j++) n       for (int(k) = 0; (k) < 3; k++) { n         int cur = dp[i][j][k]; n         if (cur < 0) continue; n         dp[i + 1][j][k] = max(dp[i + 1][j][k], cur); n         if (c[i] -  0  != j) n           dp[i + 1][c[i] -  0 ][k] = max(dp[i + 1][c[i] -  0 ][k], cur + 1); n         else if (k < 2) n           dp[i + 1][c[i] -  0 ][k + 1] = n               max(dp[i + 1][c[i] -  0 ][k + 1], cur + 1); n       } n   int ans = 0; n   for (int(j) = 0; (j) < 2; j++) n     for (int(k) = 0; (k) < 3; k++) ans = max(ans, dp[n][j][k]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, cnt, flag = 1; n int a[N], b[N], c[N]; n int main() { n   cnt = n = read(); n   a[0] = -1, b[0] = 0; n   while (cnt--) { n     for (int i = 0; i < N - 1; i++) c[i + 1] = a[i]; n     c[0] = 0; n     for (int i = 0; i < N; i++) c[i] += b[i]; n     for (int i = 0; i < N; i++) n       if (abs(c[i]) > 1) { n         for (int i = 0; i < N - 1; i++) c[i + 1] = a[i] * (-1); n         c[0] = 0; n         for (int i = 0; i < N; i++) c[i] += b[i]; n         break; n       } n     for (int i = 0; i < N; i++) b[i] = a[i], a[i] = c[i]; n   } n   cout << n << endl; n   if (a[n] < 0) n     flag = -1; n   else n     flag = 1; n   for (int i = 0; i <= n; i++) cout << a[i] * flag <<    ; n   cout << endl << n - 1 << endl; n   if (b[n - 1] < 0) n     flag = -1; n   else n     flag = 1; n   for (int i = 0; i <= n - 1; i++) cout << b[i] * flag <<    ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ) n using namespace std; n const long long N = 1e6 + 5; n void pairsort(long long a[], long long b[], long long n) { n   pair<long long, long long> pairt[n]; n   for (long long i = 0; i < n; i++) { n     pairt[i].first = a[i]; n     pairt[i].second = b[i]; n   } n   sort(pairt, pairt + n); n   for (long long i = 0; i < n; i++) { n     a[i] = pairt[i].first; n     b[i] = pairt[i].second; n   } n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long isPrime(long long n) { n   if (n < 2) return 0; n   if (n < 4) return 1; n   if (n % 2 == 0 or n % 3 == 0) return 0; n   for (long long i = 5; i * i <= n; i += 6) n     if (n % i == 0 or n % (i + 2) == 0) return 0; n   return 1; n } n long long C(long long n, long long r) { n   if (r > n - r) r = n - r; n   long long ans = 1; n   for (long long i = 1; i <= r; i++) { n     ans *= n - r + i; n     ans /= i; n   } n   return ans; n } n long long mod = 1e9 + 7; n long long modexpo(long long x, long long p) { n   long long res = 1; n   x = x % mod; n   while (p) { n     if (p % 2) res = res * x; n     p >>= 1; n     x = x * x % mod; n     res %= mod; n   } n   return res; n } n long long n, t, a[N], b[N], c[N], d[N], x[N]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> t; n   a[n + 1] = 3000000000000000000 - t + 1; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 1; i <= n; i++) cin >> x[i]; n   for (long long i = 1; i <= n; i++) n     if (x[i] < x[i - 1] or x[i] < i) return cout <<  No , 0; n   for (long long i = 1; i <= n; i++) b[i] = a[x[i] + 1] + t - 1; n   for (long long i = n; i > 1; i--) n     if (b[i - 1] >= b[i]) b[i - 1] = b[i] - 1; n   for (long long i = 1; i <= n; i++) c[i + 1]++, c[x[i] + 1]--, d[x[i] + 1]++; n   for (long long i = 1; i <= n; i++) c[i] += c[i - 1]; n   for (long long i = 1; i <= n; i++) n     if (c[i] and a[i] + t > b[i - 1]) return cout <<  No , 0; n   for (long long i = 1; i <= n; i++) n     if (b[i] < a[i] + t) return cout <<  No , 0; n   for (long long i = 1; i <= n; i++) n     if (c[i] and d[i]) return cout <<  No , 0; n   cout <<  Yes  n        <<   n ; n   for (long long i = 1; i <= n; i++) cout << b[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const long long maxn = (long long)1e6 + 6; n const long double EPS = 1e-9; n const long long INF = (long long)1e18 + 18; n const long long mod = (long long)1e9 + 9; n long long n, m, k; n inline long long add(long long x, long long y) { n   x += y; n   if (x >= mod) x -= mod; n   return x; n } n inline long long sub(long long x, long long y) { n   x -= y; n   if (x < 0) x += mod; n   return x; n } n inline long long mul(long long x, long long y) { return (x * 1LL * y) % mod; } n inline long long binpow(long long a, long long b) { n   long long x = 1 % mod; n   while (b) { n     if (b & 1) x = mul(x, a); n     a = mul(a, a); n     b >>= 1; n   } n   return x; n } n inline long long inv(long long a) { return binpow(a, mod - 2); } n bool works(long long x) { n   long long t = x * k + ((n - x * k) / k) * (k - 1) + (n - x * k) % k; n   return t >= m; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   long long left = 0, right = (n + k - 1) / k; n   while (left < right) { n     long long mid = (left + right) >> 1; n     if (works(mid)) { n       right = mid; n     } else { n       left = mid + 1; n     } n   } n   long long ans = binpow(2, left + 1); n   ans = sub(ans, 2); n   ans = mul(ans, k); n   ans = add(ans, sub(m, left * k)); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, u, r; n long long a[40], b[40], k[40], p[40]; n long long maxx = (long long)1 << 63; n long long eval() { n   long long val = 0; n   for (int i = 0; i < n; i++) { n     val += a[i] * k[i]; n   } n   return val; n } n void gen(int now, int last) { n   if (now % 2 == u % 2) maxx = max(maxx, eval()); n   if (now < u) { n     long long temp[40]; n     memcpy(temp, a, sizeof(a)); n     if (last == 1) { n       for (int i = 0; i < n; i++) { n         a[i] = temp[i] ^ b[i]; n       } n       gen(now + 1, 0); n     } n     for (int i = 0; i < n; i++) { n       a[i] = temp[p[i]] + r; n     } n     gen(now + 1, 1); n   } n } n int main() { n   cin >> n >> u >> r; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> k[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n     p[i]--; n   } n   gen(0, 1); n   cout << maxx << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e7 + 5; n int n, pos[N], ans1, ans2; n long long ans = 1e14; n int main() { n   scanf( %d , &n); n   for (int i = 1, x; i <= n; ++i) { n     scanf( %d , &x); n     if (pos[x]) { n       if (x < ans) ans1 = pos[x], ans2 = i, ans = x; n     } else n       pos[x] = i; n   } n   for (int i = 1, tmp1, tmp2; i < N; ++i) { n     if (i >= ans) break; n     tmp1 = tmp2 = 0; n     for (int j = i; j < N; j += i) { n       if (!pos[j]) continue; n       if (!tmp1) n         tmp1 = j, tmp2 = pos[j]; n       else { n         if (1LL * j / i * tmp1 < ans) { n           ans = 1LL * j / i * tmp1, ans1 = pos[j], ans2 = tmp2; n           break; n         } n       } n     } n   } n   if (ans1 > ans2) ans1 ^= ans2 ^= ans1 ^= ans2; n   printf( %d %d n , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long arr[200010], tot = 0; n int main(void) { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (i > 0) n       tot += abs(arr[i] - arr[i - 1]); n     else n       tot += abs(arr[i]); n   } n   cout << tot; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void swap(long long int &x, long long int &y) { n   x = x ^ y; n   y = x ^ y; n   x = x ^ y; n } n template <typename T> n void print(T a) { n   for (auto i = a.begin(); i != a.end(); i++) cout << *i <<    ; n   cout << endl; n } n bool isVowel(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ) return true; n   return false; n } n void sync() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   sync(); n   long long int n; n   cin >> n; n   map<long long int, long long int> ck1, ck2; n   map<pair<long long int, long long int>, long long int> ck3; n   while (n--) { n     long long int x, y; n     cin >> x >> y; n     ck1[x]++; n     ck2[y]++; n     ck3[{x, y}]++; n   } n   long long int ans = 0; n   for (auto i = ck1.begin(); i != ck1.end(); i++) { n     long long int x = i->second; n     ans += ((x) * (x - 1) / 2); n   } n   for (auto i = ck2.begin(); i != ck2.end(); i++) { n     long long int x = i->second; n     ans += ((x) * (x - 1) / 2); n   } n   for (auto i = ck3.begin(); i != ck3.end(); i++) { n     long long int x = i->second; n     ans -= ((x) * (x - 1) / 2); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int k = 0; n   int cnt = 0; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int p; n     cin >> p; n     if (p > k) n       cnt++; n     else n       cnt = 1; n     k = p; n     ans = max(cnt, ans); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n namespace solution {} n namespace solution { n class ISolution { n  public: n   virtual void init(){}; n   virtual bool input() { return false; }; n   virtual void output(){}; n   virtual int run() = 0; n }; n }  // namespace solution n namespace solution { n using namespace std; n const long long MOD = 1000000000LL; n const int SIZE = 2 * 100000 + 11; n const int QUERY_SIZE = 2 * 100000 + 11; n int n, m; n long long A[SIZE]; n long long F[SIZE]; n void query_1() { n   int x, v; n   cin >> x >> v; n   A[x] = v; n } n void query_2() { n   int l, r; n   cin >> l >> r; n   int len = r - l + 1; n   long long sum = 0; n   for (int i = 0; i < len; ++i) sum = (sum + F[i] * A[l + i]) % MOD; n   cout << sum << endl; n } n void solve() { n   F[0] = F[1] = 1; n   for (int i = 2; i < SIZE; ++i) F[i] = (F[i - 1] + F[i - 2]) % MOD; n   for (int i = 0; i < m; ++i) { n     int t; n     cin >> t; n     if (t == 1) { n       query_1(); n     } else if (t == 2) { n       query_2(); n     } n   } n } n class Solution : public ISolution { n  public: n   bool input() { n     if (!(cin >> n >> m)) return false; n     for (int i = 0; i < n; ++i) cin >> A[i + 1]; n     return true; n   } n   int run() { n     while (init(), input()) { n       solve(); n       output(); n     } n     return 0; n   } n }; n }  // namespace solution n int main() { return solution::Solution().run(); } n


#include <bits/stdc++.h> n using namespace std; n double x1, yy1, x2, y2, v, t, vx, vy, wx, wy, l, r, mid, x, y, dis; n int main() { n   scanf( %lf%lf , &x1, &yy1); n   scanf( %lf%lf , &x2, &y2); n   scanf( %lf%lf , &v, &t); n   scanf( %lf%lf , &vx, &vy); n   scanf( %lf%lf , &wx, &wy); n   l = 0.0; n   r = 1e9; n   for (int i = 1; i <= 1000; ++i) { n     mid = (l + r) / 2; n     x = x2 - x1 - min(t, mid) * vx - max(0.0, mid - t) * wx; n     y = y2 - yy1 - min(t, mid) * vy - max(0.0, mid - t) * wy; n     dis = sqrt(x * x + y * y); n     if (v * mid > dis) n       r = mid; n     else n       l = mid; n   } n   printf( %.10lf n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 2; n struct { n   int x; n   int last; n   string it; n } a; n struct kek { n   string w, e, r, t; n }; n map<char, int> bak; n vector<string> st[N + 5][7], q; n vector<pair<string, string> > be, en; n vector<kek> ans; n int n, m; n string ins; n char c; n bool frog = false; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   bak[ u ] = 1; n   bak[ a ] = 2; n   bak[ o ] = 3; n   bak[ e ] = 4; n   bak[ i ] = 5; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> ins; n     m = ins.length(); n     a.x = 0; n     a.last = 0; n     for (int j = 0; j < m; j++) { n       if (ins[j] ==  u  || ins[j] ==  a  || ins[j] ==  o  || ins[j] ==  e  || n           ins[j] ==  i ) { n         a.x++; n         c = ins[j]; n         a.last = bak[c]; n       } n     } n     a.it = ins; n     st[a.x][a.last].push_back(ins); n   } n   for (int i = 1; i <= N; i++) { n     q.clear(); n     for (int j = 1; j <= 5; j++) { n       int k; n       for (k = 1; k < st[i][j].size(); k += 2) { n         en.push_back({st[i][j][k], st[i][j][k - 1]}); n       } n       if (k == st[i][j].size()) { n         q.push_back(st[i][j][k - 1]); n       } n     } n     for (int j = 1; j < q.size(); j += 2) { n       be.push_back({q[j], q[j - 1]}); n     } n   } n   while (!be.empty() && !en.empty()) { n     ans.push_back( n         {be.back().first, en.back().first, be.back().second, en.back().second}); n     en.pop_back(); n     be.pop_back(); n   } n   for (int i = 1; i < en.size(); i += 2) { n     ans.push_back( n         {en[i].first, en[i - 1].first, en[i].second, en[i - 1].second}); n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); ++i) { n     cout << ans[i].w <<     << ans[i].e << endl n          << ans[i].r <<     << ans[i].t << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int N = 300009; n void swap(char *p, char *q) { n   char temp = *p; n   *p = *q; n   *q = temp; n   return; n } n long long int powxy(long long int x, long long int y) { n   if (y == 0) return 1; n   if (y % 2 == 1) return (x * powxy(x, y - 1)) % MOD; n   long long int t = powxy(x, y / 2) % MOD; n   return (t * t) % MOD; n } n vector<pair<int, int> > adj[N]; n int dp[500005]; n vector<int> ans[500001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int ans = 1; n     string s; n     long long int n; n     cin >> n >> s; n     string temp = s; n     for (int i = 0; i < n; i++) { n       if ((i + 1) % 2 != 0) { n         string q = s.substr(0, i); n         if ((n - i + 1) % 2 == 0) reverse(q.begin(), q.end()); n         string p = s.substr(i, n - i) + q; n         if (p < temp) { n           temp = p; n           ans = i + 1; n         } n       } else { n         string q = s.substr(0, i); n         reverse(q.begin(), q.end()); n         if ((n - i + 1) % 2 != 0) reverse(q.begin(), q.end()); n         string p = s.substr(i, n - i) + q; n         if (p < temp) { n           temp = p; n           ans = i + 1; n         } n       } n     } n     cout << temp <<   n ; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &a) { n   register int b = 1, c = getchar(); n   a = 0; n   for (; !isdigit(c); c = getchar()) n     if (c == 45) b *= -1; n   for (; isdigit(c); c = getchar()) a = (a << 3) + (a << 1) + c - 48; n   a *= b; n } n const int maxn = 105; n const int maxm = 1 << 18; n long long dp[maxm][maxn]; n string str; n int len; n int m; n int main() { n   cin >> str; n   len = int((str).size()); n   read(m); n   dp[0][0] = 1; n   for (int msk = 0; msk < int(1 << len); msk++) { n     for (int rem = 0; rem < int(m); rem++) { n       for (int j = 0; j < int(len); j++) n         if (!(msk & (1 << j))) { n           int nwrem = (rem * 10 + str[j] -  0 ) % m; n           int nwmsk = msk | (1 << j); n           if (!msk && str[j] ==  0 ) continue; n           dp[nwmsk][nwrem] += dp[msk][rem]; n         } n     } n   } n   long long ans = dp[(1 << len) - 1][0]; n   for (int i = 0; i < int(10); i++) { n     int cnt = 0; n     for (int j = 0; j < int(len); j++) { n       if (str[j] == i + 48) cnt++; n     } n     long long fac = 1; n     for (int j = 1; j <= cnt; j++) fac *= j; n     ans /= fac; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include<bits/stdc++.h> n #define ll long long int n using namespace std; n  n  int main() n  { n   int t; n   cin>>t; n   while(t--) n   { n    ll a, b, c; n    cin>>a>>b>>c; n     n    if((a+b+c)%9 == 0 && min(a, min(b,c)) >= (a+b+c)/9) n     cout<< YES <<endl; n    else n     cout<< NO <<endl; n   } n  }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   if (k % 2 == 1) { n     cout << 1 << endl; n     return 0; n   } n   long long x = log2(k); n   long long y = pow(2, x) - k; n   while (y != 0) { n     x = log2(y); n     y = pow(2, x) - y; n   } n   cout << x + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1000000007; n const long long N = 1e5; n long long expo(long long x, long long y) { n   long long res = 1; n   x = x % M; n   while (y > 0) { n     if (y & 1) res = (res * x) % M; n     y = y >> 1; n     x = (x * x) % M; n   } n   return (res) % M; n } n string binary(long long n) { n   if (n == 0) return  0 ; n   string s; n   while (n > 0) { n     if (n & 1) n       s +=  1 ; n     else n       s +=  0 ; n     n >>= 1; n   } n   reverse(s.begin(), s.end()); n   return s; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) { n     if (n % i == 0 || n % (i + 2) == 0) return false; n   } n   return true; n } n long long ncr(long long n, long long r) { n   if (r > n - r) r = n - r; n   long long ans = 1; n   for (long long k = 0; k < r; k++) { n     ans *= (n - k); n     ans /= (k + 1); n   } n   return ans; n } n void solve() { n   long long n, d; n   cin >> n >> d; n   if (n >= d) { n     cout <<  YES n ; n     return; n   } n   long long val = n / 2 + ceil(d / (1.0 * (n / 2 + 1))); n   if (val <= n) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   ; n   long long test = 1; n   cin >> test; n   while (test--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = (int)1e9 + 207; n const int N = (int)1e6 + 2; n long long n, m, s, t; n long long cnt1[N], cnt2[N]; n long long D1[N], D2[N]; n vector<pair<long long, long long> > G1[N], G2[N]; n vector<pair<long long, pair<long long, long long> > > edge; n void solve1() { n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq; n   for (int i = 0; i <= n; i++) D1[i] = 1e14; n   D1[s] = 0; n   cnt1[s] = 1; n   pq.push(pair<long long, long long>(D1[s], s)); n   while (!pq.empty()) { n     long long du = pq.top().first; n     long long u = pq.top().second; n     pq.pop(); n     if (du != D1[u]) continue; n     for (int i = 0; i < G1[u].size(); i++) { n       long long v = G1[u][i].first; n       long long w = G1[u][i].second; n       if (D1[v] > du + w) { n         D1[v] = du + w; n         cnt1[v] = cnt1[u]; n         pq.push(pair<long long, long long>(D1[v], v)); n       } else if (D1[v] == du + w) { n         cnt1[v] += cnt1[u]; n       } n       cnt1[v] %= mod; n     } n   } n } n void solve2() { n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq; n   for (int i = 0; i <= n; i++) D2[i] = 1e14; n   D2[t] = 0; n   cnt2[t] = 1; n   pq.push(pair<long long, long long>(D2[t], t)); n   while (!pq.empty()) { n     long long du = pq.top().first; n     long long u = pq.top().second; n     pq.pop(); n     if (du != D2[u]) continue; n     for (int i = 0; i < G2[u].size(); i++) { n       long long v = G2[u][i].first; n       long long w = G2[u][i].second; n       if (D2[v] > du + w) { n         D2[v] = du + w; n         cnt2[v] = cnt2[u]; n         pq.push(pair<long long, long long>(D2[v], v)); n       } else if (D2[v] == du + w) { n         cnt2[v] += cnt2[u]; n       } n       cnt2[v] %= mod; n     } n   } n } n int main() { n   cin >> n >> m >> s >> t; n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     edge.push_back(pair<long long, pair<long long, long long> >( n         w, pair<long long, long long>(u, v))); n     G1[u].push_back(pair<long long, long long>(v, w)); n     G2[v].push_back(pair<long long, long long>(u, w)); n   } n   solve1(); n   solve2(); n   for (int i = 0; i < m; i++) { n     long long u = edge[i].second.first; n     long long v = edge[i].second.second; n     long long w = edge[i].first; n     if (D1[u] + w + D2[v] == D1[t] && n         ((1LL * cnt1[u] * cnt2[v]) % mod) == cnt1[t]) { n       cout <<  YES n ; n     } else if (D1[u] + D2[v] + 1 < D1[t]) { n       cout <<  CAN   << w - (D1[t] - D1[u] - D2[v] - 1) <<   n ; n     } else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, w = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = getchar(); n   } n   while (c <=  9  && c >=  0 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return w == 1 ? x : -x; n } n int r[40][40], v[40][40], n, m, ans = -1e9, bin[40]; n inline int F(int x, int y) { return r[x][y] ? -v[x][y] : v[x][y]; } n inline int query(int x, int y, int d) { n   r[x][y] = d; n   r[x + m][y] = r[m][y] ^ d; n   r[x][y + m] = r[x][m] ^ d; n   r[x + m][y + m] = r[m][y + m] ^ r[x][y + m]; n   return F(x, y) + F(x + m, y) + F(x, y + m) + F(x + m, y + m); n } n inline int query(int y, int d) { n   int res = 0; n   r[m][y] = d; n   r[m][y + m] = r[m][m] ^ d; n   res += F(m, y) + F(m, y + m); n   for (int x = 1; x < m; x++) res += max(query(x, y, 0), query(x, y, 1)); n   return res; n } n inline int calc() { n   int res = 0; n   for (int x = 1; x <= n; x++) res += F(x, m); n   for (int y = 1; y < m; y++) res += max(query(y, 0), query(y, 1)); n   return res; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) v[i][j] = read(); n   m = (n + 1) >> 1; n   ans = -1e9; n   bin[0] = 1; n   for (int i = 1; i <= m + 1; i++) bin[i] = bin[i - 1] << 1; n   for (int zt = 0; zt < bin[m]; zt++) { n     for (int x = 1; x <= m; x++) r[x][m] = zt >> (x - 1) & 1; n     for (int x = 1; x < m; x++) r[x + m][m] = r[m][m] ^ r[x][m]; n     ans = max(ans, calc()); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int maxn = 1e5 + 5; n const int Inf = 1e9 + 7; n int main() { n   int a, b; n   while (cin >> a >> b) { n     if (a == 0 && b == 0) { n       printf( NO n ); n     } else if (abs(a - b) <= 1) { n       printf( YES n ); n     } else { n       printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n inline long long getint() { n   long long _x = 0, _tmp = 1; n   char _tc = getchar(); n   while ((_tc <  0  || _tc >  9 ) && _tc !=  - ) _tc = getchar(); n   if (_tc ==  - ) _tc = getchar(), _tmp = -1; n   while (_tc >=  0  && _tc <=  9 ) _x *= 10, _x += (_tc -  0 ), _tc = getchar(); n   return _x * _tmp; n } n inline long long add(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x += _y; n   return _x >= _mod ? _x - _mod : _x; n } n inline long long sub(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x -= _y; n   return _x < 0 ? _x + _mod : _x; n } n inline long long mul(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x *= _y; n   return _x >= _mod ? _x % _mod : _x; n } n long long mypow(long long _a, long long _x, long long _mod) { n   if (_x == 0) return 1LL; n   long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod); n   if (_x & 1) _ret = mul(_ret, _a, _mod); n   return _ret; n } n void sleep(double sec = 1021) { n   clock_t s = clock(); n   while (clock() - s < CLOCKS_PER_SEC * sec) n     ; n } n int __ = 1, _cs; n int dp[702][10]; n int fac[702 + 702], inv[702 + 702]; n inline int C(int a, int b) { return mul(fac[a], mul(inv[b], inv[a - b])); } n int pp[702], ten[702 + 702]; n int sv[702][10][702]; n int sv2[702][10][702]; n int iten[702 + 702]; n int dpw[10][702]; n void build() { n   ten[0] = fac[0] = inv[0] = 1; n   iten[0] = 1; n   int inv10 = mypow(10, 1000000007LL - 2, 1000000007LL); n   for (int i = 1; i < 702 + 702; i++) { n     fac[i] = mul(fac[i - 1], i); n     inv[i] = mypow(fac[i], 1000000007LL - 2, 1000000007LL); n     ten[i] = mul(ten[i - 1], 10); n     iten[i] = mul(iten[i - 1], inv10); n   } n   int inv9 = mypow(9, 1000000007LL - 2, 1000000007LL); n   for (int i = 1; i < 702; i++) pp[i] = mul(sub(ten[i], 1), inv9); n   for (int i = 1; i < 10; i++) { n     dpw[i][0] = 1; n     for (int j = 1; j < 702; j++) dpw[i][j] = mul(dpw[i][j - 1], i); n   } n   for (int k = 0; k < 702; k++) n     for (int dgt = 1; dgt < 10; dgt++) n       for (int i = 0; i <= k; i++) { n         int tmp = 1; n         tmp = mul(tmp, C(k, i)); n         tmp = mul(tmp, dpw[dgt][i]); n         if (9 - dgt > 0) tmp = mul(tmp, dpw[9 - dgt][k - i]); n         tmp = mul(tmp, ten[k - i]); n         sv[k][dgt][i] = tmp; n         if (i) sv[k][dgt][i] = add(sv[k][dgt][i], sv[k][dgt][i - 1]); n       } n   for (int k = 1; k < 702; k++) n     for (int dgt = 1; dgt < 10; dgt++) { n       if (dgt == 9) { n         for (int i = 0; i < k; i++) { n           int j = k - i; n           if (dgt == 9 and i + j < k) continue; n           int way = C(k, j); n           way = mul(way, C(k - j, i)); n           way = mul(way, dpw[dgt][i]); n           if (9 - dgt > 0) way = mul(way, dpw[9 - dgt][k - i - j]); n           way = mul(way, ten[k - i - j]); n           way = mul(way, pp[j]); n           dp[k][dgt] = add(dp[k][dgt], mul(way, dgt)); n         } n         continue; n       } n       for (int j = 1; j <= k; j++) { n         int way = C(k, j); n         way = mul(way, sv[k - j][dgt][k - j]); n         way = mul(way, pp[j]); n         dp[k][dgt] = add(dp[k][dgt], mul(way, dgt)); n       } n     } n } n char c[702]; n int ans, len; n void init() { n   scanf( %s , c); n   len = strlen(c); n } n int cnt[10]; n void go(int bt, bool bnd) { n   if (bt == len or not bnd) { n     int res = len - bt; n     if (res) { n       int ps = 0; n       for (int i = 9; i; i--) { n         ans = add(ans, mul(dp[res][i], ten[ps])); n         ps += cnt[i]; n       } n     } n     int ps = 0; n     for (int i = 0; i < 10; i++) { n       if (i) { n         for (int j = 0; j <= res; j++) { n           int way = sv2[res][i][j]; n           way = mul(way, ten[len - (ps + j) - cnt[i]]); n           way = mul(way, pp[cnt[i]]); n           ans = add(ans, mul(i, way)); n         } n       } n       ps += cnt[i]; n     } n     return; n   } n   for (int i = 0; i <= c[bt] -  0 ; i++) { n     if (bt == 0 and i == 0) continue; n     cnt[i]++; n     go(bt + 1, i == c[bt] -  0 ); n     cnt[i]--; n   } n } n void solve() { n   for (int res = 0; res < 702; res++) n     for (int i = 1; i < 10; i++) { n       for (int j = 0; j <= res; j++) { n         int way = C(res, j); n         way = mul(way, dpw[i][j]); n         way = mul(way, dpw[10 - i][res - j]); n         sv2[res][i][j] = way; n       } n     } n   if (len > 1) { n     for (int i = 1; i < 10; i++) ans = add(ans, dp[len - 1][i]); n   } n   go(0, true); n   cout << ans << endl; n } n int main() { n   build(); n   while (__--) { n     init(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9; n const long long MD = 1e9 + 7; n const double pi = acos(-1); n struct segmentTree { n   long long tl = 1, tr = (1ll << 21); n   segmentTree* left = nullptr; n   segmentTree* right = nullptr; n   long long val = 0; n   segmentTree(long long tl, long long tr) : tl(tl), tr(tr) {} n   segmentTree() = default; n   void createSons() { n     long long tm = (tl + tr) / 2; n     if (!left) { n       left = new segmentTree(tl, tm); n     } n     if (!right) { n       right = new segmentTree(tm + 1, tr); n     } n   } n   void update(long long pos, long long val) { n     if (tl == tr) { n       this->val = val; n       return; n     } n     long long tm = (tl + tr) / 2; n     createSons(); n     if (pos <= tm) { n       left->update(pos, val); n     } else { n       right->update(pos, val); n     } n     this->val = (left->val) ^ (right->val); n   } n   long long get(long long l, long long r) { n     if (l > r) { n       return 0; n     } n     if (l == tl && tr == r) { n       return val; n     } n     long long tm = (tl + tr) / 2; n     createSons(); n     return (left->get(l, min(r, tm))) ^ (right->get(max(tm + 1, l), r)); n   } n }; n ostream& operator<<(ostream& stream, const vector<long long>& v) { n   for (auto i : v) { n     stream << i <<   n ; n   } n   return stream; n } n pair<long long, long long> operator+=(pair<long long, long long>& a, n                                       const pair<long long, long long>& b) { n   a.first += b.first; n   a.second += b.second; n   return a; n }; n long long sqr(long long a) { return a * a % MD; } n long long binpow(long long a, long long b) { n   if (b == 0) { n     return 1; n   } n   if (b % 2 == 0) { n     return sqr(binpow(a, b / 2)); n   } n   return a * binpow(a, b - 1) % MD; n } n long long fact(long long a) { n   long long res = 1; n   for (long long i = 2; i <= a; i++) { n     res *= i; n     res %= MD; n   } n   return res; n } n struct edge { n   long long u, v, c; n   bool operator<(const edge& other) const { n     if (c == other.c) { n       if (u == other.u) { n         return v < other.v; n       } n       return u < other.u; n     } n     return c < other.c; n   } n }; n struct query { n   long long u, v, k, ind; n   bool operator<(const query& other) const { n     if (k == other.k) { n       if (u == other.u) { n         if (ind == other.ind) { n           return ind < other.ind; n         } n         return v < other.v; n       } n       return u < other.u; n     } n     return k < other.k; n   } n }; n vector<long long> g[100006]; n long long timer = 1; n long long subTreeSize[100006]; n long long pred[100006]; n long long tIn[100006]; n long long tOut[100006]; n void dfs(long long v) { n   tIn[v] = timer++; n   subTreeSize[v] = 1; n   for (auto i : g[v]) { n     if (!tIn[i]) { n       pred[i] = v; n       dfs(i); n       subTreeSize[v] += subTreeSize[i]; n     } n   } n   tOut[v] = timer++; n } n bool isPred(long long a, long long b) { n   return (tIn[a] <= tIn[b] && tOut[b] <= tOut[a]); n } n long long chainNumOfVertex[100006], inChainPositionOfVertex[100006], n     chainSize[100006], chainRoot[100006], chainsCount; n vector<segmentTree> segmTrees; n struct HLD { n   void build(long long v, long long chainNum) { n     chainNumOfVertex[v] = chainNum; n     inChainPositionOfVertex[v] = chainSize[chainNum]; n     chainSize[chainNum]++; n     if (chainSize[chainNum] == 1) { n       chainRoot[chainNum] = v; n     } n     long long maxChild = -1; n     for (auto i : g[v]) { n       if (i != pred[v] && n           (maxChild == -1 || subTreeSize[i] > subTreeSize[maxChild])) { n         maxChild = i; n       } n     } n     if (maxChild != -1) { n       for (auto i : g[v]) { n         if (i == pred[v]) { n           continue; n         } n         if (i == maxChild) { n           build(i, chainNum); n         } else { n           segmTrees.push_back(segmentTree()); n           build(i, ++chainsCount); n         } n       } n     } n   } n   long long get(long long a, long long b) { n     long long res = 0; n     for (long long i = 1; i <= 2; i++) { n       while (1) { n         if (isPred(chainRoot[chainNumOfVertex[a]], b)) { n           break; n         } n         segmTrees[chainNumOfVertex[a]].tl = 0; n         segmTrees[chainNumOfVertex[a]].tr = chainSize[chainNumOfVertex[a]]; n         res ^= n             segmTrees[chainNumOfVertex[a]].get(0, inChainPositionOfVertex[a]); n         a = pred[chainRoot[chainNumOfVertex[a]]]; n       } n       swap(a, b); n     } n     if (inChainPositionOfVertex[a] > inChainPositionOfVertex[b]) { n       swap(a, b); n     } n     segmTrees[chainNumOfVertex[a]].tl = 0; n     segmTrees[chainNumOfVertex[a]].tr = chainSize[chainNumOfVertex[a]]; n     res ^= segmTrees[chainNumOfVertex[a]].get(inChainPositionOfVertex[a] + 1, n                                               inChainPositionOfVertex[b]); n     return res; n   } n   void update(long long a, long long val) { n     segmTrees[chainNumOfVertex[a]].tl = 0; n     segmTrees[chainNumOfVertex[a]].tr = chainSize[chainNumOfVertex[a]]; n     segmTrees[chainNumOfVertex[a]].update(inChainPositionOfVertex[a], val); n   } n   void add(edge e) { n     if (pred[e.u] == e.v) { n       update(e.u, e.c); n     } else { n       update(e.v, e.c); n     } n   } n }; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     string st; n     cin >> st; n     st =  0  + st; n     long long ans = 0; n     for (long long i = 1; i < st.size(); i++) { n       if (st[i] ==  1 ) { n         ans = max(2 * i, ans); n         ans = max((n - i + 1) * 2, ans); n       } n     } n     cout << max(ans, n) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n void ran() { srand(chrono::steady_clock::now().time_since_epoch().count()); } n long long get_rand() { n   long long a = rand(); n   long long b = rand(); n   return a * (RAND_MAX + 1ll) + b; n } n void solve() { n   int n, m; n   cin >> n >> m; n   vector<int> subs[n + 1], inc[n + 1]; n   int labels[n + 1]; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     subs[a].push_back(b); n     inc[b].push_back(a); n   } n   vector<int> rem; n   for (int i = 1; i <= n; i++) { n     int from0 = 0, from1 = 0, from2 = 0, total = 0; n     for (auto x : inc[i]) { n       total++; n       if (labels[x] == 0) { n         from0++; n       } else if (labels[x] == 1) { n         from1++; n       } else { n         from2++; n       } n     } n     if (from2 == total) { n       labels[i] = 0; n     } else if (from1 == 0) { n       labels[i] = 1; n     } else { n       rem.push_back(i); n       labels[i] = 2; n     } n   } n   cout << rem.size() << endl; n   for (auto x : rem) cout << x <<    ; n   cout << endl; n } n signed main() { n   ran(); n   fast(); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct segment { n   int pos; n   int len; n   int type; n }; n struct segmentByLength { n   segment seg; n   bool operator<(segmentByLength o) const { n     segment seg2 = o.seg; n     if (seg.len != seg2.len) n       return seg.len > seg2.len; n     else { n       return seg.pos < seg2.pos; n     } n   } n }; n struct segmentByPosition { n   segment seg; n   bool operator<(segmentByPosition o) const { n     segment seg2 = o.seg; n     return seg.pos < seg2.pos; n   } n }; n set<segmentByLength> lengthSet; n set<segmentByPosition> positionSet; n void add_segment(segment s) { n   lengthSet.insert({s}); n   positionSet.insert({s}); n } n void remove_segment(segment s) { n   lengthSet.erase({s}); n   positionSet.erase({s}); n } n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   a.push_back(1000000001); n   int start = 0; n   for (int i = 0; i < a.size(); i++) { n     if (i > 0 and a[i] != a[i - 1]) { n       segment s = {start, (i - 1) - start + 1, a[i - 1]}; n       lengthSet.insert({{s}}); n       positionSet.insert({{s}}); n       start = i; n     } n   } n   int sol = 0; n   while (!lengthSet.empty()) { n     segment s = (*lengthSet.begin()).seg; n     segment left, right; n     bool leftExists = true, rightExists = true; n     auto lbIterator = positionSet.lower_bound({s}); n     if (lbIterator != positionSet.begin()) { n       --lbIterator; n       left = (*lbIterator).seg; n     } else { n       leftExists = false; n     } n     auto ubIterator = positionSet.upper_bound({s}); n     if (ubIterator != positionSet.end()) { n       right = (*ubIterator).seg; n     } else { n       rightExists = false; n     } n     remove_segment(s); n     if (leftExists and rightExists and left.type == right.type) { n       segment merged = {left.pos, left.len + right.len, left.type}; n       remove_segment(left); n       remove_segment(right); n       add_segment(merged); n     } n     sol++; n   } n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     long long k; n     int x; n     scanf( %lld%d , &k, &x); n     printf( %lld n , x + 9 * (k - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long input[(long long)1e5 + 1]; n long long ans = 0; n long long n; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> input[i]; n   for (int i = n - 2; i >= 0; i--) { n     if (input[i] > input[i + 1]) ans += input[i] - input[i + 1]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 5; n int a[MAXN], x, f, b, n; n vector<int> v; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   cin >> f >> b; n   sort(a, a + n); n   n = unique(a, a + n) - a; n   for (int i = 0; i < n; i++) v.push_back(a[i]); n   int tmp; n   while (f != b) { n     tmp = f - 1; n     for (int i = v.size() - 1; ~i; i--) { n       if (f - f % v[i] < b) n         v.erase(v.begin() + i); n       else n         tmp = min(tmp, f - f % v[i]); n     } n     x++; n     f = tmp; n   } n   cout << x; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long d = 20; n long long n, m, dsu[80], cc[80], al[40], dp[1ll << d]; n long long fd(long long x) { n   if (dsu[x] != x) { n     dsu[x] = fd(dsu[x]); n   } n   return dsu[x]; n } n int main() { n   long long i, ii, k, l, mk, mk2, z[4] = {1, 1, 1, 0}; n   scanf( %lld%lld , &n, &m); n   if (!m) { n     printf( 0 n ); n     return 0; n   } n   for (i = 0; i < n * 2; i++) { n     dsu[i] = i; n     cc[i] = 1; n   } n   for (i = 0; i < m; i++) { n     scanf( %lld%lld , &k, &l); n     k--; n     l--; n     for (ii = 0; ii < 2; ii++) { n       if (fd(k) != fd(n + l)) { n         cc[fd(k)] += cc[fd(n + l)]; n         dsu[fd(n + l)] = fd(k); n       } n       al[k] |= 1ll << l; n       swap(k, l); n     } n   } n   for (i = 0; i < n * 2; i++) { n     z[0] *= i >= n || fd(i) != fd(n + i); n   } n   for (i = 0; i < n; i++) { n     if (fd(i) != fd(n + i)) { n       cc[fd(i)] += cc[fd(n + i)]; n       dsu[fd(n + i)] = fd(i); n     } n   } n   for (i = 0; i < n * 2; i++) { n     if (fd(i) == i) { n       z[0] *= 2; n       z[1] *= 2; n       z[2] *= 1 + (fd(i) == i && cc[i] == 2); n     } n   } n   for (mk = 0; mk < 1ll << min(d, n); mk++) { n     for (i = 0; i < min(d, n); i++) { n       if ((mk >> i & 1) && (mk & al[i])) { n         break; n       } n     } n     dp[mk] += i >= min(d, n); n   } n   for (i = 0; i < min(d, n); i++) { n     for (mk = (1ll << min(d, n)) - 1; mk + 1; mk--) { n       dp[mk] += dp[mk ^ 1ll << i] * (mk >> i & 1); n     } n   } n   for (mk = 0; mk < 1ll << n; mk += 1ll << min(d, n)) { n     for (i = min(d, n); i < n; i++) { n       if ((mk >> i & 1) && (mk & al[i])) { n         break; n       } n     } n     if (i < n) { n       continue; n     } n     mk2 = 0; n     for (i = 0; i < min(d, n); i++) { n       mk2 |= (long long)!(mk & al[i]) << i; n     } n     z[3] += dp[mk2]; n   } n   printf( %lld n , (1ll << n) - z[1] - z[3] * 2 + z[0] + z[2] * 2); n } n


#include <bits/stdc++.h> n using namespace std; n int g[100010]; n int main(void) { n   int A, T, N, M; n   scanf( %d , &N); n   for (int i = (int)(3); i <= (int)(N); i++) { n     set<int> st; n     A = 2000000000; n     for (int j = (int)(2); j <= (int)(N); j++) { n       int k = i - j * (j + 1) / 2; n       if (k < 0) break; n       if (k % j != 0) continue; n       int l = 1 + k / j, r = j + k / j, nim = 0; n       while (l <= r) { n         nim = nim ^ g[l++]; n       } n       st.insert(nim); n       if (nim == 0) A = min(A, j); n     } n     int res = 0; n     while (st.find(res) != st.end()) res++; n     g[i] = res; n   } n   if (g[N]) n     printf( %d n , A); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int n, m; n char ch[20][120]; n int l[20], r[20], visit[20], dp[20][2]; n int main() { n   while (~scanf( %d%d , &n, &m)) { n     memset(visit, 0, sizeof visit); n     memset(r, 0, sizeof r); n     memset(l, 0, sizeof l); n     for (int i = n; i >= 1; i--) { n       scanf( %s , ch[i] + 1); n       for (int j = 2; j <= m + 1; j++) n         if (ch[i][j] ==  1 ) { n           l[i] = j; n           visit[i] = 1; n           break; n         } n       for (int j = m + 1; j >= 2; j--) n         if (ch[i][j] ==  1 ) { n           r[i] = j; n           break; n         } n     } n     int x = n; n     while (!visit[x--] && x >= 1) n--; n     if (n == 1) { n       printf( %d n , r[1] ? r[1] - 1 : 0); n       continue; n     } n     memset(dp, INF, sizeof dp); n     dp[1][0] = r[1] ? (r[1] - 1) * 2 : 0, dp[1][1] = m + 1; n     for (int i = 2; i < n; i++) { n       if (!visit[i]) { n         dp[i][0] = dp[i - 1][0] + 1; n         dp[i][1] = dp[i - 1][1] + 1; n         continue; n       } n       dp[i][0] = min(dp[i - 1][0] + 2 * (r[i] - 1) + 1, dp[i][0]); n       dp[i][0] = min(dp[i - 1][1] + m + 1 + 1, dp[i][0]); n       dp[i][1] = min(dp[i - 1][1] + 2 * (m + 2 - l[i]) + 1, dp[i][1]); n       dp[i][1] = min(dp[i - 1][0] + m + 1 + 1, dp[i][1]); n     } n     int ans = min(dp[n - 1][0] + r[n], dp[n - 1][1] + 1 + m + 2 - l[n]); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void pr(vector<T> &v) { n   for (int i = 0; i < (int)(v).size(); i++) cout << v[i] <<    ; n   cout << endl; n } n template <typename T> n void pr(vector<vector<T>> &v) { n   for (int i = 0; i < (int)(v).size(); i++) { n     pr(v[i]); n   } n } n template <typename T> n void re(T &first) { n   cin >> first; n } n template <typename T> n void re(vector<T> &a) { n   for (int i = 0; i < (int)(a).size(); i++) re(a[i]); n } n template <class Arg, class... Args> n void re(Arg &first, Args &...rest) { n   re(first); n   re(rest...); n } n template <typename T> n void pr(T first) { n   cout << first << endl; n } n template <class Arg, class... Args> n void pr(const Arg &first, const Args &...rest) { n   cout << first <<    ; n   pr(rest...); n   cout << endl; n } n void ps() { cout << endl; } n template <class T, class... Ts> n void ps(const T &t, const Ts &...ts) { n   cout << t; n   if (sizeof...(ts)) cout <<    ; n   ps(ts...); n } n int n; n vector<int> cur; n int cnt(vector<int> &v, int mn) { n   int a = 0; n   for (int i = 0; i < (int)(v).size(); i++) { n     if (mn < n && v[i] == cur[mn]) { n       mn++; n       a++; n     } n   } n   return (int)(v).size() - a; n } n int rec(vector<int> v, int mn) { n   if ((int)(v).size() == 0) return 0; n   int ind = 0; n   for (int i = 0; i < (int)(v).size(); i++) { n     if (v[i] == cur[mn]) ind = i; n   } n   int xx = cnt(v, mn); n   v.erase(v.begin() + ind); n   int yy = 1 + rec(v, mn + 1); n   int ack = min(xx, yy); n   return ack; n } n void solve() { n   re(n); n   vector<int> a(n); n   re(a); n   cur.clear(); n   cur = a; n   sort(cur.begin(), cur.end()); n   pr(rec(a, 0)); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   re(t); n   for (int tt = 0; tt < t; tt++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n, l, s = 0; n   cin >> n >> m >> l; n   vector<int> a(n); n   vector<int> otvet; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   if (n == 1) { n     for (int i = 0; i < m; ++i) { n       int x; n       cin >> x; n       if (x == 0) { n         if (a[0] <= l) n           otvet.push_back(0); n         else n           otvet.push_back(1); n         continue; n       } n       int y, z; n       cin >> y >> z; n       if (a[0] <= l) a[0] += z; n     } n     for (int i = 0; i < otvet.size(); ++i) cout << otvet[i] << endl; n     return 0; n   } n   int i = 0; n   while (i < n) { n     if (a[i] <= l) { n       ++i; n       continue; n     } n     int j = i; n     while (a[j] > l && j < n) { n       j++; n     } n     s++; n     i = j; n   } n   for (int i = 0; i < m; ++i) { n     int x; n     cin >> x; n     if (x == 0) { n       otvet.push_back(s); n       continue; n     } n     int y, z; n     cin >> y >> z; n     y--; n     if (a[y] > l) continue; n     a[y] += z; n     if (a[y] > l) { n       if (y > 0 && y < n - 1) { n         if (a[y - 1] <= l && a[y + 1] <= l) n           s++; n         else if (a[y - 1] > l && a[y + 1] > l) { n           s--; n         } n       } else if (y == 0) { n         if (a[y + 1] <= l) s++; n       } else if (y == n - 1) { n         if (a[y - 1] <= l) s++; n       } n     } n   } n   for (int i = 0; i < otvet.size(); ++i) cout << otvet[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void io() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n long long int a[100001], n, k, pos = 1000000000000000; n map<long long int, long long int> m; n set<long long int> s; n int main() { n   cin >> n >> k; n   s.clear(); n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     if (s.size() < k) m[a[i]]++; n     s.insert(a[i]); n     if (s.size() == k) pos = min(pos, i); n   } n   if (pos == 1000000000000000) { n     cout << -1 <<     << -1 << endl; n     return 0; n   } n   for (long long int i = 0; i < pos + 1; i++) { n     m[a[i]]--; n     if (m[a[i]] == 0) { n       cout << i + 1 <<     << pos + 1 << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   int64_t res1 = 0, res2 = 0, sum = 0; n   bool is; n   std::cin >> n; n   std::vector<int> m(n); n   for (int i = 0; i < n; i++) { n     std::cin >> m[i]; n     sum += m[i]; n   } n   is = sum % n != 0; n   sum /= n; n   for (int i = 0; i < n; i++) { n     if (m[i] < sum) { n       res1 += sum - m[i]; n     } else if (is) { n       if (m[i] > sum + 1) { n         res2 += m[i] - sum - 1; n       } n     } else { n       if (m[i] > sum) { n         res2 += m[i] - sum; n       } n     } n   } n   std::cout << std::max(res1, res2); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 20009; n int INF = 2e9; n struct edge { n   int to, cap, rev; n   edge() {} n   edge(int tt, int cc, int rr) { n     to = tt; n     cap = cc; n     rev = rr; n   } n }; n vector<edge> Graph[N]; n int level[N]; n int iter[N]; n void add_edge(int from, int to, int cap) { n   Graph[from].push_back(edge(to, cap, Graph[to].size())); n   Graph[to].push_back(edge(from, 0, Graph[from].size() - 1)); n } n void bfs(int s) { n   memset(level, -1, sizeof(level)); n   queue<int> que; n   level[s] = 0; n   que.push(s); n   while (que.size()) { n     int v = que.front(); n     que.pop(); n     for (int i = 0; i < Graph[v].size(); i++) { n       edge &e = Graph[v][i]; n       if (e.cap > 0 && level[e.to] < 0) { n         level[e.to] = level[v] + 1; n         que.push(e.to); n       } n     } n   } n } n int dfs(int v, int t, int f) { n   if (v == t) return f; n   for (int &i = iter[v]; i < Graph[v].size(); i++) { n     edge &e = Graph[v][i]; n     if (e.cap > 0 && level[v] < level[e.to]) { n       int d = dfs(e.to, t, min(f, e.cap)); n       if (d > 0) { n         e.cap -= d; n         Graph[e.to][e.rev].cap += d; n         return d; n       } n     } n   } n   return 0; n } n int max_flow(int s, int t) { n   int flow = 0; n   while (1) { n     bfs(s); n     if (level[t] < 0) return flow; n     memset(iter, 0, sizeof(iter)); n     int f; n     while ((f = dfs(s, t, INF)) > 0) { n       flow += f; n     } n   } n   return flow; n } n int a[105], b[105], res[105][105], SumIn, SumOut; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) n     scanf( %d , &a[i]), add_edge(0, i + 1, a[i]), SumIn += a[i]; n   for (int i = 0; i < n; i++) n     scanf( %d , &b[i]), add_edge(i + 1 + n, 2 * n + 1, b[i]), SumOut += b[i]; n   for (int i = 0; i < n; i++) add_edge(i + 1, i + 1 + n, 1e6); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     add_edge(u, v + n, 1e6); n     add_edge(v, u + n, 1e6); n   } n   if (SumIn != SumOut || SumIn != max_flow(0, 2 * n + 1)) n     return 0 * printf( NO n ); n   printf( YES n ); n   for (int i = 1; i <= n; i++) n     for (auto &e : Graph[i]) n       if (e.to > n) res[i - 1][e.to - n - 1] = Graph[e.to][e.rev].cap; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) printf( %d  , res[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, i, j; n     cin >> n; n     long long int arr[n][n]; n     for (i = 0; i < n; i++) { n       string s; n       cin >> s; n       for (j = 0; j < n; j++) { n         long long int temp; n         temp = s[j] - 48; n         arr[i][j] = temp; n       } n     } n     int flag = 1; n     for (i = 0; i < n; i++) { n       for (j = 0; j < n; j++) { n         if (arr[i][j] == 1) { n           if (j != n - 1 && i != n - 1) { n             if (arr[i + 1][j] != 1 && arr[i][j + 1] != 1) { n               flag = 0; n               break; n             } n           } n         } n       } n       if (flag == 0) break; n     } n     if (flag == 1) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int DX[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n const int DY[8] = {0, 0, 1, -1, 1, -1, 1, -1}; n const int intmax = 0x7fffffff; n const int mod = 1000000007; n int n, m; n int f[205]; n int p[205]; n vector<pair<int, int> > op; n int main() { n   scanf( %d%d , &n, &m); n   int cnt = 0; n   for (int i = 0; i < m; i++) { n     int nk; n     scanf( %d , &nk); n     for (int j = 0; j < nk; j++) { n       int a; n       scanf( %d , &a); n       cnt++; n       f[a] = cnt; n       p[cnt] = a; n     } n   } n   int pemp; n   for (int i = 1; i <= n; i++) n     if (!f[i]) pemp = i; n   for (int j = 1; j <= cnt; j++) { n     if (f[j] != j) { n       if (!f[j] || j == pemp) { n         pemp = p[j]; n         op.push_back(make_pair(p[j], j)); n         p[j] = j; n         f[j] = j; n         continue; n       } n       f[pemp] = f[j]; n       p[f[j]] = pemp; n       op.push_back(make_pair(j, pemp)); n       int now = j; n       while (f[now] != now && now <= cnt && now >= 1) { n         op.push_back(make_pair(p[now], now)); n         int tmp = now; n         f[now] = now; n         now = p[now]; n         p[tmp] = tmp; n       } n       pemp = now; n     } n   } n   printf( %d n , op.size()); n   for (int i = 0; i < op.size(); i++) { n     printf( %d %d n , op[i].first, op[i].second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline char nc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline void read(int &x) { n   char c = nc(), b = 1; n   for (; !(c >=  0  && c <=  9 ); c = nc()) n     if (c ==  - ) b = -1; n   for (x = 0; c >=  0  && c <=  9 ; x = x * 10 + c -  0 , c = nc()) n     ; n   x *= b; n } n const int N = 55; n const int M = 2505; n struct edge { n   int u, v, w, f; n   int next; n } G[M << 1]; n int head[N], inum = 1; n inline void add(int u, int v, int w, int f, int p) { n   G[p].u = u; n   G[p].v = v; n   G[p].w = w; n   G[p].f = f; n   G[p].next = head[u]; n   head[u] = p; n } n inline void link(int u, int v, int w, int f) { n   add(u, v, w, f, ++inum); n   add(v, u, -w, 0, ++inum); n } n int n, m, P; n int S, T, Mincost; n int Q[N * M], l, r; n int dis[N], pre[N], ins[N]; n inline bool SPFA() { n   for (int i = 1; i <= n; i++) dis[i] = 1 << 29, pre[i] = 0, ins[i] = 0; n   l = r = -1; n   Q[++r] = S, dis[S] = 0, ins[S] = 1; n   while (l < r) { n     int u = Q[++l]; n     ins[u] = 0; n     for (int p = head[u]; p; p = G[p].next) n       if (G[p].f && dis[G[p].v] > dis[u] + G[p].w) { n         dis[G[p].v] = dis[u] + G[p].w; n         pre[G[p].v] = p; n         if (!ins[G[p].v]) Q[++r] = G[p].v, ins[G[p].v] = 1; n       } n   } n   if (dis[T] == 1 << 29) return 0; n   Mincost += dis[T]; n   for (int p = pre[T]; p; p = pre[G[p].u]) G[p].f--, G[p ^ 1].f++; n   return 1; n } n int _, Qry[123456]; n double Ans[123456]; n int main() { n   int u, v, c, d; n   read(n); n   read(m); n   S = 1; n   T = n; n   for (int i = 1; i <= m; i++) n     read(u), read(v), read(d), c = 1, link(u, v, d, c); n   read(_); n   for (int i = 0; i < _; i++) read(Qry[i]); n   double ans = 1e20; n   int m = 0; n   for (int i = 0; i < _; i++) Ans[i] = 1e20; n   while (SPFA()) { n     m++; n     for (int i = 0; i < _; i++) n       Ans[i] = min(Ans[i], ((double)(Mincost + Qry[i])) / m); n     ans = min(ans, (double)(Mincost + P) / m); n   } n   for (int i = 0; i < _; i++) printf( %.10f n , Ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long getint() { n   char ch = getchar(); n   long long f = 1, x = 0; n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return f * x; n } n const int N = 200200; n int n; n struct dat { n   long long w, h, c; n } a[N]; n int bg[N], ed[N]; n int cnt; n int ans; n bool operator<(const dat &a, const dat &b) { n   return (a.w == b.w) ? a.h < b.h : a.w < b.w; n } n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n bool validate() { n   for (int i = 2; i <= cnt; i++) { n     if (ed[i] - bg[i] != ed[i - 1] - bg[i - 1]) return false; n     for (int j = bg[i], k = bg[i - 1]; j <= ed[i]; j++, k++) { n       if (a[j].h != a[k].h) return false; n     } n     long long g1, g2; n     for (int j = bg[i], k = bg[i - 1]; j < ed[i]; j++, k++) { n       g1 = gcd(a[j].c, a[j + 1].c); n       g2 = gcd(a[k].c, a[k + 1].c); n       if (a[j].c / g1 != a[k].c / g2 || a[j + 1].c / g1 != a[k + 1].c / g2) n         return false; n     } n   } n   return true; n } n void init() { n   n = getint(); n   for (int i = 1; i <= n; i++) { n     a[i].w = getint(); n     a[i].h = getint(); n     a[i].c = getint(); n   } n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     ++cnt; n     bg[cnt] = i; n     while (a[i].w == a[i + 1].w) i++; n     ed[cnt] = i; n   } n } n int main() { n   init(); n   if (!validate()) { n     puts( 0 ); n     return 0; n   } n   long long g = a[1].c; n   for (int i = 2; i <= n; i++) { n     g = gcd(g, a[i].c); n   } n   for (long long i = 1; i * i <= g; i++) { n     if (g % i == 0) { n       ans += (i * i == g) ? 1 : 2; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<long long, vector<long long>, greater<long long>> q; n int a[100005]; n int main() { n   int n, x; n   long long b = 0, s; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &x); n     s = 0; n     q.push(a[i] + b); n     while (!q.empty() && q.top() <= b + x) { n       s += q.top() - b; n       q.pop(); n     } n     printf( %I64d  , s + (long long)q.size() * x); n     b += x; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using minpq = priority_queue<T, vector<T>, greater<T>>; n struct node { n   int val, le, ri; n   node() : val(1e9 + 5), le(0), ri(0) {} n }; n const int N = 1e5 + 5; n int n, r, m, p, q, u, v, a[N], depth[N], root[N], tin[N], tout[N], ti = 1; n vector<int> adj[N]; n bool vis[N]; n vector<node> tree; n void dfs(int x) { n   vis[x] = true; n   tin[x] = ti++; n   for (int y : adj[x]) { n     if (!vis[y]) { n       depth[y] = 1 + depth[x]; n       dfs(y); n     } n   } n   tout[x] = ti; n } n int build(int l, int r) { n   int k = ((int)(tree).size()); n   tree.emplace_back(); n   if (l < r) { n     int m = (l + r) / 2; n     tree[k].le = build(l, m); n     tree[k].ri = build(m + 1, r); n     tree[k].val = min(tree[tree[k].le].val, tree[tree[k].ri].val); n   } n   return k; n } n int upd(int i, int l, int r, int idx, int x) { n   int k = ((int)(tree).size()); n   tree.emplace_back(); n   if (l < r) { n     int m = (l + r) / 2; n     if (idx <= m) { n       tree[k].le = upd(tree[i].le, l, m, idx, x); n       tree[k].ri = tree[i].ri; n     } else { n       tree[k].le = tree[i].le; n       tree[k].ri = upd(tree[i].ri, m + 1, r, idx, x); n     } n     tree[k].val = min(tree[tree[k].le].val, tree[tree[k].ri].val); n   } else { n     tree[k].val = x; n   } n   return k; n } n int query(int i, int l, int r, int L, int R) { n   if (r < L || R < l) return 1e9 + 5; n   if (L <= l && r <= R) return tree[i].val; n   int m = (l + r) / 2; n   return min(query(tree[i].le, l, m, L, R), query(tree[i].ri, m + 1, r, L, R)); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> r; n   for (int i = (1); i < (n + 1); i++) { n     cin >> a[i]; n   } 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   dfs(r); n   vector<pair<int, int>> ve; n   for (int i = (1); i < (n + 1); i++) { n     ve.emplace_back(depth[i], i); n   } n   sort((ve).begin(), (ve).end()); n   root[0] = build(1, n); n   int prevdepth = 0; n   for (auto &p : ve) { n     int u = p.second; n     root[depth[u]] = upd(root[prevdepth], 1, n, tin[u], a[u]); n     prevdepth = depth[u]; n   } n   cin >> m; n   int last = 0; n   while (m--) { n     cin >> p >> q; n     int x = 1 + ((p + last) % n); n     int k = (q + last) % n; n     last = query(root[min(prevdepth, depth[x] + k)], 1, n, tin[x], tout[x] - 1); n     cout << last <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005]; n long long b[200005]; n vector<long long> edge[200005]; n vector<long long> roots; n vector<bool> visited; n long long max_val[200005]; n long long ans = 0; n long long dfs(long long v) { n   visited[v] = true; n   long long ret = 0; n   for (long long next : edge[v]) { n     if (!visited[next]) { n       ret += max(dfs(next), (long long)0); n     } n   } n   ans += ret + a[v]; n   return max_val[v] = ret + a[v]; n } n void dfs2(long long v) { n   visited[v] = true; n   for (long long next : edge[v]) { n     if (!visited[next]) { n       if (max_val[next] >= 0) { n         dfs2(next); n       } n     } n   } n   printf( %lld  , v); n   for (long long next : edge[v]) { n     if (!visited[next]) { n       dfs2(next); n     } n   } n } n int main() { n   long long n; n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , a + i); n   } n   for (int j = 1; j <= n; j++) { n     long long x; n     scanf( %lld , &x); n     if (x != -1) { n       edge[x].push_back(j); n     } else { n       roots.push_back(j); n     } n   } n   visited = vector<bool>(200005, false); n   for (int i = 0; i < roots.size(); i++) { n     dfs(roots[i]); n   } n   printf( %lld n , ans); n   visited = vector<bool>(200005, false); n   for (int i = 0; i < roots.size(); i++) { n     dfs2(roots[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, n; n   cin >> a >> b >> n; n   long long l, t, m; n   for (int i = 0; i < n; ++i) { n     cin >> l >> t >> m; n     long long sl = a + b * (l - 1), sr; n     if (t < sl) { n       cout << -1 << endl; n       continue; n     } n     int lf = l, rt = (t - a) / b + 1; n     int r = (lf + rt) / 2; n     while (lf != r) { n       sr = a + b * (r - 1); n       if ((sl + sr) * (r - l + 1) / 2 <= t * m) { n         lf = r; n       } else { n         rt = r - 1; n       } n       r = (lf + rt) / 2; n     } n     if ((sl + a + b * (rt - 1)) * (rt - l + 1) / 2 <= t * m) { n       r = rt; n     } n     cout << r << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int32_t n; n   std::cin >> n; n   int32_t* arr = new int32_t[n]; n   for (int32_t i = 0; i < n; i++) { n     std::cin >> arr[i]; n   } n   std::string ans =   ; n   char last =  b ; n   for (int32_t i = n - 1; i >= 0; i--) { n     if (arr[i] == 0) continue; n     int32_t length = i + 1; n     for (int32_t j = 0; j < arr[i]; j++) { n       ans += std::string(length, last); n       last = last ==  a  ?  b  :  a ; n     } n     for (int32_t j = i - 1; j >= 0; j--) arr[j] -= arr[i] * (i - j + 1); n   } n   std::cout << ans; n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   scanf( %d %d , &a, &b); n   if (a == 0 && b == 0) n     printf( NO ); n   else if (fabs(a - b) == 1 || a - b == 0) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int charToDec(char c) { n   if ((c >=  0 ) && (c <=  9 )) return c -  0 ; n   if ((c >=  A ) && (c <=  Z )) return c -  A  + 10; n } n char decToChar(int c) { n   if ((c >= 0) && (c <= 9)) return c +  0 ; n   if ((c >= 10) && (c <= 26)) return c +  A  - 10; n } n long long hzToDec(string s, int a) { n   long long r = 0; n   for (int i = 0; i < s.length(); i++) { n     r *= a; n     r += charToDec(s[i]); n   } n   return r; n } n string decToHz(long long x, int a) { n   string s =   ; n   while (x > 0) { n     s = decToChar(x % a) + s; n     x = x / a; n   } n   if (s ==   ) s =  0 ; n   return s; n } n string toRome(long long X) { n   string r[3100]; n   r[3000] =  MMM ; n   r[2000] =  MM ; n   r[1000] =  M ; n   r[900] =  CM ; n   r[800] =  DCCC ; n   r[700] =  DCC ; n   r[600] =  DC ; n   r[500] =  D ; n   r[400] =  CD ; n   r[300] =  CCC ; n   r[200] =  CC ; n   r[100] =  C ; n   r[90] =  XC ; n   r[80] =  LXXX ; n   r[70] =  LXX ; n   r[60] =  LX ; n   r[50] =  L ; n   r[40] =  XL ; n   r[30] =  XXX ; n   r[20] =  XX ; n   r[10] =  X ; n   r[9] =  IX ; n   r[8] =  VIII ; n   r[7] =  VII ; n   r[6] =  VI ; n   r[5] =  V ; n   r[4] =  IV ; n   r[3] =  III ; n   r[2] =  II ; n   r[1] =  I ; n   return r[X / 1000 * 1000] + r[(X % 1000) / 100 * 100] + n          r[(X % 100) / 10 * 10] + r[X % 10]; n } n int main() { n   int a, b; n   string bb, x; n   long long x10; n   cin >> a >> bb >> x; n   x10 = hzToDec(x, a); n   if (bb ==  R ) { n     cout << toRome(x10); n   } else n     cout << decToHz(x10, atoi(bb.c_str())); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 100035; n int n, num, cnt, pr[maxn], phi[maxn]; n bool vis[maxn]; n long long ans, f[maxn]; n std::vector<int> fac[maxn], opt; n void makePrime() { n   phi[1] = 1; n   for (int i = 2; i < maxn; i++) { n     if (!vis[i]) pr[++pr[0]] = i, phi[i] = i - 1; n     for (int j = 1, chk = 1; j <= pr[0] && i * pr[j] < maxn && chk; j++) n       if (pr[j] % i) n         phi[i * pr[j]] = phi[i] * (pr[j] - 1); n       else n         chk = 0, phi[i * pr[j]] = phi[i] * pr[j]; n   } n   for (int i = 1; i < maxn; i++) n     for (int j = i; j < maxn; j += i) fac[j].push_back(i); n } n int qmi(int a, int b) { n   int ret = 1; n   for (; b; b >>= 1, a = 1ll * a * a % 1000000007) n     if (b & 1) ret = 1ll * ret * a % 1000000007; n   return ret; n } n int dmd; n void dfs(int x, int c, int t) { n   if (x == opt.size()) n     ++dmd, cnt += ((t & 1) ? -num / c : num / c); n   else { n     dfs(x + 1, c, t); n     dfs(x + 1, c * opt[x], t + 1); n   } n } n int calc(int p, int d) { n   bool debug = p == 12; n   std::vector<int>().swap(opt), p /= d, num = n / d; n   for (int i = 1; pr[i] <= p; i++) n     if (p % pr[i] == 0) { n       opt.push_back(pr[i]); n       while (p % pr[i] == 0) p /= pr[i]; n     } n   cnt = 0, dfs(0, 1, 0); n   return cnt; n } n int main() { n   scanf( %d , &n), ans = 1, f[1] = 0; n   makePrime(); n   for (int i = 2; i <= n; i++) { n     int lwr = n         (1 - 1ll * qmi(n, 1000000007 - 2) * (n / i) % 1000000007 + 1000000007) % n         1000000007; n     for (int j = 1; j < fac[i].size() - 1; j++) n       f[i] = (f[i] + 1ll * f[fac[i][j]] * calc(i, fac[i][j]) % 1000000007) % n              1000000007; n     f[i] = 1ll * (1ll * f[i] * qmi(n, 1000000007 - 2) % 1000000007 + 1) * n            qmi(lwr, 1000000007 - 2) % 1000000007; n     ans = (ans + f[i] + 1) % 1000000007; n   } n   printf( %lld n , 1ll * ans * qmi(n, 1000000007 - 2) % 1000000007); n   return 0; n } n