#include <bits/stdc++.h> n using namespace std; n const int N = 2010, mod = 998244353; n int sm[2][N], n, f[N][N], a[N][N], ans; n void add(int x, int y, int z) { n   for (; x <= n; x += x & -x) sm[z][x] += y; n } n int query(int x, int z) { n   int ret = 0; n   for (; x; x -= x & -x) ret += sm[z][x]; n   return ret; n } n void Init() { n   f[0][0] = 1; n   for (int i = 1; i <= n; ++i) { n     f[i][0] = (long long)f[i - 1][0] * i % mod; n     for (int j = 1; j <= i; ++j) n       f[i][j] = (f[i][j - 1] - f[i - 1][j - 1] + mod) % mod; n   } n } n int p[N], q[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   Init(); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) cin >> a[i][j]; n   for (int i = 1; i <= n; ++i) { n     int les = n; n     ans = (long long)ans * f[n][n] % mod; n     memset(sm, 0, sizeof sm); n     memset(p, 0, sizeof p); n     memset(q, 0, sizeof q); n     for (int j = 1; j <= n; ++j) { n       int u = query(a[i][j] - 1, 0); n       p[a[i - 1][j]] = 1; n       if (q[a[i - 1][j]]) n         ++les; n       else if (a[i - 1][j]) n         add(a[i - 1][j], 1, 0), add(a[i - 1][j], 1, 1); n       int v = a[i][j] - 1 - query(a[i][j] - 1, 1); n       les -= 2; n       if (i == 1) n         ans = (ans + (long long)v * f[n - j][0]) % mod; n       else n         ans = (ans + (long long)u * f[n - j][les + 1] + n                (long long)v * f[n - j][les]) % n               mod; n       q[a[i][j]] = 1; n       if (p[a[i][j]]) n         add(a[i][j], -1, 0), ++les; n       else n         add(a[i][j], 1, 1); n     } n   } n   cout << ans <<   n ; 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     int n; n     cin >> n; n     string s; n     cin >> s; n     vector<int> p0; n     vector<int> p1; n     vector<int> ans(n); n     for (int i = 0; i < n; i++) { n       int newpos = p0.size() + p1.size(); n       if (s[i] ==  0 ) { n         if (p1.empty()) { n           p0.push_back(newpos); n         } else { n           newpos = p1.back(); n           p0.push_back(newpos); n           p1.pop_back(); n         } n       } else { n         if (p0.empty()) { n           p1.push_back(newpos); n         } else { n           newpos = p0.back(); n           p1.push_back(newpos); n           p0.pop_back(); n         } n       } n       ans[i] = newpos + 1; n     } n     cout << p0.size() + p1.size() << endl; n     for (int i = 0; i < n; i++) { n       cout << ans[i] <<    ; n     } 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(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     string s; n     cin >> s; n     reverse(s.begin(), s.end()); n     if (s.substr(0, 2) ==  op ) { n       cout <<  FILIPINO n ; n     } else if (s.substr(0, 4) ==  used  || s.substr(0, 4) ==  usam ) { n       cout <<  JAPANESE n ; n     } else if (s.substr(0, 5) ==  adinm ) { n       cout <<  KOREAN n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char field[1000][1000]; n int deltas[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n int n, m; n int si, sj, ti, tj; n bool possible; n void go(int i, int j, char num) { n   for (auto d : deltas) { n     int ii = i; n     int jj = j; n     while (true) { n       ii += d[0]; n       jj += d[1]; n       if (ii < 0 || jj < 0 || ii >= n || jj >= m) break; n       if (field[ii][jj] ==  T ) possible = true; n       if (field[ii][jj] !=  . ) break; n       field[ii][jj] = num; n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     string line; n     cin >> line; n     for (int j = 0; j < m; j++) { n       field[i][j] = line[j]; n       if (line[j] ==  T ) { n         ti = i; n         tj = j; n       } n       if (line[j] ==  S ) { n         si = i; n         sj = j; n       } n     } n   } n   go(si, sj,  0 ); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (field[i][j] ==  0 ) { n         go(i, j,  1 ); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (field[i][j] ==  1 ) { n         go(i, j,  2 ); n       } n     } n   } n   if (possible) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Df[300005], GG, RR, fw[300005 * 2], U, lt[300005], val[300005], rt[300005], n     color[300005], Dep[300005], st[300005], rev[300005], cnt, fa[300005][20], n     Fa[300005], cl, dfn[300005], in, col[300005], low[300005], n     tot = 1, node[300005], vst[300005 << 1], dep[300005]; n vector<int> son[300005], sonn[300005]; n struct edge { n   int to, nxt; n } e[300005 << 1]; n inline void add(int x, int y) { n   e[++tot].to = y; n   e[tot].nxt = node[x]; n   node[x] = tot; n } n void dfs(int now) { n   dfn[now] = low[now] = ++in; n   for (int i = node[now]; i; i = e[i].nxt) { n     if (vst[i] | vst[i ^ 1]) continue; n     vst[i] = 1; n     if (!dfn[e[i].to]) n       dep[e[i].to] = dep[now] + 1, Fa[e[i].to] = now, dfs(e[i].to), n       low[now] = min(low[now], low[e[i].to]); n     else n       low[now] = min(low[now], dfn[e[i].to]); n   } n } n void MEM(int now) { n   dfn[now] = 0; n   Fa[now] = dep[now] = 0; n   for (int i = node[now]; i; i = e[i].nxt) { n     if (dfn[e[i].to]) MEM(e[i].to); n   } n   node[now] = 0; n } n void Dfs(int now) { n   col[now] = cl; n   for (int T, i = 0; i < son[now].size(); ++i) n     if (!col[T = son[now][i]]) col[T] = cl, Dfs(T); n } n void Dfs_(int now) { n   color[now] = 0; n   for (int T, i = 0; i < sonn[now].size(); ++i) n     if (!~color[T = sonn[now][i]]) Dfs_(T); n } n void dfs_v(int now, int fat) { n   lt[now] = cnt; n   Df[now] = ++in; n   vst[now] = 1; n   for (int i = 1; i <= 18; ++i) fa[now][i] = fa[fa[now][i - 1]][i - 1]; n   for (int T, i = 0; i < son[now].size(); ++i) n     if ((T = son[now][i]) != fat) n       fa[T][0] = now, Dep[T] = Dep[now] + 1, dfs_v(T, now); n } n inline int lca(int x, int y) { n   int i = 18; n   if (Dep[x] < Dep[y]) swap(x, y); n   for (; ~i; --i) n     if (Dep[fa[x][i]] >= Dep[y]) x = fa[x][i]; n   if (x == y) return x; n   for (i = 18; ~i; --i) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return fa[x][0]; n } n inline bool cmp(int x, int y) { return Df[x] < Df[y]; } n int n, m, las, fl, lc, hd, q, v, i, X[300005 * 3], V, AL, E, Y[300005 * 3], x, n     y, j, Ex[300005], Ey[300005]; n int main() { n   cin >> n >> m >> q; n   for (i = 1; i <= m; ++i) cin >> x >> y, add(y, x), add(X[i] = x, Y[i] = y); n   for (i = 1; i <= n; ++i) n     if (!dfn[i]) dfs(i); n   for (i = 1; i <= m; ++i) n     if (dep[X[i]] > dep[Y[i]]) swap(X[i], Y[i]); n   for (i = 1; i <= m; ++i) { n     if (Fa[Y[i]] == X[i] && low[Y[i]] > dfn[X[i]]) continue; n     vst[i] = -1; n     son[X[i]].push_back(Y[i]); n     son[Y[i]].push_back(X[i]); n   } n   for (i = 1; i <= n; ++i) n     if (!col[i]) ++cl, Dfs(i); n   for (i = 1; i <= n; ++i) son[i].clear(); n   for (i = 1; i <= m; ++i) n     if (col[X[i]] != col[Y[i]]) n       sonn[col[X[i]]].push_back(col[Y[i]]), n           sonn[col[Y[i]]].push_back(col[X[i]]); n   for (i = 1; i <= cl; ++i) n     if (sonn[i].size()) n       for (sort(sonn[i].begin(), sonn[i].end()), son[i].push_back(sonn[i][0]), n            j = 1; n            j < sonn[i].size(); ++j) n         if (sonn[i][j] != sonn[i][j - 1]) son[i].push_back(sonn[i][j]); n   memset(Fa, 0, sizeof(Fa)); n   memset(vst, 0, sizeof(vst)); n   memset(dep, 0, sizeof(dep)); n   memset(node, 0, sizeof(node)); n   memset(dfn, 0, sizeof(dfn)); n   in = 0; n   for (i = 1; i <= cl; ++i) n     if (!vst[i]) ++cnt, Dep[i] = 1, rt[cnt] = i, dfs_v(i, 0); n   while (q--) { n     cin >> V >> E; n     ++GG; n     for (i = 1; i <= V; ++i) n       cin >> X[i], X[i] = (X[i] + RR - 1) % n + 1, Y[i] = col[X[i]]; n     for (i = 1; i <= E; ++i) n       cin >> X[V + i * 2 - 1] >> X[V + i * 2], n           X[V + i * 2 - 1] = (X[V + i * 2 - 1] + RR - 1) % n + 1, n                         X[V + i * 2] = (X[V + i * 2] + RR - 1) % n + 1, n                         Ex[i] = col[X[V + i * 2 - 1]], n                         Ey[i] = col[X[V + i * 2]]; n     AL = V + E + E + 1; n     for (i = 1; i < AL; ++i) X[i] = col[X[i]]; n     tot = 1; n     for (i = V + 1; i < AL; i += 2) add(X[i], X[i + 1]), add(X[i + 1], X[i]); n     sort(X + 1, X + AL, cmp); n     X[AL] = X[0] = 300005 - 1; n     U = 0; n     for (las = 1, i = 2; i <= AL; ++i) { n       if (lt[X[i]] != lt[X[i - 1]]) { n         st[hd = 1] = rt[lt[X[las]]]; n         fw[++U] = st[1]; n         for (j = las; j < i; ++j) { n           if (X[j] == X[j - 1]) continue; n           lc = lca(st[hd], X[j]); n           if (st[hd] != lc) { n             while (Dep[lc] < Dep[st[hd - 1]]) { n               add(st[hd - 1], st[hd]); n               add(st[hd], st[hd - 1]); n               Ex[++E] = st[hd - 1], Ey[E] = st[hd]; n               --hd; n             } n             add(lc, st[hd]); n             add(st[hd], lc); n             Ex[++E] = lc, Ey[E] = st[hd]; n             --hd; n             if (lc != st[hd]) st[++hd] = lc, fw[++U] = lc; n           } n           if (X[j] != st[hd]) st[++hd] = X[j], fw[++U] = X[j]; n         } n         while (hd > 1) n           add(st[hd - 1], st[hd]), add(st[hd], st[hd - 1]), n               Ex[++E] = st[hd - 1], Ey[E] = st[hd], --hd; n         las = i; n       } n     } n     for (i = 1; i <= U; ++i) dfn[fw[i]] = 0; n     for (i = 2; i <= tot; ++i) vst[i] = 0; n     dfs(Y[1]); n     for (fl = 0, i = 2; i <= V; ++i) n       if (!dfn[Y[i]]) fl = 1; n     if (fl) { n       cout <<  NO n ; n       MEM(Y[1]); n       for (i = 1; i <= U; ++i) node[fw[i]] = 0; n       continue; n     } n     for (i = 1; i <= U; ++i) sonn[fw[i]].clear(); n     for (i = 1; i <= E; ++i) n       if (dep[Ex[i]] > dep[Ey[i]]) swap(Ex[i], Ey[i]); n     for (i = 1; i <= E; ++i) { n       if (Fa[Ey[i]] == Ex[i] && low[Ey[i]] > dfn[Ex[i]]) continue; n       sonn[Ex[i]].push_back(Ey[i]); n       sonn[Ey[i]].push_back(Ex[i]); n     } n     MEM(Y[1]); n     for (i = 1; i <= U; ++i) node[fw[i]] = 0, color[fw[i]] = -1; n     Dfs_(Y[1]); n     for (fl = 0, i = 2; i <= V; ++i) n       if (!~color[Y[i]]) fl = 1; n     if (fl) n       cout <<  NO n ; n     else n       cout <<  YES n , (RR += GG) %= n; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int powe(long long int n, long long int m) { n   if (m == 0) return 1; n   long long int t = powe(n, m / 2); n   if (m % 2 == 0) return (t * t); n   return (((t * t)) * n); n } n long long int mpowe(long long int n, long long int m) { n   if (m == 0) return 1; n   long long int t = powe(n, m / 2); n   t %= 1000000007; n   if (m % 2 == 0) return (t * t) % 1000000007; n   return (((t * t) % 1000000007) * n) % 1000000007; n } n long long int logtwo(long long int n) { n   if (n == 1) return 0; n   return logtwo(n / 2) + 1; n } n long long int A[200000 + 1]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long int n, d, i, sum = 0, dt; n     cin >> n >> d; n     long long int a[n]; n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     ; n     long long int c; n     for (i = 1; i < n; i++) { n       c = i; n       dt = d / c; n       if (a[i] < dt) { n         sum += a[i]; n         d -= (a[i] * c); n       } else { n         sum += dt; n         break; n       } n     } n     sum += a[0]; n     cout << sum <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const double PI = 3.1415926535897932; n const int dir[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1}; n const int maxn = 2e5 + 5; n const int MOD = 998244353; n int n, m; n long long A[maxn], invA[maxn]; n int w[maxn]; n bool vis[maxn]; n bool del[maxn]; n int B[maxn]; n int pos[maxn]; n int pre[maxn], nex[maxn]; n bool judge(int x) { n   if (x == 0 || x == n + 1) return 0; n   if (vis[w[x]]) return 0; n   return 1; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= n; i++) { n       del[i] = vis[i] = false; n       pre[i] = i - 1; n       nex[i] = i + 1; n     } n     for (int i = 1; i <= n; i++) scanf( %d , &w[i]), pos[w[i]] = i; n     for (int i = 1; i <= m; i++) { n       scanf( %d , &B[i]); n       vis[B[i]] = true; n     } n     long long ans = 1; n     for (int i = 1; i <= m; i++) { n       int x = pos[B[i]]; n       int cnt = 0; n       cnt += judge(pre[x]); n       cnt += judge(nex[x]); n       ans = ans * cnt % MOD; n       vis[w[x]] = false; n       nex[pre[x]] = nex[x]; n       pre[nex[x]] = pre[x]; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, n, m, i, j, k, sum; n   long long int a[100001]; n   cin >> n >> k; n   bool yo = false; n   for (i = 1; i <= k; i++) cin >> a[i]; n   sum = LONG_LONG_MAX; n   for (i = 1; i <= k; i++) { n     if (n % a[i] < sum) { n       sum = n % a[i]; n       j = i; n       m = n / a[i]; n     } n   } n   cout << j <<     << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T sqr(T x) { n   return x * x; n } n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n template <class T> n inline T bigmod(T p, T e, T M) { n   if (e == 0) return 1; n   if (e % 2 == 0) { n     long long int t = bigmod(p, e / 2, M); n     return (T)((t * t) % M); n   } n   return (T)((long long int)bigmod(p, e - 1, M) * (long long int)p) % M; n } n template <class T> n inline T bigexp(T p, T e) { n   if (e == 0) return 1; n   if (e % 2 == 0) { n     long long int t = bigexp(p, e / 2); n     return (T)((t * t)); n   } n   return (T)((long long int)bigexp(p, e - 1) * (long long int)p); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n int dx4[] = {1, 0, -1, 0}; n int dy4[] = {0, 1, 0, -1}; n int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}; n int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1}; n int nx8[] = {1, 1, -1, -1, 2, 2, -2, -2}; n int ny8[] = {2, -2, 2, -2, 1, -1, 1, -1}; n int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int n, q, m, a[200005], b[105], c[105], d[200005], sz, q1[200005][3], t, r[105]; n int main() { n   scanf( %d , &n); n   scanf( %d %d , &q, &m); n   for (__typeof(n) i = (1); i <= (n); i++) scanf( %d , &a[i]); n   for (__typeof(q) i = (1); i <= (q); i++) n     scanf( %d %d %d , &q1[i][0], &q1[i][1], &q1[i][2]); n   for (__typeof(m) i = (1); i <= (m); i++) { n     scanf( %d , &t); n     d[t] = 1; n     b[i] = t; n   } n   for (__typeof(n) i = (1); i <= (n); i++) n     if (d[i] > 0) { n       c[++sz] = i; n       r[sz] = c[sz]; n     } n   for (__typeof(1) i = (q); i >= (1); i--) { n     if (q1[i][0] == 1) { n       for (int j = 1; j <= sz; j++) { n         if (r[j] >= q1[i][1] && r[j] <= q1[i][2]) { n           r[j]--; n           if (r[j] < q1[i][1]) r[j] = q1[i][2]; n         } n       } n     } else { n       int l1, r1; n       for (int j = 1; j <= sz; j++) { n         if (r[j] >= q1[i][1] && r[j] <= q1[i][2]) { n           int mid = (q1[i][1] + q1[i][2]) / 2; n           if (r[j] <= mid) { n             l1 = r[j] - q1[i][1]; n             r[j] = q1[i][2] - l1; n           } else { n             l1 = q1[i][2] - r[j]; n             r[j] = q1[i][1] + l1; n           } n         } n       } n     } n   } n   for (__typeof(m) i = (1); i <= (m); i++) n     for (__typeof(sz) j = (1); j <= (sz); j++) n       if (b[i] == c[j]) { n         cout << a[r[j]] <<    ; n         break; n       } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int big = 1e7 + 5; n long long int arr[big]; n void minDivsieve() { n   for (long long int i = 0; i <= big; ++i) arr[i] = i; n   for (long long int i = 2; i <= big; ++i) { n     if (arr[i] != i) continue; n     for (long long int j = i; j <= big; j += i) arr[j] = min(arr[j], i); n   } n } n long long int fac[1000001]; n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int modInverse(long long int n, int p) { return power(n, p - 2, p); } n long long int nCrModPFermat(unsigned long long n, long long int r, n                             long long int p) { n   if (r == 0) return 1; n   return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % n          p; n } n int dp[2][1048577]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int test = 1; n   while (test--) { n     int n; n     cin >> n; n     long long int arr[n]; n     for (int i = 0; i < n; ++i) cin >> arr[i]; n     long long int res = 0; n     long long int currxor = 0; n     dp[1][0] = 1; n     for (int i = 0; i < n; ++i) { n       currxor = currxor ^ arr[i]; n       res += dp[i % 2][currxor]; n       dp[i % 2][currxor] += 1; n     } n     cout << res; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, val = 0, q[1000010]; n char d[1000010]; n void build(int now, int x, int y) { n   if (x == y) return; n   int mid = (x + y) / 2; n   build(2 * now, x, mid); n   build(2 * now, mid + 1, y); n } n int main() { n   build(1, 1, 1000); n   cin >> x >> y; n   while (1) { n     if (x == y && x > 1) { n       cout <<  Impossible ; n       return 0; n     } n     if (x == y && x == 1) { n       for (int i = 1; i <= val; i++) { n         cout << q[i] << d[i]; n       } n       return 0; n     } n     if (x > y) { n       long long k = (x - 1) / y; n       x -= k * y; n       q[++val] = k; n       d[val] =  A ; n       continue; n     } n     if (x < y) { n       long long k = (y - 1) / x; n       y -= k * x; n       q[++val] = k; n       d[val] =  B ; n       continue; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e7 + 1; n int32_t main() { n   long long n; n   cin >> n; n   if (n % 2) n     cout << 1; n   else n     cout << 2; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double EPS = 1e-6; n const int INF = 0x3f3f3f3f; n inline int readint() { n   int sum = 0; n   char c = getchar(); n   bool f = 0; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) return -sum; n   return sum; n } n inline long long readLL() { n   long long sum = 0; n   char c = getchar(); n   bool f = 0; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) return -sum; n   return sum; n } n const int maxn = 250015; n long long inv[maxn], p[maxn]; n void CNumberInit(int MAXNUM, long long mod) { n   inv[0] = inv[1] = 1; n   p[0] = p[1] = 1; n   for (int i = 2; i <= MAXNUM; i++) { n     inv[i] = (mod - mod / i) * inv[mod % i] % mod; n     p[i] = p[i - 1] * i % mod; n   } n   for (int i = 2; i <= MAXNUM; i++) inv[i] = inv[i - 1] * inv[i] % mod; n } n long long qmod(long long a, long long b, long long c) { n   long long ret = 1; n   for (; b; b >>= 1) { n     if (b & 1) ret = ret * a % c; n     a = a * a % c; n   } n   return ret; n } n vector<int> megumi; n long long mod; n int k; n long long dp[20][250005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n >> k >> mod; n   CNumberInit(n + 5, mod); n   if (n % 2) { n     cout << qmod(k, n, mod) << endl; n   } else { n     for (int i = 2; i <= n; i += 2) { n       if ((i & n) == i) megumi.push_back(i); n     } n     long long ans = 0; n     dp[0][n] = 1; n     for (int i = 1; i <= min(17, k); i++) { n       int tmpans = 0; n       for (int j = 0; j < megumi.size(); j++) { n         if (!dp[i - 1][megumi[j]]) continue; n         int Megumi = megumi[j] & (megumi[j] - 1); n         int step = Megumi ^ megumi[j]; n         for (int k = step; k <= Megumi; k += step) { n           if ((k & Megumi) == k) { n             dp[i][k] += dp[i - 1][megumi[j]] * p[megumi[j]] % mod * inv[k] % n                         mod * inv[megumi[j] - k] % mod; n             dp[i][k] %= mod; n           } n         } n         tmpans = (tmpans + dp[i - 1][megumi[j]]) % mod; n       } n       ans += tmpans * inv[k - i] % mod; n     } n     ans = ans * p[k] % mod; n     ans = qmod(k, n, mod) - ans; n     ans = (ans % mod + mod) % mod; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, d; n   cin >> n >> d; n   pair<long long, long long> s[n]; n   deque<long long> p; n   long long ind, cnt = 1; n   for (long long i = 0; i < n; i++) { n     cin >> s[i].first; n     s[i].second = i; n   } n   for (long long i = 0; i < n; i++) { n     long long tmp; n     cin >> tmp; n     p.push_back(tmp); n   } n   s[d - 1].first += p[0]; n   long long num = s[d - 1].first; n   p.pop_front(); n   for (long long i = 0; i < n; i++) { n     if (s[i].second != d - 1 && s[i].first + p[p.size() - 1] <= num) { n       s[i].first += p[p.size() - 1]; n       p.pop_back(); n     } else if (s[i].second != d - 1) { n       s[i].first += p[0]; n       p.pop_front(); n     } n   } n   sort(s, s + n); n   for (long long i = n - 1; s[i].second != d - 1; i--) { n     if (s[i].first != num) { n       cnt++; n     } n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long n; n   cin >> n; n   long long p[n + 1]; n   p[1] = -1; n   for (long long i = 2; i <= n; i++) cin >> p[i]; n   vector<long long> ans; n   long long x = n; n   while (p[x] != -1) { n     ans.push_back(p[x]); n     x = p[x]; n   } n   reverse(ans.begin(), ans.end()); n   for (long long i : ans) cout << i <<    ; n   cout << n << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     if (n & 1) { n       int d = (n - 1) / 2; n       printf( %d n , d * 2 + d * d - d); n       for (int i = 2; i < n; i += 2) { n         for (int j = 2; j < i; j += 2) { n           printf( 4 %d %d %d %d n , j, i, j + 1, i + 1); n           printf( 4 %d %d %d %d n , j, i, j + 1, i + 1); n         } n         printf( 3 1 %d %d n , i, i + 1); n         printf( 3 1 %d %d n , i, i + 1); n       } n     } else { n       int d = (n - 2) / 2; n       printf( %d n , 4 + (d - 1) * 3 + d * d - d); n       printf( 3 1 2 3 n ); n       printf( 3 1 2 4 n ); n       printf( 3 3 4 1 n ); n       printf( 3 3 4 2 n ); n       for (int i = 5; i < n; i += 2) { n         for (int j = 1; j < i - 2; j += 2) { n           printf( 4 %d %d %d %d n , j, i, j + 1, i + 1); n           printf( 4 %d %d %d %d n , j, i, j + 1, i + 1); n         } n         printf( 4 %d %d %d %d n , i - 2, i, i - 1, i + 1); n         printf( 3 %d %d %d n , i - 2, i, i + 1); n         printf( 3 %d %d %d n , i - 1, i, i + 1); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const long long int maxN = 5005; n long long int n; n vector<long long int> a, coords, keys; n long long int cnt[maxN]; n long long int getKey(long long int x) { n   if (x == 0) return 0; n   vector<long long int> ans; n   if (x < 0) ans.push_back(-1); n   x = abs(x); n   for (long long int i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       long long int cnt = 0; n       while (x % i == 0) { n         cnt++; n         x /= i; n       } n       if (cnt % 2 == 1) ans.push_back(i); n     } n   } n   if (x > 1) ans.push_back(x); n   long long int res = 1; n   for (auto i : ans) res *= i; n   return res; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   a.resize(n); n   keys.resize(n); n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   for (long long int i = 0; i < n; i++) { n     long long int key = getKey(a[i]); n     keys[i] = key; n     coords.push_back(key); n   } n   sort(coords.begin(), coords.end()); n   coords.erase(unique(coords.begin(), coords.end()), coords.end()); n   for (long long int i = 0; i < n; i++) n     keys[i] = n         (lower_bound(coords.begin(), coords.end(), keys[i]) - coords.begin()); n   vector<long long int> ans(n + 1, 0); n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < maxN; j++) cnt[j] = 0; n     long long int cntZ = 0, cntD = 0; n     for (long long int j = i; j < n; j++) { n       long long int key = keys[j]; n       if (coords[key] == 0) n         cntZ++; n       else { n         cnt[key]++; n         if (cnt[key] == 1) cntD++; n       } n       if (cntD > 0) n         ans[cntD]++; n       else n         ans[cntZ > 0]++; n     } n   } n   for (long long int i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int h[5005], pref[5005], dp[5005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &h[i]); n     pref[i] = pref[i - 1] + h[i]; n     for (int j = i - 1; j >= 0; j--) { n       if (pref[i] - pref[j] >= h[j]) { n         dp[i] = dp[j] + i - j - 1; n         h[i] = pref[i] - pref[j]; n         break; n       } n     } n   } n   printf( %d n , dp[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 12; n const int M = 305; n const int INF = 1e9; n int n; n int a[N][N]; n pair<int, int> pos[N * N]; n pair<int, int> dist[M][M]; n pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2}; n int dy[] = {1, 2, 2, 1, -1, -2, -2, -1}; n bool in(int first, int second) { n   return (0 <= first && first < n && 0 <= second && second < n); n } n int get(int first, int second, int p) { return first * n * 3 + second * 3 + p; } n pair<int, int> dp[N * N][3]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < int(n); i++) n     for (int j = 0; j < int(n); j++) { n       scanf( %d , &a[i][j]); n       --a[i][j]; n       pos[a[i][j]] = make_pair(i, j); n     } n   for (int i = 0; i < int(M); i++) n     for (int j = 0; j < int(M); j++) dist[i][j] = make_pair(INF, INF); n   for (int i = 0; i < int(M); i++) dist[i][i] = make_pair(0, 0); n   for (int first = 0; first < int(n); first++) n     for (int second = 0; second < int(n); second++) { n       for (int i = 0; i < int(8); i++) { n         int nx = first + dx[i]; n         int ny = second + dy[i]; n         if (in(nx, ny)) n           dist[get(first, second, 0)][get(nx, ny, 0)] = make_pair(1, 0); n       } n       for (int i = -n + 1; i <= n - 1; ++i) { n         int nx = first + i; n         int ny = second + i; n         if (in(nx, ny)) n           dist[get(first, second, 1)][get(nx, ny, 1)] = make_pair(1, 0); n         ny = second - i; n         if (in(nx, ny)) n           dist[get(first, second, 1)][get(nx, ny, 1)] = make_pair(1, 0); n       } n       for (int i = 0; i < int(n); i++) { n         int nx = first; n         int ny = i; n         dist[get(first, second, 2)][get(nx, ny, 2)] = make_pair(1, 0); n         nx = i; n         ny = second; n         dist[get(first, second, 2)][get(nx, ny, 2)] = make_pair(1, 0); n       } n       for (int i = 0; i < int(3); i++) n         for (int j = 0; j < int(3); j++) { n           if (i != j) { n             dist[get(first, second, i)][get(first, second, j)] = n                 make_pair(1, 1); n           } n         } n     } n   for (int k = 0; k < int(M); k++) n     for (int i = 0; i < int(M); i++) n       for (int j = 0; j < int(M); j++) n         dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); n   for (int i = 0; i < int(N * N); i++) n     for (int j = 0; j < int(3); j++) dp[i][j] = make_pair(INF, INF); n   dp[0][0] = dp[0][1] = dp[0][2] = make_pair(0, 0); n   for (int i = 0; i < int(n * n - 1); i++) n     for (int j = 0; j < int(3); j++) n       for (int k = 0; k < int(3); k++) n         dp[i + 1][k] = n             min(dp[i + 1][k], n                 dp[i][j] + dist[get(pos[i].first, pos[i].second, j)] n                                [get(pos[i + 1].first, pos[i + 1].second, k)]); n   pair<int, int> ans = make_pair(INF, INF); n   ans = min(ans, dp[n * n - 1][0]); n   ans = min(ans, dp[n * n - 1][1]); n   ans = min(ans, dp[n * n - 1][2]); n   printf( %d %d n , ans.first, ans.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n long long a[N], sum[N]; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int main() { n   int n; n   scanf( %d , &n); n   int i; n   long long ans = 0; n   for (i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n     ans = ans + a[i]; n   } n   sort(a + 1, a + n + 1); n   for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; n   for (i = 1; i <= n; i++) { n     int l = 1, r = n; n     int idx = -1; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (a[mid] <= a[i]) { n         l = mid + 1; n         idx = mid; n       } else n         r = mid - 1; n     } n     ans = ans + (idx)*1LL * a[i] - sum[idx] + sum[n] - sum[idx] - n           (n - idx) * 1LL * a[i]; n   } n   long long g = gcd(ans, 1LL * n); n   ans = ans / g; n   n = n / g; n   printf( %I64d %d n , ans, n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 3e5 + 5; n long long a[MAXN], kkorz[MAXN], bit[MAXN]; n long long n; n long long sum(long long idx) { n   long long res = 0; n   for (; idx > 0; idx -= (idx & (-idx))) res += bit[idx]; n   return res; n } n void upd(long long idx, long long x) { n   for (; idx <= n; idx += (idx & (-idx))) bit[idx] += x; n } n long long countInv() { n   vector<pair<long long, long long>> v; n   for (long long i = 0; i < n; i++) v.push_back(make_pair(kkorz[i], i)); n   sort(v.begin(), v.end()); n   for (long long i = 0; i < n; i++) kkorz[v[i].second] = i + 1; n   long long res = 0; n   for (long long i = 1; i <= n; i++) bit[i] = 0; n   for (long long i = n - 1; i >= 0; i--) { n     res += sum(kkorz[i] - 1); n     upd(kkorz[i], 1); n   } n   return res; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long long maxbit = 32 - __builtin_clz(*max_element(a, a + n)); n   for (long long i = 0; i < n; i++) kkorz[i] = a[i]; n   long long ans = countInv(), minx = 0, x = 0; n   for (long long b = 0; b <= maxbit; b++) { n     long long tmp = x | (1 << b); n     for (long long i = 0; i < n; i++) kkorz[i] = a[i] ^ tmp; n     long long i1 = countInv(); n     if (i1 < ans) { n       x |= (1 << b); n       ans = i1; n       minx = x; n     } n   } n   cout << ans <<     << minx <<   n ; 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 = 1e18; n const long long mod = 1e9 + 7; n vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1}; n vector<int> dy = {1, 1, 0, -1, -1, -1, 0, 1}; n int can[507][507]; n set<pair<int, int> > second[32][9]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   int a[35]; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int j = 250; j <= 250 + a[1] - 1; j++) can[250][j] = 1; n   second[1][0].insert(make_pair(250, 250 + a[1] - 1)); n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < 8; j++) { n       for (pair<int, int> k : second[i][j]) { n         for (int l = 1; l <= a[i + 1]; l++) { n           can[k.first + dx[(j + 1) % 8] * l][k.second + dy[(j + 1) % 8] * l] = n               1; n           can[k.first + dx[(j + 7) % 8] * l][k.second + dy[(j + 7) % 8] * l] = n               1; n         } n         second[i + 1][(j + 1) % 8].insert( n             make_pair(k.first + dx[(j + 1) % 8] * a[i + 1], n                       k.second + dy[(j + 1) % 8] * a[i + 1])); n         second[i + 1][(j + 7) % 8].insert( n             make_pair(k.first + dx[(j + 7) % 8] * a[i + 1], n                       k.second + dy[(j + 7) % 8] * a[i + 1])); n       } n     } n   } n   int ans = 0; n   for (int i = 0; i <= 500; i++) n     for (int j = 0; j <= 500; j++) ans += can[i][j]; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a[55], b[55], ans[5000] = {}, k = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   cin >> m; n   for (int i = 0; i < m; ++i) cin >> b[i]; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (b[j] % a[i] == 0) { n         ans[k] = b[j] / a[i]; n         k++; n       } n     } n   } n   sort(ans, ans + k); n   int c = 0; n   for (int i = k - 1; i >= 0; --i) n     if (ans[i] == ans[k - 1]) c++; n   cout << c; n   return 0; n } n


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


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t, n, m, i, j, k, sum, cnt, temp, ans; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     if (n % 3 == 0) { n       cout << n / 3 <<     << n / 3 <<   n ; n     } else if (n % 3 == 1) { n       cout << n / 3 + 1 <<     << n / 3 <<   n ; n     } else { n       cout << n / 3 <<     << n / 3 + 1 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[15], b[15]; n int cnt[15]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= m; i++) { n     int k; n     cin >> k; n     cnt[k] = 1; n   } n   vector<int> ans; n   for (int i = 1; i <= n; i++) { n     if (cnt[a[i]] == 1) { n       ans.push_back(a[i]); n     } n   } n   for (auto e : ans) cout << e <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char ch; n   int sig = 1; n   for (ch = getchar(); ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) sig = -1; n   int x = ch -  0 ; n   for (ch = getchar(); ch >=  0  && ch <=  9 ; ch = getchar()) n     x = x * 10 + ch -  0 ; n   return x * sig; n } n const int N = 1e6 + 5; n const long long Mo = 1e9 + 7; n struct P { n   long long x, y; n   P(long long _x = 0, long long _y = 0) { n     x = _x; n     y = _y; n   } n   friend P operator+(P a, P b) { return P(a.x + b.x, a.y + b.y); } n   friend P operator-(P a, P b) { return P(a.x - b.x, a.y - b.y); } n   friend long long operator^(P a, P b) { return a.x * b.y - a.y * b.x; } n } p[N]; n int n; n long long S, sq[N], sx[N], sy[N]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) p[i].x = read(), p[i].y = read(); n   for (int i = 1; i <= n; i++) p[i + n] = p[i]; n   for (int i = 1; i <= n; i++) S += p[i + 1] ^ p[i]; n   long long ans = 0, sum = 0; n   for (int i = 1; i <= n * 2; i++) { n     (sum += p[i] ^ (p[i - 1])) %= Mo; n     sq[i] = (sq[i - 1] + sum) % Mo; n     sx[i] = (sx[i - 1] + p[i].x) % Mo; n     sy[i] = (sy[i - 1] + p[i].y) % Mo; n   } n   long long ns = S % Mo; n   int pos = 3; n   sum = 0; n   for (int i = 2; i <= pos; i++) sum += p[i] ^ p[i - 1]; n   long long now = 0; n   for (int i = 1; i <= n; i++) { n     (now += p[i + 1] ^ p[i]) %= Mo; n     sum -= p[i + 1] ^ p[i]; n     if (pos < i + 2) pos++, sum += p[pos] ^ p[pos - 1]; n     while (pos <= i + n - 2) { n       long long ar = sum; n       ar -= p[i] ^ p[i + 1]; n       ar += p[i] ^ p[pos]; n       if (ar < S - ar) n         pos++, sum += p[pos] ^ p[pos - 1]; n       else n         break; n     } n     int ls = i + 2, rs = pos - 1; n     long long dx = (sx[rs] - sx[ls - 1]) % Mo, dy = (sy[rs] - sy[ls - 1]) % Mo; n     if (ls <= rs) { n       (ans += ns * (rs - ls + 1) % Mo) %= Mo; n       (ans -= (sq[rs] - sq[ls - 1]) % Mo * 2 % Mo) %= Mo; n       (ans += now * 2 % Mo * (rs - ls + 1) % Mo) %= Mo; n       (ans += (p[i] ^ p[i + 1]) % Mo * (rs - ls + 1) % Mo * 2 % Mo) %= Mo; n       (ans -= (p[i] ^ P(dx, dy)) % Mo * 2 % Mo) %= Mo; n     } n     ls = pos, rs = i + n - 2, dx = (sx[rs] - sx[ls - 1]) % Mo, n     dy = (sy[rs] - sy[ls - 1]) % Mo; n     if (ls <= rs) { n       (ans -= ns * (rs - ls + 1) % Mo) %= Mo; n       (ans += (sq[rs] - sq[ls - 1]) % Mo * 2 % Mo) %= Mo; n       (ans -= now * 2 % Mo * (rs - ls + 1) % Mo) %= Mo; n       (ans -= (p[i] ^ p[i + 1]) % Mo * (rs - ls + 1) % Mo * 2 % Mo) %= Mo; n       (ans += (p[i] ^ P(dx, dy)) % Mo * 2 % Mo) %= Mo; n     } n   } n   ans = ans * (Mo + 1) / 2 % Mo; n   printf( %lld n , (ans + Mo) % Mo); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str[8][10]; n   int n, k, i, j, l, mx, mn, mind; n   int p[] = {1, 2, 3, 4, 5, 6, 7, 8}; n   cin >> n >> k; n   for (i = 0; i < n; i++) cin >> str[i]; n   mx = 0; n   mn = 1000000000; n   mind = mn - mx; n   do { n     int arr[8] = {0}; n     mx = 0; n     mn = 1000000000; n     for (i = 0; i < n; i++) { n       for (j = 0; j < k; j++) { n         arr[i] *= 10; n         arr[i] += (int)(str[i][p[j] - 1] -  0 ); n       } n       if (arr[i] > mx) mx = arr[i]; n       if (arr[i] < mn) mn = arr[i]; n     } n     mind = min(mind, (mx - mn)); n   } while (std::next_permutation(p, p + k)); n   cout << mind << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int a; n   cin >> a; n   string s; n   cin >> s; n   map<int, int> Freq; n   for (int i = 0; i < s.size(); i++) { n     int sum = 0; n     bool zero = false; n     for (int j = i; j < s.size(); j++) { n       sum += (s[j] -  0 ); n       Freq[sum]++; n     } n   } n   long long ans = 0; n   if (a == 0) { n     for (int i = 0; i < s.size(); i++) { n       int sum = 0; n       for (int j = i; j < s.size(); j++) { n         sum += s[j] -  0 ; n         if (sum == 0) n           ans += (s.size() * (s.size() + 1)) / 2; n         else n           ans += Freq[0]; n       } n     } n     cout << ans; n     return 0; n   } n   for (auto &x : Freq) { n     if (x.first && a % x.first == 0) n       ans += (long long)Freq[a / x.first] * x.second; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n typedef struct { n   double x; n   double y; n   double r; n } circle; n typedef struct { n   double x; n   double y; n } pt; n int x[3]; n int y[3]; n int r[3]; n pt np(double x, double y) { n   pt q; n   q.x = x; n   q.y = y; n   return q; n } n double abs_d(double d1) { n   if (d1 > 0) return d1; n   return -d1; n } n bool flag2 = true; n pt p; n double dist(pt p1, pt p2) { n   return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); n } n void circumcenter(pt p1, pt p2, pt p3) { n   double K = abs_d(.5 * (p1.x * p2.y + p2.x * p3.y + p3.x * p1.y - p1.y * p2.x - n                          p2.y * p3.x - p3.y * p1.x)); n   if (K < .01) { n     flag2 = false; n     return; n   } n   double a = dist(p1, p2); n   double b = dist(p1, p3); n   double c = dist(p2, p3); n   double R = a * b * c / 4 / K; n   double d = sqrt(R * R - (a / 2) * (a / 2)); n   double nx = (p1.x + p2.x) / 2; n   double ny = (p1.y + p2.y) / 2; n   double mx = p1.y - p2.y; n   double my = p2.x - p1.x; n   double len = sqrt(mx * mx + my * my); n   mx /= len; n   my /= len; n   p.x = nx + d * mx; n   p.y = ny + d * my; n   if (abs_d(dist(p, p3) - R) > .001) { n     p.x = nx - d * mx; n     p.y = ny - d * my; n   } n } n bool flag = true; n pt pos1; n pt pos2; n void intersection(circle c1, circle c2) { n   pt p; n   p.x = -1e9; n   p.y = -1e9; n   double d = n       sqrt((c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y)); n   if (d > c1.r + c2.r) flag = false; n   if (abs_d(c1.r - c2.r) > d) flag = false; n   if (flag == false) return; n   double a = d; n   double b = c1.r; n   double c = c2.r; n   double s = (a + b + c) / 2; n   double K = sqrt(s * (s - a) * (s - b) * (s - c)); n   double h = 2 * K / a; n   double d_par = sqrt(b * b - h * h); n   if (b * b + d * d < c * c) d_par = -d_par; n   double nx = c1.x + (c2.x - c1.x) * d_par / d; n   double ny = c1.y + (c2.y - c1.y) * d_par / d; n   double oppx = c2.y - c1.y; n   double oppy = c1.x - c2.x; n   double len = sqrt(oppx * oppx + oppy * oppy); n   oppx /= len; n   oppy /= len; n   pos1.x = nx + h * oppx; n   pos1.y = ny + h * oppy; n   pos2.x = nx - h * oppx; n   pos2.y = ny - h * oppy; n } n int main() { n   for (int i = 0; i < 3; i++) { n     scanf( %d %d %d , &x[i], &y[i], &r[i]); n   } n   pt p1 = np(x[0], y[0]); n   pt p2 = np(x[1], y[1]); n   pt p3 = np(x[2], y[2]); n   if (r[0] == r[1] && r[1] == r[2]) { n     circumcenter(p1, p2, p3); n     if (flag2) { n       printf( %.5f %.5f , p.x, p.y); n     } n   } else { n     circle c1, c2, c3; n     if (r[0] != r[1]) { n       double x1 = (r[1] * p1.x + r[0] * p2.x) / (r[1] + r[0]); n       double y1 = (r[1] * p1.y + r[0] * p2.y) / (r[1] + r[0]); n       double x2 = (r[1] * p1.x - r[0] * p2.x) / (r[1] - r[0]); n       double y2 = (r[1] * p1.y - r[0] * p2.y) / (r[1] - r[0]); n       c1.x = (x1 + x2) / 2; n       c1.y = (y1 + y2) / 2; n       c1.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2; n     } n     if (r[0] != r[2]) { n       double x1 = (r[2] * p1.x + r[0] * p3.x) / (r[2] + r[0]); n       double y1 = (r[2] * p1.y + r[0] * p3.y) / (r[2] + r[0]); n       double x2 = (r[2] * p1.x - r[0] * p3.x) / (r[2] - r[0]); n       double y2 = (r[2] * p1.y - r[0] * p3.y) / (r[2] - r[0]); n       c2.x = (x1 + x2) / 2; n       c2.y = (y1 + y2) / 2; n       c2.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2; n     } n     if (r[1] != r[2]) { n       double x1 = (r[2] * p2.x + r[1] * p3.x) / (r[2] + r[1]); n       double y1 = (r[2] * p2.y + r[1] * p3.y) / (r[2] + r[1]); n       double x2 = (r[2] * p2.x - r[1] * p3.x) / (r[2] - r[1]); n       double y2 = (r[2] * p2.y - r[1] * p3.y) / (r[2] - r[1]); n       c3.x = (x1 + x2) / 2; n       c3.y = (y1 + y2) / 2; n       c3.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2; n     } n     if (r[0] == r[1]) { n       intersection(c2, c3); n     } else if (r[0] == r[2]) { n       intersection(c1, c3); n     } else { n       intersection(c1, c2); n     } n     if (flag) { n       if (dist(pos1, p1) > dist(pos2, p1)) { n         printf( %.5f %.5f , pos2.x, pos2.y); n       } else { n         printf( %.5f %.5f , pos1.x, pos1.y); n       } n     } n   } n   fflush(stdin); n   getchar(); n } n


