#include <bits/stdc++.h> n const double pi = acos(-1.0); n const int max_n = 1e5 + 5; n template <class Q> n void getint(Q &res, char ch = getchar()) { n   while (!isdigit(ch) && ch !=  - ) ch = getchar(); n   bool flag = true; n   if ( -  == ch) flag = false, ch = getchar(); n   for (res = 0; isdigit(ch); ch = getchar()) res = res * 10 + ch - 48; n   if (!flag) res = -res; n } n void putint(int x) { n   int i = 0, a[10]; n   bool flag = true; n   if (x < 0) { n     flag = false; n     x = -x; n   } else if (x == 0) { n     a[i++] = 0; n   } n   while (x > 0) { n     a[i++] = x % 10; n     x /= 10; n   } n   if (flag == false) putchar( - ); n   for (int j = i - 1; j >= 0; --j) putchar(a[i] + 48); n } n int mmax(int a, int b) { return a > b ? a : b; } n int mmin(int a, int b) { return a < b ? a : b; } n int gcd(int a, int b) { n   while (b) { n     b ^= a ^= b ^= a %= b; n   } n   return a; n } n int n; n char s[1005], t[1005]; n int a[10], b[10]; n int main() { n   std::cin >> n; n   std::cin >> s >> t; n   for (int i = 0; i < n; ++i) { n     a[t[i] -  0 ]++; n     b[t[i] -  0 ]++; n   } n   int ans1 = 0, ans2 = 0; n   for (int i = 0; i < n; ++i) { n     int f = 0; n     for (int j = s[i] -  0 ; j <= 9; ++j) { n       if (a[j] > 0) { n         a[j]--; n         f = 1; n         break; n       } n     } n     if (f == 0) ans1++; n     for (int j = s[i] -  0  + 1; j <= 9; ++j) { n       if (b[j] > 0) { n         b[j]--; n         ans2++; n         break; n       } n     } n   } n   std::cout << ans1 << std::endl << ans2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int sz, n; n   cin >> sz >> n; n   int sx, sy, tx, ty; n   cin >> sx >> sy >> tx >> ty; n   vector<vector<pair<int, int>>> g(n + 2); n   g[n].emplace_back(n + 1, abs(tx - sx) + abs(ty - sy)); n   vector<int> x(n), y(n); n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     g[n].emplace_back(i, min(abs(x[i] - sx), abs(y[i] - sy))); n     g[i].emplace_back(n + 1, abs(tx - x[i]) + abs(ty - y[i])); n   } n   vector<int> order(n); n   iota(order.begin(), order.end(), 0); n   for (int rot = 0; rot < 2; rot++) { n     sort(order.begin(), order.end(), [&](int i, int j) { return x[i] < x[j]; }); n     for (int z = 0; z < n - 1; z++) { n       int i = order[z]; n       int j = order[z + 1]; n       g[i].emplace_back(j, x[j] - x[i]); n       g[j].emplace_back(i, x[j] - x[i]); n     } n     swap(x, y); n   } n   priority_queue<pair<int, int>> q; n   vector<int> dist(n + 2, (int)2e9); n   dist[n] = 0; n   q.emplace(0, n); n   while (!q.empty()) { n     auto pr = q.top(); n     q.pop(); n     int expected = -pr.first; n     int v = pr.second; n     if (dist[v] != expected) { n       continue; n     } n     for (const auto& pr : g[v]) { n       int to = pr.first; n       int cost = pr.second; n       if (dist[v] + cost < dist[to]) { n         dist[to] = dist[v] + cost; n         q.emplace(-dist[to], to); n       } n     } n   } n   cout << dist[n + 1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int DBG = 0, INF = int(1e9); n int main() { n   ios_base::sync_with_stdio(0); n   cout.setf(ios::fixed); n   int n; n   cin >> n; n   vector<int> a(n); n   for (auto& x : a) cin >> x; n   int mx = *max_element(a.begin(), a.end()); n   for (int q = 0; q < mx; ++q) { n     for (int i = 0; i < n; ++i) { n       if (a[i] > 0) { n         cout <<  P ; n         --a[i]; n       } n       if (i < n - 1) cout <<  R ; n     } n     for (int i = 0; i < n - 1; ++i) cout <<  L ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[8000]; n char s[8000]; n int main() { n   scanf( %s , s); n   int n = strlen(s), m = n; n   if (s[0] ==  *  || s[0] ==  / ) { n     puts( 0 ); n     return 0; n   } n   f[m] = 1; n   for (int i = 0; i < n; i++) n     if (!isdigit(s[i])) n       if (s[i + 1] ==  *  || s[i + 1] ==  /  || i == n - 1) { n         puts( 0 ); n         return 0; n       } else n         m--; n     else if (!isdigit(s[i + 1])) n       for (int j = m + n; j > m; j--) f[j - 1] = (f[j - 1] + f[j]) % 1000003; n   printf( %d n , f[m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string str1, str2, ans1, ans2; n   cin >> str1 >> str2; n   ans1 = str1; n   ans2 = str2; n   cout << ans1 <<     << ans2 << endl; n   cin >> n; n   while (n--) { n     cin >> str1 >> str2; n     if (str1 == ans1) { n       ans1 = str2; n     } else n       ans2 = str2; n     cout << ans1 <<     << ans2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     if (n == 1) { n       int x; n       cin >> x; n       cout <<  YES ; n     } else { n       vector<int> a(n); n       bool flag = true; n       cin >> a[0]; n       for (int i = 1; i < n; ++i) { n         cin >> a[i]; n         if ((a[i] % 2) != (a[i - 1] % 2)) { n           flag = false; n         } n       } n       if (flag) { n         cout <<  YES ; n       } else { n         cout <<  NO ; n       } n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   long long n, k; n   cin >> n >> k; n   vector<long long> ans(n); n   set<pair<long long, long long>> demid; n   for (int i = 0; i < k; i++) { n     long long s, m; n     cin >> s >> m; n     demid.insert({s + m, i}); n   } n   for (int i = 0; i < n - k; i++) { n     long long s, m; n     pair<long long, long long> now; n     cin >> s >> m; n     now = *demid.begin(); n     demid.erase(demid.begin()); n     ans[now.second] = now.first; n     demid.insert({max(s, now.first) + m, i + k}); n   } n   while (demid.size()) { n     pair<long long, long long> now; n     now = *demid.begin(); n     demid.erase(demid.begin()); n     ans[now.second] = now.first; n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long int n; n   scanf( %ld , &n); n   long int a[n + 1], i, sum = 0; n   for (i = 0; i < n; i++) { n     scanf( %ld , &a[i]); n     sum = sum + a[i]; n   } n   if (sum % n == 0) { n     printf( %ld n , n); n   } else { n     printf( %ld n , n - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n long long a[N], f[N], g[N], r1, r2, r3, d; n int n; n long long nkb() { return min(r1, r2); } n long long cnkb(int i) { return min(r2, r1 * a[i] + nkb()); } n long long clear_go(int i) { n   return min(r1 * a[i] + r3, cnkb(i) + d + d + nkb()); n } n void Solve() { n   cin >> n >> r1 >> r2 >> r3 >> d; n   r1 = min(r1, r3); n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) f[i] = 1e18; n   f[0] = 0; n   f[1] = clear_go(1); n   for (int i = 2; i <= n; ++i) { n     f[i] = f[i - 1] + clear_go(i) + d; n     if (i > 1) { n       f[i] = min(f[i], f[i - 2] + cnkb(i - 1) + d + cnkb(i) + d + nkb() + d + n                            nkb() + d * (i != 2)); n     } n   } n   g[n + 1] = 0; n   g[n] = clear_go(n); n   for (int i = n - 1; i >= 1; --i) { n     g[i] = g[i + 1] + 2ll * d + min(cnkb(i) + nkb(), clear_go(i)); n   } n   long long ans = min(g[1], f[n]); n   for (int i = 1; i < n; ++i) { n     ans = min(ans, f[i] + d + g[i + 1]); n   } n   cout << ans <<   n ; n } n int main() { n   int test = 1; n   while (test--) Solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } else { n     return gcd(b, a % b); n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   long long i, j, k, n, m, t = 1; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     n = s.size(); n     map<char, long long> make_pair; n     long long mx = INT_MIN; n     for (i = 0; i < n; i++) { n       make_pair[s[i]]++; n     } n     for (char c =  0 ; c <=  9 ; c++) { n       if (make_pair[c] > mx) { n         mx = make_pair[c]; n       } n     } n     for (char c1 =  0 ; c1 <  9 ; c1++) { n       for (char c2 = c1 + 1; c2 <=  9 ; c2++) { n         k = 0; n         char prev =  $ ; n         for (i = 0; i < n; i++) { n           if (s[i] == c1) { n             if (prev == c2) { n               prev = c1; n               k++; n             } else if (prev ==  $ ) { n               prev = c1; n               k++; n             } n           } else if (s[i] == c2) { n             if (prev == c1) { n               prev = c2; n               k++; n             } else if (prev ==  $ ) { n               prev = c2; n               k++; n             } n           } n         } n         if (k > mx && k % 2 == 0) { n           mx = k; n         } else if ((k - 1) > mx && (k - 1) % 2 == 0) { n           mx = k - 1; n         } n       } n     } n     cout << s.size() - mx << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n struct point { n   int x, y; n   inline point operator+(const point &a) const { return {x + a.x, y + a.y}; } n   inline point operator-(const point &a) const { return {x - a.x, y - a.y}; } n   inline int operator^(const point &a) const { return x * a.x + y * a.y; } n   inline int operator*(const point &a) const { return x * a.y - y * a.x; } n   inline bool operator<(const point &a) const { n     return x == a.x ? y > a.y : x < a.x; n   } n }; n void input(point *arr, int n) { n   for (int i = 0; i < n; i++) scanf( %d%d , &arr[i].x, &arr[i].y); n } n void convex_hull(point *vec, int n, point *res, int &tp) { n   tp = 0; n   std::sort(vec, vec + n); n   for (int i = 0; i < n; i++) { n     while (tp >= 2 && (res[tp - 1] - vec[i]) * (res[tp - 2] - vec[i]) >= 0) n       tp--; n     res[tp++] = vec[i]; n   } n   int orig = tp--; n   for (int i = n - 1; i >= 0; i--) { n     while (tp > orig && (res[tp - 1] - vec[i]) * (res[tp - 2] - vec[i]) >= 0) n       tp--; n     res[tp++] = vec[i]; n   } n   tp--; n } n point edge[40005]; n int ecnt; n void get_edge(point *arr, int l, int r) { n   edge[ecnt++] = {l, r}; n   if (r - l + 1 <= 2) return; n   double mx = -1e18; n   int pos = -1; n   for (int i = l + 1; i < r; i++) { n     double d = (double)((arr[i] - arr[l]) ^ (arr[i] - arr[r])); n     d /= (arr[l] - arr[i]) * (arr[l] - arr[r]); n     if (d > mx) { n       mx = d; n       pos = i; n     } n   } n   get_edge(arr, l, pos); n   get_edge(arr, pos, r); n } n bool solve(point *A, int n, point *b, int m) { n   static point a[40005]; n   convex_hull(A, n, a, n); n   ecnt = 0; n   get_edge(a, 0, n - 1); n   for (int j = 0; j < ecnt; j++) { n     int l = edge[j].x, r = edge[j].y; n     double lp = -1e18, rp = 1e18; n     for (int i = 0; i < n && lp < rp; i++) { n       if (i == l || i == r) continue; n       int x = (a[i] - a[l]) ^ (a[i] - a[r]); n       int y = (a[l] - a[i]) * (a[l] - a[r]); n       if (!y) continue; n       double t = (double)x / y; n       if (y < 0) n         rp = std::min(rp, t); n       else n         lp = std::max(lp, t); n     } n     for (int i = 0; i < m && lp < rp; i++) { n       int x = (b[i] - a[l]) ^ (b[i] - a[r]); n       int y = (a[l] - b[i]) * (a[l] - a[r]); n       if (!y) { n         if (x > 0) continue; n         if (x <= 0) { n           lp = 1e18; n           break; n         } n       } n       double t = (double)x / y; n       if (y > 0) n         rp = std::min(rp, t); n       else n         lp = std::max(lp, t); n     } n     if (lp < rp) return true; n   } n   return false; n } n point a[40005], b[40005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   input(a, n); n   input(b, m); n   if (n == 1 || m == 1) { n     puts( YES ); n     return 0; n   } n   puts(solve(a, n, b, m) || solve(b, m, a, n) ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e6 + 10; n const int mod = 998244353; n long long a[maxn], b[maxn], n, m, sum1, sum2, res1, res2; n long long dp[3030][3030]; n inline long long qm(long long a, long long b, long long res = 1) { n   for (a %= mod; b; b >>= 1, a = a * a % mod) n     if (b & 1) res = res * a % mod; n   return res; n } n int main() { n   scanf( %I64d%I64d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &b[i]); n     if (a[i] == 1) n       sum1 += b[i]; n     else n       sum2 += b[i]; n   } n   dp[1][1] = sum1 * qm(sum1 + sum2, mod - 2) % mod; n   dp[1][0] = sum2 * qm(sum1 + sum2, mod - 2) % mod; n   for (int i = 1; i < m; i++) { n     for (int j = 0; j <= i; j++) { n       int tmp = i - j; n       dp[i + 1][j + 1] = n           (dp[i + 1][j + 1] + n            dp[i][j] * (sum1 + j) % mod * qm(sum1 + sum2 + j - tmp, mod - 2)) % n           mod; n       dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * max(0LL, sum2 - tmp) % mod * n                                          qm(sum1 + sum2 + j - tmp, mod - 2)) % n                      mod; n     } n   } n   for (int i = 0; i <= m; i++) { n     res1 = (res1 + dp[m][i] * (sum1 + i) % mod) % mod; n     res2 = (res2 + dp[m][i] * max(0LL, sum2 - m + i) % mod) % mod; n   } n   for (int i = 1; i <= n; i++) { n     if (a[i]) { n       printf( %I64d n , res1 * b[i] % mod * qm(sum1, mod - 2) % mod); n     } else { n       printf( %I64d n , res2 * b[i] % mod * qm(sum2, mod - 2) % mod); n     } n   } n } n


