#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000; n int prime[MAXN + 1]; n void getPrime() { n   memset(prime, 0, sizeof(prime)); n   for (int i = 2; i <= MAXN; i++) { n     if (!prime[i]) prime[++prime[0]] = i; n     for (int j = 1; j <= prime[0] && prime[j] <= MAXN / i; j++) { n       prime[prime[j] * i] = 1; n       if (i % prime[j] == 0) break; n     } n   } n } n unordered_map<int, bool> Mcnt; n unordered_map<int, int> M; n const int maxn = 105; n int a[maxn]; n int getsg(int S) { n   if (S == 0) return 0; n   if (M.count(S)) return M[S]; n   vector<int> V; n   V.clear(); n   for (int i = 0; i < 29; i++) n     if (S & (1 << i)) V.push_back(i + 1); n   int ma = 0; n   for (int i = 0; i < V.size(); i++) ma = max(ma, V[i]); n   int x[30]; n   set<int> pre; n   pre.clear(); n   for (int i = 1; i <= ma; i++) { n     for (int j = 0; j < V.size(); j++) { n       if (V[j] >= i) n         x[j] = V[j] - i; n       else n         x[j] = V[j]; n     } n     int SS = 0; n     for (int j = 0; j < V.size(); j++) { n       if (x[j]) SS |= (1 << (x[j] - 1)); n     } n     pre.insert(getsg(SS)); n   } n   int now = 0; n   while (pre.count(now)) now++; n   M[S] = now; n   return now; n } n int cnt[maxn]; n int main() { n   getPrime(); n   int ans = 0; n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= prime[0]; i++) { n     memset(cnt, 0, sizeof(cnt)); n     for (int j = 1; j <= n; j++) { n       while (a[j] % prime[i] == 0) { n         a[j] /= prime[i]; n         cnt[j]++; n       } n     } n     int S = 0; n     for (int j = 1; j <= n; j++) n       if (cnt[j]) S |= (1 << (cnt[j] - 1)); n     ans ^= getsg(S); n   } n   for (int i = 1; i <= n; i++) n     if (a[i] != 1) { n       if (!Mcnt.count(a[i])) { n         ans ^= 1; n         Mcnt[a[i]] = true; n       } n     } n   if (ans == 0) n     printf( Arpa ); n   else n     printf( Mojtaba ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<vector<int>> v; n   vector<int> cur; n   if ((n * m) % k == 0) { n     int div = (n * m) / k; n     for (int i = 0; i < n; i++) { n       if (!(i & 1)) { n         for (int j = 0; j < m; j++) { n           cur.push_back(i + 1); n           cur.push_back(j + 1); n           if (cur.size() >= div * 2) { n             vector<int> add = cur; n             v.push_back(add); n             cur.clear(); n           } n         } n       } else { n         for (int j = m - 1; j >= 0; j--) { n           if (cur.size() <= div * 2) { n             cur.push_back(i + 1); n             cur.push_back(j + 1); n           } n           if (cur.size() >= div * 2) { n             vector<int> add = cur; n             v.push_back(add); n             cur.clear(); n           } n         } n       } n     } n   } else { n     int div = ((n * m) - 2) / (k - 1); n     int count = 0; n     int flag = 0; n     for (int i = 0; i < n; i++) { n       if (!(i & 1)) { n         for (int j = 0; j < m; j++) { n           if (count == k - 1 && flag == 0) { n             div = (n * m - (k - 1) * (div)); n             flag = 1; n           } n           cur.push_back(i + 1); n           cur.push_back(j + 1); n           if (cur.size() >= div * 2) { n             vector<int> add = cur; n             v.push_back(add); n             cur.clear(); n             count++; n           } n         } n       } else { n         for (int j = m - 1; j >= 0; j--) { n           if (count == k - 1 && flag == 0) { n             div = (n * m - (k - 1) * (div)); n             flag = 1; n           } n           if (cur.size() <= div * 2) { n             cur.push_back(i + 1); n             cur.push_back(j + 1); n           } n           if (cur.size() >= div * 2) { n             vector<int> add = cur; n             v.push_back(add); n             cur.clear(); n             count++; n           } n         } n       } n     } n   } n   for (int i = 0; i < v.size(); i++) { n     int s = v[i].size(); n     s /= 2; n     cout << s <<    ; n     for (int j = 0; j < v[i].size(); j++) { n       cout << v[i][j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 7; n int n; n int v[N], f[N]; n bool e[N]; n int st, dr; n bool ok(int l, int r) { n   for (int i = 1; i <= n; i++) { n     e[i] = 0; n     f[i] = 0; n   } n   for (int i = l; i <= r; i++) { n     e[i] = 1; n     f[v[i]]++; n   } n   for (int i = 1; i < n + 1 - i; i++) { n     if (e[i] == 1 && e[n + 1 - i] == 0) { n       if (f[v[n + 1 - i]] > 0) { n         f[v[n + 1 - i]]--; n       } else { n         return 0; n       } n     } n     if (e[i] == 0 && e[n + 1 - i] == 1) { n       if (f[v[i]] > 0) { n         f[v[i]]--; n       } else { n         return 0; n       } n     } n     if (e[i] == 0 && e[n + 1 - i] == 0 && v[i] != v[n + 1 - i]) { n       return 0; n     } n   } n   int cntimp = 0; n   for (int i = 1; i <= n; i++) { n     if (f[i] % 2 == 1) { n       cntimp++; n     } n   } n   return (cntimp <= 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n     f[v[i]]++; n   } n   int imp = 0; n   for (int i = 1; i <= n; i++) { n     if (f[i] % 2 == 1) { n       imp++; n     } n   } n   if (imp >= 2) { n     cout <<  0 n ; n     return 0; n   } n   bool pali = 1; n   for (int i = 1; i < n + 1 - i; i++) { n     if (v[i] != v[n + 1 - i]) { n       pali = 0; n     } n   } n   if (pali) { n     long long res = n * (long long)(n + 1) >> 1; n     cout << res <<   n ; n     return 0; n   } n   st = 1; n   dr = n; n   while (v[st] == v[dr]) { n     st++; n     dr--; n   } n   int lo = st; n   int hi = dr; n   int prf = -1, suf = -1; n   while (lo <= hi) { n     int mid = (lo + hi) >> 1; n     if (ok(st, mid)) { n       prf = mid; n       hi = mid - 1; n     } else { n       lo = mid + 1; n     } n   } n   lo = 1; n   hi = n; n   while (lo <= hi) { n     int mid = (lo + hi) >> 1; n     if (ok(mid, dr)) { n       suf = mid; n       lo = mid + 1; n     } else { n       hi = mid - 1; n     } n   } n   int cntgood = 0; n   if (prf != -1) { n     cntgood += (dr - prf + 1); n   } n   if (suf != -1) { n     cntgood += (suf - st + 1); n   } n   long long res = n       cntgood * (long long)st - 1 + (st - 1LL) * (long long)(st - 1LL); n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a1, a2, b1, b2, c1, c2; n void rec(long long p, long long q) { n   long long mod = c1 * c1 + c2 * c2; n   if ((p * c1 + q * c2) % mod == 0 && (q * c1 - p * c2) % mod == 0) { n     puts( YES ); n     exit(0); n   } n } n int main() { n   scanf( %I64d%I64d%I64d%I64d%I64d%I64d , &a1, &a2, &b1, &b2, &c1, &c2); n   if (c1 == 0 && c2 == 0) { n     if ((a1 == b1 && a2 == b2) || (a1 == -b1 && a2 == -b2) || n         (a1 == b2 && a2 == -b1) || (a1 == -b2 && a2 == b1)) n       puts( YES ); n     else n       puts( NO ); n     return 0; n   } n   rec(b1 - a1, b2 - a2); n   rec(b1 + a1, b2 + a2); n   rec(b1 + a2, b2 - a1); n   rec(b1 - a2, b2 + a1); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int l, r, x, y; n   double k; n   cin >> l >> r >> x >> y >> k; n   bool flag = false; n   double value; n   while (x <= y) { n     value = (double)x * k; n     if (value >= l && value <= r) { n       flag = true; n       break; n     } n     x++; n   } n   if (flag) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n const int inf = 1000 * 1000 * 1000; n const int mod = 998244353; n int n; n vector<int> g[N]; n int dp[N][3]; n int sz[N][3]; n void dfs(int v, int p) { n   dp[v][0] = dp[v][1] = 0; n   sz[v][0] = 1; n   sz[v][1] = 0; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     dfs(to, v); n     int d0 = dp[v][0] * 1ll * (sz[to][0] + sz[to][1]) % mod + n              (dp[to][0] + dp[to][1]) * 1ll * sz[v][0] % mod; n     d0 %= mod; n     int s0 = sz[v][0] * 1ll * (sz[to][0] + sz[to][1]) % mod; n     int d1 = dp[v][1] * 1ll * (sz[to][0] + sz[to][1]) % mod + n              (dp[to][0] + dp[to][1]) * 1ll * sz[v][1] % mod; n     d1 %= mod; n     int s1 = sz[v][1] * 1ll * (sz[to][0] + sz[to][1]) % mod; n     int dd0 = n         dp[v][0] * 1ll * sz[to][1] % mod + dp[to][1] * 1ll * sz[v][0] % mod; n     dd0 %= mod; n     int ss0 = sz[v][0] * 1ll * sz[to][1] % mod; n     int dd1 = dp[v][1] * 1ll * (sz[to][0] + sz[to][1]) % mod + n               (dp[to][0] + dp[to][1]) * 1ll * sz[v][1] % mod; n     dd1 %= mod; n     dd1 += dp[v][0] * 1ll * sz[to][0] % mod; n     dd1 %= mod; n     dd1 += sz[v][0] * 1ll * dp[to][0] % mod; n     dd1 %= mod; n     int ss1 = sz[v][1] * 1ll * (sz[to][0] + sz[to][1]) % mod + n               sz[v][0] * 1ll * sz[to][0] % mod; n     ss1 %= mod; n     dd1 += sz[v][0] * 1ll * sz[to][0] % mod; n     dd1 %= mod; n     dp[v][0] = (d0 + dd0) % mod; n     sz[v][0] = (s0 + ss0) % mod; n     dp[v][1] = (d1 + dd1) % mod; n     sz[v][1] = (s1 + ss1) % mod; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(1, -1); n   int answ = (dp[1][0] + dp[1][1]) % mod; n   answ *= 2; n   answ %= mod; n   cout << answ << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Tpoint { n   double x, y; n } v[200], pg[200]; n struct Tline { n   Tpoint a, b; n } ls[200]; n int n, tot, q[200], ord[200]; n double at2[200]; n double getdouble() { n   double d = 0, d2, d3; n   char ch =    ; n   bool flag = false; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) break; n   if (ch ==  - ) n     flag = 1; n   else n     d = ch - 48; n   while (isdigit(ch = getchar())) d = d * 10 + ch - 48; n   if (ch ==  - ) { n     d2 = 0; n     d3 = 1; n     while (isdigit(ch = getchar())) d2 = d2 * 10 + ch - 48, d3 /= 10; n     d += d2 * d3; n   } n   if (flag) n     return -d; n   else n     return d; n } n void init() { n   int i, w, h; n   double angle; n   n = 4; n   scanf( %d%d%lf , &w, &h, &angle); n   v[1].x = (double)w / 2; n   v[1].y = -(double)h / 2; n   v[2].x = (double)w / 2; n   v[2].y = (double)h / 2; n   v[3].x = -(double)w / 2; n   v[3].y = (double)h / 2; n   v[4].x = -(double)w / 2; n   v[4].y = -(double)h / 2; n   for (int i = 1; i < n; i++) ls[i].a = v[i], ls[i].b = v[i + 1]; n   ls[n].a = v[n]; n   ls[n].b = v[1]; n   angle = angle * 3.1415926535897932384626433832795 / 180; n   for (i = 1; i <= 4; i++) { n     v[i + n].x = v[i].x * cos(angle) - v[i].y * sin(angle); n     v[i + n].y = v[i].x * sin(angle) + v[i].y * cos(angle); n   } n   for (int i = 1; i < n; i++) n     ls[n + i].a = v[n + i], ls[n + i].b = v[n + i + 1]; n   ls[n + n].a = v[n + n]; n   ls[n + n].b = v[n + 1]; n   n = n + n; n } n int sig(double d) { return d < -1e-8 ? -1 : d > 1e-8; } n double multi(Tpoint p0, Tpoint p1, Tpoint p2) { n   return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y); n } n int cmp(const void *a, const void *b) { n   int p1 = *(int *)a, p2 = *(int *)b; n   if (sig(at2[p1] - at2[p2]) == 0) { n     return -sig(multi(ls[p2].a, ls[p2].b, ls[p1].b)); n   } else n     return sig(at2[p2] - at2[p1]); n } n Tpoint ispoint(Tline l1, Tline l2) { n   double dot1, dot2; n   Tpoint I; n   dot1 = multi(l2.a, l1.b, l1.a); n   dot2 = multi(l1.b, l2.b, l1.a); n   I.x = (l2.a.x * dot2 + l2.b.x * dot1) / (dot1 + dot2); n   I.y = (l2.a.y * dot2 + l2.b.y * dot1) / (dot1 + dot2); n   return I; n } n bool judgein(int x, int y, int z) { n   Tpoint pnt = ispoint(ls[x], ls[y]); n   return sig(multi(ls[z].a, ls[z].b, pnt)) < 0; n } n void halfplane() { n   int i, tmp, front, rear; n   for (i = 1; i <= n; i++) { n     at2[i] = atan2(ls[i].b.y - ls[i].a.y, ls[i].b.x - ls[i].a.x); n     ord[i] = i; n   } n   qsort(ord + 1, n, sizeof(int), cmp); n   for (i = 2, tmp = 1; i <= n; i++) { n     if (sig(at2[ord[i]] - at2[ord[i - 1]]) != 0) ord[++tmp] = ord[i]; n   } n   front = 1; n   rear = 2; n   q[1] = ord[1]; n   q[2] = ord[2]; n   for (i = 3; i <= tmp; i++) { n     while (front < rear && judgein(q[rear - 1], q[rear], ord[i])) rear--; n     while (front < rear && judgein(q[front + 1], q[front], ord[i])) front++; n     q[++rear] = ord[i]; n   } n   while (front < rear && judgein(q[rear - 1], q[rear], q[front])) rear--; n   while (front < rear && judgein(q[front + 1], q[front], q[rear])) front++; n   q[--front] = q[rear]; n   tot = 0; n   for (i = front + 1; i <= rear; i++) { n     pg[++tot] = ispoint(ls[q[i - 1]], ls[q[i]]); n   } n } n void work() { n   halfplane(); n   int i; n   double ans = 0; n   for (i = 1; i < tot; i++) { n     ans += multi(pg[1], pg[i], pg[i + 1]); n   } n   ans += multi(pg[1], pg[tot], pg[1]); n   ans /= 2; n   if (fabs(ans) > 0) n     printf( %.8lf , fabs(ans)); n   else n     printf( 0.0 ); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <iostream> n #include <string> n #include <algorithm> n #include <map> n #include <vector> n #include <cmath> n #include <queue> n #include <set> n #include <bitset> n  n using namespace std; n  n typedef long long ll; n ll MOD = 1000000007; n  n void solve(int testCase) n { n  ll n; n  cin >> n; n  for (int i = 0; i * 2020 <= n; i++) n  { n   if ((n - i * 2020) % 2021 == 0) n   { n    cout <<  YES  << endl; n    return; n   } n  } n  cout <<  NO  << endl; n  n } n   n int main() n { n  int t; n  cin >> t; n  //t = 1; n  int i = 1; n  while (t--) n  { n   solve(i++); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void chkmin(T &a, const T &b) { n   a = a < b ? a : b; n } n template <typename T> n inline void chkmax(T &a, const T &b) { n   a = a > b ? a : b; n } n const int MAXN = 150005, MAXT = 300005; n int sz[MAXN], val[MAXN], vis[MAXN], n, tot; n vector<int> gra[MAXN]; n struct Tree { n   long long k, b; n   Tree *ls, *rs; n   bool did; n } nd[MAXT], *rt; n vector<Tree *> cls; n long long ans; n Tree *build(int l, int r) { n   Tree *cur = nd + (tot++); n   cur->did = false; n   if (l == r) return cur; n   int mid = (l + r) >> 1; n   cur->ls = build(l, mid); n   cur->rs = build(mid + 1, r); n   return cur; n } n void modify(long long k, long long b, int l = 1, int r = n, Tree *p = rt) { n   if (p->did) { n     long long f1 = p->k * l + p->b, f2 = p->k * r + p->b, g1 = k * l + b, n               g2 = k * r + b; n     if (g1 >= f1 && g2 >= f2) { n       p->k = k, p->b = b; n       return; n     } else if (g1 <= f1 && g2 <= f2) n       return; n     double in = 1.0 * (b - p->b) / (k - p->k); n     int mid = (l + r) >> 1; n     if (in <= mid) { n       if (f1 >= g1 && f2 <= g2) { n         modify(p->k, p->b, l, mid, p->ls); n         p->k = k, p->b = b; n       } else n         modify(k, b, l, mid, p->ls); n     } else { n       if (f1 <= g1 && f2 >= g2) { n         modify(p->k, p->b, mid + 1, r, p->rs); n         p->k = k, p->b = b; n       } else n         modify(k, b, mid + 1, r, p->rs); n     } n   } else n     p->k = k, p->b = b, p->did = true, cls.push_back(p); n } n long long query(int x, int l = 1, int r = n, Tree *p = rt) { n   if (!p->did) return -1e18; n   int mid = (l + r) >> 1; n   long long res = p->k * x + p->b; n   if (x <= mid) n     chkmax(res, query(x, l, mid, p->ls)); n   else n     chkmax(res, query(x, mid + 1, r, p->rs)); n   return res; n } n void get_sz(int u, int fa) { n   sz[u] = 1; n   for (int v : gra[u]) n     if (v != fa && !vis[v]) { n       get_sz(v, u); n       sz[u] += sz[v]; n     } n } n pair<int, int> get_rt(int u, int fa, int s) { n   auto res = make_pair(MAXN, MAXN); n   int mn = s; n   for (int v : gra[u]) n     if (v != fa && !vis[v]) { n       res = min(res, get_rt(v, u, s + sz[u] - sz[v])); n       chkmax(mn, sz[v]); n     } n   return min(res, make_pair(mn, u)); n } n void dfs1(int u, int fa, int dep, long long sum, long long cur, long long tmp) { n   sum += val[u], cur += sum, ++dep, tmp += (long long)dep * val[u]; n   chkmax(ans, cur), chkmax(ans, tmp); n   chkmax(ans, query(dep) + cur); n   for (int v : gra[u]) n     if (v != fa && !vis[v]) dfs1(v, u, dep, sum, cur, tmp); n } n void dfs2(int u, int fa, int dep, long long sum, long long cur) { n   ++dep, sum += val[u], cur += (long long)dep * val[u]; n   modify(sum, cur); n   for (int v : gra[u]) n     if (v != fa && !vis[v]) dfs2(v, u, dep, sum, cur); n } n void divide(int u) { n   get_sz(u, 0); n   u = get_rt(u, 0, 0).second; n   vis[u] = 1; n   for (int v : gra[u]) n     if (!vis[v]) { n       dfs1(v, u, 1, val[u], val[u], val[u]); n       dfs2(v, u, 0, 0, 0); n     } n   for (Tree *p : cls) p->did = false; n   cls.clear(); n   reverse(gra[u].begin(), gra[u].end()); n   for (int v : gra[u]) n     if (!vis[v]) { n       dfs1(v, u, 1, val[u], val[u], val[u]); n       dfs2(v, u, 0, 0, 0); n     } n   for (Tree *p : cls) p->did = false; n   cls.clear(); n   for (int v : gra[u]) n     if (!vis[v]) divide(v); n   vis[u] = 0; n } n int main() { n   scanf( %d , &n); n   rt = build(1, n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     gra[u].push_back(v); n     gra[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) scanf( %d , val + i); n   divide(1); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[200007]; n int brr[200007]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     cin >> n >> x; n     map<int, int> mpp; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       mpp[arr[i]]++; n     } n     int cnt = 0; n     for (int i = 1;; i++) { n       if (x == 0) break; n       if (mpp[i] == 0) { n         mpp[i]++; n         x--; n       } n     } n     for (int i = 1;; i++) { n       if (mpp[i] == 0) n         break; n       else n         cnt++; n     } n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n int mark[N], t[N], n; n long long res; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n; n   for (int i = (1); i <= (n); i += (1)) cin >> mark[i]; n   int cur = 0; n   for (int i = (n); i >= (1); i -= (1)) { n     cur = max(0, cur - 1); n     cur = max(cur, mark[i] + 1); n     t[i] = cur; n   } n   cur = 0; n   for (int i = (1); i <= (n); i += (1)) { n     cur = max(cur, t[i]); n     res += cur; n   } n   for (int i = (1); i <= (n); i += (1)) res -= (mark[i] + 1); n   cout << res <<   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 t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     sort(arr, arr + n); n     int ans = 1; n     for (int i = 1; i < n; i++) { n       if (arr[i] > arr[i - 1]) ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace Base { n const int inf = 0x3f3f3f3f, INF = 0x7fffffff; n const long long infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll; n template <typename T> n void read(T &x) { n   x = 0; n   int fh = 1; n   double num = 1.0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   if (ch ==  . ) { n     ch = getchar(); n     while (isdigit(ch)) { n       num /= 10; n       x = x + num * (ch -  0 ); n       ch = getchar(); n     } n   } n   x = x * fh; n } n template <typename T> n void chmax(T &x, T y) { n   x = x < y ? y : x; n } n template <typename T> n void chmin(T &x, T y) { n   x = x > y ? y : x; n } n }  // namespace Base n using namespace Base; n const int N = 510; n bool tag[N]; n int nxt[N], n, m, len[N], pre[N][N], bak[N][N], now[N], ch[N]; n long long k, cnt[N]; n void kmp(int *a, int lena, int *b, int lenb) { n   memset(tag, 0, sizeof(tag)); n   for (int i = 1; i <= lenb; i++) { n     int p = i - 1; n     while (p != 0 && b[nxt[p] + 1] != b[i]) p = nxt[p]; n     if (b[nxt[p] + 1] != b[i] || i == 1) n       nxt[i] = 0; n     else n       nxt[i] = nxt[p] + 1; n   } n   for (int i = 1, j = 1; i <= lena; i++) { n     while (j != 1 && a[i] != b[j]) j = nxt[j - 1] + 1; n     if (a[i] == b[j]) j++; n     if (j > lenb) { n       tag[i] = true, j = nxt[j - 1] + 1; n     } n   } n } n int main() { n   read(n); n   read(k); n   read(m); n   k = k + 1; n   len[0] = len[1] = 1; n   pre[0][1] = 0; n   pre[1][1] = 1; n   bak[0][1] = 0; n   bak[1][1] = 1; n   for (int j = 2; j <= n; j++) { n     for (int k = 1; k <= len[j - 2]; k++) pre[j][k] = pre[j - 2][k]; n     if (len[j - 2] < m) { n       for (int k = len[j - 2] + 1; k <= min(len[j - 2] + len[j - 1], m); k++) n         pre[j][k] = pre[j - 1][k - len[j - 2]]; n     } n     int tmp = 0; n     if (len[j - 1] < m) { n       tmp = min(len[j - 2], m - len[j - 1]); n       for (int k = 1; k <= tmp; k++) n         bak[j][k] = bak[j - 2][len[j - 2] - tmp + k]; n     } n     for (int k = 1; k <= len[j - 1]; k++) bak[j][k + tmp] = bak[j - 1][k]; n     len[j] = min(m, len[j - 1] + len[j - 2]); n   } n   for (int i = 1; i <= m; i++) { n     bool flag = false; n     for (int j = 1; j <= i - 1; j++) n       if (bak[n][len[n] - i + j + 1] != ch[j]) { n         flag = true; n         break; n       } n     if (flag == false) { n       k--; n       if (k == 0) { n         for (int j = 1; j <= i - 1; j++) printf( %d , ch[j]); n         printf(  n ); n         exit(0); n       } n     } n     flag = false; n     ch[i] = 0; n     memset(cnt, 0, sizeof(cnt)); n     if (i == 1) cnt[0] = 1; n     for (int j = 2; j <= n; j++) { n       cnt[j] = cnt[j - 1] + cnt[j - 2]; n       if (cnt[j] >= k) { n         flag = true; n         break; n       } n       int id = 0, cut = 0; n       for (int k = 1; k <= len[j - 2]; k++) now[++id] = bak[j - 2][k]; n       cut = id; n       for (int k = 1; k <= len[j - 1]; k++) now[++id] = pre[j - 1][k]; n       kmp(now, id, ch, i); n       for (int k = cut + 1; k <= min(cut + i - 1, id); k++) n         if (tag[k] == true) cnt[j]++; n     } n     if (cnt[n] >= k || flag == true) n       ch[i] = 0; n     else n       ch[i] = 1, k -= cnt[n]; n   } n   for (int i = 1; i <= m; i++) printf( %d , ch[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[100000], p[100000]; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i] >> p[i]; n   for (int i = 1; i < n; ++i) p[i] = min(p[i], p[i - 1]); n   int ans = 0; n   for (int i = 0; i < n; ++i) ans += p[i] * a[i]; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace ::std; n const long long maxn = 52; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 500; n long long dp[2][maxn][maxn][maxn][maxn]; n long long d[maxn]; n bool bit; n long long kv; n void add(long long a, long long b, long long c, long long d, long long z) { n   if (0 <= a && 0 <= b && 0 <= c && 0 <= d && a < maxn && b < maxn && n       c < maxn && d < maxn) { n     dp[bit][a][b][c][d] += z * kv; n     dp[bit][a][b][c][d] %= mod; n   } n } n int main() { n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> d[i]; n   } n   long long a1 = 0, a2 = 0, b1 = 0, b2 = 0; n   if (d[0] == 2) { n     b1++; n   } else { n     b2++; n   } n   if (d[1] == 2) { n     a1++; n   } else { n     a2++; n   } n   dp[1][a1][a2][b1][b2] = 1; n   for (long long i = 2; i < n; i++) { n     bit = (i & 1); n     long long k = i; n     for (long long a1 = 0; a1 <= k; a1++) { n       for (long long a2 = 0; a2 + a1 <= k; a2++) { n         for (long long b1 = 0; b1 + a1 + a2 <= k; b1++) { n           for (long long b2 = 0; b2 + a1 + a2 + b1 <= k; b2++) { n             dp[bit][a1][a2][b1][b2] = 0; n           } n         } n       } n     } n     for (long long a1 = 0; a1 <= k; a1++) { n       for (long long a2 = 0; a2 + a1 <= k; a2++) { n         for (long long b1 = 0; b1 + a1 + a2 <= k; b1++) { n           for (long long b2 = 0; b2 + a1 + a2 + b1 <= k; b2++) { n             kv = dp[!bit][a1][a2][b1][b2]; n             if (d[i] == 2) { n               add(a1 - 1, a2, b1 - 1, b2, a1 * b1); n               add(a1 + 1, a2 - 1, b1 - 1, b2, a2 * b1); n               add(a1 + 1, a2, b1 - 1, b2, b1); n               add(a1 - 1, a2, b1 + 1, b2 - 1, a1 * b2); n               add(a1 + 1, a2 - 1, b1 + 1, b2 - 1, a2 * b2); n               add(a1 + 1, a2, b1 + 1, b2 - 1, b2); n               if (b1 == 0 && b2 == 0) { n                 add(1, 0, a1 - 1, a2, a1); n                 add(1, 0, a1 + 1, a2 - 1, a2); n               } n             } else { n               if (b1 == 0 && b2 == 0) { n                 add(0, 1, a1 - 1, a2, a1); n                 add(0, 1, a1 + 1, a2 - 1, a2); n               } n               add(a1, a2 + 1, b1 - 1, b2, b1); n               add(a1 + 1 + 1, a2 - 1, b1 - 1, b2, b1 * a2); n               add(a1 + 1 - 1, a2, b1 - 1, b2, b1 * a1); n               add(a1 - 2, a2, b1 - 1, b2, b1 * (a1 * (a1 - 1)) / 2); n               add(a1 + 2, a2 - 2, b1 - 1, b2, b1 * (a2 * (a2 - 1)) / 2); n               add(a1 + 1 - 1, a2 - 1, b1 - 1, b2, b1 * a1 * a2); n               add(a1, a2 + 1, b1 + 1, b2 - 1, b2); n               add(a1 + 1 + 1, a2 - 1, b1 + 1, b2 - 1, b2 * a2); n               add(a1 + 1 - 1, a2, b1 + 1, b2 - 1, b2 * a1); n               add(a1 - 2, a2, b1 + 1, b2 - 1, b2 * (a1 * (a1 - 1)) / 2); n               add(a1 + 2, a2 - 2, b1 + 1, b2 - 1, b2 * (a2 * (a2 - 1)) / 2); n               add(a1 + 1 - 1, a2 - 1, b1 + 1, b2 - 1, b2 * a1 * a2); n             } n           } n         } n       } n     } n     k++; n     for (long long a1 = 0; a1 <= k; a1++) { n       for (long long a2 = 0; a2 + a1 <= k; a2++) { n         for (long long b1 = 0; b1 + a1 + a2 <= k; b1++) { n           for (long long b2 = 0; b2 + a1 + a2 + b1 <= k; b2++) { n             dp[bit][a1][a2][b1][b2] %= mod; n           } n         } n       } n     } n   } n   long long ans = 0; n   bool bb = (n - 1) & 1; n   ans = dp[bb][0][0][0][0]; n   cout << ans % mod; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, m = 1e9 + 7; n   cin >> n >> k; n   int a[100005]; n   a[0] = 1; n   for (int i = 1; i < 100005; ++i) { n     if (i >= k) { n       a[i] = (a[i - k] + a[i - 1]) % m; n     } else { n       a[i] = a[i - 1]; n     } n   } n   for (int i = 1; i < 100005; ++i) { n     a[i] = (a[i] + a[i - 1]) % m; n   } n   while (n--) { n     int x, y; n     scanf( %d %d , &x, &y); n     printf( %d n , (a[y] - a[x - 1] + m) % m); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void write(vector<int> &v) { n   for (auto i : v) cout << i <<    ; n   cout <<   n ; n } n void read(vector<int> &v) { n   for (auto &i : v) cin >> i; n } n const int INF = 1e9; n const int64_t INFF = 1e18; n const int N = 1e6 + 69; n void solve() { n   int n; n   cin >> n; n   string s1, s2; n   cin >> s1 >> s2; n   int ans = 0; n   vector<int> pos(n, 1); n   for (int i = 0; i < n; i++) { n     if (s2[i] ==  1 ) { n       if (s1[i] ==  0 ) { n         ans++; n         pos[i] = 0; n       } else if (i > 0 && s1[i - 1] ==  1  && pos[i - 1]) { n         ans++; n         pos[i - 1] = 0; n       } else if (i < n - 1 && s1[i + 1] ==  1  && pos[i + 1]) { n         ans++; n         pos[i + 1] = 0; n       } n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   cin >> t; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long p = 1e9 + 7; n inline long long read() { n   long long neg = 1, num = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) neg = -1; n   for (; isdigit(c); c = getchar()) num = (num << 1) + (num << 3) + c -  0 ; n   return neg * num; n } n long long n, k, tot, f[200010], g[200010], fac[200010], inv[200010], n     pri[200010], mu[200010], mul[200010]; n bool vis[200010]; n inline long long Pow(long long x, long long y) { n   long long res = 1; n   for (; y; y >>= 1, x = x * x % p) n     if (y & 1) res = res * x % p; n   return res; n } n inline long long C(long long x, long long y) { n   return fac[x] * inv[y] % p * inv[x - y] % p; n } n inline long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } n inline void sieve() { n   vis[1] = mu[1] = 1; n   for (long long i = 2; i <= n; i++) { n     if (!vis[i]) pri[++tot] = i, mu[i] = -1; n     for (long long j = 1; j <= tot && pri[j] * i <= n; j++) { n       vis[i * pri[j]] = 1; n       if (i % pri[j] == 0) break; n       mu[i * pri[j]] = -mu[i]; n     } n   } n   fac[0] = inv[0] = mul[0] = 1; n   for (long long i = 1; i <= 2 * n; i++) n     fac[i] = fac[i - 1] * i % p, mul[i] = mul[i - 1] * k % p; n   inv[2 * n] = Pow(fac[2 * n], p - 2); n   for (long long i = 2 * n - 1; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % p; n   for (long long i = 2; i <= n; i++) n     f[i] = (f[i - 1] + (i - 1) * mul[i] % p) % p; n   for (long long i = 1; i <= n; i++) g[i] = Pow(f[n / i], i) % p; n } n signed main() { n   n = read(); n   k = read(); n   sieve(); n   long long ans = 0, sub = 0; n   for (long long i = 2; i <= 2 * n; i++) { n     if (i <= n) n       ans = (ans + (i - 1) * mul[i] % p) % p; n     else n       ans = (ans + (2 * n - i + 1) * mul[i] % p) % p; n   } n   ans = ans * Pow(mul[n], p - 2) % p; n   for (long long d = 1; d <= n; d++) { n     for (long long T = d; T <= n; T += d) { n       long long s1 = (n / T) * (n / T - 1) / 2 % p, s2 = g[T]; n       if ((n + d - 1) / T != n / T) n         s1 = (s1 + (n + d - 1) / T - 1) % p, n         s2 = (s2 + ((n + d - 1) / T - 1) * mul[(n + d - 1) / T * T] % p) % p; n       ans = (ans + s1 * mul[d] * mu[T / d] % p) % p; n       sub = (sub + s2 * mu[T / d]) % p; n     } n   } n   sub = sub * Pow(mul[n], p - 2) % p; n   ans = (ans - sub) % p; n   for (long long i = 1; i <= n; i++) n     ans = (ans - (1 + (i != n)) * mul[max(gcd(i, n), i)]) % p; n   ans = ans * Pow(mul[n], p - 2) % p; n   ans = (ans % p + p) % p; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[100020], nxt[100020]; n struct point { n   int st, en, num; n } pnt[100020 * 4]; n int max1(int a, int b) { n   if (a > b) return a; n   return b; n } n int main() { n   int n, sum = 0, cnt = 0, besti, min1 = 1 << 25, tmp, i; n   scanf( %d , &n); n   num[n + 1] = nxt[0] = num[0] = 0; n   for (i = 1; i <= n; i++) { n     scanf( %d , &num[i]), nxt[i] = i; n   } n   for (i = 0; i < 100020 * 3; i++) pnt[i].st = pnt[i].en = pnt[i].num = 0; n   for (i = 1; i <= n + 1; i++) { n     if (num[i] >= num[i - 1]) { n       sum += num[i] - num[i - 1]; n     } else { n       besti = i - 1; n       while (num[besti] > num[i]) { n         tmp = max1(num[nxt[besti] - 1], num[i]); n         pnt[cnt].en = i - 1; n         pnt[cnt].st = nxt[besti], pnt[cnt++].num = num[besti] - tmp; n         nxt[i] = nxt[besti]; n         besti = nxt[besti] - 1; n       } n       if (num[besti] >= num[i]) nxt[i] = nxt[besti]; n     } n   } n   printf( %d n , sum); n   for (i = 0; i < cnt; i++) { n     for (int j = 0; j < pnt[i].num; j++) n       printf( %d %d n , pnt[i].st, pnt[i].en); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> mp; n map<int, string> g; n int n, k, cnt; n char name1[10100][30], name2[10100][30]; n int mark[1010][1010]; n int x[10100], y[10100], tt[10100]; n int ans[10100], num; n struct Node { n   int l, r; n } node[10100]; n struct Mark { n   int l, r; n   Mark() {} n   Mark(int a, int b) { n     if (a > b) swap(a, b); n     l = a; n     r = b; n   } n   bool operator<(const Mark a) const { n     if (l == a.l) return r < a.r; n     return l < a.l; n   } n }; n map<Mark, int> vis; n int vv; n int main() { n   string s1, s2; n   int u, v, t; n   while (scanf( %d%d , &n, &k) == 2) { n     cnt = 0; n     num = 0; n     vv = 0; n     vis.clear(); n     mp.clear(); n     for (int i = 0; i < n; i++) { n       scanf( %s%s%d , name1[i], name2[i], &t); n       s1 = name1[i]; n       s2 = name2[i]; n       if (!mp.count(s1)) { n         mp[s1] = ++cnt; n         g[cnt] = s1; n       } n       u = mp[s1]; n       if (!mp.count(s2)) { n         mp[s2] = ++cnt; n         g[cnt] = s2; n       } n       v = mp[s2]; n       x[i] = u; n       y[i] = v; n       tt[i] = t; n       for (int j = 0; j < n; j++) { n         if (tt[i] != tt[j] && tt[i] - tt[j] <= k) { n           if (x[i] == y[j] && x[j] == y[i]) { n             if (!vis.count(Mark(x[i], y[i]))) { n               node[num].l = x[i]; n               node[num++].r = y[i]; n               vis[Mark(x[i], y[i])] = vv++; n             } n           } n         } n       } n     } n     printf( %d n , num); n     for (int i = 0; i < num; i++) n       cout << g[node[i].l] <<     << g[node[i].r] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int LG_N = 20; n struct node { n   int l, r, cnt; n   node *left, *right; n   node(int _l, int _r, int _c = 0) : l(_l), r(_r), cnt(_c) {} n }; n node *init(int l, int r) { n   node *p = new node(l, r); n   if (l < r) { n     int mid = (l + r) / 2; n     p->left = init(l, mid); n     p->right = init(mid + 1, r); n   } n   return p; n } n node *update(node *p, int idx) { n   node *q = new node(p->l, p->r, p->cnt + 1); n   if (p->l < p->r) { n     int mid = (p->l + p->r) / 2; n     q->left = (idx <= mid ? update(p->left, idx) : p->left); n     q->right = (idx > mid ? update(p->right, idx) : p->right); n   } n   return q; n } n int seg_query(node *p, int l, int r) { n   if (p->r < l or r < p->l) return 0; n   if (l <= p->l and p->r <= r) return p->cnt; n   return seg_query(p->left, l, r) + seg_query(p->right, l, r); n } n struct Query { n   int type, u, v, k, y; n   void read(void) { n     scanf( %d , &type); n     if (type == 1) { n       scanf( %d , &u); n     } else { n       scanf( %d%d%d%d , &u, &v, &k, &y); n     } n   } n } query[N]; n node *head[N]; n vector<int> adj[N]; n int P[N][LG_N], H[N]; n int attack_year[N]; n int n, m; n int root = 1; n int count(int u, int v, int l, int r) { n   int ans = 0; n   if (l > 0) { n     ans += seg_query(head[u], 1, l - 1); n     ans -= seg_query(head[v], 1, l - 1); n   } n   if (r < m + 1) { n     ans += seg_query(head[u], r + 1, m + 1); n     ans -= seg_query(head[v], r + 1, m + 1); n   } n   return ans; n } n int my_advance(int u, int l) { n   for (int i = LG_N - 1; i >= 0; i--) { n     if ((1 << i) <= l) { n       l -= 1 << i; n       u = P[u][i]; n     } n   } n   return u; n } n int lca(int u, int v) { n   if (H[u] < H[v]) swap(u, v); n   for (int i = LG_N - 1; i >= 0; i--) { n     if (H[P[u][i]] >= H[v]) { n       u = P[u][i]; n     } n   } n   if (u == v) return u; n   for (int i = LG_N - 1; i >= 0; i--) { n     if (P[u][i] != P[v][i]) { n       u = P[u][i]; n       v = P[v][i]; n     } n   } n   return P[u][0]; n } n void dfsInit(int u, int h) { n   H[u] = h; n   for (int i = 1; i < LG_N; i++) { n     P[u][i] = P[P[u][i - 1]][i - 1]; n   } n   head[u] = update(u == root ? (head[0] = init(0, m + 1)) : head[P[u][0]], n                    attack_year[u]); n   for (int v : adj[u]) { n     dfsInit(v, h + 1); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int u; n     scanf( %d , &u); n     if (u == 0) { n       root = i; n     } n     adj[u].push_back(i); n     P[i][0] = u; n   } n   scanf( %d , &m); n   for (int i = 1; i <= n; i++) { n     attack_year[i] = m + 1; n   } n   for (int i = 1; i <= m; i++) { n     query[i].read(); n     if (query[i].type == 1) { n       attack_year[query[i].u] = i; n     } n   } n   H[0] = 0; n   dfsInit(root, 1); n   for (int i = 1; i <= m; i++) { n     if (query[i].type == 1) continue; n     int u = query[i].u, v = query[i].v, k = query[i].k, y = query[i].y; n     int l = lca(u, v); n     k += (attack_year[u] < y + 1 or attack_year[u] > i); n     int lo = 0, hi = H[u] - H[l]; n     while (lo < hi) { n       int mid = (lo + hi) / 2; n       if (count(u, my_advance(u, mid + 1), y + 1, i) >= k) { n         hi = mid; n       } else { n         lo = mid + 1; n       } n     } n     k -= count(u, my_advance(u, lo + 1), y + 1, i); n     u = my_advance(u, lo); n     if (k == 0) { n       printf( %d n , (u == v ? -1 : u)); n       continue; n     } n     k = count(v, l, y + 1, i) - k + 1; n     if (k >= 0) { n       lo = 0, hi = H[v] - H[l]; n       while (lo < hi) { n         int mid = (lo + hi) / 2; n         if (count(v, my_advance(v, mid + 1), y + 1, i) >= k) { n           hi = mid; n         } else { n           lo = mid + 1; n         } n       } n       k -= count(v, my_advance(v, lo + 1), y + 1, i); n       v = my_advance(v, lo); n       if (lo == 0) { n         k = -1; n       } n     } n     if (k == 0) { n       printf( %d n , v); n     } else { n       printf( -1 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int a[maxn], n, s, b[maxn], num[maxn], len; n int ufs[maxn], meme[maxn]; n inline int find(int u) { n   if (ufs[u] == u) n     return u; n   else n     return ufs[u] = find(ufs[u]); n } n inline void join(int u, int v, int e) { n   u = find(u), v = find(v); n   if (u == v) return; n   ufs[v] = u; n   meme[u] = e; n } n vector<vector<int> > mema; n vector<pair<int, int> > adj[maxn]; n vector<int> curm; n inline void dfs(int u) { n   while (adj[u].size()) { n     int v = adj[u].back().first, id = adj[u].back().second; n     adj[u].pop_back(); n     dfs(v); n     curm.push_back(id); n   } n } n int main() { n   cin >> n >> s; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     num[++len] = a[i]; n   } n   sort(num + 1, num + len + 1); n   len = unique(num + 1, num + len + 1) - num - 1; n   for (int i = 1; i <= n; i++) { n     a[i] = lower_bound(num + 1, num + len + 1, a[i]) - num; n     b[i] = a[i]; n     ufs[i] = i; n   } n   sort(b + 1, b + n + 1); n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] != b[i]) { n       cnt++; n       join(a[i], b[i], i); n     } n   } n   if (cnt > s) { n     cout <<  -1  << endl; n     return 0; n   } n   if (cnt == 0) { n     cout <<  0  << endl; n     return 0; n   } n   if (s - cnt > 1) { n     for (int i = 1; i <= len; i++) { n       if (ufs[i] == i && meme[i] != 0) { n         curm.push_back(meme[i]); n         if (curm.size() == s - cnt) break; n       } n     } n     if (curm.size() > 1) { n       mema.push_back(curm); n       int lstv = a[curm.back()]; n       for (int i = curm.size() - 1; i >= 1; i--) { n         a[curm[i]] = a[curm[i - 1]]; n       } n       a[curm[0]] = lstv; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] != b[i]) { n       adj[a[i]].push_back(make_pair(b[i], i)); n     } n   } n   for (int i = 1; i <= len; i++) { n     if (adj[i].size() != 0) { n       curm.clear(); n       dfs(i); n       mema.push_back(curm); n     } n   } n   cout << mema.size() << endl; n   for (int i = 0; i < mema.size(); i++) { n     cout << mema[i].size() << endl; n     for (int j = 0; j < mema[i].size(); j++) { n       printf( %d  , mema[i][j]); n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n struct poke { n   double p, q; n } srt[N]; n inline bool cmp1(poke a, poke b) { n   return a.p > b.p || a.p == b.p && a.q > b.q; n } n inline bool cmp2(poke a, poke b) { return a.p - a.p * a.q > b.p - b.p * b.q; } n double f[N]; n int n, a, b; n double ans, sum; n multiset<double> heap; n void calc() { n   ans = 0., sort(srt + 1, srt + 1 + n, cmp1); n   for (int i = a; i <= n; ++i) { n     if (i - a > b) break; n     sort(srt + 1, srt + 1 + i, cmp2), heap.clear(); n     double cur = sum = f[i + 1] = 0.; n     for (int j = i; j >= 1; --j) { n       heap.insert(srt[j].q - srt[j].p), sum += srt[j].q - srt[j].p; n       if ((int)heap.size() > i - a) n         sum -= *heap.begin(), heap.erase(heap.begin()); n       f[j] = sum, cur += srt[j].p; n     } n     heap.clear(); n     for (int j = i + 1; j <= n; ++j) { n       heap.insert(srt[j].q), cur += srt[j].q; n       if ((int)heap.size() > b - (i - a)) n         cur -= *heap.begin(), heap.erase(heap.begin()); n     } n     for (int j = 0; j <= i; ++j) { n       if (j > a) break; n       if (j) n         heap.insert(srt[j].q - srt[j].p * srt[j].q), n             cur += srt[j].q - srt[j].p * srt[j].q; n       if ((int)heap.size() > b - (i - a)) n         cur -= *heap.begin(), heap.erase(heap.begin()); n       if ((int)heap.size() == b - (i - a)) ans = max(ans, cur + f[j + 1]); n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 1; i <= n; ++i) scanf( %lf , &srt[i].p); n   for (int i = 1; i <= n; ++i) scanf( %lf , &srt[i].q); n   calc(), printf( %.6lf n , ans); 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 n; n     cin >> n; n     char c; n     cin >> c; n     string str; n     cin >> str; n     long long ctr = 0; n     long long k = 0; n     for (long long i = 0; i < n; i++) { n       if (str[i] != c) { n         ctr++; n         k = i; n       } n     } n     if (ctr == 0) { n       cout << 0; n     } else if (ctr == 1) { n       if (k == n - 1) { n         cout << 1 <<   n ; n         cout << k; n       } else { n         cout << 1 <<   n ; n         cout << k + 2; n       } n     } else { n       long long flag = 0; n       for (long long i = n - 1; i >= n / 2; i--) { n         if (str[i] == c) { n           flag = i; n           break; n         } n       } n       if (flag) { n         cout << 1 <<   n ; n         cout << flag + 1; n       } else { n         cout << 2 <<   n ; n         cout << n <<     << n - 1; n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105], b[105]; n long long tinh(long long x, int n, int nc) { n   long long i, cnt = 0; n   for ((i) = (1); (i) <= (n); (i)++) { n     cnt += (((long long)a[i] * x) / b[i]) + 1; n     if (cnt > nc) return 1001001001; n   } n   return cnt; n } n void input() { n   int i, n, nc; n   int dau, giua, cuoi; n   int l, r; n   scanf( %d %d , &n, &nc); n   for ((i) = (1); (i) <= (n); (i)++) scanf( %d %d , &a[i], &b[i]); n   dau = 1, cuoi = 1001001001; n   while (dau <= cuoi) { n     giua = (dau + cuoi) >> 1; n     long long val = tinh(giua, n, nc); n     if (val > nc) n       cuoi = giua - 1; n     else n       dau = giua + 1; n   } n   r = cuoi; n   dau = 1, cuoi = 1001001001; n   while (dau <= cuoi) { n     giua = (dau + cuoi) >> 1; n     long long val = tinh(giua, n, nc); n     if (val < nc) n       dau = giua + 1; n     else n       cuoi = giua - 1; n   } n   l = dau; n   if (r < l) n     printf( 0 n ); n   else if (r == 1001001001) n     printf( -1 n ); n   else n     printf( %d n , r - l + 1); n } n int main() { n   input(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1000000000 ) n using namespace std; n const bool db = false; n int n, m, x, y; n int a[1010][1010], b[1010][1010]; n long long s[1010][1010], cs[1010][1010]; n vector<pair<int, int> > ans; n char mark[1010][1010]; n pair<long long, pair<int, int> > st[1010 * 1010]; n int sz = 0; n long long sum(int lx, int ly, int rx, int ry) { n   return s[rx][ry] + s[lx - 1][ly - 1] - s[rx][ly - 1] - s[lx - 1][ry]; n } n int main() { n   scanf( %d%d%d%d , &n, &m, &x, &y); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       scanf( %d , &a[i][j]); n       s[i][j] = a[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     deque<int> dq; n     for (int j = 1; j < y; ++j) { n       while (!dq.empty() && dq.back() > a[i][j]) dq.pop_back(); n       dq.push_back(a[i][j]); n     } n     for (int j = 1; j + y - 1 <= m; ++j) { n       if (j != 1) { n         if (a[i][j - 1] == dq.front()) dq.pop_front(); n       } n       while (!dq.empty() && dq.back() > a[i][j + y - 1]) dq.pop_back(); n       dq.push_back(a[i][j + y - 1]); n       b[i][j] = dq.front(); n     } n   } n   for (int i = 1; i + y - 1 <= m; ++i) { n     deque<int> dq; n     for (int j = 1; j < x; ++j) { n       while (!dq.empty() && dq.back() > b[j][i]) dq.pop_back(); n       dq.push_back(b[j][i]); n     } n     for (int j = 1; j + x - 1 <= n; ++j) { n       if (j != 1) { n         if (b[j - 1][i] == dq.front()) dq.pop_front(); n       } n       while (!dq.empty() && dq.back() > b[j + x - 1][i]) dq.pop_back(); n       dq.push_back(b[j + x - 1][i]); n       cs[j][i] = dq.front(); n     } n   } n   long long tot = 1LL << 60; n   for (int i = 1; i + x - 1 <= n; ++i) { n     for (int j = 1; j + y - 1 <= m; ++j) { n       st[sz].first = cs[i][j] = n           sum(i, j, i + x - 1, j + y - 1) - 1LL * x * y * cs[i][j]; n       st[sz].second.first = i; n       st[sz++].second.second = j; n     } n   } n   sort(st, st + sz); n   ans.reserve(n * m); n   for (int it = 0; it < sz; ++it) { n     int i = st[it].second.first, j = st[it].second.second; n     if (mark[i][j] || mark[i + x - 1][j] || mark[i][j + y - 1] || n         mark[i + x - 1][j + y - 1]) { n       continue; n     } n     ans.emplace_back(make_pair(i, j)); n     for (int di = 0; di < x; ++di) { n       for (int dj = 0; dj < y; ++dj) { n         mark[i + di][j + dj] = 1; n       } n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); ++i) n     printf( %d %d %lld n , ans[i].first, ans[i].second, n            cs[ans[i].first][ans[i].second]); n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _p(T a) { n   cout << a <<   n ; n } n template <class T> n void _pa(T a) { n   cout << a <<    ; n } n template <class T1, class T2> n void _p(T1 a, T2 b) { n   cout << a <<     << b <<   n ; n } n template <class T1, class T2, class T3> n void _p(T1 a, T2 b, T3 c) { n   cout << a <<     << b <<     << c <<   n ; n } n template <class T1, class T2, class T3, class T4> n void _p(T1 a, T2 b, T3 c, T4 d) { n   cout << a <<     << b <<     << c <<     << d <<     <<   n ; n } n template <class T1, class T2, class T3, class T4, class T5> n void _p(T1 a, T2 b, T3 c, T4 d, T5 e) { n   cout << a <<     << b <<     << c <<     << d <<     << e <<     <<   n ; n } n template <class T> n void _pa(T a[], long long n) { n   long long i; n   for (i = 0; i < n; i++) cout << a[i] <<    ; n   cout <<   n ; n } n template <class T> n void _p(vector<T> v) { n   for (auto x : v) cout << x <<    ; n   cout <<   n ; n } n template <class T> n void _p(list<T> l) { n   for (auto x : l) cout << x <<    ; n   cout <<   n ; n } n template <class T> n void _p(set<T> s) { n   for (auto x : s) cout << x <<    ; n   cout <<   n ; n } n template <class T> n void _p(multiset<T> s) { n   for (auto x : s) cout << x <<    ; n   cout <<   n ; n } n template <class T> n void _p(unordered_set<T> s) { n   for (auto x : s) cout << x <<    ; n   cout <<   n ; n } n template <class T1, class T2> n void _p(map<T1, T2> m) { n   for (auto x : m) cout << x.first <<  ->  << x.second <<   n ; n   cout <<   n ; n } n template <class T1, class T2> n void _p(unordered_map<T1, T2> m) { n   for (auto x : m) cout << x.first <<  ->  << x.second <<   n ; n   cout <<   n ; n } n template <class T1, class T2> n void _p(map<T1, vector<T2>> m) { n   for (auto x : m) { n     cout << x.first <<  -> ; n     _p(x.second); n   } n   cout <<   n ; n } n template <class T1, class T2> n void _p(unordered_map<T1, vector<T2>> m) { n   for (auto x : m) { n     cout << x.first <<  -> ; n     _p(x.second); n   } n   cout <<   n ; n } n void solution() { n   string a, b; n   int i, n, c = 0; n   cin >> a >> b; n   map<char, int> mp; n   n = b.length(); n   for (i = 0; i < 26; i++) mp[a[i]] = i; n   for (i = 1; i < n; i++) c += abs(mp[b[i]] - mp[b[i - 1]]); n   _p(c); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tcse; n   cin >> tcse; n   while (tcse--) solution(); n } n


