#include <bits/stdc++.h> n using namespace std; n int n, p[50], sz[50], v[50], id[50], ct, lg[(1 << 24)], bc[(1 << 24)]; n unsigned int f[(1 << 24)], av[50]; n char s[50][50]; n vector<int> vc[50]; n int fd(int x) { return p[x] == x ? x : p[x] = fd(p[x]); } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) p[i] = i; n   for (int i = 1; i <= n; i++) { n     scanf( %s , s[i] + 1); n     for (int j = 1; j <= n; j++) n       if (s[i][j] ==  A ) { n         int xx = fd(i), yy = fd(j); n         p[xx] = yy; n       } n   } n   for (int i = 1; i <= n; i++) { n     int xx = fd(i); n     sz[xx]++; n     vc[xx].push_back(i); n   } n   for (int i = 1; i <= n; i++) n     if (sz[i] > 1 && p[i] == i) { n       id[i] = ct++; n       for (auto x : vc[i]) n         for (auto y : vc[i]) n           if (s[x][y] ==  X ) { n             printf( -1 n ); n             return 0; n           } n     } n   for (int i = 0; i < ct; i++) bc[1 << i] = i; n   if (!ct) { n     printf( %d , n - 1); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (p[i] == i && sz[i] == 1) continue; n     for (int j = 1; j <= n; j++) { n       if (p[j] == j && sz[j] == 1) continue; n       if (s[i][j] ==  X ) { n         int xx = fd(i), yy = fd(j); n         v[id[xx]] |= (1 << id[yy]); n         v[id[yy]] |= (1 << id[xx]); n       } n     } n   } n   f[0] = 1; n   for (int i = 1; i < (1 << ct); i++) { n     int lb = i & -i; n     lg[i] = lg[i - lb] + 1; n     f[i] = f[i - lb] + f[(i - lb) ^ (i & v[bc[lb]])]; n   } n   for (int i = 0; i < (1 << ct); i++) f[i]--; n   for (int i = 0; i < (1 << ct); i++) { n     unsigned int r = 1; n     for (int j = 1; j <= ct; j++) { n       r *= f[i]; n       if ((ct - lg[i]) & 1) n         av[j] -= r; n       else n         av[j] += r; n     } n   } n   for (int j = 1; j <= ct; j++) n     if (av[j]) { n       printf( %d , j + n - 1); n       return 0; n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAX = 1123; n int ans[MAX]; n int main() { n   int n; n   cin >> n; n   memset(ans, INF, sizeof(ans)); n   for (int i = 0; i < 10; i++) { n     vector<int> data1, data2; n     for (int j = 1; j <= n; j++) { n       if (j & (1 << i)) n         data1.push_back(j); n       else n         data2.push_back(j); n     } n     if (data1.size()) { n       cout << data1.size() << endl; n       for (int j = 0; j < data1.size(); j++) { n         cout << data1[j] <<    ; n       } n       for (int j = 1; j <= n; j++) { n         int t; n         cin >> t; n         if ((j & (1 << i)) == 0) { n           ans[j] = min(ans[j], t); n         } n       } n       cout << endl; n       fflush(stdout); n     } n     if (data2.size()) { n       cout << data2.size() << endl; n       for (int j = 0; j < data2.size(); j++) { n         cout << data2[j] <<    ; n       } n       for (int j = 1; j <= n; j++) { n         int t; n         cin >> t; n         if (j & (1 << i)) ans[j] = min(ans[j], t); n       } n       cout << endl; n       fflush(stdout); n     } n   } n   fflush(stdout); n   cout << -1 << endl; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int r, c, i, j, flag = 0; n   cin >> r >> c; n   char a[r + 2][c + 2]; n   for (i = 0; i < (r + 2); i++) { n     for (j = 0; j < (c + 2); j++) a[i][j] =  . ; n   } n   for (i = 1; i < (r + 1); i++) { n     for (j = 1; j < (c + 1); j++) { n       cin >> a[i][j]; n     } n   } n   for (i = 1; i < (r + 1); i++) { n     for (j = 1; j < (c + 1); j++) { n       if (a[i][j] ==  S ) { n         if (a[i + 1][j] ==  W  || a[i][j + 1] ==  W  || a[i - 1][j] ==  W  || n             a[i][j - 1] ==  W ) { n           flag = 1; n           cout <<  No n ; n           break; n         } n       } n     } n     if (flag == 1) break; n   } n   if (flag == 0) { n     cout <<  Yes n ; n     for (i = 1; i < (r + 1); i++) { n       for (j = 1; j < (c + 1); j++) { n         if (a[i][j] ==  . ) n           cout <<  D ; n         else n           cout << a[i][j]; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n vector<int> adj[100005]; n int din[100005]; n queue<int> que; n int Vec[100005], V_size; n int a, b, c, d, cur; n bool succ; n int main() { n   int Test; n   scanf( %d , &Test); n   while (Test--) { n     scanf( %d%d%d , &n, &m, &k); n     for (int i = 1; i <= n; i += 1) { n       adj[i].clear(); n       din[i] = 0; n     } n     while (!que.empty()) que.pop(); n     for (int i = 1; i <= m; i += 1) { n       scanf( %d%d , &a, &b); n       adj[a].push_back(b), adj[b].push_back(a); n       din[a]++, din[b]++; n     } n     if (k == 1) { n       puts( 2 ); n       puts( 1 ); n       continue; n     } n     if (k > ((int)sqrt((double)2 * m) + 2)) { n       puts( -1 ); n       continue; n     } n     for (int i = 1; i <= n; i += 1) sort(adj[i].begin(), adj[i].end()); n     for (int i = 1; i <= n; i += 1) n       if (din[i] < (k - 1)) que.push(i); n     while (!que.empty()) { n       cur = que.front(); n       que.pop(); n       for (auto& i : adj[cur]) { n         if (din[i] <= 0) continue; n         din[cur]--, din[i]--; n         if (din[i] == (k - 2)) que.push(i); n       } n       if (din[cur] != 0) { n         cout <<  fds ; n         return 0; n       } n     } n     for (int i = 1; i <= n; i += 1) n       if (din[i] == (k - 1)) que.push(i); n     bool nex_loop = false; n     while (!que.empty()) { n       cur = que.front(); n       que.pop(); n       if (din[cur] == (k - 1)) { n         V_size = 0; n         for (auto& i : adj[cur]) { n           if (din[i] <= 0) continue; n           Vec[V_size++] = i; n         } n         succ = true; n         for (int i = 0; i <= V_size - 1; i += 1) { n           for (int j = i + 1; j <= V_size - 1; j += 1) { n             a = Vec[i], b = Vec[j]; n             auto it = lower_bound(adj[a].begin(), adj[a].end(), b); n             if ((it == adj[a].end()) || (*it != b)) { n               succ = false; n               break; n             } n           } n           if (!succ) break; n         } n         if (succ) { n           puts( 2 ); n           printf( %d  , cur); n           for (int i = 0; i <= V_size - 1; i += 1) printf( %d  , Vec[i]); n           printf(  n ); n           nex_loop = true; n           break; n         } n       } n       for (auto& i : adj[cur]) { n         if (din[i] <= 0) continue; n         din[cur]--, din[i]--; n         if (din[i] == (k - 1)) que.push(i); n       } n     } n     if (nex_loop) continue; n     V_size = 0; n     for (int i = 1; i <= n; i += 1) { n       if (din[i] <= 0) continue; n       Vec[V_size++] = i; n     } n     if (V_size == 0) n       puts( -1 ); n     else { n       printf( 1 %d n , V_size); n       for (int i = 0; i <= V_size - 1; i += 1) { n         printf( %d  , Vec[i]); n       } n       printf(  n ); n     } 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   cout.tie(NULL); n   int t; n   cin >> t; n   int n, num, ctr; n   string s; n   while (t--) { n     cin >> n >> s; n     num = 0; n     ctr = 0; n     for (char ch : s) { n       if (ch ==  ) ) { n         num--; n         if (num < 0) ctr++, num++; n       } else n         num++; n     } n     cout << ctr <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     int n, m, i, x = 0, y, z; n     cin >> n; n     string s[n + 1]; n     for (int i = 0; i < n; i++) cin >> s[i]; n     vector<pair<int, int>> v; n     if (s[0][1] == s[1][0]) { n       if (s[n - 1][n - 2] == s[0][1]) { n         v.push_back({n, n - 1}); n       } n       if (s[n - 2][n - 1] == s[0][1]) { n         v.push_back({n - 1, n}); n       } n     } else if (s[n - 1][n - 2] == s[n - 2][n - 1]) { n       if (s[n - 1][n - 2] == s[0][1]) { n         v.push_back({1, 2}); n       } n       if (s[n - 2][n - 1] == s[1][0]) { n         v.push_back({2, 1}); n       } n     } else { n       if (s[0][1] !=  0 ) { n         v.push_back({1, 2}); n       } n       if (s[1][0] !=  0 ) { n         v.push_back({2, 1}); n       } n       if (s[n - 2][n - 1] !=  1 ) { n         v.push_back({n - 1, n}); n       } n       if (s[n - 1][n - 2] !=  1 ) { n         v.push_back({n, n - 1}); n       } n     } n     cout << v.size() << endl; n     for (int i = 0; i < v.size(); i++) { n       cout << v[i].first <<     << v[i].second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007LL; n const long long MXSZ = 1000000LL; n long long fpow(long long b, long long e) { n   long long ret = 1; n   while (e > 0) { n     if (e & 1) (ret *= b) %= mod; n     (b *= b) %= mod; n     e >>= 1; n   } n   return ret; n } n long long fac[MXSZ + 5]; n void prec() { n   fac[0] = 1; n   for (long long i = 1; i <= MXSZ; i++) { n     fac[i] = fac[i - 1] * i; n     fac[i] %= mod; n   } n } n long long inv(long long u) { return fpow(fac[u], mod - 2); } n inline long long comb(long long n, long long k) { n   long long ret = fac[n]; n   (ret *= inv(k)) %= mod; n   (ret *= inv(n - k)) %= mod; n   return ret; n } n inline long long perm(long long n, long long k) { n   long long ret = fac[n]; n   (ret *= inv(n - k)) %= mod; n   return ret; n } n long long n, m, a, b; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   prec(); n   cin >> n >> m >> a >> b; n   long long tot = 0LL; n   for (int btwn = 0; btwn < n - 2; btwn++) { n     if (m < btwn + 1) break; n     long long vleft = n - btwn - 2; n     long long k = btwn + 2; n     long long curr = comb(m - 1, btwn) * perm(n - 2, btwn); n     curr %= mod; n     curr *= k; n     curr %= mod; n     curr *= fpow(n, n - k - 1); n     curr %= mod; n     curr *= fpow(m, vleft); n     curr %= mod; n     tot += curr; n     tot %= mod; n   } n   if (m - 1 >= n - 2) { n     long long curr = comb(m - 1, n - 2) * perm(n - 2, n - 2); n     curr %= mod; n     tot += curr; n     tot %= mod; n   } n   cout << tot <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dist[200001] = {0}; n int n, ans[200001], Val[200001], lg, tree[200001][20]; n vector<vector<pair<int, long long> > > G(200001); n void Build(int v, int par = 0) { n   tree[v][0] = par; n   for (int i = 1; i <= lg; i++) tree[v][i] = tree[tree[v][i - 1]][i - 1]; n   for (int i = 0; i < G[v].size(); i++) { n     dist[G[v][i].first] = dist[v] + G[v][i].second; n     Build(G[v][i].first, v); n   } n } n int FinalDfs(int v) { n   for (int i = 0; i < G[v].size(); i++) ans[v] += FinalDfs(G[v][i].first) + 1; n   return ans[v]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &Val[i]); n   while ((1 << lg) <= n) lg++; n   lg--; n   for (int i = 2; i <= n; i++) { n     long long dis; n     int par; n     scanf( %d%I64d , &par, &dis); n     G[par].push_back(make_pair(i, dis)); n   } n   Build(1); n   for (int i = 1; i <= n; i++) { n     if (dist[i] <= Val[i]) continue; n     int Pos = i; n     int Value = Val[i]; n     for (int j = lg; j >= 0; j--) n       if (dist[Pos] - dist[tree[Pos][j]] <= Value && tree[Pos][j] != 0) { n         Value -= dist[Pos] - dist[tree[Pos][j]]; n         Pos = tree[Pos][j]; n       } n     ans[tree[Pos][0]]--; n   } n   FinalDfs(1); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string A, B; n int used[10][2]; n int memo[10][2]; n int m; n int dp(int pos, int mayor) { n   if (pos == m) return mayor; n   if (used[pos][mayor]) return memo[pos][mayor]; n   used[pos][mayor] = 1; n   int &dev = memo[pos][mayor] = 0; n   if (B[pos] ==  ? ) { n     if (mayor) { n       if (pos == 0) { n         dev = dp(pos + 1, 1); n       } else { n         dev = dp(pos + 1, 1); n       } n     } else { n       for (int c = A[pos]; c <=  9 ; ++c) { n         if (pos == 0 && c ==  0 ) continue; n         dev |= dp(pos + 1, c > A[pos]); n       } n     } n   } else { n     if (!mayor && B[pos] < A[pos]) n       return 0; n     else n       dev = dp(pos + 1, mayor ? 1 : (B[pos] > A[pos])); n   } n   return dev; n } n string rec(int pos, int mayor) { n   if (pos == m) return   ; n   if (B[pos] ==  ? ) { n     if (mayor) { n       if (pos == 0) { n         return string(1,  1 ) + rec(pos + 1, 1); n       } else { n         return string(1,  0 ) + rec(pos + 1, 1); n       } n     } else { n       for (int c = A[pos]; c <=  9 ; ++c) { n         if (pos == 0 && c ==  0 ) continue; n         if (dp(pos + 1, c > A[pos])) n           return string(1, c) + rec(pos + 1, c > A[pos]); n       } n     } n   } else { n     return string(1, B[pos]) + rec(pos + 1, mayor ? 1 : (B[pos] > A[pos])); n   } n } n int toi(string &s) { n   istringstream in(s); n   int x; n   in >> x; n   return x; n } n string tos(int x) { n   ostringstream out; n   out << x; n   return out.str(); n } n string S[100000 + 5]; n void fix(string &s) { n   for (int i = 0; i < ((int)(s).size()); ++i) { n     if (s[i] ==  ? ) { n       if (i == 0) n         s[i] =  1 ; n       else n         s[i] =  0 ; n     } n   } n } n bool f(string &s, string &t) { n   if (((int)(s).size()) > ((int)(t).size())) return 0; n   for (int i = 0; i < ((int)(s).size()); ++i) n     if (s[i] ==  ? ) return 0; n   if (((int)(s).size()) < ((int)(t).size())) { n     fix(t); n     return 1; n   } n   memset(used, 0, sizeof(used)); n   m = ((int)(s).size()); n   A = s, B = t; n   if (!dp(0, 0)) return 0; n   string C = rec(0, 0); n   t = C; n   return 1; n } n int main() { n   ios_base ::sync_with_stdio(0); n   int n; n   while (cin >> n) { n     S[0] =   ; n     bool possible = 1; n     for (int i = 0; i < n; ++i) { n       string s; n       cin >> s; n       bool ok = f(S[i], s); n       if (!ok) possible = 0; n       S[i + 1] = s; n     } n     if (!possible) n       cout <<  NO  <<   n ; n     else { n       cout <<  YES  <<   n ; n       for (int i = 0; i < n; ++i) cout << S[i + 1] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct ufds { n   int n; n   vector<int> p, s; n   ufds(int n = 0) : n(n), p(n), s(n, 1) { n     for (int i = 0; i < n; i++) p[i] = i; n   } n   int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } n   bool inSameSet(int i, int j) { return findSet(i) == findSet(j); } n   int getSizeOfSet(int i) { return s[findSet(i)]; } n   void unionSet(int i, int j) { n     i = findSet(i); n     j = findSet(j); n     if (i != j) { n       if (s[i] < s[j]) swap(i, j); n       s[i] += s[j]; n       p[j] = i; n     } n   } n }; n struct maximum_matching { n   int n, mm = 0, t = 0; n   vector<int> m, l, p, o, v; n   vector<vector<int>> &e; n   queue<int> q; n   maximum_matching(vector<vector<int>> &e) n       : n(e.size()), m(n, -1), l(n), p(n), o(n), v(n), e(e) { n     for (int i = 0; i < n; i++) n       if (m[i] == -1) mm += bfs(i); n   } n   int lca(int i, int j) { n     for (t++;; swap(i, j)) n       if (i != -1) { n         if (v[i] == t) return i; n         v[i] = t; n         i = m[i] == -1 ? -1 : o[p[m[i]]]; n       } n   } n   void blossom(int i, int j, int a) { n     while (o[i] != a) { n       p[i] = j; n       j = m[i]; n       if (l[j] == 1) l[j] = 0, q.push(j); n       o[i] = o[j] = a; n       i = p[j]; n     } n   } n   bool bfs(int s) { n     for (int i = 0; i < n; i++) l[i] = -1, o[i] = i; n     while (!q.empty()) q.pop(); n     l[s] = 0; n     q.push(s); n     while (!q.empty()) { n       int i = q.front(); n       q.pop(); n       for (int j : e[i]) n         if (l[j] == -1) { n           l[j] = 1; n           p[j] = i; n           if (m[j] == -1) { n             while (j != -1) m[j] = p[j], swap(j, m[m[j]]); n             return true; n           } n           l[m[j]] = 0; n           q.push(m[j]); n         } else if (l[j] == 0 && o[i] != o[j]) { n           int a = lca(o[i], o[j]); n           blossom(i, j, a); n           blossom(j, i, a); n         } n     } n     return false; n   } n }; n int n, a[300100], r[300100], value, m, rng[300100][3], c = 1, comp[700]; n bool u[300100], rngu[300100]; n vector<int> er[2], te[700]; n void fill() { n   int nv = 1; n   for (int i = 0; i < n - 1; i++) n     if (r[i] == 0 && r[i + 1] == 0) { n       while (u[nv]) nv++; n       r[i] = r[i + 1] = nv, value++, u[nv] = true; n     } n   for (int i = 0; i < n; i++) n     if (r[i] == 0) r[i] = 1; n } n void fillOE(int k, int av) { n   int i = rng[k][0], j = rng[k][1]; n   if (av == a[i - 1]) n     r[i] = av; n   else n     r[j] = av; n   value++, u[av] = true; n } n void fillE(int k) { n   int i = rng[k][0], j = rng[k][1]; n   r[i] = r[i - 1], r[j] = r[j + 1]; n   u[a[i - 1]] = u[a[j + 1]] = true; n   value++; n } n void dfsTree(int i, int p = -1) { n   for (int k : te[i]) { n     int j = i ^ rng[k][2]; n     if (j != p) { n       fillOE(k, j); n       dfsTree(j, i); n     } n   } n } n void dfsComp(int i) { n   comp[i] = c; n   for (int k : te[i]) { n     int j = i ^ rng[k][2]; n     if (comp[j] == 0) dfsComp(j); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(12); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i], r[i] = a[i]; n   int s = 0; n   while (s < n && a[s] == 0) s++; n   if (s < n && s % 2 == 1) a[s - 1] = r[s - 1] = a[s], s--; n   int e = n - 1; n   while (e >= 0 && a[e] == 0) e--; n   if (e >= 0 && (n - e - 1) % 2 == 1) a[e + 1] = r[e + 1] = a[e], e++; n   if (s < e) { n     for (int i = s + 1; i < e; i++) n       if (a[i] == 0) { n         int j = i; n         while (a[j + 1] == 0) j++; n         rng[m][0] = i, rng[m][1] = j; n         rng[m][2] = a[i - 1] ^ a[j + 1]; n         er[(j - i + 1) & 1].push_back(m); n         m++; n         i = j; n       } n     ufds uf(700); n     for (int k : er[1]) { n       int i = rng[k][0], j = rng[k][1]; n       if (!uf.inSameSet(a[i - 1], a[j + 1])) { n         uf.unionSet(a[i - 1], a[j + 1]); n         te[a[i - 1]].push_back(k); n         te[a[j + 1]].push_back(k); n         rngu[k] = true; n       } n     } n     for (int k : er[1]) { n       int i = rng[k][0], j = rng[k][1]; n       if (!rngu[k] && uf.inSameSet(a[i - 1], a[j + 1]) && !u[a[i - 1]]) { n         fillOE(k, a[i - 1]); n         dfsTree(a[i - 1]); n       } n     } n     for (int i = s; i < e; i++) n       if (a[i] > 0 && a[i] == a[i + 1] && !u[a[i]]) n         value++, u[a[i]] = true, dfsTree(a[i]); n     for (int i = 1; i <= min(n, 600); i++) n       if (!u[i] && comp[i] == 0) dfsComp(i), c++; n     vector<vector<int>> e(c); n     for (int k : er[0]) { n       int i = rng[k][0], j = rng[k][1]; n       if (!u[a[i - 1]] && !u[a[j + 1]] && comp[a[i - 1]] != comp[a[j + 1]]) { n         e[comp[a[i - 1]]].push_back(comp[a[j + 1]]), n             e[comp[a[j + 1]]].push_back(comp[a[i - 1]]); n       } n     } n     maximum_matching mm(e); n     for (int k : er[0]) { n       int i = rng[k][0], j = rng[k][1]; n       if (mm.m[comp[a[i - 1]]] != -1 && n           mm.m[comp[a[i - 1]]] == comp[a[j + 1]]) { n         fillE(k); n         dfsTree(a[i - 1]); n         dfsTree(a[j + 1]); n         mm.m[comp[a[i - 1]]] = -1, mm.m[comp[a[j + 1]]] = -1; n       } n     } n     for (int i = 1; i <= min(n, 600); i++) n       if (!u[i]) dfsTree(i); n   } n   fill(); n   for (int i = 0; i < n; i++) cout << r[i] << (i < n - 1 ?     :   n ); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename tp> n inline void read(tp& x) { n   x = 0; n   char tmp; n   bool key = 0; n   for (tmp = getchar(); !isdigit(tmp); tmp = getchar()) key = (tmp ==  - ); n   for (; isdigit(tmp); tmp = getchar()) x = (x << 3) + (x << 1) + (tmp ^  0 ); n   if (key) x = -x; n } n template <typename tp> n inline void ckmn(tp& x, tp y) { n   x = x < y ? x : y; n } n template <typename tp> n inline void ckmx(tp& x, tp y) { n   x = x < y ? y : x; n } n const long long LLINF = (long long)1e18; n const int N = 100010, MP = 20; n int n, q, sta[N]; n long long rec[MP], val[N]; n struct node { n   long long mn; n   long long rv; n   long long tag; n   int rec; n } t[N << 2]; n void puttag(int x, long long v) { n   t[x].tag += v; n   t[x].mn -= v; n   t[x].rv += v; n } n void putrec(int x) { n   t[x].rec = 1; n   t[x].rv = t[x].mn = LLINF; n   t[x].tag = 0; n } n void push_down(int x) { n   if (t[x].tag) { n     puttag(x << 1, t[x].tag); n     puttag(x << 1 | 1, t[x].tag); n     t[x].tag = 0; n   } n   if (t[x].rec) { n     putrec(x << 1); n     putrec(x << 1 | 1); n     t[x].rec = 0; n   } n } n void push_up(int x) { n   t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn); n   t[x].rv = t[x << 1 | 1].rv; n   if (t[x].rv >= LLINF) t[x].rv = t[x << 1].rv; n } n long long query(int p, int x = 1, int lp = 1, int rp = n) { n   if (lp == rp) return t[x].rv; n   push_down(x); n   int mid = (lp + rp) >> 1; n   if (p <= mid) return query(p, x << 1, lp, mid); n   long long tmp = query(p, x << 1 | 1, mid + 1, rp); n   if (tmp >= LLINF) n     return t[x << 1].rv; n   else n     return tmp; n } n long long askmn(int l, int r, int x = 1, int lp = 1, int rp = n) { n   if (l > rp || lp > r) return LLINF; n   if (lp >= l && rp <= r) return t[x].mn; n   int mid = (lp + rp) >> 1; n   push_down(x); n   return min(askmn(l, r, x << 1, lp, mid), n              askmn(l, r, x << 1 | 1, mid + 1, rp)); n } n void modify(int l, int r, long long v, int x = 1, int lp = 1, int rp = n) { n   if (l > rp || lp > r) return; n   if (lp >= l && rp <= r) { n     puttag(x, v); n     return; n   } n   int mid = (lp + rp) >> 1; n   push_down(x); n   modify(l, r, v, x << 1, lp, mid), modify(l, r, v, x << 1 | 1, mid + 1, rp); n   push_up(x); n } n void doit(int x = 1, int lp = 1, int rp = n) { n   if (t[x].mn >= 0) return; n   if (lp == rp) { n     while (t[x].mn < 0) { n       ++sta[lp]; n       t[x].mn = rec[sta[lp]] - t[x].rv; n     } n     return; n   } n   int mid = (lp + rp) >> 1; n   push_down(x); n   doit(x << 1, lp, mid); n   doit(x << 1 | 1, mid + 1, rp); n   push_up(x); n } n void fill(int l, int r, int x = 1, int lp = 1, int rp = n) { n   if (l > rp || lp > r) return; n   if (lp >= l && rp <= r) { n     putrec(x); n     return; n   } n   int mid = (lp + rp) >> 1; n   push_down(x); n   fill(l, r, x << 1, lp, mid); n   fill(l, r, x << 1 | 1, mid + 1, rp); n   push_up(x); n } n void setval(int p, long long v, int x = 1, int lp = 1, int rp = n) { n   if (lp == rp) { n     t[x].rv = v; n     t[x].tag = 0, t[x].rec = 0; n     t[x].mn = rec[0] - v; n     sta[lp] = 0; n     return; n   } n   int mid = (lp + rp) >> 1; n   push_down(x); n   if (p <= mid) n     setval(p, v, x << 1, lp, mid); n   else n     setval(p, v, x << 1 | 1, mid + 1, rp); n   push_up(x); n } n void build(int x = 1, int lp = 1, int rp = n) { n   t[x].tag = t[x].rec = 0; n   if (lp == rp) { n     t[x].rv = val[lp]; n     t[x].mn = rec[sta[lp]] - val[lp]; n     return; n   } n   int mid = (lp + rp) >> 1; n   build(x << 1, lp, mid); n   build(x << 1 | 1, mid + 1, rp); n   push_up(x); n } n int main() { n   int tp, x, y, z; n   read(n), read(q); n   for (int i = (1); i <= (n); ++i) read(val[i]); n   rec[0] = 1; n   for (int i = 1; rec[i - 1] <= LLINF / 42; ++i) rec[i] = rec[i - 1] * 42; n   build(); n   for (int i = (1); i <= (q); ++i) { n     read(tp); n     if (tp == 1) { n       read(x); n       printf( %lld n , query(x)); n     } else if (tp == 2) { n       read(x), read(y), read(z); n       if (y < n) setval(y + 1, query(y + 1)); n       setval(x, z); n       if (x < y) fill(x + 1, y); n     } else { n       read(x), read(y), read(z); n       setval(x, query(x)); n       if (y < n) setval(y + 1, query(y + 1)); n       while (true) { n         modify(x, y, z); n         doit(); n         if (askmn(x, y) > 0) break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 3; n struct tree { n   long long sum; n   int tag; n   int lc; n   int rc; n } a[N << 1]; n long long s[N]; n int n, m, t = 1; n long long ans; n long long win; n vector<pair<long long, long long> > other, change; n inline int read() { n   int v = 0, c = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) c = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     v = v * 10 + ch - 48; n     ch = getchar(); n   } n   return v * c; n } n inline void pushup(int u) { a[u].sum = a[a[u].lc].sum + a[a[u].rc].sum; } n inline void pushdown(int u, int l, int r) { n   if (a[u].tag) { n     int mid = (l + r) >> 1; n     a[a[u].lc].sum = mid - l + 1 - a[a[u].lc].sum; n     a[a[u].lc].tag ^= 1; n     a[a[u].rc].sum = r - mid - a[a[u].rc].sum; n     a[a[u].rc].tag ^= 1; n   } n   a[u].tag = 0; n } n void build(int u, int l, int r) { n   if (l == r) return; n   int mid = (l + r) >> 1; n   a[u].lc = ++t; n   build(a[u].lc, l, mid); n   a[u].rc = ++t; n   build(a[u].rc, mid + 1, r); n   pushup(u); n } n long long query(int u, int l, int r, int ll, int rr, int w) { n   if (l == ll && r == rr) { n     if (w == 1) return a[u].sum; n     return r - l + 1 - a[u].sum; n   } n   pushdown(u, l, r); n   int mid = (l + r) >> 1; n   if (rr <= mid) n     return query(a[u].lc, l, mid, ll, rr, w); n   else if (ll > mid) n     return query(a[u].rc, mid + 1, r, ll, rr, w); n   else n     return query(a[u].lc, l, mid, ll, mid, w) + n            query(a[u].rc, mid + 1, r, mid + 1, rr, w); n } n void updata(int u, int l, int r, int ll, int rr) { n   if (l == ll && r == rr) { n     a[u].sum = r - l + 1 - a[u].sum; n     a[u].tag ^= 1; n     return; n   } n   pushdown(u, l, r); n   int mid = (l + r) >> 1; n   if (rr <= mid) n     updata(a[u].lc, l, mid, ll, rr); n   else if (ll > mid) n     updata(a[u].rc, mid + 1, r, ll, rr); n   else { n     updata(a[u].lc, l, mid, ll, mid); n     updata(a[u].rc, mid + 1, r, mid + 1, rr); n   } n   pushup(u); n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= n; ++i) s[i] = (long long)read(); n   sort(s + 1, s + n + 1); n   for (int i = 1; i <= m; ++i) { n     int l = read(), r = read(); n     l = lower_bound(s + 1, s + n + 1, l) - s; n     r = upper_bound(s + 1, s + n + 1, r) - s - 1; n     if (l > r) continue; n     change.push_back(make_pair(l, r)); n     other.push_back(make_pair(r, l)); n   } n   build(1, 1, n); n   sort(change.begin(), change.end()); n   sort(other.begin(), other.end()); n   ans = (long long)(n - 2) * (n - 1) * n / 6; n   for (int i = 1, r = 0, l = 0; i <= n; ++i) { n     while (l < change.size() && change[l].first == i) { n       updata(1, 1, n, change[l].first, change[l].second); n       l++; n     } n     win = 0; n     if (i != 1) win += query(1, 1, n, 1, i - 1, 0); n     if (i != n) win += query(1, 1, n, i + 1, n, 1); n     ans -= win * (win - 1) / 2; n     while (r < other.size() && other[r].first == i) { n       updata(1, 1, n, other[r].second, other[r].first); n       r++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n const double PI = acos(-1); n template <class A, class B> n std::ostream& operator<<(std::ostream& st, const std::pair<A, B> p) { n   st <<  (  << p.first <<  ,   << p.second <<  ) ; n   return st; n } n using namespace std; n const int mod = 998244353; n int add(int a, int b) { n   a += b; n   if (a >= mod) a -= mod; n   if (a < 0) a += mod; n   return a; n } n int mul(int a, int b) { return (a * (long long)b) % mod; } n const int MAX = 500 * 1000 + 10; n int n, K, m; n int L[MAX], R[MAX], X[MAX]; n int cnt[MAX]; n int val[MAX]; n int tail[MAX]; n int p2[MAX]; n int dp[MAX], sum[MAX]; n int prv[MAX], nxt[MAX]; n int solve(int b) { n   static std::vector<std::pair<int, int> > V; n   for (int i = 0; i < (n + 1); i++) cnt[i] = 0; n   V.clear(); n   for (int i = 0; i < (m); i++) { n     int x = (X[i] >> b) & 1; n     if (x) { n       cnt[L[i]]++; n       cnt[R[i] + 1]--; n     } else { n       V.emplace_back(L[i], R[i]); n     } n   } n   for (int i = 1; i <= n; i++) { n     cnt[i] += cnt[i - 1]; n     val[i] = (cnt[i] > 0) + val[i - 1]; n   } n   for (int i = 1, v = -1; i <= n; i++) { n     if (!cnt[i]) v = i; n     prv[i] = v; n   } n   for (int i = n, v = -1; i >= 1; i--) { n     if (!cnt[i]) v = i; n     nxt[i] = v; n   } n   for (int i = 1; i <= n; i++) tail[i] = -1; n   for (auto p : V) { n     int l = p.first, r = p.second; n     int ctr = val[r] - val[l - 1]; n     if (ctr == r - l + 1) return 0; n     l = nxt[l]; n     r = prv[r]; n     l = l - val[l]; n     r = r - val[r]; n     tail[r] = max(tail[r], l); n   } n   int m = n - val[n]; n   if (!m) return 1; n   dp[0] = 1; n   sum[0] = 1; n   int L = 0; n   for (int i = 1; i <= m; i++) { n     dp[i] = add(sum[i - 1], L ? -sum[L - 1] : 0); n     sum[i] = add(dp[i], sum[i - 1]); n     L = max(L, tail[i]); n   } n   int ret = add(sum[m], L ? -sum[L - 1] : 0); n   return ret; n } n int main() { n   p2[0] = 1; n   for (int i = 1; i < MAX; i++) p2[i] = mul(2, p2[i - 1]); n   scanf( %d %d %d , &n, &K, &m); n   for (int i = 0; i < (m); i++) scanf( %d %d %d , L + i, R + i, X + i); n   int ans = 1; n   for (int b = 0; b < (K); b++) { n     ans = mul(ans, solve(b)); n     if (!ans) break; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n using namespace std; n const long long MOD = 1000000009; n const int MAXM = 100010; n int m; n long long pow_mod[MAXM]; n int X[MAXM], Y[MAXM]; n map<pair<int, int>, int> ma; n set<int> S; n set<int> up[MAXM], down[MAXM]; n void init() { n   pow_mod[0] = 1; n   for (int i = 1; i < m; i++) { n     pow_mod[i] = (pow_mod[i - 1] * m) % MOD; n   } n } n bool ok(int k) { n   for (auto el : up[k]) { n     if (down[el].size() <= 1) return false; n   } n   return true; n } n int main() { n   cin >> m; n   init(); n   for (int i = 0; i < m; i++) { n     cin >> X[i] >> Y[i]; n     ma.insert(make_pair(pair<int, int>(X[i], Y[i]), i)); n   } n   for (auto el : ma) { n     int y = el.first.second; n     int x = el.first.first; n     for (int d = -1; d <= 1; d++) { n       pair<int, int> tmp = pair<int, int>(x + d, y + 1); n       if (ma.count(tmp)) { n         up[el.second].insert(ma[tmp]); n         down[ma[tmp]].insert(el.second); n       } n     } n   } n   for (int i = 0; i < m; i++) n     if (ok(i)) S.insert(i); n   long long ret = 0; n   for (int i = 0; i < m; i++) { n     int er; n     if (i % 2 == 0) { n       er = *S.rbegin(); n     } else { n       er = *S.begin(); n     } n     ret += pow_mod[m - i - 1] * (long long)er % MOD; n     ret %= MOD; n     S.erase(er); n     for (auto el : up[er]) { n       down[el].erase(er); n       if (ok(el)) n         S.insert(el); n       else n         S.erase(el); n       for (auto ell : down[el]) { n         if (ok(ell)) n           S.insert(ell); n         else n           S.erase(ell); n       } n     } n     for (auto el : down[er]) { n       up[el].erase(er); n       if (ok(el)) n         S.insert(el); n       else n         S.erase(el); n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5 * 10000; n const long long mod = 1000000007; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n), b(n), c(n), d(n), e(n); n   string str; n   for (int i = 0; i < n; i++) { n     cin >> str; n     int qtd[] = {0, 0, 0, 0, 0}; n     for (char c : str) { n       qtd[c -  a ]++; n     } n     a[i] = 2 * qtd[0] - str.length(); n     b[i] = 2 * qtd[1] - str.length(); n     c[i] = 2 * qtd[2] - str.length(); n     d[i] = 2 * qtd[3] - str.length(); n     e[i] = 2 * qtd[4] - str.length(); n   } n   sort(a.rbegin(), a.rend()); n   sort(b.rbegin(), b.rend()); n   sort(c.rbegin(), c.rend()); n   sort(d.rbegin(), d.rend()); n   sort(e.rbegin(), e.rend()); n   int ans = 0; n   int sum = 0, i = 0; n   while (i < n && sum + a[i] > 0) { n     sum += a[i]; n     i++; n   } n   ans = max(ans, i); n   sum = 0, i = 0; n   while (i < n && sum + b[i] > 0) { n     sum += b[i]; n     i++; n   } n   ans = max(ans, i); n   sum = 0, i = 0; n   while (i < n && sum + c[i] > 0) { n     sum += c[i]; n     i++; n   } n   ans = max(ans, i); n   sum = 0, i = 0; n   while (i < n && sum + d[i] > 0) { n     sum += d[i]; n     i++; n   } n   ans = max(ans, i); n   sum = 0, i = 0; n   while (i < n && sum + e[i] > 0) { n     sum += e[i]; n     i++; n   } n   ans = max(ans, i); n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt = 1; n   cin >> tt; n   while (tt--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using vi = vector<int>; n using ld = long double; n struct Node { n   vi to; n   int lk; n   ll w; n   Node() : to(14), lk(0), w(0) {} n }; n vector<Node> au(1); n int sz = 1; n void add(const string& s, int w) { n   int v = 0; n   for (char c : s) { n     c -=  a ; n     if (!au[v].to[c]) { n       au[v].to[c] = sz++; n       au.push_back(Node()); n     } n     v = au[v].to[c]; n   } n   au[v].w += w; n } n void build() { n   queue<int> q; n   q.push(0); n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     au[v].w += au[au[v].lk].w; n     for (char kek =  a ; kek <=  n ; kek++) { n       auto c = kek -  a ; n       if (au[v].to[c]) { n         int u = au[v].to[c]; n         au[u].lk = (v ? au[au[v].lk].to[c] : 0); n         q.push(u); n       } else { n         au[v].to[c] = au[au[v].lk].to[c]; n       } n     } n   } n } n ll dp[1 << 14][1001]; n ll dp_new[1 << 14][1001]; n const ll inf = 1e18 + 228; n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int k; n   cin >> k; n   for (int i = 0; i < k; i++) { n     string s; n     int w; n     cin >> s >> w; n     add(s, w); n   } n   build(); n   string s; n   cin >> s; n   string KEK = s; n   int n = (int)((s).size()); n   const ll inf = 1e18 + 228; n   vi bpc(1 << 14); n   for (int msk = 1; msk < (1 << 14); msk++) { n     bpc[msk] = 1 + bpc[msk & (msk - 1)]; n   } n   vi qps; n   ll ans_add = 0; n   int v = 0; n   int lst = -sz; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  ? ) n       qps.push_back(i), v = 0, lst = i; n     else n       v = au[v].to[s[i] -  a ]; n     if (i - lst >= sz) ans_add += au[v].w; n   } n   for (int i = 0; i < (1 << 14); i++) { n     for (int j = 0; j < sz; j++) { n       dp[i][j] = dp_new[i][j] = -inf; n     } n   } n   dp[0][0] = 0; n   int pi = (qps.empty() || qps[0] < sz ? 0 : qps[0] - sz); n   int yet = 0; n   vector<vi> msks(15); n   for (int m = 0; m < (1 << 14); m++) { n     msks[bpc[m]].push_back(m); n   } n   for (int i : qps) { n     pi = max(pi, i - sz); n     for (int m : msks[yet]) { n       for (int s = 0; s < sz; s++) { n         dp_new[m][s] = -inf; n       } n     } n     for (int s = 0; s < sz; s++) { n       int ss = s; n       for (int j = pi; j < i; j++) { n         ss = au[ss].to[KEK[j] -  a ]; n       } n       for (int m : msks[yet]) { n         if (dp[m][s] == -inf) continue; n         dp_new[m][ss] = max(dp_new[m][ss], dp[m][s]); n       } n     } n     memcpy(dp, dp_new, sizeof(dp)); n     for (int m : msks[yet]) { n       for (int b = 0; b < 14; b++) { n         if (m >> b & 1) continue; n         int mm = m | (1 << b); n         for (int s = 0; s < sz; s++) { n           if (dp[m][s] == -inf) continue; n           int ss = au[s].to[b]; n           dp[mm][ss] = max(dp[mm][ss], dp[m][s] + au[ss].w); n         } n       } n     } n     yet++; n     pi = i + 1; n     while (pi < n && pi - i < sz && s[pi] !=  ? ) pi++; n     for (int s = 0; s < sz; s++) { n       int ss = s; n       ll add = 0; n       for (int j = i + 1; j < pi; j++) { n         ss = au[ss].to[KEK[j] -  a ]; n         add += au[ss].w; n       } n       for (int m : msks[yet]) { n         if (dp[m][s] == -inf) continue; n         dp_new[m][ss] = max(dp_new[m][ss], dp[m][s] + add); n       } n     } n     memcpy(dp, dp_new, sizeof(dp)); n   } n   ll ans = -inf; n   for (int i = 0; i < (1 << 14); i++) { n     if (bpc[i] != (int)((qps).size())) continue; n     for (int j = 0; j < sz; j++) { n       if (ans < dp[i][j]) { n         ans = dp[i][j]; n       } n     } n   } n   cout << ans + ans_add; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char ch1, ch2, ch; n   int i, j, k, tc, res, total, temp, mx, num, num1, num2; n   int n, a, b; n   { n     scanf( %d %d %d , &n, &a, &b); n     if (b == 0) n       printf( %d n , a); n     else if (b > 0) { n       while (b--) { n         if (a == n) n           a = 1; n         else n           a++; n       } n       printf( %d n , a); n     } else { n       b = (-1 * b); n       while (b--) { n         if (a == 1) n           a = n; n         else n           a--; n       } n       printf( %d n , a); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, aa, bb; n   cin >> a >> b; n   a += 100; n   b += 100; n   cin >> aa >> bb; n   aa += 100; n   bb += 100; n   if (aa != a && bb != b) { n     int tmp = (abs(a - aa)); n     int tmp2 = (abs(b - bb)); n     cout << 2 * abs(tmp2 + tmp) + 4; n   } else n     cout << max(8, (6 + (abs(a - aa) + abs(b - bb)) * 2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n); n   for (auto &i : a) scanf( %d , &i); n   map<int, int> dp; n   int ans = 0; n   int last = 0; n   for (int i = 0; i < n; i++) { n     int x = a[i]; n     dp[x] = dp[x - 1] + 1; n     if (ans < dp[x]) { n       ans = dp[x]; n       last = x; n     } n   } n   vector<int> res; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] == last) { n       res.push_back(i); n       last--; n     } n   } n   reverse(res.begin(), res.end()); n   printf( %d n , ans); n   for (auto &i : res) printf( %d  , i + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int id = 0; n vector<int> group(2e5 + 5, -1); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int N; n   cin >> N; n   vector<int> A(N); n   for (int i = 0; i < N; i++) cin >> A[i]; n   string S; n   cin >> S; n   for (int i = 0; i < N; i++) { n     if (group[A[i]] != -1) continue; n     group[A[i]] = id; n     int j = i; n     while (j + 1 < N && S[j] ==  1 ) group[A[++j]] = id; n     id++; n   } n   for (int i = 0; i < N; i++) { n     if (A[i] == i + 1) continue; n     if (group[i + 1] != group[A[i]]) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200005]; n int alph[26]; n int main() { n   scanf( %s , &s); n   for (int i = 0; s[i]; i++) alph[s[i] -  a ]++; n   for (int i = 0; i < 26; i++) n     for (int j = 26 - 1; j > i; j--) n       if (alph[i] & 1) n         if (alph[j] & 1) alph[i]++, alph[j]--; n   int odd = -1; n   for (int i = 0; i < 26; i++) n     if (alph[i] & 1) odd = i; n   for (int i = 0; i < 26; i++) n     for (int j = 1; j <= alph[i] / 2; j++) printf( %c , i +  a ); n   if (odd != -1) printf( %c , odd +  a ); n   for (int i = 26 - 1; i >= 0; i--) n     for (int j = 1; j <= alph[i] / 2; j++) printf( %c , i +  a ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using db = double; n using str = string; n using pi = pair<int, int>; n using pl = pair<ll, ll>; n using pd = pair<db, db>; n using vi = vector<int>; n using vb = vector<bool>; n using vl = vector<ll>; n using vd = vector<db>; n using vs = vector<str>; n using vpi = vector<pi>; n using vpl = vector<pl>; n using vpd = vector<pd>; n template <class T> n using V = vector<T>; n template <class T, size_t SZ> n using AR = array<T, SZ>; n template <class T> n using PR = pair<T, T>; n const int MOD = 1e9 + 7; n const int mxN = 1e3 + 5; n const ll INF = 1e18; n const ld PI = acos((ld)-1); n const int tSZ = (1 << 21); n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); n constexpr int pcnt(int x) { return __builtin_popcount(x); } n constexpr int bits(int x) { return 31 - __builtin_clz(x); } n ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } n ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } n ll add(ll first, ll second) { n   first += second; n   if (first >= MOD) first %= MOD; n   if (first < 0) first += MOD; n   return first; n } n ll mul(ll first, ll second) { n   first *= second; n   if (first >= MOD) first %= MOD; n   return first; n } n ll pw(ll x, ll y) { n   ll ans = 1, ml = x; n   while (y) { n     if (y & 1) ans = mul(ans, ml); n     ml = mul(ml, ml); n     y >>= 1; n   } n   return ans; n } n ll inv(ll x) { return pw(x, MOD - 2); } n template <class T> n void remDup(vector<T>& x) { n   sort(begin(x), end(x)); n   x.erase(unique(begin(x), end(x)), end(x)); n } n template <class T, class U> n void erase(T& x, const U& v) { n   auto it = x.find(v); n   assert(it != end(x)); n   x.erase(v); n } n template <class T> n void re(complex<T>& c); n template <class T, class U> n void re(pair<T, U>& p); n template <class T> n void re(vector<T>& v); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(db& d) { n   str t; n   re(t); n   d = stod(t); n } n void re(ld& d) { n   str t; n   re(t); n   d = stold(t); n } n template <class T, class... U> n void re(T& t, U&... u) { n   re(t); n   re(u...); n } n template <class T> n void re(complex<T>& c) { n   T a, b; n   re(a, b); n   c = {a, b}; n } n template <class T, class U> n void re(pair<T, U>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& x) { n   for (auto& a : x) re(a); n } n template <class T, size_t SZ> n void re(array<T, SZ>& x) { n   for (auto& a : x) re(a); n } n template <class T> n void rv(int& n, vector<T>& x) { n   re(n); n   x.resize(n); n   for (auto& a : x) re(a); n } n str to_string(char c) { return str(1, c); } n str to_string(const char* second) { return (str)second; } n str to_string(str second) { return second; } n str to_string(bool b) { return to_string((int)b); } n template <class T> n str to_string(complex<T> c) { n   stringstream ss; n   ss << c; n   return ss.str(); n } n str to_string(vector<bool> v) { n   str res =  { ; n   for (int i = (0); i < ((int)(v).size()); i++) res += char( 0  + v[i]); n   res +=  } ; n   return res; n } n template <size_t SZ> n str to_string(bitset<SZ> b) { n   str res =   ; n   for (int i = (0); i < (SZ); i++) res += char( 0  + b[i]); n   return res; n } n template <class T, class U> n str to_string(pair<T, U> p); n template <class T> n str to_string(T v) { n   bool fst = 1; n   str res =   ; n   for (const auto& x : v) { n     if (!fst) res +=    ; n     fst = 0; n     res += to_string(x); n   } n   return res; n } n template <class T, class U> n str to_string(pair<T, U> p) { n   return to_string(p.first) +     + to_string(p.second); n } n template <class T> n void pr(T v) { n   cout << to_string(v); n } n template <class T, class... U> n void pr(const T& t, const U&... u) { n   pr(t); n   pr(u...); n } n void ps() { pr(  n ); } n template <class T, class... U> n void ps(const T& t, const U&... u) { n   pr(t); n   if (sizeof...(u)) pr(   ); n   ps(u...); n } n void setIn(str second) { freopen(second.c_str(),  r , stdin); } n void setOut(str second) { freopen(second.c_str(),  w , stdout); } n void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } n void setIO(str st =   ) { n   unsyncIO(); n   if ((int)(st).size()) { n     setIn(st +  .in ), setOut(st +  .out ); n   } n } n void DBG() { cerr <<  ]  << endl; } n template <class T, class... U> n void DBG(const T& t, const U&... u) { n   cerr << to_string(t); n   if (sizeof...(u)) cerr <<  ,  ; n   DBG(u...); n } n int n, k; n str matr[mxN * mxN]; n vector<str> vv; n int graph[30][30]; n int c_br[mxN * mxN]; n bool ok = 1; n void calc() { n   for (int i = (0); i < ((int)(vv).size() - 1); i++) { n     str tmp = vv[i]; n     str nxt = vv[i + 1]; n     int k = 0; n     int zz = min((int)(tmp).size(), (int)(nxt).size()); n     while (k < zz && tmp[k] == nxt[k]) { n       k++; n     } n     if (k < zz) { n       graph[tmp[k] -  a ][nxt[k] -  a ] = 1; n     } else if ((int)(tmp).size() > (int)(nxt).size()) { n       ok = 0; n     } n   } n } n void solve() { n   vi vis(27, 0); n   set<int> alphabet; n   for (int i = (0); i < (n * k); i++) { n     if ((int)(matr[i]).size() != 0) { n       vv.push_back(matr[i]); n       for (auto& x : matr[i]) alphabet.insert(x -  a ); n     } n   } n   for (int i = (0); i < ((int)(vv).size()); i++) { n     c_br[i] = (int)(vv[i]).size(); n   } n   calc(); n   vi ans; n   for (int i = (0); i < (27); i++) { n     for (int j = (0); j < (27); j++) { n       if (vis[j] || !alphabet.count(j)) continue; n       int cnt = 0; n       for (int z = (0); z < (27); z++) { n         cnt += graph[z][j]; n       } n       if (cnt == 0) { n         ans.push_back(j); n         vis[j] = 1; n         for (int z = (0); z < (27); z++) { n           graph[j][z] = 0; n         } n       } n     } n   } n   if (!ok || (int)(ans).size() != (int)(alphabet).size()) { n     ps( IMPOSSIBLE ); n     return; n   } n   for (auto& x : ans) { n     pr(char(x +  a )); n   } n   ps(); n } n int main() { n   setIO(); n   re(n, k); n   int c; n   for (int i = (0); i < (n); i++) { n     re(c); n     for (int j = (0); j < (k); j++) { n       re(matr[c * n + j]); n     } n   } n   solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n char tbl[209][209]; n int vid[209][209]; n int hid[209][209]; n int n, m; n vector<int> G[100009]; n int pr[100009]; n int was[100009]; n int wcnt; n bool vert(int x, int y) { return vid[x][y] > 0; } n bool improve(int x) { n   was[x] = wcnt; n   for (int nb : G[x]) { n     if (was[nb] < wcnt) { n       if (pr[nb] == nb) { n         pr[nb] = x; n         pr[x] = nb; n         return true; n       } n       was[nb] = wcnt; n       if (improve(pr[nb])) { n         pr[nb] = x; n         pr[x] = nb; n         return true; n       } n     } n   } n   return false; n } n int h2 = 0; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> tbl[i][j]; n       if (tbl[i][j] ==  # ) h2++; n     } n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j < m; j++) { n       if (tbl[i][j] ==  #  && tbl[i][j + 1] ==  # ) { n         cnt++; n         vid[i][j] = cnt; n       } n     } n   } n   int rows = cnt; n   for (int i = 1; i < n; i++) { n     for (int j = 1; j <= m; j++) { n       if (tbl[i][j] ==  #  && tbl[i + 1][j] ==  # ) { n         cnt++; n         hid[i][j] = cnt; n       } n     } n   } n   for (int i = 1; i < n; i++) { n     for (int j = 1; j <= m; j++) { n       if (tbl[i][j] ==  #  && tbl[i + 1][j] ==  # ) { n         if (vert(i, j - 1)) { n           G[hid[i][j]].push_back(vid[i][j - 1]); n           G[vid[i][j - 1]].push_back(hid[i][j]); n         } n         if (vert(i, j)) { n           G[hid[i][j]].push_back(vid[i][j]); n           G[vid[i][j]].push_back(hid[i][j]); n         } n         if (vert(i + 1, j - 1)) { n           G[hid[i][j]].push_back(vid[i + 1][j - 1]); n           G[vid[i + 1][j - 1]].push_back(hid[i][j]); n         } n         if (vert(i + 1, j)) { n           G[hid[i][j]].push_back(vid[i + 1][j]); n           G[vid[i + 1][j]].push_back(hid[i][j]); n         } n       } n     } n   } n   for (int i = 1; i <= cnt; i++) { n     pr[i] = i; n   } n   int ans = 0; n   for (int i = 1; i <= rows; i++) { n     if (pr[i] == i) { n       wcnt++; n       if (improve(i)) ans++; n     } n   } n   cout << h2 - (cnt - ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned int MOD = 1000000007; n void solve() { n   string s; n   cin >> s; n   string t; n   cin >> t; n   vector<char> v; n   bool check1 = false; n   bool check2 = false; n   bool check3 = false; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  a ) { n       check1 = true; n     } n     if (s[i] ==  b ) { n       check2 = true; n     } n     if (s[i] ==  c ) { n       check3 = true; n     } n   } n   if (t[0] ==  a  && t[1] ==  b  && t[2] ==  c  && check1 && check2 && check3) { n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==  a ) { n         v.push_back(s[j]); n       } n     } n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==  c ) { n         v.push_back(s[j]); n       } n     } n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==  b ) { n         v.push_back(s[j]); n       } n     } n   } else { n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==  a ) { n         v.push_back(s[j]); n       } n     } n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==  b ) { n         v.push_back(s[j]); n       } n     } n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==  c ) { n         v.push_back(s[j]); n       } n     } n   } n   sort(s.begin(), s.end()); n   for (int i = 0; i < s.length(); i++) { n     if (s[i] !=  a  && s[i] !=  b  && s[i] !=  c ) { n       v.push_back(s[i]); n     } n   } n   for (auto it : v) { n     cout << it; n   } n   cout << endl; n } n signed main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 220, maxm = 220 * 220; n struct edge { n   int s, t, next; n } e[maxm]; n struct vtx { n   int dfn, low, belong, leave; n   bool instack; n } ver[maxn]; n int hd[maxn], cnt, ind, sta[maxn], top, bcnt; n void insert(int a, int b) { n   e[cnt].next = hd[a]; n   hd[a] = cnt; n   e[cnt].t = b; n   e[cnt].s = a; n   cnt++; n } n vector<int> bcc[maxn]; n void tarjanSCC(int u) { n   ver[u].dfn = ver[u].low = ind++; n   ver[u].instack = true; n   sta[top++] = u; n   for (int i = hd[u]; i != -1; i = e[i].next) { n     int v = e[i].t; n     if (!ver[v].dfn) { n       tarjanSCC(v); n       ver[u].low = min(ver[u].low, ver[v].low); n     } n     if (ver[v].instack) ver[u].low = min(ver[u].low, ver[v].dfn); n   } n   if (ver[u].dfn == ver[u].low) { n     bcnt++; n     int j = -1; n     do { n       j = sta[--top]; n       ver[j].belong = bcnt; n       bcc[bcnt].push_back(j); n       ver[j].instack = false; n     } while (j != u); n   } n   ver[u].leave = ind; n } n void tarjanInit() { n   memset(hd, -1, sizeof hd); n   memset(ver, 0, sizeof ver); n   ind = top = 1; n   cnt = 0; n } n int ord[maxn]; n bool cmp(int i, int j) { return ver[i].leave < ver[j].leave; } n bool twoSat(int n, int *sel) { n   for (int i = 0; i < 2 * n; i++) bcc[i].clear(); n   for (int i = 0; i < 2 * n; i++) n     if (!ver[i].dfn) tarjanSCC(i); n   for (int i = 0; i < n; i++) n     if (ver[i * 2].belong == ver[i * 2 + 1].belong) return false; n   for (int i = 0; i < 2 * n; i++) ord[i] = i; n   sort(ord, ord + 2 * n, cmp); n   memset(sel, -1, n * sizeof(int)); n   for (int i = 0; i < 2 * n; i++) { n     if (sel[ord[i] >> 1] != -1) n       continue; n     else { n       int c = ver[ord[i]].belong; n       for (int j = 0; j < (int)bcc[c].size(); j++) n         sel[bcc[c][j] >> 1] = (bcc[c][j] & 1); n     } n   } n   return true; n } n bool intersect(int a1, int a2, int b1, int b2) { n   if (a1 > a2) swap(a1, a2); n   if (b1 > b2) swap(b1, b2); n   return a1 == b1 ? 0 n                   : (a1 < b1 ? (a2 - b1) * (a2 - b2) < 0 n                              : (b2 - a1) * (b2 - a2) < 0); n } n int ans[110]; n int main() { n   int n, m, p[110][2]; n   scanf( %d%d , &n, &m); n   tarjanInit(); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &p[i][0], &p[i][1]); n   } n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < i; j++) { n       if (intersect(p[i][0], p[i][1], p[j][0], p[j][1])) { n         insert(i * 2, j * 2 + 1); n         insert(i * 2 + 1, j * 2); n         insert(j * 2, i * 2 + 1); n         insert(j * 2 + 1, i * 2); n       } n     } n   } n   if (twoSat(m, ans)) { n     for (int i = 0; i < m; i++) { n       if (ans[i]) n         printf( i ); n       else n         printf( o ); n     } n   } else n     printf( Impossible ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using vi = vector<int>; n using ii = pair<int, int>; n using vii = vector<pair<int, int>>; n void solve() { n   int n; n   cin >> n; n   int r = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       int x; n       cin >> x; n       if (i == j) r += x; n     } n   } n   r %= 2; n   string res; n   int q; n   cin >> q; n   while (q--) { n     int t; n     cin >> t; n     if (t == 3) n       res += to_string(r); n     else if (t == 1 || t == 2) { n       int waste; n       cin >> waste; n       r ^= 1; n     } n   } n   cout << res <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int tc = 1; n   while (tc--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n void solve() { n   int n; n   cin >> n; n   vector<set<int>> v(n + 1); n   for (int i = 0; i < n - 2; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     v[x].insert(y); n     v[x].insert(z); n     v[y].insert(x); n     v[y].insert(z); n     v[z].insert(x); n     v[z].insert(y); n   } n   vector<int> res(n); n   for (int i = 1; i < n + 1; i++) { n     if (((int)(v[i]).size()) == 2) { n       res[0] = i; n       break; n     } n   } n   vector<bool> been(n + 1); n   int num1 = res[0], num2; n   int can1 = *v[num1].begin(), can2 = *v[num1].rbegin(); n   if (((int)(v[can1]).size()) == 3) n     num2 = can1; n   else n     num2 = can2; n   res[1] = num2; n   been[num1] = been[num2] = 1; n   for (int i = 2; i < n - 2; i++) { n     vector<int> can; n     for (auto& j : v[num1]) { n       if (been[j]) continue; n       can.push_back(j); n     } n     for (auto& j : v[num2]) { n       if (been[j]) continue; n       can.push_back(j); n     } n     sort(can.begin(), can.end()); n     int num = 0; n     for (int j = 0; j < ((int)(can).size()) - 1; j++) { n       if (can[j] == can[j + 1]) { n         num = can[j]; n         break; n       } n     } n     been[num] = 1; n     res[i] = num; n     num1 = num2; n     num2 = num; n   } n   for (int i = 1; i < n + 1; i++) { n     if (((int)(v[i]).size()) == 3 && !been[i]) { n       res[n - 2] = i; n     } else if (((int)(v[i]).size()) == 2 && !been[i]) { n       res[n - 1] = i; n     } n   } n   for (auto& i : res) cout << i <<    ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef const char* cstr; n const int oo = (~0u) >> 1; n const long long int ooll = (~0ull) >> 1; n const double inf = 1e+20; n const double eps = 1e-8; n const double pi = acos(-1.0); n const int MOD = 1000000007; n template <typename type> n inline bool cmax(type& a, const type& b) { n   return a < b ? a = b, true : false; n } n template <typename type> n inline bool cmin(type& a, const type& b) { n   return b < a ? a = b, true : false; n } n template <typename type> n inline type sqr(const type& first) { n   return first * first; n } n inline int dbcmp(const double& a, const double& b) { n   return (a > b + eps) - (a < b - eps); n } n inline int sgn(const double& first) { return dbcmp(first, 0); } n template <typename type> n inline type cross(const pair<type, type>& a, const pair<type, type>& b, n                   const pair<type, type>& c) { n   return (b.first - a.first) * (c.second - a.second) - n          (b.second - a.second) * (c.first - a.first); n } n template <typename type> n inline type dot(const pair<type, type>& a, const pair<type, type>& b, n                 const pair<type, type>& c) { n   return (b.first - a.first) * (c.first - a.first) + n          (b.second - a.second) * (c.second - a.second); n } n template <typename type> n inline type gcd(type a, type b) { n   if (b) n     while ((a %= b) && (b %= a)) n       ; n   return a + b; n } n template <typename type> n inline type lcm(type a, type b) { n   return a * b / gcd(a, b); n } n template <typename type> n inline void bit_inc(vector<type>& st, int first, type inc) { n   while (first < ((int)(st).size())) st[first] += inc, first |= first + 1; n } n template <typename type> n inline type bit_sum(const vector<type>& st, int first) { n   type s = 0; n   while (first >= 0) s += st[first], first = (first & (first + 1)) - 1; n   return s; n } n template <typename type> n inline type bit_kth(const vector<type>& st, int k) { n   int first = 0, second = 0, z = 0; n   while ((1 << (++second)) < ((int)(st).size())) n     ; n   for (int i = (second - 1); i >= 0; --i) { n     if ((first += 1 << i) > ((int)(st).size()) || z + st[first - 1] > k) n       first -= 1 << i; n     else n       z += st[first - 1]; n   } n   return first; n } n inline void make_set(vector<int>& set) { n   for (int i = (0); i < (((int)(set).size())); ++i) set[i] = i; n } n inline int find_set(vector<int>& set, int first) { n   int second = first, z; n   while (second != set[second]) second = set[second]; n   while (first != set[first]) z = set[first], set[first] = second, first = z; n   return second; n } n inline bool union_set(vector<int>& set, int a, int b) { n   a = find_set(set, a), b = find_set(set, b); n   return a != b ? set[a] = b, true : false; n } n template <typename type> n inline void merge(type& a, type& b) { n   if (((int)(a).size()) < ((int)(b).size())) swap(a, b); n   for (typename type::iterator it = b.begin(); it != b.end(); b.erase(it++)) n     a.insert(*it); n } n template <typename type> n inline void merge(priority_queue<type>& a, priority_queue<type>& b) { n   if (((int)(a).size()) < ((int)(b).size())) swap(a, b); n   while (((int)(b).size())) a.push(b.top()), b.pop(); n } n struct Initializer { n   Initializer() { ios::sync_with_stdio(false); } n   ~Initializer() { cerr; } n } initializer; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   vector<vector<int> > adj(n); n   vector<vector<int> > len(n); n   for (int i = (0); i < (m); ++i) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w), --u, --v; n     adj[u].push_back(v); n     len[u].push_back(w); n     adj[v].push_back(u); n     len[v].push_back(w); n   } n   int k; n   scanf( %d , &k); n   vector<bool> isp(n); n   for (int i = (0); i < (k); ++i) { n     int p; n     scanf( %d , &p), --p; n     isp[p] = true; n   } n   long long int ans; n   int s; n   { n     priority_queue<pair<long long int, int>, vector<pair<long long int, int> >, n                    greater<pair<long long int, int> > > n         q; n     vector<long long int> d(n, ooll); n     if (cmin(d[0], 0ll)) q.push(make_pair(d[0], 0)); n     while (((int)(q).size())) { n       if (d[q.top().second] == q.top().first) { n         int u = q.top().second; n         for (int i = (0); i < (((int)(adj[u]).size())); ++i) { n           int v = adj[u][i], l = len[u][i]; n           if (cmin(d[v], d[u] + l)) q.push(make_pair(d[v], v)); n         } n         if (isp[u]) { n           ans = d[u]; n           s = u; n           break; n         } n       } n       q.pop(); n     } n   } n   { n     priority_queue<pair<long long int, int>, vector<pair<long long int, int> >, n                    greater<pair<long long int, int> > > n         q; n     vector<long long int> d(n, ooll); n     vector<int> rt(n); n     for (int i = (0); i < (n); ++i) n       if (isp[i]) n         if (cmin(d[i], 0ll)) q.push(make_pair(d[i], i)), rt[i] = i; n     while (((int)(q).size())) { n       if (d[q.top().second] == q.top().first) { n         int u = q.top().second; n         for (int i = (0); i < (((int)(adj[u]).size())); ++i) { n           int v = adj[u][i], l = len[u][i]; n           if (cmin(d[v], d[u] + l)) q.push(make_pair(d[v], v)), rt[v] = rt[u]; n         } n       } n       q.pop(); n     } n     vector<pair<long long int, pair<int, int> > > edges; n     for (int u = (0); u < (n); ++u) n       for (int i = (0); i < (((int)(adj[u]).size())); ++i) { n         int v = adj[u][i], l = len[u][i]; n         edges.push_back(make_pair(d[u] + d[v] + l, make_pair(rt[u], rt[v]))); n       } n     sort((edges).begin(), (edges).end()); n     vector<int> st(n); n     make_set(st); n     for (int i = (0); i < (((int)(edges).size())); ++i) { n       int u = edges[i].second.first, v = edges[i].second.second; n       if (union_set(st, u, v)) ans += edges[i].first; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int _ = 5e5 + 7; n int n, m, a[_], b[_]; n bool check(int mid) { n   memset(b, -1, sizeof(b)); n   b[0] = 0; n   for (int i = 1; i <= n; ++i) { n     int L = a[i], R = (a[i] + mid) % m; n     if (L <= R) { n       if (b[i - 1] <= R) n         b[i] = max(b[i - 1], L); n       else n         return 0; n     } else { n       if (R < b[i - 1] && b[i - 1] < L) n         b[i] = L; n       else n         b[i] = b[i - 1]; n     } n     if (b[i] < b[i - 1]) return 0; n   } n   return 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   int l = 0, r = m - 1, ans = 0; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n int main(){ n     //freopen( input.inp , r ,stdin); n     //freopen( output.out , w ,stdout); n     ios::sync_with_stdio(0); n     cin.tie(0); n     int t; n     cin >> t; n     while (t--){ n         int a,b; n         cin >> a >> b; n         int ans = 0; n         for (int i = 0; i < min(a,b);i++) ans+=2;  n         if (a != b){ n             for (int i = min(a,b); i < max(a,b);i++){ n                 ans+=2; n             } n             ans--; n         } n         cout << ans << endl; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long N = 100005; n char en =   n ; n long long inf = 1e16; n long long mod = 1e9 + 7; n long long power(long long x, long long n, long long mod) { n   long long res = 1; n   x %= mod; n   while (n) { n     if (n & 1) res = (res * x) % mod; n     x = (x * x) % mod; n     n >>= 1; n   } n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k; n   cin >> n >> k; n   long long curr = n * (n + 1) / 2; n   if (curr > k) { n     cout << -1 << en; n     return 0; n   } n   long long arr[n + 5]; n   for (long long i = 1; i <= n; i++) arr[i] = i; n   long long rem = k - curr; n   long long start = 1; n   while (rem) { n     long long end1 = n - start + 1; n     long long max1 = end1 - start; n     if (max1 <= 0) break; n     if (rem >= max1) { n       swap(arr[start], arr[end1]); n       rem -= max1; n     } else { n       swap(arr[start], arr[start + rem]); n       break; n     } n     start++; n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     ans += max(i, arr[i]); n   } n   cout << ans << en; n   for (long long i = 1; i <= n; i++) cout << i <<    ; n   cout << en; n   for (long long i = 1; i <= n; i++) cout << arr[i] <<    ; n   cout << en; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 9000020; n const int K = 60; n const int MOd = 1e9 + 7; n int a, b; n int nex[maxn][2]; n int ar[3020], st[maxn]; n int dn[maxn], N; n int ans; n int add(int a, int b) { n   a += b; n   a %= MOd; n   if (a < 0) return a + MOd; n   return a; n } n void dfs(int p, int n, int x) { n   st[n] = p; n   if (nex[p][0]) dfs(nex[p][0], n + 1, (x + x) & 15); n   if (nex[p][1]) dfs(nex[p][1], n + 1, (x + x + 1) & 15); n } n int main() { n   scanf( %d , &a); n   for (int i = 1; i <= a; i++) scanf( %d , &ar[i]); n   dn[0] = 1; n   for (int i = 1; i <= a; i++) { n     int n = 0; n     for (int j = i, p = 0, x = 0; j >= 1; j--) { n       st[n++] = p; n       x = (x + x + ar[j]) & 15; n       if (!nex[p][ar[j]]) { n         nex[p][ar[j]] = ++N; n         p = nex[p][ar[j]]; n         if (n > 0) dn[p] = add(dn[p], dn[st[n - 1]]); n         if (n > 1) dn[p] = add(dn[p], dn[st[n - 2]]); n         if (n > 2) dn[p] = add(dn[p], dn[st[n - 3]]); n         if (n > 3 && x != 12 && x != 10 && x != 7 && x != 15) n           dn[p] = add(dn[p], dn[st[n - 4]]); n         ans = add(ans, dn[p]); n       } else n         p = nex[p][ar[j]]; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 500 + 5; n int n, m, mod, h[MaxN], tmp, tot[3]; n long long f[MaxN << 1][MaxN]; n char ch; n void read(int &num) { n   ch = 0; n   while (ch <  0  || ch >  9 ) ch = getchar(); n   num = ch -  0 ; n } n int main() { n   scanf( %d%d%d , &n, &m, &mod); n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) { n       read(tmp); n       if (tmp) h[j]++; n     } n   for (int i = 1; i <= n; i++) tot[h[i]]++; n   f[tot[1]][tot[0]] = 1; n   for (int j = tot[0]; j >= 0; j--) n     for (int i = tot[1] + tot[0]; i >= 0; i--) { n       if (i >= 2) n         f[i - 2][j] = (f[i - 2][j] + f[i][j] * (i * (i - 1)) / 2) % mod; n       if (j >= 1) f[i][j - 1] = (f[i][j - 1] + f[i][j] * i * j) % mod; n       if (j >= 2) n         f[i + 2][j - 2] = (f[i + 2][j - 2] + f[i][j] * (j * (j - 1)) / 2) % mod; n     } n   cout << f[0][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n const int N = 1e6 + 10; n struct UFS { n   stack<pair<int *, int>> stk; n   int fa[N], rnk[N]; n   inline void init(int n) { n     for (int i = 0; i <= n; ++i) fa[i] = i, rnk[i] = 0; n   } n   inline int Find(int x) { n     while (x ^ fa[x]) x = fa[x]; n     return x; n   } n   inline void Merge(int x, int y) { n     x = Find(x), y = Find(y); n     if (x == y) return; n     if (rnk[x] <= rnk[y]) { n       stk.push({fa + x, fa[x]}); n       fa[x] = y; n       if (rnk[x] == rnk[y]) { n         stk.push({rnk + y, rnk[y]}); n         rnk[y]++; n       } n     } else { n       stk.push({fa + y, fa[y]}); n       fa[y] = x; n     } n   } n   inline void Undo() { n     *stk.top().first = stk.top().second; n     stk.pop(); n   } n } ufs; n struct Edge { n   int u, v; n }; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<int> c(n + 1); n   for (int i = 1; i <= n; i++) cin >> c[i]; n   vector<Edge> same_edges; n   map<pair<int, int>, vector<Edge>> diff_edges; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     if (c[u] == c[v]) n       same_edges.push_back({u, v}); n     else { n       if (c[u] > c[v]) swap(u, v); n       diff_edges[{c[u], c[v]}].push_back({u, v}); n     } n   } n   vector<int> group_flag(k + 1, 1); n   ufs.init((n + 1) * 2); n   for (auto &[u, v] : same_edges) { n     ufs.Merge(u * 2, v * 2 + 1); n     ufs.Merge(u * 2 + 1, v * 2); n     if (ufs.Find(u * 2) == ufs.Find(u * 2 + 1)) group_flag[c[u]] = 0; n     if (ufs.Find(v * 2) == ufs.Find(v * 2 + 1)) group_flag[c[v]] = 0; n   } n   ll good_group_cnt = 0; n   for (int i = 1; i <= k; i++) n     if (group_flag[i]) good_group_cnt++; n   ll cnt_fail = 0; n   for (auto &[key, row] : diff_edges) { n     if (!group_flag[key.first]) continue; n     if (!group_flag[key.second]) continue; n     int mark = (int)ufs.stk.size(); n     int flag = 1; n     for (auto &[u, v] : row) { n       ufs.Merge(u * 2, v * 2 + 1); n       ufs.Merge(u * 2 + 1, v * 2); n       if (ufs.Find(u * 2) == ufs.Find(u * 2 + 1)) flag = 0; n       if (ufs.Find(v * 2) == ufs.Find(v * 2 + 1)) flag = 0; n     } n     if (!flag) cnt_fail++; n     while ((int)ufs.stk.size() > mark) { n       ufs.Undo(); n     } n   } n   ll ans = good_group_cnt * (good_group_cnt - 1) / 2; n   ans = ans - cnt_fail; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   cin >> n >> t; n   int edge_n = 2 * n; n   double edges[edge_n]; n   double currX, currA; n   for (int i = 0; i < n; i++) { n     cin >> currX >> currA; n     edges[2 * i] = currX - currA / 2; n     edges[2 * i + 1] = currX + currA / 2; n   } n   for (int i = 0; i < edge_n; i++) { n     for (int j = i; j > 0 && (edges[j] < edges[j - 1]);) { n       double temp = edges[j - 1]; n       edges[j - 1] = edges[j]; n       edges[j] = temp; n       j--; n     } n   } n   int count = 2; n   for (int i = 1; i < edge_n; i += 2) { n     if (edges[i + 1] - edges[i] > t) { n       count += 2; n     } else if (edges[i + 1] - edges[i] == t) { n       count++; n     } n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n int bs(int a[], int l, int u, int x) { n   int m, p = -1; n   while (l <= u) { n     m = (l + u) / 2; n     if (a[m] == x || (a[m] < x && m + 1 <= u && a[m + 1] >= x)) n       return m; n     else if (a[m] < x) { n       p = m; n       l = m + 1; n     } else { n       u = m - 1; n     } n   } n   return p; n } n int gc(int a, int b) { n   if (!a) return b; n   return gc(b % a, a); n } n bool isP(unsigned long long int x) { n   if (x == 2) return true; n   if (x % 2 == 0 || x <= 1) return false; n   for (int i = 3; i <= sqrt(x); i += 2) { n     if ((x % i) == 0) return false; n   } n   return true; n } n int main() { n   int n, j, g, g1, pre, i, uc, f4, l, v, li, ri, a4, e1, ind, t3, n9, t, r, f, n       c1, p, p1, p3, y, x, z, t1, t2, t3n, m, k; n   int a[100000]; n   unordered_map<string, int> mp; n   priority_queue<pair<int, int>> pq; n   int ty = 0, ty1; n   set<int> st; n   set<int> sto; n   ty = INT_MAX; n   g = 0; n   vector<int> ar; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     g += a[i]; n   } n   f = 1; n   if (g % k != 0) n     cout <<  No n ; n   else { n     g1 = 0; n     ind = 0; n     for (i = 0; i < n; i++) { n       g1 += a[i]; n       ind++; n       if (g1 == (g / k)) { n         ar.push_back(ind); n         g1 = 0; n         ind = 0; n       } else if (g1 > (g / k)) { n         f = 0; n         cout <<  No n ; n         break; n       } n     } n     if (f) { n       cout <<  Yes n ; n       for (i = 0; i < ar.size(); i++) cout << ar[i] <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxint = -1u >> 1; n int n; n long long x[100010]; n bool v[100010]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %I64d , &x[i]); n   sort(x, x + n); n   long long a = -1, b = -1; n   long long ret = 0; n   for (int i = n - 2; i >= 0; i--) { n     if (v[i + 1]) continue; n     if (x[i + 1] - x[i] <= 1) { n       if (a == -1) { n         a = x[i]; n         v[i] = true; n       } else { n         b = x[i]; n         v[i] = true; n         ret += a * b; n         a = -1; n       } n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, count = 0, ans; n   cin >> n; n   string s; n   cin >> s; n   long long r = 0, b = 0; n   for (long long i = 0; i < n; i++) n     if (s[i] ==  r ) n       r++; n     else if (s[i] ==  b ) n       b++; n   for (long long i = 0; i < n; i++) n     if (i % 2 == 0 && s[i] !=  r ) n       count++; n     else if (i % 2 == 1 && s[i] !=  b ) n       count++; n   long long rx = n % 2 == 1 ? 1 + (long long)(n / 2) : n / 2; n   ans = abs(r - rx); n   count -= ans; n   ans += (count / 2); n   count = 0; n   for (long long i = 0; i < n; i++) n     if (i % 2 == 1 && s[i] !=  r ) n       count++; n     else if (i % 2 == 0 && s[i] !=  b ) n       count++; n   ans = min(ans, abs(b - rx) + (count - abs(b - rx)) / 2); n   cout << ans; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, s = 0, t = 0, r = 0, i, c, d; n   scanf( %d %d , &a, &b); n   for (i = 1; i <= 6; i++) { n     c = abs(a - i); n     d = abs(b - i); n     if (c < d) n       s++; n     else if (c > d) n       t++; n     else n       r++; n   } n   printf( %d %d %d n , s, r, t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int v; n   long long w; n   node(){}; n   node(int v, long long w) : v(v), w(w) {} n }; n vector<vector<node> > G; n int b[102550]; n long long solve(int s) { n   long long ans = 10000000000000000; n   int len = G[s].size(); n   for (int i = 0; i < len; i++) { n     node p = G[s][i]; n     if (b[p.v]) { n       ans = min(ans, p.w); n     } n   } n   if (ans == 10000000000000000) ans = -1; n   return ans; n } n int main() { n   int n, m, k, num; n   scanf( %d %d %d , &n, &m, &k); n   G.clear(); n   G.resize(n + 5); n   memset(b, 0, sizeof(b)); n   for (int i = 1; i <= m; i++) { n     int u, v; n     long long w; n     scanf( %d %d %I64d , &u, &v, &w); n     G[u].push_back(node(v, w)); n     G[v].push_back(node(u, w)); n   } n   for (int i = 1; i <= k; i++) { n     scanf( %d , &num); n     b[num] = 1; n   } n   long long ans = 10000000000000000; n   for (int i = 1; i <= n; i++) { n     if (b[i] == 1) continue; n     long long num = solve(i); n     if (num != -1) ans = min(ans, num); n   } n   if (ans == 10000000000000000) ans = -1; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, s, ans = 0, counter = 0; n   cin >> n >> s; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   while (ans < s) { n     counter++; n     ans += a[n - counter]; n   } n   cout << counter; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:60777216 ) n using namespace std; n int main() { n   int tc; n   cin >> tc; n   for (int TC = (0); TC < (tc); TC++) { n     int n, k; n     string s; n     cin >> n >> k >> s; n     k--; n     string res =   ; n     for (int i = (0); i < (n / 2 - k); i++) res.push_back( ( ); n     for (int i = (0); i < (n / 2 - k); i++) res.push_back( ) ); n     for (int i = (0); i < (k); i++) res.push_back( ( ), res.push_back( ) ); n     vector<pair<int, int> > r; n     for (int i = (0); i < (n); i++) n       if (res[i] != s[i]) { n         for (int j = (i + 1); j < (n); j++) n           if (s[j] == res[i]) { n             reverse(s.begin() + i, s.begin() + j + 1); n             r.push_back(pair<int, int>(i, j)); n             break; n           } n       } n     cout << r.size() << endl; n     for (int i = (0); i < (r.size()); i++) n       printf( %d %d n , r[i].first + 1, r[i].second + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500 + 4; n const int maxlog = 22; n const int mod = 1e9 + 7; n const int sq = 350; n int n, m; n int ans[maxn][maxn][2]; n pair<int, int> par[maxn][maxn][2]; n queue<pair<pair<int, int>, bool> > q; n vector<int> edge[maxn]; n void solve() { n   while (!q.empty()) { n     int u = q.front().first.first; n     int v = q.front().first.second; n     bool b = q.front().second; n     q.pop(); n     if (b == 0) { n       for (int i = 0; i < edge[u].size(); i++) { n         int k = edge[u][i]; n         if (!ans[k][v][1]) { n           par[k][v][1] = make_pair(u, v); n           ans[k][v][1] = ans[u][v][0] + 1; n           q.push(make_pair(make_pair(k, v), 1)); n         } n       } n     } else { n       for (int i = 0; i < edge[v].size(); i++) { n         int k = edge[v][i]; n         if (!ans[u][k][0] && k != u) { n           par[u][k][0] = make_pair(u, v); n           ans[u][k][0] = ans[u][v][1] + 1; n           q.push(make_pair(make_pair(u, k), 0)); n         } n       } n     } n   } n } n vector<pair<int, int> > res; n void print(int x, int y, bool b) { n   if (x == 0 && y == 0) return; n   res.push_back(make_pair(x, y)); n   print(par[x][y][b].first, par[x][y][b].second, b ^ 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(20); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     edge[u].push_back(v), edge[v].push_back(u); n   } n   ans[1][n][0] = 1; n   q.push(make_pair(make_pair(1, n), 0)); n   solve(); n   if (ans[n][1][0] / 2 == 0) { n     cout << -1; n     return 0; n   } n   cout << ans[n][1][0] / 2 << endl; n   print(n, 1, 0); n   reverse(res.begin(), res.end()); n   for (int i = 0; i < res.size(); i += 2) cout << res[i].first <<    ; n   cout << endl; n   for (int i = 0; i < res.size(); i += 2) cout << res[i].second <<    ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   char c[12][13]; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %s , &c[i][1]); n   int eaten(0); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (c[i][j] ==  W ) { n         if (c[i - 1][j] ==  P  || c[i + 1][j] ==  P  || c[i][j - 1] ==  P  || n             c[i][j + 1] ==  P ) { n           eaten += 1; n         } n       } n     } n   } n   printf( %d n , eaten); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (cin >> n) { n     int arr[100005]; n     for (int i = 1; i <= n; i++) { n       cin >> arr[i]; n     } n     long long cnt = 0; n     for (int i = 1; i < n; i++) { n       long long x = log2(n - i); n       arr[i + (1 << x)] += arr[i]; n       cnt += arr[i]; n       arr[i] = 0; n       cout << cnt << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string flip1(string st) { n   swap(st[0], st[1]); n   swap(st[1], st[2]); n   swap(st[2], st[3]); n   return st; n } n string flip2(string st) { n   swap(st[0], st[4]); n   swap(st[4], st[2]); n   swap(st[2], st[5]); n   return st; n } n string fun(string x) { n   string res = x; n   int i, j, k; n   for (i = 0; i < 2; i++) { n     for (j = 0; j < 4; j++) { n       for (k = 0; k < 4; k++) { n         x = flip1(x); n         res = min(res, x); n       } n       x = flip2(x); n     } n     x = flip1(x); n   } n   return res; n } n int main() { n   set<string> s; n   int n, m, i, j, k; n   string x, y; n   cin >> x; n   sort(x.begin(), x.end()); n   do { n     s.insert(fun(x)); n   } while (next_permutation(x.begin(), x.end())); n   cout << s.size() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b; n int main() { n   while (cin >> a >> b) { n     int min = 0; n     int temp = a; n     while (temp) { n       if (min < temp % 10) min = temp % 10; n       temp /= 10; n     } n     temp = b; n     while (temp) { n       if (min < temp % 10) min = temp % 10; n       temp /= 10; n     } n     min++; n     int i = 0; n     int c = 0; n     temp = a + b; n     while (a || b || c) { n       int sum = a % 10 + b % 10 + c; n       c = sum / min; n       a /= 10; n       b /= 10; n       i++; n     } n     cout << i << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:20000000 ) n string problem_name =  a ; n void init() { freopen((problem_name +  .in ).c_str(),  rt , stdin); } n struct tpoint { n   int x, y; n }; n long long vp(tpoint a, tpoint b, tpoint c) { n   tpoint f, s; n   f.x = b.x - a.x; n   f.y = b.y - a.y; n   s.x = c.x - a.x; n   s.y = c.y - a.y; n   return f.x * (long long)s.y - (long long)f.y * s.x; n } n tpoint mas[100111]; n int main() { n   int n, t; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &mas[i].x, &mas[i].y); n   int p = 1; n   scanf( %d , &t); n   while (t--) { n     tpoint c; n     long long res = 0; n     scanf( %d%d , &c.x, &c.y); n     p = 1; n     bool ok = 1; n     for (int i = 0; i < n; i++) { n       if (vp(mas[i], mas[(i + 1) % n], c) >= 0) ok = 0; n     } n     if (ok) n       for (int i = 0; i < n; i++) { n         if (p == i) p++; n         if (p >= n) p = 0; n         while (vp(mas[i], mas[p], c) < 0) { n           p++; n           if (p == n) p = 0; n         } n         p--; n         if (p < 0) p = n - 1; n         long long col = p - i + 1; n         if (col <= 0) col += n; n         if (col > 2) { n           col--; n           res += col * (col - 1) / 2; n         } n       } n     if (!ok) n       res = 0; n     else n       res = (long long)n * (n - 1) * (n - 2) / 6 - res; n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 9e5 + 7; n const long long INF = 1e9 + 7; n int main() { 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   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long inf = 2147483647; n const long long ll = 9223372036854775807, ninf = 1000000000; n const double eps = 1e-6; n const long long nll = 1000000000000000000; n class MulticoreProcessingEasy { n  public: n   int fastestTime(int jobLength, int corePenalty, vector<int> speed, n                   vector<int> cores) {} n }; n int main() { n   int a; n   while (cin >> a) { n     int A[100005] = {}; n     for (int i = 1; i <= a; i++) cin >> A[i]; n     int no = 0; n     for (int i = 1; i <= a; i++) { n       if (A[i] != A[A[i]]) { n         no = 1; n       } n     } n     if (no) { n       cout << -1 <<   n ; n       continue; n     } n     int b[100005] = {}; n     int c[100005] = {}; n     int vis[100005] = {}; n     int now = 0; n     for (int i = 1; i <= a; i++) { n       if (!vis[A[i]]) { n         now++; n         vis[A[i]] = now; n         c[now] = A[i]; n       } n       b[i] = vis[A[i]]; n     } n     cout << now <<   n ; n     for (int i = 1; i <= a; i++) cout << b[i] <<    ; n     cout <<   n ; n     for (int i = 1; i <= now; i++) cout << c[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powm(long long base, long long exp, long long mod = 1000000007) { n   long long ans = 1; n   while (exp) { n     if (exp & 1) ans = (ans * base) % mod; n     exp >>= 1, base = (base * base) % mod; n   } n   return ans; n } n long long ctl(char x, char an =  a ) { return (long long)(x - an); } n char ltc(long long x, char an =  a ) { return (char)(x + an); } n long long a[1000005], nxt[1000005], suff[1000005]; n bool overflow(long long a, long long b) { n   long double ta = a, tb = b; n   if (3e18 / ta < tb) return 1; n   return 0; n } n int main() { n   long long n, k, ans = 0; n   cin >> n >> k, nxt[n + 1] = n + 1; n   for (long long i = 1; i < n + 1; i++) cin >> a[i]; n   for (long long i = n; i >= 1; i--) { n     suff[i] = suff[i + 1] + a[i]; n     if (a[i] == 1) n       nxt[i] = nxt[i + 1]; n     else n       nxt[i] = i; n   } n   for (long long i = 1; i < n + 1; i++) { n     long long p = 1, second = 0; n     long long iter = 0; n     for (long long j = i; j < n + 1; j++) { n       if (a[j] == 1) { n         long long tot_ones = nxt[j] - j; n         if (p % k == 0) { n           long long req_ones = p / k - second; n           if (req_ones > 0 and req_ones <= tot_ones) { n             ans++; n           } n         } n         j = nxt[j] - 1; n         second += tot_ones; n       } else { n         if (overflow(p, a[j])) break; n         p *= a[j]; n         second += a[j]; n         if (p == k * second) ans++; n       } n       if (p > k * suff[i]) break; n     } n   } n   cout << ans; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define w(t) int t; cin>>t; while(t--) n #define int long long int n #define I int32_t n #define asc(i,a,n) for(int i=a;i<n;i++) n #define dsc(i,n,a) for(int i=n;i>=a;i--) n #define vi vector<int> n #define pi pair<int,int> n #define PB push_back n #define MP make_pair n I main(){ n     ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n     w(t){ n         int n; n         cin>>n; n         int a[n]; n         int f[2*n+1]; n         asc(i,0,2*n+1){ n             f[i]=1000000; n         } n         asc(i,0,n){ n             cin>>a[i]; n             f[a[i]]=i+1; n         } n         int ans=0; n         asc(k,3,2*n){ n             int r=sqrt(k); n             asc(j,1,r+1){ n                 if(k%j==0&&k!=j*j){ n                     if(f[j]+f[k/j]==k){ n                         ans++; n                     } n                 } n             } n         } n         cout<<ans<<endl; n     } n }


