#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, f = 0, d = 0; n   cin >> a >> b; n   for (int i = 1; i <= 6; i++) { n     int d1 = abs(a - i), d2 = abs(b - i); n     if (d1 < d2) n       f++; n     else if (d1 == d2) n       d++; n   } n   cout << f <<     << d <<     << 6 - f - d << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   long long maxi; n   maxi = -9999999999; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     int a, b; n     long long tmp; n     cin >> a >> b; n     if (b > k) { n       tmp = a - (b - k); n     } else { n       tmp = a; n     } n     maxi = max(tmp, maxi); n   } n   cout << maxi; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5, maxlen = 5e5 + 5; n const int inf = 3e6 + 5; n int n, x[maxn], y[maxn]; n char s[maxlen]; n int ansb, ansn; n bool check(int mid, int ty) { n   int xmin = 0, xmax = inf, ymin = 0, ymax = inf, dmin = -inf, dmax = inf; n   for (int i = 1; i <= n; i++) { n     xmin = max(xmin, x[i] - mid); n     xmax = min(xmax, x[i] + mid); n     ymin = max(ymin, y[i] - mid); n     ymax = min(ymax, y[i] + mid); n     dmin = max(dmin, x[i] - y[i] - mid); n     dmax = min(dmax, x[i] - y[i] + mid); n   } n   if (xmin > xmax || ymin > ymax || dmin > dmax) return 0; n   ymin = max(ymin, xmin - dmax); n   ymax = min(ymax, xmax - dmin); n   if (ymin > ymax) return 0; n   if (ty) { n     for (int x = xmin; x <= xmax; x++) { n       int rymin = max(ymin, x - dmax), rymax = min(ymax, x - dmin); n       if (rymin <= rymax && (x > 0 || rymax > 0)) { n         ansb = x, ansn = (x == 0) ? rymax : rymin; n         break; n       } n     } n   } n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     int len = strlen(s + 1); n     for (int j = 1; j <= len; j++) n       if (s[j] ==  B ) n         x[i]++; n       else n         y[i]++; n   } n   int l = 0, r = 2e6; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid, 0)) n       r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %d n , ++r); n   check(r, 1); n   for (int i = 1; i <= ansb; i++) putchar( B ); n   for (int i = 1; i <= ansn; i++) putchar( N ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> g; n vector<int> c; n int root, n; n vector<int> dfs(int u) { n   vector<int> combine; n   for (int v : g[u]) { n     vector<int> child = dfs(v); n     for (int z : child) combine.emplace_back(z); n   } n   if (c[u] > combine.size()) { n     cout <<  NO ; n     exit(0); n   } n   combine.insert(combine.begin() + c[u], u); n   return combine; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   g.resize(n + 1); n   c.resize(n + 1); n   for (int i = 1; i <= n; i++) { n     int p; n     cin >> p >> c[i]; n     if (!p) n       root = i; n     else n       g[p].emplace_back(i); n   } n   vector<int> order = dfs(root); n   vector<int> ans(n + 1); n   for (int i = 0; i < order.size(); i++) ans[order[i]] = i + 1; n   cout <<  YES n ; n   for (int i = 1; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve() { n   long long n; n   cin >> n; n   vector<long long> arr(n); n   map<long long, long long> counts; n   map<long long, long long> checks; n   for (auto &x : arr) { n     cin >> x; n     counts[x]++; n   } n   for (auto x : counts) { n     checks[x.second]++; n   } n   while (n--) { n     if (checks.size() == 1) { n       auto x = checks.begin(); n       if (x->first == 1 or x->second == 1) { n         cout << n + 1 << endl; n         return 0; n       } n     } n     if (checks.size() == 2) { n       auto it1 = checks.begin(); n       auto it2 = checks.begin(); n       if (it1->first == 1 and it1->second == 1) { n         cout << n + 1 << endl; n         return 0; n       } n       it2++; n       if (it2->first - it1->first == 1 and it2->second == 1) { n         cout << n + 1 << endl; n         return 0; n       } n     } n     long long x = counts[arr[n]]; n     if (x) { n       checks[x]--; n       counts[arr[n]]--; n       if (x - 1) checks[x - 1]++; n       if (!checks[x]) checks.erase(x); n     } n   } n } n signed main() { n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, a[100008], cnt = 0; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     if (a[i] == 1) { n       if (!cnt) cnt++; n       int j = i + 1; n       while (a[j] != 1 && j <= n) j++; n       if (a[j] == 1) { n         cnt += min(j - i, 2); n         i = j - 1; n       } n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline void fast_and_furious() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int n; n long long a[1005], b[1005], u, l, ans; n int main() { n   fast_and_furious(); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   ans = 0; n   for (int i = 0; i < n; i++) { n     u = l = 0; n     for (int j = i; j < n; j++) { n       u = u | a[j]; n       l = l | b[j]; n       ans = max(u + l, ans); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX_I = 1000; n long long a[MAX_I + 1]; n long long b[MAX_I + 1]; n int main() { n   long long n, m; n   long long c[5]; n   long long tmp; n   long long asum = 0; n   long long bsum = 0; n   cin >> c[1] >> c[2] >> c[3] >> c[4]; n   cin >> n >> m; n   for (long long i = 0; i < n; ++i) { n     cin >> tmp; n     a[i] = min(c[2], c[1] * tmp); n     asum += a[i]; n   } n   for (long long i = 0; i < m; ++i) { n     cin >> tmp; n     b[i] = min(c[2], c[1] * tmp); n     bsum += b[i]; n   } n   asum = min(asum, c[3]); n   bsum = min(bsum, c[3]); n   cout << min(asum + bsum, c[4]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int pos[n + 1]; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     pos[x] = i; n   } n   int curIndex = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (pos[x] < curIndex) { n       cout <<  0  ; n     } else { n       cout << pos[x] - curIndex + 1 <<    ; n       curIndex = pos[x] + 1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void go() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed; n   cout << setprecision(6); n } n void solve() { n   string s; n   cin >> s; n   long long n = s.size(); n   vector<long long> arr(n); n   long long cur = 0; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  + ) n       cur++; n     else n       cur--; n     arr[i] = cur; n   } n   map<long long, long long> mp; n   for (long long i = n - 1; i >= 0; i--) { n     if (arr[i] < 0) mp[arr[i]] = i + 1; n   } n   long long ans = n; n   for (auto x : mp) { n     ans += x.second; n   } n   cout << ans <<   n ; n } n signed main() { n   go(); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 10; n int T, n, m, a[N], b[N], l, r, ins[N], las[N]; n vector<int> to[N]; n struct node { n   int u; n   long long k; n   bool f; n }; n node q[N]; n inline bool check(long long now) { n   vector<int> s; n   for (int i = 1; i <= n; ++i) ins[i] = las[i] = 0; n   s.push_back(1); n   ins[1] = 1; n   while (s.size() != n) { n     memset(las + 1, 0, sizeof(int) * (n)); n     l = 1; n     r = 0; n     for (int i : s) q[++r] = (node){i, now, 0}; n     int x = -1, y = -1; n     while (l <= r) { n       node p = q[l++]; n       int u = p.u; n       for (int v : to[u]) { n         if (v == las[u]) continue; n         if (p.k <= a[v]) continue; n         if (las[v] || (ins[v] && p.f)) { n           x = u; n           y = v; n           break; n         } n         if (ins[v]) continue; n         las[v] = u; n         q[++r] = (node){v, p.k + b[v], 1}; n       } n       if (x != -1) break; n     } n     if (x == -1) return false; n     while (x) { n       if (ins[x]) { n         x = las[x]; n         continue; n       } n       ins[x] = 1; n       s.push_back(x); n       now += b[x]; n       x = las[x]; n     } n     while (y) { n       if (ins[y]) { n         y = las[y]; n         continue; n       } n       ins[y] = 1; n       s.push_back(y); n       now += b[y]; n       y = las[y]; n     } n   } n   return true; n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &m); n     for (int i = 2; i <= n; ++i) scanf( %d , &a[i]); n     for (int i = 2; i <= n; ++i) scanf( %d , &b[i]); n     for (int i = 1, u, v; i <= m; ++i) { n       scanf( %d%d , &u, &v); n       to[u].push_back(v), to[v].push_back(u); n     } n     int l = 0, r = 1e9 + 1, ans = 1e9 + 1; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (check(mid)) n         r = mid - 1, ans = mid; n       else n         l = mid + 1; n     } n     printf( %d n , ans); n     for (int i = 1; i <= n; ++i) to[i].clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 50; n int n; n int a[5][NMAX + 5]; n int b[NMAX + 5]; n vector<int> ans; n int main() { n   cin >> n; n   for (int i = 1; i <= 2; i++) n     for (int j = 1; j < n; j++) { n       cin >> a[i][j]; n       a[i][j] += a[i][j - 1]; n     } n   a[1][n] = a[1][n - 1]; n   a[2][n] = a[2][n - 1]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= n; i++) { n     int cost = b[i]; n     cost += a[1][i - 1]; n     cost += a[2][n - 1] - a[2][i - 1]; n     ans.push_back(cost); n   } n   sort(ans.begin(), ans.end()); n   cout << ans[0] + ans[1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NM = 1e5 + 10; n int color[NM]; n vector<pair<int, char> > adj[NM]; n bool dfs(int u, bool c, char type, vector<int> &all) { n   if (color[u] != -1) return (color[u] == c); n   all.push_back(u); n   color[u] = c; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i].first; n     char edge = adj[u][i].second; n     if (type == edge) { n       if (!dfs(v, c, type, all)) return false; n     } else { n       if (!dfs(v, !c, type, all)) return false; n     } n   } n   return true; n } n vector<int> solve(int n, char type) { n   vector<int> ret; n   memset(color, -1, sizeof(color)); n   for (int i = 1; i <= n; i++) { n     if (color[i] == -1) { n       vector<int> all; n       if (!dfs(i, 0, type, all)) { n         for (int j = 0; j <= n; j++) { n           ret.push_back(-1); n         } n         return ret; n       } else { n         int cnt[2] = {0, 0}; n         for (int j = 0; j < all.size(); j++) { n           cnt[color[all[j]]]++; n         } n         if (cnt[0] > cnt[1]) { n           for (int j = 0; j < all.size(); j++) { n             color[all[j]] ^= 1; n           } n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (color[i] == 0) { n       ret.push_back(i); n     } n   } n   return ret; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     char c; n     scanf( %d %d %c , &u, &v, &c); n     adj[u].push_back(make_pair(v, c)); n     adj[v].push_back(make_pair(u, c)); n   } n   vector<int> a = solve(n,  R ), b = solve(n,  B ); n   if (a.size() > n && b.size() > n) { n     printf( -1 n ); n   } else { n     if (a.size() > b.size()) { n       a.swap(b); n     } n     printf( %d n , a.size()); n     for (int i = 0; i < a.size(); i++) { n       printf( %d  , a[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long b; n   scanf( %lld , &b); n   long long flag = 0; n   for (long long i = 1; i < sqrt(b); i++) n     if (b % i == 0) flag = flag + 1; n   flag = 2 * flag; n   long long a = sqrt(b); n   if (a * a == b) flag = flag + 1; n   printf( %lld , flag); n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n vector<long long int> BIT((int)1e6 + 5, 0); n void _update(int x) { n   while (x <= n) { n     BIT[x]++; n     x += x & -x; n   } n } n int query(int x) { n   long long int sum = 0; n   while (x > 0) { n     sum += BIT[x]; n     x -= x & -x; n   } n   return sum; n } n int main() { n   long long int k; n   cin >> n >> k; n   long long int res = 1; n   k = min(n - k, k); n   int pos = 1; n   for (int i = 0; i < n; i++) { n     res++; n     int fi = pos + k; n     if (fi > n) { n       fi -= n; n     } n     if (fi > pos) { n       res += query(fi - 1) - query(pos); n     } else { n       res += query(fi - 1) + query(n) - query(pos); n     } n     _update(pos); n     _update(fi); n     pos = fi; n     cout << res <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2 * 1000 + 17; n int n, init[MAXN][MAXN], hope[MAXN][MAXN], c[MAXN][MAXN], r[MAXN], n     cnt = 1, comp[MAXN << 2]; n vector<int> out[MAXN << 2], in[MAXN << 2], order, trash; n vector<pair<string, int>> ans; n void add(int i, bool f1, int j, bool f2) { n   int v = (i << 1) | f1, u = (j << 1) | f2; n   out[v].push_back(u); n   in[u].push_back(v); n   out[u ^ 1].push_back(v ^ 1); n   in[v ^ 1].push_back(u ^ 1); n } n void scc(int v, vector<int> g[], vector<int> &vec) { n   comp[v] = cnt; n   for (auto i : g[v]) n     if (!comp[i]) scc(i, g, vec); n   vec.push_back(v); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   char C; n   cin >> n; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) cin >> C, init[i][j] = int(C -  0 ); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       cin >> C, hope[i][j] = int(C -  0 ), c[i][j] = init[i][j] ^ hope[i][j]; n   for (int i = 1; i <= n; i++) cin >> C, r[i] = int(C -  0 ); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (c[i][j] && r[i] && r[j]) n         add(i, true, n + j, false), add(i, false, n + j, true); n       else if (c[i][j] && r[i] && !r[j]) n         add(i, true, n + j, true), add(i, false, n + j, true); n       else if (c[i][j] && !r[i] && r[j]) n         add(n + j, true, i, true), add(n + j, false, i, true); n       else if (c[i][j] && !r[i] && !r[j]) n         return cout << -1 << endl, 0; n       else if (!c[i][j] && r[i] && r[j]) n         add(i, true, n + j, true), add(i, false, n + j, false); n       else if (!c[i][j] && r[i] && !r[j]) n         add(i, true, n + j, false), add(i, false, n + j, false); n       else if (!c[i][j] && !r[i] && r[j]) n         add(n + j, true, i, false), add(n + j, false, i, false); n       else n         ; n   for (int i = 1; i <= ((n << 2) | 1); i++) n     if (!comp[i]) scc(i, out, order); n   cnt = 0; n   memset(comp, 0, sizeof(comp)); n   for (int i = (n << 1); ~i; i--) { n     int v = order[i]; n     if (!comp[v]) cnt++, scc(v, in, trash); n   } n   for (int i = 1; i <= (n << 1); i++) { n     if (comp[i << 1] == comp[i << 1 | 1]) return cout << -1 << endl, 0; n     if (comp[i << 1 | 1] > comp[i << 1]) { n       if (i > n) n         ans.push_back({ col , i - n - 1}); n       else n         ans.push_back({ row , i - 1}); n     } n   } n   cout << ans.size() << endl; n   for (auto i : ans) cout << i.first <<     << i.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9; n long long int n, h; n bool check(long long int maxh) { n   long long int k = min(maxh, h); n   long long int count = maxh * maxh - k * (k - 1) / 2; n   return (n >= count); n } n long long int get(long long int maxh) { n   long long int k = min(h, maxh); n   long long int count = maxh * maxh - k * (k - 1) / 2; n   long long int length = (2 * maxh - 1) - (k - 1); n   n -= count; n   return length + (n + maxh - 1) / maxh; n } n int main() { n   cin >> n >> h; n   long long int l = 1, r = INF; n   while (l < r - 1) { n     long long int m = l + (r - l) / 2; n     if (check(m)) n       l = m; n     else n       r = m; n   } n   cout << (check(r) ? get(r) : get(l)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int it[200041], n, io; n int down(int x) { n   int s = 0, rs = x; n   while (x <= n * 2) { n     s = max(it[x], s); n     x += x & (-x); n   } n   return s; n } n void up(int x, int u) { n   while (x > 0) { n     it[x] = max(it[x], u); n     x -= x & (-x); n   } n } n int main() { n   int i, a[100005], o = 0, co; n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = n - 1; i >= 0; i--) { n     co = down(a[i]); n     up(a[i], co + 1); n     o = max(co + 1, o); n   } n   cout << o << endl; n } n


