#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   set<string> st; n   while (n--) { n     cin >> s; n     if (st.count(s)) { n       cout <<  YES ; n     } else { n       cout <<  NO ; n       st.insert(s); n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : (-x); n } n template <class T> n T sqr(T x) { n   return x * x; n } n const double eps = 1E-8; n struct P { n   double x, y; n   P(double x = 0.0, double y = 0.0) : x(x), y(y) {} n   P operator+(P a) { return P(x + a.x, y + a.y); } n   P operator-(P a) { return P(x - a.x, y - a.y); } n   P operator*(double k) { return P(x * k, y * k); } n   double operator*(P a) { return x * a.y - y * a.x; } n   double operator^(P a) { return x * a.x + y * a.y; } n   double len() { return sqrt(x * x + y * y); } n   P perp() { return P(y, -x); } n   P norm() { n     double l = len(); n     return abs(l) < eps ? P(x, y) : P(x / l, y / l); n   } n   bool operator==(P a) { return abs(x - a.x) < eps && abs(y - a.y) < eps; } n   void load() { cin >> x >> y; } n }; n int sgn(double x) { n   if (x > eps) return 1; n   if (x < -eps) return -1; n   return 0; n } n bool pointOnSegment(P p, P a, P b) { n   if (abs((a - p) * (b - p)) > eps) return false; n   return ((a - p) ^ (b - p)) < eps; n } n bool crossSegments1d(double a, double aa, double b, double bb) { n   return min(a, aa) < max(b, bb) + eps && min(b, bb) < max(a, aa) + eps; n } n bool crossSegments(P a, P aa, P b, P bb) { n   if (sgn((b - a) * (aa - a)) * sgn((aa - a) * (bb - a)) < 0) return false; n   if (sgn((a - b) * (bb - b)) * sgn((bb - b) * (aa - b)) < 0) return false; n   return crossSegments1d(a.x, aa.x, b.x, bb.x) && n          crossSegments1d(a.y, aa.y, b.y, bb.y); n } n struct L { n   double a, b, c; n   L(double a = 0.0, double b = 0.0, double c = 0.0) : a(a), b(b), c(c) {} n   bool operator||(L l) { return abs(a * l.b - b * l.a) < eps; } n }; n L getLine(P p1, P p2) { n   double x0 = p1.x, y0 = p1.y; n   double al = (p2 - p1).x, be = (p2 - p1).y; n   return L(be, -al, al * y0 - be * x0); n } n P getPoint(L l1, L l2) { n   double det = l1.a * l2.b - l1.b * l2.a; n   double det1 = -(l1.c * l2.b - l1.b * l2.c); n   double det2 = -(l1.a * l2.c - l1.c * l2.a); n   return P(det1 / det, det2 / det); n } n double h, f; n pair<double, double> calc1(double y, double x1, double x2) { n   double l = x1 * (f - y) / (f - h); n   double r = x2 * (f - y) / (f - h); n   return make_pair(l, r); n } n pair<double, double> calc2(double y, double x1, double x2) { n   double l = x1 * (y + f) / (f - h); n   double r = x2 * (y + f) / (f - h); n   return make_pair(l, r); n } n int main() { n   int n; n   cin >> n; n   cin >> h >> f; n   vector<pair<int, int> > v(n); n   double res = 0.0; n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &v[i].first, &v[i].second); n     double l = abs(v[i].second - v[i].first); n     res += 2. * h * (l + l * (f + h) / (f - h)); n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       vector<P> V; n       L l11 = getLine(P(0, f), P(v[i].first, h)); n       L l12 = getLine(P(0, f), P(v[i].second, h)); n       L l21 = getLine(P(0, -f), P(v[j].first, -h)); n       L l22 = getLine(P(0, -f), P(v[j].second, -h)); n       if (!(l11 || l21)) V.push_back(getPoint(l11, l21)); n       if (!(l11 || l22)) V.push_back(getPoint(l11, l22)); n       if (!(l12 || l21)) V.push_back(getPoint(l12, l21)); n       if (!(l12 || l22)) V.push_back(getPoint(l12, l22)); n       vector<double> ev; n       ev.push_back(-h); n       ev.push_back(-h); n       ev.push_back(h); n       ev.push_back(h); n       for (int k = 0; k < ((int)(V).size()); ++k) { n         if (abs(V[k].y) < h + eps) ev.push_back(V[k].y); n       } n       sort((ev).begin(), (ev).end()); n       double cur = 0; n       double st = 0.0; n       for (int k = 1; k < ((int)(ev).size()); ++k) { n         pair<double, double> p = calc1(ev[k], v[i].first, v[i].second); n         pair<double, double> q = calc2(ev[k], v[j].first, v[j].second); n         double l = max(p.first, q.first); n         double r = min(p.second, q.second); n         double t = max(0.0, r - l); n         cur += (t + st) * 0.5 * (ev[k] - ev[k - 1]); n         st = t; n       } n       res -= abs(cur); n     } n   } n   printf( %.10lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, x, y; n   cin >> n >> x >> y; n   long long a = y - n + 1; n   long long b = x - n + 1; n   for (long long i = a; i >= 1; i--) { n     long long c = i * i; n     if (c >= b) { n       for (int j = 1; j <= n - 1; j++) cout <<  1  << endl; n       cout << i << endl; n       return 0; n     } n   } n   cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   if (b == 1 && a == 9) { n     cout <<  9 10  << endl; n   } else if (b - a == 1) { n     cout << a <<     << b << endl; n   } else if (a == b) { n     cout << a <<  1   << b <<  2  << endl; n   } else { n     cout <<  -1  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int L = 1; n int R = 0; n int cnt[300005]; n int a[300005]; n int big[300005][2]; n int is1 = 0; n int ap[300005]; n vector<int> st; n void in(int id) { n   id = a[id]; n   if (++cnt[id] == 1) n     is1++; n   else if (cnt[id] == 2) n     is1--; n } n void out(int id) { n   id = a[id]; n   if (--cnt[id] == 1) n     is1++; n   else if (cnt[id] == 0) n     is1--; n } n void update(int l, int r) { n   while (R < r) in(++R); n   while (L > l) in(--L); n   while (L < l) out(L++); n   while (R > r) out(R--); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   a[0] = n + 1; n   a[n + 1] = n + 1; n   int res = 0; n   st.push_back(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     while (st.back() < a[i]) st.pop_back(); n     big[i][0] = ap[st.back()]; n     st.push_back(a[i]); n     ap[a[i]] = i; n   } n   st.clear(); n   st.push_back(n + 1); n   ap[n + 1] = n + 1; n   for (int i = n; i >= 1; i--) { n     while (st.back() < a[i]) st.pop_back(); n     big[i][1] = ap[st.back()]; n     st.push_back(a[i]); n     ap[a[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     int l = max(i - a[i] + 1, big[i][0] + 1); n     int r = min(l + a[i] - 1, big[i][1] - 1); n     if (l + a[i] - 1 != r) continue; n     for (; l <= i; l++) { n       update(l, r); n       if (is1 == a[i]) res++; n       if (++r == big[i][1]) break; n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n int n, a[N], b[N]; n vector<int> vec[N]; n set<pair<int, int> > S; n struct node { n   int cl, cr, mx, vl, vr, v; n   node() {} n   node(int c) { n     cl = cr = c; n     mx = vl = vr = v = 1; n   } n } t[N * 4]; n node operator+(node a, node b) { n   node ans; n   ans.cl = a.cl; n   ans.cr = b.cr; n   ans.mx = max(a.mx, b.mx); n   ans.vl = a.vl; n   ans.vr = b.vr; n   ans.v = a.v + b.v; n   if (a.cr ^ b.cl) { n     ans.mx = max(ans.mx, a.vr + b.vl); n     if (a.v == a.vl) ans.vl += b.vl; n     if (b.v == b.vr) ans.vr += a.vr; n   } n   return ans; n } n void build(int k, int l, int r) { n   if (l == r) { n     t[k] = node(0); n     return; n   } n   int mid = (l + r) / 2; n   build(k * 2, l, mid); n   build(k * 2 + 1, mid + 1, r); n   t[k] = t[k * 2] + t[k * 2 + 1]; n } n void change(int k, int l, int r, int p) { n   if (l == r) { n     t[k] = node(1); n     return; n   } n   int mid = (l + r) / 2; n   if (p <= mid) n     change(k * 2, l, mid, p); n   else n     change(k * 2 + 1, mid + 1, r, p); n   t[k] = t[k * 2] + t[k * 2 + 1]; n } n struct YYN { n   int t[N * 4], nn; n   void build(int n) { n     for (nn = 1; nn <= n; nn <<= 1) n       ; n     for (int i = (int)(0); i <= (int)(2 * nn - 1); i++) t[i] = 0; n   } n   void change(int x, int y, int v) { n     x += nn - 1; n     y += nn + 1; n     for (; x ^ y ^ 1; x >>= 1, y >>= 1) { n       if (!(x & 1)) t[x ^ 1] = max(t[x ^ 1], v); n       if (y & 1) t[y ^ 1] = max(t[y ^ 1], v); n     } n   } n   void output() { n     for (int i = (int)(1); i <= (int)(nn - 1); i++) { n       t[i * 2] = max(t[i * 2], t[i]); n       t[i * 2 + 1] = max(t[i * 2 + 1], t[i]); n     } n     for (int i = (int)(1); i <= (int)(n); i++) printf( %d  , t[i + nn]); n     puts(  ); n   } n } RE; n void flip(int x) { n   assert(a[x] == 0); n   if (x == 1 || a[x] == a[x - 1]) S.erase(pair<int, int>(x, a[x])); n   if (x == n || a[x] == a[x + 1]) S.erase(pair<int, int>(x + 1, a[x])); n   a[x] ^= 1; n   change(1, 1, n, x); n   if (x == 1 || a[x] == a[x - 1]) S.insert(pair<int, int>(x, a[x])); n   if (x == n || a[x] == a[x + 1]) S.insert(pair<int, int>(x + 1, a[x])); n } n void updateee(int x, int v) { n   auto it = S.lower_bound(pair<int, int>(x, -1)); n   auto pre = it, suf = it; n   pre--; n   if (pre->second == 1 && suf->second == 1) n     RE.change(pre->first, suf->first - 1, v); n   else if (pre->second == 1 && suf->second == 0) n     RE.change(pre->first, (pre->first + suf->first) / 2 - 1, v); n   else if (pre->second == 0 && suf->second == 1) n     RE.change((pre->first + suf->first) / 2, suf->first - 1, v); n } n void updatee(int x, int v) { n   auto it = S.lower_bound(pair<int, int>(x, -1)); n   auto pre = it, suf = it; n   if (x != 1) { n     --pre; n     if (pre->second == 1) n       RE.change(pre->first, it->first - 1, v); n     else n       RE.change((pre->first + it->first) / 2, it->first - 1, v); n   } n   if (x != n + 1) { n     ++suf; n     if (suf->second == 1) n       RE.change(it->first, suf->first - 1, v); n     else n       RE.change(it->first, (it->first + suf->first) / 2 - 1, v); n   } n } n void update(int x, int v) { n   if (x == 1 || a[x] == a[x - 1]) n     updatee(x, v); n   else n     updateee(x, v); n   if (x == n || a[x] == a[x + 1]) n     updatee(x + 1, v); n   else n     updateee(x + 1, v); n } n int main() { n   scanf( %d , &n); n   for (int i = (int)(1); i <= (int)(n); i++) scanf( %d , &a[i]); n   for (int i = (int)(1); i <= (int)(n); i++) b[i] = a[i]; n   sort(b + 1, b + n + 1); n   *b = unique(b + 1, b + n + 1) - b - 1; n   for (int i = (int)(1); i <= (int)(n); i++) { n     a[i] = lower_bound(b + 1, b + *b + 1, a[i]) - b; n     vec[a[i]].push_back(i); n   } n   memset(a, 0, sizeof(a)); n   build(1, 1, n); n   RE.build(n); n   for (int i = (int)(1); i <= (int)(n + 1); i++) S.insert(pair<int, int>(i, 0)); n   int ansT = 0; n   for (int i = (int)(*b); i >= (int)(1); i--) { n     for (auto j : vec[i]) flip(j); n     for (auto j : vec[i]) update(j, b[i]); n     ansT = max(ansT, t[1].mx); n   } n   printf( %d n , (ansT - 1) / 2); n   RE.output(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> usedTrains; n vector<vector<pair<long long, long long>>> gNoTrain; n vector<vector<pair<long long, long long>>> gYesTrain; n vector<long long> disNoTrain; n vector<long long> disYesTrain; n unordered_set<long long> tracks; n void no_train_dijkstra(int s) { n   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                  greater<pair<long long, long long>>> n       Q; n   Q.push(make_pair(0, s)); n   disNoTrain[s] = 0; n   while (!Q.empty()) { n     pair<long long, long long> p = Q.top(); n     Q.pop(); n     int currV = (int)p.second; n     if (disNoTrain[currV] != p.first) continue; n     for (pair<long long, long long>& edge : gNoTrain[currV]) { n       int u = (int)edge.first; n       long long w = edge.second; n       if (disNoTrain[currV] + w < disNoTrain[u]) { n         disNoTrain[u] = disNoTrain[currV] + w; n         Q.push(make_pair(disNoTrain[u], u)); n       } n     } n   } n } n void yes_train_dijkstra(int s) { n   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                  greater<pair<long long, long long>>> n       Q; n   Q.push(make_pair(0, s)); n   disYesTrain[s] = 0; n   while (!Q.empty()) { n     pair<long long, long long> p = Q.top(); n     Q.pop(); n     int currV = (int)p.second; n     if (disYesTrain[currV] != p.first) continue; n     for (pair<long long, long long>& edge : gYesTrain[currV]) { n       int u = (int)edge.first; n       long long w = edge.second; n       if (currV != 0 && disYesTrain[currV] + w == disYesTrain[u]) { n         tracks.erase(u); n       } n       if (disYesTrain[currV] + w < disYesTrain[u]) { n         if (currV != 0) tracks.erase(u); n         disYesTrain[u] = disYesTrain[currV] + w; n         Q.push(make_pair((long long)disYesTrain[u], u)); n       } n     } n   } n } n int n, m, k; n int main() { n   cin >> n >> m >> k; n   usedTrains = vector<long long>(k, 0); n   gNoTrain = vector<vector<pair<long long, long long>>>( n       n, vector<pair<long long, long long>>()); n   gYesTrain = vector<vector<pair<long long, long long>>>( n       n, vector<pair<long long, long long>>()); n   disNoTrain = vector<long long>(n, 100000000000l); n   disYesTrain = vector<long long>(n, 100000000000l); n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     gNoTrain[u - 1].push_back({(long long)v - 1, (long long)w}); n     gNoTrain[v - 1].push_back({(long long)u - 1, (long long)w}); n     gYesTrain[u - 1].push_back({(long long)v - 1, (long long)w}); n     gYesTrain[v - 1].push_back({(long long)u - 1, (long long)w}); n   } n   no_train_dijkstra(0); n   for (int i = 0; i < k; i++) { n     int v, w; n     cin >> v >> w; n     gYesTrain[0].push_back({(long long)v - 1, (long long)w}); n     gYesTrain[v - 1].push_back({0, (long long)w}); n     if (w < disNoTrain[v - 1]) tracks.insert(v - 1); n   } n   no_train_dijkstra(0); n   yes_train_dijkstra(0); n   cout << (k - (long long)tracks.size()) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300000 + 15; n int n; n int a[N]; n char c[N]; n int flag, st, bb, rr, ll, cc, dd, ee, hh; n int main() { n   scanf( %d , &n); n   for (int i(1); i <= (n); ++i) scanf( %d %c , a + i, c + i); n   long long ans = 0; n   for (int i(1); i <= (n); ++i) n     if (c[i] ==  B ) { n       if (!bb) bb = i; n       dd = a[i] - a[ll] > dd ? a[i] - a[ll] : dd; n       ll = i; n     } else if (c[i] ==  R ) { n       if (!rr) rr = i; n       ee = a[i] - a[cc] > ee ? a[i] - a[cc] : ee; n       cc = i; n     } else { n       if (flag) { n         int tmp1 = a[i] - a[hh]; n         int tmp2 = (ee ? a[i] - a[hh] - max(a[i] - a[cc], ee) : 0) + n                    (dd ? a[i] - a[hh] - max(a[i] - a[ll], dd) : 0); n         ans += a[i] - a[hh] + min(tmp1, tmp2); n       } else n         ans += (bb ? a[i] - a[bb] : 0) + (rr ? a[i] - a[rr] : 0); n       flag = 1; n       hh = cc = ll = i; n       ee = dd = 0; n     } n   if (!flag) n     printf( %lld n , 0ll + a[cc] - a[rr] + a[ll] - a[bb]); n   else n     printf( %lld n , n            ans + (cc > hh ? a[cc] - a[hh] : 0) + (ll > hh ? a[ll] - a[hh] : 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[5005]; n int times[5005]; n int maxn, val; n int valcnt[5005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= n; i++) { n     memset(times, 0, sizeof(times)); n     maxn = -1; n     val = 0; n     for (int j = i; j <= n; j++) { n       times[a[j]]++; n       if (times[a[j]] == maxn && a[j] < val) { n         maxn = times[a[j]]; n         val = a[j]; n       } else if (times[a[j]] > maxn) { n         maxn = times[a[j]]; n         val = a[j]; n       } n       valcnt[val]++; n     } n   } n   for (int i = 1; i <= n; i++) printf( %d  , valcnt[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n const long long mod = 998244353; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n, m, k; n vector<int> a; n int f(int off) { n   int q = 0; n   int left = m - 1; n   int at_w = k; n   for (int i = off; i < n; i++) { n     if (at_w - a[i] >= 0) { n       at_w -= a[i]; n       q++; n     } else { n       if (left == 0) break; n       at_w = k - a[i]; n       q++; n       left--; n     } n   } n   return q == (n - off); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   a.resize(n); n   { n     for (auto &_re : a) cin >> _re; n   }; n   int l = 0, r = n, mid; n   int ans = 0; n   while (l <= r) { n     mid = l + r >> 1; n     if (f(mid)) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   cout << n - ans <<   n ; n } n


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


