#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000010; n struct node { n   int v, id; n } MAX[MAXN], MMAX[MAXN]; n int n, f[MAXN]; n int main() { n   scanf( %d , &n); n   n++; n   MAX[1].v = MMAX[1].v = 0; n   MAX[1].id = MMAX[1].id = 1; n   for (register int i = 2; i <= n; i++) { n     scanf( %d , &f[i]); n     MAX[i].v = 0; n     MMAX[i].v = 0; n     MAX[i].id = MMAX[i].id = i; n     int now = i; n     while (now) { n       if (MAX[f[now]].id == now) { n         if (max(MAX[now].v, MMAX[now].v + 1) > MAX[f[now]].v) { n           MAX[f[now]].v = max(MAX[now].v, MMAX[now].v + 1); n         } else { n           break; n         } n       } else if (MMAX[f[now]].id == now) { n         if (max(MAX[now].v, MMAX[now].v + 1) > MAX[f[now]].v) { n           swap(MAX[f[now]], MMAX[f[now]]); n           MAX[f[now]].v = max(MAX[now].v, MMAX[now].v + 1); n         } else if (max(MAX[now].v, MMAX[now].v + 1) > MMAX[f[now]].v) { n           MMAX[f[now]].v = max(MAX[now].v, MMAX[now].v + 1); n         } else { n           break; n         } n       } else { n         if (max(MAX[now].v, MMAX[now].v + 1) > MAX[f[now]].v) { n           swap(MAX[f[now]], MMAX[f[now]]); n           MAX[f[now]].v = max(MAX[now].v, MMAX[now].v + 1); n           MAX[f[now]].id = now; n         } else if (max(MAX[now].v, MMAX[now].v + 1) > MMAX[f[now]].v) { n           MMAX[f[now]].v = max(MAX[now].v, MMAX[now].v + 1); n           MMAX[f[now]].id = now; n         } else { n           break; n         } n       } n       now = f[now]; n     } n     printf( %d  , MAX[1].v); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n auto t1 = chrono::high_resolution_clock::now(); n bool prime[100001]; n long long int expo(long long int a, long long int b) { n   a = a % 1000000007; n   b = b % 1000000007; n   if (b == 0) return 1; n   if (b % 2) n     return ((a % 1000000007) * n             (expo((a * a) % 1000000007, b / 2) % 1000000007)) % n            1000000007; n   return expo((a * a) % 1000000007, b >> 1) % 1000000007; n } n long long int mul(long long int a, long long int b) { n   a = a % 1000000007; n   b = b % 1000000007; n   if (b == 0) return 0; n   if (b % 2) n     return ((a % 1000000007) + n             (mul((a + a) % 1000000007, b / 2) % 1000000007)) % n            1000000007; n   return mul((a + a) % 1000000007, b >> 1) % 1000000007; n } n bool ispoweroftwo(long long int x) { return x && !(x & (x - 1)); } n long long int xorcal(long long int n) { n   if (n % 4 == 0) return n; n   if (n % 4 == 1) return 1; n   if (n % 4 == 2) return n + 1; n   if (n % 4 == 3) return 0; n } n void sieve(long long int n) { n   memset(prime, true, sizeof(prime)); n   prime[0] = false; n   prime[1] = false; n   for (long long int i = 2; i * i <= n; i++) { n     if (prime[i] == true) { n       for (long long int j = i * i; j <= n; j += i) { n         prime[j] = false; n       } n     } n   } n } n bool isprime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 or n % 3 == 0) return false; n   for (long long int i = 5; i * i <= n; i += 6) { n     if (n % i == 0 or n % (i + 2) == 0) return false; n   } n   return true; n } n long long int modinverse(long long int a, long long int m) { n   long long int m0 = m; n   long long int y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long int q = a / m; n     long long int t = m; n     m = a % m; n     a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) { n     return b; n   } n   return (b % a, a); n } n long long int power(long long int A, long long int B) { n   if (A == 0) return 0; n   if (B == 0) return 1; n   long long int y; n   if (B % 2 == 0) { n     y = power(A, B / 2); n     y = (y * y) % 1000000007; n   } else { n     y = A % 1000000007; n     y = (y * power(A, B - 1) % 1000000007) % 1000000007; n   } n   return (long long int)((y + 1000000007) % 1000000007); n } n bool ispalindrome(string s) { n   string t = s; n   reverse(t.begin(), t.end()); n   if (t == s) return true; n   return false; n } n void binary(long long int a[], long long int n) { n   for (long long int i = 64 - 1; i >= 0; i--) { n     long long int k = n >> i; n     a[i] = k & (long long int)1; n   } n } n long long int countBits(long long int n) { n   if (n == 0) return 0; n   return 1 + countBits(n & (n - 1)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int a, b, c; n   cin >> a >> b >> c; n   for (long long int i = 0; i <= c; i += a) { n     if ((c - i) % b == 0) { n       cout <<  Yes  <<   n ; n       return 0; n     } n   } n   cout <<  No  <<   n ; n   auto t2 = chrono::high_resolution_clock::now(); n   auto duration = chrono::duration_cast<chrono::milliseconds>(t2 - t1).count(); n   clog <<  Time taken =   << duration * 1.0 / 1000 <<   seconds  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, l, a, x, y, sum, t[1000009], px, py; n   sum = 0; n   px = py = 0; n   cin >> n >> l >> a; n   for (int i = 0; i <= n; i++) { n     if (i == n) { n       t[i] = l - (px + py); n       break; n     } n     cin >> x >> y; n     if (i == 0) { n       t[i] = x - 0; n       px = x; n       py = y; n     } else { n       t[i] = x - (px + py); n     } n     px = x; n     py = y; n   } n   for (int i = 0; i <= n; i++) { n     if (t[i] >= a) { n       sum += (t[i] / a); n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print(vector<long long int> &v) { n   long long int n = v.size(); n   for (long long int i = 0; i < n; i++) cout << v[i] <<    ; n   cout <<   n ; n   ; n   return; n } n vector<long long int> parent(200001); n vector<long long int> size1(200001); n long long int find(long long int a) { n   if (a == parent[a]) return a; n   return parent[a] = find(parent[a]); n } n void union1(long long int a, long long int b) { n   long long int a1 = find(a); n   long long int b1 = find(b); n   if (a1 == b1) return; n   long long int t1 = size1[a1]; n   long long int t2 = size1[b1]; n   if (t1 < t2) { n     parent[a1] = b1; n     size1[b1] += size1[a1]; n   } else { n     parent[b1] = a1; n     size1[a1] += size1[b1]; n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int n; n   cin >> n; n   for (long long int i = 0; i < n + 1; i++) { n     parent[i] = i; n     size1[i] = 1; n   } n   vector<string> v(n); n   vector<vector<bool> > ar(n, vector<bool>(26, 0)); n   for (long long int i = 0; i < n; i++) { n     cin >> v[i]; n     long long int p = v[i].length(); n     for (long long int j = 0; j < p; j++) { n       ar[i][v[i][j] -  a ] = 1; n     } n   } n   for (long long int j = 0; j < 26; j++) { n     long long int prev = -1; n     for (long long int i = 0; i < n; i++) { n       if (ar[i][j] == 1) { n         if (prev == -1) n           prev = i; n         else { n           union1(prev, i); n         } n       } n     } n   } n   set<long long int> st; n   for (long long int i = 0; i < n; i++) { n     st.insert(find(i)); n   } n   cout << st.size() <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, c, b, v, d, l, n; n   cin >> a >> b >> c; n   v = c / (b * 2); n   n = c % (b * 2); n   if (n > 0) { n     v += 1; n   } n   l = n / 2 + n % 2; n   if (l == 0) { n     l = b; n   } n   if (c % 2 == 0) { n     cout << v <<     << l <<     <<  R ; n   } else n     cout << v <<     << l <<     <<  L ; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return f == -1 ? ~x + 1 : x; n } n long long n, m; n long long a[4010]; n long long ls[4010], rs[4010]; n long long stk[4010], top; n void build() { n   for (long long i = 1; i <= n; ++i) { n     while (top && a[stk[top]] > a[i]) { n       rs[stk[top]] = ls[i], ls[i] = stk[top]; n       --top; n     } n     if (top) rs[stk[top]] = i; n     stk[++top] = i; n   } n } n long long sz[4010], dp[4010][4010], tmp[4010]; n void dfs(long long x) { n   sz[x] = 1; n   dp[x][1] = (m - 1) * a[x]; n   if (ls[x]) { n     dfs(ls[x]); n     for (long long i = 0; i <= sz[x] + sz[ls[x]]; ++i) tmp[i] = 0; n     for (long long i = 0; i <= sz[x]; ++i) { n       for (long long j = 0; j <= sz[ls[x]]; ++j) { n         tmp[i + j] = n             max(tmp[i + j], dp[x][i] + dp[ls[x]][j] - 2 * i * j * a[x]); n       } n     } n     sz[x] += sz[ls[x]]; n     for (long long i = 0; i <= sz[x]; ++i) dp[x][i] = tmp[i]; n   } n   if (rs[x]) { n     dfs(rs[x]); n     for (long long i = 0; i <= sz[x] + sz[rs[x]]; ++i) tmp[i] = 0; n     for (long long i = 0; i <= sz[x]; ++i) { n       for (long long j = 0; j <= sz[rs[x]]; ++j) { n         tmp[i + j] = n             max(tmp[i + j], dp[x][i] + dp[rs[x]][j] - 2 * i * j * a[x]); n       } n     } n     sz[x] += sz[rs[x]]; n     for (long long i = 0; i <= sz[x]; ++i) dp[x][i] = tmp[i]; n   } n } n signed main() { n   n = read(), m = read(); n   for (long long i = 1; i <= n; ++i) a[i] = read(); n   build(); n   dfs(stk[1]); n   printf( %lld n , dp[stk[1]][m]); n } n


