#include <bits/stdc++.h> n long long n, m, Q; n long long T; n std::vector<long long> req, l, r; n std::vector<long long> fill(std::vector<long long>& req) { n   std::vector<long long> ans(m + 1); n   long long tem = T; n   for (long long i = m; i; i--) { n     ans[i] = 0; n     if (i < m) ans[i] = ans[i + 1]; n     if (~req[i]) ans[i] = req[i]; n     tem -= ans[i]; n   } n   if (tem < 0) return std::vector<long long>(); n   for (long long i = m, last = m + 1; tem && ~i; i--) { n     if (~req[i]) { n       long long mx = (i < m) ? (last - 1 - i) * (req[i] - ans[i + 1]) : 0; n       if (mx <= tem) { n         for (long long j = i + 1; j < last; j++) ans[j] = req[i]; n         tem -= mx; n       } else { n         for (long long j = i + 1; j < last; j++) n           ans[j] += n               (tem / (last - 1 - i)) + ((tem % (last - 1 - i)) >= (j - i)); n         tem = 0; n       } n       last = i; n     } n   } n   if (tem) return std::vector<long long>(); n   return ans; n } n long long q[100001], h, t; n bool check(std::vector<long long> a, std::vector<long long> b) { n   long long n = (long long)a.size() - 1, m = (long long)b.size() - 1; n   h = t = 0; n   for (long long i = 2; i <= n; i++) a[i] += a[i - 1]; n   for (long long i = 2; i <= m; i++) b[i] += b[i - 1]; n   for (long long i = 1; i <= n; i++) a[i] -= 1ll * i * m; n   for (long long i = 1; i <= m; i++) b[i] -= 1ll * i * n; n   for (long long i = 1; i <= m; i++) { n     while (h < t - 1 && (1ll * (b[q[t]] - b[i]) * (q[t] - q[t - 1]) >= n                          1ll * (b[q[t - 1]] - b[q[t]]) * (i - q[t]))) n       --t; n     q[++t] = i; n   } n   for (long long i = 1; i <= n; i++) { n     while (h < t - 1 && (b[q[t]] + q[t] * i) >= (b[q[t - 1]] + q[t - 1] * i)) n       --t; n     if (b[q[t]] + q[t] * i < a[n] - a[i]) return 0; n   } n   return 1; n } n bool judge(long long x, long long y = -1) { n   std::vector<long long> Req = req; n   if (~y) { n     for (long long i = 1; i <= x; i++) Req[i] = y; n   } n   std::vector<long long> now = fill(Req); n   if (now.empty()) return 0; n   if (now[1] != now[x]) { n     for (long long i = 1; i <= x; i++) n       if (~req[i] && req[i] != now[1]) return 0; n     for (long long i = 1; i <= x; i++) Req[i] = now[1]; n     now = fill(Req); n     if (now.empty()) return 0; n   } n   std::reverse(now.begin() + 1, now.end()); n   return check(l, now) & check(now, r); n } n signed main() { n   scanf( %lld%lld , &n, &m); n   l.resize(n + 1), r.resize(n + 1); n   for (long long i = 1; i <= n; i++) scanf( %lld%lld , &l[i], &r[i]); n   std::sort(l.begin() + 1, l.end()); n   std::sort(r.begin() + 1, r.end()); n   req.resize(m + 1); n   for (long long i = 1; i <= m; i++) req[i] = -1; n   req[0] = 0x7f7f7f7f; n   scanf( %lld , &Q); n   for (long long i = 1, a, b; i <= Q; i++) { n     scanf( %lld%lld , &a, &b); n     req[a] = b; n   } n   scanf( %lld , &T); n   long long l = 1, r = m, mid, ans = -1; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (judge(mid)) n       ans = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   if (!~ans) { n     printf( -1 -1 ); n     return 0; n   } n   for (long long i = 1; i <= ans; i++) n     if (~req[i]) { n       printf( %lld %lld n , ans, req[i]); n       return 0; n     } n   l = fill(req)[1], r = n; n   long long ans1; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (judge(ans, mid)) n       ans1 = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   printf( %lld %lld n , ans, ans1); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 7; n const int MOD = 1e9 + 7; n long long a[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &a[i]); n     ans += a[i] * (n - a[i] + 1); n     if (i == 1) continue; n     long long t1 = a[i - 1], t2 = a[i]; n     if (t1 > t2) swap(t1, t2); n     ans -= t1 * (n - t2 + 1); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000][1000]; n int main(int argc, const char* argv[]) { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   int r[1000]; n   int c[1000]; n   for (int i = 0; i < 1000; i++) { n     r[i] = c[i] = i; n   } n   for (int i = 0; i < k; i++) { n     char cc; n     int x, y; n     cin >> cc >> x >> y; n     if (cc ==  c ) { n       swap(c[x - 1], c[y - 1]); n     } else if (cc ==  r ) { n       swap(r[x - 1], r[y - 1]); n     } else { n       cout << a[r[x - 1]][c[y - 1]] << endl; n       ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   long long cur = 9; n   long long ans = 0; n   long long dig = 1; n   cin >> n; n   while (n > 0) { n     ans += min(n * dig, cur * dig); n     n -= cur; n     cur *= 10; n     dig++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e3 + 10; n bool f[2][mx]; n int nxt[mx], bef[mx]; n int d[mx]; n int main() { n   int n, x, u; n   scanf( %d %d , &n, &x); n   memset(bef, -1, sizeof bef); n   memset(nxt, -1, sizeof nxt); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &u); n     if (u != 0) { n       nxt[i] = u; n       bef[u] = i; n     } n   } n   int lx = 0; n   int c = 0; n   for (int i = 1; i <= n; i++) { n     if (bef[i] == -1) { n       u = i; n       int len = 0; n       int lenx = 0; n       while (u != -1) { n         if (u == x || lenx > 0) lenx++; n         len++; n         u = nxt[u]; n       } n       if (lenx == 0) n         d[c++] = len; n       else n         lx = lenx; n     } n   } n   int rw = 0; n   f[0][0] = true; n   for (int i = 0; i < c; i++) { n     rw ^= 1; n     for (int s = 0; s < mx; s++) { n       f[rw][s] = f[rw ^ 1][s]; n       if (s >= d[i]) f[rw][s] |= f[rw ^ 1][s - d[i]]; n     } n   } n   vector<int> ans; n   for (int s = 0; s < n; s++) n     if (f[rw][s]) printf( %d n , s + lx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const int L = 20; n int h[N], par[N][L]; n int getLca(int u, int v) { n   if (h[v] < h[u]) swap(u, v); n   int k = h[v] - h[u]; n   for (int i = 0; i < L; i++) n     if (k >> i & 1) v = par[v][i]; n   if (u == v) return v; n   for (int i = L - 1; i >= 0; i--) n     if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i]; n   return par[u][0]; n } n int dis(int u, int v) { return h[u] + h[v] - 2 * h[getLca(u, v)]; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n = 5, mx = 2, ans = 2; n   for (int i = 2; i <= 4; i++) h[i] = par[i][0] = 1; n   int q; n   cin >> q; n   while (q--) { n     int v; n     cin >> v; n     h[n] = h[v] + 1; n     par[n][0] = v; n     for (int i = 1; i < L; i++) par[n][i] = par[par[n][i - 1]][i - 1]; n     n++; n     h[n] = h[v] + 1; n     par[n][0] = v; n     for (int i = 1; i < L; i++) par[n][i] = par[par[n][i - 1]][i - 1]; n     if (h[v] == h[mx]) mx = n, ans++; n     ans = max(ans, dis(n, mx)); n     n++; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   string str; n   cin >> t; n   while (t--) { n     cin >> str; n     sort(str.begin(), str.end()); n     int sz = str.size(); n     if (str[0] == str[sz - 1]) n       cout << -1 << endl; n     else n       cout << str << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using cd = complex<double>; n const long long INF = 1e15; n const int mod = 1e9 + 7; n const double Pi = acos(-1); n void Fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n int n, cnt = 0; n long long x, y; n bool Deg[200005]; n vector<int> Adj[200005]; n void DFS(int node, int p = -1) { n   int c = 0; n   for (auto x : Adj[node]) { n     if (x != p) { n       DFS(x, node); n       if (Deg[x]) { n         c++; n       } n     } n   } n   cnt += min(2, c); n   if (c < 2) { n     Deg[node] = true; n   } n } n signed main() { n   Fastio(); n   cin >> n >> x >> y; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     Adj[u].emplace_back(v); n     Adj[v].emplace_back(u); n   } n   if (x >= y) { n     for (int i = 1; i <= n; i++) { n       if (Adj[i].size() == n - 1) { n         cout << y * (n - 2) + x; n         return 0; n       } n     } n     cout << y * (n - 1); n     return 0; n   } n   DFS(1); n   cout << x * cnt + y * (n - 1 - cnt); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n const long long INF = (long long)0x7fffffffffff; n long long dp[maxn][maxn][maxn], wi[maxn], c[maxn]; n long long min(long long a, long long b) { return a > b ? b : a; } n int main() { n   int n, m, k, temp, flag = 1, ans1 = 0; n   cin >> n >> m >> k; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       for (int l = 0; l <= n; l++) { n         dp[i][j][l] = INF; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     cin >> c[i]; n   } n   for (int i = 1; i <= m; i++) cin >> wi[i]; n   if (c[1] == 0) n     for (int i = 1; i <= m; i++) dp[1][i][1] = wi[i]; n   else n     dp[1][c[1]][1] = 0; n   for (int i = 2; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> wi[j]; n       for (int l = 1; l <= i; l++) { n         if (c[i] == 0) { n           for (int p = 1; p <= m; p++) { n             if (p == j) { n               dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + wi[j]); n             } else { n               dp[i][j][l] = min(dp[i][j][l], dp[i - 1][p][l - 1] + wi[j]); n             } n           } n         } else { n           for (int p = 1; p <= m; p++) { n             if (p == c[i]) { n               dp[i][c[i]][l] = min(dp[i][c[i]][l], dp[i - 1][c[i]][l]); n             } else { n               dp[i][c[i]][l] = min(dp[i][c[i]][l], dp[i - 1][p][l - 1]); n             } n           } n         } n       } n     } n   } n   long long ans = INF; n   for (int i = 1; i <= m; i++) ans = min(ans, dp[n][i][k]); n   if (ans >= INF) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e3 + 5; n const int mod = 998244353; n inline void add_mod(int &a, int b) { n   a += b; n   if (a >= mod) a -= mod; n } n inline void chk_mod(int &a) { n   if (a < 0) a += mod; n } n inline long long pw(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res; n } n long long pw2[MAXN], icoef[MAXN]; n int n, c; n int a[MAXN]; n namespace Subtask1 { n int sum[MAXN][MAXN], f[MAXN][MAXN], g[MAXN][MAXN]; n void solve(void) { n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= c; ++j) sum[i][j] = sum[i - 1][j]; n     ++sum[i][a[i]]; n   } n   for (int i = 1; i <= n; ++i) { n     if (c == 1) { n       f[i][i] = 1; n       continue; n     } n     static int t[MAXN]; n     for (int j = 1; j <= c; ++j) t[j] = 0; n     int cnt = 0, cur = 1; n     for (int j = i; j <= n; ++j) { n       int ct = ++t[a[j]]; n       if (ct == 1) { n         ++cnt; n         if (cnt == c) { n           for (int k = 1; k <= c; ++k) n             if (k != a[i]) cur = cur * (pw2[t[k]] - 1) % mod; n           chk_mod(cur); n         } n       } else if (cnt == c && a[j] != a[i]) n         chk_mod(cur = cur * icoef[ct - 1] % mod * (pw2[ct] - 1) % mod); n       if (cnt == c && a[j] != a[i]) n         f[i][j] = cur * icoef[ct] % mod * pw2[t[a[i]] - 1] % mod; n     } n   } n   g[n + 1][0] = 1; n   for (int i = n; i >= 1; --i) { n     int mxp = (n - i + 1) / c; n     for (int j = i; j <= n; ++j) n       if (f[i][j]) n         for (int k = 1; k <= mxp; ++k) n           g[i][k] = (g[i][k] + (long long)g[j + 1][k - 1] * f[i][j]) % mod; n     g[i][0] = pw2[n - i]; n     for (int j = 1; j <= n; ++j) add_mod(g[i][0], mod - g[i][j]); n     for (int j = 0; j <= n; ++j) add_mod(g[i][j], g[i + 1][j]); n   } n   add_mod(g[1][0], mod - 1); n   for (int i = 0; i <= n; ++i) printf( %d  , g[1][i]); n   exit(0); n } n }  // namespace Subtask1 n namespace Subtask2 { n const int ALL = (1 << 12) + 5; n long long g[MAXN][ALL]; n void solve(void) { n   int all = (1 << c) - 1; n   g[0][0] = 1; n   for (int k = n; k >= 1; --k) { n     int mxp = (n - k + 1) / c; n     int cura = (1 << (a[k] - 1)); n     for (int i = mxp; i >= 0; --i) n       for (int mask = all - 1; mask >= 0; --mask) n         if (g[i][mask]) { n           if ((mask | cura) == all) n             g[i + 1][0] += g[i][mask]; n           else n             g[i][mask | cura] += g[i][mask]; n         } n     if (k % 20 == 0) { n       for (int i = 0; i <= mxp; ++i) n         for (int mask = 0; mask <= all; ++mask) g[i][mask] %= mod; n     } n   } n   g[0][0] += mod - 1; n   int mxp = n / c; n   for (int i = 0; i <= n; ++i) { n     if (i > mxp) { n       printf( 0  ); n       continue; n     } n     int res = 0; n     for (int mask = 0; mask < all; ++mask) add_mod(res, g[i][mask] % mod); n     printf( %d  , res); n   } n   exit(0); n } n }  // namespace Subtask2 n int main(void) { n   pw2[0] = 1; n   for (int i = 1; i < MAXN; ++i) pw2[i] = pw2[i - 1] * 2 % mod; n   for (int i = 0; i < MAXN; ++i) n     icoef[i] = pw((pw2[i] - 1 + mod) % mod, mod - 2); n   scanf( %d%d , &n, &c); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   if (c <= 12) n     Subtask2::solve(); n   else n     Subtask1::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   long long int n; n   cin >> n; n   long long int k; n   cin >> k; n   long long int sum = k; n   for (int i = (int)2; i <= (int)n; i++) { n     cin >> k; n     sum = sum + ((k - 1ll) * ((long long int)i - 1ll)) + k; n   } n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   if (b == 0) return a; n   return gcd(b % a, a); n } n long long power(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b % 2 != 0) res = (res * 1ll * a); n     b /= 2; n     a = (a * 1ll * a); n   } n   return res; n } n void solve() { n   string s; n   cin >> s; n   long long n = s.length(); n   bool v = false; n   bool h = false; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       if (!h) { n         h = true; n         cout << 1 <<     << 1 << endl; n       } else { n         h = false; n         cout << 1 <<     << 3 << endl; n       } n     } else { n       if (!v) { n         v = true; n         cout << 3 <<     << 4 << endl; n       } else { n         v = false; n         cout << 1 <<     << 4 << endl; n       } n     } n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n string s; n vector<string> t; n vector<pair<pair<int, int>, int> > cro; n bool cmp(const pair<pair<int, int>, int> &x, n          const pair<pair<int, int>, int> &y) { n   if (x.second == y.second) { n     if (x.first.first == y.first.first) return x.first.second < y.first.second; n     return x.first.first < y.first.first; n   } n   return x.second < y.second; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < (n); i++) { n     cin >> s; n     t.push_back(s); n   } n   for (int i = 0; i < (n); i++) { n     for (int j = 0; j < (m); j++) { n       if (t[i][j] ==  * ) { n         for (int r = (1); r <= (min(i, min(j, min(n - i - 1, m - j - 1)))); n              r++) { n           if ((t[i - r][j] ==  * ) && (t[i + r][j] ==  * ) && n               (t[i][j - r] ==  * ) && (t[i][j + r] ==  * )) n             cro.push_back(make_pair(make_pair(i, j), r)); n         } n       } n     } n   } n   if (cro.size() < k) n     cout <<  -1 n ; n   else { n     sort((cro).begin(), (cro).end(), cmp); n     cout << cro[k - 1].first.first + 1 <<     << cro[k - 1].first.second + 1 n          <<   n ; n     cout << cro[k - 1].first.first + 1 - cro[k - 1].second <<     n          << cro[k - 1].first.second + 1 <<   n ; n     cout << cro[k - 1].first.first + 1 + cro[k - 1].second <<     n          << cro[k - 1].first.second + 1 <<   n ; n     cout << cro[k - 1].first.first + 1 <<     n          << cro[k - 1].first.second + 1 - cro[k - 1].second <<   n ; n     cout << cro[k - 1].first.first + 1 <<     n          << cro[k - 1].first.second + 1 + cro[k - 1].second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fio() {} n void pti() { n   double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC; n   cerr <<  Timeuse   << timeuse <<  ms  << endl; n } n void end() { exit(0); } n namespace io { n const int SIZ = 55; n int que[SIZ], op, qr; n char ch; n template <class I> n inline void gi(I& w) { n   ch = getchar(), op = 1, w = 0; n   while (!isdigit(ch)) { n     if (ch ==  - ) op = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     w = w * 10 + ch -  0 ; n     ch = getchar(); n   } n   w *= op; n } n template <typename T, typename... Args> n inline void gi(T& t, Args&... args) { n   gi(t); n   gi(args...); n } n template <class I> n inline void print(I w) { n   qr = 0; n   if (!w) putchar( 0 ); n   if (w < 0) putchar( - ), w = -w; n   while (w) que[++qr] = w % 10 +  0 , w /= 10; n   while (qr) putchar(que[qr--]); n } n }  // namespace io n using io::gi; n using io::print; n const int N = 5005; n int n, m; n int a[N]; n int main() { n   fio(); n   gi(n, m); n   int t = 0; n   for (int i = 1; i <= n; ++i) { n     a[i] = i; n     t += (i - 1) >> 1; n     if (t >= m) { n       int res = (t - m) << 1; n       a[i] += res; n       for (int j = n, s = 1e9; j > i; --j, s -= a[i] + 1) a[j] = s; n       for (int j = 1; j <= n; ++j) print(a[j]), putchar(   ); n       end(); n     } n   } n   print(-1); n   end(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n const int Mod = 1e9 + 7; n struct Mat { n   long long A[2][2]; n   Mat() { memset(A, 0, sizeof(A)); } n   void init(void) { n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) A[i][j] = (i == j); n   } n   Mat operator+(struct Mat A) { n     struct Mat B; n     for (int i = 0; i < 2; i++) { n       for (int j = 0; j < 2; j++) { n         B.A[i][j] = this->A[i][j] + A.A[i][j]; n         B.A[i][j] %= Mod; n       } n     } n     return B; n   } n   Mat operator*(struct Mat A) { n     struct Mat B; n     for (int i = 0; i < 2; i++) { n       for (int j = 0; j < 2; j++) { n         for (int k = 0; k < 2; k++) { n           B.A[i][j] += (this->A[i][k] * A.A[k][j]); n           B.A[i][j] %= Mod; n         } n       } n     } n     return B; n   } n   Mat operator^(int t) { n     struct Mat A, B; n     A.init(); n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) B.A[i][j] = this->A[i][j]; n     while (t) { n       if (t & 1) A = A * B; n       t >>= 1; n       B = B * B; n     } n     return A; n   } n }; n struct Mat Tree[N << 2], Laze[N << 2], f; n int A[N]; n void pushUp(int n) { Tree[n] = (Tree[n << 1] + Tree[n << 1 | 1]); } n void pushDown(int n) { n   Tree[n << 1] = Laze[n] * Tree[n << 1]; n   Tree[n << 1 | 1] = Laze[n] * Tree[n << 1 | 1]; n   Laze[n << 1] = Laze[n] * Laze[n << 1]; n   Laze[n << 1 | 1] = Laze[n] * Laze[n << 1 | 1]; n   Laze[n].init(); n } n void build(int l, int r, int rt) { n   Laze[rt].init(); n   if (l == r) { n     Tree[rt] = f ^ (A[l] - 1); n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, rt << 1); n   build(mid + 1, r, rt << 1 | 1); n   pushUp(rt); n } n void update(int l, int r, int rt, int _l, int _r, struct Mat x) { n   if (l >= _l && r <= _r) { n     Tree[rt] = x * Tree[rt]; n     Laze[rt] = x * Laze[rt]; n     return; n   } n   pushDown(rt); n   int mid = (l + r) >> 1; n   if (_r <= mid) { n     update(l, mid, rt << 1, _l, _r, x); n   } else if (_l > mid) { n     update(mid + 1, r, rt << 1 | 1, _l, _r, x); n   } else { n     update(l, mid, rt << 1, _l, _r, x); n     update(mid + 1, r, rt << 1 | 1, _l, _r, x); n   } n   pushUp(rt); n } n int query(int l, int r, int rt, int _l, int _r) { n   if (l >= _l && r <= _r) { n     return Tree[rt].A[0][0]; n   } n   pushDown(rt); n   int mid = (l + r) >> 1; n   if (_r <= mid) { n     return query(l, mid, rt << 1, _l, _r); n   } else if (_l > mid) { n     return query(mid + 1, r, rt << 1 | 1, _l, _r); n   } n   return (query(l, mid, rt << 1, _l, _r) + n           query(mid + 1, r, rt << 1 | 1, _l, _r)) % n          Mod; n } n int main(void) { n   int n, m; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) f.A[i][j] = 1; n   f.A[1][1] = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> A[i]; n   } n   build(1, n, 1); n   while (m--) { n     int type, l, r; n     cin >> type >> l >> r; n     if (type == 1) { n       int x; n       cin >> x; n       update(1, n, 1, l, r, f ^ x); n     } else if (type == 2) { n       cout << query(1, n, 1, l, r) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool solve() { n   int a, b; n   scanf( %d %d , &a, &b); n   int both = int(floor(pow((double)a * b, 1.0 / 3) + 0.5)); n   if (both == 0) return a == 1 && b == 1; n   if (a % both != 0 || b % both != 0) return false; n   if ((long long)a * b != (long long)both * both * both) return false; n   long long A = a / both; n   long long B = b / both; n   if (B * B * A != b || A * A * B != a) return false; n   return true; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     if (solve()) n       puts( Yes ); n     else n       puts( No ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void cline() { cout <<   n ; } n template <typename T, typename... V> n void cline(T t, V... v) { n   cout << t; n   if (sizeof...(v)) cout <<    ; n   cline(v...); n } n void cspc() { cout <<    ; } n template <typename T, typename... V> n void cspc(T t, V... v) { n   cout << t; n   if (sizeof...(v)) cout <<    ; n   cspc(v...); n } n const long long N = 2e5 + 4; n long long state[2 * N][2]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(state, 0, sizeof(state)); n   long long n, m; n   cin >> n >> m; n   long long ind = -1; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (m == a[i]) ind = i; n   } n   long long oki = 0, cnt = 0; n   for (long long i = 0; i < n; i++) { n     if (a[i] < m) n       oki--; n     else if (a[i] > m) n       oki++; n     if (i >= ind) state[N + oki][i & 1]++; n   } n   oki = 0; n   for (long long i = 0; i <= ind; ++i) { n     if (i % 2 == 1) { n       cnt += state[N + oki + 1][0]; n       cnt += state[N + oki][1]; n     } else { n       cnt += state[N + oki][0]; n       cnt += state[N + oki + 1][1]; n     } n     if (a[i] < m) n       oki--; n     else n       oki++; n   } n   cspc(cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chkmax(T& x, T y) { n   if (x < y) x = y; n } n template <typename T> n void chkmin(T& x, T y) { n   if (x > y) x = y; n } n inline int read() { n   int x = 0; n   char c = getchar(); n   bool f = 0; n   while (c < 48) f |= c ==  - , c = getchar(); n   while (c > 47) x = x * 10 + (c ^ 48), c = getchar(); n   return f ? -x : x; n } n const int maxn = 5e5 + 10, P = 998244353; n int n, m, A[maxn]; n char str[maxn]; n inline void Add(int& x, int y) { n   x += y; n   if (x >= P) x -= P; n } n struct matrix { n   int arr[2][2]; n   void clear() { memset(arr, 0, sizeof arr); } n   inline int* operator[](const int& pos) { return arr[pos]; } n   inline void print() { n     for (int i = (0), iend = (1); i <= iend; ++i) n       for (int j = (0), jend = (1); j <= jend; ++j) n         printf( %d%c , arr[i][j],   n  [j < 1]); n   } n } E; n matrix operator*(matrix A, matrix B) { n   matrix res; n   res.clear(); n   for (int k = (0), kend = (1); k <= kend; ++k) n     for (int i = (0), iend = (1); i <= iend; ++i) n       for (int j = (0), jend = (1); j <= jend; ++j) n         Add(res[i][j], 1ll * A[i][k] * B[k][j] % P); n   return res; n } n matrix qp(matrix A, long long k) { n   matrix res = E; n   for (; k; k >>= 1, A = A * A) n     if (k & 1) res = res * A; n   return res; n } n void minit() { n   E.clear(); n   for (int i = (0), iend = (1); i <= iend; ++i) E[i][i] = 1; n } n matrix val[maxn << 2]; n void ins(int k, int l, int r, int x) { n   if (l == r) { n     auto tmp = val[k].arr; n     tmp[1][1] = 0, tmp[0][1] = 1; n     tmp[0][0] = (A[x] + 1), tmp[1][0] = x > 1 && A[x - 1] == 1 ? 9 - A[x] : 0; n     return; n   } n   x <= ((l + r) >> 1) ? ins(k << 1, l, ((l + r) >> 1), x) n                       : ins(k << 1 | 1, ((l + r) >> 1) + 1, r, x), n       val[k] = val[k << 1] * val[k << 1 | 1]; n } n void solve() { n   minit(); n   scanf( %d%d%s , &n, &m, str + 1); n   for (int i = (1), iend = (n); i <= iend; ++i) n     A[i] = str[i] -  0 , ins(1, 1, n, i); n   while (m--) { n     int xx = read(), yy = read(); n     A[xx] = yy; n     ins(1, 1, n, xx); n     if (xx < n) ins(1, 1, n, xx + 1); n     printf( %d n , val[1][0][0]); n   } n } n signed main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void minu(T& x, T y) { n   x = min(x, y); n } n template <class T> n inline void maxu(T& x, T y) { n   x = max(x, y); n } n inline long long bit(long long n) { return 1LL << n; } n template <class T> n ostream& operator<<(ostream& os, vector<T> x) { n   for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++) n     os << *it <<    ; n   return os; n } n long long dx[8] = {1, -1, 0, 0, -1, 1, 1, -1}; n long long dy[8] = {0, 0, 1, -1, -1, 1, -1, 1}; n const long long mod = 1e9 + 7; n long long pow2[200005]; n long long memo[505][505]; n long long rec(long long n, long long k, long long t) { n   if (k < 0) return 0; n   if (t == 0) { n     return k == 0 ? 1 : 0; n   } n   if (n == 0) { n     return (k == 0 && t == 0) ? 1 : 0; n   } n   if (t < n) { n     return rec(t, k, t) * pow2[t * (n - t)] % mod; n   } n   long long& ret = memo[n][k]; n   if (ret >= 0) return ret; n   ret = 0; n   for (long long bt = 0; bt < ((long long)n); ++bt) { n     ret += n         rec(n - 1, k - 1, bt) * (pow2[n - bt] - 1 + mod) % mod * pow2[bt] % mod; n     ret %= mod; n   } n   return ret; n } n int main() { n   long long n, k; n   cin >> n >> k; n   pow2[0] = 1; n   for (long long i = 0; i < ((long long)200000); ++i) n     pow2[i + 1] = pow2[i] * 2 % mod; n   long long ans = 0; n   for (long long i = 0; i < ((long long)505); ++i) n     for (long long j = 0; j < ((long long)505); ++j) memo[i][j] = -1; n   for (long long t = 0; t < ((long long)n + 1); ++t) n     ans = (ans + rec(n, k, t)) % mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[200005], n, k, a[200010], ans, sum, num = 1; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   if (sum % k) { n     cout <<  No ; n     return 0; n   } else { n     sum /= k; n     for (int i = 1; i < n; i++) { n       ans += a[i]; n       cnt[num]++; n       if (ans > sum) { n         cout <<  No ; n         return 0; n       } n       if (ans == sum) { n         ans = 0; n         num++; n       } n     } n     ans += a[n]; n     if (ans != sum) { n       cout <<  No ; n       return 0; n     } else n       cnt[num]++; n     cout <<  Yes  << endl; n     for (int i = 1; i <= num; i++) cout << cnt[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int isreq(char c) { return (c ==  H ) || (c ==  Q ) || (c ==  9 ); } n int main() { n   int n; n   cin >> n; n   int cnt = 0; n   string str; n   for (int i = 0; i < n; i++) { n     cin >> str; n     if (str[1] ==  + ) { n       cnt++; n     } else if (str[1] ==  - ) { n       cnt--; n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int len(const T& c) { n   return (int)c.size(); n } n int main() { n   int n, bb; n   double b1; n   vector<double> b; n   cin >> n >> bb; n   for (int i(0), _n(n); i < _n; ++i) { n     cin >> b1; n     b.push_back(b1); n   } n   double sum = accumulate(b.begin(), b.end(), 0); n   double mx = *max_element(b.begin(), b.end()); n   double avg = (sum + bb) / n; n   if (mx > avg) { n     cout << -1; n     return 0; n   } n   for (int i(0), _n(n); i < _n; ++i) printf( %.6f n , avg - b[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, c; n   int j, i; n   cin >> r >> c; n   char a; n   map<int, int> m, n; n   for (j = 0; j < r; j++) { n     for (i = 0; i < c; i++) { n       cin >> a; n       if (a ==  S ) { n         m[i]++; n         n[j]++; n       } n     } n   } n   cout << (r * c) - (m.size() * n.size()); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 402; n int a[maxn], f[maxn][2]; n int n, m, s, ans; n int main() { n   int i, j, k; n   scanf( %d%d , &n, &m); n   while (m--) { n     scanf( %d , &k); n     while (k--) { n       scanf( %d , &i); n       a[i] = ++s; n     } n   } n   for (k = 1; k <= n; k++) n     if (!a[k]) break; n   for (i = 1; i <= s; i++) { n     if (a[i] != i) { n       for (j = i + 1; j <= n; j++) n         if (a[j] == i) break; n       if (i != k) { n         f[++ans][0] = i; n         f[ans][1] = k; n       } n       f[++ans][0] = j; n       f[ans][1] = i; n       a[k] = a[i]; n       a[i] = i; n       k = j; n     } n   } n   printf( %d n , ans); n   for (i = 1; i <= ans; i++) printf( %d %d n , f[i][0], f[i][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a, b; n int main() { n   int n, m; n   cin >> n >> m; n   int x = n + m; n   int mx = 1; n   while (1LL * mx * (mx + 1) / 2 <= x) mx++; n   mx--; n   for (int i = mx; i >= 1; i--) { n     if (n >= i) { n       n -= i; n       a.push_back(i); n     } else { n       m -= i; n       b.push_back(i); n     } n   } n   printf( %d n , (int)a.size()); n   for (int i : a) printf( %d  , i); n   puts(  ); n   printf( %d n , (int)b.size()); n   for (int i : b) printf( %d  , i); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int p[4][4]; n int a[(1 << 16)]; n int sum1[4]; n int sum2[4]; n int dio1, dio2; n bool ok; n int mask; n void dfs(int x) { n   int i, j; n   if (x == n * n) { n     if (dio1 == dio2 && dio1 == sum1[0] && sum1[1] == dio1 && sum1[2] == dio1 && n         sum2[0] == dio1 && sum2[1] == dio1 && sum2[2] == dio1) { n       ok = true; n       printf( %d n , dio1); n       for (i = 0; i < n; i++) { n         printf( %d , p[i][0]); n         for (j = 1; j < n; j++) printf(  %d , p[i][j]); n         printf(  n ); n       } n     } n     return; n   } n   for (i = 0; i < n * n && !ok; i++) n     if ((mask & (1 << i)) == 0) { n       mask |= (1 << i); n       p[x / n][x % n] = a[i]; n       sum1[x / n] += a[i]; n       sum2[x % n] += a[i]; n       if (x / n == x % n) dio1 += a[i]; n       if (x == 2 || x == 4 || x == 6) dio2 += a[i]; n       dfs(x + 1); n       sum1[x / n] -= a[i]; n       sum2[x % n] -= a[i]; n       if (x / n == x % n) dio1 -= a[i]; n       if (x == 2 || x == 4 || x == 6) dio2 -= a[i]; n       mask ^= (1 << i); n     } n } n void dfs2(int x) { n   int i, j; n   if (x == n * n) { n     if (dio1 == dio2 && dio1 == sum1[0] && sum1[1] == dio1 && sum2[0] == dio1 && n         sum2[1] == dio1) { n       ok = true; n       printf( %d n , dio1); n       for (i = 0; i < n; i++) { n         printf( %d , p[i][0]); n         for (j = 1; j < n; j++) printf(  %d , p[i][j]); n         printf(  n ); n       } n     } n     return; n   } n   for (i = 0; i < n * n && !ok; i++) n     if ((mask & (1 << i)) == 0) { n       mask |= (1 << i); n       p[x / n][x % n] = a[i]; n       sum1[x / n] += a[i]; n       sum2[x % n] += a[i]; n       if (x / n == x % n) dio1 += a[i]; n       if (x == 1 || x == 2) dio2 += a[i]; n       dfs2(x + 1); n       sum1[x / n] -= a[i]; n       sum2[x % n] -= a[i]; n       if (x / n == x % n) dio1 -= a[i]; n       if (x == 1 || x == 2) dio2 -= a[i]; n       mask ^= (1 << i); n     } n } n int sum; n void super_dfs(int x) { n   int i, j; n   if (x == 16) { n     if (dio1 == dio2 && dio1 == sum1[0] && dio1 == sum1[3]) n       if (dio1 == sum2[1]) { n         ok = true; n         printf( %d n , dio1); n         for (i = 0; i < n; i++) { n           printf( %d , p[i][0]); n           for (j = 1; j < n; j++) printf(  %d , p[i][j]); n           printf(  n ); n         } n       } n     return; n   } n   int tem; n   int znac; n   if (x > 3 && sum1[0] != sum) return; n   if (x == 7) { n     if (ok) return; n     tem = (1 << 16) - 1; n     tem ^= mask; n     while (tem > 0) { n       znac = tem - (tem & (tem - 1)); n       if (a[znac] == sum1[0] - sum1[1]) { n         mask |= znac; n         p[1][3] = a[znac]; n         sum1[1] = sum1[0]; n         sum2[3] += a[znac]; n         super_dfs(x + 1); n         sum1[1] -= a[znac]; n         sum2[3] -= a[znac]; n         mask ^= znac; n         break; n       } n       tem &= (tem - 1); n     } n     return; n   } n   if (x == 8) { n     tem = (1 << 16) - 1; n     tem ^= mask; n     while (tem > 0 && !ok) { n       znac = tem - (tem & (tem - 1)); n       mask |= znac; n       p[(x >> 2)][(x & 3)] = a[znac]; n       sum1[(x >> 2)] += a[znac]; n       sum2[(x & 3)] += a[znac]; n       if ((x >> 2) == (x & 3)) dio1 += a[znac]; n       if (x == 3 || x == 6 || x == 12) dio2 += a[znac]; n       int tt = (1 << 16) - 1; n       tt ^= mask; n       while (tt > 0) { n         int zc = tt - (tt & (tt - 1)); n         if (a[zc] + sum2[0] == sum) { n           sum2[0] = sum; n           sum1[3] += a[zc]; n           dio2 += a[zc]; n           mask |= zc; n           p[3][0] = a[zc]; n           super_dfs(x + 1); n           dio2 -= a[zc]; n           sum2[0] -= a[zc]; n           sum1[3] -= a[zc]; n           mask ^= zc; n         } n         tt &= (tt - 1); n       } n       if ((x >> 2) == (x & 3)) dio1 -= a[znac]; n       if (x == 3 || x == 6 || x == 12) dio2 -= a[znac]; n       sum1[(x >> 2)] -= a[znac]; n       sum2[(x & 3)] -= a[znac]; n       mask ^= znac; n       tem &= (tem - 1); n     } n     return; n   } n   if (x == 9) { n     if (ok) return; n     tem = (1 << 16) - 1; n     tem ^= mask; n     while (tem > 0) { n       znac = tem - (tem & (tem - 1)); n       if (a[znac] == sum2[0] - dio2) { n         mask |= znac; n         p[2][1] = a[znac]; n         sum2[1] += a[znac]; n         sum1[2] += a[znac]; n         dio2 = sum2[0]; n         super_dfs(x + 1); n         sum2[1] -= a[znac]; n         sum1[2] -= a[znac]; n         dio2 -= a[znac]; n         mask ^= znac; n         break; n       } n       tem &= (tem - 1); n     } n     return; n   } n   if (x == 11) { n     if (ok) return; n     if (sum1[1] - sum1[2] != dio1 - sum2[3]) return; n     tem = (1 << 16) - 1; n     tem ^= mask; n     while (tem > 0) { n       znac = tem - (tem & (tem - 1)); n       if (a[znac] == dio1 - sum2[3]) { n         mask |= znac; n         p[2][3] = a[znac]; n         sum2[3] = dio1; n         sum1[2] += a[znac]; n         super_dfs(13); n         sum2[3] -= a[znac]; n         sum1[2] -= a[znac]; n         mask ^= znac; n         break; n       } n       tem &= (tem - 1); n     } n     return; n   } n   if (x == 13) { n     if (ok) return; n     tem = (1 << 16) - 1; n     tem ^= mask; n     while (tem > 0) { n       znac = tem - (tem & (tem - 1)); n       if (a[znac] == sum2[0] - sum2[1]) { n         mask |= znac; n         p[3][1] = a[znac]; n         sum2[1] = sum2[0]; n         sum1[3] += a[znac]; n         super_dfs(x + 1); n         sum2[1] -= a[znac]; n         sum1[3] -= a[znac]; n         mask ^= znac; n         break; n       } n       tem &= (tem - 1); n     } n     return; n   } n   if (x == 14) { n     if (ok) return; n     tem = (1 << 16) - 1; n     tem ^= mask; n     while (tem > 0) { n       znac = tem - (tem & (tem - 1)); n       if (a[znac] == sum2[1] - sum2[2]) { n         mask |= znac; n         p[3][2] = a[znac]; n         sum2[2] = sum2[1]; n         sum1[3] += a[znac]; n         super_dfs(x + 1); n         sum2[2] -= a[znac]; n         sum1[3] -= a[znac]; n         mask ^= znac; n         break; n       } n       tem &= (tem - 1); n     } n     return; n   } n   tem = (1 << 16) - 1; n   tem ^= mask; n   while (tem > 0 && !ok) { n     znac = tem - (tem & (tem - 1)); n     mask |= znac; n     p[(x >> 2)][(x & 3)] = a[znac]; n     sum1[(x >> 2)] += a[znac]; n     sum2[(x & 3)] += a[znac]; n     if ((x >> 2) == (x & 3)) dio1 += a[znac]; n     if (x == 3 || x == 6 || x == 12) dio2 += a[znac]; n     super_dfs(x + 1); n     if ((x >> 2) == (x & 3)) dio1 -= a[znac]; n     if (x == 3 || x == 6 || x == 12) dio2 -= a[znac]; n     sum1[(x >> 2)] -= a[znac]; n     sum2[(x & 3)] -= a[znac]; n     mask ^= znac; n     tem &= (tem - 1); n   } n } n int main() { n   scanf( %d , &n); n   int i; n   int ss = 0; n   for (i = 0; i < n * n; i++) { n     scanf( %d , &a[(1 << i)]); n     ss += a[(1 << i)]; n   } n   if (n == 1) { n     printf( %d n , a[1]); n     printf( %d n , a[1]); n     return 0; n   } n   if (n == 2) { n     for (i = 0; i < n * n; i++) a[i] = a[(1 << i)]; n     dfs2(0); n     return 0; n   } n   if (n == 3) { n     for (i = 0; i < n * n; i++) a[i] = a[(1 << i)]; n     dfs(0); n     return 0; n   } n   if (n == 4) { n     sum = ss / n; n     super_dfs(0); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, A, B; n int main() { n   scanf( %d%d%d%d , &N, &M, &A, &B); n   int rA = (A - 1) / M + 1, cA = A - (rA - 1) * M; n   int rB = (B - 1) / M + 1, cB = B - (rB - 1) * M; n   if (B == N) cB = M; n   if (rA == rB) { n     puts( 1 ); n     exit(0); n   } n   if (rA + 1 == rB) { n     if (cA == 1 && cB == M) n       puts( 1 ); n     else n       puts( 2 ); n     exit(0); n   } n   int fA = (cA == 1); n   int fB = (cB == M); n   if (fA + fB) { n     printf( %d n , 3 - fA - fB); n     exit(0); n   } n   if (cB + 1 == cA) n     puts( 2 ); n   else n     puts( 3 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[11][11]; n bool ok; n string s; n int sol; n queue<int> dulj, broj; n int pomocnopolje[11][11]; n void bfs(int x, int y, int a, int b) { n   broj.push(a); n   dulj.push(1); n   int v[10] = {0}; n   while (!broj.empty()) { n     int duljina = dulj.front(); n     dulj.pop(); n     int br = broj.front(); n     broj.pop(); n     if (!v[(br + x) % 10]) { n       v[(br + x) % 10] = duljina; n       broj.push((br + x) % 10); n       dulj.push(duljina + 1); n     } n     if (!v[(br + y) % 10]) { n       v[(br + y) % 10] = duljina; n       broj.push((br + y) % 10); n       dulj.push(duljina + 1); n     } n   } n   pomocnopolje[a][b] = v[b] - 1; n   return; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> s; n   for (int i = 0; i < 10; i++) { n     for (int j = i; j < 10; j++) { n       ok = 1; n       sol = 0; n       for (int k = 0; k < 10; k++) { n         for (int l = 0; l < 10; l++) { n           bfs(i, j, k, l); n         } n       } n       for (int znj = 1; znj < s.size() and ok; znj++) { n         int a = s[znj - 1] -  0 , b = s[znj] -  0 ; n         if (pomocnopolje[a][b] >= 0) n           sol += pomocnopolje[a][b]; n         else n           ok = 0; n       } n       if (ok) { n         ans[i][j] = sol; n         ans[j][i] = sol; n       } else { n         ans[i][j] = -1; n         ans[j][i] = -1; n       } n     } n   } n   for (int i = 0; i < 10; i++) { n     for (int j = 0; j < 10; j++) { n       cout << ans[i][j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n const int INF = 0x3f3f3f3f; n template <typename Tp> n struct RMQ { n   int n; n   int len[MAXN]; n   Tp mx[MAXN][21]; n   Tp mi[MAXN][21]; n   void build_rmq() { n     for (int j = 1; (1 << j) <= n; j++) n       for (int i = 1; i + (1 << j) - 1 <= n; i++) { n         mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]); n         mi[i][j] = min(mi[i][j - 1], mi[i + (1 << (j - 1))][j - 1]); n       } n   } n   int QueryMax(int L, int R) { n     if (L > R) return 0; n     int k = len[R - L + 1]; n     return max(mx[L][k], mx[R - (1 << k) + 1][k]); n   } n   int QueryMin(int L, int R) { n     if (L > R) return INF; n     int k = len[R - L + 1]; n     return min(mi[L][k], mi[R - (1 << k) + 1][k]); n   } n   void init(Tp *a, int sz) { n     n = sz; n     for (int i = 1; i <= n; i++) mx[i][0] = mi[i][0] = a[i]; n     build_rmq(); n     for (int i = 0; i <= n; i++) len[i] = (i == 0 ? -1 : len[i >> 1] + 1); n   } n }; n RMQ<int> st; n int n, q, mx = 0, tim = 0; n vector<int> E[MAXN]; n int in[MAXN], out[MAXN], pos[MAXN], dep[MAXN], fa[MAXN][20]; n void dfs(int now, int f = 1, int dp = 0) { n   mx = max(mx, dp); n   fa[now][0] = f; n   for (int i = 1; i < 20; i++) fa[now][i] = fa[fa[now][i - 1]][i - 1]; n   in[now] = ++tim; n   dep[now] = dp; n   pos[tim] = now; n   for (int v : E[now]) { n     dfs(v, now, dp + 1); n   } n   out[now] = tim; n } n int up(int L, int R, int now) { n   if (in[now] <= L && out[now] >= R) return dep[now]; n   for (int i = 19; i >= 0; i--) { n     int f = fa[now][i]; n     if (in[f] <= L && out[f] >= R) continue; n     now = fa[now][i]; n   } n   return dep[fa[now][0]]; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 2; i <= n; i++) { n     int x; n     scanf( %d , &x); n     E[x].push_back(i); n   } n   dfs(1); n   st.init(in, n); n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     if (l == r) { n       printf( %d %d n , l, mx); n     } else { n       int Mi = st.QueryMin(l, r); n       int Mx = st.QueryMax(l, r); n       int dep1, dep2; n       int now1, now2; n       int st1, st2; n       now1 = min(st.QueryMin(l, pos[Mi] - 1), st.QueryMin(pos[Mi] + 1, r)); n       now2 = max(st.QueryMax(l, pos[Mx] - 1), st.QueryMax(pos[Mx] + 1, r)); n       st1 = pos[Mx], st2 = pos[Mi]; n       dep1 = up(now1, Mx, st1); n       dep2 = up(Mi, now2, st2); n       if (dep1 > dep2) n         printf( %d %d n , st2, dep1); n       else n         printf( %d %d n , st1, dep2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace std { n template <typename A, typename B> n inline string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n inline string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n inline string to_string(tuple<A, B, C, D> p); n inline string to_string(const string& s) { return     + s +    ; } n inline string to_string(const char* s) { return to_string((string)s); } n inline string to_string(bool b) { return (b ?  true  :  false ); } n inline string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n inline string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n inline string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n inline string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n inline string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n inline string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n }  // namespace std n inline void debug_out() { std::cerr << std::endl; } n template <typename Head, typename... Tail> n inline void debug_out(Head H, Tail... T) { n   std::cerr <<     << std::to_string(H); n   debug_out(T...); n } n using namespace std; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   vector<vector<int>> e(n); n   for (int i = 0; i < m; i++) { n     int v, u; n     scanf( %d%d , &v, &u); n     v--; n     u--; n     e[v].push_back(u); n     e[u].push_back(v); n   } n   vector<pair<int, int>> color(n); n   vector<int> b(n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &color[i].first); n     color[i].first--; n     color[i].second = i; n     b[i] = color[i].first; n   } n   sort((color).begin(), (color).end()); n   bool good = true; n   vector<int> use(n); n   for (int i = 0; i < n; i++) { n     int v = color[i].second; n     int tgt = color[i].first; n     vector<int> mask(tgt + 1); n     for (auto u : e[v]) { n       if (use[u]) { n         assert(b[u] <= tgt); n         mask[b[u]] = 1; n       } n     } n     for (int j = 0; j < tgt; j++) { n       if (mask[j] == 0) { n         good = false; n       } n     } n     if (mask[tgt]) { n       good = false; n     } n     use[v] = 1; n   } n   if (!good) { n     puts( -1 ); n     return 0; n   } n   for (auto x : color) { n     cout << x.second + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 10; n const int M = 1000000007; n const double eps = 1e-9; n const double PI = acos(-1); n const int oo = 1000000000; n typedef double TT; n struct point { n   TT x, y; n   point() : x(0), y(0) {} n   point(TT a, TT b) { n     x = a; n     y = b; n   } n   point operator+(const point& p) const { return point(x + p.x, y + p.y); } n   friend ostream& operator<<(ostream& os, const point& p) { n     return os <<  (  << p.x <<  ,  << p.y <<  ) ; n   } n   point operator-(const point& p) const { return point(x - p.x, y - p.y); } n   bool operator<(const point& p) const { n     if (x - p.x) return x < p.x; n     return y < p.y; n   } n   bool operator==(const point& p) const { return x == p.x && y == p.y; } n   bool operator!=(const point& p) const { return !(*this == p); } n   void scan() { scanf( %lf%lf , &x, &y); } n   double len() { return sqrt(x * x + y * y); } n   double dist(const point& p) const { return (p - *this).len(); } n   point operator*(TT d) { return point(x * d, y * d); } n   point operator/(TT d) { return point(x / d, y / d); } n   TT operator*(const point& p) const { return x * p.x + y * p.y; } n   TT operator^(const point& p) const { return x * p.y - y * p.x; } n   double linePointDist(point a, point b, bool seg = false) { n     point c = *this; n     if (seg) { n       if ((b - a) * (c - b) > 0) return b.dist(c); n       if ((a - b) * (c - a) > 0) return a.dist(c); n     } n     return abs((b - a) ^ (c - a)) / a.dist(b); n   } n   void rotate(double theta, point Z = point(0, 0)) { n     TT ox = x - Z.x; n     TT oy = y - Z.y; n     x = ox * cos(theta) - oy * sin(theta) + Z.x; n     y = ox * sin(theta) + oy * cos(theta) + Z.y; n   } n }; n double areaOfTri(point a, point b, point c) { n   point x = b - a; n   point y = c - a; n   return (x ^ y) / 2.0; n } n double areaOfPolygon(vector<point>& P) { n   TT area = 0; n   for (int i = 1; i < P.size() - 1; ++i) n     area += (P[i] - P[0]) ^ (P[i + 1] - P[0]); n   return abs(area / 2.0); n } n bool collinear(point a, point b, point c) { return areaOfTri(a, b, c) == 0; } n vector<point> x(4), y(4); n int main() { n   double mnx = oo, mxx = -oo, mny = oo, mxy = -oo; n   for (int i = 0; i < 4; ++i) { n     x[i].scan(); n     mnx = min(mnx, x[i].x); n     mny = min(mny, x[i].y); n     mxx = max(mxx, x[i].x); n     mxy = max(mxy, x[i].y); n   } n   point dx((mnx + mxx) / 2, (mny + mxy) / 2); n   for (int i = 0; i < 4; ++i) y[i].scan(); n   for (int i = 0; i < 4; ++i) n     if (y[i].x >= mnx && y[i].x <= mxx && y[i].y >= mny && y[i].y <= mxy) n       return cout <<  YES , 0; n   double area = abs(areaOfPolygon(y)); n   for (int i = 0; i < 4; ++i) { n     double cc = abs(areaOfTri(y[3], y[0], x[i])); n     for (int j = 1; j < 4; ++j) cc += abs(areaOfTri(y[j - 1], y[j], x[i])); n     if (abs(cc - area) < eps) return cout <<  YES , 0; n   } n   double cc = abs(areaOfTri(y[3], y[0], dx)); n   for (int j = 1; j < 4; ++j) cc += abs(areaOfTri(y[j - 1], y[j], dx)); n   if (abs(cc - area) < eps) return cout <<  YES , 0; n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, l; n long long sum; n int main() { n   cin >> a >> b >> c >> l; n   sum = ((l + 1) * (l + 2) * (l + 3)) / 6; n   for (long long i = 0; i <= l; i++) { n     long long temp1 = a - b - c + i; n     if (temp1 >= 0) { n       long long temp2 = l - i; n       long long x = min(temp1, temp2); n       sum -= (x + 1) * (x + 2) / 2; n     } n   } n   for (long long i = 0; i <= l; i++) { n     long long temp1 = b - a - c + i; n     if (temp1 >= 0) { n       long long temp2 = l - i; n       long long x = min(temp1, temp2); n       sum -= (x + 1) * (x + 2) / 2; n     } n   } n   for (long long i = 0; i <= l; i++) { n     long long temp1 = c - b - a + i; n     if (temp1 >= 0) { n       long long temp2 = l - i; n       long long x = min(temp1, temp2); n       sum -= (x + 1) * (x + 2) / 2; n     } n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using pii = pair<int, int>; n constexpr int ALPHABET_SIZE =  z  -  a  + 1; n constexpr int MAX_SIZE = 256; n constexpr int INF = 1e8; n int n; n string s; n string as, bs, cs; n vector<int> next_pos[ALPHABET_SIZE]; n int calced_pos[MAX_SIZE][MAX_SIZE][MAX_SIZE]; n int calced_mark[MAX_SIZE][MAX_SIZE][MAX_SIZE]; n int added_mark[MAX_SIZE][MAX_SIZE][MAX_SIZE]; n int dda[(int)3e5]; n int ddb[(int)3e5]; n int ddc[(int)3e5]; n void promote(int s_pos, int mark, int type) { n   int dsize = 0; n   int dptr = 0; n   if (type == 1) { n     for (int bb = 0; bb <= bs.size(); bb++) { n       for (int cc = 0; cc <= cs.size(); cc++) { n         dda[dsize] = s_pos; n         ddb[dsize] = bb; n         ddc[dsize] = cc; n         dsize++; n         calced_mark[s_pos][bb][cc] = mark; n         added_mark[s_pos][bb][cc] = mark; n       } n     } n   } else if (type == 2) { n     for (int aa = 0; aa <= as.size(); aa++) { n       for (int cc = 0; cc <= cs.size(); cc++) { n         dda[dsize] = aa; n         ddb[dsize] = s_pos; n         ddc[dsize] = cc; n         dsize++; n         calced_mark[aa][s_pos][cc] = mark; n         added_mark[aa][s_pos][cc] = mark; n       } n     } n   } else { n     for (int aa = 0; aa <= as.size(); aa++) { n       for (int bb = 0; bb <= bs.size(); bb++) { n         dda[dsize] = aa; n         ddb[dsize] = bb; n         ddc[dsize] = s_pos; n         dsize++; n         calced_mark[aa][bb][s_pos] = mark; n         added_mark[aa][bb][s_pos] = mark; n       } n     } n   } n   while (dptr < dsize) { n     int a = dda[dptr]; n     int b = ddb[dptr]; n     int c = ddc[dptr]; n     dptr++; n     int &m = calced_mark[a][b][c]; n     m = mark; n     added_mark[a][b][c] = mark; n     int &pos = calced_pos[a][b][c]; n     if (a == 0 && b == 0 && c == 0) { n       pos = 0; n     } n     if (a < as.size()) { n       int &p = calced_pos[a + 1][b][c]; n       int &mm = calced_mark[a + 1][b][c]; n       int &am = added_mark[a + 1][b][c]; n       if (am != mark) { n         if (mm != mark) { n           p = INF; n           mm = mark; n           dda[dsize] = a + 1; n           ddb[dsize] = b; n           ddc[dsize] = c; n           dsize++; n         } n         if (pos < n) p = min(p, next_pos[as[a] -  a ][pos] + 1); n       } n     } n     if (b < bs.size()) { n       int &p = calced_pos[a][b + 1][c]; n       int &mm = calced_mark[a][b + 1][c]; n       int &am = added_mark[a][b + 1][c]; n       if (am != mark) { n         if (mm != mark) { n           p = INF; n           mm = mark; n           dda[dsize] = a; n           ddb[dsize] = b + 1; n           ddc[dsize] = c; n           dsize++; n         } n         if (pos < n) p = min(p, next_pos[bs[b] -  a ][pos] + 1); n       } n     } n     if (c < cs.size()) { n       int &p = calced_pos[a][b][c + 1]; n       int &mm = calced_mark[a][b][c + 1]; n       int &am = added_mark[a][b][c + 1]; n       if (am != mark) { n         if (mm != mark) { n           p = INF; n           mm = mark; n           dda[dsize] = a; n           ddb[dsize] = b; n           ddc[dsize] = c + 1; n           dsize++; n         } n         if (pos < n) p = min(p, next_pos[cs[c] -  a ][pos] + 1); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int q; n   cin >> n >> q; n   cin >> s; n   memset(calced_mark, 0, sizeof(calced_mark)); n   memset(added_mark, 0, sizeof(added_mark)); n   for (int i = 0; i < ALPHABET_SIZE; i++) { n     next_pos[i].resize(n, INF); n   } n   for (int i = n - 1; i >= 0; i--) { n     if (i + 1 < n) { n       for (int z = 0; z < ALPHABET_SIZE; z++) { n         next_pos[z][i] = next_pos[z][i + 1]; n       } n     } n     int c = s[i] -  a ; n     next_pos[c][i] = i; n   } n   int mark = 228; n   char c, which; n   int num; n   while (q--) { n     mark++; n     cin >> c >> num; n     if (c ==  - ) { n       if (num == 1) { n         as.pop_back(); n       } else if (num == 2) { n         bs.pop_back(); n       } else if (num == 3) { n         cs.pop_back(); n       } n     } else { n       cin >> which; n       if (num == 1) { n         as.push_back(which); n         promote(as.size() - 1, mark, num); n       } else if (num == 2) { n         bs.push_back(which); n         promote(bs.size() - 1, mark, num); n       } else if (num == 3) { n         cs.push_back(which); n         promote(cs.size() - 1, mark, num); n       } n     } n     cout << (calced_pos[as.size()][bs.size()][cs.size()] <= s.size() ?  YES  n                                                                      :  NO ) n          << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long long INF = 1e18; n const int MAXN = 2e5 + 5; n const int T = 1e3 + 10; n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n long long binpow(long long val, long long deg) { n   if (!deg) return 1; n   if (deg & 1) return binpow(val, deg - 1) * val % MOD; n   long long res = binpow(val, deg / 2); n   return (res * res) % MOD; n } n long long inv(long long a) { return binpow(a, MOD - 2); } n pair<int, int> operator+(const pair<int, int>& a, const pair<int, int>& b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n pair<int, int> operator-(const pair<int, int>& a, const pair<int, int>& b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n void rotate90(int& first, int& second, int x0, int y0) { n   first = first - x0; n   second = second - y0; n   swap(first, second); n   first = -first; n   first += x0; n   second += y0; n } n long long i, j; n long long n, m; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n   cout.precision(10); n   cin >> n >> m; n   set<int> column, line; n   while (m--) { n     int first, second; n     cin >> first >> second; n     column.insert(first); n     line.insert(second); n     cout << (n - column.size()) * (n - line.size()) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const double PI = 3.141592653589793238463; n const int INF = 0x3f3f3f3f; n const int INFS = 1000000000; n const long long LLINF = 9223372036854775807; n const int M = 1000000007; n const double EPS = 1e-9; n set<int> get(int n) { n   set<int> ans = {}; n   for (int i = 2; i * i <= n; ++i) { n     if (n % i == 0) { n       ans.insert(i); n       while (n % i == 0) n /= i; n     } n   } n   if (n != 1) ans.insert(n); n   return ans; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<set<int> > a(n + 1); n   for (int i = 1; i < n + 1; i++) a[i] = get(i); n   map<int, int> curr; n   vector<bool> on(n + 1, false); n   char t; n   int i; n   while (m--) { n     cin >> t >> i; n     if (t ==  + ) { n       if (on[i]) n         cout <<  Already on  << endl; n       else { n         bool ok = true; n         int j; n         for (auto x : a[i]) { n           if (curr[x] != 0) { n             ok = false; n             j = curr[x]; n             break; n           } n         } n         if (ok) { n           cout <<  Success  << endl; n           for (auto x : a[i]) curr[x] = i; n           on[i] = true; n         } else { n           cout <<  Conflict with   << j << endl; n         } n       } n     } else { n       if (!on[i]) n         cout <<  Already off  << endl; n       else { n         cout <<  Success  << endl; n         for (auto x : a[i]) curr[x] = 0; n         on[i] = false; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n typedef long long ll; n typedef long double ld; n  n typedef vector<int> vi; n typedef vector<ll> vll; n  n typedef vector<vi> vvi; n typedef vector<vll> vvll; n  n typedef pair<int, int> pii; n typedef pair<ll, ll> pll; n  n typedef vector<pii> vpii; n typedef vector<pll> vpll; n  n typedef vector<vpii> vvpii; n typedef vector<vpll> vvpll; n  n #define pb push_back n #define mp make_pair n #define all(x) (x).begin(), (x).end() n #define rall(x) (x).rbegin(), (x).rend() n #define sz(x) (int)(x).size() n #define fi first n #define se second n  n template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;} n template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;} n  n namespace debug { n     void __print(int x) {cerr << x;} n     void __print(long long x) {cerr << x;} n     void __print(double x) {cerr << x;} n     void __print(long double x) {cerr << x;} n     void __print(char x) {cerr <<      << x <<     ;} n     void __print(const string &x) {cerr <<     << x <<    ;} n     void __print(bool x) {cerr << (x ?  true  :  false );} n  n     template<typename T, typename V> n     void __print(const pair<T, V> &x) {cerr <<  { ; __print(x.first); cerr <<  , ; __print(x.second); cerr <<  } ;} n     template<typename T> n     void __print(const T &x) {int f = 0; cerr <<  { ; for(auto z : x) cerr << (f++ ?  ,  :   ), __print(z); cerr <<  } ;} n     void _print() {cerr <<  ] n ;} n     template <typename T, typename... V> n     void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr <<  ,  ; _print(v...);} n  n #ifdef ljuba n #define dbg(x...) cerr <<  LINE(  << __LINE__ <<  ) ->   <<  [  << #x <<  ] = [ ; _print(x) n #else n #define dbg(x...) n #endif n } n  n using namespace debug; n  n const char nl =   n ; n  n mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); n  n int mat[1000][1000]; n int m, k; n  n bool moze(int n, const vi &v) { n     for(int i = 0; i < n; ++i) { n         for(int j = 0; j < n; ++j) { n             mat[i][j] = -1; n         } n     } n  n     vpii nz; n     for(int i = 0; i < k; ++i) { n         if(v[i]) { n             nz.pb({v[i], i+1}); n         } n     } n     sort(all(nz)); n  n     for(int i = 1; i < n; i += 2) { n         for(int j = 0; j < n; j += 2) { n             if(mat[i][j] != -1) continue; n             if(sz(nz) && nz.back().fi > 0) { n                 nz.back().fi--; n                 mat[i][j] = nz.back().se; n                 if(nz.back().fi == 0) n                     nz.pop_back(); n             } n         } n     } n  n     pii kasnije{-1, -1}; n  n     if(sz(nz) && nz.back().fi > 0) { n         kasnije = nz.back(); n         nz.pop_back(); n     } n  n     dbg(kasnije); n  n     for(int i = 0; i < n; i += 2) { n         for(int j = 1; j < n; j += 2) { n             if(mat[i][j] != -1) continue; n             if(sz(nz) && nz.back().fi > 0) { n                 nz.back().fi--; n                 mat[i][j] = nz.back().se; n                 if(nz.back().fi == 0) n                     nz.pop_back(); n             } n         } n     } n  n     if(kasnije.fi != -1) n         nz.pb(kasnije); n  n     for(int i = 0; i < n; i += 2) { n         for(int j = 0; j < n; j += 2) { n             if(mat[i][j] != -1) continue; n             if(sz(nz) && nz.back().fi > 0) { n                 nz.back().fi--; n                 mat[i][j] = nz.back().se; n                 if(nz.back().fi == 0) n                     nz.pop_back(); n             } n         } n     } n  n     for(int i = 0; i < n; ++i) { n         for(int j = 0; j < n; ++j) { n             if(i % 2 == 1 && j % 2 == 1) continue; n             if(sz(nz)) { n                 nz.back().fi--; n                 mat[i][j] = nz.back().se; n                 if(nz.back().fi == 0) n                     nz.pop_back(); n             } n         } n     } n  n     return nz.empty(); n } n  n void solve() { n     cin >> m >> k; n     vi v(k); n     for(auto &z : v) cin >> z; n  n     int mx = *max_element(all(v)); n     int n = -1; n     for(int i = 1; ; ++i) { n         ll izraz = i * 1LL * ((i + 1) / 2); n         if(mx <= izraz) { n             ckmax(n, i); n             break; n         } n     } n     for(int i = 1; ; ++i) { n         ll izraz = i * 1LL * i; n         ll temp = i / 2; n         izraz -= temp * 1LL * temp; n         if(m <= izraz) { n             ckmax(n, i); n             break; n         } n     } n  n     while(!moze(n, v)) n         ++n; n  n     cout << n << nl; n     for(int i = 0; i < n; ++i) { n         for(int j = 0; j < n; ++j) { n             if(mat[i][j] == -1) mat[i][j] = 0; n             cout << mat[i][j] <<    ; n         } n         cout << nl; n     } n } n  n int main() { n     ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n  n #ifdef ljuba n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n     freopen( error.txt ,  w , stderr); n #endif n  n     int testCases = 1; n     cin >> testCases; n     while(testCases--) n         solve(); n  n }


