#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   std::vector<long long> x0, x1, y0, y1; n   long long a[n][4]; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3]; n     x0.push_back(a[i][0]); n     x1.push_back(a[i][2] + 1); n     y0.push_back(a[i][1]); n     y1.push_back(a[i][3] + 1); n   } n   sort((x0).begin(), (x0).end()); n   sort((x1).begin(), (x1).end()); n   sort((y0).begin(), (y0).end()); n   sort((y1).begin(), (y1).end()); n   long long lx = x0[n - 1]; n   long long ly = y0[n - 1]; n   long long ux = x1[0]; n   long long uy = y1[0]; n   for (long long int i = 0; i < n; i++) { n     long long xi = a[i][0]; n     long long xf = a[i][2] + 1; n     long long yi = a[i][1]; n     long long yf = a[i][3] + 1; n     long long x00 = lx; n     long long x11 = ux; n     long long y00 = ly; n     long long y11 = uy; n     if (xi == lx) { n       x00 = x0[n - 2]; n     } n     if (xf == ux) { n       x11 = x1[1]; n     } n     if (yi == ly) { n       y00 = y0[n - 2]; n     } n     if (yf == uy) { n       y11 = y1[1]; n     } n     if (x00 < x11 && y00 < y11) { n       cout << x00 <<     << y00; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char x[1001][1001]; n int check(int i, int j) { n   if (x[i][j] ==  . ) { n     for (int a = -1; a <= 1; a++) { n       for (int k = -1; k <= 1; k++) { n         if (i + a >= 0 && i + a < n && j + k >= 0 && j + k < m && n             x[i + a][j + k] ==  * ) { n           return 1; n         } n       } n     } n   } else { n     int cnt = 0; n     for (int a = -1; a <= 1; a++) { n       for (int k = -1; k <= 1; k++) { n         if (i + a >= 0 && i + a < n && j + k >= 0 && j + k < m && n             x[i + a][j + k] ==  * ) { n           cnt++; n         } n       } n     } n     if (cnt != x[i][j] -  0 ) { n       return 1; n     } n   } n   return 0; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     scanf( %s , x[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (x[i][j] !=  *  && check(i, j)) { n         printf( NO n ); n         return 0; n       } n     } n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 100; n int n, m, k, ans; n int num[maxn]; n int main() { n   ios::sync_with_stdio(false); n   while (scanf( %d%d%d , &n, &m, &k) != EOF) { n     ans = 0; n     for (int i = 0; i < n; ++i) { n       scanf( %d , &num[i]); n     } n     ans = num[n - 1] - num[0] + 1; n     for (int i = 0; i < n; ++i) { n       num[i] = num[i + 1] - num[i] - 1; n     } n     sort(num, num + n - 1); n     for (int i = 0; i < k - 1; ++i) { n       ans -= num[n - 2 - i]; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, m; n long long t[300005]; n long long a[300005]; n long long s; n long long res[300005], ans[300005]; n long long cnt[300005]; n long long pre[300005], p[300005], pp[300005]; n struct query { n   long long l; n   long long r; n   long long id; n } q[300005]; n bool cmp(query a, query b) { n   if (a.l / s != b.l / s) return a.l / s < b.l / s; n   return a.r / s < b.r / s; n } n void in() { n   scanf( %lld%lld , &n, &k); n   s = sqrt(n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &t[i]); n   } n   pre[0] = 0; n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     if (t[i] == 1) { n       pre[i] = pre[i - 1] + a[i]; n     } else { n       pre[i] = pre[i - 1] - a[i]; n     } n   } n   set<long long> al; n   map<long long, long long> idx; n   long long cnt = 0; n   for (long long i = 0; i <= n; i++) { n     if (al.empty() || al.find(pre[i] - k) == al.end()) { n       al.insert(pre[i] - k); n       cnt++; n       idx[pre[i] - k] = cnt; n       p[i] = cnt; n     } else { n       p[i] = idx[pre[i] - k]; n     } n     if (al.empty() || al.find(pre[i] + k) == al.end()) { n       al.insert(pre[i] + k); n       cnt++; n       idx[pre[i] + k] = cnt; n       pp[i] = cnt; n     } else { n       pp[i] = idx[pre[i] + k]; n     } n     if (al.empty() || al.find(pre[i]) == al.end()) { n       al.insert(pre[i]); n       cnt++; n       idx[pre[i]] = cnt; n       pre[i] = cnt; n     } else { n       pre[i] = idx[pre[i]]; n     } n   } n   scanf( %lld , &m); n   for (long long i = 1; i <= m; i++) { n     scanf( %lld%lld , &q[i].l, &q[i].r); n     q[i].id = i; n   } n } n void solve() { n   sort(q + 1, q + m + 1, cmp); n   long long l = 1, r = 0; n   long long rf = 0; n   for (long long i = 1; i <= m; i++) { n     long long cur = 0; n     while (l < q[i].l) { n       cnt[pre[l]]--; n       cur -= cnt[pp[l]]; n       l++; n     } n     while (l > q[i].l) { n       l--; n       cur += cnt[pp[l]]; n       cnt[pre[l]]++; n     } n     while (r < q[i].r) { n       r++; n       cur += cnt[p[r]]; n       cnt[pre[r]]++; n     } n     while (r > q[i].r) { n       cnt[pre[r]]--; n       cur -= cnt[p[r]]; n       r--; n     } n     rf += cur; n     res[q[i].id] = rf + cnt[pp[l - 1]]; n   } n } n void ou() { n   for (long long i = 1; i <= m; i++) { n     printf( %lld n , res[i]); n   } n } n signed main() { n   in(); n   solve(); n   ou(); n } n


