#include <bits/stdc++.h> n using namespace std; n int main() { n   long long k, l, ans = 0; n   cin >> k >> l; n   while (l % k == 0) { n     l = l / k; n     if (l != 1) ans++; n   } n   if (l == 1) n     cout <<  YES  << endl << ans; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, w, used[300005]; n long long ans, a[300005], b[300005]; n priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                greater<pair<long long, int> > > n     Q1, Q2; n priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                less<pair<long long, int> > > n     Used; n int main() { n   scanf( %d %d , &n, &w); n   for (int i = 1; i <= n; i++) { n     scanf( %lld %lld , &a[i], &b[i]); n     Q1.push(make_pair(a[i], i)); n     Q2.push(make_pair(b[i], i)); n   } n   if (w == 1) { n     pair<long long, int> x = Q1.top(); n     ans = x.first; n     used[x.second]++; n     printf( %lld n , ans); n     for (int i = 1; i <= n; i++) printf( %d , used[i]); n     return 0; n   } n   while (w > 0 && !Q1.empty() && !Q2.empty()) { n     pair<long long, int> x = Q1.top(); n     if (used[abs(x.second)] == 2) { n       Q1.pop(); n       continue; n     } n     pair<long long, int> y = Q2.top(); n     if (used[y.second]) { n       Q2.pop(); n       continue; n     } n     if (x.first * 2 <= y.first) { n       int now = x.second; n       w--; n       ans += x.first; n       Q1.pop(); n       if (now > 0) Q1.push(make_pair(b[now] - a[now], -now)); n       used[abs(now)]++; n       Used.push(make_pair(x.first, abs(now))); n     } else { n       int now = y.second; n       w--, w--; n       ans += y.first; n       Q2.pop(); n       used[now] = 2; n       Used.push(make_pair(b[now] - a[now], now)); n     } n   } n   while (w > 0 && !Q1.empty()) { n     pair<long long, int> x = Q1.top(); n     if (used[abs(x.second)] == 2) { n       Q1.pop(); n       continue; n     } n     int now = x.second; n     w--; n     ans += x.first; n     Q1.pop(); n     if (now > 0) Q1.push(make_pair(b[now] - a[now], -now)); n     used[abs(now)]++; n     Used.push(make_pair(x.first, abs(now))); n   } n   while (w > 0 && !Q2.empty()) { n     pair<long long, int> y = Q2.top(); n     if (used[y.second]) { n       Q2.pop(); n       continue; n     } n     int now = y.second; n     w--, w--; n     ans += y.first; n     Q2.pop(); n     used[now] = 2; n     Used.push(make_pair(b[now] - a[now], now)); n   } n   if (w == -1) { n     pair<long long, int> x = Used.top(); n     ans -= x.first; n     used[x.second]--; n   } n   int f = 1; n   while (f) { n     f = 0; n     while (!Q1.empty()) Q1.pop(); n     while (!Used.empty()) Used.pop(); n     for (int i = 1; i <= n; i++) { n       if (used[i] == 2) n         Used.push(make_pair(b[i], i)); n       else if (used[i] == 1) n         Q1.push(make_pair(b[i] - a[i], i)); n       else n         Q1.push(make_pair(a[i], i)); n     } n     if (Q1.size() < 2 || Used.empty()) break; n     pair<long long, int> x = Q1.top(); n     Q1.pop(); n     pair<long long, int> y = Q1.top(); n     Q1.pop(); n     pair<long long, int> z = Used.top(); n     Used.pop(); n     if (x.first + y.first < z.first) { n       ans = ans - (z.first - x.first - y.first); n       used[x.second]++; n       used[y.second]++; n       used[z.second] = 0; n       f = 1; n     } n   } n   printf( %lld n , ans); n   for (int i = 1; i <= n; i++) printf( %d , used[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const int MOD = 998244353; n struct node { n   long long a, b; n   node() {} n   node(long long a, long long b) { n     this->a = a; n     this->b = b; n   } n   node operator+(const node& rhs) const { n     return node((a + rhs.a) % MOD, (b + rhs.b) % MOD); n   } n   node operator*(const node& rhs) const { n     return node(a * rhs.a % MOD, b * rhs.b % MOD); n   } n   bool operator==(const node& rhs) const { return a == rhs.a && b == rhs.b; } n }; n node BASE(2333, 233333); n node h[maxn]; n node ALL(0, 0); n node s[maxn][15]; n vector<pair<int, int> > vec[maxn]; n int n, m, k, ans; n void dfs(int u, node hh) { n   if (u > k) { n     ans += (hh == ALL ? 1 : 0); n     return; n   } n   for (int i = 0; i < u; i++) { n     node v = hh + s[u][i]; n     dfs(u + 1, v); n   } n   return; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   h[0] = node(1, 1); n   for (int i = 1; i <= n; i++) { n     h[i] = h[i - 1] * BASE; n     ALL = ALL + h[i]; n   } n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     vec[u].emplace_back(make_pair(v, w)); n   } n   for (int i = 1; i <= n; i++) { n     sort(vec[i].begin(), vec[i].end(), [](pair<int, int> a, pair<int, int> b) { n       return a.second < b.second; n     }); n     int lens = vec[i].size(); n     for (int j = 0; j < lens; j++) s[lens][j] = s[lens][j] + h[vec[i][j].first]; n   } n   dfs(1, node(0, 0)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m = 1; n vector<vector<int> > edge(105); n int from[105]; n int to[105]; n bool bfs(int a, int b) { n   int use[105] = {0}; n   queue<int> q; n   q.push(a); n   use[a] = 1; n   while (!q.empty()) { n     int cur = q.front(); n     q.pop(); n     for (int i = 0; i < edge[cur].size(); i++) { n       if (edge[cur][i] == b) return true; n       if (use[edge[cur][i]] == 0) { n         q.push(edge[cur][i]); n         use[edge[cur][i]] = 1; n       } n     } n   } n   return false; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     int t, x, y; n     cin >> t >> x >> y; n     if (t == 1) { n       from[m] = x; n       to[m] = y; n       for (int j = 1; j < m; j++) { n         if ((from[j] < x && x < to[j] && y >= to[j]) || n             (from[j] < y && y < to[j]) && x <= from[j]) { n           edge[j].push_back(m); n           edge[m].push_back(j); n         } else if (x <= from[j] && y >= to[j]) { n           edge[j].push_back(m); n         } n       } n       m++; n     } else { n       if (bfs(x, y)) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 1e6 + 5; n int n, p = 1, pa[N]; n int ch[N][26]; n vector<int> cnt[N][2]; n vector<pair<int, int> > as; n long long ans; n inline void dfs(int u, int dep) { n   for (register int i = 0; i <= 25; ++i) { n     int v = ch[u][i]; n     if (v) { n       dfs(v, dep + 1); n       for (register int j = 0; j < cnt[v][0].size(); ++j) n         cnt[u][0].push_back(cnt[v][0][j]); n       for (register int j = 0; j < cnt[v][1].size(); ++j) n         cnt[u][1].push_back(cnt[v][1][j]); n     } n   } n   ans += min(cnt[u][0].size(), cnt[u][1].size()) * dep; n   for (register int i = min(cnt[u][0].size(), cnt[u][1].size()) - 1; i > -1; n        --i) { n     as.push_back( n         {cnt[u][0][cnt[u][0].size() - 1], cnt[u][1][cnt[u][1].size() - 1]}); n     cnt[u][0].pop_back(); n     cnt[u][1].pop_back(); n   } n } n string a; n int main() { n   cin >> n; n   for (register int id = 1; id <= n; ++id) { n     cin >> a; n     int u = 1; n     for (register int i = 0; i < a.size(); ++i) { n       int c = a[i] -  a ; n       if (ch[u][c] == 0) ch[u][c] = ++p; n       u = ch[u][c]; n     } n     cnt[u][0].push_back(id); n   } n   for (register int id = 1; id <= n; ++id) { n     cin >> a; n     int u = 1; n     for (int i = 0; i < a.size(); ++i) { n       int c = a[i] -  a ; n       if (!ch[u][c]) ch[u][c] = ++p; n       u = ch[u][c]; n     } n     cnt[u][1].push_back(id); n   } n   dfs(1, 0); n   printf( %d n , ans); n   for (register int i = 0; i <= n - 1; ++i) n     printf( %d %d n , as[i].first, as[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[105][3]; n int main() { n   scanf( %s%d , a[0], &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , a[i]); n     if (a[i][0] == a[0][0] && a[i][1] == a[0][1]) { n       printf( YES n ); n       return 0; n     } n   } n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       if ((a[i][1] == a[0][0] && a[j][0] == a[0][1])) { n         printf( YES n ); n         return 0; n       } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int T, n, a[N]; n map<int, int> mp; n int main() { n   cin >> T; n   while (T--) { n     double sum = 0; n     mp.clear(); n     cin >> n; n     for (int i = 0; i < n; ++i) { n       cin >> a[i]; n       sum += a[i]; n     } n     double mean = sum / n; n     double need = sum - (n - 2) * mean; n     if (need != floor(need)) { n       cout << 0 << endl; n       continue; n     } n     long long ans = 0; n     for (int i = 0; i < n; ++i) { n       if (mp.find(need - a[i]) != mp.end()) ans += mp[need - a[i]]; n       mp[a[i]]++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(int num); n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= 1000; i++) { n     if (!(isPrime(n * i + 1))) { n       cout << i; n       break; n     } n   } n   return 0; n } n bool isPrime(int num) { n   if (num <= 1) n     return false; n   else if (num == 2) n     return true; n   else if (num % 2 == 0) n     return false; n   else { n     bool prime = true; n     int divisor = 3; n     double num_d = static_cast<double>(num); n     int upperLimit = static_cast<int>(sqrt(num_d) + 1); n     while (divisor <= upperLimit) { n       if (num % divisor == 0) prime = false; n       divisor += 2; n     } n     return prime; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const double esp = 1e-7; n const int mod = 1e9 + 7; n vector<int> adj[N]; n int leaf = 0; n void dfs(int node = 1, int par = 0) { n   int cnt = 0; n   for (int child : adj[node]) { n     if (child == par) continue; n     dfs(child, node); n     cnt++; n   } n   if (!cnt) { n     leaf++; n   } n } n int main() { n   int n, s, u, v; n   double ans; n   scanf( %d %d , &n, &s); n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(); n   if (adj[1].size() == 1) leaf++; n   if (n == 2) { n     leaf = 1; n     ans = s; n   } else { n     ans = 2.0 * s / double(leaf); n   } n   printf( %.09f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cout << *it <<   =   << a << endl; n   err(++it, args...); n } n const long long MOD = 1e9 + 7; n long long modExp(long long b, long long p) { n   long long res = 1; n   while (p > 0) { n     if (p & 1) { n       res = (res * b) % MOD; n     } n     b = (b * b) % MOD; n     p >>= 1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   long long ans = 1; n   for (int i = 1; i <= n; i++) { n     ans = ((2 * m - 1) * ans + modExp(m, i - 1)) % MOD; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int k = 1; k <= n / 3; k++) { n     if (n % k == 0) { n       for (int p = 0; p < k; p++) { n         int sum = 0; n         for (int v = p; v < n; v += k) { n           if (a[v] == 1) sum++; n         } n         if (sum == n / k) { n           cout <<  YES n ; n           return 0; n         } n       } n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double pi = acos(-1.0); n template <class T> n long long chkmin(T &a, T b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n long long chkmax(T &a, T b) { n   return a < b ? a = b, 1 : 0; n } n template <class T> n T sqr(T a) { n   return a * a; n } n template <class T> n T mmin(T a, T b) { n   return a < b ? a : b; n } n template <class T> n T mmax(T a, T b) { n   return a > b ? a : b; n } n template <class T> n T aabs(T a) { n   return a < 0 ? -a : a; n } n template <class T> n long long dcmp(T a, T b) { n   return a > b; n } n template <long long *a> n long long cmp_a(long long first, long long second) { n   return a[first] < a[second]; n } n namespace io { n const long long SIZE = (1 << 21) + 1; n char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, n                                        qu[55]; n long long f, qr; n inline void flush() { n   fwrite(obuf, 1, oS - obuf, stdout); n   oS = obuf; n } n inline void putc(char first) { n   *oS++ = first; n   if (oS == oT) flush(); n } n inline bool read(signed &first) { n   for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                             : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) n       f = -1; n     else if (c == EOF) n       return 0; n   for (first = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     first = first * 10 + (c & 15); n   first *= f; n   return 1; n } n inline bool read(long long &first) { n   for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                             : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) n       f = -1; n     else if (c == EOF) n       return 0; n   for (first = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     first = first * 10 + (c & 15); n   first *= f; n   return 1; n } n inline bool read(char &first) { n   first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                     : *iS++); n   return first != EOF; n } n inline bool read(char *first) { n   while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                                 (iS == iT ? EOF : *iS++)) n                              : *iS++)) ==   n  || n          *first ==     || *first ==   r ) n     if (*first == EOF) return 0; n   while (!(*first ==   n  || *first ==     || *first ==   r  || *first == EOF)) n     *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                            : *iS++); n   *first = 0; n   return 1; n } n template <typename A, typename... B> n inline bool read(A &first, B &...second) { n   return read(first) && read(second...); n } n inline bool write(signed first) { n   if (!first) putc( 0 ); n   if (first < 0) putc( - ), first = -first; n   while (first) qu[++qr] = first % 10 +  0 , first /= 10; n   while (qr) putc(qu[qr--]); n   return 0; n } n inline bool write(long long first) { n   if (!first) putc( 0 ); n   if (first < 0) putc( - ), first = -first; n   while (first) qu[++qr] = first % 10 +  0 , first /= 10; n   while (qr) putc(qu[qr--]); n   return 0; n } n inline bool write(char first) { n   putc(first); n   return 0; n } n inline bool write(const char *first) { n   while (*first) { n     putc(*first); n     ++first; n   } n   return 0; n } n inline bool write(char *first) { n   while (*first) { n     putc(*first); n     ++first; n   } n   return 0; n } n template <typename A, typename... B> n inline bool write(A first, B... second) { n   return write(first) || write(second...); n } n struct Flusher_ { n   ~Flusher_() { flush(); } n } io_flusher_; n }  // namespace io n using io ::putc; n using io ::read; n using io ::write; n long long inf; n struct _init_ { n   _init_() { memset(&inf, 0x3f, sizeof(inf)); } n }; n _init_ ___INIT__; n long long a[20005], b[20005]; n long long f[40005], beg[20005]; n vector<long long> poly[40005]; n long long t; n const long long p = 998244353, g = 3; n long long rev[1 << 18]; n long long fpm(long long a, long long b) { n   long long c = 1; n   for (; b; b >>= 1, a = a * a % p) n     if (b & 1) c = c * a % p; n   return c; n } n void NTT(vector<long long> &a, long long n, long long flg) { n   for (long long i = 0; i < n; ++i) { n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (n >> 1)); n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   } n   for (long long i = 1; i < n; i <<= 1) { n     long long ww = fpm(g, (p - 1) / i / 2 * (flg + p - 1)), w; n     for (long long j = 0; j < n; j += i + i) { n       w = 1; n       for (long long k = 0; k < i; ++k) { n         long long u = a[j + k], v = a[j + k + i] * w % p; n         a[j + k] = (u + v) % p; n         a[j + k + i] = (u - v + p) % p; n         w = w * ww % p; n       } n     } n   } n   if (flg == -1) { n     long long in = fpm(n, p - 2); n     for (long long i = 0; i < n; ++i) a[i] = a[i] * in % p; n   } n } n void Prod(vector<long long> &a, vector<long long> &b, vector<long long> &c) { n   long long first = a.size() + b.size(), n = 1; n   while (n < first) n <<= 1; n   a.resize(n); n   b.resize(n); n   c.resize(n); n   NTT(a, n, 1); n   NTT(b, n, 1); n   for (long long i = 0; i < n; ++i) c[i] = a[i] * b[i] % p; n   NTT(c, n, -1); n   c.resize(first - 1); n   a.clear(); n   b.clear(); n } n long long query(long long k) { n   long long l = 1; n   while (l < t) { n     Prod(poly[l], poly[l + 1], poly[++t]); n     l += 2; n   } n   return poly[t][k]; n } n signed main() { n   long long n, k; n   read(n, k); n   for (long long i = 1; i <= n; ++i) read(a[i]); n   long long q; n   long long tp, first, l, r, d; n   read(q); n   for (; q; --q) { n     read(tp, first, l); n     if (tp == 1) { n       read(d); n       for (long long i = 1; i <= n; ++i) { n         poly[i].clear(); n         poly[i].push_back(1); n         poly[i].push_back((first - (i == l ? d : a[i]) + 2 * p) % p); n       } n     } else { n       read(r, d); n       for (long long i = 1; i <= n; ++i) { n         poly[i].clear(); n         poly[i].push_back(1); n         poly[i].push_back((first - a[i] + 2 * p) % p); n       } n       for (long long i = l; i <= r; ++i) n         poly[i][1] = (poly[i][1] + 2 * p - d) % p; n     } n     t = n; n     write(query(k),   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Popcount(long long int a) { n   int k; n   for (k = 0; a; a >>= 1) n     if (a & 1ll) k++; n   return k; n } n int Ffs(long long int a) { n   int k; n   for (k = 0; a; a >>= 1, k++) n     ; n   return k; n } n long long int C[50][50]; n long long int getAns(long long int n, long long int t) { n   if (n < t || t <= 0) return 0ll; n   long long int i; n   int k = 0, j = Ffs(t) - 1; n   for (i = 1ll; (i << 1) < n; (i <<= 1), k++) n     ; n   long long int res = getAns(n - i, t >> 1); n   if (k < j) return res; n   return C[k][j] + res; n } n int main() { n   C[0][0] = 1ll; n   for (int i = 1; i < 50; i++) { n     C[i][0] = C[i][i] = 1ll; n     for (int j = 1; j < i; j++) C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; n   } n   long long int N, T; n   while (cin >> N >> T) { n     N++; n     if (Popcount(T) != 1) { n       puts( 0 ); n       continue; n     } n     long long int Ans = 0ll, x, di = 0ll; n     for (x = 1ll; x + di <= N; x <<= 1) { n       Ans += getAns(x, T); n       di += x; n     } n     Ans += getAns(N - di, T); n     if (T == 1ll) Ans--; n     cout << Ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct BIT { n   vector<int> t; n   int n; n   void add(int x, int v) { n     for (; x < n; x += x & -x) { n       t[x] += v; n     } n   } n   BIT(int sz) { n     t = vector<int>(sz); n     n = sz; n   } n   void add(int l, int r, int v) { n     ++l; n     ++r; n     add(l, v); n     add(r, -v); n   } n   int query(int x) { n     ++x; n     int ans = 0; n     for (; x; x -= x & -x) { n       ans += t[x]; n     } n     return ans; n   } n }; n const int N = 100100; n vector<BIT> bits; n vector<int> adj[N]; n int my_bit[N], my_ind[N], n, q; n int dfs(int u, int p, int bit_ind, int ind) { n   my_bit[u] = bit_ind; n   my_ind[u] = ind; n   int res = ind; n   for (auto& v : adj[u]) { n     if (v == p) continue; n     res = dfs(v, u, bit_ind, ind + 1); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n >> q; n   int u, v; n   for (int i = 1; i <= n - 1; ++i) { n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   bits.emplace_back(1); n   int mx = 5; n   for (int v : adj[1]) { n     int amt = dfs(v, 1, bits.size(), 2); n     bits.emplace_back(amt + 5); n     mx = max(mx, amt + 5); n   } n   BIT all(mx); n   my_ind[1] = 1; n   for (int qq = 0; qq < q; ++qq) { n     int type; n     cin >> type; n     if (type == 0) { n       int x, d; n       cin >> u >> x >> d; n       if (u == 1) { n         all.add(1, 2 + d, x); n       } else { n         int extra = 0; n         int lo = my_ind[u] - d; n         int hi = my_ind[u] + d + 1; n         if (lo < 2) { n           extra = 2 - lo; n           lo = extra + 1; n         } n         bits[my_bit[u]].add(lo, hi, x); n         if (extra) { n           all.add(1, extra + 1, x); n         } n       } n     } else if (type == 1) { n       cin >> u; n       int res = all.query(my_ind[u]); n       if (u != 1) { n         assert(my_ind[u] > 1); n         res += bits[my_bit[u]].query(my_ind[u]); n       } n       cout << res <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lli = long long int; n using pii = pair<int, int>; n using vi = vector<int>; n using vb = vector<bool>; n using vvi = vector<vector<int>>; n using vlli = vector<long long int>; n using vpii = vector<pair<int, int>>; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   lli n, k, a, x[100005], c[100005], money = 0; n   multiset<lli> unused; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   cin >> a; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1; i <= n; i++) { n     unused.insert(c[i]); n     while (!unused.empty() && k < x[i]) { n       auto temp = unused.begin(); n       k += a; n       money += *temp; n       unused.erase(temp); n     } n     if (k < x[i]) return cout << -1, 0; n   } n   cout << money; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n const int N = 1e6 + 5, M = 300, MAX = 10, T = 3e4, INF = 1e9; n int d[N], w[T], dist[300][M + 5]; n vector<int> h[T], pr; n vector<vector<int>> v; n map<vector<int>, int> mp; n int cnt = 0; n vector<int> path; n void f(int max, int prod, int ct) { n   v.push_back(path); n   pr.push_back(prod); n   mp[path] = cnt++; n   if (ct == MAX) return; n   for (int i = 2; i <= max; i++) { n     if (prod * i <= M) { n       path.push_back(i); n       f(i, prod * i, ct + 1); n       path.pop_back(); n     } n   } n } n vector<int> getV(int a) { n   vector<int> g; n   while (a > 1) { n     int x = 0, y = d[a]; n     while (d[a] == y) { n       a /= d[a]; n       x++; n     } n     g.push_back(x + 1); n   } n   sort(g.rbegin(), g.rend()); n   return g; n } n int main() { n   f(M, 1, 0); n   map<vector<int>, int> g; n   for (int i = 2; i < N; i++) { n     if (!d[i]) { n       d[i] = i; n       for (long long int j = i * (long long int)i; j < N; j += i) { n         if (!d[j]) d[j] = i; n       } n     } n   } n   for (int i = 1; i < N; i++) { n     vector<int> t = getV(i); n     if (!g.count(t)) { n       int x = g.size(); n       g[t] = x; n     } n   } n   for (int i = 0; i < v.size(); i++) { n     vector<int> &c = v[i]; n     for (int j = 0; j < c.size(); j++) { n       if (j == 0 || c[j - 1] != c[j]) { n         c[j]++; n         if (mp.count(c)) { n           h[i].push_back(mp[c]); n           h[mp[c]].push_back(i); n         } n         c[j]--; n       } n     } n     if (c.size() < MAX) { n       c.push_back(2); n       if (mp.count(c)) { n         h[i].push_back(mp[c]); n         h[mp[c]].push_back(i); n       } n       c.pop_back(); n     } n   } n   for (int i = 0; i < 300; i++) { n     for (int j = 0; j <= M; j++) { n       dist[i][j] = INF; n     } n   } n   for (auto &p : g) { n     fill(w, w + T, -1); n     queue<int> q; n     w[mp[p.first]] = 0; n     q.push(mp[p.first]); n     while (!q.empty()) { n       int node = q.front(); n       q.pop(); n       for (int to : h[node]) { n         if (w[to] == -1) { n           w[to] = w[node] + 1; n           q.push(to); n         } n       } n     } n     for (int j = 0; j < v.size(); j++) { n       dist[p.second][pr[j]] = min(dist[p.second][pr[j]], w[j]); n     } n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int a, b; n     scanf( %d%d , &a, &b); n     a = g[getV(a)]; n     b = g[getV(b)]; n     int ans = INF; n     for (int i = 0; i <= M; i++) ans = min(ans, dist[a][i] + dist[b][i]); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 1; n inline int min(const int& x, const int& y) { return (x - y >> 31) ? x : y; } n namespace ST { n int lg[N], t[20][N]; n inline void build(int* a, int n) { n   copy(a + 1, a + 1 + n, t[0] + 1); n   for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1; n   for (int i = 1; i <= 19; i++) n     for (int j = 1; j <= n - (1 << i); j++) n       t[i][j] = min(t[i - 1][j], t[i - 1][j + (1 << i - 1)]); n } n inline int ask(int l, int r) { n   int p = lg[r - l]; n   return min(t[p][l], t[p][r - (1 << p)]); n } n }  // namespace ST n namespace SA { n int n, m, sa[N], x[N], y[N], c[N], h[N]; n char* st; n inline void input(char* a, int len) { n   n = len, m = 128, st = a; n   for (int i = 1; i <= n; i++) x[i] = st[i], sa[i] = i; n } n inline void radix_sort() { n   for (int i = 1; i <= m; i++) c[i] = 0; n   for (int i = 1; i <= n; i++) c[x[i]]++; n   for (int i = 1; i <= m; i++) c[i] += c[i - 1]; n   for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i]; n } n inline void init() { n   for (int k = 0, p = 0; k <= n; k = k << 1 | !k) { n     for (int i = n - k + 1; i <= n; i++) y[++p] = i; n     for (int i = 1; i <= n; i++) n       if (sa[i] > k) y[++p] = sa[i] - k; n     radix_sort(), swap(x, y), p = 0; n     for (int i = 1; i <= n; i++) n       x[sa[i]] = p += n           y[sa[i]] != y[sa[i - 1]] || y[sa[i] + k] != y[sa[i - 1] + k]; n     if (p >= n) break; n     m = p, p = 0; n   } n   for (int i = 1; i <= n; i++) { n     int& p = h[x[i]] = max(0, h[x[i - 1]] - 1); n     while (st[i + p] == st[sa[x[i] + 1] + p]) h[x[i]]++; n   } n   ST::build(h, n); n } n }  // namespace SA n struct tree { n   set<int> mp; n   long long int val; n   inline void init() { mp.clear(), mp.insert(0), mp.insert(N), val = 0; } n   inline int pre(int x) { return *--mp.lower_bound(x); } n   inline int nxt(int x) { return *mp.upper_bound(x); } n   inline void add(int x, int y, int w) { n     if (x && y != N) val += ST::ask(x, y) * w; n   } n   inline void ins(int x) { n     int p = pre(x), q = nxt(x); n     add(p, q, 1), add(p, x, -1), add(x, q, -1), mp.insert(x); n   } n } t[N]; n namespace SAM { n int n, tot, last, top; n int fa[N], len[N], ch[N][26], d[N], sta[N], pos[N]; n inline void cpy(int x, int y) { n   for (int i = 0; i <= 25; i++) ch[x][i] = ch[y][i]; n } n inline void ins(int x) { n   int p(last), np, q, nq; n   len[np = last = ++tot] = len[p] + 1; n   while (p && !ch[p][x]) ch[p][x] = np, p = fa[p]; n   if (!p) return void(fa[np] = 1); n   if (len[q = ch[p][x]] == len[p] + 1) return void(fa[np] = q); n   cpy(nq = ++tot, q), len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq; n   while (p && ch[p][x] == q) ch[p][x] = nq, p = fa[p]; n } n inline void input(char* a, int len) { n   n = len, tot = last = pos[0] = 1; n   for (int i = 1; i <= n; i++) ins(a[i] -  a ), pos[i] = last; n } n inline void merge(tree& x, tree& y) { n   if (x.mp.size() < y.mp.size()) swap(x, y); n   for (int v : y.mp) n     if (v && v != N) x.ins(v), x.val += n - SA::sa[v] + 1; n } n inline void work() { n   long long int ans = 0; n   for (int i = 0; i <= tot; i++) d[fa[i]]++, t[i].init(); n   for (int i = 1; i <= tot; i++) n     if (!d[i]) sta[++top] = i; n   for (int i = 0; i <= n - 2; i++) n     t[pos[i]].ins(SA::x[i + 2]), t[pos[i]].val += n - i - 1; n   for (int i = 1; i <= tot; i++) ans += len[i] - len[fa[i]] << (i != pos[n]); n   while (int u = sta[top--]) { n     ans += t[u].val * (len[u] - len[fa[u]]); n     if (fa[u]) merge(t[fa[u]], t[u]); n     if (!--d[fa[u]]) sta[++top] = fa[u]; n   } n   cout << ans + t[1].val + 2; n } n };  // namespace SAM n int n; n char a[N]; n int main() { n   scanf( %s , a + 1), n = strlen(a + 1), SA::input(a, n); n   SA::init(), SAM::input(a, n), SAM::work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[2 * 1000005]; n int n; n int input() { n   int i; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) p[i] = i; n   return 0; n } n int solve() { n   int i, j, ii; n   int t1, t2; n   for (i = 2; i <= n; i++) { n     t1 = p[i - 1]; n     for (j = i + i - 1; j < n + i - 1; j += i) { n       t2 = p[j]; n       p[j] = t1; n       t1 = t2; n     } n     p[n + i - 1] = t1; n   } n   return 0; n } n int output() { n   int i; n   for (i = n; i < n + n; i++) printf( %d  , p[i]); n   printf(  n ); n   return 0; n } n int main() { n   input(); n   solve(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n int C[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, r; n   cin >> n >> r; n   double sum = 0; n   for (int i = 0; i < (1 << n); i++) { n     cin >> C[i]; n     sum += C[i]; n   } n   cout << setprecision(15) << fixed << sum / (1 << n) <<   n ; n   while (r--) { n     int z, g; n     cin >> z >> g; n     sum += g - C[z]; n     C[z] = g; n     cout << setprecision(15) << fixed << sum / (1 << n) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 7; n long long a[maxn], b[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i] >> b[i]; n     a[i] ^= b[i], b[i] ^= b[i - 1]; n   } n   vector<long long> linbs; n   for (int i = 1; i <= n; i++) { n     long long temp = a[i]; n     for (auto &e : linbs) temp = min(temp, temp ^ e); n     if (temp) { n       for (auto &e : linbs) e = min(e, e ^ temp); n       linbs.push_back(temp); n     } n   } n   for (auto &e : linbs) b[n] = min(b[n], b[n] ^ e); n   if (b[n]) n     cout << 1 <<  /  << 1 << endl; n   else { n     int sz = linbs.size(); n     long long t1 = 1ll << sz, t2 = t1 - 1; n     cout << t2 <<  /  << t1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1000000007; n void __print(long long int x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename Th, typename V> n void __print(const pair<Th, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename Th> n void __print(const Th &x) { n   long long int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename Th, typename... V> n void _print(Th t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n void binary_rock() { n   long long int v, c, n, m; n   cin >> v >> c >> n >> m; n   if (v + c < n + m) { n     cout <<  No  << endl; n     return; n   } n   long long int mini = min(v, c); n   if (mini >= m) { n     cout <<  Yes  << endl; n     return; n   } n   if (v >= c) { n     long long int req = (v - c); n     if (n < req) { n       cout <<  No  << endl; n       return; n     } n     n -= req; n     v -= req; n     if (n >= m) { n       cout <<  Yes  << endl; n       return; n     } n     m -= n; n     v -= n; n     c -= n; n     n -= n; n     if (m > v) { n       cout <<  No  << endl; n       return; n     } else { n       cout <<  Yes  << endl; n       return; n     } n     return; n   } else { n     long long int req = (c - v); n     if (n < req) { n       cout <<  No  << endl; n     } else { n       n -= req; n       c -= req; n       if (n >= m) { n         cout <<  Yes  << endl; n         return; n       } n       m -= n; n       v -= n; n       c -= n; n       n -= n; n       if (m > v) { n         cout <<  No  << endl; n         return; n       } else { n         cout <<  Yes  << endl; n         return; n       } n       cout <<  Yes  << endl; n     } n     return; n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   clock_t t1 = clock(); n   long long int t = 1; n   cin >> t; n   while (t--) { n     binary_rock(); n   } n   clock_t t2 = clock(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 305, Q = (int)2e6 + 50; n struct Qry { n   int x, y, c; n   void read() { n     cin >> x >> y >> c; n     x--, y--; n   } n } qry[Q]; n int n, m, q; n int mp[N][N]; n int f[N * N]; n int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0}; n int ch[Q], off[Q]; n int ans[Q]; n int gid(int i, int j) { return i * m + j; } n int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } n bool unite(int x, int y) { n   x = find(x); n   y = find(y); n   if (x == y) return false; n   f[x] = y; n   return true; n } n int build(int val, int ig) { n   for (int k = 0; k < n * m; k++) { n     f[k] = k; n   } n   for (int ki = 0; ki < n; ki++) { n     for (int kj = 0; kj < m; kj++) { n       if (ig != -1 && (!((mp[ki][kj] == val) ^ ig))) continue; n       if (ki + 1 < n && mp[ki + 1][kj] == mp[ki][kj]) n         unite(gid(ki + 1, kj), gid(ki, kj)); n       if (kj + 1 < m && mp[ki][kj + 1] == mp[ki][kj]) n         unite(gid(ki, kj + 1), gid(ki, kj)); n     } n   } n   int ccnt = 0; n   for (int k = 0; k < n * m; k++) ccnt += (f[k] == k); n   return ccnt; n } n bool in_bound(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m >> q; n   for (int i = 0; i < q; i++) qry[i].read(); n   int r = 0; n   for (int i = 0; i < q; i = r) { n     while (r < q && qry[r].c == qry[i].c) r++; n     build(qry[i].c, 0); n     int cur = 0; n     for (int j = i; j < r; j++) { n       int x = qry[j].x, y = qry[j].y; n       ch[j] = mp[x][y]; n       if (mp[x][y] != qry[j].c) cur++; n       mp[x][y] = qry[j].c; n       for (int d = 0; d < 4; d++) { n         int nx = x + dx[d], ny = y + dy[d]; n         if (in_bound(nx, ny) && mp[x][y] == mp[nx][ny]) { n           if (unite(gid(x, y), gid(nx, ny))) cur--; n         } n       } n       off[j] += cur; n     } n     cur = 0; n     build(qry[i].c, 1); n     for (int j = r - 1; j >= i; j--) { n       off[j] += cur; n       int x = qry[j].x, y = qry[j].y; n       if (mp[x][y] != ch[j]) { n         cur++; n         mp[x][y] = ch[j]; n         for (int d = 0; d < 4; d++) { n           int nx = x + dx[d], ny = y + dy[d]; n           if (in_bound(nx, ny) && mp[x][y] == mp[nx][ny]) { n             if (unite(gid(x, y), gid(nx, ny))) cur--; n           } n         } n       } n     } n     int ccnt = build(qry[i].c, -1); n     for (int j = i; j < r; j++) { n       ans[j] = ccnt - cur + off[j]; n     } n     for (int j = i; j < r; j++) { n       int x = qry[j].x, y = qry[j].y; n       mp[x][y] = qry[j].c; n     } n   } n   for (int i = 0; i < q; i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x[105], mem[101][201][51][3], ans; n string s; n int solve(int idx, int pos, int rem, int dir) { n   int &ret = mem[idx][100 + pos][rem][dir + 1]; n   if (rem == 0) { n     int cur_dir = dir, sum = 0; n     for (int i = idx; i < s.size(); i++) n       if (x[i]) n         sum += cur_dir; n       else n         cur_dir *= -1; n     return abs(pos + sum); n   } n   if (idx == s.size()) return -1e8; n   if (ret != -1) return ret; n   int next_dir = (x[idx]) ? dir : -dir; n   int c1 = solve(idx + 1, pos + x[idx] * dir, rem, next_dir); n   int new_dir = (x[idx]) ? -dir : dir; n   int new_pos = (x[idx]) ? pos : pos + dir; n   int c2 = -1e8; n   if (rem >= 2) c2 = solve(idx, pos, rem - 2, dir); n   int c3 = -1e8; n   if (rem >= 1) c3 = solve(idx + 1, new_pos, rem - 1, new_dir); n   ans = max(ans, max(max(c1, c2), c3)); n   return ret = ans; n } n int main() { n   cin >> s >> n; n   memset(mem, -1, sizeof mem); n   for (int i = 0; i < s.size(); i++) x[i] = (s[i] ==  F ); n   cout << max(solve(0, 0, n, -1), solve(0, 0, n, 1)); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long a, b, p, x; n long long Pow(long long x, long long y, long long mod) { n   if (!y) return 1; n   if (y == 1) return x % mod; n   long long t = Pow(x, y >> 1, mod), ans = t * t % mod; n   if (y & 1) ans = ans * x % mod; n   return ans; n } n int main() { n   while (scanf( %lld %lld %lld %lld , &a, &b, &p, &x) != EOF) { n     long long P = p * (p - 1), ans = 0; n     for (long long i = 1; i < p; i++) { n       long long r = b * Pow(Pow(a, i, p), p - 2, p) % p, c; n       if (i >= r) n         c = (i + (i - r) * (p - 1)) % P; n       else n         c = (i + (i + p - r) * (p - 1)) % P; n       if (!c) c = P; n       ans += ((x / P) + ((x % P >= c) ? 1 : 0)); n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int i, j; n   if (k == 1 && n == 1) { n     cout <<  a ; n     return 0; n   } n   if (k > n || (k == 1)) { n     cout << -1; n     return 0; n   } n   i = 1; n   while (n - i >= k - 2) { n     cout << char( a  + !(i % 2)); n     ++i; n   } n   for (i = 2; i < k; i++) cout << char( a  + i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     cout << a + b + c - (2 * (min(min(a, b), min(b, c)))) << endl; n   } n } n