#include <bits/stdc++.h> n using namespace std; n long long solve(vector<long long> arr, int index, int currIndex) { n   if (currIndex == index) { n     return 1; n   } else { n     return 1 + solve(arr, index, arr[currIndex] - 1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long testCase; n   cin >> testCase; n   while (testCase--) { n     long long n; n     cin >> n; n     vector<long long> arr; n     for (int i = 0; i < n; i++) { n       long long j; n       cin >> j; n       arr.push_back(j); n     } n     std::vector<int> dp(n, 0); n     for (int i = 0; i < n; i++) { n       if (!dp[i]) { n         long long currentIndex = arr[i] - 1; n         long long ans = 1; n         while (currentIndex != i) { n           ans++; n           currentIndex = arr[currentIndex] - 1; n         } n         long long pos = arr[i] - 1; n         dp[arr[i] - 1] = ans; n         while (pos != i) { n           pos = arr[pos] - 1; n           dp[pos] = ans; n         } n       } n     } n     for (int i = 0; i < n; i++) { n       cout << dp[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int N, M; n vector<int> g[maxn]; n long long out[maxn], in[maxn]; n int main() { n   cin >> N >> M; n   for (int i = 1; i <= N; ++i) { n     g[i].clear(); n     out[i] = in[i] = 0; n   } n   for (int i = 1; i <= M; ++i) { n     int a, b; n     cin >> a >> b; n     int x = max(a, b), y = min(a, b); n     g[y].push_back(x); n     ++out[x]; n     ++in[y]; n   } n   long long ret = 0; n   for (int i = 1; i <= N; ++i) { n     ret += out[i] * in[i]; n   } n   int Q; n   cin >> Q; n   cout << ret << endl; n   for (int q = 1; q <= Q; ++q) { n     int x; n     cin >> x; n     ret -= out[x] * in[x]; n     for (int i : g[x]) { n       ret -= out[i] * in[i]; n       --out[i]; n       ++in[i]; n       g[i].push_back(x); n       ret += out[i] * in[i]; n       ++out[x]; n       --in[x]; n     } n     ret += out[x] * in[x]; n     g[x].clear(); n     cout << ret << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long> > neib; n struct Square { n   long long max; n   long long diff; n   long long size; n }; n vector<long long> a; n vector<Square> stat; n vector<int> color; n void dfs(int u) { n   color[u] = 1; n   stat[u].max += a[u]; n   if (neib[u].size() == 1 && u != 0) { n     stat[u].max = a[u]; n     stat[u].diff = 0; n     stat[u].size = 1; n     return; n   } n   for (auto to : neib[u]) { n     if (color[to] == 1) { n       continue; n     } n     dfs(to); n     stat[u].size += stat[to].size; n     stat[u].max += stat[to].max; n   } n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   neib.resize(n); n   a.resize(n); n   stat.resize(n); n   color.resize(n, 0); n   for (long long i = 0; i < (long long)n - 1; i++) { n     int v; n     cin >> v; n     neib[i + 1].push_back(v - 1); n     neib[v - 1].push_back(i + 1); n   } n   for (long long i = 0; i < (long long)n; i++) { n     cin >> a[i]; n   } n   if (n == 2) { n     cout << a[0] + a[1]; n   } else { n     dfs(0); n     long long ans = -1; n     for (long long i = 0; i < (long long)n; i++) { n       long long t = (stat[i].max + stat[i].size - 1) / stat[i].size; n       ans = max(ans, t); n     } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int id[105]; n int A[105]; n int B[105]; n long long dp[77][77]; n bool check(int left, int right, int pos1, int pos2) { n   for (int i = 0; i < k; i++) { n     if (A[i] == pos1) { n       if (B[i] == pos2 && id[i] == 1) return false; n       if (B[i] <= left || B[i] >= right) { n         return false; n       } n     } else if (A[i] == pos2) { n       if (B[i] == pos1 && id[i] == 1) return false; n       if (B[i] <= left || B[i] >= right) { n         return false; n       } n     } else if (B[i] == pos1) { n       if (A[i] == pos2 && id[i] == 1) return false; n       if (A[i] <= left || A[i] >= right) { n         if (id[i] == 0) return false; n       } n     } else if (B[i] == pos2) { n       if (A[i] == pos1 && id[i] == 1) return false; n       if (A[i] <= left || A[i] >= right) { n         if (id[i] == 0) return false; n       } n     } n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 0; i < k; i++) { n     string s; n     cin >> A[i] >> s >> B[i]; n     if (s ==  < ) { n       id[i] = 1; n     } else if (s ==  > ) { n       id[i] = 1; n       swap(A[i], B[i]); n     } else if (s ==  = ) { n       id[i] = 0; n     } else if (s ==  <= ) { n       id[i] = 2; n     } else { n       id[i] = 2; n       swap(A[i], B[i]); n     } n   } n   dp[0][2 * n + 1] = 1; n   for (int i = 0; i <= 2 * n - 2; i++) { n     for (int j = 2 * n + 1; j >= i + 3; j--) { n       long long x = dp[i][j]; n       if (x == 0) continue; n       if (j - i > 3 && check(i, j, i + 1, i + 2)) dp[i + 2][j] += x; n       if (check(i, j, i + 1, j - 1)) dp[i + 1][j - 1] += x; n       if (j - i > 3 && check(i, j, j - 2, j - 1)) dp[i][j - 2] += x; n     } n   } n   long long answer = 0; n   for (int i = 1; i < 2 * n; i++) { n     answer += dp[i][i + 1]; n   } n   cout << answer << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void imp() { n   cout << 0 << endl; n   exit(0); n } n int n, m, q; n int LCA[20][20]; n vector<pair<int, int> > E; n bool mat[20][20]; n int mat2[20]; n int Count[20][20]; n int Bio[20]; n int CheckDFS(int id, int p) { n   if (Bio[id] == 1) imp(); n   if (Bio[id] == 2) return Count[id][p]; n   Bio[id] = 1; n   int &r = Count[id][p]; n   for (int i = (int)(0); i < (int)(n); ++i) n     if (mat[id][i] && i != p) r += CheckDFS(i, id); n   Bio[id] = 2; n   return r; n } n map<pair<int, int>, int> M; n unordered_map<int, int> M2; n int Has[20]; n const int None = -1; n int LOW[1 << 13]; n int low(int bit) { n   if (bit == 0) return -1; n   int cnt = 0; n   while (bit % 2 == 0) ++cnt, bit >>= 1; n   return cnt; n } n bitset<20> CANT[1 << 13]; n long long dp(int id, int bit, bool ch = true) { n   static long long memo[20][1 << 13][2]; n   static bool bio[20][1 << 13][2]; n   long long &R = memo[id][bit][ch]; n   if (bio[id][bit][ch]++) return R; n   R = 0; n   if (M2.count(bit)) n     if (M2[bit] != id) return R = 0; n   if (ch) n     if ((Has[id] & bit) != Has[id]) return R = 0; n   if (CANT[bit][id]) return R = 0; n   if (bit == 0) return R = 1; n   int bit2 = (bit & mat2[id]); n   if (!bit2) { n     bit2 = bit; n     int t = LOW[bit]; n     do { n       if ((bit2 >> t) & 1) { n         if (M.count({bit, bit2})) { n           int bl = M[{bit, bit2}]; n           if (bl != None && ((bit2 >> bl) & 1)) n             R += dp(bl, bit2 ^ (1 << bl)) * dp(id, bit ^ bit2, false); n         } else n           for (int i = (int)(0); i < (int)(n); ++i) n             if ((bit2 >> i) & 1) { n               R += dp(i, bit2 ^ (1 << i)) * dp(id, bit ^ bit2, false); n             } n       } n       bit2 = (bit & (bit2 - 1)); n     } while (bit2 != bit); n     return R; n   } n   int t = (1 << LOW[bit2]); n   int bit3 = bit; n   do { n     if ((bit2 & bit3) == t) n       if (!M.count({bit, bit3}) || M[{bit, bit3}] == LOW[bit2]) { n         R += dp(LOW[bit2], bit3 ^ t) * dp(id, bit ^ bit3, false); n       } n     bit3 = (bit & (bit3 - 1)); n   } while (bit3 != bit); n   return R; n } n void AddEdge(int u, int v) { n   for (int bit = (int)(0); bit < (int)(1 << n); ++bit) { n     int bit2 = bit; n     do { n       if (((bit >> u) & 1) && ((bit >> v) & 1)) n         if (((bit2 >> u) & 1) && !((bit2 >> v) & 1)) { n           if (!M.count({bit, bit2})) M[{bit, bit2}] = v; n           if (M[{bit, bit2}] != v) M[{bit, bit2}] = None; n         } n       bit2 = (bit & (bit2 - 1)); n     } while (bit2 != bit); n   } n } n void AddLCA(int a, int b, int c) { n   if (a == b && a != c) imp(); n   if (a == b) return; n   if (c != a) Has[c] |= (1 << a); n   if (c != b) Has[c] |= (1 << b); n   for (int bit = (int)(0); bit < (int)(1 << n); ++bit) { n     if (((bit >> a) & 1) && ((bit >> b) & 1) && !((bit >> c) & 1)) { n       if (!M2.count(bit)) M2[bit] = c; n       if (M2[bit] != c) M2[bit] = None; n     } n     if (((bit >> a) & 1) && !((bit >> b) & 1)) { n       if (b != c) CANT[bit][b] = 1; n     } n     if (((bit >> b) & 1) && !((bit >> a) & 1)) { n       if (a != c) CANT[bit][a] = 1; n     } n   } n } n int main() { n   memset(LCA, -1, sizeof LCA); n   for (int i = (int)(0); i < (int)(1 << 13); ++i) LOW[i] = low(i); n   cin >> n >> m >> q; n   for (int i = (int)(0); i < (int)(m); ++i) { n     int u, v; n     cin >> u >> v, --u, --v; n     E.push_back({u, v}); n     AddEdge(u, v); n     AddEdge(v, u); n     mat[u][v] = mat[v][u] = true; n     mat2[u] |= (1 << v); n     mat2[v] |= (1 << u); n   } n   for (int i = (int)(0); i < (int)(q); ++i) { n     int a, b, c; n     cin >> a >> b >> c, --a, --b, --c; n     if (LCA[a][b] != -1 && LCA[a][b] != c) imp(); n     LCA[a][b] = LCA[b][a] = c; n     AddLCA(a, b, c); n   } n   for (int i = (int)(0); i < (int)(n); ++i) { n     memset(Bio, 0, sizeof Bio); n     CheckDFS(i, i); n   } n   cout << dp(0, (1 << n) - 2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> vc; n     vc.clear(); n     for (int i = 1; i <= sqrt(n); i++) { n       if (n / i != i) vc.push_back(n / i); n     } n     int rt = sqrt(n); n     cout << rt + vc.size() + 1 << endl; n     cout << 0 <<    ; n     for (int i = 1; i <= sqrt(n); i++) { n       cout << i <<    ; n     } n     for (int i = vc.size() - 1; i >= 0; i--) cout << vc[i] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> XandY, XXX, YYY; n int NOseg; n int query(int x, int y) { n   cout << 0 <<     << x <<     << y << endl; n   int ret; n   cin >> ret; n   return ret; n } n void SOLVE(int l, int r) { n   if (l > r) return; n   int mid = (l + r) / 2; n   int x = query(mid, mid); n   if (x == 0) n     XandY.push_back(mid), x = 1; n   else n     NOseg = mid; n   SOLVE(l, mid - x); n   SOLVE(mid + x, r); n } n void solve() { n   SOLVE(-100000000, 100000000); n   for (int k : XandY) { n     if (query(NOseg, k) == 0) YYY.push_back(k); n     if (query(k, NOseg) == 0) XXX.push_back(k); n   } n } n int main() { n   solve(); n   cout <<  1   << XXX.size() <<     << YYY.size() << endl; n   for (int x : XXX) cout << x <<    ; n   cout << endl; n   for (int y : YYY) cout << y <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n const int N = 60; n int G[N][N], n, m, top; n std::vector<int> v[500000]; n inline void out() { n   puts(  ); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       printf( %d  , G[i][j]); n     } n     puts(  ); n   } n   puts(  ); n   return; n } n inline int id(int x, int y) { return (x - 1) * m + y; } n inline bool check() { n   return G[n - 1][m - 1] == id(n - 1, m - 1) && G[n - 1][m] == id(n - 1, m) && n          G[n][m - 1] == id(n, m - 1) && G[n][m] == id(n, m); n } n inline void Cir(int x, int y, int f) { n   if (f == 0) { n     ++top; n     v[top].push_back(G[x][y]); n     v[top].push_back(G[x][y + 1]); n     v[top].push_back(G[x + 1][y + 1]); n     v[top].push_back(G[x + 1][y]); n     int t = G[x][y]; n     G[x][y] = G[x + 1][y]; n     G[x + 1][y] = G[x + 1][y + 1]; n     G[x + 1][y + 1] = G[x][y + 1]; n     G[x][y + 1] = t; n   } else { n     ++top; n     v[top].push_back(G[x][y]); n     v[top].push_back(G[x + 1][y]); n     v[top].push_back(G[x + 1][y + 1]); n     v[top].push_back(G[x][y + 1]); n     int t = G[x][y]; n     G[x][y] = G[x][y + 1]; n     G[x][y + 1] = G[x + 1][y + 1]; n     G[x + 1][y + 1] = G[x + 1][y]; n     G[x + 1][y] = t; n   } n   return; n } n inline void Move(int x, int y, int tx, int ty) { n   if (tx == n) { n     while (x != tx) { n       if (y == m) { n         Cir(x, y - 1, 0); n         x++; n       } else { n         Cir(x, y, 1); n         x++; n       } n     } n     while (y != ty) { n       Cir(x - 1, y - 1, 0); n       y--; n     } n   } else { n     if (x == n) { n       if (y == m) { n         Cir(x - 1, y - 1, 1); n         x--; n       } else { n         Cir(x - 1, y, 0); n         x--; n       } n     } n     while (y != ty) { n       if (y < ty) { n         Cir(x, y, 0); n         y++; n       } else { n         Cir(x, y - 1, 1); n         y--; n       } n     } n     while (x != tx) { n       Cir(x - 1, y, 0); n       x--; n     } n   } n   return; n } n inline void getPos(int &i, int &j, int t) { n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (G[i][j] == t) { n         return; n       } n     } n   } n   return; n } n inline void SwapDown() { n   Cir(n - 1, m - 1, 0); n   Cir(n - 1, m - 2, 0); n   ++top; n   v[top].push_back(G[n - 1][m - 2]); n   v[top].push_back(G[n][m - 2]); n   v[top].push_back(G[n][m - 1]); n   v[top].push_back(G[n][m]); n   v[top].push_back(G[n - 1][m]); n   v[top].push_back(G[n - 1][m - 1]); n   int t = G[n - 1][m - 2]; n   G[n - 1][m - 2] = G[n - 1][m - 1]; n   G[n - 1][m - 1] = G[n - 1][m]; n   G[n - 1][m] = G[n][m]; n   G[n][m] = G[n][m - 1]; n   G[n][m - 1] = G[n][m - 2]; n   G[n][m - 2] = t; n   return; n } n inline void SwapUp() { n   Cir(n - 1, m - 1, 1); n   Cir(n - 1, m - 2, 1); n   ++top; n   v[top].push_back(G[n - 1][m - 2]); n   v[top].push_back(G[n - 1][m - 1]); n   v[top].push_back(G[n - 1][m]); n   v[top].push_back(G[n][m]); n   v[top].push_back(G[n][m - 1]); n   v[top].push_back(G[n][m - 2]); n   int t = G[n - 1][m - 2]; n   G[n - 1][m - 2] = G[n][m - 2]; n   G[n][m - 2] = G[n][m - 1]; n   G[n][m - 1] = G[n][m]; n   G[n][m] = G[n - 1][m]; n   G[n - 1][m] = G[n - 1][m - 1]; n   G[n - 1][m - 1] = t; n   return; n } n int main() { 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 , &G[i][j]); n     } n   } n   int x, y; n   for (int i = 1; i <= n - 2; i++) { n     for (int j = 1; j <= m - 2; j++) { n       getPos(x, y, id(i, j)); n       Move(x, y, i, j); n     } n     getPos(x, y, i * m); n     Move(x, y, i, m - 1); n     getPos(x, y, i * m - 1); n     if (x == i && y == m) { n       Cir(x, y - 1, 0); n       Cir(x + 1, y - 1, 0); n       Cir(x, y - 1, 1); n       getPos(x, y, i * m - 1); n     } n     Move(x, y, i + 1, m - 1); n     Cir(i, m - 1, 0); n   } n   for (int i = 1; i <= m - 2; i++) { n     getPos(x, y, id(n - 1, i)); n     Move(x, y, n, i); n     getPos(x, y, id(n, i)); n     if (x == n - 1 && y == i) { n       Cir(x, y, 0); n       Cir(x, y + 1, 0); n       Cir(x, y, 1); n     } n     getPos(x, y, id(n, i)); n     Move(x, y, n, i + 1); n     Cir(n - 1, i, 0); n   } n   int xx, yy; n   getPos(x, y, id(n - 1, m - 1)); n   getPos(xx, yy, id(n - 1, m)); n   if (abs(x - xx) + abs(y - yy) != 1) { n     SwapDown(); n   } n   if (x == n && xx == n) { n     Cir(n - 1, m - 1, 0); n     Cir(n - 1, m - 1, 0); n   } n   getPos(x, y, id(n - 1, m - 1)); n   getPos(xx, yy, id(n - 1, m)); n   if (x != xx) { n     Cir(n - 1, m - 1, y == m); n   } n   if (G[n][m - 1] > G[n][m]) { n     SwapDown(); n   } n   if (G[n - 1][m - 1] > G[n - 1][m]) { n     SwapUp(); n   } n   printf( %d n , top); n   for (int i = 1; i <= top; i++) { n     int len = v[i].size(); n     printf( %d  , len); n     for (int j = 0; j < len; j++) { n       printf( %d  , v[i][j]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n string s, p; n long long bin[34][34]; n void read() { n   cin >> n; n   for (int i = 0; i <= 30; i++) { n     bin[i][0] = bin[i][i] = 1; n     for (int j = 1; j < i; j++) { n       bin[i][j] = bin[i - 1][j - 1] + bin[i - 1][j]; n     } n   } n } n void solve() { n   string res =   ; n   for (int i = 30; i >= 9; i--) { n     while (n >= bin[i][9]) { n       n -= bin[i][9]; n       res +=  b ; n     } n     res +=  a ; n   } n   cout <<  aaaaaaaa ; n   for (int i = res.length() - 1; i >= 0; i--) cout << res[i]; n   cout <<   aaaaaaaaab n ; n } n int main() { n   std::ios::sync_with_stdio(false); n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, e, f; n void read() { cin >> a >> b >> c >> d >> e >> f; } n void solve() { n   int res = 0; n   res += (a + b + f) * (a + b + f); n   res -= f * f; n   res -= b * b; n   res -= d * d; n   cout << res << endl; n } n int main() { n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[100005], B[100005]; n long long pwr[15]; n int main() { n   int n, k, i; n   long long res = 1, MOD = 1000000007, c; n   cin >> n >> k; n   for (i = 0; i < n / k; i++) cin >> A[i]; n   for (i = 0; i < n / k; i++) cin >> B[i]; n   for (i = pwr[0] = 1; i <= 10; i++) pwr[i] = 10 * pwr[i - 1]; n   for (i = 0; i < n / k; i++) { n     long long l = pwr[k] - 1; n     l = l / A[i] + 1; n     long long t1 = pwr[k - 1]; n     if (B[i]) { n       l = l - ((((B[i] + 1) * t1) - 1) / A[i] - ((B[i] * t1) - 1) / A[i]); n     } else { n       l = l - (((B[i] + 1) * t1) - 1) / A[i] - 1; n     } n     res = (res * l) % MOD; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int n = 8; n   bool ww = 1; n   int con = 0; n   while (n--) { n     string s; n     cin >> s; n     bool tb = 1; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  W ) { n         tb = 0; n       } n     } n     if (tb == 0 && ww == 1) { n       for (int i = 0; i < s.size(); i++) { n         if (s[i] ==  B ) { n           con++; n         } n       } n       ww = 0; n     } else if (tb) { n       con++; n     } n   } n   cout << con; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& x) { n   char c; n   bool f = 0; n   while ((c = getchar()) <  0  ||  9  < c) f |= (c ==  - ); n   for (x = (c ^ 48);  0  <= (c = getchar()) && c <=  9 ; n        x = (x << 1) + (x << 3) + (c ^ 48)) n     ; n   if (f) x = -x; n } n template <class T> n inline T read(const T sample) { n   T x = 0; n   char c; n   bool f = 0; n   while ((c = getchar()) <  0  ||  9  < c) f |= (c ==  - ); n   for (x = (c ^ 48);  0  <= (c = getchar()) && c <=  9 ; n        x = (x << 1) + (x << 3) + (c ^ 48)) n     ; n   return f ? -x : x; n } n template <class T> n void fwrit(const T x) { n   if (x < 0) return (void)(putchar( - ), fwrit(-x)); n   if (x > 9) fwrit(x / 10); n   putchar(x % 10 ^ 48); n } n template <class T> n inline T Max(const T x, const T y) { n   return x > y ? x : y; n } n template <class T> n inline T Min(const T x, const T y) { n   return x < y ? x : y; n } n template <class T> n inline T fab(const T x) { n   return x > 0 ? x : -x; n } n inline int gcd(const int a, const int b) { return b ? gcd(b, a % b) : a; } n inline void getInv(int inv[], const int lim, const int MOD) { n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= lim; ++i) n     inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD; n } n inline long long mulMod(const long long a, const long long b, n                         const long long mod) { n   return ((a * b - (long long)((long double)a / mod * b + 1e-8) * mod) % mod + n           mod) % n          mod; n } n const int MAXN = 100; n const int MAXM = 100; n int n, m, a[MAXN + 5][MAXM + 5]; n int x[MAXN + 5], y[MAXN + 5]; n vector<int> tx, ty; n inline void Init() { n   n = read(1), m = read(1); n   for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n     for (signed j = (1), j_end_ = (m); j <= j_end_; ++j) { n       a[i][j] = read(1); n       x[i] += a[i][j], y[j] += a[i][j]; n     } n   for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n     if (x[i] < 0) tx.push_back(i); n   for (signed j = (1), j_end_ = (m); j <= j_end_; ++j) n     if (y[j] < 0) ty.push_back(j); n } n bool opx[MAXN + 5], opy[MAXN + 5]; n inline void solve() { n   int px, py; n   while (tx.size() + ty.size() > 0) { n     while (!tx.empty()) { n       px = tx.back(); n       tx.pop_back(); n       x[px] *= -1; n       opx[px] ^= 1; n       for (signed j = (1), j_end_ = (m); j <= j_end_; ++j) { n         y[j] -= a[px][j]; n         a[px][j] *= -1; n         y[j] += a[px][j]; n       } n     } n     ty.clear(); n     for (signed j = (1), j_end_ = (m); j <= j_end_; ++j) n       if (y[j] < 0) ty.push_back(j); n     while (!ty.empty()) { n       py = ty.back(); n       ty.pop_back(); n       y[py] *= -1; n       opy[py] ^= 1; n       for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) { n         x[i] -= a[i][py]; n         a[i][py] *= -1; n         x[i] += a[i][py]; n       } n     } n     tx.clear(); n     for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n       if (x[i] < 0) tx.push_back(i); n   } n } n inline void writAns() { n   int tot = 0; n   for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n     if (opx[i]) ++tot; n   fwrit(tot); n   for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n     if (opx[i]) printf(  %d , i); n   putchar(  n ); n   tot = 0; n   for (signed j = (1), j_end_ = (m); j <= j_end_; ++j) n     if (opy[j]) ++tot; n   fwrit(tot); n   for (signed j = (1), j_end_ = (m); j <= j_end_; ++j) n     if (opy[j]) printf(  %d , j); n   putchar(  n ); n } n signed main() { n   Init(); n   solve(); n   writAns(); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define int long long n  n  void hota(){ n   int n,m,x; n   cin>>n>>m>>x; n   vector<pair<int,int>> v; n   for (int i = 0; i < n; ++i) n   { n    int x; n    cin>>x; n    v.push_back(make_pair(x,i+1)); n   } n   sort(v.begin(), v.end()); n   std::vector<int> ans(m,0); n   vector<pair<int,int>> r; n   int j=0; n   while(j<n) n   { n    int i =0; n    while(i<m && j<n) n    { n     ans[i]+=v[j].first; n     r.push_back(make_pair(v[j].second,i+1)); n     i++; n     j++; n    } n   } n   int mx = *max_element(ans.begin(), ans.end()); n   int mn = *min_element(ans.begin(), ans.end()); n   if(mx-mn>x || mn == 0) n    {cout<< NO <<endl;} n   else n   { n    sort(r.begin(), r.end()); n    cout<< YES <<endl; n    for (int i = 0; i < n; ++i) n    { n     cout<<r[i].second<<   ; n    } n    cout<<endl; n   } n  } n  n  signed main(){ n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin>>t; n   for(int tc=1;tc<=t;tc++){ n    n    hota(); n   } n  }


