#include <bits/stdc++.h> n using namespace std; n const int Inf = (1 << 30) - 1; n const long long lInf = (1LL << 62) - 1; n const int mInf = 0x7f7f7f7f; n const int Maxn = 200010; n vector<int> a[Maxn]; n pair<int, int> S[Maxn << 1]; n int f[Maxn]; n int t[Maxn]; n int n, m, ans; n void solve(int k) { n   map<int, int> M; n   for (int i = 1; i <= n; i++) M[++f[i]] = i; n   for (int i = 1; i <= n; i++) t[i] = a[i][k]; n   int cnt = 0, tot = 0, minn = Inf; n   for (int i = 1; i <= n; i++) { n     int tmp = M[t[i]]; n     if (tmp != 0 && i - tmp != 0) n       S[++tot] = make_pair(i - tmp > 0 ? i - tmp : i - tmp + n, 1); n     if (tmp != 0) n       S[++tot] = make_pair(i - tmp + 1 > 0 ? i - tmp + 1 : i - tmp + n + 1, -1); n     if (t[i] == f[i]) cnt++; n   } n   sort(S + 1, S + tot + 1); n   minn = min(minn, n - cnt); n   int p = 1; n   for (int i = 1; i < n; i++) { n     while (S[p].first <= i && p <= tot) cnt += S[p++].second; n     minn = min(minn, i + n - cnt); n   } n   ans += minn; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     a[i].push_back(0); n     for (int x, j = 1; j <= m; j++) cin >> x, a[i].push_back(x); n   } n   for (int i = 1; i <= n; i++) f[i] = (i - 1) * m; n   for (int i = 1; i <= m; i++) solve(i); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, s, f, add = 0, rund = 1; n   cin >> n >> m >> s >> f; n   if (s < f) n     add = 1; n   else n     add = -1; n   for (int i = 0; i < m; i++) { n     int t, l, r; n     cin >> t >> l >> r; n     while (rund < t) { n       if (s == f) break; n       if (add == 1) n         cout <<  R ; n       else if (add == -1) n         cout <<  L ; n       s += add; n       rund++; n     } n     if (s == f) continue; n     if (t == rund) { n       rund++; n       if (s >= l && s <= r || s + add >= l && s + add <= r) n         cout <<  X ; n       else if (add == 1) { n         cout <<  R ; n         s += add; n       } else if (add == -1) { n         cout <<  L ; n         s += add; n       } n     } n   } n   while (s != f) { n     if (add == 1) n       cout <<  R ; n     else if (add == -1) n       cout <<  L ; n     s += add; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Nmax = 1e5 + 5, lg = 16; n struct updates { n   int l, x, y, id; n   bool operator<(const updates &other) const { n     if (l != other.l) return l > other.l; n     return id < other.id; n   } n }; n vector<updates> upd; n vector<int> v[Nmax]; n struct Node { n   int s, l, r; n   bool full; n   Node() { s = l = r = full = 0; } n   Node(int S, int L, int R, bool Full) { n     s = S; n     l = L; n     r = R; n     full = Full; n   } n }; n int f[Nmax], x, y, w, i, answer[Nmax], n, q; n Node combina(Node a, Node b) { n   if (a.full && b.full) return Node(0, a.l + b.l, a.r + b.r, 1); n   if (a.full) return Node(b.s, a.l + b.l, b.r, 0); n   if (b.full) return Node(a.s, a.l, a.r + b.r, 0); n   return Node(a.s + b.s + f[a.r + b.l], a.l, b.r, 0); n } n class SegmentTree { n   Node *a; n  n  public: n   void build(int sz) { a = new Node[4 * sz + 5]; } n   void update(int node, int st, int dr, int pos) { n     if (st == dr) { n       a[node].s = 0; n       a[node].l = a[node].r = a[node].full = 1; n       return; n     } n     if (pos <= ((st + dr) / 2)) n       update((node << 1), st, ((st + dr) / 2), pos); n     else n       update(((node << 1) + 1), ((st + dr) / 2) + 1, dr, pos); n     a[node] = combina(a[(node << 1)], a[((node << 1) + 1)]); n   } n   Node query(int node, int st, int dr, int Left, int Right) { n     if (Left <= st && dr <= Right) return a[node]; n     if (Left <= ((st + dr) / 2) && ((st + dr) / 2) < Right) n       return combina( n           query((node << 1), st, ((st + dr) / 2), Left, Right), n           query(((node << 1) + 1), ((st + dr) / 2) + 1, dr, Left, Right)); n     if (Left <= ((st + dr) / 2)) n       return query((node << 1), st, ((st + dr) / 2), Left, Right); n     else n       return query(((node << 1) + 1), ((st + dr) / 2) + 1, dr, Left, Right); n   } n } aint[Nmax]; n class HeavyPath { n   int level[Nmax], dad[Nmax], to[Nmax], pos[Nmax], w[Nmax], sz[Nmax], n       First[Nmax], t[Nmax][lg + 2], nrChains; n   void dfs(int node) { n     for (auto i = 1; i <= lg; ++i) t[node][i] = t[t[node][i - 1]][i - 1]; n     w[node] = 1; n     for (auto it : v[node]) n       if (!level[it]) { n         level[it] = level[node] + 1; n         dad[it] = t[it][0] = node; n         dfs(it); n         w[node] += w[it]; n       } n   } n   void chains(int node) { n     to[node] = nrChains; n     pos[node] = ++sz[nrChains]; n     if (v[node].size() == 1 && node != 1) return; n     int xson = 0; n     for (auto it : v[node]) n       if (it != dad[node] && w[xson] < w[it]) xson = it; n     chains(xson); n     for (auto it : v[node]) n       if (it != dad[node] && it != xson) { n         First[++nrChains] = it; n         chains(it); n       } n   } n   Node query(int x, int y) { n     if (to[x] == to[y]) n       return aint[to[x]].query(1, 1, sz[to[x]], pos[x], pos[y]); n     return combina(query(x, dad[First[to[y]]]), n                    aint[to[y]].query(1, 1, sz[to[y]], 1, pos[y])); n   } n  n  public: n   void prepare() { n     memset(level, 0, sizeof(level)); n     level[1] = 1; n     dad[1] = 0; n     dfs(1); n     nrChains = 1; n     First[1] = 1; n     memset(sz, 0, sizeof(sz)); n     chains(1); n     int i; n     for (i = 1; i <= nrChains; ++i) aint[i].build(sz[i]); n   } n   int get_answer(int x, int y) { n     int X = x, Y = y; n     if (level[x] < level[y]) swap(x, y), swap(X, Y); n     int up = level[x] - level[y] - 1, i; n     if (up != -1) { n       for (i = 0; i <= lg; ++i) n         if (up & (1 << i)) x = t[x][i]; n       if (dad[x] == y) { n         auto ans = query(x, X); n         if (ans.full) return f[ans.l]; n         return ans.s + f[ans.l] + f[ans.r]; n       } n       x = dad[x]; n     } n     for (i = lg; i >= 0; --i) n       if (t[x][i] != t[y][i]) x = t[x][i], y = t[y][i]; n     auto ans1 = query(x, X), ans2 = query(y, Y); n     swap(ans2.l, ans2.r); n     ans1 = combina(ans2, ans1); n     if (ans1.full) return f[ans1.l]; n     return ans1.s + f[ans1.l] + f[ans1.r]; n   } n   void add_edge(int x, int y) { n     if (y == dad[x]) swap(x, y); n     aint[to[y]].update(1, 1, sz[to[y]], pos[y]); n   } n } hp; n int main() { n   cin.sync_with_stdio(false); n   cin >> n >> q; n   for (i = 1; i < n; ++i) cin >> f[i]; n   for (i = 1; i < n; ++i) { n     cin >> x >> y >> w; n     v[x].push_back(y); n     v[y].push_back(x); n     upd.push_back({w, x, y, 0}); n   } n   for (i = 1; i <= q; ++i) { n     cin >> x >> y >> w; n     upd.push_back({w, x, y, i}); n   } n   sort(upd.begin(), upd.end()); n   hp.prepare(); n   for (auto it : upd) { n     if (!it.id) n       hp.add_edge(it.x, it.y); n     else n       answer[it.id] = hp.get_answer(it.x, it.y); n   } n   for (i = 1; i <= q; ++i) cout << answer[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int(n); n   scanf( %d , &(n)); n   int val[100001][2]; n   double arr[100001]; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   int sum = 0; n   for (int i = 1; i <= n; i++) { n     int t = arr[i]; n     if (arr[i] - t != 0) { n       if (t > 0) { n         val[i][0] = t; n         val[i][1] = t + 1; n       } n       if (t < 0) { n         val[i][0] = t - 1; n         val[i][1] = t; n       } n       if (t == 0) { n         if (arr[i] > 0) { n           val[i][0] = 0; n           val[i][1] = 1; n         } n         if (arr[i] < 0) { n           val[i][0] = -1; n           val[i][1] = 0; n         } n       } n     } else { n       val[i][0] = t; n       val[i][1] = t; n     } n     sum += val[i][1]; n   } n   int k = sum; n   for (int i = 1; i <= n; i++) { n     if (k > 0 && val[i][0] != val[i][1]) { n       cout << val[i][0] <<   n ; n       k--; n     } else n       cout << val[i][1] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[100001]; n int w[100001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> w[i]; n   } n   int ch = m; n   set<pair<long long, int> > s; n   long long cnt = 0; n   for (int i = 0; i < n; i++) { n     if (ch < c[i] % 100) { n       long long mn = (100 - c[i] % 100) * w[i]; n       if (s.begin() != s.end() && s.begin()->first < mn) { n         cnt += s.begin()->first; n         ch += 100; n         ch -= c[i] % 100; n         s.erase(s.begin()); n         s.insert(make_pair((100 - c[i] % 100) * w[i], i)); n       } else { n         ch += 100 - c[i] % 100; n         cnt += mn; n       } n     } else { n       if (c[i] % 100 != 0) { n         s.insert(make_pair((100 - c[i] % 100) * w[i], i)); n         ch -= c[i] % 100; n       } n     } n   } n   cout << cnt << endl; n   vector<bool> p(n, 0); n   for (set<pair<long long, int> >::iterator it = s.begin(); it != s.end(); n        it++) { n     p[it->second] = 1; n   } n   for (int i = 0; i < n; i++) { n     if (p[i]) { n       cout << c[i] / 100 <<     << c[i] % 100 << endl; n     } else { n       cout << (c[i] + 99) / 100 <<   0  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   pair<int, int> p[100]; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> p[i].first; n     p[i].second = i + 1; n   } n   sort(p, p + n); n   for (int i = 0; i < n; i++) cout << p[i].second <<    ; n } n


