#include <bits/stdc++.h> n using namespace std; n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n long long int sizes[100001]; n long long int arr[100001]; n bool ok = true; n long long int A[100001]; n pair<long long int, long long int> B[100001]; n long long int root(long long int i) { n   while (i != arr[i]) i = arr[i]; n   return (i); n } n void Union(long long int i, long long int j) { n   long long int root_i = root(i), root_j = root(j); n   if (root_i == root_j) return; n   if (sizes[root_i] > sizes[root_j]) swap(root_i, root_j); n   sizes[root_j] += sizes[root_i]; n   arr[root_i] = arr[root_j]; n   return; n } n long long int Arr[100001]; n bool solve(long long int n) { n   if (n == 1) return (true); n   set<long long int> st; n   for (long long int i = 0; i < n; i++) { n     long long int m = (Arr[i] + i) % n; n     if (m < 0) m += n; n     if (st.count(m)) return (false); n     st.insert(m); n   } n   return (true); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     set<pair<pair<long long int, long long int>, n              pair<long long int, long long int> > > n         st; n     long long int ans = 0, x = 0, y = 0; n     for (long long int i = 0; i < s.size(); i++) { n       long long int a, b, c, d; n       if (s[i] ==  N ) { n         a = x; n         b = y + 1; n         c = 0; n         d = 1; n       } else if (s[i] ==  S ) { n         a = x; n         b = y - 1; n         c = 0; n         d = -1; n       } else if (s[i] ==  W ) { n         a = x + 1; n         b = y; n         c = 1; n         d = 0; n       } else if (s[i] ==  E ) { n         a = x - 1; n         b = y; n         c = -1; n         d = 0; n       } n       if (st.count(pair<pair<long long int, long long int>, n                         pair<long long int, long long int> >( n               pair<long long int, long long int>(x, y), n               pair<long long int, long long int>(a, b)))) n         ans++; n       else if (st.count(pair<pair<long long int, long long int>, n                              pair<long long int, long long int> >( n                    pair<long long int, long long int>(a, b), n                    pair<long long int, long long int>(x, y)))) n         ans++; n       else n         ans += 5; n       st.insert(pair<pair<long long int, long long int>, n                      pair<long long int, long long int> >( n           pair<long long int, long long int>(x, y), n           pair<long long int, long long int>(a, b))); n       x += c; n       y += d; n     } n     cout << ans <<   n ; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll mod = 1e9L + 7; n const int N = 121, M = 1211; n int x[N], y[N]; n ll dp[N][M]; n ll c[M][M]; n int main() { n   for (int i = 0; i < M; i++) { n     c[i][0] = 1; n     for (int j = 1; j <= i; j++) n       c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; n   } n   ios ::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   for (int i = 1; i <= n; i++) cin >> y[i]; n   dp[0][0] = 1; n   for (int i = 0; i <= n; i++) n     for (int j = 0; j < M; j++) n       for (int k = 0; k <= y[i + 1] && j + x[i + 1] - k >= 0; k++) { n         dp[i + 1][j + x[i + 1] - k] += dp[i][j] * c[j + x[i + 1]][k]; n         dp[i + 1][j + x[i + 1] - k] %= mod; n       } n   ll ans = dp[n + 1][0]; n   int m = accumulate(x, x + n + 1, 0); n   for (int i = 1; i <= n; i++) { n     ans *= c[m][x[i]]; n     ans %= mod; n     m -= x[i]; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops ) n #pragma GCC optimize( no-stack-protector,fast-math ) n using namespace std; n const int N = 5e5 + 100, OO = 724249387, T = 4500 + 100, M = 1e9 + 7, P = 6151, n           SQ = 280, lg = 30; n map<int, int> mp; n int dp[N], l[N], r[N], t[N], seg[4][N << 2], lazy[4][N << 2]; n vector<pair<int, int> > v[N]; n void shift(int c, int id) { n   seg[id][c << 1] += lazy[id][c]; n   seg[id][c << 1 | 1] += lazy[id][c]; n   lazy[id][c << 1] += lazy[id][c]; n   lazy[id][c << 1 | 1] += lazy[id][c]; n   lazy[id][c] = 0; n } n void upd(int id, int c, int b, int e, int l, int r, int t) { n   if (e <= l || r <= b) return; n   if (l <= b && e <= r) { n     seg[id][c] += t; n     lazy[id][c] += t; n     return; n   } n   int mid = (b + e) >> 1; n   shift(c, id); n   upd(id, c << 1, b, mid, l, r, t), upd(id, c << 1 | 1, mid, e, l, r, t); n   seg[id][c] = max(seg[id][c << 1 | 1], seg[id][c << 1]); n } n int get(int id, int c, int b, int e, int l, int r) { n   if (r <= b || e <= l) return 0; n   if (l <= b && e <= r) return seg[id][c]; n   int mid = (b + e) >> 1; n   shift(c, id); n   return max(get(id, c << 1, b, mid, l, r), get(id, c << 1 | 1, mid, e, l, r)); n } n int32_t main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   set<int> st; n   st.insert(0); n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> r[i] >> t[i]; n     st.insert(l[i]), st.insert(r[i]); n   } n   int cnt = 0; n   for (auto u : st) mp[u] = cnt++; n   for (int i = 0; i < n; i++) { n     l[i] = mp[l[i]], r[i] = mp[r[i]]; n     v[r[i]].push_back({l[i], t[i]}); n   } n   for (int i = 1; i < cnt; i++) { n     for (auto u : v[i]) upd(u.second, 1, 0, cnt, 0, u.first, +1); n     dp[i] = max(get(1, 1, 0, cnt, 0, i), get(2, 1, 0, cnt, 0, i)); n     upd(1, 1, 0, cnt, i, i + 1, dp[i]), upd(2, 1, 0, cnt, i, i + 1, dp[i]); n   } n   cout << dp[cnt - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXL = 1e5; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int k, p; n   long long int ans = 0; n   cin >> k >> p; n   for (int i = 1; i <= k; i++) { n     int x = i; n     string str1, str2; n     while (x) { n       str1 += (x % 10 +  0 ); n       x /= 10; n     } n     for (int j = str1.size() - 1; j >= 0; j--) str2 += str1[j]; n     str2 += str1; n     long long int sum = 0; n     for (int j = 0; j < str2.size(); j++) sum = sum * 10 + str2[j] -  0 ; n     ans = (ans + sum) % p; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n std::mt19937 rnd( n     (int)std::chrono::steady_clock::now().time_since_epoch().count()); n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXH = 100000; n const int MAXW = 100000; n const int MAXDIM = MAXH >= MAXW ? MAXH : MAXW; n const int MAXQ = 100000; n const int MAXS = 4 * MAXDIM; n int h, w, nq; n char initlft[MAXH + 1]; n char initrgt[MAXH + 1]; n char inittop[MAXW + 1]; n char initbot[MAXW + 1]; n char qside[MAXQ]; n int ql[MAXQ], qr[MAXQ], qans[MAXQ + 1]; n struct SegNode { n   int cost[2][2][2][2]; n   bool lazyfliptop, lazyflipbot; n   int cntblue[2], cntred[2]; n }; n struct SegTree { n   int n; n   int crosscost, lftcost[2], rgtcost[2]; n   char inittop[MAXDIM + 1]; n   char initbot[MAXDIM + 1]; n   SegNode sval[MAXS]; n   void sfliptop(int x) { n     swap(sval[x].cntblue[0], sval[x].cntred[0]); n     for (int lftcol = (0); lftcol < (2); ++lftcol) n       for (int rgtcol = (0); rgtcol < (2); ++rgtcol) n         for (int botflip = (0); botflip < (2); ++botflip) n           swap(sval[x].cost[lftcol][rgtcol][0][botflip], n                sval[x].cost[lftcol][rgtcol][1][botflip]); n     sval[x].lazyfliptop = !sval[x].lazyfliptop; n   } n   void sflipbot(int x) { n     swap(sval[x].cntblue[1], sval[x].cntred[1]); n     for (int lftcol = (0); lftcol < (2); ++lftcol) n       for (int rgtcol = (0); rgtcol < (2); ++rgtcol) n         for (int topflip = (0); topflip < (2); ++topflip) n           swap(sval[x].cost[lftcol][rgtcol][topflip][0], n                sval[x].cost[lftcol][rgtcol][topflip][1]); n     sval[x].lazyflipbot = !sval[x].lazyflipbot; n   } n   void spush(int x) { n     if (sval[x].lazyfliptop) n       sfliptop(2 * x + 1), sfliptop(2 * x + 2), sval[x].lazyfliptop = false; n     if (sval[x].lazyflipbot) n       sflipbot(2 * x + 1), sflipbot(2 * x + 2), sval[x].lazyflipbot = false; n   } n   void spull(int x) { n     const SegNode &a = sval[2 * x + 1]; n     const SegNode &b = sval[2 * x + 2]; n     for (int side = (0); side < (2); ++side) n       sval[x].cntblue[side] = a.cntblue[side] + b.cntblue[side]; n     for (int side = (0); side < (2); ++side) n       sval[x].cntred[side] = a.cntred[side] + b.cntred[side]; n     for (int lftcol = (0); lftcol < (2); ++lftcol) n       for (int rgtcol = (0); rgtcol < (2); ++rgtcol) n         for (int topflip = (0); topflip < (2); ++topflip) n           for (int botflip = (0); botflip < (2); ++botflip) { n             int cur = INT_MAX; n             for (int midcol1 = (0); midcol1 < (2); ++midcol1) n               for (int midcol2 = (0); midcol2 < (2); ++midcol2) { n                 int acost = a.cost[lftcol][midcol1][topflip][botflip]; n                 int bcost = b.cost[midcol2][rgtcol][topflip][botflip]; n                 int ccost = midcol1 == midcol2 ? 0 : crosscost; n                 if (acost == INT_MAX || bcost == INT_MAX) continue; n                 cur = min(cur, acost + bcost + ccost); n               } n             sval[x].cost[lftcol][rgtcol][topflip][botflip] = cur; n           } n   } n   void sinit(int x, int l, int r) { n     sval[x].lazyfliptop = sval[x].lazyflipbot = false; n     if (l == r) { n       int sa = inittop[l] ==  B  ? 0 : 1; n       int sb = initbot[l] ==  B  ? 0 : 1; n       sval[x].cntblue[0] = sa == 0 ? 1 : 0, n       sval[x].cntred[0] = 1 - sval[x].cntblue[0]; n       sval[x].cntblue[1] = sb == 0 ? 1 : 0, n       sval[x].cntred[1] = 1 - sval[x].cntblue[1]; n       for (int lftcol = (0); lftcol < (2); ++lftcol) n         for (int rgtcol = (0); rgtcol < (2); ++rgtcol) n           for (int topflip = (0); topflip < (2); ++topflip) n             for (int botflip = (0); botflip < (2); ++botflip) { n               int cur; n               if (lftcol == rgtcol) { n                 int a = sa; n                 if (topflip) a = 1 - a; n                 int b = sb; n                 if (botflip) b = 1 - b; n                 cur = (a == lftcol ? 0 : 1) + (b == lftcol ? 0 : 1); n               } else { n                 cur = INT_MAX; n               } n               sval[x].cost[lftcol][rgtcol][topflip][botflip] = cur; n             } n     } else { n       int m = l + (r - l) / 2; n       sinit(2 * x + 1, l, m); n       sinit(2 * x + 2, m + 1, r); n       spull(x); n     } n   } n   void sflip(int x, int l, int r, int L, int R, bool istop) { n     if (L <= l && r <= R) { n       if (istop) n         sfliptop(x); n       else n         sflipbot(x); n     } else { n       int m = l + (r - l) / 2; n       spush(x); n       if (L <= m) sflip(2 * x + 1, l, m, L, R, istop); n       if (m + 1 <= R) sflip(2 * x + 2, m + 1, r, L, R, istop); n       spull(x); n     } n   } n   void init() { sinit(0, 0, n - 1); } n   void flip(int l, int r, bool istop) { sflip(0, 0, n - 1, l, r, istop); } n   int calc() { n     int ret = INT_MAX; n     for (int lftcol = (0); lftcol < (2); ++lftcol) n       for (int rgtcol = (0); rgtcol < (2); ++rgtcol) { n         int cur = sval[0].cost[lftcol][rgtcol][0][0]; n         if (cur == INT_MAX) continue; n         cur += lftcost[lftcol]; n         cur += rgtcost[rgtcol]; n         ret = min(ret, cur); n       } n     return ret; n   } n }; n SegTree stopbot, slftrgt; n int calc() { n   stopbot.lftcost[0] = slftrgt.sval[0].cntred[0], n   stopbot.lftcost[1] = slftrgt.sval[0].cntblue[0]; n   stopbot.rgtcost[0] = slftrgt.sval[0].cntred[1], n   stopbot.rgtcost[1] = slftrgt.sval[0].cntblue[1]; n   slftrgt.lftcost[0] = stopbot.sval[0].cntred[0], n   slftrgt.lftcost[1] = stopbot.sval[0].cntblue[0]; n   slftrgt.rgtcost[0] = stopbot.sval[0].cntred[1], n   slftrgt.rgtcost[1] = stopbot.sval[0].cntblue[1]; n   return min(stopbot.calc(), slftrgt.calc()); n } n void solve() { n   stopbot.n = w, stopbot.crosscost = h; n   for (int i = (0); i <= (w); ++i) n     stopbot.inittop[i] = inittop[i], stopbot.initbot[i] = initbot[i]; n   stopbot.init(); n   slftrgt.n = h, slftrgt.crosscost = w; n   for (int i = (0); i <= (h); ++i) n     slftrgt.inittop[i] = initlft[i], slftrgt.initbot[i] = initrgt[i]; n   slftrgt.init(); n   qans[0] = calc(); n   for (int i = (0); i < (nq); ++i) { n     char side = qside[i]; n     int l = ql[i], r = qr[i]; n     if (side ==  U ) stopbot.flip(l, r, true); n     if (side ==  D ) stopbot.flip(l, r, false); n     if (side ==  L ) slftrgt.flip(l, r, true); n     if (side ==  R ) slftrgt.flip(l, r, false); n     qans[i + 1] = calc(); n   } n } n void run() { n   scanf( %d%d%d , &h, &w, &nq); n   scanf( %s , initlft); n   scanf( %s , initrgt); n   scanf( %s , inittop); n   scanf( %s , initbot); n   for (int i = (0); i < (nq); ++i) n     scanf(  %c%d%d , &qside[i], &ql[i], &qr[i]), --ql[i], --qr[i]; n   solve(); n   for (int i = (0); i <= (nq); ++i) printf( %d n , qans[i]); n } n void stress() { n   h = MAXH, w = MAXW, nq = MAXQ; n   for (int i = (0); i < (h); ++i) initlft[i] =  BR [rnd() % 2]; n   initlft[h] =   0 ; n   for (int i = (0); i < (h); ++i) initrgt[i] =  BR [rnd() % 2]; n   initrgt[h] =   0 ; n   for (int i = (0); i < (w); ++i) inittop[i] =  BR [rnd() % 2]; n   inittop[w] =   0 ; n   for (int i = (0); i < (w); ++i) initbot[i] =  BR [rnd() % 2]; n   initbot[w] =   0 ; n   for (int i = (0); i < (nq); ++i) { n     qside[i] =  UDLR [rnd() % 4]; n     int dim = qside[i] ==  U  || qside[i] ==  D  ? w : h; n     ql[i] = rnd() % dim, qr[i] = rnd() % dim; n     if (ql[i] > qr[i]) swap(ql[i], qr[i]); n   } n   solve(); n   printf( done n ); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << n / 2 * 3 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n struct _hash { n   int operator()(const complex<double> &p) const { n     return p.real() * 20001 + p.imag(); n   } n }; n int n; n complex<double> p[100]; n unordered_set<complex<double>, _hash> pnts; n double ans = -1; n inline int compare(const double &a, const double &b) { n   if (fabs(a - b) < eps) return 0; n   return a < b ? -1 : 1; n } n bool intersect(const complex<double> &a, const complex<double> &b, n                const complex<double> &p, const complex<double> &q, n                complex<double> &ret) { n   double d1 = ((conj(p - a) * (b - a)).imag()); n   double d2 = ((conj(q - a) * (b - a)).imag()); n   ret = (d1 * q - d2 * p) / (d1 - d2); n   return (fabs(d1 - d2) > eps); n } n complex<double> findCenter(const complex<double> &a, const complex<double> &b, n                            const complex<double> &c) { n   complex<double> m1 = (((a) + (b)) / 2.0); n   complex<double> m2 = (((b) + (c)) / 2.0); n   complex<double> p1 = n       (complex<double>(-(((b) - (a))).imag(), (((b) - (a))).real())); n   complex<double> p2 = n       (complex<double>(-(((c) - (b))).imag(), (((c) - (b))).real())); n   complex<double> r; n   assert(intersect(m1, m1 + p1, m2, m2 + p2, r)); n   return r; n } n void check(const complex<double> &center, const double &r) { n   double minDist = 1e9; n   for (int i = 0; i < n; ++i) n     minDist = min(minDist, (hypot((((p[i]) - (center))).real(), n                                   (((p[i]) - (center))).imag()))); n   if (compare(minDist, r) < 0) return; n   assert(compare(r, minDist) == 0); n   ans = max(ans, r); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d %d , &x, &y); n     pnts.insert(p[i] = complex<double>(x, y)); n   } n   for (int i = 0; i < n; ++i) n     for (int j = i + 1; j < n; ++j) n       for (int k = j + 1; k < n; ++k) { n         complex<double> triangle[3] = {p[i], p[j], p[k]}; n         for (int a = 0; a < 3; ++a) { n           int b = (a + 1) % 3; n           int c = (b + 1) % 3; n           double d = ((conj(((triangle[b]) - (triangle[a]))) * n                        (((triangle[c]) - (triangle[a])))) n                           .real()); n           if (d < 0) goto next; n           if (d == 0) { n             if (!compare((hypot((((triangle[b]) - (triangle[a]))).real(), n                                 (((triangle[b]) - (triangle[a]))).imag())), n                          (hypot((((triangle[c]) - (triangle[a]))).real(), n                                 (((triangle[c]) - (triangle[a]))).imag())))) { n               { n                 complex<double> p = ((triangle[a]) - (triangle[b])); n                 complex<double> m = ((triangle[c]) - (triangle[b])); n                 complex<double> _4th = conj(p / m) * m; n                 if (pnts.find(_4th) == pnts.end()) goto next; n                 { n                   complex<double> center = n                       (((triangle[b]) + (triangle[c])) / 2.0); n                   check(center, (hypot((((triangle[0]) - (center))).real(), n                                        (((triangle[0]) - (center))).imag()))); n                 } n               } n             } n             goto next; n           } n         } n         { n           complex<double> center = n               findCenter(triangle[0], triangle[1], triangle[2]); n           check(center, (hypot((((triangle[0]) - (center))).real(), n                                (((triangle[0]) - (center))).imag()))); n         } n       next:; n       } n   printf( %.17lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 400; n int x[MAXN], y[MAXN]; n inline bool lt(int i, int xx, int yy) { n   return x[i] < xx || (x[i] == xx && y[i] < yy); n } n inline bool gt(int i, int xx, int yy) { n   return x[i] > xx || (x[i] == xx && y[i] > yy); n } n void mysort(int l, int r) { n   if (l < r) { n     int pivot = int((rand() / (1.0 + RAND_MAX)) * (r - l + 1)) + l; n     int xx = x[pivot]; n     int yy = y[pivot]; n     int i = l; n     int j = r; n     while (1) { n       while (lt(i, xx, yy)) ++i; n       while (gt(j, xx, yy)) --j; n       if (i <= j) { n         swap(x[i], x[j]); n         swap(y[i], y[j]); n         ++i; n         --j; n       } else { n         break; n       } n     } n     mysort(l, j); n     mysort(i, r); n   } n } n bool outside(int xx, int xlo, int xhi) { return xx < xlo || xx > xhi; } n bool outside(int xx, int yy, int xlo, int xhi, int ylo, int yhi) { n   return outside(xx, xlo, xhi) || outside(yy, ylo, yhi); n } n const int MAXSTEPS = 1000000; n char steps[MAXSTEPS + 1]; n int dy[] = {1, 0, -1, 0}; n int dx[] = {0, 1, 0, -1}; n string stepcode =  URDL ; n const int OFFSET = 120; n bool board[2 * OFFSET + 1][2 * OFFSET + 1]; n int from[2 * OFFSET + 1][2 * OFFSET + 1]; n int n; n bool tree(int xx, int yy) { n   pair<int*, int*> p_ = equal_range(x, x + n, xx); n   int p = p_.first - x; n   int q = p_.second - x; n   cerr <<  tree:   << xx <<     << yy <<     << p <<     << q <<   n ; n   if (p < n && x[p] == xx) { n     int i = lower_bound(y + p, y + q, yy) - y; n     cerr << i <<   n ; n     return (i < q && y[i] == yy); n   } n   return false; n } n int main() { n   int vx, vy, sx, sy; n   cin >> vx >> vy >> sx >> sy >> n; n   if (n == 0) { n     puts( -1 ); n     return 0; n   } n   int orig_vx = vx; n   int orig_vy = vy; n   int orig_sx = sx; n   int orig_sy = sy; n   for (int i = 0; i < n; ++i) { n     cin >> x[i] >> y[i]; n   } n   mysort(0, n - 1); n   for (int i = 0; i < n; ++i) { n     cerr << x[i] <<     << y[i] <<   n ; n   } n   cerr <<   n ; n   int xlo = x[0]; n   int xhi = x[n - 1]; n   int ylo = *min_element(y, y + n); n   int yhi = *max_element(y, y + n); n   int step = 0; n   if (!outside(vx, vy, xlo, xhi, ylo, yhi) || n       !outside(sx, sy, xlo, xhi, ylo, yhi)) { n     for (int i = 0; i < n; ++i) { n       (board[(x[i]) + OFFSET][(y[i]) + OFFSET]) = 1; n     } n     memset(from, 0xff, sizeof from); n     queue<pair<int, int> > Q; n     Q.push(make_pair(vx, vy)); n     (from[(vx) + OFFSET][(vy) + OFFSET]) = 1; n     while (!Q.empty()) { n       pair<int, int> tmp = Q.front(); n       Q.pop(); n       int u = tmp.first; n       int v = tmp.second; n       for (int d = 0; d < 4; ++d) { n         int r = u + dx[d]; n         int c = v + dy[d]; n         if (r + OFFSET < 0 || c + OFFSET < 0 || r > OFFSET || c > OFFSET || n             (from[(r) + OFFSET][(c) + OFFSET]) != -1 || n             (board[(r) + OFFSET][(c) + OFFSET])) { n           continue; n         } n         (from[(r) + OFFSET][(c) + OFFSET]) = d; n         if (r == sx && c == sy) { n           goto BFS_DONE; n         } n         Q.push(make_pair(r, c)); n       } n     } n   BFS_DONE:; n     if ((from[(sx) + OFFSET][(sy) + OFFSET]) == -1) { n       puts( -1 ); n       return 0; n     } n     vector<int> D; n     int xx = sx; n     int yy = sy; n     while (xx != vx || yy != vy) { n       D.push_back((from[(xx) + OFFSET][(yy) + OFFSET])); n       xx -= dx[D.back()]; n       yy -= dy[D.back()]; n     } n     reverse(D.begin(), D.end()); n     cerr << D.size() <<   n ; n     int at = 0; n     while ((!outside(vx, vy, xlo, xhi, ylo, yhi) || n             !outside(sx, sy, xlo, xhi, ylo, yhi)) && n            (vx != sx || vy != sy)) { n       assert(at < int(D.size())); n       cerr << at <<     << D[at] <<   n ; n       steps[step++] = stepcode[D[at]]; n       cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n       vx += dx[D[at]]; n       vy += dy[D[at]]; n       cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n       assert(!tree(vx, vy)); n       if (!tree(sx + dx[D[at]], sy + dy[D[at]])) { n         sx += dx[D[at]]; n         sy += dy[D[at]]; n         D.push_back(D[at]); n       } n       cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n       cerr <<   n ; n       ++at; n     } n   } n   if (vx != sx || vy != sy) { n     int d = -1; n     if ((vx > xhi || outside(vy, ylo, yhi)) && n         (sx > xhi || outside(sy, ylo, yhi))) { n       d = 1; n     } else if ((vx < xlo || outside(vy, ylo, yhi)) && n                (sx < xlo || outside(sy, ylo, yhi))) { n       d = 3; n     } else if ((vy > yhi || outside(vx, xlo, xhi)) && n                (sy > yhi || outside(sx, xlo, xhi))) { n       d = 0; n     } else { n       assert((vy < ylo || outside(vx, xlo, xhi)) && n              (vy < ylo || outside(sx, xlo, xhi))); n       d = 2; n     } n     vx += 500 * dx[d]; n     vy += 500 * dy[d]; n     sx += 500 * dx[d]; n     sy += 500 * dy[d]; n     for (int i = 0; i < 500; ++i) { n       steps[step++] = stepcode[d]; n     } n     cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n     if (d != 0) { n       if (d == 2) { n         for (int i = 0; i < 500; ++i) { n           steps[step++] =  R ; n         } n       } n       vy += 1000; n       sy += 1000; n       for (int i = 0; i < 1000; ++i) { n         steps[step++] =  U ; n       } n       cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n       if (d == 2) { n         for (int i = 0; i < 500; ++i) { n           steps[step++] =  L ; n         } n       } else { n         d = (d + 2) % 4; n         cerr <<  bla   << d <<   n ; n         cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n         vx += 500 * dx[d]; n         vy += 500 * dy[d]; n         sx += 500 * dx[d]; n         sy += 500 * dy[d]; n         for (int i = 0; i < 500; ++i) { n           steps[step++] = stepcode[d]; n         } n         cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n       } n     } n   } n   cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n   if (vx != sx) { n     if (vx > sx) { n       vx += 500; n       sx += 500; n       for (int i = 0; i < 500; ++i) { n         steps[step++] =  R ; n       } n       int k = sy - y[n - 1]; n       vy -= k; n       sy -= k; n       for (int i = 0; i < k; ++i) { n         steps[step++] =  D ; n       } n       while (vx != sx) { n         --vx; n         steps[step++] =  L ; n         if (sx - 1 > x[n - 1]) { n           --sx; n         } n       } n     } else { n       vx -= 500; n       sx -= 500; n       for (int i = 0; i < 500; ++i) { n         steps[step++] =  L ; n       } n       int k = sy - y[0]; n       vy -= k; n       sy -= k; n       for (int i = 0; i < k; ++i) { n         steps[step++] =  D ; n       } n       while (vx != sx) { n         ++vx; n         steps[step++] =  R ; n         if (sx + 1 < x[0]) { n           ++sx; n         } n       } n     } n   } n   cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n   assert(vx == sx); n   if (vy != sy) { n     if (vy > sy) { n       vy += 2000; n       sy += 2000; n       for (int i = 0; i < 2000; ++i) { n         steps[step++] =  U ; n       } n       int top = 0; n       for (int i = 1; i < n; ++i) { n         if (y[i] > y[top]) { n           top = i; n         } n       } n       if (vx > x[top]) { n         int k = vx - x[top]; n         vx -= k; n         sx -= k; n         for (int i = 0; i < k; ++i) { n           steps[step++] =  L ; n         } n       } else { n         int k = -(vx - x[top]); n         vx += k; n         sx += k; n         for (int i = 0; i < k; ++i) { n           steps[step++] =  R ; n         } n       } n       while (vy != sy) { n         --vy; n         steps[step++] =  D ; n         if (sy - 1 > y[top]) { n           --sy; n         } n       } n     } else { n       vy -= 2000; n       sy -= 2000; n       for (int i = 0; i < 2000; ++i) { n         steps[step++] =  D ; n       } n       int bot = 0; n       for (int i = 1; i < n; ++i) { n         if (y[i] < y[bot]) { n           bot = i; n         } n       } n       if (vx > x[bot]) { n         int k = vx - x[bot]; n         vx -= k; n         sx -= k; n         for (int i = 0; i < k; ++i) { n           steps[step++] =  L ; n         } n       } else { n         int k = -(vx - x[bot]); n         vx += k; n         sx += k; n         for (int i = 0; i < k; ++i) { n           steps[step++] =  R ; n         } n       } n       while (vy != sy) { n         ++vy; n         steps[step++] =  U ; n         if (sy + 1 < y[bot]) { n           ++sy; n         } n       } n     } n   } n   cerr << vx <<     << vy <<     << sx <<     << sy <<   n ; n   int xx = orig_vx; n   int yy = orig_vy; n   for (int s = 0; s < step; ++s) { n     int d = stepcode.find(steps[s]); n     xx += dx[d]; n     yy += dy[d]; n   } n   cerr << xx <<     << yy <<   expect   << vx <<     << vy; n   assert(xx == vx && yy == vy); n   xx = orig_sx; n   yy = orig_sy; n   for (int s = 0; s < step; ++s) { n     int d = stepcode.find(steps[s]); n     xx += dx[d]; n     yy += dy[d]; n     if (tree(xx, yy)) { n       xx -= dx[d]; n       yy -= dy[d]; n     } n   } n   cerr << xx <<     << yy <<   expect   << sx <<     << sy; n   assert(xx == sx && yy == sy); n   puts(steps); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, cnt = 0; n   float y; n   scanf( %d , &n); n   int p[n]; n   for (i = 1; i <= n; i++) { n     scanf( %d , &p[i]); n     cnt += p[i]; n   } n   y = (float)cnt / (float)n; n   printf( %f , y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   string res =   ; n   if (s.find( . ) == string::npos) { n     s +=  . ; n   } n   int d = s.find( . ); n   int i = d - 1; n   int c = 0; n   while (i >= 0) { n     if (s[i] !=  - ) res = s[i] + res; n     c++; n     if (c == 3 && i > 0) { n       if (i - 1 >= 0 && s[i - 1] !=  - ) res =  ,  + res; n       c = 0; n     } n     i--; n   } n   res +=  . ; n   int dc = s.length() - 1 - d; n   if (dc == 0) { n     res +=  00 ; n   } else if (dc == 1) { n     res += s[d + 1]; n     res +=  0 ; n   } else if (dc >= 2) { n     res += s[d + 1]; n     res += s[d + 2]; n   } n   res =  $  + res; n   if (s[0] ==  - ) res =  (  + res +  ) ; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[505][505], dp2[505]; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (auto &i : a) cin >> i; n   memset(dp, 0, sizeof(dp)); n   for (int i = 0; i < n; i++) dp[i][i] = a[i]; n   for (int i = 1; i < n; i++) { n     for (int j = i - 1; j >= 0; j--) { n       for (int k = i; k > j; k--) { n         if (dp[k][i] and dp[k][i] == dp[j][k - 1]) dp[j][i] = dp[k][i] + 1; n       } n     } n   } n   memset(dp2, INT_MAX, sizeof(dp2)); n   for (int i = 0; i <= n; i++) dp2[i] = i + 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= i; j++) { n       if (dp[j][i]) { n         if (j == 0) n           dp2[i] = 1; n         else n           dp2[i] = min(dp2[i], dp2[j - 1] + 1); n       } n     } n   } n   cout << dp2[n - 1]; n } n int main() { n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e18; n const int mod = (int)1e9 + 7; n const int MAXN = (int)1e5 + 5; n long long n; n const int bs = 350; n int a[MAXN]; n int mp[600][MAXN]; n vector<int> ls[1000]; n void swp(int l, int r) { n   int size = 0, i = 0; n   vector<int> tmp1, tmp2; n   for (i = 0; i <= (n - 1) / bs; i++) { n     if (size + ls[i].size() > r) { n       break; n     } n     size += ls[i].size(); n   } n   long long sayi = 0; n   for (int j = 0; j < ls[i].size(); j++) { n     mp[i][ls[i][j]] = 0; n     if (j + size == r) { n       sayi = ls[i][j]; n       continue; n     } n     tmp1.push_back(ls[i][j]); n   } n   ls[i].clear(); n   for (int j : tmp1) { n     ls[i].push_back(j); n     mp[i][j]++; n   } n   size = 0; n   i = 0; n   for (i = 0; i <= (n - 1) / bs; i++) { n     if (size + ls[i].size() > l) { n       break; n     } n     size += ls[i].size(); n   } n   if (l == n - 1) { n     ls[(l / bs)].push_back(sayi); n     mp[l / bs][sayi]++; n     return; n   } n   for (int j = 0; j < ls[i].size(); j++) { n     mp[i][ls[i][j]] = 0; n     if (j + size == l) { n       tmp2.push_back(sayi); n     } n     tmp2.push_back(ls[i][j]); n   } n   ls[i].clear(); n   for (int j : tmp2) { n     ls[i].push_back(j); n     mp[i][j]++; n   } n } n long long get(long long ind, long long k) { n   if (ind < 0) return 0; n   long long ans = 0; n   int size = 0; n   int yer; n   for (int i = 0; i <= (n - 1) / bs; i++) { n     if (size + ls[i].size() > ind) { n       yer = i; n       break; n     } n     size += ls[i].size(); n     ans += mp[i][k]; n   } n   for (int i = 0; i + size <= ind; i++) { n     ans += (ls[yer][i] == k); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     ls[i / bs].push_back(a[i]); n     mp[i / bs][a[i]]++; n   } n   int lastans = 0; n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     if (i % bs == 0) { n       vector<int> tmp; n       for (int i = 0; i <= (n - 1) / bs; i++) { n         for (auto j = ls[i].begin(); j < ls[i].end(); j++) { n           tmp.push_back(*j); n           mp[i][*j] = 0; n         } n       } n       for (int j = 0; j < n; j++) { n         if (j % bs == 0) { n           ls[j / bs].clear(); n         } n         mp[j / bs][tmp[j]]++; n         ls[j / bs].push_back(tmp[j]); n       } n     } n     int a; n     cin >> a; n     if (a == 1) { n       int l, r; n       cin >> l >> r; n       l = (l + lastans - 1 + n) % n; n       r = (r + lastans - 1 + n) % n; n       if (l > r) swap(l, r); n       swp(l, r); n     } else { n       int l, r, k; n       cin >> l >> r >> k; n       l = (l + lastans - 1 + n) % n; n       r = (r + lastans - 1 + n) % n; n       k = 1 + (k + lastans - 1 + n) % n; n       if (l > r) swap(l, r); n       lastans = get(r, k) - get(l - 1, k); n       cout << lastans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string name; n   int counter = 0; n   cin >> name; n   for (int i = 0; i < name.length() / 2; i++) { n     if (name[i] != name[name.length() - 1 - i]) counter++; n   } n   if (name.length() % 2 == 0) { n     if (counter == 1) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } else { n     if (counter == 1 || counter == 0) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long b, n                  long long mod = (long long)(1e9 + 7)) { n   if (!b) return 1; n   a %= mod; n   return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int mxn = 1100; n char string1[mxn]; n char string2[mxn]; n int xt1[mxn], xt2[mxn], xt3[mxn]; n int bound[mxn]; n int n, m; n void solution() { n   long long answer = 0; n   scanf( %d , &n); n   scanf( %d , &m); n   for (int i = 0; i < n; i++) { n     scanf( %s , string2); n     long long composite = 0; n     for (int j = 0; j < m; j++) { n       if (string2[j] != string1[j]) { n         xt1[j] = xt2[j]; n         xt2[j] = xt3[j]; n         xt3[j] = 1; n       } else { n         xt3[j]++; n       } n       if (j > 0) { n         if (string2[j] == string2[j - 1]) n           bound[j]++; n         else n           bound[j] = 0; n       } n       if (xt1[j] >= xt2[j] && xt2[j] == xt3[j]) { n         composite++; n         if (bound[j] < xt3[j] * 3) composite = 1; n       } else n         composite = 0; n       string1[j] = string2[j]; n       answer += composite; n     } n   } n   cout << answer << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solution(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     if (n == 2 || n == 3 || n == 4 || n == 5 || n == 6 || n == 7) n       cout <<  1  << endl; n     else if (n % 7 == 1) n       cout << ((n - 1) / 7) + 1 << endl; n     else n       cout << (n / 7) + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 1e5 + 5; n struct edge { n   int u, v; n   ll w; n   edge(int u, int v, ll w) : u(u), v(v), w(w) {} n }; n int n, m, k, closestP[N], par[N]; n ll d[N], res; n bool is1portal; n vector<int> portal; n vector<edge> adj[N], edges; n int root(int v) { return (par[v] < 0) ? v : (par[v] = root(par[v])); } n void join(int x, int y) { n   if ((x = root(x)) == (y = root(y))) return; n   if (par[x] > par[y]) swap(x, y); n   par[x] += par[y]; n   par[y] = x; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     adj[u].emplace_back(u, v, w); n     adj[v].emplace_back(v, u, w); n   } n   cin >> k; n   for (int i = 1; i <= k; ++i) { n     int p; n     cin >> p; n     portal.emplace_back(p); n     if (p == 1) is1portal = true; n   } n   priority_queue<pair<ll, pair<int, int>>> q; n   for (int i = 1; i <= n; ++i) d[i] = 1e18; n   for (int p : portal) q.push({d[p] = 0, {p, p}}); n   while (!q.empty()) { n     int u = q.top().second.first; n     int p = q.top().second.second; n     q.pop(); n     if (closestP[u]) continue; n     closestP[u] = p; n     for (edge e : adj[u]) { n       if (d[e.v] > d[u] + e.w) { n         d[e.v] = d[u] + e.w; n         q.push({-d[e.v], {e.v, p}}); n       } n     } n   } n   for (int u = 1; u <= n; ++u) { n     for (edge e : adj[u]) { n       edges.emplace_back(closestP[u], closestP[e.v], d[u] + d[e.v] + e.w); n     } n   } n   sort(edges.begin(), edges.end(), n        [](const edge &x, const edge &y) { return x.w < y.w; }); n   for (int i = 1; i <= n; ++i) par[i] = -1; n   for (edge e : edges) { n     if (root(e.u) != root(e.v)) { n       join(e.u, e.v); n       res += e.w; n     } n   } n   if (!is1portal) res += d[1]; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   vector<long long> a(n); n   long long sums = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     sums += a[i]; n   } n   if (sums % 2 == 0) { n     long long maxs = -1; n     for (long long i = 0; i < n; i++) { n       if (a[i] > maxs) maxs = a[i]; n     } n     if (maxs > sums - maxs) { n       cout <<  NO n ; n     } else { n       cout <<  YES n ; n     } n   } else { n     cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string t; n map<string, int> mp; n int solve(int l, int r) { n   int val = 0; n   for (int i = r - 1; i >= l; i--) { n     if (t[i] ==  ( ) val++; n     if (t[i] ==  ) ) val--; n     if (!val && (t[i] ==  +  || t[i] ==  - )) { n       int L = solve(l, i); n       int R = solve(i + 1, r); n       if (L && R && (t[i] ==  +  || R > 1)) n         return 1; n       else n         return 0; n     } n   } n   for (int i = r - 1; i >= l; i--) { n     if (t[i] ==  ( ) val++; n     if (t[i] ==  ) ) val--; n     if (!val && (t[i] ==  *  || t[i] ==  / )) { n       int L = solve(l, i); n       int R = solve(i + 1, r); n       if (L > 1 && R > 1 && (t[i] ==  *  || R > 2)) n         return 2; n       else n         return 0; n     } n   } n   string x = t.substr(l, r - l); n   if (t[l] ==  ( ) { n     if (solve(l + 1, r - 1)) return 3; n     return 0; n   } n   if (mp.count(x)) return mp[x]; n   return 3; n } n int Get() { n   string tmp; n   getline(cin, tmp); n   int len = tmp.length(); n   t =   ; n   for (int i = 0; i < len; i++) n     if (tmp[i] !=    ) t += tmp[i]; n   return solve(0, t.length()); n } n int main() { n   scanf( %d , &n); n   string s; n   for (int i = 1; i <= n; i++) { n     scanf(  #%*s ); n     cin >> s; n     mp[s] = Get(); n   } n   if (Get()) n     puts( OK ); n   else n     puts( Suspicious ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); n const double eps = 0.00001; n const int MOD = 1000000007; n const int INF = 1000055555; n const long long LLINF = 1223372000000000555; n const int N = 1e5 + 1e4; n const int M = 1234; n const int MAGIC = 10; n vector<int> merge(vector<int> a, vector<int> b) { n   vector<int> v; n   int i = 0, j = 0; n   while (i < a.size() || j < b.size()) { n     if (v.size() == MAGIC) break; n     if (i == a.size()) n       v.push_back(b[j++]); n     else if (j == b.size()) n       v.push_back(a[i++]); n     else if (a[i] < b[j]) n       v.push_back(a[i++]); n     else n       v.push_back(b[j++]); n     if (v.size() > 1 && v[v.size() - 2] == v.back()) v.pop_back(); n   } n   return v; n } n vector<int> g[N]; n vector<int> arr[N][20]; n int tin[N], tout[N], timer = 0; n int up[N][20]; n void dfs(int v, int p) { n   tin[v] = ++timer; n   up[v][0] = p; n   for (int i = 1; i < 20; i++) { n     up[v][i] = up[up[v][i - 1]][i - 1]; n     int UP = up[v][i - 1]; n     arr[v][i] = merge(arr[v][i - 1], arr[UP][i - 1]); n   } n   for (int u : g[v]) { n     if (u != p) dfs(u, v); n   } n   tout[v] = ++timer; n } n bool upper(int a, int b) { return (tin[a] <= tin[b] && tout[a] >= tout[b]); } n int LCA(int a, int b) { n   if (upper(a, b)) return a; n   if (upper(b, a)) return b; n   for (int i = 19; i >= 0; i--) { n     if (!upper(up[a][i], b)) a = up[a][i]; n   } n   return up[a][0]; n } n void out(vector<int> &v, int a) { n   int k = min(a, ((int)v.size())); n   cout << k <<    ; n   for (int i = 0; i < k; i++) { n     cout << v[i] <<    ; n   } n   cout << endl; n } n void solve() { n   int n, m, q; n   cin >> n >> m >> q; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= m; i++) { n     int x; n     cin >> x; n     arr[x][0].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     if (arr[i][0].size() > MAGIC) arr[i][0].resize(MAGIC); n   } n   dfs(1, 1); n   while (q--) { n     int u, v, a; n     cin >> u >> v >> a; n     if (v == u) { n       out(arr[v][0], a); n       continue; n     } n     int lca = LCA(u, v); n     if (u == lca) { n       vector<int> vv; n       for (int i = 19; i >= 0; i--) { n         if (!upper(up[v][i], lca) && up[v][i]) { n           vv = merge(vv, arr[v][i]); n           v = up[v][i]; n         } n       } n       vv = merge(vv, arr[u][0]); n       vv = merge(vv, arr[v][0]); n       out(vv, a); n       continue; n     } n     if (v == lca) { n       vector<int> vv; n       for (int i = 19; i >= 0; i--) { n         if (!upper(up[u][i], lca)) { n           vv = merge(vv, arr[u][i]); n           u = up[u][i]; n         } n       } n       vv = merge(vv, arr[u][0]); n       vv = merge(vv, arr[v][0]); n       out(vv, a); n       continue; n     } n     vector<int> vv = arr[lca][0]; n     for (int i = 19; i >= 0; i--) { n       if (!upper(up[v][i], lca)) { n         vv = merge(vv, arr[v][i]); n         v = up[v][i]; n       } n     } n     for (int i = 19; i >= 0; i--) { n       if (!upper(up[u][i], lca)) { n         vv = merge(vv, arr[u][i]); n         u = up[u][i]; n       } n     } n     vv = merge(vv, arr[u][0]); n     vv = merge(vv, arr[v][0]); n     out(vv, a); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n template <class T, class Comp = less<T> > n struct monoqueue { n   deque<pair<T, int> > dq; n   Comp comp; n   int left, right; n   bool one_index; n   monoqueue(bool ind1) n       : dq(), comp(), left(ind1), right(ind1), one_index(ind1) {} n   void push(T t) { n     while (!dq.empty() && comp(t, dq.back().first)) { n       dq.pop_back(); n     } n     dq.push_back(make_pair(t, right++)); n   } n   template <class Iterator> n   void push(Iterator i, Iterator j) { n     for (; i != j; i++) { n       push(*i); n     } n   } n   void pop() { n     assert(!empty()); n     if (dq.front().second == left) { n       dq.pop_front(); n     } n     left++; n   } n   T get() { n     assert(!empty()); n     return dq.front().first; n   } n   int getind() { n     assert(!empty()); n     return dq.front().second; n   } n   int size() { return right - left; } n   bool empty() { return left == right; } n   void clear() { n     dq.clear(); n     left = right = 0; n   } n }; n int N, S, L; n int arr[MAXN]; n int upto[MAXN]; n monoqueue<int> dqmin(true); n monoqueue<int, greater<int> > dqmax(true); n int dp[MAXN]; n int main() { n   if (fopen( input.txt ,  r )) { n     freopen( input.txt ,  r , stdin); n   } n   ios_base::sync_with_stdio(false); n   cin >> N >> S >> L; n   for (int i = 1; i <= N; i++) { n     cin >> arr[i]; n   } n   for (int i = 1; i <= N; i++) { n     dqmin.push(arr[i]); n     dqmax.push(arr[i]); n     while (!dqmin.empty() && dqmax.get() - dqmin.get() > S) { n       dqmin.pop(); n       dqmax.pop(); n     } n     int m = dqmin.left; n     if (m >= i - L + 2) { n       m = i - L + 2; n     } n     upto[i] = m; n   } n   fill_n(dp + 1, N, MAXN); n   dqmin.clear(); n   for (int i = L; i <= N; i++) { n     dqmin.push(dp[i - L]); n     while (dqmin.left < upto[i] - 1) { n       dqmin.pop(); n     } n     if (dqmin.empty() || dqmin.get() == MAXN) { n       dp[i] = MAXN; n     } else { n       dp[i] = dqmin.get() + 1; n     } n   } n   int ans = dp[N]; n   cout << (ans == MAXN ? -1 : ans) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n bool good[N], need[N]; n int n; n void ask(vector<int> &v) { n   printf( %d , (int)v.size()); n   for (int i = 0; i < (int)v.size(); i++) { n     printf(  %d , v[i]); n     good[v[i]] = true; n   } n   puts(  ); n   fflush(stdout); n   int idx; n   scanf( %d , &idx); n   for (int i = 0; i < (int)v.size(); i++) { n     good[idx] = false; n     idx++; n     if (idx > n) idx = 1; n   } n } n int main() { n   scanf( %d , &n); n   if (n <= 3) { n     puts( 0 ); n     fflush(stdout); n     return 0; n   } n   int cur, best = 0, k; n   for (int sz = 1; sz < n; sz++) { n     cur = ((n + sz) / (sz + 1)); n     cur = n - cur - sz; n     if (cur > best) { n       best = cur; n       k = sz; n     } n   } n   for (int i = 1; i < n; i += k + 1) { n     for (int j = i; j < min(n, i + k); j++) need[j] = true; n   } n   vector<int> v; n   while (true) { n     cur = 0; n     for (int l = 1; l <= n; l++) n       if (good[l]) cur++; n     if (cur >= best) break; n     v.clear(); n     for (int i = 1; i <= n; i++) { n       if (need[i] && !good[i]) v.push_back(i); n     } n     ask(v); n   } n   puts( 0 ); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a1, a2, b1, b2, c1, c2; n struct Point { n   long long x, y; n   Point(long long x = 0, long long y = 0) : x(x), y(y) {} n }; n int exgcd(int a, int b, int& x, int& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   int d = exgcd(b, a % b, x, y); n   int t = x; n   x = y; n   y = t - a / b * y; n   return d; n } n int dcmp(int x) { n   if (x == 0) return 0; n   return x < 0 ? -1 : 1; n } n Point operator+(const Point& a, const Point& b) { n   return Point(a.x + b.x, a.y + b.y); n } n Point ans[5]; n int main() { n   scanf( %lld %lld %lld %lld %lld %lld , &a1, &a2, &b1, &b2, &c1, &c2); n   ans[1] = Point(b1 - a1, b2 - a2); n   ans[2] = Point(b1 - a2, b2 + a1); n   ans[3] = Point(b1 + a1, b2 + a2); n   ans[4] = Point(b1 + a2, b2 - a1); n   if (c1 == 0 && c2 == 0) { n     for (int i = 1; i <= 4; i++) n       if (ans[i].x == 0 && ans[i].y == 0) { n         printf( YES n ); n         return 0; n       } n     printf( NO n ); n     return 0; n   } n   for (int i = 1; i <= 4; i++) { n     long long t1, t2; n     if ((c1 * ans[i].x + c2 * ans[i].y) % (c1 * c1 + c2 * c2) != 0) continue; n     t1 = (c1 * ans[i].x + c2 * ans[i].y) / (c1 * c1 + c2 * c2); n     if ((c2 * ans[i].x - c1 * ans[i].y) % (c1 * c1 + c2 * c2) != 0) continue; n     t2 = (c2 * ans[i].x - c1 * ans[i].y) / (c1 * c1 + c2 * c2); n     printf( YES n ); n     return 0; n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n, ans, x = 201, y = 201, u[405][405]; n int main() { n   scanf( %d , &n); n   cin >> s; n   for (int i = 0; i < n; ++i) { n     ans += u[x][y]; n     ++u[x][y]; n     if (s[i] ==  U ) ++x; n     if (s[i] ==  D ) --x; n     if (s[i] ==  R ) ++y; n     if (s[i] ==  L ) --y; n   } n   ans += u[x][y]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, const T& b) { n   return a > b ? a = b, true : false; n } n template <class T> n bool umax(T& a, const T& b) { n   return a < b ? a = b, true : false; n } n template <long long sz> n using tut = array<long long, sz>; n void usaco(string s) { n   freopen((s +  .in ).c_str(),  r , stdin); n   freopen((s +  .out ).c_str(),  w , stdout); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const long long N = 2e5 + 5; n const long long mod = (119 << 23) + 1; n const long long inf = LLONG_MAX; n const long double Pi = acos(-1); n void add(long long& a, long long b, long long mod = ::mod) { n   a = (a + b) % mod; n } n void sub(long long& a, long long b, long long mod = ::mod) { n   a = ((a - b) % mod + mod) % mod; n } n void mul(long long& a, long long b, long long mod = ::mod) { n   a = (a * 1ll * b) % mod; n } n long long n, m, k, t, q, ans, res, a[N]; n long long rr[N]; n void solve(long long t_case) { n   cin >> n >> k; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   auto check2 = [&](long long b, long long a) -> long long { n     return (a - 3ll * b * b - 1); n   }; n   auto check = [&](long long mm) -> bool { n     long long cnt = 0; n     for (long long i = 1; i <= n; i++) { n       long long l = 0, r = a[i]; n       while (l < r) { n         long long m = (l + r + 1) >> 1; n         if (check2(m, a[i]) >= mm) n           l = m; n         else n           r = m - 1; n       } n       rr[i] = l; n       cnt += l; n     } n     return (cnt >= k); n   }; n   long long l = -inf, r = inf; n   while (l < r) { n     long long m = (l + r + 1) >> 1; n     if (check(m)) n       l = m; n     else n       r = m - 1; n   } n   check(l); n   long long cnt = 0; n   for (long long i = 1; i <= n; i++) cnt += rr[i]; n   cnt -= k; n   for (long long i = 1; i <= n && cnt; i++) { n     if (rr[i] && check2(rr[i], a[i]) == l) cnt--, rr[i]--; n   } n   for (long long i = 1; i <= n; i++) cout << rr[i] <<    ; n   cout <<   n ; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   if (0) { n     long long t; n     cin >> t; n     for (long long t_case = 1; t_case <= t; t_case++) solve(t_case); n   } else n     solve(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, val, sum = 0; n   cin >> n; n   vector<long long> v, q; n   for (long long i = 0; i < n; i++) { n     cin >> val; n     v.push_back(val); n     sum += val; n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   cin >> m; n   for (long long i = 0; i < m; i++) { n     cin >> val; n     cout << sum - v[val - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct vt { n   int p; n   int a; n   int b; n }; n int main() { n   int n, index; n   cin >> n; n   int a[n + 5], b[200005]; n   for (int i = 1; i < 200005; i++) { n     b[i] = 0; n   } n   int maxd = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     b[a[i]]++; n   } n   for (int i = n; i >= 1; i--) { n     if (maxd < b[a[i]]) { n       maxd = b[a[i]]; n       index = i; n     } n   } n   if (maxd == n) n     cout <<  0 ; n   else if (maxd == 1) { n     int u = 0; n     vt x[n + 5]; n     for (int i = 2; i <= n; i++) { n       if (a[i] == a[i - 1]) continue; n       if (a[i] > a[i - 1]) { n         x[u].p = 2; n         x[u].a = i; n         x[u].b = i - 1; n         u++; n         a[i] = a[i - 1]; n       } else if (a[i] < a[i - 1]) { n         x[u].p = 1; n         x[u].a = i; n         x[u].b = i - 1; n         u++; n         a[i] = a[i - 1]; n       } n     } n     cout << u << endl; n     for (int i = 0; i < u; i++) { n       cout << x[i].p <<     << x[i].a <<     << x[i].b << endl; n     } n   } else { n     int u = 0; n     vt x[n + 5]; n     for (int i = index - 1; i >= 1; i--) { n       if (a[i] == a[i + 1]) continue; n       if (a[i] > a[i + 1]) { n         x[u].p = 2; n         x[u].a = i; n         x[u].b = i + 1; n         u++; n         a[i] = a[i + 1]; n       } else if (a[i] < a[i + 1]) { n         x[u].p = 1; n         x[u].a = i; n         x[u].b = i + 1; n         u++; n         a[i] = a[i + 1]; n       } n     } n     for (int i = index + 1; i <= n; i++) { n       if (a[i] == a[i - 1]) continue; n       if (a[i] > a[i - 1]) { n         x[u].p = 2; n         x[u].a = i; n         x[u].b = i - 1; n         u++; n         a[i] = a[i - 1]; n       } else if (a[i] < a[i - 1]) { n         x[u].p = 1; n         x[u].a = i; n         x[u].b = i - 1; n         u++; n         a[i] = a[i - 1]; n       } n     } n     cout << u << endl; n     for (int i = 0; i < u; i++) { n       cout << x[i].p <<     << x[i].a <<     << x[i].b << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n bitset<300000010> su; n int main() { n   su[1] = 1; n   int l, r, ans = 0; n   cin >> l >> r; n   if (l < 3 && r > 1) ans++; n   for (int i = 3; i * i <= r; i += 2) n     if (!su[i]) n       for (long long j = 1LL * i * i; j <= r; j += i << 1) su[j] = 1; n   while (l % 4 != 1) l++; n   for (int i = l; i <= r; i += 4) n     if (!su[i]) ans++; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int power(long long x, unsigned int y, int p) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   long long n, p; n   cin >> n >> p; n   cout << (power(3, n, p) * 1ll + p * 1ll - 1ll) % p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n string s1, s2; n int n, m; n long long dp[maxn][3][3]; n int p1[maxn], p2[maxn]; n int f(char a) { return a ==  G  ? 0 : (a ==  R  ? 1 : 2); } n void init(const string s, int n, const string t, int m, int p[]) { n   p[0] = 0; n   while (p[0] < m && s[0] != t[p[0]]) p[0]++; n   p[0] = min(p[0], m - 1); n   for (int i = 1; i < n; i++) { n     p[i] = p[i - 1] + 1; n     while (p[i] < m && s[i] != t[p[i]]) p[i]++; n     p[i] = min(p[i], m - 1); n   } n } n int main() { n   cin >> s1; n   cin >> s2; n   n = s1.size(); n   m = s2.size(); n   init(s1, n, s2, m, p1); n   init(s2, m, s1, n, p2); n   memset(dp, 0ll, sizeof(dp)); n   for (int i = 1; i < m; i++) { n     for (int j = 0; j < 3; j++) { n       for (int k = 0; k < 3; k++) { n         if (f(s2[i - 1]) == j && f(s2[i]) == k) n           dp[i][j][k] = dp[i - 1][j][k] + 1; n         else n           dp[i][j][k] = dp[i - 1][j][k]; n       } n     } n   } n   long long ret = 0; n   int l = 0, r; n   for (int i = 0; i < n; i++) { n     r = p1[i]; n     while (l < m && p2[l] < i) l++; n     ret += max(r - l + 1, 0); n     if (i && l <= r) { n       int j = f(s1[i - 1]), k = f(s1[i]); n       if (j != k) { n         ret -= dp[r][k][j]; n         if (l - 1 >= 0) ret += dp[l - 1][k][j]; n       } n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int house[300010]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int a, b, c, d; n   for (int i = 0; i < n; ++i) cin >> house[i]; n   a = house[0]; n   d = house[n - 1]; n   if (a != d) { n     cout << n - 1 << endl; n     return 0; n   } n   int i = 0; n   while (house[++i] == a) n     ; n   int j = n; n   while (house[--j] == d) n     ; n   cout << max(n - i - 1, j + 1 - 1) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int dl = 10069; n void bib() { n   for (int i = 0; i < (int)10; i++) { n     cout <<   a ; n   } n } n const int M = 4 * 3 * 100 * 1000 + 10; n int seg[M]; n int ans[M / 2]; n int n, m; n void battle(int s, int e, int winner, int l = 0, int r = n, int id = 1) { n   if (s >= r || e <= l) return; n   if (seg[id] != -1) return; n   if (s <= l && r <= e && (winner < l || winner >= r)) { n     seg[id] = winner; n   } n   if (r - l < 2) return; n   int mid = (r + l) / 2; n   battle(s, e, winner, l, mid, id * 2); n   battle(s, e, winner, mid, r, id * 2 + 1); n } n void print(int l = 0, int r = n, int id = 1) { n   if (r - l == 1) { n     ans[l] = seg[id] + 1; n     return; n   } n   int mid = (l + r) / 2; n   print(l, mid, id * 2); n   print(mid, r, id * 2 + 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < (int)M; i++) seg[i] = -1; n   for (int i = 0; i < (int)m; i++) { n     int l, r, x; n     cin >> l >> r >> x; n     l--; n     x--; n     battle(l, r, x); n   } n   print(); n   for (int i = 0; i < (int)n; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int a[510]; n int dp[510][510]; n int dfs(int l, int r) { n   if (dp[l][r] != -1) { n     return dp[l][r]; n   } n   if (l > r) { n     return 0; n   } n   if (l == r) { n     return 1; n   } n   int mi = 1 + dfs(l + 1, r); n   if (a[l] == a[l + 1]) { n     mi = min(mi, 1 + dfs(l + 2, r)); n   } n   for (int i = l + 2; i <= r; ++i) { n     if (a[i] == a[l]) { n       mi = min(mi, dfs(l + 1, i - 1) + dfs(i + 1, r)); n     } n   } n   return dp[l][r] = mi; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   memset(dp, -1, sizeof(dp)); n   printf( %d n , dfs(0, n - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long n, a, b, c, d; n     cin >> n >> a >> b >> c >> d; n     int x = 0; n     if (((c + d) < n * (a - b)) || ((c - d) > n * (a + b))) n       cout <<  No  << endl; n     else n       cout <<  Yes  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:36777216 ) n template <class T> n inline void RD(T &); n template <class T> n inline void OT(const T &); n template <class T> n inline T &_RD(T &x) { n   RD(x); n   return x; n } n inline int RD() { n   int x; n   return _RD(x); n } n inline void RF(double &x) { scanf( %lf , &x); } n inline double _RF(double &x) { n   scanf( %lf , &x); n   return x; n } n inline double RF() { n   double x; n   return _RF(x); n } n inline void RC(char &c) { scanf(  %c , &c); } n inline char _RC(char &c) { n   scanf(  %c , &c); n   return c; n } n inline char RC() { n   char c; n   return _RC(c); n } n inline void RS(char *s) { scanf( %s , s); } n template <class T0, class T1> n inline void RD(T0 &x0, T1 &x1) { n   RD(x0), RD(x1); n } n template <class T0, class T1, class T2> n inline void RD(T0 &x0, T1 &x1, T2 &x2) { n   RD(x0), RD(x1), RD(x2); n } n template <class T0, class T1, class T2, class T3> n inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { n   RD(x0), RD(x1), RD(x2), RD(x3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); n } n template <class T0, class T1> n inline void OT(T0 &x0, T1 &x1) { n   OT(x0), OT(x1); n } n template <class T0, class T1, class T2> n inline void OT(T0 &x0, T1 &x1, T2 &x2) { n   OT(x0), OT(x1), OT(x2); n } n template <class T0, class T1, class T2, class T3> n inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { n   OT(x0), OT(x1), OT(x2), OT(x3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); n } n template <class T> n inline void RST(T &A) { n   memset(A, 0, sizeof(A)); n } n template <class T0, class T1> n inline void RST(T0 &A0, T1 &A1) { n   RST(A0), RST(A1); n } n template <class T0, class T1, class T2> n inline void RST(T0 &A0, T1 &A1, T2 &A2) { n   RST(A0), RST(A1), RST(A2); n } n template <class T0, class T1, class T2, class T3> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { n   RST(A0), RST(A1), RST(A2), RST(A3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); n } n template <class T> n inline void CLR(priority_queue<T, vector<T>, less<T> > &Q) { n   while (!Q.empty()) Q.pop(); n } n template <class T> n inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) { n   while (!Q.empty()) Q.pop(); n } n template <class T> n inline void CLR(T &A) { n   A.clear(); n } n template <class T0, class T1> n inline void CLR(T0 &A0, T1 &A1) { n   CLR(A0), CLR(A1); n } n template <class T0, class T1, class T2> n inline void CLR(T0 &A0, T1 &A1, T2 &A2) { n   CLR(A0), CLR(A1), CLR(A2); n } n template <class T0, class T1, class T2, class T3> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6); n } n template <class T> n inline void CLR(T &A, int n) { n   for (int i = 0; i < int(n); ++i) CLR(A[i]); n } n template <class T> n inline void FLC(T &A, int x) { n   memset(A, x, sizeof(A)); n } n template <class T0, class T1> n inline void FLC(T0 &A0, T1 &A1, int x) { n   FLC(A0, x), FLC(A1, x); n } n template <class T0, class T1, class T2> n inline void FLC(T0 &A0, T1 &A1, T2 &A2) { n   FLC(A0), FLC(A1), FLC(A2); n } n template <class T0, class T1, class T2, class T3> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { n   FLC(A0), FLC(A1), FLC(A2), FLC(A3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { n   FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { n   FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { n   FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6); n } n template <class T> n inline void SRT(T &A) { n   sort(A.begin(), A.end()); n } n template <class T, class C> n inline void SRT(T &A, C B) { n   sort(A.begin(), A.end(), B); n } n const int MOD = 1000000007; n const int INF = 0x3f3f3f3f; n const long long INF_64 = 0x3f3f3f3f3f3f3f3fLL; n const double EPS = 1e-8; n const double OO = 1e15; n const double PI = acos(-1.0); n template <class T> n inline void checkMin(T &a, const T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkMax(T &a, const T b) { n   if (b > a) a = b; n } n template <class T, class C> n inline void checkMin(T &a, const T b, C c) { n   if (c(b, a)) a = b; n } n template <class T, class C> n inline void checkMax(T &a, const T b, C c) { n   if (c(a, b)) a = b; n } n template <class T> n inline T min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline T min(T a, T b, T c, T d) { n   return min(min(a, b), min(c, d)); n } n template <class T> n inline T max(T a, T b, T c, T d) { n   return max(min(a, b), max(c, d)); n } n template <class T, class C> n inline T min_cp(T a, T b, C cp) { n   return min(a, b, cp); n } n template <class T, class C> n inline T min_cp(T a, T b, T c, C cp) { n   return min_cp(min_cp(a, b, cp), c, cp); n } n template <class T, class C> n inline T min_cp(T a, T b, T c, T d, C cp) { n   return min_cp(min_cp(a, b, cp), min_cp(c, d, cp), cp); n } n template <class T, class C> n inline T max_cp(T a, T b, C cp) { n   return max(a, b, cp); n } n template <class T, class C> n inline T max_cp(T a, T b, T c, C cp) { n   return max_cp(max_cp(a, b, cp), c, cp); n } n template <class T, class C> n inline T max_cp(T a, T b, T c, T d, C cp) { n   return max_cp(max_cp(a, b, cp), max_cp(c, d, cp), cp); n } n template <class T> n inline T sqr(T a) { n   return a * a; n } n template <class T> n inline T cub(T a) { n   return a * a * a; n } n int Ceil(int x, int y) { return (x - 1) / y + 1; } n inline bool _1(int x, int i) { return x & 1 << i; } n inline bool _1(long long x, int i) { return x & 1LL << i; } n inline long long _1(int i) { return 1LL << i; } n inline long long _U(int i) { return _1(i) - 1; }; n template <class T> n inline T low_bit(T x) { n   return x & -x; n } n template <class T> n inline T high_bit(T x) { n   T p = low_bit(x); n   while (p != x) x -= p, p = low_bit(x); n   return p; n } n template <class T> n inline T cover_bit(T x) { n   T p = 1; n   while (p < x) p <<= 1; n   return p; n } n inline int count_bits(int x) { n   x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1); n   x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2); n   x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4); n   x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8); n   x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16); n   return x; n } n inline int count_bits(long long x) { n   x = (x & 0x5555555555555555LL) + ((x & 0xaaaaaaaaaaaaaaaaLL) >> 1); n   x = (x & 0x3333333333333333LL) + ((x & 0xccccccccccccccccLL) >> 2); n   x = (x & 0x0f0f0f0f0f0f0f0fLL) + ((x & 0xf0f0f0f0f0f0f0f0LL) >> 4); n   x = (x & 0x00ff00ff00ff00ffLL) + ((x & 0xff00ff00ff00ff00LL) >> 8); n   x = (x & 0x0000ffff0000ffffLL) + ((x & 0xffff0000ffff0000LL) >> 16); n   x = (x & 0x00000000ffffffffLL) + ((x & 0xffffffff00000000LL) >> 32); n   return x; n } n int reverse_bits(int x) { n   x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa); n   x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc); n   x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); n   x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00); n   x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000); n   return x; n } n long long reverse_bits(long long x) { n   x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL); n   x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL); n   x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL); n   x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL); n   x = ((x >> 16) & 0x0000ffff0000ffffLL) | ((x << 16) & 0xffff0000ffff0000LL); n   x = ((x >> 32) & 0x00000000ffffffffLL) | ((x << 32) & 0xffffffff00000000LL); n   return x; n } n using namespace std; n class bignum { n   friend istream &operator>>(istream &, bignum &); n   friend ostream &operator<<(ostream &, const bignum &); n   friend bignum operator+(const bignum &, const bignum &); n   friend bignum operator-(const bignum &, const bignum &); n   friend bignum operator*(const bignum &, const bignum &); n   friend bignum operator/(const bignum &, const bignum &); n   friend bignum operator%(const bignum &, const bignum &); n   friend bignum operator+(const bignum &, const int &); n   friend bignum operator-(const bignum &, const int &); n   friend bignum operator*(const bignum &, const int &); n   friend bignum operator/(const bignum &, const int &); n   friend bignum operator%(const bignum &, const int &); n   friend bool operator==(const bignum &, const bignum &); n   friend bool operator!=(const bignum &, const bignum &); n   friend bool operator<(const bignum &, const bignum &); n   friend bool operator>(const bignum &, const bignum &); n   friend bool operator<=(const bignum &, const bignum &); n   friend bool operator>=(const bignum &, const bignum &); n   friend bool operator==(const bignum &, const int &); n   friend bool operator!=(const bignum &, const int &); n   friend bool operator<(const bignum &, const int &); n   friend bool operator>(const bignum &, const int &); n   friend bool operator<=(const bignum &, const int &); n   friend bool operator>=(const bignum &, const int &); n   friend int do_comp(const bignum &, const int &); n   friend int do_comp(const bignum &, const bignum &); n   friend void divide(const bignum &, const bignum &, bignum &, bignum &); n   friend bignum pow(bignum, int); n  n  public: n   inline bignum(){}; n   inline bignum(int s) { n     while (s != 0) { n       data.push_back(s % 100000000); n       s /= 100000000; n     } n     if (data.empty()) data.push_back(0); n   } n   inline bignum(long long s) { n     while (s != 0) { n       data.push_back(int(s % 100000000)); n       s /= 100000000; n     } n     if (data.empty()) data.push_back(0); n   } n   inline bignum(string s) { n     int t, i; n     data.clear(); n     for (i = int(s.size()) - 8; i > 0; i -= 8) { n       istringstream(s.substr(i, 8)) >> t; n       data.push_back(t); n     } n     istringstream(s.substr(0, i + 8)) >> t; n     data.push_back(t); n   } n   void operator=(const int); n   void operator=(const string); n   void operator=(const bignum); n   bignum &operator+=(const bignum &); n   bignum &operator-=(const bignum &); n   bignum &operator*=(const bignum &); n   bignum &operator/=(const bignum &); n   bignum &operator%=(const bignum &); n   bignum &operator+=(const int &); n   bignum &operator-=(const int &); n   bignum &operator*=(const int &); n   bignum &operator/=(const int &); n   bignum &operator%=(const int &); n   bignum &operator%=(const long long &); n   bool undefined(); n   int do_try(const int &); n   int do_try(const bignum &); n   void do_trim(); n   list<int> data; n   void add(const int a) { n     list<int>::iterator it = data.end(); n     it--; n     if ((*it) < 10) n       (*it) = a; n     else if ((*it) < 100) n       (*it) = (*it) % 10 + 10 * a; n     else if ((*it) < 1000) n       (*it) = (*it) % 100 + 100 * a; n     else if ((*it) < 10000) n       (*it) = (*it) % 1000 + 1000 * a; n     else if ((*it) < 100000) n       (*it) = (*it) % 10000 + 10000 * a; n     else if ((*it) < 1000000) n       (*it) = (*it) % 100000 + 100000 * a; n     else if ((*it) < 10000000) n       (*it) = (*it) % 1000000 + 1000000 * a; n     else if ((*it) < 100000000) n       (*it) = (*it) % 10000000 + 10000000 * a; n   } n   void clear() { data.clear(); } n   long long t() { n     if (data.size() == 1) n       return *data.begin(); n     else { n       list<int>::iterator it = data.begin(), jt = data.begin(); n       ++it; n       return (long long)(*it) * 100000000 + (*jt); n     } n   } n   int size() { n     list<int>::iterator it; n     int res = 0; n     for (it = data.begin(); it != data.end(); it++) res += 8; n     it--; n     if (*it >= 10000) { n       if ((*it) >= 1000000) { n         if (*it >= 10000000) n           ; n         else n           res--; n       } else { n         if ((*it) >= 100000) n           res -= 2; n         else n           res -= 3; n       } n     } else if ((*it) >= 100) { n       if (*it >= 1000) n         res -= 4; n       else n         res -= 5; n     } else { n       if ((*it) >= 10) n         res -= 6; n       else n         res -= 7; n     } n     return res; n   } n   void do_reserve(int a) { n     if (a <= 0) return; n     list<int>::iterator it; n     for (it = data.begin(); it != data.end() && a > 0; it++) a -= 8; n     if (it == data.end() && a >= 0) return; n     a += 8, it--; n     int f = 1; n     for (int i = 0; i < a; i++) f *= 10; n     (*it) %= f; n     data.erase(++it, data.end()); n     do_trim(); n   } n   void output() { n     list<int>::reverse_iterator i = data.rbegin(); n     printf( %d , *i); n     for (i++; i != data.rend(); i++) printf( %08d , *i); n   } n }; n inline void bignum::operator=(const bignum a) { n   data.clear(); n   for (list<int>::const_iterator i = a.data.begin(); i != a.data.end(); i++) { n     data.push_back(*i); n   } n } n inline void bignum::operator=(const string a) { (*this) = bignum(a); } n inline void bignum::operator=(const int a) { (*this) = bignum(a); } n inline istream &operator>>(istream &input, bignum &a) { n   string s; n   int t, i; n   input >> s; n   a.data.clear(); n   for (i = int(s.size()) - 8; i > 0; i -= 8) { n     istringstream(s.substr(i, 8)) >> t; n     a.data.push_back(t); n   } n   istringstream(s.substr(0, i + 8)) >> t; n   a.data.push_back(t); n   return input; n } n inline ostream &operator<<(ostream &output, const bignum &a) { n   list<int>::const_reverse_iterator i = a.data.rbegin(); n   output << *i; n   for (i++; i != a.data.rend(); i++) { n     if (*i >= 10000) { n       if (*i >= 1000000) { n         if (*i >= 10000000) n           cout << *i; n         else n           cout << 0 << *i; n       } else { n         if (*i >= 100000) n           cout <<  00  << *i; n         else n           cout <<  000  << *i; n       } n     } else { n       if (*i >= 100) { n         if (*i >= 1000) n           cout <<  0000  << *i; n         else n           cout <<  00000  << *i; n       } else { n         if (*i >= 10) n           cout <<  000000  << *i; n         else n           cout <<  0000000  << *i; n       } n     } n   } n   return output; n } n inline bool bignum::undefined() { return data.empty(); } n inline int do_comp(const bignum &a, const bignum &b) { n   if (a.data.size() < b.data.size()) return -1; n   if (a.data.size() > b.data.size()) return 1; n   list<int>::const_reverse_iterator i; n   list<int>::const_reverse_iterator j; n   for (i = a.data.rbegin(), j = b.data.rbegin(); j != b.data.rend(); i++, j++) { n     if (*i < *j) return -1; n     if (*i > *j) return 1; n   } n   return 0; n } n inline int do_comp(const bignum &a, const int &b) { n   return do_comp(a, bignum(b)); n } n inline bool operator==(const bignum &a, const bignum &b) { n   return do_comp(a, b) == 0; n } n inline bool operator!=(const bignum &a, const bignum &b) { n   return do_comp(a, b) != 0; n } n inline bool operator<(const bignum &a, const bignum &b) { n   return do_comp(a, b) == -1; n } n inline bool operator>(const bignum &a, const bignum &b) { n   return do_comp(a, b) == 1; n } n inline bool operator<=(const bignum &a, const bignum &b) { n   return do_comp(a, b) != 1; n } n inline bool operator>=(const bignum &a, const bignum &b) { n   return do_comp(a, b) != -1; n } n inline bool operator==(const bignum &a, const int &b) { n   return do_comp(a, b) == 0; n } n inline bool operator!=(const bignum &a, const int &b) { n   return do_comp(a, b) != 0; n } n inline bool operator<(const bignum &a, const int &b) { n   return do_comp(a, b) == -1; n } n inline bool operator>(const bignum &a, const int &b) { n   return do_comp(a, b) == 1; n } n inline bool operator<=(const bignum &a, const int &b) { n   return do_comp(a, b) != 1; n } n inline bool operator>=(const bignum &a, const int &b) { n   return do_comp(a, b) != -1; n } n inline void bignum::do_trim() { n   while (data.size() > 1 && data.back() == 0) data.pop_back(); n } n inline bignum &bignum::operator+=(const bignum &a) { n   list<int>::iterator i; n   list<int>::const_iterator j; n   int t = 0; n   for (i = data.begin(), j = a.data.begin(); n        i != data.end() && j != a.data.end(); i++, j++) { n     *i += *j + t; n     t = *i / 100000000; n     *i %= 100000000; n   } n   while (i != data.end()) { n     *i += t; n     t = *i / 100000000; n     *i %= 100000000; n     i++; n   } n   while (j != a.data.end()) { n     data.push_back(t + *j); n     t = data.back() / 100000000; n     data.back() %= 100000000; n     j++; n   } n   if (t != 0) data.push_back(t); n   return *this; n } n inline bignum &bignum::operator-=(const bignum &a) { n   list<int>::iterator i; n   list<int>::const_iterator j; n   int t = 0; n   for (i = data.begin(), j = a.data.begin(); j != a.data.end(); i++, j++) { n     *i -= t + *j; n     if (*i >= 0) n       t = 0; n     else n       *i += 100000000, t = 1; n   } n   while (i != data.end()) { n     *i -= t; n     if (*i >= 0) n       t = 0; n     else n       *i += 100000000, t = 1; n     i++; n   } n   (*this).do_trim(); n   return *this; n } n inline bignum &bignum::operator+=(const int &a) { return (*this) += bignum(a); } n inline bignum &bignum::operator-=(const int &a) { return (*this) -= bignum(a); } n inline bignum operator+(const bignum &a, const bignum &b) { n   list<int>::const_iterator i, j; n   bignum c; n   int t = 0; n   for (i = a.data.begin(), j = b.data.begin(); n        i != a.data.end() && j != b.data.end(); i++, j++) { n     c.data.push_back(t + *i + *j); n     t = c.data.back() / 100000000; n     c.data.back() %= 100000000; n   } n   while (i != a.data.end()) { n     c.data.push_back(t + *i); n     t = c.data.back() / 100000000; n     c.data.back() %= 100000000; n     i++; n   } n   while (j != b.data.end()) { n     c.data.push_back(t + *j); n     t = c.data.back() / 100000000; n     c.data.back() %= 100000000; n     j++; n   } n   if (t != 0) c.data.push_back(t); n   return c; n } n inline bignum operator-(const bignum &a, const bignum &b) { n   list<int>::const_iterator i, j; n   bignum c; n   int t = 0; n   for (i = a.data.begin(), j = b.data.begin(); j != b.data.end(); i++, j++) { n     t = *i - t; n     if (t >= *j) n       c.data.push_back(t - *j), t = 0; n     else n       c.data.push_back(t + 100000000 - *j), t = 1; n   } n   while (i != a.data.end()) { n     t = *i - t; n     if (t >= 0) n       c.data.push_back(t), t = 0; n     else n       c.data.push_back(t + 100000000), t = 1; n     i++; n   } n   c.do_trim(); n   return c; n } n inline bignum operator*(const bignum &a, const bignum &b) { n   list<int>::const_iterator i, j; n   list<int>::iterator k, kk; n   bignum c; n   long long t = 0; n   for (int i = 0; i < a.data.size() + b.data.size(); i++) c.data.push_back(0); n   for (i = a.data.begin(), k = c.data.begin(); i != a.data.end(); i++, k++) { n     for (j = b.data.begin(), kk = k; j != b.data.end(); j++, kk++) { n       t += (long long)(*i) * (*j) + (*kk); n       *kk = int(t % 100000000); n       t /= 100000000; n     } n     *kk += t; n     t = 0; n   } n   c.do_trim(); n   return c; n } n inline int bignum::do_try(const bignum &a) { n   int l = 1, r = 99999999, m, t; n   while (l + 2 < r) { n     m = (l + r) / 2; n     t = do_comp(*this, a * bignum(m)); n     if (t == 0) return m; n     if (t < 0) n       r = m - 1; n     else n       l = m; n   } n   while (do_comp(*this, a * bignum(r)) < 0) r--; n   return r; n } n inline void divide(const bignum &a, const bignum &b, bignum &d, bignum &r) { n   list<int>::const_reverse_iterator i = a.data.rbegin(); n   int t; n   d = bignum(0); n   r = bignum(0); n   do { n     while (r < b && i != a.data.rend()) { n       d.data.push_front(0); n       r.data.push_front(*i); n       r.do_trim(); n       i++; n     } n     if (r >= b) { n       t = r.do_try(b); n       d.data.front() = t; n       r -= (b * bignum(t)); n     } n   } while (i != a.data.rend()); n   d.do_trim(); n } n inline bignum operator/(const bignum &a, const bignum &b) { n   bignum d, r; n   divide(a, b, d, r); n   return d; n } n inline bignum operator%(const bignum &a, const bignum &b) { n   bignum d, r; n   divide(a, b, d, r); n   return r; n } n inline bignum operator+(const bignum &a, const int &b) { return a + bignum(b); } n inline bignum operator-(const bignum &a, const int &b) { return a - bignum(b); } n inline bignum operator*(const bignum &a, const int &b) { return a * bignum(b); } n inline bignum operator/(const bignum &a, const int &b) { return a / bignum(b); } n inline bignum &bignum::operator*=(const bignum &a) { n   (*this) = (*this) * a; n   return *this; n } n inline bignum &bignum::operator/=(const bignum &a) { n   (*this) = (*this) / a; n   return *this; n } n inline bignum &bignum::operator%=(const bignum &a) { n   (*this) = (*this) % a; n   return *this; n } n inline bignum &bignum::operator*=(const int &a) { return (*this) *= bignum(a); } n inline bignum &bignum::operator/=(const int &a) { return (*this) /= bignum(a); } n inline bignum &bignum::operator%=(const long long &b) { n   return (*this) %= bignum(b); n } n inline bignum pow(bignum a, int b) { n   bignum c(1); n   while (b != 0) { n     if (b & 1) c *= a; n     a = a * a; n     b >>= 1; n   } n   return c; n } n inline int rand32() { n   return (bool(rand() & 1) << 30) | (rand() << 15) + rand(); n } n inline int random32(int l, int r) { return rand32() % (r - l + 1) + l; } n inline int random(int l, int r) { return rand() % (r - l + 1) + l; } n int dice() { return rand() % 6; } n bool coin() { return rand() % 2; } n template <class T> n inline void RD(T &x) { n   char c; n   for (c = getchar(); c <  0 ; c = getchar()) n     ; n   x = c -  0 ; n   for (c = getchar(); c >=  0 ; c = getchar()) x = x * 10 + c -  0 ; n } n template <class T> n inline void OT(const T &x) { n   printf( %d n , x); n } n const int maxn = 2; n long long modo; n class Mat { n  public: n   int Matn, Matm; n   long long a[maxn][maxn]; n   Mat() { n     Matn = 0; n     Matm = 0; n     memset(a, 0, sizeof(a)); n   } n   void output(); n   void init(); n   void initI(); n   Mat mul(const Mat &a); n   Mat power(const Mat &a, long long k); n }; n void Mat::output() { n   for (int i = 0; i < Matn; ++i) { n     for (int j = 0; j < Matm; ++j) { n       if (j != 0) printf(   ); n       printf( %d , a[i][j]); n     } n     printf(  n ); n   } n } n void Mat::init() { n   Matn = 0; n   Matm = 0; n   memset(a, 0, sizeof(a)); n } n Mat Mat::mul(const Mat &A) { n   Mat c; n   c.Matn = Matn; n   c.Matm = A.Matm; n   for (int i = 0; i < Matn; ++i) n     for (int j = 0; j < A.Matm; ++j) { n       for (int k = 0; k < Matm; ++k) { n         c.a[i][j] = (c.a[i][j] + (a[i][k] * A.a[k][j]) % modo) % modo; n       } n     } n   return c; n } n void Mat::initI() { n   memset(a, 0, sizeof(a)); n   for (int i = 0; i < Matn; ++i) a[i][i] = 1LL; n } n Mat Mat::power(const Mat &a, long long k) { n   Mat c = a, b; n   b.init(); n   b.Matn = a.Matn; n   b.Matm = a.Matm; n   b.initI(); n   while (k) { n     if (k & 1LL) b = b.mul(c); n     c = c.mul(c); n     k >>= 1LL; n   } n   return b; n } n long long gao(long long t) { n   if (t <= 1) return 1LL; n   Mat a; n   a.Matm = a.Matn = 2; n   a.a[0][0] = 1LL, a.a[1][0] = 1LL, a.a[0][1] = 1LL; n   a = a.power(a, t - 1LL); n   return a.a[0][0]; n } n long long l, r, k; n void solve() { n   long long tmp, ans, tmp1; n   if ((r - l) / k <= 2000000) { n     for (long long i = (r - l) / (k - 1); i >= (r - l) / k; i--) { n       if ((r / i - (l - 1) / i) >= k) { n         ans = i; n         break; n       } n     } n   } else { n     for (long long i = k - 1; i <= 2000000; i++) { n       tmp = r / i; n       tmp1 = tmp * (i - k + 1); n       if (tmp1 >= l) { n         ans = tmp; n         break; n       } n     } n   } n   cout << gao(ans) % modo << endl; n } n int main() { n   while (cin >> modo >> l >> r >> k) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1005]; n bool t[1005]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   memset(t, 0, sizeof(t)); n   int count = 0; n   int taken = 0; n   bool right = true; n   while (1) { n     if (right) { n       for (int i = 0; i < n; i++) { n         if (taken >= a[i] && !t[i]) { n           t[i] = true; n           taken++; n         } n       } n     } else { n       for (int i = n - 1; i >= 0; i--) { n         if (taken >= a[i] && !t[i]) { n           t[i] = true; n           taken++; n         } n       } n     } n     if (taken == n) break; n     count++; n     right = !right; n   } n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, speed, limit, t6, ans; n stack<int> st; n int main() { n   cin >> N; n   for (int t, i = 1; i <= N; i++) { n     scanf( %d , &t); n     if (t == 6) { n       t6++; n     } else if (t == 4) { n       t6 = 0; n     } else if (t == 2) { n       ans += t6; n       t6 = 0; n     } else if (t == 1) { n       scanf( %d , &speed); n       while (!st.empty() && speed > st.top()) { n         ans++; n         st.pop(); n       } n     } else if (t == 5) { n       while (!st.empty()) st.pop(); n     } else if (t == 3) { n       scanf( %d , &limit); n       st.push(limit); n       while (!st.empty() && speed > st.top()) { n         ans++; n         st.pop(); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, dp[20][4][4][11][11]; n int main() { n   scanf( %d%d , &n, &t); n   memset(dp, 0, sizeof(dp)); n   for (int i = 0; i != 4; ++i) n     for (int j = 0; j != 4; ++j) dp[1][i][j][0][0] = i != j; n   for (int i = 2; i != n; ++i) n     for (int j = 0; j != 4; ++j) n       for (int k = 0; k != 4; ++k) n         for (int l = 0; l <= t; ++l) n           for (int m = 0; m <= t; ++m) n             for (int o = 0; o != 4; ++o) n               dp[i][k][o][l + (j < k && k > o)][m + (j > k && k < o)] += n                   k == o ? 0 : dp[i - 1][j][k][l][m]; n   int ans = 0; n   for (int i = 0; i != 4; ++i) n     for (int j = 0; j != 4; ++j) ans += dp[n - 1][i][j][t][t - 1]; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 0x3f3f3f3f; n const int N = 2e5 + 10; n const int M = 1e6 + 10; n const long long mod = 1e9 + 7; n const double PI = acos(-1); n int sq = 3000; n int cntq, cntc; n map<int, int> mp; n int vis[N]; n int cnt[N]; n int num[N]; n int ans[N]; n struct query { n   int l, r, z, id; n } qq[N]; n struct change { n   int x, y; n } ch[N]; n bool cmp1(query a, query b) { n   if (a.l / sq == b.l / sq) { n     if (a.r / sq == b.r / sq) n       return a.z < b.z; n     else n       return a.r < b.r; n   } else n     return a.l < b.l; n } n void update(int x, int y) { n   vis[cnt[num[x]]]--; n   cnt[num[x]] += y; n   vis[cnt[num[x]]]++; n } n void solve() { n   int l = 1, r = 1, c = 0, res; n   update(1, 1); n   for (int i = 1; i <= cntq; ++i) { n     while (r < qq[i].r) update(++r, 1); n     while (r > qq[i].r) update(r--, -1); n     while (l > qq[i].l) update(--l, 1); n     while (l < qq[i].l) update(l++, -1); n     while (c > qq[i].z) { n       if (ch[c].x >= l && ch[c].x <= r) update(ch[c].x, -1); n       swap(ch[c].y, num[ch[c].x]); n       if (ch[c].x >= l && ch[c].x <= r) update(ch[c].x, 1); n       c--; n     } n     while (c < qq[i].z) { n       c++; n       if (ch[c].x >= l && ch[c].x <= r) update(ch[c].x, -1); n       swap(ch[c].y, num[ch[c].x]); n       if (ch[c].x >= l && ch[c].x <= r) update(ch[c].x, 1); n     } n     res = 1; n     while (vis[res]) res++; n     ans[qq[i].id] = res; n   } n } n int main() { n   int n, q, tot = 0, x, op, l, r; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &x); n     if (mp[x]) n       num[i] = mp[x]; n     else n       num[i] = mp[x] = ++tot; n   } n   for (int i = 1; i <= q; ++i) { n     scanf( %d%d%d , &op, &l, &r); n     if (op == 1) { n       ++cntq; n       qq[cntq] = {l, r, cntc, cntq}; n     } else { n       ++cntc; n       if (mp[r]) n         r = mp[r]; n       else n         r = mp[r] = ++tot; n       ch[cntc] = {l, r}; n     } n   } n   sort(qq + 1, qq + cntq + 1, cmp1); n   solve(); n   for (int i = 1; i <= cntq; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > solve(int N) { n   vector<vector<int> > res(2, vector<int>(1, 1)); n   for (int n = 2, r = 0; n <= N; ++n) { n     res[r].push_back(n); n     ++r; n     if (r >= int((res).size())) { n       vector<int> new_row; n       for (const vector<int>& r : res) new_row.push_back(r.back()); n       res.push_back(new_row); n       r = 0; n     } n   } n   return res; n } n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int N; n   cin >> N; n   vector<vector<int> > res = solve(N); n   cout << int((res).size()) <<   n ; n   for (const vector<int>& row : res) { n     for (int x : row) cout << x <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 505; n int n, m; n int v[MAXN]; n int head[MAXN]; n double e[MAXN * MAXN]; n int a, b, c; n int main() { n   while (scanf( %d%d , &n, &m) != EOF) { n     for (int i = 1; i <= n; i++) scanf( %d , &v[i]); n     for (int i = 1; i <= m; i++) { n       scanf( %d%d%d , &a, &b, &c); n       e[i] = 1.0 * (v[a] + v[b]) / c; n     } n     double ans = 0.0; n     for (int i = 1; i <= m; i++) ans = max(ans, e[i]); n     printf( %.12lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } debug1; n template <typename T1, typename T2> n inline ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n inline ostream& operator<<(ostream& os, const vector<T>& v) { n   bool first = true; n   os <<  [ ; n   for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n const long long inf = numeric_limits<long long>::min(); n const int NN = 112345; n long long dp[NN][3], cnt[NN]; n bool vis[NN][3]; n long long f(int x, int last) { n   last = ((cnt[x - 1] == 0 and last == 0) ? 2 : last); n   if (cnt[x] == 0 and last == 0) return inf; n   if (x > 100000) return 0LL; n   if (vis[x][last]) return dp[x][last]; n   vis[x][last] = true; n   long long ans = 0; n   if (last == 0) { n     ans = cnt[x] * (long long)x; n     ans += f(x + 1, 1); n   } else if (last == 1) n     ans = f(x + 1, 2); n   else { n     ans = max(ans, f(x + 1, 0)); n     if (cnt[x] > 0) { n       long long add = cnt[x] * (long long)x; n       ans = max(ans, add + f(x + 1, 1)); n     } n   } n   dp[x][last] = ans; n   return ans; n } n void solve() { n   int n, num; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> num; n     cnt[num]++; n   } n   long long ans = f(1, 2); n   cout << ans <<   n ; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m, a, b; n   cin >> n >> m >> a >> b; n   int ans = 3; n   if (a % m == 1 && b % m == 0) { n     ans = min(ans, 1); n   } n   if (a % m == 1 || b % m == 0) { n     ans = min(ans, 2); n   } n   int bf = (a - 1) / m; n   int bs = (b - 1) / m; n   if (bf == bs) { n     ans = min(ans, 1); n   } n   if (bf + 1 == bs) { n     ans = min(ans, 2); n   } n   if (a % m == 1 && b == n) { n     ans = min(ans, 1); n   } n   if (b == n) { n     ans = min(ans, 2); n   } n   int p1 = (a - 1) % m; n   int p2 = (b - 1) % m; n   if (p1 == p2 + 1) { n     ans = min(ans, 2); n   } n   if (m == 1) { n     ans = min(ans, 1); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n vector<int> g[N]; n int deg[N]; n int cnt[N]; n void dfs(int v, int &l) { n   for (int i = 0; i < g[v].size(); i++) { n     cnt[g[v][i]] = l + 1; n     l++; n     dfs(g[v][i], l); n     l--; n   } n } n int main() { n   ios_base::sync_with_stdio(), cout.tie(NULL), cin.tie(NULL); n   ; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> deg[i]; n     if (deg[i] != -1) { n       g[deg[i]].push_back(i); n     } n   } n   int ans = 1; n   for (int i = 1; i <= n; i++) { n     if (deg[i] == -1) { n       int level = 1; n       dfs(i, level); n     } n   } n   for (int i = 1; i <= n; i++) { n     ans = max(ans, cnt[i]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool getmin(T *a, const T &b) { n   if (b < *a) { n     *a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool getmax(T *a, const T &b) { n   if (b > *a) { n     *a = b; n     return true; n   } n   return false; n } n template <class T> n inline void read(T *a) { n   char c; n   while (isspace(c = getchar())) { n   } n   bool flag = 0; n   if (c ==  - ) n     flag = 1, *a = 0; n   else n     *a = c - 48; n   while (isdigit(c = getchar())) *a = *a * 10 + c - 48; n   if (flag) *a = -*a; n } n const int mo = 1000000007; n template <class T> n T pow(T a, T b, int c = mo) { n   T res = 1; n   for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c) n     if (b & i) res = 1LL * res * a % c; n   return res; n } n const int N = 100100; n int n; n int a[N]; n int s1[N], s2[N]; n pair<int, int> ans[N]; n int check(int x) { n   int j = 0, i = 1, num1 = 0, num2 = 0, last = 0; n   while (i <= n) { n     int l = i, r = n, res = 0; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (max(s1[mid] - s1[j], s2[mid] - s2[j]) >= x) n         res = mid, r = mid - 1; n       else n         l = mid + 1; n     } n     if (!res) return 0; n     if (s1[res] - s1[j] == x) n       ++num1, last = 1; n     else n       ++num2, last = 2; n     j = res, i = res + 1; n   } n   if (num1 > num2 && last == 1) n     return num1; n   else if (num2 > num1 && last == 2) n     return num2; n   return 0; n } n int main() { n   cin >> n; n   for (int i = (1); i <= (n); ++i) { n     read(a + i); n     s1[i] = s1[i - 1] + (a[i] == 1); n     s2[i] = s2[i - 1] + (a[i] == 2); n   } n   int m = 0; n   for (int i = (1); i <= (n); ++i) { n     int z = check(i); n     if (z) ans[++m] = std::make_pair(z, i); n   } n   sort(ans + 1, ans + m + 1); n   cout << m << endl; n   for (int i = (1); i <= (m); ++i) n     printf( %d %d n , ans[i].first, ans[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n int n, m, dis[55][55], fast[55], slow[55], g1, g2, s1, s2; n long long f[55][55][55]; n bool cang[55], cans[55], canb[55]; n long long work(int g, int b, int su, int sd) { n   memset(f, 0, sizeof(f)); n   memset(cang, false, sizeof(cang)); n   memset(cans, false, sizeof(cans)); n   memset(canb, false, sizeof(canb)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) { n         continue; n       } n       if (dis[i][j] <= g) { n         cang[i] = true; n       } n       if (dis[i][j] > su && dis[i][j] < sd) { n         cans[i] = true; n       } n       if (dis[i][j] >= b) { n         canb[i] = true; n       } n     } n   } n   f[0][0][0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       for (int k = 0; j + k <= i; k++) { n         if (cang[i]) { n           f[i][j + 1][k] += f[i - 1][j][k]; n         } n         if (cans[i]) { n           f[i][j][k + 1] += f[i - 1][j][k]; n         } n         if (canb[i]) { n           f[i][j][k] += f[i - 1][j][k]; n         } n       } n     } n   } n   long long res = 0; n   for (int i = g1; i <= g2; i++) { n     for (int j = s1; j <= s2; j++) { n       res += f[n][i][j]; n     } n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i < n; i++) { n     for (int j = i + 1; j <= n; j++) { n       dis[i][j] = dis[j][i] = inf; n     } n   } n   for (int i = 0; i < m; i++) { n     int x, y, w; n     scanf( %d%d%d , &x, &y, &w); n     dis[x][y] = dis[y][x] = w; n   } n   scanf( %d%d%d%d , &g1, &g2, &s1, &s2); n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (dis[i][j] < inf) { n         dis[i][j] = dis[i][j] * n + i; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     fast[i] = inf; n     slow[i] = -inf; n     for (int j = 1; j <= n; j++) { n       if (i == j) { n         continue; n       } n       fast[i] = min(fast[i], dis[i][j]); n       slow[i] = max(slow[i], dis[i][j]); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       ans += work(fast[i], slow[j], fast[i], slow[j]) - n              work(fast[i] - 1, slow[j], fast[i], slow[j]) - n              work(fast[i], slow[j] + 1, fast[i], slow[j]) + n              work(fast[i] - 1, slow[j] + 1, fast[i], slow[j]); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n stack<int> S; n int n, m, q, x, y; n struct rs_tree { n   int hed[200005], ver[600005], nxt[600005], tot, cnt; n   int dep[200005], fa[200005], top[200005], siz[200005], son[200005]; n   void push(int x, int y) { n     ver[++tot] = y; n     nxt[tot] = hed[x]; n     hed[x] = tot; n   } n   void dfs1(int now, int en) { n     dep[now] = dep[en] + 1; n     fa[now] = en; n     for (int i = hed[now]; i; i = nxt[i]) { n       if (ver[i] == en) continue; n       dfs1(ver[i], now); n       if (son[now] == 0 || siz[son[now]] < siz[ver[i]]) son[now] = ver[i]; n       siz[now] += siz[ver[i]]; n     } n     return; n   } n   void dfs2(int now, int TOP) { n     top[now] = TOP; n     if (son[now]) dfs2(son[now], TOP); n     for (int i = hed[now]; i; i = nxt[i]) { n       if (ver[i] == fa[now] || ver[i] == son[now]) continue; n       dfs2(ver[i], ver[i]); n     } n     return; n   } n   int find_lca(int x, int y) { n     int sum = dep[x] + dep[y]; n     while (top[x] != top[y]) { n       if (dep[top[x]] < dep[top[y]]) swap(x, y); n       x = fa[top[x]]; n     } n     if (dep[x] > dep[y]) swap(x, y); n     sum -= (2 * dep[x] - 1); n     return sum >> 1; n   } n } RST; n struct graph { n   int hed[100005], ver[1000005], nxt[1000005], tot; n   int dfn[100005], low[100005], num, cnt, vis[100005]; n   void push(int x, int y) { n     ver[++tot] = y; n     nxt[tot] = hed[x]; n     hed[x] = tot; n   } n   void dfs(int now, int en) { n     S.push(now); n     dfn[now] = low[now] = ++cnt; n     vis[now] = 1; n     for (int i = hed[now]; i; i = nxt[i]) { n       if (!dfn[ver[i]]) { n         dfs(ver[i], now); n         low[now] = min(low[now], low[ver[i]]); n         if (low[ver[i]] >= dfn[now]) { n           num++; n           int z; n           do { n             z = S.top(); n             S.pop(); n             RST.push(z, num); n             RST.push(num, z); n             vis[z] = 0; n           } while (z != ver[i]); n           RST.push(now, num); n           RST.push(num, now); n         } n       } else if (vis[ver[i]]) n         low[now] = min(low[now], dfn[ver[i]]); n     } n   } n   void tarjan() { n     num = n; n     cnt = 0; n     dfs(1, 0); n   } n } G; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     G.push(x, y); n     G.push(y, x); n   } n   G.tarjan(); n   RST.dfs1(1, 0); n   RST.dfs2(1, 1); n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &x, &y); n     printf( %d n , RST.find_lca(x, y)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, m, n, p, q, r, ans, l, ch = 1; n string s; n int f; n vector<long long> v; n int main() { n   cin >> s; n   l = s.size(); n   f = s.find( bear , 0); n   while (f >= 0) { n     p = f + 1; n     ch = abs(p - ch) + 1; n     q = p + 3; n     r = (l - q) + 1; n     ans += (r * ch); n     ch = p + 1; n     f = s.find( bear , ch); n   } n   cout << ans; n } n