#include <bits/stdc++.h> n using namespace std; n const int maxn = 51; n long long ans[maxn], a[maxn]; n long long poww(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a; n     b /= 2; n     a = a * a; n   } n   return res; n } n void init() { n   ans[0] = 1; n   a[0] = 1; n   for (long long i = 1; i <= 38; i++) { n     a[i] = poww(3, i); n   } n   for (long long i = 1; i <= 38; i++) { n     ans[i] = ans[i - 1] + a[i]; n   } n } n int main() { n   ios::sync_with_stdio(false); n   init(); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long m = 0; n     for (long long i = 38; i >= 1; i--) { n       if (ans[i - 1] >= n) continue; n       n -= a[i]; n       m += a[i]; n     } n     if (n > 0) { n       m += 1; n     } n     cout << m << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char a[1001][1001]; n int d[1001][1001]; n int n, m, ans; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   for (int i = n; i > 0; i--) n     for (int j = m; j > 0; j--) { n       int cur; n       d[i][j] = d[i + 1][j] + d[i][j + 1] - d[i + 1][j + 1]; n       if (a[i][j] ==  W ) n         cur = 1; n       else n         cur = -1; n       if (cur != d[i][j]) ans++; n       d[i][j] = cur; n     } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, i, p; n int main() { n   cin >> n; n   if (n < 3) { n     cout << -1; n   } else { n     for (i = n; i >= 1; i--) { n       cout << i <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t MOD = 1e9 + 7; n const int N = 2e5 + 5; n  n int64_t mpow(int64_t b,int64_t e,int64_t m){ n     int64_t r = 1; n     while(e > 0){ n         if(e & 1){ n             r = (r*b)%m; n         } n         b = (b*b)%m; n         e >>= 1; n     } n     return r; n } n  n int main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n  n     int64_t fact[N],ifact[N]; n     ifact[0] = fact[0] = 1; n     for(int i=1;i<N;i++){ n         fact[i] = (i * fact[i-1]) % MOD; n         ifact[i] = mpow(fact[i],MOD-2,MOD); n     } n     auto comb = [&](int x,int y){ // xCy n         return (fact[x]*ifact[x-y]%MOD*ifact[y])%MOD; n     }; n     int t; n     cin >> t; n     while(t--){ n         int n,l,r; n         cin >> n >> l >> r; n         int x = min(1-l,r-n); n         int L=0,R=n+1; n         int64_t res = (x*(((n%2)+1)*comb(n,n/2))%MOD)%MOD; n         res %= MOD; n         for(int i=x+1;;i++){ n             while(L+1-i < l) L++; n             while(R-1+i > r) R--; n             if(L > (n+1)/2 || n+1-R > (n+1)/2 || R-L-1 < 0) break; n             if(n%2 == 1){ n                 if(L <= n/2) res = (res+comb(R-L-1,n/2-L))%MOD; n                 if(L <= (n+1)/2) res = (res+comb(R-L-1,(n+1)/2-L))%MOD; n             }else{ n                 res = (res+comb(R-L-1,n/2-L))%MOD; n             } n         } n         cout << (res%MOD+MOD)%MOD <<   n ; n     } n }


