#include <bits/stdc++.h> n #define N 410 n #define ll long long n #define ull unsigned long long n //#define mo (ll)((ll)1e9+7) n #define squ(x) ((x)*(x)) n #define db double n #define all(x) x.begin(),x.end() n #define cmax(a,b) a=max(a,b) n #define cmin(a,b) a=min(a,b) n #define rep(i,l,r) for(register int i=l;i<=r;++i) n #define drep(i,r,l)for(register int i=r;i>=l;--i) n #define pb push_back n #define inline inline __attribute__((always_inline)) n using namespace std; n  n inline ll read() { n  ll x = 0, f = 1; char ch = getchar(); n  while (ch <  0  || ch >  9 ) {if (ch ==  - )f = -1; ch = getchar();} n  while (ch >=  0  && ch <=  9 ) {x = x * 10 + ch -  0 ; ch = getchar();} n  return x * f; n } n ll mo; n ll dp[N][N]; n  n int fac[N], inv[N], bit[N]; n ll Pow(ll a, ll b) { n  ll res = 1; n  a %= mo; n  for (; b; b >>= 1, a = a * a % mo) n   if (b & 1) res = res * a % mo; n  return res; n } n void init_inv(int n) { n  fac[0] = 1; n  rep(i, 1, n)fac[i] = (ll)fac[i - 1] * i % mo; n  inv[n] = Pow(fac[n], mo - 2); n  drep(i, n, 1) inv[i - 1] = (ll)inv[i] * i % mo; n } n ll C(int n, int m) { n  if (n < m)return 0; n  return 1ll * fac[n] * inv[m] % mo * inv[n - m] % mo; n } n int main() { n  int n = read(); mo = read(); n  init_inv(n); n  bit[0] = 1; rep(i, 1, 400)bit[i] = bit[i - 1] * 1ll * 2 % mo; n  dp[0][0] = 1; n  rep(i, 0, n)rep(j, 0, i)if (dp[i][j] > 0) { n   rep(k, 1, n - i)(dp[i + k + 1][j + k] += dp[i][j] * 1ll * C(j + k, k) % mo n                    * bit[k - 1] % mo) %= mo; n  } n  ll ans = 0; n  rep(i, 1, n)(ans += dp[n + 1][i]) %= mo; n  printf( %lld n , ans); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int a[20]; n int main() { n   int n, b[20]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> b[i]; n   int kol = 1; n   for (int i = 0; i < n; i++) kol *= 2; n   for (int i = 0; i < kol; i++) { n     int sum = 0; n     a[n - 1]++; n     for (int j = n - 1; j > 0; j--) { n       a[j - 1] = a[j] / 2 + a[j - 1]; n       a[j] %= 2; n     } n     for (int j = 0; j < n; j++) { n       if (a[j] == 1) n         sum += b[j]; n       else n         sum -= b[j]; n     } n     sum = abs(sum); n     sum %= 360; n     if (sum == 0) { n       cout <<  yes ; n       return 0; n     } n   } n   cout <<  no ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)500; n const int linf = (int)1e9 + 7; n const int mod = (int)1e9 + 7; n const int sN = (int)27000; n inline void ioi2020() {} n using ll = long long; n using ld = long double; n using ull = unsigned long long; n ll n, m, k; n ll a[N]; n ll dp[(1 << 20)][20]; n map<ll, ll> mp[N]; n int main() { n   ioi2020(); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= k; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     mp[a - 1][b - 1] = c; n   } n   for (int mask = 0; mask < (1 << n); mask++) { n     for (int bit = 0; bit < n; bit++) { n       if ((mask >> bit) & 1) { n         for (int sbit = 0; sbit < n; sbit++) { n           if ((mask >> sbit) & 1) dp[mask][bit] += a[sbit]; n         } n       } n     } n   } n   for (int mask = 0; mask < (1 << n); mask++) { n     for (int bit = 0; bit < n; bit++) { n       if (!((mask >> bit) & 1)) { n         for (int sbit = 0; sbit < n; sbit++) { n           if ((mask >> sbit) & 1) n             dp[(mask ^ (1 << bit))][bit] = n                 max(dp[(mask ^ (1 << bit))][bit], n                     dp[mask][sbit] + mp[sbit][bit] + a[bit]); n         } n       } n     } n   } n   ll res = 0; n   for (int mask = 0; mask < (1 << n); mask++) { n     if (__builtin_popcount(mask) == m) { n       for (int bit = 0; bit < n; bit++) { n         res = max(res, dp[mask][bit]); n       } n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXP = 1e5; n long long int n, s, p, q, x, sell[MAXP + 5] = {0}, buy[MAXP + 5] = {0}; n char d; n vector<pair<long long int, long long int> > ansell; n int main() { n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     cin >> d >> p >> q; n     if (d ==  S ) n       sell[p] += q; n     else n       buy[p] += q; n   } n   x = 0; n   for (int i = 0; i <= MAXP; i++) { n     if (x == s) break; n     if (sell[i]) { n       ansell.push_back(make_pair(i, sell[i])); n       x++; n     } n   } n   for (int i = ((int(ansell.size()))) - 1; i >= 0; i--) n     cout <<  S   << ansell[i].first <<     << ansell[i].second << endl; n   x = 0; n   for (int i = MAXP; i >= 0; i--) { n     if (x == s) break; n     if (buy[i]) { n       cout <<  B   << i <<     << buy[i] << endl; n       x++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tm[100005]; n int gcd(int a, int b) { n   int r; n   while (b > 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n int main() { n   int n, i, j, tt, m; n   scanf( %d , &n); n   scanf( %d , &tm[1]); n   for (i = 2; i <= n; i++) { n     scanf( %d , &tm[i]); n   } n   int flag = 0; n   while (tm[1] % 2 == 0) tm[1] /= 2; n   while (tm[1] % 3 == 0) tm[1] /= 3; n   int gd = tm[1]; n   for (i = 1; i <= n; i++) { n     while (tm[i] % 2 == 0) tm[i] /= 2; n     while (tm[i] % 3 == 0) tm[i] /= 3; n     if (tm[i] == tm[1]) n       continue; n     else { n       flag = 1; n       break; n     } n   } n   if (flag) n     printf( NO n ); n   else n     printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string ans =  no ; n   cin >> n; n   vector<int> lineas(n); n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     lineas[i] = a; n   } n   int minimo; n   int maximo; n   for (int i = 0; i < n - 1; i++) { n     minimo = min(lineas[i], lineas[i + 1]); n     maximo = max(lineas[i], lineas[i + 1]); n     for (int j = 0; j < n - 1; j++) { n       if (j != i) { n         int anterior = min(lineas[j], lineas[j + 1]); n         int siguiente = max(lineas[j], lineas[j + 1]); n         if (siguiente < maximo and minimo < siguiente and anterior < minimo) { n           ans =  yes ; n         } n         if (minimo < anterior and maximo < siguiente and anterior < maximo) { n           ans =  yes ; n         } n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Pos[1 << (24)], dp[1 << (24)]; n int a, b; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> Pos[1 << i]; n   } n   int m; n   cin >> m; n   if (m) cin >> a, m--; n   if (m) cin >> b, m--; n   int upper = 1 << n; n   dp[0] = 1; n   for (int i = 1; i < upper; ++i) { n     int GetLow = ((i) & (-(i))), tmp = i; n     Pos[i] = Pos[GetLow] + Pos[GetLow ^ i]; n     if (Pos[i] == a || Pos[i] == b) continue; n     while (tmp) { n       int s = dp[i] + dp[GetLow ^ i]; n       dp[i] = s > (1000000007) ? s - (1000000007) : s; n       tmp ^= ((tmp) & (-(tmp))); n       GetLow = ((tmp) & (-(tmp))); n     } n   } n   cout << dp[(1 << n) - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, n; n   cin >> n; n   k = n / 2; n   for (i = 1; i <= n; i++) { n     cout << (i - 1) * k + 1; n     for (j = (i - 1) * k + 2; j <= i * k; j++) cout <<     << j; n     for (j = n * n - (i - 1) * k; j > n * n - i * k; j--) cout <<     << j; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int ppr = 257; n const long long INF = 2e18; n const int inf = 2e9; n const int mod = 1e9 + 7; n const int N = 1e6 + 123; n const long double pi = 3.141592653589793238462643; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n int n, dp[55][55][55][55], a[55][55], p[55][55]; n bool u[55][55][55][55]; n inline int get(int x1, int aza_ibagaliev, int x2, int y2) { n   return p[x2][y2] - p[x2][aza_ibagaliev - 1] - p[x1 - 1][y2] + n          p[x1 - 1][aza_ibagaliev - 1]; n } n int calc(int x1, int aza_ibagaliev, int x2, int y2) { n   if (u[x1][aza_ibagaliev][x2][y2]) return dp[x1][aza_ibagaliev][x2][y2]; n   u[x1][aza_ibagaliev][x2][y2] = 1; n   dp[x1][aza_ibagaliev][x2][y2] = max((x2 - x1 + 1), (y2 - aza_ibagaliev + 1)) * n                                   (get(x1, aza_ibagaliev, x2, y2) > 0); n   for (int i = x1; i < x2; i++) { n     dp[x1][aza_ibagaliev][x2][y2] = min( n         dp[x1][aza_ibagaliev][x2][y2], n         calc(x1, aza_ibagaliev, i, y2) + calc(i + 1, aza_ibagaliev, x2, y2)); n   } n   for (int i = aza_ibagaliev; i < y2; i++) { n     dp[x1][aza_ibagaliev][x2][y2] = n         min(dp[x1][aza_ibagaliev][x2][y2], n             calc(x1, aza_ibagaliev, x2, i) + calc(x1, i + 1, x2, y2)); n   } n   return dp[x1][aza_ibagaliev][x2][y2]; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       char c; n       cin >> c; n       a[i][j] = (c ==  # ); n       p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + a[i][j]; n     } n   cout << calc(1, 1, n, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n int main() { n   cin >> n; n   long long int summ = n + (n - 1); n   long long int wei = 0; n   while (summ != 0) { n     summ /= 10; n     wei++; n   } n   long long int stdl = 9; n   for (int i = 1; i < wei; i++) { n     stdl = stdl * 10 + 9; n   } n   summ = n + (n - 1); n   if (summ < stdl) wei--, stdl /= 10; n   long long int ans, p = stdl / 2; n   ans = min(p, n - p); n   long long int qian = 1; n   for (int i = 1; i <= wei; i++) qian *= 10; n   while (stdl + qian <= summ) { n     stdl += qian; n     p = stdl / 2; n     ans += min(p, n - p); n   } n   if (n < 5) { n     if (n == 2) ans = 1; n     if (n == 3) ans = 3; n     if (n == 4) ans = 6; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int const N = 100 * 1000 + 16; n int const M = 1000 * 1000 * 1000 + 7; n int n, k; n int a[N]; n int c[N]; n ll e, v[N]; n vector<int> idx[N]; n struct wavelet { n   int L, R; n   vector<int> b; n   wavelet* l = 0; n   wavelet* r = 0; n   wavelet(int* pl, int* pr, int L, int R) { n     this->L = L; n     this->R = R; n     if (L == R || pl >= pr) return; n     int M = L + R >> 1; n     auto f = [M](int x) { return x <= M; }; n     b.reserve(pr - pl + 1); n     b.emplace_back(0); n     for (auto p = pl; p != pr; ++p) b.emplace_back(b.back() + f(*p)); n     auto pm = stable_partition(pl, pr, f); n     l = new wavelet(pl, pm, L, M); n     r = new wavelet(pm, pr, M + 1, R); n   } n   ~wavelet() { n     delete l; n     delete r; n   } n   int query(int l, int r, int k) { n     if (k < L || l >= r) return 0; n     if (R <= k) return r - l; n     int lf = b.size() > l ? b[l] : 0; n     int rg = b.size() > r ? b[r] : 0; n     return (this->l ? this->l->query(lf, rg, k) : 0) + n            (this->r ? this->r->query(l - lf, r - rg, k) : 0); n   } n }; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   cin >> n >> k; n   fill_n(a + 1, n, n + 1); n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     idx[x].emplace_back(i); n     if (++c[x] > k) a[idx[x][c[x] - k - 1]] = i; n   } n   wavelet t(a + 1, a + 1 + n, 1, 1000 * 1000 * 1000); n   int q; n   cin >> q; n   int last = 0; n   while (q--) { n     int xi, yi; n     cin >> xi >> yi; n     int l = (xi + last) % n + 1; n     int r = (yi + last) % n + 1; n     if (l > r) swap(l, r); n     int z = t.query(l - 1, r, r); n     last = r - l + 1 - z; n     cout << last <<   n ; n   } n   cout << flush; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n const double eps = 1e-8; n bool order(double a, double b, double c) { return a < b && b < c; } n int main() { n   double h, m, s, t1, t2; n   cin >> h >> m >> s >> t1 >> t2; n   s /= 60; n   m = m / 60 + s / 60; n   h = h / 12 + m / 12; n   if (m >= 1 - eps) m -= 1; n   if (h >= 1 - eps) h -= 1; n   t1 /= 12; n   t2 /= 12; n   if (t1 >= 1 - eps) t1 -= 1; n   if (t2 >= 1 - eps) t2 -= 1; n   if (t1 > t2) swap(t1, t2); n   bool should = order(t1, h, t2); n   if (should == order(t1, m, t2) && should == order(t1, s, t2)) n     puts( YES ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 7; n const int MOD = 1e9 + 7; n vector<int> Gimp[MAXN]; n vector<int> Gcrew[MAXN]; n vector<int> G[MAXN]; n int comp[MAXN]; n int compSize[MAXN]; n bool vis[MAXN]; n bool col[MAXN]; n bool ans; n int res; n int allSize; n void DFScomp(int v, int nr) { n   comp[v] = nr; n   ++compSize[nr]; n   for (auto u : Gcrew[v]) { n     if (comp[u] > 0) continue; n     DFScomp(u, nr); n   } n } n void DFScolor(int v, bool c) { n   vis[v] = true; n   col[v] = c; n   if (c) res += compSize[v]; n   allSize += compSize[v]; n   for (auto u : G[v]) { n     if (vis[u]) { n       if (col[u] == c) ans = false; n       continue; n     } n     DFScolor(u, !c); n   } n } n void solve() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     int u, v; n     string c; n     cin >> u >> v >> c; n     if (c ==  imposter ) { n       Gimp[v].push_back(u); n       Gimp[u].push_back(v); n     } else { n       Gcrew[v].push_back(u); n       Gcrew[u].push_back(v); n     } n   } n   int c = 1; n   for (int i = 1; i <= n; ++i) { n     if (comp[i] == 0) { n       DFScomp(i, c); n       ++c; n     } n   } n   for (int v = 1; v <= n; ++v) { n     for (auto u : Gimp[v]) { n       G[comp[u]].push_back(comp[v]); n       G[comp[v]].push_back(comp[u]); n     } n   } n   int best = 0; n   ans = true; n   for (int i = 1; i < c; ++i) { n     if (!vis[i]) { n       res = 0; n       allSize = 0; n       DFScolor(i, 0); n       best += max(res, allSize - res); n     } n   } n   if (!ans) best = -1; n   cout << best <<   n ; n   for (int i = 1; i <= n; ++i) { n     Gimp[i].clear(); n     Gcrew[i].clear(); n     G[i].clear(); n     col[i] = 0; n     comp[i] = 0; n     compSize[i] = 0; n     vis[i] = false; n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k; n   cin >> n; n   cin >> k; n   if (n % 2 == 0) { n     if (k <= (n / 2)) n       cout << 2 * (k - 1) + 1; n     else { n       k = k - (n / 2); n       cout << 2 * k; n     } n   } else { n     if (k <= (n + 1) / 2) n       cout << 2 * (k - 1) + 1; n     else { n       k = k - (n + 1) / 2; n       cout << 2 * k; n     } n   } n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int n, r; n   cin >> n >> r; n   double R = -r / (sin(acos(-1) / n) - 1); n   double ans = R - r; n   cout << fixed << setprecision(10) << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, d, ct, q, ans, l, m, r, n, mx, mn, i, j, k, x, y, z, s; n char ch; n string ss =   , str =   ; n map<pair<long long, long long>, long long> mp; n set<long long> st; n long long a[300003], b[300003]; n vector<pair<long long, long long>> v; n bool ask(long long k) { n   long long i; n   for (i = 0; i < v.size(); i++) n     if (v[i].first != k && v[i].second != k) break; n   if (i == v.size()) return 1; n   long long x = v[i].first; n   long long g = k; n   if (x > k) swap(x, k); n   if (a[x] + a[k] - mp[{x, k}] == m) return 1; n   x = v[i].second; n   if (x > g) swap(x, g); n   if (a[x] + a[g] - mp[{x, g}] == m) return 1; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   s = k = d = l = mx = m = mn = r = q = i = j = z = x = y = ct = ans = 0; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> x >> y, a[x]++, a[y]++; n     v.push_back({x, y}); n     if (x > y) swap(x, y); n     mp[{x, y}]++; n   } n   x = v[0].first; n   y = v[0].second; n   bool ya = ask(x); n   if (ya) { n     cout <<  YES ; n     return 0; n   } n   ya = ask(y); n   if (ya) { n     cout <<  YES ; n     return 0; n   } n   cout <<  NO ; 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   string sub, inp =   ; n   while (cin >> sub) { n     inp += sub; n   } n   string s =   ; n   for (int i = 0; i < (int)inp.size(); ++i) { n     if (inp[i] ==  a ) { n       inp[i - 2] ==  /  ? s +=  A  : s +=  a ; n     } else if (inp[i] ==  r ) { n       inp[i - 2] ==  /  ? s +=  R  : s +=  r ; n     } else if (inp[i] ==  d ) { n       inp[i - 2] ==  /  ? s +=  D  : s +=  d ; n     } n   } n   vector<int> st, ans; n   for (int i = 0; i < static_cast<int>((s).size()); ++i) { n     if (s[i] ==  a ) { n       st.push_back(0); n     } else if (s[i] ==  A ) { n       ans.push_back(st.back()); n       st.pop_back(); n     } else if (s[i] ==  d ) { n       st[static_cast<int>((st).size()) - 1]++; n     } n   } n   sort(ans.begin(), ans.end()); n   for (auto x : ans) cout << x <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 5, Mod = 1e9 + 7; n struct Edge { n   int nxt, to; n } e[maxn << 1]; n int hd[maxn], e_cnt = 1; n void add(int u, int v) { n   e[++e_cnt] = (Edge){hd[u], v}; n   hd[u] = e_cnt; n } n int n, pre[maxn << 2], nxt[maxn << 2], p[maxn], dep[maxn], fac[maxn]; n bool vis[maxn << 2]; n void dfs(int u) { n   for (int i = hd[u]; i; i = e[i].nxt) n     if (i != p[u]) p[e[i].to] = i ^ 1, dep[e[i].to] = dep[u] + 1, dfs(e[i].to); n } n vector<int> getRoute(int u, int v) { n   vector<int> v1, v2; n   while (dep[u] != dep[v]) { n     if (dep[u] > dep[v]) n       v1.push_back(p[u]), u = e[p[u]].to; n     else n       v2.push_back(p[v]), v = e[p[v]].to; n   } n   while (u != v) n     v1.push_back(p[u]), v2.push_back(p[v]), u = e[p[u]].to, v = e[p[v]].to; n   int siz = v2.size(); n   for (int i = siz - 1; i >= 0; i--) v1.push_back(v2[i] ^ 1); n   return v1; n } n void link(int a, int b) { n   if (nxt[a] || pre[b]) { n     cout << 0; n     exit(0); n   } n   nxt[a] = b; n   pre[b] = a; n } n void cover(int u) { n   vis[u] = true; n   if (nxt[u]) cover(nxt[u]); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   fac[0] = 1; n   for (int i = 1; i <= 5e5; i++) fac[i] = (long long)fac[i - 1] * i % Mod; n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     int a, b; n     cin >> a >> b; n     add(a, b); n     add(b, a); n   } n   dfs(1); n   int sum = 0; n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     if (!a) continue; n     if (i == a) { n       cout << 0; n       return 0; n     } n     vector<int> vec = getRoute(a, i); n     int siz = vec.size(); n     sum += siz; n     if (sum > n * 2 - 2) { n       cout << 0; n       return 0; n     } n     link(a, vec[0] + n * 2); n     link((vec[siz - 1] ^ 1) + n * 2, i + n); n     for (int j = 0; j + 1 < siz; j++) n       link((vec[j] ^ 1) + n * 2, vec[j + 1] + n * 2); n   } n   int ans = 1; n   for (int i = 1; i <= n; i++) { n     cover(i); n     if (vis[i + n]) { n       for (int j = hd[i]; j; j = e[j].nxt) n         if (!vis[j + n * 2]) { n           cout << 0; n           return 0; n         } n     } else { n       int cnt = 0; n       for (int j = hd[i]; j; j = e[j].nxt) n         if (!pre[j + n * 2]) cover(j + n * 2), cnt++; n       for (int j = hd[i]; j; j = e[j].nxt) n         if (!vis[j + n * 2]) { n           cout << 0; n           return 0; n         } n       if (vis[i + n]) cnt--; n       ans = (long long)ans * fac[cnt] % Mod; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char ch = getchar(); n   int x, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   x = ch -  0 ; n   while (1) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     x = x * 10 + ch -  0 ; n   } n   return x * f; n } n const int maxn = 510; n const int inf = 0x7fffffff; n int n, a[maxn], dp[maxn][maxn]; n int main() { n   n = read(); n   for (int i = 0; i < n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) { n     for (int j = 0, k = i - 1; k < n; j++, k++) { n       if (i == 1) { n         dp[j][k] = 1; n         continue; n       } n       dp[j][k] = dp[j + 1][k] + 1; n       for (int p = j + 2; p <= k; p++) n         if (a[p] == a[j]) n           dp[j][k] = min(dp[j][k], dp[j + 1][p - 1] + dp[p + 1][k]); n       if (a[j] == a[j + 1]) dp[j][k] = min(dp[j][k], dp[j + 2][k] + 1); n     } n   } n   printf( %d n , dp[0][n - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   long long f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n using namespace std; n long long fun(long long a[], long long b[], long long n) { n   long long ans = 0; n   long long pa[n]; n   for (long long i = 0; i < n; ++i) pa[a[i]] = i; n   long long push_back[n]; n   for (long long i = 0; i < n; ++i) push_back[b[i]] = i; n   vector<long long> freq(n); n   for (long long i = 0; i < n; ++i) { n     if (pa[i] < push_back[i]) { n       long long k = n - 1 - push_back[i] + pa[i] + 1; n       freq[k]++; n     } else { n       long long k = pa[i] - push_back[i]; n       freq[k]++; n     } n   } n   return *max_element((freq).begin(), (freq).end()); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long T = 1; n   for (long long tp = 1; tp <= T; ++tp) { n     long long n; n     cin >> n; n     long long a[n]; n     for (long long i = 0; i < n; ++i) { n       cin >> a[i]; n       a[i]--; n     } n     long long b[n]; n     for (long long i = 0; i < n; ++i) { n       cin >> b[i]; n       b[i]--; n     } n     cout << (fun(a, b, n)); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long i, j, k, l, n, m, a, b, u; n   cin >> n; n   vector<pair<string, pair<long long, long long>>> v(n); n   for (i = 0; i < n; i++) { n     cin >> v[i].first >> v[i].second.first >> v[i].second.second; n   } n   long long ans = 0; n   for (i = 1; i <= 366; i++) { n     long long m = 0, f = 0; n     for (j = 0; j < n; j++) { n       if (v[j].second.first <= i && v[j].second.second >= i) { n         if (v[j].first ==  M ) n           m++; n         else n           f++; n       } n     } n     ans = max(ans, min(m, f)); n   } n   cout << ans * 2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   int i = 0; n   while (i < n and s[i] ==  a ) i++; n   int j = i + 1; n   while (j < n and s[j] !=  a ) j++; n   if (i == n) n     s[n - 1] =  z ; n   else n     for (int k = i; k < j; k++) s[k] = (s[k] -  a  - 1 + 26) % 26 +  a ; n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n long long POW(long long a, long long b, long long MMM = MOD) { n   long long ret = 1; n   for (; b; b >>= 1, a = (a * a) % MMM) n     if (b & 1) ret = (ret * a) % MMM; n   return ret; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lcm(long long a, long long b) { n   if (a == 0 || b == 0) return a + b; n   return a * (b / gcd(a, b)); n } n int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; n int ddx[] = {2, 2, -2, -2, 1, 1, -1, -1}, ddy[] = {1, -1, 1, -1, 2, -2, 2, -2}; n int a[10][10], num[10][10], to[100]; n double d[100]; n double go(int N) { n   if (N == 99) return 0; n   double &ret = d[N]; n   if (ret != -1) return ret; n   ret = 1; n   if (N >= 94) { n     int t = 99 - N; n     for (int(i) = (1); (i) <= (6); (i) += (1)) n       if (N + i <= 99) ret += go(N + i) / 6; n     ret = ret * 6 / t; n     return ret; n   } n   for (int(i) = (1); (i) <= (6); (i) += (1)) { n     int next = N + i; n     if (to[next] != -1) n       ret += min(go(next), go(to[next])) / 6; n     else n       ret += go(next) / 6; n   } n   return ret; n } n int main() { n   fill(d, d + 100, -1); n   memset((to), -1, sizeof(to)); n   ; n   for (int(i) = (0); (i) <= (9); (i) += (1)) n     for (int(j) = (0); (j) <= (9); (j) += (1)) scanf( %d , &a[i][j]); n   for (int(i) = (0); (i) <= (9); (i) += (1)) n     for (int(j) = (0); (j) <= (9); (j) += (1)) { n       if (i & 1) n         num[i][j] = (9 - i) * 10 + j; n       else n         num[i][j] = (9 - i) * 10 + (9 - j); n     } n   for (int(i) = (0); (i) <= (9); (i) += (1)) n     for (int(j) = (0); (j) <= (9); (j) += (1)) n       if (a[i][j]) { n         to[num[i][j]] = num[i - a[i][j]][j]; n       } n   printf( %.9f , go(0)); n } n


