#include <bits/stdc++.h> n int a[52], b[52]; n int main() { n   int x, max, k, n, m, i, j, c1; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   scanf( %d , &m); n   for (i = 0; i < m; i++) { n     scanf( %d , &b[i]); n   } n   max = 0; n   for (i = 0; i < m; i++) { n     for (j = 0; j < n; j++) { n       if ((b[i] % a[j]) == 0) { n         k = (b[i] / a[j]); n         if (k >= max) { n           max = k; n         } n       } n     } n   } n   c1 = 0; n   x = 0; n   for (i = 0; i < m; i++) { n     for (j = 0; j < n; j++) { n       x = (b[i] / a[j]); n       if (x == max && (b[i] % a[j] == 0)) c1++; n     } n   } n   printf( %d , c1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[2001]; n int f[2000], ok, n, px, ok2; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].first; n     a[i].second = i; n     f[a[i].first]++; n     if (f[a[i].first] == 2) ok++; n     if (f[a[i].first] == 3) ok2++; n   } n   if (ok < 2 && ok2 == 0) n     cout <<  NO ; n   else { n     cout <<  YES n ; n     sort(a + 1, a + 1 + n); n     for (int i = 1; i <= n; i++) { n       cout << a[i].second <<    ; n       if (f[a[i].first] >= 2) { n         px = i; n       } n     } n     cout <<   n ; n     swap(a[px], a[px - 1]); n     for (int i = 1; i <= n; i++) { n       cout << a[i].second <<    ; n     } n     for (int i = 1; i <= n; i++) { n       if (f[a[i].first] >= 2) { n         px = i; n         break; n       } n     } n     cout <<   n ; n     swap(a[px], a[px + 1]); n     for (int i = 1; i <= n; i++) { n       cout << a[i].second <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 500; n const int N = 220000; n const int M1 = 1e9 + 7; n const int M2 = 1e9 + 9; n int chd[N][26], go[N][26]; n int len[N], q[N], p[N]; n int s[N], dp[N], f[17][N], t[N]; n char str[N], _str[N]; n int st[N], en[N], wh[N], fr[N], to[N], cur[N], state[N], id[N], ans[N], cnt[N], n     sum[N], chk[N], a[N], b[N]; n int h1[N], h2[N], hh1[N], hh2[N], hs1[N], hs2[N], p1[N], p2[N]; n vector<int> adj[N], con[N]; n int sn, e[N]; n char w[N], ch[N]; n void dfs(int u, int p) { n   s[u] = sn; n   f[0][u] = p; n   dp[u] = dp[p] + 1; n   for (int i = 1; i < 17; i++) f[i][u] = f[i - 1][f[i - 1][u]]; n   for (int tt : adj[u]) { n     int v = e[tt] ^ u; n     if (v == p) continue; n     ch[v] = w[tt]; n     a[sn] = u, b[sn] = v; n     t[sn++] = tt; n     dfs(v, u); n     a[sn] = v, b[sn] = u; n     t[sn++] = tt; n   } n } n int T; n void dfs(int u) { n   st[u] = ++T; n   for (int v : con[u]) { n     dfs(v); n   } n   en[u] = T; n } n void modify(int x, int y) { n   cnt[x] += y; n   sum[x / L] += y; n } n int query(int l, int r) { n   int ll = l / L, rr = r / L; n   int ret = 0; n   if (ll == rr) { n     for (int i = l; i <= r; i++) ret += cnt[i]; n     return ret; n   } n   for (int i = l; i < min((ll + 1) * L, r + 1); i++) ret += cnt[i]; n   for (int i = max(l, rr * L); i <= r; i++) ret += cnt[i]; n   for (int i = ll + 1; i < rr; i++) ret += sum[i]; n   return ret; n } n int lca(int u, int v) { n   if (dp[u] < dp[v]) swap(u, v); n   for (int d = dp[u] - dp[v], i = 0; i < 17; i++) n     if (d >> i & 1) u = f[i][u]; n   if (u == v) return u; n   for (int i = 16; i >= 0; i--) { n     if (f[i][u] == f[i][v]) continue; n     u = f[i][u], v = f[i][v]; n   } n   return f[0][u]; n } n int n, m, Q; n const int B1 = 31; n const int B2 = 47; n pair<int, int> get(int *h1, int *h2, int l, int s, int e) { n   return make_pair((h1[e + 1] + 1LL * (M1 - h1[s]) * p1[e - s + 1]) % M1, n                    (h2[e + 1] + 1LL * (M2 - h2[s]) * p2[e - s + 1]) % M2); n } n int make_path(int u, int v) { n   int p = lca(u, v); n   int ret = 0; n   while (u != p) { n     _str[ret++] = ch[u]; n     u = f[0][u]; n   } n   int nret = ret; n   while (v != p) { n     _str[ret++] = ch[v]; n     v = f[0][v]; n   } n   reverse(_str + nret, _str + ret); n   for (int i = 0; i < ret; i++) { n     hh1[i + 1] = (1LL * hh1[i] * B1 + _str[i] -  a ) % M1; n     hh2[i + 1] = (1LL * hh2[i] * B2 + _str[i] -  a ) % M2; n   } n   return ret; n } n int dist(int u, int v) { return dp[u] + dp[v] - 2 * dp[lca(u, v)]; } n int main() { n   scanf( %d%d%d , &n, &m, &Q); n   for (int i = 1; i < n; i++) { n     int u, v; n     char c[3]; n     scanf( %d%d%s , &u, &v, c); n     e[i] = u ^ v; n     w[i] = c[0]; n     adj[u].push_back(i); n     adj[v].push_back(i); n   } n   dfs(1, 0); n   int nodes = 1; n   for (int i = 0; i < N; i++) n     p1[i] = (i == 0 ? 1 : 1LL * p1[i - 1] * B1 % M1), n     p2[i] = (i == 0 ? 1 : 1LL * p2[i - 1] * B2 % M2); n   for (int i = 1; i <= m; i++) { n     scanf( %s , str); n     int l = strlen(str); n     for (int k = 0; k < 2; k++) { n       int u = 1, ret1 = 0, ret2 = 0; n       for (int j = 0; str[j]; j++) { n         int c = str[j] -  a ; n         ret1 = (1LL * ret1 * B1 + c) % M1, ret2 = (1LL * ret2 * B2 + c) % M2; n         int &v = chd[u][c]; n         if (!v) v = ++nodes; n         u = v; n       } n       hs1[k * m + i] = ret1; n       hs2[k * m + i] = ret2; n       state[k * m + i] = u; n       reverse(str, str + l); n     } n     len[i] = len[i + m] = l; n   } n   int qn = 0; n   for (int i = 0, u; i < 26; i++) n     if (u = chd[1][i]) q[qn++] = u, p[u] = 1; n   for (int i = 0; i < qn; i++) { n     int u = q[i]; n     for (int j = 0, v; j < 26; j++) n       if (v = chd[u][j]) { n         int w = p[u]; n         while (w > 1 && !chd[w][j]) w = p[w]; n         w = max(1, chd[w][j]); n         p[v] = w; n         q[qn++] = v; n       } n   } n   for (int i = 0; i < 26; i++) go[1][i] = max(1, chd[1][i]); n   for (int i = 0; i < qn; i++) { n     int u = q[i]; n     for (int j = 0; j < 26; j++) { n       if (chd[u][j]) n         go[u][j] = chd[u][j]; n       else n         go[u][j] = go[p[u]][j]; n     } n   } n   for (int i = 2; i <= nodes; i++) con[p[i]].push_back(i); n   dfs(1); n   for (int i = 1; i <= Q; i++) { n     scanf( %d%d%d , &fr[i], &to[i], &wh[i]); n     if (s[fr[i]] > s[to[i]]) { n       swap(fr[i], to[i]); n       wh[i] += m; n     } n     fr[i] = s[fr[i]]; n     to[i] = s[to[i]] - 1; n     id[i] = i; n   } n   sort(id + 1, id + Q + 1, [](int i, int j) { n     if (fr[i] / L != fr[j] / L) return fr[i] / L < fr[j] / L; n     return to[i] < to[j]; n   }); n   for (int i = 1, j; i <= Q; i = j) { n     int u = id[i]; n     int r = fr[u] / L * L - 1; n     memset(chk, 0, sizeof chk); n     memset(cnt, 0, sizeof cnt); n     memset(sum, 0, sizeof sum); n     int rn = 0; n     cur[0] = 1; n     for (j = i; j <= Q && fr[id[i]] / L == fr[id[j]] / L; j++) { n       int v = id[j]; n       while (r < to[v]) { n         ++r; n         int eid = t[r]; n         int c = w[eid] -  a ; n         if (chk[eid]) { n           modify(st[cur[rn]], -1); n           rn--; n         } else { n           cur[rn + 1] = go[cur[rn]][c]; n           h1[rn + 1] = (1LL * h1[rn] * B1 + c) % M1; n           h2[rn + 1] = (1LL * h2[rn] * B2 + c) % M2; n           modify(st[cur[++rn]], 1); n         } n         chk[eid] ^= 1; n       } n       int who = wh[v]; n       ans[v] = query(st[state[who]], en[state[who]]); n       int xx = a[fr[u] / L * L]; n       int yy = a[fr[v]]; n       int zz = b[r]; n       int pxy = lca(xx, yy); n       int pyz = lca(yy, zz); n       int pzx = lca(zz, xx); n       int pp = pxy ^ pyz ^ pzx; n       int xy = dp[xx] + dp[yy] - 2 * dp[pxy]; n       int yz = dp[zz] + dp[yy] - 2 * dp[pyz]; n       int zx = dp[xx] + dp[zz] - 2 * dp[pzx]; n       int xlen = (xy + zx - yz) / 2; n       assert(xlen == dist(xx, pp)); n       assert(rn == zx); n       int _ans = ans[v]; n       for (int k = 0; k < xlen; k++) { n         if (k + len[who] - 1 >= rn) break; n         auto ret = get(h1, h2, rn, k, k + len[who] - 1); n         if (ret.first == hs1[who] && ret.second == hs2[who]) ans[v]--; n       } n       int __ans = ans[v]; n       int ylen = make_path(yy, pp); n       for (int k = 0; k < ylen; k++) { n         if (k + len[who] - 1 < ylen) { n           auto ret = get(hh1, hh2, ylen, k, k + len[who] - 1); n           if (ret.first == hs1[who] && ret.second == hs2[who]) ans[v]++; n           continue; n         } n         if (len[who] - ylen + k > rn - xlen) break; n         auto first = get(hh1, hh2, ylen, k, ylen - 1); n         auto second = get(h1, h2, rn, xlen, xlen + len[who] - (ylen - k) - 1); n         int ret1 = n             (1LL * first.first * p1[len[who] - (ylen - k)] + second.first) % M1; n         int ret2 = n             (1LL * first.second * p2[len[who] - (ylen - k)] + second.second) % n             M2; n         if (ret1 == hs1[who] && ret2 == hs2[who]) ans[v]++; n       } n     } n   } n   for (int i = 1; i <= Q; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream f( dima1.in ); n ofstream g( dima1.out ); n int n, a[200005], b[200005], b2[200005], k, ap[200005], ap2[200005], ok, m, p, n     spos[200005], sol = 0; n vector<int> r[200005]; n vector<int> ps[200005]; n int Cbin(int x) { n   int i, j, st = 1, dr = k, mid; n   while (st < dr) { n     mid = (st + dr) / 2; n     if (x < b2[mid]) n       dr = mid - 1; n     else if (x > b2[mid]) n       st = mid + 1; n     else if (x == b2[mid]) n       return mid; n   } n   mid = (st + dr) / 2; n   if (b2[mid] == x) n     return mid; n   else n     return 0; n } n int main() { n   int i, j, pos, ok; n   cin >> n >> m >> p; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= m; i++) cin >> b[i]; n   sort(b + 1, b + m + 1); n   b2[1] = b[1]; n   k = 1; n   ap[1] = 1; n   for (i = 2; i <= m; i++) { n     if (b[i] == b2[k]) n       ap[k]++; n     else { n       k++; n       b2[k] = b[i]; n       ap[k] = 1; n     } n   } n   for (i = 1; i <= n; i++) { n     pos = Cbin(a[i]); n     a[i] = pos; n   } n   for (i = 1; i <= n; i++) { n     if (r[i % p].size() == 0) { n       r[i % p].push_back(0); n       ps[i % p].push_back(0); n     } n     r[i % p].push_back(a[i]); n     ps[i % p].push_back(i); n   } n   for (i = 0; i < p; i++) n     if ((r[i].size() - 1) >= m) { n       ok = 0; n       for (j = 1; j < r[i].size(); j++) { n         if (r[i][j] != 0) { n           if (ap2[r[i][j]] == ap[r[i][j]]) n             ok--; n           else if (ap2[r[i][j]] + 1 == ap[r[i][j]]) n             ok++; n           ap2[r[i][j]]++; n         } n         if (j > m) { n           if (r[i][j - m] != 0) { n             if (ap2[r[i][j - m]] == ap[r[i][j - m]]) n               ok--; n             else if (ap2[r[i][j - m]] - 1 == ap[r[i][j - m]]) n               ok++; n             ap2[r[i][j - m]]--; n           } n         } n         if (j >= m && ok == k) { n           sol++; n           spos[sol] = ps[i][j - m + 1]; n         } n       } n       for (j = r[i].size() - 1; j >= 1; j--) ap2[r[i][j]] = 0; n     } n   sort(spos + 1, spos + sol + 1); n   cout << sol <<   n ; n   for (i = 1; i <= sol; i++) cout << spos[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using db = long double; n using str = string; n using pi = pair<int, int>; n template <class T> n using V = vector<T>; n template <class T, size_t SZ> n using AR = array<T, SZ>; n using vi = V<int>; n using vb = V<bool>; n using vpi = V<pi>; n const int MOD = 1e9 + 7; n const db EPS = 1e-9; n const ll BIG = 1e18; n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n struct mi { n   int v; n   explicit operator int() const { return v; } n   mi() : v(0) {} n   mi(ll _v) : v(int(_v % MOD)) { v += (v < 0) * MOD; } n }; n mi& operator+=(mi& a, mi b) { n   if ((a.v += b.v) >= MOD) a.v -= MOD; n   return a; n } n mi& operator-=(mi& a, mi b) { n   if ((a.v -= b.v) < 0) a.v += MOD; n   return a; n } n mi operator+(mi a, mi b) { return a += b; } n mi operator-(mi a, mi b) { return a -= b; } n mi operator*(mi a, mi b) { return mi((ll)a.v * b.v); } n mi& operator*=(mi& a, mi b) { return a = a * b; } n mi pow(mi a, ll p) { n   assert(p >= 0); n   return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1); n } n mi inv(mi a) { n   assert(a.v != 0); n   return pow(a, MOD - 2); n } n mi operator/(mi a, mi b) { return a * inv(b); } n bool operator==(mi a, mi b) { return a.v == b.v; } n vector<mi> invs, fac, ifac; n void genFac(int SZ) { n   invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); n   invs[1] = fac[0] = ifac[0] = 1; n   for (int i = (2); i < (SZ); ++i) n     invs[i] = mi(-(ll)MOD / i * (int)invs[MOD % i]); n   for (int i = (1); i < (SZ); ++i) n     fac[i] = fac[i - 1] * i, ifac[i] = ifac[i - 1] * invs[i]; n } n mi comb(int a, int b) { n   if (a < b || b < 0) return 0; n   return fac[a] * ifac[b] * ifac[a - b]; n } n const int mx = 100005; n struct Eff { n   int n, k; n   mi first[mx]; n   void solve() { n     genFac(100005); n     cin >> n >> k; n     map<int, int> cnt; n     int negs = 0; n     for (int i = 1; i <= n; i++) { n       int A; n       cin >> A; n       if (A == -1) { n         negs++; n       } else { n         cnt[A]++; n       } n     } n     map<int, mi> all_cnts; n     for (auto u : cnt) { n       all_cnts[u.second] = mi(0); n     } n     all_cnts[0] = mi(0); n     first[0] = mi(0); n     for (int i = 0; i <= n - 1; i++) { n       first[i + 1] = mi(-n); n       if (i - 1 >= 0) { n         first[i + 1] -= mi(i) * mi(k - 1) * first[i - 1]; n       } n       first[i + 1] += first[i] * (mi(n) - 2 * mi(i) + mi(i) * mi(k)); n       first[i + 1] = first[i + 1] / mi(n - i); n     } n     vector<mi> k1pows(n + 1, mi(1)); n     for (int i = 1; i <= n; i++) { n       k1pows[i] = k1pows[i - 1] * mi(k - 1); n     } n     for (auto& u : all_cnts) { n       for (int i = 0; i <= negs; i++) { n         u.second += first[u.first + i] * mi(comb(negs, i)) * k1pows[negs - i]; n       } n       u.second = u.second / pow(mi(k), negs); n     } n     mi ans = mi(0); n     map<int, int> cnt_cnt; n     for (auto u : cnt) { n       cnt_cnt[u.second]++; n     } n     cnt_cnt[0] += (k - int((cnt).size())); n     for (auto u : cnt_cnt) { n       ans += all_cnts[u.first] * mi(u.second); n     } n     ans -= (first[n] + mi(k - 1) * first[0]); n     cout << ans.v <<   n ; n   } n }; n int main() { n   cin.tie(0)->sync_with_stdio(0); n   Eff e; n   e.solve(); n } n


