#include <bits/stdc++.h> n using namespace std; n template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (T& x : v) is >> x; n   return is; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (int i = 0; i < (int)v.size(); i++) { n     os << v[i] << (i + 1 == (int)v.size() ?    :    ); n   } n   return os; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const pair<T, U>& p) { n   os <<  (  << p.first <<  ,  << p.second <<  ) ; n   return os; n } n template <typename T, typename U, typename V> n ostream& operator<<(ostream& os, const tuple<T, U, V>& t) { n   os <<  (  << get<0>(t) <<  ,  << get<1>(t) <<  ,  << get<2>(t) <<  ) ; n   return os; n } n template <typename T, typename U, typename V, typename W> n ostream& operator<<(ostream& os, const tuple<T, U, V, W>& t) { n   os <<  (  << get<0>(t) <<  ,  << get<1>(t) <<  ,  << get<2>(t) <<  ,  n      << get<3>(t) <<  ) ; n   return os; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const map<T, U>& m) { n   os <<  { ; n   for (auto itr = m.begin(); itr != m.end();) { n     os <<  (  << itr->first <<  ,  << itr->second <<  ) ; n     if (++itr != m.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const unordered_map<T, U>& m) { n   os <<  { ; n   for (auto itr = m.begin(); itr != m.end();) { n     os <<  (  << itr->first <<  ,  << itr->second <<  ) ; n     if (++itr != m.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& s) { n   os <<  { ; n   for (auto itr = s.begin(); itr != s.end();) { n     os << *itr; n     if (++itr != s.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const multiset<T>& s) { n   os <<  { ; n   for (auto itr = s.begin(); itr != s.end();) { n     os << *itr; n     if (++itr != s.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const unordered_set<T>& s) { n   os <<  { ; n   for (auto itr = s.begin(); itr != s.end();) { n     os << *itr; n     if (++itr != s.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const deque<T>& v) { n   for (int i = 0; i < (int)v.size(); i++) { n     os << v[i] << (i + 1 == (int)v.size() ?    :    ); n   } n   return os; n } n void debug_out() { cerr <<   n ; } n template <class Head, class... Tail> n void debug_out(Head&& head, Tail&&... tail) { n   cerr << head; n   if (sizeof...(Tail) > 0) cerr <<  ,  ; n   debug_out(move(tail)...); n } n template <typename T> n T gcd(T x, T y) { n   return y != 0 ? gcd(y, x % y) : x; n } n template <typename T> n T lcm(T x, T y) { n   return x / gcd(x, y) * y; n } n template <class T1, class T2> n inline bool chmin(T1& a, T2 b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T1, class T2> n inline bool chmax(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n const int INF = 1e9; n const long long IINF = 1e18; n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n const char dir[4] = { D ,  R ,  U ,  L }; n const long long MOD = 1000000007; n namespace n91 { n using usize = std::size_t; n using u64 = std::uint64_t; n std::vector<bool> stob(std::string s) { n   std::vector<u64> num(s.size() / 9 + 10, 0); n   for (usize i = 0; i < s.size(); i += 9) { n     const std::string t = s.substr(i, 9); n     u64 mul = 1; n     u64 add = 0; n     for (const auto c : t) { n       add = add * 10 + (c -  0 ); n       mul *= 10; n     } n     for (auto& d : num) { n       d = d * mul + add; n       add = d >> 32; n       d -= add << 32; n     } n   } n   std::vector<bool> res(num.size() * 32); n   for (usize i = 0; i < num.size(); ++i) { n     for (usize j = 0; j < 32; ++j) { n       res[i * 32 + j] = num[i] >> j & 1; n     } n   } n   while (!res.empty() && !res.back()) { n     res.pop_back(); n   } n   return res; n } n }  // namespace n91 n const int MAX = 2048; n using u64 = uint64_t; n using BS = bitset<2 * MAX>; n BS calc(const string& s) { n   int n = s.size(); n   vector<u64> num(n / 9 + 10, 0); n   for (int i = 0; i < n; i += 9) { n     string t = s.substr(i, 9); n     u64 mul = 1, add = 0; n     for (auto c : t) { n       add = add * 10 + (c -  0 ); n       mul *= 10; n     } n     for (auto& d : num) { n       d = d * mul + add; n       add = d >> 32; n       d -= add << 32; n     } n   } n   BS res; n   for (int i = 0; i < num.size(); i++) { n     for (int j = 0; j < 32; j++) { n       res[i * 32 + j] = num[i] >> j & 1; n     } n   } n   return res; n } n vector<BS> bases; n vector<int> idx(MAX, -1); n vector<int> solve(BS b, int x) { n   for (int i = MAX - 1; i >= 0; i--) { n     if (!b[i]) continue; n     if (~idx[i]) n       b ^= bases[idx[i]]; n     else { n       b.set(MAX + x, 1); n       idx[i] = bases.size(); n       bases.emplace_back(b); n       return {}; n     } n   } n   vector<int> res; n   for (int i = 0; i < MAX; i++) { n     if (b[MAX + i]) { n       res.emplace_back(i); n     } n   } n   return res; n }; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     string S; n     cin >> S; n     vector<int> ans = solve(calc(S), i); n     cout << ans.size(); n     for (int x : ans) cout <<     << x; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 300; n inline long long read() { n   long long s = 0, ww = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) ww = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     s = 10 * s + ch -  0 ; n     ch = getchar(); n   } n   return s * ww; n } n struct vec { n   long long x, y; n   vec() {} n   vec(long long _x, long long _y) { x = _x, y = _y; } n   inline friend vec operator-(vec a, vec b) { n     return vec(a.x - b.x, a.y - b.y); n   } n   inline friend long long Cross(vec a, vec b) { return a.x * b.y - a.y * b.x; } n   inline friend bool InUpper(vec a) { return a.y > 0 || (a.y == 0 && a.x > 0); } n } p[N + 5]; n inline bool cmp(pair<long long, long long> i, pair<long long, long long> j) { n   vec a = p[i.second] - p[i.first], b = p[j.second] - p[j.first]; n   bool k1 = InUpper(a), k2 = InUpper(b); n   if (k1 != k2) return k1 < k2; n   return Cross(a, b) > 0; n } n long long f[N + 5][N + 5][7]; n long long n; n pair<long long, long long> seg[N * N + 5]; n int main() { n   n = read(); n   for (register int i = 1; i <= n; i++) { n     long long x = read(), y = read(); n     p[i] = vec(x, y); n   } n   int cnt = 0; n   for (register int i = 1; i <= n; i++) n     for (register int j = 1; j <= n; j++) n       if (i != j) seg[++cnt] = make_pair(i, j); n   sort(seg + 1, seg + cnt + 1, cmp); n   for (register int i = 1; i <= n; i++) f[i][i][1] = 1; n   for (register int k = 1; k <= cnt; k++) { n     int u = seg[k].first, v = seg[k].second; n     for (register int i = 1; i <= n; i++) n       for (register int j = 1; j <= 5; j++) f[i][v][j + 1] += f[i][u][j]; n   } n   long long ans = 0; n   for (register int i = 1; i <= n; i++) ans += f[i][i][6]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int S; n   cin >> S; n   if (S == 1) { n     cout << 1 <<     << 1 <<   n ; n     cout << 1 <<   n ; n     return 0; n   } n   cout << (S - 1) * 2 <<     << 2 <<   n ; n   cout << 1 <<     << 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2000 + 10; n bool mrk[MAX_N][MAX_N]; n bool Ver[MAX_N][MAX_N]; n bool Hor[MAX_N][MAX_N]; n int N; n int Total; n void Solve(int x, int y) { n   bool flag = false; n   if ((mrk[x][y] ^ Ver[x][y] ^ Hor[x][y]) == 1) flag = true; n   int dx = -1, dy = 1; n   if (x < y) dx = 1, dy = -1; n   if (x + dx != y + dx) Ver[x + dx][y] ^= (Ver[x][y] ^ flag); n   if (y + dy != x + dy) Hor[x][y + dy] ^= (Hor[x][y] ^ flag); n   mrk[x][y] ^= flag; n   if (flag) Total++; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> N; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) { n       char temp; n       cin >> temp; n       mrk[j][i] = (temp -  0 ); n     } n   for (int l = 0; l < N; l++) n     for (int i = 0; i <= l; i++) { n       Solve(N - 1 - i, l - i); n       Solve(i, N - 1 - (l - i)); n     } n   cout << Total << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n, i, j, l, r, u, d, sum; n   char s[209]; n   while (scanf( %d , &n) != EOF) { n     getchar(); n     scanf( %s , s); n     sum = 0; n     for (i = 0; i < n; i++) { n       l = r = u = d = 0; n       for (j = i + 1; j < n; j++) { n         int x = 1, y = 1; n         for (int k = i; k <= j; k++) { n           if (s[k] ==  U ) n             y++; n           else if (s[k] ==  D ) n             y--; n           else if (s[k] ==  L ) n             x--; n           else n             x++; n         } n         if (x == 1 && y == 1) sum++; n       } n     } n     printf( %d n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p, numb, hh, i, j, l, n, m, a; n char k; n bool flag; n int h[200000], check[500000], check1[500000], prime[500000], dp[500000]; n int main() { n   for (i = 2; i <= 100000; i++) n     if (check[i] == 0) { n       for (j = 2 * i; j <= 100000; j += i) check[j] = 1; n     } n   for (i = 2; i <= 100000; i++) n     if (check[i] == 0) { n       l++; n       prime[l] = i; n     } n   cin >> n >> m; n   for (i = 1; i <= m; i++) { n     cin >> k >> a; n     hh = a; n     flag = true; n     if (k ==  + ) { n       if (dp[a] == 1) { n         cout <<  Already on  << endl; n       } else if (a == 1) { n         dp[a] = 1; n         cout <<  Success  << endl; n       } else { n         numb = 0; n         hh = a; n         for (j = 1; prime[j] * prime[j] <= a; j++) n           if (a % prime[j] == 0) { n             while (a % prime[j] == 0) a /= prime[j]; n             if (check1[prime[j]] > 0) { n               cout <<  Conflict with   << check1[prime[j]] << endl; n               flag = false; n               break; n             } else { n               numb++; n               h[numb] = prime[j]; n             } n           } n         if (flag) { n           if (check[a] == 0 && a > 1) { n             if (check1[a] > 0) { n               cout <<  Conflict with   << check1[a] << endl; n               flag = false; n             } else n               check1[a] = hh; n           } n           if (flag) { n             for (p = 1; p <= numb; p++) check1[h[p]] = hh; n             cout <<  Success  << endl; n             dp[hh] = 1; n           } n         } n       } n     } else { n       if (dp[a] == 1) { n         hh = a; n         cout <<  Success  << endl; n         for (j = 1; prime[j] * prime[j] <= a; j++) n           if (a % prime[j] == 0) { n             while (a % prime[j] == 0) a /= prime[j]; n             check1[prime[j]] = 0; n           } n         if (check[a] == 0 && a > 1) check1[a] = 0; n         dp[hh] = 0; n       } else n         cout <<  Already off  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, k[100100]; n const int du = 0.000001; n bool cmp(int x, int y) { return x > y; } n int main() { n   scanf( %d%d%d , &n, &a, &b); n   long long sum = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &k[i]); n     sum += k[i]; n   } n   if (a == b) { n     printf( %d , n - 1); n     return 0; n   } n   sort(k + 2, k + 1 + n, cmp); n   double cnt = 0; n   int xx = sum; n   double hah = b; n   cnt = a * k[1] / 1.0 / xx; n   if (cnt >= b) { n     printf( 0 n ); n     return 0; n   } n   for (int i = 2; i <= n; ++i) { n     xx = xx - k[i]; n     cnt = a * k[1] / 1.0 / xx; n     if (cnt >= b) { n       printf( %d n , i - 1); n       return 0; n     } n   } n   printf( %d n , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long int c(long long int n, long long int i) { n   if (i == 0) n     return 1; n   else if (i == 1) n     return n; n   else if (i == 2) n     return (n * (n - 1)) / 2; n   else if (i == 3) n     return (n * (n - 1) * (n - 2)) / 6; n   else if (i == 4) n     return (n * (n - 1) * (n - 2) * (n - 3)) / 24; n } n int main() { n   long long int d[5]; n   d[0] = 1; n   d[1] = 0; n   d[2] = 1; n   d[3] = 2; n   d[4] = 9; n   long long int n, k; n   cin >> n >> k; n   long long int ans = 0; n   for (long long int i = 0; i <= k; i++) ans += d[i] * c(n, i); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f(int n) { n   if (n < 10) { n     return n; n   } n   int ans = 1; n   while (n) { n     if (n % 10 != 0) { n       ans *= n % 10; n     } n     n /= 10; n   } n   return ans; n } n int g[1000005]; n int cnt[10][1000005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   for (int i = 1; i <= 1000000; i++) { n     if (i < 10) { n       g[i] = i; n     } else { n       g[i] = g[f(i)]; n     } n     if (i <= 60) { n     } n     for (int j = 1; j <= 9; j++) { n       cnt[j][i] = cnt[j][i - 1]; n     } n     cnt[g[i]][i]++; n   } n   int T; n   cin >> T; n   while (T--) { n     int l, r, k; n     cin >> l >> r >> k; n     cout << cnt[k][r] - cnt[k][l - 1] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int N = 100200; n struct Node { n   int to, cost; n }; n vector<Node> g[N]; n int dist[105][N] = {}, arr[N] = {}, n, m, type[N] = {}, k, s, vis[N] = {}; n void addedge(int u, int v, int cost) { n   g[u].push_back((Node){v, cost}); n   g[v].push_back((Node){u, cost}); n } n void bfs(int s, int n, int* d) { n   queue<int> que; n   memset(vis, 0, sizeof(vis)); n   int p, to, cost; n   que.push(s); n   vis[s] = 1; n   while (!que.empty()) { n     p = que.front(); n     que.pop(); n     const int sz = g[p].size(); n     for (int i = 0; i < sz; i++) { n       to = g[p][i].to; n       if (to > n - 1 || vis[to]) continue; n       d[to] = d[p] + 1; n       que.push(to); n       vis[to] = 1; n     } n   } n } n int main(void) { n   std::ios::sync_with_stdio(false); n   cin >> n >> m >> k >> s; n   for (int i = 0; i < n; i++) cin >> type[i]; n   int u, v; n   for (int i = 0; i < m; i++) { n     cin >> u >> v; n     u--; n     v--; n     addedge(u, v, 1); n   } n   for (int i = 0; i < n; i++) { n     addedge(n - 1 + type[i], i, 1); n   } n   for (int i = 1; i <= k; i++) { n     bfs(n - 1 + i, n, dist[i]); n   } n   for (int i = 0; i < n; i++) { n     int ans = 0; n     for (int j = 1; j <= k; j++) arr[j] = dist[j][i] - 1; n     sort(arr + 1, arr + k + 1); n     for (int i = 1; i <= s; i++) ans += arr[i]; n     cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, l, x, y, a[1 << 20]; n set<long> has; n long hx, hy; n long ans; n long thx, thy; n void check(long ps) { n   if (ps < 0 || ps > l) return; n   hx = thx; n   hy = thy; n   if (has.find(ps + x) != has.end()) hx = 1; n   if (has.find(ps + y) != has.end()) hy = 1; n   if (has.find(ps - x) != has.end()) hx = 1; n   if (has.find(ps - y) != has.end()) hy = 1; n   if (hx && hy) ans = ps; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> l >> x >> y; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     has.insert(a[i]); n   } n   hx = hy = 0; n   for (int i = 0; i < n; i++) { n     if (has.find(a[i] + x) != has.end()) hx = 1; n     if (has.find(a[i] + y) != has.end()) hy = 1; n     if (has.find(a[i] - x) != has.end()) hx = 1; n     if (has.find(a[i] - y) != has.end()) hy = 1; n   } n   thx = hx; n   thy = hy; n   if (hx && hy) { n     cout << 0 << endl; n   } else { n     ans = -1; n     for (int i = 0; i < n; i++) { n       check(a[i] - x); n       check(a[i] + x); n       check(a[i] - y); n       check(a[i] + y); n     } n     if (ans >= 0) n       cout << 1 << endl << ans << endl; n     else { n       cout << 2 << endl; n       cout << x <<     << y << endl; n     } n   } n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int k; n bool debug = false; n int a[105]; n int main() { n   cin >> n; n   for (int i = 0, x; i < n; i++) { n     scanf( %d , &x); n     a[x]++; n   } n   for (int i = 0; i < 105; i++) k += a[i] / 2; n   printf( %d n , k / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, a[1005], b[1005], ans; n bool c[1005], lol; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     lol = false; n     fill(c, c + 1005, false); n     cin >> n >> m; n     for (int i = 0; i < n; ++i) { n       cin >> a[i]; n       c[a[i]] = true; n     } n     for (int i = 0; i < m; ++i) { n       cin >> b[i]; n       if (c[b[i]] == true) { n         ans = b[i]; n         lol = true; n       } n     } n     if (lol == true) n       cout <<  YES n1   << ans <<   n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> adj[200003]; n vector<pair<int, pair<int, int>>> edges; n vector<long long> len; n set<int> vertices; n void dijkstra(int start) { n   set<int> vis; n   priority_queue<pair<long long, int>, vector<pair<long long, int>>, n                  greater<pair<long long, int>>> n       pq; n   pq.push(make_pair(0, start)); n   while (!pq.empty()) { n     while (!pq.empty() && vis.find(pq.top().second) != vis.end()) pq.pop(); n     if (pq.empty()) break; n     long long l = pq.top().first; n     int v = pq.top().second; n     pq.pop(); n     vis.insert(v); n     if (l != 0) len.push_back(l); n     for (int i = 0; i < adj[v].size(); i++) { n       if (vis.find(adj[v][i].first) == vis.end()) { n         pq.push(make_pair(l + 1ll * adj[v][i].second, adj[v][i].first)); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < m; i++) { n     int a, b, w; n     cin >> a >> b >> w; n     edges.push_back(make_pair(w, make_pair(a, b))); n   } n   sort(edges.begin(), edges.end()); n   for (int i = 0; i < k && i < edges.size(); i++) { n     vertices.insert(edges[i].second.first); n     vertices.insert(edges[i].second.second); n     adj[edges[i].second.first].push_back( n         make_pair(edges[i].second.second, edges[i].first)); n     adj[edges[i].second.second].push_back( n         make_pair(edges[i].second.first, edges[i].first)); n   } n   for (int x : vertices) { n     dijkstra(x); n   } n   sort(len.begin(), len.end()); n   cout << len[2 * k - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, x, i; n     cin >> n >> x; n     long long int second = 0; n     long long int a[n]; n     for (long long int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a, a + n); n     for (i = n - 1; i >= 0; i--) { n       second += a[i]; n       if (second / (n - i) < x) break; n     } n     cout << n - (i + 1) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long n, k, l, a[1000010], m, p, q, b[1000010], d, c[1000010], f[1000010], n     g[1000010], ans; n struct BIT { n   long long a[1000010]; n   void clear() { memset(a, 0, sizeof(a)); } n   void add(int x, long long v) { n     for (; x <= 1000010 - 5; x += x & (-x)) a[x] = (a[x] + v + mod) % mod; n   } n   long long get(int x) { n     long long ans = 0; n     for (; x; x -= x & (-x)) ans = (ans + a[x]) % mod; n     return ans; n   } n } tr; n int main() { n   scanf( %lld%lld%lld , &n, &l, &k); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), b[i] = a[i]; n   sort(b + 1, b + n + 1); n   d = unique(b + 1, b + n + 1) - b - 1; n   for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + d + 1, a[i]) - b; n   m = l / n; n   p = l % n; n   if (!p) { n     m--; n     p += n; n   } n   q = max(0ll, m - k + 1); n   q %= mod; n   int cnt = 0; n   for (int i = 1; i <= k - 1; i++) n     for (int j = 1; j <= n; j++) c[++cnt] = a[j]; n   for (int i = 1; i <= p; i++) c[++cnt] = a[i]; n   for (int i = cnt - p + 1; i <= cnt; i++) f[i] = 1, tr.add(c[i], f[i]); n   for (int i = k - 1; i; --i) { n     for (int j = 1; j <= n; j++) { n       int pos = (i - 1) * n + j; n       f[pos] = (tr.get(1000010 - 5) - tr.get(c[pos] - 1) + mod) % mod; n     } n     for (int j = 1; j <= n; j++) { n       int pos = (i - 1) * n + j; n       if (pos + n <= cnt) tr.add(c[pos + n], -f[pos + n]); n       tr.add(c[pos], f[pos]); n     } n   } n   for (int i = max(cnt - p - m * n + 1, 1ll); i <= cnt; i++) n     ans = (ans + f[i]) % mod; n   memset(c, 0, sizeof(c)); n   tr.clear(); n   cnt = 0; n   for (int i = 1; i <= k; i++) n     for (int j = 1; j <= n; j++) c[++cnt] = a[j]; n   for (int i = cnt - n + 1; i <= cnt; i++) g[i] = 1, tr.add(c[i], g[i]); n   for (int i = k - 1; i; --i) { n     for (int j = 1; j <= n; j++) { n       int pos = (i - 1) * n + j; n       g[pos] = (tr.get(1000010 - 5) - tr.get(c[pos] - 1) + mod) % mod; n     } n     for (int j = 1; j <= n; j++) { n       int pos = (i - 1) * n + j; n       tr.add(c[pos + n], -g[pos + n]); n       tr.add(c[pos], g[pos]); n     } n   } n   long long tmp = 0; n   for (int i = 1; i <= min(m, k - 1); i++) { n     for (int j = 1; j <= n; j++) { n       int pos = (k - i) * n + j; n       tmp = (tmp + g[pos]) % mod; n     } n     ans = (ans + tmp) % mod; n   } n   tmp = 0; n   for (int i = 1; i <= k; i++) n     for (int j = 1; j <= n; j++) { n       int pos = (i - 1) * n + j; n       tmp = (tmp + g[pos]) % mod; n     } n   tmp = tmp * q % mod; n   ans = (ans + tmp) % mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 1e6 + 5; n int n, q, m; n const int N = 105; n const int L = 1505; n char s[maxn]; n int tr[maxn][30], tot = 0; n int fail[maxn], w[maxn]; n vector<int> g[maxn]; n void add(char *s, int id) { n   int u = 0; n   int n = strlen(s); n   for (int i = 0; i < n; i++) { n     int c = s[i] -  a ; n     if (!tr[u][c]) tr[u][c] = ++tot; n     u = tr[u][c]; n   } n   w[id] = u; n } n struct bit_tree { n   long long c[maxn], n; n   void init(int x) { n = x; } n   inline int lowbit(int x) { return x & (-x); } n   inline void update(int x, int v) { n     for (int i = x; i <= n; i += lowbit(i)) { n       c[i] += v; n     } n   } n   inline long long query(int x) { n     long long ans = 0; n     for (int i = x; i > 0; i -= lowbit(i)) ans += c[i]; n     return ans; n   } n } T; n void ac() { n   queue<int> q; n   for (int i = 0; i < 26; i++) { n     if (tr[0][i]) q.push(tr[0][i]), fail[tr[0][i]] = 0; n   } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < 26; i++) { n       if (tr[u][i]) { n         fail[tr[u][i]] = tr[fail[u]][i]; n         q.push(tr[u][i]); n       } else { n         tr[u][i] = tr[fail[u]][i]; n       } n     } n   } n } n int ins[maxn], out[maxn], cnt = 0; n int vis[maxn]; n void dfs(int x) { n   ins[x] = ++cnt; n   for (int i : g[x]) dfs(i); n   out[x] = cnt; n } n int main() { n   cin >> q >> n; n   for (int i = 1; i <= n; i++) { n     cin >> s; n     add(s, i); n   } n   ac(); n   for (int i = 1; i <= tot; i++) { n     g[fail[i]].push_back(i); n   } n   dfs(0); n   T.init(cnt); n   for (int i = 1; i <= n; i++) { n     vis[i] = 1; n     T.update(ins[w[i]], 1); n     T.update(out[w[i]] + 1, -1); n   } n   char c; n   int id; n   while (q--) { n     cin >> c; n     if (c ==  + ) { n       cin >> id; n       if (vis[id]) continue; n       vis[id] = 1; n       T.update(ins[w[id]], 1); n       T.update(out[w[id]] + 1, -1); n     } else if (c ==  - ) { n       cin >> id; n       if (!vis[id]) continue; n       vis[id] = 0; n       T.update(ins[w[id]], -1); n       T.update(out[w[id]] + 1, +1); n     } else { n       cin >> s; n       int m = strlen(s); n       long long p = 0, ans = 0; n       for (int i = 0; i < m; i++) { n         p = tr[p][s[i] -  a ]; n         ans += T.query(ins[p]); n       } n       printf( %lld n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 7; n int n, m; n vector<int> d[MAXN]; n int r[MAXN]; n vector<pair<int, int> > g[MAXN]; n inline void add_edge(int u, int v, int c) { n   g[u].push_back(make_pair(c, v)); n   g[v].push_back(make_pair(c, u)); n } n int col[MAXN]; n inline bool dfs(int u, int c) { n   if (col[u] != -1) return col[u] == c; n   col[u] = c; n   for (auto e : (g[u])) { n     if (!dfs(e.second, c ^ e.first)) return 0; n   } n   return 1; n } n inline void read(int &x) { n   x = 0; n   int 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   x *= f; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   read(n); n   read(m); n   for (int i = (1); i <= (n); ++i) read(r[i]); n   for (int i = (1); i <= (m); ++i) { n     int x; n     read(x); n     for (int j = (1); j <= (x); ++j) { n       int p; n       read(p); n       d[p].push_back(i); n     } n   } n   for (int i = (1); i <= (n); ++i) add_edge(d[i][0], d[i][1], r[i] ^ 1); n   memset(col, -1, sizeof col); n   bool ans = 1; n   for (int i = 1; i <= m && ans; ++i) n     if (col[i] == -1) ans &= dfs(i, 0); n   cout << (ans ?  YES  :  NO ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[1000000]; n int main() { n   int n; n   cin >> n; n   long long int count = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   long long int Min = arr[0]; n   long long int Max = arr[0]; n   for (int i = 1; i < n; i++) { n     if (arr[i] >= arr[i - 1]) n       Min = Max = arr[i]; n     else { n       int j = i; n       long long int mmin = arr[j - 1]; n       long long int mmax = arr[j - 1]; n       int c = 0; n       while (arr[j - 1] > arr[j]) { n         mmin = min(arr[j], mmin); n         mmax = max(arr[j], mmax); n         j++; n         if (j == n) break; n         c++; n       } n       count += mmax - mmin; n       i += c; n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, k = 0, nod; n   cin >> a; n   for (int i = 2; i < a; i++) { n     int b = a; n     while (b != 0) { n       k += b % i; n       b /= i; n     } n   } n   int b = a - 2; n   for (int i = k; i > 0; i--) n     if (k % i == 0 && b % i == 0) { n       nod = i; n       k /= nod; n       b /= nod; n       break; n     } n   cout << k <<  /  << b; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[(20ll + (long long)2e5)], b[(20ll + (long long)2e5)], n     c[(20ll + (long long)2e5)], *d[(20ll + (long long)2e5)], n     e[(20ll + (long long)2e5)]; n int main() { n   long long n; n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     a[i] *= i * (n - i + 1); n     b[i] = a[i]; n   } n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &c[i]); n     d[i] = &b[i]; n   } n   sort(d + 1, d + n + 1, [](long long* l, long long* r) { return *l < *r; }); n   sort(c + 1, c + 1 + n, greater<long long>()); n   for (long long i = 1; i <= n; i++) { n     *d[i] = c[i]; n   } n   long long s = 0, mod = 998244353ll; n   for (long long i = 1; i <= n; i++) n     s = (s + (a[i] % mod * (b[i] % mod)) % mod) % mod; n   printf( n        %lld  n         n , n       s); n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n struct Tree { n   int n; n   vector<long long> t; n   vector<int> mx; n   vector<int> val; n   vector<int> weights; n   void add_val(int v, int l, int r, int now) { n     t[v] = weights[v] * (long long)now; n     mx[v] = now; n     val[v] = now; n   } n   void push(int v, int l, int r) { n     if (val[v] == -1) return; n     int m = (l + r) / 2; n     add_val(2 * v, l, m, val[v]); n     add_val(2 * v + 1, m, r, val[v]); n     val[v] = -1; n   } n   void relax(int v) { n     t[v] = t[2 * v] + t[2 * v + 1]; n     mx[v] = max(mx[2 * v], mx[2 * v + 1]); n     weights[v] = weights[2 * v] + weights[2 * v + 1]; n   } n   void build(vector<int>& a, vector<int>& w, int v, int l, int r) { n     if (r - l == 1) { n       t[v] = a[l] * (long long)w[l]; n       mx[v] = a[l]; n       weights[v] = w[l]; n       return; n     } n     int m = (l + r) / 2; n     build(a, w, 2 * v, l, m); n     build(a, w, 2 * v + 1, m, r); n     relax(v); n   } n   Tree(vector<int>& a, vector<int>& w) n       : n(a.size()), t(4 * n), val(4 * n, -1), weights(4 * n), mx(4 * n) { n     build(a, w, 1, 0, n); n   } n   long long get_sum(int l, int r, int vt, int lt, int rt) { n     if (l >= rt || lt >= r) { n       return 0; n     } n     if (l <= lt && rt <= r) { n       return t[vt]; n     } n     push(vt, lt, rt); n     int mt = (lt + rt) / 2; n     auto first = get_sum(l, r, 2 * vt, lt, mt); n     auto second = get_sum(l, r, 2 * vt + 1, mt, rt); n     relax(vt); n     return first + second; n   } n   long long get_sum(int l, int r) { return get_sum(l, r, 1, 0, n); } n   void change_max(int l, int r, int now, int vt, int lt, int rt) { n     if (l >= rt || lt >= r) { n       return; n     } n     int mt = (lt + rt) / 2; n     if (l <= lt && rt <= r) { n       if (mx[vt] < now) { n         add_val(vt, lt, rt, now); n         return; n       } n       if (rt - lt == 1) return; n       push(vt, lt, rt); n       if (mx[2 * vt] < now) { n         add_val(2 * vt, lt, mt, now); n         change_max(l, r, now, 2 * vt + 1, mt, rt); n       } else { n         change_max(l, r, now, 2 * vt, lt, mt); n       } n       relax(vt); n       return; n     } n     push(vt, lt, rt); n     change_max(l, r, now, 2 * vt, lt, mt); n     change_max(l, r, now, 2 * vt + 1, mt, rt); n     relax(vt); n   } n   void change_max(int l, int r, int now) { change_max(l, r, now, 1, 0, n); } n   int get_pos(int val, int vt, int lt, int rt) { n     if (rt - lt == 1) { n       return (t[vt] >= val ? lt : -1); n     } n     push(vt, lt, rt); n     int mt = (lt + rt) / 2; n     if (mx[2 * vt] >= val) { n       return get_pos(val, 2 * vt, lt, mt); n     } n     return get_pos(val, 2 * vt + 1, mt, rt); n   } n   int get_pos(int val) { return get_pos(val, 1, 0, n); } n   void print(int vt, int lt, int rt) { n     if (rt - lt == 1) { n       cout << mx[vt] <<    ; n       return; n     } n     push(vt, lt, rt); n     int mt = (lt + rt) / 2; n     print(2 * vt, lt, mt); n     print(2 * vt + 1, mt, rt); n     relax(vt); n   } n   void print() { n     cout <<  FXS n ; n     print(1, 0, n); n     cout <<   n ; n   } n }; n void add(int& first, int second) { n   first += second; n   if (first >= INF) { n     first -= INF; n   } n   if (first < 0) { n     first += INF; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, k, L; n   cin >> n >> k >> L; n   set<int> xs; n   set<int> ys; n   vector<int> color(n); n   vector<pair<int, int>> points(n); n   map<int, vector<int>> ymp; n   for (int i = 0; i < n; ++i) { n     int c, first, second; n     cin >> first >> second >> c; n     --c; n     color[i] = c; n     points[i] = {first, second}; n     ymp[second].push_back(i); n     xs.insert(first); n     ys.insert(second); n   } n   ys.insert(L); n   map<int, int> nx; n   vector<int> xval; n   int now = 0; n   for (int first : xs) { n     nx[first] = now++; n     xval.push_back(first); n   } n   int res = 0; n   vector<pair<int, int>> xcur; n   vector<multiset<int>> allx(k); n   int y_prev = 0; n   for (int y_up : ys) { n     vector<int> fx; n     vector<int> lens; n     map<int, int> posq; n     int pr = 0; n     for (auto t : xcur) { n       lens.push_back(xval[t.first] - pr + 1); n       posq[t.first] = lens.size() - 1; n       pr = xval[t.first] + 1; n     } n     lens.push_back(L - pr); n     vector<int> color_count(k, 0); n     int exist_colors = 0; n     int rt = 0; n     while (rt < (int)xcur.size() && exist_colors < k) { n       int c = xcur[rt].second; n       ++color_count[c]; n       exist_colors += (color_count[c] == 1); n       ++rt; n     } n     if (exist_colors == k) { n       fx.push_back(xval[xcur[rt - 1].first]); n     } n     for (int lt = 0; lt < (int)xcur.size(); ++lt) { n       int c = xcur[lt].second; n       --color_count[c]; n       exist_colors -= (color_count[c] == 0); n       while (rt < (int)xcur.size() && exist_colors < k) { n         int c = xcur[rt].second; n         ++color_count[c]; n         exist_colors += (color_count[c] == 1); n         ++rt; n       } n       if (exist_colors == k) { n         fx.push_back(xval[xcur[rt - 1].first]); n       } else n         break; n     } n     while (fx.size() < lens.size()) { n       fx.push_back(L); n     } n     Tree tree(fx, lens); n     pr = 0; n     for (int y_down : ys) { n       if (y_down == y_up) break; n       int dy = y_down - pr + 1; n       int ddy = y_up - y_prev; n       add(res, ddy * (long long)dy % INF * n                    ((L * (long long)L - tree.get_sum(0, fx.size())) % INF) % n                    INF); n       for (auto ind : ymp[y_down]) { n         int first = nx[points[ind].first]; n         int c = color[ind]; n         allx[c].erase(allx[c].find(first)); n       } n       vector<pair<int, pair<int, int>>> queries; n       for (auto ind : ymp[y_down]) { n         int first = nx[points[ind].first]; n         int c = color[ind]; n         int x_ind = n             upper_bound(xcur.begin(), xcur.end(), make_pair(first, INF)) - n             xcur.begin() - 1; n         auto it = allx[c].lower_bound(first); n         int prv = 0, nxt; n         if (it != allx[c].end()) { n           nxt = xval[*it]; n         } else { n           nxt = L; n         } n         it = allx[c].upper_bound(first); n         if (it != allx[c].begin()) { n           prv = posq[*(--it)] + 1; n         } n         prv = max(prv, tree.get_pos(xval[first])); n         queries.push_back({x_ind, {prv, nxt}}); n       } n       sort(queries.rbegin(), queries.rend()); n       for (auto t : queries) { n         int x_ind = t.first; n         int prv = t.second.first; n         int nxt = t.second.second; n         tree.change_max(prv, x_ind + 1, nxt); n       } n       pr = y_down + 1; n     } n     for (auto ind : ymp[y_up]) { n       xcur.push_back({nx[points[ind].first], color[ind]}); n     } n     sort(xcur.begin(), xcur.end()); n     for (int i = 0; i < k; ++i) { n       allx[i].clear(); n     } n     for (auto t : xcur) { n       allx[t.second].insert(t.first); n     } n     y_prev = y_up; n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1e3 + 10, K = 10 + 2, E = -(1e8 + 10); n long long dp[N][N][K][2]; n int n, m, kk; n string s, t; n void read_input() { n   cin >> n >> m >> kk; n   cin >> s >> t; n } n void solve() { n   s =  *  + s, t =  *  + t; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       for (int k = 1; k <= kk; k++) { n         dp[i][j][k][1] = (s[i] == t[j] ? 1 + max(dp[i - 1][j - 1][k][1], n                                                  dp[i - 1][j - 1][k - 1][0]) n                                        : 0); n         dp[i][j][k][0] = n             max({dp[i - 1][j][k][0], dp[i][j - 1][k][0], dp[i][j][k][1]}); n       } n   cout << dp[n][m][kk][0]; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   read_input(), solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n long long pows(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n long long powm(long long x, long long y, long long m = 998244353) { n   x = x % m; n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x; n     res %= m; n     y = y >> 1; n     x = x * x; n     x %= m; n   } n   return res; n } n long long modInverse(long long a, long long m) { return powm(a, m - 2, m); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, i, j, k, x, y, z, t, e, f, p, q, g, l, r, w, h, n       count1 = 0, prod = 1, a, b, c, d, index, x1, x2, y1, y2, diff, ans = 0, n       sum = 0, sum1 = 0, sum2 = 0, flag = 0, flag1 = 0, flag2 = 0; n   string s, s1, s2; n   cin >> n >> k; n   set<long long> st; n   vector<pair<long long, long long>> v(n); n   for (i = 0; i < n; i++) { n     cin >> v[i].first; n     v[i].second = i + 1; n   } n   sort(v.begin(), v.end(), greater<pair<long long, long long>>()); n   vector<long long> v1(n + 1); n   for (i = k + 1; i <= k + n; i++) { n     st.insert(i); n   } n   for (i = 0; i < n; i++) { n     p = *st.lower_bound(v[i].second); n     v1[v[i].second] = p; n     st.erase(p); n     ans += (p - v[i].second) * v[i].first; n   } n   cout << ans <<   n ; n   for (i = 1; i <= n; i++) { n     cout << v1[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 5e9; n long long w[20], s[20], m; n bool solve(int bm) { n   long long l = 0, r = m; n   for (int i = 0; i < 14; ++i) { n     if (bm % 3 == 0) n       l += w[i]; n     else if (bm % 3 == 1) n       r += w[i]; n     bm /= 3; n   } n   return l == r; n } n void special4() { n   while (m > 0) { n     if (m % 4 == 2) { n       cout <<  NO n ; n       exit(0); n     } n     if (m % 4 == 3) { n       m += 4; n     } n     m /= 4; n   } n   cout <<  YES n ; n } n int main() { n   w[0] = 1; n   cin >> w[1] >> m; n   if (w[1] <= 3) { n     cout <<  YES n ; n     return 0; n   } n   for (int i = 2; i < 20; ++i) { n     w[i] = w[i - 1] * w[1]; n     if (w[i] > inf) w[i] = 0; n   } n   if (w[1] == 4) { n     special4(); n     return 0; n   } n   for (int bm = 0; bm < (int)5e6; ++bm) { n     if (solve(bm)) { n       cout <<  YES n ; n       return 0; n     } n   } n   cout <<  NO n ; n }; n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, b; n   cin >> n >> b; n   int a[n], maxx = b; n   for (int x = 0; x < n; x++) cin >> a[x]; n   for (int x = 0; x < n; x++) { n     for (int y = x + 1; y < n; y++) n       maxx = max(maxx, (b / a[x]) * a[y] + (b % a[x])); n   } n   cout << maxx; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int n, k; n vector<vector<long long>> adj, leaf; n vector<long long> level, par, out; n vector<long long> counti; n void dfs(int src, int lvl, int p = -1) { n   int child = 0; n   level[src] = lvl; n   for (auto it : adj[src]) { n     if (it == p) continue; n     par[it] = src; n     child++; n     dfs(it, lvl + 1, src); n   } n   if (!child) leaf.push_back({src, lvl, 0}); n   out[src] = child; n } n class comp { n  public: n   bool operator()(vector<long long> a, vector<long long> b) { n     return a[1] - a[2] < b[1] - b[2]; n   } n }; n void solve() { n   cin >> n >> k; n   adj.resize(n); n   for (int i = 0, x, y; i < n - 1; i++) { n     cin >> x >> y; n     x--, y--; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   level.resize(n); n   par.resize(n); n   out.resize(n); n   dfs(0, 0); n   vector<long long> total(n, 0); n   priority_queue<vector<long long>, vector<vector<long long>>, comp> pq; n   for (auto it : leaf) pq.push(it); n   long long ans = 0; n   while (!pq.empty() && k--) { n     auto f = pq.top(); n     pq.pop(); n     long long node = f[0]; n     ans += f[1] - total[node]; n     if (node) { n       total[par[node]] += f[2] + 1; n       --out[par[node]]; n       if (!out[par[node]]) { n         pq.push({par[node], f[1] - 1, total[par[node]]}); n       } n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 77, L = 17; n int n, q, tim = 1, root = 1; n int b[N], d[N], Par[N][L], St[N], En[N]; n long long S[N << 2], lz[N << 2]; n inline void Shift(int l, int r, int id) { n   long long x = lz[id]; n   lz[id] = 0; n   S[id] += (r - l) * x; n   if (r - l > 1) lz[id << 1] += x, lz[id << 1 ^ 1] += x; n } n void Add(int ql, int qr, int x, int l = 1, int r = n + 1, int id = 1) { n   Shift(l, r, id); n   if (qr <= l || r <= ql) return; n   if (ql <= l && r <= qr) { n     lz[id] += x; n     Shift(l, r, id); n     return; n   } n   int mid = ((l + r) >> 1); n   Add(ql, qr, x, l, mid, id << 1); n   Add(ql, qr, x, mid, r, id << 1 ^ 1); n   S[id] = S[id << 1] + S[id << 1 ^ 1]; n } n long long Get(int ql, int qr, int l = 1, int r = n + 1, int id = 1) { n   Shift(l, r, id); n   if (qr <= l || r <= ql) return 0; n   if (ql <= l && r <= qr) return S[id]; n   int mid = ((l + r) >> 1); n   return Get(ql, qr, l, mid, id << 1) + Get(ql, qr, mid, r, id << 1 ^ 1); n } n vector<int> a[N]; n void dfs(int v = 1) { n   St[v] = tim++; n   for (int i = 1; i < L; ++i) Par[v][i] = Par[Par[v][i - 1]][i - 1]; n   for (int u : a[v]) { n     if (u == Par[v][0]) continue; n     Par[u][0] = v; n     d[u] = d[v] + 1; n     dfs(u); n   } n   En[v] = tim; n } n inline bool isPar(int v, int u) { return St[v] <= St[u] && En[u] <= En[v]; } n inline int GetPar(int v, int k) { n   for (int i = 0; i < L; ++i) n     if (k & (1 << i)) v = Par[v][i]; n   return v; n } n inline int GetLca(int v, int u) { n   if (d[v] < d[u]) swap(v, u); n   v = GetPar(v, d[v] - d[u]); n   if (v == u) return v; n   for (int i = L - 1; i >= 0; --i) n     if (Par[v][i] != Par[u][i]) v = Par[v][i], u = Par[u][i]; n   return Par[v][0]; n } n inline int GetLca(int r, int v, int u) { n   int t = GetLca(v, u); n   if (isPar(r, t)) return t; n   if (isPar(r, u) || isPar(r, v)) return r; n   if (!isPar(t, r)) return t; n   int x = GetLca(r, v); n   int y = GetLca(r, u); n   return (d[x] > d[y] ? x : y); n } n inline void Update(int r, int v, int u, int x) { n   int t = GetLca(r, v, u); n   if ((!isPar(t, r))) { n     Add(St[t], En[t], x); n     return; n   } n   Add(1, n + 1, x); n   if (r == t) return; n   int p = GetPar(r, d[r] - d[t] - 1); n   Add(St[p], En[p], -x); n } n inline long long GetSum(int r, int v) { n   if (!isPar(v, r)) return Get(St[v], En[v]); n   long long res = Get(1, n + 1); n   if (r == v) return res; n   int p = GetPar(r, d[r] - d[v] - 1); n   res -= Get(St[p], En[p]); n   return res; n } n int main() { n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= n; ++i) scanf( %d , b + i); n   for (int v, u, i = 1; i < n; ++i) n     scanf( %d %d , &v, &u), a[v].push_back(u), a[u].push_back(v); n   d[1] = 1; n   dfs(); n   for (int i = 1; i <= n; ++i) Add(St[i], St[i] + 1, b[i]); n   while (q--) { n     int tp, v, u, x; n     scanf( %d %d , &tp, &v); n     if (tp == 1) { n       root = v; n     } else if (tp == 2) { n       scanf( %d %d , &u, &x); n       Update(root, v, u, x); n     } else { n       printf( %lld n , GetSum(root, v)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int Pow(long long int a, long long int b, long long int md, n                   long long int ans = 1) { n   for (; b; b >>= 1, a = a * a % md) n     if (b & 1) ans = ans * a % md; n   return ans % md; n } n const long long int MAXK = 1e6 + 10; n const long long int MAXN = 2e3 + 10; n const long long int INF = 8e18; n const long long int MOD = 1e9 + 7; n long long int dp[MAXN][MAXN], fact[MAXK], inv[MAXK], ans = 0, n, m, k; n long long int add(long long int x, long long int y) { return (x + y) % MOD; } n long long int mul(long long int x, long long int y) { return x * y % MOD; } n long long int C(long long int x, long long int y) { n   if (y > x) return 0; n   return fact[x] * inv[y] % MOD * inv[x - y] % MOD; n } n int main() { n   scanf( %lld%lld%lld , &n, &m, &k); n   if (m == 1) return !printf( %lld n , Pow(k, n, MOD)); n   fact[0] = inv[0] = 1; n   for (long long int i = 1; i < MAXK; i++) { n     fact[i] = fact[i - 1] * i % MOD; n     inv[i] = Pow(fact[i], MOD - 2, MOD); n   } n   dp[0][0] = 1; n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 1; j <= i; j++) { n       dp[i][j] = j * (dp[i - 1][j] + dp[i - 1][j - 1]) % MOD; n     } n   } n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 0; j <= i; j++) { n       ans = add(ans, mul(Pow(j, n * (m - 2) % MOD, MOD), n                          mul(dp[n][i] * dp[n][i] % MOD, n                              mul(C(k - i, i - j), mul(C(k, i), C(i, j)))))); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #define pb push_back n #define speed ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) n using namespace std; n typedef long long ll; n typedef long double ld; n typedef vector <ll> vll; n typedef pair<ll,ll> pll; n typedef vector <pair<ll,ll>> vpll; n typedef vector <vll> vvll; n  n const ll N=1e5+1; n ll t,n,k; n  n int main() n { n     speed; n     cin>>t; n     while(t--) n     { n         cin>>n>>k; n         vll ans; n         cout<<n-(k+1)/2<<  n ; n         for(int i=(k+1)/2;i<=n;i++) n             if(i!=k) n                 cout<<i<<   ; n         cout<<  n ; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long a[200010]; n long long vis[200100]; n long long x, n, k, sum, y; n string s; n long long dfs(long long x, long long k, long long y) { n   if (x == y) return k; n   return dfs(a[x], k + 1, y); n } n long long poww(long long ba) { n   if (ba == 0) return 1; n   if (ba == 1) return 3; n   if (ba % 2 == 0) return poww(ba / 2) * poww(ba / 2); n   return poww(ba / 2) * poww(ba / 2) * 3; n } n int main() { n   long long q; n   cin >> q; n   while (q--) { n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     for (int i = 1; i <= n; i++) { n       cout << dfs(a[i], 0, i) + 1 <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1010; n string s[maxn]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   int ans = 6, cnt; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       cnt = 0; n       for (int k = 0; k < 6; k++) n         if (s[i][k] != s[j][k]) cnt++; n       ans = min(ans, (cnt + 1) / 2 - 1); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int t; n   long long s, a, b, c; n   scanf( %d , &t); n   while (t--) { n     scanf( %I64d %I64d %I64d %I64d , &s, &a, &b, &c); n     printf( %I64d n , s / c + s / c / a * b); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int i, j; n   cin >> s; n   for (i = 0; i < s.size(); i++) n     if (s[i] ==  . ) break; n   if (s[i - 1] ==  9 ) { n     cout <<  GOTO Vasilisa. ; n     return 0; n   } n   if (s[i + 1] >=  5 ) s[i - 1]++; n   for (j = 0; j < i; j++) cout << s[j]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, k, n2; n   long long dip, cert, losers; n   cin >> n >> k; n   dip = n / (2 * (k + 1)); n   cert = k * dip; n   losers = n - dip - cert; n   cout << dip <<     << cert <<     << losers; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int a[25], b[25]; n int dp[1 << 24]; n int main() { n   int n, k; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   scanf( %d , &k); n   for (int i = 1; i <= k; i++) scanf( %d , &b[i]); n   dp[0] = 1; n   for (int i = 1; i < (1 << n); i++) { n     int sum = 0, ans = 0; n     for (int j = 0; j < n; j++) { n       if ((i >> j) & 1) { n         sum += a[j]; n         ans = (ans + dp[i ^ (1 << j)]) % mod; n         if (sum > 1e9) sum = 1e9 + 1; n       } n     } n     bool ok = 1; n     for (int j = 1; j <= k; j++) { n       if (sum == b[j]) ok = 0; n     } n     if (!ok) continue; n     dp[i] = (dp[i] + ans) % mod; n   } n   printf( %d n , dp[(1 << n) - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long x, long long y) { n   if (x == 0) return y; n   return gcd(y % x, x); n } n int main() { n   int t; n   long long r, b, k; n   scanf( %d , &t); n   while (t--) { n     scanf( %I64d%I64d%I64d , &r, &b, &k); n     long long Gcd = gcd(r, b); n     r /= Gcd; n     b /= Gcd; n     if (r <= (k - 1) * b + 1 && b <= (k - 1) * r + 1) n       printf( OBEY n ); n     else n       printf( REBEL n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1000000], prime[1000000]; n vector<long long> v[3000000]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(), cout.tie(); n   long long e; n   for (long long i = 3; i * i <= 1000000; i += 2) n     if (a[i] == 0) n       for (long long j = i * i; j <= 1000000; j = j + (i * 2)) a[j] = 1; n   prime[0] = 2; n   for (long long i = 3, j = 1; i <= 1000000; i += 2) n     if (a[i] == 0) { n       prime[j++] = i; n       e = j; n     } n   long long n; n   cin >> n; n   long long b, c = 0, d = 1, f = 0, g = 0, h; n   for (long long i = 0; i < n; i++) { n     cin >> b; n     for (long long j = 0;; j++) { n       c = 0; n       if (prime[j] > sqrt(b)) { n         break; n       } n       if (b % prime[j] == 0) { n         while (b % prime[j] == 0) { n           c++; n           b /= prime[j]; n         } n       } n       v[prime[j]].push_back(c); n       g = max(g, prime[j]); n     } n     if (b > 1) { n       g = max(g, b); n       v[b].push_back(1); n     } n   } n   for (long long i = 0; i < 210000; i++) { n     long long y = prime[i]; n     if (v[y].size() == n - 1) { n       sort(v[y].begin(), v[y].end()); n       long long x = v[y][0]; n       d = d * (pow(y, x)); n     } n     if (v[y].size() == n) { n       sort(v[y].begin(), v[y].end()); n       long long x = v[y][1]; n       d = d * (pow(y, x)); n     } n   } n   cout << d << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     long long n, k, s; n     cin >> n >> k >> s; n     if (s >= k && s <= k * (n - 1)) { n       long long step = s / k; n       long long x = s - k * step; n       long long last = 1, fact = 1; n       cout <<  YES  << endl; n       for (int i = 0; i < k; i++) { n         long long tmp = step; n         if (x) { n           tmp++; n           x--; n         } n         last += tmp * fact; n         cout << last <<    ; n         fact *= -1; n       } n       cout << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5023; n int grundy[N], canCut[N], mex[N]; n void init() { n   memset(grundy, 0, sizeof(grundy)); n   memset(canCut, 0, sizeof(canCut)); n   for (int i = 1; i < N; i++) { n     for (int j = 0; j < i; j++) { n       mex[grundy[max(0, j - 1)] ^ grundy[max(0, i - j - 2)]] = i; n     } n     for (int j = 0;; j++) { n       if (mex[j] != i) { n         grundy[i] = j; n         break; n       } n     } n   } n } n bool firstWin(int x, int n) { n   int sg = 0, len = 0; n   for (int i = 0; i < n; i++) { n     if (canCut[i] && abs(x - i) > 1) { n       len++; n     } else { n       sg ^= grundy[len]; n       len = 0; n     } n   } n   return sg == 0; n } n int main() { n   string s; n   while (cin >> s) { n     init(); n     int n = s.size(); n     for (int i = 1; i + 1 < n; i++) { n       if (s[i - 1] == s[i + 1]) canCut[i] = 1; n     } n     int ans = -1; n     for (int i = 0; i < n; i++) { n       if (canCut[i] && firstWin(i, n)) { n         ans = i + 1; n         break; n       } n     } n     if (ans == -1) n       printf( Second n ); n     else n       printf( First n%d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   map<int, int> mp; n   vector<int> ans; n   int a; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     mp[a] = 1; n   } n   n = m; n   for (int i = 1; i <= n; i++) { n     if (i > m) break; n     if (mp[i] == 0) { n       if (i <= m) { n         ans.push_back(i); n         m -= i; n       } n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt, T; n vector<int> g[128]; n void input(int x) { n   if (g[x].size()) return; n   if (cnt == 16) { n     g[x].push_back(1), g[x].push_back(1); n     return; n   } n   cnt++; n   printf( ? %d n , x); n   fflush(stdout); n   int nw, y; n   scanf( %d , &nw); n   while (nw--) { n     scanf( %d , &y); n     g[x].push_back(y); n   } n   return; n } n void solve() { n   cnt = 0; n   for (int i = 0; i < 128; i++) g[i].clear(); n   cin >> n; n   input(1); n   if (g[1].size() == 2) { n     printf( ! 1 n ); n     fflush(stdout); n     return; n   } n   deque<int> q; n   q.push_back(1); n   int nw = 1; n   while (g[nw].size() != 1) { n     for (int i = 0; i < g[nw].size(); i++) n       if (g[g[nw][i]].size() == 0) { n         nw = g[nw][i]; n         break; n       } n     q.push_front(nw); n     input(nw); n     if (g[nw].size() == 2) { n       cout <<  !   << nw << endl; n       fflush(stdout); n       return; n     } n   } n   nw = 1; n   while (g[nw].size() != 1) { n     for (int i = 0; i < g[nw].size(); i++) n       if (g[g[nw][i]].size() == 0) { n         nw = g[nw][i]; n         break; n       } n     q.push_back(nw); n     input(nw); n     if (g[nw].size() == 2) { n       cout <<  !   << nw << endl; n       fflush(stdout); n       return; n     } n   } n   nw = q[q.size() / 2]; n   int dep = q.size() / 2; n   vector<int> pa; n   if (q.size() != 1) { n     pa.push_back(q[q.size() / 2 - 1]); n     pa.push_back(q[q.size() / 2 + 1]); n   } n   int couter = 0; n   while (dep != n - 3 && dep != n - 1) { n     vector<int> maybe; n     input(nw); n     if (g[nw].size() == 2) { n       cout <<  !   << nw << endl; n       fflush(stdout); n       return; n     } n     for (int i = 0; i < g[nw].size(); i++) { n       bool ok = 1; n       for (int j = 0; j < pa.size(); j++) n         if (pa[j] == g[nw][i]) ok = 0; n       if (ok) maybe.push_back(g[nw][i]); n     } n     dep++; n     pa.clear(); n     couter = couter * 10 + maybe.size(); n     if (maybe.size() == 1) { n       pa.push_back(nw); n       nw = maybe[0]; n       continue; n     } n     int nwchk = maybe[0]; n     for (int i = 0; i < dep - 2; i++) { n       input(nwchk); n       if (g[nwchk].size() == 2) { n         cout <<  !   << nwchk << endl; n         fflush(stdout); n         return; n       } n       for (int j = 0; j < g[nwchk].size(); j++) n         if (g[g[nwchk][j]].size() == 0) nwchk = g[nwchk][j]; n     } n     input(nwchk); n     if (g[nwchk].size() == 2) { n       cout <<  !   << nwchk << endl; n       fflush(stdout); n       return; n     } n     pa.push_back(nw); n     if (g[nwchk].size() == 1) n       nw = maybe[1]; n     else n       nw = maybe[0]; n   } n   if (dep == n - 1) { n     cout <<  !   << nw << endl; n     fflush(stdout); n     return; n   } n   input(nw); n   vector<int> maybe; n   for (int i = 0; i < g[nw].size(); i++) { n     maybe.push_back(g[nw][i]); n     input(g[nw][i]); n     if (g[g[nw][i]].size() == 2) { n       cout <<  !   << g[nw][i] << endl; n       fflush(stdout); n       return; n     } n   } n   for (int i = 0; i < maybe.size(); i++) n     for (int j = 0; j < g[maybe[i]].size(); j++) { n       input(g[maybe[i]][j]); n       if (g[g[maybe[i]][j]].size() == 2) { n         cout <<  !   << g[maybe[i]][j] << endl; n         fflush(stdout); n         return; n       } n     } n   if (T == 7) { n     cout <<  !   << couter; n     fflush(stdout); n     exit(0); n   } n   cout <<  ! -1 n ; n   fflush(stdout); n   return; n } n int main() { n   cin >> T; n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int INF = 1000000000; n const ll LINF = 1001002003004005006ll; n int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (b < a) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (int i = 0; i < (int)v.size(); i++) n     os << v[i] << (i + 1 == (int)v.size() ?    :    ); n   return os; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &v) { n   for (T &x : v) is >> x; n   return is; n } n signed main() { n   int n; n   cin >> n; n   vector<int> bx; n   for (int i = 0; i < (n); i++) { n     cout <<  ?   << 0 <<     << i << endl; n     int x; n     cin >> x; n     bx.push_back(x); n   } n   vector<int> px; n   for (int i = 0; i < (n); i++) { n     cout <<  ?   << i <<     << 0 << endl; n     int x; n     cin >> x; n     px.push_back(x); n   } n   int cnt = 0; n   vector<int> print_res; n   for (int i = 0; i < (n); i++) { n     bool f = true; n     vector<int> b; n     for (auto x : bx) b.push_back(x ^ i); n     auto v = b; n     sort(begin(v), end(v)); n     for (int j = 0; j < (n); j++) n       if (v[j] != j) f = false; n     if (!f) continue; n     vector<int> p; n     for (auto x : px) p.push_back(x ^ b[0]); n     for (int j = 0; j < (n); j++) n       if (p[b[j]] != j) f = false; n     if (!f) continue; n     cnt++; n     print_res = p; n   } n   cout <<  !  << endl; n   cout << cnt << endl; n   cout << print_res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > h; n vector<vector<long long> > g; n long long p[500001][21]; n long long volume[500001]; n long long n, dt, ans, s, f; n void dfs(long long v, long long m) { n   p[v][0] = m; n   for (long long i = 0; i < g[v].size(); i++) { n     long long k = g[v][i]; n     if (k != m) { n       volume[k] = volume[v] + 1; n       dfs(k, v); n     } n   } n } n void prepare() { n   for (long long i = 0; i <= 500000; i++) n     for (long long k = 1; k <= 20; k++) p[i][k] = -1; n   dfs(1, -1); n   for (long long k = 1; k <= 20; k++) n     for (long long i = 1; i <= 500000; i++) n       if (p[i][k - 1] != -1) p[i][k] = p[p[i][k - 1]][k - 1]; n   ans = 1; n   cout << ans <<    ; n   s = 1; n   f = 2; n } n long long kanst(long long v, long long k) { n   while (k > 0) { n     long long k0 = log2(k); n     v = p[v][k0]; n     k -= 1 << k0; n   } n   return v; n } n long long lca(long long a, long long b) { n   if (a == b) return a; n   if (volume[b] > volume[a]) swap(a, b); n   a = kanst(a, volume[a] - volume[b]); n   if (a == b) return a; n   for (long long j = 20; j >= 0; j--) { n     if (p[a][j] != p[b][j]) { n       a = p[a][j]; n       b = p[b][j]; n     } n   } n   return p[a][0]; n } n int main() { n   cin >> n; n   g.resize(200001); n   vector<long long> d; n   d.resize(n + 1); n   for (long long i = 2; i <= n; i++) { n     long long a; n     cin >> a; n     d[i] = i; n     g[i].push_back(a); n     g[a].push_back(i); n   } n   prepare(); n   for (long long k = 3; k <= n; k++) { n     if (volume[s] + volume[k] - 2 * volume[lca(s, k)] > ans) { n       ans = volume[s] + volume[k] - 2 * volume[lca(s, k)]; n       f = k; n     } n     if (volume[f] + volume[k] - 2 * volume[lca(f, k)] > ans) { n       ans = volume[f] + volume[k] - 2 * volume[lca(f, k)]; n       s = k; n     } n     cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n char s[5005], t[5005]; n long long DP[5005][5005]; n int main(int argc, char** argv) { n   scanf( %s%s , s, t); n   int lens = strlen(s); n   int lent = strlen(t); n   long long ans = 0; n   for (int i = 1; i <= lens; i++) { n     for (int j = 1; j <= lent; j++) { n       DP[i][j] = DP[i][j - 1]; n       if (s[i - 1] == t[j - 1]) n         DP[i][j] = (DP[i][j] + DP[i - 1][j - 1] + 1) % mod; n     } n     ans = (ans + DP[i][lent]) % mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using ::std::cin; n using ::std::cout; n using ::std::endl; n using ::std::multiset; n using ::std::pair; n using ::std::vector; n int main() { n   ::std::ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   vector<pair<int, int>> tr; n   for (int i = 0; i < m; ++i) { n     int t, r; n     cin >> t >> r; n     while (!tr.empty() && tr.back().second <= r) tr.pop_back(); n     tr.push_back({t, r}); n   } n   tr.push_back({-1, 0}); n   multiset<int> s; n   for (int i = 0; i < tr[0].second; ++i) s.insert(a[i]); n   for (int i = 0; i < static_cast<int>(tr.size()); ++i) { n     for (int j = tr[i].second - 1; j >= tr[i + 1].second; --j) { n       if (tr[i].first == 2) { n         a[j] = *s.begin(); n         s.erase(s.begin()); n       } else { n         a[j] = *--s.end(); n         s.erase(--s.end()); n       } n     } n   } n   for (int x : a) cout << x <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n int ans[N]; n void solve() { n   int n; n   cin >> n; n   int d = 0; n   for (int i = 0; i <= 30; i++) { n     ans[i] = (1 << i); n   } n   int sum = 0; n   for (int i = 0; i <= 30; i++) { n     sum += ans[i]; n     if (sum >= n) { n       d = i; n       break; n     } n   } n   int extra = sum - n; n   for (int i = d; i >= 1; i--) { n     long long need = (ans[i] - ans[i - 1] + 0ll) * (d - i + 1ll); n     if (extra > need) { n       extra -= need; n       for (int j = d; j >= i; j--) ans[j] = ans[i - 1]; n     } else { n       int dv = (extra / (d - i + 1)); n       int c = (extra % (d - i + 1)); n       for (int j = d; j >= i; j--) ans[j] -= dv; n       for (int j = i; j < i + c; j++) ans[j]--; n       break; n     } n   } n   cout << d <<   n ; n   for (int i = 1; i <= d; i++) cout << ans[i] - ans[i - 1] <<    ; n   cout <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const double PI = 3.1415926535897932384626; n const int KL = 1e6; n const long long MOD = 1e9 + 7; n using namespace std; n void show(complex<double> a) { n   cout << fixed << setprecision(10) << a.real() <<   +   << a.imag() <<  i  n        << endl; n } n complex<double> rot(complex<double> a, complex<double> o, double alpha) { n   return (a - o) * polar(1.0, alpha * PI / 180.0) + o; n } n complex<double> intersection(complex<double> a, complex<double> b, double h) { n   double m = (a.imag() - b.imag()) / (a.real() - b.real()); n   double p = a.imag() - m * a.real(); n   complex<double> ret((h - p) / m, h); n   return ret; n } n double h, w; n double alpha; n int main() { n   cin >> w >> h >> alpha; n   if (w < h) swap(h, w); n   if (alpha >= 90) alpha = 180 - alpha; n   double wt = sqrt(w * w + h * h); n   if (alpha == 0) { n     cout << fixed << setprecision(10) << w * h << endl; n     return 0; n   } n   complex<double> a(-w / 2.0, h / 2.0); n   complex<double> b(w / 2.0, h / 2.0); n   complex<double> d(w / 2.0, -h / 2.0); n   complex<double> o(0, 0); n   double mx_angle = 2 * abs(arg(b)) * 180.0 / PI; n   if (alpha >= mx_angle) { n     cout << fixed << setprecision(10) << h * h / sin(alpha * PI / 180.0) n          << endl; n     return 0; n   } n   complex<double> a1 = rot(a, o, alpha); n   complex<double> b1 = rot(b, o, alpha); n   complex<double> c1 = rot(d, o, alpha); n   complex<double> A = intersection(a1, b1, h / 2); n   complex<double> B = intersection(b1, c1, h / 2); n   double d1 = A.real() - a.real(); n   double d2 = b.real() - B.real(); n   double wt1 = d1 / cos(alpha * PI / 180.0); n   double wt2 = d2 / sin(alpha * PI / 180.0); n   double area = n       d1 * wt1 * sin(alpha * PI / 180.0) + d2 * wt2 * cos(alpha * PI / 180.0); n   double ans = w * h - area; n   cout << fixed << setprecision(10) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n struct SFountain { n   size_t beauty; n   size_t price; n   SFountain() = default; n   SFountain(size_t b, size_t p) : beauty(b), price(p) {} n   static bool less_price(const SFountain &lhs, const SFountain &rhs) { n     return lhs.price < rhs.price; n   } n   static bool less_beauty(const SFountain &lhs, const SFountain &rhs) { n     return lhs.beauty < rhs.beauty; n   } n }; n size_t maxSum(const std::vector<SFountain> &fountains, size_t max_price) { n   if (fountains.size() < 2) return 0; n   size_t max_beauty_total = 0; n   size_t max_beauty = 0; n   std::vector<SFountain> x; n   for (const SFountain &fountain : fountains) { n     auto upper = std::upper_bound(x.begin(), x.end(), n                                   SFountain(0, max_price - fountain.price), n                                   SFountain::less_price); n     if (upper != x.begin()) { n       --upper; n       max_beauty_total = n           std::max(max_beauty_total, fountain.beauty + upper->beauty); n     } n     if (max_beauty < fountain.beauty) { n       max_beauty = fountain.beauty; n       x.push_back(fountain); n     } n   } n   return max_beauty_total; n } n int main() { n   size_t n, c, d, maxC = 0, maxD = 0; n   std::cin >> n >> c >> d; n   std::vector<SFountain> coins; n   std::vector<SFountain> diamonds; n   coins.reserve(n); n   diamonds.reserve(n); n   for (size_t i = 0; i < n; ++i) { n     SFountain fountain; n     char price_type; n     std::cin >> fountain.beauty >> fountain.price >> price_type; n     if (price_type ==  C  && fountain.price <= c) { n       coins.push_back(fountain); n       maxC = std::max(maxC, fountain.beauty); n     } else if (price_type ==  D  && fountain.price <= d) { n       diamonds.push_back(fountain); n       maxD = std::max(maxD, fountain.beauty); n     } n   } n   std::sort(coins.begin(), coins.end(), SFountain::less_price); n   std::sort(diamonds.begin(), diamonds.end(), SFountain::less_price); n   size_t maxMixed = !coins.empty() && !diamonds.empty() ? maxC + maxD : 0; n   std::cout << std::max(std::max(maxSum(coins, c), maxSum(diamonds, d)), n                         maxMixed) n             << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, -1, 1, -1, -1, 1, 1}; n int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1}; n bool mark[100007]; n long long int n, in = -1, prime[100007]; n void seive() { n   prime[++in] = 2; n   for (long long int i = 3; i * i < 100007; i += 2) { n     if (mark[i] == 0) n       for (long long int j = i * i; j < 100007; j += 2 * i) mark[j] = 1; n   } n   for (long long int i = 3; i < 100007; i += 2) { n     if (mark[i] == 0) prime[++in] = i; n   } n } n int main() { n   seive(); n   cin >> n; n   long long int c, div = 1; n   for (long long int i = 0; i <= in; i++) { n     if (n % prime[i] == 0) { n       c = 1; n       while (n % prime[i] == 0) { n         n /= prime[i]; n         c++; n       } n       div *= c; n     } n   } n   if (n > 2) div *= 2; n   cout << div << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[200001]; n vector<int> G[200001]; n int res[200001]; n vector<int> divisor; n vector<int> now; n int gcd(int x, int y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n void dfs(int v, int p) { n   for (int i = 0; i < G[v].size(); i++) { n     if (p != G[v][i]) { n       res[G[v][i]] = gcd(res[v], a[G[v][i]]); n       dfs(G[v][i], v); n     } n   } n } n void dfs2(int v, int p, int dist) { n   for (int i = 0; i < divisor.size(); i++) { n     now[i] += (a[v] % divisor[i] == 0); n     if (now[i] >= dist) { n       res[v] = max(res[v], divisor[i]); n     } n   } n   for (int i = 0; i < G[v].size(); i++) { n     if (p != G[v][i]) { n       dfs2(G[v][i], v, dist + 1); n     } n   } n   for (int i = 0; i < divisor.size(); i++) { n     now[i] -= (a[v] % divisor[i] == 0); n   } n } n int main(void) { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     int f, t; n     scanf( %d%d , &f, &t); n     f--; n     t--; n     G[f].push_back(t); n     G[t].push_back(f); n   } n   int tmp = a[0]; n   a[0] = 0; n   dfs(0, -1); n   a[0] = tmp; n   for (int i = 1; i * i <= a[0]; i++) { n     if (a[0] % i == 0) { n       divisor.push_back(i); n       divisor.push_back(a[0] / i); n       if (i * i == a[0]) { n         divisor.pop_back(); n       } n     } n   } n   sort(divisor.begin(), divisor.end()); n   now.resize(divisor.size()); n   dfs2(0, -1, 0); n   for (int i = 0; i < n; i++) { n     printf( %d%c , res[i], i + 1 == n ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define int long long n  n void solve() n { n     int n, m, k; n     cin >> n >> m >> k; n     vector<tuple<int, int, int>> edges(m + 2), edges_large, edges_small; n     vector<int> values; n     for (int i = 1; i <= m; i++) n     { n         int a, b, c; n         cin >> a >> b >> c; n         edges[i] = {a, b, c}; n         if (c > k) n             edges_large.push_back(edges[i]); n         else n             edges_small.push_back(edges[i]); n         values.push_back(c); n     } n     sort(values.begin(), values.end()); n     vector<int> fa(n + 2); n     for (int i = 1; i <= n; i++) n         fa[i] = i; n     function<int(int)> find = [&](int x) -> int { n         return x == fa[x] ? x : fa[x] = find(fa[x]); n     }; n     int tot = 1; n     function<void(int, int)> merge = [&](int x, int y) -> void { n         x = find(x); n         y = find(y); n         if (x != y) n             fa[x] = y, tot++; n     }; n     for (auto [u, v, w] : edges_small) n         merge(u, v); n     int flag = tot == n; n     if (flag) n     { n         int i = lower_bound(values.begin(), values.end(), k) - values.begin(); n         int j = i - 1; n         int ans = 1e18; n         if (i >= 0 && i < m) n             ans = min(ans, abs(k - values[i])); n         if (j >= 0 && j < m) n             ans = min(ans, abs(k - values[j])); n         cout << ans << endl; n     } n     else n     { n         int ans = 0; n         vector<pair<int, pair<int, int>>> ve; n         for (auto [x, y, z] : edges_large) n             ve.push_back({z, {x, y}}); n         sort(ve.begin(), ve.end()); n         for (int i = 0; i < ve.size(); i++) n         { n             auto [w, pr] = ve[i]; n             auto [u, v] = pr; n             if (find(u) != find(v)) n                 merge(u, v), ans += w - k; n             if (tot == n) n                 break; n         } n         cout << ans << endl; n     } n } n  n signed main() n { n     ios::sync_with_stdio(false); n  n     int t; n     cin >> t; n     while (t--) n     { n         solve(); n     } n }


