#include <bits/stdc++.h> n using namespace std; n char A[2005][2005], B[2005][2005]; n int last_valid; n pair<int, int> lb[26], ub[26]; n bool ans; n void _clear(int n, int m) { n   for (int i = 0; i < 26; ++i) { n     lb[i] = make_pair(INT_MAX, INT_MAX); n     ub[i] = make_pair(INT_MIN, INT_MIN); n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       B[i][j] =  . ; n     } n   } n   ans = true; n   last_valid = -1; n } n void _populate(int n, int m) { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (A[i][j] ==  . ) continue; n       char c = A[i][j] -  a ; n       lb[c].first = min(lb[c].first, i); n       lb[c].second = min(lb[c].second, j); n       ub[c].first = max(ub[c].first, i); n       ub[c].second = max(ub[c].second, j); n     } n   } n } n void _regenerate() { n   for (int i = 0; i < 26; ++i) { n     if (lb[i].first != INT_MAX) { n       last_valid = i; n       if (lb[i].first == ub[i].first) { n         int x = lb[i].first; n         for (int j = lb[i].second; j <= ub[i].second; ++j) B[x][j] = i +  a ; n       } else if (lb[i].second == ub[i].second) { n         int x = lb[i].second; n         for (int j = lb[i].first; j <= ub[i].first; ++j) B[j][x] = i +  a ; n       } else n         ans = false; n     } n   } n } n void _compare(int n, int m) { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (A[i][j] != B[i][j]) ans = false; n     } n   } n } n void _print() { n   if (!ans) { n     cout <<  NO  << endl; n     return; n   } n   cout <<  YES  << endl; n   cout << last_valid + 1 << endl; n   for (int i = 0; i <= last_valid; ++i) { n     if (lb[i].first != INT_MAX) { n       cout << lb[i].first + 1 <<     << lb[i].second + 1 <<     n            << ub[i].first + 1 <<     << ub[i].second + 1 << endl; n     } else { n       int x = last_valid; n       cout << lb[x].first + 1 <<     << lb[x].second + 1 <<     n            << ub[x].first + 1 <<     << ub[x].second + 1 << endl; n     } n   } n } n void _debug(int n, int m) { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) cout << A[i][j] <<    ; n     cout << endl; n   } n   cout << endl; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) cout << B[i][j] <<    ; n     cout << endl; n   } n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   int t, n, m, i, j, k, x, y, z, a, b, c; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     for (i = 0; i < n; ++i) cin >> A[i]; n     _clear(n, m); n     _populate(n, m); n     _regenerate(); n     _compare(n, m); n     _print(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n long long dp[200011]; n const long long Mod = 1e9 + 7; n long long f[200011][3]; n long long power(long long x, long long n) { n   if (n < 0) return 0; n   if (n == 0) return 1; n   if (n % 2 == 0) return power(x * x % Mod, n / 2); n   return x * power(x * x % Mod, n / 2) % Mod; n } n int main() { n   cin >> n; n   cin >> s; n   s =  .  + s; n   int c = 0; n   for (int i = n; i >= 1; i--) { n     if (s[i] ==  ? ) c++; n   } n   long long cnt1 = 0; n   long long cnt2 = 0; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  b ) { n       f[i][0] += cnt1; n       f[i][1] += cnt2; n     } n     if (s[i] ==  ? ) { n       f[i][1] += cnt1; n       f[i][2] += cnt2; n     } n     if (s[i] ==  a ) { n       cnt1++; n     } n     if (s[i] ==  ? ) cnt2++; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= 2; j++) (f[i][j] += f[i - 1][j]) %= Mod; n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  c ) { n       ans += f[i - 1][0] * power(3, c); n       ans %= Mod; n       ans += f[i - 1][1] * power(3, c - 1); n       ans %= Mod; n       ans += f[i - 1][2] * power(3, c - 2); n       ans %= Mod; n     } n     if (s[i] ==  ? ) { n       ans += f[i - 1][0] * power(3, c - 1); n       ans %= Mod; n       ans += f[i - 1][1] * power(3, c - 2); n       ans %= Mod; n       ans += f[i - 1][2] * power(3, c - 3); n       ans %= Mod; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x[1042], y[1042]; n double eps = 0.00000001; n int dist2(int x1, int y1, int x2, int y2) { n   return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); n } n double dist(double x1, double y1, double x2, double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n int cross(int a, int b, int c) { n   return (x[a] - x[b]) * (y[c] - y[b]) - (x[c] - x[b]) * (y[a] - y[b]); n } n bool acute(int a, int b, int c) { n   if (cross(a, b, c) == 0) { n     return 0; n   } n   int dAB = dist2(x[a], y[a], x[b], y[b]); n   int dBC = dist2(x[b], y[b], x[c], y[c]); n   int dAC = dist2(x[a], y[a], x[c], y[c]); n   if (dAB >= dBC + dAC) { n     return 0; n   } n   if (dAC >= dBC + dAB) { n     return 0; n   } n   if (dBC >= dAC + dAB) { n     return 0; n   } n   return 1; n } n bool r(int a, int b, int c) { n   int dAB = dist2(x[a], y[a], x[b], y[b]); n   int dBC = dist2(x[b], y[b], x[c], y[c]); n   int dAC = dist2(x[a], y[a], x[c], y[c]); n   if (dAB == dBC + dAC) { n     return 1; n   } n   if (dAC == dBC + dAB) { n     return 1; n   } n   if (dBC == dAC + dAB) { n     return 1; n   } n   return 0; n } n bool covered(double cx, double cy, double t) { n   for (int i = 0; i < n; i++) { n     if (dist(cx, cy, x[i], y[i]) < t) { n       return 1; n     } n   } n   return 0; n } n pair<double, double> getCirC(int a, int b, int c) { n   int dx1 = x[a] - x[b], dx2 = x[b] - x[c], dx3 = x[c] - x[a]; n   int dy1 = y[a] - y[b], dy2 = y[b] - y[c], dy3 = y[c] - y[a]; n   double xc1 = (x[a] + x[b]) / 2., xc2 = (x[b] + x[c]) / 2., n          xc3 = (x[c] + x[a]) / 2.; n   double yc1 = (y[a] + y[b]) / 2., yc2 = (y[b] + y[c]) / 2., n          yc3 = (y[c] + y[a]) / 2.; n   if (dy1 == 0) { n     swap(dx1, dx3); n     swap(dy1, dy3); n     swap(xc1, xc3); n     swap(yc1, yc3); n   } n   if (dy2 == 0) { n     swap(dx2, dx3); n     swap(dy2, dy3); n     swap(xc2, xc3); n     swap(yc2, yc3); n   } n   double n1 = -1. * dx1 / (double)dy1, n2 = -1. * dx2 / (double)dy2; n   double x = (yc2 - yc1 + n1 * xc1 - n2 * xc2) / (n1 - n2); n   double y = n1 * (x - xc1) + yc1; n   return make_pair(x, y); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n   } n   double ans = -1; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       for (int k = j + 1; k < n; k++) { n         if (acute(i, j, k)) { n           pair<double, double> crr = getCirC(i, j, k); n           double px = crr.first, py = crr.second; n           double d = dist(px, py, x[i], y[i]); n           if (!covered(px, py, d - eps)) { n             ans = max(ans, d); n           } n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       vector<int> ids; n       ids.clear(); n       for (int k = 0; k < n; k++) { n         if (k == i || k == j) { n           continue; n         } n         if (dist2(x[i], y[i], x[k], y[k]) != dist2(x[j], y[j], x[k], y[k])) { n           continue; n         } n         if (!r(i, j, k)) { n           continue; n         } n         ids.push_back(k); n       } n       if (ids.size() < 2) { n         continue; n       } n       int k = ids[0]; n       int cc = ids[1]; n       double px = (x[i] + x[j] + x[k] + x[cc]) / 4.; n       double py = (y[i] + y[j] + y[k] + y[cc]) / 4.; n       double d = dist(x[i], y[i], px, py); n       if (!covered(px, py, d - eps)) { n         ans = max(ans, d); n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vec[100010]; n long long a[100010]; n long long s; n int n; n long long dfs(int f, int u, long long val) { n   int sz = vec[u].size(), v; n   long long ret = 0, mv = ((long long)1e18), tmp; n   if (sz == 1 && vec[u][0] == f) n     return (a[u] = ((val) < (a[u]) ? (val) : (a[u]))); n   if (u != 1) n     val = val / (sz - 1) * (sz - 1) / (sz - 1); n   else n     val = val / sz * sz / sz; n   for (int i = 0; i < sz; i++) { n     v = vec[u][i]; n     if (v == f) continue; n     tmp = dfs(u, v, val); n     mv = ((mv) < (tmp) ? (mv) : (tmp)); n   } n   while (true) { n     tmp = mv; n     for (int i = 0; i < sz; i++) { n       v = vec[u][i]; n       if (v == f) continue; n       if (a[v] > tmp) tmp = ((dfs(u, v, mv)) < (tmp) ? (dfs(u, v, mv)) : (tmp)); n     } n     if (tmp == mv) n       break; n     else n       mv = tmp; n   } n   for (int i = 0; i < sz; i++) n     if (vec[u][i] != f) ret += a[vec[u][i]]; n   return a[u] = ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , a + i), s += a[i]; n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     vec[u].push_back(v); n     vec[v].push_back(u); n   } n   long long ret = dfs(0, 1, ((long long)1e18)); n   cout << s - ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n template <class T> n inline void smin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline void smax(T &a, T b) { n   if (a < b) a = b; n } n int qf, qb, q[11111111]; n long long res[555], a[555][555], dst[555][555]; n int r[555], first[555]; n int main() { n   int N, ii, i, j, nn, u, st; n   scanf( %d , &N); n   for (i = 0; i < N; i++) n     for (j = 0; j < N; j++) scanf( %I64d , &dst[i][j]); n   for (i = 0; i < N; i++) { n     scanf( %d , r + i); n     r[i]--; n   } n   nn = 0; n   for (ii = N - 1; ii >= 0; ii--) { n     st = r[ii]; n     for (i = 0; i < nn; i++) { n       for (j = 0; j < nn; j++) { n         smin(dst[st][first[j]], dst[st][first[i]] + dst[first[i]][first[j]]); n         smin(dst[first[j]][st], dst[first[j]][first[i]] + dst[first[i]][st]); n       } n     } n     for (i = 0; i < nn; i++) n       for (j = 0; j < nn; j++) n         smin(dst[first[i]][first[j]], dst[first[i]][st] + dst[st][first[j]]); n     first[nn++] = st; n     res[ii] = 0; n     for (i = 0; i < nn; i++) n       for (j = 0; j < nn; j++) res[ii] += dst[first[i]][first[j]]; n   } n   for (i = 0; i < N; i++) { n     if (i) printf(   ); n     printf( %I64d , res[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400005; n int n, q, K, m, i, j, k, h[N], t[N], num, fa[N], s[N], id[N], size[N], son[N], n     tp[N], dep[N], head[N], adj[N * 2], nxt[N * 2], p[N], cnt, stk[N], top, n     g[N], Head[N], Adj[N * 2], Nxt[N * 2], l, f[N]; n void dfs(int x, int dad) { n   size[x] = 1; n   for (int y = head[x]; y; y = nxt[y]) n     if (adj[y] != dad) { n       dep[adj[y]] = dep[x] + 1; n       fa[adj[y]] = x; n       dfs(adj[y], x); n       size[x] += size[adj[y]]; n       if (!son[x] || size[adj[y]] > size[son[x]]) son[x] = adj[y]; n     } n } n void Dfs(int x, int dad) { n   id[x] = ++i; n   if (son[x]) { n     tp[son[x]] = tp[x]; n     Dfs(son[x], x); n   } n   for (int y = head[x]; y; y = nxt[y]) n     if (adj[y] != dad && adj[y] != son[x]) { n       tp[adj[y]] = adj[y]; n       Dfs(adj[y], x); n     } n } n int lca(int a, int b) { n   while (tp[a] != tp[b]) { n     if (dep[tp[a]] < dep[tp[b]]) swap(a, b); n     a = fa[tp[a]]; n   } n   return dep[a] < dep[b] ? a : b; n } n int dis(int a, int b) { return dep[a] + dep[b] - 2 * dep[lca(a, b)]; } n bool cmp(int a, int b) { return id[a] < id[b]; } n void addedge(int u, int v) { n   Adj[++l] = v; n   Nxt[l] = Head[u]; n   Head[u] = l; n   Adj[++l] = u; n   Nxt[l] = Head[v]; n   Head[v] = l; n } n void dzx(int x, int dad) { n   if (s[x]) n     f[x] = x; n   else n     f[x] = 0; n   for (int y = Head[x], a, b; y; y = Nxt[y]) n     if (Adj[y] != dad) { n       dzx(Adj[y], x); n       if (f[Adj[y]] && (!f[x] || n                         1ll * (a = dis(f[Adj[y]], x) - 1) / s[f[Adj[y]]] < n                             1ll * (b = dis(f[x], x) - 1) / s[f[x]] || n                         1ll * (a = dis(f[Adj[y]], x) - 1) / s[f[Adj[y]]] == n                                 1ll * (b = dis(f[x], x) - 1) / s[f[x]] && n                             h[f[Adj[y]]] < h[f[x]])) n         f[x] = f[Adj[y]]; n     } n   if (s[x]) f[x] = x; n } n void phh(int x, int dad, int u) { n   int a, b; n   if (u) { n     if (u && (!f[x] || n               1ll * (a = dis(u, x) - 1) / s[u] < n                   1ll * (b = dis(f[x], x) - 1) / s[f[x]] || n               1ll * (a = dis(u, x) - 1) / s[u] == n                       1ll * (b = dis(f[x], x) - 1) / s[f[x]] && n                   h[u] < h[f[x]])) n       f[x] = u; n   } n   if (s[x]) f[x] = x; n   for (int y = Head[x]; y; y = Nxt[y]) n     if (Adj[y] != dad) phh(Adj[y], x, f[x]); n   Head[x] = 0; n } n int main() { n   scanf( %d , &n); n   for (i = 1; i < n; ++i) { n     scanf( %d%d , &j, &k); n     adj[i * 2 - 1] = k; n     nxt[i * 2 - 1] = head[j]; n     head[j] = i * 2 - 1; n     adj[i * 2] = j; n     nxt[i * 2] = head[k]; n     head[k] = i * 2; n   } n   dfs(1, -1); n   tp[1] = 1; n   i = 0; n   Dfs(1, -1); n   scanf( %d , &q); n   while (q--) { n     if (n == 10) { n       for (i = 1; i <= n; ++i) Head[i] = h[i] = s[i] = g[i] = t[i] = p[i] = 0; n     } n     scanf( %d%d , &K, &m); n     num = 0; n     for (i = 1; i <= K; ++i) { n       scanf( %d%d , &j, &k); n       s[j] = k; n       h[j] = i; n       t[++num] = j; n     } n     for (i = 1; i <= m; ++i) { n       scanf( %d , &j); n       g[i] = t[++num] = j; n     } n     t[++num] = 1; n     sort(t + 1, t + 1 + num, cmp); n     cnt = 0; n     for (i = 1; i <= num; i = j) { n       for (j = i; j <= num && t[i] == t[j]; ++j) n         ; n       p[++cnt] = t[i]; n     } n     if (cnt == 1) { n       puts( 1 ); n       continue; n     } n     l = 0; n     stk[1] = p[1], stk[2] = p[2]; n     top = 2; n     for (i = 3; i <= cnt; ++i) { n       while (top > 1 && dep[lca(stk[top], p[i])] <= dep[stk[top - 1]]) { n         addedge(stk[top - 1], stk[top]); n         --top; n       } n       if ((j = lca(p[i], stk[top])) == stk[top]) n         stk[++top] = p[i]; n       else { n         addedge(j, stk[top]); n         stk[top] = j; n         stk[++top] = p[i]; n       } n     } n     while (top > 1) { n       addedge(stk[top - 1], stk[top]); n       --top; n     } n     dzx(1, -1); n     phh(1, -1, 0); n     for (i = 1; i <= m; ++i) printf( %d  , h[f[g[i]]]); n     puts(  ); n     for (i = 1; i <= cnt; ++i) s[p[i]] = 0; n   } n   return 0; n } n