#include <bits/stdc++.h> n int count[5001] = {0}, i = 0, n, a, c = 0; n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a); n     count[a - 1]++; n   } n   for (i = 0; i < n; i++) n     if (count[i] == 0) c++; n   printf( %d n , c); n   return 0; 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 const int N = 1e5 + 7; n pair<int, int> q[N]; n int n, m, fa[N << 1], a[N]; n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= n; i++) a[i] = !read(); n   for (int i = 1; i <= m; i++) fa[i] = i, fa[i + m] = i + m; n   for (int i = 1; i <= m; i++) { n     int opt = read(); n     for (int j = 1; j <= opt; j++) { n       int v = read(); n       if (!q[v].first) n         q[v].first = i; n       else n         q[v].second = i; n     } n   } n   for (int i = 1; i <= n; i++) { n     int x1 = find(q[i].first), x2 = find(q[i].first + m); n     int y1 = find(q[i].second), y2 = find(q[i].second + m); n     if (a[i]) { n       fa[x1] = y2; n       x2 = find(x2), y1 = find(y1); n       fa[x2] = y1; n     } else { n       fa[x1] = y1; n       x2 = find(x2), y2 = find(y2); n       fa[x2] = y2; n     } n   } n   for (int i = 1; i <= m; i++) { n     if (find(i) == find(i + m)) { n       printf( NO ); n       return 0; n     } n   } n   printf( YES ); n } n


