#include <bits/stdc++.h> n using namespace std; n const int N = 110; n long long read(void) { n   char ch = getchar(); n   long long sum = 0; n   bool f = 0; n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) { n     f = 1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) sum = sum * 10 + ch -  0 , ch = getchar(); n   if (f) sum = -sum; n   return sum; n } n int T, n, m; n string st; n int col[N][N]; n int tA; n struct node { n   int x1, y1, x2, y2, x3, y3; n } que[N * N * 3]; n void update(int x, int y) { n   if (x + 1 <= n) { n     if (y + 1 <= m) { n       que[++tA] = (node){x, y, x, y + 1, x + 1, y + 1}; n       que[++tA] = (node){x, y, x + 1, y, x + 1, y + 1}; n       que[++tA] = (node){x, y, x + 1, y, x, y + 1}; n     } else { n       que[++tA] = (node){x, y, x, y - 1, x + 1, y - 1}; n       que[++tA] = (node){x, y, x + 1, y, x + 1, y - 1}; n       que[++tA] = (node){x, y, x + 1, y, x, y - 1}; n     } n   } else { n     if (y + 1 <= m) { n       que[++tA] = (node){x, y, x, y + 1, x - 1, y + 1}; n       que[++tA] = (node){x, y, x - 1, y, x - 1, y + 1}; n       que[++tA] = (node){x, y, x - 1, y, x, y + 1}; n     } else { n       que[++tA] = (node){x, y, x, y - 1, x - 1, y - 1}; n       que[++tA] = (node){x, y, x - 1, y, x - 1, y - 1}; n       que[++tA] = (node){x, y, x - 1, y, x, y - 1}; n     } n   } n } n void solve(void) { n   n = read(); n   m = read(); n   tA = 0; n   for (int i = 1; i <= n; i++) { n     cin >> st; n     for (int j = 1; j <= m; j++) col[i][j] = (st[j - 1] ==  0 ) ? 0 : 1; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (col[i][j] == 1) update(i, j); n   printf( %d n , tA); n   for (int i = 1; i <= tA; i++) n     printf( %d %d %d %d %d %d n , que[i].x1, que[i].y1, que[i].x2, que[i].y2, n            que[i].x3, que[i].y3); n   return; n } n int main() { n   T = read(); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int a[5]; n   int Min; n   while (scanf( %d%d%d%d%d , a, a + 1, a + 2, a + 3, a + 4) != EOF) { n     Min = a[0]; n     if (a[1] < Min) Min = a[1]; n     if (a[2] / 2 < Min) Min = a[2] / 2; n     if (a[3] / 7 < Min) Min = a[3] / 7; n     if (a[4] / 4 < Min) Min = a[4] / 4; n     printf( %d n , Min); n   } n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n long long int gcd(long long int a, long long int b) { n   if (a < b) n     return gcd(b, a); n   else if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n long long int isPrime(long long int n) { n   long long int p = (long long int)sqrt(n); n   for (int i = (2); i <= (p); i += (1)) n     if (n % i == 0) return 0; n   return 1; n } n long long int pow(long long int b, long long int e) { n   if (e == 0) n     return 1; n   else if (e % 2 == 0) { n     long long int a = pow(b, e / 2); n     return a * a; n   } else { n     long long int a = pow(b, e / 2); n     return b * a * a; n   } n } n long long int powm(long long int x, long long int y, n                    long long int m = 1000000007) { n   x = x % m; n   long long int res = 1; n   while (y) { n     if (y & 1) res = res * x; n     res %= m; n     y = y >> 1; n     x = x * x; n     x %= m; n   } n   return res; n } n long long int modInverse(long long int a, long long int m) { n   return powm(a, m - 2, m); n } n void solve() { n   long long int x, y; n   cin >> x >> y; n   long long int c[7]; n   for (int i = (1); i <= (6); i += (1)) cin >> c[i]; n   long long int ans = LLONG_MAX; n   int isnegx = 1, isnegy = 1; n   if (x < 0) isnegx = -1; n   if (y < 0) isnegy = -1; n   swap(x, y); n   if (x >= 0 and y >= 0) { n     if (x <= y) { n       ans = min({ans, c[1] * x + (y - x) * c[6], c[1] * y + (y - x) * c[5], n                  c[2] * x + c[6] * y}); n     } else { n       ans = min({ans, c[1] * x + (x - y) * c[3], c[1] * y + (x - y) * c[2], n                  c[2] * x + c[6] * y}); n     } n   } else if (x <= 0 and y <= 0) { n     x *= -1; n     y *= -1; n     if (x <= y) { n       ans = min({ans, c[4] * x + (y - x) * c[3], c[4] * y + (y - x) * c[2], n                  c[5] * x + c[3] * y}); n     } else { n       ans = min({ans, c[4] * x + (x - y) * c[6], c[4] * y + (x - y) * c[5], n                  c[5] * x + c[3] * y}); n     } n   } else { n     x = isnegx * abs(x); n     y = isnegy * abs(y); n     if (x <= 0) { n       x *= -1; n       ans = min({ans, c[1] * x + (x + y) * c[3], c[4] * y + (x + y) * c[2], n                  c[2] * x + c[3] * y}); n     } else { n       y *= -1; n       ans = min({ans, c[4] * x + (x + y) * c[6], c[1] * y + (x + y) * c[5], n                  c[5] * x + c[6] * y}); n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; 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 int n, ar[1000001], x, ars[1000001]; n int main() { n   cin >> n >> x; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     ars[ar[i]]++; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     ars[ar[i]]--; n     ans += (ars[(ar[i] ^ x)]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n const int MaxN = 300; n using namespace std; n int n, m; n long long K; n int A1[MaxN + 5][2], A3[MaxN + 5][MaxN + 5]; n const long long INF = 1e18; n long long A2[MaxN + 5][MaxN + 5]; n int fail[MaxN + 5]; n void Get(string s) { n   int z = (int)s.length(); n   fail[0] = 0; n   for (int i = 1; i < z; i++) { n     int j = fail[i - 1]; n     while (j > 0 && s[j] != s[i]) j = fail[j - 1]; n     if (s[j] == s[i]) j++; n     fail[i] = j; n   } n   for (int i = 0; i <= z; i++) { n     for (int j = 0; j < 2; j++) { n       if (i < z && s[i] == char( 0  + j)) n         A1[i][j] = i + 1; n       else if (i == 0) n         A1[i][j] = 0; n       else n         A1[i][j] = A1[fail[i - 1]][j]; n     } n   } n   for (int i = 0; i <= z; i++) { n     for (int j = 0; j < 2; j++) { n       A3[i][j] = A1[i][j]; n       A2[i][j] = (A3[i][j] == z ? 1 : 0); n     } n   } n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j <= z; j++) { n       A3[j][i] = A3[A3[j][i - 2]][i - 1]; n       A2[j][i] = min((A2[j][i - 2] + A2[A3[j][i - 2]][i - 1]), INF); n     } n   } n } n int main() { n   while (~scanf( %d%lld%d , &n, &K, &m)) { n     string res =   ; n     for (int i = 0; i < m; i++) { n       if (res !=   ) Get(res); n       int x = 0; n       if (res !=    && A3[0][n] == i) x = 1; n       if (K == 1 && x == 1) break; n       K -= x; n       Get(res +  0 ); n       long long tmp = A2[0][n]; n       if (K > tmp) { n         res +=  1 ; n         K -= tmp; n       } else n         res +=  0 ; n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, i; n int digits(int n) { n   int d = 0; n   while (n) { n     n /= 10; n     d++; n   } n   return d; n } n int main() { n   cin >> a >> b; n   c = a; n   d = digits(b); n   for (i = 1; b; i++) { n     c += (b % 10) * (int)floor(pow((double)10, (double)d - i)); n     b /= 10; n   } n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   multiset<int> Q; n   cin >> n; n   long long ans = 0; n   long long times = 0; n   bool b = false; n   int count = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (Q.size() == 0 || (*Q.begin()) > x) { n       Q.insert(x); n     } else { n       ans += x - *Q.begin(); n       if (b) { n         times += 2; n         count++; n         if (count == 2) { n           b = false; n           count = 0; n         } n       } else n         b = true; n       Q.erase(Q.begin()); n       Q.insert(x); n       Q.insert(x); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char ch = getchar(); n   int x = 0; n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x; n } n const int maxn = 2e5 + 5, inf = 1e9; n struct Data { n   int next, to, w; n }; n struct LinkTable { n   Data data[maxn]; n   int head[maxn], cnt; n   void add(int x, int y, int w) { n     data[++cnt] = (Data){head[x], y, w}; n     head[x] = cnt; n   } n } E, R; n struct DP { n   int sum, id, lca; n } f[maxn], g[maxn]; n int N, K, mx[5], mi[5], ret[maxn], fa[maxn], w[maxn]; n bool mark[maxn]; n int update(int &x, int y) { n   if (x < y) return x = y, 1; n   if (x == y) return 0; n   return -1; n } n void F(int x) { n   int sum = mark[x] ? 0 : -inf, id = x; n   for (int i = E.head[x]; i; i = E.data[i].next) n     if (E.data[i].to != fa[x]) { n       Data &e = E.data[i]; n       fa[e.to] = x; n       w[e.to] = e.w; n       F(e.to); n       if (update(sum, f[e.to].sum + e.w) == 1) n         id = f[e.to].id; n       else if (!update(sum, f[e.to].sum + e.w)) n         id = x; n     } n   f[x].sum = sum; n   f[x].id = id; n   f[x].lca = x; n } n void G(int x) { n   g[x] = g[fa[x]]; n   g[x].sum += w[x]; n   if (mark[fa[x]] && w[x] > g[x].sum) n     g[x].sum = w[x], g[x].id = g[x].lca = fa[x]; n   for (int i = R.head[fa[x]]; i; i = R.data[i].next) n     if (R.data[i].to != x) { n       Data &e = R.data[i]; n       if (update(g[x].sum, f[e.to].sum + e.w + w[x]) == 1) n         g[x].id = f[e.to].id, g[x].lca = fa[x]; n       else if (update(g[x].sum, f[e.to].sum + e.w + w[x]) == 0) n         g[x].id = g[x].lca = fa[x]; n     } n   for (int i = 0; i < 3; ++i) mi[i] = 0, mx[i] = -inf; n   for (int i = E.head[x]; i; i = E.data[i].next) n     if (E.data[i].to != fa[x]) { n       Data &e = E.data[i]; n       if (update(mx[0], f[e.to].sum + e.w) == 1) { n         mx[2] = mx[1], mi[2] = mi[1]; n         mx[1] = mx[0], mi[1] = mi[0]; n         mi[0] = e.to; n       } else if (update(mx[1], f[e.to].sum + e.w) == 1) n         mx[2] = mx[1], mi[2] = mi[1], mi[1] = e.to; n       else if (update(mx[2], f[e.to].sum + e.w) == 1) n         mi[2] = e.to; n     } n   for (int i = 0; i < 3; ++i) n     if (mi[i]) R.add(x, mi[i], w[mi[i]]); n   for (int i = E.head[x]; i; i = E.data[i].next) n     if (E.data[i].to != fa[x]) G(E.data[i].to); n } n void init() { n   N = read(), K = read(); n   for (int i = 1; i <= K; ++i) mark[read()] = true; n   for (int i = 1, u, v, w; i < N; ++i) n     u = read(), v = read(), w = read(), E.add(u, v, w), E.add(v, u, w); n   g[0].sum = -inf; n } n void dfs(int x) { n   for (int i = E.head[x]; i; i = E.data[i].next) n     if (E.data[i].to != fa[x]) { n       Data &e = E.data[i]; n       dfs(e.to); n       ret[x] += ret[e.to]; n     } n } n void solve() { n   for (int i = 1; i <= N; ++i) n     if (mark[i] && f[i].sum != g[i].sum) n       if (f[i].sum > g[i].sum) n         ++ret[f[i].id], --ret[f[i].lca]; n       else n         ++ret[g[i].id], --ret[g[i].lca], ++ret[i], --ret[fa[g[i].lca]]; n   dfs(1); n   int sum = -inf, once = 0; n   for (int i = 1; i <= N; ++i) n     if (!mark[i]) n       if (ret[i] > sum) n         sum = ret[i], once = 1; n       else if (ret[i] == sum) n         ++once; n   printf( %d %d n , sum, once); n } n int main() { n   init(); n   F(1), G(1); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n const int INF = 0x3f3f3f3f; n long long get(long long n) { n   long long res = 2; n   long long ans = 0; n   while (res <= n) { n     ans += n / res; n     res *= 2; n   } n   return ans; n } n int main() { n   long long n; n   cin >> n; n   long long m = n; n   long long ans = n; n   long long p = 1; n   while (p < n) { n     ans += ((n - (p + 1)) / (2 * p) * p) + p; n     p *= 2; n   } n   cout << ans - n << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, k; n   cin >> n >> k; n   set<int> s; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     s.insert(a); n   } n   if (s.size() > k) { n     cout << -1 << endl; n     return; n   } n   cout << n * k << endl; n   for (int i = 0; i < n; i++) { n     for (int b : s) cout << b <<    ; n     for (int j = 0; j < k - (int)s.size(); j++) cout << 1 <<    ; n   } n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long parent[100]; n long nm[100]; n vector<long> v[100]; n long pp(long z) { n   if (parent[z] == z) { n     return z; n   } else { n     parent[z] = pp(parent[z]); n   } n } n int main() { n   long i, j, k, l, m, n, p, two, one; n   scanf( %ld%ld , &n, &m); n   for (i = 1; i <= n; i++) parent[i] = i; n   for (i = 1; i <= m; i++) { n     scanf( %ld%ld , &k, &l); n     parent[l] = k; n   } n   for (i = 1; i <= n; i++) { n     k = pp(i); n   } n   for (i = 1; i <= n; i++) { n     nm[parent[i]]++; n     if (nm[parent[i]] > 3) { n       printf( -1 n ); n       return 0; n     } n   } n   l = one = two = 0; n   for (i = 1; i <= n; i++) { n     if (nm[i] == 3) { n       k = 0; n       for (j = 1; j <= n; j++) { n         if (parent[j] == i) { n           v[l].push_back(j); n           parent[j] = -1; n           k++; n         } n       } n       nm[i] = -1; n       l++; n     } n     if (nm[i] == 2) n       two++; n     else if (nm[i] == 1) n       one++; n   } n   if (one >= two) { n     if ((one - two) % 3) { n       printf( -1 n ); n       return 0; n     } n     while (two) { n       for (i = 1; i <= n; i++) { n         if (nm[i] == 2) { n           for (j = 1; j <= n; j++) { n             if (nm[j] == 1) { n               for (k = 1; k <= n; k++) { n                 if (parent[k] == j) { n                   v[l].push_back(k); n                   nm[j] = -1; n                   parent[k] = -1; n                   break; n                 } n               } n               break; n             } n           } n           for (j = 1; j <= n; j++) { n             if (parent[j] == i) { n               v[l].push_back(j); n               parent[j] = -1; n             } n           } n           nm[i] = -1; n           two--; n           one--; n           l++; n           break; n         } n       } n     } n     while (one) { n       k = 0; n       for (i = 1; i <= n; i++) { n         if (nm[i] == 1) { n           v[l].push_back(i); n           nm[i] = -1; n           k++; n           one--; n           if (k == 3) { n             l++; n             break; n           } n         } n       } n     } n     for (i = 0; i < l; i++) printf( %ld %ld %ld n , v[i][0], v[i][1], v[i][2]); n   } else { n     printf( -1 n ); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int compare(const void* pa, const void* pb) { n   long long int p1 = *(const long long int*)pa; n   long long int p2 = *(const long long int*)pb; n   if (p1 < p2) n     return 1; n   else if (p1 > p2) n     return -1; n   else if (p1 == p2) n     return 0; n } n long long int arr[200500], s[200500][2], ans[200500]; n int main() { n   int j, t, n; n   cin >> t; n   while (t--) { n     int p = 0, x, y, a1, b1, a, b, low, high, mx, l = 0, m = 0, h = 0, w = -1; n     long long sum = 0, k; n     cin >> n; n     for (j = 0; j < n; j++) cin >> arr[j]; n     cin >> x >> a; n     cin >> y >> b; n     cin >> k; n     a1 = a; n     b1 = b; n     qsort(arr, n, sizeof(arr[0]), compare); n     while (1) { n       if (a < b) { n         s[p][0] = a; n         s[p][1] = x; n         p++; n         a += a1; n       } else if (b < a) { n         s[p][0] = b; n         s[p][1] = y; n         p++; n         b += b1; n       } else { n         s[p][0] = a; n         s[p][1] = x + y; n         p++; n         a += a1; n         b += b1; n       } n       if (a > n && b > n) break; n     } n     low = min(x, y); n     high = max(x, y); n     mx = x + y; n     for (j = 0; j < p; j++) { n       if (s[j][1] == low) { n         sum += (arr[l] / 100) * (s[j][1] - ans[l]); n         ans[l] = low; n         l++; n       } else if (s[j][1] == high) { n         sum += (arr[h] / 100) * (s[j][1] - ans[h]); n         ans[h] = high; n         if (h != l) { n           sum += (arr[l] / 100) * (low - ans[l]); n           ans[l] = low; n         } n         h++; n         l++; n       } else if (s[j][1] == mx) { n         sum += (arr[m] / 100) * (s[j][1] - ans[m]); n         ans[m] = mx; n         if (m != h) { n           sum += (arr[h] / 100) * (high - ans[h]); n           ans[h] = high; n         } n         if (h != l) { n           sum += (arr[l] / 100) * (low - ans[l]); n           ans[l] = low; n         } n         m++; n         h++; n         l++; n       } n       if (sum >= k) { n         w = s[j][0]; n         break; n       } n     } n     cout << w <<   n ; n     for (j = 0; j < n; j++) { n       s[j][0] = 0; n       s[j][1] = 0; n       arr[j] = 0; n       ans[j] = 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char s[505]; n int sz; n int val[505]; n int ch[505][11]; n void insert() { n   int f, k = 0; n   scanf( %s%d , s, &f); n   for (int i = 0; s[i]; ++i) { n     int c = s[i] -  0 ; n     if (!ch[k][c]) ch[k][c] = ++sz; n     k = ch[k][c]; n   } n   val[k] += f; n } n void init() { n   scanf( %d%d , &n, &m); n   while (n--) insert(); n } n int dep[505]; n int dp[505][505][15], f[505][15]; n void dfs(int i) { n   if (val[i]) n     for (int l = 0; l <= dep[i]; ++l) n       for (int k = 0; k <= m; ++k) dp[i][l][k] = l * val[i]; n   for (int c = 0; c < 11; ++c) { n     int j = ch[i][c]; n     if (!j) continue; n     dep[j] = dep[i] + 1; n     dfs(j); n     memcpy(f, dp[i], sizeof(f)); n     memset(dp[i], 0x3f, sizeof(dp[i])); n     for (int l = 0; l <= dep[i]; ++l) n       for (int k = m; ~k; --k) n         for (int p = 0; p <= k; ++p) { n           dp[i][l][k] = min(dp[i][l][k], f[l][k - p] + dp[j][l + 1][p]); n           if (p) dp[i][l][k] = min(dp[i][l][k], f[l][k - p] + dp[j][0][p - 1]); n         } n   } n } n void solve() { n   dfs(0); n   printf( %d n , dp[0][0][m]); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int LEN = (int)1e5; n int n; n long long a[LEN]; n vector<int> g[60]; n int bfs(int r, int t) { n   queue<int> q; n   static int d[LEN]; n   memset(d, 0, sizeof d); n   q.push(r); n   d[r] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int j = 0; (1ll << j) <= a[u]; j++) n       if ((1ll << j) & a[u]) n         for (auto v : g[j]) n           if (!d[v] && !(v == t && u == r)) { n             d[v] = d[u] + 1; n             if (v == t) return d[v] > 2 ? d[v] : INT_MAX; n             q.push(v); n           } n   } n   return INT_MAX; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     for (int j = 0; (1ll << j) <= a[i]; j++) n       if ((1ll << j) & a[i]) g[j].push_back(i); n     ; n     ; n   } n   for (int i = 0; i < 60; i++) n     if ((int)(g[i]).size() >= 3) { n       cout << 3; n       return 0; n     } n   int ans = INT_MAX; n   for (int i = 0; i < 60; i++) n     if ((int)(g[i]).size() > 1) { n       a[g[i][0]] &= ~(1ll << i); n       ans = min(ans, bfs(g[i][0], g[i][1])); n       a[g[i][0]] |= 1ll << i; n     } n   cout << (ans < INT_MAX ? ans : -1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s[10], a, b, c; n   cin >> a >> b >> c; n   s[0] = a + (b * c); n   s[1] = a * (b + c); n   s[2] = a * b * c; n   s[3] = (a + b) * c; n   s[4] = a + b + c; n   s[5] = (a * b) + c; n   sort(s, s + 5, greater<int>()); n   cout << s[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T __trace(string s, T x) { n   cerr << s << x << endl; n   return x; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (T x : v) os << x <<    ; n   return os; n } n template <class T> n istream& operator>>(istream& is, vector<T>& v) { n   for (T& x : v) is >> x; n   return is; n } n void run() { n   long long n; n   cin >> n; n   map<pair<long long, long long>, long long> m; n   long long ans = 0; n   for (long long i = 0; i < (n); i++) { n     long long h, mi; n     cin >> h >> mi; n     m[make_pair(h, mi)]++; n     ans = max(ans, m[make_pair(h, mi)]); n   } n   cout << ans << endl; n } n int main() { n   run(); 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 ch = getchar(); n   bool sign = true; n   while (!isdigit(ch)) { n     if (ch ==  - ) sign = false; n     ch = getchar(); n   } n   for (x = 0; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch -  0 ; n   if (!sign) x = -x; n } n bool mark[1505]; n int n, ans[1505], lim; n int d[5] = {0, 1, 5, 10, 50}; n void dfs(int id, int x, int tot) { n   if (x == lim + 1) { n     mark[tot] = true; n     return; n   } n   for (int i = id; i <= 4; i++) dfs(i, x + 1, tot + d[i]); n } n int main() { n   cin >> n; n   for (int i = 1; i <= 30; i++) { n     lim = i; n     memset(mark, 0, sizeof(mark)); n     dfs(1, 1, 0); n     int temp = 0; n     for (int j = 1; j <= i * 50; j++) n       if (mark[j]) temp++; n     ans[i] = temp; n   } n   if (n <= 12) { n     cout << ans[n]; n     return 0; n   } n   cout << 1ll * 49 * (n - 12) + ans[12]; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> S; n void flip(string& s1) { n   char temp = s1[0]; n   for (int i = 1; i < 4; i++) s1[i - 1] = s1[i]; n   s1[3] = temp; n   return; n } n bool isFirst(string s1) { n   for (int i = 0; i < 4; i++) { n     if (S.find(s1) != S.end()) return false; n     flip(s1); n   } n   return true; n } n int main() { n   int N, result(0); n   string s1, s2; n   cin >> N; n   cin >> s1 >> s2; n   s1 += s2[1]; n   s1 += s2[0]; n   if (isFirst(s1)) { n     S.insert(s1); n     result++; n   } n   for (int i = 1; i < N; i++) { n     cin >> s1; n     cin >> s1 >> s2; n     s1 += s2[1]; n     s1 += s2[0]; n     if (isFirst(s1)) { n       S.insert(s1); n       result++; n     } n   } n   cout << result << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline void Boost() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const int NMax = 1e5 + 50; n struct Event { n   int d, w, t; n   bool operator<(const Event &e) const { n     if (w != e.w) return w > e.w; n     return d > e.d; n   } n }; n vector<int> DP, jump; n map<Event, int> Map; n vector<Event> v[NMax]; n int main() { n   Boost(); n   int n, k, m; n   cin >> n >> m >> k; n   DP.resize(n); n   jump.resize(n); n   for (int i = 0; i < n; ++i) jump[i] = i + 1; n   for (int i = 0; i < k; ++i) { n     int s, t, d, w; n     cin >> s >> t >> d >> w; n     v[s - 1].push_back({d, w, 1}); n     v[t].push_back({d, w, 0}); n   } n   for (int i = 0; i < n; ++i) { n     for (auto x : v[i]) { n       if (x.t == 1) { n         if (Map.count(x)) { n           ++Map[x]; n         } else { n           Map[x] = 1; n         } n       } else { n         --Map[x]; n         if (Map[x] == 0) { n           Map.erase(x); n         } n       } n     } n     if (!Map.empty()) { n       DP[i] = (*Map.begin()).first.w; n       jump[i] = (*Map.begin()).first.d; n     } n   } n   vector<long long int> ans[m + 1]; n   for (auto &x : ans) x.resize(n + 1, LLONG_MAX); n   ans[0][0] = 0; n   for (int i = 0; i <= n; ++i) { n     for (int j = 1; j <= m && i; ++j) { n       ans[j][i] = min(ans[j][i], ans[j - 1][i - 1]); n     } n     for (int j = 0; j <= m && i < n; ++j) { n       if (ans[j][i] == LLONG_MAX) continue; n       ans[j][jump[i]] = min(ans[j][jump[i]], ans[j][i] + DP[i]); n     } n   } n   long long int ret = ans[0][n]; n   for (int i = 0; i <= m; ++i) { n     ret = min(ret, ans[i][n]); n   } n   cout << ret; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2510; n const int INF = 0x3f3f3f3f; n int prime[400]; n bool isprime[N]; n int k; n void init() { n   k = 0; n   for (int i = 2; i <= N; i++) { n     if (!isprime[i]) { n       prime[k++] = i; n       for (int j = i + i; j <= N; j += i) isprime[j] = 1; n     } n   } n } n char s[N]; n int ma[N * 2][N * 2], pre[N * 2][N * 2]; n int main() { n   init(); n   memset(pre, 0, sizeof(pre)); n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i < 2 * N; i++) { n     if (i <= n) scanf( %s , s + 1); n     int tmp = 0; n     for (int j = 1; j <= m; j++) { n       if (i > n) n         ma[i][j] = 0; n       else { n         if (s[j] ==  0 ) n           ma[i][j] = 0; n         else { n           ma[i][j] = 1; n           tmp++; n         } n       } n       pre[i][j] = tmp + pre[i - 1][j]; n     } n     for (int j = m + 1; j < N * 2; j++) { n       pre[i][j] = tmp + pre[i - 1][j]; n     } n   } n   int ans = INF; n   for (int f = 0; f < k; f++) { n     int p = prime[f]; n     int res = 0; n     for (int i = p; i < n + p; i += p) { n       for (int j = p; j < m + p; j += p) { n         int ff = pre[i][j] + pre[i - p][j - p] - pre[i][j - p] - pre[i - p][j]; n         res = res + min(p * p - ff, ff); n       } n     } n     ans = min(ans, res); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Node { n  public: n   int vol, l, r, tot; n   Node(int val) { n     tot = vol = val; n     l = r = 0; n   } n   Node() { n     tot = vol = 0; n     l = r = 0; n   } n }; n int main() { n   int H, q; n   cin >> H >> q; n   vector<Node> tree(1); n   string s; n   int v, val; n   for (int query = 0; query < q; ++query) { n     cin >> s; n     if (s[0] ==  a ) { n       cin >> v >> val; n       vector<int> vbit; n       while (v) { n         vbit.push_back(v % 2); n         v /= 2; n       } n       int it = 0; n       for (int h = (int)vbit.size() - 2; h >= 0; --h) { n         if (!tree[it].r) { n           tree[it].l = tree.size(); n           tree[it].r = tree.size() + 1; n           tree.resize(tree.size() + 2); n         } n         tree[it].tot += val; n         int nextit = tree[it].l; n         if (vbit[h]) { n           nextit = tree[it].r; n         } n         it = nextit; n       } n       tree[it].vol += val; n       tree[it].tot += val; n     } else { n       double res = 0.; n       int it = 0; n       int ma = 0; n       int h = H; n       for (; h > 0 && tree[it].r; --h) { n         int nextit = tree[it].r; n         if (tree[it].vol + tree[tree[it].r].tot > tree[tree[it].l].tot) { n           res += max(ma, tree[it].vol + tree[tree[it].r].tot) * n                  (double)(1 << (h - 1)); n           ma = max(ma, tree[it].vol + tree[tree[it].l].tot); n         } else { n           res += max(ma, tree[it].vol + tree[tree[it].l].tot) * n                  (double)(1 << (h - 1)); n           ma = max(ma, tree[it].vol + tree[tree[it].r].tot); n           nextit = tree[it].l; n         } n         it = nextit; n       } n       res += max(ma, tree[it].vol) * (double)(1 << h); n       printf( %lf n , res / (1 << (H))); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   long long int a, c1 = 0, c2 = 0, c3 = 0; n   vector<long long int> v1; n   vector<long long int> v2; n   vector<long long int> v3; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (a == 0) { n       v3.push_back(a); n     } else if (a > 0) { n       v2.push_back(a); n     } else { n       v1.push_back(a); n     } n   } n   if (v2.size() == 0) { n     for (int i = 0; i < 2; i++) { n       v2.push_back(v1.back()); n       v1.pop_back(); n     } n   } n   if (v1.size() % 2 == 0) { n     v3.push_back(v1.back()); n     v1.pop_back(); n   } n   cout << v1.size() <<    ; n   for (int i = 0; i < v1.size(); i++) cout << v1[i] <<    ; n   cout << endl; n   cout << v2.size() <<    ; n   for (int i = 0; i < v2.size(); i++) cout << v2[i] <<    ; n   cout << endl; n   cout << v3.size() <<    ; n   for (int i = 0; i < v3.size(); i++) cout << v3[i] <<    ; n   cout << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int w, h; n   cin >> w >> h; n   int ans = 1; n   for (int i = 0; i < w + h; i++) { n     ans = (ans * 2) % 998244353; n   } n   cout << ans % 998244353 <<   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 sum = 0; n   string s; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (s ==  Icosahedron ) n       sum += 20; n     else if (s ==  Dodecahedron ) n       sum += 12; n     else if (s ==  Octahedron ) n       sum += 8; n     else if (s ==  Cube ) n       sum += 6; n     else if (s ==  Tetrahedron ) n       sum += 4; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>( n                 chrono::high_resolution_clock::now().time_since_epoch()) n                 .count()); n const long long int N = 20005; n const long long int LG = 22; n long long int n, m; n long long int power(long long int x, long long int y, long long int m) { n   if (y == 0) return 1; n   long long int p = power(x, y / 2, m) % m; n   p = (p * p) % m; n   return (y % 2 == 0) ? p : (x * p) % m; n } n long long int fact[N], ifact[N]; n void proecss() { n   fact[0] = 1; n   for (long long int i = 1; i < N; i++) n     fact[i] = (fact[i - 1] * i) % 1000000007; n   for (long long int i = 0; i < N; i++) n     ifact[i] = power(fact[i], 1000000007 - 2, 1000000007); n } n long long int nCr(long long int n, long long int r) { n   if (r > n) return 0; n   if (r == n) return 1; n   long long int ans = (ifact[n - r] * ifact[r]) % 1000000007; n   ans = (ans * fact[n]) % 1000000007; n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   proecss(); n   cin >> n >> m; n   long long int b[n + 5], a[n + 5]; n   for (long long int i = 1; i <= n; i++) { n     long long int nn = n - i + 1; n     long long int rr = m - 1; n     b[i] = nCr(nn + rr - 1, rr); n     a[i] = nCr(i + rr - 1, rr); n   } n   for (long long int i = n - 1; i >= 1; i--) n     b[i] += b[i + 1], b[i] %= 1000000007; n   long long int ans = 0; n   for (long long int i = 1; i <= n; i++) { n     ans = (ans + (a[i] * b[i]) % 1000000007) % 1000000007; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string out; n long long int T; n unsigned canDo1[5000 / 32 + 1], canDo2[5000 / 32 + 1]; n unsigned *cd1 = canDo1, *cd2 = canDo2, *sw; n int A[5000]; n list<int> P; n int N; n void process() { n   cin >> N; n   N *= 2; n   P.clear(); n   for (int i = 0; i < N; i++) cin >> A[i]; n   int ps = 0, p = 0; n   for (int i = 0; i < N; i++) n     if (A[i] <= A[ps]) n       p++; n     else n       P.push_back(p), ps = i, p = 1; n   P.push_back(p); n   for (int i = 0; i <= N / 32 + 1; i++) cd1[i] = cd2[i] = 0; n   cd1[0] = cd2[0] = 1; n   for (int p : P) { n     for (int i = 0; i <= N; i++) n       if ((cd2[i / 32] & 1 << i % 32) || n           (i >= p && (cd2[(i - p) / 32] & 1 << (i - p) % 32))) n         cd1[i / 32] |= 1 << i % 32; n     sw = cd1, cd1 = cd2, cd2 = sw; n   } n   out += (cd2[N / 2 / 32] & 1 << N / 2 % 32) ?  YES n  :  NO n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> T; n   for (int t = 1; t <= T; t++) process(); n   cout << out; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, k, bd, s, ar[6000], ans, calc[1200000]; n long lst[1200000]; n long del; n long q; n void check(long mod) { n   del = 0; n   for (int i = 1; i <= n; i++) { n     q = ar[i] % mod; n     if (lst[q] == mod) n       ++del; n     else n       lst[q] = mod; n     if (del > k) break; n   } n   if (del <= k) ans = mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   if (k == 0) bd = 0; n   if (k == 1) bd = 1; n   if (k == 2) bd = 3; n   if (k == 3) bd = 6; n   if (k == 4) bd = 10; n   for (int i = 1; i <= n; i++) cin >> ar[i]; n   sort(ar + 1, ar + n + 1); n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) calc[ar[j] - ar[i]]++; n   ans = 0; n   for (int mod = 1; mod <= 1000001; mod++) { n     s = 0; n     if (ans > 0) break; n     for (int p = mod; p <= 1000001; p += mod) { n       s += calc[p]; n       if (s > bd) break; n     } n     if (s <= bd) check(mod); n   } n   cout << ans << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline istream &operator>>(istream &s, vector<T> &v) { n   for (T &t : v) { n     s >> t; n   } n   return s; n } n template <typename T> n inline ostream &operator<<(ostream &s, const vector<T> &v) { n   for (int i = 0; i < int(v.size()); ++i) { n     s << (    + !i) << v[i]; n   } n   return s; n } n template <typename T> n inline T fromString(const string &s) { n   T res; n   istringstream iss(s); n   iss >> res; n   return res; n }; n template <typename T> n inline string toString(const T &a) { n   ostringstream oss; n   oss << a; n   return oss.str(); n }; n const long long mod = 1000000007; n const long long INF = 1e18; n int main() { n   long long t; n   t = 1; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     char s[2][n]; n     for (int i = (0); i < (2); i++) { n       for (int j = (0); j < (n); j++) { n         cin >> s[i][j]; n       } n     } n     long long f = 0; n     for (int i = (0); i < (1); i++) { n       for (int j = (0); j < (n); j++) { n         if (s[i][j] ==  1  && s[i + 1][j] ==  1 ) { n           f = 1; n         } n       } n     } n     if (f) { n       cout <<  NO  n            <<   n ; n     } else { n       cout <<  YES  n            <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, k; n struct node { n   int c[2], fa, siz, tot; n   int val, id, mid, mv; n   bool rev; n   node() { rev = false; } n } t[600010]; n void maintain(int rt) { n   t[rt].siz = t[t[rt].c[0]].siz + t[t[rt].c[1]].siz + t[rt].tot; n   t[rt].mid = t[rt].id; n   t[rt].mv = t[rt].val; n   if (t[rt].c[0] && t[t[rt].c[0]].mv < t[rt].mv) n     t[rt].mv = t[t[rt].c[0]].mv, t[rt].mid = t[t[rt].c[0]].mid; n   if (t[rt].c[1] && t[t[rt].c[1]].mv < t[rt].mv) n     t[rt].mv = t[t[rt].c[1]].mv, t[rt].mid = t[t[rt].c[1]].mid; n   return; n } n bool isroot(int k) { return (t[t[k].fa].c[0] != k && t[t[k].fa].c[1] != k); } n void connect(int k, int f, int p) { n   t[k].fa = f; n   t[f].c[p] = k; n   return; n } n int id(int k) { return (t[t[k].fa].c[0] == k ? 0 : 1); } n void rotate(int x) { n   int y = t[x].fa, z = t[y].fa, fx = id(x), fy = id(y); n   if (!isroot(y)) t[z].c[fy] = x; n   t[x].fa = z; n   connect(t[x].c[fx ^ 1], y, fx); n   connect(y, x, fx ^ 1); n   maintain(y); n   maintain(x); n   return; n } n void pushdown(int rt) { n   if (!t[rt].rev) return; n   t[t[rt].c[0]].rev ^= 1; n   t[t[rt].c[1]].rev ^= 1; n   swap(t[rt].c[0], t[rt].c[1]); n   t[rt].rev = false; n   return; n } n stack<int> s; n void splay(int x) { n   s.push(x); n   for (int i = x; !isroot(i); i = t[i].fa) s.push(t[i].fa); n   while (!s.empty()) { n     pushdown(s.top()); n     s.pop(); n   } n   while (!isroot(x)) { n     int y = t[x].fa; n     if (isroot(y)) { n       rotate(x); n       break; n     } n     if (id(x) == id(y)) { n       rotate(y); n       rotate(x); n     } else { n       rotate(x); n       rotate(x); n     } n   } n   return; n } n void access(int k) { n   for (int i = 0; k; i = k, k = t[k].fa) { n     splay(k); n     t[k].tot += t[t[k].c[1]].siz; n     t[k].tot -= t[i].siz; n     t[k].c[1] = i; n     maintain(k); n   } n   return; n } n void makeroot(int k) { n   access(k); n   splay(k); n   t[k].rev ^= 1; n   return; n } n void link(int x, int y) { n   makeroot(x); n   makeroot(y); n   t[x].fa = y; n   t[y].tot += t[x].siz; n   maintain(y); n   return; n } n void cut(int x, int y) { n   makeroot(x); n   access(y); n   splay(x); n   t[x].c[1] = 0; n   t[y].fa = 0; n   t[x].siz -= t[y].siz; n   maintain(x); n   return; n } n struct edge { n   int u, v, t; n   bool del; n } es[600010]; n int en = 0; n int main() { n   scanf( %d%d%d , &n, &q, &k); n   int ecur = 0; n   int cur = 1; n   int ty, x, y; n   for (int i = 1; i <= n; ++i) n     t[i].siz = t[i].tot = 1, t[i].val = t[i].mv = 0x3f3f3f3f; n   for (int i = 1; i <= q; ++i) { n     scanf( %d , &ty); n     if (ty == 1) { n       scanf( %d%d , &x, &y); n       es[++en] = (edge){x, y, cur, false}; n       makeroot(x); n       access(y); n       splay(x); n       int v = y; n       while (t[v].fa) v = t[v].fa; n       if (v == x) { n         splay(y); n         int cute = t[y].mid; n         cut(es[cute].u, cute + n); n         cut(es[cute].v, cute + n); n         es[cute].del = true; n       } n       t[en + n].siz = 1; n       t[en + n].val = cur; n       t[en + n].id = en; n       t[en + n].mv = cur; n       t[en + n].mid = en; n       link(x, en + n); n       link(y, en + n); n     } n     if (ty == 2) { n       scanf( %d , &x); n       makeroot(x); n       printf( %d n , t[x].siz); n     } n     if (ty == 3) { n       ++cur; n       while (ecur < en && es[ecur + 1].t <= cur - k) { n         ++ecur; n         if (!es[ecur].del) { n           cut(es[ecur].u, ecur + n); n           cut(es[ecur].v, ecur + n); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using std::cerr; n const int N = 998244, M = 1001; n int n, m, d[N], las[N], seq[N], cnt[M][M], blo, x[N], y[N]; n inline void add(int a, int b, int val) { seq[a] += val, seq[b + 1] -= val; } n signed main() { n   std::ios::sync_with_stdio(0); n   scanf( %d%d , &n, &m), blo = sqrt(m); n   for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); n   int op, k; n   for (int now = 1; now <= m; now++) { n     scanf( %d%d , &op, &k); n     if (op == 1) { n       if (x[k] + y[k] <= blo) { n         for (int i = x[k]; i < x[k] + y[k]; i++) n           cnt[x[k] + y[k]][(now + i) % (x[k] + y[k])]++; n       } else n         for (int i = now + x[k]; i <= m; i += x[k] + y[k]) n           add(i, i + y[k] - 1, 1); n       las[k] = now; n     } else { n       if (x[k] + y[k] <= blo) { n         for (int i = x[k]; i < x[k] + y[k]; i++) n           cnt[x[k] + y[k]][(las[k] + i) % (x[k] + y[k])]--; n       } else { n         for (int i = las[k] + x[k]; i <= m; i += x[k] + y[k]) { n           if (i + y[k] - 1 < now) continue; n           if (i <= now && i + y[k] - 1 >= now) n             add(now, i + y[k] - 1, -1); n           else n             add(i, i + y[k] - 1, -1); n         } n       } n     } n     int ans = 0; n     for (int i = 2; i <= blo; i++) { n       ans += cnt[i][now % i]; n     } n     seq[now] += seq[now - 1]; n     printf( %d n , ans + seq[now]); n   } n } n


