#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > snakeG(int x1, int y1, int x2, int y2) { n   vector<pair<int, int> > res; n   int n = y2 - y1 + 1, m = x2 - x1 + 1; n   for (int i = 0; i < m; ++i) { n     for (int j = 0; j < n; ++j) { n       if (i % 2 == 0) { n         res.push_back(make_pair(x1 + i, y1 + j)); n       } else { n         res.push_back(make_pair(x1 + i, y2 - j)); n       } n     } n   } n   return res; n } n vector<pair<int, int> > snakeV(int x1, int y1, int x2, int y2) { n   vector<pair<int, int> > res; n   int n = y2 - y1 + 1, m = x2 - x1 + 1; n   for (int j = 0; j < n; ++j) { n     for (int i = 0; i < m; ++i) { n       if (j % 2 == 0) { n         res.push_back(make_pair(x1 + i, y1 + j)); n       } else { n         res.push_back(make_pair(x2 - i, y1 + j)); n       } n     } n   } n   return res; n } n int main() { n   int n, m; n   cin >> n >> m; n   if (m == 1) { n     if (n == 2) { n       cout << 0 << endl; n       cout << 1 <<     << 1 << endl; n       cout << 2 <<     << 1 << endl; n       cout << 1 <<     << 1 << endl; n       return 0; n     } else { n       cout << 1 << endl; n       cout << n <<     << m <<     << 1 <<     << 1 << endl; n       for (int i = 1; i < n + 1; ++i) cout << i <<     << 1 << endl; n       cout << 1 <<     << 1 << endl; n       return 0; n     } n   } n   if (n == 1) { n     if (m == 2) { n       cout << 0 << endl; n       cout << 1 <<     << 1 << endl; n       cout << 1 <<     << 2 << endl; n       cout << 1 <<     << 1 << endl; n       return 0; n     } else { n       cout << 1 << endl; n       cout << n <<     << m <<     << 1 <<     << 1 << endl; n       for (int i = 1; i < m + 1; ++i) cout << 1 <<     << i << endl; n       cout << 1 <<     << 1 << endl; n       return 0; n     } n   } n   if (n == m) { n     if (n % 2 == 0) { n       cout << 0 << endl; n       vector<pair<int, int> > res = snakeG(1, 2, n, m); n       cout << 1 <<     << 1 << endl; n       for (int i = 0; i < res.size(); ++i) n         cout << res[i].first <<     << res[i].second << endl; n       for (int i = n; i >= 1; --i) cout << i <<     << 1 << endl; n     } else { n       cout << 1 << endl; n       cout << n <<     << m <<     << 1 <<     << 1 << endl; n       vector<pair<int, int> > res = snakeG(1, 1, n, m); n       for (int i = 0; i < res.size(); ++i) n         cout << res[i].first <<     << res[i].second << endl; n       cout << 1 <<     << 1 << endl; n     } n   } else { n     if (n > m) { n       if (m % 2 == 0) { n         if (n % 2 == 0) { n           cout << 0 << endl; n           vector<pair<int, int> > res = snakeG(1, 2, n, m); n           cout << 1 <<     << 1 << endl; n           for (int i = 0; i < res.size(); ++i) n             cout << res[i].first <<     << res[i].second << endl; n           for (int i = n; i >= 1; --i) cout << i <<     << 1 << endl; n         } else { n           cout << 0 << endl; n           vector<pair<int, int> > res = snakeG(1, 2, n - 2, m); n           vector<pair<int, int> > res1 = snakeV(n - 1, 1, n, m); n           reverse(res1.begin(), res1.end()); n           cout << 1 <<     << 1 << endl; n           for (int i = 0; i < res.size(); ++i) n             cout << res[i].first <<     << res[i].second << endl; n           for (int i = 0; i < res1.size(); ++i) n             cout << res1[i].first <<     << res1[i].second << endl; n           for (int i = n - 2; i >= 1; --i) cout << i <<     << 1 << endl; n         } n       } else { n         if (n % 2 == 0) { n           cout << 0 << endl; n           vector<pair<int, int> > res = snakeG(1, 2, n, m); n           cout << 1 <<     << 1 << endl; n           for (int i = 0; i < res.size(); ++i) n             cout << res[i].first <<     << res[i].second << endl; n           for (int i = n; i >= 1; --i) cout << i <<     << 1 << endl; n         } else { n           cout << 1 << endl; n           cout << n <<     << m <<     << 1 <<     << 1 << endl; n           vector<pair<int, int> > res = snakeG(1, 1, n, m); n           for (int i = 0; i < res.size(); ++i) n             cout << res[i].first <<     << res[i].second << endl; n           cout << 1 <<     << 1 << endl; n         } n       } n     } else { n       if (n % 2 == 0) { n         cout << 0 << endl; n         vector<pair<int, int> > res = snakeG(1, 2, n, m); n         cout << 1 <<     << 1 << endl; n         for (int i = 0; i < res.size(); ++i) n           cout << res[i].first <<     << res[i].second << endl; n         for (int i = n; i >= 1; --i) cout << i <<     << 1 << endl; n       } else { n         if (m % 2 == 0) { n           cout << 0 << endl; n           vector<pair<int, int> > res = snakeG(1, 2, n - 2, m); n           vector<pair<int, int> > res1 = snakeV(n - 1, 1, n, m); n           reverse(res1.begin(), res1.end()); n           cout << 1 <<     << 1 << endl; n           for (int i = 0; i < res.size(); ++i) n             cout << res[i].first <<     << res[i].second << endl; n           for (int i = 0; i < res1.size(); ++i) n             cout << res1[i].first <<     << res1[i].second << endl; n           for (int i = n - 2; i >= 1; --i) cout << i <<     << 1 << endl; n         } else { n           cout << 1 << endl; n           cout << n <<     << m <<     << 1 <<     << 1 << endl; n           vector<pair<int, int> > res = snakeG(1, 1, n, m); n           for (int i = 0; i < res.size(); ++i) n             cout << res[i].first <<     << res[i].second << endl; n           cout << 1 <<     << 1 << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool fl, vis[200005]; n int en, d[200005], head[200005]; n struct edge { n   int u, v, w, nxt; n } e[200005 * 4], e0[200005]; n void dfs(int); n void adde(int, int, int); n int calc(int x) { n   int ans = 0; n   while (x) { n     ans ^= x & 1; n     x >>= 1; n   } n   return ans; n } n int main() { n   int i, k, t, n, m, u, v, w; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d , &n, &m); n     fl = 0; n     en = 0; n     memset(vis, 0, n + 1); n     memset(head, 0, 4 * (n + 1)); n     for (i = 1; i < n; ++i) { n       scanf( %d %d %d , &u, &v, &w); n       e0[i].u = u; n       e0[i].v = v; n       e0[i].w = w; n       if (w != -1) { n         k = calc(w); n         adde(u, v, k); n         adde(v, u, k); n       } n     } n     for (i = 1; i <= m; ++i) { n       scanf( %d %d %d , &u, &v, &w); n       adde(u, v, w); n       adde(v, u, w); n     } n     for (i = 1; i <= n; ++i) n       if (!vis[i]) { n         d[i] = 0; n         dfs(i); n       } n     if (fl) { n       puts( NO ); n       continue; n     } n     for (i = 1; i < n; ++i) n       if (e0[i].w == -1) { n         u = e0[i].u; n         v = e0[i].v; n         if (d[u] == d[v]) n           e0[i].w = 0; n         else n           e0[i].w = 1; n       } n     puts( YES ); n     for (i = 1; i < n; ++i) printf( %d %d %d n , e0[i].u, e0[i].v, e0[i].w); n   } n   return 0; n } n void dfs(int u) { n   int i, v; n   vis[u] = 1; n   for (i = head[u]; i; i = e[i].nxt) n     if (!vis[v = e[i].v]) { n       d[v] = (d[u] ^ e[i].w); n       dfs(v); n     } else n       fl |= d[v] != (d[u] ^ e[i].w); n } n void adde(int u, int v, int w) { n   e[++en].v = v; n   e[en].w = w; n   e[en].nxt = head[u]; n   head[u] = en; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int v, u; n   long long w; n   edge() {} n   edge(int a, int b, long long c) : v(a), u(b), w(c) {} n   bool operator<(edge e) const { return w < e.w; } n }; n const int N = 100 * 1000 + 10; n vector<int> graph[N]; n vector<long long> wei[N]; n edge e[N * 2]; n int n, m, par[N], cnt, upd[N]; n bool is[N]; n long long d[N]; n int root(int v) { n   if (v == par[v]) return v; n   return par[v] = root(par[v]); n } n bool merge(int v, int u) { n   v = root(v); n   u = root(u); n   if (v == u) return false; n   par[v] = u; n   return true; n } n long long mst() { n   sort(e, e + cnt); n   long long ans = 0; n   for (int i = 0; i <= n - 1; i++) par[i] = i; n   for (int i = 0; i <= cnt - 1; i++) n     if (merge(e[i].v, e[i].u)) ans += e[i].w; n   return ans; n } n void dijk() { n   set<pair<long long, int> > st; n   for (int i = 0; i <= n - 1; i++) { n     d[i] = (1000LL * 1000 * 1000 * 1000 * 1000 * 1000); n     if (is[i]) { n       d[i] = 0; n       upd[i] = i; n     } n     st.insert(pair<long long, int>(0, i)); n   } n   while (!st.empty()) { n     int ind = st.begin()->second; n     st.erase(st.begin()); n     for (int i = 0; i <= ((int)(graph[ind]).size()) - 1; i++) { n       int u = graph[ind][i]; n       long long w = wei[ind][i] + d[ind]; n       if (d[u] > w) { n         st.erase(pair<long long, int>(d[u], u)); n         d[u] = w; n         upd[u] = upd[ind]; n         st.insert(pair<long long, int>(d[u], u)); n       } n     } n   } n   for (int i = 0; i <= n - 1; i++) n     for (int j = 0; j <= ((int)(graph[i]).size()) - 1; j++) { n       int u = graph[i][j]; n       e[cnt++] = edge(upd[i], upd[u], wei[i][j] + d[i] + d[u]); n     } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i <= m - 1; i++) { n     int a, b; n     long long c; n     cin >> a >> b >> c; n     a--; n     b--; n     graph[a].push_back(b); n     graph[b].push_back(a); n     wei[a].push_back(c); n     wei[b].push_back(c); n   } n   int k; n   cin >> k; n   for (int i = 0; i <= k - 1; i++) { n     int x; n     cin >> x; n     x--; n     is[x] = true; n   } n   dijk(); n   cout << mst() + d[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     vector<long long> p; n     vector<long long> pre; n     for (long long i = 0; i < n; i++) { n       p.push_back(i + 1); n     } n     long long sum = 0; n     for (long long i = 0; i < n; i++) { n       sum += p[i]; n       pre.push_back(sum); n     } n     long long ind = lower_bound(pre.begin(), pre.end(), k) - pre.begin(); n     long long x = p[ind]; n     long long y = n - x - 1; n     long long bb; n     bb = y + (pre[ind] - k) + 1; n     vector<char> ans; n     for (long long i = 0; i < n; i++) { n       if (i == y || i == bb) n         ans.push_back( b ); n       else n         ans.push_back( a ); n     } n     for (int i = 0; i < n; i++) { n       cout << ans[i]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 100005; n const int D = 1005; n struct data { n   int ind; n   long long Sum; n   data(int _ind, long long _Sum) { n     ind = _ind; n     Sum = _Sum; n   } n }; n bool compare(data A, data B) { n   if (A.Sum < B.Sum) return true; n   if (A.Sum == B.Sum && A.ind < B.ind) return true; n   return false; n } n long long f(long long a, long long b, long long c) { n   return (((a - b) * (a - b)) + (c * c)); n } n long long a[MAXN], S[MAXN]; n vector<data> Q; n int main() { n   int N; n   cin >> N; n   S[0] = 0; n   for (int i = 1; i <= N; i++) { n     cin >> a[i]; n     S[i] = S[i - 1] + a[i]; n     data r(i, S[i]); n     Q.push_back(r); n   } n   long long ans = MAXN * MAXN; n   for (long long i = 2; i <= N; i++) { n     for (long long j = i; j <= i + D && j <= N; j++) { n       long long res = f(S[i - 1], S[j], j - i + 1); n       ans = min(ans, res); n     } n   } n   sort(Q.begin(), Q.end(), compare); n   for (int i = 0; i < Q.size(); i++) { n     for (int j = i + 1; j < Q.size() && j < i + D; j++) { n       ans = min(ans, f(Q[i].Sum, Q[j].Sum, abs(Q[i].ind - Q[j].ind))); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int inf = INT_MAX; n const int MAX = 1e3 + 8; n const long long MOD = 1e9 + 7; n const int TOT_PRIMES = 1e6 + 9; n const int MAX_A = 71; n const int LN = 20; n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int a[32]; n   memset(a, 0, sizeof(a)); n   for (int i = 0; i < n; i++) { n     int res = 0; n     for (int j = 0; j < k; j++) { n       int x; n       cin >> x; n       res = x + res * 2; n     } n     a[res] = 1; n   } n   int f = 0; n   for (int i = 0; i < (1 << k); i++) { n     for (int j = 0; j < (1 << k); j++) { n       if ((!(i & j)) && a[i] && a[j]) f = 1; n     } n   } n   if (f) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   string s; n   cin >> s; n   vector<set<int>> v(26); n   for (int i = 0, l = s.size(); i < l; i++) { n     v[s[i] -  a ].insert(i); n   } n   int q; n   cin >> q; n   int t; n   while (q--) { n     cin >> t; n     if (t == 1) { n       int pos; n       char c; n       cin >> pos >> c; n       pos--; n       v[s[pos] -  a ].erase(pos); n       s[pos] = c; n       v[c -  a ].insert(pos); n     } else if (t == 2) { n       int l, r, cnt = 0; n       cin >> l >> r; n       l--; n       for (int i = 0; i < 26; i++) { n         auto itr = v[i].lower_bound(l); n         if (itr != v[i].end() && *itr < r) { n           cnt++; n         } n       } n       cout << cnt << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     long long n, k; n     cin >> n >> k; n     long long ans = 0; n     while (n != 0) { n       if (n % k == 0) { n         n /= k; n         ans++; n       } else { n         ans += n % k; n         n -= n % k; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005; n int N, K; n long long L[MAXN], R[MAXN]; n double dp[MAXN][MAXN]; n double calc(long long l, long long r) { n   long long ret = 0, cur = 1; n   for (; cur <= r; cur *= 10) { n     if (cur == 1e18) { n       ret++; n       break; n     } n     if (cur * 10 <= l) continue; n     if (cur <= l && cur * 10 > l) { n       int a = l / cur; n       if (a == 1) { n         if (cur * 10 <= r) n           ret += cur * 2 - l; n         else { n           int b = r / cur; n           if (b == 1) n             ret += r - l + 1; n           else n             ret += cur * 2 - l; n         } n       } n     } else if (cur * 10 > r) { n       int b = r / cur; n       if (b == 1) n         ret += r - cur + 1; n       else n         ret += cur; n     } else n       ret += cur; n   } n   return double(ret) / (r - l + 1); n } n int main() { n   if (fopen( input.txt ,  r )) freopen( input.txt ,  r , stdin); n   cin >> N; n   for (int i = 0; i < N; i++) cin >> L[i] >> R[i]; n   cin >> K; n   K = ceil(K / 100. * N); n   dp[0][0] = 1; n   for (int i = 0; i < N; i++) { n     double p = calc(L[i], R[i]); n     for (int j = 0; j < N; j++) { n       dp[i + 1][j + 1] += dp[i][j] * p; n       dp[i + 1][j] += dp[i][j] * (1 - p); n     } n   } n   double ans = 0; n   for (int k = K; k <= N; k++) ans += dp[N][k]; n   cout << fixed << setprecision(10) << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int b[100]; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[a[i]]++; n   } n   int f = 0, h = -1; n   for (int i = 1; i <= 100; i++) { n     if (b[i] == 1) { n       f++; n     } else if (b[i] > 2) { n       h = i; n     } n   } n   if (f % 2 == 0) { n     cout <<  YES n ; n     int r = f / 2; n     for (int i = 0; i < n; i++) { n       if (b[a[i]] == 1) { n         if (r > 0) { n           cout <<  A ; n           r--; n         } else { n           cout <<  B ; n         } n       } else { n         cout <<  A ; n       } n     } n   } else { n     if (h == -1) { n       return cout <<  NO , 0; n     } n     int y = 0; n     cout <<  YES n ; n     int r = f / 2; n     for (int i = 0; i < n; i++) { n       if ((a[i] != h) || (a[i] == h && y != 0)) { n         if (b[a[i]] == 1) { n           if (r > 0) { n             cout <<  A ; n             r--; n           } else { n             cout <<  B ; n           } n         } else { n           cout <<  B ; n         } n       } else { n         cout <<  A ; n         y++; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, mod1 = 998244353, mod2 = 1000000007, mod3 = 1222827239; n string s1, s2; n struct ha { n   long long q1, q2, q3; n   bool operator==(const ha &aa) const { n     return (q1 == aa.q1 && q2 == aa.q2 && q3 == aa.q3); n   } n } has[500001], gg[500001], base[500001]; n void mo(ha &qq) { n   qq.q1 %= mod1; n   qq.q1 += mod1; n   qq.q1 %= mod1; n   qq.q2 %= mod2; n   qq.q2 += mod2; n   qq.q2 %= mod2; n   qq.q3 %= mod3; n   qq.q3 += mod3; n   qq.q3 %= mod3; n } n ha get(long long ll, long long rr) { n   ha tt; n   tt.q1 = has[rr].q1 - has[ll - 1].q1 * base[rr - ll + 1].q1 % mod1; n   tt.q2 = has[rr].q2 - has[ll - 1].q2 * base[rr - ll + 1].q2 % mod2; n   tt.q3 = has[rr].q3 - has[ll - 1].q3 * base[rr - ll + 1].q3 % mod3; n   mo(tt); n   return tt; n } n ha get1(long long ll, long long rr) { n   ha tt; n   tt.q1 = gg[rr].q1 - gg[ll - 1].q1 * base[rr - ll].q1 % mod1; n   tt.q2 = gg[rr].q2 - gg[ll - 1].q2 * base[rr - ll].q2 % mod2; n   tt.q3 = gg[rr].q3 - gg[ll - 1].q3 * base[rr - ll].q3 % mod3; n   mo(tt); n   return tt; n } n int main() { n   base[0].q1 = base[0].q2 = base[0].q3 = 1; n   for (int i = 1; i <= 500000; i++) { n     base[i].q1 = base[i - 1].q1 * 10; n     base[i].q2 = base[i - 1].q2 * 10; n     base[i].q3 = base[i - 1].q3 * 10; n     mo(base[i]); n   } n   cin >> s1 >> s2; n   for (int i = 1; i <= s1.size(); i++) { n     has[i].q1 = has[i - 1].q1 * 10 + s1[i - 1] -  0 ; n     has[i].q2 = has[i - 1].q2 * 10 + s1[i - 1] -  0 ; n     has[i].q3 = has[i - 1].q3 * 10 + s1[i - 1] -  0 ; n     mo(has[i]); n   } n   for (int i = 1; i <= s2.size(); i++) { n     gg[i].q1 = gg[i - 1].q1 * 10 + s2[i - 1] -  0 ; n     gg[i].q2 = gg[i - 1].q2 * 10 + s2[i - 1] -  0 ; n     gg[i].q3 = gg[i - 1].q3 * 10 + s2[i - 1] -  0 ; n     mo(gg[i]); n   } n   b = s2.size(); n   for (int i = s2.size() - 1; i + s2.size() - 1 <= s1.size(); i++) { n     ha f1 = get(i - b + 2, i), f2 = get(i + 1, i + b - 1); n     f1.q1 = f1.q1 + f2.q1, f1.q2 = f1.q2 + f2.q2, f1.q3 = f1.q3 + f2.q3; n     mo(f1); n     if (f1.q1 == gg[b].q1 && f1.q2 == gg[b].q2 && f1.q3 == gg[b].q3) { n       long long l1 = i - b + 2, r1 = i, l2 = i + 1, r2 = i + b - 1; n       printf( %lld %lld n%lld %lld , l1, r1, l2, r2); n       return 0; n     } n   } n   for (int i = s2.size(); i <= s1.size(); i++) { n     long long ll = 1, rr = s2.size(), ann = 0; n     while (ll <= rr) { n       long long mid = ((ll + rr) >> 1); n       if (get(i - s2.size() + 1, i - s2.size() + mid) == get1(1, mid)) { n         ann = mid; n         ll = mid + 1; n       } else n         rr = mid - 1; n     } n     long long tt = i + b - ann; n     if (tt > s1.size() + 1) continue; n     ha f1, f2; n     if (s1[0] ==  3  && s1[1] ==  1  && tt >= 9681) { n       cout << i - s2.size() + 1 <<     << i <<     << i + 1 <<     << tt n            <<   n ; n       f1 = get(i - s2.size() + 1, i), f2 = get(i + 1, tt); n       f1.q1 = f1.q1 + f2.q1, f1.q2 = f1.q2 + f2.q2, f1.q3 = f1.q3 + f2.q3; n       mo(f1); n       cout << f1.q1 <<     << f1.q2 <<     << f1.q3 <<     << gg[b].q1 <<     n            << gg[b].q2 <<     << gg[b].q3 <<   n ; n     } n     if (i + 1 <= tt - 1) { n       f1 = get(i - s2.size() + 1, i), f2 = get(i + 1, tt - 1); n       f1.q1 = f1.q1 + f2.q1, f1.q2 = f1.q2 + f2.q2, f1.q3 = f1.q3 + f2.q3; n       mo(f1); n       if (f1.q1 == gg[b].q1 && f1.q2 == gg[b].q2 && f1.q3 == gg[b].q3) { n         long long l1 = i - s2.size() + 1, r1 = i, l2 = i + 1, r2 = tt - 1; n         printf( %lld %lld n%lld %lld , l1, r1, l2, r2); n         return 0; n       } n     } n     if (tt <= s1.size() && i + 1 <= tt) { n       f1 = get(i - s2.size() + 1, i), f2 = get(i + 1, tt); n       f1.q1 = f1.q1 + f2.q1, f1.q2 = f1.q2 + f2.q2, f1.q3 = f1.q3 + f2.q3; n       mo(f1); n       if (f1.q1 == gg[b].q1 && f1.q2 == gg[b].q2 && f1.q3 == gg[b].q3) { n         long long l1 = i - s2.size() + 1, r1 = i, l2 = i + 1, r2 = tt; n         printf( %lld %lld n%lld %lld , l1, r1, l2, r2); n         return 0; n       } n     } n   } n   for (int i = s1.size() - s2.size() + 1; i >= 1; i--) { n     long long ll = 1, rr = s2.size(), ann = 0; n     while (ll <= rr) { n       long long mid = ((ll + rr) >> 1); n       if (get(i, i + mid - 1) == get1(1, mid)) { n         ann = mid; n         ll = mid + 1; n       } else n         rr = mid - 1; n     } n     long long tt = i - (long long)(s2.size()) + ann; n     if (tt < 0) continue; n     ha f1, f2; n     if (tt + 1 <= i - 1) { n       f1 = get(i, i + s2.size() - 1), f2 = get(tt + 1, i - 1); n       f1.q1 = f1.q1 + f2.q1, f1.q2 = f1.q2 + f2.q2, f1.q3 = f1.q3 + f2.q3; n       mo(f1); n       if (f1.q1 == gg[b].q1 && f1.q2 == gg[b].q2 && f1.q3 == gg[b].q3) { n         long long l1 = tt + 1, r1 = i - 1, l2 = i, r2 = i + s2.size() - 1; n         printf( %lld %lld n%lld %lld , l1, r1, l2, r2); n         return 0; n       } n     } n     if (tt > 0 && tt <= i - 1) { n       f1 = get(i, i + s2.size() - 1), f2 = get(i - s2.size() + ann, i - 1); n       f1.q1 = f1.q1 + f2.q1, f1.q2 = f1.q2 + f2.q2, f1.q3 = f1.q3 + f2.q3; n       mo(f1); n       if (f1.q1 == gg[b].q1 && f1.q2 == gg[b].q2 && f1.q3 == gg[b].q3) { n         long long l1 = tt, r1 = i - 1, l2 = i, r2 = i + s2.size() - 1; n         printf( %lld %lld n%lld %lld , l1, r1, l2, r2); n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     if (n % 4 != 0) { n       cout <<  NO n ; n       continue; n     } n     cout <<  YES n ; n     for (int i = 2; i <= n; i += 2) cout << i <<    ; n     for (int i = 1; i < n - 1; i += 2) cout << i <<    ; n     cout << (n - 1) + (n / 2) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n vector<T> uniq(vector<T> v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   return v; n } n template <typename T> n struct RMQ { n   using F = function<T(const T &, const T &)>; n   int n; n   vector<T> t; n   F f; n   RMQ(int n, F f, T df = 0) : n(n), f(f), t(vector<T>(n << 1, df)) {} n   void modify(int p, T v) { n     for (t[p += n] = v; p > 1; p >>= 1) t[p >> 1] = f(t[p], t[p ^ 1]); n   } n   T query(int l, int r, T res = 0) { n     for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) { n       if (l & 1) res = f(res, t[l++]); n       if (r & 1) res = f(res, t[--r]); n     } n     return res; n   } n }; n int32_t main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> l(n + 1, 0), r(n + 1, 0); n   vector<int> a(n); n   for (int &x : a) cin >> x; n   vector<int> b; n   for (int i = 0; i < n; i++) { n     b.push_back(a[i] - i); n     b.push_back(a[i] - i + 1); n   } n   b = uniq(b); n   int ptr = 0; n   map<int, int> mp; n   for (int x : b) mp[x] = ++ptr; n   RMQ<int> ST(b.size() + 10, [&](int p, int q) { return max(p, q); }); n   for (int i = 0; i < n; i++) { n     l[i] = 1 + ST.query(1, mp[a[i] - i]); n     ST.modify(mp[a[i] - i], l[i]); n   } n   ST = RMQ<int>(b.size() + 10, [&](int p, int q) { return max(p, q); }); n   for (int i = n - 1; i >= 0; i--) { n     r[i] = 1 + ST.query(mp[a[i] - i], b.size() + 9); n     if (i + 1 < n) { n       int x = 1 + ST.query(mp[a[i + 1] - (i + 1) + 1], b.size() + 9); n       ST.modify(mp[a[i + 1] - (i + 1) + 1], x); n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) ans = max(ans, l[i] + r[i] - 1); n   cout << n - 1 - min(n - 1, ans) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n const long long MAXN = 100010; n const long long INF = 1e9; n const long long Mod = 1e9 + 7; n long long N; n set<pair<long long, long long> > S; n long long P; n long long last = 0; n long long h[9][MAXN + 1]; n long long inv[MAXN + 1]; n long long top; n long long f[MAXN + 1]; n long long g[9]; n pair<long long, long long> sta[MAXN + 1]; n const long long inv2 = (Mod + 1) / 2; n inline void Add(long long x, long long fn) { n   if (top && fn == sta[top].second) n     S.erase(sta[top]), sta[top].first = x, S.insert(sta[top]); n   else n     sta[++top] = make_pair(x, fn), S.insert(sta[top]); n } n inline long long Qf(long long x) { n   return (*S.upper_bound(make_pair(x - 1, 3))).second; n } n inline long long OPR(long long i) { n   long long l = 1, r = i + 1, d = (r - l) / 3; n   long long a = Qf(d), b = Qf((r - d) - l), fx = 0; n   fx = 0; n   while (fx == a || fx == b) ++fx; n   return fx; n } n long long ifac[MAXN + 1]; n int main() { n   N = read(); n   P = read(); n   long long now = 2; n   f[1] = f[2] = 0; n   Add(2, 0); n   for (long long i = 3;; i++) { n     long long l = 1, r = i + 1, d = (r - l) / 3; n     long long a = f[d], b = f[(r - d) - l]; n     f[i] = 0; n     while (f[i] == a || f[i] == b) ++f[i]; n     if (f[i] != f[i - 1]) { n       last = i, Add(i - 1, f[i - 1]); n       if (i > 1000) { n         now = i - 1; n         break; n       } n     } n   } n   long long lena = 1, lenb = 1; n   for (; now <= P;) { n     ++now; n     while (sta[lena].first * 3 < now) ++lena; n     while ((sta[lenb].first * 3) / 2 < now) ++lenb; n     now = max(min((sta[lenb].first * 3 / 2) - 3, sta[lena].first * 3 - 3), now); n     for (long long j = 1; j <= 10; j++) Add(now, OPR(now)), ++now; n   } n   --P; n   for (long long i = 1; i <= top; i++) { n     long long len = min(sta[i].first, P) - min(sta[i - 1].first, P); n     (g[sta[i].second] += len * (P + 1) % Mod - n                          (min(sta[i].first, P) + min(sta[i - 1].first, P) + 1) * n                              len % Mod * inv2 % Mod + n                          Mod) %= Mod; n   } n   inv[1] = 1; n   ifac[0] = 1; n   for (long long i = 2; i <= N; i++) n     inv[i] = (Mod - (Mod / i) * inv[Mod % i] % Mod) % Mod; n   for (long long i = 1; i <= N; i++) ifac[i] = ifac[i - 1] * inv[i] % Mod; n   for (long long i = 0; i < 3; i++) { n     long long ans = 1; n     for (long long j = 0; j <= N; j++) { n       if ((!(j & 1)) || (!i)) n         for (long long k = N - j; k >= 0; k--) n           (h[i][k + j] += n            (i ? h[i - 1][k] : (!k ? 1 : 0)) * ans % Mod * ifac[j] % Mod) %= Mod; n       ans = ans * g[i] % Mod; n     } n   } n   long long fac = 1, pw = 1, sum = (g[0] + g[1] + g[2]) % Mod; n   for (long long i = 1; i <= N; i++) fac = fac * i % Mod, pw = pw * sum % Mod; n   printf( %lld n , (pw - h[2][N] * fac % Mod + Mod) % Mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long x, y, z; n vector<pair<long long, long long> > v[200005]; n long long dp[300]; n bool used[2000005]; n long long len[100005]; n void dfs(long long x) { n   used[x] = true; n   for (long long i = 0; i < v[x].size(); ++i) { n     long long to = v[x][i].first; n     if (used[to]) { n       long long l = len[x] ^ len[to] ^ v[x][i].second; n       for (long long j = 32; j >= 0; --j) n         if ((!dp[j] || dp[j] > l) && l & (1 << j)) { n           dp[j] = l; n         } else n           l = min(l, l ^ dp[j]); n     } else { n       len[to] = len[x] ^ v[x][i].second; n       dfs(to); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (long long i = 1; i <= m; ++i) { n     cin >> x >> y >> z; n     v[x].push_back(make_pair(y, z)); n     v[y].push_back(make_pair(x, z)); n   } n   dfs(1); n   long long ans = len[n]; n   for (long long i = 32; i >= 0; --i) ans = min(ans, ans ^ dp[i]); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   char m =  b ; n   int ans = 0; n   int k = 0, bm = 0, rm = 0; n   while (k < n) { n     if (s[k] == m) { n       if (m ==  b ) n         m =  r ; n       else n         m =  b ; n       k++; n     } else { n       if (s[k] ==  b ) n         bm++; n       else n         rm++; n       if (m ==  b ) n         m =  r ; n       else n         m =  b ; n       k++; n     } n   } n   int ans1 = min(bm, rm) + abs(rm - bm); n   m =  r ; n   k = 0, bm = 0, rm = 0; n   while (k < n) { n     if (s[k] == m) { n       if (m ==  b ) n         m =  r ; n       else n         m =  b ; n       k++; n     } else { n       if (s[k] ==  b ) n         bm++; n       else n         rm++; n       if (m ==  b ) n         m =  r ; n       else n         m =  b ; n       k++; n     } n   } n   int ans2 = min(bm, rm) + abs(rm - bm); n   cout << min(ans1, ans2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char A[5010], B[5010]; n int N, M; n int f[5010][5010]; n int main() { n   scanf( %d%d , &N, &M); n   scanf( %s , A + 1); n   scanf( %s , B + 1); n   int ans = 0; n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= M; j++) { n       f[i][j] = max(f[i - 1][j] - 1, f[i][j - 1] - 1); n       if (A[i] == B[j]) { n         f[i][j] = max(f[i][j], f[i - 1][j - 1] + 2); n         f[i][j] = max(f[i][j], 2); n       } n       ans = max(ans, f[i][j]); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> t; n void upd(int v, int x) { n   for (int i = v; i < t.size(); i |= (i + 1)) { n     t[i] += x; n   } n } n int get(int v) { n   int sum = 0; n   for (int i = v; i >= 0; i = (i & (i + 1)) - 1) { n     sum += t[i]; n   } n   return sum; n } n int low_b(int s) { n   int k = 0; n   for (int i = 20; i > -1; i--) { n     if (k + (1 << i) - 1 >= t.size()) { n       continue; n     } n     if (t[k + (1 << i) - 1] < s) { n       s -= t[k + (1 << i) - 1]; n       k += (1 << i); n     } n   } n   return k; n } n const int k = 600; n bool cmp(const vector<int>& a, const vector<int>& b) { n   if (a[0] / k == b[0] / k) return a[1] / k > b[1] / k; n   return a[0] / k < b[0] / k; n } n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   vector<vector<int>> pos(n); n   for (int i = 0; i < n; ++i) { n     pos[i].push_back(-1); n     pos[i].push_back(n); n   } n   for (int i = 0; i < n; ++i) { n     pos[a[i] - 1].push_back(i); n   } n   for (int i = 0; i < n; ++i) { n     sort(pos[i].begin(), pos[i].end()); n   } n   vector<vector<int>> queries; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < pos[i].size() - 1; ++j) { n       int l = pos[i][j] + 1; n       int r = pos[i][j + 1] - 1; n       if (l <= r) queries.push_back({l, r, i + 1}); n     } n   } n   queries.push_back({0, n - 1, n + 1}); n   sort(queries.begin(), queries.end(), cmp); n   t.resize(n + 2); n   for (int i = 0; i < n + 2; ++i) { n     upd(i, 1); n   } n   vector<int> cnt(n + 1); n   int L = 0, R = -1; n   for (int i = 0; i < queries.size(); ++i) { n     int l = queries[i][0]; n     int r = queries[i][1]; n     while (L > l) { n       L--; n       cnt[a[L] - 1]++; n       if (cnt[a[L] - 1] == 1) { n         upd(a[L] - 1, -1); n       } n     } n     while (L < l) { n       cnt[a[L] - 1]--; n       if (cnt[a[L] - 1] == 0) { n         upd(a[L] - 1, 1); n       } n       L++; n     } n     while (R > r) { n       cnt[a[R] - 1]--; n       if (cnt[a[R] - 1] == 0) { n         upd(a[R] - 1, 1); n       } n       R--; n     } n     while (R < r) { n       R++; n       cnt[a[R] - 1]++; n       if (cnt[a[R] - 1] == 1) { n         upd(a[R] - 1, -1); n       } n     } n     queries[i].push_back(low_b(1) + 1); n   } n   vector<bool> ok(n + 2, false); n   for (auto elem : queries) { n     if (elem[2] == elem[3]) { n       ok[elem[2] - 1] = 1; n     } n   } n   int answe = 0; n   for (; answe <= n + 1; ++answe) { n     if (!ok[answe]) break; n   } n   cout << answe + 1; n } n


