#include <bits/stdc++.h> n using namespace std; n bool isPrime(int n) { n   if (n <= 1) return false; n   for (int i = 2; i < n; i++) n     if (n % i == 0) return false; n   return true; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int sum = 0; n     vector<long long> a(n + 1, 0); n     long long odd = -1; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       sum += a[i]; n       if (a[i] & 1) odd = i; n     } n     if (isPrime(sum)) { n       cout << n - 1 << endl; n       for (int i = 1; i <= n; i++) { n         if (i == odd) continue; n         cout << i <<    ; n       } n       cout << endl; n     } else { n       cout << n << endl; n       for (int i = 1; i <= n; i++) { n         cout << i <<    ; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, c, i; n   cin >> r >> c; n   string s; n   int R[11] = {0}, C[11] = {0}; n   for (i = 0; i < r; ++i) { n     cin >> s; n     for (int j = 0; j < c; ++j) { n       if (s[j] ==  S ) { n         R[i] = 1; n         C[j] = 1; n       } n     } n   } n   int count = 0; n   for (i = 0; i < r; ++i) { n     for (int j = 0; j < c; ++j) { n       if (R[i] == 0 || C[j] == 0) { n         count += 1; n       } n     } n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 * 300 + 100; n vector<pair<int, int> > v[N], e; n pair<int, int> dir[N]; n bool mark[N], mark2[N], use[N]; n void sfd(int p) { n   mark2[p] = true; n   for (int i = 0; i < v[p].size(); i++) { n     int id = v[p][i].second, q = v[p][i].first; n     if (use[id] == false && mark2[q] == false) { n       use[id] = true; n       dir[id].first = q; n       dir[id].second = p; n       sfd(q); n     } else if (dir[id].first == q && mark2[q] == false) { n       sfd(q); n     } n   } n } n void dfs(int p) { n   mark[p] = true; n   for (int i = 0; i < v[p].size(); i++) { n     int id = v[p][i].second, q = v[p][i].first; n     if (mark[q] == false) { n       dir[id].first = p; n       dir[id].second = q; n       use[id] = true; n       dfs(q); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(make_pair(b, i)); n     v[b].push_back(make_pair(a, i)); n     e.push_back(make_pair(a, b)); n   } n   dfs(1); n   sfd(1); n   for (int i = 1; i <= n; i++) n     if (mark[i] == false || mark2[i] == false) { n       cout << 0; n       return 0; n     } n   for (int i = 0; i < m; i++) { n     if (use[i]) n       cout << dir[i].first <<     << dir[i].second <<   n ; n     else n       cout << e[i].first <<     << e[i].second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[110][110]; n bool equal_row(int i, int k) { n   for (int j = 0; j < m; j++) n     if (a[i][j] != a[k][j]) return false; n   return true; n } n bool can(int from, int mid, int end) { n   int i = mid - 1, k = mid; n   while (i >= 0 && k < end) { n     if (!equal_row(i, k)) return false; n     i--, k++; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   if (n & 1) n     cout << n <<   n ; n   else { n     int ans = n; n     int end = n; n     while (ans % 2 == 0) { n       int mid = ans / 2; n       if (!can(0, mid, end) || mid == 0) break; n       ans = mid; n       end = mid; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, m, sum, tot; n struct P { n   int val, x, y; n } a[500]; n struct R { n   int x, y; n } b[10]; n bool cmp1(const P& a, const P& b) { return a.val > b.val; } n bool cmp2(const R& a, const R& b) { return a.y > b.y; } n bool Judge(int i, int j, int k, int u) { n   int num[5], tmp = 1; n   memset(num, 0, sizeof(num)); n   b[1].x = a[i].x, b[1].y = a[i].y; n   b[2].x = a[j].x, b[2].y = a[j].y; n   b[3].x = a[k].x, b[3].y = a[k].y; n   b[4].x = a[u].x, b[4].y = a[u].y; n   sort(b + 1, b + 4 + 1, cmp2); n   num[1]++; n   for (int l = 2; l <= 4; l++) n     if (b[l].y == b[l - 1].y) n       num[tmp]++; n     else n       num[tmp + 1]++, tmp++; n   if (num[1] > num[2]) swap(num[1], num[2]); n   if (tmp != 2 || num[1] == 1 && num[2] == 3) return true; n   int t1 = abs(b[1].x - b[2].x), t2 = abs(b[3].x - b[4].x); n   if (t1 < t2) swap(t1, t2); n   if (t1 == 3 && t2 == 2 || t1 == 2 && t2 == 1) return false; n   return true; n } n int main() { n   scanf( %d , &T); n   while (T--) { n     sum = tot = 0; n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         scanf( %d , &a[++tot].val); n         a[tot].x = i; n         a[tot].y = j; n       } n     sort(a + 1, a + tot + 1, cmp1); n     if (n <= 3 || n == 4 && m == 1) { n       for (int i = 1; i <= n; i++) sum += a[i].val; n       printf( %d n , sum); n       continue; n     } n     for (int i = 1; i <= 8; i++) n       for (int j = i + 1; j <= 8; j++) n         for (int k = j + 1; k <= 8; k++) n           for (int u = k + 1; u <= 8; u++) n             if (Judge(i, j, k, u)) n               sum = max(sum, a[i].val + a[j].val + a[k].val + a[u].val); n     printf( %d n , sum); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a(201), b(201); n int n, m; n int dp[201][2000]; n int fun(int ind, int val) { n   if (ind >= n) return val; n   if (dp[ind][val] != (-1)) return dp[ind][val]; n   int i, temp = INT_MAX, j; n   for (i = 0; i < m; i++) { n     j = fun(ind + 1, ((a[ind] & b[i]) | val)); n     temp = min(temp, j); n   } n   return dp[ind][val] = temp; n } n int main() { n   int i, j; n   cin >> n >> m; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < m; i++) cin >> b[i]; n   for (i = 0; i < n; i++) n     for (j = 0; j < 2000; j++) dp[i][j] = (-1); n   cout << fun(0, 0); n   return 0; n } n


#include <bits/stdc++.h> n char c[301][301]; n int n, m, k; n int v[5][2] = {{0, 0}, {-1, 0}, {1, 0}, {0, -1}, {0, 1}}; n void read() { n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 0; i < n; i++) scanf( %s , &c[i]); n } n void print(int i, int j, int r) { n   for (int z = 0; z < 5; z++) n     printf( %d %d n , i + v[z][0] * r + 1, j + v[z][1] * r + 1); n } n int process() { n   bool ok; n   int i, j, x, y, z, tot = 0; n   for (int r = 1; (r < 2 * n) && (r < 2 * m); r++) { n     for (i = r; i < n - r; i++) { n       for (j = r; j < m - r; j++) { n         ok = true; n         for (z = 0; z < 5 && ok; z++) { n           if (c[i + (v[z][0] * r)][j + (v[z][1] * r)] ==  . ) ok = false; n         } n         if (ok) tot++; n         if (tot == k) { n           print(i, j, r); n           return 0; n         } n       } n     } n   } n   return 1; n } n int main() { n   read(); n   if (process()) printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300020; n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n int T, n; n char x[N]; n int main() { n   T = read(); n   while (T--) { n     n = read(); n     scanf( %s , x); n     int c1 = 0, c2 = 0, c3 = 0; n     for (int i = 0; i < n; i++) { n       if (x[i] ==  > ) c1++; n       if (x[i] ==  < ) c2++; n       if (x[i] ==  -  || x[(i + 1) % n] ==  - ) c3++; n     } n     if (c1 != 0 && c2 != 0) n       printf( %d n , c3); n     else n       printf( %d n , n); n   } n   return 0; n } n


