#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAXN = 20; n const int MAXS = (1 << 16) + 10; n long long dp[MAXS]; n int ans[MAXN], req[MAXN]; n bool vis[MAXN]; n int main() { n   long long n, y, m; n   cin >> n >> y >> m; n   y -= 2001; n   for (int i = 1; i <= m; i++) { n     int a, b; n     cin >> a >> b; n     req[b - 1] |= (1 << (a - 1)); n   } n   memset(ans, -1, sizeof ans); n   for (int i = 0; i < n; i++) { n     for (ans[i] = 0; ans[i] < n; ans[i]++) { n       if (!vis[ans[i]]) { n         memset(dp, 0, sizeof dp); n         dp[0] = 1; n         for (int s = 0; s < (1 << n); s++) { n           if (dp[s] > 0) { n             for (int j = 0; j < n; j++) { n               if (!(s & (1 << j))) { n                 if ((ans[j] == -1 || ans[j] == __builtin_popcount(s)) && n                     ((s & req[j]) == req[j])) { n                   dp[s | (1 << j)] += dp[s]; n                 } n               } n             } n           } n         } n         if (y >= dp[(1 << n) - 1]) { n           y -= dp[(1 << n) - 1]; n         } else { n           vis[ans[i]] = true; n           break; n         } n       } n     } n     if (ans[i] >= n) { n       cout <<  The times have changed  << endl; n       return 0; n     } n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n const long long N = 2e5 + 100; n vector<long long> v[N]; n long long dfn[N], num[N], val[N], sz[N], hs[N], tp[N], dep[N], f[N]; n inline void dfs(long long x, long long fa) { n   sz[x] = 1; n   long long num = 0; n   dep[x] = dep[fa] + 1; n   f[x] = fa; n   for (auto y : v[x]) { n     if (y == fa) continue; n     dfs(y, x); n     sz[x] += sz[y]; n     if (num < sz[y]) hs[x] = y, num = sz[y]; n   } n } n long long cnt; n inline void redfs(long long x, long long fa, long long top) { n   tp[x] = top; n   dfn[x] = ++cnt; n   num[cnt] = x; n   if (hs[x]) redfs(hs[x], x, top); n   for (auto y : v[x]) { n     if (y == fa || y == hs[x]) continue; n     redfs(y, x, y); n   } n } n struct seg { n   long long l, r, v; n } t[N << 2]; n long long a[N]; n inline void build(long long p, long long l, long long r) { n   t[p].l = l, t[p].r = r; n   if (l == r) { n     t[p].v = abs(val[num[l]]); n     return; n   } n   long long mid = l + r >> 1; n   build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r); n   t[p].v = t[p << 1].v + t[p << 1 | 1].v; n } n inline void modify(long long p, long long x, long long v) { n   if (t[p].l == t[p].r) { n     t[p].v = v; n     return; n   } n   long long mid = t[p].l + t[p].r >> 1; n   if (x <= mid) n     modify(p << 1, x, v); n   else n     modify(p << 1 | 1, x, v); n   t[p].v = t[p << 1].v + t[p << 1 | 1].v; n } n inline long long query(long long p, long long x, long long y) { n   if (x <= t[p].l && t[p].r <= y) { n     return t[p].v; n   } n   long long mid = t[p].l + t[p].r >> 1; n   if (y <= mid) return query(p << 1, x, y); n   if (x > mid) return query(p << 1 | 1, x, y); n   return query(p << 1, x, y) + query(p << 1 | 1, x, y); n } n signed main() { n   ios::sync_with_stdio(false); n   cin >> n >> q; n   for (long long i = 1; i <= n; i++) cin >> val[i], val[i] = abs(val[i]); n   for (long long i = 1; i <= n - 1; i++) { n     long long x, y; n     cin >> x >> y; n     v[x].push_back(y), v[y].push_back(x); n   } n   dfs(1, 0); n   redfs(1, 0, 1); n   build(1, 1, n); n   for (long long i = 1; i <= q; i++) { n     long long opt, x, y; n     cin >> opt >> x >> y; n     if (opt == 1) { n       modify(1, dfn[x], abs(y)); n       val[x] = abs(y); n     } else { n       long long ans = 0, X = x, Y = y; n       ; n       while (tp[x] != tp[y]) { n         if (dep[tp[x]] < dep[tp[y]]) swap(x, y); n         ans += query(1, dfn[tp[x]], dfn[x]); n         x = f[tp[x]]; n       } n       if (dep[x] < dep[y]) swap(x, y); n       ans += query(1, dfn[y], dfn[x]); n       cout << ans * 2 - val[X] - val[Y] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (1 << 30) - 1; n const long long linf = (1ll << 62) - 1; n const int N = 200; n const int M = 1e9 + 7; n const long long M2 = 1ll * M * M; n int n; n long long a[N]; n int mat[N][N]; n int result[N][N]; n void multiply(int a[N][N], int b[N][N]) { n   static int c[N][N]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       long long result = 0; n       for (int z = 0; z < n; z++) { n         result += 1ll * a[i][z] * b[z][j]; n         if (result >= M2) { n           result -= M2; n         } n       } n       c[i][j] = result % M; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       a[i][j] = c[i][j]; n     } n   } n } n void power(long long n) { n   for (int i = 0; i < ::n; i++) { n     result[i][i] = 1; n   } n   for (; n > 0; n >>= 1) { n     if (n & 1) { n       multiply(result, mat); n     } n     multiply(mat, mat); n   } n } n int main() { n   long long k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       long long x = a[i] ^ a[j]; n       int cnt = 0; n       while (x > 0) { n         cnt += x & 1; n         x /= 2; n       } n       if (cnt % 3 == 0) { n         mat[i][j]++; n       } n     } n   } n   power(k - 1); n   int answer = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       answer += result[i][j]; n       if (answer >= M) { n         answer -= M; n       } n     } n   } n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100; n inline long long int max(long long int a, long long int b) { n   return a > b ? a : b; n } n inline long long int min(long long int a, long long int b) { n   return a > b ? b : a; n } n inline int read() { n   int x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x; n } n long long int sum[maxn * 4], mq[maxn * 4], dl[maxn * 4], dr[maxn * 4]; n int lb[maxn * 4], rb[maxn * 4]; n int N, M; n int a[maxn]; n inline void update(int i) { n   if (lb[i] == rb[i]) { n     sum[i] = fabs(dr[i]); n     mq[i] = max(dl[i], 0) + max(dr[i], 0); n     return; n   } n   sum[i] = sum[i << 1] + sum[(i << 1) + 1]; n   mq[i] = min(mq[i << 1], mq[(i << 1) + 1]); n } n inline void updateall(int i) { n   while (i > 1) { n     update(i); n     i = i >> 1; n   } n   update(1); n } n void build(int i, int l, int r) { n   lb[i] = l; n   rb[i] = r; n   if (l == r) { n     dl[i] = a[l - 1] - a[l]; n     dr[i] = a[l + 1] - a[l]; n     sum[i] = fabs(dr[i]); n     mq[i] = max(dl[i], 0) + max(dr[i], 0); n   } else { n     int m = (l + r) >> 1; n     build(i << 1, l, m); n     build((i << 1) + 1, m + 1, r); n     update(i); n   } n } n long long int getmq(int i, int l, int r) { n   if (l == lb[i] && r == rb[i]) return mq[i]; n   int m = (lb[i] + rb[i]) >> 1; n   long long int re = 1e9 + 10; n   if (l <= m) re = min(re, getmq(i << 1, l, min(m, r))); n   if (r > m) re = min(re, getmq((i << 1) + 1, max(m + 1, l), r)); n   return re; n } n int find(int x, int i) { n   if (lb[i] == rb[i] && lb[i] == x) n     return i; n   else if (x <= ((lb[i] + rb[i]) >> 1)) n     return find(x, i << 1); n   else n     return find(x, (i << 1) + 1); n } n inline long long int solve1(int l, int r, int x) { n   if (l == r) { n     int p = find(l, 1); n     return fabs(dl[p] - x) + fabs(dr[p] - x) - fabs(dl[p]) - fabs(dr[p]) + n            sum[1]; n   } else { n     return max(0, 2 * x - 2 * getmq(1, l, r)) + sum[1]; n   } n } n inline void solve2(int l, int r, int x) { n   int p = find(l, 1); n   dl[p] -= x; n   updateall(p); n   p = find(l - 1, 1); n   dr[p] += x; n   updateall(p); n   p = find(r, 1); n   dr[p] -= x; n   updateall(p); n   p = find(r + 1, 1); n   dl[p] += x; n   updateall(p); n } n int main() { n   int i, j, k, x, y; n   N = read(); n   for (i = 1; i <= N; i++) a[i] = read(); n   a[N + 1] = a[N]; n   a[0] = a[1]; n   build(1, 1, N); n   M = read(); n   for (i = 1; i <= M; i++) { n     j = read(); n     k = read(); n     x = read(); n     y = read(); n     if (j == 1) { n       printf( %I64d n , solve1(k, x, y)); n     } else { n       solve2(k, x, y); n     } n   } n } n