#include<bits/stdc++.h> n #include<vector> n #include<map> n #include<queue> n #define LL long long n #define INF INT64_MAX n #define MOD 1000000007 n #define stree SegTree[root] n #define lson SegTree[root << 1] n #define rson SegTree[root << 1 | 1] n using namespace std; n const int N = 100005; n LL a[N]; n int main() n { n     int t, n; n     cin>>t; n     LL Min = INF; n     int tt = 0; n     while(t--) n     { n         tt++; n         cin>>n; n         for(int i = 1;i <= n;i++) n         { n             cin>>a[i]; n         } n         LL ans = INF, tem = 0, m1 = INT_MAX, m2 = INT_MAX; n         for(int i = 1;i <= n;i++) n         { n             if(i&1) n             { n                 m1 = min(m1, a[i]); n                 ans = min(ans, tem + m1*(n-i/2) + m2*(n-i/2)); n                 tem += a[i]; n                 //printf( 1ans = %lld tem = %lld n , ans, tem); n             } n             else n             { n                 m2 = min(m2, a[i]); n                 ans = min(ans, tem + m2*(n-(i-1)/2) + m1*(n-i/2)); n                 tem += a[i]; n                 //printf( 2ans = %lld tem = %lld n , ans, tem); n             } n         } n         printf( %lld n , ans); n     } n     return 0; n } n