#include<bits/stdc++.h> n using namespace std; n  n int main(){ n     int t; n     cin>>t; n      n     while(t--){ n         int n; n         cin>>n; n          n         int A[n],B[n]; n         vector<int>pos,neg; n         for(int i=0;i<n;++i){ n             cin>>A[i]; n         } n          n         int z=0; n         int m=0; n         for(int i=0;i<n;++i){ n             cin>>B[i]; n             z+=(B[i]-A[i]); n             if(B[i]>A[i]){ n                 m+=(B[i]-A[i]); n                 neg.push_back(i); n             } n             if(B[i]<A[i]){ n                 pos.push_back(i); n             } n         } n          n         if(z==0){ n               n             cout<<m<<  n ; n             int i1=0,i2=0; n             for(int i=0;i<m;++i){ n                 cout<<(pos[i1]+1)<<   <<(neg[i2]+1)<<  n ; n                 A[pos[i1]]--; n                 A[neg[i2]]++; n                  n                 if(A[pos[i1]]==B[pos[i1]])i1++; n                 if(A[neg[i2]]==B[neg[i2]])i2++; n             } n         } n         else{ n             cout<< -1 n ; n         } n          n     } n }


#include <bits/stdc++.h> n using namespace std; n int arr[3]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     int x; n     cin >> x; n     arr[i % 3] += x; n   } n   int count = 0; n   for (int i = 0; i < 3; i++) { n     count = max(count, arr[i]); n   } n   if (arr[0] == count) { n     cout <<  chest  n          <<   n ; n   } n   if (arr[1] == count) { n     cout <<  biceps  n          <<   n ; n   } n   if (arr[2] == count) { n     cout <<  back  n          <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n + 1]; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   int x; n   cin >> x; n   int w, l; n   while (x--) { n     cin >> w >> l; n     if (w == 1) { n       arr[2] += arr[1] - l; n       arr[1] = 0; n       continue; n     } n     if (w == n) { n       arr[n - 1] += l - 1; n       arr[n] = 0; n       continue; n     } n     arr[w - 1] += l - 1; n     arr[w + 1] += arr[w] - l; n     arr[w] = 0; n   } n   for (int i = 1; i <= n; i++) { n     cout << arr[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   register int X = 0; n   register char ch = 0; n   while (ch < 48 || ch > 57) ch = getchar(); n   while (ch >= 48 && ch <= 57) X = X * 10 + (ch ^ 48), ch = getchar(); n   return X; n } n long long powM(long long a, int t = 998244353 - 2) { n   long long ans = 1; n   while (t) { n     if (t & 1) ans = ans * a % 998244353; n     a = a * a % 998244353; n     t >>= 1; n   } n   return ans; n } n int r[135000 << 2]; n long long invG = powM(3), fac[135000], inv[135000]; n void NTT(long long *f, bool op, int n) { n   for (int i = 0; i < n; i++) n     if (r[i] < i) swap(f[r[i]], f[i]); n   for (int len = 1; len < n; len <<= 1) { n     int w = powM(op == 1 ? 3 : invG, (998244353 - 1) / len / 2); n     for (int p = 0; p < n; p += len + len) { n       long long buf = 1; n       for (int i = p; i < p + len; i++) { n         int sav = f[i + len] * buf % 998244353; n         f[i + len] = f[i] - sav; n         if (f[i + len] < 0) f[i + len] += 998244353; n         f[i] = f[i] + sav; n         if (f[i] >= 998244353) f[i] -= 998244353; n         buf = buf * w % 998244353; n       } n     } n   } n } n long long g[135000 << 1]; n void times(long long *f, long long *gg, int len, int lim) { n   int m = len + len, n; n   for (int i = 0; i < len; i++) g[i] = gg[i]; n   for (n = 1; n < m; n <<= 1) n     ; n   for (int i = len; i < n; i++) g[i] = 0; n   for (int i = 0; i < n; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0); n   NTT(f, 1, n); n   NTT(g, 1, n); n   for (int i = 0; i < n; ++i) f[i] = f[i] * g[i] % 998244353; n   NTT(f, 0, n); n   long long invn = powM(n); n   for (int i = 0; i < lim; ++i) f[i] = f[i] * invn % 998244353; n   for (int i = lim; i < n; ++i) f[i] = 0; n } n long long f[135000 << 1], p[135000 << 1], m; n int n; n int main() { n   n = read() + 1; n   scanf( %I64d , &m); n   m %= (998244353 - 1); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 998244353; n   inv[n] = powM(fac[n]); n   for (int i = n; i; i--) inv[i - 1] = inv[i] * i % 998244353; n   for (int i = 0; i < n; i++) f[i] = read(); n   for (int i = 0; i < n; i++) p[n - i - 1] = f[i] * fac[i] % 998244353; n   for (int i = 0; i < n; i++) f[i] = inv[i] % 998244353; n   times(p, f, n, n); n   for (int i = 0; i < n; i++) f[n - i - 1] = p[i] * inv[n - i - 1] % 998244353; n   for (int i = 0; i < n; i++) n     f[i] = f[i] * powM(i + 1, 998244353 - 1 - m) % 998244353; n   for (int i = 0; i < n; i++) n     if (i & 1) n       p[n - i - 1] = 998244353 - (f[i] * fac[i] % 998244353); n     else n       p[n - i - 1] = f[i] * fac[i] % 998244353; n   for (int i = 0; i < n; i++) f[i] = inv[i] % 998244353; n   times(p, f, n, n); n   for (int i = 0; i < n; i++) f[n - i - 1] = p[i] * inv[n - i - 1] % 998244353; n   for (int i = 0; i < n; i++) n     printf( %I64d  , (i & 1) ? (998244353 - f[i]) % 998244353 : f[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 20071027; n const double pi = acos(1.0 * (-1)); n int p[1001111]; n long long fun(long long x) { n   long long dt = 1 + 8 * x; n   long long t = sqrt(dt * 1.0) + 0.0000001; n   if (t * t != dt) return -1; n   return (1 + t) / 2; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n00, n01, n10, n11; n   cin >> n00 >> n01 >> n10 >> n11; n   long long n1, n0; n   n1 = fun(n11); n   n0 = fun(n00); n   if (n00 + n01 + n10 + n11 == 0) { n     cout <<  1 ; n     return 0; n   } n   if (n1 == -1 || n0 == -1) { n     cout <<  Impossible ; n     return 0; n   } n   if (n01 + n10 == 0) { n     if (n0 == 1) n       for (int i = 0; i < n1; i++) cout <<  1 ; n     else if (n1 == 1) n       for (int i = 0; i < n0; i++) cout <<  0 ; n     else n       cout <<  Impossible ; n     return 0; n   } n   if (n01 + n10 != n0 * n1) { n     cout <<  Impossible ; n     return 0; n   } n   if (n01 > n0) { n     p[n0] = n01 / n0; n     n01 -= n0 * p[n0]; n     n1 -= p[n0]; n   } n   if (n1) { n     p[n01]++; n     n1--; n   } n   p[0] += n1; n   for (int i = 0; i < n0; i++) { n     for (int j = 0; j < p[i]; j++) cout <<  1 ; n     cout <<  0 ; n   } n   for (int i = 0; i < p[n0]; i++) cout <<  1 ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( unroll-loops ) n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     long long a[n + 5], m = 0; n     vector<int> idx; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     long long ans = 0; n     for (int i = 0; i < n - 1; i++) { n       ans = max(ans, a[i] * a[i + 1]); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int b, k, a[100005], sum, tmp; n int main() { n   cin >> b >> k; n   for (int i = 1; i <= k; ++i) { n     scanf( %d , &a[i]); n     a[i] %= 2; n   } n   tmp = 1; n   for (int i = k; i >= 1; --i) { n     sum = (sum + tmp * a[i]) % 2; n     tmp = tmp * b % 2; n   } n   if (sum & 1) { n     cout <<  odd  << endl; n   } else { n     cout <<  even  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d , &n, &m), ++n; n   long long ans = 0; n   for (int a = m + 1; a <= n / 2; ++a) { n     int tmp = 0; n     for (int b = m + 1, c = n - m - 1; b < n - m && c > m; ++b) { n       while (c > m && a * ((a << 1) + b - (n << 1) - c) >= -c * (n + b)) --c; n       tmp += c - m; n     } n     if (a != n - a) tmp *= 2; n     ans += tmp; n   } n   cout << ans * 3 << endl; n } n