#include <bits/stdc++.h> n using namespace std; n int64_t b, s, c; n int64_t pr[3]; n int64_t m; n int64_t req[] = {0, 0, 0}; n bool binary_search(int64_t mid) { n   int64_t to[3]; n   for (int64_t i = 0; i < 3; i++) { n     to[i] = req[i] * mid; n   } n   to[0] -= b; n   to[1] -= s; n   to[2] -= c; n   int64_t cost = 0; n   for (int64_t i = 0; i < 3; i++) n     if (to[i] > 0) cost += to[i] * pr[i]; n   if (cost <= m) return true; n   return false; n } n signed main() { n   string ss; n   cin >> ss; n   for (char i : ss) { n     if (i ==  B ) n       req[0]++; n     else if (i ==  S ) n       req[1]++; n     else n       req[2]++; n   } n   cin >> b >> s >> c; n   for (int64_t i = 0; i < 3; i++) cin >> pr[i]; n   cin >> m; n   int64_t st = 0; n   int64_t en = 1e14; n   int64_t ans = 0; n   while (st <= en) { n     int64_t mid = st + (en - st) / 2; n     if (binary_search(mid)) { n       ans = mid; n       st = mid + 1; n     } else { n       en = mid - 1; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int mod = 1000000007; n const int inf = 0x3f3f3f3f; n const int maxn = 200005; n void task() { n   int r1, c1, r2, c2; n   cin >> r1 >> c1 >> r2 >> c2; n   int Rook = 0, Bishop = 0, King = 0; n   if (r1 == r2 or c1 == c2) n     Rook = 1; n   else n     Rook = 2; n   if ((r1 + c1) == (r2 + c2) or (r2 - r1) == (c2 - c1)) n     Bishop = 1; n   else if ((r1 + c1) % 2 == (r2 + c2) % 2) n     Bishop = 2; n   else n     Bishop = 0; n   King = max(abs(r1 - r2), abs(c1 - c2)); n   cout << Rook <<     << Bishop <<     << King <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T = 1; n   while (T--) { n     task(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans = 0; n int m; n void dfs(int parent, int s, int cnt, vector<int> &cats, n          vector<vector<int> > &adj) { n   if (cats[s] == 1) n     cnt++; n   else n     cnt = 0; n   if (cnt > m) return; n   if (s != 1 && adj[s].size() == 1) { n     ans++; n     return; n   } n   for (int i : adj[s]) { n     if (i != parent) dfs(s, i, cnt, cats, adj); n   } n } n int main() { n   int n; n   int x, y; n   cin >> n >> m; n   vector<int> cats(n + 1); n   for (int i = 1; i <= n; i++) cin >> cats[i]; n   vector<vector<int> > adj(n + 1); n   for (int i = 0; i < n - 1; i++) { n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   dfs(0, 1, 0, cats, adj); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[1000006], ans[1000006]; n bool b[1000006]; n stack<pair<int, int> > s1, s2; n int main() { n   int i, j; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   scanf( %d , &m); n   for (i = 1; i <= m; i++) { n     scanf( %d , &j); n     b[j] = true; n   } n   for (i = 1; i <= n; i++) { n     if (b[i]) { n       while (s1.size()) { n         if (s1.top().first == a[i] && s2.size() == 0) { n           break; n         } else if (s2.size() && s1.top().first == s2.top().first) { n           ans[s1.top().second] = 1; n           ans[s2.top().second] = -1; n           s1.pop(); n           s2.pop(); n         } else { n           s2.push(s1.top()); n           s1.pop(); n         } n       } n       if (s1.size() == 0) return cout <<  NO  << endl, 0; n       ans[s1.top().second] = 1; n       ans[i] = -1; n       s1.pop(); n     } else { n       s1.push({a[i], i}); n     } n   } n   while (s1.size()) { n     if (s2.size() == 0) { n       s2.push(s1.top()); n       s1.pop(); n     } else if (s1.top().first == s2.top().first) { n       ans[s1.top().second] = 1; n       ans[s2.top().second] = -1; n       s1.pop(); n       s2.pop(); n     } else { n       s2.push(s1.top()); n       s1.pop(); n     } n   } n   if (s2.size()) return cout <<  NO  << endl, 0; n   printf( YES n ); n   for (i = 1; i <= n; i++) printf( %d  , a[i] * ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e2, W = 10; n long long dp[N + 5][N + 5][W + 5][W + 5]; n int n1, n2, k1, k2; n int main() { n   memset(dp, -1, sizeof(dp)); n   cin >> n1 >> n2 >> k1 >> k2; n   for (int i = 0; i <= n1; i++) { n     for (int j = 0; j <= n2; j++) { n       for (int k = 0; k <= k1; k++) { n         for (int l = 0; l <= k2; l++) { n           long long ans = 0; n           if ((i + j) == 0) { n             ans = 1; n           } else { n             ans = 0; n             if (i > 0 && k > 0) n               ans = (ans + dp[i - 1][j][k - 1][k2]) % 100000000; n             if (j > 0 && l > 0) n               ans = (ans + dp[i][j - 1][k1][l - 1]) % 100000000; n           } n           dp[i][j][k][l] = ans; n         } n       } n     } n   } n   cout << dp[n1][n2][k1][k2] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long EPS = 1000000007; n const int INF = 1000000000; n double PI = 3.14159265358979323846; n const int MAXN = 1000040; n long long abss(long long h) { return max(h, -h); } n double fabss(double h) { return max(h, -h); } n long long ceill(long long x, long long y) { n   if (x % y != 0) return (x / y) + 1; n   return x / y; n } n string itos(long long num) { n   string str =   ; n   if (num == 0) return  0 ; n   while (num != 0) { n     str += ((num % 10) +  0 ); n     num /= 10; n   } n   reverse(str.begin(), str.end()); n   return str; n } n long long sstoi(string num) { n   long long count = 1; n   for (int i = 0; i < (int)num.size() - 1; i++) { n     count *= 10; n   } n   long long ans = 0; n   for (int i = 0; i < (int)num.size(); i++) { n     ans += (num[i] -  0 ) * count; n     count /= 10; n   } n   return ans; n } n long long n, m, k, h; n pair<char, int> a[MAXN]; n int b[MAXN]; n int c[MAXN]; n pair<char, int> t[MAXN]; n void build(int v, int tl, int tr) { n   if (tl == tr) { n     t[v].first = a[tl].first; n     t[v].second = a[tl].second; n   } else { n     int tm = (tl + tr) / 2; n     build(v * 2, tl, tm); n     build(v * 2 + 1, tm + 1, tr); n     if (t[v * 2].first > t[v * 2 + 1].first) { n       t[v] = t[v * 2 + 1]; n     } else if (t[v * 2].first < t[v * 2 + 1].first) { n       t[v] = t[v * 2]; n     } else if (t[v * 2].first == t[v * 2 + 1].first && n                t[v * 2].second > t[v * 2 + 1].second) { n       t[v] = t[v * 2]; n     } else { n       t[v] = t[v * 2 + 1]; n     } n   } n } n pair<char, int> sum(int v, int tl, int tr, int l, int r) { n   if (l > r) return make_pair( z , -1); n   if (l == tl && r == tr) return t[v]; n   int tm = (tl + tr) / 2; n   pair<char, int> q1 = sum(v * 2, tl, tm, l, min(r, tm)); n   pair<char, int> q2 = sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); n   if (q1.first > q2.first) { n     return q2; n   } else if (q1.first < q2.first) { n     return q1; n   } else if (q1.first == q2.first && q1.second > q2.second) { n     return q1; n   } else { n     return q2; n   } n } n int counter[30]; n int main() { n   int m; n   cin >> m; n   string str; n   cin >> str; n   int n = str.size(); n   for (int i = 0; i < n; i++) { n     a[i].first = str[i]; n     a[i].second = i; n     counter[str[i] -  a ]++; n   } n   build(1, 0, n - 1); n   int index = 0; n   vector<char> answer; n   char maxChar =  a ; n   while (true) { n     pair<char, int> w = sum(1, 0, n - 1, index, index + m - 1); n     answer.push_back(w.first); n     maxChar = max(maxChar, w.first); n     index = w.second; n     if (index + m - 1 >= n - 1) { n       break; n     } n     index++; n   } n   sort(answer.begin(), answer.end()); n   for (char i =  a ; i <=  z ; i++) { n     if (i != maxChar) { n       for (int j = 0; j < counter[i -  a ]; j++) { n         cout << i; n       } n     } else { n       for (int j = 0; j < answer.size(); j++) { n         if (answer[j] == maxChar) { n           cout << maxChar; n         } n       } n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   return (b % a, a); n } n int binarysearch(int a[], int ele, int n) { n   int beg = 0, end = n, mid, ans; n   while (beg <= end) { n     mid = beg + (end - beg) / 2; n     if (a[mid] > ele) { n       ans = mid; n       end = mid - 1; n     } else n       beg = mid + 1; n   } n   return ans; n } n bool checkpallin(string a) { n   int i = 0, j = a.length() - 1; n   while (i < j) { n     if (a[i] != a[j]) return false; n     i++, j--; n   } n   return true; n } n bool comp(string a, string b) { return a.length() < b.length(); } n int main(void) { n   int n, a; n   cin >> n >> a; n   if (a & 1) n     cout << 1 + a / 2; n   else n     cout << 1 + (n - a) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n int dis[N][N]; n int vis[N], arr[N]; n long long ans[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       cin >> dis[i][j]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     cin >> arr[i]; n   } n   for (int i = n; i > 0; --i) { n     int x = arr[i]; n     vis[x] = 1; n     for (int j = 1; j <= n; ++j) { n       for (int k = 1; k <= n; ++k) { n         dis[j][k] = min(dis[j][k], dis[j][x] + dis[x][k]); n         if (vis[j] && vis[k]) ans[i] += dis[j][k]; n       } n     } n   } n   for (int i = 1; i <= n; ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200 * 1000 + 20; n int p[N], q[N], n, ans[N], cnt[N], seg[N << 2]; n void build(int id = 1, int st = 0, int en = n) { n   if (en - st == 1) { n     seg[id] = 1; n     return; n   } n   int mid = st + en >> 1; n   build(id << 1, st, mid); n   build(id << 1 | 1, mid, en); n   seg[id] = seg[id << 1] + seg[id << 1 | 1]; n } n void update(int idx, int id = 1, int st = 0, int en = n) { n   if (en - st == 1) { n     seg[id] = 0; n     return; n   } n   int mid = st + en >> 1; n   if (idx < mid) n     update(idx, id << 1, st, mid); n   else n     update(idx, id << 1 | 1, mid, en); n   seg[id] = seg[id << 1] + seg[id << 1 | 1]; n } n int get(int l, int r, int id = 1, int st = 0, int en = n) { n   if (l <= st && en <= r) return seg[id]; n   if (r <= st || en <= l) return 0; n   int mid = st + en >> 1; n   return get(l, r, id << 1, st, mid) + get(l, r, id << 1 | 1, mid, en); n } n int get2(int val, int id = 1, int st = 0, int en = n) { n   if (en - st == 1) return st; n   int mid = st + en >> 1; n   if (val < seg[id << 1]) n     return get2(val, id << 1, st, mid); n   else n     return get2(val - seg[id << 1], id << 1 | 1, mid, en); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> p[i]; n   for (int i = 0; i < n; i++) cin >> q[i]; n   build(); n   for (int i = 0; i < n; i++) { n     cnt[n - i - 1] = get(0, p[i]); n     update(p[i]); n   } n   build(); n   for (int i = 0; i < n; i++) { n     cnt[n - i - 1] += get(0, q[i]); n     update(q[i]); n   } n   for (int i = 0; i < n; i++) { n     cnt[i + 1] += cnt[i] / (i + 1); n     cnt[i] %= i + 1; n   } n   build(); n   for (int i = n - 1; ~i; i--) { n     ans[n - i - 1] = get2(cnt[i]); n     update(ans[n - i - 1]); n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return cout <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long s = 0, w = 1; n   register char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); n   return s * w; n } n void print(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) print(x / 10); n   putchar(x % 10 +  0 ); n } n long long n, c, x, y, res = 1, now = 1; n signed main() { n   n = read(), x = read(), y = read(), c = read(); n   if (c == 1) { n     puts( 0 ); n     return 0; n   } n   for (; c > res; now++) { n     long long L = min(now, y - 1); n     if (L == now) res--; n     res += max(0ll, L - max(now - x + 1, 1ll) + 1) + n            max(0ll, L - max(x + now - n, 1ll) + 1); n     long long R = min(now, n - y); n     if (R == now) res--; n     res += max(0ll, R - max(now - x + 1, 0ll) + 1) + n            max(0ll, R - max(x + now - n, 0ll) + 1); n   } n   printf( %lld n , --now); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int rdi() { n   int d; n   scanf( %d , &d); n   return d; n } n inline char rdc() { n   scanf(   ); n   return getchar(); n } n inline string rds() { n   string s; n   cin >> s; n   return s; n } n inline double rddb() { n   double d; n   scanf( %lf , &d); n   return d; n } n template <class T> n inline bool setmin(T& a, T b) { n   return a > b ? a = b, true : false; n } n template <class T> n inline bool setmax(T& a, T b) { n   return a < b ? a = b, true : false; n } n struct debugger { n   template <typename T> n   debugger& operator,(const T& x) { n     cerr << x <<  , ; n     return *this; n   } n } dbgr; n const int N = 131072, M = 128; n char s[N]; n int n, c[3], cnt[N][3]; n int Abs(int x) { return x < 0 ? -x : +x; } n int main() { n   scanf( %s , s + 1), n = strlen(s + 1); n   for (int i = (1); i <= (n); ++i) n     memcpy(cnt[i], cnt[i - 1], sizeof cnt[i]), ++cnt[i][s[i] -  x ]; n   for (int repp_b = (rdi()), repp = 0; repp < repp_b; ++repp) { n     int l = rdi(), r = rdi(); n     for (int i = (0); i < (3); ++i) c[i] = cnt[r][i] - cnt[l - 1][i]; n     int mx = *max_element(c, c + 3); n     int mn = *min_element(c, c + 3); n     puts(r - l + 1 < 3 || mx - mn <= 1 ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[4]; n int y[4]; n int newx[4]; n int newy[4]; n int T; n int get() { n   int answ = INT_MAX; n   for (auto y1 : y) n     for (auto y2 : y) { n       int delta = abs(y1 - y2); n       if (delta == 0) continue; n       set<int> s; n       for (auto x1 : x) n         for (auto x2 : x) n           for (auto d : {0, -delta, -2 * delta, 1, -delta + 1, -2 * delta + 1}) n             s.insert((x1 + x2 + d) / 2); n       for (auto x1 : s) { n         int p[4] = {3, 2, 1, 0}; n         for (int ori = 0; ori < 24; ori++) { n           next_permutation(p, p + 4); n           int xs[4] = {x1, x1, x1 + delta, x1 + delta}; n           int ys[4] = {y1, y2, y1, y2}; n           bool flag = false; n           for (int i = 0; i < 4; i++) n             if (xs[i] != x[p[i]] && ys[i] != y[p[i]]) { n               flag = true; n             } n           if (flag) continue; n           int t = 0; n           for (int i = 0; i < 4; i++) n             t = max(t, abs(xs[i] - x[p[i]]) + abs(ys[i] - y[p[i]])); n           if (answ > t) { n             answ = t; n             for (int i = 0; i < 4; i++) { n               newx[p[i]] = xs[i]; n               newy[p[i]] = ys[i]; n             } n           } n         } n       } n     } n   return answ; n } n int boom = 1e9; n int main() { n   scanf( %d , &T); n   while (T--) { n     for (int i = 0; i < 4; i++) { n       scanf( %d%d , &x[i], &y[i]); n       x[i] = x[i] + boom; n       y[i] = y[i] + boom; n     } n     int t1 = get(); n     swap(x, y); n     swap(newx, newy); n     int t2 = get(); n     swap(x, y); n     swap(newx, newy); n     if (t1 < t2) get(); n     if (min(t1, t2) == INT_MAX) { n       puts( -1 ); n     } else { n       printf( %d n , min(t1, t2)); n       for (int i = 0; i < 4; i++) n         printf( %d %d n , newx[i] - boom, newy[i] - boom); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<char> sequ; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   long long num; n   cin >> n; n   vector<long long> a(n); n   vector<long long> b(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n   } n   if (n == 1 && a[0] == b[0]) { n     cout <<  SMALL n0 n ; n     return 0; n   } n   if (n == 1) { n     cout <<  IMPOSSIBLE n ; n     return 0; n   } n   long long pep_op = 0, rev_op = 0; n   if (n == 2) { n     long long a0 = a[0], a1 = a[1], b0 = b[0], b1 = b[1]; n     if (a0 > a1) { n       swap(a0, a1); n     } n     while (1) { n       if ((a0 == b0 && a1 == b1) || (a0 == b1 && a1 == b0)) { n         if (a[0] != a0) { n           rev_op++; n           sequ.push_back( R ); n         } n         if (pep_op > 200000) n           cout <<  BIG n  << pep_op <<   n ; n         else { n           cout <<  SMALL n  << pep_op + rev_op <<   n ; n           int sz = sequ.size(); n           for (int i = sz - 1; i >= 0; i--) cout << sequ[i]; n           cout <<   n ; n         } n         return 0; n       } n       if (b0 > b1) { n         swap(b0, b1); n         sequ.push_back( R ); n         rev_op++; n       } n       if (a0 > b0 || a1 > b1) { n         cout <<  IMPOSSIBLE n ; n         return 0; n       } n       long long temp = (b1 - max(b0, a1)) % b0; n       long long pref = (b1 - max(b0, a1)) / b0; n       pep_op = pref + pep_op; n       if (pep_op <= 200000) { n         while (pref--) sequ.push_back( P ); n       } n       b1 = temp + max(b0, a1); n       if (b0 + b1 > a0 + a1) { n         long long temp1 = b0; n         b0 = b1 - b0; n         b1 = temp1; n         sequ.push_back( P ); n         sequ.push_back( R ); n         pep_op++; n         rev_op++; n       } n     } n     return 0; n   } n   while (b[0] + b[n - 1] > a[0] + a[n - 1]) { n     if (b[1] != b[0]) { n       bool order = true; n       if (b[1] > b[0]) { n         for (int i = 1; i < n; i++) { n           if (b[i] <= b[i - 1]) { n             order = false; n             break; n           } n         } n       } else if (b[0] > b[1]) { n         reverse(b.begin(), b.end()); n         for (int i = 1; i < n; i++) { n           if (b[i] <= b[i - 1]) { n             order = false; n             break; n           } n         } n         if (order == true) { n           sequ.push_back( R ); n           rev_op++; n         } n       } n       if (order == true) { n         for (int i = n - 1; i > 0; i--) b[i] = b[i] - b[i - 1]; n         sequ.push_back( P ); n         pep_op++; n       } else n         break; n     } else n       break; n   } n   int count = 0; n   int rcount = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] == b[i]) count++; n   } n   for (int i = 0, j = n - 1; i < n; i++, j--) { n     if (a[i] == b[j]) rcount++; n   } n   if (rcount == n && count != n) { n     sequ.push_back( R ); n     rev_op++; n   } n   if (count == n || rcount == n) { n     if (pep_op <= 200000) { n       cout <<  SMALL n  << pep_op + rev_op <<   n ; n       int sz = sequ.size(); n       for (int i = sz - 1; i >= 0; i--) cout << sequ[i]; n       cout <<   n ; n     } else n       cout <<  BIG n  << pep_op <<   n ; n   } else { n     cout <<  IMPOSSIBLE n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   int a; n   scanf( %d%d , &n, &x); n   int sum = 0; n   while (n--) { n     scanf( %d , &a); n     sum += a; n   } n   int count = 0; n   if (sum == 0) { n   } else { n     if (abs(sum) <= x) { n       count = 1; n     } else { n       count = abs(sum) / abs(x); n       if (abs(sum) % abs(x) != 0) count++; n     } n   } n   cout << count << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int mp[27][27]; n int dp[111][111][26]; n bool flag[111][111][26]; n char s[111]; n inline void checkmax(int &a, int b) { n   if (a < b) a = b; n } n int main() { n   int i, j, k, n, m; n   scanf( %s , s); n   scanf( %d , &k); n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     getchar(); n     char ch1, ch2; n     scanf( %c , &ch1); n     getchar(); n     scanf( %c%d , &ch2, &m); n     mp[ch1 -  a ][ch2 -  a ] = m; n   } n   memset(flag, 0, sizeof(flag)); n   for (i = 0; i < 26; i++) { n     if (s[0] -  a  == i) { n       dp[0][0][i] = 0; n       flag[0][0][i] = true; n     } else if (k > 0) { n       dp[0][1][i] = 0; n       flag[0][1][i] = true; n     } n   } n   for (i = 0; i < strlen(s) - 1; i++) { n     for (j = 0; j <= k; j++) n       for (int x = 0; x < 26; x++) { n         if (!flag[i][j][x]) continue; n         for (int y = 0; y < 26; y++) n           if (s[i + 1] -  a  == y) { n             if (!flag[i + 1][j][y]) dp[i + 1][j][y] = dp[i][j][x] + mp[x][y]; n             flag[i + 1][j][y] = true; n             checkmax(dp[i + 1][j][y], dp[i][j][x] + mp[x][y]); n           } else if (j < k) { n             if (!flag[i + 1][j + 1][y]) n               dp[i + 1][j + 1][y] = dp[i][j][x] + mp[x][y]; n             flag[i + 1][j + 1][y] = true; n             checkmax(dp[i + 1][j + 1][y], dp[i][j][x] + mp[x][y]); n           } n       } n   } n   m = -999999999; n   for (i = 0; i <= k; i++) { n     for (j = 0; j < 26; j++) { n       if (flag[strlen(s) - 1][i][j]) checkmax(m, dp[strlen(s) - 1][i][j]); n     } n   } n   cout << m << endl; n } n