#include<bits/stdc++.h> n #define pairs pair<int,int> n #define loop(i,a,n) for(int i=a;i<n;i++) n #define m_p make_pair n #define fi first n #define se second n #define pb push_back n #define ll long long n #define all(v) v.begin(),v.end() n using namespace std; n //vector<ll> siz; n /*ll indes(ll i,ll right){ n     ll val=siz[i]; n     ll left=0; n     ll ans=-1; n     while(left<=right){ n         ll mid=(left+right)/2; n         if(2*siz[mid]<=val){ n             ans=mid; n             left=mid+1; n         } n         else n         { n             right=mid-1; n         } n          n     } n     return ans;  n }*/ n map<pairs,bool> mp; n  n void solve(){ n ll n,k; n cin>>n>>k; n vector<ll> vec(n); n vector<pairs> range(n); n loop(i,0,n){ n     cin>>vec[i]; n } n range[0].fi=vec[0]; n range[0].se=vec[0]; n range[n-1].fi=vec[n-1]; n range[n-1].se=vec[n-1]; n for(ll i=1;i<n-1;i++){ n   ll a=range[i-1].fi; n   ll b=range[i-1].se; n   ll c= b+k-1; n   ll d=a-(k-1); n   range[i].fi=max(d,vec[i]); n   range[i].se=min(c,vec[i]+(k-1)); n } n  n loop(i,1,n){ n   ll a=range[i-1].fi; n   ll b=range[i-1].se; n   ll c=range[i].fi; n   ll d=range[i].se; n  n   if(a-k>=d||b+k<=c){ n       cout<< NO ; n       return; n   } n      n } n cout<< YES ; n return; n } n int main(){ n     int test=1; n     cin>>test; n     while(test--){ n         solve(); n         cout<<endl; n     } n }


#include <bits/stdc++.h> n using namespace std; n const int Mod = 1e9 + 7; n int main() { n   long long N, M, ans = 0, Last = 0; n   scanf( %I64d%I64d , &N, &M); n   long long Min = min(N, M); n   for (long long i = 1, j; i <= Min; i = j + 1) { n     j = min(N / (N / i), M); n     long long tmp; n     if (j & 1LL) n       tmp = ((j + 1) / 2 % Mod) * (j % Mod) % Mod; n     else n       tmp = (j / 2 % Mod) * ((j + 1) % Mod) % Mod; n     ans = n         (ans + (N / i) % Mod * (((tmp - Last) % Mod + Mod) % Mod) % Mod) % Mod; n     Last = tmp; n   } n   ans = (((N % Mod) * (M % Mod) - ans) % Mod + Mod) % Mod; n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2E5 + 10, mod = 1E9 + 7, M = 1E6 + 10, lim = 1E6; n int n, m, a[N], s[N], ans[N]; n struct node { n   int l, r, num; n } p[N]; n vector<int> f[M]; n int pos[M]; n inline int cmp(node x, node y) { return x.r < y.r; } n struct sgtree { n   int l, r, sum; n } t[N << 2]; n inline int ksm(int x, int y) { n   return y == 1 n              ? x n              : 1ll * ksm(1ll * x * x % mod, y >> 1) * ((y & 1) ? x : 1) % mod; n } n inline void pl(int l = 1, int r = n, int x = 1) { n   t[x] = (sgtree){l, r, 1}; n   if (l == r) return; n   int mid = l + r >> 1; n   pl(l, mid, x << 1); n   pl(mid + 1, r, x << 1 | 1); n } n inline void modify(int l, int r, int v, int x = 1) { n   if (l == t[x].l && r == t[x].r) { n     t[x].sum = 1ll * t[x].sum * v % mod; n     return; n   } n   int mid = t[x].l + t[x].r >> 1; n   if (r <= mid) n     modify(l, r, v, x << 1); n   else if (l > mid) n     modify(l, r, v, x << 1 | 1); n   else n     modify(l, mid, v, x << 1), modify(mid + 1, r, v, x << 1 | 1); n } n inline void insert(int v, int x) { n   for (int i = 0; i < f[v].size(); i++) { n     modify(pos[f[v][i]] + 1, x, n            1ll * ksm(f[v][i], mod - 2) * (f[v][i] - 1) % mod); n     pos[f[v][i]] = x; n   } n } n inline int query(int l, int x = 1) { n   if (t[x].l == t[x].r) return t[x].sum; n   int mid = t[x].l + t[x].r >> 1; n   return 1ll * t[x].sum * (l <= mid ? query(l, x << 1) : query(l, x << 1 | 1)) % n          mod; n } n inline int sol(int l, int r) { n   return 1ll * s[r] * ksm(s[l - 1], mod - 2) % mod * query(l) % mod; n } n int main() { n   scanf( %d , &n); n   s[0] = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     s[i] = 1ll * s[i - 1] * a[i] % mod; n   } n   scanf( %d , &m); n   pl(); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &p[i].l, &p[i].r); n     p[i].num = i; n   } n   sort(p + 1, p + 1 + m, cmp); n   int now = 1; n   for (int i = 2; i <= lim; i++) n     if (!f[i].size()) n       for (int j = 1; i * j <= lim; j++) f[i * j].push_back(i); n   for (int i = 1; i <= n; i++) { n     insert(a[i], i); n     while (now <= m && p[now].r == i) { n       ans[p[now].num] = sol(p[now].l, p[now].r); n       now++; n     } n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fact[1000010]; n long long m = 1e9 + 7; n long long me(long long a, long long b, long long m) { n   long long ans = 1; n   while (b > 0) { n     if (b % 2 == 1) { n       ans = (ans % m * a % m) % m; n     } n     a = (a * a) % m; n     b /= 2; n   } n   return ans % m; n } n bool check(long long x, long long a, long long b) { n   while (x > 0) { n     int dg = x % 10; n     if (dg != a && dg != b) return false; n     x /= 10; n   } n   return true; n } n long long ncr(long long n, long long r) { n   long long fn = fact[n]; n   long long tf = fact[r] * fact[n - r]; n   tf = tf % m; n   tf = me(tf, m - 2, m); n   fn = (tf % m * fn % m) % m; n   return fn % m; n } n int main() { n   fact[0] = 1; n   for (long long i = 1; i <= 1000001; i++) { n     fact[i] = (fact[i - 1] * i) % m; n   } n   int n, a, b; n   cin >> a >> b >> n; n   if (a == b) { n     if (check(n * a, a, b)) { n       cout <<  1 n ; n     } else n       cout <<  0 n ; n     return 0; n   } n   long long ans = 0; n   for (long long i = 0; i <= n; i++) { n     long long tmp = i * a + (n - i) * b; n     if (check(tmp, a, b)) { n       ans += ncr(n, i); n       ans %= m; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (cin >> n) { n     string s; n     cin >> s; n     string res =   ; n     for (int i = 0; i < n; i++) { n       if (s[i] <=  1 ) n         continue; n       else { n         if (s[i] ==  4 ) n           res +=  3 , res +=  2 , res +=  2 ; n         else { n           if (s[i] ==  6 ) n             res +=  5 , res +=  3 ; n           else { n             if (s[i] ==  8 ) n               res +=  7 , res +=  2 , res +=  2 , res +=  2 ; n             else { n               if (s[i] ==  9 ) n                 res +=  3 , res +=  3 , res +=  2 , res +=  7 ; n               else n                 res += s[i]; n             } n           } n         } n       } n     } n     sort(res.begin(), res.end()); n     reverse(res.begin(), res.end()); n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int i, j, n, ans = 0, k1, k2; n     cin >> n; n     long long int arr[n]; n     vector<vector<long long int> > vec(26); n     for (i = 0; i < n; i++) { n       cin >> arr[i]; n       vec[arr[i] - 1].push_back(i); n     } n     long long int dp1[n][26], dp2[n][26]; n     for (i = 0; i < n; i++) n       for (j = 0; j < 26; j++) { n         dp1[i][j] = 0; n         dp2[i][j] = 0; n       } n     dp1[0][arr[0] - 1]++; n     for (i = 1; i < n; i++) { n       for (j = 0; j < 26; j++) n         if (arr[i] == j + 1) n           dp1[i][j] += dp1[i - 1][j] + 1; n         else n           dp1[i][j] += dp1[i - 1][j]; n     } n     dp2[n - 1][arr[n - 1] - 1]++; n     for (i = n - 2; i >= 0; i--) { n       for (j = 0; j < 26; j++) n         if (arr[i] == j + 1) n           dp2[i][j] += dp2[i + 1][j] + 1; n         else n           dp2[i][j] += dp2[i + 1][j]; n     } n     for (i = 0; i < 26; i++) { n       long long int dd = vec[i].size(); n       ans = max(ans, dd); n     } n     for (i = 0; i < 26; i++) { n       for (k1 = 0; k1 < vec[i].size(); k1++) { n         for (k2 = k1; k2 < vec[i].size(); k2++) { n           long long int d1 = vec[i][k1], d2 = vec[i][k2]; n           for (j = 0; j < 26; j++) n             if (i != j) n               ans = max(ans, k2 - k1 + 1 + 2 * min(dp1[d1][j], dp2[d2][j])); n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 500005; n long long a[MAX], pre[MAX], suf[MAX], t; n int main() { n   int ans = -1, pos = 0, n; n   scanf( %d , &n); n   for (register int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     pre[i] = pre[i - 1] | a[i]; n   } n   for (register int i = n; i >= 1; i--) suf[i] = suf[i + 1] | a[i]; n   for (register int i = 1; i <= n; i++) { n     t = pre[i - 1] | suf[i + 1]; n     if ((a[i] | t) - t > ans) { n       ans = (a[i] | t) - t; n       pos = i; n     } n   } n   printf( %lld  , a[pos]); n   for (register int i = 1; i <= n; i++) n     if (i != pos) printf( %lld  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<pair<int, string> > all[10010]; n int main() { n   int n, m; n   cin >> n >> m; n   string name; n   int reg, sc; n   for (int i = 0; i < n; i++) { n     cin >> name >> reg >> sc; n     all[reg].push(make_pair(sc, name)); n   } n   for (int i = 1; i <= m; i++) { n     int tt[4]; n     int tool = 0; n     pair<int, string> p[4]; n     while (all[i].size()) { n       p[tool] = all[i].top(); n       all[i].pop(); n       tt[tool++] = p[tool].first; n       if (tool == 3) break; n     } n     if (tool == 2) { n       cout << p[0].second <<     << p[1].second << endl; n     } else { n       if (p[0].first == p[2].first || p[1].first == p[2].first) { n         cout <<  ?  << endl; n       } else { n         cout << p[0].second <<     << p[1].second << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n = 1009; n int LG2(int N) { n   int ans = 0; n   while (N) { n     N >>= 1; n     ans++; n   } n   return ans - 1; n } n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int n; n   cin >> n; n   map<string, int> M; n   int o = 1; n   vector<long long> v(n); n   vector<string> vv(n); n   int ans = 0; n   for (int i = 0; i < (n); i++) { n     string s; n     cin >> s; n     ans += s.length(); n     vv[i] = s; n     if (M[s]) { n       v[i] = M[s]; n     } else { n       M[s] = o; n       v[i] = o; n       o++; n     } n   } n   ans += n - 1; n   int L = ans; n   for (int f = 0; f < n; f++) { n     int len = 0; n     vector<long long> p; n     for (int l = f; l < n; l++) { n       len += vv[l].length(); n       if (l > f) len++; n       p.push_back(v[l]); n       int pL = l - f + 1; n       int cntP = 0; n       for (int i = l + 1; i < n; i++) { n         int o = i + pL - 1; n         if (o > n) continue; n         int c = 0; n         bool b = 1; n         for (int j = i; j <= o; j++) { n           if (v[j] == p[c]) { n           } else { n             b = 0; n             break; n           } n           c++; n         } n         if (b) { n           cntP++; n           i = o; n         } n       } n       if (!cntP) continue; n       int rB = (len - pL) * (cntP + 1); n       ans = min(ans, L - rB); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define INF 1000000009 n  n int get(int i){ n     cout <<  ?   << i+1 << endl; n     cout.flush(); n     int a; cin >> a; n     int b; n     if(i == 0) b = INF; n     else{ n         cout <<  ?   << i << endl; n         cout.flush(); n         cin >> b; n     } n     return b-a; n } n  n int main(){ n     // freopen( input.txt ,  r , stdin); n     // freopen( output.txt ,  w , stdout); n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     int n; cin >> n; n     if(n == 1){ n         cout <<  !   << 1 << endl; n         cout.flush(); n         return 0; n     } n     if(get(1) < 0){ n         cout <<  !   << 1 << endl; n         cout.flush(); n         return 0; n     } n     if(get(n-1) > 0){ n         cout <<  !   << n << endl; n         cout.flush(); n         return 0; n     } n     int lo = 0, hi = n-1; n     while(lo < hi){ n         int mid = lo+(hi-lo)/2; n         if(get(mid) > 0) lo = mid+1; n         else hi = mid; n     } n     cout <<  !   << lo << endl; n     cout.flush(); n }


