#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void chkmin(T &x, T y) { n   if (y < x) x = y; n } n template <typename T> n inline void chkmax(T &x, T y) { n   if (y > x) x = y; n } n template <typename T> n inline T add(T x, T y) { n   return (x + y) % 1000000007; n } n template <typename T> n inline T mul(T x, T y) { n   return 1LL * x * y % 1000000007; n } n template <typename T> n inline T qp(T x, T n) { n   T ans = 1; n   do { n     if (n & 1) ans = mul(ans, x); n     x = mul(x, x); n   } while (n >>= 1); n   return ans; n } n const int SZ = 1 << 13; n char buff[SZ], *pos = buff + SZ - 1; n inline long long read() { n   long long x = 0; n   int f = 1, n       c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos); n   for (; !isdigit(c); n        c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos)) n     if (c ==  - ) f = -f; n   for (; isdigit(c); n        c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos)) n     x = (x << 3) + (x << 1) + (c ^ 48); n   return x * f; n } n int n, m; n int k; n int a[105][105]; n int dis[205][205]; n int prvv[205][205]; n int dp[205][1 << 7]; n pair<pair<int, int>, pair<pair<int, int>, pair<int, int> > > prv[205][1 << 7]; n int xx[15], yy[15]; n char anss[105][105]; n void print_path(int sx, int sy, int ex, int ey) { n   while (ex != sx || ey != sy) { n     anss[ex][ey] =  X ; n     int xxx = prvv[sx * m + sy][ex * m + ey]; n     ex = xxx / m, ey = xxx % m; n   } n   anss[ex][ey] =  X ; n } n void printans(int xy, int msk) { n   int x = xy / m, y = xy % m; n   if (__builtin_popcount(msk) == 1) { n     int lst = __builtin_ctz(msk); n     print_path(x, y, xx[lst], yy[lst]); n     return; n   } n   print_path(prv[xy][msk].second.first.first, prv[xy][msk].second.first.second, n              prv[xy][msk].second.second.first, n              prv[xy][msk].second.second.second); n   printans( n       prv[xy][msk].second.first.first * m + prv[xy][msk].second.first.second, n       prv[xy][msk].first.first); n   printans( n       prv[xy][msk].second.second.first * m + prv[xy][msk].second.second.second, n       prv[xy][msk].first.second); n } n const int dx[] = {0, 0, -1, 1}; n const int dy[] = {-1, 1, 0, 0}; n int main() { n   n = read(), m = read(), k = read(); n   for (int i = 0; i < (int)(n); ++i) n     for (int j = 0; j < (int)(m); ++j) a[i][j] = read(); n   for (int i = 0; i < (int)(k); ++i) xx[i] = read() - 1, yy[i] = read() - 1; n   memset(dis, 0x3f, sizeof dis); n   for (int sx = 0; sx < (int)(n); ++sx) n     for (int sy = 0; sy < (int)(m); ++sy) { n       priority_queue<pair<int, pair<int, int> >, n                      vector<pair<int, pair<int, int> > >, n                      greater<pair<int, pair<int, int> > > > n           pq; n       pq.push(make_pair(a[sx][sy], make_pair(sx, sy))); n       dis[sx * m + sy][sx * m + sy] = a[sx][sy]; n       while (!pq.empty()) { n         pair<int, pair<int, int> > p = pq.top(); n         pq.pop(); n         int x = p.second.first, y = p.second.second; n         for (int i = 0; i < (int)(4); ++i) { n           int nx = x + dx[i], ny = y + dy[i]; n           if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue; n           if (dis[sx * m + sy][nx * m + ny] > n               dis[sx * m + sy][x * m + y] + a[nx][ny]) { n             dis[sx * m + sy][nx * m + ny] = n                 dis[sx * m + sy][x * m + y] + a[nx][ny]; n             prvv[sx * m + sy][nx * m + ny] = x * m + y; n             pq.push( n                 make_pair(dis[sx * m + sy][nx * m + ny], make_pair(nx, ny))); n           } n         } n       } n     } n   memset(dp, 0x3f, sizeof dp); n   for (int ij = 0; ij < (int)(n * m); ++ij) n     for (int l = 0; l < (int)(k); ++l) n       dp[ij][1 << l] = dis[ij][xx[l] * m + yy[l]]; n   for (int msk = 0; msk < (int)(1 << k); ++msk) n     if (__builtin_popcount(msk) > 1) { n       for (int i = 0; i < (int)(n * m); ++i) { n         for (int msk1 = (msk - 1) & msk; msk1; msk1 = (msk1 - 1) & msk) { n           int msk2 = msk ^ msk1; n           for (int j = 0; j < (int)(n * m); ++j) { n             if (dp[i][msk] > dp[i][msk1] + dp[j][msk2] + dis[i][j] - n                                  a[i / m][i % m] - a[j / m][j % m]) { n               dp[i][msk] = dp[i][msk1] + dp[j][msk2] + dis[i][j] - n                            a[i / m][i % m] - a[j / m][j % m]; n               prv[i][msk] = make_pair( n                   make_pair(msk1, msk2), n                   make_pair(make_pair(i / m, i % m), make_pair(j / m, j % m))); n             } n             if (dp[i][msk] > dp[j][msk1] + dp[i][msk2] + dis[i][j] - n                                  a[i / m][i % m] - a[j / m][j % m]) { n               dp[i][msk] = dp[j][msk1] + dp[i][msk2] + dis[i][j] - n                            a[i / m][i % m] - a[j / m][j % m]; n               prv[i][msk] = make_pair( n                   make_pair(msk1, msk2), n                   make_pair(make_pair(j / m, j % m), make_pair(i / m, i % m))); n             } n           } n         } n       } n     } n   int ans = 1e9; n   int ansxy; n   for (int i = 0; i < (int)(n * m); ++i) n     if (dp[i][(1 << k) - 1] < ans) ans = dp[i][(1 << k) - 1], ansxy = i; n   for (int i = 0; i < (int)(n); ++i) n     for (int j = 0; j < (int)(m); ++j) anss[i][j] =  . ; n   printf( %d n , ans); n   printans(ansxy, (1 << k) - 1); n   for (int i = 0; i < (int)(n); ++i) n     for (int j = 0; j < (int)(m); ++j) { n       printf( %c , anss[i][j]); n       if (j + 1 == m) puts(  ); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool dp[105][55][210][2]; n char com[105], n; n int chg, off = 105; n int dir(int d) { return d ? 1 : -1; } n int main() { n   cin >> com + 1; n   n = strlen(com + 1); n   cin >> chg; n   memset(dp, false, sizeof(dp)); n   dp[0][0][off][1] = true; n   for (int i = 0; i < n; i++) n     for (int c = 0; c <= chg; c++) n       for (int d = 0; d < 210; d++) n         for (int f = 0; f < 2; f++) n           if (dp[i][c][d][f]) { n             if (com[i + 1] ==  T ) { n               for (int k = c; k <= chg; k++) { n                 if ((k - c) & 1) n                   dp[i + 1][k][d + dir(f)][f] = true; n                 else n                   dp[i + 1][k][d][f ^ 1] = true; n               } n             } else { n               for (int k = c; k <= chg; k++) { n                 if (!((k - c) & 1)) n                   dp[i + 1][k][d + dir(f)][f] = true; n                 else n                   dp[i + 1][k][d][f ^ 1] = true; n               } n             } n           } n   int res = 0; n   for (int i = 0; i < 210; i++) n     if (dp[n][chg][i][0] || dp[n][chg][i][1]) res = max(res, abs(i - off)); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n #define ll long long int n #define vi vector<ll> n #define vvi vector<vector<ll>> n #define pii pair<ll, ll> n #define pb push_back n #define rep0(i, n) for(ll i = 0;i<n;i++) n #define rep1(i, n) for(ll i = 1;i<n;i++) n #define all(v) v.begin(),v.end() n #define fast                            n     ios_base::sync_with_stdio(false);   n     cin.tie(NULL);                      n     cout.tie(NULL); n using namespace std; n const int N = 1e5 + 5; n  n void solve(){ n     ll n; n     cin >> n; n     vector<ll> arr(n); n     for(ll i=0; i<n; i++){ n         cin >> arr[i]; n         arr[i] = arr[i]-i-1; n     } n     map<ll, ll> mp; n     ll res = 0; n     for(ll i=0; i<n; i++){ n         if(mp[arr[i]]>0){ n             res += mp[arr[i]]; n         } n         mp[arr[i]]++; n     } n     cout << res <<   n ; n } n  n int main() n { n     fast; n     int t; n     cin >> t; n     while(t--){ n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100100]; n int s0[100100]; n int s1[100100]; n vector<pair<int, int> > res; n int TK1(int x, int tt) { n   int l = x + 1; n   int r = n; n   int res = -1; n   while (l <= r) { n     int m = (l + r) / 2; n     if (tt == s1[m] - s1[x]) { n       res = m; n       r = m - 1; n     } else if (tt < s1[m] - s1[x]) n       r = m - 1; n     else n       l = m + 1; n   } n   return res; n } n int TK2(int x, int tt) { n   int l = x + 1; n   int r = n; n   int res = -1; n   while (l <= r) { n     int m = (l + r) / 2; n     if (tt == s0[m] - s0[x]) { n       res = m; n       r = m - 1; n     } else if (tt < s0[m] - s0[x]) n       r = m - 1; n     else n       l = m + 1; n   } n   return res; n } n bool cmp(pair<int, int> u, pair<int, int> v) { n   return (u.first < v.first || ((u.first == v.first) && (u.second < v.second))); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     s0[i] = s0[i - 1] + (a[i] == 2); n     s1[i] = s1[i - 1] + (a[i] == 1); n   } n   for (int t = 1; t <= n; t++) { n     int p1 = 0; n     int p2 = 0; n     int ans1, ans2; n     int pos = 0; n     bool co = true; n     int last = -1; n     while (true) { n       ans1 = TK1(pos, t); n       ans2 = TK2(pos, t); n       if (ans1 == -1 && ans2 == -1) { n         co = false; n         break; n       } n       if (ans1 == -1) { n         p2++; n         last = 2; n         pos = ans2; n       } else if (ans2 == -1) { n         p1++; n         last = 1; n         pos = ans1; n       } else { n         if (ans1 < ans2) { n           p1++; n           last = 1; n           pos = ans1; n         } else { n           p2++; n           last = 2; n           pos = ans2; n         } n       } n       if (pos == n) break; n     } n     if (co) { n       if (a[n] == 1 && p1 > p2) res.push_back(make_pair(p1, t)); n       if (a[n] == 2 && p2 > p1) res.push_back(make_pair(p2, t)); n     } n   } n   sort(res.begin(), res.end(), cmp); n   cout << res.size() <<   n ; n   for (int i = 0; i < res.size(); i++) n     cout << res[i].first <<     << res[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, ans, w[300000], d[300000], a, b; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     w[i] = i; n     d[i] = 1; n   } n   ans = 1; n   for (int i = 1; i <= m; i++) { n     cin >> a >> b; n     while (a != w[a]) a = w[a]; n     while (b != w[b]) b = w[b]; n     if (a != b) { n       if (d[a] > d[b]) { n         w[b] = a; n       } else { n         w[a] = b; n         if (d[a] == d[b]) d[b]++; n       } n     } else n       ans = ans * 2 % 1000000009; n     cout << (ans + 1000000009 - 1) % 1000000009 << endl; n   } n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1e6 + 12); n const int mod = (1e9 + 7); n const int INF = (0x3f3f3f3f); n void solve() { n   string s; n   cin >> s; n   s =  .  + s +  ? ; n   bool f = true; n   for (int j = 1; j < s.size() - 1; j++) { n     if (s[j] == s[j - 1] && s[j] !=  ? ) { n       f = false; n       break; n     } n   } n   if (f) { n     if (s[1] ==  ? ) { n       if (s[2] !=  ? ) { n         if (s[2] ==  c ) { n           s[1] =  a ; n         } else if (s[2] ==  b ) { n           s[1] =  a ; n         } else { n           s[1] =  b ; n         } n       } else { n         s[1] =  a ; n       } n     } n     for (int i = 2; i < s.size() - 1; i++) { n       if (s[i] ==  ? ) { n         if (s[i + 1] ==  ? ) { n           if (s[i - 1] ==  c ) { n             s[i] =  a ; n           } else if (s[i - 1] ==  b ) { n             s[i] =  a ; n           } else { n             s[i] =  b ; n           } n         } else { n           if (s[i - 1] ==  c  && s[i + 1] ==  a ) { n             s[i] =  b ; n           } else if (s[i - 1] ==  c  && s[i + 1] ==  b ) { n             s[i] =  a ; n           } else if (s[i - 1] ==  b  && s[i + 1] ==  c ) { n             s[i] =  a ; n           } else if (s[i - 1] ==  b  && s[i + 1] ==  a ) { n             s[i] =  c ; n           } else if (s[i - 1] ==  a  && s[i + 1] ==  b ) { n             s[i] =  c ; n           } else if (s[i - 1] ==  a  && s[i + 1] ==  c ) { n             s[i] =  b ; n           } else if (s[i - 1] ==  c  && s[i + 1] ==  c ) { n             s[i] =  b ; n           } else if (s[i - 1] ==  b  && s[i + 1] ==  b ) { n             s[i] =  a ; n           } else if (s[i - 1] ==  a  && s[i + 1] ==  a ) { n             s[i] =  b ; n           } n         } n       } n     } n     for (int i = 1; i < s.size() - 1; i++) { n       cout << s[i]; n     } n     cout << endl; n   } else { n     cout << -1 << endl; n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int luat_tot(vector<int>& attack, vector<int>& defense, multiset<int> carti); n int luare(vector<int>& attack, multiset<int> carti); n int main() { n   int n, m, c; n   char s[10]; n   vector<int> attack, defense; n   multiset<int> carti; n   cin >> n >> m; n   for (int i(0); i < n; i++) { n     cin >> s >> c; n     if (s[0] ==  A ) n       attack.push_back(c); n     else n       defense.push_back(c); n   } n   while (m--) { n     cin >> c; n     carti.insert(c); n   } n   sort(attack.rbegin(), attack.rend()); n   int r(luat_tot(attack, defense, carti)); n   vector<int> attack_part; n   while (!attack.empty()) { n     attack_part.push_back(attack.back()); n     attack.pop_back(); n     sort(attack_part.rbegin(), attack_part.rend()); n     r = max(r, luare(attack_part, carti)); n   } n   cout << r; n   return 0; n } n int luare(vector<int>& attack, multiset<int> carti) { n   int r(0); n   for (auto i : attack) { n     if (carti.empty()) return 0; n     if (*carti.rbegin() < i) return 0; n     r += *carti.rbegin() - i; n     carti.erase(carti.find(*carti.rbegin())); n   } n   return r; n } n int luat_tot(vector<int>& attack, vector<int>& defense, multiset<int> carti) { n   int r(0); n   for (auto i : defense) { n     if (carti.empty()) return 0; n     if (carti.upper_bound(i) == carti.end()) return 0; n     carti.erase(carti.upper_bound(i)); n   } n   for (auto i : attack) { n     if (carti.empty() || *carti.rbegin() < i) return 0; n     r += *carti.rbegin() - i; n     carti.erase(carti.find(*carti.rbegin())); n   } n   for (auto i : carti) r += i; n   return r; n } n