#include<bits/stdc++.h> n #define fre(x) freopen(#x .in , r ,stdin);freopen(#x .out , w ,stdout) n #define ll long long n using namespace std; n inline int read(){ n  int x=0,f=1;char c=getchar(); n  while(c< 0 ||c> 9 ) { if(c== - ) f=-1;c=getchar(); } n  while(c>= 0 &&c<= 9 ) x=(x<<3)+(x<<1)+(c^ 0 ),c=getchar(); n  return x*f; n } n const int N=1e5+5; n int n,l[N],r[N]; n ll f[N][2]; n vector<int>e[N]; n void dfs(int x,int fa){ n  f[x][0]=f[x][1]=0; n  for(auto y:e[x]) if(y^fa){ n   dfs(y,x); n   f[x][0]+=max(f[y][0]+abs(l[x]-l[y]),f[y][1]+abs(l[x]-r[y])); n   f[x][1]+=max(f[y][0]+abs(r[x]-l[y]),f[y][1]+abs(r[x]-r[y])); n  } n } n inline void solve(){ n  n=read(); n  for(int i=1;i<=n;i++) l[i]=read(),r[i]=read(),e[i].clear(); n  for(int i=1;i<n;i++){ n   int x=read(),y=read(); n   e[x].push_back(y); n   e[y].push_back(x); n  } n  dfs(1,0); n  printf( %lld n ,max(f[1][0],f[1][1])); n } n int main(){ n  int T=read(); n  while(T--) solve(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, k; n int d[100015]; n char baca[100015]; n int main() { n   scanf( %d %d , &n, &k); n   scanf( %s , baca); n   for (int i = (int)1; i <= (int)n; ++i) d[i] = (int)(baca[i - 1] -  0 ); n   d[0] = d[n + 1] = 0; n   for (int i = (int)1; i <= (int)n; ++i) { n     if (!k) break; n     if (d[i] == 4 && d[i + 1] == 7) { n       if (i % 2 == 1) { n         if (d[i + 2] == 7) { n           if (k % 2 == 0) n             break; n           else { n             d[i + 1] = 4; n             break; n           } n         } else n           d[i + 1] = 4; n       } else { n         if (d[i - 1] == 4) { n           if (k % 2 == 0) n             break; n           else { n             d[i] = 7; n             break; n           } n         } else n           d[i] = 7; n       } n       k--; n     } n   } n   for (int i = (int)1; i <= (int)n; ++i) printf( %d , d[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n int v[N], c[N]; n int n, k; n vector<int> z; n void input() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &v[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n   z.push_back(min(100 * v[n], c[n])); n   for (int i = n - 1; i >= 1; i--) { n     int larger = max(z.back(), v[i] * 100); n     int smaller = min(larger, c[i]); n     z.push_back(min(larger, smaller)); n   } n } n void pro() { n   sort(z.begin(), z.end()); n   double ans = 0; n   double chance = 1; n   for (int i = 0; i < k; i++) { n     ans += z[i] * chance * k / (n - i); n     chance *= 1. * (n - k - i) / (n - i); n   } n   cout << fixed << setprecision(10) << ans << endl; n } n int main() { n   input(); n   pro(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<int> v(n, 0); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   m--; n   int p = -1; n   for (int i = m + 1; i < n; i++) { n     if (v[i] != 0 && v[i] <= k) { n       if (p == -1 || abs(m - i) < abs(m - p)) { n         p = i; n       } n     } n   } n   for (int i = m - 1; i >= 0; i--) { n     if (v[i] != 0 && v[i] <= k) { n       if (p == -1 || abs(m - i) < abs(m - p)) { n         p = i; n       } n     } n   } n   cout << abs(m - p) * 10 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a; n int gcd(int a, int b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n void solve() { n   int temp, sum = 0, m, i; n   for (i = 2; i < a; i++) { n     temp = a; n     while (temp) { n       sum += temp % i; n       temp = temp / i; n     } n   } n   m = gcd(sum, a - 2); n   printf( %d/%d n , sum / m, (a - 2) / m); n } n int main() { n   while (scanf( %d , &a) == 1) { n     solve(); n   } n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long res1, res2, f[5][5]; n string s; n int main() { n   std::ios::sync_with_stdio(false); n   cin >> s; n   n = s.size(); n   for (int i = 0; i < (n); i++) { n     f[s[i] -  a ][i % 2]++; n     res1 += f[s[i] -  a ][1 - (i % 2)]; n     res2 += f[s[i] -  a ][i % 2]; n   } n   cout << res1 <<     << res2 << endl; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 3e5 + 5; n const long long MOD = 1e9 + 7; n const int BASE = 100000000; n const int INF = 1e9 + 8; n long long g[mxn], cnt[mxn], a[mxn]; n void up_g(int x, const long long &val) { n   while (x < mxn) { n     g[x] += val; n     x += x & (-x); n   } n } n void update_g(int l, int r, const long long &val) { n   up_g(l, val); n   up_g(r + 1, -val); n } n long long get_g(int x) { n   long long res = 0; n   while (x) { n     res += g[x]; n     x -= x & (-x); n   } n   return res; n } n void update_cnt(int x) { n   while (x < mxn) { n     cnt[x] += 1LL; n     x += x & (-x); n   } n } n long long get(int x) { n   long long res = 0; n   while (x) { n     res += cnt[x]; n     x -= x & (-x); n   } n   return res; n } n long long get_cnt(int l, int r) { return (get(r) - get(l - 1)); } n signed main() { n   int n; n   cin >> n; n   for (int i = (1); i <= (n); ++i) cin >> a[i]; n   long long cur = 0, sum = 0; n   for (int i = (1); i <= (n); ++i) { n     cur += (long long)(i - 1) * a[i] - get_g(a[i]); n     cur += sum; n     int x = (int)(a[i]); n     while (x < mxn) { n       long long k = min(x + (int)(a[i]) - 1, (int)(mxn - 5)); n       cur -= (long long)(x)*get_cnt(x, k); n       update_g(x, k, x); n       x += a[i]; n     } n     update_cnt(a[i]); n     sum += a[i]; n     cout << cur <<    ; n   } n } n


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