#include <bits/stdc++.h> n using namespace std; n void swap(int &a, int &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n   return; n } n void get_output(int arr[], int n) { n   int flag = 1; n   while (flag) { n     flag = 0; n     for (int i = 0; i < n - 1; i++) { n       if (arr[i] > arr[i + 1]) { n         swap(arr[i], arr[i + 1]); n         cout << i + 1 <<     << i + 2 << endl; n         flag = 1; n       } n     } n   } n   return; n } n int main() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   get_output(arr, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = ~(1 << 31); n const int N = 200000; n int n; n char s[N + 1]; n char t[N + 1]; n int suffix_array[N + 1]; n int *eq; n int lcp[N + 1]; n vector<pair<int, int> > lcp_; n inline int add(int a, int b, int md) { n   a += b; n   if (a >= md) a -= md; n   return a; n } n inline int sub(int a, int b, int md) { n   a -= b; n   if (a < 0) a += md; n   return a; n } n int cnt[N + 2]; n pair<int, int> res[N + 1]; n void count_sort(pair<int, int> *a, int n) { n   memset(cnt, 0, 4 * n); n   for (int i = 0; i < n; ++i) ++cnt[a[i].first + 1]; n   for (int i = 1; i <= n; ++i) cnt[i] += cnt[i - 1]; n   for (int i = 0; i < n; ++i) { n     res[cnt[a[i].first]] = a[i]; n     ++cnt[a[i].first]; n   } n   for (int i = 0; i < n; ++i) a[i] = res[i]; n } n pair<int, int> substr[N + 1]; n int eq_c1[N + 1], eq_c2[N + 1]; n int *suffix_array_build(char *s, int n, int *suffix_array) { n   for (int i = 0; i < n; ++i) substr[i] = make_pair((int)s[i], i); n   sort(substr, substr + n); n   int *eq = eq_c1; n   int *eqnw = eq_c2; n   eq[substr[0].second] = 0; n   for (int i = 1; i < n; ++i) n     eq[substr[i].second] = n         eq[substr[i - 1].second] + (substr[i].first != substr[i - 1].first); n   for (int i = 1; i < n; i <<= 1) { n     for (int j = 0; j < n; ++j) n       substr[j] = make_pair(eq[sub(substr[j].second, i, n)], n                             sub(substr[j].second, i, n)); n     count_sort(substr, n); n     eqnw[substr[0].second] = 0; n     for (int j = 1; j < n; ++j) n       eqnw[substr[j].second] = eqnw[substr[j - 1].second] + n                                (substr[j].first != substr[j - 1].first || n                                 eq[add(substr[j].second, i, n)] != n                                     eq[add(substr[j - 1].second, i, n)]); n     swap(eq, eqnw); n   } n   for (int i = 0; i < n; ++i) suffix_array[i] = substr[i].second; n   return eq; n } n void lcp_build(char *s, int n, int *suffix_array, int *eq, int *lcp) { n   for (int i = 0; i < n - 1; ++i) { n     while (s[i + lcp[eq[i]]] == s[suffix_array[eq[i] - 1] + lcp[eq[i]]]) n       ++lcp[eq[i]]; n     lcp[eq[i + 1]] = max(lcp[eq[i]] - 1, 0); n   } n } n void erase_forb() { n   int mn = INF; n   for (int i = 0; i < n; ++i) { n     mn = min(mn, lcp[i]); n     if (t[suffix_array[i]] ==  0 ) { n       lcp_.push_back(make_pair(suffix_array[i], mn)); n       mn = INF; n     } n   } n } n long long calc() { n   vector<pair<int, int> > st; n   st.push_back(make_pair(0, 0)); n   long long ans = 0; n   for (int i = 0; i < lcp_.size(); ++i) { n     ans = max(ans, (long long)n - lcp_[i].first - 1); n     while (!st.empty() && lcp_[i].second <= st.back().second) st.pop_back(); n     ans = max(ans, (long long)(lcp_[i].second * (i - st.back().first + 1))); n     st.push_back(make_pair(i, lcp_[i].second)); n   } n   for (int i = 0; i < st.size() - 1; ++i) n     ans = max(ans, (long long)(lcp_.size() - st[i].first) * (st[i + 1].second)); n   return ans; n } n int main() { n   scanf( %d%s%s , &n, s, t); n   reverse(s, s + n); n   reverse(t, t + n); n   ++n; n   eq = suffix_array_build(s, n, suffix_array); n   lcp_build(s, n, suffix_array, eq, lcp); n   erase_forb(); n   printf( %lld n , calc()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x, f = 1; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) f = 0; n   for (x = c -  0 ; (c = getchar()) >=  0  && c <=  9 ;) x = x * 10 + c -  0 ; n   return f ? x : -x; n } n struct vec { n   int x, y; n } v[10 + 5]; n struct work { n   double a; n   int x, y, z; n } w[100000 * 2 + 5]; n bool cmpv(const vec& a, const vec& b) { n   return atan2(a.y, a.x) < atan2(b.y, b.x); n } n bool cmpw(const work& a, const work& b) { n   return fabs(a.a - b.a) < 1e-7 ? a.z < b.z : a.a < b.a; n } n int x[100000 + 5], y[100000 + 5], a[100000 + 5], c[100000 + 5], cn, sx, sy, n     px[100000 + 5], py[100000 + 5], t[100000 + 5]; n long long ans[100000 + 5], s[100000 + 5]; n void add(int k, int x) { n   for (; k <= 100000; k += k & -k) s[k] += x; n } n long long sum(int k) { n   long long res = 0; n   for (; k; k -= k & -k) res += s[k]; n   return res; n } n int main() { n   int k, n, q, i, j; n   k = read(); n   n = read(); n   q = read(); n   for (i = 1; i <= k; ++i) { n     v[i].x = read(); n     v[i].y = read(); n     if (v[i].x < 0) v[i].x = -v[i].x, v[i].y = -v[i].y; n     if (!v[i].x && v[i].y < 0) v[i].y = -v[i].y; n     sx += v[i].x; n     sy += v[i].y; n   } n   sort(v + 1, v + k + 1, cmpv); n   for (i = 1; i <= n; ++i) c[i] = x[i] = read(), y[i] = read(), a[i] = read(); n   sort(c + 1, c + n + 1); n   cn = unique(c + 1, c + n + 1) - c - 1; n   for (i = 1; i <= q; ++i) n     px[i] = read(), py[i] = read(), t[i] = read(), px[i] -= sx * t[i], n     py[i] -= sy * t[i], t[i] <<= 1; n   for (i = 1; i <= k; ++i) { n     if (!v[i].x) { n       for (j = 1; j <= q; ++j) py[j] += v[i].y * t[j]; n       continue; n     } n     for (j = 1; j <= n; ++j) n       w[j] = (work){y[j] - (double)x[j] * v[i].y / v[i].x, x[j], a[j], 0}; n     for (j = 1; j <= q; ++j) n       w[n + j] = (work){py[j] - (double)px[j] * v[i].y / v[i].x, px[j], n                         px[j] + v[i].x * t[j], -j}, n             px[j] += v[i].x * t[j], py[j] += v[i].y * t[j]; n     sort(w + 1, w + n + q + 1, cmpw); n     memset(s, 0, sizeof(s)); n     for (j = 1; j <= n + q; ++j) n       if (w[j].z) n         ans[-w[j].z] -= n             sum((upper_bound(c + 1, c + cn + 1, w[j].y) - c - 1)) - n             sum((upper_bound(c + 1, c + cn + 1, w[j].x - (i < 2)) - c - 1)); n       else n         add((upper_bound(c + 1, c + cn + 1, w[j].x) - c - 1), w[j].y); n   } n   for (i = 1; i <= k; ++i) { n     if (!v[i].x) break; n     for (j = 1; j <= n; ++j) n       w[j] = (work){y[j] - (double)x[j] * v[i].y / v[i].x, x[j], a[j], 0}; n     for (j = 1; j <= q; ++j) n       w[n + j] = (work){py[j] - (double)px[j] * v[i].y / v[i].x, n                         px[j] - v[i].x * t[j], px[j], j}, n             px[j] -= v[i].x * t[j], py[j] -= v[i].y * t[j]; n     sort(w + 1, w + n + q + 1, cmpw); n     memset(s, 0, sizeof(s)); n     for (j = 1; j <= n + q; ++j) n       if (w[j].z) n         ans[w[j].z] += n             sum((upper_bound(c + 1, c + cn + 1, w[j].y - (i > 1)) - c - 1)) - n             sum((upper_bound(c + 1, c + cn + 1, w[j].x - 1) - c - 1)); n       else n         add((upper_bound(c + 1, c + cn + 1, w[j].x) - c - 1), w[j].y); n   } n   for (i = 1; i <= q; ++i) printf( %I64d n , ans[i]); n } n