#include <bits/stdc++.h> n using namespace std; n bool pd[5005][5005]; n char str[5005]; n int n; n bool vis[5050]; n bool dfs(int u, int lst) { n   vis[u] = 1; n   for (int i = 1; i <= n; ++i) { n     if (!pd[u][i]) continue; n     if (lst != -1 && pd[i][lst]) { n       printf( %d %d %d n , lst, u, i); n       return 1; n     } n     if (!vis[i] && dfs(i, u)) return 1; n   } n   return 0; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , str + 1); n     for (int j = 1; j <= n; ++j) n       if (str[j] ==  1 ) pd[i][j] = 1; n   } n   for (int i = 1; i <= n; ++i) n     if (!vis[i] && dfs(i, -1)) { n       return 0; n     } n   printf( -1 n ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   long long s = 2 * n * m; n   if (s % k != 0) { n     cout <<  NO ; n     return 0; n   } n   s /= k; n   long long x2, x3, y2, y3; n   x2 = n; n   if (s % n == 0) n     y3 = s / n; n   else n     y3 = s / n + 1; n   long long r = y3 * n - s; n   y2 = 1, x3 = r; n   cout <<  YES n ; n   cout <<  0 0 n ; n   cout << x2 <<     << y2 <<   n ; n   cout << x3 <<     << y3 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void prepare() {} n void solve() { n   string s; n   cin >> s; n   set<int> ss[26]; n   for (int i = 0; i < s.size(); ++i) ss[s[i] -  a ].insert(i); n   int i = 25; n   int cur = -1; n   while (i != -1) { n     if (ss[i].empty()) { n       --i; n       continue; n     } n     while (!ss[i].empty() && *ss[i].begin() < cur) ss[i].erase(ss[i].begin()); n     if (ss[i].empty()) { n       --i; n       continue; n     } n     for (int j = 0; j < ss[i].size(); ++j) printf( %c , i +  a ); n     cur = *(--ss[i].end()); n     --i; n   } n } n int main() { n   prepare(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 200005; n const int N = 900005; n struct node { n   int son[27]; n   int fa; n } a[N]; n int sz; n char s[N]; n int mat[M]; n vector<int> over[N]; n inline int new_id() { n   ++sz; n   over[sz].clear(); n   for (int i = (1); i <= (26); i++) a[sz].son[i] = -1; n   return sz; n } n int np, ans; n void ins(int pos, int x, int y) { n   over[pos].push_back(np); n   if (x == y) return; n   int cur = s[x] -  a  + 1; n   if (a[pos].son[cur] == -1) { n     a[pos].son[cur] = new_id(); n     a[a[pos].son[cur]].fa = pos; n   } n   ins(a[pos].son[cur], ++x, y); n   return; n } n int u[M], n; n void dfs(int x, int dep) { n   for (int i = (1); i <= (26); i++) n     if (a[x].son[i] != -1) dfs(a[x].son[i], dep + 1); n   vector<int> g1, g2; n   g1.clear(); n   g2.clear(); n   for (int i = 0; i < over[x].size(); i++) { n     int j = over[x][i]; n     if (u[j]) continue; n     if (j <= n) n       g1.push_back(j); n     else n       g2.push_back(j); n   } n   for (int i = 0; i < min(g1.size(), g2.size()); i++) { n     ans += dep; n     u[g1[i]] = u[g2[i]] = 1; n     mat[g1[i]] = g2[i] - n; n   } n } n int main() { n   scanf( %d , &n); n   sz = -1; n   new_id(); n   for (int i = (1); i <= (n * 2); i++) { n     np = i; n     scanf( %s , &s); n     int len = strlen(s); n     ins(0, 0, len); n   } n   dfs(0, 0); n   printf( %d n , ans); n   for (int i = (1); i <= (n); i++) printf( %d %d n , i, mat[i]); 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   cout.tie(NULL); n   double s, x, y, z; n   cin >> s >> x >> y >> z; n   if ((x == y) && (y == z) && (z == 0.0)) { n     cout <<  0 0   << s << endl; n     return 0; n   } n   double k = x + y + z, x0 = x * s / k, y0 = y * s / k, z0 = z * s / k; n   if (x0 + y0 + z0 >= s) z0 -= (x0 + y0 + z0 - s); n   cout << setprecision(14) << x0 <<     << y0 <<     << z0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double xx[10], yy[10]; n int main() { n   double px, py, vx, vy, a, b, c, d; n   cin >> px >> py >> vx >> vy >> a >> b >> c >> d; n   double x = vx / sqrt(vx * vx + vy * vy); n   double y = vy / sqrt(vx * vx + vy * vy); n   xx[1] = b * x + px; n   yy[1] = b * y + py; n   xx[2] = px - (y * a / 2); n   yy[2] = py + (x * a / 2); n   xx[3] = px - (y * c / 2); n   yy[3] = py + (x * c / 2); n   xx[4] = xx[3] - (d * x); n   yy[4] = yy[3] - (d * y); n   xx[6] = px + (y * c / 2); n   yy[6] = py - (x * c / 2); n   xx[5] = xx[6] - (x * d); n   yy[5] = yy[6] - (d * y); n   xx[7] = px + (y * a / 2); n   yy[7] = py - (x * a / 2); n   cout.precision(20); n   for (int i = 1; i <= 7; i++) cout << xx[i] <<     << yy[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int b, l, k = 0, p, m; n   cin >> b >> l; n   int n[l]; n   for (int i = 0; i < l; i++) { n     n[i] = 0; n   } n   for (int i = 0; i < b; i++) { n     cin >> p; n     for (int j = 0; j < p; j++) { n       cin >> m; n       n[m - 1]++; n     } n   } n   for (int i = 0; i < l; i++) { n     if (n[i] == 0) { n       k++; n     } n   } n   if (k > 0) { n     cout <<  NO ; n   } else { n     cout <<  YES ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int pwr(long long int base, long long int p, long long int mod) { n   long long int ans = 1; n   while (p) { n     if (p & 1) ans = (ans * base) % mod; n     base = (base * base) % mod; n     p /= 2; n   } n   return ans; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   long long int k, a, b; n   scanf( %lld , &k); n   scanf( %lld , &a); n   scanf( %lld , &b); n   long long int ans = (a / k) + (b / k); n   if ((a < k && b % k != 0) || (b < k && a % k != 0)) { n     printf( -1 n ); n     return 0; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 22; n int d, p, q; n int c[N][N], s[5555]; n int a[N][N], b[N][N], f[N]; n int fpow(int x, int y = p - 2) { n   if (!y) return 1; n   long long z = fpow(x, y >> 1); n   z = z * z % p; n   if (y & 1) z = z * x % p; n   return z; n } n int w; n void pr(int x, int y, int z = 0) { n   if (z) n     printf( + %d %d %d n , x, y, z); n   else n     printf( ^ %d %d n , x, y); n } n void mul(int r, int x) { n   int t = 100, i; n   if (x % 2 == 0) x += p; n   pr(r, r, t); n   x--; n   for (i = 2; x; i = i + i) { n     if (x & i) pr(r, t, r), x -= i; n     pr(t, t, t); n   } n } n void go(int r) { n   int t = 10, i; n   mul(9, 0); n   for (i = t; i <= t + d; i = i + 1) { n     pr(8, 9, i); n     mul(i, i - t); n     pr(i, r, i); n     pr(i, i); n     mul(i, b[q][i - t]); n   } n   pr(9, 9, r); n   for (i = t; i <= t + d; i = i + 1) pr(r, i, r); n } n int main() { n   int i, j, k, e, x; n   cin >> d >> p; n   for (i = 0; i < N; i = i + 1) { n     c[i][0] = 1; n     for (j = 1; j <= i; j = j + 1) n       c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % p; n   } n   for (i = 0; i <= d; i = i + 1) { n     for (j = 0; j <= d; j = j + 1) n       a[i][j] = (long long)fpow(i, d - j) * c[d][j] % p; n     b[i][i] = 1; n   } n   for (i = 0; i <= d; i = i + 1) { n     for (j = 0; j <= d; j = j + 1) n       if (!f[j] && a[j][i]) break; n     e = j; n     f[e] = 1; n     if (i == 2) q = j; n     x = fpow(a[j][i]); n     for (k = 0; k <= d; k = k + 1) { n       a[e][k] = (long long)a[e][k] * x % p; n       b[e][k] = (long long)b[e][k] * x % p; n     } n     for (j = 0; j <= d; j = j + 1) { n       if (j == e || !a[j][i]) continue; n       x = a[j][i]; n       for (k = i; k <= d; k = k + 1) n         a[j][k] = (a[j][k] - (long long)a[e][k] * x % p + p) % p; n       for (k = 0; k <= d; k = k + 1) n         b[j][k] = (b[j][k] - (long long)b[e][k] * x % p + p) % p; n     } n   } n   pr(1, 2, 3); n   go(1); n   go(2); n   go(3); n   mul(1, p - 1); n   mul(2, p - 1); n   pr(1, 2, 1); n   pr(1, 3, 1); n   mul(1, fpow(2)); n   printf( f 1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1ll << 18; n long long a, b, top, now; n queue<long long> q; n map<long long, int> mp; n long long rev(long long x) { n   long long y = 0; n   while (x != 0) { n     y = (y << 1) | (x & 1); n     x >>= 1; n   } n   return y; n } n void push(long long &x) { n   if (x > b && rev(x) > b) return; n   if (mp.find(x) == mp.end()) { n     q.push(x); n     mp[x] = 1; n   } n } n string solve() { n   if (a == b) return  YES ; n   q.push(a); n   mp[a] = 1; n   while (!q.empty()) { n     top = q.front(); n     q.pop(); n     now = rev(top << 1); n     if (now == b) return  YES ; n     push(now); n     now = rev((top << 1) | 1); n     if (now == b) return  YES ; n     push(now); n   } n   return  NO ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> a >> b; n   cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n ostream& operator<<(ostream& os, const pair<T1, T2>& v) { n   os <<  (  << v.first <<     << v.second <<  ) ; n   return os; n } n template <typename T, typename U = typename T::value_type, n           typename = typename enable_if<!is_same<T, string>::value>::type> n ostream& operator<<(ostream& os, const T& v) { n   os <<  [ ; n   for (const auto& it : v) os << it <<    ; n   if (!v.empty()) os <<   b ; n   os <<  ] ; n   return os; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   int n, x; n   cin >> n; n   int cnt[6] = {0, 0, 0, 0, 0, 0}; n   for (int i = 0; i < (n); i++) { n     cin >> x; n     if (x == 4) { n       cnt[0]++; n       continue; n     } n     if (x == 8 && cnt[0]) { n       cnt[0]--; n       cnt[1]++; n       continue; n     } n     if (x == 15 && cnt[1]) { n       cnt[1]--; n       cnt[2]++; n       continue; n     } n     if (x == 16 && cnt[2]) { n       cnt[2]--; n       cnt[3]++; n       continue; n     } n     if (x == 23 && cnt[3]) { n       cnt[3]--; n       cnt[4]++; n       continue; n     } n     if (x == 42 && cnt[4]) { n       cnt[4]--; n       cnt[5]++; n       continue; n     } n   } n   cout << n - 6 * cnt[5] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct par { n   int x, y, label; n   par(int _x, int _y, int _label) { n     x = _x; n     y = _y; n     label = _label; n   } n   par() {} n }; n bool operator<(par p1, par p2) { n   if (p1.x != p2.x) return p1.x < p2.x; n   return p1.y < p2.y; n } n int main() { n   int n; n   while (cin >> n) { n     vector<par> V; n     int x, y; n     for (int i = 0; i < n; i++) { n       cin >> x >> y; n       V.push_back(par(x, y, i + 1)); n     } n     sort((V).begin(), (V).end()); n     vector<int> sol; n     for (int i = 0; i < n; i++) { n       bool ok = 1; n       vector<par> aux; n       for (int j = 0; j < n; j++) n         if (j != i) aux.push_back(V[j]); n       for (int j = 1; j < aux.size(); j++) { n         if (aux[j - 1].y > aux[j].x) { n           ok = 0; n           break; n         } n       } n       if (ok) sol.push_back(V[i].label); n     } n     sort((sol).begin(), (sol).end()); n     cout << sol.size() << endl; n     for (int i = 0; i < sol.size(); i++) { n       if (i == 0) n         printf( %d , sol[i]); n       else n         printf(  %d , sol[i]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n inline int read() { n   int w = 1, s = 0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     s = s * 10 + ch -  0 ; n     ch = getchar(); n   } n   return w * s; n } n int n, k, fac[1001000], Ifac[1001000], f[1000010], Sum[1010010]; n inline long long ksm(long long x, long long y) { n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return res; n } n inline int add(int a, int b) { n   a += b; n   return a >= mod ? a - mod : a; n } n inline int A(int a, int b) { return 1ll * fac[a] * Ifac[a - b] % mod; } n inline void Init() { n   fac[0] = Ifac[0] = 1; n   for (register int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; n   Ifac[n] = ksm(fac[n], mod - 2); n   for (register int i = n - 1; i; --i) n     Ifac[i] = 1ll * Ifac[i + 1] * (i + 1) % mod; n } n int main() { n   n = read(), k = read(); n   Init(); n   for (register int i = k + 1; i <= n; ++i) { n     int tmp = 1ll * max(i - k - 1, 0) * fac[i - 2] % mod; n     f[i] = add(f[i], tmp); n     int l = max(i - k, 1); n     f[i] = n         add(f[i], 1ll * fac[i - 2] * add(Sum[i - 1], mod - Sum[l - 1]) % mod); n     Sum[i] = add(Sum[i - 1], 1ll * f[i] * Ifac[i - 1] % mod); n   } n   cout << 1ll * Sum[n] * fac[n - 1] % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 4e5 + 10; n int n, m, k, t; n bool vis[MAXN], ans[MAXN]; n int v[MAXN]; n set<int> p[MAXN]; n priority_queue<pair<int, int> > pq; n int main() { n   cin >> n >> m; n   memset(vis, false, sizeof(vis)); n   memset(ans, false, sizeof(ans)); n   for (int i = 0; i <= 2 * m + 2; i++) v[i] = -1; n   for (int i = 1; i <= n; i++) { n     cin >> k; n     for (int j = 0; j < k; j++) { n       cin >> t; n       v[t + m] = i; n       p[i].insert(t + m); n     } n     pq.push({-p[i].size(), i}); n   } n   bool flag = true; n   while (!pq.empty()) { n     int top = pq.top().second; n     pq.pop(); n     if (vis[top]) continue; n     if (p[top].empty()) { n       flag = false; n       break; n     } n     int x = *p[top].begin(); n     vis[top] = true; n     ans[x] = true; n     int y = m + m - x; n     if (v[y] != -1 && !vis[v[y]]) { n       p[v[y]].erase(y); n       pq.push({-p[v[y]].size(), v[y]}); n     } n   } n   if (flag) { n     cout <<  YES  << endl; n     for (int i = m + 1; i < m + 1 + m; i++) { n       if (!ans[i]) n         cout << 0; n       else n         cout << 1; n     } n     cout << endl; n   } else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int gi() { n   int w = 0, q = 0; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && (c !=  - )) c = getchar(); n   if (c ==  - ) q = 1, c = getchar(); n   while (c >=  0  && c <=  9 ) w = w * 10 + c -  0 , c = getchar(); n   return q ? -w : w; n } n int n, k; n long long a[500000], d[500000]; n int main() { n   n = gi(), k = gi(); n   long long ans = 0, sum = 0; n   for (int i = 1; i <= n; ++i) a[i] = gi(); n   for (int i = 1; i <= n; ++i) d[i] = gi(); n   for (int i = n; i; --i) { n     sum += 1ll * a[i]; n     if ((i == 1) && (k == 1)) continue; n     ans = max(ans, sum - d[i]); n   } n   if (!k) printf( %I64d n , ans), exit(0); n   sum -= d[1]; n   for (int i = 2; i < n; ++i) n     ans = max(ans, sum - a[i] + max(0ll, a[i] - d[i])); n   long long mn = 5000000, mn1 = 5000000; n   sum = 0; n   for (int i = 1; i < n; ++i) { n     sum += 1ll * a[i]; n     if (d[i] < mn) n       mn1 = mn, mn = d[i]; n     else if (d[i] == mn) n       mn1 = d[i]; n     else n       mn1 = min(mn1, d[i]); n   } n   ans = max(ans, sum + 1ll * (max(0ll, a[n] - d[n]) - mn)); n   if (k == 1) n     ans = max(ans, sum + 1ll * (a[n] - mn - mn1)); n   else n     ans = max(ans, sum + 1ll * (a[n] - mn)); n   printf( %I64d 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   vector<int> b[5001]; n   for (int i = 0; i < n; i++) { n     string s; n     int a, b1, c; n     cin >> s >> a >> b1; n     if (s[0] ==  M ) n       c = 0; n     else n       c = 1; n     b[i].push_back(c); n     b[i].push_back(a); n     b[i].push_back(b1); n   } n   int ans = 0; n   for (int i = 1; i <= 366; i++) { n     int m[2] = {}; n     for (int j = 0; j < n; j++) { n       if (b[j][1] <= i && b[j][2] >= i) m[b[j][0]]++; n     } n     ans = max(ans, 2 * min(m[0], m[1])); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m, ar[1000005], sum, t, ar2[1000005]; n vector<int> ans; n vector<pair<int, char> > res; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (long long int i = 1; i <= n; i++) cin >> ar[i]; n   cin >> m; n   for (long long int i = 1; i <= m; i++) cin >> ar2[i]; n   long long int top = 0; n   long long int l = 1; n   for (long long int i = 1; i <= n + 1; i++) { n     if (l > m) return cout <<  NO , 0; n     if (top < ar2[l]) { n       top += ar[i]; n       continue; n     } n     if (top > ar2[l]) return cout <<  NO , 0; n     ans.push_back(i - 1); n     l++; n     top = ar[i]; n   } n   if (top || l <= m) return cout <<  NO , 0; n   cout <<   n ; n   int last = 1; n   long long int cnt = 0; n   for (int h = 0; h < ans.size(); h++) { n     long long int y = ans[h]; n     long long int mk = 0, mn = INT_MAX; n     for (int i = last; i <= y; i++) { n       mk = max(mk, ar[i]); n       mn = min(mn, ar[i]); n     } n     if (last == y) { n       last++; n       cnt = y - h - 1; n       continue; n     } n     if (mn == mk) { n       return cout <<  NO , 0; n     } n     long long int mki = 0; n     char mki2; n     long long int z = y - last + 1; n     for (long long int i = last; i <= y; i++) { n       if (ar[i] != mk) continue; n       if (i != last && ar[i - 1] != ar[i]) { n         mki = i; n         mki2 =  L ; n         res.push_back({mki - cnt, mki2}); n         mki--; n         break; n       } n       if (i != y && ar[i] != ar[i + 1]) { n         mki = i; n         mki2 =  R ; n         res.push_back({mki - cnt, mki2}); n         break; n       } n     } n     z--; n     z--; n     while (mki > last) { n       res.push_back({mki - cnt,  L }); n       mki--; n       z--; n     } n     while (z--) { n       res.push_back({mki - cnt,  R }); n     } n     cnt = y - h - 1; n     last = y + 1; n   } n   cout <<  YES  n        <<   n ; n   for (int i = 0; i < res.size(); i++) n     cout << res[i].first <<     << res[i].second <<   n ; n } n