#include <bits/stdc++.h> n using namespace std; n int n, ans, kol[100500]; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     v.push_back(x); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (kol[j] <= v[i]) { n         kol[j]++; n         break; n       } n   for (int i = 0; i < n; i++) ans += (kol[i] > 0); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string s; n   int a; n   int x = 0, y = n + 1; n   for (int i = 0; i < m; i++) { n     cin >> s; n     cin >> s; n     cin >> s; n     if (s[0] ==  r ) { n       cin >> s; n       cin >> a; n       if (a > x) x = a; n     } else { n       cin >> s; n       cin >> a; n       if (a < y) y = a; n     } n   } n   if (y - x <= 1 || x == n || y == 1) n     cout << -1; n   else n     cout << y - x - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q, n; n   cin >> q; n   while (q--) { n     cin >> n; n     vector<int> original; n     int temp; n     for (int i = 0; i < n; i++) { n       cin >> temp; n       original.push_back(temp); n     } n     for (int i = 0; i < original.size(); i++) { n       int days = 0; n       int origpos = i; n       int newpos = original[i]; n       bool home = false; n       while (!home) { n         newpos = original[newpos - 1]; n         if (newpos == original[i]) { n           home = true; n         } n         days++; n       } n       cout << days <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   priority_queue<int> q; n   for (int i = 0; i < 32; i++) { n     if (n & (1 << i)) { n       q.push(i); n     } n   } n   if (q.size() > k) { n     cout <<  NO n ; n   } else { n     while (q.size() != k) { n       int f = q.top(); n       q.pop(); n       if (f == 0) { n         cout <<  NO n ; n         return 0; n       } n       q.push(f - 1); n       q.push(f - 1); n     } n     cout <<  YES n ; n     while (!q.empty()) { n       int val = 1 << q.top(); n       cout << val <<    ; n       q.pop(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T mod_v(T num) { n   if (num >= 0) n     return num % 1000000007; n   else n     return (num % 1000000007 + 1000000007) % 1000000007; n } n template <class T> n inline T gcd(T a, T b) { n   a = abs(a); n   b = abs(b); n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n template <class T> n T fast_pow(T n, T p) { n   if (p == 0) return 1; n   if (p % 2) { n     T g = mod_v(mod_v(n) * mod_v(fast_pow(n, p - 1))); n     return g; n   } else { n     T g = fast_pow(n, p / 2); n     g = mod_v(mod_v(g) * mod_v(g)); n     return g; n   } n } n template <class T> n inline T modInverse(T n) { n   return fast_pow(n, 1000000007 - 2); n } n template <class T> n inline void debug(string S1, T S2, string S3) { n   cout << S1 << S2 << S3; n } n template <class T> n inline T in() { n   register char c = 0; n   register T num = 0; n   bool n = false; n   while (c < 33) c = getchar(); n   while (c > 33) { n     if (c ==  - ) n       n = true; n     else n       num = num * 10 + c -  0 ; n     c = getchar(); n   } n   return n ? -num : num; n } n class data { n  public: n   int t, w; n }; n int dp[108][208][208]; n int n; n data ara[108]; n vector<int> ans; n int re(int p, int s, int u) { n   if (p == n) { n     return s; n     return ((s >= u) ? s : 100000000); n   } n   if (dp[p][s][u] != -1) return dp[p][s][u]; n   dp[p][s][u] = 1 << 30; n   if (u - ara[p].w - ara[p].t >= 0) { n     dp[p][s][u] = n         min(dp[p][s][u], re(p + 1, s - ara[p].t, u - ara[p].t - ara[p].w)); n   } n   ans.push_back(p + 1); n   dp[p][s][u] = min(dp[p][s][u], re(p + 1, s, u)); n   ans.pop_back(); n   return dp[p][s][u]; n } n int main() { n   n = in<int>(); n   int t = 0, w = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &ara[i].t, &ara[i].w); n     t += ara[i].t; n   } n   memset(dp, -1, sizeof(dp)); n   ; n   printf( %d n , re(0, t, t)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     vector<long long int> vec(n); n     for (int i = 0; i < n; i++) { n       cin >> vec[i]; n     } n     sort(vec.begin(), vec.end()); n     long long int sum = vec[n - 1]; n     int x = n - 2; n     while (k--) { n       sum += vec[x]; n       x--; n     } n     cout << sum <<   n ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 100001; n int main(void) { n   char arr1[N] = {0}; n   char arr2[N] = {0}; n   char str1[2] = {0}, str2[2] = {0}; n   scanf( %[^ n]s , arr1); n   scanf(  %[^ n]s , arr2); n   if (strlen(arr1) != strlen(arr2)) { n     cout <<  NO n ; n     return 0; n   } n   int a = 0; n   for (int i = 0, j = 0, k = 0; i < strlen(arr1); i++) { n     if (arr1[i] != arr2[i]) { n       a++; n       if (a <= 2) { n         str1[j++] = arr1[i]; n         str2[k++] = arr2[i]; n       } n     } n   } n   if (a == 2) { n     if ((str1[1] == str2[0]) && (str1[0] == str2[1])) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int main() { n   cin >> n >> k; n   if (n / 2 > k) return !printf( -1 ); n   if (n == 1) { n     if (k == 0) n       printf( 1 ); n     else n       printf( -1 ); n     return 0; n   } n   int t = k - (n - 2) / 2; n   printf( %d %d , t, 2 * t); n   for (int i = 1; i <= n - 2; ++i) printf(  %d , i + 2 * t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void input() {} n void solve() { n   long long int n, m; n   cin >> n >> m; n   long long int a[n][m]; n   long long int flag = 1, temp = 0; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       char ch; n       cin >> ch; n       if (ch ==  A ) { n         a[i][j] = 1; n       } else { n         a[i][j] = 0; n       } n       if (a[i][j]) { n         flag = 0; n       } n       temp += a[i][j]; n     } n   } n   if (temp == n * m) { n     cout <<  0 ; n     return; n   } n   if (flag) { n     cout <<  MORTAL ; n     return; n   } n   for (long long int i = 0; i < 1; i++) { n     long long int cnt1 = 0, cnt2 = 0; n     for (long long int j = 0; j < m; j++) { n       if (a[0][j]) { n         cnt1++; n       } n       if (a[n - 1][j]) { n         cnt2++; n       } n     } n     if (cnt1 == m || cnt2 == m) { n       cout <<  1 ; n       return; n     } n   } n   for (long long int j = 0; j < 1; j++) { n     long long int cnt1 = 0, cnt2 = 0; n     for (long long int i = 0; i < n; i++) { n       if (a[i][0]) { n         cnt1++; n       } n       if (a[i][m - 1]) { n         cnt2++; n       } n     } n     if (cnt1 == n || cnt2 == n) { n       cout <<  1 ; n       return; n     } n   } n   for (long long int i = 1; i < n - 1; i++) { n     long long int cnt = 0; n     for (long long int j = 0; j < m; j++) { n       if (a[i][j]) { n         cnt++; n       } n     } n     if (cnt == m) { n       cout <<  2 ; n       return; n     } n   } n   for (long long int j = 1; j < m - 1; j++) { n     long long int cnt = 0; n     for (long long int i = 0; i < n; i++) { n       if (a[i][j]) { n         cnt++; n       } n     } n     if (cnt == n) { n       cout <<  2 ; n       return; n     } n   } n   if (a[0][m - 1] || a[0][0] || a[n - 1][0] || a[n - 1][m - 1]) { n     cout <<  2 ; n     return; n   } n   long long int cnt = 0; n   for (long long int i = 0; i < n; i++) { n     cnt += a[i][0]; n     cnt += a[i][m - 1]; n   } n   for (long long int i = 0; i < m; i++) { n     cnt += a[0][i]; n     cnt += a[n - 1][i]; n   } n   if (cnt) { n     cout <<  3 ; n   } else { n     cout <<  4 ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   input(); n   int t = 1, i = 1; n   cin >> t; n   while (t--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long pow(long long p, long long r, long long m) { n   long long ans = 1; n   while (r > 0) { n     if (r & 1) { n       ans = ((ans % m) * (p % m)) % m; n     } n     r = r >> 1; n     p = ((p % m) * (p % m)) % m; n   } n   return ans; n } n string con(string s) { n   for (long long i = 0; i < s.length(); i++) { n     s[i] = toupper(s[i]); n   } n   return s; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     stack<long long> s1; n     for (char i : s) { n       if (!s1.empty() && i ==  B ) n         s1.pop(); n       else n         s1.push(i); n     } n     cout << s1.size() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string dna =  ACGT ; n string word[20]; n set<string> words; n map<string, int> mp; n string pm[110]; n pair<int, int> graph[110][20][4]; n long long dp[1010][110][20]; n int main() { n   ios::sync_with_stdio(false); n   int N, K, i, j, k, l, x; n   cin >> N >> K; n   for (i = 0; i < K; i++) cin >> word[i]; n   for (i = 0; i < K; i++) words.insert(word[i]); n   for (i = 0; i < K; i++) n     for (j = 1; j <= word[i].length(); j++) mp[word[i].substr(0, j)] = 0; n   mp[  ] = 0; n   int s = 0; n   for (auto itr = mp.begin(); itr != mp.end(); itr++) { n     (itr->second) = s; n     pm[s] = (itr->first); n     s++; n   } n   for (i = 0; i < s; i++) { n     for (j = 0; j < pm[i].length() + 1; j++) { n       for (k = 0; k < 4; k++) { n         string s = pm[i] + dna[k]; n         int nextst = -1; n         for (l = 0;; l++) { n           if (mp.find(s.substr(l)) != mp.end()) { n             nextst = mp[s.substr(l)]; n             break; n           } n         } n         bool cover = false; n         for (l = 0; l < pm[i].length() - j + 1; l++) n           if (words.find(s.substr(l)) != words.end()) cover = true; n         graph[i][j][k] = make_pair(-1, -1); n         int nextcover = j + 1; n         if (cover) nextcover = 0; n         if (nextcover >= 10 || nextcover > pm[nextst].length()) continue; n         graph[i][j][k] = make_pair(nextst, nextcover); n       } n     } n   } n   int start = mp[  ]; n   dp[0][start][0] = 1; n   for (i = 0; i < N; i++) { n     for (j = 0; j < s; j++) { n       for (k = 0; k < 10; k++) { n         if (dp[i][j][k] != 0) { n           for (x = 0; x < 4; x++) { n             pair<int, int> p = graph[j][k][x]; n             if (p.first != -1) { n               int i2 = i + 1, j2 = p.first, k2 = p.second; n               dp[i2][j2][k2] = (dp[i][j][k] + dp[i2][j2][k2]) % 1000000009ll; n             } n           } n         } n       } n     } n   } n   long long ans = 0; n   for (i = 0; i < s; i++) ans = (ans + dp[N][i][0]) % 1000000009ll; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n vector<int> v[2][N]; n int a[N], q[N], n; n bool vst[2][N]; n void bfs(int val) { n   int i, j, qh = 0, qe = 0, k = val - 1, s, out; n   for (i = 1; i <= n; i++) n     if (a[i] == val) q[qe++] = i, vst[k][i] = 1; n   while (qh < qe) { n     out = q[qh++]; n     if (val != 2 || a[out] != 1) n       for (i = 0, s = v[k][out].size(); i < s; i++) n         if (!vst[k][j = v[k][out][i]]) q[qe++] = j, vst[k][j] = 1; n   } n } n int main() { n   int i, j, e; n   while (2 == scanf( %d%d , &n, &e)) { n     for (i = 1; i <= n; i++) n       scanf( %d , a + i), v[0][i].clear(), v[1][i].clear(); n     while (e--) { n       scanf( %d%d , &i, &j); n       v[0][i].push_back(j); n       v[1][j].push_back(i); n     } n     memset(vst, 0, sizeof vst); n     bfs(1); n     bfs(2); n     for (i = 1; i <= n; i++) puts(vst[0][i] && vst[1][i] ?  1  :  0 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPossible(long long k, long long x, long long y, long long p, n                 long long q) { n   p = p * k; n   q = q * k; n   if (x > p || y > q) { n     return false; n   } n   if (q - y < p - x) { n     return false; n   } n   return true; n } n long long solve(long long x, long long y, long long p, long long q) { n   long long low = 1; n   long long high = 1e9; n   while (low < high) { n     long long mid = low + (high - low) / 2; n     if (!isPossible(mid, x, y, p, q)) { n       low = mid + 1; n     } else { n       high = mid; n     } n   } n   if (!isPossible(low, x, y, p, q)) { n     return -1; n   } n   return (q * low - y); n } n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long x, y, p, q; n     cin >> x >> y >> p >> q; n     cout << solve(x, y, p, q) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool eq(double a, double b) { return fabs(a - b) < 1e-9; } n const long long int inf = 1e15; n inline int two(int n) { return 1 << n; } n inline int test(int n, int b) { return (n >> b) & 1; } n inline void set_bit(int& n, int b) { n |= two(b); } n inline void unset_bit(int& n, int b) { n &= ~two(b); } n inline int last_bit(int n) { return n & (-n); } n inline int ones(int n) { n   int res = 0; n   while (n && ++res) n -= n & (-n); n   return res; n } n string str; n string sol(int n) { n   string ans; n   for (int i = (0); i < (n); i++) { n     ans = ans + (i < n / 2 ?  4  :  7 ); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> str; n   int n = str.length(); n   if (n & 1) { n     cout << sol(n + 1) << endl; n   } else { n     int ind = -1, c4 = 0, c7 = 0; n     for (int i = (0); i < (n); i++) { n       if (str[i] >  7 ) { n         if (ind == -1) { n           cout << sol(n + 2) << endl; n           return 0; n         } else { n           str[ind] =  7 ; n           str[i] =  4 ; n           c7++; n           if (abs(c4 - c7) > n - i - 1) { n             cout << sol(n + 2) << endl; n             return 0; n           } else { n             int j = i + 1; n             for (int k = (j); k < (n); k++) { n               str[k] =  4 ; n               c4++; n             } n             for (int k = (n - 1); k >= (i + 1); k--) { n               if (c4 > c7) { n                 str[k] =  7 ; n                 c4--; n                 c7++; n               } else { n                 break; n               } n             } n             cout << str << endl; n             return 0; n           } n         } n       } else if (str[i] !=  4  && str[i] !=  7 ) { n         if (str[i] <  4 ) { n           str[i] =  4 ; n           c4++; n         } else { n           str[i] =  7 ; n           c7++; n         } n         if (abs(c4 - c7) > n - i - 1) { n           cout << sol(n + 2) << endl; n           return 0; n         } else { n           int j = i + 1; n           for (int k = (j); k < (n); k++) { n             str[k] =  4 ; n             c4++; n           } n           for (int k = (n - 1); k >= (i + 1); k--) { n             if (c4 > c7) { n               str[k] =  7 ; n               c4--; n               c7++; n             } else { n               break; n             } n           } n           cout << str << endl; n           return 0; n         } n       } else { n         if (str[i] ==  4 ) { n           ind = i; n           c4++; n         } else n           c7++; n       } n     } n     if (c4 > c7) { n       for (int i = (ind); i >= (0); i--) { n         if (c4 > c7) { n           str[i] =  7 ; n           c4--; n           c7++; n         } else { n           break; n         } n       } n       cout << str << endl; n     } else if (c7 > c4) { n       int cc4 = 0, cc7 = 0; n       for (int i = 0; i <= ind; i++) { n         if (str[i] ==  4 ) n           cc4++; n         else n           cc7++; n       } n       c4 = cc4, c7 = cc7; n       for (int i = (ind); i >= (0); i--) { n         if (str[i] ==  4 ) { n           if (abs(c4 - c7 - 1) <= n - i - 1) { n             str[i] =  7 ; n             c4--; n             c7++; n             int j = i + 1; n             for (int k = (j); k < (n); k++) { n               str[k] =  4 ; n               c4++; n             } n             for (int k = (n - 1); k >= (i + 1); k--) { n               if (c4 > c7) { n                 str[k] =  7 ; n                 c4--; n                 c7++; n               } else { n                 break; n               } n             } n             cout << str << endl; n             return 0; n           } n           c4--; n         } else { n           c7--; n         } n       } n       cout << sol(n + 2) << endl; n     } else { n       cout << str << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d, m, n, x[25], a[30005]; n vector<long long> ok; n long long L, R; n map<long long, vector<long long> > ans; n map<long long, vector<long long> > zip; n vector<long long> calc(long long lim) { n   if (zip.find(lim) != zip.end()) return zip[lim]; n   vector<long long> ret(m, 0); n   if (lim == 0) { n     ret[0] = 1; n     return ret; n   } n   for (int beg = 0; beg < d && beg <= lim; beg++) { n     long long nxt_lim = (lim - beg) / d; n     vector<long long> R = calc(nxt_lim); n     for (int i = 0; i < m; i++) { n       int a = (i + x[beg]); n       if (a >= m) a -= m; n       ret[a] += R[i]; n     } n   } n   return zip[lim] = ret; n } n void rec(long long lim, vector<long long> mask, bool F) { n   if (mask.size() == 1) { n     if (ans.find(lim) == ans.end()) { n       ans[lim] = vector<long long>(m, 0); n     } n     for (int i = 0; i < m; i++) { n       if (((mask[0] >> i) & 1LL)) { n         if (F) n           ans[lim][i]++; n         else n           ans[lim][i]--; n       } n     } n     return; n   } n   for (int beg = 0; beg < d && beg <= lim; beg++) { n     long long nxt_lim = (lim - beg) / d; n     vector<long long> vec; n     int zan = mask.size(); n     int bb = (d - beg); n     int xx = 1 + (zan - bb + d - 1) / d; n     vec.resize(xx, 0LL); n     long long B = (1LL << m) - 1; n     int cur = beg; n     int id = 0; n     for (int i = 0; i < mask.size(); i++) { n       int add = x[cur]; n       long long nw = 0; n       nw = (mask[i] >> add) | (mask[i] << (m - add)); n       B &= nw; n       if (cur == d - 1 || i == mask.size() - 1) { n         vec[id++] = B; n         B = (1LL << m) - 1; n       } n       cur++; n       if (cur == d) cur = 0; n     } n     assert(id == xx); n     rec(nxt_lim, vec, F); n   } n } n int main() { n   cin >> d >> m; n   for (int i = 0; i < d; i++) { n     cin >> x[i]; n   } n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     ok.push_back((2LL << a[i]) - 1LL); n   } n   cin >> L >> R; n   L--; n   R--; n   rec(R - n + 1, ok, true); n   if (L - 1 >= 0) rec(L - 1, ok, false); n   long long ret = 0; n   for (map<long long, vector<long long> >::iterator it = ans.begin(); n        it != ans.end(); it++) { n     vector<long long> tbl = calc(it->first); n     for (int i = 0; i < m; i++) { n       ret += tbl[i] * it->second[i]; n     } n   } n   cout << ret << endl; n } n