#include <bits/stdc++.h> n const int inf = 2e9; n using namespace std; n int dp[505][505], change[505][505]; n pair<int, int> parent[505][505]; n string second; n int calc(int l, int r) { n   int ans = 0; n   while (l < r) { n     if (second[l] != second[r]) ans++; n     l++; n     r--; n   } n   return ans; n } n void _try(int l, int r) { n   while (l < r) { n     if (second[l] != second[r]) second[r] = second[l]; n     l++; n     r--; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   int k; n   cin >> second >> k; n   int n = second.length(); n   for (int i = 0; i < 505; i++) n     for (int j = 0; j < 505; j++) { n       dp[i][j] = inf; n       parent[i][j] = make_pair(i, j); n     } n   for (int i = 0; i < n; i++) n     for (int j = i; j < n; j++) { n       change[i][j] = calc(i, j); n     } n   for (int pos = 0; pos < n; pos++) n     for (int cnt = 1; cnt <= k; cnt++) { n       if (cnt == 1) { n         dp[pos][cnt] = change[0][pos]; n       } else { n         for (int pos1 = 0; pos1 < pos; pos1++) { n           int new_v = dp[pos1][cnt - 1] + change[pos1 + 1][pos]; n           if (dp[pos][cnt] > new_v) { n             dp[pos][cnt] = new_v; n             parent[pos][cnt] = make_pair(pos1, cnt - 1); n           } n         } n       } n     } n   int ansi = 1; n   for (int i = 2; i <= k; i++) { n     if (dp[n - 1][i] < dp[n - 1][ansi]) ansi = i; n   } n   int pos = n - 1, j = ansi; n   vector<pair<int, int> > split; n   while (parent[pos][j] != make_pair(pos, j)) { n     pair<int, int> next = parent[pos][j]; n     pos = next.first; n     j = next.second; n     split.push_back(make_pair(pos, j)); n   } n   reverse(split.begin(), split.end()); n   cout << dp[n - 1][ansi] << endl; n   int l = 0; n   if (split.empty()) { n     _try(0, n - 1); n     cout << second; n     return 0; n   } n   for (int i = 0; i < split.size(); i++) { n     _try(l, split[i].first); n     for (int j = l; j <= split[i].first; j++) { n       cout << second[j]; n       cout.flush(); n     } n     cout <<  + ; n     l = split[i].first + 1; n   } n   _try(l, n - 1); n   for (int i = l; i < n; i++) { n     cout << second[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long n; n   cin >> n; n   vector<pair<long long, long long>> a((size_t)n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n   } n   sort(a.rbegin(), a.rend()); n   vector<long long> b((size_t)n); n   for (long long i = 0; i < n; i++) { n     b[i] = a[i].second * 2; n     if (i >= 1) { n       cout << b[i - 1] + 1 <<     << b[i] + 1 <<   n ; n     } n   } n   for (long long i = 0; i < n; i++) { n     cout << a[i].second * 2 + 1 + 1 <<     << b[i + a[i].first - 1] + 1 <<   n ; n     if (i + a[i].first - 1 == (long long)b.size() - 1) { n       b.push_back(a[i].second * 2 + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t = 1; n   while (t--) { n     long long int n; n     cin >> n; n     cout << 0 <<     << 0 <<     << n; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 1e9 + 7, INF = 0x3f3f3f3f; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long qpow(long long a, long long n) { n   long long r = 1 % P; n   for (a %= P; n; a = a * a % P, n >>= 1) n     if (n & 1) r = r * a % P; n   return r; n } n long long inv(long long first) { n   return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; n } n inline int rd() { n   int first = 0; n   char p = getchar(); n   while (p <  0  || p >  9 ) p = getchar(); n   while (p >=  0  && p <=  9 ) first = first * 10 + p -  0 , p = getchar(); n   return first; n } n const int N = 1e6 + 50; n int n, m, w[N], dep[N]; n vector<pair<int, int>> g[N]; n vector<pair<int, long long>> route[N]; n void dfs(int first, int d) { n   dep[first] = d; n   for (auto &e : g[first]) dfs(e.first, d + e.second); n } n int p(int f, int first) { n   if (first == f * 2) return first + 1; n   return first - 1; n } n long long qry(int a, int h, int c) { n   if (a > n || c > h) return 0; n   auto p = --upper_bound(route[a].begin(), route[a].end(), n                          pair<int, long long>(h - c, 1e18)); n   long long cnt = p - route[a].begin() + 1; n   return cnt * (h - c) - p->second; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 2; i <= n; ++i) { n     int u = i / 2, v = i; n     scanf( %d , w + i); n     g[u].push_back({v, w[i]}); n   } n   dfs(1, 0); n   for (int i = 1; i <= n; ++i) { n     int j = i; n     route[i].push_back({0, 0ll}); n     while (j != 1) { n       j /= 2; n       route[j].push_back({dep[i] - dep[j], 0ll}); n     } n   } n   for (int i = 1; i <= n; ++i) { n     sort(route[i].begin(), route[i].end()); n     int sz = route[i].size(); n     for (int j = 1; j <= sz - 1; ++j) n       route[i][j].second = route[i][j].first + route[i][j - 1].second; n   } n   while (m--) { n     int a, h; n     scanf( %d%d , &a, &h); n     long long ans = qry(a, h, 0); n     int c = 0; n     while (1) { n       c += w[a]; n       if (a == 1 || c > h) break; n       int t = p(a / 2, a); n       ans += qry(t, h, c + w[t]); n       ans += h - c; n       a /= 2; n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<long long, vector<long long>, greater<long long>> pq1, pq2; n long long power(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     res *= a; n     b--; n   } n   return res; n } n long long parent[200005], sz[200005], visit[200005] = {0}, cnt[200005] = {0}; n vector<pair<long long, long long>> vt[200005]; n vector<long long> v[200005]; n struct Test { n   long long a, b, c; n }; n bool comp(const Test &p1, const Test &p2) { return p1.a < p2.a; } n long long find_parent(long long a) { n   if (a == parent[a]) n     return a; n   else n     return parent[a] = find_parent(parent[a]); n } n long long dfs(long long a) { n   visit[a] = 1; n   cnt[a]++; n   for (int i = 0; i < v[a].size(); ++i) { n     long long b = v[a][i]; n     if (visit[b] == 0) { n       dfs(b); n     } n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m, x, y, st1, st2, z; n   vector<Test> w; n   cin >> n >> m; n   long long cnt1 = 0; n   for (int i = 0; i < m; ++i) { n     cin >> x >> y >> z; n     vt[x].push_back({y, z}); n     vt[y].push_back({x, z}); n     if (cnt1 == 0) { n       w.push_back({0, x, y}); n       st1 = x; n       st2 = y; n     } else n       w.push_back({z, x, y}); n     cnt1++; n   } n   for (int i = 1; i < n + 1; ++i) { n     sz[i] = 0; n     parent[i] = i; n   } n   sort(w.begin(), w.end(), comp); n   for (int i = 0; i < w.size(); ++i) { n     long long p1 = w[i].b; n     long long p2 = w[i].c; n     long long par1 = find_parent(p1); n     long long par2 = find_parent(p2); n     if (par1 != par2) { n       v[p1].push_back(p2); n       v[p2].push_back(p1); n       if (sz[par1] <= sz[par2]) { n         sz[par2] += sz[par1]; n         parent[par1] = par2; n       } else { n         sz[par1] += sz[par2]; n         parent[par2] = par1; n       } n     } n   } n   if (v[st1].size() == 1) { n     if (vt[st1].size() == 1) n       cout <<  1000000000  << endl; n     else { n       long long mn = 1000000000; n       for (int j = 0; j < vt[st1].size(); ++j) { n         if (v[st1][j] == st2) continue; n         mn = min(mn, vt[st1][j].second); n       } n       cout << mn << endl; n     } n   } else { n     visit[st1] = 1; n     dfs(st2); n     long long mn = 1000000000; n     for (int i = 1; i < n + 1; ++i) { n       if (cnt[i] > 0) { n         for (int j = 0; j < vt[i].size(); ++j) { n           if (i == st2 && vt[i][j].first == st1) continue; n           if (cnt[vt[i][j].first] == 0) mn = min(mn, vt[i][j].second); n         } n       } n     } n     cout << mn << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int N = 1000005; n class cmp { n  public: n   bool operator()(long long &A, long long &B) { return A > B; } n }; n bool by_sec(pair<long long, long long> &A, pair<long long, long long> &B) { n   return A.second < B.second; n } n bool is(pair<long long, long long> a, pair<long long, long long> b) { n   return !((a.first > b.second && a.second > b.second) || n            (a.first < b.first && a.second < b.first)); n } n long double dist(pair<long long, long long> p1, pair<long long, long long> p2) { n   return (long double)sqrt( n       (long double)(p1.first - p2.first) * (p1.first - p2.first) + n       (long double)(p1.second - p2.second) * (p1.second - p2.second)); n } n pair<bool, vector<long long> > is_in(vector<long long> v, long long sum) { n   long long n = v.size(); n   bool dp[30][1000]; n   memset(dp, 0, sizeof(dp)); n   ; n   for (int i = 0; i < sum + 1; i++) dp[0][i] = 0; n   for (int i = 0; i < n + 1; i++) dp[i][0] = 1; n   for (int i = 1; i < n + 1; i++) n     for (int j = 1; j < sum + 1; j++) { n       dp[i][j] = dp[i - 1][j]; n       if (v[i - 1] <= j) dp[i][j] = dp[i][j] | dp[i - 1][j - v[i - 1]]; n     } n   long long i = n; n   long long currSum = sum; n   vector<long long> set1, set2; n   if (!dp[n][sum]) return (make_pair(dp[n][sum], set1)); n   while (i > 0 && currSum >= 0) { n     if (dp[i - 1][currSum]) { n       i--; n       set2.push_back(v[i]); n     } else { n       i--; n       currSum -= v[i]; n       set1.push_back(v[i]); n     } n   } n   return make_pair(dp[n][sum], set1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   bool mp[8000 + 5]; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     memset(mp, 0, sizeof(mp)); n     ; n     cin >> n; n     vector<long long> v(n); n     for (int i = 0; i < n; i++) cin >> v[i], mp[v[i]] = 1; n     for (int i = 0; i < n; i++) { n       long long s = v[i]; n       for (int j = i + 1; j < n; j++) { n         s += v[j]; n         if (s <= n && mp[s]) mp[s] = 0; n       } n     } n     long long cnt = 0; n     for (int i = 0; i < n; i++) n       if (!mp[v[i]]) cnt++; n     cout << cnt <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long mnr = 1e9, mxl = -1e9; n     for (long long i = 0; i < n; i++) { n       long long l, r; n       cin >> l >> r; n       mnr = min(mnr, r); n       mxl = max(mxl, l); n     } n     cout << (n == 1 ? 0 : max(mxl - mnr, 0ll)) <<   n ; n   } n } n