#include <bits/stdc++.h> n inline int mymin(int x, int y) { return x < y ? x : y; } n bool isprime[22000]; n inline void init_prime() { n   memset(isprime, true, sizeof(isprime)); n   for (int i = 2; i <= (int)sqrt(20000 * 1.0); ++i) n     if (isprime[i]) n       for (int j = 2; i * j <= 20000; ++j) isprime[i * j] = false; n } n struct edge { n   int to, c, next; n } e[22000]; n int n, id[220], a[220], head[220], h[220], q[220], cnt, tot, num[55], n     path[55][220]; n bool f[220]; n inline void print() { printf( Impossible n ); } n inline void ins(int u, int v, int c) { n   e[++cnt].to = v; n   e[cnt].c = c; n   e[cnt].next = head[u]; n   head[u] = cnt; n   e[++cnt].to = u; n   e[cnt].c = 0; n   e[cnt].next = head[v]; n   head[v] = cnt; n } n int dfs(int u, int in) { n   if (u == n + 1 || !in) return in; n   int out = 0; n   for (int i = head[u]; i; i = e[i].next) { n     int v = e[i].to, c = e[i].c; n     if (h[v] == h[u] + 1 && c) { n       int flow = dfs(v, mymin(in - out, c)); n       if (!flow) n         h[v] = 0; n       else n         out += flow; n       e[i].c -= flow; n       e[i ^ 1].c += flow; n       if (in == out) return out; n     } n   } n   return out; n } n inline bool bfs() { n   int op = 0, cl = 0; n   memset(h, 0, sizeof(h)); n   h[q[++cl] = 0] = 1; n   while (op < cl) { n     int u = q[++op]; n     if (u == n + 1) break; n     for (int i = head[u]; i; i = e[i].next) { n       int v = e[i].to, c = e[i].c; n       if (!h[v] && c) h[q[++cl] = v] = h[u] + 1; n     } n   } n   return h[n + 1]; n } n inline int Dinic() { n   int rec = 0; n   while (bfs()) rec += dfs(0, n); n   return rec; n } n int dfs_path(int x) { n   path[tot][++num[tot]] = x; n   int y; n   for (int i = head[x]; i; i = e[i].next) { n     if (e[i].c) continue; n     y = e[i].to; n     if (f[y]) continue; n     f[y] = true; n     path[tot][++num[tot]] = y; n     break; n   } n   for (int i = head[y]; i; i = e[i].next) { n     if (e[i ^ 1].c) continue; n     x = e[i].to; n     if (f[x]) continue; n     f[x] = true; n     dfs_path(x); n   } n } n int main() { n   scanf( %d , &n); n   if (n & 1) { n     print(); n     return 0; n   } n   int cnt1 = 0, cnt2 = n >> 1; n   for (int i = 1; i <= n; ++i) { n     int x; n     scanf( %d , &x); n     if (x & 1) { n       a[++cnt1] = x; n       id[cnt1] = i; n     } else { n       a[++cnt2] = x; n       id[cnt2] = i; n     } n     if (cnt1 > n >> 1 || cnt2 > n) { n       print(); n       return 0; n     } n   } n   init_prime(); n   memset(head, 0, sizeof(head)); n   cnt = 1; n   for (int i = 1; i <= (n >> 1); ++i) { n     ins(0, id[i], 2); n     for (int j = (n >> 1) + 1; j <= n; ++j) n       if (isprime[a[i] + a[j]]) ins(id[i], id[j], 1); n   } n   for (int i = (n >> 1) + 1; i <= n; ++i) ins(id[i], n + 1, 2); n   int maxflow = Dinic(); n   if (maxflow != n) { n     print(); n     return 0; n   } n   memset(f, false, sizeof(f)); n   f[0] = f[n + 1] = true; n   tot = 0; n   for (int i = 1; i <= (n >> 1); ++i) n     if (!f[id[i]]) { n       f[id[i]] = true; n       ++tot; n       num[tot] = 0; n       dfs_path(id[i]); n     } n   printf( %d n , tot); n   for (int i = 1; i <= tot; ++i) { n     printf( %d , num[i]); n     for (int j = 1; j <= num[i]; ++j) printf(  %d , path[i][j]); n     if (i != tot) printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n const T& max(const T& a, const T& b, const T& c) { n   return max(a, max(b, c)); n } n template <class T> n const T& min(const T& a, const T& b, const T& c) { n   return min(a, min(b, c)); n } n struct sp { n   bool operator()(const pair<string, int>& a, const pair<string, int>& b) { n     if (a.second != b.second) { n       return (a.second > b.second); n     } n     return (a.first < b.first); n   } n }; n long long stoi(string& str) { n   istringstream second(str); n   long long i; n   second >> i; n   return i; n } n string itos(long long i) { n   stringstream second; n   second << i; n   return second.str(); 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 lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long poww(long long a, long long b) { n   long long res = 1; n   for (int i = 1; i <= b; i++) res *= a; n   return res; n } n long long sumOfDigs(string s) { n   long long sum = 0; n   for (int i = 0; i < s.length(); i++) sum += s[i] -  0 ; n   return sum; n } n long long mod = 1000000007; n using namespace std; n long long a[100001]; n int fn(int x) { n   if (x == 0) n     return 0; n   else if (x & 1) { n     return (fn(x >> 1) + 1); n   } else n     return fn(x >> 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   int n; n   cin >> n; n   int x; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     a[fn(x)]++; n   } n   long long s = 0; n   for (int i = 0; i < 100001; i++) { n     if (a[i] > 1) { n       s += (a[i] * (a[i] - 1)) / 2; n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int ms = 1001000; n bool hole[ms]; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   long long n, m, k; n   cin >> n; n   if (n == 1 || n == 2) { n     cout << 0; n     return 0; n   } n   cout << (n + 1) / 2 - 1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, x, ans, num, n, fa[2001]; n   while (scanf( %d , &n) != EOF) { n     for (i = 1; i <= n; i++) { n       scanf( %d , &fa[i]); n     } n     ans = 1; n     for (i = 1; i <= n; i++) { n       num = 1; n       x = i; n       while (fa[x] != -1) { n         num++; n         x = fa[x]; n       } n       if (num > ans) ans = num; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const double eps = 1e-12; n int n, k, h; n struct lemming { n   int w, v, id; n   bool operator<(const lemming &rhs) const { n     if (w != rhs.w) return w < rhs.w; n     return v < rhs.v; n   } n } a[maxn]; n vector<int> ans; n int dcmp(double x) { n   if (fabs(x) < eps) return x; n   return x > 0 ? 1 : -1; n } n bool ok(double t) { n   vector<int> v; n   for (int i = 1, j = 1; i <= n; i++) { n     if (dcmp(t - (double)h * j / a[i].v) < 0) continue; n     v.emplace_back(a[i].id); n     j++; n     if (j > k) break; n   } n   if (v.size() < k) return 0; n   ans = v; n   return 1; n } n int main() { n   scanf( %d %d %d , &n, &k, &h); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i].w), a[i].id = i; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i].v); n   sort(a + 1, a + 1 + n); n   double l = 0, r = 1e9; n   for (int i = 0; i < 150; i++) { n     double mid = (l + r) / 2; n     if (ok(mid)) n       r = mid; n     else n       l = mid; n   } n   for (auto i : ans) printf( %d  , i); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110000; n char a[N]; n int chk[N]; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   cin >> a; n   if (n == 1) { n     cout <<  Yes  << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) chk[a[i]]++; n   bool flg = 1; n   for (int i = 0; i < 256; i++) { n     if (chk[i] != 1 && chk[i] != 0) flg = 0; n   } n   if (flg) n     cout <<  No  << endl; n   else n     cout <<  Yes  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; n const double eps = 1e-8; n void read(int &k) { n   k = 0; n   char x = getchar(); n   while (x <  0  || x >  9 ) x = getchar(); n   while (x >=  0  && x <=  9 ) { n     k = k * 10 - 48 + x; n     x = getchar(); n   } n } n long long mul[40][40], tmp[40][40], now[40], nxt[40]; n int L, R, no[10][10]; n long long powe(long long a, long long b) { n   long long ret = 1; n   for (; b; b >>= 1, a = a * a % MOD) n     if (b & 1) ret = ret * a % MOD; n   return ret; n } n long long solve1(int x) { n   if (x == 0) return 0; n   memset(mul, 0, sizeof mul); n   for (int i = 0; i < 5; i++) n     for (int j = 0; j < 5; j++) n       if (!no[i][j]) n         for (int k = 1; k < 5; k++) n           if (!no[j][k]) n             if ((i != 4 || j != 1 || k != 3) && (i != 3 || j != 1 || k != 4)) n               mul[i * 6 + j][j * 6 + k] = 1; n   for (int i = 0; i < 5; i++) n     for (int j = 0; j < 5; j++) mul[i * 6 + j][35] = 1; n   mul[35][35] = 1; n   memset(now, 0, sizeof now); n   now[0] = 1; n   for (; x; x >>= 1) { n     if (x & 1) { n       memset(nxt, 0, sizeof nxt); n       for (int k = 0; k < 36; k++) n         if (now[k]) n           for (int j = 0; j < 36; j++) n             nxt[j] = (nxt[j] + now[k] * mul[k][j]) % MOD; n       memcpy(now, nxt, sizeof now); n     } n     memset(tmp, 0, sizeof tmp); n     for (int i = 0; i < 36; i++) n       for (int k = 0; k < 36; k++) n         if (mul[i][k]) n           for (int j = 0; j < 36; j++) n             tmp[i][j] = (tmp[i][j] + mul[i][k] * mul[k][j]) % MOD; n     memcpy(mul, tmp, sizeof mul); n   } n   long long ret = 0; n   for (int i = 0; i < 36; i++) ret = (ret + now[i]) % MOD; n   return (ret + MOD - 1) % MOD; n } n long long solve2(int x) { n   long long ret = 0; n   x--; n   int y = x; n   for (int T = 1; T <= 4; T++) { n     x = y; n     memset(mul, 0, sizeof mul); n     for (int i = 0; i < 5; i++) n       for (int j = 0; j < 5; j++) n         if (!no[i][j]) n           for (int k = 1; k < 5; k++) n             if (!no[j][k]) n               if ((i != 4 || j != 1 || k != 3) && (i != 3 || j != 1 || k != 4)) n                 mul[i * 6 + j][j * 6 + k] = 1; n     for (int i = 0; i < 5; i++) n       for (int j = 0; j < 5; j++) mul[i * 6 + j][35] = 1; n     mul[35][35] = 1; n     memset(now, 0, sizeof now); n     now[T] = 1; n     for (; x; x >>= 1) { n       if (x & 1) { n         memset(nxt, 0, sizeof nxt); n         for (int k = 0; k < 36; k++) n           if (now[k]) n             for (int j = 0; j < 36; j++) n               nxt[j] = (nxt[j] + now[k] * mul[k][j]) % MOD; n         memcpy(now, nxt, sizeof now); n       } n       memset(tmp, 0, sizeof tmp); n       for (int i = 0; i < 36; i++) n         for (int k = 0; k < 36; k++) n           if (mul[i][k]) n             for (int j = 0; j < 36; j++) n               tmp[i][j] = (tmp[i][j] + mul[i][k] * mul[k][j]) % MOD; n       memcpy(mul, tmp, sizeof mul); n     } n     for (int i = 0; i < 36; i++) ret = (ret + now[i]) % MOD; n   } n   return (ret + MOD) % MOD; n } n long long calc(int x) { n   if (x == 0) return 0; n   return (solve1(x) + solve2((x + 1) / 2)) % MOD * powe(2, MOD - 2) % MOD; n } n int main() { n   cin >> L >> R; n   no[1][1] = no[2][2] = no[3][3] = no[4][4] = no[1][2] = no[3][4] = no[2][1] = n       no[4][3] = 1; n   cout << (calc(R) - calc(L - 1) + MOD) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 9; n int n, v[MAX]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0, x; i < n; i++) cin >> x, v[x % n] = i; n   for (int i = 0, x; i < n; i++) cin >> x, cout << v[x % n] + 1 <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000000 + 7; n const int N = 200000 + 10; n const int NUMHASH = 3; n const long long hashNum[3] = {M, M + 2, M + 14}; n struct re { n   int x, y; n }; n int n, q; n string s; n int cnt[26][N], f[26][N], g[26][N]; n long long h[NUMHASH][26][N], p[NUMHASH][N]; n int countLetter(int x, int y, int c) { return cnt[c][x] - cnt[c][y + 1]; } n int getHash(int x, int l, int c, int k) { n   return (h[k][c][x + l - 1] - (h[k][c][x - 1] * p[k][l] % hashNum[k]) + n           hashNum[k]) % n          hashNum[k]; n } n bool equalHash(int x, int i, int y, int j, int l) { n   for (int k = (0); (k <= (NUMHASH - 1)); ++k) { n     if (getHash(x, l, i, k) != getHash(y, l, j, k)) return false; n   } n   return true; n } n bool iso(int x, int y, int l) { n   for (int i = (0); (i <= (25)); ++i) { n     int pos = f[i][x]; n     if (pos < l) { n       int j = s[y - 1 + pos] -  a ; n       if (f[j][y] != pos) return false; n       if (countLetter(x, x + l - 1, i) != countLetter(y, y + l - 1, j)) n         return false; n       if (!equalHash(x, i, y, j, l)) return false; n     } n   } n   return true; n } n int main() { n   cin >> n >> q; n   cin >> s; n   for (int i = (n); (i >= (1)); --i) { n     for (int j = (0); (j <= (25)); ++j) n       cnt[j][i] = cnt[j][i + 1], f[j][i] = f[j][i + 1] + 1; n     int j = s[i - 1] -  a ; n     cnt[j][i] = cnt[j][i + 1] + 1; n     f[j][i] = 0; n     g[j][i] = 1; n   } n   for (int k = (0); (k <= (NUMHASH - 1)); ++k) { n     p[k][0] = 1; n     for (int i = (1); (i <= (n)); ++i) p[k][i] = (p[k][i - 1] * 2) % hashNum[k]; n     for (int j = (0); (j <= (25)); ++j) { n       for (int i = (1); (i <= (n)); ++i) n         h[k][j][i] = (h[k][j][i - 1] * 2 + g[j][i]) % hashNum[k]; n     } n   } n   while (q--) { n     int x, y, len; n     cin >> x >> y >> len; n     if (iso(x, y, len)) n       cout <<  YES ; n     else n       cout <<  NO ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, ans = -1; n   cin >> n; n   pair<int, int> g[n]; n   for (i = 0; i < n; i++) { n     cin >> g[i].first >> g[i].second; n   } n   sort(g, g + n); n   for (i = 0; i < n; i++) { n     if (ans <= g[i].second) n       ans = g[i].second; n     else n       ans = g[i].first; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, k; n   cin >> n >> k; n   int r[257] = {}; n   for (int i = 0; i < 257; i++) { n     r[i] = -1; n   } n   int p; n   for (int i = 0; i < n; i++) { n     cin >> p; n     if (r[p] == -1) { n       for (int j = (p - k + 1 >= 0) ? p - k + 1 : 0; j <= p; j++) { n         if (r[j] == -1) { n           for (int x = j; x <= p; x++) { n             if (r[x] != -1) break; n             r[x] = j; n           } n           break; n         } else if (r[j + 1] == -1) { n           if (r[j] + k - 1 >= p) { n             for (int x = j + 1; x <= p; x++) { n               r[x] = r[j]; n             } n             break; n           } n         } n       } n     } n     cout << r[p] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> p[300100]; n int per[300300]; n long long sz[300300]; n long long mx[300300]; n int res[300300]; n long long dfs(int x) { n   sz[x]++; n   for (int i = 0; i < p[x].size(); i++) { n     long long t = dfs(p[x][i]); n     sz[x] += t; n     mx[x] = max(mx[x], t); n   } n   return sz[x]; n } n bool ch(int x, int y) { n   if (mx[y] * 2 > sz[x]) return 0; n   if ((sz[x] - sz[y]) * 2 > sz[x]) return 0; n   return 1; n } n void f(int x) { n   int st = x, maxx = 0; n   if (ch(x, x)) res[x] = x; n   for (int i = 0; i < p[x].size(); i++) { n     f(p[x][i]); n     if (maxx < sz[p[x][i]]) { n       maxx = sz[p[x][i]]; n       st = res[p[x][i]]; n     } n   } n   while (1) { n     if (ch(x, st)) { n       res[x] = st; n       break; n     } n     st = per[st]; n   } n } n int main() { n   int n, q, z; n   cin >> n >> q; n   for (int i = 2; i <= n; i++) { n     scanf( %d , &z); n     per[i] = z; n     p[z].push_back(i); n   } n   dfs(1); n   f(1); n   for (int i = 0; i < q; i++) { n     scanf( %d , &z); n     printf( %d n , res[z]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1234567; n int a[N], gcd[N], is_max[N], cnt[N]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     a[i + n] = a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     if (n % i == 0) { n       for (int j = i; j <= n; j += i) { n         gcd[j] = i; n       } n     } n   } n   long long ans = 0ll; n   for (int i = 1; i <= n; ++i) { n     if (n % i == 0) { n       for (int j = 0; j < i; ++j) { n         int m = 0; n         for (int q = j; q < n; q += i) { n           m = max(m, a[q]); n         } n         for (int q = j; q < n * 2; q += i) { n           is_max[q] = a[q] == m; n         } n       } n       for (int j = 1; j < n * 2; ++j) { n         if (is_max[j]) { n           is_max[j] += is_max[j - 1]; n         } n       } n       cnt[0] = 0; n       for (int j = 1; j < n; ++j) { n         cnt[j] = cnt[j - 1] + (gcd[j] == i); n       } n       for (int j = n; j < n * 2; ++j) { n         ans += cnt[min(is_max[j], n - 1)]; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, ans = 0, f[30][30], h, t, l; n char st[20]; n int main() { n   scanf( %d n , &n); n   memset(f, 0, sizeof(f)); n   while (n--) { n     gets(st); n     l = strlen(st); n     h = (int)st[0] - 97; n     t = (int)st[l - 1] - 97; n     for (i = 0; i < 26; i++) n       if (f[i][h] && (f[i][t] < f[i][h] + l)) { n         f[i][t] = f[i][h] + l; n         if (t == i && ans < f[i][t]) ans = f[i][t]; n       } n     if (f[h][t] < l) { n       f[h][t] = l; n       if (t == h) ans = max(ans, l); n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e6 + 10; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n inline int read() { n   static char buf[1000000], *p1 = buf, *p2 = buf; n   register int x = false; n   register char ch = n       p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n           ? EOF n           : *p1++; n   ; n   register bool sgn = false; n   while (ch !=  -  && (ch <  0  || ch >  9 )) n     ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n              ? EOF n              : *p1++; n   ; n   if (ch ==  - ) n     sgn = true, n     ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n              ? EOF n              : *p1++; n   ; n   while (ch >=  0  && ch <=  9 ) n     x = (x << 1) + (x << 3) + (ch ^ 48), n     ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n              ? EOF n              : *p1++; n   ; n   return sgn ? -x : x; n } n long long fpow(long long a, int b, int mod) { n   long long res = 1; n   for (; b > 0; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long d[N]; n long long cnt[26]; n long long fac[N]; n char s[N]; n int len; n long long num[N]; n long long calc(int x) { n   long long tot = 0; n   for (int i = 0; i < x; i++) tot += cnt[i]; n   return tot; n } n long long dfs(int d) { n   if (d == len + 1) return 0; n   long long tot = calc(s[d] -  a ) * fac[len - d] % mod; n   if (cnt[s[d] -  a ]) { n     cnt[s[d] -  a ]--; n     long long u = dfs(d + 1); n     cnt[s[d] -  a ]++; n     tot = (tot + cnt[s[d] -  a ] * u % mod) % mod; n   } n   return tot; n } n int main() { n   fac[0] = 1; n   for (long long i = 1; i < N; i++) { n     fac[i] = (fac[i - 1] * i) % mod; n   } n   scanf( %s , s + 1); n   len = strlen(s + 1); n   for (int i = 1; i <= len; i++) cnt[s[i] -  a ]++; n   long long ans = dfs(1); n   scanf( %s , s + 1); n   ans = (dfs(1) - ans + mod) % mod; n   for (int i = 0; i < 26; i++) { n     if (cnt[i] >= 1) ans = ans * fpow(fac[cnt[i]], mod - 2, mod) % mod; n   } n   cout << ans - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   int p = 0; n   int aa = a; n   while (aa > 0) { n     p = max(p, aa % 10); n     aa = aa / 10; n   } n   int bb = b; n   while (bb > 0) { n     p = max(p, bb % 10); n     bb = bb / 10; n   } n   p++; n   int carry = 0; n   int num = 0; n   while (a > 0 or b > 0 or carry > 0) { n     carry = ((a % 10) + (b % 10) + carry) / p; n     a = a / 10; n     b = b / 10; n     num++; n   } n   cout << num << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long k, d, t, cnt = 0; n bool f(double T) { n   long long interval = ((long long)ceil(k / (d * 1.0))) * d; n   long long no_of_interval = T / interval; n   double t_on = no_of_interval * k, t_off = (interval - k) * no_of_interval; n   double rem = T - no_of_interval * interval; n   if (rem <= (double)k) n     t_on += rem; n   else { n     t_on += k; n     t_off += rem - k; n   } n   return t_on + t_off / 2 >= t; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> k >> d >> t; n   double s = t, e = 2 * t, mid; n   while (cnt++ <= 10000) { n     mid = (s + e) / 2; n     if (f(mid)) n       e = mid; n     else n       s = mid; n   } n   cout << setprecision(20) << mid; 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, rock = 0, peper = 0, sec = 0, de = 0; n     cin >> n; n     cin >> rock >> peper >> sec; n     string s, ans; n     cin >> s; n     for (long long int i = 0; i < n; i++) { n       if (s[i] ==  R  && peper > 0) { n         ans = ans +  P ; n         peper--; n         de++; n         continue; n       } else if (s[i] ==  P  && sec > 0) { n         sec--; n         ans = ans +  S ; n         de++; n         continue; n       } else if (s[i] ==  S  && rock > 0) { n         rock--; n         ans = ans +  R ; n         de++; n         continue; n       } else { n         ans = ans +  1 ; n       } n     } n     for (long long int i = 0; i < n; i++) { n       if (ans[i] ==  1 ) { n         if (rock > 0) { n           ans[i] =  R ; n           rock--; n         } else if (peper > 0) { n           ans[i] =  P ; n           peper--; n         } else if (sec > 0) { n           sec--; n           ans[i] =  S ; n         } n       } n     } n     if (n % 2 == 0) n       n = n / 2; n     else n       n = (n + 1) / 2; n     if (de >= n) { n       cout <<  YES  n            <<   n ; n       cout << ans <<   n ; n     } else n       cout <<  NO  n            <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n bool cmp_second(const pair<T, U> &a, const pair<T, U> &b) { n   return a.second < b.second; n } n pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n pair<int, int> operator-(const pair<int, int> &a, const pair<int, int> &b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n pair<int, int> &operator+=(pair<int, int> &a, const pair<int, int> &b) { n   a.first += b.first; n   a.second += b.second; n   return a; n } n pair<int, int> &operator-=(pair<int, int> &a, const pair<int, int> &b) { n   a.first -= b.first; n   a.second -= b.second; n   return a; n } n inline int sg(int x) { return x ? (x > 0 ? 1 : -1) : 0; } n const double pi = acos(-1.0); n int main(void) { n   double w, h, a; n   scanf( %lf %lf %lf , &w, &h, &a); n   if (a > 90.0) a = 180.0 - a; n   a = a / 180.0 * pi; n   double d = sqrt(w * w + h * h) * 0.5; n   double sol; n   if ((d * d + d * d - min(w, h) * min(w, h)) / (2 * d * d) <= cos(a)) { n     double A = 1 + cos(a); n     double B = sin(a); n     double x = (A * w - B * h) / (A * A - B * B); n     double y = (A * h - B * w) / (A * A - B * B); n     sol = w * h - (x * x + y * y) * sin(a) * cos(a); n   } else { n     double s = min(w, h); n     double z = s / sin(a); n     sol = z * z * sin(a); n   } n   printf( %.12lf n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 250000; n int n, k, C[30], c[30]; n vector<string> S; n bool anda, Z, term; n bool trythis(string X) { n   bool ans = true; n   for (int i = (0); i < (int)(k); i++) { n     int cnt = 0; n     for (int j = (0); j < (int)(n); j++) n       if (S[i][j] != X[j]) cnt++; n     if (!(cnt == 2 || (!cnt && Z))) ans = false; n   } n   return ans; n } n void proba(string X, int a, int b) { n   swap(X[a], X[b]); n   if (trythis(X)) { n     cout << X << endl; n     term = true; n   } n   swap(X[a], X[b]); n } n int main() { n   while (cin >> k >> n) { n     for (int i = (0); i < (int)(30); i++) C[i] = 0; n     anda = true; n     term = false; n     S.resize(k); n     for (int i = (0); i < (int)(k); i++) cin >> S[i]; n     for (int j = (0); j < (int)(n); j++) C[S[0][j] -  a ]++; n     for (int i = (0); i < (int)(k); i++) { n       for (int j = (0); j < (int)(30); j++) c[j] = 0; n       for (int j = (0); j < (int)(n); j++) c[S[i][j] -  a ]++; n       for (int j = (0); j < (int)(30); j++) n         if (C[j] != c[j]) anda = false; n     } n     if (!anda) { n       cout << -1 << endl; n       continue; n     } n     for (int i = (0); i < (int)(30); i++) n       if (C[i] > 1) Z = true; n     for (int i = (0); i < (int)(k); i++) n       for (int ii = (i + 1); ii < (int)(k); ii++) { n         int cnt = 0; n         for (int j = (0); j < (int)(n); j++) n           if (S[i][j] != S[ii][j]) cnt++; n         switch (cnt) { n           case 0: n             break; n           case 2: { n             vector<int> P; n             for (int j = (0); j < (int)(n); j++) n               if (S[i][j] != S[ii][j]) P.push_back(j); n             int t1 = P[0], t2 = P[1]; n             for (int c = (0); c < (int)(n); c++) { n               if (S[i][c] == S[i][t1]) { n                 proba(S[i], c, t2); n                 if (term) return 0; n               } n               if (S[i][c] == S[i][t2]) { n                 proba(S[i], c, t1); n                 if (term) return 0; n               } n             } n             proba(S[i], t1, t2); n             if (term) return 0; n             proba(S[ii], t1, t2); n             if (term) return 0; n             cout << -1 << endl; n             return 0; n           } n           case 3: { n             string X = S[i]; n             vector<int> P; n             for (int j = (0); j < (int)(n); j++) n               if (S[i][j] != S[ii][j]) P.push_back(j); n             for (int a = (0); a < (int)(3); a++) n               for (int b = (a + 1); b < (int)(3); b++) { n                 proba(X, P[a], P[b]); n                 if (term) return 0; n               } n             cout << -1 << endl; n             term = true; n             return 0; n           } n           case 4: { n             string X = S[i]; n             vector<int> P; n             for (int j = (0); j < (int)(n); j++) n               if (S[i][j] != S[ii][j]) P.push_back(j); n             for (int a = (0); a < (int)(4); a++) n               for (int b = (a + 1); b < (int)(4); b++) { n                 proba(X, P[a], P[b]); n                 if (term) return 0; n               } n             cout << -1 << endl; n             term = true; n             return 0; n           } n           default: { n             cout << -1 << endl; n             term = true; n             return 0; n           } n         } n       } n     if (!term) proba(S[0], 0, 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n int fac[100100]; n int n, m, i, x, j, l, r, p, ans, st, dr, mn, mx, M; n void init() { n   int i, j; n   for (i = 2; i <= 100000; i++) n     if (!fac[i]) { n       fac[i] = i; n       if (i > 316) continue; n       for (j = i * i; j <= 100000; j += i) n         if (!fac[j]) fac[j] = i; n     } n } n int solve(int x, int y) { n   int v[20]; n   int N; n   int i, j, lim, ind, nr, rez = 0; n   if (y == 0) return 0; n   v[0] = 0; n   N = 0; n   for (i = x; i > 1; i /= fac[i]) n     if (N == 0 || v[N - 1] != fac[i]) v[N++] = fac[i]; n   lim = 1 << N; n   for (i = 0; i < lim; i++) { n     ind = 0; n     nr = 1; n     for (j = 0; j < N; j++) n       if (i & (1 << j)) { n         ind++; n         nr = nr * v[j]; n       } n     int ax = y / nr; n     if (ind % 2 == 1) n       rez = (long long)((long long)((long long)rez - (long long)(M + 1) * ax + n                                     (long long)nr * ax * ((long long)ax + 1) / n                                         2) % n                             p + n                         (long long)p) % n             p; n     else n       rez = (long long)((long long)((long long)rez + (long long)(M + 1) * ax - n                                     (long long)nr * ax * ((long long)ax + 1) / n                                         2) % n                             p + n                         (long long)p) % n             p; n   } n   rez = (long long)((long long)rez * ((long long)n - x + 1)) % p; n   return ((int)rez); n } n int main() { n   scanf( %d%d%d%d%d , &n, &M, &l, &r, &p); n   init(); n   for (x = 1; x <= n; x++) { n     for (st = 0, dr = 100001; st < dr;) { n       m = (st + dr) / 2; n       if ((long long)x * x + (long long)m * m >= (long long)l * l) n         dr = m; n       else n         st = m + 1; n     } n     mn = st; n     for (st = 0, dr = 100001; st < dr;) { n       m = (st + dr) / 2 + 1; n       if ((long long)x * x + (long long)m * m <= (long long)r * r) n         st = m; n       else n         dr = m - 1; n     } n     if (mn < 1) mn = 1; n     mx = st; n     if (mx > M) mx = M; n     if (mn > mx) continue; n     ans = (long long)((long long)((long long)ans + solve(x, mx) - n                                   solve(x, mn - 1)) % n                           p + n                       p) % n           p; n   } n   ans = (long long)ans * 2 % p; n   if (l <= 1) n     ans = (long long)((long long)ans + n * ((long long)M + 1) + n                       ((long long)n + 1) * M) % n           p; n   printf( %d , ans); n   return 0; n } n


