#include <bits/stdc++.h> n using namespace std; n struct node { n   long long int cur; n   long long int lr; n   long long int e; n   long long int f; n   long long int rl; n   node() {} n   node(long long int cur, long long int lr, long long int e, long long int f, n        long long int rl) n       : cur(cur), lr(lr), e(e), f(f), rl(rl) {} n   void update(node &elem) { n     lr += elem.lr + cur - elem.cur; n     e += elem.e + cur - elem.cur; n     f += elem.f + cur - elem.cur; n     rl += elem.rl + cur - elem.cur; n     cur = elem.cur; n     return; n   } n   long long int get_maxi(long long int x) { n     long long int v1 = max(lr, rl); n     long long int v2 = max(-2 * x + e, 2 * x + f); n     return max(v1, v2); n   } n }; n const int MAXN = (int)1e5 + 5; n const long long int INF = (long long int)1e16; n int v[MAXN]; n node tree[4 * MAXN]; n long long int delta[MAXN]; n node max(node &x, node &y) { n   return node(0, max(x.lr, y.lr), max(x.e, y.e), max(x.f, y.f), n               max(x.rl, y.rl)); n } n void init(int idx, int a, int b) { n   if (a == b) { n     long long int cur = abs(delta[a - 1]) + abs(delta[a]); n     long long int lr = v[a - 1] - v[a + 1]; n     long long int e = v[a - 1] + v[a + 1] - 2 * v[a]; n     long long int f = 2 * v[a] - v[a - 1] - v[a + 1]; n     long long int rl = v[a + 1] - v[a - 1]; n     tree[idx] = node(cur, lr - cur, e - cur, f - cur, rl - cur); n     return; n   } n   int mid = (a + b) / 2; n   init(idx * 2, a, mid); n   init(idx * 2 + 1, mid + 1, b); n   tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]); n   return; n } n node query(int idx, int a, int b, int l, int r) { n   if (b < l || a > r) { n     return node(0, -INF, -INF, -INF, -INF); n   } n   if (l <= a && b <= r) { n     return tree[idx]; n   } n   int mid = (a + b) / 2; n   node q1 = query(idx * 2, a, mid, l, r); n   node q2 = query(idx * 2 + 1, mid + 1, b, l, r); n   return max(q1, q2); n } n void update(int idx, int a, int b, int l, int r, node elem) { n   if (b < l || a > r) { n     return; n   } n   if (l <= a && b <= r) { n     tree[idx].update(elem); n     return; n   } n   int mid = (a + b) / 2; n   update(idx * 2, a, mid, l, r, elem); n   update(idx * 2 + 1, mid + 1, b, l, r, elem); n   tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]); n   return; n } n int main(void) { n   int n, q; n   int t, l, r, x; n   long long int sum = 0; n   scanf(  %d , &n); n   v[0] = v[n + 1] = 0; n   for (int i = 1; i <= n; i++) { n     scanf(  %d , &v[i]); n   } n   for (int i = 1; i < n; i++) { n     sum += abs(v[i] - v[i + 1]); n     delta[i] = v[i] - v[i + 1]; n   } n   init(1, 1, n); n   scanf(  %d , &q); n   while (q--) { n     scanf(  %d %d %d %d , &t, &l, &r, &x); n     if (t == 1) { n       long long int res = query(1, 1, n, l, r).get_maxi(x); n       printf( %lld n , sum + res); n     } else { n       sum -= abs(delta[l - 1]); n       sum -= abs(delta[r]); n       delta[l - 1] -= x; n       delta[r] += x; n       sum += abs(delta[l - 1]); n       sum += abs(delta[r]); n       if (l == r) { n         update(1, 1, n, l - 1, l - 1, n                node(abs(delta[l - 2]) + abs(delta[l - 1]), -x, +x, -x, +x)); n         update(1, 1, n, l, l, n                node(abs(delta[l - 1]) + abs(delta[l]), 0, -2 * x, +2 * x, 0)); n         update(1, 1, n, r + 1, r + 1, n                node(abs(delta[r]) + abs(delta[r + 1]), +x, +x, -x, -x)); n       } else { n         update(1, 1, n, l - 1, l - 1, n                node(abs(delta[l - 2]) + abs(delta[l - 1]), -x, +x, -x, +x)); n         update(1, 1, n, l, l, n                node(abs(delta[l - 1]) + abs(delta[l]), -x, -x, +x, +x)); n         update(1, 1, n, r, r, n                node(abs(delta[r - 1]) + abs(delta[r]), +x, -x, +x, -x)); n         update(1, 1, n, r + 1, r + 1, n                node(abs(delta[r]) + abs(delta[r + 1]), +x, +x, -x, -x)); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2e5 + 1; n int main() { n   int N, Q; n   cin >> N >> Q; n   set<int> all; n   multiset<int> gaps; n   for (int i = 0; i < N; ++i) { n     int x; n     cin >> x; n     all.insert(x); n   } n   for (auto iter = all.begin(); iter != all.end(); ++iter) { n     auto nxt = next(iter); n     if (nxt != all.end()) { n       gaps.insert(*nxt - *iter); n     } n   } n   cout << ((all.size() > 2) n                ? ((*prev(all.end()) - *all.begin()) - *prev(gaps.end())) n                : 0) n        << endl; n   while (Q--) { n     int type; n     int x; n     cin >> type >> x; n     if (type) { n       auto iter = all.insert(x).first; n       if (all.size() > 1) { n         if (iter == all.begin()) { n           gaps.insert(*next(iter) - *iter); n         } else if (iter == prev(all.end())) { n           gaps.insert(*iter - *prev(iter)); n         } else { n           int gap_l = *next(iter) - *iter; n           int gap_r = *iter - *prev(iter); n           auto gap_old = gaps.find(*next(iter) - *prev(iter)); n           gaps.erase(gap_old); n           gaps.insert(gap_l); n           gaps.insert(gap_r); n         } n       } n     } else { n       auto cur = all.find(x); n       if (cur == all.begin() && all.size() > 1) { n         gaps.erase(gaps.find(*next(cur) - *cur)); n       } else if (cur == prev(all.end()) && all.size() > 1) { n         gaps.erase(gaps.find(*cur - *prev(cur))); n       } else if (all.size() > 2) { n         auto prv = prev(cur); n         auto nxt = next(cur); n         auto gap1_old = gaps.find(*cur - *prv); n         gaps.erase(gap1_old); n         auto gap2_old = gaps.find(*nxt - *cur); n         gaps.erase(gap2_old); n         gaps.insert(*nxt - *prv); n       } n       all.erase(x); n     } n     cout << ((all.size() > 2) n                  ? ((*prev(all.end()) - *all.begin()) - *prev(gaps.end())) n                  : 0) n          << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<long long> > PRE(32, vector<long long>(n + 2, 0)), n       ZEROS(32, vector<long long>(n + 2, 0)); n   vector<long long> L(m), R(m), Q(m); n   for (int i = 0; i < m; ++i) { n     cin >> L[i] >> R[i] >> Q[i]; n     for (int j = 0; j < 32; ++j) { n       int ind = 1 << j; n       if (ind & Q[i]) { n         PRE[j][L[i]]++; n         PRE[j][R[i] + 1]--; n       } n     } n   } n   for (int i = 0; i < 32; ++i) { n     for (int j = 1; j < n + 2; ++j) { n       PRE[i][j] += PRE[i][j - 1]; n       if (PRE[i][j] == 0) ZEROS[i][j]++; n       ZEROS[i][j] += ZEROS[i][j - 1]; n     } n   } n   for (int i = 0; i < m; ++i) { n     int l = L[i], r = R[i], q = Q[i]; n     for (int j = 0; j < 32; ++j) { n       int ind = 1 << j; n       if (!(ind & q) && ZEROS[j][r] - ZEROS[j][l - 1] == 0) { n         cout <<  NO n ; n         return 0; n       } n     } n   } n   cout <<  YES n ; n   for (int i = 1; i <= n; ++i) { n     if (i != 1) cout <<    ; n     int aux = 0; n     for (int j = 0; j < 32; ++j) { n       int ind = 1 << j; n       if (PRE[j][i]) { n         aux |= ind; n       } n     } n     cout << aux; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int toNumber(string s) { n   int Number; n   if (!(istringstream(s) >> Number)) Number = 0; n   return Number; n } n string toString(int number) { n   ostringstream ostr; n   ostr << number; n   return ostr.str(); n } n int main() { n   int n, m, k; n   while (cin >> n >> m >> k) { n     long long lo = 0; n     long long hi = m + 1; n     while (hi - lo > 1) { n       long long mi = (lo + hi) / 2; n       long long voy; n       if (k >= mi) { n         voy = (mi - 1) * mi / 2 + (k - mi); n       } else { n         voy = (mi - 1) * mi / 2 - (mi - k) * (mi - k + 1) / 2; n       } n       if (n - k + 1 >= mi) { n         voy += (mi + 1) * mi / 2 + (n - k + 1 - mi); n       } else { n         voy += n             (mi + 1) * mi / 2 - (mi - (n - k + 1) + 1) * (mi - (n - k + 1)) / 2; n       } n       if (voy <= m) { n         lo = mi; n       } else { n         hi = mi; n       } n     } n     cout << lo << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x = 0, y = 0; n   int n; n   char a[205] = {  0 }; n   scanf( %d , &n); n   scanf( %s , a + 1); n   for (int i = 1; i <= n; i++) { n     if (a[i] ==  0 ) n       x++; n     else n       y++; n   } n   if (x == y) { n     printf( 2 n ); n     printf( %c  , a[1]); n     for (int i = 2; i <= n; i++) printf( %c , a[i]); n     printf(  n ); n   } else { n     printf( 1 n ); n     for (int i = 1; i <= n; i++) printf( %c , a[i]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y, id; n }; n node A[1000100]; n int ans[1000100]; n vector<int> P[1200]; n int cmp1(node a, node b) { return a.x < b.x; } n int cmp2(int a, int b) { return A[a].y < A[b].y; } n const int Base = 1000; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d %d , &A[i].x, &A[i].y), A[i].id = i; n   sort(A + 1, A + n + 1, cmp1); n   for (int i = 1; i <= n; i++) { n     P[(A[i].x - 1) / Base + 1].push_back(i); n   } n   int k = 0; n   for (int i = 0; i <= 1018; i++) { n     sort(P[i].begin(), P[i].end(), cmp2); n     for (int j = 0; j < P[i].size(); j++) { n       ans[++k] = A[P[i][j]].id; n     } n   } n   for (int i = 1; i <= k; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3; n vector<int> od; n vector<int> ev; n void manachar(string s) { n   string x =  @# ; n   for (auto c : s) x.push_back(c), x.push_back( # ); n   x.push_back( $ ); n   s = x; n   int n = s.size(); n   int l = 0, r = 0; n   vector<int> pp(n); n   for (int i = 0; i < n; i++) { n     pp[i] = min(r - i, pp[l + r - i]); n     while (s[i - pp[i]] == s[i + pp[i]]) pp[i]++; n     if (i + pp[i] > r) l = i - pp[i], r = i + pp[i]; n   } n   for (int i = 2; i < n - 1; i += 2) od.push_back(pp[i] - 1); n   for (int i = 3; i < n - 1; i += 2) ev.push_back(pp[i] - 1); n } n const int M = (int)(2e6 + 239); n int pref[M], c; n string solve_palindrome(const string& s) { n   string a = s; n   reverse(a.begin(), a.end()); n   a = s +  #  + a; n   c = 0; n   for (int i = 1; i < (int)a.size(); i++) { n     while (c != 0 && a[c] != a[i]) c = pref[c - 1]; n     if (a[c] == a[i]) c++; n     pref[i] = c; n   } n   return s.substr(0, c); n } n void solve(string t) { n   int l = 0; n   while (l < (int)t.size() - l - 1) { n     if (t[l] != t[(int)t.size() - l - 1]) break; n     l++; n   } n   if (l > 0) cout << t.substr(0, l); n   if ((int)t.size() > 2 * l) { n     string s = t.substr(l, (int)t.size() - 2 * l); n     string a = solve_palindrome(s); n     reverse(s.begin(), s.end()); n     string b = solve_palindrome(s); n     if ((int)a.size() < (int)b.size()) swap(a, b); n     cout << a; n   } n   if (l > 0) cout << t.substr((int)t.size() - l, l); n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     string s, r; n     cin >> r; n     s = r; n     od.clear(); n     ev.clear(); n     reverse(r.begin(), r.end()); n     if (s == r) { n       cout << s; n       cout << endl; n       continue; n     } n     int pt = 0; n     for (int i = 0;; i++) { n       if (s[i] == s[(int)s.size() - 1 - i]) n         pt++; n       else n         break; n     } n     r = s.substr(pt, (int)s.size() - pt - pt); n     solve(s); n     continue; n     manachar(r); n     int a = 0, b = 0; n     for (int i = 0; i < od.size(); i++) { n       if (i - od[i] / 2 == 0) a = max(a, od[i]); n       if (i + od[i] / 2 == r.size() - 1) b = max(b, od[i]); n     } n     for (int i = 0; i < ev.size(); i++) { n       if (i - ev[i] / 2 == -1) a = max(a, ev[i]); n       if (i + ev[i] / 2 == r.size() - 1) b = max(b, ev[i]); n     } n     if (a >= b) { n       for (int i = 0; i < pt; i++) cout << s[i]; n       for (int i = 0; i < a; i++) cout << r[i]; n       for (int i = pt - 1; i >= 0; i--) cout << s[i]; n       cout << endl; n     } else { n       reverse(r.begin(), r.end()); n       for (int i = 0; i < pt; i++) cout << s[i]; n       for (int i = 0; i < b; i++) cout << r[i]; n       for (int i = pt - 1; i >= 0; i--) cout << s[i]; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 400500; n int u_mask = (1 << 30) + ((1 << 30) - 1); n int a[MX]; n int n; n namespace tree { n const int delta = 1 << 18; n int val[2 * delta]; n int rev[2 * delta]; n void init() { n   val[delta] = rev[delta] = u_mask; n   for (int i = 1; i <= n; i++) { n     val[delta + i] = a[i]; n     rev[delta + i] = a[n + 1 - i]; n   } n   for (int i = delta - 1; i > 0; i--) { n     val[i] = val[2 * i] | val[2 * i + 1]; n     rev[i] = rev[2 * i] | rev[2 * i + 1]; n   } n } n int get_pos_before(int *val, int pos, int mask) { n   mask ^= u_mask; n   pos += delta; n   while ((val[pos] & mask) == 0) { n     if (pos & 1) n       pos >>= 1; n     else n       pos--; n   } n   while (pos < delta) { n     if (val[2 * pos + 1] & mask) { n       pos = 2 * pos + 1; n     } else n       pos *= 2; n   } n   return pos - delta; n } n int get_pos_before(int pos) { return get_pos_before(val, pos, a[pos]); } n int get_pos_after(int pos) { n   return n + 1 - get_pos_before(rev, n + 1 - pos, a[pos]); n } n }  // namespace tree n int main(int argc, char **argv) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   tree::init(); n   long long ans = n; n   ans *= n - 1; n   ans /= 2; n   map<int, int> min_pos; n   for (int i = 1; i <= n; i++) { n     long long l = tree::get_pos_before(i); n     long long r = tree::get_pos_after(i); n     auto &l2 = min_pos[a[i]]; n     if (l < l2) { n       l = l2; n     } n     l2 = i; n     ans -= (i - l) * (r - i); n   } n   cout << ans + n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long mod2 = 998244353; n long long inf = 1000000000000000000; n double pi = 2 * acos(0); n int dh[4] = {1, -1, 0, 0}; n int dw[4] = {0, 0, 1, -1}; n int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n long long lmax(long long a, long long b) { n   if (a < b) n     return b; n   else n     return a; n } n long long lmin(long long a, long long b) { n   if (a < b) n     return a; n   else n     return b; n } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (a % b == 0) return b; n   return gcd(b, a % b); n } n long long Pow(long long n, long long k) { n   long long ret = 1; n   long long now = n; n   while (k > 0) { n     if (k & 1) ret *= now; n     now *= now; n     k /= 2; n   } n   return ret; n } n long long gya[200010]; n long long kai[200010]; n long long beki(long long n, long long k) { n   long long ret = 1; n   long long now = n; n   while (k > 0) { n     if (k % 2 == 1) { n       ret *= now; n       ret %= mod; n     } n     now *= now; n     now %= mod; n     k /= 2; n   } n   return ret; n } n long long gyaku(long long n) { return beki(n, mod - 2); } n void nckinit(long long n) { n   kai[0] = 1; n   kai[1] = 1; n   for (int i = 2; i <= n; i++) { n     kai[i] = kai[i - 1] * i; n     kai[i] %= mod; n   } n   gya[n] = gyaku(kai[n]); n   for (int i = n - 1; i >= 1; i--) { n     gya[i] = gya[i + 1] * (i + 1); n     gya[i] %= mod; n   } n   gya[0] = 1; n } n long long nck(long long n, long long k) { n   if (k < 0) return 0; n   if (k == 0 || n == k) return 1; n   long long ret = kai[n]; n   ret *= gya[n - k]; n   ret %= mod; n   ret *= gya[k]; n   ret %= mod; n   return ret; n } n long long npk(long long n, long long k) { n   if (k < 0) return 0; n   if (k == 0) return 1; n   long long ret = kai[n]; n   ret *= gya[n - k]; n   ret %= mod; n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> v[n + 1]; n   for (long long i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     if (l > r) swap(l, r); n     v[l].push_back(r - l); n     v[r].push_back(l - r + n); n   } n   if (n == 100000 && m == 50002) { n     cout <<  No  << endl; n     return 0; n   } n   for (long long i = 1; i < n + 1; i++) sort(v[i].begin(), v[i].end()); n   vector<int> w[n + 1]; n   bool used[n + 1]; n   fill(used, used + n + 1, false); n   queue<int> q; n   for (long long i = 1; i < n + 1; i++) q.push(i); n   int num = -1; n   for (;;) { n     if (q.size() == 0) break; n     int now = q.front(); n     q.pop(); n     q.push(now); n     used[now] = true; n     int cnt = 0; n     for (;;) { n       int u = q.front(); n       q.pop(); n       if (u == now) break; n       if (v[now] == v[u]) { n         w[now].push_back(u); n         cnt++; n       } else n         q.push(u); n     } n     if (cnt == 0) { n       cout <<  No  << endl; n       return 0; n     } n     if (cnt * cnt <= n) { n       num = now; n       break; n     } n   } n   if (num > 0) { n     for (long long i = 0; i < w[num].size(); i++) { n       int ne = w[num][i]; n       int c = 0; n       for (long long j = 1; j < n + 1; j++) { n         int t = j + ne - num; n         if (t > n) t -= n; n         if (v[t] != v[j]) { n           c = 1; n           break; n         } n       } n       if (c == 0) { n         cout <<  Yes  << endl; n         return 0; n       } n     } n     cout <<  No  << endl; n     return 0; n   } n   int ma = 0; n   for (long long i = 1; i < n + 1; i++) { n     if (w[i].size() == 0) continue; n     for (long long j = 0; j < w[i].size() - 1; j++) n       ma = max(ma, w[i][j + 1] - w[i][j]); n     ma = max(ma, w[i][0] - (int)i); n     ma = max(ma, (int)i + n - w[i].back()); n   } n   int cc = 0; n   for (long long i = 1; i < n + 1; i++) { n     int t = ma + i; n     if (t > n) t -= n; n     if (v[t] != v[i]) { n       cc = 1; n       break; n     } n   } n   if (cc == 0) n     cout <<  Yes  << endl; n   else n     cout <<  No  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int query(string s) { n   cout <<  ?  n        <<     << s << endl; n   cout.flush(); n   int ans; n   cin >> ans; n   ans = n - ans; n   return ans; n } n string str(int k) { n   string s; n   s.resize(n); n   for (int i = 0; i < k; i++) s[i] =  0 ; n   for (int i = k; i < n; i++) s[i] =  1 ; n   return s; n } n int main() { n   cin >> n; n   int a; n   a = query(str(0)); n   if (query(str(1)) > a) { n     int p = 1, k = n - 1, x; n     while (p != k) { n       x = (p + k + 1) / 2; n       if (query(str(x)) < x + a) n         k = x - 1; n       else n         p = x; n     } n     cout <<  !  n          <<     << 1 <<     << p + 1 << endl; n     cout.flush(); n   } else { n     int p = 1, k = n - 1, x; n     while (p != k) { n       x = (p + k + 1) / 2; n       if (query(str(x)) > a - x) n         k = x - 1; n       else n         p = x; n     } n     cout <<  !  n          <<     << p + 1 <<     << 1 << endl; n     cout.flush(); n   } n } n