#include <bits/stdc++.h> n int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } n long long pow(long long a, int p) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans *= a; n     a *= a; n     p >>= 1; n   } n   return ans; n } n int main() { n   int n, l, r; n   scanf( %d%d%d , &n, &l, &r); n   if (n == 1) { n     printf( %d n , r - l + 1); n     return 0; n   } n   if (n == 2) { n     printf( %lld n , (long long)(r - l + 1) * (r - l)); n     return 0; n   } n   if (n > 30) { n     printf( 0 n ); n     return 0; n   } n   n--; n   int M = 1; n   while (pow(M + 1, n) <= r) M++; n   long long ans = 0; n   for (int y = 1; y <= M; y++) { n     for (int x = y + 1; x <= M; x++) n       if (gcd(x, y) == 1) { n         int py = pow(y, n), px = pow(x, n); n         int L = 1, R = l / py + 1; n         int RR = r / px; n         while (L < R) { n           int M = (L + R) / 2; n           if (M * py < l) n             L = M + 1; n           else n             R = M; n         } n         if (L <= RR) ans += RR - L + 1; n       } n   } n   printf( %lld n , ans << 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long int n, m; n   cin >> n >> m; n   long long int a[n], b[m]; n   long long int aux[n]; n   for (long long int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (i == 0) n       aux[i] = a[i]; n     else n       aux[i] = aux[i - 1] + a[i]; n   } n   for (long long int i = 0; i < m; ++i) cin >> b[i]; n   long long int j = 0; n   for (long long int i = 0; i < m; ++i) { n     while (j < n && aux[j] < b[i]) ++j; n     if (j == 0) n       cout << j + 1 <<     << b[i] << endl; n     else n       cout << j + 1 <<     << b[i] - aux[j - 1] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n set<long long> bms[6]; n long long to_bit(string &s) { n   long long x = 0; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  0 ) continue; n     x |= (1LL << i); n   } n   return x; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   bms[0].insert(0); n   for (int j = 1; j <= n; j++) { n     for (int i = min(j, 5); i >= 1; i--) { n       for (long long x : bms[i - 1]) { n         bms[i].insert(x | (1LL << (j - 1))); n       } n     } n   } n   int c; n   long long x; n   string s; n   set<long long> poss; n   cin >> s >> c; n   x = to_bit(s); n   for (long long bm : bms[c]) { n     poss.insert(~(x ^ bm)); n   } n   for (int i = 1; i < m; i++) { n     cin >> s >> c; n     x = to_bit(s); n     set<long long> tposs; n     for (long long bm : bms[c]) { n       if (!poss.count(~(x ^ bm))) continue; n       tposs.insert(~(x ^ bm)); n     } n     poss = tposs; n   } n   cout << poss.size() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int g[100000], cnt[100000]; n int v[100000], b[100000]; n int s[100005], sc[100005], sz; n bool used[100005] = {}, dead[100005] = {}, marked[100005] = {}; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (int)(n); ++i) scanf( %d , g + i), --g[i], cnt[i] = 1; n   for (int i = 0; i < (int)(m); ++i) scanf( %d , v + i); n   for (int i = 0; i < (int)(m); ++i) scanf( %d , b + i); n   int prevAns = 0; n   for (int q = 0; q < (int)(m); ++q) { n     int a = (v[q] + prevAns - 1) % n; n     int ans = 0; n     int rem = b[q]; n     --rem; n     if (!used[a]) { n       ans = 1; n       used[a] = true; n     } n     while (true) { n       sz = 0; n       while (used[g[a]]) { n         if (marked[a] || dead[a]) break; n         s[sz] = a; n         marked[a] = true; n         rem -= cnt[a]; n         a = g[a]; n         ++sz; n       } n       if (marked[a] || dead[a]) { n         for (int i = 0; i < (int)(sz); ++i) dead[s[i]] = true; n         for (int i = 0; i < (int)(sz); ++i) marked[s[i]] = false; n         break; n       } n       for (int i = 0; i < (int)(sz); ++i) marked[s[i]] = false; n       for (int i = sz - 1; i >= 0; --i) { n         cnt[s[i]] += cnt[g[s[i]]]; n         g[s[i]] = g[g[s[i]]]; n       } n       rem -= cnt[a]; n       if (rem < 0) { n         break; n       } n       a = g[a]; n       used[a] = true; n       ++ans; n     } n     printf( %d n , ans); n     prevAns = ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200010; n int a[MAXN]; n int b[MAXN]; n int tmp[MAXN]; n int main() { n   int n; n   cin >> n; n   int aidx; n   for (int i = 0; i < n; i++) { n     cin >> tmp[i]; n     if (tmp[i] == 0) aidx = i; n   } n   for (int i = 0; i < n; i++) a[i] = tmp[(i + aidx) % n]; n   int bidx; n   for (int i = 0; i < n; i++) { n     cin >> tmp[i]; n     if (tmp[i] == 0) bidx = i; n   } n   for (int i = 0; i < n; i++) b[i] = tmp[(i + bidx) % n]; n   for (int i = 0; i < n - 1; i++) { n     a[i] = a[i + 1]; n     b[i] = b[i + 1]; n   } n   for (int i = 0; i < n - 1; i++) n     if (b[i] == a[0]) bidx = i; n   for (int i = 0; i < n - 1; i++) n     if (a[i] != b[(i + bidx) % (n - 1)]) { n       cout <<  NO  << endl; n       return 0; n     } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-11; n const int INF = 101e7; n template <class T> n T sqr(T first) { n   return first * first; n } n template <class T> n T abs(T first) { n   return (first < 0) ? -first : first; n } n int main() { n   int n, m, h; n   cin >> n >> m >> h; n   n--; n   int sh = 0, sum = 0; n   for (int _b = (m), i = (1); i <= _b; i++) { n     if (i == h) { n       cin >> sh; n       sh--; n     } else { n       int k; n       cin >> k; n       sum += k; n     } n   } n   if (n > sh + sum) { n     cout <<  -1 n ; n     return 0; n   } n   double c = 1; n   for (int i = 0, _n = (n); i < _n; i++) { n     c *= (double(sum) / (sum + sh)); n     sum--; n   } n   printf( %.8f n , 1 - c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long k; n   cin >> k; n   long long i = 0, prev = 0; n   while (k > prev + (9 * pow(10, i) * (i + 1))) { n     prev += 9 * pow(10, i) * (i + 1); n     i++; n   } n   k -= prev; n   long long temp; n   temp = pow(10, i) + (k / (i + 1)) - 1; n   if (k % (i + 1) != 0) { n     temp++; n   } n   if (k % (i + 1) == 0) { n     cout << temp % 10 <<   n ; n   } else { n     long long ans = temp / pow(10, i + 1 - (k % (i + 1))); n     ans %= 10; n     cout << ans <<   n ; n   } n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t N = 1e3 + 5, M = 1e9 + 7; n int64_t a[N][N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int64_t n, m, s = 0; n   cin >> n >> m; n   for (int64_t i = 0; i < n; i++) n     for (int64_t j = 0; j < m; j++) cin >> a[i][j]; n   for (int64_t i = n - 2; i; i--) n     for (int64_t j = m - 2; j; j--) n       if (!a[i][j]) a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1; n   for (int64_t i = 0; i < n; i++) n     for (int64_t j = 1; j < m; j++) n       if (a[i][j] <= a[i][j - 1]) return cout << -1, 0; n   for (int64_t j = 0; j < m; j++) n     for (int64_t i = 1; i < n; i++) n       if (a[i][j] <= a[i - 1][j]) return cout << -1, 0; n   for (int64_t i = 0; i < n; i++) n     for (int64_t j = 0; j < m; j++) s += a[i][j]; n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5; n long long line[maxn]; n int sum, q1, q2; n int flag1, k, qw, wa, flag2; n int main() { n   int n, zz = 1; n   cin >> n; n   for (int i = (int)(1); i < (int)(n + 1); i++) cin >> line[i]; n   while (zz <= n) { n     q1 = 0, q2 = 0, flag1 = 0, k = 0, wa = 0, flag2 = 0; n     sum++; n     for (int i = (int)(zz); i < (int)(n + 1); i++) { n       qw = i; n       if (line[i] != -1 && flag1 == 0) { n         q1 = line[i]; n         flag1 = 1; n         continue; n       } n       if (flag1 == 1 && line[i] == -1) { n         k++; n         continue; n       } n       if (line[i] == -1 && flag1 == 0) { n         wa++; n         continue; n       } n       if (line[i] != -1 && flag1 == 1) { n         q2 = line[i]; n         flag2 = 1; n         break; n       } n     } n     zz = qw; n     if (qw > n) break; n     if (qw == n && (flag2 == 0 || flag1 == 0)) break; n     if ((q2 - q1) % (k + 1) != 0) continue; n     long long d = (q2 - q1) / (k + 1); n     if (q1 - wa * d <= 0) continue; n     zz = qw + 1; n     while (zz <= n) { n       if (line[zz] == -1) { n         if (line[zz - 1] + d <= 0) break; n         line[zz] = line[zz - 1] + d; n         zz++; n       } else { n         if (line[zz] - line[zz - 1] != d) break; n         zz++; n       } n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a, b = 1, c, can[100000 * 10], cc, M, tmp[100000 * 10], n                    ans = 0x3f3f3f3f3f3f3f3f, XH; n inline long long cheng(long long u, long long v) { n   long long res = 0; n   for (; v; v >>= 1) { n     if (v & 1) res = (res + u) % M; n     u = u * 2 % M; n   } n   return res; n } n struct Jz { n   long long num[2][2]; n   Jz() { memset(num, 0, sizeof(num)); } n   Jz operator*(const Jz &u) const { n     Jz res; n     long long i, j, k; n     for (i = 0; i < 2; i++) { n       for (j = 0; j < 2; j++) { n         for (k = 0; k < 2; k++) { n           res.num[i][j] = (res.num[i][j] + cheng(num[i][k], u.num[k][j])) % M; n         } n       } n     } n     return res; n   } n } dw; n inline Jz po(Jz u, long long v) { n   Jz res; n   res.num[0][0] = res.num[1][1] = 1; n   for (; v;) { n     if (v & 1) res = res * u; n     u = u * u; n     v >>= 1; n   } n   return res; n } n inline long long ask(long long u) { n   Jz res = po(dw, u); n   return res.num[0][0]; n } n int main() { n   dw.num[0][1] = dw.num[1][0] = dw.num[1][1] = 1; n   long long i, j, k, t; n   cin >> n; n   t = n % 100000; n   for (i = 1;; i++) { n     if (t == a) can[++cc] = i; n     c = (a + b) % 100000; n     a = b, b = c; n     if (!a && b == 1) break; n   } n   for (M = 100000 * 10, XH = 150000; M <= (long long)1e13; M *= 10, XH *= 10) { n     t = n % M, k = 0; n     for (i = 1; i <= cc; i++) { n       for (j = 0; j <= 9; j++) { n         if (ask(can[i] + XH * j) % M == t) { n           tmp[++k] = can[i] + XH * j; n         } n       } n     } n     for (i = 1; i <= k; i++) can[i] = tmp[i]; n     cc = k; n   } n   if (!cc) { n     puts( -1 ); n     return 0; n   } n   for (i = 1; i <= cc; i++) ans = min(ans, can[i]); n   cout << ans - 1; n } n