#include <bits/stdc++.h> n using namespace std; n vector<string> substrings; n bool cmp(pair<int, int> &a, pair<int, int> &b) { n   if (a.first < b.first) return true; n   if (a.first > b.first) return false; n   if (substrings[a.second].length() > substrings[b.second].length()) n     return true; n   return false; n } n int main() { n   int n, m, pos, dlen, maxlen = 1; n   cin >> n; n   string dummy; n   vector<pair<int, int> > v; n   for (int i = 0; i < n; i++) { n     cin >> dummy >> m; n     substrings.push_back(dummy); n     dlen = dummy.length(); n     for (int j = 0; j < m; j++) { n       cin >> pos; n       if (dlen + pos - 1 > maxlen) maxlen = dlen + pos - 1; n       v.push_back({pos - 1, i}); n     } n   } n   sort(v.begin(), v.end(), cmp); n   string s(maxlen,  a ); n   int curr_pos = v[0].first, offset, copy_size; n   s.replace(curr_pos, substrings[v[0].second].length(), n             substrings[v[0].second]); n   curr_pos += substrings[v[0].second].length(); n   for (int i = 1; i < v.size(); i++) { n     if (v[i].first + substrings[v[i].second].length() <= curr_pos) continue; n     offset = curr_pos - v[i].first; n     if (offset < 0) { n       offset = 0; n       curr_pos = v[i].first; n     } n     copy_size = substrings[v[i].second].length() - offset; n     s.replace(curr_pos, copy_size, substrings[v[i].second], offset, copy_size); n     curr_pos += copy_size; n   } n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> mp; n string get_type(string s) { n   if (mp.count(s)) return mp[s]; n   int n = s.size(); n   if (s[0] ==  &  && s[n - 1] ==  * ) return get_type(s.substr(1, n - 2)); n   if (s[0] ==  & ) { n     string ret = get_type(s.substr(1, n - 1)); n     int sz = ret.size(); n     if (ret ==  void  or ret ==  errtype ) n       return  errtype ; n     else { n       if (ret[sz - 1] ==  * ) return ret.substr(0, sz - 1); n       return s[0] + ret; n     } n   } n   if (s[n - 1] ==  * ) { n     string ret = get_type(s.substr(0, n - 1)); n     if (ret[0] ==  & ) return ret.substr(1, n - 1); n     if (ret ==  errtype ) return  errtype ; n     return get_type(s.substr(0, n - 1)) + s[n - 1]; n   } n   return  errtype ; n } n void add_type(string u, string v) { mp[v] = get_type(u); } n int main() { n   mp[ void ] =  void ; n   mp[ &void ] =  errtype ; n   mp[ errtype ] =  errtype ; n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int q; n   cin >> q; n   while (q--) { n     string fun, u, v; n     cin >> fun; n     if (fun ==  typedef ) { n       cin >> u >> v; n       add_type(u, v); n     } else { n       cin >> u; n       cout << get_type(u) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 8; n int main() { n   char r1, r2; n   int x1, x2; n   while (4 == scanf(  %c%d %c%d , &r1, &x1, &r2, &x2)) { n     int y1 = r1 -  a ; n     int y2 = r2 -  a ; n     --x1; n     --x2; n     int ans = 0; n     for (int y = 0; y < (int)(N); y++) n       for (int x = 0; x < (int)(N); x++) { n         if (y1 == y || x1 == x) continue; n         int dx1 = abs(x1 - x), dy1 = abs(y1 - y); n         int dx2 = abs(x2 - x), dy2 = abs(y2 - y); n         if (dx1 > dy1) swap(dx1, dy1); n         if (dx2 > dy2) swap(dx2, dy2); n         if (dx1 == 1 && dy1 == 2) continue; n         if (dx2 == 1 && dy2 == 2) continue; n         if (dx2 == 0 && dy2 == 0) continue; n         ++ans; n       } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int N = 1e5 + 2; n const int M = 1 << 19; n const int inf = 2e9; n const long long linf = 1e18; n long long n, m, k; n int ok(long long mid) { n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += min(m, mid / i); n   } n   return ans >= k; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   long long l = 1, r = n * m, ans = 0; n   while (l <= r) { n     long long mid = l + r >> 1; n     if (ok(mid)) { n       r = mid - 1; n       ans = mid; n     } else n       l = mid + 1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double pi = M_PI; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     n = n * 2; n     double angle = ((n - 2) * M_PI) / (2 * n); n     cout << fixed << setprecision(9) << tan(angle); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tc; n   cin >> tc; n   while (tc--) { n     int n; n     cin >> n; n     priority_queue<pair<int, int> > pq; n     for (int i = 0; i < n; i++) { n       int ai; n       cin >> ai; n       pq.push(pair<int, int>(ai, i)); n     } n     int turn = 0; n     while (not pq.empty()) { n       auto pp = pq.top(); n       pq.pop(); n       turn = 1 - turn; n       if (pq.empty()) break; n       auto qq = pq.top(); n       pq.pop(); n       turn = 1 - turn; n       pp.first -= 1; n       qq.first -= 1; n       if (pp.first != 0) pq.push(pp); n       if (qq.first != 0) pq.push(qq); n     } n     if (turn == 0) n       cout <<  HL n ; n     else n       cout <<  T n ; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 2e5 + 5; n void solve() { n   long long int n; n   cin >> n; n   long long int k = sqrt(n); n   long long int sum = 0; n   if (n == k * k) { n     cout << 4 * k <<   n ; n     return; n   } n   sum = sum + k * 4; n   n = n - (k * k); n   if (n > (k)) { n     sum = sum + (n / (k)) * 2; n     if (n % k != 0) sum = sum + 2; n   } else { n     sum = sum + 2; n   } n   cout << sum <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n vector<int> g[N], ans; n bool vis[N]; n int x[N]; n void Set(int u) { n   x[u] ^= 1; n   ans.push_back(u); n } n void dfs(int u) { n   Set(u); n   vis[u] = 1; n   for (int i = 0; i < g[u].size(); ++i) { n     int v = g[u][i]; n     if (vis[v]) continue; n     dfs(v); n     Set(u); n     if (x[v]) { n       Set(v); n       Set(u); n     } n   } n } n int main() { n   int n, m, u, v, r = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &x[i]); n     if (x[i]) r = i; n   } n   dfs(r); n   int f = 0; n   if (x[r]) x[r] = 0, f = 1; n   bool ok = true; n   for (int i = 1; i <= n; ++i) n     if (x[i]) { n       ok = false; n       break; n     } n   if (!ok) n     puts( -1 ); n   else { n     printf( %d n , ans.size() - f); n     for (int i = 0; i < ans.size() - f; ++i) printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int T_DATA = 100000; n const int N = 100000; n int T; n int a, b; n char mp[N + 10]; n int n, m; n int l[N + 10], r[N + 10]; n int dis[N + 10]; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d %d , &a, &b); n     scanf( %s , mp + 1); n     n = strlen(mp + 1); n     m = 0; n     for (int i = 1, j; i <= n; i = j + 1) { n       while (i <= n && mp[i] !=  1 ) ++i; n       if (i > n) break; n       for (j = i; j < n && mp[j + 1] ==  1 ; ++j) n         ; n       ++m; n       l[m] = i, r[m] = j; n     } n     if (m == 0) { n       printf( 0 n ); n       continue; n     } n     if (m == 1) { n       printf( %d n , a); n       continue; n     } n     for (int i = 1; i <= m - 1; ++i) dis[i] = l[i + 1] - r[i] - 1; n     std::sort(dis + 1, dis + m); n     int ans = a * m; n     for (int ind = 1; ind <= m - 1; ++ind) { n       if (dis[ind] * b >= a) continue; n       ans -= a - dis[ind] * b; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os <<  (  << p.first <<  ,   << p.second <<  ) ; } n template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os <<  { ; string sep; for (const T &x : v) os << sep << x, sep =  ,  ; return os <<  } ; } n void dbg_out() { cerr << endl; } n template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr <<     << H; dbg_out(T...); } n #ifdef WA_DEBUG n #define dbg(...) cerr <<  (  << #__VA_ARGS__ <<  ): , dbg_out(__VA_ARGS__) n #else n #define dbg(...) n #endif n  n using ll  = long long; n using ull = unsigned long long; n #define pb push_back n #define fi first n #define se second n #define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++) n #define per(i,a,b) for(int i=int(a);i>=(int)(b);i--) n const int mod  = 1e9+7; n const int inf  = 0x3f3f3f3f; n const int maxn = 1e5+10; n int main() { n #ifndef WA_DEBUG n     ios::sync_with_stdio(false);cin.tie(nullptr); n #endif n     int _; n     cin>>_; n     while(_--) { n         int n; n         cin>>n;  n         string a,b,c; n         cin>>a>>b>>c; n         int lena=0,lenb=0,lenc=0; n         rep(i,1,3*n) { n             vector<int> cnt(2,0); n             if(lena<2*n) cnt[a[lena]- 0 ]++; n             if(lenb<2*n) cnt[b[lenb]- 0 ]++; n             if(lenc<2*n) cnt[c[lenc]- 0 ]++; n             if(cnt[0]>cnt[1]) { n                 cout<<0; n                 if(lena<2*n&&a[lena]== 0 ) lena++; n                 if(lenb<2*n&&b[lenb]== 0 ) lenb++; n                 if(lenc<2*n&&c[lenc]== 0 ) lenc++; n             } n             else { n                 cout<<1; n                 if(lena<2*n&&a[lena]== 1 ) lena++; n                 if(lenb<2*n&&b[lenb]== 1 ) lenb++; n                 if(lenc<2*n&&c[lenc]== 1 ) lenc++; n             } n             if(lena==2*n||lenb==2*n||lenc==2*n) { n                 break; n             } n         } n         if(lena==2*n) { n             if(lenb>lenc) { n                 while(lenb<2*n) cout<<b[lenb++]; n             } n             else { n                 while(lenc<2*n) cout<<c[lenc++]; n             } n         } n         else if(lenb==2*n) { n             if(lena>lenc) { n                 while(lena<2*n) cout<<a[lena++]; n             } n             else { n                 while(lenc<2*n) cout<<c[lenc++]; n             } n         } n         else { n             if(lena>lenb) { n                 while(lena<2*n) cout<<a[lena++]; n             } n             else { n                 while(lenb<2*n) cout<<b[lenb++]; n             } n         } n         cout<<  n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int NumberOfJus; n double n; n double sum; n int main() { n   cin >> NumberOfJus; n   for (int i = 0; i < NumberOfJus; i++) { n     cin >> n; n     sum += n / NumberOfJus; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxbuff = 1 << 17; n char ibuff[maxbuff], obuff[maxbuff]; n inline char Getchar() { n   static char *p1 = ibuff, *p2 = ibuff; n   if (p1 == p2) { n     p1 = ibuff; n     p2 = ibuff + fread(ibuff, 1, maxbuff, stdin); n     if (p1 == p2) return -1; n   } n   return *p1++; n } n int optr = 0; n inline void Putchar(char c) { n   if (optr == maxbuff) { n     fwrite(obuff, 1, optr, stdout); n     optr = 0; n   } n   obuff[optr++] = c; n } n inline void Flush() { fwrite(obuff, 1, optr, stdout); } n template <class T> n inline int read(T &x) { n   x = 0; n   char c = Getchar(); n   if (c == EOF) return -1; n   for (; c <  0  || c >  9 ; c = Getchar()) n     ; n   for (; c >=  0  && c <=  9 ; c = Getchar()) x = x * 10 + c -  0 ; n   return 0; n } n template <class T> n inline void write(const T &x, int tag = 0) { n   if (!x) { n     if (!tag) Putchar(x +  0 ); n     return; n   } n   write(x / 10, 1); n   Putchar(x % 10 +  0 ); n } n template <class T> n inline void writeln(const T &x) { n   write(x); n   Putchar(  n ); n } n template <class T> n inline void writesp(const T &x) { n   write(x); n   Putchar(   ); n } n const int N = 4e6 + 19; n long long num[N], A[N]; n int cnt, fac[N]; n map<long long, int> mp1, mp2; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n void solve(int n, long long x, long long y) { n   long long t = y; n   for (long long i = 2; i <= 1000000 && i * i <= y && i <= t; ++i) { n     if (t % i == 0) { n       while (t % i == 0) t /= i; n       fac[cnt++] = i; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (A[i] % x) continue; n     long long tmp = gcd(A[i] / x, y / x); n     int msk = 0; n     for (int j = 0; j < cnt; ++j) msk |= (tmp % fac[j] == 0) << j; n     ++num[msk]; n   } n   for (int i = 0; i < cnt; ++i) n     for (int j = 0; j < (1 << cnt); ++j) n       if ((j & (1 << i)) == 0) num[j | (1 << i)] += num[j]; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     if (y % A[i]) continue; n     long long tmp = y / A[i]; n     int msk = 0; n     for (int j = 0; j < cnt; ++j) msk |= (tmp % fac[j] == 0) << j; n     ans += num[((1 << cnt) - 1) ^ msk]; n   } n   writeln(ans); n } n signed main() { n   long long n, x, y; n   read(n); n   read(x); n   read(y); n   if (y % x) { n     puts( 0 ); n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     read(A[i]); n     if (A[i] % x == 0) ++mp1[gcd(A[i] / x, y / x)]; n     if (y % A[i] == 0) ++mp2[y / A[i]]; n   } n   if (mp1.size() < 5000 && mp2.size() < 5000) { n     long long ans = 0; n     for (auto u : mp1) n       for (auto v : mp2) n         if (gcd(u.first, v.first) == 1) ans += 1ll * u.second * v.second; n     writeln(ans); n   } else n     solve(n, x, y); n   Flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> a[1000000]; n long long ans = 0; n bool check(long long k, pair<long long, long long> l, n            pair<long long, long long> r) { n   long long cur = abs(k - l.second) + abs(k - r.second); n   if (cur <= r.first - l.first) { n     return true; n   } n   return false; n } n int main() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= m; ++i) { n     cin >> a[i].first >> a[i].second; n     ans = max(ans, a[i].second * 1LL); n   } n   if (a[m].first != n) { n     a[m + 1] = make_pair(n, a[m].second + n - a[m].first); n     ++m; n     ans = max(ans, a[m].second); n   } n   if (a[1].first != 1) { n     for (int i = m + 1; i >= 2; --i) { n       a[i] = a[i - 1]; n     } n     a[1] = make_pair(1, a[2].second + a[2].first - 1); n     ans = max(ans, a[1].second); n     ++m; n   } n   for (long long i = 1; i <= m - 1; ++i) { n     if (abs(a[i + 1].second - a[i].second) > a[i + 1].first - a[i].first) { n       return 0 * printf( IMPOSSIBLE ); n     } n     long long bot = min(a[i].second, a[i + 1].second); n     long long top = 22814882281488; n     while (top > bot + 1) { n       long long mid = (bot + top) / 2; n       if (check(mid, a[i], a[i + 1])) { n         bot = mid; n       } else { n         top = mid; n       } n     } n     ans = max(ans, bot); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int key[300005], dia[300005], m1[300005], m2[300005], dp1[300005], n     dp2[300005]; n vector<long long int> kv[300005], adj[300005]; n bool mrk[300005]; n inline void dfs(long long int x, long long int pr, long long int root) { n   if (mrk[x]) return; n   mrk[x] = 1; n   key[x] = root; n   if (x != root) kv[root].push_back(x); n   for (auto j : adj[x]) { n     if (j == pr) continue; n     if (mrk[j]) continue; n     dfs(j, x, root); n     if (dp1[j] >= m1[x]) { n       m2[x] = m1[x]; n       m1[x] = dp1[j]; n     } else if (dp1[j] >= m2[x]) n       m2[x] = dp1[j]; n   } n   dp1[x] = m1[x] + 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int n, m, q; n   cin >> n >> m >> q; n   for (long long int i = 1; i <= n; i++) { n     m1[i] = m2[i] = -1; n     key[i] = i; n     kv[i].push_back(i); n   } n   for (long long int i = 0; i < m; i++) { n     long long int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   for (long long int i = 1; i <= n; i++) { n     if (!mrk[i]) dfs(i, -1, i); n   } n   long long int mx = 0; n   for (long long int i = 1; i <= n; i++) { n     dp2[i] = m1[i] + m2[i] + 2; n     mx = max(dp1[i], dp2[i]); n     dia[key[i]] = max(dia[key[i]], mx); n   } n   while (q--) { n     long long int ch; n     cin >> ch; n     if (ch == 1) { n       long long int x; n       cin >> x; n       cout << dia[key[x]] <<   n ; n       continue; n     } n     long long int x, y; n     cin >> x >> y; n     if (key[x] != key[y]) { n       if (kv[key[x]].size() < kv[key[y]].size()) swap(x, y); n       long long int xx = ceil(dia[key[x]] / 2.0) + ceil(dia[key[y]] / 2.0) + 1; n       dia[key[x]] = max(max(dia[key[x]], dia[key[y]]), xx); n       long long int z = key[y]; n       for (auto j : kv[z]) { n         kv[key[x]].push_back(j); n         key[j] = key[x]; n       } n       kv[z].clear(); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1010, maxm = 1 << 16, INF = 1e9; n int n, dp[2][maxm], a[5], cMask[maxn]; n vector<int> optionalMasks[5]; n string s[4]; n int addColumn(int column, int mask) { n   return (mask >> 4) | (cMask[column] << 12); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= 4; i++) scanf( %d , &a[i]); n   for (int i = 0; i < 4; i++) cin >> s[i]; n   for (int sz = 1; sz <= 4; sz++) { n     for (int i = 0; i + sz <= 4; i++) { n       int mask = maxm - 1; n       for (int c = 0; c < sz; c++) { n         for (int r = i; r < i + sz; r++) { n           mask ^= 1 << (r + c * 4); n         } n       } n       optionalMasks[sz].push_back(mask); n     } n   } n   for (int i = 0; i < n; i++) { n     int mask = 0; n     for (int j = 0; j < 4; j++) mask |= (s[j][i] ==  * ) << j; n     cMask[i] = mask; n   } n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < maxm; j++) dp[i][j] = INF; n   int mask = 0, cur = 0; n   for (int i = 0; i < 4; i++) mask = addColumn(i, mask); n   dp[cur][mask] = 0; n   for (int i = 0; i < n; i++) { n     int nxt = !cur; n     for (int j = maxm - 1; j >= 0; j--) { n       if (dp[cur][j] < INF) { n         if (!(j & 15)) { n           int nxtJ = addColumn(i + 4, j); n           dp[nxt][nxtJ] = min(dp[nxt][nxtJ], dp[cur][j]); n         } n         for (int sz = 1; sz <= 4 && i + sz <= n; sz++) { n           for (int k = 0; k < optionalMasks[sz].size(); k++) { n             int nxtJ = j & optionalMasks[sz][k]; n             dp[cur][nxtJ] = min(dp[cur][nxtJ], dp[cur][j] + a[sz]); n           } n         } n       } n       dp[cur][j] = INF; n     } n     cur = !cur; n   } n   printf( %d n , dp[cur][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 100000; n vector<pair<int, int> > g[N + 1]; n vector<int> imp; n long long cl[42][N + 1]; n int dp[N + 1], n; n pair<int, long long> pa[N + 1][17]; n bool vis[N + 1]; n void pl(int v = 1, int p = 0, int w = 0) { n   dp[v] = dp[p] + 1; n   vis[v] = true; n   pa[v][0] = make_pair(p, w); n   int k = 1, z; n   while (k < 17 && (z = pa[v][k - 1].first)) n     pa[v][k] = make_pair(pa[z][k - 1].first, n                          pa[v][k - 1].second + pa[z][k - 1].second), n     ++k; n   for (int i = 0; i < (int)(g[v].size()); ++i) { n     int u = g[v][i].first; n     if (u == p) continue; n     if (vis[u]) n       imp.push_back(v), imp.push_back(u); n     else n       pl(u, v, g[v][i].second); n   } n } n long long dis(int a, int b) { n   if (dp[a] < dp[b]) swap(a, b); n   int d = dp[a] - dp[b], j = 0; n   long long an = 0; n   while (d) { n     if (d & 1) an += pa[a][j].second, a = pa[a][j].first; n     d >>= 1; n     ++j; n   } n   if (a == b) return an; n   for (int i = 16; i >= 0; --i) n     if (pa[a][i].first != pa[b][i].first) n       an += pa[a][i].second + pa[b][i].second, a = pa[a][i].first, n                                                b = pa[b][i].first; n   return an + pa[a][0].second + pa[b][0].second; n } n void bfs(int v, long long dis[]) { n   for (int i = 1; i < (int)(n + 1); ++i) dis[i] = 1e18; n   priority_queue<pair<long long, int> > q; n   q.push(make_pair(0, v)); n   dis[v] = 0; n   while (!q.empty()) { n     int v = q.top().second; n     long long d = -q.top().first; n     q.pop(); n     for (int i = 0; i < (int)(g[v].size()); ++i) { n       int u = g[v][i].first; n       long long nd = d + g[v][i].second; n       if (dis[u] > nd) { n         dis[u] = nd; n         q.push(make_pair(-nd, u)); n       } n     } n   } n } n int main() { n   int m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (int)(m); ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     g[a].push_back(make_pair(b, c)); n     g[b].push_back(make_pair(a, c)); n   } n   pl(); n   sort(imp.begin(), imp.end()); n   imp.resize(unique(imp.begin(), imp.end()) - imp.begin()); n   for (int i = 0; i < (int)(imp.size()); ++i) bfs(imp[i], cl[i]); n   int q; n   scanf( %d , &q); n   while (q--) { n     int a, b; n     scanf( %d%d , &a, &b); n     long long an = dis(a, b); n     for (int i = 0; i < (int)(imp.size()); ++i) n       for (int j = 0; j < (int)(imp.size()); ++j) n         an = min(an, cl[i][a] + cl[i][imp[j]] + cl[j][b]); n     printf( %lld n , an); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int n, m; n int low[maxn], dfn[maxn], v[maxn << 1], A[maxn]; n int nxt[maxn << 1], head[maxn], belong[maxn], a[maxn], vis[maxn << 1]; n stack<int> s; n int indexs = 0, cnt = 0; n int ans = 0; n void add_edge(int x, int y) { n   static int N = 1; n   N++; n   v[N] = y; n   nxt[N] = head[x]; n   head[x] = N; n } n void tarjan(int x, int fa) { n   dfn[x] = low[x] = ++indexs; n   for (int i = head[x]; i != -1; i = nxt[i]) { n     if (vis[i]) continue; n     vis[i] = vis[i ^ 1] = 1; n     s.push(i); n     if (!dfn[v[i]]) { n       tarjan(v[i], x); n       low[x] = min(low[x], low[v[i]]); n       if (low[v[i]] >= dfn[x]) { n         cnt++; n         int num = 0, bcnt = 0; n         while (1) { n           int tmp = s.top(); n           s.pop(); n           if (belong[v[tmp]] != cnt) belong[v[tmp]] = cnt, num++; n           if (belong[v[tmp ^ 1]] != cnt) belong[v[tmp ^ 1]] = cnt, num++; n           a[++bcnt] = tmp; n           if (tmp == i) break; n         } n         if (num == bcnt) n           for (int i = 1; i <= bcnt; i++) A[++ans] = a[i]; n       } n     } else n       low[x] = min(low[x], dfn[v[i]]); n   } n } n int main() { n   memset(head, -1, sizeof(head)); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add_edge(x, y); n     add_edge(y, x); n   } n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) tarjan(i, 0); n   sort(A + 1, A + 1 + ans); n   printf( %d n , ans); n   for (int i = 1; i <= ans; i++) cout << (A[i] >> 1) <<    ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long int inf = INT_MAX; n const long long int mod = 1000000007; 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 long long int lcm(long long int a, long long int b) { n   return (a / gcd(a, b)) * b; n } n int factorial(long long int n) { n   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); n } n long long int sm(long long int n) { n   long long int ans = n * (n + 1) / 2; n   return ans; n } n bool isprime(long long int x) { n   for (long long int i = 2; i * i <= x; i++) { n     if (x % i == 0) return 0; n   } n   return 1; n } n bool perfectSquare(long double x) { n   long double sr = sqrt(x); n   return ((sr - floor(sr)) == 0); n } n void solve() { n   int64_t w, h, i; n   cin >> w >> h; n   int64_t ans1 = 0, ans2 = 0; n   for (i = 2; i <= w; i += 2) ans1 += (w - i + 1); n   for (i = 2; i <= h; i += 2) ans2 += (h - i + 1); n   cout << ans1 * ans2 <<   n ; n   ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long int test_case = 1; n   while (test_case--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long b = 0, s = 0, c = 0; n long long nb, ns, nc; n long long pb, ps, pc; n long long r; n bool isPoss(long long m) { n   long long rnb = b * m - nb < 0 ? 0 : b * m - nb; n   long long rns = s * m - ns < 0 ? 0 : s * m - ns; n   long long rnc = c * m - nc < 0 ? 0 : c * m - nc; n   long long rs = rnb * pb + rns * ps + rnc * pc; n   if (rs <= r && rs >= 0) { n     return true; n   } n   return false; n } n int main() { n   string st; n   cin >> st; n   long long n = st.size(); n   for (long long i = 0; i < n; i++) { n     if (st[i] ==  B ) { n       b++; n     } n     if (st[i] ==  S ) { n       s++; n     } n     if (st[i] ==  C ) { n       c++; n     } n   } n   cin >> nb >> ns >> nc; n   cin >> pb >> ps >> pc; n   cin >> r; n   long long l = 0; n   long long h = 1e16; n   long long m = (h + l) / 2; n   int flag = 0; n   while (l <= h) { n     m = (h + l) / 2; n     if (isPoss(m)) { n       if (!isPoss(m + 1)) { n         flag = 1; n         cout << m << endl; n         break; n       } else { n         l = m + 1; n       } n     } else { n       h = m - 1; n     } n   } n   if (flag == 0) cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 23, M = 2e4 + 1e3 + 7, P = 998244353; n int n; n int cnt[N][26], pw[M]; n char s[M]; n int f[1 << N]; n int mn[26]; n int main() { n   pw[0] = 1; n   for (int i = 1; i < M; i++) pw[i] = pw[i - 1] * 2 % P; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , s); n     int m = strlen(s); n     for (int j = 0; j < m; j++) cnt[i][s[j] -  a ]++; n   } n   for (int S = 1; S < (1 << n); S++) { n     for (int j = 0; j < 26; j++) mn[j] = 1e9; n     int flag = P - 1; n     for (int i = 0; i < n; i++) n       if (S & (1 << i)) { n         flag = P - flag; n         for (int j = 0; j < 26; j++) mn[j] = min(mn[j], cnt[i][j]); n       } n     f[S] = flag; n     for (int i = 0; i < 26; i++) f[S] = 1ll * f[S] * (mn[i] + 1) % P; n   } n   for (int i = 0; i < n; i++) n     for (int S = 0; S < (1 << n); S++) n       if (S & (1 << i)) f[S] = (f[S] + f[S ^ (1 << i)]) % P; n   long long ans = 0; n   for (int S = 0; S < (1 << n); S++) { n     int b = 0, a = 0; n     for (int i = 0; i < n; i++) n       if (S & (1 << i)) b++, a += i + 1; n     ans ^= 1ll * a * b * f[S]; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n string S; n int ans; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   for (int i = 1; i <= 3; i++) { n     getline(cin, S); n     ans = 0; n     for (int j = 0; j < S.length(); j++) { n       if (S[j] ==  a  || S[j] ==  i  || S[j] ==  u  || S[j] ==  e  || n           S[j] ==  o ) n         ans++; n     } n     if (ans == 5 && i == 2) { n       cout <<  NO n ; n       return 0; n     } else if (ans == 7 && i % 2 == 1) { n       cout <<  NO n ; n       return 0; n     } else if (ans != 5 && ans != 7) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[200000]; n long long int left1[200000][2]; n long long int right1[200000][2]; n long long int ans[200000]; n int main() { n   long long int n; n   cin >> n; n   left1[1][0] = 0; n   right1[n][0] = 0; n   for (long long int i = 1; i <= n; i++) cin >> a[i]; n   left1[1][1] = a[1]; n   right1[n][1] = a[n]; n   for (long long int i = 2; i <= n; i++) { n     left1[i][1] = max(a[i], left1[i - 1][1] + 1); n     left1[i][0] = left1[i - 1][0] + (left1[i][1] - a[i]); n   } n   for (long long int i = n - 1; i >= 0; i--) { n     right1[i][1] = max(a[i], right1[i + 1][1] + 1); n     right1[i][0] = right1[i + 1][0] + right1[i][1] - a[i]; n   } n   for (long long int i = 1; i <= n; i++) n     ans[i] = left1[i - 1][0] + right1[i + 1][0] + n              max(left1[i][1], right1[i][1]) - a[i]; n   long long int j = 1e18; n   for (long long int i = 1; i <= n; i++) { n     j = min(ans[i], j); n   } n   cout << j; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> poz[100]; n int main() { n   string s; n   cin >> s; n   for (int i(0); i <= 26; i++) poz[i].push_back(-1); n   for (int i(0); i < s.size(); i++) poz[s[i] -  a ].push_back(i); n   for (int i(0); i <= 26; i++) poz[i].push_back(s.size()); n   int kmin = s.size(); n   for (int i(0); i < 26; i++) { n     int dmax = 0; n     for (int j(0); j < poz[i].size() - 1; j++) n       dmax = max(dmax, poz[i][j + 1] - poz[i][j]); n     kmin = min(kmin, dmax); n   } n   cout << kmin; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = int64_t; n using vll = vector<ll>; n using vvll = vector<vll>; n using pll = pair<ll, ll>; n using vpll = vector<pll>; n using vvpll = vector<vpll>; n bool sorted(int arr[], int n) { n   bool f1 = 1, f2 = 1; n   for (int i = 0; i < n - 1; i++) { n     if (arr[i + 1] >= arr[i]) { n       f1 = 0; n       break; n     } n   } n   for (int i = 0; i < n - 1; i++) { n     if (arr[i + 1] <= arr[i]) { n       f2 = 0; n       break; n     } n   } n   return f1 || f2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   ; n   cout.setf(ios::fixed); n   cout.precision(32); n   ; n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     int index = -1; n     for (int i = 0; i < n - 1; i++) { n       if (abs(arr[i] - arr[i + 1]) != 1) { n         index = i; n         break; n       } n     } n     if (index != -1) { n       reverse(arr, arr + index + 1); n       reverse(arr + index + 1, arr + n); n     } n     if (sorted(arr, n)) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   long long int n; n   cin >> n; n   getline(cin, s); n   getline(cin, s); n   long long int l = s.length(); n   long long int a = 0, ii = 0, f = 0; n   for (long long int i = 0; i < l; i++) { n     if (s[i] ==  A ) { n       a++; n     } else { n       if (s[i] ==  I ) { n         ii++; n       } else { n         f++; n       } n     } n   } n   if (ii == 0) { n     cout << a << endl; n   } else { n     if (ii == 1) { n       cout << 1 << endl; n     } else { n       cout << 0 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n long long mul(long long a, long long b) { return (a * b) % MOD; } n long long bin_pow(long long a, long long p) { n   if (p == 0) return 1; n   long long ans = bin_pow(mul(a, a), p / 2); n   if (p % 2) ans = mul(ans, a); n   return ans; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, a; n   cin >> n >> m >> a; n   vector<long long> b(m + 1); n   b[0] = 0; n   for (long long i = 0; i < m; i++) cin >> b[i + 1]; n   long long ans = 1; n   for (long long i = 0; i < (long long)(b).size() - 1; i++) { n     long long seg = b[i + 1] - b[i]; n     long long p = bin_pow(a, seg); n     long long p1 = (p * (p - 1) / 2) % MOD; n     long long delta = (p1 + p) % MOD; n     ans = mul(ans, delta); n   } n   long long ost = n - b.back() * 2; n   ans = mul(ans, bin_pow(a, ost)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x[101] = {0}, i, a, m = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a; n     x[a]++; n   } n   for (i = 0; i <= 100; i++) { n     if (x[i] > m) m = x[i]; n   } n   cout << m; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100 + 10]; n int main() { n   int n; n   while (~scanf( %d , &n)) { n     int p1, p2, p3, t1, t2; n     scanf( %d%d%d%d%d , &p1, &p2, &p3, &t1, &t2); n     int ans = 0; n     int temp, len = 0; n     for (int i = 1; i <= n; i++) { n       int x, y; n       scanf( %d%d , &x, &y); n       ans += (y - x) * p1; n       if (i > 1) { n         a[len++] = x - temp; n       } n       temp = y; n     } n     for (int i = 0; i < len; i++) { n       if (a[i] < t1) { n         ans += a[i] * p1; n       } else if (a[i] < t1 + t2) { n         ans += t1 * p1 + (a[i] - t1) * p2; n       } else { n         ans += t1 * p1 + t2 * p2 + (a[i] - t1 - t2) * p3; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAX_L = 2005; n const int INF_INT = 0x3f3f3f3f; n const long long INF_LL = 0x7fffffff; n const int MOD = 1e9 + 7; n const double eps = 1e-9; n const double pi = acos(-1); n using namespace std; n int n; n int a[26], b[26][26]; n int main() { n   while (cin >> n) { n     string s; n     memset(a, 0, sizeof(a)); n     memset(b, 0, sizeof(b)); n     for (int i = 1; i <= n; i++) { n       cin >> s; n       set<char> st; n       for (int i = 0; i < s.size(); i++) st.insert(s[i]); n       if (st.size() > 2) continue; n       if (st.size() == 1) n         a[s[0] -  a ] += s.size(); n       else if (st.size() == 2) { n         char tmp[2]; n         int t = 0; n         for (auto &i : st) { n           tmp[t++] = i; n         } n         b[tmp[0] -  a ][tmp[1] -  a ] += s.size(); n         b[tmp[1] -  a ][tmp[0] -  a ] += s.size(); n       } n     } n     int ans = 0; n     for (int i = 0; i < 26; i++) { n       for (int j = 0; j < 26; j++) { n         if (i == j) continue; n         ans = max(ans, a[i] + a[j] + b[i][j]); n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void task(); n int main() { n   srand(time(0)); n   ios_base::sync_with_stdio(0); n   task(); n   return 0; n } n const int INF = 0x3f3f3f3f; n const int N = 1e6 + 10; n const int M = 1e5 + 10; n struct SufAutomaton { n   struct State { n     int len; n     int link; n     int cnt; n     map<char, int> next; n     State() : len(0), link(-1), cnt(0){}; n   }; n   State st[2 * N]; n   int sz = 1; n   int last = 0; n   void push(char ch) { n     int cur = sz++; n     st[cur].len = st[last].len + 1; n     st[cur].cnt = 1; n     int p = last; n     last = cur; n     for (; p != -1 && !st[p].next.count(ch); p = st[p].link) n       st[p].next[ch] = cur; n     if (p == -1) { n       st[cur].link = 0; n       return; n     } n     int q = st[p].next[ch]; n     if (st[p].len + 1 == st[q].len) { n       st[cur].link = q; n       return; n     } n     int clone = sz++; n     st[clone].len = st[p].len + 1; n     st[clone].next = st[q].next; n     st[clone].link = st[q].link; n     st[q].link = st[cur].link = clone; n     for (; p != -1 && st[p].next.count(ch) && st[p].next[ch] == q; n          p = st[p].link) n       st[p].next[ch] = clone; n   } n   void calc() { n     vector<pair<int, int> > v; n     for (int i = 0; i < sz; ++i) v.push_back(pair<int, int>{st[i].len, i}); n     sort((v.begin()), (v.end())); n     reverse((v.begin()), (v.end())); n     for (pair<int, int> p : v) st[st[p.second].link].cnt += st[p.second].cnt; n   } n   int prefix_function(const string&& s) { n     int n = s.length(); n     vector<int> p(n, 0); n     for (int i = 1; i < n; ++i) { n       int j = p[i - 1]; n       while (j && s[i] != s[j]) j = p[j - 1]; n       if (s[i] == s[j]) p[i] = j + 1; n       if (p[i] == n / 2) return i; n     } n   } n   int solve(string& s) { n     int n = s.length(); n     int yet = prefix_function(s + s) - n; n     int len = 0; n     int ans = 0; n     int cur = 0; n     for (int i = 0; i < n; ++i) { n       while (cur != -1 && st[cur].next.count(s[i]) == 0) { n         cur = st[cur].link; n         len = cur == -1 ? 0 : st[cur].len; n       } n       if (cur == -1) { n         cur = 0; n         len = 0; n       } else { n         ++len; n         cur = st[cur].next[s[i]]; n       } n     } n     if (len == n) ans += st[cur].cnt; n     for (int i = 0; i < yet; ++i) { n       if (len == n) { n         --len; n         if (len <= st[st[cur].link].len) cur = st[cur].link; n       } n       while (cur != -1 && st[cur].next.count(s[i]) == 0) { n         cur = st[cur].link; n         len = cur == -1 ? 0 : st[cur].len; n       } n       if (cur == -1) { n         cur = 0; n         len = 0; n       } else { n         ++len; n         cur = st[cur].next[s[i]]; n       } n       if (len == n) { n         ans += st[cur].cnt; n       } n     } n     return ans; n   } n }; n string s; n SufAutomaton sa; n void task() { n   cin >> s; n   for (int i = 0; i < s.length(); ++i) sa.push(s[i]); n   sa.calc(); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> s; n     cout << sa.solve(s) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int a, v, c, next; n   node() {} n   node(int a, int v, int c, int next) : a(a), v(v), c(c), next(next) {} n } edge[200010]; n struct pp { n   int v, d1, d2; n }; n queue<int> q; n int n, m, tot; n int dis[100010], d[100010], pre[100010], ans[100010], head[100010]; n int vis[100010]; n void add(int x, int y, int z) { n   edge[++tot] = node(x, y, z, head[x]); n   head[x] = tot; n } n void SPFA() { n   memset(d, 0, sizeof d); n   memset(ans, 0, sizeof ans); n   memset(vis, 0, sizeof vis); n   for (int i = 1; i <= 100010; i++) dis[i] = 111111; n   dis[1] = 0; n   vis[1] = 1; n   q.push(1); n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     vis[v] = 0; n     for (int i = head[v]; i != -1; i = edge[i].next) n       if (dis[v] + 1 < dis[edge[i].v] || n           (dis[v] + 1 == dis[edge[i].v] && d[v] + edge[i].c > d[edge[i].v])) { n         dis[edge[i].v] = dis[v] + 1; n         d[edge[i].v] = d[v] + edge[i].c; n         pre[edge[i].v] = i; n         if (!vis[edge[i].v]) { n           q.push(edge[i].v); n           vis[edge[i].v] = 1; n         } n       } n   } n   int xx = n; n   while (xx != 1) { n     ans[(pre[xx] + 1) / 2] = 1; n     xx = edge[pre[xx]].a; n   } n } n int main() { n   while (scanf( %d %d , &n, &m) != EOF) { n     tot = 0; n     int sum = 0, x, y, z; n     memset(head, -1, sizeof head); n     for (int i = 1; i <= m; i++) { n       scanf( %d %d %d , &x, &y, &z); n       add(y, x, z); n       add(x, y, z); n       if (z) sum++; n     } n     SPFA(); n     printf( %d n , sum + dis[n] - 2 * d[n]); n     for (int i = 2; i <= 2 * m; i += 2) n       if (ans[i / 2] ^ edge[i].c) n         printf( %d %d %d n , edge[i].a, edge[i].v, edge[i].c ^ 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pow1(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   long long ab = pow1(a, b / 2) % 1000000007; n   if (b % 2 == 0) n     return (ab * ab) % 1000000007; n   else n     return ((ab * ab) % 1000000007 * a) % 1000000007; n } n int main() { n   string s; n   long long k; n   cin >> s >> k; n   long long ans = 0; n   for (int i = (int)(s.size()) - 1; i >= 0; i--) { n     if (s[i] ==  0  || s[i] ==  5 ) { n       ans = (ans + pow1(2, i)) % 1000000007; n     } n   } n   long long ha = pow1(2, (int)(s.size()) * k) - 1; n   ha = (ha * pow1(pow1(2, (int)(s.size())) - 1, 1000000007 - 2)) % 1000000007; n   ans = (ans * ha) % 1000000007; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   vector<long long> arr(n, 0); n   vector<long long> pref(n, 0); n   vector<long long> suff(n, 0); n   map<long long, long long> pre; n   map<long long, long long> suf; n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   pref[0] = arr[0]; n   suff[n - 1] = arr[n - 1]; n   for (long long i = 1; i < n; i++) pref[i] = pref[i - 1] + arr[i]; n   for (long long i = n - 2; i >= 0; i--) suff[i] = suff[i + 1] + arr[i]; n   if (pref[n - 1] % 2 != 0) { n     cout <<  NO n ; n     return 0; n   } n   for (long long i = n - 1; i >= 0; i--) suf[arr[i]]++; n   long long fl = 0; n   for (long long i = 0; i < n - 1; i++) { n     long long a = pref[i]; n     if (suf[arr[i]] == 1) n       suf.erase(suf.find(arr[i])); n     else n       suf[arr[i]]--; n     pre[arr[i]]++; n     long long b = suff[i + 1]; n     if (1) { n       long long temp = suff[i + 1] + arr[i]; n       long long diff = temp - pref[n - 1] / 2; n       if (suf.find(diff) != suf.end()) { n         fl = 1; n         break; n       } n     } n     if (a == b) { n       fl = 1; n       break; n     } n     if (1) { n       long long temp = pref[i] + arr[i + 1]; n       long long diff = temp - pref[n - 1] / 2; n       if (pre.find(diff) != pre.end()) { n         fl = 1; n         break; n       } n     } n   } n   if (!fl) n     cout <<  NO n ; n   else n     cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n namespace FASTIO { n char buf[1 << 21]; n char *p1, *p2; n template <typename T> n inline void wt(T x, char c =   n ) { n   if (x < 0) { n     x = -x; n     putchar( - ); n   } n   static char sta[sizeof(T) * 8]; n   T top = 0; n   do { n     sta[top++] = x % 10; n     x /= 10; n   } while (x); n   while (top) putchar(sta[--top] +  0 ); n   putchar(c); n } n template <typename T> n inline T rd() { n   T sum = 0, fl = 1; n   char ch = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n            ? EOF n            : *p1++); n   for (; ch <  0  || ch >  9 ; n        ch = (p1 == p2 && n                      (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n                  ? EOF n                  : *p1++)) n     if (ch ==  - ) n       fl = -1; n     else if (ch == EOF) n       exit(0); n   for (;  0  <= ch && ch <=  9 ; n        ch = (p1 == p2 && n                      (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n                  ? EOF n                  : *p1++)) n     sum = sum * 10 + ch -  0 ; n   return sum * fl; n } n }  // namespace FASTIO n using namespace FASTIO; n const int N = 3e5 + 2, logN = 19; n int dep[N], anc[N][logN]; n inline int lca(int x, int y) { n   if (dep[x] < dep[y]) std::swap(x, y); n   { n     int dt = dep[x] - dep[y]; n     if (dt) n       for (int i = (31 - __builtin_clz(dt)); i >= 0; i--) n         if (dt & 1 << i) x = anc[x][i]; n   } n   if (x == y) return x; n   for (int i = (31 - __builtin_clz(dep[x])); i >= 0; i--) n     if (anc[x][i] != anc[y][i]) { n       x = anc[x][i]; n       y = anc[y][i]; n     } n   return anc[x][0]; n } n inline int dist(const int x, const int y) { n   return dep[x] + dep[y] - dep[lca(x, y)] * 2; n } n std::vector<int> s1, s2; n int main() { n   const int n = rd<int>() + 1; n   s1.reserve(n + 1); n   s2.reserve(n + 1); n   s1.push_back(1); n   dep[1] = 1; n   for (int i = 2, maxd = 0, d1, d2; i <= n; i++) { n     dep[i] = dep[anc[i][0] = rd<int>()] + 1; n     for (int j = 1; 1 << j <= dep[i]; j++) n       anc[i][j] = anc[anc[i][j - 1]][j - 1]; n     d1 = s1.empty() ? 0 : dist(i, s1.back()); n     d2 = s2.empty() ? 0 : dist(i, s2.back()); n     if (std::max(d1, d2) > maxd) { n       maxd = std::max(d1, d2); n       if (maxd != d1) { n         s1.swap(s2); n         std::swap(d1, d2); n       } n       for (const int &x : s2) n         if (dist(x, i) == d1) s1.push_back(x); n       s2.clear(); n     } n     if (std::max(d1, d2) == maxd) (maxd == d1 ? s2 : s1).push_back(i); n     wt(s1.size() + s2.size()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n int n, pre[maxn], son[maxn], dep[maxn], Ans[maxn], cnt, lis[maxn]; n vector<int> g[maxn]; n int main() { n   scanf( %d , &n); n   dep[1] = 1; n   for (int i = 1; i < n; i++) { n     scanf( %d , &pre[i + 1]); n     pre[i + 1]++; n     g[pre[i + 1]].push_back(i + 1); n     son[pre[i + 1]]++; n     dep[i + 1] = dep[pre[i + 1]] + 1; n   } n   int p = 1; n   for (int i = 2; i <= n; i++) n     if (dep[i] > dep[p]) p = i; n   int from = p; n   cnt = 0; n   int lim = n - dep[p]; n   for (int i = 1; i <= lim; i++) { n     while (pre[p] && son[pre[p]] <= 1) p = pre[p]; n     if (!pre[p]) break; n     Ans[++cnt] = p; n     int f = pre[p]; n     pre[p] = 0; n     while (pre[g[f].back()] != f) g[f].pop_back(); n     pre[p] = g[f].back(); n     g[pre[p]].push_back(p); n     son[pre[p]]++; n     son[f]--; n   } n   int lcnt = 0; n   p = from; n   lis[++lcnt] = p; n   while (pre[p]) { n     lis[++lcnt] = pre[p]; n     p = pre[p]; n   } n   for (int i = lcnt; i >= 1; i--) printf( %d  , lis[i] - 1); n   puts(  ); n   printf( %d n , cnt); n   for (int i = cnt; i >= 1; i--) { n     int x = Ans[i] - 1; n     printf( %d  , x); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char s[110][110]; n void work1() { n   if (n % 3) { n     puts( NO ); n     return; n   } n   int k = n / 3; n   if (s[0][0] == s[k][0] || s[0][0] == s[k * 2][0] || s[k][0] == s[k * 2][0]) { n     puts( NO ); n     return; n   } n   for (int h = 0; h < 3; h++) { n     for (int i = 0; i < k; i++) { n       for (int j = 0; j < m; j++) { n         if (s[h * k][0] != s[h * k + i][j]) { n           puts( NO ); n           return; n         } n       } n     } n   } n   puts( YES ); n } n void work2() { n   if (m % 3) { n     puts( NO ); n     return; n   } n   int k = m / 3; n   if (s[0][0] == s[0][k] || s[0][0] == s[0][k * 2] || s[0][k] == s[0][k * 2]) { n     puts( NO ); n     return; n   } n   for (int h = 0; h < 3; h++) { n     for (int i = 0; i < k; i++) { n       for (int j = 0; j < n; j++) { n         if (s[0][h * k] != s[j][h * k + i]) { n           puts( NO ); n           return; n         } n       } n     } n   } n   puts( YES ); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %s , s[i]); n   } n   bool f = true; n   for (int i = 1; i < m; i++) { n     if (s[0][i] != s[0][i - 1]) { n       f = false; n       break; n     } n   } n   if (f) { n     work1(); n   } else { n     work2(); n   } n   return 0; n } n