#include <iostream> n #include <iosfwd> n #include <iomanip> n #include <cstdio> n #include <cstring> n #include <cstdlib> n #include <ctime> n #include <cmath> n #include <cassert> n #include <cctype> n #include <climits> n #include <vector> n #include <bitset> n #include <set> n #include <queue> n #include <math.h> n #include <stack> n #include <map> n #include <deque> n #include <string> n #include <list> n #include <iterator> n #include <sstream> n #include <complex> n #include <fstream> n #include <functional> n #include <numeric> n #include <utility> n #include <algorithm> n #include <assert.h> n #include <unordered_map> n #include<ctime> n #include <chrono> n #pragma GCC optimize ( -O3 ) n using namespace std; n  n #define tolower(s) transform(s.begin(),s.end(),s.begin(),::tolower); n #define toupper(s) transform(s.begin(),s.end(),s.begin(),::toupper); n #define ll long long int n #define pii pair <ll , ll > n #define pb push_back n #define all(s) s.begin(),s.end() n ll gcd(ll a, ll b) { if (a == 0) return b; return b == 0 ? a : gcd(b, a % b);} n #define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(),  , ,    ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr <<   n ; } n void err(istream_iterator<string> it) {} n template<typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { cerr <<  [   << *it <<   =   << a <<   ] ; err(++it, args...);} n  n /**************************************************************************/ n const int x4[] = {1, -1, 0, 0}; n const int y4[] = {0, 0, 1, -1}; n const int x8[] = {1, 1, 1, -1, -1, -1, 0, 0}; n const int y8[] = {1, 0, -1, 1, 0, -1, 1, -1}; n /***************************************************************************/ n const ll MOD = 1e9 + 7; n const ll MOD2 = 998244353; n const ll INF = 9223372036854775800; n const long double PI = 3.141592653589793; n const int PRECISION = 10; n /***************************************************************************/ n const int DIGITSIZE = 18; n const int MAXN = 100001; n const int LOGN = 22; n const int MAXBIT = 62; n /***************************************************************************/ n const bool take_test_case = 1; n const bool print_case_no = 0; n /****************************************************************************/ n  n void getac(int T) { n  int n; n  cin >> n; n  if (n == 2) { n   cout << -1 <<   n ; n   return; n  } n  int curr = 1; n  int sq = n * n; n  for (int i = 0; i < n; i++) { n   for (int i = 0; i < n; i++) { n    cout << curr <<    ; n    curr += 2; n    if (curr > sq) { n     curr -= sq - (n % 2 == 0); n    } n   } n   cout <<   n ; n  } n  n } n int  main() n { n #ifndef ONLINE_JUDGE n  freopen( input.txt ,  r , stdin); n  freopen( output.txt ,  w , stdout); n  freopen( error.txt ,  w , stderr); n #endif n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  cerr.tie(NULL); n  auto start_time = std::chrono::high_resolution_clock::now(); n  n  int test_cases = 1; n  if (take_test_case) cin >> test_cases; n  for (int test_case_no = 1; test_case_no <= test_cases; test_case_no++) { n   if (print_case_no) n    cout <<  Case #  << test_case_no <<  :  ; n   getac(test_case_no); n  } n  cerr <<  Time elapsed   << chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start_time).count(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int c[1200000], save[5500]; n bool flag[1200000]; n int main() { n   int n, k; n   while (scanf( %d%d , &n, &k) != EOF) { n     for (int i = 1; i <= n; i++) scanf( %d , &save[i]); n     sort(save + 1, save + n + 1); n     memset(c, 0, sizeof(c)); n     for (int i = 1; i <= n; i++) n       for (int j = i + 1; j <= n; j++) { n         save[j] > save[i] ? c[save[j] - save[i]]++ : c[save[i] - save[j]]++; n       } n     int K = k * (k + 1) / 2; n     for (int i = 1; i <= save[n] + 1; i++) { n       int temp = 0; n       for (int j = i; j <= save[n] + 1; j += i) { n         temp += c[j]; n         if (temp > K) break; n       } n       if (temp <= K) { n         int cnt = 0; n         memset(flag, 0, sizeof(flag)); n         for (int j = 1; j <= n; j++) { n           int tt = save[j] % i; n           if (flag[tt]) { n             cnt++; n             if (cnt > k) break; n           } n           flag[tt] = 1; n         } n         if (cnt <= k) { n           printf( %d n , i); n           break; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using VI = vector<int>; n using VI2 = vector<VI>; n using UM = unordered_map<int, int>; n using UM2 = unordered_map<int, UM>; n using M = map<int, int>; n using M2 = map<int, M>; n using u64 = unsigned long long; n using i64 = long long; n using u32 = unsigned int; n using USI = unordered_set<int>; n using USS = unordered_set<string>; n using VUS = vector<USI>; n int main() { n   int n, m; n   cin >> n >> m; n   VI a(n, 1); n   for (int j = 0; j < m; j++) { n     int ind = -1; n     int ind2 = -1; n     bool f = false; n     for (int i = 0; i < n; i++) { n       if (!a[i]) continue; n       ind2 = i; n       int ip1 = (i + 1) % n; n       int im1 = !i ? n - 1 : i - 1; n       int sc = 0; n       if (a[ip1]) sc++; n       if (a[im1]) sc++; n       if (sc == 2) { n         a[i] = 0; n         f = true; n         break; n       } n       if (ind >= 0 && sc == 1) { n         ind = i; n         break; n       } n     } n     if (ind >= 0) { n       a[ind] = 0; n       continue; n     } n     if (!f && ind2 < 0) { n       cout <<  0 n ; n       return 0; n     } n     if (!f) a[ind2] = 0; n   } n   int mgn = 1; n   if (a[0]) a[0] = mgn++; n   for (int i = 1; i < n; i++) { n     if (!a[i]) continue; n     if (a[i - 1]) n       a[i] = a[i - 1]; n     else n       a[i] = mgn++; n   } n   mgn--; n   if (a[0] && a[n - 1] && a[0] != a[n - 1]) { n     mgn--; n   } n   cout << mgn <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, tmp = 1; n int sum(int a) { n   int res = 0; n   while (a) { n     res += a % 10; n     a /= 10; n   } n   return res; n } n int main() { n   cin >> n >> m; n   if (2 * n > 2220) { n     int nn = n / 2 + n % 2; n     for (int i = 1; i <= nn; ++i) { n       cout << 2; n     } n     cout << endl; n     for (int i = 1; i <= nn - 1; ++i) { n       cout << 7; n     } n     cout << 8 << endl; n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     cout << 1; n   } n   cout << endl; n   for (int i = 1; i <= n - 1; ++i) { n     cout << 8; n   } n   cout << 9 << endl; n } n