#include <bits/stdc++.h> n std::vector<int> G[200006]; n int check, a, i, x, y, D, o[200006], z[200006], par[200006], d[200006]; n void dfs(int p) { n   d[p] = D; n   for (int k = 0; k < G[p].size(); k++) { n     if (d[G[p][k]] == 0) { n       D++; n       dfs(G[p][k]); n       D--; n     } else n       par[p] = G[p][k]; n   } n } n int main() { n   scanf( %d , &a); n   for (i = 1; i < a; i++) { n     scanf( %d%d , &x, &y); n     G[x].push_back(y); n     G[y].push_back(x); n   } n   D = 1; n   dfs(1); n   for (i = 1; i <= a; i++) { n     scanf( %d , &z[i]); n     o[z[i]] = i; n   } n   for (i = 1; i < a; i++) { n     if (d[z[i]] > d[z[i + 1]]) n       check = 1; n     else if (d[z[i]] == d[z[i + 1]]) { n       if (o[par[z[i]]] > o[par[z[i + 1]]]) check = 1; n     } n   } n   if (check == 1) n     puts( No ); n   else n     puts( Yes ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10000 + 20; n int sa[N], cc[N], t1[N], t2[N], height[N], rk[N]; n int n; n char s[N], t[N]; n bool cmp(int *y, int a, int b, int k) { n   int a1 = y[a], a2 = (a + k >= n) ? -1 : y[a + k]; n   int b1 = y[b], b2 = (b + k >= n) ? -1 : y[b + k]; n   return a1 == b1 && a2 == b2; n } n void buildSa(int m) { n   int *x = t1, *y = t2; n   for (int i = 0; i < m; i++) cc[i] = 0; n   for (int i = 0; i < n; i++) cc[x[i] = s[i]]++; n   for (int i = 1; i < m; i++) cc[i] += cc[i - 1]; n   for (int i = n - 1; i >= 0; i--) sa[--cc[x[i]]] = i; n   for (int k = 1; k <= n; k <<= 1) { n     int p = 0; n     for (int i = n - k; i < n; i++) y[p++] = i; n     for (int i = 0; i < n; i++) n       if (sa[i] >= k) y[p++] = sa[i] - k; n     for (int i = 0; i < m; i++) cc[i] = 0; n     for (int i = 0; i < n; i++) cc[x[y[i]]]++; n     for (int i = 1; i < m; i++) cc[i] += cc[i - 1]; n     for (int i = n - 1; i >= 0; i--) sa[--cc[x[y[i]]]] = y[i]; n     swap(x, y); n     x[sa[0]] = 0; n     m = 1; n     for (int i = 1; i < n; i++) { n       x[sa[i]] = cmp(y, sa[i], sa[i - 1], k) ? m - 1 : m++; n     } n     if (m >= n) break; n   } n } n void buildH() { n   for (int i = 0; i < n; i++) rk[sa[i]] = i; n   height[0] = 0; n   int k = 0; n   for (int i = 0; i < n; i++) { n     if (!rk[i]) continue; n     int j = sa[rk[i] - 1]; n     if (k) k--; n     while (s[i + k] == s[j + k]) k++; n     height[rk[i]] = k; n   } n } n int main() { n   scanf( %s , t); n   int len1 = strlen(t); n   for (int i = 0; t[i]; i++) s[n++] = t[i]; n   s[n++] =  # ; n   scanf( %s , t); n   for (int i = 0; t[i]; i++) s[n++] = t[i]; n   buildSa(1000); n   buildH(); n   int ans = 1e9 + 7; n   for (int i = 1; i < n; i++) { n     if (height[i]) { n       if ((sa[i] >= len1 && sa[i - 1] < len1) || n           (sa[i] < len1 && sa[i - 1] >= len1)) { n         if (height[i - 1] < height[i] && height[i + 1] < height[i]) n           ans = min(ans, max(height[i - 1], height[i + 1]) + 1); n       } n     } n   } n   if (ans == 1e9 + 7) ans = -1; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int sz; n vector<pair<int, string> > e; n int v[2][200009]; n int c[200009]; n int Call(int in, int count) { n   if (c[in] != -1) { n     Call(c[in] - 1, count + 1); n   } else { n     int i; n     printf(  n%d  , count + 1); n   } n } n int Pr(int in) { n   printf( %d  , in + 1); n   if (c[in] != -1) { n     Pr(c[in] - 1); n     c[in] = 0; n   } else { n     c[in] = 0; n   } n } n int main() { n   int i, j, k = 0, n = 0, m = 0; n   cin >> s; n   sz = s.size(); n   for (i = 0; i < sz; i++) { n     if (s[i] - 48 == 0) n       v[0][n++] = i + 1; n     else n       v[1][m++] = i + 1; n   } n   int u = 0; n   for (i = 0, j = 0; i < sz; i++) { n     if (s[i] - 48 == 0) { n       if (j == m) n         c[i] = -1; n       else if (i + 1 > v[1][j]) n         return 0 * printf( -1 ); n       else { n         c[i] = v[1][j]; n         j++; n       } n     } else { n       while (i + 1 > v[0][u]) { n         u++; n         if (u >= n) return 0 * printf( -1 ); n       } n       c[i] = v[0][u]; n       u++; n     } n   } n   string q; n   for (i = 0; i < sz; i++) n     if (c[i] == -1) k++; n   printf( %d , k); n   for (i = 0; i < sz; i++) { n     q = i + 1 + 48; n     if (c[i] == -1) n       printf(  n1 %d , i + 1); n     else if (c[i] != 0) { n       Call(i, 0); n       Pr(i); n     } n   } n } n


