#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n void ckmin(T &a, U b) { n   if (a > b) a = b; n } n template <class T, class U> n void ckmax(T &a, U b) { n   if (a < b) a = b; n } n const int MAXN = 52; n const int MAXM = 113; n const int MAXT = 20013; n const int INF = 1000000007; n const long double EPS = 1e-9; n const long double PI = 3.141592653589793; n vector<complex<long double> > roots; n void gen(int deg) { n   roots.resize(deg); n   roots[0] = complex<long double>(1, 0); n   roots[1] = complex<long double>(cos(2.0 * PI / deg), sin(2.0 * PI / deg)); n   for (auto i = (2); i < (deg); i++) { n     roots[i] = roots[i - 1] * roots[1]; n   } n   return; n } n void fft(vector<complex<long double> > &f) { n   int n = ((int)(f).size()); n   vector<complex<long double> > g(n); n   for (int i = n >> 1; i; i >>= 1) { n     for (int j = 0; j < n; j += i) { n       int t = (j << 1) - (((j << 1) >= n) ? n : 0); n       for (auto k = (0); k < (i); k++) { n         g[j + k] = f[t + k] + roots[j] * f[t + k + i]; n       } n     } n     swap(f, g); n   } n } n vector<long double> conv(vector<long double> p, vector<long double> q) { n   int siz = ((int)(p).size()) + ((int)(q).size()) - 1, n       siz1 = (1 << (32 - __builtin_clz(siz))); n   vector<long double> res(siz); n   if (((int)(p).size()) <= 100 || ((int)(q).size()) <= 100) { n     for (auto i = (0); i < (((int)(p).size())); i++) { n       for (auto j = (0); j < (((int)(q).size())); j++) { n         res[i + j] += p[i] * q[j]; n       } n     } n     return res; n   } n   vector<complex<long double> > p1(((int)(p).size())), q1(((int)(q).size())); n   for (auto i = (0); i < (((int)(p).size())); i++) n     p1[i] = complex<long double>(p[i], 0); n   for (auto i = (0); i < (((int)(q).size())); i++) n     q1[i] = complex<long double>(q[i], 0); n   gen(siz1); n   p1.resize(siz1); n   q1.resize(siz1); n   fft(p1); n   fft(q1); n   for (auto i = (0); i < (siz1); i++) p1[i] *= q1[i]; n   fft(p1); n   for (auto i = (0); i < (siz1); i++) p1[i] /= siz1; n   reverse(p1.begin() + 1, p1.end()); n   for (auto i = (0); i < (siz); i++) { n     res[i] = real(p1[i]); n   } n   return res; n } n int N, M, T, X; n vector<int> edge[MAXN]; n int to[MAXM], cost[MAXM]; n vector<long double> prob[MAXM], dp[MAXN]; n int path[MAXN]; n priority_queue<pair<int, int>, vector<pair<int, int> >, n                greater<pair<int, int> > > n     pq1; n queue<int> q; n bitset<MAXN> active; n vector<long double> comb(vector<long double> a, vector<long double> b) { n   for (auto i = (0); i < (T - 1); i++) b.push_back(b[T]); n   reverse((a).begin(), (a).end()); n   vector<long double> c = conv(a, b); n   vector<long double> res(T); n   for (auto i = (0); i < (T); i++) { n     res[i] = c[T - 1 + i]; n   } n   return res; n } n int32_t main() { n   cout << fixed << setprecision(12); n   cerr << fixed << setprecision(4); n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> N >> M >> T >> X; n   T++; n   for (auto i = (0); i < (M); i++) { n     int u, v, c; n     cin >> u >> v >> c; n     u--; n     v--; n     edge[v].push_back(i); n     to[i] = u; n     cost[i] = c; n     prob[i].resize(T); n     for (auto j = (1); j < (T); j++) { n       int x; n       cin >> x; n       prob[i][j] = 0.00001 * x; n     } n   } n   fill(path, path + N, INF); n   path[N - 1] = 0; n   pq1.push({0, N - 1}); n   while (!pq1.empty()) { n     int d = pq1.top().first, u = pq1.top().second; n     pq1.pop(); n     if (d != path[u]) { n       continue; n     } n     for (int e : edge[u]) { n       int v = to[e]; n       if (path[v] > d + cost[e]) { n         path[v] = d + cost[e]; n         pq1.push({path[v], v}); n       } n     } n   } n   for (auto i = (0); i < (N - 1); i++) { n     dp[i].resize(T + 1); n     fill((dp[i]).begin(), (dp[i]).end(), INF); n     dp[i][T] = path[i] + X; n   } n   dp[N - 1].resize(T + 1); n   fill((dp[N - 1]).begin(), (dp[N - 1]).end(), 0); n   dp[N - 1][T] = X; n   q.push(N - 1); n   active[N - 1] = true; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     if (!active[u]) continue; n     active[u] = false; n     for (int e : edge[u]) { n       int v = to[e]; n       vector<long double> res = comb(prob[e], dp[u]); n       bool ins = false; n       for (auto j = (0); j < (T); j++) { n         res[j] += cost[e]; n         if (dp[v][j] > res[j] + EPS) { n           dp[v][j] = res[j]; n           ins = true; n         } n       } n       if (ins && !active[v]) { n         active[v] = true; n         q.push(v); n       } n     } n   } n   cout << dp[0][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[9] = {-1, 0, 1, 0, -1, -1, 1, 1, 0}; n const int dy[9] = {0, 1, 0, -1, -1, 1, -1, 1, 0}; n long long n; n void solve() { n   int a = sqrt(n); n   int b = cbrt(n); n   int c = sqrt(cbrt(n)); n   cout << a + b - c <<   n ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int T; n   cin >> T; n   while (T--) { n     cin >> n; n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)2e5 + 5; n const int MOD = 998244353; n int bit[MAXN]; n bool got[MAXN]; n int p[MAXN]; n void gcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return; n   } n   gcd(b, a % b, y, x); n   y -= (a / b) * x; n   return; n } n long long inv(long long a) { n   long long x, y; n   gcd(a, MOD, x, y); n   return (x % MOD + MOD) % MOD; n } n void add(int idx, int n) { n   while (idx <= n) { n     bit[idx]++; n     idx += idx & -idx; n   } n   return; n } n int query(int idx) { n   int sum = 0; n   while (idx > 0) { n     sum += bit[idx]; n     idx -= idx & -idx; n   } n   return sum; n } n void init(int n) { n   for (int i = 1; i <= n; i++) { n     bit[i] = 0; n   } n   return; n } n long long calc(int n) { n   long long ans = 0; n   init(n); n   for (int i = n; i >= 1; i--) { n     if (p[i] != -1) { n       ans += query(p[i]); n       add(p[i], n); n     } n   } n   return ans % MOD; n } n long long expected(int n) { n   long long ans = (1LL * n * (n - 1)) % MOD; n   ans *= inv(4); n   return ans % MOD; n } n bool compatible(vector<int> v, int n) { n   for (int i = 1; i <= n; i++) { n     if (p[i] != -1 && v[i - 1] != p[i]) { n       return false; n     } n   } n   return true; n } n long long get_inversions(vector<int> v, int n) { n   init(n); n   long long ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     ans += query(v[i]); n     add(v[i], n); n   } n   return ans % MOD; n } n long long brute(int n) { n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     v[i] = i + 1; n   } n   long long ans = 0; n   long long perm = 0; n   do { n     if (compatible(v, n)) { n       ans += get_inversions(v, n); n       perm++; n     } n   } while (next_permutation(v.begin(), v.end())); n   printf( here ans = %lld, perm = %lld, inv = %lld n , ans, perm, inv(perm)); n   return (1LL * ans * inv(perm)) % MOD; n } n int main(void) { n   int n; n   int vacant = 0; n   int start_vacant = 0; n   scanf(  %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf(  %d , &p[i]); n     got[p[i]] = true; n     if (p[i] == -1) { n       vacant++; n     } n   } n   init(n); n   start_vacant = vacant; n   for (int i = 1; i <= n; i++) { n     if (!got[i]) { n       add(i, n); n     } n   } n   long long ans = 0; n   int left = 0; n   for (int i = 1; i <= n; i++) { n     if (p[i] != -1) { n       long long aux = 1LL * query(p[i]) * vacant; n       aux %= MOD; n       ans += aux * inv(start_vacant); n       ans %= MOD; n       aux = 1LL * (query(n) - query(p[i])) * left; n       aux %= MOD; n       ans += aux * inv(start_vacant); n       ans %= MOD; n     } else { n       left++; n       vacant--; n     } n   } n   ans += expected(start_vacant); n   ans += calc(n); n   ans %= MOD; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[15][15], b[15][15], t[15][15]; n int n; n void copy() { n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) t[i][j] = a[i][j]; n } n int check() { n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       if (a[i][j] != b[i][j]) return 0; n   return 1; n } n void Upd() { n   copy(); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) a[i][j] = t[n - j - 1][i]; n } n void put() { n   for (int i = 0; i < n; ++i, puts(  )) printf( %s , a[i]); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %s , a[i]); n   for (int i = 0; i < n; ++i) scanf( %s , b[i]); n   for (int i = 0; i < 4; ++i) { n     if (check()) return puts( Yes ), 0; n     Upd(); n   } n   copy(); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) a[i][j] = t[n - i - 1][j]; n   for (int i = 0; i < 4; ++i) { n     if (check()) return puts( Yes ), 0; n     Upd(); n   } n   copy(); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) a[i][j] = t[i][n - j - 1]; n   for (int i = 0; i < 4; ++i) { n     if (check()) return puts( Yes ), 0; n     Upd(); n   } n   return puts( No ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt; n bool f; n vector<int> p, m, z; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     if (x < 0) m.push_back(x); n     if (x > 0) p.push_back(x); n     if (x == 0) z.push_back(x); n   } n   printf( 1 %d n , m[0]); n   if (p.size() > 0) n     printf( 1 %d  n , p[0]); n   else { n     printf( 2 %d %d n , m[1], m[2]); n     f = true; n   } n   cnt = z.size(); n   for (int i = (f ? 0 : 1); i < p.size(); i++) cnt++; n   for (int i = (f ? 3 : 1); i < m.size(); i++) cnt++; n   printf( %d  , cnt); n   for (int i = 0; i < z.size(); i++) printf( %d  , z[0]); n   for (int i = (f ? 0 : 1); i < p.size(); i++) printf( %d  , p[i]); n   for (int i = (f ? 3 : 1); i < m.size(); i++) printf( %d  , m[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf_int = 2e9; n const long long inf_ll = 2e18; n int Gcd(int p, int q) { return q == 0 ? p : Gcd(q, p % q); } n int Pow(int p, int q) { n   int ans = 1; n   while (q) { n     if (q & 1) ans = ans * p; n     p = p * p; n     q >>= 1; n   } n   return ans; n } n inline int read() { n   int ra, fh; n   char rx; n   rx = getchar(), ra = 0, fh = 1; n   while ((rx <  0  || rx >  9 ) && rx !=  - ) rx = getchar(); n   if (rx ==  - ) fh = -1, rx = getchar(); n   while (rx >=  0  && rx <=  9 ) ra *= 10, ra += rx - 48, rx = getchar(); n   return ra * fh; n } n int n, q; n vector<int> v[300010]; n vector<bool> vis; n int op, x; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   scanf( %d%d , &n, &q); n   int ans = 0, k = 0; n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &op, &x); n     if (op == 1) { n       x--; n       v[x].push_back(vis.size()); n       vis.push_back(true); n       ans++; n     } else if (op == 2) { n       x--; n       for (int j : v[x]) { n         if (vis[j]) { n           ans--; n           vis[j] = false; n         } n       } n       v[x].clear(); n     } else { n       for (int j = k; j < x; j++) { n         if (vis[j]) { n           vis[j] = false; n           ans--; n         } n       } n       k = max(k, x); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, b; n   scanf( %d , &n); n   int a = sqrt(n); n   if (n % a == 0) n     b = n / a; n   else n     b = n / a + 1; n   printf( %d , (a + b) * 2); n } n