#include <bits/stdc++.h> n using namespace std; n struct team { n   string nam; n   int pt, diff, get; n } T[55]; n int n; n void gao(string s, int pt, int diff, int get) { n   for (int i = 0; i < n; i++) { n     if (s == T[i].nam) { n       T[i].pt += pt; n       T[i].diff += diff; n       T[i].get += get; n       break; n     } n   } n } n bool cmp(team a, team b) { n   if (a.pt != b.pt) return a.pt > b.pt; n   if (a.diff != b.diff) return a.diff > b.diff; n   return a.get > b.get; n } n void tostring(string &a, string &b, char *s) { n   int l = strlen(s); n   a =   ; n   b =   ; n   for (int i = 0; i < l; i++) { n     if (s[i] ==  - ) { n       for (int j = i + 1; j < l; j++) { n         b += s[j]; n       } n       return; n     } n     a += s[i]; n   } n } n int main() { n   while (cin >> n) { n     for (int i = 0; i < n; i++) n       cin >> T[i].nam, T[i].pt = 0, T[i].diff = 0, T[i].get = 0; n     ; n     for (int i = 0; i < n * (n - 1) / 2; i++) { n       char str[66]; n       string a, b; n       int sa, sb; n       scanf( %s %d:%d , str, &sa, &sb); n       tostring(a, b, str); n       if (sa > sb) { n         gao(a, 3, sa - sb, sa); n         gao(b, 0, sb - sa, sb); n       } else if (sa == sb) { n         gao(a, 1, sa - sb, sa); n         gao(b, 1, sb - sa, sb); n       } else { n         gao(a, 0, sa - sb, sa); n         gao(b, 3, sb - sa, sb); n       } n     } n     sort(T, T + n, cmp); n     vector<string> res; n     res.clear(); n     for (int i = 0; i < n / 2; i++) { n       res.push_back(T[i].nam); n     } n     sort(res.begin(), res.end()); n     for (int i = 0; i < res.size(); i++) { n       cout << res[i] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200005], t[200005]; n vector<int> pos[200005]; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     for (int i = 0; i <= 30; i++) pos[i].clear(); n     scanf( %s%s , s, t); n     int l1 = strlen(s), l2 = strlen(t); n     int now = -1, ans = 0; n     for (int i = 0; i < l1; i++) pos[s[i] -  a ].push_back(i); n     for (int i = 0; i < l2; i++) { n       int nxt = n           upper_bound(pos[t[i] -  a ].begin(), pos[t[i] -  a ].end(), now) - n           pos[t[i] -  a ].begin(); n       if (nxt == pos[t[i] -  a ].size()) { n         if (now == -1) { n           ans = -1; n           break; n         } else { n           now = -1; n           ans++; n           i--; n         } n       } else n         now = pos[t[i] -  a ][nxt]; n     } n     if (ans != -1 && now != -1) ans++; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long f = 1, x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int MAXN = 1e5 + 5; n struct BIT { n   int N, c[MAXN << 1]; n   inline int lowbit(int x) { return x & (-x); } n   void add(int p, int x) { n     for (; p <= N; p += lowbit(p)) c[p] += x; n   } n   int ask(int p) { n     int res = 0; n     for (; p > 0; p -= lowbit(p)) res += c[p]; n     return res; n   } n } T; n int n, m, p[MAXN], s[MAXN], b[MAXN], inc[MAXN], pref[MAXN], p1[MAXN], p2[MAXN], n     p3[MAXN << 1], ans[MAXN]; n void lisanhua(int *p1, int *p2, int *p3) { n   vector<int> v; n   for (int i = 1; i <= n; ++i) v.push_back(p1[i]); n   for (int i = 1; i <= m; ++i) v.push_back(p2[i]); n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   for (int i = 1; i <= n; ++i) n     p3[i] = lower_bound(v.begin(), v.end(), p1[i]) - v.begin() + 1; n   for (int i = 1; i <= m; ++i) n     p3[i + n] = lower_bound(v.begin(), v.end(), p2[i]) - v.begin() + 1; n } n struct node { n   int x, y, z, typ, id; n } q[MAXN << 1], tmp[MAXN << 1]; n bool cmp1(node a, node b) { n   return (a.x < b.x) || (a.x == b.x && a.y < b.y) || n          (a.x == b.x && a.y == b.y && a.z < b.z) || n          (a.x == b.x && a.y == b.y && a.z == b.z && a.typ > b.typ); n } n bool cmp2(node a, node b) { return a.y <= b.y; } n void cdq(int l, int r) { n   if (l == r) return; n   int mid = (l + r) >> 1; n   cdq(l, mid); n   cdq(mid + 1, r); n   int pa = l, push_back = mid + 1, ct = 0; n   while (pa <= mid && push_back <= r) { n     if (cmp2(q[pa], q[push_back])) { n       if (q[pa].typ == 1) T.add(q[pa].z, 1); n       tmp[++ct] = q[pa++]; n     } else { n       if (q[push_back].typ == 0) ans[q[push_back].id] += T.ask(q[push_back].z); n       tmp[++ct] = q[push_back++]; n     } n   } n   while (pa <= mid) { n     if (q[pa].typ == 1) T.add(q[pa].z, 1); n     tmp[++ct] = q[pa++]; n   } n   while (push_back <= r) { n     if (q[push_back].typ == 0) ans[q[push_back].id] += T.ask(q[push_back].z); n     tmp[++ct] = q[push_back++]; n   } n   for (int i = l; i <= mid; ++i) { n     if (q[i].typ == 1) T.add(q[i].z, -1); n   } n   for (int i = 1; i <= ct; ++i) q[l + i - 1] = tmp[i]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   n = read(); n   m = read(); n   T.N = n + m; n   for (int i = 1; i <= n; ++i) p[i] = read(); n   for (int i = 1; i <= n; ++i) s[i] = read(); n   for (int i = 1; i <= n; ++i) b[i] = read(); n   for (int i = 1; i <= m; ++i) inc[i] = read(); n   for (int i = 1; i <= m; ++i) pref[i] = read(); n   for (int i = 1; i <= n; ++i) p1[i] = b[i] + p[i]; n   for (int i = 1; i <= m; ++i) p2[i] = inc[i] + pref[i]; n   lisanhua(p1, p2, p3); n   for (int i = 1; i <= n + m; ++i) q[i].x = p3[i]; n   for (int i = 1; i <= n; ++i) p1[i] = p[i] - b[i]; n   for (int i = 1; i <= m; ++i) p2[i] = inc[i] - pref[i]; n   lisanhua(p1, p2, p3); n   for (int i = 1; i <= n + m; ++i) q[i].y = p3[i]; n   for (int i = 1; i <= n; ++i) p1[i] = -s[i]; n   for (int i = 1; i <= m; ++i) p2[i] = -inc[i]; n   lisanhua(p1, p2, p3); n   for (int i = 1; i <= n + m; ++i) q[i].z = p3[i]; n   for (int i = 1; i <= n; ++i) q[i].typ = 1, q[i].id = i; n   for (int i = 1; i <= m; ++i) q[i + n].id = i; n   sort(q + 1, q + n + m + 1, cmp1); n   cdq(1, n + m); n   for (int i = 1; i <= m; ++i) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long A, B; n }; n bool func(node s1, node s2) { return s1.A < s2.A; } n int main() { n   long long n; n   cin >> n; n   std::vector<node> b(n); n   vector<long long> a(n); n   ; n   for (long long i = 0; i <= n - 1; i++) { n     cin >> a[i]; n   } n   for (long long j = 0; j <= n - 1; j++) { n     cin >> b[j].A; n     b[j].B = j; n   } n   sort(b.begin(), b.end(), func); n   sort(a.begin(), a.end(), greater<int>()); n   vector<long long> c(n); n   ; n   for (long long i = 0; i <= n - 1; i++) { n     c[b[i].B] = a[i]; n   } n   for (long long i = 0; i <= n - 1; i++) { n     cout << c[i] <<    ; n   } n   cout << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long a, b; n   cin >> a >> b; n   long long x1 = a, x2 = b, ans1 = 0, ans2 = 0; n   if (a < b) n     ans1 += a; n   else n     ans2 += b; n   while (x1 != x2) { n     if (x1 < x2) { n       if ((x1 + a) < x2) n         ans1 += a; n       else if ((x1 + a) == x2) { n         if (a < b) n           ans2 += a; n         else n           ans1 += a; n       } else n         ans2 += x2 - x1; n       x1 += a; n     } else { n       if ((x2 + b) < x1) n         ans2 += b; n       else if ((x2 + b) == x1) { n         if (a < b) n           ans2 += b; n         else n           ans1 += b; n       } else n         ans1 += x1 - x2; n       x2 += b; n     } n   } n   if (ans1 == ans2) n     cout <<  Equal ; n   else if (ans1 > ans2) n     cout <<  Dasha ; n   else n     cout <<  Masha ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, tc; n   cin >> t; n   for (tc = 0; tc < t; tc++) { n     int n, i; n     cin >> n; n     vector<int> v(n), mx(n + 1, 0), mn(n + 1, n + 5); n     for (i = 0; i < n; i++) { n       cin >> v[i]; n       mx[v[i]] = i; n       mn[v[i]] = i; n     } n     for (i = 2; i <= n; i++) { n       mx[i] = max(mx[i - 1], mx[i]); n       mn[i] = min(mn[i - 1], mn[i]); n     } n     for (i = 1; i <= n; i++) { n       if (mx[i] - mn[i] == i - 1) { n         cout <<  1 ; n       } else { n         cout <<  0 ; n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long lower, upper, res; n char num1[65536], num2[65536], num[65536]; n void DFS(int depth, bool lower_flag, bool upper_flag) { n   if (depth == 10) { n     long long a = 0, b = 0; n     bool flag = false; n     for (int i = 0; i < 10; i++) { n       if (num[i] !=  0 ) { n         flag = true; n       } n       if (flag) { n         a = a * 10 + num[i] -  0 ; n         b = b * 10 + 9 - (num[i] -  0 ); n       } n     } n     if (lower <= a && a <= upper) { n       res = max(res, a * b); n     } n     return; n   } n   char a, b; n   if (lower_flag && upper_flag) { n     a = min(num1[depth], num2[depth]); n     b = max(num1[depth], num2[depth]); n   } else if (lower_flag) { n     a = num1[depth]; n     b =  9 ; n   } else if (upper_flag) { n     a =  0 ; n     b = num2[depth]; n   } else { n     a =  0 ; n     b =  9 ; n   } n   if (a <=  4  &&  4  <= b) { n     num[depth] =  4 ; n     DFS(depth + 1, lower_flag && num1[depth] ==  4 , n         upper_flag && num2[depth] ==  4 ); n   } n   if (a <=  5  &&  5  <= b) { n     num[depth] =  5 ; n     DFS(depth + 1, lower_flag && num1[depth] ==  5 , n         upper_flag && num2[depth] ==  5 ); n   } n   num[depth] = a; n   DFS(depth + 1, lower_flag && num1[depth] == a, n       upper_flag && num2[depth] == a); n   num[depth] = b; n   DFS(depth + 1, lower_flag && num1[depth] == b, n       upper_flag && num2[depth] == b); n } n int main() { n   scanf( %lld%lld , &lower, &upper); n   sprintf(num1,  %010lld , lower); n   sprintf(num2,  %010lld , upper); n   DFS(0, true, true); n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int b) { n   long long int ans = 1; n   while (b) { n     if (b & 1) { n       ans = ((ans % 1000000007) * (a % 1000000007)) % 1000000007; n     } n     a = ((a % 1000000007) * (a % 1000000007)) % 1000000007; n     b = b >> 1; n   } n   return ans; n } n long long int powe(long long int a, long long int b) { n   long long int ans = 1; n   while (b) { n     if (b & 1) { n       ans = a * ans; n     } n     a = a * a; n     b = b >> 1; n   } n   return ans; n } n void solve() { n   string s; n   cin >> s; n   vector<pair<char, long long int> > v; n   for (int i = 0; i < s.length(); i++) { n     long long int curlen = 1; n     while (i < s.length() - 1 && s[i + 1] == s[i]) { n       curlen++; n       i++; n     } n     v.push_back({s[i], curlen}); n   } n   if (v.size() % 2 == 0) { n     cout << 0 << endl; n   } else { n     int i = 0; n     for (i = 0; i < v.size() / 2; i++) { n       if (v[i].first == v[v.size() - 1 - i].first && n           v[i].second + v[v.size() - 1 - i].second >= 3) { n       } else { n         cout << 0 << endl; n         return; n       } n     } n     if (v[v.size() / 2].second >= 2) n       cout << v[v.size() / 2].second + 1 << endl; n     else n       cout << 0 << endl; n     return; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int t = 1; n   while (t > 0) { n     solve(); n     t--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v(7); n int main() { n   int n; n   string s; n   cin >> n; n   for (int j = 0; j < n; j++) { n     auto a = 0; n     cin >> s; n     for (auto &c : s) { n       int r = c - 48; n       v.at(a) += r; n       a++; n     } n   } n   auto result = *max_element(v.begin(), v.end()); n   cout << result; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 200010; n const long long int MOD = 998244353; n long long int mod(long long int n) { return (n % MOD + MOD) % MOD; } n void solve() { n   long long int n; n   cin >> n; n   long long int a[n]; n   long long int sum = 0; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   long long int bob = 0, alice = 0, prev_bob = 0, prev_alice = 0, sum1 = 0, n                 sum2 = 0, curr1 = 0, curr2 = 0; n   long long int i = 0, j = n - 1, f = 1, count = 0; n   sum1 = a[0]; n   prev_alice = a[0]; n   count = 1; n   i = 1; n   while (i <= j) { n     if (f == 0) { n       while (alice <= prev_bob && i <= j) { n         alice += a[i]; n         i++; n       } n       count++; n       prev_alice = alice; n       sum1 += alice; n       alice = 0; n       f = 1; n     } else { n       while (bob <= prev_alice && i <= j) { n         bob += a[j]; n         j--; n       } n       count++; n       prev_bob = bob; n       sum2 += bob; n       bob = 0; n       f = 0; n     } n   } n   cout << count <<     << sum1 <<     << sum2 << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int T = 1; n   cin >> T; n   for (long long int i = 0; i < T; i++) solve(); n } n