#include <bits/stdc++.h> n using namespace std; n long long nxt(long long x) { n   long long mn = 10; n   long long mx = 0; n   long long xx = x; n   while (x > 0) { n     mn = min(mn, x % 10); n     mx = max(mx, x % 10); n     x /= 10; n   } n   return xx + mn * mx; n } n signed main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a1, k; n     cin >> a1 >> k; n     k--; n     while (k--) { n       if (a1 == nxt(a1)) break; n       a1 = nxt(a1); n     } n     cout << a1 <<   n ; n   } n } n


#include <bits/stdc++.h> n const int MAX = 2000010, mod = 1000000007; n int n, m, mp[MAX], c[MAX], d[MAX], ds[MAX]; n bool ty[MAX]; n void upd(int i, int x) { n   x > d[i] ? d[i] = x, ds[i] = 1 : x == d[i] ? ds[i]++ : 0; n } n int main() { n   scanf( %d , &n); n   for (int p; n--;) scanf( %d , &p), c[p > m ? m = p : p]++; n   for (int i = 2; i <= m; i++) n     if (!mp[i]) n       for (int j = i; j <= m; j += i) n         if (!mp[j]) mp[j] = i; n   int s = 1, t = 0; n   for (int i = m; i; i--) n     for (int j = c[i]; j--;) { n       if (d[i]) { n         for (int x = i - 1; x > 1;) { n           int p = mp[x], c = 0; n           for (; x % p == 0; x /= p) c++; n           for (int e = d[p]; e < c; e++) s = 1ll * s * p % mod; n           upd(p, c); n         } n         ty[i] = 1; n       } else n         upd(i, 1), s = 1ll * s * i % mod; n     } n   for (int i = m; i; i--) n     if (ty[i]) { n       bool fl = 1; n       for (int x = i - 1; x > 1;) { n         int p = mp[x], c = 0; n         for (; x % p == 0; x /= p) c++; n         if (c == d[p] && ds[p] == 1) fl = 0; n       } n       if (fl) t = 1; n     } n   printf( %d n , (s + t) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10e5; n int minc, maxk = 0; n long long int a[maxn], c[maxn]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cout << setprecision(30); n   int n, s; n   cin >> n >> s; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int l = 0, r = n + 1; n   while (r - l > 1) { n     long long int mid = (l + r) / 2, tc = 0; n     for (int i = 0; i < n; i++) c[i] = mid * (i + 1) + a[i]; n     sort(c, c + n); n     for (int i = 0; i < mid; i++) tc += c[i]; n     if (tc > s) { n       r = mid; n     } n     if (tc <= s) { n       minc = tc; n       maxk = mid; n       l = mid; n     } n   } n   cout << maxk <<     << minc; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 100005; n vector<int> G[NMax]; n long double Array[NMax]; n int DP[NMax], N; n void Read() { n   cin >> N; n   for (int i = 1; i < N; i++) { n     int x; n     cin >> x; n     G[x].push_back(i + 1); n   } n } n void buildDP(int node) { n   DP[node] = 1; n   for (int i = 0; i < G[node].size(); i++) { n     int neighb = G[node][i]; n     buildDP(neighb); n     DP[node] += DP[neighb]; n   } n } n void DFS(int node) { n   int sz = G[node].size(); n   for (int i = 0; i < G[node].size(); i++) { n     int neighb = G[node][i]; n     Array[neighb] = n         Array[node] + 0.5 * ((long double)DP[node] - 1.0 - DP[neighb]) + 1.0; n     DFS(neighb); n   } n } n void Print() { n   for (int i = 1; i <= N; i++) n     cout << fixed << setprecision(2) << Array[i] <<    ; n   cout <<   n ; n } n int main() { n   Read(); n   buildDP(1); n   Array[1] = 1.0; n   DFS(1); n   Print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void no() { n   cout <<  IMPOSSIBLE  << endl; n   exit(0); n } n int main() { n   cin.sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   int answer = 0; n   int last_day = 0, last_h = 0; n   for (int i = 0; i < m; i++) { n     int cur, h; n     cin >> cur >> h; n     if (i == 0) { n       answer = h + cur - 1; n     } else { n       answer = max(answer, h); n       int dif = abs(h - last_h); n       int days = cur - last_day; n       if (dif > days) no(); n       if (h >= last_h) { n         answer = max(answer, h + (days - dif) / 2); n       } else { n         answer = max(answer, last_h + (days - dif) / 2); n       } n     } n     last_day = cur; n     last_h = h; n   } n   answer = max(answer, last_h + n - last_day); n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<long long, long long> > s[17]; n long long a[100002]; n long long g[100002]; n signed main() { n   ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i <= n; i++) g[i] = (i * (i + 1)) / 2LL; n   long long ans = 0; n   for (long long i = 0; i <= n - 1; i++) cin >> a[i]; n   for (long long j = 0; j <= 16; j++) { n     for (long long i = 0; i <= n - 1; i++) { n       if (a[i] & (1LL << j)) { n         long long len = 1; n         long long st = i; n         i++; n         while (1) { n           if (i == n or !(a[i] & (1LL << j))) { n             s[j].insert({st, len}); n             ans += (g[len] * (1LL << j)); n             break; n           } n           len++; n           i++; n         } n       } n     } n   } n   while (m--) { n     long long idx, val, pval; n     cin >> idx >> val; n     idx--; n     pval = a[idx]; n     for (long long j = 0; j <= 16; j++) { n       if (val & (1LL << j) and !(pval & (1LL << j))) { n         auto it = s[j].lower_bound({idx, 1e9}); n         if (it == s[j].end() or (*it).first != idx + 1) n           s[j].insert({idx, 1}), ans += (1LL << j); n         else n           ans += (1LL << j) * ((*it).second + 1), n               s[j].insert({idx, (*it).second + 1}), s[j].erase(it); n         it = s[j].lower_bound({idx, -1}); n         if (it != s[j].begin()) { n           auto pt = prev(it); n           if ((*pt).second + (*pt).first - 1 == idx - 1) { n             ans -= (1LL << j) * (g[(*pt).second] + g[(*it).second]), n                 ans += (1LL << j) * g[(*it).second + (*pt).second], n                 s[j].insert({(*pt).first, (*pt).second + (*it).second}), n                 s[j].erase(it), s[j].erase(pt); n           } n         } n       } else if (pval & (1LL << j) and !(val & (1LL << j))) { n         auto it = s[j].lower_bound({idx, -1}); n         if (it != s[j].end() and (*it).first == idx) { n           if ((*it).second == 1) n             ans -= (1LL << j), s[j].erase({idx, 1}); n           else n             ans -= (1LL << j) * ((*it).second), n                 s[j].insert({idx + 1, (*it).second - 1}), s[j].erase(it); n         } else { n           it = prev(it); n           if ((*it).first + (*it).second - 1 == idx) n             ans -= (1LL << j) * ((*it).second), n                 s[j].insert({(*it).first, (*it).second - 1}), s[j].erase(it); n           else { n             ans -= (1LL << j) * g[(*it).second], n                 ans += (1LL << j) * g[idx - 1 - (*it).first + 1], n                 ans += n                 (1LL << j) * g[(*it).first + (*it).second - 1 - idx - 1 + 1]; n             s[j].insert({(*it).first, idx - 1 - (*it).first + 1}), n                 s[j].insert( n                     {idx + 1, (*it).first + (*it).second - 1 - idx - 1 + 1}), n                 s[j].erase(it); n           } n         } n       } n     } n     a[idx] = val; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long aa[33]; n long long a[33], b[33], k[33], p[33]; n long long n, u, r; n long long ans = -1e10; n void dfs(long long d, long long pre) { n   if ((u - d) % 2 == 0) { n     long long t = 0; n     for (int i = 1; i <= n; i++) t = t + a[i] * k[i]; n     ans = max(ans, t); n   } n   if (d == u) return; n   long long tmp[33]; n   for (int i = 1; i <= n; i++) tmp[i] = a[i]; n   for (int i = 1; i <= n; i++) aa[i] = a[i]; n   if (pre != 1) { n     for (int i = 1; i <= n; i++) a[i] = a[i] ^ b[i]; n     dfs(d + 1, 1); n     for (int i = 1; i <= n; i++) a[i] = tmp[i]; n   } n   for (int i = 1; i <= n; i++) aa[i] = a[p[i]] + r; n   for (int i = 1; i <= n; i++) a[i] = aa[i]; n   dfs(d + 1, -1); n   for (int i = 1; i <= n; i++) a[i] = tmp[i]; n } n int main() { n   cin >> n >> u >> r; n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %I64d , &b[i]); n   for (int i = 1; i <= n; i++) scanf( %I64d , &k[i]); n   for (int i = 1; i <= n; i++) scanf( %I64d , &p[i]); n   dfs(0, -1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   int a[n]; n   map<int, int> mp; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]]++; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 32; j++) { n       int p = 1 << j; n       if (p < a[i]) { n         continue; n       } n       ans += mp[p - a[i]]; n       if (a[i] == p - a[i]) { n         ans--; n       } n     } n   } n   ans /= 2; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int msg1[n]; n   int msg2[m]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &msg1[i]); n   } n   for (int i = 0; i < m; i++) { n     scanf( %d , &msg2[i]); n   } n   for (int i = 1; i < n; i++) { n     msg1[i] = msg1[i] + msg1[i - 1]; n   } n   for (int i = 1; i < m; i++) { n     msg2[i] = msg2[i] + msg2[i - 1]; n   } n   int count = 0; n   int i = 0, j = 0; n   while (i < n && j < m) { n     if (msg1[i] < msg2[j]) n       i++; n     else if (msg1[i] > msg2[j]) n       j++; n     else { n       i++; n       j++; n       count++; n     } n   } n   printf( %d , count); n } n


