#include <bits/stdc++.h> n std::mt19937 rng( n     (int)std::chrono::steady_clock::now().time_since_epoch().count()); n const int MOD = 1e9 + 7; n const int ms = 220; n int c = 1; n int to[ms][20]; n int fail[ms]; n int cost[ms]; n int addString(const std::vector<int> &str) { n   int on = 0; n   for (auto ch : str) { n     if (to[on][ch] == 0) { n       to[on][ch] = c++; n     } n     on = to[on][ch]; n   } n   return on; n } n void buildAho() { n   std::queue<int> que; n   que.push(0); n   while (!que.empty()) { n     int on = que.front(); n     que.pop(); n     cost[on] += cost[fail[on]]; n     for (int i = 0; i < 20; i++) { n       if (to[on][i]) { n         fail[to[on][i]] = on == 0 ? 0 : to[fail[on]][i]; n         que.push(to[on][i]); n       } else { n         to[on][i] = to[fail[on]][i]; n       } n     } n   } n } n int l[ms], r[ms]; n int dp[ms][ms][505]; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   int n, m, k; n   std::cin >> n >> m >> k; n   { n     int len; n     std::cin >> len; n     while (len > 0) { n       std::cin >> l[ms - len]; n       len--; n     } n     l[ms - 1]--; n     for (int i = ms - 1; l[i] < 0; i--) { n       l[i] += m; n       l[i - 1]--; n     } n   } n   { n     int len; n     std::cin >> len; n     while (len > 0) { n       std::cin >> r[ms - len]; n       len--; n     } n   } n   while (n--) { n     int len; n     std::cin >> len; n     std::vector<int> str(len); n     for (auto &v : str) std::cin >> v; n     int v; n     std::cin >> v; n     cost[addString(str)] += v; n   } n   buildAho(); n   { n     for (int i = 0; i < c; i++) { n       for (int j = 0; j <= k; j++) { n         dp[0][i][j] = 1; n       } n     } n     for (int i = 1; i < ms; i++) { n       for (int j = 0; j < c; j++) { n         for (int val = 0; val <= k; val++) { n           for (int ch = 0; ch < m; ch++) { n             int newState = to[j][ch]; n             int newCost = val - cost[newState]; n             if (newCost >= 0) { n               dp[i][j][val] = n                   (dp[i][j][val] + dp[i - 1][newState][newCost]) % MOD; n             } n           } n         } n       } n     } n   } n   int ans = 0; n   { n     bool wtf = false; n     int st = 0; n     int sum = k; n     for (int on = 0; on < ms; on++) { n       if (!wtf && r[on] == 0) { n         continue; n       } n       for (int i = (wtf ? 0 : 1); i < r[on]; i++) { n         int newState = to[st][i]; n         int newCost = sum - cost[newState]; n         if (newCost >= 0) { n           ans = (ans + dp[ms - on - 1][newState][newCost]) % MOD; n         } n       } n       st = to[st][r[on]]; n       sum -= cost[st]; n       if (wtf) { n         for (int i = 1; i < m; i++) { n           int newState = to[0][i]; n           int newCost = k - cost[newState]; n           if (newCost >= 0) { n             ans = (ans + dp[ms - on - 1][newState][newCost]) % MOD; n           } n         } n       } n       wtf = true; n     } n     if (sum >= 0) { n       ans = (ans + 1) % MOD; n     } n   } n   { n     bool wtf = false; n     int st = 0; n     int sum = k; n     for (int on = 0; on < ms; on++) { n       if (!wtf && l[on] == 0) { n         continue; n       } n       for (int i = (wtf ? 0 : 1); i < l[on]; i++) { n         int newState = to[st][i]; n         int newCost = sum - cost[newState]; n         if (newCost >= 0) { n           ans = (ans - dp[ms - on - 1][newState][newCost]) % MOD; n         } n       } n       st = to[st][l[on]]; n       sum -= cost[st]; n       if (wtf) { n         for (int i = 1; i < m; i++) { n           int newState = to[0][i]; n           int newCost = k - cost[newState]; n           if (newCost >= 0) { n             ans = (ans - dp[ms - on - 1][newState][newCost]) % MOD; n           } n         } n       } n       wtf = true; n     } n     if (wtf && sum >= 0) { n       ans = (ans - 1 + MOD) % MOD; n     } n   } n   ans = (ans % MOD + MOD) % MOD; n   std::cout << ans << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, k, l, n, m, s, sum, K; n double an; n const long long N = 1020; n long long f[N][N], lu[N][N], ld[N][N], ru[N][N], rd[N][N]; n long long Lu[N][N], Ld[N][N], Ru[N][N], Rd[N][N]; n int hang[N], lie[N]; n char a[N][N]; n void work(long long x, long long y) { n   long long s = 0; n   long long sum = 0; n   for (long long i = 1; i < y; i++) { n     sum += s; n     f[x][i] += sum; n     s++; n   } n   sum += 3 * s; n   for (long long i = y + 1; i <= m; i++) { n     sum += s; n     f[x][i] += sum; n     s++; n   } n   s = 0; n   sum = 0; n   for (long long i = m; i > y; i--) { n     sum += s; n     f[x][i] += sum; n     s++; n   } n   sum += 3 * s; n   for (long long i = y - 1; i >= 1; i--) { n     sum += s; n     f[x][i] += sum; n     s++; n   } n   s = 0; n   sum = 0; n   for (long long i = 1; i < x; i++) { n     sum += s; n     f[i][y] += sum; n     s++; n   } n   sum += 3 * s; n   for (long long i = x + 1; i <= n; i++) { n     sum += s; n     f[i][y] += sum; n     s++; n   } n   s = 0; n   sum = 0; n   for (long long i = n; i > x; i--) { n     sum += s; n     f[i][y] += sum; n     s++; n   } n   sum += 3 * s; n   for (long long i = x - 1; i >= 1; i--) { n     sum += s; n     f[i][y] += sum; n     s++; n   } n } n int main() { n   scanf( %I64d%I64d , &n, &m); n   for (long long i = 1; i <= n; i++) { n     scanf( %s , a[i] + 1); n     for (long long j = 1; j <= m; j++) n       if (a[i][j] ==  X ) { n         hang[i] = j; n         lie[j] = i; n         break; n       } n   } n   for (long long i = 1; i <= n; i++) { n     long long s = 0; n     long long sum = 0; n     for (long long j = 1; j <= m; j++) { n       sum += s; n       lu[i][j] = lu[i - 1][j] + sum + Lu[i - 1][j] * 1; n       if (a[i][j] !=  X ) s++; n       Lu[i][j] = Lu[i - 1][j] + s; n     } n   } n   for (long long i = 1; i <= n; i++) { n     long long s = 0; n     long long sum = 0; n     for (long long j = m; j >= 1; j--) { n       sum += s; n       ru[i][j] = ru[i - 1][j] + sum + Ru[i - 1][j] * 1; n       if (a[i][j] !=  X ) s++; n       Ru[i][j] = Ru[i - 1][j] + s; n     } n   } n   for (long long i = n; i >= 1; i--) { n     long long s = 0; n     long long sum = 0; n     for (long long j = 1; j <= m; j++) { n       sum += s; n       ld[i][j] = ld[i + 1][j] + sum + Ld[i + 1][j] * 1; n       if (a[i][j] !=  X ) s++; n       Ld[i][j] = Ld[i + 1][j] + s; n     } n   } n   for (long long i = n; i >= 1; i--) { n     long long s = 0; n     long long sum = 0; n     for (long long j = m; j >= 1; j--) { n       sum += s; n       rd[i][j] = rd[i + 1][j] + sum + Rd[i + 1][j] * 1; n       if (a[i][j] !=  X ) s++; n       Rd[i][j] = Rd[i + 1][j] + s; n     } n   } n   long long sx = 0; n   for (long long i = 1; i <= n; i++) { n     bool ok = false; n     for (long long j = 1; j <= m; j++) n       if (a[i][j] ==  X ) { n         sx++; n         work(i, j); n         ok = true; n         break; n       } n     if (!ok) work(i, 0); n   } n   for (long long j = 1; j <= m; j++) { n     bool ok = false; n     for (long long i = 1; i <= n; i++) n       if (a[i][j] ==  X ) { n         ok = true; n         break; n       } n     if (!ok) work(0, j); n   } n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= m; j++) n       if (a[i][j] !=  X ) { n         an += lu[i - 1][j - 1] + ru[i - 1][j + 1] + ld[i + 1][j - 1] + n               rd[i + 1][j + 1]; n         an += (Lu[i - 1][j - 1] + Ru[i - 1][j + 1] + Ld[i + 1][j - 1] + n                Rd[i + 1][j + 1]) * n               2; n         an += f[i][j]; n       } n   for (i = 1; i < n; i++) { n     int j = i; n     while (hang[j] && hang[j + 1] && (hang[j + 1] > hang[j])) j++; n     for (int x = i; x < j; x++) n       for (int y = x + 1; y <= j; y++) { n         if (hang[x] < hang[y]) n           an += 4 * (hang[x] - 1) * (m - hang[y]); n         else n           an += 4 * (hang[y] - 1) * (m - hang[x]); n       } n     i = j; n   } n   for (i = 1; i < n; i++) { n     int j = i; n     while (hang[j] && hang[j + 1] && (hang[j + 1] < hang[j])) j++; n     for (int x = i; x < j; x++) n       for (int y = x + 1; y <= j; y++) { n         if (hang[x] < hang[y]) n           an += 4 * (hang[x] - 1) * (m - hang[y]); n         else n           an += 4 * (hang[y] - 1) * (m - hang[x]); n       } n     i = j; n   } n   for (i = 1; i < m; i++) { n     int j = i; n     while (lie[j] && lie[j + 1] && (lie[j + 1] > lie[j])) j++; n     for (int x = i; x < j; x++) n       for (int y = x + 1; y <= j; y++) { n         if (lie[x] < lie[y]) n           an += 4 * (lie[x] - 1) * (n - lie[y]); n         else n           an += 4 * (lie[y] - 1) * (n - lie[x]); n       } n     i = j; n   } n   for (i = 1; i < m; i++) { n     int j = i; n     while (lie[j] && lie[j + 1] && (lie[j + 1] < lie[j])) j++; n     for (int x = i; x < j; x++) n       for (int y = x + 1; y <= j; y++) { n         if (lie[x] < lie[y]) n           an += 4 * (lie[x] - 1) * (n - lie[y]); n         else n           an += 4 * (lie[y] - 1) * (n - lie[x]); n       } n     i = j; n   } n   an = an / ((n * m - sx) * (n * m - sx)); n   printf( %.8lf , an); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 610; n int N; n vector<int> edge[MAXN]; n bool seen[MAXN]; n int mtype[MAXN]; n int par[MAXN]; n int get_count(vector<int> v) { n   if (v.size() <= 1) return 0; n   cout <<  ?   << v.size() <<   n ; n   for (int i = 0; i < v.size(); i++) { n     if (i) cout <<    ; n     cout << v[i] + 1; n   } n   cout <<   n  << flush; n   int r; n   cin >> r; n   return r; n } n int through(vector<int> v, int cloc) { n   int l = get_count(v); n   v.push_back(cloc); n   int r = get_count(v); n   v.pop_back(); n   return r - l; n } n vector<int> find_all(vector<int> v, int cloc, int ntot) { n   vector<int> res; n   if (!ntot) return res; n   if (ntot == v.size()) { n     for (int x : v) res.push_back(x); n     return res; n   } n   vector<int> vleft, vright; n   for (int i = 0; i < v.size(); i++) { n     if (i % 2 == 0) n       vleft.push_back(v[i]); n     else n       vright.push_back(v[i]); n   } n   int tnew = through(vleft, cloc); n   res = find_all(vleft, cloc, tnew); n   vector<int> vres = find_all(vright, cloc, ntot - tnew); n   for (int x : vres) res.push_back(x); n   return res; n } n void bflood(int x) { n   for (int neigh : edge[x]) { n     if (par[neigh] == -1) { n       par[neigh] = x; n       bflood(neigh); n     } n   } n } n void solve_bad(int l, int r) { n   for (int i = 0; i < MAXN; i++) par[i] = -1; n   par[l] = l; n   bflood(l); n   vector<int> res; n   int cloc = r; n   while (cloc != l) { n     res.push_back(cloc); n     cloc = par[cloc]; n   } n   res.push_back(cloc); n   cout <<  N   << res.size() <<   n ; n   for (int i = 0; i < res.size(); i++) { n     if (i) cout <<    ; n     cout << res[i] + 1; n   } n   cout << endl; n } n void wrong(vector<int> v) { n   int vtot = get_count(v); n   while (true) { n     int cloc = v.back(); n     v.pop_back(); n     if (get_count(v) != vtot) { n       for (int i = 0; i < v.size(); i++) { n         vector<int> vq; n         vq.push_back(cloc); n         vq.push_back(v[i]); n         if (get_count(vq)) { n           solve_bad(cloc, v[i]); n           return; n         } n       } n     } n   } n } n void dfs(int cloc) { n   seen[cloc] = true; n   vector<int> vposs; n   for (int i = 0; i < N; i++) n     if (cloc != i && !edge[i].size()) vposs.push_back(i); n   vector<int> bv = find_all(vposs, cloc, through(vposs, cloc)); n   for (int i = 0; i < bv.size(); i++) { n     edge[cloc].push_back(bv[i]); n     edge[bv[i]].push_back(cloc); n   } n   for (int i = 0; i < bv.size(); i++) { n     int neigh = bv[i]; n     if (!seen[neigh]) dfs(neigh); n   } n } n void flood(int cloc, int ctype) { n   mtype[cloc] = ctype; n   for (int neigh : edge[cloc]) { n     if (!mtype[neigh]) flood(neigh, 3 - ctype); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N; n   dfs(0); n   flood(0, 1); n   vector<int> v1, v2; n   for (int i = 0; i < N; i++) { n     if (mtype[i] == 1) n       v1.push_back(i); n     else n       v2.push_back(i); n   } n   if (get_count(v1) > 0) n     wrong(v1); n   else if (get_count(v2) > 0) n     wrong(v2); n   else { n     cout <<  Y   << v1.size() <<   n ; n     for (int i = 0; i < v1.size(); i++) { n       if (i) cout <<    ; n       cout << v1[i] + 1; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char c; n   char prev; n   vector<int> A; n   int i = 0, count = 1; n   scanf( %c , &prev); n   A.push_back(1); n   while (scanf( %c , &c) != EOF) { n     if (c !=  +  && c !=  - ) continue; n     if (c == prev) { n       A[i]++; n     } else { n       if (!(A[i] & 1)) { n         if (i != 0) { n           A[i - 1] += A[i]; n           A.erase(A.begin() + i); n           i--; n         } n         A[i]++; n         prev = c; n       } else { n         A.push_back(1); n         i++; n         prev = c; n       } n     } n   } n   int flag = 0; n   while (flag == 0 && A.size() != 1) { n     flag = 1; n     for (vector<int>::iterator it = A.begin(); it != A.end(); it++) { n       if (!((*it) & 1)) { n         flag = 0; n         if (it != A.begin()) { n           *it += *(it - 1); n         } n         if ((it + 1) != A.end()) { n           *it += *(it + 1); n         } n         if ((it + 1) != A.end()) A.erase(it + 1); n         if (it != A.begin()) A.erase(it - 1); n         break; n       } n     } n   } n   if (A.size() == 1 && !(A[0] & 1)) n     cout <<  Yes n ; n   else n     cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n vector<int> g[N]; n int used[N], cant, pep[N][N]; n void dfs(int u, int col) { n   if (g[u].size() == 0) return; n   used[u] = col; n   int ncol = ((col == 1) ? 2 : 1); n   for (int v : g[u]) n     if (!used[v]) { n       dfs(v, ncol); n     } else if (used[v] == col) { n       cant = 1; n     } n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     pep[u][v] = pep[v][u] = 1; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       if (!pep[i][j] && i != j) { n         g[i].push_back(j); n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (!used[i]) dfs(i, 1); n   } n   if (cant) { n     printf( No n ); n     return 0; n   } n   char s[N]; n   for (int i = 1; i <= n; ++i) { n     if (used[i] == 1) { n       s[i - 1] =  a ; n     } else if (used[i] == 2) { n       s[i - 1] =  c ; n     } else { n       s[i - 1] =  b ; n     } n   } n   s[n] =   0 ; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       if (pep[i][j]) { n         if (abs(s[i - 1] - s[j - 1]) > 1) { n           printf( No n ); n           return 0; n         } n       } n     } n   } n   printf( Yes n%s n , s); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 2; n bool vis[N]; n int trie[N * 20][2], sz; n void inser(int n) { n   int curr = 0; n   for (int i = 19; i >= 0; i--) { n     int dir = (n >> i) & 1; n     if (trie[curr][dir] == -1) trie[curr][dir] = ++sz; n     curr = trie[curr][dir]; n   } n } n int query(int n) { n   int curr = 0, ans = 0; n   for (int i = 19; i >= 0; i--) { n     int dir = (n >> i) & 1; n     if (trie[curr][dir] != -1) { n       curr = trie[curr][dir]; n     } else { n       ans += (1 << i); n       curr = trie[curr][1 ^ dir]; n     } n   } n   return ans; n } n int main() { n   memset(trie, -1, sizeof(trie)); n   int n, q; n   scanf( %d%d , &n, &q); n   while (n--) { n     int a; n     scanf( %d , &a); n     vis[a] = true; n   } n   for (int i = 0; i < N; i++) n     if (!vis[i]) inser(i); n   int xorval = 0; n   while (q--) { n     int x; n     scanf( %d , &x); n     xorval ^= x; n     printf( %d n , query(xorval)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n const long long M = 1000003; n struct ban { n   long long x; n   ban() { x = 0; } n   ban(long long x) { this->x = x % M; } n }; n bool operator<(const ban& a, const ban& b) { return a.x < b.x; } n bool operator>(const ban& a, const ban& b) { return a.x > b.x; } n bool operator==(const ban& a, const ban& b) { return a.x == b.x; } n ban operator+(const ban& a, const ban& b) { return ban(a.x + b.x); } n ban operator*(const ban& a, const ban& b) { return ban(a.x * b.x); } n ban ast(ban x, int n) { n   if (n == 0) return ban(1); n   if (n % 2 == 0) { n     ban y = ast(x, n / 2); n     return y * y; n   } else { n     ban y = ast(x, n - 1); n     return x * y; n   } n } n ban operator/(const ban& a, const ban& b) { return a * ast(b, M - 2); } n ban operator-(const ban& a, const ban& b) { return ban(a.x - b.x + M); } n const int n = 50; n const int m = 10; n ban a[N][N]; n ban b[N]; n int w[N]; n ban g[N]; n int solv() { n   for (int ii = 1; ii <= n; ++ii) { n     cout <<  ?   << ii << endl; n     cin >> b[ii].x; n     for (int j = 0; j <= m; ++j) a[ii][j] = ast(ban(ii), j); n   } n   for (int jj = 0, ii = 1; jj <= m && ii <= n; ++jj) { n     ban maxu; n     int maxi; n     for (int i = ii; i <= n; ++i) { n       if (a[i][jj] > maxu) { n         maxu = a[i][jj]; n         maxi = i; n       } n     } n     w[jj] = ii; n     for (int j = 0; j <= m; ++j) { n       swap(a[ii][j], a[maxi][j]); n     } n     swap(b[ii], b[maxi]); n     for (int i = 1; i <= n; ++i) { n       if (i == ii) continue; n       ban t = a[i][jj] / a[ii][jj]; n       for (int j = jj; j <= m; ++j) { n         a[i][j] = a[i][j] - a[ii][j] * t; n       } n       b[i] = b[i] - b[ii] * t; n     } n     ++ii; n   } n   for (int jj = 0; jj <= m; ++jj) { n     int ii = w[jj]; n     if (ii == 0) continue; n     g[jj] = b[ii] / a[ii][jj]; n   } n   for (int x = 0; x < M; ++x) { n     ban u; n     for (int j = 0; j <= m; ++j) { n       u = u + g[j] * ast(ban(x), j); n     } n     if (u.x == 0) return x; n   } n   return -1; n } n int main() { n   int x = solv(); n   cout <<  !   << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n int pprime[1010] = {0}; n int prime[1010]; n int total; n void Getprime() { n   int i, j; n   for (i = 2; i * i <= 1010; i++) { n     if (!pprime[i]) { n       for (j = i; j * i <= 1010; j++) pprime[j * i] = 1; n     } n   } n   total = 0; n   for (i = 2; i <= 1010; i++) n     if (!pprime[i]) prime[total++] = i; n } n struct Edge { n   int next; n   int v; n   int gcd, lcm; n } edge[10010]; n int head[110]; n int pos; n void insert(int x, int y, int gcd, int lcm) { n   edge[pos].next = head[x]; n   edge[pos].v = y; n   edge[pos].gcd = gcd; n   edge[pos].lcm = lcm; n   head[x] = pos++; n } n int ans[110]; n int mark[110]; n bool dfs(int now, int pre) { n   int i; n   int l, r; n   for (i = head[now]; i; i = edge[i].next) { n     int v = edge[i].v; n     if (v == pre) continue; n     l = edge[i].gcd; n     r = edge[i].lcm; n     if (mark[now] != l && mark[now] != r) return false; n     if (mark[now] == l) { n       if (mark[v] == r) n         continue; n       else if (mark[v] == -1) { n         mark[v] = r; n         if (!dfs(v, now)) { n           mark[v] = -1; n           return false; n         } n       } else n         return false; n     } else { n       if (mark[v] == l) n         continue; n       else if (mark[v] == -1) { n         mark[v] = l; n         if (!dfs(v, now)) { n           mark[v] = -1; n           return false; n         } n       } else n         return false; n     } n   } n   return true; n } n struct fff { n   int x, y, gcd, lcm; n } ooxx[10010]; n int judge(int m) { n   for (int i = 0; i < m; i++) { n     if (ooxx[i].gcd != 1) return ooxx[i].gcd; n     if (ooxx[i].lcm != 1) return ooxx[i].lcm; n   } n   return 0; n } n int main() { n   Getprime(); n   int n, m; n   int x, y; n   int l, r; n   int now; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) ans[i] = 1; n   for (int i = 0; i < m; i++) n     scanf( %d %d %d %d , &ooxx[i].x, &ooxx[i].y, &ooxx[i].gcd, &ooxx[i].lcm); n   for (int i = 0; i < m; i++) n     if (ooxx[i].lcm % ooxx[i].gcd != 0) { n       printf( NO n ); n       return 0; n     } n   int flag; n   for (int k = 0; k < total; k++) { n     flag = judge(m); n     if (!flag) break; n     now = prime[k]; n     memset(head, 0, sizeof(head)); n     pos = 1; n     for (int i = 0; i < m; i++) { n       x = ooxx[i].x; n       y = ooxx[i].y; n       l = 0; n       r = 0; n       while (ooxx[i].gcd % now == 0) { n         l++; n         ooxx[i].gcd /= now; n       } n       while (ooxx[i].lcm % now == 0) { n         r++; n         ooxx[i].lcm /= now; n       } n       insert(x, y, l, r); n       insert(y, x, l, r); n     } n     memset(mark, -1, sizeof(mark)); n     for (int i = 1; i <= n; i++) n       if (mark[i] == -1) { n         if (!head[i]) { n           mark[i] = 0; n           continue; n         } n         int j = head[i]; n         l = edge[j].gcd; n         r = edge[j].lcm; n         mark[i] = l; n         if (dfs(i, -1)) continue; n         mark[i] = r; n         if (dfs(i, -1)) continue; n         printf( NO n ); n         return 0; n       } n     for (int i = 1; i <= n; i++) { n       int add = 1; n       int time = mark[i]; n       while (time--) add *= now; n       ans[i] *= add; n     } n   } n   while (1) { n     flag = judge(m); n     if (!flag) break; n     now = flag; n     memset(head, 0, sizeof(head)); n     pos = 1; n     for (int i = 0; i < m; i++) { n       x = ooxx[i].x; n       y = ooxx[i].y; n       l = 0; n       r = 0; n       while (ooxx[i].gcd % now == 0) { n         l++; n         ooxx[i].gcd /= now; n       } n       while (ooxx[i].lcm % now == 0) { n         r++; n         ooxx[i].lcm /= now; n       } n       insert(x, y, l, r); n       insert(y, x, l, r); n     } n     memset(mark, -1, sizeof(mark)); n     for (int i = 1; i <= n; i++) n       if (mark[i] == -1) { n         if (!head[i]) { n           mark[i] = 0; n           continue; n         } n         int j = head[i]; n         l = edge[j].gcd; n         r = edge[j].lcm; n         mark[i] = l; n         if (dfs(i, -1)) continue; n         mark[i] = r; n         if (dfs(i, -1)) continue; n         printf( NO n ); n         return 0; n       } n     for (int i = 1; i <= n; i++) { n       int add = 1; n       int time = mark[i]; n       while (time--) add *= now; n       ans[i] *= add; n     } n   } n   printf( YES n ); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n const int maxn = 1e6 + 50; n int seg[2 * maxn], ans[maxn], last_pos[maxn], next_pos[maxn]; n int n, m; n vector<int> v, get_val, partial_xor; n vector<pair<pair<int, int>, int> > ques; n void update(int val, int l, int r = n) { n   for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n     if (l & 1) seg[l++] ^= val; n     if (r & 1) seg[--r] ^= val; n   } n } n int get_xor(int idx) { n   int res = 0; n   for (idx += n; idx; idx >>= 1) res ^= seg[idx]; n   return res; n } n void sweep(int pos) { n   int val = get_val[v[pos]]; n   int nxt = next_pos[pos]; n   update(val, pos, nxt); n } n void input() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     int b; n     cin >> b; n     if (i == 0) n       partial_xor.push_back(b); n     else n       partial_xor.push_back(partial_xor[i - 1] ^ b); n     get_val.push_back(b); n     v.push_back(b); n     last_pos[i] = n; n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     l--, r--; n     ques.push_back({{l, r}, i}); n   } n } n void map_it() { n   sort(get_val.begin(), get_val.end()); n   get_val.resize(unique((get_val).begin(), (get_val).end()) - get_val.begin()); n   for (int i = v.size() - 1; i > -1; i--) { n     v[i] = n         lower_bound((get_val).begin(), (get_val).end(), v[i]) - get_val.begin(); n     next_pos[i] = last_pos[v[i]]; n     last_pos[v[i]] = i; n   } n   for (int i = 0; i < get_val.size(); i++) update(get_val[i], last_pos[i]); n } n void answer_ques(int i) { n   pair<int, int> p = ques[i].first; n   ans[ques[i].second] = get_xor(p.second) ^ partial_xor[p.first] ^ n                         partial_xor[p.second] ^ get_val[v[p.first]]; n } n void calc() { n   int go_pos = 0; n   int i = 0; n   sort(ques.begin(), ques.end()); n   while (go_pos < ques.size()) { n     while (go_pos < ques.size() and i == ques[go_pos].first.first) n       answer_ques(go_pos++); n     if (go_pos == ques.size()) break; n     while (i < ques[go_pos].first.first) sweep(i++); n   } n } n void ansing() { n   for (int i = 0; i < m; i++) cout << ans[i] <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   input(); n   map_it(); n   calc(); n   ansing(); n } n