#include <cassert> n #include <cstdio> n #include <cstdlib> n #include <cstring> n #include <algorithm> n #include <iostream> n #include <set> n #include <map> n #include <vector> n #include <string> n  n #include <cmath> n #include <queue> n #include <stack> n  n #include <unordered_set> n #include <unordered_map> n #include <array> n  n using namespace std; n  n #define LL long long n  n inline int read() { n     int x = 0, f = 1; n     char c = getchar(); n     while (c <  0  || c >  9 ) { n         if (c ==  - ) { n             f = -1; n         } n         c = getchar(); n     } n     while (c >=  0  && c <=  9 ) { n         x = x * 10 + c -  0 ; n         c = getchar(); n     } n     return x * f; n } n  n inline void write(int x) { n     static int sta[35]; n     int top = 0; n     do { n         sta[top++] = x % 10, x /= 10; n     } while (x); n     while (top) putchar(sta[--top] + 48);  n } n  n inline void writeln(int x) { n  if (x < 0) { n   putchar( - ); x = -x; n  } n     static int sta[35]; n     int top = 0; n     do { n         sta[top++] = x % 10, x /= 10; n     } while (x); n     while (top) putchar(sta[--top] + 48);  n  putchar(  n ); n } n  n inline LL readll() { n     LL x = 0, f = 1; n     char c = getchar(); n     while (c <  0  || c >  9 ) { n         if (c ==  - ) { n             f = -1; n         } n         c = getchar(); n     } n     while (c >=  0  && c <=  9 ) { n         x = x * 10 + c -  0 ; n         c = getchar(); n     } n     return x * f; n } n  n inline void writell(LL x) { n  if (x < 0) { n   putchar( - ); x = -x; n  } n     static LL sta[35]; n     int top = 0; n     do { n         sta[top++] = x % 10, x /= 10; n     } while (x); n     while (top) putchar(sta[--top] + 48);  n } n  n inline void writellln(LL x) { n  if (x < 0) { n   putchar( - ); x = -x; n  } n     static LL sta[35]; n     int top = 0; n     do { n         sta[top++] = x % 10, x /= 10; n     } while (x); n     while (top) putchar(sta[--top] + 48);  n  putchar(  n ); n } n  n const int hashMod = 999983; n  n template<class TYPENAME> n class HashNode { n public: n     TYPENAME val; n     int key, next; n     HashNode(TYPENAME _val, int _key, int _next): val(_val), key(_key), next(_next) {} n     bool isNull() { n         return key == -1; n     } n }; n  n template<class TYPENAME> n class Hash { n public: n     vector<HashNode<TYPENAME> > nodes; n     vector<int> d; n     int nodeN; n     Hash() { n         nodeN = 0;  n         d = vector<int>(hashMod, -1); n     } n     void push(int key, TYPENAME val) { n         nodes.push_back(HashNode<TYPENAME>(val, key, d[key])); n         d[key] = nodeN++; n     } n     HashNode<TYPENAME> find(int key, TYPENAME val) { n         for (int p = d[key]; p != -1; p = nodes[p].next) { n             if (nodes[p].val == val) { n                 return nodes[p]; n             } n         } n         return HashNode<TYPENAME>(val, -1, -1); n     } n }; n  n int n, c, q; n vector<int> ii; n vector<LL> jj; n vector<int> p; n vector<vector<LL> > ways, wayC; n  n LL permContained(int l, int r, int cc) { n     if (l == -1) { n         return 1; // result for {-1, -1} n     } n     // otherwise, we can rotate (r, n) n     // we have n - r - 1 to rotate, and cc - (r - l) coins n     return ways[n - r - 1][cc - (r - l)]; n } n  n // we can discretize all the queries, so we have an index for each query n // for each layer, we first save all the queries into boxes n // and take them out n  n bool pSmaller(const pair<int, int> &a, const pair<int, int> &b) { n     return p[a.second] < p[b.second]; n } n bool pGreater(const pair<int, int> &a, const pair<int, int> &b) { n     return p[a.second] > p[b.second]; n } n  n class Query { n public: n     int ii, idx; n     LL jj; n     Query(int _idx, int _ii, LL _jj): idx(_idx), ii(_ii), jj(_jj) {} n     void print() { n         printf( Query(idx = %d, ii = %d, jj = %lld)  , idx, ii, jj); n     } n }; n  n bool cmpQuery(const Query &a, const Query &b) { n     return a.jj < b.jj; n } n void solve() { n     n = read(); c = read(); q = read(); n     // printf( task: n = %d. c = %d, q = %d n , n, c, q); n     p = vector<int>(n); n     for (int i = 0; i < n; ++i) { n         p[i] = read(); n     } n     ii = vector<int>(q); n     jj = vector<LL>(q); n     for (int i = 0; i < q; ++i) { n         ii[i] = read() - 1; jj[i] = readll() - 1; n         // cout << ii[i] <<     << jj[i] << endl; n     } n  n     // for (int i = 0; i < q; ++i) { n     //     // ii[i] = read() - 1; jj[i] = readll() - 1; n     //     cout << ii[i] <<     << jj[i] << endl; n     // } n  n     wayC = vector<vector<LL> >(n); n     for (int i = 0; i < n; ++i) { n         wayC[i] = vector<LL>(c + 1, 0); n         wayC[i][0] = 1; n     } n  n     // wayC[n][c] = C(n, c) n     for (int i = 0; i < n; ++i) { n         for (int j = 1; j <= c; ++j) { n             // consider C(i, j) n             if (i < j) { n                 wayC[i][j] = 0; n             } else { n                 wayC[i][j] = wayC[i - 1][j] + wayC[i - 1][j - 1]; n             } n         } n     } n  n     // cout <<  finish wayC  << endl; n     // for (int i = 0; i < q; ++i) { n     //     // ii[i] = read() - 1; jj[i] = readll() - 1; n     //     cout << ii[i] <<     << jj[i] << endl; n     // } n      n     ways = vector<vector<LL> >(n + 1); n     for (int i = 0; i <= n; ++i) { n         ways[i] = vector<LL>(c + 1, 1); n     } n     // ways[n][c] = C(n - 1, 0) + C(n - 1, 1) + ... + C(n - 1, c) n     // ways[n][c] = ways[n][c - 1] + wayC[n - 1][c] (n > 0) n     for (int i = 1; i <= n; ++i) { n         ways[i][0] = wayC[i - 1][0]; n         for (int j = 1; j <= c; ++j) { n             ways[i][j] = ways[i][j - 1] + wayC[i - 1][j]; n             // printf( ways(len = %d, c = %d) = %lld n , i, j, ways[i][j]); n         } n     } n  n     // cout <<  finish ways  << endl; n  n     // for (int i = 0; i < q; ++i) { n     //     // ii[i] = read() - 1; jj[i] = readll() - 1; n     //     cout << ii[i] <<     << jj[i] << endl; n     // } n  n     // now ways[len][c] = ways(n, c) n  n     // we should build a deque for each 0 .. c, contains n     // the [l, r] as the leftmost rotation(not over c) n     // and sorted by the lex order. Our queries are all on layer c(we can use at most c coins) n     // we sort all the jj, and scan the deque, every time we find an [l, r] so that jj is behind this n     // we know that [0 .. r] has been decided for this jj query. if ii <= r, we can return the query result n     // otherwise we want to decide the remaining part jj - ord([l, r]) = jj , with coins c - r + l,  n     // and start from r + 1 n     // for each c, we can decide p[l]  permutations before using [l, r] started at l  for each l in [0, n) n     // so the query becomes another query (c - r + l, jj  + p[r + 1], ii) on layer c - r + l n     // finally we have all queryies on layer 0, so there is only one state for queries n     // queries down to here must asks something hard to decide, and here we must have jj = 1 n     // then we just return i-th n  n     // first to make deques for each c  = [0 .. c] n  n     vector<deque<pair<int, int> > > dq(c + 1); n     for (int cc = 0; cc <= c; ++cc) { n         // dq[cc].push_back({-1, -1}); // no change n         dq[cc].push_back({n - 1, n - 1}); n         for (int i = n - 1; i >= 0; --i) { n             vector<pair<int, int> > lft, rgt; n             for (int j = i + 1; j <= i + cc && j < n; ++j) { n                 // we have rotations [i, j] n                 // current deque has all permutations with i-th = p[i] n                 // so [i, j] is with i-th = p[j] n                 // we should divide it into two parts: p[j] < p[i] && p[j] > p[i] n                 // for p[j] < p[i], we insert it at front, from the order p[j] large to small n                 // for p[j] > p[i], we insert it at back, from small to large n                 // printf( p[j = %d] = %d, p[i = %d] = %d n , j, p[j], i, p[i]); n                 if (p[j] < p[i]) { n                     lft.push_back({i, j}); n                 } else { n                     rgt.push_back({i, j}); n                 } n             } n             sort(lft.begin(), lft.end(), pGreater); n             sort(rgt.begin(), rgt.end(), pSmaller); n             for (int i = 0; i < lft.size(); ++i) { n                 dq[cc].push_front(lft[i]); n             } n             for (int i = 0; i < rgt.size(); ++i) { n                 dq[cc].push_back(rgt[i]); n             } n         } n  n         // cout <<  c =   << cc << endl; n         // for (auto pr: dq[cc]) { n         //     printf( (%d, %d)  , pr.first, pr.second); n         // } n         // putchar(  n ); n     } n  n     // cout <<  finish creating deques  << endl; n  n     // int mc = 0; n     // vector<int> queryIdx = discretize(jj, mc); n     // vector<LL> ts(mc + 1, 0); n     // for (int i = 0; i < q; ++i) { n     //     ts[queryIdx[i]] = jj[i]; n     // } n     // vector<vector<vector<Query> > > ids(c + 1); n     // for (int i = 0; i <= c; ++i) { n     //     ids[i] = vector<vector<Query> >(mc + 1); n     // } n     // for (int i = 0; i < q; ++i) { n     //     ids[c][queryIdx[i]].push_back(Query(i, ii[i], queryIdx[i], jj[i])); n     // } n  n     vector<int> ans(q, -1); n     // for (int i = 0; i < q; ++i) { n     //     ans[i] = -1; n     // } n     // for (int i = 0; i < q; ++i) { n     //     cout << ans[i] <<    ; n     // } n     // cout << endl; n  n     vector<vector<Query> > que(c + 1); n     for (int i = 0; i < q; ++i) { n         que[c].push_back(Query(i, ii[i], jj[i])); n         // que[c].back().print(); n     } n  n     vector<vector<LL> > pp(c + 1); n     // pp[cc][l]: the permutations before the first [l, r] started at l(l in [0, n - 1)) n     for (int cc = 0; cc <= c; ++cc) { n         pp[cc] = vector<LL>(n, -1); n         LL perms = 0; n         int ll, rr; n         for (pair<int, int> pr: dq[cc]) { n             ll = pr.first; rr = pr.second; n             if (ll == -1) { n                 continue; n             } n             if (pp[cc][ll] == -1) { n                 pp[cc][ll] = perms; n                 // printf( pp[c = %d][l = %d] = %lld, from (l = %d, r = %d) n , cc, ll, pp[cc][ll], ll, rr); n             } n             perms += permContained(ll, rr, cc); n         } n         // pp[cc][n - 1] = perms - 1; n         for (int i = n - 2; i >= 0; --i) { n             pp[cc][i] = min(pp[cc][i], pp[cc][i + 1]); n         } n         for (int i = 0; i < n; ++i) { n             // printf( pp[c = %d][l = %d] = %lld n , cc, i, pp[cc][i]); n         } n         // how to deal with -1, -1? it means no rotation at all n         // and contains exactly 1 permutation p0 ... p_{n - 1} n     } n  n     // cout <<  finish calculating pp s  << endl; n  n     for (int cc = c; cc >= 0; --cc) { n         // work on layer cc n         if (que[cc].size() == 0) { n             continue; n         } n         sort(que[cc].begin(), que[cc].end(), cmpQuery); n         // now we have queries saved in que[cc] n         // and sorted by order of jj n  n         // for [l, r] on layer cc, the undecided part is (r, n), len = n - r - 1 n         // so it contains ways[n - r - 1][cc - (r - l)] n         // we go from the beginning of dq[cc], and use a pointer in que[cc] for current  n  n         // when we want to insert a new query c , jj , ii, we just do que[c ].push_back(Query(idx, ii, jj )) n         // TODO: !!!!! n         int qcur = 0; n         LL currPerm = 0; n         // cout <<  coin   << cc <<  :  ; n         // for (int i = 0; i < que[cc].size(); ++i) { n         //     // printf( Query(idx = %d, ii = %d, jj = %lld)  , que[cc][i].idx, que[cc][i].ii, que[cc][i].jj); n         //     que[cc][i].print(); n         // } n         // cout << endl; n  n         // for (int i = 0; i < q; ++i) { n         //     cout << ans[i] <<    ; n         // } n         // cout << endl; n  n         for (pair<int, int> pr: dq[cc]) { n             int ll = pr.first, rr = pr.second; n             // printf( (%d, %d)  , ll, rr); n             // printf( l = %d, r = %d, cc = %d, currPerm = %lld n , ll, rr, cc, currPerm); n             LL newPerm = permContained(ll, rr, cc); n             // printf( new perm = %lld n , newPerm); n             while (qcur < que[cc].size() && que[cc][qcur].jj < currPerm + newPerm) { n                 int cii = que[cc][qcur].ii; n                 LL cjj = que[cc][qcur].jj; n                 int cidx = que[cc][qcur].idx; n                 // printf( qcur = %d: idx = %d, ii = %d, jj = %lld n , qcur, cidx, cii, cjj); n                 if (ll == -1 || cii < ll) { n                     // cout <<  quit1  << endl; n                     ans[cidx] = p[cii]; n                     ++qcur; continue; n                 } n                 if (cii >= ll && cii <= rr) { n                     // cout <<  quit2  << endl; n                     ans[cidx] = p[rr - (cii - ll)]; n                     ++qcur; continue; n                 } n                 // otherwise, it is not determined n                 // we should insert it into cc - (rr - ll) n                 int nc = cc - (rr - ll); n                 // what is the rank of the permutation in nc? n                 // all [0, r] fixed, so we need to find the (que[cc][qucr].jj - currPerm)-th  n                 // in dq[cc], after pp[cc][r + 1] n                 if (rr >= n - 1) { n                     // cout <<  quit-1  << endl; n                     ans[cidx] = -1; n                     ++qcur; continue; n                 } n                 // otherwise, we should insert it to layer nc n                 que[nc].push_back(Query(cidx, cii, pp[nc][rr + 1] + cjj - currPerm)); n                 ++qcur; n                 // cout <<  quit0  << endl; n             } n             currPerm += newPerm; n         } n     } n  n     for (int i = 0; i < q; ++i) { n         writeln(ans[i]); n     } n } n  n int main() { n  // std::ios::sync_with_stdio(false); n     // std::cin.tie(0); n     int t = read(); n     for (int i = 0; i < t; ++i) { n         solve(); n     } n  return 0; n }


