#include <bits/stdc++.h> n const long long mod = 1000000007; n using namespace std; n int main() { n   long long ax, ay, bx, by, tx, ty; n   scanf( %I64d , &(ax)); n   scanf( %I64d , &(ay)); n   scanf( %I64d , &(bx)); n   scanf( %I64d , &(by)); n   scanf( %I64d , &(tx)); n   scanf( %I64d , &(ty)); n   long long n; n   scanf( %I64d , &(n)); n   vector<pair<long long, long long>> bot(n); n   vector<double> toT(n); n   vector<pair<double, int>> toA(n), toB(n); n   double sum = 0; n   for (int(i) = 0; (i) < n; (i)++) { n     scanf( %I64d , &(bot[i].first)); n     scanf( %I64d , &(bot[i].second)); n     toT[i] = sqrt((double)((bot[i].first - tx) * (bot[i].first - tx) + n                            (bot[i].second - ty) * (bot[i].second - ty))); n     sum += 2 * toT[i]; n     toA[i].first = sqrt((double)((bot[i].first - ax) * (bot[i].first - ax) + n                                  (bot[i].second - ay) * (bot[i].second - ay))); n     toA[i].first -= toT[i]; n     toA[i].second = i; n     toB[i].first = sqrt((double)((bot[i].first - bx) * (bot[i].first - bx) + n                                  (bot[i].second - by) * (bot[i].second - by))); n     toB[i].first -= toT[i]; n     toB[i].second = i; n   } n   sort((toA).begin(), (toA).end()); n   sort((toB).begin(), (toB).end()); n   double ans = 1e18; n   for (int i = 0; i < n; i++) { n     int j = toA[i].second; n     int k = toB[0].second; n     double tmp = sum + toA[i].first; n     ans = min(ans, tmp); n     tmp = sum + toB[i].first; n     ans = min(ans, tmp); n     if (j != k) n       tmp = sum + toA[i].first + toB[0].first; n     else n       tmp = sum + toA[i].first + toB[1].first; n     j = toB[i].second; n     ans = min(ans, tmp); n     k = toA[0].second; n     if (j != k) n       tmp = sum + toA[0].first + toB[i].first; n     else n       tmp = sum + toA[1].first + toB[i].first; n     ans = min(ans, tmp); n   } n   printf( %.10f , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5; n const long long M = 1e4; n const long long mod = 1e9 + 7; n const long long inf = 1e9; n long long read() { n   long long s = 0; n   register bool neg = 0; n   register char c = getchar(); n   for (; c <  0  || c >  9 ; c = getchar()) neg |= (c ==  - ); n   for (; c >=  0  && c <=  9 ; s = s * 10 + (c ^ 48), c = getchar()) n     ; n   s = (neg ? -s : s); n   return s; n } n long long a, s[N + 5], r[N + 5]; n long double val[N + 5]; n vector<long long> p[63], q[63]; n bool cmp(long long p1, long long p2) { return val[p1] < val[p2]; } n signed main() { n   a = read(); n   for (long long i = (1); i <= (a); ++i) { n     s[i] = read(); n     long long j = 0, x = s[i]; n     for (; x; ++j) n       if (x & (1ll << j)) { n         if ((x ^ (1ll << j))) q[j].push_back(i); n         x ^= (1ll << j); n       } n     --j; n     p[j].push_back(i); n   } n   long long L = 0; n   for (long long i = (61); i >= (0); --i) { n     if (!((long long)((p[i]).size()))) continue; n     if (((long long)((p[i]).size())) > ((long long)((q[i]).size())) + 1) { n       puts( No ); n       return 0; n     } n     sort(q[i].begin(), q[i].end(), cmp); n     r[p[i][0]] = L; n     val[p[i][0]] = val[L] - 1e18; n     L = p[i][0]; n     for (long long j = (1); j <= (((long long)((p[i]).size())) - 1); ++j) { n       long long k = p[i][j], t = q[i][j - 1]; n       val[k] = (val[t] + val[r[t]]) / 2; n       r[k] = r[t]; n       r[t] = k; n     } n   } n   puts( Yes ); n   for (long long i = L; i; i = r[i]) printf( %lld  , s[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, p, i, j, k, l; n   double sum = 0; n   int a[50]; n   long long b[51][51]; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   scanf( %d , &p); n   for (i = 0; i < n; i++) { n     double tmp = 0; n     for (j = 0; j <= n; j++) { n       for (k = 0; k <= p; k++) { n         b[j][k] = 0; n       } n     } n     b[0][0] = 1; n     for (j = 0; j < n; j++) { n       if (j == i) continue; n       for (k = n - 1; k >= 0; k--) { n         for (l = p; l >= 0; l--) { n           if (b[k][l] == 0) continue; n           if (l + a[j] <= p) b[k + 1][l + a[j]] += b[k][l]; n         } n       } n     } n     for (j = 0; j <= n; j++) { n       for (k = 0; k <= p - a[i]; k++) { n         double tmp2 = b[j][k]; n         for (l = 1; l <= j; l++) tmp2 *= l; n         for (l = 1; l <= n - j - 1; l++) tmp2 *= l; n         tmp += tmp2; n       } n     } n     for (j = 1; j <= n; j++) tmp /= j; n     sum += tmp; n   } n   printf( %lf n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n const int MAX = 1e6 + 1; n int n, f[MAX], dep[MAX]; n int u[MAX], pr[MAX]; n int stk[2][N], q[N << 1]; n bool mk[MAX], cmp[N]; n vector<int> a, adj[MAX], dv[MAX]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     a.push_back(0); n     cin >> a[i]; n   } n   fill(f, f + MAX, 0); n   fill(u, u + MAX, -1); n   fill(cmp, cmp + N, 1); n   for (int i = 0; i < n; i++) { n     int p1 = -1, p2 = -1; n     int x = a[i], y, z = 1; n     if (x == 1) { n       f[1]++; n       dv[1].push_back(1); n       continue; n     } n     for (int j = 2; j < a[i]; j++) { n       if (j * j > a[i]) break; n       if (x % j == 0) { n         y = 0; n         if (p1 == -1) n           p1 = j; n         else n           p2 = j; n         while (x % j == 0) { n           x /= j; n           y ^= 1; n         } n         if (y) n           z *= j; n         else { n           if (p2 == j) n             p2 = -1; n           else n             p1 = -1; n         } n       } n     } n     if (x != 1) { n       if (p1 == -1) n         p1 = x; n       else n         p2 = x; n       z *= x; n     } n     a[i] = z; n     u[z] = i; n     stk[0][i] = max(p1, 0); n     stk[1][i] = max(p2, 0); n     for (int j = 0; j < 2; j++) cmp[i] &= (stk[j][i] != 0); n     if (cmp[i]) n       for (int j = 0; j < 2; j++) dv[stk[j][i]].push_back(stk[j ^ 1][i]); n     else { n       dv[1].push_back(z); n       dv[z].push_back(1); n     } n     f[z]++; n   } n   if (f[1]) { n     cout << 1 <<   n ; n     return 0; n   } n   int mx = -1; n   for (int x = 0; x < MAX; x++) mx = max(mx, f[x]); n   if (mx > 1) { n     cout << 2 <<   n ; n     return 0; n   } n   fill(mk, mk + MAX, 0); n   for (int x = 0; x < MAX; x++) { n     for (auto& y : dv[x]) n       if (!mk[y]) { n         mk[y] = 1; n         adj[x].push_back(y); n       } n     for (auto& y : dv[x]) mk[y] = 0; n   } n   fill(pr, pr + MAX, 0); n   for (int x = 0; x < MAX; x++) n     for (auto& y : adj[x]) pr[y]++; n   int l, r, mn = MAX; n   mx = -1; n   for (int x = 0; x < MAX; x++) n     if (pr[x]) mx = max(mx, x); n   mx = sqrt(mx) + 1; n   fill(q, q + (N << 1), 0); n   for (int x = 0; x < MAX; x++) n     if (pr[x]) { n       if (x > mx) continue; n       fill(mk, mk + MAX, 0); n       fill(dep, dep + MAX, 0); n       l = 0; n       r = 0; n       dep[x] = 0; n       mk[x] = 1; n       q[r++] = x; n       q[r++] = x; n       while (l != r) { n         int y = q[l++]; n         int z = q[l++]; n         for (auto& w : adj[y]) { n           if (w == z) continue; n           if (mk[w]) { n             mn = min(mn, dep[w] + 1 + dep[y]); n             continue; n           } n           dep[w] = 1 + dep[y]; n           mk[w] = 1; n           q[r++] = w; n           q[r++] = y; n         } n       } n     } n   if (mn == MAX) mn = -1; n   cout << mn <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n set<string> name; n map<string, int> id; n string pos[N]; n string s, st, o; n int n, a[N], cnt; n void add(string x) { n   if (name.find(x) != name.end()) return; n   name.insert(x); n   cnt++; n   id[x] = cnt; n   pos[cnt] = x; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> st; n   add(st); n   cin >> n; n   getline(cin, st); n   for (int i = 1; i <= n; ++i) { n     getline(cin, st); n     stringstream ss(st); n     string tmp; n     vector<string> v; n     while (ss >> tmp) { n       v.emplace_back(tmp); n     } n     add(v[0]); n     if (v[1] ==  posted ) { n       v[3].pop_back(); n       v[3].pop_back(); n       add(v[3]); n       int u = id[v[0]], vv = id[v[3]]; n       if (u == 1 || vv == 1) { n         a[u] += 15, a[vv] += 15; n       } n     } n     if (v[1] ==  commented ) { n       v[3].pop_back(); n       v[3].pop_back(); n       add(v[3]); n       int u = id[v[0]], vv = id[v[3]]; n       if (u == 1 || vv == 1) { n         a[u] += 10, a[vv] += 10; n       } n     } n     if (v[1] ==  likes ) { n       v[2].pop_back(); n       v[2].pop_back(); n       add(v[2]); n       int u = id[v[0]], vv = id[v[2]]; n       if (u == 1 || vv == 1) { n         a[u] += 5, a[vv] += 5; n       } n     } n   } n   sort(pos + 2, pos + cnt + 1, [](string x, string y) { n     return a[id[x]] != a[id[y]] ? a[id[x]] > a[id[y]] : x < y; n   }); n   for (int i = 2; i <= cnt; ++i) cout << pos[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3002; n int dfn[N], low[N], c; n bool ipt[N]; n pair<int, int> edge[N]; n vector<pair<int, int> > p[N]; n void tarjan(int x, int pre) { n   dfn[x] = low[x] = ++c; n   for (__typeof(p[x].begin()) it = p[x].begin(); it != p[x].end(); ++it) { n     if (pre == (*it).second) continue; n     int y = (*it).first; n     if (dfn[y]) { n       low[x] = min(low[x], dfn[y]); n     } else { n       tarjan(y, (*it).second); n       low[x] = min(low[x], low[y]); n       if (dfn[x] < low[y]) ipt[(*it).second] = 1; n     } n   } n } n int n, m, k, f[N]; n void init() { n   for (int i = 1; i <= n; ++i) f[i] = i; n } n int finds(int x) { return x == f[x] ? x : f[x] = finds(f[x]); } n void unions(int x, int y) { n   x = finds(x); n   y = finds(y); n   f[x] = y; n } n bool vis[N]; n int dfs(int x, int y, int deep) { n   if (x == y) return deep; n   vis[x] = true; n   for (__typeof(p[x].begin()) e = p[x].begin(); e != p[x].end(); ++e) n     if (!vis[e->first]) { n       int g = dfs(e->first, y, deep + 1); n       if (g != -1) return g; n     } n   return -1; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   int x, y; n   for (int i = 0; i < m; ++i) { n     cin >> edge[i].first >> edge[i].second; n     x = edge[i].first; n     y = edge[i].second; n     p[x].push_back(make_pair(y, i)); n     p[y].push_back(make_pair(x, i)); n   } n   init(); n   for (int i = 1; i <= n; ++i) n     if (!dfn[i]) tarjan(i, -1); n   for (int i = 0; i < m; ++i) n     if (!ipt[i]) { n       unions(edge[i].first, edge[i].second); n     } n   for (int i = 0; i <= n; ++i) p[i].clear(); n   for (int i = 0; i < m; ++i) n     if (ipt[i]) { n       p[finds(f[edge[i].first])].push_back( n           make_pair(finds(f[edge[i].second]), -1)); n       p[finds(f[edge[i].second])].push_back( n           make_pair(finds(f[edge[i].first]), -1)); n     } n   cin >> m; n   for (int i = 0; i < m; ++i) { n     cin >> x >> y; n     for (int i = 0; i <= n; ++i) vis[i] = false; n     cout << dfs(finds(f[x]), finds(f[y]), 0) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dir; n int n, m, k; n int go(int pos) { n   if (pos == 1) { n     dir = 1; n     return pos + 1; n   } n   if (pos == n) { n     dir = 0; n     return pos - 1; n   } n   if (dir) return pos + 1; n   return pos - 1; n } n int main() { n   cin >> n >> m >> k; n   string napr, isstop; n   cin >> napr >> napr; n   cin >> isstop; n   if (napr ==  head ) n     dir = 0; n   else n     dir = 1; n   for (int i = 0; i < isstop.size(); i++) { n     if (isstop[i] ==  1 ) { n       k = go(k); n       if (dir == 0) { n         if (k == n) n           m = 1; n         else n           m = n; n       } else { n         if (k == 1) n           m = n; n         else n           m = 1; n       } n     } else { n       if (dir == 0) { n         if (m < k) { n           m--; n           m = max(1, m); n         } else { n           m++; n           m = min(m, n); n         } n       } else { n         if (m > k) { n           m++; n           m = min(m, n); n         } else { n           m--; n           m = max(1, m); n         } n       } n       k = go(k); n     } n     if (m == k) { n       cout <<  Controller   << i + 1; n       return 0; n     } n   } n   cout <<  Stowaway ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool palindrome(string s) { n   int n = s.length(); n   for (int i = 0; i < n; i++) { n     if (s[i] != s[n - i - 1]) return 0; n   } n   return 1; n } n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   for (int i = 0; i < t; i++) { n     int x, y, n, cnt = 0, chk = 0, tmp, pos = -1, pos2 = -1, res; n     map<pair<int, int>, int> mp, Tmp, Imp; n     map<int, string> mp2; n     string s =   , s2 =   , s3 =   ; n     scanf( %d , &n); n     cin >> s; n     x = 0, y = 0; n     Tmp[{x, y}]++; n     mp[{x, y}]++; n     for (int j = 0; j < s.size(); j++) { n       s2 =   , s3 =   ; n       if (s[j] ==  U ) { n         y++; n       } else if (s[j] ==  D ) { n         y--; n       } else if (s[j] ==  R ) { n         x++; n       } else if (s[j] ==  L ) { n         x--; n       } n       if (j == 0) { n         int tx = x, ty = y; n       } n       tmp = mp[{x, y}]; n       mp[{x, y}]++; n       if (j == 0) { n         Imp[{x, y}] = j; n       } n       if (mp[{x, y}] != tmp && tmp != 0) { n         int dist; n         if (x == 0 && y == 0 && pos == -1 && pos2 == -1) { n           dist = abs((j + 1) - (Imp[{x, y}] + 1)); n         } else { n           dist = abs((j + 1) - (Imp[{x, y}] + 2)); n         } n         chk++; n         if (chk == 1 || dist < res) { n           res = dist; n           if (x == 0 && y == 0 && pos == -1 && pos2 == -1) { n             pos2 = Imp[{x, y}] + 1; n             pos = j + 1; n           } else { n             pos2 = Imp[{x, y}] + 2; n             pos = j + 1; n           } n         } n       } n       if (j != 0) { n         Imp[{x, y}] = j; n       } n     } n     if (chk == 0) { n       printf( -1 n ); n     } else { n       printf( %d %d n , min(pos, pos2), max(pos, pos2)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int arr[600][600] = {}; n int visited[600][600] = {}; n void dfs(int, int); n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char t; n       cin >> t; n       if (t ==  . ) { n         arr[i][j] = 1; n       } n       if (t ==  # ) arr[i][j] = 2; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (arr[i][j] == 1) { n         dfs(i, j); n         break; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (arr[i][j] == 1) cout <<  . ; n       if (arr[i][j] == 2) cout <<  # ; n       if (arr[i][j] == 3) cout <<  X ; n     } n     cout << endl; n   } n } n void dfs(int i, int j) { n   visited[i][j] = 1; n   if (arr[i][j + 1] == 1 && !visited[i][j + 1]) { n     dfs(i, j + 1); n   } n   if (arr[i + 1][j] == 1 && !visited[i + 1][j]) { n     dfs(i + 1, j); n   } n   if (arr[i][j - 1] == 1 && !visited[i][j - 1]) { n     dfs(i, j - 1); n   } n   if (arr[i - 1][j] == 1 && !visited[i - 1][j]) { n     dfs(i - 1, j); n   } n   if (k) { n     arr[i][j] = 3; n     k--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n long long arr[200005]; n pair<long long, long long> pt[200005]; n int main() { n   long long n, x, y, newX, newY, cnt = 0, ansX, ansY, val = 0; n   map<pair<long long, long long>, long long> mp1; n   cin >> n >> x >> y; n   for (int i = 0; i < n; i++) { n     cin >> pt[i].first >> pt[i].second; n   } n   newX = x + 1, newY = y; n   for (int i = 0; i < n; i++) { n     if (newX >= min(pt[i].first, x) and newX <= max(pt[i].first, x)) { n       if (newY >= min(pt[i].second, y) and newY <= max(pt[i].second, y)) cnt++; n     } n   } n   mp1[{newX, newY}] = cnt; n   cnt = 0; n   newX = x - 1, newY = y; n   for (int i = 0; i < n; i++) { n     if (newX >= min(pt[i].first, x) and newX <= max(pt[i].first, x)) { n       if (newY >= min(pt[i].second, y) and newY <= max(pt[i].second, y)) cnt++; n     } n   } n   mp1[{newX, newY}] = cnt; n   cnt = 0; n   newX = x, newY = y + 1; n   for (int i = 0; i < n; i++) { n     if (newX >= min(pt[i].first, x) and newX <= max(pt[i].first, x)) { n       if (newY >= min(pt[i].second, y) and newY <= max(pt[i].second, y)) cnt++; n     } n   } n   mp1[{newX, newY}] = cnt; n   cnt = 0; n   newX = x, newY = y - 1; n   for (int i = 0; i < n; i++) { n     if (newX >= min(pt[i].first, x) and newX <= max(pt[i].first, x)) { n       if (newY >= min(pt[i].second, y) and newY <= max(pt[i].second, y)) cnt++; n     } n   } n   mp1[{newX, newY}] = cnt; n   for (auto it = mp1.begin(); it != mp1.end(); it++) { n     if (it->second > val) { n       val = it->second; n       ansX = it->first.first; n       ansY = it->first.second; n     } n   } n   cout << val << endl; n   cout << ansX <<     << ansY << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<string> rs; n   if (n == 1) { n     rs.push_back( a ); n     rs.push_back( a ); n     rs.push_back( b ); n     rs.push_back( b ); n   } else if (n == 2) { n     rs.push_back( aa ); n     rs.push_back( bb ); n     rs.push_back( aa ); n     rs.push_back( bb ); n   } else if (n == 3) { n     rs.push_back( aac ); n     rs.push_back( bbc ); n     rs.push_back( add ); n     rs.push_back( abb ); n   } else if (n == 4) { n     rs.push_back( aabb ); n     rs.push_back( cddc ); n     rs.push_back( ceec ); n     rs.push_back( aabb ); n   } else if (n % 2 == 0) { n     int b = 0; n     int c = 5; n     int d = 10; n     string pal1 =   ; n     string pal2 =  z ; n     string pal3 =  z ; n     for (int i = 0; i < n / 2; i++) { n       b = (b + 1) % 24; n       c = (c + 1) % 24; n       d = (d + 1) % 24; n       pal1 += string(2, char( a  + b)); n       if (i == n / 2 - 1) continue; n       pal2 += string(2, char( a  + c)); n       pal3 += string(2, char( a  + d)); n     } n     pal2 +=  z ; n     pal3 +=  z ; n     rs.push_back(pal1); n     rs.push_back(pal2); n     rs.push_back(pal3); n     rs.push_back(pal1); n   } else if (n % 2 != 0) { n     int b = 0; n     int c = 5; n     int d = 10; n     string pal1 =   ; n     string pal2 =   ; n     string pal3 =  z ; n     for (int i = 0; i < n / 2; i++) { n       b = (b + 1) % 24; n       c = (c + 1) % 24; n       d = (d + 1) % 24; n       pal1 += string(2, char( a  + b)); n       pal2 += string(2, char( a  + c)); n       pal3 += string(2, char( a  + d)); n     } n     string pal4 =  z  + pal1; n     pal1 +=  z ; n     pal2 +=  z ; n     rs.push_back(pal1); n     rs.push_back(pal2); n     rs.push_back(pal3); n     rs.push_back(pal4); n   } n   for (int i = int(0); i < int(4); i++) { n     cout << rs[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-6; n int H[1005]; n int main() { n   int n, m, h; n   int l, r, x; n   int i, j; n   scanf( %d%d%d , &n, &h, &m); n   for (i = 1; i <= n; i++) H[i] = h; n   for (i = 0; i < m; i++) { n     scanf( %d%d%d , &l, &r, &x); n     for (j = l; j <= r; j++) H[j] = min(H[j], x); n   } n   int ans = 0; n   for (i = 1; i <= n; i++) ans += H[i] * H[i]; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[500006]; n long long dp[500006]; n long long vis[500006]; n vector<long long> v[500006]; n long long on; n long long n, d, che; n void dfs(long long root, long long parent) { n   vis[root] = 1; n   dp[root] = 1; n   for (auto it : v[root]) { n     if (!vis[it]) { n       dfs(it, root); n       if (a[it] == che && it < on) { n         continue; n       } n       if (it != parent && che <= a[it] && a[it] <= che + d) { n         (dp[root] *= (1 + dp[it])) %= 1000000007; n       } n     } n   } n } n void solve() { n   cin >> d >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 0; i < n - 1; i++) { n     long long a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     on = i; n     for (long long j = 1; j <= n; j++) vis[j] = dp[j] = 0; n     che = a[i]; n     dfs(i, -1); n     (ans += dp[i]) %= 1000000007; n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long ma(long long a, long long b) { return a > b ? a : b; } n inline long long mi(long long a, long long b) { return a > b ? b : a; } n template <typename Arg1> n void __f(const char *name, Arg1 &&arg1) { n   cerr << name <<   :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char *names, Arg1 &&arg1, Args &&...args) { n   const char *comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n vector<long long> adj[100005], v; n bool Q; n struct line { n   mutable long long k, m, p; n   bool operator<(const line &o) const { return Q ? p < o.p : k < o.k; } n }; n struct LineContainer : multiset<line> { n   const long long inf = LLONG_MAX; n   inline long long div(long long a, long long b) { n     return a / b - ((a ^ b) < 0 && a % b); n   } n   inline bool isect(iterator x, iterator y) { n     if (y == end()) { n       x->p = inf; n       return false; n     } n     if (x->k == y->k) n       x->p = x->m > y->m ? inf : -inf; n     else n       x->p = div(y->m - x->m, x->k - y->k); n     return x->p >= y->p; n   } n   inline void add(long long k, long long m) { n     auto z = insert({k, m, 0}), y = z++, x = y; n     while (isect(y, z)) z = erase(z); n     if (y != begin() && isect(--x, y)) isect(x, erase(y)); n     while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y)); n   } n   inline long long query(long long x) { n     assert(!empty()); n     Q = 1; n     auto l = *lower_bound({0, 0, x}); n     Q = 0; n     return l.k * x + l.m; n   } n }; n struct T { n   LineContainer lc; n   T() {} n   T(LineContainer &lc) : lc(lc) {} n }; n struct ST { n   long long n; n   T t[2 * 100005]; n   inline void Op(T &ty, T &val1, T &val2) { n     for (auto xd : val1.lc) { n       ty.lc.add(xd.k, xd.m); n     } n     for (auto xd : val2.lc) { n       ty.lc.add(xd.k, xd.m); n     } n   } n   inline long long Op1(T &val1, long long cur, long long x) { n     long long val = val1.lc.query(x); n     return mi(cur, -val); n   } n   inline void build() { n     for (long long(i) = (n - 1); (i) >= (1); (i)--) n       Op(t[i], t[i << 1], t[i << 1 | 1]); n   } n   inline long long que(long long l, long long r, long long ra) { n     long long ans = 23242532523; n     for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n       if (l & 1) ans = Op1(t[l++], ans, ra); n       if (r & 1) ans = Op1(t[--r], ans, ra); n     } n     return ans; n   } n }; n long long dp[100005], ar[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long n; n   cin >> n; n   ST st; n   st.n = n; n   for (long long(i) = (0); (i) < (n); (i)++) cin >> ar[i]; n   for (long long(i) = (0); (i) < (n); (i)++) n     dp[i] = (i == 0) ? ar[i] : dp[i - 1] + ar[i]; n   for (long long(i) = (0); (i) < (n); (i)++) { n     long long x = ar[i], y = ar[i] * (i + 1) - dp[i]; n     st.t[i + n].lc.add(-x, -y); n   } n   st.build(); n   long long q; n   cin >> q; n   vector<pair<long long, pair<long long, long long> > > qs; n   for (long long(i) = (0); (i) < (q); (i)++) { n     long long l, r; n     cin >> l >> r; n     l--; n     qs.push_back({l - r + 1, {i, r}}); n   } n   sort((qs).begin(), (qs).end()); n   reverse((qs).begin(), (qs).end()); n   long long ez[100005]; n   for (long long(i) = (0); (i) < (qs.size()); (i)++) { n     long long val = qs[i].first, r = qs[i].second.second, n               id = qs[i].second.first; n     long long l = val + r; n     l = r - l; n     ez[id] = st.que(l, r, val) + dp[r - 1]; n   } n   for (long long(i) = (0); (i) < (q); (i)++) cout << ez[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 64; n int n, m, r, a[maxn][maxn], dis[maxn]; n int dp[maxn][maxn][maxn]; n set<pair<int, int> > s; n void dij(int v) { n   memset(dis, 127, sizeof dis); n   dis[v] = 0; n   s.insert(make_pair(dis[v], v)); n   while (s.size()) { n     v = s.begin()->second; n     s.erase(s.begin()); n     for (int u = 0; u < n; u++) n       if (dis[v] + a[v][u] < dis[u]) { n         s.erase(make_pair(dis[u], u)); n         dis[u] = dis[v] + a[v][u]; n         s.insert(make_pair(dis[u], u)); n       } n   } n } n void set_distance() { n   memset(dis, 127, sizeof dis); n   for (int i = 0; i < n; i++) { n     dij(i); n     for (int j = 0; j < n; j++) n       if (dis[j] < dp[i][j][0]) dp[i][j][0] = dis[j]; n   } n } n void update_dp() { n   for (int k = 1; k < n; k++) n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         for (int l = 0; l < n; l++) n           dp[i][j][k] = min(dp[i][j][k], dp[i][l][k - 1] + dp[l][j][0]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> r; n   memset(dp, 31, sizeof dp); n   while (m--) { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) cin >> a[i][j]; n     set_distance(); n   } n   update_dp(); n   for (int S, T, k; r--;) { n     cin >> S >> T >> k; n     cout << dp[S - 1][T - 1][min(k, n - 2)] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], b[100005], c[100005]; n class segtree { n  public: n   int seg[524288 * 2]; n   int lazy[524288 * 2]; n   void init() { n     for (int i = 0; i < 524288 * 2; i++) { n       seg[i] = 0; n     } n   } n   void lazy_evaluate(int x) { n     if (x * 2 + 2 >= 524288 * 2) return; n     lazy[x * 2 + 1] += lazy[x]; n     lazy[x * 2 + 2] += lazy[x]; n     seg[x * 2 + 1] += lazy[x]; n     seg[x * 2 + 2] += lazy[x]; n     lazy[x] = 0; n   } n   int update(int a, int b, int k, int lb, int ub, int val) { n     pair<int, int> p; n     p.first = -2000000000, p.second = -2000000000; n     if (ub < a || b < lb) return seg[k]; n     if (a <= lb && ub <= b) { n       lazy[k] += val; n       seg[k] += val; n       return seg[k]; n     } n     if (lazy[k]) lazy_evaluate(k); n     int p1 = update(a, b, k * 2 + 1, lb, (lb + ub) / 2, val); n     int p2 = update(a, b, k * 2 + 2, (lb + ub) / 2 + 1, ub, val); n     return seg[k] = max(p1, p2); n   } n   int query() { return seg[0]; } n } seg; n int main() { n   int n; n   vector<pair<int, pair<int, int> > > p; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d %d , &a[i], &b[i], &c[i]); n     p.push_back(make_pair(a[i], make_pair(b[i], c[i]))); n   } n   sort(p.begin(), p.end()); n   seg.init(); n   int ret = -1; n   int lb, ub; n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       que; n   for (int i = 0; i < n; i++) { n     while (!que.empty() && p[i].first > que.top().first) { n       seg.update(p[que.top().second].second.first, n                  p[que.top().second].second.second, 0, 0, 524288 - 1, -1); n       que.pop(); n     } n     que.push(make_pair(p[i].second.first, i)); n     seg.update(p[i].second.first, p[i].second.second, 0, 0, 524288 - 1, 1); n     int pp = seg.query(); n     if (pp > ret) { n       ret = pp; n       lb = p[i].first; n     } n   } n   printf( %d n , ret); n   priority_queue<int, vector<int>, greater<int> > que2; n   int next; n   vector<pair<int, int> > in; n   for (int i = 0; i < n; i++) { n     if (p[i].second.first >= lb && p[i].first <= lb) { n       in.push_back(make_pair(p[i].second.first, i)); n     } n   } n   sort(in.begin(), in.end()); n   next = 0; n   for (int j = lb;; j++) { n     while (!que2.empty() && que2.top() < j) que2.pop(); n     while (next != in.size() && in[next].first <= j) { n       que2.push(p[in[next].second].second.second); n       next++; n     } n     if (que2.size() == ret) { n       ub = j; n       break; n     } n   } n   int x = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] <= lb && ub <= c[i] && lb <= b[i] && b[i] <= ub) { n       x++; n       printf( %d%c , i + 1, x == ret ?   n  :    ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int ans; n int p1 = 1 << 17; n int p2 = 1 << 18; n void solve(int x) { n   int a = p1 | x; n   int b = p2 | p1; n   cout << a <<     << b <<     << p2; n } n int main() { n   cin >> n >> k; n   if (n == 2 && k == 0) { n     cout <<  NO  << endl; n     return 0; n   } n   if (n == 1) { n     cout <<  YES n  << k << endl; n     return 0; n   } n   if (n == 2) { n     cout <<  YES n  << k <<     <<  0  << endl; n     return 0; n   } n   cout <<  YES n ; n   for (int i = 1; i <= n - 3; i++) { n     cout << i <<    ; n     ans ^= i; n   } n   solve(ans ^ k); n } n