#include <bits/stdc++.h> n #include <time.h> n using namespace std; n  n #define ll long long n #define ld long double n #define precision(x,d) cout<<fixed<<setprecision(d)<<x n #define all(x) x.begin(),x.end() n #define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL)); n #define MOD 1000000007 n #define INF LONG_LONG_MAX n #define NINF LONG_LONG_MIN n #define endl   n  n #define yes cout<< YES <<endl n #define no cout<< NO <<endl n #define db4(x,y,z,k) cout<<#x<<  :  <<x<<  t <<#y<<  :  <<y<<  t <<#z<<  :  <<z<<  t <<#k<<  :  <<k<<endl; n  n int main() n { n     IOS; n     ll t,n,m,k,u,v,x,y; n     t=1; n     cin >> t; n     while(t--) n     { n         cin >> n; n         vector<ll> a(n); n         for(int i=0;i<n;i++) n             cin >> a[i]; n         sort(all(a)); n         ll ans = 0; n         for(int i=1;i<n;i++) n         { n             m = i; n             ans -= m*a[i]; n             k = (n-i-1); n             ans += k*a[i]; n         } n         ans += a[n-1]; n         cout << ans << endl; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using Pii = pair<int, int>; n using Pll = pair<ll, ll>; n template <typename...> n static inline int getchar_unlocked(void) { n   return getchar(); n } n template <typename...> n static inline void putchar_unlocked(int c) { n   putchar(c); n } n void reader(int& x) { n   int k, m = 0; n   x = 0; n   for (;;) { n     (k) = getchar_unlocked(); n     if (k ==  - ) { n       m = 1; n       break; n     } n     if ( 0  <= k && k <=  9 ) { n       x = k -  0 ; n       break; n     } n   } n   for (;;) { n     (k) = getchar_unlocked(); n     if (k <  0  || k >  9 ) break; n     x = x * 10 + k -  0 ; n   } n   if (m) x = -x; n } n void reader(ll& x) { n   int k, m = 0; n   x = 0; n   for (;;) { n     (k) = getchar_unlocked(); n     if (k ==  - ) { n       m = 1; n       break; n     } n     if ( 0  <= k && k <=  9 ) { n       x = k -  0 ; n       break; n     } n   } n   for (;;) { n     (k) = getchar_unlocked(); n     if (k <  0  || k >  9 ) break; n     x = x * 10 + k -  0 ; n   } n   if (m) x = -x; n } n int reader(char c[]) { n   int i, s = 0; n   for (;;) { n     (i) = getchar_unlocked(); n     if (i !=     && i !=   n  && i !=   r  && i !=   t  && i != EOF) break; n   } n   c[s++] = i; n   for (;;) { n     (i) = getchar_unlocked(); n     if (i ==     || i ==   n  || i ==   r  || i ==   t  || i == EOF) break; n     c[s++] = i; n   } n   c[s] =   0 ; n   return s; n } n int reader(string& c) { n   int i; n   for (;;) { n     (i) = getchar_unlocked(); n     if (i !=     && i !=   n  && i !=   r  && i !=   t  && i != EOF) break; n   } n   c.push_back(i); n   for (;;) { n     (i) = getchar_unlocked(); n     if (i ==     || i ==   n  || i ==   r  || i ==   t  || i == EOF) break; n     c.push_back(i); n   }; n   return ((int)c.size()); n } n template <class T, class S> n void reader(T& x, S& y) { n   reader(x); n   reader(y); n } n template <class T, class S, class U> n void reader(T& x, S& y, U& z) { n   reader(x); n   reader(y); n   reader(z); n } n template <class T, class S, class U, class V> n void reader(T& x, S& y, U& z, V& w) { n   reader(x); n   reader(y); n   reader(z); n   reader(w); n } n void writer(int x, char c) { n   int s = 0, m = 0; n   char f[10]; n   if (x < 0) m = 1, x = -x; n   while (x) f[s++] = x % 10, x /= 10; n   if (!s) f[s++] = 0; n   if (m) putchar_unlocked( - ); n   while (s--) putchar_unlocked(f[s] +  0 ); n   putchar_unlocked(c); n } n void writer(ll x, char c) { n   int s = 0, m = 0; n   char f[20]; n   if (x < 0) m = 1, x = -x; n   while (x) f[s++] = x % 10, x /= 10; n   if (!s) f[s++] = 0; n   if (m) putchar_unlocked( - ); n   while (s--) putchar_unlocked(f[s] +  0 ); n   putchar_unlocked(c); n } n void writer(const char c[]) { n   int i; n   for (i = 0; c[i] !=   0 ; i++) putchar_unlocked(c[i]); n } n void writer(const string& x, char c) { n   int i; n   for (i = 0; x[i] !=   0 ; i++) putchar_unlocked(x[i]); n   putchar_unlocked(c); n } n void writer(const char x[], char c) { n   int i; n   for (i = 0; x[i] !=   0 ; i++) putchar_unlocked(x[i]); n   putchar_unlocked(c); n } n template <class T> n void writerLn(T x) { n   writer(x,   n ); n } n template <class T, class S> n void writerLn(T x, S y) { n   writer(x,    ); n   writer(y,   n ); n } n template <class T, class S, class U> n void writerLn(T x, S y, U z) { n   writer(x,    ); n   writer(y,    ); n   writer(z,   n ); n } n template <class T, class S, class U, class V> n void writerLn(T x, S y, U z, V v) { n   writer(x,    ); n   writer(y,    ); n   writer(z,    ); n   writer(v,   n ); n } n template <class T> n void writerArr(T x[], int n) { n   if (!n) { n     putchar_unlocked(  n ); n     return; n   } n   for (int i = 0; i < (n - 1); i++) writer(x[i],    ); n   writer(x[n - 1],   n ); n } n template <class T> n void writerArr(vector<T>& x) { n   writerArr(x.data(), (int)x.size()); n } n template <class T> n void chmin(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T& a, const T& b) { n   if (a < b) a = b; n } n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n ll mod_pow(ll a, ll n, ll mod) { n   ll ret = 1; n   ll p = a % mod; n   while (n) { n     if (n & 1) ret = ret * p % mod; n     p = p * p % mod; n     n >>= 1; n   } n   return ret; n } n template <class T> n T extgcd(T a, T b, T& x, T& y) { n   for (T u = y = 1, v = x = 0; a;) { n     T q = b / a; n     swap(x -= q * u, u); n     swap(y -= q * v, v); n     swap(b -= q * a, a); n   } n   return b; n } n ll mod_inv(ll a, ll m) { n   ll x, y; n   extgcd<ll>(a, m, x, y); n   return (m + x % m) % m; n } n int test(int a, int b, int c) { n   set<tuple<int, int, int>> st; n   for (int i = 0; i < (a + 1); i++) n     if (i != 0 && a % i == 0) { n       for (int j = 0; j < (b + 1); j++) n         if (j != 0 && b % j == 0) { n           for (int k = 0; k < (c + 1); k++) n             if (k != 0 && c % k == 0) { n               int cur[] = {i, j, k}; n               sort(cur, cur + 3); n               st.emplace(cur[0], cur[1], cur[2]); n             } n         } n     } n   int x[] = {0, 0, 0, 0}; n   for (auto abc : st) { n     int a, b, c; n     tie(a, b, c) = abc; n     if (a == b && b == c) n       x[0]++; n     else if (a == b && b < c) n       x[1]++; n     else if (a < b && b == c) n       x[2]++; n     else if (a < b && b < c) n       x[3]++; n     else { n       cout <<  ? ; n     } n   } n   return ((int)st.size()); n } n int cnt[((int)1e5) + 1]; n void init() { n   for (int i = 1; i <= ((int)1e5); i++) { n     for (int j = i; j <= ((int)1e5); j += i) { n       cnt[j]++; n     } n   } n } n int solve(int x, int y, int z) { n   int g = cnt[gcd(gcd(x, y), z)]; n   int d = cnt[gcd(x, y)] - g; n   int e = cnt[gcd(x, z)] - g; n   int f = cnt[gcd(y, z)] - g; n   int a = cnt[x] - d - e - g; n   int b = cnt[y] - d - f - g; n   int c = cnt[z] - f - e - g; n   int h[] = {a, b, c, d, e, f, g}; n   int oks[3][7] = { n       {1, 0, 0, 1, 1, 0, 1}, n       {0, 1, 0, 1, 0, 1, 1}, n       {0, 0, 1, 0, 1, 1, 1}, n   }; n   bool used[7][7][7] = {}; n   auto checkUsed = [&](int i, int j, int k) { n     if (i > k) swap(i, k); n     if (j > k) swap(j, k); n     if (i > j) swap(i, j); n     if (used[i][j][k]) return false; n     used[i][j][k] = true; n     return true; n   }; n   int part[5] = {}; n   int ans = 0; n   for (int i = 0; i < (7); i++) n     if (oks[0][i]) { n       for (int j = 0; j < (7); j++) n         if (oks[1][j]) { n           for (int k = 0; k < (7); k++) n             if (oks[2][k]) { n               if (!checkUsed(i, j, k)) { n                 continue; n               } n               if (i != j && j != k && i != k) { n                 int cur = h[i] * h[j] * h[k]; n                 ans += cur; n                 part[0] += cur; n               } else if (i == j && j != k) { n                 int cur = (h[i] - 1) * h[i] / 2 + h[i]; n                 cur *= h[k]; n                 ans += cur; n                 part[1] += cur; n               } else if (i != j && j == k) { n                 int cur = (h[j] - 1) * h[j] / 2 + h[j]; n                 cur *= h[i]; n                 ans += cur; n                 part[2] += cur; n               } else if (i == k && j != k) { n                 int cur = (h[k] - 1) * h[k] / 2 + h[k]; n                 cur *= h[j]; n                 ans += cur; n                 part[3] += cur; n               } else { n                 int aa = h[i]; n                 int bb = (h[i]) * (h[i] - 1) * (h[i] - 2); n                 int cc = h[i] * h[i] * h[i] - aa - bb; n                 int cur = aa + bb / 6 + cc / 3; n                 ans += cur; n                 part[4] += cur; n               } n             } n         } n     } n   return ans; n } n void test() { n   for (int i = 1; i <= 70; i++) { n     for (int j = 1; j <= 70; j++) { n       for (int k = 1; k <= 70; k++) { n         int a1 = test(i, j, k); n         int a2 = solve(i, j, k); n         if (a1 != a2) { n           cout <<  ? ; n         } n       } n     } n   } n   cout <<  ok  << endl; n } n int main() { n   init(); n   int t; n   reader(t); n   while (t--) { n     int x, y, z; n     reader(x, y, z); n     int ans = solve(x, y, z); n     writerLn(ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int c[301]; n vector<int> a[301]; n vector<int> lt[301]; n int id[301]; n int dem = 0; n int d[301]; n void nhap() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &c[i]); n     id[c[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==  1 ) { n         int u = c[i]; n         int v = c[j + 1]; n         a[u].push_back(v); n         a[v].push_back(u); n       } n     } n   } n } n void bfs(int s) { n   queue<int> q; n   q.push(s); n   dem++; n   d[s] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     lt[dem].push_back(u); n     for (vector<int>::iterator i = a[u].begin(); i != a[u].end(); i++) { n       if (d[*i] == 0) { n         d[*i] = 1; n         q.push(*i); n       } n     } n   } n } n void xuly(int k) { n   priority_queue<int, vector<int>, greater<int> > q; n   vector<int> t; n   for (auto i : lt[k]) { n     q.push(i); n     t.push_back(id[i]); n   } n   sort(t.begin(), t.end()); n   for (auto i : t) { n     c[i] = q.top(); n     q.pop(); n   } n } n int main() { n   nhap(); n   for (int i = 1; i <= n; i++) { n     if (d[i] == 0) { n       bfs(i); n     } n   } n   for (int i = 1; i <= dem; i++) { n     xuly(i); n   } n   for (int i = 1; i <= n; i++) cout << c[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans % m; n } n const long long N = 1e6 + 5; n long long n, m; n long long a[N], b[N], x[N], y[N]; n map<long long, long long> ans; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i] >> x[i]; n     ans[a[i]] = x[i]; n   } n   cin >> m; n   for (long long i = 1; i <= m; i++) { n     cin >> b[i] >> y[i]; n     ans[b[i]] = max(ans[b[i]], y[i]); n   } n   long long answer = 0; n   for (auto it : ans) answer += it.second; n   cout << answer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 888; n long long dp[maxn][12]; n void solve() { n   long long n; n   scanf( %lld , &n); n   memset(dp, -1, sizeof(dp)); n   dp[0][1] = 0; n   vector<long long> v[4]; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 0; j < 4; j++) v[j].clear(); n     long long k; n     scanf( %lld , &k); n     while (k--) { n       long long c, d; n       scanf( %lld %lld , &c, &d); n       v[c].push_back(d); n     } n     for (long long j = 1; j <= 3; j++) { n       sort(v[j].begin(), v[j].end()); n       reverse(v[j].begin(), v[j].end()); n     } n     for (long long prec = 0; prec <= 9; prec++) { n       if (dp[i - 1][prec] < 0) continue; n       long long val = LLONG_MIN / 10; n       if (!v[1].empty()) val = max(val, v[1][0]); n       if (!v[2].empty()) val = max(val, v[2][0]); n       if (!v[3].empty()) val = max(val, v[3][0]); n       if (prec == 0) val *= 2; n       if (val > 0) n         dp[i][(prec + 1) % 10] = n             max(dp[i][(prec + 1) % 10], dp[i - 1][prec] + val); n       val = LLONG_MIN / 10; n       if (!v[1].empty() && !v[2].empty()) { n         long long temp = v[1][0] + v[2][0]; n         long long mx = max(v[1][0], v[2][0]); n         if (prec == 0 || prec == 9) temp += mx; n         val = max(val, temp); n       } n       if (v[1].size() >= 2) { n         long long temp = v[1][0] + v[1][1]; n         long long mx = max(v[1][0], v[1][1]); n         if (prec == 0 || prec == 9) temp += mx; n         val = max(val, temp); n       } n       if (val > 0) n         dp[i][(prec + 2) % 10] = n             max(dp[i][(prec + 2) % 10], dp[i - 1][prec] + val); n       val = LLONG_MIN / 10; n       if (v[1].size() >= 3) { n         long long temp = v[1][0] + v[1][1] + v[1][2]; n         long long mx = max(v[1][0], max(v[1][2], v[1][1])); n         if (prec == 0 || prec == 9 || prec == 8) temp += mx; n         val = max(val, temp); n       } n       if (val > 0) n         dp[i][(prec + 3) % 10] = n             max(dp[i][(prec + 3) % 10], dp[i - 1][prec] + val); n       dp[i][prec] = max(dp[i][prec], dp[i - 1][prec]); n     } n   } n   long long ans = 0; n   for (long long i = 0; i <= 9; i++) ans = max(ans, dp[n][i]); n   cout << ans << endl; n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n unsigned long long bt[maxn]; n unsigned long long d[maxn]; n map<unsigned long long, int> has; n pair<int, int> edg[maxn]; n int f[maxn]; n int id[maxn]; n int lab[maxn]; n vector<int> e[maxn]; n int vis[maxn]; n int getf(int x) { n   if (x == f[x]) n     return x; n   else n     return f[x] = getf(f[x]); n } n void dfs(int x, int y, int de) { n   int i; n   lab[x] = de; n   vis[x] = 1; n   for (i = 0; i < (int)e[x].size(); i++) { n     if (e[x][i] == y) continue; n     dfs(e[x][i], x, de + 1); n   } n } n int main() { n   int i, j, n, m; n   cin >> n >> m; n   int x, y; n   bt[0] = 1; n   for (int i = 1; i <= n; i++) { n     bt[i] = 3 * bt[i - 1]; n     d[i] = bt[i]; n   } n   for (i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     edg[i] = make_pair(x, y); n     d[x] += bt[y], d[y] += bt[x]; n   } n   j = 1; n   for (i = 1; i <= n; i++) { n     if (!has[d[i]]) has[d[i]] = j++; n     id[i] = has[d[i]]; n     f[i] = i; n   } n   for (i = 0; i < m; i++) { n     x = id[edg[i].first], y = id[edg[i].second]; n     if (x == y) continue; n     for (j = 0; j < (int)e[x].size(); j++) { n       if (e[x][j] == y) break; n     } n     if (j >= 2) { n       printf( NO n ); n       return 0; n     } n     if (j == (int)e[x].size()) n       e[x].push_back(y); n     else n       continue; n     for (j = 0; j < (int)e[y].size(); j++) { n       if (e[y][j] == x) break; n     } n     if (j >= 2) { n       printf( NO n ); n       return 0; n     } n     if (j == (int)e[y].size()) n       e[y].push_back(x); n     else n       continue; n     if (getf(x) == getf(y)) { n       printf( NO n ); n       return 0; n     } else n       f[getf(y)] = f[getf(x)]; n   } n   int cnt = 0; n   for (i = 1; i <= n; i++) { n     if (vis[id[i]] == 0 && (int)e[id[i]].size() < 2) { n       vis[id[i]] = 1; n       dfs(id[i], -1, 1); n     } n   } n   printf( YES n ); n   for (i = 1; i <= n; i++) printf(i == n ?  %d n  :  %d  , lab[id[i]]); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX_N = 2e5 + 5, inf = 1e18, mod = 1000000007; n const double PI = 3.1415926536; n int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n template <typename T> n ostream& operator<<(ostream& out, const vector<T>& mas) { n   for (const auto& x : mas) { n     out << x <<    ; n   } n   return out; n } n void ok() { cout <<  YES  <<   n ; } n void no() { cout <<  NO  <<   n ; } n inline long long nxt() { n   long long x; n   cin >> x; n   return x; n } n struct Node { n   vector<int> g; n   long long sum = 0; n   long long sum1 = 0; n   void push(int v) { g.push_back(v); } n   vector<int>::iterator begin() { return g.begin(); } n   vector<int>::iterator end() { return g.end(); } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   int n = nxt(); n   vector<Node> g(n); n   vector<long long> a(n); n   long long total = 0; n   for (int i = 0; i < n; i++) { n     a[i] = nxt(); n     total += a[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     int v = nxt() - 1, u = nxt() - 1; n     g[v].push(u); n     g[u].push(v); n   } n   vector<int> lvl(n); n   function<void(int, int)> dfs = [&](int v, int p) { n     g[v].sum1 = a[v] * lvl[v]; n     g[v].sum = a[v]; n     for (const auto& to : g[v]) { n       if (to != p) { n         lvl[to] += lvl[v] + 1; n         dfs(to, v); n         g[v].sum += g[to].sum; n         g[v].sum1 += g[to].sum1; n       } n     } n   }; n   dfs(0, -1); n   long long ans = 0; n   long long res = g[0].sum1; n   function<void(int, int)> calc = [&](int v, int p) { n     ans = max(ans, res); n     for (const auto& to : g[v]) { n       if (to == p) continue; n       g[v].sum -= g[to].sum; n       res -= g[to].sum; n       g[to].sum += g[v].sum; n       res += g[v].sum; n       calc(to, v); n       res -= g[v].sum; n       g[to].sum -= g[v].sum; n       res += g[to].sum; n       g[v].sum += g[to].sum; n     } n   }; n   calc(0, -1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Mod = 1000000000 + 7; n const int MAXK = 1000000; n int power(int x, int k) { n   int ret = 1; n   while (k) { n     if (k & 1) ret = 1LL * ret * x % Mod; n     x = 1LL * x * x % Mod; n     k >>= 1; n   } n   return ret; n } n int n, k, ans; n int f[MAXK + 10]; n void init() { n   cin >> n >> k; n   if (n <= k + 10) { n     for (int i = 1; i <= n; i++) ans = (power(i, k) + ans) % Mod; n     cout << ans; n   } else { n     for (int i = 1; i <= k + 2; i++) f[i] = (f[i - 1] + power(i, k)) % Mod; n     int s = 1, m = 1; n     for (int j = 2; j <= k + 2; j++) s = 1LL * s * (n - j) % Mod; n     for (int j = 2; j <= k + 2; j++) m = 1LL * m * ((1 - j) + Mod) % Mod; n     int l = -(k + 1), r = 1; n     for (int i = 1; i <= k + 2; i++) { n       ans = (1LL * f[i] * s % Mod * power(m, Mod - 2) % Mod + ans) % Mod; n       s = 1LL * s * (n - i) % Mod * power(n - (i + 1), Mod - 2) % Mod; n       m = 1LL * m * r % Mod * power(l + Mod, Mod - 2) % Mod; n       l++, r++; n     } n     cout << ans; n   } n   return; n } n int main() { n   init(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 202020; n long long a[N]; n long long l[N], r[N]; n int main() { n   int n; n   long long k; n   scanf( %d%I64d , &n, &k); n   for (int i = 1; i <= (n); ++i) scanf( %I64d , &a[i]); n   for (int i = 1; i <= (n); ++i) l[i] = a[i - 1] == 1 ? 1 + l[i - 1] : 0; n   for (int i = n; i >= 1; --i) r[i] = a[i + 1] == 1 ? 1 + r[i + 1] : 0; n   long long mx = 0x3FFFFFFFFFFFFFFFLL; n   long long ans = 0; n   if (k == 1) { n     for (int i = 1; i <= (n); ++i) n       if (a[i] == 1) ++ans; n   } n   for (int i = 1; i <= (n); ++i) n     if (a[i] != 1) { n       long long ts = 0, tr = 1; n       for (int j = i; j <= n; ++j) { n         if (a[j] == 1) { n           ts += r[j - 1]; n           j += r[j - 1]; n           j -= 1; n           continue; n         } n         if (tr > mx / a[j]) break; n         ts += a[j]; n         tr *= a[j]; n         if (tr % k == 0) { n           long long need = tr / k - ts; n           if (need >= 0) { n             long long lc = min(need, l[i]), rc = min(need, r[j]); n             long long cnt = lc + rc - need + 1; n             cnt = max(cnt, 0LL); n             ans += cnt; n           } n         } n       } n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int s = 0, ans = 0; n   for (int i = 1;; i++) { n     for (int j = 1; j <= i; j++) s += j; n     if (s > n) break; n     ans++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void zanj0() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n void solve() { n   int n, m; n   cin >> n >> m; n   int S = int(0.45 * sqrt(n)) + 1; n   vector<int> v(n), rev_v(n), s_jump(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     v[i]--; n   } n   for (int i = 0; i < n; i++) { n     rev_v[v[i]] = i; n   } n   auto compute_s_jump = [&](int index) -> int { n     int x = index; n     for (int i = 0; i < S; i++) { n       x = v[x]; n     } n     return s_jump[index] = x; n   }; n   for (int i = 0; i < n; i++) compute_s_jump(i); n   while (m--) { n     int type; n     cin >> type; n     if (type == 2) { n       int x, k; n       cin >> x >> k; n       x--; n       while (S <= k) { n         k -= S; n         x = s_jump[x]; n       } n       while (k--) { n         x = v[x]; n       } n       cout << x + 1 <<   n ; n     } else { n       int x, y; n       cin >> x >> y; n       x--; n       y--; n       swap(rev_v[v[x]], rev_v[v[y]]); n       swap(v[x], v[y]); n       int sx = compute_s_jump(x); n       int sy = compute_s_jump(y); n       for (int i = 0; i < S; i++) { n         s_jump[x] = sx; n         s_jump[y] = sy; n         x = rev_v[x]; n         y = rev_v[y]; n         sx = rev_v[sx]; n         sy = rev_v[sy]; n       } n     } n   } n } n int32_t main() { n   zanj0(); n   solve(); n   cerr <<  Time :   << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC n        <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e6; n ; n int n, m, a, l, r, num; n int arr[2]; n int main() { n   scanf( %d%d , &n, &m); n   while (n--) { n     scanf( %d , &a); n     if (a == -1) n       ++arr[0]; n     else n       ++arr[1]; n   } n   while (m--) { n     scanf( %d%d , &l, &r); n     if (l == r) { n       printf( 0 n ); n       continue; n     } n     num = r - l + 1; n     if (num % 2 != 0) n       printf( 0 n ); n     else { n       if (num / 2 > arr[0] || num / 2 > arr[1]) n         printf( 0 n ); n       else n         printf( 1 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Maxn = 1e5 + 7; n const long long Max = 1e3 + 7; n const long long Mod = 1e9 + 7; n const long long Inf = 1e9 + 7; n string s[Maxn], V[Maxn]; n long long ans[5]; n long long check(char t) { n   if (t ==  u  || t ==  o  || t ==  a  || t ==  i  || t ==  e ) return true; n   return false; n } n void f(long long a, long long b, long long c, long long d) { n   if (V[a] == V[b] && V[c] == V[d]) n     ; n   else n     ans[1] = 0; n   if (V[a] == V[c] && V[b] == V[d]) n     ; n   else n     ans[2] = 0; n   if (V[a] == V[d] && V[c] == V[b]) n     ; n   else n     ans[3] = 0; n   if (V[a] == V[b] && V[c] == V[d] && V[c] == V[b]) n     ; n   else n     ans[4] = 0; n } n void print(long long i) { n   if (i == 1) cout <<  aabb  << endl; n   if (i == 2) cout <<  abab  << endl; n   if (i == 3) cout <<  abba  << endl; n   if (i == 4) cout <<  aaaa  << endl; n } n int main() { n   long long n, k; n   cin >> n >> k; n   for (long long i = 1; i <= 4 * n; i++) { n     cin >> s[i]; n     long long K = k; n     for (long long j = s[i].size() - 1; K && j >= 0; j--) { n       V[i] += s[i][j]; n       if (check(s[i][j])) K--; n     } n     if (K) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   ans[1] = ans[2] = ans[3] = ans[4] = 1; n   for (long long i = 1; i <= n; i++) { n     long long st = (i - 1) * 4; n     f(st + 1, st + 2, st + 3, st + 4); n   } n   for (long long i = 4; i >= 1; i--) { n     if (ans[i]) { n       print(i); n       return 0; n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma gcc optimize( Ofast ) n using namespace std; n using ll = long long; n const int N = 1e6 + 2; n int n, cnt, f[N], vis[N]; n int qt, q[N]; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   if (n & 1) return cout <<  -1 n , 0; n   v.push_back(0); n   for (int i = 0; i < n; ++i) { n     int a; n     cin >> a; n     if (a > 0) { n       if (f[a] || vis[a]) return cout <<  -1 n , 0; n       ++f[a], ++cnt, vis[a] = 1, q[++qt] = a; n     } else { n       a = -a; n       if (!f[a]) return cout <<  -1 n , 0; n       --f[a], --cnt; n       if (!cnt) { n         v.push_back(i + 1); n         while (qt) vis[q[qt--]] = 0; n       } n     } n   } n   if (cnt) return cout <<  -1 n , 0; n   cout << v.size() - 1 <<   n ; n   for (int i = 1; i < v.size(); ++i) n     cout << v[i] - v[i - 1] <<    n [i + 1 == v.size()]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   for (int i = 0; (n - a * i) >= 0; i++) { n     if ((n - a * i) % b == 0) { n       cout <<  YES n ; n       cout << i <<     << (n - a * i) / b << endl; n       return 0; n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n const int N = (int)2e5 + 7; n const int inf = (int)1e9 + 7; n const int mod = (int)1e9 + 7; n const long long linf = (long long)1e18 + 7; n const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}; n const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; n using namespace std; n struct line { n   long long k, b; n   int id; n   line() {} n   line(long long k, long long b, int id) : k(k), b(b), id(id) {} n   long long get(long long x) { return k * x + b; } n   bool operator<(const line &R) const { n     if (k != R.k) return k > R.k; n     return b > R.b; n   } n }; n int n; n class CHT { n  private: n   vector<line> v; n  n  public: n   long double inter(line a, line b) { n     return (b.b - a.b) / (double)(a.k - b.k); n   } n   void add(line x) { n     while ((int)v.size() > 1 && inter(v[(int)v.size() - 2], x) >= n                                     inter(v[(int)v.size() - 2], v.back())) n       v.pop_back(); n     v.push_back(x); n   } n   void push(line x) { v.push_back(x); } n   void build() { n     vector<line> b; n     sort(v.begin(), v.end()); n     for (int i = 0; i < (int)v.size(); i++) { n       if ((int)b.size() && b.back().k == v[i].k) continue; n       b.push_back(v[i]); n     } n     v.clear(); n     for (auto it : b) { n       add(it); n     } n   } n   line get(long long x) { n     int l = 0, r = (int)v.size() - 2, res = 0; n     while (l <= r) { n       int mid = l + r >> 1; n       if (v[mid].get(x) <= v[mid + 1].get(x)) n         res = mid + 1, l = mid + 1; n       else n         r = mid - 1; n     } n     return v[res]; n   } n }; n struct SegTree { n   CHT t[N << 2]; n   void upd(int p, line x, int v = 1, int tl = 1, int tr = n) { n     t[v].push(x); n     if (tl == tr) return; n     int tm = tl + tr >> 1; n     if (p <= tm) n       upd(p, x, v << 1, tl, tm); n     else n       upd(p, x, v << 1 | 1, tm + 1, tr); n   } n   void build(int v = 1, int tl = 1, int tr = n) { n     if (tl == tr) return; n     int tm = tl + tr >> 1; n     build(v << 1, tl, tm); n     build(v << 1 | 1, tm + 1, tr); n     t[v].build(); n   } n   line maxi(line a, line b, int x) { n     if (a.get(x) >= b.get(x)) return a; n     return b; n   } n   line get(int l, int r, int x, int v = 1, int tl = 1, int tr = n) { n     if (l <= tl && tr <= r) return t[v].get(x); n     int tm = tl + tr >> 1; n     if (r <= tm) return get(l, r, x, v << 1, tl, tm); n     if (tm < l) return get(l, r, x, v << 1 | 1, tm + 1, tr); n     return maxi(get(l, r, x, v << 1, tl, tm), n                 get(l, r, x, v << 1 | 1, tm + 1, tr), x); n   } n } t; n int a[N]; n long long s1[N], s2[N]; n int main() { n   ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     s1[i] = (long long)a[i] * i; n     s2[i] = a[i]; n   } n   for (int i = n; i >= 1; i--) { n     s1[i] += s1[i + 1]; n     s2[i] += s2[i + 1]; n     t.upd(i, line(s2[i + 1], -s1[i + 1], i)); n   } n   t.build(); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = max(ans, t.get(i, n, i - 1).get(i - 1) + s1[i] - (i - 1) * s2[i]); n   } n   cout << ans; n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n void answer() { n   int n; n   string s, t; n   cin >> n >> s >> t; n   int ps[26], pt[26]; n   int cs[26], ct[26]; n   memset(ps, -1, sizeof(ps)); n   memset(pt, -1, sizeof(pt)); n   memset(cs, 0, sizeof(cs)); n   memset(ct, 0, sizeof(ct)); n   for (int i = 0; i < s.size(); i++) { n     ps[s[i] -  a ] = i; n     cs[s[i] -  a ]++; n   } n   for (int i = 0; i < t.size(); i++) { n     pt[t[i] -  a ] = i; n     ct[t[i] -  a ]++; n   } n   for (int i = 0; i < 26; i++) { n     if (cs[i] != ct[i]) { n       cout <<  NO  <<   n ; n       return; n     } n   } n   int c = 0; n   for (int i = 0; i < 26; i++) { n     if (cs[i] > 1 || ct[i] > 1) { n       cout <<  YES  <<   n ; n       return; n     } n     for (int j = i + 1; j < 26; j++) { n       c += ((ps[i] < ps[j]) != (pt[i] < pt[j])); n     } n   } n   if (c % 2) n     cout <<  NO  <<   n ; n   else n     cout <<  YES  <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) answer(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double tick() { n   static clock_t oldtick; n   clock_t newtick = clock(); n   double diff = 1.0 * (newtick - oldtick) / CLOCKS_PER_SEC; n   oldtick = newtick; n   return diff; n } n long long gcd(long long a, long long b) { n   if ((a == 0) || (b == 0)) { n     return a + b; n   } n   return gcd(b, a % b); n } n long long powMod(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return (res % 1000000007); n } n long long pow2(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a); n     a = (a * a); n     b >>= 1; n   } n   return res; n } n bool isPrime(long long a) { n   for (long long i = 3; (i * i) <= a; i += 2) { n     if ((a % i) == 0) return false; n   } n   if ((a != 2) && ((a % 2) == 0)) { n     return false; n   } n   if (a == 1) { n     return false; n   } n   return true; n } n string conLlToStr(long long a) { n   stringstream mystr; n   mystr << a; n   return mystr.str(); n } n long long conStrToLl(string st) { n   long long numb = 0; n   long long len = st.size(), i, j = 0; n   for (long long i = len - 1; i >= 0; i--) { n     numb += (pow2(10, j) * (st[i] -  0 )); n     j++; n   } n   return numb; n } n long long basenoToDecimal(string st, long long basee) { n   long long i = 0, j, anss = 0; n   for (long long j = (int)st.length() - 1; j >= 0; j--) { n     anss += (st[j] -  0 ) * pow2(basee, i); n     i++; n   } n   return anss; n } n string decimalToString(long long num, long long basee) { n   long long i = 0, j, opop; n   string anss =   ; n   vector<string> stri; n   stri.push_back( 0 ); n   stri.push_back( 1 ); n   stri.push_back( 2 ); n   stri.push_back( 3 ); n   stri.push_back( 4 ); n   stri.push_back( 5 ); n   stri.push_back( 6 ); n   stri.push_back( 7 ); n   stri.push_back( 8 ); n   stri.push_back( 9 ); n   stri.push_back( A ); n   stri.push_back( B ); n   stri.push_back( C ); n   stri.push_back( D ); n   stri.push_back( E ); n   stri.push_back( F ); n   if (num == 0) { n     return  0 ; n   } n   while (num) { n     opop = (num % basee); n     anss += stri[opop]; n     num /= basee; n   } n   reverse(anss.begin(), anss.end()); n   return anss; n } n long long my_sqrt(long long x) { n   long long y = (long long)(sqrtl((long double)x) + 0.5); n   while (y * y < x) { n     y++; n   } n   while (y * y > x) { n     y--; n   } n   if (y * y == x) { n     return y; n   } n   return -1; n } n long long my_crt(long long x) { n   long long y = (long long)(powl((long double)x, 1.0 / 3.0) + 0.5); n   while (y * y * y < x) { n     y++; n   } n   while (y * y * y > x) { n     y--; n   } n   if (y * y * y == x) { n     return y; n   } n   return -1; n } n const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; n const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; n string S[100010]; n vector<string> stringContained[100010][30]; n vector<pair<string, string> > firstString, secondString; n vector<pair<pair<string, string>, pair<string, string> > > ans; n set<int> numVowel; n vector<string> temp; n bool isVowel(char ch) { n   if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u ) { n     return 1; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int N; n   cin >> N; n   for (long long i = 1; i <= N; i++) { n     cin >> S[i]; n     int vowel = 0; n     char lastVowel; n     for (long long j = 0; j <= (int)S[i].length() - 1; j++) { n       if (isVowel(S[i][j])) { n         vowel++; n         lastVowel = S[i][j]; n       } n     } n     if (vowel) { n       numVowel.insert(vowel); n       stringContained[vowel][lastVowel -  a ].push_back(S[i]); n     } n   } n   for (auto it = numVowel.begin(); it != numVowel.end(); ++it) { n     int i = (*it); n     for (long long j = 0; j <= 25; j++) { n       for (int k = 0; k < (int)stringContained[i][j].size(); k += 2) { n         if ((k + 1) < (int)stringContained[i][j].size()) { n           secondString.push_back(make_pair(stringContained[i][j][k], n                                            stringContained[i][j][k + 1])); n         } n       } n       if (stringContained[i][j].size() % 2) { n         temp.push_back(stringContained[i][j][stringContained[i][j].size() - 1]); n       } n     } n     for (int k = 0; k < (int)temp.size(); k += 2) { n       if ((k + 1) < (int)temp.size()) { n         firstString.push_back(make_pair(temp[k], temp[k + 1])); n       } n     } n     temp.clear(); n   } n   int ind1 = 0, ind2 = 0; n   while (ind1 < firstString.size() && ind2 < secondString.size()) { n     ans.push_back(make_pair( n         make_pair(firstString[ind1].first, secondString[ind2].first), n         make_pair(firstString[ind1].second, secondString[ind2].second))); n     ind1 += 1; n     ind2 += 1; n   } n   for (int k = ind2; k < (int)secondString.size(); k += 2) { n     if ((k + 1) < (int)secondString.size()) { n       ans.push_back(make_pair( n           make_pair(secondString[k].first, secondString[k + 1].first), n           make_pair(secondString[k].second, secondString[k + 1].second))); n     } n   } n   cout << ans.size() <<   n ; n   for (long long i = 0; i <= (int)ans.size() - 1; i++) { n     cout << ans[i].first.first <<     << ans[i].first.second <<   n ; n     cout << ans[i].second.first <<     << ans[i].second.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T ABS(T a) { n   return a > 0 ? a : -a; n } n template <typename T> n inline T MIN(T a, T b) { n   return a < b ? a : b; n } n template <typename T> n inline T MAX(T a, T b) { n   return a > b ? a : b; n } n template <typename T> n inline T CHKMIN(T &a, T b) { n   if (a > b) a = b; n   return a; n } n template <typename T> n inline T CHKMAX(T &a, T b) { n   if (a < b) a = b; n   return a; n } n template <typename T> n inline void SWAP(T &a, T &b) { n   static T c; n   c = a; n   a = b; n   b = c; n } n template <typename T, typename... T0> n T MAX(T a, T b, T0... c) { n   return a > b ? MAX(a, c...) : MAX(b, c...); n } n template <typename T, typename... T0> n T MIN(T a, T b, T0... c) { n   return a < b ? MIN(a, c...) : MIN(b, c...); n } n template <typename T, int n> n void myin(T a[]) { n   for (int i = (0); i < (int)(n); ++i) cin >> a[i]; n } n template <typename T> n void myin(T &a) { n   cin >> a; n } n template <typename T> n void print(T a) { n   cout << a <<    ; n } n template <typename T, typename... T0> n void print(T a, T0... b) { n   print(a); n   print(b...); n } n template <typename T> n void println(T a) { n   cout << a << endl; n } n template <typename T, typename... T0> n void println(T a, T0... b) { n   print(a); n   println(b...); n } n int n, in[120000]; n long long dp[120000]; n int main() { n   long long sum = 0; n   while (cin >> n) { n     for (int i = (0); i < (int)(n); ++i) { n       scanf( %d , in + i); n       sum += in[i]; n     } n     if (sum % 3 != 0) { n       puts( 0 ); n       continue; n     } n     long long tmp = sum / 3, now = 0, ans = 0; n     for (int i = (0); i < (int)(n); ++i) { n       now += in[i]; n       if (i == 0) n         dp[i] = 0; n       else n         dp[i] = dp[i - 1]; n       if (now == tmp) ++dp[i]; n     } n     tmp += tmp, now = in[0]; n     for (int i = (1); i < (int)(n - 1); ++i) { n       now += in[i]; n       if (now == tmp) ans += dp[i - 1]; n     } n     println(ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int kN = 1e5 + 10; n const int MOD = 1e9 + 7; n int N, M; n int power(int d, int k) { n   int ret = 1, tmp = d; n   while (k) { n     if (k & 1) { n       ret = 1ll * ret * tmp % MOD; n     } n     tmp = 1ll * tmp * tmp % MOD; n     k >>= 1; n   } n   return ret; n } n void cant() { n   printf( 0 ); n   exit(0); n } n struct Edge { n   int u, v; n   bool diff; n } es[kN]; n struct UFS { n   int fa[kN]; n   int lowbit(int i) { return i & -i; } n   void init() { n     for (int i = 0; i < kN; i++) fa[i] = i; n   } n   int getFa(int i) { n     if (fa[i] != i) fa[i] = getFa(fa[i]); n     return fa[i]; n   } n   bool isSameFa(int u, int v) { n     int uf = getFa(u); n     int vf = getFa(v); n     return uf == vf; n   } n   void link(int u, int v) { n     int uf = getFa(u); n     int vf = getFa(v); n     fa[uf] = vf; n   } n } ufs; n int col[kN]; n vector<int> G[kN]; n void dfs(int u, int c) { n   if (col[u]) { n     if (col[u] != c) cant(); n     return; n   } n   col[u] = c; n   c = c == 1 ? 2 : 1; n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i]; n     dfs(v, c); n   } n } n int main() { n   scanf( %d %d , &N, &M); n   for (int i = 1; i <= M; i++) { n     int op, u, v; n     scanf( %d %d %d , &u, &v, &op); n     if (u > v) swap(u, v); n     es[i].u = u, es[i].v = v, es[i].diff = !op; n   } n   ufs.init(); n   for (int i = 1; i <= M; i++) { n     if (es[i].diff == false) { n       ufs.link(es[i].u, es[i].v); n     } n   } n   for (int i = 1; i <= M; i++) { n     if (es[i].diff) { n       int uf = ufs.getFa(es[i].u); n       int vf = ufs.getFa(es[i].v); n       G[uf].push_back(vf); n       G[vf].push_back(uf); n     } n   } n   int cnt = 0; n   for (int i = 1; i <= N; i++) n     if (ufs.getFa(i) == i && !col[i]) { n       dfs(i, 1); n       cnt++; n     } n   int ans = power(2, cnt - 1); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 100005; n int n, cnt; n int a[maxn], p[maxn]; n int main() { n   scanf( %d , &n); n   p[1] = 1; n   for (int i = 2; i <= n; i++) n     if (p[i] == 0) { n       p[i] = ++cnt; n       for (int j = i; j <= n; j += i) a[j] = cnt, p[j] = 1; n     } n   for (int i = 2; i <= n; i++) printf( %d%c , a[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int CMax = 5002; n const int CMax2 = 100002; n int m, B[CMax], A[CMax2], ats = 0, sk, pr; n int main() { n   scanf( %d , &m); n   for (int i = 0; i < CMax; i++) { n     B[i] = 0; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d , &sk); n     B[sk]++; n   } n   for (int i = 0; i < CMax; i++) { n     if (B[i] > 0) { n       B[i]--; n       A[ats] = i; n       ats++; n       pr = i; n     } n   } n   for (int i = pr - 1; i >= 0; i--) { n     if (B[i] > 0) { n       B[i]--; n       A[ats] = i; n       ats++; n     } n   } n   printf( %d n , ats); n   if (ats > 0) { n     for (int i = 0; i < ats - 1; i++) { n       printf( %d  , A[i]); n     } n     printf( %d n , A[ats - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, ans; n   string s; n   cin >> s; n   x = s.size(); n   for (int i = 1; i < x; i++) { n     if (s[i] ==  1 ) { n       x++; n       break; n     } n   } n   cout << x / 2 << endl; n   return 0; n } n


