#include <bits/stdc++.h> n using namespace std; n template <class T> n void read(T& x) { n   T res = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     res = (res << 3) + (res << 1) + c -  0 ; n     c = getchar(); n   } n   x = res * f; n } n const long long N = 300000 + 10; n long long n, m, mod = 998244353, fac[N], inv[N], mx = 300000; n long long sum[N][55]; n int L[N], R[N], cnt[N]; n long long fpow(long long x, long long p) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = ans * x % mod; n     x = x * x % mod; n     p >>= 1; n   } n   return ans; n } n long long C(int x, int y) { n   if (y < 0 || x < y) return 0; n   return (fac[x] * inv[y] % mod) * inv[x - y] % mod; n } n void init() { n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod; n   inv[n] = fpow(fac[n], mod - 2); n   for (int i = n - 1; i >= 0; i--) { n     inv[i] = inv[i + 1] * (i + 1) % mod; n   } n } n struct node { n   int a, b; n } nod[50]; n int main() { n   read(n), read(m); n   init(); n   for (int i = 1; i <= n; i++) n     read(L[i]), read(R[i]), cnt[L[i]]++, cnt[R[i] + 1]--; n   for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; n   for (int i = 0; i < m; i++) { n     read(nod[i].a), read(nod[i].b); n   } n   for (int i = 0; i <= 2 * m; i++) n     for (int j = 1; j <= n; j++) n       sum[j][i] = (sum[j - 1][i] + C(cnt[j] - i, j - i)) % mod; n   int mx = (1 << m) - 1; n   long long ans = 0; n   for (int i = 0; i <= mx; i++) { n     int l = 1, r = n; n     set<int> in; n     for (int j = 0; j < m; j++) { n       if ((i >> j) & 1) { n         in.insert(nod[j].a); n         in.insert(nod[j].b); n         l = max(l, L[nod[j].a]); n         l = max(l, L[nod[j].b]); n         r = min(r, R[nod[j].a]); n         r = min(r, R[nod[j].b]); n       } n     } n     int num = in.size(); n     long long temp = 0; n     if (l <= r) temp = (sum[r][num] - sum[l - 1][num]) % mod; n     if (__builtin_popcount(i) & 1) { n       ans = (ans - temp) % mod; n     } else { n       ans = (ans + temp) % mod; n     } n   } n   printf( %lld n , (ans % mod + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int LIM = 200 * 1000, MAXN = 300 * 1000; n int fen[MAXN]; n void add(int pos, int val) { n   pos++; n   while (pos < MAXN) { n     fen[pos] += val; n     pos += pos & -pos; n   } n } n int sum(int pos) { n   pos++; n   int tot = 0; n   while (pos) { n     tot += fen[pos]; n     pos -= pos & -pos; n   } n   return tot; n } n bool by_id(pair<pair<int, int>, pair<int, int> > a, n            pair<pair<int, int>, pair<int, int> > b) { n   return a.second.first < b.second.first; n } n vector<int> occ[MAXN]; n int pt[MAXN], a[MAXN], n, q; n pair<pair<int, int>, pair<int, int> > seg[MAXN]; n int main() { n   cin >> n >> q; n   for (int i = 0; i < int(n); i++) { n     cin >> a[i]; n     if (a[i] < LIM) occ[a[i]].push_back(i); n   } n   for (int i = 1; i < int(LIM); i++) n     if (i <= (int((occ[i]).size()))) { n       pt[i] = i - 1; n       add(occ[i][pt[i]], 1); n       if (pt[i] + 1 < (int((occ[i]).size()))) add(occ[i][pt[i] + 1], -1); n     } else n       pt[i] = (int((occ[i]).size())); n   cerr <<  Salam :)  << endl; n   for (int i = 0; i < int(q); i++) { n     seg[i].second.first = i; n     cin >> seg[i].first.first >> seg[i].first.second; n     seg[i].first.first--; n     seg[i].first.second--; n   } n   sort(seg, seg + q); n   int cur = 0; n   for (int i = 0; i < int(n); i++) { n     while (cur < q && seg[cur].first.first == i) { n       seg[cur].second.second = sum(seg[cur].first.second); n       cur++; n     } n     int x = a[i]; n     if (x < LIM && pt[x] < (int((occ[x]).size()))) { n       add(occ[x][pt[x]], -1); n       if (pt[x] + 1 < (int((occ[x]).size()))) add(occ[x][pt[x] + 1], +1); n       pt[x]++; n       if (pt[x] < (int((occ[x]).size()))) { n         add(occ[x][pt[x]], 1); n         if (pt[x] + 1 < (int((occ[x]).size()))) add(occ[x][pt[x] + 1], -1); n       } n     } n   } n   sort(seg, seg + q, by_id); n   for (int i = 0; i < int(q); i++) cout << seg[i].second.second << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 20005; n const int M = 400005; n vector<pair<int, int> > E[N]; n int dep[N], fa[N][16], fav[N][16]; n int n, m, S, T, nd, jud[N * 2]; n int head[M], vis[M], tot = 1; n int dis[M], q[M]; n struct edge { n   int to, next, f; n } e[M * 5]; n void add(int x, int y, int f) { n   e[++tot] = (edge){y, head[x], f}; n   head[x] = tot; n   e[++tot] = (edge){x, head[y], 0}; n   head[y] = tot; n } n bool bfs() { n   for (int i = (int)(1); i <= (int)(nd); i++) dis[i] = -1; n   int h = 0, t = 1; n   q[1] = S; n   dis[S] = 0; n   while (h != t) { n     int x = q[++h]; n     for (int i = head[x]; i; i = e[i].next) n       if (dis[e[i].to] == -1 && e[i].f) { n         dis[e[i].to] = dis[x] + 1; n         if (e[i].to == T) return 1; n         q[++t] = e[i].to; n       } n   } n   return 0; n } n int dfs(int x, int flow) { n   if (x == T) return flow; n   int k, rest = flow; n   for (int i = head[x]; i && rest; i = e[i].next) n     if (dis[e[i].to] == dis[x] + 1 && e[i].f) { n       k = dfs(e[i].to, min(rest, e[i].f)); n       e[i].f -= k; n       e[i ^ 1].f += k; n       rest -= k; n     } n   if (rest) dis[x] = -1; n   return flow - rest; n } n void dfs(int x) { n   for (auto i : E[x]) n     if (i.first != fa[x][0]) { n       dep[i.first] = dep[x] + 1; n       fa[i.first][0] = x; n       fav[i.first][0] = i.second; n       dfs(i.first); n     } 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)(14); i >= (int)(0); i--) n     if (tmp & (1 << i)) x = fa[x][i]; n   for (int i = (int)(14); 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 int main() { n   scanf( %d%d , &n, &m); 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(pair<int, int>(y, i)); n     E[y].push_back(pair<int, int>(x, i)); n   } n   S = nd = n; n   T = ++nd; n   for (int i = (int)(1); i <= (int)(n - 1); i++) add(S, i, 1), jud[i] = tot; n   for (int i = (int)(1); i <= (int)(m); i++) add(++nd, T, 1), jud[i + n] = tot; n   dfs(1); n   for (int i = (int)(1); i <= (int)(14); i++) n     for (int j = (int)(1); j <= (int)(n); j++) { n       fa[j][i] = fa[fa[j][i - 1]][i - 1]; n       if (dep[j] >= 1 << i) { n         fav[j][i] = ++nd; n         add(fav[j][i - 1], fav[j][i], 1 << 30); n         add(fav[fa[j][i - 1]][i - 1], fav[j][i], 1 << 30); n       } n     } n   for (int i = (int)(1); i <= (int)(m); i++) { n     int p = n + 1 + i, x, y; n     scanf( %d%d , &x, &y); n     int L = LCA(x, y); n     for (int j = (int)(14); j >= (int)(0); j--) { n       if (dep[x] - dep[L] >= 1 << j) n         add(fav[x][j], n + 1 + i, 1 << 30), x = fa[x][j]; n       if (dep[y] - dep[L] >= 1 << j) n         add(fav[y][j], n + 1 + i, 1 << 30), y = fa[y][j]; n     } n     assert(x == L && y == L); n   } n   int tmp = 0; n   for (; bfs(); tmp += dfs(S, 1 << 30)) n     ; n   vector<int> ans1, ans2; n   for (int i = (int)(1); i <= (int)(n - 1); i++) n     if (dis[i] == -1) ans1.push_back(i); n   for (int i = (int)(1); i <= (int)(m); i++) n     if (dis[i + n + 1] != -1) ans2.push_back(i); n   assert(ans1.size() + ans2.size() == tmp); n   printf( %d n , ans1.size() + ans2.size()); n   printf( %d  , ans2.size()); n   for (auto i : ans2) printf( %d  , i); n   puts(  ); n   printf( %d  , ans1.size()); n   for (auto i : ans1) printf( %d  , i); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<int, int>, int> M; n set<pair<int, int> > tmp; n char s[77777][11]; n int main() { n   int n; n   scanf( %d , &n); n   for (int ii = 0; ii < n; ii++) { n     scanf( %s , s[ii]); n     for (int i = 0; i < 9; i++) { n       for (int j = i; j < 9; j++) { n         int ans = 0; n         for (int k = i; k <= j; k++) ans = ans * 10 + s[ii][k] -  0 ; n         tmp.insert(make_pair(j - i + 1, ans)); n       } n     } n     for (set<pair<int, int> >::iterator it = tmp.begin(); it != tmp.end(); n          it++) { n       M[*it]++; n     } n     tmp.clear(); n   } n   for (int ii = 0; ii < n; ii++) { n     int di, dj, res = 10; n     for (int i = 0; i < 9; i++) { n       for (int j = i; j < 9; j++) { n         int ans = 0; n         for (int k = i; k <= j; k++) ans = ans * 10 + s[ii][k] -  0 ; n         if (M[make_pair(j - i + 1, ans)] == 1 && res > j - i + 1) { n           res = j - i + 1; n           di = i, dj = j; n         } n       } n     } n     for (int k = di; k <= dj; k++) putchar(s[ii][k]); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int b = 1e4 + 200; n int ans[b]; n int n = 0, ez1, ez2; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ez1 >> ez2; n     ans[i] = (ez2 - (ez1 % ez2)) % ez2; n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 10; n const long long inf = 1e18; n long long n, m, Q, E[N][3], l[N], r[N], op[N]; n long long hd[N], ht = 1, tf[N], id[N]; n long long cnt = 0; n struct edge { n   long long v, w, n; n } e[N * 2]; n void addedge(long long u, long long v, long long w) { n   e[++ht] = (edge){v, w, hd[u]}; n   hd[u] = ht; n } n long long diss[N], dist[N]; n priority_queue<pair<long long, long long> > q; n pair<long long, long long> qtmp; n void Dijkstra(long long S, long long T, bool typed, long long typef, n               long long u = 0) { n   memset((typed ? dist : diss), 0x3f, sizeof((typed ? dist : diss))); n   (typed ? dist : diss)[S] = 0; n   q.push(make_pair(0, S)); n   while (!q.empty()) { n     qtmp = q.top(); n     q.pop(); n     if (qtmp.first != -(typed ? dist : diss)[u = qtmp.second]) continue; n     for (long long i = hd[u], v; i; i = e[i].n) { n       v = e[i].v; n       if ((typed ? dist : diss)[v] > (typed ? dist : diss)[u] + e[i].w) { n         tf[v] = i >> 1; n         (typed ? dist : diss)[v] = (typed ? dist : diss)[u] + e[i].w; n         if (typef == 1 && !op[v]) l[v] = l[u]; n         if (typef == 2 && !op[v]) r[v] = r[u]; n         q.push(make_pair(-(typed ? dist : diss)[v], v)); n       } n     } n   } n } n long long t[N << 3]; n void build(long long k, long long l, long long r) { n   t[k] = inf; n   if (l == r) return; n   long long mid = (l + r) >> 1; n   build(k << 1, l, mid); n   build(k << 1 | 1, mid + 1, r); n } n void modify(long long k, long long l, long long r, long long L, long long R, n             long long x) { n   if (R < l || r < L) return; n   if (L <= l && r <= R) { n     t[k] = min(t[k], x); n     return; n   } n   long long mid = (l + r) >> 1; n   modify(k << 1, l, mid, L, R, x); n   modify(k << 1 | 1, mid + 1, r, L, R, x); n } n long long query(long long k, long long l, long long r, long long x) { n   if (l == r) return t[k]; n   long long mid = (l + r) >> 1; n   if (x <= mid) n     return min(t[k], query(k << 1, l, mid, x)); n   else n     return min(t[k], query(k << 1 | 1, mid + 1, r, x)); n } n signed main() { n   scanf( %lld%lld%lld , &n, &m, &Q); n   memset(id, -1, sizeof(id)); n   for (long long i = 1; i <= m; i++) { n     scanf( %lld%lld%lld , &E[i][0], &E[i][1], &E[i][2]); n     addedge(E[i][0], E[i][1], E[i][2]); n     addedge(E[i][1], E[i][0], E[i][2]); n     id[i] = -1; n   } n   Dijkstra(n, 1, 1, 0); n   op[1] = 1, l[1] = r[1] = 0; n   for (long long i = 1, x = 1, tmp; x != n; i++) { n     id[tmp = tf[x]] = i, ++cnt; n     op[x ^= E[tmp][0] ^ E[tmp][1]] = 1; n     l[x] = r[x] = i; n   } n   Dijkstra(1, n, 0, 1); n   Dijkstra(n, 1, 1, 2); n   build(1, 1, cnt); n   for (long long i = 1; i <= m; i++) n     if (!~id[i]) { n       modify(1, 1, cnt, l[E[i][0]] + 1, r[E[i][1]], n              diss[E[i][0]] + E[i][2] + dist[E[i][1]]); n       modify(1, 1, cnt, l[E[i][1]] + 1, r[E[i][0]], n              diss[E[i][1]] + E[i][2] + dist[E[i][0]]); n     } n   long long ans, tmpw; n   for (long long i = 1, tmp; i <= Q; i++) { n     scanf( %lld%lld , &tmp, &tmpw); n     ans = diss[n]; n     if (id[tmp] != -1) { n       ans -= E[tmp][2] - tmpw; n       if (tmpw > E[tmp][2]) ans = min(ans, query(1, 1, cnt, id[tmp])); n     } else { n       if (tmpw < E[tmp][2]) { n         ans = min(ans, diss[E[tmp][0]] + tmpw + dist[E[tmp][1]]); n         ans = min(ans, diss[E[tmp][1]] + tmpw + dist[E[tmp][0]]); n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 2010, LOG = 20; n int n, m, k, u, v, x, y, t, a, b, ans; n string S[MAXN]; n int lcp[MAXN]; n int dp[MAXN][MAXN], N = 1; n int solve(int l, int r) { n   if (l == r) return 0; n   int id = N++, mid = l; n   for (int i = l + 1; i < r; i++) n     if (lcp[i] < lcp[mid]) mid = i; n   int L = solve(l, mid), sl = mid - l + 1; n   int R = solve(mid + 1, r), sr = r - mid; n   for (int i = 0; i <= sl; i++) n     for (int j = 0; j <= sr; j++) n       dp[id][i + j] = n           max(dp[id][i + j], dp[L][i] + dp[R][j] + i * j * lcp[mid]); n   return id; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> S[i]; n   sort(S + 1, S + n + 1); n   for (int i = 1; i < n; i++) n     while (lcp[i] < min(S[i].size(), S[i + 1].size()) && n            S[i][lcp[i]] == S[i + 1][lcp[i]]) n       lcp[i]++; n   solve(1, n); n   cout << dp[1][k] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MOD = 1000000007; n int L; n ll sum[8040]; n int len[8040]; n int last[8040]; n int lazy[8040]; n void init(int v, int l, int r, const vector<int> &xs) { n   len[v] = xs[r] - xs[l - 1]; n   if (l < r) { n     int md = (l + r) >> 1; n     init(v << 1, l, md, xs); n     init(v << 1 | 1, md + 1, r, xs); n   } n } n void reset(int v, int l, int r, const vector<int> &go) { n   lazy[v] = -1; n   if (l == r) { n     sum[v] = ll(len[v]) * (L - go[l]); n     last[v] = go[l]; n     return; n   } n   int md = (l + r) >> 1; n   reset(v << 1, l, md, go); n   reset(v << 1 | 1, md + 1, r, go); n   sum[v] = sum[v << 1] + sum[v << 1 | 1]; n   last[v] = last[v << 1 | 1]; n } n void push(int v, int l, int r) { n   if (lazy[v] != -1) { n     last[v] = lazy[v]; n     sum[v] = ll(len[v]) * (L - lazy[v]); n     if (l < r) { n       lazy[v << 1] = lazy[v]; n       lazy[v << 1 | 1] = lazy[v]; n     } n     lazy[v] = -1; n   } n } n void modify(int v, int l, int r, int L, int R, int qv) { n   push(v, l, r); n   if (L > r || R < l) return; n   if (L <= l && r <= R) { n     lazy[v] = qv; n     push(v, l, r); n     return; n   } n   int md = (l + r) >> 1; n   modify(v << 1, l, md, L, R, qv); n   modify(v << 1 | 1, md + 1, r, L, R, qv); n   sum[v] = sum[v << 1] + sum[v << 1 | 1]; n   last[v] = last[v << 1 | 1]; n } n int walk(int v, int l, int r, int qv) { n   push(v, l, r); n   if (last[v] <= qv) return -1; n   if (l == r) return l; n   int md = (l + r) >> 1; n   int ans = walk(v << 1, l, md, qv); n   if (ans == -1) ans = walk(v << 1 | 1, md + 1, r, qv); n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int N, K; n   cin >> N >> K >> L; n   vector<int> X(N), Y(N), C(N); n   vector<int> xs = {-1, L}; n   vector<int> ys = {-1, L}; n   for (int i = 0; i < N; ++i) { n     cin >> X[i] >> Y[i] >> C[i]; n     --C[i]; n     xs.emplace_back(X[i]); n     ys.emplace_back(Y[i]); n   } n   sort(xs.begin(), xs.end()); n   xs.resize(unique(xs.begin(), xs.end()) - xs.begin()); n   sort(ys.begin(), ys.end()); n   ys.resize(unique(ys.begin(), ys.end()) - ys.begin()); n   int NX = xs.size(); n   int NY = ys.size(); n   { n     vector<int> order(N); n     iota(order.begin(), order.end(), 0); n     sort(order.begin(), order.end(), [&](int i, int j) { n       return make_pair(Y[i], -X[i]) > make_pair(Y[j], -X[j]); n     }); n     vector<int> newX(N), newY(N), newC(N); n     for (int i = 0; i < N; ++i) { n       newX[i] = X[order[i]]; n       newY[i] = Y[order[i]]; n       newC[i] = C[order[i]]; n     } n     X.swap(newX), Y.swap(newY), C.swap(newC); n   } n   init(1, 1, NX - 2, xs); n   int ans = 0; n   for (int yr = 1; yr + 1 < NY; ++yr) { n     vector<vector<int>> addAt(NX); n     for (int i = 0; i < N; ++i) { n       if (Y[i] <= ys[yr]) { n         int xi = lower_bound(xs.begin(), xs.end(), X[i]) - xs.begin(); n         addAt[xi].emplace_back(C[i]); n       } n     } n     int bad = K; n     vector<int> cnts(K); n     auto inc = [&](int z) { n       if (++cnts[z] == 1) --bad; n     }; n     auto dec = [&](int z) { n       if (--cnts[z] == 0) ++bad; n     }; n     vector<int> go(NX); n     int ptr = 0; n     for (int i = 1; i + 1 < NX; ++i) { n       while (bad && ptr + 2 < NX) { n         ptr++; n         for (int z : addAt[ptr]) inc(z); n       } n       if (bad) n         go[i] = L; n       else n         go[i] = xs[ptr]; n       for (int z : addAt[i]) dec(z); n     } n     reset(1, 1, NX - 2, go); n     vector<int> prv(N); n     vector<int> nxt(N); n     vector<map<int, int>> mp(K); n     for (int i = 0; i < N; ++i) { n       if (Y[i] <= ys[yr]) { n         auto it = mp[C[i]].lower_bound(X[i]); n         if (it == mp[C[i]].end()) { n           nxt[i] = -1; n         } else { n           nxt[i] = it->second; n         } n         it = mp[C[i]].upper_bound(X[i]); n         if (it == mp[C[i]].begin()) { n           prv[i] = -1; n         } else { n           prv[i] = prev(it)->second; n         } n         mp[C[i]][X[i]] = i; n       } n     } n     auto remove = [&](int i) { n       int xprv = (prv[i] == -1 ? -1 : X[prv[i]]); n       int xcur = X[i]; n       int xnxt = (nxt[i] == -1 ? L : X[nxt[i]]); n       int l = lower_bound(xs.begin(), xs.end(), xprv) - xs.begin() + 1; n       int r = walk(1, 1, NX - 2, xnxt); n       if (r == -1) r = NX - 1; n       --r; n       r = min(r, int(lower_bound(xs.begin(), xs.end(), xcur) - xs.begin())); n       if (l <= r) modify(1, 1, NX - 2, l, r, xnxt); n     }; n     ptr = N - 1; n     for (int yl = 1; yl <= yr; ++yl) { n       ll add = sum[1] % MOD * (ys[yr + 1] - ys[yr]) % MOD * n                (ys[yl] - ys[yl - 1]) % MOD; n       ans = (ans + add) % MOD; n       while (ptr >= 0 && Y[ptr] == ys[yl]) remove(ptr--); n     } n     assert(sum[1] == 0); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, next = 0; n   scanf( %d , &n); n   int a[n], b[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &a[i], &b[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (a[j] == b[i] && j != i) { n         next++; n         a[j] = 0; n       } n     } n   } n   printf( %d , n - next); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n int main() { n   int t, n, x; n   cin >> n; n   vector<pair<int, int>> arr; n   vector<int> arr2(n + 2, 0); n   set<int, greater<int>> st; n   for (int i = 1; i <= n; ++i) { n     cin >> x; n     arr2[x] = 1; n     arr.push_back(make_pair(i, x)); n   } n   for (int i = 1; i < arr2.size() - 1; ++i) { n     if (arr2[i] == 0) { n       st.insert(i); n     } n   } n   sort(arr.begin(), arr.end(), sortbysec); n   for (int i = 0; i < arr.size(); ++i) { n     for (auto j = st.begin(); j != st.end(); ++j) { n       if (*j == arr[i].first) { n         if (i == 0) n           swap(arr[i].first, arr[i + 1].first); n         else n           swap(arr[i].first, arr[i - 1].first); n       } n       arr[i].second = *j; n       st.erase(j); n       break; n     } n   } n   sort(arr.begin(), arr.end()); n   for (int i = 0; i < arr.size(); ++i) { n     cout << arr[i].second <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, ans = 0, a, b; n   cin >> n; n   ans = 1; n   for (int i = sqrt(n); i >= 1; i--) { n     if (n % i == 0) { n       k = i; n       break; n     } n   } n   cout << k <<     << n / k << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a, count = 0; n     cin >> a; n     for (long long i = 0; i < 31; i++) { n       if (a & (1 << i)) { n         count++; n       } n     } n     long long c = powl(2, count); n     cout << c << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n const ll MAX = 1e18 + 1; n const int NN = 1e5 + 5; n const int mod = 1e9 + 7; n void solve() { n   string s1; n   cin >> s1; n   char vow[] = { a ,  e ,  o ,  u ,  i }; n   vector<int> ans; n   int i = 0; n   while (i < s1.size()) { n     set<char> s; n     for (int j = i; j < min(i + 3, (int)s1.size()); j++) { n       s.insert(s1[j]); n     } n     int cnt = 0; n     if (s.size() != 1) { n       for (auto ch : s) { n         for (int y = 0; y < 5; y++) { n           if (ch == vow[y]) cnt++; n         } n       } n       if (cnt == 0) { n         ans.push_back(i + 1); n         i++; n       } n     } n     i++; n   } n   int k = 0; n   for (int y = 0; y < s1.size(); y++) { n     cout << s1[y]; n     if (k < ans.size() && y == ans[k]) { n       cout <<    ; n       k++; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt = 1; n   while (tt--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int a, num, res; n   while (cin >> n) { n     num = 0; n     for (int i = 0; i < n; i++) { n       cin >> a; n       if (a % 2 == 0) { n         num++; n       } n       if (num % 2 == 1) { n         res = 1; n       } else { n         res = 2; n       } n       if (i == 0) { n         cout << res; n       } else { n         cout <<     << res; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Day12[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int prime100[] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37,  41, 43, n                   47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103}; n template <typename T> n inline bool isLeap(T y) { n   return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false); n } n template <typename T> n inline T BIGMOD(T A, T B, T M = 1000000007) { n   T ret = 1; n   while (B) { n     if (B & 1) ret = (ret * A) % M; n     A = (A * A) % M; n     B = B >> 1; n   } n   return ret; n } n template <typename T> n inline T BigMod(T A, T B, T M) { n   T ret = 1; n   while (B) { n     if (B & 1) ret = (ret * A) % M; n     A = (A * A) % M; n     B = B >> 1; n   } n   return ret; n } n long long int MySqrt(long long int n) { n   long long int p = sqrt(n); n   if ((p + 1) * (p + 1) <= n) n     return p + 1; n   else if (p * p <= n) n     return p; n   else n     return p - 1; n } n long long int MyPow(long long int x, long long int n) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return MyPow(x * x, n / 2); n   else n     return x * MyPow(x * x, ((n - 1) / 2)); n } n long long int modInverse(long long int n) { n   return BIGMOD(n, (long long int)1000000007 - 2) % 1000000007; n } n int a[105][105]; n int main() { n   int n, d; n   scanf( %d%d , &(n), &(d)); n   for (int i = 1; i <= d; i++) { n     for (int j = 1; j <= n; j++) { n       char ch; n       cin >> ch; n       a[i][j] = ch -  0 ; n     } n   } n   int mx = INT_MIN; n   int cnt = 0; n   for (int i = 1; i <= d; i++) { n     int present = 1; n     for (int j = 1; j <= n; j++) { n       present = present & a[i][j]; n     } n     if (!present) { n       cnt++; n     } else n       cnt = 0; n     mx = max(mx, cnt); n   } n   cout << mx <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n int n, m, W, H, a[N][N], b[N][N]; n long long s[N][N]; n int q[N]; n inline long long S(int x1, int y1, int x2, int y2) { n   return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]; n } n struct BIT { n   int c[N][N]; n   inline void add(int x1, int y1, int x2, int y2) { n     add(x2, y2); n     add(x1, y2); n     add(x2, y1); n     add(x1, y1); n   } n   inline void add(int x, int y) { n     for (int i = x + 1; i <= n + 1; i += i & -i) n       for (int j = y + 1; j <= m + 1; j += j & -j) c[i][j]++; n   } n   inline int qry(int x, int y) { n     int ret = 0; n     for (int i = x + 1; i; i -= i & -i) n       for (int j = y + 1; j; j -= j & -j) ret += c[i][j]; n     return ret; n   } n   inline int qry(int x1, int y1, int x2, int y2) { n     return qry(x2, y2) - qry(x1 - 1, y2) - qry(x2, y1 - 1) + n            qry(x1 - 1, y1 - 1); n   } n } bit; n int tot, X[N * N], Y[N * N]; n long long V[N * N]; n int main() { n   scanf( %d%d%d%d , &n, &m, &W, &H); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       scanf( %d , &a[i][j]), n           s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j]; n   for (int i = 1; i <= n; i++) { n     int h = 1, t = 0; n     for (int j = 1; j <= m; j++) { n       while (h <= t && j - q[h] >= H) h++; n       while (h <= t && a[i][j] <= a[i][q[t]]) t--; n       q[++t] = j; n       b[i][j] = a[i][q[h]]; n     } n   } n   for (int j = 1; j <= m; j++) { n     int h = 1, t = 0; n     for (int i = 1; i <= n; i++) { n       while (h <= t && i - q[h] >= W) h++; n       while (h <= t && b[i][j] <= b[q[t]][j]) t--; n       q[++t] = i; n       a[i][j] = b[q[h]][j]; n     } n   } n   vector<pair<long long, int> > v; n   for (int i = W; i <= n; i++) n     for (int j = H; j <= m; j++) n       v.push_back(pair<long long, int>( n           S(i - W + 1, j - H + 1, i, j) - 1ll * W * H * a[i][j], n           (i - 1) * m + j)); n   sort(v.begin(), v.end()); n   for (int i = 0, sz = v.size(); i < sz; i++) { n     int y = (v[i].second - 1) % m + 1, x = (v[i].second - y) / m + 1; n     if (bit.qry(x - W + 1, y - H + 1, x, y)) continue; n     tot++; n     X[tot] = x - W + 1; n     Y[tot] = y - H + 1; n     V[tot] = v[i].first; n     bit.add(x - W + 1, y - H + 1, x, y); n   } n   printf( %d n , tot); n   for (int i = 1; i <= tot; i++) printf( %d %d %lld n , X[i], Y[i], V[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500000; n template <typename T> n class graph { n  public: n   struct edge { n     int from; n     int to; n     T cost; n   }; n   vector<edge> edges; n   vector<vector<int>> g; n   int n; n   graph(int _n) : n(_n) { g.resize(n); } n   virtual int add(int from, int to, T cost) = 0; n }; n template <typename T> n class undigraph : public graph<T> { n  public: n   using graph<T>::edges; n   using graph<T>::g; n   using graph<T>::n; n   undigraph(int _n) : graph<T>(_n) {} n   int add(int from, int to, T cost = 1) { n     assert(0 <= from && from < n && 0 <= to && to < n); n     int id = (int)edges.size(); n     g[from].push_back(id); n     g[to].push_back(id); n     edges.push_back({from, to, cost}); n     return id; n   } n }; n template <typename T> n class cycle_finder { n  public: n   vector<int> was; n   vector<int> st; n   vector<vector<int>> cycles; n   const graph<T> &g; n   int bound_cnt; n   int bound_size; n   int total_size = 0; n   cycle_finder(graph<T> &_g, int _bound_cnt = 1 << 30, n                int _bound_size = 1 << 30) n       : g(_g), bound_cnt(_bound_cnt), bound_size(_bound_size) { n     was.assign(g.n, -1); n   } n   void dfs(int v, int pe) { n     if ((int)cycles.size() >= bound_cnt || total_size >= bound_size) { n       return; n     } n     was[v] = (int)st.size(); n     for (int id : g.g[v]) { n       if (id == pe) { n         continue; n       } n       auto &e = g.edges[id]; n       int to = e.from ^ e.to ^ v; n       if (was[to] >= 0) { n         vector<int> cycle(1, id); n         for (int j = was[to]; j < (int)st.size(); j++) { n           cycle.push_back(st[j]); n         } n         cycles.push_back(cycle); n         total_size += (int)cycle.size(); n         if ((int)cycles.size() >= bound_cnt || total_size >= bound_size) { n           return; n         } n         continue; n       } n       if (was[to] == -1) { n         st.push_back(id); n         dfs(to, id); n         st.pop_back(); n       } n     } n     was[v] = -2; n   }; n   void find_cycles() { n     for (int i = 0; i < g.n; i++) { n       if (was[i] == -1) { n         dfs(i, -1); n       } n     } n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   if (fopen( input.in ,  r )) { n     freopen( input.in ,  r , stdin); n     freopen( output.out ,  w , stdout); n   } n   int n, m; n   cin >> n >> m; n   undigraph<int> g(n); n   int dp[maxn]; n   int subtractI[maxn]; n   for (int i = 0; i < n; i++) dp[i] = 1; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     g.add(a, b, i + 1); n     subtractI[i] = -1; n   } n   cycle_finder<int> cycles(g); n   cycles.find_cycles(); n   for (auto &cycle : cycles.cycles) { n     int pos = (int)(min_element(cycle.begin(), cycle.end()) - cycle.begin()); n     rotate(cycle.begin(), cycle.begin() + pos, cycle.end()); n     pos = (int)(max_element(cycle.begin(), cycle.end()) - cycle.begin()); n     bool subCycle = true; n     for (int i = 0; i < pos - 1; i++) { n       int costA = g.edges[cycle[i]].cost; n       int costB = g.edges[cycle[i + 1]].cost; n       if (costA > costB) { n         subCycle = false; n         break; n       } n     } n     for (int i = pos; i < cycle.size() - 1; i++) { n       int costA = g.edges[cycle[i]].cost; n       int costB = g.edges[cycle[i + 1]].cost; n       if (costA < costB) { n         subCycle = false; n         break; n       } n     } n     if (subCycle) { n       subtractI[cycle[0]] = cycle[pos]; n     } n   } n   int hist[maxn]; n   for (int i = m - 1; i >= 0; i--) { n     int a = g.edges[i].from, b = g.edges[i].to; n     int sum = dp[a] + dp[b]; n     if (subtractI[i] != -1) sum -= hist[subtractI[i]]; n     dp[a] = sum; n     dp[b] = sum; n     hist[i] = sum; n   } n   for (int i = 0; i < n; i++) cout << dp[i] - 1 <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[3005][3005]; n long long zkw[3005 << 2], M; n inline long long query(long long l, long long r) { n   long long ret = 1234567891; n   for (l += M - 1, r += M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { n     if (~l & 1) ret = min(ret, zkw[l ^ 1]); n     if (r & 1) ret = min(ret, zkw[r ^ 1]); n   } n   return ret; n } n long long brr[3005][3005]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, m, a, b; n   cin >> n >> m >> a >> b; n   long long x, y, z; n   cin >> arr[1][1] >> x >> y >> z; n   long long px, py; n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= m; j++) { n       if (i != 1 || j != 1) arr[i][j] = (arr[px][py] * x + y) % z; n       px = i, py = j; n     } n   for (M = 1; M - 2 < n; M <<= 1) n     ; n   for (long long j = 1; j <= m; j++) { n     for (long long i = 1; i <= n; i++) zkw[i + M] = arr[i][j]; n     for (long long i = M - 1; i; i--) n       zkw[i] = min(zkw[i << 1], zkw[i << 1 | 1]); n     for (long long i = 1; i + a - 1 <= n; i++) brr[i][j] = query(i, i + a - 1); n   } n   long long ans = 0; n   for (M = 1; M - 2 < m; M <<= 1) n     ; n   for (long long i = 1; i + a - 1 <= n; i++) { n     for (long long j = 1; j <= m; j++) zkw[j + M] = brr[i][j]; n     for (long long j = M - 1; j; j--) n       zkw[j] = min(zkw[j << 1], zkw[j << 1 | 1]); n     for (long long r = b; r <= m; r++) { n       long long l = r - b + 1; n       ans += query(l, r); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a == 0 || b == 0) { n     return 0; n   } n   long long s; n   while (a != 0) { n     b = b % a; n     s = a; n     a = b; n     b = s; n   } n   return b; n } n int main() { n   long long a, b; n   cin >> a >> b; n   long long g = gcd(a, b); n   long long lcm = (a * b) / g; n   long long toa = 0; n   long long tob = 0; n   long long pos = 0; n   long long nexta = a; n   long long nextb = b; n   while (pos < lcm) { n     if (nexta == nextb) { n       if (a < b) { n         tob += (nextb - pos); n         pos = nextb; n         nextb += b; n       } else { n         toa += (nexta - pos); n         pos = nexta; n         nexta += a; n       } n     } else if (nexta < nextb) { n       toa += (nexta - pos); n       pos = nexta; n       nexta += a; n     } else { n       tob += (nextb - pos); n       pos = nextb; n       nextb += b; n     } n   } n   if (toa == tob) { n     cout <<  Equal  << endl; n   } else if (toa > tob) { n     cout <<  Dasha  << endl; n   } else { n     cout <<  Masha  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, mx = 0, my = (1 << 31) - 1, m2x = 0, m2y = (1 << 31) - 1, a, b; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &a, &b); n     mx = max(mx, a); n     my = min(my, b); n   } n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &a, &b); n     m2x = max(m2x, a); n     m2y = min(m2y, b); n   } n   int res = 0; n   if (m2x > my) res = max(res, m2x - my); n   if (mx > m2y) res = max(res, mx - m2y); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(long long tc) { n   long long n, m, k; n   cin >> n >> m >> k; n   vector<long long> v(n); n   for (long long i = (0), ed = (n); i < (ed); ++i) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   unsigned long long mx1 = v[n - 1], mx2 = v[n - 2]; n   if (0) n     cout <<  :>>     n          <<  mx1  n          <<   :   << mx1 <<   n , n         cout <<  :>>     n              <<  mx2  n              <<   :   << mx2 <<   n ; n   unsigned long long ans = mx2 * (m / (k + 1)); n   if (0) n     cout <<  :>>     n          <<  ans  n          <<   :   << ans <<   n ; n   m = m - (m / (k + 1)); n   ans += mx1 * m; n   cout << ans <<   n ; n } n int32_t main() { n   solve(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int get() { n   int f = 0, v = 0; n   char ch; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) break; n   if (ch ==  - ) n     f = 1; n   else n     v = ch -  0 ; n   while (isdigit(ch = getchar())) v = v * 10 + ch -  0 ; n   if (f) n     return -v; n   else n     return v; n } n const int maxn = 100005, inf = 314000000; n vector<int> rule[maxn], g[maxn], belong[maxn]; n struct data { n   int t, v; n   data() {} n   data(int _t, int _v) { t = _t, v = _v; } n   inline bool operator<(const data &b) const { return v > b.v; } n }; n priority_queue<data> Q; n int T[maxn], cnt[maxn], n, m, a[maxn], b[maxn], val[maxn], vis[maxn]; n void init() { n   m = get(), n = get(); n   memset(a, 60, sizeof(a)); n   for (int i = 1; i <= m; i++) { n     int x = get(), y = get(), t; n     g[x].push_back(i), rule[i].push_back(x); n     while (y--) n       if ((t = get()) < 0) n         val[i]++; n       else n         rule[i].push_back(t), belong[t].push_back(i), T[i]++; n     if (!T[i]) a[x] = min(a[x], val[i]); n     cnt[i] = val[i]; n   } n } n int dfs(int x) { n   if (vis[x]) return b[x]; n   vis[x] = 1; n   for (int i = 0; i < g[x].size(); i++) { n     int id = g[x][i], flag = 1, tot = cnt[id]; n     for (int j = 1; j < rule[id].size() && flag; j++) n       if (a[rule[id][j]] > inf) flag = 0; n     if (!flag) continue; n     flag = 0; n     for (int j = 1; j < rule[id].size(); j++) { n       int p = rule[id][j], v; n       if (vis[p] == 1) { n         flag = 1; n         break; n       } n       v = dfs(p); n       if (v == -2) { n         flag = 1; n         break; n       } n       tot = min(tot + v, inf); n     } n     if (flag) { n       b[x] = -2; n       break; n     } n     b[x] = max(b[x], tot); n   } n   vis[x] = 2; n   return b[x]; n } n void work() { n   for (int i = 1; i <= n; i++) n     if (a[i] < 1e9) Q.push(data(i, a[i])); n   while (!Q.empty()) { n     data tp = Q.top(); n     Q.pop(); n     int t = tp.t, v = tp.v; n     if (a[t] != v) continue; n     for (int i = 0; i < belong[t].size(); i++) { n       int p = belong[t][i], s = rule[p][0]; n       val[p] = min(val[p] + v, inf); n       T[p]--; n       if (!T[p] && a[s] > val[p]) Q.push(data(s, a[s] = val[p])); n     } n   } n   for (int i = 1; i <= n; i++) n     if (a[i] <= inf) dfs(i); n   for (int i = 1; i <= n; i++) n     if (a[i] > inf) n       puts( -1 -1 ); n     else n       printf( %d %d n , a[i], b[i]); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Set(int N, int pos) { return N |= (1LL << pos); } n int Reset(int N, int pos) { return N &= ~(1LL << pos); } n bool Check(int N, int pos) { return N & (1LL << pos); } n int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; n inline void cn(long &n) { n   n = 0; n   long ch = getchar(); n   int sign = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) sign = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     n = (n << 3) + (n << 1) + ch -  0 , ch = getchar(); n   n = n * sign; n } n template <class T> n void cmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n void cmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n int len(const T &c) { n   return (int)c.size(); n } n template <class T> n int len(char c[]) { n   return (int)strlen(c); n } n string itos(long n) { n   string s; n   while (n) { n     s += (n % 10 + 48); n     n /= 10; n   } n   reverse(s.begin(), s.end()); n   return s; n } n long stoi(string s) { n   long n = 0; n   for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48); n   return n; n } n int ar[100005][3]; n int main() { n   string s, second; n   int q, l, r, x, y, z, m; n   cin >> s; n   for (int i(0), _n(len(s)); i < _n; ++i) { n     ar[i + 1][0] = ar[i][0]; n     ar[i + 1][1] = ar[i][1]; n     ar[i + 1][2] = ar[i][2]; n     ar[i + 1][s[i] -  x ]++; n   } n   cin >> q; n   for (int i(0), _n(q); i < _n; ++i) { n     cin >> l >> r; n     x = ar[r][0] - ar[l - 1][0]; n     y = ar[r][1] - ar[l - 1][1]; n     z = ar[r][2] - ar[l - 1][2]; n     int sr[3]; n     sr[0] = x; n     sr[1] = y; n     sr[2] = z; n     sort(sr, sr + 3); n     if (sr[2] - sr[0] <= 1 || (r - l) < 2) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void out() { n   cout.flush(); n   cerr << endl <<  OUT  << endl; n   int bred; n   cin >> bred; n   exit(0); n } n void updMax(int& x, int y) { n   if (y > x) x = y; n } n int sum10(int x, int y) { n   x += y; n   if (x >= 10) x -= 10; n   return x; n } n int cs[10]; n int n, m; n string a, bs[1024]; n int dp[1024][1024], sorted[1024][1024], h[10], whereGo[1024][10], res[1024][10], n     curc[1024]; n int getCipher(const string& s, int ind) { n   if (ind >= s.length()) return 0; n   return s[ind] -  0 ; n } n void buildSorted() { n   for (int j = 0; j < n; ++j) sorted[0][j] = j; n   for (int i = 0; i < 1002; ++i) { n     memset(h, 0, sizeof(h)); n     for (int j = 0; j < n; ++j) h[getCipher(bs[j], i)]++; n     for (int j = 1; j < 10; ++j) h[j] += h[j - 1]; n     for (int j = n - 1; j >= 0; --j) { n       int v = sorted[i][j]; n       sorted[i + 1][--h[getCipher(bs[v], i)]] = v; n     } n   } n } n int ps[1024], qs[1024], revps[1024], revqs[1024]; n void calcWhereGo(int pos) { n   for (int i = 0; i < n; ++i) { n     ps[i] = sorted[pos][i]; n     qs[i] = sorted[pos + 1][i]; n     revps[ps[i]] = i; n     revqs[qs[i]] = i; n   } n   for (int c = 0; c <= 9; ++c) { n     int curper = n; n     for (int i = 0; i < n; ++i) n       if (getCipher(bs[ps[i]], pos) + c >= 10) n         curper = min(curper, revqs[ps[i]]); n     whereGo[n][c] = curper; n     for (int i = n - 1; i >= 0; --i) { n       if (getCipher(bs[ps[i]], pos) + c + 1 >= 10) n         curper = min(curper, revqs[ps[i]]); n       whereGo[i][c] = curper; n     } n   } n } n void calcRes(int pos) { n   for (int c = 0; c <= 9; ++c) { n     int sum = 0; n     for (int i = 0; i < n; ++i) { n       curc[i] = sum10(getCipher(bs[sorted[pos][i]], pos), c); n       if (curc[i] == 0 && pos >= bs[sorted[pos][i]].length()) n         curc[i] = 0; n       else n         curc[i] = cs[curc[i]]; n       sum += curc[i]; n     } n     res[n][c] = sum; n     int cc = sum10(c, 1); n     for (int i = n - 1; i >= 0; --i) { n       sum -= curc[i]; n       curc[i] = sum10(getCipher(bs[sorted[pos][i]], pos), cc); n       sum += cs[curc[i]]; n       res[i][c] = sum; n     } n   } n } n void calcDynamics() { n   for (int i = 0; i <= 1001; ++i) n     for (int j = 0; j <= n; ++j) dp[i][j] = -1000000000; n   dp[0][n] = 0; n   for (int i = 0; i <= 1000; ++i) { n     calcWhereGo(i); n     calcRes(i); n     int l, r; n     if (i >= m) n       l = r = 0; n     else if (a[i] ==  ? ) { n       r = 9; n       l = (i == m - 1); n     } else n       l = r = a[i] -  0 ; n     for (int j = 0; j <= n; ++j) n       for (int c = l; c <= r; ++c) n         updMax(dp[i + 1][whereGo[j][c]], dp[i][j] + res[j][c]); n   } n } n void solve() { n   cin >> a >> n; n   reverse(a.begin(), a.end()); n   m = a.length(); n   for (int i = 0; i < n; ++i) { n     cin >> bs[i]; n     reverse(bs[i].begin(), bs[i].end()); n     while (bs[i].length() < m) bs[i].push_back( 0 ); n   } n   for (int i = 0; i < 10; ++i) cin >> cs[i]; n   buildSorted(); n   calcDynamics(); n   int ans = 0; n   for (int i = 0; i <= n; ++i) ans = max(ans, dp[1001][i]); n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   out(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int mof = 1000000007; n const int inf = 1000000010; n const long long llinf = 1000000007000000007; n const double pi = 3.14159265359; n int a[211111]; n int a2[211111]; n int t[211111]; n int t2[211111]; n int n, k, A, B, q; n int sum(int r) { n   int result = 0; n   for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r]; n   return result; n } n void inc(int i, int delta) { n   if (a[i] + delta > B) { n     delta = B - a[i]; n     a[i] = B; n   } else { n     a[i] = a[i] + delta; n   } n   for (; i < n; i = (i | (i + 1))) t[i] += delta; n } n int sum(int l, int r) { return sum(r) - sum(l - 1); } n void init(int a[]) { n   for (unsigned i = 0; i < n; i++) inc(i, a[i]); n } n int sum2(int r) { n   int result = 0; n   for (; r >= 0; r = (r & (r + 1)) - 1) result += t2[r]; n   return result; n } n void inc2(int i, int delta) { n   if (a2[i] + delta > A) { n     delta = A - a2[i]; n     a2[i] = A; n   } else { n     a2[i] = a2[i] + delta; n   } n   for (; i < n; i = (i | (i + 1))) t2[i] += delta; n } n int sum2(int l, int r) { return sum2(r) - sum2(l - 1); } n void init2(int a[]) { n   for (unsigned i = 0; i < n; i++) inc2(i, a[i]); n } n int main() { n   scanf( %d%d%d%d%d , &n, &k, &A, &B, &q); n   while (q--) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int d, a; n       scanf( %d%d , &d, &a); n       inc(d - 1, a); n       inc2(d - 1, a); n     } else { n       int p; n       scanf( %d , &p); n       printf( %d n , sum(0, p - 2) + sum2(p + k - 1, n - 1)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 210; n const double eps = 1e-9; n int n; n int lsh[maxn * 2], ln, K; n map<int, int> mt; n int To[maxn]; n int li[maxn], ri[maxn]; n double f[2][maxn][maxn]; n double ans[maxn][maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     scanf( %d%d , &li[i], &ri[i]), lsh[++ln] = li[i], lsh[++ln] = ri[i]; n   sort(lsh + 1, lsh + ln + 1); n   lsh[0] = lsh[1] - 1; n   K = 0; n   for (int i = 1; i <= ln; i++) n     if (lsh[i] != lsh[i - 1]) To[mt[lsh[i]] = ++K] = lsh[i]; n   for (int i = 1; i <= n; i++) li[i] = mt[li[i]], ri[i] = mt[ri[i]]; n   for (int i = 1; i <= n; i++) { n     for (int x = li[i]; x < ri[i]; x++) { n       double inow = (double)(To[x + 1] - To[x]) / (To[ri[i]] - To[li[i]]); n       for (int x1 = 0; x1 <= n; x1++) n         for (int x2 = 0; x2 <= n; x2++) f[0][x1][x2] = f[1][x1][x2] = 0; n       int now = 1; n       f[now][0][0] = 1.0; n       for (int j = 1; j <= n; j++) { n         if (i == j) continue; n         int L = To[li[j]], R = To[ri[j]]; n         now = !now; n         if (x < li[j]) { n           for (int x1 = 0; x1 < j; x1++) n             for (int x2 = 0; x2 < j - x1; x2++) n               if (f[!now][x1][x2] > eps) { n                 double tmp = f[!now][x1][x2]; n                 f[!now][x1][x2] = 0; n                 f[now][x1][x2] += tmp; n               } n         } else if (ri[j] <= x) { n           for (int x1 = 0; x1 < j; x1++) n             for (int x2 = 0; x2 < j - x1; x2++) n               if (f[!now][x1][x2] > eps) { n                 double tmp = f[!now][x1][x2]; n                 f[!now][x1][x2] = 0; n                 f[now][x1 + 1][x2] += tmp; n               } n         } else { n           double p1 = (double)(To[x] - L) / (R - L), n                  p2 = (double)(To[x + 1] - To[x]) / (R - L), n                  p3 = (double)(R - To[x + 1]) / (R - L); n           for (int x1 = 0; x1 < j; x1++) n             for (int x2 = 0; x2 < j - x1; x2++) n               if (f[!now][x1][x2] > eps) { n                 double tmp = f[!now][x1][x2]; n                 f[!now][x1][x2] = 0; n                 f[now][x1 + 1][x2] += tmp * p1; n                 f[now][x1][x2 + 1] += tmp * p2; n                 f[now][x1][x2] += tmp * p3; n               } n         } n       } n       for (int x1 = 0; x1 < n; x1++) n         for (int x2 = 0; x2 < n - x1; x2++) n           if (f[now][x1][x2] > eps) { n             double tmp = f[now][x1][x2], temp = 1.0 / (x2 + 1.0); n             for (int l = 0; l <= x2; l++) n               ans[i][x1 + l + 1] += inow * tmp * temp; n           } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j < n; j++) printf( %.10lf  , ans[i][j]); n     printf( %.10lf n , ans[i][n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int ans[1000010], p[1000010], mem[1000010], q[1000010]; n int main() { n   int n, t, i, j; n   memset(q, 0, sizeof(q)); n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &p[i]); n   scanf( %d , &t); n   for (i = 0; i < t; i++) { n     scanf( %d , &j); n     q[j - 1] = 1; n   } n   int cnt = 0, num = n - 1; n   if (n % 2 == 1) { n     printf( NO n ); n     return 0; n   } n   for (i = n - 1; i >= 0; i--) { n     if (!cnt || q[i] == 1 || mem[cnt] != p[i]) { n       mem[++cnt] = p[i]; n       ans[i] = -p[i]; n     } else { n       ans[i] = p[i]; n       cnt--; n     } n   } n   if (cnt != 0) n     printf( NO n ); n   else { n     printf( YES n ); n     for (i = 0; i < n - 1; i++) printf( %d  , ans[i]); n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, k; n char s[4000]; n int opx[4000]; n int opy[4000]; n int count2; n void Reverse(char *s, int a, int b) { n   for (int i = a, j = b; i < j; i++, j--) { n     char c = s[i]; n     s[i] = s[j]; n     s[j] = c; n   } n } n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> k; n     cin >> s; n     int count1 = 0; n     while (count1 < 2 * k - 3) { n       if (s[count1] ==  ( ) { n         for (int i = count1 + 1; i < n; i++) { n           if (s[i] ==  ) ) { n             count2++; n             opx[count2] = count1 + 2; n             opy[count2] = i + 1; n             Reverse(s, count1 + 1, i); n             break; n           } n         } n       } else { n         for (int i = count1 + 1; i < n; i++) { n           if (s[i] ==  ( ) { n             count2++; n             opx[count2] = count1 + 1; n             opy[count2] = i + 1; n             Reverse(s, count1, i); n             break; n           } n         } n       } n       count1 += 2; n     } n     while (count1 < n && s[count1] ==  ( ) count1++; n     while (count1 < n) { n       int count3 = count1; n       while (count3 < n && s[count3] ==  ) ) count3++; n       if (count3 < n) { n         count2++; n         opx[count2] = count1 + 1; n         opy[count2] = count3 + 1; n         Reverse(s, count1, count3); n         count1++; n       } else { n         break; n       } n     } n     cout << count2 << endl; n     for (int i = 1; i <= count2; i++) { n       cout << opx[i] <<     << opy[i] << endl; n     } n     count2 = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5004; n int main() { n   int a = 1, b = 1, n; n   string s; n   cin >> n; n   while (n--) { n     cin >> s; n     if (s ==  UL  || s ==  DR ) n       ++a; n     else if (s.size() == 2) n       ++b; n     else n       ++a, ++b; n   } n   printf( %lld n , 1ll * a * b); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k = 8; n   int t = 4; n   int p = 2; n   int s = 6; n   int n; n   while (cin >> n) { n     if (n == 0) n       cout << 1 << endl; n     else if (n % 4 == 1) n       cout << k << endl; n     else if (n % 4 == 2) n       cout << t << endl; n     else if (n % 4 == 3) n       cout << p << endl; n     else if (n % 4 == 0) n       cout << s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = (int)(1e5) + 10; n int n, m, a, b; n vector<pair<int, pair<int, int> > > ed; n pair<int, int> res[NMAX]; n int last; n int now[NMAX]; n bool func(pair<int, pair<int, int> > i, pair<int, pair<int, int> > j) { n   if (i.first == j.first) n     return (i.second.first > j.second.first); n   else n     return (i.first < j.first); n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &a, &b); n     ed.push_back(make_pair(a, make_pair(b, i))); n   } n   sort(ed.begin(), ed.end(), func); n   int i = 1; n   last = 1; n   for (int kk = 0; kk <= (int)ed.size() - 1; kk++) { n     int val = ed[kk].first; n     int type = ed[kk].second.first; n     int vt = ed[kk].second.second; n     if (type == 1) { n       res[vt] = make_pair(last, 1 + last); n       last++; n       now[last] = last; n     } else { n       bool co = 0; n       while (!co) { n         for (int j = min(i - 2, now[i] - 1); j >= 1; j--) { n           res[vt] = make_pair(j, i); n           co = 1; n           now[i] = j; n           break; n         } n         if (!co) i++; n         if (i > last) { n           cout << -1; n           return 0; n         } n       } n     } n   } n   if (last != n) { n     cout << -1; n     return 0; n   } n   for (int i = 1; i <= m; i++) n     cout << res[i].first <<     << res[i].second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int st[610], aim[(300010)], nxt[(300010)], ln; n int flow[(300010)], con[(300010)], cost[(300010)]; n int inq[610], dis[610]; n int g[610], e[610]; n int q[610]; n int a[610]; n int d[610 << 3], tag[610 << 3], dn; n int num[610]; n int N, source, remit; n int ans; n int n, m; n void in_edge(int x, int y, int f, int c) { n   aim[ln] = y; n   nxt[ln] = st[x]; n   flow[ln] = 0; n   con[ln] = f; n   cost[ln] = c; n   st[x] = ln++; n   aim[ln] = x; n   nxt[ln] = st[y]; n   flow[ln] = con[ln] = 0; n   cost[ln] = -c; n   st[y] = ln++; n } n int calc(int x) { n   int ret = 0; n   while (x) ret += (x & 1), x >>= 1; n   return ret; n } n int spfa() { n   int qn = 1; n   for (int i = 0; i <= remit; i++) dis[i] = (10000000); n   memset(inq, 0, sizeof(inq)); n   dis[source] = 0; n   inq[source] = 1; n   q[0] = source; n   for (int p = 0; p != qn;) { n     int u = q[p]; n     p++; n     if (p == 610) p = 0; n     inq[u] = 0; n     for (int i = st[u]; i != -1; i = nxt[i]) n       if (flow[i] < con[i]) { n         int v = aim[i]; n         if (dis[u] + cost[i] < dis[v]) { n           dis[v] = dis[u] + cost[i]; n           g[v] = u; n           e[v] = i; n           if (!inq[v]) { n             inq[v] = 1; n             q[qn] = v; n             ++qn; n             if (qn == 610) qn = 0; n           } n         } n       } n   } n   if (dis[remit] == (10000000)) return 0; n   int delta = (10000000); n   for (int i = remit; i != source; i = g[i]) n     delta = min(delta, con[e[i]] - flow[e[i]]); n   for (int i = remit; i != source; i = g[i]) { n     int x = e[i]; n     flow[x] += delta; n     flow[x ^ 1] -= delta; n   } n   ans += delta * dis[remit]; n   return 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   source = n + n + m; n   remit = source + 1; n   memset(st, -1, sizeof(st)); n   ln = 0; n   for (int i = 0; i < m; i++) in_edge(source, (n << 1) + i, 1, 0); n   for (int i = 0; i < n; i++) { n     in_edge(source, n + i, 1, 0); n     in_edge(i, remit, 1, 0); n   } n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) in_edge((n << 1) + i, j, 1, calc(a[j])); n   } n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) n       if (a[i] == a[j]) n         in_edge(n + i, j, 1, 0); n       else n         in_edge(n + i, j, 1, calc(a[j])); n   ans = 0; n   while (spfa()) n     ; n   int dn = 0; n   for (int i = 0; i < n; i++) { n     int flag = 0; n     for (int j = st[i]; j != -1; j = nxt[j]) n       if (flow[j] < con[j]) { n         int v = aim[j]; n         if (v >= (n << 1) && v < source) { n           tag[dn] = 0; n           d[dn++] = i; n           tag[dn] = 1; n           d[dn++] = v - (n << 1); n           num[i] = v - (n << 1); n         } else if (v >= n && v <= (n << 1)) { n           int p = v - n; n           if (a[p] != a[i]) { n             tag[dn] = 0; n             d[dn++] = i; n           } n           tag[dn] = 1; n           d[dn++] = num[p]; n           num[i] = num[p]; n         } n       } n   } n   printf( %d %d n , dn, ans); n   for (int i = 0; i < dn; i++) n     if (!tag[i]) { n       printf( %c=%d n ,  a  + num[d[i]], a[d[i]]); n     } else { n       printf( print(%c) n ,  a  + d[i]); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool poate[5020][510]; n pair<int, int> from[5020][510]; n int last[5020][510]; n queue<pair<int, int>> de_calc; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int d, s; n   cin >> d >> s; n   for (int i = 1; i <= 9; i++) { n     poate[i][i % d] = 1; n     from[i][i % d] = {-1, -1}; n     last[i][i % d] = i; n     de_calc.push({i, i % d}); n   } n   bool found = 0; n   while (!de_calc.empty() && !found) { n     int i = de_calc.front().first, j = de_calc.front().second; n     de_calc.pop(); n     if (i > s) continue; n     for (int cif = 0; cif <= 9; cif++) { n       int j2 = (10 * j + cif) % d, i2 = i + cif; n       if (poate[i2][j2]) continue; n       poate[i2][j2] = 1; n       from[i2][j2] = {i, j}; n       last[i2][j2] = cif; n       de_calc.push({i2, j2}); n       if (i2 == s && j2 == 0) { n         found = 1; n         break; n       } n     } n   } n   if (!poate[s][0]) { n     cout <<  -1 n ; n     return 0; n   } n   string ans =   ; n   for (int i = s, j = 0;;) { n     ans += last[i][j] +  0 ; n     if (from[i][j].first == -1) break; n     int j2 = from[i][j].second; n     i = from[i][j].first; n     j = j2; n   } n   reverse(ans.begin(), ans.end()); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 10; n signed main() { n   ios::sync_with_stdio(false); n   long long n, a, b; n   cin >> n >> a >> b; n   long long ans = 0; n   vector<long long> x(n), y(n); n   for (long long i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     for (long long j = 0; j < i; j++) { n       for (long long _ = 2; _--;) { n         for (long long __ = 2; __--;) { n           for (long long ___ = 2; ___--;) { n             if (x[i] + x[j] <= a && max(y[i], y[j]) <= b) { n               ans = max(ans, x[i] * y[i] + x[j] * y[j]); n             } n             swap(x[j], y[j]); n           } n           swap(x[i], y[i]); n         } n         swap(a, b); n       } n     } n   } n   cout << ans <<   n ; 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   cout.tie(0); n   int n; n   cin >> n; n   if (n & (n - 1)) { n     cout <<  NO  <<   n ; n   } else { n     cout <<  YES  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int ttt; n   cin >> ttt; n   while (ttt--) { n     int n, m; n     cin >> n >> m; n     vector<pair<int, int>> v(n * m); n     for (int i = 0; i < n * m; i++) { n       cin >> v[i].first; n       v[i].second = i; n     } n     sort((v).begin(), (v).end()); n     for (int i = 0; i < n * m; i++) { n       v[i].second *= -1; n     } n     int ans = 0; n     for (int i = 0; i < n; i++) { n       sort(v.begin() + i * m, v.begin() + (i + 1) * (m)); n       for (int l = 0; l < m; l++) { n         for (int j = 0; j < l; j++) { n           if (v[i * m + l].second < v[i * m + j].second) ans++; n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0; n   bool flg = false; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) flg = true; n   for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); n   return flg ? -x : x; n } n int n, m; n char a[64][64]; n void solve() { n   n = read(), m = read(); n   for (int i(1), _i(n); i <= _i; i++) scanf( %s , a[i] + 1); n   int tot = 0; n   for (int i(1), _i(n); i <= _i; i++) n     for (int j(1), _j(m); j <= _j; j++) tot += a[i][j] ==  A ; n   if (!tot) return puts( MORTAL ), void(); n   if (tot == n * m) return puts( 0 ), void(); n   { n     bool g = 1; n     for (int i(1), _i(n); i <= _i; i++) g = g && a[i][1] ==  A ; n     if (g) return puts( 1 ), void(); n     g = 1; n     for (int i(1), _i(n); i <= _i; i++) g = g && a[i][m] ==  A ; n     if (g) return puts( 1 ), void(); n     g = 1; n     for (int i(1), _i(m); i <= _i; i++) g = g && a[1][i] ==  A ; n     if (g) return puts( 1 ), void(); n     g = 1; n     for (int i(1), _i(m); i <= _i; i++) g = g && a[n][i] ==  A ; n     if (g) return puts( 1 ), void(); n   } n   { n     for (int x : {1, n}) n       for (int y : {1, m}) n         if (a[x][y] ==  A ) return puts( 2 ), void(); n     for (int i(1), _i(n); i <= _i; i++) { n       bool g = 1; n       for (int j(1), _j(m); j <= _j; j++) g = g && a[i][j] ==  A ; n       if (g) return puts( 2 ), void(); n     } n     for (int j(1), _j(m); j <= _j; j++) { n       bool g = 1; n       for (int i(1), _i(n); i <= _i; i++) g = g && a[i][j] ==  A ; n       if (g) return puts( 2 ), void(); n     } n   } n   { n     for (int i(1), _i(n); i <= _i; i++) n       for (int j(1), _j(m); j <= _j; j++) { n         if (a[i][j] ==  A  && (i == 1 || i == n || j == 1 || j == m)) n           return puts( 3 ), void(); n       } n   } n   puts( 4 ); n } n int main() { n   for (int T = read(); T--;) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct per { n   string name; n   int score; n } store[55]; n bool cmp(per a, per b) { return a.score > b.score; } n int main() { n   int n, tmp; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> store[i].name; n     cin >> tmp; n     store[i].score = 0; n     store[i].score += tmp * 100; n     cin >> tmp; n     store[i].score -= tmp * 50; n     for (int j = 0; j < 5; j++) { n       cin >> tmp; n       store[i].score += tmp; n     } n   } n   sort(store, store + n, cmp); n   cout << store[0].name << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int f[50005][65], a[50005], po[50005], cnt, tr[65][200005], la[65][200005]; n void pushup(int num, int id) { n   tr[num][id] = max(tr[num][id << 1], tr[num][id << 1 | 1]); n } n void pushdo(int num, int id) { n   if (la[num][id] != 0) { n     la[num][id << 1] += la[num][id]; n     la[num][id << 1 | 1] += la[num][id]; n     tr[num][id << 1] += la[num][id]; n     tr[num][id << 1 | 1] += la[num][id]; n     la[num][id] = 0; n   } n } n void upda(int num, int id, int l, int r, int x, int y, int p) { n   if (x <= l && r <= y) { n     tr[num][id] += p; n     la[num][id] += p; n     return; n   } n   pushdo(num, id); n   int m = (l + r) >> 1; n   if (x <= m) upda(num, id << 1, l, m, x, y, p); n   if (y > m) upda(num, id << 1 | 1, m + 1, r, x, y, p); n   pushup(num, id); n } n int query(int num, int id, int l, int r, int x, int y) { n   if (x <= l && r <= y) return tr[num][id]; n   pushdo(num, id); n   int m = (l + r) >> 1, ans = 0; n   if (x <= m) ans = max(ans, query(num, id << 1, l, m, x, y)); n   if (y > m) ans = max(ans, query(num, id << 1 | 1, m + 1, r, x, y)); n   return ans; n } n inline int min(int x, int y) { n   if (x < y) return x; n   return y; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= k; i++) upda(0, 1, 0, n, i, i, -2e9); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= min(i, k); j++) { n       upda(j, 1, 0, n, po[a[i]], i - 1, 1); n     } n     po[a[i]] = i; n     for (int j = 1; j <= min(i, k); j++) { n       f[i][j] = query(j - 1, 1, 0, n, 0, i - 1); n       upda(j, 1, 0, n, i, i, f[i][j]); n     } n   } n   printf( %d n , f[n][k]); 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     int a[n], b[n], o = 0, z = 0, sorted = 1; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (i > 0 && a[i] < a[i - 1]) sorted = 0; n     } n     for (int i = 0; i < n; i++) { n       cin >> b[i]; n       if (b[i]) n         o++; n       else n         z++; n     } n     if ((z && o) || (sorted)) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(const vector<vector<int> >& edge_list, vector<bool>& visited, int i, n          int& edges) { n   visited[i] = true; n   for (int j : edge_list[i]) { n     if (!visited[j]) dfs(edge_list, visited, j, edges); n     if (j >= i) ++edges; n   } n } n int main() { n   int n, m, u, v; n   scanf( %d %d , &n, &m); n   vector<int> deg_noloop(n, 0); n   vector<vector<int> > edge_list(n); n   vector<bool> visited(n, false); n   long long nonloops = 0, loops = 0, good_paths = 0; n   int start = 0, edges = 0; n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &u, &v); n     --u; n     --v; n     start = u; n     if (u != v) { n       edge_list[u].push_back(v); n       edge_list[v].push_back(u); n       ++deg_noloop[u]; n       ++deg_noloop[v]; n       ++nonloops; n     } else { n       edge_list[u].push_back(v); n       ++loops; n     } n   } n   dfs(edge_list, visited, start, edges); n   for (int i = 0; i < n; i++) { n     long long deg = deg_noloop[i]; n     if (deg >= 2) good_paths += (deg) * (deg - 1) / 2; n   } n   good_paths += loops * nonloops + (loops) * (loops - 1) / 2; n   if (edges < m) good_paths = 0; n   printf( %lld n , good_paths); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100111; n long long n, arr[N], x, k; n int main() { n   cin >> n; n   while (n--) { n     cin >> k >> x; n     long long u = 9 * k; n     cout << u - (9 - x) << endl; n   } n } n