#include <bits/stdc++.h> n long long T, n, k; n long long a, sum, num; n int main() { n   scanf( %I64d%I64d , &n, &k); n   for (long long i = 1; i <= n; i++) { n     scanf( %I64d , &a); n     if (sum - (i - 1 - num) * (n - i) * a >= k) n       sum += a * (i - 1 - num); n     else { n       printf( %I64d n , i); n       num++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9, MOD = 1e9 + 7, n           around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0}; n const long long LINF = 1e18; n const long double PI = abs(acos(-1)); n int a, b, c; n int main() { n   cin >> a >> b >> c; n   for (int x1 = (0); x1 < (3); x1++) { n     for (int x2 = (0); x2 < (3); x2++) { n       for (int x3 = (0); x3 < (3); x3++) { n         bool li[1500 * 1510 + 1] = {}; n         int ma = 0; n         for (int i = (0); i < (max(a, max(b, c))); i++) { n           li[x1 + a * i] = li[x2 + b * i] = li[x3 + c * i] = true; n           ma = max(x1 + a * i, max(x2 + b * i, x3 + c * i)); n         } n         for (int i = 0; x1 + a * i < ma; i++) li[x1 + a * i] = true; n         for (int i = 0; x2 + b * i < ma; i++) li[x2 + b * i] = true; n         for (int i = 0; x3 + c * i < ma; i++) li[x3 + c * i] = true; n         bool f = true; n         for (int i = (0); i < (ma + 1); i++) f = f & li[i]; n         if (f) { n           cout <<  YES  << endl; n           return 0; n         } n       } n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[3001]; n int n; n struct node { n   int pos, val; n   bool operator<(const node& a) const { return val < a.val; } n } nodes[3001]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int ans = 0; n     scanf( %d , &n); n     for (int i = 0; i < n; i++) { n       scanf( %d , &nodes[i].val); n       nodes[i].pos = i; n     } n     sort(nodes, nodes + n); n     dp[0] = 1; n     for (int i = 1; i < n; i++) { n       if (nodes[i - 1].pos < nodes[i].pos) n         dp[i] = dp[i - 1] + 1; n       else n         dp[i] = 1; n       ans = max(ans, dp[i]); n     } n     ans = max(ans, dp[0]); n     printf( %d n , n - ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = ~0u >> 1, MOD = 1e9 + 7; n inline int rd() { n   int first = 0, c = 0, f = 1; n   for (; c <  0  || c >  9 ; c = getchar()) f = c !=  - ; n   for (; c >=  0  && c <=  9 ; c = getchar()) first = first * 10 + c -  0 ; n   return f ? first : -first; n } n const int N = 2e5 + 11; n struct Edge { n   int v, id; n   Edge *nex; n } pl[N << 1], *cur = pl, *G[N]; n void ins(int u, int v, int id) { n   cur->v = v, cur->id = id; n   cur->nex = G[u], G[u] = cur++; n } n int wx[N], wy[N], n, m; n int f[N], vis[N], g[N]; n void dfs(int first) { n   vis[first] = 1; n   for (Edge *i = G[first]; i; i = i->nex) n     if (!vis[i->v]) { n       dfs(i->v); n       if (f[i->v]) { n         f[i->v] = 0; n         f[first] ^= 1; n         g[i->id] ^= 1; n       } n     } n } n int q[N]; n int main() { n   n = rd(), m = rd(); n   for (int i = (1), i_end = (m); i <= i_end; i++) { n     wx[i] = rd(), wy[i] = rd(); n     ins(wx[i], wy[i], i), ins(wy[i], wx[i], i); n     f[wx[i]] ^= 1; n   } n   for (int i = (1), i_end = (n); i <= i_end; i++) n     if (!vis[i]) dfs(i); n   int ans = m; n   for (int i = (1), i_end = (n); i <= i_end; i++) n     if (f[i]) ans--; n   printf( %d n , ans >> 1); n   for (int first = (1), x_end = (n); first <= x_end; first++) { n     int cnt = 0; n     for (Edge *i = G[first]; i; i = i->nex) n       if ((wy[i->id] != first) ^ g[i->id]) { n         q[++cnt] = (wx[i->id] ^ wy[i->id] ^ first); n       } n     for (int i = 1; i < cnt; i += 2) { n       printf( %d %d %d n , q[i], first, q[i + 1]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int lsb(int val) { return val & ((val ^ (val - 1))); } n pair<int, pair<int, int> > a[600666]; n int Arb[600666], SEG[600666], result[600666]; n bool comp(pair<int, pair<int, int> > i1, pair<int, pair<int, int> > i2) { n   return i1.second.second < i2.second.second; n } n int query(int pos) { n   if (pos) return Arb[pos] + query(pos - lsb(pos)); n   return 0; n } n void update(int pos, int n) { n   if (pos > n) return; n   Arb[pos]++; n   update(pos + lsb(pos), n); n } n int bs(int value, int n, int A[]) { n   int left = 1, right = n + 1; n   while (left < right - 1) { n     int mid = (right + left) / 2; n     if (A[mid] <= value) n       left = mid; n     else n       right = mid; n   } n   return left; n } n int main() { n   cin.sync_with_stdio(false); n   int n, i, nr_seg = 0; n   cin >> n; n   for (i = 1; i <= n; ++i) { n     cin >> a[i].second.first >> a[i].second.second; n     a[i].first = i; n     SEG[++nr_seg] = a[i].second.first; n     SEG[++nr_seg] = a[i].second.second; n   } n   sort(SEG + 1, SEG + nr_seg + 1); n   sort(a + 1, a + n + 1, comp); n   for (i = 1; i <= n; ++i) { n     a[i].second.first = bs(a[i].second.first, nr_seg, SEG); n     a[i].second.second = bs(a[i].second.second, nr_seg, SEG); n   } n   for (i = 1; i <= n; ++i) { n     result[a[i].first] = query(a[i].second.second) - query(a[i].second.first); n     update(a[i].second.first, n * 2); n   } n   for (i = 1; i <= n; ++i) cout << result[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   while (b != 0) { n     int r = a % b; n     a = b; n     b = r; n   } n   return a; n } n void FindMinimum(int a[], int &n) { n   int g = gcd(a[0], a[1]); n   for (int i = 2; i < n; ++i) g = gcd(a[i], g); n   cout << g * n; n } n int main() { n   int n, a[100]; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   sort(a, a + n); n   FindMinimum(a, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int expo(int base, int exponent, int mod) { n   int ans = 1; n   while (exponent != 0) { n     if (exponent & 1) ans = (1LL * ans * base) % mod; n     base = (1LL * base * base) % mod; n     exponent >>= 1; n   } n   return ans % mod; n } n int n, a[5005], ans[10005], pre[5005], inv[5005]; n int up[5005][10005]; n int get_down(int l, int r) { n   if (l <= 0 or r < 0) return 0; n   if (r == 0) return 1; n   r = l + r - 1; n   if (r >= n) return 0; n   return 1LL * pre[r] * inv[l - 1] % 1000000007; n } n void solve() { n   cin >> n; n   for (int i = 1; i < n; i++) cin >> a[i]; n   a[0] = pre[0] = inv[0] = 1; n   for (int i = 1; i < n + 1; i++) n     pre[i] = 1LL * a[i] * pre[i - 1] % 1000000007, n     inv[i] = expo(pre[i], 1000000007 - 2, 1000000007); n   for (int i = 1; i < n + 1; i++) { n     for (int j = 1; j < 2 * n - 1; j++) { n       if (i >= 2) n         up[i][j] = (0LL + up[i - 1][j - 1] + get_down(i - 1, j - 1) - n                     get_down(i, j - 2) + 1000000007) % n                    1000000007; n       ans[j] = (ans[j] + 1LL * pre[i - 1] * (up[i][j] + get_down(i, j))) % n                1000000007; n     } n   } n   for (int i = 1; i < 2 * n - 1; i++) n     cout << 1LL * ans[i] * (1000000007 + 1) / 2 % 1000000007 <<    ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long lim = 100000000000ll; n long long qv[61], dis[100010], ma, nw, dep[100010]; n int n, Q, ans[61], x, y, v, hd[100010], cnt, q[100010], l, r, maa, la[100010], n     rt[100010]; n struct node { n   int to, next, val; n } e[200010]; n struct seg { n   int ls, rs, val; n } t[6000010]; n void addedge(int x, int y, int v) { n   e[++cnt] = (node){y, hd[x], v}, hd[x] = cnt; n   e[++cnt] = (node){x, hd[y], v}, hd[y] = cnt; n } n void add(int i, long long l, long long r, long long x) { n   t[i].val++; n   if (l == r) return; n   long long mid = (l + r) >> 1; n   if (x <= mid) n     add(t[i].ls ? t[i].ls : (t[i].ls = (++cnt)), l, mid, x); n   else n     add(t[i].rs ? t[i].rs : (t[i].rs = (++cnt)), mid + 1, r, x); n } n int query(int i, long long l, long long r, long long ql, long long qr) { n   if (l == ql && r == qr) return t[i].val; n   long long mid = (l + r) >> 1; n   if (qr <= mid) return t[i].ls ? query(t[i].ls, l, mid, ql, qr) : 0; n   if (ql > mid) return t[i].rs ? query(t[i].rs, mid + 1, r, ql, qr) : 0; n   return (t[i].ls ? query(t[i].ls, l, mid, ql, mid) : 0) + n          (t[i].rs ? query(t[i].rs, mid + 1, r, mid + 1, qr) : 0); n } n void merge(int i, int I, long long l, long long r) { n   t[i].val += t[I].val; n   if (l == r) return; n   long long mid = (l + r) >> 1; n   if (t[i].ls) { n     if (t[I].ls) merge(t[i].ls, t[I].ls, l, mid); n   } else n     t[i].ls = t[I].ls; n   if (t[i].rs) { n     if (t[I].rs) merge(t[i].rs, t[I].rs, mid + 1, r); n   } else n     t[i].rs = t[I].rs; n } n void dfs(int x, int fa) { n   rt[x] = (++cnt), add(rt[x], 0, lim, dep[x]); n   for (int i = hd[x]; i; i = e[i].next) n     if (e[i].to != fa) { n       if (dep[e[i].to] == -1) dep[e[i].to] = dep[x] + e[i].val; n       dfs(e[i].to, x), merge(rt[x], rt[e[i].to], 0, lim); n     } n   for (int i = 1; i <= Q; i++) n     ans[i] = max(ans[i], query(rt[x], 0, lim, dep[x], dep[x] + qv[i])); n } n int main() { n   scanf( %d , &n), cnt = 0; n   memset(hd, 0, sizeof(hd)); n   for (int i = 1; i < n; i++) { n     scanf( %d%d%d , &x, &y, &v); n     addedge(x, y, v); n   } n   scanf( %d , &Q); n   for (int i = 1; i <= Q; i++) scanf( %I64d , &qv[i]), ans[i] = 0; n   for (int i = 2; i <= n; i++) dis[i] = -1; n   dis[1] = 0, q[l = r = 1] = 1; n   while (l <= r) { n     x = q[l++]; n     for (int i = hd[x]; i; i = e[i].next) n       if (dis[e[i].to] == -1) n         dis[e[i].to] = dis[x] + e[i].val, q[++r] = e[i].to; n   } n   ma = 0, maa = 0; n   for (int i = 1; i <= n; i++) n     if (dis[i] > ma) ma = dis[i], maa = i; n   for (int i = 1; i <= n; i++) dis[i] = -1; n   dis[maa] = 0, q[l = r = 1] = maa; n   while (l <= r) { n     x = q[l++]; n     for (int i = hd[x]; i; i = e[i].next) n       if (dis[e[i].to] == -1) n         dis[e[i].to] = dis[x] + e[i].val, q[++r] = e[i].to, n         la[e[i].to] = ((i & 1) ? (i + 1) : (i - 1)); n   } n   ma = 0, maa = 0, nw = 0; n   for (int i = 1; i <= n; i++) n     if (dis[i] > ma) ma = dis[i], maa = i; n   for (int i = 1; i <= n; i++) dep[i] = -1; n   while (1) { n     nw += e[la[maa]].val; n     if ((nw << 1) > ma) { n       x = maa, y = e[la[maa]].to; n       dep[x] = ma - (nw - e[la[maa]].val); n       dep[y] = nw; n       break; n     } n     if ((nw << 1) == ma) { n       x = y = e[la[maa]].to; n       dep[x] = nw; n       break; n     } n     maa = e[la[maa]].to; n   } n   cnt = 0; n   if (dep[x] < dep[y]) n     dfs(x, 0); n   else n     dfs(y, 0); n   for (int i = 1; i <= Q; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int nax = 2e5 + 10; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, f; n   cin >> n >> f; n   vector<int> k(n), l(n); n   vector<pair<int, int> > a; n   for (int i = 0; i < n; i++) { n     cin >> k[i] >> l[i]; n     a.push_back({min(2 * k[i], l[i]) - min(k[i], l[i]), i}); n   } n   sort(a.rbegin(), a.rend()); n   ll ans = 0; n   for (int i = 0; i < f; i++) { n     ans += min(2 * k[a[i].second], l[a[i].second]); n   } n   for (int i = f; i < n; i++) { n     ans += min(k[a[i].second], l[a[i].second]); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 987654321; n bool adj[4010][4010]; n int n, m, u, v, cnt[4010]; n vector<vector<int> > node; n int main() { n   scanf( %d%d , &n, &m); n   node.resize(n + 1); n   while (m--) { n     scanf( %d%d , &u, &v); n     node[u].push_back(v); n     node[v].push_back(u); n     adj[u][v] = adj[v][u] = true; n   } n   for (int i = 1; i <= n; ++i) cnt[i] = node[i].size(); n   int sol = INF; n   for (int i = 1; i <= n; ++i) { n     for (int j = i + 1; j <= n; ++j) { n       if (!adj[i][j]) continue; n       for (int a = 0; a < node[i].size(); ++a) { n         int k = node[i][a]; n         if (!adj[k][j]) continue; n         sol = min(sol, cnt[i] + cnt[j] + cnt[k] - 6); n       } n     } n   } n   printf( %d n , sol == INF ? -1 : sol); n } n


#include <bits/stdc++.h> n using namespace std; n struct wall { n   double x, y; n   wall() {} n   wall(double x, double y) : x(x), y(y) {} n }; n struct tx { n   double d; n   int v; n   tx() {} n   tx(double d, int v) : d(d), v(v) {} n }; n bool cmp2(const tx& p1, const tx& p2) { return p1.d < p2.d; } n bool cmp(const wall& p1, const wall& p2) { return p1.x < p2.x; } n int dcmp(double x) { return fabs(x) < 1e-8 ? 0 : (x > 0 ? 1 : -1); } n wall w[2000001]; n wall ans[2000001]; n int n, m; n int f[2000001]; n double V; n tx ang[2000001]; n int main() { n   scanf( %d %lf , &n, &V); n   for (int i = 1; i <= n; i++) { n     scanf( %lf , &ang[i].d); n     ang[i].v = i; n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %lf %lf , &w[i].x, &w[i].y); n   sort(w + 1, w + 1 + m, cmp); n   sort(ang + 1, ang + 1 + n, cmp2); n   int tp = 1; n   for (int i = 1; i <= n; i++) { n     int flag = 0; n     if (tp > m) ans[i] = wall(V * V * sin(2 * ang[i].d) / 9.8, 0.0); n     while (tp <= m) { n       double tmpx = (w[tp].x * sin(2 * ang[i].d) / 2.0) - n                     (9.8 * w[tp].x * w[tp].x / (2.0 * V * V)); n       tmpx /= cos(ang[i].d) * cos(ang[i].d); n       if (tmpx <= 0.0) { n         ans[i] = wall(V * V * sin(2 * ang[i].d) / 9.8, 0.0), flag = 1; n         break; n       } else if (!dcmp(tmpx - w[tp].y) || tmpx < w[tp].y) { n         ans[i] = wall(w[tp].x, tmpx); n         flag = 1; n         break; n       } else n         tp++; n     } n     if (!flag) ans[i] = wall(V * V * sin(2 * ang[i].d) / 9.8, 0.0); n   } n   for (int i = 1; i <= n; i++) f[ang[i].v] = i; n   for (int i = 1; i <= n; i++) n     printf( %.9lf %.9lf n , ans[f[i]].x, ans[f[i]].y); n   return 0; n } n