#include <bits/stdc++.h> n const int N = 100010; n const long long INF = 1ll << 60; n const int inf = 0x3f3f3f3f; n int t; n int n, m, k; n int a[N]; n int b[N]; n std::vector<int> row[N]; n std::vector<int> col[N]; n int cur; n int r, c; n long long ans; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; ++i) { n     scanf( %d%d , &a[i], &b[i]); n     row[a[i]].push_back(b[i]); n     col[b[i]].push_back(a[i]); n   } n   for (int i = 1; i <= n; ++i) { n     row[i].push_back(m + 1); n     row[i].push_back(0); n     std::sort(row[i].begin(), row[i].end()); n   } n   for (int j = 1; j <= m; ++j) { n     col[j].push_back(n + 1); n     col[j].push_back(0); n     std::sort(col[j].begin(), col[j].end()); n   } n   r = 1; n   c = 1; n   ans = 1ll; n   int r_max = inf; n   int c_max = inf; n   int r_min = 2; n   int c_min = -inf; n   for (int i = 1; i <= 2 * (n + m) + 1; ++i) { n     if (!cur) { n       auto x = std::lower_bound(row[r].begin(), row[r].end(), c); n       int c_nxt = (*x) - 1; n       c_nxt = std::min(c_nxt, c_max); n       assert(c_nxt >= c); n       ans += c_nxt - c; n       if (c == c_nxt && i != 1) { n         break; n       } n       c = c_nxt; n       c_max = c - 1; n     } else if (cur == 1) { n       auto x = std::lower_bound(col[c].begin(), col[c].end(), r); n       int r_nxt = (*x) - 1; n       r_nxt = std::min(r_nxt, r_max); n       assert(r_nxt >= r); n       ans += r_nxt - r; n       if (r == r_nxt && i != 1) { n         break; n       } n       r = r_nxt; n       r_max = r - 1; n     } else if (cur == 2) { n       auto x = std::lower_bound(row[r].begin(), row[r].end(), c); n       --x; n       int c_nxt = (*x) + 1; n       c_nxt = std::max(c_nxt, c_min); n       assert(c_nxt <= c); n       ans += c - c_nxt; n       if (c == c_nxt && i != 1) { n         break; n       } n       c = c_nxt; n       c_min = c + 1; n     } else if (cur == 3) { n       auto x = std::lower_bound(col[c].begin(), col[c].end(), r); n       --x; n       int r_nxt = (*x) + 1; n       r_nxt = std::max(r_nxt, r_min); n       assert(r_nxt <= r); n       ans += r - r_nxt; n       if (r == r_nxt && i != 1) { n         break; n       } n       r = r_nxt; n       r_min = r + 1; n     } n     cur = (cur + 1) % 4; n   } n   if (ans + k != 1ll * n * m) { n     printf( NO n ); n   } else { n     printf( YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   cout << n / 2 + 1 <<   n ; n } n int main() { n   int tc, cse = 0; n   cin >> tc; n   while (tc--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 7; n int ans[maxn]; n int lowbit(int x) { return x & -x; } n bool is_two(int x) { return x == lowbit(x); } n bool dfs(int n, int k, int id) { n   if (k == 1) { n     printf( %d %d %d n , n, k, id); n     if (is_two(n)) { n       return false; n     } n     ans[id] = max(id - 2, 0); n     for (int i = 2; i <= n; ++i) { n       ans[id + i - 1] = id + i / 2 - 1; n     } n     return true; n   } else if (k == 0) { n     if (is_two(n)) { n       ans[1] = 0; n       for (int i = 2; i <= n; ++i) { n         ans[i] = i / 2; n       } n       return true; n     } else n       return false; n   } else { n     while (k >= 2) { n       ans[id] = max(id - 2, 0); n       ans[id + 1] = id; n       n -= 2; n       k -= 1; n       id += 2; n     } n     return dfs(n, k, id); n   } n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int lim = max(0, (n - 3) / 2); n   if (n % 2 == 0 || k > lim || (n == 9 && k == 2) || n       (!is_two(n + 1) && k == 0) || (is_two(n + 1) && k == 1)) { n     printf( NO n ); n   } else { n     int id = 1; n     while (k >= 2) { n       ans[id] = max(id - 2, 0); n       ans[id + 1] = id; n       n -= 2; n       k -= 1; n       id += 2; n     } n     ans[id] = max(id - 2, 0); n     for (int i = 2; i <= n - 2; ++i) { n       ans[id + i - 1] = id + i / 2 - 1; n     } n     if (is_two(n + 1)) { n       if (k == 0) n         ans[id + n - 2] = id + (n - 1) / 2 - 1, n                      ans[id + n - 1] = id + n / 2 - 1; n       else n         ans[id + n - 2] = 2, ans[id + n - 1] = 2; n     } else { n       if (k == 0) n         return printf( NO n ), 0; n       else n         ans[id + n - 2] = id + (n - 1) / 2 - 1, n                      ans[id + n - 1] = id + n / 2 - 1; n     } n     printf( YES n ); n     for (int i = 1; i < id + n - 1; ++i) { n       printf( %d  , ans[i]); n     } n     printf( %d n , ans[id + n - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 1, inf = 1e18; n vector<pair<long long, long long> > a, b; n vector<pair<pair<long long, long long>, long long> > z; n long long add[4 * N], t[4 * N]; n void push(long long v, long long l, long long r) { n   t[v] += add[v]; n   if (l != r - 1) { n     add[2 * v] += add[v]; n     add[2 * v + 1] += add[v]; n   } n   add[v] = 0; n } n void update(long long v, long long l, long long r, long long vl, long long vr, n             long long val) { n   push(v, l, r); n   if (l >= vr || vl >= r) return; n   if (l >= vl && r <= vr) { n     add[v] += val; n     push(v, l, r); n     return; n   } n   long long m = (l + r) / 2; n   update(2 * v, l, m, vl, vr, val); n   update(2 * v + 1, m, r, vl, vr, val); n   t[v] = max(t[2 * v + 1], t[2 * v]); n } n long long get(long long v, long long l, long long r, long long vl, n               long long vr) { n   push(v, l, r); n   if (l >= vr || vl >= r) return -inf; n   if (l >= vl && r <= vr) { n     return t[v]; n   } n   long long m = (l + r) / 2; n   return max(get(2 * v, l, m, vl, vr), get(2 * v + 1, m, r, vl, vr)); n } n void fuckyou(long long v, long long l, long long r, long long pos, n              long long val) { n   if (l > pos || r <= pos) return; n   if (l == r - 1) { n     t[v] = max(t[v], val); n     return; n   } n   long long m = (l + r) / 2; n   fuckyou(2 * v, l, m, pos, val); n   fuckyou(2 * v + 1, m, r, pos, val); n   t[v] = max(t[2 * v], t[2 * v + 1]); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   for (long long i = 0; i < 4 * N; i++) t[i] = -inf; n   long long n, m, p, ans = -inf; n   cin >> n >> m >> p; n   a.resize(n); n   b.resize(m); n   z.resize(p); n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n   } n   for (long long i = 0; i < m; i++) { n     cin >> b[i].first >> b[i].second; n     fuckyou(1, 0, N, b[i].first, -b[i].second); n   } n   for (long long i = 0; i < p; i++) { n     cin >> z[i].first.first >> z[i].first.second >> z[i].second; n   } n   sort((z).begin(), (z).end()); n   sort((a).begin(), (a).end()); n   cout << endl; n   long long j = 0; n   for (long long i = 0; i < n; i++) { n     while (j < z.size() && a[i].first > z[j].first.first) { n       update(1, 0, N, z[j].first.second + 1, N, z[j].second); n       j++; n     } n     ans = max(ans, -a[i].second + get(1, 0, N, 0, N)); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n string rev(string s) { n   string p; n   for (auto i : s) p.insert(0, 1, i); n   return p; n } n int main() { n   cin >> s; n   int n = s.size(); n   for (int i = 0; i <= s.size(); i++) n     for (int j = 0; j < 26; j++) { n       string p = s; n       p.insert(i, 1, j +  a ); n       if (p == rev(p)) return cout << p, 0; n     } n   cout <<  NA ; n } n


