#include <bits/stdc++.h> n using namespace std; n long long a[1010000]; n inline bool read(long long &num) { n   char in; n   bool IsN = false; n   in = getchar(); n   if (in == EOF) return false; n   while (in !=  -  && (in <  0  || in >  9 )) in = getchar(); n   if (in ==  - ) { n     IsN = true; n     num = 0; n   } else n     num = in -  0 ; n   while (in = getchar(), in >=  0  && in <=  9 ) { n     num *= 10, num += in -  0 ; n   } n   if (IsN) num = -num; n   return true; n } n int main() { n   long long n; n   scanf( %lld , &n); n   long long sum = 0; n   for (long long i = 1; i <= n; i++) { n     read(a[i]); n     sum += a[i]; n   } n   if (n == 1) { n     printf( %lld n , a[1]); n     return 0; n   } n   if ((sum - (n * (n - 1) / 2)) % n == 0) { n     long long x = (sum - (n * (n - 1) / 2)) / n; n     for (long long i = 1; i < n; i++) printf( %lld  , x + i - 1); n     printf( %lld n , x + n - 1); n     return 0; n   } n   for (long long i = 1; i <= n; i++) { n     if ((sum - (n * (n - 1) / 2) + i) % n == 0) { n       long long x = (i + sum - (n * (n - 1) / 2)) / n, nd = x + n - i - 1; n       for (long long j = 1; j < n - 1; j++) { n         printf( %lld  , x); n         if (x == nd) printf( %lld  , x); n         x++; n       } n       if (x == nd) printf( %lld  , x); n       printf( %lld n , x); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   unsigned long long a, b, x; n   std::cin >> a >> b; n   a < b | a - b & 1 n       ? std::cout << -1 n       : (x = a - b >> 1, n          x ^ a - x ^ b ? std::cout << -1 : std::cout << x <<     << a - x); n } n