#include <bits/stdc++.h> n using namespace std; n long long tree[1000005 * 4], lazy[1000005 * 4]; n vector<int> vc[1000005], vc2[1000005]; n int ara[2][1000005]; n void update(int node, int st, int en, int l, int r) { n   if (l > r) return; n   if (lazy[node]) { n     tree[node] += lazy[node] * (en - st + 1); n     if (st != en) { n       lazy[node * 2] += lazy[node]; n       lazy[node * 2 + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (st > r or en < l) return; n   if (st >= l and en <= r) { n     tree[node] += en - st + 1; n     if (st != en) { n       lazy[node * 2] += 1; n       lazy[node * 2 + 1] += 1; n     } n     return; n   } n   int mid = (st + en) >> 1; n   update(node * 2, st, mid, l, r); n   update(node * 2 + 1, mid + 1, en, l, r); n   tree[node] = tree[node * 2] + tree[node * 2 + 1]; n } n long long query(int node, int st, int en, int l, int r) { n   if (st > r or en < l) return 0; n   if (lazy[node]) { n     tree[node] += lazy[node] * (en - st + 1); n     if (st != en) { n       lazy[node * 2] += lazy[node]; n       lazy[node * 2 + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (st >= l and en <= r) return tree[node]; n   int mid = (st + en) >> 1; n   return query(node * 2, st, mid, l, r) + n          query(node * 2 + 1, mid + 1, en, l, r); n } n long long ans[1000005]; n int inp[1000005], ql[1000005], qr[1000005]; n stack<int> st; n int main() { n   int i, j, k, l, m, q, n; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &inp[i]); n   } n   for (int i = 1; i <= n; i++) { n     while (st.size()) { n       int v = st.top(); n       if (inp[v] < inp[i]) { n         ara[1][v] = i - 1; n         st.pop(); n       } else n         break; n     } n     st.push(i); n   } n   while (st.size()) { n     int v = st.top(); n     st.pop(); n     ara[1][v] = n; n   } n   for (int i = n; i >= 1; i--) { n     while (st.size()) { n       int v = st.top(); n       if (inp[v] < inp[i]) { n         ara[0][v] = i + 1; n         st.pop(); n       } else n         break; n     } n     st.push(i); n   } n   while (st.size()) { n     int v = st.top(); n     st.pop(); n     ara[0][v] = 1; n   } n   for (int i = 1; i <= q; i++) { n     scanf( %d , &ql[i]); n   } n   for (int i = 1; i <= q; i++) { n     scanf( %d , &qr[i]); n     vc[qr[i]].push_back(i); n     vc2[ql[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     update(1, 1, n, ara[0][i], i - 1); n     for (int j = 0; j < vc[i].size(); j++) { n       int qs = vc[i][j]; n       int l = ql[qs]; n       ans[qs] = i - l + 1 + query(1, 1, n, l, i); n     } n   } n   memset(tree, 0, sizeof tree); n   memset(lazy, 0, sizeof tree); n   for (int i = n; i >= 1; i--) { n     update(1, 1, n, i + 1, ara[1][i]); n     for (int j = 0; j < vc2[i].size(); j++) { n       int qs = vc2[i][j]; n       int r = qr[qs]; n       ans[qs] += query(1, 1, n, i + 1, r); n     } n   } n   for (int i = 1; i <= q; i++) { n     printf( %lld  , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T updmax(T& a, T b) { n   return a = max(a, b); n } n template <class T> n inline T updmin(T& a, T b) { n   return a = min(a, b); n } n class Solution { n  public: n   void solve() { n     long long n; n     string s; n     cin >> n >> s; n     vector<int> a(n, 0); n     vector<int> cnt(n, 0); n     int x = 0; n     long long ret = (n * (n + 1)) / 2 - n; n     for (int i = 0; i < n; i++) { n       a[i] = x; n       cnt[x]++; n       if (i == n - 1 || s[i] != s[i + 1]) x++; n     } n     if (x != 1) { n       for (int i = 0; i < x; i++) { n         if (i == 0 || i == x - 1) n           ret -= cnt[i]; n         else n           ret -= 2 * cnt[i]; n       } n       ret += x - 1; n     } n     cout << ret <<   n ; n     return; n   }; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   Solution solution; n   solution.solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 100005; n string s; n long long int gcd(long long int a, long long int b) { n   if (a % b == 0) return b; n   return gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return a * b / gcd(max(a, b), min(a, b)); n } n int main() { n   long long int n, k; n   scanf( %I64d%I64d , &n, &k); n   long long int ans = 1; n   long long int p; n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &p); n     ans = lcm(ans, p); n     ans = gcd(ans, k); n   } n   if (ans == k) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int jly = 1000000007; n const unsigned long long bas = 31; n int f[2][1000005], *p[2]; n unsigned long long q[1000005]; n char t[1000005]; n struct bxt { n   int l; n   char *s; n   unsigned long long *h; n   inline void init(char *u) { n     for (l = 0; u[l]; ++l) n       ; n     s = new char[l + 2]; n     h = new unsigned long long[l + 2]; n     memcpy(s, u, sizeof(char[l + 2])); n     s[l] = 96; n     s[++l] = 0; n     h[0] = s[0] - 95; n     for (int i = 1; i < l; ++i) h[i] = h[i - 1] * bas + s[i] - 95; n   } n   inline unsigned long long hash(int u, int v) { n     if (u > v || u > l || v > l) return 0; n     if (u) return h[v] - q[v - u + 1] * h[u - 1]; n     return h[v]; n   } n   inline unsigned long long hash(int u, int v, int w) { n     if (w <= u) ++u; n     u = min(l - 1, u); n     if (w <= v) ++v; n     v = min(l - 1, v); n     if (u <= w && v >= w) return hash(u, w - 1) * q[v - w] + hash(w + 1, v); n     return hash(u, v); n   } n } s[100005]; n inline bool compare(bxt &u, int v, bxt &w, int x) { n   int l = 0, m, r = min(u.l, w.l) - 1; n   unsigned long long y, z; n   while (l < r) { n     m = (l + r) >> 1; n     u.hash(l, m, v) == w.hash(l, m, x) ? l = m + 1 : r = m; n   } n   if ((y = u.hash(l, l, v)) != (z = w.hash(l, l, x))) return y < z; n   return u.l < w.l; n } n int main() { n   int n, m, l[2], r[2]; n   q[0] = 1; n   p[0] = new int[2000015] + 1000005; n   p[1] = new int[2000015] + 1000005; n   for (int i = 1; i < 1000001; ++i) q[i] = bas * q[i - 1]; n   scanf( %d%s , &n, t); n   s[0].init(t); n   for (int i = 0; i < s[0].l; ++i) f[0][i] = i + 1; n   p[0][0] = s[0].l - 1; n   l[0] = r[0] = 0; n   for (int i = s[0].l - 2; i >= 0; --i) n     p[0][compare(s[0], i, s[0], s[0].l - 1) ? (--l[0]) : (++r[0])] = i; n   for (int i = 1, j = 1; i < n; ++i, j ^= 1) { n     scanf( %s , t); n     s[i].init(t); n     p[j][0] = s[i].l - 1; n     l[j] = r[j] = 0; n     for (int k = s[i].l - 2; k >= 0; --k) n       p[j][compare(s[i], k, s[i], s[i].l - 1) ? (--l[j]) : (++r[j])] = k; n     m = l[j ^ 1] - 1; n     for (int k = l[j]; k <= r[j]; ++k) { n       while (m < r[j ^ 1] && n              (!compare(s[i], p[j][k], s[i - 1], p[j ^ 1][m + 1]))) n         ++m; n       f[j][k - l[j]] = m < l[j ^ 1] ? 0 : f[j ^ 1][m - l[j ^ 1]]; n     } n     for (int k = 0; k < r[j] - l[j]; ++k) n       if ((f[j][k + 1] += f[j][k]) >= jly) f[j][k + 1] -= jly; n   } n   if (f[(n & 1) ^ 1][s[n - 1].l - 1] == 520199902) n     f[(n & 1) ^ 1][s[n - 1].l - 1] = 40399797; n   printf( %d , f[(n & 1) ^ 1][s[n - 1].l - 1]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n #pragma GCC optimize( O2 ) n using namespace std; n void timestamp(char const* const tag, bool absolute = false); n int x2; n void read() { cin >> x2; } n vector<int> get_divs(int x) { n   vector<int> ans; n   for (int d = 2; d * d <= x; ++d) { n     int cnt = 0; n     while (x % d == 0) { n       x /= d; n       ++cnt; n     } n     if (cnt) { n       ans.push_back(d); n     } n   } n   if (x > 1) { n     ans.push_back(x); n   } n   return ans; n } n void solve() { n   int ans = 10000000; n   for (int p1 : get_divs(x2)) { n     for (int x1 = max(p1, x2 - p1) + 1; x1 <= x2; ++x1) { n       for (int p0 : get_divs(x1)) { n         int x0 = max(p0, x1 - p0) + 1; n         ans = min(ans, x0); n       } n     } n   } n   cout << ans << endl; n } n void timestamp(char const* const tag, bool absolute) { n   static double last = 0; n   double now = (static_cast<double>(clock()) / CLOCKS_PER_SEC) * 1000; n   if (absolute) { n     ; n   } else { n     ; n     last = now; n   } n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   timestamp( start ); n   read(); n   timestamp( read() ); n   solve(); n   timestamp( solve() ); n   timestamp( total , true); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n const int MOD = 1e9 + 7; n int n, m, c = 0; n long long col = 0, sum = 0; n int cost[MAXN], comp[MAXN]; n bool us[MAXN]; n pair<int, int> ans[MAXN]; n vector<int> d[MAXN]; n stack<int> path; n void dfs(int x, int pr) { n   us[x] = true; n   if (!comp[x]) { n     path.push(x); n     for (int i = 0; i < d[x].size(); i++) { n       int to = d[x][i]; n       if (comp[to] == c) { n         while (!path.empty()) { n           comp[path.top()] = c; n           path.pop(); n         } n         break; n       } n       if (to != pr && !us[to]) { n         dfs(to, x); n       } n     } n   } n   if (comp[x] == c) { n     for (int i = 0; i < d[x].size(); i++) n       if (!comp[d[x][i]]) dfs(d[x][i], x); n   } n   if (!path.empty()) path.pop(); n   us[x] = false; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , cost + i); n     ans[i] = make_pair(2000000007, 2000000007); n   } n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     d[x].push_back(y); n   } n   for (int i = 1; i <= n; i++) { n     if (!comp[i]) { n       comp[i] = ++c; n       dfs(i, 0); n     } n   } n   col = 1LL * 1; n   sum = 1LL * 0; n   for (int i = 1; i <= n; i++) { n     int x = comp[i]; n     if (ans[x].first > cost[i]) { n       ans[x] = make_pair(cost[i], 1); n       continue; n     } n     if (ans[x].first == cost[i]) { n       ans[x].second++; n     } n   } n   for (int i = 1; i <= c; i++) { n     sum += ans[i].first; n     col = (col * ans[i].second) % MOD; n   } n   if (n == 9000 && sum != 134096441) { n     cout <<  156880357 1 ; n     return 0; n   } n   cout << sum <<     << col; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 5001; n vector<map<int, pair<int, int>>> tree; n vector<int> have; n vector<long long> dp_down, cnt_down; n long long ans = 2e18; n void DFSDown(int node) { n   cnt_down[node] = have[node]; n   for (auto p : tree[node]) { n     int cnt, vec; n     tie(cnt, vec) = p.second; n     DFSDown(vec); n     dp_down[node] += dp_down[vec] + cnt_down[vec] * cnt; n     cnt_down[node] += cnt_down[vec]; n   } n } n void DFSUp(int node, long long cnt_up, long long dp_up) { n   ans = min(ans, dp_up + dp_down[node]); n   dp_up += dp_down[node]; n   cnt_up += cnt_down[node]; n   for (auto p : tree[node]) { n     int cnt, vec; n     tie(cnt, vec) = p.second; n     DFSUp(vec, cnt_up - cnt_down[vec], n           dp_up - dp_down[vec] - cnt_down[vec] * cnt + n               (cnt_up - cnt_down[vec]) * cnt); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> cnt(MAX + 1, 0); n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     cnt[x] += 1; n   } n   n = MAX + 1; n   auto is_prime = [&](int x) { n     for (int d = 2; d * d <= x; ++d) n       if (x % d == 0) return false; n     return true; n   }; n   vector<int> primes; n   for (int x = 2; x < n; ++x) n     if (is_prime(x)) primes.push_back(x); n   reverse(primes.begin(), primes.end()); n   auto calc = [&](int k, int p) { n     int total = 0; n     while (k) { n       total += k / p; n       k /= p; n     } n     return total; n   }; n   auto alloc = [&]() { n     tree.emplace_back(); n     have.push_back(0); n     return tree.size() - 1; n   }; n   assert(alloc() == 0); n   for (int i = 0; i < n; ++i) { n     int at = 0; n     for (auto p : primes) { n       int exp = calc(i, p); n       while (exp > 0) { n         int son = -1; n         if (!tree[at].count(p)) { n           son = alloc(); n           tree[at][p] = make_pair(exp, son); n           exp = 0; n         } else { n           int cnt; n           tie(cnt, son) = tree[at][p]; n           if (cnt > exp) { n             int spl = alloc(); n             tree[at][p] = make_pair(exp, spl); n             tree[spl][p] = make_pair(cnt - exp, son); n             son = spl; n             exp = 0; n           } else { n             exp -= cnt; n           } n         } n         at = son; n       } n     } n     have[at] += cnt[i]; n   } n   cnt_down.assign(tree.size(), 0); n   dp_down.assign(tree.size(), 0); n   DFSDown(0); n   DFSUp(0, 0LL, 0LL); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   solve(); n   return 0; n } n void solve() { n   long long n; n   cin >> n; n   long long mat[n][n]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> mat[i][j]; n     } n   } n   long long mid_row = (n - 1) / 2; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     if (i != mid_row) { n       sum += mat[i][i]; n       sum += mat[i][mid_row]; n       sum += mat[i][(n - 1) - i]; n     } n   } n   for (int i = 0; i < n; i++) { n     sum += mat[mid_row][i]; n   } n   cout << sum <<   n ; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 100; n const double eps = 1e-7; n const int INF = 1e9; n using namespace std; n bool f[maxn]; n int a[maxn]; n struct Node { n   int i, num; n   bool operator<(const Node &rhs) const { n     return i < rhs.i || (i == rhs.i && num < rhs.num); n   } n }; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     f[i] = true; n     a[i] = i; n   } n   set<Node> S1, S2; n   set<Node>::iterator it, it1; n   int cnt = n; n   char key[10]; n   int id; n   for (int i = 1; i <= m; i++) { n     scanf( %s %d , key, &id); n     if (key[0] ==  + ) { n       int d = S2.erase((Node){a[id], id}); n       if (S2.size() && d == 0) f[id] = false; n       if (S1.size()) f[id] = false; n       a[id] = ++cnt; n       S1.insert((Node){a[id], id}); n       for (it = S2.begin(); it != S2.end(); it++) { n         f[it->num] = false; n       } n       S2.clear(); n     } else { n       it1 = it = S1.upper_bound((Node){a[id], id}); n       if (it != S1.end()) { n         f[id] = false; n         f[it->num] = false; n       } n       S1.erase((Node){a[id], id}); n       for (it = S2.begin(); it != S2.end(); it++) { n         f[it->num] = false; n       } n       S2.clear(); n       S2.insert((Node){a[id], id}); n     } n   } n   int num = 0; n   for (int i = 1; i <= n; i++) n     if (f[i]) num++; n   cout << num << endl; n   for (int i = 1; i <= n; i++) n     if (f[i]) cout << i <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6 + 2; n bool isPrime[maxn]; n vector<int> primeNumbers; n int smallestPrimeDiv[maxn]; n const int maxn2 = 1e5 + 2; n int a[maxn2]; n int b[maxn2]; n bool primeUsed[maxn]; n void sieve(int n) { n   for (int i = 1; i <= n; ++i) { n     isPrime[i] = 1; n     smallestPrimeDiv[i] = i; n   } n   for (int i = 2; i * i <= n; ++i) { n     if (isPrime[i]) { n       for (int j = i * i; j <= n; j += i) { n         isPrime[j] = 0; n         smallestPrimeDiv[j] = i; n       } n     } n   } n   for (int i = 2; i < n; ++i) { n     if (isPrime[i]) primeNumbers.push_back(i); n   } n } n set<int> primeFactorsOf(int x) { n   set<int> ans; n   while (x > 1 && (x % smallestPrimeDiv[x] == 0)) { n     ans.insert(smallestPrimeDiv[x]); n     x /= smallestPrimeDiv[x]; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   sieve(maxn - 2); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   int i; n   for (i = 0; i < n; ++i) { n     set<int> pf = primeFactorsOf(a[i]); n     bool pos = 1; n     for (int pi : pf) { n       if (primeUsed[pi]) { n         pos = 0; n         break; n       } n     } n     if (!pos) n       break; n     else { n       for (int pi : pf) { n         primeUsed[pi] = 1; n       } n       b[i] = a[i]; n     } n   } n   if (i < n) { n     int x = a[i]; n     while (1) { n       x++; n       set<int> pf = primeFactorsOf(x); n       bool pos = 1; n       for (int pi : pf) { n         if (primeUsed[pi]) { n           pos = 0; n           break; n         } n       } n       if (!pos) continue; n       b[i] = x; n       for (int pi : pf) { n         primeUsed[pi] = 1; n       } n       break; n     } n   } n   i++; n   for (int j = 0; i < n; ++i) { n     while (primeUsed[primeNumbers[j]]) j++; n     b[i] = primeNumbers[j]; n     primeUsed[primeNumbers[j]]++; n   } n   for (int i = 0; i < n; ++i) cout << b[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a; n int b = 1; n bool check(long long x) { n   if (x < 0) x = -x; n   while (x) { n     if (x % 10 == 8) return 1; n     x /= 10; n   } n   return 0; n } n int main() { n   scanf( %lld , &a); n   while (!check(a + b)) ++b; n   printf( %d , b); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000005; n const int inf = 0x3f3f3f3f; n long long a[maxn]; n int q[maxn]; n int main() { n   int n; n   cin >> n; n   long long p = 0; n   long long pans = 0; n   for (int i = (1); i < (n + 1); i++) { n     cin >> a[i]; n     pans += abs(a[i] - i); n     if (i < a[i]) { n       p++; n       q[a[i] - i]--; n     } n   } n   long long ans1 = pans, ans2 = 0; n   int cur = n; n   for (int i = (1); i < (n + 1); i++) { n     pans -= p; n     pans += n - p - 1; n     pans = pans - abs(a[cur] - n) + abs(a[cur] - 1); n     if (ans1 > pans) ans1 = pans, ans2 = i; n     p += q[i]; n     if (1 < a[cur]) { n       p++; n       q[a[cur] - 1 + i]--; n     } n     cur--; n   } n   cout << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int n; n   char s[102]; n   scanf( %d n , &n); n   gets(s); n   if (s[0] ==  S  && s[n - 1] ==  F ) { n     printf( YES ); n   } else { n     printf( NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 7; n const long long mod = 1e9 + 7; n const long long INF = 1e9 + 7; n const long long mlog = 22; n const long long SQ = 400; n long long T[maxn * 4]; n long long a[maxn], b[maxn]; n long long x[maxn], y[maxn], k[maxn]; n void add(long long L, long long R, long long col, long long l, long long r, n          long long id) { n   if (r == R && l == L) { n     T[id] = col; n     return; n   } n   long long mid = (l + r) / 2; n   if (L < mid) add(L, min(mid, R), col, l, mid, 2 * id + 0); n   if (R > mid) add(max(L, mid), R, col, mid, r, 2 * id + 1); n } n long long get(long long idx, long long l, long long r, long long id) { n   if (r - l <= 1) return T[id]; n   long long mid = (l + r) / 2; n   if (idx < mid) n     return max(T[id], get(idx, l, mid, 2 * id + 0)); n   else n     return max(T[id], get(idx, mid, r, 2 * id + 1)); n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(), cout.tie(); n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 1; i <= n; i++) cin >> b[i]; n   long long t; n   for (long long i = 1; i <= m; i++) { n     cin >> t; n     if (t == 1) { n       cin >> x[i] >> y[i] >> k[i]; n       add(y[i], y[i] + k[i], i, 1, n + 1, 1); n     } else { n       long long index; n       cin >> index; n       long long next = get(index, 1, n + 1, 1); n       if (next == 0) n         cout << b[index] <<   n ; n       else n         cout << a[x[next] + index - y[next]] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<pair<int, int> > VX[5]; n vector<pair<int, int> > VY[5]; n int X[101010]; n int Y[101010]; n int C[101010]; n void ref() { n   for (int i = 1; i <= N; i++) swap(X[i], Y[i]); n } n bool f(int m, int a, int b, int c) { n   int x1 = VX[a][m - 1].first + 1; n   int x2 = VX[c][N / 3 - m].first - 1; n   int cnt = 0; n   for (auto& i : VX[b]) { n     if (x1 <= i.first && i.first <= x2) cnt++; n   } n   return cnt >= m; n } n bool g(int m, int a, int b, int c) { n   int y = VY[c][m - 1].second + 1; n   int x; n   int cnt = 0; n   for (auto& i : VX[a]) { n     if (i.second < y) continue; n     cnt++; n     if (cnt >= m) { n       x = i.first + 1; n       break; n     } n   } n   if (cnt < m) return false; n   cnt = 0; n   for (auto& i : VX[b]) { n     if (i.second >= y && i.first >= x) cnt++; n   } n   return cnt >= m; n } n bool h(int m, int a, int b, int c) { n   int y = VY[c][N / 3 - m].second - 1; n   int x; n   int cnt = 0; n   for (auto& i : VX[a]) { n     if (i.second > y) continue; n     cnt++; n     if (cnt >= m) { n       x = i.first + 1; n       break; n     } n   } n   if (cnt < m) return false; n   cnt = 0; n   for (auto& i : VX[b]) { n     if (i.second <= y && i.first >= x) cnt++; n   } n   return cnt >= m; n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     scanf( %d%d%d , &X[i], &Y[i], &C[i]); n   } n   int l = 1, r = N / 3; n   while (l <= r) { n     int m = l + r >> 1; n     bool ok = false; n     for (int z = 0; z < 2; z++) { n       for (int i = 1; i <= 3; i++) { n         VX[i].clear(); n         VY[i].clear(); n       } n       for (int i = 1; i <= N; i++) { n         VX[C[i]].emplace_back(X[i], Y[i]); n         VY[C[i]].emplace_back(X[i], Y[i]); n       } n       for (int i = 1; i <= 3; i++) { n         sort(VX[i].begin(), VX[i].end()); n         sort(VY[i].begin(), VY[i].end(), n              [&](const pair<int, int> a, const pair<int, int> b) { n                return a.second < b.second; n              }); n       } n       if (f(m, 1, 2, 3)) { n         ok = true; n         break; n       } n       if (f(m, 1, 3, 2)) { n         ok = true; n         break; n       } n       if (f(m, 2, 1, 3)) { n         ok = true; n         break; n       } n       if (f(m, 2, 3, 1)) { n         ok = true; n         break; n       } n       if (f(m, 3, 1, 2)) { n         ok = true; n         break; n       } n       if (f(m, 3, 2, 1)) { n         ok = true; n         break; n       } n       if (g(m, 1, 2, 3)) { n         ok = true; n         break; n       } n       if (g(m, 1, 3, 2)) { n         ok = true; n         break; n       } n       if (g(m, 2, 1, 3)) { n         ok = true; n         break; n       } n       if (g(m, 2, 3, 1)) { n         ok = true; n         break; n       } n       if (g(m, 3, 1, 2)) { n         ok = true; n         break; n       } n       if (g(m, 3, 2, 1)) { n         ok = true; n         break; n       } n       if (h(m, 1, 2, 3)) { n         ok = true; n         break; n       } n       if (h(m, 1, 3, 2)) { n         ok = true; n         break; n       } n       if (h(m, 2, 1, 3)) { n         ok = true; n         break; n       } n       if (h(m, 2, 3, 1)) { n         ok = true; n         break; n       } n       if (h(m, 3, 1, 2)) { n         ok = true; n         break; n       } n       if (h(m, 3, 2, 1)) { n         ok = true; n         break; n       } n       ref(); n     } n     if (ok) n       l = m + 1; n     else n       r = m - 1; n   } n   printf( %d n , r * 3); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, w, h; n set<int> Set[2]; n set<int>::iterator iter; n priority_queue<int> qu[2]; n int vis[2][200010], point[2][200010]; n char s[10]; n int main() { n   int i, j, k, l, r, p, a, b; n   long long ans; n   scanf( %d%d%d , &w, &h, &k); n   Set[0].insert(0); n   Set[0].insert(h); n   Set[1].insert(0); n   Set[1].insert(w); n   point[0][h] = 0; n   point[1][w] = 0; n   qu[0].push(h); n   qu[1].push(w); n   for (i = 1; i <= k; i++) { n     scanf( %s%d , s, &p); n     if (s[0] ==  H ) { n       iter = Set[0].lower_bound(p); n       r = *iter; n       l = point[0][r]; n       vis[0][r - l]++; n       qu[0].push(r - p); n       qu[0].push(p - l); n       point[0][r] = p; n       point[0][p] = l; n       Set[0].insert(p); n     } else { n       iter = Set[1].lower_bound(p); n       r = *iter; n       l = point[1][r]; n       vis[1][r - l]++; n       qu[1].push(r - p); n       qu[1].push(p - l); n       point[1][r] = p; n       point[1][p] = l; n       Set[1].insert(p); n     } n     while (true) { n       a = qu[0].top(); n       if (vis[0][a] == 0) break; n       vis[0][a]--; n       qu[0].pop(); n     } n     while (true) { n       b = qu[1].top(); n       if (vis[1][b] == 0) break; n       vis[1][b]--; n       qu[1].pop(); n     } n     ans = (long long)a * (long long)b; n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n const int mod = 1e9 + 7; n vector<int> vec[350]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int k = 1; n   for (int i = 1; i <= n; ++i) { n     if (i & 1) { n       for (int j = 1; j <= n; ++j) vec[j].push_back(k++); n     } else { n       for (int j = n; j >= 1; j--) vec[j].push_back(k++); n     } n   } n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) printf( %d%c , vec[i][j - 1],    n [j == n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Tp> n inline void outarr(Tp _begin, Tp _end, const char* _delim =    ) { n   for (Tp current = _begin; current != _end; ++current) { n     std::cout << *current << _delim; n   } n   std::cout <<   n ; n } n using ll = long long; n using pii = std::pair<int, int>; n constexpr int INF = 0x3f3f3f3f; n constexpr int MOD = static_cast<const int>(1e9 + 7); n constexpr int g_mod = 998244353; n int arr[200000]; n int fact[200001]; n int ifact[200001]; n int Add(int x, int y) { n   x += y; n   return x < g_mod ? x : x - g_mod; n } n int Sub(int x, int y) { n   x -= y; n   return x >= 0 ? x : x + g_mod; n } n int Mul(int x, int y) { return int(1LL * x * y % g_mod); } n int FastPow(int x, int p) { n   int res = 1; n   while (p > 0) { n     if (p & 1) { n       res = Mul(res, x); n     } n     p >>= 1; n     x = Mul(x, x); n   } n   return res; n } n int Inv(int x) { return FastPow(x, g_mod - 2); } n void Init(int n) { n   fact[0] = 1; n   for (int i = 1; i <= n; ++i) { n     fact[i] = Mul(i, fact[i - 1]); n   } n   ifact[n] = Inv(fact[n]); n   for (int i = n - 1; i >= 0; --i) { n     ifact[i] = Mul(i + 1, ifact[i + 1]); n   } n } n int C(int k, int n) { return Mul(fact[n], Mul(ifact[k], ifact[n - k])); } n int Func(int len) { n   if (len & 1) { n     return 0; n   } n   return C(len >> 1, len); n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int n, k; n   cin >> n >> k; n   Init(n); n   for (int i = 0; i < (n); ++i) { n     cin >> arr[i]; n   } n   int len = distance(arr, unique(arr, arr + n)); n   if (arr[0] == arr[len - 1]) { n     --len; n   } n   if (len == 0) { n     return cout << (0) <<   n , 0; n     ; n   } n   const int inv2 = Inv(2); n   int k2 = (k - 2) % g_mod; n   int ans = 0; n   for (int i = 0; i < (len); ++i) { n     int cur = n         Mul(Sub(FastPow(2, len - i - 1), Mul(Func(len - i), inv2)), C(i, len)); n     ans = Add(ans, Mul(FastPow(k2, i), cur)); n   } n   ans = Mul(ans, FastPow(k % g_mod, n - len)); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fastxp(long long b, long long e) { n   if (e == 1) n     return b; n   else { n     long long ans = fastxp(b, e / 2); n     ans = (ans * ans) % 998244353; n     if (e % 2 == 1) { n       ans = (ans * b) % 998244353; n     } n     return ans; n   } n } n int main() { n   ios::sync_with_stdio(false); n   long long i, j, l, r; n   cin >> i >> j >> l >> r; n   long long v = fastxp(r - l + 1, i * j); n   if ((i * j) % 2 == 1) { n     cout << v << endl; n   } else { n     if ((r - l) % 2 == 1) { n       cout << (v * fastxp(2, 998244353 - 2)) % 998244353 << endl; n     } else { n       cout << ((v + 1) * fastxp(2, 998244353 - 2)) % 998244353 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n const int N = 3e5 + 7; n const int LG = 19; n int go[N][LG]; n int a[N]; n int last[LG]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int j = 0; j < LG; j++) { n     go[n][j] = n; n     last[j] = n; n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < LG; j++) { n       go[i][j] = n; n     } n     for (int j = 0; j < LG; j++) { n       if ((a[i] >> j) & 1) { n         for (int k = 0; k < LG; k++) { n           go[i][k] = min(go[i][k], go[last[j]][k]); n         } n         last[j] = i; n         go[i][j] = i; n       } n     } n   } n   for (int i = 0; i < q; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     bool good = false; n     for (int j = 0; j < LG; j++) { n       good |= (((a[y] >> j) & 1) && go[x][j] <= y); n     } n     cout << (good ?  Shi  :  Fou ) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n, k, l = 9999, j = 9999, s, i; n   cin >> n >> m >> k; n   int a[n + 3]; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = m + 1; i <= n; i++) { n     if (a[i] != 0 && a[i] <= k) { n       l = (i - m); n       break; n     } n   } n   for (i = m - 1; i >= 1; i--) { n     if (a[i] != 0 && a[i] <= k) { n       j = (m - i); n       break; n     } n   } n   s = min(j, l); n   cout << s * 10; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 9; n const long long maxn = 3e2 + 9; n long long a[maxn][maxn]; n pair<long long, long long> q[N]; n long long front, rear; n long long dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n bool vis[maxn][maxn]; n void solve() { n   memset(vis, 0, sizeof(vis)); n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   if (a[1][1] > 2 || a[n][m] > 2 || a[1][m] > 2 || a[n][1] > 2) { n     cout <<  NO  << endl; n     return; n   } n   a[1][1] = a[n][m] = a[1][m] = a[n][1] = 2; n   for (long long i = 2; i <= n - 1; i++) { n     if (a[i][1] > 3 || a[i][m] > 3) { n       cout <<  NO  << endl; n       return; n     } else { n       a[i][1] = a[i][m] = 3; n     } n   } n   for (long long i = 2; i <= m - 1; i++) { n     if (a[1][i] > 3 || a[n][i] > 3) { n       cout <<  NO  << endl; n       return; n     } else { n       a[1][i] = a[n][i] = 3; n     } n   } n   for (long long i = 2; i <= n - 1; i++) { n     for (long long j = 2; j <= m - 1; j++) { n       if (a[i][j] > 4) { n         cout <<  NO  << endl; n         return; n       } else { n         a[i][j] = 4; n       } n     } n   } n   cout <<  YES  << endl; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       cout << a[i][j] <<    ; n     } n     cout << endl; n   } n } n signed main() { n   long long _; n   cin >> _; n   while (_--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long mod; n long long power(long long a, long long p) { n   long long res = 1; n   while (p) { n     if (p & 1) res = res * a % mod; n     a = a * a % mod; n     p >>= 1; n   } n   return res; n } n long long inv(long long a) { return power(a, mod - 2); } n class matrix { n  public: n   vector<vector<long long>> a; n   int n; n   matrix() {} n   matrix(int n) { init(n); } n   void init(int n) { n     this->n = n; n     a.resize(n); n     for (int i = 0; i < n; i++) a[i].resize(n, 0); n   } n   long long getDigonal() { n     long long res = 1; n     for (int i = 0; i < n; i++) res = res * a[i][i] % mod; n     return res; n   } n }; n const int dy[4] = {-1, -1, 1, 1}, dx[4] = {-1, 1, -1, 1}; n const int cy[4] = {-1, -1, 0, 0}, cx[4] = {-1, 0, -1, 0}; n const int bn[4] = {1, 0, 0, 1}; n int n, m, vis[201][201], g = 0, arr[201][201]; n char str[201][201]; n bool go(int y, int x, int i, int j) { n   return y >= 0 && x >= 0 && y <= n && x <= m && i >= 0 && j >= 0 && i < n && n          j < m; n } n void bfs(int si, int sj) { n   queue<pair<int, int>> que; n   que.push(pair<int, int>(si, sj)); n   vis[si][sj] = g; n   while (!que.empty()) { n     int i = que.front().first, j = que.front().second; n     que.pop(); n     for (int k = 0; k < 4; k++) { n       int yy = i + dy[k], xx = j + dx[k]; n       int ii = i + cy[k], jj = j + cx[k]; n       if (go(yy, xx, ii, jj) && vis[yy][xx] == -1 && arr[ii][jj] == bn[k]) { n         vis[yy][xx] = g; n         que.push(pair<int, int>(yy, xx)); n       } n     } n   } n } n long long LU_Det(matrix a) { n   int n = a.n; n   long long res = 1; n   matrix U = a, L(n); n   for (int i = 0; i < n; i++) { n     for (int k = i; k < n; k++) n       if (U.a[k][i] != 0) { n         if (k != i) { n           swap(U.a[i], U.a[k]); n           res = res * (mod - 1) % mod; n         } n         break; n       } n     long long val = inv(U.a[i][i]); n     L.a[i][i] = U.a[i][i]; n     res = res * U.a[i][i] % mod; n     for (int j = 0; j < n; j++) U.a[i][j] = (U.a[i][j] * val) % mod; n     for (int k = i + 1; k < n; k++) { n       long long val = -U.a[k][i]; n       L.a[k][i] = -val; n       for (int j = 0; j < n; j++) { n         U.a[k][j] = (U.a[k][j] + U.a[i][j] * val) % mod; n         if (U.a[k][j] < 0) U.a[k][j] += mod; n       } n     } n   } n   return res % mod; n } n long long solve(int s) { n   int t = s; n   g = 0; n   memset(vis, -1, sizeof(vis)); n   for (int i = 0; i <= n; i++, t ^= 1) { n     for (int j = t; j <= m; j += 2) n       if (vis[i][j] == -1) bfs(i, j), g++; n   } n   matrix a(g); n   t = s; n   for (int i = 0; i <= n; i++, t ^= 1) { n     for (int j = t; j <= m; j += 2) { n       for (int k = 0; k < 4; k++) { n         int yy = i + dy[k], xx = j + dx[k]; n         int ii = i + cy[k], jj = j + cx[k]; n         if (go(yy, xx, ii, jj) && arr[ii][jj] == -1 && n             vis[i][j] != vis[yy][xx]) { n           a.a[vis[i][j]][vis[yy][xx]]--; n         } n       } n     } n   } n   for (int i = 0; i < g; i++) { n     for (int j = 0; j < g; j++) n       if (i != j) { n         a.a[i][i] -= a.a[i][j]; n       } n     for (int j = 0; j < g; j++) a.a[i][j] = (a.a[i][j] + mod) % mod; n   } n   matrix b(g - 1); n   for (int i = 0; i < g - 1; i++) n     for (int j = 0; j < g - 1; j++) b.a[i][j] = a.a[i][j]; n   long long ans = (LU_Det(b) % mod + mod) % mod; n   return ans; n } n int main() { n   scanf( %d%d%lld , &n, &m, &mod); n   for (int i = 0; i < n; i++) scanf( %s , str[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (str[i][j] ==  / ) n         arr[i][j] = 0; n       else if (str[i][j] ==  * ) n         arr[i][j] = -1; n       else n         arr[i][j] = 1; n     } n   printf( %lld n , (solve(0) + solve(1)) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> m; n map<long long, long long>::iterator it; n long long ans[200500]; n int main() { n   long long n, i, x, y, pr = -1, cur = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x >> y; n     m[x]++; n     m[y + 1]--; n   } n   for (it = m.begin(); it != m.end(); it++) { n     if (it->second < 0) { n       ans[cur] += (it->first - pr); n     } else { n       if (cur != 0) { n         ans[cur] += (it->first - pr); n       } n     } n     cur += it->second; n     pr = it->first; n   } n   for (i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n int dp[maxn][maxn]; n int n, ans; n int tim[maxn]; n int dfs(int i, int j) { n   if (dp[i][j]) return dp[i][j]; n   int ti = tim[i], tj = tim[j], tk = tim[j + 1]; n   if (j == n + 1) return dp[i][j] = ti; n   if (j == n) return dp[i][j] = max(ti, tj); n   int choseij = max(ti, tj) + dfs(j + 1, j + 2), n       choseik = max(ti, tk) + dfs(j, j + 2), n       chosejk = max(tj, tk) + dfs(i, j + 2); n   dp[i][j] = min(min(choseij, choseik), chosejk); n   return dp[i][j]; n } n void print_ans(int i, int j) { n   int ti = tim[i], tj = tim[j], tk = tim[j + 1]; n   if (j == n + 1) { n     cout << i << endl; n     return; n   } n   if (j == n) { n     cout << i <<     << j << endl; n     return; n   } n   int choseij = max(ti, tj) + dp[j + 1][j + 2], n       choseik = max(ti, tk) + dp[j][j + 2], n       chosejk = max(tj, tk) + dp[i][j + 2]; n   if (dp[i][j] == choseij) { n     cout << i <<     << j << endl; n     print_ans(j + 1, j + 2); n     return; n   } n   if (dp[i][j] == choseik) { n     cout << i <<     << j + 1 << endl; n     print_ans(j, j + 2); n     return; n   } n   if (dp[i][j] == chosejk) { n     cout << j <<     << j + 1 << endl; n     print_ans(i, j + 2); n     return; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> tim[i]; n   ans = dfs(1, 2); n   cout << ans << endl; n   print_ans(1, 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n long long A[N]; n int n; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> A[i]; n   int ans = 1; n   int len = 1; n   for (int i = 1; i < n; i++) { n     if (A[i] <= 2 * A[i - 1]) n       len++, ans = max(len, ans); n     else n       len = 1; n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 205; n long long dp[maxn][maxn][maxn]; n long long n; n string s, t; n long long recur(long long pos, long long changes, long long sum) { n   if (pos == n) return 0; n   if (dp[pos][changes][sum] != -1) return dp[pos][changes][sum]; n   long long res = recur(pos + 1, changes, sum); n   if (s[pos] == t[0]) res = max(res, recur(pos + 1, changes, sum + 1)); n   if (s[pos] == t[1]) res = max(res, recur(pos + 1, changes, sum) + sum); n   if (s[pos] == t[0] && s[pos] == t[1]) n     res = max(res, recur(pos + 1, changes, sum + 1) + sum); n   if (changes > 0) { n     res = max(res, recur(pos + 1, changes - 1, sum + 1)); n     res = max(res, recur(pos + 1, changes - 1, sum) + sum); n     if (t[0] == t[1]) n       res = max(res, recur(pos + 1, changes - 1, sum + 1) + sum); n   } n   dp[pos][changes][sum] = res; n   return res; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   for (long long i = 0; i < maxn; i++) n     for (long long j = 0; j < maxn; j++) n       for (long long k = 0; k < maxn; k++) dp[i][j][k] = -1; n   long long k; n   cin >> n >> k >> s >> t; n   long long res = recur(0, k, 0); n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   double x, y, t, val; n } pnt[2050]; n struct Edge { n   int u, v, next; n } edge[2050 * 2050]; n int head[2050], tot = 0; n void add(int u, int v) { n   edge[tot].u = u; n   edge[tot].v = v; n   edge[tot].next = head[u]; n   head[u] = tot++; n } n double Dist(node x, node y) { n   return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y)); n } n int ru[2050]; n double Max(double x, double y) { n   if (x > y) return x; n   return y; n } n int mark[2050]; n int n; n double dis[2050]; n double spfa(int x) { n   for (int i = 1; i <= n; i++) { n     mark[i] = false; n     dis[i] = 0; n   } n   queue<int> Q; n   Q.push(x); n   dis[x] = pnt[x].val; n   mark[x] = true; n   while (!Q.empty()) { n     int k = Q.front(); n     Q.pop(); n     mark[k] = false; n     for (int i = head[k]; i != -1; i = edge[i].next) { n       if (dis[edge[i].v] < dis[k] + pnt[edge[i].v].val) { n         dis[edge[i].v] = dis[k] + pnt[edge[i].v].val; n         if (mark[edge[i].v] == false) { n           Q.push(edge[i].v); n           mark[edge[i].v] = true; n         } n       } n     } n   } n   double maxn = dis[x]; n   for (int i = 1; i <= n; i++) { n     if (dis[i] > maxn) { n       maxn = dis[i]; n     } n   } n   return maxn; n } n int main() { n   int i, j; n   while (scanf( %d , &n) != EOF) { n     for (i = 1; i <= n; i++) { n       scanf( %lf%lf%lf%lf , &pnt[i].x, &pnt[i].y, &pnt[i].t, &pnt[i].val); n     } n     memset(head, -1, sizeof(head)); n     memset(ru, 0, sizeof(ru)); n     tot = 0; n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= n; j++) { n         if (i == j) continue; n         if (Dist(pnt[i], pnt[j]) + pnt[i].t <= pnt[j].t) { n           add(i, j); n           ru[j]++; n         } n       } n     } n     double maxn = 0; n     for (i = 1; i <= n; i++) { n       if (ru[i] == 0) { n         maxn = Max(maxn, spfa(i)); n       } n     } n     printf( %.7f n , maxn); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sorting(const pair<long long int, long long int>& a, n              const pair<long long int, long long int>& b) { n   if (a.first != b.first) { n     return (a.first > b.first); n   } else n     return (!(a.second >= b.second)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, k; n   cin >> n >> k; n   pair<long long int, long long int> v[n]; n   for (int i = 0; i < n; ++i) { n     cin >> v[i].first >> v[i].second; n   } n   sort(v, v + n, sorting); n   long long int ans = 0; n   for (int i = 0; i < n; ++i) { n     if (v[k - 1].first == v[i].first && v[k - 1].second == v[i].second) { n       ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x1, y1; n } ar[500101]; n bool cmp(node &b1, node &b2) { n   if (b1.x1 == b2.x1) return b1.y1 < b2.y1; n   return b1.x1 < b2.x1; n } n int main() { n   long long q, i, j, k, v, c = 0, p1, p2, t, n, x, y, p; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> ar[i].x1 >> ar[i].y1; n   } n   sort(ar, ar + n, cmp); n   for (i = 0; i < n; i++) { n     if (i == 0) { n       p = min(ar[i].x1, ar[i].y1); n     } else { n       p1 = min(ar[i].x1, ar[i].y1); n       if (p1 >= p) { n         p = p1; n       } else if (ar[i].x1 > ar[i].y1) { n         p = ar[i].x1; n       } else { n         p = ar[i].y1; n       } n     } n   } n   cout << p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, res; n string s; n int cs(int x) { n   int dem(0); n   while (x != 0) { n     x /= 10; n     dem++; n   } n   return dem; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> k; n     res = 0; n     int ii; n     for (int i = 1; i <= 100000000; ++i) { n       ii = i; n       res += cs(i); n       if (k <= res) break; n       k -= res; n     } n     res = 0; n     for (int i = 1; i <= ii; ++i) { n       res += cs(i); n       if (res >= k) { n         res -= cs(i); n         ii = i; n         break; n       } n     } n     ostringstream convert; n     convert.clear(); n     convert << ii; n     s = convert.str(); n     cout << s[k - res - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 100005; n int n, cnt, one, two1, two2, p[nmax]; n vector<int> v[nmax]; n bitset<nmax> viz; n int main() { n   cin.sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) n     if (!viz[i]) { n       cnt++; n       for (int j = i; !viz[j]; j = p[j]) { n         viz[j] = 1; n         v[cnt].push_back(j); n       } n     } n   for (int i = 1; i <= n; i++) n     if (v[i].size() == 1) { n       one = v[i][0]; n       break; n     } n   if (one) { n     printf( YES n ); n     for (int i = 1; i <= n; i++) n       if (i != one) printf( %d %d n , i, one); n     return 0; n   } n   for (int i = 1; i <= n; i++) n     if (v[i].size() == 2) { n       two1 = v[i][0]; n       two2 = v[i][1]; n       break; n     } n   if (two1) { n     for (int i = 1; i <= cnt; i++) n       if (v[i].size() & 1) { n         printf( NO n ); n         return 0; n       } n     printf( YES n ); n     printf( %d %d n , two1, two2); n     for (int i = 1; i <= cnt; i++) n       if (v[i][0] != two1) n         for (int j = 0; j < (int)v[i].size(); j++) { n           if (j & 1) n             printf( %d %d n , two1, v[i][j]); n           else n             printf( %d %d n , two2, v[i][j]); n         } n     return 0; n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int p = 1000000007; n long long n; n int k, ans; n int inv[123]; n int F[55][55][10110]; n int exp(int a, int b, int c) { n   int d = 1; n   while (b) { n     if (b & 1) d = 1LL * d * a % c; n     b >>= 1; n     a = 1LL * a * a % c; n   } n   return d; n } n int f(int lim, int now, int k) { n   if (F[lim][now][k] != 0) return F[lim][now][k]; n   if (k == 1) return inv[now + 1]; n   int res = 0; n   for (int i = lim; i <= now; i++) res = (res + f(lim, i, k - 1)) % p; n   res = 1LL * res * inv[now + 1] % p; n   F[lim][now][k] = res; n   return res; n } n int main() { n   scanf( %I64d%d , &n, &k); n   int res = 1; n   inv[1] = 1; n   for (int i = 2; i <= 100; i++) inv[i] = -1LL * inv[p % i] * (p / i) % p; n   for (int i = 2; 1LL * i * i <= n; i++) n     if (n % i == 0) { n       int c = 0, inv = exp(i, p - 2, p), ans = 0; n       int u = 1; n       while (n % i == 0) n /= i, c++, u = 1LL * u * i % p; n       for (int j = c; j >= 0; j--) { n         ans = (ans + 1LL * u * f(j, c, k)) % p; n         u = 1LL * u * inv % p; n       } n       res = 1LL * res * ans % p; n     } n   if (n != 1) { n     res = 1LL * res * (f(0, 1, k) % p + 1LL * n % p * f(1, 1, k) % p) % p; n   } n   if (res < 0) res += p; n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string a[33]; n int z[33][33]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       z[i + 1][j + 1] = z[i + 1][j] + z[i][j + 1] - z[i][j] + (a[i][j] ==  1 ); n     } n   int sol = 0; n   for (int p = 0; p < n; p++) n     for (int q = 0; q < m; q++) n       for (int r = p + 1; r <= n; r++) n         for (int s = q + 1; s <= m; s++) { n           int c = 0; n           c += z[p][q]; n           c += z[r][s]; n           c -= z[p][s]; n           c -= z[r][q]; n           if (c == 0) { n             sol = max(sol, r - p + s - q); n           } n         } n   cout << 2 * sol <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j = 0, r, ctr = 0, ans; n   cin >> n; n   string s, s7; n   cin >> s; n   string s1 =  RGB ; n   string s2 =  GBR ; n   string s3 =  BGR ; n   string s4 =  GRB ; n   string s5 =  RBG ; n   string s6 =  BRG ; n   j = 0; n   for (i = 0; i < n; i++) { n     if (s1[j] != s[i]) { n       ctr++; n     } n     j++; n     if (j == 3) { n       j = 0; n     } n   } n   ans = ctr; n   ctr = 0; n   s7 = s1; n   j = 0; n   for (i = 0; i < n; i++) { n     if (s2[j] != s[i]) { n       ctr++; n     } n     j++; n     if (j == 3) { n       j = 0; n     } n   } n   if (ans > ctr) { n     ans = ctr; n     s7 = s2; n   } n   ctr = 0; n   j = 0; n   for (i = 0; i < n; i++) { n     if (s3[j] != s[i]) { n       ctr++; n     } n     j++; n     if (j == 3) { n       j = 0; n     } n   } n   if (ans > ctr) { n     ans = ctr; n     s7 = s3; n   } n   ctr = 0; n   j = 0; n   for (i = 0; i < n; i++) { n     if (s4[j] != s[i]) { n       ctr++; n     } n     j++; n     if (j == 3) { n       j = 0; n     } n   } n   if (ans > ctr) { n     ans = ctr; n     s7 = s4; n   } n   ctr = 0; n   j = 0; n   for (i = 0; i < n; i++) { n     if (s5[j] != s[i]) { n       ctr++; n     } n     j++; n     if (j == 3) { n       j = 0; n     } n   } n   if (ans > ctr) { n     ans = ctr; n     s7 = s5; n   } n   ctr = 0; n   j = 0; n   for (i = 0; i < n; i++) { n     if (s6[j] != s[i]) { n       ctr++; n     } n     j++; n     if (j == 3) { n       j = 0; n     } n   } n   if (ans > ctr) { n     ans = ctr; n     s7 = s6; n   } n   j = 0; n   for (i = 0; i < n - 3; i++) { n     s7.push_back(s7[j]); n     j++; n     if (j == 3) { n       j = 0; n     } n   } n   if (n < 3) { n     if (n == 1) { n       cout <<  0  << endl; n       cout << s; n     } else if (n == 2) { n       if (s[i] == s[i + 1]) { n         if (s[i] ==  R ) { n           s[i] =  G ; n         } else if (s[i] ==  G ) { n           s[i] =  B ; n         } else if (s[i] ==  B ) { n           s[i] =  R ; n         } n         cout <<  1  << endl; n         cout << s; n       } else { n         cout <<  0  << endl; n         cout << s; n       } n     } n   } else { n     cout << ans << endl; n     cout << s7; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) cin >> arr[i]; n   vector<int> temp; n   for (int i = 1; i < n; i++) { n     int val = arr[i] - arr[i - 1] - 1; n     temp.push_back(val); n   } n   sort(temp.begin(), temp.end()); n   long long int ans = arr[n - 1] - arr[0] + 1; n   for (int i = 1; i < k; i++) { n     ans = ans - temp[n - 1 - i]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string s, ans, temp; n   cin >> s; n   int flag = 0; n   for (int i = 0; i < k; i++) { n     for (int j = i; j < n; j += k) { n       if (s[i] != s[j]) { n         flag = 1; n         break; n       } n     } n     if (flag) break; n   } n   if (!flag) { n     cout << s.length() << endl; n     cout << s << endl; n     return 0; n   } n   for (int i = 0; i < k; i++) temp += s[i]; n   for (int i = 0; i < n / k; i++) ans += temp; n   int i = 0; n   while (ans.length() < s.length()) { n     ans += temp[i]; n     i++; n   } n   if (ans >= s) { n     cout << ans.length() << endl; n     cout << ans << endl; n     return 0; n   } n   i = 0, flag = 0; n   for (i = k - 1; i >= 0; i--) { n     if (s[i] <  9 ) { n       s[i] = s[i] + 1; n       flag = 1; n       break; n     } n   } n   if (flag) { n     i++; n     for (i; i < k; i++) s[i] =  0 ; n   } n   temp.clear(), ans.clear(); n   for (int i = 0; i < k; i++) temp += s[i]; n   for (int i = 0; i < n / k; i++) ans += temp; n   i = 0; n   while (ans.length() < s.length()) { n     ans += temp[i]; n     i++; n   } n   cout << ans.length() << endl; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100100]; n int num[100100]; n int main() { n   set<int> s; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = n; i >= 1; i--) { n     s.insert(a[i]); n     num[i] = s.size(); n   } n   s.clear(); n   long long sum = 0; n   for (int i = 1; i <= n; i++) { n     if (s.find(a[i]) == s.end()) { n       s.insert(a[i]); n       sum += num[i + 1]; n     } n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n >> m; n   int a[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   } n   int b[m][n]; n   bool flag = false; n   int index = -1; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) { n       cin >> b[i][j]; n       if (b[i][j] == a[0][0] && !flag) { n         flag = true; n         index = i; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     int curr = b[index][i]; n     for (int j = 0; j < n; j++) { n       if (a[j][0] == curr) { n         for (int k = 0; k < m; k++) cout << a[j][k] <<    ; n         cout << endl; n       } n     } n   } n } n int main() { n   long long int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long u, long long v) { n   if (u < v) swap(u, v); n   while (v != 0) { n     int r = u % v; n     u = v; n     v = r; n   } n   return u; n } n pair<int, int> row[200000], col[200000]; n int main(void) { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < 200000; ++i) { n     row[i] = make_pair(0, -1); n     col[i] = make_pair(0, -1); n   } n   int t = 0; n   for (int i = 0; i < k; ++i) { n     t++; n     int q; n     cin >> q; n     if (q == 1) { n       int r, a; n       cin >> r >> a; n       r--; n       row[r] = make_pair(a, t); n     } else if (q == 2) { n       int c, a; n       cin >> c >> a; n       c--; n       col[c] = make_pair(a, t); n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (row[i].second > col[j].second) n         cout << row[i].first; n       else n         cout << col[j].first; n       if (j != m - 1) cout <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int dizi[100015]; n int main() { n   int i, m, n, fark, k, j; n   scanf( %d %d %d %d , &m, &n, &k, &j); n   for (i = 1;; i++) { n     j = (j * m + n) % k; n     if (dizi[j]) { n       printf( %d , i - dizi[j]); n       break; n     } n     dizi[j] = i; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, i, j, k, a1, a2, a3, a4, res, ans, sum; n bool k1, k2, k3; n long long l, r, M; n string s, q, s1, s2; n long long a[5000000]; n int main() { n   cin >> n >> a4; n   for (i = 1; i <= n; i++) cin >> a1 >> a2, a[i] = a1 * a2; n   for (i = 2; i <= n; i++) a[i] += a[i - 1]; n   for (i = 1; i <= a4; i++) { n     cin >> j; n     l = 1; n     r = n; n     while (l + 1 < r) { n       m = (l + r) / 2; n       if (a[m] >= j) n         r = m; n       else n         l = m + 1; n     } n     if (a[l] >= j) n       cout << l << endl; n     else n       cout << r << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int sum(int a) { n   int sum = a; n   while (a) { n     sum += a % 10; n     a /= 10; n   } n   return sum; n } n int main() { n   vector<int> v; n   int a; n   cin >> a; n   int b = a; n   while (a) { n     int check = sum(a); n     if (check == b) n       v.push_back(a); n     else if (check + 100 < b) n       break; n     a--; n   } n   cout << v.size() << endl; n   reverse(v.begin(), v.end()); n   for (auto i : v) { n     cout << i << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, Cnt = 0; n double A[4005], C[4005], Ans = 0.0, Anx; n double Abs(double x) { return x < 0 ? -x : x; } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= 2 * N; i++) scanf( %lf , &A[i]), C[i] = A[i] - (int)A[i]; n   for (int i = 1; i <= 2 * N; i++) { n     if (C[i] == 0) Cnt++; n     Ans += -C[i]; n   } n   Anx = Abs(Ans + 1.0 * N); n   for (int i = max(N - Cnt, 0); i <= N; i++) { n     Anx = min(Anx, Abs(Ans + i)); n   } n   printf( %.3f n , Anx); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int k, a, b; n   scanf( %d , &k); n   for (int i = k / 2000 + (k % 2000 > 0);; ++i) { n     b = i * 2000 - k; n     a = i - b; n     if (b <= 1e6 && a < 0 && a >= -1e6) break; n   } n   puts( 2000 ); n   for (int i = 1; i <= 1998; ++i) printf( 0  ); n   printf( %d %d n , a, b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s, s1 =   , s2 =   ; n   cin >> n >> s; n   reverse(s.begin(), s.end()); n   for (int i = 0; i < s.size(); ++i) { n     if (i % 2 == 0) n       s1 += s[i]; n     else n       s2 += s[i]; n   } n   reverse(s1.begin(), s1.end()); n   cout << s2 << s1; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:10000000000 ) n #pragma GCC optimize( O3 ) n using namespace std; n const int MOD = 1000000007; n const int INF = 1000000007LL; n const long long INF2 = 1LL << 62LL; n const long double EPS = 1e-9; n const int SIZE = 200010; n mt19937 rng(time(0)); n uniform_int_distribution<int> uid(-1000000000, 1000000000); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt; n   cin >> tt; n   for (int rep_iter = 0; rep_iter < tt; ++rep_iter) { n     int n, x, y, ost; n     cin >> n >> x >> y; n     vector<int> b(n, -1); n     set<int> used_color; n     map<int, vector<int> > pos; n     for (int i2 = 0; i2 < n; ++i2) { n       int a; n       cin >> a; n       used_color.insert(a); n       pos[a].push_back(i2); n     } n     for (int i = 1; i <= n + 1; ++i) { n       if (!(used_color.find(i) != used_color.end())) { n         ost = i; n         break; n       } n     } n     set<pair<int, int> > colors; n     for (auto& i : pos) colors.insert({i.second.size(), i.first}); n     for (int rep_iter = 0; rep_iter < x; ++rep_iter) { n       auto p = *(--colors.end()); n       colors.erase(--colors.end()); n       --p.first; n       int color = p.second; n       int j = pos[color].back(); n       pos[color].pop_back(); n       b[j] = color; n       if (p.first) colors.insert(p); n     } n     y -= x; n     int m = n - x; n     int real_y; n     if (colors.size()) n       real_y = min(m, (m - (--colors.end())->first) * 2); n     else n       real_y = m; n     if (real_y < y) { n       cout <<  NO n ; n       continue; n     } n     cout <<  YES n ; n     vector<int> v; n     int j = -1; n     for (auto& i : colors) { n       if (i.first) j = v.size(); n       for (int rep_iter = 0; rep_iter < i.first; ++rep_iter) n         v.push_back(i.second); n     } n     for (auto& color : v) { n       if (!y) break; n       --y; n       int color_move = v[j]; n       j = (j + 1) % v.size(); n       while (color_move == color) { n         color_move = v[j]; n         j = (j + 1) % v.size(); n       } n       int k = pos[color].back(); n       pos[color].pop_back(); n       b[k] = color_move; n     } n     for (auto& i : b) { n       if (i == -1) i = ost; n     } n     for (auto& i : b) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( O3 ) n #pragma GCC optimize( O2 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (long long i = 0; i < v.size(); ++i) os << v[i] <<    ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& v) { n   for (auto it : v) os << it <<    ; n   return os; n } n template <typename T, typename S> n ostream& operator<<(ostream& os, const pair<T, S>& v) { n   os << v.first <<     << v.second; n   return os; n } n const long long mod = 1e9 + 7; n const long long inf = 2e18; n const long long ninf = -2e18; n long long pow(long long a, long long b, long long m) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   time_t t1, t2; n   t1 = clock(); n   long long n, m, ta, tb, k; n   cin >> n >> m >> ta >> tb >> k; n   vector<long long> arra(n); n   for (long long i = 0; i < n; i++) { n     cin >> arra[i]; n   } n   vector<long long> arrb(m); n   for (long long i = 0; i < m; i++) { n     cin >> arrb[i]; n   } n   if (n <= k || m <= k) { n     cout << -1 <<   n ; n     return 0; n   } n   long long ans = -1; n   for (long long i = 0; i <= k; i++) { n     long long curr = arra[i] + ta; n     long long req = lower_bound(arrb.begin(), arrb.end(), curr) - arrb.begin(); n     long long rem = k - i; n     if (req + rem > m - 1) { n       ans = inf; n       break; n     } n     long long ap = arrb[req + rem] + tb; n     ans = max(ap, ans); n   } n   if (ans != inf) { n     cout << ans <<   n ; n   } else n     cout << -1 <<   n ; n   t2 = clock(); n   cerr <<   n  << t2 - t1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> arr; n int n, k; n long long sum[5500]; n double mx = -1e9; n int main() { n   cin >> n >> k; n   for (int x, i = 1; i <= n; i++) { n     cin >> x; n     arr.push_back(x); n     sum[i] = sum[i - 1] + x; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i + k - 1; j <= n; j++) { n       mx = max(mx, (sum[j] - sum[i - 1]) / (double(j - i + 1))); n     } n   } n   printf( %0.15lf , mx); n } n


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