#include <bits/stdc++.h> n using namespace std; n const int max_N = 2e5 + 10; n const int l_N = 20; n int st[l_N][max_N]; n int lg[max_N]; n int inp[max_N]; n vector<int> bits[30]; n int query_max(int i, int j); n int main() { n   { n     int j = 0; n     for (int i = 0; i < max_N; ++i) { n       while ((1 << j) <= i) { n         ++j; n       } n       lg[i] = j - 1; n     } n   } n   int N; n   scanf( %d , &N); n   for (int i = 1; i <= N; ++i) { n     scanf( %d , &inp[i]); n     st[0][i] = inp[i]; n   } n   for (int j = 1; j < l_N; ++j) { n     for (int i = 1; i + (1 << j) - 1 <= N; ++i) { n       st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]); n     } n   } n   for (int i = 1; i <= N; ++i) { n     for (int j = 0; j < 30; ++j) { n       if ((inp[i] >> j) & 1) { n         bits[j].push_back(i); n       } n     } n   } n   long long ans = 0; n   for (int i = 1; i <= N; ++i) { n     vector<int> or_changes; n     for (int j = 0; j < 30; ++j) { n       auto it = lower_bound(bits[j].begin(), bits[j].end(), i); n       if (it != bits[j].end()) { n         or_changes.push_back(*it); n       } n     } n     sort(or_changes.begin(), or_changes.end()); n     or_changes.push_back(N + 1); n     int cid = *or_changes.begin(); n     int cor = inp[cid]; n     for (int j = 1; j < or_changes.size(); ++j) { n       int L = cid, R = or_changes[j]; n       int low = L, high = R - 1, mid, best = R; n       while (low <= high) { n         mid = (low + high) >> 1; n         if (query_max(L, mid) >= cor) { n           best = mid; n           high = mid - 1; n         } else { n           low = mid + 1; n         } n       } n       ans += best - L; n       cid = R; n       cor |= inp[R]; n     } n   } n   cout << ans << endl; n } n int query_max(int i, int j) { n   int d = lg[j - i + 1]; n   return max(st[d][i], st[d][j - (1 << d) + 1]); n } n