#include <bits/stdc++.h> n int main() { n   int c, input[100010]; n   int i = -1, j, k; n   while ((c = getchar()) !=   n ) { n     if (i < 0 || input[i] != c) n       input[++i] = c; n     else n       i--; n   } n   if (i < 0) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 1 << 10; n int pointer = SIZE; n char buffer[SIZE]; n char Advance() { n   if (pointer == SIZE) { n     fread(buffer, 1, SIZE, stdin); n     pointer = 0; n   } n   return buffer[pointer++]; n } n int Read() { n   int answer = 0; n   char ch = Advance(); n   while (!isdigit(ch)) ch = Advance(); n   while (isdigit(ch)) { n     answer = answer * 10 + ch -  0 ; n     ch = Advance(); n   } n   return answer; n } n const int MAXN = 5000; n const long long MOD = 1000000007; n void Add(int &x, int y) { n   x += y; n   if (x >= MOD) x -= MOD; n } n void Subtract(int &x, int y) { n   x -= y; n   if (x < 0) x += MOD; n } n int factorial[1 + MAXN], inverse[1 + MAXN]; n int RaiseToPower(int base, int power) { n   int answer = 1; n   while (power) { n     if (power % 2) answer = (1LL * answer * base) % MOD; n     base = (1LL * base * base) % MOD; n     power /= 2; n   } n   return answer; n } n int Combinations2(int n) { n   if (n % 2 == 0) n     return (1LL * (n / 2) * (n - 1)) % MOD; n   else n     return (1LL * n * ((n - 1) / 2)) % MOD; n } n int children[1 + MAXN], depth[1 + MAXN]; n int answer[1 + 2 * MAXN], chains[1 + 2 * MAXN]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) scanf( %d , &children[i]); n   depth[1] = 1; n   for (int i = 2; i <= n; i++) n     depth[i] = (1LL * depth[i - 1] * children[i - 1]) % MOD; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       chains[i + j] = (1LL * depth[i] * depth[j] + chains[i + j]) % MOD; n   int multiplier = 1; n   for (int i = 1; i < n; i++) { n     int divide = RaiseToPower(children[i], MOD - 2); n     for (int j = i + 1; j <= n; j++) { n       int add = (1LL * multiplier * depth[j]) % MOD; n       Add(answer[j - i], add); n       Subtract(chains[j - i + 2], (2LL * depth[j] * depth[i]) % MOD); n       depth[j] = (1LL * depth[j] * divide) % MOD; n     } n     divide = (1LL * divide * divide) % MOD; n     for (int j = 2; j <= 2 * (n - i); j++) { n       chains[j] = (1LL * chains[j + 2] * divide) % MOD; n       int add = (1LL * chains[j] * Combinations2(children[i])) % MOD; n       add = (1LL * add * multiplier) % MOD; n       Add(answer[j], add); n     } n     multiplier = (1LL * multiplier * children[i]) % MOD; n   } n   for (int i = 1; i <= 2 * n - 2; i++) printf( %d  , answer[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[200010], b[200010]; n int nxt[200010]; n void make_next() { n   int i = 0, j = -1; n   nxt[0] = -1; n   while (i < m) { n     if (j == -1 || b[i] == b[j]) { n       i++; n       j++; n       if (b[i] == b[j]) n         nxt[i] = nxt[j]; n       else n         nxt[i] = j; n     } else n       j = nxt[j]; n   } n } n int kmp() { n   int i = 0, j = 0; n   int cnt = 0; n   while (i < n) { n     if (a[i] == b[j]) { n       i++; n       j++; n       if (j == m) { n         cnt++; n       } n     } else { n       j = nxt[j]; n       if (j == -1) { n         i++; n         j = 0; n       } n     } n   } n   return cnt; n } n int main() { n   int i, j; n   while (cin >> n >> m) { n     for (i = 0; i < n; i++) cin >> a[i]; n     for (i = 0; i < m; i++) cin >> b[i]; n     if (n < m) { n       cout <<  0 n ; n       continue; n     } else if (m == 1) { n       cout << n << endl; n       continue; n     } n     for (i = 0; i < n - 1; i++) a[i] = a[i + 1] - a[i]; n     for (i = 0; i < m - 1; i++) b[i] = b[i + 1] - b[i]; n     m--; n     n--; n     make_next(); n     cout << kmp() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 1e5 + 5; n long long n, m, s; n vector<pair<long long, long long> > bug; n vector<pair<long long, long long> > stud; n vector<long long> pass; n long long allot[SIZE]; n struct cmp { n   bool operator()(pair<long long, long long> const& lhs, n                   pair<long long, long long> const& rhs) { n     return lhs.first < rhs.first; n   } n }; n long long ok(long long val) { n   long long i = 0; n   long long j = 0; n   long long temp = s; n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq; n   while (i < bug.size()) { n     while (stud[j].first >= bug[i].first && j < stud.size()) { n       pq.push(make_pair(pass[stud[j].second], stud[j].second)); n       j++; n     } n     if (pq.size() == 0) return 0; n     pair<long long, long long> cheap = pq.top(); n     pq.pop(); n     if (temp < cheap.first) return 0; n     temp -= cheap.first; n     long long days = 0; n     while (i < bug.size() && days < val) { n       allot[bug[i].second] = cheap.second; n       i++; n       days++; n     } n   } n   return 1; n } n long long lowerbound(long long l, long long h) { n   long long mid = (l + h) / 2; n   if (l == h) return mid; n   if (ok(mid)) { n     return lowerbound(l, mid); n   } else n     return lowerbound(mid + 1, h); n } n int main() { n   cin.sync_with_stdio(0); n   cin >> n >> m >> s; n   for (int i = 0; i <= m - 1; i++) { n     long long a; n     cin >> a; n     bug.push_back(make_pair(a, i)); n   } n   sort(bug.begin(), bug.end()); n   reverse(bug.begin(), bug.end()); n   for (int i = 0; i <= n - 1; i++) { n     long long a; n     cin >> a; n     stud.push_back(make_pair(a, i)); n   } n   sort(stud.begin(), stud.end()); n   reverse(stud.begin(), stud.end()); n   for (int i = 0; i <= n - 1; i++) { n     long long a; n     cin >> a; n     pass.push_back(a); n   } n   long long l = 0; n   long long h = bug.size(); n   long long temp = lowerbound(l, h); n   if (ok(temp)) { n     cout <<  YES n ; n     for (int i = 0; i <= m - 1; i++) cout << allot[i] + 1 <<    ; n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[100001], mat2[100001]; n long long k, suma, a, b, s, c; n int n; n map<int, int> mapa; n long long ct; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     cin >> mat[i]; n     mapa[mat[i]]++; n   } n   sort(mat, mat + n); n   suma = 1; n   c = 1; n   for (int i = 0; i < n; i++) { n     if (i == 0) { n     } else if (mat[i] == mat[i - 1]) { n       suma += n; n       continue; n     } n     ct = mapa[mat[i]]; n     if (suma + ct * n > k) { n       a = mat[i]; n       s = k - suma; n       b = mat[(int)(s / ct)]; n       break; n     } n     suma += n; n   } n   cout << a <<     << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxm = 1e5 + 5; n vector<int> g[maxm]; n int n, q; n int sz[maxm]; n int d[maxm]; n int f[maxm][25]; n const int maxd = 20; n void dfs(int x, int fa) { n   sz[x] = 1; n   for (int j = 1; j <= maxd; j++) { n     f[x][j] = f[f[x][j - 1]][j - 1]; n   } n   for (int v : g[x]) { n     if (v == fa) continue; n     d[v] = d[x] + 1; n     f[v][0] = x; n     dfs(v, x); n     sz[x] += sz[v]; n   } n } n int lca(int a, int b) { n   if (d[a] < d[b]) swap(a, b); n   for (int i = maxd; i >= 0; i--) { n     if (d[f[a][i]] >= d[b]) { n       a = f[a][i]; n     } n   } n   if (a == b) return a; n   for (int i = maxd; i >= 0; i--) { n     if (f[a][i] != f[b][i]) { n       a = f[a][i]; n       b = f[b][i]; n     } n   } n   return f[a][0]; n } n signed main() { n   ios::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   d[1] = 1; n   f[1][0] = 1; n   dfs(1, 1); n   cin >> q; n   while (q--) { n     int a, b; n     cin >> a >> b; n     if (a == b) { n       cout << n << endl; n       continue; n     } n     int lc = lca(a, b); n     int dab = d[a] + d[b] - 2 * d[lc]; n     if (dab % 2) { n       cout << 0 << endl; n       continue; n     } n     if (d[a] < d[b]) swap(a, b); n     int dist = dab / 2; n     for (int i = maxd; i >= 0; i--) { n       if (dist - (1 << i) > 0) { n         a = f[a][i]; n         b = f[b][i]; n         dist -= (1 << i); n       } n     } n     if (d[a] == d[b]) { n       cout << n - sz[a] - sz[b] << endl; n       continue; n     } n     cout << sz[f[a][0]] - sz[a] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n const double PI = acos(-1); n template <class A, class B> n std::ostream& operator<<(std::ostream& st, const std::pair<A, B> p) { n   st <<  (  << p.first <<  ,   << p.second <<  ) ; n   return st; n } n using namespace std; n const int MAX = 100 * 1000 + 10; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n std::vector<int> cand_base{26,  29,  31,  37,  41,  43,  47,  53, 59, n                            61,  67,  71,  73,  79,  83,  89,  97, 101, n                            103, 107, 109, 113, 127, 131, 137, 139}; n std::vector<int> cand_mod{1000001273, 1000001279, 1000001311, 1000001329, n                           1000001333}; n int base[2], mod[2], base_power[2][MAX]; n int n; n std::vector<int> adj[MAX]; n int siz[MAX]; n int add(int a, int b, int m) { n   a += b; n   if (a >= m) a -= m; n   if (a < 0) a += m; n   if (a >= m || a < 0) a = (a % m + m) % m; n   return a; n } n int mul(int a, int b, int m) { return (a * (long long)b) % m; } n void init_hashing() { n   srand(0); n   shuffle(cand_base.begin(), cand_base.end(), rng); n   shuffle(cand_mod.begin(), cand_mod.end(), rng); n   for (int i = 0; i < (2); i++) base[i] = cand_base[i] + n; n   for (int i = 0; i < (2); i++) mod[i] = cand_mod[i]; n   for (int i = 0; i < (2); i++) base_power[i][0] = 1; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < MAX - 1; j++) n       base_power[i][j + 1] = mul(base_power[i][j], base[i], mod[i]); n } n struct DS { n   map<std::pair<int, int>, int> cnt; n   void add(std::pair<int, int> x, int v) { n     cnt[x] += v; n     if (cnt[x] == 0) n       cnt.erase(x); n     else n       assert(cnt[x] > 0); n   } n }; n std::pair<int, int> f[MAX]; n vector<pair<std::pair<int, int>, int>> children[MAX]; n DS ds; n std::pair<int, int> merge(std::pair<int, int> L, std::pair<int, int> R, n                           int len) { n   std::pair<int, int> h; n   h.first = add(mul(L.first, base_power[0][len], mod[0]), R.first, mod[0]); n   h.second = add(mul(L.second, base_power[1][len], mod[1]), R.second, mod[1]); n   return h; n } n std::pair<int, int> dfs(int u, int p) { n   auto& C = children[u]; n   for (int v : adj[u]) n     if (v != p) { n       auto tmp = dfs(v, u); n       C.emplace_back(tmp, v); n       siz[u] += siz[v]; n     } n   sort(C.begin(), C.end()); n   f[u] = std::pair<int, int>(0, 0); n   siz[u] = 1; n   for (auto [h, v] : C) { n     f[u] = merge(f[u], h, siz[v]); n     siz[u] += siz[v]; n   } n   f[u] = merge(f[u], std::pair<int, int>(siz[u], siz[u]), 1); n   ds.add(f[u], 1); n   return f[u]; n } n std::pair<int, int> ans; n void dfs(int u, int p, std::pair<int, int> fp) { n   std::pair<int, int> old = f[u]; n   ds.add(old, -1); n   ans = max(ans, std::pair<int, int>(ds.cnt.size(), -u)); n   auto& C = children[u]; n   if (p) { n     C.emplace_back(fp, p); n     sort(C.begin(), C.end()); n   } n   int m = ((int)C.size()); n   std::vector<int> sizes(m); n   std::vector<std::pair<int, int>> suff(m); n   for (int i = 0; i < (m); i++) { n     int v = C[i].second; n     sizes[i] = (v == p) ? (n - siz[u]) : siz[v]; n   } n   for (int i = m - 1, tot = 0; i > 0; i--) { n     std::pair<int, int> h = C[i].first; n     suff[i - 1] = merge(h, suff[i], tot); n     tot += sizes[i]; n   } n   std::pair<int, int> H(0, 0); n   int tot = 0; n   for (int i = 0; i < (m); i++) { n     auto [h, v] = C[i]; n     int s = sizes[i]; n     std::pair<int, int> tmp = merge(H, suff[i], n - 1 - s - tot); n     tmp = merge(tmp, std::pair<int, int>(n - s, n - s), 1); n     ds.add(tmp, 1); n     if (v != p) dfs(v, u, tmp); n     ds.add(tmp, -1); n     H = merge(H, h, s); n     tot += s; n   } n   ds.add(old, 1); n } n int main() { n   scanf( %d , &n); n   for (int e = 0; e < (n - 1); e++) { n     int a, b; n     scanf( %d %d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   init_hashing(); n   dfs(1, 0); n   ans = std::pair<int, int>(-1, 0); n   dfs(1, 0, std::pair<int, int>(0, 0)); n   cout << -ans.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) n #pragma GCC optimize( fast-math ) n const long long N = 1e6 + 5; n const long double EPS = 1e-12; n const long long INF = 2e18 + 5; n const long long MOD = 1e9 + 7; n const long double PI = 3.14159265358979323; n vector<set<long long> > g; n vector<pair<long long, long long> > a; n vector<long long> ans, u; n void dfs(long long v) { n   u[v] = 1; n   for (auto &i : g[v]) n     if (!u[i]) dfs(i); n   ans.push_back(v); n } n void topsort(long long n) { n   ans.clear(); n   for (long long i = 0; i < n; i++) { n     if (!u[i]) dfs(i); n   } n   reverse(ans.begin(), ans.end()); n } n bool bin(long long n, long long m) { n   for (long long i = 0; i < n; i++) { n     g[i].clear(); n     u[i] = 0; n   } n   for (long long i = 0; i < m; i++) { n     g[a[i].first].insert(a[i].second); n   } n   topsort(n); n   for (long long i = 0; i + 1 < ans.size(); i++) { n     if (g[ans[i]].find(ans[i + 1]) == g[ans[i]].end()) return 0; n   } n   return 1; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long ttt = 1; n   for (; ttt; --ttt) { n     long long n, m; n     cin >> n >> m; n     a.resize(m); n     g.resize(n); n     u.resize(n); n     for (long long i = 0; i < m; i++) { n       cin >> a[i].first >> a[i].second; n       a[i].first--; n       a[i].second--; n     } n     if (!bin(n, m)) { n       return cout <<  -1 n , 0; n     } n     long long l = 1, r = m; n     while (r - l > 1) { n       long long mid = (l + r) / 2; n       if (mid <= m && bin(n, mid)) n         r = mid; n       else n         l = mid; n     } n     if (bin(n, l)) n       cout << l <<   n ; n     else n       cout << r <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, *a, *b, times, i, day; n   while (cin >> n >> m) { n     a = new int[m]; n     b = new int[m]; n     times = 1; n     for (i = 0; i < m; i++) cin >> a[i] >> b[i]; n     for (i = 0; i < m - 1; i++) { n       if (b[i] != a[i + 1] - 1) break; n     } n     if (i + 1 == m && b[i] == n) n       cout <<  OK  << endl; n     else { n       if (a[0] != 1) { n         day = 1; n         times--; n       } else if (b[i] == a[i + 1]) { n         day = b[i]; n         while (b[i] == a[i + 1] && b[i] == day) { n           times++; n           i++; n         } n       } else { n         day = b[i] + 1; n         times--; n       } n       cout << day <<     << times << endl; n     } n     delete[] a; n     delete[] b; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   char m[11], digit[10] = { 0 }, digitcount(0); n   scanf( %d%s , &n, &m); n   while (n != 0) { n     digit[digitcount++] =  0  + n % 10; n     n /= 10; n   } n   sort(digit, digit + digitcount); n   if (digit[0] ==  0 ) { n     for (int i = 1; i < digitcount; ++i) { n       if (digit[i] !=  0 ) { n         digit[0] = digit[i]; n         digit[i] =  0 ; n         break; n       } n     } n   } n   if (strcmp(m, digit) == 0) n     cout <<  OK  << endl; n   else n     cout <<  WRONG_ANSWER  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1000 + 10; n int n, flag, a[maxN], i, j, m; n int main() { n   cin >> n; n   for (i = 2; i <= n; i++) { n     for (j = 2; j * j <= i; j++) { n       if (i != j && i % j == 0) flag = 1; n     } n     j = i; n     while (flag == 0 && j <= n) { n       a[m++] = j; n       j *= i; n     } n     flag = 0; n   } n   cout << m << endl; n   for (int k = 0; k < m; k++) cout << a[k] <<    ; 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 < 0) a = -a; n   if (b < 0) b = -b; n   return b == 0 ? a : gcd(b, a % b); n } n template <typename T> n void read(T &res) { n   bool flag = false; n   char ch; n   while (!isdigit(ch = getchar())) (ch ==  - ) && (flag = true); n   for (res = ch - 48; isdigit(ch = getchar()); n        res = (res << 1) + (res << 3) + ch - 48) n     ; n   flag && (res = -res); n } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n long long qp(long long a, long long b, long long mod) { n   long long ans = 1; n   if (b == 0) { n     return ans % mod; n   } n   while (b) { n     if (b % 2 == 1) { n       b--; n       ans = ans * a % mod; n     } n     a = a * a % mod; n     b = b / 2; n   } n   return ans % mod; n } n long long qpn(long long a, long long b, long long p) { n   long long ans = 1; n   a %= p; n   while (b) { n     if (b & 1) { n       ans = (ans * a) % p; n       --b; n     } n     a = (a * a) % p; n     b >>= 1; n   } n   return ans % p; n } n set<long long> s; n signed main() { n   long long t; n   read(t); n   while (t--) { n     long long n; n     read(n); n     long long po = 0; n     s.insert(po); n     for (long long i = 1; i * i <= n; i++) { n       s.insert(n / i); n       s.insert(n / (n / i)); n     } n     printf( %d n , s.size()); n     for (auto x : s) { n       printf( %lld  , x); n     } n     s.clear(); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } 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   int 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 const long long N = 2e5 + 10; n const long long mod = 1e9 + 7; n struct Trie { n   Trie *child[2]; n   long long cnt; n }; n Trie *createNode() { n   Trie *node = (Trie *)malloc(sizeof(Trie)); n   memset(node->child, NULL, sizeof node->child); n   node->cnt = 0; n   return node; n } n void insert(Trie *root, long long x) { n   for (long long i = 30; i >= 0; --i) { n     long long val = (1LL << i); n     long long bit = 0; n     if (val & x) bit = 1; n     if (!root->child[bit]) root->child[bit] = createNode(); n     root = root->child[bit]; n     root->cnt++; n   } n } n void erase(Trie *root, long long x) { n   for (long long i = 30; i >= 0; --i) { n     long long val = (1LL << i); n     long long bit = 0; n     if (val & x) bit = 1; n     if (!root->child[bit]) root->child[bit] = createNode(); n     root = root->child[bit]; n     root->cnt--; n   } n } n long long query(Trie *root, long long x) { n   long long ans = 0; n   for (long long i = 30; i >= 0; --i) { n     long long val = (1 << i); n     long long bit = 0; n     if (val & x) bit = 1; n     if (root->child[bit] && root->child[bit]->cnt > 0) { n       root = root->child[bit]; n     } else { n       assert(root->child[bit ^ 1] && root->child[bit ^ 1]->cnt > 0); n       ans += val; n       root = root->child[bit ^ 1]; n     } n   } n   return ans; n } n void solve() { n   long long n; n   cin >> n; n   long long a[n], b[n]; n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   for (long long i = 0; i < n; ++i) cin >> b[i]; n   Trie *root = createNode(); n   for (long long i = 0; i < n; ++i) { n     insert(root, b[i]); n   } n   vector<long long> ans; n   for (long long i = 0; i < n; ++i) { n     long long x = query(root, a[i]); n     ans.push_back(x); n     erase(root, a[i] ^ x); n   } n   for (long long i = 0; i < n; ++i) cout << ans[i] <<    ; n   cout <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline int read(T &a) { n   a = 0; n   char c = getchar(), f = 1; n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = (~f) + 1; n     if (c == -1) return -1; n     c = getchar(); n   } n   while (c <=  9  && c >=  0 ) n     a = (a << 1) + (a << 3) + (c ^ 48), c = getchar(); n   a *= f; n   return 1; n } n template <typename T> n inline int write(T a) { n   if (a < 0) putchar( - ), a = (~a) + 1; n   if (a / 10) return write(a / 10) & putchar(a % 10 | 48); n   return putchar(a | 48); n } n template <typename T> n inline int write(T a, char c) { n   return write(a) + putchar(c); n } n int n, m; n int w[200001]; n int x[200001], y[200001]; n int need[200001]; n bool used[200001]; n vector<int> v[200001]; n bool ok[200001]; n queue<int> q; n int ans[200001], cnt; n int main() { n   read(n), read(m); n   for (int i = 1; i <= n; i++) read(w[i]); n   for (int i = 1; i <= m; i++) { n     read(x[i]), read(y[i]), need[x[i]]++, need[y[i]]++; n     v[x[i]].push_back(i); n     v[y[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) n     if (need[i] <= w[i]) { n       used[i] = 1; n       for (auto j : v[i]) n         if (!ok[j]) { n           ok[j] = 1; n           q.push(j); n         } n     } n   while (!q.empty()) { n     int t = q.front(); n     q.pop(); n     ans[++cnt] = t; n     need[x[t]]--, need[y[t]]--; n     int i = y[t]; n     if (need[i] <= w[i] && !used[i]) { n       used[i] = 1; n       for (auto j : v[i]) n         if (!ok[j]) { n           ok[j] = 1; n           q.push(j); n         } n     } n     i = x[t]; n     if (need[i] <= w[i] && !used[i]) { n       used[i] = 1; n       for (auto j : v[i]) n         if (!ok[j]) { n           ok[j] = 1; n           q.push(j); n         } n     } n   } n   if (cnt != m) return puts( DEAD ) & 0; n   puts( ALIVE ); n   for (int i = cnt; i >= 1; i--) write(ans[i],    ); n   puts(  ); n } n