#include <bits/stdc++.h> n int main() { n   int q; n   std::cin >> q; n   std::vector<int> V; n   for (int i = 0; i < q; ++i) { n     int n; n     std::cin >> n; n     std::vector<int> v1; n     std::vector<int> v2(n); n     for (int j = 0; j < n; ++j) { n       std::cin >> v2[j]; n       v1.push_back(j + 1); n     } n     for (int k = 0; k < v1.size(); ++k) { n       int count = 1; n       int num = k; n       while (v1[k] != v2[num]) { n         num = v2[num] - 1; n         ++count; n       } n       V.push_back(count); n     } n     V.push_back(0); n   } n   for (int i = 0; i < V.size(); ++i) { n     if (V[i] == 0) n       std::cout <<   n ; n     else { n       std::cout << V[i] <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5001; n int n, tmp[N]; n vector<int> g[N]; n bool vst[N]; n void dfs(int c, int p) { n   bool dp[N]; n   fill(dp, dp + n + 1, false); n   dp[0] = true; n   tmp[c] = 1; n   for (int ch : g[c]) { n     if (ch != p) { n       dfs(ch, c); n       tmp[c] += tmp[ch]; n       for (int i = n - 1; i >= 0; --i) { n         if (dp[i]) { n           dp[i + tmp[ch]] = true; n         } n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     dp[n - i - 1] |= dp[i]; n   } n   for (int i = 1; i <= n; ++i) { n     vst[i] |= dp[i]; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = (1), _i = (n - 1); i <= _i; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1, 1); n   int cnt = 0; n   for (int i = (1), _i = (n - 1 - 1); i <= _i; ++i) { n     if (vst[i]) { n       cnt += 1; n     } n   } n   printf( %d n , cnt); n   for (int i = (1), _i = (n - 1 - 1); i <= _i; ++i) { n     if (vst[i]) { n       printf( %d %d n , i, n - i - 1); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const unsigned long long base = 311; n unsigned long long TH[maxn][3], L[3], P[maxn]; n int Sol, lona, lonb; n void calc_Hash(string& a, int id) { n   int lon = L[id]; n   for (int i = (1); i <= (lon); i += (1)) { n     TH[i][id] = TH[i - 1][id] * base + (unsigned long long)(a[i] -  0 ); n   } n   return; n } n void calc_pot(int N) { n   P[0] = 1; n   for (int i = (1); i <= (N); i += (1)) { n     P[i] = P[i - 1] * base; n   } n   return; n } n bool completa(int tam, int id) { n   int lon = L[id]; n   unsigned long long H = TH[tam][1]; n   bool flag = true; n   for (int i = (tam); i <= (lon); i += (tam)) { n     if (TH[i][id] - TH[i - tam][id] * P[tam] != H) { n       flag = false; n       break; n     } n   } n   return flag; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string A, B; n   cin >> A >> B; n   lona = A.size(); n   lonb = B.size(); n   L[1] = lona; n   L[2] = lonb; n   A =     + A; n   B =     + B; n   calc_pot(((lona) > (lonb) ? (lona) : (lonb))); n   calc_Hash(A, 1); n   calc_Hash(B, 2); n   int l = ((lona) < (lonb) ? (lona) : (lonb)); n   for (int i = (1); i <= (l); i += (1)) { n     if (!(lona % i) && !(lonb % i)) { n       if (completa(i, 1) && completa(i, 2)) { n         Sol++; n       } n     } n   } n   cout << Sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e6 + 5; n long long n, k, a[N], res; n void solo() { n   cin >> n; n   for (int i = 1; i <= n; i++) cout << i + 1 <<    ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     solo(); n     cout << endl; n   } n   cerr <<   n  << (double)clock() / CLOCKS_PER_SEC * 1000 <<   ms ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n vector<string> vec[1003], v; n bool mat[26][26], flag, ok[26], vis[26], run[26]; n int ind[26]; n inline bool func(int x, int y) { n   vector<int> vs, vt; n   for (int i = 0; i < v[x].size(); i++) vs.push_back(v[x][i]); n   for (int i = 0; i < v[y].size(); i++) vt.push_back(v[y][i]); n   reverse(vs.begin(), vs.end()); n   reverse(vt.begin(), vt.end()); n   while (!vs.empty() && !vt.empty()) { n     if (vt.back() != vs.back()) n       return mat[vs.back() -  a ][vt.back() -  a ] = 1; n     else n       vs.pop_back(), vt.pop_back(); n   } n   if (!vs.empty()) n     return 0; n   else n     return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   flag = 1; n   string str; n   int n, k, p; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> p; n     for (int j = 0; j < k; j++) { n       cin >> str, vec[p].emplace_back(str); n       for (int l = 0; l < str.size(); l++) ok[str[l] -  a ] = 1; n     } n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < k; j++) v.emplace_back(vec[i][j]); n   for (int i = 0; i < v.size() - 1 && flag; i++) flag = func(i, i + 1); n   for (int i = 0; i < 26; i++) n     for (int j = 0; j < 26; j++) ind[j] += mat[i][j]; n   int cnt = 0, tot = 0; n   for (int i = 0; i < 26; i++) tot += ok[i]; n   queue<int> q; n   for (int i = 0; i < 26; i++) n     if (ok[i] && !ind[i]) q.push(i); n   vector<int> res; n   while (!q.empty() && flag) { n     int x = q.front(); n     q.pop(); n     res.push_back(x); n     for (int i = 0; i < 26; i++) n       if (mat[x][i]) { n         ind[i]--; n         if (!ind[i]) q.push(i); n       } n   } n   if (flag && res.size() == tot) { n     for (int i = 0; i < res.size(); i++) cout << (char)(res[i] +  a ); n     cout <<   n ; n   } else n     cout <<  IMPOSSIBLE n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n1, n2, k1, k2; n long long aa[105][105][2]; n long long foo(int u, int v, int a) { n   if (u == 0 && v == 0) { n     return 1; n   } n   if (u < 0 || v < 0) { n     return 0; n   } n   if (aa[u][v][a] != -1) return (aa[u][v][a]); n   long long s = 0; n   if (a) { n     for (int i = 1; i <= k1; i++) { n       s += foo(u - i, v, !a) % 100000000; n     } n   } else { n     for (int i = 1; i <= k2; i++) { n       s += foo(u, v - i, !a) % 100000000; n     } n   } n   aa[u][v][a] = s; n   return s; n } n int main() { n   cin >> n1 >> n2 >> k1 >> k2; n   long long s; n   for (int i = 0; i <= 103; i++) { n     for (int j = 0; j <= 103; j++) { n       aa[i][j][0] = -1; n       aa[i][j][1] = -1; n     } n   } n   s = foo(n1, n2, 1); n   s += foo(n1, n2, 0); n   cout << s % 100000000; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n using pi = pair<int, int>; n const int MAXN = 1005; n const int MAXM = 1005; n struct edge_color { n   int deg[2][MAXN]; n   pi has[2][MAXN][MAXN]; n   int color[MAXM]; n   int c[2]; n   void clear(int n) { n     for (int t = 0; t < 2; t++) { n       for (int i = 0; i <= n; i++) { n         deg[t][i] = 0; n         for (int j = 0; j <= n; j++) { n           has[t][i][j] = pi(0, 0); n         } n       } n     } n   } n   void dfs(int x, int p) { n     auto i = has[p][x][c[!p]]; n     if (has[!p][i.first][c[p]].second) n       dfs(i.first, !p); n     else n       has[!p][i.first][c[!p]] = pi(0, 0); n     has[p][x][c[p]] = i; n     has[!p][i.first][c[p]] = pi(x, i.second); n     color[i.second] = c[p]; n   } n   int solve(vector<pi> v, vector<int> &cv) { n     int m = ((int)(v).size()); n     int ans = 0; n     for (int i = 1; i <= m; i++) { n       int x[2]; n       x[0] = v[i - 1].first; n       x[1] = v[i - 1].second; n       for (int d = 0; d < 2; d++) { n         deg[d][x[d]] += 1; n         ans = max(ans, deg[d][x[d]]); n         for (c[d] = 1; has[d][x[d]][c[d]].second; c[d]++) n           ; n       } n       if (c[0] != c[1]) dfs(x[1], 1); n       for (int d = 0; d < 2; d++) has[d][x[d]][c[0]] = pi(x[!d], i); n       color[i] = c[0]; n     } n     cv.resize(m); n     for (int i = 1; i <= m; i++) { n       cv[i - 1] = color[i]; n       color[i] = 0; n     } n     return ans; n   } n } EC; n int cnt[2][MAXN]; n int idx[2][MAXN], L, R; n int main() { n   int n, m, t; n   scanf( %d %d %d , &n, &m, &t); n   for (int i = 0; i < n; i++) scanf( %*d ); n   vector<pi> ans; n   for (int i = 0; i < m; i++) { n     int l, r; n     scanf( %d %d , &l, &r); n     if (cnt[0][l] == 0) idx[0][l] = ++L; n     if (cnt[1][r] == 0) idx[1][r] = ++R; n     cnt[0][l]++; n     cnt[1][r]++; n     if (cnt[0][l] == t) cnt[0][l] = 0; n     if (cnt[1][r] == t) cnt[1][r] = 0; n     ans.emplace_back(idx[0][l], idx[1][r]); n   } n   vector<int> color; n   EC.solve(ans, color); n   for (auto &i : color) printf( %d n , i); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string start, end; n   cin >> start >> end; n   string srev = start; n   reverse(srev.begin(), srev.end()); n   vector<int> vs[26], vr[26]; n   int slen = start.length(); n   for (int i = 0; i < slen; i++) { n     vs[start[i] -  a ].push_back(i); n     vr[srev[i] -  a ].push_back(i); n   } n   int elen = end.length(); n   bool flag = true; n   for (int i = 0; i < elen; i++) { n     if (vs[end[i] -  a ].size() == 0) { n       flag = false; n       break; n     } n   } n   if (!flag) n     cout << -1 << endl; n   else { n     int no = 0; n     vector<pair<int, int> > res; n     for (int i = 0; i < elen;) { n       int size = vs[end[i] -  a ].size(); n       pair<int, int> p; n       int maxl, ans = 0, index, tmp; n       for (int j = 0; j < size; j++) { n         index = vs[end[i] -  a ][j]; n         tmp = index; n         maxl = i; n         while ((index < slen - 1) && (maxl < elen - 1) && n                start[index + 1] == end[maxl + 1]) { n           index++; n           maxl++; n         } n         if (maxl - i + 1 > ans) { n           ans = maxl - i + 1; n           p = make_pair(tmp, index); n         } n       } n       maxl = i; n       for (int j = 0; j < size; j++) { n         index = vr[end[i] -  a ][j]; n         tmp = index; n         maxl = i; n         while ((index < slen - 1) && (maxl < elen - 1) && n                srev[index + 1] == end[maxl + 1]) { n           index++; n           maxl++; n         } n         if (maxl - i + 1 > ans) { n           ans = maxl - i + 1; n           p = make_pair(slen - 1 - tmp, slen - 1 - index); n         } n       } n       i += ans; n       no++; n       res.push_back(p); n     } n     cout << no << endl; n     for (int i = 0; i < res.size(); i++) n       cout << res[i].first + 1 <<     << res[i].second + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long s, x1, x2, t1, t2, p, d; n   cin >> s >> x1 >> x2 >> t1 >> t2 >> p >> d; n   long long ans = abs(x1 - x2) * t2; n   long long i = p, t = 0; n   while (i != x1) { n     i += d; n     t += t1; n     if (i == s) n       d = -1; n     else if (i == 0) n       d = 1; n   } n   while (i != x2) { n     i += d; n     t += t1; n     if (i == s) n       d = -1; n     else if (i == 0) n       d = 1; n   } n   ans = min(ans, t); n   cout << ans <<   n ; n } n void solve(bool testcase) { n   long long t; n   cin >> t; n   while (t--) solve(); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mo = (int)1e9 + 7; n const int maxn = 1000010; n int n; n long long fac[maxn], ans; n int a[maxn], num[maxn], num1[maxn], pos[maxn], mul[maxn]; n int pre[maxn], ne[maxn]; n vector<int> v[maxn]; n inline void init() { n   fac[0] = 1; n   for (int i = 1; i <= n + 2; ++i) fac[i] = fac[i - 1] * i % mo, mul[i] = 1; n   for (int i = 2; i <= n; ++i) { n     if (!v[i].size()) { n       num[n / i]++; n       pos[i] = n / i; n       for (int j = i; j <= n; j += i) v[j].push_back(i), mul[j] *= i; n     } n   } n   for (int i = 1; i <= n; ++i) num1[mul[i]]++; n   num[1]++; n   pos[1] = 1; n   v[1].push_back(1); n } n int main() { n   scanf( %d , &n); n   init(); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) n     if (a[i]) { n       num1[mul[a[i]]]--; n       if (v[a[i]].size() != v[i].size()) return puts( 0 ), 0; n       for (int j = 0; j < v[i].size() - 1; j++) n         if (v[a[i]][j] != v[i][j]) return puts( 0 ), 0; n       long long x = v[a[i]].back(), y = v[i].back(); n       if (pos[x] != pos[y]) return puts( 0 ), 0; n       if (pre[x] && pre[x] != y) return puts( 0 ), 0; n       if (ne[y] && ne[y] != x) return puts( 0 ), 0; n       if (!pre[x] && !ne[y]) num[pos[x]]--; n       pre[x] = y; n       ne[y] = x; n     } n   ans = 1; n   for (int i = 1; i <= n; ++i) n     ans = ans * fac[num[i]] % mo, ans = ans * fac[num1[i]] % mo; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int tmp; n   int ans = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= i; j++) { n       tmp = i * i + j * j; n       if (tmp <= n * n && int(sqrt(tmp)) * int(sqrt(tmp)) == tmp) { n         ans++; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; n const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; n const int MAX = 2 * 1000 + 10; n const long long INF = 1e18; n const int MOD = 1e9 + 7; n long long n, m, x, y, a[MAX], b[MAX], c[MAX], d[MAX]; n long long dp[MAX][20]; n long long sol(long long n, long long ind) { n   if (n < 0) return -MOD; n   if (ind > m or n == 0) return 0; n   if (dp[n][ind] != -1) return dp[n][ind]; n   long long &ans = dp[n][ind]; n   ans = y + sol(n - x, ind); n   for (long long i = 0; i <= n; i++) { n     if (i * c[ind] > n or i * b[ind] > a[ind]) break; n     ans = max(ans, i * d[ind] + sol(n - i * c[ind], ind + 1)); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m >> x >> y; n   for (long long i = 1; i <= m; i++) { n     cin >> a[i] >> b[i] >> c[i] >> d[i]; n   } n   memset(dp, -1, sizeof(dp)); n   cout << sol(n, 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> A(n); n   for (int& a : A) scanf( %d , &a); n   sort(A.begin(), A.end()); n   int m = A[n - 1]; n   vector<bool> B(m + 2); n   for (int a : A) { n     if (a > 1 && !B[a - 1]) n       B[a - 1] = true; n     else if (!B[a]) n       B[a] = true; n     else if (!B[a + 1]) n       B[a + 1] = true; n   } n   printf( %lu n , count(B.begin(), B.end(), true)); n } n