#include <bits/stdc++.h> n #define mset(arr, val) memset(arr, val, sizeof(arr)) n #define all(v) v.begin(), v.end() n #define F first n #define S second n #define endl   n  n using namespace std; n // const double PI = acos(-1.0); n // const double E = exp(1.0); n // const double EPS = 1e-9; n const int MOD = (int)1e9 + 7; n const int N = (int)2e5; n  n int main() { n #ifdef MY_LOCAL n     freopen( in.txt ,  r , stdin); n     freopen( out.txt ,  w , stdout); n #endif n     ios_base::sync_with_stdio(false); n     cin.tie(0); n  n     int T; n     cin >> T; n     for (int tc = 1; tc <= T; tc++) { n         int n; n         cin >> n; n         vector<int> a(n); n         map<int, int> mp; n         cin >> a[0]; n         mp[a[0]] = 0; n         for (int i = 1; i < n; i++) { n             cin >> a[i]; n             if (a[i] != a[i - 1]) { n                 mp[a[i]]++; n             } n         } n          n         int ans = INT32_MAX; n         for (auto x : mp) { n             if (x.first != a[n - 1]) { n                 x.second++; n             } n             ans = min(ans, x.second); n         } n         if (ans == INT32_MAX) { n             ans = 0; n         } n         cout << ans << endl; n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n vector<int> mp[maxn]; n vector<int> R[maxn]; n vector<int> L[maxn]; n int ans = 0; n bool vis[maxn]; n int n, m; n struct node { n   int last_L; n   int last_R; n } last[maxn]; n void dfs(int now, int l, int r) { n   if (now == n) { n     ans = max(ans, r - l + 1); n     return; n   } n   for (int i = 0; i < mp[now].size(); i++) { n     if (vis[mp[now][i]]) continue; n     int nl = max(l, L[now][i]); n     int nr = min(r, R[now][i]); n     vis[mp[now][i]] = 1; n     if (nl <= nr && nr - nl + 1 > ans && n         !(last[mp[now][i]].last_L <= nl && last[mp[now][i]].last_R >= nr)) n       last[mp[now][i]].last_L = nl, last[mp[now][i]].last_R = nr, n       dfs(mp[now][i], nl, nr); n     vis[mp[now][i]] = 0; n   } n   return; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int a, b, l, r; n     cin >> a >> b >> l >> r; n     mp[a].push_back(b); n     mp[b].push_back(a); n     L[a].push_back(l); n     L[b].push_back(l); n     R[a].push_back(r); n     R[b].push_back(r); n   } n   vis[1] = 1; n   last[1].last_L = 1; n   last[1].last_R = INT_MAX; n   dfs(1, 1, INT_MAX); n   if (!ans) n     cout <<  Nice work, Dima!  << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Data { n   int Value, Number; n }; n bool operator<(Data A, Data B) { return A.Value < B.Value; } n int Get() { n   char c; n   while (c = getchar(), c <  0  || c >  9 ) n     ; n   int X = 0; n   while (c >=  0  && c <=  9 ) { n     X = X * 10 + c - 48; n     c = getchar(); n   } n   return X; n } n void Output(int X) { n   int Len = 0, Data[10]; n   while (X) { n     Data[Len++] = X % 10; n     X /= 10; n   } n   if (!Len) Data[Len++] = 0; n   while (Len--) putchar(Data[Len] + 48); n } n int main() { n   int N = Get(); n   static Data A[100000]; n   for (int i = 0; i < N; i++) { n     A[i].Value = Get(); n     A[i].Number = i; n   } n   sort(A, A + N); n   static int X[100000], Y[100000]; n   memset(X, 0, sizeof(X)); n   memset(Y, 0, sizeof(Y)); n   int K = (N - 1) / 3 + 1, T = N - K * 2; n   for (int i = 0; i < K; i++) { n     X[A[i].Number] = A[i].Value; n     Y[A[i + K].Number] = A[i + K].Value; n   } n   for (int i = K * 2; i < N; i++) { n     X[A[i].Number] = A[i].Value - N + i; n     Y[A[i].Number] = N - i; n   } n   printf( YES n ); n   for (int i = 0; i < N; i++) { n     Output(X[i]); n     putchar((i + 1 == N) ?   n  :    ); n   } n   for (int i = 0; i < N; i++) { n     Output(Y[i]); n     putchar((i + 1 == N) ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int64_t a[400005], b[200005]; n int64_t len; n int n; n bool check(int64_t value) { n   std::vector<std::pair<int64_t, int64_t>> v; n   for (int i = 0; i < n; ++i) { n     auto rg = std::make_pair(b[i] - value, b[i] + value); n     if (rg.first < 0) { n       rg.first += len; n       rg.second += len; n     } n     v.push_back(rg); n     if (rg.second < len) v.emplace_back(rg.first + len, rg.second + len); n   } n   std::sort(v.begin(), v.end()); n   int pa = 0, pr = 0; n   while (pr < (int)v.size()) { n     while (pa < n * 2 && (a[pa] < v[pr].first || a[pa] > v[pr].second)) ++pa; n     if (pa >= n * 2) n       return false; n     else { n       ++pa; n       ++pr; n     } n   } n   return true; n } n int main(int argc, char* argv[]) { n   scanf( %d%lld , &n, &len); n   for (int i = 0; i < n; ++i) scanf( %lld , &a[i]); n   for (int i = 0; i < n; ++i) scanf( %lld , &b[i]); n   std::sort(a, a + n); n   for (int i = 0; i < n; ++i) a[n + i] = a[i] + len; n   int64_t left = 0, right = len; n   int64_t ans = 0; n   while (right >= left) { n     int64_t mid = (left + right) / 2; n     if (check(mid)) { n       ans = mid; n       right = mid - 1; n     } else n       left = mid + 1; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n inline int add(int a, int b) { n   if ((a += b) >= mod) a -= mod; n   return a; n } n inline int mult(int a, int b) { n   long long t = 1ll * a * b; n   if (t >= mod) t %= mod; n   return t; n } n inline int dec(int a, int b) { n   if ((a -= b) < 0) a += mod; n   return a; n } n int n, q, a[200010], w[200010]; n inline int lowbit(int x) { return x & (-x); } n class tree1 { n  public: n   long long dat[200010]; n   void modify(int p, int v) { n     for (; p <= n; p += lowbit(p)) dat[p] += v; n   } n   long long query(int p) { n     long long ret = 0; n     for (; p; p -= lowbit(p)) ret += dat[p]; n     return ret; n   } n   long long query(int l, int r) { return query(r) - query(l - 1); } n } T1; n class tree2 { n  public: n   int dat[200010]; n   void modify(int p, int v) { n     for (; p <= n; p += lowbit(p)) dat[p] = add(dat[p], v); n   } n   int query(int p) { n     int ret = 0; n     for (; p; p -= lowbit(p)) ret = add(ret, dat[p]); n     return ret; n   } n   int query(int l, int r) { return dec(query(r), query(l - 1)); } n } T2; n int x, y; n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &w[i]); n     T1.modify(i, w[i]); n     T2.modify(i, mult(w[i], a[i] - i)); n   } n   while (q--) { n     scanf( %d%d , &x, &y); n     if (x < 0) { n       x = -x; n       T1.modify(x, y - w[x]); n       T2.modify(x, mult(dec(y, w[x]), a[x] - x)); n       w[x] = y; n     } else { n       if (x == y) { n         puts( 0 ); n         continue; n       } n       int L = x, R = y, pos = L; n       while (L <= R) { n         int mid = (L + R) >> 1; n         if (T1.query(x, mid) >= T1.query(mid + 1, y)) n           pos = mid, R = mid - 1; n         else n           L = mid + 1; n       } n       printf( %d n , add(dec(mult(a[pos] - pos, T1.query(x, pos) % mod), n                              T2.query(x, pos)), n                          dec(T2.query(pos, y), n                              mult(a[pos] - pos, T1.query(pos, y) % mod)))); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   bool b = false; n   for (int i = 2; i < n; i++) { n     if (a[i - 2] + a[i - 1] > a[i] && a[i] + a[i - 1] > a[i - 2] && n         a[i - 2] + a[i] > a[i - 1]) { n       b = true; n       break; n     } n   } n   cout << (b ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int nCase; n   scanf( %d , &nCase); n   while (nCase--) { n     int a, b; n     scanf( %d%d , &a, &b); n     if (a == 0) { n       if (b == 0) n         printf( 1 n ); n       else n         printf( 0.5 n ); n       continue; n     } n     if (b == 0) { n       printf( 1 n ); n       continue; n     } n     b *= 4; n     if (a >= b) n       printf( %.6f n , (0.5 * (a - b + a) * b + 1.0 * a * b) / (2.0 * b * a)); n     else n       printf( %.6f n , (1.0 * a * b + 0.5 * a * a) / (2.0 * b * a)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void faster() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const int MAXN = 2e5 + 1; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n const long double PI = acos(-1); n long long n, a[MAXN], dp[MAXN], d[MAXN], cnt[MAXN], ans; n vector<pair<int, bool>> g[MAXN]; n bool is_lucky(int x) { n   bool res = 1; n   while (x) { n     res &= (((x % 10) == 7) | ((x % 10) == 4)); n     x /= 10; n   } n   return res; n } n void dfs(int v, int pr = 0) { n   cnt[v] = 1; n   for (pair<int, int> x : g[v]) { n     int to = x.first, cost = x.second; n     if (to == pr) continue; n     dfs(to, v); n     cnt[v] += cnt[to]; n     if (cost) n       d[v] += cnt[to]; n     else n       d[v] += d[to]; n   } n } n void dfs_a(int v, int pr, long long res) { n   ans += (res * (res - 1)); n   for (pair<int, int> x : g[v]) { n     int to = x.first, cost = x.second; n     if (pr == to) continue; n     if (cost) n       dfs_a(to, v, d[to] + (cnt[1] - cnt[to])); n     else n       dfs_a(to, v, res); n   } n } n int main() { n   faster(); n   cin >> n; n   for (int i = 1, v, u, w; i < n; ++i) { n     cin >> u >> v >> w; n     w = is_lucky(w); n     g[u].push_back({v, w}); n     g[v].push_back({u, w}); n   } n   dfs(1); n   dfs_a(1, 0, d[1]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int powmod(int x, int y, int p) { n   int res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y % 2 == 1) res = (res * x) % p; n     y /= 2; n     x = (x * x) % p; n   } n   return res; n } n string dectobin(int x) { n   string s =   ; n   while (x > 0) { n     int t = x % 2; n     s.push_back(t +  0 ); n     x /= 2; n   } n   reverse(s.begin(), s.end()); n   if (s.compare(  ) == 0) n     return  0 ; n   else n     return s; n } n int long long bintodec(string s) { n   int long long ans = 0; n   int long long n = s.size(); n   for (int long long i = n - 1; i >= 0; i--) { n     if (s[i] ==  1 ) ans += pow(2, n - i - 1); n   } n   return ans; n } n int find(int k, int n) { return ((n & (1 << (k - 1))) >> (k - 1)); } n int long long f[100000 + 1]; n int32_t main() { n   f[0] = 1; n   f[1] = 1; n   for (int i = 2; i <= 100000; i++) { n     f[i] = ((i << 1) - 1ll) * f[i - 1] % 1000000007 * (i << 1) % 1000000007; n   } n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int long long n; n     cin >> n; n     cout << f[n] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[5]; n char x[4]; n long long res = 1e14; n inline long long calc(long long x, long long y, char xx) { n   if (xx ==  * ) return x * y; n   return x + y; n } n int main() { n   register int i, j; n   for (i = 1; i <= 4; i++) cin >> a[i]; n   for (i = 1; i <= 3; i++) cin >> x[i]; n   int f[5] = {0, 1, 2, 3, 4}; n   do { n     long long A = calc(a[f[1]], a[f[2]], x[1]); n     long long B = calc(A, a[f[3]], x[2]); n     long long T1 = calc(B, a[f[4]], x[3]); n     A = calc(a[f[1]], a[f[2]], x[1]); n     B = calc(a[f[3]], a[f[4]], x[2]); n     long long T2 = calc(A, B, x[3]); n     res = min(res, min(T1, T2)); n   } while (next_permutation(f + 1, f + 5)); n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int i; n   string b, a; n   cin >> b; n   int size = b.size(); n   for (i = 0; i <= size - 1; i++) { n     a += b[i]; n     i++; n   } n   a += b[size - 1]; n   cout << a <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   getline(cin, s); n   int _t = stoi(s); n   while (_t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 7; n int main() { n   long long n; n   cin >> n; n   for (long long a = 0; a < n / 1234567 + 1; a++) n     for (long long b = 0; b < n / 123456 + 1; b++) { n       long long c = n - a * 1234567 - b * 123456; n       if (c >= 0 && c % 1234 == 0) { n         cout <<  YES  << endl; n         return 0; n       } n     } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k, x, y; n   cin >> n >> k; n   for (int i = 0; i < n - 1; ++i) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   int kol = 0; n   for (int i = 1; i <= n; ++i) { n     if ((int)g[i].size() == 1) kol++; n   } n   cout << fixed << setprecision(7) << k / (double)(kol)*2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005, mod = 1e9 + 7; n inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; } n int n, cnt[maxn], vec[maxn], tot; n int main() { n   scanf( %d , &n); n   cnt[0] = 1; n   for (int i = 1, x; i <= n; ++i) { n     scanf( %d , &x), tot = 0; n     for (int j = 1; j * j <= x; ++j) { n       if (x % j == 0) { n         vec[++tot] = j; n         if (j * j != x && x / j <= n) n           cnt[x / j] = Add(cnt[x / j], cnt[x / j - 1]); n       } n     } n     for (int j = tot; j; --j) n       if (vec[j] <= n) cnt[vec[j]] = Add(cnt[vec[j]], cnt[vec[j] - 1]); n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) ans = Add(ans, cnt[i]); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n int main() { n   int i = 1, n, m; n   scanf( %d%d , &n, &m); n   while (1) { n     if (m < i) break; n     m -= i; n     i = i % n + 1; n   } n   printf( %d n , m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n, x; n     cin >> n >> x; n     long long int a[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a, a + n); n     int j = 1, teams = 0; n     for (int i = n - 1; i >= 0; i--) { n       if (a[i] * j >= x) { n         teams++; n         j = 0; n       } n       j++; n     } n     cout << teams << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 1; n const long long LIMIT = 2e18 + 1; n int n; n long long k; n long long arr[N]; n long long cnt[N]; n bool isUnsafe(long long a, long long b) { n   return a > LIMIT / b || b > LIMIT / a; n } n int solve(int i) { n   int res = 0; n   long long s, p; n   if (arr[i] != 1) { n     s = k * arr[i]; n     p = arr[i]; n     res += (s == p); n   } else { n     s = k * cnt[i]; n     p = 1; n     res += (k == 1); n   } n   for (++i; i < n; ++i) { n     if (arr[i] == 1) { n       long long sOld = s; n       s += k * cnt[i]; n       if (sOld < p && s >= p && (p - sOld) % k == 0) { n         ++res; n       } n     } else { n       if (isUnsafe(p, arr[i])) { n         break; n       } n       p *= arr[i]; n       s += k * arr[i]; n       if (s == p) { n         ++res; n       } n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n     if (i > 0 && arr[i] == 1 && arr[i - 1] == 1) { n       --i; n       --n; n     } n     ++cnt[i]; n   } n   long long res = 0; n   for (int i = 0; i < n; ++i) { n     res += solve(i); n     if (arr[i] == 1) { n       --cnt[i]; n       if (cnt[i]) { n         --i; n       } n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long double pi = acos(-1.0); n const long double eps = 1e-9; n const int MAX_SIZE = 50; n const int dX[] = {-1, 1, 0, 0, -1, 1, -1, 1}; n const int dY[] = {0, 0, -1, 1, 1, -1, -1, 1}; n template <class S, class T> n ostream& operator<<(ostream& o, const pair<S, T>& a) { n   o << a.first <<     << a.second; n   return o; n } n template <class S, class T> n ostream& operator<<(ostream& o, const map<S, T>& a) { n   for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) n     o << (*it) <<   n ; n   return o; n } n template <class S> n ostream& operator<<(ostream& o, const vector<S>& a) { n   for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) n     o << *it <<    ; n   return o; n } n template <class S> n ostream& operator<<(ostream& o, const vector<vector<S> >& a) { n   for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) { n     o << (*it) <<   n ; n   } n   return o; n } n template <class S> n ostream& operator<<(ostream& o, const set<S>& a) { n   for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) n     o << *it <<    ; n   return o; n } n const long long MAX = 2005; n int a[MAX][MAX]; n long long diagl[MAX * 2], diagr[MAX * 2]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   int i, j, n, x1, y1, x2, y2; n   long long val, max1 = 0, max2 = 0; n   scanf( %d , &n); n   for (i = (0); (((1) > 0) - ((1) < 0)) * i < (((1) > 0) - ((1) < 0)) * (n); n        i += (1)) { n     for (j = (0); (((1) > 0) - ((1) < 0)) * j < (((1) > 0) - ((1) < 0)) * (n); n          j += (1)) { n       scanf( %d , &a[i][j]); n       diagl[i + j] += a[i][j]; n       diagr[n - (i - j) - 1] += a[i][j]; n     } n   } n   for (i = (0); (((1) > 0) - ((1) < 0)) * i < (((1) > 0) - ((1) < 0)) * (n); n        i += (1)) { n     for (j = ((i % 2)); n          (((2) > 0) - ((2) < 0)) * j < (((2) > 0) - ((2) < 0)) * (n); n          j += (2)) { n       val = diagl[i + j] + diagr[n - (i - j) - 1] - a[i][j]; n       if (val >= max1) { n         max1 = val; n         x1 = i + 1; n         y1 = j + 1; n       } n     } n     for (j = ((1 - (i % 2))); n          (((2) > 0) - ((2) < 0)) * j < (((2) > 0) - ((2) < 0)) * (n); n          j += (2)) { n       val = diagl[i + j] + diagr[n - (i - j) - 1] - a[i][j]; n       if (val >= max2) { n         max2 = val; n         x2 = i + 1; n         y2 = j + 1; n       } n     } n   } n   printf( %I64 d n%d %d %d %d , max1 + max2, x1, y1, x2, y2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 260; n const int MOD = 1000000000 + 7; n int n, m, t; n int mid; n int v[N], mod[N]; n bool g[10][N]; n long long ans, f[10]; n inline void Dfs(int k, int d) { n   ans += f[d]; n   if (ans > MOD) ans -= MOD; n   if (d > 5) return; n   for (int i = k; i <= mid; i++) n     if (v[i] && !g[d][i] && !g[d][m - i]) { n       if (d < 5) { n         for (int j = 0; j < m; j++) g[d + 1][j] = g[d][j]; n         for (int j = 0; j < m; j++) n           if (g[d][j]) g[d + 1][mod[j + i]] = g[d + 1][mod[j + m - i]] = 1; n       } n       f[d + 1] = (f[d] * v[i]) % MOD; n       Dfs(i + 1, d + 1); n     } n } n int main() { n   int x; n   for (scanf( %d %d %d , &n, &m, &t); t--; v[((x) < (m - x) ? (x) : (m - x))]++) n     scanf( %d , &x), x %= m; n   mid = m / 2; n   for (int i = 0; i <= 2 * m; i++) mod[i] = i % m; n   f[0] = 1; n   g[0][0] = 1; n   Dfs(1, 0); n   printf( %d n , (int)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, h, l, r; n int a[2000 + 5], dp[2000 + 5][2000 + 5]; n int solve(int curr, int idx) { n   if (idx == n) { n     if (l <= curr && curr <= r) n       return 1; n     else n       return 0; n   } n   if (dp[curr][idx] != -1) return dp[curr][idx]; n   int p1 = 0, p2 = 0; n   if (l <= curr && curr <= r) { n     p1 = 1; n     p2 = 1; n   } n   p1 += solve((curr + a[idx]) % h, idx + 1); n   p2 += solve((curr + a[idx] - 1) % h, idx + 1); n   return dp[curr][idx] = max(p1, p2); n } n int main(int argc, const char* argv[]) { n   scanf( %d %d %d %d , &n, &h, &l, &r); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i <= h; i++) { n     for (int j = 0; j <= n; j++) { n       dp[i][j] = -1; n     } n   } n   int k = solve(a[0], 1); n   for (int i = 0; i <= h; i++) { n     for (int j = 0; j <= n; j++) { n       dp[i][j] = -1; n     } n   } n   k = max(k, solve(a[0] - 1, 1)); n   printf( %d n , k); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e6 + 10; n const int maxm = 1e5 + 10; n const long long int mod = 1e9 + 7; n const long long int INF = 1e18 + 100; n const int inf = 0x3f3f3f3f; n const double pi = acos(-1.0); n const double eps = 1e-8; n using namespace std; n int n, m; n int cas, tol, T; n struct Sam { n   int node[maxn << 1][27], step[maxn << 1], fa[maxn << 1]; n   int dp[maxn << 1], tax[maxn << 1], gid[maxn << 1]; n   int vis[maxn << 1]; n   int last, sz; n   int newnode() { n     memset(node[++sz], 0, sizeof node[++sz]); n     dp[sz] = step[sz] = fa[sz] = 0; n     return sz; n   } n   void init() { n     sz = 0; n     last = newnode(); n   } n   void insert(int k) { n     int p = last, np = last = newnode(); n     dp[np] = 1; n     step[np] = step[p] + 1; n     for (; p && !node[p][k]; p = fa[p]) node[p][k] = np; n     if (p == 0) { n       fa[np] = 1; n     } else { n       int q = node[p][k]; n       if (step[q] == step[p] + 1) { n         fa[np] = q; n       } else { n         int nq = newnode(); n         memcpy(node[nq], node[q], sizeof(node[q])); n         fa[nq] = fa[q]; n         step[nq] = step[p] + 1; n         fa[np] = fa[q] = nq; n         for (; p && node[p][k] == q; p = fa[p]) node[p][k] = nq; n       } n     } n   } n   void handle() { n     for (int i = 0; i <= sz; i++) tax[i] = 0; n     for (int i = 1; i <= sz; i++) tax[step[i]]++; n     for (int i = 1; i <= sz; i++) tax[i] += tax[i - 1]; n     for (int i = 1; i <= sz; i++) gid[tax[step[i]]--] = i; n     for (int i = sz; i >= 1; i--) { n       int u = gid[i]; n       dp[fa[u]] += dp[u]; n     } n   } n   int solve(char *s, int len, int id) { n     int p = 1, res = 0; n     int ans = 0; n     for (int i = 1; i <= len + len; i++) { n       int k = s[i] -  a  + 1; n       while (p && !node[p][k]) { n         p = fa[p]; n         res = step[p]; n       } n       if (p == 0) { n         p = 1; n         res = 0; n       } else { n         p = node[p][k]; n         res++; n         if (res >= len) { n           int tmp = p; n           while (tmp && vis[tmp] != id && n                  !(step[fa[tmp]] + 1 <= len && len <= step[tmp])) { n             vis[tmp] = id; n             tmp = fa[tmp]; n           } n           if (vis[tmp] != id) { n             vis[tmp] = id; n             ans += dp[tmp]; n           } n         } n       } n     } n     return ans; n   } n } sam; n char s[maxn], t[maxn << 1]; n int main() { n   scanf( %s , s + 1); n   int slen = strlen(s + 1); n   sam.init(); n   for (int i = 1; i <= slen; i++) { n     sam.insert(s[i] -  a  + 1); n   } n   sam.handle(); n   scanf( %d , &T); n   for (int tt = 1; tt <= T; tt++) { n     scanf( %s , t + 1); n     int tlen = strlen(t + 1); n     for (int i = 1; i <= tlen; i++) { n       t[i + tlen] = t[i]; n     } n     t[tlen + tlen + 1] =   0 ; n     int ans = sam.solve(t, tlen, tt); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n string tostring(T x) { n   ostringstream out; n   out << x; n   return out.str(); n } n long long toint(string s) { n   istringstream in(s); n   long long x; n   in >> x; n   return x; n } n int main() { n   int n, x, tmp; n   cin >> n >> x; n   int ans = 0, zero = 0; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     ans += tmp; n   } n   if (ans == 0) { n     cout << 0 << endl; n   } else { n     ans = ((ans < 0) ? -(ans) : (ans)); n     if (ans % x == 0) { n       cout << ans / x << endl; n     } else { n       cout << ans / x + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void past_code(); n vector<int> v[100005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int x, y, j, k; n   cin >> x; n   for (int i = 1; i <= 2 * x; i++) { n     cin >> y; n     v[y].push_back(i); n   } n   long long sum = 0; n   j = 1; n   k = 1; n   for (int i = 1; i <= x; i++) { n     sum += abs(j - v[i][0]) + abs(k - v[i][1]); n     j = v[i][0]; n     k = v[i][1]; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n = 0, s = 0, d = 0; n map<int, vector<int> > graph; n map<int, bool> vis; n bool stop = false; n void Dfs(int m) { n   if (m == d || stop) { n     cout << m; n     stop = true; n     return; n   } n   cout << m <<    ; n   for (int i = 0; i < (int)graph[m].size(); i++) { n     if (vis.find(graph[m][i]) == vis.end()) { n       vis[graph[m][i]] = true; n       Dfs(graph[m][i]); n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s >> d; n     graph[s].push_back(d); n     graph[d].push_back(s); n   } n   vector<int> k; n   for (map<int, vector<int> >::iterator it = graph.begin(); it != graph.end(); n        it++) { n     if ((*it).second.size() == 1) { n       k.push_back((*it).first); n     } n   } n   s = k[0], d = k[1]; n   vis[s] = true; n   Dfs(s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(51); n const long long INF = 1e18; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     vector<vector<long long>> a(n, vector<long long>(m)), n         pr(n, vector<long long>(m)); n     vector<vector<long long>> mn(m, vector<long long>(m, INF)); n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         char c; n         cin >> c; n         a[i][j] = c -  0 ; n         pr[i][j] = a[i][j] + (j > 0 ? pr[i][j - 1] : 0); n       } n     } n     long long anss = 1e18; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         for (long long jj = j; jj < m; jj++) { n           if (jj - j + 1 >= 4) { n             if (i > 0) n               mn[j][jj] += (a[i - 1][j] ^ 1) + (a[i - 1][jj] ^ 1) + n                            (pr[i - 1][jj - 1] - pr[i - 1][j]); n             if (i - 4 >= 0) { n               long long add = 0; n               for (long long f = i - 3; f < i; f++) { n                 add += n                     (a[f][j] ^ 1) + (a[f][jj] ^ 1) + (pr[f][jj - 1] - pr[f][j]); n               } n               mn[j][jj] = n                   min(mn[j][jj], n                       (jj - j - 1) - (pr[i - 4][jj - 1] - pr[i - 4][j]) + add); n             } n             anss = min(anss, n                        mn[j][jj] + (jj - j - 1) - (pr[i][jj - 1] - pr[i][j])); n           } n         } n       } n     } n     cout << anss << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e6 + 7; n inline long long stoll(char* s, const long long& p, bool f = 0) { n   long long a = 0; n   bool fl = 0; n   for (; *s; ++s) a = a * 10 + *s - 48, fl |= (f && a >= p), a %= p; n   return a + fl * p; n } n long long b, n, p, phi; n char B[M], N[M]; n inline long long fp(long long x, long long k, const long long& p) { n   long long r = 1; n   for (; k; k >>= 1, x = x * x % p) n     if (k & 1) r = r * x % p; n   return r; n } n inline long long getphi(long long n) { n   long long r = n, nn = n; n   for (long long i = 2; i * i <= nn; ++i) n     if (!(n % i)) n       for (r = r / i * (i - 1); !(n % i); n /= i) n         ; n   if (n > 1) r = r / n * (n - 1); n   return r; n } n inline long long mod(long long x, long long p) { n   return x < p ? x : x % p + p; n }; n inline char* N_1() { n   register int s = strlen(N) - 1; n   for (; s >= 0 && !N[s]; --s) n     ; n   --N[s]; n   return N; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> B >> N >> p; n   phi = getphi(p); n   b = stoll(B, p), n = stoll(N_1(), phi, 1); n   long long ans = ((b - 1) * fp(b, n, p) + p) % p; n   if (!(ans)) ans = p; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d; n   pair<int, int> friendlist[100001]; n   scanf( %d%d , &n, &d); n   for (int i = 0; i < n; i++) n     scanf( %d%d , &((friendlist + i)->first), &((friendlist + i)->second)); n   sort(friendlist, friendlist + n); n   int it_left = 0; n   int it_right = 0; n   long long maxfriendship = 0; n   long long friendship = 0; n   while (true) { n     while (it_right < n && n            friendlist[it_right].first - friendlist[it_left].first < d) { n       friendship += friendlist[it_right].second; n       maxfriendship = max(maxfriendship, friendship); n       it_right++; n     } n     if (it_right >= n) break; n     while (friendlist[it_right].first - friendlist[it_left].first >= d) { n       friendship -= friendlist[it_left].second; n       it_left++; n     } n   } n   cout << maxfriendship; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 * 1000 + 7, MAX_LOG = 20; n int n, m; n vector<int> adj[MAXN]; n bool isPassed[MAXN]; n int par[MAXN][MAX_LOG], cycleCnt[MAXN], h[MAXN]; n void pre(int root) { n   isPassed[root] = true; n   for (auto nei : adj[root]) { n     if (!isPassed[nei]) { n       par[nei][0] = root; n       h[nei] = h[root] + 1; n       pre(nei); n       cycleCnt[root] = cycleCnt[root] + cycleCnt[nei]; n     } else if (par[root][0] != nei and h[nei] < h[root]) { n       cycleCnt[nei]--; n       cycleCnt[root]++; n     } n   } n   return; n } n int nanCyCnt[MAXN][MAX_LOG]; n void dfs(int root, int lstRt) { n   par[root][0] = lstRt; n   if (lstRt != -1 and cycleCnt[root] == 0) nanCyCnt[root][0] = 1; n   for (int i = 1; i < MAX_LOG and h[root] >= (1 << i) and n                   par[par[root][i - 1]][i - 1] != -1; n        ++i) { n     par[root][i] = par[par[root][i - 1]][i - 1]; n     nanCyCnt[root][i] = n         nanCyCnt[root][i - 1] + nanCyCnt[par[root][i - 1]][i - 1]; n   } n   isPassed[root] = true; n   for (auto nei : adj[root]) { n     if (!isPassed[nei]) { n       h[nei] = h[root] + 1; n       dfs(nei, root); n     } n   } n   return; n } n int lca(int v, int u) { n   int nancy = 0; n   if (h[v] > h[u]) swap(v, u); n   for (int i = 0; h[v] ^ h[u]; ++i) { n     if ((h[u] - h[v]) & (1 << i)) { n       nancy += nanCyCnt[u][i]; n       u = par[u][i]; n     } n   } n   if (v == u) return nancy; n   for (int i = MAX_LOG - 1; i >= 0; --i) { n     if (par[v][i] != par[u][i]) { n       nancy += (nanCyCnt[v][i] + nanCyCnt[u][i]); n       v = par[v][i], u = par[u][i]; n     } n   } n   return nancy + nanCyCnt[v][0] + nanCyCnt[u][0]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(par, -1, sizeof par); n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int v, u; n     cin >> v >> u, v--, u--; n     adj[v].push_back(u); n     adj[u].push_back(v); n   } n   memset(isPassed, 0, sizeof isPassed); n   pre(0); n   memset(isPassed, 0, sizeof isPassed); n   memset(h, 0, sizeof h); n   dfs(0, -1); n   int mercsNum; n   cin >> mercsNum; n   while (mercsNum--) { n     int sh, wa; n     cin >> wa >> sh, wa--, sh--; n     cout << lca(wa, sh) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool isPrime(uint64_t n) { n   if (n == 1) return false; n   if (n == 2) return true; n   if (n % 2 == 0) return false; n   for (uint64_t div = 3; div * div <= n; div += 2) n     if (n % div == 0) return false; n   return true; n } n inline std::vector<uint64_t> generatePrimesUntil(uint64_t upperBound) { n   if (upperBound < 2) return {}; n   std::vector<uint64_t> result = {2}; n   for (uint64_t candidate = 3; candidate <= upperBound; candidate += 2) { n     bool divisibleByPrevious = false; n     for (size_t divisorIndex = 1; n          divisorIndex < result.size() && n          result[divisorIndex] * result[divisorIndex] <= candidate; n          ++divisorIndex) { n       if (candidate % result[divisorIndex] == 0) { n         divisibleByPrevious = true; n         break; n       } n     } n     if (!divisibleByPrevious) result.push_back(candidate); n   } n   return result; n } n inline std::vector<std::pair<uint64_t, uint64_t>> decomposeToPrimePowers( n     uint64_t n) { n   std::vector<std::pair<uint64_t, uint64_t>> result; n   auto tryPrime = [&](uint64_t p) { n     if (n % p == 0) { n       result.push_back({p, 0}); n       while (n % p == 0) { n         n /= p; n         result.back().second++; n       } n     } n   }; n   tryPrime(2); n   for (uint64_t primeCandidate = 3; primeCandidate * primeCandidate <= n; n        primeCandidate += 2) n     tryPrime(primeCandidate); n   if (n > 1) result.push_back({n, 1}); n   return result; n } n template <typename T> n void printVector(const std::vector<T>& out, std::string delimiter =    ) { n   if (out.empty()) return; n   for (size_t i = 0; i < out.size() - 1; ++i) std::cout << out[i] << delimiter; n   std::cout << out.back(); n } n class Solver576A { n  public: n   void run(); n }; n void Solver576A::run() { n   int n; n   cin >> n; n   auto primes = generatePrimesUntil(n); n   vector<int> ans; n   for (auto p : primes) n     for (int pp = p; pp <= n; pp *= p) ans.push_back(pp); n   cout << ans.size() <<   n ; n   printVector(ans); n } n using CurrentSolver = Solver576A; n int main() { n   ios::sync_with_stdio(false); n   CurrentSolver().run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007, BIG = 0x3f3f3f3f; n long long p, k; n inline long long Mod(long long x, long long m) { n   x %= m; n   if (x < 0) x += m; n   return x; n } n vector<long long> Process() { n   vector<long long> ans; n   while (p) { n     const long long m = Mod(p, k); n     ans.push_back(m); n     p -= m; n     p /= -k; n   } n   return ans; n } n void Output() { n   const vector<long long> ans = Process(); n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   while (cin >> p) { n     cin >> k; n     Output(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pow1(long long b, long long expo, long long md) { n   if (!expo) return 1; n   long long tmp = pow1(b, expo / 2, md); n   tmp = (tmp * tmp) % md; n   if (expo % 2) tmp = (tmp * b) % md; n   return tmp; n } n long long w[1000005] = {}; n long long apow[1000005] = {}; n long long inv[1000005] = {}; n vector<long long> v[1000005] = {}; n long long get(long long a, long long m1, long long b, long long m2, n               long long x) { n   long long ret = 0; n   long long first, k; n   k = (a - b) * inv[m2]; n   k %= m1; n   k += m1; n   k %= m1; n   first = k * m2 + b; n   first %= m1 * m2; n   x -= first; n   if (x >= 0) { n     ret += 1 + x / (m1 * m2); n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(15); n   long long md, a, b, x, per; n   long long ans = 0; n   cin >> a >> b >> md >> x; n   apow[0] = 1; n   v[1].push_back(0); n   if (a != 1) { n     apow[1] = a; n     v[a].push_back(1); n     for (long long f = (long long)2; f <= (long long)md; ++f) { n       apow[f] = a * apow[f - 1]; n       apow[f] %= md; n       if (apow[f] == 1) { n         per = f; n         break; n       } n       v[apow[f]].push_back(f); n     } n   } else { n     per = 1; n   } n   for (long long f = (long long)1; f <= (long long)md - 1; ++f) { n     inv[f] = pow1(f, md - 2, md); n   } n   for (long long f = (long long)1; f <= (long long)md - 1; ++f) { n     w[f] = (inv[f] * b) % md; n   } n   for (long long f = (long long)1; f <= (long long)md - 1; ++f) { n     if ((long)v[w[f]].size()) { n       for (long long ff = (long long)0; n            ff <= (long long)(long)v[w[f]].size() - 1; ++ff) { n         ans += get(f, md, v[w[f]][ff], per, x); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, long long> x, y; n map<pair<int, int>, long long> xy; n int main() { n   int n; n   cin >> n; n   while (n--) { n     int xx, yy; n     cin >> xx >> yy; n     x[xx]++; n     y[yy]++; n     xy[make_pair(xx, yy)]++; n   } n   long long ans = 0; n   for (auto i : x) { n     ans += i.second * (i.second - 1) / 2; n   } n   for (auto i : y) { n     ans += i.second * (i.second - 1) / 2; n   } n   for (auto i : xy) { n     ans -= i.second * (i.second - 1) / 2; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, ans; n int main() { n   cin >> a >> b >> c; n   long long pom = min(a, b); n   a -= pom; n   b -= pom; n   ans = c + pom; n   ans <<= 1; n   if (a or b) ans++; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n vector<long long> v; n int main() { n   if (fopen( a.txt ,  r )) { n     freopen( a.txt ,  r , stdin); n   } else n     ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   if (k >= 1e6) { n     cout << -1; n     exit(0); n   }; n   long long temp = (k * (k + 1)) / 2; n   if (k == 1) { n     cout << n; n     return 0; n   } n   if (n == 1) { n     cout << -1; n     exit(0); n   }; n   if (temp > n) { n     cout << -1; n     exit(0); n   }; n   for (long long i = 1; i * i <= n; i++) { n     if (n % i != 0) continue; n     v.push_back(i); n     if (i * i < n) v.push_back(n / i); n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   for (long long x : v) { n     long long a = n / x; n     if (a >= temp) { n       for (int i = 1; i <= k - 1; i++) cout << i * x <<    , a -= i; n       cout << a * x; n       return 0; n     } n   } n } n


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


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


