#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n const int maxn = 5e5 + 5; n int n, k; n vector<ii> g[maxn]; n ll dp[maxn][2]; n void solve(int u, int par) { n   for (ii e : g[u]) { n     int v = e.first; n     ll w = e.second; n     if (v - par) { n       solve(v, u); n     } n   } n   ll tot0 = 0; n   vector<ll> pot; n   for (ii e : g[u]) { n     int v = e.first; n     ll w = e.second; n     if (v - par) { n       tot0 += dp[v][0]; n       pot.emplace_back(w + dp[v][1] - dp[v][0]); n     } n   } n   sort(pot.begin(), pot.end()); n   reverse(pot.begin(), pot.end()); n   dp[u][0] = dp[u][1] = tot0; n   for (int i = 0; i < k and i < pot.size() and pot[i] > 0; i++) { n     dp[u][0] += pot[i]; n   } n   for (int i = 0; i < k - 1 and i < pot.size() and pot[i] > 0; i++) { n     dp[u][1] += pot[i]; n   } n } n int main(int argc, char const *argv[]) { n   int tc; n   scanf( %d , &tc); n   while (tc--) { n     scanf( %d %d , &n, &k); n     for (int i = 1; i <= n; i++) { n       g[i].clear(); n     } n     for (int i = 1; i < n; i++) { n       int u, v, w; n       scanf( %d %d %d , &u, &v, &w); n       g[u].emplace_back(v, w); n       g[v].emplace_back(u, w); n     } n     solve(1, -1); n     printf( %lld n , dp[1][0]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[101] = { n       0, n   }; n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int t; n     scanf( %d , &t); n     for (int j = 0; j < t; j++) { n       int tmp; n       scanf( %d , &tmp); n       arr[tmp]++; n     } n   } n   for (int i = 1; i <= 100; i++) { n     if (arr[i] == n) printf( %d  , i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 200015; n const int nil = 100005; n int n; n set<pair<int, int> > S; n set<pair<int, int> > T; n int w[Maxn]; n vector<pair<int, int> > cand[Maxn]; n pair<int, int> res[Maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     S.insert(pair<int, int>(x, y)); n   } n   for (int i = 1; i <= n; i++) scanf( %d , &w[i]); n   for (set<pair<int, int> >::iterator it = S.begin(); it != S.end(); it++) { n     pair<int, int> p = *it; n     if (S.find(pair<int, int>(p.first + 1, p.second)) == S.end() && n         S.find(pair<int, int>(p.first, p.second + 1)) == S.end()) { n       cand[p.second - p.first + nil].push_back(p); n       T.insert(p); n     } n   } n   for (int i = n; i >= 1; i--) { n     int diag = w[i] + nil; n     if (cand[diag].empty()) { n       printf( NO n ); n       return 0; n     } n     pair<int, int> p = cand[diag].back(); n     cand[diag].pop_back(); n     res[i] = p; n     pair<int, int> u = pair<int, int>(p.first - 1, p.second); n     if (S.find(u) != S.end() && T.find(u) == T.end()) { n       cand[u.second - u.first + nil].push_back(u); n       T.insert(u); n     } n     u = pair<int, int>(p.first, p.second - 1); n     if (S.find(u) != S.end() && T.find(u) == T.end()) { n       cand[u.second - u.first + nil].push_back(u); n       T.insert(u); n     } n   } n   printf( YES n ); n   for (int i = 1; i <= n; i++) printf( %d %d n , res[i].first, res[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base ::sync_with_stdio(false); n   ; n   cin.tie(NULL); n   ; n   long long int t = 1; n   while (t--) { n     long long int n; n     cin >> n; n     pair<long long int, long long int> tree[n]; n     long long int i, j, k; n     map<pair<long long int, long long int>, long long int> mp; n     for (i = 0; i < n; i++) { n       cin >> j >> k; n       tree[i].first = j; n       tree[i].second = k; n     } n     list<long long int> l; n     for (i = 0; i < n; i++) { n       if (tree[i].first == 1) l.push_back(i); n     } n     vector<pair<long long int, long long int> > edges; n     while (!l.empty()) { n       long long int node = l.front(); n       l.pop_front(); n       if (tree[node].first == 0) continue; n       long long int xors = tree[node].second; n       tree[xors].first--; n       tree[xors].second ^= node; n       edges.push_back({node, xors}); n       if (tree[xors].first == 1) l.push_back(xors); n     } n     cout << edges.size() << endl; n     for (auto x : edges) cout << x.first <<     << x.second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-5; n const int p = 131; n const int inf = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const int N = 1e6 + 10; n long long Hash[N], fac[N]; n char s[N], ss[N]; n long long get(int l, int r) { n   return (Hash[r] - Hash[l - 1] * fac[r - l + 1] % mod + mod) % mod; n } n void run() { n   scanf( %s %s , s + 1, ss + 1); n   int len1 = strlen(s + 1); n   int len2 = strlen(ss + 1); n   fac[0] = 1; n   int n0 = 0, n1 = 0; n   for (int i = 1; i <= len1; i++) { n     n0 += (s[i] ==  0 ); n     n1 += (s[i] ==  1 ); n   } n   for (int i = 1; i <= len2; i++) { n     Hash[i] = (Hash[i - 1] * p % mod + ss[i] -  a  + 1) % mod; n     fac[i] = fac[i - 1] * p % mod; n   } n   int res = 0; n   for (int l0 = 1, l1; l0 < len2; l0++) { n     if (l0 * n0 >= len2) break; n     if ((len2 - l0 * n0) % n1) continue; n     l1 = (len2 - l0 * n0) / n1; n     long long hash0 = -1, hash1 = -1; n     int idx = 1; n     int flag = 1; n     for (int j = 1; j <= len1; j++) { n       if (s[j] ==  1 ) { n         if (hash1 == -1) { n           hash1 = get(idx, idx + l1 - 1); n         } else if (hash1 != get(idx, idx + l1 - 1)) { n           flag = 0; n           break; n         } n         idx += l1; n       } else { n         if (hash0 == -1) { n           hash0 = get(idx, idx + l0 - 1); n         } else if (hash0 != get(idx, idx + l0 - 1)) { n           flag = 0; n           break; n         } n         idx += l0; n       } n       if (~hash0 && ~hash1 && hash0 == hash1) { n         flag = 0; n         break; n       } n     } n     if (flag) res++; n   } n   printf( %d n , res); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   int fh = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= fh; n } n struct shj { n   int we, l, r, fh; n } t[100010 * 6]; n struct pte { n   int l0, r0, l1, r1; n } g[100010]; n struct teache { n   int l, r; n } v[100010]; n multiset<int> nl, nr; n int T1, T2, m, n, x, y, b1[100010], b2[100010], num, nt, anx = -1, any = -1, n                                                          no[100010]; n bool ans = 1, v1[100010]; n vector<int> e[100010]; n bool c1(const shj &a, const shj &b) { n   if (a.we != b.we) return a.we < b.we; n   return a.fh > b.fh; n } n void dfs(int no, int bi) { n   b1[no] = bi; n   b2[no] = num; n   v1[no] = 0; n   for (unsigned i = 0; i < e[no].size(); i++) { n     if (v1[e[no][i]]) n       dfs(e[no][i], bi ^ 1); n     else if (b1[no] == b1[e[no][i]]) n       ans = 0; n   } n } n int main() { n   read(T1); n   read(T2); n   read(n); n   read(m); n   for (int i = 1; i <= n; i++) { n     read(v[i].l); n     read(v[i].r); n   } n   for (int i = 1; i <= m; i++) { n     read(x); n     read(y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) v1[i] = 1; n   for (int i = 1; i <= n; i++) n     if (v1[i]) { n       num++; n       dfs(i, 0); n     } n   if (!ans) { n     puts( IMPOSSIBLE ); n     return 0; n   } n   for (int i = 1; i <= num; i++) { n     g[i].l0 = g[i].l1 = 0; n     g[i].r0 = g[i].r1 = 1e9; n   } n   for (int i = 1; i <= n; i++) { n     if (b1[i] == 0) { n       g[b2[i]].l0 = max(g[b2[i]].l0, v[i].l); n       g[b2[i]].r0 = min(g[b2[i]].r0, v[i].r); n     } else { n       g[b2[i]].l1 = max(g[b2[i]].l1, v[i].l); n       g[b2[i]].r1 = min(g[b2[i]].r1, v[i].r); n     } n   } n   for (int i = 1; i <= num; i++) n     if (g[i].l0 > g[i].r0 || g[i].l1 > g[i].r1) { n       puts( IMPOSSIBLE ); n       return 0; n     } n   for (int i = 1; i <= num; i++) { n     pte no = g[i]; n     if (no.l0 > no.l1) { n       swap(no.l0, no.l1); n       swap(no.r0, no.r1); n     } n     t[++nt] = (shj){no.l0, no.l1, no.r1, 1}; n     t[++nt] = (shj){no.l1, no.l1, no.r1, -1}; n     if (no.r0 >= no.r1) { n       t[++nt] = (shj){no.l1, no.l0, no.r0, 1}; n       t[++nt] = (shj){no.r1 + 1, no.l0, no.r0, -1}; n       t[++nt] = (shj){no.r1 + 1, no.l1, no.r1, 1}; n       t[++nt] = (shj){no.r0 + 1, no.l1, no.r1, -1}; n     } else { n       t[++nt] = (shj){no.l1, no.l0, no.r1, 1}; n       t[++nt] = (shj){no.r0 + 1, no.l0, no.r1, -1}; n       t[++nt] = (shj){no.r0 + 1, no.l0, no.r0, 1}; n       t[++nt] = (shj){no.r1 + 1, no.l0, no.r0, -1}; n     } n   } n   sort(t + 1, t + nt + 1, c1); n   for (int l = 1, r = 0; l <= nt; l = r + 1) { n     while (t[r + 1].we == t[l].we && r + 1 <= nt) { n       r++; n       if (t[r].fh == 1) { n         nl.insert(t[r].l); n         nr.insert(t[r].r); n       } else { n         auto i = nl.find(t[r].l); n         nl.erase(i); n         auto j = nr.find(t[r].r); n         nr.erase(j); n       } n     } n     if (nl.size() == num) { n       auto i = nl.end(); n       i--; n       int al = (*i), ar = (*(nr.begin())), sl = t[l].we, sr = t[r + 1].we - 1; n       if (al + sl > T2 || ar + sr < T1 || al > ar || sl > sr) continue; n       if (al + sl >= T1) { n         anx = sl; n         any = al; n       } else { n         if (ar + sr <= T2) { n           anx = sr; n           any = ar; n         } else { n           if (al + sr > T2) { n             anx = T1 - al; n             any = al; n           } else { n             anx = sr; n             any = T2 - sr; n           } n         } n       } n     } n   } n   if (anx == -1 && any == -1) { n     puts( IMPOSSIBLE ); n     return 0; n   } n   puts( POSSIBLE ); n   printf( %d %d n , anx, any); n   for (int i = 1; i <= num; i++) n     if (anx <= g[i].r0 && anx >= g[i].l0 && any <= g[i].r1 && any >= g[i].l1) n       no[i] = 0; n     else n       no[i] = 1; n   for (int i = 1; i <= n; i++) n     if (no[b2[i]] == b1[i]) n       putchar( 1 ); n     else n       putchar( 2 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int vis[maxn], son[maxn], f[maxn], sum, root, ans[maxn]; n vector<int> g[maxn]; n void getroot(int x, int fa) { n   son[x] = 1; n   f[x] = 0; n   for (int i = 0; i < g[x].size(); i++) { n     int v = g[x][i]; n     if (v == fa || vis[v]) continue; n     getroot(v, x); n     son[x] += son[v]; n     f[x] = max(f[x], son[v]); n   } n   f[x] = max(f[x], sum - son[x]); n   if (f[x] < f[root]) root = x; n } n void work(int x, int fa, int dep) { n   ans[x] = dep; n   vis[x] = 1; n   for (int i = 0; i < g[x].size(); i++) { n     int v = g[x][i]; n     if (vis[v]) continue; n     sum = son[v], root = 0; n     getroot(v, x); n     work(root, x, dep + 1); n   } n } n int main(void) { n   int n; n   while (~scanf( %d , &n) && n) { n     for (int i = 1; i <= n; i++) g[i].clear(); n     int a, b; n     for (int i = 0; i < n - 1; i++) { n       scanf( %d %d , &a, &b); n       g[a].push_back(b); n       g[b].push_back(a); n     } n     f[0] = sum = n; n     getroot(1, 0); n     work(root, 0, 0); n     for (int i = 1; i <= n; i++) { n       printf( %c  , ans[i] +  A ); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using vi = vector<int>; n struct ele { n   int value, jj, index; n }; n bool ordena(ele a, ele b) { return a.value < b.value; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int k, n; n   cin >> k; n   vector<ele> arr; n   for (int i = 0; i < k; i++) { n     int v[200010], sum = 0; n     cin >> n; n     for (int j = 0; j < n; j++) { n       cin >> v[j]; n       sum += v[j]; n     } n     for (int j = 0; j < n; j++) { n       ele myele; n       myele.value = sum - v[j]; n       myele.jj = j; n       myele.index = i; n       arr.push_back(myele); n     } n   } n   sort(arr.begin(), arr.end(), ordena); n   for (int i = 0; i < arr.size() - 1; i++) { n     if (arr[i].value == arr[i + 1].value && arr[i].index != arr[i + 1].index) { n       cout <<  YES  <<   n ; n       cout << arr[i].index + 1 <<     << arr[i].jj + 1 <<   n ; n       cout << arr[i + 1].index + 1 <<     << arr[i + 1].jj + 1 <<   n ; n       return 0; n     } n   } n   cout <<  NO  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, c = 0, n, s = 0; n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     s += (5 * i); n     if (s + k <= 240) c++; n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   int n, a, b; n   cin >> n >> a >> b; n   string s; n   cin >> s; n   vector<int> dp(n + 1, INT_MAX); n   vector<vector<int> > lcp(n, vector<int>(n)); n   for (int i = n - 1; i >= 0; --i) { n     for (int j = n - 1; j >= 0; --j) { n       if (s[i] == s[j]) { n         if (i == n - 1 || j == n - 1) { n           lcp[i][j] = 1; n         } else { n           lcp[i][j] = 1 + lcp[i + 1][j + 1]; n         } n       } n     } n   } n   dp[0] = 0; n   for (int i = 0; i < n; ++i) { n     dp[i + 1] = min(dp[i + 1], dp[i] + a); n     for (int j = 0; j < i; ++j) { n       int k = min(lcp[i][j], i - j); n       dp[i + k] = min(dp[i + k], dp[i] + b); n     } n   } n   cout << dp[n]; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[100001]; n int pi[100001]; n int level[100001]; n list<int> adj[100001]; n void dfs(int s) { n   vis[s] = 1; n   bool flag = 1; n   for (list<int>::iterator it = adj[s].begin(); it != adj[s].end(); ++it) n     if (!vis[*it]) { n       level[*it] = level[s] + 1; n       pi[*it] = s; n       dfs(*it); n       flag = 0; n     } n   if (flag) { n     int least = s; n     stack<int> stk; n     for (list<int>::iterator it = adj[s].begin(); it != adj[s].end(); ++it) n       if (level[least] > level[(*it)]) least = *it; n     while (s != least) { n       stk.push(s); n       s = pi[s]; n     } n     stk.push(s); n     cout << stk.size() <<   n ; n     while (!stk.empty()) { n       cout << stk.top() <<    ; n       stk.pop(); n     } n     exit(0); n   } n } n int main() { n   int n, m, k, i, l, r; n   cin >> n >> m >> k; n   for (i = 1; i <= m; ++i) { n     cin >> l >> r; n     adj[l].push_back(r); n     adj[r].push_back(l); n   } n   pi[1] = -1; n   dfs(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename tn> n void read(tn &a) { n   tn x = 0, f = 1; n   char c =    ; n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   a = x * f; n } n const int N = 101000; n long long lim[8], lm[8]; n int n; n struct point { n   long long x, y, z; n   long long getval(int k) { n     return x * (k & 1 ? -1 : 1) + y * (k >> 1 & 1 ? -1 : 1) + n            z * (k >> 2 & 1 ? -1 : 1); n   } n } p[N], ans; n bool chk(int tag, long long *lim) { n   long long l[4] = {(-lim[7] + 1 - 3 * tag) >> 1, (-lim[6] + 1 - tag) >> 1, n                     (-lim[5] + 1 - tag) >> 1, (-lim[3] + 1 - tag) >> 1}; n   long long r[4] = {(lim[0] - 3 * tag) >> 1, (lim[1] - tag) >> 1, n                     (lim[2] - tag) >> 1, (lim[4] - tag) >> 1}; n   long long a = l[1], b = l[2], c = l[3]; n   for (int i = 0; i < 4; i++) n     if (r[i] < l[i]) return 0; n   if (a + b + c > r[0]) return 0; n   if (a + b + c < l[0]) { n     a = min(r[1], l[0] - b - c); n     b = min(r[2], l[0] - a - c); n     c = min(r[3], l[0] - a - b); n   } n   if (a + b + c < l[0]) return 0; n   ans = {b + c + tag, a + c + tag, a + b + tag}; n   return 1; n } n bool check(long long len) { n   memcpy(lm, lim, sizeof(lm)); n   for (int i = 0; i < 8; i++) lm[i] += len; n   if (chk(0, lm)) return 1; n   if (chk(1, lm)) return 1; n   return 0; n } n int main() { n   int T; n   read(T); n   while (T--) { n     read(n); n     for (int i = 1; i <= n; i++) read(p[i].x), read(p[i].y), read(p[i].z); n     memset(lim, 63, sizeof(lim)); n     for (int i = 1; i <= n; i++) n       for (int j = 0; j < 8; j++) lim[j] = min(lim[j], p[i].getval(j)); n     long long l = -1, r = 4e18l + 1; n     while (l + 1 < r) { n       long long mid = l + r >> 1; n       if (check(mid)) n         r = mid; n       else n         l = mid; n     } n     check(r); n     cout << ans.x <<     << ans.y <<     << ans.z <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 7; n long long arr[maxn]; n int vis[maxn]; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n; n     if (n == 1) { n       printf( YES n ); n       continue; n     } n     scanf( %d , &n); n     n *= 4; n     for (int i = 1; i <= n; i++) { n       scanf( %lld , &arr[i]); n     } n     sort(arr + 1, arr + n + 1); n     long long aver; n     int mark = 1; n     for (int i = 1, j = n - 1; i <= j; i += 2, j -= 2) { n       if (arr[i] != arr[i + 1] || arr[j] != arr[j + 1]) { n         mark = 0; n         break; n       } n       if (i == 1) { n         aver = arr[i] * arr[j]; n       } else if (arr[i] * arr[j] != aver) { n         mark = 0; n         break; n       } n     } n     if (mark) { n       printf( YES n ); n     } else { n       printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, cnt = 0, sum = 1; n   vector<int> v; n   cin >> n >> k; n   if (k == 1) { n     cout << n << endl; n     return 0; n   } n   for (int i = 2; i <= n / 2; i++) { n     while (n % i == 0 && cnt < k - 1) { n       cnt++; n       n /= i; n       v.push_back(i); n     } n     if (cnt == k - 1 && n > 1) { n       v.push_back(n); n       cnt++; n       break; n     } n   } n   if (cnt == k) { n     for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n     cout << endl; n   } else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n long long dp[N][2]; n struct segment { n   int l, r; n }; n auto cmp = [](segment& s1, segment& s2) { n   if (s1.r != s2.r) n     return s1.r < s2.r; n   else n     return s1.l > s2.l; n }; n void solve() { n   int n, m; n   cin >> n >> m; n   long long p[n + 2]; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   p[0] = -1e10; n   p[n + 1] = 1e10; n   sort(p, p + n + 2); n   vector<segment> s; n   for (int i = 1; i <= m; i++) { n     int l, r; n     cin >> l >> r; n     if (lower_bound(p, p + n + 2, l) != upper_bound(p, p + n + 2, r)) continue; n     s.push_back((segment){l, r}); n   } n   sort(s.begin(), s.end(), cmp); n   for (int i = 1; i < s.size(); i++) { n     if (s[i - 1].l >= s[i].l || s[i - 1].r == s[i].r) n       s.erase(s.begin() + i), i--; n   } n   long long dp[n + 2][2]; n   for (int i = 1; i <= n + 1; i++) dp[i][0] = dp[i][1] = 1e15; n   dp[0][0] = dp[0][1] = 0; n   int now = 0; n   for (int i = 1; i < n + 2; i++) { n     vector<long long> l, r; n     l.push_back(p[i - 1]); n     while (now < s.size() && s[now].r < p[i]) { n       l.push_back(s[now].l); n       r.push_back(s[now].r); n       now++; n     } n     r.push_back(p[i]); n     for (int j = 0; j < l.size(); j++) { n       dp[i][0] = n           min(dp[i][0], dp[i - 1][0] + (l[j] - p[i - 1]) + 2 * (p[i] - r[j])); n       dp[i][1] = n           min(dp[i][1], dp[i - 1][0] + (l[j] - p[i - 1]) + (p[i] - r[j])); n       dp[i][0] = min(dp[i][0], n                      dp[i - 1][1] + 2 * (l[j] - p[i - 1]) + 2 * (p[i] - r[j])); n       dp[i][1] = n           min(dp[i][1], dp[i - 1][1] + 2 * (l[j] - p[i - 1]) + (p[i] - r[j])); n     } n   } n   cout << min(dp[n + 1][0], dp[n + 1][1]) << endl; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n inline long long int rand(long long int x, long long int y) { n   ++y; n   return (rng() % (y - x)) + x; n } n string to_string(char c) { n   string second(1, c); n   return second; n } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void degug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void degug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   degug_out(T...); n } n long long int n, fac[(300006)], dp[(300006)]; n const long long int MOD = 998244353; n vector<long long int> v[(300006)]; n void fact() { n   fac[0] = fac[1] = 1; n   for (long long int i = 2; i <= n; ++i) { n     fac[i] = (i * fac[i - 1]) % MOD; n   } n   return; n } n void dfs(long long int x, long long int p) { n   long long int ans = 1; n   long long int child = 1; n   for (auto i : v[x]) n     if (i != p) { n       child++; n       dfs(i, x); n       ans *= dp[i]; n       ans %= MOD; n     } n   ans *= fac[child - 1]; n   ans %= MOD; n   if (x != 1) ans *= child; n   ans %= MOD; n   return void(dp[x] = ans); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   fact(); n   for (long long int i = 0; i < (long long int)n - 1; ++i) { n     long long int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   dfs(1, 1); n   cout << (dp[1] * n) % MOD; n } n


