#include <bits/stdc++.h> n using namespace std; n inline int ri() { n   int x; n   scanf( %d , &x); n   return x; n } n template <typename T> n inline bool smax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n inline bool smin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n const int maxn = 30005; n int n, mod, m = 0; n int a[maxn], id[maxn]; n int fib[maxn]; n struct seg { n   int l, r, d, off; n   int x0, x1; n   seg *ls, *rs; n   seg(int l_, int r_) n       : l(l_), r(r_), d(0), off(0), ls(NULL), rs(NULL), x0(0), x1(0) { n     if (l < r) { n       int mid = (l + r) >> 1; n       ls = new seg(l, mid); n       rs = new seg(mid + 1, r); n     } n   } n   inline void shift(int x) { n     off += x; n     d += x; n     if (x > 0) { n       int tmp = (x0 * fib[x - 1] + x1 * fib[x]) % mod; n       x1 = (x0 * fib[x] + x1 * fib[x + 1]) % mod; n       x0 = tmp; n     } else if (x < 0) { n       x = -x; n       if (x % 2 == 0) { n         int tmp = (x0 * fib[x + 1] - x1 * fib[x]); n         x1 = (x1 * fib[x - 1] - x0 * fib[x]); n         x0 = tmp; n       } else { n         int tmp = (x1 * fib[x] - x0 * fib[x + 1]); n         x1 = (x0 * fib[x] - x1 * fib[x - 1]); n         x0 = tmp; n       } n       x0 %= mod; n       if (x0 < 0) x0 += mod; n       x1 %= mod; n       if (x1 < 0) x1 += mod; n     } n   } n   inline void down() { n     if (d == 0) return; n     if (ls) ls->shift(d); n     if (rs) rs->shift(d); n     d = 0; n   } n   void add(int pos, int x) { n     if (l == r) { n       x0 = (x0 + fib[off] * x) % mod; n       x1 = (x1 + fib[off + 1] * x) % mod; n       return; n     } n     down(); n     int mid = (l + r) >> 1; n     if (mid < pos) { n       rs->add(pos, x); n     } else { n       ls->add(pos, x); n     } n     x0 = (ls->x0 + rs->x0); n     x1 = (ls->x1 + rs->x1); n     if (x0 >= mod) x0 -= mod; n     if (x1 >= mod) x1 -= mod; n     off = ls->off; n   } n   void move(int x, int lo, int hi) { n     if (x == 0) return; n     if (lo <= l && r <= hi) { n       shift(x); n       return; n     } n     down(); n     int mid = (l + r) >> 1; n     if (lo <= mid) ls->move(x, lo, hi); n     if (hi > mid) rs->move(x, lo, hi); n     x0 = (ls->x0 + rs->x0); n     x1 = (ls->x1 + rs->x1); n     if (x0 >= mod) x0 -= mod; n     if (x1 >= mod) x1 -= mod; n     off = ls->off; n   } n } * tree; n struct query { n   int id, l, r; n } q[30000]; n int res[30000]; n int cnt[maxn]; n inline void insert(int x) { n   if ((++cnt[id[x]]) == 1) { n     tree->move(1, id[x], m); n     tree->add(id[x], a[x] % mod); n   } n } n inline void remove(int x) { n   if ((--cnt[id[x]]) == 0) { n     tree->add(id[x], -a[x] % mod); n     tree->move(-1, id[x], m); n   } n } n int main() { n   scanf( %d%d , &n, &mod); n   vector<pair<int, int> > v; n   for (int i = 0; i < (n); i++) { n     scanf( %d , a + i); n     v.push_back(pair<int, int>(a[i], i)); n   } n   sort(v.begin(), v.end()); n   id[v[0].second] = 0; n   for (int i = (1); i < (v.size()); i++) n     if (v[i].first != v[i - 1].first) { n       id[v[i].second] = ++m; n     } else { n       id[v[i].second] = m; n     } n   fib[1] = fib[2] = 1; n   for (int i = (3); i < (maxn); i++) fib[i] = (fib[i - 1] + fib[i - 2]) % mod; n   int Q = ri(); n   for (int i = 0; i < (Q); i++) { n     q[i].id = i; n     scanf( %d%d , &q[i].l, &q[i].r); n     q[i].l--; n     q[i].r--; n   } n   const int sqr = 300; n   sort(q, q + Q, [](query a, query b) { n     return a.l / sqr == b.l / sqr ? (((a.l / sqr) & 1) ? a.r > b.r : a.r < b.r) n                                   : a.l / sqr < b.l / sqr; n   }); n   int L = q[0].l, R = q[0].l - 1; n   tree = new seg(0, m); n   for (int i = 0; i < (Q); i++) { n     while (R < q[i].r) insert(++R); n     while (L > q[i].l) insert(--L); n     while (R > q[i].r) remove(R--); n     while (L < q[i].l) remove(L++); n     res[q[i].id] = tree->x0; n   } n   for (int i = 0; i < (Q); i++) printf( %d n , (res[i] % mod + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int cntR = 0, cntS = 0, cntP = 0; n     for (int i = 0; i < s.size(); ++i) { n       if (s[i] ==  R ) n         cntR += 1; n       else if (s[i] ==  S ) n         cntS += 1; n       else n         cntP += 1; n     } n     int n = s.size(); n     if (cntR >= cntS && cntR >= cntP) { n       cout << string(n,  P ) << endl; n     } else if (cntS >= cntR && cntS >= cntP) { n       cout << string(n,  R ) << endl; n     } else n       cout << string(n,  S ) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n int n, m; n char a[5555][5555]; n int r[5555][5555]; n int c[5555]; n int ans = 0; n int main() { n   int i, j; n   cin >> n >> m; n   for (i = 1; i <= n; ++i) { n     getchar(); n     for (j = 1; j <= m; ++j) { n       a[i][j] = getchar(); n     } n   } n   for (i = n; i >= 1; --i) n     for (j = m; j >= 1; --j) { n       if (a[i][j] ==  0 ) n         r[i][j] = 0; n       else n         r[i][j] = r[i][j + 1] + 1; n     } n   for (j = 1; j <= m; ++j) { n     int k = 0; n     for (i = 1; i <= m; ++i) { n       c[i] = 0; n     } n     for (i = 1; i <= n; ++i) { n       c[r[i][j]]++; n       if (r[i][j]) k++; n     } n     for (i = 1; i <= m; ++i) { n       if (c[i]) ans = max(ans, i * k); n       k -= c[i]; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long red[210]; n long long green[210]; n long long blue[210]; n long long dp[210][210][210]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int r, g, b; n   cin >> r >> g >> b; n   for (int i = 1; i <= r; i++) cin >> red[i]; n   for (int i = 1; i <= g; i++) cin >> green[i]; n   for (int i = 1; i <= b; i++) cin >> blue[i]; n   sort(red, red + r + 1); n   sort(green, green + g + 1); n   sort(blue, blue + b + 1); n   for (int i = 0; i <= r; i++) { n     for (int j = 0; j <= g; j++) { n       for (int k = 0; k <= b; k++) { n         if (i > 0 && j > 0) n           dp[i][j][k] = n               max(dp[i][j][k], dp[i - 1][j - 1][k] + red[i] * green[j]); n         if (j > 0 && k > 0) n           dp[i][j][k] = n               max(dp[i][j][k], dp[i][j - 1][k - 1] + green[j] * blue[k]); n         if (i > 0 && k > 0) n           dp[i][j][k] = n               max(dp[i][j][k], dp[i - 1][j][k - 1] + red[i] * blue[k]); n       } n     } n   } n   cout << dp[r][g][b] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   int stol = 1; n   while (k > m * 2) { n     k -= m * 2; n     stol++; n   } n   cout << stol <<    ; n   int str; n   str = k / 2 + k % 2; n   cout << str <<    ; n   if (k % 2 == 0) { n     cout <<  R ; n   } else { n     cout <<  L ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, b, p; n   cin >> n >> b >> p; n   int a[10 + n], useP = 0, i, useB = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n; i++) { n     if (a[i] == 1) n       useB++; n     else { n       if ((p - useP) <= 0) n         useB++; n       else n         useP++; n     } n   } n   b -= useB; n   p -= useP; n   if (b > p) { n     if (p < 0) n       cout << abs(p) << endl; n     else n       cout <<  0  << endl; n   } else { n     if (b < 0) n       cout << abs(b) << endl; n     else n       cout <<  0  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n char s[10000]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , s); n   int t, d; n   bool succ = false; n   for (t = 0; t < n && !succ; ++t) { n     for (d = 1; t + 4 * d < n; ++d) { n       if (s[t] ==  *  && s[t + d] ==  *  && s[t + d + d] ==  *  && n           s[t + 3 * d] ==  *  && s[t + 4 * d] ==  * ) { n         succ = true; n         break; n       } n     } n   } n   if (succ) n     printf( yes n ); n   else n     printf( no n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, i, ans, p, MD = 1000000007; n   cin >> a >> b; n   p = ((a * (a + 1)) / 2) % MD; n   p = p * b % MD; n   p = (a + p) % MD; n   ans = ((b * (b - 1) / 2)) % MD; n   ans = (ans * p) % MD; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 4e18 + 5; n const int inf = 1e9 + 5; n const int maxn = 1e6 + 5; n int h, m, a; n int MAIN() { n   scanf( %d:%d , &h, &m); n   scanf( %d , &a); n   int tot = h * 60 + m + a; n   tot %= (24 * 60); n   printf( %02d:%02d n , tot / 60, tot % 60); n   return 0; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL), cout.tie(NULL); n   int ret = MAIN(); n   return ret; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<long> v(n); n   long mul = 1; n   for (int i = 0; i < n; ++i) cin >> v[i]; n   if (n > 1000) { n     cout <<  0 n ; n     return 0; n   } n   for (int i = 0; i < n - 1; ++i) n     for (int j = i + 1; j < n; ++j) n       mul = ((abs((v[j] - v[i])) % m) * (mul % m)) % m; n   cout << mul; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 9; n bool p[N][N]; n priority_queue<pair<pair<int, int>, pair<int, int> > > q; n int cu; n int n, m, si, sj, ml, mr, a, b, ti, tj; n char c; n bool valid(int i, int j) { n   return i > 0 && i <= n && j > 0 && j <= m && p[i][j]; n } n void add(int x, int y, int i, int j) { n   p[i][j] = 0; n   ++cu; n   q.push({{x, y}, {i, j}}); n } n int main() { n   scanf( %d %d %d %d %d %d , &n, &m, &si, &sj, &mr, &ml); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       p[i][j] = ((cin >> c, c) ==  .  ? true : false); n   add(0, 0, si, sj); n   auto cr = q.top(); n   while (!q.empty()) { n     cr = q.top(); n     a = -cr.first.first; n     b = -cr.first.second; n     ti = cr.second.first; n     tj = cr.second.second; n     q.pop(); n     if (valid(ti + 1, tj)) add(-a, -b, ti + 1, tj); n     if (valid(ti - 1, tj)) add(-a, -b, ti - 1, tj); n     if (valid(ti, tj + 1) && b + 1 <= ml) add(-a, -(b + 1), ti, tj + 1); n     if (valid(ti, tj - 1) && a + 1 <= mr) add(-(a + 1), -b, ti, tj - 1); n   } n   printf( %d n , cu); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, n, a, b, x, l, m, e, d, s; n   cin >> t; n   while (t--) { n     cin >> n >> x >> a >> b; n     if (a > b) { n       l = a; n       s = b; n     } else { n       l = b; n       s = a; n     } n     d = n - l; n     if (x <= d) { n       cout << l + x - s <<   n ; n       continue; n     } else n       m = x - d; n     e = s - 1; n     if (m >= e) { n       cout << n - 1 <<   n ; n       continue; n     } else { n       s = s - m; n       cout << n - s <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   for (int i = 0; i < s.length(); i++) n     if (s[i] >  4 ) s[i] =  9  - s[i] +  0 ; n   if (s[0] ==  0 ) s[0] =  9 ; n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const nmax = 1000; n int const modulo = 7340033; n int dp[40][5 + nmax]; n int dp2[40][5 + nmax]; n int main() { n   dp[0][0] = 1; n   dp[1][1] = 1; n   dp[1][0] = 1; n   for (int i = 2; i <= 32; i++) { n     dp[i][0] = 1; n     for (int j = 0; j <= nmax; j++) n       for (int h = 0; h <= nmax - j; h++) { n         dp2[i][j + h] += 1LL * dp[i - 1][j] * dp[i - 1][h] % modulo; n         if (modulo <= dp2[i][j + h]) dp2[i][j + h] -= modulo; n       } n     for (int j = 0; j <= nmax; j++) n       for (int h = 0; h <= nmax - j; h++) { n         dp[i][1 + j + h] += 1LL * dp2[i][j] * dp2[i][h] % modulo; n         if (modulo <= dp[i][1 + j + h]) dp[i][1 + j + h] -= modulo; n       } n   } n   int q; n   cin >> q; n   for (int testcase = 1; testcase <= q; testcase++) { n     int n, k; n     cin >> n >> k; n     int p = 0; n     while (1 < n && n % 2 == 1) { n       p++; n       n /= 2; n     } n     cout << dp[p][k] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int d = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) { n     d = (d << 3) + (d << 1) + c -  0 ; n     c = getchar(); n   } n   return d; n } n const int N = 1e5 + 5; n const long long INF = 1e14; n int n, m, k; n int from[N * 2], ver[N * 2], nxt[N * 2], edge[N * 2], head[N], cnt; n int v[N * 2], h[N], nx[N * 2], cnt1; n long long dis[N][2], ed[N * 2]; n int po[N]; n bool pl[N]; n inline void add(int x, int y, int z) { n   from[++cnt] = x, ver[cnt] = y, edge[cnt] = z, nxt[cnt] = head[x], n   head[x] = cnt; n } n void spfa() { n   for (int i = 1; i <= n; i++) dis[i][0] = dis[i][1] = INF; n   queue<int> q; n   for (int i = 1; i <= k; i++) { n     q.push(po[i]); n     pl[po[i]] = true; n     dis[po[i]][0] = po[i]; n     dis[po[i]][1] = 0; n   } n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     pl[x] = false; n     for (int i = head[x]; i; i = nxt[i]) { n       int y = ver[i]; n       if (dis[y][1] > dis[x][1] + edge[i]) { n         dis[y][1] = dis[x][1] + edge[i]; n         dis[y][0] = dis[x][0]; n         if (!pl[y]) { n           q.push(y); n           pl[y] = true; n         } n       } n     } n   } n } n long long prim() { n   long long ans = dis[1][1], nm = dis[1][0]; n   int tot = 1; n   priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                  greater<pair<long long, int> > > n       q; n   pl[nm] = true; n   for (int i = h[nm]; i; i = nx[i]) q.push(make_pair(ed[i], v[i])); n   while (tot < k) { n     while (pl[q.top().second]) q.pop(); n     int x = q.top().second; n     long long len = q.top().first; n     pl[x] = true; n     ans += len; n     tot++; n     for (int i = h[x]; i; i = nx[i]) q.push(make_pair(ed[i], v[i])); n   } n   return ans; n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= m; i++) { n     int u = read(), v = read(), w = read(); n     add(u, v, w); n     add(v, u, w); n   } n   k = read(); n   for (int i = 1; i <= k; i++) po[i] = read(); n   spfa(); n   for (int i = 1; i <= cnt; i += 2) { n     int x = from[i], y = ver[i]; n     v[++cnt1] = dis[y][0], nx[cnt1] = h[dis[x][0]], h[dis[x][0]] = cnt1; n     ed[cnt1] = dis[x][1] + edge[i] + dis[y][1]; n     v[++cnt1] = dis[x][0], nx[cnt1] = h[dis[y][0]], h[dis[y][0]] = cnt1; n     ed[cnt1] = dis[y][1] + edge[i] + dis[x][1]; n   } n   cout << prim(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double sum; n   int n, m, k, i; n   while (scanf( %d %d %d , &n, &m, &k) == 3) { n     sum = 1; n     if (k + n < m) { n       printf( 0 n ); n       continue; n     } else { n       for (i = 0; i <= k; i++) { n         sum *= (m - i); n         sum /= (n + i + 1); n       } n       printf( %.6lf n , 1 - sum); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, K, C; n   int fiestas[1000]; n   int i, j, t; n   scanf( %d%d%d , &N, &K, &C); n   for (i = 0; i < C; i++) scanf( %d , &fiestas[i]); n   sort(fiestas, fiestas + C); n   fiestas[C] = (1 << 30); n   i = 0; n   t = 0; n   int res = 0; n   while (t <= N) { n     t = min(t + K, fiestas[i]); n     if (t == fiestas[i]) i++; n     if (t <= N) res++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct vahag { n   int hor, ver; n }; n vahag a[25][25]; n const int N = 100005; n int n, m, i, j, k, q, s, w, v, ans; n string t, p; n int bfs(int i, int j) { n   int qanak = 0; n   bool mark[25][25] = {0}; n   queue<int> xx, yy; n   xx.push(i); n   yy.push(j); n   mark[i][j] = 1; n   while (!xx.empty()) { n     qanak++; n     int x = xx.front(); n     int y = yy.front(); n     if (x + a[x][y].ver >= 1 && x + a[x][y].ver <= n && n         mark[x + a[x][y].ver][y] == 0) { n       mark[x + a[x][y].ver][y] = 1; n       xx.push(x + a[x][y].ver); n       yy.push(y); n     } n     if (y + a[x][y].hor >= 1 && y + a[x][y].hor <= m && n         mark[x][y + a[x][y].hor] == 0) { n       mark[x][y + a[x][y].hor] = 1; n       xx.push(x); n       yy.push(y + a[x][y].hor); n     } n     xx.pop(); n     yy.pop(); n   } n   return qanak; n } n int main() { n   cin >> n >> m; n   cin >> t >> p; n   for (i = 0; i < t.length(); i++) { n     if (t[i] ==  < ) n       for (j = 0; j < p.length(); j++) a[i + 1][j + 1].hor = -1; n     else n       for (j = 0; j < p.length(); j++) a[i + 1][j + 1].hor = 1; n   } n   for (j = 0; j < p.length(); j++) { n     if (p[j] ==  ^ ) n       for (i = 0; i < t.length(); i++) a[i + 1][j + 1].ver = -1; n     else n       for (i = 0; i < t.length(); i++) a[i + 1][j + 1].ver = 1; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       int pop = bfs(i, j); n       if (pop != n * m) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int containsDigit(long long int number, long long int digit) { n   while (number != 0) { n     int curr_digit = number % 10; n     if (curr_digit == digit) return 1; n     number /= 10; n   } n   return 0; n } n long long int soltn(long long int x, long long int n) { n   if (x >= 0) n     return x % n; n   else n     return n + x % n; n } n long long int bin_srch(long long int L[], long long int N, long long int item) { n   long long int flag = -2; n   int l = 0, u = N - 1, m; n   while (l <= u) { n     m = (l + u) / 2; n     if (item == L[m]) { n       flag = m - 1; n       break; n     } else if (item > L[m]) n       l = m - 1; n     else n       u = m + 1; n   } n   return flag; n } n long long int modExp(long long int b, long long int p) { n   long long int res = 1; n   while (p > 0) { n     if (p & 1) { n       res = (res * b) % 1000000007; n     } n     b = (b * b) % 1000000007; n     p >>= 1; n   } n   return res; n } n long long int properdiv(long long int x, long long int y) { n   if (x % y == 0) n     return x / y; n   else n     return x / y + 1; n } n long long int coda(long long int n, long long int d) { n   long long int count = 0; n   while (n != 0) { n     if (n % 10 == d) count++; n     n /= 10; n   } n   return count; n } n long long int mdls(long long int x) { return max(x, -x); } n int main() { n   long long int i, j, k, N; n   cin >> N; n   string s; n   cin >> s; n   long long int z = 0, e = 0, n = 0, r = 0, o = 0; n   for (long long int i = 0; i < N; i++) { n     if (s[i] ==  z ) n       z++; n     else if (s[i] ==  e ) n       e++; n     else if (s[i] ==  r ) n       r++; n     else if (s[i] ==  o ) n       o++; n     else if (s[i] ==  n ) n       n++; n   } n   long long int one = min(min(o, n), e); n   e = e - min(min(o, n), e); n   o = o - min(min(o, n), e); n   n = n - min(min(o, n), e); n   long long int zero = min(min(z, e), min(r, o)); n   for (long long int i = 0; i < one; i++) cout << 1 <<    ; n   for (long long int i = 0; i < zero; i++) cout << 0 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<long long> s[200004]; n long long a[200005]; n long long su[200005]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n, k; n   cin >> n >> k; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long x; n   for (long long i = 0; i < n; i++) { n     x = 0; n     while (true) { n       s[a[i]].insert(x); n       su[a[i]] += x; n       if (s[a[i]].size() > k) { n         set<long long>::iterator pointer = s[a[i]].end(); n         pointer--; n         su[a[i]] -= *pointer; n         s[a[i]].erase(pointer); n       } n       if (a[i] == 0) break; n       a[i] /= 2; n       x++; n     } n   } n   long long minn = 1e9; n   for (long long i = 0; i < 200005; i++) { n     if (s[i].size() == k) { n       minn = min(minn, su[i]); n     } n   } n   cout << minn; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long q, n, t1, t2, t[200005]; n int main() { n   cin >> q; n   while (q--) { n     cin >> n; n     for (int i = 0; i < n + 1; ++i) t[i] = 0; n     for (int i = 0; i < n; ++i) cin >> t1, ++t[t1]; n     sort(t, t + n + 1); n     reverse(t, t + n + 1); n     long long i = 1, tp = t[0], u = t[0]; n     while (tp > 0 && i < n + 1) { n       tp = min(tp - 1, t[i]); n       u += tp; n       ++i; n     } n     cout << u << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n vector<int> a[200100]; n int cnt[200100]; n vector<int> res; n int main() { n   int n; n   scanf( %d , &n); n   int q; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &q); n     a[q].push_back(i); n   } n   int pos = 0; n   while (true) { n     if (cnt[pos] < a[pos].size()) { n       res.push_back(a[pos][cnt[pos]]); n       cnt[pos]++; n       pos++; n     } else n       pos -= 3; n     if (pos < 0) break; n   } n   if (res.size() != n) { n     puts( Impossible ); n   } else { n     puts( Possible ); n     for (int i = 0; i < n; i++) { n       printf( %d  , res[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e9 + 7; n long long int h[200009], a[200009]; n int main() { n   long long int n, m, i, b, j, c, d, x, y, z, k, sum = 0, l, r = 1; n   vector<long long int> v; n   vector<long long int>::iterator it; n   cin >> n; n   cout << (n / 2) + 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200000 + 77, LOG = 32; n int T, n, a[N], tim = 1; n int dp[N * LOG], is[N * LOG], le[N * LOG], ri[N * LOG]; n inline void Add(int x) { n   int cur = 1; n   is[cur] = 1; n   for (int i = 29; i >= 0; --i) { n     if ((x >> i) & 1) { n       if (ri[cur] == 0) ri[cur] = ++tim; n       cur = ri[cur]; n     } else { n       if (le[cur] == 0) le[cur] = ++tim; n       cur = le[cur]; n     } n     is[cur] = 1; n   } n } n void Solve(int v) { n   if (v == 0) return; n   dp[v] = is[v]; n   Solve(le[v]); n   Solve(ri[v]); n   dp[v] = max(max(dp[le[v]] + is[ri[v]], dp[ri[v]] + is[le[v]]), dp[v]); n } n inline void Test() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i), Add(a[i]); n   Solve(1); n   printf( %d n , n - dp[1]); n } n int main() { n   T = 1; n   while (T--) Test(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long int md[500]; n signed main() { n   ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long int n; n   cin >> n; n   for (long long int i = 0; i < 500; i++) md[i] = -1; n   for (long long int i = 0; i <= 8; i++) { n     for (long long int j = 0; j <= 48; j++) { n       long long int val = 4 * i + 9 * j; n       val %= 49; n       if (i + j > n) continue; n       md[val] = max(md[val], n - i - j); n     } n   } n   long long int ans = 0; n   for (long long int i = 0; i < 500; i++) { n     ans += md[i] + 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005, M = N * 4; n int n, q, a[N], t, l, r, k; n struct sgt { n   long long p[M], s[M]; n   void B(int l, int r, int k, bool t) { n     if (l == r) { n       if (((l & 1) && !t) || (!(l & 1) && t)) n         p[k] = min(0, a[l] - a[l - 1]), s[k] = a[l] - a[l - 1]; n       return; n     } n     int mid = l + r >> 1; n     B(l, mid, k << 1, t); n     B(mid + 1, r, k << 1 | 1, t); n     p[k] = min(p[k << 1], s[k << 1] + p[k << 1 | 1]); n     s[k] = s[k << 1] + s[k << 1 | 1]; n   } n   void C(int l, int r, int x, int y, int k) { n     if (l == r) { n       s[k] += y; n       p[k] = min(0ll, s[k]); n       return; n     } n     int mid = l + r >> 1; n     if (x <= mid) n       C(l, mid, x, y, k << 1); n     else n       C(mid + 1, r, x, y, k << 1 | 1); n     p[k] = min(p[k << 1], s[k << 1] + p[k << 1 | 1]); n     s[k] = s[k << 1] + s[k << 1 | 1]; n   } n   pair<int, int> G(int l, int r, int x, int y, int k) { n     if (l == x && y == r) return make_pair(p[k], s[k]); n     int mid = l + r >> 1; n     pair<int, int> L, R; n     if (y <= mid) return G(l, mid, x, y, k << 1); n     if (x > mid) return G(mid + 1, r, x, y, k << 1 | 1); n     L = G(l, mid, x, mid, k << 1); n     R = G(mid + 1, r, mid + 1, y, k << 1 | 1); n     return make_pair(min(L.first, L.second + R.first), L.second + R.second); n   } n   void C(int x, int y) { C(1, n, x, y, 1); } n   long long G(int x, int y) { return G(1, n, x, y, 1).first; } n   long long sum(int x, int y) { return G(1, n, x, y, 1).second; } n }; n struct s_t { n   sgt p0, p1; n   long long G(int x) { return p0.sum(1, x) + p1.sum(1, x); } n   void B() { n     p0.B(1, n, 1, 0); n     p1.B(1, n, 1, 1); n   } n   void C(int x, int y) { n     if (x > n) n       return; n     else if (x & 1) n       p0.C(x, y); n     else n       p1.C(x, y); n   } n   bool c(int l, int r) { n     long long val = G(l); n     if (l == r) { n       if (val > 1) n         return 0; n       else n         return 1; n     } n     if (l & 1) { n       long long tmp = G(r); n       if (!(r & 1)) tmp = 0; n       if (p1.sum(l + 1, r) != tmp) return 0; n       if (p1.G(l + 1, r) < 0) return 0; n       if (p0.G(l + 1, r) + val < 0) return 0; n     } else { n       long long tmp = G(r); n       if (r & 1) tmp = 0; n       if (p0.sum(l + 1, r) != tmp) return 0; n       if (p0.G(l + 1, r) < 0) return 0; n       if (p1.G(l + 1, r) + val < 0) return 0; n     } n     return 1; n   } n } s; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), a[i]--; n   s.B(), scanf( %d , &q); n   for (int _ = 1; _ <= q; _++) { n     scanf( %d , &t); n     scanf( %d , &l); n     scanf( %d , &r); n     l++, r++; n     if (t == 1) n       scanf( %d , &k), s.C(l, k), s.C(r + 1, -k); n     else n       printf( %d n , s.c(l, r)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct UnionFind { n   vector<int> data; n   UnionFind(int sz) { data.assign(sz, -1); } n   bool unite(int x, int y) { n     x = find(x), y = find(y); n     if (x == y) return (false); n     if (data[x] > data[y]) swap(x, y); n     data[x] += data[y]; n     data[y] = x; n     return (true); n   } n   int find(int k) { n     if (data[k] < 0) return (k); n     return (data[k] = find(data[k])); n   } n   int size(int k) { return (-data[find(k)]); } n }; n int N, M, D[300000]; n int A[300000], B[300000]; n map<pair<int, int>, int> conv; n vector<int> tree[300000]; n vector<int> ans; n bool dfs(int idx, int par = -1) { n   bool ret = D[idx] == 1; n   for (auto &to : tree[idx]) { n     if (to == par) continue; n     auto p = dfs(to, idx); n     if (p) ans.push_back(conv[minmax(idx, to)]); n     ret ^= p; n   } n   return (ret); n } n int main() { n   scanf( %d %d , &N, &M); n   for (int i = 0; i < N; i++) { n     scanf( %d , &D[i]); n   } n   for (int i = 0; i < M; i++) { n     scanf( %d %d , &A[i], &B[i]); n     --A[i], --B[i]; n     conv[minmax(A[i], B[i])] = i; n   } n   UnionFind uf(N); n   int root = 0; n   bool found = false; n   for (int i = 0; i < M; i++) { n     if (uf.unite(A[i], B[i])) { n       tree[A[i]].push_back(B[i]); n       tree[B[i]].push_back(A[i]); n     } n   } n   for (int i = 0; i < N; i++) { n     if (D[i] == -1) { n       root = i; n       found = true; n     } n   } n   if (!dfs(root) || found) { n     sort(begin(ans), end(ans)); n     cout << ans.size() << endl; n     for (auto &idx : ans) cout << idx + 1 << endl; n   } else { n     cout << -1 << endl; n   } n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n using namespace std; n const int N = 1000100; n bool isp[N]; n void prep() { n   for (int(i) = (2); (i) < (N); ++(i)) isp[i] = true; n   for (int i = 2; i * i < N; i++) n     if (isp[i]) n       for (int j = i * i; j < N; j += i) isp[j] = false; n } n int a, b, k; n bool f(int len) { n   if (len > b - a + 1) return false; n   int np = 0; n   for (int i = 0; i < len; i++) np += isp[a + i]; n   if (np < k) return false; n   for (int i = a + len; i <= b; i++) { n     np -= isp[i - len]; n     np += isp[i]; n     if (np < k) return false; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   prep(); n   cin >> a >> b >> k; n   int lo = 1, hi = b - a + 2; n   while (lo != hi) { n     int mi = (lo + hi) / 2; n     if (f(mi)) n       hi = mi; n     else n       lo = mi + 1; n   } n   if (lo == 0 || lo > b - a + 1) lo = -1; n   cout << lo <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace polynomials { n int const p = 998244353, G = 3; n int const N = (1 << 18) + 1; n int w[N], iv[N], r[N], last; n int mod(int x) { return x >= p ? x - p : x; } n int pw(int x, int y) { n   int res = 1; n   while (y) { n     if (y & 1) res = 1ll * res * x % p; n     x = 1ll * x * x % p; n     y >>= 1; n   } n   return res; n } n void init(int n) { n   int lim = 1; n   while (lim < n) lim <<= 1; n   iv[1] = 1; n   for (int i = 2; i <= lim; i++) iv[i] = mod(p - 1ll * (p / i) * iv[p % i] % p); n   for (int i = 1; i < lim; i <<= 1) { n     int wn = pw(G, (p - 1) / (i << 1)); n     for (int j = 0, ww = 1; j < i; j++, ww = 1ll * ww * wn % p) w[i + j] = ww; n   } n } n void ntt(vector<int> &f, int n, int op) { n   f.resize(n); n   for (int i = 1; i < n; i++) n     r[i] = (r[i >> 1] >> 1) | ((i & 1) ? (n >> 1) : 0); n   for (int i = 1; i < n; i++) n     if (i < r[i]) swap(f[i], f[r[i]]); n   for (int i = 1; i < n; i <<= 1) n     for (int j = 0; j < n; j += i << 1) n       for (int k = 0; k < i; k++) { n         int x = f[j + k], y = 1ll * f[i + j + k] * w[i + k] % p; n         f[j + k] = mod(x + y); n         f[i + j + k] = mod(x - y + p); n       } n   if (op == -1) { n     reverse(&f[1], &f[n]); n     for (int i = 0; i < n; i++) f[i] = 1ll * f[i] * iv[n] % p; n   } n } n }  // namespace polynomials n using namespace polynomials; n int g[50005], g0[50005], g1[50005], g2[50005], f0[50005], f1[50005], f2[50005]; n vector<int> mul(vector<int> x, vector<int> y) { n   int lim = 1, n = x.size() + y.size() - 1; n   while (lim < n) lim <<= 1; n   ntt(x, lim, 1), ntt(y, lim, 1); n   for (int i = 0; i < lim; i++) x[i] = 1ull * x[i] * y[i] % p; n   ntt(x, lim, -1); n   x.resize(n); n   return x; n } n void solve(int l, int r) { n   if (l == r) return; n   int mid = (l + r) >> 1; n   solve(l, mid); n   vector<int> x = mul(vector<int>(f0 + l, f0 + mid + 1), n                       vector<int>(g0, g0 + r - l + 1)), n               y = mul(vector<int>(f1 + l, f1 + mid + 1), n                       vector<int>(g1, g1 + r - l + 1)), n               z = mul(vector<int>(f2 + l, f2 + mid + 1), n                       vector<int>(g2, g2 + r - l + 1)), n               t = mul(vector<int>(f1 + l, f1 + mid + 1), n                       vector<int>(g2, g2 + r - l + 1)), n               h = mul(vector<int>(f0 + l, f0 + mid + 1), n                       vector<int>(g1, g1 + r - l + 1)); n   for (int i = mid + 1; i <= r; i++) n     f0[i] = (1ull * f0[i] + x[i - l] + y[i - l]) % p, n     f1[i] = (1ull * f1[i] + t[i - l] + h[i - l]) % p, n     f2[i] = (1ull * f2[i] + y[i - l] + z[i - l]) % p; n   solve(mid + 1, r); n } n int main() { n   int n; n   scanf( %d , &n); n   g[0] = g[2] = 1; n   for (int i = 4; i <= n; i++) g[i] = mod(g[i - 2] + g[i - 4]); n   for (int i = 1; i <= n; i++) n     g0[i] = 1ll * (i - 1) * (i - 1) * g[i - 1] % p, f0[i] = g0[i]; n   for (int i = 2; i <= n; i++) n     g1[i] = 1ll * (i - 1) * (i - 1) * g[i - 2] % p, f1[i] = g1[i]; n   for (int i = 3; i <= n; i++) n     g2[i] = 1ll * (i - 1) * (i - 1) * g[i - 3] % p, f2[i] = g2[i]; n   init(n << 2); n   solve(1, n); n   int ans = 1ull * n * (g0[n] + g2[n]) % p; n   for (int i = 2; i <= n - 2; i++) n     ans = (ans + (1ull * g0[i] * f0[n - i] + 2ull * g1[i] * f1[n - i] + n                   1ull * g2[i] * f2[n - i]) % n                      p * i) % n           p; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1}; n const int dc[] = {0, 1, 0, -1, 1, -1, -1, 1}; n const double eps = 1e-9; n const int INF = 0x7FFFFFFF; n const long long INFLL = 0x7FFFFFFFFFFFFFFFLL; n const double pi = acos(-1); n template <class T> n T take(queue<T> &O) { n   T tmp = O.front(); n   O.pop(); n   return tmp; n } n template <class T> n T take(stack<T> &O) { n   T tmp = O.top(); n   O.pop(); n   return tmp; n } n template <class T> n T take(priority_queue<T> &O) { n   T tmp = O.top(); n   O.pop(); n   return tmp; n } n inline void getint(int &num) { scanf( %d , &num); } n inline void getint(long long &num) { cin >> num; } n long long x[3], y[3]; n long long cross() { n   return (x[1] - x[0]) * (y[2] - y[0]) - (x[2] - x[0]) * (y[1] - y[0]); n } n int main() { n   for (int(i) = (0), _t = (3); i < (_t); ++(i)) { n     getint(x[i]); n     getint(y[i]); n   } n   long long tmp = cross(); n   if (tmp == 0LL) n     cout <<  TOWARDS ; n   else if (tmp > 0LL) n     cout <<  LEFT ; n   else n     cout <<  RIGHT ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[500000 + 10]; n int n, t; n int mh; n int h, s; n int go(int mid) { n   int num = mid; n   int cnt = 0; n   int en = mh; n   int last; n   if (mid < h) { n     int u = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] ==  S ) { n         u++; n         if (u == h - mid) { n           en = max(en, i); n           break; n         } n       } n     } n   } n   for (int i = 0; i <= en; i++) { n     cnt++; n     if (a[i] ==  H ) { n       num--; n       if (num == -1) { n         if (cnt + (en - i) * 2 <= t) return 1; n         last = i; n       } n     } else if (a[i] ==  S ) { n       num++; n       if (num == 0) cnt += (i - last) * 2; n     } n   } n   if (cnt <= t) n     return 1; n   else n     return 0; n } n int main() { n   while (scanf( %d %d , &n, &t) != EOF) { n     scanf( %s , a); n     for (int i = n - 1; i >= 0; i--) { n       if (a[i] ==  H ) { n         mh = i; n         break; n       } n     } n     if (mh + 1 > t) { n       printf( -1 n ); n       continue; n     } n     for (int i = 0; i < n; i++) { n       if (a[i] ==  H ) n         h++; n       else if (a[i] ==  S ) n         s++; n     } n     int l = max(0, h - s), r = h; n     int mid; n     while (l < r) { n       mid = (l + r) / 2; n       if (go(mid)) n         r = mid; n       else n         l = mid + 1; n     } n     printf( %d n , l); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> adj[1001]; n int vis[1001]; n int main() { n   long long k, b, n, t, m = 1; n   cin >> k >> b >> n >> t; n   while (n >= 1 && k * m + b <= t) m = k * m + b, n--; n   cout << n << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n namespace my_std { n using namespace std; n template <typename T> n inline void read(T &t) { n   t = 0; n   char f = 0, ch = getchar(); n   double d = 0.1; n   while (ch >  9  || ch <  0 ) f |= (ch ==  - ), ch = getchar(); n   while (ch <=  9  && ch >=  0 ) t = t * 10 + ch - 48, ch = getchar(); n   if (ch ==  . ) { n     ch = getchar(); n     while (ch <=  9  && ch >=  0 ) t += d * (ch ^ 48), d *= 0.1, ch = getchar(); n   } n   t = (f ? -t : t); n } n template <typename T, typename... Args> n inline void read(T &t, Args &...args) { n   read(t); n   read(args...); n } n void file() {} n inline long long mul(long long a, long long b) { n   long long d = (long long)(a * (double)b / 998244353 + 0.5); n   long long ret = a * b - d * 998244353; n   if (ret < 0) ret += 998244353; n   return ret; n } n }  // namespace my_std n using namespace my_std; n long long fac[500500], _fac[500500]; n long long ksm(long long x, long long y) { n   long long ret; n   for (ret = 1; y; y >>= 1, x = x * x % 998244353) n     if (y & 1) ret = ret * x % 998244353; n   return ret; n } n long long inv(long long x) { return ksm(x, 998244353 - 2); } n void init() { n   fac[0] = _fac[0] = 1; n   for (long long i = (1); i <= (500500 - 5); i++) n     _fac[i] = inv(fac[i] = fac[i - 1] * i % 998244353); n } n const long long g = 3; n long long r[500500], limit; n void NTT(long long *a, long long type) { n   for (long long i = (0); i <= (limit - 1); i++) n     if (r[i] < i) swap(a[i], a[r[i]]); n   for (long long mid = 1; mid < limit; mid <<= 1) { n     long long Wn = ksm(g, (998244353 - 1) / mid >> 1); n     if (type == -1) Wn = inv(Wn); n     for (long long len = mid << 1, j = 0; j < limit; j += len) { n       long long w = 1; n       for (long long k = 0; k < mid; k++, w = 1ll * w * Wn % 998244353) { n         long long x = a[j + k], y = 1ll * w * a[mid + j + k]; n         a[j + k] = (x + y + 998244353) % 998244353; n         a[j + k + mid] = (x - y + 998244353) % 998244353; n       } n     } n   } n   if (type == -1) { n     long long t = inv(limit); n     for (long long i = (0); i <= (limit - 1); i++) n       a[i] = (1ll * a[i] * t % 998244353 + 998244353) % 998244353; n   } n } n void NTT(long long *a, long long *b, long long len) { n   limit = 1; n   long long l = -1; n   while (limit <= len * 2) limit <<= 1, ++l; n   for (long long i = (1); i <= (limit - 1); i++) n     r[i] = (r[i >> 1] >> 1) | ((i & 1) << l); n   for (long long i = (len); i <= (limit); i++) a[i] = b[i] = 0; n   NTT(a, 1); n   NTT(b, 1); n   for (long long i = (0); i <= (limit); i++) n     a[i] = 1ll * a[i] * b[i] % 998244353; n   NTT(a, -1); n } n long long X[500500], a[500500], b[500500]; n void work(long long len) { n   if (len == 1) return (void)(X[1] = 1); n   if (len & 1) { n     work(len - 1); n     X[len] = 0; n     for (long long i = (len); i >= (1); i--) n       (X[i] = X[i - 1] + 1ll * X[i] * (len - 1) % 998244353) %= 998244353; n     return; n   } n   long long n = len >> 1; n   work(n); n   for (long long i = n, w = 1; i >= 0; i--, w = 1ll * w * n % 998244353) n     a[i] = 1ll * w * _fac[n - i] % 998244353; n   for (long long i = (0); i <= (n); i++) b[i] = fac[i] * X[i] % 998244353; n   for (long long i = (n + 1); i <= (n << 2); i++) a[i] = b[i] = 0; n   NTT(a, b, n + 1); n   for (long long i = (0); i <= (n); i++) n     a[i] = 1ll * a[i + n] * _fac[i] % 998244353; n   for (long long i = (n + 1); i <= (limit); i++) a[i] = 0; n   NTT(X, a, n + 1); n } n long long n, A, B; n signed main() { n   file(); n   init(); n   read(n, A, B); n   if (n == 1) return puts(A == B && A == 1 ?  1  :  0 ), 0; n   work(n - 1); n   cout << 1ll * X[A + B - 2] * fac[A + B - 2] % 998244353 * _fac[A - 1] % n               998244353 * _fac[B - 1] % 998244353; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, temp; n   cin >> n; n   map<int, int> a, b; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     a[temp]++; n   } n   for (int i = 0; i < n; i++) { n     cin >> temp; n     b[temp]++; n   } n   int ans = 0, sum = 0; n   vector<int> bal(6); n   for (int i = 1; i < 6; i++) { n     if ((a[i] + b[i]) % 2) { n       cout << -1; n       return 0; n     } n     bal[i] = a[i] - b[i]; n     bal[i] /= 2; n     sum += bal[i]; n     if (bal[i] > 0) ans += bal[i]; n   } n   if (sum) { n     cout << -1; n     return 0; n   } n   cout << abs(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1010; n char str[MAXN][MAXN]; n bool used[MAXN][MAXN]; n int n, m; n int Move[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; n int pp[4]; n void gao(int id, int dis[][MAXN]) { n   queue<pair<int, int> > q; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (str[i][j] == ( 0  + id)) { n         dis[i][j] = 0; n         q.push(make_pair(i, j)); n       } n     } n   } n   while (!q.empty()) { n     pair<int, int> tmp = q.front(); n     q.pop(); n     char ss = str[tmp.first][tmp.second]; n     if (ss >=  1  && ss <=  3 ) { n       if (pp[ss -  0 ] == -1) pp[ss -  0 ] = dis[tmp.first][tmp.second]; n     } n     if (str[tmp.first][tmp.second] != ( 0  + id) && n         str[tmp.first][tmp.second] !=  . ) n       continue; n     for (int i = 0; i < 4; i++) { n       int x = tmp.first + Move[i][0]; n       int y = tmp.second + Move[i][1]; n       if (x < 0 || x >= n || y < 0 || y >= m) continue; n       if (dis[x][y] != -1) continue; n       if (str[x][y] ==  # ) continue; n       dis[x][y] = dis[tmp.first][tmp.second] + 1; n       q.push(make_pair(x, y)); n     } n   } n } n int dp1[MAXN][MAXN], dp2[MAXN][MAXN], dp3[MAXN][MAXN]; n void Update(int &a, int b) { n   if (b == -1) return; n   if (a == -1) n     a = b; n   else n     a = min(a, b); n } n int main() { n   while (scanf( %d%d , &n, &m) == 2) { n     int ans = -1; n     for (int i = 0; i < n; i++) scanf( %s , str[i]); n     memset(pp, -1, sizeof(pp)); n     memset(dp1, -1, sizeof(dp1)); n     gao(1, dp1); n     if (pp[1] != -1 && pp[2] != -1 && pp[3] != -1) n       Update(ans, pp[1] + pp[2] + pp[3] - 2); n     memset(pp, -1, sizeof(pp)); n     memset(dp2, -1, sizeof(dp2)); n     gao(2, dp2); n     if (pp[1] != -1 && pp[2] != -1 && pp[3] != -1) n       Update(ans, pp[1] + pp[2] + pp[3] - 2); n     memset(pp, -1, sizeof(pp)); n     memset(dp3, -1, sizeof(dp3)); n     gao(3, dp3); n     if (pp[1] != -1 && pp[2] != -1 && pp[3] != -1) n       Update(ans, pp[1] + pp[2] + pp[3] - 2); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) { n         if (dp1[i][j] == -1) continue; n         if (dp2[i][j] == -1) continue; n         if (dp3[i][j] == -1) continue; n         Update(ans, max(0, dp1[i][j] - 1) + max(0, dp2[i][j] - 1) + n                         max(0, dp3[i][j] - 1) + (str[i][j] ==  . )); n       } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   vector<long long> mult(32, 1); n   for (int i = 0; i < 32; i++) mult[i] = (1 << i) + 1; n   int t; n   cin >> t; n   while (t--) { n     long long d, m, final = 1; n     cin >> d >> m; n     d++; n     int x = logb(d); n     for (int i = 0; i < x; i++) { n       final *= (mult[i]) % m; n       final %= m; n     } n     final *= (d - ((1 << (x))) + 1) % m; n     cout << (final % m - 1 + m) % m << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T[1000010][30], cnt = 0, fa[1000010]; n char s[400010]; n struct Q { n   int id, x; n }; n int A[400010], val[400010]; n vector<Q> g[1000010]; n vector<int> gg[1000010]; n queue<int> q; n void insert(int id) { n   int f = 0, i, v; n   for (i = 0; s[i]; i++) { n     v = s[i] -  a ; n     if (T[f][v] == 0) T[f][v] = ++cnt; n     f = T[f][v]; n   } n   A[id] = f; n } n void build() { n   int i, u, v, p; n   for (i = 0; i < 27; i++) n     if (T[0][i]) q.push(T[0][i]); n   for (; q.size(); q.pop()) { n     u = q.front(); n     gg[fa[u]].push_back(u); n     for (i = 0; i < 27; i++) { n       v = T[u][i]; n       if (v) { n         p = fa[u]; n         while (p && T[p][i] == 0) p = fa[p]; n         fa[v] = T[p][i]; n         q.push(v); n       } n     } n   } n } n int in[1000010], out[1000010], dfn = 0; n void dfs(int u) { n   int i, v; n   in[u] = ++dfn; n   for (i = 0; i < gg[u].size(); i++) { n     v = gg[u][i]; n     dfs(v); n   } n   out[u] = dfn; n } n int sum[1000100], ans[400010]; n int lb(int x) { return x & -x; } n void add(int x, int v) { n   for (; x <= 1e6; x += lb(x)) sum[x] += v; n } n int qsum(int x) { n   int k = 0; n   for (; x; x -= lb(x)) k += sum[x]; n   return k; n } n void dfs1(int u) { n   int i, v; n   add(in[u], 1); n   Q x; n   for (i = 0; i < g[u].size(); i++) { n     x = g[u][i]; n     ans[x.id] = qsum(out[x.x]) - qsum(in[x.x] - 1); n   } n   for (i = 0; i < 27; i++) n     if (T[u][i]) dfs1(T[u][i]); n   add(in[u], -1); n } n int main() { n   int i, n, k, v, u, m, j; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &k); n     if (k == 1) { n       scanf( %s , s); n       v = s[0] -  a ; n       if (T[0][v] == 0) T[0][v] = ++cnt; n       val[i] = T[0][v]; n     } else { n       scanf( %d%s , &k, s); n       v = s[0] -  a ; n       u = val[k]; n       if (T[u][v] == 0) T[u][v] = ++cnt; n       val[i] = T[u][v]; n     } n   } n   scanf( %d , &m); n   for (i = 1; i <= m; i++) { n     scanf( %d%s , &k, s); n     insert(i); n     g[val[k]].push_back(Q({i, A[i]})); n   } n   build(); n   dfs(0); n   dfs1(0); n   for (i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum; n int cV = 0; n void add(string n, int c) { n   if (n.size() == 1) { n     cV = c; n     return; n   } n   sum = 0; n   for (char e : n) { n     int a = e -  0 ; n     sum += a; n   } n   c++; n   add(to_string(sum), c); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   string n; n   cin >> n; n   add(n, 0); n   cout << cV <<   n ; 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 int64_t binom2(int64_t N) { n   if (N < 2) return 0; n   return (N * (N - 1)) / 2; n } n struct Counter { n   struct Node { n     vector<int> y; n     unique_ptr<Node> left, right; n     Node(const vector<int>& p, int node_left, int node_right) n         : y(p.begin() + node_left, p.begin() + node_right) { n       sort((y).begin(), (y).end()); n       if (y.size() >= 2) { n         int node_middle = (node_left + node_right) / 2; n         left.reset(new Node(p, node_left, node_middle)); n         right.reset(new Node(p, node_middle, node_right)); n       } n     } n   }; n   unique_ptr<Node> root; n   void init(const vector<int>& p) { root.reset(new Node(p, 0, p.size())); } n   int go_count(const Node* node, int node_left, int node_right, int l, int r, n                int d, int u) { n     if (node_left >= r || node_right <= l) return 0; n     if (l <= node_left && node_right <= r) n       return upper_bound((node->y).begin(), (node->y).end(), u) - n              lower_bound((node->y).begin(), (node->y).end(), d); n     int node_middle = (node_left + node_right) / 2; n     return go_count(node->left.get(), node_left, node_middle, l, r, d, u) + n            go_count(node->right.get(), node_middle, node_right, l, r, d, u); n   } n   int count(int l, int r, int d, int u) { n     return go_count(root.get(), 0, root->y.size(), l, r + 1, d, u); n   } n }; n int N, Q; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> N >> Q; n   vector<int> p(N); n   for (int i = 0; i < (N); i++) { n     cin >> p[i]; n     p[i]--; n   } n   Counter counter; n   counter.init(p); n   for (int i = 0; i < (Q); i++) { n     int l, d, r, u; n     cin >> l >> d >> r >> u; n     l--, d--, r--, u--; n     int64_t ans = binom2(N) - binom2(l) - binom2(N - r - 1); n     ans -= binom2(counter.count(0, N - 1, u + 1, N - 1)); n     ans -= binom2(counter.count(0, N - 1, 0, d - 1)); n     ans += binom2(counter.count(0, l - 1, 0, d - 1)); n     ans += binom2(counter.count(0, l - 1, u + 1, N - 1)); n     ans += binom2(counter.count(r + 1, N - 1, 0, d - 1)); n     ans += binom2(counter.count(r + 1, N - 1, u + 1, N - 1)); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int INF = 2147483647; n int inf = -2147483648; n const double PI = acos(-1.0); n const double pi = 2 * asin(1.0); n int vis[1000009]; n int vi[1000009]; n int main() { n   ios::sync_with_stdio(false); n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   for (int i = 0; i <= 100; i++) { n     vis[b + a * i] = 1; n   } n   for (int i = 0; i <= 100; i++) { n     vi[d + c * i] = 1; n   } n   for (int i = 0; i < 1000009; i++) { n     if (vis[i] == 1 && vi[i] == 1) { n       cout << i << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n clock_t start; n void Time(bool timeIt) { n   if (!timeIt) return; n   clock_t end = clock(); n   double elapsed_time = n       ((long double)end - (long double)start) / (long double)CLOCKS_PER_SEC; n   fprintf(stderr,  Time elapsed = %0.4lf n , elapsed_time); n } n const int SIZE = 200010; n const int ALPHABET = 26; n char a[SIZE], b[SIZE]; n vector<pair<long long, long long> > ps[ALPHABET][2]; n int main() { n   start = clock(); n   int n; n   scanf( %d %s %s , &n, a, b); n   long long numerator = 0, denominator = 0; n   for (long long i = 1; i <= n; ++i) { n     denominator += 1ll * (n - i + 1) * (n - i + 1); n   } n   for (int i = 0; i < n; ++i) { n     int ch = a[i] -  A ; n     if (ps[ch][0].empty()) { n       ps[ch][0].push_back(make_pair(i + 1, i)); n       ps[ch][1].push_back(make_pair(n - i, i)); n     } else { n       ps[ch][0].push_back(make_pair(ps[ch][0].back().first + i + 1, i)); n       ps[ch][1].push_back(make_pair(ps[ch][1].back().first + n - i, i)); n     } n   } n   long double ans = 0; n   for (int i = 0; i < n; ++i) { n     int ch = b[i] -  A ; n     int e = ps[ch][0].size(), s = -1; n     while (e - s > 1) { n       int m = (e + s) >> 1; n       if (ps[ch][0][m].second <= i) n         s = m; n       else n         e = m; n     } n     if (s != -1) n       ans += (long double)(1ll * ps[ch][0][s].first * (n - i)) / n              (long double)denominator; n     e = ps[ch][1].size(); n     s = -1; n     while (e - s > 1) { n       int m = (e + s) >> 1; n       if (ps[ch][1][m].second > i) n         e = m; n       else n         s = m; n     } n     if (e != ps[ch][1].size()) { n       long long sum = n           (ps[ch][1].back().first - (s != -1 ? ps[ch][1][s].first : 0)); n       ans += (long double)(1ll * sum * (i + 1)) / (long double)denominator; n     } n   } n   printf( %0.10Lf n , ans); n   Time(true); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n #pragma GCC target( avx,avx2 ) n using namespace std; n long long mod = 1e9 + 7; n const long long inf = 1e9; n int main() { n   cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0); n   int n, l, x, y; n   cin >> n >> l >> x >> y; n   vector<int> a(n); n   set<int> ax, ay; n   map<int, int> have; n   bool havex = 0, havey = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     have[a[i]] = 1; n   } n   for (int i = 0; i < n; ++i) { n     if (a[i] + x < l && !have[a[i] + x]) ax.insert(a[i] + x); n     if (a[i] + y < l && !have[a[i] + y]) ay.insert(a[i] + y); n     if (a[i] - x > 0 && !have[a[i] - x]) ax.insert(a[i] - x); n     if (a[i] - y > 0 && !have[a[i] - y]) ay.insert(a[i] - y); n   } n   for (int i = 0; i < n; ++i) { n     if (have[a[i] - x] || have[a[i] + x]) havex = 1; n     if (have[a[i] - y] || have[a[i] + y]) havey = 1; n   } n   vector<int> ans; n   if (havex && havey) { n     cout << 0; n     exit(0); n   } n   if (havex) { n     cout <<  1 n  << (*ay.begin()); n     exit(0); n   } n   if (havey) { n     cout <<  1 n  << (*ax.begin()); n     exit(0); n   } n   for (int val : ax) { n     if (ay.count(val)) { n       cout <<  1 n  << val; n       exit(0); n     } n   } n   cout <<  2 n  << (*ax.begin()) <<     << (*ay.begin()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int main() { n   int n; n   scanf( %d , &n); n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     while (1) { n       if (mp[a] == 0 || a == 0) { n         mp[a] = 1; n         sum += (long long)a; n         break; n       } else n         a--; n     } n   } n   printf( %I64d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, c, a, b; n   cin >> a >> b >> c; n   c -= 1; n   while (c) { n     c--; n     i = a + b; n     a = b; n     b = i; n   } n   cout << b << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int p[100010]; n bool used[100010]; n int main() { n   scanf( %d , &n); n   memset(p, 0, sizeof(p)); n   memset(used, false, sizeof(used)); n   if (n == 1) { n     printf( 1 n ); n     return 0; n   } n   if (n % 2 == 0) { n     if (n % 4 != 0) { n       printf( -1 n ); n       return 0; n     } n   } else { n     if ((n - 1) % 4 != 0) { n       printf( -1 n ); n       return 0; n     } n   } n   if (n % 2 != 0) { n     p[n / 2 + 1] = n / 2 + 1; n     used[n / 2 + 1] = true; n   } n   for (int i = 1; i <= n / 2; i++) { n     if (!p[i]) { n       int num = 1; n       while (used[num] || num == i) num++; n       p[i] = num; n       used[num] = true; n       p[num] = n + 1 - i; n       used[n + 1 - i] = true; n       p[n + 1 - i] = n + 1 - num; n       used[n + 1 - num] = true; n       p[n + 1 - num] = i; n       used[i] = true; n     } n   } n   for (int i = 1; i <= n; i++) printf( %d  , p[i]); 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> v; n   for (int i = 1; i <= 20; ++i) { n     int first = ((1 << i) - 1) * (1 << (i - 1)); n     if (first > 1e5) break; n     v.push_back(first); n   } n   for (int i = v.size() - 1; i >= 0; --i) { n     if (n % v[i] == 0) return cout << v[i], 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; n int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1}; n using namespace std; n long long power(long long a, long long p, long long M) { n   long long r = 1; n   a = a % M; n   while (p > 0) { n     if (p % 2) r = (r * a) % M; n     a = (a * a) % M; n     p /= 2; n   } n   return r; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int n; n long long a[200000]; n vector<long long> v[200000]; n void solve() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   ; n   for (int i = 0; i < n; i++) { n     v[i].push_back(0); n   } n   for (int i = 0; i < n; i++) { n     long long l = 0, r = n - 1; n     while (l <= r) { n       long long mid = (l + r) / 2; n       if (v[mid][v[mid].size() - 1] < a[i]) { n         r = mid - 1; n       } else { n         l = mid + 1; n       } n     } n     v[l].push_back(a[i]); n   } n   for (int i = 0; i < n; i++) { n     if (v[i].size() > 1) { n       for (int j = 1; j < v[i].size(); j++) { n         printf( %lld  , v[i][j]); n       } n       printf(  n ); n     } else { n       break; n     } n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[51][51], ans[51]; n bool check(int j, int x) { n   for (int i = 1; i <= n; i++) n     if (a[j][i] > x || a[i][j] > x) return false; n   ans[j] = x; n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &a[i][j]); n   for (int i = 1; i <= n; i++) { n     int j = 1; n     while (ans[j] || !check(j, i)) j++; n   } n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   if (s.length() < 26) { n     cout << -1 << endl; n     return 0; n   } n   int j = -1; n   for (int i = 0; i < s.length(); i++) n     if (s[i] ==  a ) { n       j = i + 1; n       break; n     } n   if (j == -1) { n     cout << -1 << endl; n     return 0; n   } n   for (char c =  b ; c <=  z ; c++) { n     bool f = 0; n     for (int i = j; i < s.length(); i++) { n       if (s[i] <= (char)(c - 1)) { n         s[i] = c, j = i + 1, f = 1; n         break; n       } n       if (s[i] == (char)(c)) { n         j = i + 1, f = 1; n         break; n       } n     } n     if (f == 0) { n       cout << -1 << endl; n       return 0; n     } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int n; n long long a[maxn], l[maxn], r[maxn], lans[maxn], rans[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   for (int i = 1; i <= n; i++) { n     l[i] = max(a[i], l[i - 1] + 1); n     lans[i] = l[i] - a[i] + lans[i - 1]; n   } n   for (int i = n; i >= 0; i--) { n     r[i] = max(a[i], r[i + 1] + 1); n     rans[i] = r[i] - a[i] + rans[i + 1]; n   } n   long long ans = 1e18; n   for (int i = 1; i <= n; i++) n     ans = n         min(ans, lans[i - 1] + rans[i + 1] + max(l[i], max(r[i], a[i])) - a[i]); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, INF = 2e9; n int n, m, k, p[N][N], rnk[N][N], le[N][N], ri[N][N]; n bool used[N][N], iswap; n pair<int, pair<int, int> > ans; n int dsu_get(int x, int y) { n   return p[x][y] == y ? y : p[x][y] = dsu_get(x, p[x][y]); n } n inline void dsu_unite(int x, int y1, int y2) { n   int px = dsu_get(x, y1), py = dsu_get(x, y2); n   if (px != py) { n     if (rnk[x][px] < rnk[x][py]) { n       swap(px, py); n     } n     if (rnk[x][px] == rnk[x][py]) { n       rnk[x][px]++; n     } n     p[x][py] = px; n     le[x][px] = min(le[x][px], le[x][py]); n     ri[x][px] = max(ri[x][px], ri[x][py]); n   } n } n inline void upd(pair<int, pair<int, int> > val) { n   if (iswap) { n     if (val.first < ans.first) { n       ans = val; n     } else if (val.first == ans.first) { n       auto it1 = ans.second, it2 = val.second; n       swap(it1.first, it1.second); n       swap(it2.first, it2.second); n       if (it1 > it2) { n         ans = val; n       } n     } n   } else { n     ans = min(ans, val); n   } n } n inline pair<int, int> getans(int x, int y) { n   ans = make_pair(INF, make_pair(-1, -1)); n   for (int i = 0; i <= ans.first; i++) { n     if (x - i >= 1) { n       int py = dsu_get(x - i, y); n       int ll = le[x - i][py], rr = ri[x - i][py]; n       if (used[x - i][ll]) { n         ll--; n       } n       if (used[x - i][rr]) { n         rr++; n       } n       if (ll >= 1) { n         upd(make_pair(i + y - ll, make_pair(x - i, ll))); n       } n       if (rr <= m) { n         upd(make_pair(i + rr - y, make_pair(x - i, rr))); n       } n     } n     if (x + i <= n) { n       int py = dsu_get(x + i, y); n       int ll = le[x + i][py], rr = ri[x + i][py]; n       if (used[x + i][ll]) { n         ll--; n       } n       if (used[x + i][rr]) { n         rr++; n       } n       if (ll >= 1) { n         upd(make_pair(i + y - ll, make_pair(x + i, ll))); n       } n       if (rr <= m) { n         upd(make_pair(i + rr - y, make_pair(x + i, rr))); n       } n     } n   } n   return ans.second; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   if (n > m) { n     iswap = true; n     swap(n, m); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       le[i][j] = ri[i][j] = p[i][j] = j; n     } n   } n   while (k--) { n     int x, y; n     cin >> x >> y; n     if (iswap) { n       swap(x, y); n     } n     auto it = getans(x, y); n     used[it.first][it.second] = true; n     if (it.second - 1 >= 1 && used[it.first][it.second - 1]) { n       dsu_unite(it.first, it.second - 1, it.second); n     } n     if (it.second + 1 <= m && used[it.first][it.second + 1]) { n       dsu_unite(it.first, it.second, it.second + 1); n     } n     if (iswap) { n       swap(it.first, it.second); n     } n     cout << it.first <<     << it.second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 998244353; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   vector<int> a(n), c(m, -1), d(m), bb(m); n   map<int, int> b; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int j = 0; j < m; j++) { n     int q; n     cin >> q; n     b.insert(make_pair(q, j)); n     bb[j] = q; n   } n   for (int k = 0; k < n; k++) { n     if (b.find(a[k]) != b.end()) { n       c[b.find(a[k])->second] = k; n     } n   } n   if (c[0] == -1) { n     cout << 0; n     return 0; n   } n   for (int l = 1; l < m; l++) { n     if ((c[l] < c[l - 1]) || (c[l - 1] == -1)) { n       cout << 0; n       return 0; n     } n   } n   int s = 0, t = 0; n   d[0] = c[0]; n   while (s <= c[t]) { n     if (t >= 1) { n       if (a[s] < bb[t - 1]) { n         cout << 0; n         return 0; n       } n     } n     if (a[s] < bb[t]) d[t] = s; n     if (s == c[t]) { n       t++; n       if (t >= m) break; n       d[t] = c[t - 1]; n     } n     s++; n   } n   if (d[0] < c[0]) { n     cout << 0; n     return 0; n   } n   for (int e = c[m - 1]; e < n; e++) { n     if (a[e] < bb[m - 1]) { n       cout << 0; n       return 0; n     }; n   } n   if (m == 1) { n     cout << 1; n     return 0; n   } n   long long int sum = 1; n   for (int w = 1; w < m; w++) { n     sum = (sum * (c[w] - d[w])) % P; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::deque; n using std::endl; n using std::map; n using std::max; n using std::min; n using std::multiset; n using std::pair; n using std::queue; n using std::set; n using std::sort; n using std::stack; n using std::string; n using std::vector; n int n, k, s, t; n int c[200005] = { n     0, n }; n int v[200005] = { n     0, n }; n int g[200005] = { n     0, n }; n bool isGood(int middle) { n   int time = 0; n   for (int i = 1; i <= k + 1; i++) { n     int len = g[i] - g[i - 1]; n     if (len > middle) { n       return false; n     } n     if (middle >= len * 2) { n       time += len; n     } else { n       time += (len * 2 - middle) * 2 + (middle - len); n     } n   } n   if (time <= t) return true; n   return false; n } n int main() { n   cin >> n >> k >> s >> t; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , c + i, v + i); n   } n   g[0] = 0; n   g[k + 1] = s; n   for (int i = 1; i <= k; i++) { n     scanf( %d , g + i); n   } n   sort(g, g + k + 2); n   const int up = 2123456789; n   int badBak = 0; n   int goodBak = up; n   while (badBak + 1 != goodBak) { n     int middle = (int)((1LL * badBak + goodBak) / 2); n     if (isGood(middle)) { n       goodBak = middle; n     } else { n       badBak = middle; n     } n   } n   if (goodBak == up) { n     cout << -1; n     return 0; n   } n   int cheap = up; n   for (int i = 0; i < n; i++) { n     if (v[i] >= goodBak && c[i] < cheap) { n       cheap = c[i]; n     } n   } n   if (cheap == up) { n     cout << -1; n     return 0; n   } n   cout << cheap; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, n, p, q, a; n   cin >> t; n   while (t--) { n     cin >> n >> p >> q; n     vector<long long int> player1, player2; n     for (unsigned long long i = 0; i < p; i++) { n       cin >> a; n       player1.push_back(a); n     } n     for (unsigned long long i = 0; i < q; i++) { n       cin >> a; n       player2.push_back(a); n     } n     sort(player1.begin(), player1.end(), greater<long long int>()); n     sort(player2.begin(), player2.end(), greater<long long int>()); n     if (player1[0] > player2[0]) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-6; n int N, M, Q; n vector<int> V[100002], W[100002]; n int X[100002], Y[100002], T[100002], weight[100002]; n int A[100002]; n bool S[100002]; n int xglob, yglob; n inline bool compare(const int& i1, const int& i2) { n   return atan2(Y[i1] - yglob, X[i1] - xglob) < n          atan2(Y[i2] - yglob, X[i2] - xglob); n } n void Dfs(int x) { n   S[x] = true; n   weight[x] = 1; n   W[x].resize(V[x].size()); n   for (int i = 0; i < int(V[x].size()); ++i) n     if (!S[V[x][i]]) { n       T[V[x][i]] = x; n       Dfs(V[x][i]); n       weight[x] += weight[V[x][i]]; n       W[x][i] = -weight[V[x][i]]; n     } n   for (int i = 0; i < int(V[x].size()); ++i) n     if (V[x][i] == T[x]) W[x][i] = weight[x]; n   for (int i = 1; i < int(W[x].size()); ++i) W[x][i] += W[x][i - 1]; n } n int main() { n   cin >> N >> M; n   for (int i = 1, nod1, nod2; i <= M; ++i) { n     cin >> nod1 >> nod2; n     V[nod1].push_back(nod2); n     V[nod2].push_back(nod1); n   } n   int minx = -1; n   for (int i = 1; i <= N; ++i) { n     cin >> X[i] >> Y[i]; n     if (minx == -1 || X[i] < X[minx]) minx = i; n   } n   X[N + 1] = -0x3f3f3f3f, Y[N + 1] = -0x3f3f3f3f; n   V[minx].push_back(N + 1); n   V[N + 1].push_back(minx); n   for (int i = 1; i <= N; ++i) { n     xglob = X[i], yglob = Y[i]; n     sort(V[i].begin(), V[i].end(), compare); n   } n   Dfs(N + 1); n   cin >> Q; n   for (int i = 1; i <= Q; ++i) { n     cin >> A[0]; n     long long area = 0; n     for (int j = 1; j <= A[0]; ++j) { n       cin >> A[j]; n       if (j >= 2) n         area += 1LL * X[A[j - 1]] * Y[A[j]] - 1LL * X[A[j]] * Y[A[j - 1]]; n     } n     area += 1LL * X[A[A[0]]] * Y[A[1]] - 1LL * X[A[1]] * Y[A[A[0]]]; n     if (area < 0) reverse(A + 1, A + A[0] + 1); n     A[A[0] + 1] = A[1]; n     ++A[0]; n     A[A[0] + 1] = A[2]; n     ++A[0]; n     int result = 0; n     for (int j = 2; j <= A[0] - 1; ++j) { n       int step = 1 << 16, nod1, nod2; n       for (nod1 = -1; step; step >>= 1) n         if (nod1 + step < int(V[A[j]].size()) && n             atan2(Y[V[A[j]][nod1 + step]] - Y[A[j]], n                   X[V[A[j]][nod1 + step]] - X[A[j]]) - n                     atan2(Y[A[j - 1]] - Y[A[j]], X[A[j - 1]] - X[A[j]]) < n                 eps) n           nod1 += step; n       step = 1 << 16; n       for (nod2 = -1; step; step >>= 1) n         if (nod2 + step < int(V[A[j]].size()) && n             atan2(Y[V[A[j]][nod2 + step]] - Y[A[j]], n                   X[V[A[j]][nod2 + step]] - X[A[j]]) - n                     atan2(Y[A[j + 1]] - Y[A[j]], X[A[j + 1]] - X[A[j]]) < n                 eps) n           nod2 += step; n       if (nod1 < nod2) n         result += W[A[j]][nod2 - 1] - W[A[j]][nod1]; n       else n         result += W[A[j]][W[A[j]].size() - 1] - n                   (W[A[j]][nod1] - (nod2 == 0 ? 0 : W[A[j]][nod2 - 1])); n     } n     cout << result <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int n; n   cin >> n; n   vector<long long int> a, d; n   for (long long int i = 0; i < n; i++) { n     long long int temp; n     cin >> temp; n     a.push_back(temp); n   } n   for (long long int i = 0; i < n; i++) { n     long long int temp; n     cin >> temp; n     d.push_back(a[i] - temp); n   } n   sort(d.begin(), d.end()); n   long long int i = 0, j = n - 1, s = 0; n   while (i != j) { n     if (d[i] + d[j] > 0) { n       j--; n     } else { n       i++; n       s += n - j - 1; n     } n   } n   s += (n - j - 1) * (n - j) / 2; n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string n; n   cin >> n; n   int ans = 0; n   while (n.size() > 1) { n     int k = 0; n     for (int i = 0; i < n.size(); i++) { n       k += n[i] - 48; n     } n     ans++; n     stringstream ss; n     ss << k; n     string str = ss.str(); n     n = str; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T, a, b, c, i, count = 0; n   cin >> T; n   for (i = 0; i < T; i++) { n     cin >> a >> b >> c; n     if ((a + b + c) >= 2) { n       count++; n     } n   } n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n namespace fft { n using namespace std; n const double PI = std::atan2(0, -1); n using dbl = double; n struct num { n   dbl x, y; n   num(dbl x_ = 0, dbl y_ = 0) : x(x_), y(y_) {} n }; n inline num operator+(num a, num b) { return num(a.x + b.x, a.y + b.y); } n inline num operator-(num a, num b) { return num(a.x - b.x, a.y - b.y); } n inline num operator*(num a, num b) { n   return num(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); n } n inline num conj(num a) { return num(a.x, -a.y); } n inline num inv(num a) { n   dbl n = (a.x * a.x + a.y * a.y); n   return num(a.x / n, -a.y / n); n } n using vn = vector<num>; n vector<int> rev({0, 1}); n vn rt(2, num(1)), fa, fb; n inline void init(int n) { n   if (n <= (int)(rt).size()) return; n   rev.resize(n); n   for (int i = 0; i < (n); ++i) rev[i] = (rev[i >> 1] | ((i & 1) * n)) >> 1; n   rt.reserve(n); n   for (int k = (int)(rt).size(); k < n; k *= 2) { n     rt.resize(2 * k); n     double a = PI / k; n     num z(cos(a), sin(a)); n     for (int i = k / 2; i < (k); ++i) n       rt[2 * i] = rt[i], rt[2 * i + 1] = rt[i] * z; n   } n } n inline void fft(vector<num>& a, int n) { n   init(n); n   int s = __builtin_ctz((int)(rev).size() / n); n   for (int i = 0; i < (n); ++i) n     if (i < rev[i] >> s) swap(a[i], a[rev[i] >> s]); n   for (int k = 1; k < n; k *= 2) n     for (int i = 0; i < n; i += 2 * k) n       for (int j = 0; j < (k); ++j) { n         num t = rt[j + k] * a[i + j + k]; n         a[i + j + k] = a[i + j] - t; n         a[i + j] = a[i + j] + t; n       } n } n using vd = vector<double>; n vd multiply(const vd& a, const vd& b) { n   int s = (int)(a).size() + (int)(b).size() - 1; n   if (s <= 0) return {}; n   int L = s > 1 ? 32 - __builtin_clz(s - 1) : 0, n = 1 << L; n   if ((int)(fa).size() < n) fa.resize(n); n   if ((int)(fb).size() < n) fb.resize(n); n   fill(fa.begin(), fa.begin() + n, 0); n   for (int i = 0; i < ((int)(a).size()); ++i) fa[i].x = a[i]; n   for (int i = 0; i < ((int)(b).size()); ++i) fa[i].y = b[i]; n   fft(fa, n); n   for (auto& x : fa) x = x * x; n   for (int i = 0; i < (n); ++i) fb[i] = fa[(n - i) & (n - 1)] - conj(fa[i]); n   fft(fb, n); n   vd r(s); n   for (int i = 0; i < (s); ++i) r[i] = fb[i].y / (4 * n); n   return r; n } n }  // namespace fft n int main() { n   using namespace std; n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   array<int, 2> N; n   int Q; n   cin >> N[0] >> N[1] >> Q; n   array<vector<int>, 2> A; n   for (int z = 0; z < 2; z++) { n     A[z].resize(N[z]); n     for (int& v : A[z]) cin >> v; n   } n   vector<int> queries(Q); n   for (auto& q : queries) cin >> q; n   const int X = int(1e5) + 100; n   array<vector<double>, 2> mults; n   for (int z = 0; z < 2; z++) { n     mults[z].assign(X, 0); n     for (int i = 0; i + 1 < N[z]; i++) { n       mults[z][min(A[z][i], A[z][i + 1])]--; n       mults[z][max(A[z][i], A[z][i + 1])]++; n     } n   } n   vector<long long> ans(2 * X - 1); n   vector<double> ans_double = fft::multiply(mults[0], mults[1]); n   for (int i = 0; i < 2 * X - 1; i++) { n     ans[i] = ((ans_double[i] > 0) - (ans_double[i] < 0)) * n              (long long)(abs(ans_double[i]) + 0.5); n   } n   for (int a : {A[0].front(), A[0].back()}) { n     for (int b : {A[1].front(), A[1].back()}) { n       ans[a + b]++; n     } n   } n   for (int i = 2 * X - 3; i >= 0; i--) { n     ans[i] += ans[i + 1]; n   } n   long long tot = ans[0]; n   for (auto& a : ans) { n     a = 2 * a - tot; n     a /= 4; n   } n   for (int q : queries) { n     cout << ans[q] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n struct Node { n   int a[3], idx; n   bool operator<(const Node& ano) const { n     if (a[1] == ano.a[1]) { n       if (a[2] == ano.a[2]) return a[0] < ano.a[0]; n       return a[2] < ano.a[2]; n     } n     return a[1] < ano.a[1]; n   } n } t[N]; n int main() { n   int n; n   cin >> n; n   double ans1 = 0; n   int k1 = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 3; j++) scanf( %d , &t[i].a[j]); n     t[i].idx = i; n     sort(t[i].a, t[i].a + 3); n     if (ans1 < 0.5 * t[i].a[0]) { n       ans1 = 0.5 * t[i].a[0]; n       k1 = i; n     } n   } n   sort(t + 1, t + 1 + n); n   double ans2 = 0; n   int k21 = 0, k22 = 0; n   for (int i = 2; i <= n; i++) { n     if (i != n && t[i].a[0] == t[i + 1].a[0] && t[i].a[2] == t[i + 1].a[2] && n         t[i].a[1] == t[i + 1].a[1]) { n       continue; n     } n     if (t[i].a[2] == t[i - 1].a[2] && t[i].a[1] == t[i - 1].a[1]) { n       int nlen = t[i].a[0] + t[i - 1].a[0]; n       int minlen = min(t[i].a[1], min(t[i].a[2], nlen)); n       if (ans2 < 0.5 * minlen) { n         ans2 = 0.5 * minlen; n         k21 = t[i - 1].idx, k22 = t[i].idx; n       } n     } n   } n   if (ans1 >= ans2) n     cout <<  1 n  << k1 << endl; n   else n     cout <<  2 n  << k21 <<     << k22 << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:65777216 ) n using namespace std; n const int N = 2222; n const int INF = 111111111; n const double eps = 1e-9; n const double pi = 3.14159265358979; n char s[N], u[N]; n int n, m, ans = INF; n inline int check(int pos) { n   int res = 0; n   for (int i = 0; i < (m); i++) { n     if (pos + i < 0 || pos + i >= n) continue; n     if (s[pos + i] == u[i]) ++res; n   } n   return res; n } n int main() { n   gets(s); n   gets(u); n   n = strlen(s); n   m = strlen(u); n   for (int i = -m; i <= m + n; i++) ans = min(ans, m - check(i)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k = 0; n   int ladder = 0; n   string slogan; n   cin >> k >> ladder; n   cin >> slogan; n   int h = k / 2; n   if (ladder != 1 && ladder != k) { n     if (ladder > h) { n       while (ladder != k) { n         cout <<  RIGHT  << endl; n         ladder++; n       } n     } else if (ladder <= h) { n       while (ladder != 1) { n         cout <<  LEFT  << endl; n         ladder--; n       } n     } n   } n   string toPrint = ladder == 1 ?  RIGHT  :  LEFT ; n   if (toPrint ==  LEFT ) { n     string reversed =   ; n     for (int i = k - 1; i >= 0; i--) { n       reversed += slogan.at(i); n     } n     slogan = reversed; n   } n   for (int i = 0; i < k; i++) { n     if (i != 0) { n       cout << toPrint << endl; n     } n     cout <<  PRINT   << slogan.at(i) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int md = 1000000007; n void add(int &a, int b) { n   a += b; n   if (a >= md) a -= md; n } n int f[155][155][2][2], oldf[155][155][2][2], trueoldf[155][155][2][2]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   memset(f, 0, sizeof(f)); n   for (int a = 1; a <= m; a++) n     for (int b = a; b <= m; b++) f[a][b][1][1] = 1; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     long long cnt = 0; n     for (int a = 1; a <= m; a++) n       for (int b = a; b <= m; b++) n         for (int ka = 0; ka <= 1; ka++) n           for (int kb = 0; kb <= 1; kb++) cnt += f[a][b][ka][kb]; n     ans = (ans + cnt * (n - i + 1)) % md; n     for (int a = 1; a <= m; a++) n       for (int b = a + 1; b <= m; b++) n         for (int kb = 0; kb <= 1; kb++) { n           add(f[a + 1][b][0][kb], f[a][b][0][kb]); n           add(f[a + 1][b][0][kb], f[a][b][1][kb]); n         } n     for (int a = 1; a <= m; a++) n       for (int b = m; b >= a + 1; b--) n         for (int ka = 0; ka <= 1; ka++) { n           add(f[a][b - 1][ka][0], f[a][b][ka][0]); n           add(f[a][b - 1][ka][0], f[a][b][ka][1]); n         } n     for (int a = m; a >= 2; a--) n       for (int b = a; b <= m; b++) n         for (int kb = 0; kb <= 1; kb++) { n           add(f[a - 1][b][1][kb], f[a][b][1][kb]); n         } n     for (int a = m; a >= 1; a--) n       for (int b = a; b <= m - 1; b++) n         for (int ka = 0; ka <= 1; ka++) { n           add(f[a][b + 1][ka][1], f[a][b][ka][1]); n         } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long v[3], l, ans, minusVal; n long long accSum(long long limit) { n   return (limit + 1LL) * (limit + 2LL) / 2LL; n } n long long invalidCase(long long a, long long b, long long c) { n   long long ret = 0LL; n   for (long long i = 0LL; i <= l; i++) { n     long long val = min(c - a - b + i, l - i); n     if (val >= 0LL) ret += accSum(val); n   } n   return ret; n } n int main() { n   scanf( %lld %lld %lld %lld , &v[0], &v[1], &v[2], &l); n   ans = (l + 1LL) * (l + 2LL) * (l + 3LL) / 6LL; n   ans -= invalidCase(v[2], v[1], v[0]); n   ans -= invalidCase(v[0], v[2], v[1]); n   ans -= invalidCase(v[0], v[1], v[2]); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000 * 1000 * 1000 + 7; n const long long MAXN = 100 * 1000 + 10; n long long t; n long long n, k; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n >> k; n     cout << k * 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fin(string s, int i1) { n   int mi = 999999999; n   for (int d = 1; d <= 9; d++) { n     for (int i = i1; i < i1 + 10; i++) { n       if (i + 2 * d < s.length()) { n         if (s[i] == s[i + d] && s[i] == s[i + 2 * d]) { n           mi = min(mi, i + 2 * d); n         } n       } n     } n   } n   return mi; n } n int main() { n   string s; n   cin >> s; n   long long n = s.length(); n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     int p = fin(s, i); n     if (n > p) { n       ans = ans + n - p; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e5 + 100; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int modexp(long long int x, long long int n, long long int mod) { n   long long int result = 1; n   while (n > 0) { n     if (n % 2 == 1) result = (result % mod * x % mod) % mod; n     x = (x % mod * x % mod) % mod; n   } n   return result; n } n bool vis[N]; n vector<long long int> adj[N]; n long long int mx = 0; n void DFS(long long int s) { n   vis[s] = true; n   for (int i = 0; i < adj[s].size(); i++) n     if (!vis[adj[s][i]]) DFS(adj[s][i]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     long long int ans = n - (n % k); n     long long int x = k / 2; n     ans += min(x, n % k); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9223372036854775807ll; n const int inf = 2147483647, nmax = 1000006; n struct thing { n   long long a, b, c, i; n   bool operator<(const thing &obj) const { return c < obj.c; } n } e[nmax], e2[nmax], apm[nmax]; n struct thing2 { n   long long a, b, c, i, st, dr, mid, ans; n   bool operator<(const thing2 &obj) const { return mid < obj.mid; } n }; n long long n, m, c[nmax], rng[nmax], v[nmax], ans[nmax]; n vector<thing2> r; n long long find(long long x) { n   if (x == c[x]) return x; n   return c[x] = find(c[x]); n } n void unite(long long x, long long y) { n   if (x == y) return; n   if (rng[x] > rng[y]) n     c[y] = x; n   else n     c[x] = y; n   if (rng[x] == rng[y]) ++rng[y]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     cin >> e[i].a >> e[i].b >> e[i].c; n     e[i].i = i; n     e2[i] = e[i]; n     ans[i] = -1; n   } n   sort(e2 + 1, e2 + m + 1); n   for (int i = 1; i <= n; ++i) rng[i] = 1, c[i] = i; n   int p = 0; n   for (int i = 1; i <= m; ++i) n     if (find(e2[i].a) != find(e2[i].b)) { n       apm[++p] = e2[i]; n       unite(find(e2[i].a), find(e2[i].b)); n       v[p] = e2[i].c; n     } else { n       thing2 obj; n       obj.a = e2[i].a; n       obj.b = e2[i].b; n       obj.c = e2[i].c; n       obj.i = e2[i].i; n       obj.st = 1; n       obj.dr = n - 1; n       r.push_back(obj); n     } n   sort(v + 1, v + n); n   for (int t = 1; t <= 32; ++t) { n     for (auto &x : r) x.mid = (x.st + x.dr) / 2; n     sort(r.begin(), r.end()); n     for (int i = 1; i <= n; ++i) rng[i] = 1, c[i] = i; n     unsigned p = 0; n     for (int i = 1; i < n; ++i) { n       unite(find(apm[i].a), find(apm[i].b)); n       while (p < r.size() && v[r[p].mid] <= apm[i].c) { n         if (find(r[p].a) == find(r[p].b)) { n           r[p].dr = r[p].mid - 1; n           r[p].ans = v[r[p].mid]; n         } else n           r[p].st = r[p].mid + 1; n         ++p; n       } n     } n   } n   for (auto x : r) ans[x.i] = x.ans; n   for (int i = 1; i <= m; ++i) n     if (ans[i] != -1) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n struct compF { n   bool operator()(const pair<T, T> &a, const pair<T, T> &b) { n     return a.first < b.first; n   } n }; n template <typename T> n struct compS { n   bool operator()(const pair<T, T> &a, const pair<T, T> &b) { n     return a.second < b.second; n   } n }; n template <typename T> n void printMatrix(const string &txt, vector<vector<T>> &a) { n   cout << txt; n   for (int i = 0; i < a.size(); ++i) { n     for (int j = 0; j < a[i].size(); ++j) cout << a[i][j] <<    ; n     cout << endl; n   } n } n template <typename T> n void printVector(const string &txt, vector<T> &a) { n   cout << txt; n   for (int i = 0; i < a.size(); ++i) cout << a[i] <<    ; n   cout << endl; n } n template <typename T> n void readVector(int n, vector<T> &a) { n   if (a.size() < n) a.resize(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n } n template <typename T1, typename T2> n bool exist(map<T1, T2> &m, T1 x) { n   if (m.find(x) == m.end()) return false; n   return true; n } n long long gcd(long long a, long long b) { n   while (a != 0 && b != 0) { n     if (b > a) n       b %= a; n     else n       a %= b; n   } n   return a + b; n } n long long minFactor(long long x) { n   for (long long i = 2; i * i <= x; ++i) n     if (x % i == 0) return i; n   return x; n } n long long allFactors(long long x, vector<long long> &prms, n                      vector<long long> &prm_cnt) { n   prms.clear(); n   prm_cnt.clear(); n   while (x > 1) { n     long long t = minFactor(x); n     prms.push_back(t); n     int s = prms.size() - 1; n     prm_cnt.push_back(0); n     while (x % t == 0) { n       x /= t; n       ++prm_cnt[s]; n     } n   } n   return 0; n } n long long add(long long x, long long y) { n   if (x >= 998244353) x %= 998244353; n   if (y >= 998244353) y %= 998244353; n   return (x + y) % 998244353; n } n long long mul(long long x, long long y) { n   if (x >= 998244353) x %= 998244353; n   if (y >= 998244353) y %= 998244353; n   return (x * y) % 998244353; n } n long long pow_(long long x, long long y) { n   if (y == 0) return 1ll; n   if (y % 2 == 0) { n     long long k = pow_(x, y / 2); n     return (k * k) % 998244353; n   } else { n     return mul(x, pow_(x, y - 1)); n   } n } n long long n, m; n long long fact[2 * (int)(1e5) + 2], rfact[2 * (int)(1e5) + 2]; n long long C(long long n, long long m) { n   long long num = fact[n]; n   long long den = mul(rfact[m], rfact[n - m]); n   return mul(num, den); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   fact[0] = fact[1] = rfact[0] = rfact[1] = 1ll; n   for (long long i = 2; i <= 2 * (int)(1e5); ++i) { n     fact[i] = mul(fact[i - 1], i); n     rfact[i] = pow_(fact[i], 998244353 - 2); n   } n   cin >> n >> m; n   if (n == 2) { n     cout << 0 << endl; n     return 0; n   } n   long long t = n - 1; n   long long res = 0; n   long long c = 0; n   for (long long i = t; i <= m; ++i) { n     long long cr = C(i - 1, t - 1); n     c = add(c, cr); n   } n   long long t2 = pow_(2, t - 2); n   res = mul(c, mul(max(t2, 1ll), t - 1)); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int isP(long int hj) { n   long int op; n   for (op = 2; op <= sqrt(hj); op++) { n     if (hj % op == 0) return 0; n   } n   return 1; n } n void swap(long long int *p, long long int *q) { n   long long int tmp = *p; n   *p = *q; n   *q = tmp; n } n void tog(int *p) { n   if (*p == 1) n     *p = 0; n   else n     *p = 1; n } n string fdi(int hi) { n   switch (hi) { n     case 0: n       return  zero ; n     case 1: n       return  one ; n     case 2: n       return  two ; n     case 3: n       return  three ; n     case 4: n       return  four ; n     case 5: n       return  five ; n     case 6: n       return  six ; n     case 7: n       return  seven ; n     case 8: n       return  eight ; n     case 9: n       return  nine ; n   } n } n string rdx(int hi) { n   switch (hi) { n     case 2: n       return  twenty ; n     case 3: n       return  thirty ; n     case 4: n       return  forty ; n     case 5: n       return  fifty ; n     case 6: n       return  sixty ; n     case 7: n       return  seventy ; n     case 8: n       return  eighty ; n     case 9: n       return  ninety ; n   } n } n long long int bd(long long int mk, long long int nk) { n   long long int i; n   for (i = min(mk, nk); i >= 1; i--) { n     if (mk % i == 0 && nk % i == 0) return i; n   } n } n int dsm(long long int pkk) { n   if (pkk < 0) pkk *= -1; n   while (pkk > 0) { n     if (pkk % 10 == 8) return 1; n     pkk /= 10; n   } n   return 0; n } n int main() { n   long long int nk, l, nl, np, odI, evI, r, a1, a2, amt, maxu, y3, sx, sy, bsm, n       fsm, bu, cp, bf, pr, res, lastm, c, r1, r2, x1, x2, y1, y2, maxN, m1, m2, n       prev, prc, cost, ind, ans, g, p, inc, cap, sm, d, nxt, mn, t, up, a, b, n       lc, la, e, preI, t2, brk, clbp, clb, et, jmp, u, l1, l2, sq, nd, maxi, li, n       ri, tr, mp, og, lt1, lt2, n1, n2, c1, c2, minI, min1, p2, p3, ze, fst, n       lst, min2, s, sm1, sm2, sm3, sr, sl, ln, rz, rn, car, eq, shr, ng, v, n       curr, spl, fl, z, ev, od, m, ct, j, q, k, maxI, max1, max2, lt, md, f, n, n       xP, xN, w, h, d2, d1, u1, u2, i, x, y; n   cin >> n; n   string st; n   cin >> st; n   a = 0; n   b = 0; n   if (st[0] ==  a ) n     a++; n   else n     b++; n   ct = 0; n   for (i = 1; i < n; i++) { n     if (i % 2 != 0) { n       if (st[i] ==  a ) n         a++; n       else n         b++; n       if (a != b) { n         ct++; n         if (st[i] ==  a ) { n           st[i] =  b ; n           a--; n           b++; n         } else { n           st[i] =  a ; n           a++; n           b--; n         } n       } n     } else { n       if (st[i] ==  a ) n         a++; n       else n         b++; n     } n   } n   cout << ct << endl; n   cout << st; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q; n long long int input; n long long int lastSum[20]; n long long int limit[20]; n long long int targetSum(int digit, long long int target) { n   long long int ret = lastSum[digit - 1] * target; n   ret += target * (target + 1) * digit / 2; n   return ret; n } n long long int targetSubSum(long long int target) { n   if (target == 0) return 0; n   int digit = 0; n   long long int tmp = target, ret = 0, mul = 1; n   while (tmp) digit++, tmp /= 10, mul *= 10; n   ret += lastSum[digit - 1]; n   target -= (mul / 10) - 1; n   ret += target * digit; n   return ret; n } n int main() { n   long long int mul = 9; n   for (int digit = 1; digit < 10; digit++) { n     lastSum[digit] = lastSum[digit - 1] + digit * mul; n     limit[digit] = limit[digit - 1] + lastSum[digit - 1] * mul + n                    digit * mul * (mul + 1) / 2; n     mul *= 10; n   } n   scanf( %d , &q); n   while (q) { n     scanf( %lld , &input); n     int digit = 1; n     long long int low = 0, high = 9, answer = 1; n     while (limit[digit] < input) digit++, high *= 10; n     input -= limit[digit - 1]; n     while (low <= high) { n       long long int mid = (low + high) / 2; n       if (targetSum(digit, mid) <= input) { n         answer = mid; n         low = mid + 1; n       } else n         high = mid - 1; n     } n     long long int mul = 1; n     for (int i = 1; i < digit; i++) mul *= 10; n     input -= targetSum(digit, answer); n     if (input == 0) n       printf( %lld n , (answer + mul - 1) % 10); n     else { n       low = 0, high = answer + mul - 1, answer = 0; n       while (low <= high) { n         long long int mid = (low + high) / 2; n         if (targetSubSum(mid) <= input) { n           answer = mid; n           low = mid + 1; n         } else n           high = mid - 1; n       } n       input -= targetSubSum(answer); n       if (input == 0) n         printf( %lld n , answer % 10); n       else { n         stack<int> s; n         answer++; n         while (answer) { n           s.push(answer % 10); n           answer /= 10; n         } n         for (int i = 1; i < input; i++) s.pop(); n         printf( %d n , s.top()); n       } n     } n     q--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long BigMod(long long B, long long P, long long M) { n   long long R = 1; n   while (P > 0) { n     if (P % 2 == 1) { n       R = (R * B) % M; n     } n     P /= 2; n     B = (B * B) % M; n   } n   return R; n } n template <class T1> n void deb(T1 e1) { n   cout << e1 << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n const int MAX = 100060; n map<int, int> mpp; n int cnt[2][MAX]; n int res[MAX]; n int main() { n   memset(cnt, (0), sizeof(cnt)); n   ; n   vector<int> tmp, rval; n   int v; n   set<int> com; n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &v); n     rval.push_back((v)); n     tmp.push_back((v)); n   } n   sort(tmp.begin(), tmp.end()); n   for (int i = 0; i < n; i++) { n     mpp[tmp[i]] = i + 1; n   } n   v = mpp[rval[0]]; n   com.insert(v); n   for (int i = 1; i < n; i++) { n     v = mpp[rval[i]]; n     auto it = com.lower_bound(v); n     if (it != com.begin()) { n       auto it1 = it; n       it1--; n       int u = (*it1); n       if (cnt[0][u] != -1) { n         res[i] = tmp[u - 1]; n         com.insert(v); n         cnt[0][u]--; n         continue; n       } n     } n     if (it != com.end()) { n       auto it1 = it; n       int u = (*it1); n       if (cnt[1][u] != -2) { n         res[i] = tmp[u - 1]; n         com.insert(v); n         cnt[1][u]--; n         continue; n       } n     } n   } n   for (int i = 1; i < n; i++) { n     if (i != 1) printf(   ); n     printf( %d , res[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   while (a != 0 && b != 0) { n     if (a > b) n       a %= b; n     else n       b = b % a; n   } n   return a + b; n } n int main() { n   int k, l, m, n, d; n   cin >> k >> l >> m >> n >> d; n   int ans = 0; n   for (int i = (1); i < (d + 1); i++) n     if (i % k == 0 || i % l == 0 || i % m == 0 || i % n == 0) ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long rdtsc() { n   long long tmp; n   asm( rdtsc  :  =A (tmp)); n   return tmp; n } n inline int myrand() { return abs((rand() << 15) ^ rand()); } n inline int rnd(int x) { return myrand() % x; } n const long long INF = (long long)1e16 + 1; n const long double EPS = 1e-9; n struct SegmTree { n   const static int maxn = (1 << 17); n   int a[maxn * 2]; n   int n; n   void build(int _n) { n     for (n = 1; n < _n; n <<= 1) n       ; n     assert(n <= maxn); n     for (int i = 1; i < 2 * n; ++i) a[i] = -1; n   } n   int get(int v) { n     int res = -1; n     for (v += n; v > 0; v >>= 1) n       if (a[v] != -1) res = a[v]; n     return res; n   } n   void make(int v, int l, int r, int l0, int r0, int nval) { n     if (l >= r0 || l0 >= r) return; n     if (l0 <= l && r <= r0) { n       a[v] = nval; n       return; n     } n     if (a[v] != -1) a[2 * v] = a[2 * v + 1] = a[v], a[v] = -1; n     int m = (l + r) / 2; n     make(2 * v, l, m, l0, r0, nval); n     make(2 * v + 1, m, r, l0, r0, nval); n   } n   void make(int l, int r, int nval) { make(1, 0, n, l, r, nval); } n } tree; n const int maxn = (int)1e5 + 10 + 1; n struct Vertex { n   int x, y; n   int g; n   Vertex() {} n   Vertex(int _x, int _y, int _g) : x(_x), y(_y), g(_g) {} n }; n const int maxg = 4; n const int go[maxg + 1][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {0, 0}}; n const char chgo[maxg] = { R ,  U ,  L ,  D }; n int dir[maxn][2][2]; n int dirg[maxn]; n struct Event { n   int type; n   int x; n   int l, r; n   int id; n   Event() {} n   Event(int _type, int _x, int _y, int _id) n       : type(_type), x(_x), l(_y), r(-1), id(_id) { n     assert(type == 1); n   } n   Event(int _type, int _x, int _l, int _r, int _id) n       : type(_type), x(_x), l(_l), r(_r), id(_id) { n     assert(type == 2); n   } n   inline bool operator<(const Event &e) const { n     if (x != e.x) return x < e.x; n     return type > e.type; n   } n }; n Event evs[3 * maxn]; n int n, b; n void rotate(int &x, int &y) { n   int nx = b - y, ny = x; n   x = nx, y = ny; n } n int gov[maxn * 2]; n void fillGo(int cnt, Vertex *vs) { n   for (int rot = 0; rot < maxg; ++rot) { n     int m = 0; n     for (int i = 0; i < cnt; ++i) { n       if (vs[i].g != 2) continue; n       evs[m++] = Event(1, vs[i].x, vs[i].y, i); n     } n     for (int i = 0; i < n; ++i) { n       if (dir[i][0][0] == dir[i][1][0]) { n         int l = dir[i][0][1], r = dir[i][1][1]; n         if (l > r) swap(l, r); n         evs[m++] = Event(2, dir[i][0][0], l, r, i); n       } else { n         assert(dir[i][0][1] == dir[i][1][1]); n         int x = n             (dir[i][0][0] < dir[i][1][0]) ? dir[i][0][0] : (dir[i][1][0] + 1); n         evs[m++] = Event(2, x, dir[i][0][1], dir[i][1][1], i); n       } n     } n     sort(evs, evs + m); n     tree.build(b + 1); n     for (int i = 0; i < m; ++i) { n       if (evs[i].type == 1) { n         gov[evs[i].id] = tree.get(evs[i].l); n       } else { n         assert(evs[i].type == 2); n         tree.make(evs[i].l, evs[i].r + 1, evs[i].id); n       } n     } n     for (int i = 0; i < cnt; ++i) { n       rotate(vs[i].x, vs[i].y); n       vs[i].g += 1; n       if (vs[i].g == 4) vs[i].g = 0; n     } n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < 2; ++j) { n         rotate(dir[i][j][0], dir[i][j][1]); n       } n     } n   } n } n Vertex vs[maxn * 2 * 2]; n int start[maxn][2]; n int startDir[maxn]; n long long startTime[maxn]; n int mynext[maxn * 4]; n int price[maxn * 4]; n const int maxl = 50; n int pr[maxl][maxn * 4]; n long long cs[maxl][maxn * 4]; n int sign(int x) { n   if (x < 0) return -1; n   return !!x; n } n bool solve() { n   if (scanf( %d%d , &n, &b) < 2) return 0; n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < 2; ++j) n       for (int k = 0; k < 2; ++k) scanf( %d , &dir[i][j][k]); n     int dx = dir[i][1][0] - dir[i][0][0], dy = dir[i][1][1] - dir[i][0][1]; n     if (dx == 0) { n       assert(dy != 0); n       dy /= abs(dy); n     } else { n       assert(dy == 0); n       dx /= abs(dx); n     } n     dirg[i] = -1; n     for (int g = 0; g < maxg; ++g) { n       if (go[g][0] == dx && go[g][1] == dy) dirg[i] = g; n     } n     assert(dirg[i] != -1); n     vs[cnt++] = Vertex(dir[i][1][0], dir[i][1][1], dirg[i]); n   } n   int q; n   scanf( %d , &q); n   for (int iter = 0; iter < q; ++iter) { n     char ch; n     scanf( n          %d%d %c  n          %lld , n         &start[iter][0], &start[iter][1], &ch, &startTime[iter]); n     startDir[iter] = find(chgo, chgo + maxg, ch) - chgo; n     assert(startDir[iter] != maxg); n     vs[cnt++] = Vertex(start[iter][0], start[iter][1], startDir[iter]); n   } n   fillGo(cnt, vs); n   int rcnt = cnt; n   for (int i = 0; i < cnt; ++i) { n     if (gov[i] == -1) { n       mynext[i] = rcnt; n       if (vs[i].g == 0) { n         price[i] = b - vs[i].x; n       } else { n         if (vs[i].g == 1) n           price[i] = b - vs[i].y; n         else { n           if (vs[i].g == 2) n             price[i] = vs[i].x; n           else { n             assert(vs[i].g == 3); n             price[i] = vs[i].y; n           } n         } n       } n       vs[rcnt] = Vertex(vs[i].x + go[vs[i].g][0] * price[i], n                         vs[i].y + go[vs[i].g][1] * price[i], 4); n       mynext[rcnt] = rcnt; n       price[rcnt] = 1; n       ++rcnt; n       continue; n     } n     int crash = gov[i]; n     mynext[i] = crash; n     price[i] = abs(vs[crash].x - vs[i].x) + abs(vs[crash].y - vs[i].y); n   } n   for (int i = 0; i < rcnt; ++i) { n     pr[0][i] = mynext[i], cs[0][i] = price[i]; n   } n   for (int it = 1; it < maxl; ++it) { n     for (int v = 0; v < rcnt; ++v) { n       pr[it][v] = pr[it - 1][pr[it - 1][v]]; n       cs[it][v] = cs[it - 1][v] + cs[it - 1][pr[it - 1][v]]; n       cs[it][v] = min(cs[it][v], INF); n     } n   } n   for (int iter = 0; iter < q; ++iter) { n     int v = iter + n; n     long long left = startTime[iter]; n     for (int it = maxl - 1; it >= 0; --it) { n       if (left >= cs[it][v]) { n         left -= cs[it][v]; n         v = pr[it][v]; n       } n     } n     { n       assert(left <= 3 * b + 5); n       int x = vs[v].x, y = vs[v].y; n       int nx = vs[pr[0][v]].x, ny = vs[pr[0][v]].y; n       if (x != nx && y != ny) { n         int dist1 = (vs[v].g & 1) ? (ny - y) : (nx - x); n         x += go[vs[v].g][0] * min((int)left, abs(dist1)); n         y += go[vs[v].g][1] * min((int)left, abs(dist1)); n         left -= min((int)left, abs(dist1)); n       } n       if (left > 0) { n         if (nx != x) x += sign(nx - x) * (int)left; n         if (ny != y) y += sign(ny - y) * (int)left; n       } n       printf( %d %d n , x, y); n     } n   } n   return 1; n } n int main() { n   srand(rdtsc()); n   while (1) { n     if (!solve()) break; n   } n   return 0; n } n