#include <iostream> n #include <bits/stdc++.h> n  n int i = 0, j = 0, g = 0; n  n #define int long long n #define float double n  n using namespace std; n  n void solve(void) n { n     int n; n  n     cin >> n; n  n     int a[2*n], config = 0; n  n     set <int> s; n  n     for (i = 0; i < 2*n; i++) n     { n         cin >> a[i]; n  n         if (a[i] % 2 == 1) n         { n             config = -1; n         } n  n         s.insert(a[i]); n     } n  n     sort(a, a + 2*n); n  n     if (config == -1 || s.size() != n) n     { n         cout <<  NO n ; n         return; n     } n  n     j = 2; n  n     int asum = 0, csum = 0; n      n     for (i = 1; i <= n - 1; i++) n     { n         if ((a[2*i] - a[2*(i - 1)]) % j != 0) n         { n             cout <<  NO n ; n             return; n         } n  n         asum += (a[2*i] - a[2*(i - 1)])/j; n         csum += asum; n  n         j += 2; n     } n  n     if (((a[0]/2) - csum <= 0) || ((((a[0]/2) - csum) % n) != 0)) n     { n         cout <<  NO n ; n         return; n     } n  n     cout <<  YES n ; n } n  n int32_t main(void) n { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n  n     int t; n  n     cin >> t;    n  n     while (t--) n     { n         solve(); n     } n }


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n int x[N], y[N], c[N], k[N], f[N]; n int s[N], st, tu[N], tv[N]; n struct node { n   int u, v; n   long long s; n   bool operator<(const node &p) const { return s < p.s; } n } d[N * N + N]; n int dis(int a, int b) { return abs(x[a] - x[b]) + abs(y[a] - y[b]); } n int fd(int x) { n   if (f[x] == x) return x; n   return f[x] = fd(f[x]); n } n void mg(int x, int y) { n   int fa = fd(x), fb = fd(y); n   f[fa] = fb; n } n int main() { n   int n, p = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &c[i]); n     f[i] = i; n     d[++p].s = c[i]; n     d[p].u = i; n     d[p].v = n + 1; n   } n   for (int i = 1; i <= n; i++) scanf( %d , &k[i]); n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       d[++p].s = 1ll * dis(i, j) * (k[i] + k[j]), d[p].u = i, d[p].v = j; n   sort(d + 1, d + p + 1); n   int cnt = 0; n   long long ans = 0; n   for (int i = 1; i <= p; i++) { n     if (fd(d[i].u) == fd(d[i].v)) continue; n     if (d[i].v == n + 1) n       s[++s[0]] = d[i].u; n     else { n       st++; n       tu[st] = d[i].u; n       tv[st] = d[i].v; n     } n     mg(d[i].u, d[i].v); n     ans += d[i].s; n     cnt++; n     if (cnt == n) break; n   } n   printf( %lld n , ans); n   printf( %d n , s[0]); n   for (int i = 1; i <= s[0]; i++) printf( %d  , s[i]); n   puts(  ); n   printf( %d n , st); n   for (int i = 1; i <= st; i++) printf( %d %d n , tu[i], tv[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void write(long long a) { n   if (a < 0) { n     printf( - ); n     a = -a; n   } n   if (a >= 10) write(a / 10); n   putchar(a % 10 +  0 ); n } n inline void writeln(long long a) { n   write(a); n   printf(  n ); n } n long long bin[110]; n int main() { n   long long t = read(), a = read(), b = read(); n   if (t == a && a == b) { n     if (t == 1ll) printf( inf n ); n     if (t != 1ll) printf( 2 n ); n     return 0; n   } n   if (t > a) { n     if (a != b) n       puts( 0 ); n     else n       puts( 1 ); n     return 0; n   } n   if (a == b) { n     puts( 1 ); n     return 0; n   } n   int len = 0, cnt = 0; n   if (t == 1 && a == 1 && b != 1) { n     puts( 0 ); n     return 0; n   } n   while (b) { n     bin[++len] = b % a; n     cnt += bin[len]; n     b /= a; n   } n   long long ans = 0; n   for (int i = len; i >= 1; i--) { n     ans = ans * t + bin[i]; n   } n   if (cnt == 1) { n     if (ans != a) { n       ans = 0; n       len--; n       bin[len] = a; n       for (int i = len; i >= 1; i--) ans = ans * t + bin[i]; n     } n   } n   printf( %d n , ans == a); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n const int MAXN = 1e4 + 10; n const int MOD = 1e9 + 7; n const int inf = 1e9; n const double pi = acos(-1.0); n const double eps = 1e-6; n int dx[] = {0, -1, 0, 1}; n int dy[] = {1, 0, -1, 0}; n int x, y; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int a, b, c; n   cin >> x >> y; n   a = b = c = y; n   int res = 0; n   while (a < x || b < x || c < x) { n     int maxa = max(a, max(b, c)); n     int maxc = min(a, min(b, c)); n     int maxb = a + b + c - maxa - maxc; n     int l = maxa + maxb - maxc - 1; n     if (a == maxc) n       a += l; n     else if (b == maxc) n       b += l; n     else n       c += l; n     res++; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fp(long long x, long long y, long long m = 998244353) { n   long long ANS = 1; n   while (y) { n     if (y & 1) ANS = (ANS * x) % 998244353; n     x = (x * x) % 998244353; n     y >>= 1; n   } n   return ANS; n } n long long inv(long long x, long long m = 998244353) { return fp(x, m - 2); } n long long testcases, arr[200001], n; n string s; n int32_t main() { n   iostream::sync_with_stdio(0), cin.tie(0); n   cin >> testcases; n   while (testcases--) { n     cin >> n; n     for (long long i = 0; i < n; i++) cin >> arr[i]; n     cin >> s; n     multiset<long long> L, S; n     for (long long i = 0; i < n; i++) { n       if (s[i] ==  R ) n         L.insert(arr[i]); n       else n         S.insert(arr[i]); n     } n     for (long long i = 1; i <= n; i++) { n       auto itr = S.lower_bound(i); n       if (itr == S.end()) { n         auto itr2 = L.upper_bound(i); n         if (itr2 == L.begin()) { n           cout <<  NO  <<   n ; n           goto a; n         } n         itr2--; n         L.erase(itr2); n       } else n         S.erase(itr); n     } n     cout <<  YES  <<   n ; n   a:; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const int oo = (int)1e9; n const double PI = 2 * acos(0.0); n const double eps = 1e-7; n const int MAXN = 1e5 + 10; n int dx[] = {1, 0, -1, 0, -1, -1, 1, 1}; n int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; n int main() { n   int n, k, a[100009]; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   set<long long> s; n   for (int i = n - 1; i >= 0; i--) n     if (s.find((long long)k * a[i]) == s.end()) s.insert(a[i]); n   cout << ((int)((s).size())) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int k, tot; n int op[4]; n char s; n long long ans = 0x3f3f3f3f3f3f3f3f; n long long a[5]; n template <typename T> n inline void read(T& x) { n   x = 0; n   T op = 1; n   char c = getchar(); n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) op = -1; n   for (; c <=  9  && c >=  0 ; c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n } n void dfs(int p, int k) { n   if (p == 4) { n     ans = ans < a[k] ? ans : a[k]; n     return; n   } n   for (int i = 1; i <= 4; ++i) { n     if (a[i] == -1) continue; n     for (int j = 1; j <= 4; ++j) { n       if (i == j || a[j] == -1) continue; n       if (op[p]) { n         a[i] += a[j]; n         long long t = a[j]; n         a[j] = -1; n         dfs(p + 1, i); n         a[j] = t; n         a[i] -= a[j]; n       } else { n         a[i] *= a[j]; n         long long t = a[j]; n         a[j] = -1; n         dfs(p + 1, i); n         a[j] = t; n         a[i] /= a[j]; n       } n     } n   } n } n int main() { n   for (int i = 1; i <= 4; ++i) { n     read(a[i]); n     if (!a[i]) k = 1; n   } n   while (tot < 3) { n     scanf( %c , &s); n     if (s ==  + ) op[++tot] = 1; n     if (s ==  * ) { n       op[++tot] = 0; n       if (k) { n         puts( 0 ); n         return 0; n       } n     } n   } n   dfs(1, 0); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double sqr(double x) { return x * x; } n struct CPoint { n   double x, y, n, l; n   double length() { return sqrt(sqr(x) + sqr(y)); } n }; n CPoint a[100100], b[100100]; n double length(CPoint p1, CPoint p2) { n   return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y)); n } n double calc(CPoint X, CPoint Y) { return X.length() + length(X, Y) + Y.l; } n int b_search(CPoint X, int l, int r) { n   if (r - l <= 10) { n     int i = -1; n     double v = 100000000, tmp; n     for (; l <= r; l++) { n       tmp = calc(a[l], X); n       if (tmp < v) { n         v = tmp; n         i = l; n       } n     } n     return i; n   } n   int mid = (l + r) / 2; n   double p1 = calc(a[mid - 1], X); n   double p2 = calc(a[mid], X); n   double p3 = calc(a[mid + 1], X); n   if (p2 <= p3 && p2 <= p1) { n     int i = b_search(X, l, mid); n     int j = b_search(X, mid, r); n     if (calc(a[i], X) < calc(a[j], X)) return i; n     return j; n   } n   if (p1 <= p2 && p2 <= p3) return b_search(X, l, mid); n   return b_search(X, mid, r); n } n int main() { n   int N, M, i, j; n   double A, B; n   scanf( %d%d%lf%lf , &N, &M, &A, &B); n   for (i = 0; i < N; i++) { n     scanf( %lf , &a[i].y); n     a[i].x = A; n     a[i].n = i + 1; n   } n   for (i = 0; i < M; i++) { n     scanf( %lf , &b[i].y); n     b[i].x = B; n     b[i].n = i + 1; n   } n   for (i = 0; i < M; i++) scanf( %lf , &b[i].l); n   double v = 1000000000; n   int ii = -1, jj = -1; n   for (i = 0; i < M; i++) { n     j = b_search(b[i], 0, N - 1); n     if (v > calc(a[j], b[i])) { n       jj = i + 1; n       ii = j + 1; n       v = calc(a[j], b[i]); n     } n   } n   printf( %d %d , ii, jj); 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, y; n     cin >> n >> x >> y; n     long long int maxi = x + y - 2 + 1; n     maxi = min(maxi, n); n     long long int mini = x + y - n; n     if (mini <= 0) n       mini = 1; n     else n       mini++; n     mini = min(mini, n); n     cout << mini <<     << maxi << endl; n   } n } n


#include <bits/stdc++.h> n const int base = 1000003; n int n, k, d[201][30001], v[201]; n bool isv[30010]; n int fa[30010], tmp[30010]; n bool on[30010]; n struct edge { n   int to; n   edge* next; n } E[60010], *ne = E, *first[30010]; n void link(int u, int v) { n   *ne = (edge){v, first[u]}; n   first[u] = ne++; n } n bool chk(int i, int f, int d, int* g) { n   if (d != g[i]) return 0; n   for (edge* e = first[i]; e; e = e->next) n     if (e->to != f && !chk(e->to, i, d + 1, g)) return 0; n   return 1; n } n int list[30010], len; n bool cmp(int i, int j) { return d[0][i] < d[0][j]; } n unsigned long long hval[30010], hadd; n std::map<unsigned long long, int> M; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < k; i++) { n     int fd = 0; n     for (int j = 0; j < n; j++) n       scanf( %d , d[i] + j), !d[i][j] ? fd++, v[i] = j : 1; n     if (fd != 1) return puts( -1 ), 0; n     fd = 0; n     for (int j = 0; j < i; j++) n       if (v[j] == v[i]) { n         fd = 1; n         for (int x = 0; x < n; x++) n           if (d[j][x] != d[i][x]) return puts( -1 ), 0; n       } n     if (fd) i--, k--; n     isv[v[i]] = 1; n   } n   on[v[0]] = 1; n   for (int i = 0; i < n; i++) fa[i] = -1; n   for (int i = 1; i < k; i++) { n     int x = v[i]; n     for (int j = 0; j <= d[0][x]; j++) tmp[j] = -1; n     for (int j = 0; j < n; j++) n       if (d[0][j] + d[i][j] == d[0][x]) { n         if (tmp[d[0][j]] > -1) return puts( -1 ), 0; n         tmp[d[0][j]] = j; n       } n     for (int j = 0; j <= d[0][x]; j++) { n       if (tmp[j] < 0) return puts( -1 ), 0; n       if (j) { n         if (fa[tmp[j]] > -1 && fa[tmp[j]] != tmp[j - 1]) return puts( -1 ), 0; n         fa[tmp[j]] = tmp[j - 1]; n         on[tmp[j]] = 1; n       } n     } n   } n   for (int i = 0; i < n; i++) n     if (on[i] && fa[i] > -1) link(i, fa[i]), link(fa[i], i); n   for (int i = 0; i < k; i++) n     if (!chk(v[i], -1, 0, d[i])) return puts( -1 ), 0; n   for (int i = 0; i < k; i++) hadd = hadd * base + 1; n   for (int i = 0; i < n; i++) { n     for (int j = k; j--;) hval[i] = hval[i] * base + d[j][i]; n     if (on[i]) M[hval[i]] = i; n   } n   for (int i = 0; i < n; i++) n     if (!on[i]) list[len++] = i; n   std::sort(list, list + len, cmp); n   for (int* i = list; i < list + len; i++) { n     unsigned long long h = hval[*i] - hadd; n     if (!M.count(h)) return puts( -1 ), 0; n     fa[*i] = M[h]; n     M[hval[*i]] = *i; n   } n   for (int i = 0; i < n; i++) n     if (fa[i] > -1) printf( %d %d n , i + 1, fa[i] + 1); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 7; n int n; n int val[N]; n struct edge { n   int to, nex; n } e[N << 1]; n int fir[N], eid; n int siz[N], fa[N], son[N]; n int dfn[N], pos[N], inx; n int cnt[N], res[N]; n long long mul[N], ans[N]; n void addedge(int u, int v) { n   e[++eid] = (edge){v, fir[u]}; n   fir[u] = eid; n } n void hld(int s) { n   siz[s] = 1; n   pos[dfn[s] = ++inx] = s; n   for (int i = fir[s]; i; i = e[i].nex) n     if (e[i].to != fa[s]) n       fa[e[i].to] = s, hld(e[i].to), siz[s] += siz[e[i].to], n       son[s] = siz[e[i].to] > siz[son[s]] ? e[i].to : son[s]; n } n void dfs(int s, int rem) { n   for (int i = fir[s]; i; i = e[i].nex) n     if (e[i].to != fa[s] && e[i].to != son[s]) dfs(e[i].to, 0); n   if (son[s]) dfs(son[s], 1); n   int c = val[s]; n   mul[cnt[c]] -= c, mul[++cnt[c]] += c, res[s] = max(res[son[s]], cnt[c]); n   for (int i = fir[s]; i; i = e[i].nex) { n     if (e[i].to == fa[s] || e[i].to == son[s]) continue; n     for (int j = dfn[e[i].to], c; j < dfn[e[i].to] + siz[e[i].to]; ++j) n       c = val[pos[j]], mul[cnt[c]] -= c, mul[++cnt[c]] += c, n       res[s] = max(res[s], cnt[c]); n   } n   ans[s] = mul[res[s]]; n   if (rem) return; n   memset(cnt, 0, sizeof(int) * (n + 2)); n   memset(mul, 0, sizeof(long long) * (res[s] + 2)); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , val + i); n   for (int i = 1, u, v; i < n; ++i) n     scanf( %d%d , &u, &v), addedge(u, v), addedge(v, u); n   hld(1); n   dfs(1, 1); n   for (int i = 1; i <= n; ++i) printf( %I64d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[1010] = {0}; n   int n, i, s = 0, x, m = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     a[x]++; n   } n   for (i = 0; i <= 1000; i++) { n     if (a[i]) m++; n     if (a[i] > s) s = a[i]; n   } n   cout << s <<     << m << endl; 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 int GLL(long long& x) { return scanf( %lld , &x); } n int GI(int& x) { return scanf( %d , &x); } n void IO() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin.exceptions(ios::badbit | ios::failbit); n } n void file() { n   freopen( mountains.in ,  r , stdin); n   freopen( mountains.out ,  w , stdout); n } n const int N = 1e5 + 7, M = 2 * N; n int to[M], nxt[M], head[N], ne, visited[N], vid; n int n, m; n void init() { n   memset(head, -1, n * sizeof(head[0])); n   ne = 0; n } n void addEdge(int u, int v) { n   to[ne] = v; n   nxt[ne] = head[u]; n   head[u] = ne++; n } n void BiEdge(int u, int v) { n   addEdge(u, v); n   addEdge(v, u); n } n long long A[100001], B[100001], C[100001]; n void solve() { n   int n; n   cin >> n; n   if (n % 2 == 0) n     puts( -1 ); n   else { n     for (int i = 0; i < n; i++) { n       A[i] = B[i] = i; n       C[i] = (A[i] + B[i]) % n; n     } n     for (int i = 0; i < n; i++) cout << A[i] <<    ; n     cout << endl; n     for (int j = 0; j < n; j++) cout << B[j] <<    ; n     cout << endl; n     for (int j = 0; j < n; j++) cout << C[j] <<    ; n     cout << endl; n   } n } n int main() { n   IO(); n   long long t = 1; n   solve(); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize(2) n #pragma GCC optimize(3) n const int N = 1e6 + 10, p1 = 1e9 + 7, p2 = 1e9 + 9; n int n, k, len; n char s[N << 1], ch; n int strhash[N][2], hashbase[2], hashpow[2] = {1, 1}, tmp[2]; n std::map<std::pair<int, int>, int> mp; n std::set<int> st; n int main() { n   int i, j, q; n   srand(time(NULL)); n   scanf( %d%d , &n, &k); n   len = n * k; n   for (i = 1; i <= len; i++) { n     do s[i] = getchar(); n     while (s[i] <  a  || s[i] >  z ); n     s[len + i] = s[i]; n   } n   hashbase[0] = (long long)rand() * rand() % p1, n   hashbase[1] = (long long)rand() * rand() % p2; n   for (i = 1; i <= k; i++) { n     if (i > 1) n       hashpow[0] = (long long)hashpow[0] * hashbase[0] % p1, n       hashpow[1] = (long long)hashpow[1] * hashbase[1] % p2; n     strhash[1][0] = ((long long)strhash[1][0] * hashbase[0] % p1 + s[i]) % p1, n     strhash[1][1] = ((long long)strhash[1][1] * hashbase[1] % p2 + s[i]) % p2; n   } n   for (i = 2; i <= len; i++) n     strhash[i][0] = n         ((strhash[i - 1][0] - (long long)s[i - 1] * hashpow[0] % p1 + p1) % p1 * n              hashbase[0] % p1 + n          s[i + k - 1]) % n         p1, n     strhash[i][1] = n         ((strhash[i - 1][1] - (long long)s[i - 1] * hashpow[1] % p2 + p2) % p2 * n              hashbase[1] % p2 + n          s[i + k - 1]) % n         p2; n   scanf( %d , &q); n   for (i = 1; i <= q; i++) { n     tmp[0] = tmp[1] = 0; n     for (j = 1; j <= k; j++) { n       do ch = getchar(); n       while (ch <  a  || ch >  z ); n       tmp[0] = ((long long)tmp[0] * hashbase[0] % p1 + ch) % p1, n       tmp[1] = ((long long)tmp[1] * hashbase[1] % p2 + ch) % p2; n     } n     if (mp[std::make_pair(tmp[0], tmp[1])]) return 233; n     mp[std::make_pair(tmp[0], tmp[1])] = i; n   } n   int tmp; n   for (i = 1; i <= k; i++) { n     st.clear(); n     for (j = 0; j < n; j++) { n       tmp = mp[std::make_pair(strhash[(i + j * k - 1) % len + 1][0], n                               strhash[(i + j * k - 1) % len + 1][1])]; n       if (!tmp || st.count(tmp)) n         break; n       else n         st.insert(tmp); n     } n     if (j == n) { n       puts( YES ); n       for (j = 0; j < n; j++) n         printf( %d  , n                mp[std::make_pair(strhash[(i + j * k - 1) % len + 1][0], n                                  strhash[(i + j * k - 1) % len + 1][1])]); n       return 0; n     } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int t; n   cin >> t; n   while (t--) { n     long long int a, b, c, d; n     cin >> a >> b; n     if (a % b == 0) n       cout << 0 << endl; n     else if (b > a) n       cout << (b - a) << endl; n     else { n       c = a / b; n       c++; n       c = c * b; n       cout << (c - a) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n const long long inf = 9e18 + 9; n const int mod = 1e9 + 7; n long long res; n int indeg[N], outdeg[N], diam[N], adj[N]; n void solve() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v, val; n     cin >> u >> v >> val; n     outdeg[u]++; n     indeg[v]++; n     diam[u] = val; n     adj[u] = v; n   } n   for (int i = 1; i <= n; i++) { n     if (outdeg[i] > 0 && indeg[i] == 0) { n       res++; n     } n   } n   cout << res <<   n ; n   for (int i = 1; i <= n; i++) { n     if (outdeg[i] > 0 && indeg[i] == 0) { n       int ans = INT_MAX; n       int curr = i; n       while (outdeg[curr]) { n         ans = min(ans, diam[curr]); n         curr = adj[curr]; n       } n       cout << i <<     << curr <<     << ans <<   n ; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6, inf = 2e9 + 2; n vector<pair<int, int> > ad[N + 2], tree[N + 2]; n int br[N + 2]; n vector<int> bkad[N + 2]; n int vis[N + 2], lev[N + 2]; n void clear(int n, int m) { n   for (int i = 1; i <= n; i++) { n     tree[i].clear(), bkad[i].clear(); n     vis[i] = 0, lev[i] = 0; n   } n   for (int i = 1; i <= m; i++) br[i] = 0; n } n void dfs(int node, int par, int lv) { n   vis[node] = 1, lev[node] = lv; n   for (auto z : ad[node]) { n     int x = z.first; n     if (x == par) continue; n     if (vis[x]) { n       bkad[node].push_back(x); n       bkad[x].push_back(node); n     } else { n       tree[node].push_back({x, z.second}); n       tree[x].push_back({node, z.second}); n       dfs(x, node, lv + 1); n     } n   } n } n int bridge(int node, int par, int nm) { n   int mn = 1e9; n   for (auto x : bkad[node]) mn = min(mn, lev[x]); n   for (auto x : tree[node]) { n     if (x.first == par) continue; n     mn = min(mn, bridge(x.first, node, x.second)); n   } n   if (mn >= lev[node] && par != -1) br[nm] = 1; n   return mn; n } n vector<int> pth; n int vs[N + 2]; n bool find(int nd, int tr) { n   if (nd == tr) return true; n   vs[nd] = 1; n   for (auto x : ad[nd]) { n     if (vs[x.first]) continue; n     pth.push_back(x.second); n     if (find(x.first, tr)) return true; n     pth.pop_back(); n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   int s, t; n   cin >> s >> t; n   vector<vector<int> > ed(m + 1); n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     ed[i] = {u, v, w}; n     ad[u].push_back({v, i}); n     ad[v].push_back({u, i}); n   } n   find(s, t); n   int ans = inf, one = -1, two = -1; n   if ((int)pth.size() == 0) cout << 0 <<     << 0 << endl, exit(0); n   vector<int> ok = pth; n   for (auto x : ok) { n     int u = ed[x][0], v = ed[x][1]; n     int id = 0; n     for (auto z : ad[u]) { n       if (z.second == x) { n         ad[u].erase(ad[u].begin() + id); n         break; n       } n       id++; n     } n     id = 0; n     for (auto z : ad[v]) { n       if (z.second == x) { n         ad[v].erase(ad[v].begin() + id); n         break; n       } n       id++; n     } n     pth.clear(); n     for (int i = 1; i <= n; i++) vs[i] = 0; n     find(s, t); n     if ((int)pth.size() == 0) { n       if (ed[x][2] < ans) ans = ed[x][2], one = x, two = -1; n     } n     dfs(s, -1, 0); n     bridge(s, -1, 0); n     for (auto z : pth) { n       if (br[z] == 0) continue; n       if (ed[x][2] + ed[z][2] < ans) n         ans = ed[x][2] + ed[z][2], one = x, two = z; n     } n     clear(n, m); n     ad[ed[x][0]].push_back({ed[x][1], x}); n     ad[ed[x][1]].push_back({ed[x][0], x}); n   } n   if (ans == inf) n     cout << -1 << endl; n   else { n     cout << ans <<    ; n     if (two == -1) n       cout << 1 <<     << one << endl; n     else n       cout << 2 <<     << one <<     << two << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n using namespace std; n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n int prime[100007]; n void SieveOfEratosthenes(int n) { n   memset(prime, -1, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == -1) { n       for (int i = p * p; i <= n; i += p) prime[i] = p; n     } n   } n } n int lps(string s) { n   int len = 0; n   int n = s.length(); n   int dp[n]; n   dp[0] = 0; n   for (int i = 1; i < n; i++) { n     if (s[i] == s[len]) { n       len++; n       dp[i] = len; n       continue; n     } n     if (len != 0) { n       len = dp[len - 1]; n       i--; n       continue; n     } n     dp[i] = 0; n   } n   return dp[n - 1]; n } n long long power(unsigned long long x, unsigned long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n vector<pair<long long, long long>> pFactor; n void factorize(long long n) { n   int count = 0; n   while (!(n % 2)) { n     n >>= 1; n     count++; n   } n   if (count) pFactor.push_back({2, count}); n   for (long long i = 3; i <= sqrt(n); i += 2) { n     count = 0; n     while (n % i == 0) { n       count++; n       n = n / i; n     } n     if (count) pFactor.push_back({i, count}); n   } n   if (n > 2) pFactor.push_back({n, 1}); n } n int dp[405][405]; n int solve(int a[], int n, int i, int t) { n   if (i > n) { n     return 0; n   } n   if (t > 2 * n) { n     return INT_MAX; n   } n   if (dp[i][t] != -1) { n     return dp[i][t]; n   } n   int x = solve(a, n, i + 1, t + 1); n   if (x != INT_MAX) { n     x += abs(t - a[i]); n   } n   x = min(x, solve(a, n, i, t + 1)); n   dp[i][t] = x; n   return x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n + 1]; n     for (long long i = 1; i < n + 1; i++) cin >> a[i]; n     sort(a + 1, a + n + 1); n     memset(dp, -1, sizeof(dp)); n     int x = solve(a, n, 1, 1); n     cout << x << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long power(long long x, long long y) { n   long long temp = 1; n   while (y > 0) { n     if (y % 2 == 1) temp = (x * temp) % 1000000007; n     x = (x * x) % 1000000007; n     y = y / 2; n   } n   return temp; n } n long long n; n string st; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   set<string> ans; n   for (long long i = (long long)(1); i <= (long long)(n); i++) { n     cin >> st; n     while (1) { n       long long len = st.length(), ind = -1; n       for (long long i = (long long)(0); i <= (long long)(len - 2); i++) { n         if (st[i] ==  k  && st[i + 1] ==  h ) { n           ind = i; n           break; n         } n       } n       if (ind != -1) { n         st = st.substr(0, ind) +  h  + st.substr(ind + 2); n         continue; n       } n       for (long long i = (long long)(0); i <= (long long)(len - 1); i++) { n         if (st[i] ==  u ) { n           ind = i; n           break; n         } n       } n       if (ind != -1) { n         st = st.substr(0, ind) +  oo  + st.substr(ind + 1); n         continue; n       } else n         break; n     } n     ans.insert(st); n   } n   cout << ans.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Braka() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   Braka(); n   int n, k, ans = 0; n   cin >> n >> k; n   while (n--) { n     set<char> st; n     string s; n     cin >> s; n     for (int i = 0; i < s.length(); i++) n       if (s[i] -  0  <= k) st.insert(s[i]); n     if (st.size() == k + 1) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6; n int n, seg[MAXN], r[MAXN], v[MAXN], resp; n void update(int ini, int fim, int cur, int id, int aux) { n   if (id < ini || fim < id) return; n   if (ini == fim) { n     seg[cur] = aux; n     r[cur] = aux; n     return; n   } n   int m = (ini + fim) / 2; n   update(ini, m, 2 * cur, id, aux); n   update(m + 1, fim, 2 * cur + 1, id, aux); n   seg[cur] = seg[2 * cur] + seg[2 * cur + 1]; n   r[cur] = max(r[2 * cur + 1], seg[2 * cur + 1] + r[2 * cur]); n } n int BB(int ini, int fim, int cur, int k) { n   if (ini == fim) return ini; n   int m = (ini + fim) / 2; n   if (r[2 * cur + 1] < k) return BB(ini, m, 2 * cur, k - seg[2 * cur + 1]); n   return BB(m + 1, fim, 2 * cur + 1, k); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   ; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int auxid, auxx, aux; n     cin >> auxid >> auxx; n     if (!auxx) n       update(1, n, 1, auxid, -1); n     else { n       cin >> aux; n       v[auxid] = aux; n       update(1, n, 1, auxid, 1); n     } n     resp = v[BB(1, n, 1, 1)]; n     if (r[1] < 1) n       cout << -1 <<   n ; n     else n       cout << resp <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void readi(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readll(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readc(char &x) { n   char c; n   while ((c = getchar()) ==    ) n     ; n   x = c; n } n void writes(string s) { puts(s.c_str()); } n void writeln() { writes(  ); } n void writei(int x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n void writell(long long x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n inline long long inc(int &x) { return ++x; } n inline long long inc(long long &x) { return ++x; } n inline long long inc(int &x, long long y) { return x += y; } n inline long long inc(long long &x, long long y) { return x += y; } n inline double inc(double &x, double y) { return x += y; } n inline long long dec(int &x) { return --x; } n inline long long dec(long long &x) { return --x; } n inline long long dec(int &x, long long y) { return x -= y; } n inline long long dec(long long &x, long long y) { return x -= y; } n inline double dec(double &x, double y) { return x -= y; } n inline long long mul(int &x) { return x = ((long long)x) * x; } n inline long long mul(long long &x) { return x = x * x; } n inline long long mul(int &x, long long y) { return x *= y; } n inline long long mul(long long &x, long long y) { return x *= y; } n inline double mul(double &x, double y) { return x *= y; } n inline long long divi(const int &x) { n   long long ans, l, r, mid; n   ans = 0; n   l = 0; n   r = 0x3fffffff; n   while (l < r) { n     mid = (l + r) / 2; n     if (mid * mid <= x) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid; n   } n   return ans; n } n inline long long divi(const long long &x) { n   long long ans, l, r, mid; n   ans = 0; n   l = 0; n   r = 0x3fffffff; n   while (l < r) { n     mid = (l + r) / 2; n     if (mid * mid <= x) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid; n   } n   return ans; n } n inline long long divi(int &x, long long y) { return x /= y; } n inline long long divi(long long &x, long long y) { return x /= y; } n inline double divi(double &x, double y) { return x /= y; } n inline long long mod(int &x, long long y) { return x %= y; } n inline long long mod(long long &x, long long y) { return x %= y; } n long long n, m, i, j, l, r, ans[200005], f, lst; n map<long long, long long> cnt; n int main() { n   readll(n); n   for (i = 1; i <= n; i++) { n     readll(l); n     readll(r); n     cnt[l]++; n     cnt[r + 1]--; n   } n   lst = f = 0; n   for (__typeof((cnt).begin()) it = (cnt).begin(); it != (cnt).end(); it++) { n     ans[f] += (it->first - lst); n     lst = it->first; n     f += it->second; n   } n   if ((1) <= ((n))) n     for (((i)) = (1); ((i)) <= ((n)); ((i))++) printf( %I64d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     static int aa[100]; n     static char used[100]; n     int n, m, i, yes; n     scanf( %d%d , &n, &m); n     for (i = 0; i < n; i++) scanf( %d , &aa[i]); n     memset(used, 0, (n - 1) * sizeof *used); n     while (m--) { n       scanf( %d , &i), i--; n       if (!used[i]) used[i] = 1; n     } n     yes = 1; n     for (i = 0; i < n; i++) { n       int j, j_; n       j_ = -1; n       for (j = i; j < n; j++) n         if (j_ == -1 || aa[j_] > aa[j]) j_ = j; n       if (j_ != i) { n         int k, good; n         good = 1; n         for (k = j_ - 1; k >= i; k--) { n           int tmp; n           if (!used[k]) { n             good = 0; n             break; n           } n           tmp = aa[k], aa[k] = aa[k + 1], aa[k + 1] = tmp; n         } n         if (!good) { n           yes = 0; n           break; n         } n       } n     } n     printf(yes ?  YES n  :  NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[200005]; n queue<pair<int, int> > q; n int deg[200005], a, n; n bool v[200005]; n set<int> s[200005]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n     deg[a]++, deg[b]++; n   } n   for (int i = 1; i <= n; i++) n     if (deg[i] == 1) q.push(pair<int, int>(i, 0)); n   int c = 0, lst = -1; n   while (!q.empty()) { n     int x = q.front().first, d = q.front().second; n     q.pop(); n     v[x] = true; n     lst = x, c++; n     for (auto y : g[x]) { n       if (v[y]) continue; n       deg[y]--; n       s[y].insert(d + 1); n       if (deg[y] == 1 and s[y].size() == 1) q.push(pair<int, int>(y, d + 1)); n     } n   } n   if (c < n - 1) n     a = -1; n   else { n     if (c == n - 1) n       for (int i = 1; i <= n; i++) n         if (!v[i]) lst = i; n     if (s[lst].size() > 2) n       a = -1; n     else { n       for (auto e : s[lst]) a += e; n     } n   } n   while (a % 2 == 0) a /= 2; n   printf( %d n , a); n } n


#include <bits/stdc++.h> n int x[100005]; n using namespace std; n bool cmp(int a, int b) { return a < b; } n int main() { n   int n, i, q, k; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &x[i]); n   } n   sort(x, x + n, cmp); n   scanf( %d , &q); n   while (q--) { n     int m; n     scanf( %d , &m); n     printf( %d n , upper_bound(x, x + n, m) - x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1005], b[1005]; n int x[1005], y[1005]; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     memset(x, 0, sizeof(x)); n     memset(y, 0, sizeof(y)); n     for (int i = 0; i < n; ++i) { n       scanf( %d%d , &a[i], &b[i]); n       x[a[i]]++; n       y[b[i]]++; n     } n     int sum = 0; n     for (int i = 0; i < n; ++i) { n       if (x[b[i]]) { n         if (a[i] != b[i]) { n           sum += x[b[i]]; n           x[b[i]] = 0; n         } else if (y[a[i]] > 1) { n           sum += x[b[i]]; n           x[b[i]] = 0; n         } else if (x[b[i]] > 1) { n           sum += x[b[i]] - 1; n           x[b[i]] = 1; n         } n       } n     } n     printf( %d n , n - sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const int INF = 0x3f3f3f3f; n int sgn(double a) { return ((a > EPS) ? (1) : ((a < -EPS) ? (-1) : (0))); } n int cmp(double a, double b = 0.0) { return sgn(a - b); } n struct node { n   int x, y; n   node(int x = 0, int y = 0) : x(x), y(y) {} n }; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   bool rated = false; n   vector<int> rating; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     if (x != y) n       rated = true; n     else n       rating.push_back(y); n   } n   if (rated) n     cout <<  rated n ; n   else { n     for (int i = 0; i < n - 1; i++) n       if (rating[i] < rating[i + 1]) { n         cout <<  unrated n ; n         return 0; n       } n     cout <<  maybe n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n const long long mod = 998244353; n vector<long long> adj[N]; n long long dp[N][4], f[N][3]; n void dfs(long long x) { n   if (adj[x].size() == 0) { n     dp[x][2] = 1; n     return; n   } else { n     dp[x][0] = 1; n   } n   for (long long i : adj[x]) { n     dfs(i); n     dp[x][2] = (dp[x][2] * (dp[i][0] + dp[i][2]) + n                 (dp[x][1] + dp[x][2]) * (dp[i][1] + dp[i][2])) % n                mod; n     dp[x][1] = n         (dp[x][1] * (dp[i][0] + dp[i][2]) + dp[x][0] * (dp[i][1] + dp[i][2])) % n         mod; n     dp[x][0] = (dp[x][0] * (dp[i][0] + dp[i][2])) % mod; n   } n } n signed main() { n   long long n, i, j, k; n   cin >> n; n   for (i = 2; i <= n; i++) { n     cin >> j; n     adj[j].push_back(i); n   } n   dfs(1); n   cout << (dp[1][0] + dp[1][2]) % mod; n } n


#include <bits/stdc++.h> n int main() { n   int x; n   scanf( %d , &x); n   if (x == 3) n     printf( 5 n ); n   else { n     for (int i = 1; i <= 100; i += 2) { n       if (i * i - (i * i) / 2 >= x) { n         printf( %d n , i); n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[111111], b[111111]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].first); n     a[i].second = i; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i].first); n     b[i].second = i; n   } n   int m; n   scanf( %d , &m); n   sort(a + 1, a + 1 + n); n   sort(b + 1, b + 1 + n); n   long long ret = 1; n   for (int i = 1, j = 1; i <= n || j <= n;) { n     int val; n     if (i <= n && (j > n || a[i].first <= b[j].first)) n       val = a[i].first; n     else n       val = b[j].first; n     int cnt = 0, l2 = 0; n     map<int, bool> f; n     f.clear(); n     for (; i <= n && a[i].first == val; i++) { n       cnt++; n       f[a[i].second] = 1; n     } n     for (; j <= n && b[j].first == val; j++) { n       cnt++; n       if (f[b[j].second]) l2++; n     } n     for (int k = 2; k <= cnt; k++) { n       int now = k; n       while (now % 2 == 0 && l2) { n         now /= 2; n         l2--; n       } n       ret = (ret * now) % m; n     } n   } n   printf( %I64d , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fact[300001]; n int invfact[300001]; n int pw(int a, int e) { n   if (e == 0) return 1; n   int tmp = pw(a, e / 2); n   tmp = (tmp * ((long long)tmp)) % 1000000007; n   if (e & 1) tmp = (tmp * ((long long)a)) % 1000000007; n   return tmp; n } n void computeFactorials() { n   fact[0] = invfact[0] = 1; n   for (int i = 1; i <= 300000; i++) { n     fact[i] = (fact[i - 1] * ((long long)i)) % 1000000007; n     invfact[i] = pw(fact[i], 1000000007 - 2); n   } n } n void mult(int &a, int b) { a = (a * ((long long)b)) % 1000000007; } n void add(int &a, int b) { n   a = a + b; n   if (a >= 1000000007) a -= 1000000007; n } n void sub(int &a, int b) { n   a = a + 1000000007 - b; n   if (a >= 1000000007) a -= 1000000007; n } n int chs(int n, int k) { n   if (k > n) return 0; n   int tmp = (fact[n] * ((long long)invfact[k])) % 1000000007; n   tmp = (tmp * ((long long)invfact[n - k])) % 1000000007; n   return tmp; n } n vector<int> pcs[300001]; n int A[300000]; n int occ[20]; n int ps[20]; n int N, C; n int tmp[300001]; n int computePrime(int p) { n   for (int i = 0; i < 20; i++) occ[i] = 0; n   for (int j = 0; j < pcs[p].size(); j++) { n     int l = 0; n     while (pcs[p][j] % p == 0) { n       l++; n       pcs[p][j] /= p; n     } n     occ[l]++; n   } n   occ[0] = N - pcs[p].size(); n   int numLess = 0; n   int numMore; n   int ans = 0; n   for (int k = 0; k < 19; k++) { n     numLess += occ[k]; n     numMore = N - numLess; n     tmp[0] = 0; n     for (int i = 1; i < numMore; i++) { n       tmp[i] = i; n       mult(tmp[i], chs(numLess, i)); n       add(tmp[i], tmp[i - 1]); n       int cur = tmp[i]; n       mult(cur, chs(numMore, i + 1)); n       add(ans, cur); n     } n     tmp[0] = pw(2, numLess); n     for (int i = 1; i <= numMore; i++) { n       tmp[i] = tmp[i - 1]; n       sub(tmp[i], chs(numLess, i - 1)); n       int cur = tmp[i]; n       mult(cur, chs(numMore, i)); n       mult(cur, i); n       add(ans, cur); n     } n   } n   return ans; n } n int main() { n   cin >> N; n   computeFactorials(); n   for (int i = 0; i < N; i++) { n     cin >> A[i]; n     int c = A[i]; n     for (int j = 2; j * j <= A[i]; j++) { n       if (c % j == 0) { n         pcs[j].push_back(A[i]); n         while (c % j == 0) c /= j; n       } n     } n     if (c > 1) { n       pcs[c].push_back(A[i]); n     } n   } n   int ans = 0; n   for (int i = 1; i <= 300000; i++) n     if (pcs[i].size() > 0) add(ans, computePrime(i)); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (1e9) + 7; n const long double EPS = 0.0000001; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int pa; n   cin >> pa; n   deque<int> arr; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (i == pa - 1) { n       if (x != 0) { n         x = 0; n         ans++; n       } n     } n     arr.push_back(x); n   } n   sort(arr.begin(), arr.end()); n   arr.pop_front(); n   int cur = 0; n   while (arr.front() == 0) { n     arr.pop_front(); n     arr.push_back(INT_MAX); n   } n   while (arr.size()) { n     cur++; n     int x = arr.front(); n     if (x == cur) { n       while (arr.size() && arr.front() == cur) arr.pop_front(); n       continue; n     } n     while (arr.size()) { n       x = arr.front(); n       if (cur == x) { n         cur--; n         break; n       } n       arr.pop_back(); n       cur++; n       ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m; n int a[10][10]; n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> m; n     int ok = 0; n     for (int i = 1; i <= n; i++) { n       cin >> a[1][1] >> a[1][2] >> a[2][1] >> a[2][2]; n       if (a[1][2] == a[2][1]) { n         ok = 1; n       } n     } n     if (m % 2) ok = 0; n     if (ok) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   int n; n   cin >> t; n   for (int u = 0; u < t; u++) { n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     vector<int> v; n     int mini = arr[n - 1]; n     for (int i = (n - 2); i >= 0; i--) { n       if (mini < arr[i]) n         v.push_back(arr[i]); n       else n         mini = arr[i]; n     } n     cout << v.size() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000000 + 7; n const int N = 100000 + 50; n vector<int> a; n int val[N]; n int p[N]; n int rp[N]; n int avl[N]; n pair<int, int> dp[N]; n int curn; n pair<int, int> bit[N]; n vector<vector<int> > fa; n void clear_bit(int n) { n   for (int i = 0; i < n + 2; i++) { n     bit[i] = {0, -1}; n   } n } n void upd(int i, int x, int y) { n   while (i < curn) { n     if (x > bit[i].first) { n       bit[i] = {x, y}; n     } n     i += i & (-i); n   } n } n pair<int, int> get(int i) { n   pair<int, int> ans = {0, 0}; n   while (i > 0) { n     if (bit[i].first > ans.first) { n       ans = bit[i]; n     } n     i -= i & (-i); n   } n   return ans; n } n void build() { n   int n = a.size(); n   if (n == 0) return; n   clear_bit(n + 3); n   curn = n + 2; n   pair<int, int> vl = {0, 0}; n   for (int i = 1; i <= n; i++) { n     avl[i] = 1; n     auto an = get(a[i - 1]); n     dp[i] = {an.first + 1, an.second}; n     if (dp[i].first > vl.first) { n       vl = {dp[i].first, i}; n     } n     upd(a[i - 1], dp[i].first, i); n   } n   if (val[n - vl.first] < val[n]) { n     vector<int> ans; n     int cur = vl.second; n     while (cur > 0) { n       avl[a[cur - 1]] = 0; n       ans.push_back(p[a[cur - 1]]); n       cur = dp[cur].second; n     } n     reverse(ans.begin(), ans.end()); n     fa.push_back(ans); n     int c = 0; n     for (int i = 1; i <= n; i++) { n       c += avl[i]; n       if (avl[i] == 1) { n         p[c] = p[i]; n         rp[i] = c; n       } n     } n     vector<int> temp; n     for (int i = 1; i <= n; i++) { n       if (avl[a[i - 1]]) { n         temp.push_back(rp[a[i - 1]]); n       } n     } n     a = temp; n     build(); n     return; n   } else { n     vector<int> v[n + 5]; n     for (int i = 1; i <= n; i++) { n       v[dp[i].first].push_back(p[a[i - 1]]); n     } n     for (int i = 0; i <= n; i++) { n       if (v[i].size()) fa.push_back(v[i]); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int cur = 2; n   for (int i = 1; i < N; i++) { n     for (int j = 0; j < cur && i + j < N; j++) { n       val[i + j] = cur - 1; n     } n     i += cur - 1; n     cur++; n   } n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     a.clear(); n     for (int i = 1; i <= n; i++) { n       p[i] = i; n       int x; n       cin >> x; n       a.push_back(x); n     } n     fa.clear(); n     build(); n     cout << fa.size() << endl; n     for (auto& it : fa) { n       cout << it.size() <<    ; n       for (auto it1 : it) cout << it1 <<    ; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n template <typename T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n const int maxn = 1e5 + 5; n const long long mod = 1e9 + 7; n int Case = 1; n int n, m; n struct node { n   int x, y, xx, yy; n } a, b, c; n vector<int> A, B; n int getid(int x, int id) { n   if (id == 1) n     return lower_bound(((A).begin()), ((A).end()), x) - A.begin() + 1; n   else n     return lower_bound(((B).begin()), ((B).end()), x) - B.begin() + 1; n } n int maze[20][20]; n void solve() { n   cin >> a.x >> a.y >> a.xx >> a.yy; n   cin >> b.x >> b.y >> b.xx >> b.yy; n   cin >> c.x >> c.y >> c.xx >> c.yy; n   A.push_back(a.x * 2); n   A.push_back(a.xx * 2); n   A.push_back(a.x * 2 + 1); n   A.push_back(a.xx * 2 + 1); n   B.push_back(a.y * 2); n   B.push_back(a.yy * 2); n   B.push_back(a.y * 2 + 1); n   B.push_back(a.yy * 2 + 1); n   A.push_back(b.x * 2); n   A.push_back(b.xx * 2); n   A.push_back(b.x * 2 + 1); n   A.push_back(b.xx * 2 + 1); n   B.push_back(b.y * 2); n   B.push_back(b.yy * 2); n   B.push_back(b.y * 2 + 1); n   B.push_back(b.yy * 2 + 1); n   A.push_back(c.x * 2); n   A.push_back(c.xx * 2); n   A.push_back(c.x * 2 + 1); n   A.push_back(c.xx * 2 + 1); n   B.push_back(c.y * 2); n   B.push_back(c.yy * 2); n   B.push_back(c.y * 2 + 1); n   B.push_back(c.yy * 2 + 1); n   sort(((A).begin()), ((A).end())); n   A.erase(unique(((A).begin()), ((A).end())), A.end()); n   sort(((B).begin()), ((B).end())); n   B.erase(unique(((B).begin()), ((B).end())), B.end()); n   int x = getid(b.x * 2, 1), xx = getid(b.xx * 2, 1); n   int y = getid(b.y * 2, 2), yy = getid(b.yy * 2, 2); n   for (int i = x; i <= xx; i++) { n     for (int j = y; j <= yy; j++) { n       maze[i][j] = 1; n     } n   } n   x = getid(c.x * 2, 1), xx = getid(c.xx * 2, 1); n   y = getid(c.y * 2, 2), yy = getid(c.yy * 2, 2); n   for (int i = x; i <= xx; i++) { n     for (int j = y; j <= yy; j++) { n       maze[i][j] = 1; n     } n   } n   x = getid(a.x * 2, 1), xx = getid(a.xx * 2, 1); n   y = getid(a.y * 2, 2), yy = getid(a.yy * 2, 2); n   for (int i = x; i <= xx; i++) { n     for (int j = y; j <= yy; j++) { n       if (maze[i][j] == 0) { n         cout <<  YES n ; n         return; n       } n     } n   } n   cout <<  NO n ; n   return; n } n int main() { n   while (Case--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void calc() { n   long long n, m; n   cin >> n >> m; n   vector<pair<long long, long long> > v; n   vector<long long> a; n   for (long long i = 0; i < m; i++) { n     long long ai, bi; n     cin >> ai >> bi; n     v.push_back(make_pair(ai, bi)); n     a.push_back(ai); n   } n   sort(a.begin(), a.end()); n   reverse(a.begin(), a.end()); n   vector<long long> acc(m + 2); n   for (long long i = 1; i <= m; i++) { n     acc[i] = acc[i - 1] + a[i - 1]; n   } n   long long ans = -1; n   for (long long i = 0; i < m; i++) { n     long long ai = v[i].first; n     long long bi = v[i].second; n     long long lo, hi; n     lo = 0; n     hi = m; n     while (lo + 1 != hi) { n       long long mid = (lo + hi) / 2; n       if (a[mid] > bi) n         lo = mid; n       else n         hi = mid; n     } n     long long curr, rem; n     if (a[lo] > bi) { n       curr = ai; n       rem = n - 1; n       if (rem > 0) { n         long long used = min(lo + 1, rem); n         curr += acc[used]; n         if (a[used - 1] <= ai) { n           curr -= ai; n           rem -= used; n           rem++; n           if (used + 1 <= m && a[used] > bi) { n             rem--; n             curr += a[used]; n           } n         } else { n           rem -= used; n         } n         if (rem > 0) curr += rem * bi; n       } n     } else { n       curr = ai; n       rem = n - 1; n       if (rem > 0) curr += rem * bi; n     } n     ans = max(ans, curr); n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) calc(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 7; n const int inf = INT_MAX; n const long long inff = 5e18; n const long long mod = 1e9 + 7; n long long f[7]; n long long dp[maxn], dp2[maxn]; n vector<long long> value = {0, 0, 0, 1, 0, 0, 2, 0, 0, 3}; n void update(long long x, long long dd) { n   for (long long i = 999999; i >= 0; i--) n     if (i >= x) dp[i] = max(dp[i], dp[i - x] + dd); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long k; n   cin >> k; n   for (long long i = 0; i < 6; i++) cin >> f[i]; n   for (long long i = 1; i <= 999999; i++) dp[i] = -inff; n   long long now = 3; n   for (long long i = 0; i < 6; i++) { n     long long cnt = 3 * (k - 1); n     for (long long j = 1;; j <<= 1) { n       if (j >= cnt) { n         update(now * cnt, f[i] * cnt); n         break; n       } else n         update(now * j, f[i] * j), cnt -= j; n     } n     now = now * 10; n   } n   now = 1; n   for (long long i = 0; i < 6; i++) { n     for (long long k = 0; k <= 999999; k++) dp2[k] = dp[k]; n     for (long long j = 0; j < 10; j++) { n       for (long long k = 1; k <= 999999; k++) n         if (k >= now * j) n           dp2[k] = max(dp2[k], dp[k - j * now] + value[j] * f[i]); n     } n     for (long long k = 0; k <= 999999; k++) dp[k] = max(dp2[k], dp[k]); n     now = now * 10; n   } n   long long q; n   cin >> q; n   while (q--) { n     long long x; n     cin >> x; n     cout << dp[x] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, i; n const int maxn = 1e5 + 20; n int arr[maxn]; n int main() { n   cin >> n >> m; n   int x = m; n   if (m <= n * 2) { n     for (int i = 1; i <= m; i++) { n       cout << i <<    ; n     } n   } else { n     for (i = 1; i <= (m - n * 2); i++) { n       cout << i + (n * 2) <<     << i <<    ; n       arr[i + (n * 2)]++, arr[i]++; n     } n     for (int i = 1; i <= m; i++) { n       if (arr[i] == 0) cout << i <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n long long dp[10000005], sol = 0; n int main() { n   int n, i; n   long long x, y; n   scanf( %d%I64d%I64d , &n, &x, &y); n   for (i = 1; i <= 10000005; i++) dp[i] = inf; n   dp[1] = x; n   for (i = 2; i <= n; i++) { n     dp[i] = min(dp[i], dp[i - 1] + x); n     if (i % 2 == 0) dp[i] = min(dp[i], dp[i / 2] + y); n     if (i % 2 == 1) { n       dp[i] = min(dp[i], dp[(i - 1) / 2] + y + x); n       dp[i] = min(dp[i], dp[(i + 1) / 2] + y + x); n     } n   } n   printf( %I64d n , dp[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pen[2000005]; n void update(int ind, int add) { n   while (ind < 2000005) { n     pen[ind] += add; n     ind += ind & (-ind); n   } n } n int query(int ind) { n   int ret = 0; n   while (ind > 0) { n     ret = ret + pen[ind]; n     ind = ind & (ind - 1); n   } n   return ret; n } n pair<int, int> r[2000005]; n bool vis[2000005]; n int getIndex(int y) { n   int sum = 0; n   int ind = 0; n   for (int i = 20; i >= 0; --i) { n     int nind = ind + (1 << i); n     if (nind < 2000005) { n       int nsum = sum + pen[nind]; n       if (nsum < y) { n         sum = nsum; n         ind = nind; n       } n     } n   } n   return ind + 1; n } n int out[2000005]; n int mapping[2000005]; n void solve() { n   int n, m; n   scanf(  %d %d  , &n, &m); n   int pos = m + 1; n   for (int i = 1; i <= n; ++i) { n     r[pos + i] = {-1, i}; n     update(pos + i, 1); n   } n   for (int i = 0; i < m; ++i) { n     int x, y; n     scanf( %d %d  , &x, &y); n     int ind = getIndex(y); n     if (vis[x]) { n       if (r[ind].first != x) { n         printf( -1 n ); n         return; n       } n     } else { n       if (r[ind].first != -1) { n         printf( -1 n ); n         return; n       } n     } n     update(ind, -1); n     update(pos, 1); n     r[pos] = r[ind]; n     r[pos].first = x; n     vis[x] = true; n     --pos; n   } n   int p = 1; n   for (int i = 1; i <= n; ++i) { n     int ind = getIndex(i); n     if (r[ind].first == -1) { n       while (p <= n) { n         if (vis[p]) { n           ++p; n         } else { n           break; n         } n       } n       r[ind].first = p++; n     } n     out[r[ind].second] = r[ind].first; n   } n   for (int i = 1; i <= n; ++i) { n     printf( %d  , out[i]); n   } n } n int main() { n   solve(); n   return 0; n } n


#include<bits/stdc++.h> n #define For(i,x,y) for (register int i=(x);i<=(y);i++) n #define FOR(i,x,y) for (register int i=(x);i<(y);i++) n #define Dow(i,x,y) for (register int i=(x);i>=(y);i--) n #define Debug(v) for (auto i:v) cout<<i<<   ;puts(  ) n #define mp make_pair n #define fi first n #define se second n #define pb push_back n #define ep emplace_back n #define siz(x) ((int)(x).size()) n #define all(x) (x).begin(),(x).end() n #define fil(a,b) memset((a),(b),sizeof(a)) n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n typedef pair<int,int> pa; n typedef pair<ll,ll> PA; n typedef vector<int> poly; n inline ll read(){ n  ll x=0,f=1;char c=getchar(); n  while ((c< 0 ||c> 9 )&&(c!= - )) c=getchar(); n  if (c== - ) f=-1,c=getchar(); n  while (c>= 0 &&c<= 9 ) x=x*10+c- 0 ,c=getchar(); n  return x*f; n } n  n const int N = 5010; n int n,p[N]; n pa a[N]; n  n inline pa operator - (pa a,pa b){ n  return mp(a.fi-b.fi,a.se-b.se); n } n inline bool check(pa a,pa b,pa c){ n  pa A=a-b,B=c-b; n  return (1ll*A.fi*B.fi+1ll*A.se*B.se)>0; n } n  n int main(){ n  n=read(); n  For(i,1,n) a[i].fi=read(),a[i].se=read(),p[i]=i; n  For(i,3,n){ n   Dow(j,i,3) if (!check(a[p[j-2]],a[p[j-1]],a[p[j]])){ n    swap(p[j],p[j-1]); n   } n  } n  For(i,1,n) printf( %d  ,p[i]); n }


#include <bits/stdc++.h> n using namespace std; n bool compare(const pair<int, int> &i, const pair<int, int> &j) { n   return i.second > j.second; n } n int n, d; n vector<pair<long long, long long>> a; n long long p(long long a, long long b) { n   long long x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y) % 998244353; n     } n     y = (y * y) % 998244353; n     b /= 2; n   } n   return x % 998244353; n } n long long inv(long long a) { return p(a, 998244353 - 2); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   map<char, int> m; n   vector<char> v; n   for (int i = 0; i < s.length(); i++) { n     m[s[i]]++; n   } n   for (auto x : m) n     if (x.second % 2) v.push_back(x.first); n   for (int i = v.size() - 1, j = 0; i > j; i--, j++) { n     if (i <= j) break; n     m[v[j]]++, m[v[i]]--; n   } n   string str =   ; n   char flag =  0 ; n   for (auto x : m) { n     if (!x.second) continue; n     if (x.second % 2) flag = x.first; n     for (int i = 0; i < (x.second) / 2; i++) { n       str += x.first; n     } n   } n   string ans = str; n   if (flag !=  0 ) { n     ans += flag; n   } n   reverse(str.begin(), str.end()); n   ans += str; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> g[100005]; n long long int vis[100005]; n vector<double> ans; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     long long int a, b, c, d; n     cin >> a >> b >> c >> d; n     if ((b - a) % (c + d) == 0) n       cout << (b - a) / (c + d); n     else n       cout << -1; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   long long n, k; n   cin >> n >> k; n   long long a[k]; n   map<long long, bool> m; n   for (long long i = 0; i < k; i++) { n     cin >> a[i]; n     m[a[i]] = true; n   } n   long long cnt = 1; n   for (long long i = 0; i < k; i++) { n     cout << a[i]; n     for (long long i = 0; i < n - 1; i++) { n       if (!m[cnt]) n         cout <<     << cnt; n       else n         i--; n       cnt++; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mx = 1e6 + 5; n const int inf = 0x3f3f3f3f; n using namespace std; n vector<int> ans; n int dfs(long long int a, long long int b) { n   if (a > b) return 0; n   if (a == b) return 1; n   if (dfs(a * 2, b)) { n     ans.push_back(2 * a); n     return 1; n   } n   if (dfs(a * 10 + 1, b)) { n     ans.push_back(a * 10 + 1); n     return 1; n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int a, b; n   cin >> a >> b; n   if (dfs(a, b)) { n     printf( YES n%d n%d  , int(1 + ans.size()), a); n     for (int i = ans.size() - 1; i >= 0; --i) printf( %d  , ans[i]); n     puts(  ); n   } else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int sqr(int x) { return x * x; } n int main() { n   int n, m, x, y; n   map<int, int> targets; n   vector<int> ans, r; n   scanf( %d , &n); n   ans.assign(n, -1); n   r.assign(n, 0); n   for (int i = 0; i != n; ++i) { n     scanf( %d%d , &x, &r[i]); n     targets[x] = i; n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &x, &y); n     auto target = targets.lower_bound(x); n     if (target != targets.end()) { n       if (ans[target->second] == -1 && n           sqr(target->first - x) + sqr(y) <= sqr(r[target->second])) n         ans[target->second] = i; n     } n     if (target != targets.begin()) { n       --target; n       if (ans[target->second] == -1 && n           sqr(target->first - x) + sqr(y) <= sqr(r[target->second])) n         ans[target->second] = i; n     } n   } n   printf( %d n , n - count(ans.begin(), ans.end(), -1)); n   for (auto itans = ans.begin(); itans != ans.end(); ++itans) n     printf( %d  , *itans); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   int x0, y0, x, y, n; n   int i, j, r, a, b; n   set<pair<int, int> > s; n   queue<pair<int, pair<int, int> > > q; n   cin >> x0 >> y0 >> x >> y >> n; n   for (i = 0; i < n; i++) { n     cin >> r >> a >> b; n     for (j = a; j <= b; j++) s.insert(make_pair(r, j)); n   } n   q.push(make_pair(0, make_pair(x0, y0))); n   while (!q.empty()) { n     int cr = q.front().first; n     int cx = q.front().second.first; n     int cy = q.front().second.second; n     q.pop(); n     for (int i = -1; i <= 1; i++) n       for (int j = -1; j <= 1; j++) { n         if (cx + i == x && cy + j == y) { n           cout << cr + 1 << endl; n           return 0; n         } n         if (i == 0 && j == 0) continue; n         if (s.count(make_pair(cx + i, cy + j)) > 0) { n           q.push(make_pair(cr + 1, make_pair(cx + i, cy + j))); n           s.erase(make_pair(cx + i, cy + j)); n         } n       } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e6 + 10, inf = 1e9 + 10; n long long int t, n, ans, a[mx]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     cout << (n + 1) / 10 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[10000]; n int n = 0; n void init() { n   queue<string> q; n   q.push( 4 ); n   q.push( 7 ); n   while (!q.empty()) { n     string u = q.front(); n     q.pop(); n     arr[n++] = stoi(u); n     if (u.length() < 9) { n       q.push(u +  4 ); n       q.push(u +  7 ); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   init(); n   int t = 1; n   while (t--) { n     int k; n     cin >> k; n     cout << lower_bound(arr, arr + n, k) - arr + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void mod(string &s) { n   for (int i = 0; i < (int)s.size(); i++) { n     switch (s[i]) { n       case  N : n         s[i] =  S ; n         break; n       case  S : n         s[i] =  N ; n         break; n       case  E : n         s[i] =  W ; n         break; n       case  W : n         s[i] =  E ; n         break; n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   string a, b; n   cin >> a >> b; n   n = (int)a.size(); n   reverse(b.begin(), b.end()); n   mod(b); n   b += a; n   vector<int> pref(2 * n, 0); n   int i = 1, j = 0; n   while (i < 2 * n) { n     if (b[j] == b[i]) { n       pref[i++] = ++j; n     } else { n       while (j != 0 && b[j] != b[i]) j = pref[j - 1]; n       if (b[j] != b[i]) i++; n     } n   } n   if (pref[2 * n - 1]) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n struct fastio { n   char s[100000]; n   int it, len; n   fastio() { it = len = 0; } n   inline char get() { n     if (it < len) return s[it++]; n     it = 0; n     len = fread(s, 1, 100000, stdin); n     if (len == 0) n       return EOF; n     else n       return s[it++]; n   } n   bool notend() { n     char c = get(); n     while (c ==     || c ==   n ) c = get(); n     if (it > 0) it--; n     return c != EOF; n   } n } _buff; n inline long long getnum() { n   long long r = 0; n   bool ng = 0; n   char c; n   c = _buff.get(); n   while (c !=  -  && (c <  0  || c >  9 )) c = _buff.get(); n   if (c ==  - ) ng = 1, c = _buff.get(); n   while (c >=  0  && c <=  9 ) r = r * 10 + c -  0 , c = _buff.get(); n   return ng ? -r : r; n } n template <class T> n inline void putnum(T x) { n   if (x < 0) putchar( - ), x = -x; n   register short a[20] = {}, sz = 0; n   while (x) a[sz++] = x % 10, x /= 10; n   if (sz == 0) putchar( 0 ); n   for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]); n } n inline char getreal() { n   char c = _buff.get(); n   while (c <= 32) c = _buff.get(); n   return c; n } n long long qpow(long long x, long long k) { n   return k == 0 ? 1 n                 : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod; n } n const int maxn = 300111; n int n, q; n int a[maxn]; n void makev(vector<pair<int, int> > &v) { n   sort(v.begin(), v.end()); n   int c = 0; n   vector<pair<int, int> > nv; n   for (auto x : v) { n     if ((c | x.second) > c) { n       c = c | x.second; n       nv.push_back(make_pair(x.first, c)); n     } n   } n   v = nv; n } n vector<pair<int, int> > v[maxn]; n int lst[20]; n int main() { n   n = getnum(), q = getnum(); n   for (int i = 1; i <= n; i++) a[i] = getnum(); n   for (int i = n; i >= 1; i--) { n     vector<pair<int, int> > &cur = v[i]; n     cur.push_back(make_pair(i, a[i])); n     for (int j = 0; j < 20; j++) { n       if ((a[i] >> j) & 1) { n         if (lst[j]) { n           for (auto x : v[lst[j]]) cur.push_back(x); n         } n         lst[j] = i; n       } n     } n     makev(cur); n   } n   for (int i = 1; i <= q; i++) { n     int x, y; n     x = getnum(), y = getnum(); n     bool ok = false; n     for (auto t : v[x]) { n       ok |= t.first <= y && ((t.second & a[y]) > 0); n     } n     puts(ok ?  Shi  :  Fou ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> piles(n); n   vector<pair<int, int>> cost(n); n   for (int i = 0; i < n; i++) { n     int tmp; n     scanf( %d , &tmp); n     piles[i] = tmp; n     int closest = int(sqrt(tmp) + 0.5f); n     cost[i] = pair<int, int>(abs(tmp - closest * closest), tmp); n   } n   sort(cost.begin(), cost.end()); n   unsigned long long result = 0; n   for (int i = 0; i < n; i++) { n     int _cost = cost[i].first; n     int _number = cost[i].second; n     if (i < n / 2) { n       result += _cost; n     } else { n       if (_cost == 0) result++; n       if (_number == 0) result++; n     } n   } n   printf( %llu n , result); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(int a, int b) { n   bool dig[10] = {0}; n   while (a > 0) { n     dig[a % 10] = true; n     a /= 10; n   } n   while (b > 0) { n     if (dig[b % 10]) { n       return true; n     } n     b /= 10; n   } n   return false; n } n int main() { n   int a; n   cin >> a; n   int i, s = 0; n   for (i = 1; i * i <= a; i++) { n     if (a % i == 0) { n       if (check(a, i)) { n         s++; n       } n       if (i != a / i && check(a, a / i)) { n         s++; n       } n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   t = 1; n   while (t--) { n     int c = 0; n     std::vector<int> v; n     string s[3]; n     getline(cin, s[0]); n     getline(cin, s[1]); n     getline(cin, s[2]); n     for (int i = 0; i <= 2; i++) { n       for (int j = 0; j <= s[i].length() - 1; j++) { n         if (s[i][j] ==  a  || s[i][j] ==  e  || s[i][j] ==  i  || n             s[i][j] ==  o  || s[i][j] ==  u ) n           c++; n       } n       v.push_back(c); n       c = 0; n     } n     if (v[0] == 5 && v[1] == 7 && v[2] == 5) { n       cout <<  YES ; n     } else { n       cout <<  NO ; n     } n   } n   cerr <<  Time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   string s =    ; n   sort(b.rbegin(), b.rend()); n   if (a.size() > b.size()) n     s = b.substr(0, a.size()); n   else n     s = b; n   for (int i = 0; i < a.size(); i++) { n     if (a[i] ==  9 ) n       continue; n     else if ((a[i] +  0 ) < (s[0] +  0 )) { n       a[i] = s[0]; n       s.erase(0, 1); n     } n   } n   cout << a << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, ans =   ; n   cin >> s; n   int i = 0, j = s.size() - 1; n   while (i < j) { n     if (s[i] == s[j]) { n       i++; n       j--; n       continue; n     } else { n       s.insert(s.begin() + j + 1, s[i]); n       i++; n     } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, flag; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   if (n == 2) n     cout << a[0] + 2 * (a[1] - a[0]); n   else { n     for (int i = 0; i < n - 2; i++) { n       if ((a[i + 2] - a[i + 1]) != (a[1 + i] - a[i])) { n         flag = 0; n         break; n       } else n         flag = 1; n     } n     if (flag) { n       cout << a[0] + (n) * (a[1] - a[0]); n     } else n       cout << a[n - 1]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void Read(int &p) { n   p = 0; n   char c = getchar(); n   for (; c <  0  || c >  9 ; c = getchar()) n     ; n   for (; c >=  0  && c <=  9 ; c = getchar()) p = p * 10 + c -  0 ; n } n struct ma { n   int x, y; n   ma(int _x = 0, int _y = 0) : x(_x), y(_y) {} n }; n bool operator<(ma a, ma b) { return a.x < b.x; } n priority_queue<ma> q[100005], Q, _q; n int n, m, x, y, z, rot, mi = 1e9, si[100005], nm = 0, be[100005]; n int fir[100005], tar[200005], len[200005], nex[200005], cnt; n long long ans1; n void Add(int a, int b, int c) { n   ++cnt; n   tar[cnt] = b; n   len[cnt] = c; n   nex[cnt] = fir[a]; n   fir[a] = cnt; n } n int Dfs(int r, int fa) { n   int sz = 1, mx = 0; n   for (int i = fir[r]; i; i = nex[i]) { n     int v = tar[i]; n     if (v != fa) { n       int sum = Dfs(v, r); n       mx = max(mx, sum); n       sz += sum; n       ans1 += (long long)len[i] * min(sum, n - sum); n     } n   } n   mx = max(mx, n - sz); n   if (mx < mi) rot = r, mi = mx; n   return sz; n } n void DFS(int x, int fa, int d) { n   q[d].push(ma{-x, d}); n   si[d]++; n   be[x] = d; n   for (int i = fir[x]; i; i = nex[i]) n     if (tar[i] != fa) DFS(tar[i], x, d); n } n int work(int u) { n   while (_q.top().x != q[_q.top().y].top().x || q[_q.top().y].empty()) _q.pop(); n   ma o; n   if (_q.top().y == u) o = _q.top(), _q.pop(); n   while (_q.top().x != q[_q.top().y].top().x || q[_q.top().y].empty()) _q.pop(); n   u = _q.top().y; n   if (o.x) _q.push(o); n   return u; n } n int main() { n   Read(n); n   if (n == 1) { n     printf( 0 n1 n ); n     return 0; n   } n   for (int i = 1; i < n; i++) { n     int u, v, w; n     Read(u), Read(v), Read(w); n     Add(u, v, w), Add(v, u, w); n   } n   Dfs(1, 0); n   q[0].push(ma{-rot, 0}); n   _q.push(ma{-rot, 0}); n   si[0] = 1; n   for (int i = fir[rot]; i; i = nex[i]) { n     DFS(tar[i], rot, ++nm); n     Q.push(ma{si[nm] << 1, nm}); n     _q.push(ma(q[nm].top().x, nm)); n   } n   printf( %lld n , ans1 << 1); n   for (int i = 1; i <= n; i++) { n     while (Q.top().x != si[Q.top().y] + q[Q.top().y].size()) Q.pop(); n     if (be[i] == Q.top().y || Q.top().x < n - i + 1) n       m = work(be[i]); n     else n       m = Q.top().y; n     if (i == rot && -q[m].top().x > rot && !q[0].empty() && n         Q.top().x < n - i + 1) n       m = 0; n     printf( %d  , -q[m].top().x); n     q[m].pop(); n     si[be[i]]--; n     if (!q[m].empty()) _q.push(ma(q[m].top().x, m)); n     Q.push(ma(si[m] + q[m].size(), m)); n     Q.push(ma(si[be[i]] + q[be[i]].size(), be[i])); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long expo(long long base, long long exponent, long long mod) { n   long long 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 const long long mod = 998244353; n const long long root = 565042129; n const long long root_1 = 950391366; n const long long root_pw = 1 << 20; n void fft(vector<long long> &a, bool invert) { n   long long i, j, n = (long long)a.size(); n   for (i = 1, j = 0; i < n; ++i) { n     long long bit = n >> 1; n     for (; j >= bit; bit >>= 1) { n       j -= bit; n     } n     j += bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (long long len = 2; len <= n; len <<= 1) { n     long long wlen = invert ? root_1 : root; n     for (i = len; i < root_pw; i <<= 1) { n       wlen = (wlen * wlen % 998244353); n     } n     for (i = 0; i < n; i += len) { n       long long w = 1; n       for (j = 0; j < len / 2; ++j) { n         long long u = a[i + j], n                   v = (long long)(a[i + j + len / 2] * w % 998244353); n         a[i + j] = (u + v + 998244353) % 998244353; n         a[i + j + len / 2] = (u - v + 998244353) % 998244353; n         w = (w * wlen % 998244353); n       } n     } n   } n   if (invert) { n     long long nrev = expo(n, 998244353 - 2, 998244353); n     for (i = 0; i < n; ++i) { n       a[i] = (a[i] * nrev % 998244353); n     } n   } n } n void multiply(const vector<long long> &a, const vector<long long> &b, n               vector<long long> &res) { n   vector<long long> fa(a.begin(), a.end()), fb(b.begin(), b.end()); n   size_t n = 1; n   while (n < max(a.size(), b.size())) n <<= 1; n   n <<= 1; n   fa.resize(n), fb.resize(n); n   fft(fa, false), fft(fb, false); n   for (size_t i = 0; i < n; ++i) { n     fa[i] *= fb[i]; n   } n   fft(fa, true); n   res.resize(n); n   for (size_t i = 0; i < n; ++i) { n     res[i] = fa[i]; n   } n } n vector<long long> ans; n signed main() { n   long long n, first, k; n   vector<long long> pol(1001, 0); n   vector<long long> ans(1, 1); n   cin >> n >> k; n   vector<long long> temp; n   for (long long i = 0; i < (long long)n; i++) { n     cin >> first; n     pol[first] = 1; n   } n   while (k) { n     if (k % 2) { n       multiply(ans, pol, ans); n       for (long long i = 0; i < (long long)ans.size(); i++) { n         if (ans[i]) n           ans[i] = 1; n         else n           ans[i] = 0; n       } n     } n     multiply(pol, pol, pol); n     for (long long i = 0; i < (long long)pol.size(); i++) { n       if (pol[i]) n         pol[i] = 1; n       else n         pol[i] = 0; n     } n     k /= 2; n   } n   for (long long i = 0; i < (long long)ans.size(); i++) { n     if (ans[i]) cout << i <<    ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, s = 0; n   cin >> a >> b; n   while (b > 1) { n     if (b > a) swap(a, b); n     s += a / b; n     a -= (b * (a / b)); n     if (a < 1) break; n   } n   s += a; n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod_Expo(long long int x, long long int n, long long int M) { n   if (x == 0) return 0; n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return mod_Expo((x * x) % M, n / 2, M); n   else n     return (x * mod_Expo((x * x) % M, (n - 1) / 2, M)) % M; n } n long long int binaryExponentiation(long long int x, long long int n) { n   if (x == 0) return 0; n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return binaryExponentiation(x * x, n / 2); n   else n     return x * binaryExponentiation(x * x, (n - 1) / 2); n } n long long int GCD(long long int A, long long int B) { n   if (B == 0) n     return A; n   else n     return GCD(B, A % B); n } n bool prime_check(long long int x) { n   bool prime = (x >= 2); n   for (long long i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       prime = false; n       break; n     } n   } n   return prime; n } n long long int min1(long long int a, long long int b, long long int c) { n   vector<long long int> v1, v2, v3; n   v1.push_back(b); n   v1.push_back(c); n   v1.push_back(a); n   sort(v1.begin(), v1.end()); n   return v1[0]; n } n long long int min(long long int a, long long int b) { n   if (a <= b) n     return a; n   else n     return b; n } n long long int bin_search(long long int x, long long int low, long long int high, n                          vector<long long int> v1) { n   while (high > low) { n     long long int mid = low + (high - low) / 2; n     if (v1[mid] > x) n       high = mid; n     else n       low = mid + 1; n   } n   return high; n } n long long int digitsum(long long int n) { n   long long int sum = 0; n   while (n != 0) { n     sum = sum + n % 10; n     n = n / 10; n   } n   return sum; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, m, i, j, t, f = 0, g = 0, x, y, z, k, a = 0, b = 0, c = 0, q, n                                x1, x2, y1, y2, x3, max2, min1 = 1000000, x4, n                                sum, d, M = 998244353, r, p, sum1 = 0, sum2 = 0, n                                sum3 = 0, sum4, sum5, max1 = 0, sum6; n   cin >> t; n   while (t--) { n     cin >> n; n     long long int arr[n], arr1[n]; n     f = 0; n     set<long long int> s; n     for (i = 0; i < n; i++) { n       cin >> arr[i]; n       s.insert(arr[i]); n       arr1[i] = arr[i]; n     } n     sort(arr1, arr1 + n); n     for (i = n - 1; i >= 0; i--) { n       if (arr1[i] == arr[n - 1 - i]) { n         f++; n       } n     } n     if (f == n) { n       if (s.size() < n) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } else n       cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0; n   cin >> n; n   int a[n][2], maxi = -100; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 2; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= 0; j++) { n       sum = sum - a[i][j]; n       sum = sum + a[i][j + 1]; n       maxi = max(maxi, sum); n     } n   } n   cout << maxi; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int maxN = 2e5 + 100; n const long long int maxM = 1e3 + 100; n const long long int mod = 1e9 + 7; n const long long int hmod = 1e16 + 7; n const long double PI = 3.141592653; n const long double eps = 1e-8; n const long long int D = 1379; n const long long int INF = 1e18 + 20; n const long long int Inf = 1e9 + 140; n long long int dp1[maxN]; n long long int dp2[maxN]; n long long int n, a[maxN]; n long long int sum[maxN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long int i = 1; i <= n; i++) n     cin >> a[i], sum[i] = sum[i - 1] + a[i]; n   dp1[n] = sum[n]; n   dp2[n] = -dp1[n]; n   for (long long int i = n - 1; i >= 1; i--) { n     dp1[i] = max(dp1[i + 1], dp2[i + 1] + sum[i]); n     dp2[i] = -dp1[i]; n   } n   cout << dp1[2] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long heavy; n long long ukupno; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cerr.tie(0); n   cin >> s; n   heavy = 0; n   ukupno = 0; n   for (int i = 4; i < s.size(); i++) { n     if (s[i] ==  l  && s[i - 1] ==  a  && s[i - 2] ==  t  && s[i - 3] ==  e  && n         s[i - 4] ==  m ) n       ukupno += heavy; n     else if (s[i] ==  y  && s[i - 1] ==  v  && s[i - 2] ==  a  && n              s[i - 3] ==  e  && s[i - 4] ==  h ) n       heavy++; n   } n   cout << ukupno; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[93], b[93]; n   scanf( %d , &n); n   int ans = 0; n   a[0] = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[i - 1] = a[i] - a[i - 1]; n   } n   b[n] = 90 - a[n]; n   for (int i = 0; i <= n; i++) { n     if (b[i] > 15) { n       ans = a[i] + 15; n       break; n     } n   } n   if (!ans) ans = 90; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 0x1fffffffffffffff; n const double INFD = 1e20; n const double EPS = 1e-9; n const double PI = atan(1) * 4; n const long long M = 998244353; n long long mod(long long x) { return ((x % M) + M) % M; } n long long add(long long a, long long b) { return mod(mod(a) + mod(b)); } n long long sub(long long a, long long b) { return mod(mod(a) - mod(b)); } n long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); } n long long power(long long b, long long e) { n   long long ans = 1; n   while (e > 0) { n     if (e % 2) ans = mul(ans, b); n     b = mul(b, b); n     e /= 2; n   } n   return mod(ans); n } n long long gcd(long long a, long long b) { n   for (; b > 0; swap(a, b)) a %= b; n   return a; n } n long long extended_euclid(long long a, long long b, long long &x, n                           long long &y) { n   long long xx = y = 0; n   long long yy = x = 1; n   while (b) { n     long long q = a / b; n     long long t = b; n     b = a % b; n     a = t; n     t = xx; n     xx = x - q * xx; n     x = t; n     t = yy; n     yy = y - q * yy; n     y = t; n   } n   return a; n } n long long inv(long long a) { n   long long x, y; n   long long d = extended_euclid(a, M, x, y); n   if (d > 1) return -1; n   return mod(x); n } n long long scan() { n   long long r; n   scanf( %lld , &r); n   return r; n } n void scanstr(char *buf) { scanf( %s , buf); } n int main() { n   long long n = scan(); n   vector<long long> a(n); n   for (long long i = 0; i < n; ++i) { n     a[i] = scan(); n   } n   sort(a.begin(), a.end()); n   vector<long long> pfs(n + 1, 0); n   for (long long i = 0; i < n; ++i) pfs[i + 1] = add(pfs[i], a[i]); n   long long q_inv = inv(n); n   for (long long i = 1; i < n; ++i) { n     long long segcount = (i <= n / 2) ? i : (n - i); n     long long ballcount = n - i; n     long long q = ballcount / segcount; n     long long r = ballcount % segcount; n     long long ans = mul(q + 1, pfs[r]); n     for (long long j = 1; j < q + 1; ++j) { n       long long cur = mul( n           q + 1 - j, sub(pfs[r + segcount * j], pfs[r + segcount * (j - 1)])); n       ans = add(ans, cur); n     } n     cout << mul(ans, q_inv) << endl; n   } n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> adj[200005]; n long long siz = 0; n long long cc = 0; n long long t, n; n long long a[200005]; n long long vis[200005]; n long long size_dict[200005]; n void dfs(long long u) { n   vis[u] = cc; n   siz += 1; n   for (long long i = 0; i < adj[u].size(); i++) { n     if (!vis[adj[u][i]]) { n       dfs(adj[u][i]); n     } n   } n } n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     for (long long i = 0; i <= 200004; i++) { n       adj[i].clear(); n     } n     for (long long i = 0; i <= 200004; i++) vis[i] = 0; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       adj[i + 1].push_back(a[i]); n       adj[a[i]].push_back(i + 1); n     } n     cc = 0; n     for (long long i = 0; i < n; i++) { n       if (!vis[i + 1]) { n         cc++; n         dfs(i + 1); n         size_dict[cc] = siz; n         siz = 0; n       } n     } n     for (long long i = 1; i <= n; i++) { n       cout << size_dict[vis[i]] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n_keys, n_chests; n int odd_keys, even_keys, odd_chests, even_chests; n int main() { n   cin >> n_keys >> n_chests; n   for (int i = 0; i < n_keys; i++) { n     int x; n     cin >> x; n     if (x % 2 == 0) n       even_keys++; n     else n       odd_keys++; n   } n   for (int i = 0; i < n_chests; i++) { n     int x; n     cin >> x; n     if (x % 2 == 0) n       even_chests++; n     else n       odd_chests++; n   } n   cout << min(even_chests, odd_keys) + min(odd_chests, even_keys) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2) { n     cout <<  contest n ; n   } else { n     cout <<  home n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     v.push_back(x); n   } n   sort(v.begin(), v.end()); n   if (v[0] == v[n / 2]) { n     cout <<  Bob  << endl; n   } else { n     cout <<  Alice  << endl; n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, a[102], sum1 = 0, sum2 = 0; n   int s, t; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   scanf( %d %d , &s, &t); n   if (s == t) { n     printf( 0 n ); n   } else { n     int i; n     if (s > t) { n       s ^= t; n       t ^= s; n       s ^= t; n     } n     for (i = s - 1; i < t - 1; i++) sum1 += a[i]; n     for (i = t - 1; i < n; i++) sum2 += a[i]; n     for (i = 0; i < s - 1; i++) sum2 += a[i]; n     int ans; n     sum1 > sum2 ? (ans = sum2) : (ans = sum1); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 3e6 + 10, Maxk = 1e6 + 10; n int a[Maxn], l[Maxn], r[Maxn], s[Maxn], n, k; n vector<int> v[Maxk]; n long long ans; n int ask(int k, int l, int r) { n   int x = upper_bound(v[k].begin(), v[k].end(), r) - v[k].begin() - 1, n       y = lower_bound(v[k].begin(), v[k].end(), l) - v[k].begin() - 1; n   return x - y; n } n int main() { n   ios::sync_with_stdio(0); n   int i, j; n   cin >> n >> k; n   for (i = 1; i <= n; i++) cin >> a[i], s[i] = (s[i - 1] + a[i]) % k; n   for (i = 0; i <= n; i++) v[s[i]].push_back(i); n   for (i = 1; i <= n; i++) n     for (l[i] = i - 1; l[i] >= 1 && a[l[i]] < a[i]; l[i] = l[l[i]]) n       ; n   for (i = n; i >= 1; i--) n     for (r[i] = i + 1; r[i] <= n && a[r[i]] <= a[i]; r[i] = r[r[i]]) n       ; n   for (i = 1; i <= n; i++) n     if (i - l[i] < r[i] - i) { n       for (j = l[i] + 1; j <= i; j++) n         ans += ask((a[i] + s[j - 1]) % k, i, r[i] - 1); n     } else { n       for (j = i; j <= r[i] - 1; j++) n         ans += ask(((s[j] - a[i]) % k + k) % k, l[i], i - 1); n     } n   cout << ans - n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n, ans = 0; n     scanf( %d , &n); n     map<string, int> book; n     vector<string> aa, temp; n     for (int i = 1; i <= n; i++) { n       string s; n       cin >> s; n       book[s] = 1; n       temp.push_back(s); n     } n     for (vector<string>::iterator it = temp.begin(); it < temp.end(); it++) { n       string s; n       s = *it; n       if (book[s] == 1) { n         book[s]++; n         aa.push_back(s); n       } else { n         for (int i = 0; i <= 9; i++) { n           s[3] = i +  0 ; n           if (!book.count(s)) { n             book[s] = 1; n             aa.push_back(s); n             break; n           } n         } n         ans++; n       } n     } n     printf( %d n , ans); n     for (vector<string>::iterator it = aa.begin(); it < aa.end(); it++) n       cout << *it << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t = 1; n   while (t--) { n     long long l, r, i, sum = 0; n     deque<long long> a; n     a.push_back(4); n     a.push_back(7); n     cin >> l >> r; n     for (i = l; i <= r;) { n       if (i <= a.front()) { n         sum += a.front(); n         i++; n       } else { n         a.push_back(a.front() * 10 + 4); n         a.push_back(a.front() * 10 + 7); n         a.pop_front(); n       } n     } n     cout << sum <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const double PI = 3.1415926535897932384626433; n const int KL = 1e6; n using namespace std; n const long long MOD = 998244353; n int mul(int x, int y) { return x * 1LL * y % MOD; } n int add(int x, int y) { n   x += y; n   if (x >= MOD) x -= MOD; n   return x; n } n int sub(int x, int y) { n   x -= y; n   if (x < 0) x += MOD; n   return x; n } n int pow(int u, int v) { n   if (v == 0) return 1; n   int ret = pow(u, v / 2); n   ret = mul(ret, ret); n   if (v % 2 == 1) ret = mul(ret, u); n   return ret; n } n int fact[KL + 10], invi[KL + 10], inv[KL + 10]; n void init() { n   fact[0] = invi[0] = invi[1] = inv[0] = 1; n   for (int i = 1; i < KL; i++) fact[i] = mul(fact[i], i); n   for (int i = 2; i < KL; i++) invi[i] = mul(invi[MOD % i], MOD - MOD / i); n   for (int i = 1; i < KL; i++) inv[i] = mul(inv[i - 1], invi[i]); n } n int C(int u, int v) { n   if (u < v) return 0; n   return mul(fact[u], mul(inv[v], inv[u - v])); n } n int k, A, h, pw[KL], place[KL]; n vector<pair<int, int>> res[2][2]; n void solve(int mask, int pos) { n   vector<int> vec; n   if (pos == 0) n     for (int i = 1; i <= pw[k - 1]; i++) vec.push_back(i); n   else n     for (int i = pw[k - 1] + 1; i <= pw[k]; i++) vec.push_back(i); n   int ret = 0, cnt = 0; n   while (1) { n     if (vec.size() == 1) { n       place[vec[0]] = 2; n       int ad = mul(A, vec[0]); n       res[pos][0].push_back({add(ad, ret), mask}); n       ad = mul(ad, A); n       res[pos][1].push_back({add(ad, ret), mask}); n       break; n     } n     vector<int> nw; n     for (int j = 1; j < vec.size(); j += 2) { n       if ((mask & pw[cnt])) { n         nw.push_back(vec[j - 1]); n         int ad = mul(vec[j], pow(A, (int)vec.size() + 1)); n         ret = add(ret, ad); n         place[vec[j]] = vec.size() + 1; n       } else { n         nw.push_back(vec[j]); n         int ad = mul(vec[j - 1], pow(A, (int)vec.size() + 1)); n         ret = add(ret, ad); n         place[vec[j - 1]] = vec.size() + 1; n       } n       cnt++; n     } n     vec.clear(); n     for (auto v : nw) vec.push_back(v); n   } n } n map<int, int> mp; n bool fnd(int i, int j) { n   mp.clear(); n   int pos = 1; n   for (auto v : res[0][i]) { n     mp[v.first] = pos; n     pos++; n   } n   int pos1 = 0, pos2 = 1; n   for (auto v : res[1][j]) { n     int rem = sub(h, v.first); n     pos1 = mp[rem]; n     if (pos1 > 0) break; n     pos2++; n   } n   if (pos1 == 0) return false; n   int mask1 = res[0][i][pos1 - 1].second; n   int mask2 = res[1][j][pos2 - 1].second; n   solve(mask1, 0); n   solve(mask2, 1); n   if (i == 0) { n     for (int id = 1; id <= pw[k - 1]; id++) n       if (place[id] == 2) place[id] = 1; n   } else { n     for (int id = pw[k - 1] + 1; id <= pw[k]; id++) n       if (place[id] == 2) place[id] = 1; n   } n   for (int id = 1; id <= pw[k]; id++) printf( %d  , place[id]); n   printf(  n ); n   return 1; n } n int main() { n   pw[0] = 1; n   for (int i = 1; i <= 17; i++) pw[i] = mul(pw[i - 1], 2); n   scanf( %d , &k); n   scanf( %d , &A); n   scanf( %d , &h); n   for (int mask = 0; mask < pw[pw[k - 1]]; mask++) { n     solve(mask, 0); n     solve(mask, 1); n   } n   if (fnd(1, 0)) return 0; n   if (fnd(0, 1)) return 0; n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int typetest; n inline void fastIOfileinput() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   typetest = 1; n } n const int N = 1e2 + 2; n long long n; n void Enter() { cin >> n; } n long long cnt() { n   return (long long)((long long)sqrt((long double)(24 * n + 1)) - 7) / 6; n } n void solve() { n   long long ans = 0, x; n   while (n > 1) { n     ++ans; n     x = cnt(); n     n -= (x + 1) * (3 * x + 4) / 2; n   } n   cout << ans <<   n ; n } n int main() { n   fastIOfileinput(); n   if (typetest) { n     int t; n     cin >> t; n     while (t--) { n       Enter(); n       solve(); n     } n   } else { n     Enter(); n     solve(); n   } n } n


#include <bits/stdc++.h> n int main() { n   float a, b, x, y, v, g, k = 10000.000; n   int i, n; n   scanf( %f %f %d , &a, &b, &n); n   for (i = 0; i < n; i++) { n     scanf( %f %f %f , &x, &y, &v); n     g = (x - a) * (x - a) + (y - b) * (y - b); n     g = sqrt(g); n     g = g / v; n     if (g < k) { n       k = g; n     } n   } n   printf( %f n , k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline string toString(T a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n template <typename T> n inline long long toLong(T a) { n   long long res; n   istringstream os(a); n   os >> res; n   return res; n } n template <typename T> n inline int toInt(T a) { n   int res; n   istringstream os(a); n   os >> res; n   return res; n } n template <typename T> n inline double toDouble(T a) { n   double res; n   istringstream os(a); n   os >> res; n   return res; n } n template <typename T> n inline T SQ(T a) { n   return a * a; n } n template <typename T> n inline T GCD(T a, T b) { n   if (b == 0) n     return a; n   else n     return GCD(b, a % b); n } n template <typename T> n inline T LCM(T a, T b) { n   long long res = a * b; n   res /= GCD(a, b); n   return res; n } n template <typename T> n inline unsigned long long BIGMOD(T a, T b, T m) { n   if (b == 0) n     return 1; n   else if (b % 2 == 0) n     return SQ(BIGMOD(a, b / 2, m)) % m; n   else n     return (a % m * BIGMOD(a, b - 1, m)) % m; n } n template <typename T> n inline vector<string> PARSE(T str) { n   vector<string> res; n   string s; n   istringstream os(str); n   while (os >> s) res.push_back(s); n   return res; n } n template <typename T> n inline unsigned long long DIST(T A, T B) { n   unsigned long long res = n       (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); n   return res; n } n template <typename T> n inline long long CROSS(T A, T B, T C) { n   return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y); n } n template <typename T> n inline double cosAngle(T a, T b, T c) { n   double res = a * a + b * b - c * c; n   res = res / (2 * a * b); n   res = acos(res); n   return res; n } n template <typename T> n inline T POWER(T base, int po) { n   T res = 1; n   if (base == 0) return 0; n   for (int i = (0); i < (po); i++) res *= base; n   return res; n } n template <typename T> n inline bool IS_ON(T mask, T pos) { n   return mask & (1 << pos); n } n template <typename T> n inline int OFF(T mask, T pos) { n   return mask ^ (1 << pos); n } n template <typename T> n inline int ON(T mask, T pos) { n   return mask | (1 << pos); n } n template <typename T> n inline bool INSIDE_GRID(int R, int C, int ro, int clm) { n   if (R >= 0 && C >= 0 && R < ro && C < clm) return 1; n   return 0; n } n template <typename T> n inline void PRINT_GRID(T GRID, int ro, int clm) { n   cout <<  GRID  n        <<  :   << GRID << endl; n   for (int i = (0); i < (ro); i++) { n     for (int j = (0); j < (clm); j++) cout << GRID[i][j] <<    ; n     puts(  ); n   } n } n map<string, int> mp; n pair<long long, long long> dp[800005]; n vector<int> g[800005]; n vector<int> gr[800005]; n int stk[800008]; n string str, str2; n vector<string> inp; n pair<long long, long long> Tolower(string &str) { n   int len = str.size(), r = 0; n   for (int i = (0); i < (len); i++) { n     str[i] = tolower(str[i]); n     if (str[i] ==  r ) r++; n   } n   return make_pair((long long)r, (long long)len); n } n int vis[800005], top = 0, rng; n void trop(int u) { n   vis[u] = 1; n   for (int i = (g[u].size() - 1); i >= (0); i--) n     if (!vis[g[u][i]]) trop(g[u][i]); n   stk[++top] = u; n } n void manipul(int u, int papa) { n   vis[u] = 3; n   dp[u] = min(dp[u], dp[papa]); n   for (int i = (gr[u].size() - 1); i >= (0); i--) n     if (vis[gr[u][i]] != 3) manipul(gr[u][i], papa); n } n pair<long long, long long> DP(int pos) { n   if (vis[pos] != 2) { n     vis[pos] = 2; n     for (int i = (g[pos].size() - 1); i >= (0); i--) n       dp[pos] = min(dp[pos], DP(g[pos][i])); n   } n   return dp[pos]; n } n int main() { n   int n, m; n   int cnt = 1; n   int s, d, len, r; n   for (int i = (0); i < (800000); i++) n     dp[i] = make_pair(100000000000000LL, 100000000000000LL); n   scanf( %d , &n); n   pair<long long, long long> tmp; n   for (int i = (0); i < (n); i++) { n     cin >> str; n     tmp = Tolower(str); n     inp.push_back(str); n     if (mp.find(str) == mp.end()) mp[str] = cnt++; n     s = mp[str]; n     dp[s] = min(dp[s], tmp); n   } n   rng = cnt; n   scanf( %d , &m); n   for (int i = (0); i < (m); i++) { n     cin >> str >> str2; n     tmp = Tolower(str); n     tmp = Tolower(str2); n     if (mp.find(str) == mp.end()) mp[str] = cnt++; n     if (mp.find(str2) == mp.end()) mp[str2] = cnt++; n     s = mp[str]; n     d = mp[str2]; n     dp[d] = min(dp[d], tmp); n     dp[s] = min(dp[s], dp[d]); n     g[s].push_back(d); n     gr[d].push_back(s); n   } n   pair<long long, long long> res; n   res = make_pair(0, 0); n   for (int i = (0); i < (n); i++) { n     s = mp[inp[i]]; n     if (!vis[s]) trop(s); n   } n   for (int i = (top + 1 - 1); i >= (0); i--) { n     DP(stk[i]); n   } n   for (int i = (top + 1 - 1); i >= (0); i--) { n     if (vis[stk[i]] != 3) manipul(stk[i], stk[i]); n   } n   for (int i = (0); i < (n); i++) { n     s = mp[inp[i]]; n     res.first += dp[s].first; n     res.second += dp[s].second; n   } n   cout << res.first <<     << res.second << endl; n   return 0; n } n