#include <bits/stdc++.h> n using namespace std; n int N, M; n int timew, cnt; n stack<int> si; n int low[2000005], dep[2000005], siz[2000005], dfn[2000005], nex[2000005], n     las[2000005], Arrive[2000005], st[2000005], cnt1; n bool isin[2000005]; n void jt(int xx, int yy) { n   cnt++; n   nex[cnt] = las[xx]; n   las[xx] = cnt; n   Arrive[cnt] = yy; n } n void tarjan(int noww) { n   timew++; n   low[noww] = dfn[noww] = timew; n   si.push(noww); n   isin[noww] = true; n   for (int i = las[noww]; i; i = nex[i]) { n     if (!dfn[Arrive[i]]) { n       tarjan(Arrive[i]); n       low[noww] = min(low[noww], low[Arrive[i]]); n     } else if (isin[Arrive[i]]) n       low[noww] = min(low[noww], dfn[Arrive[i]]); n   } n   if (low[noww] == dfn[noww]) { n     cnt1++; n     int k = -1; n     while (k != noww) { n       k = si.top(); n       si.pop(); n       isin[k] = false; n       st[k] = cnt1; n       siz[cnt1]++; n     } n   } n } n void Clear() { n   cnt = 0; n   cnt1 = 0; n   timew = 0; n   for (int i = 1; i <= N; i++) n     las[i] = dep[i] = st[i] = siz[i] = dfn[i] = low[i] = dep[i] = 0; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &N, &M); n     Clear(); n     for (int i = 1; i <= M; i++) { n       int u, v; n       scanf( %d%d , &u, &v); n       if (u != v) jt(u, v); n     } n     for (int i = 1; i <= N; i++) n       if (!dfn[i]) tarjan(i); n     if (siz[st[1]] == N) { n       puts( No ); n       continue; n     } n     for (int i = 1; i <= N; i++) n       for (int j = las[i]; j; j = nex[j]) n         if (st[i] != st[Arrive[j]]) dep[st[i]]++; n     bool flag = false; n     for (int i = 1; i <= cnt1; i++) { n       if (!dep[i] && siz[i]) { n         flag = true; n         puts( Yes ); n         vector<int> ans[2]; n         for (int j = 1; j <= N; j++) ans[st[j] == i].push_back(j); n         printf( %d %d n , ans[1].size(), ans[0].size()); n         for (auto &i : ans[1]) printf( %d  , i); n         cout <<   n ; n         for (auto &i : ans[0]) printf( %d  , i); n         cout <<   n ; n         break; n       } n     } n     if (!flag) puts( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n const int inf = 1e9 + 100; n int a[N + 100], b[N + 100]; n vector<pair<int, int> > mon[N + 100]; n int tree[N * 4 + 100], lazy[N * 4 + 100]; n void build(int v, int tl, int tr) { n   if (tl == tr) { n     tree[v] = -b[tl + 1]; n     return; n   } n   int tm = (tl + tr) / 2; n   build(v * 2, tl, tm); n   build(v * 2 + 1, tm + 1, tr); n   tree[v] = max(tree[v * 2], tree[v * 2 + 1]); n } n void push(int v) { n   tree[v * 2] += lazy[v]; n   tree[v * 2 + 1] += lazy[v]; n   lazy[v * 2] += lazy[v]; n   lazy[v * 2 + 1] += lazy[v]; n   lazy[v] = 0; n } n void update(int v, int tl, int tr, int l, int r, int addend) { n   if (l > r) { n     return; n   } n   if (tl == l && tr == r) { n     tree[v] += addend; n     lazy[v] += addend; n     return; n   } n   push(v); n   int tm = (tl + tr) / 2; n   update(v * 2, tl, tm, l, min(r, tm), addend); n   update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, addend); n   tree[v] = max(tree[v * 2], tree[v * 2 + 1]); n } n int query(int v, int tl, int tr, int l, int r) { n   if (l > r) { n     return -inf; n   } n   if (tl == l && tr == r) { n     return tree[v]; n   } n   push(v); n   int tm = (tl + tr) / 2; n   return max(query(v * 2, tl, tm, l, min(r, tm)), n              query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r)); n } n int main() { n   for (int i = 1; i <= N + 1; i++) { n     a[i] = b[i] = inf; n   } n   int n, m, p; n   scanf( %d%d%d , &n, &m, &p); n   for (int i = 1; i <= n; i++) { n     int cur, cost; n     scanf( %d%d , &cur, &cost); n     a[cur] = min(cost, a[cur]); n   } n   int mx = 0; n   for (int i = 1; i <= m; i++) { n     int cur, cost; n     scanf( %d%d , &cur, &cost); n     b[cur] = min(cost, b[cur]); n     mx = max(mx, cur); n   } n   for (int i = 1; i <= p; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     mon[x].push_back(make_pair(y, z)); n   } n   for (int i = 1; i <= N; i++) { n     sort(mon[i].begin(), mon[i].end()); n   } n   for (int i = N; i > 0; i--) { n     a[i] = min(a[i], a[i + 1]); n     b[i] = min(b[i], b[i + 1]); n   } n   build(1, 1, N); n   int ans = -a[1] - b[1]; n   int total = 0; n   for (int i = 1; i <= N; i++) { n     if (a[i + 1] == inf) { n       break; n     } n     for (auto it : mon[i]) { n       update(1, 1, N, it.first, N, it.second); n       ans = max(ans, query(1, 1, N, 1, mx - 1) - a[i + 1]); n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n char s[500][500]; n int di[4] = {-1, 1, 0, 0}; n int dj[4] = {0, 0, -1, 1}; n typedef int int2d[500][500]; n int2d p[26]; n int2d d; n struct Pos { n   int r, c; n   Pos(int r = 0, int c = 0) : r(r), c(c) {} n   void init(int r, int c) { n     this->r = r; n     this->c = c; n   } n   bool operator<(const Pos& p) const { n     int d1 = d[r][c], d2 = d[p.r][p.c]; n     return d1 < d2 || d1 == d2 && (r < p.r || r == p.r && c < p.c); n   } n } wh[27]; n int n, m; n bool isDigit(char c) { return c >=  0  && c <=  9 ; } n set<Pos> heap; n void calc(int i, int j, int2d& par) { n   memset(d, 127, sizeof(d)); n   d[i][j] = 0; n   heap.insert(Pos(i, j)); n   while (!heap.empty()) { n     Pos p = *heap.begin(); n     heap.erase(heap.begin()); n     char ch = s[p.r][p.c]; n     if (!isDigit(ch) && ch != s[i][j]) { n       continue; n     } n     int cost = (isDigit(ch) ? ch -  0  : 1) + d[p.r][p.c]; n     for (int dir = 0; dir < 4; dir++) { n       int r = p.r + di[dir], c = p.c + dj[dir]; n       if (r >= 0 && c >= 0 && r < n && c < m && s[r][c] !=  # ) { n         if (d[r][c] > cost) { n           heap.erase(Pos(r, c)); n           d[r][c] = cost; n           heap.insert(Pos(r, c)); n           par[r][c] = dir; n         } n       } n     } n   } n } n char sm[10000]; n int main() { n   int k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     scanf( %s , s[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] >=  a  && s[i][j] <=  z ) { n         wh[s[i][j] -  a ].init(i, j); n         calc(i, j, p[s[i][j] -  a ]); n       } n     } n   } n   int r, c; n   int r2, c2; n   scanf( %d %d %s %d %d , &r, &c, sm, &r2, &c2); n   r--, c--; n   r2--, c2--; n   calc(r2, c2, p[26]); n   int len = strlen(sm); n   for (int i = 0; i < len && k;) { n     char ch = s[r][c]; n     if (ch == sm[i]) { n       i++; n       continue; n     } n     int cost = (isDigit(ch) ? ch -  0  : 1); n     int dir = p[sm[i] -  a ][r][c]; n     if (cost > k) { n       k = 0; n       break; n     } n     k -= cost; n     r -= di[dir]; n     c -= dj[dir]; n   } n   while (k > 0 && (r != r2 || c != c2)) { n     char ch = s[r][c]; n     int cost = (isDigit(ch) ? ch -  0  : 1); n     if (cost > k) { n       k = cost; n       exit(1); n       break; n     } n     k -= cost; n     int dir = p[26][r][c]; n     r -= di[dir]; n     c -= dj[dir]; n   } n   printf( %d %d n , r + 1, c + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : -x; n } n const int tt = 75; n int n; n int m; n int x[100]; n int y[100]; n int z[100]; n vector<int> v; n vector<int> w[300]; n int res[301][301][2]; n int was[301][301][2]; n map<int, int> num; n int f[300]; n int calc() { n   vector<pair<int, int> > v; n   for (int i = 0; i < n; i++) n     if (z[i]) { n       v.push_back(make_pair(x[i], 1)); n       v.push_back(make_pair(x[i] + y[i], -1)); n     } else { n       v.push_back(make_pair(x[i] - y[i], 1)); n       v.push_back(make_pair(x[i], -1)); n     } n   sort((v).begin(), (v).end()); n   int cur = 0, bal = 0; n   for (int i = 0; i + 1 < 2 * n; i++) { n     bal += v[i].second; n     if (bal) cur += v[i + 1].first - v[i].first; n   } n   return cur; n } n int go(int i, int j, int t) { n   if (j < i) { n     t = 0; n     j = i; n   } n   if (i == ((int)(v).size())) return 0; n   if (was[i][j][t]) return res[i][j][t]; n   was[i][j][t] = 1; n   int cur = go(i + 1, j, t); n   if ((!t || i < j) && f[i] > j) n     cur = max(cur, go((t ? j : i) + 1, f[i], 0) + (v[f[i]] - v[j])); n   for (int k = 0; k < ((int)(w[i]).size()); k++) n     if (w[i][k] > j) n       cur = max(cur, go((t ? j : i) + 1, w[i][k], 1) + (v[w[i][k]] - v[j])); n   return res[i][j][t] = cur; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x[i], &y[i]); n     v.push_back(x[i] - y[i]); n     v.push_back(x[i]); n     v.push_back(x[i] + y[i]); n   } n   sort((v).begin(), (v).end()); n   v.resize(unique((v).begin(), (v).end()) - v.begin()); n   for (int i = 0; i < ((int)(v).size()); i++) num[v[i]] = i; n   for (int i = 0; i < n; i++) { n     int a = num[x[i] - y[i]]; n     int b = num[x[i]]; n     int c = num[x[i] + y[i]]; n     f[b] = c; n     w[a].push_back(b); n   } n   printf( %d n , go(0, 0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() n { n      ios_base :: sync_with_stdio(false); cin.tie(NULL); n      int t ; cin >> t ; while(t--) n      { n          int a , b ; cin >> a >> b ; n    a-=b ; n    while(b--) cout <<  a  ; n    string d =  cba  ; n    for(int i = 0 ; i < a ; i++) n    { n        cout << d[i % 3] ; n    } n    cout << endl ; n      } n  n } n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n long long n, m, rr[10][10]; n long long func(long long), dp[(1 << 21)]; n map<long long, pair<long long, long long> > mr; n long long check(long long i, long long j) { n   return (abs(mr[i].first - mr[j].first) + abs(mr[i].second - mr[j].second)) == n          3; n } n int main() { n   scanf( %lld %lld , &n, &m); n   if (n > m) swap(n, m); n   if (n * m <= 21) { n     long long x = 0; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) { n         mr[x++] = make_pair(i, j); n       } n     memset(dp, -1, sizeof(dp)); n     cout << func(0); n   } else { n     long long ans = 0; n     if (n == 1) { n       ans += m - m % 6; n       m = m % 6; n       ans += (m >= 3 ? 2 * (m - 3) : 0); n     } else n       ans = n * m - (n * m) % 2; n     cout << ans <<   n ; n   } n } n long long func(long long mask) { n   if (dp[mask] != -1) return dp[mask]; n   long long ans = 0; n   for (int i = 0; i < n * m; i++) { n     for (int j = i + 1; j < n * m; j++) { n       if ((mask & (1 << i)) == 0 && (mask & (1 << j)) == 0 && check(i, j)) { n         ans = max(ans, 2 + func(mask | (1 << i) | (1 << j))); n       } n     } n   } n   return dp[mask] = ans; n } n