#include <bits/stdc++.h> n using namespace std; n long long pf(int v) { return (long long)1 * v * v; } n struct sd { n   int a, b; n   bool operator<(const sd& w) const { return a < w.a; } n } x[100005]; n long long maxx(long long a, long long b) { return a > b ? a : b; } n long long minx(long long a, long long b) { return a < b ? a : b; } n int n, fr[2][100005], ba[2][100005]; n long long ans = 1e18; n bool jud(long long v) { n   int mid = sqrt(v); n   for (int i = 1; i <= n; i++) { n     if (x[i].a > 0) break; n     int l = 1, r = n, R = minx(mid + x[i].a, -x[i].a); n     while (l < r) { n       int mid = ((l + r) >> 1) + 1; n       if (x[mid].a <= R) n         l = mid; n       else n         r = mid - 1; n     } n     if (i == 1 && l == n) return 1; n     int ymin = minx(fr[0][i], ba[0][l]), ymax = maxx(fr[1][i], ba[1][l]); n     if (maxx(maxx(pf(ymax), pf(ymin)) + pf(x[i].a), pf(ymax - ymin)) <= v) n       return 1; n   } n   return 0; n } n void work() { n   sort(x + 1, x + 1 + n); n   fr[0][1] = 1e9; n   fr[1][1] = -1e9; n   ba[0][n] = 1e9; n   ba[1][n] = -1e9; n   for (int i = 2; i <= n; i++) n     fr[0][i] = minx(fr[0][i - 1], x[i - 1].b), n     fr[1][i] = maxx(fr[1][i - 1], x[i - 1].b); n   for (int i = n - 1; i >= 1; i--) n     ba[0][i] = minx(ba[0][i + 1], x[i + 1].b), n     ba[1][i] = maxx(ba[1][i + 1], x[i + 1].b); n   long long l = 0, r = 1e18; n   while (l < r) { n     long long mid = (l + r) / 2; n     if (jud(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   ans = minx(ans, l); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &x[i].a, &x[i].b); n   work(); n   for (int i = 1; i <= n; i++) x[i].a *= -1; n   work(); n   int ymax = -1e9, ymin = 1e9; n   for (int i = 1; i <= n; i++) n     ymax = maxx(ymax, x[i].b), ymin = minx(ymin, x[i].b); n   ans = minx(ans, pf(ymax - ymin)); n   cout << ans << endl; 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 x1, x2, y1, y2; n     long long int k; n     cin >> x1 >> y1 >> x2 >> y2; n     if (x1 == x2 || y1 == y2) n       k = abs(x1 - x2) + abs(y1 - y2); n     else n       k = 2 + abs(x1 - x2) + abs(y1 - y2); n     cout << k << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n string S[100001]; n int cnt[26]; n int from[26]; n int find(int ind, char C) { n   for (int i = 0; i < S[ind].length(); i++) n     if (S[ind][i] == C) return i; n   return -1; n } n void merge(int I, int J, char C) { n   int indI = find(I, C); n   int indJ = find(J, C); n   int lenI = S[I].length() - indI; n   int lenJ = S[J].length() - indJ; n   int L = min(lenI, lenJ); n   if (min(indI, indJ) > 0 || S[I].substr(indI, L) != S[J].substr(indJ, L)) { n     cout <<  NO  <<   n ; n     exit(0); n   } n   string s; n   if (indI > indJ) { n     s = S[I]; n     if (indJ + L < (int)S[J].length()) n       s += S[J].substr(indJ + L, (int)S[J].length() - (indJ + L)); n   } else { n     s = S[J]; n     if (indI + L < (int)S[I].length()) n       s += S[I].substr(indI + L, (int)S[I].length() - (indI + L)); n   } n   S[J] = s; n   S[I] =  ! ; n } n int main() { n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> S[i]; n     for (int j = 0; j < 26; j++) cnt[j] = 0; n     for (int j = 0; j < S[i].length(); j++) cnt[S[i][j] -  a ]++; n     for (int j = 0; j < 26; j++) n       if (cnt[j] > 1) { n         cout <<  NO  <<   n ; n         return 0; n       } n   } n   for (int i = 0; i < 26; i++) from[i] = -1; n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < S[i].length(); j++) { n       if (from[S[i][j] -  a ] == -1) n         from[S[i][j] -  a ] = i; n       else if (from[S[i][j] -  a ] != i) { n         merge(from[S[i][j] -  a ], i, S[i][j]); n         int temp = from[S[i][j] -  a ]; n         for (int k = 0; k < 26; k++) n           if (from[k] == temp) from[k] = i; n       } n     } n   } n   vector<string> v; n   for (int i = 0; i < N; i++) n     if (S[i] !=  ! ) v.push_back(S[i]); n   sort(v.begin(), v.end()); n   string ans =   ; n   for (auto s : v) ans += s; n   for (int i = 0; i < 26; i++) cnt[i] = 0; n   for (int i = 0; i < ans.length(); i++) cnt[ans[i] -  a ]++; n   for (int i = 0; i < 26; i++) n     if (cnt[i] > 1) { n       cout <<  NO  <<   n ; n       return 0; n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[400000], cur, path[500000], block[500000], x, y, n, m, q, k, s, t, n     seen[400010], ans[400010]; n vector<int> g[4000]; n map<pair<int, int>, vector<pair<int, int> > > M; n void color(int x) { n   if (block[x]) return; n   block[x] = 1; n   for (int i = 0; i < g[x].size(); i++) color(g[x][i]); n } n void dfs(int x, int dep) { n   if (vis[x]) { n     int now = dep - 1; n     while (path[now] != x && now > 0) color(path[now--]); n     color(path[now]); n   } n   if (seen[x]) return; n   path[dep] = x; n   vis[x] = seen[x] = 1; n   if (M.find(make_pair(cur, x)) != M.end()) { n     if (!block[x]) { n       vector<pair<int, int> > tmp = M[make_pair(cur, x)]; n       for (int i = 0; i < tmp.size(); i++) n         if (tmp[i].first <= dep) ans[tmp[i].second] = path[tmp[i].first]; n     } n   } n   for (int i = 0; i < g[x].size(); i++) dfs(g[x][i], dep + 1); n   vis[x] = 0; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n   } n   for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); n   for (int i = 1; i <= q; i++) { n     scanf( %d%d%d , &s, &t, &k); n     M[make_pair(s, t)].push_back(make_pair(k, i)); n   } n   memset(ans, 0xff, sizeof(ans)); n   for (int i = 1; i <= n; i++) { n     cur = i; n     memset(seen, 0, sizeof(seen)); n     memset(block, 0, sizeof(block)); n     dfs(i, 1); n   } n   for (int i = 1; i <= q; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> in[100005]; n pair<int, int> id[100005]; n int bit[100005]; n int lastNum[100005]; n int color[100005]; n void solve() { n   int n, m; n   scanf( %d %d  , &n, &m); n   long long sum = 0; n   for (int i = 1; i <= n; ++i) { n     int num; n     scanf( %d  , &num); n     sum += num; n     while (num--) { n       int x; n       scanf( %d  , &x); n       in[i].insert(x); n     } n     id[i] = {in[i].size(), i}; n   } n   int x = sum / n; n   int lim = (n - sum % n); n   int t = 0; n   for (int i = 1; i <= n; ++i) { n     if (i <= lim) { n       lastNum[i] = x; n     } else { n       lastNum[i] = x + 1; n     } n     t += lastNum[i]; n   } n   assert(t == sum); n   sort(id + 1, id + n + 1); n   int l = 1, r = n; n   vector<pair<pair<int, int>, int> > out; n   bool lin = false, rin = false; n   set<int> swapNums; n   while (l < r) { n     int lid = id[l].second; n     int rid = id[r].second; n     if (in[lid].size() == lastNum[l]) { n       if (lin) { n         for (int x : in[lid]) { n           assert(color[x] & 1); n           color[x] ^= 1; n           if (color[x] == 2) { n             assert(!swapNums.count(x)); n             swapNums.insert(x); n           } n         } n       } n       ++l; n       lin = false; n       continue; n     } n     if (in[rid].size() == lastNum[r]) { n       if (rin) { n         for (int x : in[rid]) { n           assert(color[x] & 2); n           if (color[x] == 2) { n             assert(swapNums.count(x)); n             swapNums.erase(x); n           } n           color[x] ^= 2; n         } n       } n       --r; n       rin = false; n       continue; n     } n     if (!lin) { n       for (int x : in[lid]) { n         color[x] |= 1; n         if (color[x] == 3) { n           assert(swapNums.count(x)); n           swapNums.erase(x); n         } n       } n       lin = true; n     } n     if (!rin) { n       for (int x : in[rid]) { n         color[x] |= 2; n         if (color[x] == 2) { n           assert(!swapNums.count(x)); n           swapNums.insert(x); n         } n       } n       rin = true; n     } n     assert(in[lid].size() < lastNum[l]); n     assert(in[rid].size() > lastNum[r]); n     while (in[lid].size() != lastNum[l] && in[rid].size() != lastNum[r]) { n       assert(swapNums.size() > 0); n       int g = *swapNums.begin(); n       swapNums.erase(g); n       in[lid].insert(g); n       in[rid].erase(g); n       out.push_back({{rid, lid}, g}); n       color[g] = 1; n     } n   } n   printf( %d n , out.size()); n   for (pair<pair<int, int>, int> tmp : out) { n     printf( %d %d %d n , tmp.first.first, tmp.first.second, tmp.second); n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   queue<int> left; n   queue<int> right; n   int n; n   cin >> n; n   char x; n   for (int i = 0; i < n; ++i) { n     cin >> x; n     if (x ==  D ) n       left.push(i); n     else n       right.push(i); n   } n   while (!left.empty() && !right.empty()) { n     if (left.front() < right.front()) n       left.push(left.front() + n); n     else n       right.push(right.front() + n); n     right.pop(); n     left.pop(); n   } n   if (left.empty()) n     cout <<  R ; n   else n     cout <<  D ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int a, b, cnt = 0, sum = 0, i, j, hand, flag = 0, c, n, m, k, t, res, n             maxy = 0, miny = 12345678910; n   cin >> n; n   string str; n   getline(cin, str); n   int len = str.size(); n   if (str[len - 1] ==  k ) { n     if (n == 5 || n == 6) n       printf( 53 n ); n     else n       printf( 52 ); n   } else if (str[len - 1] ==  h ) { n     if (n == 30) n       printf( 11 ); n     else if (n < 30) n       printf( 12 ); n     else n       printf( 7 ); n   } n } n