#include <bits/stdc++.h> n using namespace std; n int nxt() { n   int x; n   cin >> x; n   return x; n } n int main() { n   int n = nxt(), k = nxt(); n   vector<int> a(n); n   generate(a.begin(), a.end(), nxt); n   int s = accumulate(a.begin(), a.end(), 0); n   int ans = 0; n   while ((s + k * ans) * 1.0 / (n + ans) < k - 0.5) { n     ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   char c; n   bool nega = 0; n   while ((!isdigit(c = getchar())) && (c !=  - )) n     ; n   if (c ==  - ) { n     nega = 1; n     c = getchar(); n   } n   x = c - 48; n   while (isdigit(c = getchar())) x = x * 10 + c - 48; n   if (nega) x = -x; n } n template <typename T> n inline void writep(T x) { n   if (x > 9) writep(x / 10); n   putchar(x % 10 + 48); n } n template <typename T> n inline void write(T x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   writep(x); n } n template <typename T> n inline void writeln(T x) { n   write(x); n   putchar(  n ); n } n int n; n int ans[100001]; n const long long inf = 2250000000000; n inline long long cal(const pair<pair<long long, long long>, long long> &first) { n   return first.first.first * first.first.first + n          first.first.second * first.first.second; n } n inline long long mul(long long first, long long second) { n   return first * first + second * second; n } n inline bool cmp(const pair<pair<long long, long long>, long long> &a, n                 const pair<pair<long long, long long>, long long> &b) { n   return cal(a) < cal(b); n } n pair<pair<long long, long long>, long long> a[100001]; n void enter() { n   read(n); n   for (int i = 0; i < n; i++) { n     read(a[i].first.first); n     read(a[i].first.second); n     a[i].second = i; n   } n   sort(a, a + n, cmp); n } n void add(long long &first, long long second) { first = first + second; } n void sub(long long &first, long long second) { first = first - second; } n void progress() { n   long long first = 0, second = 0; n   long long xx = 0, yy = 0; n   for (int i = 0; i < n; i++) { n     add(xx, a[i].first.first); n     add(yy, a[i].first.second); n     if (mul(first + a[i].first.first, second + a[i].first.second) <= n         mul(first, second)) { n       add(second, a[i].first.second); n       add(first, a[i].first.first); n       ans[a[i].second] = 1; n     } else { n       sub(second, a[i].first.second); n       sub(first, a[i].first.first); n       ans[a[i].second] = -1; n     } n   } n   while (mul(first, second) > inf) { n     for (int i = n - 1; i >= 0; i--) { n       if (ans[a[i].second] == -1 && n           mul(first + 2 * a[i].first.first, second + 2 * a[i].first.second) <= n               inf) { n         add(second, 2 * a[i].first.second); n         add(first, 2 * a[i].first.first); n         ans[a[i].second] = 1; n       } else if (ans[a[i].second] == 1 && n                  mul(first - 2 * a[i].first.first, n                      second - 2 * a[i].first.second) <= inf) { n         sub(first, 2 * a[i].first.first); n         sub(second, 2 * a[i].first.second); n         ans[a[i].second] = -1; n       } n     } n   } n } n int main() { n   enter(); n   progress(); n   for (int i = 0; i < n; i++) { n     write(ans[i]); n     putchar(   ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool visited[100005] = {0}; n vector<int> v[100005]; n int color[100005]; n int dfs(int s) { n   int result = 0; n   visited[s] = 1; n   for (int i = 0; i < v[s].size(); i++) { n     if (!visited[v[s][i]]) { n       if (color[v[s][i]] != color[s]) n         result += (1 + dfs(v[s][i])); n       else n         result += dfs(v[s][i]); n     } n   } n   return result; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < 100005; i++) visited[i] = 0; n   for (int i = 2; i <= n; i++) { n     int a; n     cin >> a; n     v[i].push_back(a); n     v[a].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     cin >> color[i + 1]; n   } n   cout << (1 + dfs(1)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(const vector<int> &next, vector<bool> &visited, vector<int> &end, n          int x) { n   visited[x] = true; n   int y = next[x]; n   if (!visited[y]) { n     dfs(next, visited, end, y); n     end[x] = end[y]; n   } else n     end[x] = x; n } n int main() { n   int n; n   scanf( %d , &n); n   vector<int> next(n), deg(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &next[i]); n     --next[i]; n     ++deg[next[i]]; n   } n   vector<int> start, end, g_end(n, -1); n   vector<bool> visited(n, false); n   for (int i = 0; i < n; ++i) n     if (deg[i] == 0) { n       start.push_back(i); n       dfs(next, visited, g_end, i); n       end.push_back(g_end[i]); n     } n   for (int i = 0; i < n; ++i) n     if (!visited[i]) { n       start.push_back(i); n       dfs(next, visited, g_end, i); n       end.push_back(g_end[i]); n     } n   int cnt = int(start.size()); n   if (cnt == 1 && deg[start[0]] > 0) n     printf( 0 n ); n   else { n     printf( %d n , cnt); n     for (int i = 0; i < cnt; ++i) n       printf( %d %d n , end[i] + 1, start[(i + 1) % cnt] + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int AL = 26; n const int N = (int)1e6 + 9; n int powr(int n, int k) { n   if (k == 0) return 1; n   int v = powr(n, k / 2); n   v = (v * 1ll * v) % MOD; n   if (k & 1) v = (v * 1ll * n) % MOD; n   return v; n } n int cnt[AL]; n int f[N]; n int inv[N]; n int C(int a, int b) { n   if (b == 0) return 1; n   int v = f[a]; n   v = (v * 1ll * inv[b]) % MOD; n   v = (v * 1ll * inv[a - b]) % MOD; n   return v; n } n int ways(string a, string b) { n   for (int j = 0; j < AL; j++) cnt[j] = 0; n   for (auto x : a) { n     cnt[x -  a ]++; n   } n   int answ = 0; n   int mlt; n   int sz = a.size(); n   int nsz; n   int y; n   bool ok; n   for (int i = 0; i < b.size(); i++) { n     nsz = sz - 1; n     y = b[i] -  a ; n     ok = false; n     for (int t = 0; t < y; t++) { n       if (cnt[t] > 0) ok = true; n     } n     if (ok) { n       mlt = 1; n       for (int r = AL - 1; r >= 0; r--) { n         mlt = (mlt * 1ll * C(nsz, cnt[r])) % MOD; n         nsz -= cnt[r]; n         nsz += (r == y); n       } n       answ = (answ + mlt) % MOD; n     } n     cnt[b[i] -  a ]--; n     if (cnt[b[i] -  a ] < 0) break; n     sz--; n   } n   return answ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   f[0] = 1; n   inv[0] = 1; n   for (int i = 1; i < N; i++) { n     f[i] = (1ll * i * f[i - 1]) % MOD; n     inv[i] = powr(f[i], MOD - 2); n   } n   string a, b; n   cin >> a >> b; n   cout << (ways(a, b) - ways(a, a) + MOD - 1) % MOD <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solution() { n   long long n, x, m = 0, ans = 0; n   cin >> n; n   ans += 2 * n - 1; n   while (n--) { n     cin >> x; n     ans += abs(x - m); n     m = x; n   } n   cout << ans <<   n ; n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   t = 1; n   while (t--) { n     solution(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     int n, r; n     cin >> n >> r; n     int arr[n]; n     for (int j = 0; j < n; j++) { n       cin >> arr[j]; n     } n     sort(arr, arr + n); n     int shots = 0, act[n], p = 0; n     act[0] = arr[0]; n     p++; n     for (int j = 1; j < n; j++) { n       if (arr[j] == arr[j - 1]) { n       } else { n         act[p] = arr[j]; n         p++; n       } n     } n     for (int j = 0; j < p; j++) { n       if (act[p - j - 1] - j * r <= 0) { n         break; n       } n       shots++; n     } n     cout << shots << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int ch = 0, nch = 0; n   int x; n   int b; n   if (n >= m) { n     b = m; n   } else { n     b = n; n   } n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x % 2 == 0) { n       ch++; n     } else { n       nch++; n     } n   } n   for (int i = 0; i < m; i++) { n     cin >> x; n     if (x % 2 == 0) { n       ch++; n     } else { n       nch++; n     } n   } n   if (nch > ch) { n     if (ch < b) { n       cout << ch << endl; n     } else { n       cout << b << endl; n     } n   } else { n     if (nch < b) { n       cout << nch << endl; n     } else { n       cout << b << endl; n     } 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   for (; b; a %= b, swap(a, b)) n     ; n   return a; n } n int n; n pair<int, int> board[1001][1001]; n int ans[1001][1001]; n int dx[5] = {0, 1, -1, 0, 0}; n int dy[5] = {0, 0, 0, 1, -1}; n bool isValid(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   memset(ans, -1, sizeof ans); n   cin >> n; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) cin >> board[i][j].first >> board[i][j].second; n   queue<pair<int, int> > q; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (board[i][j].first == i && board[i][j].second == j) { n         ans[i][j] = 0; n         q.push({i, j}); n       } n     } n   while (!q.empty()) { n     pair<int, int> cur = q.front(); n     q.pop(); n     int x = cur.first, y = cur.second; n     for (int k = 1; k <= 4; k++) { n       int cx = x + dx[k], cy = y + dy[k]; n       if (!isValid(cx, cy)) continue; n       if (ans[cx][cy] != -1) continue; n       if (board[cx][cy] != board[x][y]) continue; n       ans[cx][cy] = k; n       q.push({cx, cy}); n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (ans[i][j] == -1 && board[i][j] == pair<int, int>(-1, -1)) { n         int x = i, y = j; n         for (int k = 1; k <= 4; k++) { n           int cx = x + dx[k], cy = y + dy[k]; n           if (!isValid(cx, cy)) continue; n           if (ans[cx][cy] != -1) continue; n           if (board[cx][cy] != board[x][y]) continue; n           q.push({cx, cy}); n           q.push({x, y}); n           ans[cx][cy] = k; n           if (k == 1) n             ans[x][y] = 2; n           else if (k == 2) n             ans[x][y] = 1; n           else if (k == 3) n             ans[x][y] = 4; n           else if (k == 4) n             ans[x][y] = 3; n           while (!q.empty()) { n             pair<int, int> cur = q.front(); n             q.pop(); n             int x = cur.first, y = cur.second; n             for (int k = 1; k <= 4; k++) { n               int cx = x + dx[k], cy = y + dy[k]; n               if (!isValid(cx, cy)) continue; n               if (ans[cx][cy] != -1) continue; n               if (board[cx][cy] != board[x][y]) continue; n               ans[cx][cy] = k; n               q.push({cx, cy}); n             } n           } n           break; n         } n       } n     } n   bool isValid = true; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (ans[i][j] == -1) isValid = false; n     } n   if (!isValid) { n     cout <<  INVALID ; n     return 0; n   } n   cout <<  VALID n ; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       switch (ans[i][j]) { n         case 0: n           cout <<  X ; n           break; n         case 1: n           cout <<  U ; n           break; n         case 2: n           cout <<  D ; n           break; n         case 3: n           cout <<  L ; n           break; n         case 4: n           cout <<  R ; n           break; n       } n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 21, inf = 1e7; n int n, k, a[1 << N], pre[1 << N], ns, cs[1 << N]; n inline int cmax(int x, int y) { n   if (x == -1 || y == -1) return x == -1 ? y : x; n   return a[x] > a[y] ? x : y; n } n bool E[1 << N][N]; n void UPD(int x) { n   cs[x] = -1; n   for (int i = (0); i <= (n - 1); ++i) n     if (E[x][i]) { n       int nx = cmax(cs[x], x ^ (1 << i)); n       if (nx != cs[x]) pre[x] = x ^ (1 << i), cs[x] = x ^ (1 << i); n     } n } n int teshu[1 << N], g; n void change(int u, int v) { n   int t = u ^ v; n   t = log2(t), E[u][t] ^= 1, E[v][t] ^= 1; n } n bool op[1 << N], vis[1 << N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = (0); i <= ((1 << n) - 1); ++i) n     cin >> a[i], op[i] = op[i >> 1] ^ (i & 1), cs[i] = -1; n   for (int i = (0); i <= ((1 << n) - 1); ++i) n     if (!op[i]) n       for (int j = (0); j <= (n - 1); ++j) E[i][j] = true; n   for (int i = (0); i <= ((1 << n) - 1); ++i) n     if (!op[i]) UPD(i); n   while (k--) { n     int x = -1, r = -1; n     for (int i = (0); i <= ((1 << n) - 1); ++i) n       if (!op[i] && cs[i] != -1 && (x == -1 || a[i] + a[cs[i]] >= r)) n         x = i, r = a[i] + a[cs[i]]; n     if (x == -1 || a[x] + a[cs[x]] <= 0) break; n     ns += a[x], ns += a[cs[x]]; n     int rn = x; n     while (rn != cs[x]) change(rn, pre[rn]), rn = pre[rn]; n     a[cs[x]] = a[x] = -inf; n     teshu[++g] = x, teshu[++g] = cs[x]; n     cs[cs[x]] = -1, cs[x] = -1; n     for (int i = (1); i <= (g); ++i) { n       int x = teshu[i]; n       for (int j = (0); j <= (n - 1); ++j) { n         int v = x ^ (1 << j); n         cs[v] = -1; n         if (!op[v]) UPD(v); n       } n     } n     for (int i = (1); i <= (g); ++i) { n       int u = teshu[i]; n       if (op[u] == 1) n         cs[u] = pre[u] = -1; n       else n         pre[u] = -1, UPD(u); n     } n     for (int i = (1); i <= (g); ++i) vis[teshu[i]] = false; n     for (int i = (1); i <= (g); ++i) { n       int x = -1; n       for (int j = (1); j <= (g); ++j) n         if (!vis[teshu[j]]) x = teshu[j]; n       for (int j = (1); j <= (g); ++j) n         if (!vis[teshu[j]] && cmax(cs[x], cs[teshu[j]]) != cs[x]) x = teshu[j]; n       vis[x] = true; n       for (int j = (0); j <= (n - 1); ++j) n         if (!E[x][j]) { n           int v = x ^ (1 << j); n           int nx = cmax(cs[x], cs[v]); n           if (nx != cs[v]) pre[v] = x, cs[v] = cs[x]; n         } n     } n   } n   cout << ns <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 * 1000 + 5; n int l[N]; n int w[N]; n int getw(long long s, long long e) { n   if (e % 2) { n     if (s % 2) { n       return 2; n     } n     return 1; n   } n   if (s * 2 > e) { n     if (s % 2 == e % 2) { n       return 2; n     } n     return 1; n   } n   if (s * 4 > e) { n     return 1; n   } n   return getw(s, e / 4); n } n int getl(long long s, long long e) { n   if (s * 2 > e) { n     return 1; n   } n   return getw(s, e / 2); n } n int main() { n   int n; n   cin >> n; n   w[0] = 2, l[0] = 1; n   for (int i = 1; i <= n; i++) { n     long long s, e; n     cin >> s >> e; n     int x = getw(s, e), y = getl(s, e); n     if (x == 1) { n       w[i] = l[i - 1]; n     } else { n       w[i] = w[i - 1]; n     } n     if (y == 1) { n       l[i] = l[i - 1]; n     } else { n       l[i] = w[i - 1]; n     } n   } n   cout << (w[n] == 1) <<     << (l[n] == 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 10 + 10; n int minn0[M][M]; n int minn[M][M]; n int dig[M]; n int dignum; n int fac(int x) { n   int ans = 1; n   for (int i = 1; i <= x; i++) { n     ans = ans * i; n   } n   return ans; n } n int ex(int a, int x) { n   int ans = 1; n   for (int i = 1; i <= x; i++) { n     ans = ans * a; n   } n   return ans; n } n void mluckyinn0() { n   for (int n = 1; n <= 9; n++) { n     for (int m = 0; m <= n; m++) { n       minn0[0][m] = 1; n       int nchm = fac(n) / fac(m) / fac(n - m); n       int lucky = ex(2, m); n       int unlucky = ex(8, n - m); n       minn0[n][m] = nchm * lucky * unlucky; n     } n   } n } n void mluckyinn() { n   minn[1][0] = 7; n   minn[1][1] = 2; n   for (int n = 2; n <= 9; n++) { n     for (int m = 0; m <= n; m++) { n       int t1 = 0; n       if (m - 1 >= 0 && n - 1 >= 0) { n         t1 = minn0[n - 1][m - 1]; n       } n       minn[n][m] = 2 * t1 + 7 * minn0[n - 1][m]; n     } n   } n } n void caldig(int x) { n   int idx = 0; n   while (x > 0) { n     dig[dignum - idx] = x % 10; n     x = x / 10; n     idx++; n   } n } n int lnum[M]; n long long int mod = 1000000000 + 7; n int main() { n   int m; n   cin >> m; n   mluckyinn0(); n   mluckyinn(); n   int tmp = m; n   while (tmp > 0) { n     dignum++; n     tmp /= 10; n   } n   caldig(m); n   for (int d = 1; d < dignum; d++) { n     for (int j = 0; j <= d; j++) { n       lnum[j] = lnum[j] + minn[d][j]; n     } n   } n   int pre = 0; n   int fi = 0; n   for (int i = 1; i <= dignum; i++) { n     int st = 0; n     if (i == 1) { n       st = 1; n     } n     int y47 = 0; n     int n47 = 0; n     for (int j = st; j < dig[i]; j++) { n       if (j == 4 || j == 7) { n         y47++; n       } else { n         n47++; n       } n     } n     int flag = 0; n     if (y47 > 0) { n       flag = 1; n     } n     int ndg = dignum - i; n     for (int j = 0; j <= ndg; j++) { n       lnum[pre + j] = lnum[pre + j] + minn0[ndg][j] * n47; n       lnum[pre + j + flag] = lnum[pre + j + flag] + minn0[ndg][j] * y47; n     } n     if (dig[i] == 4 || dig[i] == 7) { n       pre++; n     } n   } n   lnum[pre]++; n   long long int ans = 0; n   int sta[M]; n   for (int i1 = 0; i1 <= 9; i1++) { n     sta[1] = lnum[i1]; n     lnum[i1]--; n     for (int i2 = 0; i2 <= 9; i2++) { n       sta[2] = lnum[i2]; n       lnum[i2]--; n       for (int i3 = 0; i3 <= 9; i3++) { n         sta[3] = lnum[i3]; n         lnum[i3]--; n         for (int i4 = 0; i4 <= 9; i4++) { n           sta[4] = lnum[i4]; n           lnum[i4]--; n           for (int i5 = 0; i5 <= 9; i5++) { n             sta[5] = lnum[i5]; n             lnum[i5]--; n             for (int i6 = 0; i6 <= 9; i6++) { n               sta[6] = lnum[i6]; n               lnum[i6]--; n               int st = i1 + i2 + i3 + i4 + i5 + i6; n               for (int i7 = st + 1; i7 <= 9; i7++) { n                 sta[7] = lnum[i7]; n                 lnum[i7]--; n                 long long int val = 1; n                 for (int j = 1; j <= 7; j++) { n                   if (sta[j] <= 0) { n                     val = 0; n                     break; n                   } else { n                     val = (val * sta[j]) % mod; n                   } n                 } n                 ans = (ans + val) % mod; n                 lnum[i7]++; n               } n               lnum[i6]++; n             } n             lnum[i5]++; n           } n           lnum[i4]++; n         } n         lnum[i3]++; n       } n       lnum[i2]++; n     } n     lnum[i1]++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   long long k; n   cin >> k; n   long long sum = 0; n   map<long long, long long> m; n   vector<long long> ssum(k, 0), g(5005 * k, -1); n   vector<vector<long long>> a(k); n   vector<pair<long long, long long>> v; n   for (long long i = 0; i < k; ++i) { n     long long n; n     cin >> n; n     a[i].resize(n); n     for (long long j = 0; j < n; ++j) { n       cin >> a[i][j]; n       m[a[i][j]] = v.size(); n       v.emplace_back(a[i][j], i); n       ssum[i] += a[i][j]; n     } n     sum += ssum[i]; n   } n   if (sum % k) { n     cout <<  No n ; n     return 0; n   } n   sum /= k; n   for (long long i = 0; i < k; ++i) { n     for (long long j = 0; j < (long long)a[i].size(); ++j) { n       if (m.count(sum - ssum[i] + a[i][j])) { n         long long id = m[sum - ssum[i] + a[i][j]], u = m[a[i][j]]; n         if (u != id && v[u].second == v[id].second) continue; n         g[u] = id; n       } n     } n   } n   long long timer = 0; n   vector<long long> dp(1 << k, 0), par(1 << k, 0), head(1 << k, -1); n   vector<long long> vis(v.size() + 5, 0); n   for (long long i = 0; i < k; ++i) { n     for (auto j : a[i]) { n       long long mask = 0, u = m[j]; n       ++timer; n       if (vis[u] || g[u] == -1) continue; n       while (~u && !vis[u]) { n         vis[u] = timer; n         u = g[u]; n       } n       if (u == -1 || vis[u] != timer) continue; n       long long c = 0, x = u, good = 1; n       while (x != u || !c) { n         if ((mask >> v[x].second) & 1) good = 0; n         mask |= (1 << v[x].second); n         x = g[x]; n         ++c; n       } n       if (good) { n         dp[mask] = 1; n         head[mask] = u; n       } n     } n   } n   dp[0] = 1; n   for (long long i = 0; i < (1 << k); ++i) { n     for (long long s = i; s; s = (s - 1) & i) { n       if (dp[s] && dp[i ^ s]) { n         dp[i] = 1; n         par[i] = s; n       } n     } n   } n   if (!dp[(1 << k) - 1]) { n     cout <<  No n ; n     return 0; n   } n   vector<pair<long long, long long>> ans(k); n   function<void(long long)> getans = [&](long long mask) { n     if (~head[mask]) { n       long long u = head[mask], x = g[u]; n       ans[v[x].second].first = v[x].first; n       ans[v[x].second].second = v[u].second + 1; n       while (u != x) { n         ans[v[g[x]].second].first = v[g[x]].first; n         ans[v[g[x]].second].second = v[x].second + 1; n         x = g[x]; n       } n     } else { n       getans(par[mask]); n       getans(mask ^ par[mask]); n     } n   }; n   getans((1 << k) - 1); n   cout <<  Yes n ; n   for (long long i = 0; i < k; ++i) n     cout << ans[i].first <<     << ans[i].second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, len; n   int i = 1; n   cin >> n; n   cin >> k; n   vector<int> a; n   while (n > 1) { n     if (n % (i + 1) == 0) { n       a.push_back(i + 1); n       n = n / (i + 1); n       i -= 1; n     } n     i += 1; n   } n   len = a.size() - k; n   if (len >= 0) { n     for (int i = 0; i < len; i++) { n       a[len + k - i - 2] = a[len + k - i - 1] * a[len + k - i - 2]; n       a.pop_back(); n     } n     for (int i = 0; i < a.size(); i++) { n       cout << a[i] <<    ; n     } n   } else { n     cout << -1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long ar[100010]; n unordered_map<long long, long long> um; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, n, x, ans, y, prev; n   scanf( %lld , &t); n   long long sm = 0; n   while (t--) { n     scanf( %lld , &n); n     scanf( %lld , &x); n     for (int i = 0; i < n; i++) { n       scanf( %lld , &ar[i]); n     } n     sm = 0; n     prev = ans = -1; n     for (long long i = 0; i < n; i++) { n       sm = (sm + ar[i]) % x; n       if (sm && prev == -1) prev = i; n       if (sm) n         ans = i + 1; n       else if (prev != -1) n         ans = max(ans, i - prev); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long INF = 2e18; n constexpr long long MOD = 1e9 + 7; n constexpr long long MAXN = 2e5 + 3; n struct Matrix { n   static constexpr long long MOD = 1e9 + 7; n   static constexpr long long MAXN = 200, MAXM = 200; n   array<array<long long, MAXM>, MAXN> mat = {}; n   long long n, m; n   Matrix(const long long n, const long long m) : n(n), m(m) {} n   static long long mod(long long n) { n     n %= MOD; n     if (n < 0) n += MOD; n     return n; n   } n   Matrix identity() { n     assert(n == m); n     Matrix mat_identity(n, m); n     for (long long i = 0; i < n; ++i) mat_identity.mat[i][i] = 1; n     return mat_identity; n   } n   Matrix operator*(const Matrix &other) const { n     assert(m == other.n); n     Matrix ans(n, other.m); n     for (long long i = 0; i < n; ++i) n       for (long long j = 0; j < m; ++j) n         for (long long k = 0; k < m; ++k) n           ans.mat[i][j] = mod(ans.mat[i][j] + mat[i][k] * other.mat[k][j]); n     return ans; n   } n   Matrix expo(long long p) { n     assert(p >= 0); n     Matrix ans = identity(), cur_power(n, m); n     cur_power.mat = mat; n     while (p) { n       if (p & 1) ans = ans * cur_power; n       cur_power = cur_power * cur_power; n       p >>= 1; n     } n     return ans; n   } n }; n void solve() { n   long long n; n   cin >> n; n   long long b, k, x; n   cin >> b >> k >> x; n   vector<long long> fq(10); n   for (long long i = 0; i < n; ++i) { n     long long x; n     cin >> x; n     ++fq[x]; n   } n   Matrix mat(x, x); n   for (long long j = 0; j < x; ++j) n     for (long long d = 0; d <= 9; ++d) mat.mat[(10 * j + d) % x][j] += fq[d]; n   mat = mat.expo(b); n   Matrix ini(x, 1); n   ini.mat[0][0] = 1; n   mat = mat * ini; n   cout << mat.mat[k].front() <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n inline int Read() { n   int x(0); n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) x = (x << 3) + (x << 1) + c -  0 , c = getchar(); n   return x; n } n const int maxn = 1e5 + 9, mod = 998244353, maxm = 409; n int n, k, ans; n int a[maxn], cnt[maxn], lst[maxn], fir[maxn], bl[maxm], br[maxm], col[maxn], n     v[maxn], lazy[maxm], f[maxn], sum[maxm][maxn]; n inline int G(int x) { n   while (x > mod) x -= mod; n   return x; n } n inline void Fir() { n   int size(sqrt(n)), pieces(ceil(1.0 * n / size)); n   for (int i = 1; i < pieces; ++i) { n     bl[i] = (i - 1) * size + 1; n     br[i] = i * size; n     for (int j = bl[i]; j <= br[i]; ++j) col[j] = i; n   } n   bl[pieces] = (pieces - 1) * size + 1; n   br[pieces] = n; n   for (int j = bl[pieces]; j <= br[pieces]; ++j) col[j] = pieces; n   for (int i = 1; i <= n; ++i) { n     lst[i] = fir[a[i]]; n     fir[a[i]] = i; n   } n } n inline void Modify(int l, int r, int val) { n   int lt(col[l]), rt(col[r]); n   if (lt == rt) { n     for (int i = l; i <= r; ++i) { n       if (val == 1) { n         if (v[i] + lazy[lt] == k) ans = G(ans - f[i] + mod); n       } else { n         if (v[i] + lazy[lt] == k + 1) ans = G(ans + f[i]); n       } n       sum[lt][v[i]] = G(sum[lt][v[i]] - f[i] + mod), v[i] += val, n       sum[lt][v[i]] = G(sum[lt][v[i]] + f[i]); n     } n   } else { n     for (int i = l; i <= br[lt]; ++i) { n       if (val == 1) { n         if (v[i] + lazy[lt] == k) ans = G(ans - f[i] + mod); n       } else { n         if (v[i] + lazy[lt] == k + 1) ans = G(ans + f[i]); n       } n       sum[lt][v[i]] = G(sum[lt][v[i]] - f[i] + mod), v[i] += val, n       sum[lt][v[i]] = G(sum[lt][v[i]] + f[i]); n     } n     for (int i = bl[rt]; i <= r; ++i) { n       if (val == 1) { n         if (v[i] + lazy[rt] == k) ans = G(ans - f[i] + mod); n       } else { n         if (v[i] + lazy[rt] == k + 1) ans = G(ans + f[i]); n       } n       sum[rt][v[i]] = G(sum[rt][v[i]] - f[i] + mod), v[i] += val, n       sum[rt][v[i]] = G(sum[rt][v[i]] + f[i]); n     } n     for (int i = lt + 1; i <= rt - 1; ++i) { n       if (val == 1) n         ans = G(ans - sum[i][k - lazy[i]] + mod); n       else n         ans = G(ans + sum[i][k + 1 - lazy[i]]); n       lazy[i] += val; n     } n   } n } n inline void Solve() { n   ans = f[1] = sum[1][0] = 1; n   for (int i = 1; i <= n; ++i) { n     ++cnt[a[i]]; n     int p(lst[i]), q(lst[p]); n     if (cnt[a[i]] == 1) n       Modify(1, i, 1); n     else if (cnt[a[i]] == 2) { n       Modify(p + 1, i, 1); n       Modify(1, p, -1); n     } else { n       Modify(p + 1, i, 1); n       Modify(q + 1, p, -1); n     } n     f[i + 1] = ans; n     sum[col[i + 1]][v[i + 1]] = G(sum[col[i + 1]][v[i + 1]] + f[i + 1]); n     ans = G(ans + f[i + 1]); n   } n   printf( %d , f[n + 1]); n } n int main() { n   n = Read(); n   k = Read(); n   for (int i = 1; i <= n; ++i) a[i] = Read(); n   Fir(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void REV(T *a, int n) { n   reverse(a + 1, a + 1 + n); n } n template <class T> n void SRT(T *a, int n) { n   sort(a + 1, a + 1 + n); n } n template <class T> n int UNI(T *a, int n) { n   sort(a + 1, a + 1 + n); n   return unique(a + 1, a + 1 + n) - (a + 1); n } n template <class T> n int POS(T *a, int n, T v) { n   return lower_bound(a + 1, a + 1 + n, v) - a; n } n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(long long &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const long long &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.12f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.F); n   putchar(   ); n   _W(x.S); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n template <typename T> n void cmin(T &x, T y) { n   if (y < x) x = y; n } n template <typename T> n void cmax(T &x, T y) { n   if (y > x) x = y; n } n long long sum; n int n; n int a[200005]; n int cnt[64]; n void purin_solve() { n   R(sum, n); n   memset((cnt), 0, sizeof((cnt))); n   long long cursum = 0; n   for (int i = (1); i <= (n); ++i) { n     long long x; n     R(x); n     cursum += x; n     int c = -1; n     while (x) { n       ++c; n       x >>= 1; n     } n     ++cnt[c]; n   } n   if (cursum < sum) do { n       puts( -1 ); n       return; n     } while (false); n   ; n   long long SUM = 0; n   int ans = 0; n   for (int i = 0; i < 64; ++i) { n     if (SUM >= (1LL << i)) { n       SUM -= (1LL << i); n       ++cnt[i]; n     } n     if (sum >> i & 1) { n       int j; n       for (j = i; j < 64; ++j) { n         if (cnt[j]) break; n       }; n       if (j == 64) do { n           puts( -1 ); n           return; n         } while (false); n       while (j > i) { n         --cnt[j]; n         --j; n         cnt[j] += 2; n         ++ans; n       } n       assert(cnt[i]); n       --cnt[i]; n     } n     while (cnt[i]) { n       SUM += (1LL << i); n       --cnt[i]; n     } n   } n   W(ans); n } n int main() { n   cout << fixed << setprecision(12); n   cerr << fixed << setprecision(12); n   int t = 1; n   scanf( %d , &t); n   while (t--) purin_solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int k; n   cin >> k; n   if (k > 36) n     cout << -1; n   else { n     for (int i = 0; i < k / 2; i++) cout << 8; n     if (k % 2) cout << 4; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char N[(int)1e6 + 5], B[(int)1e6 + 5]; n long long n, b, c; n inline long long euler(long long n) { n   long long ans = n; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       ans = ans / i * (i - 1); n       while (n % i == 0) n /= i; n     } n   } n   if (n > 1) ans = ans / n * (n - 1); n   return ans; n } n long long qpow(long long x, long long n, long long mod) { n   if (n == 0) return 1; n   long long ans = qpow(x, n >> 1, mod); n   ans = ans * ans % mod; n   if (n & 1) ans = ans * x % mod; n   return ans % mod; n } n int main() { n   cin >> B; n   cin >> N; n   int xx = strlen(N); n   int yy = strlen(B); n   cin >> c; n   n = 0; n   long long u = euler(c); n   if (xx < 10) { n     for (int i = 0; i < xx; i++) n = n * 10 + N[i] -  0 ; n     n--; n   } else { n     for (int i = 0; i < xx; i++) { n       n = n * 10 + N[i] -  0 ; n       n = n % u; n     } n     n = ((n - 1) + u) % u; n     n += u; n   } n   for (int i = 0; i < yy; i++) { n     b = b * 10 + B[i] -  0 ; n     b = b % c; n   } n   long long ans = (b - 1 + c) % c; n   ans = ans * qpow(b, n, c) % c; n   if (!ans) n     cout << c << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n double dp[100111], ans; n int sz[100111], ds[100111]; n std::list<int> edge[100111]; n int i, j, n; n void dfs(int now, int fa) { n   ds[now] = ds[fa] + 1; n   ans += 1 / (double)ds[now]; n   for (std::list<int>::const_iterator it(edge[now].begin()); n        it != edge[now].end(); ++it) n     if (*it != fa) dfs(*it, now); n } n int main() { n   scanf( %d , &n); n   while (--n) { n     scanf( %d %d , &i, &j); n     edge[i].push_back(j); n     edge[j].push_back(i); n   } n   dfs(1, 0); n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, jj = 0; n long long a[22], b[22]; n long long dp[10000][1000]; n long long memo(int idx, int cnt) { n   long long res = (long long)(1e18); n   if (idx == 2 * n - 1) { n     if (cnt == 1) res = b[((idx + jj) % n)]; n     return dp[idx][cnt] = res; n   } n   if (dp[idx][cnt] != -1) return dp[idx][cnt]; n   if (cnt > 0) res = b[((idx + jj) % n)] + memo(idx + 1, cnt - 1); n   res = min(res, a[((idx + jj) % n)] + memo(idx + 1, cnt + 1)); n   return dp[idx][cnt] = res; n } n int main() { n   memset(dp, -1, sizeof dp); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   long long mn = memo(0, 0); n   long long ans = mn; n   for (jj = 1; jj < n; jj++) { n     memset(dp, -1, sizeof dp); n     mn = min(memo(0, 0), mn); n   } n   cout << (m / 2 - 1) * mn + ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> leftSub[1000005]; n vector<int> rightSub[1000005]; n int leftSon[1000005], rightSon[1000005]; n int maxVal[1000005], minVal[1000005]; n int leftMax[1000005]; n bool vis[1000005]; n bool possible; n int n, c, a, b; n string d; n int cnt; n void findDfs(int u) { n   if (vis[u]) return; n   vis[u] = true; n   maxVal[u] = minVal[u] = u; n   int lmax = 0, rmax = 0; n   int lmin = n + 1, rmin = n + 1; n   int Lmax = 0, Rmin = n + 1; n   for (int i = 0; i < leftSub[u].size(); ++i) { n     int v = leftSub[u][i]; n     lmax = max(lmax, v); n     lmin = min(lmin, v); n     findDfs(v); n     Lmax = max(Lmax, maxVal[v]); n     maxVal[u] = max(maxVal[u], maxVal[v]); n     minVal[u] = min(minVal[u], minVal[v]); n   } n   for (int i = 0; i < rightSub[u].size(); ++i) { n     int v = rightSub[u][i]; n     rmax = max(rmax, v); n     rmin = min(rmin, v); n     findDfs(v); n     Rmin = min(Rmin, minVal[v]); n     maxVal[u] = max(maxVal[u], maxVal[v]); n     minVal[u] = min(minVal[u], minVal[v]); n   } n   if (lmin <= u || lmax >= rmin) { n     possible = false; n     return; n   } n   if (Lmax >= Rmin) { n     possible = false; n     return; n   } n   leftMax[u] = Lmax; n } n void findBoundary() { n   memset(vis, false, sizeof(vis)); n   for (int i = n; i >= 1; --i) { n     if (!vis[i]) { n       findDfs(i); n     } n   } n } n void dfs(int l, int r) { n   ++cnt; n   int lmin = n + 1, lmax = 0; n   int rmin = n + 1, rmax = 0; n   if (leftSub[l].size()) { n     leftSon[l] = l + 1; n     for (int i = 0; i < leftSub[l].size(); ++i) { n       int v = leftSub[l][i]; n       lmin = min(lmin, v); n       lmax = max(lmax, v); n     } n     if (lmin <= l) { n       possible = false; n       return; n     } n     dfs(l + 1, lmax); n   } n   if (rightSub[l].size()) { n     for (int i = 0; i < rightSub[l].size(); ++i) { n       int v = rightSub[l][i]; n       rmin = min(rmin, v); n       rmax = max(rmax, v); n     } n     if (rmin <= cnt) { n       possible = false; n       return; n     } n     if (leftSub[l].size()) { n       rightSon[l] = cnt + 1; n       dfs(cnt + 1, max(r, rmax)); n     } else { n       rightSon[l] = l + 1; n       dfs(l + 1, max(r, rmax)); n     } n   } else { n     if (l == r) return; n     int leftBound = max(l, cnt); n     if (leftBound < r) { n       rightSon[l] = leftBound + 1; n       dfs(leftBound + 1, r); n     } n   } n } n void print(int u) { n   if (leftSon[u]) { n     print(leftSon[u]); n   } n   cout << u <<    ; n   if (rightSon[u]) { n     print(rightSon[u]); n   } n } n void init() { n   memset(leftSon, 0, sizeof(leftSon)); n   memset(rightSon, 0, sizeof(rightSon)); n   possible = true; n   cnt = 0; n   for (int i = 1; i <= n; ++i) { n     leftMax[i] = 0; n   } n } n int main() { n   while (cin >> n >> c) { n     for (int i = 0; i < c; ++i) { n       cin >> a >> b >> d; n       if (d[0] ==  L ) { n         leftSub[a].push_back(b); n       } else { n         rightSub[a].push_back(b); n       } n     } n     init(); n     dfs(1, n); n     if (!possible) { n       puts( IMPOSSIBLE ); n     } else { n       print(1); n       puts(  ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MOD = 998244353; n void add(int& a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n } n int mul(int a, int b) { return ll(a) * b % MOD; } n int modinv(int a, int m = MOD) { n   return a == 1 ? a : m - modinv(m % a, a) * ll(m) / a; n } n const int MAXV = 4242; n int p2[MAXV], invp2[MAXV]; n int p2m1[MAXV], invp2m1[MAXV]; n void precomp() { n   p2[0] = 1; n   for (int i = 1; i < MAXV; i++) { n     p2[i] = mul(2, p2[i - 1]); n   } n   for (int i = 0; i < MAXV; i++) { n     invp2[i] = modinv(p2[i]); n   } n   for (int i = 0; i < MAXV; i++) { n     p2m1[i] = p2[i] - 1; n   } n   for (int i = 1; i < MAXV; i++) { n     invp2m1[i] = modinv(p2m1[i]); n   } n } n const int MAXN = 3010; n int N, C; n int A[MAXN]; n int main() { n   precomp(); n   scanf( %d %d , &N, &C); n   for (int i = 0; i < N; i++) { n     scanf( %d , &A[i]); n     A[i]--; n   } n   vector<vector<int>> dp(N + 1); n   dp[0] = {{1}}; n   if (C > 10) { n     for (int i = 0; i < N; i++) { n       vector<int> freq(C); n       int prod = 1; n       int cnt_bad = C - 1; n       for (int j = i; j >= 0; j--) { n         if (A[i] != A[j]) { n           if (!freq[A[j]]) { n             cnt_bad--; n           } else { n             prod = mul(prod, invp2m1[freq[A[j]]]); n           } n           freq[A[j]]++; n           prod = mul(prod, p2m1[freq[A[j]]]); n         } n         if (cnt_bad == 0) { n           if (dp[i + 1].size() < dp[j].size() + 1) { n             dp[i + 1].resize(dp[j].size() + 1); n           } n           for (int l = 0; l < int(dp[j].size()); l++) { n             add(dp[i + 1][l + 1], mul(prod, dp[j][l])); n           } n         } n       } n     } n   } else { n     vector<vector<int>> dp2(1 << C); n     dp2[0] = {{1}}; n     for (int i = 0; i < N; i++) { n       for (int m = (1 << C) - 1; m >= 0; m--) { n         if (dp2[m | (1 << A[i])].size() < dp2[m].size()) { n           dp2[m | (1 << A[i])].resize(dp2[m].size()); n         } n         for (int l = 0; l < int(dp2[m].size()); l++) { n           add(dp2[m | (1 << A[i])][l], dp2[m][l]); n         } n       } n       swap(dp[i + 1], dp2[(1 << C) - 1]); n       dp[i + 1].insert(dp[i + 1].begin(), 0); n       if (dp2[0].size() < dp[i + 1].size()) { n         dp2[0].resize(dp[i + 1].size()); n       } n       for (int l = 0; l < int(dp[i + 1].size()); l++) { n         add(dp2[0][l], dp[i + 1][l]); n       } n     } n   } n   vector<int> ans(N + 1); n   for (int i = 0; i <= N; i++) { n     for (int l = 0; l < int(dp[i].size()); l++) { n       add(ans[l], mul(p2[N - i], dp[i][l])); n     } n   } n   ans[0]--; n   for (int i = 0; i < N; i++) { n     add(ans[i], MOD - ans[i + 1]); n   } n   for (int i = 0; i <= N; i++) { n     printf( %d%c , ans[i],    n [i == N]); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ); n using namespace std; n const int MAX_ARRAY = 100005; n string num2str(int i) { n   stringstream ss; n   ss << i; n   return ss.str(); n } n std::vector<std::string> split(std::string str, std::string pattern) { n   std::string::size_type pos; n   std::vector<std::string> result; n   str += pattern; n   int size = str.size(); n   for (int i = 0; i < size; i++) { n     pos = str.find(pattern, i); n     if (pos < size) { n       std::string s = str.substr(i, pos - i); n       result.push_back(s); n       i = pos + pattern.size() - 1; n     } n   } n   return result; n } n bool DesCmp(int a, int b) { return a > b; } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cout << (j - 1) * n + ((i + j - 1) % n) + 1 <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void start() {} n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long a, b; n     cin >> a >> b; n     if (a < b) swap(a, b); n     b = 2 * b; n     if (b >= a) n       cout << b * b << endl; n     else n       cout << a * a << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string str; n     cin >> str; n     long long count = 0, count2 = 0; n     for (int i = 0; i < n; i++) { n       if (str[i] ==  < ) { n         count++; n       } else { n         break; n       } n     } n     for (int i = n - 1; i >= 0; i--) { n       if (str[i] ==  > ) { n         count2++; n       } else { n         break; n       } n     } n     cout << min(count, count2) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char a[n][7]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 7; j++) cin >> a[i][j]; n   } n   int ans = 0, ct = 0; n   for (int j = 0; j < 7; j++) { n     ct = 0; n     for (int i = 0; i < n; i++) { n       if (a[i][j] ==  1 ) ct++; n     } n     ans = max(ans, ct); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n inline long long read() { n   long long x = 0, w = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = getchar(); n   } n   while (c <=  9  && c >=  0 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return w == 1 ? x : -x; n } n const int N = 1e5 + 10; n int a[N], n; n int main() { n   int _ = read(); n   while (_--) { n     long long x1 = read(), y1 = read(); n     long long x2 = read(), y2 = read(); n     if (x1 == x2 || y1 == y2) { n       puts( 1 ); n       continue; n     } n     long long ans = (x2 - x1) * (y2 - y1) + 1; n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(time(0)); n uniform_int_distribution<int> uid(-1e9, 1e9); n vector<int> g[100002]; n int d[100002], u[100002], k; n long long res, w[505], v[505]; n int find(int x, int px, int sz) { n   for (auto& i : g[x]) n     if (i != px && !u[i] && d[i] > sz / 2) return find(i, x, sz); n   return x; n } n int dfs(int x, int px) { n   d[x] = 1; n   for (auto& i : g[x]) n     if (i != px && !u[i]) d[x] += dfs(i, x); n   return d[x]; n } n void dfs1(int x, int px, int dx) { n   if (dx <= k) ++w[dx]; n   for (auto& i : g[x]) n     if (i != px && !u[i]) dfs1(i, x, dx + 1); n } n void solve(int x) { n   int y = find(x, x, dfs(x, x)); n   u[y] = 1; n   fill(v, v + k + 1, 0); n   for (auto& i : g[y]) { n     if (!u[i]) { n       fill(w, w + k + 1, 0); n       dfs1(i, i, 1); n       res += w[k]; n       for (int i = 1; i < k; ++i) res += w[i] * v[k - i]; n       for (int i = 0; i < k; ++i) v[i] += w[i]; n     } n   } n   for (auto& i : g[y]) n     if (!u[i]) solve(i); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, x, y; n   cin >> n >> k; n   for (int i = 0; i < n - 1; ++i) { n     cin >> x >> y; n     g[--x].push_back(--y); n     g[y].push_back(x); n   } n   solve(0); n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, len, dp[200][100][2][2]; n char x[200]; n const int unDEF = 10000, INF = 1000000000; n int solve(int idx, int left, int dir, int isM) { n   if (idx < 0) { n     if (left + dir == 0) { n       return 0; n     } n     if (isM) { n       return -INF; n     } n     return INF; n   } n   int &temp = dp[idx][left][dir][isM]; n   if (temp != unDEF) { n     return temp; n   } n   if (isM) { n     temp = -INF; n   } else n     temp = INF; n   int sign = dir ? -1 : 1; n   for (int i = 0; i <= left; i++) { n     int newDir = (x[idx] !=  T ) ^ (i & 1); n     if (newDir) { n       if (isM) { n         temp = max(temp, solve(idx - 1, left - i, dir, isM) + sign); n       } else { n         temp = min(temp, solve(idx - 1, left - i, dir, isM) + sign); n       } n     } else { n       if (isM) { n         temp = max(temp, solve(idx - 1, left - i, dir ^ 1, isM)); n       } else { n         temp = min(temp, solve(idx - 1, left - i, dir ^ 1, isM)); n       } n     } n   } n   return temp; n } n int main() { n   cin >> x >> n; n   len = strlen(x); n   for (int i = 0; i < len; i++) { n     for (int j = 0; j <= n; j++) { n       for (int k = 0; k < 2; k++) { n         for (int l = 0; l < 2; l++) { n           dp[i][j][k][l] = unDEF; n         } n       } n     } n   } n   int ans = 0, a = solve(len - 1, n, 0, 0), b = solve(len - 1, n, 0, 1), n       c = solve(len - 1, n, 1, 0), d = solve(len - 1, n, 1, 1); n   if (a < 1000) ans = max(ans, abs(a)); n   if (c < 1000) ans = max(ans, abs(c)); n   if (b > -1000) ans = max(ans, abs(b)); n   if (d > -1000) ans = max(ans, abs(d)); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n unsigned int a[100000 + 5], b[100000 + 5], p; n int f(int x) { return ~((x - 1) >> 31); } n int main() { n   int n, t, l, r, k, i; n   long long ans = 0; n   for (scanf( %d , &n); n--;) { n     scanf( %d%d%d , &t, &l, &r); n     if (t == 1) { n       scanf( %d , &k); n       p = k > 0 ? k - 1 : -k - 1; n       if (k > 0) n         for (i = l; i < r; ++i) a[i] = (a[i] - 1 < p ? a[i] - 1 : p) + 1; n       else n         for (i = l; i < r; ++i) b[i] = (b[i] - 1 < p ? b[i] - 1 : p) + 1; n     } else { n       for (ans = 0, i = l; i < r; ++i) ans += (a[i] + b[i]) & f(a[i]) & f(b[i]); n       printf( %I64d n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n long long powm(long long a, long long b, long long mod) { 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 bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < (n); i++) { n     cin >> a[i]; n   }; n   if (n == 1) { n     cout <<  YES n ; n     cout << 1 <<     << a[0] << endl; n     return; n   } n   long long x = 0, y = 0, m = 0; n   for (long long i = 0; i < (n - 1); i++) { n     m = max({m, a[i], a[i + 1]}); n     if (abs(a[i] - a[i + 1]) == 0) { n       cout <<  NO n ; n       return; n     } n     if (abs(a[i] - a[i + 1]) != 1 and y == 0) { n       y = abs(a[i] - a[i + 1]); n     } else if (abs(a[i] - a[i + 1]) != 1 and y != 0) { n       if (y != abs(a[i] - a[i + 1])) { n         cout <<  NO n ; n         return; n       } n     } n   } n   if (y == 0) { n     cout <<  YES n ; n     cout << (mod) <<     << (mod) << endl; n     return; n   } n   for (long long i = 0; i < (n - 1); i++) { n     if (abs(a[i] - a[i + 1]) == 1) { n       if (a[i] % y == 0 and a[i + 1] == a[i] + 1) { n         cout <<  NO n ; n         return; n       } n       if (a[i + 1] % y == 0 and a[i] == a[i + 1] + 1) { n         cout <<  NO n ; n         return; n       } n     } n   } n   cout <<  YES n ; n   cout << (m / y) + 1 <<     << y << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n long long a[100001]; n int main() { n   int n; n   scanf( %d , &n); n   int i, j, k; n   int m = n / 2; n   for (i = 1; i < n; i += 2) { n     scanf( %I64d , &a[i]); n   } n   long long cur = 100000000000001LL; n   for (i = n - 1; i >= 1; i -= 2) { n     bool ok = false; n     for (k = 2 - a[i] % 2; (long long)(k)*k <= a[i]; k += 2) { n       if (a[i] % k) continue; n       long long miden = a[i] / k; n       if ((miden - k) % 2) continue; n       long long upper = (a[i] / k) + (k - 1); n       if (upper + 2 < cur) { n         ok = true; n         long long lower = (a[i] / k) - (k - 1); n         a[i + 1] = ((upper + 2) + (cur - 2)) * ((cur - (upper + 2)) / 2) / 2; n         cur = lower; n         break; n       } n     } n     if (!ok) { n       break; n     } n   } n   a[0] = (1 + (cur - 2)) * ((cur - 1) / 2) / 2; n   if (a[0] > 0 && i < 1) { n     puts( Yes ); n     for (i = 0; i < n; i++) { n       printf( %I64d%c , a[i],    n [i == n - 1]); n     } n   } else { n     puts( No ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n using namespace std; n const int N = 20; n pair<double, double> p1[N], p2[N]; n int n, m; n bool tag; n int p[N], vis[N]; n bool ok(pair<double, double> a, pair<double, double> b, pair<double, double> c, n         pair<double, double> d) { n   if (!(min(a.first, b.first) <= max(c.first, d.first) && n         min(c.second, d.second) <= max(a.second, b.second) && n         min(c.first, d.first) <= max(a.first, b.first) && n         min(a.second, b.second) <= max(c.second, d.second))) n     return 0; n   double u, v, w, z; n   u = (c.first - a.first) * (b.second - a.second) - n       (b.first - a.first) * (c.second - a.second); n   v = (d.first - a.first) * (b.second - a.second) - n       (b.first - a.first) * (d.second - a.second); n   w = (a.first - c.first) * (d.second - c.second) - n       (d.first - c.first) * (a.second - c.second); n   z = (b.first - c.first) * (d.second - c.second) - n       (d.first - c.first) * (b.second - c.second); n   return (u * v <= 0.00000001 && w * z <= 0.00000001); n } n void dfs(int now) { n   bool flag; n   if (tag) return; n   if (now > n) { n     flag = 1; n     for (int i = 1; i <= n; i++) n       for (int j = i + 1; j <= n; j++) n         if (!ok(p1[i], p2[p[i]], p1[j], p2[p[j]])) return; n     tag = 1; n     return; n   } n   for (int i = 1; i <= m; i++) n     if (!vis[i]) { n       p[now] = i; n       dfs(now + 1); n     } n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) scanf( %lf%lf , &p1[i].first, &p1[i].second); n   for (int i = 1; i <= m; i++) scanf( %lf%lf , &p2[i].first, &p2[i].second); n   if (n != m) { n     puts( No ); n     return 0; n   } n   tag = 0; n   dfs(1); n   if (tag) n     puts( Yes ); n   else n     puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int BITS(long long __a) { n   short int __c = 0; n   __a <<= 1; n   while (__a) __c += ((__a >>= 1) & 1); n   return __c; n } n inline bool prime(int n) { n   for (int i = 3; i * i <= n; i += 2) n     if (n % i == 0) return false; n   return true; n } n set<int> s; n int N, M, V[100010], A[100010]; n int main() { n   cin >> N >> M; n   for (int i = 0; i < N; i++) cin >> V[i]; n   s.insert(V[N - 1]); n   A[N - 1] = 1; n   for (int i = N - 2; i >= 0; i--) { n     s.insert(V[i]); n     A[i] = ((int)s.size()); n   } n   for (int i = 0; i < M; i++) { n     int j; n     cin >> j; n     cout << A[j - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100100]; n char s1[111]; n char s2[110]; n int main() { n   scanf( %s%s%s , s, s1, s2); n   int flag1, flag2; n   int n = strlen(s); n   int n1 = strlen(s1); n   int n2 = strlen(s2); n   flag1 = flag2 = 0; n   int i, j; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n1; j++) { n       if (s[i + j] != s1[j]) break; n     } n     if (j == n1) { n       break; n     } n   } n   i += n1; n   for (; i < n; i++) { n     for (j = 0; j < n2; j++) { n       if (s[i + j] != s2[j]) break; n     } n     if (j == n2) { n       flag1 = 1; n       break; n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < n2; j++) { n       if (s[i + n2 - j - 1] != s2[j]) break; n     } n     if (j == n2) { n       break; n     } n   } n   i += n2; n   for (; i < n; i++) { n     for (j = 0; j < n1; j++) { n       if (s[i + n1 - j - 1] != s1[j]) break; n     } n     if (j == n1) { n       flag2 = 1; n       break; n     } n   } n   if (flag1 == 0 && flag2 == 0) { n     printf( fantasy n ); n   } n   if (flag1 == 0 && flag2 == 1) { n     printf( backward n ); n   } n   if (flag1 == 1 && flag2 == 0) { n     printf( forward n ); n   } n   if (flag1 == 1 && flag2 == 1) { n     printf( both n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, sum = 0; n   cin >> n >> d; n   vector<pair<int, int> > ins(n); n   for (int i = 0; i < n; i++) { n     cin >> ins[i].first; n     ins[i].second = i + 1; n   } n   sort(ins.begin(), ins.end()); n   int i = 0; n   while (i < n and ((sum + ins[i].first) <= d)) { n     sum += ins[i].first; n     i++; n   } n   cout << i << endl; n   for (int j = 0; j < i; j++) { n     if (j > 0) cout <<    ; n     cout << ins[j].second; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[1050000]; n int n, k; n double p[25]; n double ans[25]; n int main() { n   memset(dp, 0, sizeof(dp)); n   memset(ans, 0, sizeof(ans)); n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %lf , p + i); n   dp[0] = 1; n   int st = (1 << n); n   for (int i = 1; i < st; i++) { n     double sp = 0; n     int o = 0; n     for (int j = 0; j < n; j++) { n       if (i & 1 << j) n         o++; n       else n         sp += p[j]; n     } n     if (o > k) continue; n     for (int j = 0; j < n; j++) n       if ((i & 1 << j) && p[j] > 0.000000001) { n         dp[i] += dp[i - (1 << j)] * p[j] / (sp + p[j]); n         ans[j] += dp[i - (1 << j)] * p[j] / (sp + p[j]); n       } n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> st; n const int N = 1e6 + 5; n int a[3000]; n int c1 = 0, c2; n string s, c; n int na, nb, m, ans = 0, mx = 0; n int main() { n   int n, a, b; n   cin >> n; n   while (n--) { n     cin >> a >> b; n     if (a == 2) { n       cout << b / a << endl; n       continue; n     } n     if (a == 1) { n       cout << b << endl; n       continue; n     } n     if (a % 2 == 1) { n       cout << b / (a - a / 2) << endl; n     } else n       cout << b / (a - (a / 2 - 1)) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long> myset; n int base; n int N; n long long A[100005]; n map<long long, int> Map; n int ANS[600000]; n int T[1200000]; n int Lo[300000]; n int ql, qu; n int P[300000]; n void update(int ind) { n   ind += base; n   T[ind] = ind - base; n   while (1) { n     ind /= 2; n     if (ind < 1) break; n     T[ind] = T[2 * ind]; n     if (ANS[T[2 * ind + 1]] > ANS[T[ind]]) T[ind] = T[2 * ind + 1]; n   } n } n int query(int ind, int cl, int cu) { n   if (cu < ql || cl > qu) return -1; n   if (cl >= ql && cu <= qu) return T[ind]; n   int ansl = query(2 * ind, cl, (cl + cu) / 2); n   int ansr = query(1 + 2 * ind, 1 + (cl + cu) / 2, cu); n   if (ansl == -1 || ansr == -1) return (ansl + ansr + 1); n   if (ANS[ansl] >= ANS[ansr]) return ansl; n   return ansr; n } n int main() { n   int n; n   int d; n   cin >> n >> d; n   for (__typeof((__typeof(n))0) i((__typeof(n))0); i < n; i++) { n     scanf( %lld , &A[i]); n     myset.insert(A[i]); n     myset.insert(A[i] - d); n     myset.insert(A[i] + d); n   } n   int c = 0; n   for (__typeof(myset.begin()) it(myset.begin()); it != myset.end(); it++) n     Map[*it] = c++; n   N = c; n   base = 1; n   while (base < N) base *= 2; n   for (int i = base; i < 2 * base; i++) T[i] = i - base; n   for (int i = base - 1; i > 0; i--) { n     T[i] = T[2 * i]; n     if (ANS[T[2 * i + 1]] > ANS[T[i]]) T[i] = T[2 * i + 1]; n   } n   for (int i = 0; i < n; i++) { n     long long h = A[i]; n     int x = Map[h]; n     int ans = -1; n     qu = Map[h - d]; n     ql = 0; n     ans = query(1, 0, base - 1); n     ql = Map[h + d]; n     qu = N - 1; n     int ans2 = query(1, 0, base - 1); n     if (ans == -1 || ANS[ans] < ANS[ans2]) ans = ans2; n     if (ans == -1 || ANS[ans] == 0) { n       ANS[x] = 1; n       P[i] = -1; n     } else { n       ANS[x] = 1 + ANS[ans]; n       P[i] = Lo[ans]; n     } n     update(x); n     Lo[x] = i; n   } n   int p = 0; n   for (int i = 0; i < N; i++) n     if (ANS[i] > ANS[p]) p = i; n   p = Lo[p]; n   vector<int> TP; n   while (p >= 0) { n     TP.push_back(p + 1); n     p = P[p]; n   } n   reverse(TP.begin(), TP.end()); n   cout << (int)(TP.size()) << endl; n   for (__typeof(TP.begin()) i(TP.begin()); i != TP.end(); i++) n     cout << *i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x1, y1, x2, y2; n   long long int answ = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x1 >> y1 >> x2 >> y2; n     answ += (x2 - x1 + 1) * (y2 - y1 + 1); n   } n   cout << answ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[10][10]; n double me[10][10]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n = 10; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> arr[i][j]; n     } n   } n   me[0][0] = 0.; n   me[0][1] = 6.; n   me[0][2] = 6.; n   me[0][3] = 6.; n   me[0][4] = 6.; n   me[0][5] = 6.; n   double fix = 1. / 6.; n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0) { n       for (int j = (i == 0) ? 6 : 0; j < n; j++) { n         double best = 1e6; n         for (int k = 0; k <= (1 << 6) - 1; k++) { n           double summ = 1; n           for (int s = 0; s < 6; s++) { n             if (((k >> s) & 1) == 1) { n               int posx = (j - s - 1 >= 0) n                              ? j - s - 1 n                              : n - (((j - s - 1) % 10 + 10) % 10) - 1; n               int posy = (j - s - 1 >= 0 || i == 0) ? i : i - 1; n               summ += fix * me[posy - arr[posy][posx]][posx]; n             } else { n               int posx = (j - s - 1 >= 0) n                              ? j - s - 1 n                              : n - (((j - s - 1) % 10 + 10) % 10) - 1; n               int posy = (j - s - 1 >= 0 || i == 0) ? i : i - 1; n               summ += fix * me[posy][posx]; n             } n           } n           best = min(best, summ); n         } n         me[i][j] = best; n       } n     } else { n       for (int j = n - 1; j >= 0; j--) { n         double best = 1e6; n         for (int k = 0; k <= (1 << 6) - 1; k++) { n           double summ = 1; n           for (int s = 0; s < 6; s++) { n             if (((k >> s) & 1) == 1) { n               int posx = (j + s + 1 < n) ? j + s + 1 n                                          : n - ((j + s + 1) % 10 + 10) % 10 - 1; n               int posy = (j + s + 1 < n || i == 0) ? i : i - 1; n               summ += fix * me[posy - arr[posy][posx]][posx]; n             } else { n               int posx = (j + s + 1 < n) ? j + s + 1 n                                          : n - ((j + s + 1) % 10 + 10) % 10 - 1; n               int posy = (j + s + 1 < n || i == 0) ? i : i - 1; n               summ += fix * me[posy][posx]; n             } n           } n           best = min(best, summ); n         } n         me[i][j] = best; n       } n     } n   } n   cout.precision(9); n   cout << fixed << me[9][0]; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1000006; n const long long inf = 1e18; n long long n, m, p, a[MX], ca[MX], b[MX], cb[MX], mn[MX]; n long long x[MX], y[MX], z[MX]; n long long st[4 * MX + 20], lz[4 * MX + 20], res = -inf; n vector<int> pos[MX]; n void push(int i, int j, int pos) { n   if (i < j) { n     lz[pos * 2] += lz[pos]; n     lz[pos * 2 + 1] += lz[pos]; n   } n   st[pos] += lz[pos]; n   lz[pos] = 0; n } n void update(int i, int j, int pos, int a, int b, long long k) { n   if (lz[pos]) push(i, j, pos); n   if (j < a || b < i) return; n   if (a <= i && j <= b) { n     lz[pos] = k; n     push(i, j, pos); n     return; n   } n   int m = (i + j) / 2; n   update(i, m, pos * 2, a, b, k); n   update(m + 1, j, pos * 2 + 1, a, b, k); n   st[pos] = max(st[pos * 2], st[pos * 2 + 1]); n } n long long query(int i, int j, int pos, int a, int b) { n   if (lz[pos]) push(i, j, pos); n   if (j < a || b < i) return -inf; n   if (a <= i && j <= b) return st[pos]; n   int m = (i + j) / 2; n   return max(query(i, m, pos * 2, a, b), query(m + 1, j, pos * 2 + 1, a, b)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> p; n   fill(mn, mn + MX, inf); n   for (int i = 1; i <= n; i++) { n     cin >> a[i] >> ca[i]; n     mn[a[i]] = min(mn[a[i]], ca[i]); n   } n   for (int i = 1; i < MX; i++) update(1, MX - 1, 1, i, i, -mn[i]); n   fill(mn, mn + MX, inf); n   for (int i = 1; i <= m; i++) { n     cin >> b[i] >> cb[i]; n     mn[b[i]] = min(mn[b[i]], cb[i]); n   } n   for (int i = 1; i <= p; i++) { n     cin >> x[i] >> y[i] >> z[i]; n     pos[y[i]].push_back(i); n   } n   for (int i = 1; i < MX - 1; i++) { n     for (int j : pos[i - 1]) update(1, MX - 1, 1, x[j] + 1, MX - 1, z[j]); n     push(1, MX - 1, 1); n     res = max(res, st[1] - mn[i]); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9; n const long long INFL = 2e18; n const int N = 1e6, M = N; n int n, m, G = 0; n vector<int> adj[N + 5]; n int gr[N + 5], p[N + 5]; n vector<int> pth[N + 5]; n vector<int> PTH_myEndlessLove; n int Pos[N + 5]; n void dfs(int u) { n   gr[u] = G; n   PTH_myEndlessLove.push_back(u); n   for (int i = (0), _b = (((int)(adj[u]).size())); i < _b; ++i) { n     int v = adj[u][i]; n     if (gr[v]) continue; n     dfs(v); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = (1), _b = (n); i <= _b; ++i) cin >> p[i]; n   int u, v; n   while (m--) { n     cin >> u >> v; n     adj[p[u]].push_back(p[v]); n     adj[p[v]].push_back(p[u]); n   } n   for (int i = (1), _b = (n); i <= _b; ++i) n     if (!gr[p[i]]) { n       PTH_myEndlessLove.clear(); n       ++G; n       dfs(p[i]); n       sort(PTH_myEndlessLove.begin(), PTH_myEndlessLove.end()); n       reverse(PTH_myEndlessLove.begin(), PTH_myEndlessLove.end()); n       pth[G] = PTH_myEndlessLove; n     } n   for (int i = (1), _b = (n); i <= _b; ++i) { n     cout << pth[gr[p[i]]][Pos[gr[p[i]]]] <<    ; n     ++Pos[gr[p[i]]]; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[10005]; n char b[1000005]; n vector<int> poj[10005]; n int alen, blen; n long long total; n int pos; n vector<int>::iterator it; n int main() { n   scanf( %s , a); n   scanf( %s , b); n   alen = strlen(a), blen = strlen(b); n   pos = 0; n   total = 1; n   for (int i = 0; i <= alen - 1; i++) { n     poj[a[i] -  a ].push_back(i); n   } n   for (int i = 0; i <= 25; i++) { n     sort(poj[i].begin(), poj[i].end()); n   } n   for (int i = 0; i <= blen - 1; i++) { n     if (poj[b[i] -  a ].empty()) { n       printf( -1 n ); n       return 0; n     } n     it = lower_bound(poj[b[i] -  a ].begin(), poj[b[i] -  a ].end(), pos); n     if (it == poj[b[i] -  a ].end()) { n       it = poj[b[i] -  a ].begin(); n       total++; n     } n     pos = (*it) + 1; n   } n   printf( %d n , total); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n{0}, a{0}, m{0}, s{0}; n   cin >> n; n   for (long long int i = 1; i <= n; i++) { n     cin >> a; n     s += a; n     m = max(m, a); n   } n   if (s % 2 == 1 || s < 2 * m) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0xffffff; n int LEN; n class Mcmf { n  public: n   struct T { n     int from, to, val; n     int next, cost; n   } edge[200001]; n   int len; n   int visit[200001], pre[200001], dist[200001], que[200001], vis[200001], n       pos[200001]; n   void init() { n     memset(vis, -1, sizeof(vis)); n     len = 0; n   } n   void add(int from, int to, int val, int cost) { n     edge[len].from = from, edge[len].to = to, edge[len].val = val, n     edge[len].cost = cost; n     edge[len].next = vis[from], vis[from] = len++; n     edge[len].from = to, edge[len].to = from, edge[len].val = 0, n     edge[len].cost = -cost; n     edge[len].next = vis[to], vis[to] = len++; n   } n   int SPFA(int s, int t, int n) { n     int i, to, k; n     for (i = 0; i <= n; i++) { n       pre[i] = -1, visit[i] = 0; n     } n     int head, tail; n     head = tail = 0; n     for (i = 0; i <= n; i++) dist[i] = inf; n     que[tail++] = s, pre[s] = s, dist[s] = 0, visit[s] = 1; n     while (head != tail) { n       int from = que[head++]; n       visit[from] = 0; n       for (k = vis[from]; k != -1; k = edge[k].next) { n         to = edge[k].to; n         if (edge[k].val > 0 && dist[from] + edge[k].cost < dist[to]) { n           dist[to] = dist[from] + edge[k].cost; n           pre[to] = from; n           pos[to] = k; n           if (!visit[to]) { n             visit[to] = 1; n             que[tail++] = to; n           } n         } n       } n     } n     if (pre[t] != -1 && dist[t] < inf) return 1; n     return 0; n   } n   int mnCostFlow(int s, int t, int n) { n     if (s == t) { n     } n     int flow = 0, cost = 0; n     while (SPFA(s, t, n)) { n       int from, mn = inf; n       for (from = t; from != s; from = pre[from]) n         mn = min(mn, edge[pos[from]].val); n       flow += mn; n       cost += dist[t] * mn; n       for (from = t; from != s; from = pre[from]) { n         edge[pos[from]].val -= mn; n         edge[pos[from] ^ 1].val += mn; n       } n     } n     if (flow != LEN) return -1; n     return cost; n   } n }; n int n, q; n char s[10010]; n int cnt[27]; n int tmp[27]; n Mcmf mcf; n int main() { n   mcf.init(); n   scanf( %s , s); n   LEN = strlen(s); n   for (int i = 0; i < LEN; ++i) { n     cnt[s[i] -  a ]++; n   } n   int len; n   scanf( %d , &q); n   int st = q + 26, en = st + 1, num; n   for (int i = 0; i < q; ++i) { n     scanf( %s , s); n     len = strlen(s); n     memset(tmp, 0, sizeof(tmp)); n     for (int k = 0; k < len; ++k) ++tmp[s[k] -  a ]; n     for (int k = 0; k < 26; ++k) mcf.add(i, q + k, tmp[k], 0); n     scanf( %d , &num); n     mcf.add(st, i, num, i + 1); n   } n   for (int i = 0; i < 26; ++i) mcf.add(q + i, en, cnt[i], 0); n   int ans = mcf.mnCostFlow(st, en, en + 1); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n unsigned int dp[100010][400]; n vector<int> nima[100010]; n int a[100010], b[100010]; n int n, m, s, e; n int solve(int all = s, int c = e) { n   unsigned int INF = -1; n   vector<int> vt[100010]; n   int cur = 0; n   memset(dp, -1, sizeof(dp)); n   int cnt = (all + c - 1) / c; n   for (int i = 0; i < m; ++i) { n     vt[b[i]].push_back(i + 1); n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     dp[cur][0] = 0; n     cur ^= 1; n     memset(dp[cur], -1, sizeof(dp[cur])); n     for (int j = 1; j < cnt + 1; ++j) { n       unsigned int pre = dp[cur ^ 1][j - 1]; n       unsigned int p = n           upper_bound(vt[a[i]].begin(), vt[a[i]].end(), pre) - vt[a[i]].begin(); n       if (p == vt[a[i]].size()) n         p = INF; n       else n         p = vt[a[i]][p]; n       dp[cur][j] = min(dp[cur ^ 1][j], p); n       if (dp[cur ^ 1][j] > p && p + i + j * c <= all) ans = max(ans, j); n     } n   } n   return ans; n } n int main() { n   while (cin >> n >> m >> s >> e) { n     memset(dp, -1, sizeof(dp)); n     for (int i = 0; i < n; ++i) { n       scanf( %d , &a[i]); n     } n     for (int i = 0; i < m; ++i) { n       scanf( %d , &b[i]); n       nima[b[i]].push_back(i + 1); n     } n     int ans = 0; n     int cnt = s / e; n     for (int i = 0; i < n; ++i) { n       dp[i][0] = 0; n       if (nima[a[i]].size()) n         dp[i][1] = nima[a[i]][0]; n       else n         dp[i][1] = -1; n       if (i == 0) continue; n       for (int j = 1; j <= cnt; ++j) { n         unsigned int now = -1; n         int w = a[i]; n         int last = dp[i - 1][j - 1]; n         now = n             upper_bound(nima[w].begin(), nima[w].end(), last) - nima[w].begin(); n         if (now == nima[w].size() || dp[i - 1][j - 1] == -1) { n           now = -1; n         } else { n           now = nima[w][now]; n         } n         dp[i][j] = min(dp[i - 1][j], now); n       } n     } n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < cnt + 1; ++j) { n         if (dp[i][j] != -1) { n           if (i + dp[i][j] + 1 + j * e <= s) ans = max(ans, j); n         } n       } n     cout << ans << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[11][5]; n bool ok(int l, int j, int k) { n   int i, an = 0; n   for (i = 0; i < 5; i++) { n     an = an + (a[j][i] - a[l][i]) * (a[k][i] - a[l][i]); n   } n   if (an > 0) return false; n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   int i, j, k, n; n   cin >> n; n   if (n > 11) { n     n *= 5; n     while (n--) cin >> i; n     cout << 0; n   } else { n     for (i = 0; i < n; i++) n       cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3] >> a[i][4]; n     vector<int> v; n     k = n; n     j = n; n     for (i = 0; i < n; i++) { n       k = n; n       for (j = 0; j < n && k == n; j++) n         for (k = j + 1; k < n; k++) { n           if (!ok(i, j, k)) { n             break; n           } n         } n       if (j == n && k == n) v.push_back(i + 1); n     } n     cout << v.size() << endl; n     for (i = 0; i < v.size(); i++) cout << v[i] <<     << endl; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mx = 70, Mod = 1e9 + 7, mxx = 350000; n int n; n int get(int x, int y, int z) { n   if (x > y) swap(x, y); n   if (x > z) swap(x, z); n   if (y > z) swap(y, z); n   return x * mx * mx + y * mx + z; n } n bool mark[mxx]; n vector<int> e[mxx]; n int dp[mxx]; n int par[mxx]; n short d[mx][mx]; n vector<pair<int, int> > res; n void connect(int u, int v) { n   e[u].push_back(v); n   e[v].push_back(u); n } n void bfs(int v) { n   mark[v] = 1; n   vector<int> vc; n   vc.clear(); n   vc.push_back(v); n   for (int i = 0; i < vc.size(); i++) n     for (int u : e[v = vc[i]]) n       if (!mark[u]) mark[u] = 1, vc.push_back(u), par[u] = v; n } n void move(int u, int v) { n   short a[3], b[3]; n   bool ma[3], mb[3]; n   for (int i = 0; i < 3; i++) { n     a[i] = u % mx; n     b[i] = v % mx; n     u /= mx; n     v /= mx; n     ma[i] = 0; n     mb[i] = 0; n   } n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) n       if (a[i] == b[j]) ma[i] = 1, mb[j] = 1; n   for (int i = 0; i < 3; i++) { n     if (!ma[i]) u = a[i]; n     if (!mb[i]) v = b[i]; n   } n   res.push_back(make_pair(u + 1, v + 1)); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int x, y, z, u; n   cin >> n >> x >> y >> z; n   x--, y--, z--; n   string s; n   for (int i = 0; i < n; i++) { n     cin >> s; n     for (int j = 0; j < n; j++) d[i][j] = s[j] -  a  + 1; n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       for (int k = j + 1; k < n; k++) { n         u = get(i, j, k); n         for (int v = min(min(i, j), k) + 1; v < n; v++) n           if (v != i && v != j && v != k) { n             if (d[v][i] == d[j][k] && v > i) connect(u, get(v, j, k)); n             if (d[v][j] == d[i][k] && v > j) connect(u, get(v, i, k)); n             if (d[v][k] == d[i][j] && v > k) connect(u, get(v, i, j)); n           } n       } n     } n   } n   u = get(x, y, z); n   if (u == get(0, 1, 2)) return cout <<  0 n , 0; n   bfs(get(0, 1, 2)); n   if (!par[u]) return cout <<  -1 , 0; n   while (par[u]) { n     move(u, par[u]); n     u = par[u]; n   } n   cout << res.size() <<   n ; n   for (pair<int, int> p : res) cout << p.first <<     << p.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n long long a[maxn]; n long long fac[maxn], inv[maxn]; n const int mod = 1e9 + 7; n long long Pow(long long x, long long y) { n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return res; n } n long long C(int n, int m) { n   if (n < m) return 0; n   return fac[n] * inv[n - m] % mod * inv[m] % mod; n } n int main() { n   fac[0] = inv[0] = 1; n   for (int i = 1; i < maxn; i++) { n     fac[i] = 1ll * fac[i - 1] * i % mod; n     inv[i] = Pow(fac[i], mod - 2); n   } n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), a[i] %= mod; n   if (n <= 2) { n     cout << (a[1] + a[2]) % mod << endl, exit(0); n   } n   if (n % 2 != 0) { n     for (int i = 1; i < n; i++) { n       if (i % 2 != 0) n         a[i] = (a[i] + a[i + 1]) % mod; n       else n         a[i] = (a[i] - a[i + 1]) % mod; n     } n     n -= 1; n   } n   long long ans1 = 0, ans2 = 0; n   for (int i = 1; i <= n; i += 2) { n     ans1 = (ans1 + C(n / 2 - 1, (i - 1) / 2) * a[i] % mod) % mod; n     ans2 = (ans2 + C(n / 2 - 1, (i - 1) / 2) * a[i + 1] % mod) % mod; n   } n   if ((1ll * n * (n - 1) / 2) % 2 == 0) n     cout << (ans1 - ans2 + mod) % mod << endl; n   else n     cout << (ans1 + ans2 + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream &operator<<(ostream &os, vector<T> &vec) { n   os <<  { ; n   for (int i = (0), __i = (((int)(vec).size())); i < __i; i++) { n     os << vec[i]; n     if (i + 1 != ((int)(vec).size())) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <class T1, class T2> n ostream &operator<<(ostream &os, pair<T1, T2> &par) { n   os <<  (  << par.first <<  ,  << par.second <<  ) ; n   return os; n } n const int dim = 110000; n char s[dim]; n char s1[dim]; n int p[dim]; n int a[dim]; n int a1[dim]; n long long r1[dim]; n long long r2[dim]; n long long r3[dim]; n int main() { n   scanf( %s , s); n   int n = strlen(s); n   for (int i = (0), __i = (n); i < __i; i++) s1[i] = s[n - 1 - i]; n   s1[n] = 0; n   p[0] = -1; n   int g = -1; n   for (int i = (1), __i = (n); i < __i; i++) { n     while (g != -1 && s1[g + 1] != s1[i]) { n       g = p[g]; n     } n     if (s1[g + 1] == s1[i]) ++g; n     p[i] = g; n   } n   g = -1; n   a[0] = 0; n   a1[0] = 0; n   for (int i = (0), __i = (n); i < __i; i++) { n     while (g != -1 && s1[g + 1] != s[i]) { n       g = p[g]; n     } n     if (s1[g + 1] == s[i]) ++g; n     if (a[i] > g + 1) { n       a1[i + 1] = a1[i]; n     } else { n       a1[i + 1] = i - g; n     } n     a[i + 1] = max(a[i], g + 1); n   } n   r1[0] = 0; n   r2[0] = 0; n   r3[0] = 1; n   long long cc = 52152551; n   for (int i = (0), __i = (n); i < __i; i++) { n     r1[i + 1] = r1[i] + s[i] * r3[i]; n     r2[i + 1] = r2[i] + s1[i] * r3[i]; n     r3[i + 1] = r3[i] * cc; n   } n   int ans = 0; n   int i1, j1; n   int i2, j2; n   for (int i = (0), __i = (n); i < __i; i++) { n     int l = 0, r = n; n     while (r - l > 1) { n       int mid = (l + r) / 2; n       if (i - mid >= 0 && i + mid < n && n           (r1[i + 1] - r1[i - mid]) * r3[n - i - mid - 1] == n               (r2[n - i] - r2[n - i - mid - 1]) * r3[i - mid]) { n         l = mid; n       } else { n         r = mid; n       } n     } n     int t = min(a[i - l], n - i - l - 1); n     int ans1 = t * 2 + l * 2 + 1; n     if (ans < ans1) { n       ans = ans1; n       i1 = a1[i - l]; n       j1 = t; n       i2 = i - l; n       j2 = l * 2 + 1; n     } n   } n   if (j1 == 0) { n     printf( 1 n ); n     printf( %d %d n , i2 + 1, j2); n   } else { n     printf( 3 n ); n     printf( %d %d n , i1 + 1, j1); n     printf( %d %d n , i2 + 1, j2); n     printf( %d %d n , n - j1 + 1, j1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int df[26]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   int mini = s.length(); n   for (int i = 0; i < s.length(); i++) { n     df[s[i] -  A ]++; n   } n   for (int i = 0; i < k; i++) mini = min(mini, df[i]); n   cout << k * mini <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, i; n   cin >> n; n   int a[n], b[n + 1]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     b[a[i]] = i; n   } n   int l, r, ans[n + 1]; n   l = b[1]; n   r = b[1]; n   for (i = 2; i <= n; i++) { n     l = min(l, b[i]); n     r = max(r, b[i]); n     if ((r - l + 1) != i) n       ans[i] = 0; n     else n       ans[i] = 1; n   } n   cout << 1; n   for (i = 2; i <= n; i++) cout << ans[i]; n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace FGF { n int n, m; n const int N = 2e6 + 5; n const int mo = 1e9 + 7; n char s[N]; n long long a[N], pw[N], cnt[N]; n long long ans; n void FWTxor(long long *y, int op) { n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < (1 << n); j += (1 << i)) n       for (int k = 0; k < (1 << (i - 1)); k++) { n         long long c = (y[j | k] + y[j | k | (1 << (i - 1))]) % mo, n                   d = (y[j | k] - y[j | k | (1 << (i - 1))] + mo) % mo; n         y[j | k] = c * op % mo, y[j | k | (1 << (i - 1))] = d * op % mo; n       } n } n void work() { n   scanf( %d , &n); n   scanf( %s , s); n   pw[0] = 1; n   for (int i = 0; i < (1 << n); i++) a[i] = s[i] -  0 ; n   for (int i = 1; i <= n; i++) pw[i] = 2LL * pw[i - 1] % mo; n   for (int i = 1; i < (1 << n); i++) cnt[i] = cnt[i >> 1] + (i & 1); n   FWTxor(a, 1); n   for (int i = 0; i < (1 << n); i++) a[i] = a[i] * a[i] % mo; n   FWTxor(a, (mo + 1) / 2); n   for (int i = 0; i < (1 << n); i++) n     ans = (ans + 1LL * a[i] * pw[n - cnt[i]]) % mo; n   printf( %lld , ans * 3 % mo); n } n }  // namespace FGF n int main() { n   FGF::work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 2e4 + 5; n int sum[55][N], a[55][N]; n int lazy[N << 2], tree[N << 2]; n int dp[55][N]; n void pushdown(int root) { n   if (lazy[root]) { n     tree[root << 1] += lazy[root]; n     tree[root << 1 | 1] += lazy[root]; n     lazy[root << 1] += lazy[root]; n     lazy[root << 1 | 1] += lazy[root]; n     lazy[root] = 0; n   } n } n void update(int root, int l, int r, int L, int R, int val) { n   if (L <= l && r <= R) { n     tree[root] += val; n     lazy[root] += val; n     return; n   } n   pushdown(root); n   int mid = (l + r) >> 1; n   if (L <= mid) update(root << 1, l, mid, L, R, val); n   if (R > mid) update(root << 1 | 1, mid + 1, r, L, R, val); n   tree[root] = max(tree[root << 1], tree[root << 1 | 1]); n } n int getsum(int x1, int y1, int x2, int y2) { n   return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1]; n } n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; n     } n   } n   for (int i = 1; i <= m; i++) { n     dp[1][i] = getsum(1, i, min(2, n), min(m, i + k - 1)); n   } n   for (int i = 2; i <= n; i++) { n     memset(lazy, 0, sizeof lazy); n     memset(tree, 0, sizeof tree); n     for (int j = 1; j <= m; j++) { n       update(1, 1, m, j, j, dp[i - 1][j]); n     } n     for (int j = 1; j <= k; j++) { n       update(1, 1, m, 1, j, -a[i][j]); n     } n     for (int j = 1; j <= m; j++) { n       dp[i][j] = tree[1] + getsum(i, j, min(n, i + 1), min(m, j + k - 1)); n       update(1, 1, m, max(j - k + 1, 1), j, a[i][j]); n       if (j + k <= m) { n         update(1, 1, m, j + 1, j + k, -a[i][j + k]); n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= m; i++) { n     ans = max(ans, dp[n][i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 707406378; n const int maxn = 1.5e7 + 10; n void read(long long &x) { n   char ch; n   bool flag = 0; n   for (ch = getchar(); !isdigit(ch) && ((flag |= (ch ==  - )) || 1); n        ch = getchar()) n     ; n   for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar()) n     ; n   x *= 1 - 2 * flag; n } n int main() { n   long long n, x, y; n   cin >> n; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     ans = max(ans, x + y); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int p = 998244353; n const int inv2 = 499122177; n const int MAXN = 140000; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   do x = (x * 10 + ch - 48) % p, ch = getchar(); n   while (ch >=  0  && ch <=  9 ); n   return (x * f % p + p) % p; n } n int n, k, r[MAXN]; n long long c[MAXN] = {1, 6, 1}; n long long s[MAXN], f[MAXN], g[MAXN]; n long long fastpow(long long a, int b) { n   long long res = 1; n   a %= p; n   while (b) { n     if (b & 1) res = res * a % p; n     a = a * a % p; n     b >>= 1; n   } n   return res; n } n void NTT(long long *a, int N) { n   for (int i = 0; i < N; i++) n     if (i < r[i]) std::swap(a[i], a[r[i]]); n   for (int n = 2, m = 1; n <= N; m = n, n <<= 1) { n     long long g1 = fastpow(3, (p - 1) / n); n     for (int l = 0; l < N; l += n) { n       long long g = 1, t1, t2; n       for (int i = l; i < l + m; i++) { n         t1 = a[i], t2 = g * a[i + m] % p; n         a[i] = (t1 + t2) % p; n         a[i + m] = (t1 - t2 + p) % p; n         g = g * g1 % p; n       } n     } n   } n   return; n } n void INTT(long long *a, int N) { n   NTT(a, N); n   std::reverse(a + 1, a + N); n   int invN = fastpow(N, p - 2); n   for (int i = 0; i < N; i++) a[i] = a[i] * invN % p; n } n long long a1[MAXN], b1[MAXN]; n void Inv(long long *a, long long *b, int n) { n   if (n == 1) return void(b[0] = fastpow(a[0], p - 2)); n   Inv(a, b, (n + 1) >> 1); n   int N = 1, l = -1; n   while (N <= n << 1) N <<= 1, l++; n   for (int i = 1; i < N; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << l); n   for (int i = 0; i < n; i++) a1[i] = a[i]; n   for (int i = n; i < N; i++) a1[i] = 0; n   NTT(a1, N); n   NTT(b, N); n   for (int i = 0; i < N; i++) n     b[i] = ((b[i] << 1) % p + p - a1[i] * b[i] % p * b[i] % p) % p; n   INTT(b, N); n   for (int i = n; i < N; i++) b[i] = 0; n   return; n } n void Dervt(long long *a, long long *b, int n) { n   for (int i = 1; i < n; i++) b[i - 1] = a[i] * i % p; n   b[n - 1] = 0; n } n void Integ(long long *a, long long *b, int n) { n   for (int i = 1; i < n; i++) b[i] = a[i - 1] * fastpow(i, p - 2) % p; n   b[0] = 0; n } n long long f1[MAXN], f2[MAXN]; n void Ln(long long *f, long long *g, int n) { n   memset(f1, 0, sizeof(f1)); n   memset(f2, 0, sizeof(f2)); n   Dervt(f, f1, n); n   Inv(f, f2, n); n   int N = 1, l = -1; n   while (N <= n << 1) N <<= 1, l++; n   for (int i = 1; i < N; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << l); n   NTT(f1, N); n   NTT(f2, N); n   for (int i = 0; i < N; i++) f1[i] = f1[i] * f2[i] % p; n   INTT(f1, N); n   Integ(f1, g, n); n   return; n } n long long g1[MAXN]; n void Exp(long long *f, long long *g, int n) { n   if (n == 1) return void(g[0] = 1); n   Exp(f, g, (n + 1) >> 1); n   Ln(g, g1, n); n   int N = 1, l = -1; n   while (N <= n << 1) N <<= 1, l++; n   for (int i = 1; i < N; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << l); n   for (int i = 0; i < n; i++) f1[i] = f[i]; n   for (int i = n; i < N; i++) g1[i] = f1[i] = 0; n   for (int i = 0; i < N; i++) f1[i] = ((f1[i] - g1[i]) % p + p) % p; n   f1[0] = (f1[0] + 1) % p; n   NTT(f1, N); n   NTT(g, N); n   for (int i = 0; i < N; i++) g[i] = f1[i] * g[i] % p; n   INTT(g, N); n   for (int i = n; i < N; i++) g[i] = 0; n   return; n } n long long lnf[MAXN]; n void Pow(long long *f, long long *g, int n, int k) { n   Ln(f, lnf, n); n   for (int i = 0; i < n; i++) lnf[i] = lnf[i] * k % p; n   memset(g, 0, sizeof(g1)); n   Exp(lnf, g, n); n   return; n } n int main() { n   n = read(); n   int t = read(); n   k = std::min(n, t); n   int N = 1, l = -1; n   while (N <= (k + 1) << 1) N <<= 1, l++; n   for (int i = 1; i < N; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << l); n   Pow(c, s, k + 1, inv2 - 1); n   Pow(c, f, k + 1, inv2); n   f[0]++, f[1]++; n   for (int i = 0; i <= k; i++) f[i] = f[i] * inv2 % p; n   Pow(f, g, k + 1, n + 1); n   NTT(s, N); n   NTT(g, N); n   for (int i = 0; i < N; i++) s[i] = s[i] * g[i] % p; n   INTT(s, N); n   for (int i = 1; i <= k; i++) std::printf( %lld  , s[i]); n   for (int i = k + 1; i <= t; i++) std::printf( 0  ); n   std::putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int x, long long int y) { n   if (y > x) { n     return gcd(y, x); n   } n   if (y == 0) { n     return x; n   } n   return gcd(y, x % y); n } n bool prime(long long int x) { n   for (long long int i = 2; i <= sqrt(x); i++) { n     if (x % i == 0) { n       return 0; n     } n   } n   return 1; n } n long long int fact(long long int n) { n   if (n == 0) { n     return 1; n   } n   return n * fact(n - 1); n } n void solve() { n   long long int r, c; n   cin >> r >> c; n   long long int b = 1; n   for (long long int i = 0; i < r; i++) { n     if (i % 2 == 0) { n       for (long long int j = 0; j < c; j++) cout <<  # ; n       cout << endl; n     } else { n       if (b == 1) { n         for (long long int j = 0; j < c - 1; j++) cout <<  . ; n         cout <<  # ; n         cout << endl; n         b = 0; n       } else { n         cout <<  # ; n         for (long long int j = 0; j < c - 1; j++) cout <<  . ; n         cout << endl; n         b = 1; n       } n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   long long int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int dp[10][310]; n int nex[1010][10][1010]; n int ai[1010]; n int up; n void init(void) { n   memset(dp, -1, sizeof(dp)); n   int i, j; n   int cnt[10]; n   for (i = 1; i <= n; i++) { n     memset(cnt, 0, sizeof(cnt)); n     for (j = 1; j <= 8; j++) n       for (int k = 1; k <= n; k++) nex[i][j][k] = 1e9; n     for (j = i; j <= n; j++) { n       cnt[ai[j]]++; n       nex[i][ai[j]][cnt[ai[j]]] = j; n     } n   } n } n int main() { n   int i, j, k; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> ai[i]; n   } n   init(); n   up = (1 << 8) - 1; n   int ans = 0; n   for (int num = 0; num * 8 <= n; num++) { n     for (int i = 0; i < 256; i++) n       for (int k = 0; k <= 8; k++) dp[k][i] = 1e9; n     dp[0][0] = 1; n     for (i = 0; i <= up; i++) { n       for (j = 0; j <= 8; j++) { n         for (k = 1; k <= 8; k++) { n           if (i & (1 << (k - 1))) continue; n           if (dp[j][i] > n) continue; n           dp[j][i ^ (1 << (k - 1))] = n               min(dp[j][i ^ (1 << (k - 1))], nex[dp[j][i]][k][num] + 1); n           dp[j + 1][i ^ (1 << (k - 1))] = n               min(dp[j + 1][i ^ (1 << (k - 1))], nex[dp[j][i]][k][num + 1] + 1); n         } n       } n     } n     for (i = 0; i <= 8; i++) n       if (dp[i][up] <= n + 1) { n         ans = max(ans, num * 8 + i); n       } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int num[105]; n int a[105][105], sum[105][105], mx[105][105]; n int f[105][10005]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> num[i]; n     for (int j = 1; j <= num[i]; j++) { n       cin >> a[i][j]; n       sum[i][j] = sum[i][j - 1] + a[i][j]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= num[i]; j++) { n       for (int k = 0; k <= j; k++) { n         mx[i][j] = max(mx[i][j], n                        sum[i][k] + (sum[i][num[i]] - sum[i][num[i] - (j - k)])); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       int maxi = 0; n       for (int k = 0; k <= num[i] && k <= j; k++) { n         maxi = max(maxi, f[i - 1][j - k] + mx[i][k]); n       } n       f[i][j] = maxi; n     } n   } n   cout << f[n][m] << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long a, b, c, flag = 0; n   scanf( %lld , &a); n   if (a == 1 || a == 2) { n     printf( -1 ); n     return 0; n   } n   if (a % 2 == 0) { n     flag = 1; n   } n   if (flag != 1) { n     b = (a * a + 1) / 2; n     c = (a * a - 1) / 2; n     printf( %lld %lld , c, b); n   } else { n     b = (a * a + 4) / 4; n     c = (a * a - 4) / 4; n     printf( %lld %lld , c, b); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   vector<int> w(n, 0); n   w[0] = v[0]; n   for (int i = 1; i < n; i++) w[i] = w[i - 1] + v[i]; n   int score = 0, penalty = 0; n   for (int i = 0; i < n; i++) { n     if (w[i] <= 60 * 12 - 10) n       score++; n     else n       break; n     if (w[i] > 60 * 6 - 10) penalty += w[i] - 60 * 6 + 10; n   } n   cout << score <<     << penalty << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int freq[200005]; n int arr[1005]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) freq[arr[i] + arr[j]]++; n   int ans = 0; n   for (int i = 0; i < 2e5 + 5; i++) ans = max(ans, freq[i]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct __timestamper {}; n struct change { n   int s, t, u; n }; n vector<long long> solve(vector<int> a, vector<change> b) { n   long long sum = 0; n   for (auto x : a) sum += x; n   vector<long long> ans; n   int n = a.size(); n   vector<int> cntr(n); n   auto change_counter = [&](int i, int delta) { n     sum -= max(0, a[i] - cntr[i]); n     cntr[i] += delta; n     sum += max(0, a[i] - cntr[i]); n   }; n   map<pair<int, int>, int> mp; n   for (auto c : b) { n     auto it = mp.find({c.s, c.t}); n     if (it != mp.end()) { n       change_counter(it->second, -1); n       mp.erase(it); n     } n     if (c.u != -1) { n       mp[{c.s, c.t}] = c.u; n       change_counter(c.u, +1); n     } n     ans.push_back(sum); n   } n   return ans; n } n int main() { n   int n; n   while (scanf( %d , &n) == 1) { n     vector<int> a(n); n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n     } n     int q; n     scanf( %d , &q); n     vector<change> b(q); n     for (int i = 0; i < q; i++) { n       scanf( %d%d%d , &b[i].s, &b[i].t, &b[i].u); n       b[i].s--; n       b[i].u--; n     } n     auto ans = solve(a, b); n     for (auto x : ans) printf( %lld n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int x, y = 0, z = 0; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x != y) { n       z++; n     } n     y = x; n   } n   cout << z; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, t[100001], ans; n double w; n long long lowbit(long long x) { return x & (-x); } n struct rua { n   double x, v; n   long long id; n } _[100001]; n void change(long long x) { n   while (x <= n) t[x]++, x += lowbit(x); n } n int sum(long long k) { n   long long res = 0; n   while (k > 0) res += t[k], k -= lowbit(k); n   return res; n } n bool cmp(rua a, rua b) { return fabs(a.x * b.v) < fabs(a.v * b.x); } n int main() { n   scanf( %I64d%lf , &n, &w), w += 1e-6; n   for (long long i = 1; i <= n; i++) n     scanf( %lf%lf , &_[i].x, &_[i].v), _[i].v -= w; n   sort(_ + 1, _ + n + 1, cmp); n   for (long long i = 1; i <= n; i++) _[i].id = i, _[i].v += 2 * w; n   sort(_ + 1, _ + n + 1, cmp); n   for (long long i = 1; i <= n; i++) n     ans += i - 1 - sum(_[i].id), change(_[i].id); n   return printf( %I64d n , ans), 0; n } n