#include <bits/stdc++.h> n using namespace std; n long long tu[2000001], tu1[2000001]; n int main() { n   long long n, m; n   cin >> n; n   long long q; n   map<long long, long long> wp; n   for (long long a = 1; a <= n; a++) { n     scanf( %I64d , &q); n     wp[q]++; n   } n   cin >> m; n   for (long long a = 1; a <= m; a++) scanf( %I64d , &tu[a]); n   for (long long a = 1; a <= m; a++) scanf( %I64d , &tu1[a]); n   long long gx = -1, my = -1; n   long long zuobiao = -1; n   for (long long a = 1; a <= m; a++) { n     if (wp[tu[a]] > gx || wp[tu[a]] == gx && wp[tu1[a]] >= my) { n       gx = wp[tu[a]]; n       zuobiao = a; n       my = wp[tu1[a]]; n     } n   } n   cout << zuobiao << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, n, i; n   cin >> t; n   while (t--) { n     cin >> n; n     if (n == 1) { n       cout <<  -1 n ; n       continue; n     } n     cout <<  2 ; n     for (i = 2; i <= n; i++) { n       cout <<  9 ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n const long long INF = 1e9 + 7; n const long long maxn = 2e5 + 7; n using namespace std; n void solve() { n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   long long ans = 0; n   for (long long z = c; z < d + 1; z++) { n     long long r1 = z - c + 1; n     long long r2 = z - b + 1; n     if (r1 > b) continue; n     long long sStart = 1; n     if (r2 < b) { n       sStart += (b - r2); n     } n     r2 = max(r2, b); n     long long numTerms = c - r2 + 1; n     long long whichTermGreater = b - a + 2 - sStart; n     long long numGreater = max(min(numTerms - whichTermGreater, numTerms), 0LL); n     ans += numGreater * (b - a + 1); n     numTerms -= numGreater; n     long long finalTerm = sStart + numTerms - 1; n     long long summn = (numTerms) * (sStart + finalTerm); n     summn /= 2; n     ans += summn; n   } n   cout << ans <<   n ; n } n int main(int argc, char** argv) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> m; n int main() { n   int long long n; n   cin >> n; n   int long long arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   int long long a = 0, b = 0; n   if (arr[0] == arr[n - 1]) { n     cout << 0 <<     << n * (n - 1) / 2 << endl; n   } else { n     for (int i = 0; i < n; i++) { n       if (arr[i] == arr[0]) a++; n       if (arr[i] == arr[n - 1]) b++; n     } n     cout << arr[n - 1] - arr[0] <<     << a * b << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> xi; n vector<long long int> hi; n int main() { n   long long int n, x, h, ans = 1, temp, temp1, curr, i; n   scanf( %lld , &n); n   for (i = 0; i < n; i++) { n     scanf( %lld %lld , &x, &h); n     xi.push_back(x); n     hi.push_back(h); n   } n   curr = xi[0]; n   for (i = 1; i < n; i++) { n     temp = xi[i] - hi[i]; n     temp1 = xi[i] + hi[i]; n     if (temp > curr) { n       ans++; n       curr = xi[i]; n     } else { n       if (i + 1 < n && temp1 < xi[i + 1]) { n         ans++; n         curr = temp1; n       } else { n         if (i + 1 == n) ans++; n         curr = xi[i]; n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct single_node { n   long long total; n   long long prefix; n   long long suffix; n }; n vector<pair<long long, pair<int, int> > > v; n set<int> bounds; n long long a[300000 + 50]; n long long d[300000 + 50]; n long long c[300000 + 50]; n long long lazy[1200000 + 50]; n long long maxseg[1200000 + 50]; n long long totseg[1200000 + 50]; n long long maxpref[1200000 + 50]; n long long maxsuff[1200000 + 50]; n void build(int node, int l, int r) { n   if (l == r) { n     maxseg[node] = a[l]; n     maxpref[node] = a[l]; n     maxsuff[node] = a[l]; n     totseg[node] = a[l]; n     return; n   } n   int mid = (l + r) / 2; n   build(2 * node + 1, l, mid); n   build(2 * node + 2, mid + 1, r); n   totseg[node] = totseg[2 * node + 1] + totseg[2 * node + 2]; n   maxseg[node] = max(maxsuff[2 * node + 1] + maxpref[2 * node + 2], n                      max(maxseg[2 * node + 1], maxseg[2 * node + 2])); n   maxpref[node] = n       max(maxpref[2 * node + 1], totseg[2 * node + 1] + maxpref[2 * node + 2]); n   maxsuff[node] = n       max(maxsuff[2 * node + 2], totseg[2 * node + 2] + maxsuff[2 * node + 1]); n   return; n } n single_node query(int node, int l, int r, int ql, int qr) { n   if (l == r) { n     if (ql <= l && qr >= r) { n       single_node bc; n       bc.total = totseg[node]; n       bc.prefix = maxpref[node]; n       bc.suffix = maxsuff[node]; n       return bc; n     } else { n       single_node bc; n       bc.total = 0; n       bc.prefix = 0; n       bc.suffix = 0; n       return bc; n     } n   } n   if (ql <= l && qr >= r) { n     single_node bc; n     bc.total = totseg[node]; n     bc.prefix = maxpref[node]; n     bc.suffix = maxsuff[node]; n     return bc; n   } else if (ql > r || qr < l) { n     single_node bc; n     bc.total = 0; n     bc.prefix = 0; n     bc.suffix = 0; n     return bc; n   } n   int mid = (l + r) / 2; n   single_node le = query(2 * node + 1, l, mid, ql, qr); n   single_node ri = query(2 * node + 2, mid + 1, r, ql, qr); n   single_node res; n   res.total = le.total + ri.total; n   res.prefix = max(le.prefix, le.total + ri.prefix); n   res.suffix = max(ri.suffix, ri.total + le.suffix); n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   long long aa; n   cin >> n >> aa; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> d[i] >> c[i]; n     a[i] = aa - c[i]; n     ans = max(ans, a[i]); n     if (i > 0) { n       v.push_back(make_pair(d[i] - d[i - 1], make_pair(i - 1, i))); n     } n   } n   build(0, 0, n - 1); n   sort(v.begin(), v.end()); n   bounds.insert(0); n   bounds.insert(n - 1); n   for (int i = v.size() - 1; i >= 0; i--) { n     int leend, ribeg; n     leend = v[i].second.first; n     ribeg = v[i].second.second; n     set<int>::iterator iter1, iter2; n     iter1 = bounds.lower_bound(ribeg); n     iter2 = bounds.upper_bound(leend); n     --iter2; n     int lebeg, riend; n     lebeg = (*iter2); n     riend = (*iter1); n     ans = max(ans, query(0, 0, n - 1, lebeg, leend).suffix + n                        query(0, 0, n - 1, ribeg, riend).prefix - n                        (v[i].first * v[i].first)); n     bounds.insert(leend); n     bounds.insert(ribeg); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void preKmp(char *src, int m, int *rule) { n   int i = 0, j = -1; n   rule[0] = j; n   while (i < m) { n     while (j != -1 && src[j] != src[i]) j = rule[j]; n     i++, j++; n     if (j >= m) n       rule[i] = rule[j - 1]; n     else n       rule[i] = j; n   } n } n int recount = 0; n void kmp(char *src, int m, char *dest, int n) { n   int rule[10005]; n   preKmp(src, m, rule); n   int i = 0, j = 0; n   while (i < n) { n     while (j != -1 && src[j] != dest[i]) { n       j = rule[j]; n     } n     i++, j++; n     if (j == m) { n       recount++; n       j = rule[j]; n     } n   } n } n char Reg[10005], ori[1000005]; n char os[100][300], a[500], temp[500]; n int l[100], r[100], n, len, le[100]; n int main() { n   set<string> now; n   set<string>::iterator iter; n   bool flag; n   int ans = 0, i, j, k; n   while (scanf( %s , a) != EOF) { n     now.clear(); n     scanf( %d , &n); n     for (i = 1; i <= n; i++) { n       scanf( %s%d%d , os[i], &l[i], &r[i]); n       le[i] = strlen(os[i]); n     } n     len = strlen(a); n     ans = 0; n     for (i = 0; i < len; i++) n       for (j = i; j < len; j++) { n         flag = true; n         for (k = 1; k <= n; k++) { n           recount = 0; n           kmp(a + i, j - i + 1, os[k], le[k]); n           if (recount < l[k] || recount > r[k]) { n             flag = false; n             break; n           } n         } n         if (flag) { n           for (k = i; k <= j; k++) temp[k - i] = a[k]; n           temp[k - i] =   0 ; n           string wc = temp; n           iter = now.find(wc); n           if (iter == now.end()) { n             now.insert(wc); n             ans++; n           } n         } n       } n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 10; n int n, a[N], vis[N]; n int main() { n   scanf( %d , &n); n   int64_t ans = 0; n   int f = 0, g = 1, pos = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     ans += abs(a[i] - i); n     if (a[i] == 1) continue; n     if (a[i] > i) n       ++f, ++vis[a[i] - i]; n     else n       ++g, ++vis[a[i] - i + n]; n   } n   int64_t ret = ans; n   for (int k = 1; k < n; ++k) { n     ret -= abs(n + 1 - a[n - k + 1]); n     ret += abs(1 - a[n - k + 1]); n     ret += -f + g; n     if (a[n - k + 1] != 1) ++f, --g; n     f -= vis[k]; n     g += vis[k]; n     if (ret < ans) ans = ret, pos = k; n   } n   printf( %lld %d n , ans, pos); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a[1010], maxx = -1, res[1010], ans = 0; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (res[a[i]] > 0) n       res[a[i]]++; n     else n       res[a[i]] = 1; n     maxx = max(maxx, res[a[i]]); n   } n   maxx = maxx + ((k - (maxx % k)) % k); n   for (int i = 1; i < 101; i++) { n     if (res[i] > 100 || res[i] < 1) continue; n     ans += (maxx - res[i]); n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, sum = 0; n     cin >> a >> b; n     while (a > 0 && b > 0) { n       if (a < b) swap(a, b); n       sum += a / b; n       a = a - b * (a / b); n     } n     cout << sum << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename T> n std::ostream& operator<<(std::ostream& str, const std::vector<T>& v) { n   str <<  [ ; n   for (auto n : v) str << n <<  ,  ; n   str <<  ] ; n   return str; n } n template <typename K, typename V> n std::ostream& operator<<(std::ostream& str, const std::unordered_map<K, V>& m) { n   str <<  [ ; n   for (auto n : m) str << n.first <<   =>   << n.second <<  ,  ; n   str <<  ] ; n   return str; n } n void gen(int i, string c, vector<string>& r) { n   if (i == 0) { n     r.push_back(c); n     return; n   } n   gen(i - 1, c +  4 , r); n   gen(i - 1, c +  7 , r); n } n int main() { n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   int input; n   cin >> input; n   std::vector<string> all_n; n   for (int i = 1; i <= 10; i++) { n     gen(i - 1,  4 , all_n); n     gen(i - 1,  7 , all_n); n   } n   stringstream ss; n   ss << input; n   string inp_string = ss.str(); n   for (int i = 0; i < all_n.size(); i++) { n     if (all_n[i] == inp_string) { n       cout << i + 1 << endl; n       return 0; n     } n   } n   cout <<  all_n  n        <<  :   << all_n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, x, y; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d%d , &n, &x, &y); n     int d, dif = y - x; n     for (int i = 1; i <= dif; i++) { n       if (dif % i == 0 && (dif / i <= n - 1)) { n         d = i; n         break; n       } n     } n     printf( %d , y); n     n--; n     int val = y - d; n     while (val > 0 && n > 0) { n       n--; n       printf(  %d , val); n       val -= d; n     } n     val = y + d; n     while (n > 0) { n       n--; n       printf(  %d , val); n       val += d; n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const long long INF = 1e18 + 5; n const int MAXN = 1e6; n const int base = 317; n const long long bs = 1e5 + 1; n const long long mod = 998244353; n struct node { n   node *left, *right; n   int val; n   node(node* l = NULL, node* r = NULL, int v = 0) { n     left = l; n     right = r; n     val = v; n   } n }; n node* version[N]; n int n, m, d[N], up[21][N]; n int tin[N], tout[N], timer; n vector<int> g[N]; n void build(node* v, int L, int R) { n   if (L == R) return; n   int md = (L + R) >> 1; n   v->left = new node(); n   v->right = new node(); n   build(v->left, L, md); n   build(v->right, md + 1, R); n } n node* upd(node* v, int L, int R, int pos, int val) { n   if (L == R) { n     return new node(NULL, NULL, val); n   } n   node* nw = new node(); n   int md = (L + R) >> 1; n   if (md >= pos) { n     nw->right = v->right; n     nw->left = upd(v->left, L, md, pos, val); n   } else { n     nw->left = v->left; n     nw->right = upd(v->right, md + 1, R, pos, val); n   } n   nw->val = nw->left->val + nw->right->val; n   return nw; n } n void dfs(int v, int p = 0) { n   d[v] = d[p] + 1; n   up[0][v] = p; n   tin[v] = ++timer; n   for (int i = 1; i <= 20; i++) { n     up[i][v] = up[i - 1][up[i - 1][v]]; n   } n   for (int to : g[v]) { n     if (to != p) { n       dfs(to, v); n     } n   } n   tout[v] = ++timer; n } n int lca(int a, int b) { n   if (d[a] > d[b]) { n     swap(a, b); n   } n   for (int i = 20; i >= 0; i--) { n     if (d[a] <= d[up[i][b]]) { n       b = up[i][b]; n     } n   } n   if (a == b) return a; n   for (int i = 20; i >= 0; i--) { n     if (up[i][a] != up[i][b]) { n       a = up[i][a]; n       b = up[i][b]; n     } n   } n   return up[0][a]; n } n int get(node* v, int L, int R, int l, int r) { n   if (L > r || l > R) return 0; n   if (l <= L && R <= r) return v->val; n   int md = (L + R) >> 1; n   return get(v->left, L, md, l, r) + get(v->right, md + 1, R, l, r); n } n int calc(int u, int v, int y1, int y2) { n   int r = tin[u], l = tin[v]; n   return d[u] - d[v] + 1 - n          (get(version[y2], 1, 2 * n, l, r) - get(version[y1], 1, 2 * n, l, r)); n } n int getk(int a, int k, int y1, int y2) { n   for (int i = 20; i >= 0; i--) { n     int count = calc(a, up[i][a], y1, y2) - calc(a, a, y1, y2); n     if (count < k) { n       k -= count; n       a = up[i][a]; n     } n   } n   return up[0][a]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int p; n     cin >> p; n     g[i].push_back(p); n     g[p].push_back(i); n   } n   dfs(g[0][0], g[0][0]); n   version[0] = new node(); n   build(version[0], 0, n * 2); n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       int id; n       cin >> id; n       version[i] = upd(version[i - 1], 1, 2 * n, tin[id], 1); n       version[i] = upd(version[i], 1, 2 * n, tout[id], -1); n     } else { n       int a, b, k, y; n       cin >> a >> b >> k >> y; n       version[i] = version[i - 1]; n       int lc = lca(a, b); n       int len1 = calc(a, lc, y, i) - calc(a, a, y, i), n           len2 = calc(b, lc, y, i) - calc(b, b, y, i); n       int count = len1 + len2 - calc(lc, lc, y, i); n       if (count < k) { n         cout << -1 <<   n ; n         continue; n       } n       if (len1 >= k) { n         cout << getk(a, k, y, i) <<   n ; n       } else { n         cout << getk(b, count - k + 1, y, i) <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 0x7f7f7f7f; n inline void gettime() { ; } n inline int read() { n   static char c; n   static int x; n   int flag = 1; n   while (c = getchar(), !isdigit(c)) n     if (c ==  - ) flag = -1; n   x = c -  0 ; n   while (c = getchar(), isdigit(c)) x = (x << 1) + (x << 3) + c -  0 ; n   return x * flag; n } n inline char achar() { n   static char c; n   while (c = getchar(), !isdigit(c)) n     ; n   return c; n } n int n, ones; n char s[200200]; n std::queue<int> ns[200200]; n std::queue<int> ps[2]; n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; ++i) ones += s[i] ==  1 ; n   int k = (n - ones) - ones; n   if (k <= 0) return puts( -1 ), 0; n   for (int i = 1; i <= k; ++i) ps[0].push(i); n   for (int i = 1; i <= n; ++i) { n     int cur = s[i] ==  1 ; n     if (ps[cur].empty()) return puts( -1 ), 0; n     ns[ps[cur].front()].push(i); n     ps[cur ^ 1].push(ps[cur].front()); n     ps[cur].pop(); n   } n   if (!ps[0].empty()) return puts( -1 ), 0; n   printf( %d n , k); n   for (int i = 1; i <= k; ++i) { n     printf( %d  , ns[i].size()); n     for (; !ns[i].empty(); ns[i].pop()) printf( %d  , ns[i].front()); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e6; n long long n, m; n long long a[N]; n long long read(void) { n   long long s = 0, w = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) w |= c ==  - , c = getchar(); n   while (c >=  0  && c <=  9 ) s = s * 10 + c - 48, c = getchar(); n   return w ? -s : s; n } n long long point(long long x) { n   if (x % 2 == 1) return x * (x - 1) / 2 + 1; n   if (x % 2 == 0) return x * (x - 1) / 2 + x / 2; n } n signed main(void) { n   n = read(), m = read(); n   for (long long i = 1; i <= m; ++i) { n     long long q = read(); n     a[i] = read(); n   } n   sort(a + 1, a + m + 1); n   reverse(a + 1, a + m + 1); n   long long sum = 0; n   for (long long i = 1; i <= m; ++i) { n     if (point(i) > n) return cout << sum, 0; n     sum += a[i]; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7, P = 1e7 + 19; n const int N = 200005; n int nt = 1, k, n, Q, x, y, tot, op; n long long pm(long long x, long long p) { n   long long res = 1; n   while (p) { n     if (p & 1) res = res * x % M; n     p >>= 1; n     x = x * x % M; n   } n   return res; n } n struct node { n   int l, r, v, w; n } q[N << 2]; n int qc[N]; n vector<int> a; n long long f[50][2000]; n void cdq(int l, int r, int d, vector<int> a) { n   if (d > 1) { n     for (int i = 1; i <= k; i++) f[d][i] = f[d - 1][i]; n   } n   for (int i = 0; i < (int)a.size(); ++i) n     if (q[a[i]].l <= l && r <= q[a[i]].r) { n       for (int j = k; j >= q[a[i]].w; j--) { n         f[d][j] = max(f[d][j], f[d][j - q[a[i]].w] + q[a[i]].v); n       } n     } n   if (l == r) { n     long long res = 0; n     for (int i = k; i >= 1; i--) { n       res = (res * P + f[d][i]) % M; n     } n     for (int i = 1; i <= qc[l]; i++) { n       printf( %I64d n , res); n     } n     return; n   } n   int mid = l + r >> 1; n   vector<int> la, ra; n   for (int i = 0; i < (int)a.size(); ++i) { n     if (q[a[i]].l <= l && r <= q[a[i]].r) continue; n     if (q[a[i]].r <= mid) { n       la.push_back(a[i]); n     } else if (q[a[i]].l > mid) { n       ra.push_back(a[i]); n     } else { n       la.push_back(a[i]); n       ra.push_back(a[i]); n     } n   } n   cdq(l, mid, d + 1, la); n   cdq(mid + 1, r, d + 1, ra); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x, &y); n     q[++tot] = (node){1, 0, x, y}; n   } n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d , &op); n     if (op == 1) { n       ++tot; n       ++nt; n       scanf( %d%d , &x, &y); n       q[tot] = (node){nt, 0, x, y}; n     } else if (op == 2) { n       scanf( %d , &x); n       q[x].r = nt; n       ++nt; n     } else { n       qc[nt]++; n     } n   } n   for (int i = 1; i <= tot; i++) { n     a.push_back(i); n     if (q[i].r == 0) q[i].r = nt; n   } n   cdq(1, nt, 1, a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 201; n inline void NEXT(const char *T, int *next, int t_len) { n   next[0] = -1; n   for (int i = 1; i < t_len; i++) { n     int j = next[i - 1]; n     while (j >= 0 && T[i - 1] != T[j]) j = next[j]; n     if (j >= 0 && T[i - 1] == T[j]) n       next[i] = j + 1; n     else n       next[i] = 0; n   } n } n int COUNT_KMP(const char *S, const char *T, int s_len, int t_len) { n   int next[t_len]; n   NEXT(T, next, t_len); n   int index, count = 0; n   for (index = 0; index < s_len; ++index) { n     int pos = 0; n     int iter = index; n     while (pos < t_len && iter < s_len) { n       if (S[iter] == T[pos]) { n         ++iter; n         ++pos; n       } else { n         if (pos == 0) n           ++iter; n         else n           pos = next[pos - 1] + 1; n       } n     } n     if (pos == t_len && (iter - index) == t_len) ++count; n   } n   return count; n } n int my_substr(char *s, char *t, int pos, int len) { n   int i; n   for (i = 0; i < len; ++i) { n     if (t[i + pos] ==   0 ) { n       break; n     } n     s[i] = t[i + pos]; n   } n   s[i] =   0 ; n   return i; n } n int64_t cal(const char *s, int n, int64_t k) { n   int64_t sum[MAX_N]; n   memset(sum, 0, sizeof(sum)); n   char f[MAX_N][MAX_N * 10 + 1]; n   int len[MAX_N]; n   memset(len, 0, sizeof(len)); n   int s_len = strlen(s); n   sprintf(f[0],  0 ); n   sprintf(f[1],  1 ); n   len[0] = len[1] = 1; n   if (s_len == 1) { n     if (s[0] ==  0 ) { n       sum[0]++; n     } else if (s[0] ==  1 ) { n       sum[1]++; n     } n   } n   int max_len = s_len - 1; n   char tmp_str[MAX_N * 10 + 1]; n   for (int i = 2; i <= n; ++i) { n     len[i] = len[i - 2] + len[i - 1]; n     sprintf(f[i],  %s%s , f[i - 2], f[i - 1]); n     int start = 0; n     if (len[i - 2] > max_len) { n       start = len[i - 2] - max_len; n     } n     int my_len = my_substr(tmp_str, f[i - 2], start, max_len); n     my_len += my_substr(tmp_str + my_len, f[i - 1], 0, max_len); n     int64_t result = COUNT_KMP(tmp_str, s, my_len, s_len); n     sum[i] = sum[i - 2] + sum[i - 1] + result; n     if (sum[i] > k) { n       return sum[i]; n     } n     if (len[i] > max_len * 2) { n       int del_num = len[i] - (max_len * 2); n       for (int pos = max_len + del_num; pos < len[i]; ++pos) { n         f[i][pos - del_num] = f[i][pos]; n       } n       len[i] = max_len * 2; n       f[i][len[i]] =   0 ; n     } n   } n   return sum[n]; n } n int process() { n   int n, m; n   int64_t k; n   char suffix[] = n        110101101011011010110101101101011011010110101101101011011010110101101101  n        011010110110101101101011010110110101101011011010110110101101011011010110  n        11010110101101101011010110110101101101011010110110101101 ; n   int suffix_len = strlen(suffix); n   cin >> n >> k >> m; n   char s[MAX_N]; n   memset(s,   0 , sizeof(s)); n   for (int i = 0; i < m; ++i) { n     int pos = suffix_len - i; n     int s_pos = 0; n     while (pos < suffix_len) { n       if (s[s_pos] != suffix[pos]) { n         break; n       } n       s_pos++; n       pos++; n     } n     if (pos == suffix_len && i > 0) { n       if (k == 1) { n         break; n       } n       k--; n     } n     if (i > 0 && s[i - 1] ==  0 ) { n       s[i] =  1 ; n       s[i + 1] =   0 ; n       continue; n     } n     if (i > 1 && s[i - 1] ==  1  && s[i - 2] ==  1 ) { n       s[i] =  0 ; n       s[i + 1] =   0 ; n       continue; n     } n     s[i] =  0 ; n     s[i + 1] =   0 ; n     int64_t count = cal(s, n, k); n     if (count < k) { n       k = k - count; n       s[i] =  1 ; n     } n   } n   printf( %s n , s); n   return 0; n } n int main(int argc, char *argv[]) { n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch -  0 ; n   x *= f; n } n int t, n; n const int maxn = 1e5 + 10; n int a[maxn]; n map<int, bool> taked; n int main() { n   read(t); n   while (t--) { n     bool flag = true; n     taked.clear(); n     read(n); n     int cnt = 0; n     for (register int i = 0; i < n; i++) { n       read(a[i]); n       int c = ((a[i] + cnt) % n + n) % n; n       if (taked[c]) flag = false; n       taked[c] = true; n       cnt++; n     } n     if (flag) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #include <iostream> n #define ll long long n using namespace std; n  n const long long mod=998244353; n const long long mod2=1ll<<32; n const double pi = 2 * acos(0.0); n  n const ll N=200001; n  n  n bool compare(pair<int,int> quan1,pair<int,int> quan2) n { n     return quan1.second <quan2.second ; n } n ll power(ll base,ll expo) n { n     ll ans=1; n     while(expo>0) n     { n         if(expo%2==1) n         { n             ans*=base; n             ans=(ans)%mod; n             expo--; n         } n         else n         { n             base*=base; n             base=(base)%mod; n             expo/=2; n         } n     } n     return ans; n } n  n ll minimax(ll t, bool isMax,ll score,ll h,ll scores[]) n { n  n  if (t+1 == h) n   return score; n  n  if (isMax&&scores[t]!=1) n     { n       if(scores[t]%2==0) n       { n         return max(minimax(t+1, false, score+scores[t]-1, h,scores), n    minimax(t+1,true, score+scores[t], h,scores)); n       } n       else n       { n        return max(minimax(t+1, true, score+scores[t]-1, h,scores), n    minimax(t+1, false, score+scores[t], h,scores)); n       } n     } n  else if (isMax&&scores[t]==1) n     { n       return minimax(t+1, false, score+1, h,scores); n     } n  n  else if (!isMax&&scores[t]!=1) n     { n       if(scores[t]%2==0) n       { n         return max(minimax(t+1, true, score, h,scores), n    minimax(t+1,false, score, h,scores)); n       } n       else n       { n        return max(minimax(t+1, false, score, h,scores), n    minimax(t+1, true, score, h,scores)); n       } n     } n    else if (!isMax&&scores[t]==1) n     { n      return minimax(t+1, true, score, h,scores); n     } n } n  n long long exponentiation(long long num_one,long long num_two,long long Mod) n { n     long long answer= 1; n     num_one = ((num_one)+Mod) % Mod; n     while (num_two > 0) n     { n         if (num_two & 1) n         { n             answer =(((answer*num_one))+Mod) % Mod; n         } n         num_two = num_two>>1; n         num_one = (((num_one*num_one))+Mod) % Mod; n     } n     return ((answer%Mod))%Mod; n } n  n ll combi(ll n,ll r,ll mod) n { n  if(r==0) n     { n         return 1; n     } n     if(r>n) n     { n         return 0; n     } n     ll factorial[N+1]; n     ll inverse_fac[N+1]; n     factorial[0]=1; n     inverse_fac[0]=1; n     for(ll i=1;i<=n;i++) n     { n         factorial[i]=(factorial[i-1]*i)%mod; n         inverse_fac[i]=power(factorial[i],mod-2); n     } n  return factorial[n]* inverse_fac[r]%mod* inverse_fac[n-r]%mod; n } n  n long long gcd(long long a,long long b) n { n     if(b==0) n     { n         return a; n     } n     else n     { n         return gcd(b,a%b); n     } n } n ll LCM(ll aa[],ll n,ll Mod) n { n     ll lcm = aa[0]; n  n     for (ll i=1;i<n;i++) n     { n         lcm = ((((aa[i]*lcm)%Mod)+Mod)%Mod)/(gcd(aa[i],lcm)); n     } n     return lcm; n } n vector<ll> finding_all_prime_factors(ll n) n { n  vector<ll> v; n  while(n%2==0) n  { n   v.push_back(2); n   n = n/2; n  } n  for (ll y=3;y*y<=(n);y+=2) n  { n   while (n%y==0) n   { n    v.push_back(y); n    n = n/y; n   } n  } n  if(n>2) n     { n         v.push_back(n); n     } n     return v; n } n ll dfs(unordered_map<ll,ll> &size,ll node,bool visit[],vector<ll> adj[]) n { n     visit[node]=true; n     ll s=1; n     for(int i=0;i<adj[node].size();i++) n     { n         if(visit[adj[node][i]]==false) n         { n             s+=dfs(size,adj[node][i],visit,adj); n         } n     } n     size[node]=s; n     return size[node]; n } n ll node; n  n void b_f_s(ll visited[],queue<ll> q,ll vertex,vector<ll> v[],vector<ll> adj[]) n { n     q.push(vertex); n     visited[vertex]=1; n     while(q.size()>0) n     { n         node=q.front(); n         visited[node]=1; n         q.pop(); n         for(ll i=0;i<adj[node].size();i++) n         { n             if(visited[adj[node][i]]==0) n             { n                 //visited[adj[node][i]]=1; n                 q.push(adj[node][i]); n                 v[adj[node][i]]=v[node]; n                 v[adj[node][i]].push_back(node); n             } n         } n     } n } n  n void sieve_to_count_primes(ll n) n { n     ll is_it_prime[1000001]; n     for(ll i=1;i<=1000000;i++) n     { n         is_it_prime[i]=1; n     } n     for(ll i=2;i<=sqrt(1000000);i++) n     { n         if(is_it_prime[i]==1) n         { n             for(ll j=i*i;j<=1000000;) n             { n                 is_it_prime[j]=0; n                 j+=i; n             } n         } n     } n } n  n ll LIS_Size(vector<ll>& v) n { n  if(v.size()==0) n     { n         return 0; n     } n  vector<ll> vv(v.size(),0); n  ll size=1; n  vv[0]=v[0]; n  for (ll i=1;i<v.size();i++) n  { n   auto b=vv.begin(),e =vv.begin()+size; n   auto iter=lower_bound(b,e,v[i]); n   if(iter==vv.begin()+size) n   { n       vv[size]=v[i]; n       size++; n   } n   else n   { n       *iter=v[i]; n   } n  } n  return size; n } n  n  n ll sum_in_rational_form_up(ll n,ll ups[],ll downs[],ll Mod) n { n  ll ans_down=LCM(downs,n,Mod); n  ll ans_up=0; n  for (ll j=0;j<n;j++) n  { n   ans_up=(ans_up+((ups[j])*(ans_down/downs[j])))%Mod; n  } n  ll g_c_d=gcd(ans_up,ans_down); n  ans_up/=g_c_d; n  ans_down/=g_c_d; n  //cout<<ans_up<<   <<ans_down; n  return ((ans_up%Mod)+Mod)%Mod; n     //cout<<ans.first<<   <<ans.second; n } n  n ll sum_in_rational_form_down(ll n,ll ups[],ll downs[],ll Mod) n { n  ll ans_down=LCM(downs,n,Mod); n  ll ans_up=0; n  for (ll j=0;j<n;j++) n  { n   ans_up=(ans_up+((ups[j])*(ans_down/downs[j])))%Mod; n  } n  ll g_c_d=gcd(ans_up,ans_down); n  ans_up/=g_c_d; n  ans_down/=g_c_d; n  //cout<<ans_up<<   <<ans_down; n  return (ans_down%Mod+Mod)%Mod; n     //cout<<ans.first<<   <<ans.second; n } n  n ll mod_inverse(ll a,ll m) n { n     ll GCD=gcd(a,m); n     if(GCD==1) n     { n         return power(a,m-2); n     } n } n  n bool possible(ll ind,vector<ll>& from_left,ll val1,ll val2,vector<ll>& from_right) n { n     ll n=from_left.size(); n     if(ind>=1&&(ind+2)<n) n     { n         if(val2>=from_left[ind-1]&&((val1-(val2-from_left[ind-1]))==from_right[ind+2])) n         { n             return true; n         } n         else n         { n             return false; n         } n     } n     else if(ind==0) n     { n         if(((val1-val2)==from_right[ind+2])) n         { n             return true; n         } n         else n         { n             return false; n         } n     } n     else n     { n         if(((val2-val1)==from_left[ind-1])) n         { n             return true; n         } n         else n         { n             return false; n         } n     } n } n  n  n int main() n { n  ios::sync_with_stdio(false); n  cin.tie(0); n  ll t,n,k,m; n     cin>>t; n     while(t--) n     { n         cin>>n; n         vector<ll> aa(n); n         for(ll i=0;i<n;i++) n         { n             cin>>aa[i]; n         } n         if(n==2) n         { n             if(aa[0]==aa[1]) n             { n                 cout<< YES <<endl; n             } n             else n             { n                 cout<< NO <<endl; n             } n         } n         else n         { n             vector<ll> bb(n); n             bb=aa; n             vector<ll> from_left(n),from_right(n); n             from_left[0]=bb[0]; n             for(ll i=1;i<n;i++) n             { n                 if(bb[i]<from_left[i-1]) n                 { n                     from_left[i]=LLONG_MIN; n                     continue; n                 } n                 if(from_left[i-1]==LLONG_MIN) n                 { n                     from_left[i]=LLONG_MIN; n                     continue; n                 } n                 from_left[i]=bb[i]-from_left[i-1]; n             } n             bb=aa; n             from_right[n-1]=bb[n-1]; n             for(ll i=n-2;i>=0;i--) n             { n                 if(bb[i]<from_right[i+1]) n                 { n                     from_right[i]=LLONG_MIN; n                     continue; n                 } n                 if(from_right[i+1]==LLONG_MIN) n                 { n                     from_right[i]=LLONG_MIN; n                     continue; n                 } n                 from_right[i]=bb[i]-from_right[i+1]; n             } n             ll f=0; n             if(from_left[n-1]==0||from_right[0]==0) n             { n                 f=1; n             } n             for(ll i=0;i<n-1;i++) n             { n                 if(i>=1&&from_left[i-1]==LLONG_MIN) n                 { n                     continue; n                 } n                 if(i+2<=(n-1)&&from_right[i+2]==LLONG_MIN) n                 { n                     continue; n                 } n                 if(possible(i,from_left,aa[i],aa[i+1],from_right)==true) n                 { n                     f=1; n                     break; n                 } n             } n             if(f==1) n             { n                 cout<< YES <<endl; n             } n             else n             { n                 cout<< NO <<endl; n             } n  n         } n  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 arr[5] = {0}; n   for (int i = 0; i < n; ++i) { n     int tmp; n     cin >> tmp; n     arr[tmp]++; n   } n   int ret = arr[4]; n   ret += min(arr[1], arr[3]); n   ret += arr[2] / 2; n   if (arr[3] > arr[1]) { n     ret += arr[3] - arr[1]; n     ret += (arr[2] % 2) ? 1 : 0; n   } else { n     int tmp = arr[1] - arr[3]; n     ret += tmp / 4; n     if (arr[2] % 2) { n       if (tmp % 4 <= 2) n         ret += 1; n       else n         ret += 2; n     } else { n       ret += (tmp % 4) ? 1 : 0; n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long inf = 0x3f3f3f3f3f3f3f3f; n const long long mod = 1e9 + 7; n const int N = 2e5 + 10; n int T; n int n, m; n int cnt[1000][2], mz[40][40]; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &m); n     memset(cnt, 0, sizeof(cnt)); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         scanf( %d , &mz[i][j]); n         int a = abs(i - 1) + abs(j - 1); n         int b = abs(i - n) + abs(j - m); n         cnt[min(a, b)][mz[i][j]]++; n       } n     } n     int ans = 0; n     for (int i = 0; i <= (n - 1 + m - 1 - 1) / 2; i++) { n       ans += min(cnt[i][1], cnt[i][0]); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000005; n int N, M, dx, dy; n int xidx[MAXN]; n int cnt[MAXN]; n int ans[MAXN][2]; n int main() { n   cin >> N >> M >> dx >> dy; n   for (int i = 0; i < N; i++) xidx[((long long)i * dx) % N] = i; n   for (int i = 0; i < M; i++) { n     int x, y; n     cin >> x >> y; n     int val = (y - (long long)xidx[x] * dy) % N; n     if (val < 0) val += N; n     cnt[val]++; n     ans[val][0] = x, ans[val][1] = y; n   } n   int best = 0; n   for (int i = 1; i < N; i++) n     if (cnt[best] < cnt[i]) best = i; n   cout << ans[best][0] <<     << ans[best][1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void dbs(string str, T t) { n   cerr << str <<   :   << t <<   n ; n } n template <class T, class... S> n void dbs(string str, T t, S... s) { n   int idx = str.find( , ); n   cerr << str.substr(0, idx) <<   :   << t <<  ,  ; n   dbs(str.substr(idx + 1), s...); n } n template <class S, class T> n ostream& operator<<(ostream& os, const pair<S, T>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, const set<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class S, class T> n ostream& operator<<(ostream& os, const map<S, T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n void prc(T a, T b) { n   cerr <<  [ ; n   for (T i = a; i != b; ++i) { n     if (i != a) cerr <<  ,  ; n     cerr << *i; n   } n   cerr <<  ] n ; n } n void code() { n   long long n; n   cin >> n; n   vector<long long> cnt(4); n   long long a, b, c; n   a = b = c = 0; n   for (long long i = 0; i < (long long)(n); ++i) { n     string s; n     cin >> s; n     if (s ==  M ) a++; n     if (s ==  S ) b++; n     if (s ==  L ) n       c++; n     else if (s ==  XS ) n       cnt[1]++; n     else if (s ==  XXS ) n       cnt[2]++; n     else if (s ==  XXXS ) n       cnt[3]++; n   } n   for (long long i = 0; i < (long long)(n); ++i) { n     string s; n     cin >> s; n     if (s ==  M ) a--; n     if (s ==  S ) b--; n     if (s ==  L ) n       c--; n     else if (s ==  XS ) n       cnt[1]--; n     else if (s ==  XXS ) n       cnt[2]--; n     else if (s ==  XXXS ) n       cnt[3]--; n   } n   long long ans = 0; n   ans += abs(a) + abs(b) + abs(c); n   ans /= 2; n   ans += abs(cnt[1]) + abs(cnt[2]) + abs(cnt[3]); n   cout << ans; n } n int main() { n   int t = 1; n   for (auto i = (1); i != (t + 1); i++) { n     code(); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n void MACRO_VAR_Scan(T& t) { n   std::cin >> t; n } n template <typename First, typename... Rest> n void MACRO_VAR_Scan(First& first, Rest&... rest) { n   std::cin >> first; n   MACRO_VAR_Scan(rest...); n } n template <typename T> n void MACRO_VEC_ROW_Init(int n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_VEC_ROW_Init(n, rest...); n } n template <typename T> n void MACRO_VEC_ROW_Scan(int p, T& t) { n   std::cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { n   std::cin >> first[p]; n   MACRO_VEC_ROW_Scan(p, rest...); n } n template <class T> n inline T CHMAX(T& a, const T b) { n   return a = (a < b) ? b : a; n } n template <class T> n inline T CHMIN(T& a, const T b) { n   return a = (a > b) ? b : a; n } n template <class T> n std::vector<std::vector<T>> VV(int n, int m, T init = T()) { n   return std::vector<std::vector<T>>(n, std::vector<T>(m, init)); n } n template <typename S, typename T> n std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using PAIR = std::pair<ll, ll>; n using PAIRLL = std::pair<ll, ll>; n constexpr ll INFINT = 1 << 30; n constexpr ll INFINT_LIM = (1LL << 31) - 1; n constexpr ll INFLL = 1LL << 60; n constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); n constexpr double EPS = 1e-10; n constexpr ll MOD = 998244353; n constexpr double PI = 3.141592653589793238462643383279; n template <class T, size_t N> n void FILL(T (&a)[N], const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, size_t N, size_t M, class T> n void FILL(ARY (&a)[N][M], const T& val) { n   for (auto& b : a) FILL(b, val); n } n template <class T> n void FILL(std::vector<T>& a, const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, class T> n void FILL(std::vector<std::vector<ARY>>& a, const T& val) { n   for (auto& b : a) FILL(b, val); n } n std::vector<bool> banned; n std::vector<ll> pairSum; n std::vector<ll> getDiameterPath(const std::vector<std::vector<ll>>& g) { n   size_t n = g.size(); n   std::vector<ll> depth(n), depth2(n); n   ll maxDepth = 0, farthest = -1; n   std::function<void(std::vector<ll>&, ll, ll)> getDepth = n       [&](std::vector<ll>& d, ll v, ll par) { n         if (par == -1) { n           for (size_t i = 0; i < n; ++i) d[i] = n; n           d[v] = 0; n           maxDepth = 0, farthest = v; n         } n         for (auto&& to : g[v]) n           if (to != par && !banned[to]) { n             d[to] = d[v] + 1; n             if (PAIR(maxDepth, pairSum[farthest]) < PAIR(d[to], pairSum[to])) { n               maxDepth = d[farthest = to]; n             } n             getDepth(d, to, v); n           } n       }; n   ll start = -1; n   for (ll i = 0; i < ll(n); ++i) n     if (!banned[i]) start = i; n   getDepth(depth, start, -1); n   getDepth(depth, farthest, -1); n   getDepth(depth2, farthest, -1); n   const ll R = (maxDepth + 1) / 2; n   for (ll i = 0; i < ll(n); ++i) { n     if (banned[i]) continue; n     if (depth[i] <= R && depth2[i] <= R) { n       getDepth(depth, i, -1); n       getDepth(depth, farthest, -1); n       break; n     } n   } n   std::vector<ll> res(maxDepth + 1); n   size_t now = farthest; n   while (depth[now] != 0) { n     res[depth[now]] = now; n     for (auto&& to : g[now]) n       if (depth[to] + 1 == depth[now]) { n         now = to; n         break; n       } n   } n   res[depth[now]] = now; n   return res; n } n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   ; n   ll n; n   MACRO_VAR_Scan(n); n   ; n   std::vector<ll> a, b; n   MACRO_VEC_ROW_Init(n - 1, a, b); n   for (ll w = 0; w < n - 1; ++w) { n     MACRO_VEC_ROW_Scan(w, a, b); n   }; n   std::vector<std::vector<ll>> g(n); n   for (ll i = 0; i < ll(n - 1); ++i) { n     --a[i]; n     --b[i]; n     g[a[i]].emplace_back(b[i]); n     g[b[i]].emplace_back(a[i]); n   } n   std::vector<bool> checked(n, false); n   for (ll i = 0; i < ll(n); ++i) { n     if (g[i].size() >= 3) checked[i] = true; n   } n   banned = checked; n   { n     std::vector<bool> deleted(n, false); n     std::vector<ll> in(n, 0); n     std::queue<ll> que; n     for (ll i = 0; i < ll(n); ++i) { n       in[i] = g[i].size(); n       if (g[i].size() == 1) que.push(i), deleted[i] = true; n     } n     while (!que.empty()) { n       ll now = que.front(); n       que.pop(); n       for (auto& to : g[now]) n         if (!banned[to] && !deleted[to]) { n           deleted[to] = true; n           if (--in[to] <= 1) que.push(to); n         } n     } n     for (ll i = 0; i < ll(n); ++i) banned[i] = !deleted[i]; n   } n   std::vector<ll> depth(n, INFINT); n   auto getP = [&](ll start) { n     std::vector<PAIR> v; n     for (auto& to : g[start]) n       if (!banned[to]) { n         ll dist = 1; n         ll pre = start; n         ll now = to; n         while (true) { n           bool ch = false; n           for (auto& ne : g[now]) n             if (ne != pre) { n               ++dist; n               ch = true; n               pre = now; n               now = ne; n               break; n             } n           if (!ch) break; n         } n         v.emplace_back(dist, now); n       } n     std::sort((v).begin(), (v).end()); n     std::reverse((v).begin(), (v).end()); n     return std::vector<PAIR>({v[0], v[1]}); n   }; n   pairSum.resize(n, 0); n   for (ll i = 0; i < ll(n); ++i) { n     if (!banned[i]) continue; n     ll cnt = 0; n     for (auto& to : g[i]) n       if (banned[to]) ++cnt; n     if (cnt == 1) { n       auto pp = getP(i); n       pairSum[i] = pp[0].first + pp[1].first; n     } n   } n   for (ll i = 0; i < ll(n); ++i) banned[i] = !banned[i]; n   auto path = getDiameterPath(g); n   for (ll i = 0; i < ll(n); ++i) banned[i] = !banned[i]; n   auto pa(getP(path.front())); n   auto pb(getP(path.back())); n   std::cout << (pa[0].second + 1); n   std::cout <<    ; n   std::cout << (pb[0].second + 1); n   std::cout <<   n ; n   ; n   std::cout << (pa[1].second + 1); n   std::cout <<    ; n   std::cout << (pb[1].second + 1); n   std::cout <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 99009000; n const long long MOD = 1000000007; n const int N = 100001; n int main() { n   { n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n   }; n   long long a, b, w, x, c; n   cin >> a >> b >> w >> x >> c; n   long long A = a * w + b; n   long long B = w * c; n   long long dif = B - A; n   if (dif <= 0) n     cout << 0 <<   n ; n   else n     cout << (dif + w - x - 1) / (w - x) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2e5 + 5; n long long res = 0, mx = 0; n long long sub[MAX_N]; n int custo[MAX_N]; n vector<vector<int> > g(MAX_N); n void dfs(int node, int p = -1, int d = 0) { n   res += 1LL * d * custo[node]; n   sub[node] = custo[node]; n   for (auto to : g[node]) { n     if (to != p) { n       dfs(to, node, d + 1); n       sub[node] += sub[to]; n     } n   } n } n void root(int node, int p = -1) { n   mx = max(mx, res); n   for (auto to : g[node]) { n     if (to != p) { n       long long old_res = res; n       long long old_node = sub[node]; n       long long old_to = sub[to]; n       res -= sub[to]; n       sub[node] -= sub[to]; n       res += sub[node]; n       sub[to] += sub[node]; n       root(to, node); n       res = old_res; n       sub[node] = old_node; n       sub[to] = old_to; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> custo[i]; n   for (int i = 0; i < n - 1; i++) { n     int st, et; n     cin >> st >> et; n     --st; n     --et; n     g[st].emplace_back(et); n     g[et].emplace_back(st); n   } n   dfs(0); n   root(0); n   cout << mx <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:20000000 ) n using namespace std; n template <typename T1, typename T2, typename T3> n struct triple { n   T1 a; n   T2 b; n   T3 c; n   triple(){}; n   triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {} n }; n template <typename T1, typename T2, typename T3> n bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) { n   if (t1.a != t2.a) n     return t1.a < t2.a; n   else n     return t1.b < t2.b; n } n template <typename T1, typename T2> n inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { n   bool first = true; n   os <<  [ ; n   for (unsigned int i = 0; i < v.size(); i++) { n     if (!first) os <<  ,  ; n     os << v[i]; n     first = false; n   } n   return os <<  ] ; n } n template <typename T> n inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) { n   bool first = true; n   os <<  [ ; n   for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n template <typename T1, typename T2> n inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) { n   bool first = true; n   os <<  [ ; n   for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n template <typename T, typename T2> n void printarray(T a[], T2 sz, T2 beg = 0) { n   for (T2 i = beg; i < sz; i++) cout << a[i] <<    ; n } n inline long long binpow(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res *= x; n     x *= x; n     n >>= 1; n   } n   return res; n } n inline long long powmod(long long x, long long n, long long _mod) { n   long long res = 1; n   while (n) { n     if (n & 1) res = (res * x) % _mod; n     x = (x * x) % _mod; n     n >>= 1; n   } n   return res; n } n inline long long mulmod(long long x, long long n, long long _mod) { n   long long res = 0; n   while (n) { n     if (n & 1) res = (res + x) % _mod; n     x = (x + x) % _mod; n     n >>= 1; n   } n   return res; n } n inline long long gcd(long long a, long long b) { n   long long t; n   while (b) { n     a = a % b; n     t = a; n     a = b; n     b = t; n   } n   return a; n } n inline int gcd(int a, int b) { n   int t; n   while (b) { n     a = a % b; n     t = a; n     a = b; n     b = t; n   } n   return a; n } n inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long gcd(long long a, long long b, long long c) { n   return gcd(gcd(a, b), c); n } n inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); } n inline long long lcm(long long a, long long b, long long c) { n   return lcm(lcm(a, b), c); n } n inline long long lcm(int a, int b, int c) { n   return lcm(lcm(a, b), (long long)c); n } n inline long long max(long long a, long long b) { return (a > b) ? a : b; } n inline int max(int a, int b) { return (a > b) ? a : b; } n inline double max(double a, double b) { return (a > b) ? a : b; } n inline long long max(long long a, long long b, long long c) { n   return max(a, max(b, c)); n } n inline int max(int a, int b, int c) { return max(a, max(b, c)); } n inline double max(double a, double b, double c) { return max(a, max(b, c)); } n inline long long min(long long a, long long b) { return (a < b) ? a : b; } n inline int min(int a, int b) { return (a < b) ? a : b; } n inline double min(double a, double b) { return (a < b) ? a : b; } n inline long long min(long long a, long long b, long long c) { n   return min(a, min(b, c)); n } n inline int min(int a, int b, int c) { return min(a, min(b, c)); } n inline double min(double a, double b, double c) { return min(a, min(b, c)); } n inline void getar(int *a, int n) { n   for (int ii = 0; ii < n; ii++) { n     scanf( %d , a + ii); n   } n } n inline void getar(long long *a, int n) { n   for (int ii = 0; ii < n; ii++) { n     scanf( %I64 , a + ii); n   } n } n struct point { n   int x, y; n   point(int xx = 0, int yy = 0) { n     x = xx; n     y = yy; n   } n }; n bool operator<(const point &p1, const point &p2) { return p1.x < p2.x; } n int n; n point a[100010]; n int minpref[100010]; n int maxpref[100010]; n int minsuf[100010]; n int maxsuf[100010]; n bool check(double m) { n   for (int i = 0; i < n; i++) { n     point *v = n         upper_bound(a, a + n, point(a[i].x + 2 * m + (double)5 * (1e-7), 0)); n     double miny = min(minpref[i], minsuf[(v - a)]); n     double maxy = max(maxpref[i], maxsuf[(v - a)]); n     if (maxy - miny <= 2 * m) return true; n   } n   return false; n } n int main() { n   cin >> n; n   int x, y; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x, &y); n     a[i].x = x + y; n     a[i].y = x - y; n   } n   sort(a, a + n); n   maxpref[0] = -2111111111; n   minpref[0] = 2111111111; n   maxsuf[n] = -2111111111; n   minsuf[n] = 2111111111; n   for (int i = 1; i <= n; i++) { n     minpref[i] = min(minpref[i - 1], a[i - 1].y); n     maxpref[i] = max(maxpref[i - 1], a[i - 1].y); n     minsuf[n - i] = min(minsuf[n - i + 1], a[n - i].y); n     maxsuf[n - i] = max(maxsuf[n - i + 1], a[n - i].y); n   } n   double l = 0; n   double r = max(1.0 * a[n - 1].x - a[0].x, 1.0 * maxsuf[0] - minsuf[0]) / 2; n   while (l + (double)5 * (1e-7) < r) { n     double m = (l + r) / 2; n     if (check(m)) { n       r = m; n     } else { n       l = m; n     } n   } n   cout << fixed; n   cout.precision(7); n   cout << (l + r) / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, deg[8010], type[8010], in[8010]; n long long ans = 0; n bool e[8010][8010]; n char s[8010]; n queue<int> q; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= n / 4; j++) { n       int k = (s[j] <=  9  && s[j] >=  0 ) ? s[j] -  0  : s[j] -  A  + 10; n       for (int l = 0; l < 4; l++) e[i][j * 4 - l] = (k >> l) & 1; n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (e[i][j]) deg[j]++; n   for (int i = 1; i <= n; i++) n     if (!deg[i]) q.push(i); n   int tmp = n; n   while (!q.empty()) { n     int now = q.front(); n     q.pop(); n     tmp--; n     type[now] = 3; n     ans += (614ll * n + 1) * tmp; n     for (int i = 1; i <= n; i++) n       if (e[now][i]) { n         deg[i]--; n         if (!deg[i]) q.push(i); n       } n   } n   if (!tmp) return printf( %lld n , ans), 0; n   int pos = -1, mx = 0; n   for (int i = 1; i <= n; i++) n     if (deg[i] > mx) mx = deg[i], pos = i; n   for (int i = 1; i <= n; i++) n     if (type[i] != 3) type[i] = (i == pos || e[i][pos]) ? 1 : 2; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (i != j && type[i] != 3 && type[j] != 3 && type[i] != type[j] && n           e[i][j]) n         in[j]++; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (type[i] != 3 && type[j] != 3) n         ans += 3 + (type[i] == type[j] && in[i] == in[j]); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 7, MAXM = 1e5 + 5, MOD = 1e9 + 7, bit_one = (1 << MAXN) - 1; n int n, ans; n int w[MAXN]; n int pre_process[MAXN][(1 << MAXN)][(1 << MAXN)]; n int add(int a, int b) { n   int c = a + b; n   while (c >= MOD) c -= MOD; n   return c; n } n int mul(int a, int b) { return 1LL * a * b % MOD; } n struct matrix { n   int data[(1 << MAXN)][(1 << MAXN)]; n   int n, m; n   matrix(int a = (1 << MAXN), int b = (1 << MAXN), bool c = 0) { n     n = a; n     m = b; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) data[i][j] = (i == j) * c; n   } n   void operator*=(matrix tmp) { n     matrix res(n, tmp.m); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) n         for (int k = 0; k < tmp.m; k++) n           res.data[i][k] = add(res.data[i][k], mul(data[i][j], tmp.data[j][k])); n     (*this) = res; n   } n   void operator^=(int tmp) { n     matrix res(n, m, 1); n     for (; tmp; tmp /= 2) { n       if (tmp & 1) res *= (*this); n       (*this) *= (*this); n     } n     (*this) = res; n   } n }; n bool cheak(int a, int b, int c, int d) { n   for (int i = 0; i <= a; i++) n     if (((d >> i) & 1) && ((d >> (i + 1)) & 1) && ((b >> i) & 1) && n         ((c >> i) & 1)) n       return 0; n   return 1; n } n int main() { n   for (int i = 0; i < 7; i++) scanf( %d , &w[i]); n   for (int i = 0; i < 7; i++) n     for (int j = 0; j < (1 << (i + 1)); j++) n       for (int k = 0; k < (1 << (i + 1)); k++) n         for (int h = 0; h < (1 << i); h++) n           pre_process[i][j][k] += cheak(i, j, k, (1 << (i + 1)) | (h << 1 | 1)); n   matrix M[7]; n   for (int i = 0; i < 7; i++) M[i].n = M[i].m = (1 << (i + 1)); n   for (int i = 0; i < 7; i++) { n     for (int j = 0; j < (1 << (i + 1)); j++) n       for (int k = 0; k < (1 << (i + 1)); k++) n         M[i].data[j][k] = pre_process[i][j][k]; n     M[i] ^= w[i]; n   } n   matrix A[7]; n   for (int i = 0; i < 7; i++) { n     A[i].n = (1 << (i + 1)); n     A[i].m = 1; n   } n   A[0].data[1][0] = 1; n   M[0] *= A[0]; n   A[0] = M[0]; n   for (int i = 1; i < 7; i++) { n     for (int j = 0; j < (1 << i); j++) { n       int tmp = j << 1 | 1; n       A[i].data[tmp][0] = A[i - 1].data[j][0]; n     } n     M[i] *= A[i]; n     A[i] = M[i]; n   } n   ans = A[6].data[bit_one][0]; n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long sum[1000006], add[1000006]; n int Q[1000006], A[1000006]; n int main() { n   int n, m, a; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a); n     A[a + 1] = 0; n     int head = 1, tail = 0, mx = 0; n     for (int j = 1; j <= a; j++) scanf( %d , &A[j]), mx = max(mx, A[j]); n     Q[++tail] = 0; n     for (int j = 1; j <= a; j++) { n       while (tail >= head && A[j] > A[Q[tail]]) tail--; n       Q[++tail] = j; n       while (head <= tail && j + a - Q[head] > m) head++; n       sum[j] += A[Q[head]]; n     } n     int len = a; n     head = tail = 1; n     Q[1] = a + 1; n     for (int j = m; j >= max(m - a + 1, a + 1); j--) { n       while (tail >= head && A[len] > A[Q[tail]]) tail--; n       Q[++tail] = len--; n       while (head <= tail && j - Q[head] < 0) head++; n       sum[j] += A[Q[head]]; n     } n     if (a * 2 < m) add[a + 1] += mx, add[m - a + 1] -= mx; n   } n   for (int i = 1; i <= m; i++) n     add[i] += add[i - 1], sum[i] += add[i], printf( %lld  , sum[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, m; n     cin >> n >> m; n     vector<long long int> graph[n + 1]; n     for (long long int i = 0; i < m; i++) { n       long long int u, v; n       cin >> u >> v; n       graph[u].push_back(v); n       graph[v].push_back(u); n     } n     long long int dis[n + 1]; n     memset(dis, 0, sizeof(dis)); n     long long int s = 1; n     bool visited[n + 1]; n     memset(visited, false, sizeof(visited)); n     queue<long long int> q; n     q.push(s); n     visited[s] = true; n     dis[s] = 0; n     while (!q.empty()) { n       long long int v = q.front(); n       q.pop(); n       for (auto i : graph[v]) { n         if (!visited[i]) { n           visited[i] = true; n           q.push(i); n           dis[i] = dis[v] + 1; n         } n       } n     } n     vector<long long int> even, odd; n     for (long long int i = 1; i < n + 1; i++) { n       if (dis[i] % 2 == 0) { n         even.push_back(i); n       } else n         odd.push_back(i); n     } n     if (odd.size() < even.size()) { n       cout << odd.size() << endl; n       for (auto i : odd) cout << i <<    ; n     } else { n       cout << even.size() << endl; n       for (auto i : even) cout << i <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const double eps = 1e-50; n const int maxN = 1100; n const double inf = 1e50; n int n, m; n vector<int> a[maxN]; n double d[maxN][maxN]; n int b[maxN]; n double c[maxN][maxN]; n bool have[maxN]; n double t[maxN][maxN], s[maxN]; n int main() { n   for (int i = 0; i < maxN; ++i) { n     c[i][0] = 1; n     for (int j = 1; j <= i; ++j) { n       c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; n       if (c[i][j] >= inf) { n         c[i][j] = inf; n       } n     } n   } n   cin >> n >> m; n   vector<pair<int, int> > x; n   for (int i = 0; i < m; ++i) { n     int k; n     cin >> k; n     a[i].resize(k); n     for (int j = 0; j < k; ++j) { n       cin >> a[i][j]; n       x.push_back(make_pair(a[i][j], i)); n     } n   } n   sort(x.rbegin(), x.rend()); n   for (int i = 0; i < n; ++i) { n     if (x[i].first != x[n - 1].first) { n       ++b[x[i].second]; n     } n   } n   int needed = x[n - 1].first; n   for (int i = 0; i < m; ++i) { n     for (int j = 0; j < a[i].size(); ++j) { n       if (a[i][j] == needed) { n         have[i] = true; n       } n     } n   } n   d[0][0] = 1.0; n   t[0][0] = 1.0; n   for (int i = 0; i < m; ++i) { n     for (int j = 0; j <= n; ++j) { n       if (d[i][j] < eps) { n         d[i][j] = 0.0; n       } n       if (t[i][j] > inf) { n         t[i][j] = inf; n       } n       int cnt = b[i]; n       if (j + cnt <= n) { n         d[i + 1][j + cnt] += d[i][j] / c[a[i].size()][cnt]; n         t[i + 1][j + cnt] += t[i][j]; n       } n       if (have[i] && j + cnt + 1 <= n) { n         d[i + 1][j + cnt + 1] += d[i][j] / c[a[i].size()][cnt + 1]; n         t[i + 1][j + cnt + 1] += t[i][j]; n       } n     } n   } n   t[0][0] = 1.0; n   for (int i = 0; i <= n; ++i) { n     s[i] = t[0][i]; n     if (i > 0) { n       s[i] += s[i - 1]; n     } n   } n   printf( %.10lf n , d[m][n] / t[m][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void out() { n   cout.flush(); n   int n; n   cin >> n; n   exit(0); n } n int tree[1 << 21]; n int st = 1 << 20; n void add(int pos, int d) { n   pos += st; n   for (; pos; pos >>= 1) tree[pos] += d; n } n int findKth(int k) { n   int pos = 1; n   while (pos < st) { n     pos *= 2; n     if (tree[pos] < k) { n       k -= tree[pos]; n       pos++; n     } n   } n   return pos - st; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m, q; n   cin >> n >> m >> q; n   vector<int> kols(m, 0); n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     kols[x - 1]++; n   } n   vector<pair<int, int> > ps; n   for (int i = 0; i < m; ++i) ps.push_back(make_pair(kols[i], i)); n   sort(ps.begin(), ps.end()); n   long long inf = 1000; n   inf *= inf; n   inf = inf * inf * inf; n   inf += 1; n   vector<long long> starts(m, 0); n   for (int i = 1; i < m; ++i) n     starts[i] = min( n         inf, starts[i - 1] + (long long)i * (ps[i].first - ps[i - 1].first)); n   starts.push_back(inf + 1); n   vector<pair<long long, int> > qs; n   for (int i = 0; i < q; ++i) { n     long long k; n     cin >> k; n     qs.push_back(make_pair(k - n - 1, i)); n   } n   sort(qs.begin(), qs.end()); n   vector<int> anss(q, -1); n   memset(tree, 0, sizeof(tree)); n   int cur = 0; n   for (int i = 0; i < q; ++i) { n     while (starts[cur] <= qs[i].first) { n       add(ps[cur].second, 1); n       ++cur; n     } n     long long k = qs[i].first - starts[cur - 1]; n     k %= cur; n     anss[qs[i].second] = findKth(k + 1); n   } n   for (int i = 0; i < q; ++i) cout << anss[i] + 1 <<   n ; n   out(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   double d, h, v, e; n   scanf( %lf%lf%lf%lf , &d, &h, &v, &e); n   if (v / (d * d * 3.1415926 / 4) > e) { n     printf( YES n%.10f , h / (v / (d * d * 3.1415926 / 4) - e)); n   } else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a, b; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (x == 0) continue; n     a.push_back(x); n   } n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (x == 0) continue; n     b.push_back(x); n   } n   n = a.size(); n   if (a.size() == b.size()) { n     int s = -1; n     { n       auto it = find(begin(b), end(b), a[0]); n       if (it != b.end()) { n         s = it - b.begin(); n       } n     } n     if (s == -1) { n       cout <<  NO  << endl; n       return 0; n     } n     rotate(b.begin(), b.begin() + s, b.end()); n     cout << (a == b ?  YES  :  NO ) << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 1e6 + 7; n int main() { n   int n, q; n   cin >> n >> q; n   vector<int> a(n + 10, 0), b; n   vector<pair<int, int>> v; n   vector<int> cnt(n + 10, 0), cnt1(n + 10, 0); n   set<int> s; n   for (int i = 1; i <= n; i++) cin >> a[i], s.insert(a[i]); n   for (int i = 0; i < q; i++) { n     int l, r; n     cin >> l >> r; n     cnt[l]++; n     cnt1[r]++; n     v.push_back({l, r}); n   } n   vector<int> ans; n   int ans_mx = 0; n   for (int i = 1; i <= n; i++) { n     b = a; n     vector<int> union_segment, l(n + 10, 0), r(n + 10, 0); n     for (int j = 0; j < v.size(); j++) { n       if (v[j].second < i || v[j].first > i) { n         l[v[j].first]++; n         r[v[j].second]++; n         union_segment.push_back(j + 1); n       } n     } n     int val = 0; n     for (int j = 1; j <= n; j++) { n       val += l[j]; n       b[j] -= val; n       val -= r[j]; n     } n     int mi = 1e9, mx = -1e9; n     for (int j = 1; j <= n; j++) { n       mx = max(mx, b[j]), mi = min(mi, b[j]); n     } n     if (mx - mi > ans_mx) { n       ans_mx = mx - mi; n       ans = union_segment; n     } n   } n   cout << ans_mx <<   n ; n   cout << ans.size() <<   n ; n   for (int i : ans) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1e9 + 7; n int dp[111][1 << 18], tr[111][1 << 18], prime[111], lp, mark[111]; n int getbit(int x, int pos) { return (x >> pos) & 1; } n int onbit(int x, int pos) { return (x + (1 << pos)); } n void init() { n   int nt[200]; n   for (int i = (1), _b = (60); i <= _b; i++) nt[i] = 1; n   nt[1] = 0; n   for (int i = (2), _b = (10); i <= _b; i++) { n     if (nt[i] == 1) { n       int j = i; n       while (j < 60) { n         j += i; n         nt[j] = 0; n       } n     } n   } n   lp = -1; n   for (int i = (1), _b = (60); i <= _b; i++) { n     if (nt[i]) { n       lp++; n       prime[lp] = i; n     } n   } n   for (int i = (1), _b = (60); i <= _b; i++) mark[i] = 0; n   for (int i = (1), _b = (60); i <= _b; i++) { n     int t = 0; n     for (int j = (0), _b = (lp); j <= _b; j++) { n       if (i % prime[j] == 0) { n         t = onbit(t, j); n       } n     } n     mark[i] = t; n   } n } n int abS(int x) { n   if (x > 0) return x; n   return -x; n } n int main() { n   int n, a[111]; n   cin >> n; n   for (int i = (1), _b = (n); i <= _b; i++) cin >> a[i]; n   init(); n   int maxtt = (1 << 17) - 1; n   for (int i = (0), _b = (maxtt); i <= _b; i++) dp[0][i] = 0; n   for (int i = (1), _b = (n); i <= _b; i++) n     for (int tt = (0), _b = (maxtt); tt <= _b; tt++) dp[i][tt] = oo; n   for (int i = (1), _b = (n); i <= _b; i++) { n     for (int val = (1), _b = (60); val <= _b; val++) { n       for (int tt = (0), _b = (maxtt); tt <= _b; tt++) { n         if ((mark[val] & tt) == 0) { n           int ttnew = tt + mark[val]; n           int dpval = dp[i - 1][tt] + abS(val - a[i]); n           if (dpval < dp[i][ttnew]) { n             dp[i][ttnew] = dpval; n             tr[i][ttnew] = val; n           } n         } n       } n     } n   } n   int ret = 0; n   for (int tt = (0), _b = (maxtt); tt <= _b; tt++) n     if (dp[n][ret] > dp[n][tt]) ret = tt; n   int res[111]; n   for (int i = (n), _b = (1); i >= _b; i--) { n     res[i] = tr[i][ret]; n     ret -= mark[res[i]]; n   } n   for (int i = (1), _b = (n); i <= _b; i++) n     printf( %d%c , res[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void func() { n   long long n; n   cin >> n; n   std::vector<pair<long long, long long> > v(n); n   for (long long i = 0; i < n; ++i) { n     cin >> v[i].first; n   } n   for (long long i = 0; i < n; ++i) { n     cin >> v[i].second; n   } n   sort(v.begin(), v.end()); n   multiset<long long> s; n   long long i = 0, ans = 0, sum = 0, cur = v[0].first; n   while (i < n || !s.empty()) { n     while (v[i].first == cur && i < n) { n       s.insert(v[i].second); n       sum += v[i].second; n       ++i; n     } n     if (!s.empty()) { n       long long x = *s.rbegin(); n       sum -= x; n       ans += sum; n       s.erase(s.find(x)); n       cur++; n     } else { n       cur = v[i].first; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     func(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2 == 0) n     cout <<  8   << n - 8; n   else n     cout <<  9   << n - 9; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 2005; n const long long INF = 1e9 + 5; n char grid[MAX][MAX]; n long long dp[MAX][MAX]; n auto solve(long long n, long long k) -> string { n   auto inside = [&](long long row, long long col) { n     return (row < n and row >= 0 and col < n and col >= 0); n   }; n   for (long long row = 0; row < n; ++row) { n     for (long long col = 0; col < n; ++col) { n       dp[row][col] = INF; n     } n   } n   dp[0][0] = (grid[0][0] !=  a ) ? 1 : 0; n   for (long long row = 0; row < n; ++row) { n     for (long long col = 0; col < n; ++col) { n       if (row == 0 and col == 0) continue; n       if ((row != 0) and (col != 0)) { n         dp[row][col] = n             min(dp[row][col - 1], dp[row - 1][col]) + (grid[row][col] !=  a ); n       } else { n         dp[row][col] = (row == 0) ? dp[row][col - 1] + (grid[row][col] !=  a ) n                                   : dp[row][col]; n         dp[row][col] = (col == 0) ? dp[row - 1][col] + (grid[row][col] !=  a ) n                                   : dp[row][col]; n       } n     } n   } n   long long pref = 0; n   for (long long row = 0; row < n; ++row) { n     for (long long col = 0; col < n; ++col) { n       if (dp[row][col] <= k) { n         pref = max(pref, row + col + 1); n       } n     } n   } n   string answer(pref,  a ); n   vector<pair<long long, long long>> list; n   for (long long row = 0; row < n; ++row) { n     for (long long col = 0; col < n; ++col) { n       if ((dp[row][col] <= k) and (pref == row + col + 1)) { n         list.push_back(make_pair(row, col)); n       } n     } n   } n   if (list.empty()) { n     answer = grid[0][0]; n     list.emplace_back(0, 0); n   } n   while (!list.empty()) { n     char best_char =  z  + 1; n     vector<pair<long long, long long>> new_list; n     for (pair<long long, long long> p : list) { n       for (long long d_row : {0, 1}) { n         long long d_col = 1 - d_row; n         long long r2 = p.first + d_row; n         long long c2 = p.second + d_col; n         if (inside(r2, c2)) { n           if (grid[r2][c2] <= best_char) { n             if (grid[r2][c2] < best_char) { n               new_list.clear(); n             } n             best_char = grid[r2][c2]; n             new_list.push_back(make_pair(r2, c2)); n           } n         } n       } n     } n     new_list.resize(std::unique(new_list.begin(), new_list.end()) - n                     new_list.begin()); n     list = new_list; n     if (best_char !=  z  + 1) answer = answer + best_char; n   } n   return answer; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, k; n   std::cin >> n >> k; n   for (long long i = 0; i < n; ++i) { n     std::cin >> grid[i]; n   } n   std::cout << solve(n, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod(long long a) { n   return a >= 0 ? a % 1000000009 : (1000000009 + a % 1000000009) % 1000000009; n } n long long st[2 * 3000010]; n long long lz[2][2 * 3000010]; n long long fib[2][3000010]; n long long fibSum[2][3000010]; n long long getFib(int i, long long i0, long long i1) { n   return mod(mod(fib[0][i] * i0) + mod(fib[1][i] * i1)); n } n long long getFibSum(int i, long long i0, long long i1) { n   return mod(mod(fibSum[0][i] * i0) + mod(fibSum[1][i] * i1)); n } n long long getFibSumRange(int i, int j, int i0, int i1) { n   if (i == 0) return getFibSum(j, i0, i1); n   return mod(getFibSum(j, i0, i1) - getFibSum(i - 1, i0, i1)); n } n void buildU(vector<int> &v, int i, int l, int r) { n   if (l == r) { n     st[i] = v[l]; n     return; n   } n   int m = (l + r) / 2; n   buildU(v, i << 1, l, m); n   buildU(v, i << 1 | 1, m + 1, r); n   st[i] = mod(st[i << 1] + st[i << 1 | 1]); n } n void build(vector<int> &v) { n   for (int i = 1; i < 2 * v.size(); i++) { n     lz[0][i] = 0; n     lz[1][i] = 0; n   } n   buildU(v, 1, 0, v.size() - 1); n } n const int add = 1; n bool lzEmpty(int i) { return lz[0][i] == 0 && lz[1][i] == 0; } n void lazyUpdate(int i, int l, int r) { n   st[i] = mod(st[i] + getFibSum(r - l, lz[0][i], lz[1][i])); n   if (l != r) { n     lz[0][i << 1] = mod(lz[0][i << 1] + lz[0][i]); n     lz[1][i << 1] = mod(lz[1][i << 1] + lz[1][i]); n     int m = (l + r) / 2; n     lz[0][i << 1 | 1] = n         mod(lz[0][i << 1 | 1] + getFib(m + 1 - l, lz[0][i], lz[1][i])); n     lz[1][i << 1 | 1] = n         mod(lz[1][i << 1 | 1] + getFib(m + 2 - l, lz[0][i], lz[1][i])); n   } n   lz[0][i] = 0; n   lz[1][i] = 0; n } n void changeU(int cl, int cr, int i, int l, int r) { n   if (!lzEmpty(i)) lazyUpdate(i, l, r); n   if (r < cl || cr < l) return; n   if (cl <= l && r <= cr) { n     st[i] = mod(st[i] + getFibSumRange(l - cl, r - cl, 1, 1)); n     if (l != r) { n       lz[0][i << 1] = mod(lz[0][i << 1] + getFib(l - cl, 1, 1)); n       lz[1][i << 1] = mod(lz[1][i << 1] + getFib(l - cl + 1, 1, 1)); n       int m = (l + r) / 2; n       lz[0][i << 1 | 1] = mod(lz[0][i << 1 | 1] + getFib(m + 1 - cl, 1, 1)); n       lz[1][i << 1 | 1] = mod(lz[1][i << 1 | 1] + getFib(m + 2 - cl, 1, 1)); n     } n     return; n   } n   int m = (l + r) / 2; n   changeU(cl, cr, i << 1, l, m); n   changeU(cl, cr, i << 1 | 1, m + 1, r); n   st[i] = mod(st[i << 1] + st[i << 1 | 1]); n } n void change(int n, int cl, int cr) { changeU(cl, cr, 1, 0, n - 1); } n int queryU(int ql, int qr, int i, int l, int r) { n   if (!lzEmpty(i)) lazyUpdate(i, l, r); n   if (r < ql || qr < l) return 0; n   if (ql <= l && r <= qr) return st[i]; n   int m = (l + r) / 2; n   int q1 = queryU(ql, qr, i << 1, l, m); n   int q2 = queryU(ql, qr, i << 1 | 1, m + 1, r); n   return mod(q1 + q2); n } n int query(int n, int ql, int qr) { return queryU(ql, qr, 1, 0, n - 1); } n void print(int n) { n   cout <<  st  << endl; n   for (int i = 1; i < 2 * n; i++) { n     cout << setw(4) << st[i] <<    ; n   } n   cout << endl; n   for (int j = 0; j < 2; j++) { n     cout <<  lz[  << j <<  ]  << endl; n     for (int i = 1; i < 2 * n; i++) { n       cout << setw(4) << lz[j][i] <<    ; n     } n     cout << endl; n   } n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   fib[0][0] = 1; n   fib[0][1] = 0; n   fib[1][0] = 0; n   fib[1][1] = 1; n   fibSum[0][0] = fib[0][0]; n   fibSum[1][0] = fib[1][0]; n   for (int j = 0; j < 2; j++) { n     for (int i = 2; i < n; i++) { n       fib[j][i] = (fib[j][i - 2] + fib[j][i - 1]) % 1000000009; n     } n   } n   for (int j = 0; j < 2; j++) { n     for (int i = 0; i < n; i++) { n       fibSum[j][i] = (fibSum[j][i - 1] + fib[j][i]) % 1000000009; n     } n   } n   vector<int> v; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     v.push_back(x); n   } n   build(v); n   for (int i = 0; i < m; i++) { n     int op, l, r; n     cin >> op >> l >> r; n     l--; n     r--; n     if (op == 1) { n       change(n, l, r); n     } else { n       cout << query(n, l, r) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100005; n using namespace std; n inline int Getint() { n   register int x = 0, f = 1; n   register char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const double pi = acos(-1); n struct Z { n   double r, i; n   Z(double _r = 0, double _i = 0) { r = _r, i = _i; } n   Z operator+(const Z &a) const { return Z(r + a.r, i + a.i); } n   Z operator-(const Z &a) const { return Z(r - a.r, i - a.i); } n   Z operator*(const Z &a) const { n     return Z(r * a.r - i * a.i, r * a.i + i * a.r); n   } n   Z operator/(const double &a) const { return Z(r / a, i / a); } n   Z operator/=(const double &a) { return *this = Z(r / a, i / a); } n }; n void FFT(Z *a, int x, int K) { n   static int rev[N], lst; n   int n = 1 << x; n   if (n != lst) { n     for (int i = 0; i < n; i++) n       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << x - 1); n     lst = n; n   } n   for (int i = 0; i < n; i++) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (int i = 1; i < n; i <<= 1) { n     int tmp = i << 1; n     Z wn(cos(pi / i), sin(pi * K / i)); n     for (int j = 0; j < n; j += tmp) { n       Z w(1, 0); n       for (int k = 0; k < i; k++, w = w * wn) { n         Z x = a[j + k], y = w * a[i + j + k]; n         a[j + k] = x + y, a[i + j + k] = x - y; n       } n     } n   } n   if (K == -1) n     for (int i = 0; i < n; i++) a[i] /= n; n } n Z a[N], b[N]; n int n, m, T, X; n double p[105][40005]; n struct node { n   int x, y, z; n } s[105]; n double mp[55][55], f[55][N], g[105][N]; n void Floyd() { n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]); n } n void Cal(int l, int mid, int r) { n   int len = r - l; n   for (int j = 1; j <= m; j++) { n     int x = ceil(log2(len + r - mid)); n     fill(a, a + (1 << x), 0), fill(b, b + (1 << x), 0); n     for (int i = 0; i < r - mid + 1; i++) a[i].r = f[s[j].y][r - i]; n     for (int i = 0; i < len; i++) b[i].r = p[j][i + 1]; n     FFT(a, x, 1), FFT(b, x, 1); n     for (int i = 0; i < (1 << x); i++) a[i] = a[i] * b[i]; n     FFT(a, x, -1); n     for (int i = mid - 1; i >= l; i--) g[j][i] += a[r - i - 1].r; n   } n } n void Binary(int l, int r) { n   if (l == r) { n     for (int i = 1; i <= m; i++) n       f[s[i].x][l] = min(f[s[i].x][l], s[i].z + g[i][l]); n     return; n   } n   int mid = l + r >> 1; n   Binary(mid + 1, r); n   Cal(l, mid + 1, r); n   Binary(l, mid); n } n int main() { n   n = Getint(), m = Getint(), T = Getint(), X = Getint(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (i ^ j) mp[i][j] = 1 << 30; n   for (int i = 1; i <= m; i++) { n     int x = Getint(), y = Getint(), z = Getint(); n     s[i] = (node){x, y, z}; n     mp[x][y] = min(mp[x][y], 1.0 * z); n     for (int j = 1; j <= T; j++) p[i][j] = (double)Getint() / 100000; n   } n   Floyd(); n   for (int j = T + 1; j <= 2 * T; j++) f[n][j] = X; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j <= T; j++) f[i][j] = 1 << 30; n     for (int j = T + 1; j <= 2 * T; j++) f[i][j] = X + mp[i][n]; n   } n   Cal(1, T + 1, 2 * T); n   Binary(0, T); n   printf( %.6f , f[1][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 10; n long long c[2][2][maxn][maxn]; n int lowbit(int s) { return s & (-s); } n void modify(int x, int y, int v) { n   for (int i = x; i < maxn; i += lowbit(i)) n     for (int j = y; j < maxn; j += lowbit(j)) c[x & 1][y & 1][i][j] ^= v; n   return; n } n long long query(int x, int y) { n   long long ret = 0; n   for (int i = x; i > 0; i -= lowbit(i)) n     for (int j = y; j > 0; j -= lowbit(j)) ret ^= c[x & 1][y & 1][i][j]; n   return ret; n } n int main(void) { n   int n, m; n   scanf( %d %d , &n, &m); n   while (m--) { n     long long v; n     int op, x0, y0, x1, y1; n     scanf( %d %d %d %d %d , &op, &x0, &y0, &x1, &y1); n     if (op == 2) { n       scanf( %I64d , &v); n       modify(x0, y0, v); n       modify(x0, y1 + 1, v); n       modify(x1 + 1, y0, v); n       modify(x1 + 1, y1 + 1, v); n     } else n       printf( %I64d n , query(x1, y1) ^ query(x0 - 1, y1) ^ query(x1, y0 - 1) ^ n                             query(x0 - 1, y0 - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   long long n, k, m; n   cin >> n >> k >> m; n   vector<long long> a(k); n   long long second = 0; n   for (long long i = 0; i < k; i++) { n     cin >> a[i]; n     second += a[i]; n   } n   sort(a.begin(), a.end()); n   long long ans = 0; n   for (long long p = 0; p <= n; p++) { n     long long t = m - second * p; n     long long kol = 0; n     if (t < 0) break; n     kol = p * (k + 1); n     for (long long j = 0; j < k; j++) { n       long long ost = n - p; n       while (ost > 0 && t - a[j] >= 0) { n         ost--; n         t -= a[j]; n         kol++; n       } n     } n     ans = max(ans, kol); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const double EXP = 2.7182818284590452; n const double Pi = 3.14159265; n const double EPS = 1e-11; n const int INF = 1000 * 1000 * 1000; n const long long INFL = INF * 1000 * 100; n char arr[2003][2003], c; n bool was[2003][2003]; n int n, m; n queue<std::pair<int, int> > stars; n std::pair<int, int> cur; n std::pair<int, int> mask[] = {std::make_pair(-1, -1), std::make_pair(-1, 0), n                               std::make_pair(-1, 1),  std::make_pair(0, -1), n                               std::make_pair(0, 1),   std::make_pair(1, -1), n                               std::make_pair(1, 0),   std::make_pair(1, 1)}; n bool IsValid(int r, int c) { n   if (r < 1 || r > n) return false; n   if (c < 1 || c > m) return false; n   return true; n } n bool Check(int r, int c) { n   if (arr[r][c - 1] == arr[r - 1][c - 1] && n       arr[r - 1][c - 1] == arr[r - 1][c] && arr[r - 1][c] ==  . ) n     return true; n   if (arr[r - 1][c] == arr[r - 1][c + 1] && n       arr[r - 1][c + 1] == arr[r][c + 1] && arr[r][c + 1] ==  . ) n     return true; n   if (arr[r][c + 1] == arr[r + 1][c + 1] && n       arr[r + 1][c + 1] == arr[r + 1][c] && arr[r + 1][c] ==  . ) n     return true; n   if (arr[r + 1][c] == arr[r + 1][c - 1] && n       arr[r + 1][c - 1] == arr[r][c - 1] && arr[r][c - 1] ==  . ) n     return true; n   return false; n } n void AddNew(int r, int c) { n   int rr, cc; n   for (int i = (0); i < (8); ++i) { n     rr = r + mask[i].first; n     cc = c + mask[i].second; n     if (IsValid(rr, cc) && arr[rr][cc] ==  * ) { n       if (!was[rr][cc] && Check(rr, cc)) { n         stars.push(std::make_pair(rr, cc)); n         was[rr][cc] = true; n       } n     } n   } n } n int main(void) { n   cin >> n >> m; n   for (int i = (0); i < (n + 2); ++i) n     for (int j = (0); j < (m + 2); ++j) arr[i][j] =  * ; n   for (int i = (1); i < (n + 1); ++i) { n     scanf( %c , &c); n     for (int j = (1); j < (m + 1); ++j) scanf( %c , arr[i] + j); n   } n   for (int i = (1); i < (n + 1); ++i) n     for (int j = (1); j < (m + 1); ++j) { n       if (Check(i, j)) { n         stars.push(std::make_pair(i, j)); n         was[i][j] = true; n       } n     } n   while (stars.size()) { n     cur = stars.front(); n     stars.pop(); n     arr[cur.first][cur.second] =  . ; n     AddNew(cur.first, cur.second); n   } n   for (int i = (1); i < (n + 1); ++i) { n     for (int j = (1); j < (m + 1); ++j) { n       printf( %c , arr[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m; n void Add(int &a, int b) { n   a += b; n   if (a >= m) a -= m; n } n int dp[1010][110][2]; n int main() { n   int n, k; n   while (scanf( %d%d%d , &n, &k, &m) == 3) { n     memset(dp, 0, sizeof(dp)); n     dp[0][0][0] = 1; n     long long tmp = 1; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < k; j++) n         for (int x = 0; x < 2; x++) { n           if (dp[i][j][x] == 0) continue; n           for (int y = 0; y < 10; y++) { n             if (i == n - 1 && y == 0) continue; n             int nj = (y * tmp % k + j) % k; n             int nx = x; n             if (nj == 0 && (y > 0 || j > 0)) nx = 1; n             Add(dp[i + 1][nj][nx], dp[i][j][x]); n           } n         } n       tmp = tmp * 10 % k; n     } n     int ans = 0; n     for (int j = 0; j < k; j++) Add(ans, dp[n][j][1]); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long double n; n   int m; n   cin >> n >> m; n   long sum = n; n   long double newsum = 0.0; n   for (int i = 0; i < n; i++) { n     newsum += pow((long double)i / (long double)n, m); n   } n   cout << sum - newsum; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 18 | 7; n int a[N], b[N], o[N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, s; n   cin >> n >> s; n   --s; n   for (int i = 0; i < n; ++i) cin >> a[i], b[i] = a[i]; n   for (int i = 0; i < n; ++i) n     if (!a[i]) a[i] = n; n   if (a[s]) a[s] = 0; n   priority_queue<pair<int, int> > Q; n   for (int i = 0; i < n; ++i) Q.push({a[i], i}), o[a[i]] = 1; n   for (int i = 1; i < Q.top().first; ++i) { n     if (o[i]) continue; n     auto now = Q.top(); n     Q.pop(); n     a[now.second] = i; n     now.first = i; n     Q.push(now); n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) ans += a[i] != b[i]; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < int(n); i++) { n     cin >> a[i]; n   } n   vector<vector<int>> g(n); n   for (int i = 0; i < int(n - 1); i++) { n     int p; n     cin >> p; n     p--; n     g[p].push_back(i + 1); n   } n   vector<int> color(n); n   function<void(int)> dfs = [&](int u) { n     for (const auto& v : g[u]) { n       color[v] = color[u] ^ 1; n       dfs(v); n     } n   }; n   dfs(0); n   int WHITE = 0; n   for (int i = 0; i < int(n); i++) { n     if (g[i].empty()) { n       WHITE = color[i]; n     } n   } n   int nim_sum = 0; n   map<int, int> freq; n   for (int i = 0; i < int(n); i++) { n     if (color[i] == WHITE) { n       nim_sum ^= a[i]; n       ++freq[a[i]]; n     } n   } n   long long ans = 0; n   if (nim_sum == 0) { n     int cnt = 0; n     for (int i = 0; i < int(n); i++) { n       if (color[i] != WHITE) { n         cnt++; n       } n     } n     ans = (long long)cnt * (cnt - 1) / 2 + n           (long long)(n - cnt) * (n - cnt - 1) / 2; n   } n   for (int i = 0; i < int(n); i++) { n     if (color[i] != WHITE) { n       ans += freq[nim_sum ^ a[i]]; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 4e5 + 5; n const int BN = 25; n int n, a[N], cnt; n vector<int> v[BN]; n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   int res = 0; n   for (int i = 1; i <= n; ++i) { n     if (a[i] & 1) { n       int k = (i - 1) - cnt; n       if (k & 1) { n         res ^= 1; n       } n     } else { n       int k = cnt; n       if (k & 1) { n         res ^= 1; n       } n     } n     int cur_val = (a[i] & 1); n     for (int bit = 1; bit <= 24; ++bit) { n       if ((a[i] >> bit) & 1) { n         cur_val += (1 << bit); n       } n       v[bit].push_back(cur_val); n     } n     cnt += (a[i] & 1); n   } n   for (int bit = 1; bit <= 24; ++bit) sort(v[bit].begin(), v[bit].end()); n   for (int bit = 1; bit <= 24; ++bit) { n     while (!v[bit].empty()) { n       int cur_val = v[bit].back(); n       v[bit].pop_back(); n       int k = lower_bound(v[bit].begin(), v[bit].end(), (1 << bit) - cur_val) - n               v[bit].begin(); n       k = v[bit].size() - k; n       int j = lower_bound(v[bit].begin(), v[bit].end(), n                           (1 << (bit + 1)) - cur_val) - n               v[bit].begin(); n       j = v[bit].size() - j; n       int l = lower_bound(v[bit].begin(), v[bit].end(), n                           (1 << (bit + 1)) + (1 << bit) - cur_val) - n               v[bit].begin(); n       l = v[bit].size() - l; n       k += l; n       k -= j; n       if (k & 1) { n         res ^= (1 << bit); n       } n     } n   } n   cout << res; 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   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long ans = (n * (n + 1)) / 2; n     long long x = (n - m) / (m + 1); n     long long y = (n - m) % (m + 1); n     long long l = (x * (x + 1)) / 2; n     x++; n     long long r = (x * (x + 1)) / 2; n     long long a = (m + 1 - y) * l + y * r; n     ans -= a; n     cout << ans <<   n ; n   } n   return 0; n } n