#include<cstdio> n #include<cstring> n #include<algorithm> n using namespace std; n int t,n,m,f[100005]; n int main() n { n  scanf( %d ,&t); n  while(t--) n  { n   memset(f,0,sizeof(f)); n   scanf( %d%d ,&n,&m); n   for(int i=1;i<=n;i++) n    for(int j=1;j<=m;j++) n    { n     int x; n     scanf( %d ,&x); n     f[i+j]^=x; n    } n   int fl=0; n   for(int i=1;i<=n+m;i++) n    if(f[i]) fl=1; n   if(fl) printf( Ashish n ); n   else printf( Jeel n ); n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 2005; n int n, m; n char B[Maxn][Maxn]; n bool U[Maxn][Maxn], D[Maxn][Maxn], L[Maxn][Maxn], R[Maxn][Maxn]; n long long res; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , B[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       U[i][j] = (i == 0 || U[i - 1][j]) && B[i][j] ==  . ; n       L[i][j] = (j == 0 || L[i][j - 1]) && B[i][j] ==  . ; n     } n   for (int i = n - 1; i >= 0; i--) n     for (int j = m - 1; j >= 0; j--) { n       D[i][j] = (i == n - 1 || D[i + 1][j]) && B[i][j] ==  . ; n       R[i][j] = (j == m - 1 || R[i][j + 1]) && B[i][j] ==  . ; n     } n   for (int i = 1; i + 1 < n; i++) res += L[i][m - 1]; n   for (int j = 1; j + 1 < m; j++) res += U[n - 1][j]; n   for (int i = 1; i + 1 < n; i++) n     for (int j = 1; j + 1 < m; j++) n       res += int(U[i][j] && L[i][j]) + int(U[i][j] && R[i][j]) + n              int(D[i][j] && L[i][j]) + int(D[i][j] && R[i][j]); n   for (int i = 1; i + 1 < n; i++) { n     int ft = 0, fb = 0, fts = 0, fbs = 0; n     for (int j = 1; j + 1 < m; j++) { n       if (U[i][j]) res += fb + fts; n       if (D[i][j]) res += ft + fbs; n       if (B[i][j] ==  . ) { n         ft += U[i][j]; n         fb += D[i][j]; n         if (j - 1 > 0) { n           fts += U[i][j - 1]; n           fbs += D[i][j - 1]; n         } n       } else n         ft = fb = fts = fbs = 0; n     } n   } n   for (int j = 1; j + 1 < m; j++) { n     int fl = 0, fr = 0, fls = 0, frs = 0; n     for (int i = 1; i + 1 < n; i++) { n       if (L[i][j]) res += fr + fls; n       if (R[i][j]) res += fl + frs; n       if (B[i][j] ==  . ) { n         fl += L[i][j]; n         fr += R[i][j]; n         if (i - 1 > 0) { n           fls += L[i - 1][j]; n           frs += R[i - 1][j]; n         } n       } else n         fl = fr = fls = frs = 0; n     } n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, fs[100010], to[300010], nx[300010], e, sz[300010], as, bs, l[300010], n     op, ed, yoo, aya, ans[100010]; n bool p[100010], done[100010], q[100010], r[100010]; n void ins(int x, int y) { n   nx[++e] = fs[x]; n   fs[x] = e; n   to[e] = y; n   nx[++e] = fs[y]; n   fs[y] = e; n   to[e] = x; n } n int main() { n   cin >> n >> m; n   for (int i = 1, x, y; i <= m; ++i) { n     cin >> x >> y; n     ins(x, y); n     sz[x]++; n     sz[y]++; n   } n   for (int i = 1; i <= n; ++i) n     if (!done[i]) { n       for (done[l[op = ed = 0] = i] = 1; op <= ed; ++op) n         for (int v = fs[l[op]]; v; v = nx[v]) n           if (!done[to[v]]) { n             done[to[v]] = 1; n             p[to[v]] = p[l[op]] ^ 1; n             l[++ed] = to[v]; n           } n     } n   for (int i = 1; i <= n; ++i) n     if (!p[i]) n       as++; n     else n       bs++; n   if (as % 3 == 0) { n     cout <<  YES  << endl; n     int out = 0; n     for (int i = 1; i <= n; ++i) n       if (p[i]) ans[i] = (out++) / 3 + 1; n     for (int i = 1; i <= n; ++i) n       if (!p[i]) ans[i] = (out++) / 3 + 1; n     for (int i = 1; i <= n; ++i) cout << ans[i] <<    ; n     cout << endl; n     return 0; n   } n   if (as % 3 == 2) { n     for (int i = 1; i <= n; ++i) p[i] ^= 1; n     swap(as, bs); n   } n   for (int i = 1; i <= n; ++i) n     if (!p[i]) { n       if (sz[i] <= bs - 2) { n         cout <<  YES  << endl; n         int out = 3; n         ans[i] = 1; n         for (int v = fs[i]; v; v = nx[v]) n           if (p[to[v]]) q[to[v]] = 1; n         for (int j = 1, k = 0; j <= n && k < 2; ++j) n           if (p[j] && !q[j]) { n             ans[j] = 1; n             k++; n           } n         for (int j = 1; j <= n; ++j) n           if (!p[j] && j != i) ans[j] = (out++) / 3 + 1; n         for (int j = 1; j <= n; ++j) n           if (p[j] && ans[j] != 1) ans[j] = (out++) / 3 + 1; n         for (int j = 1; j <= n; ++j) cout << ans[j] <<    ; n         cout << endl; n         return 0; n       } n     } n   memset(q, 0, sizeof q); n   for (int i = 1; i <= n; ++i) n     if (p[i] && (yoo == 0 || as - sz[i] > as - sz[yoo])) yoo = i; n   for (int v = fs[yoo]; v; v = nx[v]) q[to[v]] = 1; n   int opt = 0; n   for (int i = 1; i <= n; ++i) n     if (p[i] && i != yoo && as - sz[i] >= 2) { n       int c = 0, a = as - sz[yoo], b = as - sz[i]; n       for (int v = fs[i]; v; v = nx[v]) n         if (!q[to[v]]) c++; n       c = a - c; n       if (a >= 2 && b >= 2 && a + b - c >= 4) { n         int out = 6, k = 0; n         cout <<  YES  << endl; n         for (int v = fs[i]; v; v = nx[v]) r[to[v]] = 1; n         ans[i] = 1; n         for (int j = 1; j <= n && k < 2; ++j) n           if (!p[j] && !r[j] && q[j]) { n             ans[j] = 1; n             k++; n           } n         for (int j = 1; j <= n && k < 2; ++j) n           if (!p[j] && !r[j]) { n             ans[j] = 1; n             k++; n           } n         k = 0; n         ans[yoo] = 2; n         for (int j = 1; j <= n && k < 2; ++j) n           if (!p[j] && !q[j] && !ans[j]) { n             ans[j] = 2; n             k++; n           } n         for (int j = 1; j <= n; ++j) n           if (!p[j] && !ans[j]) ans[j] = (out++) / 3 + 1; n         for (int j = 1; j <= n; ++j) n           if (p[j] && !ans[j]) ans[j] = (out++) / 3 + 1; n         for (int j = 1; j <= n; ++j) cout << ans[j] <<    ; n         cout << endl; n         return 0; n       } n     } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[70]; n int n; n long long ans[100010], cur = 0; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     for (int j = 60; j >= 0; j--) n       if ((x >> j) & 1) { n         v[j].push_back(x); n         break; n       } n   } n   for (int i = 1; i <= n; i++) { n     bool flag = 0; n     for (int j = 0; j <= 60; j++) n       if (!(cur & (1ll << j)) && !v[j].empty()) { n         cur ^= (ans[i] = v[j].back()); n         v[j].pop_back(); n         flag = 1; n         break; n       } n     if (!flag) { n       cout <<  No  << endl; n       return 0; n     } n   } n   cout <<  Yes  << endl; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   long long int n, ans, c = 1, i; n   cin >> t; n   while (t--) { n     cin >> n; n     c = 1, ans = 0, i = 1; n     while (n > 0) { n       if (n <= 4) { n         if (i % 2 != 0) { n           ans = ans + max(c, n - 1); n         } else n           ans += n - max(c, n - 1); n         break; n       } else if (n > 4 && (n % 2 != 0 || n % 4 == 0)) { n         if (i % 2 != 0) ans = ans + 1; n         i++; n         n--; n       } else { n         if (i % 2 != 0) { n           ans = ans + n / 2; n         } n         i++; n         n = n / 2; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[200001]; n int deg[200001]; n bool vis[200001]; n void dfs(int s) { n   vis[s] = 1; n   deg[s] = 1; n   for (auto i : v[s]) { n     if (vis[i]) continue; n     dfs(i); n     deg[s] += deg[i]; n   } n } n void solve(int s) { n   for (auto i : v[s]) n     if (deg[i] % 2 == 0) solve(i); n   cout << s <<   n ; n   for (auto i : v[s]) n     if (deg[i] % 2 == 1) solve(i); n } n int main() { n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   int rt; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     if (x == 0) n       rt = i; n     else n       v[x].push_back(i); n   } n   dfs(rt); n   solve(rt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, h; n   cin >> n >> h; n   vector<int> v(n); n   for (auto &a : v) cin >> a; n   int res = 0; n   for (int i = 0; i < n; i++) { n     if (v[i] <= h) n       res++; n     else n       res += 2; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int c[n + 1], e[n - 1][2]; n   for (int i = 0; i < n - 1; ++i) { n     cin >> e[i][0] >> e[i][1]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> c[i]; n   } n   vector<int> e1; n   vector<int> e2; n   for (int i = 0; i < n - 1; ++i) { n     if (c[e[i][1]] != c[e[i][0]]) { n       e1.push_back(e[i][1]); n       e2.push_back(e[i][0]); n     } n   } n   bool k = true; n   int v = 1; n   if (n > 2 && e1.size() > 1) { n     if (e1[0] == e1[1] || e1[0] == e2[1]) { n       v = e1[0]; n     } else if (e2[0] == e1[1] || e2[0] == e2[1]) { n       v = e2[0]; n     } else { n       k = false; n     } n     for (int i = 1; i < e1.size(); ++i) { n       if (e1[i] != v && e2[i] != v) { n         k = false; n         break; n       } n     } n   } n   if (e1.size() == 1) v = e1[0]; n   if (k || n < 3) n     cout <<  YES  n          <<   n  n          << v; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void rip() { n   cout << -1; n   exit(0); n } n set<int> st; n set<int> seen; n vector<int> ans; n int main() { n   int N; n   cin >> N; n   for (int i = 1; i <= N; i++) { n     int x; n     cin >> x; n     if (x > 0) { n       if (!seen.insert(x).second) rip(); n       st.insert(x); n     } else { n       if (st.find(-x) == st.end()) rip(); n       st.erase(-x); n     } n     if (st.empty()) { n       ans.push_back(i); n       seen.clear(); n     } n   } n   if (ans.empty() || ans.back() != N) rip(); n   assert(st.empty()); n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i] - (i == 0 ? 0 : ans[i - 1]) <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, mz; n char tmp; n bool t[51][51]; n bool m[51][51]; n int mel, skm; n bool atr; n bool state; n void apstaig(int x, int y) { n   int i, j, z; n   for (i = x; i <= mz; i++) { n     if (t[i][y] == false) n       break; n     else { n       m[i][y] = !state; n       skm++; n       j = y - 1; n       while (j > 0) { n         if (t[i][j] == false) n           break; n         else { n           if (m[i][j] == state) { n             m[i][j] = !state; n             skm++; n           } n         } n         j--; n       } n       j = y + 1; n       while (j <= n) { n         if (t[i][j] == false) n           break; n         else { n           if (m[i][j] == state) { n             m[i][j] = !state; n             skm++; n           } n         } n         j++; n       } n     } n   } n   for (i = x - 1; i > 0; i--) { n     if (t[i][y] == false) n       break; n     else { n       m[i][y] = !state; n       skm++; n       j = y - 1; n       while (j > 0) { n         if (t[i][j] == false) n           break; n         else { n           if (m[i][j] == state) { n             m[i][j] = !state; n             skm++; n           } n         } n         j--; n       } n       j = y + 1; n       while (j <= n) { n         if (t[i][j] == false) n           break; n         else { n           if (m[i][j] == state) { n             m[i][j] = !state; n             skm++; n           } n         } n         j++; n       } n     } n   } n   for (i = y - 1; i > 0; i--) { n     if (t[x][i] == false) n       break; n     else { n       if (m[x][i] == state) { n         m[j][i] = !state; n         skm++; n       } n       j = x - 1; n       while (j > 0) { n         if (t[j][i] == false) n           break; n         else { n           if (m[j][i] == state) { n             m[j][i] = !state; n             skm++; n           } n         } n         j--; n       } n       j = x + 1; n       while (j <= mz) { n         if (t[j][i] == false) n           break; n         else { n           if (m[j][i] == state) { n             m[j][i] = !state; n             skm++; n           } n         } n         j++; n       } n     } n   } n   for (i = y + 1; i <= n; i++) { n     if (t[x][i] == false) n       break; n     else { n       if (m[x][i] == state) { n         m[j][i] = !state; n         skm++; n       } n       j = x - 1; n       while (j > 0) { n         if (t[j][i] == false) n           break; n         else { n           if (m[j][i] == state) { n             m[j][i] = !state; n             skm++; n           } n         } n         j--; n       } n       j = x + 1; n       while (j <= mz) { n         if (t[j][i] == false) n           break; n         else { n           if (m[j][i] == state) { n             m[j][i] = !state; n             skm++; n           } n         } n         j++; n       } n     } n   } n } n int main() { n   int i, j; n   cin >> n >> mz; n   mel = 0; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= mz; j++) { n       cin >> tmp; n       if (tmp ==  B ) { n         t[j][i] = true; n         mel++; n         m[j][i] = false; n       } else n         t[j][i] = false; n     } n   atr = true; n   state = false; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= mz; j++) { n       if (t[j][i] == true) { n         skm = 0; n         apstaig(j, i); n         if (skm != mel) { n           atr = false; n           break; n         } n         state = !state; n       } n     } n   if (atr) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n long long c[100005], a[100005], b[100005], cur[100005]; n int n, m; n void update(int x, long long y) { n   while (x <= m) { n     c[x] += y; n     if (c[x] >= 1000000007) c[x] -= 1000000007; n     x += x & (-x); n   } n } n long long query(int x) { n   long long sum = 0; n   while (x > 0) { n     sum += c[x]; n     if (sum >= 1000000007) sum -= 1000000007; n     x -= x & (-x); n   } n   return sum % 1000000007; n } n int main() { n   int i, j; n   while (~scanf( %d , &n)) { n     for (i = 0; i < n; i++) scanf( %I64d , &a[i]), b[i] = a[i]; n     sort(b, b + n); n     m = unique(b, b + n) - b; n     memset(c, 0, sizeof(c)); n     memset(cur, 0, sizeof(cur)); n     for (i = 0; i < n; i++) { n       j = lower_bound(b, b + m, a[i]) - b + 1; n       long long t = query(j), t1 = cur[j]; n       cur[j] = (t * a[i] + a[i]) % 1000000007; n       t = cur[j] - t1; n       if (t < 0) t += 1000000007; n       update(j, t); n     } n     long long sum = 0; n     for (i = 1; i <= m; i++) sum += cur[i]; n     sum %= 1000000007; n     if (sum < 0) sum += 1000000007; n     printf( %I64d n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e3 + 10, INF = 0x3f3f3f3f; n struct edge { n   int x; n   int y; n } e[maxn]; n int w[maxn][maxn]; n int num[maxn]; n vector<int> vec[maxn]; n int depth[maxn]; n int fa[maxn]; n void dfs(int cur, int p, int dep) { n   fa[cur] = p; n   depth[cur] = dep; n   for (int x : vec[cur]) { n     if (x != p) { n       dfs(x, cur, dep + 1); n     } n   } n   num[cur] = INF; n } n bool lca_modify(int x, int y, int k) { n   int minn = INF; n   if (depth[x] < depth[y]) swap(x, y); n   while (depth[x] != depth[y]) { n     if (num[x] == INF) { n       num[x] = k; n       w[x][fa[x]] = w[fa[x]][x] = k; n     } n     minn = min(minn, num[x]); n     x = fa[x]; n   } n   while (x != y) { n     if (num[x] == INF) { n       num[x] = k; n       w[x][fa[x]] = w[fa[x]][x] = k; n     } n     minn = min(minn, num[x]); n     x = fa[x]; n     if (num[y] == INF) { n       num[y] = k; n       w[y][fa[y]] = w[fa[y]][y] = k; n     } n     minn = min(minn, num[y]); n     y = fa[y]; n   } n   return minn == k; n } n const int maxm = 5e3 + 10; n struct query { n   int x, y, len; n } q[maxm]; n bool cmp(query a, query b) { return a.len > b.len; } n int main() { n   int n, m; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     e[i].x = x, e[i].y = y; n     vec[x].push_back(y); n     vec[y].push_back(x); n   } n   dfs(1, 0, 1); n   cin >> m; n   int x, y, len; n   for (int i = 1; i <= m; i++) { n     cin >> x >> y >> len; n     q[i].x = x, q[i].y = y, q[i].len = len; n   } n   sort(q + 1, q + 1 + m, cmp); n   int flag = 1; n   for (int i = 1; i <= m; i++) { n     x = q[i].x, y = q[i].y, len = q[i].len; n     if (lca_modify(x, y, len) == false) { n       flag = 0; n       break; n     } n   } n   if (!flag) { n     cout << -1 << endl; n   } else { n     for (int i = 0; i < n - 1; i++) { n       x = e[i].x, y = e[i].y; n       if (x > y) swap(x, y); n       if (w[x][y] == 0) n         cout << 1000000; n       else n         cout << w[x][y]; n       if (i != n - 2) n         cout <<    ; n       else n         cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 20, N = 1e6 + 5; n const long long MOD = 1e9 + 7; n int dp[1 << L], n; n long long pow2[N]; n int main() { n   scanf( %d , &n); n   int a; n   for (int i = (int)1; i <= n; i++) { n     scanf( %d , &a); n     dp[a]++; n   } n   pow2[0] = 1; n   for (int i = (int)1; i <= N - 1; i++) pow2[i] = pow2[i - 1] * 2 % MOD; n   for (int i = (int)0; i <= L - 1; i++) n     for (int msk = (int)0; msk <= (1 << L) - 1; msk++) n       if (msk >> i & 1 ^ 1) dp[msk] += dp[msk ^ (1 << i)]; n   for (int msk = (int)0; msk <= (1 << L) - 1; msk++) { n   } n   long long ans = (pow2[dp[0]] - 1 + MOD) % MOD; n   for (int msk = (int)1; msk <= (1 << L) - 1; msk++) { n     if (dp[msk] == 0) continue; n     if (__builtin_popcount(msk) % 2) { n       ans = (ans - pow2[dp[msk]] + 1 + MOD) % MOD; n     } else { n       ans = (ans + pow2[dp[msk]] - 1 + MOD) % MOD; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 107374182; n using namespace std; n int n, k; n int ans[100005][2]; n struct sa { n   int ma, mi; n } segTree[1000000]; n void build(int l, int r, int root) { n   if (l == r) { n     if (l != n + 1) { n       scanf( %d , &segTree[root].ma); n       segTree[root].mi = segTree[root].ma; n     } else { n       segTree[root].ma = 0; n       segTree[root].mi = INF; n     } n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, root << 1); n   build(mid + 1, r, (root << 1) | 1); n   segTree[root].ma = max(segTree[root << 1].ma, segTree[(root << 1) | 1].ma); n   segTree[root].mi = min(segTree[root << 1].mi, segTree[(root << 1) | 1].mi); n } n sa query(int L, int R, int l, int r, int root) { n   if (L <= l && r <= R) { n     return segTree[root]; n   } n   sa ans, temp; n   if (l == r) { n     temp.ma = 0; n     temp.mi = INF; n     return temp; n   } n   int mid = (l + r) >> 1; n   ans.ma = 0; n   ans.mi = INF; n   if (L <= mid) { n     temp = query(L, R, l, mid, root << 1); n     ans.ma = max(ans.ma, temp.ma); n     ans.mi = min(ans.mi, temp.mi); n   } n   if (R > mid) { n     temp = query(L, R, mid + 1, r, (root << 1) | 1); n     ans.ma = max(ans.ma, temp.ma); n     ans.mi = min(ans.mi, temp.mi); n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &k); n   { n     build(1, n, 1); n     int maxlength = 0, cnt = 0; n     sa tempx, tempy; n     for (int i = 1; i <= n; i++) { n       int l = i, r = n + 1; n       while (r > l + 1) { n         int mid = (l + r) >> 1; n         tempx = query(i, mid + 1, 1, n, 1); n         if (tempx.ma - tempx.mi > k) n           r = mid; n         else n           l = mid; n       } n       tempx = query(i, l, 1, n, 1); n       tempy = query(i, r, 1, n, 1); n       if (tempx.ma - tempx.mi <= tempy.ma - tempy.mi && n           tempy.ma - tempy.mi <= k && l < n) n         l++; n       if (maxlength < l - i + 1) { n         maxlength = l - i + 1; n         ans[0][0] = i; n         ans[0][1] = l; n         cnt = 1; n       } else if (maxlength == l - i + 1) { n         ans[cnt][0] = i; n         ans[cnt++][1] = l; n       } n     } n     printf( %d %d n , maxlength, cnt); n     for (int i = 0; i < cnt; i++) { n       printf( %d %d n , ans[i][0], ans[i][1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct two { n   long long int f, s; n }; n signed main() { n   long long int n; n   cin >> n; n   map<string, set<string> > mp; n   while (n--) { n     string str; n     cin >> str; n     for (long long int i = 0; i < str.size(); i++) { n       string tmp =   ; n       for (long long int j = i; j < str.size(); j++) { n         tmp += str[j]; n         mp[tmp].insert(str); n       } n     } n   } n   long long int q; n   cin >> q; n   while (q--) { n     string str; n     cin >> str; n     if (mp.find(str) != mp.end()) n       cout << mp[str].size() <<     << *mp[str].begin() << endl; n     else n       cout << 0 <<     <<  -  << endl; n   } n } n