#include <bits/stdc++.h> n using namespace std; n long long numbit(long long x) { return (1LL << x) - 1LL; } n long long getbit(long long x, long long i) { return (x >> i) & 1LL; } n long long onbit(long long x, long long i) { return x | (1LL << i); } n long long offbit(long long x, long long i) { return x & (~(1LL << i)); } n const int oo = 1000111000; n const int Nmax = 1e5 + 10; n const long long mod = 1e9 + 7; n const long double PI = 3.14159265358979323846; n int p[Nmax * 2], c[Nmax * 2]; n int ch[Nmax * 2]; n bool ok[Nmax * 2]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   int test; n   cin >> test; n   for (long long e = (1); e <= (test); e++) { n     int n; n     cin >> n; n     for (long long i = (1); i <= (n); i++) cin >> p[i]; n     for (long long i = (1); i <= (n); i++) cin >> c[i]; n     memset(ok, false, sizeof(ok)); n     memset(ch, 0, sizeof(ch)); n     int cnt = 0; n     vector<int> circle[n + 1]; n     for (long long i = (1); i <= (n); i++) n       if (!ok[i]) { n         cnt++; n         int j = i; n         while (!ok[j]) { n           circle[cnt].push_back(j); n           ok[j] = true; n           j = p[j]; n         } n       } n     int res = n + 1; n     for (long long i = (1); i <= (cnt); i++) { n       int it = 0; n       for (long long m = (1); m <= (circle[i].size()); m++) n         if (circle[i].size() % m == 0) { n           if (m >= res) break; n           int j = 0; n           it++; n           bool ok = false; n           while (j < circle[i].size()) { n             if (ch[circle[i][j]] == it) { n               j++; n               continue; n             } n             ok = true; n             int h = j; n             while (ok) { n               if (ch[circle[i][h]] == it || n                   c[circle[i][j]] != c[circle[i][h]]) { n                 ok = false; n                 break; n               } n               ch[circle[i][h]] = it; n               h = (h + m) % circle[i].size(); n               if (h == j) break; n             } n             if (ok) break; n             j++; n           } n           if (ok) { n             res = min(res, int(m)); n             break; n           } n         } n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int p, n, a[100100], b[100100]; n bool ok(double t) { n   double charging = t * p; n   for (int i = 0; i < n; ++i) { n     double x = b[i] - a[i] * t; n     if (x >= 0) n       continue; n     else n       charging += x; n     if (charging < 0) return 0; n   } n   return 1; n } n int main() { n   cin >> n >> p; n   for (int i = 0; i < n; ++i) cin >> a[i] >> b[i]; n   double low = 0, high = 1e11, tol = 1e-7, mid; n   while (high - low > tol) { n     mid = (high + low) / 2; n     if (ok(mid)) n       low = mid; n     else n       high = mid; n   } n   if (low < 9.9e10) n     cout << setprecision(10) << fixed << mid; n   else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, tot, vis[50005]; n pair<long double, long double> stk[50005 << 1]; n int bit[50005 << 1]; n const long double Pi = acos(-1); n struct Point { n   long double x, y; n   Point() {} n   Point(long double a, long double b) { x = a, y = b; } n   Point operator+(Point p) { return Point(x + p.x, y + p.y); } n   Point operator-(Point p) { return Point(x - p.x, y - p.y); } n   Point operator*(long double c) { return Point(x * c, y * c); } n   Point operator/(long double c) { return Point(x / c, y / c); } n } P, S[50005]; n long double Abs(long double x) { return x > 0 ? x : -x; } n bool Equal(long double a, long double b) { return Abs(a - b) < 1e-8; } n long double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } n Point Rotate(Point p, long double a) { n   return Point(p.x * cos(a) - p.y * sin(a), p.x * sin(a) + p.y * cos(a)); n } n bool Onleft(int x) { n   Point a = Point(0, S[x].y), b = Point(1, S[x].x + S[x].y); n   return Cross(a - P, b - P) > 0; n } n long double Length(Point p) { return sqrt(p.x * p.x + p.y * p.y); } n long double Getang(long double x) { n   while (x < -Pi) x += Pi * 2; n   while (x > Pi) x -= Pi * 2; n   return x; n } n void Update(int x, int v) { n   for (int i = x; i <= tot; i += i & -i) bit[i] += v; n } n int Getsum(int x) { n   int ret = 0; n   for (int i = x; i; i -= i & -i) ret += bit[i]; n   return ret; n } n Point Intersect(int i, int j) { n   long double x = (S[j].y - S[i].y) / (S[i].x - S[j].x); n   return Point(x, S[i].x * x + S[i].y); n } n int Solve(long double r) { n   tot = 0; n   for (int i = 1; i <= n; i++) { n     long double dis = n         Abs(S[i].x * P.x - P.y + S[i].y) / sqrt(S[i].x * S[i].x + 1); n     if (dis > r) continue; n     long double ang = asin(dis / r); n     ang = Getang(ang); n     if (ang > Pi / 2) ang = Pi / 2 - ang; n     Point T(100, 100 * S[i].x); n     if (vis[i]) T = T * -1; n     Point t1 = Rotate(T, ang) * -1; n     Point t2 = Rotate(T, -ang); n     long double a1 = Getang(atan2(t1.y, t1.x)); n     long double a2 = Getang(atan2(t2.y, t2.x)); n     if (a1 > a2) swap(a1, a2); n     stk[++tot] = make_pair(a1, a2); n     stk[++tot] = make_pair(a2, a1); n   } n   sort(stk + 1, stk + tot + 1); n   for (int i = 1; i <= tot; i++) bit[i] = 0; n   int cnt = 0; n   for (int i = 1; i <= tot; i++) { n     if (stk[i].second > stk[i].first) n       Update(i, 1); n     else { n       int p = lower_bound(stk + 1, stk + tot + 1, n                           make_pair(stk[i].second, stk[i].first)) - n               stk; n       cnt += Getsum(i) - Getsum(p); n       Update(p, -1); n     } n   } n   return cnt; n } n set<pair<long double, int> > s; n long double Calc(long double r) { n   tot = 0; n   long double cnt = 0; n   for (int i = 1; i <= n; i++) { n     long double dis = n         Abs(S[i].x * P.x - P.y + S[i].y) / sqrt(S[i].x * S[i].x + 1); n     if (dis > r) continue; n     long double ang = asin(dis / r); n     ang = Getang(ang); n     if (ang > Pi / 2) ang = Pi / 2 - ang; n     Point T(100, 100 * S[i].x); n     if (vis[i]) T = T * -1; n     Point t1 = Rotate(T, ang) * -1; n     Point t2 = Rotate(T, -ang); n     long double a1 = Getang(atan2(t1.y, t1.x)); n     long double a2 = Getang(atan2(t2.y, t2.x)); n     if (a1 > a2) swap(a1, a2); n     stk[++tot] = make_pair(a1, i + 500); n     stk[++tot] = make_pair(a2, a1); n   } n   sort(stk + 1, stk + tot + 1); n   for (int i = 1; i <= tot; i++) { n     if (stk[i].second > stk[i].first) n       s.insert(make_pair(stk[i].first, (int)(stk[i].second - 500 + 0.1))); n     else { n       set<pair<long double, int> >::iterator p = n           s.lower_bound(make_pair(stk[i].second, 0)); n       if (p == s.end()) continue; n       set<pair<long double, int> >::iterator t = p; n       for (t++; t != s.end(); t++) n         cnt += Length(Intersect(t->second, p->second) - P); n       s.erase(p); n     } n   } n   return cnt; n } n int main() { n   scanf( %d , &n); n   int x, y; n   scanf( %d%d , &x, &y); n   P = Point((long double)x / 1000, (long double)y / 1000); n   scanf( %d , &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x, &y); n     S[i] = Point((long double)x / 1000, (long double)y / 1000); n   } n   for (int i = 1; i <= n; i++) vis[i] = Onleft(i); n   long double l = 0, r = 1e10; n   int c; n   for (int i = 1; i <= 80; i++) { n     long double mid = (l + r) / 2; n     c = Solve(mid); n     if (c >= m) n       r = mid; n     else n       l = mid; n   } n   c = Solve(l - 1e-8); n   printf( %.12lf n , double(Calc(l - 1e-8) + (m - c) * l)); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[5001], b[1000001], d[1000001]; n void doc() { n   for (int i = 1; i <= n; i++) cin >> a[i]; n } n void xuli() { n   sort(a + 1, a + n + 1); n   for (int i = 1; i < n; i++) n     for (int j = i + 1; j <= n; j++) d[a[j] - a[i]]++; n   int m = 0; n   while (true) { n     m++; n     int dem = 0, res = 0; n     for (int j = m; j <= 1000001; j += m) dem += d[j]; n     if (dem > (k * (k + 1)) / 2) continue; n     for (int i = 1; i <= n; i++) { n       if (b[a[i] % m] == m) res++; n       b[a[i] % m] = m; n     } n     if (res <= k) break; n   } n   cout << m; n } n int main() { n   cin >> n >> k; n   doc(); n   xuli(); n } n