#include <bits/stdc++.h> n using namespace std; n int num[1005]; n char num0[1005]; n int res1[1005]; n int N; n int query() { n   int res = 0; n   for (int i = 0; i < N; i++) { n     if (num[i] != 0) { n       res *= 10; n       res += num[i]; n     } else if (num[i] == 0) { n       res *= 10; n     } n   } n   return res >= 0 ? res : -res; n } n int move_1() { n   int t = num[0]; n   for (int i = 0; i < N - 1; i++) { n     num[i] = num[i + 1]; n   } n   num[N - 1] = t; n   return 0; n } n int up1() { n   for (int i = 0; i < N; i++) { n     if (num[i] == 9) { n       num[i] = 0; n     } else { n       num[i] += 1; n     } n   } n   return 0; n } n void print() { n   printf(  num =  ); n   for (int i = 0; i < N; i++) { n     printf( %d , num[i]); n   } n   puts(  ); n } n void printres() { n   for (int i = 0; i < N; i++) { n     printf( %d , res1[i]); n   } n   puts(  ); n } n int panduan() { n   char temp1[1005], temp2[1005]; n   for (int i = 0; i < N; i++) { n     temp1[i] = num[i] +  0 ; n   } n   for (int i = 0; i < N; i++) { n     temp2[i] = res1[i] +  0 ; n   } n   if (strcmp(temp1, temp2) < 0) { n     return 1; n   } else n     return 0; n } n int copy1() { n   for (int i = 0; i < N; i++) { n     num[i] = num0[i] -  0 ; n   } n   return 0; n } n int copy2() { n   for (int i = 0; i < N; i++) { n     res1[i] = num[i]; n   } n   return 0; n } n int main() { n   while (scanf( %d , &N) != EOF) { n     getchar(); n     for (int i = 0; i < N; i++) { n       scanf( %c , &num0[i]); n     } n     int res = 1e9; n     copy1(); n     copy2(); n     for (int i = 0; i < 10; i++) { n       for (int j = 1; j <= N; j++) { n         move_1(); n         if (panduan() > 0) { n           copy2(); n         } n       } n       up1(); n     } n     printres(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long nax = 100001; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (auto& x : a) { n       cin >> x; n     } n     if (a[0]) { n       cout << n + 1 <<    ; n       for (int i = 1; i <= n; ++i) { n         cout << i <<    ; n       } n       cout <<   n ; n       continue; n     } n     if (!a.back()) { n       for (int i = 1; i <= n + 1; ++i) { n         cout << i <<    ; n       } n       cout <<   n ; n       continue; n     } n     for (int i = 0; i < n - 1; ++i) { n       if (a[i] == 0 && a[i + 1] == 1) { n         for (int j = 1; j <= i + 1; ++j) { n           cout << j <<    ; n         } n         cout << n + 1 <<    ; n         for (int j = i + 2; j <= n; ++j) { n           cout << j <<    ; n         } n         cout <<   n ; n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int x, n, p; n int a[1005]; n long double f[205][205][205]; n int check(int m) { n   int s = 0; n   while (m % 2 == 0) { n     s++; n     m /= 2; n   } n   return s; n } n int main() { n   ios_base::sync_with_stdio(false); n   int i, j, k; n   long double xx, yy; n   cin >> x >> n >> p; n   xx = (long double)p / 100.0; n   yy = (long double)(100 - p) / 100.0; n   for (i = 0; i <= 1000; i++) a[i] = check(x + i); n   for (j = 0; j <= 201; j++) { n     for (k = 0; k <= 201; k++) { n       f[0][j][k] = (long double)(a[j] + k); n     } n   } n   for (i = 1; i <= n; i++) { n     for (j = 0; j <= 200; j++) { n       for (k = 0; k <= 200; k++) { n         f[i][j][k] = f[i - 1][j + 1][k] * yy; n         if (j % 2 == 0) n           f[i][j][k] += (f[i - 1][j / 2][k + 1] * xx); n         else n           f[i][j][k] += ((long double)k * xx); n       } n     } n   } n   cout.precision(9); n   cout << fixed << f[n][0][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tr[100001]; n int sum(int ind) { n   int sum = 0; n   ind = ind + 1; n   while (ind) { n     sum += tr[ind]; n     ind -= ind & (-ind); n   } n   return sum; n } n void update(int ind, int val) { n   ++ind; n   while (ind <= 100000) { n     tr[ind] += val; n     ind += ind & (-ind); n   } n } n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   set<int> ind[100001]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     ind[a[i]].emplace(i); n   } n   long long ans = 1; n   int pos = 0; n   sort(a.begin(), a.end()); n   for (int i = 0; i < n; ++i) { n     int next; n     if (i > 0 && a[i] == a[i - 1]) { n       next = (ind[a[i]].upper_bound(pos) == ind[a[i]].end()) n                  ? *(ind[a[i]].begin()) n                  : *(ind[a[i]].upper_bound(pos)); n     } else { n       next = (ind[a[i]].lower_bound(pos) == ind[a[i]].end()) n                  ? *(ind[a[i]].begin()) n                  : *(ind[a[i]].lower_bound(pos)); n     } n     int rn = next - sum(next); n     int rp = pos - sum(pos); n     ans += (rn >= rp) ? (long long)(rn - rp) : (long long)(n - i - rp + rn + 1); n     if (i) update(pos, 1); n     pos = next; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000000; n const int mod = 998244353; n int used[N][2]; n int f[N][2]; n bool ok[N]; n int solve(int n, int k) { n   if (used[n][k]) { n     return f[n][k]; n   } n   if (!ok[n]) { n     return 0; n   } n   int last = 0; n   int m = n; n   int j = 0; n   for (int i = 0; i < 40; i++) { n     if ((1 << i) > m) { n       last = m; n       break; n     } n     j = i; n     m -= (1 << i); n   } n   if (m == 0) { n     used[n][k] = 1; n     f[n][k] = 0; n     return 0; n   } n   int ans = 0; n   int nxt = (1 << j); n   int sz = (n - last) / 2; n   for (int i = max(0, last - sz - 1); i <= min(last, sz + 1); i++) { n     if ((sz + i) % 2 != k) { n       continue; n     } n     ans += 1ll * (solve(sz + i, 0) + solve(sz + i, 1)) % mod * n            solve(sz + last - i, 1) % mod; n     if (ans >= mod) { n       ans -= mod; n     } n   } n   used[n][k] = 1; n   f[n][k] = ans; n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   int x = 1; n   int v = 0; n   while (x <= n) { n     ok[x] = 1; n     ok[x + 1] = 1; n     x++; n     x = x * 2 - v; n     v = 1 - v; n   } n   used[0][0] = 1; n   f[0][0] = 1; n   used[0][1] = 1; n   f[0][1] = 1; n   used[1][0] = 1; n   f[1][0] = 1; n   used[1][1] = 1; n   f[1][1] = 0; n   cout << (solve(n, 0) + solve(n, 1)) % mod <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   long long a, b, c; n   for (scanf( %d , &T); T; T--) { n     cin >> a >> b >> c; n     if (a >= c) { n       printf( -1  ); n     } else n       printf( 1  ); n     if (a * b > c) n       printf( %I64d n , b); n     else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void upmax(T& a, T b) { n   if (a < b) a = b; n } n template <class T> n inline void upmin(T& a, T b) { n   if (a > b) a = b; n } n const int maxn = 307; n const int maxm = 100007; n const int mod = 1000000007; n const int inf = 0x7fffffff; n const double eps = 1e-7; n typedef int arr[maxn]; n typedef int adj[maxm]; n inline int fcmp(double a, double b) { n   if (fabs(a - b) <= eps) return 0; n   if (a < b - eps) return -1; n   return 1; n } n inline int add(int a, int b) { return ((long long)a + b) % mod; } n inline int mul(int a, int b) { return ((long long)a * b) % mod; } n inline int dec(int a, int b) { return add(a, mod - b % mod); } n inline int Pow(int a, int b) { n   int t = 1; n   while (b) { n     if (b & 1) t = mul(t, a); n     a = mul(a, a), b >>= 1; n   } n   return t; n } n template <typename Type> n inline Type RD() { n   char c = getchar(); n   Type x = 0, flag = 1; n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     flag = -1; n   else n     x = c -  0 ; n   while (isdigit(c = getchar())) x = x * 10 + c -  0 ; n   return x * flag; n } n inline char rdch() { n   char c = getchar(); n   while (!isalpha(c)) c = getchar(); n   return c; n } n int n, m, k; n double C[maxn][maxn], f[maxm]; n void input() { n   n = RD<int>(), m = RD<int>(), k = RD<int>(); n   for (int i = 0, _ = n; i <= _; i++) { n     C[i][0] = 1; n     for (int j = 1, _ = i; j <= _; j++) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n   } n   f[0] = 1; n   for (int i = 1, _ = k; i <= _; i++) n     f[i] = f[i - 1] * (k - i + 1) / (m - i + 1); n } n void solve() { n   double ans = 0; n   for (int i = 0, _ = n; i <= _; i++) { n     for (int j = 0, _ = n; j <= _; j++) { n       ans += C[n][i] * C[n][j] * f[(i + j) * n - i * j]; n       if (ans > 1e99) break; n     } n     if (ans > 1e99) break; n   } n   if (ans > 1e99) ans = 1e99; n   printf( %.10lf n , ans); n } n int main() { n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 52; n const long long mod = 1e9 + 7; n int N; n long long dp[2][MN][MN][MN][MN]; n vector<int> d; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> N; n   d.resize(N); n   for (int i = 0; i < N; i++) { n     cin >> d[i]; n   } n   int cur = 0, next = 1; n   dp[cur][d[0] == 2][d[0] == 3][d[1] == 2][d[1] == 3] = 1; n   for (int i = 1; i < N - 1; i++) { n     memset(dp[next], 0, sizeof(dp[next])); n     for (int p1 = 0; p1 <= N; p1++) n       for (int p2 = 0; p1 + p2 <= N; p2++) n         for (int c1 = 0; c1 + p1 + p2 <= N; c1++) n           for (int c2 = 0; c1 + c2 + p1 + p2 <= N; c2++) { n             if (dp[cur][p1][p2][c1][c2] == 0) continue; n             long long val = dp[cur][p1][p2][c1][c2]; n             if (p1 == 0 && p2 == 0) { n               dp[cur][c1][c2][0][0] += val; n               dp[cur][c1][c2][0][0] %= mod; n               continue; n             } n             for (int type = 0; type <= 1; type++) { n               int way; n               if (type == 0) { n                 way = p1; n                 if (p1 == 0) continue; n                 p1--; n               } else { n                 way = p2; n                 if (p2 == 0) continue; n                 p1++, p2--; n               } n               if (d[i + 1] == 2) { n                 dp[next][p1][p2][c1 + 1][c2] += val * way; n                 dp[next][p1][p2][c1 + 1][c2] %= mod; n                 if (c1 >= 1) { n                   dp[next][p1][p2][c1 - 1][c2] += val * way * c1; n                   dp[next][p1][p2][c1 - 1][c2] %= mod; n                 } n                 if (c2 >= 1) { n                   dp[next][p1][p2][c1 + 1][c2 - 1] += val * way * c2; n                   dp[next][p1][p2][c1 + 1][c2 - 1] %= mod; n                 } n               } else { n                 dp[next][p1][p2][c1][c2 + 1] += val * way; n                 dp[next][p1][p2][c1][c2 + 1] %= mod; n                 if (c1 >= 1) { n                   dp[next][p1][p2][c1 - 1 + 1][c2] += val * way * c1; n                   dp[next][p1][p2][c1 - 1 + 1][c2] %= mod; n                 } n                 if (c2 >= 1) { n                   dp[next][p1][p2][c1 + 2][c2 - 1] += val * way * c2; n                   dp[next][p1][p2][c1 + 2][c2 - 1] %= mod; n                 } n                 if (c1 >= 2) { n                   dp[next][p1][p2][c1 - 2][c2] += val * way * c1 * (c1 - 1) / 2; n                   dp[next][p1][p2][c1 - 2][c2] %= mod; n                 } n                 if (c2 >= 2) { n                   dp[next][p1][p2][c1 + 2][c2 - 2] += n                       val * way * c2 * (c2 - 1) / 2; n                   dp[next][p1][p2][c1 + 2][c2 - 2] %= mod; n                 } n                 if (c1 >= 1 && c2 >= 1) { n                   dp[next][p1][p2][c1 - 1 + 1][c2 - 1] += val * way * c2 * c1; n                   dp[next][p1][p2][c1 - 1 + 1][c2 - 1] %= mod; n                 } n               } n               if (type == 0) { n                 p1++; n               } else { n                 p1--, p2++; n               } n             } n           } n     cur ^= 1, next ^= 1; n   } n   cout << dp[cur][0][0][0][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int n, Q, pos; n long long a[maxn], sum[maxn], x; n long long tr[maxn << 2], tag[maxn << 2]; n void pushup(int rt) { tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]); } n void build(int rt = 1, int l = 1, int r = n) { n   tag[rt] = 0; n   if (l == r) { n     tr[rt] = sum[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(rt << 1, l, mid); n   build(rt << 1 | 1, mid + 1, r); n   pushup(rt); n } n void load(int rt, long long k) { n   tag[rt] += k; n   tr[rt] += k; n } n void pushdown(int rt, int l, int r) { n   if (tag[rt] == 0) return; n   load(rt << 1, tag[rt]); n   load(rt << 1 | 1, tag[rt]); n   tag[rt] = 0; n } n void update(int nl, int nr, long long k, int l = 1, int r = n, int rt = 1) { n   if (nl <= l && nr >= r) { n     load(rt, k); n     return; n   } n   pushdown(rt, l, r); n   int mid = (l + r) >> 1; n   if (nl <= mid) update(nl, nr, k, l, mid, rt << 1); n   if (nr >= mid + 1) update(nl, nr, k, mid + 1, r, rt << 1 | 1); n   pushup(rt); n } n long long temp_x; n int id; n int get_pos(long long limit, int l = 1, int r = n, int rt = 1) { n   if (l == r) { n     temp_x = tr[rt]; n     return l; n   } n   pushdown(rt, l, r); n   int mid = (l + r) >> 1; n   if (tr[rt << 1] >= limit) n     return get_pos(limit, l, mid, rt << 1); n   else n     return get_pos(limit, mid + 1, r, rt << 1 | 1); n } n void go() { n   long long now_x = 0; n   int now_id = 0; n   while (now_id != n) { n     int temp_id = get_pos(2 * now_x); n     if (temp_x == 2 * a[temp_id]) { n       printf( %d n , temp_id); n       return; n     } n     now_id = temp_id; n     now_x = temp_x; n   } n   puts( -1 ); n } n int main() { n   scanf( %d %d , &n, &Q); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , a + i); n     sum[i] = sum[i - 1] + a[i]; n   } n   build(); n   while (Q--) { n     scanf( %d %lld , &pos, &x); n     update(pos, n, x - a[pos]); n     a[pos] = x; n     go(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = 1000000000; n class V { n  public: n   long long a[2]; n   V(long long a0 = 0, long long a1 = 0) { n     a[0] = a0 % mod; n     a[1] = a1 % mod; n   } n   V operator+(const V &v) { return V(a[0] + v.a[0], a[1] + v.a[1]); } n }; n class M { n  public: n   long long a[2][2]; n   M(long long a00 = 1, long long a01 = 0, long long a10 = 0, n     long long a11 = 1) { n     a[0][0] = a00 % mod; n     a[0][1] = a01 % mod; n     a[1][0] = a10 % mod; n     a[1][1] = a11 % mod; n   } n   M operator+(const M &m) { n     return M(a[0][0] + m.a[0][0], a[0][1] + m.a[0][1], a[1][0] + m.a[1][0], n              a[1][1] + m.a[1][1]); n   } n   M operator*(const M &m) { n     M r(0, 0, 0, 0); n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) { n         for (int k = 0; k < 2; k++) r.a[i][j] += a[i][k] * m.a[k][j]; n         r.a[i][j] %= mod; n       } n     return r; n   } n   V operator*(const V &v) { n     return V(a[0][0] * v.a[0] + a[0][1] * v.a[1], n              a[1][0] * v.a[0] + a[1][1] * v.a[1]); n   } n }; n const int maxn = 200010; n const int maxit = (1 << 19) + 5; n M pow_m[maxn], sum_m[maxn], m0; n long long it_a[maxit], it_b[maxit]; n V it_v[maxit]; n V it_vr(int u, int L, int R, long long bonus = 0) { n   return it_v[u] + sum_m[R - L + 1] * V(it_b[u] + bonus, it_b[u] + bonus); n } n void it_update(int u, int L, int R, int x, int v) { n   if (x < L || x > R) return; n   if (L == R) { n     it_a[u] = v; n     it_b[u] = 0; n     it_v[u] = V(v, v); n     return; n   } n   it_b[2 * u] += it_b[u]; n   it_b[2 * u + 1] += it_b[u]; n   it_b[u] = 0; n   int M = (L + R) / 2; n   it_update(2 * u, L, M, x, v); n   it_update(2 * u + 1, M + 1, R, x, v); n   it_v[u] = it_vr(2 * u, L, M) + pow_m[M - L + 1] * it_vr(2 * u + 1, M + 1, R); n } n void it_add(int u, int L, int R, int l, int r, int v) { n   if (r < L || l > R) return; n   if (l <= L && r >= R) { n     it_b[u] += v; n     return; n   } n   int M = (L + R) / 2; n   it_add(2 * u, L, M, l, r, v); n   it_add(2 * u + 1, M + 1, R, l, r, v); n   it_v[u] = it_vr(2 * u, L, M) + pow_m[M - L + 1] * it_vr(2 * u + 1, M + 1, R); n } n V it_get(int u, int L, int R, int l, int r, long long bonus = 0) { n   if (r < L || l > R) return V(0, 0); n   if (l <= L && r >= R) { n     return pow_m[L - l] * it_vr(u, L, R, bonus); n   } n   int M = (L + R) / 2; n   return it_get(2 * u, L, M, l, r, bonus + it_b[u]) + n          it_get(2 * u + 1, M + 1, R, l, r, bonus + it_b[u]); n } n int main() { n   m0 = M(0, 1, 1, 1); n   pow_m[0] = M(1, 0, 0, 1); n   sum_m[0] = M(0, 0, 0, 0); n   for (int i = 0; i + 1 < maxn; i++) { n     pow_m[i + 1] = pow_m[i] * m0; n     sum_m[i + 1] = sum_m[i] + pow_m[i]; n   } n   memset(it_b, 0, sizeof(it_b)); n   int n, m, x, l, r, a, t; n   assert(scanf( %d%d , &n, &m) == 2); n   for (int i = 0; i < n; i++) { n     assert(scanf( %d , &a) == 1); n     it_update(1, 0, n - 1, i, a); n   } n   for (int i = 0; i < m; i++) { n     assert(scanf( %d , &t) == 1); n     if (t == 1) { n       assert(scanf( %d%d , &x, &a) == 2); n       it_update(1, 0, n - 1, x - 1, a); n     } else if (t == 2) { n       assert(scanf( %d%d , &l, &r) == 2); n       V v = it_get(1, 0, n - 1, l - 1, r - 1); n       printf( %d n , (int)v.a[0]); n     } else if (t == 3) { n       assert(scanf( %d%d%d , &l, &r, &a) == 3); n       it_add(1, 0, n - 1, l - 1, r - 1, a); n     } else n       assert(t == 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n int II() { n   int n; n   scanf( %d , &n); n   return n; n } n void II(int n) { printf( %d , n); } n void IIn(int n) { printf( %d n , n); } n void IIb(int n) { printf( %d  , n); } n long long int LL() { n   long long int n; n   scanf( %lld , &n); n   return n; n } n void LL(long long int n) { printf( %lld , n); } n void LLn(long long int n) { printf( %lld n , n); } n void LLb(long long int n) { printf( %lld  , n); } n char CC() { return getchar(); } n char CCa() { n   char c = getchar(); n   while (c <= 32) c = getchar(); n   return c; n } n void CC(char c) { putchar(c); } n void CCn(char c) { n   putchar(c); n   putchar(10); n } n void CCb(char c) { n   putchar(c); n   putchar(32); n } n void CCn() { putchar(10); } n void CCb() { putchar(32); } n void SS(char *s) { scanf( %s , s); } n void SSb(const char *s) { printf( %s  , s); } n void SSn(const char *s) { printf( %s n , s); } n float FF() { n   float n; n   scanf( %f , &n); n   return n; n } n void FF(float n) { printf( %f , n); } n void FFn(float n) { printf( %f n , n); } n void FFb(float n) { printf( %f  , n); } n void FF(double n, int prec) { std::cout << std::setprecision(prec) << n; } n void FFn(double n, int prec) { n   std::cout << std::setprecision(prec) << n; n   putchar(10); n } n void FFb(double n, int prec) { n   std::cout << std::setprecision(prec) << n; n   putchar(32); n } n struct node { n   int val; n   int prio; n   int subsize; n   bool flagrotate; n   node *left, *right; n   node() { n     prio = rand(); n     subsize = 1; n     flagrotate = false; n     left = right = NULL; n   } n   void clean() { n     if (flagrotate) { n       flagrotate = false; n       std::swap(left, right); n       if (left) left->flagrotate ^= 1; n       if (right) right->flagrotate ^= 1; n     } n   } n   void assest() { n     subsize = 1 + (left ? left->subsize : 0) + (right ? right->subsize : 0); n   } n }; n typedef node *tree; n tree merge(tree A, tree B) { n   if (!A) return B; n   if (!B) return A; n   A->clean(); n   B->clean(); n   if (A->prio > B->prio) { n     A->right = merge(A->right, B); n     A->assest(); n     return A; n   } else { n     B->left = merge(A, B->left); n     B->assest(); n     return B; n   } n } n void print(tree x, int d = 0) { n   if (!x) return; n   x->clean(); n   print(x->left, d + 1); n   IIb(x->val); n   print(x->right, d + 1); n } n std::pair<tree, tree> split(tree x, int k) { n   if (!x) return {NULL, NULL}; n   x->clean(); n   int sl = x->left ? x->left->subsize : 0; n   if (k <= sl) { n     std::pair<tree, tree> r = split(x->left, k); n     x->left = r.second; n     x->assest(); n     return {r.first, x}; n   } n   k -= sl; n   --k; n   std::pair<tree, tree> r = split(x->right, k); n   x->right = r.first; n   x->assest(); n   return {x, r.second}; n } n int nth(tree x, int p) { n   x->clean(); n   int sl = x->left ? x->left->subsize : 0; n   if (sl == p) return x->val; n   if (sl > p) return nth(x->left, p); n   return nth(x->right, p - sl - 1); n } n constexpr int MAXN = 200000; n node V[MAXN]; n int main() { n   int N = II(), Q = II(), M = II(); n   for (int x = 0; x < N; x++) V[x].val = II(); n   tree T = NULL; n   for (int x = 0; x < N; x++) T = merge(T, V + x); n   while (Q--) { n     if (II() == 1) { n       int l = II() - 1, r = II(); n       std::pair<tree, tree> A = split(T, r); n       std::pair<tree, tree> B = split(A.first, l); n       std::pair<tree, tree> C = split(B.second, B.second->subsize - 1); n       T = merge(merge(B.first, C.second), merge(C.first, A.second)); n     } else { n       int l = II() - 1, r = II(); n       std::pair<tree, tree> A = split(T, r); n       std::pair<tree, tree> B = split(A.first, l); n       B.second->flagrotate ^= 1; n       T = merge(merge(B.first, B.second), A.second); n     } n   } n   while (M--) IIb(nth(T, II() - 1)); n   CCn(); n } n