#include <bits/stdc++.h> n using lli = long long int; n using li = long int; n using namespace std; n const lli MOD{1000000007}; n const lli INF{(false ? (INT_MAX - 1) / 2 : (lli)1e18 + 5)}; n const lli N{200005}; n int main() { n   cout << setprecision(15); n   cin.tie(nullptr)->sync_with_stdio(false); n   li T{1}; n   cin >> T; n   while (T--) { n     lli n{}, x{}, y{}; n     cin >> n >> x >> y; n     lli z{x + y - 1}; n     lli done{n - z}; n     if (done > 0) n       cout << 1 <<    ; n     else n       cout << min(2 - done, n) <<    ; n     done = max((lli)0, done); n     cout << n - done <<   n ; n     if (z <= n) done -= n - z + 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fnc() { n   int n; n   cin >> n; n   cout << n << endl; n   for (int i = 0; i < n; i++) cout << i + 1 <<    ; n   cout << endl; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) fnc(); n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n template <class T> n inline void chkmin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline void chkmax(T &a, T b) { n   if (a < b) a = b; n } n int main() { n   int n, m; n   cin >> n >> m; n   --n; n   int l = 1, r = 1000; n   for (int i = 0; i < m; ++i) { n     int k, first; n     cin >> k >> first; n     chkmax(l, (k + first - 1) / first); n     if (first != 1) chkmin(r, (k - 1) / (first - 1)); n   } n   int f1 = n / l + 1; n   int f2 = n / r + 1; n   if (f1 != f2) n     cout <<  -1 n ; n   else n     cout << f1 <<   n ; n } n