#include <bits/stdc++.h> n const int maxn = 2e5 + 10; n int N; n int A[maxn]; n long long Q[maxn]; n long long sum1[maxn]; n long long sum2[maxn]; n double X(int i) { return i; } n double Y(int i) { return i * sum1[i] - sum2[i]; }; n double slope(int i, int j) { return (Y(i) - Y(j)) / (X(i) - X(j)); } n long long Calc(int i, int j) { n   return sum2[j] - sum2[i] - 1ll * i * (sum1[j] - sum1[i]); n } n int main() { n   scanf( %d , &N); n   for (register int i = 1; i <= N; i++) { n     scanf( %d , &A[i]); n     sum1[i] = sum1[i - 1] + A[i]; n     sum2[i] = sum2[i - 1] + 1ll * A[i] * i; n   } n   int tail = 0; n   long long Ans = 0; n   for (register int j = 1; j <= N; j++) { n     long long k = sum1[j]; n     int l = 0, r = tail; n     while (l < r) { n       int mid = l + r >> 1; n       if (slope(Q[mid], Q[mid + 1]) > k) n         l = mid + 1; n       else n         r = mid; n     } n     int i = Q[r]; n     Ans = std::max(Ans, Calc(i, j)); n     while (tail >= 1 && slope(Q[tail], Q[tail - 1]) < slope(Q[tail], j)) tail--; n     Q[++tail] = j; n   } n   printf( %I64d n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, res; n int check(int a, int b) { return a < b; } n int main() { n   scanf( %d%d , &a, &b); n   if (a == b) { n     printf( infinity ); n     return 0; n   } n   res = 0; n   a -= b; n   if (a < 0) { n     printf( 0 ); n     return 0; n   } n   for (int i = 1; i * i <= a; i++) n     if (!(a % i)) { n       res += check(b, i); n       if (i * i != a) res += check(b, a / i); n     } n   printf( %d , res); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, c, l, r, cp, cm; n string s; n int main() { n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     l = i - 1; n     r = i; n     cp = 1; n     cm = 1; n     while (l >= 0 && s[l] !=  L ) { n       cp += (s[l] ==  R ); n       l--; n     } n     while (r < n - 1 && s[r] !=  R ) { n       cm += (s[r] ==  L ); n       r++; n     } n     cout << max(cp, cm) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node; n struct node { n   node* arr[26]; n   int val; n   node(); n   void insert(char ch[], int l, int pos); n   void win(); n   void lose(); n }; n node::node() { n   val = 0; n   for (int i = 0; i < 26; i++) arr[i] = 0; n } n void node::insert(char ch[], int l, int pos) { n   if (pos == l) n     return; n   else { n     int index = ch[pos] -  a ; n     if (arr[index] == 0) { n       arr[index] = new node(); n       arr[index]->insert(ch, l, pos + 1); n     } else { n       arr[index]->insert(ch, l, pos + 1); n     } n   } n } n void node::win() { n   int ind = 0; n   for (int i = 0; i < 26; i++) { n     if (arr[i] != 0) { n       arr[i]->win(); n       ind = ind | arr[i]->val; n     } n   } n   val = !ind; n } n void node::lose() { n   int ind = 1; n   int leaf = 1; n   for (int i = 0; i < 26; i++) { n     if (arr[i] != 0) { n       leaf = 0; n       arr[i]->lose(); n       ind = ind & arr[i]->val; n     } n   } n   if (leaf) n     val = leaf; n   else n     val = !ind; n } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   node* root = new node(); n   char ch[100000]; n   for (int i = 0; i < n; i++) { n     scanf( %s , ch); n     int l = strlen(ch); n     root->insert(ch, l, 0); n   } n   int ind = 0; n   for (int i = 0; i < 26; i++) { n     if (root->arr[i] != 0) { n       root->arr[i]->win(); n       ind = ind | root->arr[i]->val; n     } n   } n   int w = ind; n   ind = 0; n   for (int i = 0; i < 26; i++) { n     if (root->arr[i] != 0) { n       root->arr[i]->lose(); n       ind = ind | !root->arr[i]->val; n     } n   } n   int l = ind; n   if (w & l) { n     printf( First n ); n   } else if (w & !l) { n     if (k & 1) n       printf( First n ); n     else n       printf( Second n ); n   } else n     printf( Second n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a, b; n   cin >> a >> b; n   int n; n   cin >> n; n   n = n % 4; n   if (n == 0) { n     cout <<  undefined ; n     return 0; n   } n   int arr[] = {118, 60, 94, 62}; n   int arr2[] = {118, 62, 94, 60}; n   int i = 0; n   for (i = 0; i < 4; i++) { n     if (a == arr[i]) break; n   } n   char c = arr[i]; n   for (int j = 1; j <= n; j++) { n     i = (i + 1) % 4; n     c = arr[i]; n   } n   int k = 0; n   for (k = 0; k < 4; k++) { n     if (a == arr2[k]) break; n   } n   char d = arr[k]; n   for (int j = 1; j <= n; j++) { n     k = (k + 1) % 4; n     d = arr2[k]; n   } n   if (c == b && d == b) n     cout <<  undefined ; n   else if (c == b) n     cout <<  cw ; n   else n     cout <<  ccw ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const long long INFL = 1e18 + 123; n const double PI = atan2(0, -1); n mt19937 tw(960172); n long long rnd(long long x, long long y) { n   static uniform_int_distribution<long long> d; n   return d(tw) % (y - x + 1) + x; n } n int cross(int x1, int y1, int x2, int y2) { return x1 * y2 - y1 * x2; } n bool inside(int x, int y, vector<pair<int, int> > poly) { n   int s1 = 0, s2 = 0; n   for (int i = 0; i < ((int)(poly).size()); ++i) { n     int next = (i + 1) % ((int)(poly).size()); n     int tmp = cross(poly[i].first - x, poly[i].second - y, poly[next].first - x, n                     poly[next].second - y); n     s1 += tmp; n     s2 += abs(tmp); n   } n   return abs(s1) == s2; n } n void solve() { n   vector<pair<int, int> > poly[2]; n   for (int i = 0; i < 2; ++i) { n     for (int j = 0; j < 4; ++j) { n       int x, y; n       cin >> x >> y; n       poly[i].push_back({x, y}); n     } n   } n   for (int x = -150; x <= 150; ++x) { n     for (int y = -150; y <= 150; ++y) { n       if (inside(x, y, poly[0]) && inside(x, y, poly[1])) { n         cout <<  YES n ; n         return; n       } n     } n   } n   cout <<  NO n ; n } n int main() { n   cerr << fixed << setprecision(15); n   cout << fixed << setprecision(15); n   ios::sync_with_stdio(false); n   int tests = 1; n   for (int it = 1; it <= tests; ++it) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n, i, k = 0; n int main() { n   cin >> s; n   for (i = 0; i < s.size(); i++) { n     while (s[i] == s[i + 1] && s.size()) { n       s.erase(i, 2); n       i--; n       k++; n     } n   } n   if (k % 2 != 0) n     cout <<  YES  << endl; n   else if (k % 2 == 0) n     cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 100; n vector<int> ans; n set<pair<int, int>> add[MAX], rem[MAX], cur; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     add[x].insert({y, i}); n     rem[y + 1].insert({y, i}); n   } n   for (int i = 1; i < MAX; i++) { n     for (auto p : rem[i]) { n       cur.erase(p); n     } n     for (auto p : add[i]) { n       cur.insert(p); n     } n     while (cur.size() > k) { n       set<pair<int, int>>::iterator it; n       it = prev(cur.end()); n       ans.push_back((*it).second); n       cur.erase(it); n     } n   } n   cout << ans.size() << endl; n   for (int x : ans) { n     cout << x <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n], b[n], min, count = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       b[i] = a[i]; n     } n     sort(b, b + n); n     min = b[0]; n     for (int i = 0; i < n; i++) { n       if (a[i] % min == 0 || a[i] == b[i]) { n         count++; n       } else n         break; n     } n     if (count == n) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long n; n   cin >> n; n   long long ans = 0; n   for (long long i = 1; i < n; i++) { n     if (n % i == 0) { n       ans++; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n vector<pair<int, int> > v; n struct Node { n   Node *left, *right; n   int val; n   int lazy; n   Node() { n     left = right = NULL; n     val = 0; n     lazy = 0; n   } n }; n inline void refresh(Node*& root, int x, int y) { n   if (root->lazy != 0 && root->val != (y - x + 1)) { n     root->val = 1LL * (y - x + 1) * root->lazy; n     if (x == y) { n       root->lazy = 0; n       return; n     } n     if (root->left == NULL) { n       root->left = new Node(); n     } n     if (root->right == NULL) { n       root->right = new Node(); n     } n     root->left->lazy = root->lazy; n     root->right->lazy = root->lazy; n     root->lazy = 0; n   } n } n inline void update(Node*& root, int x, int y, int b, int e, int val) { n   refresh(root, x, y); n   if (x > e || y < b || (root->val == y - x + 1)) { n     return; n   } n   int mid = (x + y) / 2; n   if (x >= b && y <= e) { n     root->lazy = val; n     refresh(root, x, y); n     return; n   } n   if (root->left == NULL) { n     root->left = new Node(); n   } n   if (root->right == NULL) { n     root->right = new Node(); n   } n   update(root->left, x, mid, b, e, val); n   update(root->right, mid + 1, y, b, e, val); n   root->val = root->left->val + root->right->val; n } n int query(Node*& root, int x, int y, int b, int e) { n   if (root == NULL || x > e || y < b) { n     return 0; n   } n   refresh(root, x, y); n   if (x >= b && y <= e) { n     return root->val; n   } n   int mid = (x + y) / 2; n   int L = query(root->left, x, mid, b, e); n   int R = query(root->right, mid + 1, y, b, e); n   return L + R; n } n vector<pair<int, int> > Q[N]; n int ans[N]; n int main() { n   Node* root = new Node(); n   int n; n   scanf( %d , &n); n   ; n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d , &x); n     ; n     scanf( %d , &y); n     ; n     v.push_back(make_pair(x, y)); n   } n   int q; n   scanf( %d , &q); n   ; n   for (int i = 0; i < q; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     Q[x].push_back(make_pair(y, i)); n   } n   for (int i = v.size() - 1; i >= 0; i--) { n     update(root, 1, 1000000007, v[i].first, n            min(v[i].first + v[i].second - 1, 1000000007), 1); n     for (auto x : Q[i + 1]) { n       int a = i; n       int b = x.first; n       int len = v[b - 1].first - v[a].first; n       ans[x.second] = n           len - query(root, 1, 1000000007, v[a].first, v[b - 1].first - 1); n     } n   } n   int y; n   for (int i = 0; i < q; i++) { n     printf( %d n , ans[i]); n   } n } n