#include <bits/stdc++.h> n using namespace std; n long long int visited[100001]; n vector<long long int> adj[100001]; n long long int sum = 0; n void dfs(long long int s) { n   if (visited[s] == 1) { n     return; n   } n   sum++; n   visited[s] = 1; n   for (auto u : adj[s]) { n     dfs(u); n   } n } n long long int binpow(long long int x, long long int n) { n   if (n == 0) return 1; n   long long u = binpow(x, n / 2); n   u = (u * u); n   if (n % 2 == 1) u = (u * x); n   return u; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m; n   cin >> n >> m; n   while (m--) { n     long long int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   memset(visited, 0, sizeof(visited)); n   ; n   long long int ans = 1; n   for (long long int i = 1; i <= n; i++) { n     if (visited[i] == 0) { n       sum = 0; n       dfs(i); n       long long int y = sum - 1; n       long long int r = binpow(2, y); n       ans = ans * r; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[10010], leftx[10010], rightx[10010], l[5010], r[5010]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n, x; n   cin >> n >> x; n   vector<int> ry; n   for (int i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     l[i] = a; n     r[i] = b; n     ry.push_back(a); n     ry.push_back(b); n   } n   ry.push_back(x); n   sort(ry.begin(), ry.end()); n   set<int> rt; n   for (auto val : ry) { n     rt.insert(val); n   } n   ry.clear(); n   for (auto val : rt) { n     ry.push_back(val); n   } n   int ind = -1; n   for (int i = 0; i < ry.size(); i++) { n     if (ry[i] == x) ind = i; n   } n   for (int i = 0; i < ry.size(); i++) dp[i] = 1e18; n   for (int i = 0; i < ry.size(); i++) { n     if (ry[i] == x) dp[i] = 0; n   } n   for (int i = 0; i <= n; i++) { n     if (i) { n       for (int j = 0; j < ry.size(); j++) { n         long long val = 0; n         if (ry[j] < l[i - 1] || ry[j] > r[i - 1]) n           val = min(abs(l[i - 1] - ry[j]), abs(r[i - 1] - ry[j])); n         dp[j] = min(leftx[j] + ry[j] + val, rightx[j] - ry[j] + val); n       } n     } n     leftx[0] = dp[0] - ry[0]; n     for (int j = 1; j < ry.size(); j++) n       leftx[j] = min(leftx[j - 1], dp[j] - ry[j]); n     rightx[ry.size() - 1] = dp[ry.size() - 1] + ry[ry.size() - 1]; n     for (int j = (int)ry.size() - 2; j >= 0; j--) n       rightx[j] = min(rightx[j + 1], dp[j] + ry[j]); n   } n   long long mini = 1e18; n   for (int j = 0; j < ry.size(); j++) mini = min(mini, dp[j]); n   cout << mini <<   n ; n } n