#include <bits/stdc++.h> n unsigned *G[222222], Ga[222222], Gi[222222]; n void add(unsigned i, unsigned j) { n   if (Ga[i] == Gi[i]) { n     if (!Ga[i]) n       G[i] = (unsigned *)calloc(Ga[i] = 1, sizeof(unsigned)); n     else n       G[i] = (unsigned *)realloc(G[i], (Ga[i] <<= 1) * sizeof(unsigned)); n   } n   G[i][Gi[i]++] = j; n   return; n } n unsigned P[222222], D[222222], IL[222222], mi, ma; n void F(unsigned n, unsigned p, unsigned d) { n   if (!d) { n     mi = -1; n     ma = 0; n   } n   unsigned i, j; n   P[n] = p; n   D[n] = d; n   if (IL[n]) { n     if (mi > d) mi = d; n     if (ma < d) ma = d; n   } n   for (i = Gi[n]; i--;) n     if ((j = G[n][i]) != p) F(j, n, d + 1); n   return; n } n unsigned L[222222], Li, Q[9], Qi; n int main() { n   unsigned n, i, j, k, x; n   scanf( %u , &n); n   for (k = n; --k;) { n     scanf( %u%u , &i, &j); n     add(i, j); n     add(j, i); n   } n   for (i = 0; ++i <= n;) n     if (Gi[i] == 1) IL[L[Li++] = i] = 1; n   F(L[0], 0, 0); n   for (i = j = k = 0; ++i <= n;) n     if (D[i] > k) k = D[j = i]; n   F(j, 0, 0); n   for (i = j = k = 0; ++i <= n;) n     if (D[i] > k) k = D[j = i]; n   for (i = k >> 1; i--;) j = P[j]; n   if (k & 1) { n     F(i = P[j], j, 0); n     if (mi != ma) goto NO; n     F(j, i, 0); n     if (mi != ma) goto NO; n   } else { n     for (i = Gi[j]; i--;) { n       F(G[j][i], j, 0); n       if (mi != ma) goto NO; n       for (x = -1; ++x < Qi;) n         if (Q[x] == mi) goto tebak; n       Q[Qi++] = mi; n       if (Qi > 2) goto NO; n     tebak:; n     } n     for (k = 0, x = -1; ++x < Qi;) k += Q[x] + 1; n   } n   while (!(k & 1)) k >>= 1; n   printf( %u n , k); n   return 0; n NO:; n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const MAXN = 2e5 + 10; n int n; n int a[MAXN], L[MAXN * 30], R[MAXN * 30]; n struct Trie { n   int tot = 1; n   int trie[MAXN * 30][2]; n   void insert(int pos) { n     int x = 1; n     for (int i = 30;; i--) { n       int dir = (a[pos] >> i) & 1; n       L[x] = min(L[x], pos), R[x] = max(R[x], pos); n       if (i < 0) break; n       if (!trie[x][dir]) trie[x][dir] = ++tot; n       x = trie[x][dir]; n     } n     return; n   } n   long long query(int x, int now, int dep) { n     if (dep < 0) return 0ll; n     int dir = (x >> dep) & 1; n     if (trie[now][dir]) n       return query(x, trie[now][dir], dep - 1); n     else n       return query(x, trie[now][dir ^ 1], dep - 1) + (1ll << dep); n   } n   long long dfs(int now, int dep) { n     if (dep < 0) return 0; n     if (trie[now][0] && trie[now][1]) { n       long long ans = 1ll << 30; n       for (int i = L[trie[now][0]]; i <= R[trie[now][0]]; i++) { n         ans = min(ans, (long long)query(a[i], trie[now][1], dep - 1)); n       } n       ans += dfs(trie[now][0], dep - 1); n       ans += dfs(trie[now][1], dep - 1); n       ans += (1 << dep); n       return ans; n     } else if (trie[now][0]) { n       return dfs(trie[now][0], dep - 1); n     } else if (trie[now][1]) { n       return dfs(trie[now][1], dep - 1); n     } n     return 0ll; n   } n } trie; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   memset(L, 0x3f, sizeof(L)); n   for (int i = 1; i <= n; i++) trie.insert(i); n   printf( %lld n , trie.dfs(1, 30)); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ,  unroll-all-loops ) n #pragma GCC target( sse4.2 ) n using namespace std; n ifstream in; n ofstream out; n const long long kk = 1000; n const long long ml = kk * kk; n const long long mod = 998244353; n const long long inf = ml * ml * ml + 7; n const int C = 5; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n, nxt, root; n vector<int> p, comp, sz, edges; n set<pair<int, pair<int, int>>> e; n vector<vector<int>> kids; n bool viv = false; n bool big = false; n const long double PI = 3.1415926535897932384626433; n inline int rev(int val, int b) { n   int res = 0; n   for (int i = 0; i < b; i++) n     if (val & (1 << i)) res |= (1 << (b - 1 - i)); n   return res; n } n vector<int> mul_stpd(const vector<int> &a, const vector<int> &b) { n   vector<long long> res(a.size() + b.size() - 1); n   for (int i = 0; i < b.size(); i++) { n     for (int j = 0; j < a.size(); j++) { n       res[i + j] += (long long)a[j] * b[i]; n       res[i + j] %= mod; n     } n   } n   return vector<int>(res.begin(), res.end()); n } n int pp(int v) { n   if (p[v] == v) return v; n   return p[v] = pp(p[v]); n } n bool full(int v) { n   if (viv) n     cout <<  at   << v <<   with sz =   << sz[v] <<   see   << edges[v] << endl; n   return ((int)sz[v] - 1) * (sz[v]) == 2 * edges[v]; n } n void unit(int v, int u) { n   v = pp(v); n   u = pp(u); n   if (v == u) { n     edges[v]++; n     return; n   } n   if (full(v) && full(u)) { n     if (viv) cout <<  Fully  << endl; n     if (sz[v] < sz[u]) swap(v, u); n     sz[v] += sz[u]; n     edges[v] += edges[u]; n     p[u] = v; n     edges[v]++; n     kids[nxt].push_back(comp[v]); n     kids[nxt].push_back(comp[u]); n     comp[v] = nxt++; n   } else { n     if (viv) cout <<  Not Fully  << endl; n     if (sz[v] < sz[u]) swap(v, u); n     if (full(v) && !full(u)) swap(v, u); n     sz[v] += sz[u]; n     edges[v] += edges[u]; n     p[u] = v; n     edges[v]++; n     assert(comp[v] >= n); n     if (full(u)) n       kids[comp[v]].push_back(comp[u]); n     else { n       for (auto s : kids[comp[u]]) kids[comp[v]].push_back(s); n     } n   } n   root = comp[v]; n } n void work(int v, int u) { unit(v, u); } n void show() { n   for (int i = nxt - 1; i >= 0; i--) { n     cout << i <<  :  ; n     for (auto s : kids[i]) cout << s <<    ; n     cout << endl; n   } n } n vector<int> get(int v) { n   if (viv) { n     cout <<  get(  << v <<  )  << endl; n   } n   if (v < n) { n     return vector<int>({0, 1}); n   } n   vector<int> lres(1, 1); n   for (auto s : kids[v]) { n     lres = mul_stpd(lres, get(s)); n     if (viv) { n       cout <<   tnow   << v <<  :  ; n       for (auto i : lres) cout << i <<    ; n       cout << endl; n     } n   } n   while (lres.size() < 2) lres.push_back(0); n   lres[1]++; n   if (viv) { n     cout << v <<  :  ; n     for (auto i : lres) cout << i <<    ; n     cout << endl; n   } n   return lres; n } n void solve() { n   cin >> n; n   nxt = n; n   sz.resize(n, 1); n   comp.resize(n); n   p.resize(n); n   edges.resize(n, 0); n   for (int i = 0; i < n; i++) p[i] = i, comp[i] = i; n   kids.resize(2 * n); n   vector<pair<int, int>> use(n * (n - 1) / 2); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int w; n       cin >> w; n       if (i < j) { n         use[w - 1] = {i, j}; n       } n     } n   } n   for (auto p : use) work(p.first, p.second); n   if (viv) show(); n   auto res = get(root); n   while (res.size() < n + 1) res.push_back(0); n   for (int i = 1; i <= n; i++) cout << res[i] <<    ; n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a1, a2, a3, a4; n   scanf( %d %d %d %d , &a1, &a2, &a3, &a4); n   int n = min(min(a1, a4), a3); n   long long ans = 0; n   ans += n * 256; n   a1 -= n; n   a3 -= n; n   a4 -= n; n   int m = min(a1, a2); n   ans += m * 32; n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long inf = 1e14; n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, ans = 0; n   cin >> n; n   for (long long i = 2; i <= n; i++) { n     for (long long j = i + i; j <= n; j += i) { n       ans += (j / i); n     } n   } n   cout << ans * 4; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int rem = 1000000007; n vector<long long> v; n vector<long long> d(21); n vector<long long> down(21); n long long n, s; n long long binpow(long long a, long long n) { n   if (n == 0) return 1; n   long long tmp = binpow(a, n / 2); n   if (n & 1) return tmp * tmp % rem * a % rem; n   return tmp * tmp % rem; n } n long long one = 1; n long long getc(long long m, long long n) { n   if (n < m) return 0; n   long long ans = 1; n   for (int i = 1; i <= m; ++i) n     ans = (n - i + 1) % rem * ans % rem * down[i] % rem; n   return ans; n } n void fn(long long indx, long long numnumbers, long long sum) { n   if (sum > s) return; n   if (indx == n) { n     sum = s - sum; n     d[numnumbers] += getc(n - one, sum + n - one); n     return; n   } n   fn(indx + one, numnumbers, sum); n   fn(indx + one, numnumbers + one, sum + v[indx] + one); n } n int main() { n   cin >> n >> s; n   for (int i = 1; i <= 20; i++) { n     down[i] = binpow(i, rem - 2); n   } n   v.resize(n); n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &v[i]); n   } n   long long rest = getc(n - one, n + s - one); n   fn(0, 0, 0); n   for (int i = 1; i <= n; i++) { n     if (i & 1) n       rest -= d[i]; n     else n       rest += d[i]; n     rest += rem; n     rest %= rem; n   } n   cout << rest; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int a[305]; n int main() n { n     int t; n     cin>>t; n     while(t--) n     { n         int n,x; n         cin>>n; n         string s; n         cin>>s; n         bool flag=false; n         if(s== 2020 ) n             flag=true; n         else n         { n             for(int i=0; i<n; ++i) n             { n                 string tmp=s; n                 if(n-4>=0) n                     tmp.erase(i,n-4); n                 if(tmp== 2020 ) n                     flag=true; n             } n         } n         if(flag) n             cout<< YES ; n         else n             cout<< NO ; n         cout<<endl; n     } n     //getchar(); n     //getchar(); n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[100005]; n bitset<100005> vis; n int nodes = 0, edges = 0; n void dfs(int v) { n   nodes++; n   vis[v] = true; n   for (auto nv : adj[v]) { n     if (!vis[nv]) dfs(nv); n     edges++; n   } n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int x[k], y[k]; n   for (int i = 0; i < k; i++) { n     scanf( %d%d , &x[i], &y[i]); n     adj[--x[i]].push_back(--y[i]); n     adj[y[i]].push_back(x[i]); n   } n   int sad = 0; n   for (int i = 0; i < n; i++) { n     if (!vis[i]) { n       nodes = 0; n       edges = 0; n       dfs(i); n       edges >>= 1; n       sad += edges - nodes + 1; n     } n   } n   printf( %d n , sad); n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   long long ans = 1; n   while (b > 0) { n     if (b % 2 != 0) { n       ans = (ans * a) % 1000000007; n     } n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return ans; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t = 1; n   cin >> t; n   while (t--) { n     long long d, m; n     cin >> d >> m; n     vector<int> v, p, q; n     long long z = 1, s = 1; n     v.push_back(1); n     for (int i = 2; i < 65; i++) { n       long long k = pow(2, i - 1); n       if (s + k < d) { n         v.push_back(k); n         s += k; n       } else { n         v.push_back(d - s); n         break; n       } n     } n     vector<long long> ha; n     ha.push_back(1); n     ha.push_back(0); n     for (int i = 0; i < v.size(); i++) { n       vector<long long> u; n       u.push_back(1); n       for (int j = 0; j < ha.size() - 1; j++) { n         u.push_back((ha[j] * v[i] + ha[j + 1]) % m); n       } n       u.push_back(0); n       ha = u; n     } n     long long sum = 0; n     for (int i = 1; i < ha.size(); i++) { n       sum += ha[i]; n       sum %= m; n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n void read(long long &x) { n   x = 0; n   char ch = getchar(); n   long long pd = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) { n       pd = -pd; n     } n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= pd; n } n long long gcd(long long x, long long y) { return !x ? y : gcd(y % x, x); } n long long l, n, m, aa[10010]; n long long a[N], b[N]; n long long ans, lcm, x, y, bg; n long long mul(long long a, long long b, long long P) { n   long long s = 0; n   for (; b; b >>= 1, a = (a + a) % P) n     if (b & 1) s = (s + a) % P; n   return s; n } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1, y = 0; n     return a; n   } n   long long gd = exgcd(b, a % b, y, x); n   y -= a / b * x; n   return gd; n } n long long excrt() { n   ans = a[1], lcm = b[1], x, y; n   for (long long i = 2; i <= l; i++) { n     long long B = b[i], C = (a[i] - ans % B + B) % B; n     long long d = exgcd(lcm, B, x, y); n     if (C % d) return -1; n     B = B / d; n     C = C / d; n     x = (x + B) % B; n     x = mul(x, C, B); n     ans += x * lcm; n     lcm *= B; n     ans %= lcm; n   } n   return ans == 0 ? lcm : ans; n } n signed main() { n   scanf( %lld%lld%lld , &n, &m, &l); n   if (l > m) { n     puts( NO ); n     return 0; n   } n   for (register long long i = 1; i <= l; ++i) { n     scanf( %lld , &aa[i]); n     b[i] = aa[i]; n   } n   for (register long long i = 1; i <= l; ++i) { n     a[i] = (1 - i); n     while (a[i] < aa[i]) { n       a[i] += aa[i]; n     } n     a[i] %= aa[i]; n   } n   long long yy = excrt(); n   if (yy == -1) { n     puts( NO ); n     return 0; n   } n   if (yy + l - 1 > m) { n     puts( NO ); n     return 0; n   } n   if (lcm > n) { n     puts( NO ); n     return 0; n   } n   for (register long long i = 1; i <= l; ++i) { n     if (gcd(lcm, yy + i - 1) != aa[i]) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long nax = 1e5 + 100; n long long n, m; n signed main() { n   cin >> n >> m; n   long long dp[n][m], a[n][m]; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) { n       cin >> a[i][j]; n       dp[i][j] = 1; n     } n   for (long long j = 0; j < m; j++) n     for (long long i = 1; i < n; i++) n       if (a[i][j] >= a[i - 1][j]) dp[i][j] = dp[i - 1][j] + 1; n   for (long long i = 0; i < n; i++) sort(dp[i], dp[i] + m); n   long long q; n   cin >> q; n   while (q--) { n     long long l, r; n     cin >> l >> r; n     --l; n     --r; n     if (dp[r][m - 1] >= (r - l + 1)) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long rdtsc() { n   long long tmp; n   asm( rdtsc  :  =A (tmp)); n   return tmp; n } n inline int myrand() { return abs((rand() << 15) ^ rand()); } n inline int rnd(int x) { return myrand() % x; } n const int maxn = (int)1e5; n char s[maxn + 1]; n long long hs[maxn + 1]; n const long long Q = 239017; n long long qs[maxn + 1]; n const int maxc = 26; n int colors[maxc][maxn + 1]; n void precalc(int n) { n   hs[0] = 0; n   for (int i = 0; i < n; ++i) { n     hs[i + 1] = hs[i]; n     hs[i + 1] *= Q; n     hs[i + 1] += (int)s[i]; n   } n   qs[0] = 1; n   for (int i = 1; i <= n; ++i) qs[i] = qs[i - 1], qs[i] *= Q; n   for (int color = 0; color < maxc; ++color) { n     colors[color][0] = 0; n     for (int i = 0; i < n; ++i) n       colors[color][i + 1] = colors[color][i] + (s[i] == color +  a ); n   } n } n inline long long getHash(int pos, int len) { n   long long tmp = hs[pos]; n   tmp *= qs[len]; n   long long res = hs[pos + len]; n   res -= tmp; n   return res; n } n const int maxl = 17; n inline int commonLen(int left, int right, int n) { n   assert(left < right); n   int ans = 0; n   for (int it = maxl - 1; it >= 0; --it) { n     if (right + ans + (1 << it) <= n && n         getHash(left, ans + (1 << it)) == getHash(right, ans + (1 << it))) n       ans += (1 << it); n   } n   return ans; n } n int gray[maxl][maxn]; n int grayLen[maxl]; n long long wholeTaken[maxn + 1]; n long long getInitial(int n) { n   for (int i = 0; i <= n; ++i) wholeTaken[i] = 0; n   long long res = 0; n   for (int len = 0; len < maxl; ++len) { n     grayLen[len] = (1 << (len + 1)) - 1; n     int n0 = n - grayLen[len] + 1; n     for (int i = 0; i < n0; ++i) { n       int &current = gray[len][i]; n       current = 0; n       if (!len) { n         current = 1; n         continue; n       } n       int half = i + grayLen[len - 1]; n       int *countMidColor = colors[s[half] -  a ]; n       if (gray[len - 1][i] && n           countMidColor[i + grayLen[len]] - countMidColor[i] == 1 && n           getHash(i, grayLen[len - 1]) == getHash(half + 1, grayLen[len - 1])) n         current = 1; n     } n     long long sqrLen = (long long)grayLen[len] * grayLen[len]; n     for (int i = 0; i < n0; ++i) { n       if (!gray[len][i]) continue; n       wholeTaken[i] += sqrLen; n       wholeTaken[i + grayLen[len]] -= sqrLen; n       res += sqrLen; n     } n   } n   for (int i = 1; i <= n; ++i) wholeTaken[i] += wholeTaken[i - 1]; n   return res; n } n long long changes[maxc][maxn]; n void findChanges(int n) { n   for (int i = 0; i < maxc; ++i) n     for (int j = 0; j < n; ++j) changes[i][j] = 0; n   for (int len = 0; len < maxl; ++len) { n     int n0 = n - grayLen[len] + 1; n     for (int i = 0; i < n0; ++i) { n       if (!len) { n         for (int c = 0; c < maxc; ++c) n           if (c +  a  != s[i]) changes[c][i] += 1; n         continue; n       } n       int half = i + grayLen[len - 1]; n       if (!gray[len - 1][i] && !gray[len - 1][half + 1]) continue; n       long long sqrLen = (long long)grayLen[len] * grayLen[len]; n       if (gray[len - 1][i] && n           getHash(i, grayLen[len - 1]) == getHash(half + 1, grayLen[len - 1])) { n         for (int c = 0; c < maxc; ++c) { n           int *countMidColor = colors[c]; n           if (countMidColor[i + grayLen[len]] - countMidColor[i] == 0) { n             changes[c][half] += sqrLen; n           } n         } n       } n       int *countMidColor = colors[s[half] -  a ]; n       int firstLen = commonLen(i, half + 1, n); n       if (firstLen >= grayLen[len - 1]) continue; n       int left = i + firstLen, right = left + grayLen[len - 1] + 1; n       int secondLen = commonLen(left + 1, right + 1, n); n       if (firstLen + 1 + secondLen < grayLen[len - 1]) continue; n       if (gray[len - 1][half + 1] && n           countMidColor[half + 1 + grayLen[len - 1]] - n                   countMidColor[half + 1] == n               0) n         changes[s[right] -  a ][left] += sqrLen; n       if (gray[len - 1][i] && countMidColor[half] - countMidColor[i] == 0) n         changes[s[left] -  a ][right] += sqrLen; n     } n   } n } n bool solve() { n   if (scanf( %s , s) < 1) return 0; n   int n = strlen(s); n   precalc(n); n   long long ans = getInitial(n); n   findChanges(n); n   long long maxAdd = 0; n   for (int i = 0; i < maxc; ++i) n     for (int j = 0; j < n; ++j) n       maxAdd = max(maxAdd, -wholeTaken[j] + changes[i][j]); n   printf( n        %lld  n         n , n       ans + maxAdd); n   return 1; n } n int main() { n   srand(rdtsc()); n   while (1) { n     if (!solve()) break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> fr; n int main() { n   ios_base::sync_with_stdio(false); n   string s; n   cin >> s; n   for (int k = 0; k <= 9; k++) { n     string a; n     cin >> a; n     fr[a] = k; n   } n   int id = 0; n   string act =   ; n   for (int k = 0; k < 80; k++) { n     act += s[k]; n     id++; n     if (id == 10) { n       cout << fr[act]; n       id = 0; n       act =   ; n     } n   } n   return 0; n } n