#include <bits/stdc++.h> n const int N_MAX = 600007; n std::map<std::pair<int, int>, int> ext; n long long ans[N_MAX], now; n int fa[N_MAX], siz[N_MAX][2], top; n std::pair<int, int> stk[N_MAX]; n int find(int x) { n   while (fa[x] != x) x = fa[x]; n   return x; n } n void merge(std::pair<int, int> v) { n   int x = find(v.first), y = find(v.second); n   if (x != y) { n     if (siz[x][0] + siz[x][1] < siz[y][0] + siz[y][1]) std::swap(x, y); n     now -= 1ll * siz[x][0] * siz[x][1] + 1ll * siz[y][0] * siz[y][1]; n     siz[x][0] += siz[y][0], siz[x][1] += siz[y][1]; n     now += 1ll * siz[x][0] * siz[x][1]; n     fa[y] = x, stk[++top] = std::make_pair(x, y); n   } n } n void undoMerge() { n   int x = stk[top].first, y = stk[top].second; n   now -= 1ll * siz[x][0] * siz[x][1]; n   siz[x][0] -= siz[y][0], siz[x][1] -= siz[y][1]; n   now += 1ll * siz[x][0] * siz[x][1] + 1ll * siz[y][0] * siz[y][1]; n   fa[y] = y, --top; n } n static const int T_MAX = 1200007; n std::vector<std::pair<int, int> > op[T_MAX]; n void modify(int k, int l, int r, int x, int y, std::pair<int, int> v) { n   if (x <= l && r <= y) { n     op[k].push_back(v); n     return; n   } n   if (x <= ((l + r) >> 1)) modify((k << 1), l, ((l + r) >> 1), x, y, v); n   if (((l + r) >> 1) < y) modify((k << 1 | 1), ((l + r) >> 1) + 1, r, x, y, v); n } n void dfs(int k, int l, int r) { n   int temp = top; n   for (auto v : op[k]) merge(v); n   if (l == r) n     ans[l] = now; n   else n     dfs((k << 1), l, ((l + r) >> 1)), dfs((k << 1 | 1), ((l + r) >> 1) + 1, r); n   while (top > temp) undoMerge(); n } n int main() { n   const int n = 3e5; n   int Q; n   scanf( %d , &Q); n   for (int i = 1; i <= Q; ++i) { n     std::pair<int, int> temp; n     scanf( %d%d , &temp.first, &temp.second), temp.second += n; n     if (ext.find(temp) == ext.end()) n       ext[temp] = i; n     else n       modify(1, 1, Q, ext[temp], i - 1, temp), ext.erase(temp); n   } n   for (auto i : ext) modify(1, 1, Q, i.second, Q, i.first); n   for (int i = 1; i <= n; ++i) fa[i] = i, siz[i][0] = 1; n   for (int i = n + 1; i <= n + n; ++i) fa[i] = i, siz[i][1] = 1; n   dfs(1, 1, Q); n   for (int i = 1; i <= Q; ++i) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int prv[100010], nxt[100010]; n int main() { n   int n, k; n   string s; n   cin >> n >> k; n   cin >> s; n   int closest = -1; n   for (int i = 0; i < s.length(); ++i) { n     if (s[i] ==  0 ) { n       closest = i; n     } n     prv[i] = closest; n   } n   closest = n; n   for (int i = s.length() - 1; i >= 0; --i) { n     if (s[i] ==  0 ) { n       closest = i; n     } n     nxt[i] = closest; n   } n   int p1 = 0, p2 = 0; n   int sum = 0; n   int ans = n; n   for (p1 = 0; p1 < s.length(); ++p1) { n     while (sum < k + 1 && p2 < s.length()) { n       sum += (s[p2] ==  0  ? 1 : 0); n       ++p2; n     } n     if (sum == k + 1) { n       int pos = p1 + (p2 - p1) / 2; n       if (nxt[pos] != n) { n         ans = min(ans, max(nxt[pos] - p1, p2 - 1 - nxt[pos])); n       } n       if (prv[pos] != -1) { n         ans = min(ans, max(prv[pos] - p1, p2 - 1 - prv[pos])); n       } n     } n     sum -= (s[p1] ==  0  ? 1 : 0); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n long long arr[N]; n int main() { n   long long n, k; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   sort(arr, arr + n); n   --k; n   int x = 0, y = 0; n   for (int i = 0; i < n; ++i) { n     if (arr[i] < arr[k / n]) x++; n     if (arr[i] == arr[k / n]) y++; n   } n   cout << arr[k / n] <<     << arr[(k - n * x) / y] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   char c, res[2] = { W ,  B }; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> c; n       if (c ==  - ) n         cout << c; n       else n         cout << res[((i % 2) + (j % 2)) % 2]; n     } n     cout << endl; n   } n   return 0; n } n