#include <bits/stdc++.h> n int main() { n   int ans = 0; n   int count; n   scanf( %d , &count); n   for (int i = 0; i < count; i++) { n     int prize; n     scanf( %d , &prize); n     int minTimeToPrize = std::min(prize - 1, 1000000 - prize); n     ans = std::max(ans, minTimeToPrize); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n struct Point { n   int x, y; n   void read() { n     int a, b; n     scanf( %d%d , &a, &b); n     x = a + b; n     y = a - b; n   } n   bool operator<(const Point p) const { return x != p.x ? x < p.x : y < p.y; } n } p[N]; n int n; n int pre_min[N], pre_max[N]; n int suf_min[N], suf_max[N]; n bool check(long long mid) { n   for (int l = 1, r = 1; r <= n; r++) { n     while (p[r].x - p[l].x > mid && l <= n) l++; n     int mx = max(pre_max[l - 1], suf_max[r + 1]); n     int mn = min(pre_min[l - 1], suf_min[r + 1]); n     if (mx - mn <= mid) return true; n   } n   return false; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) p[i].read(); n   sort(p + 1, p + 1 + n); n   pre_min[0] = INT_MAX; n   pre_max[0] = INT_MIN; n   suf_min[n + 1] = INT_MAX; n   suf_max[n + 1] = INT_MIN; n   for (int i = 1; i <= n; i++) { n     pre_max[i] = max(pre_max[i - 1], p[i].y); n     pre_min[i] = min(pre_min[i - 1], p[i].y); n   } n   for (int i = n; i >= 1; i--) { n     suf_max[i] = max(suf_max[i + 1], p[i].y); n     suf_min[i] = min(suf_min[i + 1], p[i].y); n   } n   long long ans, low = 0, high = 1LL << 50, mid; n   while (low <= high) { n     mid = (low + high) >> 1; n     if (check(mid)) n       high = mid - 1, ans = mid; n     else n       low = mid + 1; n   } n   printf( %.10f n , ans / 2.0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 40, MMax = 200; n int N, nn, A[NMax][NMax], B[NMax]; n int get(int a, int b) { return (a ^ b); } n int P(int a, int b) { n   if (b) n     return a * -1; n   else n     return a; n } n int main() { n   scanf( %d , &N); n   nn = (N + 1) >> 1; n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= N; j++) scanf( %d , &A[i][j]); n   int ret = -100000000; n   for (int I = 0; I < (1 << nn); I++) { n     int cnt = 0; n     int k = I; n     for (int i = 1; i <= nn; i++) { n       B[i] = (I & 1); n       cnt += P(A[i][nn], B[i]); n       I >>= 1; n     } n     I = k; n     for (int i = 1; i < nn; i++) cnt += P(A[i + nn][nn], get(B[i], B[nn])); n     for (int i = 1; i < nn; i++) { n       int tot0 = P(A[nn][i], 0) + P(A[nn][nn + i], get(0, B[nn])), n           tot1 = P(A[nn][i], 1) + P(A[nn][nn + i], get(1, B[nn])); n       for (int j = 1; j < nn; j++) { n         int maxx = -100000000; n         for (int first = 0; first < 2; first++) { n           int second = 0; n           int x1 = get(first, B[j]), x2 = get(second, B[nn]), n               x3 = get(B[j], B[nn]), x4 = get(first, second), x5 = get(x1, x2); n           if (get(x3, x4) == x5) n             maxx = n                 max(maxx, P(A[j][i], first) + P(A[j][nn + i], x1) + n                               P(A[nn + j][i], x4) + P(A[nn + j][nn + i], x5)); n         } n         if (maxx != -100000000) n           tot0 += maxx; n         else n           tot0 = -100000000; n       } n       for (int j = 1; j < nn; j++) { n         int maxx = -100000000; n         for (int first = 0; first < 2; first++) { n           int second = 1; n           int x1 = get(first, B[j]), x2 = get(second, B[nn]), n               x3 = get(B[j], B[nn]), x4 = get(first, second), x5 = get(x1, x2); n           if (get(x3, x4) == x5) n             maxx = n                 max(maxx, P(A[j][i], first) + P(A[j][nn + i], x1) + n                               P(A[nn + j][i], x4) + P(A[nn + j][nn + i], x5)); n         } n         if (maxx != -100000000) n           tot1 += maxx; n         else n           tot1 = -100000000; n       } n       cnt += max(tot0, tot1); n     } n     ret = max(ret, cnt); n   } n   printf( %d n , ret); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, flag = 0, j; n   cin >> n; n   while (n--) { n     int x; n     cin >> x; n     flag = 0; n     for (i = 0; i <= 33; i++) { n       for (j = 0; j <= 14; j++) { n         if (i * 3 + j * 7 == x) { n           cout <<  YES  << endl; n           flag = 1; n           break; n         } n       } n       if (flag == 1) break; n     } n     if (flag == 0) cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N; n bool valid(int P, int d, vector<int> guess) { n   if (P >= N) { n     return true; n   } n   if (P == 0) { n     if (valid(P + 1, d, guess)) return true; n     guess[P] -= d; n     if (valid(P + 1, d, guess)) return true; n     guess[P] += d; n     guess[P] += d; n     if (valid(P + 1, d, guess)) return true; n     guess[P] -= d; n   } else { n     if (guess[P] == guess[P - 1]) { n       if (valid(P + 1, d, guess)) return true; n     } else if ((guess[P] - d) == guess[P - 1]) { n       guess[P] -= d; n       if (valid(P + 1, d, guess)) return true; n       guess[P] += d; n     } else if ((guess[P] + d) == guess[P - 1]) { n       guess[P] += d; n       if (valid(P + 1, d, guess)) return true; n       guess[P] -= d; n     } n   } n   return false; n } n int main() { n   cin >> N; n   vector<int> V(N); n   for (int i = 0; i < N; i++) cin >> V[i]; n   for (int d = 0; d <= 100; d++) { n     if (valid(0, d, V)) { n       cout << d <<   n ; n       return 0; n     } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   int64_t n; n   cin >> n; n   int64_t a[n]; n   for (int64_t i = 0; i < n; i++) cin >> a[i]; n   map<int64_t, int64_t> first; n   map<int64_t, int64_t> second; n   first[a[0]] = 1; n   int64_t sum = 0; n   for (int64_t i = 0; i < n; i++) sum += a[i]; n   for (int64_t i = 1; i < n; i++) second[a[i]]++; n   int64_t cur = 0; n   if (sum % 2) { n     cout <<  NO n ; n     return 0; n   } n   int64_t tar = sum / 2; n   for (int64_t i = 0; i < n; i++) { n     cur += a[i]; n     if (cur == tar) { n       cout <<  YES n ; n       return 0; n     } n     if (cur > tar) { n       int64_t x = cur - tar; n       if (first[x] > 0) { n         cout <<  YES n ; n         return 0; n       } n     } n     if (cur < tar) { n       int64_t x = tar - cur; n       if (second[x] > 0) { n         cout <<  YES n ; n         return 0; n       } n     } n     first[a[i + 1]]++; n     second[a[i + 1]]--; n   } n   cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int n, m, q; n int a[1110][810]; n char s[1100]; n int ni[5] = {0, 1, 3, 2, 4}; n long long pow_mod(long long a, long long e) { n   long long res = 1; n   for (; e; a = a * a % mod, e >>= 1) n     if (e & 1) res = res * a % mod; n   return res; n } n int frnum; n const int BASE = 5; n int wr[1100]; n void gauss() { n   for (int i = 1; i <= n; i++) { n     int flag = 0; n     for (int j = i; j <= m; j++) n       if (a[j][i]) { n         for (int k = 1; k <= n + q; k++) swap(a[j][k], a[i][k]); n         flag = 1; n         break; n       } n     if (flag == 0) { n       m++; n       for (int j = 1; j <= n + q; j++) a[m][j] = a[i][j], a[i][j] = 0; n       continue; n     } n     for (int j = i + 1; j <= n + q; j++) n       a[i][j] = (a[i][j] * ni[a[i][i]]) % BASE; n     a[i][i] = 1; n     for (int j = 1; j <= m; j++) n       if (a[j][i] && j != i) { n         for (int k = 1; k <= n + q; k++) n           if (k != i) n             a[j][k] = (a[j][k] + 5 - (a[j][i] * a[i][k]) % BASE) % BASE; n         a[j][i] = 0; n       } n   } n   for (int i = 1; i <= n; i++) n     if (a[i][i] == 0) frnum++; n   for (int i = 1; i <= m; i++) { n     int ff = 0; n     for (int j = 1; j <= n; j++) n       if (a[i][j]) ff = 1; n     if (ff) continue; n     for (int j = n + 1; j <= n + q; j++) n       if (a[i][j]) wr[j - n] = 1; n   } n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= m; j++) a[j][i] = s[j] -  a ; n   } n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= m; j++) a[j][i + n] = s[j] -  a ; n   } n   gauss(); n   for (int i = 1; i <= q; i++) { n     if (wr[i]) n       cout << 0 << endl; n     else n       cout << pow_mod(5, frnum) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, a; n   cin >> n >> a; n   map<int, int> my; n   set<int> rem; n   int ca = 0; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (x == a) { n       ++ca; n     } else { n       if (rem.find(x) != rem.end()) continue; n       my[x]++; n       if (my[x] <= ca) { n         my.erase(x); n         rem.insert(x); n       } n     } n   } n   if (my.empty()) { n     cout <<  -1 n ; n     return 0; n   } else { n     for (auto it : my) { n       if (it.second >= ca) { n         cout << it.first <<   n ; n         return 0; n       } n     } n   } n   cout <<  -1 n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int64_t n, l, d; n   cin >> n >> d >> l; n   int a[25001]; n   for (int i = 1; i <= n; i++) { n     if (d > 0) { n       a[i] = l; n     } else { n       a[i] = 1; n     } n     d = a[i] - d; n   } n   a[n] = a[n] - d; n   if (a[n] <= 0 || a[n] > l) { n     cout << -1; n   } else { n     for (int i = 1; i <= n; i++) { n       cout << a[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const long long int INFLL = 0x3f3f3f3f3f3f3f3fll; n long long int getint() { n   long long int ret = 0; n   bool f = 0; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) f ^= 1; n   while (c >=  0  && c <=  9 ) { n     ret = ret * 10 + c -  0 ; n     c = getchar(); n   } n   return f ? -ret : ret; n } n using namespace std; n int k, p; n long long int l, r; n int mypow(int a, long long int b, int mod) { n   int ret = 1; n   for (; b; b >>= 1, a = (long long int)a * a % mod) n     if (b & 1) ret = (long long int)ret * a % mod; n   return ret; n } n int work() { n   k = getint(); n   l = getint(), r = getint(); n   p = getint(); n   if (p == 2) return (k & 1) ? 0 : 1; n   if (k % p == 0) return k & 1 ? mypow(mypow(2, p - 2, p), r - l, p) : 1; n   int t = mypow(2, l, p - 1), ans; n   t = mypow(k, t, p); n   if ((t - 1) % p == 0) n     ans = mypow(2, r - l + 1, p); n   else { n     int c = mypow(2, r - l + 1, p - 1); n     c = mypow(t, c, p); n     ans = (long long int)(c + p - 1) * mypow(t + p - 1, p - 2, p) % p; n   } n   if (k & 1) ans = (long long int)ans * mypow(mypow(2, p - 2, p), r - l, p) % p; n   return ans; n } n int main() { n   int T = getint(); n   while (T--) printf( %d n , work()); n } n