#include <bits/stdc++.h> n int main() { n   int N, gre[101], res[100][101], n, i, j; n   scanf( %d , &N); n   for (i = 0; i < N; i++) { n     for (j = 0; j < N; j++) { n       scanf( %d , &res[i][j]); n     } n     gre[i] = 1; n   } n   n = 0; n   for (i = 0; i < N; i++) { n     for (j = i; j < N; j++) { n       if (res[i][j] == 0) { n         continue; n       } n       if (res[i][j] == 1) { n         gre[i] = 0; n       } n       if (res[i][j] == 2) { n         gre[j] = 0; n       } n       if (res[i][j] == 3) { n         gre[i] = gre[j] = 0; n       } n     } n     if (gre[i]) { n       n++; n     } n   } n   j = 0; n   printf( %d n , n); n   for (i = 0; j < n; i++) { n     if (gre[i]) { n       printf( %d%c , i + 1, j++ == n - 1 ?   n  :    ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Fenwick { n   int n; n   long long tree[200002]; n   void init(int _n) { n     n = _n; n     fill(tree + 1, tree + n + 1, 0LL); n   } n   void upd(int x, long long val) { n     while (x <= n) { n       tree[x] += val; n       x += x & -x; n     } n   } n   long long sum(int x) { n     long long ret = 0; n     while (x) { n       ret += tree[x]; n       x -= x & -x; n     } n     return ret; n   } n } tree; n struct Query { n   int u, v; n   long long cost; n   Query() {} n   Query(int u, int v, long long cost) : u(u), v(v), cost(cost) {} n }; n int n, m; n int par[200002]; n vector<int> child[200002]; n int depth[200002], LCADB[200002][20]; n long long DP[200002]; n int sz[200002]; n vector<Query> queryVec[200002]; n int in[200002], top[200002], idx[200002]; n long long DPSum[200002]; n void makeTree(int x) { n   sz[x] = 1; n   for (auto &y : child[x]) { n     depth[y] = depth[x] + 1; n     makeTree(y); n     sz[x] += sz[y]; n     if (sz[y] > sz[child[x][0]]) swap(child[x][0], y); n   } n } n int in_cnt; n void dfs_hld(int x) { n   in[x] = ++in_cnt; n   idx[in[x]] = x; n   for (auto y : child[x]) { n     top[y] = (child[x][0] == y) ? top[x] : y; n     dfs_hld(y); n   } n } n int getLCA(int x, int y) { n   if (depth[x] < depth[y]) swap(x, y); n   for (int d = 0; d < 20; d++) n     if ((depth[x] - depth[y]) & (1 << d)) x = LCADB[x][d]; n   if (x == y) return x; n   for (int d = 19; d >= 0; d--) n     if (LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d]; n   return par[x]; n } n int kth_parent(int x, int k) { n   for (int d = 0; d < 20; d++) { n     if ((k >> d) & 1) x = LCADB[x][d]; n   } n   return x; n } n long long pathQuery(int x, int topD) { n   long long ret = 0; n   while (depth[x] >= topD) { n     int tp = in[top[x]]; n     int tStart = max(topD, depth[top[x]]) - depth[top[x]] + tp; n     int tEnd = depth[x] - depth[top[x]] + tp; n     ret += tree.sum(tEnd) - tree.sum(tStart - 1); n     x = par[top[x]]; n   } n   return ret; n } n void dfs(int x) { n   long long dpSum = 0; n   for (auto y : child[x]) { n     dfs(y); n     dpSum += DP[y]; n   } n   DPSum[x] = DP[x] = dpSum; n   for (auto y : child[x]) { n     tree.upd(in[y], dpSum - DP[y]); n   } n   for (auto query : queryVec[x]) { n     int tx = query.u, ty = query.v; n     long long tv = query.cost; n     assert(depth[tx] <= depth[ty]); n     long long val; n     if (tx == x) { n       val = dpSum + tv; n       val -= DP[kth_parent(ty, depth[ty] - depth[tx] - 1)]; n       val += pathQuery(ty, depth[x] + 2); n       val += DPSum[ty]; n     } else { n       val = dpSum + tv; n       val -= DP[kth_parent(tx, depth[tx] - depth[x] - 1)]; n       val -= DP[kth_parent(ty, depth[ty] - depth[x] - 1)]; n       val += pathQuery(tx, depth[x] + 2); n       val += pathQuery(ty, depth[x] + 2); n       val += DPSum[tx]; n       val += DPSum[ty]; n     } n     DP[x] = max(DP[x], val); n   } n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &par[i]); n     LCADB[i][0] = par[i]; n     child[par[i]].push_back(i); n   } n   makeTree(1); n   top[1] = 1; n   dfs_hld(1); n   for (int d = 1; d < 20; d++) n     for (int i = 1; i <= n; i++) LCADB[i][d] = LCADB[LCADB[i][d - 1]][d - 1]; n   for (int i = 1; i <= m; i++) { n     Query tmp; n     scanf( %d %d %lld , &tmp.u, &tmp.v, &tmp.cost); n     if (depth[tmp.u] > depth[tmp.v]) swap(tmp.u, tmp.v); n     int z = getLCA(tmp.u, tmp.v); n     queryVec[z].push_back(tmp); n   } n   tree.init(n); n   dfs(1); n   printf( %lld , DP[1]); n } n


