#include <bits/stdc++.h> n using namespace std; n const int N = 505; n char g[N][N]; n int id[N][N]; n int cnt = 1; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n int amt[N * N]; n int sz[N * N]; n int sum; n int n, k; n void dfs(int i, int j, int idx) { n   if (g[i][j] ==  X  or id[i][j]) return; n   id[i][j] = idx; n   sz[idx]++; n   for (int k = 0; k < 4; k++) dfs(i + dx[k], j + dy[k], idx); n } n void add(int c, int l, int r, bool row, bool in, bool rem = false) { n   for (int i = l; i <= r; i++) { n     int idx; n     if (row) n       idx = id[c][i]; n     else n       idx = id[i][c]; n     if (idx == 0) { n       if (in) sum = sum + !rem - rem; n     } else { n       amt[idx] = amt[idx] + !rem - rem; n       if (amt[idx] == 1 and !rem) sum += sz[idx]; n       if (amt[idx] == 0 and rem) sum -= sz[idx]; n     } n   } n } n void addEnv(int i, int j, bool rem = false) { n   add(i - 1, j, j + k - 1, true, false, rem); n   add(i + k, j, j + k - 1, true, false, rem); n   add(j - 1, i, i + k - 1, false, false, rem); n   add(j + k, i, i + k - 1, false, false, rem); n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) cin >> g[i][j]; n   for (int i = 0; i <= n + 1; i++) n     g[i][0] =  X , g[i][n + 1] =  X , g[0][i] =  X , g[n + 1][i] =  X ; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (!id[i][j] and g[i][j] !=  X ) dfs(i, j, cnt++); n   add(0, 1, k, true, false, false); n   add(k + 1, 1, k, true, false, false); n   add(0, 1, k, false, false, false); n   add(k + 1, 1, k, false, false, false); n   for (int i = 1; i <= k; i++) add(i, 1, k, true, true, false); n   int ans = sum; n   int dir = 1; n   for (int i = 1; i <= n - k + 1;) { n     int lim = n - k + 1, bg = 1; n     int j; n     if (dir == -1) swap(lim, bg); n     for (j = bg; j != lim;) { n       addEnv(i, j, true); n       if (dir == 1) n         add(j, i, i + k - 1, false, true, true), n             add(j + k, i, i + k - 1, false, true, false); n       else n         add(j + k - 1, i, i + k - 1, false, true, true), n             add(j - 1, i, i + k - 1, false, true, false); n       j += dir; n       addEnv(i, j, false); n       ans = max(ans, sum); n     } n     if (i == n - k + 1) break; n     addEnv(i, j, true); n     add(i, j, j + k - 1, true, true, true), n         add(i + k, j, j + k - 1, true, true, false); n     i++; n     dir = -dir; n     addEnv(i, j, false); n     ans = max(ans, sum); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct pair_hash { n   template <class T1, class T2> n   size_t operator()(pair<T1, T2> const &pair) const { n     size_t h1 = std::hash<T1>()(pair.first); n     size_t h2 = std::hash<T2>()(pair.second); n     return h1 ^ h2; n   } n }; n unordered_set<pair<int, int>, pair_hash> segmentos; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<pair<int, int> > a(m); n   for (int i = 0; i < m; i++) { n     cin >> a[i].first >> a[i].second; n     segmentos.insert({a[i].first, a[i].second}); n   } n   for (int i = 1; i < n; i++) { n     if (n % i) continue; n     bool solved = true; n     for (int j = 0; j < m && solved; j++) { n       int optA = a[j].first; n       int optB = a[j].second; n       optA = (a[j].first + i > n ? 1 : 0) + (a[j].first + i) % (n + 1); n       optB = (a[j].second + i > n ? 1 : 0) + (a[j].second + i) % (n + 1); n       if (segmentos.find({optA, optB}) == segmentos.end() && n           segmentos.find({optB, optA}) == segmentos.end()) { n         solved = false; n       } n     } n     if (solved) { n       printf( Yes ); n       exit(0); n     } n   } n   printf( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p, first, cnts, cntw, S, W; n int solve(long long picked) { n   int cap1 = p, cap2 = first, cnt1 = cnts, cnt2 = cntw; n   long long temp = picked; n   cap1 -= (picked * S); n   cnt1 -= picked; n   if (cap1 >= W) { n     temp += min(cap1 / W, cnt2); n     cnt2 -= min(cap1 / W, cnt2); n   } n   if (S <= W) { n     temp += min(cap2 / S, cnt1); n     cap2 -= (min(cap2 / S, cnt1) * S); n     temp += min(cap2 / W, cnt2); n   } else { n     temp += min(cap2 / W, cnt2); n     cap2 -= (min(cap2 / W, cnt2) * W); n     temp += min(cap2 / S, cnt1); n   } n   return temp; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d , &p, &first); n     scanf( %d %d , &cnts, &cntw); n     scanf( %d %d , &S, &W); n     long long ans = 0, temp; n     for (int pickedS = 0; pickedS <= min(cnts, p / S); pickedS++) { n       temp = solve(pickedS); n       ans = max(ans, temp); n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long a, b, x, y, i, k, j, t, n, s; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     if (n % 2 == 0) { n       s = n + (k * 2); n       cout << s << endl; n     } else { n       s = (k - 1) * 2; n       a = 0; n       for (i = 3; i * i <= n; i += 2) { n         if (n % i == 0) { n           a = i; n           break; n         } n       } n       if (a == 0) a = n; n       s = a + n + s; n       cout << s << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long p, k; n   cin >> p >> k; n   long long d = (k - p) / k; n   if ((((k - p) % k) + k) % k == 0 || (d <= 0 && p > k)) d--; n   vector<long long> res; n   res.push_back(d); n   if (k > p) return cout <<  1 n  << p, 0; n   while (1) { n     long long r = res.back(); n     d = (k - r) / k; n     if ((((k - r) % k) + k) % k == 0 || d <= 0) d--; n     res.push_back(d); n     if (d < k && d >= 0) break; n   } n   cout << res.size() + 1 << endl; n   long long last = p; n   for (int i = 0; i < res.size(); i++) { n     cout << k * res[i] + last <<    ; n     last = res[i]; n   } n   cout << d; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const long long infLL = 9000000000000000000; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline bool checkBit(long long n, int i) { return n & (1LL << i); } n inline long long setBit(long long n, int i) { return n | (1LL << i); } n inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); } n int dx[] = {0, 0, +1, -1, +1, +1, -1, -1}; n int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1}; n template <typename first, typename second> n ostream &operator<<(ostream &os, const pair<first, second> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename T> n ostream &operator<<(ostream &os, const multiset<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const map<first, second> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n void faltu() { cerr <<   n ; } n template <typename T> n void faltu(T a[], int n) { n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr <<   n ; n } n template <typename T, typename... hello> n void faltu(T arg, const hello &...rest) { n   cerr << arg <<    ; n   faltu(rest...); n } n inline int max(int a, int b) { return (a > b) ? a : b; } n struct xorSpaces { n   int Rank, dim; n   array<int, 20> basis; n   xorSpaces() { n     Rank = 0, dim = 20; n     fill((basis).begin(), (basis).end(), 0); n   } n   void addBasis(int n) { n     for (int i = dim - 1; i >= 0; i--) { n       if (n & (1 << i)) { n         if (basis[i] == 0) { n           basis[i] = n; n           Rank++; n           break; n         } else { n           n ^= basis[i]; n         } n       } n     } n   } n   xorSpaces operator+=(xorSpaces other) { n     if (other.Rank == 0) return other; n     if (Rank == 0) { n       return *this = other; n     } n     for (int i = dim - 1; i >= 0; i--) { n       if (other.basis[i]) addBasis(other.basis[i]); n     } n     return *this; n   } n   int getMax() { n     int ans = 0; n     for (int i = dim - 1; i >= 0; i--) { n       if (basis[i] and (ans & (1 << i)) == 0) ans ^= basis[i]; n     } n     return ans; n   } n }; n const int mx = 5e5 + 123; n xorSpaces table[(mx / 20) + 123][23], s[mx], p[mx]; n int a[mx]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, l, r, q; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int d = (n) / 20; n   for (int i = 0; i < n; i++) { n     if (i % 20) p[i] = p[i - 1]; n     p[i].addBasis(a[i]); n   } n   for (int i = n - 1; i >= 0; i--) { n     if (i % 20 != 19) s[i] = s[i + 1]; n     s[i].addBasis(a[i]); n   } n   for (int i = 0; i <= d; i++) { n     table[i][0] = s[i * 20]; n   } n   for (int i = 1; i <= 15; i++) { n     for (int j = 0; j + (1 << (i - 1)) <= d; j++) { n       table[j][i] = table[j][i - 1]; n       table[j][i] += (table[j + (1 << (i - 1))][i - 1]); n     } n   } n   cin >> q; n   while (q--) { n     cin >> l >> r; n     l--, r--; n     int block_l = l / 20, block_r = r / 20; n     xorSpaces ans; n     if (block_l == block_r) { n       for (int i = l; i <= r; i++) { n         ans.addBasis(a[i]); n       } n     } else { n       ans += (s[l]); n       ans += (p[r]); n       block_l++, block_r--; n       if (block_l <= block_r) { n         r = block_r, l = block_l; n         int lg = log2(r - l + 1); n         ans += (table[l][lg]); n         ans += (table[r - (1 << lg) + 1][lg]); n       } n     } n     cout << ans.getMax() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 123456789; n template <class T> n void scanint(T& x) { n   register int c = getchar(); n   x = 0; n   int m = 0; n   if (c ==  - ) { n     m = 1; n   } n   for (; (c < 48 || c > 57); c = getchar()) n     ; n   for (; c > 47 && c < 58; c = getchar()) { n     x = (x << 1) + (x << 3) + c - 48; n   } n   if (m == 1) { n     x *= -1; n   } n } n template <typename T> n void cinall(T& t) { n   cin >> t; n } n template <typename T, typename... Args> n void cinall(T& t, Args&... args) { n   cin >> t; n   cinall(args...); n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cinall(n); n   ; n   vector<int> v(n); n   vector<int> xv(n + 1); n   long long sol = 0; n   for (int i = 0; i < n; ++i) { n     cin >> v[i]; n     sol ^= v[i]; n     xv[i + 1] = xv[i] ^ i; n   } n   for (int i = 2; i <= n; ++i) { n     sol ^= xv[((n / i) % 2) * i] ^ xv[(n % i) + 1]; n   } n   cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, a[300005], ans[300005]; n vector<pair<int, int> > queries[300005]; n int tree[1200006]; n void upd(int l, int r, int pos, int x) { n   if (l == r) { n     tree[pos]++; n     return; n   } n   int mid = l + r >> 1; n   if (x <= mid) n     upd(l, mid, pos << 1, x); n   else n     upd(mid + 1, r, pos << 1 | 1, x); n   tree[pos]++; n } n int query(int l, int r, int pos, int x) { n   if (l == r) return r; n   int mid = l + r >> 1; n   if (tree[pos << 1] >= x) n     return query(l, mid, pos << 1, x); n   else n     return query(mid + 1, r, pos << 1 | 1, x - tree[pos << 1]); n } n int query1(int l, int r, int pos, int ql, int qr) { n   if (l >= ql && r <= qr) return tree[pos]; n   if (l > qr || r < ql) return 0; n   int mid = l + r >> 1; n   return query1(l, mid, pos << 1, ql, qr) + n          query1(mid + 1, r, pos << 1 | 1, ql, qr); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 0; i < q; i++) { n     int x, y; n     cin >> x >> y; n     queries[n - y].push_back({x, i}); n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] > i) n       upd(0, n, 1, 0); n     else if (a[i] == i) n       upd(0, n, 1, i); n     else { n       int x = query(0, n, 1, a[i]); n       upd(0, n, 1, x); n     } n     for (auto p : queries[i]) { n       int x = query1(0, n, 1, 0, p.first); n       ans[p.second] = i - x; n     } n   } n   for (int i = 0; i < q; i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tests; n   cin >> tests; n   for (int test = 0; test < (int)(tests); test++) { n     int n; n     cin >> n; n     map<int, int> m; n     for (int i = 0; i < (int)(n); i++) { n       int x; n       cin >> x; n       m[-x]++; n     } n     vector<int> v; n     for (auto i : m) { n       v.push_back(i.second); n     } n     int g, s, b; n     int j = 0; n     int k = 0; n     g = s = b = 0; n     vector<int> ans(3); n     g += v[0]; n     s -= v[0]; n     for (; j < n; j++) { n       s += v[j]; n       b -= v[j]; n       if (s > g) { n         break; n       } n     } n     for (; k < n; k++) { n       b += v[k]; n       if (b > g) { n         break; n       } n     } n     k++; n     while (g + s + b <= n / 2 && k < v.size()) { n       ans[0] = g; n       ans[1] = s; n       ans[2] = b; n       b += v[k]; n       k++; n     } n     cout << ans[0] <<     << ans[1] <<     << ans[2] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const long long inf64 = 1e18; n const int N = 1e6 + 5; n int n, m; n vector<int> g[N], gr[N]; n int ptr = 1; n int del[N]; n int mark[N]; n void solve() { n   while (ptr <= n && del[ptr]) ptr++; n   if (ptr > n) return; n   int v = ptr++; n   for (int to : g[v]) del[to] = 1; n   solve(); n   int any = 0; n   for (int to : gr[v]) { n     if (mark[to]) { n       any = 1; n       break; n     } n   } n   if (!any) { n     mark[v] = 1; n   } n } n int main() { n   scanf( %d %d , &n, &m); n   for (int u, v, i = 0; i < m; i++) { n     scanf( %d %d , &u, &v); n     g[u].push_back(v); n     gr[v].push_back(u); n   } n   solve(); n   printf( %d n , count(mark + 1, mark + n + 1, 1)); n   for (int i = 1; i <= n; i++) { n     if (mark[i]) { n       printf( %d  , i); n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int pre[N][32], suf[N][32], mx[N][32]; n int n, a[N]; n long long ans; n void init() { n   for (int i = 0; i < n; i++) mx[i][0] = i; n   for (int i = 1; (1 << i) < N; i++) { n     for (int j = 0; j < n; j++) { n       mx[j][i] = (a[mx[j][i - 1]] > a[mx[j + (1 << (i - 1))][i - 1]] n                       ? mx[j][i - 1] n                       : mx[j + (1 << (i - 1))][i - 1]); n     } n   } n } n int query(int l, int r) { n   int d = (int)(log((double)r - l + 1) / log(2.0)); n   return a[mx[l][d]] > a[mx[r - (1 << d) + 1][d]] ? mx[l][d] n                                                   : mx[r - (1 << d) + 1][d]; n } n void dfs(int l, int r) { n   if (l > r) return; n   int x = query(l, r); n   int L = l, R = r; n   int fl = 0, fr = 0; n   for (int i = 0; i < 32; i++) { n     if (!((a[x] >> i) & 1) && pre[x][i] != -1 && pre[x][i] >= l) { n       fl = 1; n       L = max(L, pre[x][i]); n     } n     if (!((a[x] >> i) & 1) && suf[x][i] != -1 && suf[x][i] <= r) { n       fr = 1; n       R = min(R, suf[x][i]); n     } n   } n   L -= !fl; n   R += !fr; n   ans -= 1LL * (x - L) * (R - x); n   dfs(l, x - 1); n   dfs(x + 1, r); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     for (int j = 0; j < 32; j++) { n       if (!((a[i] >> j) & 1)) { n         if (i) n           pre[i][j] = pre[i - 1][j]; n         else n           pre[i][j] = -1; n       } else n         pre[i][j] = i; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < 32; j++) { n       if (!((a[i] >> j) & 1)) { n         if (i < n - 1) n           suf[i][j] = suf[i + 1][j]; n         else n           suf[i][j] = -1; n       } else n         suf[i][j] = i; n     } n   } n   init(); n   ans = 1LL * n * (n + 1) / 2; n   dfs(0, n - 1); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m, k, dp[2020][2020]; n long long int getDp(long long int x, long long int y) { n   if (x == n) return y == k; n   if (~dp[x][y]) return dp[x][y]; n   long long int &ret = dp[x][y] = 0; n   ret = getDp(x + 1, y); n   ret = (ret + (getDp(x + 1, y + 1) * (m - 1)) % 998244353); n   ret %= 998244353; n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> k; n   memset(dp, -1, sizeof(dp)); n   cout << ((getDp(1, 0) * m) % 998244353) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1 << 19; n struct node { n   long long left, right, cnt, tot, length; n }; n struct segtree { n   node t[MX * 2]; n   node merge(node& l, node& r) { n     node ans; n     ans.left = l.left + r.left + l.length * r.cnt; n     ans.right = l.right + r.right + r.length * l.cnt; n     ans.cnt = l.cnt + r.cnt; n     ans.length = l.length + r.length; n     ans.tot = l.tot + r.tot + l.right * r.cnt + r.left * l.cnt; n     return ans; n   } n   void update(int wi, int su) { n     wi += MX; n     t[wi].cnt = su; n     while (wi > 1) { n       wi /= 2; n       t[wi] = merge(t[wi * 2], t[wi * 2 + 1]); n     } n   } n   long long read(int s, int e) { n     s += MX, e += MX; n     node l = {0}, r = {0}; n     while (s <= e) { n       if (s & 1) n         l = merge(l, t[s]), s = s / 2 + 1; n       else n         s /= 2; n       if (e & 1) n         e /= 2; n       else n         r = merge(t[e], r), e = e / 2 - 1; n     } n     return merge(l, r).tot; n   } n } t; n struct query { n   long long o, s, e; n } Q[MX]; n map<long long, long long> point; n long long D[MX], tmp[MX]; n int main() { n   int N, T; n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) n     scanf( %lld , D + i), tmp[i] = D[i], point[D[i]] = 0; n   scanf( %d , &T); n   for (int i = 0; i < T; i++) scanf( %lld%lld%lld , &Q[i].o, &Q[i].s, &Q[i].e); n   for (int i = 0; i < T; i++) { n     if (Q[i].o == 2) continue; n     tmp[Q[i].s] += Q[i].e; n     point[tmp[Q[i].s]] = 0; n   } n   int wi = 0; n   for (auto it = point.begin(), it2 = point.begin(); it != point.end(); n        ++it, wi++) { n     it->second = wi * 2; n     ++it2; n     if (it2 != point.end()) n       t.t[MX + wi * 2 + 1].length = it2->first - it->first; n   } n   for (int i = MX - 1; i >= 1; i--) { n     t.t[i].length = t.t[i * 2].length + t.t[i * 2 + 1].length; n   } n   for (int i = 1; i <= N; i++) { n     t.update(point[D[i]], 1); n   } n   for (int i = 0; i < T; i++) { n     if (Q[i].o == 1) { n       t.update(point[D[Q[i].s]], 0); n       D[Q[i].s] += Q[i].e; n       t.update(point[D[Q[i].s]], 1); n     } else { n       auto l = point.lower_bound(Q[i].s), r = point.upper_bound(Q[i].e); n       if (r == l) n         printf( 0  n ); n       else n         --r, printf( %lld n , t.read(l->second, r->second)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, mod = 998244353; n int n, p[N], c[N]; n void add(int& x, int y) { n   x += y; n   if (x >= mod) { n     x -= mod; n   } n } n int mul(int x, int y) { return (long long)x * y % mod; } n int qpow(int v, int p) { n   int result = 1; n   for (; p; p >>= 1, v = mul(v, v)) { n     if (p & 1) { n       result = mul(result, v); n     } n   } n   return result; n } n void modify(int p) { n   for (; p <= n; p += p & -p) { n     ++c[p]; n   } n } n int query(int p) { n   int result = 0; n   for (; p; p -= p & -p) { n     result += c[p]; n   } n   return result; n } n int query(int l, int r) { return query(r) - query(l - 1); } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &p[i]); n   } n   int answer = 0, t = 0, emp = 0; n   for (int i = 1; i <= n; ++i) { n     if (!~p[i]) { n       ++t; n     } n   } n   emp = t; n   add(answer, mul(mul(t, t - 1), qpow(4, mod - 2))); n   for (int i = 1; i <= n; ++i) { n     if (~p[i]) { n       add(answer, query(p[i], n)); n       modify(p[i]); n     } n   } n   t = 0; n   for (int i = 1; i <= n; ++i) { n     if (!~p[i]) { n       ++t; n     } else { n       int pos = mul(n - p[i] - query(p[i] + 1, n), qpow(emp, mod - 2)); n       add(answer, mul(pos, t)); n     } n   } n   t = 0; n   for (int i = n; i; --i) { n     if (!~p[i]) { n       ++t; n     } else { n       int pos = mul(p[i] - 1 - query(1, p[i] - 1), qpow(emp, mod - 2)); n       add(answer, mul(pos, t)); n     } n   } n   printf( %d n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n int n, q; n long long int m; n vector<long long int> mods; n long long int w[N]; n long long int phi(long long int x) { n   long long int ans = x; n   for (long long int i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       ans -= ans / i; n       while (x % i == 0) { n         x /= i; n       } n     } n   } n   if (x > 1) { n     ans -= ans / x; n   } n   return ans; n } n long long int magic(long long int x, long long int mod) { n   if (x < 50) { n     return x; n   } n   return 32 + (x - 32) % mod; n } n long long int fexp(long long int base, long long int e, long long int mod) { n   long long int ans = 1; n   while (e > 0) { n     if (e & 1) { n       ans = magic(ans * base, mod); n     } n     base = magic(base * base, mod); n     e >>= 1; n   } n   return ans; n } n long long int go(int l, int r, int on) { n   if (r == l) { n     return magic(w[l], mods[on]); n   } n   if (mods[on] == 1) { n     return magic(w[l], mods[on]); n   } n   long long int e = go(l + 1, r, on + 1); n   return fexp(w[l], e, mods[on]); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m; n   mods.push_back(m); n   while (mods.back() != 1) { n     mods.push_back(phi(mods.back())); n   } n   for (int i = 0; i < n; i++) { n     cin >> w[i]; n   } n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     cout << go(l, r, 0) % mods[0] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[1005]; n long long int M[1005][1005], T[1005][1005]; n queue<pair<long long int, long long int> > q; n long long int xr[] = {0, 0, -1, 1}; n long long int xc[] = {-1, 1, 0, 0}; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   long long int p, t, n, m, i, j, k, x, y, z, a, b, c; n   cin >> n >> m >> t; n   for (i = 0; i < n; ++i) cin >> s[i]; n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       a = 0; n       for (k = 0; k < 4; ++k) { n         x = i + xr[k]; n         y = j + xc[k]; n         if (x < 0 || x >= n || y < 0 || y >= m) continue; n         a += (s[x][y] == s[i][j]); n       } n       if (a) { n         q.push(make_pair(i, j)); n         M[i][j] = 1; n       } else n         T[i][j] = LLONG_MAX; n     } n   } n   while (!q.empty()) { n     auto it = q.front(); n     q.pop(); n     i = it.first; n     j = it.second; n     a = b = c = 0; n     for (k = 0; k < 4; ++k) { n       x = i + xr[k]; n       y = j + xc[k]; n       if (x < 0 || x >= n || y < 0 || y >= m || M[x][y]) continue; n       if (s[x][y] != s[i][j]) { n         q.push(make_pair(x, y)); n         M[x][y] = 1; n         T[x][y] = T[i][j] + 1; n       } n     } n   } n   while (t--) { n     cin >> i >> j >> p; n     i -= 1; n     j -= 1; n     if (T[i][j] > p) n       cout << s[i][j] << endl; n     else n       cout << (((p - T[i][j]) & 1) ^ (s[i][j] -  0 )) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x, y, a, b, d[2010][2010], v[2010][2010]; n bool solve(int x, int y, int k) { n   int l = max(1, x - k), r = min(x + k, n), t; n   for (int i = l; i < r + 1; i++) { n     t = k - abs(i - x); n     if (y - t > 0 && !v[i][y - t]) return a = i, b = y - t, 1; n     if (y + t <= m && !v[i][y + t]) return a = i, b = y + t, 1; n   } n   return 0; n } n int main() { n   cin >> n >> m >> k; n   for (int t = 0; t < k; t++) { n     cin >> x >> y; n     for (int i = -2; i < 3; i++) n       for (int j = -2; j < 3; j++) { n         if (x + i < 1 || x + i > n || y + j < 1 || y + j > m) continue; n         d[x][y] = max(d[x][y], d[x + i][y + j] - abs(i) - abs(j)); n       } n     while (!solve(x, y, d[x][y])) d[x][y]++; n     cout << a <<     << b << endl; n     v[a][b] = 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100100]; n int a[100100], b[100100], s[100100]; n int n, sa, sb; n double ans; n void dfs(int v, int p) { n   s[v] = 1; n   for (int i = 0; i < g[v].size(); i++) { n     int u = g[v][i]; n     if (u == p) continue; n     dfs(u, v); n     s[v] += s[u]; n     a[v] += a[u]; n     ans += 1.0 * b[v] * s[u] * a[u]; n   } n   ans += 1.0 * b[v] * (n - s[v]) * (sa - a[v]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) scanf( %d%d , a + i, b + i); n   for (int i = 1; i <= n; i++) { n     sa += a[i]; n     sb += b[i]; n   } n   dfs(1, 0); n   printf( %.12lf n , ans / sa / sb); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e9 + 7; n const int P = 1e6 + 9; n long long n, m; n long long bin_pow(int a, int x) { n   if (x == 1) return a % N; n   if (x % 2 == 0) return (bin_pow(a, x / 2) * bin_pow(a, x / 2)) % N; n   if (x % 2 != 0) return (bin_pow(a, x - 1) * (a % N)) % N; n } n int main() { n   cin >> n >> m; n   cout << bin_pow(bin_pow(2, m) - 1, n); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n const int MAX = 1e3 + 10; n vector<int> g[MAX]; n int qt[MAX]; n int dfs(int i, int p = -1) { n   int ret = 1; n   for (int j : g[i]) n     if (j != p) ret += dfs(j, i); n   return qt[i] = ret; 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     int n, x; n     cin >> n >> x; n     x--; n     for (int i = 0; i < n; i++) g[i].clear(); n     int deg = 0; n     for (int i = 0; i < n - 1; i++) { n       int a, b; n       cin >> a >> b; n       a--, b--; n       if (a == x or b == x) deg++; n       g[a].push_back(b); n       g[b].push_back(a); n     } n     if (deg <= 1) { n       cout <<  Ayush  <<   n ; n       continue; n     } n     dfs(x); n     vector<int> v; n     for (int i : g[x]) v.push_back(qt[i]); n     int par = 0; n     for (int i : v) par += (i % 2 == 0); n     if (par % 2 != deg % 2) n       cout <<  Ayush  <<   n ; n     else n       cout <<  Ashish  <<   n ; n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000]; n int m[100001]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   m[n] = 1e9 + 1; n   for (int i = n - 1; i >= 0; i--) { n     m[i] = min(m[i + 1], a[i]); n   } n   for (int i = 0; i < n; i++) { n     cout << (lower_bound(m + i + 1, m + n, a[i]) - m - i - 2) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int x, s = 0, t = 0, m, ans = 0, hp = 0; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   if (a[0] == 1) n     t++; n   else n     s++; n   for (int i = 1; i < n; i++) { n     if (a[i] != a[i - 1] && hp == 0) { n       hp = 1; n     } n     if (a[i] != a[i - 1] && hp == 1) { n       m = min(s, t); n       if (ans < m) ans = m; n       if (a[i] == 1) n         t = 0; n       else n         s = 0; n     } n     if (a[i] == 1) { n       t++; n     } else if (a[i] == 2) { n       s++; n     } n   } n   m = min(s, t); n   if (ans < m) ans = m; n   cout << ans * 2 << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n inline void boost() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const long long maxn = 2e6 + 123; n const long long inf = 1e9 + 123; n const long long linf = 1e18 + 123; n const long long mod = 1e9 + 7; n const double eps = 1e-9; n const double pi = acos(-1); n int dx[8] = {0, 1, -1, 0, 1, -1, 1, -1}; n int dy[8] = {1, 0, 0, -1, 1, 1, -1, -1}; n long long a[maxn], b[maxn]; n vector<pair<long long, long long> > v[maxn]; n int main() { n   boost(); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] += a[i - 1]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n     b[i] += b[i - 1]; n   } n   bool WAS = 0; n   if (a[n] > b[n]) { n     swap(a, b); n     WAS = 1; n   } n   for (int i = 1; i <= n; i++) { n     int j = lower_bound(b + 1, b + 1 + n, a[i]) - b; n     if (a[i] != b[j]) { n       j--; n     } n     v[a[i] - b[j]].push_back({i, j}); n   } n   if (v[0].size() >= 1) { n     if (WAS == 0) { n       cout << v[0][0].first <<   n ; n       for (int i = 1; i <= v[0][0].first; i++) { n         cout << i <<    ; n       } n       cout <<   n ; n       cout << v[0][0].second <<   n ; n       for (int i = 1; i <= v[0][0].second; i++) { n         cout << i <<    ; n       } n       cout <<   n ; n     } else { n       cout << v[0][0].second <<   n ; n       for (int i = 1; i <= v[0][0].second; i++) { n         cout << i <<    ; n       } n       cout <<   n  << v[0][0].first <<   n ; n       for (int i = 1; i <= v[0][0].first; i++) { n         cout << i <<    ; n       } n       cout <<   n ; n     } n     return 0; n   } n   for (int i = 1; i < n; i++) { n     if (v[i].size() >= 2) { n       if (WAS == 0) { n         cout << v[i][1].first - v[i][0].first <<   n ; n         for (int j = v[i][0].first + 1; j <= v[i][1].first; j++) { n           cout << j <<    ; n         } n         cout <<   n  << v[i][1].second - v[i][0].second <<   n ; n         for (int j = v[i][0].second + 1; j <= v[i][1].second; j++) { n           cout << j <<    ; n         } n         return 0; n       } else { n         cout << v[i][1].second - v[i][0].second <<   n ; n         for (int j = v[i][0].second + 1; j <= v[i][1].second; j++) { n           cout << j <<    ; n         } n         cout <<   n  << v[i][1].first - v[i][0].first <<   n ; n         for (int j = v[i][0].first + 1; j <= v[i][1].first; j++) { n           cout << j <<    ; n         } n         return 0; n       } n     } n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n void mejor(int player, long long int k, int *arr, int n) { n   if (player == 0) { n     if (player + k >= n - 1) n       cout << n << endl; n     else { n       for (int i = 0; i < k; i++) { n         if (arr[player] < arr[player + 1 + i]) { n           mejor(player + 1 + i, k, arr, n); n           return; n         } n       } n       cout << arr[player] << endl; n     } n   } else { n     if (player + k > n - 1) n       cout << n << endl; n     else { n       for (int i = 0; i < k - 1; i++) { n         if (arr[player] < arr[player + 1 + i]) { n           mejor(player + 1 + i, k, arr, n); n           return; n         } n       } n       cout << arr[player] << endl; n     } n   } n   return; n } n int main() { n   int n; n   long long int k; n   cin >> n >> k; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   if (k >= n - 1) n     cout << n << endl; n   else n     mejor(0, k, arr, n); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int len, i, l = 0, r = 0, u = 0, d = 0, dif1, dif2; n   scanf( %d , &len); n   char str[101]; n   scanf(  %s , str); n   for (i = 0; i < len; i++) { n     if (str[i] ==  L ) n       l++; n     else if (str[i] ==  R ) n       r++; n     else if (str[i] ==  U ) n       u++; n     else if (str[i] ==  D ) n       d++; n   } n   dif1 = l - r; n   if (dif1 < 0) dif1 = -dif1; n   dif2 = u - d; n   if (dif2 < 0) dif2 = -dif2; n   printf( %d n , (len - (dif1 + dif2))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const int MOD = (int)1e9 + 7; n const int N = (int)1e6 + 7; n const int M = (int)-1; n int a[N], cnt[100]; n int main() { n   int T; n   cin >> T; n   while (T--) { n     long long n; n     int m; n     cin >> n >> m; n     for (int i = (0); i < (m); ++i) cin >> a[i]; n     long long sum = 0; n     for (int i = (0); i < (m); ++i) sum += a[i]; n     if (sum < n) { n       cout << -1 << endl; n       continue; n     } n     sort(a, a + m); n     memset(cnt, 0, sizeof(cnt)); n     for (int i = 0, j = 0; i < m; i++) { n       while (a[i] > (1ll << j)) j++; n       cnt[j]++; n     } n     long long res = 0; n     int ans = 0; n     for (int i = (0); i < (63); ++i) { n       if (n >> i & 1) { n         if (!cnt[i] && res < (1ll << i)) { n           int j = i + 1; n           cnt[i] = 2; n           while (!cnt[j]) { n             cnt[j]++; n             j++; n             ans++; n           } n           cnt[j]--; n           ans++; n         } n         if (cnt[i]) n           cnt[i]--; n         else n           res -= 1ll << i; n       } n       res += (1ll << i) * cnt[i]; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n const int seed = 131; n char s[100005], t[1000005]; n int hshv[1000005]; n int powm(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = 1LL * ret * a % mod; n     a = 1LL * a * a % mod; n     b >>= 1; n   } n   return ret; n } n void gethash(char* s) { n   int len = strlen(s), now; n   now = 0; n   for (int i = 0; i < len; i++) { n     now = (1LL * now * seed + s[i]) % mod; n     hshv[i] = now; n   } n } n int get(int s, int t) { n   int a, b; n   if (s == 0) n     a = 0; n   else n     a = 1LL * hshv[s - 1] * powm(seed, t - s + 1) % mod; n   b = hshv[t]; n   return (b - a + mod) % mod; n } n int main() { n   int cnt0, cnt1, lens, lent, len0, len1, hsh0, hsh1, p, ans; n   bool f; n   scanf( %s , s); n   scanf( %s , t); n   lens = strlen(s); n   lent = strlen(t); n   if (s[0] ==  1 ) n     for (int i = 0; i < lens; i++) s[i] = ((s[i] -  0 ) ^ 1) +  0 ; n   cnt0 = 0; n   cnt1 = 0; n   for (int i = 0; i < lens; i++) n     if (s[i] ==  0 ) n       cnt0++; n     else n       cnt1++; n   ans = 0; n   gethash(t); n   for (int i = 1; cnt0 * i < lent; i++) { n     if ((lent - cnt0 * i) % cnt1 != 0) continue; n     len0 = i; n     len1 = (lent - cnt0 * len0) / cnt1; n     hsh0 = -1; n     hsh1 = -1; n     p = 0; n     f = true; n     for (int j = 0; j < lens; j++) { n       if (s[j] ==  0 ) { n         if (hsh0 == -1) n           hsh0 = get(p, p + len0 - 1); n         else if (get(p, p + len0 - 1) != hsh0) { n           f = false; n           break; n         } n         p += len0; n       } else { n         if (hsh1 == -1) n           hsh1 = get(p, p + len1 - 1); n         else if (get(p, p + len1 - 1) != hsh1) { n           f = false; n           break; n         } n         p += len1; n       } n     } n     if (f && hsh0 != hsh1) ans++; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n template <typename T> n void ckmn(T& a, T b) { n   a = min(a, b); n } n template <typename T> n void ckmx(T& a, T b) { n   a = max(a, b); n } n void rd(int& x) { scanf( %i , &x); } n void rd(long long& x) { scanf( %lld , &x); } n void rd(char* x) { scanf( %s , x); } n void rd(double& x) { scanf( %lf , &x); } n void rd(string& x) { scanf( %s , &x); } n template <typename T1, typename T2> n void rd(pair<T1, T2>& x) { n   rd(x.first); n   rd(x.second); n } n template <typename T> n void rd(vector<T>& x) { n   for (T& i : x) rd(i); n } n template <typename T, typename... A> n void rd(T& x, A&... args) { n   rd(x); n   rd(args...); n } n template <typename T> n void rd() { n   T x; n   rd(x); n   return x; n } n int ri() { n   int x; n   rd(x); n   return x; n } n template <typename T> n vector<T> rv(int n) { n   vector<T> x(n); n   rd(x); n   return x; n } n template <typename T> n void ra(T a[], int n, int st = 1) { n   for (int i = 0; i < n; ++i) rd(a[st + i]); n } n template <typename T1, typename T2> n void ra(T1 a[], T2 b[], int n, int st = 1) { n   for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]); n } n template <typename T1, typename T2, typename T3> n void ra(T1 a[], T2 b[], T3 c[], int n, int st = 1) { n   for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]), rd(c[st + i]); n } n void re(vector<int> E[], int m, bool dir = 0) { n   for (int i = 0, u, v; i < m; ++i) { n     rd(u, v); n     E[u].push_back(v); n     if (!dir) E[v].push_back(u); n   } n } n template <typename T> n void re(vector<pair<int, T>> E[], int m, bool dir = 0) { n   for (int i = 0, u, v; i < m; ++i) { n     T w; n     rd(u, v, w); n     E[u].push_back({v, w}); n     if (!dir) E[v].push_back({u, w}); n   } n } n int main() { n   for (int t = ri(); t--;) { n     int n = ri(); n     vector<int> a(n + 1, 0); n     vector<vector<int>> ids(n + 1, vector<int>()); n     for (int i = 1; i <= n; i++) { n       rd(a[i]); n       ids[a[i]].push_back(i); n     } n     vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0)); n     vector<vector<bool>> was(n + 1, vector<bool>(n + 1, false)); n     function<int(int, int)> Solve = [&](int l, int r) { n       if (l > r) return 0; n       if (was[l][r]) return dp[l][r]; n       was[l][r] = 1; n       if (l == r) return dp[l][r] = 1; n       dp[l][r] = Solve(l + 1, r) + 1; n       for (int i : ids[a[l]]) n         if (i > l && i <= r) { n           ckmn(dp[l][r], Solve(l + 1, i - 1) + Solve(i, r)); n         } n       return dp[l][r]; n     }; n     printf( %i n , Solve(1, n) - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int p[100001]; n int sz[100001]; n int bc[100001]; n int top[100001]; n int newi[100001]; n int sr[100001]; n vector<int> c[100001]; n pair<int, int> zero = {0, -1e9}; n pair<int, int> f(pair<int, int> x, pair<int, int> y) { n   return {x.first + y.first, max(y.second, x.second + y.first)}; n } n pair<int, int> info[262144]; n bool tag[262144]; n int lazy[262144]; n void put(int id, int l, int r, int v) { n   tag[id] = true; n   lazy[id] = v; n   info[id] = {v * (r - l + 1), max(v * (r - l + 1), v)}; n } n void push(int id, int l, int r) { n   if (!tag[id]) return; n   int mid = (l + r) / 2; n   put(id * 2, l, mid, lazy[id]); n   put(id * 2 + 1, mid + 1, r, lazy[id]); n   tag[id] = false; n } n void upd1(int id, int l, int r, int ql, int qr, int v) { n   if (ql > r || qr < l) return; n   if (ql <= l && r <= qr) { n     put(id, l, r, v); n     return; n   } n   push(id, l, r); n   int mid = (l + r) / 2; n   upd1(id * 2, l, mid, ql, qr, v); n   upd1(id * 2 + 1, mid + 1, r, ql, qr, v); n   info[id] = f(info[id * 2], info[id * 2 + 1]); n } n void upd2(int id, int l, int r, int pos, int v) { n   if (l == r) { n     info[id] = {info[id].first + v, info[id].second + v}; n     return; n   } n   push(id, l, r); n   int mid = (l + r) / 2; n   if (pos <= mid) n     upd2(id * 2, l, mid, pos, v); n   else n     upd2(id * 2 + 1, mid + 1, r, pos, v); n   info[id] = f(info[id * 2], info[id * 2 + 1]); n } n pair<int, int> qry(int id, int l, int r, int ql, int qr) { n   if (ql > r || qr < l) return zero; n   if (ql <= l && r <= qr) return info[id]; n   push(id, l, r); n   int mid = (l + r) / 2; n   return f(qry(id * 2, l, mid, ql, qr), qry(id * 2 + 1, mid + 1, r, ql, qr)); n } n int g(int id) { n   pair<int, int> res = zero; n   while (id != 0) { n     res = f(qry(1, 1, n, newi[top[id]], newi[id]), res); n     id = p[top[id]]; n   } n   return res.second; n } n void dfs(int id) { n   sz[id] = 1; n   for (auto cur : c[id]) { n     dfs(cur); n     sz[id] += sz[cur]; n     if (sz[bc[id]] < sz[cur]) bc[id] = cur; n   } n } n int ptr = 0; n void dfs2(int id) { n   newi[id] = ++ptr; n   sr[id] = ptr; n   if (c[id].empty()) return; n   top[bc[id]] = top[id]; n   dfs2(bc[id]); n   sr[id] = sr[bc[id]]; n   for (auto cur : c[id]) { n     if (cur != bc[id]) { n       top[cur] = cur; n       dfs2(cur); n       sr[id] = sr[cur]; n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> q; n   for (int i = 2; i <= n; i++) { n     cin >> p[i]; n     c[p[i]].push_back(i); n   } n   dfs(1); n   top[1] = 1; n   dfs2(1); n   upd1(1, 1, n, 1, n, -1); n   for (int i = 1; i <= q; i++) { n     int t, u; n     cin >> t >> u; n     if (t == 1) n       upd2(1, 1, n, newi[u], 1); n     else if (t == 2) { n       upd1(1, 1, n, newi[u], sr[u], -1); n       int num = g(u); n       if (num >= 0) upd2(1, 1, n, newi[u], -num - 1); n     } else { n       int num = g(u); n       if (num >= 0) n         cout <<  black n ; n       else n         cout <<  white n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ch[300010][26]; n int ans[300010], maxdep = 0; n int N; n int merge(vector<int> now) { n   int res = now.size() - 1; n   vector<int> next; n   for (int i = 0; i < 26; i++) { n     next.clear(); n     for (int j = 0; j < now.size(); j++) n       if (ch[now[j]][i]) next.push_back(ch[now[j]][i]); n     if (next.size() <= 1) continue; n     res += merge(next); n   } n   return res; n } n void dfs(int x, int dep) { n   vector<int> now; n   now.clear(); n   maxdep = max(maxdep, dep); n   for (int i = 0; i < 26; i++) { n     if (!ch[x][i]) continue; n     now.push_back(ch[x][i]); n   } n   int res = merge(now) + 1; n   ans[dep] += res; n   for (int i = 0; i < 26; i++) { n     if (ch[x][i]) dfs(ch[x][i], dep + 1); n   } n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i < N; i++) { n     int u, v; n     char s[5]; n     scanf( %d%d , &u, &v); n     scanf( %s , s); n     ch[u][s[0] -  a ] = v; n   } n   memset(ans, 0, sizeof(ans)); n   dfs(1, 1); n   int maxres = 0, mp; n   for (int i = 1; i <= maxdep; i++) n     if (maxres < ans[i]) { n       maxres = ans[i]; n       mp = i; n     } n   printf( %d n%d n , N - maxres, mp); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n int par[maxn], sub[maxn], col[maxn]; n vector<pair<int, int>> parHis, colHis, subHis; n bool bad[maxn]; n int c[maxn]; n int root(int x) { n   while (x != par[x]) { n     x = par[x]; n   } n   return x; n } n int getCol(int x) { n   int res = 0; n   while (x != par[x]) { n     res ^= col[x]; n     x = par[x]; n   } n   return res; n } n void save(int x, int y) { n   parHis.emplace_back(x, par[x]); n   subHis.emplace_back(y, sub[y]); n   colHis.emplace_back(x, col[x]); n } n void join(int x, int y, bool rollback = false) { n   int rx = root(x); n   int ry = root(y); n   int dif = (getCol(x) == getCol(y)); n   if (sub[rx] > sub[ry]) swap(rx, ry); n   if (rollback) save(rx, ry); n   par[rx] = ry; n   sub[ry] += sub[rx]; n   col[rx] = dif; n } n bool check(int p, int q) { n   if (root(p) == root(q) && getCol(p) == getCol(q)) return false; n   return true; n } n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &c[i]); n   } n   map<pair<int, int>, vector<pair<int, int>>> cont; n   for (int i = 1; i <= m; i++) { n     int p, q; n     scanf( %d %d , &p, &q); n     if (c[p] > c[q]) swap(p, q); n     cont[make_pair(c[p], c[q])].emplace_back(p, q); n   } n   for (int i = 1; i <= n; i++) { n     par[i] = i; n     col[i] = 0; n     sub[i] = 1; n   } n   int active = k; n   for (int i = 1; i <= k; i++) { n     auto v = cont[make_pair(i, i)]; n     for (auto j : v) { n       int p = j.first; n       int q = j.second; n       if (check(p, q)) { n         join(p, q); n       } else { n         bad[i] = true; n         active -= 1; n         break; n       } n     } n   } n   long long ans = (1LL * active * (active - 1)) / 2; n   for (auto i : cont) { n     if (i.first.first == i.first.second) continue; n     if (bad[i.first.first] || bad[i.first.second]) continue; n     auto v = i.second; n     for (auto j : v) { n       int p = j.first; n       int q = j.second; n       if (check(p, q)) { n         join(p, q, true); n       } else { n         ans -= 1; n         break; n       } n     } n     while (not parHis.empty()) { n       auto ph = parHis.back(); n       auto ch = colHis.back(); n       auto sh = subHis.back(); n       parHis.pop_back(); n       colHis.pop_back(); n       subHis.pop_back(); n       par[ph.first] = ph.second; n       col[ch.first] = ch.second; n       sub[sh.first] = sh.second; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using Int = long long; n constexpr static int mod = 1e9 + 7; n constexpr static int inf = (1 << 30) - 1; n constexpr static Int infll = (1LL << 61) - 1; n int Competitive_Programming = n     (ios_base::sync_with_stdio(false), cin.tie(nullptr), n      cout << fixed << setprecision(15), 0); n #pragma GCC target( avx2 ) n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n int main() { n   Int N, D, M; n   cin >> N >> D >> M; n   vector<Int> a(N); n   vector<Int> low, high; n   for (int i = 0; i < N; i++) { n     cin >> a[i]; n     if (a[i] > M) n       high.push_back(a[i]); n     else n       low.push_back(a[i]); n   } n   sort(low.rbegin(), low.rend()); n   sort(high.rbegin(), high.rend()); n   vector<Int> lowsum(low.size() + 1), highsum(high.size() + 1); n   for (int i = 0; i < low.size(); i++) lowsum[i + 1] = lowsum[i] + low[i]; n   for (int i = 0; i < high.size(); i++) highsum[i + 1] = highsum[i] + high[i]; n   Int ans = 0, hsz = highsum.size(), lsz = lowsum.size(); n   for (int i = 0; i < lsz; i++) n     ans = max(ans, highsum[min(hsz - 1, (N - i + D) / (D + 1))] + lowsum[i]); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500001; n const int sqN = 708; n int ans[N]; n int fre[N]; n int v[N]; n int block[sqN]; n int answer = 0; n struct query { n   int l; n   int r; n   int idx; n } Q[N]; n bool comp(query &q1, query &q2) { n   if (q1.l / sqN != q2.l / sqN) { n     return q1.l < q2.l; n   } n   if ((q1.l / sqN) & 1) { n     return q1.r < q2.r; n   } n   return q1.r > q2.r; n } n inline void add(int idx) { n   int val = v[idx]; n   fre[val]++; n   if (fre[val] == 1) { n     answer++; n     block[val / sqN]++; n   } n   if (fre[val] == 2) { n     answer--; n     block[val / sqN]--; n   } n } n inline void rem(int idx) { n   int val = v[idx]; n   fre[val]--; n   if (fre[val] == 1) { n     answer++; n     block[val / sqN]++; n   } n   if (fre[val] == 0) { n     answer--; n     block[val / sqN]--; n   } n } n inline int solve() { n   if (answer == 0) { n     return 0; n   } n   for (int i = 0; i < sqN; i++) { n     if (block[i] > 0) { n       for (int j = i * sqN; j < i * sqN + sqN; j++) { n         if (fre[j] == 1) { n           return j; n         } n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   int q, l, r; n   cin >> q; n   for (int i = 0; i < q; i++) { n     cin >> l >> r; n     l--; n     r--; n     Q[i].l = l; n     Q[i].r = r; n     Q[i].idx = i; n   } n   sort(Q, Q + q, comp); n   l = 0, r = -1; n   for (int i = 0; i < q; i++) { n     int L = Q[i].l; n     int R = Q[i].r; n     while (l < L) { n       rem(l); n       l++; n     } n     while (l > L) { n       l--; n       add(l); n     } n     while (r < R) { n       r++; n       add(r); n     } n     while (r > R) { n       rem(r); n       r--; n     } n     ans[Q[i].idx] = solve(); n   } n   for (int i = 0; i < q; i++) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5000; n long long cumulative[N + 10], arr[N + 10][3]; n signed main() { n   ios ::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, q; n   cin >> n >> q; n   vector<pair<long long, long long> > pi; n   for (long long i = 0; i < q; i++) { n     long long a, b; n     cin >> a >> b; n     cumulative[a] += 1; n     cumulative[b + 1] -= 1; n     pi.push_back({a, b}); n   } n   for (long long i = 1; i <= n; i++) { n     cumulative[i] += cumulative[i - 1]; n   } n   for (long long i = 1; i <= n; i++) { n     for (long long j = 0; j < 3; j++) { n       if (cumulative[i] == j) { n         arr[i][j] += (arr[i - 1][j] + 1); n       } else { n         arr[i][j] += arr[i - 1][j]; n       } n     } n   } n   long long sz = pi.size(); n   sort(pi.begin(), pi.end()); n   long long ans = 0; n   for (long long i = 0; i < sz - 1; i++) { n     long long x = pi[i].first; n     long long y = pi[i].second; n     for (long long j = i + 1; j < sz; j++) { n       long long two = 0; n       long long a = pi[j].first; n       long long b = pi[j].second; n       if (a <= y) { n         long long ax = max(x, a); n         long long by = min(y, b); n         two = (arr[by][2] - arr[ax - 1][2]); n       } n       long long zero = (arr[n][0] - arr[0][0]); n       long long one = (arr[y][1] - arr[x - 1][1]) + (arr[b][1] - arr[a - 1][1]); n       long long total = one + two + zero; n       long long res = n - total; n       ans = max(ans, res); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int test = 1; n   for (int loop = 0; loop < test; loop++) { n     long long int num, val; n     cin >> num >> val; n     vector<long long int> arr; n     long long int temp; n     for (int i = 0; i < num; i++) { n       cin >> temp; n       arr.push_back(temp); n     } n     long long int count = 0; n     for (int i = 0; i < num; i++) { n       for (int j = 0; j < num; j++) { n         if (abs(arr[i] - arr[j]) <= val && i != j) count++; n       } n     } n     cout << count <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n const int dx[4] = {0, 0, -1, 1}; n const int dy[4] = {-1, 1, 0, 0}; n const int N = 101; n int n, m, a[N][N], psum[N][N], T; n bool Check_1() { n   if (psum[1][m] == 0) return true; n   if (psum[n][1] == 0) return true; n   if (psum[n][m] - psum[n - 1][m] == 0) return true; n   if (psum[n][m] - psum[n][m - 1] == 0) return true; n   return false; n } n bool Check_2() { n   if (a[1][1] == 0 || a[1][m] == 0 || a[n][1] == 0 || a[n][m] == 0) return true; n   for (int i = 2; i < n; i++) { n     if (psum[i][m] - psum[i - 1][m] == 0) return true; n   } n   for (int i = 2; i < m; i++) { n     if (psum[n][i] - psum[n][i - 1] == 0) return true; n   } n   return false; n } n bool Check_3() { n   for (int i = 2; i < n; i++) { n     if (a[i][1] == 0 || a[i][m] == 0) return true; n   } n   for (int i = 2; i < m; i++) { n     if (a[1][i] == 0 || a[n][i] == 0) return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> T; n   while (T--) { n     cin >> n >> m; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         char c; n         cin >> c; n         int x = 0; n         if (c ==  P ) x = 1; n         psum[i][j] = psum[i - 1][j] + psum[i][j - 1] - psum[i - 1][j - 1] + x; n         a[i][j] = x; n       } n     } n     if (psum[n][m] == n * m) { n       cout <<  MORTAL n ; n       continue; n     } n     if (psum[n][m] == 0) { n       cout << 0 <<   n ; n       continue; n     } n     if (Check_1()) n       cout << 1 <<   n ; n     else if (Check_2()) n       cout << 2 <<   n ; n     else if (Check_3()) n       cout << 3 <<   n ; n     else n       cout << 4 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct P { n   long long x, y; n }; n P rotate90(P a) { n   P b; n   b.x = -a.y, b.y = a.x; n   return b; n } n P ar[4], br[4], cr[4]; n P add(P a, P b) { n   P c; n   c.x = a.x + b.x; n   c.y = a.y + b.y; n   return c; n } n long long dis(P a, P b) { n   return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); n } n int check(P eop[]) { n   long long l[16], tp = 0; n   for (int i = 0; i < 4; i++) n     for (int j = i + 1; j < 4; j++) l[tp++] = dis(eop[i], eop[j]); n   sort(l, l + tp); n   if (l[0] == l[1] && l[1] == l[2] && l[2] == l[3] && l[0] == l[3] && l[0] && n       l[4] == l[5] && l[0] * 2 == l[4]) n     return 1; n   else n     return 0; n } n int main() { n   int n; n   scanf( %d , &n); n   while (n--) { n     for (int i = 0; i < 4; i++) { n       scanf( %I64d%I64d%I64d%I64d , &ar[i].x, &ar[i].y, &br[i].x, &br[i].y); n       cr[i].x = ar[i].x - br[i].x; n       cr[i].y = ar[i].y - br[i].y; n     } n     int ans = 999; n     for (int i = 0; i < 4; i++) { n       for (int j = 0; j < 4; j++) { n         for (int k = 0; k < 4; k++) { n           for (int l = 0; l < 4; l++) { n             P eop[4]; n             for (int z = 0; z < 4; z++) eop[z] = cr[z]; n             for (int z = 0; z < i; z++) eop[0] = rotate90(eop[0]); n             for (int z = 0; z < j; z++) eop[1] = rotate90(eop[1]); n             for (int z = 0; z < k; z++) eop[2] = rotate90(eop[2]); n             for (int z = 0; z < l; z++) eop[3] = rotate90(eop[3]); n             for (int z = 0; z < 4; z++) eop[z] = add(eop[z], br[z]); n             if (check(eop)) ans = min(ans, i + j + k + l); n           } n         } n       } n     } n     printf( %d n , ans == 999 ? -1 : ans); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long int n, m, sx, sy, vx, vy; n inline long long int extgcd(long long int a, long long int b, long long int &x, n                             long long int &y) { n   if (!b) { n     x = 1; n     y = 0; n     return a; n   } n   long long int d = extgcd(b, a % b, x, y); n   long long int _x = y; n   long long int _y = x - (a / b) * y; n   x = _x; n   y = _y; n   return d; n } n inline long long int crt(long long int a1, long long int m1, long long int a2, n                          long long int m2) { n   a1 = (a1 + m1) % m1; n   a2 = (a2 + m2) % m2; n   if (a1 > a2) { n     swap(a1, a2); n     swap(m1, m2); n   } n   long long int x, y; n   long long int d = extgcd(m1, m2, x, y); n   if ((a2 - a1) % d) return -1; n   x *= (a2 - a1) / d; n   y *= -(a2 - a1) / d; n   x = (x - (x / (m2 / d)) * m2 / d + m2 / d) % (m2 / d); n   return m1 * x + a1; n } n signed main() { n   cin >> n >> m >> sx >> sy >> vx >> vy; n   if ((sx == 0 && sy == 0) || (sx == 0 && sy == m) || (sx == n && sy == 0) || n       (sx == n && sy == m)) { n     cout << sx <<     << sy << endl; n     return 0; n   } n   if (!vx) { n     if (sx == 0) { n       if (vy == 1) { n         cout <<  0   << m << endl; n       } else { n         cout <<  0 0 n  << endl; n       } n     } else if (sx == n) { n       if (vy == 1) { n         cout << n <<     << m << endl; n       } else { n         cout << n <<     << 0 << endl; n       } n     } else { n       cout << -1 << endl; n     } n     return 0; n   } n   if (!vy) { n     if (sy == 0) { n       if (vx == 1) { n         cout << n <<   0  << endl; n       } else { n         cout <<  0 0 n  << endl; n       } n     } else if (sy == m) { n       if (vx == 1) { n         cout << n <<     << m << endl; n       } else { n         cout <<  0   << m << endl; n       } n     } else { n       cout << -1 << endl; n     } n     return 0; n   } n   long long int T = crt(n - vx * sx, n, m - vy * sy, m); n   if (T == -1) { n     cout << -1 << endl; n     return 0; n   } n   long long int X = sx + T * vx; n   long long int Y = sy + T * vy; n   X = (X < 0 ? -X : X); n   Y = (Y < 0 ? -Y : Y); n   cout << ((X / n) & 1) * n <<     << ((Y / m) & 1) * m << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:500000000 ) n using namespace std; n bool isq[22 * 22 * 22 * 22]; n bool isSquare(int n) { return isq[n]; } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= 22 * 22; i++) isq[i * i] = true; n   for (int columnLine = 0; columnLine <= m; columnLine++) { n     for (int rowLine = 0; rowLine <= n; rowLine++) { n       for (int aValue = 1; aValue <= 22; aValue++) n         for (int bValue = 1; bValue <= 22; bValue++) n           for (int cValue = 1; cValue <= 22; cValue++) n             for (int dValue = 1; dValue <= 22; dValue++) { n               int sqA = aValue * aValue; n               int sqB = bValue * bValue; n               int sqC = cValue * cValue; n               int sqD = dValue * dValue; n               if (isSquare(columnLine * sqA + (m - columnLine) * sqB) && n                   isSquare(columnLine * sqC + (m - columnLine) * sqD) && n                   isSquare(rowLine * sqA + (n - rowLine) * sqC) && n                   isSquare(rowLine * sqB + (n - rowLine) * sqD)) { n                 for (int i = 0; i < n; i++) { n                   for (int j = 0; j < m; j++) { n                     if (i < rowLine && j < columnLine) n                       printf( %d  , aValue); n                     else if (i < rowLine && j >= columnLine) n                       printf( %d  , bValue); n                     else if (i >= rowLine && j < columnLine) n                       printf( %d  , cValue); n                     else n                       printf( %d  , dValue); n                   } n                   puts(  ); n                 } n                 return 0; n               } n             } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 5, M = 2e6 + 5, Mod = 1e9 + 7; n int add(int a, int b) { return ((a += b) >= Mod) ? (a - Mod) : (a); } n int gcd(int a, int b) { return (!b) ? (a) : (gcd(b, a % b)); } n bool _pool[M << 1]; n bool *_top = _pool; n bool *alloc(int sz) { n   bool *top = _top; n   _top += sz; n   return top; n } n int n, a, b; n char s[M]; n int sz[N]; n bool *own[N]; n bool G[N][N]; n stack<int> S; n vector<int> SCC; n bool ins[N], vis[M]; n int dfs_clock, cnt_scc; n int dfn[N], low[N], bel[N]; n int sz_sc[N]; n bool *own_sc[N]; n void dispose() { n   int D = 0; n   for (auto p : SCC) { n     D = gcd(D, sz[p]); n   } n   sz_sc[cnt_scc] = D; n   own_sc[cnt_scc] = alloc(D); n   bool *app = own_sc[cnt_scc]; n   for (auto &p : SCC) { n     for (int i = 0; i < sz[p]; i++) { n       if (own[p][i]) { n         app[i % D] = true; n       } n     } n   } n } n void tarjan(int p) { n   dfn[p] = low[p] = ++dfs_clock; n   vis[p] = ins[p] = true; n   S.push(p); n   for (int e = 1; e <= n; e++) { n     if (G[p][e]) { n       if (!vis[e]) { n         tarjan(e); n         low[p] = min(low[p], low[e]); n       } else if (ins[e]) { n         low[p] = min(low[p], dfn[e]); n       } n     } n   } n   if (dfn[p] == low[p]) { n     int cur; n     SCC.clear(); n     ++cnt_scc; n     do { n       cur = S.top(); n       S.pop(); n       SCC.push_back(cur); n       bel[cur] = cnt_scc; n       ins[cur] = false; n     } while (cur != p); n     dispose(); n   } n } n int deg[N]; n int order[N]; n bool nG[N][N]; n void rebuild() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (bel[i] != bel[j] && G[i][j]) { n         nG[bel[i]][bel[j]] = true; n       } n     } n   } n   for (int i = 1; i <= cnt_scc; i++) { n     for (int j = i + 1; j <= cnt_scc; j++) { n       if (nG[i][j]) { n         deg[j]++; n       } else { n         deg[i]++; n       } n     } n   } n   for (int i = 1; i <= cnt_scc; i++) { n     order[deg[i]] = i; n   } n } n pair<int, int> A[N]; n int comb[N][N]; n int main() { n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= n; j++) { n       G[i][j] = s[j] -  0 ; n     } n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , sz + i); n     own[i] = alloc(sz[i]); n     scanf( %s , s); n     for (int j = 0; j < sz[i]; j++) { n       own[i][j] = s[j] -  0 ; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       tarjan(i); n     } n   } n   rebuild(); n   for (int i = 1; i < cnt_scc; i++) { n     int x = order[i - 1], y = order[i]; n     int d = gcd(sz_sc[x], sz_sc[y]); n     memset(vis, false, d); n     for (int j = 0; j < sz_sc[x]; j++) { n       if (own_sc[x][j]) { n         vis[j % d] = true; n       } n     } n     for (int j = 0; j < sz_sc[y]; j++) { n       own_sc[y][j] = own_sc[y][j] || vis[j % d]; n     } n   } n   for (int i = 1; i <= n; i++) { n     bool *app = own_sc[bel[i]]; n     int D = sz_sc[bel[i]]; n     for (int j = 0; j < sz[i]; j++) { n       if (own[i][j]) { n         A[i].second++; n       } else if (app[j % D]) { n         A[i].first++; n       } n     } n     A[i].first += A[i].second; n   } n   sort(A + 1, A + n + 1); n   reverse(A + 1, A + n + 1); n   comb[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     comb[i][0] = comb[i][i] = 1; n     for (int j = 1; j < i; j++) { n       comb[i][j] = add(comb[i - 1][j - 1], comb[i - 1][j]); n     } n   } n   int c = a - b, ans = 0; n   for (int i = b; i <= n; i++) { n     int x = 0, y; n     for (int j = 1; j < i; j++) { n       if (A[j].second > A[i].first) { n         x++; n       } n     } n     y = max(x - c, 0); n     for (int j = y; j < b; j++) { n       ans = (ans + comb[x][j] * 1ll * comb[i - x - 1][b - j - 1]) % Mod; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     string s; n     cin >> n >> s; n     map<long long int, vector<long long int>> mp; n     for (long long int i = 0; i < s.size(); i++) { n       mp[s[i] -  0 ].push_back(i); n     } n     vector<long long int> ans(n + 1, 0); n     long long int las = -1; n     for (auto x : mp) { n       vector<long long int> v = x.second; n       long long int cnt = 0; n       for (long long int i = 0; i < v.size(); i++) { n         if (v[i] > las) { n           ans[v[i]] = 1; n           cnt++; n           las = v[i]; n         } n       } n       if (cnt != v.size()) { n         break; n       } n     } n     vector<long long int> ck; n     for (long long int i = 0; i < n; i++) { n       if (ans[i] == 1) { n         ck.push_back(s[i] -  0 ); n       } n     } n     for (long long int i = 0; i < n; i++) { n       if (ans[i] != 1) { n         ck.push_back(s[i] -  0 ); n       } n     } n     long long int f = 0; n     for (long long int i = 1; i < n; i++) { n       if (ck[i] < ck[i - 1]) { n         f = 1; n         break; n       } n     } n     if (f == 1) { n       cout <<  -  << endl; n     } else { n       string str; n       for (long long int i = 0; i < n; i++) { n         if (ans[i] == 0) { n           str +=  2 ; n         } else { n           str +=  1 ; n         } n       } n       cout << str << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int min(int a, int b) { return a < b ? a : b; } n int n, m, t1, t2, val[1024]; n long long ans; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &val[i]); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &t1, &t2); n     ans += min(val[t1], val[t2]); n   } n   printf( %lld , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> arr(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   long long c = 0; n   for (int i = 0; i < n; i++) { n     c += arr[i] - 1; n     if (c % 2 == 0) n       cout << 2 <<   n ; n     else n       cout << 1 <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   long long T; n   T = 1; n   for (int t = 0; t < T; t++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char cross[101][101]; n int main() { n   int n; n   while (cin >> n) { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) cin >> cross[i][j]; n     int ok = 0; n     for (int i = 1; i < n - 1; i++) { n       for (int j = 1; j < n - 1; j++) { n         if (cross[i][j] ==  # ) { n           if (cross[i][j] ==  #  && cross[i - 1][j] ==  #  && n               cross[i][j - 1] ==  #  && cross[i][j + 1] ==  #  && n               cross[i + 1][j] ==  # ) { n             cross[i][j] =  a ; n             cross[i - 1][j] =  a ; n             cross[i][j - 1] =  a ; n             cross[i][j + 1] =  a ; n             cross[i + 1][j] =  a ; n           } n         } n       } n     } n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         if (cross[i][j] ==  # ) { n           ok = 1; n           break; n           break; n         } n     if (ok == 0) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace std { n template <typename...> n using void_t = void; n } n template <typename T, typename _ = void> n struct is_container : std::false_type {}; n template <typename... Ts> n struct is_container_helper {}; n template <typename T> n struct is_container< n     T, typename std::conditional< n            false, n            is_container_helper<decltype(std::declval<T>().size()), n                                decltype(std::declval<T>().begin()), n                                decltype(std::declval<T>().end()), n                                decltype(std::declval<T>().cbegin()), n                                decltype(std::declval<T>().cend())>, n            void>::type> : public std::true_type {}; n class IO { n   static const int bufSize = 1 << 20; n   char inBuf[bufSize], outBuf[bufSize]; n   char *ip1 = inBuf, *ip2 = inBuf; n   int goodReadBit = 1, op1 = -1, op2 = bufSize - 1; n   __inline__ __attribute__((always_inline)) int gc() { n     return ip1 == ip2 && (ip2 = (ip1 = inBuf) + fread(inBuf, 1, bufSize, stdin), n                           ip1 == ip2) n                ? (goodReadBit = 0, EOF) n                : *ip1++; n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) void __RI(T &x) { n     int ch = gc(), neg = 1; n     x = 0; n     for (; !(isdigit(ch) || ch ==  -  || ch == EOF); ch = gc()) { n     } n     if (ch == EOF) return; n     if (ch ==  - ) neg = -1, ch = gc(); n     for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - 48) * neg; n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) void __RC(T &x) { n     int ch; n     while (isspace(ch = gc())) { n     } n     if (ch == EOF) return; n     x = ch; n   } n   __inline__ __attribute__((always_inline)) void __RS(std::string &x) { n     char ch; n     x.clear(); n     for (ch = gc(); isspace(ch); ch = gc()) { n     } n     if (ch == EOF) return; n     for (; !isspace(ch) && ch != EOF; ch = gc()) x.push_back(ch); n   } n  n  public: n   __inline__ __attribute__((always_inline)) IO &R(char &x) { n     return __RC(x), (*this); n   } n   __inline__ __attribute__((always_inline)) IO &R(unsigned char &x) { n     return __RC(x), (*this); n   } n   __inline__ __attribute__((always_inline)) IO &R(std::string &x) { n     return __RS(x), (*this); n   } n   template <typename T1, typename T2> n   __inline__ __attribute__((always_inline)) IO &R(std::pair<T1, T2> &x) { n     return R(x.first), R(x.second), (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO &RA(T *a, int n) { n     for (int i = 0; i < n; ++i) R(a[i]); n     return (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO &R(T &x, Args &...args) { n     return R(x), R(args...), (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO &RA(T *a, int n, Args... args) { n     for (int i = 0; i < n; ++i) RA(a[i], args...); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) n   typename std::enable_if<std::is_integral<T>::value, IO>::type & n   R(T &x) { n     return __RI(x), (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) n   typename std::enable_if<is_container<T>::value, IO>::type & n   R(T &x) { n     for (auto &i : x) R(i); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) typename std::enable_if< n       std::is_void<std::void_t<decltype(std::declval<T>().read())>>::value, n       IO>::type & n   R(T &x) { n     return x.read(), (*this); n   } n  n  private: n   char space =    ; n   __inline__ __attribute__((always_inline)) void pc(const char &x) { n     if (op1 == op2) flush(); n     outBuf[++op1] = x; n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) void __WI(T x) { n     static char buf[sizeof(T) * 16 / 5]; n     static int len = -1; n     if (x >= 0) { n       do { n         buf[++len] = x % 10 + 48, x /= 10; n       } while (x); n     } else { n       pc( - ); n       do { n         buf[++len] = -(x % 10) + 48, x /= 10; n       } while (x); n     } n     while (len >= 0) { n       pc(buf[len]), --len; n     } n   } n  n  public: n   __inline__ __attribute__((always_inline)) void flush() { n     fwrite(outBuf, 1, op1 + 1, stdout), op1 = -1; n   } n   __inline__ __attribute__((always_inline)) IO &W(const char &x) { n     return pc(x), (*this); n   } n   __inline__ __attribute__((always_inline)) IO &W(const char *x) { n     while (*x !=   0 ) pc(*(x++)); n     return (*this); n   } n   __inline__ __attribute__((always_inline)) IO &W(const std::string &x) { n     return W(x.c_str()), (*this); n   } n   template <typename T1, typename T2> n   __inline__ __attribute__((always_inline)) IO &W(const std::pair<T1, T2> &x) { n     WS(x.first); n     W(x.second); n     return (*this); n   } n   __inline__ __attribute__((always_inline)) IO &WL() { n     return W(  n ), (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO &WL(const T &x) { n     return W(x), W(  n ), (*this); n   } n   __inline__ __attribute__((always_inline)) IO &WS() { n     return W(space), (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO &WS(const T &x) { n     return W(x), W(space), (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO &WA(T *a, int n) { n     for (int i = 0; i < n; i++) WS(a[i]); n     WL(); n     return (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO &W(const T &x, n                                                   const Args &...args) { n     W(x), W(space), W(args...); n     return (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO &WS(const T &x, n                                                    const Args &...args) { n     return W(x), W(space), W(args...), W(space), (*this); n   } n   template <typename... Args> n   __inline__ __attribute__((always_inline)) IO &WL(const Args &...args) { n     return W(args...), W(  n ), (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO &WA(T *a, int n, Args... args) { n     for (int i = 0; i < n; i++) WA(a[i], args...); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) n   typename std::enable_if<std::is_integral<T>::value, IO>::type & n   W(const T &x) { n     return __WI(x), (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) n   typename std::enable_if<is_container<T>::value, IO>::type & n   W(const T &x) { n     for (auto &i : x) WS(i); n     WL(); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) typename std::enable_if< n       std::is_void<std::void_t<decltype(std::declval<T>().write())>>::value, n       IO>::type & n   W(const T &x) { n     return x.write(), (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO &operator>>(T &x) { n     R(x); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO &operator<<(const T &x) { n     W(x); n     return (*this); n   } n   int good() { return goodReadBit; } n   IO() {} n   ~IO() { flush(); } n } io; n const std::int32_t INF = 0x3f3f3f3f; n const std::int64_t INFL = 0x3f3f3f3f3f3f3f3f; n using namespace std; n namespace Math { n template <typename T, typename T2> n T qpow(T a, T2 b, const int &MOD) { n   T ans = 1; n   for (; b; b >>= 1) { n     if (b & 1) ans = (a * ans) % MOD; n     a = (a * a) % MOD; n   } n   return ans; n } n template <typename T> n T exgcd(T l, T r, T &x, T &y) { n   if (!r) { n     x = 1, y = 0; n     return l; n   } else { n     int d = exgcd(r, l % r, y, x); n     y -= l / r * x; n     return d; n   } n } n template <typename T> n T inv(T a, const int &MOD) { n   return qpow(a, MOD - 2, MOD); n } n template <typename T> n T exinv(T a, const int &MOD) { n   T x, y; n   exgcd(a, MOD, x, y); n   x %= MOD; n   return x + ((x >> (sizeof(T) * 8 - 1)) & MOD); n } n template <typename T, T MOD> n struct ModInt { n   T x; n   ModInt(T x = 0) : x(x) { n     while (x < 0) x += MOD; n   } n   ModInt<T, MOD> operator+=(const ModInt<T, MOD> &y) { n     x += y.x - MOD; n     x += (x >> (sizeof(T) * 8 - 1)) & MOD; n     return (*this); n   } n   ModInt<T, MOD> operator-=(const ModInt<T, MOD> &y) { n     x = x - y.x; n     x += (x >> (sizeof(T) * 8 - 1)) & MOD; n     return (*this); n   } n   ModInt<T, MOD> operator*=(const ModInt<T, MOD> &y) { n     x = x * y.x % MOD; n     return (*this); n   } n   ModInt<T, MOD> operator/=(const ModInt<T, MOD> &y) { n     x = x * inv(y.x, MOD) % MOD; n     return (*this); n   } n   ModInt<T, MOD> pow(int64_t p) { return x = qpow(x, p, MOD), *this; } n   void write() const { io.W(x); } n   void read() { n     io.R(x); n     x %= MOD; n     if (x < 0) x += MOD; n   } n }; n template <typename T, T MOD> n ModInt<T, MOD> operator+(ModInt<T, MOD> a, const ModInt<T, MOD> &b) { n   a += b; n   return a; n } n template <typename T, T MOD> n ModInt<T, MOD> operator-(ModInt<T, MOD> a, const ModInt<T, MOD> &b) { n   a -= b; n   return a; n } n template <typename T, T MOD> n ModInt<T, MOD> operator*(ModInt<T, MOD> a, const ModInt<T, MOD> &b) { n   a *= b; n   return a; n } n template <typename T, T MOD> n ModInt<T, MOD> operator/(ModInt<T, MOD> a, const ModInt<T, MOD> &b) { n   a /= b; n   return a; n } n template <typename T, T MOD> n bool operator==(const ModInt<T, MOD> &a, const ModInt<T, MOD> &b) { n   return a.x == b.x; n } n }  // namespace Math n using mint = Math::ModInt<int64_t, int(1e9 + 7)>; n int main() { n   int n, q; n   io.R(n, q); n   vector<mint> a(n); n   io.R(a); n   a.insert(a.begin(), mint(0)); n   vector<mint> preSum(n + 1), preSquareSum(n + 1); n   partial_sum(a.begin(), a.end(), preSum.begin()); n   partial_sum(a.begin(), a.end(), preSquareSum.begin(), n               [](mint a, mint b) { return a + b * b; }); n   mint inv2 = mint(1) / mint(2); n   mint inv6 = mint(1) / mint(6); n   while (q--) { n     int l, r, d; n     io.R(l, r, d); n     mint len = r - l + 1; n     mint x = ((preSum[r] - preSum[l - 1]) * mint(2) / len - n               (len - mint(1)) * mint(d)) * n              inv2; n     mint y = x * x * len + x * (len - mint(1)) * len * mint(d) + n              len * (mint(2) * len * len - mint(3) * len + mint(1)) * mint(d) * n                  mint(d) * inv6; n     mint z = (preSquareSum[r] - preSquareSum[l - 1]); n     io.WL(y == z ?  Yes  :  No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct no { n   int r; n   int l; n   int p; n }; n int main() { n   int n; n   double v; n   double a[110]; n   double b[110]; n   cin >> n >> v; 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     if (b[i] == 0) { n       cout << 0.0 << endl; n       return 0; n     } n   } n   double cu = a[0], ni = b[0]; n   double aa = 0; n   for (int i = 1; i < n; i++) { n     if (b[i] / a[i] < ni / cu) { n       cu = a[i]; n       ni = b[i]; n     } n   } n   aa = ni / cu; n   double ans = 0; n   for (int i = 0; i < n; i++) ans += aa * a[i]; n   if (ans > v) ans = v; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n typedef long long ll; n #define f(i,a,b) for(i=a;i<b;i++) n  n using namespace std; n  n int main() n { n     ll n,i,sum=0, count=0; n     cin>>n; n     vector<ll> a(n); n     f(i,0,n) cin>>a[i]; n     priority_queue <int, vector<int>, greater<int>> p; n     f(i,0,n){ n         if(a[i]>=0) sum+=a[i], count++; n         else{ n              n             if(sum+a[i]>=0){ n               sum+=a[i], count++;  n               p.push(a[i]); n             }  n             else{ n                 if(!p.empty()&&p.top()<a[i]){ n                   sum-=p.top(); n                   p.pop(); n                   sum+=a[i]; n                   p.push(a[i]); n                 }  n             } n         } n     } n     cout<<count<<endl; n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ml, mr, wl, wr; n int main() { n   cin >> ml >> mr >> wl >> wr; n   if ((ml * 2 + 3 > wr && wr >= ml - 1) || (mr * 2 + 3 > wl && wl >= mr - 1)) { n     cout <<  YES ; n     return 0; n   } else { n     cout <<  NO ; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct que { n   int l, r, y, val; n   bool operator<(const que &v) const { return y < v.y; } n }; n int n, k, x[100010], y[100010], cnt[100010 * 2], pre[100010 * 2]; n long long ans[100010]; n vector<que> q; n vector<int> h; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); n   for (int i = 1; i <= 2 * n; i++) pre[i] = -0x3f3f3f3f; n   h.push_back(-0x3f3f3f3f); n   for (int i = 1; i <= n; i++) { n     h.push_back(x[i]); n     h.push_back(x[i] + k); n   } n   sort(h.begin(), h.end()); n   h.resize(unique(h.begin(), h.end()) - h.begin()); n   for (int i = 1, l, r; i <= n; i++) { n     l = upper_bound(h.begin(), h.end(), x[i]) - h.begin(); n     r = lower_bound(h.begin(), h.end(), x[i] + k) - h.begin(); n     q.push_back({l, r, y[i], 1}); n     q.push_back({l, r, y[i] + k, -1}); n   } n   sort(q.begin(), q.end()); n   for (auto i : q) n     for (int j = i.l; j <= i.r; j++) { n       ans[cnt[j]] += 1ll * (h[j] - h[j - 1]) * (i.y - pre[j]); n       cnt[j] += i.val; n       pre[j] = i.y; n     } n   for (int i = 1; i <= n; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n using vi = vector<int>; n using vll = vector<ll>; n using vii = vector<ii>; n const ll MOD = 998244353; n const ll INF = 1e15 + 9; n const int MAXN = 1000006; n int n, m, a[MAXN], p[MAXN], b[MAXN]; n int ind[MAXN], seen[MAXN]; n ll dp[MAXN], sum[MAXN], negsum[MAXN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   ++n; n   for (int i = 1; i < n; ++i) cin >> a[i]; n   for (int i = 1; i < n; ++i) cin >> p[i]; n   cin >> m; n   ++m; n   for (int i = 1; i < m; ++i) cin >> b[i]; n   a[n] = n; n   b[m] = n; n   ++n, ++m; n   for (int i = 0, j = 0; i < m; ++i) { n     while (j < b[i]) ind[j++] = 2 * i - 1; n     ind[j++] = 2 * i; n   } n   for (int i = 0; i < n; ++i) { n     a[i] = ind[a[i]]; n     sum[i] = sum[seen[a[i]]] + max(p[i], 0); n     negsum[i + 1] = negsum[i] + min(p[i], 0); n     seen[a[i]] = i; n   } n   memset(seen, -1, sizeof seen); n   seen[0] = 0; n   for (int i = 1; i < n; ++i) { n     dp[i] = INF; n     if ((a[i] & 1) == 0) { n       if (seen[a[i] - 2] != -1) { n         dp[i] = dp[seen[a[i] - 2]] + sum[seen[a[i] - 1]] + sum[seen[a[i]]] + n                 negsum[i] - negsum[seen[a[i] - 2] + 1]; n         if (seen[a[i]] != -1) n           dp[i] = min(dp[i], n                       dp[seen[a[i]]] + negsum[i + 1] - negsum[seen[a[i]] + 1]); n       } n     } n     seen[a[i]] = i; n   } n   if (dp[n - 1] >= INF / 2) n     cout <<  NO n ; n   else n     cout <<  YES n  << dp[n - 1] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T checkmin(T &a, T b) { n   return (a < b) ? a : a = b; n } n template <class T> n inline T checkmax(T &a, T b) { n   return (a > b) ? a : a = b; n } n template <class T> n T GCD(T a, T b) { n   if (a < 0) return GCD(-a, b); n   if (b < 0) return GCD(a, -b); n   return (a == 0) ? b : GCD(b % a, a); n } n template <class T> n T LCM(T a, T b) { n   if (a < 0) return LCM(-a, b); n   if (b < 0) return LCM(a, -b); n   return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b; n } n template <class T> n T ExtGCD(T a, T b, T &x, T &y) { n   if (a < 0) { n     T c = ExtGCD(-a, b, x, y); n     x = -x; n     return c; n   } n   if (b < 0) { n     T c = ExtGCD(a, -b, x, y); n     y = -y; n     return c; n   } n   if (a == 0) { n     x = 0, y = 1; n     return b; n   } else { n     T c = ExtGCD(b % a, a, y, x); n     x -= b / a * y; n     return c; n   } n } n template <class T> n inline T sqr(T X) { n   return X * X; n } n class TUFSet { n  private: n   vector<int> S; n  n  public: n   TUFSet() {} n   TUFSet(int N) { S = vector<int>(N, -1); } n   int Find(int X) { return S[X] < 0 ? X : (S[X] = Find(S[X])); } n   void Union(int X, int Y) { n     X = Find(X); n     Y = Find(Y); n     if (X == Y) return; n     if (S[X] < S[Y]) n       S[Y] = X; n     else { n       if (S[X] == S[Y]) --S[Y]; n       S[X] = Y; n     } n   } n }; n namespace Poor { n int N, M, K; n TUFSet UFS; n vector<int> D; n void Run() { n   scanf( %d%d%d , &N, &M, &K); n   UFS = TUFSet(N); n   for (int i = 0; i < M; ++i) { n     int U, V; n     scanf( %d%d , &U, &V); n     --U, --V; n     UFS.Union(U, V); n   } n   int Ans = 1 % K; n   int C = 0; n   D = vector<int>(N, 0); n   for (int i = 0; i < N; ++i) { n     ++D[UFS.Find(i)]; n     if (UFS.Find(i) == i) ++C; n   } n   if (C > 1) { n     for (int i = 0; i < C - 2; ++i) Ans = ((long long)Ans * N) % K; n     for (int i = 0; i < N; ++i) n       if (D[i] > 0) Ans = ((long long)Ans * D[i]) % K; n   } n   printf( %d n , Ans); n } n }  // namespace Poor n int main() { n   Poor::Run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int a[1000005]; n map<int, int> ht; n map<int, int>::reverse_iterator its; n long long mul(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b % 2 == 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b /= 2; n   } n   return res; n } n int main() { n   int n; n   scanf( %d , &n); n   long long nf = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     ht[a[i]]++; n     nf = (nf * i) % mod; n   } n   int add = 0; n   long long ans = 0; n   for (its = ht.rbegin(); its != ht.rend(); its++) { n     if (add != 0) n       ans = (ans + nf * its->second % mod * n                        mul((its->second + add) % mod, mod - 2) % mod * n                        its->first % mod) % n             mod; n     add += its->second; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long long N = 2e5 + 36; n const long long M = 1e3 + 36; n const long long INF = 2e9 + 7; n const long long MOD = 987654323; n const int X[8] = {1, 1, -1, -1, 2, 2, -2, -2}; n const int Y[8] = {2, -2, 2, -2, 1, -1, 1, -1}; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, ans = 0; n   cin >> n; n   string s, t; n   cin >> s >> t; n   for (int i(0); i < (n >> 1); ++i) { n     if (t[i] == t[n - i - 1]) { n       if (s[i] == s[n - i - 1]) { n         continue; n       } else { n         ans += 1; n         continue; n       } n     } else { n       if (s[i] == s[n - i - 1]) { n         if (t[i] == s[i] || t[n - i - 1] == s[i]) { n           ans += 1; n           continue; n         } else { n           ans += 2; n           continue; n         } n       } else { n         if (s[i] != t[i] && s[i] != t[n - i - 1] && s[n - i - 1] != t[i] && n             s[n - i - 1] != t[n - i - 1]) { n           ans += 2; n           continue; n         } n         if (min(t[i], t[n - i - 1]) == min(s[i], s[n - i - 1]) && n             max(t[i], t[n - i - 1]) == max(s[i], s[n - i - 1])) { n           continue; n         } else { n           ans += 1; n           continue; n         } n       } n     } n   } n   if (n % 2 == 1) { n     if (s[(n >> 1)] != t[(n >> 1)]) ans++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); n long long rand(long long l, long long r) { n   return uniform_int_distribution<long long>(l, r)(rd); n } n const int N = 49; n int n, m, d[N][N]; n int b[N]; n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int(i) = (1); (i) <= (m); ++(i)) { n     int a, b; n     cin >> a >> b; n     if (max(a, b) > 48) continue; n     d[a][b] = d[b][a] = 1; n   } n   n = min(n, 48); n   for (int(i) = (1); (i) <= (n); ++(i)) b[i] = i; n   shuffle(b + 1, b + 1 + n, rd); n   for (int(i) = (1); (i) <= (n); ++(i)) n     for (int(j) = (i + 1); (j) <= (n); ++(j)) n       for (int(z) = (j + 1); (z) <= (n); ++(z)) n         for (int(k) = (z + 1); (k) <= (n); ++(k)) n           for (int(x) = (k + 1); (x) <= (n); ++(x)) { n             int CNT = 0; n             vector<int> a; n             a.push_back(b[i]), a.push_back(b[j]), a.push_back(b[z]), n                 a.push_back(b[k]), a.push_back(b[x]); n             for (int(i) = (0); (i) <= ((int)(a).size() - 1); ++(i)) n               for (int(j) = (0); (j) <= (i - 1); ++(j)) n                 if (d[a[i]][a[j]]) ++CNT; n             if (CNT == 0 || CNT == 10) { n               cout << b[i] <<     << b[j] <<     << b[z] <<     << b[k] <<     n                    << b[x] <<   n ; n               return 0; n             } n           } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n const int N = 1e6, M = 1e6; n pair<int, int> adj[M + 7]; n long long val[N + 7]; n long long sum[N + 7]; n long long res = 0; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n void addRes(int numb) { res += 1LL * numb * (numb - 1) / 2; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1, r1, r2; i <= m; i++) { n     cin >> r1 >> r2; n     adj[i] = {r1, r2}; n   } n   for (int i = 1; i <= n; i++) { n     val[i] = rng(); n     sum[i] = 0; n   } n   for (int i = 1; i <= m; i++) { n     sum[adj[i].first] += val[adj[i].second]; n     sum[adj[i].second] += val[adj[i].first]; n   } n   int u, v; n   for (int i = 1; i <= m; i++) { n     u = adj[i].first; n     v = adj[i].second; n     if (sum[u] - val[v] == sum[v] - val[u]) res++; n   } n   int start = 1, finish = 1; n   sort(sum + 1, sum + n + 1); n   for (int i = 2; i <= n; i++) n     if (sum[i] == sum[i - 1]) { n       finish = i; n       if (i == n) addRes(finish - start + 1); n     } else { n       addRes(finish - start + 1); n       start = finish = i; n     } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long pow2(long long n) { n   long long x = 1; n   for (long long i = 1; i <= n; i++) { n     x *= 2; n   } n   return x; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   cout << 2 * (n - 1) + 1 <<   2  << endl; n   cout <<  1 2  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int inf = 1000000005; n long long int llinf = 2000000000000000005LL; n long long int mod = 1000000007; n long long int mod9 = 1000000009; n double pi = 3.1415926535897; n double eps = 1e-15; n int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}, dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; n vector<bool> isprime; n vector<int> primes; n void seive(int n, bool wantlist = true) { n   isprime.resize(n + 1, true); n   isprime[0] = isprime[1] = false; n   int sq = sqrt(n + 1); n   for (int i = 2; i < sq + 1; i++) { n     if (isprime[i]) { n       for (int j = i * i; j <= n; j += i) isprime[j] = false; n     } n   } n   for (int i = 2; wantlist && i <= n; i++) { n     if (isprime[i]) primes.push_back(i); n   } n } n template <class T> n inline T gcd(T a, T b) { n   while (b > 0) { n     a %= b; n     swap(a, b); n   } n   return 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 vector<T> operator+(vector<T>& a, vector<T>& b) { n   assert(a.size() == b.size()); n   int n = a.size(); n   vector<T> c(n); n   for (int i = 0; i < n; i++) c[i] = a[i] + b[i]; n   return c; n } n int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } n inline long long int bexp(long long int x, long long int n, n                           long long int m = mod) { n   long long int res = 1; n   x %= m; n   while (n) { n     if (n & 1) res = res * x % m; n     x = x * x % m; n     n >>= 1; n   } n   return res; n } n inline bool ispalin(string& str) { n   int n = str.length(); n   for (int i = 0; i < n / 2; i++) n     if (str[i] != str[n - i - 1]) return false; n   return true; n } n inline void mapper(vector<int>& a, int idx = 0) { n   int cc = idx, n = a.size(); n   map<int, vector<int> > mapp; n   for (int i = 0; i < n; i++) mapp[a[i]].push_back(i); n   for (auto& p : mapp) { n     for (int i : p.second) a[i] = cc; n     cc++; n   } n } n template <class T> n inline void ri(T& i) { n   char c; n   for (c = getchar(); c <  0  || c >  9 ; c = getchar()) n     ; n   for (i = 0; c >=  0  && c <=  9 ; c = getchar()) n     i = (i << 3) + (i << 1) + (c - 48); n } n long long int Euclid(long long int a, long long int b, long long int& x, n                      long long int& y) { n   if (b == 0LL) { n     x = (a < 0LL ? -1LL : 1LL); n     y = 0LL; n     return a * x; n   } n   long long int q = a / b; n   long long int r = a % b; n   long long int d = Euclid(b, r, y, x); n   y -= q * x; n   return d; n } n long double calDistance(long long int x, long long int y) { n   long double tx = (long double)(x); n   long double ty = (long double)(y); n   long double res = tx * tx + ty * ty; n   res = sqrt(res); n   return res; n } n int main() { n   pair<long long int, long long int> a, b, t; n   cin >> a.first >> a.second >> b.first >> b.second >> t.first >> t.second; n   int n; n   cin >> n; n   vector<vector<long double> > dis(n, vector<long double>(3, 0.0)); n   for (int i = 0; i < n; i++) { n     pair<long long int, long long int> bottle; n     cin >> bottle.first >> bottle.second; n     long long int x, y; n     x = labs(bottle.first - t.first); n     y = labs(bottle.second - t.second); n     dis[i][0] = calDistance(x, y); n     x = labs(bottle.first - a.first); n     y = labs(bottle.second - a.second); n     dis[i][1] = calDistance(x, y); n     x = labs(bottle.first - b.first); n     y = labs(bottle.second - b.second); n     dis[i][2] = calDistance(x, y); n   } n   vector<vector<long double> > AL(n, vector<long double>(2, 0.0)); n   for (int i = 0; i < n; i++) { n     if (!i) { n       AL[i][0] = dis[i][0]; n       AL[i][1] = dis[i][1]; n     } else { n       AL[i][0] = dis[i][0] + AL[i - 1][0]; n       AL[i][1] = min(AL[i - 1][0] + dis[i][1], AL[i - 1][1] + dis[i][0]); n     } n   } n   vector<vector<long double> > AR(n, vector<long double>(2, 0.0)); n   for (int i = n - 1; i >= 0; i--) { n     if (i == n - 1) { n       AR[i][0] = dis[i][0]; n       AR[i][1] = dis[i][1]; n     } else { n       AR[i][0] = dis[i][0] + AR[i + 1][0]; n       AR[i][1] = min(AR[i + 1][0] + dis[i][1], AR[i + 1][1] + dis[i][0]); n     } n   } n   vector<vector<long double> > BL(n, vector<long double>(2, 0.0)); n   for (int i = 0; i < n; i++) { n     if (!i) { n       BL[i][0] = dis[i][0]; n       BL[i][1] = dis[i][2]; n     } else { n       BL[i][0] = dis[i][0] + BL[i - 1][0]; n       BL[i][1] = min(BL[i - 1][0] + dis[i][2], BL[i - 1][1] + dis[i][0]); n     } n   } n   vector<vector<long double> > BR(n, vector<long double>(2, 0.0)); n   for (int i = n - 1; i >= 0; i--) { n     if (i == n - 1) { n       BR[i][0] = dis[i][0]; n       BR[i][1] = dis[i][2]; n     } else { n       BR[i][0] = dis[i][0] + BR[i + 1][0]; n       BR[i][1] = min(BR[i + 1][0] + dis[i][2], BR[i + 1][1] + dis[i][0]); n     } n   } n   long double ans = min(AL[n - 1][1], BL[n - 1][1]); n   for (int i = 0; i < n - 1; i++) { n     long double val = min(AL[i][1] + BR[i + 1][1], BL[i][1] + AR[i + 1][1]); n     ans = min(val, ans); n   } n   for (int i = 0; i < n; i++) { n     ans += dis[i][0]; n   } n   cout << fixed << setprecision(12) << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1000000000000000000LL; n const int N = 100005; n vector<pair<int, int>> ds; n long long data[2][100005]; n int x[N], p[N], c[N], st[N]; n long long val[N]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , x + i); n     ds.push_back({x[i], i}); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , p + i, c + i); n     ds.push_back({p[i], -i}); n   } n   sort(ds.begin(), ds.end()); n   long long* f[2] = {data[0] + 10005, data[1] + 10005}; n   int flag = 0; n   for (int i = -n; i <= n; i++) f[flag][i] = oo; n   f[flag][0] = 0; n   long long last = ds[0].first; n   for (int i = 0; i < ds.size(); i++) { n     int old = flag; n     flag ^= 1; n     if (ds[i].second > 0) { n       f[flag][n] = oo; n       for (int j = -n; j < n; j++) { n         f[flag][j] = oo; n         if (f[old][j + 1] < oo) n           f[flag][j] = f[old][j + 1] + (ds[i].first - last) * abs(j + 1); n       } n     } else { n       int head = 0, tail = -1, cc = c[-ds[i].second]; n       for (int j = -n; j <= n; j++) { n         if (f[old][j] < oo) { n           long long tmp = f[old][j] + abs(j) * (ds[i].first - last); n           while (head <= tail && val[tail] > tmp) tail--; n           st[++tail] = j; n           val[tail] = tmp; n         } n         while (head <= tail && st[head] + cc < j) head++; n         f[flag][j] = oo; n         if (head <= tail) f[flag][j] = val[head]; n       } n     } n     last = ds[i].first; n   } n   if (f[flag][0] == oo) n     printf( -1 ); n   else n     printf( %lld , f[flag][0]); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double sv[500050]; n struct info { n   int x, v; n } A[500050]; n int main() { n   scanf( %d , &n); n   int l = 0; n   for (int i = 0; i < n; i++) scanf( %d %d , &A[i].x, &A[i].v); n   int st = n, nd = -1; n   for (int i = 0; i < n; i++) n     if (A[i].v > 0) { n       st = i; n       break; n     } n   for (int i = n - 1; i >= 0; i--) n     if (A[i].v < 0) { n       nd = i; n       break; n     } n   if (st > nd) n     puts( -1.000000000000 ); n   else { n     int it = 100; n     double low = 0, high = 1e20, mid; n     while (it--) { n       mid = (low + high) / 2; n       double mx = -1e20, mn = 1e20; n       int i; n       for (i = 0; i < n; i++) n         if (A[i].v > 0) sv[i] = mx = max(mx, A[i].v * mid + A[i].x); n       for (i = n - 1; i >= 0; i--) { n         if (A[i].v < 0) n           mn = min(mn, A[i].v * mid + A[i].x); n         else if (sv[i] > mn) n           break; n       } n       if (i >= 0) n         high = mid; n       else n         low = mid; n     } n     printf( %.12lf n , mid); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ex(long long a, long long b) { n   long long tmp = 1; n   for (int i = 0; i < b; i++) { n     tmp = tmp * a; n   } n   return tmp; n } n int main() { n   long long r, l, tmp, ans = -1; n   bool good = 1; n   cin >> l >> r; n   for (long long i = l; i <= r; i++) { n     for (long long k = 0; k <= 5; k++) { n       for (long long j = 0; j <= 5; j++) { n         if (i / (ex(10, j)) % 10 == (i / (ex(10, k))) % 10 && ex(10, k) <= i && n             ex(10, j) <= i && j != k) { n           good = 0; n         } n       } n     } n     if (good) { n       ans = i; n     } n     good = 1; n   } n   cout << ans << endl; n } n