#include <bits/stdc++.h> n using namespace std; n int exgcd(int a, int b, int &x, int &y) { n   if (b == 0) { n     y = 0; n     x = 1; n     return a; n   } n   int r = exgcd(b, a % b, x, y); n   int tmp = y; n   y = x - (a / b) * y; n   x = tmp; n   return r; n } n int t, n, a[200005], s, ans[200005], k1, k2, len, nos[200005]; n set<int> se[200005]; n unordered_map<int, int> vis, id; n int main() { n   while (~scanf( %d%d , &t, &n)) { n     vis.clear(); n     id.clear(); n     for (int i = 0; i < 200005; ++i) se[i].clear(); n     memset(ans, -1, 4 * n + 4); n     a[0] = s = 0; n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &a[i]); n       s = (s + a[i]) % t; n     } n     a[1] = 0; n     for (int i = 2; i <= n; ++i) a[i] = (a[i - 1] + a[i]) % t; n     int g = exgcd(s, t, k1, k2), cnt = 0, len = t / g; n     for (int i = 1; i <= n; ++i) { n       if (!vis.count(a[i])) { n         vis[a[i]] = true; n         if (!id[a[i] % g]) id[a[i] % g] = ++cnt; n         nos[i] = 1LL * k1 * (a[i] % g - a[i]) / g % len; n         nos[i] = (nos[i] + len) % len; n         se[id[a[i] % g]].insert(nos[i]); n       } else n         ans[i] = 0; n     } n     for (int i = 1; i <= n; ++i) { n       if (ans[i] == -1) { n         set<int>::iterator it = se[id[a[i] % g]].find(nos[i]); n         if (it == se[id[a[i] % g]].begin()) n           it = --se[id[a[i] % g]].end(); n         else n           --it; n         int s1 = nos[i], s2 = *it, tmp = s1 - s2; n         ans[i] = tmp != 0 ? (tmp + len) % len : len; n       } n     } n     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n const int INF = (int)1E9; n const long long INF64 = (long long)1E18; n const double EPS = 1E-9; n const double PI = acos((double)0) * 2; n const int MAXN = 210000; n struct pt { n   int x, y, id; n   bool operator<(const pt& p) const { n     return make_pair(x, y) < make_pair(p.x, p.y); n   } n }; n int n, k; n pt a[MAXN], b[MAXN]; n bool read() { n   if (!(cin >> n >> k)) return false; n   for (int i = 0; i < int(n); ++i) { n     scanf( %d%d , &a[i].x, &a[i].y); n     a[i].id = i; n   } n   return true; n } n int killed[MAXN], cu; n inline void skip(pt p[], int& ptr) { n   while (ptr < n && killed[p[ptr].id] == cu) ++ptr; n } n bool check(pt delta) { n   ++cu; n   int already = 0; n   int pa = 0, pb = 0; n   for (;;) { n     skip(a, pa); n     skip(b, pb); n     if (pa == n && pb == n) break; n     if (pa == n) { n       ++pb; n       if (++already > k) return false; n       continue; n     } n     if (pb == n) { n       ++pa; n       if (++already > k) return false; n       continue; n     } n     pt tmp = b[pb]; n     tmp.x += delta.x; n     tmp.y += delta.y; n     if (tmp.x == a[pa].x && tmp.y == a[pa].y) { n       ++pa; n       ++pb; n       continue; n     } n     if (++already > k) return false; n     if (tmp < a[pa]) { n       killed[b[pb].id] = cu; n       ++pb; n     } else { n       killed[a[pa].id] = cu; n       ++pa; n     } n   } n   return already <= k; n } n void solve() { n   if (k >= n) { n     puts( -1 ); n     return; n   } n   for (int i = 0; i < int(n); ++i) { n     b[i] = a[i]; n     b[i].x *= -1; n     b[i].y *= -1; n   } n   sort(a, a + n); n   sort(b, b + n); n   set<pt> ans; n   for (int c1 = 0; c1 < int(min(n, k + 13)); ++c1) n     for (int c2 = 0; c2 < int(min(n, k + 13)); ++c2) { n       pt delta; n       delta.x = a[c2].x - b[c1].x; n       delta.y = a[c2].y - b[c1].y; n       if (!ans.count(delta)) n         if (check(delta)) ans.insert(delta); n     } n   printf( %d n , ans.size()); n   for (set<pt>::iterator i = ans.begin(); i != ans.end(); ++i) { n     pt p = *i; n     printf( %.1lf %.1lf n , p.x / 2.0, p.y / 2.0); n   } n } n int main() { n   if (!read()) throw; n   solve(); n } n