#include <bits/stdc++.h> n const long long mod = 1e9 + 7; n const int N = 1e5 + 20; n using namespace std; n int main() { n   int n, s[N], m; n   s[0] = 0; n   cin >> n; n   for (int i = (1); i <= (n); i++) { n     cin >> m; n     s[i] = s[i - 1] + m; n   } n   cin >> m; n   for (int i = (1); i <= (m); i++) { n     int x, y; n     cin >> x >> y; n     cout << (s[y] - s[x - 1]) / 10 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   int out; n   cin >> m >> n; n   if (m >= n && m >= 2) { n     out = (m / 2) * n; n     if (m % 2 && n / 2) { n       out += (n / 2); n     } n   } else if (n > m && n >= 2) { n     out = (n / 2) * m; n     if (n % 2 && m / 2) { n       out += (m / 2); n     } n   } else { n     out = 0; n   } n   cout << out; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 7; n const int MOD = (int)1e9 + 7; n const long long INF = (long long)1e18 + 7; n vector<vector<long long>> a; n long long col[N], row[N], trow[N], tcol[N]; n long long res; n int m, n; n long long getrow() { n   long long ma = trow[1]; n   long long pos = 1; n   for (int i = 2; i <= m; i++) n     if (trow[i] > ma) { n       ma = trow[i]; n       pos = i; n     } n   trow[pos] = 0; n   for (int i = 1; i <= n; i++) tcol[i] -= a[pos][i]; n   return max(0ll, ma); n } n long long getcol() { n   long long ma = tcol[1]; n   long long pos = 1; n   for (int i = 2; i <= n; i++) n     if (tcol[i] > ma) { n       ma = tcol[i]; n       pos = i; n     } n   tcol[pos] = 0; n   for (int i = 1; i <= m; i++) trow[i] -= a[i][pos]; n   return max(0ll, ma); n } n void sett() { n   for (int i = 1; i <= m; i++) trow[i] = row[i]; n   for (int i = 1; i <= n; i++) tcol[i] = col[i]; n } n void proces(int st, int nd, int rd, int th) { n   long long ma = 0; n   sett(); n   if (st) n     ma += getcol(); n   else n     ma += getrow(); n   if (nd) n     ma += getcol(); n   else n     ma += getrow(); n   if (rd) n     ma += getcol(); n   else n     ma += getrow(); n   if (th) n     ma += getcol(); n   else n     ma += getrow(); n   res = max(res, ma); n } n int main() { n   cin >> m >> n; n   a.resize(m + 1); n   for (int i = 1; i <= m; i++) { n     a[i].push_back(0); n     long long t; n     for (int j = 1; j <= n; j++) { n       cin >> t; n       a[i].push_back(t); n     } n   } n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) row[i] += a[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) col[i] += a[j][i]; n   for (int a1 = 0; a1 <= 1; a1++) n     for (int a2 = 0; a2 <= 1; a2++) n       for (int a3 = 0; a3 <= 1; a3++) n         for (int a4 = 0; a4 <= 1; a4++) proces(a1, a2, a3, a4); n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[10001]; n const long long INF = 1e60 + 7; n long long binpow(long long a, long long b) { n   if (a == 0) { n     return 0; n   } n   if (b == 0) return 1; n   long long temp = binpow(a, b / 2); n   if (b & 1) { n     return temp * temp * a; n   } else n     return temp * temp; n } n long long poweroftwo(long long n) { n   if ((n && n & (n - 1)) == 0) n     cout <<  y ; n   else n     cout <<  n ; n } n long long binarysearch(long long a[], long long l, long long r, long long x) { n   while (l <= r) { n     long long mid = l + (r - l) / 2; n     if (a[mid] == x) n       return 1; n     else if (a[mid] > x) { n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   return 0; n } n long long findSplitPoint(long long arr[], long long n) { n   long long leftSum = 0; n   for (long long i = 0; i < n; i++) { n     leftSum += arr[i]; n     long long rightSum = 0; n     for (long long j = i + 1; j < n; j++) rightSum += arr[j]; n     if (leftSum == rightSum) return i + 1; n   } n   return -1; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   { n     long long n, m; n     cin >> n >> m; n     long long ans = 0; n     for (long long i = 0; i < n; i++) { n       long long x; n       cin >> x; n       ans = ans + x; n       cout << ans / m <<    ; n       ans = ans % m; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int fx[] = {+1, -1, +0, +0, +1, +1, -1, -1, +0}; n int fy[] = {+0, +0, +1, -1, +1, -1, +1, -1, +0}; n template <typename T> n inline T GCD(T a, T b) { n   a = abs(a); n   b = abs(b); n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n template <typename T> n inline T LCM(T x, T y) { n   T tp = GCD(x, y); n   if ((x / tp) * 1. * y > 9e18) return 9e18; n   return (x / tp) * y; n } n template <typename T> n inline T BigMod(T A, T B, T M) { n   T ret = 1; n   while (B) { n     if (B & 1) ret = (ret * A) % M; n     A = (A * A) % M; n     B = B >> 1; n   } n   return ret; n } n template <typename T> n inline T ModInv(T A, T M) { n   return BigMod(A, M - 2, M); n } n template <typename T> n bool scani(T &n) { n   n = 0; n   bool got = false; n   bool negative = false; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) negative = true; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     got = true; n     n = n * 10 + c - 48; n     c = getchar(); n   } n   if (negative) n = ~(n - 1); n   return got; n } n template <typename T> n void write(T n, bool line = true) { n   if (n < 0) { n     putchar( - ); n     n = -n; n   } n   if (!n) { n     putchar( 0 ); n     if (line) putchar(  n ); n     return; n   } n   char buff[22]; n   int len = 0; n   while (n) buff[len++] = n % 10 + 48, n /= 10; n   for (int i = len - 1; i >= 0; i--) putchar(buff[i]); n   if (line) putchar(  n ); n } n int scans(char *a) { n   int i = 0; n   char c = 0; n   while (c < 33) c = getchar(); n   while (c > 33) { n     a[i++] = c; n     c = getchar(); n   } n   a[i] = 0; n   return i; n } n int main() { n   int a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e; n   write(min(min(min(a / 1, b / 1), min(c / 2, d / 7)), e / 4)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n bool vis[MAXN]; n char str[MAXN]; n int main() { n   gets(str); n   int len = strlen(str); n   char maxch = 0; n   memset(vis, false, sizeof(vis)); n   for (int i = len - 1; i >= 0; i--) { n     if (str[i] >= maxch) vis[i] = true; n     maxch = max(maxch, str[i]); n   } n   for (int i = 0; i < len; i++) { n     if (vis[i]) putchar(str[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n template <class T> n T gcd(T a, T b) { n   return !b ? a : gcd(b, a % b); n } n template <class T> n inline void umin(T &x, T y) { n   x = x < y ? x : y; n } n template <class T> n inline void umax(T &x, T y) { n   x = x > y ? x : y; n } n inline unsigned int R() { n   static unsigned int seed = 416; n   return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; n } n const int N = 505000; n int n; n map<int, int> Map; n pair<int, int> s[N]; n int len; n long long res; n priority_queue<long long, vector<long long>, greater<long long> > heap; n int main() { n   read(n); n   for (register int i = (1); i <= (n); i++) { n     int x; n     read(x); n     Map[x]++; n     res += x; n   } n   for (auto i : Map) s[++len] = pair<int, int>(i.first, i.second); n   int sum = 0; n   for (register int j = (len); j >= (1); j--) { n     int c = min(s[j].second, sum - 2 * ((int)heap.size())); n     vector<long long> cur(c, s[j].first); n     c = s[j].second - c; n     for (int i = 1; i <= c; i += 2) n       if (!heap.empty()) { n         long long t = heap.top(); n         heap.pop(); n         if (t < s[j].first) { n           cur.push_back(s[j].first); n           if (i < c) cur.push_back(s[j].first); n         } else { n           cur.push_back(t); n           if (t < 2 * s[j].first && i < c) cur.push_back(2 * s[j].first - t); n         } n       } n     for (auto i : cur) heap.push(i); n     sum += s[j].second; n   } n   while (!heap.empty()) res -= heap.top(), heap.pop(); n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n unsigned long long int factorial(unsigned long long int n) { n   unsigned long long int facto; n   if (n == 0) return 1; n   return facto = n * factorial(n - 1); n } n int main() { n   long long int a, b, GCD; n   unsigned long long int fa, fb; n   scanf( %I64d %I64d , &a, &b); n   if (a > b) { n     fb = factorial(b); n     printf( %I64d n , fb); n   } else { n     fa = factorial(a); n     printf( %I64d n , fa); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[101010], v[101010], t[101010]; n int n, s; n int l[1010101], r[1010101]; n bool check(double m) { n   memset(l, 0, sizeof(l)); n   memset(r, 0, sizeof(r)); n   for (int i = 1; i <= n; i++) { n     if (t[i] == 1) { n       if (1.0 * x[i] / v[i] < m) n         l[0]++; n       else { n         l[x[i]]++; n         l[max((long long)x[i], n               min(1000001LL, n                   (long long)floor((m * (v[i] + s) - x[i]) * (s - v[i]) / s + n                                    x[i])) + n                   1)]--; n       } n     } else { n       if (1.0 * (1000000 - x[i]) / v[i] < m) n         r[0]++; n       else { n         r[min( n             x[i] + 1LL, n             max(0LL, (long long)ceil(x[i] - (m * (v[i] + s) + x[i] - 1000000) * n                                                 (s - v[i]) / s)))]++; n         r[x[i] + 1]--; n       } n     } n   } n   for (int i = 1; i <= 1000000; i++) { n     l[i] += l[i - 1]; n     r[i] += r[i - 1]; n     if (l[i] && r[i]) return true; n   } n   return false; n } n int main() { n   scanf( %d %d , &n, &s); n   for (int i = 1; i <= n; i++) scanf( %d %d %d , &x[i], &v[i], &t[i]); n   double l1 = 1e-8, r1 = 1e6; n   while (r1 - l1 > 1e-7) { n     double mid = (l1 + r1) / 2; n     if (check(mid)) n       r1 = mid; n     else n       l1 = mid; n   } n   printf( %.12lf n , (l1 + r1) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, ans; n int main() { n   scanf( %d %d %d , &n, &a, &b); n   if (b < 0) { n     for (int i = b; i < 0; i++) { n       a--; n       if (a == 0) { n         a = n; n       } n     } n     ans = a; n   } else { n     for (int i = 0; i < b; i++) { n       a++; n       if (a > n) { n         a = 1; n       } n     } n     ans = a; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 40; n const int MAXM = 210; n int n, m, A[MAXN]; n double p[MAXM][MAXN][MAXN]; n void lucky() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &A[i]); n } n int main() { n   lucky(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) p[0][i][j] = (A[i] > A[j]); n   double rat = 1.0 / (0.5 * n * (n + 1)); n   for (int R = 1; R <= m; R++) n     for (int l = 1; l <= n; l++) n       for (int r = l; r <= n; r++) { n         for (int i = 1; i <= n; i++) n           for (int j = 1; j <= n; j++) { n             int x = (l <= i && i <= r) ? l + r - i : i; n             int y = (l <= j && j <= r) ? l + r - j : j; n             p[R][i][j] += rat * p[R - 1][x][y]; n           } n       } n   double ans = 0; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) ans += p[m][i][j]; n   printf( %.10lf n , ans); n   return 0; n } n