#include <bits/stdc++.h> n using namespace std; n int store[20][1 << 20]; n struct trie { n   int k; n   struct Node { n     Node* ch[2]; n     Node() { ch[0] = ch[1] = NULL; } n   }; n   Node* root; n   trie(int k) : k(k) { root = new Node(); } n   void insert(int x) { n     Node* cur = root; n     for (int i = k - 1; i >= 0; i--) { n       int side = x >> i & 1; n       if (!cur->ch[side]) cur->ch[side] = new Node(); n       cur = cur->ch[side]; n     } n   } n   void eval() { n     vector<int> ret(1 << k); n     for (int i = 0; i <= k; i++) fill(store[i], store[i] + (1 << k), 1 << 30); n     function<vector<pair<int, int> >(Node*, int)> dfs = [&](Node* node, int h) { n       if (!node) return vector<pair<int, int> >(); n       if (h == 0) { n         vector<pair<int, int> > ret = {{0, 0}}; n         return ret; n       } n       vector<pair<int, int> > A = dfs(node->ch[0], h - 1), n                               B = dfs(node->ch[1], h - 1); n       vector<pair<int, int> > ret(1 << h); n       for (int x = 0; x < (1 << h); x++) { n         int add = 1 << (h - 1); n         int rem = x & (add - 1); n         if (x >> (h - 1) & 1) { n           ret[x].first = node->ch[1] ? B[rem].first : A[rem].first + add; n           ret[x].second = node->ch[0] ? A[rem].second + add : B[rem].second; n         } else { n           ret[x].first = node->ch[0] ? A[rem].first : B[rem].first + add; n           ret[x].second = node->ch[1] ? B[rem].second + add : A[rem].second; n         } n         if (!A.empty() && !B.empty()) { n           if (x >> (h - 1) & 1) n             store[h][x] = min(store[h][x], A[rem].first + add - B[rem].second); n           else n             store[h][x] = min(store[h][x], B[rem].first + add - A[rem].second); n         } n         assert(max(ret[x].first, ret[x].second) < (1 << h) && n                min(ret[x].first, ret[x].second) >= 0); n       } n       return ret; n     }; n     dfs(root, k); n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   trie T(k); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     T.insert(x); n   } n   T.eval(); n   for (int x = 0; x < (1 << k); x++) { n     int ans = 1 << 30; n     for (int h = k; h; h--) { n       ans = min(ans, store[h][x & ((1 << h) - 1)]); n     } n     cout << ans <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[101], x = 0; n   string s1, s2; n   cin >> s1 >> s2; n   for (int i = 0; i < s1.size(); i++) { n     if (s1[i] != s2[i]) n       a[i] = 1; n     else n       a[i] = 0; n     x++; n   } n   for (int i = 0; i < x; i++) { n     cout << a[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct app { n   int x, y; n }; n int main() { n   int n, m; n   scanf( %d , &n); n   scanf( %d , &m); n   int m1, m2; n   vector<app> arr(n); n   for (__typeof(0) i = 0; i <= n - 1; i++) { n     scanf( %d , &arr[i].x); n     scanf( %d , &arr[i].y); n     if (i == 0) { n       m1 = arr[i].x; n       m2 = arr[i].y; n     } else { n       if (arr[i].x * m2 < arr[i].y * m1) { n         m1 = arr[i].x; n         m2 = arr[i].y; n       } n     } n   } n   double ans = m * m1; n   ans = ans * 1.0 / m2 * 1.0; n   cout << fixed << setprecision(8) << ans; n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const double eps = 1e-9; n const int MOD = 1000003; n int R, C; n char *grid[600000]; n int main() { n   scanf( %d%d , &R, &C); n   for (int i = (0); i < (R); i++) { n     grid[i] = new char[C + 5]; n     scanf( %s , grid[i]); n   } n   int res = 1; n   for (int i = (0); i < (R); i++) { n     int b1 = 1, b2 = 1; n     for (int j = (0); j < (C); j++) { n       if (grid[i][j] ==  1  || grid[i][j] ==  2 ) { n         if (j % 2) { n           b1 = 0; n         } else { n           b2 = 0; n         } n       } else if (grid[i][j] ==  3  || grid[i][j] ==  4 ) { n         if (j % 2) { n           b2 = 0; n         } else { n           b1 = 0; n         } n       } n     } n     res = (res * (b1 + b2)) % MOD; n   } n   for (int j = (0); j < (C); j++) { n     int b1 = 1, b2 = 1; n     for (int i = (0); i < (R); i++) { n       if (grid[i][j] ==  1  || grid[i][j] ==  4 ) { n         if (i % 2) { n           b1 = 0; n         } else { n           b2 = 0; n         } n       } else if (grid[i][j] ==  2  || grid[i][j] ==  3 ) { n         if (i % 2) { n           b2 = 0; n         } else { n           b1 = 0; n         } n       } n     } n     res = (res * (b1 + b2)) % MOD; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long n; n   cin >> n; n   long long s = 0; n   for (int i = 0; i < n; ++i) { n     long long x; n     cin >> x; n     s += x; n   } n   long long add = (s - n * (n - 1) / 2) / n; n   long long cnt = s - (n * (n - 1) / 2 + add * n); n   for (int i = 0; i < n; ++i) { n     cout << i + add + int(i < cnt) <<    ; n   } n } n