#include <bits/stdc++.h> n int main() { n   static char xx[100 + 1], yy[100 + 1]; n   int n, i; n   scanf( %s%s , xx, yy); n   n = strlen(xx); n   for (i = 0; i < n; i++) n     if (xx[i] < yy[i]) { n       printf( -1 n ); n       return 0; n     } n   printf( %s n , yy); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100002]; n int main() { n   int i, n, ans = 0, mm = 0; n   cin >> n; n   n = n << 1; n   for (i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     ans = ans - arr[a]; n     arr[a] = arr[a] ^ 1; n     ans += arr[a]; n     mm = max(mm, ans); n   } n   cout << mm << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n int main() { n   int T = read(); n   while (T--) { n     int n = read(), a = read(), flag = 0, k; n     for (int i = 2; i <= n; i++) { n       int b = read(); n       if (abs(a - b) >= 2) { n         k = i; n         flag = 1; n       } n       a = b; n     } n     if (flag) n       cout <<  YES n  << k - 1 <<     << k << endl; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool foiare[1024], cand[1024], foivert[1024]; n int n, a[32]; n int main() { n   scanf( %d , &n); n   memset(foiare, false, sizeof(foiare)); n   memset(foivert, false, sizeof(foivert)); n   for (int i = 0; i < n; i++) { n     for (int j = 1; j <= 1000; j++) n       if (!foivert[j]) { n         bool podej = true; n         memset(cand, false, sizeof(cand)); n         for (int k = 0; k < i; k++) n           if (foiare[a[k] + j] or a[k] + j > 1000) { n             podej = false; n             break; n           } else if (cand[a[k] + j]) { n             podej = false; n             break; n           } else n             cand[a[k] + j] = true; n         if (podej) { n           a[i] = j; n           foivert[j] = true; n           for (int k = 0; k < i; k++) foiare[a[k] + a[i]] = true; n           break; n         } n       } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (j) printf(   ); n       if (i == j) n         printf( 0 ); n       else n         printf( %d , a[i] + a[j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m; n long double p[3010][310], delta[310], f[310][3010], ans = 0, lastf[3010]; n int num[310]; n void solve(int cur) { n   num[cur]++; n   if (num[cur] >= n) { n     delta[cur] = 0; n     return; n   } n   memcpy(lastf, f[cur], sizeof(f[cur])); n   f[cur][0] = 0; n   for (int i = 1; i <= n; i++) n     f[cur][i] = f[cur][i - 1] * (1 - p[i][cur]) + lastf[i - 1] * p[i][cur]; n   delta[cur] -= f[cur][n]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %Lf , &p[i][j]); n       p[i][j] /= 1000; n     } n   } n   for (int i = 1; i <= m; i++) { n     f[i][0] = 1; n     for (int j = 1; j <= n; j++) f[i][j] = f[i][j - 1] * (1 - p[j][i]); n     delta[i] = 1 - f[i][n]; n   } n   for (int i = 1; i <= n; i++) { n     long double mx = 0; n     int k = 0; n     for (int j = 1; j <= m; j++) { n       if (delta[j] > mx) mx = delta[j], k = j; n     } n     ans += mx; n     if (!k) break; n     solve(k); n   } n   printf( %.10Lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10000], b[10000]; n void mergesort(int *a, int *b, int n) { n   int i0, i1, iRight, iEnd, width, i, j; n   for (width = 1; width < n; width <<= 1) n     for (i = 0; i < n; i += (width << 1)) { n       i0 = i; n       i1 = iRight = min(i + width, n); n       iEnd = min(i + (width << 1), n); n       for (j = i; j < iEnd; j++) n         if (i0 < iRight && (a[i0] <= a[i1] || i1 == iEnd)) n           b[j] = a[i0++]; n         else n           b[j] = a[i1++]; n       for (j = i; j < iEnd; j++) a[j] = b[j]; n     } n } n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   int i; n   for (i = 0; i < n; i++) cin >> a[i]; n   mergesort(a, b, n); n   int count = 0; n   i = 0; n   while (count < y) { n     i++; n     count++; n   } n   if (a[i] == a[i - 1]) n     cout <<  0 ; n   else n     cout << a[i] - a[i - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Test() { n   long long n; n   cin >> n; n   string s[n]; n   for (long long i = 0; i < n; i++) { n     cin >> s[i]; n   } n   long long ans = 0; n   for (char i =  a ; i <=  e ; i++) { n     vector<pair<long long, long long> > v; n     for (long long j = 0; j < n; j++) { n       long long cnti = 0, len = s[j].size(); n       for (long long k = 0; k < len; k++) { n         if (s[j][k] == i) { n           ++cnti; n         } n       } n       v.push_back(make_pair(2 * cnti - len, cnti)); n     } n     sort(v.begin(), v.end()); n     long long curr = 0, oth = 0, temp = 0; n     for (long long j = n - 1; j >= 0; j--) { n       long long length = ((2 * v[j].second) - v[j].first); n       long long x = v[j].second; n       long long y = (length - v[j].second); n       if ((curr + x) > (oth + y)) { n         curr += x; n         oth += y; n         ++temp; n       } n     } n     ans = max(ans, temp); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout << fixed << setprecision(10); n   long long tests; n   cin >> tests; n   for (long long tc = 1; tc <= tests; tc++) Test(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n const int MAXM = 4e5 + 5; n template <typename T> n void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= f; n } n int n, m, q, res[MAXN]; n int tot, point[MAXN][2]; n int x[MAXN], y[MAXN]; n int f[MAXM], s[MAXM]; n int find(int x) { n   if (f[x] == x) n     return x; n   else n     return find(f[x]); n } n void work(int xl, int xr, int yl, int yr) { n   if (xl > xr) return; n   vector<pair<int, pair<int, int>>> rev; n   auto merge = [&](int x, int y) { n     x = find(x), y = find(y); n     if (x == y) return; n     if (s[x] < s[y]) swap(x, y); n     rev.emplace_back(x, make_pair(f[x], s[x])); n     rev.emplace_back(y, make_pair(f[y], s[y])); n     s[x] += s[y], f[y] = x; n     return; n   }; n   auto addedge = [&](int x, int y) { n     merge(point[x][0], point[y][1]); n     merge(point[x][1], point[y][0]); n     return; n   }; n   auto check = [&](int x, int y) { n     return find(point[x][0]) != find(point[y][0]); n   }; n   auto clear = [&]() { n     reverse(rev.begin(), rev.end()); n     for (auto x : rev) { n       f[x.first] = x.second.first; n       s[x.first] = x.second.second; n     } n     rev.clear(); n     return; n   }; n   if (yl == yr) { n     for (int i = xl; i <= xr; i++) res[i] = yl; n     return; n   } n   bool fail = false; n   int mid = (yl + yr + 1) / 2, divp = 0; n   for (int i = max(yl, 1); i < mid && !fail; i++) { n     fail |= !check(x[m - i + 1], y[m - i + 1]); n     if (!fail) addedge(x[m - i + 1], y[m - i + 1]); n   } n   if (fail) { n     clear(); n     work(xl, xr, yl, mid - 1); n     return; n   } n   divp = xl; n   for (int i = xl; i <= xr - 1; i++) { n     if (check(x[i], y[i])) { n       divp++; n       addedge(x[i], y[i]); n     } else n       break; n   } n   clear(); n   for (int i = max(yl, 1); i < mid; i++) addedge(x[m - i + 1], y[m - i + 1]); n   work(xl, divp, mid, yr); n   clear(); n   fail = false; n   for (int i = xl; i <= divp; i++) { n     if (!check(x[i], y[i])) { n       fail = true; n       break; n     } n     addedge(x[i], y[i]); n   } n   if (!fail) work(divp + 1, xr, yl, mid - 1); n   clear(); n } n int main() { n   read(n), read(m), read(q); n   for (int i = 1; i <= m; i++) read(x[i]), read(y[i]); n   for (int i = 1; i <= n; i++) { n     point[i][0] = ++tot; n     f[tot] = tot, s[tot] = 1; n     point[i][1] = ++tot; n     f[tot] = tot, s[tot] = 1; n   } n   work(1, m, 0, m); n   for (int i = 1; i <= q; i++) { n     int l, r; n     read(l), read(r); n     if (res[l] >= m - r + 1) n       puts( NO ); n     else n       puts( YES ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int p, q, l, r; n   scanf( %d %d %d %d , &p, &q, &l, &r); n   std::vector<std::pair<int, int> > fixed; n   std::vector<std::pair<int, int> > changing; n   for (int k = 0; k < p; k++) { n     int a, b; n     scanf( %d %d , &a, &b); n     fixed.push_back(std::pair<int, int>(a, b)); n   } n   for (int k = 0; k < q; k++) { n     int a, b; n     scanf( %d %d , &a, &b); n     changing.push_back(std::pair<int, int>(a, b)); n   } n   bool done; n   int output(0); n   for (int k = l; k <= r; k++) { n     done = 0; n     for (int x = 0; x < fixed.size(); x++) { n       if (done) { n         continue; n       } n       for (int y = 0; y < changing.size(); y++) { n         if ((fixed[x].second < k + changing[y].first) || n             (k + changing[y].second < fixed[x].first)) { n           continue; n         } else { n           done = 1; n           ++output; n           break; n         } n       } n     } n   } n   printf( %d n , output); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int mod = 1000000007; n const int inf = 0x3f3f3f3f; n const int maxn = 200005; n void task() { n   int n; n   cin >> n; n   vector<int> row(n, 0), coulmn(n, 0); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       int a; n       cin >> a; n       row[i] += a; n       coulmn[j] += a; n     } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (coulmn[j] > row[i]) ans++; n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T = 1; n   while (T--) { n     task(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   cin >> k; n   int a[k + 1]; n   for (int i = 0; i < k; i++) cin >> a[i]; n   long long int comb[1005][1005]; n   comb[0][0] = 1; n   for (int i = 1; i < 1005; i++) { n     comb[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % 1000000007; n     } n   } n   long long sum = 0, ans = 1; n   for (int i = 0; i < k; i++) { n     ans = (ans * comb[sum + a[i] - 1][a[i] - 1]) % 1000000007; n     sum += a[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 50005, M = 50005; n int mo; n struct matrix { n   int a[2][2]; n   int *operator[](const int i) { return a[i]; } n   const int *operator[](const int i) const { return a[i]; } n   friend matrix operator*(const matrix &a, const matrix &b) { n     matrix c; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) { n         long long s = 0; n         for (int k = 0; k < 2; k++) s += (long long)a[i][k] * b[k][j]; n         c[i][j] = s % mo; n       } n     return c; n   } n }; n int n, m; n long long k; n int s[N]; n pair<long long, int> a[M]; n inline int gs(long long i) { n   pair<long long, int> *it = lower_bound(a, a + m, pair<long long, int>(i, 0)); n   if (it < a + m && it->first == i) return it->second; n   return s[i % n]; n } n matrix t[N * 4]; n void build(int cur, int beg, int end) { n   if (end - beg == 1) { n     t[cur][1][0] = 1 % mo; n     t[cur][0][1] = s[beg]; n     t[cur][1][1] = s[(beg + 1) % n]; n     return; n   } n   int mid = beg + end >> 1; n   build(cur << 1, beg, mid); n   build(cur << 1 | 1, mid, end); n   t[cur] = t[cur << 1] * t[cur << 1 | 1]; n } n matrix ans; n void query(int cur, int beg, int end, int L, int R) { n   if (end <= L || beg >= R) return; n   if (beg >= L && end <= R) { n     ans = ans * t[cur]; n     return; n   } n   int mid = beg + end >> 1; n   query(cur << 1, beg, mid, L, R); n   query(cur << 1 | 1, mid, end, L, R); n } n long long p[M * 2], *top = p; n long long q[M * 2], *toq = q; n void power(long long s) { n   matrix a = t[1]; n   for (; s; s >>= 1, a = a * a) n     if (s & 1) ans = ans * a; n } n int main() { n   scanf( %I64d%d , &k, &mo); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &s[i]), s[i] %= mo; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) n     scanf( %I64d%d , &a[i].first, &a[i].second), a[i].second %= mo; n   sort(a, a + m); n   for (int i = 0; i < m; i++) n     p[i << 1] = a[i].first - 1, p[i << 1 | 1] = a[i].first; n   p[m * 2] = k; n   sort(p, p + m * 2 + 1); n   top = unique(p, p + m * 2 + 1); n   for (int i = 0; i < top - p; i++) q[i] = p[i] / n; n   toq = unique(q, q + (top - p)); n   build(1, 0, n); n   ans[0][0] = ans[1][1] = 1 % mo; n   ans[0][1] = ans[1][0] = 0; n   long long block = 0; n   for (int i = 0, j = 0; i < toq - q; i++) { n     power(q[i] - block); n     block = q[i]; n     long long id = block * n; n     for (; j < top - p && p[j] / n == block; j++) { n       query(1, 0, n, id % n, p[j] % n); n       id = p[j]; n       if (id == k) { n         printf( %d n , ans[1][0]); n         return 0; n       } n       matrix tmp; n       tmp[0][0] = 0; n       tmp[1][0] = 1 % mo; n       tmp[0][1] = gs(id); n       tmp[1][1] = gs(id + 1); n       ans = ans * tmp; n       id++; n     } n     query(1, 0, n, id - block * n, n); n     id = (++block) * n; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s[1010]; n int main() { n   int n; n   cin >> n; n   bool f = 1; n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n     if (f && s[i].substr(0, 2) ==  OO ) { n       f = 0; n       s[i][0] =  + ; n       s[i][1] =  + ; n     } n     if (f && s[i].substr(3, 2) ==  OO ) { n       f = 0; n       s[i][3] =  + ; n       s[i][4] =  + ; n     } n   } n   if (f) { n     cout <<  NO ; n   } else { n     cout <<  YES  << endl; n     for (int i = 0; i < n; ++i) cout << s[i] << endl; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define ll int long long n #define mp make_pair n //#define for(i,n) for(int i=0;i<n;i++) n #define F first n #define S second n #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); n #define N 1000000007 n #define pq priority queue n #define pb push_back n #define pf push_front n #define mt make_tuple n #define prec(a) fixed<<setprecision(9)<<a n #define endl   n  n #define fast_set s.max_load_factor(0.25);s.reserve(500); n #define cy cout<< YES <<endl; n #define cn cout<< NO <<endl; n #define all(v) v.begin(),v.end() n #define allr(v) v.rbegin(),v.rend() n const long double PI = (long double)(3.1415926535897932384626433832795); n bool check(int a,int b,int hh,int mm) n { n   n  int savea=a,saveb=b; n  int arr[10]; n  memset(arr,-1,sizeof(arr)); n  arr[0]=0; n  arr[1]=1; n  arr[2]=5; n  arr[5]=2; n  arr[8]=8; n  int h=0,m=0; n  h=arr[(b%10)]*10+arr[(b/10)]; n  m=arr[(a%10)]*10+arr[(a/10)]; n  if(arr[a%10] == -1 || arr[a/10] == -1 || arr[b%10] == -1 || arr[b/10] == -1){ n   return 0; n  } n  return (h<hh&&m<mm); n } n int main()  n { n  fast; n  int test,n,i,j,k; n  cin>>test; n  while(test--) n  { n   string str; n   cin>>n>>k>>str; n   if(n%k!=0) n   { n    cout<< -1 <<endl; n   } n   else n   { n    int hash1[26]={0}; n    n=str.length(); n    for(i=0;i<n;i++) n    { n     hash1[str[i]- a ]++; n    } n    for(i=0;i<26;i++) n    { n     if(hash1[i]%k!=0) n     { n      break; n     } n    } n    if(i==26) n    { n     cout<<str<<endl; n    } n    else n    { n     for(i=n-1;i>=0;i--) n     { n      hash1[str[i]- a ]--; n      for(j=str[i]- a +1;j<26;j++) n      { n       bool flag=1; n       hash1[j]++; n       int it=0,cnt=0; n       for(it=0;it<26;it++) n       { n        cnt+=((k-(hash1[it]%k))%k); n       } n       int avail = n-i-1; n       if(cnt>avail||(avail-cnt)%k!=0) n       { n        flag=0; n       } n       hash1[j]--; n       if(flag) n       { n        break; n       } n      } n      if(j<26) n      { n       break; n      } n     } n     if(i<0) n     { n      cout<<i<<endl; n     } n     else n     { n      //cout<<i<<   <<j<<endl; n      str[i]= a +j; n      hash1[j]++; n      int it=0,mini=0; n      vector<int>s; n      int sum=0; n      for(it=0;it<26;it++) n      { n       if(hash1[it]>0) n        mini=min(mini,it); n       int a=((k-(hash1[it]%k))%k); n       sum+=a; n       while(a--) n       { n        mini=min(mini,it); n        s.pb(it); n       } n      } n      //cout<<sum<<endl; n      int a=n-i-1-sum; n      while(a--) n      { n       s.pb(mini); n      } n      int c=0; n      //cout<<s.size()<<endl; n      sort(all(s)); n      for(j=i+1;j<n;j++) n      { n       str[j]= a +s[c]; n       c++; n        n      } n      cout<<str<<endl; n     } n    } n   } n  } n  return 0; n } n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, counter, a, b, c; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c; n     counter = 0; n     while (++counter) { n       (a < b) ? a += b : b += a; n       if (max(a, b) > c) break; n     } n     cout << counter << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<int, int> > ss; n struct Node { n   int l, r, ope; n   bool operator<(const Node &b) const { n     if (l != b.l) return l < b.l; n     return ope > b.ope; n   } n }; n Node a[200010]; n int num[100010], ans[100010]; n int main() { n   int n, m; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &a[i].l, &a[i].r); n     a[i].ope = -1 - i; n   } n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d%d , &a[i + n].l, &a[i + n].r, &num[i]); n     a[i + n].ope = i; n   } n   sort(a, a + n + m); n   for (int i = 0; i < n + m; i++) { n     if (a[i].ope >= 0) { n       ss.insert(make_pair(a[i].r, a[i].ope)); n     } else { n       if (ss.size() == 0) { n         puts( NO ); n         return 0; n       } n       set<pair<int, int> >::iterator pos = n           ss.lower_bound(make_pair(a[i].r, -1)); n       if (pos == ss.end()) { n         puts( NO ); n         return 0; n       } n       pair<int, int> x = *pos; n       num[x.second]--; n       ans[-(a[i].ope + 1)] = x.second + 1; n       if (num[x.second] == 0) { n         ss.erase(pos); n       } n     } n   } n   puts( YES ); n   for (int i = 0; i < n; i++) { n     printf( %d  , ans[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-6; n const int MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const long long INFLL = 0x3f3f3f3f3f3f3f3f; n const int MAXN = 5e5 + 5; n int n, k; n struct nodes { n   int maxn[12], minn[12], siz; n   bool operator<(const nodes& x) const { n     int f = 1; n     for (int i = 1; i <= k; i++) n       if (maxn[i] > x.minn[i]) f = 0; n     return f; n   } n }; n int main() { n   cin >> n >> k; n   multiset<nodes> s; n   for (int i = 1; i <= n; i++) { n     nodes now; n     for (int j = 1; j <= k; j++) { n       scanf( %d , &now.maxn[j]); n       now.minn[j] = now.maxn[j]; n     } n     now.siz = 1; n     auto p = s.lower_bound(now); n     auto p2 = s.upper_bound(now); n     nodes nxt = now; n     vector<multiset<nodes>::iterator> v; n     for (auto sp = p; sp != p2; sp++) { n       for (int i = 1; i <= k; i++) { n         nxt.maxn[i] = max(nxt.maxn[i], sp->maxn[i]); n         nxt.minn[i] = min(nxt.minn[i], sp->minn[i]); n       } n       nxt.siz += sp->siz; n       v.push_back(sp); n     } n     for (int i = 0; i < v.size(); i++) s.erase(v[i]); n     s.insert(nxt); n     printf( %d n , s.rbegin()->siz); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 15; n int a[N], dp[N][2]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i += 1) { n     cin >> a[i]; n   } n   int ans = 0; n   for (int i = n; i >= 1; i -= 1) { n     dp[i][0] = 1; n     if (i == n) continue; n     if (a[i] < a[i + 1]) dp[i][0] += dp[i + 1][0]; n   } n   for (int i = 1; i <= n; i += 1) { n     dp[i][1] = 1; n     if (i == 1) continue; n     if (a[i] > a[i - 1]) { n       dp[i][1] += dp[i - 1][1]; n     } n   } n   for (int i = 1; i <= n; i += 1) { n     int x = dp[i + 1][0]; n     int y = dp[i - 1][1]; n     ans = max(ans, min(n, x + 1)); n     ans = max(ans, min(n, y + 1)); n     if (a[i - 1] + 1 < a[i + 1]) ans = max(ans, min(n, x + y + 1)); n   } n   if (n == 1) ans = 1; n   cout << min(ans, n) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100100; n const long long Mod = 1e9 + 7; n long long Rec(long long i, long long j, long long c); n long long p[N]; n long long x[N]; n vector<long long> Adj[N]; n vector<long long> Dp[N][2]; n int main() { n   long long n; n   cin >> n; n   for (long long i = 2; i <= n; i++) n     scanf( %I64d , p + i), Adj[++p[i]].push_back(i); n   for (long long i = 1; i <= n; i++) scanf( %I64d , x + i); n   for (long long i = 0; i < N; i++) n     Dp[i][0] = Dp[i][1] = vector<long long>(Adj[i].size(), -1); n   cout << Rec(1, 0, 0); n } n long long Rec(long long i, long long j, long long c) { n   if (j == 0) c += x[i]; n   if (c > 1) return 0; n   if (j == Adj[i].size()) return c; n   if (Dp[i][c][j] + 1) return Dp[i][c][j]; n   long long Res1 = (Rec(i, j + 1, c) * Rec(Adj[i][j], 0, 0)) % Mod; n   long long Res2 = (c ? Rec(i, j + 1, 1) * Rec(Adj[i][j], 0, 1) : 0) % Mod; n   long long Res3 = (!c ? (Rec(i, j + 1, 1) * Rec(Adj[i][j], 0, 0)) % Mod + n                              (Rec(i, j + 1, 0) * Rec(Adj[i][j], 0, 1)) % Mod n                        : 0) % n                    Mod; n   return Dp[i][c][j] = (Res1 + Res2 + Res3) % Mod; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n map<char, bool> m; n int main() { n   cin >> n >> s; n   char pl =  a ; n   int sum = 0, sum2 = 0; n   int len = s.size(); n   int book = 0; n   for (int i = 0; i < len / 2; ++i) { n     if (isalpha(s[i])) { n       if (!m[s[i]]) { n         m[s[i]] = 1; n         sum2++; n       } n       if (s[len - i - 1] ==  ? ) { n         s[len - i - 1] = s[i]; n       } else if (s[i] != s[len - i - 1]) { n         puts( IMPOSSIBLE ); n         return 0; n       } n     } n   } n   for (int i = len / 2; i < len; ++i) { n     if (isalpha(s[i])) { n       if (!m[s[i]]) { n         m[s[i]] = 1; n         sum2++; n       } n       if (s[len - i - 1] ==  ? ) { n         s[len - i - 1] = s[i]; n       } else if (s[i] != s[len - i - 1]) { n         puts( IMPOSSIBLE ); n         return 0; n       } n     } else n       sum++; n   } n   sum2 = n - sum2; n   if (sum2 > sum) { n     puts( IMPOSSIBLE ); n     return 0; n   } n   int tot = sum - sum2; n   for (int i = 0; i <= len / 2; ++i) { n     if (s[i] ==  ? ) { n       if (tot) { n         s[i] =  a ; n         s[len - i - 1] =  a ; n         tot--; n       } else { n         while (m[pl]) pl++; n         s[i] = pl; n         s[len - i - 1] = pl; n         m[pl] = 1; n       } n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortcol(const vector<long long int>& v1, const vector<long long int>& v2) { n   if (v1[0] == v2[0]) return v1[1] < v2[1]; n   return v1[0] < v2[0]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, m; n   cin >> n >> m; n   vector<vector<long long int> > A(m, vector<long long int>(n)); n   vector<long long int> sum(n); n   for (long long int i = 0; i < m; i++) n     for (long long int j = 0; j < n; j++) cin >> A[i][j]; n   for (long long int i = 0; i < m; i++) { n     for (long long int j = 0; j < n - 1; j++) A[i][j] = A[i][n - 1] - A[i][j]; n     A[i][n - 1] = i; n   } n   long long int max = 0, store = -1; n   for (long long int i = 0; i < n - 1; i++) { n     vector<vector<long long int> > B(m, vector<long long int>(2)); n     for (long long int j = 0; j < m; j++) { n       B[j][0] = A[j][i]; n       B[j][1] = A[j][n - 1]; n     } n     sort(B.begin(), B.end(), sortcol); n     long long int ans = 0, val = 0, check = 0; n     for (long long int j = 0; j < m; j++) { n       ans += B[j][0]; n       if (ans > 0) { n         val = j; n         if (val > max) { n           max = val; n           store = i; n         } n         check = 1; n         break; n       } n     } n     if (check == 1) continue; n     val = m; n     if (val > max) { n       max = val; n       store = i; n     } n   } n   cout << m - max << endl; n   vector<vector<long long int> > B(m, vector<long long int>(2)); n   for (long long int j = 0; j < m; j++) { n     B[j][0] = A[j][store]; n     B[j][1] = A[j][n - 1]; n   } n   sort(B.begin(), B.end(), sortcol); n   for (long long int i = max; i < m; i++) cout << B[i][1] + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   int x1, x2, y1, y2; n   cin >> n >> m >> k; n   map<int, vector<pair<int, int> > > cols; n   map<int, vector<pair<int, int> > > rows; n   map<int, int> sc; n   map<int, int> sr; n   for (int i = 0; i < k; i++) { n     cin >> x1 >> y1 >> x2 >> y2; n     if (x1 == x2) n       cols[x1].push_back(make_pair(min(y1, y2), max(y1, y2))); n     else n       rows[y1].push_back(make_pair(min(x1, x2), max(x1, x2))); n   } n   vector<pair<int, int> > sizes; n   map<int, vector<pair<int, int> > >::iterator it; n   vector<pair<int, int> > cuts; n   int ans = 0; n   for (it = cols.begin(); it != cols.end(); it++) { n     cuts = (*it).second; n     int sum = 0, last = 0; n     sort(cuts.begin(), cuts.end()); n     for (int i = 0; i < cuts.size(); i++) { n       if (cuts[i].first >= last) { n         sum += cuts[i].second - cuts[i].first; n         last = cuts[i].second; n       } n       if (cuts[i].second > last) { n         sum += cuts[i].second - last; n         last = cuts[i].second; n       } n     } n     ans ^= (m - sum); n     sc[(*it).first] = m - sum; n   } n   for (it = rows.begin(); it != rows.end(); it++) { n     cuts = (*it).second; n     int sum = 0, last = 0; n     sort(cuts.begin(), cuts.end()); n     for (int i = 0; i < cuts.size(); i++) { n       if (cuts[i].first >= last) { n         sum += cuts[i].second - cuts[i].first; n         last = cuts[i].second; n       } n       if (cuts[i].second > last) { n         sum += cuts[i].second - last; n         last = cuts[i].second; n       } n     } n     ans ^= (n - sum); n     sr[(*it).first] = n - sum; n   } n   if ((n - 1 - cols.size()) % 2) ans ^= m; n   if ((m - 1 - rows.size()) % 2) ans ^= n; n   if (ans == 0) n     cout <<  SECOND  << endl; n   else { n     cout <<  FIRST  << endl; n     if (cols.size() < n - 1 && (ans ^ m) < m) { n       int col = 1; n       while (cols.find(col) != cols.end()) col++; n       cout << col <<   0   << col <<     << m - (ans ^ m) << endl; n       return 0; n     } n     if (rows.size() < m - 1 && (ans ^ n) < n) { n       int row = 1; n       while (rows.find(row) != rows.end()) row++; n       cout <<  0   << row <<     << n - (ans ^ n) <<     << row << endl; n       return 0; n     } n     for (it = cols.begin(); it != cols.end(); it++) { n       int x = (*it).first; n       if ((sc[x] ^ ans) >= sc[x]) continue; n       int need = (sc[x] ^ ans), last = 0, sum = 0; n       cuts = (*it).second; n       sort(cuts.begin(), cuts.end()); n       for (int i = 0; i < cuts.size() && need; i++) { n         if (cuts[i].first >= last) { n           sum += cuts[i].first - last; n           if (sum <= need) n             last = cuts[i].second; n           else n             last = cuts[i].first - (sum - need); n           continue; n         } n         if (cuts[i].second > last) { n           last = cuts[i].second; n         } n       } n       if (sum < need) last += (need - sum); n       cout << x <<     << last <<     << x <<     << m << endl; n       return 0; n     } n     for (it = rows.begin(); it != rows.end(); it++) { n       int x = (*it).first; n       if ((sr[x] ^ ans) >= sr[x]) continue; n       int need = (sr[x] ^ ans), last = 0, sum = 0; n       cuts = (*it).second; n       sort(cuts.begin(), cuts.end()); n       for (int i = 0; i < cuts.size() && need; i++) { n         if (cuts[i].first >= last) { n           sum += cuts[i].first - last; n           if (sum <= need) n             last = cuts[i].second; n           else n             last = cuts[i].first - (sum - need); n           continue; n         } n         if (cuts[i].second > last) { n           last = cuts[i].second; n         } n       } n       if (sum < need) last += (need - sum); n       cout << last <<     << x <<     << n <<     << x << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     long long int n; n     cin >> n; n     long long int a[n]; n     for (int i = 0; i < n; i += 1) cin >> a[i]; n     long long int pre[n]; n     pre[0] = a[0]; n     for (long long int i = 1; i < n; i += 1) { n       pre[i] = a[i]; n       pre[i] += pre[i - 1]; n     } n     long long int q; n     cin >> q; n     for (long long int i = 0; i < q; i += 1) { n       long long int x, y; n       cin >> x >> y; n       x -= 1, y -= 1; n       long long int tmp = pre[y] - pre[x] + a[x]; n       tmp = tmp / 10; n       cout << tmp <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, q, i, j, l, r; n   cin >> n >> q; n   int auxi[n + 2], sum[n + 2]; n   memset(auxi, 0, sizeof(auxi)); n   memset(sum, 0, sizeof(sum)); n   vector<pair<int, int> > v; n   for (i = 0; i < q; i++) { n     cin >> l >> r; n     v.push_back({l, r}); n     auxi[r + 1] -= 1; n     auxi[l] += 1; n   } n   int net = 0; n   for (i = 0; i <= n + 1; i++) { n     net += auxi[i]; n     sum[i] += net; n   } n   int ans = 0; n   for (i = 0; i < q; i++) { n     for (j = 0; j <= n; j++) { n       auxi[j] = sum[j]; n     } n     int count = 0; n     for (j = v[i].first; j <= v[i].second; j++) { n       auxi[j]--; n     } n     int prefix[n + 1]; n     prefix[0] = 0; n     for (j = 1; j <= n; j++) { n       prefix[j] = prefix[j - 1] + (auxi[j] == 1); n       if (auxi[j]) count++; n     } n     for (j = i + 1; j < q; j++) { n       ans = max(ans, count - prefix[v[j].second] + prefix[v[j].first - 1]); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n const int MAXN = 2e5 + 10; n const int mod = 1e9 + 7; n void reduce(int& x) { x += x >> 31 & mod; } n int mul(int a, int b) { return (long long)a * b % mod; } n int pow(int a, int b, int res = 1) { n   for (; b; b >>= 1, a = mul(a, a)) n     if (b & 1) res = mul(res, a); n   return res; n } n const int MAXS = 1e6 + 1; n int pri[MAXS], npri[MAXS], tot; n int fac[MAXS], inv[MAXS]; n void sieve() { n   inv[0] = inv[1] = 1; n   fac[1] = 1; n   for (int i = 2; i != MAXS; ++i) { n     inv[i] = mul(inv[mod % i], mod - mod / i); n     if (!npri[i]) fac[pri[++tot] = i] = i; n     for (int j = 1, t; j <= tot && (t = i * pri[j]) < MAXS; ++j) { n       npri[t] = true; n       fac[t] = pri[j]; n       if (i % pri[j] == 0) break; n     } n   } n } n int A[MAXN]; n int n, m; n int pre[MAXN]; n int tr[MAXN]; n void add(int x, int v) { n   for (; x; x &= x - 1) tr[x] = mul(tr[x], v); n } n int lst[MAXS], ansl[MAXN]; n int ls[MAXN]; n std::vector<int> qs[MAXN]; n int qry(int x) { n   int r = 1; n   for (; x <= n; x += x & -x) r = mul(r, tr[x]); n   return r; n } n int main() { n   std::ios_base::sync_with_stdio(false), std::cin.tie(0); n   sieve(); n   std::cin >> n; n   pre[0] = 1; n   for (int i = 1; i <= n; ++i) { n     std::cin >> A[i]; n     pre[i] = mul(pre[i - 1], A[i]); n     tr[i] = 1; n   } n   std::cin >> m; n   for (int i = 1, r; i <= m; ++i) { n     std::cin >> ls[i] >> r; n     qs[r].push_back(i); n   } n   for (int i = 1; i <= n; ++i) { n     std::set<int> hl; n     int x = A[i]; n     while (x > 1) hl.insert(fac[x]), x /= fac[x]; n     for (auto t : hl) { n       add(lst[t], mul(inv[t - 1], t)); n       add(lst[t] = i, mul(inv[t], t - 1)); n     } n     for (auto t : qs[i]) { n       ansl[t] = pow(pre[ls[t] - 1], mod - 2, pre[i]); n       ansl[t] = mul(ansl[t], qry(ls[t])); n     } n   } n   for (int i = 1; i <= m; ++i) std::cout << ansl[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const int N = 9; n const int MAX = 100007; n const int H = 1 << 9; n int n; n int letter[MAX][N]; n int dist[N][N]; n int ch[MAX]; n vector<int> l[N]; n bool checked[N]; n int mask[MAX]; n int cnt[N][H]; n void BFS(int x) { n   vector<int> vis(n, 0); n   queue<int> bfs; n   for (int i = 0; i < 8; ++i) { n     checked[i] = 0; n   } n   for (int i = 0; i < n; ++i) { n     letter[i][x] = (int)1e9 + 7; n     if (ch[i] == x) { n       vis[i] = 1; n       bfs.push(i); n       letter[i][x] = 0; n     } n   } n   while (!bfs.empty()) { n     int v = bfs.front(); n     bfs.pop(); n     if (v && !vis[v - 1]) { n       vis[v - 1] = 1; n       letter[v - 1][x] = letter[v][x] + 1; n       bfs.push(v - 1); n     } n     if (v + 1 < n && !vis[v + 1]) { n       vis[v + 1] = 1; n       letter[v + 1][x] = letter[v][x] + 1; n       bfs.push(v + 1); n     } n     if (!checked[ch[v]]) { n       checked[ch[v]] = 1; n       for (auto& u : l[ch[v]]) { n         if (!vis[u]) { n           vis[u] = 1; n           letter[u][x] = letter[v][x] + 1; n           bfs.push(u); n         } n       } n     } n   } n } n int FindDist(int x, int y) { n   int res = abs(x - y); n   for (int i = 0; i < 8; ++i) { n     res = min(res, 1 + letter[x][i] + letter[y][i]); n   } n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; ++i) { n     ch[i] = s[i] -  a ; n   } n   for (int i = 0; i < 8; ++i) { n     for (int j = 0; j < n; ++j) { n       if (ch[j] == i) { n         l[i].push_back(j); n       } n     } n   } n   for (int i = 0; i < 8; ++i) { n     BFS(i); n   } n   for (int i = 0; i < 8; ++i) { n     for (int j = 0; j < 8; ++j) { n       dist[i][j] = (int)1e9 + 7; n       for (auto& v : l[i]) { n         dist[i][j] = min(dist[i][j], letter[v][j]); n       } n     } n   } n   int maxxx = 2 * 8 - 1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < 8; ++j) { n       int temp = letter[i][j] - dist[ch[i]][j]; n       assert(temp < 2); n       mask[i] |= temp << j; n     } n   } n   pair<long long, long long> result = make_pair(0, 0); n   for (int i = 0; i < n; ++i) { n     for (int j = max(0, i - maxxx); j < i; ++j) { n       int d = FindDist(i, j); n       if (d > result.first) { n         result.first = d; n         result.second = 1; n       } else if (d == result.first) { n         result.second++; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int c = 0; c < 8; ++c) { n       for (int m = 0; m < 1 << 8; ++m) { n         if (cnt[c][m]) { n           int temp = (int)1e9 + 7; n           for (int d = 0; d < 8; ++d) { n             temp = min(temp, 1 + dist[c][d] + ((m >> d) & 1) + dist[ch[i]][d] + n                                  ((mask[i] >> d) & 1)); n           } n           if (temp > result.first) { n             result.first = temp; n             result.second = cnt[c][m]; n           } else if (temp == result.first) { n             result.second += cnt[c][m]; n           } n         } n       } n     } n     if (i - maxxx >= 0) { n       int z = i - maxxx; n       cnt[ch[z]][mask[z]]++; n     } n   } n   cout << result.first <<     << result.second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200005], cnt = 0; n bool vis[200005]; n void f(int x) { n   while (!vis[x]) vis[x] = 1, x = a[x]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   for (int i = 1; i <= n; ++i) n     if (!vis[i]) { n       ++cnt; n       f(i); n     } n   if (cnt == 1) cnt = 0; n   bool f = 0; n   for (int j, i = 1; i <= n; ++i) scanf( %d , &j), f ^= (j & 1); n   cnt += !f; n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     int n; n     cin >> n; n     int gnum; n     while (1) { n       gnum = n; n       while (gnum > 0) { n         if (gnum % 3 == 2) { n           break; n         } n         gnum = gnum / 3; n       } n       if (gnum == 1 || gnum == 0) { n         break; n       } n       n++; n     } n     cout << n << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long arr[100005]; n int c5; n int find(long long a, long long b, long long c) { n   long long arr[4]; n   arr[0] = a; n   arr[1] = b; n   arr[2] = c; n   sort(arr, arr + 3); n   if (arr[0] < arr[1] && arr[1] < arr[2]) n     return 6; n   else if (arr[0] == arr[1] && arr[1] < arr[2]) n     return 3; n   else if (arr[0] < arr[1] && arr[1] == arr[2]) n     return 3; n   else n     return 1; n } n int main() { n   cin >> n; n   if (n % 6 != 0) { n     printf( 0 n ); n     return 0; n   } n   n /= 3; n   for (long long x = 1; x * x <= n; x++) { n     if (n % x == 0) { n       arr[c5++] = x; n       if (x * x != n) arr[c5++] = n / x; n     } n   } n   sort(arr, arr + c5); n   long long x, y, z; n   int ans = 0, cnt = 0; n   long long a, b, c; n   for (int i = 0; i < c5; i++) { n     x = arr[i]; n     for (int j = i; j < c5 && arr[i] * arr[j] * arr[j] <= n; j++) { n       if (n % (arr[i] * arr[j]) == 0) { n         y = arr[j]; n         z = n / (arr[i] * arr[j]); n         if (x <= y <= z) { n           if (x - y + z > 0 && y - x + z > 0 && y - z + x > 0 && n               (x - y + z) % 2 == 0) { n             cnt++; n             a = (x - y + z) / 2; n             b = (y - x + z) / 2; n             c = (y - z + x) / 2; n             ans += find(a, b, c); n           } n         } n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n int main() { n   map<int, int> m, p; n   int n; n   cin >> n; n   for (int i = (1); i <= (n); i++) { n     int u, v; n     cin >> u >> v; n     m[u]++; n     if (v != u) p[v]++; n   } n   int ans = 1e9; n   if (n % 2) n++; n   for (auto i : m) { n     int cval = i.second; n     int poss = p[i.first]; n     int reqd = n / 2; n     int now = reqd - cval; n     if (now <= poss) ans = min(ans, max(0, now)); n   } n   for (auto i : p) { n     int poss = i.second; n     int cval = m[i.first]; n     int reqd = n / 2; n     int now = reqd - cval; n     if (now <= poss) ans = min(ans, max(0, now)); n   } n   if (ans == 1e9) n     cout <<  -1 n ; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long prime = 1e9 + 7; n const long long maxN = 1e5 + 5; n const long double pi = 3.1415926536; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k, a, rem = 1e18, t = 1, b = 0; n   cin >> n >> k; n   for (long long i = (long long)(1); i <= (long long)(k); i++) { n     cin >> a; n     if ((a <= n) && (n % a < rem)) { n       rem = n % a; n       t = i; n       b = n / a; n     } n   } n   cout << t <<     << b; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[3000]; n long long h[200000], pw[200000]; n string st, st1, st2; n long l1, l2, l, ans, fl, g1[20000], g2[20000]; n long long gethash(long a, long b) { n   return (h[b + 1] - h[a]) * pw[st.size() - b + 1]; n } n int main() { n   cin >> st; n   cin >> st1 >> st2; n   pw[0] = 1; n   for (int i = 1; i <= 3000; i++) pw[i] = pw[i - 1] * 29; n   for (int i = 1; i <= st.size(); i++) h[i] = h[i - 1] + st[i - 1] * pw[i]; n   l = st.size(); n   l1 = st1.size(); n   l2 = st2.size(); n   for (int i = 0; i <= l - l1; i++) { n     fl = 0; n     for (int j = 0; j < l1; j++) n       if (st1[j] != st[i + j]) { n         fl++; n         break; n       } n     if (fl == 0) g1[i] = 1; n   } n   for (int i = 0; i <= l - l2; i++) { n     fl = 0; n     for (int j = 0; j < l2; j++) n       if (st2[j] != st[i + j]) { n         fl++; n         break; n       } n     if (fl == 0) g2[i + l2 - 1] = 1; n   } n   long lll = max(l1, l2); n   for (int i = 0; i < l; i++) n     for (int j = i; j < l; j++) n       if (g1[i] && g2[j] && (j - i + 1 >= lll)) { n         v[j - i + 1].push_back(gethash(i, j)); n       } n   for (int i = 1; i <= l; i++) { n     sort(v[i].begin(), v[i].end()); n     if (v[i].size()) ans++; n     for (int j = 1; j < v[i].size(); j++) n       if (v[i][j] != v[i][j - 1]) ++ans; n   } n   cout << ans << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt_9s(long long int num); n long long int power(int num, int exp); n int main() { n   long long int num, sale, rem; n   cin >> num >> sale; n   while (1) { n     rem = power(10, cnt_9s(num) + 1); n     if (sale >= num % rem + 1) n       sale -= (num % rem + 1), num -= (num % rem + 1); n     else n       break; n   } n   cout << num << endl; n   return 0; n } n long long int power(int num, int exp) { n   long long int res = 1; n   while (exp != 0) res *= num, exp--; n   return res; n } n int cnt_9s(long long int num) { n   int cnt = 0; n   while (num != 0) n     if (num % 10 == 9) n       cnt++, num /= 10; n     else n       break; n   return cnt; n } n