#include  bits/stdc++.h  n  n using namespace std; n using ll = long long; n using pii = pair<int,int>; n  n vector<tuple<int,int,int>> edges; n  n void add(int x, int y, int z) { n   edges.push_back({x,y,z}); n } n  n int solve(int l, int r) { n   if (l > 1) { n     int k = solve(1,r-l+1); n     add(k,k+1,l-1); n     return k+1; n   } n   if ((r&(-r)) == r) // r is a power of 2  n   { n     int k = __builtin_ctz(r); // Count Trailing Zeros n     add(1,2,1); n     for (int i = 3; i <= k+2; i++) { n       add(1,i,1); n       for (int j = 2; j < i; j++) n         add(j,i,1<<(j-2)); n     } n     return k+2; n   } n   int k =0; n   while(1<<(k+1) <= r-1) k++; n   solve(1,1<<k); n   add(1,k+3,1); n   for (int i = 0; i <= k; i++){ n     if ((r-1)>>i&1) n       add(i+2,k+3,1+((r-1)>>(i+1)<<(i+1))); n   } n   return k+3; n } n  n int main() n { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int l,r; n   cin >> l >> r; n   cout <<  YES n ; n   int n = solve(l,r); n   cout << n <<     << edges.size() <<   n ; n   for (auto [x,y,z] : edges) cout << x <<     << y <<     << z <<   n ; n } n  n // DAG n // Good if all paths from 1 to n are between L and R n // All possible distances should be represented, once each n // n // How many different distances can I have? n nodes an give 2^n-1 paths. Always n // possible? n // n // Seems like we can do binary magic to solve n //  n  n