#include<bits/stdc++.h> n  n using namespace std; n  n const int N = 2000 + 10; n  n int n; n bool flag[N]; n  n bool Query(int l1, int r1, int l2, int r2) { n  printf( ? %d %d n , r1 - l1 + 1, r2 - l2 + 1); n  for (int i = l1; i <= r1; i++) printf( %d%c , i,    n [i == r1]); n  for (int i = l2; i <= r2; i++) printf( %d%c , i,    n [i == r2]); n  fflush(stdout); n  int res; scanf( %d , &res); n  return res; n } n  n void Solve() { n  scanf( %d , &n); n  for (int i = 1; i <= n; i++) flag[i] = 0; n  for (int i = 2; i <= n; i++) { n   if (Query(1, i - 1, i, i)) { n    // printf( i = %d n , i); n    flag[i] = 1; n    for (int j = i + 1; j <= n; j++) { n     if (Query(i, i, j, j)) { n      flag[j] = 1; n     } n    } n    int L = 1, R = i - 1, ans = 0; n    while (L <= R) { n     // printf( L = %d, R = %d n , L, R); n     int M = (L + R) >> 1; n     if (Query(1, M, i, i)) R = M - 1, ans = M; n     else L = M + 1; n    } n    // printf( ans = %d n , ans); n    flag[ans] = 1; n    break; n   } n  } n  // puts( ans ); n  // for (int i = 1; i <= n; i++) { n  n  // } n  vector <int> ans; n  for (int i = 1; i <= n; i++) { n   if (!flag[i]) { n    ans.push_back(i); n   } n  } n  printf( ! %d n , ans.size()); n  for (auto x : ans) { n   printf( %d  , x); n  } puts(  ); n  fflush(stdout); n } n  n int main(){ n     srand(time(0)); n #ifndef ONLINE_JUDGE n     // freopen( data.in ,  r , stdin); n     // freopen( tst.out ,  w , stdout); n     clock_t st = clock(); n #endif n     int T = 1; n  scanf( %d , &T); n     while (T--) { n   Solve(); n     } n #ifndef ONLINE_JUDGE n     clock_t ed = clock(); n     printf( time : %.6lfs n , 1.0 * (ed - st) / CLOCKS_PER_SEC); n #endif n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n int m = 0; n int block[5005]; n long long f[5005]; n int main() { n   int n, tmp; n   int num_of_for = 0; n   cin >> n; n   tmp = n; n   while (tmp--) { n     char c; n     cin >> c; n     if (c ==  s ) { n       block[m] = num_of_for; n       num_of_for = 0; n       m++; n     } else { n       num_of_for++; n     } n   } n   for (int k = 0; k < n; k++) { n     f[k] = 1; n   } n   for (int i = m - 1; i >= 0; i--) { n     f[0] = f[block[i]]; n     for (int k = 1; k < n; k++) { n       if (block[i] + k < n) { n         f[k] = f[k - 1] + f[block[i] + k]; n       } else { n         f[k] = f[k - 1]; n       } n       f[k] %= mod; n     } n   } n   cout << (f[0] + mod) % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007LL; n long long N; n vector<long long> E[100000]; n long long dp[100000][2]; n long long ans = 0; n long long mod_inv(long long n) { n   long long t = MOD - 2, p = 1, q = n; n   while (t > 0) { n     if (t % 2 == 1) p = (p * q) % MOD; n     q = (q * q) % MOD; n     t /= 2; n   } n   return p % MOD; n } n void dfs(long long v, long long p) { n   bool leaf = true; n   for (long long u : E[v]) n     if (u != p) { n       dfs(u, v); n       leaf = false; n     } n   if (leaf) { n     dp[v][0] = 1; n     dp[v][1] = 1; n   } else { n     long long ev = 1, od = 1; n     for (long long u : E[v]) n       if (u != p) { n         (ev *= dp[u][0]) %= MOD; n         (od *= dp[u][1]) %= MOD; n       } n     dp[v][0] = (ev + od) % MOD; n     dp[v][1] = (od + ev) % MOD; n   } n } n void dfs2(long long v, long long p, long long ev, long long od) { n   long long _ev = 1, _od = 1; n   for (long long u : E[v]) n     if (u != p) { n       (_ev *= dp[u][0]) %= MOD; n       (_od *= dp[u][1]) %= MOD; n     } n   if (p != -1) { n     long long d = _ev * ev % MOD + _od * od % MOD; n     ans += d; n     ans %= MOD; n   } else { n     ans += dp[v][0]; n     ans %= MOD; n   } n   long long cnt = 0; n   for (long long u : E[v]) n     if (u != p) { n       cnt++; n     } n   for (long long u : E[v]) n     if (u != p) { n       long long nev, nod, s; n       if (cnt == 1) { n         s = ev + od; n         nev = s; n         nod = s; n       } else if (p == -1) { n         s = _ev * mod_inv(dp[u][0]) % MOD + _od * mod_inv(dp[u][1]) % MOD; n         nev = s; n         nod = s; n       } else { n         s = _ev * mod_inv(dp[u][0]) % MOD * ev % MOD + n             _od * mod_inv(dp[u][1]) % MOD * od % MOD; n         nev = s; n         nod = s; n       } n       nev %= MOD; n       nod %= MOD; n       dfs2(u, v, nev, nod); n     } n } n int main(void) { n   cin >> N; n   for (long long i = ((long long)0); i < ((long long)N - 1); i++) { n     long long A, B; n     cin >> A >> B; n     A--; n     B--; n     E[A].push_back(B); n     E[B].push_back(A); n   } n   dfs(0, -1); n   dfs2(0, -1, 1, 0); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int Lpow(long long int a, long long int b) { n   long long int ans = 1; n   while (b > 0) { n     if (b % 2) ans = (ans * a); n     a = (a * a); n     b = b / 2; n   } n   return ans; n } n long long int Lpow(long long int a, long long int b, long long int M) { n   long long int ans = 1; n   while (b > 0) { n     if (b % 2) ans = (ans * a) % M; n     a = (a * a) % M; n     b = b / 2; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n; n   cin >> n; n   vector<int> brr; n   int f1 = 0; n   int f2 = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     int sum = 0; n     int y; n     int sum2 = 0; n     for (int j = 0; j < x; j++) { n       cin >> y; n       if (j > ((x - 1) / 2)) sum += y; n       if (j < (x / 2)) sum2 += y; n       if (x % 2 == 1 && j == x / 2) brr.push_back(y); n     } n     f1 += sum2; n     f2 += sum; n   } n   sort(brr.begin(), brr.end()); n   reverse(brr.begin(), brr.end()); n   for (int i = 0; i < brr.size(); i += 2) f1 += brr[i]; n   for (int i = 1; i < brr.size(); i -= -2) f2 += brr[i]; n   cout << f1 <<     << f2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int MAX_N = 102; n using namespace std; n string s; n int dp[52][11]; n long long numberOfways(int i, int digit) { n   if (i == s.length()) return 1; n   if (dp[i][digit] != -1) return dp[i][digit]; n   dp[i][digit] = 0; n   int curDigit = s[i] -  0 ; n   int newDigit = curDigit + digit; n   if (newDigit % 2) { n     return dp[i][digit] += numberOfways(i + 1, newDigit / 2) + n                            numberOfways(i + 1, newDigit / 2 + 1); n   } else { n     return dp[i][digit] += numberOfways(i + 1, newDigit / 2); n   } n } n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   }; n   memset(dp, -1, sizeof(dp)); n   cin >> s; n   long long ans = 0; n   for (int i = 0; i <= 9; i++) { n     ans += numberOfways(1, i); n   } n   bool ownNumber = true; n   for (int i = 0; i < s.length() - 1; i++) { n     int digit = abs(s[i] - s[i + 1]); n     if (digit > 1) { n       ownNumber = false; n       break; n     } n   } n   if (ownNumber) ans--; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> w(n); n   for (int i = 0; i < n; i++) cin >> w[i]; n   int x = 0; n   vector<int> ord(n); n   vector<int> rd(m); n   vector<bool> put(n, false); n   for (int i = 0; i < m; i++) { n     cin >> rd[i]; n     rd[i]--; n     if (put[rd[i]]) n       continue; n     else { n       ord[x] = rd[i]; n       put[rd[i]] = true; n       x++; n     } n   } n   for (int i = 0; i < n; i++) { n     if (!put[i]) { n       ord[x] = i; n       x++; n     } n   } n   int lift = 0; n   for (int i = 0; i < m; i++) { n     int in = 0; n     for (int j = 0; j < n; j++) { n       if (ord[j] == rd[i]) { n         in = j; n       } n     } n     for (int j = in; j > 0; j--) { n       ord[j] = ord[j - 1]; n       lift += w[ord[j]]; n     } n     ord[0] = rd[i]; n   } n   cout << lift << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n const int rows[5] = {3, 4, 5, 4, 3}; n bool dp[1 << 19], seen[1 << 19]; n pii int_to_pair(int c) { n   pii res = {0, 0}; n   while (rows[res.first] <= c) { n     c -= rows[res.first]; n     res.first++; n   } n   res.second = c; n   return res; n } n int pair_to_int(pii p) { n   int res = 0; n   for (int i = 0; i < p.first; i++) res += rows[i]; n   res += p.second; n   return res; n } n pii next_coords(pii p, int dir) { n   if (dir == 0) { n     return {p.first, p.second - 1}; n   } else if (dir == 1) { n     if (p.first <= 2) { n       return {p.first - 1, p.second - 1}; n     } else { n       return {p.first - 1, p.second}; n     } n   } else if (dir == 2) { n     if (p.first <= 2) { n       return {p.first - 1, p.second}; n     } else { n       return {p.first - 1, p.second + 1}; n     } n   } else if (dir == 3) { n     return {p.first, p.second + 1}; n   } else if (dir == 4) { n     if (p.first >= 2) { n       return {p.first + 1, p.second}; n     } else { n       return {p.first + 1, p.second + 1}; n     } n   } else if (dir == 5) { n     if (p.first >= 2) { n       return {p.first + 1, p.second - 1}; n     } else { n       return {p.first + 1, p.second}; n     } n   } n } n void print_box(int box) { n   int b = 0; n   for (int i = 0; i < 5; i++) { n     for (int j = 0; j < 5 - rows[i]; j++) printf(   ); n     for (int j = 0; j < rows[i]; j++) { n       printf( %c  ,  .O [(box >> b) & 1]); n       b++; n     } n     printf(  n ); n   } n } n bool getdp(int box) { n   if (box == 0) return false; n   if (seen[box]) { n     return dp[box]; n   } n   seen[box] = true; n   for (int i = 0; i < 19; i++) { n     if (!(box & (1 << i))) { n       continue; n     } n     for (int dir = 0; dir < 6; dir++) { n       int boxcpy = box; n       pii curr = int_to_pair(i); n       while (true) { n         if (curr.first < 0) break; n         if (curr.first > 4) break; n         if (curr.second < 0) break; n         if (curr.second >= rows[curr.first]) break; n         if (!(boxcpy & (1 << pair_to_int(curr)))) { n           break; n         } n         boxcpy ^= (1 << pair_to_int(curr)); n         if (!getdp(boxcpy)) { n           dp[box] = true; n           return true; n         } n         curr = next_coords(curr, dir); n       } n     } n   } n   return false; n } n int main() { n   int start_box = 0; n   for (int i = 0; i < 19; i++) { n     char c; n     cin >> c; n     if (c ==  O ) start_box |= 1 << i; n   } n   if (getdp(start_box)) { n     cout <<  Karlsson n ; n   } else { n     cout <<  Lillebror n ; n   } n } n


#include <bits/stdc++.h> n int n, u[1001], v[1001], dem[1001], ans = 0; n using namespace std; n void input() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> u[i] >> v[i]; n   } n } n void output() { cout << ans << endl; } n void solve() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j < n; j++) { n       if (u[j] == i) dem[i]++; n       if (v[j] == i) dem[i]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (dem[i] == 1) ans++; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   input(); n   solve(); n   output(); n } n