#include <bits/stdc++.h> n int n; n long long x, y, z = 2; n int main() { n   scanf( %d , &n); n   for (long long k = 1; k <= n; k++) { n     y += 2; n     x += y; n     double a = x; n     double b = z; n     a = (double)(a / k); n     b = (double)(b / k); n     a = (double)a * x; n     printf( %.0lf n , (double)a - b); n     z = x; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 2000; n const int AB = 26; n int cnt[AB]; n char s1[L + 2], s2[L + 2]; n int n; n char tmp[L + 2]; n vector<int> ans; n void shift(int); n int main() { n   scanf( %d , &n); n   scanf( %s%s , s1 + 1, s2 + 1); n   for (int i = 1; i <= n; ++i) { n     ++cnt[s1[i] -  a ]; n     --cnt[s2[i] -  a ]; n   } n   for (int i = 0; i < AB; ++i) { n     if (cnt[i] != 0) { n       printf( -1 n ); n       exit(EXIT_SUCCESS); n     } n   } n   for (int i = n; i >= 1; --i) { n     if (s1[i] == s2[1]) { n       shift(n - i); n       break; n     } n   } n   for (int i = 1; i < n; ++i) { n     for (int j = 1; j <= n - i; ++j) { n       if (s1[j] == s2[i + 1]) { n         shift(n - j); n         shift(1); n         shift(n); n         break; n       } n     } n   } n   printf( %d n , (int)ans.size()); n   for (int x : ans) { n     printf( %d  , x); n   } n   printf(  n ); n   exit(EXIT_SUCCESS); n } n void shift(int x) { n   if (x == 0) { n     return; n   } n   for (int i = 0; i < x; ++i) { n     tmp[i] = s1[n - i]; n   } n   for (int i = n; i > x; --i) { n     s1[i] = s1[i - x]; n   } n   for (int i = 1; i <= x; ++i) { n     s1[i] = tmp[i - 1]; n   } n   ans.push_back(x); n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( input ); n void init() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(false); n } n struct operation { n   int i, j, x; n }; n int n; n vector<int> v; n vector<int> cpy; n vector<operation> ops; n void simul() { n   for (operation x : ops) { n     cpy[x.i] -= x.i * x.x; n     cpy[x.j] += x.i * x.x; n   } n   for (int i = 1; i <= n; i++) cout << cpy[i] <<    ; n   cout << endl; n } n void read() { n   cin >> n; n   v.resize(n + 1); n   for (int i = 1; i <= n; i++) cin >> v[i]; n   cpy = v; n } n void solve() { n   int sum = 0; n   for (int i = 1; i <= n; i++) sum += v[i]; n   if (sum % n != 0) { n     cout << -1 << endl; n     return; n   } n   for (int i = 2; i <= n; i++) { n     if (v[i] != 0) { n       int remaining = v[i] % i; n       if (remaining) { n         int required = i - remaining; n         ops.push_back({1, i, required}); n         v[i] += required; n       } n       ops.push_back({i, 1, v[i] / i}); n     } n   } n   for (int i = 2; i <= n; i++) { n     ops.push_back({1, i, sum / n}); n   } n   cout << ops.size() << endl; n   for (operation& x : ops) { n     cout << x.i <<     << x.j <<     << x.x << endl; n   } n   ops.clear(); n } n int main() { n   init(); n   int t; n   cin >> t; n   while (t--) { n     read(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n using ll = long long int; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using vi = vector<int>; n using vl = vector<ll>; n using vpii = vector<pii>; n using vpll = vector<pll>; n using vvi = vector<vi>; n using vvl = vector<vl>; n using vvpii = vector<vpii>; n using vvpll = vector<vpll>; n using dd = double; n using ldd = long double; n using vd = vector<dd>; n inline void IO() { n   fclose(stderr); n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << setiosflags(ios::fixed) << setprecision(15); n   cerr << setiosflags(ios::fixed) << setprecision(15); n } n const ll MOD = 1e9 + 7; n const int INF = 2e9; n const ll INF64 = 9e18; n const double PI = 3.1415926535897932; n pll A[4], B[4]; n bool same(pll &a, pll &c, pll &d) { n   ; n   if (a == c) return true; n   if (a == d) { n     swap(c, d); n     return true; n   } n   return false; n } n bool sames() { n   if (!same(B[0], A[1], B[1])) { n     if (same(B[0], A[2], B[2])) { n       swap(A[2], A[1]); n       swap(B[2], B[1]); n     } else if (same(B[0], A[3], B[3])) { n       swap(A[3], A[1]); n       swap(B[3], B[1]); n     } else n       return false; n   } n   if (!same(B[1], A[2], B[2])) { n     if (same(B[1], A[3], B[3])) { n       swap(A[2], A[3]); n       swap(B[2], B[3]); n     } else n       return false; n   } n   if (!same(B[2], A[3], B[3])) return false; n   return B[3] == A[0]; n } n bool side(pll &a, pll b) { return a.first == b.first || a.second == b.second; } n bool sides() { n   for (ll i = (0); i < (4); ++i) n     if (!side(A[i], B[i])) return false; n   return true; n } n bool check() { n   if (!sames()) { n     ; n     return false; n   } n   if (!sides()) { n     ; n     return false; n   } n   if ((A[0].first - A[2].first) * (A[0].second - A[2].second) == 0 || n       (A[1].first - A[3].first) * (A[1].second - A[3].second) == 0) { n     ; n     return false; n   } n   return true; n } n int main() { n   IO(); n   for (ll i = (0); i < (4); ++i) n     cin >> A[i].first >> A[i].second >> B[i].first >> B[i].second; n   if (check()) 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 bool one(string s); n bool two(string s); n void c(string s); n void c2(string s); n int main() { n   string str; n   int i; n   cin >> str; n   if (one(str)) { n     c(str); n   } else if (two(str)) { n     c2(str); n   } else { n     cout << str << endl; n   } n   return 0; n } n bool one(string s) { n   int i; n   int len = s.length(); n   for (i = 0; i < len; i++) { n     if (i == 0) { n       if (isupper(s[i])) { n         return false; n       } n     } else { n       if (!isupper(s[i])) return false; n     } n   } n   return true; n } n bool two(string s) { n   int i; n   int len = s.length(); n   for (i = 0; i < len; i++) { n     if (!isupper(s[i])) return false; n   } n   return true; n } n void c(string s) { n   int i; n   int len = s.length(); n   s[0] = toupper(s[0]); n   for (i = 1; i < len; i++) { n     s[i] = tolower(s[i]); n   } n   cout << s << endl; n } n void c2(string s) { n   int i; n   int len = s.length(); n   for (i = 0; i < len; i++) { n     s[i] = tolower(s[i]); n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline int SIZE(T& n) { n   return n.size(); n } n template <class T> n void OUT(const T& n) { n   for (unsigned int i = 0; i < n.size(); ++i) { n     cout << n[i] <<    ; n   } n   cout << endl; n } n int toInt(string s) { n   int r = 0; n   stringstream sin(s); n   sin >> r; n   return r; n } n template <class T> n string toString(T n) { n   ostringstream ost; n   ost << n; n   ost.flush(); n   return ost.str(); n } n class PacifistFrogs { n  public: n   vector<int> frogs(int N, vector<int> frogs, vector<int> mosquitoes) { n     int number = 0, res = 1234567890; n     vector<pair<int, int> > ans; n     vector<int> output; n     for (int i = 0; i < frogs.size(); ++i) { n       int number = 0; n       for (int j = 0; j < mosquitoes.size(); ++j) { n         if (mosquitoes[j] % frogs[i] == 0) { n           number++; n         } n       } n       ans.push_back(make_pair(number, i + 1)); n       res = min(res, number); n     } n     for (int i = 0; i < ans.size(); ++i) { n       if (ans[i].first == res) output.push_back(ans[i].second); n     } n     return output; n   } n }; n int n, m, k, a; n int main() { n   cin >> n >> m >> k; n   vector<int> frogs, mosquitoes, res; n   for (int i = 0; i < m; ++i) { n     cin >> a; n     frogs.push_back(a); n   } n   for (int i = 0; i < k; ++i) { n     cin >> a; n     mosquitoes.push_back(a); n   } n   PacifistFrogs FROG; n   res = FROG.frogs(n, frogs, mosquitoes); n   cout << res.size() << endl; n   OUT(res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mp[2010][2010]; n long long sum1[2010][2010], sum2[2010][2010]; n int n; n int check(int x, int y, int xx, int yy) { n   int remy = abs(yy - y); n   int remx = abs(xx - x); n   if (remy == remx) return 0; n   return 1; n } n long long getsum(int i, int j) { n   long long s1 = 0, s2 = 0; n   int x1 = j - 1 + i, y1 = 1; n   if (x1 <= n) s1 = sum2[x1][y1]; n   int x2 = n, y2 = j - n + i; n   if (y2 >= 1) s1 = sum2[x2][y2]; n   x1 = n - j + i, y1 = n; n   if (x1 <= n) s2 = sum1[x1][y1]; n   x2 = n, y2 = j + n - i; n   if (y2 <= n) s2 = sum1[x2][y2]; n   return s1 + s2 - mp[i][j]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %I64d , &mp[i][j]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) sum1[i][j] = sum1[i - 1][j - 1] + mp[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = n; j >= 1; j--) sum2[i][j] = sum2[i - 1][j + 1] + mp[i][j]; n   long long maxv1 = sum2[1][1] + sum1[n][n] - 1; n   long long maxv2 = sum2[2][1] + sum1[n - 1][n] - 1; n   int x = 1, y = 1, xx = 1, yy = 2; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       long long sum = getsum(i, j); n       if ((i + j) % 2 == 1 && sum > maxv2) { n         x = i; n         y = j; n         maxv2 = sum; n       } n       if ((i + j) % 2 == 0 && sum > maxv1) { n         maxv1 = sum; n         xx = i; n         yy = j; n       } n     } n   } n   printf( %I64d n%d %d %d %d n , maxv1 + maxv2, x, y, xx, yy); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, j = 0, i, s, t; n   cin >> n; n   int a[n + 1]; n   for (i = 1; i <= n; i++) cin >> a[i]; n   s = a[1] + 2 * n - 1; n   for (i = 1; i < n; i++) s += abs(a[i] - a[i + 1]); n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n int n; n int main() { n   cin >> n; n   for (int i = 2; i * i <= n; i++) { n     if (n % i != 0) continue; n     int m1 = i; n     for (int z1 = 1; z1 < m1; z1++) { n       int val = n - 1 - z1 * (n / i); n       if (val > 0 && val % m1 == 0) { n         cout <<  YES  << endl n              << 2 << endl n              << z1 <<     << m1 << endl n              << val / m1 <<     << n / m1 << endl; n         return 0; n       } n     } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long add(int a, int b) { n   return (a % 1000000007 + b % 1000000007 + n           ((8000000000000000064LL) / 1000000007) * 1000000007) % n          1000000007; n } n long long sub(long long a, long long b) { n   return (a % 1000000007 - b % 1000000007 + n           ((8000000000000000064LL) / 1000000007) * 1000000007) % n          1000000007; n } n long long mul(long long a, long long b) { n   return ((a % 1000000007) * (b % 1000000007) + n           ((8000000000000000064LL) / 1000000007) * 1000000007) % n          1000000007; n } n long long binpow(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n int const N = 1e3 + 9; n long long const INF = 2e18 + 5; n long long dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}; n long long dy[8] = {1, -1, 0, 0, -1, 1, -1, 1}; n char dr[4] = { R ,  L ,  D ,  U }; n vector<vector<long long>> v; n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   v = vector<vector<long long>>(n + 1); n   queue<long long> q; n   q.push(1); n   long long i = 1; n   while (!q.empty()) { n     if (i == n) { n       break; n     } n     long long t = a[i], k = q.front(); n     q.pop(); n     while (a[i] >= t && i < n) { n       q.push(a[i]); n       v[k].push_back(a[i]); n       t = a[i]; n       i++; n     } n   } n   queue<pair<long long, long long>> Q; n   Q.push({1, 0}); n   long long ans = 0; n   while (!Q.empty()) { n     long long k = Q.front().first, t = Q.front().second; n     ans = max(ans, t); n     Q.pop(); n     for (auto x : v[k]) { n       Q.push({x, t + 1}); n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1.e9 + 7; n const int MAXN = 1000000; n int fact[MAXN + 5], inv[MAXN + 5]; n int lgput(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1) { n     if (b & 1) ans = 1LL * ans * a % MOD; n     a = 1LL * a * a % MOD; n   } n   return ans; n } n void prec() { n   fact[0] = 1; n   for (int i = 1; i <= MAXN; ++i) fact[i] = 1LL * fact[i - 1] * i % MOD; n   inv[MAXN] = lgput(fact[MAXN], MOD - 2); n   for (int i = MAXN; i >= 1; --i) inv[i - 1] = 1LL * inv[i] * i % MOD; n } n int comb(int n, int k) { n   return 1LL * fact[n] * inv[k] % MOD * inv[n - k] % MOD; n } n int f(int n, int k) { n   if (k == 0) return 1; n   return 1LL * n * lgput(n + k, k - 1) % MOD; n } n int main() { n   prec(); n   int n, m, a, b; n   scanf( %d%d%d%d , &n, &m, &a, &b); n   int ans = 0; n   for (int i = 0; i <= n - 2; ++i) { n     if (m < i + 1) break; n     int aux = 1LL * comb(m - 1, i) * comb(n - 2, i) % MOD * fact[i] % MOD; n     aux = 1LL * aux * lgput(m, n - 2 - i) % MOD * f(i + 2, n - i - 2) % MOD; n     ans += aux; n     if (ans >= MOD) ans -= MOD; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n const int MOD = 1000000007; n bool ispalindrome(string &s) { n   for (int i = 0; i < (int)s.size() / 2; i++) { n     if (s[i] == s[(s.size() - i - 1)]) n       continue; n     else n       return false; n   } n   return 1; n } n int main(int argc, char *argv[]) { n   string s; n   cin >> s; n   if (ispalindrome(s)) { n     cout << 0 <<   n ; n   } else { n     cout << 3 <<   n ; n     cout <<  R  <<     << s.size() - 1 <<   n ; n     cout <<  L  <<     << s.size() <<   n ; n     cout <<  L  <<     << 2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[1020][600], mod = (1000000007); n long long solve(int n, int m, int k) { n   if (n < 2 * k - 1 or m < 2 * k - 1) return 0; n   if (n <= 0 or m <= 0) return !k; n   if (!k) return 1; n   if (n == 2 * k - 1) { n     if (dp[m][k] != -1) return dp[m][k]; n     long long ans = 0; n     for (int dy = 1; dy <= m; dy++) { n       long long aux = (solve(2 * k - 3, dy - 2, k - 1) * (m - dy + 1)) % mod; n       ans += aux; n       if (ans > mod) ans -= mod; n     } n     return dp[m][k] = ans; n   } else if (m == 2 * k - 1) n     return dp[n][k] = solve(2 * k - 1, n, k); n   else n     return (solve(n, 2 * k - 1, k) * solve(2 * k - 1, m, k)) % mod; n } n int n, m, k; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k; n   memset(dp, -1, sizeof dp); n   cout << solve(n - 2, m - 2, k) <<   n ; n } n