#include <bits/stdc++.h> n const long long INF = LLONG_MAX; n const long long NINF = LLONG_MIN; n const long long MAX = 1e6 + 5; n const long long MOD = 1e9 + 7; n using namespace std; n inline long long gcd(long long a, long long b) { n   long long t; n   if (a < b) { n     t = a; n     a = b; n     b = t; n   } n   while (b != 0) { n     t = a; n     a = b; n     b = t % b; n   } n   return a; n } n inline long long mpow(long long a, long long b) { n   long long ans = 1; n   while (b != 0) { n     if (b & 1) { n       ans = ans * a; n     } n     a = a * a; n     b >>= 1; n   } n   return ans; n } n inline long long mlog2(long long n) { n   long long c = 0; n   c += 64 - __builtin_clzll(n); n   if (__builtin_popcountll(n) == 1) { n     c--; n   } n   return c; n } n void troll() { n   long long qwe[100000]; n   for (long long i = 0; i < 1; i++) n     ; n } n long long ass[MAX]; n long long ssa[MAX]; n int main() { n   troll(); n   string str1, str2; n   cin >> str1 >> str2; n   long long count = 0; n   for (long long i = 0; i < str1.size();) { n     long long flag = 1; n     if (str1[i] == str2[0]) { n       long long j = 1; n       for (j = 1; j < str2.size(); j++) { n         if (str2[j] != str1[i + j]) { n           flag = 0; n           break; n         } n       } n       if (!flag) { n         flag = 1; n         i++; n       } else { n         i += j; n         count++; n       } n     } else { n       i++; n     } n   } n   troll(); n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int main() { n   long long t, w, b; n   cin >> t >> w >> b; n   long long cas = t / (w / gcd(w, b)) / b; n   long long exa = t - w / gcd(w, b) * b * cas; n   long long minimum = min(w, b); n   long long res = minimum * cas + min(minimum, exa + 1) - 1; n   long long divition = gcd(res, t); n   res /= divition; n   t /= divition; n   cout << res <<  /  << t << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn(5e5 + 5); n const long long inf(1e18); n struct Qry { n   int l, r, id; n }; n int n, q, dfn[maxn], idx, ed[maxn]; n long long mn[maxn << 2], tag[maxn << 2], ans[maxn]; n vector<pair<int, int> > edge[maxn]; n vector<Qry> qry[maxn]; n void Update(int x, int l, int r, int p, long long v) { n   if (l == r) n     mn[x] = v + tag[x]; n   else { n     int mid = (l + r) >> 1; n     p <= mid ? Update(x << 1, l, mid, p, v) n              : Update(x << 1 | 1, mid + 1, r, p, v); n     mn[x] = min(mn[x << 1], mn[x << 1 | 1]) + tag[x]; n   } n } n void Modify(int x, int l, int r, int ql, int qr, long long v) { n   if (ql <= l && qr >= r) n     tag[x] += v, mn[x] += v; n   else { n     int mid = (l + r) >> 1; n     if (ql <= mid) Modify(x << 1, l, mid, ql, qr, v); n     if (qr > mid) Modify(x << 1 | 1, mid + 1, r, ql, qr, v); n     mn[x] = min(mn[x << 1], mn[x << 1 | 1]) + tag[x]; n   } n } n long long Query(int x, int l, int r, int ql, int qr) { n   if (ql <= l && qr >= r) return mn[x]; n   int mid = (l + r) >> 1; n   long long ret = inf; n   if (ql <= mid) ret = Query(x << 1, l, mid, ql, qr); n   if (qr > mid) ret = min(ret, Query(x << 1 | 1, mid + 1, r, ql, qr)); n   return ret + tag[x]; n } n void Dfs1(int u, long long d) { n   dfn[u] = ++idx; n   if (!edge[u].size()) Update(1, 1, n, idx, d); n   for (auto v : edge[u]) Dfs1(v.first, d + v.second); n   ed[u] = idx; n } n void Dfs2(int u) { n   for (auto ques : qry[u]) ans[ques.id] = Query(1, 1, n, ques.l, ques.r); n   for (auto v : edge[u]) { n     Modify(1, 1, n, 1, n, v.second); n     Modify(1, 1, n, dfn[v.first], ed[v.first], -(v.second << 1)); n     Dfs2(v.first); n     Modify(1, 1, n, dfn[v.first], ed[v.first], v.second << 1); n     Modify(1, 1, n, 1, n, -v.second); n   } n } n int main() { n   memset(mn, 63, sizeof(mn)); n   int p, w, i, l, r; n   scanf( %d%d , &n, &q); n   for (i = 2; i <= n; ++i) { n     scanf( %d%d , &p, &w); n     edge[p].push_back(make_pair(i, w)); n   } n   for (i = 1; i <= q; ++i) { n     scanf( %d%d%d , &p, &l, &r); n     qry[p].push_back((Qry){l, r, i}); n   } n   Dfs1(1, 0), Dfs2(1); n   for (i = 1; i <= q; ++i) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const int M = 10000005; n const int mo = 1000000007; n int pri[M / 10], mn[M], id[M]; n vector<int> e[N]; n int n, T, Q, dfn[N], ed[N]; n int fa[N][20], dep[N]; n int qx[N], qy[N], qL[N], qv[N]; n int t[N * 2], vis[N], A[N], ans[N]; n vector<int> vec[M / 10]; n pair<int, int> q1[N], q2[N]; n int power(int x, int y) { n   int s = 1; n   for (; y; y /= 2, x = 1ll * x * x % mo) n     if (y & 1) s = 1ll * s * x % mo; n   return s; n } n void change(int x, int v) { n   for (; x <= 2 * n; x += x & (-x)) t[x] += v; n } n int ask(int x) { n   int ans = 0; n   for (; x; x -= x & (-x)) ans += t[x]; n   return ans; n } n void init() { n   for (int i = (int)(2); i <= (int)(M - 1); i++) { n     if (!mn[i]) mn[i] = i, pri[++*pri] = i; n     for (int j = 1; j <= *pri && i * pri[j] < M; ++j) { n       mn[i * pri[j]] = pri[j]; n       if (i % pri[j] == 0) break; n     } n   } n   for (int i = (int)(1); i <= (int)(*pri); i++) id[pri[i]] = i; n } n void insert(int x, int id) { n   for (; x != 1;) { n     int v = mn[x]; n     for (; x % v == 0; x /= v) n       ; n     vec[::id[v]].push_back(id); n   } n } n void dfs(int x) { n   dfn[x] = ++*dfn; n   for (auto i : e[x]) n     if (i != fa[x][0]) { n       fa[i][0] = x; n       dep[i] = dep[x] + 1; n       dfs(i); n     } n   ed[x] = ++*dfn; n } n int LCA(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   int tmp = dep[x] - dep[y]; n   for (int i = (int)(0); i <= (int)(18); i++) n     if (tmp & (1 << i)) x = fa[x][i]; n   for (int i = (int)(18); i >= (int)(0); i--) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return x == y ? x : fa[x][0]; n } n void solve(int id) { n   int top1 = 0, top2 = 0; n   for (auto i : vec[id]) n     if (i < 0) n       q2[++top2] = pair<int, int>(-i, qv[-i]); n     else n       q1[++top1] = pair<int, int>(i, A[i]); n   for (; top1 && top2;) { n     int tmp1 = 0, tmp2 = 0; n     for (int i = (int)(1); i <= (int)(top1); i++) { n       change(dfn[q1[i].first], -1); n       change(ed[q1[i].first], 1); n     } n     for (int i = (int)(1); i <= (int)(top2); i++) { n       int at = q2[i].first; n       int val1 = ask(ed[qL[at]] - 1) - ask(ed[qx[at]] - 1); n       int val2 = ask(ed[qL[at]]) - ask(ed[qy[at]] - 1); n       ans[at] = 1ll * ans[at] * power(pri[id], val1 + val2) % mo; n     } n     for (int i = (int)(1); i <= (int)(top1); i++) { n       change(dfn[q1[i].first], 1); n       change(ed[q1[i].first], -1); n     } n     for (int i = (int)(1); i <= (int)(top1); i++) q1[i].second /= pri[id]; n     for (int i = (int)(1); i <= (int)(top2); i++) q2[i].second /= pri[id]; n     for (int i = (int)(1); i <= (int)(top1); i++) n       if (q1[i].second % pri[id] == 0) q1[++tmp1] = q1[i]; n     for (int i = (int)(1); i <= (int)(top2); i++) n       if (q2[i].second % pri[id] == 0) q2[++tmp2] = q2[i]; n     top1 = tmp1; n     top2 = tmp2; n   } n } n int main() { n   init(); n   scanf( %d , &n); n   for (int i = (int)(1); i <= (int)(n - 1); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   dfs(1); n   for (int i = (int)(1); i <= (int)(18); i++) n     for (int j = (int)(1); j <= (int)(n); j++) n       fa[j][i] = fa[fa[j][i - 1]][i - 1]; n   for (int i = (int)(1); i <= (int)(n); i++) { n     scanf( %d , &A[i]); n     insert(A[i], i); n   } n   scanf( %d , &Q); n   for (int i = (int)(1); i <= (int)(Q); i++) { n     scanf( %d%d%d , &qx[i], &qy[i], &qv[i]); n     qL[i] = LCA(qx[i], qy[i]); n     insert(qv[i], -i); n     ans[i] = 1; n   } n   for (int i = (int)(1); i <= (int)(*pri); i++) solve(i); n   for (int i = (int)(1); i <= (int)(Q); i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long int dp[100000 + 5] = {0}; n void fun(long long int *A, long long int N) { n   dp[0] = 0; n   long long int a = 0; n   for (long long int i = 1; i <= 100000; ++i) { n     while (a < N and A[a] <= i) ++a; n     dp[i] = a; n   } n } n void solve() { n   long long int N; n   cin >> N; n   long long int A[N]; n   for (long long int i = 0; i < N; ++i) cin >> A[i]; n   sort(A, A + N); n   fun(A, N); n   long long int Q; n   cin >> Q; n   while (Q--) { n     long long int M; n     cin >> M; n     if (M > 100000) n       cout << N <<   n ; n     else n       cout << dp[M] <<   n ; n   } n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int T = 1; n   while (T--) { n     solve(); n   } n   exit(0); n } n