#include <bits/stdc++.h> n using namespace std; n char s1[200005], s2[200005]; n int len; n bool judge(int p, char *s1, char *s2) { n   for (int i = 0; i < p; i++) { n     if (s1[i] != s2[i]) return 0; n   } n   return 1; n } n int check(int p, char *s1, char *s2) { n   if (p == 1) { n     if (s1[0] != s2[0]) return 0; n     return 1; n   } n   if (judge(p, s1, s2)) return 1; n   int len = p; n   if (len & 1) return 0; n   return (check(len / 2, s1, s2) && n           check(len / 2, s1 + len / 2, s2 + len / 2)) || n          (check(len / 2, s1 + len / 2, s2) && check(len / 2, s1, s2 + len / 2)); n } n int main(void) { n   int i, j; n   while (~scanf( %s%s , s1, s2)) { n     int len = strlen(s1); n     if (check(len, s1, s2)) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n int main() { n   int n, x, ans; n   cin >> n >> x; n   ans = x; n   a.resize(n); n   for (int i = 0; i < (int)(n); i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < (int)(n); i++) { n     for (int j = i; j < n; j++) { n       ans = max(ans, (x / a[i]) * a[j] + (x % a[i])); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(const pair<long long, long long> &a, n            const pair<long long, long long> &b) { n   if (a.first > b.first) n     return true; n   else if (a.first < b.first) n     return false; n   else { n     if (a.second < b.second) n       return true; n     else n       return false; n   } n } n int main() { n   long long n, k, a, b, i, ans = 0; n   cin >> n >> k; n   vector<pair<long long, long long>> v; n   for (i = 0; i < n; i++) { n     cin >> a >> b; n     v.push_back({a, b}); n   } n   sort(v.begin(), v.end(), check); n   for (i = 0; i < n; i++) { n     if (v[i].first == v[k - 1].first && v[i].second == v[k - 1].second) ans++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native ) n #pragma GCC optimize( fast-math ) n #pragma warning(disable : 4996) n using namespace std; n const int MAXN = 100001; n const int inf = 1000000000; n const long double INF = 1.0 / 0.0; n const long double EPS = 1e-7; n const int mod = inf + 7; n const long double PI = acos(-1.0); n signed main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   ; n   long double d, l, v1, v2; n   cin >> d >> l >> v1 >> v2; n   cout << fixed << (l - d) / (v1 + v2); n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos((long double)-1); n namespace fft { n template <class T> n class comp { n  public: n   T real, img; n   comp(T a = (T)0, T b = (T)0) : real(a), img(b) {} n   comp conj() { return comp(this->real, -(this->img)); } n   comp operator=(const comp& a) { n     this->real = a.real, this->img = a.img; n     return *this; n   } n   comp operator+(const comp& b) { n     return comp(this->real + b.real, this->img + b.img); n   } n   comp operator-(const comp& b) { n     return comp(this->real - b.real, this->img - b.img); n   } n   comp operator*(const T& num) { n     return comp(this->real * num, this->img * num); n   } n   comp operator/(const T& num) { n     return comp(this->real / num, this->img / num); n   } n   comp operator*(const comp& b) { n     return comp(this->real * b.real - this->img * b.img, n                 this->img * b.real + this->real * b.img); n   } n   comp operator/(const comp& b) { n     comp temp(b.real, -b.img); n     comp n = (*this) * temp; n     return n / (b.x * b.x + b.y * b.y); n   } n }; n comp<long double> w[1 << 20]; n int rev[1 << 20]; n void revbits(int newlim) { n   static int lim = -1; n   int t, j; n   if (newlim == lim) return; n   lim = newlim; n   int k = 0; n   while ((1 << k) < newlim) ++k; n   assert((1 << k) == newlim); n   for (int i = 1; i <= (int)(lim); ++i) { n     j = rev[i - 1]; n     t = k - 1; n     while (t >= 0 && ((j >> t) & 1)) j ^= (1 << t), --t; n     if (t >= 0) j ^= (1 << t), --t; n     rev[i] = j; n   } n } n void fft(vector<comp<long double> >& poly, int inv = false) { n   int len, l; n   revbits((int)(poly).size()); n   for (int i = 0; i < (int)((int)(poly).size()); ++i) n     if (rev[i] > i) swap(poly[i], poly[rev[i]]); n   comp<long double> u, v; n   for (len = 2, l = 1; len <= (int)(poly).size(); len += len, l += l) { n     if (w[l].real == 0 && w[l].img == 0) { n       long double ang = PI / l; n       comp<long double> ww(cos(ang), sin(ang)); n       if (l > 1) { n         for (int j = 0; j < l; ++j) { n           if (j & 1) n             w[l + j] = w[(l + j) >> 1] * ww; n           else n             w[l + j] = w[(l + j) >> 1]; n         } n       } else n         w[l] = comp<long double>(1.0, 0.0); n     } n     for (int i = 0; i < (int)(poly).size(); i += len) n       for (int j = 0; j < (int)(l); ++j) { n         u = poly[i + j], n         v = poly[i + j + l] * (inv ? w[l + j].conj() : w[l + j]); n         poly[i + j] = u + v, poly[i + j + l] = u - v; n       } n   } n   if (inv) n     for (auto& x : poly) x = x / (long double)(int)(poly).size(); n } n void fft2d(vector<vector<comp<long double> > >& A, bool inv = false) { n   for (auto& row : A) { n     fft(row, inv); n   } n   vector<comp<long double> > col((int)(A).size()); n   for (int j = 0; j < (int)((int)(A.front()).size()); ++j) { n     for (int i = 0; i < (int)((int)(A).size()); ++i) col[i] = A[i][j]; n     fft(col, inv); n     for (int i = 0; i < (int)((int)(A).size()); ++i) A[i][j] = col[i]; n   } n } n vector<vector<comp<long double> > > multiply( n     vector<vector<comp<long double> > >& x, n     vector<vector<comp<long double> > >& y) { n   int b_rows = 0, b_cols = 0; n   while ((1 << b_rows) <= max((int)(x).size(), (int)(y).size())) ++b_rows; n   ++b_rows; n   while ((1 << b_cols) <= max((int)(x.front()).size(), (int)(y.front()).size())) n     ++b_cols; n   ++b_cols; n   x.resize(1 << b_rows); n   y.resize(1 << b_rows); n   for (auto& row : x) row.resize(1 << b_cols, 0); n   for (auto& row : y) row.resize(1 << b_cols, 0); n   fft2d(x), fft2d(y); n   for (int i = 0; i < (int)((int)(x).size()); ++i) n     for (int j = 0; j < (int)((int)(x[i]).size()); ++j) n       x[i][j] = x[i][j] * y[i][j]; n   fft2d(x, 1); n   return x; n } n const int L = 805; n string A[L], B[L]; n int N, M, R, C, X, Y; n void read() { n   cin >> N >> M; n   for (int i = 0; i < (int)(N); ++i) cin >> A[i]; n   cin >> R >> C; n   for (int i = 0; i < (int)(R); ++i) cin >> B[i]; n } n void solve() { n   X = N + R, Y = M + C; n   vector<vector<comp<long double> > > a(X, vector<comp<long double> >(Y)); n   for (int i = 0; i < (int)(N); ++i) n     for (int j = 0; j < (int)(M); ++j) { n       a[i][j].real = cos(2 * PI * (A[i][j] -  a ) / 26.0), n       a[i][j].img = sin(2 * PI * (A[i][j] -  a ) / 26.0); n       for (int ii = i; ii < X; ii += N) n         for (int jj = j; jj < Y; jj += M) a[ii][jj] = a[i][j]; n     } n   vector<vector<comp<long double> > > b(R, vector<comp<long double> >(C)); n   int valid = 0; n   for (int i = 0; i < (int)(R); ++i) n     for (int j = 0; j < (int)(C); ++j) { n       if (B[i][j] ==  ? ) continue; n       ++valid; n       b[R - i - 1][C - j - 1].real = cos(2 * PI * (B[i][j] -  a ) / 26.0), n                            b[R - i - 1][C - j - 1].img = n                                -sin(2 * PI * (B[i][j] -  a ) / 26.0); n     } n   vector<vector<comp<long double> > > c = multiply(a, b); n   for (int i = 0; i < (int)(N); ++i) { n     for (int j = 0; j < (int)(M); ++j) n       cout << ((int)(c[R - 1 + i][C - 1 + j].real + 1e-5) == valid); n     cout <<   n ; n   } n } n }  // namespace fft n int main() { n   std::ios::sync_with_stdio(false); n   fft::read(); n   fft::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 5005; n const int mod = 1e9 + 7; n const int inf = 1e9 + 120; n int t[nax]; n char sl[nax]; n int pref[nax][nax]; n int nxt[nax][nax]; n int cmp(int i, int j) { n   if (t[i] < t[j]) return -1; n   if (t[i] > t[j]) return 1; n   return 0; n } n int dp(int a, int b) { n   int x = pref[a][b] - pref[a - 1][b]; n   if (x < 0) x += mod; n   return x; n } n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , sl); n   for (int i = (1); i <= ((n)); ++i) t[i] = sl[i - 1] -  0 ; n   for (int a = n; a >= 1; --a) n     for (int b = n; b > a; --b) { n       int& x = nxt[a][b]; n       if (t[a] != t[b]) n         x = a; n       else if (b == n) n         x = inf; n       else n         x = nxt[a + 1][b + 1]; n     } n   for (int i = (1); i <= ((n)); ++i) { n     for (int j = (1); j <= ((n)); ++j) pref[j][i] = 1; n   } n   for (int c = (2); c <= (n); ++c) n     for (int d = (c); d <= (n); ++d) { n       int& x = pref[c][d]; n       int b = c - 1; n       int a = b - (d - c); n       x = pref[b][b] - pref[max(a, 0)][b]; n       if (x < 0) x += mod; n       if (a >= 1) { n         int where = nxt[a][c]; n         if (where < c && t[where] < t[c + where - a]) { n           x += dp(a, b); n           if (x >= mod) x -= mod; n         } n       } n       if (t[c] == 0) x = 0; n       pref[c][d] = x + pref[c - 1][d]; n       if (pref[c][d] >= mod) pref[c][d] -= mod; n     } n   int ans = 0; n   for (int i = (1); i <= ((n)); ++i) ans = (ans + dp(i, n)) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n string to_string(string s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n const int N = 2e3 + 7, M = 1e4, OO = 0x3f3f3f3f; n int vis[N][N]; n int main() { n   int TCF = 1; n   while (TCF--) { n     int n; n     scanf( %d , &n); n     ; n     vector<pair<int, int>> p; n     int x, y; n     for (int i = 0; i < n; ++i) { n       scanf( %d %d , &x, &y); n       p.emplace_back(x, y); n       vis[x + 1000][y + 1000] = 1; n     } n     vector<pair<int, int>> mid; n     int ans = 0; n     for (int i = (0); i < (n); i++) { n       for (int j = (i + 1); j < (n); j++) { n         if (i == j) continue; n         int mx = p[i].first + p[j].first; n         int my = p[i].second + p[j].second; n         if (mx % 2 == 0 && my % 2 == 0) { n           mx /= 2; n           my /= 2; n           if (vis[mx + 1000][my + 1000]) { n             ans++; n           } n         } n       } n     } n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n int main(void) { n   int n, q, a, b; n   scanf( %d%d%d%d , &n, &q, &a, &b); n   vector<int> x(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &x[i]); n   } n   vector<double> y(n); n   vector<int> from(n, -1); n   y[0] = x[0]; n   for (int i = 1; i < n; ++i) { n     double shiftSum = 0.0; n     shiftSum = 0.0; n     double curPos = x[i]; n     int j; n     double shift = 0.0; n     double maxShift = q - curPos; n     double minShift = 1.0 - curPos; n     for (j = i - 1; j >= 0; j--) { n       shift = max(minShift, min(maxShift, shiftSum / (i - j))); n       double pos = curPos + shift; n       if (pos >= y[j] + a - eps && pos <= y[j] + b + eps) break; n       if (pos >= y[j] + b) { n         curPos -= b; n       } else { n         curPos -= a; n       } n       minShift = max(minShift, 1.0 - curPos); n       shiftSum += x[j] - curPos; n     } n     shift = max(minShift, min(maxShift, shiftSum / (i - j))); n     y[i] = x[i] + shift; n     from[i] = j; n   } n   vector<double> ry(n); n   for (int i = n - 1; i >= 0;) { n     double shift = y[i] - x[i]; n     ry[i] = y[i]; n     for (int j = i - 1; j > from[i]; --j) { n       if (ry[j + 1] >= y[j] + b) n         ry[j] = ry[j + 1] - b; n       else n         ry[j] = ry[j + 1] - a; n     } n     i = from[i]; n   } n   double res = 0.0; n   for (int i = 0; i < n; ++i) { n     double d = ry[i] - x[i]; n     res += d * d; n     printf( %.9lf  , ry[i]); n   } n   printf(  n%.9lf n , res); n } n