/* n     Washief Hossain Mugdho n     20 December 2020 n     Codeforces 691F n */ n  n #include <bits/stdc++.h> n #define ull unsigned long long n #define ll long long n #define pb push_back n #define mp make_pair n #define fr first n #define sc second n #define fastio ios_base::sync_with_stdio(0) n #define untie cin.tie(0) n #define ms(a, b) memset(a, b, sizeof a) n using namespace std; n  n void run_tests() n { n     string s; n     cin >> s; n     vector<int> bam(2 * s.size() + 1, 0); n     vector<int> dan(2 * s.size() + 1, 0); n     int pos = s.size(); n     for (auto c : s) n     { n         if (c ==  0 ) n             dan[pos++]++; n         else n             bam[pos--]++; n     } n     pos = s.size(); n     while (bam[pos] + dan[pos]) n     { n         if (dan[pos] && (!bam[pos] || bam[pos + 1])) n         { n             dan[pos++]--; n             cout << 0; n         } n         else n         { n             bam[pos--]--; n             cout << 1; n         } n     } n     cout << endl; n } n  n int main() n { n     fastio; n     untie; n #ifdef LOCAL_OUTPUT n     freopen(LOCAL_OUTPUT,  w , stdout); n #endif n #ifdef LOCAL_INPUT n     freopen(LOCAL_INPUT,  r , stdin); n #endif n  n     int tc; n     cin >> tc; n     while (tc--) n         run_tests(); n } n


#include <bits/stdc++.h> n using namespace std; n long long cnt[19], val[19]; n void init() { n   cnt[1] = 9, cnt[2] = 9, cnt[3] = 90; n   val[1] = 1, val[2] = 10, val[3] = 100; n   long long t = 100; n   for (int i = 4; i <= 19; i++) n     cnt[i] = t * 9, t *= 10, val[i] = val[i - 1] * 10; n } n long long cal(long long a) { n   long long bit = 0, tmp, p = a; n   while (p) { n     tmp = p; n     p /= 10; n     bit++; n   } n   long long ans = 0; n   for (int i = 1; i <= bit - 1; i++) { n     ans += cnt[i]; n   } n   if (bit > 2) n     ans += cnt[bit] / 9 * (tmp - 1); n   else n     ans += tmp - 1; n   long long maxn; n   long long minn = tmp; n   for (int i = 1; i < bit; i++) { n     minn *= 10; n   } n   minn += tmp; n   maxn = a; n   int mowei = a % 10; n   if (mowei >= tmp) n     maxn = maxn - mowei + tmp; n   else if (maxn - 10 >= tmp * val[bit]) n     maxn = maxn - 10 - mowei + tmp; n   else n     return ans; n   maxn = (maxn - tmp * val[bit]) / 10; n   return maxn + 1 + ans; n } n int main() { n   long long l, r; n   while (~scanf( %I64d%I64d , &l, &r)) { n     init(); n     long long a = cal(l), b = cal(r); n     if (l < 10) a = l; n     if (r < 10) b = r; n     long long ans = b - a; n     long long tmp1 = l % 10, tmp2; n     while (l) { n       tmp2 = l; n       l /= 10; n     } n     if (tmp1 == tmp2) ans++; n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Display(vector<double> row) { n   for (int i = 0; i < row.size(); i++) cout << row[i] <<    ; n   cout << endl; n } n void Display(vector<vector<double> > matrix) { n   for (int i = 0; i < matrix.size(); i++) Display(matrix[i]); n   cout << endl; n } n vector<vector<double> > New(int n, int m, double x = 0.0) { n   return vector<vector<double> >(n, vector<double>(m, x)); n } n vector<vector<double> > Ident(int n) { n   vector<vector<double> > Res = New(n, n); n   for (int i = 0; i < n; i++) Res[i][i] = 1.0; n   return Res; n } n void Multy(vector<vector<double> >& C, vector<vector<double> > A, n            vector<vector<double> > B) { n   assert(A[0].size() == B.size()); n   assert(A.size() == C.size()); n   assert(B[0].size() == C[0].size()); n   for (int i = 0; i < C.size(); i++) n     for (int j = 0; j < C[0].size(); j++) C[i][j] = 0.0; n   for (int i = 0; i < C.size(); i++) n     for (int j = 0; j < C[0].size(); j++) n       for (int k = 0; k < A[0].size(); k++) C[i][j] += A[i][k] * B[k][j]; n } n const int N = 100100; n const int M = 4 * N; n const double Eps = 1e-6; n int n; n int a[N]; n int b[N]; n vector<vector<double> > Tree[M]; n bool isZero(double x) { return abs(x) < Eps; } n void Merge(int Node) { n   Multy(Tree[Node], Tree[(Node << 1)], Tree[(Node << 1 | 1)]); n } n void Build(int Node = 1, int L = 1, int R = n) { n   Tree[Node] = New(2, 2); n   if (L == R) { n     int i = L; n     Tree[Node][0][0] = 0.0; n     Tree[Node][0][1] = 1.0 * (a[i] - b[i]) / a[i]; n     Tree[Node][1][0] = 1.0; n     Tree[Node][1][1] = 1.0 * b[i] / a[i]; n     return; n   } n   Build((Node << 1), L, ((L + R) >> 1)); n   Build((Node << 1 | 1), ((L + R) >> 1) + 1, R); n   Merge(Node); n } n void Update(int i, int Node = 1, int L = 1, int R = n) { n   if (L == R) { n     Tree[Node][0][0] = 0.0; n     Tree[Node][0][1] = 1.0 * (a[i] - b[i]) / a[i]; n     Tree[Node][1][0] = 1.0; n     Tree[Node][1][1] = 1.0 * b[i] / a[i]; n     return; n   } n   if (i <= ((L + R) >> 1)) n     Update(i, (Node << 1), L, ((L + R) >> 1)); n   else n     Update(i, (Node << 1 | 1), ((L + R) >> 1) + 1, R); n   Merge(Node); n } n void Query(vector<vector<double> >& Res, int i, int j, int Node = 1, int L = 1, n            int R = n) { n   if (i <= L && R <= j) return Multy(Res, Res, Tree[Node]); n   if (j < L || R < i) return; n   Query(Res, i, j, (Node << 1), L, ((L + R) >> 1)); n   Query(Res, i, j, (Node << 1 | 1), ((L + R) >> 1) + 1, R); n } n int main() { n   int q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) scanf( %d%d , a + i, b + i); n   Build(); n   while (q--) { n     int T; n     scanf( %d , &T); n     if (T == 1) { n       int i, x, y; n       scanf( %d%d%d , &i, &x, &y); n       a[i] = x; n       b[i] = y; n       Update(i); n     } else { n       int L, R; n       scanf( %d%d , &L, &R); n       vector<vector<double> > Res = Ident(2); n       Query(Res, L, R); n       double Ans = 1.0 / Res[1][1]; n       if (Ans != Ans) Ans = 0.0; n       printf( %.10f n , Ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, a[500050], R[500050]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     if (i <= n - i + 1) R[i] = n - i + 1, R[n - i + 1] = i; n   } n   int cnt = 0; n   for (int i = 1; i <= n - i + 1; i++) { n     cnt++; n     if (cnt & 1) swap(a[i], a[R[i]]); n   } n   for (int i = 1; i <= n; i++) printf( %d  , a[i]); n } n