#include <bits/stdc++.h> n using namespace std; n bitset<501> bt[1010]; n int c[505]; n int ans[250001], tot; n int main() { n   tot = 0; n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n   bt[0].set(0); n   int maxn = 0; n   for (int i = 1; i <= n; i++) { n     int lim = maxn; n     for (int j = lim; j >= 0; j--) { n       if (j + c[i] > k) continue; n       bt[j + c[i]] |= bt[j]; n       bt[j + c[i]] |= bt[j] << c[i]; n     } n     maxn = maxn + c[i]; n   } n   for (int i = 0; i <= 500; i++) { n     if (bt[k].test(i)) { n       ans[tot++] = i; n     } n   } n   printf( %d n , tot); n   for (int i = 0; i < tot; i++) { n     printf( %d , ans[i]); n     if (i == tot - 1) n       printf(  n ); n     else n       printf(   ); n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout << (n / 2) - 1 << endl; n   } else { n     cout << n / 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n long long a[200011]; n string s; n int main() { n   long long n, k, i, x, res = 0, j; n   cin >> n >> k; n   for (i = 0; i < n; i++) cin >> a[i]; n   cin >> s; n   char C =  A ; n   for (i = 0; i < n; i++) { n     x = k; n     if (s[i] == C) v.push_back(a[i]); n     if (s[i] != C || i == n - 1) { n       if (i == n - 1 && s[i] != C) res += a[i]; n       C = s[i]; n       sort(v.begin(), v.end()); n       if (v.size()) { n         for (j = v.size() - 1; j >= 0; j--) { n           if (x > 0) { n             res += v[j]; n             x--; n           } n         } n         v.erase(v.begin(), v.end()); n       } n       v.push_back(a[i]); n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 13; n int n, m; n int a[maxn]; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     long long n; n     long long x, y; n     scanf( %lld , &n); n     if (n == 1) { n       printf( 1 1 n ); n       continue; n     } n     long long p = sqrt(n); n     if (p * p != n) p++; n     if (p * p - n < p) { n       x = p; n       y = p * p - n + 1; n     } else { n       y = p; n       x = n - (p - 1) * (p - 1); n     } n     printf( %lld %lld n , x, y); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = ((int)2e6) + 5; n int energy[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, u; n   cin >> n >> u; n   for (int i = 0; i < n; i++) cin >> energy[i]; n   int curr = 2; n   double eff = -1; n   for (int i = 1; i < n - 1; i++) { n     int ei = energy[i - 1]; n     int ej = energy[i]; n     while (energy[curr] - ei > u) { n       curr--; n     } n     while (curr < n && energy[curr] - ei <= u) { n       eff = max(eff, (energy[curr] - ej) / (energy[curr] - ei + 0.0)); n       curr++; n     } n     if (curr == n) curr--; n   } n   if (eff < 1e-7) { n     cout << -1; n   } else { n     cout << fixed << setprecision(9) << eff; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { return getchar(); } n template <class T> n int read(T &ans) { n   ans = 0; n   char ch = gc(); n   T f = 1; n   while (!isdigit(ch)) { n     if (ch == EOF) return -1; n     if (ch ==  - ) f = -1; n     ch = gc(); n   } n   while (isdigit(ch)) ans = ans * 10 + ch -  0 , ch = gc(); n   ans *= f; n   return 1; n } n template <class T1, class T2> n int read(T1 &a, T2 &b) { n   return read(a) != EOF && read(b) != EOF ? 2 : EOF; n } n template <class T1, class T2, class T3> n int read(T1 &a, T2 &b, T3 &c) { n   return read(a, b) != EOF && read(c) != EOF ? 3 : EOF; n } n const int Maxn = 1100; n const int inf = 0x3f3f3f3f; n int a[Maxn], b[Maxn], c[Maxn], tims; n long long n, L, v; n pair<long long, long long> ans[Maxn]; n long long query(long long a, long long b) { n   printf( ? %I64d %I64d n , a, b); n   fflush(stdout); n   long long ans; n   read(ans); n   return ans; n } n long long find(int x, long long k, long long l, long long r) { n   long long mid = l + r >> 1; n   while (1) { n     long long y = query(x, mid); n     if (y == k) return mid; n     if (y < k) n       l = mid + 1; n     else n       r = mid - 1; n     mid = l + r >> 1; n   } n } n void kth(int l, int r, int k, long long ql, long long qr) { n   if (l == r) return; n   int x = rand() % (r - l + 1) + l; n   long long y = find(a[x], (L / n) * k, ql, qr); n   int mid = k - l + 1; n   int l1 = l - 1, l2 = r + 1, l3 = 0; n   c[++l3] = a[x]; n   for (int i = l; i <= r; i++) { n     if (i == x) continue; n     long long sxz = query(a[i], y); n     if (sxz < (L / n) * k) b[--l2] = a[i]; n     if (sxz == (L / n) * k) c[++l3] = a[i]; n     if (sxz > (L / n) * k) b[++l1] = a[i]; n   } n   while (l1 < k && l3) b[++l1] = c[l3--]; n   if (l1 == k) { n     v = y; n     while (l3) b[--l2] = c[l3--]; n     for (int i = l; i <= r; i++) a[i] = b[i]; n     return; n   } n   if (l1 < k) { n     for (int i = l; i <= r; i++) a[i] = b[i]; n     kth(l1 + 1, r, k, ql, qr); n     return; n   } n   while (l3) b[--l2] = c[l3--]; n   for (int i = l; i <= r; i++) a[i] = b[i]; n   kth(l, l1, k, ql, qr); n } n void solve(int l, int r, long long ql, long long qr) { n   if (l == r) { n     ans[a[l]] = make_pair(ql, qr); n     return; n   } n   int mid = l + r >> 1; n   kth(l, r, mid, ql, qr); n   long long x = v; n   solve(l, mid, ql, x); n   solve(mid + 1, r, x, qr); n } n signed main() { n   read(n, L); n   for (int i = 1; i <= n; i++) a[i] = i; n   solve(1, n, 0, 1000000000000000000ll); n   puts( ! ); n   for (int i = 1; i <= n; i++) n     printf( %I64d %I64d n , ans[i].first, ans[i].second); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x; n } n int t; n int main() { n   t = read(); n   while (t--) { n     int n = read(), d = read(); n     int ans = d; n     d--; n     int x = sqrt(d); n     if (x + d / (x + 1) + 1 < ans) ans = x + d / (x + 1) + 1; n     if (x + 1 + d / (x + 2) + 1 < ans) ans = x + 1 + d / (x + 2) + 1; n     if (ans <= n) n       puts( Yes ); n     else n       puts( No ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 100; n long long x[maxn]; n char type[maxn], t; n vector<long long> B, R, G; n long long szR, szB, n, pos; n void Put(long long st, long long en) { n   R.clear(); n   B.clear(); n   for (int i = st; i <= en; ++i) { n     if (type[i] == 1) n       R.push_back(i); n     else if (type[i] == 2) n       B.push_back(i); n   } n   szR = R.size(); n   szB = B.size(); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> pos >> t; n     x[i] = pos; n     if (t ==  P ) n       type[i] = 0, G.push_back(i); n     else if (t ==  R ) n       type[i] = 1; n     else n       type[i] = 2; n   } n   long long ans = 0; n   int sz = G.size(); n   if (sz == 0) { n     Put(1, n); n     if (szR > 0) ans += x[R[szR - 1]] - x[R[0]]; n     if (szB > 0) ans += x[B[szB - 1]] - x[B[0]]; n   } else { n     Put(1, G[0] - 1); n     if (szR > 0) ans += x[G[0]] - x[R[0]]; n     if (szB > 0) ans += x[G[0]] - x[B[0]]; n     Put(G[sz - 1] + 1, n); n     if (szR > 0) ans += x[R[szR - 1]] - x[G[sz - 1]]; n     if (szB > 0) ans += x[B[szB - 1]] - x[G[sz - 1]]; n     for (int i = 0; i < sz - 1; ++i) { n       long long x1 = G[i], x2 = G[i + 1]; n       long long tmp1 = 0, tmp2 = 0; n       Put(x1 + 1, x2 - 1); n       if (szR > 0) { n         tmp1 = x[R[0]] - x[x1]; n         for (int p = 1; p < szR; ++p) tmp1 = max(tmp1, x[R[p]] - x[R[p - 1]]); n         tmp1 = max(tmp1, x[x2] - x[R[szR - 1]]); n       } else n         tmp1 = x[x2] - x[x1]; n       if (szB > 0) { n         tmp2 = x[B[0]] - x[x1]; n         for (int p = 1; p < szB; ++p) tmp2 = max(tmp2, x[B[p]] - x[B[p - 1]]); n         tmp2 = max(tmp2, x[x2] - x[B[szB - 1]]); n       } else n         tmp2 = x[x2] - x[x1]; n       ans += min((x[x2] - x[x1]) * 2, (x[x2] - x[x1]) * 3 - tmp1 - tmp2); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool chkmax(T &x, T y) { n   return x < y ? x = y, true : false; n } n template <typename T> n bool chkmin(T &x, T y) { n   return x > y ? x = y, true : false; n } n int readint() { 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 const int cys = 998244353, g = 3, invg = (cys + 1) / 3; n int n; n long long ni[200015], fac[200015], inv[200015], tw[200005]; n int mod(int x) { return x >= cys ? x - cys : x; } n long long qpow(long long x, long long p) { n   long long ret = 1; n   for (; p; p >>= 1, x = x * x % cys) n     if (p & 1) ret = ret * x % cys; n   return ret; n } n vector<long long> operator+(vector<long long> a, vector<long long> b) { n   vector<long long> ret(max(a.size(), b.size())); n   for (int i = 0; i < ret.size(); i++) n     ret[i] = mod((i < a.size() ? a[i] : 0) + (i < b.size() ? b[i] : 0)); n   return ret; n } n vector<long long> operator-(vector<long long> a, vector<long long> b) { n   vector<long long> ret(max(a.size(), b.size())); n   for (int i = 0; i < ret.size(); i++) n     ret[i] = mod((i < a.size() ? a[i] : 0) + cys - (i < b.size() ? b[i] : 0)); n   return ret; n } n vector<long long> operator*(vector<long long> a, long long b) { n   for (int i = 0; i < a.size(); i++) a[i] = 1ll * a[i] * b % cys; n   return a; n } n vector<long long> operator>>(vector<long long> a, int b) { n   for (int i = 0; i < a.size() - b; i++) a[i] = a[i + b]; n   a.resize(a.size() - b); n   return a; n } n namespace poly { n int N, l; n int A[1100000], B[1100000], r[1100000], pre1[20][600000], pre2[20][600000]; n void pre() { n   for (int i = 1, r = 0; i < (1 << 19); i <<= 1, r++) { n     int w = 1, wn = qpow(g, (cys - 1) / (i << 1)); n     for (int j = 0; j < i; j++, w = 1ll * w * wn % cys) pre1[r][j] = w; n   } n   for (int i = 1, r = 0; i < (1 << 19); i <<= 1, r++) { n     int w = 1, wn = qpow(invg, (cys - 1) / (i << 1)); n     for (int j = 0; j < i; j++, w = 1ll * w * wn % cys) pre2[r][j] = w; n   } n } n void ntt(int *A, int N, int f) { n   for (int i = 0; i < N; i++) n     if (i < r[i]) swap(A[i], A[r[i]]); n   for (int i = 1, r = 0; i < N; i <<= 1, r++) { n     for (int j = 0; j < N; j += (i << 1)) { n       for (int k = j; k < j + i; k++) { n         int x = A[k], y = 1ll * A[k + i] * n                           (f > 0 ? pre1[r][k - j] : pre2[r][k - j]) % cys; n         A[k] = mod(x + y); n         A[k + i] = mod(x + cys - y); n       } n     } n   } n   if (f < 0) { n     int invn = qpow(N, cys - 2); n     for (int i = 0; i < N; i++) A[i] = 1ll * A[i] * invn % cys; n   } n } n void init(int t) { n   N = 1, l = 0; n   for (; N < t; N <<= 1) l++; n   for (int i = 0; i < N; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1)); n } n void getmul() { n   ntt(A, N, 1); n   ntt(B, N, 1); n   for (int i = 0; i < N; i++) A[i] = 1ll * A[i] * B[i] % cys; n   ntt(A, N, -1); n } n vector<long long> mul(vector<long long> a, vector<long long> b) { n   init(a.size() + b.size()); n   for (int i = 0; i < N; i++) A[i] = i < a.size() ? a[i] : 0; n   for (int i = 0; i < N; i++) B[i] = i < b.size() ? b[i] : 0; n   getmul(); n   vector<long long> ret(a.size() + b.size() - 1); n   for (int i = 0; i < ret.size(); i++) ret[i] = A[i]; n   return ret; n } n vector<long long> polyinv(vector<long long> a, int l) { n   if (l == 1) return vector<long long>(1, qpow(a[0], cys - 2)); n   a.resize(l); n   vector<long long> b = polyinv(a, (l + 1) / 2); n   init(l << 1); n   for (int i = 0; i < N; i++) A[i] = i < l ? a[i] : 0; n   for (int i = 0; i < N; i++) B[i] = i < (l + 1) / 2 ? b[i] : 0; n   ntt(A, N, 1); n   ntt(B, N, 1); n   for (int i = 0; i < N; i++) A[i] = 1ll * A[i] * B[i] % cys * B[i] % cys; n   ntt(A, N, -1); n   vector<long long> t = b * 2; n   t.resize(l); n   for (int i = 0; i < l; i++) t[i] = mod(t[i] + cys - A[i]); n   return t; n } n vector<long long> polydiff(vector<long long> a) { n   for (int i = 0; i < a.size() - 1; i++) a[i] = 1ll * (i + 1) * a[i + 1] % cys; n   a.resize(a.size() - 1); n   return a; n } n vector<long long> polyint(vector<long long> a) { n   a.resize(a.size() + 1); n   for (int i = a.size() - 1; i >= 1; i--) a[i] = 1ll * a[i - 1] * ni[i] % cys; n   a[0] = 0; n   return a; n } n vector<long long> polyln(vector<long long> a, int l) { n   return polyint(mul(polydiff(a), polyinv(a, l))); n } n vector<long long> polyexp(vector<long long> a, int l) { n   if (l == 1) return vector<long long>(1, 1); n   a.resize(l); n   vector<long long> b = polyexp(a, (l + 1) / 2); n   init(l << 1); n   vector<long long> t = mul(b, vector<long long>(1, 1) - polyln(b, l) + a); n   t.resize(l); n   return t; n } n vector<long long> polypow(vector<long long> a, int l, int k) { n   return polyexp(polyln(a, l) * k, l); n } n }  // namespace poly n vector<long long> getans() { n   vector<long long> f(0); n   for (int i = 0; i <= n + 1; i++) f.push_back(inv[i + 1]); n   vector<long long> tmp = n       poly::mul(f, poly::polyinv((vector<long long>(1, 1) - f) >> 1, n + 1)); n   vector<long long> tw(0); n   tw.resize(n); n   for (int i = 0; i < n; i++) tw[i] = tmp[i + 1]; n   vector<long long> h(0); n   h.push_back(1), h.push_back(1); n   h = poly::polyinv(poly::polyln(h, n + 3) >> 1, n + 2); n   vector<long long> g = n       poly::polyinv((vector<long long>(1, 1) - h) >> 1, n + 1); n   vector<long long> ph = poly::polypow(h, n + 1, n + 1); n   vector<long long> t1 = poly::mul(g, ph); n   vector<long long> tmp1 = poly::mul(poly::polydiff(h), ph); n   tmp1.resize(n + 1); n   vector<long long> tmp2 = poly::mul(g, g); n   tmp2.resize(n + 1); n   vector<long long> t2 = poly::mul(tmp1, tmp2); n   for (int i = 0; i < n; i++) n     tw[i] = n         mod(tw[i] + cys - n             ni[n + 1] * mod(t1[i + 1] * (n - i + 1) % cys + t2[i + 1]) % cys); n   for (int i = 0; i < n; i++) tw[i] = tw[i] * fac[i] % cys; n   reverse(tw.begin(), tw.end()); n   tmp.clear(); n   for (int i = 0; i < n; i++) tmp.push_back(i & 1 ? cys - inv[i] : inv[i]); n   tw = poly::mul(tw, tmp); n   tw.resize(n); n   reverse(tw.begin(), tw.end()); n   return tw; n } n int main() { n   poly::pre(); n   n = readint(); n   fac[0] = inv[0] = 1; n   for (int i = 1; i <= n + 5; i++) fac[i] = fac[i - 1] * i % cys; n   inv[n + 5] = qpow(fac[n + 5], cys - 2); n   for (int i = n + 4; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % cys; n   for (int i = 1; i <= n + 5; i++) ni[i] = inv[i] * fac[i - 1] % cys; n   vector<long long> res = getans(); n   for (int i = 0; i < n; i++) n     printf( %lld  , res[i] * fac[n] % cys * inv[i] % cys); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int n; n vector<int> a; n long long inv[32][2]; n void solve(vector<int> &a, int depth) { n   if (depth < 0) return; n   if (a.size() == 0) return; n   vector<int> left, right; n   long long cnt0 = 0, cnt1 = 0, sum01 = 0, sum10 = 0; n   for (int i = 0; i < a.size(); ++i) { n     if (a[i] >> depth & 1) { n       cnt1++; n       sum01 += cnt0; n       right.push_back(a[i]); n     } else { n       cnt0++; n       sum10 += cnt1; n       left.push_back(a[i]); n     } n   } n   inv[depth][0] += sum01; n   inv[depth][1] += sum10; n   solve(left, depth - 1); n   solve(right, depth - 1); n } n int main() { n   scanf( %d , &n); n   a.resize(n); n   for (auto &x : a) cin >> x; n   solve(a, 31); n   long long ans = 0, x = 0; n   for (int i = 0; i <= 31; ++i) { n     if (inv[i][0] >= inv[i][1]) n       ans += inv[i][1]; n     else n       ans += inv[i][0], x |= (1 << i); n   } n   printf( %lld %lld n , ans, x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = (int)1e9 + 7; n char s[5010], t[5010]; n int n, m; n int d[2][5010], ans, cs; n int main() { n   int i, j; n   gets(s + 1); n   n = strlen(s + 1); n   gets(t + 1); n   m = strlen(t + 1); n   d[1][m + 1] = 1; n   for (i = n; i >= 1; i--) { n     cs = 0; n     for (j = m + 1; j >= 1; j--) { n       if (s[i] == t[j]) n         d[0][j] = cs; n       else n         d[0][j] = 0; n       cs += d[1][j]; n       cs %= mod; n     } n     for (j = 1; j <= m; j++) { n       ans += d[0][j]; n       ans %= mod; n     } n     for (j = 1; j <= m; j++) d[1][j] = d[0][j]; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, ans = 0; n const long long maxn = 1e6 + 5; n long long p[maxn]; n int main() { n   cin >> a >> b >> c >> d; n   long long mx = min(b - a, c - b) + 1; n   for (long long i = a + b, t = 1; i <= b + c; ++i) { n     p[i] = t; n     t = min(t + 1, mx); n   } n   for (long long i = b + c - mx + 1, t = mx; i <= b + c; ++i) { n     p[i] = t; n     --t; n   } n   for (long long i = a + b; i <= b + c; ++i) { n     p[i] = p[i - 1] + p[i]; n   } n   ans = p[b + c] * (d - c + 1); n   for (long long i = c; i <= d; ++i) { n     long long ind = min(b + c, i); n     ans -= p[ind]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long int n, k; n   cin >> n >> k; n   long long int q = 8 * (n + k) + 9; n   long long int y = (-3 + sqrt(q)) / 2; n   cout << (y * (y + 1)) / 2 - k <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n int a[300005]; n bool notprime[8000005]; n map<int, int> prims; n vector<int> primes; n void factor(int num) { n   for (int i = 0; i < primes.size() && primes[i] * primes[i] <= num; i++) { n     if (num % primes[i] == 0) { n       prims[primes[i]]++; n       while (num % primes[i] == 0) { n         num /= primes[i]; n       } n       if (num == 1) { n         break; n       } n     } n   } n   if (num != 1) prims[num]++; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   notprime[0] = true; n   notprime[1] = true; n   int i; n   for (i = 2; i * i <= 8000005; i++) { n     if (!notprime[i]) { n       primes.push_back(i); n       for (int j = i * i; j < 8000005; j += i) { n         notprime[j] = true; n       } n     } n   } n   for (; i < 8000005; i++) { n     if (!notprime[i]) primes.push_back(i); n   } n   int n; n   cin >> n; n   int commgcd; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i == 0) n       commgcd = a[i]; n     else n       commgcd = gcd(a[i], commgcd); n   } n   for (int i = 0; i < n; i++) { n     factor(a[i] / commgcd); n   } n   int maxprim = -1; n   for (auto par : prims) { n     if (par.second > maxprim) { n       maxprim = par.second; n     } n   } n   if (maxprim == -1) n     cout << -1 << endl; n   else n     cout << n - maxprim << endl; n } n