#include<bits/stdc++.h> n #define fast {ios_base::sync_with_stdio(false);cin.tie(NULL);} n using namespace std;  n  n const int mxn=2e5+5,mod=1e9+7; n int dp[mxn][20],a[mxn],nxt[mxn],n; n vector<int>prime_factors[mxn]; n  n void sieve(){ n  for(int i=2;i<mxn;i++){ n   if(prime_factors[i].empty()){ n    nxt[i]=mxn; n    for(int j=1;i*j<mxn;j++) n     prime_factors[i*j].push_back(i); n   } n  } n } n  n void lifting(){ n  dp[n+1][0]=n+1; n  n  for(int i=n;i>=1;i--){ n   dp[i][0]=dp[i+1][0]; n   for(auto x:prime_factors[a[i]]){ n    dp[i][0]=min(dp[i][0],nxt[x]); n    nxt[x]=i;  n   } n  } n  for(int i=1;i<=19;i++){ n   for(int j=1;j<=n+1;j++) n    dp[j][i]=dp[dp[j][i-1]][i-1]; n  } n } n  n  n void solve(){ n  int q,i,j; n  cin>>n>>q; n  for(i=1;i<=n;i++) n   cin>>a[i]; n  sieve(); n  lifting(); n  while(q--){ n   int l,r; n   cin>>l>>r; n   int ans=0; n   for(i=19;i>=0;i--){ n    if(dp[l][i]<=r){ n     ans+=1<<i; n     l=dp[l][i]; n    } n   } n   cout<<ans+1<<  n ; n  } n } n  n  n int main(void){ n  fast; n  solve(); n } n