#include <bits/stdc++.h> n using namespace std; n string s; n void rev() { n   for (int i = 0; i < s.size(); i++) n     if (s[i] ==  L ) n       s[i] =  R ; n     else n       s[i] =  L ; n } n vector<int> v; n int k[2000011]; n const int delta = 1000005; n bool verify(int nr) { n   memset(k, 0, sizeof(k)); n   int now = 0; n   k[now + delta]++; n   for (int i = 0; i < v.size(); i++) { n     now = min(nr - 1, now + v[i]); n     k[now + delta]++; n   } n   if (k[now + delta] > 1) return false; n   return true; n } n int i, sum, mx, l, r, mid, ans; n int main(void) { n   cin >> s; n   if (s[s.size() - 1] ==  R ) rev(); n   for (i = 0; i < s.size(); i++) { n     if (s[i] ==  R ) n       v.push_back(1); n     else n       v.push_back(-1); n     sum += v[i]; n     mx = max(mx, sum); n   } n   if (verify(mx + 2)) { n     cout << 1; n     return 0; n   } n   l = 1; n   r = mx; n   while (l <= r) { n     mid = (l + r) / 2; n     if (verify(mid) == true) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 2; n long long n, a[maxn], k, ans[maxn]; n long long Check(long long m) { n   long long res = 0, l, r, mid; n   for (int i = 1; i <= n; i++) { n     l = 1; n     r = a[i]; n     while (l <= r) { n       mid = (l + r) / 2; n       if (a[i] - 3 * mid * (mid + 1) - 1 >= m) n         l = mid + 1; n       else n         r = mid - 1; n     } n     ans[i] = r; n     res += r; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   long long l = -4e18, r = 4e18, mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (Check(mid) >= k) n       l = mid + 1; n     else n       r = mid - 1; n   } n   long long sum = Check(l); n   for (int i = 1; i <= n; i++) n     if (sum < k && ans[i] < a[i] && n         a[i] - 3 * (ans[i] + 1) * (ans[i] + 2) - 1 == l - 1) { n       sum++; n       ans[i]++; n     } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int k; n bool debug = false; n int f[100005]; n int ff(int x) { n   while (x != f[x]) x = f[x]; n   return x; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) f[i] = i; n   int a, b; n   long long ans = 1; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     int fa = ff(a), fb = ff(b); n     if (fa == fb) { n       ans = ans * 2 % 1000000009; n     } else { n       f[fa] = fb; n     } n     printf( %I64d n , ans - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nn, n, b, q; n vector<pair<int, int> > d; n int main() { n   d.push_back(make_pair(0, 0)); n   scanf( %d%d%d , &n, &b, &q), nn = n; n   d.push_back(make_pair(b, n)); n   for (int i = 1; i <= q; i++) n     scanf( %d%d , &b, &n), d.push_back(make_pair(b, n)); n   sort(d.begin(), d.end()); n   for (int i = 1; i <= q; i++) n     if (d[i].second > d[i + 1].second || n         d[i + 1].second - d[i].second > d[i + 1].first - d[i].first) n       return printf( unfair n ), 0; n   for (int k = 0; k < 32; k++) { n     int res = 0; n     for (int i = 1; i <= q + 1; i++) { n       int cnt = 0; n       for (int j = d[i - 1].first + 1; j <= d[i].first; j++) n         cnt += bool((1 << (j % 5)) & k); n       res += min(cnt, d[i].second - d[i - 1].second); n     } n     if (res < nn / 5 * __builtin_popcount(k)) return printf( unfair n ), 0; n   } n   printf( fair n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 0; n const int LOGN = 0; n const int MOD = 1e9 + 7; n const int INF = 1e9 + 10; n long long N, Q; n long long x; n long long f(long long x) { n   if (x & 1) return (x + 1) >> 1; n   long long id = N - (x >> 1); n   while (!(id & 1)) id >>= 1; n   id >>= 1; n   return N - id; n } n void read() { n   cin >> N >> Q; n   while (Q--) { n     cin >> x; n     cout << f(x) <<   n ; n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   read(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long GCD(long long a, long long b) { n   if (b) return GCD(b, a % b); n   return a; n } n bool u1[10]; n bool u2[10]; n int main() { n   long long n, m, v; n   vector<long long> a, b; n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (long long i = 0; i < n; i++) { n     cin >> v; n     a.push_back(v); n     u1[v] = true; n   } n   for (long long i = 0; i < m; i++) { n     cin >> v; n     b.push_back(v); n     u2[v] = true; n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   for (long long i = 1; i <= 9; i++) { n     if (u1[i] && u2[i]) { n       cout << i; n       return 0; n     } n   } n   if (a[0] < b[0]) { n     cout << a[0] << b[0]; n   } else n     cout << b[0] << a[0]; n } n