#include <bits/stdc++.h> n using namespace std; n int n, m, u, v, s, to[5010], ne[5010], pre[5010], cnt, dfn[5010], low[5010], n     len, f[5010], ss, q[5010], w[5010], d[5010], dis[5010]; n bool us[5010]; n void add(int u, int v) { n   to[++s] = v; n   ne[s] = pre[u]; n   pre[u] = s; n } n void dfs(int k) { n   dfn[k] = low[k] = ++cnt; n   f[++len] = k; n   us[k] = true; n   for (int i = pre[k]; i; i = ne[i]) { n     int x = to[i]; n     if (!dfn[x]) { n       dfs(x); n       low[k] = min(low[k], low[x]); n     } else if (us[x]) n       low[k] = min(low[k], low[x]); n   } n   if (dfn[k] == low[k]) { n     ss++; n     while (f[len] != k) us[f[len]] = false, w[ss]++, q[f[len--]] = ss; n     us[k] = false; n     w[ss]++; n     q[f[len--]] = ss; n   } n } n int bfs(int k, int u) { n   int h = 0, t = 1; n   f[1] = u; n   memset(dis, 0, sizeof(dis)); n   while (h < t) { n     int x = f[++h]; n     for (int i = pre[x]; i; i = ne[i]) n       if (q[to[i]] == k && !dis[to[i]]) { n         int o = to[i]; n         dis[o] = dis[x] + 1; n         if (o == u) return dis[o]; n         f[++t] = o; n       } n   } n } n int get(int k) { n   if (w[k] == 1) return 0; n   int mn = 1e9; n   for (int i = 1; i <= n; i++) n     if (q[i] == k) mn = min(mn, bfs(k, i)); n   return 1 + 998 * mn; n } n int main() { n   cin >> n >> m; n   while (m--) scanf( %d%d , &u, &v), add(u, v); n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) dfs(i); n   for (int i = 1; i <= n; i++) n     for (int j = pre[i]; j; j = ne[j]) n       if (q[i] != q[to[j]]) d[q[i]]++; n   int ans = n; n   for (int i = 1; i <= ss; i++) n     if (!d[i]) ans += get(i); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int L = 2e5; n vector<vector<int>> g(L); n vector<int> dp(L), d(L, 0), par(L, -1); n vector<bool> used(L); n int dist; n int ask1(int u) { n   cout <<  d   << u + 1 << endl; n   int t; n   cin >> t; n   if (t == -1) { n     while (true) { n     } n   } n   return t; n } n int ask2(int u) { n   cout <<  s   << u + 1 << endl; n   int t; n   cin >> t; n   --t; n   if (t == -1) { n     while (true) { n     } n   } n   return t; n } n void dfs(int u) { n   used[u] = true; n   for (auto v : g[u]) { n     if (!used[v]) { n       d[v] = d[u] + 1; n       par[v] = u; n       dfs(v); n       dp[u] += dp[v]; n     } n   } n   if (d[u] == dist) dp[u]++; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     --u; n     --v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dist = ask1(0); n   dfs(0); n   int q = 0; n   if (dist == 0) { n     cout <<  ! 1  << endl; n     return 0; n   } else { n     while (d[q] != dist) { n       int r = q; n       while (d[r] != dist) { n         int t = -1; n         int ma = 0; n         for (auto v : g[r]) { n           if (d[v] > d[r]) { n             if (dp[v] > ma) { n               ma = dp[v]; n               t = v; n             } n           } n         } n         r = t; n       } n       int newdist = ask1(r); n       if (newdist == 0) { n         cout <<  !   << r + 1 << endl; n         return 0; n       } else { n         while (d[r] * 2 != 2 * dist - newdist) { n           r = par[r]; n         } n         q = ask2(r); n       } n     } n     cout <<  !   << q + 1 << endl; n     return 0; n   } n   cin >> n; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long LINF = 1e18; n const int MOD = 1e9 + 7; n void test() {} n const int MAX = 1 << 19; n int tree[2 * MAX], prefMax[2 * MAX]; n int killed = 0, m, n; n vector<pair<int, int> > play; n int query(int x, int y) { n   int res = -1; n   for (x += MAX, y += MAX; x < y; x >>= 1, y >>= 1) { n     if (x & 1) res = max(tree[x++], res); n     if (y & 1) res = max(tree[--y], res); n   } n   return res; n } n void update1(int x) { n   x += MAX; n   while (x) { n     tree[x] = 0; n     x /= 2; n   } n } n void update2(int x, int val) { n   x += MAX; n   while (x) { n     tree[x] = max(tree[x], val); n     x /= 2; n   } n } n bool check(int x) { n   int l = -1, r = m; n   int maks = query(killed, killed + x + 1); n   while (l + 1 < r) { n     int mid = (l + r) / 2; n     if (play[mid].first >= maks) n       r = mid; n     else n       l = mid; n   } n   if (r == m) return false; n   if (prefMax[r] > x) n     return true; n   else n     return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 0; i <= n; i++) update1(i); n     for (int i = 0; i < n; i++) { n       int x; n       cin >> x; n       update2(i, x); n     } n     cin >> m; n     play = vector<pair<int, int> >(m); n     for (int i = 0; i < m; i++) cin >> play[i].first >> play[i].second; n     sort(play.begin(), play.end()); n     for (int i = m; i >= 0; i--) prefMax[i] = 0; n     for (int i = m - 1; i >= 0; i--) n       prefMax[i] = max(prefMax[i + 1], play[i].second); n     killed = 0; n     int res = 0; n     bool czy = true; n     while (killed < n) { n       int l = -1, r = n - killed + 1; n       while (l + 1 < r) { n         int mid = (l + r) / 2; n         if (check(mid)) n           l = mid; n         else n           r = mid; n       } n       if (l == -1) { n         cout << -1 <<   n ; n         czy = false; n         break; n       } n       killed += (l + 1); n       res++; n     } n     if (czy) cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 6e6; n const long long inf = 1e18; n const long long maxv = 2e8 + 100; n const int miniinf = 1e9; n const long double eps = 1e-6; n const long long flowconst = 1e9; n void init() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   init(); n   int n; n   cin >> n; n   vector<string> ans1; n   vector<string> ans2; n   if (n == 1) { n     cout <<  ? 1 1  << endl; n     string ans; n     cin >> ans; n     cout <<  !   << ans << endl; n     return 0; n   } n   map<string, int> foo; n   cout <<  ? 1   << n << endl; n   for (int i = 0; i < n * (n + 1) / 2; i++) { n     string x; n     cin >> x; n     sort(x.begin(), x.end()); n     foo[x]++; n   } n   cout <<  ? 1   << n - 1 << endl; n   for (int i = 0; i < (n - 1) * (n) / 2; i++) { n     string x; n     cin >> x; n     sort(x.begin(), x.end()); n     foo[x]--; n   } n   vector<string> diff; n   for (auto ev : foo) n     if (ev.second == 1) diff.push_back(ev.first); n   sort(diff.begin(), diff.end(), n        [&](string &a, string &b) { return a.size() < b.size(); }); n   string ans; n   for (int i = 0; i < n; i++) { n     vector<int> cnt(26, 0); n     for (int j = 0; j < diff[i].size(); j++) { n       cnt[diff[i][j] -  a ]++; n     } n     for (int j = 0; j < ans.size(); j++) { n       cnt[ans[j] -  a ]--; n     } n     for (int j = 0; j < 26; j++) { n       if (cnt[j] > 0) { n         ans +=  a  + j; n       } n     } n   } n   reverse(ans.begin(), ans.end()); n   cout <<  !  ; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100; n const int M = 26; n const long long mod = 1e9 + 7; n const long long MOD = 998244353; n const int P = 1336; n const long double eps = 0.000000001; n const long long inf = 1e18 + 7; n mt19937 gen(time(0)); n vector<int> g[N]; n int sz[N], a[N]; n int ask(int v, int u) { n   cout <<  ?   << v + 1 <<     << u + 1 << endl; n   int c; n   cin >> c; n   if (c == -1) exit(0); n   c--; n   return c; n } n void answ(int v) { n   cout <<  !   << v + 1 << endl; n   exit(0); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n     sz[x]++; n     sz[y]++; n   } n   for (int i = 0; i < n; i++) { n     a[i] = 1; n   } n   int can = n / 2; n   while (can--) { n     int v = -1, u = -1; n     for (int i = 0; i < n; i++) { n       if (sz[i] == 1) { n         if (v == -1) n           v = i; n         else if (u == -1) n           u = i; n       } n     } n     for (auto to : g[v]) { n       sz[to]--; n     } n     for (auto to : g[u]) { n       sz[to]--; n     } n     sz[v]--; n     sz[u]--; n     int c = ask(v, u); n     if (c == v) answ(v); n     if (c == u) answ(u); n     a[v] = 0; n     a[u] = 0; n   } n   for (int i = 0; i < n; i++) { n     if (a[i]) answ(i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool changed[222222]; n pair<int, int> edges[222222]; n set<pair<int, int> > adj[111111]; n bool visited[111111]; n deque<pair<int, int> > to_connect; n vector<int> Harry; n char outbuf[12000000]; n char* out = outbuf; n int moves = 0; n void flush_harry() { n   out += sprintf(out,  %d , Harry.size()); n   for (int v : Harry) { n     out += sprintf(out,   %d , v); n   } n   out += sprintf(out,   n ); n   Harry.clear(); n } n void move_stairs(int id, int u, int v) { n   ++moves; n   flush_harry(); n   adj[edges[id].first].erase(pair<int, int>(edges[id].second, id)); n   adj[edges[id].second].erase(pair<int, int>(edges[id].first, id)); n   edges[id].first = u, edges[id].second = v; n   adj[u].insert(pair<int, int>(v, id)); n   adj[v].insert(pair<int, int>(u, id)); n   out += sprintf(out,  %d %d %d n , id, u, v); n   changed[id] = true; n } n vector<int> in_component; n bool dfs1(int v, int parent_edge, bool connected_to_one) { n   visited[v] = true; n   in_component.push_back(v); n   int edge_to_move = -1; n   for (auto& p : adj[v]) { n     if (!connected_to_one && visited[p.first] && parent_edge != p.second) { n       edge_to_move = p.second; n       connected_to_one = true; n     } else if (!visited[p.first]) { n       connected_to_one = dfs1(p.first, p.second, connected_to_one); n     } n   } n   if (edge_to_move != -1) { n     move_stairs(edge_to_move, v, 1); n   } n   return connected_to_one; n } n void dfs2(int v, int parent_edge) { n   if (v != 1) Harry.push_back(v); n   visited[v] = true; n   for (auto& p : adj[v]) n     if (!visited[p.first]) { n       dfs2(p.first, p.second); n       Harry.push_back(v); n     } n   vector<pair<int, int> > to_change; n   for (auto& p : adj[v]) n     if (p.second != parent_edge && !to_connect.empty() && !changed[p.second]) { n       to_change.push_back(pair<int, int>(p.second, to_connect.back().second)); n       to_connect.pop_back(); n     } n   for (auto& p : to_change) { n     move_stairs(p.first, v, p.second); n     dfs2(p.second, p.first); n     Harry.push_back(v); n   } n } n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   for (int i = (0); i < (M); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     adj[a].insert(pair<int, int>(b, i + 1)); n     adj[b].insert(pair<int, int>(a, i + 1)); n     edges[i + 1].first = a, edges[i + 1].second = b; n   } n   Harry.push_back(1); n   for (int i = (1); i < (N + 1); ++i) n     if (!visited[i]) { n       bool connected_to_one = dfs1(i, -1, i == 1); n       if (!connected_to_one) { n         for (int& v : in_component) n           if (adj[v].size() <= 1) { n             to_connect.push_back(pair<int, int>(in_component.size(), v)); n             break; n           } n       } n       in_component.clear(); n     } n   sort(to_connect.begin(), to_connect.end()); n   for (int i = (1); i < (N + 1); ++i) visited[i] = false; n   dfs2(1, -1); n   while (!to_connect.empty() && to_connect.back().first > 1) { n     int v = to_connect.back().second; n     to_connect.push_front(pair<int, int>(1, v)); n     to_connect.pop_back(); n     pair<int, int> n = *adj[v].begin(); n     move_stairs(n.second, 1, n.first); n     dfs2(n.first, n.second); n     Harry.push_back(1); n   } n   flush_harry(); n   if (!to_connect.empty()) { n     printf( NO n ); n     return 0; n   } n   printf( YES n%d n , moves); n   printf( %s n , outbuf); n } n