#include <bits/stdc++.h> n using namespace std; n char a[100100]; n int main() { n   scanf( %s , a); n   int en; n   int len = strlen(a); n   int r = 0, l = 0; n   int ll, rr; n   int flag = 0; n   int last; n   int cnt = 0; n   for (int i = 0; a[i] !=   0 ; i++) n     if (a[i] ==  # ) en = i; n   for (int i = 0; i < len; i++) { n     if (a[i] ==  ( ) n       l++; n     else { n       r++; n       if (a[i] ==  # ) cnt++; n     } n     if (r > l) { n       flag = 1; n       break; n     } n     if (i == en) ll = l, rr = r; n   } n   last = l - r + 1; n   rr += last - 1; n   if (ll < rr) flag = 1; n   for (int i = en + 1; i < len; i++) { n     if (a[i] ==  ( ) n       ll++; n     else n       rr++; n     if (rr > ll) { n       flag = 1; n       break; n     } n   } n   if (rr != ll) flag = 1; n   if (flag) n     puts( -1 ); n   else { n     for (int i = 0; i < cnt - 1; i++) puts( 1 ); n     printf( %d n , last); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 300010; n int n; n int maxx; n int node[2 * MAX][30]; n int cau[MAX]; n int site; n int other; n void in() { n   scanf( %d , &n); n   int a, b; n   char c; n   for (int i = 1; i < n; i++) { n     scanf( %d %d %c , &a, &b, &c), node[a][c -  a ] = b; n   } n } n int w = 1; n int mix(int x, int y) { n   if (x == 0 || y == 0) return x + y; n   int now = ++other; n   site++; n   for (int i = 0; i < 26; i++) node[now][i] = mix(node[x][i], node[y][i]); n   return now; n } n void dfs(int x, int deep) { n   other = n + 1; n   int now = other; n   site = 0; n   for (int i = 0; i < 26; i++) now = mix(now, node[x][i]); n   cau[deep] += site; n   for (int i = 0; i < 26; i++) n     if (node[x][i] > 0) dfs(node[x][i], deep + 1); n } n void work() { n   dfs(1, 1); n   maxx = 1; n   for (int i = 1; i <= n; i++) n     if (n - cau[maxx] > n - cau[i]) maxx = i; n } n void out() { n   printf( %d n , n - cau[maxx]); n   printf( %d n , maxx); n } n int main() { n   in(); n   work(); n   out(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class IO { n   static const int bufSize = 1 << 18; n   char inBuf[bufSize], outBuf[bufSize]; n   char *ip1 = inBuf, *ip2 = inBuf; n   int goodReadBit = 0, op1 = -1, op2 = bufSize - 1; n   template <typename T> n   __inline__ __attribute__((always_inline)) void __RI(T& x) { n     int ch = gc(), neg = 1; n     x = 0; n     for (; !(isdigit(ch) || ch ==  -  || ch == EOF); ch = gc()) n       ; n     if (ch == EOF) { n       goodReadBit = 0; n       return; n     } n     if (ch ==  - ) neg = -1, ch = gc(); n     for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - 48) * neg; n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) void __RC(T& x) { n     unsigned char ch; n     while (isspace(ch = gc())) n       ; n     x = ch; n   } n   __inline__ __attribute__((always_inline)) void __RS(string& x) { n     char ch; n     x.clear(); n     for (ch = gc(); isspace(ch); ch = gc()) n       ; n     if (ch == EOF) { n       goodReadBit = 0; n       return; n     } n     for (; !isspace(ch); ch = gc()) x.push_back(ch); n   } n  n  public: n   __inline__ __attribute__((always_inline)) int gc() { n     return ip1 == ip2 && (ip2 = (ip1 = inBuf) + fread(inBuf, 1, bufSize, stdin), n                           ip1 == ip2) n                ? EOF n                : *ip1++; n   } n   __inline__ __attribute__((always_inline)) IO& R(char& x) { n     __RC(x); n     return (*this); n   } n   __inline__ __attribute__((always_inline)) IO& R(unsigned char& x) { n     __RC(x); n     return (*this); n   } n   __inline__ __attribute__((always_inline)) IO& R(string& x) { n     __RS(x); n     return (*this); n   } n   template <typename T1, typename T2> n   __inline__ __attribute__((always_inline)) IO& R(pair<T1, T2>& x) { n     R(x.first), R(x.second); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& R(vector<T>& x) { n     for (auto& i : x) R(i); n     return (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO& RA(T* a, int n) { n     for (int i = 0; i < n; ++i) R(a[i]); n     return (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO& R(T& x, Args&... args) { n     R(x), R(args...); n     return (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO& RA(T* a, int n, Args... args) { n     for (int i = 0; i < n; ++i) RA(a[i], args...); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& R(T& x) { n     static_assert(is_integral<T>::value,  Unsupported types ); n     if (is_integral<T>::value) __RI(x); n     return (*this); n   } n  n  private: n   char space =    ; n   template <typename T> n   __inline__ __attribute__((always_inline)) void __WI(T x) { n     static char buf[64]; n     static int len = -1; n     if (x >= 0) { n       do { n         buf[++len] = x % 10 + 48, x /= 10; n       } while (x); n     } else { n       pc( - ); n       do { n         buf[++len] = -(x % 10) + 48, x /= 10; n       } while (x); n     } n     while (len >= 0) { n       pc(buf[len]), --len; n     } n   } n  n  public: n   __inline__ __attribute__((always_inline)) void pc(const char& x) { n     if (op1 == op2) flush(); n     outBuf[++op1] = x; n   } n   __inline__ __attribute__((always_inline)) void flush() { n     fwrite(outBuf, 1, op1 + 1, stdout), op1 = -1; n   } n   __inline__ __attribute__((always_inline)) IO& W(const char& x) { n     pc(x); n     return (*this); n   } n   __inline__ __attribute__((always_inline)) IO& W(const char* x) { n     while (*x !=   0 ) pc(*(x++)); n     return (*this); n   } n   __inline__ __attribute__((always_inline)) IO& W(const string& x) { n     W(x.c_str()); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& W(const vector<T>& x) { n     for (auto& i : x) WS(i); n     WL(); n     return (*this); n   } n   __inline__ __attribute__((always_inline)) IO& WL() { n     W(  n ); n     return (*this); n   } n   template <typename T1, typename T2> n   __inline__ __attribute__((always_inline)) IO& W(const pair<T1, T2>& x) { n     WS(x.first), W(x.second); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& WL(const T& x) { n     W(x), W(  n ); n     return (*this); n   } n   __inline__ __attribute__((always_inline)) IO& WS() { n     W(   ); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& WS(const T& x) { n     W(x), W(space); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& WA(T* a, int n) { n     for (int i = 0; i < n; i++) WS(a[i]); n     WL(); n     return (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO& W(const T& x, n                                                   const Args&... args) { n     W(x), W(space), W(args...); n     return (*this); n   } n   template <typename... Args> n   __inline__ __attribute__((always_inline)) IO& WL(const Args&... args) { n     W(args...), W(  n ); n     return (*this); n   } n   template <typename T, typename... Args> n   __inline__ __attribute__((always_inline)) IO& WA(T* a, int n, Args... args) { n     for (int i = 0; i < n; i++) WA(a[i], args...); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& W(const T& x) { n     static_assert(is_integral<T>::value,  Unsupported types ); n     if (is_integral<T>::value) __WI(x); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& operator>>(T& x) { n     R(x); n     return (*this); n   } n   template <typename T> n   __inline__ __attribute__((always_inline)) IO& operator<<(const T& x) { n     W(x); n     return (*this); n   } n   IO() {} n } io; n void EXIT(int code) { io.flush(), exit(code); } n const int8_t INFC = 0x3f; n const int16_t INFS = 0x3f3f; n const int32_t INF = 0x3f3f3f3f; n const int64_t INFL = 0x3f3f3f3f3f3f3f3fLL; n const int BLOCK_SIZE = 512; n struct Block { n   int n; n   vector<int> cnt, to, lst, &a; n   Block(int n, vector<int>& a) : n(n), a(a) { n     cnt.assign(n, -1), to.assign(n, -1), lst.assign(n, -1); n   } n   inline void __upd(int i, int j, int n) { n     if (a[j] + i >= n) { n       to[i] = a[j] + i - n; n       lst[i] = j; n       cnt[i] = 1; n     } else { n       to[i] = to[a[j] + i]; n       lst[i] = lst[a[j] + i]; n       cnt[i] = cnt[a[j] + i] + 1; n     } n   } n   void update(int pos) { n     if (n == BLOCK_SIZE) { n       for (int i = n - 1; i >= 0; i -= 4) { n         __upd(i, i + pos, n); n         __upd(i - 1, i + pos - 1, n); n         __upd(i - 2, i + pos - 2, n); n         __upd(i - 3, i + pos - 3, n); n       } n     } else { n       for (int i = n - 1; i >= 0; i--) { n         int j = i + pos; n         if (a[j] + i >= n) { n           to[i] = a[j] + i - n; n           lst[i] = j; n           cnt[i] = 1; n         } else { n           to[i] = to[a[j] + i]; n           lst[i] = lst[a[j] + i]; n           cnt[i] = cnt[a[j] + i] + 1; n         } n       } n     } n   } n }; n vector<Block> blocks; n int main() { n   int n, q; n   io >> n >> q; n   vector<int> v(n); n   io >> v; n   for (int i = 0; i < n; i += BLOCK_SIZE) { n     blocks.push_back(Block(min(BLOCK_SIZE, n - i), v)); n     blocks.back().update(i); n   } n   while (q--) { n     int op, a; n     io >> op >> a; n     --a; n     if (op == 1) { n       int bpos = a / BLOCK_SIZE; n       a = a % BLOCK_SIZE; n       int cnt = 0, ans = 0; n       while (bpos < blocks.size()) { n         if (a >= blocks[bpos].n) break; n         cnt += blocks[bpos].cnt[a]; n         ans = blocks[bpos].lst[a]; n         a = blocks[bpos].to[a]; n         bpos += a / BLOCK_SIZE; n         a %= BLOCK_SIZE; n         bpos++; n       } n       io.WL(ans + 1, cnt); n     } else { n       int b; n       io >> b; n       v[a] = b; n       blocks[a / BLOCK_SIZE].update(a / BLOCK_SIZE * BLOCK_SIZE); n     } n   } n   EXIT(0); n } n