#include <bits/stdc++.h> n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   for (; c >  9  || c <  0 ; c = getchar()) { n     if (c ==  - ) f = -1; n   } n   for (; c >=  0  && c <=  9 ; c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   return x * f; n } n int cnt[27][105]; n char s[105], s1[105]; n std::string sss, halfans, halfans2; n std::vector<char> ans; n std::map<std::string, int> H1[105], H2[105]; n inline char getA(std::string a, std::string b) { n   int c[30]; n   memset(c, 0, sizeof c); n   for (int i = 0; i < a.size(); i++) c[a[i] -  a ]++; n   for (int i = 0; i < b.size(); i++) c[b[i] -  a ]--; n   for (int i = 0; i < 26; i++) { n     if (c[i] > 0) return i +  a ; n   } n   return  a ; n } n inline std::string solve(int n) { n   printf( ? 1 %d n , n); n   fflush(stdout); n   for (int i = 1; i <= n * (n + 1) >> 1; i++) { n     scanf( %s , s); n     sss = std::string(s); n     std::sort(sss.begin(), sss.end()); n     H1[sss.size()][sss]++; n   } n   printf( ? 2 %d n , n); n   fflush(stdout); n   for (int i = 1; i <= n * (n - 1) >> 1; i++) { n     scanf( %s , s); n     sss = std::string(s); n     std::sort(sss.begin(), sss.end()); n     H2[sss.size()][sss]++; n   } n   for (int i = 1; i <= n; i++) { n     for (auto p : H2[i]) { n       H1[i][p.first] -= H2[i][p.first]; n     } n     for (auto p : H1[i]) { n       if (p.second > 0) { n         std::string new1 = (  ); n         for (int j = 0; j < ans.size(); j++) new1 += ans[j]; n         ans.push_back(getA(p.first, new1)); n       } n     } n   } n   std::string new1 = (  ); n   for (int j = 0; j < ans.size(); j++) new1 += ans[j]; n   return new1; n } n inline void work() { n   int n = read(); n   if (n == 1) { n     puts( ? 1 1 ); n     fflush(stdout); n     scanf( %s , s); n     printf( ! %s n , s); n     fflush(stdout); n   } else if (n == 2) { n     puts( ? 1 1 ); n     fflush(stdout); n     scanf( %s , s); n     puts( ? 2 2 ); n     fflush(stdout); n     scanf( %s , s1); n     printf( ! %s%s n , s, s1); n     fflush(stdout); n   } else { n     halfans = solve(n + 1 >> 1); n     printf( ? 1 %d n , n); n     fflush(stdout); n     for (int i = 1; i <= n * (n + 1) >> 1; i++) { n       scanf( %s , s); n       sss = std::string(s); n       for (int j = 0; j < sss.size(); j++) { n         cnt[sss[j] -  a ][sss.size()]++; n       } n     } n     halfans2 = std::string(  ); n     for (int i = 1; i <= n >> 1; i++) { n       for (int j = 0; j < 26; j++) { n         int num = cnt[j][1] - (cnt[j][i + 1] - cnt[j][i]); n         for (int k = 0; k < i; k++) { n           if (halfans[k] -  a  == j) { n             num--; n           } n         } n         for (int k = 0; k < halfans2.size(); k++) { n           if (halfans2[k] -  a  == j) { n             num--; n           } n         } n         if (num > 0) { n           halfans2 += ( a  + j); n           break; n         } n       } n     } n     for (int i = halfans2.size() - 1; ~i; i--) halfans += halfans2[i]; n     printf( ! %s n , halfans.c_str()); n     fflush(stdout); n   } n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e3 + 5, mod = 1e9 + 7; n struct p { n   int x, y; n } c[N]; n int n, D, num; n int h[N], w[N]; n int f[N][N]; n int read() { n   int x(0); n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     ; n   for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar()) n     ; n   return x; n } n void add(int x, int y) { c[++num] = (p){h[x], y}, h[x] = num; } n void dfs(int x = 1) { n   for (int i = 1; i <= n; ++i) f[x][i] = 1; n   for (int i = h[x]; i; i = c[i].x) { n     dfs(c[i].y); n     for (int j = 1; j <= n; ++j) f[x][j] = 1ll * f[x][j] * f[c[i].y][j] % mod; n   } n   for (int i = 1; i <= n; ++i) { n     f[x][i] += f[x][i - 1]; n     if (f[x][i] >= mod) f[x][i] -= mod; n   } n } n int Pow(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1, a = 1ll * a * a % mod) n     if (b & 1) ans = 1ll * ans * a % mod; n   return ans; n } n int L(int x) { n   int ans = 1; n   for (int i = 0; i <= n; ++i) n     if (x != i) n       ans = (1ll * ans * (D - i) % mod * (x - i < 0 ? -1 : 1) * n                  w[x - i < 0 ? i - x : x - i] % mod + n              mod) % n             mod; n   return ans; n } n void Solve() { n   if (n >= D) return void(printf( %d , f[1][D])); n   int ans = 0; n   for (int i = 1; i <= n; ++i) w[i] = Pow(i, mod - 2); n   for (int i = 0; i <= n; ++i) { n     ans += 1ll * f[1][i] * L(i) % mod; n     if (ans >= mod) ans -= mod; n   } n   printf( %d , ans); n } n int main() { n   n = read(), D = read(); n   for (int i = 2; i <= n; ++i) add(read(), i); n   return dfs(), Solve(), 0; n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(char c) { return string(1, c); } n string to_string(string s) { return s; } n template <class A, class B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <class T> n string to_string(T v) { n   bool f = true; n   string res =  { ; n   for (auto &x : v) { n     if (!f) { n       res +=  ,  ; n     } n     f = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void DBG() { cerr <<  ]  << endl; } n template <class H, class... T> n void DBG(H h, T... t) { n   cerr << to_string(h); n   if (sizeof...(t)) cerr <<  ,  ; n   DBG(t...); n } n string getCode(char c) { n   string res; n   if (c ==  > ) { n     res =  1000 ; n   } else if (c ==  < ) { n     res =  1001 ; n   } else if (c ==  + ) { n     res =  1010 ; n   } else if (c ==  - ) { n     res =  1011 ; n   } else if (c ==  . ) { n     res =  1100 ; n   } else if (c ==  , ) { n     res =  1101 ; n   } else if (c ==  [ ) { n     res =  1110 ; n   } else if (c ==  ] ) { n     res =  1111 ; n   } else { n     assert(false); n   } n   return res; n } n const long long mod = 1e6 + 3; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> s; n   string ss; n   for (char c : s) { n     ss += getCode(c); n   } n   long long ans = 0; n   int n = (int)(ss).size(); n   for (int i = 0; i < n; i++) { n     if (ss[i] ==  1 ) { n       ans = (ans + powmod(2, n - i - 1)) % mod; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, x, y, z; n   cin >> a >> b >> c >> x >> y >> z; n   int rest = 0, my = 0; n   a -= x; n   b -= y; n   c -= z; n   if (a > 0) n     my += (a / 2); n   else n     rest += -a; n   if (b > 0) n     my += (b / 2); n   else n     rest += -b; n   if (c > 0) n     my += (c / 2); n   else n     rest += -c; n   if (my >= rest) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, k, l, x, y, z, m, n, ans, dir, s, f, current, p, q; n pair<int, int> a; n pair<int, pair<int, int> > spies[300000]; n int main() { n   scanf( %d %d %d %d , &n, &m, &s, &f); n   for (i = 0; i < m; i++) { n     scanf( %d %d %d , &x, &y, &z); n     a = make_pair(y, z); n     spies[i] = make_pair(x, a); n   } n   if (s > f) n     dir = -1; n   else n     dir = 1; n   current = s; n   i = 0; n   j = 1; n   while (current != f) { n     a = spies[i].second; n     x = spies[i].first; n     if (x == j) { n       i++; n       y = a.first; n       z = a.second; n       p = current; n       q = current + dir; n       if ((p >= y && p <= z) || (q >= y && q <= z)) { n         printf( X ); n       } else { n         current = current + dir; n         if (dir == 1) n           printf( R ); n         else n           printf( L ); n       } n     } else { n       current = current + dir; n       if (dir == 1) n         printf( R ); n       else n         printf( L ); n     } n     j++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > E[200005]; n int U[200005], V[200005], D[200005]; n int n, m; n int vs[200005]; n queue<int> q; n int dfs(int u, int in) { n   q.push(u); n   for (int len = 0;; len++) { n     int sz = q.size(); n     if (sz == 0) break; n     in = len % 2; n     for (int i = 0; i < sz; i++) { n       u = q.front(); n       q.pop(); n       if (vs[u] != -1) { n         if (vs[u] != in) n           return false; n         else n           continue; n       } n       vs[u] = in; n       for (auto e : E[u]) { n         if (vs[e.first] == -1) n           q.push(e.first); n         else if (vs[e.first] != in ^ 1) n           return false; n       } n     } n   } n   for (int i = 0; i < m; i++) { n     if (vs[U[i]] == 0 && vs[V[i]] == 1) { n       D[i] = 1; n     } else if (vs[U[i]] == 1 && vs[V[i]] == 0) { n       D[i] = 0; n     } else n       assert(1 == 2); n   } n   return true; n } n int main() { n   memset(vs, -1, sizeof(vs)); n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &U[i], &V[i]); n     D[i] = -1; n     E[U[i]].push_back(make_pair(V[i], i)); n     E[V[i]].push_back(make_pair(U[i], i)); n   } n   if (dfs(1, 0)) { n     printf( YES n ); n     for (int i = 0; i < m; i++) printf( %d , D[i]); n     printf(  n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b); n long long nCr(long long n, long long r); n long long pow(long long b, long long n); n void read(long long a[], long long n); n void solve() { n   long long n; n   cin >> n; n   vector<long long> v; n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       v.push_back(i); n       n /= i; n       if (v.size() == 2) { n         v.push_back(n); n         break; n       } n     } n   } n   if (v.size() != 3) { n     cout <<  NO  << endl; n     return; n   } n   if (v[0] != v[1] && v[0] != v[1] && v[1] != v[2]) { n     cout <<  YES  << endl; n     cout << v[0] <<     << v[1] <<     << v[2] << endl; n   } else { n     cout <<  NO  << endl; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n long long nCr(long long n, long long r) { n   if (r > n) return 0; n   if (r * 2 > n) r = n - r; n   if (r == 0) return 1; n   long long ans = n; n   for (long long i = 2; i <= r; i++) { n     ans *= (n - i + 1); n     ans /= i; n   } n   return ans; n } n long long pow(long long b, long long n) { n   long long p = b, ans = 1; n   while (n) { n     if (n & 1) { n       ans *= p; n     } n     p *= p; n     n = n >> 1; n   } n   return ans; n } n void read(long long a[], long long n) { n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n } n