#include<iostream> n #include<cstdio> n #include<algorithm> n #include<cmath> n #include<cstring> n #include<queue> n #define ll long long n using namespace std; n const int N=310,M=100005,mod=1e9+7; n int n,tot; n ll inv,ans; n int head[N],ver[M],Nxt[M],dep[N],fa[N][20]; n ll f[N][N]; n void add(int x,int y){ n  ver[++tot]=y; n  Nxt[tot]=head[x]; n  head[x]=tot; n } n void dfs(int u,int now){ n  dep[u]=now; n  for(int i=head[u];i;i=Nxt[i]){ n   int y=ver[i]; n   if(y==fa[u][0]) continue; n   fa[y][0]=u; n   for(int j=1;j<=18;j++){ n    fa[y][j]=fa[fa[y][j-1]][j-1]; n   } n   dfs(y,now+1); n  } n } n int LCA(int x,int y){ n  if(dep[x]<dep[y]) swap(x,y); n  for(int j=18;j>=0;j--){ n   if(dep[fa[x][j]]>=dep[y]) x=fa[x][j]; n  } n  if(x==y) return x; n  for(int j=18;j>=0;j--){ n   if(fa[x][j]!=fa[y][j]){ n    x=fa[x][j];y=fa[y][j]; n   } n  } n  return fa[x][0]; n } n ll ksm(ll x,ll p){ n  ll sum=1; n  while(p){ n   if(p&1) sum=sum*x%mod; n   x=x*x%mod; n   p>>=1; n  } n  return sum; n } n int main() n { n  scanf( %d ,&n); n   n  inv=ksm(2,mod-2); n   n  for(int i=1;i<=n;i++){ n   f[i][0]=1; n   f[0][i]=0; n  } n   n  for(int i=1;i<=n;i++){ n   for(int j=1;j<=n;j++){ n    f[i][j]=(f[i-1][j]+f[i][j-1])*inv%mod; n   } n  } n   n  for(int i=1;i<n;i++){ n   int x,y; n   scanf( %d%d ,&x,&y); n   add(x,y);add(y,x); n  } n   n  for(int i=1;i<=n;i++){ n   memset(fa,0,sizeof(fa)); n   memset(dep,0,sizeof(dep)); n   dfs(i,1); n    n //  for(int j=1;j<=n;j++){ n //   printf( %d %d n ,j,dep[j]); n //  }  n    n   for(int j=1;j<=n;j++){ n    for(int k=j+1;k<=n;k++){ n     int lca=LCA(j,k); n //    printf( 以%d为根 %d 和 %d 的 LCA 是%d n ,i,j,k,lca); n     ans=(ans+f[dep[j]-dep[lca]][dep[k]-dep[lca]])%mod; n    } n   } n  } n   n  printf( %lld n ,ans*ksm(n,mod-2)%mod); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int lcp[25][25]; n int b[25]; n string s[25]; n int ans = 0; n void BT(int now, int n, int k) { n   if (now == n) { n     int have = 0; n     for (int i = 0; i < n; i++) n       for (int j = i + 1; j < n; j++) n         if (b[i] && b[j]) have += lcp[i][j]; n     ans = max(ans, have); n     return; n   } n   b[now] = 0; n   if (n - now > k) BT(now + 1, n, k); n   if (k) { n     b[now] = 1; n     BT(now + 1, n, k - 1); n   } n } n int main() { n   ios::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     for (int j = 0; j < i; j++) { n       int k; n       for (k = 0; k < s[i].length() && k < s[j].length() && s[i][k] == s[j][k]; n            k++) n         ; n       lcp[i][j] = lcp[j][i] = k; n     } n   } n   BT(0, n, k); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<double, double> arr[1000]; n int n; n double m, r; n void solve() { n   vector<pair<int, int> > v; n   double h = m; n   int t = 0; n   while (h > 0) { n     int best_i = -1; n     for (int i = 0; i < n; ++i) { n       if (arr[i].first >= h) { n         if (best_i == -1) { n           best_i = i; n         } else { n           if (arr[i].second > arr[best_i].second) { n             best_i = i; n           } n         } n       } n     } n     if (best_i == -1 && r >= 0) { n       printf( NO ); n       return; n     } n     if (best_i > -1) { n       r -= arr[best_i].second; n       arr[best_i].first = -10; n       v.push_back(make_pair(t, best_i + 1)); n     } n     h += r; n     h = min(h, m); n     t++; n   } n   printf( YES n ); n   printf( %d %d n , t, int(v.size())); n   for (int i = 0; i < int(v.size()); ++i) { n     printf( %d %d n , v[i].first, v[i].second); n   } n } n int main() { n   scanf( %d%lf%lf , &n, &m, &r); n   for (int i = 0; i < n; ++i) { n     double p, d; n     scanf( %lf%lf , &p, &d); n     arr[i].first = m / 100 * p; n     arr[i].second = d; n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:128777216 ) n const int null = 0; n using namespace std; n template <class T> n int size(const T &a) { n   return int(a.size()); n } n template <class T> n T abs(const T &a) { n   return (a < 0 ? -a : a); n } n template <class T> n T sqr(const T &a) { n   return a * a; n } n const int max_n = 52, mod = 1000000007; n int dist[max_n][max_n][2]; n int dp[max_n][max_n][2]; n bool was[max_n][max_n][2]; n struct Pos { n   int i, j, s; n   int d; n   void init(int i, int j, int s, int d) { n     this->i = i; n     this->j = j; n     this->s = s; n     this->d = d; n     was[i][j][s] = true; n     dist[i][j][s] = d; n   } n } q[max_n * max_n * 2]; n int cmn[max_n][max_n]; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 0; i <= n; i++) { n     cmn[0][i] = 1; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cmn[i][j] = cmn[i - 1][j - 1] + cmn[i][j - 1]; n     } n   } n   k /= 50; n   int c0 = 0, c1 = 0; n   for (int i = 0; i < n; i++) { n     int t; n     scanf( %d , &t); n     if (t == 50) { n       c0++; n     } else { n       c1++; n     } n   } n   q[0].init(0, 0, 1, 0); n   dp[0][0][1] = 1; n   for (int d = 0, u = 1; d < u; d++) { n     Pos p = q[d]; n     int mc0, mc1; n     if (p.s) { n       mc0 = c0 - p.i; n       mc1 = c1 - p.j; n     } else { n       mc0 = p.i; n       mc1 = p.j; n     } n     long long cnt = dp[p.i][p.j][p.s]; n     for (int i = 0; i <= mc0; i++) { n       for (int j = 0; j <= mc1; j++) { n         if (i + j > 0 && i + j * 2 <= k) { n           int c0, c1, s0 = p.s ^ 1; n           if (p.s) { n             c0 = p.i + i; n             c1 = p.j + j; n           } else { n             c0 = p.i - i; n             c1 = p.j - j; n           } n           long long cur = cnt * cmn[i][mc0] % mod * cmn[j][mc1] % mod; n           if (!was[c0][c1][s0]) { n             q[u++].init(c0, c1, s0, p.d + 1); n             dp[c0][c1][s0] = cur; n           } else if (p.d + 1 == dist[c0][c1][s0]) { n             dp[c0][c1][s0] = (cur + dp[c0][c1][s0]) % mod; n           } n         } n       } n     } n   } n   if (!was[c0][c1][0]) { n     printf( -1 n0 n ); n     return 0; n   } n   printf( %d n%d n , dist[c0][c1][0], dp[c0][c1][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pii = pair<int, int>; n using vi = vector<int>; n const int N = 4e5; n vi cnt(N, 0); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int a; n   cin >> a; n   string s; n   cin >> s; n   int n = s.size(); n   for (int i = 0; i < n; i++) { n     int sum = 0; n     for (int j = i; j < n; j++) { n       sum += int(s[j] -  0 ); n       cnt[sum]++; n     } n   } n   ll ans = 0; n   if (a == 0) { n     for (int i = 0; i < N; i++) ans += cnt[i]; n     ans = 2 * cnt[0] * ans - 1LL * cnt[0] * cnt[0]; n   } else { n     for (int i = 1; i < N; i++) { n       if (a % i) continue; n       int b = a / i; n       if (b >= N) continue; n       ans += 1LL * cnt[i] * cnt[b]; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int f = 1, res = 0, ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) res = res * 10 + ch - 48, ch = getchar(); n   return f * res; n } n int _sta[1 << 8]; n inline void write(int val) { n   if (!val) { n     putchar( 0 ); n     return; n   } n   if (val < 0) putchar( - ), val = -val; n   *_sta = 0; n   while (val > 0) _sta[++*_sta] = val % 10, val /= 10; n   for (int i = *_sta, _Lim = 1; i >= _Lim; i--) putchar(_sta[i] + 48); n } n const long long INF = 1e18; n long long Exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1, y = 0; n     return a; n   } n   int res = Exgcd(b, a % b, x, y); n   int x_ = x; n   x = y; n   y = x_ - a / b * y; n   return res; n } n int n, m, k; n long long Cal(long long x, long long y) { n   long long a, b; n   long long gcd = Exgcd(2 * n, 2 * m, a, b); n   long long lcm = 1LL * 2 * n * 2 * m / gcd; n   if (a < 0) n     a = -a; n   else n     a = 2 * m / gcd - a; n   if ((x - y) % gcd != 0) return INF; n   x += 1LL * (x - y) / gcd * a * 2 * n; n   x -= x / lcm * lcm; n   if (x < 0) x += lcm; n   return x; n } n int main() { n   n = read(); n   m = read(); n   k = read(); n   while (k--) { n     int x = read(), y = read(); n     long long Ans = INF; n     Ans = min(Ans, Cal(x, y)); n     Ans = min(Ans, Cal(x, 2 * m - y)); n     Ans = min(Ans, Cal(2 * n - x, y)); n     Ans = min(Ans, Cal(2 * n - x, 2 * m - y)); n     if (Ans < INF) n       printf( %I64d n , Ans); n     else n       printf( -1 n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int SZ = 2000010; n const int INF = 1e9 + 10; n const int mod = 1e9 + 7; n const long double eps = 1e-8; n long long read() { n   long long n = 0; n   char a = getchar(); n   bool flag = 0; n   while (a >  9  || a <  0 ) { n     if (a ==  - ) flag = 1; n     a = getchar(); n   } n   while (a <=  9  && a >=  0 ) { n     n = n * 10 + a -  0 , a = getchar(); n   } n   if (flag) n = -n; n   return n; n } n vector<int> g2[SZ]; n vector<int> g[SZ]; n int n, m, q; n int ccnt = 0; n int dfn[SZ], low[SZ], dfs_clock; n stack<int> S; n bool cut[SZ], ins[SZ]; n void Tarjan(int u, int f) { n   dfn[u] = low[u] = ++dfs_clock; n   S.push(u); n   ins[u] = 1; n   int child = 0; n   for (int i = 0; i < g2[u].size(); i++) { n     int v = g2[u][i]; n     if (v == f) continue; n     if (!dfn[v]) { n       child++; n       Tarjan(v, u); n       low[u] = min(low[u], low[v]); n       if (low[v] >= dfn[u]) { n         ccnt++; n         cut[u] = 1; n         int x; n         do { n           x = S.top(); n           S.pop(); n           ins[x] = 0; n           g[ccnt + n].push_back(x); n           g[x].push_back(ccnt + n); n         } while (x != v && !S.empty()); n         g[ccnt + n].push_back(u); n         g[u].push_back(ccnt + n); n       } n     } else if (ins[v]) { n       low[u] = min(low[u], dfn[v]); n     } n   } n   if (f == 0 && child == 1) cut[u] = 0; n } n int anc[SZ][22], deep[SZ]; n void dfsLca(int u, int f) { n   deep[u] = deep[f] + 1; n   anc[u][0] = f; n   for (int i = 1; anc[u][i - 1]; i++) anc[u][i] = anc[anc[u][i - 1]][i - 1]; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (v == f) continue; n     dfsLca(v, u); n   } n } n int askLca(int u, int v) { n   if (deep[u] < deep[v]) swap(u, v); n   int dd = deep[u] - deep[v]; n   for (int i = 20; i >= 0; i--) n     if (dd >> i & 1) u = anc[u][i]; n   if (u == v) return u; n   for (int i = 20; i >= 0; i--) n     if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i]; n   return anc[u][0]; n } n int main() { n   n = read(), m = read(), q = read(); n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(); n     g2[x].push_back(y); n     g2[y].push_back(x); n   } n   Tarjan(1, 0); n   n += ccnt; n   dfsLca(1, 0); n   while (q--) { n     int x = read(), y = read(); n     int lca = askLca(x, y); n     int ans = deep[x] + deep[y] - 2 * deep[lca]; n     printf( %d n , ans / 2); n   } n   return 0; n } n