#include <bits/stdc++.h> n  n #define fi first n #define se second n #define DB double n #define U unsigned n #define P std::pair n #define LL long long n #define LD long double n #define pb emplace_back n #define MP std::make_pair n #define SZ(x) ((int)x.size()) n #define all(x) x.begin(),x.end() n #define CLR(i,a) memset(i,a,sizeof(i)) n #define FOR(i,a,b) for(int i = a;i <= b;++i) n #define ROF(i,a,b) for(int i = a;i >= b;--i) n #define DEBUG(x) std::cerr << #x <<  =  << x << std::endl n  n const int MAXN = 400+5; n const int ha = 998244353; n int n,c[26]; n int f[2][MAXN][MAXN][3][3]; n int g[2][MAXN][2][2],pw[MAXN]; n int F[MAXN][MAXN],G[MAXN]; n // 0=a 1=b 2=oth n  n inline void add(int &x,int y){ n     x += y-ha;x += x>>31&ha; n } n  n int main(){ n     pw[0] = 1;FOR(i,1,MAXN-1) pw[i] = 25ll*pw[i-1]%ha; n     scanf( %d ,&n); n     FOR(i,0,25) scanf( %d ,c+i); n     int now = 0; n     f[now][0][0][2][2] = 24*24; n     f[now][1][0][2][0] = f[now][1][0][0][2] = 24; n     f[now][0][1][2][1] = f[now][0][1][1][2] = 24; n     f[now][1][1][0][1] = f[now][1][1][1][0] = 1; n     f[now][2][0][0][0] = f[now][0][2][1][1] = 1; n     FOR(i,3,n){ n         CLR(f[now^1],0); n         FOR(a,0,n){ n             FOR(b,0,n){ n                 FOR(c,0,2){ n                     FOR(d,0,2){ n                         // cd? n                         int gx = f[now][a][b][c][d]; n                         if(!gx) continue; n                         if(c != 0) add(f[now^1][a+1][b][d][0],gx); n                         if(c != 1) add(f[now^1][a][b+1][d][1],gx); n                         add(f[now^1][a][b][d][2],1ll*gx*(c==2?23:24)%ha); n                     } n                 } n             } n         } n         now ^= 1; n     } n     FOR(i,0,n) FOR(j,0,n) FOR(x,0,2) FOR(y,0,2) add(F[i][j],f[now][i][j][x][y]); n     ROF(i,n,0) ROF(j,n,1) add(F[i][j],F[i+1][j]),add(F[i][j],F[i][j+1]),add(F[i][j],ha-F[i+1][j+1]); n     now = 0; n     g[now][0][1][1] = 25*25; n     g[now][1][0][1] = g[now][1][1][0] = 25; n     g[now][2][0][0] = 1; n     FOR(i,3,n){ n         CLR(g[now^1],0); n         FOR(a,0,n){ n             FOR(c,0,1){ n                 FOR(d,0,1){ n                     int gx = g[now][a][c][d]; n                     if(!gx) continue; n                     if(c != 0) add(g[now^1][a+1][d][0],gx); n                     add(g[now^1][a][d][1],1ll*gx*(c==1?24:25)%ha); n                 } n             } n         } n         now ^= 1; n     } n //    DEBUG(g[now][3][0][1]); n     FOR(i,0,n) FOR(x,0,1) FOR(y,0,1) add(G[i],g[now][i][x][y]); n     ROF(i,n,0) add(G[i],G[i+1]); n     auto calc = [&](int n){ n         return 26ll*pw[n-1]%ha; n     }; n     int ans = 1ll*calc(n/2)*calc((n+1)/2)%ha; n     FOR(i,0,25) add(ans,ha-G[c[i]+1]); n     FOR(i,0,25) FOR(j,i+1,25) add(ans,F[c[i]+1][c[j]+1]); n     printf( %d n ,ans); n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[510]; n vector<pair<int, int> > edges; n vector<int> leaf, nonleaf; n int d = 0; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     if (a[i] == 1) n       leaf.push_back(i); n     else n       nonleaf.push_back(i); n   } n   d = nonleaf.size() - 1 + min((int)leaf.size(), 2); n   int reserved = -1; n   if (!leaf.empty()) reserved = leaf.back(), leaf.pop_back(); n   for (int i = 0; i < (int)nonleaf.size() - 1; i++) { n     int k = nonleaf[i]; n     while (!leaf.empty() && a[k] > 1) { n       edges.push_back({leaf.back(), k}); n       leaf.pop_back(); n       a[k]--; n     } n     a[k]--; n     a[nonleaf[i + 1]]--; n     edges.push_back({k, nonleaf[i + 1]}); n   } n   int tp = 501; n   if (!nonleaf.empty()) tp = nonleaf.back(); n   if (reserved != -1) leaf.push_back(reserved); n   while (!leaf.empty() && a[tp] > 0) { n     edges.push_back({leaf.back(), tp}); n     leaf.pop_back(); n     a[tp]--; n   } n   if (leaf.empty()) { n     printf( YES %d n , d); n     printf( %d n , (int)edges.size()); n     for (auto k : edges) { n       printf( %d %d n , k.first, k.second); n     } n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1234567; n const long long MOD = 998244353; n void solve() { n   long long n, m, i, j = 0, k, l = 0, ok = 0, o = 0, p, ans = 0, x, y, a[30]; n   string s; n   cin >> n >> k >> s; n   for (long long i = 0; i < k / 2; i++) { n     map<char, long long> mg; n     for (j = 0; j < n; j += k) { n       mg[s[i + j]]++; n       mg[s[j + k - i - 1]]++; n     } n     l = 0; n     for (auto it : mg) l = max(l, it.second); n     ans += l; n   } n   if (k % 2) { n     map<char, long long> mg; n     for (j = 0; j < n; j += k) { n       mg[s[j + k / 2]]++; n     } n     l = 0; n     for (auto it : mg) l = max(l, it.second); n     ans += l; n   } n   cout << (n - ans) << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   tuple<long long, long long, long long> p[200010]; n   int n; n   cin >> n; n   long long k, a, x, y, m; n   long long b; n   long long ans = 0; n   long long cnt = 0; n   long long sm = 0; n   for (int i = 0; i < n; i++) { n     cin >> k >> a >> x >> y >> m; n     long long t = 0; n     sm += k; n     for (int j = 0; j < k; j++) { n       if (cnt <= 200000) p[cnt++] = {t, a, i}; n       b = (a * x + y) % m; n       if (b < a && j != k - 1) t++; n       a = b; n     } n     ans = max(ans, t); n   } n   cout << ans << endl; n   if (cnt <= 200000) { n     sort(p, p + cnt, n          [](tuple<long long, long long, long long> &a, n             tuple<long long, long long, long long> &b) -> bool { n            if (get<0>(a) == get<0>(b)) { n              return get<1>(a) < get<1>(b) || n                     (get<1>(a) == get<1>(b) && get<2>(a) < get<2>(b)); n            } n            return get<0>(a) < get<0>(b); n          }); n     for (int i = 0; i < cnt; i++) { n       cout << get<1>(p[i]) <<     << get<2>(p[i]) + 1 << endl; n     } n   } n } n