#include <bits/stdc++.h> n int main() { n   long long int k, b, n, t, i; n   scanf( %I64d %I64d %I64d %I64d , &k, &b, &n, &t); n   double x = 1; n   for (i = 0; i < n && x <= t; i++) { n     x = k * x + b; n   } n   if (x > t) { n     printf( %I64d , n - i + 1); n   } else { n     printf( %I64d , n - i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string nonz; n   int ans = 0; n   string s =   ; n   bool zr = false; n   for (int i = 0; i < n; i++) { n     string a; n     cin >> a; n     if (a ==  0 ) zr = true; n     string x = a; n     bool beauty = true; n     int zero = 0, one = 0; n     for (auto j : a) { n       if (j ==  1 ) n         one++; n       else if (j ==  0 ) n         zero++; n       else { n         beauty = false; n         nonz = x; n       } n     } n     if (one > 1) { n       beauty = false; n       nonz = x; n     } else if (beauty) { n       ans += zero; n     } n   } n   if (!zr) { n     if (nonz.size()) n       cout << nonz; n     else n       cout << 1; n     for (int i = 0; i < ans; i++) { n       cout << 0; n     } n     cout << endl; n   } else { n     cout << 0 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   cin >> n; n   cout << (n - 1) / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = false; n int n, m, k; n long long ln, lk, lm; n int b[5105], a[5105]; n void update(int x) { n   for (int i = x; i <= n; i += i & -i) { n     b[i]++; n   } n } n int query(int x) { n   int ret = 0; n   for (; x > 0; ret += b[x], x -= x & -x) n     ; n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     scanf( %d , a + i); n     a[i]++; n   } n   int tt = -((1 << 30)), cnt = 0; n   pair<int, int> pp; n   for (int i = n; i; i--) { n     memset(b, 0, sizeof b); n     for (int j = i - 1; j; j--) { n       if (a[j] > a[i]) { n         update(a[j]); n         int x = query(a[j]) - 1; n         if (x > tt) { n           cnt = 1; n           tt = x; n           pp = {i, j}; n         } else if (x == tt) n           cnt++; n       } n     } n   } n   swap(a[pp.first], a[pp.second]); n   memset(b, 0, sizeof b); n   int num = 0; n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     update(a[i]); n     num += i - query(a[i]); n   } n   printf( %d %d n , num, cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj1[1000050], adj2[1000050]; n bool vis1[1000050], vis2[1000050]; n vector<int> ans; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     adj1[u].push_back(v); n     adj2[v].push_back(u); n   } n   for (int i = n; i >= 0; i--) { n     if (!vis1[i]) { n       vis1[i] = true; n       vis2[i] = true; n       for (auto c : adj1[i]) vis1[c] = true; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (vis2[i]) { n       for (auto c : adj2[i]) { n         if (vis2[c]) vis2[i] = false; n       } n       if (vis2[i]) ans.push_back(i); n     } n   } n   printf( %d n , ans.size()); n   for (auto c : ans) printf( %d  , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c; n vector<int> ans; n int sum(int curr) { n   int sum = 0; n   while (curr > 0) { n     sum += curr % 10; n     curr = curr / 10; n   } n   return sum; n } n long long go(long long x, int n) { n   long long ans = 1; n   for (int i = 0; i < n; i++) { n     ans = ans * x; n   } n   return ans; n } n int main() { n   scanf( %d %d %d , &a, &b, &c); n   for (int i = 0; i <= 81; i++) { n     long long curr = go(i, a) * b + c; n     if (curr > 1000000000LL || curr <= 0) continue; n     if (sum(curr) == i) ans.push_back(curr); n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int a, b, c, prad[1000001]; n using namespace std; n int main() { n   long long pro = 0; n   for (int i = 1; i < 1000001; ++i) { n     for (int j = 1; j * i < 1000001; ++j) { n       ++prad[j * i]; n     } n   } n   cin >> a >> b >> c; n   for (int j = 1; j < a + 1; ++j) n     for (int ja = 1; ja < b + 1; ++ja) n       for (int ha = 1; ha < c + 1; ++ha) { n         pro += prad[ha * ja * j]; n         long long ta = pro >> 30; n         ta << 30; n         pro = pro - ta; n       } n   cout << pro; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[110000]; n int st[110000], top; n int main() { n   int n, m, i, j, k, x; n   scanf( %d , &n); n   if (n & 1) { n     puts( -1 ); n     return 0; n   } n   st[++top] = 0; n   x = n / 2; n   while (x) { n     st[++top] = x; n     vis[x] = 1; n     if (x + n >> 1 < n && !vis[x + n >> 1]) n       x = x + n >> 1; n     else n       x = x >> 1; n   } n   st[++top] = 0; n   while (top) printf( %d  , st[top--]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, j, x, xx, yy, p = 1e9 + 7, pr[1000010], nt[1000010], n                               jc[1000010], pos[1000010], mu[1000010], n                               a[1000010], num[1000010], ans; n vector<long long> v[1000010]; n int main() { n   scanf( %lld , &n); n   jc[0] = 1; n   for (i = 1; i <= n; i++) jc[i] = jc[i - 1] * i % p; n   for (i = 1; i <= n; i++) mu[i] = 1; n   for (i = 2; i <= n; i++) n     if (!v[i].size()) { n       num[n / i]++; n       pos[i] = n / i; n       for (j = i; j <= n; j += i) v[j].push_back(i), mu[j] *= i; n     } n   for (i = 1; i <= n; i++) a[mu[i]]++; n   num[1]++; n   pos[1] = 1; n   v[1].push_back(1); n   for (i = 1; i <= n; i++) { n     scanf( %lld , &x); n     if (!x) continue; n     a[mu[x]]--; n     if (v[x].size() != v[i].size()) return puts( 0 ), 0; n     for (j = 0; j < v[i].size() - 1; j++) n       if (v[x][j] != v[i][j]) return puts( 0 ), 0; n     xx = v[x].back(); n     yy = v[i].back(); n     if (pos[xx] != pos[yy]) return puts( 0 ), 0; n     if (pr[xx] && pr[xx] != yy) return puts( 0 ), 0; n     if (nt[yy] && nt[yy] != xx) return puts( 0 ), 0; n     if (!pr[xx] && !nt[yy]) num[pos[xx]]--; n     pr[xx] = yy; n     nt[yy] = xx; n   } n   ans = 1; n   for (i = 1; i <= n; i++) ans = ans * jc[a[i]] % p; n   for (i = 1; i <= n; i++) ans = ans * jc[num[i]] % p; n   printf( %lld , ans); n } n


#include<bits/stdc++.h> n #define For(i,a,b) for(register int i=(a);i<=(b);++i) n #define Rep(i,a,b) for(register int i=(a);i>=(b);--i) n using namespace std; n inline int read() n { n     char c=getchar();int x=0;bool f=0; n     for(;!isdigit(c);c=getchar())f^=!(c^45); n     for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48); n     if(f)x=-x;return x; n } n   n #define mod 998244353 n struct modint{ n     int x; n     modint(int o=0){x=o;} n     modint &operator = (int o){return x=o,*this;} n     modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;} n     modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;} n     modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;} n     modint &operator ^=(int b){ n         modint a=*this,c=1; n         for(;b;b>>=1,a*=a)if(b&1)c*=a; n         return x=c.x,*this; n     } n     modint &operator /=(modint o){return *this *=o^=mod-2;} n     modint &operator +=(int o){return x=x+o>=mod?x+o-mod:x+o,*this;} n     modint &operator -=(int o){return x=x-o<0?x-o+mod:x-o,*this;} n     modint &operator *=(int o){return x=1ll*x*o%mod,*this;} n     modint &operator /=(int o){return *this *= ((modint(o))^=mod-2);} n     template<class I>friend modint operator +(modint a,I b){return a+=b;} n     template<class I>friend modint operator -(modint a,I b){return a-=b;} n     template<class I>friend modint operator *(modint a,I b){return a*=b;} n     template<class I>friend modint operator /(modint a,I b){return a/=b;} n     friend modint operator ^(modint a,int b){return a^=b;} n     friend bool operator ==(modint a,int b){return a.x==b;} n     friend bool operator !=(modint a,int b){return a.x!=b;} n     bool operator ! () {return !x;} n     modint operator - () {return x?mod-x:0;} n }; n inline modint qpow(modint a,int b){return a^b;} n  n #define maxn 800005 n const modint G=3,Ginv=modint(1)/3; n struct poly{ n     vector<modint>a; n     modint& operator [](int i){return a[i];} n     int size(){return a.size();} n     void resize(int n){a.resize(n);} n }; n inline poly one(){poly a;a.a.push_back(1);return a;} n int rev[maxn]; n inline int ext(int n){ n  int k=0; n  while((1<<k)<n)++k;return k;  n } n inline void init(int k){ n  int n=1<<k; n  For(i,0,n-1)rev[i]=(rev[i>>1]>>1)|((i&1)<<(k-1)); n } n void ntt(poly&a,int k,int typ) n { n  int n=1<<k; n  For(i,0,n-1)if(i<rev[i])swap(a[i],a[rev[i]]);  n  for(int mid=1;mid<n;mid<<=1) n  { n   modint wn=(typ>0?G:Ginv)^((mod-1)/(mid<<1)); n   for(int r=mid<<1,j=0;j<n;j+=r){ n    modint w=1; n    for(int k=0;k<mid;++k,w=w*wn){ n     modint x=a[j+k],y=w*a[j+k+mid]; n     a[j+k]=x+y,a[j+k+mid]=x-y; n    } n   } n  } n  if(typ<0){ n   modint inv=modint(1)/n; n   For(i,0,n-1)a[i]*=inv; n  } n } n  n poly operator *(poly a,poly b) n { n  int n=(int)a.size()+(int)b.size()-1,k=ext(n); n  a.resize(1<<k),b.resize(1<<k),init(k); n  ntt(a,k,1),ntt(b,k,1); n  For(i,0,(1<<k)-1)a[i]*=b[i]; n  ntt(a,k,-1),a.resize(n);return a; n } n  n int n,k; n modint fac[maxn],ifac[maxn],iv[maxn]; n inline modint sign(int x){return (x&1)?(mod-1):(1);} n poly f,g,s,a,b; n inline void get_s(int n){ n  fac[0]=ifac[0]=1; n  For(i,1,n+n) n   fac[i]=fac[i-1]*i, n   iv[i]=modint(1)/i, n   ifac[i]=ifac[i-1]*iv[i]; n  int sz=n+1; n  a.resize(sz),b.resize(sz); n  For(i,0,n) n   a[i]=sign(i)*ifac[i], n   b[i]=(modint(i)^n)*ifac[i]; n  s=a*b; s.resize(n+1); n } n  n int main() n { n  n=read(),k=read(); n  get_s(k); n  modint res=0,C=1; n  For(i,1,k){ n   if(n-i+1==0)break; n   C*=(n-i+1),C/=i; n   res+=s[i]*fac[i]*C*qpow(n+1,n-i); n  } n  cout<<res.x; n     return 0; n }