#include <bits/stdc++.h> n using namespace std; n const int tmw168 = 1e5; n long long a, b, p, q, invp; n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n long long test(long long r) { n   r = r * invp % q; n   r += a / q * q; n   if (r < a) r += q; n   return r; n } n int f(long long x, long long y) { n   if (min(x, y) > b) return false; n   cout << min(x, y) <<   n ; n   return true; n } n long long inv(long long a, long long b) { n   long long s = 0, t = 1; n   long long old_s = 1, old_t = 0; n   long long r = b, old_r = a; n   while (r != 0) { n     long long quotient = old_r / r; n     long long tr = r; n     r = old_r - quotient * r; n     old_r = tr; n     long long ts = s; n     s = old_s - quotient * s; n     old_s = ts; n     long long tt = t; n     t = old_t - quotient * t; n     old_t = tt; n   } n   old_s = (old_s % q + q) % q; n   return old_s; n } n void solve() { n   cin >> a >> b >> p >> q; n   long long g = gcd(p, q); n   p /= g; n   q /= g; n   p %= q; n   int ma = -1, gd = 0; n   if (b - a < tmw168) { n     for (int i = a; i <= b; i++) { n       int cur = min(i * p % q, q - i * p % q); n       if (cur > ma) { n         ma = cur; n         gd = i; n       } n     } n     cout << gd <<   n ; n     return; n   } n   invp = inv(p, q); n   for (int i = 0; i < q; i++) { n     if (f(test(q / 2 - i), test((q + 1) / 2 + i))) return; n   } n } n int main() { n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, pos; n   string s; n   cin >> n >> s; n   bool flag = false; n   for (int i = 1; i < n; i++) { n     if (s[i] == s[i - 1]) n       continue; n     else { n       flag = true; n       pos = i; n       break; n     } n   } n   if (flag) n     cout <<  YES  << endl, cout << s[pos - 1] << s[pos] << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Z = 3 * (int)1e3 + 111; n const int N = 3 * (int)1e5 + 111; n const int INF = (int)1e9 + 111; n const int MOD = (int)1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   double l, d, v, g, r; n   cin >> l >> d >> v >> g >> r; n   double t(0); n   t = d / v; n   if (int(t + 1) % int(g + r) <= g) { n     if (int(t) % int(g + r) >= g) t = int(t) + 1; n   } else { n     t += (g + r) - int(t); n   } n   t += (l - d) / v; n   cout << setprecision(20) << t; n } n