#include <bits/stdc++.h> n bool chCount[26]; n int main() { n   int n, m; n   std::cin >> n >> m; n   int max = -1; n   for (; n >= 1; --n) { n     int min = 1000000000 + 10; n     for (int i = 0; i < m; ++i) { n       int x; n       std::cin >> x; n       min = std::min(x, min); n     } n     max = std::max(min, max); n   } n   std::cout << max; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int a, m; n   cin >> a >> m; n   a %= m; n   map<int, bool> vis; n   while (!vis[a]) { n     vis[a] = true; n     if (!a) { n       cout <<  Yes n ; n       return 0; n     } n     a *= 2; n     a %= m; n   } n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nMax = 100007; n int parent[nMax]; n vector<int> g[nMax]; n bool used[nMax]; n void dfs(int root, int parent, int& cnt, int b, bool& bad) { n   if (root == b) { n     bad = true; n   } n   used[root] = true; n   ++cnt; n   for (int i = 0; i < g[root].size(); ++i) { n     int to = g[root][i]; n     if (used[to] == false && to != parent) dfs(to, root, cnt, b, bad); n   } n } n int main() { n   int n, pos; n   scanf( %d%d , &n, &pos); n   for (int i = 0; i < n; ++i) { n     int a; n     scanf( %d , &a); n     parent[i] = a - 1; n     if (a != 0) { n       g[i].push_back(a - 1); n       g[a - 1].push_back(i); n     } n   } n   --pos; n   int h = 1; n   int t = pos; n   while (parent[t] != -1) { n     ++h; n     t = parent[t]; n   } n   vector<int> c; n   for (int i = 0; i < n; ++i) { n     int cnt = 0; n     bool bad = false; n     if (used[i] == false) { n       dfs(i, -1, cnt, pos, bad); n       if (!bad) c.push_back(cnt); n     } n   } n   int to = (int)pow(2.0, (int)c.size()); n   set<int> s; n   set<int>::iterator it; n   for (int i = 1; i < to + 1; ++i) { n     int t = i, sum = 0; n     for (int j = 0; j < c.size(); ++j) { n       if (t & 1) sum += c[j]; n       t >>= 1; n     } n     s.insert(sum); n   } n   set<int> ans; n   ans.insert(h); n   for (it = s.begin(); it != s.end(); ++it) ans.insert(*it + h); n   for (it = ans.begin(); it != ans.end(); ++it) cout << *it << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n const int size = 1 << 8, M = 1e6 + 10; n int n, q, w[N], head[N], ne[N], cnt; n void add(int f, int t) { n   w[++cnt] = t; n   ne[cnt] = head[f]; n   head[f] = cnt; n } n int fa[N], a[N], h[N]; n void dfs(int x) { n   for (int i = head[x]; i; i = ne[i]) { n     int v = w[i]; n     if (fa[v]) continue; n     fa[v] = x; n     h[v] = h[x] + 1; n     dfs(v); n   } n } n int ance[N], go[M][2], id, dp[N][size]; n void trie(int x) { n   for (int i = 15, p = 1; i >= 0; i--) { n     int w = x >> i & 1; n     if (!go[p][w]) go[p][w] = ++id; n     p = go[p][w]; n   } n } n int query(int x) { n   int ans = 0; n   for (int i = 15, p = 1; i >= 0; i--) { n     int w = x >> i & 1; n     if (go[p][w ^ 1]) n       ans |= (1 << i), p = go[p][w ^ 1]; n     else n       p = go[p][w]; n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     add(u, v); n     add(v, u); n   } n   fa[1] = h[1] = 1; n   dfs(1); n   fa[1] = 0; n   for (int i = 1; i <= n; ++i) { n     for (; id; id--) go[id][0] = go[id][1] = 0; n     id = 1; n     int p = i; n     for (int j = 0; p && j < size; j++, p = fa[p]) trie(a[p] ^ (h[i] - h[p])); n     ance[i] = p; n     for (int j = 0; j < size; j++) dp[i][j] = query(j << 8); n   } n   for (int i = 1; i <= q; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     int p = v, dep = 0, ans = 0; n     for (; h[p] - size >= h[u]; p = ance[p], dep++) ans = max(ans, dp[p][dep]); n     for (; h[p] >= h[u]; p = fa[p]) ans = max(ans, a[p] ^ (h[v] - h[p])); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int n = 10000; n long long qm[6][n + 2]; n void ask(int i, long long b) { n   int j = min(b, (long long)n); n   vector<long long> v; n   long long r = qm[i - 1][j]; n   for (int s = 1; s <= j; ++s) { n     v.push_back(r + b); n     ++r; n     r += qm[i - 1][min(r + j, (long long)n)]; n     if (r > (long long)2e18) break; n   } n   printf( %d n , int(v.size())); n   for (auto t : v) printf( %lld  , t); n   cout << endl; n   int t; n   cin >> t; n   if (t == -1) exit(0); n   if (t == 0) ask(i - 1, b); n   r = qm[i - 1][j]; n   for (int s = 1; s <= j; ++s) { n     ++r; n     --t; n     if (!t) ask(i - 1, r + b); n     r += qm[i - 1][min(r + j, (long long)n)]; n   } n } n int main() { n   for (int i = 1; i <= 4; ++i) { n     for (int j = 1; j <= n; ++j) { n       long long r = qm[i - 1][j]; n       for (int s = 1; s <= j; ++s) { n         ++r, r += qm[i - 1][min(r + j, (long long)n)]; n         if (r > (long long)2e18) break; n       } n       qm[i][j] = r; n     } n   } n   ask(5, 1); n } n