#include <bits/stdc++.h> n bool IsCircular(const std::string &s, const int n) { n   bool isCircular = true; n   for (int i = 0; i < n; i++) { n     int j = (i + 1) % n; n     isCircular &= n         ((s[i] ==  >  || s[i] ==  - ) && (s[j] ==  >  || s[j] ==  - )); n   } n   if (isCircular) return true; n   isCircular = true; n   for (int i = 0; i < n; i++) { n     int j = (i + 1) % n; n     isCircular &= n         ((s[i] ==  <  || s[i] ==  - ) && (s[j] ==  <  || s[j] ==  - )); n   } n   return isCircular; n } n int main() { n   int t; n   std::cin >> t; n   while (t--) { n     int n; n     std::string s; n     std::cin >> n >> s; n     if (IsCircular(s, n)) n       std::cout << n << std::endl; n     else { n       int ans = 0; n       for (int i = 0; i < n; i++) { n         int j = (i - 1 + n) % n; n         if (s[i] ==  -  || s[j] ==  - ) ans++; n       } n       std::cout << ans << std::endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1 << 18) + 10; n int n, m; n long long tree[N], add[N]; n int same[N]; n int col[N]; n void build(int x, int l, int r) { n   tree[x] = same[x] = col[x] = add[x] = 0; n   if (l == r) { n     same[x] = 1; n     col[x] = l + 1; n     return; n   } n   int s = (l + r) / 2; n   build(x + x + 1, l, s); n   build(x + x + 2, s + 1, r); n } n int push(int x, int l, int r) { n   if (add[x]) { n     tree[x] += (long long)(r - l + 1) * add[x]; n     if (l != r) { n       add[x * 2 + 1] += add[x]; n       add[x * 2 + 2] += add[x]; n     } n     add[x] = 0; n   } n   if (same[x]) { n     if (l != r) { n       same[x * 2 + 1] = same[x * 2 + 2] = 1; n       col[x * 2 + 1] = col[x * 2 + 2] = col[x]; n     } n   } n   return 0; n } n long long upd(int x, int l, int r, int lc, int rc, int y) { n   push(x, l, r); n   if (r < lc || l > rc) return tree[x]; n   if (lc <= l && r <= rc) { n     if (same[x]) { n       add[x] += abs(col[x] - y); n       col[x] = y; n       push(x, l, r); n       return tree[x]; n     } else n       same[x] = 1, col[x] = y; n   } else if (same[x] && col[x] != y) n     col[x] = same[x] = 0; n   int s = (l + r) / 2; n   return tree[x] = upd(x + x + 1, l, s, lc, rc, y) + n                    upd(x + x + 2, s + 1, r, lc, rc, y); n } n long long get(int x, int l, int r, int lc, int rc) { n   push(x, l, r); n   if (r < lc || l > rc) return 0; n   if (lc <= l && r <= rc) return tree[x]; n   int s = (l + r) / 2; n   return get(x + x + 1, l, s, lc, rc) + get(x + x + 2, s + 1, r, lc, rc); n } n int main() { n   scanf( %d%d , &n, &m); n   build(0, 0, n - 1); n   int t, l, r, x; n   for (int i = 0; i < m; i++) { n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d%d%d , &l, &r, &x); n       l--; n       r--; n       upd(0, 0, n - 1, l, r, x); n     } else { n       scanf( %d%d , &l, &r); n       l--; n       r--; n       printf( %I64d n , get(0, 0, n - 1, l, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  *  && i + 4 <= s.size()) { n       for (long long j = 1; j < s.size(); j++) { n         if (i + 4 * j <= s.size()) { n           if (s[i] ==  *  && s[i + j] ==  *  && s[i + 2 * j] ==  *  && n               s[i + 3 * j] ==  *  && s[i + 4 * j] ==  * ) { n             cout <<  yes ; n             return 0; n           } n         } n       } n     } n   } n   cout <<  no ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1E6 + 50; n const long long p = 1E9 + 7; n int n, k; n long long ans, sum, f[N], s[N], fac[N], inv[N]; n inline void upd(long long &a, long long b) { n   a += b; n   if (a >= p) a -= p; n } n inline void sub(long long &a, long long b) { n   a -= b; n   if (a < 0) a += p; n } n long long Pow(long long a, long long b = p - 2) { n   long long res = 1; n   for (; b; b >>= 1, a = a * a % p) n     if (b & 1) res = res * a % p; n   return res; n } n void init() { n   fac[0] = inv[0] = 1; n   for (register int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % p; n   inv[n] = Pow(fac[n]); n   for (register int i = n - 1; i; --i) inv[i] = inv[i + 1] * (i + 1) % p; n } n int main() { n   scanf( %d%d , &n, &k); n   if (k >= n) return 0 * printf( 0 n ); n   init(); n   f[0] = s[0] = 1; n   for (register int i = 1; i <= k; ++i) { n     f[i] = fac[i], s[i] = 1; n     upd(sum, s[i]); n   } n   for (register int i = k + 1; i <= n; ++i) { n     f[i] = sum * fac[i - 1] % p; n     s[i] = f[i] * inv[i] % p; n     upd(sum, s[i]), sub(sum, s[i - k]); n   } n   for (int i = 0; i < n - k; ++i) sub(ans, s[i]); n   ans = ans * fac[n - 1] % p; n   sub(ans, f[n]); n   upd(ans, fac[n]); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   int n, res = 0, t, b; n   cin >> n; n   while (n) { n     res++; n     t = n; n     b = -1; n     while (t) { n       b = max(b, t % 10); n       t /= 10; n     } n     n -= b; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, a[55], P; n double dp[55][55][55]; n double fac(int n) { n   double ans = 1; n   for (int i = 0; i < (n); i++) ans *= (i + 1); n   return ans; n } n int main(int argc, char *argv[]) { n   scanf( %d , &N); n   for (int i = 0; i < (N); i++) scanf( %d , a + i); n   scanf( %d , &P); n   dp[0][0][0] = 1; n   for (int i = 0; i < (N); i++) n     for (int in = (0); in <= (N); in++) n       for (int p = (0); p <= (P); p++) n         if (dp[i][in][p]) { n           dp[i + 1][in][p] += dp[i][in][p]; n           if (p + a[i] <= P) dp[i + 1][in + 1][p + a[i]] += dp[i][in][p]; n         } n   double tmp = 0, ans = 0, sum; n   for (int in = (N); in >= (0); in--) { n     sum = 0; n     for (int p = (0); p <= (P); p++) sum += dp[N][in][p]; n     sum *= fac(in) * fac(N - in); n     ans += in * (sum - tmp); n     tmp = sum; n   } n   printf( %.9lf n , ans / fac(N)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n const int LG = 27; n const int INF = 1e9 + 7; n pair<int, int> seg[LG][N]; n int mx[LG][N]; n int r[N], a[N], sol[N]; n pair<int, int> p[N]; n int n, k; n void build(int l = 0, int r = n, int d = 0) { n   if (r - l == 1) { n     seg[d][l] = make_pair(p[l].second, l); n     mx[d][l] = sol[l]; n     return; n   } n   int mid = (l + r) / 2; n   build(l, mid, d + 1); n   build(mid, r, d + 1); n   merge(seg[d + 1] + l, seg[d + 1] + mid, seg[d + 1] + mid, seg[d + 1] + r, n         seg[d] + l); n   int out = 0; n   for (int i = r - 1; i >= l; i--) { n     out = max(out, sol[seg[d][i].second]); n     mx[d][i] = out; n   } n } n pair<int, int> get(int l, int r, int val, int s = 0, int e = n, int d = 0) { n   if (l >= e || r <= s) return make_pair(0, 0); n   if (l <= s && e <= r) { n     int pos = lower_bound(seg[d] + s, seg[d] + e, make_pair(val, 0)) - seg[d]; n     int pos2 = n         lower_bound(seg[d] + s, seg[d] + e, make_pair(val + 1, 0)) - seg[d]; n     int ans1 = 0; n     if (pos < e) ans1 = mx[d][pos]; n     return make_pair(ans1, pos2 - s); n   } n   int mid = (s + e) / 2; n   pair<int, int> a = get(l, r, val, s, mid, d + 1); n   pair<int, int> b = get(l, r, val, mid, e, d + 1); n   return make_pair(max(a.first, b.first), a.second + b.second); n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &r[i]); n     p[i].second = r[i]; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     p[i].first = a[i]; n   } n   sort(p, p + n); n   build(); n   int L = 0, R = 0; n   for (int i = 0; i < n; i++) { n     while (L < n && p[L].first + k < p[i].first) L++; n     while (R < n && p[i].first + k >= p[R].first) R++; n     sol[i] = get(L, R, p[i].second).second; n   } n   build(); n   int m; n   cin >> m; n   while (m--) { n     int x, y; n     scanf( %d %d , &x, &y); n     x--, y--; n     if (a[x] > a[y]) swap(x, y); n     int L = lower_bound(p, p + n, make_pair(a[y] - k, 0)) - p; n     int R = lower_bound(p, p + n, make_pair(a[x] + k + 1, 0)) - p; n     int ans = get(L, R, max(r[x], r[y])).first; n     if (ans < 2) n       printf( -1 n ); n     else n       printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename t> n inline void read(t &x) { n   x = 0; n   char c = getchar(), f = 0; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = 1; n   for (; c >=  0  && c <=  9 ; c = getchar()) n     x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n } n int n, rs[100005], tp; n char vs[100005]; n inline void dfs(int x) { n   if (vs[x]) n     return; n   else n     vs[x] = 1, dfs(x * 2 % n), dfs((x * 2 + 1) % n), rs[++tp] = x; n } n int main() { n   read(n); n   if (n & 1) return puts( -1 ), 0; n   dfs(0); n   while (tp) printf( %d  , rs[tp--]); n   return printf( 0 n ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, v, arr[1000], c = 0; n   cin >> x >> v; n   for (int i = 0; i < x - 1; i++) { n     cin >> arr[i]; n   } n   while (1) { n     sort(arr, arr + x - 1); n     if (v > arr[x - 2]) break; n     arr[x - 2]--; n     v++; n     c++; n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f(char c) { return c - 48; } n void soroban(int n, char *ans) { n   if (n < 5) { n     ans[0] =  O ; n     ans[1] =  - ; n     ans[2] =  | ; n     for (int i = 3; i <= 7; i++) { n       if (i == n + 3) { n         ans[i] =  - ; n       } else { n         ans[i] =  O ; n       } n     } n   } else { n     ans[0] =  - ; n     ans[1] =  O ; n     ans[2] =  | ; n     for (int i = 3; i <= 7; i++) { n       if (i == n - 2) { n         ans[i] =  - ; n       } else { n         ans[i] =  O ; n       } n     } n   } n   ans[8] =   0 ; n } n int main() { n   char ch[10], ans[10]; n   cin >> ch; n   for (int i = strlen(ch) - 1; i >= 0; i--) { n     soroban(f(ch[i]), ans); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[5005], n, k, L[5005][5005], S[5005], E[5005]; n void doc() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &A[i]); n   } n } n void xuly() { n   sort(A + 1, A + n + 1); n   for (int i = 1; i <= n; i++) { n     S[i] = 1; n     E[i] = i; n     for (int j = i - 1; j > 0; j--) n       if (A[i] - A[j] <= 5) { n         S[i]++; n         E[i] = j; n       } else n         break; n   } n   for (int i = 1; i <= k; i++) { n     for (int j = 1; j <= n; j++) { n       L[i][j] = max(max(L[i][j - 1], L[i][j]), L[i - 1][E[j] - 1] + S[j]); n     } n   } n   cout << L[k][n]; n } n int main() { n   doc(); n   xuly(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const long long mod = 998244353; n const int maxn = 3005; n int dp[maxn][maxn]; n int dp2[maxn][maxn]; n long long add(long long a, long long b) { return (a + b) % mod; } n long long mul(long long a, long long b) { return (a * b) % mod; } n long long bin_pow(long long a, long long b) { n   long long res = 1; n   long long tmp = a; n   while (b) { n     if (b & 1) { n       res = mul(res, tmp); n     } n     tmp = mul(tmp, tmp); n     b >>= 1; n   } n   return res; n } n long long inv(long long a) { return bin_pow(a, mod - 2); } n long long min(long long a, long long b) { n   if (a > b) n     return b; n   else n     return a; n } n void solve() { n   int n, m; n   cin >> n >> m; n   long long n_good = 0, n_bad = 0; n   vector<bool> flags(n); n   for (int i = 0; i < n; i++) { n     int ai; n     cin >> ai; n     flags[i] = ai == 1; n   } n   vector<long long> weights(n); n   for (int i = 0; i < n; i++) { n     int w; n     cin >> w; n     weights[i] = w; n     if (flags[i]) { n       n_good += w; n     } else { n       n_bad += w; n     } n   } n   for (int i = m; i >= 0; i--) { n     dp[i][m - i] = dp2[i][m - i] = 1; n     for (int j = min(m - i - 1, (long long)n_bad); j >= 0; j--) { n       long long total = inv(n_good + n_bad + i - j); n       long long tmp = mul(dp[i + 1][j], n_good + i + 1); n       long long tmp2 = mul(dp[i][j + 1], n_bad - j); n       long long tmp3 = add(tmp, tmp2); n       dp[i][j] = mul(tmp3, total); n       tmp = mul(dp2[i + 1][j], n_good + i); n       tmp2 = mul(dp2[i][j + 1], n_bad - j - 1); n       tmp3 = add(tmp, tmp2); n       dp2[i][j] = mul(tmp3, total); n     } n   } n   for (int i = 0; i < n; i++) { n     if (flags[i]) { n       cout << mul(weights[i], dp[0][0]); n     } else { n       cout << mul(weights[i], dp2[0][0]); n     } n     cout << endl; n   } n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int oo = (1e9) + 7; n int main() { n   long long n; n   cin >> n; n   vector<long long> b(n / 2 + 5, 0); n   for (int i = 1; i <= n / 2; i++) { n     scanf( %lld , &b[i]); n   } n   vector<long long> out(n + 5, 0); n   long long l = 0; n   long long r = 0; n   for (int i = 1; i <= n / 2; i++) { n     out[i] = 0; n     out[n - i + 1] = b[i]; n     if (i > 1) { n       if (b[i] > r) { n         out[i] = b[i] - r; n         out[n - i + 1] = r; n       } n       long long tmp = b[i]; n       if (out[i] < l) { n         out[i] = l; n         out[n - i + 1] = b[i] - l; n       } n     } n     l = out[i]; n     r = out[n - i + 1]; n   } n   for (int i = 1; i <= n; i++) { n     printf( %lld  , out[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T sqr(const T& a) { n   return a * a; n } n template <typename T> n inline int sign(const T& a) { n   return a < 0 ? -1 : a > 0; n } n void task(); n int main() { n   task(); n   return 0; n } n vector<int> z_function(string s) { n   int n = (int)s.length(); n   vector<int> z(n); n   for (int i = 1, l = 0, r = 0; i < n; ++i) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n   return z; n } n int n, k; n string s; n int ans[int(2e6) + 10]; n int add[int(2e6) + 10]; n int sub[int(2e6) + 10]; n void task() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   cin >> s; n   auto z = z_function(s); n   z[0] = s.length(); n   (void(1)); n   for (int len = 1; len <= n; ++len) { n     for (int i = 0, step = 0; i < n; i += len, ++step) { n       if (z[i] < len) break; n       if (step == k - 1) { n         (void(1)); n         add[i + len - 1]++; n         sub[i + len - 1 + min(len, z[i + len])]++; n         break; n       } n     } n   } n   int cur = 0; n   for (int i = 0; i < n; ++i) { n     cur += add[i]; n     ans[i] = cur > 0; n     cur -= sub[i]; n   } n   for (int i = 0; i < n; ++i) cout << ans[i]; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fi( n      easy  n      .inp ); n ofstream fo( n      easy  n      .out ); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<string> s; n   for (int i = 0; i < n; i++) { n     string x; n     cin >> x; n     s.push_back(x); n   } n   vector<string> a(n); n   for (int i = 0; i < n; i++) { n     string tmp =   ; n     for (int j = 0; j < m; j++) { n       tmp += (j & 1 ? ( Z  - s[i][j]) : (s[i][j] -  A )) +  A ; n     } n     a[i] = tmp; n   } n   vector<pair<string, int>> mp; n   for (int i = 0; i < n; i++) { n     mp.push_back({a[i], i}); n   } n   sort((mp).begin(), (mp).end()); n   for (int i = 0; i < n; i++) { n     cout << mp[i].second + 1 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 300000 + 10; n long long n; n long long ans[maxn], tot, vis[maxn], last; n int main() { n   while (scanf( %lld , &n) != EOF) { n     memset(vis, 0, sizeof(vis)); n     memset(ans, 0, sizeof(ans)); n     for (int i = 0; i < n; i++) scanf( %lld , &ans[i]); n     last = n; n     tot = 0; n     printf( 1 ); n     for (int i = 0; i < n; i++) { n       vis[ans[i]] = 1; n       tot++; n       while (vis[last] > 0) { n         tot--; n         last--; n       } n       printf(  %lld , tot + 1); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( sse4,avx ) n int getNimb(int c, int64_t forbidden_mask, n             std::map<std::pair<int, int64_t>, int>& map) { n   auto it = map.find(std::pair<int, int64_t>{c, forbidden_mask}); n   if (it != map.end()) { n     return it->second; n   } n   if (c == 0) { n     return 0; n   } n   std::vector<int> other; n   for (int i = 1; i <= c; i++) { n     if ((forbidden_mask & (1 << i)) == 0) { n       other.push_back(getNimb(c - i, forbidden_mask + (1 << i), map)); n     } n   } n   std::sort(other.begin(), other.end()); n   if (other.empty()) { n     return 0; n   } n   int j = 0; n   for (size_t i = 0; i < other.size(); i++) { n     if (j >= other.size() || other[j] != i) { n       map[std::pair<int, int64_t>(c, forbidden_mask)] = i; n       return i; n     } n     while (j < other.size() && other[j] == i) j++; n   } n   map[std::pair<int, int64_t>(c, forbidden_mask)] = other.size(); n   return other.size(); n } n int data[] = {0, 1, 1, 2, 2, 2, 3, 3,  3,  3,  4,  4,  4, 4, 4, 5, n               5, 5, 5, 5, 5, 6, 6, 6,  6,  6,  6,  6,  7, 7, 7, 7, n               7, 7, 7, 7, 8, 8, 8, 8,  8,  8,  8,  8,  8, 9, 9, 9, n               9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10}; n void run(std::istream& in, std::ostream& out) { n   int n; n   in >> n; n   int res = 0; n   for (int i = 0; i < n; i++) { n     int x; n     in >> x; n     res ^= data[x]; n   } n   if (res == 0) { n     out <<  YES  << std::endl; n   } else { n     out <<  NO  << std::endl; n   } n } n int main() { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   run(std::cin, std::cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int z[100001]; n int main() { n   string s; n   int T, a, b; n   cin >> s; n   for (a = 1; a < s.size(); a++) { n     z[a] = z[a - 1] + (s[a] == s[a - 1]); n   } n   for (cin >> T; T--; cout << z[b - 1] - z[a - 1] << endl) cin >> a >> b; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   bool taken[n + 1]; n   for (int i = 0; i < n + 1; i++) taken[i] = 0; n   int cc = 0; n   for (int i = 1; i < n + 1; i++) { n     for (int j = 1; j <= n; j++) { n       int a; n       cin >> a; n       if (a == 2 && !taken[j]) taken[j] = 1, cc++; n       if (a == 3) { n         if (!taken[i]) taken[i] = 1, cc++; n         if (!taken[j]) taken[j] = 1, cc++; n       } n     } n   } n   cout << n - cc << endl; n   for (int i = 1; i < n + 1; i++) n     if (!taken[i]) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MXN = 100 + 10; n const long long Mod = 1e9 + 7; n void mkay(long long &x) { n   if (x >= Mod) x -= Mod; n } n long long n, k; n long long sub[MXN], DP[MXN]; n long long dp[MXN][MXN][2], pd[MXN][2]; n vector<long long> adj[MXN]; n vector<pair<long long, long long>> A, B; n void DFS(long long u, long long par) { n   sub[u] = dp[u][k][0] = dp[u][0][1] = 1; n   for (auto v : adj[u]) { n     if (v == par) continue; n     DFS(v, u); n     A.clear(), B.clear(); n     for (int i = 0; i < sub[u]; i++) A.emplace_back(i, 1); n     for (int i = 0; i < sub[v]; i++) B.emplace_back(i, 1); n     for (long long i = max(0ll, k - sub[u]); i <= k; i++) A.emplace_back(i, 0); n     for (long long i = max(0ll, k - sub[v]); i <= k; i++) B.emplace_back(i, 0); n     for (auto Pr0 : A) { n       long long x, a, f, s; n       tie(x, a) = Pr0; n       for (auto Pr1 : B) { n         long long y, b; n         tie(y, b) = Pr1; n         if (a == 0 && b == 0) { n           f = max(x, y - 1), s = 0; n         } else if (a == 0 && b == 1) { n           if (x - 1 >= y) n             f = x, s = 0; n           else n             f = y + 1, s = 1; n         } else if (a == 1 && b == 0) { n           if (y - 1 >= x) n             f = y - 1, s = 0; n           else n             f = x, s = 1; n         } else { n           f = max(x, y + 1), s = 1; n         } n         mkay(pd[f][s] += dp[u][x][a] * dp[v][y][b] % Mod); n       } n     } n     sub[u] += sub[v]; n     for (int f = 0; f <= sub[u]; f++) dp[u][f][1] = pd[f][1], pd[f][1] = 0; n     for (int f = max(0ll, k - sub[u]); f <= k; f++) n       dp[u][f][0] = pd[f][0], pd[f][0] = 0; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i < n; i++) { n     long long u, v; n     cin >> u >> v; n     adj[u].push_back(v), adj[v].push_back(u); n   } n   DFS(1, 0); n   for (int f = 0; f <= k; f++) { n     mkay(DP[k] += dp[1][f][0]); n   } n   cout << DP[k] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, p[200100], c[200100], ans = 100100; n bool hamin[200100]; n vector<vector<long long> > cycle; n bool same_color(long long ind, long long start, long long dist) { n   long long asdf = (start + dist) % cycle[ind].size(); n   while (asdf != start) { n     if (c[cycle[ind][start]] != c[cycle[ind][asdf]]) return false; n     asdf = (asdf + dist) % cycle[ind].size(); n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> t; n   while (t--) { n     ans = 100000000; n     cycle.clear(); n     cin >> n; n     for (int i = 1; i <= n; i++) hamin[i] = false; n     for (int i = 1; i <= n; i++) cin >> p[i]; n     for (int i = 1; i <= n; i++) cin >> c[i]; n     for (int i = 1; i <= n; i++) { n       if (hamin[i]) continue; n       long long start = i; n       vector<long long> fuck; n       fuck.push_back(start); n       hamin[start] = true; n       while (p[start] != fuck[0]) { n         start = p[start]; n         fuck.push_back(start); n         hamin[start] = true; n       } n       cycle.push_back(fuck); n     } n     for (long long i = 0; i < cycle.size(); i++) { n       for (long long j = 1; j <= cycle[i].size(); j++) { n         if (cycle[i].size() % j) continue; n         for (long long z = 0; z < j; z++) n           if (same_color(i, z, j)) ans = min(ans, j); n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool comprobarNum(vector<int> aux, int num) { n   for (int i = 0; i < aux.size(); i++) { n     if (aux[i] == num) { n       return true; n     } n   } n   return false; n } n int main() { n   int n, aux; n   cin >> n; n   vector<int> s; n   vector<int> numR; n   int contador = 1; n   while (n--) { n     cin >> aux; n     if (comprobarNum(s, aux)) { n       numR.push_back(aux); n     } else { n       s.push_back(aux); n     } n   } n   int max = 1; n   for (int i = 0; i < numR.size(); i++) { n     for (int j = 0; j < numR.size(); j++) { n       if (numR[j] == numR[i]) { n         contador++; n       } n     } n     if (contador > max) max = contador; n     contador = 1; n   } n   cout << max <<     << s.size(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   double n, R, r; n   cin >> n >> R >> r; n   if (r > R) return cout <<  NO , 0; n   if (2 * r == R) { n     if (n <= 2) n       return cout <<  YES , 0; n     else n       return cout <<  NO , 0; n   } n   if (2 * r > R) { n     if (n <= 1) n       return cout <<  YES , 0; n     else n       return cout <<  NO , 0; n   } n   if (2 * r < R) { n     double h = R - r; n     double t = atan2(r, (double)sqrt(h * h - r * r)); n     if (t * n - PI > 0.0000000000000001) n       cout <<  NO ; n     else n       cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, m, n, rev_dig[25], cnt, non_zero, anscnt; n long long x, ans1[100005], ans2[100005], ans3[100005]; n long long getinv(long long a, long long p) { n   if (a == 1) return 1; n   long long k = a - getinv(p % a, a); n   return (k * p + 1) / a; n } n void answer(long long a, long long b, int ty) { n   anscnt++; n   ans1[anscnt] = a; n   ans2[anscnt] = b; n   ans3[anscnt] = ty; n } n void getmul(int t, long long x) { n   long long temp = 1; n   long long xx = x; n   while (temp < t) { n     answer(xx, xx, 1); n     xx <<= 1; n     temp <<= 1; n   } n   temp = 1; n   long long now = 0; n   while (temp <= t) { n     if (temp & t) { n       if (now > 0) { n         answer(temp * x, now, 1); n         now += temp * x; n       } else { n         now = temp * x; n       } n     } n     temp <<= 1; n   } n } n int main() { n   anscnt = 0; n   scanf( %lld , &x); n   if (x % 4 == 1) { n     answer(x, x, 1); n     answer(x, x * 2, 1); n     x *= 3; n   } n   cnt = 0; n   long long xx = x; n   while (xx > 0) { n     cnt++; n     rev_dig[cnt] = xx & 1; n     xx >>= 1; n   } n   non_zero = 0; n   for (i = 1; i <= cnt; i++) { n     if (rev_dig[i] == 0) { n       non_zero = i; n       break; n     } n   } n   if (non_zero == 0) { n     cnt++; n     rev_dig[cnt] = 0; n     non_zero = cnt; n   } n   long long xinv = getinv(x, 1 << cnt); n   long long r1 = x - 1; n   long long t1 = r1 * xinv % (1 << cnt); n   getmul(t1, x); n   long long b = t1 * x; n   answer(b, x, 2); n   long long c = b ^ x; n   answer(x, c, 1); n   long long d = (x + c); n   answer(b, d, 2); n   long long e = b ^ d; n   if (e != 2) { n     long long r2 = x - 2; n     long long t2 = r2 * xinv % (1 << cnt); n     getmul(t2, x); n     long long b2 = t2 * x; n     answer(b2, x, 2); n     long long c2 = b2 ^ x; n     answer(x, c2, 1); n     long long d2 = (x + c2); n     answer(b2, d2, 2); n     long long e2 = b2 ^ d2; n     answer(e, e2, 2); n   } n   e = 2; n   while (e <= x) { n     answer(e, e, 1); n     if (e & x) { n       answer(e, x, 2); n       x = e ^ x; n     } n     e <<= 1; n   } n   printf( %d n , anscnt); n   for (i = 1; i <= anscnt; i++) { n     if (ans3[i] == 1) { n       printf( %lld + %lld n , ans1[i], ans2[i]); n     } else { n       printf( %lld ^ %lld n , ans1[i], ans2[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   cin.sync_with_stdio(0); n   cout.sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, r[100], b[100], sumr, sumb, ans = 1, check = 1; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> r[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   sumr = accumulate(r, r + n, 0); n   sumb = accumulate(b, b + n, 0); n   if ((sumr == sumb && sumb == 0) || (sumr == sumb && sumb == n)) { n     cout << -1; n     return 0; n   } n   if (sumr > sumb) { n     cout << 1; n     return 0; n   } n   for (int i = 0; i < n; i++) n     if (r[i] && !b[i]) { n       check = 0; n       break; n     } n   if (check) { n     cout << -1; n     return 0; n   } n RELOOP:; n   ans++; n   for (int i = 0; i < n; i++) n     if (r[i] && !b[i]) sumr++; n   if (sumr <= sumb) goto RELOOP; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, r; n long long A[1000005], sum; n int main() { n   scanf( %d %d , &n, &r); n   for (int i = (0); i < (1 << n); i++) scanf( %lld , &A[i]), sum += A[i]; n   printf( %.12lf n , 1.0 * sum / (1 << n)); n   for (int i = (0); i < (r); i++) { n     int x; n     long long y; n     scanf( %d %lld , &x, &y); n     sum += y - A[x]; n     A[x] = y; n     printf( %.12lf n , 1.0 * sum / (1 << n)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   vector<int> a; n   int n, t; n   cin >> n; n   a.reserve(n); n   while (n--) { n     cin >> t; n     if (t != 0) { n       a.push_back(t); n     } n   } n   sort(a.begin(), a.end()); n   cout << unique(a.begin(), a.end()) - a.begin(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(2e5); n int g[N]; n int ans = 0; n int n; n bool used[N]; n void bfs(int start) { n   queue<int> q; n   q.push(start); n   used[start] = true; n   while (!q.empty()) { n     int from = q.front(); n     q.pop(); n     int to = g[from]; n     if (!used[to]) { n       used[to] = true; n       q.push(to); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> g[i]; n     g[i]--; n   } n   int c = 0; n   for (int i = 0, t; i < n; ++i) { n     cin >> t; n     c += t; n   } n   ans += !(bool(c & 1)); n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     if (!used[i]) { n       cnt++; n       bfs(i); n     } n   } n   ans += (cnt == 1 ? 0 : cnt); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, ans; n   int d; n   char c; n   cin >> n >> c; n   m = n - 1; n   if (c ==  f ) { n     d = 1; n   } n   if (c ==  e ) { n     d = 2; n   } n   if (c ==  d ) { n     d = 3; n   } n   if (c ==  a ) { n     d = 4; n   } n   if (c ==  b ) { n     d = 5; n   } n   if (c ==  c ) { n     d = 6; n   } n   if (m % 4 == 0 || m % 4 == 2) n     ans = (m / 4) * 16 + d; n   else n     ans = (m / 4) * 16 + 7 + d; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cnt[10]; n long long w, ans; n void dfs(int pos, long long c) { n   long long rec; n   if (pos > 8) { n     ans = max(ans, w - c); n     return; n   } n   rec = min(cnt[pos], c / pos); n   for (long long i = rec; i >= max(rec - 8, 0LL); i--) n     dfs(pos + 1, c - i * pos); n } n int main() { n   cin >> w; n   for (int i = 1; i <= 8; i++) cin >> cnt[i]; n   ans = 0; n   dfs(1, w); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 300000; n vector<int> g[MAX_N]; n int sz[MAX_N]; n int x[MAX_N]; n int p[MAX_N]; n int N; n int dfs(int v) { n   sz[v] = 1; n   p[v] = N; n   x[N++] = v; n   for (int i = 0; i < g[v].size(); i++) { n     sz[v] += dfs(g[v][i]); n   } n   return sz[v]; n } n int main() { n   int n, q, a, b; n   cin >> n >> q; n   for (int i = 1; i < n; i++) { n     cin >> a; n     g[a - 1].push_back(i); n   } n   for (int i = 0; i < n; i++) sort(g[i].begin(), g[i].end()); n   N = 0; n   dfs(0); n   for (int i = 0; i < q; i++) { n     cin >> a >> b; n     a--; n     if (sz[a] < b) n       cout << -1 << endl; n     else n       cout << x[p[a] + b - 1] + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n long long pr[300005], cr[300005]; n long long nr[300005][2], fr[300005][2]; n bool visit[300005], visit1[300005]; n char str[300005]; n vector<long long> vr[300005], br[300005]; n void pre(), dfs(long long, long long); n long long uni(long long, long long), ccon(long long), ffind(long long); n int main() { n   pre(); n   for (int i = 1; i < k + 1; i++) n     if (!visit[i]) dfs(i, 0); n   long long ans = 0; n   for (int idx = 1; idx < n + 1; idx++) { n     if (br[idx].size() == 1) { n       long long v1 = br[idx][0]; n       ans -= ccon(v1); n       if (str[idx] ==  0 ) { n         fr[ffind(v1)][cr[v1]] = 1; n       } else { n         fr[ffind(v1)][!cr[v1]] = 1; n       } n       ans += ccon(v1); n     } else if (br[idx].size() == 2) { n       long long v1 = br[idx][0], v2 = br[idx][1]; n       if (ffind(v1) != ffind(v2)) { n         ans -= ccon(v1) + ccon(v2); n         long long p = uni(ffind(v1), ffind(v2)); n         ans += ccon(p); n       } n     } n     cout << ans <<   n ; n   } n } n long long ffind(long long idx) { n   if (pr[idx] != idx) return pr[idx] = ffind(pr[idx]); n   return idx; n } n long long uni(long long x, long long y) { n   if (x == y) return x; n   if (nr[x][0] + nr[x][1] < nr[y][0] + nr[y][1]) swap(x, y); n   pr[y] = x; n   nr[x][0] += nr[y][0]; n   nr[x][1] += nr[y][1]; n   fr[x][0] += fr[y][0]; n   fr[x][1] += fr[y][1]; n   return x; n } n long long ccon(long long idx) { n   long long c = cr[idx]; n   long long p = ffind(idx); n   if (fr[p][0]) return nr[p][0]; n   if (fr[p][1]) return nr[p][1]; n   return min(nr[p][0], nr[p][1]); n } n void pre() { n   cin >> n >> k; n   cin >> str + 1; n   for (int i = 1; i < k + 1; i++) { n     long long s; n     cin >> s; n     for (int j = 0; j < s; j++) { n       long long x; n       cin >> x; n       vr[i].push_back(x); n       br[x].push_back(i); n     } n   } n } n void dfs(long long idx, long long t) { n   if (visit[idx]) return; n   visit[idx] = true; n   nr[idx][t] = 1; n   cr[idx] = t; n   pr[idx] = idx; n   for (int i = 0; i < vr[idx].size(); i++) { n     long long v = vr[idx][i]; n     for (int j = 0; j < br[v].size(); j++) { n       long long node = br[v][j]; n       dfs(node, str[v] ==  1  ? t : !t); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<string> s; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   s.resize(n); n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = n - 1; i > 0; i--) { n     int l = 1, r = min(s[i].length(), s[i - 1].length()); n     if (s[i - 1].compare(s[i]) < 0) continue; n     while (l < r) { n       int mid = l + r + 1 >> 1; n       if (s[i - 1].compare(0, mid, s[i]) <= 0) n         l = mid; n       else n         r = mid - 1; n     } n     s[i - 1] = s[i - 1].substr(0, l); n   } n   for (int i = 0; i < n; i++) cout << s[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline long long read() { n   long long x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(long long a) { n   if (a < 0) { n     a = -a; n     putchar( - ); n   } n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void writeln(long long a) { n   write(a); n   puts(  ); n } n inline void wri(long long a) { n   write(a); n   putchar(   ); n } n const int N = 205; n const long long inf = 4e18; n int n, m; n long long f[N][N], k, g[N][N]; n string F[N]; n long long sol(string x) { n   int len = x.length(); n   x =     + x; n   int p = 0; n   memset(f, 0, sizeof(f)); n   for (int i = 1; i <= len; i++) x[i] -=  0 ; n   f[0][x[1]] = 1; n   for (int i = 1; i <= len; i++) { n     if (i > 1) p = f[p][x[i]]; n     if (i < len && x[i + 1] == 0) n       f[i][0] = i + 1; n     else n       f[i][0] = f[p][0]; n     if (i < len && x[i + 1] == 1) n       f[i][1] = i + 1; n     else n       f[i][1] = f[p][1]; n   } n   for (int i = 0; i <= len; i++) { n     g[i][0] = f[i][0] == len; n     g[i][1] = f[i][1] == len; n   } n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j <= len; j++) { n       f[j][i] = f[f[j][i - 2]][i - 1]; n       g[j][i] = min(inf, g[j][i - 2] + g[f[j][i - 2]][i - 1]); n     } n   } n   return g[0][n]; n } n int main() { n   n = read(); n   k = read(); n   m = read(); n   string s =   ; n   F[0] =  0 ; n   F[1] =  1 ; n   for (int i = 2; i <= n; i++) { n     F[i] = F[i - 2] + F[i - 1]; n     if (F[i].length() > m) F[i] = F[i].substr(F[i].length() - m, m); n   } n   m = min(m, (int)F[n].length()); n   for (int i = 1; i <= m; i++) { n     if (s.length() && n         F[n].substr(F[n].length() - s.length(), s.length()) == s) { n       if (--k == 0) { n         cout << s << endl; n         return 0; n       } n     } n     s +=  0 ; n     long long t = sol(s); n     if (t < k) { n       k -= t; n       s.pop_back(); n       s +=  1 ; n     } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t, i, j, k, z, x, num[2]; n   string s; n   cin >> t; n   while (t--) { n     cin >> s; n     memset(num, 0, sizeof(num)); n     for (i = 0; i < s.size(); i++) { n       if (s[i] ==  1 ) n         num[1]++; n       else n         num[0]++; n     } n     z = min(num[0], num[1]); n     if (z % 2) n       cout <<  DA n ; n     else n       cout <<  NET n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const MAX = 2005; n int mp[MAX][MAX]; n int main() { n   int n; n   bool flag = true; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &mp[i][j]); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (mp[i][i] || mp[i][j] != mp[j][i] || (i != j && !mp[i][j])) { n         printf( NO n ); n         return 0; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     int u = 1; n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       if (mp[i][j] < mp[i][u]) u = j; n     } n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       if (abs(mp[i][j] - mp[j][u]) != mp[i][u]) { n         printf( NO n ); n         return 0; n       } n     } n   } n   printf( YES n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXPOW = 20; n const int MAXA = 1048577; n const int MAXN = 100009; n const int MAXM = 100009; n const int INF = 1e9 + 7; n struct Node { n   Node(int val) : val(val) {} n   int val; n }; n struct MinIntervalTree { n   MinIntervalTree(int n, int val) { n     N = 1; n     while (N < n) { n       N *= 2; n     } n     nodes.resize(N * 2 + 2, Node(val)); n   } n   int getMin(int L, int R) { return getMin(0, 0, N, L, R + 1); } n   void set(int x, int val) { set(0, 0, N, x, val); } n  n  private: n   int getMin(int v, int L, int R, int A, int B) { n     if (L == A && R == B) { n       return nodes[v].val; n     } n     int M = (L + R) / 2; n     if (B <= M) { n       return getMin(v * 2 + 1, L, M, A, B); n     } n     if (A >= M) { n       return getMin(v * 2 + 2, M, R, A, B); n     } n     return min(getMin(v * 2 + 1, L, M, A, M), getMin(v * 2 + 2, M, R, M, B)); n   } n   void set(int v, int L, int R, int P, int val) { n     if (L + 1 == R) { n       nodes[v] = val; n       return; n     } n     int M = (L + R) / 2; n     if (P < M) { n       set(v * 2 + 1, L, M, P, val); n     } else { n       set(v * 2 + 2, M, R, P, val); n     } n     nodes[v].val = min(nodes[v * 2 + 1].val, nodes[v * 2 + 2].val); n   } n   int N; n   vector<Node> nodes; n }; n void testTree() { MinIntervalTree t(10, 1e9); } n int main() { n   ios::sync_with_stdio(false); n   int n, s, l; n   cin >> n >> s >> l; n   vector<int> a(n); n   vector<int> maxLeft(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     maxLeft[i] = 0; n   } n   set<pair<int, int> > aliveUp; n   set<pair<int, int> > aliveDown; n   for (int i = n - 1; i >= 0; --i) { n     { n       auto it = aliveUp.lower_bound(make_pair(a[i] + s + 1, 0)); n       while (it != aliveUp.end()) { n         aliveUp.erase(it); n         maxLeft[it->second] = max(maxLeft[it->second], i + 1); n         it = aliveUp.lower_bound(make_pair(a[i] + s + 1, 0)); n       } n     } n     { n       auto it = aliveDown.lower_bound(make_pair(-a[i] + s + 1, 0)); n       while (it != aliveDown.end()) { n         aliveDown.erase(it); n         maxLeft[it->second] = max(maxLeft[it->second], i + 1); n         it = aliveDown.lower_bound(make_pair(-a[i] + s + 1, 0)); n       } n     } n     aliveUp.insert(make_pair(a[i], i)); n     aliveDown.insert(make_pair(-a[i], i)); n   } n   for (int i = 1; i < n; ++i) { n     maxLeft[i] = max(maxLeft[i], maxLeft[i - 1]); n   } n   vector<int> dp(n + 1, 1e9); n   MinIntervalTree T(n + 3, 1e9); n   dp[0] = 0; n   T.set(0, 0); n   for (int i = 0; i < n; ++i) { n     int L = maxLeft[i] - 1 + 1; n     int R = i - l + 1; n     if (L > R) { n       continue; n     } n     dp[i + 1] = 1 + T.getMin(L, R); n     T.set(i + 1, dp[i + 1]); n   } n   if (dp[n] >= 1e9) { n     cout << -1 << endl; n   } else { n     cout << dp[n] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   long long x, d, k = 1; n   cin >> x >> d; n   vector<long long> ans; n   while (x) { n     long long rem = 0; n     for (int i = 31; i >= 0; --i) { n       if (x >> i & 1) { n         long long tmp = i; n         if (i == 0) { n           ans.push_back(k); n           rem--; n         } n         while (tmp--) ans.push_back(k); n         k += d; n         rem++; n       } n     } n     x = rem; n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); ++i) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t, n, k; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     long long a[n], b[n], c = 0; n     for (int i = 0; i < n; i++) cin >> a[i], b[i] = (a[i] % k); n     sort(b, b + n); n     reverse(b, b + n); n     map<long long, long long> mp; n     for (int i = 0; i < n && b[i]; i++) n       if (i) { n         if (b[i - 1] == b[i]) n           mp[b[i]] += k; n         else n           mp[b[i]] = k - b[i] + 1; n       } else n         mp[b[i]] = k - b[i] + 1; n     for (auto &it : mp) c = max(c, it.second); n     cout << c <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<int, vector<pair<int, int> > > mp; n int n, tt; n int arr[300000]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   for (int r = 1; r <= n; r++) { n     long long sum = 0; n     for (int l = r; l >= 1; l--) { n       sum += arr[l]; n       mp[sum].push_back(make_pair(l, r)); n     } n   } n   map<int, vector<pair<int, int> > >::iterator it; n   vector<pair<int, int> > ans; n   for (it = mp.begin(); it != mp.end(); it++) { n     int sum = 0; n     vector<pair<int, int> > &now = it->second; n     vector<pair<int, int> > cnt; n     int r = -1; n     for (int i = 0; i < now.size(); i++) { n       if (now[i].first > r) n         sum++, r = now[i].second, n                cnt.push_back(make_pair(now[i].first, now[i].second)); n     } n     if (sum > tt) ans = cnt, tt = sum; n   } n   cout << tt << endl; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first <<     << ans[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d, v[102], c, g[102], k, S; n int main(void) { n   cin >> n >> d; n   for (int i = 1; i <= n; i++) cin >> v[i]; n   for (int i = 1; i < n; i++) { n     if (v[i] % 2) n       c++; n     else n       c--; n     if (c == 0) g[k++] = abs(v[i] - v[i + 1]); n   } n   sort(g, g + k); n   for (S = 0; S < k && d >= g[S]; S++) d -= g[S]; n   cout << S; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n, m, k; n   cin >> n >> m >> k; n   if (k > 1) { n     if (m == n) { n       cout << 2 * k + 1 << endl; n       for (int i = (int)(1); i < ((int)k + 1); i++) { n         cout << 1 <<    ; n       } n       cout << n <<    ; n       for (int i = (int)(1); i < ((int)k); i++) { n         cout << n + 1 <<    ; n       } n       cout << n + m << endl; n     } else { n       cout << 2 * k << endl; n       for (int i = (int)(1); i < ((int)k + 1); i++) { n         cout << 1 <<    ; n       } n       cout << n <<    ; n       for (int i = (int)(1); i < ((int)k); i++) { n         cout << n + 1 <<    ; n       } n       cout << endl; n     } n   } else { n     if (m + 2 <= n) { n       cout << 2 << endl; n       cout << 1 <<     << n << endl; n     } else { n       if (n == 2 and m == 2) { n         cout << 4 << endl; n         cout <<  1 2 3 4  << endl; n       } else { n         cout << 3 << endl; n         cout << 1 <<     << n <<     << 2 * n - 1 << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int** arr; n std::vector<int> opers; n void del3(int x, int y) { n   if (arr[x][y] && arr[x + 1][y] && arr[x][y + 1]) { n     opers.push_back(x + 1); n     opers.push_back(y + 1); n     opers.push_back(x + 2); n     opers.push_back(y + 1); n     opers.push_back(x + 1); n     opers.push_back(y + 2); n     arr[x][y] = 0; n     arr[x + 1][y] = 0; n     arr[x][y + 1] = 0; n   } else if (arr[x][y] && arr[x + 1][y] && arr[x + 1][y + 1]) { n     opers.push_back(x + 1); n     opers.push_back(y + 1); n     opers.push_back(x + 2); n     opers.push_back(y + 1); n     opers.push_back(x + 2); n     opers.push_back(y + 2); n     arr[x][y] = 0; n     arr[x + 1][y] = 0; n     arr[x + 1][y + 1] = 0; n   } else if (arr[x][y + 1] == 1 && arr[x + 1][y] && arr[x + 1][y + 1]) { n     opers.push_back(x + 1); n     opers.push_back(y + 2); n     opers.push_back(x + 2); n     opers.push_back(y + 1); n     opers.push_back(x + 2); n     opers.push_back(y + 2); n     arr[x][y + 1] = 0; n     arr[x + 1][y] = 0; n     arr[x + 1][y + 1] = 0; n   } else if (arr[x][y] == 1 && arr[x][y + 1] && arr[x + 1][y + 1]) { n     opers.push_back(x + 1); n     opers.push_back(y + 1); n     opers.push_back(x + 1); n     opers.push_back(y + 2); n     opers.push_back(x + 2); n     opers.push_back(y + 2); n     arr[x][y] = 0; n     arr[x][y + 1] = 0; n     arr[x + 1][y + 1] = 0; n   } n } n void del2(int x, int y) { n   bool fl = false; n   if (arr[x][y] == 0) { n     opers.push_back(x + 1); n     opers.push_back(y + 1); n     arr[x][y] = 1; n   } else { n     if (!fl) { n       opers.push_back(x + 1); n       opers.push_back(y + 1); n       arr[x][y] = 0; n       fl = true; n     } n   } n   if (arr[x + 1][y] == 0) { n     opers.push_back(x + 2); n     opers.push_back(y + 1); n     arr[x + 1][y] = 1; n   } else { n     if (!fl) { n       opers.push_back(x + 2); n       opers.push_back(y + 1); n       fl = true; n       arr[x + 1][y] = 0; n     } n   } n   if (arr[x][y + 1] == 0) { n     opers.push_back(x + 1); n     opers.push_back(y + 2); n     arr[x][y + 1] = 1; n   } else { n     if (!fl) { n       opers.push_back(x + 1); n       opers.push_back(y + 2); n       arr[x][y + 1] = 0; n       fl = true; n     } n   } n   if (arr[x + 1][y + 1] == 0) { n     opers.push_back(x + 2); n     opers.push_back(y + 2); n     arr[x + 1][y + 1] = 1; n   } else { n     if (!fl) { n       opers.push_back(x + 2); n       opers.push_back(y + 2); n       arr[x + 1][y + 1] = 0; n       fl = true; n     } n   } n   del3(x, y); n } n void del1(int x, int y) { n   int h = 0; n   if (arr[x][y] == 1) { n     opers.push_back(x + 1); n     opers.push_back(y + 1); n     arr[x][y] = 0; n   } else { n     if (h < 2) { n       opers.push_back(x + 1); n       opers.push_back(y + 1); n       arr[x][y] = 1; n       h++; n     } n   } n   if (arr[x + 1][y] == 1) { n     opers.push_back(x + 2); n     opers.push_back(y + 1); n     arr[x + 1][y] = 0; n   } else { n     if (h < 2) { n       opers.push_back(x + 2); n       opers.push_back(y + 1); n       arr[x + 1][y] = 1; n       h++; n     } n   } n   if (arr[x][y + 1] == 1) { n     opers.push_back(x + 1); n     opers.push_back(y + 2); n     arr[x][y + 1] = 0; n   } else { n     if (h < 2) { n       opers.push_back(x + 1); n       opers.push_back(y + 2); n       arr[x][y + 1] = 1; n       h++; n     } n   } n   if (arr[x + 1][y + 1] == 1) { n     opers.push_back(x + 2); n     opers.push_back(y + 2); n     arr[x + 1][y + 1] = 0; n   } else { n     if (h < 2) { n       opers.push_back(x + 2); n       opers.push_back(y + 2); n       arr[x + 1][y + 1] = 1; n       h++; n     } n   } n   del2(x, y); n } n void setup(int x, int y) { n   int iu = arr[x][y] + arr[x][y + 1] + arr[x + 1][y] + arr[x + 1][y + 1]; n   if (iu == 3) { n     del3(x, y); n   } else if (iu == 2) { n     del2(x, y); n   } else if (iu == 1) { n     del1(x, y); n   } else if (iu == 4) { n     del3(x, y); n     del1(x, y); n   } n   arr[x][y] = 0; n   arr[x][y + 1] = 0; n   arr[x + 1][y] = 0; n   arr[x + 1][y + 1] = 0; n } n int main() { n   int t; n   std::cin >> t; n   for (int i = 0; i < t; i++) { n     int n, m; n     std::cin >> n >> m; n     arr = new int*[n]; n     for (int i = 0; i < n; i++) arr[i] = new int[m]; n     for (int i = 0; i < n; i++) { n       std::string s; n       std::cin >> s; n       for (int j = 0; j < s.size(); j++) { n         arr[i][j] = s[j] -  0 ; n       } n     } n     for (int a = 0; a < n - 1; a++) { n       for (int b = 0; b < m - 1; b++) { n         setup(a, b); n       } n     } n     std::cout << opers.size() / 6 <<   n ; n     for (int i = 0; i < opers.size() / 6; i++) { n       for (int j = 0; j < 6; j++) { n         std::cout << opers[i * 6 + j] <<    ; n       } n       std::cout <<   n ; n     } n     opers.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, ans = 0; n int main() { n   cin >> n >> m >> k; n   if (n - 1 + m - 1 < k) { n     cout << -1 << endl; n     return 0; n   } n   long long buf = sqrt(n); n   buf++; n   for (int i = 0; i <= buf; i++) { n     long long x = n / (i + 1); n     if (k - i < 0) continue; n     long long y = m / (k - i + 1); n     ans = max(ans, x * y); n   } n   for (int i = 1; i <= buf; i++) { n     long long cut = n / i - 1; n     if (cut < 0) continue; n     long long left = k - cut; n     if (left < 0) left = 0; n     ans = max(ans, i * (m / (left + 1))); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int N; n   cin >> N; n   long long int dp_D = 1, dp = 0, dp_temp; n   for (int i = 0; i < N; i++) { n     dp_temp = dp_D; n     dp_D = 3 * dp; n     dp = dp * 2 + dp_temp; n     dp %= 1000000007; n     dp_D %= 1000000007; n   } n   cout << dp_D << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cal(long long x) { return (x + 1) * (x + 2) >> 1; } n int main() { n   int a, b, c, l; n   while (scanf( %d%d%d%d , &a, &b, &c, &l) != EOF) { n     long long ans = 0; n     for (int i = 0; i <= l; i++) ans += 1LL * (i + 1) * (i + 2) / 2; n     for (long long i = 0; i <= l; i++) { n       if (a + i >= b + c) ans -= cal(min(l - i, a + i - b - c)); n       if (b + i >= a + c) ans -= cal(min(l - i, b + i - a - c)); n       if (c + i >= a + b) ans -= cal(min(l - i, c + i - a - b)); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e9 + 3; n long long n, sum; n vector<int> vec; n int32_t main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = n - 100; i <= n; i++) { n     long long x = i; n     while (x) { n       sum += x % 10; n       x /= 10; n     } n     sum += i; n     if (sum == n) vec.push_back(i); n     sum = 0; n   } n   cout << vec.size() << endl; n   for (int i = 0; i < vec.size(); i++) cout << vec[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = (c & 15); n   while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); n   x = v * f; n } n inline void read(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = (c & 15); n   while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); n   x = v * f; n } n inline void readc(char &x) { n   char c; n   while (((c = getchar()) ==    ) || c ==   n ) n     ; n   x = c; n } n long long ans; n int n, m, k, i, j, fa[150005][19], dep[150005], first[150005], ed[150005], ti, n     px[150005], py[150005]; n int tt, lst[150005], stk[150005], tp; n vector<int> ne[150005]; n vector<int> e[150005]; n vector<int> v[150005]; n vector<int> add[150005]; n void addedg(int x, int y) { n   if (lst[x] != tt) { n     lst[x] = tt; n     ne[x].clear(); n   } n   if (lst[y] != tt) { n     lst[y] = tt; n     ne[y].clear(); n   } n   ne[x].push_back(y); n } n bool cmp(int x, int y) { return first[x] < first[y]; } n void dfs1(int x, int p) { n   fa[x][0] = p; n   first[x] = ++ti; n   for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) n     if (*it != p) { n       dep[*it] = dep[x] + 1; n       dfs1(*it, x); n     } n   ed[x] = ti; n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   int t = dep[x] - dep[y], i; n   for ((i) = (18); (i) >= (0); (i)--) n     if ((t >> i) & 1) x = fa[x][i]; n   if (x == y) return x; n   for ((i) = (18); (i) >= (0); (i)--) n     if (fa[x][i] != fa[y][i]) { n       x = fa[x][i]; n       y = fa[y][i]; n     } n   return fa[x][0]; n } n int gtf(int x, int y) { n   int i; n   for ((i) = (18); (i) >= (0); (i)--) n     if ((y >> i) & 1) x = fa[x][i]; n   return x; n } n struct bit { n   int c[150005]; n   void add(int x, int y) { n     while (x <= n) { n       c[x] += y; n       x += (x & -x); n     } n   } n   int query(int x) { n     int s = 0; n     while (x) { n       s += c[x]; n       x -= (x & -x); n     } n     return s; n   } n } tr; n void dfs2(int x) { n   for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) { n     if (dep[px[*it]] - dep[x] >= k) { n       int t = gtf(px[*it], dep[px[*it]] - dep[x] - k); n       ans += tr.query(ed[t]) - tr.query(first[t] - 1); n     } n     if (dep[py[*it]] - dep[x] >= k) { n       int t = gtf(py[*it], dep[py[*it]] - dep[x] - k); n       ans += tr.query(ed[t]) - tr.query(first[t] - 1); n     } n   } n   for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) { n     tr.add(first[px[*it]], 1); n     tr.add(first[py[*it]], 1); n   } n   for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) n     if (*it != fa[x][0]) { n       dfs2(*it); n     } n   for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) { n     tr.add(first[px[*it]], -1); n     tr.add(first[py[*it]], -1); n   } n } n void ins(int x) { n   if (x == 1) return; n   int z = lca(x, stk[tp]); n   if (z == stk[tp]) { n     stk[++tp] = x; n     return; n   } n   while (tp > 1 && first[stk[tp - 1]] >= first[z]) { n     addedg(stk[tp - 1], stk[tp]); n     tp--; n   } n   if (stk[tp] != z) { n     addedg(z, stk[tp]); n     stk[tp] = z; n   } n   stk[++tp] = x; n } n multiset<int> s[150005]; n int rt[150005], tot, lc[3000005], rc[3000005], sum[3000005]; n void update(int &x, int l, int r, int y) { n   if (!x) { n     x = ++tot; n     lc[x] = rc[x] = sum[x] = 0; n   } n   sum[x]++; n   if (l == r) return; n   int mid = (l + r) / 2; n   if (y <= mid) n     update(lc[x], l, mid, y); n   else n     update(rc[x], mid + 1, r, y); n } n void merge(int &x, int y, int l, int r) { n   if (!x || !y) { n     x = x + y; n     return; n   } n   if (l == r) { n     sum[x] += sum[y]; n     return; n   } n   int mid = (l + r) / 2; n   merge(lc[x], lc[y], l, mid); n   merge(rc[x], rc[y], mid + 1, r); n   sum[x] = sum[lc[x]] + sum[rc[x]]; n } n int query(int x, int l, int r, int ql, int qr) { n   if (!x) return 0; n   if (ql <= l && r <= qr) return sum[x]; n   int mid = (l + r) / 2, s = 0; n   if (ql <= mid) s += query(lc[x], l, mid, ql, qr); n   if (qr > mid) s += query(rc[x], mid + 1, r, ql, qr); n   return s; n } n void dfs3(int x) { n   if (dep[x] - dep[i] >= k) { n     int t = gtf(x, dep[x] - dep[i] - k); n     ans += 1ll * add[x].size() * (tr.query(ed[t]) - tr.query(first[t] - 1)); n   } n   for (__typeof((add[x]).begin()) it = (add[x]).begin(); it != (add[x]).end(); n        it++) { n     tr.add(first[*it], 1); n   } n   rt[x] = 0; n   s[x].clear(); n   for (__typeof((add[x]).begin()) it = (add[x]).begin(); it != (add[x]).end(); n        it++) { n     s[x].insert(*it); n     if (dep[x] - dep[i] >= k) { n       int t = gtf(x, k - 1); n       if (first[t] > 1) ans += query(rt[x], 1, n, 1, first[t] - 1); n       if (ed[t] < n) ans += query(rt[x], 1, n, ed[t] + 1, n); n     } else { n       if (dep[x] - dep[i] + dep[*it] - dep[i] < k) continue; n       int t = gtf(*it, dep[*it] - dep[i] - (k - (dep[x] - dep[i]))); n       ans += query(rt[x], 1, n, first[t], ed[t]); n     } n     update(rt[x], 1, n, first[*it]); n   } n   for (__typeof((ne[x]).begin()) it = (ne[x]).begin(); it != (ne[x]).end(); n        it++) { n     dfs3(*it); n     if (s[x].size() < s[*it].size()) { n       swap(s[x], s[*it]); n       swap(rt[x], rt[*it]); n     } n     for (__typeof((s[*it]).begin()) it2 = (s[*it]).begin(); n          it2 != (s[*it]).end(); it2++) { n       if (dep[x] - dep[i] >= k) { n         int t = gtf(x, k - 1); n         if (first[t] > 1) ans += query(rt[x], 1, n, 1, first[t] - 1); n         if (ed[t] < n) ans += query(rt[x], 1, n, ed[t] + 1, n); n       } else { n         if (dep[x] - dep[i] + dep[*it2] - dep[i] < k) continue; n         int t = gtf(*it2, dep[*it2] - dep[i] - (k - (dep[x] - dep[i]))); n         ans += query(rt[x], 1, n, first[t], ed[t]); n       } n       s[x].insert(*it2); n     } n     merge(rt[x], rt[*it], 1, n); n   } n } n int main() { n   read(n); n   read(m); n   read(k); n   for (((i)) = (1); ((i)) <= ((n - 1)); ((i))++) { n     int x, y; n     read(x); n     read(y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   dfs1(1, 0); n   for (((j)) = (1); ((j)) <= ((18)); ((j))++) n     for (((i)) = (1); ((i)) <= ((n)); ((i))++) n       fa[i][j] = fa[fa[i][j - 1]][j - 1]; n   for (((i)) = (1); ((i)) <= ((m)); ((i))++) { n     read(px[i]); n     read(py[i]); n     if (first[px[i]] > first[py[i]]) swap(px[i], py[i]); n     v[lca(px[i], py[i])].push_back(i); n   } n   dfs2(1); n   for (((i)) = (1); ((i)) <= ((n)); ((i))++) { n     for (__typeof((v[i]).begin()) it = (v[i]).begin(); it != (v[i]).end(); n          it++) { n       add[px[*it]].push_back(py[*it]); n     } n     vector<int> a; n     for (__typeof((v[i]).begin()) it = (v[i]).begin(); it != (v[i]).end(); n          it++) { n       a.push_back(px[*it]); n       a.push_back(py[*it]); n     } n     a.push_back(1); n     sort((a).begin(), (a).end(), cmp); n     a.resize(unique((a).begin(), (a).end()) - a.begin()); n     tt++; n     ne[1].clear(); n     stk[tp = 1] = 1; n     for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) { n       ins(*it); n     } n     while (tp > 1) { n       addedg(stk[tp - 1], stk[tp]); n       tp--; n     } n     tot = 0; n     dfs3(1); n     for (__typeof((v[i]).begin()) it = (v[i]).begin(); it != (v[i]).end(); n          it++) { n       add[px[*it]].clear(); n     } n     for (__typeof((v[i]).begin()) it = (v[i]).begin(); it != (v[i]).end(); n          it++) { n       tr.add(first[py[*it]], -1); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string d[] = { monday ,  tuesday ,   wednesday ,  thursday , n                  friday ,  saturday ,  sunday }; n   string a, b; n   int e, c, g, f, u; n   bool o; n   cin >> a >> b; n   for (int i = 0; i < 7; i++) { n     if (a == d[i]) { n       e = i; n     } n     if (b == d[i]) { n       c = i; n     } n   } n   u = abs(e - c); n   for (int m = 1; m <= 12; m++) { n     if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { n       g = 4; n       f = e - g; n       if (f < 0) { n         f = 7 - abs(f); n       } n       if (d[f] == d[c]) { n         o = 1; n         cout <<  YES ; n         break; n       } else n         o = 0; n     } else if (m == 2) { n       g = 0; n       f = e - g; n       if (f < 0) { n         f = 7 - abs(f); n       } n       if (d[f] == d[c]) { n         o = 1; n         cout <<  YES ; n         break; n       } else n         o = 0; n     } else if (m == 4 || m == 6 || m == 9 || m == 1) { n       g = 5; n       f = e - g; n       if (f < 0) { n         f = 7 - abs(f); n       } n       if (d[f] == d[c]) { n         o = 1; n         cout <<  YES ; n         break; n       } else n         o = 0; n     } n   } n   if (o == 0) { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   long long arr[n]; n   for (long long i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   stack<pair<long long, long long>> s1, s2; n   s1.push({0, -1}); n   long long left[n], right[n]; n   for (long long i = 0; i < n; ++i) { n     while (s1.top().first >= arr[i]) s1.pop(); n     left[i] = s1.top().second + 1; n     s1.push({arr[i], i}); n   } n   s2.push({0, n}); n   for (long long i = n - 1; i >= 0; --i) { n     while (s2.top().first >= arr[i]) s2.pop(); n     right[i] = s2.top().second - 1; n     s2.push({arr[i], i}); n   } n   long long ans[n + 1]; n   memset(ans, 0, sizeof(ans)); n   for (long long i = 0; i < n; ++i) { n     ans[right[i] - left[i] + 1] = max(arr[i], ans[right[i] - left[i] + 1]); n   } n   for (long long i = n - 1; i >= 1; --i) { n     if (ans[i] < ans[i + 1]) ans[i] = ans[i + 1]; n   } n   for (long long i = 0; i < n; ++i) { n     cout << ans[i + 1] <<    ; n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, cnt = 1; n string s; n bool ok = true, mtest = true; n void solve() { n   cin >> b; n   vector<int> v(b), vc(b); n   for (auto& i : v) cin >> i; n   for (auto& i : vc) cin >> i; n   sort(v.begin(), v.end()); n   sort(vc.begin(), vc.end()); n   for (auto i : v) cout << i <<    ; n   cout <<   n ; n   for (auto i : vc) cout << i <<    ; n   cout <<   n ; n } n int main() { n   cin >> a; n   while (a--) solve(); n   cerr <<   nTime elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n; n   vector<int> a(n); n   vector<pair<int, int> > d; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     d.push_back(make_pair(a[i], 0)); n   } n   cin >> m; n   vector<int> b(m); n   vector<pair<int, int> > c(n + m); n   vector<pair<int, int> > sum(n + m, make_pair(0, 0)); n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n     d.push_back(make_pair(b[i], 1)); n   } n   sort(d.begin(), d.end()); n   int tog = 0; n   if (d[0].second) n     sum[0].second = 1; n   else n     sum[0].first = 1; n   for (int i = 0; i < n + m; i++) { n     if (i > 0) { n       sum[i].first = sum[i - 1].first; n       sum[i].second = sum[i - 1].second; n     } n     if (!d[i].second) { n       tog++; n       if (i > 0) sum[i].first++; n     } else { n       tog--; n       if (i > 0) sum[i].second++; n     } n     c[i].first = tog; n     c[i].second = i; n   } n   sort(c.begin(), c.end()); n   if (c[0].first < 0) n     cout << 3 * n - sum[c[0].second].first <<  :  n          << 3 * m - sum[c[0].second].second << endl; n   else n     cout << 3 * n <<  :  << 3 * m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1, M = 1e9 + 7; n mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count()); n int n, q, bit[N], inv[N], a[N], last[N], INVER[N], ans[N]; n bool prime[N]; n vector<int> v[N]; n struct query { n   int l, r, id; n   void sc(int temp) { n     scanf( %d%d , &l, &r); n     id = temp; n   } n } Q[N]; n int power(int base, int to) { n   int ret = 1; n   while (to) { n     if (to & 1) ret = ret * 1LL * base % M; n     to >>= 1; n     base = base * 1LL * base % M; n   } n   return ret; n } n int INV(int a) { n   if (INVER[a] != -1) return INVER[a]; n   return INVER[a] = power(a, M - 2); n } n void add(int at, int *bit, int val) { n   for (; at < N; at += (-at) & at) bit[at] = bit[at] * 1LL * val % M; n } n int get(int at, int *bit) { n   int ret = 1; n   for (; at; at -= at & (-at)) ret = ret * 1LL * bit[at] % M; n   return ret; n } n int main() { n   for (int i = 0; i < N; i++) bit[i] = inv[i] = 1; n   memset(last, -1, sizeof last); n   memset(INVER, -1, sizeof INVER); n   scanf( %d , &n); n   for (int i = 2; i < N; i++) n     if (!prime[i]) { n       for (int j = i; j < N; j += i) { n         prime[j] = 1; n         v[j].push_back(i); n       } n     } n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   scanf( %d , &q); n   for (int i = 0; i < q; i++) Q[i].sc(i); n   sort(Q, Q + q, [&](query a, query b) { return a.r < b.r; }); n   int j = 1; n   for (int i = 0; i < q; i++) { n     while (j <= Q[i].r) { n       add(j, bit, a[j]); n       add(j, inv, INV(a[j])); n       for (auto k : v[a[j]]) { n         if (last[k] != -1) { n           add(last[k], bit, INV(k - 1) * 1LL * k % M); n           add(last[k], inv, (k - 1LL) * INV(k) % M); n         } n         last[k] = j; n         add(last[k], bit, (k - 1LL) * INV(k) % M); n         add(last[k], inv, k * 1LL * INV(k - 1) % M); n       } n       j++; n     } n     ans[Q[i].id] = get(Q[i].r, bit); n     ans[Q[i].id] = ans[Q[i].id] * 1LL * get(Q[i].l - 1, inv) % M; n   } n   for (int i = 0; i < q; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int ans[300005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   set<int> alive; n   for (int i = 1; i <= n; i++) alive.insert(i); n   for (int i = 0; i < m; i++) { n     int l, r, x; n     scanf( %d%d%d , &l, &r, &x); n     set<int>::iterator it = alive.lower_bound(l); n     vector<int> dead; n     while (it != alive.end()) { n       int now = *it; n       if (now > r) break; n       if (now != x) dead.push_back(now), ans[now] = x; n       it++; n     } n     for (int j = 0; j < (int)((dead).size()); j++) alive.erase(dead[j]); n   } n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ?   n  :    ); n   return 0; n } n