#include <bits/stdc++.h> n int b[12][12]; n int main() { n   int a; n   int i, j; n   int sum; n   scanf( %d , &a); n   memset(b, 0, sizeof(b)); n   for (i = 1; i <= 10; i++) n     for (j = 1; j <= 10; j++) { n       b[1][j] = b[i][1] = 1; n       b[i][j] = b[i - 1][j] + b[i][j - 1]; n     } n   printf( %d , b[a][a]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, Cnt = 0, Num = 0; n int l[200000 + 10], r[200000 + 10]; n map<int, int> mp; n int Di[200000 + 10]; n vector<int> f[200000 + 10], No[200000 + 10]; n int cur[200000 + 10]; n bool b[200000 + 10]; n int Ans[200000 + 10]; n int Check() { n   int Now = -1, ODD = 0; n   for (int i = 1; i <= Cnt; i++) n     if (f[i].size() % 2 == 1) { n       ODD++; n       Now = i; n     } n   if (ODD == 2) return Now; n   if (ODD == 0) return 1; n   return -1; n } n void Euler(int x) { n   for (int i = cur[x]; i < f[x].size(); i = cur[x]) { n     cur[x] = i + 1; n     if (b[No[x][i]]) continue; n     b[No[x][i]] = true; n     Euler(f[x][i]); n   } n   Ans[++Ans[0]] = x; n } n int main() { n   scanf( %d , &n); n   n--; n   for (int i = 1; i <= n; i++) scanf( %d , &l[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &r[i]); n   int x, y; n   for (int i = 1; i <= n; i++) { n     if (l[i] > r[i]) return printf( -1 ), 0; n     if (!mp.count(l[i])) n       x = mp[l[i]] = ++Cnt, Di[Cnt] = l[i]; n     else n       x = mp[l[i]]; n     if (!mp.count(r[i])) n       y = mp[r[i]] = ++Cnt, Di[Cnt] = r[i]; n     else n       y = mp[r[i]]; n     Num++; n     f[x].push_back(y); n     No[x].push_back(Num); n     f[y].push_back(x); n     No[y].push_back(Num); n   } n   int Begin = Check(); n   if (Begin == -1) return printf( -1 ), 0; n   Euler(Begin); n   if (Ans[0] != n + 1) return printf( -1 ), 0; n   for (int i = n + 1; i; i--) printf( %d  , Di[Ans[i]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n >> k; n   if (k < n / 2 || (n == 1 && k)) { n     cout << -1; n     return 0; n   } n   if (n == 1 && k == 0) { n     cout << 1; n     return 0; n   } n   k = k - (n - 2) / 2; n   cout << k <<     << k * 2 <<    ; n   k = k * 2 + 1; n   for (int i = 0; i <= n - 3; i++) cout << k + i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void qmax(long long &x, int y) { n   if (x < y) x = y; n } n void qmin(long long &x, long long y) { n   if (x > y) x = y; n } n inline long long read() { n   char s; n   long long k = 0, base = 1; n   while ((s = getchar()) !=  -  && s != EOF && !(isdigit(s))) n     ; n   if (s == EOF) exit(0); n   if (s ==  - ) base = -1, s = getchar(); n   while (isdigit(s)) { n     k = k * 10 + (s ^  0 ); n     s = getchar(); n   } n   return k * base; n } n inline void write(int x) { n   static char cnt, num[15]; n   cnt = 0; n   if (!x) { n     printf( 0 ); n     return; n   } n   for (; x; x /= 10) num[++cnt] = x % 10; n   for (; cnt; putchar(num[cnt--] + 48)) n     ; n } n const int maxn = 1e5 + 100; n int n, m, k, s, X, Y; n int a[maxn]; n int po[maxn], ne[maxn * 2], to[maxn * 2], id; n void add(int x, int y) { n   id++; n   to[id] = y; n   ne[id] = po[x]; n   po[x] = id; n } n int dis[maxn], vis[maxn]; n queue<int> q; n int c[maxn][101]; n void spfa(int x) { n   memset(vis, 0, sizeof(vis)); n   memset(dis, 0x3f3f3f3f, sizeof(dis)); n   int inf = dis[0]; n   while (!q.empty()) q.pop(); n   for (int i = 1; i <= n; i++) n     if (a[i] == x) { n       dis[i] = 0; n       vis[i] = 1; n       q.push(i); n     } n   while (!q.empty()) { n     int u = q.front(), v; n     q.pop(); n     vis[u] = 0; n     for (int i = po[u]; i; i = ne[i]) { n       v = to[i]; n       if (dis[v] > dis[u] + 1) { n         dis[v] = dis[u] + 1; n         if (!vis[v]) { n           vis[v] = 1; n           q.push(v); n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) c[i][x] = dis[i]; n } n int main() { n   n = read(); n   m = read(); n   k = read(); n   s = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= m; i++) { n     X = read(); n     Y = read(); n     add(X, Y); n     add(Y, X); n   } n   for (int i = 1; i <= k; i++) spfa(i); n   for (int i = 1; i <= n; i++) { n     long long sum = 0; n     sort(c[i] + 1, c[i] + k + 1); n     for (int j = 1; j <= s; j++) sum += c[i][j]; n     printf( %lld  , sum); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int pos[1000]; n int n, a, b; n int main() { n   cin >> n; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  x ) n       a++; n     else n       b++; n   } n   cout << abs(n / 2 - a) << endl; n   int t = abs(n / 2 - a); n   if (a > b) { n     for (int i = 0; i < s.size(); i++) n       if (s[i] ==  x  && t) { n         s[i] =  X ; n         t--; n       } n   } else if (a < b) { n     for (int i = 0; i < s.size(); i++) n       if (s[i] ==  X  && t) { n         s[i] =  x ; n         t--; n       } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n; n   cin >> t; n   while (t--) { n     int ans = 0; n     cin >> n; n     cout << n / 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void checkDefine(); n long long int a, b, c, d; n int main() { n   cin >> a >> b >> c >> d; n   long long int t1 = max(3 * a / 10, a - (a * c) / 250); n   long long int t2 = max(3 * b / 10, b - (b * d) / 250); n   if (t1 > t2) { n     cout <<  Misha ; n   } else if (t1 < t2) { n     cout <<  Vasya ; n   } else { n     cout <<  Tie ; n   } n   return 0; n } n void checkDefine() { n   long long int n, a[200005]; n   map<long long int, long long int> m; n   cin >> n; n   for (int i = (0), _b = (n - 1); i <= _b; i++) { n     cin >> a[i]; n     m[a[i]]++; n   } n   string s; n   cin >> s; n   { n     cout <<  s  n          <<   =  ; n     cout << (s) << endl; n   }; n   { n     cout <<  a  n          <<   =  ; n     for (int _ = 0, _a = (n); _ < _a; _++) cout << a[_] <<    ; n     cout << endl; n   }; n   { n     cout <<   ------------   n          <<   =  ; n     cout << ( ------------ ) << endl; n   }; n   for (__typeof(m.begin()) it = m.begin(); it != m.end(); ++it) { n     cout << it->first <<     << it->second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, cnt[200005], x, y, ans; n vector<long long> v[200005]; n void dfs(long long pos, long long pre) { n   long long len = v[pos].size(); n   for (long long i = 0; i < len; i++) { n     long long xx = v[pos][i]; n     if (xx != pre) { n       dfs(xx, pos); n       cnt[pos] += cnt[xx]; n       ans += min(cnt[xx], 2 * k - cnt[xx]); n     } n   } n } n int main() { n   scanf( %lld%lld , &n, &k); n   for (long long i = 0; i < 2 * k; i++) { n     scanf( %lld , &x); n     cnt[x] = 1; n   } n   for (long long i = 0; i < n - 1; i++) { n     scanf( %lld%lld , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1, 0); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(pair<long long, int> l, pair<long long, int> r) { n   return l.first < r.first; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, A, cf, cm, money; n   cin >> n >> A >> cf >> cm >> money; n   vector<pair<long long, int> > v(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> v[i].first; n     v[i].second = i; n   } n   sort(v.begin(), v.end()); n   vector<long long> pf(n + 1, 0); n   long long c = 0; n   for (int i = 1; i <= n; i++) pf[i] = v[i].first + pf[i - 1]; n   vector<long long> ps(n + 2, 0); n   for (int i = n; i >= 1; i--) ps[i] = ps[i + 1] + v[i].first; n   long long ans = 0; n   long long no = 0; n   long long mn = 0; n   for (long long i = 0; i <= n; i++) { n     long long m = money; n     m = m - max((long long)0, (A * i - ps[n - i + 1])); n     if (m < 0) continue; n     long long pnts = cf * i; n     int lo = 0; n     int hi = A; n     while (lo < hi) { n       long long mid = (lo + hi + 1) >> (long long)1; n       vector<pair<long long, int> >::iterator it = lower_bound( n           v.begin(), v.begin() + (n - i + 1), make_pair(mid, 0), cmp); n       if (it == v.begin()) { n         lo = mid; n         continue; n       } else n         it--; n       long long reqd = ((long long)(it - v.begin())) * mid - pf[it - v.begin()]; n       if (reqd <= m) n         lo = mid; n       else n         hi = mid - 1; n     } n     pnts += lo * cm; n     if (ans < pnts) { n       ans = pnts; n       no = i; n       mn = lo; n     } n     ans = max(ans, pnts); n   } n   cout << ans <<   n ; n   ; n   vector<long long> re(n + 1, 0); n   for (int i = n; i > n - no; i--) re[(int)v[i].second] = A; n   for (int i = 1; i <= n - no; i++) re[v[i].second] = max(v[i].first, mn); n   for (int i = 1; i <= n; i++) cout << re[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n int nextInt() { n   int x; n   scanf( %d , &x); n   return x; n } n double nextDouble() { n   double x; n   scanf( %lf , &x); n   return x; n } n long long nextLong() { n   long long x; n   scanf( %I64d , &x); n   return x; n } n char nextChar() { n   char x; n   scanf( %c , &x); n   return x; n } n void newLine() { printf(  n ); } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long powM(long long a, long long b, long long MOD) { n   long long x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y); n       if (x > MOD) x %= MOD; n     } n     y = (y * y); n     if (y > MOD) y %= MOD; n     b /= 2; n   } n   return x; n } n long long ModExp(long long n, long long k, long long MOD) { n   long long ans = 1; n   while (k > 0) { n     if (k & 1) ans = (ans * n) % MOD; n     n = (n * n) % MOD; n     k /= 2; n   } n   return ans; n } n long long Exp(long long n, long long k) { n   long long ans = 1; n   while (k > 0) { n     if (k & 1) ans = (ans * n); n     n = (n * n); n     k /= 2; n   } n   return ans; n } n int countSetBits(long long n) { n   int ans = 0; n   while (n != 0) { n     n -= (n & -n); n     ++ans; n   } n   return ans; n } n const int N = 100100; n const int M = 12; n const int INF = 1e6; n const double EPS = 1e-6; n const long double PI = 2 * acos(0); n using namespace std; n const int LOG = 18; n long long mod = 1000000009; n int par[N]; n int getPar(int x) { return x == par[x] ? x : par[x] = getPar(par[x]); }; n bool same(int x, int y) { return getPar(x) == getPar(y); } n void merge(int x, int y) { n   int xx = getPar(x); n   int yy = getPar(y); n   if (xx != yy) par[xx] = yy; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) par[i] = i; n   long long ans = 1; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     if (same(x, y)) { n       ans = (ans * 2ll) % mod; n     } else { n       merge(x, y); n     } n     cout << (ans + mod - 1) % mod << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   set<int> co[2]; n   set<pair<int, int> > delta[2]; n   int w, h, n; n   scanf( %d%d%d , &w, &h, &n); n   co[0].insert(0); n   co[0].insert(w); n   delta[0].insert(make_pair(w, 0)); n   co[1].insert(0); n   co[1].insert(h); n   delta[1].insert(make_pair(h, 0)); n   for (int i = (0), _end_ = (n); i < _end_; ++i) { n     static char s[2]; n     static int first; n     scanf( %s%d , s, &first); n     int ty = s[0] ==  H ; n     co[ty].insert(first); n     auto it = co[ty].lower_bound(first); n     auto tmp = it; n     --tmp; n     ++it; n     delta[ty].erase(make_pair(*it - *tmp, *tmp)); n     delta[ty].insert(make_pair(first - *tmp, *tmp)); n     delta[ty].insert(make_pair(*it - first, first)); n     printf( %I64d n , n            (long long)delta[0].rbegin()->first * delta[1].rbegin()->first); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void func() { n   string s; n   cin >> s; n   long long n = s.length(); n   long long i = 1, len = 0, pre[n]; n   pre[0] = 0; n   while (i < n) { n     if (s[i] == s[len]) { n       len++; n       pre[i++] = len; n     } else { n       if (len == 0) { n         pre[i++] = 0; n       } else { n         len = pre[len - 1]; n       } n     } n   } n   if (pre[n - 1] == 0) { n     cout <<  Just a legend  n          <<   n ; n     return; n   } n   for (long long i = 0; i < n - 1; ++i) { n     if (pre[i] == pre[n - 1]) { n       cout << s.substr(0, pre[n - 1]) <<   n ; n       return; n     } n   } n   if (pre[pre[n - 1] - 1] == 0) { n     cout <<  Just a legend  n          <<   n ; n     return; n   } n   cout << s.substr(0, pre[pre[n - 1] - 1]) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     func(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n struct item { n   long long t, d, p, idx; n }; n item sh[200005]; n bool cmp(item x, item y) { return x.d < y.d; } n long long n; n long long dp[105][2005]; n long long sol(long long i, long long cur) { n   if (i == n) return 0; n   if (~dp[i][cur]) return dp[i][cur]; n   if (sh[i].t + cur >= sh[i].d) return dp[i][cur] = sol(i + 1, cur); n   return dp[i][cur] = max(sh[i].p + sol(i + 1, cur + sh[i].t), sol(i + 1, cur)); n } n vector<long long> ans; n void print(long long i, long long cur) { n   if (i == n) return; n   if (sh[i].t + cur >= sh[i].d) { n     print(i + 1, cur); n   } else if (sh[i].p + sol(i + 1, cur + sh[i].t) > sol(i + 1, cur)) { n     ans.push_back(sh[i].idx); n     print(i + 1, cur + sh[i].t); n   } else n     print(i + 1, cur); n } n void solve() { n   long long i, j, k, l, r, m, a, b, c, d, x, y, z, t; n   cin >> n; n   memset(dp, -1, sizeof dp); n   for (i = 0; i < n; i++) { n     cin >> sh[i].t >> sh[i].d >> sh[i].p; n     sh[i].idx = i + 1; n   } n   sort(sh, sh + n, cmp); n   cout << sol(0, 0) << endl; n   print(0, 0); n   cout << ans.size() << endl; n   for (auto &cv : ans) { n     cout << cv <<    ; n   } n   cout << endl; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n int n, m, i, j, k, l, a, b, c; n char s[200][200]; n bool bo; n void fail() { bo = false; } n bool work(int x, int y, int o, int p, int a, int b, int c) { n   int i, j; n   if (x > o) { n     return true; n   } n   if ((a < 0) || (b < 0) || (c < 0)) return false; n   if (x == o) { n     j = 0; n     for (i = 1; i <= m; i += 2) { n       s[1][i] = s[1][i + 1] =  a  + j; n       j = (j ^ 1); n     } n     return true; n   } else if (y == p) { n     j = 0; n     for (i = 1; i <= n; i += 2) { n       s[i][1] = s[i + 1][1] =  a  + j; n       j = (j ^ 1); n     } n     return true; n   } else { n     bool bo[29]; n     for (i = 0; i < 26; i++) { n       bo[i] = false; n     } n     if (s[x - 1][y] != 0) { n       bo[s[x - 1][y] -  a ] = true; n     } n     if (s[x - 1][y + 1] != 0) { n       bo[s[x - 1][y + 1] -  a ] = true; n     } n     if (s[x][y - 1] != 0) { n       bo[s[x][y - 1] -  a ] = true; n     } n     if (s[x + 1][y - 1] != 0) { n       bo[s[x + 1][y - 1] -  a ] = true; n     } n     if (c >= 1) { n       char ch; n       for (i = 0; i < 26; i++) n         if (!bo[i]) { n           break; n         } n       ch =  a  + i; n       s[x][y] = s[x][y + 1] = s[x + 1][y] = s[x + 1][y + 1] = ch; n       c--; n     } else if (a >= 2) { n       char ch[2]; n       int k; n       k = 0; n       for (i = 0; i < 26; i++) n         if (!bo[i]) { n           ch[k] =  a  + i; n           k++; n           if (k == 2) { n             break; n           } n         } n       s[x][y] = s[x][y + 1] = ch[0]; n       s[x + 1][y] = s[x + 1][y + 1] = ch[1]; n       a -= 2; n     } else if (b >= 2) { n       char ch[2]; n       int k; n       k = 0; n       for (i = 0; i < 26; i++) n         if (!bo[i]) { n           ch[k] =  a  + i; n           k++; n           if (k == 2) { n             break; n           } n         } n       s[x][y] = s[x + 1][y] = ch[0]; n       s[x][y + 1] = s[x + 1][y + 1] = ch[1]; n       b -= 2; n     } else { n       return false; n     } n     y += 2; n     if (y > m) { n       x += 2; n       y = 1; n       if ((m & 1) == 1) { n         y = 2; n       } n     } n   } n   return work(x, y, o, p, a, b, c); n } n void rect(int x, int y, int o, int p, int a, int b, int c) { n   if (!work(x, y, o, p, a, b, c)) { n     fail(); n   } n } n int main() { n   bo = true; n   scanf( %d%d%d%d%d , &n, &m, &a, &b, &c); n   for (i = 0; i <= n; i++) n     for (j = 0; j <= m; j++) { n       s[i][j] = 0; n     } n   if ((((n * m) & 1) == 1) || (a * 2 + b * 2 + c * 4 < n * m)) { n     fail(); n   } else if ((n & 1) == 1) { n     rect(1, 1, 1, m, m / 2, 0, 0); n     if (n > 1) { n       rect(2, 1, n, m, a - m / 2, b, c); n     } else { n       if (a < m / 2) { n         fail(); n       } n     } n   } else if ((m & 1) == 1) { n     rect(1, 1, n, 1, 0, n / 2, 0); n     if (m > 1) { n       rect(1, 2, n, m, a, b - n / 2, c); n     } else { n       if (b < n / 2) { n         fail(); n       } n     } n   } else { n     rect(1, 1, n, m, a, b, c); n   } n   if (bo) { n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         printf( %c , s[i][j]); n       } n       printf(  n ); n     } n   } else { n     printf( IMPOSSIBLE n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 4e18; n const long long MAX = 1e5; n const long long MV = 1e18; n const long double PI = 3.14159265358979323846; n long long MA = -INF, MI = INF; n long long PO(long long x, long long y) { n   if (y == 0) return 1; n   long long ans = PO(x, y / 2); n   ans *= ans, ans %= MOD; n   if (y % 2 == 1) ans *= x, ans %= MOD; n   return ans % MOD; n } n void RE(long long x) { n   x %= MOD; n   if (x < 0) x += MOD; n } n long long fib[100], dp1[100], dp2[100], siz = 2; n void cal() { n   fib[1] = 1, fib[2] = 2; n   while (true) { n     long long temp = fib[siz - 1] + fib[siz]; n     if (temp > MV) n       break; n     else n       fib[++siz] = temp; n   } n } n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), n       cout << fixed << setprecision(10); n   cal(); n   long long t; n   cin >> t; n   deque<long long> rep; n   while (t--) { n     long long n; n     cin >> n; n     for (long long i = siz; i >= 1; i--) { n       if (n >= fib[i]) rep.push_back(i), n -= fib[i]; n     } n     reverse(rep.begin(), rep.end()); n     rep.push_front(0); n     long long len = rep.size(); n     dp1[0] = 1; n     dp2[0] = 0; n     for (long long i = 1; i < len; i++) { n       dp1[i] = dp1[i - 1] + dp2[i - 1]; n       dp2[i] = (dp1[i - 1] * ((rep[i] - rep[i - 1] - 1) / 2)) + n                (dp2[i - 1] * ((rep[i] - rep[i - 1]) / 2)); n     } n     cout << dp1[len - 1] + dp2[len - 1] <<   n ; n     rep.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static long long mod = 998244353; n long long pow(long long x, long long y) { n   x = x % mod; n   int res = 1; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % mod; n     } n     y = y >> 1; n     x = (x * x) % mod; n   } n   return res; n } n long long modInverse(long long n, long long mod) { return pow(n, mod - 2); } n long long nCrmodFermat(long long n, long long r) { n   if (r == 0) return 1; n   long long fact[n + 1]; n   fact[0] = 1; n   for (long long i = 1; i <= n; i++) { n     fact[i] = ((fact[i - 1] % mod) * (i % mod)) % mod; n   } n   return (fact[n] * modInverse(fact[r], mod) % mod * n           modInverse(fact[n - r], mod) % mod) % n          mod; n } n int main() { n   long long n; n   cin >> n; n   vector<long long> v(2 * n); n   long long sum = 0; n   for (int i = 0; i < 2 * n; i++) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   int i = 0; n   int j = (2 * n) - 1; n   while (i < n && j >= n) { n     sum = (sum + abs(v[i] - v[j])) % mod; n     i++; n     j--; n   } n   cout << ((sum % mod) * (nCrmodFermat((2 * n), n) % mod)) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100010]; n vector<pair<long long, long long> > people; n long long Free[100010]; n int main() { n   long long ts, tf, t; n   cin >> ts >> tf >> t; n   int n; n   scanf( %d , &n); n   if (n == 0) { n     cout << ts << endl; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n   } n   people.push_back(make_pair(0, 0)); n   long long cnt = 1; n   for (int i = 1; i < n; i++) { n     if (a[i] != a[i + 1]) { n       people.push_back(make_pair(a[i], cnt)); n       cnt = 1; n     } else n       cnt++; n   } n   people.push_back(make_pair(a[n], cnt)); n   long long ans, wait; n   ans = a[1] - 1; n   wait = ts - (a[1] - 1); n   if (wait < 0) { n     wait = 0; n     ans = ts; n   } n   long long time = max(people[1].first, ts); n   for (int i = 1; i + 1 < people.size(); i++) { n     if (time + t > tf) break; n     time = time + people[i].second * t; n     if (time + t > tf) break; n     long long come_time = people[i + 1].first - 1; n     long long new_wait = time - come_time; n     if (new_wait < 0) { n       new_wait = 0; n       come_time = time; n     } n     if (new_wait < wait) { n       wait = new_wait; n       ans = come_time; n     } n     time = max(time, people[i + 1].first); n   } n   time = time + people.back().second * t; n   if (time + t <= tf) { n     ans = time; n     wait = 0; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int co = 0, i, n, y, m, j, k, t, x = 0; n   cin >> n; n   int a[n + 1]; n   for (i = 1; i <= n; i++) cin >> a[i]; n   co = 0; n   int c = 1; n   j = 0; n   vector<int> v; n   map<int, int> mp; n   for (i = 1; i <= n; i++) { n     mp[a[i]]++; n     co += a[i]; n     j++; n     if (a[i] < 0) { n       if (mp[-1 * a[i]] != 1) { n         co = -1; n         break; n       } n     } n     if (mp[a[i]] > 1) { n       c = 0; n       break; n     } n     if (co == 0) { n       v.push_back(j); n       mp.clear(); n       j = 0; n       co = 0; n     } n   } n   if (!co) { n     if (c) { n       cout << v.size() <<   n ; n       for (int i = 0; i < v.size(); i++) { n         cout << v[i] <<    ; n       } n     } else n       cout << -1 <<   n ; n   } else n     cout << -1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, tot; n long long ans, sum; n char s[100010], t[100010]; n void print(int x, int opt) { n   tot++; n   printf( %d %d n , x, opt); n   if (tot == 100000) exit(0); n } n bool check(int i, int opt) { n   if (i == n) return 0; n   if (opt == -1) { n     if (s[i + 1] >  0 ) return s[i]--, s[i + 1]--, print(i, -1), 1; n     if (check(i + 1, 1)) return s[i]--, s[i + 1]--, print(i, -1), 1; n     return 0; n   } n   if (opt == 1) { n     if (s[i + 1] <  9 ) return s[i]++, s[i + 1]++, print(i, 1), 1; n     if (check(i + 1, -1)) return s[i]++, s[i + 1]++, print(i, 1), 1; n     return 0; n   } n } n int main() { n   scanf( %d%s%s , &n, s + 1, t + 1); n   for (int i = 1; i < n; i++) { n     int S = sum + s[i] -  a , T = t[i] -  a ; n     ans += abs(S - T), sum = T - S; n   } n   if (sum + s[n] -  a  != t[n] -  a ) return puts( -1 ), 0; n   printf( %lld n , ans); n   for (int i = 1; i < n; i++) { n     while (s[i] > t[i]) n       if (!check(i, -1)) return puts( -1 ), 0; n     while (s[i] < t[i]) n       if (!check(i, 1)) return puts( -1 ), 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100005]; n int c[100005], N, full; n int LG; n void read() { n   scanf( %d n , &N); n   for (int line = 1; line <= N; ++line) { n     scanf( %s , s + 1); n     s[0] =  # ; n     int len = strlen(s) - 1; n     LG = len; n     for (int i = 1; i <= len; ++i) { n       if (s[i] ==  ? ) continue; n       if (c[i] == 0) { n         c[i] = s[i] -  a  + 1; n         continue; n       } n       if (c[i] == s[i] -  a  + 1) n         continue; n       else { n         if (c[i] != -1) { n           c[i] = -1; n           full++; n         } n         if (full == len) return; n       } n     } n   } n } n int main() { n   read(); n   for (int i = 1; i <= LG; ++i) n     if (c[i] == -1) n       printf( ? ); n     else if (c[i] == 0) n       printf( x ); n     else n       printf( %c , c[i] +  a  - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)2e9; n const double EPS = 1e-6; n const double PI = 3.1415926535; n const int MOD = 1000003; n int last[100000]; n int main() { n   int a, b, m, now, i = 2; n   cin >> a >> b >> m >> now; n   last[now] = 1; n   while (1) { n     now = (a * now + b) % m; n     if (last[now]) { n       cout << i - last[now]; n       return 0; n     } else n       last[now] = i++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Expr { n   char a[1010], b[1010]; n   int v; n   int op; n }; n Expr exps[5010]; n char ansMin[1010], ansMax[1010]; n char buf[10010]; n map<string, int> varIndex; n int calc(int a, int b, int op) { n   if (op == 1) n     return a & b; n   else if (op == 2) n     return a | b; n   else if (op == 3) n     return a ^ b; n } n int work(int bit, int n, int v) { n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (exps[i].op) { n       int a, b; n       if (isdigit(exps[i].a[0])) n         a = exps[i].a[bit] -  0 ; n       else if (exps[i].a[0] ==  ? ) n         a = v; n       else { n         string _var; n         _var.assign(exps[i].a); n         a = varIndex[_var]; n         a = exps[a].v; n       } n       if (isdigit(exps[i].b[0])) n         b = exps[i].b[bit] -  0 ; n       else if (exps[i].b[0] ==  ? ) n         b = v; n       else { n         string _var; n         _var.assign(exps[i].b); n         b = varIndex[_var]; n         b = exps[b].v; n       } n       exps[i].v = calc(a, b, exps[i].op); n       ans += exps[i].v; n     } else { n       exps[i].v = exps[i].a[bit] -  0 ; n       ans += exps[i].v; n     } n   } n   return ans; n } n int main() { n   int n, m; n   char op[16], var[16]; n   scanf( %d%d , &n, &m); n   getchar(); n   for (int i = 0; i < n; i++) { n     gets(buf); n     if (sscanf(buf,  %s := %s %s %s , var, exps[i].a, op, exps[i].b) == 4) { n       if (op[0] ==  A ) n         exps[i].op = 1; n       else if (op[0] ==  O ) n         exps[i].op = 2; n       else if (op[0] ==  X ) n         exps[i].op = 3; n     } else { n       sscanf(buf,  %s := %s , var, exps[i].a); n       exps[i].op = 0; n     } n     string _var; n     _var.assign(var); n     varIndex[var] = i; n   } n   for (int i = 0; i < m; i++) { n     int a = work(i, n, 0); n     int b = work(i, n, 1); n     if (a > b) n       ansMin[i] =  1 , ansMax[i] =  0 ; n     else if (a < b) n       ansMin[i] =  0 , ansMax[i] =  1 ; n     else n       ansMin[i] =  0 , ansMax[i] =  0 ; n   } n   puts(ansMin); n   puts(ansMax); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   map<int, vector<int>> mappu; n   cin >> n >> m; n   for (long long i = (0); i < (m); i++) { n     int a, b; n     cin >> a >> b; n     mappu[a].push_back(b); n     mappu[b].push_back(a); n   } n   for (long long i = (1); i < (n + 1); i++) { n     mappu[i].push_back(i); n     sort(mappu[i].begin(), mappu[i].end()); n   } n   for (long long i = (1); i < (n + 1); i++) { n     for (long long j = (0); j < (mappu[i].size()); j++) { n       if (mappu[i] != mappu[mappu[i][j]]) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n struct Fenwick_Tree { n   int sz, c[305]; n   void init(int n) { n     sz = n; n     for (int i = 1; i <= n; i++) c[i] = 0; n   } n   int lowbit(int x) { return x & (-x); } n   void update(int p, int v) { n     for (int i = p; i <= sz; i += lowbit(i)) c[i] = max(c[i], v); n   } n   int query(int p) { n     int res = 0; n     for (int i = p; i > 0; i -= lowbit(i)) res = max(res, c[i]); n     return res; n   } n } BIT; n int b[200005], cnt[305]; n int pre[305], suf[305]; n int main() { n   int n, T; n   scanf( %d%d , &n, &T); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (T <= 2000) { n     for (int i = 0; i < T; i++) n       for (int j = 1; j <= n; j++) b[i * n + j] = a[j]; n     int ans = 0; n     BIT.init(300); n     for (int i = 1; i <= n * T; i++) { n       int len = BIT.query(b[i]) + 1; n       BIT.update(b[i], len); n       ans = max(ans, len); n     } n     printf( %d n , ans); n   } else { n     for (int i = 1; i <= n; i++) cnt[a[i]]++; n     for (int i = 0; i < 2000; i++) n       for (int j = 1; j <= n; j++) b[i * n + j] = a[j]; n     BIT.init(300); n     for (int i = 1; i <= 1000 * n; i++) { n       pre[b[i]] = BIT.query(b[i]) + 1; n       BIT.update(b[i], pre[b[i]]); n     } n     BIT.init(300); n     for (int i = 2000 * n; i > 1000 * n; i--) { n       suf[b[i]] = BIT.query(301 - b[i]) + 1; n       BIT.update(301 - b[i], suf[b[i]]); n     } n     int ans = 0; n     for (int i = 1; i <= 300; i++) n       ans = max(ans, pre[i] + suf[i] + cnt[i] * (T - 2000)); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1000011; n const long long INF = (1 << 29) + 123; n const long long MOD = 1000000007; n const long double PI = 4 * atan((long double)1); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, w; n   cin >> n >> w; n   int l[n]; n   int x; n   long long ans[w], pf[w + 1]; n   for (int i = 0; i < w + 1; i++) ans[i] = pf[i] = 0; n   for (int i = 0; i < n; i++) { n     cin >> l[i]; n     deque<pair<long long, int>> store; n     int window = w - l[i] + 1; n     int best = -INF; n     for (int j = 0; j < w; j++) { n       if (j < l[i]) { n         cin >> x; n         while (!store.empty() && x >= store.back().first) store.pop_back(); n         store.push_back(make_pair(x, j)); n         best = max(best, x); n       } else if (j == l[i] && j < window) { n         j = window; n         if (best >= 0) { n           pf[l[i]] += best; n           pf[window] -= best; n         } else { n           if (window - 1 < w - window + 1) { n             pf[window - 1] += best; n             pf[w - window + 1] -= best; n           } n         } n       } n       if (j == w) break; n       while (store.front().second <= j - window) store.pop_front(); n       if (store.front().first > 0 || (j >= window - 1 && j <= w - window)) { n         ans[j] += 1LL * store.front().first; n       } n     } n   } n   long long total = 0; n   for (int i = 0; i < w; i++) { n     total += pf[i]; n     cout << ans[i] + total <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse4 ) n using namespace std; n constexpr long long mod = 1000000007; n const long long INF = mod * mod; n const double eps = 1e-12; n const double pi = acosl(-1.0); n long long mod_pow(long long x, long long n, long long m = mod) { n   long long res = 1; n   while (n) { n     if (n & 1) res = res * x % m; n     x = x * x % m; n     n >>= 1; n   } n   return res; n } n struct modint { n   long long n; n   modint() : n(0) { ; } n   modint(long long m) : n(m) { n     if (n >= mod) n       n %= mod; n     else if (n < 0) n       n = (n % mod + mod) % mod; n   } n   operator int() { return n; } n }; n bool operator==(modint a, modint b) { return a.n == b.n; } n modint operator+=(modint& a, modint b) { n   a.n += b.n; n   if (a.n >= mod) a.n -= mod; n   return a; n } n modint operator-=(modint& a, modint b) { n   a.n -= b.n; n   if (a.n < 0) a.n += mod; n   return a; n } n modint operator*=(modint& a, modint b) { n   a.n = ((long long)a.n * b.n) % mod; n   return a; n } n modint operator+(modint a, modint b) { return a += b; } n modint operator-(modint a, modint b) { return a -= b; } n modint operator*(modint a, modint b) { return a *= b; } n modint operator^(modint a, long long n) { n   if (n == 0) return modint(1); n   modint res = (a * a) ^ (n / 2); n   if (n % 2) res = res * a; n   return res; n } n long long inv(long long a, long long p) { n   return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); n } n modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } n const int max_n = 1 << 10; n modint fact[max_n], factinv[max_n]; n void init_f() { n   fact[0] = modint(1); n   for (int i = 0; i < max_n - 1; i++) { n     fact[i + 1] = fact[i] * modint(i + 1); n   } n   factinv[max_n - 1] = modint(1) / fact[max_n - 1]; n   for (int i = max_n - 2; i >= 0; i--) { n     factinv[i] = factinv[i + 1] * modint(i + 1); n   } n } n modint comb(int a, int b) { n   if (a < 0 || b < 0 || a < b) return 0; n   return fact[a] * factinv[b] * factinv[a - b]; n } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   while (b) { n     long long r = a % b; n     a = b; n     b = r; n   } n   return a; n } n void solve() { n   long long a, b; n   cin >> a >> b; n   modint ans = 0; n   for (long long i = 1; i < b; i++) { n     long long num = a; n     ans += num * i; n     modint cr = num * (num + 1) / 2; n     cr *= i; n     ans += cr * (modint)b; n   } n   cout << ans <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, t, n, m, f[100010], ans, trl; n struct node { n   int next, ar, flow, b; n } k[100010 * 20]; n int first[100010], dis[100010], len; n int X[100010], y[100010], v[100010], e[100010]; n int t1[100010], t2[100010], tl1, tl2, ANS; n void add(int a, int b, int t) { n   len++; n   k[len].ar = b; n   k[len].next = first[a]; n   first[a] = len; n   k[len].flow = t; n } n int head, tail, d[100010]; n bool bfs() { n   head = 0, tail = 1; n   d[0] = a; n   for (register int i = 1; i <= n; i++) dis[i] = 0; n   dis[a] = 1; n   while (head < tail) { n     t = d[head]; n     head++; n     for (register int i = first[t]; i; i = k[i].next) { n       if (dis[k[i].ar] == 0 && k[i].flow > 0) { n         dis[k[i].ar] = dis[t] + 1; n         if (k[i].ar == b) return true; n         d[tail] = k[i].ar; n         tail++; n       } n     } n   } n   return false; n } n int dfs(int xx, int flow) { n   if (xx == b) return flow; n   if (flow == 0) return 0; n   if (dis[xx] >= dis[b]) return 0; n   int h, s = 0; n   for (register int i = first[xx]; i > 1; i = k[i].next) { n     if (flow == 0) { n       dis[xx] = 0; n       break; n     } n     if (dis[k[i].ar] == dis[xx] + 1 && k[i].flow > 0) { n       h = dfs(k[i].ar, min(flow, k[i].flow)); n       s += h; n       flow -= h; n       k[i].flow -= h; n       k[i ^ 1].flow += h; n       if (h == 0) dis[k[i].ar] = 0; n     } n   } n   return s; n } n void dinic() { n   for (register int i = 1; i <= n; i++) first[i] = 0; n   len = 1, ans = 0; n   for (register int i = 1; i <= m; i++) n     add(X[i], y[i], f[i]), add(y[i], X[i], f[i]); n   while (bfs()) ans += dfs(a, 0x7ffffff); n } n struct gGomory_Hu_Tree { n   node tr[100010 * 2], trt[2][100010]; n   int trl, first[100010]; n   void adtr(int a, int b, int t) { n     trl++; n     tr[trl].ar = b; n     tr[trl].b = a; n     tr[trl].flow = t; n     tr[trl].next = first[a]; n     first[a] = trl; n     ANS += t; n   } n   void dfs(int x, int y, int &w) { n     for (int i = first[x]; i; i = tr[i].next) { n       if (tr[i].flow >= 0 && tr[i].ar != y) { n         if (tr[i].flow < tr[w].flow) w = i; n         dfs(tr[i].ar, x, w); n       } n     } n   } n   void work(int x) { n     int t = 1; n     dfs(x, 0, t); n     if (t == 1) { n       printf( %d  , x); n       return; n     } n     tr[t].flow = tr[t ^ 1].flow = -1; n     work(tr[t].ar); n     work(tr[t].b); n   } n } ght; n void build(int l, int r) { n   if (l >= r) return; n   a = v[l]; n   b = v[l + 1]; n   dinic(); n   ght.adtr(b, a, ans); n   ght.adtr(a, b, ans); n   int tl1 = 0, tl2 = 0; n   for (register int i = l; i <= r; i++) { n     if (dis[v[i]]) n       tl1++, t1[tl1] = v[i], dis[v[i]] = 0; n     else n       tl2++, t2[tl2] = v[i]; n   } n   for (register int i = 1; i <= tl1; i++) v[i + l - 1] = t1[i]; n   for (register int i = 1; i <= tl2; i++) v[l + tl1 + i - 1] = t2[i]; n   build(l, tl1 + l - 1); n   build(l + tl1, r); n } n int main() { n   int T; n   len = 1; n   scanf( %d%d , &n, &m); n   for (register int i = 1; i <= m; i++) { n     scanf( %d%d%d , &a, &b, &t); n     X[i] = a, y[i] = b, f[i] = t; n   } n   ght.trl = 1; n   ght.tr[1].flow = 0x3f3f3f3f; n   for (register int i = 1; i <= n; i++) v[i] = i; n   build(1, n); n   printf( %d n , ANS / 2); n   ght.work(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10005], cnt[305], dp[10005], inf = 1e8; n int main() { n   int n, t; n   scanf( %d %d , &n, &t); n   int max_num = 0, len = n * min(n / 2, t); n   if (len <= 0) len = n; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n     max_num = max(max_num, ++cnt[a[i]]); n   } n   for (int i = n + 1; i <= len; ++i) { n     a[i] = a[i - n]; n   } n   int ans = 0; n   for (int i = 1; i <= len; ++i) { n     dp[i] = 1; n     for (int j = i - 1; j >= max(i - n, 1); --j) { n       if (a[i] >= a[j]) { n         dp[i] = max(dp[i], dp[j] + 1); n       } n     } n     ans = max(ans, dp[i]); n   } n   if (n / 2 == 0) n++; n   if (t <= (n / 2)) { n     printf( %d n , ans); n   } else { n     printf( %d n , ans + max_num * (t - (n / 2))); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1e5 * 5; n const int INF = 1e9 + 7; n const int N = 5000; n using namespace std; n int n, m, h[N], c[N], cnt; n char a[N][N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       cin >> a[i][j]; n       if (a[i][j] ==  * ) ++h[i], ++c[j], ++cnt; n     } n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (h[i] + c[j] - (a[i][j] ==  * ) >= cnt) { n         cout <<  YES  <<   n ; n         cout << i <<     << j; n         return 0; n       } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N_MAX = 1e9, MAX = 4000001, MOD = 1000000007; n int t, n, k, ans[200001]; n vector<int> cnt[200001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n >> k; n     int sum = 0; n     fill(ans, ans + n + 1, 0); n     for (int i = 0; i < n + 1; i++) cnt[i].clear(); n     for (int i = 0; i < n; i++) { n       int a; n       cin >> a; n       if (cnt[a].size() < k) cnt[a].push_back(i); n     } n     int curr = 1, aa = 0; n     for (int i = 1; i <= n; i++) { n       aa += cnt[i].size(); n     } n     aa -= aa % k; n     for (int i = 1; i <= n; i++) { n       int s = min(k, (int)cnt[i].size()); n       for (int j = 0; j < s; j++) { n         ans[cnt[i][j]] = curr; n         curr = curr % k + 1; n         aa--; n         if (aa == 0) break; n       } n       if (aa == 0) break; n     } n     for (int i = 0; i < n; i++) { n       cout << ans[i] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     string aba; n     cin >> aba; n     int ab = 0; n     int ba = 0; n     if (aba[0] != aba[aba.size() - 1]) { n       aba[0] = aba[aba.size() - 1]; n       for (int i = 0; i < aba.size() - 1; i++) { n         if (aba[i] ==  a  && aba[i + 1] ==  b ) { n           ab += 1; n         } else if (aba[i] ==  b  && aba[i + 1] ==  a ) { n           ba += 1; n         } n       } n       if (ab == ba) { n         for (char c : aba) { n           cout << c; n         } n       } else { n         aba[aba.size() - 1] = aba[0]; n         for (char c : aba) { n           cout << c; n         } n       } n     } else { n       for (char c : aba) { n         cout << c; n       } n     } n     cout <<   n ; n   } n } n