#include <bits/stdc++.h> n const int N = 105; n char matrix[N][N]; n void solve(int i, int j, int type) { n   char c =  a ; n   switch (type) { n     case 0: n       while (1) { n         if (matrix[i - 1][j] == c || matrix[i - 1][j + 1] == c || n             matrix[i + 1][j] == c || matrix[i + 1][j + 1] == c || n             matrix[i][j - 1] == c || matrix[i][j + 2] == c) { n           c = (char)(c + 1); n         } else { n           break; n         } n       } n       matrix[i][j] = matrix[i][j + 1] = c; n       break; n     case 1: n       while (1) { n         if (matrix[i - 1][j] == c || matrix[i + 2][j] == c || n             matrix[i][j - 1] == c || matrix[i + 1][j - 1] == c || n             matrix[i][j + 1] == c || matrix[i + 1][j + 1] == c) { n           c = (char)(c + 1); n         } else { n           break; n         } n       } n       matrix[i][j] = matrix[i + 1][j] = c; n       break; n     case 2: n       while (1) { n         if (matrix[i - 1][j] == c || matrix[i - 1][j + 1] == c || n             matrix[i + 1][j] == c || matrix[i + 1][j + 1] == c || n             matrix[i][j - 1] == c || matrix[i + 1][j - 1] == c || n             matrix[i][j + 2] == c || matrix[i + 1][j + 2] == c) { n           c = (char)(c + 1); n         } else { n           break; n         } n       } n       matrix[i][j] = matrix[i + 1][j] = matrix[i][j + 1] = n           matrix[i + 1][j + 1] = c; n       break; n   } n } n int main() { n   int n, m, a, b, c, i, j; n   bool ok = true; n   scanf( %d%d%d%d%d , &n, &m, &a, &b, &c); n   if (n & 1 && m & 1) { n     ok = false; n   } else if (n & 1) { n     for (i = 1; i <= n - 1 && ok; i += 2) { n       for (j = 1; j <= m && ok; j += 2) { n         if (c) { n           solve(i, j, 2); n           c--; n         } else if (b >= 2) { n           solve(i, j, 1); n           solve(i, j + 1, 1); n           b -= 2; n         } else if (a >= 2) { n           solve(i, j, 0); n           solve(i + 1, j, 0); n           a -= 2; n         } else { n           ok = false; n         } n       } n     } n     for (j = 1; j <= m && ok; j += 2) { n       if (a) { n         solve(i, j, 0); n         a--; n       } else { n         ok = false; n       } n     } n   } else if (m & 1) { n     for (i = 1; i <= n && ok; i += 2) { n       for (j = 1; j <= m - 1 && ok; j += 2) { n         if (c) { n           solve(i, j, 2); n           c--; n         } else if (a >= 2) { n           solve(i, j, 0); n           solve(i + 1, j, 0); n           a -= 2; n         } else if (b >= 2) { n           solve(i, j, 1); n           solve(i, j + 1, 1); n           b -= 2; n         } else { n           ok = false; n         } n       } n     } n     for (i = 1; i <= n && ok; i += 2) { n       if (b) { n         solve(i, j, 1); n         b--; n       } else { n         ok = false; n       } n     } n   } else { n     for (i = 1; i <= n && ok; i += 2) { n       for (j = 1; j <= m && ok; j += 2) { n         if (c) { n           solve(i, j, 2); n           c--; n         } else if (a >= 2) { n           solve(i, j, 0); n           solve(i + 1, j, 0); n           a -= 2; n         } else if (b >= 2) { n           solve(i, j, 1); n           solve(i, j + 1, 1); n           b -= 2; n         } else { n           ok = false; n         } n       } n     } n   } n   if (ok) { n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         printf( %c , matrix[i][j]); n       } n       printf(  n ); n     } n   } else { n     printf( IMPOSSIBLE n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> ans(n); n   for (int i = 0; i < n; i++) { n     ans[i] = i + 1; n   } n   vector<int> check; n   do { n     int sm = 0; n     for (int i = 0; i < n; i++) { n       for (int j = i; j < n; j++) { n         int mn = INF; n         for (int y = i; y <= j; y++) { n           mn = min(mn, ans[y]); n         } n         sm += mn; n       } n     } n     check.push_back(sm); n   } while (next_permutation(ans.begin(), ans.end())); n   int mx = -1; n   for (int i = 0; i < check.size(); i++) { n     mx = max(mx, check[i]); n   } n   int now = 0, s = 0; n   for (int i = 0; i < check.size(); i++) { n     if (check[i] == mx) { n       now++; n       if (now == m) { n         break; n       } n     } n     next_permutation(ans.begin(), ans.end()); n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e2 + 5; n const int INF = 1e9 + 5; n const int mod = 1000000007; n const double eps = 1e-7; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   long long a, b, k; n   while (cin >> k >> a >> b) { n     if (b <= 0) { n       a = -a; n       b = -b; n       swap(a, b); n     } n     long long ans = b / k - a / k; n     if (a % k == 0 || a < 0) ans++; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n const int M = 7; n int n, p, k; n int s[N + 1][M + 1]; n long long dp[N + 1][1 << M + 1]; n pair<int, int> a[N + 1]; n long long go(int idx, int mask) { n   if (idx > n) { n     if (mask == ((1 << p) - 1)) { n       return 0; n     } else { n       return -1e18; n     } n   } n   if (dp[idx][mask] != -1) { n     return dp[idx][mask]; n   } n   int players = __builtin_popcount(mask); n   int aud = idx - players - 1; n   long long ans = 0; n   if (players < p) { n     for (int i = 0; i < p; i++) { n       int val = mask & (1 << i); n       if (!val) { n         ans = max(ans, 1LL * s[a[idx].second][i + 1] + n                            1LL * go(idx + 1, mask | (1 << i))); n       } n     } n   } n   if (aud < k) { n     ans = max(ans, 1LL * a[idx].first + 1LL * go(idx + 1, mask)); n   } else { n     ans = max(ans, go(idx + 1, mask)); n   } n   return dp[idx][mask] = ans; n } n int main() { n   cin >> n >> p >> k; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     a[i] = {x, i}; n   } n   sort(a + 1, a + n + 1); n   reverse(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= p; j++) { n       cin >> s[i][j]; n     } n   } n   memset(dp, -1, sizeof(dp)); n   cout << go(1, 0); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<short> g[10003]; n int main() { n   scanf( %d , &n); n   int maxi; n   for (maxi = 1; ((maxi * maxi - maxi) >> 1) <= n; maxi++) n     ; n   maxi--; n   int end = 1; n   int i, j; n   for (i = 1; i < maxi; i++) { n     for (j = 0; j < maxi - i; j++) g[i].push_back(end + j); n     for (j = i + 1; j <= maxi; j++) g[j].push_back(end + (j - i - 1)); n     end += maxi - i; n   } n   printf( %d n , maxi); n   for (i = 1; i <= maxi; i++) { n     printf( %d , (int)g[i][0]); n     for (j = 1; j < g[i].size(); j++) printf(  %d , (int)g[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T x, T y) { n   if (x < y) swap(x, y); n   while (y > 0) { n     T f = x % y; n     x = y; n     y = f; n   } n   return x; n } n template <typename T> n pair<T, T> exgcd(T x, T y) { n   if (x < y) swap(x, y); n   pair<T, T> r = make_pair(1, 0); n   while (y > 0) { n     T f = x % y; n     r = make_pair(r.second, r.first - (x / y) * r.second); n     x = y; n     y = f; n   } n   return r; n } n const int maxn = 1e5; n int n; n int P[maxn + 1], T[maxn + 1]; n int SP[maxn + 1]; n int TP[maxn + 1]; n int lvl[maxn + 1]; n vector<int> child[maxn + 1]; n int bg[maxn + 1]; n int ed[maxn + 1]; n int tt = 0; n void dfs(int u, bool sp = false) { n   bg[u] = tt++; n   for (auto c : child[u]) lvl[c] = lvl[u] + 1, dfs(c); n   ed[u] = tt++; n } n bool isAsc(int u, int v) { return bg[u] <= bg[v] && ed[v] <= ed[u]; } n int main(int argc, char *argv[]) { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   cin >> n; n   { n     for (int i = 1; i <= n; i++) { n       cin >> P[i] >> T[i]; n       if (T[i] == 0) { n         SP[i] = SP[P[i]]; n         TP[i] = i; n       } else if (T[i] == 1) { n         SP[i] = i; n         TP[i] = TP[P[i]]; n       } else { n         TP[i] = i; n         SP[i] = i; n       } n       bg[i] = ed[i] = 0; n     } n     for (int i = 1; i <= n; i++) { n       if (P[i] != -1) { n         child[P[i]].push_back(i); n       } n     } n     tt = 1; n     for (int i = 1; i <= n; i++) { n       if (!bg[i]) { n         lvl[i] = 1; n         dfs(i); n       } n     } n     int q; n     cin >> q; n     while (q--) { n       int t, u, v, ov; n       cin >> t >> u >> v; n       if (u == v) { n         cout <<  NO  << endl; n         continue; n       } n       if (t == 1) { n         if ((isAsc(u, v) && lvl[SP[v]] <= lvl[u])) n           cout <<  YES  << endl; n         else n           cout <<  NO  << endl; n       } else { n         int av = TP[v]; n         if (av != v && (isAsc(av, u) && lvl[SP[u]] <= lvl[av])) n           cout <<  YES  << endl; n         else { n           int au = SP[u]; n           if (au != v && isAsc(au, v) && lvl[TP[v]] <= lvl[au]) n             cout <<  YES  << endl; n           else n             cout <<  NO  << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 300010, modP = 1e9 + 7, oo = 23041997, maxB = 600; n const double PI = acos(-1); n int prime[maxN], a[maxN], cnt[maxN], n, POW[maxN], res, p[maxN], maxv; n void sieve() { n   for (int i = 2; i <= maxv; ++i) n     if (prime[i] == 0) { n       int s = 2 * i; n       while (s <= maxv) { n         prime[s] = i; n         s += i; n       } n     } n } n void factorize(int x) { n   p[0] = 0; n   while (prime[x]) { n     p[++p[0]] = prime[x]; n     x /= prime[x]; n   } n   p[++p[0]] = x; n } n int main() { n   cin >> n; n   maxv = 1; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     maxv = max(maxv, a[i]); n     for (int j = 1; j <= (int)sqrt(a[i]); ++j) n       if (a[i] % j == 0) { n         ++cnt[j]; n         if (a[i] / j != j) ++cnt[a[i] / j]; n       } n   } n   POW[0] = 1; n   for (int i = 1; i <= n; ++i) POW[i] = (1ll * POW[i - 1] * 2) % modP; n   sieve(); n   res = POW[n] - 1; n   for (int i = 2; i <= maxv; ++i) { n     factorize(i); n     bool check = true; n     for (int j = 2; j <= p[0]; ++j) n       if (p[j] == p[j - 1]) check = false; n     if (check && cnt[i]) { n       int tmp = (((POW[cnt[i]] - 1) % modP) + modP) % modP; n       if ((p[0] % 2) == 0) n         res = (res + tmp) % modP; n       else n         res = (((res - tmp) % modP) + modP) % modP; n     } n   } n   cout << res; 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     string s; n     cin >> s; n     string ans; n     for (int i = 0; i < n; i++) { n       ans += s[2 * i]; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<long long, long long>; n using pcc = pair<char, char>; n using pdd = pair<double, double>; n using vi = vector<int>; n using vs = vector<string>; n using vll = vector<ll>; n using vpii = vector<pii>; n using vc = vector<char>; n using vd = vector<double>; n bool cmp(pii a, pii b) { n   if (a.second * b.first < b.second * a.first) return true; n   return false; n } n int main(int argc, char* argv[]) { n   ll t, a, b, p, i, j, l; n   char str[200100]; n   scanf( %d%*c , &(t)); n   while (t--) { n     scanf( %lld %lld %lld , &a, &b, &p); n     scanf( %s , str); n     l = strlen(str); n     ll pre = 0, tot = 0; n     for (i = 1; i < l; i++) { n       if (str[i] != str[pre]) { n         if (str[pre] ==  A ) { n           tot += a; n         } else n           tot += b; n         pre = i; n       } n     } n     if (pre < l - 1) { n       if (str[pre] ==  A ) { n         tot += a; n       } else n         tot += b; n     } n     pre = 0; n     for (i = 0; i < l; i++) { n       if (tot <= p) break; n       if (str[i] != str[pre]) { n         if (str[pre] ==  A ) { n           tot -= a; n         } else n           tot -= b; n         pre = i; n       } n     } n     if (tot > p) n       pre = l; n     else n       pre++; n     printf( %lld , (pre)), printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long n, d; n   cin >> n >> d; n   long long k = 0, c = 0, a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   c = 0; n   for (long long i = 1; i < n; i++) { n     if (a[i - 1] < a[i]) continue; n     k = a[i - 1] - a[i] + d; n     c += k / d; n     a[i] += (k / d) * d; n   } n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, z, l, i, lc; n   cin >> n >> m >> z; n   l = max(n, m); n   for (i = l; i <= n * m; i += l) { n     if (i % n == 0 && i % m == 0) break; n   } n   cout << z / i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> g(n + 1); n   vector<int> a(n); n   for (int i = 0; i < m; i++) { n     int maxj = 0; n     for (int j = 0; j < n; j++) { n       cin >> a[j]; n       if (a[j] > a[maxj]) n         maxj = j; n       else if (a[j] == a[maxj]) { n         if (j < maxj) maxj = j; n       } n     } n     g[maxj + 1]++; n     a.resize(n); n   } n   int ma = 0; n   for (int i = 1; i <= n; i++) { n     if (g[i] > g[ma]) n       ma = i; n     else if (g[i] == g[ma]) { n       if (i < ma) ma = i; n     } n   } n   cout << ma; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   int len = str.length(); n   int mx = 0; n   for (int j = 0; j < len; j++) { n     char ch = str[j]; n     str[j] =  V ; n     int ans = 0; n     bool flag = false; n     for (int i = 0; i < len; i++) { n       if (str[i] ==  V ) n         flag = true; n       else { n         if (str[i] ==  K  && flag) { n           ans++; n           flag = false; n         } else n           flag = false; n       } n     } n     str[j] = ch; n     mx = max(ans, mx); n   } n   for (int j = 0; j < len; j++) { n     char ch = str[j]; n     str[j] =  K ; n     int ans = 0; n     bool flag = false; n     for (int i = 0; i < len; i++) { n       if (str[i] ==  V ) n         flag = true; n       else { n         if (str[i] ==  K  && flag) { n           ans++; n           flag = false; n         } else n           flag = false; n       } n     } n     str[j] = ch; n     mx = max(ans, mx); n   } n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) { n     long long n; n     cin >> n; n     vector<long long int> a(n); n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     vector<long long int> t(n); n     t[0] = 1; n     for (long long int i = 1; i < n; i++) t[i] = max(a[i] + 1, t[i - 1]); n     for (int i = n - 2; i >= 0; i--) t[i] = max(t[i + 1] - 1, t[i]); n     long long s = 0; n     for (long long int i = 0; i < n; i++) s += t[i] - a[i] - 1; n     cout << s; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-8; n const int mod = 1e9 + 7; n const int inf = 1061109567; n const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; n map<int, int> mp[100005], vis; n int tot; n int lowbit(int x) { return x & (-x); } n void update(int t, int x, int val) { n   for (int i = t; i <= 1e9 + 2; i += lowbit(i)) { n     mp[x][i] += val; n   } n } n int query(int t, int x) { n   int ret = 0; n   for (int i = t; i > 0; i -= lowbit(i)) { n     ret += mp[x][i]; n   } n   return ret; n } n int main() { n   int n, sign, t, x; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &sign, &t, &x); n     if (!vis[x]) { n       vis[x] = ++tot; n     } n     if (sign == 1) { n       update(t, vis[x], 1); n     } else if (sign == 2) { n       update(t, vis[x], -1); n     } else { n       printf( %d n , query(t, vis[x])); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n int a[100005]; n int main() { n   int n; n   scanf( %d , &n); n   bool one = true; n   for (int i = 0; i < n + 1; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i < n + 1; ++i) { n     if (a[i - 1] > 1 && a[i] > 1) { n       one = false; n       break; n     } n   } n   if (one) { n     cout <<  perfect ; n     return 0; n   } n   cout <<  ambiguous n0  ; n   int c = 2; n   for (int i = 1; i < n + 1; ++i) { n     if (a[i - 1] == 1) { n       for (int j = 0; j < a[i]; ++j) { n         printf( %d  , c - 1); n       } n       c += a[i]; n     } else { n       for (int j = 0; j < a[i]; ++j) { n         printf( %d  , c - 1); n       } n       c += a[i]; n     } n   } n   cout <<   n0  ; n   c = 2; n   for (int i = 1; i < n + 1; ++i) { n     if (a[i - 1] == 1) { n       for (int j = 0; j < a[i]; ++j) { n         printf( %d  , c - 1); n       } n       c += a[i]; n     } else { n       for (int j = 0; j < a[i]; ++j) { n         printf( %d  , c - ((j + 1) % a[i - 1]) - 1); n       } n       c += a[i]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD9 = 1e9 + 7; n const int MOD91 = 1e9 + 9; n const unsigned long long MOD12 = 1e12 + 39LL; n const unsigned long long MOD15 = 1e15 + 37LL; n const int INF = 1e9; n const int base = 1e9; n const int MAX = 2e5; n const long double EPS = 1e-10; n string second; n int n, k; n int a[30][30]; n int dpt[101][30][101]; n int dp(int pos, int prev, int left) { n   if (pos == second.size()) { n     return 0; n   } n   if (dpt[pos][prev][left] != -1) { n     return dpt[pos][prev][left]; n   } n   if (left == 0) { n     return dpt[pos][prev][left] = dp(pos + 1, second[pos] -  a , left) + n                                   a[prev][second[pos] -  a ]; n   } n   int b = -INF; n   for (int i = (0); i < (26); ++i) { n     b = max(b, n             dp(pos + 1, i, left - (i != int(second[pos] -  a ))) + a[prev][i]); n   } n   return dpt[pos][prev][left] = b; n } n int main() { n   cin >> second >> k; n   cin >> n; n   memset(dpt, -1, sizeof(dpt)); n   for (int i = (0); i < (n); ++i) { n     char x, y; n     int xx; n     cin >> x >> y >> xx; n     a[x -  a ][y -  a ] = xx; n   } n   cout << dp(0, 27, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 85; n double l[N], r[N], ans[N][N], ago[N], now[N], dp[N][N]; n int main() { n   vector<double> g; n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %lf%lf , &l[i], &r[i]); n     g.push_back(l[i]), g.push_back(r[i]); n   } n   sort(g.begin(), g.end()); n   g.erase(unique(g.begin(), g.end()), g.end()); n   vector<pair<double, double> > p; n   for (int i = 0; i < g.size() - 1; ++i) p.push_back(make_pair(g[i], g[i + 1])); n   for (int t = 0; t < p.size(); ++t) { n     for (int i = 1; i <= n; ++i) { n       double L = p[t].first, R = p[t].second; n       if (!(l[i] >= R || r[i] <= L)) { n         for (int j = 0; j <= n; ++j) n           for (int k = 0; k <= n; ++k) dp[j][k] = 0.0; n         dp[0][0] = 1.0; n         int cnt = 0; n         vector<int> used; n         for (int k = 1; k <= n; ++k) { n           if (l[k] >= R || r[k] <= L) { n             now[k] = 0.0; n             if (r[k] <= L) cnt++; n           } else n             now[k] = (R - L) / (r[k] - l[k]), used.push_back(k); n         } n         int m = used.size(); n         for (int _ = 0; _ < m; ++_) { n           int x = used[_]; n           if (x == i) continue; n           for (int k = _ + 1; k >= 0; --k) n             for (int o = _ + 1 - k; o >= 0; --o) { n               dp[k + 1][o] += dp[k][o] * ago[x]; n               dp[k][o + 1] += dp[k][o] * now[x]; n               dp[k][o] *= 1.0 - now[x] - ago[x]; n             } n         } n         for (int j = 0; j < m; ++j) n           for (int k = 0; k < m - j; ++k) { n             ans[i][cnt + j] += dp[j][k] * now[i] / (1 + k); n             ans[i][cnt + j + k + 1] -= dp[j][k] * now[i] / (1 + k); n           } n       } n     } n     for (int i = 1; i <= n; ++i) ago[i] += now[i]; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (j) { n         ans[i][j] += ans[i][j - 1]; n       } n       printf( %.12f%c , ans[i][j], j == n - 1 ?   n  :    ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double p, e[100010], dp[100010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lf , &p); n     e[i] = p * (e[i - 1] + 1); n     dp[i] = dp[i - 1] + (2 * e[i - 1] + 1) * p; n   } n   printf( %.9lf , dp[n]); n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n const ll MAX = 1e5 + 100; n const ll inf = 1e9; n const ll mod = 998244353; n ll a[MAX], c[MAX], t[MAX]; n string b[] = { 50 ,  25 ,  75 ,  00 }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ll tc = 1, n, m, ans = 0, v, r; n   cin >> n >> m; n   for (ll i = 1; i < n + 1; i++) cin >> c[i] >> t[i]; n   ll ini = 1, time = 0; n   while (m--) { n     cin >> v; n     while (v > time + c[ini] * t[ini]) time += c[ini] * t[ini], ini++; n     cout << ini << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000006, LG = 21, mod = 1e9 + 7, M = N; n const double eps = 1e-3, pi = acos(-1.0); n const long long INF = 1123456789123456789LL; n int main() { n   string str; n   cin >> str; n   char mi =  a ; n   bool ans = true; n   for (char c : str) n     if (c > mi + 1) { n       ans = false; n       break; n     } else n       mi = max(mi, c); n   if (ans && str[0] ==  a ) n     printf( YES n ); n   else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005], n, s, ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   while (n--) { n     long long x; n     cin >> x; n     s += x; n     a[x]++; n   } n   if (s == 1 or s == 2 or s == 5) return cout << -1, 0; n   while (a[1] > 0 and a[2] > 0) { n     a[3]++; n     ans++; n     a[1]--; n     a[2]--; n   } n   if (a[1] > 0) { n     ans += a[1] - a[1] / 3; n     if (a[1] == 1 and a[3] == 0) ans++; n   } n   if (a[2] > 0) { n     ans += a[2] - a[2] / 3; n     if (a[2] % 3 == 1 and a[4] == 0) ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Inf = 101000; n int tot, n, root, a[1001000], temp, tx, ty, rx, rxl; n struct Node { n   int lc, rc, x, y, X, Y, l, r, L, R, minx, maxn, lmax, rmax, lmin, rmin, sum, n       tag; n } tree[1001000]; n pair<int, int> key[1111]; n void update(int p, int lc, int rc) { n   tree[p].sum = tree[lc].sum + tree[rc].sum; n   int t = (tree[lc].maxn > tree[rc].maxn) ? lc : rc; n   if (tree[lc].rmax + tree[rc].lmax > tree[t].maxn) { n     tree[p].maxn = tree[lc].rmax + tree[rc].lmax; n     tree[p].X = tree[lc].x; n     tree[p].Y = tree[rc].y; n   } else { n     tree[p].maxn = tree[t].maxn; n     tree[p].X = tree[t].X; n     tree[p].Y = tree[t].Y; n   } n   if (tree[lc].lmax > tree[lc].sum + tree[rc].lmax) { n     tree[p].lmax = tree[lc].lmax; n     tree[p].y = tree[lc].y; n   } else { n     tree[p].lmax = tree[lc].sum + tree[rc].lmax; n     tree[p].y = tree[rc].y; n   } n   if (tree[rc].rmax > tree[rc].sum + tree[lc].rmax) { n     tree[p].rmax = tree[rc].rmax; n     tree[p].x = tree[rc].x; n   } else { n     tree[p].rmax = tree[rc].sum + tree[lc].rmax; n     tree[p].x = tree[lc].x; n   } n   t = (tree[lc].minx < tree[rc].minx) ? lc : rc; n   if (tree[lc].rmin + tree[rc].lmin < tree[t].minx) { n     tree[p].minx = tree[lc].rmin + tree[rc].lmin; n     tree[p].L = tree[lc].l; n     tree[p].R = tree[rc].r; n   } else { n     tree[p].minx = tree[t].minx; n     tree[p].L = tree[t].L; n     tree[p].R = tree[t].R; n   } n   if (tree[lc].lmin < tree[lc].sum + tree[rc].lmin) { n     tree[p].lmin = tree[lc].lmin; n     tree[p].r = tree[lc].r; n   } else { n     tree[p].lmin = tree[lc].sum + tree[rc].lmin; n     tree[p].r = tree[rc].r; n   } n   if (tree[rc].rmin < tree[rc].sum + tree[lc].rmin) { n     tree[p].rmin = tree[rc].rmin; n     tree[p].l = tree[rc].l; n   } else { n     tree[p].rmin = tree[rc].sum + tree[lc].rmin; n     tree[p].l = tree[lc].l; n   } n } n void push(int p, int l, int r) { n   swap(tree[p].maxn, tree[p].minx); n   swap(tree[p].lmax, tree[p].lmin); n   swap(tree[p].rmax, tree[p].rmin); n   swap(tree[p].x, tree[p].l); n   swap(tree[p].y, tree[p].r); n   swap(tree[p].X, tree[p].L); n   swap(tree[p].Y, tree[p].R); n   tree[p].maxn *= -1; n   tree[p].lmax *= -1; n   tree[p].rmax *= -1; n   tree[p].minx *= -1; n   tree[p].lmin *= -1; n   tree[p].rmin *= -1; n   tree[p].sum *= -1; n   tree[p].tag *= -1; n   if (tree[p].lc) tree[tree[p].lc].tag *= -1; n   if (tree[p].rc) tree[tree[p].rc].tag *= -1; n } n int build(int l, int r) { n   int p = ++tot; n   tree[p].tag = -Inf; n   if (l == r) { n     tree[p].lc = tree[p].rc = 0; n     tree[p].sum = a[l]; n     tree[p].lmax = tree[p].rmax = tree[p].maxn = a[l]; n     tree[p].x = tree[p].y = tree[p].X = tree[p].Y = l; n     tree[p].lmin = tree[p].rmin = tree[p].minx = a[l]; n     tree[p].l = tree[p].r = tree[p].L = tree[p].R = l; n   } else { n     int mid = (l + r) >> 1; n     tree[p].lc = build(l, mid); n     tree[p].rc = build(mid + 1, r); n     update(p, tree[p].lc, tree[p].rc); n   } n   return p; n } n void ins(int p, int x, int y, int z, int l, int r) { n   if (tree[p].tag != -Inf) push(p, l, r); n   if (x <= l && r <= y) n     tree[p].tag *= -1; n   else { n     int mid = (l + r) >> 1; n     if (x <= mid) ins(tree[p].lc, x, y, z, l, mid); n     if (y > mid) ins(tree[p].rc, x, y, z, mid + 1, r); n     if (tree[p].lc && tree[tree[p].lc].tag != -Inf) push(tree[p].lc, l, mid); n     if (tree[p].rc && tree[tree[p].rc].tag != -Inf) n       push(tree[p].rc, mid + 1, r); n     update(p, tree[p].lc, tree[p].rc); n   } n } n void change(int p, int x, int y, int l, int r) { n   if (tree[p].tag != -Inf) push(p, l, r); n   if (l == r && l == x) { n     tree[p].sum = y; n     tree[p].lmax = tree[p].rmax = tree[p].maxn = y; n     tree[p].lmin = tree[p].rmin = tree[p].minx = y; n   } else { n     int mid = (l + r) >> 1; n     if (x <= mid) n       change(tree[p].lc, x, y, l, mid); n     else n       change(tree[p].rc, x, y, mid + 1, r); n     if (tree[p].lc && tree[tree[p].lc].tag != -Inf) push(tree[p].lc, l, mid); n     if (tree[p].rc && tree[tree[p].rc].tag != -Inf) n       push(tree[p].rc, mid + 1, r); n     update(p, tree[p].lc, tree[p].rc); n   } n } n void merge(int p) { n   if (tx == -1) { n     temp = tree[p].maxn; n     rx = tree[p].rmax; n     rxl = tree[p].x; n     tx = tree[p].X; n     ty = tree[p].Y; n   } else { n     if (max(tree[p].maxn, tree[p].lmax + rx) > temp) { n       if (tree[p].maxn > tree[p].lmax + rx) { n         temp = tree[p].maxn; n         tx = tree[p].X; n         ty = tree[p].Y; n       } else { n         temp = tree[p].lmax + rx; n         tx = rxl; n         ty = tree[p].y; n       } n     } n     if (tree[p].rmax > tree[p].sum + rx) { n       rx = tree[p].rmax; n       rxl = tree[p].x; n     } else n       rx += tree[p].sum; n   } n } n void query(int p, int x, int y, int l, int r) { n   if (tree[p].tag != -Inf) push(p, l, r); n   if (x <= l && r <= y) n     merge(p); n   else { n     int mid = (l + r) >> 1; n     if (x <= mid) query(tree[p].lc, x, y, l, mid); n     if (y > mid) query(tree[p].rc, x, y, mid + 1, r); n     if (tree[p].lc && tree[tree[p].lc].tag != -Inf) push(tree[p].lc, l, mid); n     if (tree[p].rc && tree[tree[p].rc].tag != -Inf) n       push(tree[p].rc, mid + 1, r); n     update(p, tree[p].lc, tree[p].rc); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   tot = 0; n   root = build(1, n); n   int T; n   for (scanf( %d , &T); T; T--) { n     int opt, L, R, x; n     scanf( %d , &opt); n     if (opt) { n       scanf( %d%d%d , &L, &R, &x); n       int ans = 0, t = 0; n       for (int i = 1; i <= x; i++) { n         temp = -1; n         rxl = rx = tx = ty = 0; n         query(root, L, R, 1, n); n         if (temp >= 0) { n           ans += temp; n           key[i] = make_pair(tx, ty); n           ins(root, tx, ty, Inf, 1, n); n         } else n           break; n         t = i; n       } n       printf( %d n , ans); n       for (int i = t; i; i--) ins(root, key[i].first, key[i].second, Inf, 1, n); n     } else { n       scanf( %d%d , &L, &x); n       change(root, L, x, 1, n); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, A[50], B[1001], i, X, C[50], t = 0; n   scanf( %d , &n); n   for (i = 0; i <= 1000; i++) { n     B[i] = 0; n   } n   for (i = 0; i < n; i++) { n     scanf( %d , &A[i]); n     X = A[i]; n     B[X] += 1; n   } n   for (i = 0; i < n; i++) { n     X = A[i]; n     if (B[X] == 1) { n       C[t] = X; n       t++; n     } else { n       B[X] -= 1; n     } n   } n   printf( %d n , t); n   for (i = 0; i < t; i++) { n     if (i == t - 1) { n       printf( %d n , C[i]); n     } else { n       printf( %d  , C[i]); 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   string a; n   cin >> a; n   if (a.size() > 26) { n     cout <<  -1 n ; n   } else { n     int count = 0; n     int hash[26] = {0}; n     for (int i = 0; i < a.size(); i++) { n       if (hash[a[i] - 97]) n         count++; n       else n         hash[a[i] - 97]++; n     } n     cout << count <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double pi = acos(-1); n void OJ() {} n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   vector<string> v; n   long long num = 0, k = 0; n   v.push_back( 4 ), v.push_back( 7 ); n   while (1) { n     long long n = v.size(); n     for (long long i = 0; i < n; i++) { n       for (long long j = k; j < n; j++) { n         string s = v[i] + v[j]; n         v.push_back(s); n         num = max(num, stoll(s, nullptr, 10)); n         if (num >= 1e9) break; n       } n       if (num >= 1e9) break; n     } n     k = n; n     if (num >= 1e9) break; n   } n   vector<long long> ar; n   for (auto s : v) { n     ar.push_back(stoll(s, nullptr, 10)); n   } n   long long i = 0; n   sort(ar.begin(), ar.end()); n   unordered_map<long long, long long> m; n   for (auto s : ar) { n     if (m.find(s) == m.end()) m[s] = (++i); n   } n   long long in; n   cin >> in; n   cout << m[in]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   vector<vector<int>> g(n); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   vector<bool> alive(n, true); n   vector<int> sz(n); n   vector<int> pv(n); n   vector<pair<int, int>> ret; n   vector<int> all; n   vector<int> tin(n); n   vector<int> tout(n); n   int T = -1; n   function<void(int)> Dfs = [&](int v) { n     all.push_back(v); n     tin[v] = ++T; n     sz[v] = 1; n     for (int u : g[v]) { n       if (alive[u] && u != pv[v]) { n         pv[u] = v; n         Dfs(u); n         sz[v] += sz[u]; n       } n     } n     tout[v] = ++T; n   }; n   function<void(int)> Solve = [&](int v) { n     all.clear(); n     pv[v] = -1; n     Dfs(v); n     int total = sz[v]; n     while (true) { n       bool found = false; n       for (int u : g[v]) { n         if (alive[u] && pv[u] == v && 2 * sz[u] >= total) { n           v = u; n           found = true; n           break; n         } n       } n       if (!found) { n         break; n       } n     } n     alive[v] = false; n     set<int> best; n     for (int u : g[v]) { n       if (alive[u]) { n         all.clear(); n         pv[u] = -1; n         Dfs(u); n         set<int> cur; n         for (int w : g[u]) { n           if (alive[w]) { n             cur.insert(w); n           } n         } n         if (cur.size() > best.size()) { n           best = cur; n         } n       } n     } n     all.clear(); n     pv[v] = -1; n     Dfs(v); n     for (int u : all) { n       if (u != v && pv[u] != v && best.find(u) == best.end()) { n         ret.emplace_back(u, v); n       } n     } n     vector<int> children; n     for (int u : g[v]) { n       if (alive[u]) { n         children.push_back(u); n       } n     } n     for (int u : children) { n       Solve(u); n     } n   }; n   Solve(0); n   assert((int)ret.size() <= 10 * n); n   cout << ret.size() <<   n ; n   for (auto& p : ret) { n     cout << p.first + 1 <<     << p.second + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fact(int x); n int main() { n   int a, b; n   cin >> a >> b; n   int m = min(a, b); n   cout << fact(m); n } n int fact(int x) { n   int sum = 1; n   for (int i = 2; i < x; i++) sum *= i; n   return sum * x; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const double PI = acos(-1.0); n const int MAXN = 1e6 + 5; n char arr[8][8]; n bool vis[8][8]; n int dx[] = {0, 0, 1, -1, -1, 1, -1, 1, 0}; n int dy[] = {1, -1, 0, 0, 1, 1, -1, -1, 0}; n bool dfs(int x, int y, int steps) { n   if (steps > 8) return true; n   vis[x][y] = true; n   if (arr[x][y] ==  A ) return true; n   bool ok = false; n   for (int i = 0; i < 9; i++) { n     int nx = x + dx[i], ny = y + dy[i]; n     if (nx < 0 || nx >= 8 || ny < 0 || ny >= 8) continue; n     if (nx - steps >= 0 && arr[nx - steps][ny] ==  S ) continue; n     if (nx - steps - 1 >= 0 && arr[nx - steps - 1][ny] ==  S ) continue; n     if (dfs(nx, ny, steps + 1)) return true; n   } n   return false; n } n void cp() { n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) cin >> arr[i][j]; n   bool win = dfs(7, 0, 0); n   if (win) n     cout <<  WIN ; n   else n     cout <<  LOSE ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   t = 1; n   while (t--) { n     cp(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long int n = ((int)s[1] -  0 ) + 10 * ((int)s[3] -  0 ) + n                     100 * ((int)s[4] -  0 ) + 1000 * ((int)s[2] -  0 ) + n                     10000 * ((int)s[0] -  0 ); n   long long int result = (n * n) % 100000; n   result = (result * result) % 100000; n   result = (result * n) % 100000; n   int digits = (int)log10(result + 1); n   digits = 4 - digits; n   for (int i = 0; i < digits; i++) cout <<  0 ; n   cout << result; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   int c = 0; n   if (k == 0) n     cout << s << endl; n   else if (n == 1) { n     cout <<  0  << endl; n   } else if (k >= 1 && n > 1) { n     int x = s[0] -  0 ; n     if (x > 1) { n       s[0] =  1 ; n       k--; n     } n     for (int i = 1; i < n && k > 0; i++) { n       x = s[i] -  0 ; n       if (x > 0) { n         s[i] =  0 ; n         k--; n       } n     } n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n int n, i, j, dn, x; n long long Min = 200000000000000000, Max = 0; n int main() { n   cin >> n; n   dn = n; n   for (i = 1; i * i <= n; i++) n     if (dn % i == 0) { n       a.push_back(i); n       if (dn != i * i) a.push_back(dn / i); n     } n   for (i = 0; i < a.size(); i++) { n     dn = n / a[i]; n     for (j = 1; j * j <= dn; j++) n       if (dn % j == 0) { n         x = dn / j; n         Min = min(Min, (long long)(a[i] + 1) * (j + 2) * (x + 2)); n         Max = max(Max, (long long)(a[i] + 1) * (j + 2) * (x + 2)); n       } n   } n   cout << Min - n <<     << Max - n; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   return (b > 0) ? gcd(b, a % b) : a; n } n template <typename T> n T lcm(T a, T b) { n   return (a * b == 0) ? 0 : (a * b) / gcd(a, b); n } n void solution() { n   int candy; n   cin >> candy; n   int x = 0; n   for (; pow(x, 2) + x <= candy * 2; ++x) { n   } n   --x; n   cout << x << endl; n   int give = 1; n   while (x--) { n     if (x == 0) { n       cout << candy <<    ; n       return; n     } n     candy -= give; n     cout << give <<    ; n     ++give; n   } n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   int t = 1; n   while (t--) { n     solution(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned int tb[32] = {13, 0,  27, 1,  28, 18, 23, 2,  29, 21, 19, n                              12, 24, 9,  14, 3,  30, 26, 17, 22, 20, 11, n                              8,  13, 25, 16, 10, 7,  15, 6,  5,  4}; n int n, m, tot; n priority_queue<long long, std::vector<long long>, std::greater<long long> > q1, n     q2, q3; n void calc(priority_queue<long long, std::vector<long long>, n                          std::greater<long long> >& q, n           long long& ans) { n   while (q.size() > 1) { n     if (q.size() >= 3) { n       long long x = q.top(); n       q.pop(); n       long long y = q.top(); n       q.pop(); n       long long z = q.top(); n       q.pop(); n       ans += x + y + z; n       q.push(x + y + z); n     } else { n       ans += q.top(); n       q.pop(); n       ans += q.top(); n     } n   } n } n int main() { n   int T = 1; n   for (int ci = 1; ci <= T; ++ci) { n     scanf( %d , &n); n     for (int i = 0; i < n; ++i) { n       int x; n       scanf( %d , &x); n       q1.push(x); n       q2.push(x); n       q3.push(x); n     } n     long long ans1 = 0, ans2 = 0, ans3 = 0; n     if (q1.size() >= 2) { n       int x = q1.top(); n       q1.pop(); n       int y = q1.top(); n       q1.pop(); n       ans1 = x + y; n       q1.push(x + y); n     } n     if (q2.size() >= 4) { n       int x = q2.top(); n       q2.pop(); n       int y = q2.top(); n       q2.pop(); n       ans2 = x + y; n       int u = q2.top(); n       q2.pop(); n       int v = q2.top(); n       q2.pop(); n       ans2 += u + v; n       q2.push(x + y); n       q2.push(u + v); n     } n     calc(q1, ans1); n     calc(q2, ans2); n     calc(q3, ans3); n     long long ans = min(min(ans1, ans2), ans3); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Network { n   int N, source, sink; n   int M; n   struct Edge { n     int source, target, id, reverse; n   }; n   vector<vector<Edge>> adjacency_list; n   vector<int> capacities, flows, costs; n   Network(const int N, const int source, const int sink) n       : N(N), source(source), sink(sink), M(0), adjacency_list(N) {} n   void add_edge(const int v, const int w, const int capacity, const int cost) { n     const auto id = M++; n     capacities.push_back(capacity); n     costs.push_back(cost); n     flows.push_back(0); n     const auto reverse = M++; n     capacities.push_back(0); n     costs.push_back(-cost); n     flows.push_back(0); n     adjacency_list[v].push_back({v, w, id, reverse}); n     adjacency_list[w].push_back({w, v, reverse, id}); n   } n   int mcmf() { n     auto cost = 0; n     while (true) { n       const auto infinity = numeric_limits<int>::max(); n       auto distances = vector<int>(N, infinity); n       auto predecessors = vector<Edge>(N); n       auto queue = std::queue<int>(); n       distances[source] = 0; n       queue.push(source); n       while (!queue.empty()) { n         const auto v = queue.front(); n         queue.pop(); n         const auto d = distances[v]; n         for (const auto e : adjacency_list[v]) { n           if (flows[e.id] == capacities[e.id]) continue; n           const auto relax = d + costs[e.id]; n           if (relax < distances[e.target]) { n             distances[e.target] = relax; n             predecessors[e.target] = e; n             queue.push(e.target); n           } n         } n       } n       if (distances[sink] == infinity) n         break; n       else { n         cost += distances[sink]; n         for (auto v = sink; v != source; v = predecessors[v].source) { n           const auto e = predecessors[v]; n           flows[e.id]++; n           flows[e.reverse]--; n         } n       } n     } n     return cost; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cout << setprecision(4) << fixed; n   int n, a, b; n   cin >> n >> a >> b; n   auto p = vector<int>(n); n   for (auto i = 0; i < n; i++) { n     double x; n     cin >> x; n     p[i] = int(round(x * 1000)); n   } n   auto u = vector<int>(n); n   for (auto i = 0; i < n; i++) { n     double x; n     cin >> x; n     u[i] = int(round(x * 1000)); n   } n   auto source = n, sink = n + 1, pokeball = n + 2, ultraball = n + 3; n   auto network = Network(n + 4, source, sink); n   network.add_edge(source, pokeball, a, 0); n   network.add_edge(source, ultraball, b, 0); n   for (auto i = 0; i < n; i++) { n     network.add_edge(pokeball, i, 1, -p[i] * 1000); n     network.add_edge(ultraball, i, 1, -u[i] * 1000); n     network.add_edge(i, sink, 1, 0); n     network.add_edge(i, sink, 1, p[i] * u[i]); n   } n   cout << double(-network.mcmf()) / 1e6 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 10; n long long n; n pair<char, long long> ar[N]; n vector<long long> vec; n set<long long> st; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   n *= 2; n   for (long long i = 0; i < n; i++) { n     char c; n     long long x; n     cin >> c; n     if (c ==  + ) n       ar[i].first = c; n     else { n       cin >> x; n       ar[i] = {c, x}; n     } n   } n   bool check = 1; n   for (long long i = 0; i < n; i++) { n     if (ar[i].first ==  + ) n       vec.push_back(i); n     else { n       if (!vec.size()) { n         check = 0; n         break; n       } n       long long indx = vec.back(); n       vec.pop_back(); n       ar[indx].second = ar[i].second; n     } n   } n   if (!check) return cout <<  NO n , 0; n   for (long long i = 0; i < n; i++) n     if (ar[i].first ==  + ) n       st.insert(ar[i].second); n     else { n       if (!st.size()) { n         check = 0; n         break; n       } n       long long x = (*st.begin()); n       st.erase(x); n       if (x != ar[i].second) { n         check = 0; n         break; n       } n     } n   if (!check) return cout <<  NO n , 0; n   cout <<  YES n ; n   for (long long i = 0; i < n; i++) n     if (ar[i].first ==  + ) cout << ar[i].second <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int up = 100000; n int n, m, s[100050], mx[100050], rp[100050]; n int b[100050], c[100050]; n long long sum[100050]; n pair<long long, long long> operator+(const pair<long long, long long> &a, n                                      const pair<long long, long long> &b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n struct Presidentree { n   bool op; n   int rt[100050], ls[100050 * 20], rs[100050 * 20], node; n   long long val[100050 * 20], sum[100050 * 20]; n   void modify(int &x, int las, int l, int r, int pos, int p) { n     x = ++node; n     ls[x] = ls[las]; n     rs[x] = rs[las]; n     val[x] = val[las] + rp[p]; n     sum[x] = sum[las] + mx[p] - (op ? 0 : s[p]); n     if (l == r) return; n     int mid = (l + r) >> 1; n     if (pos <= mid) n       modify(ls[x], ls[las], l, mid, pos, p); n     else n       modify(rs[x], rs[las], mid + 1, r, pos, p); n   } n   pair<long long, long long> query(int a, int b, int l, int r, int L, int R) { n     if (!a) return make_pair(0, 0); n     if (L <= l && r <= R) return make_pair(val[a] - val[b], sum[a] - sum[b]); n     int mid = (l + r) >> 1; n     pair<long long, long long> ans = make_pair(0, 0); n     if (L <= mid) ans = ans + query(ls[a], ls[b], l, mid, L, R); n     if (R > mid) ans = ans + query(rs[a], rs[b], mid + 1, r, L, R); n     return ans; n   } n } T[2]; n struct Node { n   int l, r, v; n   Node(int _l = 0, int _r = 0, int _v = 0) : l(_l), r(_r), v(_v) {} n   bool operator<(const Node &a) const { return l < a.l; } n }; n set<Node> S; n set<Node>::iterator split(int p) { n   set<Node>::iterator it = S.lower_bound(Node(p)); n   if (it != S.end() && it->l == p) return it; n   it--; n   int l = it->l, r = it->r, v = it->v; n   S.erase(it); n   S.insert(Node(l, p - 1, v)); n   it = S.lower_bound(Node(l, p - 1, v)); n   return S.insert(Node(p, r, v)).first; n } n long long ask(int t, int x, int y) { n   set<Node>::iterator R = split(y + 1), L = split(x); n   long long ans = sum[y] - sum[x - 1]; n   for (set<Node>::iterator it = L; it != R; it++) { n     pair<long long, long long> res; n     if (it->v == 0) n       res = T[0].query(T[0].rt[it->r], T[0].rt[it->l - 1], 0, up + 1, n                        min(up + 1, t - it->v + 1), up + 1); n     else n       res = T[1].query(T[1].rt[it->r], T[1].rt[it->l - 1], 0, up + 1, n                        min(up + 1, t - it->v + 1), up + 1); n     ans += 1ll * res.first * (t - it->v) - res.second; n   } n   S.erase(L, R); n   S.insert(Node(x, y, t)); n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &s[i], &mx[i], &rp[i]); n     sum[i] = sum[i - 1] + (long long)mx[i]; n     if (rp[i] == 0) n       b[i] = c[i] = up + 1; n     else { n       b[i] = (mx[i] - s[i]) / rp[i] + ((mx[i] - s[i]) % rp[i] > 0); n       c[i] = mx[i] / rp[i] + (mx[i] % rp[i] > 0); n     } n   } n   T[0].op = 0; n   T[1].op = 1; n   for (int i = 1; i <= n; i++) n     T[0].modify(T[0].rt[i], T[0].rt[i - 1], 0, up + 1, b[i], i); n   for (int i = 1; i <= n; i++) n     T[1].modify(T[1].rt[i], T[1].rt[i - 1], 0, up + 1, c[i], i); n   S.insert(Node(1, n, 0)); n   S.insert(Node(n + 1, n + 1, 0)); n   scanf( %d , &m); n   while (m--) { n     int t, l, r; n     scanf( %d%d%d , &t, &l, &r); n     cout << ask(t, l, r) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct EDGE { n   int to, next; n } e[400005]; n int head[200005], cnt, c[200005], deg[200005]; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n void addedge(int x, int y) { n   ++cnt; n   e[cnt].to = y; n   e[cnt].next = head[x]; n   head[x] = cnt; n   ++cnt; n   e[cnt].to = x; n   e[cnt].next = head[y]; n   head[y] = cnt; n } n int main() { n   int n = read(); n   cnt = 0; n   for (int i = 1; i < n; i++) { n     int x = read(), y = read(); n     addedge(x, y); n   } n   for (int i = 1; i <= n; i++) c[i] = read(); n   cnt = 0; n   for (int i = 1; i <= n; i++) n     for (int x = head[i]; x; x = e[x].next) n       if (c[i] != c[e[x].to]) { n         deg[i]++; n         deg[e[x].to]++; n         cnt++; n       } n   if (!cnt) n     cout <<  YES n  << 1 << endl; n   else { n     int i = 1; n     for (; i <= n; i++) n       if (deg[i] == cnt) break; n     if (i <= n) n       cout <<  YES n  << i << endl; n     else n       cout <<  NO n  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, n, c = 1; n   cin >> n; n   char s[n + 2]; n   vector<long long> d(n + 1); n   cin >> (s + 1); n   for (i = 1; i <= n; ++i) { n     cin >> d[i]; n   } n   for (i = 1;;) { n     if (s[i] ==  > ) n       i += d[i]; n     else n       i -= d[i]; n     if (i > n || i < 1) break; n     c++; n     if (c > n) break; n   } n   if (c > n) n     cout <<  INFINITE ; n   else n     cout <<  FINITE ; n   return 0; n } n