#include<bits/stdc++.h> n using namespace std; n  n #define f first n #define s second n  n using ll = long long; n  n const int mxN = 200000; n const int lgN = 18; n const int mod = 1e9 + 7; n const ll inf = 1e15; n  n void solve() { n         int n; cin >> n; n         int a[n + 1]; n         int check = true; n         for (int i = 1; i <= n; i++) { n                 cin >> a[i]; n                 if (i > 1 && a[i] < a[i - 1]) check = false; n         } n          n         if (check) cout <<  0 ; n         else if (a[1] == 1 || a[n] == n) cout <<  1 ; n         else if (a[1] != n || a[n] != 1) cout <<  2 ; n         else cout <<  3 ; n } n  n int main() { n         /*if (fopen( input.txt ,  r )) { n                 freopen( input.txt ,  r , stdin); n                 freopen( output.txt ,  w , stdout); n         }*/ n  n         ios_base::sync_with_stdio(0); cin.tie(0); n  n         int n_test = 1; cin >> n_test; n         for (int test = 1; test <= n_test; test++) { n                 //cout <<  Case #  << test <<  :  ; n                 solve(); cout <<   n ; n         } n  n         return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100050, inf = 1e9 + 7; n struct tree { n   int l, r, mid, rx, id; n } t[N << 3]; n struct node { n   int l, r, t, id; n   bool operator<(const node &p) const { n     if (l == p.l) return id < p.id; n     return l < p.l; n   } n } g[N << 1]; n int n, m, ans[N]; n vector<int> v; n int get(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; } n void pushup(int rt) { t[rt].rx = max(t[rt << 1].rx, t[rt << 1 | 1].rx); } n void build(int l, int r, int rt) { n   int m = (l + r) >> 1; n   t[rt].l = l; n   t[rt].r = r; n   t[rt].mid = m; n   if (l == r) { n     t[rt].rx = -inf; n     return; n   } n   build(l, m, rt << 1); n   build(m + 1, r, rt << 1 | 1); n   pushup(rt); n } n void update(int pos, int id, int x, int rt) { n   if (t[rt].l == t[rt].r) { n     t[rt].rx = x; n     t[rt].id = id; n     return; n   } n   if (pos <= t[rt].mid) { n     update(pos, id, x, rt << 1); n   } n   if (pos > t[rt].mid) { n     update(pos, id, x, rt << 1 | 1); n   } n   pushup(rt); n } n int query(int pos, int x, int rt) { n   if (t[rt].rx < x) return -1; n   if (t[rt].l == t[rt].r) return t[rt].id; n   int ans = -1; n   if (pos <= t[rt].mid) { n     ans = query(pos, x, rt << 1); n     if (ans > 0) return ans; n   } n   return query(pos, x, rt << 1 | 1); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n + m; i++) { n     scanf( %d%d%d , &g[i].l, &g[i].r, &g[i].t); n     g[i].id = i; n     v.push_back(g[i].t); n   } n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   build(1, v.size(), 1); n   sort(g + 1, g + n + m + 1); n   for (int i = 1; i <= n + m; i++) { n     int pos = get(g[i].t); n     if (g[i].id <= n) { n       update(pos, g[i].id, g[i].r, 1); n     } else { n       ans[g[i].id - n] = query(pos, g[i].r, 1); n     } n   } n   for (int i = 1; i <= m; i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, a[5], ans[5], f1[19491001], cxk, f2[19491001]; n int mod1 = 19260817, mod2 = 233317; n string s; n inline int Read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n void Write(int x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) Write(x / 10); n   putchar(x % 10 +  0 ); n } n inline int HAsh1() { n   long long last = 0; n   for (int i = 1; i <= 4; ++i) { n     last = last * 13331 + a[i]; n     last %= mod1; n   } n   return last; n } n inline int HAsh2() { n   long long last = 0; n   for (int i = 1; i <= 4; ++i) { n     last = last * 13331 + a[i]; n     last %= mod2; n   } n   return last; n } n void dfs(int k, int now) { n   if (f1[HAsh1()] && f2[HAsh2()]) return; n   f1[HAsh1()] = f2[HAsh2()] = 1; n   if (cxk) return; n   if (k == n + 1) { n     int minn = 2147482647; n     for (register int i = 1; i <= 4; ++i) { n       if (minn > a[i]) minn = a[i]; n     } n     for (register int i = 1; i <= 4; ++i) { n       if (a[i] == minn) { n         ans[i] = 1; n       } n     } n     int flag = 1; n     for (register int i = 1; i <= 4; ++i) n       if (!ans[i]) flag = 0; n     if (flag) cxk = 1; n     return; n   } n   if (s[k - 1] ==  G ) { n     a[1]++; n     dfs(k + 1, 1); n   } else if (s[k - 1] ==  H ) { n     a[2]++; n     dfs(k + 1, 2); n   } else if (s[k - 1] ==  R ) { n     a[3]++; n     dfs(k + 1, 3); n   } else if (s[k - 1] ==  S ) { n     a[4]++; n     dfs(k + 1, 4); n   } else { n     int minn = 2147482647; n     for (register int i = 1; i <= 4; ++i) { n       if (minn > a[i]) minn = a[i]; n     } n     for (register int i = 1; i <= 4; ++i) { n       if (a[i] == minn) { n         int a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4]; n         a[i]++; n         dfs(k + 1, i); n         a[1] = a1, a[2] = a2, a[3] = a3, a[4] = a4; n       } n     } n   } n } n int main() { n   n = Read(); n   cin >> s; n   memset(a, 0, sizeof(a)); n   memset(ans, 0, sizeof(ans)); n   memset(f1, 0, sizeof(f1)); n   memset(f2, 0, sizeof(f2)); n   cxk = 0; n   dfs(1, 0); n   if (ans[1]) puts( Gryffindor ); n   if (ans[2]) puts( Hufflepuff ); n   if (ans[3]) puts( Ravenclaw ); n   if (ans[4]) puts( Slytherin ); n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, i, j; n string s; n int main() { n   cin >> n; n   cin >> s; n   for (i = 1; i <= n; i++) { n     if (s[i - 1] != s[i]) k++; n   } n   cout << min(k + 2, n); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > v; n   v.resize(n * m); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       v[m * (i - 1) + j - 1] = {i, j}; n     } n   } n   bool low = true; n   int l = 0, r = m * n - 1; n   while (r >= l) { n     pair<int, int> u; n     if (low) { n       u = v[l]; n       l++; n       low = false; n     } else { n       u = v[r]; n       r--; n       low = true; n     } n     cout << u.first <<     << u.second <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   for (int i = 0; i < t; ++i) { n     solve(); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,inline,unroll-loops,fast-math ) n using namespace std; n class Point { n  public: n   int x; n   int y; n   Point(int _x = 0, int _y = 0) : x(_x), y(_y) { return; } n   Point operator-(Point that) { n     return Point(this->x - that.x, this->y - that.y); n   } n   long long Cross(Point that) { n     return (long long)this->x * that.y - (long long)this->y * that.x; n   } n   int Eval(int p) { return x * p + y; } n }; n int n, f[102][20002]; n pair<int, int> q[15][20002]; n deque<Point> g[20002 << 1]; n Point h[20002 << 2]; n void InitSegment(int x, int l, int r) { n   int m; n   h[x] = Point(0, INT_MAX >> 2); n   if (l == r) return; n   m = (l + r) >> 1; n   InitSegment(x << 1, l, m); n   InitSegment(x << 1 | 1, m + 1, r); n   return; n } n void InsertSegment(int x, int l, int r, int s, int t, Point v) { n   int m, p[3], q[3]; n   if (l > t || r < s) return; n   if (l >= s && r <= t) { n     m = (l + r) >> 1; n     p[0] = v.Eval(l); n     p[1] = v.Eval(r); n     p[2] = v.Eval(m); n     q[0] = h[x].Eval(l); n     q[1] = h[x].Eval(r); n     q[2] = h[x].Eval(m); n     if (l == r) { n       if (p[0] <= q[0]) h[x] = v; n     } else { n       if (p[0] <= q[0] && p[1] <= q[1]) { n         h[x] = v; n         return; n       } n       if (p[0] > q[0] && p[1] > q[1]) return; n       if (p[0] <= q[0]) n         if (p[2] <= q[2]) { n           InsertSegment(x << 1 | 1, m + 1, r, s, t, h[x]); n           h[x] = v; n         } else n           InsertSegment(x << 1, l, m, s, t, v); n       else if (p[2] <= q[2]) { n         InsertSegment(x << 1, l, m, s, t, h[x]); n         h[x] = v; n       } else n         InsertSegment(x << 1 | 1, m + 1, r, s, t, v); n     } n     return; n   } n   m = (l + r) >> 1; n   InsertSegment(x << 1, l, m, s, t, v); n   InsertSegment(x << 1 | 1, m + 1, r, s, t, v); n   return; n } n int MinSegment(int x, int l, int r, int p) { n   int m, o; n   if (l > p || r < p) return INT_MAX >> 2; n   o = h[x].Eval(p); n   if (l == r) return o; n   m = (l + r) >> 1; n   o = min( n       o, min(MinSegment(x << 1, l, m, p), MinSegment(x << 1 | 1, m + 1, r, p))); n   return o; n } n Point Tangent(deque<Point> &f, Point v) { n   int l, m, r; n   for (l = -1, r = (signed)f.size() - 1; l + 1 < r;) { n     m = (l + r) / 2; n     if ((f[m + 1] - f[m]).Cross(v) > 0) n       l = m; n     else n       r = m; n   } n   return f[r]; n } n int MaxRMQ(int l, int r) { n   int t; n   t = 31 - __builtin_clz(r - l + 1); n   return max(q[t][l], q[t][r - (1 << t) + 1]).second; n } n int DP(int &c, int l, int r, int *f) { n   int m; n   Point t; n   deque<Point> p, q; n   if (l == r) { n     g[c].clear(); n     g[c].push_back(Point(l, f[l])); n     return c++; n   } n   m = MaxRMQ(l + 1, r); n   p.swap(g[DP(c, l, m - 1, f)]); n   t = Tangent(p, Point(1, ::q[0][m].first)); n   InsertSegment(1, 0, n, m, r, n                 Point(::q[0][m].first, t.y - t.x * ::q[0][m].first)); n   q.swap(g[DP(c, m, r, f)]); n   if (p.size() > q.size()) { n     for (; !q.empty(); q.pop_front()) { n       for (; p.size() > 1 && n              (p.back() - p[p.size() - 2]).Cross(q.front() - p.back()) < 0; n            p.pop_back()) n         ; n       p.push_back(q.front()); n     } n     g[c].swap(p); n   } else { n     for (; !p.empty(); p.pop_back()) { n       for (; q.size() > 1 && (q.front() - q[1]).Cross(p.back() - q.front()) > 0; n            q.pop_front()) n         ; n       q.push_front(p.back()); n     } n     g[c].swap(q); n   } n   return c++; n } n int main(void) { n   int k; n   int i, j, c; n   scanf( %d %d , &n, &k); n   for (i = 1; i <= n; i++) { n     scanf( %d , &q[0][i].first); n     q[0][i].second = i; n   } n   for (i = 1; i < 15; i++) n     for (j = 1; j + (1 << i) - 1 <= n; j++) n       q[i][j] = max(q[i - 1][j], q[i - 1][j + (1 << (i - 1))]); n   fill(f[0] + 1, f[0] + 20002, INT_MAX >> 2); n   for (i = 1; i <= k; i++) { n     InitSegment(1, 0, n); n     DP(c = 0, 0, n, f[i - 1]); n     for (j = 0; j <= n; j++) f[i][j] = MinSegment(1, 0, n, j); n   } n   printf( %d n , f[k][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long res = 1; n long long countn = 1; n void solve(long long a[], long long n) { n   if (n == 0) { n     res = max(countn, res); n     cout << res; n   } else if (a[n] >= a[n - 1]) { n     countn++; n     solve(a, n - 1); n   } else { n     res = max(countn, res); n     countn = 1; n     solve(a, n - 1); n   } n } n int main() { n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   solve(a, n - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   pair<int, int> a, b, c; n   int s = 0; n   cin >> a.first >> a.second >> b.first >> b.second >> c.first >> c.second; n   s = a.first * a.second + b.first * b.second + c.first * c.second; n   if ((int)sqrt(s + 0.0) * (int)sqrt(s + 0.0) != s) { n     cout << -1; n     return 0; n   } n   s = sqrt(s + 0.0); n   if (a.first < a.second) swap(a.first, a.second); n   if (b.first < b.second) swap(b.first, b.second); n   if (c.first < c.second) swap(c.first, c.second); n   char mp[] = { A ,  B ,  C }; n   if (a.first < b.first) { n     swap(a, b); n     swap(mp[0], mp[1]); n   } n   if (b.first < c.first) { n     swap(mp[1], mp[2]); n     swap(b, c); n   } n   if (a.first < b.first) { n     swap(mp[0], mp[1]); n     swap(a, b); n   } n   if (a.first == s) { n     if (b.first == c.first && a.first == b.first) { n       if (a.second + b.second + c.second == s) { n         cout << s << endl; n         for (int i = 0; i < a.second; ++i, cout << endl) n           for (int j = 0; j < a.first; ++j) cout << mp[0]; n         for (int i = 0; i < b.second; ++i, cout << endl) n           for (int j = 0; j < b.first; ++j) cout << mp[1]; n         for (int i = 0; i < c.second; ++i, cout << endl) n           for (int j = 0; j < c.first; ++j) cout << mp[2]; n       } n     } else if (b.second + c.second == a.first && a.second + b.first == s && n                b.first == c.first) { n       cout << s << endl; n       for (int i = 0; i < a.second; ++i, cout << endl) n         for (int j = 0; j < a.first; ++j) cout << mp[0]; n       for (int i = 0; i < b.first; ++i, cout << endl) n         for (int j = 0; j < s; ++j) n           if (j < b.second) n             cout << mp[1]; n           else n             cout << mp[2]; n     } else if (b.first + c.first == a.first && a.second + b.second == s && n                b.second == c.second) { n       cout << s << endl; n       for (int i = 0; i < a.second; ++i, cout << endl) n         for (int j = 0; j < a.first; ++j) cout << mp[0]; n       for (int i = 0; i < s - a.second; ++i, cout << endl) n         for (int j = 0; j < s; ++j) n           if (j < b.first) n             cout << mp[1]; n           else n             cout << mp[2]; n     } else if (b.first + c.second == a.first && a.second + b.second == s && n                b.second == c.first) { n       cout << s << endl; n       for (int i = 0; i < a.second; ++i, cout << endl) n         for (int j = 0; j < a.first; ++j) cout << mp[0]; n       for (int i = 0; i < s - a.second; ++i, cout << endl) n         for (int j = 0; j < s; ++j) n           if (j < b.first) n             cout << mp[1]; n           else n             cout << mp[2]; n     } else n       cout << -1; n   } else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void ckmax(T& x, T y) { n   x = (y > x ? y : x); n } n template <typename T> n inline void ckmin(T& x, T y) { n   x = (y < x ? y : x); n } n const int MAXN = 80; n const int INF = 0x3f3f3f3f; n int n, a[MAXN + 5], cnt[2], pos[MAXN + 5], cnt_pos, n     ans[MAXN * (MAXN - 1) / 2 + 5]; n int total, max_op; n int dp[MAXN + 5][MAXN * (MAXN - 1) / 2 + 5][MAXN + 5]; n int calc(int len) { return len * (len - 1) / 2; } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     cnt[a[i]]++; n     if (a[i] == 1) { n       pos[++cnt_pos] = i; n     } n   } n   total = calc(cnt[0]); n   max_op = calc(n); n   if (!cnt[1]) { n     for (int i = 0; i <= max_op; ++i) { n       cout << 0 <<    ; n     } n     cout << endl; n     return 0; n   } n   memset(dp, 0x3f, sizeof(dp)); n   for (int i = 1; i < n; ++i) { n     dp[i][abs(pos[1] - i)][1] = calc(i - 1); n     for (int j = 0; j <= max_op; ++j) { n       for (int k = 1; k <= i && k < cnt[1]; ++k) n         if (dp[i][j][k] != INF) { n           int rest = cnt[1] - k; n           for (int l = i + 1; l <= n - rest + 1; ++l) { n             int newj = j + abs(pos[k + 1] - l); n             if (newj <= max_op) n               ckmin(dp[l][newj][k + 1], dp[i][j][k] + calc(l - i - 1)); n           } n         } n     } n   } n   for (int i = 0; i <= max_op; ++i) ans[i] = total; n   for (int i = cnt[1]; i <= n; ++i) { n     for (int j = 0; j <= max_op; ++j) { n       ckmin(ans[j], dp[i][j][cnt[1]] + calc(n - i)); n     } n   } n   for (int i = 0; i <= max_op; ++i) { n     if (i) ckmin(ans[i], ans[i - 1]); n     cout << total - ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n inline void read(T &x) { n   T f = 1; n   x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + s -  0 ; n     s = getchar(); n   } n   x *= f; n } n const int p = 1e9 + 7; n struct Edge { n   int u, v; n } e[201]; n int head[101], ecnt; n inline void addedge(int u, int v) { n   e[++ecnt].v = v; n   e[ecnt].u = head[u]; n   head[u] = ecnt; n } n inline void add(int u, int v) { n   addedge(u, v); n   addedge(v, u); n } n int f[101][41], a, b, ans, n, k, g[101]; n inline void dfs(int x, int fa) { n   f[x][0] = 1; n   f[x][k + 1] = 1; n   for (int yny = head[x], v; yny && (v = e[yny].v); yny = e[yny].u) { n     if (v == fa) continue; n     dfs(v, x); n     memset(g, 0, sizeof(g)); n     for (int i = 0; i <= 2 * k; i++) n       for (int j = 0; j <= 2 * k; j++) { n         if ((i + j) > (2 * k)) n           (g[std::max(i, j + 1)] += 1ll * f[x][i] * f[v][j] % p) %= p; n         else n           (g[std::min(i, j + 1)] += 1ll * f[x][i] * f[v][j] % p) %= p; n       } n     for (int i = 0; i <= 2 * k; i++) f[x][i] = g[i]; n   } n } n inline void solve() { n   read(n); n   read(k); n   for (int i = 1; i <= n - 1; i++) read(a), read(b), add(a, b); n   dfs(1, 0); n   for (int i = 0; i <= k; i++) (ans += f[1][i]) %= p; n   printf( %d n , ans); n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 10000000; n long long n, a, b; n long long dp[N + 1]; n signed main() { n   cin >> n >> a >> b; n   dp[0] = 0; n   for (long long i = 1; i <= n; i++) n     if (i & 1) n       dp[i] = min(dp[i - 1] + a, dp[i + 1 >> 1] + a + b); n     else n       dp[i] = min(dp[i - 1] + a, dp[i >> 1] + b); n   cout << dp[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string stringg; n   string test =  new string for test ; n   cin >> stringg; n   int a = 5, b = 20; n   int length = stringg.size(); n   int lower = 5 * 20; n   for (int i = 1; i <= 5; i++) n     for (int j = 1; j <= 20; j++) { n       if (length <= i * j && i * j <= lower && i <= a) { n         lower = i * j; n         a = i; n         b = j; n       } n     } n   vector<vector<char> > mat(a, vector<char>(b)); n   for (int i = 0; i < 1000; i++) { n     test +=  a ; n   } n   int currentcount = 0; n   int zv = (lower - length) / a; n   int zv2asdasd = (lower - length) % a; n   bool k = 1; n   cout << a <<     << b << endl; n   for (int i = 0; i < 1000; i++) { n     test +=  aa ; n   } n   for (int i = 0; i < a; i++) { n     k = 1; n     for (int j = 0; j < b; j++) { n       if (j + 1 <= zv) n         mat[i][j] =  * ; n       else if (zv2asdasd != 0 && k) { n         mat[i][j] =  * ; n         zv2asdasd--; n         k = 0; n       } else { n         mat[i][j] = stringg[currentcount]; n         currentcount++; n       } n       cout << mat[i][j]; n     } n     cout << endl; n   } n   for (int i = 0; i < 1000; i++) { n     test +=  aa ; n   } n   cin >> k; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b); n long long int gcd(long long int a, long long int b); n int prime(int n); n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     string a; n     cin >> a; n     long long int n = a.size(); n     long long int k = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] !=  0 ) k++; n     } n     cout << k << endl; n     for (int i = 0; i < n; i++) { n       if (a[i] !=  0 ) { n         cout << a[i]; n         for (int j = i + 1; j < n; j++) cout <<  0 ; n         cout <<    ; n       } n     } n     cout << endl; n   } n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int prime(int n) { n   int flag = 1; n   for (int i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       flag = 0; n       break; n     } n   } n   return flag; n } n