#include <bits/stdc++.h> n using namespace std; n int m1[502][502], m2[502][502]; n int num[502], lie[502]; n vector<int> v; n const int INF = 0x3f3f3f3f; n int main(void) { n   int a, b, i, j, flag = 1, sum = 0; n   scanf( %d %d , &a, &b); n   for (i = 0; i < a; i++) { n     for (j = 0; j < b; j++) { n       scanf( %d , &m1[i][j]); n     } n   } n   for (i = 0; i < a; i++) { n     for (j = 0; j < b; j++) { n       scanf( %d , &m2[i][j]); n     } n   } n   for (i = 0; i < b; i++) { n     num[i] = lie[i] = 0; n   } n   for (i = 0; i < a; i++) { n     for (j = 0; j < b; j++) { n       num[j] += m1[i][j] ^ m2[i][j]; n       lie[i] += m1[i][j] ^ m2[i][j]; n     } n   } n   for (i = 0; i < b; i++) { n     if (num[i] % 2) { n       flag = 0; n       break; n     } n   } n   for (i = 0; i < a; i++) { n     if (lie[i] % 2) { n       flag = 0; n       break; n     } n   } n   if (flag) { n     printf( Yes n ); n   } else { n     printf( No n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, c = 0, se = 0, so = 0, soo = 0, see = 0; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i % 2 == 0) n       so += a[i]; n     else n       se += a[i]; n   } n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0) n       soo += a[i]; n     else n       see += a[i]; n     int t = se - see; n     int t1 = so - soo; n     if ((i + 1) % 2 == 0) n       see -= a[i]; n     else n       soo -= a[i]; n     if (see + t1 == soo + t) c++; n     if ((i + 1) % 2 == 0) n       see += a[i]; n     else n       soo += a[i]; n   } n   cout << c << endl; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, n, i, t, r = 0; n   while (cin >> t) { n     for (i = 0; i < t; i++) { n       cin >> a >> b >> n; n       if (n % 3 == 1) n         cout << b << endl; n       else if (n % 3 == 0) n         cout << a << endl; n       else { n         r = a ^ b; n         cout << r << endl; n         r = 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500009; n const int mod = 1000000007; n int n, m, x, y; n vector<int> v[N]; n vector<bool> vis; n bool is; n void pre() { vis.assign(N, 0); } n void dfs(int ver, int par) { n   vis[ver] = 1; n   for (auto &it : v[ver]) { n     if (it == par) continue; n     if (vis[it] == 1) { n       is = 1; n       continue; n     } n     dfs(it, ver); n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin.exceptions(cin.failbit); n   ; n   pre(); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (vis[i]) continue; n     is = 0; n     dfs(i, -1); n     ans++; n     if (is) ans--; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n double pi = 3.1415926535; n long long int gcd(long long int a, long long int b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long int power(long long int a, long long int n) { n   long long int res = 1; n   while (n) { n     if (n % 2) { n       res = res * a; n     } n     n = n / 2; n     a = a * a; n   } n   return res; n } n bool checkprime(long long int n) { n   for (long long int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n long long int mpow(long long int a, long long int n) { n   long long int res = 1; n   while (n) { n     if (n % 2) { n       res = (res * a) % mod; n     } n     a = (a * a) % mod; n     n = n / 2; n   } n   return res; n } n void solve() { n   string s; n   cin >> s; n   map<int, int> mp; n   int n = s.size(); n   int cnt = 0; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     cnt = 0; n     while (i < n && s[i] ==  1 ) { n       i++; n       cnt++; n     } n     v.push_back(cnt); n   } n   int a[101] = {0}, ans = 0; n   bool f = true; n   sort(v.begin(), v.end(), greater<int>()); n   for (auto c : v) { n     if (f) { n       ans += c; n       f = false; n     } else { n       f = true; n     } n   } n   cout << ans << endl; n } n int main() { n   long long int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   char c = getchar(); n   T x = 0, f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   return x * f; n } n template <typename T> n inline void write(T x) { n   if (x < 0) x = (~x) + 1, putchar( - ); n   if (x / 10) write(x / 10); n   putchar(x % 10 | 48); n } n const int INF = 0x3f3f3f3f; n long long int qz[200005][30]; n long long int ans[30]; n int main() { n   int T = read<int>(); n   while (T--) { n     memset(ans, 0, sizeof(ans)); n     int n = read<int>(), m = read<int>(); n     for (int i = 1; i <= n; i++) { n       char c; n       cin >> c; n       int a = c -  a ; n       ans[a]++; n       for (int j = 0; j < 26; j++) qz[i][j] = qz[i - 1][j]; n       qz[i][a] = qz[i - 1][a] + 1; n     } n     for (int i = 0; i < m; i++) { n       int a = read<int>(); n       for (int j = 0; j < 26; j++) { n         ans[j] += qz[a][j]; n       } n     } n     for (int i = 0; i < 26; i++) { n       cout << ans[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 310; n map<int, int> Map; n int l[MAXN], c[MAXN], n; n int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &l[i]); n   for (int i = 1; i <= n; ++i) scanf( %d , &c[i]); n   for (int i = 1; i <= n; ++i) { n     if (Map[l[i]]) n       Map[l[i]] = min(Map[l[i]], c[i]); n     else n       Map[l[i]] = c[i]; n     for (map<int, int>::iterator it = Map.begin(); it != Map.end(); it++) { n       int x = gcd(l[i], it->first); n       if (Map[x]) n         Map[x] = min(Map[x], c[i] + it->second); n       else n         Map[x] = c[i] + it->second; n     } n   } n   if (Map[1]) n     printf( %d , Map[1]); n   else n     printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 1000 * 100 + 10; n const int inf = (1 << 29); n string s, t, ans, tmp; n int cnt[30], curr[30]; n int main() { n   cin >> s >> t; n   sort(s.rbegin(), s.rend()); n   for (int i = 0; i < s.size(); i++) cnt[s[i] -  a ]++; n   if (s <= t) n     cout <<  -1 n ; n   else { n     for (int i = t.size() - (t.size() == s.size()); i >= 0; i--) { n       bool good = true; n       for (int j = 0; j < 26; j++) curr[j] = cnt[j]; n       for (int j = 0; j < i; j++) { n         curr[t[j] -  a ]--; n         if (curr[t[j] -  a ] < 0) good = false; n       } n       if (!good) continue; n       char c =  z  + 1; n       if (i < t.size()) { n         for (int j = 0; j < 26; j++) n           if (curr[j] && j > t[i] -  a ) c = min(c, (char)(j +  a )); n         if (c <=  z ) n           curr[c -  a ]--; n         else n           continue; n       } n       for (int j = 0; j < i; j++) ans.push_back(t[j]); n       if (i < t.size()) ans.push_back(c); n       for (int j = 0; j < 26; j++) { n         while (curr[j]) { n           ans.push_back((char)(j +  a )); n           curr[j]--; n         } n       } n       cout << ans << endl; n       break; n     } 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   map<unsigned long long, vector<int>> pow; n   vector<int> a(m + 1), s(m + 1), u(m + 1), b(n + 1); n   for (int i = 1; i <= n; ++i) { n     unsigned long long p; n     scanf( %llu , &p); n     pow[p].push_back(i); n   } n   for (int i = 1; i <= m; ++i) scanf( %llu , &s[i]); n   int c = 0, us = 0; n   for (int i = 0; i < 32; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (!u[j] && pow.count(s[j]) && !pow[s[j]].empty()) { n         u[j] = 1; n         us += i; n         c++; n         int cp = pow[s[j]].back(); n         pow[s[j]].pop_back(); n         b[cp] = j; n         a[j] = i; n       } else n         s[j] = (s[j] + 1) / 2; n     } n   } n   printf( %d %d n , c, us); n   for (int i = 1; i <= m; ++i) { n     printf( %d  , a[i]); n   } n   printf(  n ); n   for (int i = 1; i <= n; ++i) { n     printf( %d  , b[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   double x, y; n   Point(double x = 0, double y = 0) : x(x), y(y) {} n   Point operator+(Point b) { return Point(x + b.x, y + b.y); } n   Point operator-(Point b) { return Point(x - b.x, y - b.y); } n   double operator^(Point b) { return x * b.y - y * b.x; } n   Point operator*(double b) { return Point(x * b, y * b); } n   double operator*(Point b) { return x * b.x + y * b.y; } n   inline double dis() { return sqrt(*this * *this); } n }; n struct tcurts { n   double r; n   int type; n   bool operator<(const tcurts &b) const { return r < b.r; } n } q[100010 * 2]; n int main() { n   Point A, B, C; n   scanf( %lf%lf%lf%lf , &A.x, &A.y, &B.x, &B.y), C = (A + B) * 0.5; n   Point normal = (B - A) * (1 / (B - A).dis()); n   normal = Point(-normal.y, normal.x); n   int n, top = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     Point p; n     double R; n     scanf( %lf%lf%lf , &p.x, &p.y, &R); n     int flag = ((p - A) ^ (B - A)) > 0; n     double l = -1e12, r = 1e12; n     for (int times = 1; times <= 80; times++) { n       double mid = (l + r) / 2; n       Point O = C + normal * mid; n       if ((O - A).dis() + R < (p - O).dis()) n         (flag ? r : l) = mid; n       else n         (flag ? l : r) = mid; n     } n     q[++top] = (tcurts){l, flag ? -1 : 1}, l = -1e12, r = 1e12; n     for (int times = 1; times <= 80; times++) { n       double mid = (l + r) / 2; n       Point O = C + normal * mid; n       if ((O - A).dis() - R > (p - O).dis()) n         (flag ? l : r) = mid; n       else n         (flag ? r : l) = mid; n     } n     q[++top] = (tcurts){l, flag ? 1 : -1}; n   } n   q[++top] = (tcurts){-1e12, 0}, q[++top] = (tcurts){1e12, 0}, n   q[++top] = (tcurts){0, 0}, sort(q + 1, q + top + 1); n   double res = 1e12; n   bool flag = false; n   for (int i = 1, s = 0; i <= top; i++) { n     if (!s) res = min(res, fabs(q[i].r)), flag = true; n     s += q[i].type; n     if (!s) res = min(res, fabs(q[i].r)), flag = true; n   } n   if (!flag) n     puts( -1 ); n   else n     printf( %.10lf n , (C + normal * res - A).dis()); n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 400400; n int const mod = 1000000007; n void add(int &a, int b) { n   if ((a += b) >= mod) a -= mod; n } n int prod(int a, int b) { n   auto x = (unsigned long long)a * b; n   unsigned int xh = x >> 32, xl = x, q, r; n   asm( divl %4;  n t  :  =a (q),  =d (r) :  d (xh),  a (xl),  r (mod)); n   return r; n } n int sum[N], sq[N], down[N]; n void apply(int v, int l, int r, int val) { n   add(sq[v], prod(2, prod(sum[v], val))); n   add(sq[v], prod(val, prod(val, r - l))); n   add(sum[v], prod(val, r - l)); n   add(down[v], val); n } n void push(int v, int l, int r) { n   int m = (l + r) / 2; n   apply(2 * v + 1, l, m, down[v]); n   apply(2 * v + 2, m, r, down[v]); n   down[v] = 0; n } n void add(int v, int l, int r, int from, int to, int val) { n   if (r <= from || to <= l) return; n   if (from <= l && r <= to) { n     apply(v, l, r, val); n     return; n   } n   push(v, l, r); n   int m = (l + r) / 2; n   add(2 * v + 1, l, m, from, to, val); n   add(2 * v + 2, m, r, from, to, val); n   sum[v] = sum[2 * v + 1] + sum[2 * v + 2]; n   add(sum[v], 0); n   sq[v] = sq[2 * v + 1] + sq[2 * v + 2]; n   add(sq[v], 0); n } n int get_sq(int v, int l, int r, int from, int to) { n   if (r <= from || to <= l) return 0; n   if (from <= l && r <= to) return sq[v]; n   push(v, l, r); n   int m = (l + r) / 2; n   int ans = n       get_sq(2 * v + 1, l, m, from, to) + get_sq(2 * v + 2, m, r, from, to); n   add(ans, 0); n   return ans; n } n int n; n vector<pair<int, int>> g[N]; n int tin[N], tout[N]; n void dfs(int v, int par) { n   static int timer = 0; n   tin[v] = timer++; n   for (auto e : g[v]) { n     if (e.first != par) { n       dfs(e.first, v); n       add(0, 0, n, tin[e.first], tout[e.first], e.second); n     } n   } n   tout[v] = timer; n } n int ans[N]; n vector<pair<int, int>> qs[N]; n void calc(int v, int par) { n   for (auto q : qs[v]) { n     int cur = get_sq(0, 0, n, tin[q.first], tout[q.first]); n     int all = get_sq(0, 0, n, 0, n); n     ans[q.second] = prod(2, cur); n     add(ans[q.second], mod - all); n   } n   for (auto e : g[v]) { n     if (e.first != par) { n       add(0, 0, n, 0, n, e.second); n       add(0, 0, n, tin[e.first], tout[e.first], mod - prod(2, e.second)); n       calc(e.first, v); n       add(0, 0, n, 0, n, mod - e.second); n       add(0, 0, n, tin[e.first], tout[e.first], prod(2, e.second)); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     --a, --b; n     g[a].emplace_back(b, c); n     g[b].emplace_back(a, c); n   } n   dfs(0, -1); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     --u, --v; n     qs[u].emplace_back(v, i); n   } n   calc(0, -1); n   for (int i = 0; i < q; ++i) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int x[2010], y[2010]; n long long nc2(long long n) { return (n * n - n) / 2LL; } n int main() { n   int T, n, caseNo = 0; n   map<pair<int, int>, long long> cnt; n   scanf( %d , &n); n   for (int i = 0; i <= n - 1; i++) { n     scanf( %d %d , &x[i], &y[i]); n     x[i] *= 2; n     y[i] *= 2; n   } n   for (int i = 0; i <= n - 1; i++) { n     for (int j = i + 1; j <= n - 1; j++) { n       int midX = x[i] + x[j]; n       int midY = y[i] + y[j]; n       cnt[{midX, midY}]++; n     } n   } n   long long ans = 0LL; n   map<pair<int, int>, long long>::iterator it; n   for (it = cnt.begin(); it != cnt.end(); it++) { n     long long Cnt = it->second; n     ans += nc2(Cnt); n   } n   printf( %lld n , ans); n   cnt.clear(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Node { n   set<int> adj; n  n  public: n   bool isLeave() const { return adj.size() == 1; } n   void add(int v) { adj.insert(v); } n   void remove(int v) { adj.erase(v); } n   int front() const { return *adj.begin(); } n }; n const int N = 100001; n Node neighbors[N]; n bool marked[N] = {}; n int level[N] = {}; n int pred[N] = {}; n list<int>::iterator sortedPos[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     neighbors[u].add(v); n     neighbors[v].add(u); n   } n   cout << 1; n   if (n == 1) { n     cout << endl; n   } else if (n == 2) { n     cout <<     << 2 << endl; n   } else { n     list<int> leaves, sorted; n     for (int i = 1; i <= n; i++) { n       if (neighbors[i].isLeave()) leaves.push_back(i); n       sortedPos[i] = sorted.end(); n     } n     { n       while (!leaves.empty()) { n         const int u = leaves.front(); n         leaves.pop_front(); n         if (neighbors[u].isLeave()) { n           const int v = neighbors[u].front(); n           if (level[u] + 1 > level[v]) { n             level[v] = level[u] + 1; n             pred[v] = u; n             if (sortedPos[v] != sorted.end()) sorted.erase(sortedPos[v]); n             assert(sorted.empty() || level[v] >= level[sorted.front()]); n             sorted.push_front(v); n             sortedPos[v] = sorted.begin(); n           } n           neighbors[u].remove(v); n           neighbors[v].remove(u); n           if (neighbors[v].isLeave()) leaves.push_back(v); n         } n       } n       int k = 2; n       int controlled = 0; n       int v = sorted.front(); n       sorted.pop_front(); n       controlled += level[v] + 1; n       assert(!marked[v]); n       marked[v] = true; n       v = pred[v]; n       while (v) { n         assert(!marked[v]); n         marked[v] = true; n         v = pred[v]; n       } n       while (!sorted.empty() && controlled < n - 1) { n         v = sorted.front(); n         sorted.pop_front(); n         if (!marked[v]) { n           controlled += level[v] + 1; n           marked[v] = true; n           v = pred[v]; n           while (v) { n             assert(!marked[v]); n             marked[v] = true; n             v = pred[v]; n           } n           cout <<     << controlled; n           k++; n         } n       } n       for (; k <= n; k++) { n         if (controlled < n) controlled++; n         cout <<     << controlled; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111; n const int LIMIT = 10000; n const double eps = 1e-6; n int a[N], b[N]; n int n, m; n int isSq(long long t) { n   long long tt = (long long)sqrt(t + eps); n   return tt * tt == t; n } n void gen(int n, int a[]) { n   bool flag = false; n   while (!flag) { n     long long total = 0; n     for (int i = 1; i < n; i++) { n       a[i] = rand() % LIMIT + 1; n       total += 1LL * a[i] * a[i]; n     } n     for (int i = 1; i <= LIMIT; i++) { n       long long t = total + i * i; n       a[n] = i; n       if (isSq(t)) { n         flag = true; n         break; n       } n     } n   } n } n int main() { n   scanf( %d %d , &n, &m); n   gen(n, a); n   gen(m, b); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       printf( %d%c , a[i] * b[j], j == m ?   n  :    ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cycle[3001]; n int vis[3001]; n vector<vector<int> > adj; n bool f; n int node; n bool getC(int ind, int prev) { n   if (cycle[ind]) { n     node = ind; n     return 1; n   } n   cycle[ind] = 1; n   for (int i = 0; i < (int)adj[ind].size(); i++) n     if (prev != adj[ind][i] && getC(adj[ind][i], ind)) { n       if (!f) n         cycle[ind] = 0; n       else n         vis[ind] = 0; n       if (ind == node) f = 0; n       return 1; n     } n   cycle[ind] = 0; n   return 0; n } n void dfs(int ind, int l) { n   vis[ind] = l; n   for (int i = 0; i < (int)adj[ind].size(); i++) { n     if (vis[adj[ind][i]] == -1) dfs(adj[ind][i], l + 1); n   } n } n int main() { n   memset(vis, -1, sizeof vis); n   int n; n   f = 1; n   scanf( %d , &n); n   adj = vector<vector<int> >(n + 1); n   int a, b; n   for (int i = 0; i < (int)n; i++) { n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   getC(1, -1); n   for (int i = 1; i <= n; i++) n     if (cycle[i]) { n       dfs(i, 0); n     } n   for (int i = 1; i <= n; i++) cout << vis[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long perf[100]; n struct BalancedBinaryForest { n   int n; n   vector<bool> isblack; n   vector<int> blackheight; n   vector<int> lft, rgt, par; n   void init(int _n) { n     n = _n; n     isblack = vector<bool>(n, true); n     blackheight = vector<int>(n, 1); n     lft = rgt = par = vector<int>(n, -1); n   } n   pair<int, int> split(int a) { n     ++perf[0]; n     int l = lft[a], r = rgt[a]; n     lft[a] = rgt[a] = -1; n     if (l != -1) par[l] = -1; n     if (r != -1) par[r] = -1; n     if (l != -1 && !isblack[l]) isblack[l] = true, ++blackheight[l]; n     if (r != -1 && !isblack[r]) isblack[r] = true, ++blackheight[r]; n     while (par[a] != -1) { n       ++perf[1]; n       int p = par[a], pl = lft[p], pr = rgt[p]; n       lft[p] = rgt[p] = -1; n       par[a] = par[p]; n       par[p] = -1; n       if (par[a] != -1) (lft[par[a]] == p ? lft[par[a]] : rgt[par[a]]) = a; n       if (pl != a && pl != -1) par[pl] = -1; n       if (pr != a && pr != -1) par[pr] = -1; n       if (pl != a && pl != -1 && !isblack[pl]) n         isblack[pl] = true, ++blackheight[pl]; n       if (pr != a && pr != -1 && !isblack[pr]) n         isblack[pr] = true, ++blackheight[pr]; n       if (a == pl) r = join3(r, p, pr); n       if (a == pr) l = join3(pl, p, l); n     } n     isblack[a] = true; n     blackheight[a] = 1; n     return make_pair(l, r); n   } n   int rotatelft(int a) { n     ++perf[2]; n     assert(par[a] == -1); n     int c = rgt[a], d = lft[c]; n     rgt[a] = d; n     if (d != -1) par[d] = a; n     lft[c] = a; n     par[a] = c; n     par[c] = -1; n     return c; n   } n   int rotatergt(int c) { n     ++perf[3]; n     assert(par[c] == -1); n     int a = lft[c], d = rgt[a]; n     lft[c] = d; n     if (d != -1) par[d] = c; n     rgt[a] = c; n     par[c] = a; n     par[a] = -1; n     return a; n   } n   int height(int x) { return x == -1 ? 0 : blackheight[x]; } n   int joinlft(int a, int x, int b) { n     ++perf[4]; n     if (height(a) == height(b) && (a == -1 || isblack[a])) { n       isblack[x] = false; n       blackheight[x] = height(a); n       lft[x] = a; n       rgt[x] = b; n       if (a != -1) par[a] = x; n       if (b != -1) par[b] = x; n       return x; n     } n     int r = rgt[a]; n     if (r != -1) par[r] = -1; n     rgt[a] = joinlft(r, x, b); n     par[rgt[a]] = a; n     if (!isblack[a] && !isblack[rgt[a]]) { n       isblack[a] = true; n       ++blackheight[a]; n       return a; n     } n     if (isblack[a] && isblack[rgt[a]] && n         blackheight[a] == blackheight[rgt[a]]) { n       assert(!isblack[rgt[rgt[a]]]); n       a = rotatelft(a); n       isblack[a] = false; n       isblack[rgt[a]] = true; n       ++blackheight[rgt[a]]; n       return a; n     } n     return a; n   } n   int joinrgt(int a, int x, int b) { n     ++perf[5]; n     if (height(a) == height(b) && (b == -1 || isblack[b])) { n       isblack[x] = false; n       blackheight[x] = height(a); n       lft[x] = a; n       rgt[x] = b; n       if (a != -1) par[a] = x; n       if (b != -1) par[b] = x; n       return x; n     } n     int l = lft[b]; n     if (l != -1) par[l] = -1; n     lft[b] = joinrgt(a, x, l); n     par[lft[b]] = b; n     if (!isblack[b] && !isblack[lft[b]]) { n       isblack[b] = true; n       ++blackheight[b]; n       return b; n     } n     if (isblack[b] && isblack[lft[b]] && n         blackheight[b] == blackheight[lft[b]]) { n       assert(!isblack[lft[lft[b]]]); n       b = rotatergt(b); n       isblack[b] = false; n       isblack[lft[b]] = true; n       ++blackheight[lft[b]]; n       return b; n     } n     return b; n   } n   int join3(int a, int x, int b) { n     ++perf[6]; n     int ret = height(a) >= height(b) ? joinlft(a, x, b) : joinrgt(a, x, b); n     if (!isblack[ret]) isblack[ret] = true, ++blackheight[ret]; n     return ret; n   } n   int join2(int a, int b) { n     assert(a == -1 && b == -1 || a != b); n     ++perf[7]; n     if (a == -1) n       return b; n     else if (b == -1) n       return a; n     int c = lft[b], d = rgt[b]; n     lft[b] = rgt[b] = -1; n     if (c != -1) par[c] = -1; n     if (d != -1) par[d] = -1; n     if (c != -1 && !isblack[c]) isblack[c] = true, ++blackheight[c]; n     if (d != -1 && !isblack[d]) isblack[d] = true, ++blackheight[d]; n     return join3(join2(a, c), b, d); n   } n   int merge(int a, int b) { n     ++perf[8]; n     int ret = join2(getroot(a), getroot(b)); n     perf[11] = max(perf[11], (long long)height(ret)); n     return ret; n   } n   int getroot(int a) { n     ++perf[9]; n     while (a != -1 && par[a] != -1) ++perf[10], a = par[a]; n     return a; n   } n   void myassert(bool cond, const char *s, int who) { n     if (cond) return; n     printf( fail: %s for %d n , s, who + 1); n     assert(cond); n   } n   void verify() { n     for (int i = (0); i < (n); ++i) n       if (par[i] != -1) n         myassert(lft[par[i]] == i || rgt[par[i]] == i, n                   parent with incorrect child , i); n     for (int i = (0); i < (n); ++i) n       if (lft[i] != -1) myassert(par[lft[i]] == i,  lft incorrect parent , i); n     for (int i = (0); i < (n); ++i) n       if (rgt[i] != -1) myassert(par[rgt[i]] == i,  rgt incorrect parent , i); n     for (int i = (0); i < (n); ++i) n       if (par[i] != -1 && !isblack[i]) n         myassert(isblack[par[i]],  red with red parent , i); n     for (int i = (0); i < (n); ++i) n       myassert(height(lft[i]) == height(rgt[i]),  diff heights , i); n     for (int i = (0); i < (n); ++i) n       myassert(height(i) == height(lft[i]) + (isblack[i] ? 1 : 0), n                 incorrect height , i); n   } n }; n struct ForestDynamicConnectivity { n   int n; n   BalancedBinaryForest bbf; n   queue<int> pool; n   void init(int _n) { n     n = _n; n     bbf.init(n + 2 * (n - 1)); n     for (int i = (n); i < (bbf.n); ++i) pool.push(i); n   } n   int getnode() { n     assert(!pool.empty()); n     int ret = pool.front(); n     pool.pop(); n     return ret; n   } n   void releasenode(int x) { pool.push(x); } n   void reroot(int a) { n     int b, c; n     tie(b, c) = bbf.split(a); n     bbf.merge(a, bbf.merge(c, b)); n   } n   pair<int, int> addedge(int a, int b) { n     reroot(a); n     reroot(b); n     int ab = getnode(), ba = getnode(); n     bbf.merge(bbf.merge(a, ab), bbf.merge(b, ba)); n     return make_pair(ab, ba); n   } n   void remedge(pair<int, int> e) { n     int a, b, c; n     tie(a, b) = bbf.split(e.first); n     if (bbf.getroot(a) == bbf.getroot(e.second)) { n       c = b; n       tie(a, b) = bbf.split(e.second); n     } else { n       tie(b, c) = bbf.split(e.second); n     } n     releasenode(e.first), releasenode(e.second); n     bbf.merge(a, c); n   } n   int getrepresentative(int a) { return bbf.getroot(a); } n }; n struct SegTreeMinCounter { n   int n; n   vector<int> smn, smncnt, slazy; n   void sapply(int x, int by) { n     smn[x] += by; n     slazy[x] += by; n   } n   void spush(int x) { n     if (slazy[x] != 0) n       sapply(2 * x + 1, slazy[x]), sapply(2 * x + 2, slazy[x]), slazy[x] = 0; n   } n   void spull(int x) { n     smn[x] = min(smn[2 * x + 1], smn[2 * x + 2]); n     smncnt[x] = (smn[2 * x + 1] == smn[x] ? smncnt[2 * x + 1] : 0) + n                 (smn[2 * x + 2] == smn[x] ? smncnt[2 * x + 2] : 0); n   } n   void sinit(int x, int l, int r) { n     slazy[x] = 0; n     if (l == r) { n       smn[x] = 0, smncnt[x] = 1; n     } else { n       int m = l + (r - l) / 2; n       sinit(2 * x + 1, l, m); n       sinit(2 * x + 2, m + 1, r); n       spull(x); n     } n   } n   void smod(int x, int l, int r, int L, int R, int BY) { n     if (L <= l && r <= R) { n       sapply(x, BY); n     } else { n       int m = l + (r - l) / 2; n       spush(x); n       if (L <= m) smod(2 * x + 1, l, m, L, R, BY); n       if (m + 1 <= R) smod(2 * x + 2, m + 1, r, L, R, BY); n       spull(x); n     } n   } n   int sget(int x, int l, int r, int L, int R, int VAL) { n     if (L <= l && r <= R) { n       assert(smn[x] >= VAL); n       return smn[x] == VAL ? smncnt[x] : 0; n     } else { n       int m = l + (r - l) / 2; n       spush(x); n       int ret = 0; n       if (L <= m) ret += sget(2 * x + 1, l, m, L, R, VAL); n       if (m + 1 <= R) ret += sget(2 * x + 2, m + 1, r, L, R, VAL); n       return ret; n     } n   } n   void init(int _n) { n     n = _n; n     smn = smncnt = slazy = vector<int>(4 * n); n     sinit(0, 0, n - 1); n   } n   void mod(int l, int r, int by) { smod(0, 0, n - 1, l, r, by); } n   int get(int l, int r, int val) { return sget(0, 0, n - 1, l, r, val); } n }; n const int MAXH = 1000; n const int MAXW = 1000; n const int MAXN = 200000; n const int DX[] = {-1, 0, +1, 0}, DY[] = {0, +1, 0, -1}; n int h, w, n; n int g[MAXH][MAXW]; n pair<int, int> pos[MAXN]; n ForestDynamicConnectivity conn; n vector<pair<int, int>> connedges[MAXN]; n SegTreeMinCounter segtree; n long long solve() { n   for (int x = (0); x < (h); ++x) n     for (int y = (0); y < (w); ++y) pos[g[x][y]] = make_pair(x, y); n   conn.init(n); n   segtree.init(n); n   long long ret = 0; n   int l = 0; n   for (int r = (0); r < (n); ++r) { n     int rx = pos[r].first, ry = pos[r].second; n     while (true) { n       vector<int> adj; n       for (int k = (0); k < (4); ++k) { n         int nx = rx + DX[k], ny = ry + DY[k]; n         if (nx < 0 || nx >= h || ny < 0 || ny >= w || g[nx][ny] < l || n             g[nx][ny] > r) n           continue; n         adj.push_back(conn.getrepresentative(g[nx][ny])); n       } n       sort(adj.begin(), adj.end()); n       bool duplicate = false; n       for (int i = (1); i < (((int)(adj).size())); ++i) n         if (adj[i - 1] == adj[i]) duplicate = true; n       if (!duplicate) break; n       for (int i = (0); i < (((int)(connedges[l]).size())); ++i) n         conn.remedge(connedges[l][i]); n       ++l; n     } n     segtree.mod(0, r, +1); n     for (int k = (0); k < (4); ++k) { n       int nx = rx + DX[k], ny = ry + DY[k]; n       if (nx < 0 || nx >= h || ny < 0 || ny >= w || g[nx][ny] < l || n           g[nx][ny] > r) n         continue; n       connedges[g[nx][ny]].push_back(conn.addedge(g[nx][ny], r)); n       segtree.mod(0, g[nx][ny], -1); n     } n     int cur = segtree.get(l, r, 1); n     ret += cur; n   } n   return ret; n } n void run() { n   scanf( %d%d , &h, &w); n   n = h * w; n   for (int x = (0); x < (h); ++x) n     for (int y = (0); y < (w); ++y) scanf( %d , &g[x][y]), --g[x][y]; n   printf( %lld n , solve()); n } n void stress() { n   std::mt19937 rnd(1237891); n   for (int rep = (0); rep < (1); ++rep) { n     h = 992, w = 191, n = h * w; n     memset(g, -1, sizeof(g)); n     int at = 0; n     for (int xx = (0); xx < (h); ++xx) { n       int x = (2 * xx) % h + (2 * xx >= h ? 1 : 0); n       for (int y = (0); y < (w); ++y) g[x][y] = at++; n     } n     for (int x = (0); x < (h); ++x) n       for (int y = (0); y < (w); ++y) assert(g[x][y] != -1); n     long long have = solve(); n     printf( %lld n , have); n   } n   for (int i = (0); i <= (11); ++i) printf( perf[%d]=%lld n , i, perf[i]); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long a, b; n   cin >> a >> b; n   if (b < a || (b - a) % 2) n     cout << -1 << endl; n   else if (b == a && a == 0) n     cout << 0 << endl; n   else if (b == a) n     cout << 1 << endl << a; n   else { n     long long x = 0, cnt = 1, pp = b - a; n     while (cnt <= pp) cnt *= 2; n     cnt /= 2; n     while (true) { n       if (cnt * 2 <= pp) { n         x += cnt; n         pp -= cnt * 2; n       } n       cnt /= 2; n       if (pp == 0) break; n     } n     long long tt = a ^ x; n     if (tt == a + x) n       cout << 2 << endl << a + x <<     << x << endl; n     else n       cout << 3 << endl << a <<     << x <<     << x << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double PI = acos(-1.0); n const double eps = 1e-6; n const int MAX = 1e5 + 10; n const long long mod = 1e9 + 7; n struct node { n   long long l, r, mid, sum, tag; n   node() {} n   node(long long a, long long b) { n     l = a; n     r = b; n     sum = 0; n     tag = 0; n     mid = (l + r) >> 1; n   } n } tree[4 * MAX][15]; n long long c[MAX][15]; n void change(long long id, long long son) { n   long long i, j, s[12], tag[12]; n   memset((s), (0), sizeof(s)); n   for (i = 0; i <= 9; i++) { n     tag[i] = tree[son][i].tag; n     s[i] = tree[son][i].sum; n   } n   for (i = 0; i <= 9; i++) { n     if (tree[id][i].tag != i) { n       for (j = 0; j <= 9; j++) { n         if (tree[son][j].tag == i) tag[j] = tree[id][i].tag; n       } n       s[tree[id][i].tag] += tree[son][i].sum; n       s[i] -= tree[son][i].sum; n     } n   } n   for (i = 0; i <= 9; i++) { n     tree[son][i].tag = tag[i]; n     tree[son][i].sum = s[i]; n   } n } n void pushdown(long long id) { n   long long i; n   change(id, id << 1); n   change(id, id << 1 | 1); n   for (i = 0; i <= 9; i++) { n     tree[id][i].tag = i; n   } n } n void pushup(long long id) { n   long long i; n   for (i = 0; i <= 9; i++) { n     tree[id][i].sum = tree[id << 1][i].sum + tree[id << 1 | 1][i].sum; n   } n } n void build(long long l, long long r, long long id) { n   long long mid, i; n   for (i = 0; i <= 9; i++) { n     tree[id][i] = node(l, r); n     tree[id][i].tag = i; n     if (l == r) tree[id][i].sum = c[l][i]; n   } n   if (l == r) return; n   mid = (l + r) >> 1; n   build(l, mid, id << 1); n   build(mid + 1, r, id << 1 | 1); n   pushup(id); n } n void update(long long l, long long r, long long id, long long x, long long y) { n   long long mid, i, s = 0; n   if (l <= tree[id][x].l && r >= tree[id][x].r) { n     for (i = 0; i <= 9; i++) { n       if (tree[id][i].tag == x) { n         tree[id][i].tag = y; n       } n     } n     tree[id][y].sum += tree[id][x].sum; n     tree[id][x].sum = 0; n     return; n   } n   pushdown(id); n   mid = tree[id][x].mid; n   if (r <= mid) n     update(l, r, id << 1, x, y); n   else if (l >= mid + 1) n     update(l, r, id << 1 | 1, x, y); n   else { n     update(l, mid, id << 1, x, y); n     update(mid + 1, r, id << 1 | 1, x, y); n   } n   pushup(id); n } n long long query(long long l, long long r, long long id, long long x) { n   long long mid, s = 0, i; n   if (l == tree[id][x].l && r == tree[id][x].r) { n     for (i = 0; i <= 9; i++) { n       s += tree[id][i].sum * i; n     } n     return s; n   } n   pushdown(id); n   mid = tree[id][x].mid; n   if (r <= mid) n     return query(l, r, id << 1, x); n   else if (l >= mid + 1) n     return query(l, r, id << 1 | 1, x); n   else n     return query(l, mid, id << 1, x) + query(mid + 1, r, id << 1 | 1, x); n   pushup(id); n } n int main() { n   long long i, n, q, x, op, l, r, a, b, temp; n   while (~scanf( %lld%lld , &n, &q)) { n     memset((c), (0), sizeof(c)); n     for (i = 1; i <= n; i++) { n       scanf( %lld , &x); n       temp = 1; n       while (x) { n         long long now = x % 10; n         c[i][now] += temp; n         x /= 10; n         temp *= 10; n       } n     } n     build(1, n, 1); n     while (q--) { n       scanf( %lld%lld%lld , &op, &l, &r); n       if (op == 1) { n         scanf( %lld%lld , &a, &b); n         if (a != b) update(l, r, 1, a, b); n       } else { n         long long ans = 0; n         ans = query(l, r, 1, 1); n         printf( %lld n , ans); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int MOD2 = 1007681537; n const int INF = (int)1e9; n const long long LINF = (long long)1e18; n const long double PI = acos((long double)-1); n const long double EPS = 1e-9; n inline long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n template <class T> n inline int chkmin(T& a, const T& val) { n   return val < a ? a = val, 1 : 0; n } n template <class T> n inline int chkmax(T& a, const T& val) { n   return a < val ? a = val, 1 : 0; n } n inline long long isqrt(long long k) { n   long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n inline void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } n inline int sign(long double x, long double y) { return sign(x - y); } n long long ff(long long n, long long k) { n   return min(k + k, k + (n - k) * (n - k - 1) / 2); n } n void solve() { n   int test; n   cin >> test; n   while (test--) { n     long long n; n     cin >> n; n     if (n <= 4) { n       cout << n - 1 <<   n ; n       continue; n     } n     long long lo = 1, hi = n - 3; n     while (lo < hi) { n       long long mi = lo + hi >> 1; n       if (ff(n, mi) > ff(n, mi + 1)) { n         hi = mi; n       } else { n         lo = mi + 1; n       } n     } n     cout << max(n - 1, ff(n, lo + hi >> 1)) <<   n ; n   } n } n int main() { n   int JUDGE_ONLINE = 1; n   if (fopen( in.txt ,  r )) { n     JUDGE_ONLINE = 0; n     assert(freopen( in.txt ,  r , stdin)); n   } else { n     ios_base::sync_with_stdio(0), cin.tie(0); n   } n   solve(); n   if (!JUDGE_ONLINE) { n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   ll n, m; n   cin >> n >> m; n   std::vector<int> a(n + 2); n   n = n + 2; n   a[0] = 0; n   a[n - 1] = m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   ll v[n][2]; n   v[n - 1][0] = 0; n   v[n - 1][1] = 0; n   for (int i = n - 2; i >= 0; i--) { n     v[i][0] = v[i + 1][1]; n     v[i][1] = (a[i + 1] - a[i]) + v[i + 1][0]; n   } n   ll ans = v[0][1]; n   ll cur = 0; n   for (int i = 0; i < n - 1; i++) { n     int f; n     if (i % 2 == 0) n       f = 1; n     else n       f = 0; n     if (f == 1) { n       if (a[i + 1] - a[i] > 1) { n         ans = max(ans, cur + a[i + 1] - 1 - a[i] + v[i + 1][1]); n       } n       cur += a[i + 1] - a[i]; n     } else { n       if (a[i + 1] - a[i] > 1) { n         ans = max(ans, cur + a[i + 1] - (a[i] + 1) + v[i + 1][0]); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t[3000000]; n long long t1[3000000]; n long long dp[10 * 10 + 5][10 + 5]; n vector<long long> v; n long long n, m, k, l, r, nb, q, p; n map<long long, int> mapp; n void debug(long long* tab, int len) { n   cout <<  debug ::::  n ; n   for (int i = 0; i < len; i++) cout << tab[i] <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> nb; n   while (nb--) { n     cin >> n; n     for (int i = 0; i < 4 * n; i++) cin >> t[i]; n     sort(t, t + 4 * n); n     bool test = true; n     for (int i = 0; i < 2 * n; i++) { n       if (t[2 * i + 1] != t[2 * i]) test = false; n     } n     for (int i = 0; i < 2 * n; i++) t1[i] = t[2 * i]; n     long long k = t1[0] * t1[2 * n - 1]; n     for (int i = 0; i < n; i++) { n       if (t1[i] * t1[2 * n - 1 - i] != k) test = false; n     } n     if (test) n       cout <<  YES ; n     else n       cout <<  NO ; n     if (nb) cout <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n template <typename T> n T min(T x, T y) { n   return x < y ? x : y; n } n template <typename T> n T max(T x, T y) { n   return x > y ? x : y; n }; n const int INF = 2000000005; n const long long mod = 1000000007; n const long long MAXN = 1000005; n const double eps = 1e-6; n int sgn(double x) { return x < -eps ? -1 : x > eps; } n double SQ(double x) { return x * x; } n struct point { n   double x; n   double y; n   point(double X = 0, double Y = 0) { x = X, y = Y; } n   point operator-(point t) { return {x - t.x, y - t.y}; } n   double operator*(point t) { return x * t.y - y * t.x; } n   double operator%(point t) { return x * t.x + y * t.y; } n   double norm() { return sqrt(SQ(x) + SQ(y)); } n }; n struct polygon { n   int n; n   std::vector<point> V; n   double S; n   void read() { n     double x, y; n     V.clear(); n     scanf( %d , &n); n     for (int i = 0; i < n; i++) { n       scanf( %lf %lf , &x, &y); n       V.push_back({x, y}); n     } n     S = 0; n     for (int i = 0; i < n - 1; i++) S += V[i] * V[i + 1]; n     S += V[n - 1] * V[0]; n     S = 0.5 * fabs(S); n   } n   bool in(point p) { n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       int j = (i + 1) % n; n       point u = V[i], v = V[j]; n       if (sgn(p.y - u.y) == 0 || sgn(p.y - v.y) == 0) n         return in({p.x, p.y + 3 * eps}); n       if (sgn(u.y - v.y) <= 0) std::swap(u, v); n       if (sgn(p.y - u.y) > 0 || sgn(p.y - v.y) <= 0) continue; n       cnt += sgn((v - p) * (u - p)) > 0; n     } n     return cnt & 1; n   } n   double dis(point a, point b, point p) { n     point ab = b - a, pa = a - p, pb = b - p; n     if ((ab % pb) * (ab % pa) < 0) return fabs(pa * ab) / ab.norm(); n     return min(pa.norm(), pb.norm()); n   } n   double dis(double x, double y) { n     double t = 1e15; n     for (int i = 0; i < n; i++) { n       int j = (i + 1) % n; n       point u = V[i], v = V[j]; n       t = min(t, dis(u, v, {x, y})); n     } n     return t; n   } n   bool judge(double x, double y, double r) { n     if (dis(x, y) < r + eps) return true; n     return in({x, y}); n   } n }; n double W, H; n int N, Q; n polygon P[MAXN]; n int main() { n   scanf( %lf %lf %d , &W, &H, &N); n   for (int i = 1; i <= N; i++) P[i].read(); n   double x, y, r; n   scanf( %d , &Q); n   while (Q--) { n     scanf( %lf %lf %lf , &x, &y, &r); n     std::vector<int> q; n     double t = 0; n     for (int i = 1; i <= N; i++) n       if (P[i].judge(x, y, r)) q.push_back(i - 1), t += P[i].S; n     printf( %lf %d  , t, q.size()); n     for (int i = 0; i < q.size(); i++) printf( %d  , q[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using D = double; n using uint = unsigned int; n const int maxn = 1005; n int n; n int a[maxn]; n int cnt[maxn]; n int have[maxn]; n int was[maxn]; n int ct = 0; n inline bool canset(int x) { n   int wh = lower_bound(a, a + n, x) - a; n   if (wh >= n || a[wh] != x) return false; n   if (was[wh] == ct) { n     if (!have[wh]) return false; n     have[wh]--; n     return true; n   } else { n     have[wh] = cnt[wh] - 1; n     was[wh] = ct; n     return true; n   } n } n inline int getcnt(int x) { n   int wh = lower_bound(a, a + n, x) - a; n   return have[wh]; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   int last = 0; n   cnt[0] = 1; n   for (int i = 1; i < n; i++) { n     if (a[i] == a[i - 1]) n       cnt[last]++; n     else { n       last++; n       a[last] = a[i]; n       cnt[last] = 1; n     } n   } n   n = last + 1; n   int answer = 2; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       ct++; n       int f = a[i]; n       int s = a[j]; n       canset(f); n       if (!canset(s)) continue; n       int curans = 2; n       while (true) { n         if (!canset(f + s)) break; n         curans++; n         int t = f + s; n         f = s; n         s = t; n         if (f == 0 && s == 0) { n           curans += getcnt(0); n           break; n         } n       } n       answer = max(answer, curans); n     } n   } n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[1005]; n long long a[1005]; n long long ans; n long long b[1005][5]; n long long ma; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     cin >> s[i]; n   } n   for (long long i = 0; i < m; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < m; i++) { n     ma = 0; n     for (long long j = 1; j <= n; j++) { n       b[i][s[j][i] -  A ]++; n     } n     for (long long j = 0; j < 5; j++) { n       ma = max(ma, b[i][j]); n     } n     ans += ma * a[i]; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   unsigned long long sum{}; n   int cur{1}; n   for (int i{}; i < m; i++) { n     int x; n     cin >> x; n     if (cur < x) { n       sum += x - cur; n       cur = x; n     } else if (cur > x) { n       sum += n - cur + x; n       cur = x; n     } else n       continue; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long ab[11], y[11]; n long long val(string abv) { n   long long res = 0; n   for (int i = 0; i < (int)abv.size(); i++) n     res = 10 * res + ((long long)(abv[i] -  0 )); n   return res; n } n int main() { n   cin.sync_with_stdio(false); n   long long p10 = 10; n   long long nb = 1989; n   for (int i = 0; i < (int)10; i++) { n     ab[i] = nb % p10; n     y[i] = nb; n     nb += p10; n     p10 *= ((long long)10); n   } n   cin >> n; n   while (n--) { n     char c; n     string abv; n     for (int i = 0; i < (int)4; i++) cin >> c; n     cin >> abv; n     long long k = val(abv); n     int l = abv.size() - 1; n     if (ab[l] <= k) n       printf( %I64d n , y[l] + k - ab[l]); n     else n       printf( %I64d n , y[l + 1] - (ab[l] - k)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int N = 100010; n int a[N]; n vector<int> f[N]; n void go() { n   long long n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 2; i < N; i++) { n     for (int j = i; j < N; j += i) f[j].push_back(i); n   } n   int freq[N] = {0}; n   for (int i = 0; i < n; i++) { n     int m = 0; n     for (auto x : f[a[i]]) { n       m = max(m, freq[x] + 1); n     } n     freq[a[i]] = m; n     for (auto x : f[a[i]]) { n       freq[x] = m; n     } n   } n   int ans = 1; n   for (int i = 0; i < n; i++) { n     ans = max(freq[a[i]], ans); n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int t = 1; n   cout << fixed << setprecision(9); n   while (t--) { n     go(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int l1, r1, l2, r2, ans; n int min(int a, int b) { return a < b ? a : b; } n int max(int a, int b) { return a > b ? a : b; } n int check(int a) { n   if (a > ans) ans = a; n   return a; n } n int solve(int l1, int r1, int l2, int r2, int k) { n   if (l1 == l2 && r1 == r2) { n     if (r1 - l1 + 1 > ans) ans = r1 - l1 + 1; n     return r1 - l1 + 1; n   } n   if (r1 - l1 + 1 <= ans || r2 - l2 + 1 <= ans) return 0; n   if (k == 0) return 0; n   int mid = (1 << k); n   if (r1 < mid && r2 < mid) return check(solve(l1, r1, l2, r2, k - 1)); n   if (l1 > mid && l2 > mid) n     return check(solve(l1 - mid, r1 - mid, l2 - mid, r2 - mid, k - 1)); n   if (r1 < mid && l2 > mid) n     return check(solve(l1, r1, l2 - mid, r2 - mid, k - 1)); n   if (r2 < mid && l1 > mid) n     return check(solve(l1 - mid, r1 - mid, l2, r2, k - 1)); n   if (l1 <= mid && r1 >= mid) { n     if (r2 < mid) n       return check(max(solve(l1, mid - 1, l2, r2, k - 1), n                        solve(1, r1 - mid, l2, r2, k - 1))); n     if (l2 > mid) n       return check(max(solve(l1, mid - 1, l2 - mid, r2 - mid, k - 1), n                        solve(1, r1 - mid, l2 - mid, r2 - mid, k - 1))); n     int ret = min(r1, r2) - max(l1, l2) + 1; n     ret = max(ret, solve(l1, mid - 1, 1, r2 - mid, k - 1)); n     ret = max(ret, solve(1, r1 - mid, l2, mid - 1, k - 1)); n     return check(ret); n   } n   if (l2 <= mid && r2 >= mid) { n     if (r1 < mid) n       return check(max(solve(l1, r1, l2, mid - 1, k - 1), n                        solve(l1, r1, 1, r2 - mid, k - 1))); n     if (l1 > mid) n       return check(max(solve(l1 - mid, r1 - mid, l2, mid - 1, k - 1), n                        solve(l1 - mid, r1 - mid, 1, r2 - mid, k - 1))); n     int ret = min(r1, r2) - max(l1, l2) + 1; n     ret = max(ret, solve(l1, mid - 1, 1, r2 - mid, k - 1)); n     ret = max(ret, solve(1, r1 - mid, l2, mid - 1, k - 1)); n     return check(ret); n   } n } n int main() { n   cin >> l1 >> r1 >> l2 >> r2; n   cout << solve(l1, r1, l2, r2, 29) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char temp[11]; n   int n, m, k; n   cin >> n >> m >> k; n   int a[10][100], b[10][100], c[10][100]; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     for (int j = 0; j < m; j++) cin >> a[i][j] >> b[i][j] >> c[i][j]; n   } n   int maxi = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       if (i == j) continue; n       vector<pair<int, int> > diff(m); n       for (int t = 0; t < m; t++) diff[t] = make_pair(b[j][t] - a[i][t], t); n       sort(diff.rbegin(), diff.rend()); n       int kc = k, cur = 0; n       for (int t = 0; kc > 0 && t < m && diff[t].first > 0; t++) { n         cur += min(kc, c[i][diff[t].second]) * diff[t].first; n         kc -= min(kc, c[i][diff[t].second]); n       } n       maxi = max(maxi, cur); n     } n   cout << maxi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 100010; n int x0[maxn], x1[maxn], d[maxn], c[maxn]; n int main() { n   int n, m, y0, y1; n   cin >> n >> m >> y0 >> y1; n   for (int i = 0; i < n; ++i) { n     cin >> x0[i]; n   } n   memset(d, -1, sizeof(d)); n   for (int i = 0; i < m; ++i) { n     cin >> x1[i]; n   } n   int p = 0, k = 0, last = -1; n   for (int i = 0; i < n; ++i) { n     while (p < m && abs(x0[i] - x1[p]) > abs(x0[i] - x1[p + 1])) ++p; n     if (last < p || abs(x0[i - 1] - x1[p]) == abs(x0[i] - x1[p])) n       ++k, last = p; n     else if (p < m && abs(x0[i] - x1[p]) == abs(x0[i] - x1[p + 1])) n       ++k, last = ++p; n     else n       last = p; n   } n   cout << n - k << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n void precalc(); n clock_t start; n bool doing = true; n int main() { n   int t = 1; n   cout.sync_with_stdio(0); n   cin.tie(0); n   precalc(); n   cout.precision(10); n   cout << fixed; n   start = clock(); n   int testNum = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n void precalc() {} n int binpow(int q, int w, int mod) { n   if (!w) return 1; n   if (w & 1) return q * binpow(q, w - 1, mod) % mod; n   return binpow(q * q % mod, w / 2, mod); n } n int mod = 1000000007; n int gcd(int q, int w) { n   while (w) { n     q %= w; n     swap(q, w); n   } n   return q; n } n int n, m; n string s[1005]; n void solve() { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n   } n   vector<char> good(n - 1, false); n   int ans = 0; n   for (int i = 0; i < m; ++i) { n     bool need = false; n     for (int j = 0; j + 1 < n; ++j) { n       if (good[j]) { n         continue; n       } n       if (s[j][i] > s[j + 1][i]) { n         need = true; n       } n     } n     if (need) { n       ++ans; n     } else { n       for (int j = 0; j + 1 < n; ++j) { n         if (s[j][i] < s[j + 1][i]) { n           good[j] = true; n         } n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   double x = 0, ans = 0, p[100005] = {0}; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) { n     x = x * p[i] + p[i]; n     ans += 2 * x; n     ans -= p[i]; n   } n   cout << fixed << setprecision(10); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string char_to_str(char c) { n   string tem(1, c); n   return tem; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   string ans = s; n   for (long long i = 0; i < n; ++i) { n     long long d1 =  z  - s[i]; n     long long d2 = s[i] -  a ; n     if (k >= d1 && k >= d2) { n       if (d1 > d2) { n         ans[i] =  z ; n         k -= d1; n       } else { n         ans[i] =  a ; n         k -= d2; n       } n     } else { n       if (s[i] + k <=  z ) { n         ans[i] = s[i] + k; n         k = 0; n       } else { n         ans[i] = s[i] - k; n         k = 0; n       } n     } n   } n   if (k != 0) { n     cout <<  -1 ; n   } else { n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dx[8] = {0, 0, 1, -1, -1, -1, 1, 1}; n int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; n char s[111][111]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (s[i][j] >=  1  && s[i][j] <=  8 ) { n         int cnt = 0; n         for (int k = 0; k < 8; k++) { n           int tx = i + dx[k]; n           int ty = j + dy[k]; n           if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && s[tx][ty] ==  * ) n             cnt++; n         } n         if (cnt != s[i][j] -  0 ) return 0 * printf( NO n ); n       } else if (s[i][j] ==  . ) { n         for (int k = 0; k < 8; k++) { n           int tx = i + dx[k]; n           int ty = j + dy[k]; n           if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && s[tx][ty] ==  * ) n             return 0 * printf( NO n ); n         } n       } n     } n   puts( YES ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n long long n, u, v, w, res = 0, par[N]; n vector<pair<long long, long long> > a[N]; n long long root(long long x) { n   if (par[x] < 0) return x; n   return (par[x] = root(par[x])); n } n void Merge(long long u, long long v) { n   long long a = root(u), b = root(v); n   if (a == b) return; n   if (par[a] > par[b]) swap(a, b); n   par[a] += par[b], par[b] = a; n } n bool check(long long n) { n   while (n > 0) { n     long long a = n % 10; n     if (a != 4 && a != 7) return false; n     n /= 10; n   } n   return true; n } n void in() { n   cin >> n; n   for (long long i = 1; i <= n; i++) par[i] = -1; n   for (long long i = 1; i <= n - 1; i++) { n     cin >> u >> v >> w; n     if (!check(w)) Merge(u, v); n     a[u].push_back({v, w}), a[v].push_back({u, w}); n   } n } n void process() { n   for (long long i = 1; i <= n; i++) { n     if (par[i] < 0) { n       long long tmp = -par[i]; n       res += (tmp * (n - tmp - 1) * (n - tmp)); n     } n   } n   cout << res; n } n int main() { n   if (fopen( trau.inp ,  r )) n     freopen( trau.inp ,  r , stdin), freopen( trau.out ,  w , stdout); n   in(); n   process(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int i, j, s = 0, p = 0, u = 0; n     for (i = 1; i <= n / 2; i++) { n       s = s + i * i; n     } n     cout << s * 8 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int inf32 = 0x3f3f3f3f; n constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<long long> a(n); n   for (int u = 0; u < (n); ++u) cin >> a[u]; n   vector<vector<int> > adj(n); n   for (int i = 0; i < (m); ++i) { n     int u, v; n     cin >> u >> v, --u, --v; n     adj[u].push_back(v); n   } n   int k = 1; n   vector<int> g(n, 0), vis(n, 0); n   function<void(int)> dfs = [&](int u) { n     if (vis[u]) return; n     vis[u] = true; n     for (const int &v : adj[u]) dfs(v); n     vector<int> use(k, 0); n     for (const int &v : adj[u]) use[g[v]] = true; n     while (g[u] < k && use[g[u]]) ++g[u]; n     if (g[u] >= k) ++k; n   }; n   for (int u = 0; u < (n); ++u) dfs(u); n   vector<long long> f(k, 0); n   for (int u = 0; u < (n); ++u) f[g[u]] ^= a[u]; n   int mx = -1; n   for (int i = 0; i < (k); ++i) n     if (f[i]) mx = i; n   if (!~mx) return !(cout <<  LOSE n ); n   cout <<  WIN n ; n   for (int u = 0; u < (n); ++u) n     if (g[u] == mx && (a[u] ^ f[mx]) < a[u]) { n       a[u] ^= f[mx], f[mx] = 0; n       for (const int &v : adj[u]) a[v] ^= f[g[v]], f[g[v]] = 0; n       break; n     } n   for (int u = 0; u < (n); ++u) cout << a[u] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005], b; n int n, k; n pair<long long, int> pvec[100005]; n int solve() { n   for (int i = 0; i < n - 1; i++) pvec[i] = make_pair(a[i], i + 1); n   sort(pvec, pvec + n); n   reverse(pvec, pvec + n); n   long long total = 0; n   int ans = n; n   for (int i = 0; i < k; i++) { n     total += pvec[i].first; n     ans = min(ans, pvec[i].second); n   } n   if (total > b) { n     if (k > 1) b = b - (total - pvec[k - 1].first); n     for (int j = 0; j < n; j++) { n       if (a[j] > b) return j + 1; n     } n   } else n     return n; n   return 0; n } n int main() { n   cin >> n >> k; n   cin >> b; n   for (int i = 0; i < n; i++) cin >> a[i]; n   cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int B = 500; n using namespace std; n const long long inf = 1e9 + 12; n long long mod = 1e9 + 7; n const long long mxn = 4e5 + 5; n const int N = 10000005; n const double pi = 3.14159; n int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; n int dy[] = {1, 0, -1, 0, -1, 1, 1, -1}; n long long m, n, k, y, z; n vector<int> g[mxn]; n int timer = 0; n int st[mxn], ed[mxn], d[mxn], tree[mxn << 4], lazy[mxn << 4]; n bool lit[mxn]; n void dfs(int v, int p) { n   timer++; n   st[v] = timer; n   for (auto u : g[v]) { n     if (u == p) continue; n     dfs(u, v); n   } n   timer++; n   ed[v] = timer; n } n void push_up(int node, int l, int r) { n   int mid = (l + r) >> 1; n   tree[node] = (lazy[2 * node] % 2 == 0 ? (tree[2 * node]) n                                         : (mid - l + 1 - tree[2 * node])) + n                (lazy[2 * node + 1] % 2 == 0 ? (tree[2 * node + 1]) n                                             : (r - mid - tree[2 * node + 1])); n } n void upd(int node, int l, int r, int ql, int qr, int t) { n   if (ql > r || qr < l) return; n   if (l >= ql && r <= qr) { n     if (t) n       lazy[node]++; n     else n       tree[node] = r - l + 1; n     return; n   } n   int mid = (l + r) >> 1; n   upd(2 * node, l, mid, ql, qr, t), upd(2 * node + 1, mid + 1, r, ql, qr, t); n   push_up(node, l, r); n } n int qry(int node, int l, int r, int ql, int qr, int val) { n   if (ql > r || qr < l) return 0; n   if (l >= ql && r <= qr) { n     val += lazy[node]; n     if (val % 2 == 0) n       return tree[node]; n     else n       return r - l + 1 - tree[node]; n   } n   int mid = (l + r) >> 1; n   return qry(2 * node, l, mid, ql, qr, val + lazy[node]) + n          qry(2 * node + 1, mid + 1, r, ql, qr, val + lazy[node]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int x; n     cin >> x; n     g[x].push_back(i), g[i].push_back(x); n   } n   dfs(1, 0); n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     lit[i] = x; n     if (x) upd(1, 1, 2 * n, st[i], st[i], 0), upd(1, 1, 2 * n, ed[i], ed[i], 0); n   } n   cin >> k; n   int t; n   while (k--) { n     string s; n     cin >> s >> t; n     if (s[0] ==  g ) { n       cout << qry(1, 1, 2 * n, st[t], ed[t], 0) / 2 << endl; n     } else { n       upd(1, 1, 2 * n, st[t], ed[t], 1); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char block[][3][4] = {{ ..# ,  ### ,  ..# }, n                       { .#. ,  .#. ,  ### }, n                       { #.. ,  ### ,  #.. }, n                       { ### ,  .#. ,  .#. }}; n int cell[10][10], anscell[10][10]; n int bcnt, ans; n bool isPlacable(int i, int j, int k) { n   for (int p = 0; p < 3; ++p) n     for (int q = 0; q < 3; ++q) { n       if (cell[i + p][j + q] != -1 && block[k][p][q] ==  # ) return false; n     } n   return true; n } n void setBlock(int i, int j, int k) { n   for (int p = 0; p < 3; ++p) n     for (int q = 0; q < 3; ++q) { n       if (block[k][p][q] ==  # ) cell[i + p][j + q] = bcnt; n     } n   ++bcnt; n } n void removeBlock(int i, int j, int k) { n   for (int p = 0; p < 3; ++p) n     for (int q = 0; q < 3; ++q) { n       if (block[k][p][q] ==  # ) cell[i + p][j + q] = -1; n     } n   --bcnt; n } n void go(int i, int j) { n   if (j >= m - 2) { n     go(i + 1, 0); n     return; n   } n   if (i == n - 2) { n     if (ans < bcnt) { n       ans = bcnt; n       memcpy(anscell, cell, sizeof(cell)); n     } n     return; n   } n   if (bcnt + (int)((n * m - (i * m + j)) / 6.2) <= ans) { n     return; n   } n   for (int k = 0; k < 4; ++k) { n     if (isPlacable(i, j, k)) { n       setBlock(i, j, k); n       go(i, j + 1); n       removeBlock(i, j, k); n     } n   } n   go(i, j + 1); n } n int main() { n   memset(cell, -1, sizeof(cell)); n   memset(anscell, -1, sizeof(anscell)); n   scanf( %d %d , &n, &m); n   if (n >= 3 && m >= 3) go(0, 0); n   printf( %d n , ans); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       putchar(anscell[i][j] == -1 ?  .  : ( A  + anscell[i][j])); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using uint = unsigned int; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <typename T1, typename T2> n ostream &operator<<(ostream &out, const pair<T1, T2> &item) { n   out <<  (  << item.first <<  ,   << item.second <<  ) ; n   return out; n } n template <typename T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   for (const auto &item : v) out << item <<    ; n   return out; n } n const int NMAX = 200010; n struct lol { n   int lv; n   int v0; n   int v1; n   int v2; n   int v3; n   int v4; n   lol(int v = 0) { n     lv = v; n     v0 = v; n     v1 = v; n     v2 = -v; n     v3 = -v; n     v4 = 0; n   } n   friend ostream &operator<<(ostream &out, const lol &a) { n     return out <<  (  << a.lv <<     << a.v0 <<     << a.v1 <<     << a.v2 n                <<     << a.v3 <<     << a.v4 <<  ) ; n   } n }; n int m; n string s; n lol st[NMAX << 2]; n int val(char ch) { return (ch ==  (  ? 1 : -1); } n lol combine(const lol &a, const lol &b) { n   lol ret; n   ret.lv = a.lv + b.lv; n   ret.v0 = min(a.v0, a.lv + b.v0); n   ret.v1 = max(a.v1, a.lv + b.v1); n   ret.v2 = max(max(a.v2, -a.lv + b.v2), a.v1 - 2 * (a.lv + b.v0)); n   ret.v3 = max(max(a.v3, -a.lv + b.v3), -2 * a.v0 + (a.lv + b.v1)); n   ret.v4 = max(max(a.v4, b.v4), max(a.v1 - a.lv + b.v3, a.v2 + a.lv + b.v1)); n   return ret; n } n void build(int node, int l, int r) { n   if (l == r) return void(st[node] = lol(val(s[l]))); n   int mid = (l + r) / 2; n   build(2 * node, l, mid); n   build(2 * node + 1, mid + 1, r); n   st[node] = combine(st[2 * node], st[2 * node + 1]); n } n void update(int node, int l, int r, int p) { n   if (l == r) return void(st[node] = lol(val(s[p]))); n   int mid = (l + r) / 2; n   if (p <= mid) n     update(2 * node, l, mid, p); n   else n     update(2 * node + 1, mid + 1, r, p); n   st[node] = combine(st[2 * node], st[2 * node + 1]); n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, q; n   cin >> n >> q; n   m = 2 * n - 2; n   cin >> s; n   build(1, 0, m - 1); n   cout << st[1].v4 <<   n ; n   while (q--) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     swap(s[a], s[b]); n     update(1, 0, m - 1, a); n     update(1, 0, m - 1, b); n     cout << st[1].v4 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string a, b, c; n     cin >> a >> b >> c; n     int n = a.length(); n     int count = 0; n     for (int i = 0; i < n; i++) { n       if (c[i] == a[i] || c[i] == b[i]) n         continue; n       else { n         count++; n         break; n       } n     } n     if (count) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, n[3], t[3]; n   while (scanf( %d , &k) == 1) { n     for (int i = 0; i < 3; i++) scanf( %d , &n[i]); n     for (int i = 0; i < 3; i++) scanf( %d , &t[i]); n     deque<int> q[3]; n     for (int i = 0; i < 3; i++) q[i].clear(); n     int p = 0; n     while (k--) { n       for (int i = 0; i < 3; i++) n         if (q[i].size() == n[i]) { n           p = q[i].front() + t[i]; n           for (int j = 0; j < 3; j++) { n             while (q[j].size() && q[j].front() + t[j] <= p) q[j].pop_front(); n           } n           i = -1; n         } n       for (int i = 0; i < 3; i++) q[i].push_back(p); n     } n     printf( %d n , p + t[0] + t[1] + t[2]); n   } n   return 0; n } n