#include <iostream> n #include <cmath> n  n using namespace std; n  n int main() { n     int t; n      n     cin >> t; n      n     while(t--) n     { n         int xa, ya, xb, yb, xf, yf; n          n         cin >> xa >> ya >> xb >> yb >> xf >> yf; n          n         if(xa==xb && xb==xf && yf > ya && yf < yb) n         { n             cout << abs(yb-ya) + 2 << endl; n             continue; n         } n          n         else if(xa==xb && xb==xf && yf < ya && yf > yb) n         { n             cout << abs(yb-ya) + 2 << endl; n             continue; n         } n          n         else if(ya==yb && yb==yf && xf>xa && xf<xb) n         { n             cout << abs(xb-xa) + 2 << endl; n             continue; n         } n          n         else if(ya==yb && yb==yf && xf<xa && xf>xb) n         { n             cout << abs(xb-xa) + 2 << endl; n             continue; n         } n          n         else n         { n             cout << abs(xb-xa) + abs(yb-ya) << endl; n             continue; n         } n     } n }


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const int mo = 1000000007; n const int inf = 0x3f3f3f3f; n const int INF = 2000000000; n int n, m, phi; n int lt[100008], size[100008], f[100008], vis[100008], a[100008], b[100008], sum, n     tot, root, pre1[100008], pre2[100008]; n long long ans, Alltmp; n map<int, int> mp; n struct edge { n   int u, v, w, nt; n } eg[100008 * 2]; n void add(int u, int v, int w) { n   eg[++sum] = (edge){u, v, w, lt[u]}; n   lt[u] = sum; n } n long long quick(long long x, long long y) { n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x % m; n     x = x * x % m; n     y >>= 1; n   } n   return res; n } n void init() { n   int mm = m; n   phi = m; n   for (int i = 2; i * i <= m; i++) { n     if (mm % i == 0) { n       while (mm % i == 0) mm /= i; n       phi = phi / i * (i - 1); n     } n   } n   if (mm > 1) phi = phi / mm * (mm - 1); n   memset(lt, 0, sizeof(lt)); n   ; n   sum = 1; n   memset(f, 0, sizeof(f)); n   ; n   f[0] = INF; n   memset(vis, 0, sizeof(vis)); n   ; n   ans = 0; n   pre1[0] = 1; n   pre2[0] = 1; n   for (int i = 1; i <= 100000; i++) { n     pre1[i] = 1ll * pre1[i - 1] * 10 % m; n     pre2[i] = quick(pre1[i], phi - 1); n   } n } n void getRoot(int u, int fa) { n   size[u] = 1; n   f[u] = 0; n   for (int i = lt[u]; i; i = eg[i].nt) { n     int v = eg[i].v; n     if (vis[v] || v == fa) continue; n     getRoot(v, u); n     size[u] += size[v]; n     f[u] = max(f[u], size[v]); n   } n   f[u] = max(f[u], tot - size[u]); n   if (f[u] < f[root]) root = u; n } n void getA(int u, int fa, int len) { n   for (int i = lt[u]; i; i = eg[i].nt) { n     int v = eg[i].v; n     if (vis[v] || v == fa) continue; n     a[v] = (1ll * eg[i].w * pre1[len] + a[u]) % m; n     getA(v, u, len + 1); n   } n   Alltmp += mp[a[u]]; n   int tp = (m - b[u]) % m; n   tp = 1ll * tp * pre2[len] % m; n   if (a[u] == tp) Alltmp--; n } n void getB(int u, int fa, int len) { n   int tp = (m - b[u]) % m; n   tp = 1ll * tp * pre2[len] % m; n   mp[tp]++; n   for (int i = lt[u]; i; i = eg[i].nt) { n     int v = eg[i].v; n     if (vis[v] || v == fa) continue; n     b[v] = (1ll * b[u] * 10 + eg[i].w) % m; n     getB(v, u, len + 1); n   } n } n long long calc(int u, int key) { n   key %= m; n   Alltmp = 0; n   mp.clear(); n   a[u] = b[u] = key; n   getB(u, 0, key == 0 ? 0 : 1); n   getA(u, 0, key == 0 ? 0 : 1); n   return Alltmp; n } n void solve(int u) { n   ans += calc(u, 0); n   vis[u] = 1; n   for (int i = lt[u]; i; i = eg[i].nt) { n     int v = eg[i].v; n     if (vis[v]) continue; n     ans -= calc(v, eg[i].w); n     root = 0; n     tot = size[v]; n     getRoot(v, u); n     solve(root); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   init(); n   for (int i = 1; i <= n - 1; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     add(u + 1, v + 1, w); n     add(v + 1, u + 1, w); n   } n   root = 0; n   tot = n; n   getRoot(1, 0); n   solve(root); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x1, y1, x2, y2; n   scanf( %d%d%d%d%d%d , &n, &m, &x1, &y1, &x2, &y2); n   int x, y; n   x = abs(x1 - x2); n   y = abs(y1 - y2); n   if (x > y) swap(x, y); n   if (y < 4) { n     cout <<  First  << endl; n     return 0; n   } n   if (x < 3 && y < 5) { n     cout <<  First  << endl; n     return 0; n   } n   cout <<  Second  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 55; n int n, m; n int a[MAXN]; n double dp[MAXN][MAXN][MAXN]; n double C[MAXN][MAXN]; n void init() { n   C[0][0] = 1.0; n   for (int i = 1; i <= 50; i++) { n     C[i][0] = 1.0; n     for (int j = 1; j <= i; j++) C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   init(); n   for (int i = 1; i <= m; i++) scanf( %d , &a[i]); n   for (int i = 0; i <= n; i++) dp[0][0][i] = i; n   for (int i = 1; i <= m; i++) { n     for (int j = 0; j <= n; j++) { n       for (int k = 0; k <= n; k++) { n         for (int c = 0; c <= j; c++) { n           int maxq = max(k, (c + a[i] - 1) / a[i]); n           dp[i][j][k] += n               dp[i - 1][j - c][maxq] * pow(i - 1, j - c) / pow(i, j) * C[j][c]; n         } n       } n     } n   } n   printf( %.12f n , dp[m][n][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, j; n   long long int sum = 0, x; n   cin >> n >> m; n   int a[n][m]; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   for (i = n - 2; i >= 1; i--) { n     for (j = m - 2; j >= 1; j--) { n       if (a[i][j] == 0) { n         x = min(a[i][j + 1], a[i + 1][j]); n         a[i][j] = x - 1; n       } n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m - 1; j++) { n       if (a[i][j] >= a[i][j + 1]) { n         cout <<  -1  << endl; n         return 0; n       } n     } n   } n   for (i = 0; i < m; i++) { n     for (j = 0; j < n - 1; j++) { n       if (a[j][i] >= a[j + 1][i]) { n         cout <<  -1  << endl; n         return 0; n       } n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       sum = sum + a[i][j]; n     } n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n double f(double m, vector<int> &v) { n   int n = v.size(); n   vector<long double> v2(n); n   for (int i = 0; i < (n); i++) v2[i] = v[i] - m; n   long double mx_otr = 0; n   long double mn_otr = 0; n   long double cur = 0; n   for (int i = 0; i < (n); i++) { n     cur += v2[i]; n     if (cur < 0) cur = 0; n     mx_otr = max(mx_otr, cur); n   } n   cur = 0; n   for (int i = 0; i < (n); i++) { n     cur += v2[i]; n     if (cur >= 0) cur = 0; n     mn_otr = min(mn_otr, cur); n   } n   return max(mx_otr, abs(mn_otr)); n } n inline void solve() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < (n); i++) cin >> v[i]; n   long double l = -10000, r = 10000; n   while (abs(r - l) > 1e-12) { n     long double m1 = l + (r - l) / 3; n     long double m2 = r - (r - l) / 3; n     if (f(m1, v) <= f(m2, v)) n       r = m2; n     else n       l = m1; n   } n   cout << fixed << setprecision(10) << f(l + (r - l) / 2.0, v); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int q = 1; n   while (q--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n int n, tt, rt, ans, ls[N], rs[N], val[N], tg[N], rd[N]; n inline int nw(int v) { n   val[++tt] = v; n   rd[tt] = rand(); n   return tt; n } n inline void add(int x, int v) { n   val[x] += v; n   tg[x] += v; n } n inline void dn(int x) { n   add(ls[x], tg[x]); n   add(rs[x], tg[x]); n   tg[x] = 0; n } n void split(int x, int k, int& a, int& b) { n   if (!x) { n     a = b = 0; n     return; n   } n   dn(x); n   val[x] < k ? (a = x, split(rs[x], k, rs[a], b)) n              : (b = x, split(ls[x], k, a, ls[b])); n } n int merge(int x, int y) { n   if (!x || !y) n     return x | y; n   else if (rd[x] < rd[y]) { n     dn(x); n     rs[x] = merge(rs[x], y); n     return x; n   } else { n     dn(y); n     ls[y] = merge(x, ls[y]); n     return y; n   } n } n int main() { n   scanf( %d , &n); n   while (n--) { n     int l, r, x, y, z; n     scanf( %d%d , &l, &r); n     split(rt, r, x, z); n     split(x, l, x, y); n     add(y, 1); n     if (z) { n       int w = z; n       while (ls[w]) dn(w), w = ls[w]; n       split(z, val[w] + 1, w, z); n     } else n       ans++; n     rt = merge(merge(x, nw(l)), merge(y, z)); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6; n char buff[MAXN + 5]; n string input, subs; n int freq[10]; n int counter(int num) { n   int ret = 0; n   while (num) { n     ret++; n     num /= 10; n   } n   return ret; n } n string res; n bool flag; n void solve(bool taken) { n   for (int i = 0; i <= 10; i++) { n     if (!taken && (subs[0] -  0 ) < i) { n       res += subs; n       solve(1); n       return; n     } else if (i <= 9 && freq[i]) { n       char c = (i +  0 ); n       res += c; n       freq[i]--; n       solve(taken); n       freq[i]++; n       return; n     } n   } n   return; n } n void solve2(bool taken) { n   for (int i = 0; i <= 10; i++) { n     if (!taken && (subs[0] -  0 ) <= i) { n       res += subs; n       solve2(1); n       return; n     } else if (i <= 9 && freq[i]) { n       char c = (i +  0 ); n       res += c; n       freq[i]--; n       solve2(taken); n       freq[i]++; n       return; n     } n   } n   return; n } n int main() { n   scanf( %s , buff); n   input = buff; n   scanf( %s , buff); n   subs = buff; n   if (input.size() == 2) { n     if (input[0] ==  1 ) swap(input[0], input[1]); n     printf( %c n , input[0]); n     return 0; n   } n   int k; n   string sk; n   for (int i = 1; i <= MAXN; i++) { n     if (i == input.size() - counter(i)) { n       k = i; n       break; n     } n   } n   int temp = k; n   while (temp) { n     char c = ((temp % 10) +  0 ); n     sk = c + sk; n     temp /= 10; n   } n   for (int i = 0; i < input.size(); i++) freq[input[i] -  0 ]++; n   for (int i = 0; i < subs.size(); i++) freq[subs[i] -  0 ]--; n   for (int i = 0; i < sk.size(); i++) freq[sk[i] -  0 ]--; n   flag = 0; n   char d = sk[0]; n   for (int i = 1; i < sk.size(); i++) { n     if (sk[i] < d) { n       flag = 1; n       break; n     } else if (sk[i] == d) n       ; n     else n       break; n   } n   solve(1); n   string res1 = subs + res; n   res.clear(); n   for (int i = 1; i <= 9; i++) { n     if (freq[i]) { n       char c = (i +  0 ); n       res += c; n       freq[i]--; n       solve(0); n       freq[i]++; n       break; n     } n   } n   string res2 = res; n   res.clear(); n   for (int i = 1; i <= 9; i++) { n     if (freq[i]) { n       char c = (i +  0 ); n       res += c; n       freq[i]--; n       solve2(0); n       freq[i]++; n       break; n     } n   } n   string res3 = res; n   char c =  9  + 1; n   if (res1[0] ==  0  || res1 ==   ) res1 =   , res1 += c; n   if (res2[0] ==  0  || res2 ==   ) res2 =   , res2 += c; n   if (res3[0] ==  0  || res3 ==   ) res3 =   , res3 += c; n   if (res1 < res2 && res1 < res3) n     printf( %s n , res1.c_str()); n   else if (res2 < res1 && res2 < res3) n     printf( %s n , res2.c_str()); n   else n     printf( %s n , res3.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void B(); n int main() { n   ios::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   B(); n } n class segmen { n  public: n   long long x1, y1, x2, y2; n   segmen(long long xx1, long long yy1, long long xx2, long long yy2) n       : x1{xx1}, x2{xx2}, y1{yy1}, y2{yy2} {} n   segmen() {} n }; n vector<segmen> hor, ver; n const long long N = 10010; n const long long shift = 5001; n bool compar(segmen A, segmen B) { return A.y1 <= B.y1; } n long long T[N]; n void upd(long long x, long long val) { n   for (; x < N; x += (x & (-x))) T[x] += val; n } n long long F(long long x) { n   long long ret = 0; n   for (; x; x -= (x & (-x))) ret += T[x]; n   return ret; n } n long long gett(long long l, long long r) { return F(r) - F(l - 1); } n vector<pair<long long, long long> > H[N]; n vector<pair<long long, long long> > V[N]; n void B() { n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     long long x1, x2, y1, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     x1 += shift; n     x2 += shift; n     y1 += shift; n     y2 += shift; n     if (y1 == y2) { n       H[y1].push_back({min(x1, x2), max(x1, x2)}); n     } n     if (x1 == x2) { n       V[x1].push_back({min(y1, y2), max(y1, y2)}); n     } n   } n   long long ans = 0; n   vector<long long> endy[N]; n   for (long long i = 1; i < N; i++) { n     for (auto A : H[i]) { n       long long xl = A.first; n       long long xr = A.second; n       long long maxn = 0; n       for (long long j = xl; j <= xr; j++) { n         for (auto B : V[j]) { n           if (B.first <= i && B.second > i) { n             upd(j, 1); n             endy[B.second].push_back(j); n             maxn = max(maxn, B.second); n           } n         } n       } n       for (long long j = i + 1; j <= maxn; j++) { n         for (auto B : H[j]) { n           long long ret = gett(B.first, B.second); n           ans += (ret * (ret - 1)) >> 1; n         } n         for (auto B : endy[j]) { n           upd(B, -1); n         } n         endy[j].clear(); n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 50; n long long fac[N + 1]; n void init() { n   fac[0] = 1; n   for (long long i = 1; i <= N; i++) { n     fac[i] = (i * fac[i - 1]) % 1000000007LL; n   } n } n long long pw(long long a, long long e) { n   if (e == 0) { n     return 1; n   } n   long long ret = pw(a, e / 2); n   ret = (ret * ret) % 1000000007LL; n   if (e % 2 == 1) { n     ret = (a * ret) % 1000000007LL; n   } n   return ret; n } n long long choose(long long x, long long y, long long x0, long long y0) { n   return (((((fac[x] * pw(fac[x0], 1000000007LL - 2)) % 1000000007LL) * n             pw(fac[x - x0], 1000000007LL - 2)) % n            1000000007LL) * n           ((((fac[y] * pw(fac[y0], 1000000007LL - 2)) % 1000000007LL) * n             pw(fac[y - y0], 1000000007LL - 2)) % n            1000000007LL)) % n          1000000007LL; n } n long long X, Y; n vector<pair<long long, long long> > adj[(N + 1) * (N + 1) * 2]; n long long getVertex(long long x, long long y, long long sh) { n   return sh * (X + 1) * (Y + 1) + x * (Y + 1) + y; n } n queue<long long> q; n long long dist[(N + 1) * (N + 1) * 2], ways[(N + 1) * (N + 1) * 2]; n int main() { n   init(); n   long long n, k; n   cin >> n >> k; n   while (n--) { n     long long w; n     cin >> w; n     if (w == 50) { n       X++; n     } else { n       Y++; n     } n   } n   for (long long x = 0; x <= X; x++) { n     for (long long y = 0; y <= Y; y++) { n       for (long long x0 = 0; x0 <= x; x0++) { n         for (long long y0 = 0; y0 <= y; y0++) { n           if (x0 + y0 >= 1 && (50 * x0 + 100 * y0 <= k)) { n             long long w = choose(x, y, x0, y0); n             adj[getVertex(x, y, 0)].push_back(pair<long long, long long>( n                 getVertex(X - x + x0, Y - y + y0, 1), w)); n             adj[getVertex(x, y, 1)].push_back(pair<long long, long long>( n                 getVertex(X - x + x0, Y - y + y0, 0), w)); n           } n         } n       } n     } n   } n   long long start = getVertex(X, Y, 0), end = getVertex(X, Y, 1); n   for (long long u = 0; u <= (X + 1) * (Y + 1) * 2 - 1; u++) { n     dist[u] = 1000000000000000000LL; n   } n   q.push(start); n   dist[start] = 0; n   ways[start] = 1; n   while (!q.empty()) { n     long long u = q.front(); n     q.pop(); n     for (pair<long long, long long> vw : adj[u]) { n       long long v = vw.first, w = vw.second; n       if (dist[u] + 1 < dist[v]) { n         q.push(v); n         dist[v] = dist[u] + 1; n         ways[v] = (ways[u] * w) % 1000000007LL; n       } else if (dist[u] + 1 == dist[v]) { n         ways[v] = (ways[v] + ways[u] * w) % 1000000007LL; n       } n     } n   } n   cout << (dist[end] == 1000000000000000000LL ? -1 : dist[end]) <<   n  n        << ways[end] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   char a, b; n   a = s[0]; n   b = s[s.length() - 1]; n   int j = s.length() - 2; n   int i = 0; n   int maxcount = 0; n   while (j - i >= 0) { n     int k1 = j - i; n     int k = s[k1]; n     int q1 = s.length() - 1; n     int q = s[q1]; n     int count = 0; n     while (k1 > 0 && q == k) { n       count++; n       q1--; n       q = s[q1]; n       k1--; n       k = s[k1]; n     } n     if (q == k) { n       count++; n     } else { n       k1 = -1; n     } n     if (maxcount < count && k1 == 0) { n       maxcount = count; n     } n     i++; n   } n   string x = s.substr(0, s.length() - maxcount); n   string y = s.substr(s.length() - maxcount, s.length()); n   for (int i = 0; i < k; i++) { n     cout << x; n   } n   cout << y; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, d, t, result, stored = 0; n   cin >> n >> d; n   for (int i = 0; i < n; i++) { n     cin >> t; n     stored += t; n   } n   d -= (n - 1) * 10; n   if (d - stored >= 0) { n     cout << (n - 1) * 2 + (d - stored) / 5; n   } else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solution() { n   long long n; n   long long k; n   cin >> n >> k; n   vector<long long> v(n); n   for (long long i = 0; i < n; i++) cin >> v[i]; n   long long sum = 0; n   sort(v.begin(), v.end()); n   for (long long i = 0; i < (k); i++) { n     sum += (v[n - k - 1 - i] / v[n - 1 - i]); n   } n   for (long long i = 0; i < n - 2 * k; i++) sum += v[i]; n   cout << sum << endl; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) solution(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Get() { n   char c; n   while (c = getchar(), (c <  0  || c >  9 ) && c !=  - ) n     ; n   bool Flag = (c ==  - ); n   if (Flag) c = getchar(); n   int X = 0; n   while (c >=  0  && c <=  9 ) { n     X = X * 10 + c - 48; n     c = getchar(); n   } n   return Flag ? -X : X; n } n int main() { n   int N = Get(), Sum = 0; n   for (int i = 0; i < N; i++) Sum += Get(); n   printf( %d n , (Sum % N) ? (N - 1) : N); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n namespace StrAlgo { n vector<int> Kmp(string s) { n   int n = (int(s.size())); n   vector<int> ans(n + 1); n   for (int i = 2; i <= n; i++) { n     ans[i] = ans[i - 1]; n     while (ans[i] > 0 && s[ans[i]] != s[i - 1]) ans[i] = ans[ans[i]]; n     ans[i] += s[ans[i]] == s[i - 1]; n   } n   return ans; n } n vector<int> Z(string s) { n   int n = (int(s.size())); n   vector<int> ans(n + 1); n   int L = -1, R = -1; n   for (int i = 2; i <= n; i++) { n     if (i < R) n       ans[i] = min(ans[i - L], R - i); n     else n       L = i, R = i; n     while (i + ans[i] <= n && s[i + ans[i] - 1] == s[ans[i] - 1]) ans[i]++; n     if (i + ans[i] > R) L = i, R = i + ans[i]; n   } n   return ans; n } n };  // namespace StrAlgo n void print(string s, int len) { n   if (len == 0) n     cout <<  Just a legend n ; n   else n     cout << s.substr((int(s.size())) - len) <<   n ; n   exit(0); n } n int cnt[maxn]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(), cout.tie(); n   string s; n   cin >> s; n   vector<int> kmp = StrAlgo::Kmp(s); n   for (int x : kmp) cnt[x]++; n   if (cnt[kmp.back()] > 1) print(s, kmp.back()); n   print(s, kmp[kmp.back()]); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double EPS = 1e-9; n const long long MOD = 100000000; n const int N = 300009; n const int N2 = 300009; n void fast() { std::ios_base::sync_with_stdio(0); } n int main() { n   long long n, m; n   long long x = 0; n   scanf( %lld%lld , &n, &m); n   vector<long long> b(n), g(m); n   for (long long &i : b) { n     scanf( %lld , &i); n     x += i * m; n   } n   for (long long &i : g) scanf( %lld , &i); n   sort(b.rbegin(), b.rend()); n   sort(g.rbegin(), g.rend()); n   long long i = 0, cnt = 0; n   long long ans = 0; n   if (g[m - 1] < b[0] || b[0] > g[m - 1]) { n     puts( -1 ); n     return 0; n   } n   for (long long j = 0; j < m; j++) { n     if (cnt == m - 1) i++, cnt = 0; n     if (g[j] > b[0]) { n       ans += g[j] - b[i]; n       cnt++; n     } n   } n   printf( %lld , ans + x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 21; n int n; n long long s, a[N], down = 1, ans; n void exgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n     return; n   } n   exgcd(b, a % b, y, x); n   y -= a / b * x; n } n long long inv(long long x) { n   long long res, y; n   exgcd(x, 1000000007, res, y); n   return res; n } n long long C(long long u, long long d) { n   if (d < u) return 0; n   long long res = 1; n   for (long long i = d; i > d - u; i--) (res *= i % 1000000007) %= 1000000007; n   return res * down % 1000000007; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> s; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) (down *= i) % 1000000007; n   down = inv(down); n   for (int i = 0; i < (1 << n); i++) { n     long long f = 1, d = s + n - 1, u = n - 1; n     for (int j = 0; j < n; j++) n       if (i & (1 << j)) { n         d -= a[j] + 1; n         f *= -1; n       } n     (ans += f * C(u, d)) %= 1000000007; n   } n   cout << (ans + 1000000007) % 1000000007; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500001; n const int oo = 1e9; n const int LOGN = 20; n const int MOD = 1000000007; n const double PI = acos(-1); n int n, a[N]; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   int mn = oo, idx = -1; n   for (int t = 1; t <= 101; ++t) { n     int cur = 0; n     for (int j = 0; j < n; ++j) { n       int cst = max(0, abs(a[j] - t) - 1); n       cur += cst; n     } n     if (cur < mn) { n       mn = cur; n       idx = t; n     } n   } n   cout << idx <<     << mn << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double EPS = 1e-6; n const int MOD = 1e9 + 7; n const int MAXN = 1100001; n const double PI = acos(-1.0); n long long ipt[MAXN]; n long long lsum[MAXN], rsum[MAXN]; n long long sum[MAXN]; n int main() { n   long long n, len; n   while (cin >> n >> len) { n     memset(sum, 0, sizeof(sum)); n     memset(lsum, 0, sizeof(lsum)); n     memset(rsum, 0, sizeof(rsum)); n     for (int i = 0; i < (n); ++i) cin >> ipt[i]; n     for (int i = (1); i < (n); ++i) { n       sum[i] += sum[i - 1] + ipt[i] - ipt[i - 1]; n     } n     long long group = 0; n     for (long long i = 0; i + len < n; i += len) { n       long long l = len * group, r = l + len; n       if (group) lsum[group] = lsum[group - 1]; n       lsum[group] += (sum[r] - sum[l]) * (group + 1); n       group++; n     } n     group = 0; n     for (long long i = n - 1; i - len >= 0; i -= len) { n       long long r = len * group, l = r + len; n       l = n - 1 - l; n       r = n - 1 - r; n       if (group) rsum[group] = rsum[group - 1]; n       rsum[group] += (sum[r] - sum[l]) * (group + 1); n       group++; n     } n     long long ans = 1e18; n     for (long long i = 0; i < n; i++) { n       long long t = 0; n       if (i / len > 0) t += lsum[i / len - 1] * 2; n       if ((n - 1 - i) / len > 0) t += rsum[(n - 1 - i) / len - 1] * 2; n       t += (sum[i] - sum[i / len * len]) * 2 * (i / len + 1); n       t += (sum[n - 1 - (n - 1 - i) / len * len] - sum[i]) * 2 * n            ((n - 1 - i) / len + 1); n       ans = min(ans, t); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using cat = long long; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(10); n   int N, MX = 500000; n   cin >> N; n   vector<cat> A(N); n   for (int i = 0; i < N; i++) cin >> A[i]; n   sort(begin(A), end(A)); n   vector<int> cnt(MX + 1, 0), cnt_ps(MX + 1, 0); n   for (int i = 0; i < N; i++) cnt[A[i]]++; n   for (int i = 0; i <= MX; i++) cnt_ps[i] = (i ? cnt_ps[i - 1] : 0) + cnt[i]; n   cat ans = 0; n   for (int y = 2; y <= MX; y++) { n     cat cnt_y = 0; n     for (int i = 1; i <= MX / y; i++) n       cnt_y += 1LL * (cnt_ps[min(MX, y * i + y - 1)] - cnt_ps[y * i - 1]) * i; n     if (cnt_y < 2) continue; n     int min_x = min(1LL * y, cnt_y - 2), max_x = min(MX + 1LL, cnt_y + 1); n     while (max_x - min_x > 1 && 1LL * (max_x - 1) * y > ans) { n       int x = (min_x + max_x) / 2; n       if (2 * (x / y) + x > cnt_y) { n         max_x = x; n         continue; n       } n       if (2 * x <= MX) { n         int cnt2_r = 0; n         for (int i = (2 * x) / y; i <= MX / y; i++) n           cnt2_r += cnt_ps[min(MX, y * i + y - 1)] - n                     cnt_ps[min(MX, max(0, y * i + (2 * x) % y - 1))]; n         if (cnt2_r >= 1 && x + (2 * x) / y <= cnt_y) { n           min_x = x; n           continue; n         } n         if (cnt_ps[MX] > cnt_ps[2 * x - 1] && n             x + (2 * x + y - 1) / y <= cnt_y) { n           min_x = x; n           continue; n         } n       } n       int cnt_r = 0; n       for (int i = x / y; i <= MX / y; i++) n         cnt_r += cnt_ps[min(MX, y * i + y - 1)] - n                  cnt_ps[min(MX, max(0, y * i + x % y - 1))]; n       if (cnt_r >= 2) { n         min_x = x; n         continue; n       } n       if (cnt_ps[MX] >= 2 + cnt_ps[x - 1] && n           x + 2 * ((x + y - 1) / y) - cnt_r <= cnt_y) { n         min_x = x; n         continue; n       } n       max_x = x; n     } n     if (min_x >= 2) ans = max(ans, 1LL * min_x * y); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e4; n int main() { n   ios::sync_with_stdio(false); n   int n; n   while (cin >> n) { n     int a[12]; n     for (int i = 0; i < n; ++i) cin >> a[i]; n     sort(a, a + n); n     long long p = clock(); n     while (clock() - p < 1400) n       ; n     cout << a[0]; n     for (int i = 1; i < n; ++i) cout <<     << a[i]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class DTrie { n   int N = 30; n   struct node { n     node* left = nullptr; n     node* right = nullptr; n     int cnt = 0; n   }; n   void _add(node* cur, int a, int p) { n     cur->cnt++; n     if (p == -1) return; n     if (a & (1 << p)) { n       if (cur->right == nullptr) cur->right = new node(); n       _add(cur->right, a, p - 1); n     } else { n       if (cur->left == nullptr) cur->left = new node(); n       _add(cur->left, a, p - 1); n     } n   } n   int _get(node* cur, int a, int p) { n     cur->cnt--; n     if (p == -1) return 0; n     int result = 0; n     if (a & (1 << p)) { n       if (cur->right == nullptr || cur->right->cnt == 0) n         result = _get(cur->left, a, p - 1); n       else n         result = (1 << p) + _get(cur->right, a, p - 1); n     } else { n       if (cur->left == nullptr || cur->left->cnt == 0) n         result = (1 << p) + _get(cur->right, a, p - 1); n       else n         result = _get(cur->left, a, p - 1); n     } n     return result; n   } n   node* root = nullptr; n  n  public: n   DTrie() { root = new node(); } n   void add(int a) { _add(root, a, N); } n   int get(int a) { return _get(root, a, N); } n }; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<int> a(n), p(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n   } n   DTrie dt; n   for (int i = 0; i < n; i++) { n     dt.add(p[i]); n   } n   for (int i = 0; i < n; i++) { n     cout << (a[i] ^ dt.get(a[i])) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int s, tens, sum = 0; n     cin >> s; n     for (; s > 0;) { n       tens = s / 10; n       s -= tens * 10; n       s += tens; n       sum += tens * 10; n       if (s >= 10 && s <= 18) { n         s -= 10; n         s++; n         sum += 10; n         if (s) { n           sum += s; n           s = 0; n         } n       } else if (s <= 9) { n         sum += s; n         s = 0; n       } n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using std::map; n using std::max; n using std::min; n using std::set; n using std::sort; n using std::swap; n using std::vector; n inline int readInt() { n   int ans = 0; n   char c; n   do c = getchar(); n   while (!isdigit(c)); n   while (isdigit(c)) { n     ans = ans * 10 + c -  0 ; n     c = getchar(); n   } n   return ans; n } n const int N = 55; n int A[N][N]; n int main() { n   int n = readInt(); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) A[i][j] = readInt(); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       if (A[i][j] != 1) { n         bool ok = false; n         for (int k = 0; k < n; ++k) n           if (i != k) { n             for (int l = 0; l < n; ++l) n               if (j != l && A[i][j] == A[k][j] + A[i][l]) { n                 ok = true; n                 break; n               } n             if (ok) break; n           } n         if (!ok) return puts( NO ), 0; n       } n   return puts( YES ), 0; n } n


#include <bits/stdc++.h> n const int N = 4e6 + 10; n int idx[N], rank[N]; n struct Point { n   int x, y; n } A[N], Line[N]; n inline double slope(Point a) { n   return double(A[a.x].y - A[a.y].y) / double(A[a.x].x - A[a.y].x); n } n inline long long area(Point A, Point B, Point C) { n   return fabs(1LL * (A.x - C.x) * (B.y - C.y) - n               1LL * (B.x - C.x) * (A.y - C.y)); n } n inline int find1(int l, int r, int u, int v, long long s) { n   int res = -1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (area(A[idx[mid]], A[u], A[v]) >= s) n       l = mid + 1, res = mid; n     else n       r = mid - 1; n   } n   return res != -1 && area(A[idx[res]], A[u], A[v]) == s ? res : -1; n } n inline int find2(int l, int r, int u, int v, long long s) { n   int res = -1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (area(A[idx[mid]], A[u], A[v]) <= s) n       l = mid + 1, res = mid; n     else n       r = mid - 1; n   } n   return res != -1 && area(A[idx[res]], A[u], A[v]) == s ? res : -1; n } n inline int solve(int u, int v, int n, long long s) { n   if (rank[u] > rank[v]) std::swap(u, v); n   int res1 = find1(1, rank[u] - 1, u, v, s); n   if (res1 != -1) return res1; n   int res2 = find2(rank[v] + 1, n, u, v, s); n   if (res2 != -1) return res2; n   std::swap(rank[u], rank[v]), std::swap(idx[rank[u]], idx[rank[v]]); n   return 0; n } n int main() { n   int n, tot = 0; n   long long s; n   std::cin >> n >> s; n   s <<= 1; n   for (int i = 1; i <= n; ++i) n     std::cin >> A[i].x >> A[i].y, idx[i] = i, rank[i] = i; n   std::sort(A + 1, A + n + 1, [&](const Point &a, const Point &b) { n     return a.x < b.x || a.x == b.x && a.y < b.y; n   }); n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) Line[++tot] = Point{i, j}; n   std::sort(Line + 1, Line + tot + 1, [&](const Point &a, const Point &b) { n     int x1 = A[a.y].x - A[a.x].x, y1 = A[a.y].y - A[a.x].y; n     int x2 = A[b.y].x - A[b.x].x, y2 = A[b.y].y - A[b.x].y; n     return 1LL * x1 * y2 - 1LL * x2 * y1 > 0; n   }); n   for (int i = 1; i <= tot; ++i) { n     int res = solve(Line[i].x, Line[i].y, n, s); n     if (res) n       return printf( Yes n%d %d n%d %d n%d %d n , (A[Line[i].x]).x, n                     (A[Line[i].x]).y, (A[Line[i].y]).x, (A[Line[i].y]).y, n                     (A[idx[res]]).x, (A[idx[res]]).y), n              0; n   } n   return puts( No ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n pair<int, int> Result[100005]; n bool Use[100005]; n int counter, Array[100005]; n void buildArray(int prime) { n   Array[0] = 0; n   for (int k = prime; k <= N; k += prime) { n     if (Use[k] == 0) { n       Array[++Array[0]] = k; n       Use[k] = 1; n     } n   } n } n void makeArrayValid() { n   if (Array[0] % 2 == 0) return; n   Use[Array[2]] = 0; n   swap(Array[Array[0]], Array[2]); n   Array[0]--; n } n void addToResult() { n   int i; n   for (i = 1; i <= Array[0]; i += 2) { n     Result[++counter].first = Array[i]; n     Result[counter].second = Array[i + 1]; n   } n } n void Eratostenes() { n   int i, j; n   int last = 3; n   for (i = 3; i <= N / 2; i += 2) { n     if (Use[i] == 1) continue; n     buildArray(i); n     makeArrayValid(); n     addToResult(); n   } n   buildArray(2); n   makeArrayValid(); n   addToResult(); n } n int main() { n   cin >> N; n   Eratostenes(); n   cout << counter <<   n ; n   for (int i = 1; i <= counter; i++) n     cout << Result[i].first <<     << Result[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n); n   long long i, j; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       if (a[i] == 0) continue; n       if (a[j] % a[i] == 0) { n         a[j] = 0; n       } n     } n   } n   long long cnt = 0; n   for (i = 0; i < n; i++) n     if (a[i] > 0) cnt++; n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double x, y; n   point(double x = 0, double y = 0) : x(x), y(y) {} n   point operator-(const point &b) const { return point(x - b.x, y - b.y); } n   double operator*(const point &b) const { return x * b.y - y * b.x; } n }; n point p[200050]; n int a[200050], q[200050]; n double sum[200050], t[200050], E[200050], dp[55][200050]; n bool check(int i, int j, double v) { n   return p[j].y - p[i].y <= (p[i].x - p[j].x) * v; n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) { n     sum[i] = sum[i - 1] + a[i]; n     t[i] = t[i - 1] + 1.0 / a[i]; n     E[i] = E[i - 1] + sum[i] / a[i]; n   } n   for (int i = 1; i <= n; ++i) dp[1][i] = E[i]; n   for (int x = 2; x <= k; ++x) { n     int head = 0, tail = 0; n     q[tail++] = 0; n     for (int i = 1; i <= n; ++i) { n       if (i < x) { n         p[i] = point(-sum[i], sum[i] * t[i] - E[i] + dp[x - 1][i]); n         while (head + 1 < tail && n                (p[q[tail - 1]] - p[q[tail - 2]]) * (p[i] - p[q[tail - 2]]) >= 0) n           tail--; n         q[tail++] = i; n       } n       while (head + 1 < tail && check(q[head], q[head + 1], t[i])) head++; n       int j = q[head]; n       dp[x][i] = p[j].x * t[i] + p[j].y + E[i]; n       p[i] = point(-sum[i], sum[i] * t[i] - E[i] + dp[x - 1][i]); n       while (head + 1 < tail && n              (p[q[tail - 1]] - p[q[tail - 2]]) * (p[i] - p[q[tail - 2]]) >= 0) n         tail--; n       q[tail++] = i; n     } n   } n   printf( %.9f n , dp[k][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e9; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int a[n + 5]; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     ans += a[i] * a[i]; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n / 2; i++) { n     ans += 2 * a[i] * a[n - i + 1]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = (1 << 22) + 5; n bool vis[maxn << 2][2]; n int n, m; n int a[maxn]; n bool p[maxn]; n void dfs(int id, int s) { n   if (vis[id][s]) return; n   vis[id][s] = 1; n   for (int i = 0; i < n; i++) { n     if (id & (1 << i)) { n       dfs(id ^ (1 << i), s); n     } n   } n   if (p[id]) { n     if (vis[id][0]) return; n     vis[id][0] = 1; n     dfs((1 << n) - 1 - id, 1); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   memset(vis, 0, sizeof(vis)); n   memset(p, 0, sizeof(p)); n   for (int i = 1; i <= m; i++) { n     int x; n     scanf( %d , a + i); n     p[a[i]] = 1; n   } n   int ans = 0; n   for (int i = 1; i <= m; i++) { n     if (!vis[a[i]][0]) { n       vis[a[i]][0] = 1; n       ans++; n       dfs((1 << n) - 1 - a[i], 1); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main(int argc, char* argv[]) { n   int t, cups = 0, medals = 0, n, shelives_needed = 0; n   for (int i = 0; i < 3; i++) { n     scanf( %d , &t); n     cups += t; n   } n   for (int i = 0; i < 3; i++) { n     scanf( %d , &t); n     medals += t; n   } n   scanf( %d , &n); n   if (cups % 5 != 0) { n     shelives_needed += cups / 5 + 1; n   } else { n     shelives_needed += cups / 5; n   } n   if (medals % 10 != 0) { n     shelives_needed += medals / 10 + 1; n   } else { n     shelives_needed += medals / 10; n   } n   if (shelives_needed > n) { n     printf( NO n ); n   } else { n     printf( YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx ) n using namespace std; n int a, b, c, d, n, m, k; n long long pc[1000001]; n long long dp[7][333335]; n int f[6]; n const int N = 333333; n const long long infll = (long long)1000000000 * 1000000000; n inline void upd(long long& a, long long b) { n   if (a < b) a = b; n } n long long solve(int n) { n   long long ans = pc[n]; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     int rem = n - i; n     if (rem % 3) continue; n     ans = max(ans, pc[i] + dp[5][rem / 3]); n   } n   return ans; n } n int main() { n   scanf( %d , &k); n   --k; n   for (int _n((6) - 1), i(0); i <= _n; i++) scanf( %d , &f[i]); n   pc[0] = 0; n   for (int _n((999999)), i(1); i <= _n; i++) { n     int t = i; n     long long a = 0; n     for (int _n((6) - 1), j(0); j <= _n; j++) { n       int dig = t % 10; n       if (dig == 3 || dig == 6 || dig == 9) { n         a += (long long)f[j] * (dig / 3); n       } n       t /= 10; n     } n     pc[i] = a; n   } n   for (int _n((7) - 1), i(0); i <= _n; i++) n     for (int _n((N + 2) - 1), j(0); j <= _n; j++) dp[i][j] = -8 * infll; n   for (int _n((N + 1) - 1), i(0); i <= _n; i++) { n     if (i > 3 * k) break; n     dp[0][i] = (long long)f[0] * i; n   } n   auto ev1 = [&](int i, int s) { n     return (long long)i * (f[1] - 10LL * f[0]) + (long long)s * f[0]; n   }; n   for (int _n((N + 1) - 1), i(0); i <= _n; i++) { n     if (i > 33LL * k) break; n     int l = 0; n     if (i > 3 * k) { n       int rem = i - 3 * k; n       l = rem / 10; n       if (rem % 10) ++l; n     } n     int r = min(3 * k, i / 10); n     if (l > r) continue; n     dp[1][i] = max(ev1(l, i), ev1(r, i)); n   } n   for (int _n(5), d(2); d <= _n; d++) { n     long long o = 0; n     for (int _n((d + 1) - 1), z(0); z <= _n; z++) o = o * 10 + 3; n     o *= k; n     o = min<long long>(o, N); n     int t = 1; n     for (int _n((d)-1), j(0); j <= _n; j++) t *= 10; n     for (int _n((o + 1) - 1), i(0); i <= _n; i++) { n       int r = min(3 * k, i / t); n       long long cd = 0; n       for (int _n((r + 1) - 1), j(0); j <= _n; j++) { n         upd(cd, dp[d - 1][i - j * t] + (long long)f[d] * j); n       } n       dp[d][i] = cd; n     } n   } n   scanf( %d , &m); n   for (int _n((m)), hod(1); hod <= _n; hod++) { n     scanf( %d , &n); n     long long ans = solve(n); n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[10000001]; n int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } n int lcm(int a, int b) { return ((a * b) / gcd(a, b)); } n int main() { n   int n; n   scanf( %d , &n); n   int sum = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int a; n       scanf( %d , &a); n       if (i == j) sum += a; n     } n   } n   sum %= 2; n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     int t; n     scanf( %d , &t); n     if (t == 3) n       cout << sum; n     else { n       int y; n       scanf( %d , &y); n       sum = 1 - sum; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > re; n vector<pair<int, int> > e, o; n int n, c[404][404], sink, Find, pre[404], flow; n bool ch[100009], visit[404]; n void init() { n   for (int i = 2; i * i <= 100000; i++) { n     if (!ch[i]) { n       for (int j = i * i; j <= 100000; j += i) ch[j] = 1; n     } n   } n } n void dfs(int cur) { n   visit[cur] = 1; n   if (cur == sink) Find = 1; n   if (Find) return; n   for (int i = 0; i <= sink && !Find; i++) { n     if (c[cur][i] > 0 && !visit[i]) { n       dfs(i); n       pre[i] = cur; n     } n   } n } n void solve() { n   init(); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     if (a % 2) n       o.push_back(pair<int, int>(a, i + 1)); n     else n       e.push_back(pair<int, int>(a, i + 1)); n   } n   for (int i = 0; i < o.size(); i++) { n     c[0][i + 1] = 2; n   } n   sink = e.size() + o.size() + 1; n   for (int i = 0; i < e.size(); i++) { n     c[o.size() + i + 1][sink] = 2; n   } n   for (int i = 0; i < o.size(); i++) { n     for (int j = 0; j < e.size(); j++) { n       int t = o[i].first + e[j].first; n       if (!ch[t]) c[i + 1][o.size() + j + 1] = 1; n     } n   } n   for (;;) { n     memset(visit, 0, sizeof(visit)); n     Find = 0; n     dfs(0); n     if (!Find) break; n     flow++; n     int st, en; n     en = sink; n     while (en) { n       st = pre[en]; n       c[st][en]--; n       c[en][st]++; n       en = st; n     } n   } n   if (flow != 2 * e.size()) { n     puts( Impossible ); n     return; n   } n   memset(visit, 0, sizeof(visit)); n   for (int i = 1; i < sink; i++) { n     if (visit[i]) continue; n     vector<int> tmp; n     int cur = i; n     while (!visit[cur]) { n       visit[cur] = 1; n       if (cur <= o.size()) n         tmp.push_back(o[cur - 1].second); n       else n         tmp.push_back(e[cur - o.size() - 1].second); n       for (int i = 1; i < sink; i++) { n         if (!visit[i] && ((cur <= o.size() && c[i][cur] > 0) || n                           cur > o.size() && c[cur][i] > 0)) { n           cur = i; n           break; n         } n       } n     } n     re.push_back(tmp); n   } n   printf( %d n , re.size()); n   for (int i = 0; i < re.size(); i++) { n     printf( %d  , re[i].size()); n     for (int j = 0; j < re[i].size(); j++) printf( %d  , re[i][j]); n     puts(  ); n   } n } n int main(void) { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   cin >> N; n   int max = INT_MIN, min = INT_MAX; n   for (int i = 0; i < N; ++i) { n     int x; n     cin >> x; n     if (x > max) max = x; n     if (x < min) min = x; n   } n   cout << max - min + 1 - N << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b[1001], c, cnt1, cnt2, d, n, m; n string s; n int main() { n   cin >> a; n   if (a == 1) { n     cout << 1; n     return 0; n   } n   for (int i = 1; i <= a; i++) { n     cin >> b[i]; n     c += b[i]; n   } n   sort(b + 1, b + a + 1); n   reverse(b + 1, b + a + 1); n   for (int i = 1; i <= a; i++) { n     d = (c - b[i]); n     n += c - d; n     cnt1++; n     if (n > d) { n       cout << cnt1; n       return 0; n     } n     c = d; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 20; n const int MAXX = 65536; n int n, m; n long long year; n vector<int> graph[MAXN], rev_graph[MAXN]; n vector<int> sol; n long long dp[MAXX]; n long long countPermutations(int mask, int pos) { n   dp[0] = 1; n   for (int i = 1; i < 1 << (n - pos); ++i) { n     dp[i] = 0; n   } n   for (int i = 0; i < 1 << (n - pos); ++i) { n     int value = n - 1; n     while (value >= 0 && (mask & (1 << value))) --value; n     for (int j = 0; j < n - pos; ++j) n       if (i & (1 << j)) { n         do { n           --value; n         } while (value >= 0 && (mask & (1 << value))); n       } n     for (int j = 0; j < n - pos; ++j) n       if ((i & (1 << j)) == 0) { n         int ok = 1; n         for (size_t k = 0; k < graph[j + pos].size() && ok; ++k) n           if ((graph[j + pos][k] >= pos && n                (i & (1 << (graph[j + pos][k] - pos))) == 0) || n               (graph[j + pos][k] < pos && sol[graph[j + pos][k]] < value)) { n             ok = 0; n           } n         for (size_t k = 0; k < rev_graph[j + pos].size() && ok; ++k) n           if (rev_graph[j + pos][k] < pos && sol[rev_graph[j + pos][k]] > value) n             ok = 0; n         if (ok) dp[i ^ (1 << j)] += dp[i]; n       } n   } n   return dp[(1 << (n - pos)) - 1]; n } n void findPermutation(int mask, int pos) { n   if (pos < n) { n     for (int i = 0; i < n; ++i) n       if ((mask & (1 << i)) == 0) { n         int ok = 1; n         for (size_t j = 0; j < graph[pos].size() && ok; ++j) n           if (graph[pos][j] < pos && sol[graph[pos][j]] < i) ok = 0; n         for (size_t j = 0; j < rev_graph[pos].size() && ok; ++j) n           if (rev_graph[pos][j] < pos && sol[rev_graph[pos][j]] > i) ok = 0; n         if (!ok) { n           continue; n         } n         mask ^= 1 << i; n         sol.push_back(i); n         long long cnt = countPermutations(mask, pos + 1); n         if (cnt >= year) { n           findPermutation(mask, pos + 1); n           break; n         } n         year -= cnt; n         mask ^= 1 << i; n         sol.pop_back(); n       } n   } n } n int main() { n   cin >> n >> year >> m; n   year -= 2000; n   for (int i = 1; i <= m; ++i) { n     int x, y; n     cin >> x >> y; n     --x, --y; n     graph[x].push_back(y); n     rev_graph[y].push_back(x); n   } n   findPermutation(0, 0); n   if ((int)sol.size() != n) { n     cout <<  The times have changed n ; n     return 0; n   } n   for (int i = 0; i < n - 1; ++i) cout << sol[i] + 1 <<    ; n   cout << sol[n - 1] + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long m = 0, a = 1, b = 1, ans = 0; n   while (m <= n) { n     m = a + b; n     a = b; n     b = m; n     ans++; n   } n   cout << ans - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e5; n const int Inf = 1 << 30; n double a[105]; n int main() { n   int n, cnt; n   double pos, pp, ans, sum; n   while (~scanf( %d , &n)) { n     sum = 0; n     pos = 4.5; n     pos *= n; n     for (int i = 1; i <= n; i++) { n       scanf( %lf , &a[i]); n       sum += a[i]; n     } n     cnt = 0; n     sort(a + 1, a + 1 + n); n     while (sum < pos) { n       cnt++; n       sum += 5 - a[cnt]; n     } n     printf( %d n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> tree[300003]; n int colors[300003]; n int blueC[300003]; n int redC[300003]; n int ans = 0; n pair<int, int> dfs(int nd, int pr) { n   int blue = (colors[nd] == 2); n   int red = (colors[nd] == 1); n   for (int i = 0; i < (int)tree[nd].size(); i++) { n     if (tree[nd][i] == pr) continue; n     pair<int, int> sub = dfs(tree[nd][i], nd); n     if ((bool)sub.first ^ (bool)sub.second) { n       if (sub.first) n         ans += (blueC[0] == sub.first); n       else n         ans += (redC[0] == sub.second); n     } n     blue += sub.first; n     red += sub.second; n   } n   return make_pair(blue, red); n } n void solve() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &colors[i]); n   int u, v; n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &u, &v); n     tree[u].push_back(v); n     tree[v].push_back(u); n   } n   memset(blueC, 0, sizeof(blueC)); n   memset(redC, 0, sizeof(redC)); n   for (int i = 1; i <= n; i++) { n     if (colors[i] == 1) n       redC[0]++; n     else if (colors[i] == 2) n       blueC[0]++; n   } n   dfs(1, 0); n   printf( %d , ans); n } n int main() { solve(); } n