/* n  * n  * Author: Hsueh- n  * Date: 2021-03-05 21:34:52 n  * n  * */ n  n #include <bits/stdc++.h> n  n using namespace std; n  n #define dbg(x...)                                                                n     do {                                                                         n         cout << #x <<   ->  ;                                                    n         err(x);                                                                  n     } while (0) n void err() { cout << endl; } n template <class T, class... Ts> void err(const T &arg, const Ts &...args) { n     cout << arg <<    ; n     err(args...); n } n  n #define endl   n  n #define all(A) A.begin(), A.end() n using ll = long long; n using db = double; n using pII = pair<int, int>; n  n const int INF = 0x3f3f3f3f; n const ll INFLL = 0x3f3f3f3f3f3f3f3f; n const int N = 2e5 + 10; n  n int n, m; n int a[N]; n  n void RUN() { n     cin >> n >> m; n     map<ll, int> mp; n     ll sum = 0, Max = 0; n     for (int i = 1; i <= n; ++i) { n         cin >> a[i]; n         sum += a[i]; n         if (sum > Max) { n             Max = sum; n             mp[sum] = i; n         } n     } n     for (int i = 1; i <= m; ++i) { n         ll x; n         cin >> x; n         auto it = mp.lower_bound(x); n         ll res = 0; n         if (it == mp.end() && sum <= 0) { n             res = -1; n         } else if (it == mp.end()) { n             auto last = mp.end(); n             last--; n             ll need = (x - last->first + sum - 1) / sum; n             it = mp.lower_bound(x - need * sum); n             res = it->second + need * n - 1; n         } else { n             res = it->second - 1; n         } n         cout << res <<    n [i == m]; n     } n } n  n int main() { n     ios::sync_with_stdio(false); n     cin.tie(nullptr), cout.tie(nullptr); n     cout << fixed << setprecision(20); n  n     int T; n     cin >> T; n     while (T--) { n         RUN(); n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)2e18 + 5; n const long long MOD = (long long)1e9 + 7; n const int MAXN = 55; n long long mul(long long x, long long y) { n   if (x == 0) return 0; n   if (y > INF / x) return INF; n   return x * y; n } n long long add(long long x, long long y) { return min(x + y, INF); } n long long n, k, cnt[MAXN], fac[MAXN], dp[MAXN][MAXN], way[MAXN]; n vector<int> get_p(long long len, long long k) { n   vector<int> res, from(len + 1); n   res.push_back(len); n   for (int i = 1; i <= len; i++) { n     from[i] = i; n   } n   for (int i = 2; i <= len; i++) { n     int src = i; n     while (from[src] != src) { n       src = from[src]; n     } n     int tgt = k / fac[max(0LL, len - i - 1)]; n     for (int j = 1; j <= len; j++) { n       if (j != src && from[j] == j) { n         if (tgt == 0) { n           from[j] = i; n           res.push_back(j); n           break; n         } else { n           tgt--; n         } n       } n     } n     k %= fac[max(0LL, len - i - 1)]; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   fac[0] = cnt[0] = cnt[1] = 1; n   for (int i = 1; i < MAXN; i++) fac[i] = mul(fac[i - 1], i); n   for (int i = 2; i < MAXN; i++) cnt[i] = fac[i - 2]; n   dp[0][0] = way[0] = 1; n   for (int i = 1; i < MAXN; i++) { n     for (int j = 1; j <= i; j++) { n       for (int k = 1; k <= i; k++) { n         dp[i][j] = add(dp[i][j], mul(dp[i - k][j - 1], cnt[k])); n       } n       way[i] = add(way[i], dp[i][j]); n     } n   } n   int tc; n   cin >> tc; n   while (tc--) { n     cin >> n >> k; n     k--; n     if (k >= way[n]) { n       cout <<  -1 n ; n       continue; n     } n     int put = 0; n     vector<int> ans; n     while (put < n) { n       long long seg, rem = n - put; n       for (seg = 1; seg <= rem; seg++) { n         long long tot = mul(cnt[seg], way[rem - seg]); n         if (k >= tot) { n           k -= tot; n         } else { n           break; n         } n       } n       vector<int> p = get_p(seg, k / way[rem - seg]); n       k %= way[rem - seg]; n       for (int x : p) { n         ans.push_back(x + put); n       } n       put += seg; n     } n     for (int x : ans) { n       cout << x <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1025; n long long p[MAX_N]; n int main() { n   long long s, n, sum = 0; n   scanf( %lld%lld , &n, &s); n   for (int i = 1; i <= n; ++i) scanf( %lld , &p[i]), sum += p[i]; n   sort(p + 1, p + 1 + n); n   if (sum < s) { n     printf( -1 n ); n     return 0; n   } else { n     long long tmp = 0; n     for (int i = 1; i <= n; ++i) { n       tmp += p[i] - p[1]; n     } n     if (tmp >= s) { n       printf( %lld n , p[1]); n       return 0; n     } else { n       tmp = s - tmp; n       long long k = (tmp - 1) / n + 1; n       printf( %lld n , p[1] - k); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int d1 = 7; n const int d2 = 29; n const int mod = 19260817; n char str[200005]; n bool h1[mod + 5], h2[mod + 5]; n int r1[200005], r2[200005]; n int kf[4][200005]; n int check[200005][2]; n int fff[200005]; n bool ban[200005]; n int main() { n   int t, n; n   scanf( %d , &t); n   while (t--) { n     int cnt[4] = {0, 0, 0, 0}; n     int move[4] = {0, 0, 0, 0}; n     int n, len; n     scanf( %d , &n); n     int p1 = 0, p2 = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %s , str + 1); n       int len = strlen(str + 1); n       int type = (str[1] -  0 ) * 2 + str[len] -  0 ; n       kf[type][++cnt[type]] = i; n       fff[i] = type; n       int a = 0, b = 0; n       for (int j = 1; j <= len; j++) { n         a = (a * d1 + str[j] - 5) % mod; n         b = (b * d2 + str[j] - 19) % mod; n       } n       h1[r1[++p1] = a] = h2[r2[++p2] = b] = 1; n       a = b = 0; n       for (int j = len; j >= 1; j--) { n         a = (a * d1 + str[j] - 5) % mod; n         b = (b * d2 + str[j] - 19) % mod; n       } n       check[i][0] = a; n       check[i][1] = b; n     } n     for (int i = 1; i <= n; i++) n       if (h1[check[i][0]] && h2[check[i][1]]) n         ban[i] = 1; n       else n         move[fff[i]]++; n     if (cnt[1] == 0 && cnt[2] == 0 && cnt[3] != 0 && cnt[0] != 0) n       printf( -1 n ); n     else if (cnt[1] == cnt[2]) n       printf( 0 n n ); n     else if (cnt[1] > cnt[2]) { n       int ned = (cnt[1] - cnt[2]) / 2; n       if (ned > move[1]) n         printf( -1 n ); n       else { n         printf( %d n , ned); n         for (int i = 1, ooo = 1; ooo <= ned; ooo++, i++) { n           while (ban[kf[1][i]]) { n             i++; n           } n           printf( %d  , kf[1][i]); n         } n         printf(  n ); n       } n     } else if (cnt[2] > cnt[1]) { n       int ned = (cnt[2] - cnt[1]) / 2; n       if (ned > move[2]) n         printf( -1 n ); n       else { n         printf( %d n , ned); n         for (int i = 1, ooo = 1; ooo <= ned; ooo++, i++) { n           while (ban[kf[2][i]]) { n             i++; n           } n           printf( %d  , kf[2][i]); n         } n         printf(  n ); n       } n     } n     for (int i = 1; i <= n; i++) ban[i] = 0; n     for (int i = 1; i <= p1; i++) h1[r1[i]] = 0; n     for (int i = 1; i <= p2; i++) h2[r2[i]] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.1415926535897932384626433; n const long long inf = 1e18; n const long long mod = 1e9 + 7; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   vector<vector<long long> > cnt(100001); n   long long me = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     cnt[a[i]].push_back(i); n     me = max(me, a[i]); n   } n   bool ok = 1; n   for (long long i = me; i >= 2; i--) { n     if (cnt[i].size() > cnt[i - 1].size()) { n       ok = 0; n       break; n     } n   } n   if (!ok) { n     cout <<  -1 n ; n     return; n   } n   vector<long long> ans(n); n   long long c = 0; n   for (long long i = 1; i <= me; i++) { n     long long k = 1; n     for (auto x : cnt[i]) { n       ans[x] = k; n       k++; n       c = max(c, k); n     } n   } n   cout << c - 1 <<   n ; n   for (long long i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5e5 + 5; n const long long mod = 1e9 + 7; n void solve() { n   long long n; n   cin >> n; n   if (n == 1) { n     cout << -1 << endl; n     return; n   } n   cout << n <<     << n + 1 <<     << n * (n + 1) << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, c0, c1, h; n     cin >> n >> c0 >> c1 >> h; n     string binary; n     cin >> binary; n     int count0 = 0, count1 = 0; n     int ans = 0, arbans = 0; n     for (auto i = 0; i < binary.length(); i++) { n       if (binary[i] ==  1 ) count1++; n       if (binary[i] ==  0 ) count0++; n     } n     arbans = count1 * c1 + count0 * c0; n     if (c1 > c0 && c1 > h) { n       ans = count1 * h + count0 * c0 + count1 * c0; n     } else if (c1 < c0 && c0 > h) { n       ans = count0 * h + count1 * c1 + count0 * c1; n     } else { n       ans = count0 * c0 + count1 * c1; n     } n     ans = (ans < arbans) ? ans : arbans; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000000 + 7; n long long int n, k, summ, tr, a[1000000], minn, maxx; n string s, s1; n map<int, int> been; n int compare(const void* a, const void* b) { n   return ((*(long long int*)a) - (*(long long int*)b)); n }; n int gcd(int a, int b) { n   while (b != 0) { n     int t = b; n     b = a % b; n     a = t; n   } n   return a; n } n int NOK(int n1, int n2) { return n1 * n2 / gcd(n1, n2); } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     been.clear(); n     maxx =  a  - 1; n     minn =  z  + 1; n     cin >> s; n     for (int j = 0; j < s.length(); j++) { n       if (been[s[j]] == 1) { n         minn = -1000; n         break; n       } n       been[s[j]] = 1; n       minn = min(minn, (long long int)s[j]); n       maxx = max(maxx, (long long int)s[j]); n     } n     if (maxx - minn + 1 != s.length()) { n       cout <<  No  << endl; n     } else { n       cout <<  Yes  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long t; n   cin >> t; n   while (t--) { n     int a; n     set<int> pr; n     cin >> a; n     int ar[a]; n     for (int i = 0; i < a; i++) { n       cin >> ar[i]; n       pr.insert(ar[i]); n     } n     cout << pr.size() << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxl = 100000; n map<int, int> SG, num; n int n, x, tmp, ans = 0, prime[maxl], tot = 0; n bool notprime[maxl + 1]; n inline void init() { n   notprime[1] = 1; n   for (int i = 2; i <= maxl; i++) { n     if (!notprime[i]) prime[++tot] = i; n     for (int j = 1; j <= tot && i * prime[j] <= maxl; j++) { n       notprime[i * prime[j]] = 1; n       if (i % prime[j] == 0) break; n     } n   } n } n inline int solve(int x) { n   if (x == 0) return 0; n   if (SG.count(x)) return SG[x]; n   map<int, int> vis; n   int p = x, t = 0; n   while (p) { n     p /= 2; n     t++; n   } n   for (int i = 1; i <= t; i++) n     vis[solve((x >> i) | (x & ((1 << (i - 1)) - 1)))] = 1; n   for (int i = 0;; i++) n     if (!vis[i]) return SG[x] = i; n } n int main() { n   init(); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     for (int pos = 1; prime[pos] * prime[pos] <= x; pos++) { n       tmp = 0; n       while (x % prime[pos] == 0) { n         x /= prime[pos]; n         tmp++; n       } n       if (tmp != 0) num[prime[pos]] = num[prime[pos]] | (1 << (tmp - 1)); n     } n     if (x != 1) num[x] |= 1; n   } n   for (map<int, int>::iterator it = num.begin(); it != num.end(); it++) n     ans = ans ^ solve(it->second); n   if (ans == 0) n     puts( Arpa ); n   else n     puts( Mojtaba ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pi = 1, sum = 0, n, k, a[200100], pos[200100]; n const long long inf = (long long)1e19; n int main() { n   scanf( %I64d %I64d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n     pos[i] = (a[i - 1] != 1 ? i - 1 : pos[i - 1]); n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     pi = sum = a[i]; n     ans += (k == 1); n     for (long long now = pos[i], pre = i;; pre = now, now = pos[now]) { n       long long mid = pi - sum * (long long)k; n       if (mid > 0 && mid % k == 0 && mid / k < pre - now) ans++; n       if (!now || inf / a[now] < pi) break; n       pi *= (long long)a[now]; n       sum += a[now] + pre - now - 1; n       ans += (pi == sum * (long long)k); n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100005; n int n; n unordered_map<int, int> bit[MAX]; n unordered_map<int, int> idx; n int inf = 1000000001; n void update(int pos, int t, int val) { n   for (; t < inf; t += (t & -t)) { n     bit[pos][t] += val; n   } n } n int get(int pos, int t) { n   int ans = 0; n   for (; t > 0; t -= (t & -t)) ans += bit[pos][t]; n   return ans; n } n int main() { n   int type, t, x; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d %d , &type, &t, &x); n     if (!idx.count(x)) idx[x] = i; n     int cur = idx[x]; n     if (type == 1) { n       update(idx[x], t, 1); n     } n     if (type == 2) { n       update(idx[x], t, -1); n     } n     if (type == 3) { n       int ans = get(idx[x], t); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n vector<int> graph[MAXN]; n int values[MAXN]; n long long dp[MAXN]; n int leftValue[MAXN]; n priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                greater<pair<long long, int> > > n     pq; n void dfs(int u, int prev) { n   for (int v : graph[u]) { n     if (v == prev || values[v] == 0) { n       continue; n     } n     values[v]--; n     dfs(v, u); n   } n   while (!pq.empty()) { n     pq.pop(); n   } n   for (int v : graph[u]) { n     if (v == prev) { n       continue; n     } n     pq.push({dp[v], v}); n   } n   int currentLeft = values[u]; n   while (!pq.empty()) { n     if (pq.size() <= values[u]) { n       dp[u] += pq.top().first + 1; n       values[pq.top().second] = leftValue[pq.top().second]; n       currentLeft--; n     } n     pq.pop(); n   } n   for (int v : graph[u]) { n     if (v == prev || values[v] == 0) { n       continue; n     } n     int pick = min(currentLeft, values[v]); n     currentLeft -= pick; n     values[v] -= pick; n     dp[u] += 2 * pick; n   } n   dp[u]++; n   leftValue[u] = currentLeft; n } n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> values[i]; n   } n   for (int i = 1; i < n; i++) { n     int in, out; n     cin >> in >> out; n     graph[in].push_back(out); n     graph[out].push_back(in); n   } n   int root; n   cin >> root; n   dfs(root, -1); n   cout << dp[root] - 1 << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long logn = 63; n const long long inf = 1e18; n void upd(long long& a, const long long& b) { a = min(a, b); } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<long long> a(n), b(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long mx = *max_element(a.begin(), a.end()); n   for (int i = 0; i < n; i++) b[i] = mx - a[i]; n   vector<vector<long long> > dp(logn, vector<long long>(n + 1, inf)); n   dp[0][0] = 0; n   vector<int> s; n   for (int i = 0; i < n; i++) s.push_back(i); n   for (long long bit = 0; bit + 1 < logn; bit++) { n     if (bit) { n       vector<int> nws; n       for (int i : s) n         if (!(b[i] & (1ll << (bit - 1)))) nws.push_back(i); n       for (int i : s) n         if (b[i] & (1ll << (bit - 1))) nws.push_back(i); n       s = nws; n     } n     vector<long long> pf0(n + 1, 0), pf1(n + 1, 0); n     for (int i = 0; i < n; i++) { n       pf0[i + 1] = pf0[i], pf1[i + 1] = pf1[i]; n       if (b[s[i]] & (1ll << bit)) n         pf1[i + 1]++; n       else n         pf0[i + 1]++; n     } n     for (int sf = 0; sf <= n; sf++) { n       int b0ones = pf1[n - sf] + (pf0[n] - pf0[n - sf]); n       int b0carry = pf1[n] - pf1[n - sf]; n       upd(dp[bit + 1][b0carry], dp[bit][sf] + b0ones); n       int b1ones = pf0[n - sf] + (pf1[n] - pf1[n - sf]); n       int b1carry = n - pf0[n - sf]; n       upd(dp[bit + 1][b1carry], dp[bit][sf] + b1ones); n     } n   } n   cout << dp[logn - 1][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n struct Edge { n   Edge(int _from, int _to) : from(_from), to(_to) {} n   int from, to; n }; n enum GraphNodeColor { NODE_WHITE, NODE_GREY, NODE_BLACK }; n class Graph { n  public: n   Graph(int nodes, std::vector<Edge> edges) n       : neighb(nodes, std::vector<int>()) { n     node_count = nodes; n     for (int i = 0; i < edges.size(); ++i) { n       neighb[edges[i].from].push_back(edges[i].to); n     } n   } n   void dfs() { n     timer = 0; n     components.resize(node_count, 0); n     tout.assign(node_count, 0); n     colors.assign(node_count, NODE_WHITE); n     nodeExitOrder.clear(); n     nodeExitOrder.reserve(node_count); n     for (int i = 0; i < node_count; ++i) { n       if (colors[i] == NODE_WHITE) { n         dfs_traverse(i); n       } n     } n   } n   void count_components(const std::vector<int>& order) { n     int label = 0; n     timer = 0; n     components.resize(node_count, 0); n     tout.assign(node_count, 0); n     colors.assign(node_count, NODE_WHITE); n     nodeExitOrder.clear(); n     nodeExitOrder.reserve(node_count); n     for (int i = 0; i < node_count; ++i) { n       int v = order[i]; n       if (colors[v] == NODE_WHITE) { n         dfs_traverse(v, label++); n       } n     } n   } n   void dfs_traverse(int v, int label = 0) { n     colors[v] = NODE_GREY; n     components[v] = label; n     for (int neighbor : neighb[v]) { n       if (colors[neighbor] == NODE_WHITE) { n         dfs_traverse(neighbor, label); n       } n     } n     colors[v] = NODE_BLACK; n     tout[v] = timer++; n     nodeExitOrder.push_back(v); n   } n   int node_count; n   int timer; n   std::vector<std::vector<int>> neighb; n   std::vector<GraphNodeColor> colors; n   std::vector<int> tout; n   std::vector<int> nodeExitOrder; n   std::vector<int> components; n }; n int powModulo(int base, int power, int modulo) { n   if (power == 0) { n     return 1 % modulo; n   } else { n     long long ans = powModulo(base, power / 2, modulo); n     ans = (ans * ans) % modulo; n     ans = (power % 2 == 0) ? ans : ans * base % modulo; n     return ans; n   } n } n int main() { n   const int modulo = 1e9 + 7; n   int n; n   std::cin >> n; n   std::vector<Edge> edges, edges_tr; n   edges.reserve(n); n   edges_tr.reserve(n); n   for (int i = 0; i < n; ++i) { n     int ai; n     std::cin >> ai; n     edges.emplace_back(i, ai - 1); n   } n   Graph gr(n, edges); n   gr.dfs(); n   std::vector<int> traverse_order = gr.nodeExitOrder; n   std::reverse(traverse_order.begin(), traverse_order.end()); n   for (int i = 0; i < n; ++i) { n     edges_tr.emplace_back(edges[i].to, edges[i].from); n   } n   Graph gr_tr(n, edges_tr); n   gr_tr.count_components(traverse_order); n   std::vector<int> comp_sizes; n   comp_sizes.reserve(n); n   for (int comp : gr_tr.components) { n     if (comp >= comp_sizes.size()) { n       comp_sizes.resize(comp + 1, 0); n     } n     ++comp_sizes[comp]; n   } n   long long ans = 1; n   int rest_edges = 0; n   for (int comp_size : comp_sizes) { n     if (comp_size > 1) { n       int tmp = powModulo(2, comp_size, modulo); n       if (tmp < 2) { n         tmp += modulo; n       } n       ans = ans * (tmp - 2) % modulo; n     } else { n       ++rest_edges; n     } n   } n   ans = ans * powModulo(2, rest_edges, modulo) % modulo; n   std::cout << ans << std::endl; n   return 0; n } n