#include<bits/stdc++.h> n   n using namespace std; n   n typedef unsigned long long int ull; n typedef long long int ll; n typedef long double ld; n   n #define PB push_back n #define MP make_pair n #define F first n #define S second n #define Pair vector<pair<ll,ll>> n #define vec vector <ll>  n #define all(a) a.begin(),a.end() n #define mo 1000000007 n #define decimal(i,n) fixed << setprecision(i) << n n #define show(a) for(auto xyz:a)cout<<xyz<<   ;cout<<endl; n #define show_nl(a) for(auto xyz:a)cout<<xyz<<endl; n #define MAX (ll)(pow(2,63)-1) n #define fill(a,b) memset(a, b, sizeof(a)) n #define yes cout<< YES <<endl; n #define no cout<< NO <<endl; n   n //modular inverse n void eea(ll a,ll b,ll &x,ll &y) n { n     if (a == 1) n     { n         x = 1; y = 0; n         return; n     } n     ll x1, y1; n     eea(b%a,a,x1,y1); n     x=y1-(b/a)*x1; n     y=x1; n } n ll mi(ll a,ll m) n { n     ll x,y; n     eea(a,m,x,y); n     x= (x%m+m)%m; n     return x; n } n //complete n   n void Sieve(int n)  n {  n     int prime[n+1]; n     fill(prime,-1); n     for (int p=2; p*p<=n; p++)  n     {  n         if (prime[p] == -1)  n         {  n             for (int i=p*p; i<=n; i += p)  n                 prime[i] = 0;  n         }  n     }  n     for (int p=2; p<=n; p++)  n     { n          n     }        n } n   n   n   n int main() n { n     ios::sync_with_stdio(0); n     cin.tie(0); cout.tie(0);   n   n     // freopen( ladder.in ,  r , stdin);   // name of input file n  // freopen( ladder.out ,  w , stdout); // name of output file n   n     // ifstream fin( art.in ); n     // ofstream fout( art.out ); n      n     // fstream file, output; n  // file.open(R () , ios::in);        // add the address of local input file n  // output.open(R () , ios::out);  // add the address of local output file n        n     ll t=1,t1=1; n     cin>>t; n     while(t--) n     { n         ll n; n         cin>>n; n         string s; n         cin>>s; n         ll t=0,m=0,x=0; n         for(ll i=0;i<n;i++) n         { n             if(s[i]== M ) n             { n                 m++; n             } n             else if(s[i]== T ) n             { n                 t++; n             } n             if(m>t) n             x=1; n         } n         t=0,m=0; n         for(ll i=n-1;i>=0;i--) n         { n             if(s[i]== M ) n             { n                 m++; n             } n             else if(s[i]== T ) n             { n                 t++; n             } n             if(m>t) n             x=1; n         } n         if(2*m!=t) n         x=1; n         if(x==1) n         cout<< NO n ; n         else n         cout<< YES n ; n   n     }   n } n   n /* n Some helpful points n 1.) Calm yourself n 2.) check for semicolon or data type mismatch n 3.) It can be solved n */


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int z = 0; n   for (int i = 0; i < n; ++i) { n     int t; n     cin >> t; n     if ((5 - t) >= k) ++z; n   } n   cout << z / 3; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void getre() { n   int x = 0; n   printf( %d n , 1 / x); n } n void gettle() { n   int res = 1; n   while (1) res <<= 1; n   printf( %d n , res); n } n template <typename T, typename S> n inline bool upmin(T &a, const S &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T, typename S> n inline bool upmax(T &a, const S &b) { n   return a < b ? a = b, 1 : 0; n } n template <typename N, typename PN> n inline N flo(N a, PN b) { n   return a >= 0 ? a / b : -((-a - 1) / b) - 1; n } n template <typename N, typename PN> n inline N cei(N a, PN b) { n   return a > 0 ? (a - 1) / b + 1 : -(-a / b); n } n template <typename N> n N gcd(N a, N b) { n   return b ? gcd(b, a % b) : a; n } n template <typename N> n inline int sgn(N a) { n   return a > 0 ? 1 : (a < 0 ? -1 : 0); n } n inline void gn(long long &x) { n   int sg = 1; n   char c; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   c ==  -  ? (sg = -1, x = 0) : (x = c -  0 ); n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= sg; n } n inline void gn(int &x) { n   long long t; n   gn(t); n   x = t; n } n inline void gn(unsigned long long &x) { n   long long t; n   gn(t); n   x = t; n } n inline void gn(double &x) { n   double t; n   scanf( %lf , &t); n   x = t; n } n inline void gn(long double &x) { n   double t; n   scanf( %lf , &t); n   x = t; n } n inline void gs(char *s) { scanf( %s , s); } n inline void gc(char &c) { n   while ((c = getchar()) > 126 || c < 33) n     ; n } n inline void pc(char c) { putchar(c); } n inline long long sqr(long long a) { return a * a; } n inline double sqrf(double a) { return a * a; } n const int inf = 0x3f3f3f3f; n const double pi = 3.14159265358979323846264338327950288L; n const double eps = 1e-6; n const int mo = 1; n int qp(int a, long long b) { n   int n = 1; n   do { n     if (b & 1) n = 1ll * n * a % mo; n     a = 1ll * a * a % mo; n   } while (b >>= 1); n   return n; n } n int n; n int a[222]; n int nok[33333]; n int ok[33333]; n int bo[222][222]; n const int dinic_inf = 0x3f3f3f3f; n const int DINIC_MAXV = 555 + 5; n const int DINIC_MAXE = 55555 + 5; n int s, t, vtot; n struct edge { n   int v, next; n   int f; n } e[DINIC_MAXE * 2]; n int g[DINIC_MAXV], etot, eid; n int ae(int u, int v, int f) { n   upmax(vtot, u); n   upmax(vtot, v); n   e[etot].v = v; n   e[etot].f = f; n   e[etot].next = g[u]; n   g[u] = etot++; n   e[etot].v = u; n   e[etot].f = 0; n   e[etot].next = g[v]; n   g[v] = etot++; n   return eid++; n } n int d[DINIC_MAXV]; n bool lb() { n   for (int i = 0; i <= vtot; i++) d[i] = 0; n   static int qu[DINIC_MAXV]; n   int p = 0, q = 0; n   qu[q++] = s, d[s] = 1; n   while (p != q) { n     int u = qu[p++]; n     for (int i = g[u]; ~i; i = e[i].next) n       if (e[i].f && !d[e[i].v]) { n         d[e[i].v] = d[u] + 1; n         if (e[i].v == t) return 1; n         qu[q++] = e[i].v; n       } n   } n   return 0; n } n int aug(int u, int mi) { n   if (u == t) return mi; n   int su = 0, del; n   for (int i = g[u]; ~i; i = e[i].next) n     if (e[i].f && d[e[i].v] == d[u] + 1) { n       del = aug(e[i].v, min(mi, e[i].f)); n       e[i].f -= del; n       e[i ^ 1].f += del; n       mi -= del; n       su += del; n       if (!mi) break; n     } n   if (!su) d[u] = -1; n   return su; n } n int dinic() { n   int su = 0; n   while (lb()) su += aug(s, dinic_inf); n   return su; n } n void dinic_init() { n   static bool ini = 0; n   if (!ini) { n     ini = 1; n     memset(g, -1, sizeof(g)); n   } else { n     for (int i = 0; i <= vtot; i++) g[i] = -1; n   } n   etot = eid = 0; n   vtot = 2, s = 1, t = 2; n } n inline int capaof(int ei) { return e[ei << 1].f + e[ei << 1 ^ 1].f; } n inline int flowof(int ei) { return e[ei << 1 ^ 1].f; } n inline int resiof(int ei) { return e[ei << 1].f; } n inline int uof(int ei) { return e[ei << 1 ^ 1].v; } n inline int vof(int ei) { return e[ei << 1].v; } n vector<int> nei[222]; n int vis[222]; n int cur = 0; n vector<int> ans[222]; n void dfs(int u) { n   if (vis[u]) return; n   ans[cur].push_back(u); n   vis[u] = 1; n   dfs(nei[u][0]); n   dfs(nei[u][1]); n } n int main() { n   nok[1] = 1; n   for (int i = 2; i <= 30000; i++) n     if (!nok[i]) n       for (int j = i + i; j <= 30000; j += i) nok[j] = 1; n   for (int i = 2; i <= 30000; i++) ok[i] = !nok[i]; n   gn(n); n   for (int i = (1), _ed = (n + 1); i < _ed; i++) gn(a[i]); n   for (int i = (1), _ed = (n + 1); i < _ed; i++) n     for (int j = (1), _ed = (n + 1); j < _ed; j++) n       if (i != j && ok[a[i] + a[j]]) bo[i][j] = 1; n   int nu0 = 0, nu1 = 0; n   for (int i = (1), _ed = (n + 1); i < _ed; i++) n     if (a[i] & 1) n       nu1++; n     else n       nu0++; n   if (nu0 != nu1) { n     printf( Impossible n ); n     return 0; n   } n   dinic_init(); n   for (int i = (1), _ed = (n + 1); i < _ed; i++) n     if (a[i] & 1) n       for (int j = (1), _ed = (n + 1); j < _ed; j++) n         if (bo[i][j]) ae(i, j, 1); n   s = n + 1, t = n + 2; n   for (int i = (1), _ed = (n + 1); i < _ed; i++) n     if (a[i] & 1) n       ae(s, i, 2); n     else n       ae(i, t, 2); n   int su = dinic(); n   if (su != n) { n     printf( Impossible n ); n     return 0; n   } n   for (int i = (0), _ed = (eid); i < _ed; i++) n     if (uof(i) != s && vof(i) != t && flowof(i) == 1) { n       nei[uof(i)].push_back(vof(i)); n       nei[vof(i)].push_back(uof(i)); n     } n   for (int i = (1), _ed = (n + 1); i < _ed; i++) n     if (!vis[i]) { n       ++cur; n       dfs(i); n     } n   printf( %d , cur); n   for (int i = (1), _ed = (cur + 1); i < _ed; i++) { n     printf(  n%d , ans[i].size()); n     for (int j = (0), _ed = (((int)(ans[i]).size())); j < _ed; j++) n       printf(  %d , ans[i][j]); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n using namespace std; n const int N = 101010, mod = 1e9 + 7; n namespace { n inline int add(int x, int y) { return (x += y) >= mod ? x - mod : x; } n inline int sub(int x, int y) { return (x -= y) < 0 ? x + mod : x; } n inline int mul(int x, int y) { return 1LL * x * y % mod; } n }  // namespace n struct edge { n   int u, v, w; n } e[N]; n int n, K, fac[N]; n map<long long, int> f[55][55]; n vector<pair<int, int> > adj[N]; n vector<int> allu, allv; n long long least[N]; n inline void U(int &x, int y) { (x += y) >= mod ? x -= mod : x; } n int main(void) { n   scanf( %d%d , &n, &K); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) { n     fac[i] = mul(fac[i - 1], i); n   } n   for (int i = 1, u, v, w; i <= K; i++) { n     scanf( %d%d%d , &u, &v, &w); n     e[i] = (edge){u, v, sub(w, 1)}; n     allu.push_back(u); n     allv.push_back(v); n   } n   sort(allu.begin(), allu.end()); n   sort(allv.begin(), allv.end()); n   allu.erase(unique(allu.begin(), allu.end()), allu.end()); n   allv.erase(unique(allv.begin(), allv.end()), allv.end()); n   int m = allu.size(); n   for (int i = 1; i <= K; i++) { n     int u = lower_bound(allu.begin(), allu.end(), e[i].u) - allu.begin(); n     int v = lower_bound(allv.begin(), allv.end(), e[i].v) - allv.begin(); n     adj[u].push_back((pair<int, int>){v, e[i].w}); n   } n   for (int i = m - 1; ~i; i--) { n     least[i] = least[i + 1]; n     for (auto p : adj[i]) { n       least[i] |= 1LL << p.first; n     } n   } n   f[0][0][0] = 1; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j <= i; j++) { n       for (auto p : f[i][j]) { n         U(f[i + 1][j][p.first & least[i + 1]], p.second); n         for (auto t : adj[i]) { n           if (~p.first >> t.first & 1) { n             U(f[i + 1][j + 1][(p.first | (1LL << t.first)) & least[i + 1]], n               mul(p.second, t.second)); n           } n         } n       } n     } n   } n   int ans = 0; n   for (int i = 0; i <= m; i++) { n     for (auto p : f[m][i]) { n       U(ans, mul(p.second, fac[n - i])); 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), cin.tie(NULL); n   long long k = 0; n   cin >> k; n   if (k == 1) { n     cout <<  4 ; n     return 0; n   } n   string s =   ; n   unsigned long long a = 1000000000000000000; n   unsigned long long num = 0; n   long long f = 0; n   while (k > 0) { n     if (k >= 2) { n       k -= 2; n       s += to_string(8); n       num = (num * 10) + 8; n     } else { n       k -= 1; n       s += to_string(0); n       num = (num * 10) + 0; n     } n     if (num > a) { n       f = 1; n       break; n     } n   } n   if (f == 0) n     cout << s; n   else n     cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int h[36020]; n int main() { n   char sir[4005]; n   int a, n, i, j, suma, v[4005]; n   cin >> a; n   cin.get(); n   cin.get(sir + 1, 4005); n   n = strlen(sir + 1); n   v[0] = 0; n   for (i = 1; i <= n; i++) v[i] = v[i - 1] + (sir[i] -  0 ); n   for (i = 1; i <= n; i++) n     for (j = i; j <= n; j++) { n       suma = v[j] - v[i - 1]; n       h[suma]++; n     } n   long long int rasp = 0; n   for (i = 1; i <= n; i++) n     for (j = i; j <= n; j++) { n       suma = v[j] - v[i - 1]; n       if (a == 0) { n         if (suma == 0) n           rasp += ((n * (n + 1)) / 2); n         else n           rasp += h[0]; n       } else if (suma != 0) n         if (a % suma == 0) rasp += (((a / suma) < 36020) ? h[a / suma] : 0); n     } n   cout << rasp <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n unordered_map<int, vector<int> > g; n map<pair<int, int>, int> weights; n map<pair<int, int>, int> idx; n unordered_map<int, bool> used; n struct elem { n   int prev; n   int u; n   long long dist; n   elem(int prev, int u, long long dist) : prev(prev), u(u), dist(dist) {} n   friend bool operator<(const elem& lh, const elem& rh) { n     return lh.dist > rh.dist; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 1; i <= m; ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     g[u].push_back(v); n     g[v].push_back(u); n     weights[make_pair(u, v)] = w; n     weights[make_pair(v, u)] = w; n     idx[make_pair(u, v)] = i; n     idx[make_pair(v, u)] = i; n   } n   vector<int> res; n   priority_queue<elem> q; n   q.push(elem(0, 1, 0)); n   int taken = 0; n   int mx = min(n, k); n   while (!q.empty() && mx != taken) { n     int prev = q.top().prev; n     int u = q.top().u; n     long long dist = q.top().dist; n     q.pop(); n     if (used[u]) continue; n     used[u] = true; n     if (prev) { n       res.push_back(idx[make_pair(prev, u)]); n       ++taken; n     } n     for (int v : g[u]) n       if (!used[v]) { n         q.push(elem(u, v, dist + weights[make_pair(u, v)])); n       } n   } n   cout << res.size() << endl; n   for (int i : res) cout << i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100005]; n long long ans = 0; n int main() { n   int n; n   cin >> n; n   vector<int> a; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] % 2) n       a.push_back(arr[i]); n     else n       ans += arr[i]; n   } n   int i; n   if ((int)a.size() % 2) n     i = 1; n   else n     i = 0; n   sort(a.begin(), a.end()); n   int sz = a.size(); n   for (; i < sz; i++) ans += a[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9; n const long long INFL = 1e18; n const int N = 3e5; n int n, m, R; n vector<int> adj[N + 5]; n pair<int, int> E[2 * N + 5]; n long long d[N + 5]; n struct cmp { n   bool operator()(pair<int, long long> x, pair<int, long long> y) { n     return (x.second > y.second); n   } n }; n priority_queue<pair<int, long long>, vector<pair<int, long long> >, cmp> H; n int tr[N + 5]; n void Dijkstra() { n   for (int i = (1), _b = (n); i <= _b; ++i) d[i] = INFL; n   d[R] = 0; n   H.push(make_pair(R, d[R])); n   while (!H.empty()) { n     int u = H.top().first; n     long long oldDist = H.top().second; n     H.pop(); n     if (oldDist != d[u]) continue; n     for (int i = (0), _b = (((int)(adj[u]).size())); i < _b; ++i) { n       int v = E[adj[u][i]].first, w = E[adj[u][i]].second; n       if (d[u] + w < d[v] || n           d[u] + w == d[v] && E[adj[u][i]].second < E[tr[v]].second) { n         d[v] = d[u] + w; n         H.push(make_pair(v, d[v])); n         tr[v] = adj[u][i]; n       } n     } n   } n } n vector<int> pth; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   int cntE = 0; n   while (m--) { n     int u, v, w; n     cin >> u >> v >> w; n     E[++cntE] = make_pair(v, w); n     adj[u].push_back(cntE); n     E[++cntE] = make_pair(u, w); n     adj[v].push_back(cntE); n   } n   cin >> R; n   Dijkstra(); n   long long res = 0; n   for (int i = (1), _b = (n); i <= _b; ++i) n     if (i != R) { n       res += E[tr[i]].second; n       pth.push_back(tr[i]); n     } n   cout << res <<   n ; n   for (int i = (0), _b = (((int)(pth).size())); i < _b; ++i) n     cout << (pth[i] + 1) / 2 <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 2e5 + 10, MOD = 1e9 + 7; n long long n, T, ni[MAXN], jie[MAXN], jie_ni[MAXN], A[MAXN], ans, sum; n long long sum_N, sum_K; n void Init() { n   ni[1] = 1; n   for (long long i = 2; i <= MAXN - 10; i++) { n     ni[i] = ((MOD - (MOD / i)) * ni[MOD % i] % MOD) % MOD; n   } n   jie_ni[0] = 1; n   for (long long i = 1; i <= MAXN - 10; i++) { n     jie_ni[i] = jie_ni[i - 1] * ni[i] % MOD; n   } n   jie[0] = 1; n   for (long long i = 1; i <= MAXN - 10; i++) { n     jie[i] = jie[i - 1] * i % MOD; n   } n } n long long Quick(long long x, long long y) { n   long long res = 1, a = x; n   while (y) { n     if (y & 1) res *= a, res %= MOD; n     a = a * a, a %= MOD; n     y /= 2; n   } n   return res; n } n long long C(long long x, long long y) { n   if (y == 0) return 1; n   if (x == y) return 1; n   return (jie[x] * jie_ni[y]) % MOD * jie_ni[x - y] % MOD; n } n long long work(long long N, long long K) { n   if (sum_N == 0) { n     sum_N = 1; n     for (long long i = 1; i <= K; i++) sum_N += C(N, i), sum_N %= MOD; n     sum_K = K; n     return sum_N; n   } else { n     sum_N = (sum_N * 2 - C(N - 1, sum_K) + MOD) % MOD; n     for (long long i = sum_K; i >= K + 1; i--) { n       sum_N -= C(N, i), sum_N = (sum_N + MOD) % MOD; n     } n     sum_K = K; n     return sum_N; n   } n } n signed main() { n   cin >> n >> T; n   Init(); n   for (long long i = 1; i <= n; i++) cin >> A[i]; n   long long BASE = 1; n   for (long long i = 1; i <= n; i++) { n     sum += A[i]; n     BASE = BASE * 2; n     BASE %= MOD; n     if (T - sum >= i) { n       ans++; n       ans %= MOD; n     } else if (T - sum < 0) { n       break; n     } else { n       ans += (work(i, T - sum) * Quick(BASE, MOD - 2)) % MOD; n       ans %= MOD; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[2000][2000]; n const int dx[4] = {-1, 0, 1, 0}; n const int dy[4] = {0, -1, 0, 1}; n queue<pair<int, int> > que; n int main() { n   int n, m; n   while (~scanf( %d %d , &n, &m)) { n     for (int i = 0; i < n; i++) { n       getchar(); n       for (int j = 0; j < m; j++) { n         a[i][j] = getchar(); n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         int cnt = 0; n         for (int k = 0; k < 4; k++) { n           int ni = i + dx[k]; n           int nj = j + dy[k]; n           if (ni >= 0 && ni < n && nj >= 0 && nj < m && a[ni][nj] ==  . ) { n             cnt++; n           } n         } n         if (cnt == 1) { n           que.push({i, j}); n         } n       } n     } n     while (!que.empty()) { n       pair<int, int> p = que.front(); n       que.pop(); n       int i = p.first, j = p.second, k, ni, nj; n       if (a[i][j] !=  . ) { n         continue; n       } n       for (k = 0; k < 4; k++) { n         ni = i + dx[k]; n         nj = j + dy[k]; n         if (ni >= 0 && ni < n && nj >= 0 && nj < m && a[ni][nj] ==  . ) { n           break; n         } n       } n       if (k == 0) { n         a[i][j] =  v ; n         a[i - 1][j] =  ^ ; n       } else if (k == 1) { n         a[i][j] =  > ; n         a[i][j - 1] =  < ; n       } else if (k == 2) { n         a[i][j] =  ^ ; n         a[i + 1][j] =  v ; n       } else if (k == 3) { n         a[i][j] =  < ; n         a[i][j + 1] =  > ; n       } else { n         continue; n       } n       for (int k = 0; k < 4; k++) { n         int mi = ni + dx[k]; n         int mj = nj + dy[k]; n         if (mi >= 0 && mi < n && mj >= 0 && mj < m && a[mi][mj] ==  . ) { n           int cnt = 0; n           for (int k = 0; k < 4; k++) { n             int pi = mi + dx[k]; n             int pj = mj + dy[k]; n             if (pi >= 0 && pi < n && pj >= 0 && pj < m && a[pi][pj] ==  . ) { n               cnt++; n             } n           } n           if (cnt == 1) { n             que.push({mi, mj}); n           } n         } n       } n     } n     bool flag = false; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (a[i][j] ==  . ) { n           flag = true; n           break; n         } n       } n       if (flag) { n         printf( Not unique n ); n         break; n       } n     } n     if (!flag) { n       for (int i = 0; i < n; i++) { n         for (int j = 0; j < m; j++) { n           putchar(a[i][j]); n         } n         putchar(10); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize  trapv  n const int inf1 = 1e9; n const long long int inf2 = 1e18; n const int N = 100000; n using namespace std; n int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n vector<int> z_function(string s) { n   int n = s.length(); n   vector<int> z(n); n   for (int i = 1, l = 0, r = 0; i < n; ++i) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n   return z; n } n bool ispalindrome(string s) { n   string t = s; n   reverse(s.begin(), s.end()); n   return t == s; n } n string LPP(string s) { n   if (s.length() == 1) return s; n   int l = s.length(); n   string t = s +  # ; n   string t2 = s; n   reverse(t2.begin(), t2.end()); n   t += t2; n   vector<int> z = z_function(t); n   int l2 = 2 * l + 1; n   for (int i = l + 1; i < l2; i++) { n     if (z[i] == (l2 - i)) { n       string ans = t.substr(i, l2 - i); n       return ans; n     } n   } n } n int solve() { n   string s; n   cin >> s; n   int n = s.length(); n   if (ispalindrome(s)) { n     cout << s <<   n ; n     return 0; n   } n   int i = 0; n   while (s[i] == s[n - 1 - i]) i++; n   string ans = s.substr(0, i); n   string ans4 = ans; n   reverse(ans4.begin(), ans4.end()); n   string s2 = s.substr(i, n - 2 * i); n   string s3 = s2; n   reverse(s3.begin(), s3.end()); n   string ans1 = LPP(s2); n   string ans2 = LPP(s3); n   if (ans1.size() >= ans2.size()) { n     cout << ans + ans1 + ans4 <<   n ; n   } else { n     cout << ans + ans2 + ans4 <<   n ; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T euclide(T a, T b, T& x, T& y) { n   if (a < 0) { n     T d = euclide(-a, b, x, y); n     x = -x; n     return d; n   } n   if (b < 0) { n     T d = euclide(a, -b, x, y); n     y = -y; n     return d; n   } n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     T d = euclide(b, a % b, x, y); n     T t = x; n     x = y; n     y = t - (a / b) * y; n     return d; n   } n } n template <class T> n inline void checkmin(T& a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T& a, T b) { n   if (b > a) a = b; n } n template <class T> n T Abs(T x) { n   return x > 0 ? x : -x; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline bool isPrime(T n) { n   if (n <= 1) return false; n   for (T i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n template <class T> n inline T Mod(T n, T m) { n   return (n % m + m) % m; n } n template <class T> n string toString(T n) { n   ostringstream oss; n   oss << n; n   oss.flush(); n   return oss.str(); n } n int toInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n long long toLl(string s) { n   long long r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n template <class T> n void debug(const T& e) { n   cout << e << endl; n } n template <class T1, class T2> n void debug(const T1& e1, const T2& e2) { n   cout << e1 <<   t  << e2 << endl; n } n template <class T1, class T2, class T3> n void debug(const T1& e1, const T2& e2, const T3& e3) { n   cout << e1 <<   t  << e2 <<   t  << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void debug(const T1& e1, const T2& e2, const T3& e3, const T4& e4) { n   cout << e1 <<   t  << e2 <<   t  << e3 <<   t  << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void debug(const T1& e1, const T2& e2, const T3& e3, const T4& e4, n            const T5& e5) { n   cout << e1 <<   t  << e2 <<   t  << e3 <<   t  << e4 <<   t  << e5 << endl; n } n template <class T> n void debug(vector<T>& e) { n   int i; n   for (i = 0; i < (int)e.size(); i++) cout << e[i] <<    ; n   cout << endl; n } n template <class T> n void debug(vector<basic_string<T> >& e) { n   int i, j; n   for (i = 0; i < (int)e.size(); i++) { n     for (j = 0; j < (int)e[i].size(); j++) cout << e[i][j]; n     cout << endl; n   } n   cout << endl; n } n template <class T> n void debug(vector<vector<T> >& e, int row, int col) { n   int i, j; n   for (i = 0; i < row; i++) { n     for (j = 0; j < col; j++) cout << e[i][j] <<   t ; n     cout << endl; n   } n   cout << endl; n } n template <class T> n void debug(T e[100010][100010], int row, int col) { n   int i, j; n   for (i = 0; i < row; i++) { n     for (j = 0; j < col; j++) cout << e[i][j] <<    ; n     cout << endl; n   } n } n long long Pow(long long B, long long P) { n   long long R = 1; n   while (P > 0) { n     if (P % 2 == 1) R = (R * B); n     P /= 2; n     B = (B * B); n   } n   return R; n } n int x[100010], y[100010], a[100010]; n int main() { n   int i, j, test, Case = 1, n, cnt; n   while (scanf( %d , &n) == 1) { n     for (i = 0; i < n; i++) scanf( %d , &a[i]); n     x[0] = a[0]; n     for (i = 1; i < n; i++) x[i] = a[i] + x[i - 1]; n     y[n - 1] = a[n - 1]; n     for (i = n - 2; i >= 0; i--) y[i] = a[i] + y[i + 1]; n     cnt = 0; n     for (i = 0; i < n - 1; i++) n       if (x[i] == y[i + 1]) cnt++; n     printf( %d n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) n     s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); n   return s * w; n } n int n, q; n long long A[300010], B[300010], C[300010], D[300010], E[300010]; n multiset<long long> S_Max, S_Min; n multiset<long long> S[300010]; n int main() { n   n = read(), q = read(); n   for (register int i = 1; i <= n; i++) B[i] = read(); n   for (register int i = 1; i <= n; i++) A[i] = read(), D[i] += 2, D[A[i]]++; n   for (register int i = 1; i <= n; i++) E[i] = B[i] / D[i]; n   for (register int i = 1; i <= n; i++) C[A[i]] += E[i]; n   for (register int i = 1; i <= n; i++) n     S[A[i]].insert(B[i] - D[i] * E[i] + E[i] + C[i]); n   for (register int i = 1; i <= n; i++) n     if (!S[i].empty()) { n       S_Max.insert(*S[i].rbegin() + E[i]); n       S_Min.insert(*S[i].begin() + E[i]); n     } n   while (q--) { n     int opt = read(); n     if (opt == 1) { n       int x = read(), y = read(), fa = A[x]; n       S_Max.erase(S_Max.find(*S[fa].rbegin() + E[fa])); n       S_Min.erase(S_Min.find(*S[fa].begin() + E[fa])); n       S[fa].erase(S[fa].find(B[x] - D[x] * E[x] + E[x] + C[x])); n       S_Max.erase(S_Max.find(*S[A[fa]].rbegin() + E[A[fa]])); n       S_Min.erase(S_Min.find(*S[A[fa]].begin() + E[A[fa]])); n       S[A[fa]].erase(S[A[fa]].find(B[fa] - D[fa] * E[fa] + E[fa] + C[fa])); n       S_Max.erase(S_Max.find(*S[A[A[fa]]].rbegin() + E[A[A[fa]]])); n       S_Min.erase(S_Min.find(*S[A[A[fa]]].begin() + E[A[A[fa]]])); n       S[A[A[fa]]].erase(S[A[A[fa]]].find(B[A[fa]] - D[A[fa]] * E[A[fa]] + n                                          E[A[fa]] + C[A[fa]])); n       C[A[fa]] -= E[fa]; n       D[fa]--, E[fa] = B[fa] / D[fa], C[fa] -= E[x]; n       C[A[fa]] += E[fa]; n       S[A[A[fa]]].insert(B[A[fa]] - D[A[fa]] * E[A[fa]] + E[A[fa]] + C[A[fa]]); n       S_Max.insert(*S[A[A[fa]]].rbegin() + E[A[A[fa]]]); n       S_Min.insert(*S[A[A[fa]]].begin() + E[A[A[fa]]]); n       S[A[fa]].insert(B[fa] - D[fa] * E[fa] + E[fa] + C[fa]); n       S_Max.insert(*S[A[fa]].rbegin() + E[A[fa]]); n       S_Min.insert(*S[A[fa]].begin() + E[A[fa]]); n       if (!S[fa].empty()) { n         S_Max.insert(*S[fa].rbegin() + E[fa]); n         S_Min.insert(*S[fa].begin() + E[fa]); n       } n       if (!S[y].empty()) { n         S_Max.erase(S_Max.find(*S[y].rbegin() + E[y])); n         S_Min.erase(S_Min.find(*S[y].begin() + E[y])); n       } n       S[y].insert(B[x] - D[x] * E[x] + E[x] + C[x]); n       S_Max.erase(S_Max.find(*S[A[y]].rbegin() + E[A[y]])); n       S_Min.erase(S_Min.find(*S[A[y]].begin() + E[A[y]])); n       S[A[y]].erase(S[A[y]].find(B[y] - D[y] * E[y] + E[y] + C[y])); n       S_Max.erase(S_Max.find(*S[A[A[y]]].rbegin() + E[A[A[y]]])); n       S_Min.erase(S_Min.find(*S[A[A[y]]].begin() + E[A[A[y]]])); n       S[A[A[y]]].erase( n           S[A[A[y]]].find(B[A[y]] - D[A[y]] * E[A[y]] + E[A[y]] + C[A[y]])); n       C[A[y]] -= E[y]; n       D[y]++, E[y] = B[y] / D[y], C[y] += E[x]; n       C[A[y]] += E[y]; n       S[A[A[y]]].insert(B[A[y]] - D[A[y]] * E[A[y]] + E[A[y]] + C[A[y]]); n       S_Max.insert(*S[A[A[y]]].rbegin() + E[A[A[y]]]); n       S_Min.insert(*S[A[A[y]]].begin() + E[A[A[y]]]); n       S[A[y]].insert(B[y] - D[y] * E[y] + E[y] + C[y]); n       S_Max.insert(*S[A[y]].rbegin() + E[A[y]]); n       S_Min.insert(*S[A[y]].begin() + E[A[y]]); n       S_Max.insert(*S[y].rbegin() + E[y]); n       S_Min.insert(*S[y].begin() + E[y]); n       A[x] = y; n     } else if (opt == 2) { n       int x = read(); n       printf( %lld n , B[x] - D[x] * E[x] + E[x] + C[x] + E[A[x]]); n     } else n       printf( %lld %lld n , *S_Min.begin(), *S_Max.rbegin()); 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   int N, M, Dx, Dy; n   cin >> N >> M >> Dx >> Dy; n   vector<pair<int, int>> way(N); n   way[0].first = way[0].second = 0; n   for (int i = 1; i < N; ++i) { n     way[i].first = (way[i - 1].first + Dx) % N; n     way[i].second = (way[i - 1].second + Dy) % N; n   } n   sort(way.begin(), way.end()); n   vector<pair<int, int>> trees(M); n   for (auto& p : trees) { n     cin >> p.first >> p.second; n   } n   sort(trees.begin(), trees.end()); n   map<int, int> lower, upper; n   for (const auto& tree : trees) { n     int x = tree.first; n     int y = tree.second; n     if (y == way[x].second) { n       lower[0]++; n       upper[0]++; n     } else if (y < way[x].second) { n       upper[way[x].second - y]++; n       lower[y + N - way[x].second]++; n     } else { n       lower[y - way[x].second]++; n       upper[N - y + way[x].second]++; n     } n   } n   auto it1 = n       max_element(lower.begin(), lower.end(), n                   [](const pair<int, int>& lhs, const pair<int, int>& rhs) { n                     return lhs.second < rhs.second; n                   }); n   auto it2 = n       max_element(upper.begin(), upper.end(), n                   [](const pair<int, int>& lhs, const pair<int, int>& rhs) { n                     return lhs.second < rhs.second; n                   }); n   if (it2->second >= it1->second) { n     cout << 0 <<     << (2 * N - it2->first) % N <<   n ; n   } else { n     cout << 0 <<     << it1->first % N <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, c = 0; n   cin >> n; n   int a, f[7]; n   for (int j = 0; j < 7; j++) f[j] = 0; n   for (int i = 0; i < n; i++) { n     cin >> a; n     for (int j = 0; j < 7; j++) { n       if (a % (int)pow(10, 1) == 1) f[j]++; n       a = a / 10; n     } n   } n   for (int i = 0; i < 7; i++) c = max(c, f[i]); n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int o, O, f, z, i, d; n string t, s[6] = { lios ,  liala ,  etr ,  etra ,  initis ,  inites }; n int V[ zz ]; n int main() { n   while (cin >> t) { n     for (i = O = 0; i < 6; i++) n       if ((d = t.size() - s[i].size()) < 0 ? 0 : t.substr(d) == s[i]) { n         if (z && V[z - 1] > i) break; n         O = 1; n         V[z++] = i; n         f += i / 2 == 1; n         o += i % 2; n       } n     if (!O) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout << (z != 1 && (o % z || f != 1) ?  NO n  :  YES n ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, son[100005], q; n int fir[100005], nxt[200005], to[200005], cnt; n long long wi[200005], z, dp[100005][2], f[100005]; n struct node { n   int id; n   long long val; n } c[100005]; n int size[100005], fa[100005], Fa[100005]; n long long read() { n   long long x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); n   return x; n } n void dfs(int x, int fa) { n   for (int i = fir[x]; i; i = nxt[i]) { n     if (to[i] == fa) continue; n     dfs(to[i], x); n     if (dp[x][0] < dp[to[i]][0] + wi[i]) n       dp[x][1] = dp[x][0], dp[x][0] = dp[to[i]][0] + wi[i], son[x] = to[i]; n     else if (dp[x][1] < dp[to[i]][0] + wi[i]) n       dp[x][1] = dp[to[i]][0] + wi[i]; n   } n } n void Dfs(int x, int fa, long long W) { n   f[x] = max(dp[x][0], W); n   for (int i = fir[x]; i; i = nxt[i]) { n     if (to[i] == fa) continue; n     if (to[i] == son[x]) n       Dfs(to[i], x, max(W, dp[x][1]) + wi[i]); n     else n       Dfs(to[i], x, max(W, dp[x][0]) + wi[i]); n   } n } n int cmp(node u, node v) { return u.val < v.val; } n void DFS(int x, int f) { n   Fa[x] = f; n   for (int i = fir[x]; i; i = nxt[i]) { n     if (to[i] == f) continue; n     DFS(to[i], x); n   } n } n int find(int x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n int main() { n   n = read(); n   for (int i = 1; i < n; i++) { n     x = read(), y = read(), z = read(); n     to[++cnt] = y, nxt[cnt] = fir[x], fir[x] = cnt, wi[cnt] = z; n     to[++cnt] = x, nxt[cnt] = fir[y], fir[y] = cnt, wi[cnt] = z; n   } n   dfs(1, 0); n   Dfs(1, 0, 0); n   for (int i = 1; i <= n; i++) c[i] = (node){i, f[i]}; n   sort(c + 1, c + n + 1, cmp); n   DFS(c[1].id, 0); n   q = read(); n   for (int i = 1; i <= q; i++) { n     z = read(); n     for (int i = 1; i <= n; i++) fa[i] = i; n     for (int i = 1; i <= n; i++) size[i] = 1; n     int r = n, ans = 0; n     for (int j = n; j >= 1; j--) { n       while (r && c[r].val - c[j].val > z) size[find(c[r].id)]--, r--; n       ans = max(ans, size[find(c[j].id)]); n       int u = find(c[j].id), v = find(Fa[c[j].id]); n       if (u != v) fa[u] = v, size[v] += size[u]; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[1 << 20], *p1 = buf, *p2 = buf; n template <typename T> n void read(T &x) { n   x = 0; n   bool f = 1; n   char c = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) n            ? EOF n            : *p1++); n   for (; !isdigit(c); n        c = (p1 == p2 && n                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) n                 ? EOF n                 : *p1++)) n     if (c ==  - ) f = 0; n   if (f) n     for (; isdigit(c); n          c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), n                           p1 == p2) n                   ? EOF n                   : *p1++)) n       x = x * 10 - 48 + c; n   else n     for (; isdigit(c); n          c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), n                           p1 == p2) n                   ? EOF n                   : *p1++)) n       x = x * 10 + 48 - c; n } n template <typename T, typename... Args> n void read(T &t, Args &...args) { n   read(t), read(args...); n } n const int N = 5e3 + 5; n int n, a[N]; n int raw, mn, cnt, f[N][N], g[N][N]; n int qf(int i, int l, int r) { return f[i][r] - f[i][l - 1]; } n int qg(int i, int l, int r) { return g[i][l] - g[i][r + 1]; } n int main() { n   read(n); n   for (int i = 1; i <= n; ++i) read(a[i]); n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) raw += a[i] > a[j]; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) f[i][j] = a[j] > a[i], g[i][j] = a[j] < a[i]; n     for (int j = 1; j <= n; ++j) f[i][j] += f[i][j - 1]; n     for (int j = n; j >= 1; --j) g[i][j] += g[i][j + 1]; n   } n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) { n       int cur = qf(i, i + 1, j - 1) - qg(i, i + 1, j - 1) - n                 qf(j, i + 1, j - 1) + qg(j, i + 1, j - 1) + n                 (a[i] < a[j] ? 1 : -1); n       if (cur < mn) n         mn = cur, cnt = 1; n       else if (cur == mn) n         ++cnt; n     } n   printf( %d %d , raw + mn, cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 150, M = 65; n int n; n int dist[N][N], g[N][N]; n vector<long long> v[M]; n map<long long, int> mp; n int main() { n   memset(g, 0x3f, sizeof g); n   memset(dist, 0x3f, sizeof dist); n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long t, x; n     cin >> x; n     t = x; n     int cnt = 0; n     while (t) { n       if (t & 1) v[cnt].push_back(x); n       t >>= 1; n       cnt++; n     } n   } n   int cnt = 0; n   bool flag = false; n   for (int i = 0; i <= 64; i++) { n     int m = v[i].size(); n     if (!m) n       continue; n     else if (m > 2) { n       flag = true; n       break; n     } else if (m == 2) { n       long long x = v[i][0], y = v[i][1]; n       if (!mp[x]) mp[x] = ++cnt; n       if (!mp[y]) mp[y] = ++cnt; n       x = mp[x], y = mp[y]; n       g[x][y] = g[y][x] = 1; n       dist[x][y] = dist[y][x] = 1; n     } n   } n   if (flag) n     cout << 3 << endl; n   else { n     int res = 0x3f3f3f3f; n     for (int k = 1; k <= cnt; k++) { n       for (int i = 1; i < k; i++) n         if (g[k][i] == 1) n           for (int j = i + 1; j < k; j++) n             if (g[k][j] == 1) n               if (dist[i][j] != 0x3f3f3f3f) n                 res = min(res, dist[i][j] + g[i][k] + g[j][k]); n       for (int i = 1; i <= cnt; i++) n         if (dist[i][k] != 0x3f3f3f3f) n           for (int j = 1; j <= cnt; j++) n             if (dist[k][j] != 0x3f3f3f3f) n               dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); n     } n     if (res != 0x3f3f3f3f) n       cout << res << endl; n     else n       cout << -1 << endl; n   } n } n


#include <bits/stdc++.h> n int n, k, a, b, c, d; n std::vector<int> first_path; n std::vector<int> second_path; n bool first_reversed = false; n bool second_reversed = false; n bool failed = false; n void read() { n   scanf( %d %d n%d %d %d %d , &n, &k, &a, &b, &c, &d); n   --a; n   --b; n   --c; n   --d; n   first_path.resize(n); n   second_path.resize(n); n } n void fill_with_everything_except(std::vector<int>::iterator begin, n                                  std::vector<int>::iterator end, n                                  const std::vector<int> &except) { n   for (int i = 0; begin != end; ++begin, ++i) { n     while (find(except.begin(), except.end(), i) != except.end()) { n       ++i; n     } n     *begin = i; n   } n } n void solve() { n   if (a > b) { n     std::swap(a, b); n     first_reversed = true; n   } n   if (c > d) { n     std::swap(c, d); n     second_reversed = true; n   } n   first_path[0] = a; n   first_path[n - 1] = b; n   if (a == c && b == d) { n     fill_with_everything_except(next(begin(first_path)), prev(end(first_path)), n                                 std::vector<int>{a, b}); n     copy(begin(first_path), end(first_path), begin(second_path)); n   } else { n     if (k < n) { n       failed = true; n       return; n     } n     if (a == c) { n       fill_with_everything_except(next(begin(first_path)), n                                   prev(prev(end(first_path))), n                                   std::vector<int>{a, b, d}); n       first_path[n - 2] = d; n       copy(begin(first_path), end(first_path), begin(second_path)); n       std::swap(second_path[n - 1], second_path[n - 2]); n     } else if (b == d) { n       fill_with_everything_except(next(next(begin(first_path))), n                                   prev(end(first_path)), n                                   std::vector<int>{a, b, c}); n       first_path[1] = c; n       copy(begin(first_path), end(first_path), begin(second_path)); n       std::swap(second_path[0], second_path[1]); n     } else { n       if (k < n + 1 || n == 4) { n         failed = true; n         return; n       } n       fill_with_everything_except(next(next(begin(first_path))), n                                   prev(prev(end(first_path))), n                                   std::vector<int>{a, b, c, d}); n       first_path[1] = c; n       first_path[n - 2] = d; n       copy(begin(first_path), end(first_path), begin(second_path)); n       std::swap(second_path[0], second_path[1]); n       std::swap(second_path[n - 1], second_path[n - 2]); n     } n   } n   if (first_reversed) { n     reverse(begin(first_path), end(first_path)); n   } n   if (second_reversed) { n     reverse(begin(second_path), end(second_path)); n   } n } n void print() { n   if (failed) { n     printf( -1 n ); n   } else { n     for (int i = 0; i < n; ++i) { n       printf( %d  , first_path[i] + 1); n     } n     printf(  n ); n     for (int i = 0; i < n; ++i) { n       printf( %d  , second_path[i] + 1); n     } n   } n } n int main() { n   read(); n   solve(); n   print(); n   return 0; n } n


/* n     author:  Maksim1744 n     created: 10.03.2021 16:47:29 n */ n  n #include  bits/stdc++.h  n  n using namespace std; n  n #define ll   long long n #define ld   long double n  n #define mp   make_pair n #define pb   push_back n #define eb   emplace_back n  n #define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll)) n #define mine(a)    (*min_element((a).begin(), (a).end())) n #define maxe(a)    (*max_element((a).begin(), (a).end())) n #define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin()) n #define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin()) n #define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin()) n #define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin()) n  n template<typename T>             vector<T>& operator--            (vector<T>& v){for (auto& i : v) --i;            return  v;} n template<typename T>             vector<T>& operator++            (vector<T>& v){for (auto& i : v) ++i;            return  v;} n template<typename T>             istream& operator>>(istream& is,  vector<T>& v){for (auto& i : v) is >> i;        return is;} n template<typename T>             ostream& operator<<(ostream& os,  vector<T>& v){for (auto& i : v) os << i <<    ; return os;} n template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;} n template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;} n template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second;        return is;} n template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first <<     << p.second; return os;} n template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);} n template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);} n template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;} n template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;} n  n #ifdef HOME n #define SHOW_COLORS n #include  C:/C++ libs/print.cpp  n #else n #define show(...)     42 n #define mclock        42 n #define shows         42 n #define debug if (false) n #endif n  n const int mod = 1e9 + 7; n  n int main() { n     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); n  n     int n; n     cin >> n; n     vector<int> a(n); n     vector<char> t(n); n     { n         int m; n         cin >> m; n         vector<int> p(m), k(m), b(m), w(m); n         for (int i = 0; i < m; ++i) { n             cin >> p[i] >> k[i] >> b[i] >> w[i]; n         } n         for (int i = 0; i < m; ++i) { n             ll seed = b[i]; n             ll base = w[i]; n             auto rn = [&]() { n                 ll ret = seed; n                 seed = (seed * base + 233) % mod; n                 return ret; n             }; n             for (int j = (i == 0 ? 0ll : p[i - 1]) + 1; j <= p[i]; ++j) { n                 t[j - 1] = (rn() % 2) + 1; n                 a[j - 1] = (rn() % k[i]) + 1; n             } n         } n     } n     if (t[0] == 2) { n         for (int i = 0; i < n; ++i) { n             t[i] = 3 - t[i]; n         } n     } n  n     show(a); n     show(t); n  n     auto calc = [&]() { n         vector<int> res(n, 0); n         ll s1 = 0, s2 = 0; n         for (int i = 0; i < n; ++i) { n             if (t[i] == 1) { n                 s1 += a[i]; n             } else { n                 s2 += a[i]; n             } n         } n         show(s1, s2); n         if (s1 == s2) { n             res = a; n             return res; n         } n         vector<ll> sms(n, 0); n         bool sw = false; n         int fr = 0; n         // show(a); n         // show(t); n         if (s1 < s2) { n             sw = true; n             for (int i = 0; i < n; ++i) { n                 if (t[i] == 2) { n                     fr = i; n                     break; n                 } n             } n             a[0]--; n             vector<char> tt; n             vector<int> aa; n             for (int i = 0; i < n; ++i) { n                 tt.pb(t[(i + fr) % n]); n                 aa.pb(a[(i + fr) % n]); n             } n             // show(fr); n             swap(t, tt); n             swap(a, aa); n             for (int i = 0; i < n; ++i) { n                 t[i] = 3 - t[i]; n             } n         } n  n         show(a); n         show(t); n  n         if (true) { n             vector<pair<int, ll>> v; n             for (int i = 0; i < n; ++i) { n                 if (v.empty() || t[v.back().first] != t[i]) n                     v.eb(i, 0); n                 v.back().second += a[i]; n             } n             // show(v); n             sms[0]++; n             v[0].second--; n             auto ad = [&](pair<int, ll> p) { n                 if (v.empty() || t[v.back().first] != t[p.first]) { n                     v.pb(p); n                     return; n                 } n                 v.back().second += p.second; n             }; n             if (v[0].second == 0) { n                 v.erase(v.begin()); n             } else { n                 auto x = v[0]; n                 // v.pb(v[0]); n                 v.erase(v.begin()); n                 ad(x); n             } n             show(v); n             while (v.size() > 1) { n                 show(v); n                 if (t[v[0].first] == 1) { n                     auto x = v[0]; n                     v.erase(v.begin()); n                     ad(x); n                 } n                 pair<int, ll> tmp = {-1, -1}; n                 if (t[v.back().first] == 2) { n                     tmp = v.back(); n                     v.pop_back(); n                 } n                 if (v.size() < 2) { n                     show(v, tmp); n                     if (tmp.first != -1) n                         show(t[tmp.first]); n                     show(t[v[0].first]); n                     assert(false); n                 } n                 if (v[v.size() - 2].second == v[v.size() - 1].second) { n                     ll x = v[v.size() - 2].second; n                     sms[v[v.size() - 1].first] += x; n                     sms[v[v.size() - 2].first] += x; n                     v.pop_back(); n                     v.pop_back(); n                 } else if (v[v.size() - 2].second < v[v.size() - 1].second) { n                     ll x = v[v.size() - 2].second; n                     sms[v[v.size() - 1].first] += x; n                     sms[v[v.size() - 2].first] += x; n                     auto u = v.back(); n                     v.pop_back(); n                     v.pop_back(); n                     u.second -= x; n                     ad(u); n                 } else { n                     ll x = v[v.size() - 1].second; n                     sms[v[v.size() - 1].first] += x; n                     sms[v[v.size() - 2].first] += x; n                     v.pop_back(); n                     v.back().second -= x; n                 } n                 if (tmp.first != -1) n                     ad(tmp); n             } n  n         } n  n         show(sms); n  n         if (sw) { n             vector<ll> smss; n             vector<char> tt; n             vector<int> aa; n             for (int i = 0; i < n; ++i) { n                 tt.pb(t[(i - fr + n) % n]); n                 aa.pb(a[(i - fr + n) % n]); n                 smss.pb(sms[(i - fr + n) % n]); n             } n             swap(t, tt); n             swap(a, aa); n             swap(sms, smss); n             for (int i = 0; i < n; ++i) { n                 t[i] = 3 - t[i]; n             } n             sms[0]++; n             a[0]++; n         } n  n         show(sms); n  n         vector<ll> have(3, 0); n         for (int i = 0; i < n * 2 + 5; ++i) { n             sms[i % n] += have[t[i % n]]; n             ll v = min(sms[i % n], (ll)a[i % n] - res[i % n]); n             res[i % n] += v; n             have[t[i % n]] = sms[i % n] - v; n             sms[i % n] = 0; n         } n  n         return res; n     }; n  n     auto r = calc(); n     show(r); n     ll res = 1; n     for (ll i = 1; i <= n; ++i) { n         ll ans = r[i - 1]; n         ans ^= (i * i); n         ans += 1; n         ans %= mod; n         res = res * ans % mod; n     } n     cout << res <<   n ; n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool chmax(T& a, const T& b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T& a, const T& b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n long long int ceil(long long int a, long long int b) { return (a + b - 1) / b; } n long long int digit(long long int a) { return (long long int)log10(a); } n const long long int MOD = 1e9 + 7, INF = 1e18; n long long int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1}, n               dy[8] = {0, 1, 0, -1, -1, -1, 1, 1}; n void YN(bool flag) { cout << (flag ?  YES  :  NO ) <<   n ; } n struct point { n   long long int r; n   long long int x, y; n }; n long long int N, K; n int main() { n   cin >> N >> K; n   vector<long long int> arr(N); n   for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { n     cin >> arr[i]; n   } n   sort((arr).begin(), (arr).end()); n   vector<pair<long long int, long long int> > vec; n   long long int curr = arr[0], cnt = 1; n   for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { n     if (arr[i + 1] != curr) { n       vec.push_back({curr, cnt}); n       curr = arr[i + 1]; n       cnt = 1; n     } else { n       cnt++; n     } n   } n   vec.push_back({curr, cnt}); n   N = ((long long int)(vec).size()); n   vector<long long int> sums_l(N); n   vector<long long int> sums_r(N); n   vector<long long int> sumn_l(N); n   vector<long long int> sumn_r(N); n   long long int sum = 0; n   for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { n     sum += vec[i].second; n     sums_l[i + 1] = sums_l[i] + (vec[i + 1].first - vec[i].first) * sum; n     sumn_l[i + 1] = sum; n   } n   sum = 0; n   for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { n     sum += vec[N - i - 1].second; n     sums_r[i + 1] = n         sums_r[i] + (vec[N - i - 1].first - vec[N - i - 2].first) * sum; n     sumn_r[i + 1] = sum; n   } n   long long int minv = INF; n   for (long long int i = (0), i_end_ = (((long long int)(vec).size())); n        i < i_end_; i++) { n     long long int rest = K - sums_l[i]; n     if (rest < 0) continue; n     long long int idx = n         upper_bound((sums_r).begin(), (sums_r).end(), rest) - sums_r.begin(); n     idx--; n     rest -= sums_r[idx]; n     long long int score = vec[N - idx - 1].first - vec[i].first; n     score -= rest / sumn_r[idx + 1]; n     chmin(minv, score); n   } n   for (long long int i = (0), i_end_ = (((long long int)(vec).size())); n        i < i_end_; i++) { n     long long int rest = K - sums_r[i]; n     if (rest < 0) continue; n     long long int idx = n         upper_bound((sums_l).begin(), (sums_l).end(), rest) - sums_l.begin(); n     idx--; n     rest -= sums_l[idx]; n     long long int score = vec[N - i - 1].first - vec[idx].first; n     score -= rest / sumn_l[idx + 1]; n     chmin(minv, score); n   } n   cout << max(0ll, minv) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long b, n                  long long mod = (long long)(1e9 + 7)) { n   if (!b) return 1; n   a %= mod; n   return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int mxn = 1e5, mod = 1e9 + 9; n long long ans = 0; n int n; n int x[mxn], y[mxn]; n int support[mxn]; n bool inqueue[mxn]; n map<int, map<int, int> > ds; n priority_queue<int, vector<int>, greater<int> > pq; n priority_queue<int, vector<int>, less<int> > pq2; n int take(int i, int j) { n   if (!ds.count(i) || !ds[i].count(j)) return -1; n   return ds[i][j]; n } n void supportAdjust(int i) { n   if (i == -1) return; n   support[i] = 0; n   for (int d = -1; d <= 1; d++) n     if (take(x[i] + d, y[i] - 1) != -1) support[i]++; n   if (y[i] == 0) support[i] = 30000; n } n void evaluate(int i) { n   if (i == -1) return; n   bool b4 = inqueue[i]; n   inqueue[i] = false; n   for (int d = -1; d <= 1; d++) n     if (take(x[i] + d, y[i] + 1) != -1) n       inqueue[i] |= support[take(x[i] + d, y[i] + 1)] == 1; n   inqueue[i] ^= 1; n   if (!b4 && inqueue[i]) { n     pq.push(i); n     pq2.push(i); n   } n } n void takeout(int turn) { n   int i; n   if (turn % 2 == 0) { n     while (pq2.size() && !inqueue[pq2.top()]) pq2.pop(); n     if (!pq2.size()) return; n     i = pq2.top(); n     pq2.pop(); n   } else { n     while (pq.size() && !inqueue[pq.top()]) pq.pop(); n     if (!pq.size()) return; n     i = pq.top(); n     pq.pop(); n   } n   ans = ((ans * n) % mod + i) % mod; n   inqueue[i] = 0; n   ds[x[i]].erase(y[i]); n   for (int d = -1; d <= 1; d++) supportAdjust(take(x[i] + d, y[i] + 1)); n   for (int d = -2; d <= 2; d++) evaluate(take(x[i] + d, y[i])); n   for (int d = -1; d <= 1; d++) evaluate(take(x[i] + d, y[i] - 1)); n } n void solve() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) inqueue[i] = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &x[i]); n     scanf( %d , &y[i]); n     if (!ds.count(x[i])) ds[x[i]] = map<int, int>(); n     ds[x[i]][y[i]] = i; n   } n   for (int i = 0; i < n; i++) supportAdjust(i); n   for (int i = 0; i < n; i++) evaluate(i); n   for (int i = 0; i < n; i++) takeout(i); n   cout << ans % mod << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string number; n   cin >> number; n   int n = number.length(); n   int answer; n   vector<vector<int> > remainders = { n       {}, {1}, {1, 2, 4, 3}, {1, 3, 4, 2}, {1, 4}}; n   int last_digit = number[n - 1] -  0 ; n   if (n == 1) { n     answer = (remainders[1][last_digit % 1] + remainders[2][last_digit % 4] + n               remainders[3][last_digit % 4] + remainders[4][last_digit % 2]) % n              5; n   } else { n     int last_two_digits = (number[n - 1] -  0 ) + 10 * (number[n - 2] -  0 ); n     answer = (remainders[1][last_two_digits % 1] + n               remainders[2][last_two_digits % 4] + n               remainders[3][last_two_digits % 4] + n               remainders[4][last_two_digits % 2]) % n              5; n   } n   cout << answer << endl; n } n int main() { n   { n     ios::sync_with_stdio(false); n     cout << fixed << setprecision(10); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005, mod = 1000000007; n long long int a, b, c, d, e, f, g, h[N], arr[N]; n string s; n vector<long long int> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> a; n   long long int ans = 0, tek = 0, cift = 0; n   for (long long int i = 1; i <= a; i++) { n     cin >> arr[i]; n     ans += arr[i] / 2; n     if (i % 2 == 0) n       cift += arr[i] % 2; n     else n       tek += arr[i] % 2; n     if (tek and cift) tek--, cift--, ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int cnt = 1e8; n int main() { n   cin >> n >> m; n   cout << (n >= 27 ? m : m % (1 << n)); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 31; n int a[1000010]; n long long cnt1[N + 10]; n long long cnt2[N + 10]; n long long cnt3[N + 10]; n long long cnt4[N + 10]; n int b[1000010]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = (0); i < (n); ++i) scanf( %d , &a[i]); n   int c = 0; n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (N); ++j) n       if ((a[i] >> j) & 1) cnt1[j]++; n   } n   for (int i = (0); i < (m); ++i) { n     scanf( %d , &b[i]); n     cnt4[b[i]]++; n   } n   int res = 0; n   int l = 0, r = m; n   while (l <= r) { n     int mid = (l + r) / 2; n     int k = N - 1; n     int now = mid; n     for (int j = (0); j < (N); ++j) { n       cnt3[j] = ((now < cnt4[j]) ? (now) : (cnt4[j])); n       now -= cnt3[j]; n     } n     for (int j = (0); j < (N); ++j) cnt2[j] = cnt1[j]; n     bool f = true; n     for (int i = N - 1; i >= 0; --i) { n       cnt2[i] += cnt2[i + 1] + cnt2[i + 1]; n       if (cnt2[i] < cnt3[i]) { n         f = false; n         break; n       } else n         cnt2[i] -= cnt3[i]; n     } n     if (f) { n       res = mid; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int movei[] = {0, 0, 1, -1, 1, 1, -1, -1}; n int movej[] = {1, -1, 0, 0, 1, -1, 1, -1}; n const int MXN = 1e5 + 7; n int can(vector<int> &FRQ, int k) { n   int mx = 1; n   while (true) { n     bool makable = true; n     int total = 0; n     for (int x : FRQ) total += x / mx; n     if (total < k) makable = false; n     if (!makable) { n       mx--; n       break; n     } n     mx++; n   } n   return k * mx; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     vector<int> frq(26, 0); n     for (char c : s) frq[c -  a ]++; n     int ans = 1; n     for (int i = 1; i * i <= k; i++) { n       if (k % i == 0) ans = max({ans, can(frq, i), can(frq, k / i)}); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long arr[n], arr1[n], arr2[n]; n     long long F1 = INT_MIN, F2 = INT_MIN; n     for (int i = 0; i < n; ++i) { n       cin >> arr[i]; n       F1 = max(F1, arr[i]); n     } n     for (int i = 0; i < n; ++i) { n       arr1[i] = F1 - arr[i]; n       F2 = max(F2, arr1[i]); n     } n     for (int i = 0; i < n; ++i) { n       arr2[i] = F2 - arr1[i]; n     } n     if ((k - 1) & 1) { n       for (int i = 0; i < n; ++i) { n         cout << arr2[i] <<    ; n       } n     } else { n       for (int i = 0; i < n; ++i) { n         cout << arr1[i] <<    ; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n long long int gcd(long long int a, long long int b) { n   return a ? gcd(b % a, a) : b; n } n long long int power(long long int a, long long int b) { n   long long int ans = 1; n   long long int s = a; n   long long int t = b; n   while (t) { n     if (t % 2 == 1) ans *= s, ans %= 998244353; n     s *= s, s %= 998244353; n     t /= 2; n   } n   return ans; n } n long long int inv(long long int k) { return power(k, 998244353 - 2); } n long long int fact[110]; n long long int invFact[110]; n std::vector<long long int> prime; n int check[110]; n std::vector<int> V; n long long int func(long long int k) { n   V.clear(); n   for (int i = 0; i < prime.size(); i++) { n     if (k % prime[i] != 0) continue; n     int count = 0; n     while (k % prime[i] == 0) count++, k /= prime[i]; n     V.push_back(count); n   } n   int sum = 0; n   for (int i = 0; i < V.size(); i++) sum += V[i]; n   long long int ans = fact[sum]; n   for (int i = 0; i < V.size(); i++) ans *= invFact[V[i]], ans %= 998244353; n   return ans; n } n std::vector<long long int> P; n int main() { n   fact[0] = 1; n   for (int i = 1; i <= 100; i++) n     fact[i] = fact[i - 1] * i, fact[i] %= 998244353; n   for (int i = 0; i <= 100; i++) invFact[i] = inv(fact[i]); n   long long int a; n   int b; n   scanf( %lld%d , &a, &b); n   long long int a2 = a; n   for (long long int i = 2; i * i <= a; i++) { n     if (a2 % i == 0) { n       prime.push_back(i); n       while (a2 % i == 0) a2 /= i; n     } n   } n   if (a2 > 1) prime.push_back(a2); n   for (int i = 1; i <= b; i++) { n     long long int c, d; n     scanf( %lld%lld , &c, &d); n     long long int e = gcd(c, d); n     long long int s1 = func(c / e); n     long long int s2 = func(d / e); n     printf( %lld n , (s1 * s2) % 998244353); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cout << 2000 << endl; n   for (int i = 1; i <= 1000; ++i) cout << i <<     << 1 <<     << i <<   2 n ; n   for (int i = 1000; i >= 1; --i) cout << i <<     << 1 <<     << i <<   2 n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 10; n int n, a[N], k, dp[N]; n void bsearch() { n   long long l = -1, r = 2e9 + 1; n   while (l + 1 < r) { n     long long mid = (r + l) / 2; n     bool check = false; n     for (int i = 0; i < n; i++) { n       dp[i] = i; n       for (int j = 0; j < i; j++) { n         if (abs(a[i] - a[j]) > (i - j) * mid) continue; n         dp[i] = min(dp[i], dp[j] + i - j - 1); n       } n       if (dp[i] + n - i - 1 <= k) { n         check = true; n         break; n       } n     } n     if (check) { n       r = mid; n     } else { n       l = mid; n     } n   } n   cout << r << endl; n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   bsearch(); n } n