#include <bits/stdc++.h> n using namespace std; n int solve(vector<int> &v, int pow) { n   if (pow == 20) { n     for (int j : v) { n       if (j) return 40; n     } n     return 0; n   } n   bool fnd = false; n   for (int i = 0; i < v.size(); i++) { n     if (v[i] % (1 << (pow + 1))) { n       fnd = true; n     } n   } n   if (!fnd) n     return solve(v, pow + 1); n   else { n     vector<int> v2(v); n     for (int i = 0; i < v.size(); i++) { n       if (v[i] % (1 << (pow + 1))) { n         v2[i] += (1 << pow); n       } n     } n     sort(v2.begin(), v2.end()); n     v2.erase(unique(v2.begin(), v2.end()), v2.end()); n     int tmp = 1 + solve(v2, pow + 1); n     v2.clear(); n     for (int i = 0; i < v.size(); i++) { n       v2.push_back(v[i]); n     } n     for (int i = 0; i < v.size(); i++) { n       if (v[i] % (1 << (pow + 1))) { n         v2[i] -= (1 << pow); n       } n     } n     sort(v2.begin(), v2.end()); n     v2.erase(unique(v2.begin(), v2.end()), v2.end()); n     return min(tmp, 1 + solve(v2, pow + 1)); n   } n } n int main() { n   int N; n   cin >> N; n   vector<int> v(N); n   for (int i = 0; i < N; i++) { n     cin >> v[i]; n   } n   int ans = solve(v, 0); n   cout << ans << endl; n   vector<int> lvl(v); n   int rank = 0; n   while (rank != 20) { n     bool fnd = false; n     for (int i = 0; i < v.size(); i++) { n       if (v[i] % (1 << (rank + 1))) { n         fnd = true; n       } n     } n     if (!fnd) { n       rank++; n     } else { n       vector<int> v2(v); n       vector<int> v3(v); n       for (int i = 0; i < v.size(); i++) { n         if (v[i] % (1 << (rank + 1))) { n           v2[i] += (1 << rank); n         } n       } n       sort(v2.begin(), v2.end()); n       v2.erase(unique(v2.begin(), v2.end()), v2.end()); n       int tmp = 1 + solve(v2, rank + 1); n       for (int i = 0; i < v.size(); i++) { n         if (v[i] % (1 << (rank + 1))) { n           v3[i] -= (1 << rank); n         } n       } n       sort(v3.begin(), v3.end()); n       v3.erase(unique(v3.begin(), v3.end()), v3.end()); n       int tmp2 = 1 + solve(v3, rank + 1); n       if (tmp < tmp2) { n         cout << -(1 << rank) <<    ; n         v = v2; n       } else { n         cout << (1 << rank) <<    ; n         v = v3; n       } n       rank++; n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n  n using namespace std; n using ll = long long; n  n const ll maxc = 1000000, sz = maxc + 5, modint = 998244353; n ll arrleq2[sz] = {}, arr1[sz] = {}, arr2[sz] = {}, arr3[sz] = {}; n  n int main() { n     //ios_base::sync_with_stdio(0); cin.tie(0); n     arrleq2[0] = 1; n     ll sum = 0; n     for (int i = 1; i <= maxc; i++) { n         arrleq2[i] = arrleq2[i - 1] * (sum + 1) + arrleq2[i - 1] * (arrleq2[i - 1] + 1) / 2; n         arrleq2[i] %= modint; n         arr3[i] = arrleq2[i - 1] * (arrleq2[i - 1] + 1) / 2 % modint * (arrleq2[i - 1] + 2) % modint; n         while (arr3[i] % 3) arr3[i] += modint; n         arr3[i] /= 3; n         arr3[i] += arrleq2[i - 1] * (arrleq2[i - 1] + 1) / 2 % modint * sum % modint; n         arr3[i] += sum * (sum + 1) / 2 % modint * arrleq2[i - 1]; n         arr3[i] %= modint; n         sum += arrleq2[i - 1]; n         sum %= modint; n     } n     arr1[0] = 0; arr2[0] = 0; n     for (int i = 1; i <= maxc; i++) { n         arr1[i] = arrleq2[i - 1]; n         arr2[i] = (arrleq2[i] - arr1[i] + modint) % modint; n     } n     //for (int i = 1; i <= 20; i++) cout << arrleq2[i] <<    ; cout <<   n ; n     //f//or (int i = 1; i <= 20; i++) cout << arr1[i] <<    ; cout <<   n ; n     //for (int i = 1; i <= 20; i++) cout << arr2[i] <<    ; cout <<   n ; n     //for (int i = 1; i <= 20; i++) cout << arr3[i] <<    ; cout <<   n ; n  n     int n; n     cin >> n; n     ll res = arr3[n] * 2 % modint; n     for (int i = 0; i <= n; i++) { n         if (i == 0) res += arrleq2[n]; n         else res += arr1[n - i] * arr2[i]; n         res %= modint; n     } n     res = (res + arr2[n]) % modint; n     cout << res; n     return(0); n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     long long int q = k / (n - 1); n     long long int r = k % (n - 1); n     if (r == 0) { n       cout << n * q - 1 <<   n ; n     } else { n       cout << n * q + r <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string res, s[100005], temp, given; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, pos; n   bool valid = false; n   temp =  <3 ; n   cin >> n; n   res =   ; n   res += temp; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     res += s[i]; n     res += temp; n   } n   pos = 0; n   cin >> given; n   for (int i = 0; i < given.length(); i++) { n     if (given[i] == res[pos]) pos++; n     if (pos == res.length()) { n       valid = true; n       break; n     } n   } n   if (valid) n     cout <<  yes n ; n   else n     cout <<  no n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans, flag, a[20][300010], n, i, x, anx[20], as[20]; n void dfs(int x, int y) { n   if (y >= ans) return; n   if (x < 2) { n     anx[1] = 0; n     if (a[x][0]) { n       y++; n       anx[1] = -1; n     } n     if (a[x][2]) { n       y++; n       anx[1] = 1; n     } n     if (y < ans) { n       ans = y; n       for (int i = 1; i <= 18; i++) as[i] = anx[i]; n     } n     return; n   } n   int flag = 0; n   for (int i = 0; i <= 1 << x - 1; i++) a[x - 1][i] = 0; n   for (int i = 0; i <= 1 << x; i++) n     if (a[x][i]) { n       if (i & 1) flag = 1; n       a[x - 1][i >> 1] = 1; n     } n   anx[x] = flag; n   dfs(x - 1, y + flag); n   if (flag) { n     for (int i = 0; i <= 1 << x - 1; i++) a[x - 1][i] = 0; n     for (int i = 0; i <= 1 << x; i++) n       if (a[x][i]) a[x - 1][i + 1 >> 1] = 1; n     anx[x] = -flag; n     dfs(x - 1, y + flag); n   } n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &x), a[18][x + (1 << 17)] = 1; n   ans = 20; n   dfs(18, 0); n   printf( %d n , ans); n   for (i = 18; i >= 1; i--) n     if (as[i]) printf( %d  , as[i] * (1 << 18 - i)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   const long long inf = 1e9; n   long long n; n   cin >> n; n   vector<pair<long long, long long> > m(n); n   for (int i = 0; i < n; i++) cin >> m[i].first >> m[i].second; n   long long maxy = -inf, miny = inf, maxx = -inf, minx = inf; n   long long maxyp, maxxp, minxp, minyp; n   for (int i = 0; i < n; i++) { n     if (maxy < m[i].second) maxyp = i; n     if (maxx < m[i].first) maxxp = i; n     if (miny > m[i].second) minyp = i; n     if (minx > m[i].first) minxp = i; n     maxy = max(maxy, m[i].second); n     miny = min(miny, m[i].second); n     maxx = max(maxx, m[i].first); n     minx = min(minx, m[i].first); n   } n   long long maxans = -inf; n   for (int i = 0; i < n; i++) { n     maxans = max((maxx - min(m[maxyp].first, m[i].first) + maxy - n                   min(m[maxxp].second, m[i].second)) * n                      2, n                  maxans); n     maxans = max((maxx - min(m[minyp].first, m[i].first) + n                   max(m[maxxp].second, m[i].second) - miny) * n                      2, n                  maxans); n     maxans = max((max(m[minyp].first, m[i].first) - minx + n                   max(m[minxp].second, m[i].second) - miny) * n                      2, n                  maxans); n     maxans = max((max(m[maxyp].first, m[i].first) - minx + maxy - n                   min(m[minxp].second, m[i].second)) * n                      2, n                  maxans); n   } n   cout << maxans <<    ; n   for (int i = 4; i <= n; i++) { n     cout << (maxy - miny + maxx - minx) * 2 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2097155; n int n, mS; n char s[MAXN], t[MAXN]; n long long a[MAXN], b[MAXN], bt[MAXN]; n inline void FWT_or(long long *F, int len, int typ) { n   for (int i = 0; i < n; ++i) { n     for (int S = 0; S < len; ++S) { n       if (S >> i & 1) { n         F[S] = F[S] + typ * F[S ^ (1 << i)]; n       } n     } n   } n   return; n } n inline int popcount(int x) { n   int cnt = 0; n   while (x) { n     x -= (x & (-x)); n     cnt++; n   } n   return cnt; n } n inline void init() { n   bt[0] = 1; n   for (int i = 1; i <= n; ++i) bt[i] = bt[i - 1] * 4ll; n   return; n } n int main() { n   scanf( %d , &n); n   scanf( %s , s); n   scanf( %s , t); n   mS = (1 << n); n   init(); n   for (int i = 0; i < mS; ++i) { n     a[i] = (15ll & s[i]) << (popcount(i) << 1); n   } n   for (int i = 0; i < mS; ++i) { n     b[i] = (15ll & t[i]) << (popcount(i) << 1); n   } n   FWT_or(a, mS, 1); n   FWT_or(b, mS, 1); n   for (int i = 0; i < mS; ++i) { n     a[i] = a[i] * b[i]; n   } n   FWT_or(a, mS, -1); n   for (int i = 0; i < mS; ++i) printf( %lld , (a[i] >> (popcount(i) << 1) & 3)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n long long aa[maxn]; n long long bb[maxn]; n vector<long long> gou; n int in[maxn << 2]; n long long Z[maxn << 2]; n set<long long> ans; n int main() { n   int n; n   long long m; n   scanf( %d%lld , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %lld , &aa[i]); n   bb[1] = ((aa[1] - aa[n]) + m) % m; n   for (int i = 2; i <= n; ++i) bb[i] = ((aa[i] - aa[i - 1]) % m + m) % m; n   for (int i = n; i; --i) { n     gou.push_back(bb[i]); n     in[gou.size() - 1] = i; n   } n   gou.push_back(-1LL); n   for (int i = 1; i <= n; ++i) { n     gou.push_back(bb[i]); n     in[gou.size() - 1] = i; n   } n   for (int i = 1; i <= n; ++i) { n     gou.push_back(bb[i]); n     in[gou.size() - 1] = i; n   } n   Z[0] = gou.size(); n   for (int i = 1, j = 1, k; i < (int)gou.size(); i = k) { n     j = max(j, i); n     while (gou[j] == gou[j - i]) ++j; n     Z[i] = j - i; n     k = i + 1; n     while (k + Z[k - i] < j) { n       Z[k] = Z[k - i]; n       ++k; n     } n   } n   for (int i = 1; i < (int)gou.size(); ++i) n     if (Z[i] >= n) ans.insert((aa[in[i] - 1 ? in[i] - 1 : n] + aa[n]) % m); n   printf( %d n , (int)ans.size()); n   for (auto it = ans.begin(); it != ans.end(); ++it) printf( %lld  , *it); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 * 100 + 5; n const long long inf = 9223372036854775807; n int n, q, cnt, mark[3][maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> q; n   for (int i = 1; i <= q; i++) { n     int x, y; n     cin >> x >> y; n     if (mark[x][y] == 0) { n       mark[x][y] = 1; n       if (mark[3 - x][y] == 1) cnt++; n       if (mark[3 - x][y - 1] == 1) cnt++; n       if (mark[3 - x][y + 1] == 1) cnt++; n     } else { n       mark[x][y] = 0; n       if (mark[3 - x][y] == 1) cnt--; n       if (mark[3 - x][y - 1] == 1) cnt--; n       if (mark[3 - x][y + 1] == 1) cnt--; n     } n     if (cnt > 0) n       cout <<  No ; n     else n       cout <<  Yes ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   int n; n   scanf( %d , &n); n   int c1 = 0, c2 = 0; n   for (int i = 0; i < n; ++i) { n     int a; n     scanf( %d , &a); n     if (a == 1) n       ++c1; n     else n       ++c2; n   } n   int ans = min(c2, c1); n   c1 -= ans; n   if (c1 > 0) ans += c1 / 3; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n const int N = 1e4 + 10; n const int M = 3e6 + 10; n const int maxn = 1e6 + 10; n const int mod = 1000173169; n const double eps = 1e-10; n const double pi = acos(-1.0); n struct Tree { n   int num[N << 2], sum[N << 2]; n   void add(int rt, int l, int r, int p) { n     num[rt]++; n     sum[rt] += p; n     if (l == r) return; n     int mid = ((l + r) >> 1); n     if (p <= mid) n       add(rt << 1, l, mid, p); n     else n       add(rt << 1 | 1, mid + 1, r, p); n   } n   int getPos(int rt, int l, int r, int& val) { n     if (l == r) return l; n     int mid = ((l + r) >> 1); n     if (num[rt << 1] >= val) n       return getPos(rt << 1, l, mid, val); n     else { n       val -= num[rt << 1]; n       return getPos(rt << 1 | 1, mid + 1, r, val); n     } n   } n   int getSum(int rt, int l, int r, int pos) { n     if (r <= pos) return sum[rt]; n     int mid = ((l + r) >> 1); n     int res = 0; n     if (l <= pos) res = getSum(rt << 1, l, mid, pos); n     if (mid < pos) res += getSum(rt << 1 | 1, mid + 1, r, pos); n     return res; n   } n } seg; n int n, m, k; n vector<int> id; n vector<pair<int, int> > a[3], b, p, res; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   int mx = 1e4; n   for (int i = 1; i <= n; i++) { n     int t, x, y; n     cin >> t >> x >> y; n     x = x << 1 | y; n     if (x == 3) n       b.emplace_back(t, i); n     else if (x) n       a[x].emplace_back(t, i); n     else { n       seg.add(1, 1, mx, t); n       res.emplace_back(t, i); n     } n   } n   sort(a[1].begin(), a[1].end()); n   sort(a[2].begin(), a[2].end()); n   sort(b.begin(), b.end()); n   int cnt1 = 0, cnt2 = 0; n   int t1 = min(min(((int)a[1].size()), ((int)a[2].size())), k); n   int cur = 0; n   for (int i = 0; i < t1; i++) { n     cur += a[1][i].first + a[2][i].first; n     p.emplace_back(a[1][i].first, a[2][i].first); n     cnt1++; n     cnt2 += 2; n   } n   for (int i = t1; i < max(((int)a[1].size()), ((int)a[2].size())); i++) { n     if (i < ((int)a[1].size())) n       seg.add(1, 1, mx, a[1][i].first), res.push_back(a[1][i]); n     if (i < ((int)a[2].size())) n       seg.add(1, 1, mx, a[2][i].first), res.push_back(a[2][i]); n   } n   for (int i = k; i < ((int)b.size()); i++) { n     seg.add(1, 1, mx, b[i].first); n     res.push_back(b[i]); n   } n   int P = -1; n   long long ans = INF; n   int t2 = min(((int)b.size()), k); n   for (int i = 0; i <= t2; i++) { n     if (cnt1 == k && cnt2 <= m) { n       long long res = INF; n       if (cnt2 == m) n         res = cur; n       else if (cnt2 + seg.num[1] >= m) { n         int val = m - cnt2; n         int pos = seg.getPos(1, 1, mx, val); n         res = pos * val + cur; n         if (pos > 1) res += seg.getSum(1, 1, mx, pos - 1); n       } n       if (ans > res) { n         ans = res; n         P = i; n       } n     } n     if (i < t2) { n       if (cnt1 < k) { n         cur += b[i].first; n         cnt1++; n         cnt2++; n       } else if (!p.empty()) { n         pair<int, int> t = p.back(); n         p.pop_back(); n         cur -= t.first + t.second; n         seg.add(1, 1, mx, t.first); n         seg.add(1, 1, mx, t.second); n         cur += b[i].first; n         cnt2--; n       } n     } n   } n   if (ans == INF) n     cout <<  -1 n ; n   else { n     cout << ans <<   n ; n     for (int i = 0; i < P; i++) id.push_back(b[i].second); n     for (int i = 0; i < t1; i++) { n       if (P + i + 1 <= k) { n         id.push_back(a[1][i].second); n         id.push_back(a[2][i].second); n       } else { n         res.push_back(a[1][i]); n         res.push_back(a[2][i]); n       } n     } n     sort(res.begin(), res.end()); n     for (int i = 0; i < ((int)res.size()); i++) { n       if (((int)id.size()) == m) break; n       id.push_back(res[i].second); n     } n     for (int v : id) cout << v <<    ; n     cout <<   n ; n     assert(((int)id.size()) == m); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long num = 0; n   int fix = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) fix = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     num = num * 10 + ch -  0 ; n     ch = getchar(); n   } n   return num * fix; n } n const int mod = 1e9 + 7; n const int MAXN = 50; n int N, K; n int W[MAXN + 1]; n long long fact[MAXN + 1], inv[MAXN + 1]; n struct data { n   int x, y, pos; n }; n long long power(long long x, long long y) { n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return res; n } n void initial() { n   fact[0] = 1; n   for (int i = 1; i <= MAXN; i++) { n     fact[i] = fact[i - 1] * i % mod; n   } n   inv[MAXN] = power(fact[MAXN], mod - 2); n   for (int i = MAXN - 1; i >= 0; i--) { n     inv[i] = inv[i + 1] * (i + 1) % mod; n   } n } n int flag = -1; n int dp[MAXN + 1][MAXN + 1][2]; n long long num[MAXN + 1][MAXN + 1][2]; n long long C(int x, int y) { return fact[y] * inv[x] % mod * inv[y - x] % mod; } n void BFS(int a, int b) { n   memset(dp, -1, sizeof(dp)); n   dp[a][b][0] = 0, num[a][b][0] = 1; n   queue<data> que; n   que.push((data){a, b, 0}); n   while (!que.empty()) { n     if (flag != -1 && que.front().pos == flag) break; n     data cur = que.front(); n     que.pop(); n     int x, y, fix; n     if (cur.pos) n       x = a - cur.x, y = b - cur.y, fix = 1; n     else n       x = cur.x, y = cur.y, fix = -1; n     for (int i = 0; i <= x; i++) { n       for (int j = 0; j <= y; j++) { n         if (i + j == 0) continue; n         if (i * 50 + j * 100 > K) continue; n         int newx = cur.x + i * fix, newy = cur.y + j * fix; n         if (dp[newx][newy][cur.pos ^ 1] != -1) { n           if (dp[newx][newy][cur.pos ^ 1] == dp[cur.x][cur.y][cur.pos] + 1) { n             (num[newx][newy][cur.pos ^ 1] += n              num[cur.x][cur.y][cur.pos] * C(i, x) % mod * C(j, y) % mod) %= mod; n           } n           continue; n         } n         dp[newx][newy][cur.pos ^ 1] = dp[cur.x][cur.y][cur.pos] + 1; n         num[newx][newy][cur.pos ^ 1] = n             num[cur.x][cur.y][cur.pos] * C(i, x) % mod * C(j, y) % mod; n         if (newx + newy == 0) { n           flag = !cur.pos; n         } n         que.push((data){newx, newy, cur.pos ^ 1}); n       } n     } n   } n } n void solve() { n   int cnt50 = 0, cnt100 = 0; n   for (int i = 1; i <= N; i++) { n     if (W[i] == 50) cnt50++; n     if (W[i] == 100) cnt100++; n   } n   BFS(cnt50, cnt100); n   if (flag == -1) { n     puts( -1 ); n     puts( 0 ); n     return; n   } n   printf( %d n , dp[0][0][flag]); n   printf( %lld n , num[0][0][flag]); n } n int main() { n   initial(); n   N = read(), K = read(); n   for (int i = 1; i <= N; i++) { n     W[i] = read(); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char b[101]; n char a[101][101]; n int mark[101]; n int main() { n   int n, m, s = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       if (a[i][j] > b[j]) { n         b[j] = a[i][j]; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[i][j] == b[j]) { n         if (mark[i] == 0) { n           s++; n           mark[i]++; n         } n       } n     } n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 0x3f3f3f3f; n const double eps = 1e-8; n const int MAXN = 50 + 10; n const int MOD = 29; n const int dir[][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; n int cases = 0; n int vis[MAXN][MAXN], row, col; n char mp[MAXN][MAXN]; n bool DFS(int x, int y, char color, int step) { n   for (int i = 0; i < 4; i++) { n     int xx = x + dir[i][0], yy = y + dir[i][1]; n     if (xx < 0 || xx >= row || yy < 0 || yy >= col) continue; n     if (mp[xx][yy] != color) continue; n     if (vis[xx][yy]) { n       if (mp[xx][yy] != color || step - vis[xx][yy] == 1) continue; n       return true; n     } n     vis[xx][yy] = step + 1; n     if (DFS(xx, yy, color, step + 1)) return true; n   } n   return false; n } n int main() { n   scanf( %d%d , &row, &col); n   for (int i = 0; i < row; i++) scanf( %s , mp[i]); n   bool flag = false; n   for (int i = 0; i < row; i++) n     for (int j = 0; j < col; j++) { n       if (!vis[i][j]) { n         vis[i][j] = 1; n         flag = DFS(i, j, mp[i][j], 1); n       } n       if (flag) { n         puts( Yes ); n         return 0; n       } n     } n   puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n const int MAX_N = 20; n int n, k, t; n int e[MAX_N + 1][MAX_N + 1]; n int choosen[MAX_N + 1]; n int mid; n int cnt; n void dfs(int x, int now_cost_sum) { n   if (x > n) { n     cnt++; n     return; n   } n   dfs(x + 1, now_cost_sum); n   for (int y = 1; y <= n && cnt < t; y++) { n     if (!choosen[y] && e[x][y] && now_cost_sum + e[x][y] <= mid) { n       choosen[y] = 1; n       dfs(x + 1, now_cost_sum + e[x][y]); n       choosen[y] = 0; n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &k, &t); n   for (int i = 1; i <= k; i++) { n     int h, w, r; n     scanf( %d%d%d , &h, &w, &r); n     e[h][w] = r; n   } n   int low = 0, high = 1000 * n; n   while (low < high) { n     mid = (low + high) >> 1; n     cnt = 0; n     dfs(1, 0); n     if (cnt >= t) n       high = mid; n     else n       low = mid + 1; n   } n   printf( %d n , low); n } n