#include <bits/stdc++.h> n struct PP { n   int l, r, sum, lv, rv, cover; n   int mid() { return (l + r) >> 1; } n   void doit() { sum = lv = rv = cover ? 0 : dis(); } n   int dis() { return r - l; } n } q[100005 * 3]; n int max(int a, int b) { return a > b ? a : b; } n void Build(int l, int r, int x) { n   q[x].l = l; n   q[x].r = r; n   q[x].sum = q[x].lv = q[x].rv = q[x].dis(); n   q[x].cover = -1; n   if (l == r - 1) return; n   int mid = q[x].mid(); n   Build(l, mid, (x << 1)); n   Build(mid, r, ((x << 1) | 1)); n } n void pushdown(int x) { n   q[(x << 1)].cover = q[((x << 1) | 1)].cover = q[x].cover; n   q[(x << 1)].doit(); n   q[((x << 1) | 1)].doit(); n   q[x].cover = -1; n } n void Pushup(int x) { n   q[x].sum = max(q[(x << 1)].rv + q[((x << 1) | 1)].lv, n                  max(q[(x << 1)].sum, q[((x << 1) | 1)].sum)); n   q[x].lv = q[(x << 1)].lv; n   q[x].rv = q[((x << 1) | 1)].rv; n   if (q[(x << 1)].sum == q[(x << 1)].dis()) q[x].lv += q[((x << 1) | 1)].lv; n   if (q[((x << 1) | 1)].sum == q[((x << 1) | 1)].dis()) n     q[x].rv += q[(x << 1)].rv; n } n void update(int l, int r, int c, int x) { n   if (l <= q[x].l && q[x].r <= r) { n     q[x].cover = c; n     q[x].doit(); n     return; n   } n   if (q[x].cover != -1) pushdown(x); n   int mid = q[x].mid(); n   if (l < mid) update(l, r, c, (x << 1)); n   if (mid < r) update(l, r, c, ((x << 1) | 1)); n   Pushup(x); n } n int query(int w, int x) { n   if (q[x].sum >= w && q[x].cover == 0) return q[x].l; n   if (q[x].cover != -1) pushdown(x); n   if (q[(x << 1)].sum >= w) n     return query(w, (x << 1)); n   else if (q[(x << 1)].rv + q[((x << 1) | 1)].lv >= w) n     return q[(x << 1)].r - q[(x << 1)].rv; n   else if (q[((x << 1) | 1)].sum >= w) n     return query(w, ((x << 1) | 1)); n   else n     return -1; n } n struct SS { n   int l, r; n } cnt[100005]; n int main() { n   int i, m, op, a; n   int L, B, F; n   while (scanf( %d%d%d , &L, &B, &F) != EOF) { n     scanf( %d , &m); n     Build(0, B + L + F, 1); n     for (i = 1; i <= m; i++) { n       scanf( %d%d , &op, &a); n       if (op == 1) { n         int pos = query(B + a + F, 1); n         if (pos == -1) { n           printf( -1 n ); n           continue; n         } n         printf( %d n , pos); n         update(pos + B, pos + B + a, 1, 1); n         cnt[i].l = pos + B; n         cnt[i].r = pos + B + a; n       } else n         update(cnt[a].l, cnt[a].r, 0, 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 200000; n int n, m, t, tin[mxN]; n vector<int> adj[mxN], e[mxN]; n vector<array<int, 3>> ans; n void dfs(int u, int p = -1) { n   tin[u] = ++t; n   if (p ^ -1) e[u].push_back(p); n   for (int v : adj[u]) { n     if (v == p) continue; n     if (!tin[v]) { n       dfs(v, u); n     } else if (tin[v] < tin[u]) { n       e[u].push_back(v); n     } n   } n   while (e[u].size() >= 2) { n     int v = e[u].back(); n     e[u].pop_back(); n     int w = e[u].back(); n     e[u].pop_back(); n     ans.push_back({v, u, w}); n   } n   if (e[u].size() == 1 && p ^ -1) e[p].push_back(u); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v, --u, --v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 0; i < n; ++i) n     if (!tin[i]) dfs(i); n   cout << ans.size() <<   n ; n   for (array<int, 3> x : ans) n     cout << x[0] + 1 <<     << x[1] + 1 <<     << x[2] + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int N = int(2e5) + 10; n const int M = int(1e5) + 5; n const int INF = int(1e9); n int U[N], V[N], W[N], vis[N], D[N], comp[N], val[N], ans[N], parent; n vector<int> g[N], rg[N], G[N], nodes; n int n, m; n int adj(int u, int e) { return U[e] ^ V[e] ^ u; } n void get(int u) { n   nodes.push_back(u); n   nodes.push_back(u + M); n   vis[u] = 1; n   for (auto e : G[u]) { n     int w = adj(u, e); n     if (vis[w]) continue; n     get(w); n   } n } n int NOT(int u) { n   if (u < M) n     return u + M; n   else n     return u - M; n } n void addEdge(int u, int v) { n   u = NOT(u); n   g[u].push_back(v); n   rg[v].push_back(u); n   u = NOT(u); n   v = NOT(v); n   swap(u, v); n   g[u].push_back(v); n   rg[v].push_back(u); n } n void clear(int u) { n   g[u].clear(), rg[u].clear(), vis[u] = 0, val[u] = 0, comp[u] = 0; n   for (auto e : G[u]) D[e] = 0; n } n vector<int> order, component; n void dfs1(int u) { n   vis[u] = 1; n   for (auto w : g[u]) n     if (!vis[w]) dfs1(w); n   order.push_back(u); n } n void dfs2(int u) { n   vis[u] = 1; n   comp[u] = parent; n   for (auto w : rg[u]) n     if (!vis[w]) dfs2(w); n } n int go(int t) { n   for (auto u : nodes) clear(u); n   for (auto u : nodes) n     for (auto e : G[u]) n       if (!D[e]) { n         D[e] = 1; n         if (W[e] == t) n           addEdge(U[e], V[e]), addEdge(NOT(U[e]), NOT(V[e])); n         else n           addEdge(U[e], NOT(V[e])), addEdge(NOT(U[e]), V[e]); n       } n   order.clear(); n   for (auto i : nodes) n     if (!vis[i]) dfs1(i); n   for (auto u : nodes) vis[u] = 0; n   reverse(order.begin(), order.end()); n   vector<int> Cmp; n   for (auto i : order) { n     if (!vis[i]) parent = i, Cmp.push_back(i), dfs2(i); n   } n   for (auto u : nodes) { n     if (comp[u] == comp[NOT(u)]) return INF; n   } n   reverse(Cmp.begin(), Cmp.end()); n   int a = 0, b = 0; n   for (auto u : Cmp) { n     if (!val[u]) { n       val[u] = 1; n       val[comp[NOT(u)]] = -1; n     } n   } n   for (auto u : nodes) n     if (u < M) (val[comp[u]] == 1) ? a++ : b++; n   int V = (a > b ? 0 : 1); n   for (auto u : nodes) n     if (u < M && val[comp[u]] == 1) n       ans[u] = V; n     else if (u < M) n       ans[u] = 1 - V; n   return min(a, b); n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     char x; n     scanf( %d %d %c , U + i, V + i, &x); n     G[U[i]].push_back(i); n     G[V[i]].push_back(i); n     W[i] = (x ==  R  ? 1 : 0); n   } n   long long int Ans1 = 0, Ans2 = 0; n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       nodes.clear(); n       get(i); n       Ans1 += go(0); n       Ans2 += go(1); n     } n   long long int Ans = min(Ans1, Ans2); n   if (Ans >= INF) n     puts( -1 ); n   else { n     printf( %lld n , Ans); n     int t = (Ans == Ans1 ? 0 : 1); n     memset(vis, 0, sizeof(vis)); n     for (int i = 1; i <= n; i++) n       if (!vis[i]) { n         nodes.clear(); n         get(i); n         go(t); n       } n     for (int i = 1; i <= n; i++) n       if (ans[i]) printf( %d  , i); n     puts(  ); 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   cout.tie(NULL); n   long long n; n   cin >> n; n   if (n == 47 || n == 74 || n == 477 || n == 477) n     cout <<  YES ; n   else if (n % 4 == 0 || n % 7 == 0 || n % 47 == 0) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int n, arr[105]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int ans = arr[0]; n   for (int i = 1; i < n; i++) { n     ans = gcd(ans, arr[i]); n   } n   cout << ans * n <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, r; n   cin >> n >> m >> r; n   vector<int> v(n); n   vector<int> v1(m); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> v1[i]; n   } n   sort(v.begin(), v.end()); n   sort(v1.rbegin(), v1.rend()); n   if (v[0] > v1[0]) { n     cout << r << endl; n     return 0; n   } n   int cnt = 0; n   cnt += r % v[0]; n   cnt += (r / v[0]) * v1[0]; n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n int ured() { n   int re = 0, ch; n   do { n     ch = getchar(); n   } while ( 9  < ch || ch <  0 ); n   do { n     re = re * 10 + (ch ^  0 ); n   } while ( 0  <= (ch = getchar()) && ch <=  9 ); n   return re; n } n void uwit(int da) { n   int cn = 0, ch[21]; n   do { n     ch[++cn] = da - da / 10 * 10; n   } while (da /= 10); n   do { n     putchar( 0  ^ ch[cn]); n   } while (--cn); n } n inline void dmax(int& fr, int to) { n   if (fr < to) { n     fr = to; n   } n } n inline void dmin(int& fr, int to) { n   if (fr > to) { n     fr = to; n   } n } n inline int mabs(int da) { return da >= 0 ? da : -da; } n const int _maxn = 200011, _mods = 1000000007; n int t, n, m, pdif; n char s[_maxn]; n int main() { n   t = ured(); n   while (t--) { n     n = ured(); n     m = ured(); n     if (m >= 2 * n) { n       uwit((m + 1) / 2 - 1); n     } else { n       uwit(m - n); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, res = 1; n long long a[100005]; n long long b[100005]; n long long d = 1, ans; n int main() { n   cin >> n >> k; n   for (int i = 0; i < k; i++) d *= 10; n   for (int i = 0; i < n / k; i++) cin >> a[i]; n   for (int i = 0; i < n / k; i++) cin >> b[i]; n   for (int i = 0; i < n / k; i++) { n     ans = (d - 1) / a[i] + 1; n     if (b[i] == 0) n       ans -= ((d / 10) - 1) / a[i] + 1; n     else n       ans -= n           ((b[i] + 1) * (d / 10) - 1) / a[i] - ((b[i]) * (d / 10) - 1) / a[i]; n     res = (res * ans) % (1000000007); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long X = 0, w = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) X = X * 10 + c -  0 , c = getchar(); n   return X * w; n } n const long long _ = 1e18; n signed main() { n   long long a = read(), q = (_ % a * 9 % a * 9 % a + 1) % a; n   printf( %lld %lld n , a - q + 1, a - q + _); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double PI = acosl(-1); n bool compare_int(int a, int b) { return (a > b); } n bool compare_string(string a, string b) { return a.size() < b.size(); } n bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second == b.second) n     return a.first < b.first; n   else n     return (a.second > b.second); n } n bool cmp(pair<string, int> x, pair<string, int> y) { n   return (x.second < y.second); n } n long long int bigmod(long long int a, long long int b, long long int m) { n   if (b == 0) return 1 % m; n   long long int x = bigmod(a, b / 2, m); n   x = (x * x) % m; n   if (b % 2 == 1) x = (x * a) % m; n   return x; n } n void NA() { n   printf( NO n ); n   exit(0); n } n void YA() { n   printf( YES n ); n   exit(0); n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long int mx = 0LL, mn = 0LL; n   sort(a.begin(), a.end()); n   for (int i = 0; i < n; i++) { n     mx = (mx + bigmod(2, i, 1000000007) * a[i]) % 1000000007; n     mn = (mn + bigmod(2, n - i - 1, 1000000007) * a[i]) % 1000000007; n   } n   cout << (mx - mn + 1000000007) % 1000000007 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void abc() { cout << endl; } n template <typename T, typename... U> n void abc(T a, U... b) { n   cout << a <<    , abc(b...); n } n template <typename T> n void printv(T l, T r) { n   while (l != r) cout << *l <<    n [++l == r]; n } n template <typename A, typename B> n istream& operator>>(istream& o, pair<A, B>& a) { n   return o >> a.first >> a.second; n } n template <typename A, typename B> n ostream& operator<<(ostream& o, pair<A, B> a) { n   return o <<  (  << a.first <<  ,   << a.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& o, vector<T> a) { n   bool is = false; n   for (T i : a) { n     o << (is ?     :  { ), is = true, o << i; n   } n   return o <<  } ; n } n template <typename T> n struct vv : vector<vector<T>> { n   vv(int n, int m, T v) : vector<vector<T>>(n, vector<T>(m, v)) {} n   vv() {} n }; n template <typename T> n struct vvv : vector<vv<T>> { n   vvv(int n, int m, int k, T v) : vector<vv<T>>(n, vv<T>(m, k, v)) {} n   vvv() {} n }; n const int mod = 1e9 + 7, N = 200001, logN = 20, K = 80000; n long long int modpow(long long int a, int b) { n   long long int ans = 1; n   for (; b; b >>= 1, a = a * a % mod) { n     if (b & 1) ans = ans * a % mod; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   vv<long long int> inv(n, n, 0); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) { n       inv[i][j] = modpow((a[i] + a[j]) % mod, mod - 2); n     } n   vv<long long int> pre(1 << n, n, 1); n   vv<long long int> pw(n, n + 1, 1); n   for (int i = 0; i < n; ++i) { n     for (int j = 1; j <= n; ++j) { n       pw[i][j] = pw[i][j - 1] * a[i] % mod; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int s = 0; s < 1 << n; ++s) { n       for (int j = 0; j < n; ++j) n         if (s >> j & 1) { n           pre[s][i] = pre[s][i] * inv[i][j] % mod; n         } n     } n   } n   vector<long long int> dp(1 << n, 1); n   for (int s = 0; s < 1 << n; ++s) { n     for (int t = s ^ (s & (-s)); t > 0; t = (t - 1) & s) { n       long long int res = dp[t]; n       for (int j = 0; j < n; ++j) n         if (t >> j & 1) { n           res = res * pre[t ^ s][j] % mod * pw[j][__builtin_popcount(t ^ s)] % n                 mod; n         } n       dp[s] -= res; n       if (dp[s] < 0) dp[s] += mod; n     } n   } n   long long int ans = 0; n   for (int s = 0; s < 1 << n; ++s) { n     int k = __builtin_popcount(s); n     long long int res = dp[s]; n     for (int i = 0; i < n; ++i) n       if (s >> i & 1) { n         for (int j = 0; j < n; ++j) n           if (~s >> j & 1) { n             res = res * a[i] % mod * inv[i][j] % mod; n           } n       } n     ans += res * k % mod; n     if (ans >= mod) ans -= mod; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int main() { n   int t, res = 0; n   long x, y, n; n   std::cin >> t; n   for (int i = 0; i < t; i++) { n     std::cin >> x >> y >> n; n     if (n > 0) { n       res = (n - y) % x; n       std::cout << n - res <<   n ; n     } else { n       std::cout << 0 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, i, j; n string S, R; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N >> S; n   for (i = N - 2; i >= 0; i -= 2) { n     R = R + S[i]; n   } n   if (N % 2 == 0) n     i = 1; n   else n     i = 0; n   for (i = i; i < N; i += 2) { n     R = R + S[i]; n   } n   cout << R << endl; n } n