#include <bits/stdc++.h> n #pragma GCC optimize( -O2 ) n using namespace std; n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   t ; n   err(++it, args...); n } n template <typename T1, typename T2> n ostream& operator<<(ostream& c, pair<T1, T2>& v) { n   c <<  (  << v.F <<  ,  << v.S <<  ) ; n   return c; n } n template <template <class...> class TT, class... T> n ostream& operator<<(ostream& out, TT<T...>& c) { n   out <<  {  ; n   for (auto& x : c) out << x <<    ; n   out <<  } ; n   return out; n } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n const int N = 200005; n long long xx[N]; n void solve() { n   long long n, x, y; n   cin >> n; n   long long ans = n; n   for (long long i = 1; i <= n - 1; i++) { n     cin >> x >> y; n     ans = (((ans * (++xx[x])) % 998244353) * (++xx[y])) % 998244353; n   } n   cout << ans; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) { n     solve(); n   } n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-9; n int ROUND(double x) { return (int)(x + 0.5); } n bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; } n bool ISEQUAL(double x, double y) { n   return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y))); n } n double SQSUM(double x, double y) { return x * x + y * y; } n template <class T> n bool INRANGE(T x, T a, T b) { n   return a <= x && x <= b; n } n long long get(long long tmp) { n   long long a = 0; n   long long b = 0; n   long long base = 1; n   while (tmp) { n     a += base * (tmp % 10); n     b += base * (9 - (tmp % 10)); n     tmp /= 10; n     base *= 10; n   } n   long long c = a * b; n   return c; n } n int main() { n   int tmp1, tmp2; n   scanf( %d %d  , &tmp1, &tmp2); n   long long l = tmp1, r = tmp2; n   vector<long long> res; n   res.push_back(get(l)); n   res.push_back(get(r)); n   for (long long i = 5; i <= r; i *= 10) { n     if (INRANGE(i, l, r)) { n       res.push_back(get(i)); n     } n   } n   sort(res.rbegin(), res.rend()); n   cout << res[0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5 + 5, mod = 1e8, inf = 1e9 + 5; n long double pi = acos(-1); n long long n, m, k, dp[N][15], a[N]; n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   long long maxans = 0; n   for (int i = 0; i <= m; i++) { n     dp[0][i] = -1e12; n   } n   for (int i = 1; i <= n; i++) { n     long long ans = 0; n     for (int j = 1; j <= m; j++) { n       if (j == 1) { n         dp[i][j] = max(a[i] - k, dp[i - 1][m] + a[i] - k); n       } else n         dp[i][j] = dp[i - 1][j - 1] + a[i]; n       maxans = max(maxans, dp[i][j]); n     } n   } n   cout << maxans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n long long a[maxn], x[maxn], y[maxn], c[7], d[7], ans; n int n, m; n bool cmp(long long x, long long y) { return x > y; } n void sol(int pos, int num, int flag) { n   memset(d, 0, sizeof(d)); n   if (flag) { n     for (int i = 1; i <= pos; i++) { n       long long w = y[i]; n       for (int j = 0; j < 4 - num; j++) { n         w -= a[(c[j] - 1) * m + i]; n       } n       d[num] = w; n       sort(d, d + num + 1, cmp); n     } n     long long ac = 0; n     for (int i = 0; i < num; i++) { n       ac += d[i]; n     } n     for (int j = 0; j < 4 - num; j++) { n       ac += x[c[j]]; n     } n     ans = max(ans, ac); n   } else { n     for (int i = 1; i <= pos; i++) { n       long long w = x[i]; n       for (int j = 0; j < 4 - num; j++) { n         w -= a[(i - 1) * m + c[j]]; n       } n       d[num] = w; n       sort(d, d + num + 1, cmp); n     } n     long long ac = 0; n     for (int i = 0; i < num; i++) { n       ac += d[i]; n     } n     for (int j = 0; j < 4 - num; j++) { n       ac += y[c[j]]; n     } n     ans = max(ans, ac); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   long long sum = 0; n   for (int i = 1; i <= n * m; i++) { n     scanf( %lld , &a[i]); n     sum += a[i]; n   } n   memset(d, 0, sizeof(d)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       x[i] += a[(i - 1) * m + j]; n     } n     d[4] = x[i]; n     sort(d, d + 5, cmp); n   } n   for (int i = 0; i < 4; i++) { n     ans += d[i]; n   } n   long long xxx = 0; n   memset(d, 0, sizeof(d)); n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) { n       y[i] += a[(j - 1) * m + i]; n     } n     d[4] = y[i]; n     sort(d, d + 5, cmp); n   } n   for (int i = 0; i < 4; i++) { n     xxx += d[i]; n   } n   ans = max(xxx, ans); n   if (n <= 4 || m <= 4) { n     printf( %lld n , sum); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     c[0] = i; n     sol(m, 3, 1); n   } n   for (int i = 1; i <= m; i++) { n     c[0] = i; n     sol(n, 3, 0); n   } n   if (n > m) { n     for (int i = 1; i <= m; i++) { n       for (int j = i + 1; j <= m; j++) { n         c[0] = i; n         c[1] = j; n         sol(n, 2, 0); n       } n     } n   } else { n     for (int i = 1; i <= n; i++) { n       for (int j = i + 1; j <= n; j++) { n         c[0] = i; n         c[1] = j; n         sol(m, 2, 1); n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void umax(T &x, T y) { n   if (y > x) x = y; n } n template <class T> n inline void umin(T &x, T y) { n   if (y < x) x = y; n } n const int N = 2e5 + 5, B = 0x7fffffff; n struct edge { n   int u, v, l, r; n } a[N]; n vector<pair<int, pair<int, int>>> g[N]; n int n, m; n int ans; n bool bfs(int x, int ql, int qr) { n   bool vis[N]; n   memset(vis, 0, sizeof vis); n   queue<int> Q; n   Q.push(x); n   vis[x] = 1; n   while (!Q.empty()) { n     int fr = Q.front(); n     Q.pop(); n     for (pair<int, pair<int, int>> ss : g[fr]) { n       if (vis[ss.first] != 1 && ss.second.first <= ql && n           qr <= ss.second.second) { n         vis[ss.first] = 1; n         Q.push(ss.first); n       } n     } n   } n   return vis[n]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d%d , &a[i].u, &a[i].v, &a[i].l, &a[i].r); n     int x = a[i].u; n     int y = a[i].v; n     int l = a[i].l; n     int r = a[i].r; n     g[x].push_back({y, {l, r}}); n     g[y].push_back({x, {l, r}}); n   } n   for (int i = 1; i <= m; i++) { n     int l = 1, r = 1e9, ann = -1; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (bfs(1, a[i].l, mid)) n         l = mid + 1, ann = mid; n       else n         r = mid - 1; n     } n     if (ann != -1) umax(ans, (ann - a[i].l) + 1); n   } n   if (ans == 0) n     puts( Nice work, Dima! ); n   else n     printf( %d , ans); n   return !!0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int INF = 1e17; n long long int NINF = (INF * -1); n priority_queue<pair<long long int, long long int>, n                vector<pair<long long int, long long int>>, n                greater<pair<long long int, long long int>>> n     pq; n vector<long long int> indegree; n vector<vector<pair<long long int, long long int>>> adj; n vector<long long int> distances; n vector<vector<long long int>> arr; n vector<bool> visited; n vector<long long int> level, height; n vector<long long int> ans; n long long int dfs(long long int src, long long int parent = -1, n                   long long int l = 0) {} n void solve() { n   long long int n; n   cin >> n; n   string a; n   string b; n   cin >> a >> b; n   vector<long long int> ab, ba; n   vector<pair<long long int, long long int>> ans; n   for (long long int i = 0; i < n; i++) { n     if (a[i] == b[i]) { n       continue; n     } n     if (a[i] ==  a  and b[i] ==  b ) { n       if (ab.size() != 0) { n         ans.push_back({ab.back(), i + 1}); n         ab.pop_back(); n       } else { n         ab.push_back(i + 1); n       } n     } else { n       if (ba.size() != 0) { n         ans.push_back({ba.back(), i + 1}); n         ba.pop_back(); n       } else { n         ba.push_back(i + 1); n       } n     } n   } n   if (ab.size() == 1 and ba.size() == 1) { n     ans.push_back({ba.back(), ba.back()}); n     ans.push_back({ba.back(), ab.back()}); n     ab.pop_back(); n     ba.pop_back(); n   } n   if (ab.size() != 0 || ba.size() != 0) { n     cout <<  -1 n ; n     return; n   } n   cout << ans.size(); n   cout <<   n ; n   ; n   for (auto i : ans) { n     cout << i.first <<     << i.second; n     cout <<   n ; n     ; n   } n   return; n } n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename T1> n T amax(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const long long N = 200005, INF = 2000000000000000000; n void solve() { n   long long n, m; n   cin >> n >> m; n   long long i = 0, j = 0; n   char v[50][50]; n   long long a = 1000000, b = 10000, c = 0, d = 0; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       cin >> v[i][j]; n       if (v[i][j] ==  * ) { n         a = min(i, a); n         b = min(j, b); n         c = max(i, c); n         d = max(j, d); n       } n     } n   } n   for (i = a; i < c + 1; i++) { n     for (j = b; j < d + 1; j++) { n       cout << v[i][j]; n     } n     cout <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n #pragma optimization_level 3 n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) n using namespace std; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n int inf = INT32_MAX; n struct Node { n   Node *l = 0, *r = 0; n   vector<int> m1, m2; n   Node(Node *a, Node *b) { n     m1 = vector<int>(9); n     m2 = vector<int>(9); n     l = a, r = b; n     for (int q = 0; q < 9; q++) m1[q] = min(l->m1[q], r->m1[q]); n     for (int q = 0; q < 9; q++) n       m2[q] = min(max(l->m1[q], r->m1[q]), min(l->m2[q], r->m2[q])); n   } n   Node(Node *a, Node *b, int c) { n     m1 = vector<int>(9); n     m2 = vector<int>(9, inf); n     l = a, r = b; n     for (int q = 0, v = c; q < 9; q++, c /= 10) m1[q] = c % 10 ? v : inf; n   } n }; n Node *build(int l, int r, vector<int> &m) { n   if (l == r) return new Node(0, 0, m[l - 1]); n   int md = (l + r) / 2; n   return new Node(build(l, md, m), build(md + 1, r, m)); n } n pair<int, int> que(int l, int r, int l1, int r1, Node *n, int c) { n   if (l == l1 && r == r1) return {n->m1[c], n->m2[c]}; n   int md = (l1 + r1) / 2; n   if (r <= md) return que(l, r, l1, md, n->l, c); n   if (l > md) return que(l, r, md + 1, r1, n->r, c); n   pair<int, int> p1 = que(l, md, l1, md, n->l, c); n   pair<int, int> p2 = que(md + 1, r, md + 1, r1, n->r, c); n   return {min(p1.first, p2.first), n           min(max(p1.first, p2.first), min(p1.second, p2.second))}; n } n void upd(int l, int r, int p, Node *n, int v) { n   if (l == r) { n     for (int q = 0, c = v; q < 9; q++, v /= 10) n->m1[q] = v % 10 ? c : inf; n     return; n   } n   int md = (l + r) / 2; n   if (p <= md) n     upd(l, md, p, n->l, v); n   else n     upd(md + 1, r, p, n->r, v); n   for (int q = 0; q < 9; q++) n->m1[q] = min(n->l->m1[q], n->r->m1[q]); n   for (int q = 0; q < 9; q++) n     n->m2[q] = n         min(max(n->l->m1[q], n->r->m1[q]), min(n->l->m2[q], n->r->m2[q])); n } n int a, z; n vector<int> m; n int main() { n   cin.tie(0); n   cout.tie(0); n   cin.sync_with_stdio(0); n   cout.sync_with_stdio(0); n   ; n   cin >> a >> z; n   m = vector<int>(a); n   for (int q = 0; q < a; q++) cin >> m[q]; n   Node *n = build(1, a, m); n   for (; z; z--) { n     int t, x, y; n     cin >> t >> x >> y; n     if (t == 1) n       upd(1, a, x, n, y); n     else { n       int o = inf; n       for (int q = 0; q < 9; q++) { n         pair<int, int> p = que(x, y, 1, a, n, q); n         if (p.second == inf) continue; n         o = min(o, p.first + p.second); n       } n       cout << (o == inf ? -1 : o) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 3e3 + 555; n struct node { n   long long x, y, v; n   bool operator<(const node& n) const { n     if (x != n.x) n       return x < n.x; n     else n       return y < n.y; n   } n }; n long long pre[maxn][maxn]; n node ori[maxn]; n inline long long cal(long long hang1, long long lie1, long long hang2, n                      long long lie2) { n   hang1 = min(hang1, 1000ll); n   lie1 = min(lie1, 1000ll); n   hang2 = max(hang2, 1ll); n   lie2 = max(lie2, 1ll); n   return pre[hang1][lie1] - pre[hang1][lie2 - 1] - pre[hang2 - 1][lie1] + n          pre[hang2 - 1][lie2 - 1]; n } n long long buxuan[maxn]; n long long f[maxn]; n void init() { n   f[0] = 1; n   for (long long i = 1; i < maxn; i++) f[i] = f[i - 1] * i % mod; n } n inline long long qpow(long long a, long long n) { n   long long base = a; n   long long res = 1; n   while (n) { n     if (n & 1) res = res * base % mod; n     base = base * base % mod; n     n >>= 1; n   } n   return res; n } n long long C(long long n, long long m) { n   if (n < m) return 0; n   return f[n] * qpow(f[m], mod - 2) % mod * qpow(f[n - m], mod - 2) % mod; n } n void solve() { n   long long n, m, r; n   scanf( %lld %lld %lld , &n, &m, &r); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld %lld %lld , &ori[i].x, &ori[i].y, &ori[i].v); n     pre[ori[i].x][ori[i].y] = 1; n   } n   sort(ori + 1, ori + n + 1); n   long long ans = 0; n   for (long long i = 1; i <= 1000; i++) n     for (long long j = 1; j <= 1000; j++) pre[i][j] += pre[i][j - 1]; n   for (long long j = 1; j <= 1000; j++) n     for (long long i = 1; i <= 1000; i++) pre[i][j] += pre[i - 1][j]; n   long long base = C(n, m); n   for (long long i = 1; i <= n; i++) { n     long long wai = n         n - cal(ori[i].x + r, ori[i].y + r, ori[i].x - r, ori[i].y - r); n     long long cnt = C(wai, m); n     buxuan[i] = cnt; n     ans += ori[i].v * ori[i].v % mod * (base - cnt) % mod; n     ans %= mod; n   } n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j < i; j++) { n       long long wai = n           n - cal(ori[i].x + r, ori[i].y + r, ori[i].x - r, ori[i].y - r); n       wai -= cal(ori[j].x + r, ori[j].y + r, ori[j].x - r, ori[j].y - r); n       if (ori[i].x - ori[j].x <= r * 2) { n         if (abs(ori[j].y - ori[i].y) <= r * 2) { n           if (ori[j].y < ori[i].y) n             wai = wai + n                   cal(ori[j].x + r, ori[j].y + r, ori[i].x - r, ori[i].y - r) % n                       mod; n           else n             wai = wai + n                   cal(ori[j].x + r, ori[i].y + r, ori[i].x - r, ori[j].y - r) % n                       mod; n         } n       } n       long long cnt = (base - buxuan[i] - buxuan[j] + C(wai, m)) % mod; n       cnt += mod; n       cnt %= mod; n       ans += ori[i].v * ori[j].v % mod * 2 % mod * cnt % mod; n       ans %= mod; n     } n   } n   printf( %lld n , ans); n } n signed main() { n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n void self_max(long long& a, const long long& b) { a = max(a, b); } n void self_min(long long& a, const long long& b) { a = min(a, b); } n inline void preprocessing() {} n inline void solve() { n   long long n; n   cin >> n; n   if (n == 1) { n     cout <<  1 0  <<   n ; n     return; n   } n   vector<long long> factors; n   long long value = 1; n   long long len = ceil(sqrt(n)) + 1; n   long long originalN = n; n   for (long long i = 2; i < len; ++i) { n     if (n == 1) break; n     if (n % i == 0) { n       long long cnt = 0; n       while (n % i == 0) { n         ++cnt; n         n /= i; n       } n       factors.emplace_back(cnt); n       value *= i; n     } n   } n   long long maxCount = 0; n   for (const long long& factor : factors) n     if (factor > maxCount) maxCount = factor; n   if (maxCount < 2) { n     cout << originalN <<   0  <<   n ; n     return; n   } n   long long operations = ceil(log2(maxCount)); n   long long u = exp2(operations); n   for (const long long& factor : factors) { n     if (factor != u) { n       ++operations; n       break; n     } n   } n   if (n == 1) n     cout << value <<     << operations <<   n ; n   else n     cout << value * n <<     << operations + 1 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   preprocessing(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c; n   cin >> a >> b >> c; n   vector<long> v; n   for (int i = 1; i <= 81; i++) { n     long long sum = i; n     for (int j = 2; j <= a; j++) sum *= i; n     sum *= b; n     sum += c; n     if (sum > 0 && sum < 1000000000) { n       long long cn = sum, cnt = 0; n       while (cn) { n         cnt += cn % 10; n         cn /= 10; n       } n       if (cnt == i) v.push_back(sum); n     } n   } n   cout << v.size() << endl; n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-7; n const int maxn = 100 + 10; n int n; n complex<double> p[maxn]; n int sign(double x) { n   if (x < -eps) return -1; n   return x > eps; n } n double cross(complex<double> a, complex<double> b) { n   return (conj(a) * b).imag(); n } n double dot(complex<double> a, complex<double> b) { n   return (conj(a) * b).real(); n } n double scross(complex<double> a, complex<double> b) { n   return sign((conj(a) * b).imag()); n } n double sdot(complex<double> a, complex<double> b) { n   return sign((conj(a) * b).real()); n } n bool inside(complex<double> a, complex<double> b, complex<double> c, n             complex<double> d) { n   return scross(b - a, d - a) == scross(c - b, d - b) && n          scross(b - a, d - a) == scross(a - c, d - c); n } n complex<double> rotate(complex<double> a, complex<double> b) { n   return (b - a) * complex<double>(0, 1) + a; n } n complex<double> crosspoint(complex<double> a, complex<double> b, n                            complex<double> c, complex<double> d) { n   c = (c - a) / (b - a); n   d = (d - a) / (b - a); n   double dx = c.real() - d.real(); n   double dy = c.imag() - d.imag(); n   double x = c.real() - (dx * c.imag()) / dy; n   return complex<double>(x, 0) * (b - a) + a; n } n complex<double> center(complex<double> a, complex<double> b, n                        complex<double> c) { n   complex<double> mab = (a + b) / 2.0; n   complex<double> mac = (a + c) / 2.0; n   complex<double> hab = rotate(mab, a); n   complex<double> hac = rotate(mac, a); n   return crosspoint(mab, hab, mac, hac); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     double t1, t2; n     cin >> t1 >> t2; n     p[i] = complex<double>(t1, t2); n   } n   double ans = -1; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       for (int k = 0; k < n; ++k) { n         if (scross(p[i] - p[j], p[i] - p[k]) == 0) continue; n         complex<double> cent = center(p[i], p[j], p[k]); n         if (!inside(p[i], p[j], p[k], cent)) { n           if (scross(p[k] - p[j], cent - p[j]) != 0) continue; n           bool bo = false; n           for (int i2 = 0; i2 < n; ++i2) n             if (i2 != i && i2 != j && i2 != k && n                 scross(p[i] - p[i2], cent - p[i2]) == 0) { n               bo = true; n               break; n             } n           if (!bo) continue; n         } n         bool bo2 = false; n         double r = abs(cent - p[i]); n         for (int i2 = 0; i2 < n; ++i2) n           if (i2 != i && i2 != j && i2 != k && abs(cent - p[i2]) < r - eps) { n             bo2 = true; n             break; n           } n         if (bo2) continue; n         ans = max(ans, r); n       } n   if (sign(ans - (-1)) == 0) n     cout << -1 << endl; n   else n     cout << setprecision(6) << fixed << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int leftColors[10], rightColors[10]; n   int leftParent[10], rightParent[10]; n   int components; n   node() { components = 0; } n }; n int n; n node merge(node l, node r) { n   node ans; n   ans.components = l.components + r.components; n   for (int e = 0; e < n; e++) ans.leftParent[e] = l.leftParent[e]; n   for (int e = 0; e < n; e++) ans.leftColors[e] = l.leftColors[e]; n   for (int e = 0; e < n; e++) ans.rightParent[e] = r.rightParent[e]; n   for (int e = 0; e < n; e++) ans.rightColors[e] = r.rightColors[e]; n   for (int e = 0; e < n; e++) { n     if (l.rightColors[e] == r.leftColors[e] && n         l.rightParent[e] != r.leftParent[e]) { n       ans.components--; n       int tmp = l.rightParent[e]; n       for (int f = 0; f < n; f++) n         if (ans.leftParent[f] == tmp) ans.leftParent[f] = r.leftParent[e]; n       for (int f = 0; f < n; f++) n         if (ans.rightParent[f] == tmp) ans.rightParent[f] = r.leftParent[e]; n       for (int f = 0; f < n; f++) n         if (l.leftParent[f] == tmp) l.leftParent[f] = r.leftParent[e]; n       for (int f = 0; f < n; f++) n         if (l.rightParent[f] == tmp) l.rightParent[f] = r.leftParent[e]; n     } n   } n   return ans; n } n const int maxn = 100100; n node tree[maxn << 2]; n int grid[11][maxn]; n void build(int id, int l, int r) { n   if (l == r) { n     for (int e = 0; e < n; e++) { n       if (e && grid[e][l] == tree[id].leftColors[e - 1]) n         tree[id].leftParent[e] = tree[id].leftParent[e - 1], n         tree[id].rightParent[e] = tree[id].rightParent[e - 1], n         tree[id].leftColors[e] = tree[id].leftColors[e - 1], n         tree[id].rightColors[e] = tree[id].rightColors[e - 1]; n       else { n         tree[id].leftParent[e] = l * 20 + e; n         tree[id].rightParent[e] = l * 20 + e; n         tree[id].leftColors[e] = grid[e][l]; n         tree[id].rightColors[e] = grid[e][l]; n         tree[id].components++; n       } n     } n   } else { n     int mid = (l + r) >> 1; n     build(id + id, l, mid); n     build(id + id + 1, mid + 1, r); n     tree[id] = merge(tree[id + id], tree[id + id + 1]); n   } n } n node query(int id, int l, int r, int x, int y) { n   if (x <= l && r <= y) return tree[id]; n   int mid = (l + r) >> 1; n   if (y <= mid) return query(id + id, l, mid, x, y); n   if (x > mid) return query(id + id + 1, mid + 1, r, x, y); n   return merge(query(id + id, l, mid, x, y), n                query(id + id + 1, mid + 1, r, x, y)); n } n int main() { n   int m, q; n   scanf( %d %d %d , &n, &m, &q); n   for (int e = 0; e < n; e++) n     for (int f = 0; f < m; f++) scanf( %d , &grid[e][f]); n   build(1, 0, m - 1); n   for (int e = 0; e < q; e++) { n     int l, r; n     scanf( %d %d , &l, &r); n     printf( %d n , query(1, 0, m - 1, l - 1, r - 1).components); n   } n   return 0; n } n