#include <bits/stdc++.h> n using namespace std; n map<string, long long> mp; n string S[1006]; n long long cost[1006]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int N, i, j, k; n   cin >> N; n   mp[ A ] = 1e9; n   mp[ B ] = 1e9; n   mp[ C ] = 1e9; n   mp[ AB ] = 1e9; n   mp[ AC ] = 1e9; n   mp[ BC ] = 1e9; n   mp[ ABC ] = 1e9; n   for (i = 1; i <= N; i++) { n     cin >> cost[i] >> S[i]; n     sort(S[i].begin(), S[i].end()); n     mp[S[i]] = min(mp[S[i]], cost[i]); n   } n   mp[ AB ] = min(mp[ AB ], mp[ A ] + mp[ B ]); n   mp[ AC ] = min(mp[ AC ], mp[ A ] + mp[ C ]); n   mp[ BC ] = min(mp[ BC ], mp[ B ] + mp[ C ]); n   mp[ ABC ] = min(mp[ ABC ], mp[ AB ] + mp[ C ]); n   mp[ ABC ] = min(mp[ ABC ], mp[ A ] + mp[ BC ]); n   mp[ ABC ] = min(mp[ ABC ], mp[ AC ] + mp[ B ]); n   mp[ ABC ] = min(mp[ ABC ], mp[ AB ] + mp[ BC ]); n   mp[ ABC ] = min(mp[ ABC ], mp[ AC ] + mp[ BC ]); n   mp[ ABC ] = min(mp[ ABC ], mp[ AB ] + mp[ AC ]); n   mp[ ABC ] = min(mp[ ABC ], mp[ A ] + mp[ B ] + mp[ C ]); n   if (mp[ ABC ] >= (long long)1e9) n     printf( -1 n ); n   else n     printf( %d n , mp[ ABC ]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n const int MOD = 1e9 + 7; n int n, m; n vector<int> tmp; n vector<int> g[N]; n int cnt1[N]; n int marked[N]; n int f[N]; n map<vector<int>, int> cnt; n int product(int a, int b) { return (1ll * a * b) % MOD; } n void mult(int &a, int b) { a = (1ll * a * b) % MOD; } n int main() { n   f[0] = 1; n   for (int i = 1; i < N; i++) f[i] = product(f[i - 1], i); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int k; n     scanf( %d , &k); n     for (int j = 0; j < k; j++) { n       int x; n       scanf( %d , &x); n       g[x].push_back(i); n     } n   } n   for (int i = 1; i <= m; i++) cnt[g[i]]++; n   int ans = 1; n   for (auto p : cnt) { n     mult(ans, f[p.second]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:1000000000 ) n static long long m1[70][70]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   m1[0][0] = 1; n   for (int i = 1; i <= 30; ++i) { n     m1[i][0] = 1; n     for (int j = 1; j <= 30; ++j) m1[i][j] = m1[i - 1][j - 1] + m1[i - 1][j]; n   } n   long long n, m, t, r = 0; n   cin >> n >> m >> t; n   for (int i = 4; i < t; ++i) n     if (t - i >= 0) r += m1[n][i] * m1[m][t - i]; n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n char in[2010][2010]; n int deg[2010][2010]; n pair<int, int> Q[4000010]; n int QL, QR, FREE; n int sft[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; n int get(int x, int y) { n   if (x < 0 || x >= N || y < 0 || y >= M) return  * ; n   return in[x][y]; n } n void say_no() { n   puts( Not unique ); n   exit(0); n } n void put(pair<int, int> a, pair<int, int> b) { n   if (a > b) swap(a, b); n   if (a.first == b.first) n     in[a.first][a.second] =  < , in[b.first][b.second] =  > ; n   else n     in[a.first][a.second] =  ^ , in[b.first][b.second] =  v ; n } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 0; i < N; ++i) scanf( %s , &in[i]); n   for (int i = 0; i < N; ++i) n     for (int j = 0; j < M; ++j) { n       if (in[i][j] ==  * ) n         deg[i][j] = -1; n       else n         for (int p = 0; p < 4; ++p) n           if (get(i + sft[p][0], j + sft[p][1]) ==  . ) ++deg[i][j]; n       if (deg[i][j] == 1) Q[QR++] = make_pair(i, j); n       if (deg[i][j] >= 0) ++FREE; n     } n   while (QL < QR) { n     pair<int, int> a = Q[QL++]; n     if (deg[a.first][a.second] != 1) continue; n     pair<int, int> b(-1, -1); n     for (int p = 0; p < 4; ++p) n       if (get(a.first + sft[p][0], a.second + sft[p][1]) ==  . ) n         b = make_pair(a.first + sft[p][0], a.second + sft[p][1]); n     if (b.first < 0) say_no(); n     deg[a.first][a.second] = -1; n     deg[b.first][b.second] = -1; n     put(a, b); n     FREE -= 2; n     for (int p = 0; p < 4; ++p) n       if (get(a.first + sft[p][0], a.second + sft[p][1]) ==  . ) { n         int x = a.first + sft[p][0]; n         int y = a.second + sft[p][1]; n         --deg[x][y]; n         if (deg[x][y] == 1) Q[QR++] = make_pair(x, y); n       } n     for (int p = 0; p < 4; ++p) n       if (get(b.first + sft[p][0], b.second + sft[p][1]) ==  . ) { n         int x = b.first + sft[p][0]; n         int y = b.second + sft[p][1]; n         --deg[x][y]; n         if (deg[x][y] == 1) Q[QR++] = make_pair(x, y); n       } n   } n   if (FREE != 0) say_no(); n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j < M; ++j) printf( %c , in[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, a[1111], x, n, s, y, f1, f2; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     if (x == 0 || x == 100) n       a[++s] = x; n     else if (x < 10 && f1 == 0) n       a[++s] = x, f1 = 1; n     else if (x % 10 == 0 && f2 == 0) n       a[++s] = x, f2 = 1; n     else n       y = x; n   } n   if (f1 == 0 && f2 == 0 && y != 0) a[++s] = y; n   cout << s <<   n ; n   for (i = 1; i <= s; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n int sum, mx; n vector<int> ans; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = (1); i <= (n); i++) { n     if (i == 1) { n       cin >> p; n       ans.push_back(1); n       sum += p; n       mx += p; n     } else { n       int x; n       cin >> x; n       mx += x; n       if (x <= p / 2) { n         ans.push_back(i); n         sum += x; n       } n     } n   } n   if (sum <= mx / 2) n     cout << 0; n   else { n     cout << (int)ans.size() <<   n ; n     for (int x : ans) cout << x <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 9; n struct Point { n   int col, id; n   int x, y; n   bool operator<(const Point& b) { n     if (x == b.x) return y < b.y; n     return x < b.x; n   } n } p0, p[N * 2]; n int cross(Point a, Point b, Point c) { n   return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); n } n bool cmpang(Point a, Point b) { return cross(p0, a, b) > 0; } n int ans[N]; n void solve(int L, int R) { n   int c = L; n   for (int i = L; i <= R; ++i) n     if (p[i] < p[c]) c = i; n   swap(p[c], p[L]); n   p0 = p[L]; n   sort(p + L + 1, p + R + 1, cmpang); n   int a1 = 0, a0 = 0; n   int k = 1000000; n   int mid = (L + R) >> 1; n   for (int i = L + 1; i <= R; ++i) { n     if (a1 == a0 && p[i].col != p0.col) { n       k = i; n       break; n     } n     if (p[i].col) n       ++a1; n     else n       ++a0; n   } n   if (p0.col) n     ans[p[k].id] = p0.id; n   else n     ans[p0.id] = p[k].id; n   if (L + 1 < k - 1) solve(L + 1, k - 1); n   if (k + 1 < R) solve(k + 1, R); n } n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int main() { n   int n = read(); n   for (int i = 1; i <= n; ++i) { n     p[i].x = read(); n     p[i].y = read(); n     p[i].col = 0; n     p[i].id = i; n   } n   for (int i = 1; i <= n; ++i) { n     p[i + n].x = read(); n     p[i + n].y = read(); n     p[i + n].col = 1; n     p[i + n].id = i; n   } n   solve(1, 2 * n); n   for (int i = 1; i <= n; ++i) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void factorize(long long v, std::map<long long, int> &result) { n   long long p = 2; n   while (v > 1 && p * p <= v) { n     int cnt = 0; n     while (v % p == 0) { n       cnt++; n       v /= p; n     } n     if (cnt > 0) { n       if (result.count(p) == 0) { n         result[p] = 0; n       } n       result[p] += cnt; n     } n     p += p == 2 ? 1 : 2; n   } n   if (v > 1) { n     if (result.count(v) == 0) { n       result[v] = 0; n     } n     result[v] += 1; n   } n } n long long int ex_gcd(long long int x, long long int y, long long int &a, n                      long long int &b) { n   if (y == 0) { n     a = 1; n     b = 0; n     return x; n   } n   long long int q = x / y; n   long long int r = x % y; n   long long int res = ex_gcd(y, r, a, b); n   long long int tmp = a - q * b; n   a = b; n   b = tmp; n   return res; n } n const int N = 123456; n int n; n long long int p; n vector<long long int> q; n vector<int> e; n long long int fact[N]; n long long int invfact[N]; n vector<int> fact_e[N]; n void init(void) { n   map<long long int, int> factor; n   factorize(p, factor); n   for (map<long long int, int>::iterator it = factor.begin(); n        it != factor.end(); ++it) { n     q.push_back(it->first); n     e.push_back(it->second); n   } n   int l = e.size(); n   fact_e[0] = vector<int>(l, 0); n   fact[0] = invfact[0] = 1; n   for (int i = (int)(1); i < (int)(N); i++) { n     int v = i; n     fact_e[i] = fact_e[i - 1]; n     for (int j = (int)(0); j < (int)(l); j++) { n       while (v % q[j] == 0) { n         fact_e[i][j] += 1; n         v /= q[j]; n       } n     } n     fact[i] = fact[i - 1] * v % p; n     long long int dummy2; n     long long int dummy = ex_gcd(fact[i], p, invfact[i], dummy2); n     assert(dummy == 1); n     invfact[i] = (invfact[i] % p + p) % p; n   } n } n long long int tri_comb(int x, int y) { n   long long int t = 1; n   int len = e.size(); n   t = (fact[n] * invfact[x]) % p; n   t = t * invfact[y] % p; n   t = t * invfact[n - x - y] % p; n   vector<int> es(len); n   for (int i = (int)(0); i < (int)(len); i++) { n     es[i] = fact_e[n][i] - fact_e[x][i] - fact_e[y][i] - fact_e[n - x - y][i]; n     for (int j = (int)(0); j < (int)(es[i]); j++) { n       t = t * q[i] % p; n     } n   } n   return t; n } n long long int calc(int l) { n   long long int tot = 0; n   for (int i = (int)(0); i < (int)(n - l + 1); i++) { n     if ((n + i + l) % 2 == 0) { n       tot = (tot + tri_comb(i, (n - i - l) / 2)) % p; n     } n   } n   return tot; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int l, r; n   cin >> n >> p >> l >> r; n   init(); n   cout << (calc(l) + calc(l + 1) - calc(r + 1) - calc(r + 2) + 2 * p) % p n        <<   n ; n } n