#include <bits/stdc++.h> n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool flag = false; n   while (!isdigit(c)) { n     if (c ==  - ) flag = true; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); n   if (flag) x = -x; n } n using namespace std; n int n, q, nn; n bitset<2011> bt[1011][2], h[2011]; n int htot; n int ans[2011], pos[2011]; n inline void Gauss() { n   int nw = 1; n   for (int i = 1; i < nn; ++i) { n     int id = -1; n     for (int hang = nw; hang <= htot; ++hang) n       if (h[hang][i]) { n         id = hang; n         break; n       } n     if (id == -1) continue; n     swap(h[id], h[nw]); n     pos[i] = nw; n     for (int hang = 1; hang <= htot; ++hang) n       if (hang != nw && h[hang][i]) { n         h[hang] = h[hang] ^ h[nw]; n       } n     ++nw; n     if (nw > htot) break; n   } n   for (int i = 1; i <= htot; ++i) n     if (h[i][nn]) { n       bool flag = false; n       for (int j = 1; j < nn; ++j) n         if (h[i][j]) { n           flag = true; n           break; n         } n       if (!flag) { n         puts( NO ); n         exit(0); n       } n     } n   for (int i = 1; i < nn; ++i) { n     ans[i] = h[pos[i]][nn]; n   } n   puts( YES ); n   for (int i = 1; i <= n; ++i) { n     if (ans[i] == 0 && ans[i + n] == 0) n       putchar( . ); n     else if (ans[i] == 1 && ans[i + n] == 0) n       putchar( R ); n     else if (ans[i] == 0 && ans[i + n] == 1) n       putchar( B ); n     else n       putchar( Y ); n   } n   putchar(  n ); n } n int main() { n   read(n), read(q); n   nn = n + n + 1; n   for (int i = 1; i <= n; ++i) bt[i][0][i] = bt[i][1][i + n] = 1; n   for (int i = 1; i <= q; ++i) { n     char opt[6]; n     scanf( %s , opt); n     if (opt[0] ==  m ) { n       int k; n       read(k); n       htot += 2; n       for (int j = 1; j <= k; ++j) { n         int p; n         read(p); n         h[htot - 1] ^= bt[p][0]; n         h[htot] ^= bt[p][1]; n       } n       char res[3]; n       scanf( %s , res); n       if (res[0] ==  W ) n         h[htot - 1][nn] = 0, h[htot][nn] = 0; n       else if (res[0] ==  R ) n         h[htot - 1][nn] = 1, h[htot][nn] = 0; n       else if (res[0] ==  B ) n         h[htot - 1][nn] = 0, h[htot][nn] = 1; n       else n         h[htot - 1][nn] = 1, h[htot][nn] = 1; n     } else if (opt[0] ==  R  && opt[1] ==  Y ) { n       int k; n       read(k); n       for (int j = 1; j <= k; ++j) { n         int p; n         read(p); n         bt[p][1] = bt[p][1] ^ bt[p][0]; n       } n     } else if (opt[0] ==  R  && opt[1] ==  B ) { n       int k; n       read(k); n       for (int j = 1; j <= k; ++j) { n         int p; n         read(p); n         swap(bt[p][0], bt[p][1]); n       } n     } else if (opt[0] ==  Y  && opt[1] ==  B ) { n       int k; n       read(k); n       for (int j = 1; j <= k; ++j) { n         int p; n         read(p); n         bt[p][0] = bt[p][0] ^ bt[p][1]; n       } n     } n   } n   Gauss(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, t, n, k, a, b; n double v; n string line; n char s[4]; n int main() { n   while (cin >> n >> a >> b) { n     if (b < 0) b = n + b; n     a = a + b; n     a = a % n; n     if (a == 0) n       a = n; n     else if (a < 0) n       a = n + a; n     cout << a; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n pair<int, int> p[N]; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   int i = 0, j = 0; n   while (j < n) { n     if (a[i] < a[j]) i++; n     j++; n   } n   cout << i << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = (int)1e9; n using namespace std; n const int MAX = 1e3 + 5; n int n, xa, ya, xb, yb; n pair<pair<int, int>, int> arr[MAX]; n vector<pair<int, int> > vec; n bool covered(int x1, int y1, int x2, int y2, int R) { n   double dist = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n   return dist <= (double)R; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> xa >> ya >> xb >> yb >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i].first.first >> arr[i].first.second >> arr[i].second; n   } n   if (xa > xb) swap(xa, xb); n   if (ya > yb) swap(ya, yb); n   for (int i = ya; i <= yb; i++) { n     vec.push_back(make_pair(xa, i)); n     vec.push_back(make_pair(xb, i)); n   } n   for (int i = xa + 1; i < xb; i++) { n     vec.push_back(make_pair(i, ya)); n     vec.push_back(make_pair(i, yb)); n   } n   int res = 0; n   for (int i = 0; i < vec.size(); i++) { n     bool flag = false; n     for (int j = 0; j < n; j++) { n       if (covered(vec[i].first, vec[i].second, arr[j].first.first, n                   arr[j].first.second, arr[j].second)) { n         flag = true; n         break; n       } n     } n     if (!flag) res++; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n int n, m, fa[N], f[N], d[N], head[N], xx, yy, ww, fr[N]; n int ans; n bool in[N], c[N]; n struct nd { n   int x, y, w, id; n } a[N]; n bool operator<(nd a, nd b) { return a.w < b.w || (a.w == b.w && a.id < b.id); } n struct ndd { n   int ne, to, w; n } e[N]; n void ins(int x, int y, int w) { n   static int cnt; n   e[++cnt].to = y; n   e[cnt].ne = head[x]; n   head[x] = cnt; n   e[cnt].w = w; n } n int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); } n void dfs(int x) { n   for (int i = head[x]; i; i = e[i].ne) n     if (e[i].to != f[x]) { n       int y = e[i].to; n       f[y] = x; n       d[y] = d[x] + 1; n       fr[y] = e[i].w; n       dfs(y); n     } n } n void col(int x, int b) { n   c[x] = 1; n   for (int i = head[x]; i; i = e[i].ne) n     if (e[i].to != b) { n       int y = e[i].to; n       col(y, x); n     } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , &a[i].x, &a[i].y, &a[i].w); n     a[i].id = i; n   } n   xx = a[1].x, yy = a[1].y, ww = a[1].w; n   sort(a + 1, a + m + 1); n   for (int i = 1; i <= n; ++i) fa[i] = i; n   for (int i = 1; i <= m; ++i) { n     int fx = gf(a[i].x), fy = gf(a[i].y); n     if (fx != fy) { n       fa[fx] = fy; n       in[a[i].id] = 1; n       ins(a[i].x, a[i].y, a[i].w); n       ins(a[i].y, a[i].x, a[i].w); n     } n   } n   d[1] = 1; n   dfs(1); n   if (!in[1]) { n     while (xx != yy) { n       if (d[xx] < d[yy]) swap(xx, yy); n       ans = max(ans, fr[xx]); n       xx = f[xx]; n     } n   } else { n     ans = 1e9; n     col(yy, xx); n     for (int i = 1; i <= m; ++i) n       if (!in[a[i].id]) { n         if (c[a[i].x] ^ c[a[i].y]) ans = min(ans, a[i].w); n       } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   long long x, y, z; n   point() {} n   point(long long a, long long b, long long c) : x(a), y(b), z(c) {} n   double operator()(point y) { n     return sqrt((this->x - y.x) * (this->x - y.x) + n                 (this->y - y.y) * (this->y - y.y) + n                 (this->z - y.z) * (this->z - y.z)); n   } n }; n vector<pair<int, int>> ans; n int taken[10005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   long long a, b, c; n   point arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> a >> b >> c; n     point p(a, b, c); n     arr[i] = p; n   } n   for (int i = 0; i < n; i++) { n     double minimum = 1e11; n     int correct; n     if (taken[i]) continue; n     for (int j = 0; j < n; j++) { n       if (i == j || taken[j]) continue; n       double x = arr[i](arr[j]); n       if (x < minimum) { n         minimum = x; n         correct = j; n       } n     } n     taken[correct] = 1; n     taken[i] = 1; n     ans.push_back({i, correct}); n   } n   for (int i = 0; i < n / 2; i++) n     cout << ans[i].first + 1 <<     << ans[i].second + 1 <<   n ; n   return 0; n } n