#include <bits/stdc++.h> n using namespace std; n  n /*<DEBUG>*/ n #define tem template <typename  n #define can_shift(_X_, ...) enable_if_t<sizeof test<_X_>(0) __VA_ARGS__ 8, debug&> operator<<(T i) n #define _op debug& operator<< n tem C > auto test(C *x) -> decltype(cerr << *x, 0LL); n tem C > char test(...); n tem C > struct itr{C begin, end; }; n tem C > itr<C> get_range(C b, C e) { return itr<C>{b, e}; } n struct debug{ n #ifdef _LOCAL n  ~debug(){ cerr << endl; } n  tem T > can_shift(T, ==){ cerr << boolalpha << i; return *this; } n  tem T> can_shift(T, !=){ return *this << get_range(begin(i), end(i)); } n  tem T, typename U > _op (pair<T, U> i){  n   return *this <<  <   << i.first <<   ,   << i.second <<   > ; } n  tem T> _op (itr<T> i){ n   *this <<   {  ; n   for(auto it = i.begin; it != i.end; it++){ n    *this <<   ,   + (it==i.begin?2:0) << *it; n   } n   return *this <<   } ; n  } n #else n tem T> _op (const T&) { return *this; } n #endif  n }; n  n string _ARR_(int* arr, int sz){ n  string ret =  {   + to_string(arr[0]);  n  for(int i = 1; i < sz; i++) ret +=   ,   +  to_string(arr[i]); n  ret +=   } ; return ret; n } n  n #define exp(...)   [   << #__VA_ARGS__ <<   :   << (__VA_ARGS__) <<   ]  n /*</DEBUG>*/ n  n typedef long long ll; n typedef unsigned long long ull; n typedef unsigned int uint; n typedef pair<int, int> pii; n //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n #define pb push_back n #define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) n #define TC int __TC__; cin >> __TC__; while(__TC__--) n #define ar array n  n const int INF = 1e9 + 7; n  n int main(void) n { n  FAST; n  vector<ll> mx; n  for(ll i = 1; i*i + (i+1)*(i+1) <= INF; ++i){ n   mx.pb(i*i + (i+1) * (i+1)); n  } n  n  TC{ n   int n; cin >> n; n   int pos = (lower_bound(mx.begin(), mx.end(), n) - mx.begin()); n   if(mx[pos] == n) ++pos; n   cout << pos <<   n ; n  } n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long n, k; n   cin >> n >> k; n   vector<long long> occ(255); n   string s; n   cin >> s; n   long long streak = 0; n   char curr = s[0]; n   for (long long i = 0; i < n; i++) { n     if (s[i] == curr) { n       streak++; n       if (streak == k) { n         occ[curr]++; n         streak = 0; n       } n     } else { n       streak = 1; n       curr = s[i]; n     } n   } n   long long mx = 0; n   for (long long i = 0; i < 255; i++) { n     mx = max(occ[i], mx); n   } n   cout << mx << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int MOD = 1e9 + 7; n template <typename Tp> n void read(Tp &x) { n   x = 0; n   int op = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) op = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= op; n } n template <typename Tp> n void CMax(Tp &x, Tp y) { n   if (y > x) x = y; n } n template <typename Tp> n void CMin(Tp &x, Tp y) { n   if (y < x) x = y; n } n int n, m, a[N], mx; n int f[N], res1, res2, ans; n void norm(int &x) { n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n } n int Inv(int x, int p = MOD - 2) { n   int ret = 1; n   while (p) { n     if (p & 1) ret = 1ll * ret * x % MOD; n     x = 1ll * x * x % MOD; n     p >>= 1; n   } n   return ret; n } n int main() { n   read(n); n   for (int i = (1); i <= (n); ++i) read(a[i]), m += a[i], CMax(mx, a[i]); n   ans = 1ll * m * (m - 1) % MOD; n   for (int i = (1); i <= (mx); ++i) { n     res1 += res2; n     norm(res1); n     int v = Inv(i - 1 - m + MOD); n     res2 += v; n     norm(res2); n     res1 += v; n     norm(res1); n     f[i] = 1ll * res1 * (m - 1) % MOD; n     norm(f[i]); n   } n   for (int i = (1); i <= (n); ++i) ans += f[a[i]], norm(ans); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 1 << 17; n char buffer[SIZE]; n int pointer = SIZE; n char Advance() { n   if (pointer == SIZE) { n     fread(buffer, 1, SIZE, stdin); n     pointer = 0; n   } n   return buffer[pointer++]; n } n int Read() { n   int answer = 0; n   char ch = Advance(); n   while (!isdigit(ch)) ch = Advance(); n   while (isdigit(ch)) { n     answer = answer * 10 + ch -  0 ; n     ch = Advance(); n   } n   return answer; n } n const int MAXN = 100000; n const int MAXQ = 200000; n const int MAXNODES = 262144; n struct Point { n   int x; n   int y; n   bool operator<(const Point &other) const { return x < other.x; } n }; n Point v[1 + MAXQ]; n struct Query { n   Point a; n   Point b; n   int index; n   bool operator<(const Query &other) const { return b < other.b; }; n }; n Query query[1 + MAXQ]; n bool ok[1 + MAXQ]; n int tree[1 + MAXNODES], where, from, to, change; n void Update(int node, int left, int right) { n   if (left == right) { n     tree[node] = change; n     return; n   } n   int middle = (left + right) / 2; n   if (where <= middle) n     Update(2 * node, left, middle); n   else n     Update(2 * node + 1, middle + 1, right); n   tree[node] = min(tree[2 * node], tree[2 * node + 1]); n } n int Query(int node, int left, int right) { n   if (from <= left && right <= to) return tree[node]; n   int answer = MAXN, middle = (left + right) / 2; n   if (from <= middle) answer = min(answer, Query(2 * node, left, middle)); n   if (middle + 1 <= to) n     answer = min(answer, Query(2 * node + 1, middle + 1, right)); n   return answer; n } n void Solve(int k, int q, int n, int m) { n   sort(v + 1, v + k + 1); n   sort(query + 1, query + q + 1); n   for (int i = 1, j = 1; i <= q; i++) { n     while (j <= k && v[j].x <= query[i].b.x) { n       where = v[j].y; n       change = v[j].x; n       Update(1, 1, m); n       j++; n     } n     from = query[i].a.y; n     to = query[i].b.y; n     if (Query(1, 1, m) >= query[i].a.x) ok[query[i].index] = true; n   } n } n int main() { n   int n = Read(), m = Read(), k = Read(), q = Read(); n   for (int i = 1; i <= k; i++) { n     v[i].x = Read(); n     v[i].y = Read(); n   } n   for (int i = 1; i <= q; i++) { n     query[i].a.x = Read(); n     query[i].a.y = Read(); n     query[i].b.x = Read(); n     query[i].b.y = Read(); n     query[i].index = i; n   } n   Solve(k, q, n, m); n   for (int i = 1; i <= k; i++) swap(v[i].x, v[i].y); n   memset(tree, 0, sizeof(tree)); n   for (int i = 1; i <= q; i++) { n     swap(query[i].a.x, query[i].a.y); n     swap(query[i].b.x, query[i].b.y); n   } n   Solve(k, q, m, n); n   for (int i = 1; i <= q; i++) n     if (ok[i]) n       printf( YES n ); n     else n       printf( NO n ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int N, ans = 0, overtake = 0, pspeed, plimit = 1000; n stack<int> signs; n int main() { n   cin >> N; n   for (int i = 0, a, b; i < N; i++) { n     cin >> a; n     if (a == 1) { n       cin >> b; n       pspeed = b; n       while (!signs.empty()) { n         if (signs.top() < b) { n           ans++; n           signs.pop(); n         } else { n           break; n         } n       } n     } else if (a == 2) { n       if (overtake > 0) { n         ans += overtake; n         overtake = 0; n       } n     } else if (a == 3) { n       cin >> b; n       signs.push(b); n       while (!signs.empty()) { n         if (signs.top() < pspeed) { n           ans++; n           signs.pop(); n         } else { n           break; n         } n       } n     } else if (a == 4) { n       overtake = 0; n     } else if (a == 5) { n       while (!signs.empty()) signs.pop(); n     } else { n       overtake++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long N = 1e6 + 5; n const long long inf = 1e18; n long long powermod(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n loop: n   while (t--) { n     long long n; n     cin >> n; n     long long x = n / 4; n     if (n % 4 != 0) x++; n     n -= x; n     while (n--) { n       cout << 9; n     } n     while (x--) cout << 8; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 2000010; n char A[NMax]; n int N, can[NMax], H[NMax], min1[NMax], min2[NMax], H1[NMax]; n int main() { n   scanf( %s , A); n   N = strlen(A); n   int last = 0; n   for (int i = 0; i < N; i++) A[N + i] = A[i]; n   for (int i = 0; i < N; i++) { n     if (A[i] ==  ( ) n       H[i] = last + 1; n     else n       H[i] = last - 1; n     last = H[i]; n   } n   min1[0] = H[0]; n   for (int i = 1; i < N; i++) min1[i] = min(min1[i - 1], H[i]); n   min2[N - 1] = H[N - 1]; n   for (int i = N - 2; i >= 0; i--) min2[i] = min(min2[i + 1], H[i]); n   for (int i = 0; i < N; i++) n     if (A[i] ==  ( ) { n       int flag = 1; n       int delta = -1; n       if (A[i] ==  ( ) delta = 1; n       if (min2[i] - H[i] + delta < 0) flag = 0; n       if (i > 0 && min1[i - 1] + H[N - 1] - H[i] + delta < 0) flag = 0; n       if (H[N - 1] > 0) can[i] = flag; n     } n   for (int i = 0; i < N; i++) n     if (H[N - 1] == 0) can[i] = 1; n   last = 0; n   for (int i = N - 1; i >= 0; i--) { n     if (A[i] ==  ) ) n       H1[N - i - 1] = last + 1; n     else n       H1[N - i - 1] = last - 1; n     last = H1[N - i - 1]; n   } n   min1[0] = H1[0]; n   for (int i = 1; i < N; i++) min1[i] = min(min1[i - 1], H1[i]); n   min2[N - 1] = H1[N - 1]; n   for (int i = N - 2; i >= 0; i--) min2[i] = min(min2[i + 1], H1[i]); n   for (int i = 0; i < N; i++) n     if (A[N - i - 1] ==  ) ) { n       int flag = 1; n       int delta = -1; n       if (A[N - i - 1] ==  ) ) delta = 1; n       if (min2[i] - H1[i] + delta < 0) flag = 0; n       if (i > 0 && min1[i - 1] + H1[N - 1] - H1[i] + delta < 0) flag = 0; n       if (H[N - 1] < 0) can[(N - (i - 1 + N) % N - 1 + N) % N] = flag; n     } n   for (int i = 0; i < N; i++) can[i + N] = can[i]; n   int p1 = 0, p2 = 1, pos = -1; n   while (p1 < 2 * N && !can[p1]) p1 = p1 + 1; n   p2 = p1 + 1; n   while (p2 < 2 * N && !can[p2]) p2 = p2 + 1; n   while (p1 < N && p2 < N) { n     if (A[p2] > A[p1]) { n       p2 = p2 + 1; n       while (p2 < 2 * N && !can[p2]) p2 = p2 + 1; n     } else if (A[p2] < A[p1]) { n       p1 = p2; n       p2 = p2 + 1; n       while (p2 < 2 * N && !can[p2]) p2 = p2 + 1; n     } else { n       int k = 1; n       while (k < 2 * N && A[p1 + k] == A[p2 + k]) k++; n       if (A[p1 + k] > A[p2 + k]) { n         p1 = p1 + k + 1; n         while (p1 < 2 * N && !can[p1]) p1 = p1 + 1; n       } else { n         p2 = p2 + k + 1; n         while (p2 < 2 * N && !can[p2]) p2 = p2 + 1; n       } n     } n     if (p1 == p2) { n       p2 = p1 + 1; n       while (p2 < 2 * N && !can[p2]) p2 = p2 + 1; n     } else if (p1 > p2) n       swap(p1, p2); n   } n   pos = p2; n   if (p1 < p2) pos = p1; n   if (pos >= N) pos -= N; n   if (H[N - 1] > 0) { n     printf( %c , A[pos]); n     for (int i = (pos + 1) % N; i != pos; i = (i + 1) % N) printf( %c , A[i]); n     while (H[N - 1]--) printf( ) ); n   } else { n     while (H[N - 1]++) printf( ( ); n     printf( %c , A[pos]); n     for (int i = (pos + 1) % N; i != pos; i = (i + 1) % N) printf( %c , A[i]); n   } n   puts(  ); n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   int sum = 0; n   int c; n   int ans = 0; n   int k = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> c; n     sum += c; n     k += c; n     if (k < 0) k = 0; n     ans = max(ans, k); n   } n   cout << -sum + ans * 2; n } n