#include<bits/stdc++.h> n   n using namespace std; n   n typedef long long ll; n typedef pair<int, int> pii; n typedef long double ld; n   n #define F first n #define S second n   n const int MOD = 998244353;// XXX n const int C = 26;// XXX n   n void add(int &x, int y){ n  x += y; n  while (x >= MOD) x -= MOD; n  while (x < 0) x += MOD; n } n   n int fix(int x){ n  while (x >= MOD) x -= MOD; n  while (x < 0) x += MOD; n  return x; n } n   n int pw(int a, int b){ n  int ret = 1; n  while (b){ n   if (b & 1) n    ret = 1ll*ret*a%MOD; n   b >>= 1; n   a = 1ll*a*a%MOD; n  } n  return ret; n } n  n int mul(int a, int b) { n  return (ll) a * b % MOD; n } n  n const int MAXN = 405; n const int MAXE = 605; n  n int n, m, d[MAXN][MAXN], ans[MAXN][MAXN]; n pii edges[MAXE]; n  n int onPath[MAXN], foundLen[MAXN], numPar[MAXN]; n  n void solve() { n  cin >> n >> m; n  memset(d, 63, sizeof(d)); n  n  for(int i = 0; i < m; i++) { n   int a, b; cin >> a >> b; --a; --b; n   edges[i] = {a, b}; n   d[a][b] = d[b][a] = 1; n  } n  for(int i = 0; i < n; i++) n   d[i][i] = 0; n   n  for(int k = 0; k < n; k++) n   for(int i = 0; i < n; i++) n    for(int j = 0; j < n; j++) n     d[i][j] = min(d[i][j], d[i][k] + d[k][j]); n   n  for(int u = 0; u < n; u++) { n   for(int v = 0; v < n; v++) { n    bool ok = 1; n    int cnt = 1; n    memset(onPath, 0, sizeof(onPath)); n    memset(numPar, 0, sizeof(numPar)); n    memset(foundLen, 0, sizeof(foundLen)); n  n    for(int w = 0; w < n; w++) { n     if(d[u][w] + d[v][w] == d[u][v]) { n      onPath[w] = 1; n      if(foundLen[d[u][w]])  n       ok = 0; n      foundLen[d[u][w]] = 1; n     } n    } n    for(int len = 0; len <= d[u][v]; len++)  n     if(!foundLen[len])  n      ok = 0; n  n    for(int e = 0; e < m; e++) { n     for(int rot = 0; rot < 2; rot++) { n      auto &[a, b] = edges[e]; n      if(!onPath[a]) { n       if(d[u][a] == d[u][b] + 1 && d[v][a] == d[v][b] + 1) { n        ++numPar[a]; n       } n      } n      swap(a, b); n     } n    } n    for(int w = 0; w < n; w++)  n     if(!onPath[w]) n      cnt = mul(cnt, numPar[w]); n     n    if(ok)  n     ans[u][v] = cnt; n   } n  } n   n  for(int u = 0; u < n; u++) { n   for(int v = 0; v < n; v++) { n    cout << ans[u][v] <<    ; n   } n   cout <<   n ; n  } n } n  n int main(){ n  ios::sync_with_stdio(false); n  cin.tie(0); n  cout.tie(0); n  // cout << fixed << setprecision(6); n   n  int te = 1;  n  // cin >> te; n  for (int w = 1; w <= te; w++){ n   // cout <<  Case #  << w <<  :  ; n   solve(); n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n mt19937 rnf(2106); n const int N = 100005; n int n, m, k; n vector<int> a[N]; n vector<int> v[2]; n void dfs0(int x, int p, int z) { n   v[z].push_back(x); n   for (int i = 0; i < a[x].size(); ++i) { n     int h = a[x][i]; n     if (h == p) continue; n     dfs0(h, x, (z ^ 1)); n   } n } n bool c[N]; n int dfs(int x, int p) { n   c[x] = true; n   for (int i = 0; i < a[x].size(); ++i) { n     int h = a[x][i]; n     if (h == p) continue; n     if (!c[h]) { n       int u = dfs(h, x); n       if (u) return u; n     } else n       return x; n   } n   return 0; n } n int p[N]; n int d[N]; n int f[N]; n vector<int> bfs(int s) { n   memset(c, false, sizeof c); n   queue<int> q; n   q.push(s); n   c[s] = true; n   int minu = N; n   int xx, yy; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = 0; i < a[x].size(); ++i) { n       int h = a[x][i]; n       if (!c[h]) { n         c[h] = true; n         p[h] = x; n         d[h] = d[x] + 1; n         if (x != s) n           f[h] = f[x]; n         else n           f[h] = h; n         q.push(h); n       } else { n         if (x != s && h != s) { n           if (f[h] != f[x]) { n             if (d[x] + d[h] < minu) { n               minu = d[x] + d[h]; n               xx = x; n               yy = h; n             } n           } n         } n       } n     } n   } n   vector<int> v; n   for (int i = xx; i != s; i = p[i]) v.push_back(i); n   vector<int> v1; n   for (int i = yy; i != s; i = p[i]) v1.push_back(i); n   reverse((v1).begin(), (v1).end()); n   v.push_back(s); n   for (int i = 0; i < v1.size(); ++i) v.push_back(v1[i]); n   return v; n } n void solv() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     a[x].push_back(y); n     a[y].push_back(x); n   } n   if (m == n - 1) { n     dfs0(1, 1, 0); n     if (v[1].size() > v[0].size()) swap(v[0], v[1]); n     printf( 1 n ); n     for (int i = 0; i < k / 2 + k % 2; ++i) printf( %d  , v[0][i]); n     printf(  n ); n     return; n   } n   int s = dfs(1, -1); n   vector<int> v = bfs(s); n   if (v.size() <= k) { n     printf( 2 n ); n     printf( %d n , v.size()); n     for (int i = 0; i < v.size(); ++i) printf( %d  , v[i]); n     printf(  n ); n     return; n   } n   vector<int> vv; n   for (int i = 0; i < v.size(); i += 2) vv.push_back(v[i]); n   v = vv; n   printf( 1 n ); n   for (int i = 0; i < k / 2 + k % 2; ++i) printf( %d  , v[i]); n   printf(  n ); n } n int main() { n   solv(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 2e5 + 9, LG = 35; n long long sum, b[MAX], c[MAX], a[MAX], n, k[LG]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> b[i], sum += b[i]; n   for (int i = 0; i < n; i++) cin >> c[i], sum += c[i]; n   if (sum % (2 * n) != 0) return cout << -1, 0; n   sum /= 2 * n; n   for (int i = 0; i < n; i++) { n     a[i] = b[i] + c[i] - sum; n     if (a[i] % n != 0 || a[i] < 0) return cout << -1, 0; n     a[i] /= n; n     long long x = a[i]; n     for (int j = 0; j < LG; j++) k[j] += x >> j & 1; n   } n   for (int i = 0; i < n; i++) { n     long long sum1 = 0, sum2 = 0; n     for (int j = 0; j < LG; j++) n       if (a[i] >> j & 1) n         sum1 += k[j] * (1 << j), sum2 += n * (1 << j); n       else n         sum2 += k[j] * (1 << j); n     if (sum1 != b[i] || sum2 != c[i]) return cout << -1, 0; n   } n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int mx1, my1, mx2, my2; n string in; n int main() { n   ios::sync_with_stdio(0); n   cin >> in; n   mx1 = in[0] -  a  + 1; n   my1 = in[1] -  0 ; n   cin >> in; n   mx2 = in[0] -  a  + 1; n   my2 = in[1] -  0 ; n   cout << max(abs(mx1 - mx2), abs(my1 - my2)) << endl; n   while (1) { n     if (mx1 == mx2 && my1 == my2) break; n     if (mx1 > mx2) { n       cout <<  L ; n       mx1 -= 1; n     } else if (mx1 < mx2) { n       cout <<  R ; n       mx1 += 1; n     } n     if (my1 < my2) { n       cout <<  U ; n       my1 += 1; n     } else if (my1 > my2) { n       cout <<  D ; n       my1 -= 1; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void load() {} n const int N = 5e5 + 123; n const int inf = 1e9; n int a[N], v[N]; n int dp[200][200]; n void trungtt() { n   int n, k, p; n   scanf( %d%d%d , &n, &k, &p); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]), a[i] += a[i - 1], a[i] %= p; n   if (k * p > n) { n     for (int i = 0; i <= p; ++i) n       for (int j = 0; j <= k; ++j) dp[i][j] = inf; n     dp[0][0] = 0; n     for (int i = 1; i <= n; ++i) n       for (int j = k; j >= 1; --j) n         for (int t = 0; t < p; ++t) n           dp[a[i]][j] = min(dp[a[i]][j], dp[t][j - 1] + (a[i] - t + p) % p); n     printf( %d , dp[a[n]][k]); n   } else { n     for (int i = 0; i <= n; ++i) v[i] = inf; n     for (int i = 1; i < n; ++i) *upper_bound(v + 1, v + n, a[i]) = a[i]; n     if (v[k - 1] <= a[n]) n       printf( %d , a[n]); n     else n       printf( %d , a[n] + p); n   } n } n int main() { n   load(); n   trungtt(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int MAX = 1e6; n int len1, len2, pre[MAX], suf[MAX]; n bool can_skip(int skip) { n   for (int i = 1; i + skip - 1 < len1; i++) n     if (pre[i - 1] + suf[i + skip] >= len2) return 1; n   return 0; n } n int main() { n   string x, y, z =  1 ; n   cin >> x >> y; n   z += x; n   x = z; n   len1 = x.length(); n   len2 = y.length(); n   for (int i = 1, j = 0, it = 0; i < len1; i++) { n     if (j < len2 && x[i] == y[j]) it++, j++; n     pre[i] = it; n   } n   for (int i = len1 - 1, j = len2 - 1, it = 0; i >= 1; i--) { n     if (j >= 0 && x[i] == y[j]) it++, j--; n     suf[i] = it; n   } n   int l = 0, r = len1, ans = 0; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (can_skip(mid)) { n       l = mid + 1, ans = mid; n     } else n       r = mid - 1; n   } n   cout << ans << endl; 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   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int n; n     cin >> n; n     string x; n     cin >> x; n     string a(n,  0 ); n     string b(n,  0 ); n     for (int j = 0; j < n; j++) { n       if (x[j] ==  0 ) { n         a[j] =  0 ; n         b[j] =  0 ; n       } else if (x[j] ==  2 ) { n         a[j] =  1 ; n         b[j] =  1 ; n       } else { n         a[j] =  1 ; n         b[j] =  0 ; n         for (int k = j + 1; k < n; k++) { n           a[k] =  0 ; n           b[k] = x[k]; n         } n         break; n       } n     } n     cout << a << endl; n     cout << b << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n struct node { n   int u, v, w, f, nxt; n   node() {} n   node(int u, int v, int w, int f, int nxt) n       : u(u), v(v), w(w), f(f), nxt(nxt) {} n } e[N << 2]; n int h[N], idx = 1; n void link(int x, int y, int w) { n   e[++idx] = node(x, y, w, 1, h[x]); n   h[x] = idx; n   e[++idx] = node(y, x, -w, 0, h[y]); n   h[y] = idx; n } n int dis[N], pre[N]; n bool vis[N]; n int n, m; n int Mincost = 0; n int spfa() { n   memset(dis, 0x3f, sizeof dis); n   memset(vis, 0, sizeof vis); n   memset(pre, 0, sizeof pre); n   queue<int> q; n   while (q.size()) q.pop(); n   q.push(1); n   dis[1] = 0; n   while (q.size()) { n     int t = q.front(); n     q.pop(); n     vis[t] = 0; n     for (int i = h[t]; i; i = e[i].nxt) { n       int v = e[i].v, w = e[i].w; n       if (e[i].f && dis[v] > dis[t] + w) { n         dis[v] = dis[t] + w; n         pre[v] = i; n         if (vis[v] == 0) { n           vis[v] = 1; n           q.push(v); n         } n       } n     } n   } n   if (dis[n] == 0x3f3f3f3f) return 0; n   Mincost += dis[n]; n   for (int i = pre[n]; i; i = pre[e[i].u]) e[i].f--, e[i ^ 1].f++; n   return 1; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     link(x, y, z); n   } n   vector<int> v; n   v.clear(); n   while (spfa()) { n     v.push_back(Mincost); n   } n   int m; n   cin >> m; n   while (m--) { n     int x; n     cin >> x; n     double ans = 1e18; n     for (int i = 0; i < v.size(); i++) n       ans = min(ans, 1.0 * (v[i] + x) / (i + 1)); n     printf( %.10f n , ans); 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(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   int n, i, j, tmp, tmp2; n   cin >> n >> tmp; n   for (i = 1; i < n; i++) { n     cin >> tmp2; n     if (tmp == tmp2) { n       for (i = i + 1; i < n; i++) { n         cin >> tmp2; n         if (tmp > tmp2) { n           tmp = tmp2; n           for (i = i + 1; i < n; i++) { n             cin >> tmp2; n             if (tmp <= tmp2) { n               cout <<  NO  << endl; n               return 0; n             } n             tmp = tmp2; n           } n         } else if (tmp < tmp2) { n           cout <<  NO  << endl; n           return 0; n         } n       } n     } else if (tmp > tmp2) { n       tmp = tmp2; n       for (i = i + 1; i < n; i++) { n         cin >> tmp2; n         if (tmp <= tmp2) { n           cout <<  NO  << endl; n           return 0; n         } n         tmp = tmp2; n       } n     } n     tmp = tmp2; n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e4 + 5; n const int LOG = 16; n struct trie { n   int go[N][2], NFI = 1; n   trie() { memset(go, -1, sizeof(go)); } n   void reset() { n     for (int i = 0; i <= NFI; i++) go[i][0] = go[i][1] = -1; n     NFI = 1; n   } n   void add(int x) { n     for (int at = 1, bit = LOG - 1; bit >= 0; bit--) { n       int t = x >> bit & 1; n       if (go[at][t] == -1) go[at][t] = ++NFI; n       at = go[at][t]; n     } n   } n   int ask(int x) { n     int res = 0; n     for (int at = 1, bit = LOG - 1; bit >= 0; bit--) { n       int t = x >> bit & 1; n       if (go[at][!t] != -1) { n         res |= (1 << bit); n         at = go[at][!t]; n       } else n         at = go[at][t]; n     } n     return res; n   } n } t; n int n, q, a[N]; n vector<int> nb[N]; n int par[N], dep[N], jump_256[N]; n void DFS_prep(int v) { n   for (int u : nb[v]) { n     if (u != par[v]) { n       par[u] = v; n       dep[u] = dep[v] + 1; n       DFS_prep(u); n     } n   } n } n int res[N][256]; n void precalc(int v) { n   t.reset(); n   int temp = v; n   for (int i = 0; temp && i <= 255; i++) { n     t.add(a[temp] ^ i); n     temp = par[temp]; n   } n   jump_256[v] = temp; n   for (int i = 0; i < 256; i++) res[v][i] = t.ask(i * 256); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     nb[u].push_back(v); n     nb[v].push_back(u); n   } n   DFS_prep(1); n   for (int i = 1; i <= n; i++) precalc(i); n   while (q--) { n     int u, v; n     cin >> u >> v; n     int ans = 0; n     int jumps = 0; n     while (v && dep[jump_256[v]] > dep[u]) { n       ans = max(ans, res[v][jumps]); n       jumps++; n       v = jump_256[v]; n     } n     jumps *= 256; n     while (v && dep[v] >= dep[u]) { n       ans = max(ans, a[v] ^ jumps); n       v = par[v]; n       jumps++; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int N = 100005; n long long f[N]; n long long inverse(long long x) { n   if (x >= mod) return inverse(x % mod); n   assert(x != 0); n   if (x == 1) return 1; n   return inverse(mod % x) * (mod - mod / x) % mod; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<long long> a(n); n   long long sum = 0; n   for (auto &x : a) { n     cin >> x; n     sum += x; n   } n   f[0] = 0; n   f[1] = ((sum - 1) * (sum - 1) % mod) * inverse(sum) % mod; n   for (int m = 2; m < N && m < sum; m++) { n     f[m] = 2 * f[m - 1] - f[m - 2] - ((sum - 1) * inverse(sum - m + 1) % mod); n     f[m] %= mod; n     if (f[m] < 0) f[m] += mod; n   } n   long long ans = 0; n   for (auto x : a) { n     ans += f[x]; n   } n   ans %= mod; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } n int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } n const long long int MAXN = 1e6 + 10; n long long int n, ans = 1; n long long int a[MAXN], dp[MAXN]; n void solve() { n   cin >> n; n   for (int i = (1); i <= (n); ++i) { n     cin >> a[i]; n     dp[a[i]]++; n   } n   for (int i = (MAXN - 1); i >= (1); --i) { n     if (!dp[i]) continue; n     for (long long int j = 2 * i; j <= MAXN - 1; j += i) { n       dp[i] = max(dp[i], 1 + dp[j]); n       ans = max(ans, dp[i]); n     } n   } n   cout << ans << endl; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int t; n   t = 1; n   for (int i = (1); i <= (t); ++i) { n     solve(); n   } n } n