#include <bits/stdc++.h> n using namespace std; n long long valor[100456], solapacion[100456][320], posheavy[100456], n     resp[100456], arrupd[100456]; n vector<long long> v[100456], colaheavy; n bool light[100456], existe[100456]; n void init() { n   for (int i = 0; i < 100456; ++i) { n     existe[i] = false; n   } n } n void solape(long long m) { n   long long auxcontador = 0; n   for (int i = 0; i < colaheavy.size(); ++i) { n     for (int j = 0; j < v[colaheavy[i]].size(); j++) { n       existe[v[colaheavy[i]][j]] = true; n     } n     for (int j = 0; j < m; j++) { n       auxcontador = 0; n       for (int k = 0; k < v[j].size(); k++) { n         if (existe[v[j][k]] == true) { n           auxcontador++; n         } n       } n       if (colaheavy[i] == j) auxcontador = 0; n       solapacion[j][i] = auxcontador; n     } n     for (int j = 0; j < v[colaheavy[i]].size(); j++) { n       existe[v[colaheavy[i]][j]] = false; n     } n   } n } n void updatelight(long long pos, long long val) { n   pos--; n   for (int i = 0; i < v[pos].size(); ++i) { n     valor[v[pos][i]] += val; n   } n   for (int i = 0; i < colaheavy.size(); ++i) { n     resp[colaheavy[i]] += (solapacion[pos][i]) * val; n   } n } n void updateheavy(long long pos, long long num) { n   pos--; n   resp[pos] += v[pos].size() * num; n   arrupd[pos] += num; n } n void querylight(long long num) { n   long long tot = 0; n   for (int i = 0; i < v[num].size(); ++i) { n     tot += valor[v[num][i]]; n   } n   for (int i = 0; i < colaheavy.size(); ++i) { n     tot += solapacion[num][i] * arrupd[colaheavy[i]]; n   } n   cout << tot << endl; n } n void queryheavy(long long num) { n   long long tot = resp[num]; n   for (int i = 0; i < colaheavy.size(); ++i) { n     tot += solapacion[num][i] * arrupd[colaheavy[i]]; n   } n   cout << tot << endl; n } n int main() { n   long long n, m, queries, aux, aux2, val, num; n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   string txt; n   cin >> n >> m >> queries; n   init(); n   for (int i = 0; i < n; ++i) { n     cin >> valor[i]; n   } n   for (int i = 0; i < m; ++i) { n     cin >> aux; n     light[i] = true; n     if (aux >= sqrt(100456)) { n       posheavy[i] = colaheavy.size(); n       colaheavy.push_back(i); n       light[i] = false; n     } n     resp[i] = 0; n     for (int j = 0; j < aux; j++) { n       cin >> aux2; n       aux2--; n       resp[i] += valor[aux2]; n       v[i].push_back(aux2); n     } n   } n   solape(m); n   for (int i = 0; i < queries; ++i) { n     cin >> txt; n     if (txt[0] ==  ? ) { n       cin >> val; n       val--; n       if (light[val] == true) { n         querylight(val); n       } else { n         queryheavy(val); n       } n     } else { n       cin >> num >> val; n       if (light[num - 1] == true) { n         updatelight(num, val); n       } else { n         updateheavy(num, val); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 201; n int a[maxn], N, L, K; n double f[maxn][maxn][maxn * 2], p[maxn]; n int main() { n   scanf( %d%d%d , &N, &L, &K); n   for (int i = 0; i < N; ++i) { n     scanf( %lf , &p[i]); n     p[i] /= 100; n   } n   for (int i = 0; i < N; ++i) scanf( %d , &a[i]); n   f[0][0][N + min(N, K)] = 1; n   for (int i = 0; i < N; ++i) n     for (int j = 0; j <= N; ++j) n       for (int k = -N; k <= N; ++k) n         if (f[i][j][N + k] > 0) { n           f[i + 1][j + 1][N + min(k + a[i], N)] += p[i] * f[i][j][N + k]; n           f[i + 1][j][N + k] += (1 - p[i]) * f[i][j][N + k]; n         } n   double ans = 0; n   for (int j = L; j <= N; ++j) n     for (int k = N; k <= N + N; ++k) ans += f[N][j][k]; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s[100005]; n int color[100005]; n int main() { n   int n, m; n   while (~scanf( %d%d , &n, &m)) { n     int i, j, tmp = 0; n     int u, v, ans; n     for (i = 1; i <= n; i++) scanf( %d , &color[i]); n     for (j = 0; j < m; j++) { n       scanf( %d%d , &u, &v); n       if (color[u] != color[v]) { n         s[color[v]].insert(color[u]); n         s[color[u]].insert(color[v]); n       } n     } n     ans = color[1]; n     for (i = 1; i <= n; i++) { n       if (s[color[i]].size() > tmp || n           (s[color[i]].size() == tmp && color[i] < ans)) { n         tmp = s[color[i]].size(); n         ans = color[i]; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n const long long INF = 0x3f3f3f3f3f3f3f3f; n const long long llinf = (1LL << 61); n const int inf = (1 << 30); n const int nmax = 4e5 + 50; n const int mod = 1e9 + 7; n using namespace std; n string s; n long long n, a[nmax], st[4 * nmax], i, lzy[4 * nmax], x, q, st1[4 * nmax], n     ms[4 * nmax], msk[nmax], m, ans, j, inv[100], l, r, sv[500], x1; n long long pr[] = {2,   3,   5,   7,   11,  13,  17,  19,  23,  29,  31, n                   37,  41,  43,  47,  53,  59,  61,  67,  71,  73,  79, n                   83,  89,  97,  101, 103, 107, 109, 113, 127, 131, 137, n                   139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, n                   197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, n                   263, 269, 271, 277, 281, 283, 293}; n long long pw(long long n, long long p) { n   long long tmp = n, ans = 1, mx; n   if (p == mod - 2) n     mx = 31; n   else n     mx = 18; n   for (int i = 0; i <= mx; i++) { n     if (p & (1LL << i)) ans = (ans * tmp) % mod; n     tmp = (tmp * tmp) % mod; n   } n   return ans; n } n void build(int x, int l, int r) { n   if (l == r) { n     st[x] = a[l]; n     st1[x] = msk[l]; n     return; n   } n   int mid = (l + r) / 2; n   build(2 * x, l, mid); n   build(2 * x + 1, mid + 1, r); n   st[x] = st[2 * x] * st[2 * x + 1]; n   st[x] %= mod; n   st1[x] = st1[2 * x] | st1[2 * x + 1]; n } n void push(int nod, int l, int r) { n   if (!lzy[nod]) return; n   if (lzy[nod] != 1) st[nod] = (st[nod] * pw(lzy[nod], r - l + 1)) % mod; n   st1[nod] |= ms[nod]; n   if (l != r) { n     if (lzy[nod] != 1) { n       lzy[nod * 2] *= lzy[nod]; n       lzy[nod * 2 + 1] *= lzy[nod]; n       lzy[nod * 2] %= mod; n       lzy[nod * 2 + 1] %= mod; n     } n     ms[nod * 2] |= ms[nod]; n     ms[nod * 2 + 1] |= ms[nod]; n   } n   lzy[nod] = 1; n } n void upd(int nod, int l, int r, int tl, int tr, long long v, long long m) { n   push(nod, l, r); n   if (tl <= l && r <= tr) { n     lzy[nod] = (lzy[nod] * v) % mod; n     ms[nod] |= m; n     push(nod, l, r); n     return; n   } n   int mid = (l + r) / 2; n   if (tl <= mid) upd(nod * 2, l, mid, tl, tr, v, m); n   if (mid < tr) upd(nod * 2 + 1, mid + 1, r, tl, tr, v, m); n   push(nod * 2, l, mid); n   push(nod * 2 + 1, mid + 1, r); n   st[nod] = st[nod * 2] * st[nod * 2 + 1]; n   st[nod] %= mod; n   st1[nod] = st1[nod * 2] | st1[nod * 2 + 1]; n } n pair<long long, long long> qry(int nod, int l, int r, int tl, int tr) { n   push(nod, l, r); n   if (tl <= l && r <= tr) return {st[nod], st1[nod]}; n   int mid = (l + r) / 2; n   pair<long long, long long> vl, x; n   vl.first = 1, vl.second = 0; n   if (tl <= mid) { n     x = qry(nod * 2, l, mid, tl, tr); n     vl.first = (vl.first * x.first) % mod; n     vl.second |= x.second; n   } n   if (mid < tr) { n     x = qry(nod * 2 + 1, mid + 1, r, tl, tr); n     vl.first = (vl.first * x.first) % mod; n     vl.second |= x.second; n   } n   return vl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cerr.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (j = 0; j < 62; j++) { n     inv[j] = pw(pr[j], mod - 2); n     sv[pr[j]] = j; n   } n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     for (j = 0; j < 62; j++) n       if (a[i] % pr[j] == 0) msk[i] |= (1LL << j); n   } n   for (i = 1; i <= 1600000; i++) lzy[i] = 1; n   build(1, 1, n); n   while (q--) { n     cin >> s; n     if (s[0] ==  M ) { n       cin >> l >> r >> x; n       x1 = x, m = 0; n       for (j = 0; j < 62; j++) n         if (x % pr[j] == 0) m |= (1LL << j); n       upd(1, 1, n, l, r, x, m); n     } else { n       cin >> l >> r; n       pair<long long, long long> p = qry(1, 1, n, l, r); n       ans = p.first; n       for (j = 0; j < 62; j++) { n         if (p.second & (1LL << j)) { n           ans = (ans * (1 - inv[j] + mod)) % mod; n         } n       } n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, l; n long long dp[250005]; n short big_arr[250005], small_arr[5001]; n long long pre[51], sum[51], suf[51], mx[51], maxx = -1e5; n long long solve(int indx, long long curSum) { n   if (indx == m + 1) return -2000; n   short &i = big_arr[indx]; n   long long &res = dp[indx]; n   if (~res) return res; n   res = pre[i]; n   if (curSum + sum[i] >= suf[i] && curSum + sum[i] > 0) n     res = max(res, solve(indx + 1, curSum + sum[i]) + sum[i]); n   else if (suf[i] > 0) n     res = max(res, solve(indx + 1, suf[i]) + suf[i] - curSum); n   else n     res = max(res, solve(indx + 1, 0) - curSum); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL), cout.tie(NULL); n   cin >> n >> m; n   fill(pre, pre + 51, -1e5); n   fill(suf, suf + 51, -1e5); n   fill(mx, mx + 51, -1e5); n   fill(dp, dp + 250005, -1); n   for (int i = 0; i < n; i++) { n     cin >> l; n     long long tmp = 0; n     for (int j = 0; j < l; j++) { n       cin >> small_arr[j]; n       mx[i] = max((long long)small_arr[j], mx[i]); n       if (tmp < 0) tmp = 0; n       tmp += small_arr[j]; n       mx[i] = max(mx[i], tmp); n       sum[i] += small_arr[j]; n       pre[i] = max(pre[i], sum[i]); n     } n     tmp = 0; n     for (int j = l - 1; j >= 0; j--) { n       tmp += small_arr[j]; n       suf[i] = max(suf[i], tmp); n     } n   } n   for (int i = 1; i <= m; i++) { n     cin >> big_arr[i]; n     big_arr[i]--; n     maxx = max(mx[big_arr[i]], maxx); n   } n   cout << max(solve(1, 0), maxx) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 9901; n template <class T> n int countbit(T n) { n   int t = 0; n   while (n) n &= n - 1, ++t; n   return t; n } n template <class T> n T lowbit(T n) { n   return n & (-n); n } n const double eps = 1e-8; n const double pi = acos(-1.0); n const int INF = 0x7f7f7f7f; n const int maxn = 111111; n void File() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n long long c[66][66]; n long long go(long long n, long long t) { n   int cnt = 0; n   long long tt = t; n   while (tt) tt >>= 1, ++cnt; n   int arr[65]; n   int i, size = 0; n   while (n) arr[size++] = n & 1, n >>= 1; n   reverse(arr, arr + size); n   long long ans = 0; n   bool flag; n   int ct = count(arr, arr + size, 1); n   for (i = 0; i < size; ++i) { n     flag = true; n     if (arr[i]) { n       if (size - i == ct && ct == cnt && cnt) n         ans++, flag = false; n       else { n         if (cnt >= 0) ans += c[size - i - 1][cnt], --cnt; n       } n       --ct; n     } n     if (i && !cnt) { n       ans++; n       break; n     } n     if (!flag) break; n   } n   if (t == 1) ans--; n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   int i, j; n   for (i = 0; i < 66; ++i) { n     c[i][i] = c[i][0] = 1; n   } n   for (i = 1; i < 66; ++i) { n     for (j = 1; j < i; ++j) { n       c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; n     } n   } n   long long n, t; n   cin >> n >> t; n   int k = countbit(t); n   if (k == 1) n     cout << go(n + 1, t) << endl; n   else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n struct Point { n   int x, y; n   Point* next; n   bool operator<(const Point& p) const { return 1ll * p.x * y < 1ll * x * p.y; } n } P[200010], *np = P; n std::map<long long, Point*> fir; n std::map<Point, int> M; n int main() { n   int q, s = 0; n   scanf( %d , &q); n   while (q--) { n     int t, x, y; n     scanf( %d%d%d , &t, &x, &y); n     long long d = 1ll * x * x + 1ll * y * y; n     if (t == 1) { n       M[(Point){x, y}]++; n       for (Point* p = fir[d]; p; p = p->next) n         M[(Point){x + p->x, y + p->y}] += 2; n       *np = (Point){x, y, fir[d]}; n       fir[d] = np++; n       s++; n     } n     if (t == 2) { n       M[(Point){x, y}]--; n       if (fir[d] && fir[d]->x == x && fir[d]->y == y) fir[d] = fir[d]->next; n       ; n       for (Point* p = fir[d]; p; p = p->next) { n         if (p->next && p->next->x == x && p->next->y == y) n           p->next = p->next->next; n         ; n         M[(Point){x + p->x, y + p->y}] -= 2; n       } n       s--; n     } n     if (t == 3) printf( %d n , s - M[(Point){x, y}]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long expo(long long x, long long n, long long m) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return expo((x * x) % m, n / 2, m); n   else n     return (x * expo((x * x) % m, (n - 1) / 2, m)) % m; n } n string DtoB(int j) { n   string s =   ; n   for (int i = 32; i >= 0; i--) { n     int k = j >> i; n     if (k & 1) n       s +=  1 ; n     else n       s +=  0 ; n   } n   return s; n } n void solve() { n   int n; n   cin >> n; n   int arr[n]; n   int s[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     s[i] = arr[i]; n   } n   sort(s, s + n); n   map<int, int> m; n   for (int i = 0; i < n; i++) m[s[i]] = i; n   vector<vector<int> > ans; n   int visit[n]; n   memset(visit, 0, sizeof(visit)); n   ; n   for (int i = 0; i < n; i++) { n     if (visit[i] == 0) { n       vector<int> p; n       for (int j = i; visit[j] == 0; j = m[arr[j]]) { n         p.push_back(j + 1); n         visit[j] = 1; n       } n       ans.push_back(p); n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i].size() <<    ; n     for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ; n     cout << endl; n   } n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:320000000 ) n using namespace std; n const double EPS = 1e-9; n const int INF = 0x7f7f7f7f; n const double PI = acos(-1.0); n template <class T> n inline T Abs(T n) { n   return ((n) < 0 ? -(n) : (n)); n } n template <class T> n inline T Max(T x, T y) { n   return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; n } n template <class T> n inline T Min(T x, T y) { n   return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; n } n template <class T> n inline T Swap(T &a, T &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n template <class T> n inline T gcd(T a, T b) { n   return (b) == 0 ? (a) : gcd((b), ((a) % (b))); n } n template <class T> n inline T lcm(T a, T b) { n   return ((a) / gcd((a), (b)) * (b)); n } n template <class T> n inline T bigmod(T p, T e, T M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n struct an { n   int a; n   int d; n   bool operator<(const an &t) const { return d < t.d; } n }; n int main() { n   int i, j, k, n, a[200]; n   int miniind, maxiind, maxi, mini; n   while (scanf( %d , &n) != EOF) { n     maxi = 0; n     mini = 200; n     for (i = 0; i < n; i++) { n       scanf( %d , &a[i]); n       if (a[i] > maxi) { n         maxi = a[i]; n         maxiind = i; n       } n       if (a[i] < mini) { n         mini = a[i]; n         miniind = i; n       } n     } n     printf( %d n , n            max(n - maxiind - 1, max(maxiind, max(n - miniind - 1, miniind)))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     long long n, g, b; n     cin >> n >> g >> b; n     if (g >= b) { n       cout << n << endl; n       continue; n     } n     long long good_need = n / 2; n     if (n % 2 == 1) good_need += 1; n     good_need -= g; n     long long bad_poss = n / 2; n     long long periods = bad_poss / b; n     if (good_need - periods * g <= 0) { n       cout << n << endl; n       continue; n     } n     good_need -= periods * g; n     long long restperiods = good_need / g; n     long long rest = 0; n     if (good_need % g != 0) { n       rest = good_need % g + b; n     } n     cout << g + (periods + restperiods) * (g + b) + rest << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c, d; n   while (scanf( %d , &n) != EOF) { n     int j, x, y, flag = 0; n     for (int i = 1; i <= 4; i++) { n       scanf( %d%d%d%d , &a, &b, &c, &d); n       a = min(a, b); n       b = min(c, d); n       if (a + b <= n) { n         x = a; n         y = n - x; n         j = i; n         flag = 1; n       } n     } n     if (flag) n       printf( %d %d %d n , j, x, y); n     else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int c0, c1, c2, c3; n   cin >> c0 >> c1 >> c2 >> c3; n   if (c1 - c0 == c2 - c1 && c2 - c1 == c3 - c2) { n     cout << c3 + c1 - c0 << endl; n   } else if (c1 * c1 == c0 * c2 && c2 * c2 == c1 * c3 && !((c3 * c3) % c2)) { n     cout << c3 * c1 / c0 << endl; n   } else { n     cout << 42 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int w[1501000], cnt[1501000]; n int main() { n   int N, i; n   int po, ans; n   scanf( %d , &N); n   for (int i = (0); i < (N); i++) scanf( %d , w + i); n   sort(w, w + N); n   memset(cnt, 0, sizeof(cnt)); n   for (int i = (0); i < (N); i++) cnt[w[i]]++; n   po = 0; n   ans = 0; n   for (int po = (0); po < (1501000); po++) { n     if (cnt[po] == 0) continue; n     cnt[po + 1] += cnt[po] / 2; n     if (cnt[po] % 2) ans++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int h, m, n, vn; n map<int, pair<int, int> > M; n int group[222222], pos[222222], host[222222]; n vector<int> v[222222], first[222222]; n inline void add(int i, int x, int d) { n   int m = first[i].size(); n   while (x < m) { n     first[i][x] += d; n     x = x | (x + 1); n   } n } n inline int sum(int i, int x) { n   int m = first[i].size(), res = 0; n   while (x >= 0) { n     res += first[i][x]; n     x = (x & (x + 1)) - 1; n   } n   return res; n } n int main() { n   memset(host, -1, sizeof(host)); n   scanf( %d%d%d n , &h, &m, &n); n   memset(group, -1, sizeof(group)); n   for (int i = 0; i < h; ++i) n     if (group[i] == -1) { n       for (int j = i; group[j] == -1; j = (j + m) % h) { n         pos[j] = v[vn].size(); n         group[j] = vn; n         v[vn].push_back(j); n       } n       ++vn; n     } n   for (int i = 0; i < vn; ++i) { n     first[i].resize(int(v[i].size())); n     for (int j = 0; j < v[i].size(); ++j) add(i, j, 1); n   } n   long long res = 0; n   for (int i = 0; i < n; ++i) { n     char ch; n     int id, hash; n     scanf( %c %d , &ch, &id); n     if (ch ==  + ) { n       scanf( %d , &hash); n       int j = group[hash]; n       int k = pos[hash]; n       int l = k - 1, r = v[j].size(); n       int base = sum(j, k - 1); n       while (r - l > 1) { n         int mid = (l + r) / 2; n         if (sum(j, mid) > base) n           r = mid; n         else n           l = mid; n       } n       if (r < v[j].size()) { n         M[id] = make_pair(j, r); n         add(j, r, -1); n         res += r - k; n       } else { n         l = -1, r = k; n         while (r - l > 1) { n           int mid = (l + r) / 2; n           if (sum(j, mid) > 0) n             r = mid; n           else n             l = mid; n         } n         M[id] = make_pair(j, r); n         add(j, r, -1); n         res += v[j].size() - k + r; n       } n     } else if (ch ==  - ) { n       int j = M[id].first; n       int k = M[id].second; n       add(j, k, 1); n     } n     scanf(  n ); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, Max, reg; n int TIME = 0; n vector<int> T, W; n bool used[1010]; n int p[1010], dmg[1010]; n int main() { n   int i, j, k, l, x, y; n   cin >> n >> Max >> reg; n   for (i = 0; i < n; ++i) cin >> p[i] >> dmg[i]; n   int ch = Max, cd = 0; n   while (1) { n     ch -= cd; n     ch = min(Max, ch + reg); n     if (ch <= 0) break; n     j = -1; n     for (i = 0; i < n; ++i) { n       if (used[i] || p[i] * Max < 100 * ch) continue; n       if (j == -1 || dmg[j] < dmg[i]) j = i; n     } n     if (j != -1 && dmg[j] >= 0) { n       if (dmg[j] > cd || true) { n         T.push_back(TIME); n         W.push_back(j + 1); n         used[j] = true; n         cd += dmg[j]; n       } n     } n     ++TIME; n     if (TIME >= 10000) break; n   } n   if (ch > 0) n     cout <<  NO n ; n   else { n     cout <<  YES n ; n     cout << TIME <<     << T.size() << endl; n     for (i = 0; i < T.size(); ++i) cout << T[i] <<     << W[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 10; n const int N = 2e5; n using namespace std; n long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } n long long qmul(long long p, long long q, long long mo) { n   long long f = 0; n   while (q) { n     if (q & 1) f = (f + p) % mo; n     p = (p + p) % mo; n     q >>= 1; n   } n   return f; n } n long long qpow(long long p, long long q) { n   long long f = 1; n   while (q) { n     if (q & 1) f = f * p % 19260817; n     p = p * p % 19260817; n     q >>= 1; n   } n   return f; n } n int n, m, k, t; n int main() { n   int i, j; n   scanf( %d , &n); n   for (i = n;; i--) { n     if (n % i) continue; n     j = i; n     int cnta = 0, cntb = 0; n     while (j && j % 2 == 0) cnta++, j /= 2; n     while (j && j % 2 == 1) cntb++, j /= 2; n     if (!j && cntb == cnta + 1) return 0 * printf( %d n , i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 65; n long long S, L, ans; n long long P[N] = {1}; n long long f[N][N * 2][2]; n int main() { n   scanf( %lld , &S); n   L = log2(S + 1); n   for (int i = 1; i < N; ++i) P[i] = P[i - 1] << 1; n   for (int h = 1; h <= L; ++h) { n     long long x = S % (P[h] - 1); n     for (int i = h; i; --i) n       if (x >= P[i] - 1) x -= P[i] - 1; n     ans += (!x); n   } n   for (int h0 = 1; h0 < L; ++h0) n     for (int h1 = 1; S + 1 - P[h1] >= P[h0 + 1] + P[h1 + 1] - 3; ++h1) { n       long long x = (S + 1 - P[h1]) / (P[h0 + 1] + P[h1 + 1] - 3); n       long long r = (S + 1 - P[h1]) % (P[h0 + 1] + P[h1 + 1] - 3); n       if (!r) { n         ans++; n         continue; n       } n       if (h0 == 1 && h1 == 1) { n         ans += (S == x * 5 + 1); n         continue; n       } n       for (int n = 1; n <= h0 + h1; ++n) { n         long long C = r + n, L = log2(C) + 1; n         if (C & 1) continue; n         memset(f[0], 0, sizeof f[0]); n         f[0][0][0] = 1; n         for (int i = 1; i <= L; ++i) { n           int d = (C >> i) & 1; n           memset(f[i], 0, sizeof f[i]); n           for (int j = 0; j <= i + i - 2 && j <= n; ++j) n             for (int k = 0; k < 2; ++k) n               if (f[i - 1][j][k]) n                 for (int x = 0; x < 2; ++x) n                   if (!x || i < h0) n                     for (int y = 0; y < 2; ++y) n                       if (!y || i < h1) n                         if (((k + x + y) & 1) == d) n                           f[i][j + x + y][(k + x + y) >> 1] += f[i - 1][j][k]; n         } n         ans += f[L][n][0]; n       } n     } 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   cin >> n; n   if (n & 1) { n     cout << -1; n     return 0; n   } n   if (n < 3) { n     cout <<  bb nww n nbb nww ; n     return 0; n   } n   string s1, s2; n   for (int i = 0; i < 110; i++) s1 += (i % 4 < 2) ?  w  :  b ; n   s2 = s1.substr(2); n   s1 = s1.substr(0, n); n   s2 = s2.substr(0, n); n   for (int i = 0; i < n; i++) { n     int x = i & 1; n     for (int j = 0; j < n; j++) { n       int y = (j % 4) < 2 ? 1 : 0; n       if (x ^ y) n         cout << s1 <<   n ; n       else n         cout << s2 <<   n ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[4], i, j, s; n int main() { n   for (i = 0; i < 4; i++) { n     scanf( %d , &a[i]); n     s += a[i]; n   } n   for (i = 0; i < 4; i++) { n     if (a[i] == s - a[i]) { n       puts( YES ); n       return 0; n     } n     for (j = i + 1; j < 4; j++) n       if (a[i] + a[j] == s - a[i] - a[j]) { n         puts( YES ); n         return 0; n       } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static inline void amin(int &a, int b) { n   if (a > b) a = b; n } n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   int A[N]; n   for (int i = 0; i < N; ++i) { n     scanf( %d , &A[i]); n   } n   pair<int, int> Q[M]; n   for (int i = 0; i < M; ++i) { n     scanf( %d%d , &Q[i].first, &Q[i].second); n     Q[i].first--; n     Q[i].second--; n   } n   int res = *max_element(A, A + N) - *min_element(A, A + N); n   list<int> res_q; n   int changes[N]; n   for (int mx = 0; mx < N; ++mx) { n     list<int> q; n     memset(changes, 0, sizeof changes); n     for (int i = 0; i < M; ++i) n       if (mx < Q[i].first || Q[i].second < mx) { n         q.push_back(i); n         for (int j = Q[i].first; j < Q[i].second + 1; ++j) changes[j]--; n       } n     int mn = numeric_limits<int>::max(); n     for (int i = 0; i < N; ++i) amin(mn, A[i] + changes[i]); n     if (res < A[mx] - mn) { n       res = A[mx] - mn; n       res_q = q; n     } n   } n   printf( %d n%d n , res, (int)res_q.size()); n   for (auto it : res_q) printf( %d  , it + 1); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n template <class T, class U> n istream &operator>>(istream &in, pair<T, U> &p) { n   in >> p.first >> p.second; n   return in; n } n template <class T, class U> n ostream &operator<<(ostream &out, const pair<T, U> &p) { n   out << p.first <<     << p.second; n   return out; n } n template <class T> n istream &operator>>(istream &in, vector<T> &v) { n   for (auto &i : v) { n     in >> i; n   } n   return in; n } n template <class T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   for (auto &i : v) { n     out << i <<    ; n   } n   return out; n } n inline void io(int x = 15) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   srand(time(0)); n   cout << fixed << setprecision(x); n } n vector<int> prefix_function(string s) { n   int n = s.length(); n   vector<int> pi(n); n   for (int i = 1; i < n; ++i) { n     int j = pi[i - 1]; n     while (j > 0 && s[i] != s[j]) { n       j = pi[j - 1]; n     } n     if (s[i] == s[j]) ++j; n     pi[i] = j; n   } n   return pi; n } n bool prime_test(long long n) { n   for (long long i = 2; i * i <= n; ++i) n     if ((n >= 0 && n < 2) || n % i == 0) return false; n   return true; n } n void prime_nums(int n, vector<char> &prime) { n   prime[0] = prime[1] = false; n   for (long long i = 2; i * i <= n; ++i) { n     if (prime[i]) { n       for (int j = i * i; j <= n; j += i) { n         prime[j] = false; n       } n     } n   } n } n long long bin_pow(long long a, long long b, long long m = 0) { n   if (m == 0) n     return b == 0 ? 1 : (b % 2 ? bin_pow(a, b - 1) * a : bin_pow(a * a, b / 2)); n   if (b == 0) return 1; n   long long temp = bin_pow(a, b / 2, m); n   if (b % 2 == 0) n     return (temp * temp) % m; n   else n     return (a * temp * temp) % m; n } n bool nextperm(vector<char> &a) { n   int n = a.size(); n   for (int i = n - 2; i >= 0; --i) { n     if (a[i] < a[i + 1]) { n       int mnv = a[i + 1], mni = i + 1; n       for (int j = i + 2; j < n; ++j) { n         if (a[j] < mnv && a[j] > a[i]) { n           mni = j; n           mnv = a[j]; n         } n       } n       swap(a[i], a[mni]); n       reverse(a.begin() + i + 1, a.end()); n       return true; n     } n   } n   return false; n } n int main() { n   io(); n   int n, s; n   cin >> n >> s; n   vector<int> used(n); n   for (int i = 0; i < n - 1; ++i) { n     int l, r; n     cin >> l >> r; n     ++used[--l]; n     ++used[--r]; n   } n   int x = 0; n   for (int i = 0; i < n; ++i) { n     if (used[i] == 1) { n       ++x; n     } n   } n   cout << (long double)s / x * 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long n; n long long len[1000005]; n long long pai[1000005]; n string s; n long long find(long long x) { n   if (pai[x] == x) return x; n   return pai[x] = find(pai[x]); n } n void merge(long long a, long long b) { n   long long paiA = find(a), paiB = find(b); n   pai[paiB] = paiA; n } n int32_t main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(0); n   cin >> n >> s; n   long long p = 0, b = 0; n   pai[0] = 0; n   for (long long i = 1; i <= n; i++) { n     pai[i] = i; n     if (s[i - 1] ==  1 ) { n       p++; n       b = 0; n     } else if (s[i - 1] ==  0 ) { n       b++; n       p = 0; n     } else { n       p++, b++; n     } n     len[i - 1] = max(p, b); n   } n   for (long long i = 1; i <= n; i++) { n     long long res = 0, pos = 0; n     while (pos < n) { n       pos += i - 1; n       while (pos < n and len[pos] < i) { n         merge(pos + 1, pos); n         pos = find(pos); n       } n       if (pos >= n) break; n       pos++; n       res++; n     } n     cout << res <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const bound = 2e6 + 2, N = 5e5 + 2; n long long OO = 1e18; n double eps = 1e-6; n int oo = 1e9, mod = oo + 7; n pair<int, int> can[101]; n bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n   if (a.first.first != b.first.first) return a.first.first > b.first.first; n   return a.first.second < b.first.second; n } n int main() { n   int n, m, k, a; n   cin >> n >> k >> m >> a; n   for (int(i) = 0; (i) < (a); (i)++) { n     int g; n     cin >> g; n     g--; n     can[g].first++; n     can[g].second = i; n   } n   for (int(i) = 0; (i) < (n); (i)++) { n     pair<pair<int, int>, int> tmp[101]; n     for (int(j) = 0; (j) < (n); (j)++) tmp[j] = {can[j], j}; n     int sv = tmp[i].first.second; n     tmp[i].first.first += m - a; n     if (m - a) tmp[i].first.second = m; n     sort(tmp, tmp + n, cmp); n     bool no = 1; n     for (int(j) = 0; (j) < (k); (j)++) { n       if (tmp[j].first.first && tmp[j].second == i) { n         no = 0; n         break; n       } n     } n     if (no) { n       cout << 3 <<    ; n       continue; n     } n     int to = -1, rem = m - a; n     for (int(j) = 0; (j) < (n); (j)++) n       if (tmp[j].second == i) { n         tmp[j].first.first -= m - a; n         tmp[j].first.second = sv; n       } n     sort(tmp, tmp + n, cmp); n     for (int(j) = 0; (j) < (n); (j)++) { n       if (to != -1) { n         if (rem < to - tmp[j].first.first + 1) break; n         rem -= to - tmp[j].first.first + 1; n         tmp[j].first.first += to - tmp[j].first.first + 1; n         tmp[j].first.second = m; n       } n       if (tmp[j].second == i) to = tmp[j].first.first; n     } n     sort(tmp, tmp + n, cmp); n     no = 1; n     for (int(j) = 0; (j) < (k); (j)++) n       if (tmp[j].first.first && tmp[j].second == i) no = 0; n     if (no) n       cout << 2 <<    ; n     else n       cout << 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int z[26][200001], w[26]; n int main() { n   int n, m, i, j, a, le; n   char str[200001]; n   scanf( %d %d , &n, &m); n   scanf( %s , str); n   for (i = 0; str[i]; i++) { n     z[str[i] -  a ][w[str[i] -  a ]] = i; n     w[str[i] -  a ]++; n   } n   long long ans; n   int mi; n   while (n) { n     n--; n     ans = 0; n     scanf( %s , str); n     le = strlen(str); n     for (i = 0; str[i]; i++) { n       a = str[i] -  a ; n       if (w[a] == 0) n         ans += le; n       else { n         mi = abs(i - z[a][0]); n         ; n         for (j = 1; j < w[a]; j++) { n           mi = min(mi, abs(i - z[a][j])); n         } n         ans += mi; n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9 + 100; n const long double eps = 1e-11; n const long double pi = acos(-1.0L); n int myrand() { return rand(); } n unsigned rdtsc() { n   unsigned ans; n   asm( rdtsc  :  =a (ans)); n   return ans; n } n int rnd(int x) { return myrand() % x; } n void precalc() {} n int n, k; n int a, b, c, d; n bool read() { n   if (scanf( %d%d , &n, &k) < 2) { n     return false; n   } n   scanf( %d%d%d%d , &a, &b, &c, &d); n   return true; n } n void solve() { n   if (n <= 4 || k < n + 1) { n     printf( -1 n ); n     return; n   } n   vector<int> path; n   for (int i = 1; i <= n; i++) { n     if (i != a && i != b && i != c && i != d) { n       path.push_back(i); n     } n   } n   printf( %d %d  , a, c); n   for (int i = 0; i < ((int)((path).size())); i++) { n     printf( %d  , path[i]); n   } n   printf( %d %d n , d, b); n   printf( %d %d  , c, a); n   for (int i = 0; i < ((int)((path).size())); i++) { n     printf( %d  , path[i]); n   } n   printf( %d %d n , b, d); n } n int main() { n   srand(rdtsc()); n   precalc(); n   while (true) { n     if (!read()) { n       break; n     } n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   cout << a[0]; n   for (int e = 1; e < a.size(); e++) n     if (a[e] < b[0]) n       cout << a[e]; n     else n       break; n   cout << b[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   map<int, int> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i != j) { n         ++m[a[i] + a[j]]; n       } n     } n   } n   int Max = 0; n   for (auto it = m.begin(); it != m.end(); it++) { n     Max = max(it->second / 2, Max); n   } n   cout << Max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   string In; n   vector<char> Que; n   cin >> n >> t >> In; n   for (char x : In) { n     Que.push_back(x); n   } n   for (int i = 0; i < t; i++) { n     for (int x = 0; x < n; x++) { n       if (Que[x] ==  B  && Que[x + 1] ==  G ) { n         Que[x] =  G ; n         Que[x + 1] =  B ; n         x++; n       } n     } n   } n   for (char x : Que) { n     cout << x; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 100011; n const int MAX = 1e9; n int mi[SIZE << 2], a[SIZE]; n void init(int L, int R, int id) { n   mi[id] = MAX; n   if (L == R) return; n   int mm = (L + R) >> 1; n   init(L, mm, id << 1); n   init(mm + 1, R, (id << 1) + 1); n } n void ins(int L, int R, int id, int x, int v) { n   if (L == R) { n     mi[id] = v; n     return; n   } n   int mm = (L + R) >> 1; n   if (x <= mm) n     ins(L, mm, id << 1, x, v); n   else n     ins(mm + 1, R, (id << 1) + 1, x, v); n   mi[id] = min(mi[id << 1], mi[(id << 1) | 1]); n } n int qq(int L, int R, int id, int l1, int rr) { n   if (L >= l1 && R <= rr) return mi[id]; n   if (R < l1 || L > rr) return MAX; n   int mm = (L + R) >> 1; n   return min(qq(L, mm, id << 1, l1, rr), qq(mm + 1, R, (id << 1) | 1, l1, rr)); n } n int main() { n   int n, s, l; n   scanf( %d%d%d , &n, &s, &l); n   init(0, n, 1); n   ins(0, n, 1, 0, 0); n   multiset<int> H; n   int lt = 1, an; n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     a[i] = x; n     H.insert(x); n     while (*H.rbegin() - *H.begin() > s) { n       H.erase(H.find(a[lt++])); n     } n     if (i - lt + 1 >= l) { n       ins(0, n, 1, i, an = qq(0, n, 1, lt - 1, i - l) + 1); n     } else n       an = MAX; n   } n   if (an >= MAX) n     puts( -1 ); n   else n     printf( %d n , an); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, start, x; n pair<int, int> ask(int v) { n   printf( ? %d n , v); n   fflush(stdout); n   int value, nxt; n   scanf( %d %d , &value, &nxt); n   return pair<int, int>(value, nxt); n } n const int RANDOM_Q = 1000; n int main() { n   srand(time(NULL)); n   scanf( %d %d %d , &n, &start, &x); n   vector<int> v; n   for (int i = int(1); i < int(n + 1); i++) { n     v.push_back(i); n   } n   random_shuffle(v.begin(), v.end()); n   vector<int> randoms; n   for (int i = int(0); i < int(min(RANDOM_Q, (int)v.size())); i++) { n     randoms.push_back(v[i]); n   } n   int ans = (int)2e9; n   int possible_start = start, start_val = -1; n   for (auto &each : randoms) { n     pair<int, int> p = ask(each); n     if (p.first >= x && p.first < ans) { n       ans = p.first; n     } else if (p.first < x && p.first > start_val) { n       possible_start = p.second; n       start_val = p.first; n     } n   } n   int cur = possible_start; n   for (int i = 0; cur != -1 && i < 1999 - RANDOM_Q; i++) { n     pair<int, int> p = ask(cur); n     if (p.first >= x) { n       ans = p.first; n       break; n     } n     cur = p.second; n   } n   printf( ! %d n , ans == (int)2e9 ? -1 : ans); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n #define ll long long n using namespace std; n  n int main() n { n     ios_base::sync_with_stdio(0) ; n     cin.tie(0) ; n     cout.tie(0) ; n     int t; n     cin>>t; n     while(t--) n     { n         ll n,x,s=0,a=1; n         cin>>n>>x; n         ll arr[n]; n         for(int i=0; i<n; i++) n         { n             cin>>arr[i]; n         } n  n         int mn=100000000,gg=mn,ind=-1; n  n         for(int i=0; i<n; i++) n         { n             int y=arr[i],cnt=0; n             while(!(y%x)) n             { n                 y/=x; n                 cnt++; n             } n             if(cnt<mn) n             { n                 mn=cnt; n                 ind=i; n             } n         } n  n         for(int i=0; i<n; i++) n         { n             if(i<ind) n             gg=mn+1; n             else n                 gg=mn; n             s+=arr[i]; n             a=1; n             while(((arr[i]%x)==0)&&(gg--)) n             { n                 a*=x; n                 arr[i]/=x; n                 s+=a*arr[i]; n             } n         } n  n         cout<<s<<  n ; n  n     } n  n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   int c = 0; n   for (int j = 0; j < str.length(); j++) { n     if (str[j] ==  @ ) { n       c = c + 1; n     } n   } n   if (c == 0) { n     cout <<  No solution  << endl; n   } else if (c > 0) { n     int c1 = 1; n     int i = 0; n     for (; i < str.length(); i++) { n       char ch = str[i]; n       if (ch ==  @  and (i == 0 or i == (str.length() - 1))) { n         break; n       } else if (ch ==  @  and (i > 0 and i < (str.length() - 1))) { n         char ch1 = str[i - 1]; n         char ch2 = str[i + 1]; n         if (ch1 ==  ,  or ch2 ==  , ) { n           break; n         } n         if (ch1 !=  ,  and ch2 !=  ,  and c1 != c) { n           string str1 = str.substr(0, i + 2); n           string str2 = str.substr(i + 2); n           str = str1 +  ,  + str2; n           c1 = c1 + 1; n         } n         if (ch1 !=  ,  and ch2 !=  ,  and c1 == c) { n           continue; n         } n       } n     } n     if (i < str.length()) { n       cout <<  No solution  << endl; n     } n     if (i >= str.length()) { n       cout << str << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> h(n); n   for (int i = 0; i < n; i++) cin >> h[i]; n   vector<pair<int, int> > v(n); n   for (int i = 0; i < n; i++) { n     v[i] = make_pair(h[i], i); n   } n   sort(v.begin(), v.end()); n   sort(h.begin(), h.end()); n   h.erase(unique(h.begin(), h.end()), h.end()); n   if (n - h.size() < 2) { n     cout <<  NO  << endl; n     return 0; n   } else { n     cout <<  YES  << endl; n   } n   map<int, int> x; n   for (int i = 0; i + 1 < n; i++) { n     if (v[i].first == v[i + 1].first) { n       x[v[i].first] += 1; n     } n   } n   int type = x.size(); n   for (int i = 0; i < 3; i++) { n     if (i == 0) { n       for (int j = 0; j < n; j++) { n         printf( %d  , v[j].second + 1); n       } n       printf(  n ); n     } else { n       if (type == 1) { n         vector<pair<int, int> >::iterator it = n             lower_bound(v.begin(), v.end(), make_pair(x.begin()->first, -1)); n         swap(v[it - v.begin()], v[it - v.begin() + i]); n         for (int j = 0; j < n; j++) { n           printf( %d  , v[j].second + 1); n         } n         printf(  n ); n       } else { n         vector<pair<int, int> >::iterator it = n             lower_bound(v.begin(), v.end(), make_pair(x.begin()->first, -1)); n         x.erase(x.begin()); n         swap(v[it - v.begin()], v[it - v.begin() + 1]); n         for (int j = 0; j < n; j++) { n           printf( %d  , v[j].second + 1); n         } n         printf(  n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1; n int a[N]; n int main() { n   long long ans = 0, ones = 0, k; n   string s; n   cin >> k >> s; n   a[0] = -1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) ones++, a[ones] = i; n     if (ones >= k) { n       if (k != 0) n         ans += a[ones - k + 1] - a[ones - k]; n       else if (s[i] ==  0 ) n         ans += i - a[ones]; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10, p = 1e9 + 7; n int n, cnt[26], inv[N], fac[N], finv[N]; n char a[N], b[N]; n inline int solve(char *s) { n   int ans = 0, noans = fac[n]; n   memset(cnt, 0, sizeof cnt); n   for (int i = 1; i <= n; ++i) ++cnt[a[i] -  a ]; n   for (int i = 0; i < 26; ++i) noans = 1ll * noans * finv[cnt[i]] % p; n   for (int i = 1; i <= n; ++i) { n     int v = s[i] -  a ; n     noans = 1ll * noans * inv[n - i + 1] % p; n     for (int c = 0; c < v; ++c) n       if (cnt[c]) { n         noans = 1ll * noans * cnt[c] % p; n         ans = (ans + noans) % p; n         noans = 1ll * noans * inv[cnt[c]] % p; n       } n     if (!cnt[v]) return ans; n     noans = 1ll * noans * cnt[v] % p; n     --cnt[v]; n   } n   return ans; n } n int main() { n   scanf( %s%s , a + 1, b + 1); n   n = strlen(a + 1); n   fac[0] = 1; n   for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % p; n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= n; ++i) inv[i] = 1ll * inv[p % i] * (p - p / i) % p; n   finv[0] = 1; n   for (int i = 1; i <= n; ++i) finv[i] = 1ll * inv[i] * finv[i - 1] % p; n   printf( %d , (solve(b) - solve(a) - 1 + p) % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     vector<long long> vis(n); n     for (int i = 0, ggdem = m; i < ggdem; ++i) { n       long long a, b, c; n       cin >> a >> b >> c; n       a--; n       b--; n       c--; n       vis[b] = 1; n     } n     long long ve = -1; n     for (int i = 0, ggdem = n; i < ggdem; ++i) { n       if (vis[i] == 0) ve = i; n     } n     for (int i = 0, ggdem = n; i < ggdem; ++i) { n       if (i != ve) { n         cout << i + 1 <<     << ve + 1 <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   unsigned long long int n, sum = 0, sum1 = 0, i, MAX = 0, temp; n   scanf( %llu , &n); n   int a[n]; n   for (i = 0; i < n; ++i) { n     scanf( %llu , &a[i]); n   } n   for (i = 0; i < n; ++i) { n     sum = sum + a[i]; n   } n   for (i = 0; i < n; ++i) { n     if (a[i] > MAX) { n       MAX = a[i]; n     } n   } n   for (i = 0; i < n; ++i) { n     if (a[i] == MAX) { n       break; n     } n   } n   temp = a[n - 1]; n   a[n - 1] = MAX; n   a[i] = temp; n   if (sum % 2 != 0) { n     printf( NO ); n   } else { n     for (i = 0; i < n - 1; ++i) { n       sum1 = sum1 + a[i]; n     } n     if (sum1 >= a[n - 1]) { n       printf( YES ); n     } else { n       printf( NO ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, x, y; n   cin >> n >> x >> y; n   int a[n]; n   int c = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] <= x) c++; n   } n   if (x > y) n     cout << n << endl; n   else { n     if (c % 2 == 0) n       cout << c / 2 << endl; n     else n       cout << (c / 2) + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int block[101]; n int nuclea[101]; n int inst[101][101]; n int main() { n   ios_base::sync_with_stdio(0); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> inst[j][i]; n     } n   } n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) { n       if (nuclea[j] == 0) block[inst[i][j]]++; n     } n     block[0] = 0; n     for (int j = 0; j < n; j++) { n       if (block[inst[i][j]] > 1 && nuclea[j] == 0) { n         nuclea[j] = i + 1; n       } n     } n     for (int i = 1; i < 101; i++) { n       if (block[i] < 2) block[i] = 0; n     } n   } n   for (int i = 0; i < n; i++) cout << nuclea[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3 * 100 * 1000 + 1000; n const int N1 = 5100; n long long A[N], n, k; n unsigned long long dp[N1][N1]; n void read() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> A[i]; n   sort(A, A + n); n   for (int i = 0; i < N1; i++) n     for (int j = 0; j < N1; j++) dp[i][j] = (1 << 31); n } n int cal(int p, int q) { return p * ((n + k - 1) / k) + q * (n / k) - 1; } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   read(); n   int p = n % k, q = k - n % k; n   dp[0][0] = 0; n   for (int i = 0; i <= p; i++) n     for (int j = 0; j <= q; j++) { n       int last = cal(i, j); n       if (i - 1 >= 0) n         dp[i][j] = min(dp[i][j], n                        dp[i - 1][j] + A[last] - A[last - (n + k - 1) / k + 1]); n       if (j - 1 >= 0) n         dp[i][j] = n             min(dp[i][j], dp[i][j - 1] + A[last] - A[last - (n / k) + 1]); n     } n   cout << dp[p][q] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-10; n template <typename T, typename S> n vector<T>& operator<<(vector<T>& a, S b) { n   a.push_back(b); n   return a; n } n template <typename T> n void operator>>(vector<T>& a, int b) { n   while (b--) n     if (!a.empty()) a.pop_back(); n } n bool isprime(int n) { n   if (n < 2) return false; n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n long long b_pow(long long x, long long n) { n   return n ? n == 1 ? x : b_pow(x, n >> 1) * b_pow(x, (n >> 1) + (n & 1)) : 1; n } n string itos(int n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n string table[119] = { n      - ,   H ,   He ,  Li ,  Be ,  B ,    C ,    N ,    O ,    F ,    Ne ,  Na , n      Mg ,  Al ,  Si ,  P ,   S ,   Cl ,   Ar ,   K ,    Ca ,   Sc ,   Ti ,  V , n      Cr ,  Mn ,  Fe ,  Co ,  Ni ,  Cu ,   Zn ,   Ga ,   Ge ,   As ,   Se ,  Br , n      Kr ,  Rb ,  Sr ,  Y ,   Zr ,  Nb ,   Mo ,   Tc ,   Ru ,   Rh ,   Pd ,  Ag , n      Cd ,  In ,  Sn ,  Sb ,  Te ,  I ,    Xe ,   Cs ,   Ba ,   La ,   Ce ,  Pr , n      Nd ,  Pm ,  Sm ,  Eu ,  Gd ,  Tb ,   Dy ,   Ho ,   Er ,   Tm ,   Yb ,  Lu , n      Hf ,  Ta ,  W ,   Re ,  Os ,  Ir ,   Pt ,   Au ,   Hg ,   Tl ,   Pb ,  Bi , n      Po ,  At ,  Rn ,  Fr ,  Ra ,  Ac ,   Th ,   Pa ,   U ,    Np ,   Pu ,  Am , n      Cm ,  Bk ,  Cf ,  Es ,  Fm ,  Md ,   No ,   Lr ,   Rf ,   Db ,   Sg ,  Bh , n      Hs ,  Mt ,  Ds ,  Rg ,  Cn ,  Uut ,  Uuq ,  Uup ,  Uuh ,  Uus ,  Uuo }; n vector<int> item, goal, able[17]; n int n, m; n void output(int bit, int num) { n   vector<string> ans; n   for (int i = 0; i < n; i++) n     if (bit & (1 << i)) ans.push_back(table[item[i]]); n   for (int i = 0; i < ans.size(); i++) cout << (i ?  +  :   ) << ans[i]; n   cout <<  ->  << table[goal[num]] << endl; n } n vector<int> currentUsing; n bool done[1 << 17][17]; n bool dfs(int pos, int bit) { n   if (pos == m) { n     cout <<  YES  << endl; n     for (int i = 0; i < pos; i++) output(currentUsing[i], i); n     return true; n   } n   if (done[bit][pos]) n     return false; n   else n     done[bit][pos] = 1; n   for (int i = 0; i < able[pos].size(); i++) { n     if (bit & able[pos][i]) continue; n     currentUsing.push_back(able[pos][i]); n     if (dfs(pos + 1, bit | able[pos][i])) return true; n     currentUsing.pop_back(); n   } n   return false; n } n int main() { n   cin >> n >> m; n   string tmp; n   for (int i = 0; i < n; i++) n     cin >> tmp, item.push_back(find(table, table + 119, tmp) - table); n   for (int i = 0; i < m; i++) n     cin >> tmp, goal.push_back(find(table, table + 119, tmp) - table); n   for (int i = 0; i < m; i++) { n     for (int bit = 0; bit < (1 << n); bit++) { n       int sum = 0; n       for (int j = 0; j < n; j++) n         if (bit & (1 << j)) sum += item[j]; n       if (sum == goal[i]) able[i].push_back(bit); n     } n   } n   if (!dfs(0, 0)) cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const int mod = 998244353; n int a[N], s[N], f[N][105], b[N][105]; n inline void add(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n   return; n } n template <typename T> n inline void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n inline void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n inline void read(T &x) { n   T f = 1; n   x = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   x *= f; n } n int main() { n   int n, k, len; n   read(n), read(k), read(len); n   if (len == 1) { n     printf( 0 n ); n     return 0; n   } n   s[0] = 1; n   for (int i = 1; i <= n; ++i) read(a[i]); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= k; ++j) n       if (a[i] == j || a[i] == -1) b[i][j] = 1; n   for (int j = 1; j <= k; ++j) n     for (int i = 1; i <= n; ++i) b[i][j] += b[i - 1][j]; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= k; ++j) { n       if (~a[i] && a[i] != j) continue; n       f[i][j] = s[i - 1]; n       if (i >= len && b[i][j] - b[i - len][j] == len) n         add(f[i][j], (f[i - len][j] - s[i - len] + mod) % mod); n       add(s[i], f[i][j]); n     } n   } n   printf( %d n , s[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int head[N], Next[N], ver[N], tot; n inline void add(int u, int v) { n   ver[++tot] = v, Next[tot] = head[u], head[u] = tot; n } n int pa[N][25], top[N], son[N], dep[N], ans, n, L; n long long sum[N], w[N], S; n inline void init(int u) { n   for (int i = 1; i <= 20; ++i) pa[u][i] = pa[pa[u][i - 1]][i - 1]; n   int dis = L, tmp = u; n   for (int i = 20; i >= 0; --i) { n     int fa = pa[tmp][i]; n     if (!fa || (1 << i) >= dis) continue; n     if (sum[u] - sum[fa] + w[fa] > S) continue; n     dis -= (1 << i), top[u] = fa, tmp = fa; n   } n } n void dfs1(int u, int fa) { n   sum[u] = sum[fa] + w[u], top[u] = u, dep[u] = dep[fa] + 1, init(u); n   for (int i = head[u]; i; i = Next[i]) dfs1(ver[i], u); n } n void dfs2(int u) { n   int mx = -1; n   for (int i = head[u]; i; i = Next[i]) { n     int v = ver[i]; n     dfs2(v); n     if (son[v] == v) continue; n     if (mx == -1 || dep[mx] > dep[son[v]]) mx = son[v]; n   } n   if (mx == -1) ++ans, mx = top[u]; n   son[u] = mx; n } n int main() { n   scanf( %d%d%lld , &n, &L, &S); n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &w[i]); n     if (w[i] > S) return puts( -1 ), 0; n   } n   for (int i = 2, f; i <= n; ++i) scanf( %d , &f), add(f, i), pa[i][0] = f; n   dfs1(1, 0), dfs2(1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 9; n vector<pair<int, int> > barr[N]; n int n, q, arr[N], a, b; n long long t[N], ans[N]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   scanf( %d , &q); n   int kok = sqrt(n); n   for (int i = 1; i <= q; i++) { n     scanf( %d %d , &a, &b); n     if (b > kok) { n       for (int j = a; j <= n; j += b) ans[i] += arr[j]; n     } else n       barr[b].push_back(make_pair(a, i)); n   } n   for (int i = 1; i <= kok; i++) { n     if (barr[i].size()) { n       for (int j = n; j >= 1; j--) t[j] = arr[j] + t[(i + j) * (i + j <= n)]; n       for (int j = 0; j < barr[i].size(); j++) n         ans[barr[i][j].second] = t[barr[i][j].first]; n     } n   } n   for (int i = 1; i <= q; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int maxN = 5e5 + 5; n int m, k, n, s; n vector<int> a; n int c[maxN], h[maxN]; n int main(int argc, char *argv[]) { n   cin >> m >> k >> n >> s; n   a.resize(m + 1); n   for (int i = 1; i <= m; ++i) cin >> a[i]; n   int total = 0; n   for (int i = 0; i < s; ++i) { n     int x; n     cin >> x; n     c[x]++; n     if (c[x] == 1) total++; n   } n   int cur_s = 0; n   vector<int> ans; n   bool found = false; n   for (int l = 1, r = 1; l + k - 1 <= m; ++l) { n     while ((cur_s < total && r <= m) || r - l + 1 <= k) { n       h[a[r]]++; n       if (h[a[r]] == c[a[r]]) cur_s++; n       r++; n     } n     if (cur_s == total && (l - 1) / k + (m - r + 1) / k + 1 >= n) { n       found = true; n       int to_remove = r - l - k; n       for (int i = l; to_remove > 0 && i < r; ++i) { n         if (h[a[i]] > c[a[i]]) { n           to_remove--; n           ans.emplace_back(i); n           h[a[i]]--; n         } n       } n       break; n     } n     if (h[a[l]] == c[a[l]]) cur_s--; n     h[a[l]]--; n   } n   if (found == false) n     cout << -1 << endl; n   else { n     cout << ans.size() << endl; n     for (int i = 0; i < ans.size(); ++i) { n       if (i > 0) cout <<    ; n       cout << ans[i]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a; n   cin >> a; n   cout << (a + a / 2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const char nl =   n ; n const long long mod = 1e9 + 7; n long long bigmod(long long a, long long b) { n   if (b == 0) return 1; n   long long ans = bigmod(a, b / 2); n   ans = (ans * ans) % mod; n   if (b & 1) ans *= a; n   return ans % mod; n } n void solve() { n   long long n; n   cin >> n; n   cout << (bigmod(4, pow(2, n) - 2) * 6) % mod << nl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include  bits/stdc++.h  n #define int long long  n using namespace std ; n const int M=998244353,mxM=5e4  ; n int d[21][mxM+1],n,m,p,iv[21],f1[21],f2[21]; n signed main(){ n  cin >> n >> m  ; n  for(int i=0;i<n;i++) n   for(int j=0;j<m;j++) n    cin >> d[i][j]  ; n  iv[1]=1 ; n  for(int i=2;i<n+1;i++) n   iv[i]=M-M/i*iv[M%i]%M ; n  f1[0]=f2[0]=1 ; n  for(int i=1;i<n+1;i++){ n   f1[i]=f1[i-1]*i%M  ; n   f2[i]=f2[i-1]*iv[i]%M ; n  } n  p=m*f1[n]%M ; n  for(int j=0;j<m;j++){ n   vector<int>a ; n   for(int i=0;i<n;i++) n    a.push_back(d[i][j]) ; n   sort(a.begin(),a.end()) ; n   int c=1 ; n   for(int i=0;i<n;i++){ n    if(a[i]-i-1<=0){ n     c=0 ;break ; n    } n    (c*=(a[i]-i-1))%=M  ; n   } n   p=(p-c+M)%M  ; n  } n  cout << (p*f2[n])%M<<   n  ; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool read(int &x) { n   char ch; n   bool flag = false; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     if (ch ==  - ) flag = true; n   for (x = 0; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   x = flag ? -x : x; n   if (ch ==   n ) return 0; n   return 1; n } n inline void write(int x) { n   static const int maxlen = 100; n   static char s[maxlen]; n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (!x) { n     putchar( 0 ); n     return; n   } n   int len = 0; n   for (; x; x /= 10) s[len++] = x % 10 +  0 ; n   for (int i = len - 1; i >= 0; --i) putchar(s[i]); n } n const int MAXN = 2500; n int n, lim; n int g[MAXN][MAXN]; n char s[MAXN]; n int f[MAXN][MAXN]; n bool go_l[MAXN][MAXN], go_r[MAXN][MAXN]; n bool go_up[MAXN][MAXN], go_down[MAXN][MAXN]; n int add_l[MAXN][MAXN]; n int add_h[MAXN][MAXN]; n int sum_l[MAXN][MAXN]; n int sum_h[MAXN][MAXN]; n int main() { n   read(n); n   read(lim); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s); n     for (int j = 1; j <= n; j++) n       if (s[j - 1] ==  B ) n         g[i][j] = 1; n       else n         g[i][j] = 0; n   } n   for (int i = 1; i <= n; i++) { n     go_l[i][0] = 1; n     for (int j = 1; j <= n; j++) n       if ((go_l[i][j - 1]) && (g[i][j] == 0)) n         go_l[i][j] = 1; n       else n         go_l[i][j] = 0; n     go_r[i][n + 1] = 1; n     for (int j = n; j >= 1; j--) n       if ((go_r[i][j + 1]) && (g[i][j] == 0)) n         go_r[i][j] = 1; n       else n         go_r[i][j] = 0; n     for (int j = 1; j <= n - lim + 1; j++) n       if (go_l[i][j - 1] && go_r[i][j + lim] && (!go_r[i][1])) n         add_h[i][j] = 1; n       else n         add_h[i][j] = 0; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) sum_h[i][j] = sum_h[i - 1][j] + add_h[i][j]; n   for (int j = 1; j <= n; j++) { n     go_up[0][j] = 1; n     for (int i = 1; i <= n; i++) n       if ((go_up[i - 1][j]) && (g[i][j] == 0)) n         go_up[i][j] = 1; n       else n         go_up[i][j] = 0; n     go_down[n + 1][j] = 1; n     for (int i = n; i >= 1; i--) n       if ((go_down[i + 1][j]) && (g[i][j] == 0)) n         go_down[i][j] = 1; n       else n         go_down[i][j] = 0; n     for (int i = 1; i <= n - lim + 1; i++) n       if (go_up[i - 1][j] && go_down[i + lim][j] && (!go_down[1][j])) n         add_l[i][j] = 1; n       else n         add_l[i][j] = 0; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) sum_l[i][j] = sum_l[i][j - 1] + add_l[i][j]; n   int tot = 0; n   for (int i = 1; i <= n; i++) n     if (go_r[i][1]) tot++; n   for (int i = 1; i <= n; i++) n     if (go_down[1][i]) tot++; n   int ans = 0; n   for (int i = 1; i <= n - lim + 1; i++) n     for (int j = 1; j <= n - lim + 1; j++) n       ans = max(ans, (sum_l[i][j + lim - 1] - sum_l[i][j - 1]) + n                          (sum_h[i + lim - 1][j] - sum_h[i - 1][j])); n   cout << ans + tot << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n long long int n, k, mx, sum, a[N]; n int main() { n   cin >> n >> k; n   sum++; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i) { n       if (a[i] != a[i - 1]) { n         sum++; n         mx = max(mx, sum); n       } else { n         sum = 1; n       } n     } n   } n   mx = max(sum, mx); n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n void solve() { n   int n, k; n   cin >> n >> k; n   string A[n]; n   map<string, int> m; n   for (int i = (0); i < (n); i++) { n     cin >> A[i]; n     m[A[i]]++; n   } n   long long ans = 0; n   sort(A, A + n); n   set<string> st; n   for (int i = (0); i < (n - 2); i++) { n     for (int j = (i + 1); j < (n - 1); j++) { n       string s(k,  . ); n       for (int z = (0); z < (k); z++) { n         if (A[i][z] == A[j][z]) { n           s[z] = A[i][z]; n         } else { n           int tmp = A[i][z] + A[j][z]; n           if (tmp == 152) { n             s[z] =  T ; n           } else if (tmp == 153) { n             s[z] =  S ; n           } else { n             s[z] =  E ; n           } n         } n       } n       string tmp = A[i] + A[j] + s; n       if (s.compare(A[i]) >= 0 && s.compare(A[j]) >= 0 && n           st.find(tmp) == st.end()) { n         ans += m[s]; n         if (s.compare(A[i]) == 0) { n           ans--; n         } n         if (s.compare(A[j]) == 0) { n           ans--; n         } n         st.insert(tmp); n       } n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int tt = 1; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n int n, k, f, c, ans; n int main() { n   cin >> n >> k; n   for (int i = 2; i <= n; i++) { n     f = 0; n     for (int j = 2; j <= i / 2; j++) f += (i % j == 0); n     if (f == 0) a.push_back(i); n   } n   for (int i = 2; i < a.size(); i++) n     for (int j = 1; j < i && a[j - 1] + a[j] + 1 <= a[i]; j++) n       if (a[j - 1] + a[j] + 1 == a[i]) ans++; n   if (ans >= k) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, f[200009]; n int judge(int len) { n   if (len == 2) return 0; n   if (len == 3) return k == (n - 1) ? 1 : 0; n   if ((long long)(k - 2) * ((len - 1) / 2 - 1) < n - len - k + 2) return 0; n   return 1; n } n int main() { n   scanf( %d%d , &n, &k); n   int l = 2, r = n - k + 2, len = 0; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (judge(mid)) n       r = mid - 1, len = mid; n     else n       l = mid + 1; n   } n   printf( %d n , len - 1); n   for (int i = 1; i < len; i++) printf( %d %d n , i, i + 1); n   int lf = k - 2, ilf = n - len - k + 2, t = len + 1; n   for (int i = 1; i <= lf; i++) { n     printf( %d %d n , (len + 1) / 2, t); n     f[i] = t, t++; n   } n   int x = 1; n   while (ilf > 0) { n     printf( %d %d n , f[x], t); n     f[x] = t; n     t++; n     x = x % lf + 1; n     ilf--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n const long long N = 100001; n long long mod = 1000000007; n long long min(long long a, long long b) { n   if (a < b) n     return a; n   else n     return b; n } n long long max(long long a, long long b) { n   if (a > b) n     return a; n   else n     return b; n } n long long power(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long gcd(long long p, long long q) { n   if (p % q == 0) n     return q; n   else { n     return gcd(q, p % q); n   } n } n long long mul(long long a, long long b) { n   return ((a % mod) * (b % mod)) % mod; n } n long long sub(long long a, long long b) { n   return (((a - b) % mod) + mod) % mod; n } n vector<long long> kmp(string s) { n   long long n = s.size(); n   vector<long long> ans(n); n   long long len = 0; n   for (long long i = 1; i < n; i++) { n     while (len > 0 && s[len] != s[i]) len = ans[len - 1]; n     len += (s[i] == s[len]); n     ans[i] = len; n   } n   return ans; n } n void solve() { n   string s; n   cin >> s; n   vector<long long> ans = kmp(s); n   if (ans.back() == 0) { n     cout <<  Just a legend  <<   n ; n     return; n   } n   for (long long i = 1; i < s.size() - 1; i++) { n     if (ans[i] == ans.back()) { n       cout << s.substr(0, ans[i]); n       cout <<   n ; n       return; n     } n   } n   if (ans[ans.back() - 1] == 0) n     cout <<  Just a legend ; n   else n     cout << s.substr(0, ans[ans.back() - 1]); n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   for (long long i = 0; i < t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   int curr_len = 0; n   int l = 0, r = a.size() - 1; n   while (l <= r) { n     if (a[l] - 1 < int(1e6) - a[r]) { n       curr_len = a[l] - 1; n       ++l; n     } else { n       curr_len = int(1e6) - a[r]; n       --r; n     } n   } n   cout << curr_len; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 400; n struct hh { n   double x, y; n } a[MAX]; n double jisuan(hh a, hh b, hh c) { n   double x1, y1, x2, y2; n   x1 = b.x - a.x; n   y1 = b.y - a.y; n   x2 = c.x - a.x; n   y2 = c.y - a.y; n   return (x1 * y2 - x2 * y1) / 2; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].x >> a[i].y; n   } n   double area, l, r; n   area = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       l = r = 0; n       for (int k = 0; k < n; k++) { n         double w = jisuan(a[i], a[j], a[k]); n         if (w > 0 && w > l) n           l = w; n         else if (w < 0 && -w > r) n           r = -w; n       } n       if (l + r > area && l > 0 && r > 0) area = l + r; n     } n   } n   printf( %.6f n , area); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   cout << (n * 6 - 1) * k << endl; n   for (int i = 0; i < n; i++) n     cout << (i * 6 + 1) * k <<     << (i * 6 + 2) * k <<     << (i * 6 + 3) * k n          <<     << (i * 6 + 5) * k << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n default_random_engine gen(42); n uniform_real_distribution<double> ran(0, 1); n void vin(std::vector<int> &a) { n   for (auto &x : a) cin >> x; n } n void vout(std::vector<int> &a) { n   for (auto x : a) cout << x <<    ; n   cout << endl; n } n bool vm(array<pair<int, int>, 2> m) { n   if (abs(m[0].first - m[1].first) + abs(m[0].second - m[1].second) != 3) n     return 0; n   if (m[0].first == m[1].first || m[0].second == m[1].second) return 0; n   return 1; n } n pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) { n   return {a.first + b.first, a.second + b.second}; n } n std::vector<pair<int, int> > mo{{1, 2},   {2, 1},   {2, -1}, {1, -2}, n                                 {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}}; n array<std::vector<std::vector<int> >, 2> dp; n int n, m; n void initdp() { n   for (int qw = 0; qw < 2; qw++) { n     dp[qw].resize(n, std::vector<int>(m, -1)); n     priority_queue<tuple<int, int, int>, std::vector<tuple<int, int, int> >, n                    greater<tuple<int, int, int> > > n         pq; n     pq.emplace(make_tuple(0, n / 2 - 1 + qw, m / 2 - 1)); n     while (!pq.empty()) { n       int x, y, d; n       tie(d, x, y) = pq.top(); n       pq.pop(); n       if (dp[qw][x][y] != -1) continue; n       dp[qw][x][y] = d; n       for (auto move : mo) { n         pair<int, int> t = move + make_pair(x, y); n         if (t.first >= 0 && t.first < n && t.second >= 0 && t.second < m && n             dp[qw][t.first][t.second] == -1) n           pq.emplace(make_tuple(d + 1, t.first, t.second)); n       } n     } n   } n } n int dist(pair<int, int> b, bool t) { return dp[t][b.first][b.second]; } n int end(array<pair<int, int>, 2> a, bool t) { n   if (vm(a)) return t; n   if (a[0].first == n / 2 - 1 && a[0].second == m / 2 - 1) return 0; n   if (a[1] == make_pair(n / 2, m / 2 - 1)) return 1; n   return -1; n } n pair<int, int> w; n double rek(array<pair<int, int>, 2> a, bool t, int rd = 6, double mx = INT_MAX, n            double mn = INT_MIN) { n   pair<int, int> mw = {0, 0}; n   if (end(a, t) != -1) n     return ((end(a, t) ? 1000 : -1000) + ran(gen) * .001) * pow(1.01, rd); n   if (rd == 0) { n     double d[2][2]; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) d[i][j] = dist(a[i], j); n     d[t][0] -= .5; n     d[t][1] -= .5; n     return (d[0][0] - d[1][1] + (d[0][1] - d[1][0]) * .1 + ran(gen) * .001) * n            pow(1.01, rd); n   } n   double rt = t ? -100000 : 100000; n   priority_queue<array<pair<int, int>, 2>, n                  std::vector<array<pair<int, int>, 2> >, n                  greater<array<pair<int, int>, 2> > > n       pq; n   for (auto x : mo) { n     pair<int, int> c = a[t] + x; n     if (c.first >= 0 && c.first < n && c.second >= 0 && c.second < m) n       pq.emplace(array<pair<int, int>, 2>{ n           make_pair(dp[t][c.first][c.second], dp[!t][c.first][c.second]), c}); n   } n   assert(!pq.empty()); n   while (!pq.empty()) { n     array<pair<int, int>, 2> n = a; n     n[t] = pq.top()[1]; n     pq.pop(); n     double r = rek(n, !t, rd - 1, mx, mn); n     if ((t && r > rt) || (!t && r < rt)) { n       rt = r; n       mw = n[t]; n     } n     if ((t && rt > mx) || (!t && rt < mn)) break; n     if (t) n       mn = max(mn, rt); n     else n       mx = min(mx, rt); n   } n   w = mw; n   return rt; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   initdp(); n   int xWI, yWI, xBL, yBL; n   cin >> xWI >> yWI >> xBL >> yBL; n   xWI--; n   yWI--; n   xBL--; n   yBL--; n   array<pair<int, int>, 2> a = {make_pair(xWI, yWI), make_pair(xBL, yBL)}; n   if (vm(a)) { n     cout <<  WHITE n  << xBL + 1 <<     << yBL + 1 <<   n ; n     return 0; n   } n   double r = rek(a, 0); n   int me; n   if (fabs(r) > 500) n     me = r > 0; n   else { n     int fr = ((xWI + yWI + xBL + yBL) % 2); n     int df = dp[fr][a[fr].first][a[fr].second], n         da = dp[fr][a[!fr].first][a[!fr].second]; n     cerr << df <<     << da <<   n ; n     if (df + 1 + fr <= da) n       me = fr; n     else n       me = !fr; n   } n   cout << (me ?  BLACK  :  WHITE ) << endl; n   int t = 0; n   while (true) { n     if (t == me) { n       rek(a, t); n       cout << w.first + 1 <<     << w.second + 1 << endl; n       if (w == a[!t]) return 0; n       if (!vm(a) && w == make_pair(n / 2 - 1 + t, m / 2 - 1)) return 0; n     } else { n       cin >> w.first >> w.second; n       w.first--; n       w.second--; n     } n     a[t] = w; n     t = !t; n   } n } n