#include <bits/stdc++.h> n const int maxn = 105; n int a[maxn]; n inline int max(int a, int b) { return a > b ? a : b; } n int main() { n   int n, ans = 0, temp; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (n < 3 || n % 2 == 0) n     puts( -1 ); n   else { n     for (int i = (n - 1); i >= 1; i -= 2) { n       temp = max(a[i], a[i + 1]); n       ans += temp; n       a[i / 2] -= temp; n       if (a[i / 2] < 0) a[i / 2] = 0; n     } n     ans += a[1]; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool a[200087]; n int main(void) { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     a[temp + 100000] = true; n   } n   int ans = 0; n   for (int i = 0; i <= 200000; i++) { n     if (i != 100000 && a[i] == true) { n       ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void Fail() { n   printf( 0 ); n   exit(0); n } n const int maxn = 100005; n int n, l, r, mn, id, ans, from, to, cnt, clen, mxdep, mid; n vector<pair<int, int> > edge[maxn]; n pair<int, pair<int, int> > cur[maxn]; n vector<int> beauty; n int sz[maxn], u[maxn], v[maxn], c[maxn], cen[maxn][25], total[maxn][25]; n bool centroid[maxn], debug; n class segtree { n  public: n   pair<int, int> tree[maxn * 4]; n   int sz; n   void _init(int tot) { n     for (sz = 1; sz < tot; sz <<= 1) n       ; n     for (int(i) = 0; (i) < sz << 1; i++) tree[i] = make_pair(-1e9, -1); n   } n   void upd(int pos, int val, int id) { n     pos += sz - 1; n     tree[pos] = max(tree[pos], make_pair(val, id)); n     while (pos) { n       pos >>= 1; n       if (!pos) break; n       tree[pos] = max(tree[pos << 1], tree[pos << 1 | 1]); n     } n   } n   void clr(int pos) { n     pos += sz - 1; n     tree[pos] = make_pair(-1e9, -1); n     while (pos) { n       pos >>= 1; n       if (!pos) break; n       tree[pos] = max(tree[pos << 1], tree[pos << 1 | 1]); n     } n   } n   pair<int, int> qry(int l, int r) { n     if (r < 1) return make_pair(-1e9, -1); n     l = max(l, 1); n     r = min(r, sz); n     l += sz - 1; n     r += sz - 1; n     pair<int, int> res = make_pair(-1e9, -1); n     for (; l <= r; l >>= 1, r >>= 1) { n       if (l & 1) res = max(res, tree[l++]); n       if ((r - 1) & 1) res = max(res, tree[r--]); n     } n     return res; n   } n } T; n void get_sz(int x, int p) { n   sz[x] = 1; n   for (int(i) = 0; (i) < edge[x].size(); i++) { n     pair<int, int> y = edge[x][i]; n     if (centroid[y.first] || y.first == p) continue; n     get_sz(y.first, x); n     sz[x] += sz[y.first]; n   } n } n void get_centroid(int x, int p, int tot) { n   int mx = tot - sz[x]; n   for (int(i) = 0; (i) < edge[x].size(); i++) { n     pair<int, int> y = edge[x][i]; n     if (centroid[y.first] || y.first == p) continue; n     get_centroid(y.first, x, tot); n     mx = max(mx, sz[y.first]); n   } n   if (mx < mn) { n     mn = mx; n     id = x; n   } n } n void get_dis(int x, int p, int dep, int dis, int s) { n   mxdep = max(mxdep, dep); n   cur[clen++] = make_pair(dep, make_pair(dis, x)); n   pair<int, int> mx = T.qry(l - dep + 1, r - dep + 1); n   if (dis + mx.first > ans) { n     ans = dis + mx.first; n     from = x; n     to = mx.second; n   } n   for (int(i) = 0; (i) < edge[x].size(); i++) { n     pair<int, int> y = edge[x][i]; n     if (centroid[y.first] || y.first == p) continue; n     get_dis(y.first, x, dep + 1, dis + (y.second >= mid ? 1 : -1), s); n   } n } n void solve(int t, int dep) { n   int s = cen[t][dep]; n   if (s < 0) { n     mn = 1e9; n     get_sz(t, -1); n     get_centroid(t, -1, sz[t]); n     s = id; n     cen[t][dep] = s; n     total[t][dep] = sz[t]; n   } n   centroid[s] = true; n   T._init(total[t][dep] + 1); n   T.upd(1, 0, s); n   for (int(i) = 0; (i) < edge[s].size(); i++) { n     if (centroid[edge[s][i].first]) continue; n     clen = 0; n     get_dis(edge[s][i].first, s, 1, edge[s][i].second >= mid ? 1 : -1, s); n     for (int(j) = 0; (j) < clen; j++) { n       T.upd(cur[j].first + 1, cur[j].second.first, cur[j].second.second); n     } n   } n   for (int(i) = 0; (i) < edge[s].size(); i++) n     if (!centroid[edge[s][i].first]) solve(edge[s][i].first, dep + 1); n   centroid[s] = false; n } n void add_edge(int x, int y, int z) { n   edge[x].push_back(make_pair(y, z)); n   edge[y].push_back(make_pair(x, z)); n } n bool check() { n   ans = -1e9; n   solve(0, 0); n   return ans >= 0; n } n int main() { n   memset(cen, -1, sizeof(cen)); n   scanf( %d%d%d , &n, &l, &r); n   for (int(i) = 0; (i) < n - 1; i++) { n     scanf( %d%d%d , &u[i], &v[i], &c[i]); n     u[i]--, v[i]--; n     add_edge(u[i], v[i], c[i]); n     beauty.push_back(c[i]); n   } n   sort(beauty.begin(), beauty.end()); n   beauty.resize(unique(beauty.begin(), beauty.end()) - beauty.begin()); n   int L = 0, R = beauty.size(); n   while (R - L > 1) { n     int Mid = (L + R) >> 1; n     mid = beauty[Mid]; n     if (check()) n       L = Mid; n     else n       R = Mid; n   } n   debug = true; n   mid = beauty[L]; n   check(); n   printf( %d %d , from + 1, to + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, r; n int ans[1000000 + 5]; n long long mmod = 2333, mod = 1e9 + 7; n long long shash[1000000 + 5], base[1000000 + 5]; n char s[1000000 + 5]; n int main() { n   scanf( %d%s , &n, s + 1); n   base[0] = 1; n   for (int i = 1; i <= n; i++) { n     shash[i] = (shash[i - 1] * mmod + s[i] -  a  + 1) % mod; n     base[i] = base[i - 1] * mmod % mod; n   } n   if (n % 2 == 1) { n     ans[(n + 1) / 2] = -1; n     l = r = (n + 1) / 2; n   } else { n     l = n / 2; n     r = n / 2 + 1; n     if (s[l] == s[r]) n       ans[n / 2] = 1; n     else n       ans[n / 2] = -1; n   } n   for (int i = (n + 1) / 2 - 1; i >= 1; i--) { n     l--; n     r++; n     for (int j = ans[i + 1] + 2; j >= 1; j -= 2) { n       long long a = n           (shash[l + j - 1] - shash[l - 1] * base[j] % mod + mod) % mod; n       long long b = (shash[r] - shash[r - j] * base[j] % mod + mod) % mod; n       if (a == b) { n         ans[i] = j; n         break; n       } n     } n     if (ans[i] == 0) ans[i] = -1; n   } n   for (int i = 1; i < (n + 1) / 2; i++) printf( %d  , ans[i]); n   printf( %d , ans[(n + 1) / 2]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[6][7] = { n     {0, 0, 0, 1, 1, 1, 1}, {0, 1, 1, 0, 0, 1, 1}, {1, 0, 1, 0, 1, 0, 1}, n     {0, 1, 1, 1, 1, 0, 0}, {1, 0, 1, 1, 0, 1, 0}, {1, 1, 0, 0, 1, 1, 0}, n }; n int aux[6][7]; n int res[7]; n bool solve() { n   int p, div; n   for (int i = 0; i < 6; ++i) { n     p = i; n     for (int j = i + 1; j < 6; ++j) n       if (abs(aux[j][i]) > abs(aux[p][i])) p = j; n     for (int j = 0; j <= 6; ++j) swap(aux[i][j], aux[p][j]); n     div = aux[i][i]; n     for (int k = i; k <= 6; ++k) { n       if (aux[i][k] % div != 0) return 0; n       aux[i][k] /= div; n     } n     for (int j = i + 1; j < 6; ++j) { n       div = aux[j][i]; n       for (int k = i; k <= 6; ++k) aux[j][k] -= div * aux[i][k]; n     } n   } n   for (int i = 6 - 1; i >= 0; --i) { n     res[i] = aux[i][6]; n     for (int k = 6 - 1; k > i; --k) res[i] -= res[k] * aux[i][k]; n   } n   return 1; n } n int main() { n   int big = 0; n   for (int i = 0; i < 6; ++i) n     scanf( %d , &mat[i][6]), big = max(big, mat[i][6]); n   bool g = 0; n   for (int i = 0; i <= big && !g; ++i) { n     memcpy(aux, mat, sizeof aux); n     for (int j = 0; j < 3; ++j) aux[j][6] -= i; n     g = solve(); n     for (int j = 0; j < 6 && g; ++j) n       if (res[j] < 0) g = 0; n     res[6] = i; n     int a = 0; n     for (int i = 0; i < 7; ++i) a += res[i]; n     if (!g) continue; n     printf( %d n , a); n     for (int j = 3; j >= 0; --j) { n       for (int i = 0; i < 7; ++i) { n         char b =  a ; n         if ((i + 1) & 1 << j) b =  b ; n         for (int k = 0; k < res[i]; ++k) printf( %c , b); n       } n       printf(  n ); n     } n   } n   if (!g) printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 150004; n const long long mod = 998244353; n struct edge { n   long long next, to; n } h[MAX << 1]; n long long head[MAX], cnt, cnt_dfsn; n long long n, q, ans, inv; n inline void add(long long x, long long y) { n   h[++cnt] = edge{head[x], y}; n   head[x] = cnt; n   h[++cnt] = edge{head[y], x}; n   head[y] = cnt; n } n long long dfsn[MAX], size[MAX], son[MAX], fa[MAX], top[MAX], w[MAX]; n long long tree[MAX]; n inline long long lowbit(long long x) { return x & (-x); } n inline void ad(long long x, long long y) { n   y %= mod; n   for (long long i = x; i <= n; i += lowbit(i)) tree[i] = (tree[i] + y) % mod; n } n inline long long sum(long long x) { n   long long ret = 0; n   for (long long i = x; i; i -= lowbit(i)) ret = (ret + tree[i]) % mod; n   return ret; n } n inline long long quick(long long x, long long y) { n   long long ret = 1ll; n   while (y) { n     if (y & 1) ret = (ret * x) % mod; n     y >>= 1; n     x = (x * x) % mod; n   } n   return ret; n } n void dfs1(long long now, long long father) { n   fa[now] = father; n   size[now] = 1; n   for (long long i = head[now]; i; i = h[i].next) { n     long long aim = h[i].to; n     if (aim != father) { n       dfs1(aim, now); n       size[now] += size[aim]; n       if (size[aim] > size[son[now]]) son[now] = aim; n     } n   } n } n void dfs2(long long now, long long start) { n   top[now] = start; n   dfsn[now] = ++cnt_dfsn; n   if (son[now]) dfs2(son[now], start); n   for (long long i = head[now]; i; i = h[i].next) { n     long long aim = h[i].to; n     if (aim != fa[now] && aim != son[now]) { n       dfs2(aim, aim); n     } n   } n } n signed main() { n   scanf( %lld %lld , &n, &q); n   inv = quick(n, mod - 2); n   for (long long x, y, i = 1; i < n; i++) scanf( %lld %lld , &x, &y), add(x, y); n   dfs1(1, 1), dfs2(1, 1); n   fa[1] = 0; n   for (long long op, x, y, i = 1; i <= q; i++) { n     scanf( %lld , &op); n     if (op == 1) { n       scanf( %lld %lld , &x, &y); n       w[x] = (w[x] + y) % mod; n       ad(1, size[x] * y % mod); n       ad(dfsn[x], (mod - size[x] * y % mod) % mod), n           ad(dfsn[x] + size[x], size[x] * y % mod); n       if (son[x]) n         ad(dfsn[son[x]], (n - size[son[x]]) * y % mod), n             ad(dfsn[son[x]] + size[son[x]], mod - (n - size[son[x]]) * y % mod); n     } else { n       scanf( %lld , &x); n       ans = (sum(dfsn[x]) + n * w[x] % mod) % mod; n       for (long long j = top[x]; fa[j]; j = top[fa[j]]) n         ans = (ans + (n - size[j]) * w[fa[j]] % mod) % mod; n       printf( %lld n , (ans * inv) % mod); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int limit = 1000005; n void die() { n   cout <<  NO  << endl; n   exit(0); n } n void computereachable(int u, int code, vector<int> g[limit], n                       int reachable[limit]) { n   if (reachable[u] & code) return; n   reachable[u] |= code; n   vector<int> &ar = g[u]; n   for (int i = 0; i < int(ar.size()); i++) n     computereachable(ar[i], code, g, reachable); n } n int n, m; n vector<int> g[limit], ginv[limit]; n int reachdir[limit]; n int reachinv[limit]; n int nsink; n int nsour; n int listsour[limit]; n int norder; n int order[limit]; n int seen[limit]; n void generatepostorder(int u) { n   if (seen[u]) return; n   seen[u] = 1; n   vector<int> &ar = ginv[u]; n   for (int i = 0; i < int(ar.size()); i++) generatepostorder(ar[i]); n   order[norder++] = u; n } n void traverse(int u) { n   if (seen[u]) return; n   seen[u] = 1; n   vector<int> &ar = g[u]; n   for (int i = 0; i < int(ar.size()); i++) traverse(ar[i]); n } n void checkalreadystrong() { n   for (int u = 1; u <= n; u++) generatepostorder(u); n   reverse(order, order + norder); n   for (int u = 1; u <= n; u++) seen[u] = 0; n   traverse(order[0]); n   for (int u = 1; u <= n; u++) n     if (not seen[u]) die(); n   cout <<  YES  << endl; n   exit(0); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     ginv[v].push_back(u); n   } n   for (int u = 1; u <= n; u++) { n     if (int(g[u].size()) == 0) { n       computereachable(u, 1 << (nsink++), ginv, reachinv); n     } n     if (int(ginv[u].size()) == 0) { n       computereachable(u, 1, g, reachdir); n       listsour[nsour++] = u; n     } n   } n   if (nsour == 0) checkalreadystrong(); n   for (int u = 1; u <= n; u++) n     if (not reachdir[u] or not reachinv[u]) die(); n   for (int c = 1; c < (1 << nsour) - 1; c++) { n     int creach = 0; n     for (int b = 0; b < nsour; b++) n       if (c & (1 << b)) creach |= reachinv[listsour[b]]; n     if (__builtin_popcount(creach) <= __builtin_popcount(c)) die(); n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, len, wmax, ans; n vector<long long> vec[2]; n inline void _read(long long &x) { n   long long f = 1; n   x = 0; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) n       f = -1; n     else n       f = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n   return; n } n inline void _print(long long x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x >= 10) _print(x / 10); n   putchar(x % 10 +  0 ); n   return; n } n inline long long chk(long long x1, long long x2) { n   long long x = x1 + x2, y = x2 - x1; n   return y * wmax > x && y * wmax > -x; n } n int main() { n   _read(n); n   _read(len); n   _read(wmax); n   long long x, v; n   for (long long i = 1; i <= n; i++) { n     _read(x); n     _read(v); n     if (v == -1) n       vec[0].push_back(x); n     else n       vec[1].push_back(x); n   } n   sort(vec[0].begin(), vec[0].end()); n   if (!vec[0].size()) { n     _print(0); n     return 0; n   } n   long long l, r, mid; n   for (long long i = 0; i < vec[1].size(); i++) { n     l = 0; n     r = vec[0].size(); n     while (l < r) { n       mid = (l + r) >> 1; n       if (chk(vec[1][i], vec[0][mid] + len)) n         r = mid; n       else n         l = mid + 1; n     } n     ans += (vec[0].size() - r); n   } n   _print(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3010; n bool visit[maxn]; n int n, m, ans, f[maxn]; n vector<int> ve; n vector<pair<int, int> > maps[maxn]; n void first_dfs(const int u) { n   visit[u] = 1; n   for (int i = 0; i < ((int)(maps[u]).size()); ++i) { n     int v = maps[u][i].first; n     int w = maps[u][i].second; n     if (!visit[v]) { n       first_dfs(v); n       f[u] += f[v] + w; n     } n   } n } n void second_dfs(const int u) { n   visit[u] = 1; n   for (int i = 0; i < ((int)(maps[u]).size()); ++i) { n     int v = maps[u][i].first; n     int w = maps[u][i].second; n     if (!visit[v]) { n       f[v] = f[u] + (w ? -1 : 1); n       m = min(m, f[v]); n       second_dfs(v); n     } n   } n } n void solve(const int u) { n   first_dfs(u); n   ans = min(ans, f[u]); n   if (((int)(maps[u]).size()) >= 2) { n     ve.clear(); n     memset(visit, 0, sizeof(visit)); n     visit[u] = 1; n     for (int i = 0; i < ((int)(maps[u]).size()); ++i) { n       int v = maps[u][i].first; n       int w = maps[u][i].second; n       m = f[v]; n       second_dfs(v); n       ve.push_back(m + (w ? -1 : 1) - f[v]); n     } n     sort(ve.begin(), ve.end()); n     ans = min(ans, f[u] + ve[0] + ve[1]); n   } n } n int main() { n   scanf( %d , &n); n   int u, v; n   for (int i = 1; i < n; ++i) { n     scanf( %d%d , &u, &v); n     maps[u].push_back(make_pair(v, 0)); n     maps[v].push_back(make_pair(u, 1)); n   } n   ans = maxn; n   for (int i = 1; i <= n; ++i) { n     memset(f, 0, sizeof(f)); n     memset(visit, 0, sizeof(visit)); n     solve(i); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int check(int m, int k, string s) { n   int i; n   for (i = m + 1; i <= m + k && i < s.size(); i++) { n     if (s[i] ==  1 ) return i; n   } n   return -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t > 0) { n     int i = 0, n, k, m, s = 0; n     string a; n     cin >> n >> k >> a; n     for (i = 0; i < n;) { n       int temp = check(i, k, a); n       if (temp == -1) { n         if (a[i] ==  0 ) { n           s++; n         } n         i += (k + 1); n       } else n         i = temp; n     } n     cout << s << endl; n     t--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n bool ans[50000][4]; n int main() { n   cin >> t; n   for (int i = 0; i < t; i++) { n     int toys[4]; n     for (int j = 0; j < 4; j++) { n       cin >> toys[j]; n     } n     bool neg = (toys[0] + toys[1]) % 2 == 1; n     bool small = toys[1] >= 1 || toys[2] >= 1; n     bool big = toys[0] >= 1 || toys[3] >= 1; n     if (neg) { n       if (small) { n         ans[i][1] = true; n       } n       if (big) { n         ans[i][0] = true; n       } n     } else { n       if (small) { n         ans[i][2] = true; n       } n       if (big) { n         ans[i][3] = true; n       } n     } n   } n   for (int i = 0; i < t; i++) { n     for (int j = 0; j < 4; j++) { n       if (j != 0) { n         cout <<    ; n       } n       if (ans[i][j]) { n         cout <<  Ya ; n       } else { n         cout <<  Tidak ; n       } n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using u32 = unsigned int; n using db = double; n using ld = long double; n using pii = std::array<int, 2>; n using pll = std::array<ll, 2>; n const int INF = 0x3f3f3f3f; n const long long INFll = 0x3f3f3f3f3f3f3f3f; n namespace Fast_in { n template <typename T> n void in(T &x) { n   x = 0; n   bool f = false; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = true; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   if (f) x = -x; n } n template <typename T> n T in() { n   T x = 0; n   bool f = false; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = true; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return f ? -x : x; n } n template <typename T, typename... Tp> n void in(T &x, Tp &...a) { n   in(x); n   in(a...); n } n template <typename T> n void in(T *l, T *r) { n   for (; l != r; ++l) in(*l); n } n }  // namespace Fast_in n using namespace Fast_in; n namespace Fast_out { n template <typename T> n void out_sub(T x, char ch =   n ) { n   static int tp, sk[30]; n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   do { n     sk[++tp] = x % 10; n     x /= 10; n   } while (x); n   while (tp) { n     putchar(sk[tp--] +  0 ); n   } n   putchar(ch); n } n void out(int x, char ch =   n ) { out_sub(x, ch); } n void out(ll x, char ch =   n ) { out_sub(x, ch); } n void out(u32 x, char ch =   n ) { out_sub(x, ch); } n void out(ull x, char ch =   n ) { out_sub(x, ch); } n void out(db x, char ch =   n ) { printf( %.12f%c , x, ch); } n void out(ld x, char ch =   n ) { printf( %.12Lf%c , x, ch); } n void out(char x, char ch =   n ) { printf( %d%c , x, ch); } n void out(char *x, char ch =   n ) { printf( %s%c , x, ch); } n void out(string x, char ch =   n ) { printf( %s%c , x.c_str(), ch); } n void out(pii x) { out_sub(x[0],    ), out_sub(x[1]); } n void out(pll x) { out_sub(x[0],    ), out_sub(x[1]); } n template <typename T> n void out(vector<T> x, char ch =    ) { n   if (x.empty()) { n     puts(  ); n     return; n   } n   T val = x.back(); n   x.pop_back(); n   for (auto i : x) out(i, ch); n   out(val); n } n template <typename T> n void out(set<T> x, char ch =    ) { n   if (x.empty()) { n     puts(  ); n     return; n   } n   T val = *x.rbegin(); n   x.erase(--x.end()); n   for (auto i : x) out(i, ch); n   out(val); n } n template <typename T> n void out(T *l, T *r, char ch =    ) { n   if (l == r) { n     puts(  ); n     return; n   } n   --r; n   for (; l != r; ++l) out(*l, ch); n   if (l == r) out(*l); n } n template <typename T, typename... Tp> n void out(T x, Tp... a) { n   out(x,    ); n   out(a...); n } n }  // namespace Fast_out n using namespace Fast_out; n namespace Bin { n template <int len = 0, typename T> n void out_bin(T x, char ch =   n ) { n   if (len) { n     cout << bitset<len>(x).to_string() << ch; n     return; n   } n   long long val = 1ll << 32; n   bool flg = false; n   if (x > val) { n     for (int i = 63; ~i; --i) n       if (flg) n         printf( %d , (x >> i) & 1); n       else if ((x >> i) & 1) { n         flg = true; n         printf( 1 ); n       } n   } else { n     for (int i = 31; ~i; --i) n       if (flg) n         printf( %d , (x >> i) & 1); n       else if ((x >> i) & 1) { n         flg = true; n         printf( 1 ); n       } n   } n   putchar(ch); n } n template <typename T> n void in_bin(T &x) { n   x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) { n     x <<= 1; n     x |= ch & 1; n     ch = getchar(); n   } n } n }  // namespace Bin n using namespace Bin; n struct Timer { n   int val, pre; n   Timer() { n     pre = clock(); n     val = clock(); n   } n   void operator()(int flg = 2) { n     if (flg == 2) val = pre; n     int now = clock(); n     if (flg != 0) std::cerr << now - val <<  ms n ; n     if (flg != 1) val = now; n     pre = now; n   } n }; n Timer Time; n struct Random { n   mt19937 my_rand; n   Random() { n     srand((long long)new char); n     my_rand.seed(rand()); n   } n }; n Random __Rd; n template <typename T> n T rnd(T _l, T _r) { n   T len = _r - _l + 1; n   return __Rd.my_rand() % len + _l; n } n template <typename T> n void Max(T &x, T y) { n   x < y && (x = y); n } n template <typename T> n void Min(T &x, T y) { n   x > y && (x = y); n } n template <typename T, class Func> n void Max(T &x, T y, Func F) { n   if (x < y) { n     x = y; n     F(); n   } n } n template <typename T, class Func> n void Min(T &x, T y, Func F) { n   if (x > y) { n     x = y; n     F(); n   } n } n template <typename T> n T abss(T x) { n   return x < 0 ? -x : x; n } n void File(string In, string Out) { n   if (!In.empty()) freopen(In.c_str(),  r , stdin); n   if (!Out.empty()) freopen(Out.c_str(),  w , stdout); n } n namespace Mod { n const int md = 998244353; n inline int add(const int &a, const int &b) { n   int p = a + b; n   if (p >= md) p -= md; n   return p; n } n inline int sub(const int &a, const int &b) { n   int p = a - b; n   if (p < 0) p += md; n   return p; n } n inline void add_(int &a, int b) { n   a += b; n   if (a >= md) a -= md; n } n inline void sub_(int &a, int b) { n   a -= b; n   if (a < 0) a += md; n } n inline int __mul(const int &a, const int &b) { return 1ll * a * b % md; } n inline int mul(const int &a, const int &b) { n   int p = __mul(a, b); n   if (p < 0) p += md; n   return p; n } n template <typename T> n int qpow(int a, T b) { n   int ret = 1; n   for (; b; b >>= 1, a = mul(a, a)) n     if (b & 1) ret = mul(ret, a); n   return ret; n } n inline int inv(int x) { return qpow(x, md - 2); } n inline void inv(int *F, int len, int *G = NULL, bool flg = false) { n   if (G == NULL) G = F; n   if (flg) { n     F[0] = G[0] = 1; n     for (int i = 1; i <= len; ++i) F[i] = __mul(F[i - 1], i); n   } n   std::vector<int> S(len + 1), iS(len + 1); n   S[0] = 1; n   for (int i = 1; i <= len; ++i) S[i] = mul(S[i - 1], F[i]); n   iS[len] = inv(S[len]); n   for (int i = len; i >= 1; --i) { n     iS[i - 1] = mul(iS[i], F[i]); n     G[i] = mul(S[i - 1], iS[i]); n   } n } n }  // namespace Mod n using namespace Mod; n namespace gvihvo { n const int N = 5010; n int n, k; n char s[N]; n int C[N][N]; n int sum[N]; n int top; n pii stk[N]; n void main() { n   in(n, k); n   if (k == 0) { n     out(1); n     return; n   } n   scanf( %s , s + 1); n   int ans = 0; n   for (int i = (0); i <= (n); ++i) { n     C[i][0] = 1; n     for (int j = (1); j <= (i); ++j) n       C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]); n   } n   for (int i = (1); i <= (n); ++i) sum[i] = sum[i - 1] + (s[i] ==  1 ); n   for (int i = 1, j = 1; i <= n; ++i) { n     if (s[i - 1] ==  0 ) continue; n     while (j + 1 <= n && sum[j + 1] - sum[i - 1] <= k) ++j; n     if (sum[j] - sum[i - 1] != k) break; n     stk[++top] = {i, j}; n   } n   if (!top) { n     out(1); n     return; n   } n   for (int i = (1); i <= (top - 1); ++i) { n     ans = add(ans, C[stk[i][1] - stk[i][0] + 1][k]); n     if (stk[i][1] >= stk[i + 1][0]) { n       ans = sub(ans, C[stk[i][1] - stk[i + 1][0] + 1] n                       [sum[stk[i][1]] - sum[stk[i + 1][0] - 1]]); n     } else n       ans = sub(ans, 1); n   } n   ans = add(ans, C[stk[top][1] - stk[top][0] + 1][k]); n   out(ans); n } n void init() { ; } n }  // namespace gvihvo n int main() { n   gvihvo::init(); n   gvihvo::main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v[100005]; n bool visit[100005]; n int n; n vector<int> ans; n bool dfs(int i, int last) { n   visit[i] = true; n   bool flag = false; n   for (int j = 0; j < v[i].size(); j++) { n     if (!visit[v[i][j].first]) { n       if (v[i][j].second == 2) n         flag = flag | dfs(v[i][j].first, v[i][j].first); n       else n         flag = flag | dfs(v[i][j].first, 0); n     } n   } n   if (flag == false && last != 0) { n     flag = true; n     ans.push_back(i); n   } n   return flag; n } n void Readinput() { n   int i, x, y, t; n   scanf( %d , &n); n   for (i = 0; i < n - 1; i++) { n     scanf( %d , &x); n     scanf( %d , &y); n     scanf( %d , &t); n     v[x].push_back(pair<int, int>(y, t)); n     v[y].push_back(pair<int, int>(x, t)); n   } n } n void solve() { n   int i, j; n   dfs(1, 0); n   cout << ans.size() << endl; n   for (i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   cout << endl; n } n int main() { n   memset(visit, false, sizeof(visit)); n   Readinput(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int shuruHoJaa(long long int test) { n   while (test--) { n     long long int a, b, c, d; n     cin >> a >> b >> c >> d; n     long long int time = 0; n     if (b >= a) { n       cout << b; n     } else if (c <= d) { n       cout << -1; n     } else { n       time += b; n       long long int tmp = n           ((a - b) % (c - d) == 0 ? (a - b) / (c - d) : (a - b) / (c - d) + 1); n       time += (tmp * (c)); n       cout << time; n     } n     cout <<   n ; n   } n   return 0; n } n int main(int argc, char const *argv[]) { n   int t; n   cin >> t; n   shuruHoJaa(t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1010]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , s); n   int a0 = 0; n   int a1 = 0; n   int b0 = 0; n   int b1 = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  0 ) { n       if (i % 2) n         b0++; n       else n         a0++; n     } else { n       if (i % 2) n         b1++; n       else n         a1++; n     } n   } n   int ans = min(a0 + b1, a1 + b0); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, pl = 4, otv = 1; n   cin >> n; n   if (n > 1) { n     for (long long i = 1; i < n; i++) { n       otv = otv + pl; n       pl += 4; n     } n   } else if (n == 1) { n     otv = 1; n   } n   cout << otv; n } n