#include <bits/stdc++.h> n using namespace std; n long long t[500005], mid; n long long l[500005], r[500005], h[500005]; n struct st { n   long long h, cnt; n }; n stack<st> q; n int main() { n   cin.sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> t[i]; n   q.push({-1, 1}); n   for (int i = 1; i <= n; i++) { n     long long c = 0, h = 0; n     while (q.top().h >= t[i]) { n       c += q.top().cnt; n       h += q.top().cnt * q.top().h; n       q.pop(); n     } n     l[i] = l[i - 1] - h + (t[i] * (c + 1)); n     q.push({t[i], c + 1}); n   } n   while (q.size()) q.pop(); n   q.push({0, 1}); n   for (int i = n; i > 0; i--) { n     long long c = 0, h = 0; n     while (q.top().h >= t[i]) { n       c += q.top().cnt; n       h += q.top().cnt * q.top().h; n       q.pop(); n     } n     r[i] = r[i + 1] - h + (t[i] * (c + 1)); n     q.push({t[i], c + 1}); n   } n   while (q.size()) q.pop(); n   long long ms = 0; n   for (int i = 1; i <= n; i++) n     if (l[i] + r[i] - t[i] > ms) ms = l[i] + r[i] - t[i], mid = i; n   h[mid] = t[mid]; n   for (int i = mid + 1; i <= n; i++) { n     h[i] = min(h[i - 1], t[i]); n   } n   for (int i = mid - 1; i > 0; i--) { n     h[i] = min(h[i + 1], t[i]); n   } n   for (int i = 1; i <= n; i++) cout << h[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, t; n void read_input() { cin >> a >> b; } n void solve() { n   int res = abs(a - b); n   int ans = res / 5; n   res = res % 5; n   ans += res / 2; n   res %= 2; n   ans += res; n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> t; n   while (t--) read_input(), solve(); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n long long a[1000010]; n bool cmp(long long a,long long b) n { n  return a>b; n } n int main() { n  n  long long m,n,i,j,x,y,suma=0,sumb=0; n  cin>>n; n  while(n--) n  { n   cin>>m; n   for(i=0;i<m;i++) n   cin>>a[i]; n   sort(a,a+m,cmp); n   for(i=0;i<m;) n   { n   if(a[i]%2==0) n   suma+=a[i]; n   i++; n   if(a[i]%2!=0) n   sumb+=a[i]; n   i++; n    n  } n   if(sumb==suma) n   cout<< Tie <<endl; n   if(suma>sumb) n   cout<< Alice <<endl; n   if(suma<sumb) n   cout<< Bob <<endl; n   suma=0; n   sumb=0; n  } n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int ar[n + 2], tr1[n + 2], tr2[n + 2], kl[k + 2][102]; n   memset(kl, 0, sizeof(kl)); n   vector<pair<int, int>> vec; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     tr1[i] = ar[i]; n     tr2[i] = ar[i]; n   } n   sort(ar, ar + n); n   sort(tr1, tr1 + n); n   if (ar[n - 1] - ar[0] > k) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   int mn = 101; n   for (int i = 0; i < k; i++) { n     int j = 0; n     for (; j < n; j++) { n       if (ar[j] > 0) { n         mn = ar[j]; n         break; n       } n     } n     for (; j < n; j++) { n       if (i == 0) { n         kl[i][j] = min(mn + 1, ar[j]); n         ar[j] -= min(mn + 1, ar[j]); n       } else { n         kl[i][j] = 1; n         ar[j] -= 1; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     int f = tr2[i]; n     int zz; n     for (int z = 0; z < n; z++) { n       if (tr1[z] == f) { n         zz = z; n         break; n       } n     } n     for (int j = 0; j < k; j++) { n       if (kl[j][zz] == 0) { n         continue; n       } n       for (int l = 0; l < kl[j][zz]; l++) { n         cout << j + 1 <<    ; n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void sol(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   long long ml = 1, ans = 0; n   vector<int> ls(n); n   map<long long, int> pre, mp; n   mp[0] = 1; n   vector<long long> an; n   while (ml < 1e15) { n     if (!pre[ml]) n       an.push_back(ml), pre[ml] = 1; n     else n       break; n     ml *= k; n     if ((ml > 1e15 || -ml > 1e15) || (ml % k != 0)) break; n   } n   for (int &x : ls) cin >> x; n   long long sm = 0ll; n   for (int x : ls) { n     sm += x; n     for (long long y : an) n       if (mp[sm - y]) ans += mp[sm - y]; n     mp[sm]++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n int n, bin[35], f[35][35][2]; n void updata(int &x, int y) { n   x += y; n   x -= x >= MOD ? MOD : 0; n } n int main() { n   scanf( %d , &n); n   bin[0] = 1; n   for (int i = 1; i <= 30; i++) bin[i] = bin[i - 1] * 2; n   for (int i = 30; i >= 0; i--) n     if (n & bin[i]) { n       f[i][1][1] = f[i][0][0] = 1; n       break; n     } n   for (int i = 30; i >= 1; i--) n     for (int j = 0; j <= 30; j++) { n       if (f[i][j][0]) { n         updata(f[i - 1][j + 1][0], f[i][j][0]); n         updata(f[i - 1][j][0], (long long)f[i][j][0] * bin[j] % MOD); n       } n       if (f[i][j][1]) { n         if (n & bin[i - 1]) { n           updata(f[i - 1][j + 1][1], f[i][j][1]); n           updata(f[i - 1][j][0], (long long)f[i][j][1] * bin[j - 1] % MOD); n           updata(f[i - 1][j][1], (long long)f[i][j][1] * bin[j - 1] % MOD); n         } else n           updata(f[i - 1][j][1], (long long)f[i][j][1] * bin[j - 1] % MOD); n       } n     } n   int ans = 0; n   if (!n) ans++; n   for (int i = 0; i <= 30; i++) n     updata(ans, f[0][i][0]), updata(ans, f[0][i][1]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void redondear(string &s, int pto, int idx) { n   if (idx == (int)(s.size())) { n     cout << s << endl; n     return; n   } n   s.resize(idx); n   bool unoAdelante = false; n   for (int i = (idx)-1; i >= (int)(0); i--) { n     if (i == pto) { n       continue; n     } n     if (i == 0 && s[i] ==  9 ) { n       s[i] =  0 ; n       unoAdelante = true; n       continue; n     } n     if (s[i] ==  9 ) { n       s[i] =  0 ; n     } else { n       s[i]++; n       break; n     } n   } n   if (unoAdelante) cout <<  1 ; n   if (s[s.size() - 1] ==  . ) { n     s.resize(s.size() - 1); n   } n   cout << s << endl; n } n int main() { n   int largo, segundos; n   cin >> largo >> segundos; n   string nota; n   cin >> nota; n   int punto = 0; n   for (int i = (0); i < (int)(nota.size()); i++) { n     if (nota[i] ==  . ) { n       punto = i; n       break; n     } n   } n   int minimoIndice = largo; n   int turnosUsados = 0; n   for (int i = (largo)-1; i >= (int)(punto + 1); i--) { n     if (nota[i] >=  5 ) { n       minimoIndice = i; n       turnosUsados = 1; n     } else if (nota[i] ==  4  && turnosUsados < segundos && turnosUsados > 0) { n       minimoIndice = i; n       turnosUsados++; n     } else { n       turnosUsados = 0; n     } n   } n   redondear(nota, punto, minimoIndice); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int l, r; n void did(int &x, int y) { n   printf( ? %d n , y); n   fflush(stdout); n   scanf( %d , &x); n } n int main() { n   scanf( %d , &n); n   if ((n / 2) & 1) { n     printf( ! -1 ); n     fflush(stdout); n     return 0; n   } n   while (1) { n     printf( ? 1 n ); n     fflush(stdout); n     int x, y; n     int l = 1, r = n / 2; n     scanf( %d , &x); n     printf( ? %d n , n / 2 + 1); n     fflush(stdout); n     scanf( %d , &y); n     if (x == y) { n       printf( ! %d n , 1); n       fflush(stdout); n       return 0; n     } n     int xx = x, yy = y; n     while (l <= r) { n       int mid = (l + r) / 2; n       did(xx, mid); n       did(yy, mid + n / 2); n       if (xx == yy) { n         printf( ! %d n , mid); n         fflush(stdout); n         return 0; n       } n       if ((xx < yy) == (x < y)) n         l = mid + 1; n       else n         r = mid - 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 3010; n int n, x[M]; n char d[M][6]; n int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1}; n vector<long long> X, Y; n int g[M][M], fw1, fw2; n queue<pair<int, int> > Q; n const long long INF = 20000000005LL, MINF = -INF; n struct point { n   long long lasx, lasy, nowx, nowy; n   point() {} n   point(int _lasx, int _lasy, int _nowx, int _nowy) n       : lasx(_lasx), lasy(_lasy), nowx(_nowx), nowy(_nowy) {} n } ways[M]; n inline int renumber(int x, vector<long long> _find) { n   return find(_find.begin(), _find.end(), x) - _find.begin(); n } n inline void BFS() { n   while (!Q.empty()) Q.pop(); n   Q.push(make_pair(0, 0)); n   g[0][0] = 2; n   fw1 = X.size(), fw2 = Y.size(); n   while (!Q.empty()) { n     pair<int, int> top = Q.front(); n     Q.pop(); n     for (int i = 0; i < 4; ++i) { n       pair<int, int> x; n       x.first = top.first + dx[i], x.second = top.second + dy[i]; n       if (x.first >= 0 && x.first < fw1 && x.second >= 0 && x.second < fw2 && n           g[x.first][x.second] == 0) { n         g[x.first][x.second] = 2; n         Q.push(x); n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   long long nowx, nowy; n   nowx = nowy = 0; n   X.push_back(MINF); n   X.push_back(INF); n   Y.push_back(MINF); n   Y.push_back(INF); n   for (int i = 1; i <= n; ++i) { n     int face; n     long long tx, ty; n     scanf( %s%d , d[i], &x[i]); n     if (d[i][0] ==  R ) face = 0; n     if (d[i][0] ==  L ) face = 1; n     if (d[i][0] ==  D ) face = 2; n     if (d[i][0] ==  U ) face = 3; n     tx = nowx + dx[face] * x[i]; n     ty = nowy + dy[face] * x[i]; n     int Xmin = min(nowx, tx), Xmax = max(nowx, tx) + 1; n     int Ymin = min(nowy, ty), Ymax = max(nowy, ty) + 1; n     X.push_back(Xmin); n     X.push_back(Xmax); n     Y.push_back(Ymin); n     Y.push_back(Ymax); n     ways[i] = point(Xmin, Ymin, Xmax, Ymax); n     nowx = tx, nowy = ty; n   } n   sort(X.begin(), X.end()); n   sort(Y.begin(), Y.end()); n   X.resize(unique(X.begin(), X.end()) - X.begin()); n   Y.resize(unique(Y.begin(), Y.end()) - Y.begin()); n   for (int i = 1; i <= n; ++i) { n     int tx1 = renumber(ways[i].lasx, X), tx2 = renumber(ways[i].nowx, X), n         ty1 = renumber(ways[i].lasy, Y), ty2 = renumber(ways[i].nowy, Y); n     for (int j = tx1; j < tx2; ++j) n       for (int k = ty1; k < ty2; ++k) g[j][k] = 1; n   } n   BFS(); n   long long ans = 0; n   for (int i = 0; i <= fw1 - 2; ++i) n     for (int j = 0; j <= fw2 - 2; ++j) n       if (g[i][j] != 2) n         ans = ans + (long long)(X[i + 1] - X[i]) * (Y[j + 1] - Y[j]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 60; n int n, l; n char s[maxn][maxn], t[maxn]; n bool eq(int x, int i, int j) { n   for (int k = 0; k < l; k++) { n     if (s[x][i] != t[j]) return false; n     i = (i + 1) % l; n     j = (j + 1) % l; n   } n   return true; n } n int dist(int x, int j) { n   for (int i = 0; i < l; i++) n     if (eq(x, i, j)) return i; n   return -1; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   l = strlen(s[0]); n   strcpy(t, s[0]); n   int ans = 0x3f3f3f3f; n   for (int j = 0; j < l; j++) { n     int t = 0; n     for (int x = 0; x < n; x++) { n       int d = dist(x, j); n       if (d == -1) { n         printf( -1 n ); n         return 0; n       } n       t += d; n     } n     if (t < ans) ans = t; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, p[1000001]; n long long ans; n int main() { n   scanf( %d%d , &n, &k); n   if (k == 1) { n     printf( 3 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) p[i] = i; n   for (int i = 2; i <= n; i++) n     if (p[i] == i) n       for (int j = i; j <= n; j += i) p[j] -= p[j] / i; n   sort(p + 1, p + 1 + n); n   for (int i = 1; i <= k + 2; i++) ans += p[i]; n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[10010]; n int dp[1 << 18]; n int bad[1 << 18]; n int g[100][100]; n int tmp[1 << 18]; n string s; n int get(int l, int r) { n   int t = 0; n   for (int i = l; i <= r; ++i) { n     t |= 1 << (s[i] -  a ); n   } n   return t; n } n int main() { n   int n, p; n   scanf( %d%d , &n, &p); n   cin >> s; n   memset(num, 0, sizeof(num)); n   memset(g, 0, sizeof(g)); n   memset(bad, 0, sizeof(bad)); n   for (int i = 0; i < s.size(); ++i) { n     num[s[i] -  a ]++; n   } n   for (int i = 0; i < p; ++i) { n     for (int j = 0; j < p; ++j) { n       scanf( %d , &g[i][j]); n     } n   } n   for (int i = 0; i < p; ++i) { n     for (int j = 0; j < p; ++j) { n       if (g[i][j]) continue; n       memset(tmp, 0, sizeof(tmp)); n       int l = -1; n       for (int k = 0; k < s.size(); ++k) { n         if (s[k] -  a  == i || s[k] -  a  == j) { n           if (l == -1) { n             l = k; n           } else { n             int a = s[l] -  a , b = s[k] -  a ; n             if ((a == i || a == j) && (b == i || b == j)) { n               if ((a == i && b == j) || (b == j && a == i)) { n                 int m = get(l + 1, k - 1); n                 tmp[m] = 1; n               } n               l = k; n             } n           } n         } n       } n       for (int ii = 0; ii < (1 << p); ++ii) { n         if ((ii & (1 << i)) || (ii & (1 << j))) continue; n         for (int k = 0; k < p; ++k) { n           if (k == i || k == j) continue; n           tmp[ii | (1 << k)] |= tmp[ii]; n         } n         if (tmp[ii]) bad[ii] = tmp[ii]; n       } n     } n   } n   memset(dp, 0, sizeof(dp)); n   dp[0] = 1; n   int ans = 0; n   for (int i = 0; i < (1 << p); ++i) { n     if (bad[i]) continue; n     for (int j = 0; j < p; ++j) { n       if ((1 << j) & i) { n         if (dp[i ^ (1 << j)]) dp[i] = 1; n       } n     } n     if (dp[i]) { n       int s = 0; n       for (int j = 0; j < p; ++j) { n         if ((1 << j) & i) s += num[j]; n       } n       ans = max(ans, s); n     } n   } n   printf( %d n , n - ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   scanf( %lld , &n); n   if (n == 1 || n == 2) { n     puts( -1 ); n     return 0; n   } n   if (n & 1) { n     n -= 1; n     n /= 2; n     printf( %lld %lld n , 2 * n * n + 2 * n, 2 * n * n + 2 * n + 1); n   } else { n     n /= 2; n     printf( %lld %lld n , n * n - 1, n * n + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s, a, b, c; n   cin >> s >> a >> b >> c; n   double x = 0.0, y = 0.0, z = 0.0; n   if (a || b || c) { n     x = s * a / (double)(a + b + c); n     y = s * b / (double)(a + b + c); n     z = s * c / (double)(a + b + c); n   } n   printf( %.12f %.12f %.12f , x, y, z); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0; n   int f = 1, ch = getchar(); n   for (; !isdigit(ch) && ch != EOF; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^  0 ); n   return x * f; n } n const int N = 500005; n int n, La, Lb, Lc, smx[N]; n struct card { n   int b, c; n }; n vector<card> d[N]; n long long ans, sum[N]; n int main() { n   n = read(), La = read(), Lb = read(), Lc = read(); n   for (register int i = 1; i <= n; ++i) { n     int a = min(La, read()), b = min(Lb, read()), c = min(Lc, read()); n     d[a].push_back((card){b, c}); n     smx[b] = max(smx[b], c); n   } n   for (register int i = Lb - 1; i; --i) smx[i] = max(smx[i], smx[i + 1]); n   for (register int i = 1; i <= Lb; ++i) sum[i] = sum[i - 1] + smx[i]; n   int mxb = 0, mxc = 0, k = Lb + 1; n   for (register int x = La; x; --x) { n     for (register int i = 0; i < d[x].size(); ++i) { n       int y = d[x][i].b, z = d[x][i].c; n       mxb = max(mxb, y), mxc = max(mxc, z); n       while (k > mxb + 1 && smx[k - 1] < mxc) --k; n     } n     k = max(k, mxb + 1); n     ans += 1ll * (Lc - mxc) * (Lb - k + 1) + 1ll * Lc * (k - mxb - 1) - n            sum[k - 1] + sum[mxb]; n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a; n   cin >> b; n   int lena = a.length(), lenb = b.length(); n   if (lena != lenb) n     cout <<  NO ; n   else { n     int count = 0; n     char old1, old2; n     for (int i = 0; i < lena && count < 3; i++) { n       if (a[i] != b[i]) { n         count++; n         if (count == 1) { n           old1 = b[i]; n           old2 = a[i]; n         } n         if (count == 2 && (a[i] != old1 || b[i] != old2)) count = 3; n       } n     } n     if (count == 2) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n   return 0; n } n


#include<bits/stdc++.h> n namespace my_std{ n  using namespace std; n  #define pii pair<int,int> n  #define fir first n  #define sec second n  #define MP make_pair n  #define rep(i,x,y) for (int i=(x);i<=(y);i++) n  #define drep(i,x,y) for (int i=(x);i>=(y);i--) n  #define go(x) for (int i=head[x];i;i=edge[i].nxt) n  #define templ template<typename T> n  #define sz 111 n  #define mod 1000000007ll n  typedef long long ll; n  typedef double db; n  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);} n  templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;} n  templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;} n  templ inline void read(T& t) n  { n   t=0;char f=0,ch=getchar();double d=0.1; n   while(ch> 9 ||ch< 0 ) f|=(ch== - ),ch=getchar(); n   while(ch<= 9 &&ch>= 0 ) t=t*10+ch-48,ch=getchar(); n   if(ch== . ){ch=getchar();while(ch<= 9 &&ch>= 0 ) t+=d*(ch^48),d*=0.1,ch=getchar();} n   t=(f?-t:t); n  } n  template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);} n  char __sr[1<<21],__z[20];int __C=-1,__zz=0; n  inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;} n  inline void print(int x) n  { n   if(__C>1<<20)Ot();if(x<0)__sr[++__C]= - ,x=-x; n   while(__z[++__zz]=x%10+48,x/=10); n   while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]=  n ; n  } n  void file() n  { n   #ifdef NTFOrz n   freopen( a.in , r ,stdin); n   #endif n  } n  inline void chktime() n  { n   #ifdef NTFOrz n   cerr<<clock()/1000.0<<  n ; n   #endif n  } n  #ifdef mod n  ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;} n  ll inv(ll x){return ksm(x,mod-2);} n  #else n  ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;} n  #endif n // inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;} n } n using namespace my_std; n  n int n; n int c[sz],b[sz],B[sz]; n  n ll pre[sz][sz*sz],pr; n void init() n { n  pre[0][0]=1; n  rep(i,0,n-1) rep(k,0,100*i) rep(t,0,c[i+1]) (pre[i+1][k+t]+=pre[i][k])%=mod; n  pr=1; rep(i,1,n) pr=pr*(c[i]+1)%mod; n } n  n ll dp[sz][sz*sz]; n ll work(int x) n { n  static int lim[sz]; n  rep(i,1,n) lim[i]=i*x+B[i]; n  int p=1; while (p<=n&&lim[p]<=0) ++p; if (p>n) return pr; n  rep(i,p,n) rep(j,0,i*100) dp[i][j]=0; rep(i,lim[p],p*100) dp[p][i]=pre[p][i]; n  rep(i,p,n-1) n  { n   rep(k,lim[i],i*100) { int l=k,r=k+c[i+1]; if (r<lim[i+1]) continue; chkmax(l,lim[i+1]); dp[i+1][l]+=dp[i][k],dp[i+1][r+1]+=mod-dp[i][k]; } n   dp[i+1][lim[i+1]]%=mod; n   rep(k,lim[i+1]+1,(i+1)*100) (dp[i+1][k]+=dp[i+1][k-1])%=mod; n  } n  ll res=0; rep(i,lim[n],n*100) res+=dp[n][i]; return res%mod; n } n  n int main() n { n  file(); n  read(n); n  rep(i,1,n) read(c[i]); n  rep(i,2,n) read(b[i]),B[i]=b[i]+B[i-1]; rep(i,2,n) B[i]+=B[i-1]; n  init(); n  int Q; read(Q); map<int,ll>vis; n  while (Q--) n  { n   int x; read(x); n   if (x>100) { puts( 0 ); continue; } n   if (x<-6000) { printf( %lld n ,pr); continue; } n   if (vis.count(x)) { printf( %lld n ,vis[x]); continue; } n   ll ans=work(x); printf( %lld n ,vis[x]=ans); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n string s; n int ans; n map<int, int> f; n int main() { n   cin >> s; n   int n = s.length(); n   f[0] = 1; n   int cur = 0; n   for (int i = 1; i < n; i++) { n     if (s[i] == s[i - 1]) { n       f[cur]++; n     } else { n       f[i]++; n       cur = i; n     } n   } n   while (f.size() > 1) { n     int _min = 10000000; n     for (auto i = f.begin(); i != f.end(); ++i) { n       if (i == f.begin() || next(i) == f.end()) n         cur = i->second; n       else n         cur = ceil(i->second / 2.0); n       _min = min(_min, cur); n     } n     ans += _min; n     for (auto i = f.begin(); i != f.end(); ++i) { n       cur = _min; n       auto ci = i; n       if (i == f.begin() || next(i) == f.end()) { n         i->second -= cur; n       } else { n         i->second -= cur + cur; n       } n       ci = i; n     } n     for (auto i = f.begin(); i != f.end();) { n       auto ci = i; n       ++i; n       if (ci->second < 1) f.erase(ci); n     } n     if (f.size()) n       for (auto it = f.begin(); it != prev(f.end());) { n         if (s[it->first] == s[next(it)->first]) { n           next(it)->second += it->second; n           auto cit = it; n           ++it; n           f.erase(cit); n         } else n           ++it; n       } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long double, long double> a[100005]; n long double isum = 0, min1 = 1000000000000, isum1, t[100005], n             min2 = 1000000000000, min3 = 1000000000000, min4 = 1000000000000, n             min5 = 1000000000000; n int i, j; n long long ax, ay, bx, by, tx, ty, n, x[100005], y[100005], h, k; n bool used[100005]; n int main() { n   cin >> ax >> ay >> bx >> by >> tx >> ty >> n; n   for (i = 0; i < 100005; i++) used[i] = 0; n   for (i = 0; i < n; i++) { n     cin >> x[i]; n     cin >> y[i]; n   } n   a[n].first = 0; n   a[n].second = 0; n   for (i = 0; i < n; i++) { n     t[i] = sqrt((tx - x[i]) * (tx - x[i]) + (ty - y[i]) * (ty - y[i])); n     a[i].first = n         sqrt((ax - x[i]) * (ax - x[i]) + (ay - y[i]) * (ay - y[i])) - t[i]; n     a[i].second = n         sqrt((bx - x[i]) * (bx - x[i]) + (by - y[i]) * (by - y[i])) - t[i]; n     isum += 2 * t[i]; n   } n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     min2 = min(min2, a[i].first); n   } n   for (i = 0; i < n; i++) { n     if (a[i].first == min2) { n       h = i; n       break; n     } n   } n   for (i = 0; i < n; i++) { n     if (i != h) min3 = min(min3, a[i].first); n   } n   for (i = 0; i < n; i++) { n     min4 = min(min4, a[i].second); n   } n   for (i = 0; i < n; i++) { n     if (a[i].second == min4) { n       k = i; n       break; n     } n   } n   for (i = 0; i < n; i++) { n     if (i != k) min5 = min(min5, a[i].second); n   } n   if (h != k) { n     min1 = min(min1, min2 + min4); n   } else { n     min1 = min(min1, min2 + min5); n     min1 = min(min1, min3 + min4); n     min1 = min(min1, min3 + min5); n   } n   for (i = 0; i < n; i++) min1 = min(a[i].first, min1); n   for (i = 0; i < n; i++) min1 = min(a[i].second, min1); n   cout << fixed << setprecision(12) << isum + min1; n } n