#include <bits/stdc++.h> n using namespace std; n bool f(int n, int h, int c, int t) { n   return (2 * n + 1ll) * abs(1ll * n * (h + c - 2 * t) + t - c) <= n          (2 * n - 1ll) * (1ll * n * (h + c - 2 * t) + h - t); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) { n     int h, c, t; n     cin >> h >> c >> t; n     if (2 * t <= h + c) n       cout <<  2  n            <<   n ; n     else { n       int a = h - t; n       int b = 2 * t - c - h; n       int k = 2 * (a / b) + 1; n       long long val1 = n           abs(k / 2 * 1ll * c + (k + 1) / 2 * 1ll * h - t * 1ll * k); n       long long val2 = abs((k + 2) / 2 * 1ll * c + (k + 3) / 2 * 1ll * h - n                            t * 1ll * (k + 2)); n       if (val1 * (k + 2) <= val2 * k) n         cout << k <<   n ; n       else n         cout << k + 2 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n #define sim template < class c n #define ris return * this n #define dor > debug & operator << n #define eni(x) sim > typename   n   enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { n sim > struct rge { c b, e; }; n sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } n sim > auto dud(c* x) -> decltype(cerr << *x, 0); n sim > char dud(...); n struct debug { n #ifndef ONLINE_JUDGE n ~debug() { cerr << endl; } n eni(!=) cerr << boolalpha << i; ris; } n eni(==) ris << range(begin(i), end(i)); } n sim, class b dor(pair < b, c > d) { n   ris <<  (  << d.first <<  ,   << d.second <<  ) ; n } n sim dor(rge<c> d) { n   *this <<  [ ; n   for (auto it = d.b; it != d.e; ++it) n     *this <<  ,   + 2 * (it == d.b) << *it; n   ris <<  ] ; n } n #else n sim dor(const c&) { ris; } n #endif n }; n #define imie(...)   [  << #__VA_ARGS__  :   << (__VA_ARGS__) <<  ]   n  n #define ll long long n  n const ll mod = 998244353; n  n long long power(long long x, ll y, ll p) n { n     long long res = 1; // Initialize result n   n     x = x % p; // Update x if it is more than or n     // equal to p n   n     while (y > 0) n     { n       n         // If y is odd, multiply x with result n         if (y & 1) n             res = (res * x) % p; n   n         // y must be even now n         y = y >> 1; // y = y/2 n         x = (x * x) % p; n     } n     return res; n } n   n // Returns n^(-1) mod p n long long modInverse(long long n, int p) n { n     return power(n, p - 2, p); n } n   n // Returns nCr % p using Fermat s little n // theorem. n long long nCrModPFermat(long long n,int r, int p) n { n     // If n<r, then nCr should return 0 n     if (n < r) n         return 0; n     // Base case n     if (r == 0) n         return 1; n   n     // Fill factorial array so that we n     // can find all factorial of r, n n     // and n-r n     long long fac[n + 1]; n     fac[0] = 1; n     for (int i = 1; i <= n; i++) n         fac[i] = (fac[i - 1] * i) % p; n   n     return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; n } n  n void test_case() { n     ll n; n     cin >> n; n     string s; n     cin >> s; n     ll cnt = 0, cnt2 = 0, tmp = 0; n     for(ll i = 0; i < n; ++i) { n         if(s[i] ==  0 ) { n             cnt2 = cnt2 + (tmp) / 2; n             tmp = 0; n             cnt++; n         } else { n             tmp++; n         } n     } n     cnt2 = cnt2 + (tmp) / 2; n     cout << nCrModPFermat(cnt + cnt2, cnt, mod) << endl; n } n   n int main() n { n     ll T;  n     cin >> T; n     for(ll i = 1; i <= T; ++i) { n         test_case(); n     } n     return 0; n }


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n int rd() { n   int k = 0; n   char c = getchar(); n   while (c >  9  || c <  0 ) c = getchar(); n   while (c >=  0  && c <=  9 ) k = k * 10 + c -  0 , c = getchar(); n   return k; n } n const int N = 300000, M = 600001; n int T, x, y, f[M], sx[M], sy[M]; n long long ans; n vector<pair<int, int> > v[M << 1]; n map<pair<int, int>, int> mp; n pair<int, int> p; n int F(int x) { n   while (x != f[x]) x = f[x]; n   return x; n } n void Add(int x, int l, int r, int s, int t, pair<int, int> p) { n   if (s <= l && r <= t) { n     v[x].push_back(p); n     return; n   } n   if (s <= ((l + r) >> 1)) Add((x << 1), l, ((l + r) >> 1), s, t, p); n   if (((l + r) >> 1) < t) Add((x << 1 | 1), ((l + r) >> 1) + 1, r, s, t, p); n } n void Solve(int x, int l, int r) { n   long long pre = ans; n   stack<int> S; n   for (int i = 0; i < v[x].size(); ++i) { n     int a = F(v[x][i].first), b = F(v[x][i].second + N); n     if (a == b) continue; n     if (sx[a] + sy[a] > sx[b] + sy[b]) { n       int t = a; n       a = b, b = t; n     } n     ans -= (long long)sx[a] * sy[a] + (long long)sx[b] * sy[b], n         f[a] = b, sx[b] += sx[a], sy[b] += sy[a]; n     ans += (long long)sx[b] * sy[b], S.push(a); n   } n   if (l == r) n     printf( %lld  , ans); n   else n     Solve((x << 1), l, ((l + r) >> 1)), n         Solve((x << 1 | 1), ((l + r) >> 1) + 1, r); n   while (!S.empty()) { n     int t = S.top(), z = f[t]; n     S.pop(), f[t] = t, sx[z] -= sx[t], sy[z] -= sy[t]; n   } n   ans = pre; n } n int main() { n   T = rd(); n   for (int i = 1; i <= T; ++i) { n     x = rd(), y = rd(), p = make_pair(x, y); n     if (mp.count(p)) n       Add(1, 1, T, mp[p], i - 1, p), mp.erase(p); n     else n       mp[p] = i; n   } n   for (auto t : mp) Add(1, 1, T, t.second, T, t.first); n   for (int i = 1; i <= N; ++i) sx[f[i] = i] = 1; n   for (int i = N + 1; i < M; ++i) sy[f[i] = i] = 1; n   Solve(1, 1, T); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> adj[100007][2]; n long long ecnt[100007][2] = {0}; n long long ss[100007] = {0}; n long long sum = 0; n long long n, m, q; n int main() { n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     --a; n     --b; n     if (a > b) swap(a, b); n     adj[a][0].push_back(b); n     adj[b][1].push_back(a); n     ecnt[a][0]++; n     ecnt[b][1]++; n   } n   for (long long i = 0; i < n; i++) { n     ss[i] = ecnt[i][0] * ecnt[i][1]; n     sum += ss[i]; n   } n   cin >> q; n   cout << sum << endl; n   while (q--) { n     long long v; n     cin >> v; n     --v; n     sum -= ss[v]; n     ss[v] = 0; n     ecnt[v][1] += ecnt[v][0]; n     ecnt[v][0] = 0; n     for (auto i : adj[v][0]) { n       sum -= ecnt[i][0] * ecnt[i][1]; n       ecnt[i][1]--; n       ecnt[i][0]++; n       sum += ecnt[i][0] * ecnt[i][1]; n       ss[i] = ecnt[i][0] * ecnt[i][1]; n       adj[i][0].push_back(v); n     } n     adj[v][0].clear(); n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int maxdp[27]; n int dp[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   string str; n   cin >> n >> str; n   for (int i = 0; i < n; ++i) { n     for (int c = 26; c > str[i] -  a ; --c) n       maxdp[str[i] -  a ] = max(maxdp[c] + 1, maxdp[str[i] -  a ]); n     dp[i] = maxdp[str[i] -  a ]; n   } n   cout << *max_element(dp, dp + n) << endl; n   for (int i = 0; i < n; ++i) { n     cout << dp[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 1e2 + 5; n int main() { n   int n, m, c; n   string s[SIZE], extract, f; n   c = 0, f =  acef ; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       extract.push_back(s[i][j]); n       extract.push_back(s[i + 1][j]); n       extract.push_back(s[i][j + 1]); n       extract.push_back(s[i + 1][j + 1]); n       sort(extract.begin(), extract.end()); n       if (extract == f) c++; n       extract.clear(); n     } n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 7; n pair<int, pair<int, int> > A[maxn]; n map<int, int> X; n map<int, int> Y; n int Hx[maxn], Hy[maxn]; n int ans[maxn]; n int fa[maxn]; n int fi(int u) { n   if (fa[u] == u) return u; n   return fa[u] = fi(fa[u]); n } n void un(int u, int v) { n   int p1 = fi(u), p2 = fi(v); n   if (p1 != p2) fa[p1] = p2; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n * m; i++) { n     fa[i] = i; n     scanf( %d , &A[i].first); n     A[i].second.first = i / m; n     A[i].second.second = i % m; n   } n   int j = -1; n   sort(A, A + n * m); n   for (int i = 0; i < n * m; i++) { n     if (i != n * m - 1 && A[i].first == A[i + 1].first) continue; n     for (int k = j + 1; k <= i; k++) { n       int p = A[k].second.first * m + A[k].second.second; n       int x = A[k].second.first, y = A[k].second.second; n       Hx[x] = p; n       Hy[y] = p; n     } n     for (int k = j + 1; k <= i; k++) { n       int p = A[k].second.first * m + A[k].second.second; n       int x = A[k].second.first, y = A[k].second.second; n       un(Hx[x], p); n       un(Hy[y], p); n     } n     for (int k = j + 1; k <= i; k++) { n       int p = A[k].second.first * m + A[k].second.second; n       int x = A[k].second.first, y = A[k].second.second; n       int pa = fi(p); n       ans[pa] = max(ans[pa], max(X[x], Y[y]) + 1); n     } n     for (int k = j + 1; k <= i; k++) { n       int p = A[k].second.first * m + A[k].second.second; n       int x = A[k].second.first, y = A[k].second.second; n       X[x] = max(X[x], ans[fi(p)]); n       Y[y] = max(Y[y], ans[fi(p)]); n     } n     j = i; n   } n   for (int i = 0; i < n * m; i++) { n     printf( %d  , ans[fi(i)]); n     if (i % m == m - 1) printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> adj[100005], stx, sty; n long long mx = 0ll, ans[100005]; n int sz[100005], par[100005]; n pair<int, int> centroid = {0, 0}; n bool cmp(int u, int v) { return sz[u] < sz[v]; } n void dfs(int nw, int bf) { n   par[nw] = bf; n   sz[nw] = 1; n   for (int nx : adj[nw]) n     if (nx != bf) { n       dfs(nx, nw); n       sz[nw] += sz[nx]; n     } n   sort(adj[nw].begin(), adj[nw].end(), cmp); n   mx += 2 * min(sz[nw], n - sz[nw]); n   centroid = max(centroid, {min(sz[nw], n - sz[nw]), nw}); n } n void get_subtree(int nw, int bf, vector<int> &v) { n   v.push_back(nw); n   for (int nx : adj[nw]) n     if (nx != bf) get_subtree(nx, nw, v); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int u, v, i = 1; i < n; i++) { n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1, 0); n   int x = centroid.second, y = par[x]; n   if (sz[x] * 2 > n) swap(x, y); n   get_subtree(x, y, stx); n   get_subtree(y, x, sty); n   for (int i = 0; i < stx.size(); i++) { n     ans[stx[i]] = sty[i]; n     ans[sty[sty.size() - stx.size() + i]] = stx[i]; n   } n   for (int i = 0; i < sty.size() - stx.size(); i++) n     ans[sty[i]] = sty[stx.size() + i]; n   cout << mx << endl; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a, b; n bool temp = false; n int main() { n   cin >> a; n   for (b = 1; b <= 500; b++) n     if ((b * (b + 1)) / 2 == a) { n       temp = true; n       break; n     } n   if (temp) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e5 + 9, lg = 20; n int n, q, timer = 1; n vector<int> adj[inf], level[inf]; n int depth[inf], in[inf], out[inf], parent[inf][lg]; n void dfs(int u, int par) { n   in[u] = timer++; n   level[depth[u]].push_back(in[u]); n   for (auto v : adj[u]) n     if (v != par) parent[v][0] = u, depth[v] = depth[u] + 1, dfs(v, u); n   out[u] = timer++; n } n int KthParent(int u, int k) { n   for (int j = lg - 1; j >= 0; j--) n     if (k & (1ll << j)) u = parent[u][j]; n   return u; n } n int CntLevelInSubtree(int u, int h) { n   return upper_bound(level[h].begin(), level[h].end(), out[u]) - n          lower_bound(level[h].begin(), level[h].end(), in[u]); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int tmp; n     cin >> tmp; n     if (tmp) adj[tmp].push_back(i), adj[i].push_back(tmp); n   } n   for (int i = 1; i <= n; i++) n     if (!in[i]) dfs(i, 0); n   for (int j = 1; j < lg; j++) n     for (int i = 1; i <= n; i++) parent[i][j] = parent[parent[i][j - 1]][j - 1]; n   cin >> q; n   while (q--) { n     int v, p; n     cin >> v >> p; n     if (depth[v] < p) { n       cout << 0 <<    ; n       continue; n     } n     int u = KthParent(v, p); n     cout << CntLevelInSubtree(u, depth[v]) - 1 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 1000000 + 5; n int f[maxn], inv[maxn], finv[maxn]; n void init() { n   inv[1] = 1; n   for (int i = 2; i < maxn; i++) n     inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod; n   f[0] = finv[0] = 1; n   for (int i = 1; i < maxn; i++) { n     f[i] = f[i - 1] * 1ll * i % mod; n     finv[i] = finv[i - 1] * 1ll * inv[i] % mod; n   } n } n int C(int n, int m) { n   if (m < 0 || m > n) return 0; n   return f[n] * 1ll * finv[n - m] % mod * finv[m] % mod; n } n long long qpow(long long x, long long n) { n   long long r = 1; n   while (n > 0) { n     if (n & 1) r = r * x % mod; n     n >>= 1; n     x = x * x % mod; n   } n   return r; n } n long long T(long long n, long long k) { n   if (k == n) return 1; n   return k * qpow(n, n - k - 1) % mod; n } n int n, m, a, b; n int main() { n   init(); n   cin >> n >> m >> a >> b; n   long long ans = 0; n   for (int i = 1; i < n; i++) { n     long long tot = 1ll * C(m - 1, i - 1) * C(n - 2, i - 1) % mod * f[i - 1] % n                     mod * qpow(m, n - i - 1) % mod * T(n, i + 1) % mod; n     ans += tot; n     if (ans >= mod) ans -= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int func1(long long int i) { n   long long int m = i; n   return i * (i + 1) * (2 * i + 1) / 6 - (m + m + 2) * i * (i + 1) / 2 + n          (m + m + 1 + m * m) * i; n } n long long int func2(long long int x, long long int i) { n   return x * (i + i * i - i * (i + 1) / 2); n } n int main() { n   long long int inp, tem, ans = 0, tem2; n   cin >> inp; n   vector<pair<long long int, long long int> > v; n   bool flag = 0; n   for (int i = 0; i < 100000000; i++) n     if (ans > 0) ans += 1; n   ans = 0; n   for (long long int i = 1; func1(i) <= inp; i++) { n     tem = inp - func1(i); n     if (!tem) { n       flag = 1; n       v.push_back(make_pair(i, i)); n     } else { n       tem2 = func2(1, i); n       if (tem % tem2 == 0) v.push_back(make_pair(i, i + tem / tem2)); n     } n   } n   if (!flag) n     cout << 2 * v.size(); n   else n     cout << 2 * v.size() - 1; n   cout <<   n ; n   for (int i = 0; i < v.size(); i++) n     cout << v[i].first <<     << v[i].second <<   n ; n   int i = v.size() - 1; n   if (flag) i--; n   for (; i >= 0; i--) cout << v[i].second <<     << v[i].first <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chkMax(T &x, T y) { n   if (y > x) x = y; n } n template <typename T> n void chkMin(T &x, T y) { n   if (y < x) x = y; n } n template <typename T> n void inline read(T &x) { n   int f = 1; n   x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s <=  9  && s >=  0 ) x = x * 10 + (s ^ 48), s = getchar(); n   x *= f; n } n const int N = 2005; n int n, f[10][10][10][10][10], g[10][10][10][10][10]; n int ans = 2e9; n int main() { n   read(n); n   memset(f, 0x3f, sizeof f); n   f[1][0][0][0][0] = 0; n   for (int i = 1; i <= n; i++) { n     int x, y; n     read(x), read(y); n     memcpy(g, f, sizeof f); n     memset(f, 0x3f, sizeof f); n     for (int p = 1; p <= 9; p++) { n       for (int a = 0; a <= 9; a++) { n         for (int b = 0; b <= 9; b++) { n           for (int c = 0; c <= 9; c++) { n             for (int d = 0; d <= 9; d++) { n               if (a == 0) { n                 chkMin(f[x][y][b][c][d], g[p][a][b][c][d] + abs(p - x)); n               } n               if (b == 0) { n                 chkMin(f[x][a][y][c][d], g[p][a][b][c][d] + abs(p - x)); n               } n               if (c == 0) { n                 chkMin(f[x][a][b][y][d], g[p][a][b][c][d] + abs(p - x)); n               } n               if (d == 0) { n                 chkMin(f[x][a][b][c][y], g[p][a][b][c][d] + abs(p - x)); n               } n             } n           } n         } n       } n     } n     for (int a = 9; ~a; a--) { n       for (int b = 9; ~b; b--) { n         for (int c = 9; ~c; c--) { n           for (int d = 9; ~d; d--) { n             for (int p = 1; p <= 9; p++) { n               if (a) chkMin(f[a][0][b][c][d], f[p][a][b][c][d] + abs(p - a)); n               if (b) chkMin(f[b][a][0][c][d], f[p][a][b][c][d] + abs(p - b)); n               if (c) chkMin(f[c][a][b][0][d], f[p][a][b][c][d] + abs(p - c)); n               if (d) chkMin(f[d][a][b][c][0], f[p][a][b][c][d] + abs(p - d)); n             } n           } n         } n       } n     } n   } n   for (int i = 1; i <= 9; i++) chkMin(ans, f[i][0][0][0][0]); n   printf( %d n , ans + 2 * n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   int k = 0, l = 0, n; n   cin >> a >> b; n   n = a.size(); n   for (int i = 0; i < n; i++) { n     if (a[i] ==  4  && b[i] ==  7 ) { n       k++; n     } else if (a[i] ==  7  && b[i] ==  4 ) { n       l++; n     } n   } n   int p = max(k, l); n   cout << p << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class T> n T gcd(T a, T b) { n   return ((b == 0) ? a : gcd(b, a % b)); n } n void solve() { n   int n, k; n   long long x; n   cin >> n >> k >> x; n   string s; n   cin >> s; n   x--; n   int cnt = 0; n   vector<int> nm; n   for (int i = n - 1; i >= 0; i--) { n     if (s[i] ==  * ) n       cnt++; n     else { n       if (cnt) nm.push_back(cnt); n       cnt = 0; n     } n   } n   if (cnt) nm.push_back(cnt); n   int uwu = 0; n   vector<int> d; n   while (x > 0) { n     int cr = (nm[uwu++] * k) + 1; n     d.push_back(x % cr); n     x /= cr; n   } n   int ind = 0, lst = -1; n   string ans =   ; n   for (int i = n - 1; i >= 0; i--) { n     if (s[i] ==  a ) n       ans +=  a , lst = 1; n     else { n       if (!lst) continue; n       if (ind == (int)(d.size())) continue; n       for (int j = 0; j < (int)(d[ind]); j++) ans +=  b ; n       ind++; n       lst = 0; n     } n   } n   reverse(ans.begin(), ans.end()); n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     long long n, s, t; n     cin >> n >> s >> t; n     cout << max(n - s + 1, n - t + 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace runzhe2000 { n const int INF = 1 << 29; n int ft[100005], gt[100005], f[100005][10], g[100005][10], timer = 1, c[10], n                                                           sta[100005], top; n char s[100005]; n bool insta[100005]; n struct node { n   int l, r, v; n } no[100005]; n void del(int x) { n   no[no[x].l].r = no[x].r; n   no[no[x].r].l = no[x].l; n } n void main() { n   int n, q; n   scanf( %d%d%s , &n, &q, s + 1); n   for (int i = 1, v; i <= n; i++) { n     if (s[i] >=  0  && s[i] <=  9 ) n       v = s[i] -  0 ; n     else n       v = s[i] ==  <  ? 11 : 10; n     no[i] = (node){i - 1, i + 1, v}; n   } n   memset(ft, 63, sizeof(ft)); n   memset(gt, 63, sizeof(gt)); n   ++timer; n   ++n; n   no[n] = (node){n - 1, n + 1, 10}; n   for (int l = 1; l <= n; l++) n     if (ft[l] > INF) { n       for (int cur = l, next, pos = 10; l <= cur && cur <= n; n            cur = next, ++timer) { n         if (timer < ft[cur]) { n           ft[cur] = timer; n           for (int i = 0; i < 10; i++) f[cur][i] = c[i]; n         } n         if (no[cur].v < 10) { n           c[no[cur].v--]++; n           if (no[cur].v < 0) del(cur); n         } else { n           pos = no[cur].v; n         } n         next = pos == 11 ? no[cur].l : no[cur].r; n         if (no[cur].v > 9 && no[next].v > 9) del(cur); n         if (!insta[cur]) sta[++top] = cur, insta[cur] = 1; n         if (pos == 11) { n           for (; no[cur].l < sta[top] && sta[top] <= cur && top; --top) { n             gt[sta[top]] = ++timer; n             for (int i = 0; i < 10; i++) g[sta[top]][i] = c[i]; n           } n         } n       } n     } n   for (; q--;) { n     int l, r; n     scanf( %d%d , &l, &r); n     if (ft[r + 1] < gt[l]) { n       for (int i = 0; i < 10; i++) n         printf( %d%c , f[r + 1][i] - f[l][i], i == 9 ?   n  :    ); n     } else { n       for (int i = 0; i < 10; i++) n         printf( %d%c , g[l][i] - f[l][i], i == 9 ?   n  :    ); n     } n   } n } n }  // namespace runzhe2000 n int main() { runzhe2000::main(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int r = 0, b = 0, y = 0, g = 0; n   int len = s.size(); n   for (int i = 0; i < len; i++) { n     if (s[i] ==  B ) n       b = i % 4; n     else if (s[i] ==  G ) n       g = i % 4; n     else if (s[i] ==  R ) n       r = i % 4; n     else if (s[i] ==  Y ) n       y = i % 4; n   } n   int w = 0, x = 0, yy = 0, z = 0, pos; n   for (int i = 0; i < len; i++) { n     if (s[i] ==  ! ) { n       pos = i % 4; n       if (pos == r) n         w++; n       else if (pos == b) n         x++; n       else if (pos == y) n         yy++; n       else if (pos == g) n         z++; n     } n   } n   cout << w <<     << x <<     << yy <<     << z << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   unsigned short x; n   std::cin >> x; n   (x > 1) ? std::cout << x <<     << x : std::cout << -1; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int T = 31; n const int mod = 1000000007; n const long long oo = 1LL << 60; n long long N; n int K; n long long B[T][T]; n long long power[70]; n long long dp[70][T][T]; n bool bio[70]; n long long rot[T][T][T]; n long long mult(long long a, long long b) { n   if (a == 0) return 0; n   if (b >= oo / a + 1) return oo; n   return a * b; n } n void mult(long long A[T][T], long long B[T][T]) { n   static long long C[T][T]; n   for (int i = (0); i < (T); ++i) n     for (int j = (0); j < (T); ++j) C[i][j] = 0; n   for (int i = (0); i < (T); ++i) n     for (int j = (0); j < (T); ++j) n       for (int k = (0); k < (T); ++k) n         C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod; n   for (int i = (0); i < (T); ++i) n     for (int j = (0); j < (T); ++j) A[i][j] = C[i][j]; n } n void rec(long long prefix, int n, long long A[T][T]) { n   long long lo = mult(prefix, power[n]); n   long long hi = lo + power[n] - 1; n   if (lo >= N) return; n   long long z = prefix % K; n   mult(A, rot[z]); n   if (n == 0) { n     mult(A, B); n   } else { n     bool memo = hi < N; n     auto AA = memo ? dp[n] : A; n     if (!bio[n] && memo) n       for (int i = (0); i < (T); ++i) AA[i][i] = 1; n     if (!bio[n] || !memo) n       for (int i = (0); i < (K); ++i) rec(prefix * K + i, n - 1, AA); n     if (memo) bio[n] = true, mult(A, AA); n   } n   mult(A, rot[(K - z) % K]); n } n int main() { n   scanf( %lld%d , &N, &K); n   for (int i = (0); i < (K); ++i) B[i][i] = 1; n   for (int i = (1); i < (K); ++i) B[i][0] = 1; n   B[K][0] = 1; n   B[K][K] = 1; n   for (int x = (0); x < (K); ++x) n     for (int i = (0); i < (K); ++i) rot[x][i][(i + x) % K] = 1; n   for (int x = (0); x < (K); ++x) n     for (int i = (K); i < (T); ++i) rot[x][i][i] = 1; n   static long long A[T][T]; n   A[0][K] = 1; n   power[0] = 1; n   for (int i = (1); i < (70); ++i) power[i] = mult(power[i - 1], K); n   rec(0, 69, A); n   long long ans = 1; n   for (int i = (0); i < (K); ++i) ans = (ans + A[0][i]) % mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   int n, p, q; n   cin >> n; n   vector<pair<int, int>> v1; n   int x1, x2; n   for (size_t i = 0; i < n; i++) { n     cin >> x1 >> x2; n     v1.push_back(make_pair(x1, x2)); n   } n   bool s = true; n   sort(v1.begin(), v1.end()); n   for (size_t i = 0; i < n; i++) { n     if (v1[i].second > v1[i + 1].second && i + 1 < n) { n       s = false; n       break; n     } n   } n   if (s == true) { n     cout <<  Poor Alex ; n   } else { n     cout <<  Happy Alex ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n; n double f1[N], f2[N], f3[N], a[N], ans; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     f1[i] = a[i] * (f1[i - 1] + 1); n     f2[i] = a[i] * (f2[i - 1] + 1 + 2 * f1[i - 1]); n     f3[i] = a[i] * (f3[i - 1] + 3 * f2[i - 1] + 3 * f1[i - 1] + 1); n   } n   for (int i = 1; i <= n + 1; ++i) ans += (1 - a[i]) * f2[i - 1]; n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   printf( %d , n % 2); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> t; n int n, x, i, j, h, sol, temp; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     t.push_back(x); n   } n   cin >> h; n   sort(t.begin(), t.end()); n   sol = 0; n   for (i = 0; i < n; i++) { n     temp = 1; n     for (j = i + 1; j < n; j++) { n       if (t[j] - t[i] <= h) temp++; n     } n     sol = max(sol, temp); n   } n   cout << sol << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 305; n int n, m; n class Point { n  public: n   long long x, y; n   Point() : x(0), y(0) {} n   Point(long long _x, long long _y) : x(_x), y(_y) {} n   void read() { scanf( %lld%lld , &x, &y); } n   Point operator+(const Point &o) const { return Point(x + o.x, y + o.y); } n   Point operator-(const Point &o) const { return Point(x - o.x, y - o.y); } n   long long operator*(const Point &o) const { return x * o.y - y * o.x; } n   bool operator<(const Point &o) const { n     int c = (y == 0 && x > 0) || (y > 0); n     int oc = (o.y == 0 && o.x > 0) || (o.y > 0); n     if (c != oc) return c > oc; n     return (*this) * o > 0; n   } n } p[maxn]; n class Edge { n  public: n   Point w; n   int u, v; n   Edge() {} n   Edge(Point _w, int _u, int _v) : w(_w), u(_u), v(_v) {} n   bool operator<(const Edge &o) const { return w < o.w; } n } e[maxn * maxn]; n long long dp[maxn][maxn][6]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) p[i].read(); n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) { n       e[m++] = Edge(p[j] - p[i], i, j); n       e[m++] = Edge(p[i] - p[j], j, i); n     } n   sort(e, e + m); n   for (int i = 0; i < n; i++) dp[i][i][0] = 1; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < 5; k++) dp[j][e[i].v][k + 1] += dp[j][e[i].u][k]; n   long long ans = 0; n   for (int i = 0; i < n; i++) ans += dp[i][i][5]; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, x; n   cin >> n >> x; n   string second; n   cin >> second; n   long long ans = 0; n   long long sm = 0; n   for (long long i = 0; i < n; i++) { n     if (second[i] ==  0 ) n       sm++; n     else n       sm--; n   } n   long long c = 0; n   for (long long i = 0; i < n; i++) { n     if (sm == 0 && c == x) { n       cout << -1 << endl; n       return; n     } n     if ((x - c) % sm == 0 && (x - c) / sm >= 0 && sm != 0) ans++; n     if (second[i] ==  0 ) n       c++; n     else n       c--; n   } n   cout << ans << endl; n } n int main() { n   long long t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n struct sort_pred { n   bool operator()(const std::pair<int, int> &left, n                   const std::pair<int, int> &right) { n     return right.first > left.first; n   } n }; n int n, m, k, t; n int r = 0; n vector<bool> visited; n vector<vector<int>> v; n vector<int> distanc; n queue<int> q; n map<pair<int, int>, int> mp; n vector<bool> chosen; n void bfs(int first) { n   visited[first] = true; n   q.push(first); n   while (!q.empty()) { n     int s = q.front(); n     q.pop(); n     for (auto u : v[s]) { n       if (distanc[s] - mp[make_pair(s, u)] >= 0 && n           distanc[s] - mp[make_pair(s, u)] > distanc[u]) { n         if (!visited[u]) r++; n         visited[u] = true; n         distanc[u] = distanc[s] - mp[make_pair(s, u)]; n         if (chosen[u]) distanc[u] = t; n         q.push(u); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   map<int, int> mp; n   vector<int> b(n); n   for (int i = 0; i < n; i++) { n     int first; n     cin >> first; n     mp[first] = i + 1; n   } n   for (int i = 0; i < n; i++) { n     int first; n     cin >> first; n     cout << mp[first] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int inf = 1e9 + 5; n long long powmod(long long a, long long b) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret = (ret * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ret; n } n const int maxn = 3e5 + 5; n long long a[maxn]; n int n, m; n int q[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   long long tot = 0; n   for (int i = 0; i <= n - 1; i++) { n     cin >> a[i]; n     tot += a[i]; n   } n   sort(a, a + n); n   cin >> m; n   for (int i = 0; i <= m - 1; i++) { n     cin >> q[i]; n     cout << tot - a[n - q[i]] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1002000, CAN = 34; n int n, m, p, vis[MAXN], pr[MAXN], pcnt; n char w1[MAXN], w2[MAXN]; n vector<int> can[300]; n int muln(int x, int y) { return 1LL * x * y % p; } n int mo(int x) { n   while (x >= p) x -= p; n   while (x < 0) x += p; n   return x; n } n int qpow(int x, int y) { n   int ret = 1; n   for (; y; y >>= 1, x = muln(x, x)) n     if (y & 1) ret = muln(ret, x); n   return ret; n } n struct poly { n   int deg; n   vector<int> a; n   poly(int x = 0) : deg(x) { a.resize(x + 1, 0); } n   int& operator[](int idx) { return a[idx]; } n   void print(string s) { n     cerr <<  Poly   << s <<  :  ; n     for (auto g : a) printf( %d  , g); n     cerr << endl; n   } n   int at(int x) { n     x = mo(x); n     int ret = 0; n     for (int i = deg; i >= 0; --i) ret = mo(muln(ret, x) + mo(a[i])); n     return ret; n   } n   void norm() { n     for (auto& g : a) g = mo(g); n   } n } I; n void sieve() { n   for (int i = 2; i <= 1000000; ++i) { n     if (!vis[i]) { n       pr[++pcnt] = i; n     } n     for (int j = 1; j <= pcnt && i * pr[j] <= 1000000; ++j) { n       vis[i * pr[j]] = true; n       if (i % pr[j] == 0) break; n     } n   } n   for (int k = 1, i = pr[k]; k <= pcnt; i = pr[++k]) { n     if (i > m) n       for (int j = 1; j <= CAN; ++j) n         if ((i - 1) % pr[j] == 0) can[j].push_back(i); n   } n } n void sol(int opt) { n   int d = pr[opt]; n   poly a(d - 1); n   for (int i = 0; i < n; ++i) a[i % d] += I[i]; n   for (auto pri : can[opt]) { n     p = pri; n     int rr = 2; n     while (qpow(rr, (p - 1) / d) == 1) rr++; n     rr = qpow(rr, (p - 1) / d); n     for (int Cnt = 1; Cnt < d; ++Cnt) { n       int r = qpow(rr, Cnt); n       if (r < 2 || r > p - 2) continue; n       int val = a.at(r); n       if (val == 0) { n         printf( %d %d n , p, r); n         exit(0); n       } n     } n   } n } n int main() { n   scanf( %d%d%s%s , &n, &m, w1, w2); n   sieve(); n   I = poly(n - 1); n   for (int i = 0; i < n; ++i) I[i] = w1[i] - w2[i]; n   for (int i = 1; i <= CAN; ++i) sol(i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100007], ans[100007], vis[100007]; n int count(int x) { n   int cnt = 0, now = 0; n   memset(vis, 255, sizeof(vis)); n   for (int i = 1; i <= n; i++) { n     if (vis[a[i]] == now) continue; n     vis[a[i]] = now; n     if (++cnt > x) { n       cnt = 1; n       vis[a[i]] = ++now; n     } n   } n   return now + 1; n } n void work(int l, int r) { n   if (l > r) return; n   int cnt1 = count(l), cnt2 = count(r); n   if (cnt1 == cnt2) { n     for (int i = l; i <= r; i++) ans[i] = cnt1; n     return; n   } n   ans[l] = cnt1; n   ans[r] = cnt2; n   int mid = (l + r) >> 1; n   work(l + 1, mid); n   work(mid + 1, r - 1); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   work(1, n); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   vector<long long> good, bad; n   for (long long i = 0; i < n; i++) { n     long long val = sqrt(a[i]); n     if (val * val == a[i]) n       good.push_back(a[i]); n     else n       bad.push_back(a[i]); n   } n   long long sz1 = good.size(), sz2 = bad.size(); n   if (sz1 == sz2) { n     cout << 0 <<   n ; n   } else if (sz1 > sz2) { n     long long nz = 0; n     for (auto it : good) { n       if (it) nz++; n     } n     long long req = (sz1 - sz2) / 2; n     if (nz >= req) n       cout << req <<   n ; n     else n       cout << nz + (req - nz) * 2 <<   n ; n   } else { n     vector<long long> ans; n     long long req = (sz2 - sz1) / 2; n     for (auto it : bad) { n       long long val = sqrt(it); n       long long down = it - val * val, up = (val + 1) * (val + 1) - it; n       ans.push_back(min(down, up)); n     } n     sort(ans.begin(), ans.end()); n     long long fans = 0; n     for (long long i = 0; i < req; i++) { n       fans += ans[i]; n     } n     cout << fans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = (1 << 22) + 5; n int n, m; n bool is[MAX], seen[MAX]; n int conj(int x) { n   int r = 0; n   for (int i = 0; i < n; ++i) n     if (!(x & (1 << i))) r += (1 << i); n   return r; n } n void dfs(int st, bool p = 0) { n   if (seen[st]) return; n   seen[st] = 1; n   if (is[st]) dfs(conj(st), 1); n   if (p) { n     for (int i = 0; i < n; ++i) n       if (st & (1 << i)) { n         dfs(st - (1 << i), 1); n       } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n >> m; n   vector<int> st((1 << n) + 1, 0); n   vector<bool> lz((1 << n) + 1, 0); n   for (int i = 0; i < m; ++i) { n     int x; n     cin >> x; n     is[x] = 1; n   } n   int cnt = 0; n   for (int i = 0; i < (1 << n); ++i) { n     if (!seen[i] && is[i]) { n       dfs(i); n       ++cnt; n     } n   } n   cout << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   long long int n, x; n   cin >> n; n   vector<vector<long long int> > a(n); n   for (long long int i = 0; i < n; i++) { n     cin >> x; n     for (long long int j = 0; j < x; j++) { n       long long int tp; n       cin >> tp; n       a[i].push_back(tp); n     } n   } n   long long int a1 = 0, b = 0; n   vector<long long int> q; n   for (long long int i = 0; i < n; i++) { n     long long int f = a[i].size(); n     if (f % 2 != 0) { n       for (long long int j = 0; j < (f - 1) / 2; j++) { n         a1 += a[i][j]; n       } n       for (long long int j = (f - 1) / 2 + 1; j < f; j++) { n         b += a[i][j]; n       } n       q.push_back(a[i][(f - 1) / 2]); n     } else { n       for (long long int j = 0; j < f / 2; j++) { n         a1 += a[i][j]; n       } n       for (long long int j = f / 2; j < f; j++) { n         b += a[i][j]; n       } n     } n   } n   sort(q.begin(), q.end(), greater<long long int>()); n   for (long long int k = 0; k < q.size(); k++) { n     if (k % 2 == 0) n       a1 += q[k]; n     else n       b += q[k]; n   } n   cout << a1 <<     << b; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 3 * (int)1e5 + 100; n int b[maxN]; n int t[4 * maxN]; n int a[maxN]; n void build(int v, int tl, int tr) { n   if (tl == tr) { n     t[v] = a[tl]; n     return; n   } n   int tm = (tl + tr) / 2; n   build(v + v, tl, tm); n   build(v + v + 1, tm + 1, tr); n   t[v] = min(t[v + v], t[v + v + 1]); n } n void upd(int v, int tl, int tr, int pos, int val) { n   if (tl == tr) { n     t[v] = val; n     return; n   } n   int tm = (tl + tr) / 2; n   if (pos <= tm) n     upd(v + v, tl, tm, pos, val); n   else n     upd(v + v + 1, tm + 1, tr, pos, val); n   t[v] = min(t[v + v], t[v + v + 1]); n } n const int INF = (int)1e9 + 100; n int get(int v, int tl, int tr, int l, int r) { n   if (l > r) return INF; n   if (tl == l && tr == r) { n     return t[v]; n   } n   int tm = (tl + tr) / 2; n   return min(get(v + v, tl, tm, l, min(r, tm)), n              get(v + v + 1, tm + 1, tr, max(l, tm + 1), r)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     set<pair<int, int> > s; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       s.insert(make_pair(a[i], i)); n     } n     bool ok = true; n     build(1, 1, n); n     for (int i = 1; i <= n; i++) { n       cin >> b[i]; n       if (!ok) continue; n       auto it = s.lower_bound(make_pair(b[i], -1)); n       if (it == s.end() || ((it->first) != b[i])) { n         ok = false; n         continue; n       } n       int ind = (it->second); n       s.erase(it); n       if (b[i] != get(1, 1, n, 1, ind)) { n         ok = false; n       } n       upd(1, 1, n, ind, INF); n     } n     if (ok) n       cout <<  YES  <<   n ; n     else n       cout <<  NO  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[11]; n int main() { n   int i, n, m, x; n   while (scanf( %d , &a[0]) > 0) { n     int c[33] = {0}; n     c[a[0]]++; n     for ((i) = 0; (i) < (5); (i)++) { n       scanf( %d , &a[i + 1]); n       c[a[i + 1]]++; n     } n     bool f = 0; n     int mx = 0; n     for ((i) = 0; (i) < (11); (i)++) { n       mx = max(mx, c[i]); n       c[i] = c[i] % 4; n       if (c[i] == 2) f = 1; n     } n     if (mx < 4) n       printf( Alien n ); n     else if (f) n       printf( Elephant n ); n     else n       printf( Bear n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 2000 + 10; n const int INF = std::numeric_limits<int>::max(); n const double EPS = 1e-6; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n int a[MAXN][MAXN]; n std::deque<std::pair<std::pair<int, int>, int>> q; n using std::cin; n using std::cout; n using std::endl; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(0); n   std::cout.tie(0); n   int n, m; n   cin >> n >> m; n   int r, c; n   cin >> r >> c; n   int x, y; n   cin >> x >> y; n   for (int i = 0; i <= n + 1; ++i) { n     for (int j = 0; j <= m + 1; ++j) { n       a[i][j] = -1; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       char c; n       cin >> c; n       if (c ==  . ) a[i][j] = INF; n     } n   } n   q.push_back({{r, c}, 0}); n   a[r][c] = 0; n   while (!q.empty()) { n     int cur_x = q.front().first.first, cur_y = q.front().first.second, n         cur_step = q.front().second; n     q.pop_front(); n     for (int i = 0; i < 4; ++i) { n       if (a[cur_x + dx[i]][cur_y + dy[i]] > cur_step + (dy[i] == -1)) { n         a[cur_x + dx[i]][cur_y + dy[i]] = cur_step + (dy[i] == -1); n         if (dy[i] == -1) n           q.push_back({{cur_x + dx[i], cur_y + dy[i]}, cur_step + 1}); n         else n           q.push_front({{cur_x + dx[i], cur_y + dy[i]}, cur_step}); n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (a[i][j] != -1) { n         if (a[i][j] <= x && a[i][j] + j - c <= y) { n           ++ans; n         } n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops,no-stack-protector,fast-math,inline ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n using namespace std; n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   string sep; n   for (const auto &x : v) os << sep << x, sep =  ,  ; n   return os <<  } ; n } n template <typename A, typename B> n ostream &operator<<(ostream &os, const pair<A, B> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n void dbg_out() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr <<     << H; n   dbg_out(T...); n } n template <const int &MOD> n struct _m_int { n   int val; n   _m_int(int64_t v = 0) { n     if (v < 0) v = v % MOD + MOD; n     if (v >= MOD) v %= MOD; n     val = v; n   } n   static int inv_mod(int a, int m = MOD) { n     int g = m, r = a, x = 0, y = 1; n     while (r != 0) { n       int q = g / r; n       g %= r; n       swap(g, r); n       x -= q * y; n       swap(x, y); n     } n     return x < 0 ? x + m : x; n   } n   explicit operator int() const { return val; } n   explicit operator int64_t() const { return val; } n   _m_int &operator+=(const _m_int &other) { n     val -= MOD - other.val; n     if (val < 0) val += MOD; n     return *this; n   } n   _m_int &operator-=(const _m_int &other) { n     val -= other.val; n     if (val < 0) val += MOD; n     return *this; n   } n   static unsigned fast_mod(uint64_t x, unsigned m = MOD) { n     return x % m; n     unsigned x_high = x >> 32, x_low = (unsigned)x; n     unsigned quot, rem; n     asm( divl %4 n  :  =a (quot),  =d (rem) :  d (x_high),  a (x_low),  r (m)); n     return rem; n   } n   _m_int &operator*=(const _m_int &other) { n     val = fast_mod((uint64_t)val * other.val); n     return *this; n   } n   _m_int &operator/=(const _m_int &other) { return *this *= other.inv(); } n   friend _m_int operator+(const _m_int &a, const _m_int &b) { n     return _m_int(a) += b; n   } n   friend _m_int operator-(const _m_int &a, const _m_int &b) { n     return _m_int(a) -= b; n   } n   friend _m_int operator*(const _m_int &a, const _m_int &b) { n     return _m_int(a) *= b; n   } n   friend _m_int operator/(const _m_int &a, const _m_int &b) { n     return _m_int(a) /= b; n   } n   _m_int &operator++() { n     val = val == MOD - 1 ? 0 : val + 1; n     return *this; n   } n   _m_int &operator--() { n     val = val == 0 ? MOD - 1 : val - 1; n     return *this; n   } n   _m_int operator++(int) { n     _m_int before = *this; n     ++*this; n     return before; n   } n   _m_int operator--(int) { n     _m_int before = *this; n     --*this; n     return before; n   } n   _m_int operator-() const { return val == 0 ? 0 : MOD - val; } n   bool operator==(const _m_int &other) const { return val == other.val; } n   bool operator!=(const _m_int &other) const { return val != other.val; } n   _m_int inv() const { return inv_mod(val); } n   _m_int pow(int64_t p) const { n     if (p < 0) return inv().pow(-p); n     _m_int a = *this, result = 1; n     while (p > 0) { n       if (p & 1) result *= a; n       a *= a; n       p >>= 1; n     } n     return result; n   } n   friend ostream &operator<<(ostream &os, const _m_int &m) { n     return os << m.val; n   } n }; n extern const int MOD = 998244353; n using mod_int = _m_int<MOD>; n const int BITS = 53; n template <typename T> n struct xor_basis { n   T basis[BITS]; n   int n = 0; n   T min_value(T start) const { n     if (n == BITS) return 0; n     for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]); n     return start; n   } n   T max_value(T start = 0) const { n     if (n == BITS) return ((T)1 << BITS) - 1; n     for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]); n     return start; n   } n   bool add(T x) { n     x = min_value(x); n     if (x == 0) return false; n     basis[n++] = x; n     for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--) n       swap(basis[k], basis[k - 1]); n     return true; n   } n   void merge(const xor_basis<T> &other) { n     for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]); n   } n   void merge(const xor_basis<T> &a, const xor_basis<T> &b) { n     if (a.n > b.n) { n       *this = a; n       merge(b); n     } else { n       *this = b; n       merge(a); n     } n   } n }; n int N, M, B; n vector<int64_t> basis; n vector<int64_t> answers; n void generate_all() { n   vector<int64_t> change(B + 1, 0); n   int64_t prefix = 0; n   for (int i = 0; i < B; i++) { n     prefix ^= basis[i]; n     change[i] = prefix; n   } n   change[B] = prefix; n   int64_t value = 0; n   for (unsigned mask = 0; mask < 1U << B; mask++) { n     answers[__builtin_popcountll(value)]++; n     value ^= change[__builtin_ctz(mask + 1)]; n   } n } n void dp_on_repeats() { n   ; n   for (int i = 0; i < B; i++) n     for (int j = i + 1; j < B; j++) n       basis[i] = min(basis[i], basis[i] ^ basis[j]); n   ; n   vector<int> repeats; n   for (int bit = 0; bit < M; bit++) { n     int count = 0; n     for (int i = 0; i < B; i++) n       if (basis[i] >> bit & 1) count++; n     if (count > 1) repeats.push_back(bit); n   } n   int R = repeats.size(); n   vector<pair<int, int>> options(B); n   int count_sum = 0; n   for (int i = 0; i < B; i++) { n     int64_t value = basis[i]; n     int count = __builtin_popcountll(value); n     int repeat_mask = 0; n     for (int r = 0; r < R; r++) n       if (value >> repeats[r] & 1) { n         count--; n         repeat_mask |= 1 << r; n       } n     options[i] = {count, repeat_mask}; n     count_sum += count; n   }; n   sort(options.begin(), options.end()); n   if (R <= 20) { n     vector<vector<int64_t>> dp(count_sum + 1, vector<int64_t>(1 << R, 0)); n     dp[0][0] = 1; n     int max_count = 0; n     for (pair<int, int> &option : options) { n       int opt_count = option.first; n       int opt_mask = option.second; n       for (int count = max_count; count >= 0; count--) n         for (int mask = 0; mask < 1 << R; mask++) n           dp[count + opt_count][mask ^ opt_mask] += dp[count][mask]; n       max_count += opt_count; n     } n     for (int count = 0; count <= count_sum; count++) n       for (int mask = 0; mask < 1 << R; mask++) n         if (dp[count][mask] > 0) n           answers[count + __builtin_popcount(mask)] += dp[count][mask]; n   } else { n     vector<vector<unsigned>> dp(count_sum - 1, vector<unsigned>(1 << R, 0)); n     int max_count = 0; n     for (pair<int, int> &option : options) { n       int opt_count = option.first; n       int opt_mask = option.second; n       for (int count = min(max_count - 1, count_sum - 1 - opt_count - 1); n            count >= 0; count--) n         for (int mask = 0; mask < 1 << R; mask++) n           dp[count + opt_count][mask ^ opt_mask] += dp[count][mask]; n       dp[opt_count - 1][opt_mask]++; n       max_count += opt_count; n     } n     for (int count = 0; count < count_sum - 1; count++) n       for (int mask = 0; mask < 1 << R; mask++) n         if (dp[count][mask] > 0) n           answers[count + 1 + __builtin_popcount(mask)] += dp[count][mask]; n     int everything = 0; n     for (int i = 0; i < B; i++) everything ^= options[i].second; n     answers[count_sum + __builtin_popcount(everything)]++; n     answers[0]++; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> N >> M; n   xor_basis<int64_t> basis_obj; n   for (int i = 0; i < N; i++) { n     int64_t a; n     cin >> a; n     basis_obj.add(a); n   } n   B = basis_obj.n; n   basis.resize(B); n   for (int i = 0; i < B; i++) basis[i] = basis_obj.basis[i]; n   answers.assign(M + 1, 0); n   if (B <= 0.57 * M) n     generate_all(); n   else n     dp_on_repeats(); n   for (int i = 0; i <= M; i++) { n     mod_int answer = answers[i]; n     answer *= mod_int(2).pow(N - B); n     cout << answer << (i < M ?     :   n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int g[405][405]; n vector<int> v[405]; n double ans; n double gai[405]; n double pos[405]; n double suan(int yi, int juli) { n   memset(gai, 0, sizeof(gai)); n   for (int i = 1; i <= n; i++) { n     if (g[i][yi] == juli) { n       for (int j = 0; j < (int)v[i].size(); j++) { n         gai[v[i][j]] += 1.0 / n / (int)v[i].size(); n       } n     } n   } n   vector<int> r; n   r.clear(); n   for (int i = 1; i <= n; i++) n     if (gai[i]) r.push_back(i); n   double ret = 0; n   for (int i = 1; i <= n; i++) { n     double tmp = 0; n     for (int j = 0; j < (int)r.size(); j++) { n       pos[g[i][r[j]]] = max(pos[g[i][r[j]]], gai[r[j]]); n     } n     for (int j = 0; j < (int)r.size(); j++) { n       tmp += pos[g[i][r[j]]]; n       pos[g[i][r[j]]] = 0; n     } n     ret = max(ret, tmp); n   } n   return ret; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) n       if (i != j) g[i][j] = 199912310; n   } n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     scanf( %d%d , &x, &y); n     g[x][y] = 1; n     g[y][x] = 1; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) n         g[i][j] = g[j][i] = min(g[i][j], g[i][k] + g[k][j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     double now = 0; n     for (int dist = 0; dist < n; dist++) { n       int cnt = 0; n       for (int j = 1; j <= n; j++) { n         if (g[i][j] == dist) cnt++; n       } n       if (!cnt) continue; n       double p = 1.0 / n; n       double q = suan(i, dist); n       now += max(p, q); n     } n     ans = max(ans, now); n   } n   printf( %.8lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool voltea; n int A[100005], B[100005], I[100005], D[100005], R[100005], ctos, hay, x, y, mid, n     en; n int Z[100005], grande; n void lis() { n   B[1] = A[1]; n   if (voltea) n     D[ctos] = 1; n   else n     I[1] = 1; n   hay = 1; n   for (int i = 2; i <= ctos; i++) { n     if (A[i] > B[hay]) { n       ++hay; n       B[hay] = A[i]; n       en = hay; n     } else { n       x = 1; n       y = hay; n       while (x < y) { n         mid = (x + y) / 2; n         if (B[mid] < A[i]) { n           x = mid + 1; n         } else { n           y = mid; n         } n       } n       B[x] = A[i]; n       en = x; n     } n     if (voltea) { n       D[ctos - i + 1] = en; n     } else { n       I[i] = en; n     } n   } n } n int main() { n   scanf( %d , &ctos); n   for (int i = 1; i <= ctos; i++) { n     scanf( %d , &A[i]); n   } n   lis(); n   voltea = true; n   x = 1; n   y = ctos; n   while (x < y) { n     swap(A[x], A[y]); n     A[x] *= -1; n     A[y] *= -1; n     ++x; n     --y; n   } n   if (x == y) { n     A[x] *= -1; n   } n   lis(); n   grande = 1; n   for (int i = 1; i <= ctos; i++) { n     if (I[i] + D[i] - 1 > grande) { n       grande = I[i] + D[i] - 1; n     } n   } n   for (int i = 1; i <= ctos; i++) { n     if (I[i] + D[i] - 1 == grande) { n       Z[I[i]]++; n     } else { n       R[i] = 1; n     } n   } n   for (int i = 1; i <= ctos; i++) { n     if (R[i] == 0) { n       R[i] = 2; n       if (Z[I[i]] == 1) { n         R[i] = 3; n       } n     } n     printf( %d , R[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c; n   cin >> a >> b >> c; n   if (a >= c || b >= c) { n     cout <<  0 ; n   } else if (a <= 0 && b <= 0) { n     cout <<  -1 ; n   } else { n     long long int count = 0; n     while (a < c && b < c) { n       if (a > b) { n         swap(a, b); n       } else if (a < 0) { n         count += (abs(a) / b + (abs(a) % b != 0)); n         if (abs(a) % b) { n           long long int r = abs(a) % b; n           a = (b - r); n         } else { n           a = 0; n         } n       } else { n         long long int p = b; n         b += a; n         a = p; n         count++; n       } n     } n     cout << count; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1}, n                          {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; n int par[200010], r[200010], lar[200010]; n void init() { n   for (int i = 0; i < 200010; i++) { n     par[i] = i; n     r[i] = 0; n     lar[i] = 1; n   } n } n int find(int x) { n   if (par[x] == x) return x; n   return par[x] = find(par[x]); n } n bool same(int x, int y) { return find(x) == find(y); } n void unit(int x, int y) { n   if (same(x, y)) return; n   x = find(x); n   y = find(y); n   if (r[x] < r[y]) { n     par[x] = y; n     lar[y] += lar[x]; n   } else { n     par[y] = x; n     lar[x] += lar[y]; n     if (r[x] == r[y]) { n       r[x]++; n     } n   } n } n int main() { n   static int n; n   static int a[200010]; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   init(); n   bool used[200010]; n   for (int i = 0; i < 200010; i++) { n     used[i] = false; n   } n   vector<pair<int, int> > vec; n   for (int i = 1; i <= n; i++) { n     vec.push_back(pair<int, int>(a[i], i)); n   } n   sort(vec.begin(), vec.end()); n   reverse(vec.begin(), vec.end()); n   int ans[200010]; n   int k = 1; n   for (int i = 0; i < vec.size(); i++) { n     pair<int, int> p = vec[i]; n     used[p.second] = true; n     if (used[p.second - 1]) { n       unit(p.second, p.second - 1); n     } n     if (used[p.second + 1]) { n       unit(p.second, p.second + 1); n     } n     for (; k <= lar[find(p.second)]; k++) { n       ans[k] = p.first; n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d  , ans[i]); n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 + 7; n int n; n map<pair<int, int>, int> id; n vector<int> g[N]; n long double rep(long double x) { n   if (x >= 2) { n     x -= 2; n   } n   return x; n } n void dfs(int a, int p = 0, long double t = 0) { n   int deg = (int)g[a].size(); n   long double jump = 2 / (long double)deg; n   for (auto &b : g[a]) { n     if (b == p) continue; n     t = rep(t + jump); n     cout << 1 <<     << id[{a, b}] <<    ; n     if (t <= 1) { n       cout << b <<     << a <<     << fixed << setprecision(6) << 1 - t <<   n ; n     } else { n       cout << a <<     << b <<     << fixed << setprecision(6) << 1 - rep(t + 1) n            <<   n ; n     } n     dfs(b, a, rep(t + 1)); n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     id[{x, y}] = i; n     id[{y, x}] = i; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   int root = 1; n   cout << n - 1 <<   n ; n   dfs(root); n } n


#include <bits/stdc++.h> n const double eps = 1e-8; n const double pi = acos(-1.0); n const int INF = 0x7f7f7f7f; n const double inf = 1e50; n template <class T> n inline T minx(T a, T b) { n   return (a == -1 || b < a) ? b : a; n } n template <class T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n inline T lcm(T a, T b) { n   return a * (b / gcd(a, b)); n } n template <class T> n inline T getbit(T n) { n   return n ? (1 + getbit(n & (n - 1))) : 0; n } n template <class T> n inline T lowbit(T n) { n   return n & (-n); n } n using namespace std; n pair<double, double> parse(double x, double y) { return make_pair(x, y); } n int main() { n   int i, j, k; n   pair<double, double> A, B, C; n   double x, y; n   double dx, dy, tx, ty; n   while (cin >> x >> y) { n     A = parse(x, y); n     cin >> x >> y; n     B = parse(x, y); n     cin >> x >> y; n     C = parse(x, y); n     dx = B.first - A.first; n     dy = B.second - A.second; n     tx = C.first - B.first; n     ty = C.second - B.second; n     double res = dx * ty - dy * tx; n     if (res == 0) n       puts( TOWARDS ); n     else if (res > 0) n       puts( LEFT ); n     else n       puts( RIGHT ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000009; n const int maxn = 1000010; n long long p[maxn], q[maxn]; n int main() { n   int i, n; n   long long nAns = 0; n   cin >> n; n   n /= 2; n   p[1] = 2; n   q[1] = 1; n   for (i = 2; i <= n; i++) { n     p[i] = p[i - 1] * 2 % mod; n     q[i] = q[i - 1] * (p[i] - 3) % mod; n     nAns = (nAns + q[i] * 4) % mod; n   } n   cout << (2 * nAns * nAns % mod + nAns * 8 % mod + 10) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 7; n int main(int argc, char *argv[]) { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   int n, k; n   std::set<std::pair<int, int> > t[maxn], s; n   std::cin >> n >> k; n   int u, v, w; n   int ans = 0; n   for (int i = 0; i < n - 1; i++) { n     std::cin >> u >> v >> w; n     t[u].insert(std::make_pair(v, w)); n     t[v].insert(std::make_pair(u, w)); n   } n   for (int i = 1; i <= n; i++) { n     if (t[i].size() == 1) { n       s.insert(std::make_pair((*t[i].begin()).second, i)); n     } n   } n   while (n > k || s.size() > 2) { n     auto i = *s.begin(); n     s.erase(i); n     int cur = i.second; n     ans = i.first; n     n--; n     int nxt = (*t[cur].begin()).first; n     t[nxt].erase(t[nxt].lower_bound(std::make_pair(cur, 0))); n     if (t[nxt].size() == 1) { n       s.insert(std::make_pair((*t[nxt].begin()).second + ans, nxt)); n     } n   } n   std::cout << ans << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e3 + 5; n const int MOD = 1e9 + 7; n int N, M, A, B; n int G[MAXN * MAXN], X, Y, Z; n int H[MAXN][MAXN]; n int P[MAXN][MAXN]; n int T[MAXN][MAXN]; n long long ans; n deque<pair<int, int> > qu; n void reset() { qu.clear(); } n void push(int v, int p) { n   while (qu.size() && qu.back().first >= v) { n     qu.pop_back(); n   } n   qu.push_back({v, p}); n } n int pop(int p) { n   while (qu.size() && qu.front().second < p) { n     qu.pop_front(); n   } n   assert(qu.size()); n   return qu.front().first; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> N >> M >> A >> B; n   cin >> G[0] >> X >> Y >> Z; n   for (int i = 1; i < N * M; i++) { n     G[i] = (1LL * G[i - 1] * X + Y) % Z; n   } n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < M; j++) { n       H[i][j] = G[i * M + j]; n     } n   } n   for (int i = 0; i < N; i++) { n     reset(); n     for (int j = 0; j < B; j++) push(H[i][j], j); n     P[i][B - 1] = pop(0); n     for (int j = B; j < M; j++) { n       push(H[i][j], j); n       P[i][j] = pop(j - B + 1); n     } n   } n   for (int j = B - 1; j < M; j++) { n     reset(); n     for (int i = 0; i < A; i++) push(P[i][j], i); n     ans += pop(0); n     for (int i = A; i < N; i++) { n       push(P[i][j], i); n       ans += pop(i - A + 1); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int withoutzero(int x); n int main() { n   int a, b, c, d, e, f; n   cin >> a >> b; n   d = withoutzero(a); n   e = withoutzero(b); n   c = a + b; n   f = withoutzero(c); n   if ((d + e) == f) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n int withoutzero(int x) { n   int arr[10], i = 0, t = 0, k = 0; n   while (x > 0) { n     arr[i] = x % 10; n     x = x / 10; n     i++; n   } n   for (int j = 0; j < i; j++) { n     if (arr[j] != 0) { n       t += arr[j] * pow(10, k); n       k++; n     } n   } n   return t; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[100000 + 1], a[100000 + 1], n, d, b; n int main() { n   scanf( %lld%lld%lld , &n, &d, &b); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   f[0] = 0; n   for (int i = 1; i <= n; i++) f[i] += f[i - 1] + a[i]; n   long long a1 = 0, a2 = 0; n   for (long long i = 1; i <= n / 2; i++) { n     if (f[min(n, i + i * d)] >= (a1 + 1) * b) ++a1; n     if (f[n] - f[max((long long)1, n - i - i * d)] >= (a2 + 1) * b) ++a2; n   } n   printf( %lld , n / 2 - min(a1, a2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N = 500005; n long long MAX = 9223372036854775807; n long long MOD = 1000000007; n long double PI = 4 * atan(1); n long long int t, a, b, res; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     res = 0; n     a = abs(a - b); n     res += a / 5; n     a = a % 5; n     res += a / 2 + a % 2; n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long int T; n   cin >> T; n   while (T--) { n     long long int n; n     cin >> n; n     long long int a[n]; n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     if (a[n - 1] > a[0]) n       cout <<  YES  n            <<   n ; n     else n       cout <<  NO  n            <<   n ; n   } n   return 0; n } n