#include <bits/stdc++.h> n using namespace std; n void swap(int &a, int &b) { n   int temp; n   temp = a; n   a = b; n   b = temp; n } n void printarr(int a[], int n) { n   for (int i = 0; i < 2 * n; i++) cout << a[i] <<    ; n   cout << endl; n } n int main() { n   long long int n, k, v; n   cin >> n; n   int a[2 * n], i, j, count = 0; n   for (i = 0; i < 2 * n; i++) cin >> a[i]; n   for (i = 0; i < 2 * n; i += 2) { n     if (a[i] != a[i + 1]) { n       j = a[i]; n       for (k = i + 1; k < 2 * n; k++) { n         if (j == a[k]) { n           v = k; n           while (v > i + 1) { n             count++; n             swap(a[v], a[v - 1]); n             v--; n           } n         } n       } n     } n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int add(int a, int b) { return a + b - (a + b >= MOD) * MOD; } n int mult(int a, int b) { return ((long long)a * b) % MOD; } n int n, h, w; n char s[500013]; n int dX[200], dY[200]; n int diffx, diffy; n int maxx, minx; n int maxy, miny; n int hasx_[1000013], hasy_[1000013]; n int& hasx(int i) { return hasx_[i + 500000]; } n int& hasy(int i) { return hasy_[i + 500000]; } n void calc() { n   int x = 0, y = 0; n   for (int i = 0; i < n; i++) { n     x += dX[s[i]]; n     y += dY[s[i]]; n     maxx = max(maxx, x); n     minx = min(minx, x); n     maxy = max(maxy, y); n     miny = min(miny, y); n     if (!hasx(x)) hasx(x) = i + 1; n     if (!hasy(y)) hasy(y) = i + 1; n   } n   diffx = x; n   diffy = y; n } n int main() { n   dX[ U ] = -1; n   dX[ D ] = 1; n   dY[ L ] = -1; n   dY[ R ] = 1; n   scanf( %d%d%d %s , &n, &h, &w, &s); n   calc(); n   if (maxx - minx + 1 <= h && maxy - miny + 1 <= w && diffx == 0 && diffy == 0) n     return printf( -1 n ), 0; n   int bot = -1, top = h; n   int lef = -1, rig = w; n   int x = 0, y = 0; n   int step = 0; n   int ans = 0; n   while (top > bot + 1 && rig > lef + 1) { n     int tryt = 1e9; n     if (x + top - 1 + maxx >= h) tryt = hasx(h - (top + x - 1)); n     int tryb = 1e9; n     if (x + bot + 1 + minx <= -1) tryb = hasx(-1 - (bot + x + 1)); n     int tryr = 1e9; n     if (y + rig - 1 + maxy >= w) tryr = hasy(w - (rig + y - 1)); n     int tryl = 1e9; n     if (y + lef + 1 + miny <= -1) tryl = hasy(-1 - (lef + y + 1)); n     if (min(min(tryt, tryb), min(tryr, tryl)) < 1e7) { n       if (tryt < min(tryb, min(tryr, tryl))) { n         int cur = mult(rig - lef - 1, mult(step, n) + tryt); n         ans = add(ans, cur); n         top -= 1; n       } else if (tryb < min(tryt, min(tryr, tryl))) { n         int cur = mult(rig - lef - 1, mult(step, n) + tryb); n         ans = add(ans, cur); n         bot += 1; n       } else if (tryr < min(tryt, min(tryb, tryl))) { n         int cur = mult(top - bot - 1, mult(step, n) + tryr); n         ans = add(ans, cur); n         rig -= 1; n       } else if (tryl < min(tryt, min(tryb, tryr))) { n         int cur = mult(top - bot - 1, mult(step, n) + tryl); n         ans = add(ans, cur); n         lef += 1; n       } n     } else { n       x += diffx; n       y += diffy; n       step += 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline void R(int &xx) { n   xx = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     xx = xx + xx + (xx << 3) + ch - 48, ch = getchar(); n } n inline void R(long long &xx) { n   xx = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     xx = xx + xx + (xx << 3) + ch - 48, ch = getchar(); n } n int n, prime[100011], tot; n bool f[100011], u[100011]; n vector<pair<int, int> > ans; n vector<int> tmp; n inline void init() { n   for (int i = 2; i <= n; i++) { n     if (!f[i]) prime[++tot] = i; n     for (int j = 1; j <= tot && i * prime[j] < 100011; j++) { n       f[i * prime[j]] = 1; n       if (!(i % prime[j])) break; n     } n   } n } n int main() { n   R(n); n   init(); n   for (int i = tot; i >= 1; i--) { n     int pr = prime[i]; n     tmp.clear(); n     for (int j = pr; j <= n; j += pr) n       if (!u[j]) tmp.push_back(j); n     if (tmp.size() & 1) swap(tmp[1], tmp[tmp.size() - 1]); n     for (int t = 0; t < ((int)tmp.size() - 1); t += 2) { n       ans.push_back(make_pair(tmp[t], tmp[t + 1])); n       u[tmp[t]] = u[tmp[t + 1]] = 1; n     } n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) n     printf( %d %d n , ans[i].first, ans[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> b, c; n long long s[1000000]; n long long d[1000000]; n int F(long long x) { n   vector<long long> a = b; n   a.push_back(x); n   sort(a.rbegin(), a.rend()); n   long long n = a.size(); n   s[0] = 0; n   for (int i = 1; i <= n; ++i) { n     s[i] = s[i - 1] + a[i - 1]; n     d[i] = -1; n   } n   int y = 1; n   int pos = a.size() - 1; n   while (pos >= 0 && y <= n) { n     if (a[pos] > y && pos + 1 > y) { n       d[y] = pos + 1; n       ++y; n       continue; n     } n     --pos; n   } n   for (long long i = 1; i <= n; ++i) { n     if (d[i] == -1) { n       d[i] = s[n] - s[i]; n       continue; n     } n     d[i] = (d[i] - i) * i + s[n] - s[d[i]]; n   } n   if (s[n] > n * (n - 1)) { n     return -1; n   } n   for (long long k = 1; k < n; ++k) { n     if (s[k] > k * (k - 1) + d[k]) { n       if (a[k - 1] > x) { n         return -1; n       } n       return 1; n     } n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   int n; n   cin >> n; n   int t = 0; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     b.push_back(x); n     t += x; n     t %= 2; n   } n   for (int i = 0; i <= n + 2; ++i) { n     if (i % 2 == t) { n       c.push_back(i); n     } n   } n   int l = -1; n   int r = c.size(); n   int pos = -1; n   while (r - l > 1) { n     int m = (l + r) / 2; n     if (F(c[m]) == 1) { n       r = m; n       continue; n     } n     if (F(c[m]) == -1) { n       l = m; n       continue; n     } n     pos = m; n     break; n   } n   if (pos == -1) { n     cout << -1; n     return 0; n   } n   l = -1; n   r = pos + 1; n   while (r - l > 1) { n     int m = (l + r) / 2; n     if (F(c[m]) == 0) { n       r = m; n     } else { n       l = m; n     } n   } n   int pos1 = r; n   l = pos - 1; n   r = c.size(); n   while (r - l > 1) { n     int m = (l + r) / 2; n     if (F(c[m]) == 0) { n       l = m; n     } else { n       r = m; n     } n   } n   for (int i = pos1; i <= l; ++i) { n     cout << c[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 10; n const int MN = 1e5 + 10; n int n, a[MN << 1], b[MN << 1]; n struct cmp { n   bool operator()(const int &x, const int &y) { n     if (a[x] + b[x] != a[y] + b[y]) return a[x] + b[x] > a[y] + b[y]; n     return x > y; n   } n }; n set<int, cmp> S; n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[2 * i] >> b[2 * i] >> a[2 * i + 1] >> b[2 * i + 1]; n     int x = a[2 * i] + b[2 * i], y = a[2 * i + 1] + b[2 * i + 1]; n     if (a[2 * i] > b[2 * i + 1] || b[2 * i] > a[2 * i + 1]) { n       if (x >= y) n         S.insert(2 * i), S.insert(2 * i + 1); n       else { n         if (a[2 * i] > b[2 * i + 1]) n           ans += a[2 * i] - b[2 * i + 1]; n         else n           ans += a[2 * i + 1] - b[2 * i]; n       } n     } n   } n   bool turn = false; n   while (!S.empty()) { n     int x = *S.begin(); n     S.erase(S.begin()); n     if (!turn) n       ans += a[x]; n     else n       ans -= b[x]; n     turn = !turn; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(), f = 0; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = 1; n   for (; c >=  0  && c <=  9 ; c = getchar()) n     x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n } n const int P = 1e9 + 7; n int n, D, dp[3005][3005], s[3005][3005], res; n struct edge { n   int to, nxt; n } e[3005]; n int et, head[3005], iv[3005]; n inline int ksm(int x, int q = P - 2) { n   int r = 1; n   for (; q; q >>= 1, x = 1ll * x * x % P) n     if (q & 1) r = 1ll * r * x % P; n   return r; n } n inline void adde(int x, int y) { e[++et] = (edge){y, head[x]}, head[x] = et; } n inline void dfs(int x) { n   for (int i = 1; i <= n + 1; i++) dp[x][i] = 1; n   for (int i = head[x]; i; i = e[i].nxt) { n     dfs(e[i].to); n     for (int j = 1; j <= n + 1; j++) n       dp[x][j] = 1ll * dp[x][j] * s[e[i].to][j] % P; n   } n   for (int i = 1; i <= n + 1; i++) s[x][i] = (s[x][i - 1] + dp[x][i]) % P; n } n inline int inv(int x) { return x < 0 ? P - iv[-x] : iv[x]; } n int main() { n   read(n), read(D); n   for (int i = 2, f; i <= n; i++) read(f), adde(f, i); n   iv[1] = 1; n   for (int i = 2; i <= n + 1; i++) iv[i] = 1ll * iv[P % i] * (P - P / i) % P; n   dfs(1); n   for (int i = 1, w = 1; i <= n + 1; n        (res += 1ll * w * s[1][i] % P) %= P, w = 1, i++) n     for (int j = 1; j <= n + 1; j++) n       if (i ^ j) w = 1ll * w * (D - j) % P * inv(i - j) % P; n   return printf( %d n , (res + P) % P), 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 20 + 5; n const int Mod = 1e9 + 7; n int inv[SIZE], n; n long long a[SIZE], m, ans; n inline long long read() { n   char ch = getchar(); n   long long f = 1, x = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline int Qpow(int b, int p) { n   long long ans = 1, t = b; n   for (; p; p >>= 1, t = t * t % Mod) n     if (p & 1) ans = ans * t % Mod; n   return ans % Mod; n } n inline int C(long long y, int x) { n   if (y < 0 || x < 0 || y < x) return 0; n   y %= Mod; n   if (y == 0 || x == 0) return 1; n   int ans = 1; n   for (int i = 0; i < x; i++) ans = (long long)ans * (y - i) % Mod; n   for (int i = 1; i <= x; i++) ans = (long long)ans * inv[i] % Mod; n   return ans; n } n int main() { n   for (int i = 1; i <= 20; i++) inv[i] = Qpow(i, Mod - 2); n   std::cin >> n >> m; n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int x = 0; x < (1 << n); x++) { n     if (!x) n       ans = (ans + C(n + m - 1, n - 1)) % Mod; n     else { n       long long t = n + m; n       int p = 0; n       for (int i = 0; i < n; i++) { n         if (x >> i & 1) { n           p++; n           t -= a[i + 1]; n         } n       } n       t -= p + 1; n       if (p & 1) n         ans = (ans - C(t, n - 1)) % Mod; n       else n         ans = (ans + C(t, n - 1)) % Mod; n     } n   } n   printf( %lld , (ans + Mod) % Mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1e5 + 5; n int n; n int M[9][2] = {{0, 0},  {-1, -1}, {1, 1},  {0, 1}, {1, 0}, n                {0, -1}, {-1, 0},  {1, -1}, {-1, 1}}; n int compute(vector<int> Arr, int A0, int A1, int val) { n   Arr[0] = A0; n   Arr[1] = A1; n   int d = Arr[1] - Arr[0]; n   int ans = val; n   for (int i = 2; i < n; i++) { n     if (d + Arr[i - 1] != Arr[i]) { n       if (d + Arr[i - 1] != Arr[i] + 1) { n         if (d + Arr[i - 1] != Arr[i] - 1) { n           return -1; n         } else { n           Arr[i]--; n           ans++; n         } n       } else { n         Arr[i]++; n         ans++; n       } n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int val; n   cin >> n; n   vector<int> A; n   for (int i = 0; i < n; cin >> val, A.push_back(val), i++) n     ; n   int ans = -1; n   for (int i = 0; i < 9; i++) { n     int num = n         compute(A, A[0] + M[i][0], A[1] + M[i][1], abs(M[i][0]) + abs(M[i][1])); n     if (num != -1) { n       if (ans == -1) n         ans = num; n       else n         ans = min(ans, num); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 200100; n char a[N], b[N]; n int cnt[330]; n int main() { n   int la, lb, i; n   long long ans = 0; n   scanf( %s%s , a, b); n   la = strlen(a), lb = strlen(b); n   for (i = 0; i < lb - la; i++) cnt[b[i]]++; n   for (i = 0; i < la; i++) { n     cnt[b[lb - la + i]]++; n     if (a[i] ==  0 ) n       ans += cnt[ 1 ]; n     else n       ans += cnt[ 0 ]; n     cnt[b[i]]--; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[1100000], p[1100000], n, m, a[110], b[30], c[30]; n char cc; n bool cmp(int a, int b) { return a > b; } n void init() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) scanf( %d , &a[i]); n   sort(a + 1, a + 1 + n, cmp); n   scanf( %d n , &n); n   for (int i = (0); i <= (n - 1); ++i) { n     scanf( %c%d n , &cc, &b[i]); n     c[i] = (cc ==  p ); n   } n   m = (1 << n) - 1; n   p[0] = 0; n   for (int i = (1); i <= (m); ++i) p[i] = p[i >> 1] + (i & 1); n } n void work() { n   int x, y; n   f[m] = 0; n   for (int i = (m - 1); i >= (0); --i) { n     x = p[i]; n     f[i] = b[x] == 2 ? 100000000 : -100000000; n     for (int j = (1); j <= (n); ++j) { n       y = 1 << (j - 1); n       if (i & y) continue; n       if (b[x] == 1) { n         if (c[x]) n           f[i] = max(f[i], a[j] + f[i ^ y]); n         else n           f[i] = max(f[i], f[i ^ y]); n       } else { n         if (c[x]) n           f[i] = min(f[i], f[i ^ y] - a[j]); n         else n           f[i] = min(f[i], f[i ^ y]); n       } n     } n   } n   cout << f[0]; n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> arr(n); n   vector<pair<int, int>> sorted_arr(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     sorted_arr[i] = {arr[i], i}; n   } n   sort(sorted_arr.begin(), sorted_arr.end()); n   vector<int> pos(n); n   for (int i = 0; i < n; i++) { n     pos[i] = sorted_arr[i].second; n   } n   vector<int> next(n), prev(n); n   next[n - 1] = n; n   for (int i = n - 2; i >= 0; i--) { n     int j = i + 1; n     while (j < n && arr[i] > arr[j]) { n       j = next[j]; n     } n     next[i] = j; n   } n   prev[0] = -1; n   for (int i = 1; i < n; i++) { n     int j = i - 1; n     while (j >= 0 && arr[i] > arr[j]) { n       j = prev[j]; n     } n     prev[i] = j; n   } n   int ans = 0; n   for (int i = 1; i < n - 1; i++) { n     if (i - prev[i] < next[i] - i) { n       for (int j = prev[i] + 1; j < i; j++) { n         int k = pos[arr[i] - arr[j] - 1]; n         if (i < k && k < next[i]) ans++; n       } n     } else { n       for (int j = next[i] - 1; j > i; j--) { n         int k = pos[arr[i] - arr[j] - 1]; n         if (prev[i] < k && k < i) ans++; n       } n     } n   } n   cout << ans; n } n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   solve(); n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long s(long long x) { n   long long ans = 0; n   while (x) ans += x % 10, x /= 10; n   return ans; n } n long long solve(long long x) { return x * x + s(x) * x - n; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   long long sq = sqrt(n); n   int i; n   for (i = sq - 1000000; i <= sq + 1000000; i++) { n     if (i > 0) n       if (solve(i) == 0) { n         cout << i <<   n ; n         return 0; n       } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double y111, y222, yw, xb, yb, r; n double ASIN(double w) { n   if (w < -1.0) w = -1.0; n   if (w > 1.0) w = 1.0; n   return asin(w); n } n int main() { n   cin >> y111 >> y222 >> yw >> xb >> yb >> r; n   y111 += 1E-9; n   y222 -= 1E-9; n   double st = yw - r; n   double plo = 2 * st - y111, phi = 2 * st - y222; n   if (plo > phi) swap(plo, phi); n   double D = sqrt(((xb) * (xb)) + ((plo - yb) * (plo - yb))); n   double ang = ASIN(r / D); n   complex<double> dir = complex<double>(0, plo) - complex<double>(xb, yb); n   dir *= polar(1.0, -ang); n   double ty = yb + (fabs(xb / dir.real())) * dir.imag(); n   if (ty < phi - r + 1E-9) { n     double X = xb + ((st - yb) / dir.imag()) * dir.real(); n     printf( %.18lf n , X); n   } else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double sn = 1e-6; n int n, k; n char arr[55][10]; n string s[55]; n string res[55]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n - k + 1; i++) { n     scanf( %s , arr[i]); n     string st(arr[i]); n     s[i] = st; n   } n   string val =  Aa ; n   for (int i = 0; i < n - k + 1; i++) { n     if (s[i] ==  NO ) { n       if (res[i] !=   ) { n         res[i + k - 1] = res[i]; n         continue; n       } n       res[i] = val; n       res[i + k - 1] = val; n       val[1] = val[1] + 1; n       if (val[1] >  z ) { n         val[0] = val[0] + 1; n         val[1] =  a ; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (res[i] ==   ) { n       res[i] = val; n       val[1] = val[1] + 1; n       if (val[1] >  z ) { n         val[0] = val[0] + 1; n         val[1] =  a ; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     printf( %s , res[i].c_str()); n     if (i != n - 1) printf(   ); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x; n   cin >> x; n   int y; n   if (x.size() == 1) { n     y = x[0] -  0 ; n     if (!(y % 4)) n       cout << 4; n     else n       cout << 0; n     return 0; n   } n   y = x[x.size() - 1] -  0  + ((x[x.size() - 2] -  0 ) * 10); n   if (!(y % 4)) n     cout << 4; n   else n     cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int min(int a, int b) { n   if (a < b) n     return a; n   else n     return b; n } n int main() { n   int n, a, b, c, j; n   bool t = false; n   cin >> n; n   for (int i = 0; i < (4); ++i) { n     cin >> a >> b; n     if (a < 1 || b < 1) n       c = n + 1; n     else n       c = min(a, b); n     cin >> a >> b; n     if (a < 1 || b < 1) n       j = n + 1; n     else n       j = min(a, b); n     if (n >= c + j) { n       t = true; n       a = i + 1; n       break; n     } n   } n   if (t) n     cout << a <<     << c <<     << n - c; n   else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 102; n double dp[N][N][N][3]; n int vis[N][N][N][3]; n double call(int r, int s, int p, int id) { n   if (id == 0) { n     if (r == 0) return 0; n     if (s + p == 0) return 1; n   } else if (id == 1) { n     if (s == 0) return 0; n     if (r + p == 0) return 1; n   } else { n     if (p == 0) return 0; n     if (r + s == 0) return 1; n   } n   if (vis[r][s][p][id] == 1) return dp[r][s][p][id]; n   vis[r][s][p][id] = 1; n   double ret = 0; n   double total = ((r + s + p - 1) * (r + s + p)) / 2; n   double pr = ((r * (r - 1)) / 2) / total; n   double ps = ((s * (s - 1)) / 2) / total; n   double pp = ((p * (p - 1)) / 2) / total; n   double prs = (r * s) / total; n   double prp = (r * p) / total; n   double psp = (s * p) / total; n   double prob = pr + ps + pp; n   if (r >= 1 and s >= 1) ret = ret + prs * call(r, s - 1, p, id); n   if (r >= 1 and p >= 1) ret = ret + prp * call(r - 1, s, p, id); n   if (s >= 1 and p >= 1) ret = ret + psp * call(r, s, p - 1, id); n   return dp[r][s][p][id] = ret / (1 - prob); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   int r, s, p; n   cin >> r >> s >> p; n   cout << fixed << setprecision(9) << call(r, s, p, 0) <<     n        << call(r, s, p, 1) <<     << call(r, s, p, 2) <<   n ; n } n