#include <bits/stdc++.h> n int last[1000100], next[1000100]; n long long val[1000100]; n int cnt[1000100]; n int c[3010]; n int prime[3010], top, pp, tmp; n inline void add(long long s, int &ss) { n   val[++top] = s; n   cnt[top] = ss; n   s %= 1000007; n   next[top] = last[s]; n   last[s] = top; n } n inline int find(long long &x) { n   for (int i = last[x % 1000007]; i; i = next[i]) n     if (x == val[i]) return cnt[i]; n   return -1; n } n int f(int x, int y, int z, int p) { n   if (p == 0) return 1; n   if (x > y) { n     tmp = x; n     x = y; n     y = tmp; n   } n   if (x > z) { n     tmp = x; n     x = z; n     z = tmp; n   } n   if (y > z) { n     tmp = y; n     y = z; n     z = tmp; n   } n   long long s = ((((long long)((x << 11) | y) << 11) | z) << 9) | p; n   if (~find(s)) return find(s); n   int a, b, c, aa, bb, cc, ret = 0; n   for (a = 0, aa = 1; aa <= x; ++a, aa *= prime[p]) n     for (b = 0, bb = 1; bb <= y; ++b, bb *= prime[p]) n       for (c = 0, cc = 1; cc <= z; ++c, cc *= prime[p]) n         ret = ((long long)(a + b + c + 1) * f(x / aa, y / bb, z / cc, p - 1) + n                ret) & n               1073741823; n   add(s, ret); n   return ret; n } n int main() { n   int i, j, x, y, z; n   for (i = 2; i <= 2000; ++i) n     if (c[i] == 0) { n       prime[++top] = i; n       for (j = i * i; j <= 2000; j += i) c[j] = 1; n     } n   pp = top; n   top = 0; n   scanf( %d%d%d , &x, &y, &z); n   while (pp && prime[pp] > x && prime[pp] > y && prime[pp] > z) --pp; n   printf( %d n , f(x, y, z, pp)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   long long open = count(s.begin(), s.end(),  ( ); n   if (open != n - open) { n     cout << -1 <<   n ; n     return 0; n   } n   long long count = 0, ans = 0; n   stack<char> st; n   for (char ch : s) { n     if (ch ==  ( ) { n       if (count == 0) { n         st.push(ch); n       } else { n         count++; n         ans++; n       } n     } else { n       if (st.empty()) { n         count--; n         ans++; n       } else if (st.top() ==  ( ) { n         st.pop(); n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long> v; n   int n; n   cin >> n; n   v.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort(v.rbegin(), v.rend()); n   long long ans = v[0]; n   for (int i = 1; i < n; i++) { n     if (v[i] >= v[i - 1]) v[i] = max(v[i - 1] - 1, 0LL); n     ans += v[i]; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n double PI = 3.141592653589793; n const int P = 1e9 + 9; n class mint { n  public: n   static int init_siz; n   static vector<mint> inverse, factorial; n   static int mintP; n   int n; n   static mint exp(mint a, long long e) { n     mint res = 1; n     while (e) { n       if (e & 1) res *= a; n       a *= a; n       e >>= 1; n     } n     return res; n   } n   static void init(int n) { n     init_siz = n; n     inverse.resize(n + 1); n     factorial.resize(n + 1); n     factorial[0] = 1; n     for (int i = 1; i <= n; i++) factorial[i] = factorial[i - 1] * i; n     inverse[n] = exp(factorial[n], mintP - 2); n     for (int i = n - 1; i >= 0; i--) inverse[i] = inverse[i + 1] * (i + 1); n   } n   static mint inv(mint const& a) { n     return (init_siz >= a.n ? inverse[a.n] * (a.n > 1 ? fac(a - 1) : 1) n                             : exp(a, mintP - 2)); n   } n   static mint cinv(mint const& a) { n     return (init_siz >= a.n ? inverse[a.n] : exp(fac(a), mintP - 2)); n   } n   static mint fac(mint const& a) { n     if (init_siz >= a.n) n       return factorial[a.n]; n     else { n       mint out = 1; n       for (int i = 0; i < a.n; i++) out *= (i + 1); n       return out; n     } n   } n   mint(long long val = 0) { n     n = val % mintP; n     if (n < 0) n += mintP; n   } n   mint& operator+=(mint const& b) { n     n += b.n; n     if (n >= mintP) n -= mintP; n     return *this; n   } n   mint& operator-=(mint const& b) { n     n -= b.n; n     if (n < 0) n += mintP; n     return *this; n   } n   mint& operator*=(mint const& b) { n     n = (long long)n * b.n % mintP; n     return *this; n   } n   mint exp(long long e) { return exp(*this, e); } n   mint inv() { return inv(*this); } n   mint& operator/=(mint const& b) { return *this *= inv(b); } n   friend std::ostream& operator<<(std::ostream& os, mint const& a) { n     return os << (a.n < 0 ? a.n + mintP : a.n); n   } n   friend mint operator+(mint a, mint const b) { return a += b; } n   friend mint operator-(mint a, mint const b) { return a -= b; } n   friend mint operator-(mint const a) { return 0 - a; } n   friend mint operator*(mint a, mint const b) { return a *= b; } n   friend mint operator/(mint a, mint const b) { return a /= b; } n   friend bool operator==(mint const& a, mint const& b) { return a.n == b.n; } n   friend bool operator!=(mint const& a, mint const& b) { return a.n != b.n; } n }; n int mint::init_siz = 0; n int mint::mintP = P; n vector<mint> mint::factorial, mint::inverse; n class dsu { n  private: n   int N; n   vector<int> par; n   int getpar(int v) { n     if (par[v] != v) par[v] = getpar(par[v]); n     return par[v]; n   } n   pair<int, int> getparhei(int v) { n     if (par[v] == v) n       return {v, 0}; n     else { n       auto temp = getparhei(par[v]); n       temp.second++; n       return temp; n     } n   } n  n  public: n   dsu(int siz) { n     N = siz; n     par.resize(N); n     iota(par.begin(), par.end(), 0); n   } n   int id(int v) { return getpar(v); } n   void merge(int v1, int v2) { n     auto par1 = getparhei(v1); n     auto par2 = getparhei(v2); n     if (par1.second < par2.second) swap(par1, par2); n     par[par2.first] = par1.first; n   } n   bool same(int v1, int v2) { return getpar(v1) == getpar(v2); } n }; n void solve() { n   int n, m; n   cin >> n >> m; n   dsu id(n); n   int cnt = 0; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     if (id.id(u) == id.id(v)) n       cnt++; n     else n       id.merge(u, v); n     cout << mint::exp(2, cnt) - 1 <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int T = 1; n   for (int c = 1; c < T + 1; c++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int loc[400000]; n long long sumlen[400000]; n double profit[400000]; n int a[400000]; n int b[400000]; n double hit[200000]; n class MaxSubstringSum { n  public: n   MaxSubstringSum(int n) { n     int real_n = 1; n     while (real_n < n) { n       real_n *= 2; n     } n     lb_ = new int[2 * real_n + 1]; n     ub_ = new int[2 * real_n + 1]; n     max_value_ = new double[2 * real_n + 1]; n     max_left_ = new double[2 * real_n + 1]; n     max_right_ = new double[2 * real_n + 1]; n     sum_all_ = new double[2 * real_n + 1]; n     for (int(i) = 0; (i) < (2 * real_n + 1); ++(i)) { n       lb_[i] = ub_[i] = 0; n       max_value_[i] = max_left_[i] = max_right_[i] = sum_all_[i] = 0.0; n     } n     for (int i = 0; i < real_n; ++i) { n       lb_[real_n + i] = i; n       ub_[real_n + i] = i; n     } n     for (int i = real_n - 1; i >= 1; --i) { n       lb_[i] = lb_[i * 2]; n       ub_[i] = ub_[i * 2 + 1]; n     } n   } n   void Build(int node) { n     if (lb_[node] == ub_[node]) { n       max_value_[node] = max_left_[node] = max_right_[node] = n           max(0.0, profit[lb_[node]]); n       sum_all_[node] = profit[lb_[node]]; n     } else { n       Build(node * 2); n       Build(node * 2 + 1); n       sum_all_[node] = sum_all_[node * 2] + sum_all_[node * 2 + 1]; n       max_left_[node] = max(max_left_[node * 2], n                             sum_all_[node * 2] + max_left_[node * 2 + 1]); n       max_right_[node] = max(max_right_[node * 2 + 1], n                              sum_all_[node * 2 + 1] + max_right_[node * 2]); n       max_value_[node] = n           max(max_value_[node * 2], n               max(max_value_[node * 2 + 1], n                   max_right_[node * 2] + max_left_[node * 2 + 1])); n     } n   } n   double GetLMax(int low, int hi, int node = 1) { n     if (lb_[node] >= low && ub_[node] <= hi) { n       return max_left_[node]; n     } n     if (lb_[node] > hi || ub_[node] < low) return 0.0; n     double ret = GetLMax(low, hi, node * 2); n     if (lb_[node * 2] >= low && ub_[node * 2] <= hi) { n       ret = max((ret), (GetLMax(low, hi, node * 2 + 1) + sum_all_[node * 2])); n     } n     return ret; n   } n   double GetRMax(int low, int hi, int node = 1) { n     if (lb_[node] >= low && ub_[node] <= hi) { n       return max_right_[node]; n     } n     if (lb_[node] > hi || ub_[node] < low) return 0.0; n     double ret = GetRMax(low, hi, node * 2 + 1); n     if (lb_[node * 2 + 1] >= low && ub_[node * 2 + 1] <= hi) { n       ret = max((ret), (GetRMax(low, hi, node * 2) + sum_all_[node * 2 + 1])); n     } n     return ret; n   } n   double GetMax(int low, int hi, int node = 1) { n     if (lb_[node] >= low && ub_[node] <= hi) { n       return max_value_[node]; n     } n     if (lb_[node] > hi || ub_[node] < low) return 0.0; n     double best = max(GetMax(low, hi, node * 2), GetMax(low, hi, node * 2 + 1)); n     best = max((best), n                (GetRMax(low, hi, node * 2) + GetLMax(low, hi, node * 2 + 1))); n     return best; n   } n  n  private: n   int* lb_; n   int* ub_; n   double* max_value_; n   double* max_left_; n   double* max_right_; n   double* sum_all_; n }; n int main() { n   int n, m, c; n   cin >> n >> m >> c; n   for (int(i) = 0; (i) < (n); ++(i)) { n     scanf( %d , loc + i); n   } n   sumlen[0] = 0; n   for (int(i) = (1); (i) < (n); ++(i)) { n     sumlen[i] = sumlen[i - 1] + (long long)loc[i] - (long long)loc[i - 1]; n   } n   for (int(i) = 0; (i) < (n - 1); ++(i)) { n     int x; n     scanf( %d , &x); n     hit[i] = (double)x / 100.0; n   } n   for (int(i) = 0; (i) < (m); ++(i)) { n     scanf( %d%d , a + i, b + i); n   } n   for (int(i) = 0; (i) < (n - 1); ++(i)) { n     profit[i] = n         (double)(loc[i + 1] - loc[i]) / 2.0 - (double)hit[i] * (double)c; n   } n   MaxSubstringSum tree(n); n   tree.Build(1); n   double ret = 0.0; n   for (int(i) = 0; (i) < (m); ++(i)) { n     ret += tree.GetMax(a[i] - 1, b[i] - 2, 1); n   } n   printf( %.10lf n , ret); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void rd(T &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 -  0  + c, c = getchar(); n   x *= f; n } n void FAIL() { n   printf( 0 ); n   exit(0); n } n const int N = 5e5 + 10, mod = 1e9 + 7; n int In[N], Out[N], To[N], vis[N], n; n int sol(vector<int> &G, vector<pair<int, int> > &E) { n   G.push_back(0), G.push_back(n + 1); n   for (int i = 0; i < E.size(); ++i) n     Out[E[i].first]++, In[E[i].second]++, To[E[i].first] = E[i].second; n   for (int i = 0; i < G.size(); ++i) n     if (In[G[i]] > 1 || Out[G[i]] > 1) FAIL(); n   for (int i = 0; i < G.size(); ++i) n     if (!vis[G[i]]) { n       int cur = G[i]; n       vis[cur] = 1; n       while (~To[cur] && !vis[To[cur]]) cur = To[cur], vis[cur] = 1; n       if (To[cur] == G[i]) FAIL(); n     } n   int cur = 0; n   while (~To[cur]) cur = To[cur]; n   if (cur == n + 1 && (int)G.size() - (int)E.size() > 1) FAIL(); n   for (int i = 0; i < G.size(); ++i) n     In[G[i]] = Out[G[i]] = vis[G[i]] = 0, To[G[i]] = -1; n   return max((int)G.size() - (int)E.size() - 2, 0); n } n vector<int> G[N]; n vector<pair<int, int> > E[N]; n int fa[N], dep[N]; n void dfs1(int u, int last) { n   fa[u] = last, dep[u] = dep[last] + 1; n   for (int i = 0; i < G[u].size(); ++i) { n     int v = G[u][i]; n     if (v == last) continue; n     dfs1(v, u); n   } n } n vector<int> P, Q; n void getpath(int x, int y) { n   P.clear(), Q.clear(); n   int flg = 0; n   if (dep[x] < dep[y]) swap(x, y), flg = 1; n   P.push_back(x); n   while (dep[x] > dep[y]) x = fa[x], P.push_back(x); n   if (x == y) { n     if (flg) reverse(P.begin(), P.end()); n     return; n   } n   Q.push_back(y); n   while (fa[x] != fa[y]) x = fa[x], y = fa[y], P.push_back(x), Q.push_back(y); n   P.push_back(fa[x]); n   for (int i = (int)Q.size() - 1; i >= 0; --i) P.push_back(Q[i]); n   if (flg) reverse(P.begin(), P.end()); n } n int tot; n int a[N]; n int fac[N]; n int main() { n   rd(n); n   for (int i = 1, x, y; i < n; ++i) n     rd(x), rd(y), G[x].push_back(y), G[y].push_back(x); n   dfs1(1, 0); n   for (int i = 1; i <= n; ++i) rd(a[i]); n   for (int i = 1; i <= n; ++i) n     if (a[i]) { n       if (a[i] == i) FAIL(); n       getpath(a[i], i); n       if ((tot += (int)P.size() - 1) > 2 * n - 2) FAIL(); n       int m = (int)P.size() - 1; n       E[P[0]].push_back(make_pair(0, P[1])); n       E[P[m]].push_back(make_pair(P[m - 1], n + 1)); n       for (int i = 1; i < m; ++i) n         E[P[i]].push_back(make_pair(P[i - 1], P[i + 1])); n     } n   memset(To, -1, sizeof(To)); n   int ans = 1; n   fac[0] = 1; n   for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * (long long)i % mod; n   for (int i = 1; i <= n; ++i) n     ans = ans * (long long)fac[sol(G[i], E[i])] % mod; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void rot(vector<string>& a) { n   int n = (int)(a).size(); n   int m = (int)(a[0]).size(); n   vector<string> b(m); n   for (int j = 0; j < m; ++j) { n     b[j] =   ; n     for (int i = n - 1; i >= 0; --i) { n       b[j] += a[i][j]; n     } n   } n   a.swap(b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int a, b; n   cin >> a >> b; n   vector<string> q(a); n   for (int i = 0; i < a; ++i) { n     cin >> q[i]; n   } n   int cnt = 0; n   int optx = -1, opty; n   for (int x = 1; x <= a; ++x) { n     for (int y = 1; y <= b; ++y) { n       if (a % x || b % y) { n         continue; n       } n       set<vector<string> > setik; n       bool valid = true; n       for (int i = 0; i < a / x; ++i) { n         for (int j = 0; j < b / y; ++j) { n           vector<string> vct; n           for (int u = i * x; u < (i + 1) * x; ++u) { n             string s =   ; n             for (int v = j * y; v < (j + 1) * y; ++v) { n               s += q[u][v]; n             } n             vct.push_back(s); n           } n           if (setik.count(vct)) { n             valid = false; n           } n           for (int t = 0; t < 3; ++t) { n             rot(vct); n             if (setik.count(vct)) { n               valid = false; n             } n           } n           for (int t = 0; t < 4; ++t) { n             rot(vct); n             setik.insert(vct); n           } n         } n       } n       if (valid) { n         ++cnt; n         if (optx == -1 || optx * opty > x * y || n             (optx * opty == x * y && optx > x)) { n           optx = x, opty = y; n         } n       } n     } n   } n   cout << cnt <<   n ; n   cout << optx <<     << opty <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int onexor = 0; n   bool ok = true; n   long long s, x; n   cin >> s >> x; n   for (int i = 40; i >= 0; i--) { n     if (s & (1LL << i)) { n       if (x & (1LL << i)) { n         continue; n       } else { n         bool foun = false; n         for (int j = i - 1; j >= 0; j--) { n           if (!(x & (1LL << j))) { n             foun = true; n             i = j + 1; n             break; n           } n           if (s & (1LL << (j))) { n             ok = false; n             break; n           } n         } n         if (!foun) { n           ok = false; n           break; n         } n       } n     } else if (x & (1LL << i)) { n       ok = false; n     } n   } n   long long ans = 1LL; n   for (int i = 40; i >= 0; i--) { n     if (x & (1LL << i)) { n       ans = ans * 2LL; n     } n   } n   if (x == s) ans -= 2LL; n   if (ok) { n     cout << ans << endl; n   } else n     cout << 0 << endl; n   cin >> s; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2; n   cin >> s1 >> s2; n   if (s2.size() > s1.size()) swap(s1, s2); n   int x = s1.size() - s2.size(); n   for (int i = 0; i < s1.size() && i < s2.size(); ++i) { n     if (s1[s1.size() - i - 1] != s2[s2.size() - i - 1]) { n       x = s1.size() - i + s2.size() - i; n       break; n     } n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e5; n int n, q; n int all; n int pre[2 * MaxN + 5], last[MaxN + 5], other[2 * MaxN + 5]; n int node[MaxN + 5]; n int father[MaxN + 5]; n int gcd(int x, int y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n void build(int x, int y) { n   pre[++all] = last[x]; n   last[x] = all; n   other[all] = y; n } n void Dfs(int x, int fa) { n   father[x] = fa; n   int ed = last[x], dr; n   while (ed != -1) { n     dr = other[ed]; n     if (dr != fa) { n       Dfs(dr, x); n     } n     ed = pre[ed]; n   } n } n int main() { n   while (scanf( %d %d , &n, &q) != EOF) { n     all = -1; n     memset(last, -1, sizeof(last)); n     for (int i = 1; i <= n; i++) scanf( %d , &node[i]); n     for (int i = 1; i < n; i++) { n       int u, v; n       scanf( %d %d , &u, &v); n       build(u, v); n       build(v, u); n     } n     Dfs(1, 0); n     for (int i = 1; i <= q; i++) { n       int op; n       scanf( %d , &op); n       if (op == 1) { n         int v; n         int ans = -1; n         scanf( %d , &v); n         int now = v; n         while (father[v] != 0) { n           v = father[v]; n           if (gcd(node[v], node[now]) > 1) { n             ans = v; n             break; n           } n         } n         printf( %d n , ans); n       } else { n         int v, w; n         scanf( %d %d , &v, &w); n         node[v] = w; n       } n     } n   } n   return 0; n } n