#include<cstdio> n #include<cstring> n #include<algorithm> n using namespace std; n char ch; n int t,n,k,h[110],pos; n void read(int &x) n { n  x=0; n  ch=getchar(); n  while(ch< 0 ||ch> 9 ) ch=getchar(); n  while(ch>= 0 &&ch<= 9 ) n  {  n   x=x*10+ch- 0 ; n   ch=getchar(); n  } n } n int main() n { n  read(t); n  while(t--) n  { n   read(n);read(k); n   for (int i=1;i<=n;i++) read(h[i]); n   while(k--) n   { n    pos=0; n    for (int j=1;j<=n;j++) n     if (j==n) pos=-1; n     else if (h[j]<h[j+1]) {h[j]++;pos=j;break;} n    if (pos==-1) break; n   }  n   printf( %d n ,pos); n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<long long> X(n + 1), Y(n + 1); n   for (int i = (1); i <= (n); ++i) scanf( %lld%lld , &X[i], &Y[i]); n   long long S = 0; n   for (int k = (1); k <= (n); ++k) { n     vector<double> V; n     for (int i = (1); i <= (n); ++i) n       if (i != k) V.push_back(atan2(Y[i] - Y[k], X[i] - X[k])); n     sort(V.begin(), V.end()); n     V.insert(V.end(), V.begin(), V.end()); n     for (int i = 0, j = 0; i + 1 < n; i++) { n       while (V[j] + (j >= n - 1 ? 2 * acos(-1) : 0.0) - V[i] < acos(-1)) j++; n       long long x = j - 1 - i, y = n - 2 - x; n       S += x * (x - 1) / 2 * y * (y - 1) / 2; n     } n   } n   printf( %lld n , S / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum, len; n string s; n int cnt[26][5010][26]; n int main() { n   cin >> s; n   sum = 0; n   len = s.length(); n   s += s; n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 0; i < len; i++) { n     for (int j = 1; j < len; j++) { n       cnt[s[i] -  a ][j][s[i + j] -  a ]++; n     } n   } n   for (int i = 0; i < 26; i++) { n     int mx = 0; n     for (int j = 1; j < len; j++) { n       int tmp = 0; n       for (int k = 0; k < 26; k++) n         if (cnt[i][j][k] == 1) tmp++; n       mx = max(mx, tmp); n     } n     sum += mx; n   } n   printf( %.10lf n , (double)sum / (double)len); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n bool vis[N]; n vector<int> prime; n void sieve() { n   for (int i = 2; i * i < N; i++) { n     if (vis[i]) continue; n     for (int j = 2; j * i < N; j++) { n       vis[i * j] = 1; n     } n   } n   for (int i = 2; i < N; i++) { n     if (vis[i] == 0) prime.push_back(i); n   } n } n int find(int x) { n   int ans = upper_bound(prime.begin(), prime.end(), x) - prime.begin() - 1; n   return ans; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   sieve(); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int ans = find(n) - find(sqrt(n)) + 1; n     cout << ans <<   n ; n   } n   return 0; n } n