#include <bits/stdc++.h> n using namespace std; n const char nl =   n ; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, v1, v2, t1, t2; n   cin >> n >> v1 >> v2 >> t1 >> t2; n   if (n * v1 + t1 * 2 < n * v2 + t2 * 2) n     cout <<  First  << nl; n   else if (n * v1 + t1 * 2 > n * v2 + t2 * 2) n     cout <<  Second  << nl; n   else n     cout <<  Friendship  << nl; n } n


#include <bits/stdc++.h> n using namespace std; n void swap(long long &a, long long &b) { n   auto tm = a; n   a = b; n   b = tm; n } n const long long mod = 1000000009; n const long long mod2 = 998244353; n const long long INF = (1LL << 20) - 1; n const long long N = 2e5 + 25; n long long n, m, a, b; n vector<long long> v[N]; n set<long long> c, d; n bool vt[N]; n void dfs1(long long x) { n   vt[x] = 1; n   for (auto &(i) : (v[x])) { n     if (i == b) { n       c.insert(x); n       continue; n     } n     if (vt[i] == 0) dfs1(i); n   } n } n void dfs2(long long x) { n   vt[x] = 1; n   for (auto &(i) : (v[x])) { n     if (i == a) { n       d.insert(x); n       continue; n     } n     if (vt[i] == 0) dfs2(i); n   } n } n void bheja_fry() { n   c.clear(); n   d.clear(); n   cin >> n >> m >> a >> b; n   for (long long(i) = (0); (i) < (n + 1); ++(i)) { n     v[i].clear(); n     vt[i] = 0; n   } n   long long x, y; n   for (long long(i) = (0); (i) < (m); ++(i)) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   long long ct1 = 0, ct2 = 0; n   dfs1(a); n   for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0; n   dfs2(b); n   queue<long long> q; n   for (auto &(i) : (v[b])) { n     if (c.find(i) == c.end()) q.push(i); n   } n   for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0; n   vt[b] = 1; n   while (!q.empty()) { n     x = q.front(); n     q.pop(); n     if (vt[x]) continue; n     ct1++; n     vt[x] = 1; n     for (auto &(i) : (v[x])) { n       if (vt[i] == 0) q.push(i); n     } n   } n   for (auto &(i) : (v[a])) { n     if (d.find(i) == d.end()) q.push(i); n   } n   for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0; n   vt[a] = 1; n   while (!q.empty()) { n     x = q.front(); n     q.pop(); n     if (vt[x]) continue; n     ct2++; n     vt[x] = 1; n     for (auto &(i) : (v[x])) { n       if (vt[i] == 0) q.push(i); n     } n   } n   cout << ct1 * ct2; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   for (long long(i) = (1); (i) < (t + 1); ++(i)) { n     bheja_fry(); n     if (i < t) cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n void rd(int &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n void lrd(long long &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n const int INF = 1e9; n const long long LINF = 1e18; n const int inf = 1e5 + 10; n using namespace std; n int T; n long long n; n int m, a[inf]; n int cnt[100]; n map<int, int> S; n int sta[100], top; n int main() { n   rd(T); n   int now = 1; n   for (int i = 0; i <= 30; i++) S[now] = i, now *= 2; n   while (T--) { n     lrd(n); n     rd(m); n     memset(cnt, 0, sizeof(cnt)); n     long long sum = 0; n     for (int i = 1; i <= m; i++) rd(a[i]), cnt[S[a[i]]]++, sum += a[i]; n     if (sum < n) n       printf( -1 n ); n     else { n       int now = 0; n       top = 0; n       while (n) { n         if (n & 1) { n           if (cnt[now]) n             cnt[now]--; n           else n             sta[++top] = now; n         } n         now++; n         n /= 2; n       } n       int ans = 0; n       for (int i = 1; i <= top;) { n         for (int j = 0; j < sta[i]; j++) cnt[j + 1] += cnt[j] / 2, cnt[j] %= 2; n         for (int j = sta[i]; j <= 99; j++) { n           if (cnt[j]) { n             ans += j - sta[i]; n             cnt[j]--; n             for (int k = sta[i]; k < j; k++) cnt[k]++; n             while (i <= top && sta[i] <= j) i++; n             break; n           } n         } n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define lp(i,a,n) for(i=a;i<n;i++) n #define ld(i,a,n) for(i=n-1;i>=a;i--) n #define ll long long n #define dd double n #define mod 10000007 n #define inf 1e18 n #define inp(t) cin>>t; while(t--) n int main(){ n  int t; n  ios::sync_with_stdio(false); n  inp(t){ n   ll n,i,j,k; n   cin>>n; n   int arr[n]; n   lp(i,0,n) n   cin>>arr[i]; n   int ans; n   int sm=accumulate(arr,arr+n,0); n   int chk=1; n   lp(i,0,n){ n    k=n-i; n    int val; n    if(sm%k==0) n    val=sm/k; n    else n    continue; n    int smm=0; n    lp(j,0,n){ n     smm+=arr[j]; n     if(smm<val) n     continue; n     else if(smm==val&&j!=n-1){ n      smm=0; n      continue; n     } n     else if(smm==val&&j==n-1){ n      chk=0; n      ans=i; n      break; n     } n     else{ n      smm=0; n      break; n     } n      n      n    } n    if(chk==0) n     break; n   } n   cout<<ans<<endl; n    n   } n    n   }  n