#include <bits/stdc++.h> n int t1, t2; n inline int get() { n   char c[10]; n   scanf( %s , &c); n   if (!strcmp(c,  monday )) return 1; n   if (!strcmp(c,  tuesday )) return 2; n   if (!strcmp(c,  wednesday )) return 3; n   if (!strcmp(c,  thursday )) return 4; n   if (!strcmp(c,  friday )) return 5; n   if (!strcmp(c,  saturday )) return 6; n   if (!strcmp(c,  sunday )) return 0; n } n int main() { n   t1 = get(), t2 = get(); n   if ((t1 == t2) || ((t1 + 2) % 7 == t2) || ((t1 + 3) % 7 == t2)) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long i, j, a, n = 0; n   string s, str; n   cin >> a; n   cin >> s >> str; n   for (long long i = 0; i < s.size() - 1; i++) { n     if (s[i] == str[i]) continue; n     if (s[i] != str[i] and s[i + 1] != str[i + 1]) { n       swap(s[i], s[i + 1]); n       n++; n       continue; n     } n     n++; n   } n   if (s[a - 1] != str[a - 1]) n++; n   cout << n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n char str[100005]; n int main() { n   scanf( %d %d %d , &n, &a, &b); n   scanf( %s , str + 1); n   if (str[a] == str[b]) { n     puts( 0 ); n     return 0; n   } n   puts( 1 ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x(0); n   cin >> n; n   string s; n   while (n--) { n     cin >> s; n     if (s[1] ==  + ) { n       ++x; n     } else { n       --x; n     } n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int main() { n   cin >> n >> m; n   int bagi = (n / m); n   if (bagi * m == n) { n     for (int i = 0; i < m; i++) { n       printf( %d  , bagi); n     } n   } else { n     int sisa = (n - (m * bagi)); n     for (int i = 0; i < m; i++) { n       int now = 0; n       if (sisa > 0) { n         now = bagi + 1; n         sisa--; n       } else { n         now = bagi; n       } n       printf( %d  , now); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     pair<int, int> p[n]; n     for (int i = 0; i < n; i++) { n       cin >> p[i].first >> p[i].second; n     } n     bool check = true; n     for (int i = 0; i < n; i++) { n       if (p[i].first < p[i].second) check = false; n       if (i > 0) { n         if (p[i].first == p[i - 1].first) { n           if (p[i].second != p[i - 1].second) check = false; n         } else if (p[i].first > p[i - 1].first) { n           if (p[i].second < p[i - 1].second) check = false; n         } else n           check = false; n         if (p[i].first - p[i - 1].first < p[i].second - p[i - 1].second) n           check = false; n       } n     } n     if (check) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 100003; n long long f[20 * mxn + 10], rs[2 * mxn], ar[mxn + 10]; n long long sum = 0; n int n, m, k; n struct data { n   int l, r, id; n } query[mxn]; n int bs = sqrt(mxn * 14); n bool operator<(data a, data b) { n   return a.l / bs == b.l / bs ? a.r < b.r : a.l < b.l; n } n void add(int pos) { n   sum += f[ar[pos] ^ k]; n   f[ar[pos]]++; n } n void rmv(int pos) { n   f[ar[pos]]--; n   sum -= f[ar[pos] ^ k]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) cin >> ar[i]; n   for (int i = 1; i <= n; i++) { n     ar[i] = ar[i - 1] ^ ar[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> query[i].l >> query[i].r; n     query[i].l--; n     query[i].id = i; n   } n   sort(query, query + m); n   int left = 1, right = 1; n   add(1); n   for (int i = 0; i < m; i++) { n     while (left < query[i].l) { n       rmv(left); n       left++; n     } n     while (left > query[i].l) { n       left--; n       add(left); n     } n     while (right < query[i].r) { n       right++; n       add(right); n     } n     while (right > query[i].r) { n       rmv(right); n       right--; n     } n     rs[query[i].id] = sum; n   } n   for (int i = 0; i < m; i++) { n     cout << rs[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> a[3]; n     for (int i = 0; i < 3; i++) { n       a[i].assign(n, 0); n       for (auto &x : a[i]) cin >> x; n     } n     vector<int> res; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < 3; j++) { n         if (!res.empty() && res.back() == a[j][i]) continue; n         if (i == n - 1 && !res.empty() && res[0] == a[j][i]) continue; n         res.push_back(a[j][i]); n         break; n       } n     } n     for (auto &x : res) cout << x <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int N = 2e5 + 5; n const int SZ = 1e6 + 5; n const long long INF = 2e9 + 5; n int n, m, p; n pair<int, long long> a[N], b[N]; n pair<int, pair<int, long long> > mon[N]; n long long tree[4 * SZ], lazy[4 * SZ], cost[SZ]; n void Build(int c, int ss, int se) { n   if (ss == se) { n     tree[c] = cost[ss]; n     return; n   } n   int mid = (ss + se) >> 1; n   Build(c << 1, ss, mid); n   Build(c << 1 | 1, mid + 1, se); n   tree[c] = max(tree[c << 1], tree[c << 1 | 1]); n } n void Push(int c, int ss, int se) { n   if (lazy[c]) { n     tree[c] += lazy[c]; n     if (ss != se) { n       lazy[c << 1] += lazy[c]; n       lazy[c << 1 | 1] += lazy[c]; n     } n     lazy[c] = 0; n   } n } n void Set(int c, int ss, int se, int qs, int qe, long long val) { n   Push(c, ss, se); n   if (ss > se || qs > se || qe < ss) return; n   if (qs <= ss && se <= qe) { n     lazy[c] += val; n     Push(c, ss, se); n     return; n   } n   int mid = (ss + se) >> 1; n   Set(c << 1, ss, mid, qs, qe, val); n   Set(c << 1 | 1, mid + 1, se, qs, qe, val); n   tree[c] = max(tree[c << 1], tree[c << 1 | 1]); n } n long long Get(int c, int ss, int se, int qs, int qe) { n   Push(c, ss, se); n   if (ss > se || qs > se || qe < ss) return -INF; n   if (qs <= ss && se <= qe) return tree[c]; n   int mid = (ss + se) >> 1; n   return max(Get(c << 1, ss, mid, qs, qe), n              Get(c << 1 | 1, mid + 1, se, qs, qe)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> p; n   for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; n   for (int i = 1; i <= m; i++) cin >> b[i].first >> b[i].second; n   for (int i = 1; i <= p; i++) n     cin >> mon[i].first >> mon[i].second.first >> mon[i].second.second; n   for (int i = 1; i < SZ; i++) cost[i] = -INF; n   for (int i = 1; i <= m; i++) { n     cost[b[i].first] = max(cost[b[i].first], -b[i].second); n   } n   sort(a + 1, a + 1 + n); n   sort(mon + 1, mon + 1 + p); n   Build(1, 1, SZ - 1); n   int j = 0; n   long long ans = -2 * INF; n   for (int i = 1; i <= n; i++) { n     while (j + 1 <= p && a[i].first > mon[j + 1].first) { n       j++; n       Set(1, 1, SZ - 1, mon[j].second.first + 1, SZ, mon[j].second.second); n     } n     ans = max(ans, -a[i].second + Get(1, 1, SZ - 1, 1, SZ - 1)); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10, mod = 1e9 + 7; n int a[N]; n int qmi(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = (long long)res * a % mod; n     b >>= 1; n     a = (long long)a * a % mod; n   } n   return res; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n, k; n     scanf( %d%d , &n, &k); n     for (int i = 1; i <= n; i++) scanf( %d , a + i); n     if (k == 1) { n       printf(n & 1 ?  1  :  0 ); n       puts(  ); n       continue; n     } n     sort(a + 1, a + 1 + n); n     int sum = 1, ans = 0, t = 1; n     bool flag = true; n     for (int i = n - 1; i >= 1; i--) { n       int d = a[i + 1] - a[i]; n       if (sum > 0) { n         while (sum < N && d) { n           if ((long long)sum * k > N) n             sum = N * 2; n           else n             sum = sum * k; n           d--; n           t = (long long)t * k % mod; n         } n       } n       t = (long long)t * qmi(k, d) % mod; n       if (sum) n         sum--, t = ((long long)t + mod - 1) % mod; n       else n         sum = 1, t = 1; n     } n     printf( %lld n , (long long)qmi(k, a[1]) * t % mod); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 1000; n char s1[maxn]; n char s2[maxn]; n int judge(int st1, int en1, int st2, int en2) { n   int sign = 0; n   for (int i = st1, j = st2; i <= en1; i++, j++) { n     if (s1[i] != s2[j]) { n       sign = 1; n       break; n     } n   } n   if (sign == 0) n     return 1; n   else { n     if ((en1 - st1 + 1) % 2 == 0) { n       int mid1 = st1 + (en1 - st1 + 1) / 2 - 1; n       int mid2 = st2 + (en2 - st2 + 1) / 2 - 1; n       if (judge(st1, mid1, st2, mid2) && judge(mid1 + 1, en1, mid2 + 1, en2)) n         return 1; n       if (judge(st1, mid1, mid2 + 1, en2) && judge(mid1 + 1, en1, st2, mid2)) n         return 1; n     } n   } n   return 0; n } n int main() { n   int len1, len2; n   scanf( %s%s , s1, s2); n   len1 = strlen(s1); n   len2 = strlen(s2); n   if (len1 != len2) n     cout <<  NO n  << endl; n   else { n     int sign; n     sign = judge(0, len1 - 1, 0, len1 - 1); n     if (sign) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   while (cin >> n) { n     long long int lcm = 2520; n     cout << n / lcm << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string st; n set<string> S; n map<string, int> mp; n map<string, string> mp2; n int n, q; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     S.clear(); n     cin >> st; n     int len = st.length(); n     for (int fr = 0; fr < len; fr++) n       for (int ed = fr; ed < len; ed++) { n         string sb = st.substr(fr, ed - fr + 1); n         if (S.count(sb)) { n         } else { n           S.insert(sb); n           mp[sb]++; n           mp2[sb] = st; n         } n       } n   } n   cin >> q; n   while (q--) { n     cin >> st; n     string attr2 =  - ; n     if (mp2.count(st)) attr2 = mp2[st]; n     int attr1 = 0; n     if (mp.count(st)) attr1 = mp[st]; n     cout << attr1 <<     << attr2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tot; n int num[1008]; n int is[1005]; n int n, pos[1005][5]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 5; j++) { n       scanf( %d , pos[i] + j); n     } n   } n   int flag = 0; n   int yes = 0; n   if (n < 3) { n     if (n == 1) { n       printf( 1 n1 n ); n     } else { n       printf( 2 n1 2 n ); n     } n     return 0; n   } n   for (int i = 0; i < n; i++) { n     flag = 1; n     if (is[i]) continue; n     is[i] = 1; n     for (int j = 0; j < n && flag; j++) { n       for (int k = 0; k < n && flag; k++) { n         if (i == j || i == k || k == j) { n           continue; n         } n         int x[5] = {0}, y[5] = {0}; n         for (int m = 0; m < 5; m++) { n           x[m] = pos[j][m] - pos[i][m]; n         } n         for (int m = 0; m < 5; m++) { n           y[m] = pos[k][m] - pos[i][m]; n         } n         int temp = 0; n         for (int m = 0; m < 5; m++) { n           temp += x[m] * y[m]; n         } n         if (temp <= 0) { n           is[j] = 1; n           is[k] = 1; n         } else { n           is[i] = 1; n           flag = 0; n         } n       } n     } n     if (flag == 1) { n       printf( 1 n%d , i + 1); n       yes = 1; n       break; n     } n   } n   if (yes == 0) { n     printf( 0 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int x[200010], check[200010]; n long long int ans[200010]; n long long int size[200010]; n int next[200010]; n int find(int k) { n   if (next[k] == k) n     return k; n   else n     return next[k] = find(next[k]); n } n std::vector<int> V[200010]; n int height[200010]; n long long int func(int k, int prev, int h) { n   height[k] = h; n   long long int ans = x[k]; n   for (int i = 0; i < V[k].size(); i++) { n     if (V[k][i] == prev) continue; n     ans += func(V[k][i], k, h + 1); n   } n   return size[k] = ans; n } n std::pair<int, int> edge[200010]; n int main() { n   int a; n   scanf( %d , &a); n   for (int i = 1; i <= a; i++) scanf( %d , &x[i]); n   for (int i = 1; i <= a; i++) next[i] = i; n   int b; n   scanf( %d , &b); n   for (int i = 1; i <= b; i++) { n     int c, d; n     scanf( %d%d , &c, &d); n     edge[i] = std::make_pair(c, d); n     if (find(c) == find(d)) n       check[i] = 1, ans[i] = 0; n     else { n       V[c].push_back(d); n       V[d].push_back(c); n       next[find(c)] = find(d); n     } n   } n   func(1, 0, 1); n   long long int sum = 0; n   for (int i = 1; i <= a; i++) sum += x[i]; n   if (sum != 0) { n     printf( Impossible ); n     return 0; n   } n   for (int i = 1; i <= b; i++) { n     if (check[i] == 0) { n       long long int s, t; n       int c = edge[i].first; n       int d = edge[i].second; n       if (height[d] < height[c]) n         ans[i] = size[c]; n       else n         ans[i] = -size[d]; n     } n   } n   printf( Possible n ); n   for (int i = 1; i <= b; i++) printf( %lld n , -ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, *a, q, sum = 0; n   cin >> n; n   a = new long long int[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   sort(a, a + n); n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> q; n     cout << sum - a[n - q] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<pair<int, int> > st; n int main() { n   int n, k; n   cin >> n >> k; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     int num; n     cin >> num; n     cnt += num / 10; n     if (num < 100) st.insert(make_pair(num % 10, num)); n   } n   while (st.size() > 0 && (*st.rbegin()).first != 0 && k > 0) { n     pair<int, int> num = *st.rbegin(); n     st.erase(st.find(num)); n     num.first++; n     num.second++; n     if (num.first == 10) cnt++; n     num.first %= 10; n     if (num.second < 100) st.insert(num); n     k--; n   } n   while (st.size() > 0) { n     pair<int, int> num = *st.begin(); n     st.erase(st.find(num)); n     while (num.second < 100 && k > 0) { n       k--; n       num.second++; n       num.first = (num.first + 1) % 10; n       if (num.first == 0) cnt++; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n long long n, t[N], m, res, ans[N]; n int dd[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> t[i]; n   sort(t + 1, t + n + 1); n   long long sum = 0; n   int j = 1; n   for (int i = 1; i <= 200000; i++) { n     if (sum + i > m) { n       res = i - 1; n       break; n     } n     if (j <= n && t[j] == i) n       j++; n     else n       sum += i; n   } n   int dem = 0; n   j = 1; n   for (int i = 1; i <= res; i++) { n     if (j > n || i != t[j]) { n       dem++; n       ans[dem] = i; n     } else n       j++; n   } n   cout << dem << endl; n   for (int i = 1; i <= dem; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long poww(long long x, long long y) { n   if (x == 0) return 0; n   if (x == 1) return 1; n   if (y == 0) return 1; n   if (y == 1) return x; n   long long d = poww(x, y / 2); n   if (y % 2) return x * d * d; n   return d * d; n } n int main() { n   long long n, m; n   cin >> n >> m; n   for (int i = 0; i < (n + m) * 2; i++) { n     long long aa; n     cin >> aa; n   } n   if (n == m) { n     cout <<  Yes  << endl; n   } else { n     cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int one[100007], zero[100007]; n const int mod = 1e9 + 7; n long long FastPow(long long x, long long n, long long mod) { n   long long s = 1; n   while (n) { n     if (n & 1) s = s * x % mod; n     x = x * x % mod; n     n >>= 1; n   } n   return s % mod; n } n int main() { n   int n, q; n   cin >> n >> q; n   string s; n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     if (i > 0) { n       zero[i + 1] = zero[i]; n       one[i + 1] = one[i]; n     } n     if (s[i] ==  0 ) { n       zero[i + 1]++; n     } else { n       one[i + 1]++; n     } n   } n   while (q--) { n     int l, r; n     cin >> l >> r; n     long long ans = (FastPow(2, one[r] - one[l - 1], mod) - 1 + mod) % mod * n                     FastPow(2, zero[r] - zero[l - 1], mod) % mod; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int power(unsigned long long int n, n                              unsigned long long int p) { n   unsigned long long int m = 1; n   while (p > 0) { n     m *= n; n     p--; n   } n   return m; n } n void sieveOfEratosthenes(int n, bool prime[]) { n   int i, p; n   for (i = 2; i * i <= n; i++) { n     if (prime[i]) { n       for (p = i * i; p <= n; p += i) prime[p] = false; n     } n   } n } n long long int coins(int n, int a[]) { n   int res, mn = INT_MAX; n   res = mn = -a[0]; n   for (int i = 0; i < n - 1; ++i) { n     mn = min(mn, res); n     res += a[i] - a[i + 1]; n   } n   return max(abs(res), abs(mn)); n } n int main() { n   int t, i, n, m, d, k, p; n   long long int l, r, ret; n   cin >> n; n   int arr[n]; n   for (i = 0; i < n; i++) cin >> arr[i]; n   cout << coins(n, arr); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100; n int main() { n   long long n, m, a[MAXN], st[MAXN]; n   scanf( %I64d %I64d , &n, &m); n   st[0] = 1; n   for (int i = 1; i <= n; i++) st[i] = st[i - 1] * 2; n   int l = 0; n   int r = n; n   for (int i = 1; i < n; i++) { n     if (m > st[n - i - 1]) { n       a[r - 1] = i; n       r--; n       m -= st[n - i - 1]; n     } else { n       a[l] = i; n       l++; n     } n   } n   a[l] = n; n   for (int i = 0; i < n; i++) printf( %I64d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int v; n   Edge* next; n }; n const int N = 100010; n int n, k; n bool vis[N]; n int dis[N], prt[N], deg[N]; n Edge* g[N]; n void adde(int u, int v); n void dfs(int u); n void pred(int u, int r); n int main() { n   scanf( %d%d , &n, &k); n   for (int rep = 1; rep < n; ++rep) { n     int u, v; n     scanf( %d%d , &u, &v); n     adde(u, v); n     adde(v, u); n   } n   dfs(1); n   int u = max_element(dis + 1, dis + n + 1) - dis; n   memset(vis, 0, sizeof(vis)); n   dis[u] = 0; n   prt[u] = 0; n   dfs(u); n   int m = max_element(dis + 1, dis + n + 1) - dis; n   if (dis[m] != k * 2) { n     puts( No ); n     return 0; n   } n   int cnt = k; n   while (cnt--) m = prt[m]; n   memset(vis, 0, sizeof(vis)); n   pred(m, k); n   puts( YES ); n   return 0; n } n void pred(int u, int r) { n   vis[u] = true; n   if (r == 0) { n     if (deg[u]) { n       puts( NO ); n       exit(0); n     } n     return; n   } n   if (deg[u] < 3) { n     puts( NO ); n     exit(0); n   } n   for (Edge* p = g[u]; p; p = p->next) n     if (!vis[p->v]) { n       --deg[p->v]; n       pred(p->v, r - 1); n     } n } n void dfs(int u) { n   vis[u] = true; n   for (Edge* p = g[u]; p; p = p->next) n     if (!vis[p->v]) { n       prt[p->v] = u; n       dis[p->v] = dis[u] + 1; n       dfs(p->v); n     } n } n void adde(int u, int v) { n   static Edge pool[N * 2]; n   static Edge* p = pool; n   p->v = v; n   p->next = g[u]; n   ++deg[u]; n   g[u] = p; n   ++p; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100001; n vector<pair<int, int> > g1[1000], g2[1000]; n int edge_colors[MAXN]; n pair<int, int> colors1[1000][1000]; n pair<int, int> colors2[1000][1000]; n int ans; n bool used[MAXN]; n bool dfs(int v, int c1, int c2) { n   if (used[v]) return false; n   used[v] = true; n   int to = colors1[v][c1].first; n   int num = colors1[v][c1].second; n   if (colors2[to][c2].first == -1) { n     colors1[v][c1].first = -1; n     colors1[v][c1].second = -1; n     colors2[to][c1].first = -1; n     colors2[to][c1].second = -1; n     edge_colors[colors1[v][c1].second] = -1; n     edge_colors[colors2[to][c1].second] = -1; n     colors1[v][c2].first = to; n     colors1[v][c2].second = num; n     colors2[to][c2].first = v; n     colors2[to][c2].second = num; n     edge_colors[num] = c2; n     return true; n   } n   int mto = colors2[to][c2].first; n   int mto_num = colors2[to][c2].second; n   if (colors1[mto][c1].first == -1) { n     colors1[mto][c2].first = -1; n     colors1[mto][c2].second = -1; n     edge_colors[colors1[mto][c2].second] = -1; n     colors1[mto][c1].first = to; n     colors1[mto][c1].second = mto_num; n     colors2[to][c1].first = mto; n     colors2[to][c1].second = mto_num; n     edge_colors[mto_num] = c1; n     colors1[v][c1].first = -1; n     colors1[v][c1].second = -1; n     edge_colors[colors1[v][c1].second] = -1; n     colors1[v][c2].first = to; n     colors1[v][c2].second = num; n     colors2[to][c2].first = v; n     colors2[to][c2].second = num; n     edge_colors[num] = c2; n     return true; n   } else if (dfs(mto, c1, c2)) { n     colors1[mto][c1].first = to; n     colors1[mto][c1].second = mto_num; n     colors2[to][c1].first = mto; n     colors2[to][c1].second = mto_num; n     edge_colors[mto_num] = c1; n     colors1[v][c1].first = -1; n     colors1[v][c1].second = -1; n     edge_colors[colors1[v][c1].second] = -1; n     colors1[v][c2].first = to; n     colors1[v][c2].second = num; n     colors2[to][c2].first = v; n     colors2[to][c2].second = num; n     edge_colors[num] = c2; n     return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   int a, b, m; n   cin >> a >> b >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     g1[x].emplace_back(make_pair(y, i)); n     g2[y].emplace_back(make_pair(x, i)); n   } n   for (int i = 0; i < a; i++) n     if (g1[i].size() > ans) ans = g1[i].size(); n   for (int i = 0; i < b; i++) n     if (g2[i].size() > ans) ans = g2[i].size(); n   for (int i = 0; i < 1000; i++) n     for (int j = 0; j < 1000; j++) { n       colors1[i][j] = make_pair(-1, -1); n       colors2[i][j] = make_pair(-1, -1); n     } n   fill(edge_colors, edge_colors + m, -1); n   for (int i = 0; i < a; i++) { n     for (int j = 0; j < g1[i].size(); j++) { n       int to = g1[i][j].first; n       int num = g1[i][j].second; n       if (edge_colors[num] == -1) { n         bool ok = false; n         for (int q = 0; q < ans; q++) { n           if ((colors1[i][q].first == -1) && (colors2[to][q].first == -1)) { n             edge_colors[num] = q; n             colors1[i][q].first = to; n             colors1[i][q].second = num; n             colors2[to][q].first = i; n             colors2[to][q].second = num; n             ok = true; n             break; n           } n         } n       } n     } n   } n   for (int i = 0; i < a; i++) { n     for (int j = 0; j < g1[i].size(); j++) { n       int to = g1[i][j].first; n       int num = g1[i][j].second; n       if (edge_colors[num] == -1) { n         bool ok = false; n         for (int q = 0; q < ans; q++) { n           if ((colors1[i][q].first == -1) && (colors2[to][q].first == -1)) { n             edge_colors[num] = q; n             colors1[i][q].first = to; n             colors1[i][q].second = num; n             colors2[to][q].first = i; n             colors2[to][q].second = num; n             ok = true; n             break; n           } n         } n         if (!ok) { n           int c1 = -1, c2 = -1; n           for (int q = 0; q < ans; q++) { n             if ((colors1[i][q].first != -1) && (colors2[to][q].first == -1)) n               c1 = q; n             if ((colors1[i][q].first == -1) && (colors2[to][q].first != -1)) n               c2 = q; n           } n           fill(used, used + a, false); n           used[i] = true; n           if ((c1 == -1) || (c2 == -1)) n             while (true) n               ; n           int mto = colors2[to][c2].first; n           int mto_num = colors2[to][c2].second; n           if (colors1[mto][c1].first == -1) { n             colors1[mto][c2].first = -1; n             colors1[mto][c2].second = -1; n             edge_colors[colors1[mto][c2].second] = -1; n             colors1[mto][c1].first = to; n             colors1[mto][c1].second = mto_num; n             colors2[to][c1].first = mto; n             colors2[to][c1].second = mto_num; n             edge_colors[mto_num] = c1; n             colors1[i][c2].first = to; n             colors1[i][c2].second = num; n             colors2[to][c2].first = i; n             colors2[to][c2].second = num; n             edge_colors[num] = c2; n           } else if (dfs(mto, c1, c2)) { n             colors1[mto][c1].first = to; n             colors1[mto][c1].second = mto_num; n             colors2[to][c1].first = mto; n             colors2[to][c1].second = mto_num; n             edge_colors[mto_num] = c1; n             colors1[i][c2].first = to; n             colors1[i][c2].second = num; n             colors2[to][c2].first = i; n             colors2[to][c2].second = num; n             edge_colors[num] = c2; n           } n         } n       } n     } n   } n   for (int i = 0; i < a; i++) n     for (int q = 0; q < ans; q++) n       if (colors1[i][q].first != -1) edge_colors[colors1[i][q].second] = q; n   cout << ans << endl; n   for (int i = 0; i < m; i++) { n     cout << edge_colors[i] + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   long long arr[n + 1]; n   long long sum = 0; n   for (long long i = 1; i <= n; i++) { n     cin >> arr[i]; n     sum += arr[i]; n   } n   if (sum % n != 0) { n     cout << -1 <<   n ; n     return; n   } n   vector<pair<long long, pair<long long, long long>>> ans; n   for (long long i = 2; i <= n; i++) { n     long long brak = (i - arr[i] % i) % i; n     ans.push_back(make_pair(1, make_pair(i, brak))); n     arr[1] -= brak; n     arr[i] += brak; n     ans.push_back(make_pair(i, make_pair(1, arr[i] / i))); n     arr[1] += arr[i]; n     arr[i] = 0; n   } n   for (long long i = 2; i <= n; i++) { n     ans.push_back(make_pair(1, make_pair(i, sum / n))); n   } n   cout << ans.size() <<   n ; n   for (auto x : ans) { n     cout << x.first <<     << x.second.first <<     << x.second.second <<   n ; n   } n } n int32_t main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   } n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void trace_() { cerr <<   n ; } n template <typename T1, typename... T2> n void trace_(T1 t1, T2... t2) { n   cerr <<     << t1; n   trace_(t2...); n } n const long long mxN = 1e3 + 5; n long long n, a, b; n vector<long long> g[mxN]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n; n   vector<pair<long long, long long> > v1, v2; n   for (long long i = 0; i < n - 3; i++) { n     cin >> a >> b; n     if (a > b) swap(a, b); n     if (a != 1) v1.emplace_back(a, b); n   } n   sort(v1.begin(), v1.end(), n        [](pair<long long, long long> a, pair<long long, long long> b) { n          if (a.first == b.first) return a.second > b.second; n          return a.first < b.first; n        }); n   for (long long i = 0; i < n - 3; i++) { n     cin >> a >> b; n     if (a > b) swap(a, b); n     g[a].push_back(b); n   } n   for (long long i = 2; i <= n; i++) { n     sort(g[i].begin(), g[i].end()); n     reverse(g[i].begin(), g[i].end()); n     for (long long j = 1; j < (long long)(g[i].size()); j++) n       v2.emplace_back(1, g[i][j]); n     if ((long long)(g[i].size()) >= 1) v2.emplace_back(1, i + 1); n   } n   cout << (long long)(v1.size()) + (long long)(v2.size()) <<   n ; n   for (pair<long long, long long> i : v1) n     cout << i.first <<     << i.second <<   n ; n   reverse(v2.begin(), v2.end()); n   for (pair<long long, long long> i : v2) n     cout << i.first <<     << i.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool check(int x) { n   for (; x; x /= 10) n     if (x % 10 != 4 && x % 10 != 7) return 0; n   return 1; n } n const int maxn = 100100; n int head[maxn], nxt[maxn << 1], ver[maxn << 1], val[maxn << 1], tot; n inline void addedge(int a, int b, int c) { n   nxt[++tot] = head[a]; n   ver[tot] = b; n   val[tot] = c; n   head[a] = tot; n   nxt[++tot] = head[b]; n   ver[tot] = a; n   val[tot] = c; n   head[b] = tot; n } n int size[maxn], sum[maxn]; n int n, m; n inline void getsize(int x, int fat) { n   size[x] = 1; n   sum[x] = 0; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = ver[i]; n     if (y == fat) continue; n     getsize(y, x); n     size[x] += size[y]; n     sum[x] += val[i] ? size[y] : sum[y]; n   } n } n long long ans = 0; n inline void getans(int x, int fat, int upv) { n   ans += 1ll * (upv + sum[x]) * (upv + sum[x] - 1); n   for (int i = head[x]; i; i = nxt[i]) { n     int y = ver[i]; n     if (y == fat) continue; n     getans(y, x, val[i] ? (size[1] - size[y]) : (upv + sum[x] - sum[y])); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     addedge(a, b, check(c)); n   } n   getsize(1, 0); n   getans(1, 0, 0); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n vector<int> AdjList[maxn]; n bool vis[maxn]; n int child[maxn]; n char ans[maxn]; n int n; n void dfs(int s, int p = -1) { n   child[s] = 1; n   for (int u : AdjList[s]) { n     if (vis[u]) continue; n     if (u == p) continue; n     dfs(u, s); n     child[s] += child[u]; n   } n } n int find_cen(int s, int p, int sz) { n   for (int u : AdjList[s]) { n     if (vis[u]) continue; n     if (u == p) continue; n     if (child[u] * 2 > sz) return find_cen(u, s, sz); n   } n   return s; n } n void centroid(int u, char c) { n   dfs(u, u); n   int cen = find_cen(u, u, child[u]); n   ans[cen] = c; n   vis[cen] = 1; n   for (int v : AdjList[cen]) { n     if (vis[v]) continue; n     centroid(v, c + 1); n   } n } n signed main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     AdjList[u].push_back(v); n     AdjList[v].push_back(u); n   } n   centroid(1,  A ); n   for (int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-9; n const long long INF = 1000000000; n const int MAXN = 128; n int points[] = {0, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1}; n int t, n; n map<string, vector<int> > places; n map<string, int> score; n struct T { n   string name; n   vector<int> place; n   int score; n }; n bool cmp1(const T& a, const T& b) { n   if (a.score != b.score) return a.score > b.score; n   for (int i = 1; i <= 55; ++i) n     if (a.place[i] != b.place[i]) return a.place[i] > b.place[i]; n   return a.name < b.name; n } n bool cmp2(const T& a, const T& b) { n   if (a.place[1] != b.place[1]) return a.place[1] > b.place[1]; n   if (a.score != b.score) return a.score > b.score; n   for (int i = 2; i <= 55; ++i) n     if (a.place[i] != b.place[i]) return a.place[i] > b.place[i]; n   return a.name < b.name; n } n int main() { n   cin >> t; n   for (int tt = 0; tt < t; ++tt) { n     cin >> n; n     for (int i = 1; i <= n; ++i) { n       string s; n       cin >> s; n       int kk = 0; n       if (i <= 10) kk = points[i]; n       score[s] += kk; n       if (places.find(s) == places.end()) places[s] = vector<int>(60); n       places[s][i]++; n     } n   } n   map<string, int>::iterator it; n   vector<T> a; n   for (it = score.begin(); it != score.end(); ++it) { n     a.push_back(T()); n     a.back().name = it->first; n     a.back().place = places[it->first]; n     a.back().score = it->second; n   } n   sort((a).begin(), (a).end(), cmp1); n   cout << a[0].name << endl; n   sort((a).begin(), (a).end(), cmp2); n   cout << a[0].name << endl; n } n