//EK ONKAR n #include <bits/stdc++.h> n  n using namespace std; n  n /******************************MACROS******************************/ n  n #define int long long n #define ld long double n #define all(x) (x).begin(), (x).end() n #define rall(x) (x).rbegin(), (x).rend() n #define yes cout<< Yes n  n #define no cout<< No n  n #define vt vector n #define pb push_back n #define F first n #define S second n #define u_s unordered_set n #define u_m unordered_map n #define p_q priority_queue n #define m_p make_pair n  n /******************************TEMPLATE******************************/ n  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 deb(...)   [  << #__VA_ARGS__  :   << (__VA_ARGS__) <<  ]   n  n /******************************ALGORITHMS******************************/ n  n int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;} n int __pow(int x, int y) {int res = 1;while(y>0){if(y&1) res = (res*x); y>>=1; x = (x*x);}return res;} n int mi(int x, int M) {return __pow(x, M-2, M);} n int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);} n int add(int a, int b, int M) {return (a+b) % M;} n int mul(int a, int b, int M) {return (a*b) % M;} n int sub(int a, int b, int M) {return (a-b+M) % M;} n int div(int a, int b, int M) {return (a*mi(b, M)) % M;} n void umax(int &a, int b) {a = max(a, b);} n void umin(int &a, int b) {a = min(a, b);} n  n /******************************main*code******************************/ n  n const int N = 5e3+2, inf = 1e18, M = 998244353; n  n int Test, n, m, k, dp[N][N]; n bool ok = 1; n vt<int> g[N]; n  n void dfs(int u, int par = 0) n { n     bool is_leaf = 1; n     for(auto v:g[u]) if(v != par) n         { n             dfs(v, u); n             is_leaf = 0; n         } n     if(is_leaf) n     { n         for(int j = 0; j<=k; j++) n             dp[u][j] = 1; n         return; n     } n     for(int j = 0; j<=k/2; j++) n     { n         dp[u][j] = 1; n         for(auto v:g[u]) if(v != par) n                 dp[u][j] = (dp[u][j] * ((j == 0 ? 0 : dp[v][j-1]) + dp[v][k])) % M; n     } n     for(int j = k/2 + 1; j<=k; j++) n     { n         int last = 1; n         int i = 1; n         dp[u][j] = 0; n         for(auto v:g[u]) if(v != par) n             { n                 int tmp = last; n                 last = (tmp * (dp[v][k] + (j == k ? 0 : dp[v][k-j-1]))) % M; n                 dp[u][j] = ((tmp * (j == 1 ? dp[v][j-1] : (dp[v][j-1] - dp[v][j-2] + M) % M)) % M + (dp[u][j] * (dp[v][k] + (j == k ? 0 : dp[v][k-j-1]))) % M) % M; n                 i++; n             } n         dp[u][j] = (dp[u][j-1] + dp[u][j]) % M; n     } n } n  n void test_case() n { n     cin >> n >> k; n     g[1].pb(0); n     for(int i = 1; i<n; i++) n     { n         int u, v; n         cin >> u >> v; n         g[u].pb(v); n         g[v].pb(u); n     } n     dfs(1); n     cout << dp[1][k] << endl; n } n  n int32_t main() n { n     ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n     Test = 1; n     //cin >> Test; n     while(Test--) n         test_case(); n #ifndef ONLINE_JUDGE n     cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s.  << endl; n #endif n     return 0; n } n