#include <bits/stdc++.h> n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool flag = false; n   while (!isdigit(c)) { n     if (c ==  - ) flag = true; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); n   if (flag) x = -x; n } n using namespace std; n const int inf = 987654321; n int n, m, s, t; n struct edge { n   int nxt, to, val; n   edge() {} n   edge(int jzp, int zzz, int lsr) { nxt = jzp, to = zzz, val = lsr; } n } e[5001000]; n int head[101000], ecnt = 1, hcur[101000]; n inline void addedge(int from, int to, int val) { n   e[++ecnt] = edge(head[from], to, val); n   head[from] = ecnt; n } n inline void Addedge(int u, int v, int w) { addedge(u, v, w), addedge(v, u, 0); } n int ls[101000], rs[101000], root_in, root_out, ttot; n void build_out(int L, int R, int &cur) { n   cur = ++ttot; n   if (L == R) return Addedge(s, cur, 1), void(); n   int mid = (L + R) >> 1; n   build_out(L, mid, ls[cur]), build_out(mid + 1, R, rs[cur]); n   Addedge(ls[cur], cur, inf); n   Addedge(rs[cur], cur, inf); n } n void build_in(int L, int R, int &cur) { n   cur = ++ttot; n   if (L == R) return Addedge(cur, t, 1), void(); n   int mid = (L + R) >> 1; n   build_in(L, mid, ls[cur]), build_in(mid + 1, R, rs[cur]); n   Addedge(cur, ls[cur], inf), Addedge(cur, rs[cur], inf); n } n void Addedge_out(int L, int R, int l, int r, int p, int cur) { n   if (l <= L && R <= r) return Addedge(cur, p, inf), void(); n   int mid = (L + R) >> 1; n   if (l <= mid) Addedge_out(L, mid, l, r, p, ls[cur]); n   if (r > mid) Addedge_out(mid + 1, R, l, r, p, rs[cur]); n } n void Addedge_in(int L, int R, int l, int r, int p, int cur) { n   if (l <= L && R <= r) return Addedge(p, cur, inf), void(); n   int mid = (L + R) >> 1; n   if (l <= mid) Addedge_in(L, mid, l, r, p, ls[cur]); n   if (r > mid) Addedge_in(mid + 1, R, l, r, p, rs[cur]); n } n inline void Seg_Addedge(int l, int r, int d, int u) { n   if (l > r || d > u) return; n   ++ttot; n   Addedge_out(1, n, l, r, ttot, root_out); n   Addedge_in(1, n, d, u, ttot, root_in); n } n struct node { n   int x, l, r, t; n   node() {} n   node(int jzp, int zzz, int lsr, int chr) { n     x = jzp, l = zzz, r = lsr, t = chr; n   } n } nd[101000]; n int ntot; n inline bool cmp(const node &a, const node &b) { n   return a.x < b.x || (a.x == b.x && a.t > b.t); n } n struct ODT { n   int l, r, v; n   ODT() {} n   ODT(int jzp, int zzz, int lsr) { l = jzp, r = zzz, v = lsr; } n   inline bool operator<(const ODT &a) const { return l < a.l; } n }; n set<ODT> st; n inline set<ODT>::iterator Split(int p) { n   if (p > n) return st.end(); n   set<ODT>::iterator it = --st.upper_bound(ODT(p, 0, 0)); n   if (it->l == p) return it; n   if (it->r < p) return ++it; n   int l = it->l, r = it->r, v = it->v; n   st.erase(it); n   st.insert(ODT(l, p - 1, v)); n   return st.insert(ODT(p, r, v)).first; n } n inline void Assign(int l, int r, int x) { n   set<ODT>::iterator itr = Split(r + 1), itl = Split(l); n   for (set<ODT>::iterator it = itl; it != itr; ++it) { n     Seg_Addedge(it->v, x - 1, it->l, it->r); n   } n   st.erase(itl, itr); n } n inline void Insert(int l, int r, int v) { st.insert(ODT(l, r, v)); } n int dep[101000], que[101000], front, rear; n inline bool bfs() { n   memset(dep, 0x3f, sizeof(dep)); n   front = rear = 0; n   for (int i = 1; i <= ttot; ++i) hcur[i] = head[i]; n   dep[s] = 0; n   que[++rear] = s; n   while (front < rear) { n     int cur = que[++front]; n     for (int i = head[cur]; i; i = e[i].nxt) { n       int to = e[i].to; n       if (!e[i].val || dep[to] < 5001000) continue; n       que[++rear] = to; n       dep[to] = dep[cur] + 1; n     } n   } n   return dep[t] <= 5001000; n } n int dfs(int cur, int limi) { n   if (cur == t || !limi) return limi; n   int flow = 0; n   for (int i = hcur[cur]; i; i = e[i].nxt) { n     int to = e[i].to; n     hcur[cur] = i; n     if (dep[to] != dep[cur] + 1) continue; n     int tmp = dfs(to, min(e[i].val, limi)); n     if (!tmp) continue; n     e[i].val -= tmp; n     e[i ^ 1].val += tmp; n     flow += tmp; n     limi -= tmp; n     if (!limi) break; n   } n   return flow; n } n int main() { n   read(n); n   read(m); n   for (int i = 1; i <= m; ++i) { n     int x_1, y_1, x_2, y_2; n     read(x_1), read(y_1), read(x_2), read(y_2); n     nd[++ntot] = node(x_1, y_1, y_2, 0); n     nd[++ntot] = node(x_2 + 1, y_1, y_2, 1); n   } n   sort(nd + 1, nd + 1 + ntot, cmp); n   s = 1, t = 2; n   ttot = 2; n   build_out(1, n, root_out); n   build_in(1, n, root_in); n   Insert(1, n, 1); n   for (int i = 1; i <= ntot; ++i) { n     int x = nd[i].x, l = nd[i].l, r = nd[i].r, t = nd[i].t; n     if (t == 0) { n       Assign(l, r, x); n     } else { n       Insert(l, r, x); n     } n   } n   for (set<ODT>::iterator it = st.begin(); it != st.end(); ++it) { n     Seg_Addedge(it->v, n, it->l, it->r); n   } n   int mxflow = 0; n   while (bfs()) mxflow += dfs(s, inf); n   printf( %d n , mxflow); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 666; n const double eps = 1e-10; n int n, m, K, cnt, sz; n int id[N], nod[N], cur[N]; n double deg[N], g[N][N]; n bool trap[N]; n double v[N][N]; n struct node { n   double vec[N]; n } vv[N]; n struct mat { n   double v[166][166]; n   mat() { memset(v, 0, sizeof(v)); } n } ans; n node operator*(node a, double w) { n   for (int i = 0; i <= n; i++) a.vec[i] *= w; n   return a; n } n node operator/(node a, double w) { n   for (int i = 0; i <= n; i++) a.vec[i] /= w; n   return a; n } n node operator+(node a, node b) { n   for (int i = 0; i <= n; i++) a.vec[i] += b.vec[i]; n   return a; n } n node operator-(node a, node b) { n   for (int i = 0; i <= n; i++) a.vec[i] -= b.vec[i]; n   return a; n } n mat operator*(mat a, mat b) { n   mat t; n   memset(t.v, 0, sizeof(t.v)); n   for (int i = 0; i <= sz; i++) { n     for (int j = 0; j <= sz; j++) { n       for (int k = 0; k <= sz; k++) { n         t.v[i][j] += a.v[i][k] * b.v[k][j]; n       } n     } n   } n   return t; n } n int main() { n   cin >> n >> m >> K; n   for (int i = 1; i <= n; i++) scanf( %d , &trap[i]); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     ++deg[x], ++deg[y], ++g[x][y], ++g[y][x]; n   } n   for (int i = 1; i <= n; i++) { n     if (trap[i]) { n       nod[++cnt] = i, id[i] = cnt; n     } n   } n   sz = cnt; n   for (int i = 1; i <= n; i++) { n     v[i][i] = -1; n     for (int j = 1; j <= n; j++) { n       if (trap[j]) { n         v[i][id[j] + n] += 1.0 * g[i][j] / deg[i]; n       } else { n         v[i][j] += 1.0 * g[i][j] / deg[i]; n       } n     } n   } n   for (int i = 1; i <= sz; i++) { n     vv[i + n].vec[i] = 1; n     v[i + n][i + n] = 1; n   } n   for (int i = 1; i <= n + sz; i++) { n     int me = -1; n     double mx = 0; n     for (int j = 1; j <= n + sz; j++) { n       if (abs(v[i][j]) > mx) { n         mx = abs(v[i][j]); n         me = j; n       } n     } n     if (me == -1 || abs(mx) < eps) continue; n     cur[i] = me; n     double val = v[i][me]; n     for (int j = 1; j <= n + sz; j++) v[i][j] /= val; n     vv[i] = vv[i] / val; n     for (int j = 1; j <= n + sz; j++) { n       if (j != i && abs(v[j][me]) > eps) { n         double val = v[j][me]; n         for (int k = 1; k <= n + sz; k++) v[j][k] -= v[i][k] * val; n         vv[j] = vv[j] - vv[i] * val; n       } n     } n   } n   for (int i = 1; i <= n + sz; i++) { n     if ((cur[i] <= n && trap[cur[i]]) || cur[i] == 1) { n       if (cur[i] == 1) { n         for (int j = 1; j <= sz; j++) ans.v[0][j] = vv[i].vec[j]; n       } else { n         for (int j = 1; j <= sz; j++) ans.v[id[cur[i]]][j] = vv[i].vec[j]; n       } n     } n   } n   mat res; n   memset(res.v, 0, sizeof(res.v)); n   for (int i = 0; i <= sz; i++) res.v[i][i] = 1; n   int nn = K - 1; n   for (; nn; nn >>= 1) { n     if (nn & 1) res = res * ans; n     ans = ans * ans; n   } n   printf( %.10lf n , res.v[0][id[n]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace Input { n const int BUF = 65536; n char buf[BUF + 1]; n char *head = buf, *tail = buf; n inline char inputchar() { n   if (head == tail) *(tail = (head = buf) + fread(buf, 1, BUF, stdin)) = 0; n   return *head++; n } n inline void inputnum(int &ret) { n   char ch = inputchar(); n   while (ch <  0  || ch >  9 ) ch = inputchar(); n   for (ret = 0; ch >=  0  && ch <=  9 ; ch = inputchar()) n     ret = ret * 10 + ch -  0 ; n } n };  // namespace Input n using Input::inputnum; n const int MAXN = 100005; n struct Node { n   long long sum; n   Node *c[2]; n   Node() {} n   Node(Node *a, Node *b) : sum(0) { c[0] = a, c[1] = b; } n   inline void modify(int v) { sum = v; } n   inline void update() { n     sum = (c[0] ? c[0]->sum : 0) + (c[1] ? c[1]->sum : 0); n   } n } node[MAXN * 90], *root[MAXN]; n int cnt = 0; n inline Node *newnode() { n   node[cnt] = Node(0, 0); n   return &node[cnt++]; n } n void modify(Node *&p, int x, int L, int R, int v) { n   if (!p) p = newnode(); n   if (L == R) { n     p->modify(v); n     return; n   } n   int M = (L + R) >> 1; n   if (x <= M) n     modify(p->c[0], x, L, M, v); n   else n     modify(p->c[1], x, M + 1, R, v); n   p->update(); n } n long long query(Node *p, int qL, int qR, int L, int R) { n   if (!p) return 0; n   if (L >= qL && R <= qR) return p->sum; n   int M = (L + R) >> 1; n   long long ret = 0; n   if (qL <= M) ret += query(p->c[0], qL, qR, L, M); n   if (qR > M) ret += query(p->c[1], qL, qR, M + 1, R); n   return ret; n } n set<int> pos[MAXN]; n int a[MAXN]; n int n, m, id, p, y, l, r; n inline void modify(int x, int y, int v) { n   for (int i = x; i <= n; i += (i & -i)) modify(root[i], y, 1, n, v); n } n inline long long query(int x, int R) { n   long long ret = 0; n   for (int i = x; i > 0; i ^= (i & -i)) ret += query(root[i], 1, R, 1, n); n   return ret; n } n void init() { n   memset(root, 0, sizeof(root)); n   for (int i = 1; i <= n; i++) { n     set<int> &cur = pos[a[i]]; n     cur.insert(i); n     set<int>::iterator pre = cur.find(i); n     if (pre != cur.begin()) { n       --pre; n       modify(*pre, i, i - *pre); n     } n   } n } n int main() { n   inputnum(n), inputnum(m); n   for (int i = 1; i <= n; i++) inputnum(a[i]); n   init(); n   for (int i = 1; i <= m; i++) { n     inputnum(id); n     if (id == 1) { n       inputnum(p), inputnum(y); n       int x = a[p]; n       a[p] = y; n       set<int> &sx = pos[x], &sy = pos[y]; n       set<int>::iterator pre, next; n       pre = next = sx.find(p); n       ++next; n       if (pre != sx.begin() && next != sx.end()) { n         --pre; n         modify(*pre, p, 0); n         modify(p, *next, 0); n         modify(*pre, *next, *next - *pre); n       } else if (pre != sx.begin()) { n         --pre; n         modify(*pre, p, 0); n       } else if (next != sx.end()) { n         modify(p, *next, 0); n       } n       sx.erase(p); n       sy.insert(p); n       pre = next = sy.find(p); n       ++next; n       if (pre != sy.begin() && next != sy.end()) { n         --pre; n         modify(*pre, *next, 0); n         modify(*pre, p, p - *pre); n         modify(p, *next, *next - p); n       } else if (pre != sy.begin()) { n         --pre; n         modify(*pre, p, p - *pre); n       } else if (next != sy.end()) { n         modify(p, *next, *next - p); n       } n     } else { n       inputnum(l), inputnum(r); n       printf( %I64d n , query(r, r) - query(l - 1, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n long long n, m, dau, cuoi, giua, pos1, pos2, t; n bool kt; n struct point { n   long long x, y; n } p[N], ab, ac, z; n long long ccw(point a, point b, point c) { n   ab = {b.x - a.x, b.y - a.y}; n   ac = {c.x - a.x, c.y - a.y}; n   return ab.x * ac.y - ab.y * ac.x; n } n long long dis(point a, point b) { n   return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y); n } n bool cmp1(point a, point b) { return (a.y < b.y || (a.y == b.y && a.x < b.x)); } n bool cmp2(point b, point c) { n   t = ccw(p[1], b, c); n   return t > 0 || (t == 0 && dis(p[1], b) > dis(p[1], c)); n } n bool check(point z) { n   if (ccw(p[1], p[2], z) <= 0) return 0; n   if (ccw(p[n], p[1], z) <= 0) return 0; n   pos1 = pos2 = 0; n   dau = 2; n   cuoi = n; n   while (dau <= cuoi) { n     giua = (dau + cuoi) / 2; n     if (ccw(p[1], p[giua], z) >= 0) n       pos1 = giua, dau = giua + 1; n     else n       cuoi = giua - 1; n   } n   if (pos1 == 0) return 0; n   dau = 2; n   cuoi = n; n   while (dau <= cuoi) { n     giua = (dau + cuoi) / 2; n     if (ccw(p[1], p[giua], z) <= 0) n       pos2 = giua, cuoi = giua - 1; n     else n       dau = giua + 1; n   } n   if (pos2 == 0) return 0; n   if (pos1 == pos2) { n     if (dis(p[1], p[pos1]) <= dis(p[1], z)) n       return 0; n     else n       return 1; n   } n   if (ccw(p[pos1], p[pos2], z) <= 0) return 0; n   return 1; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> p[i].x >> p[i].y; n   } n   sort(p + 1, p + 1 + n, cmp1); n   sort(p + 2, p + 1 + n, cmp2); n   cin >> m; n   kt = 1; n   for (int i = 1; i <= m; i++) { n     cin >> z.x >> z.y; n     if (kt == 1) n       if (check(z) == 0) { n         kt = 0; n       } n   } n   if (kt == 1) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n long long power(long long base, long long exp); n void solve() { n   long long n, m, k; n   cin >> n >> m >> k; n   vector<vector<long long>> arr(n, vector<long long>(m, 0)); n   long long count[3][5001]; n   memset(count, 0, sizeof(count)); n   list<pair<long long, long long>> key; n   for (long long i = 0; i < k; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     key.push_front({a, b}); n     count[a][b] = c; n   } n   set<pair<long long, long long>> s; n   for (auto k : key) { n     long long x = k.first; n     if (s.count(k)) n       continue; n     else n       s.insert(k); n     if (x == 2) { n       for (long long i = 0; i < n; i++) { n         if (arr[i][k.second - 1] == 0) { n           arr[i][k.second - 1] = count[k.first][k.second]; n         } n       } n     } else { n       for (long long i = 0; i < m; i++) { n         if (arr[k.second - 1][i] == 0) { n           arr[k.second - 1][i] = count[k.first][k.second]; n         } n       } n     } n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       cout << arr[i][j] <<    ; n     } n     cout <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   srand(chrono::high_resolution_clock::now().time_since_epoch().count()); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n long long power(long long base, long long exp) { n   base %= 1000000007; n   long long result = 1; n   while (exp > 0) { n     if (exp & 1) result = ((long long)result * base) % 1000000007; n     base = ((long long)base * base) % 1000000007; n     exp >>= 1; n   } n   return result; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   getline(cin, s); n   for (auto i = s.end() - 1; i >= s.begin(); i--) { n     if ((int(*i) >= 65 && (int(*i) <= 90)) || n         (int(*i) >= 97 && (int(*i) <= 122))) { n       if (*i ==  A  || *i ==  E  || *i ==  I  || *i ==  O  || *i ==  U  || n           *i ==  a  || *i ==  e  || *i ==  i  || *i ==  o  || *i ==  u  || n           *i ==  y  || *i ==  Y ) n         cout <<  YES ; n       else n         cout <<  NO ; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 110; n int N; n struct notlaptop { n   int speed, ram, hdd, cost; n   notlaptop(int a, int b, int c, int d) { n     speed = a; n     ram = b; n     hdd = c; n     cost = d; n   } n   notlaptop()<% %> n }; n notlaptop ar[MAXN]; n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     ar[i] = notlaptop(a, b, c, d); n   } n   int bcost = 1e9, res = -1; n   for (int i = 0; i < N; i++) { n     bool outdated = false; n     for (int j = 0; j < N; j++) { n       if (ar[i].speed < ar[j].speed and ar[i].ram < ar[j].ram and n           ar[i].hdd < ar[j].hdd) { n         outdated = true; n         break; n       } n     } n     if (!outdated) { n       if (ar[i].cost < bcost) { n         bcost = ar[i].cost, res = i + 1; n       } n     } n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char now[1 << 16], *S, *T; n   if (T == S) { n     T = (S = now) + fread(now, 1, 1 << 16, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n inline long long read() { n   register long long x = 0, f = 1; n   char ch = gc(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = gc(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = gc(); n   } n   return (f == 1) ? x : -x; n } n struct edge { n   int to, nxt, dis; n } g[1200001]; n int head[1200001], tot, n, m; n inline void made(int from, int to, int dis) { n   g[++tot].to = to; n   g[tot].nxt = head[from]; n   head[from] = tot; n   g[tot].dis = dis; n } n int sz[1200001], dep[1200001], wson[1200001]; n int s[1200001], a[1200001], F[10000001], ans[1200001]; n void dfs0(int u, int fa) { n   sz[u] = 1; n   dep[u] = dep[fa] + 1; n   for (int i = head[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == fa) continue; n     s[v] = a[v] ^ s[u] ^ g[i].dis; n     dfs0(v, u); n     sz[u] += sz[v]; n     if (sz[wson[u]] < sz[v]) wson[u] = v; n   } n } n void get(int u, int fa, int wht) { n   if (wht) n     F[s[u]] = max(F[s[u]], dep[u]); n   else n     F[s[u]] = 0; n   for (int i = head[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == fa) continue; n     get(v, u, wht); n   } n } n void calc(int zx, int u, int fa) { n   if (F[s[u]]) ans[zx] = max(F[s[u]] + dep[u] - 2 * dep[zx], ans[zx]); n   for (int i = 0, two = 1; i < 22; i++, two <<= 1) { n     if (F[s[u] ^ two]) n       ans[zx] = max(F[s[u] ^ two] + dep[u] - 2 * dep[zx], ans[zx]); n   } n   for (int i = head[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == fa) continue; n     calc(zx, v, u); n   } n } n void doit(int u, int fa, int wht) { n   for (int i = head[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == fa || v == wson[u]) continue; n     doit(v, u, 0); n   } n   if (wson[u]) doit(wson[u], u, 1); n   if (F[s[u]]) ans[u] = max(F[s[u]] - dep[u], ans[u]); n   for (int i = 0, two = 1; i < 22; i++, two <<= 1) { n     if (F[s[u] ^ two]) ans[u] = max(F[s[u] ^ two] - dep[u], ans[u]); n   } n   F[s[u]] = max(F[s[u]], dep[u]); n   for (int i = head[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == fa || v == wson[u]) continue; n     calc(u, v, u); n     get(v, u, 1); n   } n   if (!wht) get(u, fa, 0); n } n void dfs1(int u, int fa) { n   for (int i = head[u]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == fa) continue; n     dfs1(v, u); n     ans[u] = max(ans[u], ans[v]); n   } n } n int main() { n   n = read(); n   for (int i = 1; i < n; i++) { n     int u = read(); n     char ch = gc(); n     while (!isalpha(ch)) ch = gc(); n     made(i + 1, u, (1 << (ch -  a ))); n     made(u, i + 1, (1 << (ch -  a ))); n   } n   dfs0(1, 0); n   doit(1, 0, 0); n   dfs1(1, 0); n   for (int i = 1; i <= n; i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, s = 0; n   scanf( %I64d , &n); n   while (n > 0) { n     if (n % 8 == 1) s++; n     n /= 8; n   } n   printf( %I64d , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int a = sqrt(n); n     long long int b = n / a; n     if (n % a != 0) b++; n     if (n == 1) n       cout <<  0 n ; n     else { n       cout << a - 1 + b - 1 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long n, m, t; n long long a[500000]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> t; n   for (long long ti = 0; ti < t; ti++) { n     map<vector<long long>, long long> tab; n     map<vector<long long>, long long>::iterator iter; n     cin >> n >> m; n     vector<long long> v[n + 1]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     for (long long i = 0; i < m; i++) { n       long long a, b; n       cin >> a >> b; n       v[b].push_back(a); n     } n     for (long long i = 0; i < n; i++) sort(v[i + 1].begin(), v[i + 1].end()); n     for (long long i = 0; i < n; i++) n       if (!v[i + 1].empty()) tab[v[i + 1]] += a[i]; n     long long g; n     iter = tab.begin(); n     g = iter->second; n     iter++; n     for (; iter != tab.end(); iter++) g = gcd(g, iter->second); n     cout << g <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[600]; n char s[600]; n int cnt = 0; n int rec[3]; n void dfs(int src, char c) { n   s[src] = c; n   for (int i = 0; i < v[src].size(); i++) n     if (s[v[src][i]] !=  b ) { n       cnt++; n       s[v[src][i]] = c; n     } n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) n     if (v[i].size() == n - 1) { n       cnt++; n       s[i] =  b ; n     } n   rec[1] = cnt; n   for (int i = 1; i <= n; i++) n     if (s[i] !=  b ) { n       cnt++; n       dfs(i,  a ); n       break; n     } n   rec[0] = cnt - rec[1]; n   for (int i = 1; i <= n; i++) n     if (s[i] !=  a  && s[i] !=  b ) { n       cnt++; n       dfs(i,  c ); n       break; n     } n   rec[2] = cnt - rec[0] - rec[1]; n   int x = rec[0] * (rec[0] + rec[1] - 1) + rec[1] * (n - 1) + n           rec[2] * (rec[1] + rec[2] - 1); n   if (cnt != n || x / 2 != m) n     cout <<  No  << endl; n   else { n     cout <<  Yes  << endl; n     for (int i = 1; i <= n; i++) cout << s[i]; n     cout << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n inline void chmin(A &a, B b) { n   if (a > b) a = b; n } n template <typename A, typename B> n inline void chmax(A &a, B b) { n   if (a < b) a = b; n } n long long N, M; n string S; n signed main() { n   cin >> N >> M >> S; n   long long ans = 0; n   vector<long long> cnt(M); n   for (long long i = N - 1; i >= 0; i--) { n     long long c = S[i] -  a ; n     if (i + 1 < N && S[i] != S[i + 1]) { n       ans += N - i - 1 - cnt[c]; n       ans += cnt[c] * (M - 1); n       ans += (N - i - 1 - cnt[c]) * (M - 2); n     } n     cnt[c]++; n   } n   fill((cnt).begin(), (cnt).end(), 0ll); n   vector<long long> st(N); n   for (long long i = 0; i < N; i++) { n     long long c = S[i] -  a ; n     if (i && S[i - 1] != S[i]) { n       st[i] = 1; n       if (i >= 3 && S[i - 3] == S[i - 1] && S[i - 2] == S[i]) n         st[i] += st[i - 2]; n       ans += i - cnt[c] - st[i]; n       ans += cnt[c] * (M - 1); n       ans += (i - cnt[c]) * (M - 2); n       long long tmp = st[i] - 1; n       if (i - st[i] * 2 >= 0 && S[i - st[i] * 2] == S[i]) tmp++; n       ans -= tmp; n     } n     cnt[c]++; n   } n   ans += (M - 1) * N; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k, a[105], b[105], c[105], ai[105]; n map<long long int, long long int> pos; n void apply_f(long long int c[105]) { n   for (int i = 1; i <= n; i++) c[i] = a[c[i]]; n } n void apply_if(long long int c[105]) { n   for (int i = 1; i <= n; i++) c[i] = ai[c[i]]; n } n bool compare(long long int a1[105], long long int a2[105]) { n   for (int i = 1; i <= n; i++) n     if (a1[i] != a2[i]) return 0; n   return 1; n } n bool Check(int l, int r) { n   if (l == 0 || l > k) return 0; n   if ((k - l) % 2 == 1) return 0; n   if (l == 1 && k != 1 && r == 1) return 0; n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     ai[a[i]] = i; n     c[i] = i; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n   } n   long long int loc = 0; n   for (int i = 0; i <= 100; i++) { n     if (compare(c, b)) { n       pos[i] = 1; n     } n     apply_f(c); n   } n   for (int i = 1; i <= n; i++) c[i] = i; n   for (int i = 0; i <= 100; i++) { n     if (compare(c, b)) { n       pos[-i] = 1; n     } n     apply_if(c); n   } n   long long int R = 0, L = 0; n   for (int i = 0; i <= 100; i++) { n     if (pos[i]) { n       R = i; n       break; n     } n   } n   for (int i = 0; i >= -100; i--) { n     if (pos[i]) { n       L = -i; n       break; n     } n   } n   if (Check(L, R) || Check(R, L)) n     cout <<  YES  <<   n ; n   else n     cout <<  NO  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e3 + 10, MAXM = 2e5 + 10; n pair<int, int> v[MAXM]; n int l[MAXM], r[MAXM], s[MAXM], t[MAXM]; n vector<int> qv[MAXM]; n bool ans[MAXM]; n int last[MAXN][MAXN]; n int main() { n   int n, m, q; n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= m; i++) scanf( %d %d , &v[i].first, &v[i].second); n   for (int i = 0; i < q; i++) n     scanf( %d %d %d %d , l + i, r + i, s + i, t + i), qv[l[i]].push_back(i); n   for (int i = 0; i <= n; i++) n     for (int k = 0; k <= n; k++) last[i][k] = m + 1; n   for (int time = m; time > 0; time--) { n     int x, y; n     x = v[time].first, y = v[time].second; n     last[x][y] = last[y][x] = time; n     for (int vx = 1; vx <= n; vx++) { n       if (last[x][vx] < last[y][vx]) n         last[y][vx] = last[x][vx]; n       else n         last[x][vx] = last[y][vx]; n     } n     for (int ix : qv[time]) ans[ix] = (last[s[ix]][t[ix]] <= r[ix]); n   } n   for (int i = 0; i < q; i++) n     if (ans[i]) n       printf( Yes n ); n     else n       printf( No n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, x1, y1, x2, y2; n   long double s = 0; n   scanf( %d %d , &n, &k); n   scanf( %d %d , &x1, &y1); n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &x2, &y2); n     s += (long double)sqrt((long double)(x2 - x1) * (x2 - x1) + n                            (long double)(y2 - y1) * (y2 - y1)); n     x1 = x2; n     y1 = y2; n   } n   printf( %.6Lf , s * (long double)k / (long double)50); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int fx[] = {-1, 1, 0, 0}; n int fy[] = {0, 0, 1, -1}; n int dx[] = {-1, 1, 0, 0, 1, 1, -1, -1}; n int dy[] = {0, 0, 1, -1, 1, -1, -1, 1}; n int cx[] = {1, 1, 2, 2, -1, -1, -2, -2}; n int cy[] = {2, -2, 1, -1, 2, -2, 1, -1}; n const int N = 1 << 11; n int n; n int ans[N + 1]; n int ask(int p, int q) { n   printf( ? %d %d n , p, q); n   fflush(stdout); n   int x; n   scanf( %d , &x); n   return x; n } n int value(int x) { n   int ans = N - 1; n   for (int i = 1; i <= 15; i++) { n     int a = rand() % n + 1; n     if (a == x) continue; n     ans &= ask(x, a); n   } n   return ans; n } n void solve() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   srand(2137); n   scanf( %d , &n); n   vector<int> v; n   for (int i = 1; i <= n; i++) v.push_back(i); n   random_shuffle(v.begin(), v.end()); n   pair<int, int> zero = {v[0], value(v[0])}; n   for (int i = 1; i <= n - 1; i++) { n     int a = ask(zero.first, v[i]); n     if ((a & zero.second) == a) zero = {v[i], value(v[i])}; n   } n   for (int i = 1; i <= n; i++) n     if (i != zero.first) ans[i] = ask(i, zero.first); n   printf( !  ); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n } n signed main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Compteur { n   int nbs[4]; n   Compteur() { n     for (int iCoeff = 0; iCoeff < 4; iCoeff++) { n       nbs[iCoeff] = 0; n     } n   } n }; n Compteur operator+(const Compteur &a, const Compteur &b) { n   Compteur nouv; n   for (int iCoeff = 0; iCoeff < 4; iCoeff++) { n     nouv.nbs[iCoeff] = a.nbs[iCoeff] + b.nbs[iCoeff]; n   } n   return nouv; n } n int getP2(int a) { n   int s = 1; n   while (s < a) { n     s *= 2; n   } n   return s; n } n vector<Compteur> arbres[10][10]; n void modif(vector<Compteur> &a, int pos, Compteur val) { n   pos += (int)a.size() / 2; n   while (pos != 0) { n     a[pos] = a[pos] + val; n     pos /= 2; n   } n } n Compteur getSomme(vector<Compteur> &a, int deb, int fin) { n   deb += (int)a.size() / 2; n   fin += (int)a.size() / 2; n   Compteur somme; n   while (deb < fin) { n     if (deb % 2 == 1) { n       somme = somme + a[deb]; n       deb++; n     } n     if (fin % 2 == 1) { n       fin--; n       somme = somme + a[fin]; n     } n     deb /= 2; n     fin /= 2; n   } n   return somme; n } n int conv[256]; n int main() { n   string chaine; n   cin >> chaine; n   conv[ A ] = 0; n   conv[ T ] = 1; n   conv[ G ] = 2; n   conv[ C ] = 3; n   for (int iTaille = 1; iTaille <= 10; iTaille++) { n     for (int iMod = 0; iMod < iTaille; iMod++) { n       arbres[iTaille - 1][iMod] = vector<Compteur>( n           2 * getP2((chaine.size() + iTaille) / iTaille), Compteur()); n     } n   } n   for (int iCar = 0; iCar < chaine.size(); iCar++) { n     Compteur nouv; n     nouv.nbs[conv[chaine[iCar]]]++; n     for (int iTaille = 1; iTaille <= 10; iTaille++) { n       int mod = iCar % iTaille; n       int pos = iCar / iTaille; n       modif(arbres[iTaille - 1][mod], pos, nouv); n     } n   } n   int nbReqs; n   cin >> nbReqs; n   for (int iReq = 0; iReq < nbReqs; iReq++) { n     int type; n     cin >> type; n     if (type == 1) { n       int iCar; n       char val; n       cin >> iCar >> val; n       iCar--; n       Compteur nouv; n       nouv.nbs[conv[val]]++; n       nouv.nbs[conv[chaine[iCar]]]--; n       for (int iTaille = 1; iTaille <= 10; iTaille++) { n         int mod = iCar % iTaille; n         int pos = iCar / iTaille; n         modif(arbres[iTaille - 1][mod], pos, nouv); n       } n       chaine[iCar] = val; n     } else { n       int deb, fin; n       string comp; n       cin >> deb >> fin >> comp; n       deb--; n       int iTaille = comp.size(); n       int somme = 0; n       for (int iMod = 0; iMod < iTaille; iMod++) { n         int mod = (deb + iMod) % iTaille; n         int debPos = (deb + iMod) / iTaille; n         int finPos = (fin - iTaille) / iTaille; n         while (finPos * iTaille + mod < fin) { n           finPos++; n         } n         Compteur vals = getSomme(arbres[iTaille - 1][mod], debPos, finPos); n         somme += vals.nbs[conv[comp[iMod]]]; n       } n       cout << somme << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, d, b; n   char a, ch[3]; n   cin >> s; n   long long i, j, ans, n, flag = 0; n   a = s[0]; n   for (i = 1; i < s.size(); i++) { n     if (s[i] ==  . ) continue; n     if (s[i] ==  e ) { n       flag = 1; n     } n     if (s[i] !=  e ) { n       if (flag) n         b.push_back(s[i]); n       else n         d.push_back(s[i]); n     } n   } n   if (d.size() == 1 && d[0] ==  0 ) d.clear(); n   if (a ==  0 ) { n     cout << a; n     if (d.size()) cout <<  .  << d; n     return 0; n   } n   for (i = 0; i < b.size(); i++) ch[i] = b[i]; n   long long pr = atoi(ch); n   if (d.size() == pr) cout << a << d; n   if (d.size() > pr) { n     cout << a; n     for (i = 0; i < d.size(); i++) { n       if (i == pr) cout <<  . ; n       cout << d[i]; n     } n   } n   if (pr > d.size()) { n     if (a !=  0 ) cout << a; n     for (i = 0; i < pr; i++) { n       if (i < d.size()) n         cout << d[i]; n       else { n         cout <<  0 ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 15000001; n int n; n int sieve[sz]; n int v[300001]; n int cnt[sz]; n inline void solve() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &v[i]); n   } n   for (int i = 2; i < sz; i++) n     if (sieve[i] == 0) { n       for (int j = i; j < sz; j += i) sieve[j] = i; n     } n   for (int i = 0; i < n; i++) { n     int x = v[i]; n     if (x == 1) continue; n     vector<int> f; n     while (x != 1) { n       f.push_back(sieve[x]); n       x /= sieve[x]; n     } n     int cur = f[f.size() - 1]; n     cnt[cur]++; n     for (int j = f.size() - 2; j >= 0; j--) { n       if (f[j] == f[j + 1]) n         cur *= f[j]; n       else n         cur = f[j]; n       cnt[cur]++; n     } n   } n   int otv = n; n   for (int i = 0; i < sz; i++) { n     if (n != cnt[i]) otv = min(n - cnt[i], otv); n   } n   if (otv == n) n     printf( %d , -1); n   else n     printf( %d , otv); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q = 1; n   for (; q > 0; q--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n template <typename T> n inline void rit(T &x) { n   char c; n   for (x = 0, c = getchar(); c <  - ; c = getchar()) n     ; n   for (; c >=  0 ; x = x * 10 + c -  0 , c = getchar()) n     ; n } n int Q, op, x, c, p = 1; n long long int s[500001]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   rit(Q); n   while (Q--) { n     rit(op); n     if (op == 1) { n       s[c + 1] = s[c] + x, ++c, rit(x); n     } else { n       while (p < c && s[p] + x >= (s[p + 1] - s[p]) * p) ++p; n       double ans = (double)x - (double)(s[p] + x) / p; n       int a = ans; n       cout << a <<  .  << setw(7) << setfill( 0 ) << (int)((ans - a) * 1e7) n            <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n long long n; n int Q; n void ReadInput() { n   cin >> n >> Q; n   n *= 2LL; n } n long long Get(long long first) { n   if (first % 2 == 1) return (first + 1) / 2; n   return Get(first + (n - first) / 2); n } n void Solve() { n   while (Q-- > 0) { n     long long first; n     cin >> first; n     cout << Get(first) <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   ReadInput(); n   Solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n string a[2005]; n int common[2005][2005]; n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) n       for (int k = 0; k < a[i].size() && k < a[j].size(); k++) n         if (a[i][k] == a[j][k]) n           common[i][j] = k + 1; n         else n           break; n   int ans = 0; n   for (int i = 0; i < (1 << n); i++) n     if (__builtin_popcount(i) == k) { n       int cur = 0; n       for (int j = 0; j < n; j++) n         if (i & (1 << j)) n           for (int k = j + 1; k < n; k++) n             if (i & (1 << k)) cur += common[j][k]; n       if (cur > ans) ans = cur; n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void bfs(int v, vector<vector<int> > &graf, vector<vector<int> > &dist) { n   queue<int> q; n   q.push(v); n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     for (int i = 0; i < graf[p].size(); i++) { n       int to = graf[p][i]; n       if (dist[v][to] != 1e9) continue; n       dist[v][to] = dist[v][p] + 1; n       q.push(to); n     } n   } n } n struct ship { n   int pos, attack, fuel; n }; n struct base { n   int pos, def; n }; n vector<bool> used; n vector<int> lnk; n bool dfs(int v, vector<vector<int> > &vv) { n   used[v] = 1; n   for (int i = 0; i < vv[v].size(); i++) { n     int u = vv[v][i]; n     if (lnk[u] == -1 || !used[lnk[u]] && dfs(lnk[u], vv)) { n       lnk[u] = v; n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n, m; n   cin >> n >> m; n   vector<vector<int> > dist(n + 1, vector<int>(n + 1, 1e9)); n   vector<vector<int> > graf(n + 1); n   for (int i = 0; i < m; i++) { n     int first, t; n     cin >> first >> t; n     graf[first].push_back(t); n     graf[t].push_back(first); n   } n   for (int i = 1; i <= n; i++) dist[i][i] = 0; n   for (int i = 1; i <= n; i++) bfs(i, graf, dist); n   int second, b, k, h; n   cin >> second >> b >> k >> h; n   vector<ship> ships(second); n   for (int i = 0; i < second; i++) { n     cin >> ships[i].pos >> ships[i].attack >> ships[i].fuel; n   } n   vector<base> bases(b); n   for (int i = 0; i < b; i++) { n     cin >> bases[i].pos >> bases[i].def; n   } n   vector<vector<int> > vv(second); n   for (int i = 0; i < second; i++) { n     for (int j = 0; j < b; j++) { n       int d = dist[ships[i].pos][bases[j].pos]; n       if (d <= ships[i].fuel && bases[j].def <= ships[i].attack) n         vv[i].push_back(j); n     } n   } n   int ans = 0; n   used.resize(second); n   lnk.resize(b, -1); n   for (int i = 0; i < second; i++) { n     used.assign(n, 0); n     if (dfs(i, vv)) ans++; n   } n   cout << min(1LL * ans * k, 1LL * second * h); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n template <class T> n void chkmin(T &a, T b) { n   if (a > b) a = b; n } n int n, s, m; n int a[2005]; n int z[2005], id[2005]; n int dp[2][2005], f[2][2005]; n int nxt[2005], prv[2005]; n int ans; n vector<int> v[2005]; n int cnt; n int pos[2005]; n bool cmp(int x, int y) { return a[x] > a[y]; } n int dist(int x, int y) { n   if (x > y) swap(x, y); n   return min(y - x, n - y + x); n } n int cdist(int x, int y) { n   if (y <= x) return x - y; n   return n - y + x; n } n void calc_dp() { n   for (int x = m - 1; x >= 0; x--) { n     for (int ii = 0; ii < v[x].size(); ii++) { n       int i = v[x][ii]; n       if (a[i] == m - 1) n         dp[1][i] = 0; n       else { n         dp[1][i] = INF; n         for (int j = 0; j < n; j++) { n           if (a[j] == a[i] + 1) { n             if (dp[1][i] > dp[0][j] + dist(i, j)) { n               dp[1][i] = dp[0][j] + dist(i, j); n               f[1][i] = j; n             } n           } n         } n       } n     } n     for (int ii = 0; ii < v[x].size(); ii++) { n       int i = v[x][ii]; n       if (v[x].size() == 1) n         dp[0][i] = dp[1][i], f[0][i] = i; n       else { n         dp[0][i] = dp[1][prv[i]] + cdist(prv[i], i), f[0][i] = 0; n         if (dp[0][i] > dp[1][nxt[i]] + cdist(i, nxt[i])) { n           dp[0][i] = dp[1][nxt[i]] + cdist(i, nxt[i]); n           f[0][i] = 1; n         } n       } n     } n   } n   ans = INF; n   int t = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] == 0) { n       if (ans > dp[0][i] + dist(i, s)) { n         ans = dp[0][i] + dist(i, s); n         t = i; n       } n     } n   } n   printf( %d n , ans); n   if (cdist(s, t) == dist(s, t)) { n     printf( -%d n , dist(s, t)); n   } else n     printf( +%d n , dist(s, t)); n   int e = 0; n   while (1) { n     if (e == 0) { n       if (prv[t] == t) n         ; n       else { n         if (f[e][t] == 0) { n           int tl = t, tr = nxt[t]; n           while (tr != t) { n             printf( +%d n , cdist(tr, tl)); n             tr = nxt[tr], tl = nxt[tl]; n           } n           t = prv[t]; n         } else { n           int tl = prv[t], tr = t; n           while (tl != t) { n             printf( -%d n , cdist(tr, tl)); n             tr = prv[tr], tl = prv[tl]; n           } n           t = nxt[t]; n         } n       } n     } else { n       if (a[t] == m - 1) n         break; n       else { n         if (cdist(t, f[e][t]) == dist(t, f[e][t])) { n           printf( -%d n , dist(t, f[e][t])); n         } else n           printf( +%d n , dist(t, f[e][t])); n         t = f[e][t]; n       } n     } n     e ^= 1; n   } n } n int main() { n   scanf( %d %d , &n, &s); n   s--; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     z[m++] = a[i]; n     id[i] = i; n   } n   sort(z, z + m); n   m = unique(z, z + m) - z; n   for (int i = 0; i < n; i++) { n     a[i] = lower_bound(z, z + m, a[i]) - z; n     v[a[i]].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     if (v[a[i]].size() == 1) n       nxt[i] = prv[i] = i; n     else { n       for (int j = 1; j < n; j++) { n         if (a[(i + j) % n] == a[i]) prv[i] = (i + j) % n; n         if (a[(i + n - j) % n] == a[i]) nxt[i] = (i + n - j) % n; n       } n     } n   } n   sort(id, id + n, cmp); n   calc_dp(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool chk(int a, int b, int c, int sum) { return a + b + c == sum; } n int main() { n   int sum = 0; n   vector<int> u(6); n   for (int i = 0; i < 6; i++) { n     cin >> u[i]; n     sum += u[i]; n   } n   if (sum % 2) { n     cout <<  NO ; n     return 0; n   } n   sum /= 2; n   for (int a = 0; a < 6; a++) n     for (int b = 0; b < 6; b++) n       for (int c = 0; c < 6; c++) n         if (a != b && b != c && a != c && u[a] + u[b] + u[c] == sum) { n           cout <<  YES ; n           return 0; n         } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100000], lim, n, flag; n void DFS(long long x, long long y) { n   if (x > n) { n     if (y == a[lim]) flag = 1; n     return; n   } n   if (flag) return; n   DFS(x + 1, y); n   if (x != lim) { n     DFS(x + 1, y + a[x]); n     DFS(x + 1, y - a[x]); n   } n } n inline void work() { n   lim = flag = n = 0; n   memset(a, 0, sizeof(a)); n   scanf( %lld , &n); n   for (register long long i = 1; i <= n; ++i) scanf( %lld , &a[i]); n   for (lim = 1; lim <= n; ++lim) { n     DFS(1, 0); n     if (flag) { n       puts( YES ); n       return; n     } n   } n   puts( NO ); n } n signed main() { n   long long T; n   scanf( %lld , &T); n   while (T--) work(); n } n