#include <bits/stdc++.h> n int main() { n   char x[205]; n   scanf( %s , x); n   for (int i = 0; i < strlen(x); i++) { n     if (x[i] ==  . ) { n       x[i] =  0 ; n       printf( %c , x[i]); n     } n     if (x[i] ==  -  && x[i + 1] ==  . ) { n       x[i] =  1 ; n       printf( %c , x[i]); n       i = i + 1; n     } n     if (x[i] ==  -  && x[i + 1] ==  - ) { n       x[i] =  2 ; n       printf( %c , x[i]); n       i = i + 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const int LOGN = 18; n int n, q, p[N], stk[N], L[LOGN][N], R[LOGN][N]; n long long a[N], b[N], LC[LOGN][N], RC[LOGN][N]; n inline long long getdist(int u, int v) { n  long long ret = 0; n  for (int i = LOGN - 1; i >= 0; i--) if (R[i][u] <= v) { n   ret += RC[i][u]; n   u = R[i][u]; n  } n  if (u == v) return ret; n  for (int i = LOGN - 1; i >= 0; i--) if (L[i][v] >= u) { n   ret += LC[i][v]; n   v = L[i][v]; n  } n  return ret; n } n int main() { n  scanf( %d%d , &n, &q); n  for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n  p[n + 1] = n + 1; n  for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n  for (int i = 1; i <= n; i++) scanf( %lld , &b[i]); n  int tp = 0; n  stk[++tp] = n + 1; n  for (int j = 0; j < LOGN; j++) { n   R[j][n + 1] = n + 1; n   RC[j][n + 1] = 0; n  } n  for (int i = n; i >= 1; i--) { n   while (tp && p[stk[tp]] < p[i]) tp--; n   R[0][i] = stk[tp]; n   RC[0][i] = b[i] = min(b[i], a[i] + getdist(i + 1, stk[tp])); n   for (int j = 1; j < LOGN; j++) { n    R[j][i] = R[j - 1][R[j - 1][i]]; n    RC[j][i] = RC[j - 1][i] + RC[j - 1][R[j - 1][i]]; n   } n   stk[++tp] = i; n  } n  tp = 0; n  stk[++tp] = 1; n  for (int j = 0; j < LOGN; j++) { n   L[j][1] = L[j][0] = 0; n   LC[j][1] = 0; n  } n  for (int i = 2; i <= n + 1; i++) { n   while (tp && p[stk[tp]] < p[i]) tp--; n   L[0][i] = tp ? stk[tp] : 0; n   LC[0][i] = tp ? a[stk[tp]] + getdist(stk[tp] + 1, i) : 0; n   for (int j = 1; j < LOGN; j++) { n    L[j][i] = L[j - 1][L[j - 1][i]]; n    LC[j][i] = LC[j - 1][i] + LC[j - 1][L[j - 1][i]]; n   } n   stk[++tp] = i; n  } n  set<int> st; n  st.insert(1); n  st.insert(n + 1); n  long long sum = getdist(1, n + 1); n  while (q--) { n   int x; n   scanf( %d , &x); n   if (x != 1) { n    if (st.count(x)) { n     auto it = st.find(x); n     int prv = *prev(it), nxt = *next(it); n     sum += getdist(prv, nxt); n     sum -= getdist(prv, x); n     sum -= getdist(x, nxt); n     st.erase(it); n    } else { n     auto it = st.insert(x).first; n     int prv = *prev(it), nxt = *next(it); n     sum -= getdist(prv, nxt); n     sum += getdist(prv, x); n     sum += getdist(x, nxt); n    } n   } n   printf( %lld n , sum); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cout << x; } n void __print(long x) { cout << x; } n void __print(long long x) { cout << x; } n void __print(unsigned x) { cout << x; } n void __print(unsigned long x) { cout << x; } n void __print(unsigned long long x) { cout << x; } n void __print(float x) { cout << x; } n void __print(double x) { cout << x; } n void __print(long double x) { cout << x; } n void __print(char x) { cout <<      << x <<     ; } n void __print(const char *x) { cout <<     << x <<    ; } n void __print(const string &x) { cout <<     << x <<    ; } n void __print(bool x) { cout << (x ?  true  :  false ); } n void _print() { cout <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n const long long int dx4[4] = {+1, -1, 0, 0}; n const long long int dy4[4] = {0, 0, +1, -1}; n const long long int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n const long long int dy8[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n const long long int MOD = 1e9 + 7; n const long long int INF = 1e10; n const long long int N = 2e5 + 5; n void solve() { n   long long int n; n   string s; n   cin >> n >> s; n   map<char, long long int> cnt; n   for (auto x : s) cnt[x]++; n   for (auto x : cnt) n     while (x.second) cout << x.first, x.second--; n   return; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 450000 + 100; n int n, k; n vector<int> pos[maxn]; n int a[maxn]; n int b[maxn]; n int v[maxn]; n int main() { n   while (cin >> n >> k) { n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       v[i] = a[i]; n       pos[i].push_back(0); n     } n     pos[0].push_back(0); n     int tot = n; n     sort(v + 1, v + n + 1); n     tot = unique(v + 1, v + n + 1) - (v + 1); n     for (int i = 1; i <= n; i++) { n       a[i] = lower_bound(v + 1, v + 1 + tot, a[i]) - (v + 1) + 1; n     } n     long long mx = 0, ans = 0; n     for (int i = 1; i <= n; i++) { n       int v = a[i]; n       pos[v].push_back(i); n       int sz = pos[v].size(); n       while (b[v] + k < sz) { n         b[v]++; n       } n       mx = max(mx, (long long)pos[v][b[v]]); n       ans += mx; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long ans, i, l, n, d, m; n int main() { n   cin >> n >> m; n   for (i = 1; i < n; i++) { n     cin >> s; n   } n   cin >> s; n   for (i = 1; i < s.size(); i++) { n     if (s[i] != s[i - 1]) ans++; n   } n   if (ans % 2 == 0) { n     ans /= 2; n     if (s[0] ==  B ) ans++; n   } else { n     ans++; n     ans /= 2; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void input(T &x) { n   register char c = getchar(); n   x = 0; n   int neg = 0; n   for (; ((c < 48 || c > 57) && c !=  - ); c = getchar()) n     ; n   if (c ==  - ) { n     neg = 1; n     c = getchar(); n   } n   for (; c > 47 && c < 58; c = getchar()) { n     x = (x << 1) + (x << 3) + c - 48; n   } n   if (neg) x = -x; n } n inline long long bigmod(long long p, long long e, long long M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n const int N = 1e6 + 6; n char str[N], inp[26]; n int qu[10001]; n int main() { n   scanf( %s , str); n   int n = strlen(str); n   int q; n   input(q); n   int mask = 0; n   unordered_map<int, int> Map; n   for (int t = 0; t < q; t++) { n     scanf( %s  , inp); n     mask = 0; n     for (int i = 0; inp[i]; i++) { n       mask |= 1 << inp[i] -  a ; n     } n     qu[t] = mask; n     Map[mask] = 0; n   } n   for (int i = 0; i < n; i++) { n     mask = 0; n     for (int j = i; j < n; j++) { n       if (i < j && str[i] == str[j]) break; n       mask |= 1 << str[j] -  a ; n       if (j + 1 == n || (mask & 1 << str[j + 1] -  a ) == 0) n         if (Map.count(mask)) Map[mask]++; n     } n   } n   for (int i = 0; i < q; i++) { n     printf( %d n , Map[qu[i]]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lli = long long int; n using pii = pair<int, int>; n using vi = vector<int>; n using vb = vector<bool>; n using vvi = vector<vector<int>>; n using vlli = vector<long long int>; n using vpii = vector<pair<int, int>>; n lli n, m, k, u, v, x, ans, curr, T; n vlli c; n lli fast_exp(lli base, lli exp) { n   lli res = 1; n   base = base % 1000000007; n   while (exp > 0) { n     if (exp % 2) res = (res * base) % 1000000007; n     base = (base * base) % 1000000007; n     exp /= 2; n   } n   return res % 1000000007; n } n template <class T> n struct DSU { n   map<T, T> par; n   T comp_sz = n; n   T get(T u) { n     if (par.find(u) == par.end()) return u; n     return par[u] = get(par[u]); n   } n   void unite(T x, T y) { n     T X = get(x), Y = get(y); n     if (X == Y) return; n     par[Y] = X; n     --comp_sz; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   c.resize(n + 1); n   map<lli, DSU<lli>> virus; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1; i <= m; i++) { n     cin >> u >> v; n     x = c[u] ^ c[v]; n     virus[x].unite(u, v); n   } n   ans = ((fast_exp(2, k) - virus.size() + 1000000007) * fast_exp(2, n)) % n         1000000007; n   for (auto i : virus) { n     curr = (i.second).comp_sz; n     ans = (ans + fast_exp(2, curr)) % 1000000007; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n void solve() { n   int m; n   cin >> n >> m; n   vector<string> a(n); n   for (int i = (0); i <= int(n - 1); i++) cin >> a[i]; n   if (n < 3) { n     cout << -1; n     return; n   } n   int ans = 0; n   vector<vector<int>> dp(n * m, vector<int>(4, 0)); n   vector<vector<int>> res; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  * ) { n         dp[i * m + j][0] = 1 + (i - 1 >= 0 ? dp[(i - 1) * m + j][0] : 0); n       } n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  * ) { n         dp[i * m + j][1] = 1 + (i + 1 < n ? dp[(i + 1) * m + j][1] : 0); n       } n     } n   } n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) { n       if (a[j][i] ==  * ) { n         dp[j * m + i][2] = 1 + (i - 1 >= 0 ? dp[(j)*m + i - 1][2] : 0); n       } n     } n   } n   for (int i = m - 1; i >= 0; i--) { n     for (int j = 0; j < n; j++) { n       if (a[j][i] ==  * ) { n         dp[j * m + i][3] = 1 + (i + 1 < m ? dp[(j)*m + i + 1][3] : 0); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int idx = i * m + j; n       int mini = min({dp[idx][0], dp[idx][1], dp[idx][2], dp[idx][3]}); n       if (mini >= 2) { n         mini--; n         for (int k = i - mini; k <= i + mini; k++) { n           a[k][j] =  . ; n         } n         for (int k = j - mini; k <= j + mini; k++) { n           a[i][k] =  . ; n         } n         res.push_back({i + 1, j + 1, mini}); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  * ) { n         cout << -1; n         return; n       } n     } n   } n   cout << res.size() <<   n ; n   for (int i = 0; i < res.size(); i++) { n     cout << res[i][0] <<     << res[i][1] <<     << res[i][2] <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1111], f[1111]; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int day = 1000; day >= 1; --day) { n     for (int i = 0; i < n; ++i) f[i] = 0; n     f[0] = (day <= a[0]); n     for (int i = 1; i < n; ++i) { n       if (day > a[i]) continue; n       f[i] |= f[i - 1]; n       if (i > 1) f[i] |= f[i - 2]; n     } n     if (f[n - 1]) { n       cout << day << endl; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n string str, queries[100010]; n int n; n unsigned long long values[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; n unsigned long long length[10] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}; n int main() { n   cin >> str >> n; n   queries[0] =  0->  + str; n   for (int i = 0; i < n; i++) { n     cin >> queries[i + 1]; n   } n   for (int i = n; i >= 0; i--) { n     unsigned long long len = 1, val = 0; n     for (int j = 3; j < queries[i].size(); ++j) { n       int d = queries[i][j] -  0 ; n       val = (val * length[d] + values[d]) % 1000000007; n       len = len * length[d] % 1000000007; n     } n     length[queries[i][0] -  0 ] = len; n     values[queries[i][0] -  0 ] = val; n   } n   cout << values[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h, m; n   while (scanf( %d:%d , &h, &m) == 2) { n     int a; n     scanf( %d , &a); n     int t = (h * 60 + m + a) % (24 * 60); n     printf( %02d:%02d n , t / 60, t % 60); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 1000 * 1007; n long long int revi(long long int i) { n   long long int rev = 0; n   while (i > 0) { n     rev = (rev * 10) + (i % 10); n     i = i / 10; n   } n   return rev; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   long long int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] = abs(a[i]); n   } n   sort(a, a + n); n   long long int l = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     while (2 * a[l] < a[i]) l++; n     ans = ans + (i - l); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string str; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> str; n     sort(str.begin(), str.end()); n     for (int j = 0; j < str.size() - 1; j++) { n       if (str[j + 1] - str[j] != 1) { n         cout <<  NO  n              <<   n ; n         break; n       } else if (j == str.size() - 2) { n         cout <<  YES  n              <<   n ; n       } n     } n     if (str.size() == 1) { n       cout <<  YES  n            <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2 * 1000 * 100 + 100; n long long nums[maxn]; n long long ans = 0; n long long cou[30]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> nums[i]; n     cou[nums[i] + 10]++; n   } n   ans += (cou[0 + 10] * (cou[10] - 1)) / 2; n   for (int i = 1; i < 11; i++) { n     ans += cou[i + 10] * cou[-i + 10]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int l, r; n   cin >> l >> r; n   if (l == r) { n     cout << l << endl; n     return 0; n   } else { n     cout <<  2  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 8; n char a[N][N + 1]; n int d[N * N][N][N]; n inline int in(int x) { return x >= 0 && x < N; } n int main() { n   for (int i = 0; i < N; ++i) scanf( %s , a[i]); n   for (int i = 0; i < N; ++i) n     for (int j = 0; j < N; ++j) d[0][i][j] = (a[i][j] ==  M ); n   for (int i = 0; i + 1 < N * N; ++i) { n     for (int j = 0; j < N; ++j) n       for (int k = 0; k < N; ++k) { n         if (a[j][k] ==  A  && d[i][j][k]) { n           cout <<  WIN n ; n           return 0; n         } n         if (a[j][k] ==  S  && in(j + i)) d[i][j + i][k] = 0; n       } n     for (int j = 0; j < N; ++j) n       for (int k = 0; k < N; ++k) n         if (d[i][j][k]) n           for (int dx = -1; dx <= 1; ++dx) n             for (int dy = -1; dy <= 1; ++dy) n               if (in(j + dx) && in(k + dy) && n                   (!in(j + dx - i) || a[j + dx - i][k + dy] !=  S )) n                 d[i + 1][j + dx][k + dy] = 1; n   } n   printf( LOSE n ); n   return 0; n } n