#include <bits/stdc++.h> n using namespace std; n bool is_pal(string a) { n   string b = a; n   reverse(b.begin(), b.end()); n   return b == a; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   ; n   string second; n   cin >> second; n   for (int i = 0; i < second.size(); i++) { n     string temp = second; n     for (char j =  a ; j <=  z ; j++) { n       if (j == second[i]) continue; n       temp[i] = j; n       if (is_pal(temp)) { n         cout <<  YES n ; n         return 0; n       } n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n unordered_map<int, pair<int, int>> resi[50 * 3 + 2]; n struct FLOW { n   int n, *pre, *dist; n   FLOW(int n) : n(n) { n     pre = (int *)malloc((n + 2) * sizeof(int)); n     dist = (int *)malloc((n + 2) * sizeof(int)); n   } n   ~FLOW() { n     free(pre); n     free(dist); n   } n   pair<int, int> SAP() { n     int flow = 0, cost = 0; n     for (; SPFA();) { n       for (int i = n + 1; i != n; i = pre[i]) { n         assert(resi[pre[i]].count(i)); n         resi[pre[i]][i].first -= pre[n]; n         if (resi[i].count(pre[i])) n           resi[i][pre[i]].first += pre[n]; n         else n           resi[i][pre[i]] = make_pair(pre[n], -resi[pre[i]][i].second); n       } n       flow += pre[n]; n       cost += pre[n] * dist[n + 1]; n     } n     return make_pair(flow, cost); n   } n   bool SPFA() { n     int nn = n + 2, q[nn + 1]; n     bool v[nn]; n     int h = 0, l = 1, x; n     for (int i = 0; i < nn; i++) dist[i] = (50 * 50 * 50); n     memset(pre, -1, nn * sizeof(int)); n     memset(v, false, nn * sizeof(bool)); n     dist[n] = 0, pre[n] = -1; n     q[0] = n, v[n] = true; n     while (l != 0) { n       x = q[h]; n       v[q[h]] = false; n       l--; n       h = (h + 1) % nn; n       for (unordered_map<int, pair<int, int>>::const_iterator ix = n                resi[x].begin(); n            ix != resi[x].end(); ix++) { n         if (ix->second.first == 0) continue; n         int i = ix->first, p = ix->second.second; n         if (dist[x] + p < dist[i]) { n           dist[i] = dist[x] + p; n           pre[i] = x; n           if (!v[i]) v[q[(h + l++) % nn] = i] = true; n         } n       } n     } n     if (pre[n + 1] != -1) { n       int flow = (50 * 50 * 50); n       for (int i = n + 1; i != n; i = pre[i]) n         if (resi[pre[i]][i].first < flow) flow = resi[pre[i]][i].first; n       pre[n] = flow; n     } n     return pre[n + 1] != -1; n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> q; n   int arr[n][2], t, l, r, v; n   for (int i = 0; i < n; i++) arr[i][0] = 1, arr[i][1] = n; n   for (int iq = 0; iq < q; iq++) { n     cin >> t >> l >> r >> v; n     if (t == 1) n       for (int i = l - 1; i < r; i++) arr[i][0] = max(arr[i][0], v); n     else if (t == 2) n       for (int i = l - 1; i < r; i++) arr[i][1] = min(arr[i][1], v); n   } n   for (int i = 0; i < n; i++) { n     resi[3 * n][i] = make_pair(1, 0); n     resi[2 * n + i][3 * n + 1] = make_pair(n, 2 * i + 1); n     for (int j = 0; j < n; j++) resi[n + i][2 * n + j] = make_pair(1, 0); n     for (int j = arr[i][0] - 1; j < arr[i][1]; j++) n       resi[i][n + j] = make_pair(1, 0); n   } n   FLOW flow(3 * n); n   pair<int, int> ans = flow.SAP(); n   if (ans.first != n) n     cout << -1 << endl; n   else n     cout << ans.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 500500; n long long i, j, k, n, sum, ch, t, ans, num; n long long a[N], b[N], c[N]; n void R(long long &x) { n   x = 0; n   ch = getchar(); n   while (ch <  0  ||  9  < ch) ch = getchar(); n   while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n } n int main() { n   R(n); n   for (i = 1; i <= n; i++) { n     R(a[i]); n     R(b[i]); n     c[i] = a[i] ^ b[i]; n     sum ^= a[i]; n   } n   k = 60; n   for (i = 1; i <= n; i++) { n     for (j = i + 1; j <= n; j++) n       if (c[i] < c[j]) t = c[i], c[i] = c[j], c[j] = t; n     if (!c[i]) break; n     while (!(c[i] & 1ll << k)) k--; n     for (j = i + 1; j <= n; j++) n       if (c[j] & 1ll << k) c[j] ^= c[i]; n   } n   num = i - 1; n   ans = 1ll << num; n   j = 60; n   for (i = 1; i <= num; i++) { n     while (!(c[i] & 1ll << j)) j--; n     if (sum & 1ll << j) sum ^= c[i]; n   } n   if (sum) n     puts( 1/1 ); n   else n     printf( %I64d/%I64d n , ans - 1, ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   int answer = 0; n   int f[n]; n   for (int i = 0; i < n; i++) cin >> f[i]; n   sort(f, f + n); n   for (int i = n - 1; i >= 0; i -= k) answer += (2 * (f[i] - 1)); n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[102]; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   puts((n % 2 == 1) && (a[0] % 2 == 1) && (a[n - 1] % 2 == 1) ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 100000; n const int N = 1e5 + 5; n const int K = 135; n const int MOD = 1e9 + 7; n int n, first[N], incr[N], x[K]; n long long pw[K][N]; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %d , &x); n     incr[x]++; n   } n   for (int i = 1; i < K; ++i) pw[i][0] = 1; n   for (int i = 0; i < N; ++i) pw[0][i] = 1; n   for (int j = 1; j < N; ++j) n     for (int i = 0; i < K; ++i) pw[i][j] = pw[i][j - 1] * i % MOD; n   for (int i = MX; i >= 1; --i) first[i] = first[i + 1] + incr[i]; n   long long ans = 0; n   for (int i = MX; i >= 1; --i) n     if (first[i]) { n       vector<int> d1, d2, d; n       int dd = 1; n       for (; dd * dd < i; ++dd) n         if (i % dd == 0) { n           d1.push_back(dd); n           d2.push_back(i / dd); n         } n       if (dd * dd == i) d1.push_back(dd); n       reverse(d2.begin(), d2.end()); n       merge(d1.begin(), d1.end(), d2.begin(), d2.end(), back_inserter(d)); n       int prev = 0; n       long long p1 = 1, p2 = 1; n       for (int j = (int)d.size() - 1; j >= 0; --j) { n         x[j + 1] = first[d[j]] - prev; n         prev += x[j + 1]; n         p1 = p1 * pw[j + 1][x[j + 1]] % MOD; n         if (j + 1 == d.size()) n           p2 = p2 * pw[j][x[j + 1]] % MOD; n         else n           p2 = p2 * pw[j + 1][x[j + 1]] % MOD; n       } n       ans += p1 - p2; n       ans %= MOD; n       if (ans < 0) ans += MOD; n     } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   vector<int> z[26]; n   string a, b; n   cin >> a >> b; n   for (int i = 0; i < a.size(); ++i) z[a[i] -  a ].push_back(i); n   bool ok = true; n   for (int i = 0; i < b.size(); ++i) { n     if (z[b[i] -  a ].size() == 0) { n       ok = false; n       break; n     } n   } n   if (!ok) { n     cout << -1 << endl; n   } else { n     int cnt = 1, cur = -1; n     for (int i = 0; i < b.size(); ++i) { n       vector<int> &tmp = z[b[i] -  a ]; n       vector<int>::iterator iter = upper_bound(tmp.begin(), tmp.end(), cur); n       if (iter == tmp.end()) { n         ++cnt; n         cur = tmp[0]; n       } else { n         cur = *iter; n       } n     } n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double a, v, l, d, w, t1, t2, t3, t4, t5, dis1, dis2, dis3, v1; n double chuli(double x) { n   double diss = (v + w) * ((v - w) / a) / 2; n   if (diss >= x) n     return (-2.0 * w + sqrt(4.0 * w * w + 8.0 * a * (x))) / (2.0 * a); n   else n     return (v - w) / a + (x - diss) / v; n } n int main() { n   cin >> a >> v >> l >> d >> w; n   if (v >= w) { n     t1 = w / a; n     dis1 = a * t1 * t1 / 2; n     v1 = a * sqrt(2 * d / a); n     if (dis1 >= l) n       printf( %.12lf n , sqrt(2.0 * l / a)); n     else if (dis1 >= d) n       printf( %.12lf n , t1 + chuli(l - dis1)); n     else { n       dis2 = 2 * (v - w) / a * w + (v - w) * (v - w) / a; n       if (dis1 + dis2 >= d) { n         t2 = 2.0 * (-2.0 * w + sqrt(4.0 * w * w + 4.0 * a * (d - dis1))) / n              (2.0 * a); n         t3 = chuli(l - d); n         printf( %.12lf n , t1 + t2 + t3); n       } else { n         t2 = v / a; n         t3 = (v - w) / a; n         t4 = (d - dis2 - dis1) / v; n         t5 = chuli(l - d); n         printf( %.12lf n , t2 + t3 + t4 + t5); n       } n     } n   } else { n     t1 = v / a; n     dis1 = a * t1 * t1 / 2; n     if (dis1 < l) n       t2 = (l - dis1) / v; n     else { n       t1 = sqrt(2 * l / a); n       t2 = 0.0; n     } n     printf( %.12lf n , t1 + t2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A, B, C, Cans = 214748364, n, r, ansA, ansB; n bool f[1000010]; n int gcd(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n void Work() { n   int t, i, a = A, b = B; n   for (C = 0; A != 1 || B != 1;) { n     if (A > B) { n       f[++C] = 0; n       A -= B; n     } else { n       f[++C] = 1; n       B -= A; n     } n     if (C > n) break; n   } n   f[++C] = 0; n   if (C == n) { n     t = 0; n     for (int i = 2; i <= C; ++i) n       if (f[i] == f[i - 1]) ++t; n     if (t < Cans) Cans = t, ansA = a, ansB = b; n   } n } n void out() { n   if (Cans == 214748364) n     printf( IMPOSSIBLE n ); n   else { n     printf( %d n , Cans); n     A = ansA, B = ansB; n     Work(); n     for (int i = n; i >= 2; --i) printf( %c , f[i] ?  B  :  T ); n     printf( %c n , f[1] ?  B  :  T ); n   } n } n int main() { n   int i; n   scanf( %d%d , &n, &r); n   for (int i = 1; i <= r; ++i) n     if (gcd(i, r) == 1) { n       A = i, B = r; n       Work(); n       A = r, B = i; n       Work(); n     } n   out(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double x, y; n }; n double dist(point P, point Q) { n   double dx = P.x - Q.x, dy = P.y - Q.y; n   return sqrt(dx * dx + dy * dy); n } n vector<point> crossing(point O1, double r1, point O2, double r2) { n   double d = dist(O1, O2); n   double t = (d * d + r1 * r1 - r2 * r2) / 2.0 / d / d; n   double fx = O1.x + (O2.x - O1.x) * t, fy = O1.y + (O2.y - O1.y) * t; n   double h = sqrt(r1 * r1 - t * d * t * d); n   double dx = (O2.y - O1.y) / d * h, dy = (O1.x - O2.x) / d * h; n   vector<point> ans; n   point ans1 = {fx + dx, fy + dy}; n   ans.push_back(ans1); n   point ans2 = {fx - dx, fy - dy}; n   ans.push_back(ans2); n   return ans; n } n vector<double> tri[10]; n void read(int id) { n   point P, Q, R; n   cin >> P.x >> P.y >> Q.x >> Q.y >> R.x >> R.y; n   tri[id].push_back(dist(P, Q)); n   tri[id].push_back(dist(P, R)); n   tri[id].push_back(dist(Q, R)); n   sort(tri[id].begin(), tri[id].end()); n } n bool quad(double a, double b, double c, double d) { n   double x[] = {a, b, c, d}; n   sort(x, x + 4); n   return (x[0] + x[1] + x[2] + 1.0E-9 > x[3]); n } n bool triineq(double a, double b, double c) { n   double x[] = {a, b, c}; n   sort(x, x + 3); n   return (x[0] + x[1] + 1.0E-9 > x[2]); n } n bool equals(double x, double y) { return (x - y < 1.0E-9 && x - y > -1.0E-9); } n int N; n point P[20]; n int a[(1 << 4)], dp[(1 << 4)]; n void dfs(int mask) { n   int i, x, y, z; n   a[mask] = min(a[mask], N); n   for ((i) = 0; (i) < (int)(4); (i)++) n     if (!(mask & (1 << i))) { n       int mask2 = (mask | (1 << i)); n       bool found = false; n       for ((x) = 0; (x) < (int)(N); (x)++) n         for ((y) = 0; (y) < (int)(N); (y)++) n           for ((z) = 0; (z) < (int)(N); (z)++) { n             if (equals(dist(P[x], P[y]), tri[i][0]) && n                 equals(dist(P[x], P[z]), tri[i][1]) && n                 equals(dist(P[y], P[z]), tri[i][2])) { n               dfs(mask2); n               found = true; n             } n           } n       if (!found) n         for ((x) = 0; (x) < (int)(N); (x)++) n           for ((y) = 0; (y) < (int)(N); (y)++) { n             if (equals(dist(P[x], P[y]), tri[i][0])) { n               N++; n               P[N - 1] = crossing(P[x], tri[i][1], P[y], tri[i][2])[0]; n               dfs(mask2); n               P[N - 1] = crossing(P[x], tri[i][1], P[y], tri[i][2])[1]; n               dfs(mask2); n               N--; n             } n             if (equals(dist(P[x], P[y]), tri[i][1])) { n               N++; n               P[N - 1] = crossing(P[x], tri[i][0], P[y], tri[i][2])[0]; n               dfs(mask2); n               P[N - 1] = crossing(P[x], tri[i][0], P[y], tri[i][2])[1]; n               dfs(mask2); n               N--; n             } n             if (equals(dist(P[x], P[y]), tri[i][2])) { n               N++; n               P[N - 1] = crossing(P[x], tri[i][0], P[y], tri[i][1])[0]; n               dfs(mask2); n               P[N - 1] = crossing(P[x], tri[i][0], P[y], tri[i][1])[1]; n               dfs(mask2); n               N--; n             } n           } n     } n } n vector<point> crossed(point O1, double r1, point O2, double r2) { n   vector<point> ans = crossing(O1, r1, O2, r2); n   vector<point> ans2 = crossing(O1, r2, O2, r1); n   ans.push_back(ans2[0]); n   ans.push_back(ans2[1]); n   return ans; n } n void init() { n   int a, b, c, i, j, k, x, y, z; n   for ((i) = 0; (i) < (int)(4); (i)++) { n     N = 3; n     P[0].x = P[0].y = P[1].y = 0.0; n     P[1].x = tri[i][0]; n     P[2] = crossing(P[0], tri[i][1], P[1], tri[i][2])[0]; n     dfs(1 << i); n   } n   for ((a) = 0; (a) < (int)(4); (a)++) n     for ((b) = 0; (b) < (int)(a); (b)++) n       for ((c) = 0; (c) < (int)(b); (c)++) n         for ((i) = 0; (i) < (int)(3); (i)++) n           for ((j) = 0; (j) < (int)(3); (j)++) n             for ((k) = 0; (k) < (int)(3); (k)++) { n               if (triineq(tri[a][i], tri[b][j], tri[c][k])) { n                 N = 6; n                 P[0].x = P[0].y = P[1].y = 0.0; n                 P[1].x = tri[a][i]; n                 P[2] = crossing(P[0], tri[b][j], P[1], tri[c][k])[0]; n                 vector<point> three = crossed(P[0], tri[a][(i + 1) % 3], P[1], n                                               tri[a][(i + 2) % 3]); n                 vector<point> four = crossed(P[0], tri[b][(j + 1) % 3], P[2], n                                              tri[b][(j + 2) % 3]); n                 vector<point> five = crossed(P[1], tri[c][(k + 1) % 3], P[2], n                                              tri[c][(k + 2) % 3]); n                 for ((x) = 0; (x) < (int)(4); (x)++) n                   for ((y) = 0; (y) < (int)(4); (y)++) n                     for ((z) = 0; (z) < (int)(4); (z)++) { n                       P[3] = three[x]; n                       P[4] = four[y]; n                       P[5] = five[z]; n                       dfs((1 << a) | (1 << b) | (1 << c)); n                     } n               } n             } n } n int main() { n   int i, j, k, l; n   for ((i) = 0; (i) < (int)(4); (i)++) read(i); n   int ans = 9; n   for ((i) = 0; (i) < (int)(3); (i)++) n     for ((j) = 0; (j) < (int)(3); (j)++) n       for ((k) = 0; (k) < (int)(3); (k)++) n         for ((l) = 0; (l) < (int)(3); (l)++) n           if (quad(tri[0][i], tri[1][j], tri[2][k], tri[3][l])) ans = 8; n   for ((i) = 0; (i) < (int)((1 << 4)); (i)++) a[i] = (1 << 29); n   a[0] = 0; n   init(); n   for ((i) = 0; (i) < (int)((1 << 4)); (i)++) { n     dp[i] = a[i]; n     for (j = 1; j < i; j++) n       if ((i & j) == j) dp[i] = min(dp[i], dp[j] + a[i ^ j] - 1); n   } n   ans = min(ans, dp[15]); n   cout << ans << endl; n   return 0; n } n