#include <bits/stdc++.h> n using namespace std; n struct _ { n   ios_base::Init i; n   _() { n     cin.sync_with_stdio(0); n     cin.tie(0); n   } n } _; n struct House { n   int height; n   int initial_position; n }; n int main() { n   int n; n   cin >> n; n   vector<House> houses; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     House h{x, i}; n     houses.push_back(h); n   } n   stable_sort(houses.begin(), houses.end(), n               [&](House h1, House h2) { return h1.height < h2.height; }); n   int up_to = 0; n   int number_of_blocks = 0; n   for (int i = 0; i < n; i++) { n     int initial_position = houses[i].initial_position; n     up_to = max(up_to, initial_position); n     if (up_to == i) { n       up_to++; n       number_of_blocks++; n     } n   } n   cout << number_of_blocks << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, freq[100123], pairs = 0; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> x; n   long long v[n]; n   for (long long i = 0; i < n; i++) { n     cin >> v[i]; n     freq[v[i]]++; n   } n   for (long long i = 0; i < 100123; i++) n     if (freq[i] > 0) n       if ((x ^ i) < 100123) pairs += freq[i] * (freq[(x ^ i)] - ((x ^ i) == i)); n   pairs /= 2; n   cout << pairs << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Buffer_Size = 1 << 10; n char buffer[Buffer_Size], *ihead = buffer + Buffer_Size, *itail = ihead; n char Next_Char() { n   if (ihead == itail) fread(buffer, 1, Buffer_Size, stdin), ihead = buffer; n   return *ihead++; n } n template <typename T> n void read(T &n) { n   n = 0; n   char ch = Next_Char(); n   while (!isdigit(ch)) ch = Next_Char(); n   while (isdigit(ch)) { n     n = (n << 3) + (n << 1) + (ch & 15); n     ch = Next_Char(); n   } n } n long long r, c, n, k, tp, sum; n long long a[3002], cnt[3002], L[3002], R[3002]; n vector<long long> v[3002]; n struct Position { n   long long x, y; n } p[3002]; n bool Cmp(long long x, long long y) { return p[x].y < p[y].y; } n bool DFS(long long x, long long step) { n   if (!step || !x) return 1; n   if (!DFS(L[x], step - 1)) n     cnt[L[x]] = cnt[x]; n   else n     cnt[L[x]] += cnt[x]; n   return 0; n } n int main() { n   read(r), read(c), read(n), read(k); n   for (long long i = 1; i <= n; ++i) { n     read(p[i].x), read(p[i].y); n     v[p[i].x].push_back(i); n   } n   long long ans = 0; n   for (long long i = 1; i <= r; ++i) { n     tp = sum = 0; n     for (long long j = 1; j <= n; ++j) n       if (p[j].x >= i) a[++tp] = j; n     sort(a + 1, a + tp + 1, Cmp); n     a[tp + 1] = n + 1; n     memset(cnt, 0, sizeof(cnt)); n     memset(L, 0, sizeof(L)); n     memset(R, 0, sizeof(R)); n     long long now = 0; n     for (long long j = 1; j <= c; ++j) { n       while (p[a[now + 1]].y == j && now < tp) { n         ++now; n         L[a[now]] = a[now - 1], R[a[now]] = a[now + 1]; n       } n       long long tmp = a[now]; n       for (long long l = 1; l < k; ++l) tmp = L[tmp]; n       ++cnt[tmp]; n     } n     for (long long j = 1; j <= tp; ++j) sum += p[a[j]].y * cnt[a[j]]; n     ans += sum; n     for (long long j = r; j >= i + 1; --j) { n       long long sz = (long long)v[j].size(); n       for (long long l = 0; l < sz; ++l) { n         long long x = v[j][l], q = x; n         for (long long o = 1; o <= k; ++o) { n           sum -= (p[x].y - p[L[x]].y) * cnt[x]; n           x = L[x]; n           if (!x) break; n         } n         x = q; n         DFS(x, k); n         x = q; n         L[R[x]] = L[x], R[L[x]] = R[x]; n       } n       ans += sum; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[200020], b[200020]; n int x, y, c, d; n int n; n int mx[200020]; n int calc(pair<int, int> a[], int len, int val) { n   for (int i = 1; i <= len; ++i) { n     mx[i] = max(mx[i - 1], a[i].second); n   } n   int ret = 0; n   for (int i = len, j = 1; i >= 1; --i) { n     while (j <= len && a[j].first + a[i].first <= val) { n       ++j; n     } n     if (j > 1 && i > 1) ret = max(ret, a[i].second + mx[min(j - 1, i - 1)]); n   } n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &c, &d); n   for (int i = 1; i <= n; ++i) { n     int u, v; n     char op[5]; n     scanf( %d%d%s , &u, &v, op); n     if (op[0] ==  C ) n       a[++x] = make_pair(v, u); n     else n       b[++y] = make_pair(v, u); n   } n   sort(a + 1, a + x + 1); n   sort(b + 1, b + y + 1); n   int ans = calc(a, x, c); n   ans = max(ans, calc(b, y, d)); n   int mx1 = -1, mx2 = -1; n   for (int i = 1; i <= x; ++i) n     if (a[i].first <= c) mx1 = max(mx1, a[i].second); n   for (int i = 1; i <= y; ++i) n     if (b[i].first <= d) mx2 = max(mx2, b[i].second); n   if (mx1 != -1 && mx2 != -1) ans = max(ans, mx1 + mx2); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 2; n long long int i, j, n, m, s, ans = 0, cnt = 0; n vector<pair<long long int, long long int> > nf; n vector<long long int> graph[maxn]; n bool foundd[maxn]; n void dfs(long long int u, bool found[]) { n   if (!foundd[u]) ++ans; n   found[u] = true; n   vector<long long int>::iterator it; n   for (it = graph[u].begin(); it != graph[u].end(); ++it) { n     if (!found[*it]) dfs(*it, found); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m >> s; n   bool found[5200], isreachable[5200]; n   for (i = 0; i < m; ++i) { n     long long int u, v; n     cin >> u >> v; n     graph[u].push_back(v); n   } n   memset(found, false, sizeof(found)); n   dfs(s, found); n   for (i = 1; i <= n; ++i) foundd[i] = found[i]; n   for (i = 1; i <= n; ++i) { n     if (!foundd[i]) { n       bool found1[5001]; n       memset(found1, false, sizeof(found1)); n       ans = 0; n       dfs(i, found1); n       nf.push_back(make_pair(ans, i)); n     } n   } n   sort(nf.begin(), nf.end(), greater<pair<long long int, long long int> >()); n   for (i = 0; i < nf.size(); ++i) { n     if (!foundd[nf[i].second]) { n       bool found1[5001]; n       memset(found1, false, sizeof(found1)); n       dfs(nf[i].second, found1); n       ++cnt; n       for (j = 1; j <= n; ++j) n         if (found1[j]) foundd[j] = true; n     } n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n void read(int &a) { n   a = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     a = (a << 1) + (a << 3) + c -  0 ; n     c = getchar(); n   } n } n int Set(int n, int pos) { return n = n | (1LL << pos); } n int reset(int n, int pos) { return n = n & ~(1LL << pos); } n bool check(int n, int pos) { return (bool)(n & (1LL << pos)); } n int mul(int a, int b) { n   int c; n   c = (a % 1000000007 * b % 1000000007) % 1000000007; n   return c; n } n int add(int a, int b) { n   int c; n   c = (a % 1000000007 + b % 1000000007) % 1000000007; n   return c; n } n int sub(int a, int b) { n   int c; n   c = ((a % 1000000007 - b % 1000000007) % 1000000007 + 1000000007) % n       1000000007; n   return c; n } n int power(int x, int y) { n   int res = 1; n   x = x; 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 long long exgcd(long long x, long long y, long long &a, long long &b) { n   int flag = 0; n   long long t, la = 1, lb = 0, ra = 0, rb = 1; n   while (x % y) { n     if (flag == 0) n       la -= x / y * ra, lb -= x / y * rb; n     else n       ra -= x / y * la, rb -= x / y * lb; n     t = x, x = y, y = t % y; n     flag = 1 - flag; n   } n   if (flag == 0) n     a = ra, b = rb; n   else n     a = la, b = lb; n   return y; n } n int n, m, k, r; n vector<int> rooms[200005]; n vector<pair<int, int> > graph[200005]; n int st[200005]; n int col[200005]; n deque<int> q; n bool bfs(int ma_src) { n   q.clear(); n   col[ma_src] = 0; n   q.push_back(ma_src); n   while (!q.empty()) { n     int u = q.front(); n     q.pop_front(); n     for (int i = 0; i < graph[u].size(); i++) { n       int v = graph[u][i].first; n       int _v = graph[u][i].second; n       if (col[v] == -1) { n         if (_v == 1) n           col[v] = col[u]; n         else n           col[v] = col[u] ^ 1; n         q.push_back(v); n       } else { n         if (_v == 1) { n           if (col[v] != col[u]) return 0; n         } else { n           if (col[v] == col[u]) return 0; n         } n       } n     } n   } n   return 1; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &st[i]); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &k); n     for (int j = 1; j <= k; j++) { n       scanf( %d , &r); n       rooms[r].push_back(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     int u = rooms[i][0]; n     int v = rooms[i][1]; n     graph[u].push_back(pair<int, int>(v, st[i])); n     graph[v].push_back(pair<int, int>(u, st[i])); n   } n   memset((col), (-1), sizeof(col)); n   int ans = 1; n   for (int i = 1; i <= m; i++) { n     if (col[i] == -1) ans = ans & bfs(i); n   } n   if (!ans) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n