#include <bits/stdc++.h> n using namespace std; n class Node { n  public: n   vector<int> kids; n   int pos[26]; n   int size; n   int depth; n   Node() { n     this->size = 0; n     memset(pos, 0, sizeof(pos)); n   } n   void push(int kid, int next) { n     this->kids.push_back(kid); n     pos[next] = this->kids.size(); n   } n } N[300005]; n int n, x, y; n char c; n int maxdepth; n int reduce[300005]; n void init(int id) { n   N[id].size = 1; n   int l = N[id].kids.size(); n   for (int i = 0; i < l; i++) { n     N[N[id].kids[i]].depth = N[id].depth + 1; n     init(N[id].kids[i]); n     maxdepth = max(maxdepth, N[N[id].kids[i]].depth); n     N[id].size += N[N[id].kids[i]].size; n   } n } n int dfsS(vector<int> &kids) { n   if (kids.size() <= 1) return 0; n   int ret = kids.size() - 1; n   int l = kids.size(); n   int pos; n   vector<int> tmp; n   for (int i = 0; i < 26; i++) { n     for (int j = 0; j < l; j++) { n       pos = N[kids[j]].pos[i]; n       if (pos) { n         tmp.push_back(N[kids[j]].kids[pos - 1]); n       } n     } n     if (tmp.size() > 1) ret += dfsS(tmp); n     tmp.clear(); n   } n   return ret; n } n void dfsD(int id) { n   int l = N[id].kids.size(); n   int ret = l ? 1 : 0; n   ret += dfsS(N[id].kids); n   reduce[N[id].depth + 1] += ret; n   for (int i = 0; i < l; i++) { n     dfsD(N[id].kids[i]); n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> x >> y >> c; n     N[x].push(y, c -  a ); n   } n   N[1].depth = 0; n   init(1); n   dfsD(1); n   int mm = 0; n   int ans = 0; n   for (int i = 1; i <= maxdepth; i++) { n     if (reduce[i] > mm) { n       ans = i; n       mm = reduce[i]; n     } n   } n   cout << N[1].size - mm << endl; n   cout << ans; n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int C = 26; n void add(int &x, int y) { n   x += y; n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n } n int fix(int x) { n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n   return x; n } n int pw(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = 1ll * ret * a % MOD; n     b >>= 1; n     a = 1ll * a * a % MOD; n   } n   return ret; n } n bitset<3> bs(int x) { return bitset<3>(x); } n const int MAXN = 1e5 + 10; n vector<int> vec[MAXN]; n int mark[MAXN], tt; n int single(int x) { return (x & (x - 1)) == 0; } n void solve() { n   ++tt; n   int a[3]; n   vector<int> sec; n   for (int i = 0; i < 3; i++) { n     cin >> a[i]; n     for (int x : vec[a[i]]) { n       if (mark[x] ^ tt) sec.push_back(x); n       mark[x] = tt; n     } n   } n   int cnt[8] = {}; n   for (int x : sec) { n     int mask = 0; n     for (int i = 0; i < 3; i++) n       if (a[i] % x == 0) mask ^= 1 << i; n     ++cnt[mask]; n   } n   int ans = 0; n   for (int i = 0; i < (1 << 3); i++) { n     for (int j = i; j < (1 << 3); j++) { n       for (int k = j; k < (1 << 3); k++) { n         if ((i | j | k) != (1 << 3) - 1) continue; n         if ((i == j && single(i)) || (j == k && single(j))) continue; n         int cnt_single = single(i) + single(j) + single(k); n         int memo_ans = ans; n         if (cnt_single == 3) { n           ans += cnt[i] * cnt[j] * cnt[k]; n         } else if (cnt_single == 2) { n           ans += cnt[i] * cnt[j] * cnt[k]; n         } else if (cnt_single == 1) { n           if (single(i)) { n             if (j == k) { n               ans += cnt[i] * (cnt[j] + 1) * cnt[j] / 2; n             } else { n               ans += cnt[i] * cnt[j] * cnt[k]; n             } n           } n           if (single(j)) { n             if (i == k) { n               ans += cnt[j] * (cnt[i] + 1) * cnt[i] / 2; n             } else { n               ans += cnt[i] * cnt[j] * cnt[k]; n             } n           } n           if (single(k)) { n             if (i == j) { n               ans += cnt[k] * (cnt[i] + 1) * cnt[i] / 2; n             } else { n               ans += cnt[i] * cnt[j] * cnt[k]; n             } n           } n         } else if (i == j && j == k) { n           ans += (cnt[i] + 2) * (cnt[i] + 1) * cnt[i] / 6; n         } else if (i == j) { n           ans += cnt[k] * (cnt[i] + 1) * cnt[i] / 2; n         } else if (j == k) { n           ans += cnt[i] * (cnt[j] + 1) * cnt[j] / 2; n         } else { n           ans += cnt[i] * cnt[j] * cnt[k]; n         } n         if (ans != memo_ans && 0) { n           cerr << bs(i) <<     << bs(j) <<     << bs(k) <<   n ; n           cerr <<  ans:   << memo_ans <<   n ; n           cerr <<  now ans:   << ans <<   n n ; n         } n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   for (int i = 1; i < MAXN; i++) n     for (int j = i; j < MAXN; j += i) vec[j].push_back(i); n   int te = 1; n   cin >> te; n   for (int w = 1; w <= te; w++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:64000000 ) n const int MAXN = 1000002; n int n, l, t; n int a[MAXN << 1]; n inline void init() { n   scanf( %d%d%d , &n, &l, &t); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   sort(a, a + n); n } n int main() { n   init(); n   t <<= 1; n   double res = 0.0; n   int cycles = t / l; n   t %= l; n   res += (double)cycles * (double)n * (double)(n - 1) * 0.5; n   for (int i = 0; i < n; ++i) a[i + n] = a[i] + l; n   for (int i = 0; i < n; ++i) { n     int ind = i + n; n     int l = 1, r = n - 1; n     int cur = 0; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (a[ind] - a[ind - mid] <= t) { n         cur = mid; n         l = mid + 1; n       } else { n         r = mid - 1; n       } n     } n     res += (double)cur * 0.25; n     cur = 0; n     l = 1, r = n - 1; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (a[i + mid] - a[i] <= t) { n         cur = mid; n         l = mid + 1; n       } else { n         r = mid - 1; n       } n     } n     res += (double)cur * 0.25; n   } n   res /= 2.0; n   printf( %.15lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, a, b, c, n, t, f, d; n   string s, v; n   map<char, int> ma, sa; n   vector<int> vv; n   map<char, int>::iterator it, ki; n   cin >> s; n   f = 0; n   c = 0; n   for (j = 0; j < s.size(); j++) { n     if ((s[j] >=  A  && s[j] <=  Z )) { n       c++; n     } n   } n   if (c == s.size()) { n     for (j = 0; j < s.size(); j++) { n       cout << char(s[j] + 32); n     } n     cout << endl; n   } else if (s[0] >=  a  && s[0] <=  z  && c == s.size() - 1) { n     cout << char(s[0] - 32); n     for (j = 1; j < s.size(); j++) { n       cout << char(s[j] + 32); n     } n     cout << endl; n   } else { n     cout << s << endl; n   } n   return 0; n } n