#include <iostream> n #include <algorithm> n #include <string> n using namespace std; n int main() n { n  int t, n; n  string s, b =  trygub ; n  cin >> t; n  while (t--) n  { n   int k = 0; n   cin >> n >> s; n   sort(s.begin(), s.end()); n   cout << s << endl; n  } n }


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5, base = 2333, mod = 20030731; n int n, m, a[N], b[N], pw[N], h1[N], h2[N], ans[N]; n void init() { n   pw[0] = 1; n   for (int i = (1); i <= (n - 1); i++) n     pw[i] = (long long)base * pw[i - 1] % mod, n     h1[i] = (((long long)h1[i - 1] * base + b[i]) % mod + mod) % mod; n   for (int i = n - 1; i; i--) n     h2[i] = (((long long)h2[i + 1] * base + b[i]) % mod + mod) % mod; n } n int get1(int x, int y) { n   return (h1[y] + mod - (long long)h1[x - 1] * pw[y - x + 1] % mod) % mod; n } n int get2(int x, int y) { n   return (h2[x] + mod - (long long)h2[y + 1] * pw[y - x + 1] % mod) % mod; n } n bool chk(int x, int y) { return get1(x, y) == get2(x, y); } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (1); i <= (n); i++) scanf( %d , &a[i]); n   for (int i = (1); i <= (n - 1); i++) b[i] = a[i + 1] - a[i]; n   init(); n   for (int i = (1); i <= (n); i++) { n     bool flag = 1; n     if (i > 1) flag &= chk(1, i - 1); n     if (i < n) { n       flag &= a[1] + a[i] + m == a[i + 1] + a[n]; n       if (i < n - 1) flag &= chk(i + 1, n - 1); n     } n     if (flag) ans[++*ans] = (a[1] + a[i]) % m; n   } n   sort(ans + 1, ans + 1 + *ans); n   printf( %d n , *ans); n   for (int i = (1); i <= (*ans); i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n long long max3(long long x, long long y, long long z) { n   return max(max(x, y), z); n } n long long min3(long long x, long long y, long long z) { n   return min(min(x, y), z); n } n const long long N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7, n                 inf = 1e17 + 10; n const long long INF = 1e9 + 7; n void add(long long& a, long long b) { n   a += b; n   if (a >= mod) { n     a -= mod; n   } n } n long long X[] = {0, 1, 0, -1}; n long long Y[] = {-1, 0, 1, 0}; n long long power(long long x, long long n) { n   long long result = 1; n   while (n > 0) { n     if (n % 2 == 1) result = (result * x) % mod; n     x = ((x % mod) * (x % mod)) % mod; n     n = n / 2; n   } n   return result; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<string> s(n); n     long long f[26] = {0}; n     for (long long i = 0; i < n; i++) { n       cin >> s[i]; n       for (long long j = 0; j < s[i].size(); j++) { n         f[s[i][j] -  a ]++; n       } n     } n     bool flag = true; n     for (long long i = 0; i < 26; i++) { n       if (f[i] % n != 0) flag = false; n     } n     if (flag) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long front[1000001], back[1000001], vt[1000001]; n int main() { n   long long k, sz, ko = 0, j = 0, kq = 0; n   string s; n   cin >> k >> s; n   sz = s.size(); n   s =     + s; n   for (long long i = 1; i <= sz + 1; i++) { n     front[i] = ko; n     if (s[i] ==  0 ) n       ko++; n     else n       ko = 0; n   } n   ko = 0; n   for (long long i = sz; i >= 1; i--) { n     back[i] = ko; n     if (s[i] ==  0 ) n       ko++; n     else n       ko = 0; n   } n   for (long long i = 1; i <= sz; i++) n     if (s[i] ==  1 ) j++, vt[j] = i; n   if (k == 0) { n     vt[j + 1] = sz + 1; n     front[sz + 1] = ((s[sz] ==  1 ) ? 0 : front[sz] + 1); n     j++; n     for (long long i = 1; i <= j; i++) n       kq += front[vt[i]] * (front[vt[i]] + 1) / 2; n     cout << kq; n     return 0; n   } n   for (long long i = 1; i <= j - k + 1; i++) n     kq += (front[vt[i]] + 1) * (back[vt[i + k - 1]] + 1); n   cout << kq; n } n


#include <bits/stdc++.h> n using namespace std; n vector<double> p; n double g; n int n; n int main() { n   cin >> n; n   p.resize(n + 1); n   for (int i = 0; i <= n; i++) cin >> p[i]; n   g = p[n / 2]; n   for (int i = 0; i <= (n - 1) / 2; i++) n     for (int j = n - (n - 1) / 2; j <= n; j++) n       g = max(g, (p[i] * (2 * j - n) + p[j] * (n - 2 * i)) / (j - i) / 2); n   cout << g; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (!a) return b; n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n const long long MAX = 1e6 + 2047; n const long long mod = 1e9 + 7; n const long long MAX_2 = 1009; n void sout() { cout <<   n ; } n template <typename T, typename... Types> n void sout(T var1, Types... var2) { n   cout << var1 <<    ; n   sout(var2...); n } n template <typename T> n void sout(vector<T> arr) { n   for (auto it : arr) { n     cout << it <<    ; n   } n   sout(); n } n template <typename T> n void sout(set<T> st) { n   for (auto it : st) { n     cout << it <<    ; n   } n   sout(); n } n void test_case() { n   long long n; n   cin >> n; n   queue<long long> q; n   q.push(4); n   q.push(7); n   set<long long> st; n   st.insert(4); n   st.insert(7); n   while (!q.empty()) { n     long long curr = q.front(); n     q.pop(); n     st.insert(curr); n     if (curr > n) { n       continue; n     } n     q.push(curr * 10 + 4); n     q.push(curr * 10 + 7); n   } n   vector<long long> arr(st.begin(), st.end()); n   long long ans = lower_bound(arr.begin(), arr.end(), n) - arr.begin() + 1; n   cout << ans <<   n ; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   long long t = 1; n   while (t--) { n     test_case(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   int a[n + 2], cnt = n - 1; n   for (int i = 2; i <= n; i++) { n     a[i] = i; n   } n   for (int i = 2; i < n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (a[j] == 0) continue; n       if (i * j <= n && j % i != 0) { n         a[i * j] = 0; n         cnt--; n       } n     } n   } n   cnt = 0; n   for (int i = 2; i <= n; i++) { n     if (a[i] != 0) { n       cnt++; n     } n   } n   cout << cnt << endl; n   for (int i = 2; i <= n; i++) { n     if (a[i] != 0) { n       cout << a[i] <<    ; n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100100]; n char s1[100100]; n int res[5]; n vector<int> zeros, ones; n int n; n int was[100100]; n int model() { n   memset(was, 0, sizeof(was)); n   for (int i = (0); i < (n); ++i) n     if (s[i] ==  0 ) n       zeros.push_back(i); n     else n       ones.push_back(i); n   int p1 = 0; n   int p2 = 0; n   for (int i = (0); i < (n - 2); ++i) { n     if (i & 1) n       was[zeros[p2++]] = 1; n     else n       was[ones[p1++]] = 1; n   } n   int c = 0, p = 2; n   for (int i = (0); i < (n); ++i) n     if (was[i] == 0) { n       c += (s[i] -  0 ) * p; n       p /= 2; n     } n   return c; n } n int main() { n   scanf( %s , &s); n   n = strlen(s); n   int a = 0, b = 0, c = 0; n   for (int i = (0); i < (n); ++i) s1[i] = s[i]; n   for (int i = (0); i < (n); ++i) n     if (s[i] ==  0 ) n       a++; n     else if (s[i] ==  1 ) n       b++; n     else n       c++; n   if (a + c > b) res[0] = 1; n   if (b + c > a + 1) res[3] = 1; n   if ((a + c + 1 >= b) && (b + c >= a)) { n     int v = n / 2; n     if (n & 1) v++; n     for (int i = (0); i < (n); ++i) n       if (s[i] ==  1 ) v--; n     int cnt = 0; n     for (int i = (0); i < (n); ++i) n       if (s[i] ==  ? ) { n         if (cnt < v) { n           s[i] =  1 ; n           cnt++; n         } else n           s[i] =  0 ; n       } n     int q = model(); n     res[q] = 1; n     cnt = 0; n     for (int i = (0); i < (n); ++i) s[i] = s1[i]; n     for (int i = n - 1; i >= 0; --i) n       if (s[i] ==  ? ) { n         if (cnt < v) { n           s[i] =  1 ; n           cnt++; n         } else n           s[i] =  0 ; n       } n     zeros.clear(); n     ones.clear(); n     q = model(); n     res[q] = 1; n   } n   for (int i = (0); i < (4); ++i) n     if (res[i]) { n       int v1 = (i & 1); n       int v2 = i / 2; n       cout << v2 << v1 << endl; n     } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, min = 0, k; n   scanf( %d , &n); n   for (int i = 1; i < 200; i++) { n     k = (pow(2, i) - 1) * pow(2, i - 1); n     if (k > n) { n       printf( %d , min); n       break; n     } else if ((n % k) == 0) { n       min = k; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int input(long long int n, vector<long long int> &ve); n long long int input(long long int n, vector<long long int> &ve) { n   long long int val; n   for (long long int i = 0; i < n; i++) { n     cin >> val; n     ve.push_back(val); n   } n   return 0; n } n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n, a, m, b; n   vector<long long int> vis, v; n   vector<vector<long long int>> adj; n   cin >> n >> m; n   vis.resize(n + 2, 0); n   adj.resize(n + 2); n   for (long long int i = 0; i < m; i++) { n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (long long int i = 1; i < n + 1; i++) { n     if (adj[i].size() != n - 1) { n       v.push_back(i); n     } else { n       vis[i] = 1; n     } n   } n   if (v.size() == 0) { n     cout <<  YES  << endl; n     for (long long int i = 0; i < n; i++) { n       cout <<  b ; n     } n     cout << endl; n   } else { n     vis[v[0]] = 2; n     for (long long int i = 0; i < adj[v[0]].size(); i++) { n       if (vis[adj[v[0]][i]] == 0) { n         vis[adj[v[0]][i]] = 2; n       } n     } n     long long int in = -1; n     for (long long int i = 0; i < v.size(); i++) { n       if (vis[v[i]] == 0) { n         vis[v[i]] = 3; n         in = i; n         break; n       } n     } n     long long int flag = 0; n     if (in != -1) { n       for (long long int i = 0; i < adj[v[in]].size(); i++) { n         if (vis[adj[v[in]][i]] == 0) { n           vis[adj[v[in]][i]] = 3; n         } else if (vis[adj[v[in]][i]] == 2) { n           flag = 1; n           break; n         } n       } n     } n     if (flag == 1) { n       cout <<  NO  << endl; n       return 0; n     } n     for (long long int i = 0; i < v.size(); i++) { n       long long int x = vis[v[i]]; n       if (x == 0) { n         flag = 1; n         break; n       } n     } n     long long int two = 0, three = 0; n     for (long long int i = 1; i < n + 1; i++) { n       if (vis[i] == 2) { n         two++; n       } else if (vis[i] == 3) { n         three++; n       } else { n         two++; n         three++; n       } n     } n     for (long long int i = 0; i < v.size(); i++) { n       long long int x = vis[v[i]]; n       if (x != 1) { n         if (x == 2) { n           if (adj[v[i]].size() != two - 1) { n             flag = 1; n             break; n           } n         } else { n           if (adj[v[i]].size() != three - 1) { n             flag = 1; n             break; n           } n         } n         for (long long int j = 0; j < adj[v[i]].size(); j++) { n           if (vis[adj[v[i]][j]] != x && vis[adj[v[i]][j]] != 1) { n             flag = 1; n             break; n           } n         } n       } n       if (flag == 1) { n         break; n       } n     } n     if (flag == 1) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  << endl; n       for (long long int i = 1; i < n + 1; i++) { n         if (vis[i] == 1) { n           cout <<  b ; n         } else if (vis[i] == 2) { n           cout <<  a ; n         } else { n           cout <<  c ; n         } n       } n       cout << endl; n     } n   } 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   string h, v; n   cin >> h >> v; n   bool flag = true; n   if (h[0] ==  >  && v[0] ==  v ) flag = false; n   if (h[0] ==  <  && v[m - 1] ==  v ) flag = false; n   if (h[n - 1] ==  >  && v[0] ==  ^ ) flag = false; n   if (h[n - 1] ==  <  && v[m - 1] ==  ^ ) flag = false; n   if (flag) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:255000000 ) n bool firstout = 1; n template <class T> n T &minn(T &a, T b) { n   if (b < a) a = b; n   return a; n } n template <class T> n T &maxx(T &a, T b) { n   if (a < b) a = b; n   return a; n } n int &madd(int &a, int b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n   return a; n } n int &msub(int &a, int b) { n   a -= b; n   if (a < 0) a += 1000000007; n   return a; n } n int &mmult(int &a, int b) { return a = (long long)a * b % 1000000007; } n int mdiv(long long a, long long b, long long m) { n   a = (a % m + m) % m; n   b = (b % m + m) % m; n   if (a % b == 0) return a / b; n   return (a + m * mdiv(-a, m, b)) / b; n } n int n, m, q; n int t; n int P[1012]; n char A[1012][12][12]; n pair<int, pair<int, int> > E[1012 * 1012]; n vector<pair<int, int> > R[1012]; n int B[1012]; n int root(int i) { n   if (P[i] == i) return i; n   return P[i] = root(P[i]); n } n void dfs(int i) { n   B[i] = 1; n   int j, k; n   for (k = (0); k < (R[i].size()); ++k) { n     j = R[i][k].first; n     if (B[j]) continue; n     printf((firstout) ?  %  n                          d  n                       :   %  n                          d , n            (j + 1)), n         firstout = 0; n     if (R[i][k].second == t) n       printf((firstout) ?  %  n                            d  n                         :   %  n                            d , n              (0)), n           firstout = 0; n     else n       printf((firstout) ?  %  n                            d  n                         :   %  n                            d , n              (i + 1)), n           firstout = 0; n     printf(  n ), firstout = 1; n     dfs(j); n   } n } n int main() { n   int i, j, k; n   char c; n   int a, d; n   int ts; n   for (ts = 1; scanf( %  n                       d , n                      &(n)) > 0; n        ++ts) { n     int x; n     scanf( n          %  n          d , n         &(m)); n     scanf( n          %  n          d , n         &(q)); n     scanf( n          %  n          d , n         &(x)); n     for (k = (0); k < (q); ++k) { n       for (i = (0); i < (n); ++i) n         scanf( n              %  n              s , n             &(A[k][i])); n     } n     int qq = 0; n     int kk; n     for (k = (0); k < (q); ++k) n       for (kk = (k + 1); kk < (q); ++kk) { n         int r = 0; n         for (i = (0); i < (n); ++i) n           for (j = (0); j < (m); ++j) r += A[k][i][j] != A[kk][i][j]; n         E[qq++] = make_pair(min(r * x, n * m), make_pair(k, kk)); n       } n     t = n * m; n     n = q; n     m = qq; n     for (i = (0); i < (n); ++i) P[i] = i, R[i].clear(); n     sort(E, E + m); n     int res = 0; n     for (k = (0); k < (m); ++k) { n       i = E[k].second.first; n       j = E[k].second.second; n       int ii = root(i); n       int jj = root(j); n       if (ii != jj) { n         a = E[k].first; n         res += a; n         R[i].push_back(make_pair(j, a)); n         R[j].push_back(make_pair(i, a)); n         P[ii] = jj; n       } n     } n     res += t; n     printf( n          %  n          d , n         (res)); n     printf(  n ), firstout = 1; n     printf((firstout) ?  %  n                          d  n                       :   %  n                          d , n            (1)), n         firstout = 0; n     printf((firstout) ?  %  n                          d  n                       :   %  n                          d , n            (0)), n         firstout = 0; n     printf(  n ), firstout = 1; n     for (i = (0); i < (n); ++i) B[i] = 0; n     dfs(0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, bool> g; n struct node { n   int x, y, z, num; n } ans[3000]; n int a[60], sum[60], vis[60], flag, n, final, k; n int main() { n   int i, t = 0, s, j, num = 0, tt; n   flag = 0; n   final = 0; n   scanf( %d%d , &n, &k); n   g.clear(); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   ans[++t].z = 0; n   ans[t].x = 0; n   ans[t].y = 0; n   ans[t].num = 0; n   for (i = 1; i <= n; i++) { n     tt = t; n     for (j = 1; j <= tt; j++) { n       if (g[ans[j].z + a[i]] == false) { n         g[ans[j].z + a[i]] = true; n         ans[++t].z = ans[j].z + a[i]; n         ans[t].y = a[i]; n         ans[t].x = j; n         ans[t].num = ans[j].num + 1; n         num++; n         printf( %d , ans[t].num); n         s = t; n         while (1) { n           printf(  %d , ans[s].y); n           s = ans[s].x; n           if (s == 1) break; n         } n         printf(  n ); n         if (num == k) break; n       } n     } n     if (num == k) break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 1e9 + 7, INF = 0x3f3f3f3f; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long qpow(long long a, long long n) { n   long long r = 1 % P; n   for (a %= P; n; a = a * a % P, n >>= 1) n     if (n & 1) r = r * a % P; n   return r; n } n long long inv(long long first) { n   return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; n } n inline int rd() { n   int first = 0; n   char p = getchar(); n   while (p <  0  || p >  9 ) p = getchar(); n   while (p >=  0  && p <=  9 ) first = first * 10 + p -  0 , p = getchar(); n   return first; n } n int main() { n   int n; n   scanf( %d , &n); n   while (n--) { n     int a, b; n     scanf( %d%d , &a, &b); n     long long first = (long long)a * b, t = cbrt(first) + 0.5; n     puts(t * t * t == first && a % t == 0 && b % t == 0 ?  Yes  :  No ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000; n class Interval { n  public: n   int s, e, i; n } interval[MAXN + 5]; n bool cmp_input(const Interval &a, const Interval &b) { return a.i < b.i; } n bool cmp_interval(const Interval &a, const Interval &b) { n   if (a.s != b.s) return a.s < b.s; n   return a.e < b.e; n } n int arr[MAXN + 5]; n int final[MAXN + 5]; n int ptr[MAXN + 5]; n vector<int> indx[MAXN + 5]; n int answer[MAXN + 5]; n int freq[MAXN + 5]; n int tree[MAXN + 5]; n int n, m; n void _update(int ind, int val) { n   for (int i = ind; i <= n; i += i & -i) tree[i] += val; n   return; n } n int _query(int ind) { n   int ret = 0; n   for (int i = ind; i > 0; i -= i & -i) ret += tree[i]; n   return ret; n } n void update(int s, int e, int val) { n   _update(s + 1, val); n   _update(e + 2, -val); n   return; n } n int query(int ind) { return _query(ind + 1); } n void preCalculate() { n   int c, c2; n   for (c = 0; c < n; c++) { n     if (c) answer[c] = answer[c - 1]; n     if (arr[c] > n) continue; n     freq[arr[c]]++; n     if (freq[arr[c]] == arr[c]) answer[c]++; n     if (freq[arr[c]] == arr[c] + 1) answer[c]--; n     indx[arr[c]].push_back(c); n   } n   for (c = 1; c <= n; c++) indx[c].push_back(n); n   for (c = 0; c < n; c++) update(c, c, answer[c]); n   return; n } n void add(int ind, int from, int val) { n   int to = from + 1; n   if (to >= indx[ind].size()) return; n   update(indx[ind][from], indx[ind][to] - 1, val); n   return; n } n void moveOne(int &start) { n   if (arr[start] > n) { n     start++; n     return; n   } n   add(arr[start], ptr[arr[start]] + arr[start] - 1, -1); n   add(arr[start], ptr[arr[start]] + arr[start], 1); n   ptr[arr[start]]++; n   start++; n   return; n } n int main() { n   FILE *in = stdin; n   int c, c2; n   fscanf(in,  %d%d , &n, &m); n   for (c = 0; c < n; c++) fscanf(in,  %d , &arr[c]); n   for (c = 0; c < m; c++) { n     fscanf(in,  %d%d , &interval[c].s, &interval[c].e); n     interval[c].s--; n     interval[c].e--; n     interval[c].i = c; n   } n   sort(interval, interval + m, cmp_interval); n   preCalculate(); n   int start = 0; n   for (c = 0; c < m; c++) { n     while (interval[c].s > start) moveOne(start); n     final[interval[c].i] = query(interval[c].e); n   } n   sort(interval, interval + m, cmp_input); n   for (c = 0; c < m; c++) printf( %d n , final[c]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   string s2; n   cin >> s >> s2; n   transform(s.begin(), s.end(), s.begin(), ::tolower); n   transform(s2.begin(), s2.end(), s2.begin(), ::tolower); n   if (s == s2) { n     printf( 0 n ); n   } else if (s < s2) { n     printf( -1 n ); n   } else if (s > s2) { n     printf( 1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; } n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int>> a; n   for (int i = 0; i < m; i++) { n     int q, w; n     cin >> q >> w; n     a.push_back(make_pair(q, w)); n   } n   sort(a.begin(), a.end(), comp); n   int i = 0; n   int ma = 0; n   while (n > 0 && i <= m - 1) { n     if (n >= a[i].first) { n       n -= a[i].first; n       ma += a[i].first * a[i].second; n     } else { n       ma += n * a[i].second; n       n = 0; n     } n     i++; n   } n   cout << ma << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int N; n   char in[120]; n   scanf( %d%s , &N, in); n   int len = strlen(in); n   int flag = 0; n   for (int i = 0; i < len; i++) { n     for (int d = 1; d < len; d++) { n       if (i + 4 * d >= len) break; n       if (in[i] ==  *  && in[i + d] ==  *  && in[i + 2 * d] ==  *  && n           in[i + 3 * d] ==  *  && in[i + 4 * d] ==  * ) n         flag = 1; n     } n   } n   if (flag) n     printf( yes n ); n   else n     printf( no n ); n } n int main() { n   solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const double PI = 3.141592653589793; n const double eps = 1e-6; n int n, r, R; n double x; n int main() { n   scanf( %d %d %d , &n, &R, &r); n   if (n == 1) { n     if (r <= R) n       printf( YES n ); n     else n       printf( NO n ); n     return 0; n   } n   if (r > R / 2) { n     printf( NO n ); n     return 0; n   } n   x = asin(r * 1.0 / (R - r)) * 180.0 / PI; n   if (2 * n * x > 360 + eps) n     printf( NO n ); n   else n     printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)5e3 + 10; n const int MOD = (int)1e9 + 7; n long long dp[1111][1111]; n int num[1111], k; n int mark[1111]; n long long dfs(int pos, int x, int flag) { n   if (pos == 0) return mark[x] == k - 1 && (x != 0); n   if (dp[pos][x] != -1 && !flag) { n     return dp[pos][x]; n   } n   long long ans = 0; n   int ed = flag ? num[pos] : 1; n   for (int i = 0; i <= ed; i++) { n     ans += dfs(pos - 1, x + i, flag && i == ed); n     ans %= MOD; n   } n   if (!flag) dp[pos][x] = ans; n   return ans; n } n char str[1111]; n long long solve() { n   memset(dp, -1, sizeof(dp)); n   int len = 0, l = strlen(str + 1); n   for (int i = l; i >= 1; i--) { n     num[++len] = str[i] -  0 ; n   } n   if (k == 0) return 1; n   if (k == 1) return len - 1; n   return dfs(len, 0, 1); n } n int work(int x) { n   int re = 0; n   while (x) { n     re++; n     x -= x & (-x); n   } n   return re; n } n void init() { n   mark[1] = 0; n   for (int i = 2; i <= 1000; i++) mark[i] = mark[work(i)] + 1; n } n int main() { n   init(); n   scanf( %s , str + 1); n   scanf( %d , &k); n   printf( %lld n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[(1 << 20) + 4], n; n long long store[21][2], INV = 0; n long long query(int l, int r, int no) { n   return (A + r + 1 - upper_bound(A + l, A + r + 1, no)); n } n void gen(int de, int l, int r) { n   if (!de) return; n   gen(de - 1, l, (l + r) / 2); n   gen(de - 1, (l + r) / 2 + 1, r); n   int a = l, b = (l + r) / 2, c = r; n   for (int i = a; i <= b; i++) { n     long long nos = query(b + 1, c, A[i]); n     store[de][1] += nos; n   } n   for (int i = b + 1; i <= c; i++) { n     long long nos = query(a, b, A[i]); n     store[de][0] += nos; n   } n   sort(A + l, A + r + 1); n } n int main() { n   cin >> n; n   for (int i = 1; i <= (1 << n); i++) cin >> A[i]; n   gen(n, 1, (1 << n)); n   for (int de = n; de > 0; de--) INV += store[de][0]; n   int m; n   cin >> m; n   while (m--) { n     int de; n     cin >> de; n     while (de != 0) { n       INV -= store[de][0]; n       INV += store[de][1]; n       swap(store[de][0], store[de][1]); n       de--; n     } n     printf( %lld n , INV); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m, k; n int a[N]; n int b[N]; n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n - 1; ++i) { n     b[i] = a[i + 1] - a[i] - 1; n   } n   sort(b, b + n - 1); n   int kk = n; n   long long ans = n; n   for (int i = 0; i < n - 1; ++i) { n     if (kk == k) break; n     ans += b[i]; n     kk--; n     if (kk == k) break; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n bool is_Prime(long long a) { n   for (long long i = 2; i * i <= a; ++i) n     if (a % i == 0) return false; n   return true; n } n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   return (a.second < b.second); n } n long long fact(long long n) { n   vector<long long> v; n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       if (n / i == i) n         v.push_back(i); n       else { n         v.push_back(i); n         v.push_back(n / i); n       } n     } n   } n   return v.size(); n } n void C(long long n, long long r) { n   vector<vector<long long>> c(n + 1, vector<long long>(r + 1, 0)); n   for (long long i = 0; i < n + 1; i++) { n     for (long long j = 0; j <= i && j <= r; j++) n       if (j == 0 || j == i) n         c[i][j] = 1; n       else n         c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % 1000000007; n   } n } n bool is_pl(string s, int n) { n   for (long long i = 0; i < n / 2; i++) { n     if (s[i] != s[n - i - 1]) return false; n   } n   return true; n } n void solve() { n   int n, k; n   cin >> n >> k; n   int cnt[5001] = {0}; n   vector<pair<int, int>> v(n); n   for (long long i = 0; i < n; i++) { n     cin >> v[i].first; n     cnt[v[i].first]++; n     v[i].second = (int)i; n   } n   for (long long i = 0; i < 5001; i++) { n     if (cnt[i] > k) { n       cout <<  NO n ; n       return; n     } n   } n   sort(v.begin(), v.end()); n   vector<int> ans(n); n   int a = 1; n   for (long long i = 0; i < n; i++) { n     ans[v[i].second] = a; n     a++; n     if (a > k) a = 1; n   } n   cout <<  YES n ; n   for (long long i = 0; i < n; i++) cout << ans[i] <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   ; n   int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void smain(); n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(7); n   cout << fixed; n   smain(); n   return 0; n } n long long d[100001], d2[100001], r[100001], s[100001], sz[100001], c[100001]; n struct node { n   long long cnt, ps, pcnt; n }; n map<long long, node> a[100001]; n vector<long long> edges[100001]; n long long root = -1, dr = -1; n void dfs0(long long v, long long p = -1) { n   if (d[v] >= dr) root = v, dr = d[v]; n   for (auto i : edges[v]) n     if (i != p) { n       c[i] = c[v]; n       d[i] = d[v] + 1; n       dfs0(i, v); n     } n } n void dfs(long long v, long long p = -1) { n   d[v] = 0; n   for (auto i : edges[v]) n     if (i != p) { n       dfs(i, v); n       d[v] = max(d[v], d[i] + 1); n     } n } n void dfs2(long long v, long long x, long long p = -1) { n   long long mx1 = 0, mx2 = 0; n   for (auto i : edges[v]) n     if (i != p) { n       if (d[i] + 1 > mx1) n         mx2 = mx1, mx1 = d[i] + 1; n       else if (d[i] + 1 > mx2) n         mx2 = d[i] + 1; n     } n   d2[v] = max(d[v], x); n   for (auto i : edges[v]) n     if (i != p) { n       long long t = d[i] + 1 == mx1 ? mx2 : mx1; n       dfs2(i, max(t + 1, x + 1), v); n     } n } n void smain() { n   long long n, m, q; n   cout.precision(8); n   cout << fixed; n   for (; cin >> n >> m >> q;) { n     for (long long i = 0; i < n; i++) edges[i].clear(); n     memset(c, 255, sizeof(long long) * n); n     for (long long i = 0; i < m; i++) { n       long long u, v; n       cin >> u >> v; n       u -= 1, v -= 1; n       edges[u].push_back(v); n       edges[v].push_back(u); n     } n     long long k = 0; n     for (long long i = 0; i < n; i++) n       if (c[i] == -1) { n         c[i] = k++; n         root = -1, dr = -1; n         dfs0(i); n         r[k - 1] = root; n         dfs(root); n         dfs2(root, 0); n       } n     memset(s, 0, sizeof(long long) * k); n     memset(sz, 0, sizeof(long long) * k); n     for (long long i = 0; i < k; i++) a[i].clear(); n     for (long long i = 0; i < n; i++) { n       s[c[i]] += d2[i], sz[c[i]] += 1; n       a[c[i]][d2[i]].cnt += 1; n     } n     for (long long i = 0; i < k; i++) { n       long long ps = 0, pcnt = 0; n       for (auto &j : a[i]) { n         ps += j.first * j.second.cnt; n         pcnt += j.second.cnt; n         j.second.ps = ps; n         j.second.pcnt = pcnt; n       } n     } n     map<pair<long long, long long>, long double> cache; n     for (long long _i = 0; _i < q; _i++) { n       long long u = 0, v = n - 1; n       cin >> u >> v; n       u -= 1, v -= 1; n       if (c[u] == c[v]) n         cout <<  -1 n ; n       else { n         u = c[u], v = c[v]; n         if (sz[u] > sz[v] || (sz[u] == sz[v] || u > v)) swap(u, v); n         pair<long long, long long> key(u, v); n         if (cache.count(key)) { n           cout << cache.at(key) <<   n ; n           continue; n         } n         long long t = s[u] * sz[v] + s[v] * sz[u] + sz[u] * sz[v]; n         long long mn = max(d[r[u]], d[r[v]]); n         for (auto it : a[u]) { n           if (it.first + a[v].begin()->first + 1 >= mn) break; n           auto jt = a[v].upper_bound(mn - it.first - 1); n           if (jt != a[v].begin()) { n             jt = prev(jt); n             long long j = jt->second.pcnt; n             t -= jt->second.ps * it.second.cnt + j * it.first * it.second.cnt + n                  j * it.second.cnt; n             t += j * it.second.cnt * mn; n           } n         } n         long double res = (long double)t / (sz[u] * sz[v]); n         cache.emplace(key, res); n         cout << res <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n long long int qpow(long long int x, long long int y) { n   long long int ans = 1; n   while (y) { n     if (y & 1) ans = ans * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n const int maxn = 1024 * 2; n long long int dp[maxn * 2][maxn][2]; n long long int inv[maxn * 4]; n long long int Solve(int n, int k) { n   dp[1][0][1] = 1; n   long long int tem = qpow(2, mod - 2); n   long long int ans = 0; n   for (int i = 1; i < maxn * 4; i++) inv[i] = qpow(i, mod - 2); n   for (int i = 1; i <= 2 * n; i++) { n     for (int j = 0; j <= n; j++) { n       if (j >= k) { n         ans = (ans + dp[i][j][0]) % mod; n         ans = (ans + dp[i][j][1]) % mod; n       } n       if (j - 1 <= (2 * n - i) && j >= 1) { n         if (j - 1 == 2 * n - i) { n           dp[i + 1][j - 1][0] = (dp[i + 1][j - 1][0] + dp[i][j][0]) % mod; n         } else if (j == 1) { n           dp[i + 1][j - 1][1] = (dp[i + 1][j - 1][1] + dp[i][j][0]) % mod; n         } else { n           long long int send = (2 * n - (i + j - 1)); n           long long int reci = j - 1; n           long long int invt = inv[send + reci]; n           dp[i + 1][j - 1][1] = n               (dp[i + 1][j - 1][1] + dp[i][j][0] * send % mod * invt % mod) % n               mod; n           dp[i + 1][j - 1][0] = n               (dp[i + 1][j - 1][0] + dp[i][j][0] * reci % mod * invt % mod) % n               mod; n         } n       } n       if (j + 1 <= (2 * n - i)) { n         if (j + 1 == (2 * n - i)) { n           dp[i + 1][j + 1][0] = (dp[i + 1][j + 1][0] + dp[i][j][1]) % mod; n         } else { n           long long int send = (2 * n - (i + j + 1)); n           long long int reci = j + 1; n           long long int invt = inv[send + reci]; n           dp[i + 1][j + 1][1] = n               (dp[i + 1][j + 1][1] + dp[i][j][1] * send % mod * invt % mod) % n               mod; n           dp[i + 1][j + 1][0] = n               (dp[i + 1][j + 1][0] + dp[i][j][1] * reci % mod * invt % mod) % n               mod; n         } n       } n     } n   } n   return ans; n } n int main() { n   int n, k, l; n   scanf( %d%d%d , &n, &k, &l); n   long long int ans = Solve(n, k) * qpow(2 * n + 1, mod - 2) % mod * l % mod; n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n struct Query { n   int l, r, x; n   Query(int l = 0, int r = 0, int x = 0) : l(l), r(r), x(x) {} n }; n struct node { n   int pref, suff, val; n   node(int pref = 0, int suff = 0, int val = 0) n       : pref(pref), suff(suff), val(val) {} n }; n int n, k; n map<int, int> ha; n int hacnt; n int a[100005]; n node st[4000000]; n int lazy[4000000]; n Query queries[100005]; n int ori[1000005]; n bool changed[1000000]; n set<int> vals; n int mn; n int st1[5000000]; n void build1(int p = 1, int l = 0, int r = n - 1) { n   if (l == r) { n     st1[p] = a[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build1(p * 2, l, mid); n   build1(p * 2 + 1, mid + 1, r); n   st1[p] = min(st1[p * 2], st1[p * 2 + 1]); n } n int query1(int i, int j, int p = 1, int l = 0, int r = n - 1) { n   if (i > r || j < l) return 1e9 + 3; n   if (i <= l && j >= r) return st1[p]; n   int mid = (l + r) >> 1; n   int c1 = query1(i, j, p * 2, l, mid); n   int c2 = query1(i, j, p * 2 + 1, mid + 1, r); n   return min(c1, c2); n } n int get(int l, int r) { n   if (l > r) return 1000000008; n   if (r - l + 1 >= n) return mn; n   l %= n, r %= n; n   if (l > r) return min(query1(l, n - 1), query1(0, r)); n   return query1(l, r); n } n void merge(node& ret, node& x, node& y) { n   ret.pref = x.pref; n   ret.suff = y.suff; n   ret.val = min(x.val, x.suff); n   ret.val = min(ret.val, min(y.val, y.pref)); n } n void pushLazy(int p) { n   if (lazy[p]) { n     lazy[p * 2] = lazy[p * 2 + 1] = lazy[p]; n     st[p] = node(1000000008, 1000000008, lazy[p]); n     lazy[p] = 0; n   } n } n void build(int p = 1, int l = 1, int r = hacnt - 1) { n   if (l == r) { n     if (l != 1) n       st[p].pref = get(ori[l - 1] + 1, ori[l] - 1); n     else n       st[p].pref = 1000000008; n     if (r != hacnt - 1) n       st[p].suff = get(ori[r] + 1, ori[r + 1] - 1); n     else n       st[p].suff = 1000000008; n     st[p].val = get(ori[l], ori[l]); n     return; n   } n   int mid = (l + r) >> 1; n   build(p * 2, l, mid); n   build(p * 2 + 1, mid + 1, r); n   merge(st[p], st[p * 2], st[p * 2 + 1]); n } n void update(int i, int j, int x, int p = 1, int l = 1, int r = hacnt - 1) { n   pushLazy(p); n   if (i > r || j < l) return; n   if (i <= l && j >= r) { n     lazy[p] = x; n     pushLazy(p); n     return; n   } n   int mid = (l + r) >> 1; n   update(i, j, x, p * 2, l, mid); n   update(i, j, x, p * 2 + 1, mid + 1, r); n   merge(st[p], st[p * 2], st[p * 2 + 1]); n } n int query(int i, int j, int p = 1, int l = 1, int r = hacnt - 1) { n   pushLazy(p); n   if (i > r || j < l) return 1000000008; n   if (i <= l && j >= r) { n     int ret = 1000000008; n     if (i != l) ret = min(ret, st[p].pref); n     if (j != r) ret = min(ret, st[p].suff); n     ret = min(ret, st[p].val); n     return ret; n   } n   int mid = (l + r) >> 1; n   int c1 = query(i, j, p * 2, l, mid); n   int c2 = query(i, j, p * 2 + 1, mid + 1, r); n   return min(c1, c2); n } n int main() { n   scanf( %d%d , &n, &k); n   mn = 1e9 + 1; n   for (int i = 0; i < n; i++) scanf( %d , &a[i]), mn = min(mn, a[i]); n   build1(); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int l, r, x; n       scanf( %d%d%d , &l, &r, &x); n       l--, r--; n       queries[i] = Query(l, r, x); n       ha[l]; n       ha[r]; n     } else { n       int l, r; n       scanf( %d%d , &l, &r); n       l--, r--; n       ha[l]; n       ha[r]; n       queries[i] = Query(l, r, -1); n     } n   } n   hacnt = 1; n   for (map<int, int>::iterator it = ha.begin(); it != ha.end(); it++) { n     it->second = hacnt; n     ori[hacnt] = it->first; n     vals.insert(hacnt); n     hacnt++; n   } n   build(); n   for (int i = 0; i < q; i++) { n     int l = queries[i].l, r = queries[i].r, x = queries[i].x; n     if (x < 0) { n       int ans = query(ha[l], ha[r]); n       printf( %d n , ans); n     } else { n       update(ha[l], ha[r], x); n       int from = ha[l]; n       int to = ha[r]; n       while (1) { n         set<int>::iterator it = vals.lower_bound(from); n         if (it == vals.end() || *it > to) break; n         changed[*it] = 1; n         vals.erase(it); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #define ll long long int n #define ld long double n #define NMAX 1009 n using namespace std; n ll mask; n ll u,d,r,l; n ll n; n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     ll t; n     cin>>t; n     while(t--) n     { n     cin>>n>>u>>r>>d>>l; n     bool ok=0; n     for(mask=0;mask<16;mask++) n         { n             ll cu=u; n         ll cd=d; n         ll cl=l; n         ll cr=r; n        if(mask & 1) n            {cu--;cl--;} n         if(mask & 2) n            {cu--;cr--;} n           n         if(mask & 4) n            {cl--;cd--;} n          if(mask & 8) n            {cd--;cr--;} n         if(cd>=0 && cd<=n-2 && cu>=0 && cu<=n-2 && cl>=0 && cl<=n-2 && cr>=0 && cr<=n-2) n            {ok=1;break;}    n               n         } n      if(!ok) n      cout<< NO n ; n      else n      cout<< YES n ;    n     } n  n  n     return 0; n } n ///limitele si tipul de date!! n


#include <bits/stdc++.h> n const int max_N = 1000 + 21; n int n, a[max_N], b[max_N], cnt[max_N]; n std::vector<int> vec, _vec, ret[max_N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     scanf( %d%d , a + i, b + i); n     if (a[i] > b[i]) std::swap(a[i], b[i]); n     if (b[i] != n) return puts( NO ), 0; n     ++cnt[a[i]]; n   } n   for (int i = 1; i < n; ++i) n     if (!cnt[i]) { n       vec.push_back(i); n     } else { n       _vec.push_back(i); n     } n   std::reverse(vec.begin(), vec.end()); n   std::queue<int> Q; n   int i = n - 1; n   for (auto u : vec) { n     while (i && i > u) { n       if (cnt[i]--) { n         while (cnt[i]--) Q.push(i); n       } n       --i; n     } n     if (Q.empty()) return puts( NO ), 0; n     int v = Q.front(); n     ret[v].push_back(u); n     Q.pop(); n   } n   if (!Q.empty()) return puts( NO ), 0; n   puts( YES ); n   for (auto u : _vec) { n     if (ret[u].empty()) { n       printf( %d %d n , u, n); n     } else { n       printf( %d %d n , ret[u][0], n); n       for (auto i = 1; i < ret[u].size(); ++i) { n         printf( %d %d n , ret[u][i - 1], ret[u][i]); n       } n       printf( %d %d n , ret[u].back(), u); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n char a[100050], b[100050]; n int sa[100050], sb[100050], ta[100050], tb[100050], Q; n int main() { n   scanf( %s%s , a + 1, b + 1); n   int la = strlen(a + 1), lb = strlen(b + 1); n   for (int i = 1; i <= la; i++) { n     if (a[i] ==  A ) sa[i] = sa[i - 1] + 1; n     sb[i] = sb[i - 1] + (a[i] ==  B  || a[i] ==  C ); n   } n   for (int i = 1; i <= lb; i++) { n     if (b[i] ==  A ) ta[i] = ta[i - 1] + 1; n     tb[i] = tb[i - 1] + (b[i] ==  B  || b[i] ==  C ); n   } n   int l, r, lsa, lta, ssb, stb; n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d%d , &l, &r); n     lsa = std::min(r - l + 1, sa[r]), ssb = sb[r] - sb[l - 1]; n     scanf( %d%d , &l, &r); n     lta = std::min(r - l + 1, ta[r]), stb = tb[r] - tb[l - 1]; n     if (lsa < lta || ((ssb & 1) != (stb & 1)) || stb < ssb) n       printf( 0 ); n     else if (lsa > lta && stb > ssb || n              ((lsa - lta) % 3 == 0 && (ssb || (ssb == stb)))) n       printf( 1 ); n     else n       printf( 0 ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 123466; n bool is[N]; n vector<int> adj[N]; n int dp[N]; n int mx_dep[N]; n const int inf = 1 << 22; n pair<int, int> calc_dp(int v, int p) { n   int res = 0; n   int mx = -inf; n   if (is[v]) mx = 0; n   for (auto u : adj[v]) n     if (u != p) { n       auto cur = calc_dp(u, v); n       if (cur.first == -inf) continue; n       res += cur.first + 1; n       res += cur.second + 1; n       mx = max(mx, cur.first + 1); n     } n   if (mx != -inf) res -= mx; n   dp[v] = res, mx_dep[v] = mx; n   return {mx, res}; n } n int ans = inf; n int ans_pos = 1; n int final_ans[N]; n void calc(int v, int p, int load_dep, int load_res) { n   int added_dep = min(load_dep, mx_dep[v]); n   int cur_ans = load_res + dp[v] + added_dep; n   if (cur_ans < ans) ans = cur_ans, ans_pos = v; n   if (cur_ans == ans && v < ans_pos) ans_pos = v; n   final_ans[v] = cur_ans; n   multiset<int> s; n   s.insert(load_dep); n   int nload = load_dep + load_res; n   for (auto u : adj[v]) n     if (u != p) { n       if (mx_dep[u] == -inf) continue; n       s.insert(mx_dep[u] + 1); n       nload += mx_dep[u] + 1 + dp[u] + 1; n     } n   for (auto u : adj[v]) n     if (u != p) { n       if (mx_dep[u] == -inf) continue; n       int cur_nload = nload - mx_dep[u] - 1 - dp[u] - 1; n       s.erase(s.find(mx_dep[u] + 1)); n       int mx = *s.rbegin(); n       cur_nload -= mx; n       calc(u, v, mx + 1, cur_nload + 1); n       s.insert(mx_dep[u] + 1); n     } n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   int root = 1; n   for (int i = 0; i < m; i++) { n     int x; n     scanf( %d , &x); n     is[x] = true; n     root = x; n   } n   calc_dp(root, 0); n   calc(root, 0, 0, 0); n   printf( %d n%d n , ans_pos, ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   int dp[n][n]; n   memset(dp, 0, sizeof dp); n   int s = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = i - 1; j >= 0; --j) n       if (a[j] > a[i]) n         dp[i][j] = dp[i][j + 1] + 1; n       else n         dp[i][j] = dp[i][j + 1]; n     for (int j = i + 1; j < n; ++j) n       if (a[j] > a[i]) n         dp[i][j] = dp[i][j - 1] + 1; n       else n         dp[i][j] = dp[i][j - 1]; n     s += dp[i][0]; n   } n   int cnt = 0, m = INT_MAX; n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       int tmp = s + dp[i][j] + dp[i][j - 1] - dp[j][i] - dp[j][i + 1]; n       if (tmp < m) { n         m = tmp; n         cnt = 1; n       } else if (tmp == m) n         cnt++; n     } n   } n   cout << m <<     << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n FILE* g_f_; n int g_s_; n char* g_g_; n template <class _T> n inline _T sqr(const _T& x) { n   return x * x; n } n template <class _T> n inline string tostr(const _T& a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const long double PI = 3.1415926535897932384626433832795; n const long double EPS = 1e-11; n signed long long n; n int k; n signed long long a[1024000]; n int main() { n   cout << setiosflags(ios::fixed) << setprecision(10); n   cin >> n; n   if (n % 3 != 0) { n     puts( 0 ); n     return 0; n   } n   n /= 3; n   k = 0; n   for (int i = (1); i <= (int)(10000000); i++) { n     signed long long x = i; n     x *= x; n     if (x > n) break; n     if (n % i == 0) { n       a[k++] = i; n       if (x != n) a[k++] = n / i; n     } n   } n   sort(a, a + k); n   int cma = (int)(pow(n / 2.0, 1.0 / 3.0) + 1e-7); n   signed long long ans = 0; n   a[k] = n + 1; n   for (int c = (1); c <= (int)(cma); c++) { n     int bma = (int)(pow(n / 2.0 / c, 1.0 / 2.0) + 1e-7); n     signed long long b = c; n     while (b <= bma) { n       if (n % (b + c) != 0) { n         int t = upper_bound(a, a + k, n / (b + c)) - a; n         if (t == 0) break; n         signed long long y = a[t - 1]; n         signed long long nb = n / y - c; n         if (nb < b) nb = b + 1; n         b = nb; n         continue; n       } n       signed long long bb = b + c; n       signed long long cc = b * (signed long long)c - n / (b + c); n       signed long long dd = bb * bb - 4LL * cc; n       signed long long z = (int)(sqrt(dd) + 1e-7); n       signed long long a = (-bb + z) / 2; n       if (a < b) break; n       if (z * z != dd) goto l1; n       if (a != b && b != c) n         ans += 6; n       else if (a != b || b != c) n         ans += 3; n       else n         ans++; n     l1:; n       b++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n bool b1[N]; n int Z, num[N], in[N], dp[N][20], dep[N], freq[2][N], f[N]; n long long fans[N]; n vector<int> g[N], v; n map<int, int> mp; n struct qq { n   int in, l, r, c; n   qq(int a, int b, int e, int d) { in = a, l = b, r = e, c = d; } n   bool operator<(const qq& s) const { n     if (l / Z != s.l / Z) return l / Z < s.l / Z; n     return r / Z < s.r / Z; n   } n }; n vector<qq> q; n void dfs(int u, int p, int d) { n   dep[u] = d; n   dp[u][0] = p; n   in[u] = v.size(); n   v.push_back(u); n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (v != p) dfs(v, u, d + 1); n   } n   v.push_back(u); n } n int lca(int a, int b) { n   if (dep[a] < dep[b]) swap(a, b); n   int x = dep[a] - dep[b]; n   for (int i = 0; i < 20; i++) { n     if ((1 << i) & x) a = dp[a][i]; n   } n   if (a == b) return a; n   for (int i = 19; i >= 0; i--) { n     if (dp[a][i] != dp[b][i]) { n       a = dp[a][i], b = dp[b][i]; n     } n   } n   return dp[a][0]; n } n long long ans; n int l = 0, r = -1; n void add(int in, int d) { n   int x = v[in]; n   f[x] += d; n   if (d == 1 && f[x] == 2) n     d = -1; n   else if (d == -1 && f[x] == 1) n     d = 1; n   ans += d * freq[!b1[x]][num[x]]; n   freq[b1[x]][num[x]] += d; n } n void fix(int L, int R) { n   while (l < L) { n     add(l, -1); n     l++; n   } n   while (l > L) { n     l--; n     add(l, 1); n   } n   while (r < R) { n     r++; n     add(r, 1); n   } n   while (r > R) { n     add(r, -1); n     r--; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , b1 + i); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , num + i); n     if (mp.find(num[i]) == mp.end()) mp[num[i]] = mp.size(); n     num[i] = mp[num[i]]; n   } n   for (int i = 1, a, b; i < n; i++) { n     scanf( %d%d , &a, &b); n     g[a].push_back(b), g[b].push_back(a); n   } n   dfs(1, -1, 0); n   Z = sqrt(v.size() + 0.0) + 1; n   for (int k = 1; k < 20; k++) { n     for (int i = 1; i <= n; i++) { n       if (dp[i][k - 1] == -1) n         dp[i][k] = -1; n       else n         dp[i][k] = dp[dp[i][k - 1]][k - 1]; n     } n   } n   int m; n   scanf( %d , &m); n   for (int i = 0, a, b, c; i < m; i++) { n     scanf( %d%d , &a, &b); n     c = lca(a, b); n     bool f = (c != a) & (c != b); n     q.push_back(qq(i, min(in[a], in[b]) + f, max(in[a], in[b]), c * f)); n   } n   sort(q.begin(), q.end()); n   for (int i = 0; i < m; i++) { n     fix(q[i].l, q[i].r); n     if (q[i].c) add(in[q[i].c], 1); n     fans[q[i].in] = ans; n     if (q[i].c) add(in[q[i].c], -1); n   } n   for (int i = 0; i < m; i++) { n     printf( %I64d n , fans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, s = 0, x, m = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     s += x; n     m = max(m, x); n   } n   long long t = s / (n - 1); n   if (s % (n - 1)) t++; n   cout << max(m, t); n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[5000010], f[5000010]; n int main() { n   long long t, l, r, i, j, a, b, mod, ans, k = 1, final = 0; n   mod = 1e9 + 7; n   scanf( %lld %lld %lld , &t, &l, &r); n   for (i = 2; i <= r; i++) { n     if (dp[i] == 0) { n       for (j = i; j <= r; j += i) n         if (!dp[j]) dp[j] = i; n     } n     a = dp[i]; n     b = i / dp[i]; n     if (b == 1) n       f[i] = (i * (i - 1) / 2) % mod; n     else n       f[i] = (b * f[a] + f[b]) % mod; n     if (i >= l) { n       final = (final + k * f[i]) % mod; n       k = (k * t) % mod; n     } n   } n   printf( %lld n , final); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   register char c =    ; n   register int v = 0, x = 1; n   while ((c <  0 ) || (c >  9 )) { n     if (c ==  - ) x = -1; n     c = getchar(); n   } n   while ((c >=  0 ) && (c <=  9 )) { n     v = (v << 1) + (v << 3) + (c ^ 48); n     c = getchar(); n   } n   return v * x; n } n inline void write(register int x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 ^ 48); n } n const int N = 2e5 + 10; n int ans, fa[N], dep[N], dp[N][3]; n int n, cnt, head[N], v[N], nxt[N], ind[N]; n inline void add(register int x, register int y) { n   ind[x]++, ind[y]++; n   v[++cnt] = y, nxt[cnt] = head[x], head[x] = cnt; n   v[++cnt] = x, nxt[cnt] = head[y], head[y] = cnt; n } n inline void dfs(register int u) { n   dep[u] = dep[fa[u]] + 1; n   dp[u][0] = 0, dp[u][1] = 1; n   for (register int i = head[u]; i; i = nxt[i]) n     if (v[i] != fa[u]) { n       fa[v[i]] = u; n       dfs(v[i]); n       ans = max(ans, dp[u][0] + dp[v[i]][0]); n       ans = max(ans, dp[u][1] + dp[v[i]][0]); n       ans = max(ans, dp[u][0] + dp[v[i]][1]); n       dp[u][0] = max(dp[u][0], dp[v[i]][0] + ind[u] - 2); n       dp[u][0] = max(dp[u][0], dp[v[i]][1] + ind[u] - 2); n       dp[u][1] = max(dp[u][1], dp[v[i]][0] + 1); n     } n   ans = max(ans, dp[u][1]); n } n int main() { n   n = read(); n   for (register int i = 1; i < n; i++) add(read(), read()); n   dfs(1); n   write(ans); n   return ~~(0 - 0); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m = 3, l, c[20000]; n char s[500]; n int main() { n   cin >> n; n   while (c[m] <= n) m++, c[m] = (long long)m * (m - 1) * (m - 2) * (m - 3) / 24; n   for (int i = m - 1; i >= 1; i--) { n     while (c[i] >= 1 && n >= c[i]) { n       s[++l] =  a ; n       n -= c[i]; n     } n     s[++l] =  b ; n   } n   printf( %s abbbb n , s + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int px[4000010]; n int py[4000010]; n char c[2010][2010]; n int n, m; n bool pd(int x, int y, int d) { n   if (x < 0 || y < 0 || x + d >= n || y + d >= m) return false; n   for (int t = 1; t <= px[0]; t++) n     if ((px[t] != x) && (px[t] != x + d) && (py[t] != y) && (py[t] != y + d)) n       return false; n   for (int i = 0; i <= d; i++) { n     if (c[x + i][y] ==  . ) c[x + i][y] =  + ; n     if (c[x][y + i] ==  . ) c[x][y + i] =  + ; n     if (c[x + i][y + d] ==  . ) c[x + i][y + d] =  + ; n     if (c[x + d][y + i] ==  . ) c[x + d][y + i] =  + ; n   } n   for (int i = 0; i < n; i++) printf( %s n , c[i]); n   return true; n } n int main() { n   int i, j; n   scanf( %d%d , &n, &m); n   int x0 = n, x1 = -1, y0 = m, y1 = -1; n   px[0] = 0; n   py[0] = 0; n   for (i = 0; i < n; i++) { n     scanf( %s , c[i]); n     for (j = 0; j < m; j++) { n       if (c[i][j] ==  w ) { n         px[++px[0]] = i; n         py[++py[0]] = j; n         x0 = min(x0, i); n         x1 = max(x1, i); n         y0 = min(y0, j); n         y1 = max(y1, j); n       } n     } n   } n   if (px[0] > n * 4) { n     printf( -1 n ); n     return 0; n   } n   int d = max(x1 - x0, y1 - y0); n   if (pd(x0, y0, d)) return 0; n   if (pd(x0, max(y1 - d, 0), d)) return 0; n   if (pd(max(x1 - d, 0), y0, d)) return 0; n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char Ma[4105]; n int Mp[4105]; n void Manacher(char s[], int len) { n   int l = 0; n   Ma[l++] =  $ ; n   Ma[l++] =  # ; n   for (int i = 0; i < len; i++) { n     Ma[l++] = s[i]; n     Ma[l++] =  # ; n   } n   Ma[l] = 0; n   int mx = 0, id = 0; n   for (int i = 0; i < l; i++) { n     Mp[i] = mx > i ? min(Mp[id * 2 - i], mx - i) : 1; n     while (Ma[i + Mp[i]] == Ma[i - Mp[i]]) Mp[i]++; n     if (i + Mp[i] > mx) { n       mx = i + Mp[i]; n       id = i; n     } n   } n } n char s[4105]; n long long f[4105]; n int ans, cnt; n int tmp[2005 * 2005]; n int main() { n   scanf( %s , s); n   int len = strlen(s); n   Manacher(s, len); n   long long ans = 0; n   for (int i = 1; i < 2 * len + 2; i++) { n     for (int j = 1; j < Mp[i]; j++) n       if (Ma[i - j + 1] !=  # ) { n         tmp[++cnt] = i + j - 1; n         f[i - j + 1]++; n       } n   } n   for (int i = 2 * len + 2; ~i; i--) f[i] += f[i + 1]; n   for (int i = 1; i <= cnt; i++) ans += f[tmp[i] + 1]; n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1}; n const int dc[]{0, 1, 1, 1, 0, -1, -1, -1}; n void run() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const int sqrtQ = 150; n struct suffix_automaton { n   struct state { n     int len, link = 0, cnt = 0; n     bool terminal = false, is_clone = false; n     vector<int> nxt; n     state(int len = 0) : len(len), nxt(26, -1) {} n     bool have_next(char ch) { return nxt[ch -  a ] != -1; } n     void clone(const state &other, int nlen) { n       len = nlen; n       nxt = other.nxt; n       link = other.link; n       is_clone = true; n     } n   }; n   vector<state> st; n   int last = 0; n   suffix_automaton() { n     st.push_back(state()); n     st[0].link = -1; n   } n   suffix_automaton(const string &s) : suffix_automaton() { n     for (char ch : s) extend(ch); n     calc_number_of_occurrences(); n   } n   void extend(char c) { n     int cur = st.size(); n     st.push_back(state(st[last].len + 1)); n     st[cur].cnt = 1; n     int p = last; n     last = cur; n     while (p != -1 && !st[p].have_next(c)) { n       st[p].nxt[c -  a ] = cur; n       p = st[p].link; n     } n     if (p == -1) return; n     int q = st[p].nxt[c -  a ]; n     if (st[p].len + 1 == st[q].len) { n       st[cur].link = q; n       return; n     } n     int clone = st.size(); n     st.push_back(state()); n     st[clone].clone(st[q], st[p].len + 1); n     while (p != -1 && st[p].nxt[c -  a ] == q) { n       st[p].nxt[c -  a ] = clone; n       p = st[p].link; n     } n     st[q].link = st[cur].link = clone; n   } n   void calc_number_of_occurrences() { n     vector<vector<int>> lvl(st[last].len + 1); n     for (int i = 1; i < st.size(); i++) lvl[st[i].len].push_back(i); n     for (int i = st[last].len; i >= 0; i--) n       for (auto cur : lvl[i]) st[st[cur].link].cnt += st[cur].cnt; n   } n   int count(const string &y) { n     int cur = 0; n     for (auto &it : y) { n       if (!st[cur].have_next(it)) return 0; n       cur = st[cur].nxt[it -  a ]; n     } n     return st[cur].cnt; n   } n } SA[(int)1e4]; n string y; n vector<int> longestPrefix; n vector<int> Fail[26]; n int fail(int k, char nxt) { n   int &rt = Fail[nxt -  a ][k]; n   if (~rt) return rt; n   while (k > 0 && y[k] != nxt) k = longestPrefix[k - 1]; n   if (nxt == y[k]) k++; n   return rt = k; n } n void failure_function() { n   int n = y.size(); n   longestPrefix = vector<int>(n); n   for (int i = 0; i < 26; i++) Fail[i] = vector<int>(n + 1, -1); n   for (int i = 1, k = 0; i < n; i++) longestPrefix[i] = k = fail(k, y[i]); n } n int main() { n   run(); n   string s; n   int q; n   cin >> s >> q; n   for (int i = 0; i < (int)(s.size()); i += sqrtQ) n     SA[i / sqrtQ] = suffix_automaton(s.substr(i, sqrtQ)); n   auto solve = [&](int l, int r) { n     int match = 0, cnt = 0; n     for (int i = l; i <= r; i++) { n       match = fail(match, s[i]); n       if (match == (int)(y.size())) { n         cnt++; n         match = longestPrefix[match - 1]; n       } n     } n     return cnt; n   }; n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int idx; n       char ch; n       cin >> idx >> ch; n       idx--; n       s[idx] = ch; n       SA[idx / sqrtQ] = suffix_automaton(s.substr(idx / sqrtQ * sqrtQ, sqrtQ)); n     } else { n       int l, r; n       cin >> l >> r >> y; n       l--, r--; n       failure_function(); n       if ((int)(y.size()) >= sqrtQ || r - l <= 2 * sqrtQ) { n         cout << solve(l, r) <<   n ; n         continue; n       } n       int ans = 0, len = (int)(y.size()); n       int st = l / sqrtQ, ed = r / sqrtQ; n       for (int i = st + 1; i <= ed; i++) n         ans += solve(max(l, i * sqrtQ - len + 1), min(r, i * sqrtQ + len - 2)); n       for (int i = st + 1; i < ed; i++) ans += SA[i].count(y); n       ans += solve(l, (st + 1) * sqrtQ - 1); n       ans += solve(ed * sqrtQ, r); n       cout << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(int64_t &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const int64_t &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   putchar(   ); n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n int MOD = 1e9 + 7; n void ADD(long long &x, long long v) { n   x = (x + v) % MOD; n   if (x < 0) x += MOD; n } n const int SIZE = 1 << 20; n string s[201]; n int len[201]; n int n, m; n int a[202], b[202]; n bitset<4096> d[201][13]; n void solve() { n   for (int i = (1); i <= (n); ++i) { n     len[i] = ((int)(s[i]).size()); n     for (int l = (1); l <= (12); ++l) { n       for (int k = 0; k + l <= len[i]; k++) { n         int me = 0; n         for (int r = 0; r < (l); ++r) { n           me = me * 2 + (s[i][k + r] -  0 ); n         } n         d[i][l][me] = 1; n       } n     } n   } n   for (int i = (n + 1); i <= (n + m); ++i) { n     for (int l = (1); l <= (12); ++l) { n       d[i][l] = d[a[i]][l] | d[b[i]][l]; n     } n     string tmp = s[a[i]].substr(max(0, ((int)(s[a[i]]).size()) - 12)) + n                  s[b[i]].substr(0, min(12, ((int)(s[b[i]]).size()))); n     for (int l = (1); l <= (12); ++l) { n       for (int k = 0; k + l <= ((int)(tmp).size()); k++) { n         int me = 0; n         for (int r = 0; r < (l); ++r) { n           me = me * 2 + (tmp[k + r] -  0 ); n         } n         d[i][l][me] = 1; n       } n     } n     s[i] = s[a[i]] + s[b[i]]; n     if (((int)(s[i]).size()) > 24) { n       s[i] = s[i].substr(0, 12) + s[i].substr(((int)(s[i]).size()) - 12); n     } n     int ans = 0; n     while (ans < 12 && d[i][ans + 1].count() == (1 << (ans + 1))) ans++; n     W(ans); n   } n } n void input() { n   R(n); n   for (int i = (1); i <= (n); ++i) { n     R(s[i]); n   } n   R(m); n   for (int i = (1); i <= (m); ++i) { n     R(a[i + n], b[i + n]); n   } n } n int main() { n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int c[1000001]; n char str[1000001]; n int len; n int ct; n long long ans; n int main() { n   int k; n   int i; n   scanf( %d , &k); n   scanf( %s , str); n   len = strlen(str); n   if (k == 0) { n     for (i = 0; i < len; i++) { n       if (str[i] ==  0 ) n         ct++; n       else { n         ans += (long long)ct * (long long)(ct + 1) / 2; n         ct = 0; n       } n     } n     ans += (long long)ct * (long long)(ct + 1) / 2; n   } else if (k > 0) { n     for (i = 0; i < len; i++) { n       if (str[i] ==  1 ) ct++; n       c[ct]++; n     } n     ans = c[k]; n     for (i = k; i <= len; i++) ans += (long long)c[i] * (long long)c[i - k]; n   } n   printf( %I64d n , ans); n   scanf(   ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 100000000; n const long long MAXN = 20100; n int n, a[MAXN], ans; n int main() { n   ans = -INF; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int k = 1; k <= n; k++) { n     if (n % k == 0) { n       for (int j = 1; j <= k; j++) { n         long long sum = 0, cnt = 0; n         for (int i = j; i <= n; i += k) { n           cnt++; n           sum += a[i]; n         } n         if (sum > ans && cnt >= 3) { n           ans = sum; n         } n       } n     } n   } n   cout << ans; n   return 0; n } n