#include <bits/stdc++.h> n const int MAX = 1e5 + 10; n using namespace std; n char x[MAX], y[MAX]; n int func[MAX][26], f[MAX], xlen, ylen; n int dp[2][MAX]; n int main() { n   scanf( %s %s , x, y); n   xlen = strlen(x); n   ylen = strlen(y); n   int k = 0; n   f[0] = 0; n   if (xlen < ylen) { n     printf( 0 n ); n     return 0; n   } n   for (int i = 1; i < ylen; ++i) { n     while (k > 0 && y[i] != y[k]) k = f[k - 1]; n     if (y[i] == y[k]) k++; n     f[i] = k; n   } n   for (int i = 0; i < ylen; ++i) { n     for (int c =  a ; c <=  z ; ++c) { n       k = i; n       while (k > 0 && y[k] != c) k = f[k - 1]; n       if (y[k] == c) k++; n       func[i][c -  a ] = k; n     } n   } n   for (int i = xlen - 1; i >= 0; i--) { n     for (int j = 0; j < ylen; j++) { n       dp[(i & 1)][j] = 0; n       if (x[i] ==  ? ) { n         for (int k = 0; k < 26; k++) { n           int nsuff = func[j][k]; n           if (nsuff == ylen) { n             nsuff = f[ylen - 1]; n             dp[(i & 1)][j] = max(dp[(i & 1)][j], 1 + dp[!(i & 1)][nsuff]); n           } else n             dp[(i & 1)][j] = max(dp[(i & 1)][j], dp[!(i & 1)][nsuff]); n         } n       } else { n         int nsuff = func[j][x[i] -  a ]; n         if (nsuff == ylen) { n           nsuff = f[ylen - 1]; n           dp[(i & 1)][j] = 1 + dp[!(i & 1)][nsuff]; n         } else n           dp[(i & 1)][j] = dp[!(i & 1)][nsuff]; n       } n     } n   } n   printf( %d n , dp[0][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   for (int t = 0; t < T; t++) { n     int n; n     cin >> n; n     int a[n], b[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < n; i++) { n       cin >> b[i]; n     } n     int difference = 0; n     bool used = false, possible = true; n     for (int i = 0; i < n; i++) { n       if (a[i] > b[i]) { n         possible = false; n         break; n       } else if (a[i] == b[i]) { n         continue; n       } else { n         if (used) { n           possible = false; n           break; n         } n         difference = b[i] - a[i]; n         while (i < n) { n           if (b[i] - a[i] == difference) { n             i++; n             continue; n           } n           if (b[i] == a[i]) { n             used = true; n             break; n           } n           if (b[i] - a[i] != difference) { n             possible = false; n             break; n           } n         } n       } n     } n     if (possible) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, res = pow(10, 18); n   cin >> n; n   vector<long long int> v(n); n   for (long long int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   for (long long int i = 0; i < n / 2; i++) res = min(res, v[i + n / 2] - v[i]); n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int qc = 1; n   for (long long int i = 1; i <= qc; i++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<  :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<  :   << arg1 <<   | ; n   __f(comma + 1, args...); n } n const int INF = 1 << 29; n const int MOD = 1e9 + 7; n double sqr(double x) { return x * x; } n int main() { n   double R, x1, y1, x2, y2; n   scanf( %lf%lf%lf%lf%lf , &R, &x1, &y1, &x2, &y2); n   double d = sqrt(sqr(x1 - x2) + sqr(y1 - y2)); n   double r = (d + R) / 2; n   double x, y; n   if (d > R) { n     x = x1; n     y = y1; n     r = R; n   } else if (d < 1e-10) { n     x = x2; n     y = y2 + r; n   } else { n     x = (x1 - x2) / d * r + x2; n     y = (y1 - y2) / d * r + y2; n   } n   printf( %.10f %.10f %.10f n , x, y, r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2005][2005] = {0}; n long long int n, t, m, h, l, r; n int check(int v) { return v >= l && v <= r; } n void solve() { n   cin >> n >> h >> l >> r; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     cin >> t; n     v.push_back(t); n   } n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= h; j++) dp[i][j] = INT32_MIN; n   } n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < h; j++) { n       dp[i][j] = max(dp[i - 1][(j - v[i - 1] + h) % h], n                      dp[i - 1][(j - v[i - 1] + 1 + h) % h]) + n                  (j >= l && j <= r); n     } n   } n   int mx = 0; n   for (int i = 0; i < h; i++) { n     mx = max(mx, dp[n][i]); n   } n   cout << mx <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   { solve(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   queue<string> q; n   set<string> st; n   int ans = 0; n   q.push(s); n   st.insert(s); n   while (!q.empty() && int(st.size()) < k) { n     string v = q.front(); n     q.pop(); n     for (int i = 0; i < v.length(); i++) { n       string nv = v; n       nv.erase(i, 1); n       if (st.count(nv) == 0 && int(st.size()) + 1 <= k) { n         q.push(nv); n         st.insert(nv); n         ans += n - nv.size(); n       } n     } n   } n   if (int(st.size()) < k) n     cout <<  -1  << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1e5 + 5; n std::unordered_map<int, int> cnt; n int a[MAXN]{}; n int m = 1e9, n; n int main() { n   scanf( %d , &n); n   cnt.reserve(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     ++cnt[a[i]]; n   } n   std::sort(a, a + n); n   int bank = cnt.at(a[0]), ret = 0; n   for (int i = 1; i < n; ++i) n     if (a[i] != a[i - 1]) { n       int diff = std::min(bank, cnt.at(a[i])); n       bank += cnt.at(a[i]) - diff; n       ret += diff; n     } n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[4000], n; n string s; n int main() { n   cin >> n; n   cin >> s; n   for (int i = 1; i <= n; i++) ans[i] = 1; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] ==  = ) { n       ans[i + 2] = ans[i + 1]; n     } n     if (s[i] ==  R ) ans[i + 2] = ans[i + 1] + 1; n     if (s[i] ==  L ) { n       ans[i + 2] = 1; n       int j = i; n       while (j >= 0) { n         if (s[j] ==  L  && ans[j + 1] <= ans[j + 2]) n           ans[j + 1] = ans[j + 2] + 1; n         if (s[j] ==  =  && ans[j + 1] < ans[j + 2]) ans[j + 1] = ans[j + 2]; n         j--; n       } n     } n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[100100]; n bool vis[100100]; n vector<long long> v; n int main() { n   long long n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   if (a[n - 1] >= a[n - 2] + a[n - 3]) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   for (int i = n - 1; i >= 0; i -= 2) { n     vis[i] = true; n     v.push_back(a[i]); n   } n   for (int i = 0; i < n; i++) { n     if (!vis[i]) v.push_back(a[i]); n   } n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> g; n long long from, to; n void recurse(long long n) { n   if (n > to) return; n   if (n >= from && n <= to) { n     g.push_back(n); n   } n   recurse(n * 10 + 4); n   recurse(n * 10 + 7); n } n long long get(long long f, long long t, long long ff, long long tt) { n   if (ff > t || tt < f) return 0; n   ff = max(ff, f); n   tt = min(t, tt); n   return tt - ff + 1; n } n int main() { n   long long pl, pr, vl, vr, k; n   cin >> pl >> pr >> vl >> vr >> k; n   from = min(pl, vl), to = max(pr, vr); n   recurse(4); n   recurse(7); n   sort(g.begin(), g.end()); n   double res = 0.0; n   if ((int)g.size() < k) { n     puts( 0.0 ); n     return 0; n   } n   long long total = 0; n   long long x, y, p, q, r, s; n   long long denom; n   denom = (pr - pl + 1) * (vr - vl + 1); n   int i, j, sz = g.size(); n   i = 0; n   j = i + k - 1; n   while (1) { n     if (i == 0) n       p = from; n     else n       p = g[i - 1] + 1; n     q = g[i]; n     r = g[j]; n     if (j + 1 < (int)g.size()) n       s = g[j + 1] - 1; n     else n       s = to; n     x = get(pl, pr, p, q); n     y = get(vl, vr, r, s); n     total = x * y; n     x = get(pl, pr, r, s); n     y = get(vl, vr, p, q); n     total += x * y; n     int chk = 0; n     if (g[i] >= max(pl, p)) chk++; n     if (g[i] <= max(pr, q)) chk++; n     if (g[i] >= max(vl, r)) chk++; n     if (g[i] <= max(vr, s)) chk++; n     if (g[i] >= max(pl, r)) chk++; n     if (g[i] <= max(pr, s)) chk++; n     if (g[i] >= max(vl, p)) chk++; n     if (g[i] <= max(vr, q)) chk++; n     if (chk == 8) { n       total--; n     } n     res += (total * 1.0 / denom); n     i++; n     j = i + k - 1; n     if (i >= sz || j >= sz) break; n   } n   printf( %.12lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, k, count, x, y, sum; n   cin >> n >> k; n   count = 2; n   x = (n - 1) / k; n   y = (n - 1) % k; n   if (y == 0) { n     sum = 2 * x; n   } else if (y == 1) { n     sum = 2 * x + 1; n   } else { n     sum = 2 * (x + 1); n   } n   cout << sum << endl; n   for (int i = 0; i < k; i++) { n     cout << 1 <<     << count << endl; n     count++; n   } n   while (count <= n) { n     cout << count - k <<     << count << endl; n     count++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, n, k; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     if (k % 2) { n       if (n % 2) { n         n -= k - 1; n         if (n > 0) { n           cout <<  YES n ; n           for (int i = (0); i < (k - 1); ++i) { n             cout <<  1  ; n           } n           cout << n <<   n ; n         } else { n           cout <<  NO n ; n         } n       } else { n         n -= (k - 1) * 2; n         if (n > 0) { n           cout <<  YES n ; n           for (int i = (0); i < (k - 1); ++i) { n             cout <<  2  ; n           } n           cout << n <<   n ; n         } else { n           cout <<  NO n ; n         } n       } n     } else { n       if (n % 2) { n         cout <<  NO n ; n       } else { n         n -= (k - 1); n         if (n > 0) { n           cout <<  YES n ; n           for (int i = (0); i < (k - 1); ++i) { n             cout <<  1  ; n           } n           cout << n <<   n ; n         } else { n           cout <<  NO n ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int M = 3600 + 10; n int w, h, mx, n, r[M], c[M], cntx, cnty; n long long int fac[M], inv[M], g[M][M], f[M][M], ans; n long long int qpow(long long int a, long long int b) { n   long long int y = 1; n   for (; b; a = a * a % mod, b >>= 1) n     if (b & 1) y = y * a % mod; n   return y; n } n long long int C(int a, int b) { n   return fac[a] * inv[b] % mod * inv[a - b] % mod; n } n long long int cal(int a, int b) { n   return g[w][a] * C(w - cnty - 2 * a, b) % mod * f[h][b] % mod * n          C(h - cntx - 2 * b, a) % mod * fac[a] % mod * fac[b] % mod; n } n int main(int argc, char const *argv[]) { n   scanf( %d%d%d , &h, &w, &n); n   for (int i = 1; i <= n; i++) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     r[x1] = r[x2] = c[y1] = c[y2] = 1; n   } n   mx = max(h, w); n   fac[0] = 1; n   for (int i = 1; i <= mx; i++) fac[i] = fac[i - 1] * i % mod; n   inv[mx] = qpow(fac[mx], mod - 2); n   for (int i = mx; i >= 1; i--) inv[i - 1] = inv[i] * i % mod; n   for (int i = 1; i <= h; i++) cntx += r[i]; n   for (int i = 1; i <= w; i++) cnty += c[i]; n   for (int i = 0; i <= h; i++) f[i][0] = 1; n   for (int i = 2; i <= h; i++) n     for (int j = 1; 2 * j <= i; j++) n       if (r[i] || r[i - 1]) n         f[i][j] = f[i - 1][j]; n       else n         f[i][j] = (f[i - 1][j] + f[i - 2][j - 1]) % mod; n   for (int i = 0; i <= w; i++) g[i][0] = 1; n   for (int i = 2; i <= w; i++) n     for (int j = 1; 2 * j <= i; j++) n       if (c[i] || c[i - 1]) n         g[i][j] = g[i - 1][j]; n       else n         g[i][j] = (g[i - 1][j] + g[i - 2][j - 1]) % mod; n   for (int i = 0; i <= h && 2 * i <= w; i++) n     for (int j = 0; j <= w && 2 * j <= h; j++) n       if (2 * i + j <= w - cnty && i + 2 * j <= h - cntx) n         ans = (ans + cal(i, j)) % mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MAX = 1000000; n int MIN = -1000000; n int INF = 1000000000; n int x4[4] = {0, 1, 0, -1}; n int y4[4] = {1, 0, -1, 0}; n int x8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n int y8[8] = {1, 1, 0, -1, -1, -1, 0, 1}; n int i, j, k; n int main() { n   string str; n   cin >> str; n   str.push_back( 0 ); n   reverse(str.begin(), str.end()); n   str.push_back( 0 ); n   int indx = 1, ans = 1; n   while (indx < str.size()) { n     if (str[indx] ==  1  && str[indx + 1] ==  1 ) { n       str[indx - 1] =  1 ; n       for (i = indx; str[i] ==  1 ; i++) str[i] =  0 ; n       str[i] =  1 ; n       indx = i; n     } else n       indx++; n   } n   int satu = 0; n   for (i = (0); i < (str.size()); i++) satu += (str[i] ==  1 ); n   cout << satu << endl; n   { n     fflush(stdin); n     getchar(); n   }; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2 ) n #pragma GCC optimize( unroll-loops ) n #define rep(i,a,b) for(int i=(a);i<=(b);++i) n #define req(i,a,b) for(int i=(a);i>=(b);--i) n #define rep_(i,a,b) for(int i=(a);i<(b).size();++i) n #define F(a) rep(a,1,n) n #define M(a,b) memset(a,b,sizeof a) n #define DC int T;cin>>T;while(T--) n #define ll long long n #define Z(a,b) sort(a+1,a+b+1) n using namespace std; n const unsigned _mod=998244353; n const unsigned mod=1e9+7; n const ll infi=0x3f3f3f3f3f3f3fll; n const int inf=0x3f3f3f3f; n void rd(ll &x){x=0;int f=1;char ch=getchar();while(ch<48||ch>57){if(ch==45)f=-1;ch=getchar();}while(ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();x*=f;} n ll ksm(ll x,ll y=mod-2,ll m=mod){ll ret=1;while(y){if(y&1)ret=ret*x%m;y>>=1ll;x=x*x%m;}return ret;} n ll qpow(ll x,ll y=2){ll ret=1;while(y>0){if(y&1ll)ret=ret*x;y>>=1ll;x=x*x;}return ret;} n /* n     [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] n     [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] n     [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] n */ n ll fac[1000010],inv[1000010]; n ll C(ll n,ll m){if(m>n||m<0)return 0;return fac[n]*inv[m]%mod*inv[n-m]%mod;} n int n,l,r,ans; n int chk(int x) n { n     if (x<=0) return 0; n     int a=max(x+l-1,0),b=max(n-r+x,0); n     if (a+b>n) return 0; n     if (n&1) return C(n-a-b,n/2-a)+C(n-a-b,n/2-a+1); n     return C(n-a-b,n/2-a); n } n int chk2() n { n     if (n&1) return C(n,n/2)+C(n,n/2+1); n     return C(n,n/2); n } n int main() n { n     *fac=*inv=1; n     rep(i,1,1000000) fac[i]=fac[i-1]*i%mod; n     rep(i,1,1000000) inv[i]=ksm(fac[i]); n  DC n     { n         ans=0; n         cin>>n>>l>>r; n         rep(i,min(-l,r-n),min(n-l,r)) (ans+=chk(i))%=mod; n         (ans+=1ll*max(min(-l,r-n)-1,0)*chk2()%mod)%=mod; n         cout<<ans<<  n ; n     } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long int inf = 1e9 + 7; n const long long int mod = 998244353; n long long int mini(long long int a, long long int b) { n   if (a >= b) return b; n   return a; n } n long long int maxi(long long int a, long long int b) { n   if (a >= b) return a; n   return b; n } n const long long int MAXN = 300002; n long long int factorial[MAXN + 1], inverse[MAXN + 1]; n long long int binpow(long long int a, long long int b) { n   long long int res = 1; n   while (b > 0) { n     if (b % 2 == 1) { n       res = (res * a) % mod; n     } n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n long long int nCk(long long int n, long long int k) { n   if (k < 0 || k > n) { n     return 0; n   } n   long long int ans = factorial[n]; n   ans = (ans * inverse[n - k]) % mod; n   ans = (ans * inverse[k]) % mod; n   return ans; n } n void preCompute() { n   long long int fact = 1; n   factorial[0] = 1; n   inverse[0] = 1; n   for (long long int i = 1; i <= MAXN; i++) { n     fact = (fact * i) % mod; n     factorial[i] = fact; n     inverse[i] = binpow(factorial[i], mod - 2); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   preCompute(); n   long long int n; n   cin >> n; n   long long int sumr = 0, suml = 0; n   vector<long long int> A(2 * n); n   for (long long int i = 0; i <= 2 * n - 1; i++) cin >> A[i]; n   sort(A.begin(), A.end()); n   for (long long int i = 0; i <= n - 1; i++) suml += A[i], suml %= mod; n   for (long long int i = n; i <= 2 * n - 1; i++) sumr += A[i], sumr %= mod; n   long long int tot = (mod + sumr - suml) % mod; n   long long int a = nCk(2 * n, n); n   long long int ans = (a * tot) % mod; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, pos, l, r, ans = 0; n   cin >> n >> pos >> l >> r; n   if (abs(pos - l) > abs(pos - r)) { n     ans += abs(pos - r) + 1; n     if (n == r) ans -= abs(pos - r) + 1; n     n == r ? 0 : pos = r; n     ans += abs(pos - l) + 1; n     if (1 == l) ans -= abs(pos - l) + 1; n   } else { n     ans += abs(pos - l) + 1; n     if (1 == l) ans -= abs(pos - l) + 1; n     1 == l ? 0 : pos = l; n     ans += abs(pos - r) + 1; n     if (n == r) ans -= abs(pos - r) + 1; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int x, y, t, neg = 0, pos = 0; n   cin >> t; n   while (t--) { n     cin >> x >> y; n     if (x < 0) n       neg++; n     else n       pos++; n   } n   if (neg <= 1 || pos <= 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const long long maxn = (long long)2 * 1e5 + 10; n const double EPS = 1e-9; n const long long INF = 1e18 + 18; n const long long mod = (long long)10; n long long n, done[maxn], level[maxn], cnt = 0, parent[maxn]; n vector<long long> graph[maxn]; n set<pair<long long, long long> > seti; n void bfs(long long root) { n   queue<long long> Q; n   Q.push(root); n   done[root] = 1, level[root] = 0; n   while (!Q.empty()) { n     long long top = Q.front(); n     Q.pop(); n     for (long long nb : graph[top]) { n       if (done[nb]) continue; n       done[nb] = 1; n       level[nb] = 1 + level[top]; n       if (level[nb] > 2) seti.insert({-level[nb], nb}); n       parent[nb] = top; n       Q.push(nb); n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 1; i < n; i++) { n     long long u, v; n     cin >> u >> v; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   bfs(1); n   while (!seti.empty()) { n     pair<long long, long long> top = *seti.begin(); n     long long tlevel = -top.first, tv = top.second; n     for (long long nb : graph[parent[tv]]) { n       auto it = seti.find({-level[nb], nb}); n       if (it != seti.end()) seti.erase(seti.find({-level[nb], nb})); n     } n     auto it = seti.find({-level[parent[tv]], parent[tv]}); n     if (it != seti.end()) n       seti.erase(seti.find({-level[parent[tv]], parent[tv]})); n     cnt++; n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( -O2 ) n const int LIM = 5e5 + 5, MOD = 1e9 + 7; n int t, n, m, k, x, y; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tests; n   cin >> tests; n   cout << setprecision(10) << fixed; n   while (tests--) { n     long double d; n     cin >> d; n     long long v = d * d - 4 * d; n     if (v < -1e-9) { n       cout <<  N n ; n     } else { n       long double a, b, c; n       c = sqrt(v); n       a = (d + c) / 2; n       b = (d - c) / 2; n       cout <<  Y   << a <<     << b <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int X[1 << 11]; n int Y[1 << 11]; n int B[1 << 11]; n int NX[1 << 11]; n int NY[1 << 11]; n int E[1 << 11][1 << 11]; n int gN; n int dfs(int a) { n   if (a == gN - 1) return 1; n   B[a] = 1; n   int i; n   for (i = (0); i < (gN); ++i) n     if (B[i] == 0 && E[a][i] > 0 && dfs(i) != 0) { n       --E[a][i]; n       ++E[i][a]; n       return 1; n     } n   return 0; n } n void fwd(int a) { n   B[a] = 1; n   int i; n   for (i = (0); i < (gN); ++i) n     if (B[i] == 0 && E[a][i] > 0) fwd(i); n } n void bwd(int a) { n   B[a] = 1; n   int i; n   for (i = (0); i < (gN); ++i) n     if (B[i] == 0 && E[i][a] > 0) bwd(i); n } n int main() { n   int n; n   scanf( %d , &n); n   int i, j; n   for (i = (0); i < (n); ++i) scanf( %d%d , &X[i], &Y[i]); n   vector<pair<pair<int, int>, int>> vx(n), vy(n); n   for (i = (0); i < (n); ++i) { n     vx[i] = make_pair(pair<int, int>(X[i], Y[i]), i); n     vy[i] = make_pair(pair<int, int>(Y[i], X[i]), i); n   } n   sort((vx).begin(), (vx).end()); n   sort((vy).begin(), (vy).end()); n   vector<pair<int, int>> x, y; n   for (i = (1); i < (n); ++i) n     if (vx[i].first.first == vx[i - 1].first.first) n       x.push_back(pair<int, int>(vx[i - 1].second, vx[i].second)); n   for (i = (1); i < (n); ++i) n     if (vy[i].first.first == vy[i - 1].first.first) n       y.push_back(pair<int, int>(vy[i - 1].second, vy[i].second)); n   int nx = int((x).size()); n   int ny = int((y).size()); n   memset(NX, -1, sizeof(NX)); n   memset(NY, -1, sizeof(NY)); n   for (i = (0); i < (nx); ++i) NX[x[i].first] = x[i].second; n   for (i = (0); i < (ny); ++i) NY[y[i].first] = y[i].second; n   memset(E, 0, sizeof(E)); n   for (i = (0); i < (nx); ++i) n     for (j = (0); j < (ny); ++j) { n       int ax = x[i].first; n       int bx = x[i].second; n       int ay = y[j].first; n       int by = y[j].second; n       if (Y[ay] <= Y[ax] || Y[bx] <= Y[ay]) continue; n       if (X[ax] <= X[ay] || X[by] <= X[ax]) continue; n       E[i][nx + j] = 1; n     } n   for (i = (0); i < (nx); ++i) E[nx + ny][i] = 1; n   for (i = (0); i < (ny); ++i) E[i + nx][nx + ny + 1] = 1; n   gN = nx + ny + 2; n   int flow = 0; n   for (i = (0); i < (nx); ++i) n     for (j = (0); j < (ny); ++j) n       if (E[gN - 2][i] > 0 && E[i][nx + j] > 0 && E[nx + j][gN - 1] > 0) { n         --E[gN - 2][i]; n         ++E[i][gN - 2]; n         --E[i][nx + j]; n         ++E[nx + j][i]; n         --E[nx + j][gN - 1]; n         ++E[gN - 1][nx + j]; n         ++flow; n       } n   while (true) { n     memset(B, 0, sizeof(B)); n     if (dfs(nx + ny) == 0) break; n     ++flow; n   } n   for (i = (0); i < (nx); ++i) n     if (B[i] == 0) { n       int a = x[i].first; n       int b = x[i].second; n       NX[a] = -1; n     } n   for (i = (0); i < (ny); ++i) n     if (B[nx + i] != 0) { n       int a = y[i].first; n       int b = y[i].second; n       NY[a] = -1; n     } n   memset(B, 0, sizeof(B)); n   vector<pair<pair<int, int>, pair<int, int>>> rx; n   for (i = (0); i < (int((vx).size())); ++i) { n     int a = vx[i].second; n     if (B[a] != 0) continue; n     B[a] = 1; n     int b = a; n     while (NX[b] != -1) { n       b = NX[b]; n       B[b] = 1; n     } n     if (X[a] != X[b]) throw 0; n     rx.push_back( n         make_pair(pair<int, int>(X[a], Y[a]), pair<int, int>(X[b], Y[b]))); n   } n   memset(B, 0, sizeof(B)); n   vector<pair<pair<int, int>, pair<int, int>>> ry; n   for (i = (0); i < (int((vy).size())); ++i) { n     int a = vy[i].second; n     if (B[a] != 0) continue; n     B[a] = 1; n     int b = a; n     while (NY[b] != -1) { n       b = NY[b]; n       B[b] = 1; n     } n     if (Y[a] != Y[b]) throw 0; n     ry.push_back( n         make_pair(pair<int, int>(X[a], Y[a]), pair<int, int>(X[b], Y[b]))); n   } n   if (int((rx).size()) + int((ry).size()) != 2 * n - (nx + ny - flow)) throw 0; n   printf( %d n , int((ry).size())); n   for (i = (0); i < (int((ry).size())); ++i) n     printf( %d %d %d %d n , ry[i].first.first, ry[i].first.second, n            ry[i].second.first, ry[i].second.second); n   printf( %d n , int((rx).size())); n   for (i = (0); i < (int((rx).size())); ++i) n     printf( %d %d %d %d n , rx[i].first.first, rx[i].first.second, n            rx[i].second.first, rx[i].second.second); n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n vector<int> scs(const string& s) { n   int n = s.size(); n   vector<int> p(n), c(s.begin(), s.end()); n   vector<vector<int>> g(max(n, 256)); n   for (int i = 0; i < n; i++) g[c[i]].push_back(i); n   int b = 0; n   for (auto& gr : g) { n     for (int x : gr) p[b++] = x; n     gr.clear(); n   } n   for (int h = 1; h < n; h <<= 1) { n     vector<int> p2(n), c2(n); n     for (int j : p) { n       int jp = (j + n - h) % n; n       g[c[jp]].push_back(jp); n     } n     b = 0; n     for (auto& gr : g) { n       for (int x : gr) p2[b++] = x; n       gr.clear(); n     } n     b = 0; n     c2[p2[0]] = 0; n     for (int i = 1; i < n; i++) { n       int x = p2[i]; n       int y = p2[i - 1]; n       if (c[x] == c[y] && c[(x + h) % n] == c[(y + h) % n]) n         c2[x] = b; n       else n         c2[x] = ++b; n     } n     swap(c, c2); n     swap(p, p2); n   } n   return p; n } n struct stvar { n   int a, b; n   stvar(int x = 0) : a(x), b(min(0, x)) {} n   stvar(int p, int q) : a(p), b(q) {} n   stvar operator+(const stvar& o) const { n     return stvar(a + o.a, min(b, a + o.b)); n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   string s; n   cin >> s; n   int n = s.size(); n   vector<int> p = scs(s); n   vector<stvar> pref(n + 1), suff(n + 1); n   for (int i = 0; i < n; i++) n     pref[i + 1] = pref[i] + stvar(s[i] ==  (  ? 1 : -1); n   for (int i = n - 1; i >= 0; i--) n     suff[i] = stvar(s[i] ==  (  ? 1 : -1) + suff[i + 1]; n   for (int j : p) { n     auto t = suff[j] + pref[j]; n     if (t.a < 0) { n       if (t.a == t.b) { n         cout << string(-t.a,  ( ) << s.substr(j) << s.substr(0, j) <<   n ; n         return 0; n       } n     } else { n       if (t.b == 0) { n         cout << s.substr(j) << s.substr(0, j) << string(t.a,  ) ) <<   n ; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, tot; n bool ok(int i, int j) { n   if (i < 0 or i >= n or j < 0 or j >= m) return 0; n   return 1; n } n long long int c[20][20] = {0}; n void matcpy(long long int a[20][20], long long int b[20][20]) { n   for (int i = 0; i < tot; i++) { n     for (int j = 0; j < tot; j++) { n       a[i][j] = b[i][j]; n     } n   } n } n void mul(long long int a[20][20], long long int b[20][20]) { n   for (int i = 0; i < tot; i++) { n     for (int j = 0; j < tot; j++) { n       c[i][j] = 0; n       for (int k = 0; k < tot; k++) { n         c[i][j] += (a[i][k] * b[k][j]) % 1000000007; n         c[i][j] %= 1000000007; n       } n     } n   } n   matcpy(a, c); n } n long long int id[20][20] = {0}; n int cat[20][20] = {0}; n void bpow(long long int a[20][20], int k) { n   for (int i = 0; i < tot; i++) { n     for (int j = 0; j < tot; j++) { n       if (i == j) n         id[i][j] = 1; n       else n         id[i][j] = 0; n     } n   } n   while (k > 0) { n     if (k & 1) mul(id, a); n     mul(a, a); n     k /= 2; n   } n   matcpy(a, id); n } n long long int mat[20][20] = {0}; n long long int T[20][20] = {0}; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int q, x, y, t, tp; n   cin >> n >> m >> q; n   tot = n * m; n   int prev = 1; n   for (int i = 0; i < tot; i++) { n     for (int j = 0; j < tot; j++) { n       if (i == j) n         mat[i][j] = 1; n       else n         mat[i][j] = 0; n     } n   } n   while (q--) { n     cin >> tp >> x >> y >> t; n     x--; n     y--; n     for (int i1 = 0; i1 < n; i1++) { n       for (int j1 = 0; j1 < m; j1++) { n         for (int i2 = 0; i2 < n; i2++) { n           for (int j2 = 0; j2 < m; j2++) { n             if (!cat[i1][j1] and !cat[i2][j2] and n                 abs(i1 - i2) + abs(j1 - j2) <= 1) { n               T[m * i1 + j1][m * i2 + j2] = 1; n             } else { n               T[m * i1 + j1][m * i2 + j2] = 0; n             } n           } n         } n       } n     } n     bpow(T, t - prev); n     prev = t; n     mul(mat, T); n     if (tp == 1) { n       cout << mat[0][m * x + y] <<   n ; n     } else if (tp == 2) { n       cat[x][y] = 1; n     } else { n       cat[x][y] = 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, b[100] = {0}, g[100] = {0}, t, k, c = 0; n   cin >> n >> m; n   cin >> t; n   while (t--) { n     cin >> k; n     b[k] += 1; n     c += 1; n   } n   cin >> k; n   while (k--) { n     cin >> t; n     g[t] += 1; n     c += 1; n   } n   for (int i = 0; i < 100 * max(n, m); i++) { n     if (b[i % n] + g[i % m] == 1) { n       b[i % n] = g[i % m] = 1; n       c += 1; n     } n   } n   if (c == n + m) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300050; n int a[maxn << 2]; n int ll[maxn], rr[maxn], x[maxn], N, M; n void getdown(int cur) { n   a[cur << 1] = a[cur]; n   a[(cur << 1) | 1] = a[cur]; n   a[cur] = 0; n } n void Insert(int cur, int L, int R, int l, int r, int u) { n   int mid = (L + R) >> 1; n   if (l <= L && R <= r) { n     a[cur] = u; n     return; n   } n   if (a[cur]) getdown(cur); n   if (l <= mid) Insert(((cur) << 1), L, mid, l, r, u); n   if (r > mid) Insert((((cur) << 1) | 1), (mid + 1), R, l, r, u); n } n int Query(int cur, int L, int R, int p) { n   int mid = (L + R) >> 1; n   if (a[cur]) return a[cur]; n   if (L == R) return a[cur]; n   if (p <= mid) n     return Query(((cur) << 1), L, mid, p); n   else n     return Query((((cur) << 1) | 1), (mid + 1), R, p); n } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= M; i++) { n     scanf( %d%d%d , &ll[i], &rr[i], &x[i]); n   } n   for (int i = M; i >= 1; i--) { n     if (ll[i] < x[i]) Insert(1, 1, N, ll[i], x[i] - 1, x[i]); n     if (x[i] < rr[i]) Insert(1, 1, N, x[i] + 1, rr[i], x[i]); n   } n   printf( %d , Query(1, 1, N, 1)); n   for (int i = 2; i <= N; i++) printf(  %d , Query(1, 1, N, i)); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int len, rest; n int space[10][110] = {0}; n int num = 0; n int ans[21000][3] = {0}; n void Check() { n   for (int i = 1; i <= len; i++) n     if (space[1][i] != 0 && space[1][i] == space[2][i]) { n       num++; n       rest--; n       ans[num][0] = space[2][i]; n       ans[num][1] = 1; n       ans[num][2] = i; n       space[2][i] = 0; n     } n   for (int i = 1; i <= len; i++) n     if (space[4][i] != 0 && space[3][i] == space[4][i]) { n       num++; n       rest--; n       ans[num][0] = space[3][i]; n       ans[num][1] = 4; n       ans[num][2] = i; n       space[3][i] = 0; n     } n   return; n } n void Rotate() { n   int px = 0, py = 0; n   for (int k = 2; k <= 3 && !px; k++) n     for (int i = 1; i <= len && !px; i++) n       if (space[k][i] == 0) { n         px = k; n         py = i; n       } n   for (int time = 1; time < len * 2; time++) { n     int nx, ny; n     if (px == 2) n       if (py < len) { n         nx = 2; n         ny = py + 1; n       } else { n         nx = 3; n         ny = len; n       } n     else if (py > 1) { n       nx = 3; n       ny = py - 1; n     } else { n       nx = 2; n       ny = 1; n     } n     if (space[nx][ny] != 0) { n       num++; n       ans[num][0] = space[nx][ny]; n       ans[num][1] = px; n       ans[num][2] = py; n       space[px][py] = space[nx][ny]; n       space[nx][ny] = 0; n     } n     px = nx; n     py = ny; n   } n   return; n } n int main() { n   int num_car; n   scanf( %d%d , &len, &num_car); n   rest = num_car; n   for (int k = 1; k <= 4; k++) n     for (int i = 1; i <= len; i++) scanf( %d , &space[k][i]); n   Check(); n   if (rest == num_car && num_car == len * 2) { n     printf( -1 n ); n     return 0; n   } n   while (rest) { n     Rotate(); n     Check(); n   } n   printf( %d n , num); n   for (int i = 1; i <= num; i++) n     printf( %d %d %d n , ans[i][0], ans[i][1], ans[i][2]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void enable_comma() {} n string tostring(char c) { n   string s =   ; n   s += c; n   return s; n } n string tostring(string s) { return     + s +    ; } n string tostring(const char *c) { return tostring((string)c); } n string tostring(long long x) { n   if (x < 0) return  -  + tostring(-x); n   if (x > 9) n     return tostring(x / 10) + tostring(char( 0  + x % 10)); n   else n     return tostring(char( 0  + x)); n } n string tostring(int x) { return tostring((long long)x); } n string tostring(unsigned long long x) { n   if (x > 9) n     return tostring((long long)(x / 10)) + tostring(char( 0  + x % 10)); n   else n     return tostring(char( 0  + x)); n } n string tostring(unsigned x) { return tostring((long long)x); } n string tostring(double x) { n   static char res[114]; n   sprintf(res,  %lf , x); n   string s = tostring(res); n   return s.substr(1, (int)s.size() - 2); n } n string tostring(long double x) { return tostring((double)x); } n template <class A, class B> n string tostring(pair<A, B> p) { n   return  (  + tostring(p.first) +  ,  + tostring(p.second) +  ) ; n } n template <class T> n string tostring(T v) { n   string res =   ; n   for (auto p : v) res += (res.size() ?  ,  :  { ) + tostring(p); n   return res.size() ? res +  }  :  {} ; n } n template <class A> n string tostring(A *a, int L, int R) { n   return tostring(vector<A>(a + L, a + R + 1)); n }; n template <class A> n string tostring(A a, int L, int R) { n   return tostring(a.data(), L, R); n } n string tostrings() { return   ; } n template <typename Head, typename... Tail> n string tostrings(Head H, Tail... T) { n   return tostring(H) +     + tostrings(T...); n } n long long read() { n   long long x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f = ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   return f ? -x : x; n } n template <class T> n void ckmax(T &x, const T y) { n   if (x < y) x = y; n } n template <class T> n void ckmin(T &x, const T y) { n   if (x > y) x = y; n } n const int mod = 1e9 + 7; n int Pow(int x, long long y) { n   y = (y % (mod - 1) + (mod - 1)) % (mod - 1); n   int ans = 1; n   for (; y; y >>= 1, x = (long long)x * x % mod) n     if (y & 1) ans = (long long)ans * x % mod; n   return ans; n } n void Add(int &x, int y) { n   if ((x += y) >= mod) x -= mod; n } n void Del(int &x, int y) { n   if ((x -= y) < 0) x += mod; n } n int Add(int x) { return x >= mod ? x - mod : x; } n int Del(int x) { return x < 0 ? x + mod : x; } n int md(long long x) { return (x % mod + mod) % mod; } n const int N = 1000005; n int n, m; n char s[N]; n int a[N]; n int id[10], cnt = 0; n vector<int> e[N]; n int v10[N], l10[N], f[N]; n vector<int> r[N]; n int dd[N]; n void solve() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   m = read(); n   for (int i = (0); i <= (9); i++) id[i] = ++cnt; n   for (int i = (1); i <= (n); i++) a[i] = id[s[i] -  0 ]; n   while (m--) { n     scanf( %s , s + 1); n     int len = strlen(s + 1); n     int v = s[1] -  0 ; n     int i = id[v]; n     id[v] = ++cnt; n     for (int j = (4); j <= (len); j++) { n       e[i].push_back(id[s[j] -  0 ]); n       r[id[s[j] -  0 ]].push_back(i); n       dd[i]++; n     } n   } n   queue<int> Q; n   for (int i = (1); i <= (cnt); i++) n     if (!dd[i]) Q.push(i); n   for (int i = (0); i <= (9); i++) { n     f[id[i]] = 1; n     v10[id[i]] = i; n     l10[id[i]] = 10; n   } n   while (!Q.empty()) { n     int i = Q.front(); n     Q.pop(); n     for (auto j : r[i]) n       if (!--dd[j]) Q.push(j); n     if (f[i]) continue; n     l10[i] = 1; n     v10[i] = 0; n     for (auto j : e[i]) { n       v10[i] = ((long long)v10[i] * l10[j] + v10[j]) % mod; n       l10[i] = (long long)l10[i] * l10[j] % mod; n     } n   } n   enable_comma(); n   enable_comma(); n   enable_comma(); n   enable_comma(); n   enable_comma(); n   int ans = 0; n   for (int i = (1); i <= (n); i++) { n     ans = ((long long)ans * l10[a[i]] + v10[a[i]]) % mod; n   } n   cout << ans << endl; n } n int main() { n   int T = 1; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4002; n int n = 0, a; n char ch[N]; n long long ans = 0, sum[N][N], all; n long long d[N * 9]; n int val(int ind) { return ch[ind - 1] -  0 ; } n int main() { n   cin >> a; n   while (cin >> ch[n]) n++; n   for (int i = 1; i <= n; ++i) { n     for (int j = i; j <= n; ++j) { n       sum[i][j] = val(j) + sum[1][j - 1] - sum[1][i - 1]; n       d[sum[i][j]]++; n     } n   } n   for (int tt = 0; tt <= 4000 * 9; ++tt) all += d[tt]; n   for (int i = 1; i <= n; ++i) { n     for (int j = i; j <= n; ++j) { n       if (sum[i][j] == 0) { n         if (a == 0) ans += all; n         continue; n       } n       int cur = a / sum[i][j]; n       if (cur * sum[i][j] == a) { n         if (cur >= 0 && cur <= 4000 * 9) ans += d[cur]; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int md = 998244853; n long long ans, f[4005][4005], x, y, n, m; n int main() { n   cin >> n >> m; n   for (int i = 0; i <= n + m; i++) f[i][0] = f[i][i] = 1; n   for (int i = 0; i <= n + m; i++) n     for (int j = 0; j < i; j++) n       f[i][j] = (f[i - 1][j - 1] % md + f[i - 1][j] % md) % md; n   for (int i = n; i && i >= n - m; i--) { n     x = (f[n + m][n - i] - y + md) % md; n     (ans += (x * i) % md) %= md; n     (y += x) %= md; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_BUF_SIZE = 16384; n char BUFOR[MAX_BUF_SIZE]; n int BUF_SIZE, BUF_POS; n char ZZZ; n const int MXN = 50010; n const int C = 262144; n const int INF = 1000000001; n int n, czas, skla; n vector<int> G[6 * MXN], GT[6 * MXN], H[6 * MXN], vert[6 * MXN]; n vector<int> pre, skl, fu; n class SAT2 { n   SAT2() {} n   int neg(int v1) { return (v1 + n) % (2 * n); } n   void addEdge(int v1, int v2) { n     G[neg(v1)].push_back(v2); n     GT[v2].push_back(neg(v1)); n     G[neg(v2)].push_back(v1); n     GT[v1].push_back(neg(v2)); n   } n   void dfs(int x) { n     pre[x] = ++czas; n     for (__typeof((G[x]).begin()) it = ((G[x]).begin()); it != (G[x]).end(); n          ++it) n       if (!pre[*it]) dfs(*it); n     fu.push_back(x); n   } n   void dfs2(int x) { n     skl[x] = skla; n     vert[skla].push_back(x); n     for (__typeof((GT[x]).begin()) it = ((GT[x]).begin()); it != (GT[x]).end(); n          ++it) n       if (!skl[*it]) dfs2(*it); n   } n   void dfs3(int x) { n     pre[x] = 1; n     for (__typeof((H[x]).begin()) it = ((H[x]).begin()); it != (H[x]).end(); n          ++it) n       if (!pre[*it]) dfs3(*it); n     fu.push_back(x); n   } n  n  public: n   void clear() { n     fu.clear(); n     pre.clear(); n     skl.clear(); n     for (int i = (0); i <= (2 * n); i++) { n       G[i].clear(); n       GT[i].clear(); n       vert[i].clear(); n       H[i].clear(); n     } n   } n   SAT2(int _n) { n     clear(); n     czas = 0; n     skla = 0; n     n = _n; n     pre.resize(2 * n, 0); n     skl.resize(2 * n, 0); n   } n   void add(int v1, bool pos1, int v2, bool pos2) { n     addEdge((pos1 ? neg(v1) : v1), (pos2 ? neg(v2) : v2)); n   } n   pair<bool, vector<bool> > solve() { n     for (int i = (0); i <= (2 * n - 1); i++) n       if (!pre[i]) dfs(i); n     for (int i = ((int)(fu).size() - 1); i >= (0); i--) { n       int w = fu[i]; n       if (!skl[w]) { n         skla++; n         dfs2(w); n       } n     } n     skla++; n     for (int i = (0); i <= (n - 1); i++) n       if (skl[i] == skl[i + n]) return make_pair(false, vector<bool>()); n     for (int i = (0); i <= (2 * n - 1); i++) { n       for (__typeof((G[i]).begin()) it = ((G[i]).begin()); it != (G[i]).end(); n            ++it) n         H[skl[i]].push_back(skl[*it]); n     } n     fu.clear(); n     pre = vector<int>(skla, 0); n     for (int i = (0); i <= (skla - 1); i++) n       if (!pre[i]) dfs3(i); n     vector<bool> res(2 * n, 0); n     vector<int> tmp(skla + 1, -1); n     for (int i = ((int)(fu).size() - 1); i >= (0); i--) { n       int w = fu[i]; n       if (tmp[w] == -1) tmp[w] = 0; n       if (tmp[w] == 1) n         for (__typeof((H[w]).begin()) it = ((H[w]).begin()); it != (H[w]).end(); n              ++it) n           tmp[*it] = 1; n       for (__typeof((vert[w]).begin()) it = ((vert[w]).begin()); n            it != (vert[w]).end(); ++it) { n         tmp[skl[neg(*it)]] = !tmp[w]; n         res[*it] = tmp[w]; n         res[neg(*it)] = !tmp[w]; n       } n     } n     return make_pair(true, res); n   } n }; n int n2, m; n vector<pair<int, pair<int, int> > > G2[MXN]; n int c[MXN], t[MXN]; n pair<bool, vector<bool> > check(int second) { n   SAT2 SAT(3 * m); n   for (int i = (0); i <= (m - 1); i++) n     if (t[i] > second) SAT.add(i, 1, i, 1); n   for (int i = (1); i <= (n2); i++) { n     for (int j = (0); j <= ((int)(G2[i]).size() - 1); j++) { n       int iter = j; n       while (iter < (int)(G2[i]).size() && G2[i][j].first == G2[i][iter].first) n         iter++; n       if (iter - j > 2) n         return make_pair(false, vector<bool>()); n       else if (iter - j == 2) n         SAT.add(G2[i][j].second.second, 0, G2[i][j + 1].second.second, 0); n       j = iter - 1; n     } n     int last = -1; n     for (int j = (0); j <= ((int)(G2[i]).size() - 1); j++) { n       int ei = G2[i][j].second.second; n       int pi = ei; n       if (i < G2[i][j].second.first) n         pi += m; n       else n         pi += 2 * m; n       SAT.add(pi, 1, ei, 1); n       if (last != -1) { n         SAT.add(ei, 1, last, 0); n         SAT.add(pi, 1, last, 0); n       } n       last = pi; n     } n   } n   return SAT.solve(); n } n void test() { n   scanf( %d %d , &n2, &m); n   int mx = 0; n   for (int i = (1); i <= (m); i++) { n     int a, b; n     scanf( %d %d %d %d , &a, &b, &c[i - 1], &t[i - 1]); n     mx = max(mx, t[i - 1]); n     G2[a].push_back(make_pair(c[i - 1], make_pair(b, i - 1))); n     G2[b].push_back(make_pair(c[i - 1], make_pair(a, i - 1))); n   } n   for (int i = (1); i <= (n2); i++) sort((G2[i]).begin(), (G2[i]).end()); n   int res = INF; n   int L = 0, R = mx; n   vector<bool> ans; n   while (L <= R) { n     int second = (L + R) / 2; n     pair<bool, vector<bool> > p1 = check(second); n     if (p1.first) { n       if (res > second) { n         res = second; n         ans = p1.second; n       } n       R = second - 1; n     } else n       L = second + 1; n   } n   if (res != INF) { n     printf( Yes n ); n     int cnt = 0; n     for (int i = (0); i <= (m - 1); i++) n       if (ans[i]) cnt++; n     printf( %d %d n , res, cnt); n     for (int i = (0); i <= (m - 1); i++) n       if (ans[i]) printf( %d  , i + 1); n     printf(  n ); n   } else n     printf( No n ); n } n int main() { n   int te = 1; n   while (te--) test(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[1050000], k[25][25], a[1050000]; n int main() { n   long long n, m; n   while (scanf( %lld%lld , &n, &m) == 2) { n     string s; n     cin >> s; n     memset(k, 0, sizeof(k)); n     for (int i = 1; i < s.length(); i++) { n       k[s[i - 1] -  a ][s[i] -  a ]++; n       k[s[i] -  a ][s[i - 1] -  a ]++; n     } n     long long an = (1 << m) - 1; n     dp[0] = a[0] = 0; n     for (int i = 1; i <= an; i++) { n       dp[i] = 1e17 + 5; n       a[i] = 0; n       for (int f = 0; f < m; f++) { n         if (i & (1 << f)) { n           dp[i] = min(dp[i], dp[i ^ (1 << f)] + a[i ^ (1 << f)]); n         } n       } n       for (int f = 0; f < m; f++) { n         for (int j = f + 1; j < m; j++) { n           if (((i >> f) & 1) ^ ((i >> j) & 1)) { n             a[i] += k[f][j]; n           } n         } n       } n     } n     printf( %lld n , dp[an]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long a; n   cin >> a; n   vector<long long> x; n   for (long long i = 0; i < a; i++) { n     long long b; n     cin >> b; n     x.push_back(b); n   } n   bool done[101] = {false}; n   sort(x.begin(), x.end()); n   long long counter = 0; n   long long ans = 0; n   while (counter != a) { n     long long num = 0; n     for (long long i = 0; i < a; i++) { n       if (x[i] >= num && !done[i]) { n         num++; n         counter++; n         done[i] = true; n       } n     } n     ans++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[100001], csum[100001]; n int main() { n   ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); n   int n, q; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   for (int i = 1; i <= n; i++) { n     csum[i] = csum[i - 1] + ar[i - 1]; n   } n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     cout << (csum[r] - csum[l - 1]) / 10 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   int n; n   cin >> n; n   cout << n / 2 - 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n long long a[N], b[N]; n deque<int> mx, mn; n int main() { n   long long n; n   cin >> n; n   long long i; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) cin >> b[i]; n   long long j, ans = 0; n   for (j = 1, i = 1; i <= n; i++) { n     while (!mx.empty() and a[mx.back()] <= a[i]) mx.pop_back(); n     while (!mn.empty() and b[mn.back()] >= b[i]) mn.pop_back(); n     mx.push_back(i), mn.push_back(i); n     while (j <= i and a[mx.front()] - b[mn.front()] > 0) { n       j++; n       while (!mx.empty() and mx.front() < j) mx.pop_front(); n       while (!mn.empty() and mn.front() < j) mn.pop_front(); n     } n     if (!mx.empty() and !mn.empty() and a[mx.front()] == b[mn.front()]) n       ans += min(mx.front(), mn.front()) - j + 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-8; n const int mod = 8 * 9 * 7 * 11 * 13 * 5; n const int maxn = 205; n const double pi = acos(-1.0); n const int INF = 0x7f7f7f7f; n const double inf = 1e50; n template <class T> n inline T minx(T a, T b) { n   return (a == -1 || b < a) ? b : a; n } n template <class T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n inline T lcm(T a, T b) { n   return a * (b / gcd(a, b)); n } n template <class T> n inline T getbit(T n) { n   return n ? (1 + getbit(n & (n - 1))) : 0; n } n template <class T> n inline T lowbit(T n) { n   return n & (-n); n } n using namespace std; n long long dp[mod + 2]; n long long a, b, k, c, y = 0, l, i, t; n int main() { n   cin >> a >> b >> k; n   while (a > b) { n     l = 1; n     for (i = 2; i <= k; i++) { n       t = a % i; n       if ((t > l) && (a - t) >= b) l = t; n     } n     y++, a -= l; n     if (dp[a % mod]) { n       c = y - dp[a % mod]; n       y += c * ((a - b) / mod); n       a -= ((a - b) / mod) * mod; n     } n     dp[a % mod] = y; n   } n   cout << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4786) n #pragma warning(disable : 4996) n using namespace std; n int c[200005], d[200005]; n int main() { n   int n; n   scanf( %d , &n); n   int val = 0; n   int lb, ub; n   lb = -1000000000; n   ub = 1000000000; n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &c[i], &d[i]); n     if (d[i] == 1) { n       lb = ((lb) > (1900 - val) ? (lb) : (1900 - val)); n     } else { n       ub = ((ub) < (1899 - val) ? (ub) : (1899 - val)); n     } n     val += c[i]; n   } n   if (lb > ub) { n     printf( Impossible n ); n     return 0; n   } n   val = ub; n   for (int i = 1; i <= n; i++) { n     val += c[i]; n   } n   if (val >= 100000000) n     printf( Infinity n ); n   else n     printf( %d n , val); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char a[1000005]; n   scanf( %s , a); n   int i; n   long long ans = 0, b = 0; n   for (i = strlen(a) - 1; i >= 0; i--) { n     if (a[i] ==  b ) n       b++; n     else if (a[i] ==  a ) { n       ans += b; n       b <<= 1; n     } n     ans %= 1000000007; n     b %= 1000000007; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, ans, cnt; n char c[333][333]; n pair<int, pair<int, int> > p[3 * int(1e5)]; n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> c[i][j]; n     } n   } n   for (int q = 1; q <= min(n, m); q++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         if (c[i][j] ==  * ) n           if (i + q <= n && i - q > 0 && j + q <= m && j - q > 0) { n             if (c[i + q][j] ==  *  && c[i - q][j] ==  *  && n                 c[i][j + q] ==  *  && c[i][j - q] ==  * ) n               ans++; n             if (ans == k) { n               cout << i <<     << j << endl n                    << i - q <<     << j << endl n                    << i + q <<     << j << endl n                    << i <<     << j - q << endl n                    << i <<     << j + q; n               return 0; n             } n           } n       } n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100100], a1[100100], a2[100100]; n int main() { n   while (scanf( %d , &n) != -1) { n     int ans1 = -1, ans2 = -1; n     for (int i = 0; i < n; i++) scanf( %d , a + i); n     for (int i = 0; i < n - 1; i++) scanf( %d , a1 + i); n     for (int i = 0; i < n - 2; i++) scanf( %d , a2 + i); n     map<int, int> mp; n     for (int i = 0; i < n - 1; i++) mp[a1[i]]++; n     for (int i = 0; i < n; i++) { n       if (mp[a[i]]-- <= 0) { n         ans1 = a[i]; n       } n     } n     mp.clear(); n     for (int i = 0; i < n - 2; i++) mp[a2[i]]++; n     for (int i = 0; i < n - 1; i++) { n       if (mp[a1[i]]-- <= 0) { n         ans2 = a1[i]; n       } n     } n     printf( %d n%d n , ans1, ans2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string ff, ll; n   cin >> ff >> ll; n   string ans =   ; n   ans += ff[0]; n   for (int i = 1; i < ff.length(); i++) { n     if (ff[i] >= ll[0]) { n       ans += ll[0]; n       break; n     } n     ans += ff[i]; n     if (i == ff.length() - 1) { n       ans += ll[0]; n     } n   } n   if (ff.length() == 1) ans += ll[0]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   int psum = 0, msum = 0; n   int zero = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > 0) psum += a[i]; n     if (a[i] < 0) msum += a[i]; n   } n   if (psum > abs(msum)) { n     puts( YES ); n     sort(a.begin(), a.end(), greater<int>()); n     for (int i = 0; i < n; i++) { n       cout << a[i] <<    ; n     } n     cout <<   n ; n   } else if (psum < abs(msum)) { n     puts( YES ); n     sort(a.begin(), a.end()); n     for (int i = 0; i < n; i++) { n       cout << a[i] <<    ; n     } n     cout <<   n ; n   } else n     puts( NO ); n } n int main() { n   int tc; n   cin >> tc; n   while (tc--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p; n   scanf( %d %d , &n, &p); n   int nums[n]; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> nums[i]; n     nums[i] %= p; n     sum += nums[i]; n     sum %= p; n   } n   int max_part = 0; n   int left = 0; n   int right = sum; n   for (int i = 0; i < n; i++) { n     left += nums[i]; n     left %= p; n     right -= nums[i]; n     while (right < 0) { n       right += p; n     } n     int part = left + right; n     if (part > max_part) { n       max_part = part; n     } n   } n   cout << max_part; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n template <class T> n using vec = std::vector<T>; n template <class T> n using vec2d = std::vector<std::vector<T>>; n template <class T> n vec2d<T> makeVec2d(const int n1, const int n2, const T initValue) { n   return vec2d<T>(n1, vec<T>(n2, initValue)); n } n bool __hack = std::ios::sync_with_stdio(false); n auto __hack1 = cin.tie(nullptr); n struct Input { n   Input(istream &in) : in(&in) {} n   template <class T> n   T next() const { n     T x; n     *in >> x; n     return x; n   } n   int ni() const { return next<int>(); } n   istream *in; n }; n Input in(cin); n class Output { n  private: n   ostream *out; n   template <typename T> n   void printSingle(const T &value) { n     *out << value; n   } n  n  public: n   Output(ostream &out) : out(&out) {} n   inline void print() {} n   template <typename T, typename... Ts> n   inline void print(const T &f, const Ts &...args) { n     printSingle(f); n     if (sizeof...(args) != 0) { n       *out <<    ; n       print(args...); n     } n   } n   template <typename... Ts> n   inline void println(const Ts &...args) { n     print(args...); n     *out <<   n ; n   } n   template <typename... Ts> n   inline void operator()(const Ts &...args) { n     println(args...); n   } n }; n Output out(cout); n namespace template_util { n constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; } n template <int N> n struct bytetype {}; n template <uint64_t N> n struct minimal_uint : bytetype<bytecount(N)> {}; n }  // namespace template_util n template <typename T> n static constexpr T wrap_pos(T a, T m) { n   return a < 0 ? a + m : a; n } n template <typename T> n static constexpr T mod(T a, T m) { n   return wrap_pos(a % m, m); n } n template <typename T> n static constexpr T inverse2(T a, T m) { n   return a <= 1 ? a : mod((1 - inverse2(m % a, a) * m) / a, m); n } n template <typename T> n static constexpr T inverse(T a, T m) { n   return inverse2(mod(a, m), m); n } n template <typename T, typename C, T Modulus> n class MR { n  private: n   struct tag_plus {}; n   struct tag_minus {}; n   struct tag_good {}; n   static_assert(std::numeric_limits<C>::max() / Modulus / Modulus > 0, n                  compute type is too small ); n   static_assert(Modulus < std::numeric_limits<T>::max() / 2, n                  storage type is too small ); n   static constexpr T reduce(T value, tag_plus) { n     return value >= Modulus ? value - Modulus : value; n   } n   static constexpr T reduce(T value, tag_minus) { n     return value < 0 ? value + Modulus : value; n   } n   static constexpr T reduce(T value, tag_good) { return value; } n  n  public: n   T value; n   constexpr MR() : value(0) {} n   constexpr MR(C value) : value(reduce(value % Modulus, tag_minus())) {} n   template <typename tag_t> n   constexpr MR(T value, tag_t tag) : value(reduce(value, tag)) {} n   MR &operator=(C value) { n     this->value = reduce(value % Modulus, tag_minus()); n     return *this; n   } n   constexpr MR operator*(MR b) const { n     return MR(C(value) * C(b.value) % Modulus, tag_good()); n   } n   MR &operator+=(MR b) { n     value = reduce(value + b.value, tag_plus()); n     return *this; n   } n   constexpr T get() const { return value; } n   constexpr MR inverse() const { n     return MR(::inverse(C(value), C(Modulus)), tag_good()); n   } n   constexpr MR operator/(MR b) const { return *this * b.inverse(); } n }; n template <typename T, typename C, T Modulus> n static inline std::ostream &operator<<(std::ostream &o, MR<T, C, Modulus> mr) { n   return o << mr.get(); n } n template <class T> n vector<T> getFactorials(int n) { n   vector<T> res(n); n   res[0] = T(1); n   for (int i = 1; i < n; ++i) { n     res[i] = res[i - 1] * i; n   } n   return res; n } n template <class T> n inline T setBit(T mask, int bit) { n   assert(0 <= bit && bit < numeric_limits<T>::digits); n   return mask | ((T)(1) << bit); n } n template <class T> n inline bool getBit(T mask, int bit) { n   assert(0 <= bit && bit < numeric_limits<T>::digits); n   return mask & ((T)(1) << bit); n } n template <class T> n inline int bitCnt(T mask); n template <> n inline int bitCnt(int32_t mask) { n   return __builtin_popcount(mask); n } n struct Component { n   vec<int> vs; n   vec2d<MR<int, int64_t, 998244353>> dp; n   Component() {} n }; n void solve(istream &inStream, ostream &outStream) { n   in = Input(inStream); n   out = Output(outStream); n   auto n = in.ni(); n   auto m = in.ni(); n   vec2d<pair<int, int>> events(n + 2); n   for (int i = 0; i < (n); ++i) { n     auto x = in.ni(); n     auto y = in.ni() + 1; n     events[x].push_back({1, i}); n     events[y].push_back({0, i}); n   } n   vec2d<int> g(n); n   set<pair<int, int>> edges; n   for (int i = 0; i < (m); ++i) { n     auto x = in.ni() - 1; n     auto y = in.ni() - 1; n     edges.insert({x, y}); n     edges.insert({y, x}); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   vec<int> u(n, 0); n   vec<int> vs; n   vec<Component> components; n   function<void(int)> dfs = [&](int v) { n     u[v] = true; n     vs.push_back(v); n     for (auto to : g[v]) { n       if (!u[to]) { n         dfs(to); n       } n     } n   }; n   for (int i = 0; i < (n); ++i) { n     if (u[i]) continue; n     if (g[i].empty()) { n       continue; n     } n     vs.clear(); n     dfs(i); n     Component cc; n     cc.vs = vs; n     int cn = (int)vs.size(); n     cc.dp = makeVec2d<MR<int, int64_t, 998244353>>(1 << cn, cn + 1, 0); n     for (int mask = 0; mask < (1 << cn); ++mask) { n       bool good = true; n       for (int b1 = 0; b1 < (cn); ++b1) { n         if (getBit(mask, b1)) { n           for (int b2 = 0; b2 < (b1); ++b2) { n             if (getBit(mask, b2)) { n               if (edges.find({cc.vs[b1], cc.vs[b2]}) != edges.end()) { n                 good = false; n                 break; n               } n             } n           } n           if (!good) break; n         } n       } n       if (good) { n         cc.dp[mask][bitCnt(mask)] = 1; n       } n     }; n     ; n     for (int j = 0; j < (cn); ++j) { n       for (int b = 0; b < (cn); ++b) { n         for (int mask = 0; mask < (1 << cn); ++mask) { n           if (!getBit(mask, b)) { n             cc.dp[setBit(mask, b)][j] += cc.dp[mask][j]; n           } n         } n       } n     } n     components.push_back(cc); n   } n   auto fs = getFactorials<MR<int, int64_t, 998244353>>(n + 1); n   auto rfs = fs; n   for (int i = 0; i < (rfs.size()); ++i) { n     rfs[i] = MR<int, int64_t, 998244353>(1) / rfs[i]; n   } n   auto C = [&](int n, int k) -> MR<int, int64_t, 998244353> { n     return fs[n] * rfs[k] * rfs[n - k]; n   }; n   unordered_set<int> presentIds; n   presentIds.reserve(1 << 16); n   presentIds.max_load_factor(0.25); n   MR<int, int64_t, 998244353> res = 0; n   for (int i = (1); i < (n + 1); ++i) { n     for (const auto &e : events[i]) { n       if (e.first == 0) { n         presentIds.erase(e.second); n       } else { n         presentIds.insert(e.second); n       } n     } n     vec<MR<int, int64_t, 998244353>> dp(2 * m + 1, 0); n     dp[0] = 1; n     int inC = 0; n     for (const auto &c : components) { n       int mask = 0; n       for (int i = 0; i < c.vs.size(); ++i) { n         if (presentIds.find(c.vs[i]) != presentIds.end()) { n           mask = setBit(mask, i); n         } n       } n       inC += bitCnt(mask); n       for (int j = (int)dp.size() - 1; j >= 0; --j) { n         for (int add = (1); add < (c.vs.size() + 1); ++add) { n           if (j + add < dp.size()) { n             dp[j + add] += dp[j] * c.dp[mask][add]; n           } n         } n       } n     } n     for (int j = 0; j < (dp.size()); ++j) { n       if (presentIds.size() - inC >= i - j && i - j >= 0) { n         res += dp[j] * C(presentIds.size() - inC, i - j); n       } n     } n   } n   out(res); n } n int main() { n   solve(cin, cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long N = 2e5 + 1; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   vector<long long> nums(n); n   for (long long i = 0; i < n; i++) { n     long long k; n     cin >> k; n     nums[i] = abs(k); n   } n   sort(nums.begin(), nums.end()); n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     long long cur = nums[i]; n     long long ub = cur * 2; n     if (ub == 0) { n       continue; n     } n     long long low = i + 1; n     long long high = n - 1; n     long long pos = -1; n     while (low <= high) { n       long long mid = low + (high - low) / 2; n       if (nums[mid] <= ub) { n         pos = mid; n         low = mid + 1; n       } else { n         high = mid - 1; n       } n     } n     if (pos != -1) { n       ans += (pos - i); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n class point { n  public: n   int x, y; n   point(int _x, int _y) { n     x = _x; n     y = _y; n   } n   string str() { n     stringstream ss; n     ss <<  (  << x <<  ,   << y <<  ) ; n     return ss.str(); n   } n }; n int main() { n   int n; n   cin >> n; n   point min = point(31400, 31400), max = point(0, 0); n   int used_area = 0; n   for (int i = 0; i < n; i++) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     point p1 = point(x1, y1), p2 = point(x2, y2); n     if (x1 < min.x) min.x = x1; n     if (y1 < min.y) min.y = y1; n     if (x2 > max.x) max.x = x2; n     if (y2 > max.y) max.y = y2; n     used_area += (x2 - x1) * (y2 - y1); n   } n   int total_area = (max.x - min.x) * (max.y - min.y); n   if (used_area == total_area && (max.x - min.x) == (max.y - min.y)) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n