#include <bits/stdc++.h> n using namespace std; n int N, ANS, X[201], Y[201]; n char S[205]; n int main() { n   scanf( %d %s , &N, S); n   for (int i = 0; i < N; i++) { n     X[i + 1] += X[i], Y[i + 1] += Y[i]; n     char c = S[i]; n     if (c ==  U ) Y[i + 1]++; n     if (c ==  R ) X[i + 1]++; n     if (c ==  D ) Y[i + 1]--; n     if (c ==  L ) X[i + 1]--; n   } n   for (int i = 1; i <= N; i++) n     for (int j = i; j <= N; j++) n       ANS += (X[j] - X[i - 1] == 0 && Y[j] - Y[i - 1] == 0); n   printf( %d n , ANS); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double EPS = 1e-9; n const int N = (1 << 15); n const int OO = 200000; n struct edge { n   int from, to, w; n   edge(int from, int to, int w) : from(from), to(to), w(w) {} n   bool operator<(const edge& e) const { return w < e.w; } n }; n struct UnionFind { n   vector<int> rank, parent; n   int forests; n   UnionFind(int n) { n     rank = vector<int>(n); n     parent = vector<int>(n); n     int forests = n; n     for (int i = 0; i < n; i++) parent[i] = i, rank[i] = 1; n   } n   int find_set(int x) { n     if (x == parent[x]) return x; n     return parent[x] = find_set(parent[x]); n   } n   void link(int x, int y) { n     if (rank[x] > rank[y]) swap(x, y); n     parent[x] = y; n     rank[y] += rank[x]; n   } n   bool union_sets(int x, int y) { n     x = find_set(x), y = find_set(y); n     if (x != y) { n       link(x, y); n       forests--; n     } n     return x != y; n   } n   long long same_set(int x, int y) { n     x = find_set(x), y = find_set(y); n     if (x == y) return 0; n     long long t = (long long)rank[x] * rank[y]; n     link(x, y); n     forests--; n     return t; n   } n   vector<vector<int>> connected_components() { n     vector<vector<int>> list(((int)(parent).size())); n     for (int i = 0; i < ((int)(parent).size()); i++) { n       list[find_set(i)].push_back(i); n     } n     return list; n   } n }; n vector<edge> edgeList; n long long a[100005]; n int main() { n   long long n, m, b, c; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) { n     cin >> b >> c; n     edgeList.push_back(edge(b - 1, c - 1, min(a[b - 1], a[c - 1]))); n   } n   UnionFind uf(n); n   double mstCost = 0; n   priority_queue<edge> q; n   for (int i = 0; i < edgeList.size(); i++) q.push(edgeList[i]); n   while (!q.empty()) { n     edge e = q.top(); n     q.pop(); n     mstCost += uf.same_set(e.from, e.to) * e.w; n   } n   cout << fixed << setprecision(6) << 2 * mstCost / (n * (n - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, x, y, z, A[(1 << 17)], B[(1 << 17)], C[(1 << 17)], n     mmp[(1 << 17)], Comb[18][18]; n long long O[(1 << 17)], Ans[4][(1 << 17)]; n long long powmod(long long x, long long y = 998244353 - 2, n                  long long p = 998244353) { n   x %= p; n   y = (y % (p - 1) + (p - 1)) % (p - 1); n   long long ans = 1; n   while (y) { n     if (y & 1) ans = ans * x % p; n     x = x * x % p; n     y >>= 1; n   } n   return ans; n } n signed main() { n   scanf( %lld%lld%lld%lld%lld , &n, &k, &x, &y, &z); n   for (long long i = 0; i < n; i++) { n     scanf( %lld%lld%lld , &A[i], &B[i], &C[i]); n     B[i] ^= A[i]; n     C[i] ^= A[i]; n   } n   for (long long i = 0; i <= k; i++) n     for (long long j = 0; j <= i; j++) n       Comb[i][j] = !j ? 1 : Comb[i - 1][j - 1] + Comb[i - 1][j]; n   mmp[0] = 1; n   for (long long i = 1; i <= k; i++) { n     mmp[i] = 1 - (i % 2); n     for (long long j = 0; j < i; j++) mmp[i] -= Comb[i][j] * mmp[j]; n   } n   for (long long b1 = 0; b1 < 2; b1++) n     for (long long b2 = 0; b2 < 2; b2++) { n       memset(O, 0, sizeof O); n       for (long long i = 0; i < n; i++) O[(B[i] * b1) ^ (C[i] * b2)]++; n       for (long long i = 1; i < (1 << k); i *= 2) n         for (long long j = 0; j < (1 << k); j += i + i) n           for (long long k = j; k < j + i; k++) n             O[k] = (O[k] + O[k + i]) % (998244353 - 1); n       for (long long i = 0; i < (1 << k); i++) n         O[i] *= mmp[__builtin_popcount(i)], O[i] %= (998244353 - 1); n       for (long long i = 1; i < (1 << k); i *= 2) n         for (long long j = 0; j < (1 << k); j += i + i) n           for (long long k = j; k < j + i; k++) n             O[k + i] = (O[k + i] + O[k]) % (998244353 - 1); n       memcpy(Ans[b1 * 2 + b2], O, sizeof O); n     } n   for (long long i = 0; i < (1 << k); i++) { n     long long _00 = Ans[0][i], _01 = Ans[1][i], _10 = Ans[2][i], n               _11 = Ans[3][i]; n     long long __00 = (_01 + _10 + _11 - _00) / 2; n     long long __11 = _11 - __00; n     long long __01 = _10 - __00; n     long long __10 = _01 - __00; n     O[i] = powmod(x - y - z, __11) * powmod(x - y + z, __10) % 998244353 * n            powmod(x + y - z, __01) % 998244353 * powmod(x + y + z, __00) % n            998244353; n   } n   long long tot = 0; n   for (long long i = 0; i < n; i++) tot ^= A[i]; n   for (long long i = 1; i < (1 << k); i *= 2) n     for (long long j = 0; j < (1 << k); j += i + i) n       for (long long k = j; k < j + i; k++) { n         long long A = O[k], B = O[k + i]; n         O[k] = (A + B) % 998244353; n         O[k + i] = (A - B) % 998244353; n       } n   for (long long i = 0; i < (1 << k); i++) n     printf( %lld  , n            (O[i ^ tot] * powmod(1 << k) % 998244353 + 998244353) % 998244353); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int flag = 0; n   int count = 0; n   if (isupper(s[0])) { n     flag = 1; n     count = 1; n   } n   for (int i = 1; i < s.length(); i++) { n     if (isupper(s[i])) count++; n   } n   if (count == s.length()) { n     for (int i = 0; i < s.length(); i++) { n       s[i] = tolower(s[i]); n     } n   } else if (count == s.length() - 1 && flag == 0) { n     s[0] = toupper(s[0]); n     for (int i = 1; i < s.length(); i++) { n       s[i] = tolower(s[i]); n     } n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[7], flag; n int a[1005], n; n int pos[4][30], pn[4]; n struct Pair { n   int l, r; n } p[4][300]; n int pai[4]; n stack<int> s; n bool check() { n   for (int i = 1; i <= n; i++) { n     if (a[i] != i) return false; n   } n   return true; n } n void find_pos(int index) { n   pn[index] = 0; n   pos[index][++pn[index]] = 1; n   for (int i = 2; i < n; i++) { n     if (!((a[i - 1] == a[i] + 1 && a[i + 1] == a[i] - 1) || n           (a[i - 1] == a[i] - 1 && a[i + 1] == a[i] + 1))) n       pos[index][++pn[index]] = i; n   } n   pos[index][++pn[index]] = n; n } n void to_pair(int index) { n   pai[index] = 0; n   for (int i = 1; i <= pn[index]; i++) { n     for (int j = i; j <= pn[index]; j++) { n       p[index][++pai[index]].l = pos[index][i]; n       p[index][pai[index]].r = pos[index][j]; n     } n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   find_pos(1); n   to_pair(1); n   for (int i = 1; i <= pai[1]; i++) { n     reverse(a + p[1][i].l, a + p[1][i].r + 1); n     find_pos(2); n     if (pn[2] > 10) { n       reverse(a + p[1][i].l, a + p[1][i].r + 1); n       continue; n     } n     s.push(p[1][i].r); n     s.push(p[1][i].l); n     to_pair(2); n     for (int j = 1; j <= pai[2]; j++) { n       reverse(a + p[2][j].l, a + p[2][j].r + 1); n       find_pos(3); n       if (pn[3] > 6) { n         reverse(a + p[2][j].l, a + p[2][j].r + 1); n         continue; n       } n       s.push(p[2][j].r); n       s.push(p[2][j].l); n       to_pair(3); n       for (int k = 1; k <= pai[3]; k++) { n         reverse(a + p[3][k].l, a + p[3][k].r + 1); n         flag = 0; n         if (check()) { n           flag = 1; n           s.push(p[3][k].r); n           s.push(p[3][k].l); n           ans[0] = s.size() / 2; n           ans[1] = s.top(); n           s.pop(); n           ans[2] = s.top(); n           s.pop(); n           ans[3] = s.top(); n           s.pop(); n           ans[4] = s.top(); n           s.pop(); n           ans[5] = s.top(); n           s.pop(); n           ans[6] = s.top(); n           s.pop(); n           for (int i = 1; i <= 5; i += 2) { n             if (ans[i] == ans[i + 1]) { n               ans[0]--; n             } n           } n           cout << ans[0] << endl; n           for (int i = 1; i <= 5; i += 2) { n             if (ans[i] != ans[i + 1]) n               cout << ans[i] <<     << ans[i + 1] << endl; n           } n         } n         if (flag) break; n         reverse(a + p[3][k].l, a + p[3][k].r + 1); n       } n       if (flag) break; n       reverse(a + p[2][j].l, a + p[2][j].r + 1); n       s.pop(); n       s.pop(); n     } n     if (flag) break; n     reverse(a + p[1][i].l, a + p[1][i].r + 1); n     s.pop(); n     s.pop(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, dp[103][10003], C[103][103]; n long long m; n int Plus(int x, int y) { return (x += y) >= 1000000007 ? x % 1000000007 : x; } n int mul(long long x, int y) { n   return (x *= y) >= 1000000007 ? x % 1000000007 : x; n } n int qpow(int x, long long y) { n   int ans = 1; n   while (y) { n     if (y & 1) ans = mul(ans, x); n     x = mul(x, x); n     y >>= 1; n   } n   return ans; n } n int main() { n   C[1][0] = C[1][1] = 1; n   for (int i = 2; i <= 100; i++) { n     C[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       C[i][j] = Plus(C[i - 1][j], C[i - 1][j - 1]); n     } n   } n   cin >> n >> m >> k; n   long long T = (m - 1) / n; n   dp[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int l = 0; l <= min(n, k); l++) { n       int syk = qpow(C[n][l], T + (T * n + i <= m)); n       for (int j = l; j <= k; j++) { n         dp[i][j] = Plus(dp[i][j], mul(dp[i - 1][j - l], syk)); n       } n     } n   } n   cout << dp[n][k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, m, nr, nr1, st, dr, i, j, z, p1, p2, aux, a[1004], b[1004], n     s[1004], d[1004]; n bool ok, c[1004]; n int main() { n   scanf( %d %d %d , &n, &x, &y); n   ok = true; n   y = 1; n   s[1] = 1; n   d[1] = n; n   while (ok == true) { n     nr1 = 0; n     nr = 0; n     for (i = 1; i <= y; i++) { n       if (s[i] != d[i]) { n         for (j = s[i]; j <= ((s[i] + d[i]) / 2); j++) b[++nr1] = j; n         for (j = ((s[i] + d[i]) / 2 + 1); j <= d[i]; j++) a[++nr] = j; n       } n     } n     printf( ? %d  , nr1); n     for (i = 1; i <= nr1; i++) printf( %d  , b[i]); n     printf(  n ); n     fflush(stdout); n     scanf( %d , &z); n     if (z != 0 && z != x) break; n     for (i = y; i >= 1; i--) { n       s[2 * i] = (s[i] + d[i]) / 2 + 1; n       d[2 * i] = d[i]; n       s[2 * i - 1] = s[i]; n       d[2 * i - 1] = (s[i] + d[i]) / 2; n     } n     y *= 2; n   } n   st = 1; n   dr = nr1; n   while (st <= dr) { n     m = (st + dr) / 2; n     printf( ? %d  , m); n     for (i = 1; i <= m; i++) printf( %d  , b[i]); n     printf(  n ); n     fflush(stdout); n     scanf( %d , &z); n     if (z != 0 && z != x) n       dr = m - 1; n     else n       st = m + 1; n   } n   p1 = b[st]; n   for (i = 1; i <= n; i++) c[i] = false; n   for (i = 1; i <= nr1; i++) c[b[i]] = true; n   if (y > 1) { n     for (i = 1; i <= y; i++) { n       if (s[i] <= p1 && p1 <= d[i]) break; n     } n     nr = 0; n     for (j = s[i]; j < p1; j++) { n       if (c[j] == false) a[++nr] = j; n     } n     for (j = (p1 + 1); j <= d[i]; j++) { n       if (c[j] == false) a[++nr] = j; n     } n   } n   st = 1; n   dr = nr; n   if (st < dr) { n     while (st <= dr) { n       m = (st + dr) / 2; n       printf( ? %d  , m); n       for (i = 1; i <= m; i++) printf( %d  , a[i]); n       printf(  n ); n       fflush(stdout); n       scanf( %d , &z); n       if (z != 0 && z != x) n         dr = m - 1; n       else n         st = m + 1; n     } n   } n   p2 = a[st]; n   if (p1 > p2) { n     aux = p1; n     p1 = p2; n     p2 = aux; n   } n   printf( ! %d %d n , p1, p2); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename A, size_t N, typename T> n void Fill(A (&array)[N], const T& val) { n   fill((T*)array, (T*)(array + N), val); n } n const int inf = INT_MAX / 2; n const long long int INF = LLONG_MAX / 2; n vector<int> g[100010]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   vector<pair<long long int, long long int> > check(m); n   for (int i = (0); i < (m); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n     check[i] = {x, y}; n   } n   int ans[100010] = {}; n   int idx = 0; n   for (int st = (1); st < (4); st++) { n     while (idx < n && ans[idx]) idx++; n     if (idx == n) { n       printf( -1 n ); n       return 0; n     } n     ans[idx] = st; n     bool used[100010] = {}; n     used[idx] = 1; n     for (int nxt : g[idx]) { n       used[nxt] = 1; n     } n     for (int i = (0); i < (n); i++) n       if (!used[i]) { n         if (ans[i] && ans[i] != st) { n           printf( -1 n ); n           return 0; n         } n         ans[i] = st; n       } n   } n   bool f = 1; n   int cnt[4] = {}; n   for (int i = (0); i < (n); i++) { n     if (ans[i] == 0) f = 0; n     cnt[ans[i]]++; n   } n   for (int i = (0); i < (m); i++) { n     if (ans[check[i].first] == ans[check[i].second]) f = 0; n   } n   if (m != cnt[1] * cnt[2] + cnt[2] * cnt[3] + cnt[3] * cnt[1]) f = 0; n   if (f) n     for (int i = (0); i < (n); i++) printf( %d n , ans[i]); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void prin(int n) { printf( %d n , n); } n int main() { n   int n; n   cin >> n; n   long long cur = 3, mi = 7; n   for (int i = 1; i < 3 * n; i++) { n     cur *= 3; n     cur %= (int)1e9 + 7; n   } n   for (int i = 1; i < n; i++) { n     mi *= 7; n     mi %= (int)1e9 + 7; n   } n   cout << (((cur - mi) % ((int)1e9 + 7)) + ((int)1e9 + 7)) % ((int)1e9 + 7); n } n