#include <bits/stdc++.h> n inline long long gi() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) f ^= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? x : -x; n } n inline int pow(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = 1ll * ret * x % 1000000007; n     x = 1ll * x * x % 1000000007; n     y >>= 1; n   } n   return ret; n } n int f[1000010], S[1000010], fact[1000010], ifact[1000010]; n int main() { n   int n = gi(), k = gi(); n   fact[0] = 1; n   for (int i = 1; i <= n; ++i) fact[i] = 1ll * fact[i - 1] * i % 1000000007; n   ifact[n] = pow(fact[n], 1000000007 - 2); n   for (int i = n; i; --i) ifact[i - 1] = 1ll * ifact[i] * i % 1000000007; n   for (int i = k + 2; i <= n; ++i) n     f[i] = 1ll * fact[i - 2] * n            (0ll + i - k - 1 + S[i - 1] - S[i - k - 1] + 1000000007) % n            1000000007, n     S[i] = (S[i - 1] + 1ll * f[i] * ifact[i - 1]) % 1000000007; n   printf( %d n , 1ll * fact[n - 1] * S[n] % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[1005][1005]; n long long cols[1000005]; n long long rows[1000005]; n priority_queue<long long> pq; n int main() { n   int n, m; n   long long k, p; n   scanf( %d %d %lld %lld , &n, &m, &k, &p); n   for (int i = 0; i < n; i++) { n     long long sum = 0; n     for (int j = 0; j < m; j++) { n       scanf( %d , &mat[i][j]); n       sum += mat[i][j]; n     } n     pq.push(sum); n   } n   for (int i = 1; i <= k; i++) { n     long long val = pq.top(); n     pq.pop(); n     pq.push(val - p * m); n     rows[i] = rows[i - 1] + val; n   } n   while (pq.size()) pq.pop(); n   for (int i = 0; i < m; i++) { n     long long sum = 0; n     for (int j = 0; j < n; j++) { n       sum += mat[j][i]; n     } n     pq.push(sum); n   } n   for (int i = 1; i <= k; i++) { n     long long val = pq.top(); n     pq.pop(); n     pq.push(val - p * n); n     cols[i] = cols[i - 1] + val; n   } n   long long ans = -1e18; n   for (long long i = 0; i <= k; i++) { n     ans = max(ans, rows[i] + cols[k - i] - i * (k - i) * p); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int Maxn = 2e5 + 5, N = 4e5 + 5, Mod = 1e9 + 7, Inf = 1e18, n                     Log = 22; n int n, h[Maxn], r, mx; n vector<int> adj[Maxn]; n bool mrk[Maxn]; n bool flg[2]; n void Dfs(int v, int p) { n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (u == p) continue; n     h[u] = h[v] + 1; n     if (adj[u].size() == 1) { n       if (h[u] & 1) { n         flg[0] = 1; n       } else { n         flg[1] = 1; n       } n     } n     Dfs(u, v); n   } n   return; n } n void DFs(int v, int p) { n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (u == p) continue; n     if (adj[u].size() == 1) { n       mrk[v] = 1; n       mx--; n       continue; n     } n     DFs(u, v); n   } n   mx += mrk[v]; n   return; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n     if (1 < adj[v].size()) n       r = v; n     else if (1 < adj[u].size()) n       r = u; n   } n   Dfs(r, -1); n   if (flg[0] && flg[1]) n     cout << 3 <<    ; n   else n     cout << 1 <<    ; n   DFs(r, -1); n   cout << n - 1 + mx <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[5000007], b[5000007]; n long long mod = 1e9 + 7; n unsigned long long power(unsigned long long x, long long y, long long p) { n   unsigned long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n unsigned long long modInverse(unsigned long long n, long long p) { n   return power(n, p - 2, p); n } n unsigned long long nCrModPFermat(unsigned long long n, long long r, n                                  long long p) { n   if (n < r) return 0; n   if (r == 0) return 1; n   unsigned long long fac[n + 1]; n   fac[0] = 1; n   for (long long i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; n   return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % n          p; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     long long i, j, k, l, ans = 0, count = 0, n, m, x, y; n     bool flag = 0; n     string s; n     long long maxa = -1000000000000000, mina = 1000000000000000; n     cin >> n; n     for (i = 0; i < 2 * n; i++) cin >> a[i]; n     sort(a, a + 2 * n); n     for (i = 0; i < n; i++) ans -= a[i]; n     for (i = n; i < 2 * n; i++) ans += a[i]; n     cout << ((ans % 998244353) * (nCrModPFermat(2 * n, n, 998244353))) % n                 998244353 n          <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void albus() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n bool asec(long long a, long long b) { return a < b; } n bool desc(long long a, long long b) { return a > b; } n bool Asec_and_desc(pair<long long, pair<long long, long long>> a, n                    pair<long long, pair<long long, long long>> b) { n   if (a.first == b.first) { n     if (a.second.first == b.second.first) { n       return a.second.second < b.second.second; n     } n     return a.second.first < b.second.first; n   } n   return a.first > b.first; n } n bool compare(pair<long long, long long> a, pair<long long, long long> b) { n   return a.second < b.second; n } n typedef struct data { n   data *bit[2]; n   long long cnt = 0; n } trie; n trie *head; n void insert(long long x) { n   trie *node = head; n   for (long long i = 30; i >= 0; i--) { n     long long cur = (x >> i) & 1; n     if (node->bit[cur] == NULL) { n       node->bit[cur] = new trie(); n     } n     node = node->bit[cur]; n     node->cnt++; n   } n } n long long countXor(long long x, long long q) { n   trie *node = head; n   long long ans = 0, sum = 0; n   for (long long i = 30; i >= 0; i--) { n     long long cur = (x >> i) & 1; n     long long cur2 = (q >> i) & 1; n     if (cur2 == 0) { n       if (node->bit[!cur] != NULL) ans += node->bit[!cur]->cnt; n       if (node->bit[cur] != NULL) { n         node = node->bit[cur]; n       } else n         return ans; n     } else if (cur2 == 1) { n       if (node->bit[!cur] != NULL) n         node = node->bit[!cur]; n       else n         return ans; n     } n   } n   ans += node->cnt; n   return ans; n } n int32_t main() { n   albus(); n   head = new trie(); n   insert(0); n   long long n, q; n   cin >> n >> q; n   vector<long long> A; n   for (long long i = 1; i <= n; i++) { n     long long a; n     cin >> a; n     A.push_back(a); n   } n   long long xo = 0, final = 0; n   for (long long i = 0; i < A.size(); i++) { n     xo ^= A[i]; n     final += countXor(xo, q); n     insert(xo); n   } n   cout << final << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m = 0, b, c = 0; n   cin >> n; n   int a[1001]; n   for (int i = 0; i < n; i++) { n     cin >> b; n     if (b == 1) { n       a[m] = c; n       m += 1; n     } n     c = b; n   } n   a[m] = c; n   cout << m << endl; n   for (int i = 1; i < m + 1; i++) { n     cout << a[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 10; n long long ans[MAXN]; n int vis[MAXN], in[MAXN], worst[MAXN]; n vector<int> adj[MAXN], idx[MAXN], path; n vector<pair<int, int> > bad, qs[MAXN]; n struct BIT { n   long long b[MAXN]; n   BIT() { fill(b, b + MAXN, 0); } n   void upd(int p, long long v) { n     for (; p < MAXN; p += (p & -p)) b[p] += v; n   } n   long long sum(int p) { n     long long res = 0; n     for (; p; p -= (p & -p)) res += b[p]; n     return res; n   } n   long long qry(int l, int r) { return sum(r) - sum(l - 1); } n }; n BIT wo, cn; n void dfs(int s, int p = 0) { n   vis[s] = 1; n   in[s] = 1; n   path.push_back(s); n   for (auto v : adj[s]) { n     if (v == p) continue; n     if (!vis[v]) n       dfs(v, s); n     else if (in[v]) { n       int u, mi = MAXN, ma = -MAXN; n       do { n         u = path.back(); n         path.pop_back(); n         in[u] = 0; n         ma = max(ma, u); n         mi = min(mi, u); n       } while (u != v); n       worst[ma] = max(worst[ma], mi); n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int u = 1; u <= n; u++) n     if (!vis[u]) dfs(u); n   int cur = 0; n   for (int i = 1; i <= n; i++) { n     worst[i] = max(worst[i], cur); n     cur = worst[i]; n     wo.upd(i, worst[i]); n     idx[cur].push_back(i); n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     int l, r; n     cin >> l >> r; n     qs[l].push_back({r, i}); n   } n   for (int l = 1; l <= n; l++) { n     for (auto i : idx[l - 1]) { n       wo.upd(i, -worst[i]); n       cn.upd(i, 1); n     } n     for (auto p : qs[l]) { n       int r = p.first; n       long long d = r - l + 1; n       long long tot = (long long)r * (r + 1) / 2 - (long long)l * (l - 1) / 2; n       tot -= wo.qry(l, r); n       tot -= cn.qry(l, r) * (l - 1); n       ans[p.second] = tot; n     } n   } n   for (int i = 0; i < q; i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, pos, l, r, ind, ans = 0, ans1 = 0, ans2 = 0; n int main(void) { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> pos >> l >> r; n   if (pos < l) { n     ans += l - pos + 1; n     if (n - r > 0) { n       ans += r - l + 1; n     } n   } else if (pos > r) { n     ans += pos - r + 1; n     if (l - 1 > 0) { n       ans += r - l + 1; n     } n   } else { n     ind = pos; n     if (l - 1 > 0) { n       ans1 += ind - l + 1; n       ind = l; n     } n     if (n - r > 0) { n       ans1 += r - ind + 1; n     } n     ind = pos; n     if (n - r > 0) { n       ans2 += r - ind + 1; n       ind = r; n     } n     if (l - 1 > 0) { n       ans2 += ind - l + 1; n     } n     ans = min(ans1, ans2); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n = 0, x, y, s = 0; n   cin >> m; n   vector<int> c(m); n   for (int i = 0; i < (int)(m); ++i) { n     cin >> c[i]; n     n += c[i]; n   } n   cin >> x >> y; n   for (int i = 0; i < (int)(m); ++i) { n     s += c[i]; n     if (s <= y && s >= x && n - s <= y && n - s >= x) { n       cout << i + 2 << endl; n       return 0; n     } n   } n   cout << 0 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int x = 0, f = 1; n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n const int MAXN = 1e6 + 5; n struct Node { n   long long cnt; n   char c; n   inline bool operator==(Node &b) { return cnt == b.cnt && c == b.c; } n   inline bool operator!=(Node &b) { return cnt != b.cnt || c != b.c; } n } p1[MAXN], p2[MAXN], up1[MAXN], up2[MAXN], cpy[MAXN]; n int nxt[MAXN]; n int N, M, l1, l2; n char s[MAXN]; n bool pos[MAXN]; n inline void Unique1() { n   l1 = 0; n   long long now = 0; n   for (int i = 1; i <= N; i++) n     if (p1[i].c != p1[i - 1].c) { n       up1[l1].cnt = now; n       up1[++l1].c = p1[i].c; n       now = p1[i].cnt; n     } else n       now += p1[i].cnt; n   up1[l1].cnt = now; n   N = l1; n   memcpy(p1, up1, sizeof(p1)); n } n inline void Unique2() { n   l2 = 0; n   long long now = 0; n   for (int i = 1; i <= M; i++) n     if (p2[i].c != p2[i - 1].c) { n       up2[l2].cnt = now; n       up2[++l2].c = p2[i].c; n       now = p2[i].cnt; n     } else n       now += p2[i].cnt; n   up2[l2].cnt = now; n   M = l2; n   memcpy(p2, up2, sizeof(p2)); n } n namespace Subtask1 { n inline void Main() { n   long long ans = 0; n   for (int i = 1; i <= N; i++) n     if (p1[i].c == p2[1].c) { n       if (p1[i].cnt >= p2[1].cnt) ans += p1[i].cnt - p2[1].cnt + 1; n     } n   printf( %I64d n , ans); n } n }  // namespace Subtask1 n namespace Subtask2 { n inline void Main() { n   long long ans = 0; n   for (int i = 1; i < N; i++) n     if (p1[i].c == p2[1].c && p1[i + 1].c == p2[2].c) { n       if (p1[i].cnt >= p2[i].cnt && p1[i + 1].cnt >= p2[2].cnt) ans++; n     } n   printf( %I64d n , ans); n } n }  // namespace Subtask2 n int main() { n   N = read(); n   M = read(); n   for (int i = 1; i <= N; i++) { n     scanf( %s , s); n     int len = strlen(s), ret = 0; n     bool flg = false; n     char c; n     for (int j = 0; j < len; j++) { n       if (s[j] ==  - ) { n         flg = true; n         continue; n       } n       if (!flg) n         ret = ret * 10 + s[j] -  0 ; n       else n         c = s[j]; n     } n     p1[i] = {ret, c}; n   } n   Unique1(); n   for (int i = 1; i <= M; i++) { n     scanf( %s , s); n     int len = strlen(s), ret = 0; n     bool flg = false; n     char c; n     for (int j = 0; j < len; j++) { n       if (s[j] ==  - ) { n         flg = true; n         continue; n       } n       if (!flg) n         ret = ret * 10 + s[j] -  0 ; n       else n         c = s[j]; n     } n     p2[i] = {ret, c}; n   } n   Unique2(); n   if (M == 1) return Subtask1::Main(), 0; n   if (M == 2) return Subtask2::Main(), 0; n   int p = 0; n   for (int i = 2; i < M; i++) cpy[++p] = p2[i]; n   int j = 0; n   for (int i = 2; i <= p; i++) { n     while (j && cpy[i] != cpy[j + 1]) j = nxt[j]; n     if (cpy[i] == cpy[j + 1]) j++; n     nxt[i] = j; n   } n   for (int i = 1; i <= N; i++) { n     while (j && p1[i] != cpy[j + 1]) j = nxt[j]; n     if (p1[i] == cpy[j + 1]) j++; n     if (j == p) pos[i] = true, j = nxt[j]; n   } n   long long ans = 0; n   for (int i = p + 1; i < N; i++) n     if (pos[i]) { n       if (p1[i + 1].c == p2[M].c && p1[i + 1].cnt >= p2[M].cnt && n           p1[i - p].c == p2[1].c && p1[i - p].cnt >= p2[1].cnt) n         ans++; n     } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100000 + 100; n const int N = 100 + 100; n const int Mod = 1000000007; n int n, m; n int C[M][N], B[M][N], A[M]; n int cnt[N]; n inline void Pre() { n   C[0][0] = 1; n   for (int i = 1; i <= 100000; ++i) { n     C[i][0] = 1; n     for (int j = 1; j <= 100; ++j) n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod; n   } n } n int main() { n   Pre(); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &A[i]); n   for (int i = 1; i <= m; ++i) { n     int L, R, k; n     scanf( %d%d%d , &L, &R, &k); n     for (int j = 0; j <= k; ++j) B[L][j] = (B[L][j] + C[k][k - j]) % Mod; n     for (int j = 0; j <= k; ++j) n       B[R + 1][j] = (B[R + 1][j] - C[R + 1 - L + k][k - j]) % Mod; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= 100; ++j) cnt[j] = (cnt[j] + B[i][j]) % Mod; n     A[i] = (A[i] + cnt[0]) % Mod; n     for (int j = 0; j <= 100; ++j) cnt[j] = (cnt[j] + cnt[j + 1]) % Mod; n   } n   for (int i = 1; i <= n; ++i) printf( %d  , (A[i] + Mod) % Mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, k; n const int MOD = 1000000007; n int dp[5005][5005], sum[5005]; n int main() { n   scanf( %d%d%d%d , &n, &b, &a, &k); n   for (int i = b; i <= n; i++) sum[i] = 1; n   dp[0][b] = 1; n   for (int i = 1; i <= k; i++) { n     for (int j = 1; j <= n; j++) { n       if (j == a) continue; n       if (j > a) n         dp[i][j] = n             ((sum[n] - sum[(a + j) / 2] + MOD) % MOD - dp[i - 1][j] + MOD) % n             MOD; n       else n         dp[i][j] = (sum[(a - 1 + j) / 2] - dp[i - 1][j] + MOD) % MOD; n     } n     sum[0] = 0; n     for (int j = 1; j <= n; j++) sum[j] = (sum[j - 1] + dp[i][j]) % MOD; n   } n   printf( %d n , sum[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& p) { n   in >> p.first >> p.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> p) { n   out <<  {  << p.first <<  ,   << p.second <<  } ; n   return out; n } n template <typename T> n istream& operator>>(istream& in, vector<T>& arr) { n   for (auto& i : arr) { n     in >> i; n   } n   return in; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T> arr) { n   for (int64_t i = 0; i < (int64_t)arr.size() - 1; ++i) out << arr[i] <<    ; n   if (arr.size()) out << arr.back(); n   out <<   n ; n   return out; n } n int64_t get(int64_t n, int64_t k) { n   if (n >= 0) { n     return -n / k; n   } else { n     return abs(n) / k + (n % k != 0) * 1; n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int64_t p, k; n   cin >> p >> k; n   if (p < k) { n     cout << 1 <<   n ; n     cout << p <<   n ; n     return 0; n   } n   vector<int64_t> ans(500000); n   ans[0] = -p / k; n   for (int64_t i = 1; i < 500000; ++i) { n     ans[i] = get(ans[i - 1], k); n   } n   if (ans.back() != 0) { n     cout << -1 <<   n ; n     return 0; n   } n   while (ans.back() == 0) ans.pop_back(); n   vector<int64_t> ans2(ans.size() + 1); n   ans2[0] = ans[0] * k + p; n   for (int64_t i = 1; i < ans.size(); ++i) { n     ans2[i] = ans[i] * k + ans[i - 1]; n   } n   ans2[ans.size()] = ans.back(); n   cout << ans2.size() <<   n ; n   cout << ans2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int n, a[N], ans; n bool vis[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   int now = 1, dir = 0, tot = 0; n   while (tot < n) { n     if (dir == 0) { n       for (int i = now; i <= n; i++) n         if (!vis[i] && tot >= a[i]) { n           vis[i] = 1; n           tot++; n         } n       now = n; n       dir = 1; n       if (tot < n) ans++; n     } else { n       for (int i = now; i >= 1; i--) n         if (!vis[i] && tot >= a[i]) { n           vis[i] = 1; n           tot++; n         } n       now = 1; n       dir = 0; n       if (tot < n) ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mxn = 5e3 + 100; n long long dp[25][mxn]; n long long arr[mxn]; n long long n, k; n bool Check(long long id, long long lo, long long hi, long long rtk) { n   while (lo <= hi) { n     long long mid = (lo + hi) / 2; n     if (dp[id][mid] > rtk) n       hi = mid - 1; n     else if (dp[id][mid] < rtk) n       lo = mid + 1; n     else n       return true; n   } n   return false; n } n int main() { n   scanf( %lld%lld , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %lld , &arr[i]); n   sort(arr + 1, arr + n + 1); n   for (int i = 1; i <= n; i++) dp[1][i] = arr[i]; n   for (int i = 2; i <= k; i++) { n     for (long long j = 1; j <= n; j++) dp[i][j] = arr[j] * i; n   } n   long long q; n   scanf( %lld , &q); n   while (q--) { n     long long tk; n     scanf( %lld , &tk); n     long long mn = 1e9; n     long long ans = 0; n     for (int i = 1; i <= k; i++) { n       for (int j = 1; j <= n; j++) { n         long long tmp = dp[i][j]; n         if (tmp > tk) break; n         long long ans = i; n         long long rem = k - i; n         if (tmp == tk) n           mn = min(ans, mn); n         else { n           for (int jj = 1; jj <= rem; jj++) { n             if (Check(jj, j + 1, n, tk - tmp)) { n               mn = min(ans + jj, mn); n               break; n             } n           } n         } n       } n     } n     if (mn == 1e9) mn = -1; n     printf( %lld n , mn); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s[100010][200], ans = 0x3f3f3f3f, vis[200], sub, cunt; n char a[100010]; n bool check(int l, int r) { n   int cnt = 0; n   for (int i = 65; i <= 122; i++) n     if (s[r][i] - s[l - 1][i]) cnt++; n   return (cnt == cunt); n } n int find(int pos) { n   int l = pos, r = n; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (check(pos, mid)) n       r = mid; n     else n       l = mid + 1; n   } n   if (check(pos, r)) n     return r; n   else n     return 0x3f3f3f3f; n } n int main() { n   scanf( %d%s , &n, a + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 65; j <= 122; j++) s[i][j] = s[i - 1][j]; n     s[i][a[i]]++; n     if (!vis[a[i]]) cunt++, vis[a[i]] = 1; n   } n   for (int i = 1; i <= n; i++) { n     ans = min(ans, find(i) - i + 1); n     if (--vis[a[i]] == 0) sub++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> a; n char op[3]; n long long res; n void Gao(vector<long long> a) { n   if (a.size() == 1) { n     if (res == -1) n       res = a[0]; n     else n       res = min(res, a[0]); n     return; n   } n   vector<long long> tmp; n   for (int i = 0; i < a.size(); i++) n     for (int j = i + 1; j < a.size(); j++) { n       tmp.clear(); n       for (int k = 0; k < a.size(); k++) n         if (k != i && k != j) tmp.push_back(a[k]); n       if (op[4 - a.size()] ==  + ) n         tmp.push_back(a[i] + a[j]); n       else n         tmp.push_back(a[i] * a[j]); n       Gao(tmp); n     } n } n int main() { n   a.resize(4); n   for (int i = 0; i < 4; i++) cin >> a[i]; n   for (int i = 0; i < 3; i++) scanf(  %c , &op[i]); n   res = -1; n   Gao(a); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], n, sum = 0, tp = 0, ans = -(1 << 30); n int main() { n   scanf( %d , &n); n   for (register int i = 1; i <= n; ++i) scanf( %d , &a[i]), sum += a[i]; n   for (register int i = 1; i <= n; ++i) n     if (tp + a[i] < 0) n       tp = 0; n     else n       ans = max(ans, tp + a[i]), tp += a[i]; n   ans = max(0, ans); n   printf( %d n , max(ans * 2 - sum, sum)); n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const int MaxN = 100005, NA = -1, MaxC = 0x3F3F3F3F; n int a[MaxN]; n int b[MaxN], c[MaxN]; n bool f[MaxN]; n int d, e, g, m, n; n int main(void) { n   int i; n   while (scanf(  %d %d , &n, &m) != EOF) { n     memset(b, 0, sizeof(b)); n     memset(c, 0, sizeof(c)); n     d = e = 0; n     for (i = 1; i <= n; i++) { n       scanf(  %d , &a[i]); n       if (a[i] > 0) n         d++, b[abs(a[i])]++; n       else n         e++, c[abs(a[i])]++; n     } n     memset(f, 0, sizeof(f)); n     g = 0; n     for (i = 1; i <= n; i++) n       if (b[i] + e - c[i] == m) g++, f[i] = true; n     assert(g > 0); n     for (i = 1; i <= n; i++) n       if (a[i] > 0) { n         if (!f[abs(a[i])]) n           printf( Lie n ); n         else if (g > 1) n           printf( Not defined n ); n         else n           printf( Truth n ); n       } else { n         if (!f[abs(a[i])]) n           printf( Truth n ); n         else if (g > 1) n           printf( Not defined n ); n         else n           printf( Lie n ); n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[5000 + 1][5000 + 1]; n int f[5000 + 1][5000 + 1]; n int a[5000 + 1]; n int N, Q; n int main() { n   int i, j, len; n   cin >> N; n   for (i = 1; i <= N; i++) cin >> a[i]; n   for (i = 1; i <= N; i++) dp[i][i] = f[i][i] = a[i]; n   for (len = 2; len <= N; len++) n     for (i = 1; i <= N - len + 1; i++) { n       j = i + len - 1; n       f[i][j] = f[i + 1][j] ^ f[i][j - 1]; n       dp[i][j] = max(f[i][j], max(dp[i + 1][j], dp[i][j - 1])); n     } n   cin >> Q; n   while (Q--) { n     cin >> i >> j; n     cout << dp[i][j] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 0x3f3f3f3f; n const double PI = acos(-1.0); n const double eps = 1e-10; n const long long M = 1e9 + 7; n const long long N = 1e5 + 5; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, c, s[5]; n     cin >> a >> b >> c; n     long long minx = INF; n     for (long long i = 1; i <= 11000; i++) { n       for (long long j = i; j <= 11000; j += i) { n         for (long long k = j; k <= 11000; k += j) { n           long long tmp = abs(a - i) + abs(b - j) + abs(c - k); n           if (tmp < minx) { n             minx = tmp; n             s[1] = i; n             s[2] = j; n             s[3] = k; n           } n         } n       } n     } n     cout << minx << endl; n     cout << s[1] <<     << s[2] <<     << s[3] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int K = 12; n int n, k, q; n int arr[K][N]; n int who[N][K]; n int nrr[N * K][K]; n int siz; n vector<int> lol[N]; n int when[1 << K]; n int parent[N * K]; n bitset<1 << K> bit[N + K]; n int idx[N * K]; n int main() { n   scanf( %d %d %d , &n, &k, &q); n   for (int i = 0; i < k; ++i) { n     for (int j = 0; j < n; ++j) { n       scanf( %d , &arr[i][j]); n     } n   } n   for (int i = 0; i < n; ++i) { n     vector<int> tmp; n     for (int j = 0; j < k; ++j) { n       tmp.emplace_back(arr[j][i]); n     } n     sort(tmp.begin(), tmp.end()); n     for (int j = 0; j < k; ++j) { n       for (int x = 0; x < k; ++x) { n         nrr[siz][x] = (arr[x][i] > tmp[j]); n       } n       who[i][j] = siz; n       ++siz; n     } n     lol[i].swap(tmp); n   } n   memset(when, -1, sizeof(when)); n   for (int i = 0; i < siz; ++i) { n     int mask = 0; n     for (int j = 0; j < k; ++j) { n       if (nrr[i][j]) { n         mask |= 1 << j; n       } n     } n     parent[i] = when[mask]; n     if (when[mask] == -1) { n       when[mask] = i; n     } n   } n   int cnt = 0; n   for (int i = 0; i < siz; ++i) { n     if (parent[i] == -1) { n       idx[i] = cnt++; n     } n   } n   for (int i = 0; i < siz; ++i) { n     if (parent[i] != -1) { n       continue; n     } n     parent[i] = i; n     for (int j = 0; j < k; ++j) { n       if (nrr[i][j]) { n         bit[j].set(idx[i]); n       } n     } n   } n   int who = k; n   while (q--) { n     int type, x, y; n     scanf( %d %d %d , &type, &x, &y); n     if (type == 1) { n       bit[who] = bit[x - 1] | bit[y - 1]; n       ++who; n     } else if (type == 2) { n       bit[who] = bit[x - 1] & bit[y - 1]; n       ++who; n     } else { n       int ans = lol[y - 1][k - 1]; n       ; n       for (int j = 0; j < k; ++j) { n         int idx = ::idx[parent[::who[y - 1][j]]]; n         if (!bit[x - 1].test(idx)) { n           ans = min(ans, lol[y - 1][j]); n         } n       } n       printf( %d n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n long long int suma(long long int n) { n   long long int temp = n; n   long long int sum1 = 0; n   while (temp != 0) { n     sum1 += (temp % 10); n     temp = temp / 10; n   } n   return sum1; n } n int main() { n   fastio(); n   long long int q; n   cin >> q; n   while (q--) { n     long long int n; n     cin >> n; n     unordered_map<long long int, long long int> mp, count; n     std::vector<long long int> arr(n); n     for (long long int i = 0; i < n; i++) { n       cin >> arr[i]; n       mp[arr[i]]++; n     } n     priority_queue<long long int> pq; n     for (auto it = mp.begin(); it != mp.end(); it++) pq.push(it->second); n     long long int sum = 0; n     while (!pq.empty()) { n       if (count[pq.top()] == 0) { n         count[pq.top()]++; n         sum += pq.top(); n         pq.pop(); n       } else { n         long long int temp = pq.top(); n         pq.pop(); n         if (temp > 1) pq.push(temp - 1); n       } n     } n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int m; n   int sum = 0; n   int a[100005]; n   int rec[100005] = {0}; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < m; i++) { n     int ss = a[i]; n     if (rec[ss] == 0) { n       rec[ss]++; n       sum++; n       if (sum == n) { n         cout << 1; n         for (int j = 1; j <= n; j++) { n           rec[j]--; n           if (rec[j] == 0) { n             sum--; n           } n         } n       } else { n         cout << 0; n       } n     } else { n       rec[ss]++; n       cout << 0; n     } n   } n   return 0; n } n


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