#include <bits/stdc++.h> n using namespace std; n int n; n double V1; n double V2[100005]; n double L[100005]; n double D[100005]; n double X[100005]; n bool check(double T) { n   bool ok1 = false; n   bool ok1b = false; n   bool ok2 = false; n   bool ok2b = false; n   double l = 0; n   double r = 1000000; n   vector<pair<double, double> > V; n   V.clear(); n   for (int i = 1; i <= n; i++) { n     if (D[i] == 1 && !ok1) { n       if (L[i] <= V2[i] * T) { n         ok1 = true; n       } else { n         double TS = T - L[i] / (V1 + V2[i]); n         double MS = 1 / (V1 - V2[i]) - V2[i] / (V1 * V1 - V2[i] * V2[i]); n         if (TS < 0) continue; n         ok1b = true; n         double x; n         if (TS > MS * 10000000) n           x = 1000000; n         else n           x = TS / MS; n         if (x < 1) x = 0; n         if (X[i] > l) l = X[i]; n         if (X[i] + x < r) r = X[i] + x; n         V.push_back(make_pair(X[i], X[i] + x)); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (D[i] == 2 && !ok2) { n       if (L[i] <= V2[i] * T) { n         ok2 = true; n       } else { n         double TS = T - L[i] / (V1 + V2[i]); n         double MS = 1 / (V1 - V2[i]) - V2[i] / (V1 * V1 - V2[i] * V2[i]); n         if (TS < 0) continue; n         ok2b = true; n         double x; n         if (TS > MS * 10000000) n           x = 1000000; n         else n           x = TS / MS; n         if (x < 1) x = 0; n         if (X[i] - x > l) l = X[i] - x; n         if (X[i] < r) r = X[i]; n         for (int j = 0; j < V.size(); j++) n           if ((V[j].first <= X[i] && X[i] <= V[j].second) || n               (V[j].first <= (X[i] - x) && (X[i] - x) <= V[j].second)) { n             return true; n           } n       } n     } n   } n   return ((ok1 && ok2b) || (ok2 && ok1b) || (ok1 && ok2)); n } n int main() { n   cin >> n >> V1; n   for (int i = 1; i <= n; i++) { n     cin >> X[i] >> V2[i] >> D[i]; n     if (D[i] == 1) n       L[i] = X[i]; n     else n       L[i] = 1000000 - X[i]; n   } n   double l = 0; n   double r = 10000000; n   while (0.0000001 <= r - l) { n     double m = (l + r) / 2; n     if (check(m)) n       r = m - 0.00000001; n     else n       l = m + 0.00000001; n   } n   printf( %0.8f n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> pat[100000]; n int main() { n   int num, way; n   scanf( %d%d , &num, &way); n   set<pair<int, int> > se; n   for (int i = 0; i < way; i++) { n     int za, zb; n     scanf( %d%d , &za, &zb); n     za--; n     zb--; n     pat[za].push_back(zb); n     pat[zb].push_back(za); n     se.insert(make_pair(za, zb)); n     se.insert(make_pair(zb, za)); n   } n   if (num <= 9) { n     vector<int> vec; n     for (int i = 0; i < num; i++) { n       vec.push_back(i); n     } n     for (;;) { n       bool f = true; n       for (int i = 0; i < way; i++) { n         if (se.find(make_pair(vec[i], vec[(i + 1) % num])) != se.end()) { n           f = false; n           break; n         } n       } n       if (f) { n         for (int i = 0; i < way; i++) { n           printf( %d %d n , vec[i] + 1, vec[(i + 1) % num] + 1); n         } n         return 0; n       } n       if (!next_permutation(vec.begin(), vec.end())) { n         printf( -1 n ); n         return 0; n       } n     } n   } n   vector<pair<int, int> > ans; n   int now = 0; n   set<int> rem; n   for (int i = 1; i < num; i++) { n     rem.insert(i); n   } n   for (int i = 0; i < num - 9; i++) { n     set<int>::iterator it = rem.begin(); n     for (;;) { n       if (se.find(make_pair(now, (*it))) == se.end()) { n         ans.push_back(make_pair(now, (*it))); n         now = *it; n         rem.erase(it); n         break; n       } n       it++; n     } n   } n   set<int>::iterator it = rem.begin(); n   vector<int> vec; n   for (int i = num - 8; i < num; i++) { n     vec.push_back((*it)); n     it++; n   } n   for (;;) { n     bool f = true; n     for (int i = 0; i < 7; i++) { n       if (se.find(make_pair(vec[i], vec[i + 1])) != se.end()) { n         f = false; n         break; n       } n     } n     if (se.find(make_pair(ans[ans.size() - 1].second, vec[0])) != se.end() || n         se.find(make_pair(vec[vec.size() - 1], ans[0].first)) != se.end()) { n       f = false; n     } n     if (f) { n       ans.push_back(make_pair(ans[ans.size() - 1].second, vec[0])); n       for (int i = 0; i < 7; i++) { n         ans.push_back(make_pair(vec[i], vec[i + 1])); n       } n       ans.push_back(make_pair(vec[vec.size() - 1], ans[0].first)); n       break; n     } n     if (!next_permutation(vec.begin(), vec.end())) { n       abort(); n       return 0; n     } n   } n   for (int i = 0; i < way; i++) { n     printf( %d %d n , ans[i].first + 1, ans[i].second + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s1, s2; n int a, b; n int nxt[205], cnt[205]; n int main() { n   scanf( %d%d , &a, &b); n   cin >> s1 >> s2; n   for (int i = 0; i < s2.size(); i++) { n     int pos = i; n     for (int r = 0; r < s1.size(); r++) { n       if (s2[pos] == s1[r]) { n         pos++; n         if (pos == s2.size()) cnt[i]++, pos = 0; n       } n     } n     nxt[i] = pos; n   } n   int ans = 0; n   int poss = 0; n   for (int i = 1; i <= a; i++) ans += cnt[poss], poss = nxt[poss]; n   cout << ans / b; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 500005; n const int MC = 2; n int get(char c) { n   if (c ==  ( ) return 0; n   return 1; n } n struct Node { n   int l, r, pardp; n   Node *par, *sLink; n   Node *chd[MC]; n   bool isLeaf; n   int st; n   Node() { n     l = r = pardp = 0; n     par = sLink = NULL; n     st = 0; n     isLeaf = true; n     memset(chd, 0, sizeof chd); n   } n   int len() { return r - l; } n   int depth() { return pardp + len(); } n   bool inEdge(int pos) { return pos >= pardp && pos < depth(); } n   void setEdge(Node *child, int l, int r, char *S) { n     child->par = this; n     child->pardp = depth(); n     chd[get(S[l])] = child; n     child->l = l, child->r = r; n     isLeaf = false; n   } n   void print(char *S) { n     for (int i = l; i < r; i++) putchar(S[i]); n     puts(  ); n   } n }; n struct Data { n   int l, r, x; n   Data(int l = 0, int r = 0, int x = 0) : l(l), r(r), x(x) {} n   bool operator<(const Data &b) const { return l < b.l; } n }; n vector<Data> que; n struct STree { n   Node *root, *needSLink, *cur; n   int jj, m, size; n   bool needWalk; n   vector<Node> nodes; n   char *S; n   STree(char *str) { n     m = strlen(str); n     S = str; n     nodes.reserve(m * 2 + 10); n     jj = 1, size = 0; n     root = newNode(); n     cur = newNode(); n     root->setEdge(cur, 0, m, S); n     needSLink = NULL; n     needWalk = true; n     for (int i = 0; i < m - 1; i++) extend(i); n   } n   Node *newNode() { n     nodes.push_back(Node()); n     return &nodes[size++]; n   } n   Node *walk_down(Node *c, int j, int i) { n     if (i - j + 1 > 0) n       for (int h = j + c->depth(); !c->inEdge(i - j); h += c->len()) n         c = c->chd[get(S[h])]; n     return c; n   } n   void addSLink(Node *c) { n     if (needSLink) needSLink->sLink = c; n     needSLink = NULL; n   } n   void extend(int i) { n     char c = S[i + 1]; n     Node *leaf, *split; n     int k, pos; n     for (; jj <= i + 1; jj++) { n       if (needWalk) { n         if (!cur->sLink && cur->par) cur = cur->par; n         cur = (cur->sLink) ? cur->sLink : root; n         cur = walk_down(cur, jj, i); n       } n       needWalk = true; n       k = i + 1 - jj; n       if (cur->depth() == k) { n         addSLink(cur); n         if (cur->chd[get(c)]) { n           cur = cur->chd[get(c)]; n           needWalk = false; n           break; n         } else { n           leaf = newNode(); n           cur->setEdge(leaf, i + 1, m, S); n         } n       } else { n         pos = cur->l + k - cur->pardp; n         if (S[pos] == c) { n           addSLink(cur); n           needWalk = false; n           break; n         } else { n           leaf = newNode(); n           split = newNode(); n           cur->par->setEdge(split, cur->l, pos, S); n           split->setEdge(cur, pos, cur->r, S); n           split->setEdge(leaf, i + 1, m, S); n           addSLink(split); n           if (split->depth() == 1) n             split->sLink = root; n           else n             needSLink = split; n           cur = split; n         } n       } n     } n   } n   void calc(Node *c) { n     int bf = 0; n     int l = c->l - c->pardp, r = c->r; n     if (r) que.push_back(Data(l, r, 1)); n     for (int i = 0; i < MC; i++) n       if (c->chd[i]) { n         if (r) que.push_back(Data(l, r, -1)); n         calc(c->chd[i]); n         bf++; n       } n   } n }; n char A[MX]; n int val[MX * 4], cnt[MX * 4], dn[MX * 4]; n int p[MX]; n void add(pair<int, int> &p, pair<int, int> q) { n   p.first = min(p.first, q.first); n   p.second += q.second; n } n void up(int u) { n   int l = u * 2, r = u * 2 + 1; n   val[u] = min(val[l], val[r]); n   cnt[u] = 0; n   if (val[u] == val[l]) cnt[u] += cnt[l]; n   if (val[u] == val[r]) cnt[u] += cnt[r]; n } n void renew(int u, int x) { n   val[u] += x; n   dn[u] += x; n } n void down(int u) { n   val[u] += dn[u]; n   renew(u * 2, dn[u]); n   renew(u * 2 + 1, dn[u]); n   dn[u] = 0; n } n void build(int u, int l, int r) { n   if (l == r) { n     val[u] = p[l]; n     cnt[u] = 1; n     dn[u] = 0; n     return; n   } n   int md = (l + r) / 2; n   build(u * 2, l, md); n   build(u * 2 + 1, md + 1, r); n   up(u); n } n void add(int u, int l, int r, int st, int en, int x) { n   if (st <= l && r <= en) { n     renew(u, x); n     return; n   } n   int md = (l + r) / 2; n   down(u); n   if (st <= md) add(u * 2, l, md, st, en, x); n   if (md < en) add(u * 2 + 1, md + 1, r, st, en, x); n   up(u); n } n int bf = false; n pair<int, int> get(int u, int l, int r, int st, int en) { n   pair<int, int> ret(0, 0); n   if (st <= l && r <= en) { n     if (val[u] == 0) return pair<int, int>(val[u], cnt[u]); n     if (val[u] > 0) return pair<int, int>(val[u], 0); n     if (l == r) return pair<int, int>(val[u], 0); n     down(u); n     int md = (l + r) / 2; n     ret = get(u * 2, l, md, st, en); n     if (ret.first >= 0) { n       add(ret, get(u * 2 + 1, md + 1, r, st, en)); n     } n     up(u); n     return ret; n   } n   int md = (l + r) / 2; n   down(u); n   if (st <= md) ret = get(u * 2, l, md, st, en); n   if (md < en) n     if (ret.first >= 0) add(ret, get(u * 2 + 1, md + 1, r, st, en)); n   up(u); n   return ret; n } n int main() { n   int n; n   scanf( %d%s , &n, A); n   STree tr(A); n   int dp = 0; n   for (int i = 0; i < n; i++) { n     if (A[i] ==  ( ) n       dp++; n     else n       dp--; n     p[i + 1] = dp; n   } n   build(1, 1, n); n   tr.calc(tr.root); n   sort(que.begin(), que.end()); n   int lft = 0; n   long long ans = 0; n   for (int i = 0; i < que.size();) { n     while (lft < que[i].l) { n       if (A[lft] ==  ( ) n         add(1, 1, n, 1, n, -1); n       else n         add(1, 1, n, 1, n, 1); n       lft++; n     } n     while (i < que.size() && que[i].l == lft) { n       pair<int, int> ret = get(1, 1, n, lft + 1, que[i].r); n       ans += ret.second * que[i].x; n       i++; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 7005; n const int Maxm = 1000006; n int n, m; n int a[Maxn], b[Maxn], c[Maxn], d[Maxn], e[Maxn]; n int mn[Maxn], mx[Maxn]; n bool has[Maxm]; n vector<int> tk; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d %d %d , &a[i], &b[i], &c[i]); n     if (a[i] == 1) n       scanf( %d %d , &d[i], &e[i]); n     else { n       mn[b[i]] = mx[b[i]] = c[i]; n       int tomin = 0, tomax = 0; n       for (int l = b[i] + 1; l <= n; l++) { n         while (1 << tomin <= mn[l - 1] - 1) tomin++; n         mn[l] = mn[l - 1] - 1 + tomin + 1; n         while (1 << tomax <= mx[l - 1] - 1) tomax++; n         mx[l] = mx[l - 1] - 1 + tomax + 1 + (mx[l - 1] == 1 << tomax); n       } n       for (int j = 0; j < i; j++) n         if (b[i] <= b[j] && max(mn[b[j]], c[j]) <= min(mx[b[j]], d[j]) && n             !has[e[j]]) { n           has[e[j]] = true; n           tk.push_back(e[j]); n         } n       printf( %d n , tk.size()); n       for (int i = 0; i < tk.size(); i++) has[tk[i]] = false; n       tk.clear(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n struct Node { n   int pos, val, sit; n } a[305]; n bool cmp(Node x, Node y) { n   if (x.val == y.val) { n     return x.pos > y.pos; n   } n   return x.val < y.val; n } n bool cmp2(Node x, Node y) { return x.pos < y.pos; } n int main() { n   int T; n   std::cin >> T; n   while (T--) { n     cin >> n >> m; n     for (int i = 1; i <= m; i++) { n       scanf( %d , &a[i].val); n       a[i].pos = i; n     } n     sort(a + 1, a + 1 + m, cmp); n     for (int i = 1; i <= m; i++) { n       a[i].sit = i; n     } n     sort(a + 1, a + 1 + m, cmp2); n     int ans = 0; n     for (int i = 2; i <= m; i++) { n       for (int j = i - 1; j >= 1; j--) { n         if (a[j].sit < a[i].sit) ans++; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INTINF = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int MAXN = 55; n const int MAXM = (1 << 20) + 1; n long long dp[MAXM], C[MAXN][MAXN]; n int n, m; n char a[MAXN][MAXN]; n double ans = 0; n int count(long long x) { n   int res = 0; n   while (x) x &= (x - 1), res++; n   return res; n } n void init() { n   for (int i = 0; i <= m; i++) n     for (int j = C[i][0] = 1; j <= i; j++) n       C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %s , a[i]); n   m = strlen(a[0]); n   init(); n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < i; j++) { n       int same_bit = 0; n       for (int k = 0; k < m; k++) n         if (a[i][k] == a[j][k]) same_bit |= 1 << k; n       dp[same_bit] |= (1ll << i) | (1ll << j); n     } n   for (int s = (1 << m) - 1; ~s; s--) n     for (int i = 0; i < m; i++) n       if ((1 << i) & s) dp[s ^ (1 << i)] |= dp[s]; n   for (int s = 0; s < (1 << m); s++) { n     ans += (double)count(dp[s]) / n / C[m][count(s)]; n   } n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, s, t, l, rest; n long long d[1010], d2[1010], w[10010]; n bool zeredg[10010]; n pair<long long, long long> edg[10010]; n vector<vector<long long> > edglist; n void dijkstra(long long dist[], bool sec) { n   set<pair<long long, long long> > najbl; n   dist[s] = 0; n   najbl.insert({0, s}); n   for (long long i = 0; i < n; ++i) { n     if (s != i) { n       dist[i] = 1e18; n       najbl.insert({dist[i], i}); n     } n   } n   while (najbl.size()) { n     long long v = najbl.begin()->second; n     najbl.erase(najbl.begin()); n     for (auto i : edglist[v]) { n       long long u = edg[i].first + edg[i].second - v; n       if (sec and dist[u] > dist[v] + w[i] and zeredg[i] and n           w[i] < d[u] + rest - dist[v]) { n         w[i] = d[u] + rest - dist[v]; n       } n       if (dist[u] > dist[v] + w[i]) { n         najbl.erase({dist[u], u}); n         dist[u] = dist[v] + w[i]; n         najbl.insert({dist[u], u}); n       } n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m >> l >> s >> t; n   vector<long long> vi; n   edglist.resize(n, vi); n   for (long long i = 0; i < m; ++i) { n     long long u, v, e; n     cin >> u >> v >> e; n     edglist[u].push_back(i); n     edglist[v].push_back(i); n     edg[i] = {u, v}; n     if (e) { n       w[i] = e; n     } else { n       w[i] = 1; n       zeredg[i] = 1; n     } n   } n   dijkstra(d, 0); n   if (d[t] > l) { n     cout <<  NO ; n     return 0; n   } else if (d[t] == l) { n     cout <<  YES n ; n     for (long long i = 0; i < m; ++i) { n       cout << edg[i].first <<     << edg[i].second <<     << w[i] <<   n ; n     } n     return 0; n   } n   rest = l - d[t]; n   dijkstra(d2, 1); n   if (d2[t] != l) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   for (long long i = 0; i < m; ++i) { n     cout << edg[i].first <<     << edg[i].second <<     << w[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int a[200007]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   int c = n / 2 - 1; n   int l = 0, r = n - 1; n   int mn = 1e9 + 77; n   for (int i = 0; i <= c; i++) { n     mn = min(mn, a[r - c + i] - a[l + i]); n   } n   cout << mn; n   return 0; n } n